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