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