2 #if defined(__i386__) || defined(__386__)
4 #include "main/imports.h"
11 static unsigned char *cptr( void (*label)() )
13 return (unsigned char *)(unsigned long)label;
18 static void do_realloc( struct x86_function *p )
22 p->store = _mesa_exec_malloc(p->size);
26 unsigned used = p->csr - p->store;
27 unsigned char *tmp = p->store;
29 p->store = _mesa_exec_malloc(p->size);
30 memcpy(p->store, tmp, used);
31 p->csr = p->store + used;
36 /* Emit bytes to the instruction stream:
38 static unsigned char *reserve( struct x86_function *p, int bytes )
40 if (p->csr + bytes - p->store > p->size)
44 unsigned char *csr = p->csr;
52 static void emit_1b( struct x86_function *p, char b0 )
54 char *csr = (char *)reserve(p, 1);
58 static void emit_1i( struct x86_function *p, int i0 )
60 int *icsr = (int *)reserve(p, sizeof(i0));
64 static void emit_1ub( struct x86_function *p, unsigned char b0 )
66 unsigned char *csr = reserve(p, 1);
70 static void emit_2ub( struct x86_function *p, unsigned char b0, unsigned char b1 )
72 unsigned char *csr = reserve(p, 2);
77 static void emit_3ub( struct x86_function *p, unsigned char b0, unsigned char b1, unsigned char b2 )
79 unsigned char *csr = reserve(p, 3);
86 /* Build a modRM byte + possible displacement. No treatment of SIB
87 * indexing. BZZT - no way to encode an absolute address.
89 static void emit_modrm( struct x86_function *p,
91 struct x86_reg regmem )
93 unsigned char val = 0;
95 assert(reg.mod == mod_REG);
97 val |= regmem.mod << 6; /* mod field */
98 val |= reg.idx << 3; /* reg field */
99 val |= regmem.idx; /* r/m field */
103 /* Oh-oh we've stumbled into the SIB thing.
105 if (regmem.file == file_REG32 &&
106 regmem.idx == reg_SP) {
107 emit_1ub(p, 0x24); /* simplistic! */
110 switch (regmem.mod) {
115 emit_1b(p, regmem.disp);
118 emit_1i(p, regmem.disp);
127 static void emit_modrm_noreg( struct x86_function *p,
129 struct x86_reg regmem )
131 struct x86_reg dummy = x86_make_reg(file_REG32, op);
132 emit_modrm(p, dummy, regmem);
135 /* Many x86 instructions have two opcodes to cope with the situations
136 * where the destination is a register or memory reference
137 * respectively. This function selects the correct opcode based on
138 * the arguments presented.
140 static void emit_op_modrm( struct x86_function *p,
141 unsigned char op_dst_is_reg,
142 unsigned char op_dst_is_mem,
148 emit_1ub(p, op_dst_is_reg);
149 emit_modrm(p, dst, src);
154 assert(src.mod == mod_REG);
155 emit_1ub(p, op_dst_is_mem);
156 emit_modrm(p, src, dst);
170 /* Create and manipulate registers and regmem values:
172 struct x86_reg x86_make_reg( enum x86_reg_file file,
173 enum x86_reg_name idx )
185 struct x86_reg x86_make_disp( struct x86_reg reg,
188 assert(reg.file == file_REG32);
190 if (reg.mod == mod_REG)
196 reg.mod = mod_INDIRECT;
197 else if (reg.disp <= 127 && reg.disp >= -128)
200 reg.mod = mod_DISP32;
205 struct x86_reg x86_deref( struct x86_reg reg )
207 return x86_make_disp(reg, 0);
210 struct x86_reg x86_get_base_reg( struct x86_reg reg )
212 return x86_make_reg( reg.file, reg.idx );
215 unsigned char *x86_get_label( struct x86_function *p )
222 /***********************************************************************
227 void x86_jcc( struct x86_function *p,
229 unsigned char *label )
231 int offset = label - (x86_get_label(p) + 2);
233 if (offset <= 127 && offset >= -128) {
234 emit_1ub(p, 0x70 + cc);
235 emit_1b(p, (char) offset);
238 offset = label - (x86_get_label(p) + 6);
239 emit_2ub(p, 0x0f, 0x80 + cc);
244 /* Always use a 32bit offset for forward jumps:
246 unsigned char *x86_jcc_forward( struct x86_function *p,
249 emit_2ub(p, 0x0f, 0x80 + cc);
251 return x86_get_label(p);
254 unsigned char *x86_jmp_forward( struct x86_function *p)
258 return x86_get_label(p);
261 unsigned char *x86_call_forward( struct x86_function *p)
265 return x86_get_label(p);
268 /* Fixup offset from forward jump:
270 void x86_fixup_fwd_jump( struct x86_function *p,
271 unsigned char *fixup )
273 *(int *)(fixup - 4) = x86_get_label(p) - fixup;
276 void x86_jmp( struct x86_function *p, unsigned char *label)
279 emit_1i(p, label - x86_get_label(p) - 4);
283 /* This doesn't work once we start reallocating & copying the
284 * generated code on buffer fills, because the call is relative to the
287 void x86_call( struct x86_function *p, void (*label)())
290 emit_1i(p, cptr(label) - x86_get_label(p) - 4);
293 void x86_call( struct x86_function *p, struct x86_reg reg)
296 emit_modrm_noreg(p, 2, reg);
302 * Temporary. As I need immediate operands, and dont want to mess with the codegen,
303 * I load the immediate into general purpose register and use it.
305 void x86_mov_reg_imm( struct x86_function *p, struct x86_reg dst, int imm )
307 assert(dst.mod == mod_REG);
308 emit_1ub(p, 0xb8 + dst.idx);
312 void x86_push( struct x86_function *p,
315 assert(reg.mod == mod_REG);
316 emit_1ub(p, 0x50 + reg.idx);
317 p->stack_offset += 4;
320 void x86_pop( struct x86_function *p,
323 assert(reg.mod == mod_REG);
324 emit_1ub(p, 0x58 + reg.idx);
325 p->stack_offset -= 4;
328 void x86_inc( struct x86_function *p,
331 assert(reg.mod == mod_REG);
332 emit_1ub(p, 0x40 + reg.idx);
335 void x86_dec( struct x86_function *p,
338 assert(reg.mod == mod_REG);
339 emit_1ub(p, 0x48 + reg.idx);
342 void x86_ret( struct x86_function *p )
347 void x86_sahf( struct x86_function *p )
352 void x86_mov( struct x86_function *p,
356 emit_op_modrm( p, 0x8b, 0x89, dst, src );
359 void x86_xor( struct x86_function *p,
363 emit_op_modrm( p, 0x33, 0x31, dst, src );
366 void x86_cmp( struct x86_function *p,
370 emit_op_modrm( p, 0x3b, 0x39, dst, src );
373 void x86_lea( struct x86_function *p,
378 emit_modrm( p, dst, src );
381 void x86_test( struct x86_function *p,
386 emit_modrm( p, dst, src );
389 void x86_add( struct x86_function *p,
393 emit_op_modrm(p, 0x03, 0x01, dst, src );
396 void x86_mul( struct x86_function *p,
399 assert (src.file == file_REG32 && src.mod == mod_REG);
400 emit_op_modrm(p, 0xf7, 0, x86_make_reg (file_REG32, reg_SP), src );
403 void x86_sub( struct x86_function *p,
407 emit_op_modrm(p, 0x2b, 0x29, dst, src );
410 void x86_or( struct x86_function *p,
414 emit_op_modrm( p, 0x0b, 0x09, dst, src );
417 void x86_and( struct x86_function *p,
421 emit_op_modrm( p, 0x23, 0x21, dst, src );
426 /***********************************************************************
431 void sse_movss( struct x86_function *p,
435 emit_2ub(p, 0xF3, X86_TWOB);
436 emit_op_modrm( p, 0x10, 0x11, dst, src );
439 void sse_movaps( struct x86_function *p,
443 emit_1ub(p, X86_TWOB);
444 emit_op_modrm( p, 0x28, 0x29, dst, src );
447 void sse_movups( struct x86_function *p,
451 emit_1ub(p, X86_TWOB);
452 emit_op_modrm( p, 0x10, 0x11, dst, src );
455 void sse_movhps( struct x86_function *p,
459 assert(dst.mod != mod_REG || src.mod != mod_REG);
460 emit_1ub(p, X86_TWOB);
461 emit_op_modrm( p, 0x16, 0x17, dst, src ); /* cf movlhps */
464 void sse_movlps( struct x86_function *p,
468 assert(dst.mod != mod_REG || src.mod != mod_REG);
469 emit_1ub(p, X86_TWOB);
470 emit_op_modrm( p, 0x12, 0x13, dst, src ); /* cf movhlps */
473 void sse_maxps( struct x86_function *p,
477 emit_2ub(p, X86_TWOB, 0x5F);
478 emit_modrm( p, dst, src );
481 void sse_maxss( struct x86_function *p,
485 emit_3ub(p, 0xF3, X86_TWOB, 0x5F);
486 emit_modrm( p, dst, src );
489 void sse_divss( struct x86_function *p,
493 emit_3ub(p, 0xF3, X86_TWOB, 0x5E);
494 emit_modrm( p, dst, src );
497 void sse_minps( struct x86_function *p,
501 emit_2ub(p, X86_TWOB, 0x5D);
502 emit_modrm( p, dst, src );
505 void sse_subps( struct x86_function *p,
509 emit_2ub(p, X86_TWOB, 0x5C);
510 emit_modrm( p, dst, src );
513 void sse_mulps( struct x86_function *p,
517 emit_2ub(p, X86_TWOB, 0x59);
518 emit_modrm( p, dst, src );
521 void sse_mulss( struct x86_function *p,
525 emit_3ub(p, 0xF3, X86_TWOB, 0x59);
526 emit_modrm( p, dst, src );
529 void sse_addps( struct x86_function *p,
533 emit_2ub(p, X86_TWOB, 0x58);
534 emit_modrm( p, dst, src );
537 void sse_addss( struct x86_function *p,
541 emit_3ub(p, 0xF3, X86_TWOB, 0x58);
542 emit_modrm( p, dst, src );
545 void sse_andnps( struct x86_function *p,
549 emit_2ub(p, X86_TWOB, 0x55);
550 emit_modrm( p, dst, src );
553 void sse_andps( struct x86_function *p,
557 emit_2ub(p, X86_TWOB, 0x54);
558 emit_modrm( p, dst, src );
561 void sse_rsqrtps( struct x86_function *p,
565 emit_2ub(p, X86_TWOB, 0x52);
566 emit_modrm( p, dst, src );
569 void sse_rsqrtss( struct x86_function *p,
573 emit_3ub(p, 0xF3, X86_TWOB, 0x52);
574 emit_modrm( p, dst, src );
578 void sse_movhlps( struct x86_function *p,
582 assert(dst.mod == mod_REG && src.mod == mod_REG);
583 emit_2ub(p, X86_TWOB, 0x12);
584 emit_modrm( p, dst, src );
587 void sse_movlhps( struct x86_function *p,
591 assert(dst.mod == mod_REG && src.mod == mod_REG);
592 emit_2ub(p, X86_TWOB, 0x16);
593 emit_modrm( p, dst, src );
596 void sse_orps( struct x86_function *p,
600 emit_2ub(p, X86_TWOB, 0x56);
601 emit_modrm( p, dst, src );
604 void sse_xorps( struct x86_function *p,
608 emit_2ub(p, X86_TWOB, 0x57);
609 emit_modrm( p, dst, src );
612 void sse_cvtps2pi( struct x86_function *p,
616 assert(dst.file == file_MMX &&
617 (src.file == file_XMM || src.mod != mod_REG));
621 emit_2ub(p, X86_TWOB, 0x2d);
622 emit_modrm( p, dst, src );
626 /* Shufps can also be used to implement a reduced swizzle when dest ==
629 void sse_shufps( struct x86_function *p,
634 emit_2ub(p, X86_TWOB, 0xC6);
635 emit_modrm(p, dest, arg0);
639 void sse_cmpps( struct x86_function *p,
644 emit_2ub(p, X86_TWOB, 0xC2);
645 emit_modrm(p, dest, arg0);
649 void sse_pmovmskb( struct x86_function *p,
653 emit_3ub(p, 0x66, X86_TWOB, 0xD7);
654 emit_modrm(p, dest, src);
657 /***********************************************************************
662 * Perform a reduced swizzle:
664 void sse2_pshufd( struct x86_function *p,
669 emit_3ub(p, 0x66, X86_TWOB, 0x70);
670 emit_modrm(p, dest, arg0);
674 void sse2_cvttps2dq( struct x86_function *p,
678 emit_3ub( p, 0xF3, X86_TWOB, 0x5B );
679 emit_modrm( p, dst, src );
682 void sse2_cvtps2dq( struct x86_function *p,
686 emit_3ub(p, 0x66, X86_TWOB, 0x5B);
687 emit_modrm( p, dst, src );
690 void sse2_packssdw( struct x86_function *p,
694 emit_3ub(p, 0x66, X86_TWOB, 0x6B);
695 emit_modrm( p, dst, src );
698 void sse2_packsswb( struct x86_function *p,
702 emit_3ub(p, 0x66, X86_TWOB, 0x63);
703 emit_modrm( p, dst, src );
706 void sse2_packuswb( struct x86_function *p,
710 emit_3ub(p, 0x66, X86_TWOB, 0x67);
711 emit_modrm( p, dst, src );
714 void sse2_rcpps( struct x86_function *p,
718 emit_2ub(p, X86_TWOB, 0x53);
719 emit_modrm( p, dst, src );
722 void sse2_rcpss( struct x86_function *p,
726 emit_3ub(p, 0xF3, X86_TWOB, 0x53);
727 emit_modrm( p, dst, src );
730 void sse2_movd( struct x86_function *p,
734 emit_2ub(p, 0x66, X86_TWOB);
735 emit_op_modrm( p, 0x6e, 0x7e, dst, src );
741 /***********************************************************************
744 void x87_fist( struct x86_function *p, struct x86_reg dst )
747 emit_modrm_noreg(p, 2, dst);
750 void x87_fistp( struct x86_function *p, struct x86_reg dst )
753 emit_modrm_noreg(p, 3, dst);
756 void x87_fild( struct x86_function *p, struct x86_reg arg )
759 emit_modrm_noreg(p, 0, arg);
762 void x87_fldz( struct x86_function *p )
764 emit_2ub(p, 0xd9, 0xee);
768 void x87_fldcw( struct x86_function *p, struct x86_reg arg )
770 assert(arg.file == file_REG32);
771 assert(arg.mod != mod_REG);
773 emit_modrm_noreg(p, 5, arg);
776 void x87_fld1( struct x86_function *p )
778 emit_2ub(p, 0xd9, 0xe8);
781 void x87_fldl2e( struct x86_function *p )
783 emit_2ub(p, 0xd9, 0xea);
786 void x87_fldln2( struct x86_function *p )
788 emit_2ub(p, 0xd9, 0xed);
791 void x87_fwait( struct x86_function *p )
796 void x87_fnclex( struct x86_function *p )
798 emit_2ub(p, 0xdb, 0xe2);
801 void x87_fclex( struct x86_function *p )
808 static void x87_arith_op( struct x86_function *p, struct x86_reg dst, struct x86_reg arg,
809 unsigned char dst0ub0,
810 unsigned char dst0ub1,
811 unsigned char arg0ub0,
812 unsigned char arg0ub1,
813 unsigned char argmem_noreg)
815 assert(dst.file == file_x87);
817 if (arg.file == file_x87) {
819 emit_2ub(p, dst0ub0, dst0ub1+arg.idx);
820 else if (arg.idx == 0)
821 emit_2ub(p, arg0ub0, arg0ub1+arg.idx);
825 else if (dst.idx == 0) {
826 assert(arg.file == file_REG32);
828 emit_modrm_noreg(p, argmem_noreg, arg);
834 void x87_fmul( struct x86_function *p, struct x86_reg dst, struct x86_reg arg )
836 x87_arith_op(p, dst, arg,
842 void x87_fsub( struct x86_function *p, struct x86_reg dst, struct x86_reg arg )
844 x87_arith_op(p, dst, arg,
850 void x87_fsubr( struct x86_function *p, struct x86_reg dst, struct x86_reg arg )
852 x87_arith_op(p, dst, arg,
858 void x87_fadd( struct x86_function *p, struct x86_reg dst, struct x86_reg arg )
860 x87_arith_op(p, dst, arg,
866 void x87_fdiv( struct x86_function *p, struct x86_reg dst, struct x86_reg arg )
868 x87_arith_op(p, dst, arg,
874 void x87_fdivr( struct x86_function *p, struct x86_reg dst, struct x86_reg arg )
876 x87_arith_op(p, dst, arg,
882 void x87_fmulp( struct x86_function *p, struct x86_reg dst )
884 assert(dst.file == file_x87);
885 assert(dst.idx >= 1);
886 emit_2ub(p, 0xde, 0xc8+dst.idx);
889 void x87_fsubp( struct x86_function *p, struct x86_reg dst )
891 assert(dst.file == file_x87);
892 assert(dst.idx >= 1);
893 emit_2ub(p, 0xde, 0xe8+dst.idx);
896 void x87_fsubrp( struct x86_function *p, struct x86_reg dst )
898 assert(dst.file == file_x87);
899 assert(dst.idx >= 1);
900 emit_2ub(p, 0xde, 0xe0+dst.idx);
903 void x87_faddp( struct x86_function *p, struct x86_reg dst )
905 assert(dst.file == file_x87);
906 assert(dst.idx >= 1);
907 emit_2ub(p, 0xde, 0xc0+dst.idx);
910 void x87_fdivp( struct x86_function *p, struct x86_reg dst )
912 assert(dst.file == file_x87);
913 assert(dst.idx >= 1);
914 emit_2ub(p, 0xde, 0xf8+dst.idx);
917 void x87_fdivrp( struct x86_function *p, struct x86_reg dst )
919 assert(dst.file == file_x87);
920 assert(dst.idx >= 1);
921 emit_2ub(p, 0xde, 0xf0+dst.idx);
924 void x87_fucom( struct x86_function *p, struct x86_reg arg )
926 assert(arg.file == file_x87);
927 emit_2ub(p, 0xdd, 0xe0+arg.idx);
930 void x87_fucomp( struct x86_function *p, struct x86_reg arg )
932 assert(arg.file == file_x87);
933 emit_2ub(p, 0xdd, 0xe8+arg.idx);
936 void x87_fucompp( struct x86_function *p )
938 emit_2ub(p, 0xda, 0xe9);
941 void x87_fxch( struct x86_function *p, struct x86_reg arg )
943 assert(arg.file == file_x87);
944 emit_2ub(p, 0xd9, 0xc8+arg.idx);
947 void x87_fabs( struct x86_function *p )
949 emit_2ub(p, 0xd9, 0xe1);
952 void x87_fchs( struct x86_function *p )
954 emit_2ub(p, 0xd9, 0xe0);
957 void x87_fcos( struct x86_function *p )
959 emit_2ub(p, 0xd9, 0xff);
963 void x87_fprndint( struct x86_function *p )
965 emit_2ub(p, 0xd9, 0xfc);
968 void x87_fscale( struct x86_function *p )
970 emit_2ub(p, 0xd9, 0xfd);
973 void x87_fsin( struct x86_function *p )
975 emit_2ub(p, 0xd9, 0xfe);
978 void x87_fsincos( struct x86_function *p )
980 emit_2ub(p, 0xd9, 0xfb);
983 void x87_fsqrt( struct x86_function *p )
985 emit_2ub(p, 0xd9, 0xfa);
988 void x87_fxtract( struct x86_function *p )
990 emit_2ub(p, 0xd9, 0xf4);
995 * Restrictions: -1.0 <= st0 <= 1.0
997 void x87_f2xm1( struct x86_function *p )
999 emit_2ub(p, 0xd9, 0xf0);
1002 /* st1 = st1 * log2(st0);
1005 void x87_fyl2x( struct x86_function *p )
1007 emit_2ub(p, 0xd9, 0xf1);
1010 /* st1 = st1 * log2(st0 + 1.0);
1013 * A fast operation, with restrictions: -.29 < st0 < .29
1015 void x87_fyl2xp1( struct x86_function *p )
1017 emit_2ub(p, 0xd9, 0xf9);
1021 void x87_fld( struct x86_function *p, struct x86_reg arg )
1023 if (arg.file == file_x87)
1024 emit_2ub(p, 0xd9, 0xc0 + arg.idx);
1027 emit_modrm_noreg(p, 0, arg);
1031 void x87_fst( struct x86_function *p, struct x86_reg dst )
1033 if (dst.file == file_x87)
1034 emit_2ub(p, 0xdd, 0xd0 + dst.idx);
1037 emit_modrm_noreg(p, 2, dst);
1041 void x87_fstp( struct x86_function *p, struct x86_reg dst )
1043 if (dst.file == file_x87)
1044 emit_2ub(p, 0xdd, 0xd8 + dst.idx);
1047 emit_modrm_noreg(p, 3, dst);
1051 void x87_fcom( struct x86_function *p, struct x86_reg dst )
1053 if (dst.file == file_x87)
1054 emit_2ub(p, 0xd8, 0xd0 + dst.idx);
1057 emit_modrm_noreg(p, 2, dst);
1061 void x87_fcomp( struct x86_function *p, struct x86_reg dst )
1063 if (dst.file == file_x87)
1064 emit_2ub(p, 0xd8, 0xd8 + dst.idx);
1067 emit_modrm_noreg(p, 3, dst);
1072 void x87_fnstsw( struct x86_function *p, struct x86_reg dst )
1074 assert(dst.file == file_REG32);
1076 if (dst.idx == reg_AX &&
1078 emit_2ub(p, 0xdf, 0xe0);
1081 emit_modrm_noreg(p, 7, dst);
1088 /***********************************************************************
1092 void mmx_emms( struct x86_function *p )
1094 assert(p->need_emms);
1095 emit_2ub(p, 0x0f, 0x77);
1099 void mmx_packssdw( struct x86_function *p,
1101 struct x86_reg src )
1103 assert(dst.file == file_MMX &&
1104 (src.file == file_MMX || src.mod != mod_REG));
1108 emit_2ub(p, X86_TWOB, 0x6b);
1109 emit_modrm( p, dst, src );
1112 void mmx_packuswb( struct x86_function *p,
1114 struct x86_reg src )
1116 assert(dst.file == file_MMX &&
1117 (src.file == file_MMX || src.mod != mod_REG));
1121 emit_2ub(p, X86_TWOB, 0x67);
1122 emit_modrm( p, dst, src );
1125 void mmx_movd( struct x86_function *p,
1127 struct x86_reg src )
1130 emit_1ub(p, X86_TWOB);
1131 emit_op_modrm( p, 0x6e, 0x7e, dst, src );
1134 void mmx_movq( struct x86_function *p,
1136 struct x86_reg src )
1139 emit_1ub(p, X86_TWOB);
1140 emit_op_modrm( p, 0x6f, 0x7f, dst, src );
1144 /***********************************************************************
1149 /* Retreive a reference to one of the function arguments, taking into
1150 * account any push/pop activity:
1152 struct x86_reg x86_fn_arg( struct x86_function *p,
1155 return x86_make_disp(x86_make_reg(file_REG32, reg_SP),
1156 p->stack_offset + arg * 4); /* ??? */
1160 void x86_init_func( struct x86_function *p )
1167 int x86_init_func_size( struct x86_function *p, unsigned code_size )
1169 p->size = code_size;
1170 p->store = _mesa_exec_malloc(code_size);
1172 return p->store != NULL;
1175 void x86_release_func( struct x86_function *p )
1177 _mesa_exec_free(p->store);
1184 void (*x86_get_func( struct x86_function *p ))(void)
1186 if (DISASSEM && p->store)
1187 printf("disassemble %p %p\n", p->store, p->csr);
1188 return (void (*)(void)) (unsigned long) p->store;
1193 void x86sse_dummy( void )
1199 #else /* USE_X86_ASM */
1201 int x86sse_c_dummy_var; /* silence warning */
1203 #endif /* USE_X86_ASM */