1 #if defined(USE_X86_ASM) || defined(SLANG_X86)
9 /* Emit bytes to the instruction stream:
11 static void emit_1b( struct x86_function
*p
, GLbyte b0
)
13 *(GLbyte
*)(p
->csr
++) = b0
;
16 static void emit_1i( struct x86_function
*p
, GLint i0
)
18 *(GLint
*)(p
->csr
) = i0
;
22 static void disassem( struct x86_function
*p
, const char *fn
)
25 if (fn
&& fn
!= p
->fn
) {
26 _mesa_printf("0x%x: %s\n", p
->csr
, fn
);
32 static void emit_1ub_fn( struct x86_function
*p
, GLubyte b0
, const char *fn
)
38 static void emit_2ub_fn( struct x86_function
*p
, GLubyte b0
, GLubyte b1
, const char *fn
)
45 static void emit_3ub_fn( struct x86_function
*p
, GLubyte b0
, GLubyte b1
, GLubyte b2
, const char *fn
)
53 #define emit_1ub(p, b0) emit_1ub_fn(p, b0, __FUNCTION__)
54 #define emit_2ub(p, b0, b1) emit_2ub_fn(p, b0, b1, __FUNCTION__)
55 #define emit_3ub(p, b0, b1, b2) emit_3ub_fn(p, b0, b1, b2, __FUNCTION__)
59 /* Build a modRM byte + possible displacement. No treatment of SIB
60 * indexing. BZZT - no way to encode an absolute address.
62 static void emit_modrm( struct x86_function
*p
,
64 struct x86_reg regmem
)
68 assert(reg
.mod
== mod_REG
);
70 val
|= regmem
.mod
<< 6; /* mod field */
71 val
|= reg
.idx
<< 3; /* reg field */
72 val
|= regmem
.idx
; /* r/m field */
74 emit_1ub_fn(p
, val
, 0);
76 /* Oh-oh we've stumbled into the SIB thing.
78 if (regmem
.file
== file_REG32
&&
79 regmem
.idx
== reg_SP
) {
80 emit_1ub_fn(p
, 0x24, 0); /* simplistic! */
88 emit_1b(p
, regmem
.disp
);
91 emit_1i(p
, regmem
.disp
);
100 static void emit_modrm_noreg( struct x86_function
*p
,
102 struct x86_reg regmem
)
104 struct x86_reg dummy
= x86_make_reg(file_REG32
, op
);
105 emit_modrm(p
, dummy
, regmem
);
108 /* Many x86 instructions have two opcodes to cope with the situations
109 * where the destination is a register or memory reference
110 * respectively. This function selects the correct opcode based on
111 * the arguments presented.
113 static void emit_op_modrm( struct x86_function
*p
,
114 GLubyte op_dst_is_reg
,
115 GLubyte op_dst_is_mem
,
121 emit_1ub_fn(p
, op_dst_is_reg
, 0);
122 emit_modrm(p
, dst
, src
);
127 assert(src
.mod
== mod_REG
);
128 emit_1ub_fn(p
, op_dst_is_mem
, 0);
129 emit_modrm(p
, src
, dst
);
143 /* Create and manipulate registers and regmem values:
145 struct x86_reg
x86_make_reg( enum x86_reg_file file
,
146 enum x86_reg_name idx
)
158 struct x86_reg
x86_make_disp( struct x86_reg reg
,
161 assert(reg
.file
== file_REG32
);
163 if (reg
.mod
== mod_REG
)
169 reg
.mod
= mod_INDIRECT
;
170 else if (reg
.disp
<= 127 && reg
.disp
>= -128)
173 reg
.mod
= mod_DISP32
;
178 struct x86_reg
x86_deref( struct x86_reg reg
)
180 return x86_make_disp(reg
, 0);
183 struct x86_reg
x86_get_base_reg( struct x86_reg reg
)
185 return x86_make_reg( reg
.file
, reg
.idx
);
188 GLubyte
*x86_get_label( struct x86_function
*p
)
195 /***********************************************************************
200 void x86_jcc( struct x86_function
*p
,
204 GLint offset
= label
- (x86_get_label(p
) + 2);
206 if (offset
<= 127 && offset
>= -128) {
207 emit_1ub(p
, 0x70 + cc
);
208 emit_1b(p
, (GLbyte
) offset
);
211 offset
= label
- (x86_get_label(p
) + 6);
212 emit_2ub(p
, 0x0f, 0x80 + cc
);
217 /* Always use a 32bit offset for forward jumps:
219 GLubyte
*x86_jcc_forward( struct x86_function
*p
,
222 emit_2ub(p
, 0x0f, 0x80 + cc
);
224 return x86_get_label(p
);
227 GLubyte
*x86_jmp_forward( struct x86_function
*p
)
231 return x86_get_label(p
);
234 GLubyte
*x86_call_forward( struct x86_function
*p
)
238 return x86_get_label(p
);
241 /* Fixup offset from forward jump:
243 void x86_fixup_fwd_jump( struct x86_function
*p
,
246 *(int *)(fixup
- 4) = x86_get_label(p
) - fixup
;
249 void x86_jmp( struct x86_function
*p
, GLubyte
*label
)
252 emit_1i(p
, label
- x86_get_label(p
) - 4);
255 void x86_call( struct x86_function
*p
, GLubyte
*label
)
258 emit_1i(p
, label
- x86_get_label(p
) - 4);
262 * Temporary. As I need immediate operands, and dont want to mess with the codegen,
263 * I load the immediate into general purpose register and use it.
265 void x86_mov_reg_imm( struct x86_function
*p
, struct x86_reg dst
, GLint imm
)
267 assert(dst
.mod
== mod_REG
);
268 emit_1ub(p
, 0xb8 + dst
.idx
);
272 void x86_push( struct x86_function
*p
,
275 assert(reg
.mod
== mod_REG
);
276 emit_1ub(p
, 0x50 + reg
.idx
);
277 p
->stack_offset
+= 4;
280 void x86_pop( struct x86_function
*p
,
283 assert(reg
.mod
== mod_REG
);
284 emit_1ub(p
, 0x58 + reg
.idx
);
285 p
->stack_offset
-= 4;
288 void x86_inc( struct x86_function
*p
,
291 assert(reg
.mod
== mod_REG
);
292 emit_1ub(p
, 0x40 + reg
.idx
);
295 void x86_dec( struct x86_function
*p
,
298 assert(reg
.mod
== mod_REG
);
299 emit_1ub(p
, 0x48 + reg
.idx
);
302 void x86_ret( struct x86_function
*p
)
307 void x86_sahf( struct x86_function
*p
)
312 void x86_mov( struct x86_function
*p
,
316 emit_op_modrm( p
, 0x8b, 0x89, dst
, src
);
319 void x86_xor( struct x86_function
*p
,
323 emit_op_modrm( p
, 0x33, 0x31, dst
, src
);
326 void x86_cmp( struct x86_function
*p
,
330 emit_op_modrm( p
, 0x3b, 0x39, dst
, src
);
333 void x86_lea( struct x86_function
*p
,
338 emit_modrm( p
, dst
, src
);
341 void x86_test( struct x86_function
*p
,
346 emit_modrm( p
, dst
, src
);
349 void x86_add( struct x86_function
*p
,
353 emit_op_modrm(p
, 0x03, 0x01, dst
, src
);
356 void x86_mul( struct x86_function
*p
,
359 assert (src
.file
== file_REG32
&& src
.mod
== mod_REG
);
360 emit_op_modrm(p
, 0xf7, 0, x86_make_reg (file_REG32
, reg_SP
), src
);
363 void x86_sub( struct x86_function
*p
,
367 emit_op_modrm(p
, 0x2b, 0x29, dst
, src
);
372 /***********************************************************************
377 void sse_movss( struct x86_function
*p
,
381 emit_2ub(p
, 0xF3, X86_TWOB
);
382 emit_op_modrm( p
, 0x10, 0x11, dst
, src
);
385 void sse_movaps( struct x86_function
*p
,
389 emit_1ub(p
, X86_TWOB
);
390 emit_op_modrm( p
, 0x28, 0x29, dst
, src
);
393 void sse_movups( struct x86_function
*p
,
397 emit_1ub(p
, X86_TWOB
);
398 emit_op_modrm( p
, 0x10, 0x11, dst
, src
);
401 void sse_movhps( struct x86_function
*p
,
405 assert(dst
.mod
!= mod_REG
|| src
.mod
!= mod_REG
);
406 emit_1ub(p
, X86_TWOB
);
407 emit_op_modrm( p
, 0x16, 0x17, dst
, src
); /* cf movlhps */
410 void sse_movlps( struct x86_function
*p
,
414 assert(dst
.mod
!= mod_REG
|| src
.mod
!= mod_REG
);
415 emit_1ub(p
, X86_TWOB
);
416 emit_op_modrm( p
, 0x12, 0x13, dst
, src
); /* cf movhlps */
419 void sse_maxps( struct x86_function
*p
,
423 emit_2ub(p
, X86_TWOB
, 0x5F);
424 emit_modrm( p
, dst
, src
);
427 void sse_divss( struct x86_function
*p
,
431 emit_3ub(p
, 0xF3, X86_TWOB
, 0x5E);
432 emit_modrm( p
, dst
, src
);
435 void sse_minps( struct x86_function
*p
,
439 emit_2ub(p
, X86_TWOB
, 0x5D);
440 emit_modrm( p
, dst
, src
);
443 void sse_subps( struct x86_function
*p
,
447 emit_2ub(p
, X86_TWOB
, 0x5C);
448 emit_modrm( p
, dst
, src
);
451 void sse_mulps( struct x86_function
*p
,
455 emit_2ub(p
, X86_TWOB
, 0x59);
456 emit_modrm( p
, dst
, src
);
459 void sse_addps( struct x86_function
*p
,
463 emit_2ub(p
, X86_TWOB
, 0x58);
464 emit_modrm( p
, dst
, src
);
467 void sse_addss( struct x86_function
*p
,
471 emit_3ub(p
, 0xF3, X86_TWOB
, 0x58);
472 emit_modrm( p
, dst
, src
);
475 void sse_andps( struct x86_function
*p
,
479 emit_2ub(p
, X86_TWOB
, 0x54);
480 emit_modrm( p
, dst
, src
);
484 void sse_rsqrtss( struct x86_function
*p
,
488 emit_3ub(p
, 0xF3, X86_TWOB
, 0x52);
489 emit_modrm( p
, dst
, src
);
493 void sse_movhlps( struct x86_function
*p
,
497 assert(dst
.mod
== mod_REG
&& src
.mod
== mod_REG
);
498 emit_2ub(p
, X86_TWOB
, 0x12);
499 emit_modrm( p
, dst
, src
);
502 void sse_movlhps( struct x86_function
*p
,
506 assert(dst
.mod
== mod_REG
&& src
.mod
== mod_REG
);
507 emit_2ub(p
, X86_TWOB
, 0x16);
508 emit_modrm( p
, dst
, src
);
512 void sse_cvtps2pi( struct x86_function
*p
,
516 assert(dst
.file
== file_MMX
&&
517 (src
.file
== file_XMM
|| src
.mod
!= mod_REG
));
521 emit_2ub(p
, X86_TWOB
, 0x2d);
522 emit_modrm( p
, dst
, src
);
526 /* Shufps can also be used to implement a reduced swizzle when dest ==
529 void sse_shufps( struct x86_function
*p
,
534 emit_2ub(p
, X86_TWOB
, 0xC6);
535 emit_modrm(p
, dest
, arg0
);
539 void sse_cmpps( struct x86_function
*p
,
544 emit_2ub(p
, X86_TWOB
, 0xC2);
545 emit_modrm(p
, dest
, arg0
);
549 /***********************************************************************
554 * Perform a reduced swizzle:
556 void sse2_pshufd( struct x86_function
*p
,
561 emit_3ub(p
, 0x66, X86_TWOB
, 0x70);
562 emit_modrm(p
, dest
, arg0
);
566 void sse2_cvtps2dq( struct x86_function
*p
,
570 emit_3ub(p
, 0x66, X86_TWOB
, 0x5B);
571 emit_modrm( p
, dst
, src
);
574 void sse2_packssdw( struct x86_function
*p
,
578 emit_3ub(p
, 0x66, X86_TWOB
, 0x6B);
579 emit_modrm( p
, dst
, src
);
582 void sse2_packsswb( struct x86_function
*p
,
586 emit_3ub(p
, 0x66, X86_TWOB
, 0x63);
587 emit_modrm( p
, dst
, src
);
590 void sse2_packuswb( struct x86_function
*p
,
594 emit_3ub(p
, 0x66, X86_TWOB
, 0x67);
595 emit_modrm( p
, dst
, src
);
598 void sse2_rcpss( struct x86_function
*p
,
602 emit_3ub(p
, 0xF3, X86_TWOB
, 0x53);
603 emit_modrm( p
, dst
, src
);
606 void sse2_movd( struct x86_function
*p
,
610 emit_2ub(p
, 0x66, X86_TWOB
);
611 emit_op_modrm( p
, 0x6e, 0x7e, dst
, src
);
617 /***********************************************************************
620 void x87_fist( struct x86_function
*p
, struct x86_reg dst
)
623 emit_modrm_noreg(p
, 2, dst
);
626 void x87_fistp( struct x86_function
*p
, struct x86_reg dst
)
629 emit_modrm_noreg(p
, 3, dst
);
632 void x87_fldz( struct x86_function
*p
)
634 emit_2ub(p
, 0xd9, 0xee);
638 void x87_fldcw( struct x86_function
*p
, struct x86_reg arg
)
640 assert(arg
.file
== file_REG32
);
641 assert(arg
.mod
!= mod_REG
);
643 emit_modrm_noreg(p
, 5, arg
);
646 void x87_fld1( struct x86_function
*p
)
648 emit_2ub(p
, 0xd9, 0xe8);
651 void x87_fldl2e( struct x86_function
*p
)
653 emit_2ub(p
, 0xd9, 0xea);
656 void x87_fldln2( struct x86_function
*p
)
658 emit_2ub(p
, 0xd9, 0xed);
661 void x87_fwait( struct x86_function
*p
)
666 void x87_fnclex( struct x86_function
*p
)
668 emit_2ub(p
, 0xdb, 0xe2);
671 void x87_fclex( struct x86_function
*p
)
678 static void x87_arith_op( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg arg
,
683 GLubyte argmem_noreg
)
685 assert(dst
.file
== file_x87
);
687 if (arg
.file
== file_x87
) {
689 emit_2ub(p
, dst0ub0
, dst0ub1
+arg
.idx
);
690 else if (arg
.idx
== 0)
691 emit_2ub(p
, arg0ub0
, arg0ub1
+arg
.idx
);
695 else if (dst
.idx
== 0) {
696 assert(arg
.file
= file_REG32
);
698 emit_modrm_noreg(p
, argmem_noreg
, arg
);
704 void x87_fmul( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg arg
)
706 x87_arith_op(p
, dst
, arg
,
712 void x87_fsub( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg arg
)
714 x87_arith_op(p
, dst
, arg
,
720 void x87_fsubr( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg arg
)
722 x87_arith_op(p
, dst
, arg
,
728 void x87_fadd( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg arg
)
730 x87_arith_op(p
, dst
, arg
,
736 void x87_fdiv( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg arg
)
738 x87_arith_op(p
, dst
, arg
,
744 void x87_fdivr( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg arg
)
746 x87_arith_op(p
, dst
, arg
,
752 void x87_fmulp( struct x86_function
*p
, struct x86_reg dst
)
754 assert(dst
.file
== file_x87
);
755 assert(dst
.idx
>= 1);
756 emit_2ub(p
, 0xde, 0xc8+dst
.idx
);
759 void x87_fsubp( struct x86_function
*p
, struct x86_reg dst
)
761 assert(dst
.file
== file_x87
);
762 assert(dst
.idx
>= 1);
763 emit_2ub(p
, 0xde, 0xe8+dst
.idx
);
766 void x87_fsubrp( struct x86_function
*p
, struct x86_reg dst
)
768 assert(dst
.file
== file_x87
);
769 assert(dst
.idx
>= 1);
770 emit_2ub(p
, 0xde, 0xe0+dst
.idx
);
773 void x87_faddp( struct x86_function
*p
, struct x86_reg dst
)
775 assert(dst
.file
== file_x87
);
776 assert(dst
.idx
>= 1);
777 emit_2ub(p
, 0xde, 0xc0+dst
.idx
);
780 void x87_fdivp( struct x86_function
*p
, struct x86_reg dst
)
782 assert(dst
.file
== file_x87
);
783 assert(dst
.idx
>= 1);
784 emit_2ub(p
, 0xde, 0xf8+dst
.idx
);
787 void x87_fdivrp( struct x86_function
*p
, struct x86_reg dst
)
789 assert(dst
.file
== file_x87
);
790 assert(dst
.idx
>= 1);
791 emit_2ub(p
, 0xde, 0xf0+dst
.idx
);
794 void x87_fucom( struct x86_function
*p
, struct x86_reg arg
)
796 assert(arg
.file
== file_x87
);
797 emit_2ub(p
, 0xdd, 0xe0+arg
.idx
);
800 void x87_fucomp( struct x86_function
*p
, struct x86_reg arg
)
802 assert(arg
.file
== file_x87
);
803 emit_2ub(p
, 0xdd, 0xe8+arg
.idx
);
806 void x87_fucompp( struct x86_function
*p
)
808 emit_2ub(p
, 0xda, 0xe9);
811 void x87_fxch( struct x86_function
*p
, struct x86_reg arg
)
813 assert(arg
.file
== file_x87
);
814 emit_2ub(p
, 0xd9, 0xc8+arg
.idx
);
817 void x87_fabs( struct x86_function
*p
)
819 emit_2ub(p
, 0xd9, 0xe1);
822 void x87_fchs( struct x86_function
*p
)
824 emit_2ub(p
, 0xd9, 0xe0);
827 void x87_fcos( struct x86_function
*p
)
829 emit_2ub(p
, 0xd9, 0xff);
833 void x87_fprndint( struct x86_function
*p
)
835 emit_2ub(p
, 0xd9, 0xfc);
838 void x87_fscale( struct x86_function
*p
)
840 emit_2ub(p
, 0xd9, 0xfd);
843 void x87_fsin( struct x86_function
*p
)
845 emit_2ub(p
, 0xd9, 0xfe);
848 void x87_fsincos( struct x86_function
*p
)
850 emit_2ub(p
, 0xd9, 0xfb);
853 void x87_fsqrt( struct x86_function
*p
)
855 emit_2ub(p
, 0xd9, 0xfa);
858 void x87_fxtract( struct x86_function
*p
)
860 emit_2ub(p
, 0xd9, 0xf4);
865 * Restrictions: -1.0 <= st0 <= 1.0
867 void x87_f2xm1( struct x86_function
*p
)
869 emit_2ub(p
, 0xd9, 0xf0);
872 /* st1 = st1 * log2(st0);
875 void x87_fyl2x( struct x86_function
*p
)
877 emit_2ub(p
, 0xd9, 0xf1);
880 /* st1 = st1 * log2(st0 + 1.0);
883 * A fast operation, with restrictions: -.29 < st0 < .29
885 void x87_fyl2xp1( struct x86_function
*p
)
887 emit_2ub(p
, 0xd9, 0xf9);
891 void x87_fld( struct x86_function
*p
, struct x86_reg arg
)
893 if (arg
.file
== file_x87
)
894 emit_2ub(p
, 0xd9, 0xc0 + arg
.idx
);
897 emit_modrm_noreg(p
, 0, arg
);
901 void x87_fst( struct x86_function
*p
, struct x86_reg dst
)
903 if (dst
.file
== file_x87
)
904 emit_2ub(p
, 0xdd, 0xd0 + dst
.idx
);
907 emit_modrm_noreg(p
, 2, dst
);
911 void x87_fstp( struct x86_function
*p
, struct x86_reg dst
)
913 if (dst
.file
== file_x87
)
914 emit_2ub(p
, 0xdd, 0xd8 + dst
.idx
);
917 emit_modrm_noreg(p
, 3, dst
);
921 void x87_fcom( struct x86_function
*p
, struct x86_reg dst
)
923 if (dst
.file
== file_x87
)
924 emit_2ub(p
, 0xd8, 0xd0 + dst
.idx
);
927 emit_modrm_noreg(p
, 2, dst
);
931 void x87_fcomp( struct x86_function
*p
, struct x86_reg dst
)
933 if (dst
.file
== file_x87
)
934 emit_2ub(p
, 0xd8, 0xd8 + dst
.idx
);
937 emit_modrm_noreg(p
, 3, dst
);
942 void x87_fnstsw( struct x86_function
*p
, struct x86_reg dst
)
944 assert(dst
.file
== file_REG32
);
946 if (dst
.idx
== reg_AX
&&
948 emit_2ub(p
, 0xdf, 0xe0);
951 emit_modrm_noreg(p
, 7, dst
);
958 /***********************************************************************
962 void mmx_emms( struct x86_function
*p
)
964 assert(p
->need_emms
);
965 emit_2ub(p
, 0x0f, 0x77);
969 void mmx_packssdw( struct x86_function
*p
,
973 assert(dst
.file
== file_MMX
&&
974 (src
.file
== file_MMX
|| src
.mod
!= mod_REG
));
978 emit_2ub(p
, X86_TWOB
, 0x6b);
979 emit_modrm( p
, dst
, src
);
982 void mmx_packuswb( struct x86_function
*p
,
986 assert(dst
.file
== file_MMX
&&
987 (src
.file
== file_MMX
|| src
.mod
!= mod_REG
));
991 emit_2ub(p
, X86_TWOB
, 0x67);
992 emit_modrm( p
, dst
, src
);
995 void mmx_movd( struct x86_function
*p
,
1000 emit_1ub(p
, X86_TWOB
);
1001 emit_op_modrm( p
, 0x6e, 0x7e, dst
, src
);
1004 void mmx_movq( struct x86_function
*p
,
1006 struct x86_reg src
)
1009 emit_1ub(p
, X86_TWOB
);
1010 emit_op_modrm( p
, 0x6f, 0x7f, dst
, src
);
1014 /***********************************************************************
1019 /* Retreive a reference to one of the function arguments, taking into
1020 * account any push/pop activity:
1022 struct x86_reg
x86_fn_arg( struct x86_function
*p
,
1025 return x86_make_disp(x86_make_reg(file_REG32
, reg_SP
),
1026 p
->stack_offset
+ arg
* 4); /* ??? */
1030 void x86_init_func( struct x86_function
*p
)
1032 x86_init_func_size(p
, 1024);
1035 void x86_init_func_size( struct x86_function
*p
, GLuint code_size
)
1037 p
->store
= _mesa_exec_malloc(code_size
);
1041 void x86_release_func( struct x86_function
*p
)
1043 _mesa_exec_free(p
->store
);
1047 void (*x86_get_func( struct x86_function
*p
))(void)
1050 _mesa_printf("disassemble %p %p\n", p
->store
, p
->csr
);
1051 return (void (*)(void))p
->store
;
1056 void x86sse_dummy( void )