6137aef8ecee7be5aafcf313fc6f72ce81003ca1
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_maxss( struct x86_function
*p
,
431 emit_3ub(p
, 0xF3, X86_TWOB
, 0x5F);
432 emit_modrm( p
, dst
, src
);
435 void sse_divss( struct x86_function
*p
,
439 emit_3ub(p
, 0xF3, X86_TWOB
, 0x5E);
440 emit_modrm( p
, dst
, src
);
443 void sse_minps( struct x86_function
*p
,
447 emit_2ub(p
, X86_TWOB
, 0x5D);
448 emit_modrm( p
, dst
, src
);
451 void sse_subps( struct x86_function
*p
,
455 emit_2ub(p
, X86_TWOB
, 0x5C);
456 emit_modrm( p
, dst
, src
);
459 void sse_mulps( struct x86_function
*p
,
463 emit_2ub(p
, X86_TWOB
, 0x59);
464 emit_modrm( p
, dst
, src
);
467 void sse_mulss( struct x86_function
*p
,
471 emit_3ub(p
, 0xF3, X86_TWOB
, 0x59);
472 emit_modrm( p
, dst
, src
);
475 void sse_addps( struct x86_function
*p
,
479 emit_2ub(p
, X86_TWOB
, 0x58);
480 emit_modrm( p
, dst
, src
);
483 void sse_addss( struct x86_function
*p
,
487 emit_3ub(p
, 0xF3, X86_TWOB
, 0x58);
488 emit_modrm( p
, dst
, src
);
491 void sse_andps( struct x86_function
*p
,
495 emit_2ub(p
, X86_TWOB
, 0x54);
496 emit_modrm( p
, dst
, src
);
500 void sse_rsqrtss( struct x86_function
*p
,
504 emit_3ub(p
, 0xF3, X86_TWOB
, 0x52);
505 emit_modrm( p
, dst
, src
);
509 void sse_movhlps( struct x86_function
*p
,
513 assert(dst
.mod
== mod_REG
&& src
.mod
== mod_REG
);
514 emit_2ub(p
, X86_TWOB
, 0x12);
515 emit_modrm( p
, dst
, src
);
518 void sse_movlhps( struct x86_function
*p
,
522 assert(dst
.mod
== mod_REG
&& src
.mod
== mod_REG
);
523 emit_2ub(p
, X86_TWOB
, 0x16);
524 emit_modrm( p
, dst
, src
);
528 void sse_cvtps2pi( struct x86_function
*p
,
532 assert(dst
.file
== file_MMX
&&
533 (src
.file
== file_XMM
|| src
.mod
!= mod_REG
));
537 emit_2ub(p
, X86_TWOB
, 0x2d);
538 emit_modrm( p
, dst
, src
);
542 /* Shufps can also be used to implement a reduced swizzle when dest ==
545 void sse_shufps( struct x86_function
*p
,
550 emit_2ub(p
, X86_TWOB
, 0xC6);
551 emit_modrm(p
, dest
, arg0
);
555 void sse_cmpps( struct x86_function
*p
,
560 emit_2ub(p
, X86_TWOB
, 0xC2);
561 emit_modrm(p
, dest
, arg0
);
565 /***********************************************************************
570 * Perform a reduced swizzle:
572 void sse2_pshufd( struct x86_function
*p
,
577 emit_3ub(p
, 0x66, X86_TWOB
, 0x70);
578 emit_modrm(p
, dest
, arg0
);
582 void sse2_cvtps2dq( struct x86_function
*p
,
586 emit_3ub(p
, 0x66, X86_TWOB
, 0x5B);
587 emit_modrm( p
, dst
, src
);
590 void sse2_packssdw( struct x86_function
*p
,
594 emit_3ub(p
, 0x66, X86_TWOB
, 0x6B);
595 emit_modrm( p
, dst
, src
);
598 void sse2_packsswb( struct x86_function
*p
,
602 emit_3ub(p
, 0x66, X86_TWOB
, 0x63);
603 emit_modrm( p
, dst
, src
);
606 void sse2_packuswb( struct x86_function
*p
,
610 emit_3ub(p
, 0x66, X86_TWOB
, 0x67);
611 emit_modrm( p
, dst
, src
);
614 void sse2_rcpss( struct x86_function
*p
,
618 emit_3ub(p
, 0xF3, X86_TWOB
, 0x53);
619 emit_modrm( p
, dst
, src
);
622 void sse2_movd( struct x86_function
*p
,
626 emit_2ub(p
, 0x66, X86_TWOB
);
627 emit_op_modrm( p
, 0x6e, 0x7e, dst
, src
);
633 /***********************************************************************
636 void x87_fist( struct x86_function
*p
, struct x86_reg dst
)
639 emit_modrm_noreg(p
, 2, dst
);
642 void x87_fistp( struct x86_function
*p
, struct x86_reg dst
)
645 emit_modrm_noreg(p
, 3, dst
);
648 void x87_fild( struct x86_function
*p
, struct x86_reg arg
)
651 emit_modrm_noreg(p
, 0, arg
);
654 void x87_fldz( struct x86_function
*p
)
656 emit_2ub(p
, 0xd9, 0xee);
660 void x87_fldcw( struct x86_function
*p
, struct x86_reg arg
)
662 assert(arg
.file
== file_REG32
);
663 assert(arg
.mod
!= mod_REG
);
665 emit_modrm_noreg(p
, 5, arg
);
668 void x87_fld1( struct x86_function
*p
)
670 emit_2ub(p
, 0xd9, 0xe8);
673 void x87_fldl2e( struct x86_function
*p
)
675 emit_2ub(p
, 0xd9, 0xea);
678 void x87_fldln2( struct x86_function
*p
)
680 emit_2ub(p
, 0xd9, 0xed);
683 void x87_fwait( struct x86_function
*p
)
688 void x87_fnclex( struct x86_function
*p
)
690 emit_2ub(p
, 0xdb, 0xe2);
693 void x87_fclex( struct x86_function
*p
)
700 static void x87_arith_op( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg arg
,
705 GLubyte argmem_noreg
)
707 assert(dst
.file
== file_x87
);
709 if (arg
.file
== file_x87
) {
711 emit_2ub(p
, dst0ub0
, dst0ub1
+arg
.idx
);
712 else if (arg
.idx
== 0)
713 emit_2ub(p
, arg0ub0
, arg0ub1
+arg
.idx
);
717 else if (dst
.idx
== 0) {
718 assert(arg
.file
= file_REG32
);
720 emit_modrm_noreg(p
, argmem_noreg
, arg
);
726 void x87_fmul( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg arg
)
728 x87_arith_op(p
, dst
, arg
,
734 void x87_fsub( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg arg
)
736 x87_arith_op(p
, dst
, arg
,
742 void x87_fsubr( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg arg
)
744 x87_arith_op(p
, dst
, arg
,
750 void x87_fadd( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg arg
)
752 x87_arith_op(p
, dst
, arg
,
758 void x87_fdiv( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg arg
)
760 x87_arith_op(p
, dst
, arg
,
766 void x87_fdivr( struct x86_function
*p
, struct x86_reg dst
, struct x86_reg arg
)
768 x87_arith_op(p
, dst
, arg
,
774 void x87_fmulp( struct x86_function
*p
, struct x86_reg dst
)
776 assert(dst
.file
== file_x87
);
777 assert(dst
.idx
>= 1);
778 emit_2ub(p
, 0xde, 0xc8+dst
.idx
);
781 void x87_fsubp( struct x86_function
*p
, struct x86_reg dst
)
783 assert(dst
.file
== file_x87
);
784 assert(dst
.idx
>= 1);
785 emit_2ub(p
, 0xde, 0xe8+dst
.idx
);
788 void x87_fsubrp( struct x86_function
*p
, struct x86_reg dst
)
790 assert(dst
.file
== file_x87
);
791 assert(dst
.idx
>= 1);
792 emit_2ub(p
, 0xde, 0xe0+dst
.idx
);
795 void x87_faddp( struct x86_function
*p
, struct x86_reg dst
)
797 assert(dst
.file
== file_x87
);
798 assert(dst
.idx
>= 1);
799 emit_2ub(p
, 0xde, 0xc0+dst
.idx
);
802 void x87_fdivp( struct x86_function
*p
, struct x86_reg dst
)
804 assert(dst
.file
== file_x87
);
805 assert(dst
.idx
>= 1);
806 emit_2ub(p
, 0xde, 0xf8+dst
.idx
);
809 void x87_fdivrp( struct x86_function
*p
, struct x86_reg dst
)
811 assert(dst
.file
== file_x87
);
812 assert(dst
.idx
>= 1);
813 emit_2ub(p
, 0xde, 0xf0+dst
.idx
);
816 void x87_fucom( struct x86_function
*p
, struct x86_reg arg
)
818 assert(arg
.file
== file_x87
);
819 emit_2ub(p
, 0xdd, 0xe0+arg
.idx
);
822 void x87_fucomp( struct x86_function
*p
, struct x86_reg arg
)
824 assert(arg
.file
== file_x87
);
825 emit_2ub(p
, 0xdd, 0xe8+arg
.idx
);
828 void x87_fucompp( struct x86_function
*p
)
830 emit_2ub(p
, 0xda, 0xe9);
833 void x87_fxch( struct x86_function
*p
, struct x86_reg arg
)
835 assert(arg
.file
== file_x87
);
836 emit_2ub(p
, 0xd9, 0xc8+arg
.idx
);
839 void x87_fabs( struct x86_function
*p
)
841 emit_2ub(p
, 0xd9, 0xe1);
844 void x87_fchs( struct x86_function
*p
)
846 emit_2ub(p
, 0xd9, 0xe0);
849 void x87_fcos( struct x86_function
*p
)
851 emit_2ub(p
, 0xd9, 0xff);
855 void x87_fprndint( struct x86_function
*p
)
857 emit_2ub(p
, 0xd9, 0xfc);
860 void x87_fscale( struct x86_function
*p
)
862 emit_2ub(p
, 0xd9, 0xfd);
865 void x87_fsin( struct x86_function
*p
)
867 emit_2ub(p
, 0xd9, 0xfe);
870 void x87_fsincos( struct x86_function
*p
)
872 emit_2ub(p
, 0xd9, 0xfb);
875 void x87_fsqrt( struct x86_function
*p
)
877 emit_2ub(p
, 0xd9, 0xfa);
880 void x87_fxtract( struct x86_function
*p
)
882 emit_2ub(p
, 0xd9, 0xf4);
887 * Restrictions: -1.0 <= st0 <= 1.0
889 void x87_f2xm1( struct x86_function
*p
)
891 emit_2ub(p
, 0xd9, 0xf0);
894 /* st1 = st1 * log2(st0);
897 void x87_fyl2x( struct x86_function
*p
)
899 emit_2ub(p
, 0xd9, 0xf1);
902 /* st1 = st1 * log2(st0 + 1.0);
905 * A fast operation, with restrictions: -.29 < st0 < .29
907 void x87_fyl2xp1( struct x86_function
*p
)
909 emit_2ub(p
, 0xd9, 0xf9);
913 void x87_fld( struct x86_function
*p
, struct x86_reg arg
)
915 if (arg
.file
== file_x87
)
916 emit_2ub(p
, 0xd9, 0xc0 + arg
.idx
);
919 emit_modrm_noreg(p
, 0, arg
);
923 void x87_fst( struct x86_function
*p
, struct x86_reg dst
)
925 if (dst
.file
== file_x87
)
926 emit_2ub(p
, 0xdd, 0xd0 + dst
.idx
);
929 emit_modrm_noreg(p
, 2, dst
);
933 void x87_fstp( struct x86_function
*p
, struct x86_reg dst
)
935 if (dst
.file
== file_x87
)
936 emit_2ub(p
, 0xdd, 0xd8 + dst
.idx
);
939 emit_modrm_noreg(p
, 3, dst
);
943 void x87_fcom( struct x86_function
*p
, struct x86_reg dst
)
945 if (dst
.file
== file_x87
)
946 emit_2ub(p
, 0xd8, 0xd0 + dst
.idx
);
949 emit_modrm_noreg(p
, 2, dst
);
953 void x87_fcomp( struct x86_function
*p
, struct x86_reg dst
)
955 if (dst
.file
== file_x87
)
956 emit_2ub(p
, 0xd8, 0xd8 + dst
.idx
);
959 emit_modrm_noreg(p
, 3, dst
);
964 void x87_fnstsw( struct x86_function
*p
, struct x86_reg dst
)
966 assert(dst
.file
== file_REG32
);
968 if (dst
.idx
== reg_AX
&&
970 emit_2ub(p
, 0xdf, 0xe0);
973 emit_modrm_noreg(p
, 7, dst
);
980 /***********************************************************************
984 void mmx_emms( struct x86_function
*p
)
986 assert(p
->need_emms
);
987 emit_2ub(p
, 0x0f, 0x77);
991 void mmx_packssdw( struct x86_function
*p
,
995 assert(dst
.file
== file_MMX
&&
996 (src
.file
== file_MMX
|| src
.mod
!= mod_REG
));
1000 emit_2ub(p
, X86_TWOB
, 0x6b);
1001 emit_modrm( p
, dst
, src
);
1004 void mmx_packuswb( struct x86_function
*p
,
1006 struct x86_reg src
)
1008 assert(dst
.file
== file_MMX
&&
1009 (src
.file
== file_MMX
|| src
.mod
!= mod_REG
));
1013 emit_2ub(p
, X86_TWOB
, 0x67);
1014 emit_modrm( p
, dst
, src
);
1017 void mmx_movd( struct x86_function
*p
,
1019 struct x86_reg src
)
1022 emit_1ub(p
, X86_TWOB
);
1023 emit_op_modrm( p
, 0x6e, 0x7e, dst
, src
);
1026 void mmx_movq( struct x86_function
*p
,
1028 struct x86_reg src
)
1031 emit_1ub(p
, X86_TWOB
);
1032 emit_op_modrm( p
, 0x6f, 0x7f, dst
, src
);
1036 /***********************************************************************
1041 /* Retreive a reference to one of the function arguments, taking into
1042 * account any push/pop activity:
1044 struct x86_reg
x86_fn_arg( struct x86_function
*p
,
1047 return x86_make_disp(x86_make_reg(file_REG32
, reg_SP
),
1048 p
->stack_offset
+ arg
* 4); /* ??? */
1052 void x86_init_func( struct x86_function
*p
)
1054 x86_init_func_size(p
, 1024);
1057 void x86_init_func_size( struct x86_function
*p
, GLuint code_size
)
1059 p
->store
= _mesa_exec_malloc(code_size
);
1063 void x86_release_func( struct x86_function
*p
)
1065 _mesa_exec_free(p
->store
);
1069 void (*x86_get_func( struct x86_function
*p
))(void)
1072 _mesa_printf("disassemble %p %p\n", p
->store
, p
->csr
);
1073 return (void (*)(void))p
->store
;
1078 void x86sse_dummy( void )