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
);
370 void x86_or( struct x86_function
*p
,
374 emit_op_modrm( p
, 0x0b, 0x09, dst
, src
);
377 void x86_and( struct x86_function
*p
,
381 emit_op_modrm( p
, 0x23, 0x21, dst
, src
);
386 /***********************************************************************
391 void sse_movss( struct x86_function
*p
,
395 emit_2ub(p
, 0xF3, X86_TWOB
);
396 emit_op_modrm( p
, 0x10, 0x11, dst
, src
);
399 void sse_movaps( struct x86_function
*p
,
403 emit_1ub(p
, X86_TWOB
);
404 emit_op_modrm( p
, 0x28, 0x29, dst
, src
);
407 void sse_movups( struct x86_function
*p
,
411 emit_1ub(p
, X86_TWOB
);
412 emit_op_modrm( p
, 0x10, 0x11, dst
, src
);
415 void sse_movhps( struct x86_function
*p
,
419 assert(dst
.mod
!= mod_REG
|| src
.mod
!= mod_REG
);
420 emit_1ub(p
, X86_TWOB
);
421 emit_op_modrm( p
, 0x16, 0x17, dst
, src
); /* cf movlhps */
424 void sse_movlps( struct x86_function
*p
,
428 assert(dst
.mod
!= mod_REG
|| src
.mod
!= mod_REG
);
429 emit_1ub(p
, X86_TWOB
);
430 emit_op_modrm( p
, 0x12, 0x13, dst
, src
); /* cf movhlps */
433 void sse_maxps( struct x86_function
*p
,
437 emit_2ub(p
, X86_TWOB
, 0x5F);
438 emit_modrm( p
, dst
, src
);
441 void sse_maxss( struct x86_function
*p
,
445 emit_3ub(p
, 0xF3, X86_TWOB
, 0x5F);
446 emit_modrm( p
, dst
, src
);
449 void sse_divss( struct x86_function
*p
,
453 emit_3ub(p
, 0xF3, X86_TWOB
, 0x5E);
454 emit_modrm( p
, dst
, src
);
457 void sse_minps( struct x86_function
*p
,
461 emit_2ub(p
, X86_TWOB
, 0x5D);
462 emit_modrm( p
, dst
, src
);
465 void sse_subps( struct x86_function
*p
,
469 emit_2ub(p
, X86_TWOB
, 0x5C);
470 emit_modrm( p
, dst
, src
);
473 void sse_mulps( struct x86_function
*p
,
477 emit_2ub(p
, X86_TWOB
, 0x59);
478 emit_modrm( p
, dst
, src
);
481 void sse_mulss( struct x86_function
*p
,
485 emit_3ub(p
, 0xF3, X86_TWOB
, 0x59);
486 emit_modrm( p
, dst
, src
);
489 void sse_addps( struct x86_function
*p
,
493 emit_2ub(p
, X86_TWOB
, 0x58);
494 emit_modrm( p
, dst
, src
);
497 void sse_addss( struct x86_function
*p
,
501 emit_3ub(p
, 0xF3, X86_TWOB
, 0x58);
502 emit_modrm( p
, dst
, src
);
505 void sse_andps( struct x86_function
*p
,
509 emit_2ub(p
, X86_TWOB
, 0x54);
510 emit_modrm( p
, dst
, src
);
514 void sse_rsqrtss( struct x86_function
*p
,
518 emit_3ub(p
, 0xF3, X86_TWOB
, 0x52);
519 emit_modrm( p
, dst
, src
);
523 void sse_movhlps( struct x86_function
*p
,
527 assert(dst
.mod
== mod_REG
&& src
.mod
== mod_REG
);
528 emit_2ub(p
, X86_TWOB
, 0x12);
529 emit_modrm( p
, dst
, src
);
532 void sse_movlhps( struct x86_function
*p
,
536 assert(dst
.mod
== mod_REG
&& src
.mod
== mod_REG
);
537 emit_2ub(p
, X86_TWOB
, 0x16);
538 emit_modrm( p
, dst
, src
);
542 void sse_cvtps2pi( struct x86_function
*p
,
546 assert(dst
.file
== file_MMX
&&
547 (src
.file
== file_XMM
|| src
.mod
!= mod_REG
));
551 emit_2ub(p
, X86_TWOB
, 0x2d);
552 emit_modrm( p
, dst
, src
);
556 /* Shufps can also be used to implement a reduced swizzle when dest ==
559 void sse_shufps( struct x86_function
*p
,
564 emit_2ub(p
, X86_TWOB
, 0xC6);
565 emit_modrm(p
, dest
, arg0
);
569 void sse_cmpps( struct x86_function
*p
,
574 emit_2ub(p
, X86_TWOB
, 0xC2);
575 emit_modrm(p
, dest
, arg0
);
579 /***********************************************************************
584 * Perform a reduced swizzle:
586 void sse2_pshufd( struct x86_function
*p
,
591 emit_3ub(p
, 0x66, X86_TWOB
, 0x70);
592 emit_modrm(p
, dest
, arg0
);
596 void sse2_cvtps2dq( struct x86_function
*p
,
600 emit_3ub(p
, 0x66, X86_TWOB
, 0x5B);
601 emit_modrm( p
, dst
, src
);
604 void sse2_packssdw( struct x86_function
*p
,
608 emit_3ub(p
, 0x66, X86_TWOB
, 0x6B);
609 emit_modrm( p
, dst
, src
);
612 void sse2_packsswb( struct x86_function
*p
,
616 emit_3ub(p
, 0x66, X86_TWOB
, 0x63);
617 emit_modrm( p
, dst
, src
);
620 void sse2_packuswb( struct x86_function
*p
,
624 emit_3ub(p
, 0x66, X86_TWOB
, 0x67);
625 emit_modrm( p
, dst
, src
);
628 void sse2_rcpss( struct x86_function
*p
,
632 emit_3ub(p
, 0xF3, X86_TWOB
, 0x53);
633 emit_modrm( p
, dst
, src
);
636 void sse2_movd( struct x86_function
*p
,
640 emit_2ub(p
, 0x66, X86_TWOB
);
641 emit_op_modrm( p
, 0x6e, 0x7e, dst
, src
);
647 /***********************************************************************
650 void x87_fist( struct x86_function
*p
, struct x86_reg dst
)
653 emit_modrm_noreg(p
, 2, dst
);
656 void x87_fistp( struct x86_function
*p
, struct x86_reg dst
)
659 emit_modrm_noreg(p
, 3, dst
);
662 void x87_fild( struct x86_function
*p
, struct x86_reg arg
)
665 emit_modrm_noreg(p
, 0, arg
);
668 void x87_fldz( struct x86_function
*p
)
670 emit_2ub(p
, 0xd9, 0xee);
674 void x87_fldcw( struct x86_function
*p
, struct x86_reg arg
)
676 assert(arg
.file
== file_REG32
);
677 assert(arg
.mod
!= mod_REG
);
679 emit_modrm_noreg(p
, 5, arg
);
682 void x87_fld1( struct x86_function
*p
)
684 emit_2ub(p
, 0xd9, 0xe8);
687 void x87_fldl2e( struct x86_function
*p
)
689 emit_2ub(p
, 0xd9, 0xea);
692 void x87_fldln2( struct x86_function
*p
)
694 emit_2ub(p
, 0xd9, 0xed);
697 void x87_fwait( struct x86_function
*p
)
702 void x87_fnclex( struct x86_function
*p
)
704 emit_2ub(p
, 0xdb, 0xe2);
707 void x87_fclex( struct x86_function
*p
)
714 static void x87_arith_op( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg arg
,
719 GLubyte argmem_noreg
)
721 assert(dst
.file
== file_x87
);
723 if (arg
.file
== file_x87
) {
725 emit_2ub(p
, dst0ub0
, dst0ub1
+arg
.idx
);
726 else if (arg
.idx
== 0)
727 emit_2ub(p
, arg0ub0
, arg0ub1
+arg
.idx
);
731 else if (dst
.idx
== 0) {
732 assert(arg
.file
= file_REG32
);
734 emit_modrm_noreg(p
, argmem_noreg
, arg
);
740 void x87_fmul( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg arg
)
742 x87_arith_op(p
, dst
, arg
,
748 void x87_fsub( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg arg
)
750 x87_arith_op(p
, dst
, arg
,
756 void x87_fsubr( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg arg
)
758 x87_arith_op(p
, dst
, arg
,
764 void x87_fadd( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg arg
)
766 x87_arith_op(p
, dst
, arg
,
772 void x87_fdiv( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg arg
)
774 x87_arith_op(p
, dst
, arg
,
780 void x87_fdivr( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg arg
)
782 x87_arith_op(p
, dst
, arg
,
788 void x87_fmulp( struct x86_function
*p
, struct x86_reg dst
)
790 assert(dst
.file
== file_x87
);
791 assert(dst
.idx
>= 1);
792 emit_2ub(p
, 0xde, 0xc8+dst
.idx
);
795 void x87_fsubp( struct x86_function
*p
, struct x86_reg dst
)
797 assert(dst
.file
== file_x87
);
798 assert(dst
.idx
>= 1);
799 emit_2ub(p
, 0xde, 0xe8+dst
.idx
);
802 void x87_fsubrp( struct x86_function
*p
, struct x86_reg dst
)
804 assert(dst
.file
== file_x87
);
805 assert(dst
.idx
>= 1);
806 emit_2ub(p
, 0xde, 0xe0+dst
.idx
);
809 void x87_faddp( struct x86_function
*p
, struct x86_reg dst
)
811 assert(dst
.file
== file_x87
);
812 assert(dst
.idx
>= 1);
813 emit_2ub(p
, 0xde, 0xc0+dst
.idx
);
816 void x87_fdivp( struct x86_function
*p
, struct x86_reg dst
)
818 assert(dst
.file
== file_x87
);
819 assert(dst
.idx
>= 1);
820 emit_2ub(p
, 0xde, 0xf8+dst
.idx
);
823 void x87_fdivrp( struct x86_function
*p
, struct x86_reg dst
)
825 assert(dst
.file
== file_x87
);
826 assert(dst
.idx
>= 1);
827 emit_2ub(p
, 0xde, 0xf0+dst
.idx
);
830 void x87_fucom( struct x86_function
*p
, struct x86_reg arg
)
832 assert(arg
.file
== file_x87
);
833 emit_2ub(p
, 0xdd, 0xe0+arg
.idx
);
836 void x87_fucomp( struct x86_function
*p
, struct x86_reg arg
)
838 assert(arg
.file
== file_x87
);
839 emit_2ub(p
, 0xdd, 0xe8+arg
.idx
);
842 void x87_fucompp( struct x86_function
*p
)
844 emit_2ub(p
, 0xda, 0xe9);
847 void x87_fxch( struct x86_function
*p
, struct x86_reg arg
)
849 assert(arg
.file
== file_x87
);
850 emit_2ub(p
, 0xd9, 0xc8+arg
.idx
);
853 void x87_fabs( struct x86_function
*p
)
855 emit_2ub(p
, 0xd9, 0xe1);
858 void x87_fchs( struct x86_function
*p
)
860 emit_2ub(p
, 0xd9, 0xe0);
863 void x87_fcos( struct x86_function
*p
)
865 emit_2ub(p
, 0xd9, 0xff);
869 void x87_fprndint( struct x86_function
*p
)
871 emit_2ub(p
, 0xd9, 0xfc);
874 void x87_fscale( struct x86_function
*p
)
876 emit_2ub(p
, 0xd9, 0xfd);
879 void x87_fsin( struct x86_function
*p
)
881 emit_2ub(p
, 0xd9, 0xfe);
884 void x87_fsincos( struct x86_function
*p
)
886 emit_2ub(p
, 0xd9, 0xfb);
889 void x87_fsqrt( struct x86_function
*p
)
891 emit_2ub(p
, 0xd9, 0xfa);
894 void x87_fxtract( struct x86_function
*p
)
896 emit_2ub(p
, 0xd9, 0xf4);
901 * Restrictions: -1.0 <= st0 <= 1.0
903 void x87_f2xm1( struct x86_function
*p
)
905 emit_2ub(p
, 0xd9, 0xf0);
908 /* st1 = st1 * log2(st0);
911 void x87_fyl2x( struct x86_function
*p
)
913 emit_2ub(p
, 0xd9, 0xf1);
916 /* st1 = st1 * log2(st0 + 1.0);
919 * A fast operation, with restrictions: -.29 < st0 < .29
921 void x87_fyl2xp1( struct x86_function
*p
)
923 emit_2ub(p
, 0xd9, 0xf9);
927 void x87_fld( struct x86_function
*p
, struct x86_reg arg
)
929 if (arg
.file
== file_x87
)
930 emit_2ub(p
, 0xd9, 0xc0 + arg
.idx
);
933 emit_modrm_noreg(p
, 0, arg
);
937 void x87_fst( struct x86_function
*p
, struct x86_reg dst
)
939 if (dst
.file
== file_x87
)
940 emit_2ub(p
, 0xdd, 0xd0 + dst
.idx
);
943 emit_modrm_noreg(p
, 2, dst
);
947 void x87_fstp( struct x86_function
*p
, struct x86_reg dst
)
949 if (dst
.file
== file_x87
)
950 emit_2ub(p
, 0xdd, 0xd8 + dst
.idx
);
953 emit_modrm_noreg(p
, 3, dst
);
957 void x87_fcom( struct x86_function
*p
, struct x86_reg dst
)
959 if (dst
.file
== file_x87
)
960 emit_2ub(p
, 0xd8, 0xd0 + dst
.idx
);
963 emit_modrm_noreg(p
, 2, dst
);
967 void x87_fcomp( struct x86_function
*p
, struct x86_reg dst
)
969 if (dst
.file
== file_x87
)
970 emit_2ub(p
, 0xd8, 0xd8 + dst
.idx
);
973 emit_modrm_noreg(p
, 3, dst
);
978 void x87_fnstsw( struct x86_function
*p
, struct x86_reg dst
)
980 assert(dst
.file
== file_REG32
);
982 if (dst
.idx
== reg_AX
&&
984 emit_2ub(p
, 0xdf, 0xe0);
987 emit_modrm_noreg(p
, 7, dst
);
994 /***********************************************************************
998 void mmx_emms( struct x86_function
*p
)
1000 assert(p
->need_emms
);
1001 emit_2ub(p
, 0x0f, 0x77);
1005 void mmx_packssdw( struct x86_function
*p
,
1007 struct x86_reg src
)
1009 assert(dst
.file
== file_MMX
&&
1010 (src
.file
== file_MMX
|| src
.mod
!= mod_REG
));
1014 emit_2ub(p
, X86_TWOB
, 0x6b);
1015 emit_modrm( p
, dst
, src
);
1018 void mmx_packuswb( struct x86_function
*p
,
1020 struct x86_reg src
)
1022 assert(dst
.file
== file_MMX
&&
1023 (src
.file
== file_MMX
|| src
.mod
!= mod_REG
));
1027 emit_2ub(p
, X86_TWOB
, 0x67);
1028 emit_modrm( p
, dst
, src
);
1031 void mmx_movd( struct x86_function
*p
,
1033 struct x86_reg src
)
1036 emit_1ub(p
, X86_TWOB
);
1037 emit_op_modrm( p
, 0x6e, 0x7e, dst
, src
);
1040 void mmx_movq( struct x86_function
*p
,
1042 struct x86_reg src
)
1045 emit_1ub(p
, X86_TWOB
);
1046 emit_op_modrm( p
, 0x6f, 0x7f, dst
, src
);
1050 /***********************************************************************
1055 /* Retreive a reference to one of the function arguments, taking into
1056 * account any push/pop activity:
1058 struct x86_reg
x86_fn_arg( struct x86_function
*p
,
1061 return x86_make_disp(x86_make_reg(file_REG32
, reg_SP
),
1062 p
->stack_offset
+ arg
* 4); /* ??? */
1066 void x86_init_func( struct x86_function
*p
)
1068 x86_init_func_size(p
, 1024);
1071 void x86_init_func_size( struct x86_function
*p
, GLuint code_size
)
1073 p
->store
= _mesa_exec_malloc(code_size
);
1077 void x86_release_func( struct x86_function
*p
)
1079 _mesa_exec_free(p
->store
);
1083 void (*x86_get_func( struct x86_function
*p
))(void)
1086 _mesa_printf("disassemble %p %p\n", p
->store
, p
->csr
);
1087 return (void (*)(void))p
->store
;
1092 void x86sse_dummy( void )