2 * i386 micro operations
4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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
20 #include "exec-i386.h"
22 uint8_t parity_table[256] = {
23 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
24 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
25 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
26 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
27 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
28 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
29 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
30 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
31 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
32 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
33 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
34 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
35 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
36 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
37 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
38 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
39 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
40 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
41 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
42 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
43 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
44 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
45 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
46 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
47 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
48 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
49 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
50 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
51 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
52 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
53 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
54 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
58 const uint8_t rclw_table[32] = {
59 0, 1, 2, 3, 4, 5, 6, 7,
60 8, 9,10,11,12,13,14,15,
61 16, 0, 1, 2, 3, 4, 5, 6,
62 7, 8, 9,10,11,12,13,14,
66 const uint8_t rclb_table[32] = {
67 0, 1, 2, 3, 4, 5, 6, 7,
68 8, 0, 1, 2, 3, 4, 5, 6,
69 7, 8, 0, 1, 2, 3, 4, 5,
70 6, 7, 8, 0, 1, 2, 3, 4,
73 /* n must be a constant to be efficient */
74 static inline int lshift(int x, int n)
82 /* we define the various pieces of code used by the JIT */
86 #include "opreg_template.h"
92 #include "opreg_template.h"
98 #include "opreg_template.h"
104 #include "opreg_template.h"
110 #include "opreg_template.h"
116 #include "opreg_template.h"
122 #include "opreg_template.h"
128 #include "opreg_template.h"
132 /* operations with flags */
134 void OPPROTO op_addl_T0_T1_cc(void)
141 void OPPROTO op_orl_T0_T1_cc(void)
147 void OPPROTO op_andl_T0_T1_cc(void)
153 void OPPROTO op_subl_T0_T1_cc(void)
160 void OPPROTO op_xorl_T0_T1_cc(void)
166 void OPPROTO op_cmpl_T0_T1_cc(void)
172 void OPPROTO op_negl_T0_cc(void)
179 void OPPROTO op_incl_T0_cc(void)
181 CC_SRC = cc_table[CC_OP].compute_c();
186 void OPPROTO op_decl_T0_cc(void)
188 CC_SRC = cc_table[CC_OP].compute_c();
193 void OPPROTO op_testl_T0_T1_cc(void)
198 /* operations without flags */
200 void OPPROTO op_addl_T0_T1(void)
205 void OPPROTO op_orl_T0_T1(void)
210 void OPPROTO op_andl_T0_T1(void)
215 void OPPROTO op_subl_T0_T1(void)
220 void OPPROTO op_xorl_T0_T1(void)
225 void OPPROTO op_negl_T0(void)
230 void OPPROTO op_incl_T0(void)
235 void OPPROTO op_decl_T0(void)
240 void OPPROTO op_notl_T0(void)
245 void OPPROTO op_bswapl_T0(void)
250 /* multiply/divide */
251 void OPPROTO op_mulb_AL_T0(void)
254 res = (uint8_t)EAX * (uint8_t)T0;
255 EAX = (EAX & 0xffff0000) | res;
256 CC_SRC = (res & 0xff00);
259 void OPPROTO op_imulb_AL_T0(void)
262 res = (int8_t)EAX * (int8_t)T0;
263 EAX = (EAX & 0xffff0000) | (res & 0xffff);
264 CC_SRC = (res != (int8_t)res);
267 void OPPROTO op_mulw_AX_T0(void)
270 res = (uint16_t)EAX * (uint16_t)T0;
271 EAX = (EAX & 0xffff0000) | (res & 0xffff);
272 EDX = (EDX & 0xffff0000) | ((res >> 16) & 0xffff);
276 void OPPROTO op_imulw_AX_T0(void)
279 res = (int16_t)EAX * (int16_t)T0;
280 EAX = (EAX & 0xffff0000) | (res & 0xffff);
281 EDX = (EDX & 0xffff0000) | ((res >> 16) & 0xffff);
282 CC_SRC = (res != (int16_t)res);
285 void OPPROTO op_mull_EAX_T0(void)
288 res = (uint64_t)((uint32_t)EAX) * (uint64_t)((uint32_t)T0);
294 void OPPROTO op_imull_EAX_T0(void)
297 res = (int64_t)((int32_t)EAX) * (int64_t)((int32_t)T0);
300 CC_SRC = (res != (int32_t)res);
303 void OPPROTO op_imulw_T0_T1(void)
306 res = (int16_t)T0 * (int16_t)T1;
308 CC_SRC = (res != (int16_t)res);
311 void OPPROTO op_imull_T0_T1(void)
314 res = (int64_t)((int32_t)T0) * (int64_t)((int32_t)T1);
316 CC_SRC = (res != (int32_t)res);
319 /* division, flags are undefined */
320 /* XXX: add exceptions for overflow */
322 void OPPROTO op_divb_AL_T0(void)
324 unsigned int num, den, q, r;
326 num = (EAX & 0xffff);
330 raise_exception(EXCP00_DIVZ);
332 q = (num / den) & 0xff;
333 r = (num % den) & 0xff;
334 EAX = (EAX & 0xffff0000) | (r << 8) | q;
337 void OPPROTO op_idivb_AL_T0(void)
345 raise_exception(EXCP00_DIVZ);
347 q = (num / den) & 0xff;
348 r = (num % den) & 0xff;
349 EAX = (EAX & 0xffff0000) | (r << 8) | q;
352 void OPPROTO op_divw_AX_T0(void)
354 unsigned int num, den, q, r;
356 num = (EAX & 0xffff) | ((EDX & 0xffff) << 16);
360 raise_exception(EXCP00_DIVZ);
362 q = (num / den) & 0xffff;
363 r = (num % den) & 0xffff;
364 EAX = (EAX & 0xffff0000) | q;
365 EDX = (EDX & 0xffff0000) | r;
368 void OPPROTO op_idivw_AX_T0(void)
372 num = (EAX & 0xffff) | ((EDX & 0xffff) << 16);
376 raise_exception(EXCP00_DIVZ);
378 q = (num / den) & 0xffff;
379 r = (num % den) & 0xffff;
380 EAX = (EAX & 0xffff0000) | q;
381 EDX = (EDX & 0xffff0000) | r;
384 void OPPROTO op_divl_EAX_T0(void)
386 helper_divl_EAX_T0(PARAM1);
389 void OPPROTO op_idivl_EAX_T0(void)
391 helper_idivl_EAX_T0(PARAM1);
394 /* constant load & misc op */
396 void OPPROTO op_movl_T0_im(void)
401 void OPPROTO op_addl_T0_im(void)
406 void OPPROTO op_andl_T0_ffff(void)
411 void OPPROTO op_movl_T0_T1(void)
416 void OPPROTO op_movl_T1_im(void)
421 void OPPROTO op_addl_T1_im(void)
426 void OPPROTO op_movl_T1_A0(void)
431 void OPPROTO op_movl_A0_im(void)
436 void OPPROTO op_addl_A0_im(void)
441 void OPPROTO op_addl_A0_AL(void)
446 void OPPROTO op_andl_A0_ffff(void)
453 void OPPROTO op_ldub_T0_A0(void)
455 T0 = ldub((uint8_t *)A0);
458 void OPPROTO op_ldsb_T0_A0(void)
460 T0 = ldsb((int8_t *)A0);
463 void OPPROTO op_lduw_T0_A0(void)
465 T0 = lduw((uint8_t *)A0);
468 void OPPROTO op_ldsw_T0_A0(void)
470 T0 = ldsw((int8_t *)A0);
473 void OPPROTO op_ldl_T0_A0(void)
475 T0 = ldl((uint8_t *)A0);
478 void OPPROTO op_ldub_T1_A0(void)
480 T1 = ldub((uint8_t *)A0);
483 void OPPROTO op_ldsb_T1_A0(void)
485 T1 = ldsb((int8_t *)A0);
488 void OPPROTO op_lduw_T1_A0(void)
490 T1 = lduw((uint8_t *)A0);
493 void OPPROTO op_ldsw_T1_A0(void)
495 T1 = ldsw((int8_t *)A0);
498 void OPPROTO op_ldl_T1_A0(void)
500 T1 = ldl((uint8_t *)A0);
503 void OPPROTO op_stb_T0_A0(void)
505 stb((uint8_t *)A0, T0);
508 void OPPROTO op_stw_T0_A0(void)
510 stw((uint8_t *)A0, T0);
513 void OPPROTO op_stl_T0_A0(void)
515 stl((uint8_t *)A0, T0);
518 /* used for bit operations */
520 void OPPROTO op_add_bitw_A0_T1(void)
522 A0 += ((int32_t)T1 >> 4) << 1;
525 void OPPROTO op_add_bitl_A0_T1(void)
527 A0 += ((int32_t)T1 >> 5) << 2;
532 void OPPROTO op_jmp_T0(void)
537 void OPPROTO op_jmp_im(void)
542 void OPPROTO op_raise_interrupt(void)
545 unsigned int next_eip;
548 raise_interrupt(intno, 1, 0, next_eip);
551 void OPPROTO op_raise_exception(void)
554 exception_index = PARAM1;
555 raise_exception(exception_index);
558 void OPPROTO op_into(void)
561 eflags = cc_table[CC_OP].compute_all();
563 raise_interrupt(EXCP04_INTO, 1, 0, PARAM1);
568 void OPPROTO op_cli(void)
570 env->eflags &= ~IF_MASK;
573 void OPPROTO op_sti(void)
575 env->eflags |= IF_MASK;
579 /* vm86plus instructions */
580 void OPPROTO op_cli_vm(void)
582 env->eflags &= ~VIF_MASK;
585 void OPPROTO op_sti_vm(void)
587 env->eflags |= VIF_MASK;
588 if (env->eflags & VIP_MASK) {
590 raise_exception(EXCP0D_GPF);
596 void OPPROTO op_boundw(void)
599 low = ldsw((uint8_t *)A0);
600 high = ldsw((uint8_t *)A0 + 2);
602 if (v < low || v > high) {
604 raise_exception(EXCP05_BOUND);
609 void OPPROTO op_boundl(void)
612 low = ldl((uint8_t *)A0);
613 high = ldl((uint8_t *)A0 + 4);
615 if (v < low || v > high) {
617 raise_exception(EXCP05_BOUND);
622 void OPPROTO op_cmpxchg8b(void)
627 #if defined(__powerpc__)
629 /* on PowerPC we patch the jump instruction directly */
630 #define JUMP_TB(tbparam, n, eip)\
632 static void __attribute__((unused)) *__op_label ## n = &&label ## n;\
633 asm volatile ("b %0" : : "i" (&__op_jmp ## n));\
635 T0 = (long)(tbparam) + (n);\
641 /* jump to next block operations (more portable code, does not need
642 cache flushing, but slower because of indirect jump) */
643 #define JUMP_TB(tbparam, n, eip)\
645 static void __attribute__((unused)) *__op_label ## n = &&label ## n;\
646 goto *(void *)(((TranslationBlock *)tbparam)->tb_next[n]);\
648 T0 = (long)(tbparam) + (n);\
654 void OPPROTO op_jmp_tb_next(void)
656 JUMP_TB(PARAM1, 0, PARAM2);
659 void OPPROTO op_movl_T0_0(void)
664 /* multiple size ops */
669 #include "ops_template.h"
673 #include "ops_template.h"
677 #include "ops_template.h"
682 void OPPROTO op_movsbl_T0_T0(void)
687 void OPPROTO op_movzbl_T0_T0(void)
692 void OPPROTO op_movswl_T0_T0(void)
697 void OPPROTO op_movzwl_T0_T0(void)
702 void OPPROTO op_movswl_EAX_AX(void)
707 void OPPROTO op_movsbw_AX_AL(void)
709 EAX = (EAX & 0xffff0000) | ((int8_t)EAX & 0xffff);
712 void OPPROTO op_movslq_EDX_EAX(void)
714 EDX = (int32_t)EAX >> 31;
717 void OPPROTO op_movswl_DX_AX(void)
719 EDX = (EDX & 0xffff0000) | (((int16_t)EAX >> 15) & 0xffff);
724 void op_pushl_T0(void)
728 stl((void *)offset, T0);
729 /* modify ESP after to handle exceptions correctly */
733 void op_pushw_T0(void)
737 stw((void *)offset, T0);
738 /* modify ESP after to handle exceptions correctly */
742 void op_pushl_ss32_T0(void)
746 stl(env->seg_cache[R_SS].base + offset, T0);
747 /* modify ESP after to handle exceptions correctly */
751 void op_pushw_ss32_T0(void)
755 stw(env->seg_cache[R_SS].base + offset, T0);
756 /* modify ESP after to handle exceptions correctly */
760 void op_pushl_ss16_T0(void)
763 offset = (ESP - 4) & 0xffff;
764 stl(env->seg_cache[R_SS].base + offset, T0);
765 /* modify ESP after to handle exceptions correctly */
766 ESP = (ESP & ~0xffff) | offset;
769 void op_pushw_ss16_T0(void)
772 offset = (ESP - 2) & 0xffff;
773 stw(env->seg_cache[R_SS].base + offset, T0);
774 /* modify ESP after to handle exceptions correctly */
775 ESP = (ESP & ~0xffff) | offset;
778 /* NOTE: ESP update is done after */
779 void op_popl_T0(void)
781 T0 = ldl((void *)ESP);
784 void op_popw_T0(void)
786 T0 = lduw((void *)ESP);
789 void op_popl_ss32_T0(void)
791 T0 = ldl(env->seg_cache[R_SS].base + ESP);
794 void op_popw_ss32_T0(void)
796 T0 = lduw(env->seg_cache[R_SS].base + ESP);
799 void op_popl_ss16_T0(void)
801 T0 = ldl(env->seg_cache[R_SS].base + (ESP & 0xffff));
804 void op_popw_ss16_T0(void)
806 T0 = lduw(env->seg_cache[R_SS].base + (ESP & 0xffff));
809 void op_addl_ESP_4(void)
814 void op_addl_ESP_2(void)
819 void op_addw_ESP_4(void)
821 ESP = (ESP & ~0xffff) | ((ESP + 4) & 0xffff);
824 void op_addw_ESP_2(void)
826 ESP = (ESP & ~0xffff) | ((ESP + 2) & 0xffff);
829 void op_addl_ESP_im(void)
834 void op_addw_ESP_im(void)
836 ESP = (ESP & ~0xffff) | ((ESP + PARAM1) & 0xffff);
839 void OPPROTO op_rdtsc(void)
844 void OPPROTO op_cpuid(void)
852 void OPPROTO op_aam(void)
859 EAX = (EAX & ~0xffff) | al | (ah << 8);
863 void OPPROTO op_aad(void)
868 ah = (EAX >> 8) & 0xff;
869 al = ((ah * base) + al) & 0xff;
870 EAX = (EAX & ~0xffff) | al;
874 void OPPROTO op_aaa(void)
880 eflags = cc_table[CC_OP].compute_all();
883 ah = (EAX >> 8) & 0xff;
885 icarry = (al > 0xf9);
886 if (((al & 0x0f) > 9 ) || af) {
887 al = (al + 6) & 0x0f;
888 ah = (ah + 1 + icarry) & 0xff;
889 eflags |= CC_C | CC_A;
891 eflags &= ~(CC_C | CC_A);
894 EAX = (EAX & ~0xffff) | al | (ah << 8);
898 void OPPROTO op_aas(void)
904 eflags = cc_table[CC_OP].compute_all();
907 ah = (EAX >> 8) & 0xff;
910 if (((al & 0x0f) > 9 ) || af) {
911 al = (al - 6) & 0x0f;
912 ah = (ah - 1 - icarry) & 0xff;
913 eflags |= CC_C | CC_A;
915 eflags &= ~(CC_C | CC_A);
918 EAX = (EAX & ~0xffff) | al | (ah << 8);
922 void OPPROTO op_daa(void)
927 eflags = cc_table[CC_OP].compute_all();
933 if (((al & 0x0f) > 9 ) || af) {
934 al = (al + 6) & 0xff;
937 if ((al > 0x9f) || cf) {
938 al = (al + 0x60) & 0xff;
941 EAX = (EAX & ~0xff) | al;
942 /* well, speed is not an issue here, so we compute the flags by hand */
943 eflags |= (al == 0) << 6; /* zf */
944 eflags |= parity_table[al]; /* pf */
945 eflags |= (al & 0x80); /* sf */
949 void OPPROTO op_das(void)
954 eflags = cc_table[CC_OP].compute_all();
961 if (((al & 0x0f) > 9 ) || af) {
965 al = (al - 6) & 0xff;
967 if ((al1 > 0x99) || cf) {
968 al = (al - 0x60) & 0xff;
971 EAX = (EAX & ~0xff) | al;
972 /* well, speed is not an issue here, so we compute the flags by hand */
973 eflags |= (al == 0) << 6; /* zf */
974 eflags |= parity_table[al]; /* pf */
975 eflags |= (al & 0x80); /* sf */
979 /* segment handling */
981 void OPPROTO op_movl_seg_T0(void)
983 load_seg(PARAM1, T0 & 0xffff, PARAM2);
986 /* faster VM86 version */
987 void OPPROTO op_movl_seg_T0_vm(void)
991 selector = T0 & 0xffff;
992 /* env->segs[] access */
993 *(uint32_t *)((char *)env + PARAM1) = selector;
994 /* env->seg_cache[] access */
995 ((SegmentCache *)((char *)env + PARAM2))->base = (void *)(selector << 4);
998 void OPPROTO op_movl_T0_seg(void)
1000 T0 = env->segs[PARAM1];
1003 void OPPROTO op_movl_A0_seg(void)
1005 A0 = *(unsigned long *)((char *)env + PARAM1);
1008 void OPPROTO op_addl_A0_seg(void)
1010 A0 += *(unsigned long *)((char *)env + PARAM1);
1013 void OPPROTO op_lsl(void)
1018 void OPPROTO op_lar(void)
1023 /* flags handling */
1025 /* slow jumps cases : in order to avoid calling a function with a
1026 pointer (which can generate a stack frame on PowerPC), we use
1027 op_setcc to set T0 and then call op_jcc. */
1028 void OPPROTO op_jcc(void)
1031 JUMP_TB(PARAM1, 0, PARAM2);
1033 JUMP_TB(PARAM1, 1, PARAM3);
1037 /* slow set cases (compute x86 flags) */
1038 void OPPROTO op_seto_T0_cc(void)
1041 eflags = cc_table[CC_OP].compute_all();
1042 T0 = (eflags >> 11) & 1;
1045 void OPPROTO op_setb_T0_cc(void)
1047 T0 = cc_table[CC_OP].compute_c();
1050 void OPPROTO op_setz_T0_cc(void)
1053 eflags = cc_table[CC_OP].compute_all();
1054 T0 = (eflags >> 6) & 1;
1057 void OPPROTO op_setbe_T0_cc(void)
1060 eflags = cc_table[CC_OP].compute_all();
1061 T0 = (eflags & (CC_Z | CC_C)) != 0;
1064 void OPPROTO op_sets_T0_cc(void)
1067 eflags = cc_table[CC_OP].compute_all();
1068 T0 = (eflags >> 7) & 1;
1071 void OPPROTO op_setp_T0_cc(void)
1074 eflags = cc_table[CC_OP].compute_all();
1075 T0 = (eflags >> 2) & 1;
1078 void OPPROTO op_setl_T0_cc(void)
1081 eflags = cc_table[CC_OP].compute_all();
1082 T0 = ((eflags ^ (eflags >> 4)) >> 7) & 1;
1085 void OPPROTO op_setle_T0_cc(void)
1088 eflags = cc_table[CC_OP].compute_all();
1089 T0 = (((eflags ^ (eflags >> 4)) & 0x80) || (eflags & CC_Z)) != 0;
1092 void OPPROTO op_xor_T0_1(void)
1097 void OPPROTO op_set_cc_op(void)
1102 #define FL_UPDATE_MASK32 (TF_MASK | AC_MASK | ID_MASK)
1103 #define FL_UPDATE_MASK16 (TF_MASK)
1105 void OPPROTO op_movl_eflags_T0(void)
1109 CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1110 DF = 1 - (2 * ((eflags >> 10) & 1));
1111 /* we also update some system flags as in user mode */
1112 env->eflags = (env->eflags & ~FL_UPDATE_MASK32) | (eflags & FL_UPDATE_MASK32);
1115 void OPPROTO op_movw_eflags_T0(void)
1119 CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1120 DF = 1 - (2 * ((eflags >> 10) & 1));
1121 /* we also update some system flags as in user mode */
1122 env->eflags = (env->eflags & ~FL_UPDATE_MASK16) | (eflags & FL_UPDATE_MASK16);
1126 /* vm86plus version */
1127 void OPPROTO op_movw_eflags_T0_vm(void)
1131 CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1132 DF = 1 - (2 * ((eflags >> 10) & 1));
1133 /* we also update some system flags as in user mode */
1134 env->eflags = (env->eflags & ~(FL_UPDATE_MASK16 | VIF_MASK)) |
1135 (eflags & FL_UPDATE_MASK16);
1136 if (eflags & IF_MASK) {
1137 env->eflags |= VIF_MASK;
1138 if (env->eflags & VIP_MASK) {
1140 raise_exception(EXCP0D_GPF);
1146 void OPPROTO op_movl_eflags_T0_vm(void)
1150 CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1151 DF = 1 - (2 * ((eflags >> 10) & 1));
1152 /* we also update some system flags as in user mode */
1153 env->eflags = (env->eflags & ~(FL_UPDATE_MASK32 | VIF_MASK)) |
1154 (eflags & FL_UPDATE_MASK32);
1155 if (eflags & IF_MASK) {
1156 env->eflags |= VIF_MASK;
1157 if (env->eflags & VIP_MASK) {
1159 raise_exception(EXCP0D_GPF);
1166 /* XXX: compute only O flag */
1167 void OPPROTO op_movb_eflags_T0(void)
1170 of = cc_table[CC_OP].compute_all() & CC_O;
1171 CC_SRC = (T0 & (CC_S | CC_Z | CC_A | CC_P | CC_C)) | of;
1174 void OPPROTO op_movl_T0_eflags(void)
1177 eflags = cc_table[CC_OP].compute_all();
1178 eflags |= (DF & DF_MASK);
1179 eflags |= env->eflags & ~(VM_MASK | RF_MASK);
1183 /* vm86plus version */
1185 void OPPROTO op_movl_T0_eflags_vm(void)
1188 eflags = cc_table[CC_OP].compute_all();
1189 eflags |= (DF & DF_MASK);
1190 eflags |= env->eflags & ~(VM_MASK | RF_MASK | IF_MASK);
1191 if (env->eflags & VIF_MASK)
1197 void OPPROTO op_cld(void)
1202 void OPPROTO op_std(void)
1207 void OPPROTO op_clc(void)
1210 eflags = cc_table[CC_OP].compute_all();
1215 void OPPROTO op_stc(void)
1218 eflags = cc_table[CC_OP].compute_all();
1223 void OPPROTO op_cmc(void)
1226 eflags = cc_table[CC_OP].compute_all();
1231 void OPPROTO op_salc(void)
1234 cf = cc_table[CC_OP].compute_c();
1235 EAX = (EAX & ~0xff) | ((-cf) & 0xff);
1238 static int compute_all_eflags(void)
1243 static int compute_c_eflags(void)
1245 return CC_SRC & CC_C;
1248 static int compute_c_mul(void)
1255 static int compute_all_mul(void)
1257 int cf, pf, af, zf, sf, of;
1259 pf = 0; /* undefined */
1260 af = 0; /* undefined */
1261 zf = 0; /* undefined */
1262 sf = 0; /* undefined */
1264 return cf | pf | af | zf | sf | of;
1267 CCTable cc_table[CC_OP_NB] = {
1268 [CC_OP_DYNAMIC] = { /* should never happen */ },
1270 [CC_OP_EFLAGS] = { compute_all_eflags, compute_c_eflags },
1272 [CC_OP_MUL] = { compute_all_mul, compute_c_mul },
1274 [CC_OP_ADDB] = { compute_all_addb, compute_c_addb },
1275 [CC_OP_ADDW] = { compute_all_addw, compute_c_addw },
1276 [CC_OP_ADDL] = { compute_all_addl, compute_c_addl },
1278 [CC_OP_ADCB] = { compute_all_adcb, compute_c_adcb },
1279 [CC_OP_ADCW] = { compute_all_adcw, compute_c_adcw },
1280 [CC_OP_ADCL] = { compute_all_adcl, compute_c_adcl },
1282 [CC_OP_SUBB] = { compute_all_subb, compute_c_subb },
1283 [CC_OP_SUBW] = { compute_all_subw, compute_c_subw },
1284 [CC_OP_SUBL] = { compute_all_subl, compute_c_subl },
1286 [CC_OP_SBBB] = { compute_all_sbbb, compute_c_sbbb },
1287 [CC_OP_SBBW] = { compute_all_sbbw, compute_c_sbbw },
1288 [CC_OP_SBBL] = { compute_all_sbbl, compute_c_sbbl },
1290 [CC_OP_LOGICB] = { compute_all_logicb, compute_c_logicb },
1291 [CC_OP_LOGICW] = { compute_all_logicw, compute_c_logicw },
1292 [CC_OP_LOGICL] = { compute_all_logicl, compute_c_logicl },
1294 [CC_OP_INCB] = { compute_all_incb, compute_c_incl },
1295 [CC_OP_INCW] = { compute_all_incw, compute_c_incl },
1296 [CC_OP_INCL] = { compute_all_incl, compute_c_incl },
1298 [CC_OP_DECB] = { compute_all_decb, compute_c_incl },
1299 [CC_OP_DECW] = { compute_all_decw, compute_c_incl },
1300 [CC_OP_DECL] = { compute_all_decl, compute_c_incl },
1302 [CC_OP_SHLB] = { compute_all_shlb, compute_c_shlb },
1303 [CC_OP_SHLW] = { compute_all_shlw, compute_c_shlw },
1304 [CC_OP_SHLL] = { compute_all_shll, compute_c_shll },
1306 [CC_OP_SARB] = { compute_all_sarb, compute_c_sarl },
1307 [CC_OP_SARW] = { compute_all_sarw, compute_c_sarl },
1308 [CC_OP_SARL] = { compute_all_sarl, compute_c_sarl },
1311 /* floating point support. Some of the code for complicated x87
1312 functions comes from the LGPL'ed x86 emulator found in the Willows
1313 TWIN windows emulator. */
1315 #if defined(__powerpc__)
1316 extern CPU86_LDouble copysign(CPU86_LDouble, CPU86_LDouble);
1318 /* correct (but slow) PowerPC rint() (glibc version is incorrect) */
1319 double qemu_rint(double x)
1321 double y = 4503599627370496.0;
1332 #define rint qemu_rint
1337 void OPPROTO op_flds_FT0_A0(void)
1339 #ifdef USE_FP_CONVERT
1340 FP_CONVERT.i32 = ldl((void *)A0);
1343 FT0 = ldfl((void *)A0);
1347 void OPPROTO op_fldl_FT0_A0(void)
1349 #ifdef USE_FP_CONVERT
1350 FP_CONVERT.i64 = ldq((void *)A0);
1353 FT0 = ldfq((void *)A0);
1357 /* helpers are needed to avoid static constant reference. XXX: find a better way */
1358 #ifdef USE_INT_TO_FLOAT_HELPERS
1360 void helper_fild_FT0_A0(void)
1362 FT0 = (CPU86_LDouble)ldsw((void *)A0);
1365 void helper_fildl_FT0_A0(void)
1367 FT0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1370 void helper_fildll_FT0_A0(void)
1372 FT0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1375 void OPPROTO op_fild_FT0_A0(void)
1377 helper_fild_FT0_A0();
1380 void OPPROTO op_fildl_FT0_A0(void)
1382 helper_fildl_FT0_A0();
1385 void OPPROTO op_fildll_FT0_A0(void)
1387 helper_fildll_FT0_A0();
1392 void OPPROTO op_fild_FT0_A0(void)
1394 #ifdef USE_FP_CONVERT
1395 FP_CONVERT.i32 = ldsw((void *)A0);
1396 FT0 = (CPU86_LDouble)FP_CONVERT.i32;
1398 FT0 = (CPU86_LDouble)ldsw((void *)A0);
1402 void OPPROTO op_fildl_FT0_A0(void)
1404 #ifdef USE_FP_CONVERT
1405 FP_CONVERT.i32 = (int32_t) ldl((void *)A0);
1406 FT0 = (CPU86_LDouble)FP_CONVERT.i32;
1408 FT0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1412 void OPPROTO op_fildll_FT0_A0(void)
1414 #ifdef USE_FP_CONVERT
1415 FP_CONVERT.i64 = (int64_t) ldq((void *)A0);
1416 FT0 = (CPU86_LDouble)FP_CONVERT.i64;
1418 FT0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1425 void OPPROTO op_flds_ST0_A0(void)
1427 #ifdef USE_FP_CONVERT
1428 FP_CONVERT.i32 = ldl((void *)A0);
1431 ST0 = ldfl((void *)A0);
1435 void OPPROTO op_fldl_ST0_A0(void)
1437 #ifdef USE_FP_CONVERT
1438 FP_CONVERT.i64 = ldq((void *)A0);
1441 ST0 = ldfq((void *)A0);
1445 #ifdef USE_X86LDOUBLE
1446 void OPPROTO op_fldt_ST0_A0(void)
1448 ST0 = *(long double *)A0;
1451 void OPPROTO op_fldt_ST0_A0(void)
1453 helper_fldt_ST0_A0();
1457 /* helpers are needed to avoid static constant reference. XXX: find a better way */
1458 #ifdef USE_INT_TO_FLOAT_HELPERS
1460 void helper_fild_ST0_A0(void)
1462 ST0 = (CPU86_LDouble)ldsw((void *)A0);
1465 void helper_fildl_ST0_A0(void)
1467 ST0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1470 void helper_fildll_ST0_A0(void)
1472 ST0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1475 void OPPROTO op_fild_ST0_A0(void)
1477 helper_fild_ST0_A0();
1480 void OPPROTO op_fildl_ST0_A0(void)
1482 helper_fildl_ST0_A0();
1485 void OPPROTO op_fildll_ST0_A0(void)
1487 helper_fildll_ST0_A0();
1492 void OPPROTO op_fild_ST0_A0(void)
1494 #ifdef USE_FP_CONVERT
1495 FP_CONVERT.i32 = ldsw((void *)A0);
1496 ST0 = (CPU86_LDouble)FP_CONVERT.i32;
1498 ST0 = (CPU86_LDouble)ldsw((void *)A0);
1502 void OPPROTO op_fildl_ST0_A0(void)
1504 #ifdef USE_FP_CONVERT
1505 FP_CONVERT.i32 = (int32_t) ldl((void *)A0);
1506 ST0 = (CPU86_LDouble)FP_CONVERT.i32;
1508 ST0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1512 void OPPROTO op_fildll_ST0_A0(void)
1514 #ifdef USE_FP_CONVERT
1515 FP_CONVERT.i64 = (int64_t) ldq((void *)A0);
1516 ST0 = (CPU86_LDouble)FP_CONVERT.i64;
1518 ST0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1526 void OPPROTO op_fsts_ST0_A0(void)
1528 #ifdef USE_FP_CONVERT
1529 FP_CONVERT.f = (float)ST0;
1530 stfl((void *)A0, FP_CONVERT.f);
1532 stfl((void *)A0, (float)ST0);
1536 void OPPROTO op_fstl_ST0_A0(void)
1538 stfq((void *)A0, (double)ST0);
1541 #ifdef USE_X86LDOUBLE
1542 void OPPROTO op_fstt_ST0_A0(void)
1544 *(long double *)A0 = ST0;
1547 void OPPROTO op_fstt_ST0_A0(void)
1549 helper_fstt_ST0_A0();
1553 void OPPROTO op_fist_ST0_A0(void)
1555 #if defined(__sparc__) && !defined(__sparc_v9__)
1556 register CPU86_LDouble d asm("o0");
1564 stw((void *)A0, val);
1567 void OPPROTO op_fistl_ST0_A0(void)
1569 #if defined(__sparc__) && !defined(__sparc_v9__)
1570 register CPU86_LDouble d asm("o0");
1578 stl((void *)A0, val);
1581 void OPPROTO op_fistll_ST0_A0(void)
1583 #if defined(__sparc__) && !defined(__sparc_v9__)
1584 register CPU86_LDouble d asm("o0");
1592 stq((void *)A0, val);
1595 void OPPROTO op_fbld_ST0_A0(void)
1597 helper_fbld_ST0_A0();
1600 void OPPROTO op_fbst_ST0_A0(void)
1602 helper_fbst_ST0_A0();
1607 void OPPROTO op_fpush(void)
1612 void OPPROTO op_fpop(void)
1617 void OPPROTO op_fdecstp(void)
1619 env->fpstt = (env->fpstt - 1) & 7;
1620 env->fpus &= (~0x4700);
1623 void OPPROTO op_fincstp(void)
1625 env->fpstt = (env->fpstt + 1) & 7;
1626 env->fpus &= (~0x4700);
1629 void OPPROTO op_fmov_ST0_FT0(void)
1634 void OPPROTO op_fmov_FT0_STN(void)
1639 void OPPROTO op_fmov_ST0_STN(void)
1644 void OPPROTO op_fmov_STN_ST0(void)
1649 void OPPROTO op_fxchg_ST0_STN(void)
1657 /* FPU operations */
1659 /* XXX: handle nans */
1660 void OPPROTO op_fcom_ST0_FT0(void)
1662 env->fpus &= (~0x4500); /* (C3,C2,C0) <-- 000 */
1664 env->fpus |= 0x100; /* (C3,C2,C0) <-- 001 */
1665 else if (ST0 == FT0)
1666 env->fpus |= 0x4000; /* (C3,C2,C0) <-- 100 */
1670 /* XXX: handle nans */
1671 void OPPROTO op_fucom_ST0_FT0(void)
1673 env->fpus &= (~0x4500); /* (C3,C2,C0) <-- 000 */
1675 env->fpus |= 0x100; /* (C3,C2,C0) <-- 001 */
1676 else if (ST0 == FT0)
1677 env->fpus |= 0x4000; /* (C3,C2,C0) <-- 100 */
1681 /* XXX: handle nans */
1682 void OPPROTO op_fcomi_ST0_FT0(void)
1685 eflags = cc_table[CC_OP].compute_all();
1686 eflags &= ~(CC_Z | CC_P | CC_C);
1689 else if (ST0 == FT0)
1695 /* XXX: handle nans */
1696 void OPPROTO op_fucomi_ST0_FT0(void)
1699 eflags = cc_table[CC_OP].compute_all();
1700 eflags &= ~(CC_Z | CC_P | CC_C);
1703 else if (ST0 == FT0)
1709 void OPPROTO op_fadd_ST0_FT0(void)
1714 void OPPROTO op_fmul_ST0_FT0(void)
1719 void OPPROTO op_fsub_ST0_FT0(void)
1724 void OPPROTO op_fsubr_ST0_FT0(void)
1729 void OPPROTO op_fdiv_ST0_FT0(void)
1734 void OPPROTO op_fdivr_ST0_FT0(void)
1739 /* fp operations between STN and ST0 */
1741 void OPPROTO op_fadd_STN_ST0(void)
1746 void OPPROTO op_fmul_STN_ST0(void)
1751 void OPPROTO op_fsub_STN_ST0(void)
1756 void OPPROTO op_fsubr_STN_ST0(void)
1763 void OPPROTO op_fdiv_STN_ST0(void)
1768 void OPPROTO op_fdivr_STN_ST0(void)
1775 /* misc FPU operations */
1776 void OPPROTO op_fchs_ST0(void)
1781 void OPPROTO op_fabs_ST0(void)
1786 void OPPROTO op_fxam_ST0(void)
1791 void OPPROTO op_fld1_ST0(void)
1796 void OPPROTO op_fldl2t_ST0(void)
1801 void OPPROTO op_fldl2e_ST0(void)
1806 void OPPROTO op_fldpi_ST0(void)
1811 void OPPROTO op_fldlg2_ST0(void)
1816 void OPPROTO op_fldln2_ST0(void)
1821 void OPPROTO op_fldz_ST0(void)
1826 void OPPROTO op_fldz_FT0(void)
1831 /* associated heplers to reduce generated code length and to simplify
1832 relocation (FP constants are usually stored in .rodata section) */
1834 void OPPROTO op_f2xm1(void)
1839 void OPPROTO op_fyl2x(void)
1844 void OPPROTO op_fptan(void)
1849 void OPPROTO op_fpatan(void)
1854 void OPPROTO op_fxtract(void)
1859 void OPPROTO op_fprem1(void)
1865 void OPPROTO op_fprem(void)
1870 void OPPROTO op_fyl2xp1(void)
1875 void OPPROTO op_fsqrt(void)
1880 void OPPROTO op_fsincos(void)
1885 void OPPROTO op_frndint(void)
1890 void OPPROTO op_fscale(void)
1895 void OPPROTO op_fsin(void)
1900 void OPPROTO op_fcos(void)
1905 void OPPROTO op_fnstsw_A0(void)
1908 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1909 stw((void *)A0, fpus);
1912 void OPPROTO op_fnstsw_EAX(void)
1915 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1916 EAX = (EAX & 0xffff0000) | fpus;
1919 void OPPROTO op_fnstcw_A0(void)
1921 stw((void *)A0, env->fpuc);
1924 void OPPROTO op_fldcw_A0(void)
1927 env->fpuc = lduw((void *)A0);
1928 /* set rounding mode */
1929 switch(env->fpuc & RC_MASK) {
1932 rnd_type = FE_TONEAREST;
1935 rnd_type = FE_DOWNWARD;
1938 rnd_type = FE_UPWARD;
1941 rnd_type = FE_TOWARDZERO;
1944 fesetround(rnd_type);
1947 void OPPROTO op_fclex(void)
1949 env->fpus &= 0x7f00;
1952 void OPPROTO op_fninit(void)
1967 void OPPROTO op_fnstenv_A0(void)
1969 helper_fstenv((uint8_t *)A0, PARAM1);
1972 void OPPROTO op_fldenv_A0(void)
1974 helper_fldenv((uint8_t *)A0, PARAM1);
1977 void OPPROTO op_fnsave_A0(void)
1979 helper_fsave((uint8_t *)A0, PARAM1);
1982 void OPPROTO op_frstor_A0(void)
1984 helper_frstor((uint8_t *)A0, PARAM1);
1987 /* threading support */
1988 void OPPROTO op_lock(void)
1993 void OPPROTO op_unlock(void)