invd and wbinvd support - fixed code gen logic for invlpg - simpler exception handlin...
[qemu] / target-i386 / op.c
1 /*
2  *  i386 micro operations
3  * 
4  *  Copyright (c) 2003 Fabrice Bellard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 #include "exec.h"
21
22 /* n must be a constant to be efficient */
23 static inline int lshift(int x, int n)
24 {
25     if (n >= 0)
26         return x << n;
27     else
28         return x >> (-n);
29 }
30
31 /* we define the various pieces of code used by the JIT */
32
33 #define REG EAX
34 #define REGNAME _EAX
35 #include "opreg_template.h"
36 #undef REG
37 #undef REGNAME
38
39 #define REG ECX
40 #define REGNAME _ECX
41 #include "opreg_template.h"
42 #undef REG
43 #undef REGNAME
44
45 #define REG EDX
46 #define REGNAME _EDX
47 #include "opreg_template.h"
48 #undef REG
49 #undef REGNAME
50
51 #define REG EBX
52 #define REGNAME _EBX
53 #include "opreg_template.h"
54 #undef REG
55 #undef REGNAME
56
57 #define REG ESP
58 #define REGNAME _ESP
59 #include "opreg_template.h"
60 #undef REG
61 #undef REGNAME
62
63 #define REG EBP
64 #define REGNAME _EBP
65 #include "opreg_template.h"
66 #undef REG
67 #undef REGNAME
68
69 #define REG ESI
70 #define REGNAME _ESI
71 #include "opreg_template.h"
72 #undef REG
73 #undef REGNAME
74
75 #define REG EDI
76 #define REGNAME _EDI
77 #include "opreg_template.h"
78 #undef REG
79 #undef REGNAME
80
81 /* operations with flags */
82
83 /* update flags with T0 and T1 (add/sub case) */
84 void OPPROTO op_update2_cc(void)
85 {
86     CC_SRC = T1;
87     CC_DST = T0;
88 }
89
90 /* update flags with T0 (logic operation case) */
91 void OPPROTO op_update1_cc(void)
92 {
93     CC_DST = T0;
94 }
95
96 void OPPROTO op_update_neg_cc(void)
97 {
98     CC_SRC = -T0;
99     CC_DST = T0;
100 }
101
102 void OPPROTO op_cmpl_T0_T1_cc(void)
103 {
104     CC_SRC = T1;
105     CC_DST = T0 - T1;
106 }
107
108 void OPPROTO op_update_inc_cc(void)
109 {
110     CC_SRC = cc_table[CC_OP].compute_c();
111     CC_DST = T0;
112 }
113
114 void OPPROTO op_testl_T0_T1_cc(void)
115 {
116     CC_DST = T0 & T1;
117 }
118
119 /* operations without flags */
120
121 void OPPROTO op_addl_T0_T1(void)
122 {
123     T0 += T1;
124 }
125
126 void OPPROTO op_orl_T0_T1(void)
127 {
128     T0 |= T1;
129 }
130
131 void OPPROTO op_andl_T0_T1(void)
132 {
133     T0 &= T1;
134 }
135
136 void OPPROTO op_subl_T0_T1(void)
137 {
138     T0 -= T1;
139 }
140
141 void OPPROTO op_xorl_T0_T1(void)
142 {
143     T0 ^= T1;
144 }
145
146 void OPPROTO op_negl_T0(void)
147 {
148     T0 = -T0;
149 }
150
151 void OPPROTO op_incl_T0(void)
152 {
153     T0++;
154 }
155
156 void OPPROTO op_decl_T0(void)
157 {
158     T0--;
159 }
160
161 void OPPROTO op_notl_T0(void)
162 {
163     T0 = ~T0;
164 }
165
166 void OPPROTO op_bswapl_T0(void)
167 {
168     T0 = bswap32(T0);
169 }
170
171 /* multiply/divide */
172
173 /* XXX: add eflags optimizations */
174 /* XXX: add non P4 style flags */
175
176 void OPPROTO op_mulb_AL_T0(void)
177 {
178     unsigned int res;
179     res = (uint8_t)EAX * (uint8_t)T0;
180     EAX = (EAX & 0xffff0000) | res;
181     CC_DST = res;
182     CC_SRC = (res & 0xff00);
183 }
184
185 void OPPROTO op_imulb_AL_T0(void)
186 {
187     int res;
188     res = (int8_t)EAX * (int8_t)T0;
189     EAX = (EAX & 0xffff0000) | (res & 0xffff);
190     CC_DST = res;
191     CC_SRC = (res != (int8_t)res);
192 }
193
194 void OPPROTO op_mulw_AX_T0(void)
195 {
196     unsigned int res;
197     res = (uint16_t)EAX * (uint16_t)T0;
198     EAX = (EAX & 0xffff0000) | (res & 0xffff);
199     EDX = (EDX & 0xffff0000) | ((res >> 16) & 0xffff);
200     CC_DST = res;
201     CC_SRC = res >> 16;
202 }
203
204 void OPPROTO op_imulw_AX_T0(void)
205 {
206     int res;
207     res = (int16_t)EAX * (int16_t)T0;
208     EAX = (EAX & 0xffff0000) | (res & 0xffff);
209     EDX = (EDX & 0xffff0000) | ((res >> 16) & 0xffff);
210     CC_DST = res;
211     CC_SRC = (res != (int16_t)res);
212 }
213
214 void OPPROTO op_mull_EAX_T0(void)
215 {
216     uint64_t res;
217     res = (uint64_t)((uint32_t)EAX) * (uint64_t)((uint32_t)T0);
218     EAX = res;
219     EDX = res >> 32;
220     CC_DST = res;
221     CC_SRC = res >> 32;
222 }
223
224 void OPPROTO op_imull_EAX_T0(void)
225 {
226     int64_t res;
227     res = (int64_t)((int32_t)EAX) * (int64_t)((int32_t)T0);
228     EAX = res;
229     EDX = res >> 32;
230     CC_DST = res;
231     CC_SRC = (res != (int32_t)res);
232 }
233
234 void OPPROTO op_imulw_T0_T1(void)
235 {
236     int res;
237     res = (int16_t)T0 * (int16_t)T1;
238     T0 = res;
239     CC_DST = res;
240     CC_SRC = (res != (int16_t)res);
241 }
242
243 void OPPROTO op_imull_T0_T1(void)
244 {
245     int64_t res;
246     res = (int64_t)((int32_t)T0) * (int64_t)((int32_t)T1);
247     T0 = res;
248     CC_DST = res;
249     CC_SRC = (res != (int32_t)res);
250 }
251
252 /* division, flags are undefined */
253 /* XXX: add exceptions for overflow */
254
255 void OPPROTO op_divb_AL_T0(void)
256 {
257     unsigned int num, den, q, r;
258
259     num = (EAX & 0xffff);
260     den = (T0 & 0xff);
261     if (den == 0) {
262         EIP = PARAM1;
263         raise_exception(EXCP00_DIVZ);
264     }
265     q = (num / den) & 0xff;
266     r = (num % den) & 0xff;
267     EAX = (EAX & 0xffff0000) | (r << 8) | q;
268 }
269
270 void OPPROTO op_idivb_AL_T0(void)
271 {
272     int num, den, q, r;
273
274     num = (int16_t)EAX;
275     den = (int8_t)T0;
276     if (den == 0) {
277         EIP = PARAM1;
278         raise_exception(EXCP00_DIVZ);
279     }
280     q = (num / den) & 0xff;
281     r = (num % den) & 0xff;
282     EAX = (EAX & 0xffff0000) | (r << 8) | q;
283 }
284
285 void OPPROTO op_divw_AX_T0(void)
286 {
287     unsigned int num, den, q, r;
288
289     num = (EAX & 0xffff) | ((EDX & 0xffff) << 16);
290     den = (T0 & 0xffff);
291     if (den == 0) {
292         EIP = PARAM1;
293         raise_exception(EXCP00_DIVZ);
294     }
295     q = (num / den) & 0xffff;
296     r = (num % den) & 0xffff;
297     EAX = (EAX & 0xffff0000) | q;
298     EDX = (EDX & 0xffff0000) | r;
299 }
300
301 void OPPROTO op_idivw_AX_T0(void)
302 {
303     int num, den, q, r;
304
305     num = (EAX & 0xffff) | ((EDX & 0xffff) << 16);
306     den = (int16_t)T0;
307     if (den == 0) {
308         EIP = PARAM1;
309         raise_exception(EXCP00_DIVZ);
310     }
311     q = (num / den) & 0xffff;
312     r = (num % den) & 0xffff;
313     EAX = (EAX & 0xffff0000) | q;
314     EDX = (EDX & 0xffff0000) | r;
315 }
316
317 void OPPROTO op_divl_EAX_T0(void)
318 {
319     helper_divl_EAX_T0(PARAM1);
320 }
321
322 void OPPROTO op_idivl_EAX_T0(void)
323 {
324     helper_idivl_EAX_T0(PARAM1);
325 }
326
327 /* constant load & misc op */
328
329 void OPPROTO op_movl_T0_im(void)
330 {
331     T0 = PARAM1;
332 }
333
334 void OPPROTO op_addl_T0_im(void)
335 {
336     T0 += PARAM1;
337 }
338
339 void OPPROTO op_andl_T0_ffff(void)
340 {
341     T0 = T0 & 0xffff;
342 }
343
344 void OPPROTO op_andl_T0_im(void)
345 {
346     T0 = T0 & PARAM1;
347 }
348
349 void OPPROTO op_movl_T0_T1(void)
350 {
351     T0 = T1;
352 }
353
354 void OPPROTO op_movl_T1_im(void)
355 {
356     T1 = PARAM1;
357 }
358
359 void OPPROTO op_addl_T1_im(void)
360 {
361     T1 += PARAM1;
362 }
363
364 void OPPROTO op_movl_T1_A0(void)
365 {
366     T1 = A0;
367 }
368
369 void OPPROTO op_movl_A0_im(void)
370 {
371     A0 = PARAM1;
372 }
373
374 void OPPROTO op_addl_A0_im(void)
375 {
376     A0 += PARAM1;
377 }
378
379 void OPPROTO op_addl_A0_AL(void)
380 {
381     A0 += (EAX & 0xff);
382 }
383
384 void OPPROTO op_andl_A0_ffff(void)
385 {
386     A0 = A0 & 0xffff;
387 }
388
389 /* memory access */
390
391 #define MEMSUFFIX _raw
392 #include "ops_mem.h"
393
394 #if !defined(CONFIG_USER_ONLY)
395 #define MEMSUFFIX _user
396 #include "ops_mem.h"
397
398 #define MEMSUFFIX _kernel
399 #include "ops_mem.h"
400 #endif
401
402 /* used for bit operations */
403
404 void OPPROTO op_add_bitw_A0_T1(void)
405 {
406     A0 += ((int32_t)T1 >> 4) << 1;
407 }
408
409 void OPPROTO op_add_bitl_A0_T1(void)
410 {
411     A0 += ((int32_t)T1 >> 5) << 2;
412 }
413
414 /* indirect jump */
415
416 void OPPROTO op_jmp_T0(void)
417 {
418     EIP = T0;
419 }
420
421 void OPPROTO op_jmp_im(void)
422 {
423     EIP = PARAM1;
424 }
425
426 void OPPROTO op_hlt(void)
427 {
428     env->exception_index = EXCP_HLT;
429     cpu_loop_exit();
430 }
431
432 void OPPROTO op_debug(void)
433 {
434     env->exception_index = EXCP_DEBUG;
435     cpu_loop_exit();
436 }
437
438 void OPPROTO op_raise_interrupt(void)
439 {
440     int intno;
441     unsigned int next_eip;
442     intno = PARAM1;
443     next_eip = PARAM2;
444     raise_interrupt(intno, 1, 0, next_eip);
445 }
446
447 void OPPROTO op_raise_exception(void)
448 {
449     int exception_index;
450     exception_index = PARAM1;
451     raise_exception(exception_index);
452 }
453
454 void OPPROTO op_into(void)
455 {
456     int eflags;
457     eflags = cc_table[CC_OP].compute_all();
458     if (eflags & CC_O) {
459         raise_interrupt(EXCP04_INTO, 1, 0, PARAM1);
460     }
461     FORCE_RET();
462 }
463
464 void OPPROTO op_cli(void)
465 {
466     env->eflags &= ~IF_MASK;
467 }
468
469 void OPPROTO op_sti(void)
470 {
471     env->eflags |= IF_MASK;
472 }
473
474 void OPPROTO op_set_inhibit_irq(void)
475 {
476     env->hflags |= HF_INHIBIT_IRQ_MASK;
477 }
478
479 void OPPROTO op_reset_inhibit_irq(void)
480 {
481     env->hflags &= ~HF_INHIBIT_IRQ_MASK;
482 }
483
484 #if 0
485 /* vm86plus instructions */
486 void OPPROTO op_cli_vm(void)
487 {
488     env->eflags &= ~VIF_MASK;
489 }
490
491 void OPPROTO op_sti_vm(void)
492 {
493     env->eflags |= VIF_MASK;
494     if (env->eflags & VIP_MASK) {
495         EIP = PARAM1;
496         raise_exception(EXCP0D_GPF);
497     }
498     FORCE_RET();
499 }
500 #endif
501
502 void OPPROTO op_boundw(void)
503 {
504     int low, high, v;
505     low = ldsw((uint8_t *)A0);
506     high = ldsw((uint8_t *)A0 + 2);
507     v = (int16_t)T0;
508     if (v < low || v > high) {
509         EIP = PARAM1;
510         raise_exception(EXCP05_BOUND);
511     }
512     FORCE_RET();
513 }
514
515 void OPPROTO op_boundl(void)
516 {
517     int low, high, v;
518     low = ldl((uint8_t *)A0);
519     high = ldl((uint8_t *)A0 + 4);
520     v = T0;
521     if (v < low || v > high) {
522         EIP = PARAM1;
523         raise_exception(EXCP05_BOUND);
524     }
525     FORCE_RET();
526 }
527
528 void OPPROTO op_cmpxchg8b(void)
529 {
530     helper_cmpxchg8b();
531 }
532
533 void OPPROTO op_jmp(void)
534 {
535     JUMP_TB(op_jmp, PARAM1, 0, PARAM2);
536 }
537
538 void OPPROTO op_movl_T0_0(void)
539 {
540     T0 = 0;
541 }
542
543 void OPPROTO op_exit_tb(void)
544 {
545     EXIT_TB();
546 }
547
548 /* multiple size ops */
549
550 #define ldul ldl
551
552 #define SHIFT 0
553 #include "ops_template.h"
554 #undef SHIFT
555
556 #define SHIFT 1
557 #include "ops_template.h"
558 #undef SHIFT
559
560 #define SHIFT 2
561 #include "ops_template.h"
562 #undef SHIFT
563
564 /* sign extend */
565
566 void OPPROTO op_movsbl_T0_T0(void)
567 {
568     T0 = (int8_t)T0;
569 }
570
571 void OPPROTO op_movzbl_T0_T0(void)
572 {
573     T0 = (uint8_t)T0;
574 }
575
576 void OPPROTO op_movswl_T0_T0(void)
577 {
578     T0 = (int16_t)T0;
579 }
580
581 void OPPROTO op_movzwl_T0_T0(void)
582 {
583     T0 = (uint16_t)T0;
584 }
585
586 void OPPROTO op_movswl_EAX_AX(void)
587 {
588     EAX = (int16_t)EAX;
589 }
590
591 void OPPROTO op_movsbw_AX_AL(void)
592 {
593     EAX = (EAX & 0xffff0000) | ((int8_t)EAX & 0xffff);
594 }
595
596 void OPPROTO op_movslq_EDX_EAX(void)
597 {
598     EDX = (int32_t)EAX >> 31;
599 }
600
601 void OPPROTO op_movswl_DX_AX(void)
602 {
603     EDX = (EDX & 0xffff0000) | (((int16_t)EAX >> 15) & 0xffff);
604 }
605
606 /* string ops helpers */
607
608 void OPPROTO op_addl_ESI_T0(void)
609 {
610     ESI += T0;
611 }
612
613 void OPPROTO op_addw_ESI_T0(void)
614 {
615     ESI = (ESI & ~0xffff) | ((ESI + T0) & 0xffff);
616 }
617
618 void OPPROTO op_addl_EDI_T0(void)
619 {
620     EDI += T0;
621 }
622
623 void OPPROTO op_addw_EDI_T0(void)
624 {
625     EDI = (EDI & ~0xffff) | ((EDI + T0) & 0xffff);
626 }
627
628 void OPPROTO op_decl_ECX(void)
629 {
630     ECX--;
631 }
632
633 void OPPROTO op_decw_ECX(void)
634 {
635     ECX = (ECX & ~0xffff) | ((ECX - 1) & 0xffff);
636 }
637
638 /* push/pop */
639
640 void op_pushl_T0(void)
641 {
642     uint32_t offset;
643     offset = ESP - 4;
644     stl((void *)offset, T0);
645     /* modify ESP after to handle exceptions correctly */
646     ESP = offset;
647 }
648
649 void op_pushw_T0(void)
650 {
651     uint32_t offset;
652     offset = ESP - 2;
653     stw((void *)offset, T0);
654     /* modify ESP after to handle exceptions correctly */
655     ESP = offset;
656 }
657
658 void op_pushl_ss32_T0(void)
659 {
660     uint32_t offset;
661     offset = ESP - 4;
662     stl(env->segs[R_SS].base + offset, T0);
663     /* modify ESP after to handle exceptions correctly */
664     ESP = offset;
665 }
666
667 void op_pushw_ss32_T0(void)
668 {
669     uint32_t offset;
670     offset = ESP - 2;
671     stw(env->segs[R_SS].base + offset, T0);
672     /* modify ESP after to handle exceptions correctly */
673     ESP = offset;
674 }
675
676 void op_pushl_ss16_T0(void)
677 {
678     uint32_t offset;
679     offset = (ESP - 4) & 0xffff;
680     stl(env->segs[R_SS].base + offset, T0);
681     /* modify ESP after to handle exceptions correctly */
682     ESP = (ESP & ~0xffff) | offset;
683 }
684
685 void op_pushw_ss16_T0(void)
686 {
687     uint32_t offset;
688     offset = (ESP - 2) & 0xffff;
689     stw(env->segs[R_SS].base + offset, T0);
690     /* modify ESP after to handle exceptions correctly */
691     ESP = (ESP & ~0xffff) | offset;
692 }
693
694 /* NOTE: ESP update is done after */
695 void op_popl_T0(void)
696 {
697     T0 = ldl((void *)ESP);
698 }
699
700 void op_popw_T0(void)
701 {
702     T0 = lduw((void *)ESP);
703 }
704
705 void op_popl_ss32_T0(void)
706 {
707     T0 = ldl(env->segs[R_SS].base + ESP);
708 }
709
710 void op_popw_ss32_T0(void)
711 {
712     T0 = lduw(env->segs[R_SS].base + ESP);
713 }
714
715 void op_popl_ss16_T0(void)
716 {
717     T0 = ldl(env->segs[R_SS].base + (ESP & 0xffff));
718 }
719
720 void op_popw_ss16_T0(void)
721 {
722     T0 = lduw(env->segs[R_SS].base + (ESP & 0xffff));
723 }
724
725 void op_addl_ESP_4(void)
726 {
727     ESP += 4;
728 }
729
730 void op_addl_ESP_2(void)
731 {
732     ESP += 2;
733 }
734
735 void op_addw_ESP_4(void)
736 {
737     ESP = (ESP & ~0xffff) | ((ESP + 4) & 0xffff);
738 }
739
740 void op_addw_ESP_2(void)
741 {
742     ESP = (ESP & ~0xffff) | ((ESP + 2) & 0xffff);
743 }
744
745 void op_addl_ESP_im(void)
746 {
747     ESP += PARAM1;
748 }
749
750 void op_addw_ESP_im(void)
751 {
752     ESP = (ESP & ~0xffff) | ((ESP + PARAM1) & 0xffff);
753 }
754
755 void OPPROTO op_rdtsc(void)
756 {
757     helper_rdtsc();
758 }
759
760 void OPPROTO op_cpuid(void)
761 {
762     helper_cpuid();
763 }
764
765 void OPPROTO op_rdmsr(void)
766 {
767     helper_rdmsr();
768 }
769
770 void OPPROTO op_wrmsr(void)
771 {
772     helper_wrmsr();
773 }
774
775 /* bcd */
776
777 /* XXX: exception */
778 void OPPROTO op_aam(void)
779 {
780     int base = PARAM1;
781     int al, ah;
782     al = EAX & 0xff;
783     ah = al / base;
784     al = al % base;
785     EAX = (EAX & ~0xffff) | al | (ah << 8);
786     CC_DST = al;
787 }
788
789 void OPPROTO op_aad(void)
790 {
791     int base = PARAM1;
792     int al, ah;
793     al = EAX & 0xff;
794     ah = (EAX >> 8) & 0xff;
795     al = ((ah * base) + al) & 0xff;
796     EAX = (EAX & ~0xffff) | al;
797     CC_DST = al;
798 }
799
800 void OPPROTO op_aaa(void)
801 {
802     int icarry;
803     int al, ah, af;
804     int eflags;
805
806     eflags = cc_table[CC_OP].compute_all();
807     af = eflags & CC_A;
808     al = EAX & 0xff;
809     ah = (EAX >> 8) & 0xff;
810
811     icarry = (al > 0xf9);
812     if (((al & 0x0f) > 9 ) || af) {
813         al = (al + 6) & 0x0f;
814         ah = (ah + 1 + icarry) & 0xff;
815         eflags |= CC_C | CC_A;
816     } else {
817         eflags &= ~(CC_C | CC_A);
818         al &= 0x0f;
819     }
820     EAX = (EAX & ~0xffff) | al | (ah << 8);
821     CC_SRC = eflags;
822 }
823
824 void OPPROTO op_aas(void)
825 {
826     int icarry;
827     int al, ah, af;
828     int eflags;
829
830     eflags = cc_table[CC_OP].compute_all();
831     af = eflags & CC_A;
832     al = EAX & 0xff;
833     ah = (EAX >> 8) & 0xff;
834
835     icarry = (al < 6);
836     if (((al & 0x0f) > 9 ) || af) {
837         al = (al - 6) & 0x0f;
838         ah = (ah - 1 - icarry) & 0xff;
839         eflags |= CC_C | CC_A;
840     } else {
841         eflags &= ~(CC_C | CC_A);
842         al &= 0x0f;
843     }
844     EAX = (EAX & ~0xffff) | al | (ah << 8);
845     CC_SRC = eflags;
846 }
847
848 void OPPROTO op_daa(void)
849 {
850     int al, af, cf;
851     int eflags;
852
853     eflags = cc_table[CC_OP].compute_all();
854     cf = eflags & CC_C;
855     af = eflags & CC_A;
856     al = EAX & 0xff;
857
858     eflags = 0;
859     if (((al & 0x0f) > 9 ) || af) {
860         al = (al + 6) & 0xff;
861         eflags |= CC_A;
862     }
863     if ((al > 0x9f) || cf) {
864         al = (al + 0x60) & 0xff;
865         eflags |= CC_C;
866     }
867     EAX = (EAX & ~0xff) | al;
868     /* well, speed is not an issue here, so we compute the flags by hand */
869     eflags |= (al == 0) << 6; /* zf */
870     eflags |= parity_table[al]; /* pf */
871     eflags |= (al & 0x80); /* sf */
872     CC_SRC = eflags;
873 }
874
875 void OPPROTO op_das(void)
876 {
877     int al, al1, af, cf;
878     int eflags;
879
880     eflags = cc_table[CC_OP].compute_all();
881     cf = eflags & CC_C;
882     af = eflags & CC_A;
883     al = EAX & 0xff;
884
885     eflags = 0;
886     al1 = al;
887     if (((al & 0x0f) > 9 ) || af) {
888         eflags |= CC_A;
889         if (al < 6 || cf)
890             eflags |= CC_C;
891         al = (al - 6) & 0xff;
892     }
893     if ((al1 > 0x99) || cf) {
894         al = (al - 0x60) & 0xff;
895         eflags |= CC_C;
896     }
897     EAX = (EAX & ~0xff) | al;
898     /* well, speed is not an issue here, so we compute the flags by hand */
899     eflags |= (al == 0) << 6; /* zf */
900     eflags |= parity_table[al]; /* pf */
901     eflags |= (al & 0x80); /* sf */
902     CC_SRC = eflags;
903 }
904
905 /* segment handling */
906
907 /* never use it with R_CS */
908 void OPPROTO op_movl_seg_T0(void)
909 {
910     load_seg(PARAM1, T0);
911 }
912
913 /* faster VM86 version */
914 void OPPROTO op_movl_seg_T0_vm(void)
915 {
916     int selector;
917     SegmentCache *sc;
918     
919     selector = T0 & 0xffff;
920     /* env->segs[] access */
921     sc = (SegmentCache *)((char *)env + PARAM1);
922     sc->selector = selector;
923     sc->base = (void *)(selector << 4);
924 }
925
926 void OPPROTO op_movl_T0_seg(void)
927 {
928     T0 = env->segs[PARAM1].selector;
929 }
930
931 void OPPROTO op_movl_A0_seg(void)
932 {
933     A0 = *(unsigned long *)((char *)env + PARAM1);
934 }
935
936 void OPPROTO op_addl_A0_seg(void)
937 {
938     A0 += *(unsigned long *)((char *)env + PARAM1);
939 }
940
941 void OPPROTO op_lsl(void)
942 {
943     helper_lsl();
944 }
945
946 void OPPROTO op_lar(void)
947 {
948     helper_lar();
949 }
950
951 void OPPROTO op_verr(void)
952 {
953     helper_verr();
954 }
955
956 void OPPROTO op_verw(void)
957 {
958     helper_verw();
959 }
960
961 void OPPROTO op_arpl(void)
962 {
963     if ((T0 & 3) < (T1 & 3)) {
964         /* XXX: emulate bug or 0xff3f0000 oring as in bochs ? */
965         T0 = (T0 & ~3) | (T1 & 3);
966         T1 = CC_Z;
967    } else {
968         T1 = 0;
969     }
970     FORCE_RET();
971 }
972             
973 void OPPROTO op_arpl_update(void)
974 {
975     int eflags;
976     eflags = cc_table[CC_OP].compute_all();
977     CC_SRC = (eflags & ~CC_Z) | T1;
978 }
979     
980 /* T0: segment, T1:eip */
981 void OPPROTO op_ljmp_protected_T0_T1(void)
982 {
983     helper_ljmp_protected_T0_T1();
984 }
985
986 void OPPROTO op_lcall_real_T0_T1(void)
987 {
988     helper_lcall_real_T0_T1(PARAM1, PARAM2);
989 }
990
991 void OPPROTO op_lcall_protected_T0_T1(void)
992 {
993     helper_lcall_protected_T0_T1(PARAM1, PARAM2);
994 }
995
996 void OPPROTO op_iret_real(void)
997 {
998     helper_iret_real(PARAM1);
999 }
1000
1001 void OPPROTO op_iret_protected(void)
1002 {
1003     helper_iret_protected(PARAM1);
1004 }
1005
1006 void OPPROTO op_lret_protected(void)
1007 {
1008     helper_lret_protected(PARAM1, PARAM2);
1009 }
1010
1011 void OPPROTO op_lldt_T0(void)
1012 {
1013     helper_lldt_T0();
1014 }
1015
1016 void OPPROTO op_ltr_T0(void)
1017 {
1018     helper_ltr_T0();
1019 }
1020
1021 /* CR registers access */
1022 void OPPROTO op_movl_crN_T0(void)
1023 {
1024     helper_movl_crN_T0(PARAM1);
1025 }
1026
1027 /* DR registers access */
1028 void OPPROTO op_movl_drN_T0(void)
1029 {
1030     helper_movl_drN_T0(PARAM1);
1031 }
1032
1033 void OPPROTO op_lmsw_T0(void)
1034 {
1035     /* only 4 lower bits of CR0 are modified */
1036     T0 = (env->cr[0] & ~0xf) | (T0 & 0xf);
1037     helper_movl_crN_T0(0);
1038 }
1039
1040 void OPPROTO op_invlpg_A0(void)
1041 {
1042     helper_invlpg(A0);
1043 }
1044
1045 void OPPROTO op_movl_T0_env(void)
1046 {
1047     T0 = *(uint32_t *)((char *)env + PARAM1);
1048 }
1049
1050 void OPPROTO op_movl_env_T0(void)
1051 {
1052     *(uint32_t *)((char *)env + PARAM1) = T0;
1053 }
1054
1055 void OPPROTO op_movl_env_T1(void)
1056 {
1057     *(uint32_t *)((char *)env + PARAM1) = T1;
1058 }
1059
1060 void OPPROTO op_clts(void)
1061 {
1062     env->cr[0] &= ~CR0_TS_MASK;
1063 }
1064
1065 /* flags handling */
1066
1067 /* slow jumps cases : in order to avoid calling a function with a
1068    pointer (which can generate a stack frame on PowerPC), we use
1069    op_setcc to set T0 and then call op_jcc. */
1070 void OPPROTO op_jcc(void)
1071 {
1072     if (T0)
1073         JUMP_TB(op_jcc, PARAM1, 0, PARAM2);
1074     else
1075         JUMP_TB(op_jcc, PARAM1, 1, PARAM3);
1076     FORCE_RET();
1077 }
1078
1079 void OPPROTO op_jcc_im(void)
1080 {
1081     if (T0)
1082         EIP = PARAM1;
1083     else
1084         EIP = PARAM2;
1085     FORCE_RET();
1086 }
1087
1088 /* slow set cases (compute x86 flags) */
1089 void OPPROTO op_seto_T0_cc(void)
1090 {
1091     int eflags;
1092     eflags = cc_table[CC_OP].compute_all();
1093     T0 = (eflags >> 11) & 1;
1094 }
1095
1096 void OPPROTO op_setb_T0_cc(void)
1097 {
1098     T0 = cc_table[CC_OP].compute_c();
1099 }
1100
1101 void OPPROTO op_setz_T0_cc(void)
1102 {
1103     int eflags;
1104     eflags = cc_table[CC_OP].compute_all();
1105     T0 = (eflags >> 6) & 1;
1106 }
1107
1108 void OPPROTO op_setbe_T0_cc(void)
1109 {
1110     int eflags;
1111     eflags = cc_table[CC_OP].compute_all();
1112     T0 = (eflags & (CC_Z | CC_C)) != 0;
1113 }
1114
1115 void OPPROTO op_sets_T0_cc(void)
1116 {
1117     int eflags;
1118     eflags = cc_table[CC_OP].compute_all();
1119     T0 = (eflags >> 7) & 1;
1120 }
1121
1122 void OPPROTO op_setp_T0_cc(void)
1123 {
1124     int eflags;
1125     eflags = cc_table[CC_OP].compute_all();
1126     T0 = (eflags >> 2) & 1;
1127 }
1128
1129 void OPPROTO op_setl_T0_cc(void)
1130 {
1131     int eflags;
1132     eflags = cc_table[CC_OP].compute_all();
1133     T0 = ((eflags ^ (eflags >> 4)) >> 7) & 1;
1134 }
1135
1136 void OPPROTO op_setle_T0_cc(void)
1137 {
1138     int eflags;
1139     eflags = cc_table[CC_OP].compute_all();
1140     T0 = (((eflags ^ (eflags >> 4)) & 0x80) || (eflags & CC_Z)) != 0;
1141 }
1142
1143 void OPPROTO op_xor_T0_1(void)
1144 {
1145     T0 ^= 1;
1146 }
1147
1148 void OPPROTO op_set_cc_op(void)
1149 {
1150     CC_OP = PARAM1;
1151 }
1152
1153 /* XXX: clear VIF/VIP in all ops ? */
1154
1155 void OPPROTO op_movl_eflags_T0(void)
1156 {
1157     load_eflags(T0, (TF_MASK | AC_MASK | ID_MASK));
1158 }
1159
1160 void OPPROTO op_movw_eflags_T0(void)
1161 {
1162     load_eflags(T0, (TF_MASK | AC_MASK | ID_MASK) & 0xffff);
1163 }
1164
1165 void OPPROTO op_movl_eflags_T0_io(void)
1166 {
1167     load_eflags(T0, (TF_MASK | AC_MASK | ID_MASK | IF_MASK));
1168 }
1169
1170 void OPPROTO op_movw_eflags_T0_io(void)
1171 {
1172     load_eflags(T0, (TF_MASK | AC_MASK | ID_MASK | IF_MASK) & 0xffff);
1173 }
1174
1175 void OPPROTO op_movl_eflags_T0_cpl0(void)
1176 {
1177     load_eflags(T0, (TF_MASK | AC_MASK | ID_MASK | IF_MASK | IOPL_MASK));
1178 }
1179
1180 void OPPROTO op_movw_eflags_T0_cpl0(void)
1181 {
1182     load_eflags(T0, (TF_MASK | AC_MASK | ID_MASK | IF_MASK | IOPL_MASK) & 0xffff);
1183 }
1184
1185 #if 0
1186 /* vm86plus version */
1187 void OPPROTO op_movw_eflags_T0_vm(void)
1188 {
1189     int eflags;
1190     eflags = T0;
1191     CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1192     DF = 1 - (2 * ((eflags >> 10) & 1));
1193     /* we also update some system flags as in user mode */
1194     env->eflags = (env->eflags & ~(FL_UPDATE_MASK16 | VIF_MASK)) |
1195         (eflags & FL_UPDATE_MASK16);
1196     if (eflags & IF_MASK) {
1197         env->eflags |= VIF_MASK;
1198         if (env->eflags & VIP_MASK) {
1199             EIP = PARAM1;
1200             raise_exception(EXCP0D_GPF);
1201         }
1202     }
1203     FORCE_RET();
1204 }
1205
1206 void OPPROTO op_movl_eflags_T0_vm(void)
1207 {
1208     int eflags;
1209     eflags = T0;
1210     CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1211     DF = 1 - (2 * ((eflags >> 10) & 1));
1212     /* we also update some system flags as in user mode */
1213     env->eflags = (env->eflags & ~(FL_UPDATE_MASK32 | VIF_MASK)) |
1214         (eflags & FL_UPDATE_MASK32);
1215     if (eflags & IF_MASK) {
1216         env->eflags |= VIF_MASK;
1217         if (env->eflags & VIP_MASK) {
1218             EIP = PARAM1;
1219             raise_exception(EXCP0D_GPF);
1220         }
1221     }
1222     FORCE_RET();
1223 }
1224 #endif
1225
1226 /* XXX: compute only O flag */
1227 void OPPROTO op_movb_eflags_T0(void)
1228 {
1229     int of;
1230     of = cc_table[CC_OP].compute_all() & CC_O;
1231     CC_SRC = (T0 & (CC_S | CC_Z | CC_A | CC_P | CC_C)) | of;
1232 }
1233
1234 void OPPROTO op_movl_T0_eflags(void)
1235 {
1236     int eflags;
1237     eflags = cc_table[CC_OP].compute_all();
1238     eflags |= (DF & DF_MASK);
1239     eflags |= env->eflags & ~(VM_MASK | RF_MASK);
1240     T0 = eflags;
1241 }
1242
1243 /* vm86plus version */
1244 #if 0
1245 void OPPROTO op_movl_T0_eflags_vm(void)
1246 {
1247     int eflags;
1248     eflags = cc_table[CC_OP].compute_all();
1249     eflags |= (DF & DF_MASK);
1250     eflags |= env->eflags & ~(VM_MASK | RF_MASK | IF_MASK);
1251     if (env->eflags & VIF_MASK)
1252         eflags |= IF_MASK;
1253     T0 = eflags;
1254 }
1255 #endif
1256
1257 void OPPROTO op_cld(void)
1258 {
1259     DF = 1;
1260 }
1261
1262 void OPPROTO op_std(void)
1263 {
1264     DF = -1;
1265 }
1266
1267 void OPPROTO op_clc(void)
1268 {
1269     int eflags;
1270     eflags = cc_table[CC_OP].compute_all();
1271     eflags &= ~CC_C;
1272     CC_SRC = eflags;
1273 }
1274
1275 void OPPROTO op_stc(void)
1276 {
1277     int eflags;
1278     eflags = cc_table[CC_OP].compute_all();
1279     eflags |= CC_C;
1280     CC_SRC = eflags;
1281 }
1282
1283 void OPPROTO op_cmc(void)
1284 {
1285     int eflags;
1286     eflags = cc_table[CC_OP].compute_all();
1287     eflags ^= CC_C;
1288     CC_SRC = eflags;
1289 }
1290
1291 void OPPROTO op_salc(void)
1292 {
1293     int cf;
1294     cf = cc_table[CC_OP].compute_c();
1295     EAX = (EAX & ~0xff) | ((-cf) & 0xff);
1296 }
1297
1298 static int compute_all_eflags(void)
1299 {
1300     return CC_SRC;
1301 }
1302
1303 static int compute_c_eflags(void)
1304 {
1305     return CC_SRC & CC_C;
1306 }
1307
1308 CCTable cc_table[CC_OP_NB] = {
1309     [CC_OP_DYNAMIC] = { /* should never happen */ },
1310
1311     [CC_OP_EFLAGS] = { compute_all_eflags, compute_c_eflags },
1312
1313     [CC_OP_MULB] = { compute_all_mulb, compute_c_mull },
1314     [CC_OP_MULW] = { compute_all_mulw, compute_c_mull },
1315     [CC_OP_MULL] = { compute_all_mull, compute_c_mull },
1316
1317     [CC_OP_ADDB] = { compute_all_addb, compute_c_addb },
1318     [CC_OP_ADDW] = { compute_all_addw, compute_c_addw  },
1319     [CC_OP_ADDL] = { compute_all_addl, compute_c_addl  },
1320
1321     [CC_OP_ADCB] = { compute_all_adcb, compute_c_adcb },
1322     [CC_OP_ADCW] = { compute_all_adcw, compute_c_adcw  },
1323     [CC_OP_ADCL] = { compute_all_adcl, compute_c_adcl  },
1324
1325     [CC_OP_SUBB] = { compute_all_subb, compute_c_subb  },
1326     [CC_OP_SUBW] = { compute_all_subw, compute_c_subw  },
1327     [CC_OP_SUBL] = { compute_all_subl, compute_c_subl  },
1328     
1329     [CC_OP_SBBB] = { compute_all_sbbb, compute_c_sbbb  },
1330     [CC_OP_SBBW] = { compute_all_sbbw, compute_c_sbbw  },
1331     [CC_OP_SBBL] = { compute_all_sbbl, compute_c_sbbl  },
1332     
1333     [CC_OP_LOGICB] = { compute_all_logicb, compute_c_logicb },
1334     [CC_OP_LOGICW] = { compute_all_logicw, compute_c_logicw },
1335     [CC_OP_LOGICL] = { compute_all_logicl, compute_c_logicl },
1336     
1337     [CC_OP_INCB] = { compute_all_incb, compute_c_incl },
1338     [CC_OP_INCW] = { compute_all_incw, compute_c_incl },
1339     [CC_OP_INCL] = { compute_all_incl, compute_c_incl },
1340     
1341     [CC_OP_DECB] = { compute_all_decb, compute_c_incl },
1342     [CC_OP_DECW] = { compute_all_decw, compute_c_incl },
1343     [CC_OP_DECL] = { compute_all_decl, compute_c_incl },
1344     
1345     [CC_OP_SHLB] = { compute_all_shlb, compute_c_shlb },
1346     [CC_OP_SHLW] = { compute_all_shlw, compute_c_shlw },
1347     [CC_OP_SHLL] = { compute_all_shll, compute_c_shll },
1348
1349     [CC_OP_SARB] = { compute_all_sarb, compute_c_sarl },
1350     [CC_OP_SARW] = { compute_all_sarw, compute_c_sarl },
1351     [CC_OP_SARL] = { compute_all_sarl, compute_c_sarl },
1352 };
1353
1354 /* floating point support. Some of the code for complicated x87
1355    functions comes from the LGPL'ed x86 emulator found in the Willows
1356    TWIN windows emulator. */
1357
1358 #if defined(__powerpc__)
1359 extern CPU86_LDouble copysign(CPU86_LDouble, CPU86_LDouble);
1360
1361 /* correct (but slow) PowerPC rint() (glibc version is incorrect) */
1362 double qemu_rint(double x)
1363 {
1364     double y = 4503599627370496.0;
1365     if (fabs(x) >= y)
1366         return x;
1367     if (x < 0) 
1368         y = -y;
1369     y = (x + y) - y;
1370     if (y == 0.0)
1371         y = copysign(y, x);
1372     return y;
1373 }
1374
1375 #define rint qemu_rint
1376 #endif
1377
1378 /* fp load FT0 */
1379
1380 void OPPROTO op_flds_FT0_A0(void)
1381 {
1382 #ifdef USE_FP_CONVERT
1383     FP_CONVERT.i32 = ldl((void *)A0);
1384     FT0 = FP_CONVERT.f;
1385 #else
1386     FT0 = ldfl((void *)A0);
1387 #endif
1388 }
1389
1390 void OPPROTO op_fldl_FT0_A0(void)
1391 {
1392 #ifdef USE_FP_CONVERT
1393     FP_CONVERT.i64 = ldq((void *)A0);
1394     FT0 = FP_CONVERT.d;
1395 #else
1396     FT0 = ldfq((void *)A0);
1397 #endif
1398 }
1399
1400 /* helpers are needed to avoid static constant reference. XXX: find a better way */
1401 #ifdef USE_INT_TO_FLOAT_HELPERS
1402
1403 void helper_fild_FT0_A0(void)
1404 {
1405     FT0 = (CPU86_LDouble)ldsw((void *)A0);
1406 }
1407
1408 void helper_fildl_FT0_A0(void)
1409 {
1410     FT0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1411 }
1412
1413 void helper_fildll_FT0_A0(void)
1414 {
1415     FT0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1416 }
1417
1418 void OPPROTO op_fild_FT0_A0(void)
1419 {
1420     helper_fild_FT0_A0();
1421 }
1422
1423 void OPPROTO op_fildl_FT0_A0(void)
1424 {
1425     helper_fildl_FT0_A0();
1426 }
1427
1428 void OPPROTO op_fildll_FT0_A0(void)
1429 {
1430     helper_fildll_FT0_A0();
1431 }
1432
1433 #else
1434
1435 void OPPROTO op_fild_FT0_A0(void)
1436 {
1437 #ifdef USE_FP_CONVERT
1438     FP_CONVERT.i32 = ldsw((void *)A0);
1439     FT0 = (CPU86_LDouble)FP_CONVERT.i32;
1440 #else
1441     FT0 = (CPU86_LDouble)ldsw((void *)A0);
1442 #endif
1443 }
1444
1445 void OPPROTO op_fildl_FT0_A0(void)
1446 {
1447 #ifdef USE_FP_CONVERT
1448     FP_CONVERT.i32 = (int32_t) ldl((void *)A0);
1449     FT0 = (CPU86_LDouble)FP_CONVERT.i32;
1450 #else
1451     FT0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1452 #endif
1453 }
1454
1455 void OPPROTO op_fildll_FT0_A0(void)
1456 {
1457 #ifdef USE_FP_CONVERT
1458     FP_CONVERT.i64 = (int64_t) ldq((void *)A0);
1459     FT0 = (CPU86_LDouble)FP_CONVERT.i64;
1460 #else
1461     FT0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1462 #endif
1463 }
1464 #endif
1465
1466 /* fp load ST0 */
1467
1468 void OPPROTO op_flds_ST0_A0(void)
1469 {
1470     int new_fpstt;
1471     new_fpstt = (env->fpstt - 1) & 7;
1472 #ifdef USE_FP_CONVERT
1473     FP_CONVERT.i32 = ldl((void *)A0);
1474     env->fpregs[new_fpstt] = FP_CONVERT.f;
1475 #else
1476     env->fpregs[new_fpstt] = ldfl((void *)A0);
1477 #endif
1478     env->fpstt = new_fpstt;
1479     env->fptags[new_fpstt] = 0; /* validate stack entry */
1480 }
1481
1482 void OPPROTO op_fldl_ST0_A0(void)
1483 {
1484     int new_fpstt;
1485     new_fpstt = (env->fpstt - 1) & 7;
1486 #ifdef USE_FP_CONVERT
1487     FP_CONVERT.i64 = ldq((void *)A0);
1488     env->fpregs[new_fpstt] = FP_CONVERT.d;
1489 #else
1490     env->fpregs[new_fpstt] = ldfq((void *)A0);
1491 #endif
1492     env->fpstt = new_fpstt;
1493     env->fptags[new_fpstt] = 0; /* validate stack entry */
1494 }
1495
1496 void OPPROTO op_fldt_ST0_A0(void)
1497 {
1498     helper_fldt_ST0_A0();
1499 }
1500
1501 /* helpers are needed to avoid static constant reference. XXX: find a better way */
1502 #ifdef USE_INT_TO_FLOAT_HELPERS
1503
1504 void helper_fild_ST0_A0(void)
1505 {
1506     int new_fpstt;
1507     new_fpstt = (env->fpstt - 1) & 7;
1508     env->fpregs[new_fpstt] = (CPU86_LDouble)ldsw((void *)A0);
1509     env->fpstt = new_fpstt;
1510     env->fptags[new_fpstt] = 0; /* validate stack entry */
1511 }
1512
1513 void helper_fildl_ST0_A0(void)
1514 {
1515     int new_fpstt;
1516     new_fpstt = (env->fpstt - 1) & 7;
1517     env->fpregs[new_fpstt] = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1518     env->fpstt = new_fpstt;
1519     env->fptags[new_fpstt] = 0; /* validate stack entry */
1520 }
1521
1522 void helper_fildll_ST0_A0(void)
1523 {
1524     int new_fpstt;
1525     new_fpstt = (env->fpstt - 1) & 7;
1526     env->fpregs[new_fpstt] = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1527     env->fpstt = new_fpstt;
1528     env->fptags[new_fpstt] = 0; /* validate stack entry */
1529 }
1530
1531 void OPPROTO op_fild_ST0_A0(void)
1532 {
1533     helper_fild_ST0_A0();
1534 }
1535
1536 void OPPROTO op_fildl_ST0_A0(void)
1537 {
1538     helper_fildl_ST0_A0();
1539 }
1540
1541 void OPPROTO op_fildll_ST0_A0(void)
1542 {
1543     helper_fildll_ST0_A0();
1544 }
1545
1546 #else
1547
1548 void OPPROTO op_fild_ST0_A0(void)
1549 {
1550     int new_fpstt;
1551     new_fpstt = (env->fpstt - 1) & 7;
1552 #ifdef USE_FP_CONVERT
1553     FP_CONVERT.i32 = ldsw((void *)A0);
1554     env->fpregs[new_fpstt] = (CPU86_LDouble)FP_CONVERT.i32;
1555 #else
1556     env->fpregs[new_fpstt] = (CPU86_LDouble)ldsw((void *)A0);
1557 #endif
1558     env->fpstt = new_fpstt;
1559     env->fptags[new_fpstt] = 0; /* validate stack entry */
1560 }
1561
1562 void OPPROTO op_fildl_ST0_A0(void)
1563 {
1564     int new_fpstt;
1565     new_fpstt = (env->fpstt - 1) & 7;
1566 #ifdef USE_FP_CONVERT
1567     FP_CONVERT.i32 = (int32_t) ldl((void *)A0);
1568     env->fpregs[new_fpstt] = (CPU86_LDouble)FP_CONVERT.i32;
1569 #else
1570     env->fpregs[new_fpstt] = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1571 #endif
1572     env->fpstt = new_fpstt;
1573     env->fptags[new_fpstt] = 0; /* validate stack entry */
1574 }
1575
1576 void OPPROTO op_fildll_ST0_A0(void)
1577 {
1578     int new_fpstt;
1579     new_fpstt = (env->fpstt - 1) & 7;
1580 #ifdef USE_FP_CONVERT
1581     FP_CONVERT.i64 = (int64_t) ldq((void *)A0);
1582     env->fpregs[new_fpstt] = (CPU86_LDouble)FP_CONVERT.i64;
1583 #else
1584     env->fpregs[new_fpstt] = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1585 #endif
1586     env->fpstt = new_fpstt;
1587     env->fptags[new_fpstt] = 0; /* validate stack entry */
1588 }
1589
1590 #endif
1591
1592 /* fp store */
1593
1594 void OPPROTO op_fsts_ST0_A0(void)
1595 {
1596 #ifdef USE_FP_CONVERT
1597     FP_CONVERT.f = (float)ST0;
1598     stfl((void *)A0, FP_CONVERT.f);
1599 #else
1600     stfl((void *)A0, (float)ST0);
1601 #endif
1602 }
1603
1604 void OPPROTO op_fstl_ST0_A0(void)
1605 {
1606     stfq((void *)A0, (double)ST0);
1607 }
1608
1609 void OPPROTO op_fstt_ST0_A0(void)
1610 {
1611     helper_fstt_ST0_A0();
1612 }
1613
1614 void OPPROTO op_fist_ST0_A0(void)
1615 {
1616 #if defined(__sparc__) && !defined(__sparc_v9__)
1617     register CPU86_LDouble d asm("o0");
1618 #else
1619     CPU86_LDouble d;
1620 #endif
1621     int val;
1622
1623     d = ST0;
1624     val = lrint(d);
1625     if (val != (int16_t)val)
1626         val = -32768;
1627     stw((void *)A0, val);
1628 }
1629
1630 void OPPROTO op_fistl_ST0_A0(void)
1631 {
1632 #if defined(__sparc__) && !defined(__sparc_v9__)
1633     register CPU86_LDouble d asm("o0");
1634 #else
1635     CPU86_LDouble d;
1636 #endif
1637     int val;
1638
1639     d = ST0;
1640     val = lrint(d);
1641     stl((void *)A0, val);
1642 }
1643
1644 void OPPROTO op_fistll_ST0_A0(void)
1645 {
1646 #if defined(__sparc__) && !defined(__sparc_v9__)
1647     register CPU86_LDouble d asm("o0");
1648 #else
1649     CPU86_LDouble d;
1650 #endif
1651     int64_t val;
1652
1653     d = ST0;
1654     val = llrint(d);
1655     stq((void *)A0, val);
1656 }
1657
1658 void OPPROTO op_fbld_ST0_A0(void)
1659 {
1660     helper_fbld_ST0_A0();
1661 }
1662
1663 void OPPROTO op_fbst_ST0_A0(void)
1664 {
1665     helper_fbst_ST0_A0();
1666 }
1667
1668 /* FPU move */
1669
1670 void OPPROTO op_fpush(void)
1671 {
1672     fpush();
1673 }
1674
1675 void OPPROTO op_fpop(void)
1676 {
1677     fpop();
1678 }
1679
1680 void OPPROTO op_fdecstp(void)
1681 {
1682     env->fpstt = (env->fpstt - 1) & 7;
1683     env->fpus &= (~0x4700);
1684 }
1685
1686 void OPPROTO op_fincstp(void)
1687 {
1688     env->fpstt = (env->fpstt + 1) & 7;
1689     env->fpus &= (~0x4700);
1690 }
1691
1692 void OPPROTO op_fmov_ST0_FT0(void)
1693 {
1694     ST0 = FT0;
1695 }
1696
1697 void OPPROTO op_fmov_FT0_STN(void)
1698 {
1699     FT0 = ST(PARAM1);
1700 }
1701
1702 void OPPROTO op_fmov_ST0_STN(void)
1703 {
1704     ST0 = ST(PARAM1);
1705 }
1706
1707 void OPPROTO op_fmov_STN_ST0(void)
1708 {
1709     ST(PARAM1) = ST0;
1710 }
1711
1712 void OPPROTO op_fxchg_ST0_STN(void)
1713 {
1714     CPU86_LDouble tmp;
1715     tmp = ST(PARAM1);
1716     ST(PARAM1) = ST0;
1717     ST0 = tmp;
1718 }
1719
1720 /* FPU operations */
1721
1722 /* XXX: handle nans */
1723 void OPPROTO op_fcom_ST0_FT0(void)
1724 {
1725     env->fpus &= (~0x4500);     /* (C3,C2,C0) <-- 000 */
1726     if (ST0 < FT0)
1727         env->fpus |= 0x100;     /* (C3,C2,C0) <-- 001 */
1728     else if (ST0 == FT0)
1729         env->fpus |= 0x4000; /* (C3,C2,C0) <-- 100 */
1730     FORCE_RET();
1731 }
1732
1733 /* XXX: handle nans */
1734 void OPPROTO op_fucom_ST0_FT0(void)
1735 {
1736     env->fpus &= (~0x4500);     /* (C3,C2,C0) <-- 000 */
1737     if (ST0 < FT0)
1738         env->fpus |= 0x100;     /* (C3,C2,C0) <-- 001 */
1739     else if (ST0 == FT0)
1740         env->fpus |= 0x4000; /* (C3,C2,C0) <-- 100 */
1741     FORCE_RET();
1742 }
1743
1744 /* XXX: handle nans */
1745 void OPPROTO op_fcomi_ST0_FT0(void)
1746 {
1747     int eflags;
1748     eflags = cc_table[CC_OP].compute_all();
1749     eflags &= ~(CC_Z | CC_P | CC_C);
1750     if (ST0 < FT0)
1751         eflags |= CC_C;
1752     else if (ST0 == FT0)
1753         eflags |= CC_Z;
1754     CC_SRC = eflags;
1755     FORCE_RET();
1756 }
1757
1758 /* XXX: handle nans */
1759 void OPPROTO op_fucomi_ST0_FT0(void)
1760 {
1761     int eflags;
1762     eflags = cc_table[CC_OP].compute_all();
1763     eflags &= ~(CC_Z | CC_P | CC_C);
1764     if (ST0 < FT0)
1765         eflags |= CC_C;
1766     else if (ST0 == FT0)
1767         eflags |= CC_Z;
1768     CC_SRC = eflags;
1769     FORCE_RET();
1770 }
1771
1772 void OPPROTO op_fcmov_ST0_STN_T0(void)
1773 {
1774     if (T0) {
1775         ST0 = ST(PARAM1);
1776     }
1777     FORCE_RET();
1778 }
1779
1780 void OPPROTO op_fadd_ST0_FT0(void)
1781 {
1782     ST0 += FT0;
1783 }
1784
1785 void OPPROTO op_fmul_ST0_FT0(void)
1786 {
1787     ST0 *= FT0;
1788 }
1789
1790 void OPPROTO op_fsub_ST0_FT0(void)
1791 {
1792     ST0 -= FT0;
1793 }
1794
1795 void OPPROTO op_fsubr_ST0_FT0(void)
1796 {
1797     ST0 = FT0 - ST0;
1798 }
1799
1800 void OPPROTO op_fdiv_ST0_FT0(void)
1801 {
1802     ST0 /= FT0;
1803 }
1804
1805 void OPPROTO op_fdivr_ST0_FT0(void)
1806 {
1807     ST0 = FT0 / ST0;
1808 }
1809
1810 /* fp operations between STN and ST0 */
1811
1812 void OPPROTO op_fadd_STN_ST0(void)
1813 {
1814     ST(PARAM1) += ST0;
1815 }
1816
1817 void OPPROTO op_fmul_STN_ST0(void)
1818 {
1819     ST(PARAM1) *= ST0;
1820 }
1821
1822 void OPPROTO op_fsub_STN_ST0(void)
1823 {
1824     ST(PARAM1) -= ST0;
1825 }
1826
1827 void OPPROTO op_fsubr_STN_ST0(void)
1828 {
1829     CPU86_LDouble *p;
1830     p = &ST(PARAM1);
1831     *p = ST0 - *p;
1832 }
1833
1834 void OPPROTO op_fdiv_STN_ST0(void)
1835 {
1836     ST(PARAM1) /= ST0;
1837 }
1838
1839 void OPPROTO op_fdivr_STN_ST0(void)
1840 {
1841     CPU86_LDouble *p;
1842     p = &ST(PARAM1);
1843     *p = ST0 / *p;
1844 }
1845
1846 /* misc FPU operations */
1847 void OPPROTO op_fchs_ST0(void)
1848 {
1849     ST0 = -ST0;
1850 }
1851
1852 void OPPROTO op_fabs_ST0(void)
1853 {
1854     ST0 = fabs(ST0);
1855 }
1856
1857 void OPPROTO op_fxam_ST0(void)
1858 {
1859     helper_fxam_ST0();
1860 }
1861
1862 void OPPROTO op_fld1_ST0(void)
1863 {
1864     ST0 = f15rk[1];
1865 }
1866
1867 void OPPROTO op_fldl2t_ST0(void)
1868 {
1869     ST0 = f15rk[6];
1870 }
1871
1872 void OPPROTO op_fldl2e_ST0(void)
1873 {
1874     ST0 = f15rk[5];
1875 }
1876
1877 void OPPROTO op_fldpi_ST0(void)
1878 {
1879     ST0 = f15rk[2];
1880 }
1881
1882 void OPPROTO op_fldlg2_ST0(void)
1883 {
1884     ST0 = f15rk[3];
1885 }
1886
1887 void OPPROTO op_fldln2_ST0(void)
1888 {
1889     ST0 = f15rk[4];
1890 }
1891
1892 void OPPROTO op_fldz_ST0(void)
1893 {
1894     ST0 = f15rk[0];
1895 }
1896
1897 void OPPROTO op_fldz_FT0(void)
1898 {
1899     FT0 = f15rk[0];
1900 }
1901
1902 /* associated heplers to reduce generated code length and to simplify
1903    relocation (FP constants are usually stored in .rodata section) */
1904
1905 void OPPROTO op_f2xm1(void)
1906 {
1907     helper_f2xm1();
1908 }
1909
1910 void OPPROTO op_fyl2x(void)
1911 {
1912     helper_fyl2x();
1913 }
1914
1915 void OPPROTO op_fptan(void)
1916 {
1917     helper_fptan();
1918 }
1919
1920 void OPPROTO op_fpatan(void)
1921 {
1922     helper_fpatan();
1923 }
1924
1925 void OPPROTO op_fxtract(void)
1926 {
1927     helper_fxtract();
1928 }
1929
1930 void OPPROTO op_fprem1(void)
1931 {
1932     helper_fprem1();
1933 }
1934
1935
1936 void OPPROTO op_fprem(void)
1937 {
1938     helper_fprem();
1939 }
1940
1941 void OPPROTO op_fyl2xp1(void)
1942 {
1943     helper_fyl2xp1();
1944 }
1945
1946 void OPPROTO op_fsqrt(void)
1947 {
1948     helper_fsqrt();
1949 }
1950
1951 void OPPROTO op_fsincos(void)
1952 {
1953     helper_fsincos();
1954 }
1955
1956 void OPPROTO op_frndint(void)
1957 {
1958     helper_frndint();
1959 }
1960
1961 void OPPROTO op_fscale(void)
1962 {
1963     helper_fscale();
1964 }
1965
1966 void OPPROTO op_fsin(void)
1967 {
1968     helper_fsin();
1969 }
1970
1971 void OPPROTO op_fcos(void)
1972 {
1973     helper_fcos();
1974 }
1975
1976 void OPPROTO op_fnstsw_A0(void)
1977 {
1978     int fpus;
1979     fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1980     stw((void *)A0, fpus);
1981 }
1982
1983 void OPPROTO op_fnstsw_EAX(void)
1984 {
1985     int fpus;
1986     fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1987     EAX = (EAX & 0xffff0000) | fpus;
1988 }
1989
1990 void OPPROTO op_fnstcw_A0(void)
1991 {
1992     stw((void *)A0, env->fpuc);
1993 }
1994
1995 void OPPROTO op_fldcw_A0(void)
1996 {
1997     int rnd_type;
1998     env->fpuc = lduw((void *)A0);
1999     /* set rounding mode */
2000     switch(env->fpuc & RC_MASK) {
2001     default:
2002     case RC_NEAR:
2003         rnd_type = FE_TONEAREST;
2004         break;
2005     case RC_DOWN:
2006         rnd_type = FE_DOWNWARD;
2007         break;
2008     case RC_UP:
2009         rnd_type = FE_UPWARD;
2010         break;
2011     case RC_CHOP:
2012         rnd_type = FE_TOWARDZERO;
2013         break;
2014     }
2015     fesetround(rnd_type);
2016 }
2017
2018 void OPPROTO op_fclex(void)
2019 {
2020     env->fpus &= 0x7f00;
2021 }
2022
2023 void OPPROTO op_fninit(void)
2024 {
2025     env->fpus = 0;
2026     env->fpstt = 0;
2027     env->fpuc = 0x37f;
2028     env->fptags[0] = 1;
2029     env->fptags[1] = 1;
2030     env->fptags[2] = 1;
2031     env->fptags[3] = 1;
2032     env->fptags[4] = 1;
2033     env->fptags[5] = 1;
2034     env->fptags[6] = 1;
2035     env->fptags[7] = 1;
2036 }
2037
2038 void OPPROTO op_fnstenv_A0(void)
2039 {
2040     helper_fstenv((uint8_t *)A0, PARAM1);
2041 }
2042
2043 void OPPROTO op_fldenv_A0(void)
2044 {
2045     helper_fldenv((uint8_t *)A0, PARAM1);
2046 }
2047
2048 void OPPROTO op_fnsave_A0(void)
2049 {
2050     helper_fsave((uint8_t *)A0, PARAM1);
2051 }
2052
2053 void OPPROTO op_frstor_A0(void)
2054 {
2055     helper_frstor((uint8_t *)A0, PARAM1);
2056 }
2057
2058 /* threading support */
2059 void OPPROTO op_lock(void)
2060 {
2061     cpu_lock();
2062 }
2063
2064 void OPPROTO op_unlock(void)
2065 {
2066     cpu_unlock();
2067 }
2068