]> git.proxmox.com Git - qemu.git/blob - op-i386.c
first step to fix precise eflags update in case of exception
[qemu.git] / 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 /* 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 void OPPROTO op_ldub_T0_A0(void)
380 {
381 T0 = ldub((uint8_t *)A0);
382 }
383
384 void OPPROTO op_ldsb_T0_A0(void)
385 {
386 T0 = ldsb((int8_t *)A0);
387 }
388
389 void OPPROTO op_lduw_T0_A0(void)
390 {
391 T0 = lduw((uint8_t *)A0);
392 }
393
394 void OPPROTO op_ldsw_T0_A0(void)
395 {
396 T0 = ldsw((int8_t *)A0);
397 }
398
399 void OPPROTO op_ldl_T0_A0(void)
400 {
401 T0 = ldl((uint8_t *)A0);
402 }
403
404 void OPPROTO op_ldub_T1_A0(void)
405 {
406 T1 = ldub((uint8_t *)A0);
407 }
408
409 void OPPROTO op_ldsb_T1_A0(void)
410 {
411 T1 = ldsb((int8_t *)A0);
412 }
413
414 void OPPROTO op_lduw_T1_A0(void)
415 {
416 T1 = lduw((uint8_t *)A0);
417 }
418
419 void OPPROTO op_ldsw_T1_A0(void)
420 {
421 T1 = ldsw((int8_t *)A0);
422 }
423
424 void OPPROTO op_ldl_T1_A0(void)
425 {
426 T1 = ldl((uint8_t *)A0);
427 }
428
429 void OPPROTO op_stb_T0_A0(void)
430 {
431 stb((uint8_t *)A0, T0);
432 }
433
434 void OPPROTO op_stw_T0_A0(void)
435 {
436 stw((uint8_t *)A0, T0);
437 }
438
439 void OPPROTO op_stl_T0_A0(void)
440 {
441 stl((uint8_t *)A0, T0);
442 }
443
444 /* used for bit operations */
445
446 void OPPROTO op_add_bitw_A0_T1(void)
447 {
448 A0 += ((int32_t)T1 >> 4) << 1;
449 }
450
451 void OPPROTO op_add_bitl_A0_T1(void)
452 {
453 A0 += ((int32_t)T1 >> 5) << 2;
454 }
455
456 /* indirect jump */
457
458 void OPPROTO op_jmp_T0(void)
459 {
460 EIP = T0;
461 }
462
463 void OPPROTO op_jmp_im(void)
464 {
465 EIP = PARAM1;
466 }
467
468 void OPPROTO op_hlt(void)
469 {
470 env->exception_index = EXCP_HLT;
471 cpu_loop_exit();
472 }
473
474 void OPPROTO op_raise_interrupt(void)
475 {
476 int intno;
477 unsigned int next_eip;
478 intno = PARAM1;
479 next_eip = PARAM2;
480 raise_interrupt(intno, 1, 0, next_eip);
481 }
482
483 void OPPROTO op_raise_exception(void)
484 {
485 int exception_index;
486 exception_index = PARAM1;
487 raise_exception(exception_index);
488 }
489
490 void OPPROTO op_into(void)
491 {
492 int eflags;
493 eflags = cc_table[CC_OP].compute_all();
494 if (eflags & CC_O) {
495 raise_interrupt(EXCP04_INTO, 1, 0, PARAM1);
496 }
497 FORCE_RET();
498 }
499
500 void OPPROTO op_cli(void)
501 {
502 env->eflags &= ~IF_MASK;
503 }
504
505 void OPPROTO op_sti(void)
506 {
507 env->eflags |= IF_MASK;
508 }
509
510 #if 0
511 /* vm86plus instructions */
512 void OPPROTO op_cli_vm(void)
513 {
514 env->eflags &= ~VIF_MASK;
515 }
516
517 void OPPROTO op_sti_vm(void)
518 {
519 env->eflags |= VIF_MASK;
520 if (env->eflags & VIP_MASK) {
521 EIP = PARAM1;
522 raise_exception(EXCP0D_GPF);
523 }
524 FORCE_RET();
525 }
526 #endif
527
528 void OPPROTO op_boundw(void)
529 {
530 int low, high, v;
531 low = ldsw((uint8_t *)A0);
532 high = ldsw((uint8_t *)A0 + 2);
533 v = (int16_t)T0;
534 if (v < low || v > high) {
535 EIP = PARAM1;
536 raise_exception(EXCP05_BOUND);
537 }
538 FORCE_RET();
539 }
540
541 void OPPROTO op_boundl(void)
542 {
543 int low, high, v;
544 low = ldl((uint8_t *)A0);
545 high = ldl((uint8_t *)A0 + 4);
546 v = T0;
547 if (v < low || v > high) {
548 EIP = PARAM1;
549 raise_exception(EXCP05_BOUND);
550 }
551 FORCE_RET();
552 }
553
554 void OPPROTO op_cmpxchg8b(void)
555 {
556 helper_cmpxchg8b();
557 }
558
559 void OPPROTO op_jmp_tb_next(void)
560 {
561 JUMP_TB(PARAM1, 0, PARAM2);
562 }
563
564 void OPPROTO op_movl_T0_0(void)
565 {
566 T0 = 0;
567 }
568
569 /* multiple size ops */
570
571 #define ldul ldl
572
573 #define SHIFT 0
574 #include "ops_template.h"
575 #undef SHIFT
576
577 #define SHIFT 1
578 #include "ops_template.h"
579 #undef SHIFT
580
581 #define SHIFT 2
582 #include "ops_template.h"
583 #undef SHIFT
584
585 /* sign extend */
586
587 void OPPROTO op_movsbl_T0_T0(void)
588 {
589 T0 = (int8_t)T0;
590 }
591
592 void OPPROTO op_movzbl_T0_T0(void)
593 {
594 T0 = (uint8_t)T0;
595 }
596
597 void OPPROTO op_movswl_T0_T0(void)
598 {
599 T0 = (int16_t)T0;
600 }
601
602 void OPPROTO op_movzwl_T0_T0(void)
603 {
604 T0 = (uint16_t)T0;
605 }
606
607 void OPPROTO op_movswl_EAX_AX(void)
608 {
609 EAX = (int16_t)EAX;
610 }
611
612 void OPPROTO op_movsbw_AX_AL(void)
613 {
614 EAX = (EAX & 0xffff0000) | ((int8_t)EAX & 0xffff);
615 }
616
617 void OPPROTO op_movslq_EDX_EAX(void)
618 {
619 EDX = (int32_t)EAX >> 31;
620 }
621
622 void OPPROTO op_movswl_DX_AX(void)
623 {
624 EDX = (EDX & 0xffff0000) | (((int16_t)EAX >> 15) & 0xffff);
625 }
626
627 /* push/pop */
628
629 void op_pushl_T0(void)
630 {
631 uint32_t offset;
632 offset = ESP - 4;
633 stl((void *)offset, T0);
634 /* modify ESP after to handle exceptions correctly */
635 ESP = offset;
636 }
637
638 void op_pushw_T0(void)
639 {
640 uint32_t offset;
641 offset = ESP - 2;
642 stw((void *)offset, T0);
643 /* modify ESP after to handle exceptions correctly */
644 ESP = offset;
645 }
646
647 void op_pushl_ss32_T0(void)
648 {
649 uint32_t offset;
650 offset = ESP - 4;
651 stl(env->segs[R_SS].base + offset, T0);
652 /* modify ESP after to handle exceptions correctly */
653 ESP = offset;
654 }
655
656 void op_pushw_ss32_T0(void)
657 {
658 uint32_t offset;
659 offset = ESP - 2;
660 stw(env->segs[R_SS].base + offset, T0);
661 /* modify ESP after to handle exceptions correctly */
662 ESP = offset;
663 }
664
665 void op_pushl_ss16_T0(void)
666 {
667 uint32_t offset;
668 offset = (ESP - 4) & 0xffff;
669 stl(env->segs[R_SS].base + offset, T0);
670 /* modify ESP after to handle exceptions correctly */
671 ESP = (ESP & ~0xffff) | offset;
672 }
673
674 void op_pushw_ss16_T0(void)
675 {
676 uint32_t offset;
677 offset = (ESP - 2) & 0xffff;
678 stw(env->segs[R_SS].base + offset, T0);
679 /* modify ESP after to handle exceptions correctly */
680 ESP = (ESP & ~0xffff) | offset;
681 }
682
683 /* NOTE: ESP update is done after */
684 void op_popl_T0(void)
685 {
686 T0 = ldl((void *)ESP);
687 }
688
689 void op_popw_T0(void)
690 {
691 T0 = lduw((void *)ESP);
692 }
693
694 void op_popl_ss32_T0(void)
695 {
696 T0 = ldl(env->segs[R_SS].base + ESP);
697 }
698
699 void op_popw_ss32_T0(void)
700 {
701 T0 = lduw(env->segs[R_SS].base + ESP);
702 }
703
704 void op_popl_ss16_T0(void)
705 {
706 T0 = ldl(env->segs[R_SS].base + (ESP & 0xffff));
707 }
708
709 void op_popw_ss16_T0(void)
710 {
711 T0 = lduw(env->segs[R_SS].base + (ESP & 0xffff));
712 }
713
714 void op_addl_ESP_4(void)
715 {
716 ESP += 4;
717 }
718
719 void op_addl_ESP_2(void)
720 {
721 ESP += 2;
722 }
723
724 void op_addw_ESP_4(void)
725 {
726 ESP = (ESP & ~0xffff) | ((ESP + 4) & 0xffff);
727 }
728
729 void op_addw_ESP_2(void)
730 {
731 ESP = (ESP & ~0xffff) | ((ESP + 2) & 0xffff);
732 }
733
734 void op_addl_ESP_im(void)
735 {
736 ESP += PARAM1;
737 }
738
739 void op_addw_ESP_im(void)
740 {
741 ESP = (ESP & ~0xffff) | ((ESP + PARAM1) & 0xffff);
742 }
743
744 void OPPROTO op_rdtsc(void)
745 {
746 helper_rdtsc();
747 }
748
749 void OPPROTO op_cpuid(void)
750 {
751 helper_cpuid();
752 }
753
754 /* bcd */
755
756 /* XXX: exception */
757 void OPPROTO op_aam(void)
758 {
759 int base = PARAM1;
760 int al, ah;
761 al = EAX & 0xff;
762 ah = al / base;
763 al = al % base;
764 EAX = (EAX & ~0xffff) | al | (ah << 8);
765 CC_DST = al;
766 }
767
768 void OPPROTO op_aad(void)
769 {
770 int base = PARAM1;
771 int al, ah;
772 al = EAX & 0xff;
773 ah = (EAX >> 8) & 0xff;
774 al = ((ah * base) + al) & 0xff;
775 EAX = (EAX & ~0xffff) | al;
776 CC_DST = al;
777 }
778
779 void OPPROTO op_aaa(void)
780 {
781 int icarry;
782 int al, ah, af;
783 int eflags;
784
785 eflags = cc_table[CC_OP].compute_all();
786 af = eflags & CC_A;
787 al = EAX & 0xff;
788 ah = (EAX >> 8) & 0xff;
789
790 icarry = (al > 0xf9);
791 if (((al & 0x0f) > 9 ) || af) {
792 al = (al + 6) & 0x0f;
793 ah = (ah + 1 + icarry) & 0xff;
794 eflags |= CC_C | CC_A;
795 } else {
796 eflags &= ~(CC_C | CC_A);
797 al &= 0x0f;
798 }
799 EAX = (EAX & ~0xffff) | al | (ah << 8);
800 CC_SRC = eflags;
801 }
802
803 void OPPROTO op_aas(void)
804 {
805 int icarry;
806 int al, ah, af;
807 int eflags;
808
809 eflags = cc_table[CC_OP].compute_all();
810 af = eflags & CC_A;
811 al = EAX & 0xff;
812 ah = (EAX >> 8) & 0xff;
813
814 icarry = (al < 6);
815 if (((al & 0x0f) > 9 ) || af) {
816 al = (al - 6) & 0x0f;
817 ah = (ah - 1 - icarry) & 0xff;
818 eflags |= CC_C | CC_A;
819 } else {
820 eflags &= ~(CC_C | CC_A);
821 al &= 0x0f;
822 }
823 EAX = (EAX & ~0xffff) | al | (ah << 8);
824 CC_SRC = eflags;
825 }
826
827 void OPPROTO op_daa(void)
828 {
829 int al, af, cf;
830 int eflags;
831
832 eflags = cc_table[CC_OP].compute_all();
833 cf = eflags & CC_C;
834 af = eflags & CC_A;
835 al = EAX & 0xff;
836
837 eflags = 0;
838 if (((al & 0x0f) > 9 ) || af) {
839 al = (al + 6) & 0xff;
840 eflags |= CC_A;
841 }
842 if ((al > 0x9f) || cf) {
843 al = (al + 0x60) & 0xff;
844 eflags |= CC_C;
845 }
846 EAX = (EAX & ~0xff) | al;
847 /* well, speed is not an issue here, so we compute the flags by hand */
848 eflags |= (al == 0) << 6; /* zf */
849 eflags |= parity_table[al]; /* pf */
850 eflags |= (al & 0x80); /* sf */
851 CC_SRC = eflags;
852 }
853
854 void OPPROTO op_das(void)
855 {
856 int al, al1, af, cf;
857 int eflags;
858
859 eflags = cc_table[CC_OP].compute_all();
860 cf = eflags & CC_C;
861 af = eflags & CC_A;
862 al = EAX & 0xff;
863
864 eflags = 0;
865 al1 = al;
866 if (((al & 0x0f) > 9 ) || af) {
867 eflags |= CC_A;
868 if (al < 6 || cf)
869 eflags |= CC_C;
870 al = (al - 6) & 0xff;
871 }
872 if ((al1 > 0x99) || cf) {
873 al = (al - 0x60) & 0xff;
874 eflags |= CC_C;
875 }
876 EAX = (EAX & ~0xff) | al;
877 /* well, speed is not an issue here, so we compute the flags by hand */
878 eflags |= (al == 0) << 6; /* zf */
879 eflags |= parity_table[al]; /* pf */
880 eflags |= (al & 0x80); /* sf */
881 CC_SRC = eflags;
882 }
883
884 /* segment handling */
885
886 void OPPROTO op_movl_seg_T0(void)
887 {
888 load_seg(PARAM1, T0 & 0xffff, PARAM2);
889 }
890
891 /* faster VM86 version */
892 void OPPROTO op_movl_seg_T0_vm(void)
893 {
894 int selector;
895 SegmentCache *sc;
896
897 selector = T0 & 0xffff;
898 /* env->segs[] access */
899 sc = (SegmentCache *)((char *)env + PARAM1);
900 sc->selector = selector;
901 sc->base = (void *)(selector << 4);
902 }
903
904 void OPPROTO op_movl_T0_seg(void)
905 {
906 T0 = env->segs[PARAM1].selector;
907 }
908
909 void OPPROTO op_movl_A0_seg(void)
910 {
911 A0 = *(unsigned long *)((char *)env + PARAM1);
912 }
913
914 void OPPROTO op_addl_A0_seg(void)
915 {
916 A0 += *(unsigned long *)((char *)env + PARAM1);
917 }
918
919 void OPPROTO op_lsl(void)
920 {
921 helper_lsl();
922 }
923
924 void OPPROTO op_lar(void)
925 {
926 helper_lar();
927 }
928
929 /* T0: segment, T1:eip */
930 void OPPROTO op_ljmp_T0_T1(void)
931 {
932 jmp_seg(T0 & 0xffff, T1);
933 }
934
935 void OPPROTO op_iret_protected(void)
936 {
937 helper_iret_protected(PARAM1);
938 }
939
940 void OPPROTO op_lldt_T0(void)
941 {
942 helper_lldt_T0();
943 }
944
945 void OPPROTO op_ltr_T0(void)
946 {
947 helper_ltr_T0();
948 }
949
950 /* CR registers access */
951 void OPPROTO op_movl_crN_T0(void)
952 {
953 helper_movl_crN_T0(PARAM1);
954 }
955
956 /* DR registers access */
957 void OPPROTO op_movl_drN_T0(void)
958 {
959 helper_movl_drN_T0(PARAM1);
960 }
961
962 void OPPROTO op_lmsw_T0(void)
963 {
964 /* only 4 lower bits of CR0 are modified */
965 T0 = (env->cr[0] & ~0xf) | (T0 & 0xf);
966 helper_movl_crN_T0(0);
967 }
968
969 void OPPROTO op_invlpg_A0(void)
970 {
971 helper_invlpg(A0);
972 }
973
974 void OPPROTO op_movl_T0_env(void)
975 {
976 T0 = *(uint32_t *)((char *)env + PARAM1);
977 }
978
979 void OPPROTO op_movl_env_T0(void)
980 {
981 *(uint32_t *)((char *)env + PARAM1) = T0;
982 }
983
984 void OPPROTO op_movl_env_T1(void)
985 {
986 *(uint32_t *)((char *)env + PARAM1) = T1;
987 }
988
989 void OPPROTO op_clts(void)
990 {
991 env->cr[0] &= ~CR0_TS_MASK;
992 }
993
994 /* flags handling */
995
996 /* slow jumps cases : in order to avoid calling a function with a
997 pointer (which can generate a stack frame on PowerPC), we use
998 op_setcc to set T0 and then call op_jcc. */
999 void OPPROTO op_jcc(void)
1000 {
1001 if (T0)
1002 JUMP_TB(PARAM1, 0, PARAM2);
1003 else
1004 JUMP_TB(PARAM1, 1, PARAM3);
1005 FORCE_RET();
1006 }
1007
1008 /* slow set cases (compute x86 flags) */
1009 void OPPROTO op_seto_T0_cc(void)
1010 {
1011 int eflags;
1012 eflags = cc_table[CC_OP].compute_all();
1013 T0 = (eflags >> 11) & 1;
1014 }
1015
1016 void OPPROTO op_setb_T0_cc(void)
1017 {
1018 T0 = cc_table[CC_OP].compute_c();
1019 }
1020
1021 void OPPROTO op_setz_T0_cc(void)
1022 {
1023 int eflags;
1024 eflags = cc_table[CC_OP].compute_all();
1025 T0 = (eflags >> 6) & 1;
1026 }
1027
1028 void OPPROTO op_setbe_T0_cc(void)
1029 {
1030 int eflags;
1031 eflags = cc_table[CC_OP].compute_all();
1032 T0 = (eflags & (CC_Z | CC_C)) != 0;
1033 }
1034
1035 void OPPROTO op_sets_T0_cc(void)
1036 {
1037 int eflags;
1038 eflags = cc_table[CC_OP].compute_all();
1039 T0 = (eflags >> 7) & 1;
1040 }
1041
1042 void OPPROTO op_setp_T0_cc(void)
1043 {
1044 int eflags;
1045 eflags = cc_table[CC_OP].compute_all();
1046 T0 = (eflags >> 2) & 1;
1047 }
1048
1049 void OPPROTO op_setl_T0_cc(void)
1050 {
1051 int eflags;
1052 eflags = cc_table[CC_OP].compute_all();
1053 T0 = ((eflags ^ (eflags >> 4)) >> 7) & 1;
1054 }
1055
1056 void OPPROTO op_setle_T0_cc(void)
1057 {
1058 int eflags;
1059 eflags = cc_table[CC_OP].compute_all();
1060 T0 = (((eflags ^ (eflags >> 4)) & 0x80) || (eflags & CC_Z)) != 0;
1061 }
1062
1063 void OPPROTO op_xor_T0_1(void)
1064 {
1065 T0 ^= 1;
1066 }
1067
1068 void OPPROTO op_set_cc_op(void)
1069 {
1070 CC_OP = PARAM1;
1071 }
1072
1073 #define FL_UPDATE_MASK16 (FL_UPDATE_MASK32 & 0xffff)
1074
1075 void OPPROTO op_movl_eflags_T0(void)
1076 {
1077 int eflags;
1078 eflags = T0;
1079 CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1080 DF = 1 - (2 * ((eflags >> 10) & 1));
1081 /* we also update some system flags as in user mode */
1082 env->eflags = (env->eflags & ~FL_UPDATE_MASK32) |
1083 (eflags & FL_UPDATE_MASK32);
1084 }
1085
1086 void OPPROTO op_movw_eflags_T0(void)
1087 {
1088 int eflags;
1089 eflags = T0;
1090 CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1091 DF = 1 - (2 * ((eflags >> 10) & 1));
1092 /* we also update some system flags as in user mode */
1093 env->eflags = (env->eflags & ~FL_UPDATE_MASK16) |
1094 (eflags & FL_UPDATE_MASK16);
1095 }
1096
1097 void OPPROTO op_movl_eflags_T0_cpl0(void)
1098 {
1099 load_eflags(T0, FL_UPDATE_CPL0_MASK);
1100 }
1101
1102 void OPPROTO op_movw_eflags_T0_cpl0(void)
1103 {
1104 load_eflags(T0, FL_UPDATE_CPL0_MASK & 0xffff);
1105 }
1106
1107 #if 0
1108 /* vm86plus version */
1109 void OPPROTO op_movw_eflags_T0_vm(void)
1110 {
1111 int eflags;
1112 eflags = T0;
1113 CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1114 DF = 1 - (2 * ((eflags >> 10) & 1));
1115 /* we also update some system flags as in user mode */
1116 env->eflags = (env->eflags & ~(FL_UPDATE_MASK16 | VIF_MASK)) |
1117 (eflags & FL_UPDATE_MASK16);
1118 if (eflags & IF_MASK) {
1119 env->eflags |= VIF_MASK;
1120 if (env->eflags & VIP_MASK) {
1121 EIP = PARAM1;
1122 raise_exception(EXCP0D_GPF);
1123 }
1124 }
1125 FORCE_RET();
1126 }
1127
1128 void OPPROTO op_movl_eflags_T0_vm(void)
1129 {
1130 int eflags;
1131 eflags = T0;
1132 CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1133 DF = 1 - (2 * ((eflags >> 10) & 1));
1134 /* we also update some system flags as in user mode */
1135 env->eflags = (env->eflags & ~(FL_UPDATE_MASK32 | VIF_MASK)) |
1136 (eflags & FL_UPDATE_MASK32);
1137 if (eflags & IF_MASK) {
1138 env->eflags |= VIF_MASK;
1139 if (env->eflags & VIP_MASK) {
1140 EIP = PARAM1;
1141 raise_exception(EXCP0D_GPF);
1142 }
1143 }
1144 FORCE_RET();
1145 }
1146 #endif
1147
1148 /* XXX: compute only O flag */
1149 void OPPROTO op_movb_eflags_T0(void)
1150 {
1151 int of;
1152 of = cc_table[CC_OP].compute_all() & CC_O;
1153 CC_SRC = (T0 & (CC_S | CC_Z | CC_A | CC_P | CC_C)) | of;
1154 }
1155
1156 void OPPROTO op_movl_T0_eflags(void)
1157 {
1158 int eflags;
1159 eflags = cc_table[CC_OP].compute_all();
1160 eflags |= (DF & DF_MASK);
1161 eflags |= env->eflags & ~(VM_MASK | RF_MASK);
1162 T0 = eflags;
1163 }
1164
1165 /* vm86plus version */
1166 #if 0
1167 void OPPROTO op_movl_T0_eflags_vm(void)
1168 {
1169 int eflags;
1170 eflags = cc_table[CC_OP].compute_all();
1171 eflags |= (DF & DF_MASK);
1172 eflags |= env->eflags & ~(VM_MASK | RF_MASK | IF_MASK);
1173 if (env->eflags & VIF_MASK)
1174 eflags |= IF_MASK;
1175 T0 = eflags;
1176 }
1177 #endif
1178
1179 void OPPROTO op_cld(void)
1180 {
1181 DF = 1;
1182 }
1183
1184 void OPPROTO op_std(void)
1185 {
1186 DF = -1;
1187 }
1188
1189 void OPPROTO op_clc(void)
1190 {
1191 int eflags;
1192 eflags = cc_table[CC_OP].compute_all();
1193 eflags &= ~CC_C;
1194 CC_SRC = eflags;
1195 }
1196
1197 void OPPROTO op_stc(void)
1198 {
1199 int eflags;
1200 eflags = cc_table[CC_OP].compute_all();
1201 eflags |= CC_C;
1202 CC_SRC = eflags;
1203 }
1204
1205 void OPPROTO op_cmc(void)
1206 {
1207 int eflags;
1208 eflags = cc_table[CC_OP].compute_all();
1209 eflags ^= CC_C;
1210 CC_SRC = eflags;
1211 }
1212
1213 void OPPROTO op_salc(void)
1214 {
1215 int cf;
1216 cf = cc_table[CC_OP].compute_c();
1217 EAX = (EAX & ~0xff) | ((-cf) & 0xff);
1218 }
1219
1220 static int compute_all_eflags(void)
1221 {
1222 return CC_SRC;
1223 }
1224
1225 static int compute_c_eflags(void)
1226 {
1227 return CC_SRC & CC_C;
1228 }
1229
1230 static int compute_c_mul(void)
1231 {
1232 int cf;
1233 cf = (CC_SRC != 0);
1234 return cf;
1235 }
1236
1237 static int compute_all_mul(void)
1238 {
1239 int cf, pf, af, zf, sf, of;
1240 cf = (CC_SRC != 0);
1241 pf = 0; /* undefined */
1242 af = 0; /* undefined */
1243 zf = 0; /* undefined */
1244 sf = 0; /* undefined */
1245 of = cf << 11;
1246 return cf | pf | af | zf | sf | of;
1247 }
1248
1249 CCTable cc_table[CC_OP_NB] = {
1250 [CC_OP_DYNAMIC] = { /* should never happen */ },
1251
1252 [CC_OP_EFLAGS] = { compute_all_eflags, compute_c_eflags },
1253
1254 [CC_OP_MUL] = { compute_all_mul, compute_c_mul },
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 #ifdef USE_FP_CONVERT
1410 FP_CONVERT.i32 = ldl((void *)A0);
1411 ST0 = FP_CONVERT.f;
1412 #else
1413 ST0 = ldfl((void *)A0);
1414 #endif
1415 }
1416
1417 void OPPROTO op_fldl_ST0_A0(void)
1418 {
1419 #ifdef USE_FP_CONVERT
1420 FP_CONVERT.i64 = ldq((void *)A0);
1421 ST0 = FP_CONVERT.d;
1422 #else
1423 ST0 = ldfq((void *)A0);
1424 #endif
1425 }
1426
1427 #ifdef USE_X86LDOUBLE
1428 void OPPROTO op_fldt_ST0_A0(void)
1429 {
1430 ST0 = *(long double *)A0;
1431 }
1432 #else
1433 void OPPROTO op_fldt_ST0_A0(void)
1434 {
1435 helper_fldt_ST0_A0();
1436 }
1437 #endif
1438
1439 /* helpers are needed to avoid static constant reference. XXX: find a better way */
1440 #ifdef USE_INT_TO_FLOAT_HELPERS
1441
1442 void helper_fild_ST0_A0(void)
1443 {
1444 ST0 = (CPU86_LDouble)ldsw((void *)A0);
1445 }
1446
1447 void helper_fildl_ST0_A0(void)
1448 {
1449 ST0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1450 }
1451
1452 void helper_fildll_ST0_A0(void)
1453 {
1454 ST0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1455 }
1456
1457 void OPPROTO op_fild_ST0_A0(void)
1458 {
1459 helper_fild_ST0_A0();
1460 }
1461
1462 void OPPROTO op_fildl_ST0_A0(void)
1463 {
1464 helper_fildl_ST0_A0();
1465 }
1466
1467 void OPPROTO op_fildll_ST0_A0(void)
1468 {
1469 helper_fildll_ST0_A0();
1470 }
1471
1472 #else
1473
1474 void OPPROTO op_fild_ST0_A0(void)
1475 {
1476 #ifdef USE_FP_CONVERT
1477 FP_CONVERT.i32 = ldsw((void *)A0);
1478 ST0 = (CPU86_LDouble)FP_CONVERT.i32;
1479 #else
1480 ST0 = (CPU86_LDouble)ldsw((void *)A0);
1481 #endif
1482 }
1483
1484 void OPPROTO op_fildl_ST0_A0(void)
1485 {
1486 #ifdef USE_FP_CONVERT
1487 FP_CONVERT.i32 = (int32_t) ldl((void *)A0);
1488 ST0 = (CPU86_LDouble)FP_CONVERT.i32;
1489 #else
1490 ST0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1491 #endif
1492 }
1493
1494 void OPPROTO op_fildll_ST0_A0(void)
1495 {
1496 #ifdef USE_FP_CONVERT
1497 FP_CONVERT.i64 = (int64_t) ldq((void *)A0);
1498 ST0 = (CPU86_LDouble)FP_CONVERT.i64;
1499 #else
1500 ST0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1501 #endif
1502 }
1503
1504 #endif
1505
1506 /* fp store */
1507
1508 void OPPROTO op_fsts_ST0_A0(void)
1509 {
1510 #ifdef USE_FP_CONVERT
1511 FP_CONVERT.f = (float)ST0;
1512 stfl((void *)A0, FP_CONVERT.f);
1513 #else
1514 stfl((void *)A0, (float)ST0);
1515 #endif
1516 }
1517
1518 void OPPROTO op_fstl_ST0_A0(void)
1519 {
1520 stfq((void *)A0, (double)ST0);
1521 }
1522
1523 #ifdef USE_X86LDOUBLE
1524 void OPPROTO op_fstt_ST0_A0(void)
1525 {
1526 *(long double *)A0 = ST0;
1527 }
1528 #else
1529 void OPPROTO op_fstt_ST0_A0(void)
1530 {
1531 helper_fstt_ST0_A0();
1532 }
1533 #endif
1534
1535 void OPPROTO op_fist_ST0_A0(void)
1536 {
1537 #if defined(__sparc__) && !defined(__sparc_v9__)
1538 register CPU86_LDouble d asm("o0");
1539 #else
1540 CPU86_LDouble d;
1541 #endif
1542 int val;
1543
1544 d = ST0;
1545 val = lrint(d);
1546 if (val != (int16_t)val)
1547 val = -32768;
1548 stw((void *)A0, val);
1549 }
1550
1551 void OPPROTO op_fistl_ST0_A0(void)
1552 {
1553 #if defined(__sparc__) && !defined(__sparc_v9__)
1554 register CPU86_LDouble d asm("o0");
1555 #else
1556 CPU86_LDouble d;
1557 #endif
1558 int val;
1559
1560 d = ST0;
1561 val = lrint(d);
1562 stl((void *)A0, val);
1563 }
1564
1565 void OPPROTO op_fistll_ST0_A0(void)
1566 {
1567 #if defined(__sparc__) && !defined(__sparc_v9__)
1568 register CPU86_LDouble d asm("o0");
1569 #else
1570 CPU86_LDouble d;
1571 #endif
1572 int64_t val;
1573
1574 d = ST0;
1575 val = llrint(d);
1576 stq((void *)A0, val);
1577 }
1578
1579 void OPPROTO op_fbld_ST0_A0(void)
1580 {
1581 helper_fbld_ST0_A0();
1582 }
1583
1584 void OPPROTO op_fbst_ST0_A0(void)
1585 {
1586 helper_fbst_ST0_A0();
1587 }
1588
1589 /* FPU move */
1590
1591 void OPPROTO op_fpush(void)
1592 {
1593 fpush();
1594 }
1595
1596 void OPPROTO op_fpop(void)
1597 {
1598 fpop();
1599 }
1600
1601 void OPPROTO op_fdecstp(void)
1602 {
1603 env->fpstt = (env->fpstt - 1) & 7;
1604 env->fpus &= (~0x4700);
1605 }
1606
1607 void OPPROTO op_fincstp(void)
1608 {
1609 env->fpstt = (env->fpstt + 1) & 7;
1610 env->fpus &= (~0x4700);
1611 }
1612
1613 void OPPROTO op_fmov_ST0_FT0(void)
1614 {
1615 ST0 = FT0;
1616 }
1617
1618 void OPPROTO op_fmov_FT0_STN(void)
1619 {
1620 FT0 = ST(PARAM1);
1621 }
1622
1623 void OPPROTO op_fmov_ST0_STN(void)
1624 {
1625 ST0 = ST(PARAM1);
1626 }
1627
1628 void OPPROTO op_fmov_STN_ST0(void)
1629 {
1630 ST(PARAM1) = ST0;
1631 }
1632
1633 void OPPROTO op_fxchg_ST0_STN(void)
1634 {
1635 CPU86_LDouble tmp;
1636 tmp = ST(PARAM1);
1637 ST(PARAM1) = ST0;
1638 ST0 = tmp;
1639 }
1640
1641 /* FPU operations */
1642
1643 /* XXX: handle nans */
1644 void OPPROTO op_fcom_ST0_FT0(void)
1645 {
1646 env->fpus &= (~0x4500); /* (C3,C2,C0) <-- 000 */
1647 if (ST0 < FT0)
1648 env->fpus |= 0x100; /* (C3,C2,C0) <-- 001 */
1649 else if (ST0 == FT0)
1650 env->fpus |= 0x4000; /* (C3,C2,C0) <-- 100 */
1651 FORCE_RET();
1652 }
1653
1654 /* XXX: handle nans */
1655 void OPPROTO op_fucom_ST0_FT0(void)
1656 {
1657 env->fpus &= (~0x4500); /* (C3,C2,C0) <-- 000 */
1658 if (ST0 < FT0)
1659 env->fpus |= 0x100; /* (C3,C2,C0) <-- 001 */
1660 else if (ST0 == FT0)
1661 env->fpus |= 0x4000; /* (C3,C2,C0) <-- 100 */
1662 FORCE_RET();
1663 }
1664
1665 /* XXX: handle nans */
1666 void OPPROTO op_fcomi_ST0_FT0(void)
1667 {
1668 int eflags;
1669 eflags = cc_table[CC_OP].compute_all();
1670 eflags &= ~(CC_Z | CC_P | CC_C);
1671 if (ST0 < FT0)
1672 eflags |= CC_C;
1673 else if (ST0 == FT0)
1674 eflags |= CC_Z;
1675 CC_SRC = eflags;
1676 FORCE_RET();
1677 }
1678
1679 /* XXX: handle nans */
1680 void OPPROTO op_fucomi_ST0_FT0(void)
1681 {
1682 int eflags;
1683 eflags = cc_table[CC_OP].compute_all();
1684 eflags &= ~(CC_Z | CC_P | CC_C);
1685 if (ST0 < FT0)
1686 eflags |= CC_C;
1687 else if (ST0 == FT0)
1688 eflags |= CC_Z;
1689 CC_SRC = eflags;
1690 FORCE_RET();
1691 }
1692
1693 void OPPROTO op_fadd_ST0_FT0(void)
1694 {
1695 ST0 += FT0;
1696 }
1697
1698 void OPPROTO op_fmul_ST0_FT0(void)
1699 {
1700 ST0 *= FT0;
1701 }
1702
1703 void OPPROTO op_fsub_ST0_FT0(void)
1704 {
1705 ST0 -= FT0;
1706 }
1707
1708 void OPPROTO op_fsubr_ST0_FT0(void)
1709 {
1710 ST0 = FT0 - ST0;
1711 }
1712
1713 void OPPROTO op_fdiv_ST0_FT0(void)
1714 {
1715 ST0 /= FT0;
1716 }
1717
1718 void OPPROTO op_fdivr_ST0_FT0(void)
1719 {
1720 ST0 = FT0 / ST0;
1721 }
1722
1723 /* fp operations between STN and ST0 */
1724
1725 void OPPROTO op_fadd_STN_ST0(void)
1726 {
1727 ST(PARAM1) += ST0;
1728 }
1729
1730 void OPPROTO op_fmul_STN_ST0(void)
1731 {
1732 ST(PARAM1) *= ST0;
1733 }
1734
1735 void OPPROTO op_fsub_STN_ST0(void)
1736 {
1737 ST(PARAM1) -= ST0;
1738 }
1739
1740 void OPPROTO op_fsubr_STN_ST0(void)
1741 {
1742 CPU86_LDouble *p;
1743 p = &ST(PARAM1);
1744 *p = ST0 - *p;
1745 }
1746
1747 void OPPROTO op_fdiv_STN_ST0(void)
1748 {
1749 ST(PARAM1) /= ST0;
1750 }
1751
1752 void OPPROTO op_fdivr_STN_ST0(void)
1753 {
1754 CPU86_LDouble *p;
1755 p = &ST(PARAM1);
1756 *p = ST0 / *p;
1757 }
1758
1759 /* misc FPU operations */
1760 void OPPROTO op_fchs_ST0(void)
1761 {
1762 ST0 = -ST0;
1763 }
1764
1765 void OPPROTO op_fabs_ST0(void)
1766 {
1767 ST0 = fabs(ST0);
1768 }
1769
1770 void OPPROTO op_fxam_ST0(void)
1771 {
1772 helper_fxam_ST0();
1773 }
1774
1775 void OPPROTO op_fld1_ST0(void)
1776 {
1777 ST0 = f15rk[1];
1778 }
1779
1780 void OPPROTO op_fldl2t_ST0(void)
1781 {
1782 ST0 = f15rk[6];
1783 }
1784
1785 void OPPROTO op_fldl2e_ST0(void)
1786 {
1787 ST0 = f15rk[5];
1788 }
1789
1790 void OPPROTO op_fldpi_ST0(void)
1791 {
1792 ST0 = f15rk[2];
1793 }
1794
1795 void OPPROTO op_fldlg2_ST0(void)
1796 {
1797 ST0 = f15rk[3];
1798 }
1799
1800 void OPPROTO op_fldln2_ST0(void)
1801 {
1802 ST0 = f15rk[4];
1803 }
1804
1805 void OPPROTO op_fldz_ST0(void)
1806 {
1807 ST0 = f15rk[0];
1808 }
1809
1810 void OPPROTO op_fldz_FT0(void)
1811 {
1812 ST0 = f15rk[0];
1813 }
1814
1815 /* associated heplers to reduce generated code length and to simplify
1816 relocation (FP constants are usually stored in .rodata section) */
1817
1818 void OPPROTO op_f2xm1(void)
1819 {
1820 helper_f2xm1();
1821 }
1822
1823 void OPPROTO op_fyl2x(void)
1824 {
1825 helper_fyl2x();
1826 }
1827
1828 void OPPROTO op_fptan(void)
1829 {
1830 helper_fptan();
1831 }
1832
1833 void OPPROTO op_fpatan(void)
1834 {
1835 helper_fpatan();
1836 }
1837
1838 void OPPROTO op_fxtract(void)
1839 {
1840 helper_fxtract();
1841 }
1842
1843 void OPPROTO op_fprem1(void)
1844 {
1845 helper_fprem1();
1846 }
1847
1848
1849 void OPPROTO op_fprem(void)
1850 {
1851 helper_fprem();
1852 }
1853
1854 void OPPROTO op_fyl2xp1(void)
1855 {
1856 helper_fyl2xp1();
1857 }
1858
1859 void OPPROTO op_fsqrt(void)
1860 {
1861 helper_fsqrt();
1862 }
1863
1864 void OPPROTO op_fsincos(void)
1865 {
1866 helper_fsincos();
1867 }
1868
1869 void OPPROTO op_frndint(void)
1870 {
1871 helper_frndint();
1872 }
1873
1874 void OPPROTO op_fscale(void)
1875 {
1876 helper_fscale();
1877 }
1878
1879 void OPPROTO op_fsin(void)
1880 {
1881 helper_fsin();
1882 }
1883
1884 void OPPROTO op_fcos(void)
1885 {
1886 helper_fcos();
1887 }
1888
1889 void OPPROTO op_fnstsw_A0(void)
1890 {
1891 int fpus;
1892 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1893 stw((void *)A0, fpus);
1894 }
1895
1896 void OPPROTO op_fnstsw_EAX(void)
1897 {
1898 int fpus;
1899 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1900 EAX = (EAX & 0xffff0000) | fpus;
1901 }
1902
1903 void OPPROTO op_fnstcw_A0(void)
1904 {
1905 stw((void *)A0, env->fpuc);
1906 }
1907
1908 void OPPROTO op_fldcw_A0(void)
1909 {
1910 int rnd_type;
1911 env->fpuc = lduw((void *)A0);
1912 /* set rounding mode */
1913 switch(env->fpuc & RC_MASK) {
1914 default:
1915 case RC_NEAR:
1916 rnd_type = FE_TONEAREST;
1917 break;
1918 case RC_DOWN:
1919 rnd_type = FE_DOWNWARD;
1920 break;
1921 case RC_UP:
1922 rnd_type = FE_UPWARD;
1923 break;
1924 case RC_CHOP:
1925 rnd_type = FE_TOWARDZERO;
1926 break;
1927 }
1928 fesetround(rnd_type);
1929 }
1930
1931 void OPPROTO op_fclex(void)
1932 {
1933 env->fpus &= 0x7f00;
1934 }
1935
1936 void OPPROTO op_fninit(void)
1937 {
1938 env->fpus = 0;
1939 env->fpstt = 0;
1940 env->fpuc = 0x37f;
1941 env->fptags[0] = 1;
1942 env->fptags[1] = 1;
1943 env->fptags[2] = 1;
1944 env->fptags[3] = 1;
1945 env->fptags[4] = 1;
1946 env->fptags[5] = 1;
1947 env->fptags[6] = 1;
1948 env->fptags[7] = 1;
1949 }
1950
1951 void OPPROTO op_fnstenv_A0(void)
1952 {
1953 helper_fstenv((uint8_t *)A0, PARAM1);
1954 }
1955
1956 void OPPROTO op_fldenv_A0(void)
1957 {
1958 helper_fldenv((uint8_t *)A0, PARAM1);
1959 }
1960
1961 void OPPROTO op_fnsave_A0(void)
1962 {
1963 helper_fsave((uint8_t *)A0, PARAM1);
1964 }
1965
1966 void OPPROTO op_frstor_A0(void)
1967 {
1968 helper_frstor((uint8_t *)A0, PARAM1);
1969 }
1970
1971 /* threading support */
1972 void OPPROTO op_lock(void)
1973 {
1974 cpu_lock();
1975 }
1976
1977 void OPPROTO op_unlock(void)
1978 {
1979 cpu_unlock();
1980 }
1981