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 /* n must be a constant to be efficient */
23 static inline int lshift(int x, int n)
31 /* we define the various pieces of code used by the JIT */
35 #include "opreg_template.h"
41 #include "opreg_template.h"
47 #include "opreg_template.h"
53 #include "opreg_template.h"
59 #include "opreg_template.h"
65 #include "opreg_template.h"
71 #include "opreg_template.h"
77 #include "opreg_template.h"
81 /* operations with flags */
83 /* update flags with T0 and T1 (add/sub case) */
84 void OPPROTO op_update2_cc(void)
90 /* update flags with T0 (logic operation case) */
91 void OPPROTO op_update1_cc(void)
96 void OPPROTO op_update_neg_cc(void)
102 void OPPROTO op_cmpl_T0_T1_cc(void)
108 void OPPROTO op_update_inc_cc(void)
110 CC_SRC = cc_table[CC_OP].compute_c();
114 void OPPROTO op_testl_T0_T1_cc(void)
119 /* operations without flags */
121 void OPPROTO op_addl_T0_T1(void)
126 void OPPROTO op_orl_T0_T1(void)
131 void OPPROTO op_andl_T0_T1(void)
136 void OPPROTO op_subl_T0_T1(void)
141 void OPPROTO op_xorl_T0_T1(void)
146 void OPPROTO op_negl_T0(void)
151 void OPPROTO op_incl_T0(void)
156 void OPPROTO op_decl_T0(void)
161 void OPPROTO op_notl_T0(void)
166 void OPPROTO op_bswapl_T0(void)
171 /* multiply/divide */
172 void OPPROTO op_mulb_AL_T0(void)
175 res = (uint8_t)EAX * (uint8_t)T0;
176 EAX = (EAX & 0xffff0000) | res;
177 CC_SRC = (res & 0xff00);
180 void OPPROTO op_imulb_AL_T0(void)
183 res = (int8_t)EAX * (int8_t)T0;
184 EAX = (EAX & 0xffff0000) | (res & 0xffff);
185 CC_SRC = (res != (int8_t)res);
188 void OPPROTO op_mulw_AX_T0(void)
191 res = (uint16_t)EAX * (uint16_t)T0;
192 EAX = (EAX & 0xffff0000) | (res & 0xffff);
193 EDX = (EDX & 0xffff0000) | ((res >> 16) & 0xffff);
197 void OPPROTO op_imulw_AX_T0(void)
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);
206 void OPPROTO op_mull_EAX_T0(void)
209 res = (uint64_t)((uint32_t)EAX) * (uint64_t)((uint32_t)T0);
215 void OPPROTO op_imull_EAX_T0(void)
218 res = (int64_t)((int32_t)EAX) * (int64_t)((int32_t)T0);
221 CC_SRC = (res != (int32_t)res);
224 void OPPROTO op_imulw_T0_T1(void)
227 res = (int16_t)T0 * (int16_t)T1;
229 CC_SRC = (res != (int16_t)res);
232 void OPPROTO op_imull_T0_T1(void)
235 res = (int64_t)((int32_t)T0) * (int64_t)((int32_t)T1);
237 CC_SRC = (res != (int32_t)res);
240 /* division, flags are undefined */
241 /* XXX: add exceptions for overflow */
243 void OPPROTO op_divb_AL_T0(void)
245 unsigned int num, den, q, r;
247 num = (EAX & 0xffff);
251 raise_exception(EXCP00_DIVZ);
253 q = (num / den) & 0xff;
254 r = (num % den) & 0xff;
255 EAX = (EAX & 0xffff0000) | (r << 8) | q;
258 void OPPROTO op_idivb_AL_T0(void)
266 raise_exception(EXCP00_DIVZ);
268 q = (num / den) & 0xff;
269 r = (num % den) & 0xff;
270 EAX = (EAX & 0xffff0000) | (r << 8) | q;
273 void OPPROTO op_divw_AX_T0(void)
275 unsigned int num, den, q, r;
277 num = (EAX & 0xffff) | ((EDX & 0xffff) << 16);
281 raise_exception(EXCP00_DIVZ);
283 q = (num / den) & 0xffff;
284 r = (num % den) & 0xffff;
285 EAX = (EAX & 0xffff0000) | q;
286 EDX = (EDX & 0xffff0000) | r;
289 void OPPROTO op_idivw_AX_T0(void)
293 num = (EAX & 0xffff) | ((EDX & 0xffff) << 16);
297 raise_exception(EXCP00_DIVZ);
299 q = (num / den) & 0xffff;
300 r = (num % den) & 0xffff;
301 EAX = (EAX & 0xffff0000) | q;
302 EDX = (EDX & 0xffff0000) | r;
305 void OPPROTO op_divl_EAX_T0(void)
307 helper_divl_EAX_T0(PARAM1);
310 void OPPROTO op_idivl_EAX_T0(void)
312 helper_idivl_EAX_T0(PARAM1);
315 /* constant load & misc op */
317 void OPPROTO op_movl_T0_im(void)
322 void OPPROTO op_addl_T0_im(void)
327 void OPPROTO op_andl_T0_ffff(void)
332 void OPPROTO op_andl_T0_im(void)
337 void OPPROTO op_movl_T0_T1(void)
342 void OPPROTO op_movl_T1_im(void)
347 void OPPROTO op_addl_T1_im(void)
352 void OPPROTO op_movl_T1_A0(void)
357 void OPPROTO op_movl_A0_im(void)
362 void OPPROTO op_addl_A0_im(void)
367 void OPPROTO op_addl_A0_AL(void)
372 void OPPROTO op_andl_A0_ffff(void)
379 void OPPROTO op_ldub_T0_A0(void)
381 T0 = ldub((uint8_t *)A0);
384 void OPPROTO op_ldsb_T0_A0(void)
386 T0 = ldsb((int8_t *)A0);
389 void OPPROTO op_lduw_T0_A0(void)
391 T0 = lduw((uint8_t *)A0);
394 void OPPROTO op_ldsw_T0_A0(void)
396 T0 = ldsw((int8_t *)A0);
399 void OPPROTO op_ldl_T0_A0(void)
401 T0 = ldl((uint8_t *)A0);
404 void OPPROTO op_ldub_T1_A0(void)
406 T1 = ldub((uint8_t *)A0);
409 void OPPROTO op_ldsb_T1_A0(void)
411 T1 = ldsb((int8_t *)A0);
414 void OPPROTO op_lduw_T1_A0(void)
416 T1 = lduw((uint8_t *)A0);
419 void OPPROTO op_ldsw_T1_A0(void)
421 T1 = ldsw((int8_t *)A0);
424 void OPPROTO op_ldl_T1_A0(void)
426 T1 = ldl((uint8_t *)A0);
429 void OPPROTO op_stb_T0_A0(void)
431 stb((uint8_t *)A0, T0);
434 void OPPROTO op_stw_T0_A0(void)
436 stw((uint8_t *)A0, T0);
439 void OPPROTO op_stl_T0_A0(void)
441 stl((uint8_t *)A0, T0);
444 /* used for bit operations */
446 void OPPROTO op_add_bitw_A0_T1(void)
448 A0 += ((int32_t)T1 >> 4) << 1;
451 void OPPROTO op_add_bitl_A0_T1(void)
453 A0 += ((int32_t)T1 >> 5) << 2;
458 void OPPROTO op_jmp_T0(void)
463 void OPPROTO op_jmp_im(void)
468 void OPPROTO op_hlt(void)
470 env->exception_index = EXCP_HLT;
474 void OPPROTO op_raise_interrupt(void)
477 unsigned int next_eip;
480 raise_interrupt(intno, 1, 0, next_eip);
483 void OPPROTO op_raise_exception(void)
486 exception_index = PARAM1;
487 raise_exception(exception_index);
490 void OPPROTO op_into(void)
493 eflags = cc_table[CC_OP].compute_all();
495 raise_interrupt(EXCP04_INTO, 1, 0, PARAM1);
500 void OPPROTO op_cli(void)
502 env->eflags &= ~IF_MASK;
505 void OPPROTO op_sti(void)
507 env->eflags |= IF_MASK;
511 /* vm86plus instructions */
512 void OPPROTO op_cli_vm(void)
514 env->eflags &= ~VIF_MASK;
517 void OPPROTO op_sti_vm(void)
519 env->eflags |= VIF_MASK;
520 if (env->eflags & VIP_MASK) {
522 raise_exception(EXCP0D_GPF);
528 void OPPROTO op_boundw(void)
531 low = ldsw((uint8_t *)A0);
532 high = ldsw((uint8_t *)A0 + 2);
534 if (v < low || v > high) {
536 raise_exception(EXCP05_BOUND);
541 void OPPROTO op_boundl(void)
544 low = ldl((uint8_t *)A0);
545 high = ldl((uint8_t *)A0 + 4);
547 if (v < low || v > high) {
549 raise_exception(EXCP05_BOUND);
554 void OPPROTO op_cmpxchg8b(void)
559 void OPPROTO op_jmp_tb_next(void)
561 JUMP_TB(PARAM1, 0, PARAM2);
564 void OPPROTO op_movl_T0_0(void)
569 /* multiple size ops */
574 #include "ops_template.h"
578 #include "ops_template.h"
582 #include "ops_template.h"
587 void OPPROTO op_movsbl_T0_T0(void)
592 void OPPROTO op_movzbl_T0_T0(void)
597 void OPPROTO op_movswl_T0_T0(void)
602 void OPPROTO op_movzwl_T0_T0(void)
607 void OPPROTO op_movswl_EAX_AX(void)
612 void OPPROTO op_movsbw_AX_AL(void)
614 EAX = (EAX & 0xffff0000) | ((int8_t)EAX & 0xffff);
617 void OPPROTO op_movslq_EDX_EAX(void)
619 EDX = (int32_t)EAX >> 31;
622 void OPPROTO op_movswl_DX_AX(void)
624 EDX = (EDX & 0xffff0000) | (((int16_t)EAX >> 15) & 0xffff);
629 void op_pushl_T0(void)
633 stl((void *)offset, T0);
634 /* modify ESP after to handle exceptions correctly */
638 void op_pushw_T0(void)
642 stw((void *)offset, T0);
643 /* modify ESP after to handle exceptions correctly */
647 void op_pushl_ss32_T0(void)
651 stl(env->segs[R_SS].base + offset, T0);
652 /* modify ESP after to handle exceptions correctly */
656 void op_pushw_ss32_T0(void)
660 stw(env->segs[R_SS].base + offset, T0);
661 /* modify ESP after to handle exceptions correctly */
665 void op_pushl_ss16_T0(void)
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;
674 void op_pushw_ss16_T0(void)
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;
683 /* NOTE: ESP update is done after */
684 void op_popl_T0(void)
686 T0 = ldl((void *)ESP);
689 void op_popw_T0(void)
691 T0 = lduw((void *)ESP);
694 void op_popl_ss32_T0(void)
696 T0 = ldl(env->segs[R_SS].base + ESP);
699 void op_popw_ss32_T0(void)
701 T0 = lduw(env->segs[R_SS].base + ESP);
704 void op_popl_ss16_T0(void)
706 T0 = ldl(env->segs[R_SS].base + (ESP & 0xffff));
709 void op_popw_ss16_T0(void)
711 T0 = lduw(env->segs[R_SS].base + (ESP & 0xffff));
714 void op_addl_ESP_4(void)
719 void op_addl_ESP_2(void)
724 void op_addw_ESP_4(void)
726 ESP = (ESP & ~0xffff) | ((ESP + 4) & 0xffff);
729 void op_addw_ESP_2(void)
731 ESP = (ESP & ~0xffff) | ((ESP + 2) & 0xffff);
734 void op_addl_ESP_im(void)
739 void op_addw_ESP_im(void)
741 ESP = (ESP & ~0xffff) | ((ESP + PARAM1) & 0xffff);
744 void OPPROTO op_rdtsc(void)
749 void OPPROTO op_cpuid(void)
754 void OPPROTO op_rdmsr(void)
759 void OPPROTO op_wrmsr(void)
767 void OPPROTO op_aam(void)
774 EAX = (EAX & ~0xffff) | al | (ah << 8);
778 void OPPROTO op_aad(void)
783 ah = (EAX >> 8) & 0xff;
784 al = ((ah * base) + al) & 0xff;
785 EAX = (EAX & ~0xffff) | al;
789 void OPPROTO op_aaa(void)
795 eflags = cc_table[CC_OP].compute_all();
798 ah = (EAX >> 8) & 0xff;
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;
806 eflags &= ~(CC_C | CC_A);
809 EAX = (EAX & ~0xffff) | al | (ah << 8);
813 void OPPROTO op_aas(void)
819 eflags = cc_table[CC_OP].compute_all();
822 ah = (EAX >> 8) & 0xff;
825 if (((al & 0x0f) > 9 ) || af) {
826 al = (al - 6) & 0x0f;
827 ah = (ah - 1 - icarry) & 0xff;
828 eflags |= CC_C | CC_A;
830 eflags &= ~(CC_C | CC_A);
833 EAX = (EAX & ~0xffff) | al | (ah << 8);
837 void OPPROTO op_daa(void)
842 eflags = cc_table[CC_OP].compute_all();
848 if (((al & 0x0f) > 9 ) || af) {
849 al = (al + 6) & 0xff;
852 if ((al > 0x9f) || cf) {
853 al = (al + 0x60) & 0xff;
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 */
864 void OPPROTO op_das(void)
869 eflags = cc_table[CC_OP].compute_all();
876 if (((al & 0x0f) > 9 ) || af) {
880 al = (al - 6) & 0xff;
882 if ((al1 > 0x99) || cf) {
883 al = (al - 0x60) & 0xff;
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 */
894 /* segment handling */
896 void OPPROTO op_movl_seg_T0(void)
898 load_seg(PARAM1, T0 & 0xffff, PARAM2);
901 /* faster VM86 version */
902 void OPPROTO op_movl_seg_T0_vm(void)
907 selector = T0 & 0xffff;
908 /* env->segs[] access */
909 sc = (SegmentCache *)((char *)env + PARAM1);
910 sc->selector = selector;
911 sc->base = (void *)(selector << 4);
914 void OPPROTO op_movl_T0_seg(void)
916 T0 = env->segs[PARAM1].selector;
919 void OPPROTO op_movl_A0_seg(void)
921 A0 = *(unsigned long *)((char *)env + PARAM1);
924 void OPPROTO op_addl_A0_seg(void)
926 A0 += *(unsigned long *)((char *)env + PARAM1);
929 void OPPROTO op_lsl(void)
934 void OPPROTO op_lar(void)
939 /* T0: segment, T1:eip */
940 void OPPROTO op_ljmp_T0_T1(void)
942 jmp_seg(T0 & 0xffff, T1);
945 void OPPROTO op_iret_protected(void)
947 helper_iret_protected(PARAM1);
950 void OPPROTO op_lldt_T0(void)
955 void OPPROTO op_ltr_T0(void)
960 /* CR registers access */
961 void OPPROTO op_movl_crN_T0(void)
963 helper_movl_crN_T0(PARAM1);
966 /* DR registers access */
967 void OPPROTO op_movl_drN_T0(void)
969 helper_movl_drN_T0(PARAM1);
972 void OPPROTO op_lmsw_T0(void)
974 /* only 4 lower bits of CR0 are modified */
975 T0 = (env->cr[0] & ~0xf) | (T0 & 0xf);
976 helper_movl_crN_T0(0);
979 void OPPROTO op_invlpg_A0(void)
984 void OPPROTO op_movl_T0_env(void)
986 T0 = *(uint32_t *)((char *)env + PARAM1);
989 void OPPROTO op_movl_env_T0(void)
991 *(uint32_t *)((char *)env + PARAM1) = T0;
994 void OPPROTO op_movl_env_T1(void)
996 *(uint32_t *)((char *)env + PARAM1) = T1;
999 void OPPROTO op_clts(void)
1001 env->cr[0] &= ~CR0_TS_MASK;
1004 /* flags handling */
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)
1012 JUMP_TB(PARAM1, 0, PARAM2);
1014 JUMP_TB(PARAM1, 1, PARAM3);
1018 /* slow set cases (compute x86 flags) */
1019 void OPPROTO op_seto_T0_cc(void)
1022 eflags = cc_table[CC_OP].compute_all();
1023 T0 = (eflags >> 11) & 1;
1026 void OPPROTO op_setb_T0_cc(void)
1028 T0 = cc_table[CC_OP].compute_c();
1031 void OPPROTO op_setz_T0_cc(void)
1034 eflags = cc_table[CC_OP].compute_all();
1035 T0 = (eflags >> 6) & 1;
1038 void OPPROTO op_setbe_T0_cc(void)
1041 eflags = cc_table[CC_OP].compute_all();
1042 T0 = (eflags & (CC_Z | CC_C)) != 0;
1045 void OPPROTO op_sets_T0_cc(void)
1048 eflags = cc_table[CC_OP].compute_all();
1049 T0 = (eflags >> 7) & 1;
1052 void OPPROTO op_setp_T0_cc(void)
1055 eflags = cc_table[CC_OP].compute_all();
1056 T0 = (eflags >> 2) & 1;
1059 void OPPROTO op_setl_T0_cc(void)
1062 eflags = cc_table[CC_OP].compute_all();
1063 T0 = ((eflags ^ (eflags >> 4)) >> 7) & 1;
1066 void OPPROTO op_setle_T0_cc(void)
1069 eflags = cc_table[CC_OP].compute_all();
1070 T0 = (((eflags ^ (eflags >> 4)) & 0x80) || (eflags & CC_Z)) != 0;
1073 void OPPROTO op_xor_T0_1(void)
1078 void OPPROTO op_set_cc_op(void)
1083 #define FL_UPDATE_MASK16 (FL_UPDATE_MASK32 & 0xffff)
1085 void OPPROTO op_movl_eflags_T0(void)
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);
1096 void OPPROTO op_movw_eflags_T0(void)
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);
1107 void OPPROTO op_movl_eflags_T0_cpl0(void)
1109 load_eflags(T0, FL_UPDATE_CPL0_MASK);
1112 void OPPROTO op_movw_eflags_T0_cpl0(void)
1114 load_eflags(T0, FL_UPDATE_CPL0_MASK & 0xffff);
1118 /* vm86plus version */
1119 void OPPROTO op_movw_eflags_T0_vm(void)
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) {
1132 raise_exception(EXCP0D_GPF);
1138 void OPPROTO op_movl_eflags_T0_vm(void)
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) {
1151 raise_exception(EXCP0D_GPF);
1158 /* XXX: compute only O flag */
1159 void OPPROTO op_movb_eflags_T0(void)
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;
1166 void OPPROTO op_movl_T0_eflags(void)
1169 eflags = cc_table[CC_OP].compute_all();
1170 eflags |= (DF & DF_MASK);
1171 eflags |= env->eflags & ~(VM_MASK | RF_MASK);
1175 /* vm86plus version */
1177 void OPPROTO op_movl_T0_eflags_vm(void)
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)
1189 void OPPROTO op_cld(void)
1194 void OPPROTO op_std(void)
1199 void OPPROTO op_clc(void)
1202 eflags = cc_table[CC_OP].compute_all();
1207 void OPPROTO op_stc(void)
1210 eflags = cc_table[CC_OP].compute_all();
1215 void OPPROTO op_cmc(void)
1218 eflags = cc_table[CC_OP].compute_all();
1223 void OPPROTO op_salc(void)
1226 cf = cc_table[CC_OP].compute_c();
1227 EAX = (EAX & ~0xff) | ((-cf) & 0xff);
1230 static int compute_all_eflags(void)
1235 static int compute_c_eflags(void)
1237 return CC_SRC & CC_C;
1240 static int compute_c_mul(void)
1247 static int compute_all_mul(void)
1249 int cf, pf, af, zf, sf, of;
1251 pf = 0; /* undefined */
1252 af = 0; /* undefined */
1253 zf = 0; /* undefined */
1254 sf = 0; /* undefined */
1256 return cf | pf | af | zf | sf | of;
1259 CCTable cc_table[CC_OP_NB] = {
1260 [CC_OP_DYNAMIC] = { /* should never happen */ },
1262 [CC_OP_EFLAGS] = { compute_all_eflags, compute_c_eflags },
1264 [CC_OP_MUL] = { compute_all_mul, compute_c_mul },
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 },
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 },
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 },
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 },
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 },
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 },
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 },
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 },
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 },
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. */
1307 #if defined(__powerpc__)
1308 extern CPU86_LDouble copysign(CPU86_LDouble, CPU86_LDouble);
1310 /* correct (but slow) PowerPC rint() (glibc version is incorrect) */
1311 double qemu_rint(double x)
1313 double y = 4503599627370496.0;
1324 #define rint qemu_rint
1329 void OPPROTO op_flds_FT0_A0(void)
1331 #ifdef USE_FP_CONVERT
1332 FP_CONVERT.i32 = ldl((void *)A0);
1335 FT0 = ldfl((void *)A0);
1339 void OPPROTO op_fldl_FT0_A0(void)
1341 #ifdef USE_FP_CONVERT
1342 FP_CONVERT.i64 = ldq((void *)A0);
1345 FT0 = ldfq((void *)A0);
1349 /* helpers are needed to avoid static constant reference. XXX: find a better way */
1350 #ifdef USE_INT_TO_FLOAT_HELPERS
1352 void helper_fild_FT0_A0(void)
1354 FT0 = (CPU86_LDouble)ldsw((void *)A0);
1357 void helper_fildl_FT0_A0(void)
1359 FT0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1362 void helper_fildll_FT0_A0(void)
1364 FT0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1367 void OPPROTO op_fild_FT0_A0(void)
1369 helper_fild_FT0_A0();
1372 void OPPROTO op_fildl_FT0_A0(void)
1374 helper_fildl_FT0_A0();
1377 void OPPROTO op_fildll_FT0_A0(void)
1379 helper_fildll_FT0_A0();
1384 void OPPROTO op_fild_FT0_A0(void)
1386 #ifdef USE_FP_CONVERT
1387 FP_CONVERT.i32 = ldsw((void *)A0);
1388 FT0 = (CPU86_LDouble)FP_CONVERT.i32;
1390 FT0 = (CPU86_LDouble)ldsw((void *)A0);
1394 void OPPROTO op_fildl_FT0_A0(void)
1396 #ifdef USE_FP_CONVERT
1397 FP_CONVERT.i32 = (int32_t) ldl((void *)A0);
1398 FT0 = (CPU86_LDouble)FP_CONVERT.i32;
1400 FT0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1404 void OPPROTO op_fildll_FT0_A0(void)
1406 #ifdef USE_FP_CONVERT
1407 FP_CONVERT.i64 = (int64_t) ldq((void *)A0);
1408 FT0 = (CPU86_LDouble)FP_CONVERT.i64;
1410 FT0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1417 void OPPROTO op_flds_ST0_A0(void)
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;
1425 env->fpregs[new_fpstt] = ldfl((void *)A0);
1427 env->fpstt = new_fpstt;
1428 env->fptags[new_fpstt] = 0; /* validate stack entry */
1431 void OPPROTO op_fldl_ST0_A0(void)
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;
1439 env->fpregs[new_fpstt] = ldfq((void *)A0);
1441 env->fpstt = new_fpstt;
1442 env->fptags[new_fpstt] = 0; /* validate stack entry */
1445 #ifdef USE_X86LDOUBLE
1446 void OPPROTO op_fldt_ST0_A0(void)
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 */
1455 void OPPROTO op_fldt_ST0_A0(void)
1457 helper_fldt_ST0_A0();
1461 /* helpers are needed to avoid static constant reference. XXX: find a better way */
1462 #ifdef USE_INT_TO_FLOAT_HELPERS
1464 void helper_fild_ST0_A0(void)
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 */
1473 void helper_fildl_ST0_A0(void)
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 */
1482 void helper_fildll_ST0_A0(void)
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 */
1491 void OPPROTO op_fild_ST0_A0(void)
1493 helper_fild_ST0_A0();
1496 void OPPROTO op_fildl_ST0_A0(void)
1498 helper_fildl_ST0_A0();
1501 void OPPROTO op_fildll_ST0_A0(void)
1503 helper_fildll_ST0_A0();
1508 void OPPROTO op_fild_ST0_A0(void)
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;
1516 env->fpregs[new_fpstt] = (CPU86_LDouble)ldsw((void *)A0);
1518 env->fpstt = new_fpstt;
1519 env->fptags[new_fpstt] = 0; /* validate stack entry */
1522 void OPPROTO op_fildl_ST0_A0(void)
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;
1530 env->fpregs[new_fpstt] = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1532 env->fpstt = new_fpstt;
1533 env->fptags[new_fpstt] = 0; /* validate stack entry */
1536 void OPPROTO op_fildll_ST0_A0(void)
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;
1544 env->fpregs[new_fpstt] = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1546 env->fpstt = new_fpstt;
1547 env->fptags[new_fpstt] = 0; /* validate stack entry */
1554 void OPPROTO op_fsts_ST0_A0(void)
1556 #ifdef USE_FP_CONVERT
1557 FP_CONVERT.f = (float)ST0;
1558 stfl((void *)A0, FP_CONVERT.f);
1560 stfl((void *)A0, (float)ST0);
1564 void OPPROTO op_fstl_ST0_A0(void)
1566 stfq((void *)A0, (double)ST0);
1569 #ifdef USE_X86LDOUBLE
1570 void OPPROTO op_fstt_ST0_A0(void)
1572 *(long double *)A0 = ST0;
1575 void OPPROTO op_fstt_ST0_A0(void)
1577 helper_fstt_ST0_A0();
1581 void OPPROTO op_fist_ST0_A0(void)
1583 #if defined(__sparc__) && !defined(__sparc_v9__)
1584 register CPU86_LDouble d asm("o0");
1592 if (val != (int16_t)val)
1594 stw((void *)A0, val);
1597 void OPPROTO op_fistl_ST0_A0(void)
1599 #if defined(__sparc__) && !defined(__sparc_v9__)
1600 register CPU86_LDouble d asm("o0");
1608 stl((void *)A0, val);
1611 void OPPROTO op_fistll_ST0_A0(void)
1613 #if defined(__sparc__) && !defined(__sparc_v9__)
1614 register CPU86_LDouble d asm("o0");
1622 stq((void *)A0, val);
1625 void OPPROTO op_fbld_ST0_A0(void)
1627 helper_fbld_ST0_A0();
1630 void OPPROTO op_fbst_ST0_A0(void)
1632 helper_fbst_ST0_A0();
1637 void OPPROTO op_fpush(void)
1642 void OPPROTO op_fpop(void)
1647 void OPPROTO op_fdecstp(void)
1649 env->fpstt = (env->fpstt - 1) & 7;
1650 env->fpus &= (~0x4700);
1653 void OPPROTO op_fincstp(void)
1655 env->fpstt = (env->fpstt + 1) & 7;
1656 env->fpus &= (~0x4700);
1659 void OPPROTO op_fmov_ST0_FT0(void)
1664 void OPPROTO op_fmov_FT0_STN(void)
1669 void OPPROTO op_fmov_ST0_STN(void)
1674 void OPPROTO op_fmov_STN_ST0(void)
1679 void OPPROTO op_fxchg_ST0_STN(void)
1687 /* FPU operations */
1689 /* XXX: handle nans */
1690 void OPPROTO op_fcom_ST0_FT0(void)
1692 env->fpus &= (~0x4500); /* (C3,C2,C0) <-- 000 */
1694 env->fpus |= 0x100; /* (C3,C2,C0) <-- 001 */
1695 else if (ST0 == FT0)
1696 env->fpus |= 0x4000; /* (C3,C2,C0) <-- 100 */
1700 /* XXX: handle nans */
1701 void OPPROTO op_fucom_ST0_FT0(void)
1703 env->fpus &= (~0x4500); /* (C3,C2,C0) <-- 000 */
1705 env->fpus |= 0x100; /* (C3,C2,C0) <-- 001 */
1706 else if (ST0 == FT0)
1707 env->fpus |= 0x4000; /* (C3,C2,C0) <-- 100 */
1711 /* XXX: handle nans */
1712 void OPPROTO op_fcomi_ST0_FT0(void)
1715 eflags = cc_table[CC_OP].compute_all();
1716 eflags &= ~(CC_Z | CC_P | CC_C);
1719 else if (ST0 == FT0)
1725 /* XXX: handle nans */
1726 void OPPROTO op_fucomi_ST0_FT0(void)
1729 eflags = cc_table[CC_OP].compute_all();
1730 eflags &= ~(CC_Z | CC_P | CC_C);
1733 else if (ST0 == FT0)
1739 void OPPROTO op_fadd_ST0_FT0(void)
1744 void OPPROTO op_fmul_ST0_FT0(void)
1749 void OPPROTO op_fsub_ST0_FT0(void)
1754 void OPPROTO op_fsubr_ST0_FT0(void)
1759 void OPPROTO op_fdiv_ST0_FT0(void)
1764 void OPPROTO op_fdivr_ST0_FT0(void)
1769 /* fp operations between STN and ST0 */
1771 void OPPROTO op_fadd_STN_ST0(void)
1776 void OPPROTO op_fmul_STN_ST0(void)
1781 void OPPROTO op_fsub_STN_ST0(void)
1786 void OPPROTO op_fsubr_STN_ST0(void)
1793 void OPPROTO op_fdiv_STN_ST0(void)
1798 void OPPROTO op_fdivr_STN_ST0(void)
1805 /* misc FPU operations */
1806 void OPPROTO op_fchs_ST0(void)
1811 void OPPROTO op_fabs_ST0(void)
1816 void OPPROTO op_fxam_ST0(void)
1821 void OPPROTO op_fld1_ST0(void)
1826 void OPPROTO op_fldl2t_ST0(void)
1831 void OPPROTO op_fldl2e_ST0(void)
1836 void OPPROTO op_fldpi_ST0(void)
1841 void OPPROTO op_fldlg2_ST0(void)
1846 void OPPROTO op_fldln2_ST0(void)
1851 void OPPROTO op_fldz_ST0(void)
1856 void OPPROTO op_fldz_FT0(void)
1861 /* associated heplers to reduce generated code length and to simplify
1862 relocation (FP constants are usually stored in .rodata section) */
1864 void OPPROTO op_f2xm1(void)
1869 void OPPROTO op_fyl2x(void)
1874 void OPPROTO op_fptan(void)
1879 void OPPROTO op_fpatan(void)
1884 void OPPROTO op_fxtract(void)
1889 void OPPROTO op_fprem1(void)
1895 void OPPROTO op_fprem(void)
1900 void OPPROTO op_fyl2xp1(void)
1905 void OPPROTO op_fsqrt(void)
1910 void OPPROTO op_fsincos(void)
1915 void OPPROTO op_frndint(void)
1920 void OPPROTO op_fscale(void)
1925 void OPPROTO op_fsin(void)
1930 void OPPROTO op_fcos(void)
1935 void OPPROTO op_fnstsw_A0(void)
1938 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1939 stw((void *)A0, fpus);
1942 void OPPROTO op_fnstsw_EAX(void)
1945 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1946 EAX = (EAX & 0xffff0000) | fpus;
1949 void OPPROTO op_fnstcw_A0(void)
1951 stw((void *)A0, env->fpuc);
1954 void OPPROTO op_fldcw_A0(void)
1957 env->fpuc = lduw((void *)A0);
1958 /* set rounding mode */
1959 switch(env->fpuc & RC_MASK) {
1962 rnd_type = FE_TONEAREST;
1965 rnd_type = FE_DOWNWARD;
1968 rnd_type = FE_UPWARD;
1971 rnd_type = FE_TOWARDZERO;
1974 fesetround(rnd_type);
1977 void OPPROTO op_fclex(void)
1979 env->fpus &= 0x7f00;
1982 void OPPROTO op_fninit(void)
1997 void OPPROTO op_fnstenv_A0(void)
1999 helper_fstenv((uint8_t *)A0, PARAM1);
2002 void OPPROTO op_fldenv_A0(void)
2004 helper_fldenv((uint8_t *)A0, PARAM1);
2007 void OPPROTO op_fnsave_A0(void)
2009 helper_fsave((uint8_t *)A0, PARAM1);
2012 void OPPROTO op_frstor_A0(void)
2014 helper_frstor((uint8_t *)A0, PARAM1);
2017 /* threading support */
2018 void OPPROTO op_lock(void)
2023 void OPPROTO op_unlock(void)