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