]> git.proxmox.com Git - qemu.git/blob - op-i386.c
update
[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 void OPPROTO op_rdmsr(void)
755 {
756 helper_rdmsr();
757 }
758
759 void OPPROTO op_wrmsr(void)
760 {
761 helper_wrmsr();
762 }
763
764 /* bcd */
765
766 /* XXX: exception */
767 void OPPROTO op_aam(void)
768 {
769 int base = PARAM1;
770 int al, ah;
771 al = EAX & 0xff;
772 ah = al / base;
773 al = al % base;
774 EAX = (EAX & ~0xffff) | al | (ah << 8);
775 CC_DST = al;
776 }
777
778 void OPPROTO op_aad(void)
779 {
780 int base = PARAM1;
781 int al, ah;
782 al = EAX & 0xff;
783 ah = (EAX >> 8) & 0xff;
784 al = ((ah * base) + al) & 0xff;
785 EAX = (EAX & ~0xffff) | al;
786 CC_DST = al;
787 }
788
789 void OPPROTO op_aaa(void)
790 {
791 int icarry;
792 int al, ah, af;
793 int eflags;
794
795 eflags = cc_table[CC_OP].compute_all();
796 af = eflags & CC_A;
797 al = EAX & 0xff;
798 ah = (EAX >> 8) & 0xff;
799
800 icarry = (al > 0xf9);
801 if (((al & 0x0f) > 9 ) || af) {
802 al = (al + 6) & 0x0f;
803 ah = (ah + 1 + icarry) & 0xff;
804 eflags |= CC_C | CC_A;
805 } else {
806 eflags &= ~(CC_C | CC_A);
807 al &= 0x0f;
808 }
809 EAX = (EAX & ~0xffff) | al | (ah << 8);
810 CC_SRC = eflags;
811 }
812
813 void OPPROTO op_aas(void)
814 {
815 int icarry;
816 int al, ah, af;
817 int eflags;
818
819 eflags = cc_table[CC_OP].compute_all();
820 af = eflags & CC_A;
821 al = EAX & 0xff;
822 ah = (EAX >> 8) & 0xff;
823
824 icarry = (al < 6);
825 if (((al & 0x0f) > 9 ) || af) {
826 al = (al - 6) & 0x0f;
827 ah = (ah - 1 - icarry) & 0xff;
828 eflags |= CC_C | CC_A;
829 } else {
830 eflags &= ~(CC_C | CC_A);
831 al &= 0x0f;
832 }
833 EAX = (EAX & ~0xffff) | al | (ah << 8);
834 CC_SRC = eflags;
835 }
836
837 void OPPROTO op_daa(void)
838 {
839 int al, af, cf;
840 int eflags;
841
842 eflags = cc_table[CC_OP].compute_all();
843 cf = eflags & CC_C;
844 af = eflags & CC_A;
845 al = EAX & 0xff;
846
847 eflags = 0;
848 if (((al & 0x0f) > 9 ) || af) {
849 al = (al + 6) & 0xff;
850 eflags |= CC_A;
851 }
852 if ((al > 0x9f) || cf) {
853 al = (al + 0x60) & 0xff;
854 eflags |= CC_C;
855 }
856 EAX = (EAX & ~0xff) | al;
857 /* well, speed is not an issue here, so we compute the flags by hand */
858 eflags |= (al == 0) << 6; /* zf */
859 eflags |= parity_table[al]; /* pf */
860 eflags |= (al & 0x80); /* sf */
861 CC_SRC = eflags;
862 }
863
864 void OPPROTO op_das(void)
865 {
866 int al, al1, af, cf;
867 int eflags;
868
869 eflags = cc_table[CC_OP].compute_all();
870 cf = eflags & CC_C;
871 af = eflags & CC_A;
872 al = EAX & 0xff;
873
874 eflags = 0;
875 al1 = al;
876 if (((al & 0x0f) > 9 ) || af) {
877 eflags |= CC_A;
878 if (al < 6 || cf)
879 eflags |= CC_C;
880 al = (al - 6) & 0xff;
881 }
882 if ((al1 > 0x99) || cf) {
883 al = (al - 0x60) & 0xff;
884 eflags |= CC_C;
885 }
886 EAX = (EAX & ~0xff) | al;
887 /* well, speed is not an issue here, so we compute the flags by hand */
888 eflags |= (al == 0) << 6; /* zf */
889 eflags |= parity_table[al]; /* pf */
890 eflags |= (al & 0x80); /* sf */
891 CC_SRC = eflags;
892 }
893
894 /* segment handling */
895
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 /* T0: segment, T1:eip */
940 void OPPROTO op_ljmp_T0_T1(void)
941 {
942 jmp_seg(T0 & 0xffff, T1);
943 }
944
945 void OPPROTO op_iret_protected(void)
946 {
947 helper_iret_protected(PARAM1);
948 }
949
950 void OPPROTO op_lldt_T0(void)
951 {
952 helper_lldt_T0();
953 }
954
955 void OPPROTO op_ltr_T0(void)
956 {
957 helper_ltr_T0();
958 }
959
960 /* CR registers access */
961 void OPPROTO op_movl_crN_T0(void)
962 {
963 helper_movl_crN_T0(PARAM1);
964 }
965
966 /* DR registers access */
967 void OPPROTO op_movl_drN_T0(void)
968 {
969 helper_movl_drN_T0(PARAM1);
970 }
971
972 void OPPROTO op_lmsw_T0(void)
973 {
974 /* only 4 lower bits of CR0 are modified */
975 T0 = (env->cr[0] & ~0xf) | (T0 & 0xf);
976 helper_movl_crN_T0(0);
977 }
978
979 void OPPROTO op_invlpg_A0(void)
980 {
981 helper_invlpg(A0);
982 }
983
984 void OPPROTO op_movl_T0_env(void)
985 {
986 T0 = *(uint32_t *)((char *)env + PARAM1);
987 }
988
989 void OPPROTO op_movl_env_T0(void)
990 {
991 *(uint32_t *)((char *)env + PARAM1) = T0;
992 }
993
994 void OPPROTO op_movl_env_T1(void)
995 {
996 *(uint32_t *)((char *)env + PARAM1) = T1;
997 }
998
999 void OPPROTO op_clts(void)
1000 {
1001 env->cr[0] &= ~CR0_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(PARAM1, 0, PARAM2);
1013 else
1014 JUMP_TB(PARAM1, 1, PARAM3);
1015 FORCE_RET();
1016 }
1017
1018 /* slow set cases (compute x86 flags) */
1019 void OPPROTO op_seto_T0_cc(void)
1020 {
1021 int eflags;
1022 eflags = cc_table[CC_OP].compute_all();
1023 T0 = (eflags >> 11) & 1;
1024 }
1025
1026 void OPPROTO op_setb_T0_cc(void)
1027 {
1028 T0 = cc_table[CC_OP].compute_c();
1029 }
1030
1031 void OPPROTO op_setz_T0_cc(void)
1032 {
1033 int eflags;
1034 eflags = cc_table[CC_OP].compute_all();
1035 T0 = (eflags >> 6) & 1;
1036 }
1037
1038 void OPPROTO op_setbe_T0_cc(void)
1039 {
1040 int eflags;
1041 eflags = cc_table[CC_OP].compute_all();
1042 T0 = (eflags & (CC_Z | CC_C)) != 0;
1043 }
1044
1045 void OPPROTO op_sets_T0_cc(void)
1046 {
1047 int eflags;
1048 eflags = cc_table[CC_OP].compute_all();
1049 T0 = (eflags >> 7) & 1;
1050 }
1051
1052 void OPPROTO op_setp_T0_cc(void)
1053 {
1054 int eflags;
1055 eflags = cc_table[CC_OP].compute_all();
1056 T0 = (eflags >> 2) & 1;
1057 }
1058
1059 void OPPROTO op_setl_T0_cc(void)
1060 {
1061 int eflags;
1062 eflags = cc_table[CC_OP].compute_all();
1063 T0 = ((eflags ^ (eflags >> 4)) >> 7) & 1;
1064 }
1065
1066 void OPPROTO op_setle_T0_cc(void)
1067 {
1068 int eflags;
1069 eflags = cc_table[CC_OP].compute_all();
1070 T0 = (((eflags ^ (eflags >> 4)) & 0x80) || (eflags & CC_Z)) != 0;
1071 }
1072
1073 void OPPROTO op_xor_T0_1(void)
1074 {
1075 T0 ^= 1;
1076 }
1077
1078 void OPPROTO op_set_cc_op(void)
1079 {
1080 CC_OP = PARAM1;
1081 }
1082
1083 #define FL_UPDATE_MASK16 (FL_UPDATE_MASK32 & 0xffff)
1084
1085 void OPPROTO op_movl_eflags_T0(void)
1086 {
1087 int eflags;
1088 eflags = T0;
1089 CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1090 DF = 1 - (2 * ((eflags >> 10) & 1));
1091 /* we also update some system flags as in user mode */
1092 env->eflags = (env->eflags & ~FL_UPDATE_MASK32) |
1093 (eflags & FL_UPDATE_MASK32);
1094 }
1095
1096 void OPPROTO op_movw_eflags_T0(void)
1097 {
1098 int eflags;
1099 eflags = T0;
1100 CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1101 DF = 1 - (2 * ((eflags >> 10) & 1));
1102 /* we also update some system flags as in user mode */
1103 env->eflags = (env->eflags & ~FL_UPDATE_MASK16) |
1104 (eflags & FL_UPDATE_MASK16);
1105 }
1106
1107 void OPPROTO op_movl_eflags_T0_cpl0(void)
1108 {
1109 load_eflags(T0, FL_UPDATE_CPL0_MASK);
1110 }
1111
1112 void OPPROTO op_movw_eflags_T0_cpl0(void)
1113 {
1114 load_eflags(T0, FL_UPDATE_CPL0_MASK & 0xffff);
1115 }
1116
1117 #if 0
1118 /* vm86plus version */
1119 void OPPROTO op_movw_eflags_T0_vm(void)
1120 {
1121 int eflags;
1122 eflags = T0;
1123 CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1124 DF = 1 - (2 * ((eflags >> 10) & 1));
1125 /* we also update some system flags as in user mode */
1126 env->eflags = (env->eflags & ~(FL_UPDATE_MASK16 | VIF_MASK)) |
1127 (eflags & FL_UPDATE_MASK16);
1128 if (eflags & IF_MASK) {
1129 env->eflags |= VIF_MASK;
1130 if (env->eflags & VIP_MASK) {
1131 EIP = PARAM1;
1132 raise_exception(EXCP0D_GPF);
1133 }
1134 }
1135 FORCE_RET();
1136 }
1137
1138 void OPPROTO op_movl_eflags_T0_vm(void)
1139 {
1140 int eflags;
1141 eflags = T0;
1142 CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1143 DF = 1 - (2 * ((eflags >> 10) & 1));
1144 /* we also update some system flags as in user mode */
1145 env->eflags = (env->eflags & ~(FL_UPDATE_MASK32 | VIF_MASK)) |
1146 (eflags & FL_UPDATE_MASK32);
1147 if (eflags & IF_MASK) {
1148 env->eflags |= VIF_MASK;
1149 if (env->eflags & VIP_MASK) {
1150 EIP = PARAM1;
1151 raise_exception(EXCP0D_GPF);
1152 }
1153 }
1154 FORCE_RET();
1155 }
1156 #endif
1157
1158 /* XXX: compute only O flag */
1159 void OPPROTO op_movb_eflags_T0(void)
1160 {
1161 int of;
1162 of = cc_table[CC_OP].compute_all() & CC_O;
1163 CC_SRC = (T0 & (CC_S | CC_Z | CC_A | CC_P | CC_C)) | of;
1164 }
1165
1166 void OPPROTO op_movl_T0_eflags(void)
1167 {
1168 int eflags;
1169 eflags = cc_table[CC_OP].compute_all();
1170 eflags |= (DF & DF_MASK);
1171 eflags |= env->eflags & ~(VM_MASK | RF_MASK);
1172 T0 = eflags;
1173 }
1174
1175 /* vm86plus version */
1176 #if 0
1177 void OPPROTO op_movl_T0_eflags_vm(void)
1178 {
1179 int eflags;
1180 eflags = cc_table[CC_OP].compute_all();
1181 eflags |= (DF & DF_MASK);
1182 eflags |= env->eflags & ~(VM_MASK | RF_MASK | IF_MASK);
1183 if (env->eflags & VIF_MASK)
1184 eflags |= IF_MASK;
1185 T0 = eflags;
1186 }
1187 #endif
1188
1189 void OPPROTO op_cld(void)
1190 {
1191 DF = 1;
1192 }
1193
1194 void OPPROTO op_std(void)
1195 {
1196 DF = -1;
1197 }
1198
1199 void OPPROTO op_clc(void)
1200 {
1201 int eflags;
1202 eflags = cc_table[CC_OP].compute_all();
1203 eflags &= ~CC_C;
1204 CC_SRC = eflags;
1205 }
1206
1207 void OPPROTO op_stc(void)
1208 {
1209 int eflags;
1210 eflags = cc_table[CC_OP].compute_all();
1211 eflags |= CC_C;
1212 CC_SRC = eflags;
1213 }
1214
1215 void OPPROTO op_cmc(void)
1216 {
1217 int eflags;
1218 eflags = cc_table[CC_OP].compute_all();
1219 eflags ^= CC_C;
1220 CC_SRC = eflags;
1221 }
1222
1223 void OPPROTO op_salc(void)
1224 {
1225 int cf;
1226 cf = cc_table[CC_OP].compute_c();
1227 EAX = (EAX & ~0xff) | ((-cf) & 0xff);
1228 }
1229
1230 static int compute_all_eflags(void)
1231 {
1232 return CC_SRC;
1233 }
1234
1235 static int compute_c_eflags(void)
1236 {
1237 return CC_SRC & CC_C;
1238 }
1239
1240 static int compute_c_mul(void)
1241 {
1242 int cf;
1243 cf = (CC_SRC != 0);
1244 return cf;
1245 }
1246
1247 static int compute_all_mul(void)
1248 {
1249 int cf, pf, af, zf, sf, of;
1250 cf = (CC_SRC != 0);
1251 pf = 0; /* undefined */
1252 af = 0; /* undefined */
1253 zf = 0; /* undefined */
1254 sf = 0; /* undefined */
1255 of = cf << 11;
1256 return cf | pf | af | zf | sf | of;
1257 }
1258
1259 CCTable cc_table[CC_OP_NB] = {
1260 [CC_OP_DYNAMIC] = { /* should never happen */ },
1261
1262 [CC_OP_EFLAGS] = { compute_all_eflags, compute_c_eflags },
1263
1264 [CC_OP_MUL] = { compute_all_mul, compute_c_mul },
1265
1266 [CC_OP_ADDB] = { compute_all_addb, compute_c_addb },
1267 [CC_OP_ADDW] = { compute_all_addw, compute_c_addw },
1268 [CC_OP_ADDL] = { compute_all_addl, compute_c_addl },
1269
1270 [CC_OP_ADCB] = { compute_all_adcb, compute_c_adcb },
1271 [CC_OP_ADCW] = { compute_all_adcw, compute_c_adcw },
1272 [CC_OP_ADCL] = { compute_all_adcl, compute_c_adcl },
1273
1274 [CC_OP_SUBB] = { compute_all_subb, compute_c_subb },
1275 [CC_OP_SUBW] = { compute_all_subw, compute_c_subw },
1276 [CC_OP_SUBL] = { compute_all_subl, compute_c_subl },
1277
1278 [CC_OP_SBBB] = { compute_all_sbbb, compute_c_sbbb },
1279 [CC_OP_SBBW] = { compute_all_sbbw, compute_c_sbbw },
1280 [CC_OP_SBBL] = { compute_all_sbbl, compute_c_sbbl },
1281
1282 [CC_OP_LOGICB] = { compute_all_logicb, compute_c_logicb },
1283 [CC_OP_LOGICW] = { compute_all_logicw, compute_c_logicw },
1284 [CC_OP_LOGICL] = { compute_all_logicl, compute_c_logicl },
1285
1286 [CC_OP_INCB] = { compute_all_incb, compute_c_incl },
1287 [CC_OP_INCW] = { compute_all_incw, compute_c_incl },
1288 [CC_OP_INCL] = { compute_all_incl, compute_c_incl },
1289
1290 [CC_OP_DECB] = { compute_all_decb, compute_c_incl },
1291 [CC_OP_DECW] = { compute_all_decw, compute_c_incl },
1292 [CC_OP_DECL] = { compute_all_decl, compute_c_incl },
1293
1294 [CC_OP_SHLB] = { compute_all_shlb, compute_c_shlb },
1295 [CC_OP_SHLW] = { compute_all_shlw, compute_c_shlw },
1296 [CC_OP_SHLL] = { compute_all_shll, compute_c_shll },
1297
1298 [CC_OP_SARB] = { compute_all_sarb, compute_c_sarl },
1299 [CC_OP_SARW] = { compute_all_sarw, compute_c_sarl },
1300 [CC_OP_SARL] = { compute_all_sarl, compute_c_sarl },
1301 };
1302
1303 /* floating point support. Some of the code for complicated x87
1304 functions comes from the LGPL'ed x86 emulator found in the Willows
1305 TWIN windows emulator. */
1306
1307 #if defined(__powerpc__)
1308 extern CPU86_LDouble copysign(CPU86_LDouble, CPU86_LDouble);
1309
1310 /* correct (but slow) PowerPC rint() (glibc version is incorrect) */
1311 double qemu_rint(double x)
1312 {
1313 double y = 4503599627370496.0;
1314 if (fabs(x) >= y)
1315 return x;
1316 if (x < 0)
1317 y = -y;
1318 y = (x + y) - y;
1319 if (y == 0.0)
1320 y = copysign(y, x);
1321 return y;
1322 }
1323
1324 #define rint qemu_rint
1325 #endif
1326
1327 /* fp load FT0 */
1328
1329 void OPPROTO op_flds_FT0_A0(void)
1330 {
1331 #ifdef USE_FP_CONVERT
1332 FP_CONVERT.i32 = ldl((void *)A0);
1333 FT0 = FP_CONVERT.f;
1334 #else
1335 FT0 = ldfl((void *)A0);
1336 #endif
1337 }
1338
1339 void OPPROTO op_fldl_FT0_A0(void)
1340 {
1341 #ifdef USE_FP_CONVERT
1342 FP_CONVERT.i64 = ldq((void *)A0);
1343 FT0 = FP_CONVERT.d;
1344 #else
1345 FT0 = ldfq((void *)A0);
1346 #endif
1347 }
1348
1349 /* helpers are needed to avoid static constant reference. XXX: find a better way */
1350 #ifdef USE_INT_TO_FLOAT_HELPERS
1351
1352 void helper_fild_FT0_A0(void)
1353 {
1354 FT0 = (CPU86_LDouble)ldsw((void *)A0);
1355 }
1356
1357 void helper_fildl_FT0_A0(void)
1358 {
1359 FT0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1360 }
1361
1362 void helper_fildll_FT0_A0(void)
1363 {
1364 FT0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1365 }
1366
1367 void OPPROTO op_fild_FT0_A0(void)
1368 {
1369 helper_fild_FT0_A0();
1370 }
1371
1372 void OPPROTO op_fildl_FT0_A0(void)
1373 {
1374 helper_fildl_FT0_A0();
1375 }
1376
1377 void OPPROTO op_fildll_FT0_A0(void)
1378 {
1379 helper_fildll_FT0_A0();
1380 }
1381
1382 #else
1383
1384 void OPPROTO op_fild_FT0_A0(void)
1385 {
1386 #ifdef USE_FP_CONVERT
1387 FP_CONVERT.i32 = ldsw((void *)A0);
1388 FT0 = (CPU86_LDouble)FP_CONVERT.i32;
1389 #else
1390 FT0 = (CPU86_LDouble)ldsw((void *)A0);
1391 #endif
1392 }
1393
1394 void OPPROTO op_fildl_FT0_A0(void)
1395 {
1396 #ifdef USE_FP_CONVERT
1397 FP_CONVERT.i32 = (int32_t) ldl((void *)A0);
1398 FT0 = (CPU86_LDouble)FP_CONVERT.i32;
1399 #else
1400 FT0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1401 #endif
1402 }
1403
1404 void OPPROTO op_fildll_FT0_A0(void)
1405 {
1406 #ifdef USE_FP_CONVERT
1407 FP_CONVERT.i64 = (int64_t) ldq((void *)A0);
1408 FT0 = (CPU86_LDouble)FP_CONVERT.i64;
1409 #else
1410 FT0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1411 #endif
1412 }
1413 #endif
1414
1415 /* fp load ST0 */
1416
1417 void OPPROTO op_flds_ST0_A0(void)
1418 {
1419 int new_fpstt;
1420 new_fpstt = (env->fpstt - 1) & 7;
1421 #ifdef USE_FP_CONVERT
1422 FP_CONVERT.i32 = ldl((void *)A0);
1423 env->fpregs[new_fpstt] = FP_CONVERT.f;
1424 #else
1425 env->fpregs[new_fpstt] = ldfl((void *)A0);
1426 #endif
1427 env->fpstt = new_fpstt;
1428 env->fptags[new_fpstt] = 0; /* validate stack entry */
1429 }
1430
1431 void OPPROTO op_fldl_ST0_A0(void)
1432 {
1433 int new_fpstt;
1434 new_fpstt = (env->fpstt - 1) & 7;
1435 #ifdef USE_FP_CONVERT
1436 FP_CONVERT.i64 = ldq((void *)A0);
1437 env->fpregs[new_fpstt] = FP_CONVERT.d;
1438 #else
1439 env->fpregs[new_fpstt] = ldfq((void *)A0);
1440 #endif
1441 env->fpstt = new_fpstt;
1442 env->fptags[new_fpstt] = 0; /* validate stack entry */
1443 }
1444
1445 #ifdef USE_X86LDOUBLE
1446 void OPPROTO op_fldt_ST0_A0(void)
1447 {
1448 int new_fpstt;
1449 new_fpstt = (env->fpstt - 1) & 7;
1450 env->fpregs[new_fpstt] = *(long double *)A0;
1451 env->fpstt = new_fpstt;
1452 env->fptags[new_fpstt] = 0; /* validate stack entry */
1453 }
1454 #else
1455 void OPPROTO op_fldt_ST0_A0(void)
1456 {
1457 helper_fldt_ST0_A0();
1458 }
1459 #endif
1460
1461 /* helpers are needed to avoid static constant reference. XXX: find a better way */
1462 #ifdef USE_INT_TO_FLOAT_HELPERS
1463
1464 void helper_fild_ST0_A0(void)
1465 {
1466 int new_fpstt;
1467 new_fpstt = (env->fpstt - 1) & 7;
1468 env->fpregs[new_fpstt] = (CPU86_LDouble)ldsw((void *)A0);
1469 env->fpstt = new_fpstt;
1470 env->fptags[new_fpstt] = 0; /* validate stack entry */
1471 }
1472
1473 void helper_fildl_ST0_A0(void)
1474 {
1475 int new_fpstt;
1476 new_fpstt = (env->fpstt - 1) & 7;
1477 env->fpregs[new_fpstt] = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1478 env->fpstt = new_fpstt;
1479 env->fptags[new_fpstt] = 0; /* validate stack entry */
1480 }
1481
1482 void helper_fildll_ST0_A0(void)
1483 {
1484 int new_fpstt;
1485 new_fpstt = (env->fpstt - 1) & 7;
1486 env->fpregs[new_fpstt] = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1487 env->fpstt = new_fpstt;
1488 env->fptags[new_fpstt] = 0; /* validate stack entry */
1489 }
1490
1491 void OPPROTO op_fild_ST0_A0(void)
1492 {
1493 helper_fild_ST0_A0();
1494 }
1495
1496 void OPPROTO op_fildl_ST0_A0(void)
1497 {
1498 helper_fildl_ST0_A0();
1499 }
1500
1501 void OPPROTO op_fildll_ST0_A0(void)
1502 {
1503 helper_fildll_ST0_A0();
1504 }
1505
1506 #else
1507
1508 void OPPROTO op_fild_ST0_A0(void)
1509 {
1510 int new_fpstt;
1511 new_fpstt = (env->fpstt - 1) & 7;
1512 #ifdef USE_FP_CONVERT
1513 FP_CONVERT.i32 = ldsw((void *)A0);
1514 env->fpregs[new_fpstt] = (CPU86_LDouble)FP_CONVERT.i32;
1515 #else
1516 env->fpregs[new_fpstt] = (CPU86_LDouble)ldsw((void *)A0);
1517 #endif
1518 env->fpstt = new_fpstt;
1519 env->fptags[new_fpstt] = 0; /* validate stack entry */
1520 }
1521
1522 void OPPROTO op_fildl_ST0_A0(void)
1523 {
1524 int new_fpstt;
1525 new_fpstt = (env->fpstt - 1) & 7;
1526 #ifdef USE_FP_CONVERT
1527 FP_CONVERT.i32 = (int32_t) ldl((void *)A0);
1528 env->fpregs[new_fpstt] = (CPU86_LDouble)FP_CONVERT.i32;
1529 #else
1530 env->fpregs[new_fpstt] = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1531 #endif
1532 env->fpstt = new_fpstt;
1533 env->fptags[new_fpstt] = 0; /* validate stack entry */
1534 }
1535
1536 void OPPROTO op_fildll_ST0_A0(void)
1537 {
1538 int new_fpstt;
1539 new_fpstt = (env->fpstt - 1) & 7;
1540 #ifdef USE_FP_CONVERT
1541 FP_CONVERT.i64 = (int64_t) ldq((void *)A0);
1542 env->fpregs[new_fpstt] = (CPU86_LDouble)FP_CONVERT.i64;
1543 #else
1544 env->fpregs[new_fpstt] = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1545 #endif
1546 env->fpstt = new_fpstt;
1547 env->fptags[new_fpstt] = 0; /* validate stack entry */
1548 }
1549
1550 #endif
1551
1552 /* fp store */
1553
1554 void OPPROTO op_fsts_ST0_A0(void)
1555 {
1556 #ifdef USE_FP_CONVERT
1557 FP_CONVERT.f = (float)ST0;
1558 stfl((void *)A0, FP_CONVERT.f);
1559 #else
1560 stfl((void *)A0, (float)ST0);
1561 #endif
1562 }
1563
1564 void OPPROTO op_fstl_ST0_A0(void)
1565 {
1566 stfq((void *)A0, (double)ST0);
1567 }
1568
1569 #ifdef USE_X86LDOUBLE
1570 void OPPROTO op_fstt_ST0_A0(void)
1571 {
1572 *(long double *)A0 = ST0;
1573 }
1574 #else
1575 void OPPROTO op_fstt_ST0_A0(void)
1576 {
1577 helper_fstt_ST0_A0();
1578 }
1579 #endif
1580
1581 void OPPROTO op_fist_ST0_A0(void)
1582 {
1583 #if defined(__sparc__) && !defined(__sparc_v9__)
1584 register CPU86_LDouble d asm("o0");
1585 #else
1586 CPU86_LDouble d;
1587 #endif
1588 int val;
1589
1590 d = ST0;
1591 val = lrint(d);
1592 if (val != (int16_t)val)
1593 val = -32768;
1594 stw((void *)A0, val);
1595 }
1596
1597 void OPPROTO op_fistl_ST0_A0(void)
1598 {
1599 #if defined(__sparc__) && !defined(__sparc_v9__)
1600 register CPU86_LDouble d asm("o0");
1601 #else
1602 CPU86_LDouble d;
1603 #endif
1604 int val;
1605
1606 d = ST0;
1607 val = lrint(d);
1608 stl((void *)A0, val);
1609 }
1610
1611 void OPPROTO op_fistll_ST0_A0(void)
1612 {
1613 #if defined(__sparc__) && !defined(__sparc_v9__)
1614 register CPU86_LDouble d asm("o0");
1615 #else
1616 CPU86_LDouble d;
1617 #endif
1618 int64_t val;
1619
1620 d = ST0;
1621 val = llrint(d);
1622 stq((void *)A0, val);
1623 }
1624
1625 void OPPROTO op_fbld_ST0_A0(void)
1626 {
1627 helper_fbld_ST0_A0();
1628 }
1629
1630 void OPPROTO op_fbst_ST0_A0(void)
1631 {
1632 helper_fbst_ST0_A0();
1633 }
1634
1635 /* FPU move */
1636
1637 void OPPROTO op_fpush(void)
1638 {
1639 fpush();
1640 }
1641
1642 void OPPROTO op_fpop(void)
1643 {
1644 fpop();
1645 }
1646
1647 void OPPROTO op_fdecstp(void)
1648 {
1649 env->fpstt = (env->fpstt - 1) & 7;
1650 env->fpus &= (~0x4700);
1651 }
1652
1653 void OPPROTO op_fincstp(void)
1654 {
1655 env->fpstt = (env->fpstt + 1) & 7;
1656 env->fpus &= (~0x4700);
1657 }
1658
1659 void OPPROTO op_fmov_ST0_FT0(void)
1660 {
1661 ST0 = FT0;
1662 }
1663
1664 void OPPROTO op_fmov_FT0_STN(void)
1665 {
1666 FT0 = ST(PARAM1);
1667 }
1668
1669 void OPPROTO op_fmov_ST0_STN(void)
1670 {
1671 ST0 = ST(PARAM1);
1672 }
1673
1674 void OPPROTO op_fmov_STN_ST0(void)
1675 {
1676 ST(PARAM1) = ST0;
1677 }
1678
1679 void OPPROTO op_fxchg_ST0_STN(void)
1680 {
1681 CPU86_LDouble tmp;
1682 tmp = ST(PARAM1);
1683 ST(PARAM1) = ST0;
1684 ST0 = tmp;
1685 }
1686
1687 /* FPU operations */
1688
1689 /* XXX: handle nans */
1690 void OPPROTO op_fcom_ST0_FT0(void)
1691 {
1692 env->fpus &= (~0x4500); /* (C3,C2,C0) <-- 000 */
1693 if (ST0 < FT0)
1694 env->fpus |= 0x100; /* (C3,C2,C0) <-- 001 */
1695 else if (ST0 == FT0)
1696 env->fpus |= 0x4000; /* (C3,C2,C0) <-- 100 */
1697 FORCE_RET();
1698 }
1699
1700 /* XXX: handle nans */
1701 void OPPROTO op_fucom_ST0_FT0(void)
1702 {
1703 env->fpus &= (~0x4500); /* (C3,C2,C0) <-- 000 */
1704 if (ST0 < FT0)
1705 env->fpus |= 0x100; /* (C3,C2,C0) <-- 001 */
1706 else if (ST0 == FT0)
1707 env->fpus |= 0x4000; /* (C3,C2,C0) <-- 100 */
1708 FORCE_RET();
1709 }
1710
1711 /* XXX: handle nans */
1712 void OPPROTO op_fcomi_ST0_FT0(void)
1713 {
1714 int eflags;
1715 eflags = cc_table[CC_OP].compute_all();
1716 eflags &= ~(CC_Z | CC_P | CC_C);
1717 if (ST0 < FT0)
1718 eflags |= CC_C;
1719 else if (ST0 == FT0)
1720 eflags |= CC_Z;
1721 CC_SRC = eflags;
1722 FORCE_RET();
1723 }
1724
1725 /* XXX: handle nans */
1726 void OPPROTO op_fucomi_ST0_FT0(void)
1727 {
1728 int eflags;
1729 eflags = cc_table[CC_OP].compute_all();
1730 eflags &= ~(CC_Z | CC_P | CC_C);
1731 if (ST0 < FT0)
1732 eflags |= CC_C;
1733 else if (ST0 == FT0)
1734 eflags |= CC_Z;
1735 CC_SRC = eflags;
1736 FORCE_RET();
1737 }
1738
1739 void OPPROTO op_fadd_ST0_FT0(void)
1740 {
1741 ST0 += FT0;
1742 }
1743
1744 void OPPROTO op_fmul_ST0_FT0(void)
1745 {
1746 ST0 *= FT0;
1747 }
1748
1749 void OPPROTO op_fsub_ST0_FT0(void)
1750 {
1751 ST0 -= FT0;
1752 }
1753
1754 void OPPROTO op_fsubr_ST0_FT0(void)
1755 {
1756 ST0 = FT0 - ST0;
1757 }
1758
1759 void OPPROTO op_fdiv_ST0_FT0(void)
1760 {
1761 ST0 /= FT0;
1762 }
1763
1764 void OPPROTO op_fdivr_ST0_FT0(void)
1765 {
1766 ST0 = FT0 / ST0;
1767 }
1768
1769 /* fp operations between STN and ST0 */
1770
1771 void OPPROTO op_fadd_STN_ST0(void)
1772 {
1773 ST(PARAM1) += ST0;
1774 }
1775
1776 void OPPROTO op_fmul_STN_ST0(void)
1777 {
1778 ST(PARAM1) *= ST0;
1779 }
1780
1781 void OPPROTO op_fsub_STN_ST0(void)
1782 {
1783 ST(PARAM1) -= ST0;
1784 }
1785
1786 void OPPROTO op_fsubr_STN_ST0(void)
1787 {
1788 CPU86_LDouble *p;
1789 p = &ST(PARAM1);
1790 *p = ST0 - *p;
1791 }
1792
1793 void OPPROTO op_fdiv_STN_ST0(void)
1794 {
1795 ST(PARAM1) /= ST0;
1796 }
1797
1798 void OPPROTO op_fdivr_STN_ST0(void)
1799 {
1800 CPU86_LDouble *p;
1801 p = &ST(PARAM1);
1802 *p = ST0 / *p;
1803 }
1804
1805 /* misc FPU operations */
1806 void OPPROTO op_fchs_ST0(void)
1807 {
1808 ST0 = -ST0;
1809 }
1810
1811 void OPPROTO op_fabs_ST0(void)
1812 {
1813 ST0 = fabs(ST0);
1814 }
1815
1816 void OPPROTO op_fxam_ST0(void)
1817 {
1818 helper_fxam_ST0();
1819 }
1820
1821 void OPPROTO op_fld1_ST0(void)
1822 {
1823 ST0 = f15rk[1];
1824 }
1825
1826 void OPPROTO op_fldl2t_ST0(void)
1827 {
1828 ST0 = f15rk[6];
1829 }
1830
1831 void OPPROTO op_fldl2e_ST0(void)
1832 {
1833 ST0 = f15rk[5];
1834 }
1835
1836 void OPPROTO op_fldpi_ST0(void)
1837 {
1838 ST0 = f15rk[2];
1839 }
1840
1841 void OPPROTO op_fldlg2_ST0(void)
1842 {
1843 ST0 = f15rk[3];
1844 }
1845
1846 void OPPROTO op_fldln2_ST0(void)
1847 {
1848 ST0 = f15rk[4];
1849 }
1850
1851 void OPPROTO op_fldz_ST0(void)
1852 {
1853 ST0 = f15rk[0];
1854 }
1855
1856 void OPPROTO op_fldz_FT0(void)
1857 {
1858 ST0 = f15rk[0];
1859 }
1860
1861 /* associated heplers to reduce generated code length and to simplify
1862 relocation (FP constants are usually stored in .rodata section) */
1863
1864 void OPPROTO op_f2xm1(void)
1865 {
1866 helper_f2xm1();
1867 }
1868
1869 void OPPROTO op_fyl2x(void)
1870 {
1871 helper_fyl2x();
1872 }
1873
1874 void OPPROTO op_fptan(void)
1875 {
1876 helper_fptan();
1877 }
1878
1879 void OPPROTO op_fpatan(void)
1880 {
1881 helper_fpatan();
1882 }
1883
1884 void OPPROTO op_fxtract(void)
1885 {
1886 helper_fxtract();
1887 }
1888
1889 void OPPROTO op_fprem1(void)
1890 {
1891 helper_fprem1();
1892 }
1893
1894
1895 void OPPROTO op_fprem(void)
1896 {
1897 helper_fprem();
1898 }
1899
1900 void OPPROTO op_fyl2xp1(void)
1901 {
1902 helper_fyl2xp1();
1903 }
1904
1905 void OPPROTO op_fsqrt(void)
1906 {
1907 helper_fsqrt();
1908 }
1909
1910 void OPPROTO op_fsincos(void)
1911 {
1912 helper_fsincos();
1913 }
1914
1915 void OPPROTO op_frndint(void)
1916 {
1917 helper_frndint();
1918 }
1919
1920 void OPPROTO op_fscale(void)
1921 {
1922 helper_fscale();
1923 }
1924
1925 void OPPROTO op_fsin(void)
1926 {
1927 helper_fsin();
1928 }
1929
1930 void OPPROTO op_fcos(void)
1931 {
1932 helper_fcos();
1933 }
1934
1935 void OPPROTO op_fnstsw_A0(void)
1936 {
1937 int fpus;
1938 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1939 stw((void *)A0, fpus);
1940 }
1941
1942 void OPPROTO op_fnstsw_EAX(void)
1943 {
1944 int fpus;
1945 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1946 EAX = (EAX & 0xffff0000) | fpus;
1947 }
1948
1949 void OPPROTO op_fnstcw_A0(void)
1950 {
1951 stw((void *)A0, env->fpuc);
1952 }
1953
1954 void OPPROTO op_fldcw_A0(void)
1955 {
1956 int rnd_type;
1957 env->fpuc = lduw((void *)A0);
1958 /* set rounding mode */
1959 switch(env->fpuc & RC_MASK) {
1960 default:
1961 case RC_NEAR:
1962 rnd_type = FE_TONEAREST;
1963 break;
1964 case RC_DOWN:
1965 rnd_type = FE_DOWNWARD;
1966 break;
1967 case RC_UP:
1968 rnd_type = FE_UPWARD;
1969 break;
1970 case RC_CHOP:
1971 rnd_type = FE_TOWARDZERO;
1972 break;
1973 }
1974 fesetround(rnd_type);
1975 }
1976
1977 void OPPROTO op_fclex(void)
1978 {
1979 env->fpus &= 0x7f00;
1980 }
1981
1982 void OPPROTO op_fninit(void)
1983 {
1984 env->fpus = 0;
1985 env->fpstt = 0;
1986 env->fpuc = 0x37f;
1987 env->fptags[0] = 1;
1988 env->fptags[1] = 1;
1989 env->fptags[2] = 1;
1990 env->fptags[3] = 1;
1991 env->fptags[4] = 1;
1992 env->fptags[5] = 1;
1993 env->fptags[6] = 1;
1994 env->fptags[7] = 1;
1995 }
1996
1997 void OPPROTO op_fnstenv_A0(void)
1998 {
1999 helper_fstenv((uint8_t *)A0, PARAM1);
2000 }
2001
2002 void OPPROTO op_fldenv_A0(void)
2003 {
2004 helper_fldenv((uint8_t *)A0, PARAM1);
2005 }
2006
2007 void OPPROTO op_fnsave_A0(void)
2008 {
2009 helper_fsave((uint8_t *)A0, PARAM1);
2010 }
2011
2012 void OPPROTO op_frstor_A0(void)
2013 {
2014 helper_frstor((uint8_t *)A0, PARAM1);
2015 }
2016
2017 /* threading support */
2018 void OPPROTO op_lock(void)
2019 {
2020 cpu_lock();
2021 }
2022
2023 void OPPROTO op_unlock(void)
2024 {
2025 cpu_unlock();
2026 }
2027