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 "util/u_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 %14s ", 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 regmem
.mod
!= mod_REG
) {
245 emit_1ub(p
, 0x24); /* simplistic! */
248 switch (regmem
.mod
) {
253 emit_1b(p
, (char) regmem
.disp
);
256 emit_1i(p
, regmem
.disp
);
264 /* Emits the "/0".."/7" specialized versions of the modrm ("/r") bytes.
266 static void emit_modrm_noreg( struct x86_function
*p
,
268 struct x86_reg regmem
)
270 struct x86_reg dummy
= x86_make_reg(file_REG32
, op
);
271 emit_modrm(p
, dummy
, regmem
);
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.
279 static void emit_op_modrm( struct x86_function
*p
,
280 unsigned char op_dst_is_reg
,
281 unsigned char op_dst_is_mem
,
287 emit_1ub(p
, op_dst_is_reg
);
288 emit_modrm(p
, dst
, src
);
293 assert(src
.mod
== mod_REG
);
294 emit_1ub(p
, op_dst_is_mem
);
295 emit_modrm(p
, src
, dst
);
309 /* Create and manipulate registers and regmem values:
311 struct x86_reg
x86_make_reg( enum x86_reg_file file
,
312 enum x86_reg_name idx
)
324 struct x86_reg
x86_make_disp( struct x86_reg reg
,
327 assert(reg
.file
== file_REG32
);
329 if (reg
.mod
== mod_REG
)
334 if (reg
.disp
== 0 && reg
.idx
!= reg_BP
)
335 reg
.mod
= mod_INDIRECT
;
336 else if (reg
.disp
<= 127 && reg
.disp
>= -128)
339 reg
.mod
= mod_DISP32
;
344 struct x86_reg
x86_deref( struct x86_reg reg
)
346 return x86_make_disp(reg
, 0);
349 struct x86_reg
x86_get_base_reg( struct x86_reg reg
)
351 return x86_make_reg( reg
.file
, reg
.idx
);
354 int x86_get_label( struct x86_function
*p
)
356 return p
->csr
- p
->store
;
361 /***********************************************************************
366 void x86_jcc( struct x86_function
*p
,
370 int offset
= label
- (x86_get_label(p
) + 2);
374 /*assert(p->csr - p->store > -offset);*/
375 if (p
->csr
- p
->store
<= -offset
) {
376 /* probably out of memory (using the error_overflow buffer) */
381 if (offset
<= 127 && offset
>= -128) {
382 emit_1ub(p
, 0x70 + cc
);
383 emit_1b(p
, (char) offset
);
386 offset
= label
- (x86_get_label(p
) + 6);
387 emit_2ub(p
, 0x0f, 0x80 + cc
);
392 /* Always use a 32bit offset for forward jumps:
394 int x86_jcc_forward( struct x86_function
*p
,
398 emit_2ub(p
, 0x0f, 0x80 + cc
);
400 return x86_get_label(p
);
403 int x86_jmp_forward( struct x86_function
*p
)
408 return x86_get_label(p
);
411 int x86_call_forward( struct x86_function
*p
)
417 return x86_get_label(p
);
420 /* Fixup offset from forward jump:
422 void x86_fixup_fwd_jump( struct x86_function
*p
,
425 *(int *)(p
->store
+ fixup
- 4) = x86_get_label(p
) - fixup
;
428 void x86_jmp( struct x86_function
*p
, int label
)
432 emit_1i(p
, label
- x86_get_label(p
) - 4);
435 void x86_call( struct x86_function
*p
, struct x86_reg reg
)
439 emit_modrm_noreg(p
, 2, reg
);
443 void x86_mov_reg_imm( struct x86_function
*p
, struct x86_reg dst
, int imm
)
446 assert(dst
.file
== file_REG32
);
447 assert(dst
.mod
== mod_REG
);
448 emit_1ub(p
, 0xb8 + dst
.idx
);
453 * Immediate group 1 instructions.
456 x86_group1_imm( struct x86_function
*p
,
457 unsigned op
, struct x86_reg dst
, int imm
)
459 assert(dst
.file
== file_REG32
);
460 assert(dst
.mod
== mod_REG
);
461 if(-0x80 <= imm
&& imm
< 0x80) {
463 emit_modrm_noreg(p
, op
, dst
);
464 emit_1b(p
, (char)imm
);
468 emit_modrm_noreg(p
, op
, dst
);
473 void x86_add_imm( struct x86_function
*p
, struct x86_reg dst
, int imm
)
476 x86_group1_imm(p
, 0, dst
, imm
);
479 void x86_or_imm( struct x86_function
*p
, struct x86_reg dst
, int imm
)
482 x86_group1_imm(p
, 1, dst
, imm
);
485 void x86_and_imm( struct x86_function
*p
, struct x86_reg dst
, int imm
)
488 x86_group1_imm(p
, 4, dst
, imm
);
491 void x86_sub_imm( struct x86_function
*p
, struct x86_reg dst
, int imm
)
494 x86_group1_imm(p
, 5, dst
, imm
);
497 void x86_xor_imm( struct x86_function
*p
, struct x86_reg dst
, int imm
)
500 x86_group1_imm(p
, 6, dst
, imm
);
503 void x86_cmp_imm( struct x86_function
*p
, struct x86_reg dst
, int imm
)
506 x86_group1_imm(p
, 7, dst
, imm
);
510 void x86_push( struct x86_function
*p
,
514 if (reg
.mod
== mod_REG
)
515 emit_1ub(p
, 0x50 + reg
.idx
);
519 emit_modrm_noreg(p
, 6, reg
);
523 p
->stack_offset
+= 4;
526 void x86_push_imm32( struct x86_function
*p
,
533 p
->stack_offset
+= 4;
537 void x86_pop( struct x86_function
*p
,
541 assert(reg
.mod
== mod_REG
);
542 emit_1ub(p
, 0x58 + reg
.idx
);
543 p
->stack_offset
-= 4;
546 void x86_inc( struct x86_function
*p
,
550 assert(reg
.mod
== mod_REG
);
551 emit_1ub(p
, 0x40 + reg
.idx
);
554 void x86_dec( struct x86_function
*p
,
558 assert(reg
.mod
== mod_REG
);
559 emit_1ub(p
, 0x48 + reg
.idx
);
562 void x86_ret( struct x86_function
*p
)
565 assert(p
->stack_offset
== 0);
569 void x86_retw( struct x86_function
*p
, unsigned short imm
)
572 emit_3ub(p
, 0xc2, imm
& 0xff, (imm
>> 8) & 0xff);
575 void x86_sahf( struct x86_function
*p
)
581 void x86_mov( struct x86_function
*p
,
586 emit_op_modrm( p
, 0x8b, 0x89, dst
, src
);
589 void x86_movzx8(struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
592 emit_2ub(p
, 0x0f, 0xb6);
593 emit_modrm(p
, dst
, src
);
596 void x86_movzx16(struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
599 emit_2ub(p
, 0x0f, 0xb7);
600 emit_modrm(p
, dst
, src
);
603 void x86_xor( struct x86_function
*p
,
608 emit_op_modrm( p
, 0x33, 0x31, dst
, src
);
611 void x86_cmp( struct x86_function
*p
,
616 emit_op_modrm( p
, 0x3b, 0x39, dst
, src
);
619 void x86_lea( struct x86_function
*p
,
625 emit_modrm( p
, dst
, src
);
628 void x86_test( struct x86_function
*p
,
634 emit_modrm( p
, dst
, src
);
637 void x86_add( struct x86_function
*p
,
642 emit_op_modrm(p
, 0x03, 0x01, dst
, src
);
645 /* Calculate EAX * src, results in EDX:EAX.
647 void x86_mul( struct x86_function
*p
,
652 emit_modrm_noreg(p
, 4, src
);
656 void x86_imul( struct x86_function
*p
,
661 emit_2ub(p
, X86_TWOB
, 0xAF);
662 emit_modrm(p
, dst
, src
);
666 void x86_sub( struct x86_function
*p
,
671 emit_op_modrm(p
, 0x2b, 0x29, dst
, src
);
674 void x86_or( struct x86_function
*p
,
679 emit_op_modrm( p
, 0x0b, 0x09, dst
, src
);
682 void x86_and( struct x86_function
*p
,
687 emit_op_modrm( p
, 0x23, 0x21, dst
, src
);
690 void x86_div( struct x86_function
*p
,
693 assert(src
.file
== file_REG32
&& src
.mod
== mod_REG
);
694 emit_op_modrm(p
, 0xf7, 0, x86_make_reg(file_REG32
, 6), src
);
699 /***********************************************************************
703 void sse_prefetchnta( struct x86_function
*p
, struct x86_reg ptr
)
706 assert(ptr
.mod
!= mod_REG
);
707 emit_2ub(p
, 0x0f, 0x18);
708 emit_modrm_noreg(p
, 0, ptr
);
711 void sse_prefetch0( struct x86_function
*p
, struct x86_reg ptr
)
714 assert(ptr
.mod
!= mod_REG
);
715 emit_2ub(p
, 0x0f, 0x18);
716 emit_modrm_noreg(p
, 1, ptr
);
719 void sse_prefetch1( struct x86_function
*p
, struct x86_reg ptr
)
722 assert(ptr
.mod
!= mod_REG
);
723 emit_2ub(p
, 0x0f, 0x18);
724 emit_modrm_noreg(p
, 2, ptr
);
727 void sse_movntps( struct x86_function
*p
,
733 assert(dst
.mod
!= mod_REG
);
734 assert(src
.mod
== mod_REG
);
735 emit_2ub(p
, 0x0f, 0x2b);
736 emit_modrm(p
, src
, dst
);
742 void sse_movss( struct x86_function
*p
,
747 emit_2ub(p
, 0xF3, X86_TWOB
);
748 emit_op_modrm( p
, 0x10, 0x11, dst
, src
);
751 void sse_movaps( struct x86_function
*p
,
756 emit_1ub(p
, X86_TWOB
);
757 emit_op_modrm( p
, 0x28, 0x29, dst
, src
);
760 void sse_movups( struct x86_function
*p
,
765 emit_1ub(p
, X86_TWOB
);
766 emit_op_modrm( p
, 0x10, 0x11, dst
, src
);
769 void sse_movhps( struct x86_function
*p
,
774 assert(dst
.mod
!= mod_REG
|| src
.mod
!= mod_REG
);
775 emit_1ub(p
, X86_TWOB
);
776 emit_op_modrm( p
, 0x16, 0x17, dst
, src
); /* cf movlhps */
779 void sse_movlps( struct x86_function
*p
,
784 assert(dst
.mod
!= mod_REG
|| src
.mod
!= mod_REG
);
785 emit_1ub(p
, X86_TWOB
);
786 emit_op_modrm( p
, 0x12, 0x13, dst
, src
); /* cf movhlps */
789 void sse_maxps( struct x86_function
*p
,
794 emit_2ub(p
, X86_TWOB
, 0x5F);
795 emit_modrm( p
, dst
, src
);
798 void sse_maxss( struct x86_function
*p
,
803 emit_3ub(p
, 0xF3, X86_TWOB
, 0x5F);
804 emit_modrm( p
, dst
, src
);
807 void sse_divss( struct x86_function
*p
,
812 emit_3ub(p
, 0xF3, X86_TWOB
, 0x5E);
813 emit_modrm( p
, dst
, src
);
816 void sse_minps( struct x86_function
*p
,
821 emit_2ub(p
, X86_TWOB
, 0x5D);
822 emit_modrm( p
, dst
, src
);
825 void sse_subps( struct x86_function
*p
,
830 emit_2ub(p
, X86_TWOB
, 0x5C);
831 emit_modrm( p
, dst
, src
);
834 void sse_mulps( struct x86_function
*p
,
839 emit_2ub(p
, X86_TWOB
, 0x59);
840 emit_modrm( p
, dst
, src
);
843 void sse_mulss( struct x86_function
*p
,
848 emit_3ub(p
, 0xF3, X86_TWOB
, 0x59);
849 emit_modrm( p
, dst
, src
);
852 void sse_addps( struct x86_function
*p
,
857 emit_2ub(p
, X86_TWOB
, 0x58);
858 emit_modrm( p
, dst
, src
);
861 void sse_addss( struct x86_function
*p
,
866 emit_3ub(p
, 0xF3, X86_TWOB
, 0x58);
867 emit_modrm( p
, dst
, src
);
870 void sse_andnps( struct x86_function
*p
,
875 emit_2ub(p
, X86_TWOB
, 0x55);
876 emit_modrm( p
, dst
, src
);
879 void sse_andps( struct x86_function
*p
,
884 emit_2ub(p
, X86_TWOB
, 0x54);
885 emit_modrm( p
, dst
, src
);
888 void sse_rsqrtps( struct x86_function
*p
,
893 emit_2ub(p
, X86_TWOB
, 0x52);
894 emit_modrm( p
, dst
, src
);
897 void sse_rsqrtss( struct x86_function
*p
,
902 emit_3ub(p
, 0xF3, X86_TWOB
, 0x52);
903 emit_modrm( p
, dst
, src
);
907 void sse_movhlps( struct x86_function
*p
,
912 assert(dst
.mod
== mod_REG
&& src
.mod
== mod_REG
);
913 emit_2ub(p
, X86_TWOB
, 0x12);
914 emit_modrm( p
, dst
, src
);
917 void sse_movlhps( struct x86_function
*p
,
922 assert(dst
.mod
== mod_REG
&& src
.mod
== mod_REG
);
923 emit_2ub(p
, X86_TWOB
, 0x16);
924 emit_modrm( p
, dst
, src
);
927 void sse_orps( struct x86_function
*p
,
932 emit_2ub(p
, X86_TWOB
, 0x56);
933 emit_modrm( p
, dst
, src
);
936 void sse_xorps( struct x86_function
*p
,
941 emit_2ub(p
, X86_TWOB
, 0x57);
942 emit_modrm( p
, dst
, src
);
945 void sse_cvtps2pi( struct x86_function
*p
,
950 assert(dst
.file
== file_MMX
&&
951 (src
.file
== file_XMM
|| src
.mod
!= mod_REG
));
955 emit_2ub(p
, X86_TWOB
, 0x2d);
956 emit_modrm( p
, dst
, src
);
959 void sse2_cvtdq2ps( struct x86_function
*p
,
964 emit_2ub(p
, X86_TWOB
, 0x5b);
965 emit_modrm( p
, dst
, src
);
969 /* Shufps can also be used to implement a reduced swizzle when dest ==
972 void sse_shufps( struct x86_function
*p
,
977 DUMP_RRI( dst
, src
, shuf
);
978 emit_2ub(p
, X86_TWOB
, 0xC6);
979 emit_modrm(p
, dst
, src
);
983 void sse_unpckhps( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
986 emit_2ub( p
, X86_TWOB
, 0x15 );
987 emit_modrm( p
, dst
, src
);
990 void sse_unpcklps( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
993 emit_2ub( p
, X86_TWOB
, 0x14 );
994 emit_modrm( p
, dst
, src
);
997 void sse_cmpps( struct x86_function
*p
,
1002 DUMP_RRI( dst
, src
, cc
);
1003 emit_2ub(p
, X86_TWOB
, 0xC2);
1004 emit_modrm(p
, dst
, src
);
1008 void sse_pmovmskb( struct x86_function
*p
,
1012 DUMP_RR( dst
, src
);
1013 emit_3ub(p
, 0x66, X86_TWOB
, 0xD7);
1014 emit_modrm(p
, dst
, src
);
1017 void sse_movmskps( struct x86_function
*p
,
1021 DUMP_RR( dst
, src
);
1022 emit_2ub(p
, X86_TWOB
, 0x50);
1023 emit_modrm(p
, dst
, src
);
1026 /***********************************************************************
1031 * Perform a reduced swizzle:
1033 void sse2_pshufd( struct x86_function
*p
,
1038 DUMP_RRI( dst
, src
, shuf
);
1039 emit_3ub(p
, 0x66, X86_TWOB
, 0x70);
1040 emit_modrm(p
, dst
, src
);
1044 void sse2_cvttps2dq( struct x86_function
*p
,
1046 struct x86_reg src
)
1048 DUMP_RR( dst
, src
);
1049 emit_3ub( p
, 0xF3, X86_TWOB
, 0x5B );
1050 emit_modrm( p
, dst
, src
);
1053 void sse2_cvtps2dq( struct x86_function
*p
,
1055 struct x86_reg src
)
1057 DUMP_RR( dst
, src
);
1058 emit_3ub(p
, 0x66, X86_TWOB
, 0x5B);
1059 emit_modrm( p
, dst
, src
);
1062 void sse2_packssdw( struct x86_function
*p
,
1064 struct x86_reg src
)
1066 DUMP_RR( dst
, src
);
1067 emit_3ub(p
, 0x66, X86_TWOB
, 0x6B);
1068 emit_modrm( p
, dst
, src
);
1071 void sse2_packsswb( struct x86_function
*p
,
1073 struct x86_reg src
)
1075 DUMP_RR( dst
, src
);
1076 emit_3ub(p
, 0x66, X86_TWOB
, 0x63);
1077 emit_modrm( p
, dst
, src
);
1080 void sse2_packuswb( struct x86_function
*p
,
1082 struct x86_reg src
)
1084 DUMP_RR( dst
, src
);
1085 emit_3ub(p
, 0x66, X86_TWOB
, 0x67);
1086 emit_modrm( p
, dst
, src
);
1089 void sse2_punpcklbw( struct x86_function
*p
,
1091 struct x86_reg src
)
1093 DUMP_RR( dst
, src
);
1094 emit_3ub(p
, 0x66, X86_TWOB
, 0x60);
1095 emit_modrm( p
, dst
, src
);
1099 void sse2_rcpps( struct x86_function
*p
,
1101 struct x86_reg src
)
1103 DUMP_RR( dst
, src
);
1104 emit_2ub(p
, X86_TWOB
, 0x53);
1105 emit_modrm( p
, dst
, src
);
1108 void sse2_rcpss( struct x86_function
*p
,
1110 struct x86_reg src
)
1112 DUMP_RR( dst
, src
);
1113 emit_3ub(p
, 0xF3, X86_TWOB
, 0x53);
1114 emit_modrm( p
, dst
, src
);
1117 void sse2_movd( struct x86_function
*p
,
1119 struct x86_reg src
)
1121 DUMP_RR( dst
, src
);
1122 emit_2ub(p
, 0x66, X86_TWOB
);
1123 emit_op_modrm( p
, 0x6e, 0x7e, dst
, src
);
1129 /***********************************************************************
1132 static void note_x87_pop( struct x86_function
*p
)
1135 assert(p
->x87_stack
>= 0);
1138 static void note_x87_push( struct x86_function
*p
)
1141 assert(p
->x87_stack
<= 7);
1144 void x87_assert_stack_empty( struct x86_function
*p
)
1146 assert (p
->x87_stack
== 0);
1150 void x87_fist( struct x86_function
*p
, struct x86_reg dst
)
1154 emit_modrm_noreg(p
, 2, dst
);
1157 void x87_fistp( struct x86_function
*p
, struct x86_reg dst
)
1161 emit_modrm_noreg(p
, 3, dst
);
1165 void x87_fild( struct x86_function
*p
, struct x86_reg arg
)
1169 emit_modrm_noreg(p
, 0, arg
);
1173 void x87_fldz( struct x86_function
*p
)
1176 emit_2ub(p
, 0xd9, 0xee);
1181 void x87_fldcw( struct x86_function
*p
, struct x86_reg arg
)
1184 assert(arg
.file
== file_REG32
);
1185 assert(arg
.mod
!= mod_REG
);
1187 emit_modrm_noreg(p
, 5, arg
);
1190 void x87_fld1( struct x86_function
*p
)
1193 emit_2ub(p
, 0xd9, 0xe8);
1197 void x87_fldl2e( struct x86_function
*p
)
1200 emit_2ub(p
, 0xd9, 0xea);
1204 void x87_fldln2( struct x86_function
*p
)
1207 emit_2ub(p
, 0xd9, 0xed);
1211 void x87_fwait( struct x86_function
*p
)
1217 void x87_fnclex( struct x86_function
*p
)
1220 emit_2ub(p
, 0xdb, 0xe2);
1223 void x87_fclex( struct x86_function
*p
)
1229 void x87_fcmovb( struct x86_function
*p
, struct x86_reg arg
)
1232 assert(arg
.file
== file_x87
);
1233 emit_2ub(p
, 0xda, 0xc0+arg
.idx
);
1236 void x87_fcmove( struct x86_function
*p
, struct x86_reg arg
)
1239 assert(arg
.file
== file_x87
);
1240 emit_2ub(p
, 0xda, 0xc8+arg
.idx
);
1243 void x87_fcmovbe( struct x86_function
*p
, struct x86_reg arg
)
1246 assert(arg
.file
== file_x87
);
1247 emit_2ub(p
, 0xda, 0xd0+arg
.idx
);
1250 void x87_fcmovnb( struct x86_function
*p
, struct x86_reg arg
)
1253 assert(arg
.file
== file_x87
);
1254 emit_2ub(p
, 0xdb, 0xc0+arg
.idx
);
1257 void x87_fcmovne( struct x86_function
*p
, struct x86_reg arg
)
1260 assert(arg
.file
== file_x87
);
1261 emit_2ub(p
, 0xdb, 0xc8+arg
.idx
);
1264 void x87_fcmovnbe( struct x86_function
*p
, struct x86_reg arg
)
1267 assert(arg
.file
== file_x87
);
1268 emit_2ub(p
, 0xdb, 0xd0+arg
.idx
);
1273 static void x87_arith_op( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg arg
,
1274 unsigned char dst0ub0
,
1275 unsigned char dst0ub1
,
1276 unsigned char arg0ub0
,
1277 unsigned char arg0ub1
,
1278 unsigned char argmem_noreg
)
1280 assert(dst
.file
== file_x87
);
1282 if (arg
.file
== file_x87
) {
1284 emit_2ub(p
, dst0ub0
, dst0ub1
+arg
.idx
);
1285 else if (arg
.idx
== 0)
1286 emit_2ub(p
, arg0ub0
, arg0ub1
+arg
.idx
);
1290 else if (dst
.idx
== 0) {
1291 assert(arg
.file
== file_REG32
);
1293 emit_modrm_noreg(p
, argmem_noreg
, arg
);
1299 void x87_fmul( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
1301 DUMP_RR( dst
, src
);
1302 x87_arith_op(p
, dst
, src
,
1308 void x87_fsub( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
1310 DUMP_RR( dst
, src
);
1311 x87_arith_op(p
, dst
, src
,
1317 void x87_fsubr( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
1319 DUMP_RR( dst
, src
);
1320 x87_arith_op(p
, dst
, src
,
1326 void x87_fadd( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
1328 DUMP_RR( dst
, src
);
1329 x87_arith_op(p
, dst
, src
,
1335 void x87_fdiv( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
1337 DUMP_RR( dst
, src
);
1338 x87_arith_op(p
, dst
, src
,
1344 void x87_fdivr( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg src
)
1346 DUMP_RR( dst
, src
);
1347 x87_arith_op(p
, dst
, src
,
1353 void x87_fmulp( struct x86_function
*p
, struct x86_reg dst
)
1356 assert(dst
.file
== file_x87
);
1357 assert(dst
.idx
>= 1);
1358 emit_2ub(p
, 0xde, 0xc8+dst
.idx
);
1362 void x87_fsubp( struct x86_function
*p
, struct x86_reg dst
)
1365 assert(dst
.file
== file_x87
);
1366 assert(dst
.idx
>= 1);
1367 emit_2ub(p
, 0xde, 0xe8+dst
.idx
);
1371 void x87_fsubrp( struct x86_function
*p
, struct x86_reg dst
)
1374 assert(dst
.file
== file_x87
);
1375 assert(dst
.idx
>= 1);
1376 emit_2ub(p
, 0xde, 0xe0+dst
.idx
);
1380 void x87_faddp( struct x86_function
*p
, struct x86_reg dst
)
1383 assert(dst
.file
== file_x87
);
1384 assert(dst
.idx
>= 1);
1385 emit_2ub(p
, 0xde, 0xc0+dst
.idx
);
1389 void x87_fdivp( struct x86_function
*p
, struct x86_reg dst
)
1392 assert(dst
.file
== file_x87
);
1393 assert(dst
.idx
>= 1);
1394 emit_2ub(p
, 0xde, 0xf8+dst
.idx
);
1398 void x87_fdivrp( struct x86_function
*p
, struct x86_reg dst
)
1401 assert(dst
.file
== file_x87
);
1402 assert(dst
.idx
>= 1);
1403 emit_2ub(p
, 0xde, 0xf0+dst
.idx
);
1407 void x87_ftst( struct x86_function
*p
)
1410 emit_2ub(p
, 0xd9, 0xe4);
1413 void x87_fucom( struct x86_function
*p
, struct x86_reg arg
)
1416 assert(arg
.file
== file_x87
);
1417 emit_2ub(p
, 0xdd, 0xe0+arg
.idx
);
1420 void x87_fucomp( struct x86_function
*p
, struct x86_reg arg
)
1423 assert(arg
.file
== file_x87
);
1424 emit_2ub(p
, 0xdd, 0xe8+arg
.idx
);
1428 void x87_fucompp( struct x86_function
*p
)
1431 emit_2ub(p
, 0xda, 0xe9);
1432 note_x87_pop(p
); /* pop twice */
1433 note_x87_pop(p
); /* pop twice */
1436 void x87_fxch( struct x86_function
*p
, struct x86_reg arg
)
1439 assert(arg
.file
== file_x87
);
1440 emit_2ub(p
, 0xd9, 0xc8+arg
.idx
);
1443 void x87_fabs( struct x86_function
*p
)
1446 emit_2ub(p
, 0xd9, 0xe1);
1449 void x87_fchs( struct x86_function
*p
)
1452 emit_2ub(p
, 0xd9, 0xe0);
1455 void x87_fcos( struct x86_function
*p
)
1458 emit_2ub(p
, 0xd9, 0xff);
1462 void x87_fprndint( struct x86_function
*p
)
1465 emit_2ub(p
, 0xd9, 0xfc);
1468 void x87_fscale( struct x86_function
*p
)
1471 emit_2ub(p
, 0xd9, 0xfd);
1474 void x87_fsin( struct x86_function
*p
)
1477 emit_2ub(p
, 0xd9, 0xfe);
1480 void x87_fsincos( struct x86_function
*p
)
1483 emit_2ub(p
, 0xd9, 0xfb);
1486 void x87_fsqrt( struct x86_function
*p
)
1489 emit_2ub(p
, 0xd9, 0xfa);
1492 void x87_fxtract( struct x86_function
*p
)
1495 emit_2ub(p
, 0xd9, 0xf4);
1500 * Restrictions: -1.0 <= st0 <= 1.0
1502 void x87_f2xm1( struct x86_function
*p
)
1505 emit_2ub(p
, 0xd9, 0xf0);
1508 /* st1 = st1 * log2(st0);
1511 void x87_fyl2x( struct x86_function
*p
)
1514 emit_2ub(p
, 0xd9, 0xf1);
1518 /* st1 = st1 * log2(st0 + 1.0);
1521 * A fast operation, with restrictions: -.29 < st0 < .29
1523 void x87_fyl2xp1( struct x86_function
*p
)
1526 emit_2ub(p
, 0xd9, 0xf9);
1531 void x87_fld( struct x86_function
*p
, struct x86_reg arg
)
1534 if (arg
.file
== file_x87
)
1535 emit_2ub(p
, 0xd9, 0xc0 + arg
.idx
);
1538 emit_modrm_noreg(p
, 0, arg
);
1543 void x87_fst( struct x86_function
*p
, struct x86_reg dst
)
1546 if (dst
.file
== file_x87
)
1547 emit_2ub(p
, 0xdd, 0xd0 + dst
.idx
);
1550 emit_modrm_noreg(p
, 2, dst
);
1554 void x87_fstp( struct x86_function
*p
, struct x86_reg dst
)
1557 if (dst
.file
== file_x87
)
1558 emit_2ub(p
, 0xdd, 0xd8 + dst
.idx
);
1561 emit_modrm_noreg(p
, 3, dst
);
1566 void x87_fpop( struct x86_function
*p
)
1568 x87_fstp( p
, x86_make_reg( file_x87
, 0 ));
1572 void x87_fcom( struct x86_function
*p
, struct x86_reg dst
)
1575 if (dst
.file
== file_x87
)
1576 emit_2ub(p
, 0xd8, 0xd0 + dst
.idx
);
1579 emit_modrm_noreg(p
, 2, dst
);
1584 void x87_fcomp( struct x86_function
*p
, struct x86_reg dst
)
1587 if (dst
.file
== file_x87
)
1588 emit_2ub(p
, 0xd8, 0xd8 + dst
.idx
);
1591 emit_modrm_noreg(p
, 3, dst
);
1596 void x87_fcomi( struct x86_function
*p
, struct x86_reg arg
)
1599 emit_2ub(p
, 0xdb, 0xf0+arg
.idx
);
1602 void x87_fcomip( struct x86_function
*p
, struct x86_reg arg
)
1605 emit_2ub(p
, 0xdb, 0xf0+arg
.idx
);
1610 void x87_fnstsw( struct x86_function
*p
, struct x86_reg dst
)
1613 assert(dst
.file
== file_REG32
);
1615 if (dst
.idx
== reg_AX
&&
1617 emit_2ub(p
, 0xdf, 0xe0);
1620 emit_modrm_noreg(p
, 7, dst
);
1625 void x87_fnstcw( struct x86_function
*p
, struct x86_reg dst
)
1628 assert(dst
.file
== file_REG32
);
1630 emit_1ub(p
, 0x9b); /* WAIT -- needed? */
1632 emit_modrm_noreg(p
, 7, dst
);
1638 /***********************************************************************
1642 void mmx_emms( struct x86_function
*p
)
1645 assert(p
->need_emms
);
1646 emit_2ub(p
, 0x0f, 0x77);
1650 void mmx_packssdw( struct x86_function
*p
,
1652 struct x86_reg src
)
1654 DUMP_RR( dst
, src
);
1655 assert(dst
.file
== file_MMX
&&
1656 (src
.file
== file_MMX
|| src
.mod
!= mod_REG
));
1660 emit_2ub(p
, X86_TWOB
, 0x6b);
1661 emit_modrm( p
, dst
, src
);
1664 void mmx_packuswb( struct x86_function
*p
,
1666 struct x86_reg src
)
1668 DUMP_RR( dst
, src
);
1669 assert(dst
.file
== file_MMX
&&
1670 (src
.file
== file_MMX
|| src
.mod
!= mod_REG
));
1674 emit_2ub(p
, X86_TWOB
, 0x67);
1675 emit_modrm( p
, dst
, src
);
1678 void mmx_movd( struct x86_function
*p
,
1680 struct x86_reg src
)
1682 DUMP_RR( dst
, src
);
1684 emit_1ub(p
, X86_TWOB
);
1685 emit_op_modrm( p
, 0x6e, 0x7e, dst
, src
);
1688 void mmx_movq( struct x86_function
*p
,
1690 struct x86_reg src
)
1692 DUMP_RR( dst
, src
);
1694 emit_1ub(p
, X86_TWOB
);
1695 emit_op_modrm( p
, 0x6f, 0x7f, dst
, src
);
1699 /***********************************************************************
1704 void x86_cdecl_caller_push_regs( struct x86_function
*p
)
1706 x86_push(p
, x86_make_reg(file_REG32
, reg_AX
));
1707 x86_push(p
, x86_make_reg(file_REG32
, reg_CX
));
1708 x86_push(p
, x86_make_reg(file_REG32
, reg_DX
));
1711 void x86_cdecl_caller_pop_regs( struct x86_function
*p
)
1713 x86_pop(p
, x86_make_reg(file_REG32
, reg_DX
));
1714 x86_pop(p
, x86_make_reg(file_REG32
, reg_CX
));
1715 x86_pop(p
, x86_make_reg(file_REG32
, reg_AX
));
1719 /* Retreive a reference to one of the function arguments, taking into
1720 * account any push/pop activity:
1722 struct x86_reg
x86_fn_arg( struct x86_function
*p
,
1725 return x86_make_disp(x86_make_reg(file_REG32
, reg_SP
),
1726 p
->stack_offset
+ arg
* 4); /* ??? */
1730 void x86_init_func( struct x86_function
*p
)
1738 void x86_init_func_size( struct x86_function
*p
, unsigned code_size
)
1740 p
->size
= code_size
;
1741 p
->store
= rtasm_exec_malloc(code_size
);
1742 if (p
->store
== NULL
) {
1743 p
->store
= p
->error_overflow
;
1749 void x86_release_func( struct x86_function
*p
)
1751 if (p
->store
&& p
->store
!= p
->error_overflow
)
1752 rtasm_exec_free(p
->store
);
1760 static INLINE x86_func
1761 voidptr_to_x86_func(void *v
)
1767 assert(sizeof(u
.v
) == sizeof(u
.f
));
1773 x86_func
x86_get_func( struct x86_function
*p
)
1776 if (DISASSEM
&& p
->store
)
1777 debug_printf("disassemble %p %p\n", p
->store
, p
->csr
);
1779 if (p
->store
== p
->error_overflow
)
1780 return voidptr_to_x86_func(NULL
);
1782 return voidptr_to_x86_func(p
->store
);
1787 void x86sse_dummy( void );
1789 void x86sse_dummy( void )