x86sse.c revision 0397b2bb41b0f337af2949a15bcd7d0e7e8a7dc1
1#ifdef USE_X86_ASM
2#if defined(__i386__) || defined(__386__)
3
4#include "main/imports.h"
5#include "x86sse.h"
6
7#define DISASSEM 0
8#define X86_TWOB 0x0f
9
10static unsigned char *cptr( void (*label)() )
11{
12   return (unsigned char *)(unsigned long)label;
13}
14
15
16static void do_realloc( struct x86_function *p )
17{
18   if (p->size == 0) {
19      p->size = 1024;
20      p->store = _mesa_exec_malloc(p->size);
21      p->csr = p->store;
22   }
23   else {
24      unsigned used = p->csr - p->store;
25      unsigned char *tmp = p->store;
26      p->size *= 2;
27      p->store = _mesa_exec_malloc(p->size);
28      memcpy(p->store, tmp, used);
29      p->csr = p->store + used;
30      _mesa_exec_free(tmp);
31   }
32}
33
34/* Emit bytes to the instruction stream:
35 */
36static unsigned char *reserve( struct x86_function *p, int bytes )
37{
38   if (p->csr + bytes - p->store > p->size)
39      do_realloc(p);
40
41   {
42      unsigned char *csr = p->csr;
43      p->csr += bytes;
44      return csr;
45   }
46}
47
48
49
50static void emit_1b( struct x86_function *p, char b0 )
51{
52   char *csr = (char *)reserve(p, 1);
53   *csr = b0;
54}
55
56static void emit_1i( struct x86_function *p, int i0 )
57{
58   int *icsr = (int *)reserve(p, sizeof(i0));
59   *icsr = i0;
60}
61
62static void emit_1ub( struct x86_function *p, unsigned char b0 )
63{
64   unsigned char *csr = reserve(p, 1);
65   *csr++ = b0;
66}
67
68static void emit_2ub( struct x86_function *p, unsigned char b0, unsigned char b1 )
69{
70   unsigned char *csr = reserve(p, 2);
71   *csr++ = b0;
72   *csr++ = b1;
73}
74
75static void emit_3ub( struct x86_function *p, unsigned char b0, unsigned char b1, unsigned char b2 )
76{
77   unsigned char *csr = reserve(p, 3);
78   *csr++ = b0;
79   *csr++ = b1;
80   *csr++ = b2;
81}
82
83
84/* Build a modRM byte + possible displacement.  No treatment of SIB
85 * indexing.  BZZT - no way to encode an absolute address.
86 */
87static void emit_modrm( struct x86_function *p,
88			struct x86_reg reg,
89			struct x86_reg regmem )
90{
91   unsigned char val = 0;
92
93   assert(reg.mod == mod_REG);
94
95   val |= regmem.mod << 6;     	/* mod field */
96   val |= reg.idx << 3;		/* reg field */
97   val |= regmem.idx;		/* r/m field */
98
99   emit_1ub(p, val);
100
101   /* Oh-oh we've stumbled into the SIB thing.
102    */
103   if (regmem.file == file_REG32 &&
104       regmem.idx == reg_SP) {
105      emit_1ub(p, 0x24);		/* simplistic! */
106   }
107
108   switch (regmem.mod) {
109   case mod_REG:
110   case mod_INDIRECT:
111      break;
112   case mod_DISP8:
113      emit_1b(p, regmem.disp);
114      break;
115   case mod_DISP32:
116      emit_1i(p, regmem.disp);
117      break;
118   default:
119      assert(0);
120      break;
121   }
122}
123
124
125static void emit_modrm_noreg( struct x86_function *p,
126			      unsigned op,
127			      struct x86_reg regmem )
128{
129   struct x86_reg dummy = x86_make_reg(file_REG32, op);
130   emit_modrm(p, dummy, regmem);
131}
132
133/* Many x86 instructions have two opcodes to cope with the situations
134 * where the destination is a register or memory reference
135 * respectively.  This function selects the correct opcode based on
136 * the arguments presented.
137 */
138static void emit_op_modrm( struct x86_function *p,
139			   unsigned char op_dst_is_reg,
140			   unsigned char op_dst_is_mem,
141			   struct x86_reg dst,
142			   struct x86_reg src )
143{
144   switch (dst.mod) {
145   case mod_REG:
146      emit_1ub(p, op_dst_is_reg);
147      emit_modrm(p, dst, src);
148      break;
149   case mod_INDIRECT:
150   case mod_DISP32:
151   case mod_DISP8:
152      assert(src.mod == mod_REG);
153      emit_1ub(p, op_dst_is_mem);
154      emit_modrm(p, src, dst);
155      break;
156   default:
157      assert(0);
158      break;
159   }
160}
161
162
163
164
165
166
167
168/* Create and manipulate registers and regmem values:
169 */
170struct x86_reg x86_make_reg( enum x86_reg_file file,
171			     enum x86_reg_name idx )
172{
173   struct x86_reg reg;
174
175   reg.file = file;
176   reg.idx = idx;
177   reg.mod = mod_REG;
178   reg.disp = 0;
179
180   return reg;
181}
182
183struct x86_reg x86_make_disp( struct x86_reg reg,
184			      int disp )
185{
186   assert(reg.file == file_REG32);
187
188   if (reg.mod == mod_REG)
189      reg.disp = disp;
190   else
191      reg.disp += disp;
192
193   if (reg.disp == 0)
194      reg.mod = mod_INDIRECT;
195   else if (reg.disp <= 127 && reg.disp >= -128)
196      reg.mod = mod_DISP8;
197   else
198      reg.mod = mod_DISP32;
199
200   return reg;
201}
202
203struct x86_reg x86_deref( struct x86_reg reg )
204{
205   return x86_make_disp(reg, 0);
206}
207
208struct x86_reg x86_get_base_reg( struct x86_reg reg )
209{
210   return x86_make_reg( reg.file, reg.idx );
211}
212
213unsigned char *x86_get_label( struct x86_function *p )
214{
215   return p->csr;
216}
217
218
219
220/***********************************************************************
221 * x86 instructions
222 */
223
224
225void x86_jcc( struct x86_function *p,
226	      enum x86_cc cc,
227	      unsigned char *label )
228{
229   int offset = label - (x86_get_label(p) + 2);
230
231   if (offset <= 127 && offset >= -128) {
232      emit_1ub(p, 0x70 + cc);
233      emit_1b(p, (char) offset);
234   }
235   else {
236      offset = label - (x86_get_label(p) + 6);
237      emit_2ub(p, 0x0f, 0x80 + cc);
238      emit_1i(p, offset);
239   }
240}
241
242/* Always use a 32bit offset for forward jumps:
243 */
244unsigned char *x86_jcc_forward( struct x86_function *p,
245			  enum x86_cc cc )
246{
247   emit_2ub(p, 0x0f, 0x80 + cc);
248   emit_1i(p, 0);
249   return x86_get_label(p);
250}
251
252unsigned char *x86_jmp_forward( struct x86_function *p)
253{
254   emit_1ub(p, 0xe9);
255   emit_1i(p, 0);
256   return x86_get_label(p);
257}
258
259unsigned char *x86_call_forward( struct x86_function *p)
260{
261   emit_1ub(p, 0xe8);
262   emit_1i(p, 0);
263   return x86_get_label(p);
264}
265
266/* Fixup offset from forward jump:
267 */
268void x86_fixup_fwd_jump( struct x86_function *p,
269			 unsigned char *fixup )
270{
271   *(int *)(fixup - 4) = x86_get_label(p) - fixup;
272}
273
274void x86_jmp( struct x86_function *p, unsigned char *label)
275{
276   emit_1ub(p, 0xe9);
277   emit_1i(p, label - x86_get_label(p) - 4);
278}
279
280#if 0
281/* This doesn't work once we start reallocating & copying the
282 * generated code on buffer fills, because the call is relative to the
283 * current pc.
284 */
285void x86_call( struct x86_function *p, void (*label)())
286{
287   emit_1ub(p, 0xe8);
288   emit_1i(p, cptr(label) - x86_get_label(p) - 4);
289}
290#else
291void x86_call( struct x86_function *p, struct x86_reg reg)
292{
293   emit_1ub(p, 0xff);
294   emit_modrm_noreg(p, 2, reg);
295}
296#endif
297
298
299/* michal:
300 * Temporary. As I need immediate operands, and dont want to mess with the codegen,
301 * I load the immediate into general purpose register and use it.
302 */
303void x86_mov_reg_imm( struct x86_function *p, struct x86_reg dst, int imm )
304{
305   assert(dst.mod == mod_REG);
306   emit_1ub(p, 0xb8 + dst.idx);
307   emit_1i(p, imm);
308}
309
310void x86_push( struct x86_function *p,
311	       struct x86_reg reg )
312{
313   assert(reg.mod == mod_REG);
314   emit_1ub(p, 0x50 + reg.idx);
315   p->stack_offset += 4;
316}
317
318void x86_pop( struct x86_function *p,
319	      struct x86_reg reg )
320{
321   assert(reg.mod == mod_REG);
322   emit_1ub(p, 0x58 + reg.idx);
323   p->stack_offset -= 4;
324}
325
326void x86_inc( struct x86_function *p,
327	      struct x86_reg reg )
328{
329   assert(reg.mod == mod_REG);
330   emit_1ub(p, 0x40 + reg.idx);
331}
332
333void x86_dec( struct x86_function *p,
334	      struct x86_reg reg )
335{
336   assert(reg.mod == mod_REG);
337   emit_1ub(p, 0x48 + reg.idx);
338}
339
340void x86_ret( struct x86_function *p )
341{
342   emit_1ub(p, 0xc3);
343}
344
345void x86_sahf( struct x86_function *p )
346{
347   emit_1ub(p, 0x9e);
348}
349
350void x86_mov( struct x86_function *p,
351	      struct x86_reg dst,
352	      struct x86_reg src )
353{
354   emit_op_modrm( p, 0x8b, 0x89, dst, src );
355}
356
357void x86_xor( struct x86_function *p,
358	      struct x86_reg dst,
359	      struct x86_reg src )
360{
361   emit_op_modrm( p, 0x33, 0x31, dst, src );
362}
363
364void x86_cmp( struct x86_function *p,
365	      struct x86_reg dst,
366	      struct x86_reg src )
367{
368   emit_op_modrm( p, 0x3b, 0x39, dst, src );
369}
370
371void x86_lea( struct x86_function *p,
372	      struct x86_reg dst,
373	      struct x86_reg src )
374{
375   emit_1ub(p, 0x8d);
376   emit_modrm( p, dst, src );
377}
378
379void x86_test( struct x86_function *p,
380	       struct x86_reg dst,
381	       struct x86_reg src )
382{
383   emit_1ub(p, 0x85);
384   emit_modrm( p, dst, src );
385}
386
387void x86_add( struct x86_function *p,
388	       struct x86_reg dst,
389	       struct x86_reg src )
390{
391   emit_op_modrm(p, 0x03, 0x01, dst, src );
392}
393
394void x86_mul( struct x86_function *p,
395	       struct x86_reg src )
396{
397   assert (src.file == file_REG32 && src.mod == mod_REG);
398   emit_op_modrm(p, 0xf7, 0, x86_make_reg (file_REG32, reg_SP), src );
399}
400
401void x86_sub( struct x86_function *p,
402	       struct x86_reg dst,
403	       struct x86_reg src )
404{
405   emit_op_modrm(p, 0x2b, 0x29, dst, src );
406}
407
408void x86_or( struct x86_function *p,
409             struct x86_reg dst,
410             struct x86_reg src )
411{
412   emit_op_modrm( p, 0x0b, 0x09, dst, src );
413}
414
415void x86_and( struct x86_function *p,
416              struct x86_reg dst,
417              struct x86_reg src )
418{
419   emit_op_modrm( p, 0x23, 0x21, dst, src );
420}
421
422
423
424/***********************************************************************
425 * SSE instructions
426 */
427
428
429void sse_movss( struct x86_function *p,
430		struct x86_reg dst,
431		struct x86_reg src )
432{
433   emit_2ub(p, 0xF3, X86_TWOB);
434   emit_op_modrm( p, 0x10, 0x11, dst, src );
435}
436
437void sse_movaps( struct x86_function *p,
438		 struct x86_reg dst,
439		 struct x86_reg src )
440{
441   emit_1ub(p, X86_TWOB);
442   emit_op_modrm( p, 0x28, 0x29, dst, src );
443}
444
445void sse_movups( struct x86_function *p,
446		 struct x86_reg dst,
447		 struct x86_reg src )
448{
449   emit_1ub(p, X86_TWOB);
450   emit_op_modrm( p, 0x10, 0x11, dst, src );
451}
452
453void sse_movhps( struct x86_function *p,
454		 struct x86_reg dst,
455		 struct x86_reg src )
456{
457   assert(dst.mod != mod_REG || src.mod != mod_REG);
458   emit_1ub(p, X86_TWOB);
459   emit_op_modrm( p, 0x16, 0x17, dst, src ); /* cf movlhps */
460}
461
462void sse_movlps( struct x86_function *p,
463		 struct x86_reg dst,
464		 struct x86_reg src )
465{
466   assert(dst.mod != mod_REG || src.mod != mod_REG);
467   emit_1ub(p, X86_TWOB);
468   emit_op_modrm( p, 0x12, 0x13, dst, src ); /* cf movhlps */
469}
470
471void sse_maxps( struct x86_function *p,
472		struct x86_reg dst,
473		struct x86_reg src )
474{
475   emit_2ub(p, X86_TWOB, 0x5F);
476   emit_modrm( p, dst, src );
477}
478
479void sse_maxss( struct x86_function *p,
480		struct x86_reg dst,
481		struct x86_reg src )
482{
483   emit_3ub(p, 0xF3, X86_TWOB, 0x5F);
484   emit_modrm( p, dst, src );
485}
486
487void sse_divss( struct x86_function *p,
488		struct x86_reg dst,
489		struct x86_reg src )
490{
491   emit_3ub(p, 0xF3, X86_TWOB, 0x5E);
492   emit_modrm( p, dst, src );
493}
494
495void sse_minps( struct x86_function *p,
496		struct x86_reg dst,
497		struct x86_reg src )
498{
499   emit_2ub(p, X86_TWOB, 0x5D);
500   emit_modrm( p, dst, src );
501}
502
503void sse_subps( struct x86_function *p,
504		struct x86_reg dst,
505		struct x86_reg src )
506{
507   emit_2ub(p, X86_TWOB, 0x5C);
508   emit_modrm( p, dst, src );
509}
510
511void sse_mulps( struct x86_function *p,
512		struct x86_reg dst,
513		struct x86_reg src )
514{
515   emit_2ub(p, X86_TWOB, 0x59);
516   emit_modrm( p, dst, src );
517}
518
519void sse_mulss( struct x86_function *p,
520		struct x86_reg dst,
521		struct x86_reg src )
522{
523   emit_3ub(p, 0xF3, X86_TWOB, 0x59);
524   emit_modrm( p, dst, src );
525}
526
527void sse_addps( struct x86_function *p,
528		struct x86_reg dst,
529		struct x86_reg src )
530{
531   emit_2ub(p, X86_TWOB, 0x58);
532   emit_modrm( p, dst, src );
533}
534
535void sse_addss( struct x86_function *p,
536		struct x86_reg dst,
537		struct x86_reg src )
538{
539   emit_3ub(p, 0xF3, X86_TWOB, 0x58);
540   emit_modrm( p, dst, src );
541}
542
543void sse_andnps( struct x86_function *p,
544                 struct x86_reg dst,
545                 struct x86_reg src )
546{
547   emit_2ub(p, X86_TWOB, 0x55);
548   emit_modrm( p, dst, src );
549}
550
551void sse_andps( struct x86_function *p,
552		struct x86_reg dst,
553		struct x86_reg src )
554{
555   emit_2ub(p, X86_TWOB, 0x54);
556   emit_modrm( p, dst, src );
557}
558
559void sse_rsqrtps( struct x86_function *p,
560                  struct x86_reg dst,
561                  struct x86_reg src )
562{
563   emit_2ub(p, X86_TWOB, 0x52);
564   emit_modrm( p, dst, src );
565}
566
567void sse_rsqrtss( struct x86_function *p,
568		  struct x86_reg dst,
569		  struct x86_reg src )
570{
571   emit_3ub(p, 0xF3, X86_TWOB, 0x52);
572   emit_modrm( p, dst, src );
573
574}
575
576void sse_movhlps( struct x86_function *p,
577		  struct x86_reg dst,
578		  struct x86_reg src )
579{
580   assert(dst.mod == mod_REG && src.mod == mod_REG);
581   emit_2ub(p, X86_TWOB, 0x12);
582   emit_modrm( p, dst, src );
583}
584
585void sse_movlhps( struct x86_function *p,
586		  struct x86_reg dst,
587		  struct x86_reg src )
588{
589   assert(dst.mod == mod_REG && src.mod == mod_REG);
590   emit_2ub(p, X86_TWOB, 0x16);
591   emit_modrm( p, dst, src );
592}
593
594void sse_orps( struct x86_function *p,
595               struct x86_reg dst,
596               struct x86_reg src )
597{
598   emit_2ub(p, X86_TWOB, 0x56);
599   emit_modrm( p, dst, src );
600}
601
602void sse_xorps( struct x86_function *p,
603                struct x86_reg dst,
604                struct x86_reg src )
605{
606   emit_2ub(p, X86_TWOB, 0x57);
607   emit_modrm( p, dst, src );
608}
609
610void sse_cvtps2pi( struct x86_function *p,
611		   struct x86_reg dst,
612		   struct x86_reg src )
613{
614   assert(dst.file == file_MMX &&
615	  (src.file == file_XMM || src.mod != mod_REG));
616
617   p->need_emms = 1;
618
619   emit_2ub(p, X86_TWOB, 0x2d);
620   emit_modrm( p, dst, src );
621}
622
623
624/* Shufps can also be used to implement a reduced swizzle when dest ==
625 * arg0.
626 */
627void sse_shufps( struct x86_function *p,
628		 struct x86_reg dest,
629		 struct x86_reg arg0,
630		 unsigned char shuf)
631{
632   emit_2ub(p, X86_TWOB, 0xC6);
633   emit_modrm(p, dest, arg0);
634   emit_1ub(p, shuf);
635}
636
637void sse_cmpps( struct x86_function *p,
638		struct x86_reg dest,
639		struct x86_reg arg0,
640		unsigned char cc)
641{
642   emit_2ub(p, X86_TWOB, 0xC2);
643   emit_modrm(p, dest, arg0);
644   emit_1ub(p, cc);
645}
646
647void sse_pmovmskb( struct x86_function *p,
648                   struct x86_reg dest,
649                   struct x86_reg src)
650{
651    emit_3ub(p, 0x66, X86_TWOB, 0xD7);
652    emit_modrm(p, dest, src);
653}
654
655/***********************************************************************
656 * SSE2 instructions
657 */
658
659/**
660 * Perform a reduced swizzle:
661 */
662void sse2_pshufd( struct x86_function *p,
663		  struct x86_reg dest,
664		  struct x86_reg arg0,
665		  unsigned char shuf)
666{
667   emit_3ub(p, 0x66, X86_TWOB, 0x70);
668   emit_modrm(p, dest, arg0);
669   emit_1ub(p, shuf);
670}
671
672void sse2_cvttps2dq( struct x86_function *p,
673                     struct x86_reg dst,
674                     struct x86_reg src )
675{
676   emit_3ub( p, 0xF3, X86_TWOB, 0x5B );
677   emit_modrm( p, dst, src );
678}
679
680void sse2_cvtps2dq( struct x86_function *p,
681		    struct x86_reg dst,
682		    struct x86_reg src )
683{
684   emit_3ub(p, 0x66, X86_TWOB, 0x5B);
685   emit_modrm( p, dst, src );
686}
687
688void sse2_packssdw( struct x86_function *p,
689		    struct x86_reg dst,
690		    struct x86_reg src )
691{
692   emit_3ub(p, 0x66, X86_TWOB, 0x6B);
693   emit_modrm( p, dst, src );
694}
695
696void sse2_packsswb( struct x86_function *p,
697		    struct x86_reg dst,
698		    struct x86_reg src )
699{
700   emit_3ub(p, 0x66, X86_TWOB, 0x63);
701   emit_modrm( p, dst, src );
702}
703
704void sse2_packuswb( struct x86_function *p,
705		    struct x86_reg dst,
706		    struct x86_reg src )
707{
708   emit_3ub(p, 0x66, X86_TWOB, 0x67);
709   emit_modrm( p, dst, src );
710}
711
712void sse2_rcpps( struct x86_function *p,
713                 struct x86_reg dst,
714                 struct x86_reg src )
715{
716   emit_2ub(p, X86_TWOB, 0x53);
717   emit_modrm( p, dst, src );
718}
719
720void sse2_rcpss( struct x86_function *p,
721		struct x86_reg dst,
722		struct x86_reg src )
723{
724   emit_3ub(p, 0xF3, X86_TWOB, 0x53);
725   emit_modrm( p, dst, src );
726}
727
728void sse2_movd( struct x86_function *p,
729		struct x86_reg dst,
730		struct x86_reg src )
731{
732   emit_2ub(p, 0x66, X86_TWOB);
733   emit_op_modrm( p, 0x6e, 0x7e, dst, src );
734}
735
736
737
738
739/***********************************************************************
740 * x87 instructions
741 */
742void x87_fist( struct x86_function *p, struct x86_reg dst )
743{
744   emit_1ub(p, 0xdb);
745   emit_modrm_noreg(p, 2, dst);
746}
747
748void x87_fistp( struct x86_function *p, struct x86_reg dst )
749{
750   emit_1ub(p, 0xdb);
751   emit_modrm_noreg(p, 3, dst);
752}
753
754void x87_fild( struct x86_function *p, struct x86_reg arg )
755{
756   emit_1ub(p, 0xdf);
757   emit_modrm_noreg(p, 0, arg);
758}
759
760void x87_fldz( struct x86_function *p )
761{
762   emit_2ub(p, 0xd9, 0xee);
763}
764
765
766void x87_fldcw( struct x86_function *p, struct x86_reg arg )
767{
768   assert(arg.file == file_REG32);
769   assert(arg.mod != mod_REG);
770   emit_1ub(p, 0xd9);
771   emit_modrm_noreg(p, 5, arg);
772}
773
774void x87_fld1( struct x86_function *p )
775{
776   emit_2ub(p, 0xd9, 0xe8);
777}
778
779void x87_fldl2e( struct x86_function *p )
780{
781   emit_2ub(p, 0xd9, 0xea);
782}
783
784void x87_fldln2( struct x86_function *p )
785{
786   emit_2ub(p, 0xd9, 0xed);
787}
788
789void x87_fwait( struct x86_function *p )
790{
791   emit_1ub(p, 0x9b);
792}
793
794void x87_fnclex( struct x86_function *p )
795{
796   emit_2ub(p, 0xdb, 0xe2);
797}
798
799void x87_fclex( struct x86_function *p )
800{
801   x87_fwait(p);
802   x87_fnclex(p);
803}
804
805
806static void x87_arith_op( struct x86_function *p, struct x86_reg dst, struct x86_reg arg,
807			  unsigned char dst0ub0,
808			  unsigned char dst0ub1,
809			  unsigned char arg0ub0,
810			  unsigned char arg0ub1,
811			  unsigned char argmem_noreg)
812{
813   assert(dst.file == file_x87);
814
815   if (arg.file == file_x87) {
816      if (dst.idx == 0)
817	 emit_2ub(p, dst0ub0, dst0ub1+arg.idx);
818      else if (arg.idx == 0)
819	 emit_2ub(p, arg0ub0, arg0ub1+arg.idx);
820      else
821	 assert(0);
822   }
823   else if (dst.idx == 0) {
824      assert(arg.file == file_REG32);
825      emit_1ub(p, 0xd8);
826      emit_modrm_noreg(p, argmem_noreg, arg);
827   }
828   else
829      assert(0);
830}
831
832void x87_fmul( struct x86_function *p, struct x86_reg dst, struct x86_reg arg )
833{
834   x87_arith_op(p, dst, arg,
835		0xd8, 0xc8,
836		0xdc, 0xc8,
837		4);
838}
839
840void x87_fsub( struct x86_function *p, struct x86_reg dst, struct x86_reg arg )
841{
842   x87_arith_op(p, dst, arg,
843		0xd8, 0xe0,
844		0xdc, 0xe8,
845		4);
846}
847
848void x87_fsubr( struct x86_function *p, struct x86_reg dst, struct x86_reg arg )
849{
850   x87_arith_op(p, dst, arg,
851		0xd8, 0xe8,
852		0xdc, 0xe0,
853		5);
854}
855
856void x87_fadd( struct x86_function *p, struct x86_reg dst, struct x86_reg arg )
857{
858   x87_arith_op(p, dst, arg,
859		0xd8, 0xc0,
860		0xdc, 0xc0,
861		0);
862}
863
864void x87_fdiv( struct x86_function *p, struct x86_reg dst, struct x86_reg arg )
865{
866   x87_arith_op(p, dst, arg,
867		0xd8, 0xf0,
868		0xdc, 0xf8,
869		6);
870}
871
872void x87_fdivr( struct x86_function *p, struct x86_reg dst, struct x86_reg arg )
873{
874   x87_arith_op(p, dst, arg,
875		0xd8, 0xf8,
876		0xdc, 0xf0,
877		7);
878}
879
880void x87_fmulp( struct x86_function *p, struct x86_reg dst )
881{
882   assert(dst.file == file_x87);
883   assert(dst.idx >= 1);
884   emit_2ub(p, 0xde, 0xc8+dst.idx);
885}
886
887void x87_fsubp( struct x86_function *p, struct x86_reg dst )
888{
889   assert(dst.file == file_x87);
890   assert(dst.idx >= 1);
891   emit_2ub(p, 0xde, 0xe8+dst.idx);
892}
893
894void x87_fsubrp( struct x86_function *p, struct x86_reg dst )
895{
896   assert(dst.file == file_x87);
897   assert(dst.idx >= 1);
898   emit_2ub(p, 0xde, 0xe0+dst.idx);
899}
900
901void x87_faddp( struct x86_function *p, struct x86_reg dst )
902{
903   assert(dst.file == file_x87);
904   assert(dst.idx >= 1);
905   emit_2ub(p, 0xde, 0xc0+dst.idx);
906}
907
908void x87_fdivp( struct x86_function *p, struct x86_reg dst )
909{
910   assert(dst.file == file_x87);
911   assert(dst.idx >= 1);
912   emit_2ub(p, 0xde, 0xf8+dst.idx);
913}
914
915void x87_fdivrp( struct x86_function *p, struct x86_reg dst )
916{
917   assert(dst.file == file_x87);
918   assert(dst.idx >= 1);
919   emit_2ub(p, 0xde, 0xf0+dst.idx);
920}
921
922void x87_fucom( struct x86_function *p, struct x86_reg arg )
923{
924   assert(arg.file == file_x87);
925   emit_2ub(p, 0xdd, 0xe0+arg.idx);
926}
927
928void x87_fucomp( struct x86_function *p, struct x86_reg arg )
929{
930   assert(arg.file == file_x87);
931   emit_2ub(p, 0xdd, 0xe8+arg.idx);
932}
933
934void x87_fucompp( struct x86_function *p )
935{
936   emit_2ub(p, 0xda, 0xe9);
937}
938
939void x87_fxch( struct x86_function *p, struct x86_reg arg )
940{
941   assert(arg.file == file_x87);
942   emit_2ub(p, 0xd9, 0xc8+arg.idx);
943}
944
945void x87_fabs( struct x86_function *p )
946{
947   emit_2ub(p, 0xd9, 0xe1);
948}
949
950void x87_fchs( struct x86_function *p )
951{
952   emit_2ub(p, 0xd9, 0xe0);
953}
954
955void x87_fcos( struct x86_function *p )
956{
957   emit_2ub(p, 0xd9, 0xff);
958}
959
960
961void x87_fprndint( struct x86_function *p )
962{
963   emit_2ub(p, 0xd9, 0xfc);
964}
965
966void x87_fscale( struct x86_function *p )
967{
968   emit_2ub(p, 0xd9, 0xfd);
969}
970
971void x87_fsin( struct x86_function *p )
972{
973   emit_2ub(p, 0xd9, 0xfe);
974}
975
976void x87_fsincos( struct x86_function *p )
977{
978   emit_2ub(p, 0xd9, 0xfb);
979}
980
981void x87_fsqrt( struct x86_function *p )
982{
983   emit_2ub(p, 0xd9, 0xfa);
984}
985
986void x87_fxtract( struct x86_function *p )
987{
988   emit_2ub(p, 0xd9, 0xf4);
989}
990
991/* st0 = (2^st0)-1
992 *
993 * Restrictions: -1.0 <= st0 <= 1.0
994 */
995void x87_f2xm1( struct x86_function *p )
996{
997   emit_2ub(p, 0xd9, 0xf0);
998}
999
1000/* st1 = st1 * log2(st0);
1001 * pop_stack;
1002 */
1003void x87_fyl2x( struct x86_function *p )
1004{
1005   emit_2ub(p, 0xd9, 0xf1);
1006}
1007
1008/* st1 = st1 * log2(st0 + 1.0);
1009 * pop_stack;
1010 *
1011 * A fast operation, with restrictions: -.29 < st0 < .29
1012 */
1013void x87_fyl2xp1( struct x86_function *p )
1014{
1015   emit_2ub(p, 0xd9, 0xf9);
1016}
1017
1018
1019void x87_fld( struct x86_function *p, struct x86_reg arg )
1020{
1021   if (arg.file == file_x87)
1022      emit_2ub(p, 0xd9, 0xc0 + arg.idx);
1023   else {
1024      emit_1ub(p, 0xd9);
1025      emit_modrm_noreg(p, 0, arg);
1026   }
1027}
1028
1029void x87_fst( struct x86_function *p, struct x86_reg dst )
1030{
1031   if (dst.file == file_x87)
1032      emit_2ub(p, 0xdd, 0xd0 + dst.idx);
1033   else {
1034      emit_1ub(p, 0xd9);
1035      emit_modrm_noreg(p, 2, dst);
1036   }
1037}
1038
1039void x87_fstp( struct x86_function *p, struct x86_reg dst )
1040{
1041   if (dst.file == file_x87)
1042      emit_2ub(p, 0xdd, 0xd8 + dst.idx);
1043   else {
1044      emit_1ub(p, 0xd9);
1045      emit_modrm_noreg(p, 3, dst);
1046   }
1047}
1048
1049void x87_fcom( struct x86_function *p, struct x86_reg dst )
1050{
1051   if (dst.file == file_x87)
1052      emit_2ub(p, 0xd8, 0xd0 + dst.idx);
1053   else {
1054      emit_1ub(p, 0xd8);
1055      emit_modrm_noreg(p, 2, dst);
1056   }
1057}
1058
1059void x87_fcomp( struct x86_function *p, struct x86_reg dst )
1060{
1061   if (dst.file == file_x87)
1062      emit_2ub(p, 0xd8, 0xd8 + dst.idx);
1063   else {
1064      emit_1ub(p, 0xd8);
1065      emit_modrm_noreg(p, 3, dst);
1066   }
1067}
1068
1069
1070void x87_fnstsw( struct x86_function *p, struct x86_reg dst )
1071{
1072   assert(dst.file == file_REG32);
1073
1074   if (dst.idx == reg_AX &&
1075       dst.mod == mod_REG)
1076      emit_2ub(p, 0xdf, 0xe0);
1077   else {
1078      emit_1ub(p, 0xdd);
1079      emit_modrm_noreg(p, 7, dst);
1080   }
1081}
1082
1083
1084
1085
1086/***********************************************************************
1087 * MMX instructions
1088 */
1089
1090void mmx_emms( struct x86_function *p )
1091{
1092   assert(p->need_emms);
1093   emit_2ub(p, 0x0f, 0x77);
1094   p->need_emms = 0;
1095}
1096
1097void mmx_packssdw( struct x86_function *p,
1098		   struct x86_reg dst,
1099		   struct x86_reg src )
1100{
1101   assert(dst.file == file_MMX &&
1102	  (src.file == file_MMX || src.mod != mod_REG));
1103
1104   p->need_emms = 1;
1105
1106   emit_2ub(p, X86_TWOB, 0x6b);
1107   emit_modrm( p, dst, src );
1108}
1109
1110void mmx_packuswb( struct x86_function *p,
1111		   struct x86_reg dst,
1112		   struct x86_reg src )
1113{
1114   assert(dst.file == file_MMX &&
1115	  (src.file == file_MMX || src.mod != mod_REG));
1116
1117   p->need_emms = 1;
1118
1119   emit_2ub(p, X86_TWOB, 0x67);
1120   emit_modrm( p, dst, src );
1121}
1122
1123void mmx_movd( struct x86_function *p,
1124	       struct x86_reg dst,
1125	       struct x86_reg src )
1126{
1127   p->need_emms = 1;
1128   emit_1ub(p, X86_TWOB);
1129   emit_op_modrm( p, 0x6e, 0x7e, dst, src );
1130}
1131
1132void mmx_movq( struct x86_function *p,
1133	       struct x86_reg dst,
1134	       struct x86_reg src )
1135{
1136   p->need_emms = 1;
1137   emit_1ub(p, X86_TWOB);
1138   emit_op_modrm( p, 0x6f, 0x7f, dst, src );
1139}
1140
1141
1142/***********************************************************************
1143 * Helper functions
1144 */
1145
1146
1147/* Retreive a reference to one of the function arguments, taking into
1148 * account any push/pop activity:
1149 */
1150struct x86_reg x86_fn_arg( struct x86_function *p,
1151			   unsigned arg )
1152{
1153   return x86_make_disp(x86_make_reg(file_REG32, reg_SP),
1154			p->stack_offset + arg * 4);	/* ??? */
1155}
1156
1157
1158void x86_init_func( struct x86_function *p )
1159{
1160   p->size = 0;
1161   p->store = NULL;
1162   p->csr = p->store;
1163}
1164
1165int x86_init_func_size( struct x86_function *p, unsigned code_size )
1166{
1167   p->size = code_size;
1168   p->store = _mesa_exec_malloc(code_size);
1169   p->csr = p->store;
1170   return p->store != NULL;
1171}
1172
1173void x86_release_func( struct x86_function *p )
1174{
1175   _mesa_exec_free(p->store);
1176   p->store = NULL;
1177   p->csr = NULL;
1178   p->size = 0;
1179}
1180
1181
1182void (*x86_get_func( struct x86_function *p ))(void)
1183{
1184   if (DISASSEM && p->store)
1185      _mesa_printf("disassemble %p %p\n", p->store, p->csr);
1186   return (void (*)(void)) (unsigned long) p->store;
1187}
1188
1189#else
1190
1191void x86sse_dummy( void )
1192{
1193}
1194
1195#endif
1196
1197#else  /* USE_X86_ASM */
1198
1199int x86sse_c_dummy_var; /* silence warning */
1200
1201#endif /* USE_X86_ASM */
1202