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