]> git.proxmox.com Git - mirror_qemu.git/blame - target-i386/translate.c
target-i386: Don't clobber s->cc_op in gen_update_cc_op
[mirror_qemu.git] / target-i386 / translate.c
CommitLineData
2c0262af
FB
1/*
2 * i386 translation
5fafdf24 3 *
2c0262af
FB
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
8167ee88 17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
2c0262af
FB
18 */
19#include <stdarg.h>
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
23#include <inttypes.h>
24#include <signal.h>
2c0262af
FB
25
26#include "cpu.h"
76cad711 27#include "disas/disas.h"
57fec1fe 28#include "tcg-op.h"
2c0262af 29
a7812ae4
PB
30#include "helper.h"
31#define GEN_HELPER 1
32#include "helper.h"
33
2c0262af
FB
34#define PREFIX_REPZ 0x01
35#define PREFIX_REPNZ 0x02
36#define PREFIX_LOCK 0x04
37#define PREFIX_DATA 0x08
38#define PREFIX_ADR 0x10
39
14ce26e7 40#ifdef TARGET_X86_64
14ce26e7
FB
41#define CODE64(s) ((s)->code64)
42#define REX_X(s) ((s)->rex_x)
43#define REX_B(s) ((s)->rex_b)
14ce26e7 44#else
14ce26e7
FB
45#define CODE64(s) 0
46#define REX_X(s) 0
47#define REX_B(s) 0
48#endif
49
57fec1fe
FB
50//#define MACRO_TEST 1
51
57fec1fe 52/* global register indexes */
a7812ae4 53static TCGv_ptr cpu_env;
f5847c91 54static TCGv cpu_A0, cpu_cc_src, cpu_cc_dst;
a7812ae4 55static TCGv_i32 cpu_cc_op;
cc739bb0 56static TCGv cpu_regs[CPU_NB_REGS];
1e4840bf
FB
57/* local temps */
58static TCGv cpu_T[2], cpu_T3;
57fec1fe 59/* local register indexes (only used inside old micro ops) */
a7812ae4
PB
60static TCGv cpu_tmp0, cpu_tmp4;
61static TCGv_ptr cpu_ptr0, cpu_ptr1;
62static TCGv_i32 cpu_tmp2_i32, cpu_tmp3_i32;
63static TCGv_i64 cpu_tmp1_i64;
bedda79c 64static TCGv cpu_tmp5;
57fec1fe 65
1a7ff922
PB
66static uint8_t gen_opc_cc_op[OPC_BUF_SIZE];
67
022c62cb 68#include "exec/gen-icount.h"
2e70f6ef 69
57fec1fe
FB
70#ifdef TARGET_X86_64
71static int x86_64_hregs;
ae063a68
FB
72#endif
73
2c0262af
FB
74typedef struct DisasContext {
75 /* current insn context */
76 int override; /* -1 if no override */
77 int prefix;
78 int aflag, dflag;
14ce26e7 79 target_ulong pc; /* pc = eip + cs_base */
2c0262af
FB
80 int is_jmp; /* 1 = means jump (stop translation), 2 means CPU
81 static state change (stop translation) */
82 /* current block context */
14ce26e7 83 target_ulong cs_base; /* base of CS segment */
2c0262af
FB
84 int pe; /* protected mode */
85 int code32; /* 32 bit code segment */
14ce26e7
FB
86#ifdef TARGET_X86_64
87 int lma; /* long mode active */
88 int code64; /* 64 bit code segment */
89 int rex_x, rex_b;
90#endif
2c0262af 91 int ss32; /* 32 bit stack segment */
fee71888 92 CCOp cc_op; /* current CC operation */
e207582f 93 bool cc_op_dirty;
2c0262af
FB
94 int addseg; /* non zero if either DS/ES/SS have a non zero base */
95 int f_st; /* currently unused */
96 int vm86; /* vm86 mode */
97 int cpl;
98 int iopl;
99 int tf; /* TF cpu flag */
34865134 100 int singlestep_enabled; /* "hardware" single step enabled */
2c0262af
FB
101 int jmp_opt; /* use direct block chaining for direct jumps */
102 int mem_index; /* select memory access functions */
c068688b 103 uint64_t flags; /* all execution flags */
2c0262af
FB
104 struct TranslationBlock *tb;
105 int popl_esp_hack; /* for correct popl with esp base handling */
14ce26e7
FB
106 int rip_offset; /* only used in x86_64, but left for simplicity */
107 int cpuid_features;
3d7374c5 108 int cpuid_ext_features;
e771edab 109 int cpuid_ext2_features;
12e26b75 110 int cpuid_ext3_features;
a9321a4d 111 int cpuid_7_0_ebx_features;
2c0262af
FB
112} DisasContext;
113
114static void gen_eob(DisasContext *s);
14ce26e7
FB
115static void gen_jmp(DisasContext *s, target_ulong eip);
116static void gen_jmp_tb(DisasContext *s, target_ulong eip, int tb_num);
2c0262af
FB
117
118/* i386 arith/logic operations */
119enum {
5fafdf24
TS
120 OP_ADDL,
121 OP_ORL,
122 OP_ADCL,
2c0262af 123 OP_SBBL,
5fafdf24
TS
124 OP_ANDL,
125 OP_SUBL,
126 OP_XORL,
2c0262af
FB
127 OP_CMPL,
128};
129
130/* i386 shift ops */
131enum {
5fafdf24
TS
132 OP_ROL,
133 OP_ROR,
134 OP_RCL,
135 OP_RCR,
136 OP_SHL,
137 OP_SHR,
2c0262af
FB
138 OP_SHL1, /* undocumented */
139 OP_SAR = 7,
140};
141
8e1c85e3
FB
142enum {
143 JCC_O,
144 JCC_B,
145 JCC_Z,
146 JCC_BE,
147 JCC_S,
148 JCC_P,
149 JCC_L,
150 JCC_LE,
151};
152
2c0262af
FB
153/* operand size */
154enum {
155 OT_BYTE = 0,
156 OT_WORD,
5fafdf24 157 OT_LONG,
2c0262af
FB
158 OT_QUAD,
159};
160
161enum {
162 /* I386 int registers */
163 OR_EAX, /* MUST be even numbered */
164 OR_ECX,
165 OR_EDX,
166 OR_EBX,
167 OR_ESP,
168 OR_EBP,
169 OR_ESI,
170 OR_EDI,
14ce26e7
FB
171
172 OR_TMP0 = 16, /* temporary operand register */
2c0262af
FB
173 OR_TMP1,
174 OR_A0, /* temporary register used when doing address evaluation */
2c0262af
FB
175};
176
e207582f 177static void set_cc_op(DisasContext *s, CCOp op)
3ca51d07 178{
e207582f
RH
179 if (s->cc_op != op) {
180 s->cc_op = op;
181 /* The DYNAMIC setting is translator only, and should never be
182 stored. Thus we always consider it clean. */
183 s->cc_op_dirty = (op != CC_OP_DYNAMIC);
184 }
185}
186
187static inline void gen_op_set_cc_op(int32_t val)
188{
189 tcg_gen_movi_i32(cpu_cc_op, val);
190}
191
192static void gen_update_cc_op(DisasContext *s)
193{
194 if (s->cc_op_dirty) {
195 gen_op_set_cc_op(s->cc_op);
196 s->cc_op_dirty = false;
197 }
3ca51d07
RH
198}
199
57fec1fe
FB
200static inline void gen_op_movl_T0_0(void)
201{
202 tcg_gen_movi_tl(cpu_T[0], 0);
203}
204
205static inline void gen_op_movl_T0_im(int32_t val)
206{
207 tcg_gen_movi_tl(cpu_T[0], val);
208}
209
210static inline void gen_op_movl_T0_imu(uint32_t val)
211{
212 tcg_gen_movi_tl(cpu_T[0], val);
213}
214
215static inline void gen_op_movl_T1_im(int32_t val)
216{
217 tcg_gen_movi_tl(cpu_T[1], val);
218}
219
220static inline void gen_op_movl_T1_imu(uint32_t val)
221{
222 tcg_gen_movi_tl(cpu_T[1], val);
223}
224
225static inline void gen_op_movl_A0_im(uint32_t val)
226{
227 tcg_gen_movi_tl(cpu_A0, val);
228}
229
230#ifdef TARGET_X86_64
231static inline void gen_op_movq_A0_im(int64_t val)
232{
233 tcg_gen_movi_tl(cpu_A0, val);
234}
235#endif
236
237static inline void gen_movtl_T0_im(target_ulong val)
238{
239 tcg_gen_movi_tl(cpu_T[0], val);
240}
241
242static inline void gen_movtl_T1_im(target_ulong val)
243{
244 tcg_gen_movi_tl(cpu_T[1], val);
245}
246
247static inline void gen_op_andl_T0_ffff(void)
248{
249 tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xffff);
250}
251
252static inline void gen_op_andl_T0_im(uint32_t val)
253{
254 tcg_gen_andi_tl(cpu_T[0], cpu_T[0], val);
255}
256
257static inline void gen_op_movl_T0_T1(void)
258{
259 tcg_gen_mov_tl(cpu_T[0], cpu_T[1]);
260}
261
262static inline void gen_op_andl_A0_ffff(void)
263{
264 tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffff);
265}
266
14ce26e7
FB
267#ifdef TARGET_X86_64
268
269#define NB_OP_SIZES 4
270
14ce26e7
FB
271#else /* !TARGET_X86_64 */
272
273#define NB_OP_SIZES 3
274
14ce26e7
FB
275#endif /* !TARGET_X86_64 */
276
e2542fe2 277#if defined(HOST_WORDS_BIGENDIAN)
57fec1fe
FB
278#define REG_B_OFFSET (sizeof(target_ulong) - 1)
279#define REG_H_OFFSET (sizeof(target_ulong) - 2)
280#define REG_W_OFFSET (sizeof(target_ulong) - 2)
281#define REG_L_OFFSET (sizeof(target_ulong) - 4)
282#define REG_LH_OFFSET (sizeof(target_ulong) - 8)
14ce26e7 283#else
57fec1fe
FB
284#define REG_B_OFFSET 0
285#define REG_H_OFFSET 1
286#define REG_W_OFFSET 0
287#define REG_L_OFFSET 0
288#define REG_LH_OFFSET 4
14ce26e7 289#endif
57fec1fe 290
96d7073f
PM
291/* In instruction encodings for byte register accesses the
292 * register number usually indicates "low 8 bits of register N";
293 * however there are some special cases where N 4..7 indicates
294 * [AH, CH, DH, BH], ie "bits 15..8 of register N-4". Return
295 * true for this special case, false otherwise.
296 */
297static inline bool byte_reg_is_xH(int reg)
298{
299 if (reg < 4) {
300 return false;
301 }
302#ifdef TARGET_X86_64
303 if (reg >= 8 || x86_64_hregs) {
304 return false;
305 }
306#endif
307 return true;
308}
309
1e4840bf 310static inline void gen_op_mov_reg_v(int ot, int reg, TCGv t0)
57fec1fe
FB
311{
312 switch(ot) {
313 case OT_BYTE:
96d7073f 314 if (!byte_reg_is_xH(reg)) {
c832e3de 315 tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], t0, 0, 8);
57fec1fe 316 } else {
c832e3de 317 tcg_gen_deposit_tl(cpu_regs[reg - 4], cpu_regs[reg - 4], t0, 8, 8);
57fec1fe
FB
318 }
319 break;
320 case OT_WORD:
c832e3de 321 tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], t0, 0, 16);
57fec1fe 322 break;
cc739bb0 323 default: /* XXX this shouldn't be reached; abort? */
57fec1fe 324 case OT_LONG:
cc739bb0
LD
325 /* For x86_64, this sets the higher half of register to zero.
326 For i386, this is equivalent to a mov. */
327 tcg_gen_ext32u_tl(cpu_regs[reg], t0);
57fec1fe 328 break;
cc739bb0 329#ifdef TARGET_X86_64
57fec1fe 330 case OT_QUAD:
cc739bb0 331 tcg_gen_mov_tl(cpu_regs[reg], t0);
57fec1fe 332 break;
14ce26e7 333#endif
57fec1fe
FB
334 }
335}
2c0262af 336
57fec1fe
FB
337static inline void gen_op_mov_reg_T0(int ot, int reg)
338{
1e4840bf 339 gen_op_mov_reg_v(ot, reg, cpu_T[0]);
57fec1fe
FB
340}
341
342static inline void gen_op_mov_reg_T1(int ot, int reg)
343{
1e4840bf 344 gen_op_mov_reg_v(ot, reg, cpu_T[1]);
57fec1fe
FB
345}
346
347static inline void gen_op_mov_reg_A0(int size, int reg)
348{
349 switch(size) {
93ab25d7 350 case OT_BYTE:
c832e3de 351 tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], cpu_A0, 0, 16);
57fec1fe 352 break;
cc739bb0 353 default: /* XXX this shouldn't be reached; abort? */
93ab25d7 354 case OT_WORD:
cc739bb0
LD
355 /* For x86_64, this sets the higher half of register to zero.
356 For i386, this is equivalent to a mov. */
357 tcg_gen_ext32u_tl(cpu_regs[reg], cpu_A0);
57fec1fe 358 break;
cc739bb0 359#ifdef TARGET_X86_64
93ab25d7 360 case OT_LONG:
cc739bb0 361 tcg_gen_mov_tl(cpu_regs[reg], cpu_A0);
57fec1fe 362 break;
14ce26e7 363#endif
57fec1fe
FB
364 }
365}
366
1e4840bf 367static inline void gen_op_mov_v_reg(int ot, TCGv t0, int reg)
57fec1fe 368{
96d7073f
PM
369 if (ot == OT_BYTE && byte_reg_is_xH(reg)) {
370 tcg_gen_shri_tl(t0, cpu_regs[reg - 4], 8);
371 tcg_gen_ext8u_tl(t0, t0);
372 } else {
cc739bb0 373 tcg_gen_mov_tl(t0, cpu_regs[reg]);
57fec1fe
FB
374 }
375}
376
1e4840bf
FB
377static inline void gen_op_mov_TN_reg(int ot, int t_index, int reg)
378{
379 gen_op_mov_v_reg(ot, cpu_T[t_index], reg);
380}
381
57fec1fe
FB
382static inline void gen_op_movl_A0_reg(int reg)
383{
cc739bb0 384 tcg_gen_mov_tl(cpu_A0, cpu_regs[reg]);
57fec1fe
FB
385}
386
387static inline void gen_op_addl_A0_im(int32_t val)
388{
389 tcg_gen_addi_tl(cpu_A0, cpu_A0, val);
14ce26e7 390#ifdef TARGET_X86_64
57fec1fe 391 tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffffffff);
14ce26e7 392#endif
57fec1fe 393}
2c0262af 394
14ce26e7 395#ifdef TARGET_X86_64
57fec1fe
FB
396static inline void gen_op_addq_A0_im(int64_t val)
397{
398 tcg_gen_addi_tl(cpu_A0, cpu_A0, val);
399}
14ce26e7 400#endif
57fec1fe
FB
401
402static void gen_add_A0_im(DisasContext *s, int val)
403{
404#ifdef TARGET_X86_64
405 if (CODE64(s))
406 gen_op_addq_A0_im(val);
407 else
408#endif
409 gen_op_addl_A0_im(val);
410}
2c0262af 411
57fec1fe 412static inline void gen_op_addl_T0_T1(void)
2c0262af 413{
57fec1fe
FB
414 tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
415}
416
417static inline void gen_op_jmp_T0(void)
418{
317ac620 419 tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, eip));
57fec1fe
FB
420}
421
6e0d8677 422static inline void gen_op_add_reg_im(int size, int reg, int32_t val)
57fec1fe 423{
6e0d8677 424 switch(size) {
93ab25d7 425 case OT_BYTE:
cc739bb0 426 tcg_gen_addi_tl(cpu_tmp0, cpu_regs[reg], val);
c832e3de 427 tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], cpu_tmp0, 0, 16);
6e0d8677 428 break;
93ab25d7 429 case OT_WORD:
cc739bb0
LD
430 tcg_gen_addi_tl(cpu_tmp0, cpu_regs[reg], val);
431 /* For x86_64, this sets the higher half of register to zero.
432 For i386, this is equivalent to a nop. */
433 tcg_gen_ext32u_tl(cpu_tmp0, cpu_tmp0);
434 tcg_gen_mov_tl(cpu_regs[reg], cpu_tmp0);
6e0d8677
FB
435 break;
436#ifdef TARGET_X86_64
93ab25d7 437 case OT_LONG:
cc739bb0 438 tcg_gen_addi_tl(cpu_regs[reg], cpu_regs[reg], val);
6e0d8677
FB
439 break;
440#endif
441 }
57fec1fe
FB
442}
443
6e0d8677 444static inline void gen_op_add_reg_T0(int size, int reg)
57fec1fe 445{
6e0d8677 446 switch(size) {
93ab25d7 447 case OT_BYTE:
cc739bb0 448 tcg_gen_add_tl(cpu_tmp0, cpu_regs[reg], cpu_T[0]);
c832e3de 449 tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], cpu_tmp0, 0, 16);
6e0d8677 450 break;
93ab25d7 451 case OT_WORD:
cc739bb0
LD
452 tcg_gen_add_tl(cpu_tmp0, cpu_regs[reg], cpu_T[0]);
453 /* For x86_64, this sets the higher half of register to zero.
454 For i386, this is equivalent to a nop. */
455 tcg_gen_ext32u_tl(cpu_tmp0, cpu_tmp0);
456 tcg_gen_mov_tl(cpu_regs[reg], cpu_tmp0);
6e0d8677 457 break;
14ce26e7 458#ifdef TARGET_X86_64
93ab25d7 459 case OT_LONG:
cc739bb0 460 tcg_gen_add_tl(cpu_regs[reg], cpu_regs[reg], cpu_T[0]);
6e0d8677 461 break;
14ce26e7 462#endif
6e0d8677
FB
463 }
464}
57fec1fe 465
57fec1fe
FB
466static inline void gen_op_addl_A0_reg_sN(int shift, int reg)
467{
cc739bb0
LD
468 tcg_gen_mov_tl(cpu_tmp0, cpu_regs[reg]);
469 if (shift != 0)
57fec1fe
FB
470 tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, shift);
471 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
cc739bb0
LD
472 /* For x86_64, this sets the higher half of register to zero.
473 For i386, this is equivalent to a nop. */
474 tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
57fec1fe 475}
2c0262af 476
57fec1fe
FB
477static inline void gen_op_movl_A0_seg(int reg)
478{
317ac620 479 tcg_gen_ld32u_tl(cpu_A0, cpu_env, offsetof(CPUX86State, segs[reg].base) + REG_L_OFFSET);
57fec1fe 480}
2c0262af 481
7162ab21 482static inline void gen_op_addl_A0_seg(DisasContext *s, int reg)
57fec1fe 483{
317ac620 484 tcg_gen_ld_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, segs[reg].base));
57fec1fe 485#ifdef TARGET_X86_64
7162ab21
VC
486 if (CODE64(s)) {
487 tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffffffff);
488 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
489 } else {
490 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
491 tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffffffff);
492 }
493#else
494 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
57fec1fe
FB
495#endif
496}
2c0262af 497
14ce26e7 498#ifdef TARGET_X86_64
57fec1fe
FB
499static inline void gen_op_movq_A0_seg(int reg)
500{
317ac620 501 tcg_gen_ld_tl(cpu_A0, cpu_env, offsetof(CPUX86State, segs[reg].base));
57fec1fe 502}
14ce26e7 503
57fec1fe
FB
504static inline void gen_op_addq_A0_seg(int reg)
505{
317ac620 506 tcg_gen_ld_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, segs[reg].base));
57fec1fe
FB
507 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
508}
509
510static inline void gen_op_movq_A0_reg(int reg)
511{
cc739bb0 512 tcg_gen_mov_tl(cpu_A0, cpu_regs[reg]);
57fec1fe
FB
513}
514
515static inline void gen_op_addq_A0_reg_sN(int shift, int reg)
516{
cc739bb0
LD
517 tcg_gen_mov_tl(cpu_tmp0, cpu_regs[reg]);
518 if (shift != 0)
57fec1fe
FB
519 tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, shift);
520 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
521}
14ce26e7
FB
522#endif
523
57fec1fe
FB
524static inline void gen_op_lds_T0_A0(int idx)
525{
526 int mem_index = (idx >> 2) - 1;
527 switch(idx & 3) {
93ab25d7 528 case OT_BYTE:
57fec1fe
FB
529 tcg_gen_qemu_ld8s(cpu_T[0], cpu_A0, mem_index);
530 break;
93ab25d7 531 case OT_WORD:
57fec1fe
FB
532 tcg_gen_qemu_ld16s(cpu_T[0], cpu_A0, mem_index);
533 break;
534 default:
93ab25d7 535 case OT_LONG:
57fec1fe
FB
536 tcg_gen_qemu_ld32s(cpu_T[0], cpu_A0, mem_index);
537 break;
538 }
539}
2c0262af 540
1e4840bf 541static inline void gen_op_ld_v(int idx, TCGv t0, TCGv a0)
57fec1fe
FB
542{
543 int mem_index = (idx >> 2) - 1;
544 switch(idx & 3) {
93ab25d7 545 case OT_BYTE:
1e4840bf 546 tcg_gen_qemu_ld8u(t0, a0, mem_index);
57fec1fe 547 break;
93ab25d7 548 case OT_WORD:
1e4840bf 549 tcg_gen_qemu_ld16u(t0, a0, mem_index);
57fec1fe 550 break;
93ab25d7 551 case OT_LONG:
1e4840bf 552 tcg_gen_qemu_ld32u(t0, a0, mem_index);
57fec1fe
FB
553 break;
554 default:
93ab25d7 555 case OT_QUAD:
a7812ae4
PB
556 /* Should never happen on 32-bit targets. */
557#ifdef TARGET_X86_64
1e4840bf 558 tcg_gen_qemu_ld64(t0, a0, mem_index);
a7812ae4 559#endif
57fec1fe
FB
560 break;
561 }
562}
2c0262af 563
1e4840bf
FB
564/* XXX: always use ldu or lds */
565static inline void gen_op_ld_T0_A0(int idx)
566{
567 gen_op_ld_v(idx, cpu_T[0], cpu_A0);
568}
569
57fec1fe
FB
570static inline void gen_op_ldu_T0_A0(int idx)
571{
1e4840bf 572 gen_op_ld_v(idx, cpu_T[0], cpu_A0);
57fec1fe 573}
2c0262af 574
57fec1fe 575static inline void gen_op_ld_T1_A0(int idx)
1e4840bf
FB
576{
577 gen_op_ld_v(idx, cpu_T[1], cpu_A0);
578}
579
580static inline void gen_op_st_v(int idx, TCGv t0, TCGv a0)
57fec1fe
FB
581{
582 int mem_index = (idx >> 2) - 1;
583 switch(idx & 3) {
93ab25d7 584 case OT_BYTE:
1e4840bf 585 tcg_gen_qemu_st8(t0, a0, mem_index);
57fec1fe 586 break;
93ab25d7 587 case OT_WORD:
1e4840bf 588 tcg_gen_qemu_st16(t0, a0, mem_index);
57fec1fe 589 break;
93ab25d7 590 case OT_LONG:
1e4840bf 591 tcg_gen_qemu_st32(t0, a0, mem_index);
57fec1fe
FB
592 break;
593 default:
93ab25d7 594 case OT_QUAD:
a7812ae4
PB
595 /* Should never happen on 32-bit targets. */
596#ifdef TARGET_X86_64
1e4840bf 597 tcg_gen_qemu_st64(t0, a0, mem_index);
a7812ae4 598#endif
57fec1fe
FB
599 break;
600 }
601}
4f31916f 602
57fec1fe
FB
603static inline void gen_op_st_T0_A0(int idx)
604{
1e4840bf 605 gen_op_st_v(idx, cpu_T[0], cpu_A0);
57fec1fe 606}
4f31916f 607
57fec1fe
FB
608static inline void gen_op_st_T1_A0(int idx)
609{
1e4840bf 610 gen_op_st_v(idx, cpu_T[1], cpu_A0);
57fec1fe 611}
4f31916f 612
14ce26e7
FB
613static inline void gen_jmp_im(target_ulong pc)
614{
57fec1fe 615 tcg_gen_movi_tl(cpu_tmp0, pc);
317ac620 616 tcg_gen_st_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, eip));
14ce26e7
FB
617}
618
2c0262af
FB
619static inline void gen_string_movl_A0_ESI(DisasContext *s)
620{
621 int override;
622
623 override = s->override;
14ce26e7
FB
624#ifdef TARGET_X86_64
625 if (s->aflag == 2) {
626 if (override >= 0) {
57fec1fe
FB
627 gen_op_movq_A0_seg(override);
628 gen_op_addq_A0_reg_sN(0, R_ESI);
14ce26e7 629 } else {
57fec1fe 630 gen_op_movq_A0_reg(R_ESI);
14ce26e7
FB
631 }
632 } else
633#endif
2c0262af
FB
634 if (s->aflag) {
635 /* 32 bit address */
636 if (s->addseg && override < 0)
637 override = R_DS;
638 if (override >= 0) {
57fec1fe
FB
639 gen_op_movl_A0_seg(override);
640 gen_op_addl_A0_reg_sN(0, R_ESI);
2c0262af 641 } else {
57fec1fe 642 gen_op_movl_A0_reg(R_ESI);
2c0262af
FB
643 }
644 } else {
645 /* 16 address, always override */
646 if (override < 0)
647 override = R_DS;
57fec1fe 648 gen_op_movl_A0_reg(R_ESI);
2c0262af 649 gen_op_andl_A0_ffff();
7162ab21 650 gen_op_addl_A0_seg(s, override);
2c0262af
FB
651 }
652}
653
654static inline void gen_string_movl_A0_EDI(DisasContext *s)
655{
14ce26e7
FB
656#ifdef TARGET_X86_64
657 if (s->aflag == 2) {
57fec1fe 658 gen_op_movq_A0_reg(R_EDI);
14ce26e7
FB
659 } else
660#endif
2c0262af
FB
661 if (s->aflag) {
662 if (s->addseg) {
57fec1fe
FB
663 gen_op_movl_A0_seg(R_ES);
664 gen_op_addl_A0_reg_sN(0, R_EDI);
2c0262af 665 } else {
57fec1fe 666 gen_op_movl_A0_reg(R_EDI);
2c0262af
FB
667 }
668 } else {
57fec1fe 669 gen_op_movl_A0_reg(R_EDI);
2c0262af 670 gen_op_andl_A0_ffff();
7162ab21 671 gen_op_addl_A0_seg(s, R_ES);
2c0262af
FB
672 }
673}
674
6e0d8677
FB
675static inline void gen_op_movl_T0_Dshift(int ot)
676{
317ac620 677 tcg_gen_ld32s_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, df));
6e0d8677 678 tcg_gen_shli_tl(cpu_T[0], cpu_T[0], ot);
2c0262af
FB
679};
680
d824df34 681static TCGv gen_ext_tl(TCGv dst, TCGv src, int size, bool sign)
6e0d8677 682{
d824df34 683 switch (size) {
6e0d8677 684 case OT_BYTE:
d824df34
PB
685 if (sign) {
686 tcg_gen_ext8s_tl(dst, src);
687 } else {
688 tcg_gen_ext8u_tl(dst, src);
689 }
690 return dst;
6e0d8677 691 case OT_WORD:
d824df34
PB
692 if (sign) {
693 tcg_gen_ext16s_tl(dst, src);
694 } else {
695 tcg_gen_ext16u_tl(dst, src);
696 }
697 return dst;
698#ifdef TARGET_X86_64
6e0d8677 699 case OT_LONG:
d824df34
PB
700 if (sign) {
701 tcg_gen_ext32s_tl(dst, src);
702 } else {
703 tcg_gen_ext32u_tl(dst, src);
704 }
705 return dst;
706#endif
6e0d8677 707 default:
d824df34 708 return src;
6e0d8677
FB
709 }
710}
3b46e624 711
d824df34
PB
712static void gen_extu(int ot, TCGv reg)
713{
714 gen_ext_tl(reg, reg, ot, false);
715}
716
6e0d8677
FB
717static void gen_exts(int ot, TCGv reg)
718{
d824df34 719 gen_ext_tl(reg, reg, ot, true);
6e0d8677 720}
2c0262af 721
6e0d8677
FB
722static inline void gen_op_jnz_ecx(int size, int label1)
723{
cc739bb0 724 tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
6e0d8677 725 gen_extu(size + 1, cpu_tmp0);
cb63669a 726 tcg_gen_brcondi_tl(TCG_COND_NE, cpu_tmp0, 0, label1);
6e0d8677
FB
727}
728
729static inline void gen_op_jz_ecx(int size, int label1)
730{
cc739bb0 731 tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
6e0d8677 732 gen_extu(size + 1, cpu_tmp0);
cb63669a 733 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, label1);
6e0d8677 734}
2c0262af 735
a7812ae4
PB
736static void gen_helper_in_func(int ot, TCGv v, TCGv_i32 n)
737{
738 switch (ot) {
93ab25d7
PB
739 case OT_BYTE:
740 gen_helper_inb(v, n);
741 break;
742 case OT_WORD:
743 gen_helper_inw(v, n);
744 break;
745 case OT_LONG:
746 gen_helper_inl(v, n);
747 break;
a7812ae4 748 }
a7812ae4 749}
2c0262af 750
a7812ae4
PB
751static void gen_helper_out_func(int ot, TCGv_i32 v, TCGv_i32 n)
752{
753 switch (ot) {
93ab25d7
PB
754 case OT_BYTE:
755 gen_helper_outb(v, n);
756 break;
757 case OT_WORD:
758 gen_helper_outw(v, n);
759 break;
760 case OT_LONG:
761 gen_helper_outl(v, n);
762 break;
a7812ae4 763 }
a7812ae4 764}
f115e911 765
b8b6a50b
FB
766static void gen_check_io(DisasContext *s, int ot, target_ulong cur_eip,
767 uint32_t svm_flags)
f115e911 768{
b8b6a50b
FB
769 int state_saved;
770 target_ulong next_eip;
771
772 state_saved = 0;
f115e911
FB
773 if (s->pe && (s->cpl > s->iopl || s->vm86)) {
774 if (s->cc_op != CC_OP_DYNAMIC)
775 gen_op_set_cc_op(s->cc_op);
14ce26e7 776 gen_jmp_im(cur_eip);
b8b6a50b 777 state_saved = 1;
b6abf97d 778 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
a7812ae4 779 switch (ot) {
93ab25d7 780 case OT_BYTE:
4a7443be
BS
781 gen_helper_check_iob(cpu_env, cpu_tmp2_i32);
782 break;
93ab25d7 783 case OT_WORD:
4a7443be
BS
784 gen_helper_check_iow(cpu_env, cpu_tmp2_i32);
785 break;
93ab25d7 786 case OT_LONG:
4a7443be
BS
787 gen_helper_check_iol(cpu_env, cpu_tmp2_i32);
788 break;
a7812ae4 789 }
b8b6a50b 790 }
872929aa 791 if(s->flags & HF_SVMI_MASK) {
b8b6a50b
FB
792 if (!state_saved) {
793 if (s->cc_op != CC_OP_DYNAMIC)
794 gen_op_set_cc_op(s->cc_op);
795 gen_jmp_im(cur_eip);
b8b6a50b
FB
796 }
797 svm_flags |= (1 << (4 + ot));
798 next_eip = s->pc - s->cs_base;
b6abf97d 799 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
052e80d5
BS
800 gen_helper_svm_check_io(cpu_env, cpu_tmp2_i32,
801 tcg_const_i32(svm_flags),
a7812ae4 802 tcg_const_i32(next_eip - cur_eip));
f115e911
FB
803 }
804}
805
2c0262af
FB
806static inline void gen_movs(DisasContext *s, int ot)
807{
808 gen_string_movl_A0_ESI(s);
57fec1fe 809 gen_op_ld_T0_A0(ot + s->mem_index);
2c0262af 810 gen_string_movl_A0_EDI(s);
57fec1fe 811 gen_op_st_T0_A0(ot + s->mem_index);
6e0d8677
FB
812 gen_op_movl_T0_Dshift(ot);
813 gen_op_add_reg_T0(s->aflag, R_ESI);
814 gen_op_add_reg_T0(s->aflag, R_EDI);
2c0262af
FB
815}
816
b6abf97d
FB
817static void gen_op_update1_cc(void)
818{
819 tcg_gen_discard_tl(cpu_cc_src);
820 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
821}
822
823static void gen_op_update2_cc(void)
824{
825 tcg_gen_mov_tl(cpu_cc_src, cpu_T[1]);
826 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
827}
828
829static inline void gen_op_cmpl_T0_T1_cc(void)
830{
831 tcg_gen_mov_tl(cpu_cc_src, cpu_T[1]);
832 tcg_gen_sub_tl(cpu_cc_dst, cpu_T[0], cpu_T[1]);
833}
834
835static inline void gen_op_testl_T0_T1_cc(void)
836{
837 tcg_gen_discard_tl(cpu_cc_src);
838 tcg_gen_and_tl(cpu_cc_dst, cpu_T[0], cpu_T[1]);
839}
840
841static void gen_op_update_neg_cc(void)
842{
843 tcg_gen_neg_tl(cpu_cc_src, cpu_T[0]);
844 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
845}
846
8e1c85e3 847/* compute eflags.C to reg */
c7b3c873 848static void gen_compute_eflags_c(DisasContext *s, TCGv reg)
8e1c85e3 849{
c7b3c873
PB
850 if (s->cc_op != CC_OP_DYNAMIC) {
851 gen_op_set_cc_op(s->cc_op);
852 }
f0967a1a 853 gen_helper_cc_compute_c(cpu_tmp2_i32, cpu_env, cpu_cc_op);
8e1c85e3
FB
854 tcg_gen_extu_i32_tl(reg, cpu_tmp2_i32);
855}
856
c7b3c873
PB
857/* compute all eflags to reg */
858static void gen_compute_eflags(DisasContext *s, TCGv reg)
8e1c85e3 859{
c7b3c873
PB
860 if (s->cc_op != CC_OP_DYNAMIC) {
861 gen_op_set_cc_op(s->cc_op);
862 }
f0967a1a 863 gen_helper_cc_compute_all(cpu_tmp2_i32, cpu_env, cpu_cc_op);
c7b3c873
PB
864 if (TCGV_EQUAL(reg, cpu_cc_src)) {
865 tcg_gen_discard_tl(cpu_cc_dst);
3ca51d07 866 set_cc_op(s, CC_OP_EFLAGS);
c7b3c873 867 }
8e1c85e3
FB
868 tcg_gen_extu_i32_tl(reg, cpu_tmp2_i32);
869}
870
1e4840bf 871static inline void gen_setcc_slow_T0(DisasContext *s, int jcc_op)
8e1c85e3 872{
1e4840bf 873 switch(jcc_op) {
8e1c85e3 874 case JCC_O:
c7b3c873 875 gen_compute_eflags(s, cpu_T[0]);
8e1c85e3
FB
876 tcg_gen_shri_tl(cpu_T[0], cpu_T[0], 11);
877 tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 1);
878 break;
879 case JCC_B:
c7b3c873 880 gen_compute_eflags_c(s, cpu_T[0]);
8e1c85e3
FB
881 break;
882 case JCC_Z:
c7b3c873 883 gen_compute_eflags(s, cpu_T[0]);
8e1c85e3
FB
884 tcg_gen_shri_tl(cpu_T[0], cpu_T[0], 6);
885 tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 1);
886 break;
887 case JCC_BE:
c7b3c873 888 gen_compute_eflags(s, cpu_tmp0);
8e1c85e3
FB
889 tcg_gen_shri_tl(cpu_T[0], cpu_tmp0, 6);
890 tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
891 tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 1);
892 break;
893 case JCC_S:
c7b3c873 894 gen_compute_eflags(s, cpu_T[0]);
8e1c85e3
FB
895 tcg_gen_shri_tl(cpu_T[0], cpu_T[0], 7);
896 tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 1);
897 break;
898 case JCC_P:
c7b3c873 899 gen_compute_eflags(s, cpu_T[0]);
8e1c85e3
FB
900 tcg_gen_shri_tl(cpu_T[0], cpu_T[0], 2);
901 tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 1);
902 break;
903 case JCC_L:
c7b3c873 904 gen_compute_eflags(s, cpu_tmp0);
8e1c85e3
FB
905 tcg_gen_shri_tl(cpu_T[0], cpu_tmp0, 11); /* CC_O */
906 tcg_gen_shri_tl(cpu_tmp0, cpu_tmp0, 7); /* CC_S */
907 tcg_gen_xor_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
908 tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 1);
909 break;
910 default:
911 case JCC_LE:
c7b3c873 912 gen_compute_eflags(s, cpu_tmp0);
8e1c85e3
FB
913 tcg_gen_shri_tl(cpu_T[0], cpu_tmp0, 11); /* CC_O */
914 tcg_gen_shri_tl(cpu_tmp4, cpu_tmp0, 7); /* CC_S */
915 tcg_gen_shri_tl(cpu_tmp0, cpu_tmp0, 6); /* CC_Z */
916 tcg_gen_xor_tl(cpu_T[0], cpu_T[0], cpu_tmp4);
917 tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
918 tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 1);
919 break;
920 }
921}
922
923/* return true if setcc_slow is not needed (WARNING: must be kept in
924 sync with gen_jcc1) */
925static int is_fast_jcc_case(DisasContext *s, int b)
926{
927 int jcc_op;
928 jcc_op = (b >> 1) & 7;
929 switch(s->cc_op) {
930 /* we optimize the cmp/jcc case */
931 case CC_OP_SUBB:
932 case CC_OP_SUBW:
933 case CC_OP_SUBL:
934 case CC_OP_SUBQ:
935 if (jcc_op == JCC_O || jcc_op == JCC_P)
936 goto slow_jcc;
937 break;
938
939 /* some jumps are easy to compute */
940 case CC_OP_ADDB:
941 case CC_OP_ADDW:
942 case CC_OP_ADDL:
943 case CC_OP_ADDQ:
944
945 case CC_OP_LOGICB:
946 case CC_OP_LOGICW:
947 case CC_OP_LOGICL:
948 case CC_OP_LOGICQ:
949
950 case CC_OP_INCB:
951 case CC_OP_INCW:
952 case CC_OP_INCL:
953 case CC_OP_INCQ:
954
955 case CC_OP_DECB:
956 case CC_OP_DECW:
957 case CC_OP_DECL:
958 case CC_OP_DECQ:
959
960 case CC_OP_SHLB:
961 case CC_OP_SHLW:
962 case CC_OP_SHLL:
963 case CC_OP_SHLQ:
964 if (jcc_op != JCC_Z && jcc_op != JCC_S)
965 goto slow_jcc;
966 break;
967 default:
968 slow_jcc:
969 return 0;
970 }
971 return 1;
972}
973
974/* generate a conditional jump to label 'l1' according to jump opcode
975 value 'b'. In the fast case, T0 is guaranted not to be used. */
b27fc131 976static inline void gen_jcc1(DisasContext *s, int b, int l1)
8e1c85e3
FB
977{
978 int inv, jcc_op, size, cond;
979 TCGv t0;
980
981 inv = b & 1;
982 jcc_op = (b >> 1) & 7;
983
b27fc131 984 switch (s->cc_op) {
8e1c85e3
FB
985 /* we optimize the cmp/jcc case */
986 case CC_OP_SUBB:
987 case CC_OP_SUBW:
988 case CC_OP_SUBL:
989 case CC_OP_SUBQ:
990
b27fc131 991 size = s->cc_op - CC_OP_SUBB;
8e1c85e3
FB
992 switch(jcc_op) {
993 case JCC_Z:
994 fast_jcc_z:
d824df34 995 t0 = gen_ext_tl(cpu_tmp0, cpu_cc_dst, size, false);
cb63669a 996 tcg_gen_brcondi_tl(inv ? TCG_COND_NE : TCG_COND_EQ, t0, 0, l1);
8e1c85e3
FB
997 break;
998 case JCC_S:
999 fast_jcc_s:
d824df34
PB
1000 t0 = gen_ext_tl(cpu_tmp0, cpu_cc_dst, size, true);
1001 tcg_gen_brcondi_tl(inv ? TCG_COND_GE : TCG_COND_LT, t0, 0, l1);
8e1c85e3 1002 break;
d824df34 1003
8e1c85e3
FB
1004 case JCC_B:
1005 cond = inv ? TCG_COND_GEU : TCG_COND_LTU;
1006 goto fast_jcc_b;
1007 case JCC_BE:
1008 cond = inv ? TCG_COND_GTU : TCG_COND_LEU;
1009 fast_jcc_b:
1010 tcg_gen_add_tl(cpu_tmp4, cpu_cc_dst, cpu_cc_src);
d824df34
PB
1011 gen_extu(size, cpu_tmp4);
1012 t0 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, false);
8e1c85e3
FB
1013 tcg_gen_brcond_tl(cond, cpu_tmp4, t0, l1);
1014 break;
1015
1016 case JCC_L:
1017 cond = inv ? TCG_COND_GE : TCG_COND_LT;
1018 goto fast_jcc_l;
1019 case JCC_LE:
1020 cond = inv ? TCG_COND_GT : TCG_COND_LE;
1021 fast_jcc_l:
1022 tcg_gen_add_tl(cpu_tmp4, cpu_cc_dst, cpu_cc_src);
d824df34
PB
1023 gen_exts(size, cpu_tmp4);
1024 t0 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, true);
8e1c85e3
FB
1025 tcg_gen_brcond_tl(cond, cpu_tmp4, t0, l1);
1026 break;
1027
1028 default:
1029 goto slow_jcc;
1030 }
1031 break;
1032
1033 /* some jumps are easy to compute */
1034 case CC_OP_ADDB:
1035 case CC_OP_ADDW:
1036 case CC_OP_ADDL:
1037 case CC_OP_ADDQ:
1038
1039 case CC_OP_ADCB:
1040 case CC_OP_ADCW:
1041 case CC_OP_ADCL:
1042 case CC_OP_ADCQ:
1043
1044 case CC_OP_SBBB:
1045 case CC_OP_SBBW:
1046 case CC_OP_SBBL:
1047 case CC_OP_SBBQ:
1048
1049 case CC_OP_LOGICB:
1050 case CC_OP_LOGICW:
1051 case CC_OP_LOGICL:
1052 case CC_OP_LOGICQ:
1053
1054 case CC_OP_INCB:
1055 case CC_OP_INCW:
1056 case CC_OP_INCL:
1057 case CC_OP_INCQ:
1058
1059 case CC_OP_DECB:
1060 case CC_OP_DECW:
1061 case CC_OP_DECL:
1062 case CC_OP_DECQ:
1063
1064 case CC_OP_SHLB:
1065 case CC_OP_SHLW:
1066 case CC_OP_SHLL:
1067 case CC_OP_SHLQ:
1068
1069 case CC_OP_SARB:
1070 case CC_OP_SARW:
1071 case CC_OP_SARL:
1072 case CC_OP_SARQ:
1073 switch(jcc_op) {
1074 case JCC_Z:
b27fc131 1075 size = (s->cc_op - CC_OP_ADDB) & 3;
8e1c85e3
FB
1076 goto fast_jcc_z;
1077 case JCC_S:
b27fc131 1078 size = (s->cc_op - CC_OP_ADDB) & 3;
8e1c85e3
FB
1079 goto fast_jcc_s;
1080 default:
1081 goto slow_jcc;
1082 }
1083 break;
1084 default:
1085 slow_jcc:
1e4840bf 1086 gen_setcc_slow_T0(s, jcc_op);
cb63669a
PB
1087 tcg_gen_brcondi_tl(inv ? TCG_COND_EQ : TCG_COND_NE,
1088 cpu_T[0], 0, l1);
8e1c85e3
FB
1089 break;
1090 }
1091}
1092
14ce26e7
FB
1093/* XXX: does not work with gdbstub "ice" single step - not a
1094 serious problem */
1095static int gen_jz_ecx_string(DisasContext *s, target_ulong next_eip)
2c0262af 1096{
14ce26e7
FB
1097 int l1, l2;
1098
1099 l1 = gen_new_label();
1100 l2 = gen_new_label();
6e0d8677 1101 gen_op_jnz_ecx(s->aflag, l1);
14ce26e7
FB
1102 gen_set_label(l2);
1103 gen_jmp_tb(s, next_eip, 1);
1104 gen_set_label(l1);
1105 return l2;
2c0262af
FB
1106}
1107
1108static inline void gen_stos(DisasContext *s, int ot)
1109{
57fec1fe 1110 gen_op_mov_TN_reg(OT_LONG, 0, R_EAX);
2c0262af 1111 gen_string_movl_A0_EDI(s);
57fec1fe 1112 gen_op_st_T0_A0(ot + s->mem_index);
6e0d8677
FB
1113 gen_op_movl_T0_Dshift(ot);
1114 gen_op_add_reg_T0(s->aflag, R_EDI);
2c0262af
FB
1115}
1116
1117static inline void gen_lods(DisasContext *s, int ot)
1118{
1119 gen_string_movl_A0_ESI(s);
57fec1fe
FB
1120 gen_op_ld_T0_A0(ot + s->mem_index);
1121 gen_op_mov_reg_T0(ot, R_EAX);
6e0d8677
FB
1122 gen_op_movl_T0_Dshift(ot);
1123 gen_op_add_reg_T0(s->aflag, R_ESI);
2c0262af
FB
1124}
1125
1126static inline void gen_scas(DisasContext *s, int ot)
1127{
57fec1fe 1128 gen_op_mov_TN_reg(OT_LONG, 0, R_EAX);
2c0262af 1129 gen_string_movl_A0_EDI(s);
57fec1fe 1130 gen_op_ld_T1_A0(ot + s->mem_index);
2c0262af 1131 gen_op_cmpl_T0_T1_cc();
6e0d8677
FB
1132 gen_op_movl_T0_Dshift(ot);
1133 gen_op_add_reg_T0(s->aflag, R_EDI);
3ca51d07 1134 set_cc_op(s, CC_OP_SUBB + ot);
2c0262af
FB
1135}
1136
1137static inline void gen_cmps(DisasContext *s, int ot)
1138{
1139 gen_string_movl_A0_ESI(s);
57fec1fe 1140 gen_op_ld_T0_A0(ot + s->mem_index);
2c0262af 1141 gen_string_movl_A0_EDI(s);
57fec1fe 1142 gen_op_ld_T1_A0(ot + s->mem_index);
2c0262af 1143 gen_op_cmpl_T0_T1_cc();
6e0d8677
FB
1144 gen_op_movl_T0_Dshift(ot);
1145 gen_op_add_reg_T0(s->aflag, R_ESI);
1146 gen_op_add_reg_T0(s->aflag, R_EDI);
3ca51d07 1147 set_cc_op(s, CC_OP_SUBB + ot);
2c0262af
FB
1148}
1149
1150static inline void gen_ins(DisasContext *s, int ot)
1151{
2e70f6ef
PB
1152 if (use_icount)
1153 gen_io_start();
2c0262af 1154 gen_string_movl_A0_EDI(s);
6e0d8677
FB
1155 /* Note: we must do this dummy write first to be restartable in
1156 case of page fault. */
9772c73b 1157 gen_op_movl_T0_0();
57fec1fe 1158 gen_op_st_T0_A0(ot + s->mem_index);
b8b6a50b 1159 gen_op_mov_TN_reg(OT_WORD, 1, R_EDX);
b6abf97d
FB
1160 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[1]);
1161 tcg_gen_andi_i32(cpu_tmp2_i32, cpu_tmp2_i32, 0xffff);
a7812ae4 1162 gen_helper_in_func(ot, cpu_T[0], cpu_tmp2_i32);
57fec1fe 1163 gen_op_st_T0_A0(ot + s->mem_index);
6e0d8677
FB
1164 gen_op_movl_T0_Dshift(ot);
1165 gen_op_add_reg_T0(s->aflag, R_EDI);
2e70f6ef
PB
1166 if (use_icount)
1167 gen_io_end();
2c0262af
FB
1168}
1169
1170static inline void gen_outs(DisasContext *s, int ot)
1171{
2e70f6ef
PB
1172 if (use_icount)
1173 gen_io_start();
2c0262af 1174 gen_string_movl_A0_ESI(s);
57fec1fe 1175 gen_op_ld_T0_A0(ot + s->mem_index);
b8b6a50b
FB
1176
1177 gen_op_mov_TN_reg(OT_WORD, 1, R_EDX);
b6abf97d
FB
1178 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[1]);
1179 tcg_gen_andi_i32(cpu_tmp2_i32, cpu_tmp2_i32, 0xffff);
1180 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[0]);
a7812ae4 1181 gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
b8b6a50b 1182
6e0d8677
FB
1183 gen_op_movl_T0_Dshift(ot);
1184 gen_op_add_reg_T0(s->aflag, R_ESI);
2e70f6ef
PB
1185 if (use_icount)
1186 gen_io_end();
2c0262af
FB
1187}
1188
1189/* same method as Valgrind : we generate jumps to current or next
1190 instruction */
1191#define GEN_REPZ(op) \
1192static inline void gen_repz_ ## op(DisasContext *s, int ot, \
14ce26e7 1193 target_ulong cur_eip, target_ulong next_eip) \
2c0262af 1194{ \
14ce26e7 1195 int l2;\
2c0262af 1196 gen_update_cc_op(s); \
14ce26e7 1197 l2 = gen_jz_ecx_string(s, next_eip); \
2c0262af 1198 gen_ ## op(s, ot); \
6e0d8677 1199 gen_op_add_reg_im(s->aflag, R_ECX, -1); \
2c0262af
FB
1200 /* a loop would cause two single step exceptions if ECX = 1 \
1201 before rep string_insn */ \
1202 if (!s->jmp_opt) \
6e0d8677 1203 gen_op_jz_ecx(s->aflag, l2); \
2c0262af
FB
1204 gen_jmp(s, cur_eip); \
1205}
1206
1207#define GEN_REPZ2(op) \
1208static inline void gen_repz_ ## op(DisasContext *s, int ot, \
14ce26e7
FB
1209 target_ulong cur_eip, \
1210 target_ulong next_eip, \
2c0262af
FB
1211 int nz) \
1212{ \
14ce26e7 1213 int l2;\
2c0262af 1214 gen_update_cc_op(s); \
14ce26e7 1215 l2 = gen_jz_ecx_string(s, next_eip); \
2c0262af 1216 gen_ ## op(s, ot); \
6e0d8677 1217 gen_op_add_reg_im(s->aflag, R_ECX, -1); \
91642ff8 1218 gen_op_set_cc_op(s->cc_op); \
b27fc131 1219 gen_jcc1(s, (JCC_Z << 1) | (nz ^ 1), l2); \
2c0262af 1220 if (!s->jmp_opt) \
6e0d8677 1221 gen_op_jz_ecx(s->aflag, l2); \
2c0262af 1222 gen_jmp(s, cur_eip); \
3ca51d07 1223 set_cc_op(s, CC_OP_DYNAMIC); \
2c0262af
FB
1224}
1225
1226GEN_REPZ(movs)
1227GEN_REPZ(stos)
1228GEN_REPZ(lods)
1229GEN_REPZ(ins)
1230GEN_REPZ(outs)
1231GEN_REPZ2(scas)
1232GEN_REPZ2(cmps)
1233
a7812ae4
PB
1234static void gen_helper_fp_arith_ST0_FT0(int op)
1235{
1236 switch (op) {
d3eb5eae
BS
1237 case 0:
1238 gen_helper_fadd_ST0_FT0(cpu_env);
1239 break;
1240 case 1:
1241 gen_helper_fmul_ST0_FT0(cpu_env);
1242 break;
1243 case 2:
1244 gen_helper_fcom_ST0_FT0(cpu_env);
1245 break;
1246 case 3:
1247 gen_helper_fcom_ST0_FT0(cpu_env);
1248 break;
1249 case 4:
1250 gen_helper_fsub_ST0_FT0(cpu_env);
1251 break;
1252 case 5:
1253 gen_helper_fsubr_ST0_FT0(cpu_env);
1254 break;
1255 case 6:
1256 gen_helper_fdiv_ST0_FT0(cpu_env);
1257 break;
1258 case 7:
1259 gen_helper_fdivr_ST0_FT0(cpu_env);
1260 break;
a7812ae4
PB
1261 }
1262}
2c0262af
FB
1263
1264/* NOTE the exception in "r" op ordering */
a7812ae4
PB
1265static void gen_helper_fp_arith_STN_ST0(int op, int opreg)
1266{
1267 TCGv_i32 tmp = tcg_const_i32(opreg);
1268 switch (op) {
d3eb5eae
BS
1269 case 0:
1270 gen_helper_fadd_STN_ST0(cpu_env, tmp);
1271 break;
1272 case 1:
1273 gen_helper_fmul_STN_ST0(cpu_env, tmp);
1274 break;
1275 case 4:
1276 gen_helper_fsubr_STN_ST0(cpu_env, tmp);
1277 break;
1278 case 5:
1279 gen_helper_fsub_STN_ST0(cpu_env, tmp);
1280 break;
1281 case 6:
1282 gen_helper_fdivr_STN_ST0(cpu_env, tmp);
1283 break;
1284 case 7:
1285 gen_helper_fdiv_STN_ST0(cpu_env, tmp);
1286 break;
a7812ae4
PB
1287 }
1288}
2c0262af
FB
1289
1290/* if d == OR_TMP0, it means memory operand (address in A0) */
1291static void gen_op(DisasContext *s1, int op, int ot, int d)
1292{
2c0262af 1293 if (d != OR_TMP0) {
57fec1fe 1294 gen_op_mov_TN_reg(ot, 0, d);
2c0262af 1295 } else {
57fec1fe 1296 gen_op_ld_T0_A0(ot + s1->mem_index);
2c0262af
FB
1297 }
1298 switch(op) {
1299 case OP_ADCL:
c7b3c873 1300 gen_compute_eflags_c(s1, cpu_tmp4);
cad3a37d
FB
1301 tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1302 tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_tmp4);
1303 if (d != OR_TMP0)
1304 gen_op_mov_reg_T0(ot, d);
1305 else
1306 gen_op_st_T0_A0(ot + s1->mem_index);
1307 tcg_gen_mov_tl(cpu_cc_src, cpu_T[1]);
1308 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
1309 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_tmp4);
1310 tcg_gen_shli_i32(cpu_tmp2_i32, cpu_tmp2_i32, 2);
1311 tcg_gen_addi_i32(cpu_cc_op, cpu_tmp2_i32, CC_OP_ADDB + ot);
3ca51d07 1312 set_cc_op(s1, CC_OP_DYNAMIC);
cad3a37d 1313 break;
2c0262af 1314 case OP_SBBL:
c7b3c873 1315 gen_compute_eflags_c(s1, cpu_tmp4);
cad3a37d
FB
1316 tcg_gen_sub_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1317 tcg_gen_sub_tl(cpu_T[0], cpu_T[0], cpu_tmp4);
1318 if (d != OR_TMP0)
57fec1fe 1319 gen_op_mov_reg_T0(ot, d);
cad3a37d
FB
1320 else
1321 gen_op_st_T0_A0(ot + s1->mem_index);
1322 tcg_gen_mov_tl(cpu_cc_src, cpu_T[1]);
1323 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
1324 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_tmp4);
1325 tcg_gen_shli_i32(cpu_tmp2_i32, cpu_tmp2_i32, 2);
1326 tcg_gen_addi_i32(cpu_cc_op, cpu_tmp2_i32, CC_OP_SUBB + ot);
3ca51d07 1327 set_cc_op(s1, CC_OP_DYNAMIC);
cad3a37d 1328 break;
2c0262af
FB
1329 case OP_ADDL:
1330 gen_op_addl_T0_T1();
cad3a37d
FB
1331 if (d != OR_TMP0)
1332 gen_op_mov_reg_T0(ot, d);
1333 else
1334 gen_op_st_T0_A0(ot + s1->mem_index);
1335 gen_op_update2_cc();
3ca51d07 1336 set_cc_op(s1, CC_OP_ADDB + ot);
2c0262af
FB
1337 break;
1338 case OP_SUBL:
57fec1fe 1339 tcg_gen_sub_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
cad3a37d
FB
1340 if (d != OR_TMP0)
1341 gen_op_mov_reg_T0(ot, d);
1342 else
1343 gen_op_st_T0_A0(ot + s1->mem_index);
1344 gen_op_update2_cc();
3ca51d07 1345 set_cc_op(s1, CC_OP_SUBB + ot);
2c0262af
FB
1346 break;
1347 default:
1348 case OP_ANDL:
57fec1fe 1349 tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
cad3a37d
FB
1350 if (d != OR_TMP0)
1351 gen_op_mov_reg_T0(ot, d);
1352 else
1353 gen_op_st_T0_A0(ot + s1->mem_index);
1354 gen_op_update1_cc();
3ca51d07 1355 set_cc_op(s1, CC_OP_LOGICB + ot);
57fec1fe 1356 break;
2c0262af 1357 case OP_ORL:
57fec1fe 1358 tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
cad3a37d
FB
1359 if (d != OR_TMP0)
1360 gen_op_mov_reg_T0(ot, d);
1361 else
1362 gen_op_st_T0_A0(ot + s1->mem_index);
1363 gen_op_update1_cc();
3ca51d07 1364 set_cc_op(s1, CC_OP_LOGICB + ot);
57fec1fe 1365 break;
2c0262af 1366 case OP_XORL:
57fec1fe 1367 tcg_gen_xor_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
cad3a37d
FB
1368 if (d != OR_TMP0)
1369 gen_op_mov_reg_T0(ot, d);
1370 else
1371 gen_op_st_T0_A0(ot + s1->mem_index);
1372 gen_op_update1_cc();
3ca51d07 1373 set_cc_op(s1, CC_OP_LOGICB + ot);
2c0262af
FB
1374 break;
1375 case OP_CMPL:
1376 gen_op_cmpl_T0_T1_cc();
3ca51d07 1377 set_cc_op(s1, CC_OP_SUBB + ot);
2c0262af
FB
1378 break;
1379 }
b6abf97d
FB
1380}
1381
2c0262af
FB
1382/* if d == OR_TMP0, it means memory operand (address in A0) */
1383static void gen_inc(DisasContext *s1, int ot, int d, int c)
1384{
1385 if (d != OR_TMP0)
57fec1fe 1386 gen_op_mov_TN_reg(ot, 0, d);
2c0262af 1387 else
57fec1fe 1388 gen_op_ld_T0_A0(ot + s1->mem_index);
c7b3c873 1389 gen_compute_eflags_c(s1, cpu_cc_src);
2c0262af 1390 if (c > 0) {
b6abf97d 1391 tcg_gen_addi_tl(cpu_T[0], cpu_T[0], 1);
3ca51d07 1392 set_cc_op(s1, CC_OP_INCB + ot);
2c0262af 1393 } else {
b6abf97d 1394 tcg_gen_addi_tl(cpu_T[0], cpu_T[0], -1);
3ca51d07 1395 set_cc_op(s1, CC_OP_DECB + ot);
2c0262af
FB
1396 }
1397 if (d != OR_TMP0)
57fec1fe 1398 gen_op_mov_reg_T0(ot, d);
2c0262af 1399 else
57fec1fe 1400 gen_op_st_T0_A0(ot + s1->mem_index);
cd31fefa 1401 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
2c0262af
FB
1402}
1403
b6abf97d
FB
1404static void gen_shift_rm_T1(DisasContext *s, int ot, int op1,
1405 int is_right, int is_arith)
2c0262af 1406{
b6abf97d
FB
1407 target_ulong mask;
1408 int shift_label;
82786041 1409 TCGv t0, t1, t2;
1e4840bf 1410
82786041 1411 if (ot == OT_QUAD) {
b6abf97d 1412 mask = 0x3f;
82786041 1413 } else {
b6abf97d 1414 mask = 0x1f;
82786041 1415 }
3b46e624 1416
b6abf97d 1417 /* load */
82786041 1418 if (op1 == OR_TMP0) {
b6abf97d 1419 gen_op_ld_T0_A0(ot + s->mem_index);
82786041 1420 } else {
b6abf97d 1421 gen_op_mov_TN_reg(ot, 0, op1);
82786041 1422 }
b6abf97d 1423
82786041
RH
1424 t0 = tcg_temp_local_new();
1425 t1 = tcg_temp_local_new();
1426 t2 = tcg_temp_local_new();
b6abf97d 1427
82786041 1428 tcg_gen_andi_tl(t2, cpu_T[1], mask);
b6abf97d
FB
1429
1430 if (is_right) {
1431 if (is_arith) {
f484d386 1432 gen_exts(ot, cpu_T[0]);
82786041
RH
1433 tcg_gen_mov_tl(t0, cpu_T[0]);
1434 tcg_gen_sar_tl(cpu_T[0], cpu_T[0], t2);
b6abf97d 1435 } else {
cad3a37d 1436 gen_extu(ot, cpu_T[0]);
82786041
RH
1437 tcg_gen_mov_tl(t0, cpu_T[0]);
1438 tcg_gen_shr_tl(cpu_T[0], cpu_T[0], t2);
b6abf97d
FB
1439 }
1440 } else {
82786041
RH
1441 tcg_gen_mov_tl(t0, cpu_T[0]);
1442 tcg_gen_shl_tl(cpu_T[0], cpu_T[0], t2);
b6abf97d
FB
1443 }
1444
1445 /* store */
82786041 1446 if (op1 == OR_TMP0) {
b6abf97d 1447 gen_op_st_T0_A0(ot + s->mem_index);
82786041 1448 } else {
b6abf97d 1449 gen_op_mov_reg_T0(ot, op1);
82786041
RH
1450 }
1451
b6abf97d 1452 /* update eflags if non zero shift */
82786041 1453 if (s->cc_op != CC_OP_DYNAMIC) {
b6abf97d 1454 gen_op_set_cc_op(s->cc_op);
82786041 1455 }
b6abf97d 1456
82786041 1457 tcg_gen_mov_tl(t1, cpu_T[0]);
1e4840bf 1458
b6abf97d 1459 shift_label = gen_new_label();
82786041
RH
1460 tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, shift_label);
1461
1462 tcg_gen_addi_tl(t2, t2, -1);
1463 tcg_gen_mov_tl(cpu_cc_dst, t1);
1464
1465 if (is_right) {
1466 if (is_arith) {
1467 tcg_gen_sar_tl(cpu_cc_src, t0, t2);
1468 } else {
1469 tcg_gen_shr_tl(cpu_cc_src, t0, t2);
1470 }
1471 } else {
1472 tcg_gen_shl_tl(cpu_cc_src, t0, t2);
1473 }
b6abf97d 1474
82786041 1475 if (is_right) {
b6abf97d 1476 tcg_gen_movi_i32(cpu_cc_op, CC_OP_SARB + ot);
82786041 1477 } else {
b6abf97d 1478 tcg_gen_movi_i32(cpu_cc_op, CC_OP_SHLB + ot);
82786041
RH
1479 }
1480
b6abf97d 1481 gen_set_label(shift_label);
3ca51d07 1482 set_cc_op(s, CC_OP_DYNAMIC); /* cannot predict flags after */
1e4840bf
FB
1483
1484 tcg_temp_free(t0);
1485 tcg_temp_free(t1);
82786041 1486 tcg_temp_free(t2);
b6abf97d
FB
1487}
1488
c1c37968
FB
1489static void gen_shift_rm_im(DisasContext *s, int ot, int op1, int op2,
1490 int is_right, int is_arith)
1491{
1492 int mask;
1493
1494 if (ot == OT_QUAD)
1495 mask = 0x3f;
1496 else
1497 mask = 0x1f;
1498
1499 /* load */
1500 if (op1 == OR_TMP0)
1501 gen_op_ld_T0_A0(ot + s->mem_index);
1502 else
1503 gen_op_mov_TN_reg(ot, 0, op1);
1504
1505 op2 &= mask;
1506 if (op2 != 0) {
1507 if (is_right) {
1508 if (is_arith) {
1509 gen_exts(ot, cpu_T[0]);
2a449d14 1510 tcg_gen_sari_tl(cpu_tmp4, cpu_T[0], op2 - 1);
c1c37968
FB
1511 tcg_gen_sari_tl(cpu_T[0], cpu_T[0], op2);
1512 } else {
1513 gen_extu(ot, cpu_T[0]);
2a449d14 1514 tcg_gen_shri_tl(cpu_tmp4, cpu_T[0], op2 - 1);
c1c37968
FB
1515 tcg_gen_shri_tl(cpu_T[0], cpu_T[0], op2);
1516 }
1517 } else {
2a449d14 1518 tcg_gen_shli_tl(cpu_tmp4, cpu_T[0], op2 - 1);
c1c37968
FB
1519 tcg_gen_shli_tl(cpu_T[0], cpu_T[0], op2);
1520 }
1521 }
1522
1523 /* store */
1524 if (op1 == OR_TMP0)
1525 gen_op_st_T0_A0(ot + s->mem_index);
1526 else
1527 gen_op_mov_reg_T0(ot, op1);
1528
1529 /* update eflags if non zero shift */
1530 if (op2 != 0) {
2a449d14 1531 tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
c1c37968 1532 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
3ca51d07 1533 set_cc_op(s, (is_right ? CC_OP_SARB : CC_OP_SHLB) + ot);
c1c37968
FB
1534 }
1535}
1536
b6abf97d
FB
1537static inline void tcg_gen_lshift(TCGv ret, TCGv arg1, target_long arg2)
1538{
1539 if (arg2 >= 0)
1540 tcg_gen_shli_tl(ret, arg1, arg2);
1541 else
1542 tcg_gen_shri_tl(ret, arg1, -arg2);
1543}
1544
b6abf97d
FB
1545static void gen_rot_rm_T1(DisasContext *s, int ot, int op1,
1546 int is_right)
1547{
1548 target_ulong mask;
1549 int label1, label2, data_bits;
1e4840bf
FB
1550 TCGv t0, t1, t2, a0;
1551
1552 /* XXX: inefficient, but we must use local temps */
a7812ae4
PB
1553 t0 = tcg_temp_local_new();
1554 t1 = tcg_temp_local_new();
1555 t2 = tcg_temp_local_new();
1556 a0 = tcg_temp_local_new();
1e4840bf 1557
b6abf97d
FB
1558 if (ot == OT_QUAD)
1559 mask = 0x3f;
1560 else
1561 mask = 0x1f;
1562
1563 /* load */
1e4840bf
FB
1564 if (op1 == OR_TMP0) {
1565 tcg_gen_mov_tl(a0, cpu_A0);
1566 gen_op_ld_v(ot + s->mem_index, t0, a0);
1567 } else {
1568 gen_op_mov_v_reg(ot, t0, op1);
1569 }
b6abf97d 1570
1e4840bf
FB
1571 tcg_gen_mov_tl(t1, cpu_T[1]);
1572
1573 tcg_gen_andi_tl(t1, t1, mask);
b6abf97d
FB
1574
1575 /* Must test zero case to avoid using undefined behaviour in TCG
1576 shifts. */
1577 label1 = gen_new_label();
1e4840bf 1578 tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, label1);
b6abf97d
FB
1579
1580 if (ot <= OT_WORD)
1e4840bf 1581 tcg_gen_andi_tl(cpu_tmp0, t1, (1 << (3 + ot)) - 1);
b6abf97d 1582 else
1e4840bf 1583 tcg_gen_mov_tl(cpu_tmp0, t1);
b6abf97d 1584
1e4840bf
FB
1585 gen_extu(ot, t0);
1586 tcg_gen_mov_tl(t2, t0);
b6abf97d
FB
1587
1588 data_bits = 8 << ot;
1589 /* XXX: rely on behaviour of shifts when operand 2 overflows (XXX:
1590 fix TCG definition) */
1591 if (is_right) {
1e4840bf 1592 tcg_gen_shr_tl(cpu_tmp4, t0, cpu_tmp0);
5b207c00 1593 tcg_gen_subfi_tl(cpu_tmp0, data_bits, cpu_tmp0);
1e4840bf 1594 tcg_gen_shl_tl(t0, t0, cpu_tmp0);
b6abf97d 1595 } else {
1e4840bf 1596 tcg_gen_shl_tl(cpu_tmp4, t0, cpu_tmp0);
5b207c00 1597 tcg_gen_subfi_tl(cpu_tmp0, data_bits, cpu_tmp0);
1e4840bf 1598 tcg_gen_shr_tl(t0, t0, cpu_tmp0);
b6abf97d 1599 }
1e4840bf 1600 tcg_gen_or_tl(t0, t0, cpu_tmp4);
b6abf97d
FB
1601
1602 gen_set_label(label1);
1603 /* store */
1e4840bf
FB
1604 if (op1 == OR_TMP0) {
1605 gen_op_st_v(ot + s->mem_index, t0, a0);
1606 } else {
1607 gen_op_mov_reg_v(ot, op1, t0);
1608 }
b6abf97d 1609
0ff6addd 1610 /* update eflags. It is needed anyway most of the time, do it always. */
c7b3c873
PB
1611 gen_compute_eflags(s, cpu_cc_src);
1612 assert(s->cc_op == CC_OP_EFLAGS);
b6abf97d
FB
1613
1614 label2 = gen_new_label();
1e4840bf 1615 tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, label2);
b6abf97d 1616
b6abf97d 1617 tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~(CC_O | CC_C));
1e4840bf 1618 tcg_gen_xor_tl(cpu_tmp0, t2, t0);
b6abf97d
FB
1619 tcg_gen_lshift(cpu_tmp0, cpu_tmp0, 11 - (data_bits - 1));
1620 tcg_gen_andi_tl(cpu_tmp0, cpu_tmp0, CC_O);
1621 tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, cpu_tmp0);
1622 if (is_right) {
1e4840bf 1623 tcg_gen_shri_tl(t0, t0, data_bits - 1);
b6abf97d 1624 }
1e4840bf
FB
1625 tcg_gen_andi_tl(t0, t0, CC_C);
1626 tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, t0);
0ff6addd 1627
b6abf97d 1628 gen_set_label(label2);
1e4840bf
FB
1629
1630 tcg_temp_free(t0);
1631 tcg_temp_free(t1);
1632 tcg_temp_free(t2);
1633 tcg_temp_free(a0);
b6abf97d
FB
1634}
1635
8cd6345d 1636static void gen_rot_rm_im(DisasContext *s, int ot, int op1, int op2,
1637 int is_right)
1638{
1639 int mask;
1640 int data_bits;
1641 TCGv t0, t1, a0;
1642
1643 /* XXX: inefficient, but we must use local temps */
1644 t0 = tcg_temp_local_new();
1645 t1 = tcg_temp_local_new();
1646 a0 = tcg_temp_local_new();
1647
1648 if (ot == OT_QUAD)
1649 mask = 0x3f;
1650 else
1651 mask = 0x1f;
1652
1653 /* load */
1654 if (op1 == OR_TMP0) {
1655 tcg_gen_mov_tl(a0, cpu_A0);
1656 gen_op_ld_v(ot + s->mem_index, t0, a0);
1657 } else {
1658 gen_op_mov_v_reg(ot, t0, op1);
1659 }
1660
1661 gen_extu(ot, t0);
1662 tcg_gen_mov_tl(t1, t0);
1663
1664 op2 &= mask;
1665 data_bits = 8 << ot;
1666 if (op2 != 0) {
1667 int shift = op2 & ((1 << (3 + ot)) - 1);
1668 if (is_right) {
1669 tcg_gen_shri_tl(cpu_tmp4, t0, shift);
1670 tcg_gen_shli_tl(t0, t0, data_bits - shift);
1671 }
1672 else {
1673 tcg_gen_shli_tl(cpu_tmp4, t0, shift);
1674 tcg_gen_shri_tl(t0, t0, data_bits - shift);
1675 }
1676 tcg_gen_or_tl(t0, t0, cpu_tmp4);
1677 }
1678
1679 /* store */
1680 if (op1 == OR_TMP0) {
1681 gen_op_st_v(ot + s->mem_index, t0, a0);
1682 } else {
1683 gen_op_mov_reg_v(ot, op1, t0);
1684 }
1685
1686 if (op2 != 0) {
1687 /* update eflags */
c7b3c873
PB
1688 gen_compute_eflags(s, cpu_cc_src);
1689 assert(s->cc_op == CC_OP_EFLAGS);
0ff6addd 1690
8cd6345d 1691 tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~(CC_O | CC_C));
1692 tcg_gen_xor_tl(cpu_tmp0, t1, t0);
1693 tcg_gen_lshift(cpu_tmp0, cpu_tmp0, 11 - (data_bits - 1));
1694 tcg_gen_andi_tl(cpu_tmp0, cpu_tmp0, CC_O);
1695 tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, cpu_tmp0);
1696 if (is_right) {
1697 tcg_gen_shri_tl(t0, t0, data_bits - 1);
1698 }
1699 tcg_gen_andi_tl(t0, t0, CC_C);
1700 tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, t0);
8cd6345d 1701 }
1702
1703 tcg_temp_free(t0);
1704 tcg_temp_free(t1);
1705 tcg_temp_free(a0);
1706}
1707
b6abf97d
FB
1708/* XXX: add faster immediate = 1 case */
1709static void gen_rotc_rm_T1(DisasContext *s, int ot, int op1,
1710 int is_right)
1711{
b6abf97d
FB
1712 if (s->cc_op != CC_OP_DYNAMIC)
1713 gen_op_set_cc_op(s->cc_op);
c7b3c873
PB
1714 gen_compute_eflags(s, cpu_cc_src);
1715 assert(s->cc_op == CC_OP_EFLAGS);
b6abf97d
FB
1716
1717 /* load */
1718 if (op1 == OR_TMP0)
1719 gen_op_ld_T0_A0(ot + s->mem_index);
1720 else
1721 gen_op_mov_TN_reg(ot, 0, op1);
1722
a7812ae4
PB
1723 if (is_right) {
1724 switch (ot) {
93ab25d7 1725 case OT_BYTE:
7923057b
BS
1726 gen_helper_rcrb(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
1727 break;
93ab25d7 1728 case OT_WORD:
7923057b
BS
1729 gen_helper_rcrw(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
1730 break;
93ab25d7 1731 case OT_LONG:
7923057b
BS
1732 gen_helper_rcrl(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
1733 break;
a7812ae4 1734#ifdef TARGET_X86_64
93ab25d7 1735 case OT_QUAD:
7923057b
BS
1736 gen_helper_rcrq(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
1737 break;
a7812ae4
PB
1738#endif
1739 }
1740 } else {
1741 switch (ot) {
93ab25d7 1742 case OT_BYTE:
7923057b
BS
1743 gen_helper_rclb(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
1744 break;
93ab25d7 1745 case OT_WORD:
7923057b
BS
1746 gen_helper_rclw(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
1747 break;
93ab25d7 1748 case OT_LONG:
7923057b
BS
1749 gen_helper_rcll(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
1750 break;
a7812ae4 1751#ifdef TARGET_X86_64
93ab25d7 1752 case OT_QUAD:
7923057b
BS
1753 gen_helper_rclq(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
1754 break;
a7812ae4
PB
1755#endif
1756 }
1757 }
b6abf97d
FB
1758 /* store */
1759 if (op1 == OR_TMP0)
1760 gen_op_st_T0_A0(ot + s->mem_index);
1761 else
1762 gen_op_mov_reg_T0(ot, op1);
b6abf97d
FB
1763}
1764
1765/* XXX: add faster immediate case */
1766static void gen_shiftd_rm_T1_T3(DisasContext *s, int ot, int op1,
1767 int is_right)
1768{
1769 int label1, label2, data_bits;
1770 target_ulong mask;
1e4840bf
FB
1771 TCGv t0, t1, t2, a0;
1772
a7812ae4
PB
1773 t0 = tcg_temp_local_new();
1774 t1 = tcg_temp_local_new();
1775 t2 = tcg_temp_local_new();
1776 a0 = tcg_temp_local_new();
b6abf97d
FB
1777
1778 if (ot == OT_QUAD)
1779 mask = 0x3f;
1780 else
1781 mask = 0x1f;
1782
1783 /* load */
1e4840bf
FB
1784 if (op1 == OR_TMP0) {
1785 tcg_gen_mov_tl(a0, cpu_A0);
1786 gen_op_ld_v(ot + s->mem_index, t0, a0);
1787 } else {
1788 gen_op_mov_v_reg(ot, t0, op1);
1789 }
b6abf97d
FB
1790
1791 tcg_gen_andi_tl(cpu_T3, cpu_T3, mask);
1e4840bf
FB
1792
1793 tcg_gen_mov_tl(t1, cpu_T[1]);
1794 tcg_gen_mov_tl(t2, cpu_T3);
1795
b6abf97d
FB
1796 /* Must test zero case to avoid using undefined behaviour in TCG
1797 shifts. */
1798 label1 = gen_new_label();
1e4840bf 1799 tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, label1);
b6abf97d 1800
1e4840bf 1801 tcg_gen_addi_tl(cpu_tmp5, t2, -1);
b6abf97d
FB
1802 if (ot == OT_WORD) {
1803 /* Note: we implement the Intel behaviour for shift count > 16 */
1804 if (is_right) {
1e4840bf
FB
1805 tcg_gen_andi_tl(t0, t0, 0xffff);
1806 tcg_gen_shli_tl(cpu_tmp0, t1, 16);
1807 tcg_gen_or_tl(t0, t0, cpu_tmp0);
1808 tcg_gen_ext32u_tl(t0, t0);
b6abf97d 1809
1e4840bf 1810 tcg_gen_shr_tl(cpu_tmp4, t0, cpu_tmp5);
b6abf97d
FB
1811
1812 /* only needed if count > 16, but a test would complicate */
5b207c00 1813 tcg_gen_subfi_tl(cpu_tmp5, 32, t2);
1e4840bf 1814 tcg_gen_shl_tl(cpu_tmp0, t0, cpu_tmp5);
b6abf97d 1815
1e4840bf 1816 tcg_gen_shr_tl(t0, t0, t2);
b6abf97d 1817
1e4840bf 1818 tcg_gen_or_tl(t0, t0, cpu_tmp0);
b6abf97d
FB
1819 } else {
1820 /* XXX: not optimal */
1e4840bf
FB
1821 tcg_gen_andi_tl(t0, t0, 0xffff);
1822 tcg_gen_shli_tl(t1, t1, 16);
1823 tcg_gen_or_tl(t1, t1, t0);
1824 tcg_gen_ext32u_tl(t1, t1);
b6abf97d 1825
1e4840bf 1826 tcg_gen_shl_tl(cpu_tmp4, t0, cpu_tmp5);
5b207c00 1827 tcg_gen_subfi_tl(cpu_tmp0, 32, cpu_tmp5);
bedda79c
AJ
1828 tcg_gen_shr_tl(cpu_tmp5, t1, cpu_tmp0);
1829 tcg_gen_or_tl(cpu_tmp4, cpu_tmp4, cpu_tmp5);
b6abf97d 1830
1e4840bf 1831 tcg_gen_shl_tl(t0, t0, t2);
5b207c00 1832 tcg_gen_subfi_tl(cpu_tmp5, 32, t2);
1e4840bf
FB
1833 tcg_gen_shr_tl(t1, t1, cpu_tmp5);
1834 tcg_gen_or_tl(t0, t0, t1);
b6abf97d
FB
1835 }
1836 } else {
1837 data_bits = 8 << ot;
1838 if (is_right) {
1839 if (ot == OT_LONG)
1e4840bf 1840 tcg_gen_ext32u_tl(t0, t0);
b6abf97d 1841
1e4840bf 1842 tcg_gen_shr_tl(cpu_tmp4, t0, cpu_tmp5);
b6abf97d 1843
1e4840bf 1844 tcg_gen_shr_tl(t0, t0, t2);
5b207c00 1845 tcg_gen_subfi_tl(cpu_tmp5, data_bits, t2);
1e4840bf
FB
1846 tcg_gen_shl_tl(t1, t1, cpu_tmp5);
1847 tcg_gen_or_tl(t0, t0, t1);
b6abf97d
FB
1848
1849 } else {
1850 if (ot == OT_LONG)
1e4840bf 1851 tcg_gen_ext32u_tl(t1, t1);
b6abf97d 1852
1e4840bf 1853 tcg_gen_shl_tl(cpu_tmp4, t0, cpu_tmp5);
b6abf97d 1854
1e4840bf 1855 tcg_gen_shl_tl(t0, t0, t2);
5b207c00 1856 tcg_gen_subfi_tl(cpu_tmp5, data_bits, t2);
1e4840bf
FB
1857 tcg_gen_shr_tl(t1, t1, cpu_tmp5);
1858 tcg_gen_or_tl(t0, t0, t1);
b6abf97d
FB
1859 }
1860 }
1e4840bf 1861 tcg_gen_mov_tl(t1, cpu_tmp4);
b6abf97d
FB
1862
1863 gen_set_label(label1);
1864 /* store */
1e4840bf
FB
1865 if (op1 == OR_TMP0) {
1866 gen_op_st_v(ot + s->mem_index, t0, a0);
1867 } else {
1868 gen_op_mov_reg_v(ot, op1, t0);
1869 }
b6abf97d
FB
1870
1871 /* update eflags */
1872 if (s->cc_op != CC_OP_DYNAMIC)
1873 gen_op_set_cc_op(s->cc_op);
1874
1875 label2 = gen_new_label();
1e4840bf 1876 tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, label2);
b6abf97d 1877
1e4840bf
FB
1878 tcg_gen_mov_tl(cpu_cc_src, t1);
1879 tcg_gen_mov_tl(cpu_cc_dst, t0);
b6abf97d
FB
1880 if (is_right) {
1881 tcg_gen_movi_i32(cpu_cc_op, CC_OP_SARB + ot);
1882 } else {
1883 tcg_gen_movi_i32(cpu_cc_op, CC_OP_SHLB + ot);
1884 }
1885 gen_set_label(label2);
3ca51d07 1886 set_cc_op(s, CC_OP_DYNAMIC); /* cannot predict flags after */
1e4840bf
FB
1887
1888 tcg_temp_free(t0);
1889 tcg_temp_free(t1);
1890 tcg_temp_free(t2);
1891 tcg_temp_free(a0);
b6abf97d
FB
1892}
1893
1894static void gen_shift(DisasContext *s1, int op, int ot, int d, int s)
1895{
1896 if (s != OR_TMP1)
1897 gen_op_mov_TN_reg(ot, 1, s);
1898 switch(op) {
1899 case OP_ROL:
1900 gen_rot_rm_T1(s1, ot, d, 0);
1901 break;
1902 case OP_ROR:
1903 gen_rot_rm_T1(s1, ot, d, 1);
1904 break;
1905 case OP_SHL:
1906 case OP_SHL1:
1907 gen_shift_rm_T1(s1, ot, d, 0, 0);
1908 break;
1909 case OP_SHR:
1910 gen_shift_rm_T1(s1, ot, d, 1, 0);
1911 break;
1912 case OP_SAR:
1913 gen_shift_rm_T1(s1, ot, d, 1, 1);
1914 break;
1915 case OP_RCL:
1916 gen_rotc_rm_T1(s1, ot, d, 0);
1917 break;
1918 case OP_RCR:
1919 gen_rotc_rm_T1(s1, ot, d, 1);
1920 break;
1921 }
2c0262af
FB
1922}
1923
1924static void gen_shifti(DisasContext *s1, int op, int ot, int d, int c)
1925{
c1c37968 1926 switch(op) {
8cd6345d 1927 case OP_ROL:
1928 gen_rot_rm_im(s1, ot, d, c, 0);
1929 break;
1930 case OP_ROR:
1931 gen_rot_rm_im(s1, ot, d, c, 1);
1932 break;
c1c37968
FB
1933 case OP_SHL:
1934 case OP_SHL1:
1935 gen_shift_rm_im(s1, ot, d, c, 0, 0);
1936 break;
1937 case OP_SHR:
1938 gen_shift_rm_im(s1, ot, d, c, 1, 0);
1939 break;
1940 case OP_SAR:
1941 gen_shift_rm_im(s1, ot, d, c, 1, 1);
1942 break;
1943 default:
1944 /* currently not optimized */
1945 gen_op_movl_T1_im(c);
1946 gen_shift(s1, op, ot, d, OR_TMP1);
1947 break;
1948 }
2c0262af
FB
1949}
1950
0af10c86
BS
1951static void gen_lea_modrm(CPUX86State *env, DisasContext *s, int modrm,
1952 int *reg_ptr, int *offset_ptr)
2c0262af 1953{
14ce26e7 1954 target_long disp;
2c0262af 1955 int havesib;
14ce26e7 1956 int base;
2c0262af
FB
1957 int index;
1958 int scale;
1959 int opreg;
1960 int mod, rm, code, override, must_add_seg;
1961
1962 override = s->override;
1963 must_add_seg = s->addseg;
1964 if (override >= 0)
1965 must_add_seg = 1;
1966 mod = (modrm >> 6) & 3;
1967 rm = modrm & 7;
1968
1969 if (s->aflag) {
1970
1971 havesib = 0;
1972 base = rm;
1973 index = 0;
1974 scale = 0;
3b46e624 1975
2c0262af
FB
1976 if (base == 4) {
1977 havesib = 1;
0af10c86 1978 code = cpu_ldub_code(env, s->pc++);
2c0262af 1979 scale = (code >> 6) & 3;
14ce26e7
FB
1980 index = ((code >> 3) & 7) | REX_X(s);
1981 base = (code & 7);
2c0262af 1982 }
14ce26e7 1983 base |= REX_B(s);
2c0262af
FB
1984
1985 switch (mod) {
1986 case 0:
14ce26e7 1987 if ((base & 7) == 5) {
2c0262af 1988 base = -1;
0af10c86 1989 disp = (int32_t)cpu_ldl_code(env, s->pc);
2c0262af 1990 s->pc += 4;
14ce26e7
FB
1991 if (CODE64(s) && !havesib) {
1992 disp += s->pc + s->rip_offset;
1993 }
2c0262af
FB
1994 } else {
1995 disp = 0;
1996 }
1997 break;
1998 case 1:
0af10c86 1999 disp = (int8_t)cpu_ldub_code(env, s->pc++);
2c0262af
FB
2000 break;
2001 default:
2002 case 2:
0af10c86 2003 disp = (int32_t)cpu_ldl_code(env, s->pc);
2c0262af
FB
2004 s->pc += 4;
2005 break;
2006 }
3b46e624 2007
2c0262af
FB
2008 if (base >= 0) {
2009 /* for correct popl handling with esp */
2010 if (base == 4 && s->popl_esp_hack)
2011 disp += s->popl_esp_hack;
14ce26e7
FB
2012#ifdef TARGET_X86_64
2013 if (s->aflag == 2) {
57fec1fe 2014 gen_op_movq_A0_reg(base);
14ce26e7 2015 if (disp != 0) {
57fec1fe 2016 gen_op_addq_A0_im(disp);
14ce26e7 2017 }
5fafdf24 2018 } else
14ce26e7
FB
2019#endif
2020 {
57fec1fe 2021 gen_op_movl_A0_reg(base);
14ce26e7
FB
2022 if (disp != 0)
2023 gen_op_addl_A0_im(disp);
2024 }
2c0262af 2025 } else {
14ce26e7
FB
2026#ifdef TARGET_X86_64
2027 if (s->aflag == 2) {
57fec1fe 2028 gen_op_movq_A0_im(disp);
5fafdf24 2029 } else
14ce26e7
FB
2030#endif
2031 {
2032 gen_op_movl_A0_im(disp);
2033 }
2c0262af 2034 }
b16f827b
AJ
2035 /* index == 4 means no index */
2036 if (havesib && (index != 4)) {
14ce26e7
FB
2037#ifdef TARGET_X86_64
2038 if (s->aflag == 2) {
57fec1fe 2039 gen_op_addq_A0_reg_sN(scale, index);
5fafdf24 2040 } else
14ce26e7
FB
2041#endif
2042 {
57fec1fe 2043 gen_op_addl_A0_reg_sN(scale, index);
14ce26e7 2044 }
2c0262af
FB
2045 }
2046 if (must_add_seg) {
2047 if (override < 0) {
2048 if (base == R_EBP || base == R_ESP)
2049 override = R_SS;
2050 else
2051 override = R_DS;
2052 }
14ce26e7
FB
2053#ifdef TARGET_X86_64
2054 if (s->aflag == 2) {
57fec1fe 2055 gen_op_addq_A0_seg(override);
5fafdf24 2056 } else
14ce26e7
FB
2057#endif
2058 {
7162ab21 2059 gen_op_addl_A0_seg(s, override);
14ce26e7 2060 }
2c0262af
FB
2061 }
2062 } else {
2063 switch (mod) {
2064 case 0:
2065 if (rm == 6) {
0af10c86 2066 disp = cpu_lduw_code(env, s->pc);
2c0262af
FB
2067 s->pc += 2;
2068 gen_op_movl_A0_im(disp);
2069 rm = 0; /* avoid SS override */
2070 goto no_rm;
2071 } else {
2072 disp = 0;
2073 }
2074 break;
2075 case 1:
0af10c86 2076 disp = (int8_t)cpu_ldub_code(env, s->pc++);
2c0262af
FB
2077 break;
2078 default:
2079 case 2:
0af10c86 2080 disp = cpu_lduw_code(env, s->pc);
2c0262af
FB
2081 s->pc += 2;
2082 break;
2083 }
2084 switch(rm) {
2085 case 0:
57fec1fe
FB
2086 gen_op_movl_A0_reg(R_EBX);
2087 gen_op_addl_A0_reg_sN(0, R_ESI);
2c0262af
FB
2088 break;
2089 case 1:
57fec1fe
FB
2090 gen_op_movl_A0_reg(R_EBX);
2091 gen_op_addl_A0_reg_sN(0, R_EDI);
2c0262af
FB
2092 break;
2093 case 2:
57fec1fe
FB
2094 gen_op_movl_A0_reg(R_EBP);
2095 gen_op_addl_A0_reg_sN(0, R_ESI);
2c0262af
FB
2096 break;
2097 case 3:
57fec1fe
FB
2098 gen_op_movl_A0_reg(R_EBP);
2099 gen_op_addl_A0_reg_sN(0, R_EDI);
2c0262af
FB
2100 break;
2101 case 4:
57fec1fe 2102 gen_op_movl_A0_reg(R_ESI);
2c0262af
FB
2103 break;
2104 case 5:
57fec1fe 2105 gen_op_movl_A0_reg(R_EDI);
2c0262af
FB
2106 break;
2107 case 6:
57fec1fe 2108 gen_op_movl_A0_reg(R_EBP);
2c0262af
FB
2109 break;
2110 default:
2111 case 7:
57fec1fe 2112 gen_op_movl_A0_reg(R_EBX);
2c0262af
FB
2113 break;
2114 }
2115 if (disp != 0)
2116 gen_op_addl_A0_im(disp);
2117 gen_op_andl_A0_ffff();
2118 no_rm:
2119 if (must_add_seg) {
2120 if (override < 0) {
2121 if (rm == 2 || rm == 3 || rm == 6)
2122 override = R_SS;
2123 else
2124 override = R_DS;
2125 }
7162ab21 2126 gen_op_addl_A0_seg(s, override);
2c0262af
FB
2127 }
2128 }
2129
2130 opreg = OR_A0;
2131 disp = 0;
2132 *reg_ptr = opreg;
2133 *offset_ptr = disp;
2134}
2135
0af10c86 2136static void gen_nop_modrm(CPUX86State *env, DisasContext *s, int modrm)
e17a36ce
FB
2137{
2138 int mod, rm, base, code;
2139
2140 mod = (modrm >> 6) & 3;
2141 if (mod == 3)
2142 return;
2143 rm = modrm & 7;
2144
2145 if (s->aflag) {
2146
2147 base = rm;
3b46e624 2148
e17a36ce 2149 if (base == 4) {
0af10c86 2150 code = cpu_ldub_code(env, s->pc++);
e17a36ce
FB
2151 base = (code & 7);
2152 }
3b46e624 2153
e17a36ce
FB
2154 switch (mod) {
2155 case 0:
2156 if (base == 5) {
2157 s->pc += 4;
2158 }
2159 break;
2160 case 1:
2161 s->pc++;
2162 break;
2163 default:
2164 case 2:
2165 s->pc += 4;
2166 break;
2167 }
2168 } else {
2169 switch (mod) {
2170 case 0:
2171 if (rm == 6) {
2172 s->pc += 2;
2173 }
2174 break;
2175 case 1:
2176 s->pc++;
2177 break;
2178 default:
2179 case 2:
2180 s->pc += 2;
2181 break;
2182 }
2183 }
2184}
2185
664e0f19
FB
2186/* used for LEA and MOV AX, mem */
2187static void gen_add_A0_ds_seg(DisasContext *s)
2188{
2189 int override, must_add_seg;
2190 must_add_seg = s->addseg;
2191 override = R_DS;
2192 if (s->override >= 0) {
2193 override = s->override;
2194 must_add_seg = 1;
664e0f19
FB
2195 }
2196 if (must_add_seg) {
8f091a59
FB
2197#ifdef TARGET_X86_64
2198 if (CODE64(s)) {
57fec1fe 2199 gen_op_addq_A0_seg(override);
5fafdf24 2200 } else
8f091a59
FB
2201#endif
2202 {
7162ab21 2203 gen_op_addl_A0_seg(s, override);
8f091a59 2204 }
664e0f19
FB
2205 }
2206}
2207
222a3336 2208/* generate modrm memory load or store of 'reg'. TMP0 is used if reg ==
2c0262af 2209 OR_TMP0 */
0af10c86
BS
2210static void gen_ldst_modrm(CPUX86State *env, DisasContext *s, int modrm,
2211 int ot, int reg, int is_store)
2c0262af
FB
2212{
2213 int mod, rm, opreg, disp;
2214
2215 mod = (modrm >> 6) & 3;
14ce26e7 2216 rm = (modrm & 7) | REX_B(s);
2c0262af
FB
2217 if (mod == 3) {
2218 if (is_store) {
2219 if (reg != OR_TMP0)
57fec1fe
FB
2220 gen_op_mov_TN_reg(ot, 0, reg);
2221 gen_op_mov_reg_T0(ot, rm);
2c0262af 2222 } else {
57fec1fe 2223 gen_op_mov_TN_reg(ot, 0, rm);
2c0262af 2224 if (reg != OR_TMP0)
57fec1fe 2225 gen_op_mov_reg_T0(ot, reg);
2c0262af
FB
2226 }
2227 } else {
0af10c86 2228 gen_lea_modrm(env, s, modrm, &opreg, &disp);
2c0262af
FB
2229 if (is_store) {
2230 if (reg != OR_TMP0)
57fec1fe
FB
2231 gen_op_mov_TN_reg(ot, 0, reg);
2232 gen_op_st_T0_A0(ot + s->mem_index);
2c0262af 2233 } else {
57fec1fe 2234 gen_op_ld_T0_A0(ot + s->mem_index);
2c0262af 2235 if (reg != OR_TMP0)
57fec1fe 2236 gen_op_mov_reg_T0(ot, reg);
2c0262af
FB
2237 }
2238 }
2239}
2240
0af10c86 2241static inline uint32_t insn_get(CPUX86State *env, DisasContext *s, int ot)
2c0262af
FB
2242{
2243 uint32_t ret;
2244
2245 switch(ot) {
2246 case OT_BYTE:
0af10c86 2247 ret = cpu_ldub_code(env, s->pc);
2c0262af
FB
2248 s->pc++;
2249 break;
2250 case OT_WORD:
0af10c86 2251 ret = cpu_lduw_code(env, s->pc);
2c0262af
FB
2252 s->pc += 2;
2253 break;
2254 default:
2255 case OT_LONG:
0af10c86 2256 ret = cpu_ldl_code(env, s->pc);
2c0262af
FB
2257 s->pc += 4;
2258 break;
2259 }
2260 return ret;
2261}
2262
14ce26e7
FB
2263static inline int insn_const_size(unsigned int ot)
2264{
2265 if (ot <= OT_LONG)
2266 return 1 << ot;
2267 else
2268 return 4;
2269}
2270
6e256c93
FB
2271static inline void gen_goto_tb(DisasContext *s, int tb_num, target_ulong eip)
2272{
2273 TranslationBlock *tb;
2274 target_ulong pc;
2275
2276 pc = s->cs_base + eip;
2277 tb = s->tb;
2278 /* NOTE: we handle the case where the TB spans two pages here */
2279 if ((pc & TARGET_PAGE_MASK) == (tb->pc & TARGET_PAGE_MASK) ||
2280 (pc & TARGET_PAGE_MASK) == ((s->pc - 1) & TARGET_PAGE_MASK)) {
2281 /* jump to same page: we can use a direct jump */
57fec1fe 2282 tcg_gen_goto_tb(tb_num);
6e256c93 2283 gen_jmp_im(eip);
4b4a72e5 2284 tcg_gen_exit_tb((tcg_target_long)tb + tb_num);
6e256c93
FB
2285 } else {
2286 /* jump to another page: currently not optimized */
2287 gen_jmp_im(eip);
2288 gen_eob(s);
2289 }
2290}
2291
5fafdf24 2292static inline void gen_jcc(DisasContext *s, int b,
14ce26e7 2293 target_ulong val, target_ulong next_eip)
2c0262af 2294{
b27fc131 2295 int l1, l2;
3b46e624 2296
b27fc131
PB
2297 if (s->cc_op != CC_OP_DYNAMIC) {
2298 gen_op_set_cc_op(s->cc_op);
2299 }
2c0262af 2300 if (s->jmp_opt) {
14ce26e7 2301 l1 = gen_new_label();
b27fc131 2302 gen_jcc1(s, b, l1);
3ca51d07 2303 set_cc_op(s, CC_OP_DYNAMIC);
8e1c85e3 2304
6e256c93 2305 gen_goto_tb(s, 0, next_eip);
14ce26e7
FB
2306
2307 gen_set_label(l1);
6e256c93 2308 gen_goto_tb(s, 1, val);
5779406a 2309 s->is_jmp = DISAS_TB_JUMP;
2c0262af 2310 } else {
14ce26e7 2311
14ce26e7
FB
2312 l1 = gen_new_label();
2313 l2 = gen_new_label();
b27fc131 2314 gen_jcc1(s, b, l1);
3ca51d07 2315 set_cc_op(s, CC_OP_DYNAMIC);
8e1c85e3 2316
14ce26e7 2317 gen_jmp_im(next_eip);
8e1c85e3
FB
2318 tcg_gen_br(l2);
2319
14ce26e7
FB
2320 gen_set_label(l1);
2321 gen_jmp_im(val);
2322 gen_set_label(l2);
2c0262af
FB
2323 gen_eob(s);
2324 }
2325}
2326
2327static void gen_setcc(DisasContext *s, int b)
2328{
8e1c85e3 2329 int inv, jcc_op, l1;
1e4840bf 2330 TCGv t0;
14ce26e7 2331
8e1c85e3
FB
2332 if (is_fast_jcc_case(s, b)) {
2333 /* nominal case: we use a jump */
1e4840bf 2334 /* XXX: make it faster by adding new instructions in TCG */
a7812ae4 2335 t0 = tcg_temp_local_new();
1e4840bf 2336 tcg_gen_movi_tl(t0, 0);
8e1c85e3 2337 l1 = gen_new_label();
b27fc131 2338 gen_jcc1(s, b ^ 1, l1);
1e4840bf 2339 tcg_gen_movi_tl(t0, 1);
8e1c85e3 2340 gen_set_label(l1);
1e4840bf
FB
2341 tcg_gen_mov_tl(cpu_T[0], t0);
2342 tcg_temp_free(t0);
8e1c85e3
FB
2343 } else {
2344 /* slow case: it is more efficient not to generate a jump,
2345 although it is questionnable whether this optimization is
2346 worth to */
2347 inv = b & 1;
2348 jcc_op = (b >> 1) & 7;
1e4840bf 2349 gen_setcc_slow_T0(s, jcc_op);
8e1c85e3
FB
2350 if (inv) {
2351 tcg_gen_xori_tl(cpu_T[0], cpu_T[0], 1);
2352 }
2c0262af
FB
2353 }
2354}
2355
3bd7da9e
FB
2356static inline void gen_op_movl_T0_seg(int seg_reg)
2357{
2358 tcg_gen_ld32u_tl(cpu_T[0], cpu_env,
2359 offsetof(CPUX86State,segs[seg_reg].selector));
2360}
2361
2362static inline void gen_op_movl_seg_T0_vm(int seg_reg)
2363{
2364 tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xffff);
2365 tcg_gen_st32_tl(cpu_T[0], cpu_env,
2366 offsetof(CPUX86State,segs[seg_reg].selector));
2367 tcg_gen_shli_tl(cpu_T[0], cpu_T[0], 4);
2368 tcg_gen_st_tl(cpu_T[0], cpu_env,
2369 offsetof(CPUX86State,segs[seg_reg].base));
2370}
2371
2c0262af
FB
2372/* move T0 to seg_reg and compute if the CPU state may change. Never
2373 call this function with seg_reg == R_CS */
14ce26e7 2374static void gen_movl_seg_T0(DisasContext *s, int seg_reg, target_ulong cur_eip)
2c0262af 2375{
3415a4dd
FB
2376 if (s->pe && !s->vm86) {
2377 /* XXX: optimize by finding processor state dynamically */
2378 if (s->cc_op != CC_OP_DYNAMIC)
2379 gen_op_set_cc_op(s->cc_op);
14ce26e7 2380 gen_jmp_im(cur_eip);
b6abf97d 2381 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
2999a0b2 2382 gen_helper_load_seg(cpu_env, tcg_const_i32(seg_reg), cpu_tmp2_i32);
dc196a57
FB
2383 /* abort translation because the addseg value may change or
2384 because ss32 may change. For R_SS, translation must always
2385 stop as a special handling must be done to disable hardware
2386 interrupts for the next instruction */
2387 if (seg_reg == R_SS || (s->code32 && seg_reg < R_FS))
5779406a 2388 s->is_jmp = DISAS_TB_JUMP;
3415a4dd 2389 } else {
3bd7da9e 2390 gen_op_movl_seg_T0_vm(seg_reg);
dc196a57 2391 if (seg_reg == R_SS)
5779406a 2392 s->is_jmp = DISAS_TB_JUMP;
3415a4dd 2393 }
2c0262af
FB
2394}
2395
0573fbfc
TS
2396static inline int svm_is_rep(int prefixes)
2397{
2398 return ((prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) ? 8 : 0);
2399}
2400
872929aa 2401static inline void
0573fbfc 2402gen_svm_check_intercept_param(DisasContext *s, target_ulong pc_start,
b8b6a50b 2403 uint32_t type, uint64_t param)
0573fbfc 2404{
872929aa
FB
2405 /* no SVM activated; fast case */
2406 if (likely(!(s->flags & HF_SVMI_MASK)))
2407 return;
2408 if (s->cc_op != CC_OP_DYNAMIC)
2409 gen_op_set_cc_op(s->cc_op);
2410 gen_jmp_im(pc_start - s->cs_base);
052e80d5 2411 gen_helper_svm_check_intercept_param(cpu_env, tcg_const_i32(type),
a7812ae4 2412 tcg_const_i64(param));
0573fbfc
TS
2413}
2414
872929aa 2415static inline void
0573fbfc
TS
2416gen_svm_check_intercept(DisasContext *s, target_ulong pc_start, uint64_t type)
2417{
872929aa 2418 gen_svm_check_intercept_param(s, pc_start, type, 0);
0573fbfc
TS
2419}
2420
4f31916f
FB
2421static inline void gen_stack_update(DisasContext *s, int addend)
2422{
14ce26e7
FB
2423#ifdef TARGET_X86_64
2424 if (CODE64(s)) {
6e0d8677 2425 gen_op_add_reg_im(2, R_ESP, addend);
14ce26e7
FB
2426 } else
2427#endif
4f31916f 2428 if (s->ss32) {
6e0d8677 2429 gen_op_add_reg_im(1, R_ESP, addend);
4f31916f 2430 } else {
6e0d8677 2431 gen_op_add_reg_im(0, R_ESP, addend);
4f31916f
FB
2432 }
2433}
2434
2c0262af
FB
2435/* generate a push. It depends on ss32, addseg and dflag */
2436static void gen_push_T0(DisasContext *s)
2437{
14ce26e7
FB
2438#ifdef TARGET_X86_64
2439 if (CODE64(s)) {
57fec1fe 2440 gen_op_movq_A0_reg(R_ESP);
8f091a59 2441 if (s->dflag) {
57fec1fe
FB
2442 gen_op_addq_A0_im(-8);
2443 gen_op_st_T0_A0(OT_QUAD + s->mem_index);
8f091a59 2444 } else {
57fec1fe
FB
2445 gen_op_addq_A0_im(-2);
2446 gen_op_st_T0_A0(OT_WORD + s->mem_index);
8f091a59 2447 }
57fec1fe 2448 gen_op_mov_reg_A0(2, R_ESP);
5fafdf24 2449 } else
14ce26e7
FB
2450#endif
2451 {
57fec1fe 2452 gen_op_movl_A0_reg(R_ESP);
14ce26e7 2453 if (!s->dflag)
57fec1fe 2454 gen_op_addl_A0_im(-2);
14ce26e7 2455 else
57fec1fe 2456 gen_op_addl_A0_im(-4);
14ce26e7
FB
2457 if (s->ss32) {
2458 if (s->addseg) {
bbf662ee 2459 tcg_gen_mov_tl(cpu_T[1], cpu_A0);
7162ab21 2460 gen_op_addl_A0_seg(s, R_SS);
14ce26e7
FB
2461 }
2462 } else {
2463 gen_op_andl_A0_ffff();
bbf662ee 2464 tcg_gen_mov_tl(cpu_T[1], cpu_A0);
7162ab21 2465 gen_op_addl_A0_seg(s, R_SS);
2c0262af 2466 }
57fec1fe 2467 gen_op_st_T0_A0(s->dflag + 1 + s->mem_index);
14ce26e7 2468 if (s->ss32 && !s->addseg)
57fec1fe 2469 gen_op_mov_reg_A0(1, R_ESP);
14ce26e7 2470 else
57fec1fe 2471 gen_op_mov_reg_T1(s->ss32 + 1, R_ESP);
2c0262af
FB
2472 }
2473}
2474
4f31916f
FB
2475/* generate a push. It depends on ss32, addseg and dflag */
2476/* slower version for T1, only used for call Ev */
2477static void gen_push_T1(DisasContext *s)
2c0262af 2478{
14ce26e7
FB
2479#ifdef TARGET_X86_64
2480 if (CODE64(s)) {
57fec1fe 2481 gen_op_movq_A0_reg(R_ESP);
8f091a59 2482 if (s->dflag) {
57fec1fe
FB
2483 gen_op_addq_A0_im(-8);
2484 gen_op_st_T1_A0(OT_QUAD + s->mem_index);
8f091a59 2485 } else {
57fec1fe
FB
2486 gen_op_addq_A0_im(-2);
2487 gen_op_st_T0_A0(OT_WORD + s->mem_index);
8f091a59 2488 }
57fec1fe 2489 gen_op_mov_reg_A0(2, R_ESP);
5fafdf24 2490 } else
14ce26e7
FB
2491#endif
2492 {
57fec1fe 2493 gen_op_movl_A0_reg(R_ESP);
14ce26e7 2494 if (!s->dflag)
57fec1fe 2495 gen_op_addl_A0_im(-2);
14ce26e7 2496 else
57fec1fe 2497 gen_op_addl_A0_im(-4);
14ce26e7
FB
2498 if (s->ss32) {
2499 if (s->addseg) {
7162ab21 2500 gen_op_addl_A0_seg(s, R_SS);
14ce26e7
FB
2501 }
2502 } else {
2503 gen_op_andl_A0_ffff();
7162ab21 2504 gen_op_addl_A0_seg(s, R_SS);
2c0262af 2505 }
57fec1fe 2506 gen_op_st_T1_A0(s->dflag + 1 + s->mem_index);
3b46e624 2507
14ce26e7 2508 if (s->ss32 && !s->addseg)
57fec1fe 2509 gen_op_mov_reg_A0(1, R_ESP);
14ce26e7
FB
2510 else
2511 gen_stack_update(s, (-2) << s->dflag);
2c0262af
FB
2512 }
2513}
2514
4f31916f
FB
2515/* two step pop is necessary for precise exceptions */
2516static void gen_pop_T0(DisasContext *s)
2c0262af 2517{
14ce26e7
FB
2518#ifdef TARGET_X86_64
2519 if (CODE64(s)) {
57fec1fe
FB
2520 gen_op_movq_A0_reg(R_ESP);
2521 gen_op_ld_T0_A0((s->dflag ? OT_QUAD : OT_WORD) + s->mem_index);
5fafdf24 2522 } else
14ce26e7
FB
2523#endif
2524 {
57fec1fe 2525 gen_op_movl_A0_reg(R_ESP);
14ce26e7
FB
2526 if (s->ss32) {
2527 if (s->addseg)
7162ab21 2528 gen_op_addl_A0_seg(s, R_SS);
14ce26e7
FB
2529 } else {
2530 gen_op_andl_A0_ffff();
7162ab21 2531 gen_op_addl_A0_seg(s, R_SS);
14ce26e7 2532 }
57fec1fe 2533 gen_op_ld_T0_A0(s->dflag + 1 + s->mem_index);
2c0262af
FB
2534 }
2535}
2536
2537static void gen_pop_update(DisasContext *s)
2538{
14ce26e7 2539#ifdef TARGET_X86_64
8f091a59 2540 if (CODE64(s) && s->dflag) {
14ce26e7
FB
2541 gen_stack_update(s, 8);
2542 } else
2543#endif
2544 {
2545 gen_stack_update(s, 2 << s->dflag);
2546 }
2c0262af
FB
2547}
2548
2549static void gen_stack_A0(DisasContext *s)
2550{
57fec1fe 2551 gen_op_movl_A0_reg(R_ESP);
2c0262af
FB
2552 if (!s->ss32)
2553 gen_op_andl_A0_ffff();
bbf662ee 2554 tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2c0262af 2555 if (s->addseg)
7162ab21 2556 gen_op_addl_A0_seg(s, R_SS);
2c0262af
FB
2557}
2558
2559/* NOTE: wrap around in 16 bit not fully handled */
2560static void gen_pusha(DisasContext *s)
2561{
2562 int i;
57fec1fe 2563 gen_op_movl_A0_reg(R_ESP);
2c0262af
FB
2564 gen_op_addl_A0_im(-16 << s->dflag);
2565 if (!s->ss32)
2566 gen_op_andl_A0_ffff();
bbf662ee 2567 tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2c0262af 2568 if (s->addseg)
7162ab21 2569 gen_op_addl_A0_seg(s, R_SS);
2c0262af 2570 for(i = 0;i < 8; i++) {
57fec1fe
FB
2571 gen_op_mov_TN_reg(OT_LONG, 0, 7 - i);
2572 gen_op_st_T0_A0(OT_WORD + s->dflag + s->mem_index);
2c0262af
FB
2573 gen_op_addl_A0_im(2 << s->dflag);
2574 }
57fec1fe 2575 gen_op_mov_reg_T1(OT_WORD + s->ss32, R_ESP);
2c0262af
FB
2576}
2577
2578/* NOTE: wrap around in 16 bit not fully handled */
2579static void gen_popa(DisasContext *s)
2580{
2581 int i;
57fec1fe 2582 gen_op_movl_A0_reg(R_ESP);
2c0262af
FB
2583 if (!s->ss32)
2584 gen_op_andl_A0_ffff();
bbf662ee
FB
2585 tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2586 tcg_gen_addi_tl(cpu_T[1], cpu_T[1], 16 << s->dflag);
2c0262af 2587 if (s->addseg)
7162ab21 2588 gen_op_addl_A0_seg(s, R_SS);
2c0262af
FB
2589 for(i = 0;i < 8; i++) {
2590 /* ESP is not reloaded */
2591 if (i != 3) {
57fec1fe
FB
2592 gen_op_ld_T0_A0(OT_WORD + s->dflag + s->mem_index);
2593 gen_op_mov_reg_T0(OT_WORD + s->dflag, 7 - i);
2c0262af
FB
2594 }
2595 gen_op_addl_A0_im(2 << s->dflag);
2596 }
57fec1fe 2597 gen_op_mov_reg_T1(OT_WORD + s->ss32, R_ESP);
2c0262af
FB
2598}
2599
2c0262af
FB
2600static void gen_enter(DisasContext *s, int esp_addend, int level)
2601{
61a8c4ec 2602 int ot, opsize;
2c0262af 2603
2c0262af 2604 level &= 0x1f;
8f091a59
FB
2605#ifdef TARGET_X86_64
2606 if (CODE64(s)) {
2607 ot = s->dflag ? OT_QUAD : OT_WORD;
2608 opsize = 1 << ot;
3b46e624 2609
57fec1fe 2610 gen_op_movl_A0_reg(R_ESP);
8f091a59 2611 gen_op_addq_A0_im(-opsize);
bbf662ee 2612 tcg_gen_mov_tl(cpu_T[1], cpu_A0);
8f091a59
FB
2613
2614 /* push bp */
57fec1fe
FB
2615 gen_op_mov_TN_reg(OT_LONG, 0, R_EBP);
2616 gen_op_st_T0_A0(ot + s->mem_index);
8f091a59 2617 if (level) {
b5b38f61 2618 /* XXX: must save state */
2999a0b2 2619 gen_helper_enter64_level(cpu_env, tcg_const_i32(level),
a7812ae4
PB
2620 tcg_const_i32((ot == OT_QUAD)),
2621 cpu_T[1]);
8f091a59 2622 }
57fec1fe 2623 gen_op_mov_reg_T1(ot, R_EBP);
bbf662ee 2624 tcg_gen_addi_tl(cpu_T[1], cpu_T[1], -esp_addend + (-opsize * level));
57fec1fe 2625 gen_op_mov_reg_T1(OT_QUAD, R_ESP);
5fafdf24 2626 } else
8f091a59
FB
2627#endif
2628 {
2629 ot = s->dflag + OT_WORD;
2630 opsize = 2 << s->dflag;
3b46e624 2631
57fec1fe 2632 gen_op_movl_A0_reg(R_ESP);
8f091a59
FB
2633 gen_op_addl_A0_im(-opsize);
2634 if (!s->ss32)
2635 gen_op_andl_A0_ffff();
bbf662ee 2636 tcg_gen_mov_tl(cpu_T[1], cpu_A0);
8f091a59 2637 if (s->addseg)
7162ab21 2638 gen_op_addl_A0_seg(s, R_SS);
8f091a59 2639 /* push bp */
57fec1fe
FB
2640 gen_op_mov_TN_reg(OT_LONG, 0, R_EBP);
2641 gen_op_st_T0_A0(ot + s->mem_index);
8f091a59 2642 if (level) {
b5b38f61 2643 /* XXX: must save state */
2999a0b2 2644 gen_helper_enter_level(cpu_env, tcg_const_i32(level),
a7812ae4
PB
2645 tcg_const_i32(s->dflag),
2646 cpu_T[1]);
8f091a59 2647 }
57fec1fe 2648 gen_op_mov_reg_T1(ot, R_EBP);
bbf662ee 2649 tcg_gen_addi_tl(cpu_T[1], cpu_T[1], -esp_addend + (-opsize * level));
57fec1fe 2650 gen_op_mov_reg_T1(OT_WORD + s->ss32, R_ESP);
2c0262af 2651 }
2c0262af
FB
2652}
2653
14ce26e7 2654static void gen_exception(DisasContext *s, int trapno, target_ulong cur_eip)
2c0262af
FB
2655{
2656 if (s->cc_op != CC_OP_DYNAMIC)
2657 gen_op_set_cc_op(s->cc_op);
14ce26e7 2658 gen_jmp_im(cur_eip);
77b2bc2c 2659 gen_helper_raise_exception(cpu_env, tcg_const_i32(trapno));
5779406a 2660 s->is_jmp = DISAS_TB_JUMP;
2c0262af
FB
2661}
2662
2663/* an interrupt is different from an exception because of the
7f75ffd3 2664 privilege checks */
5fafdf24 2665static void gen_interrupt(DisasContext *s, int intno,
14ce26e7 2666 target_ulong cur_eip, target_ulong next_eip)
2c0262af
FB
2667{
2668 if (s->cc_op != CC_OP_DYNAMIC)
2669 gen_op_set_cc_op(s->cc_op);
14ce26e7 2670 gen_jmp_im(cur_eip);
77b2bc2c 2671 gen_helper_raise_interrupt(cpu_env, tcg_const_i32(intno),
a7812ae4 2672 tcg_const_i32(next_eip - cur_eip));
5779406a 2673 s->is_jmp = DISAS_TB_JUMP;
2c0262af
FB
2674}
2675
14ce26e7 2676static void gen_debug(DisasContext *s, target_ulong cur_eip)
2c0262af
FB
2677{
2678 if (s->cc_op != CC_OP_DYNAMIC)
2679 gen_op_set_cc_op(s->cc_op);
14ce26e7 2680 gen_jmp_im(cur_eip);
4a7443be 2681 gen_helper_debug(cpu_env);
5779406a 2682 s->is_jmp = DISAS_TB_JUMP;
2c0262af
FB
2683}
2684
2685/* generate a generic end of block. Trace exception is also generated
2686 if needed */
2687static void gen_eob(DisasContext *s)
2688{
2689 if (s->cc_op != CC_OP_DYNAMIC)
2690 gen_op_set_cc_op(s->cc_op);
a2cc3b24 2691 if (s->tb->flags & HF_INHIBIT_IRQ_MASK) {
f0967a1a 2692 gen_helper_reset_inhibit_irq(cpu_env);
a2cc3b24 2693 }
a2397807 2694 if (s->tb->flags & HF_RF_MASK) {
f0967a1a 2695 gen_helper_reset_rf(cpu_env);
a2397807 2696 }
34865134 2697 if (s->singlestep_enabled) {
4a7443be 2698 gen_helper_debug(cpu_env);
34865134 2699 } else if (s->tf) {
4a7443be 2700 gen_helper_single_step(cpu_env);
2c0262af 2701 } else {
57fec1fe 2702 tcg_gen_exit_tb(0);
2c0262af 2703 }
5779406a 2704 s->is_jmp = DISAS_TB_JUMP;
2c0262af
FB
2705}
2706
2707/* generate a jump to eip. No segment change must happen before as a
2708 direct call to the next block may occur */
14ce26e7 2709static void gen_jmp_tb(DisasContext *s, target_ulong eip, int tb_num)
2c0262af 2710{
2c0262af 2711 if (s->jmp_opt) {
728d803b 2712 gen_update_cc_op(s);
6e256c93 2713 gen_goto_tb(s, tb_num, eip);
5779406a 2714 s->is_jmp = DISAS_TB_JUMP;
2c0262af 2715 } else {
14ce26e7 2716 gen_jmp_im(eip);
2c0262af
FB
2717 gen_eob(s);
2718 }
2719}
2720
14ce26e7
FB
2721static void gen_jmp(DisasContext *s, target_ulong eip)
2722{
2723 gen_jmp_tb(s, eip, 0);
2724}
2725
8686c490
FB
2726static inline void gen_ldq_env_A0(int idx, int offset)
2727{
2728 int mem_index = (idx >> 2) - 1;
b6abf97d
FB
2729 tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_A0, mem_index);
2730 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset);
8686c490 2731}
664e0f19 2732
8686c490
FB
2733static inline void gen_stq_env_A0(int idx, int offset)
2734{
2735 int mem_index = (idx >> 2) - 1;
b6abf97d
FB
2736 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset);
2737 tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_A0, mem_index);
8686c490 2738}
664e0f19 2739
8686c490
FB
2740static inline void gen_ldo_env_A0(int idx, int offset)
2741{
2742 int mem_index = (idx >> 2) - 1;
b6abf97d
FB
2743 tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_A0, mem_index);
2744 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(0)));
8686c490 2745 tcg_gen_addi_tl(cpu_tmp0, cpu_A0, 8);
b6abf97d
FB
2746 tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_tmp0, mem_index);
2747 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(1)));
8686c490 2748}
14ce26e7 2749
8686c490
FB
2750static inline void gen_sto_env_A0(int idx, int offset)
2751{
2752 int mem_index = (idx >> 2) - 1;
b6abf97d
FB
2753 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(0)));
2754 tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_A0, mem_index);
8686c490 2755 tcg_gen_addi_tl(cpu_tmp0, cpu_A0, 8);
b6abf97d
FB
2756 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(1)));
2757 tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_tmp0, mem_index);
8686c490 2758}
14ce26e7 2759
5af45186
FB
2760static inline void gen_op_movo(int d_offset, int s_offset)
2761{
b6abf97d
FB
2762 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset);
2763 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
2764 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset + 8);
2765 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset + 8);
5af45186
FB
2766}
2767
2768static inline void gen_op_movq(int d_offset, int s_offset)
2769{
b6abf97d
FB
2770 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset);
2771 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
5af45186
FB
2772}
2773
2774static inline void gen_op_movl(int d_offset, int s_offset)
2775{
b6abf97d
FB
2776 tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env, s_offset);
2777 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, d_offset);
5af45186
FB
2778}
2779
2780static inline void gen_op_movq_env_0(int d_offset)
2781{
b6abf97d
FB
2782 tcg_gen_movi_i64(cpu_tmp1_i64, 0);
2783 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
5af45186 2784}
664e0f19 2785
d3eb5eae
BS
2786typedef void (*SSEFunc_i_ep)(TCGv_i32 val, TCGv_ptr env, TCGv_ptr reg);
2787typedef void (*SSEFunc_l_ep)(TCGv_i64 val, TCGv_ptr env, TCGv_ptr reg);
2788typedef void (*SSEFunc_0_epi)(TCGv_ptr env, TCGv_ptr reg, TCGv_i32 val);
2789typedef void (*SSEFunc_0_epl)(TCGv_ptr env, TCGv_ptr reg, TCGv_i64 val);
2790typedef void (*SSEFunc_0_epp)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b);
2791typedef void (*SSEFunc_0_eppi)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
2792 TCGv_i32 val);
c4baa050 2793typedef void (*SSEFunc_0_ppi)(TCGv_ptr reg_a, TCGv_ptr reg_b, TCGv_i32 val);
d3eb5eae
BS
2794typedef void (*SSEFunc_0_eppt)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
2795 TCGv val);
c4baa050 2796
5af45186
FB
2797#define SSE_SPECIAL ((void *)1)
2798#define SSE_DUMMY ((void *)2)
664e0f19 2799
a7812ae4
PB
2800#define MMX_OP2(x) { gen_helper_ ## x ## _mmx, gen_helper_ ## x ## _xmm }
2801#define SSE_FOP(x) { gen_helper_ ## x ## ps, gen_helper_ ## x ## pd, \
2802 gen_helper_ ## x ## ss, gen_helper_ ## x ## sd, }
5af45186 2803
d3eb5eae 2804static const SSEFunc_0_epp sse_op_table1[256][4] = {
a35f3ec7
AJ
2805 /* 3DNow! extensions */
2806 [0x0e] = { SSE_DUMMY }, /* femms */
2807 [0x0f] = { SSE_DUMMY }, /* pf... */
664e0f19
FB
2808 /* pure SSE operations */
2809 [0x10] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movups, movupd, movss, movsd */
2810 [0x11] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movups, movupd, movss, movsd */
465e9838 2811 [0x12] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movlps, movlpd, movsldup, movddup */
664e0f19 2812 [0x13] = { SSE_SPECIAL, SSE_SPECIAL }, /* movlps, movlpd */
a7812ae4
PB
2813 [0x14] = { gen_helper_punpckldq_xmm, gen_helper_punpcklqdq_xmm },
2814 [0x15] = { gen_helper_punpckhdq_xmm, gen_helper_punpckhqdq_xmm },
664e0f19
FB
2815 [0x16] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movhps, movhpd, movshdup */
2816 [0x17] = { SSE_SPECIAL, SSE_SPECIAL }, /* movhps, movhpd */
2817
2818 [0x28] = { SSE_SPECIAL, SSE_SPECIAL }, /* movaps, movapd */
2819 [0x29] = { SSE_SPECIAL, SSE_SPECIAL }, /* movaps, movapd */
2820 [0x2a] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* cvtpi2ps, cvtpi2pd, cvtsi2ss, cvtsi2sd */
d9f4bb27 2821 [0x2b] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movntps, movntpd, movntss, movntsd */
664e0f19
FB
2822 [0x2c] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* cvttps2pi, cvttpd2pi, cvttsd2si, cvttss2si */
2823 [0x2d] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* cvtps2pi, cvtpd2pi, cvtsd2si, cvtss2si */
a7812ae4
PB
2824 [0x2e] = { gen_helper_ucomiss, gen_helper_ucomisd },
2825 [0x2f] = { gen_helper_comiss, gen_helper_comisd },
664e0f19
FB
2826 [0x50] = { SSE_SPECIAL, SSE_SPECIAL }, /* movmskps, movmskpd */
2827 [0x51] = SSE_FOP(sqrt),
a7812ae4
PB
2828 [0x52] = { gen_helper_rsqrtps, NULL, gen_helper_rsqrtss, NULL },
2829 [0x53] = { gen_helper_rcpps, NULL, gen_helper_rcpss, NULL },
2830 [0x54] = { gen_helper_pand_xmm, gen_helper_pand_xmm }, /* andps, andpd */
2831 [0x55] = { gen_helper_pandn_xmm, gen_helper_pandn_xmm }, /* andnps, andnpd */
2832 [0x56] = { gen_helper_por_xmm, gen_helper_por_xmm }, /* orps, orpd */
2833 [0x57] = { gen_helper_pxor_xmm, gen_helper_pxor_xmm }, /* xorps, xorpd */
664e0f19
FB
2834 [0x58] = SSE_FOP(add),
2835 [0x59] = SSE_FOP(mul),
a7812ae4
PB
2836 [0x5a] = { gen_helper_cvtps2pd, gen_helper_cvtpd2ps,
2837 gen_helper_cvtss2sd, gen_helper_cvtsd2ss },
2838 [0x5b] = { gen_helper_cvtdq2ps, gen_helper_cvtps2dq, gen_helper_cvttps2dq },
664e0f19
FB
2839 [0x5c] = SSE_FOP(sub),
2840 [0x5d] = SSE_FOP(min),
2841 [0x5e] = SSE_FOP(div),
2842 [0x5f] = SSE_FOP(max),
2843
2844 [0xc2] = SSE_FOP(cmpeq),
d3eb5eae
BS
2845 [0xc6] = { (SSEFunc_0_epp)gen_helper_shufps,
2846 (SSEFunc_0_epp)gen_helper_shufpd }, /* XXX: casts */
664e0f19 2847
222a3336
AZ
2848 [0x38] = { SSE_SPECIAL, SSE_SPECIAL, NULL, SSE_SPECIAL }, /* SSSE3/SSE4 */
2849 [0x3a] = { SSE_SPECIAL, SSE_SPECIAL }, /* SSSE3/SSE4 */
4242b1bd 2850
664e0f19
FB
2851 /* MMX ops and their SSE extensions */
2852 [0x60] = MMX_OP2(punpcklbw),
2853 [0x61] = MMX_OP2(punpcklwd),
2854 [0x62] = MMX_OP2(punpckldq),
2855 [0x63] = MMX_OP2(packsswb),
2856 [0x64] = MMX_OP2(pcmpgtb),
2857 [0x65] = MMX_OP2(pcmpgtw),
2858 [0x66] = MMX_OP2(pcmpgtl),
2859 [0x67] = MMX_OP2(packuswb),
2860 [0x68] = MMX_OP2(punpckhbw),
2861 [0x69] = MMX_OP2(punpckhwd),
2862 [0x6a] = MMX_OP2(punpckhdq),
2863 [0x6b] = MMX_OP2(packssdw),
a7812ae4
PB
2864 [0x6c] = { NULL, gen_helper_punpcklqdq_xmm },
2865 [0x6d] = { NULL, gen_helper_punpckhqdq_xmm },
664e0f19
FB
2866 [0x6e] = { SSE_SPECIAL, SSE_SPECIAL }, /* movd mm, ea */
2867 [0x6f] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movq, movdqa, , movqdu */
d3eb5eae
BS
2868 [0x70] = { (SSEFunc_0_epp)gen_helper_pshufw_mmx,
2869 (SSEFunc_0_epp)gen_helper_pshufd_xmm,
2870 (SSEFunc_0_epp)gen_helper_pshufhw_xmm,
2871 (SSEFunc_0_epp)gen_helper_pshuflw_xmm }, /* XXX: casts */
664e0f19
FB
2872 [0x71] = { SSE_SPECIAL, SSE_SPECIAL }, /* shiftw */
2873 [0x72] = { SSE_SPECIAL, SSE_SPECIAL }, /* shiftd */
2874 [0x73] = { SSE_SPECIAL, SSE_SPECIAL }, /* shiftq */
2875 [0x74] = MMX_OP2(pcmpeqb),
2876 [0x75] = MMX_OP2(pcmpeqw),
2877 [0x76] = MMX_OP2(pcmpeql),
a35f3ec7 2878 [0x77] = { SSE_DUMMY }, /* emms */
d9f4bb27
AP
2879 [0x78] = { NULL, SSE_SPECIAL, NULL, SSE_SPECIAL }, /* extrq_i, insertq_i */
2880 [0x79] = { NULL, gen_helper_extrq_r, NULL, gen_helper_insertq_r },
a7812ae4
PB
2881 [0x7c] = { NULL, gen_helper_haddpd, NULL, gen_helper_haddps },
2882 [0x7d] = { NULL, gen_helper_hsubpd, NULL, gen_helper_hsubps },
664e0f19
FB
2883 [0x7e] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movd, movd, , movq */
2884 [0x7f] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movq, movdqa, movdqu */
2885 [0xc4] = { SSE_SPECIAL, SSE_SPECIAL }, /* pinsrw */
2886 [0xc5] = { SSE_SPECIAL, SSE_SPECIAL }, /* pextrw */
a7812ae4 2887 [0xd0] = { NULL, gen_helper_addsubpd, NULL, gen_helper_addsubps },
664e0f19
FB
2888 [0xd1] = MMX_OP2(psrlw),
2889 [0xd2] = MMX_OP2(psrld),
2890 [0xd3] = MMX_OP2(psrlq),
2891 [0xd4] = MMX_OP2(paddq),
2892 [0xd5] = MMX_OP2(pmullw),
2893 [0xd6] = { NULL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL },
2894 [0xd7] = { SSE_SPECIAL, SSE_SPECIAL }, /* pmovmskb */
2895 [0xd8] = MMX_OP2(psubusb),
2896 [0xd9] = MMX_OP2(psubusw),
2897 [0xda] = MMX_OP2(pminub),
2898 [0xdb] = MMX_OP2(pand),
2899 [0xdc] = MMX_OP2(paddusb),
2900 [0xdd] = MMX_OP2(paddusw),
2901 [0xde] = MMX_OP2(pmaxub),
2902 [0xdf] = MMX_OP2(pandn),
2903 [0xe0] = MMX_OP2(pavgb),
2904 [0xe1] = MMX_OP2(psraw),
2905 [0xe2] = MMX_OP2(psrad),
2906 [0xe3] = MMX_OP2(pavgw),
2907 [0xe4] = MMX_OP2(pmulhuw),
2908 [0xe5] = MMX_OP2(pmulhw),
a7812ae4 2909 [0xe6] = { NULL, gen_helper_cvttpd2dq, gen_helper_cvtdq2pd, gen_helper_cvtpd2dq },
664e0f19
FB
2910 [0xe7] = { SSE_SPECIAL , SSE_SPECIAL }, /* movntq, movntq */
2911 [0xe8] = MMX_OP2(psubsb),
2912 [0xe9] = MMX_OP2(psubsw),
2913 [0xea] = MMX_OP2(pminsw),
2914 [0xeb] = MMX_OP2(por),
2915 [0xec] = MMX_OP2(paddsb),
2916 [0xed] = MMX_OP2(paddsw),
2917 [0xee] = MMX_OP2(pmaxsw),
2918 [0xef] = MMX_OP2(pxor),
465e9838 2919 [0xf0] = { NULL, NULL, NULL, SSE_SPECIAL }, /* lddqu */
664e0f19
FB
2920 [0xf1] = MMX_OP2(psllw),
2921 [0xf2] = MMX_OP2(pslld),
2922 [0xf3] = MMX_OP2(psllq),
2923 [0xf4] = MMX_OP2(pmuludq),
2924 [0xf5] = MMX_OP2(pmaddwd),
2925 [0xf6] = MMX_OP2(psadbw),
d3eb5eae
BS
2926 [0xf7] = { (SSEFunc_0_epp)gen_helper_maskmov_mmx,
2927 (SSEFunc_0_epp)gen_helper_maskmov_xmm }, /* XXX: casts */
664e0f19
FB
2928 [0xf8] = MMX_OP2(psubb),
2929 [0xf9] = MMX_OP2(psubw),
2930 [0xfa] = MMX_OP2(psubl),
2931 [0xfb] = MMX_OP2(psubq),
2932 [0xfc] = MMX_OP2(paddb),
2933 [0xfd] = MMX_OP2(paddw),
2934 [0xfe] = MMX_OP2(paddl),
2935};
2936
d3eb5eae 2937static const SSEFunc_0_epp sse_op_table2[3 * 8][2] = {
664e0f19
FB
2938 [0 + 2] = MMX_OP2(psrlw),
2939 [0 + 4] = MMX_OP2(psraw),
2940 [0 + 6] = MMX_OP2(psllw),
2941 [8 + 2] = MMX_OP2(psrld),
2942 [8 + 4] = MMX_OP2(psrad),
2943 [8 + 6] = MMX_OP2(pslld),
2944 [16 + 2] = MMX_OP2(psrlq),
a7812ae4 2945 [16 + 3] = { NULL, gen_helper_psrldq_xmm },
664e0f19 2946 [16 + 6] = MMX_OP2(psllq),
a7812ae4 2947 [16 + 7] = { NULL, gen_helper_pslldq_xmm },
664e0f19
FB
2948};
2949
d3eb5eae 2950static const SSEFunc_0_epi sse_op_table3ai[] = {
a7812ae4 2951 gen_helper_cvtsi2ss,
11f8cdbc 2952 gen_helper_cvtsi2sd
c4baa050 2953};
a7812ae4 2954
11f8cdbc 2955#ifdef TARGET_X86_64
d3eb5eae 2956static const SSEFunc_0_epl sse_op_table3aq[] = {
11f8cdbc
SW
2957 gen_helper_cvtsq2ss,
2958 gen_helper_cvtsq2sd
2959};
2960#endif
2961
d3eb5eae 2962static const SSEFunc_i_ep sse_op_table3bi[] = {
a7812ae4 2963 gen_helper_cvttss2si,
a7812ae4 2964 gen_helper_cvtss2si,
bedc2ac1 2965 gen_helper_cvttsd2si,
11f8cdbc 2966 gen_helper_cvtsd2si
664e0f19 2967};
3b46e624 2968
11f8cdbc 2969#ifdef TARGET_X86_64
d3eb5eae 2970static const SSEFunc_l_ep sse_op_table3bq[] = {
11f8cdbc 2971 gen_helper_cvttss2sq,
11f8cdbc 2972 gen_helper_cvtss2sq,
bedc2ac1 2973 gen_helper_cvttsd2sq,
11f8cdbc
SW
2974 gen_helper_cvtsd2sq
2975};
2976#endif
2977
d3eb5eae 2978static const SSEFunc_0_epp sse_op_table4[8][4] = {
664e0f19
FB
2979 SSE_FOP(cmpeq),
2980 SSE_FOP(cmplt),
2981 SSE_FOP(cmple),
2982 SSE_FOP(cmpunord),
2983 SSE_FOP(cmpneq),
2984 SSE_FOP(cmpnlt),
2985 SSE_FOP(cmpnle),
2986 SSE_FOP(cmpord),
2987};
3b46e624 2988
d3eb5eae 2989static const SSEFunc_0_epp sse_op_table5[256] = {
a7812ae4
PB
2990 [0x0c] = gen_helper_pi2fw,
2991 [0x0d] = gen_helper_pi2fd,
2992 [0x1c] = gen_helper_pf2iw,
2993 [0x1d] = gen_helper_pf2id,
2994 [0x8a] = gen_helper_pfnacc,
2995 [0x8e] = gen_helper_pfpnacc,
2996 [0x90] = gen_helper_pfcmpge,
2997 [0x94] = gen_helper_pfmin,
2998 [0x96] = gen_helper_pfrcp,
2999 [0x97] = gen_helper_pfrsqrt,
3000 [0x9a] = gen_helper_pfsub,
3001 [0x9e] = gen_helper_pfadd,
3002 [0xa0] = gen_helper_pfcmpgt,
3003 [0xa4] = gen_helper_pfmax,
3004 [0xa6] = gen_helper_movq, /* pfrcpit1; no need to actually increase precision */
3005 [0xa7] = gen_helper_movq, /* pfrsqit1 */
3006 [0xaa] = gen_helper_pfsubr,
3007 [0xae] = gen_helper_pfacc,
3008 [0xb0] = gen_helper_pfcmpeq,
3009 [0xb4] = gen_helper_pfmul,
3010 [0xb6] = gen_helper_movq, /* pfrcpit2 */
3011 [0xb7] = gen_helper_pmulhrw_mmx,
3012 [0xbb] = gen_helper_pswapd,
3013 [0xbf] = gen_helper_pavgb_mmx /* pavgusb */
a35f3ec7
AJ
3014};
3015
d3eb5eae
BS
3016struct SSEOpHelper_epp {
3017 SSEFunc_0_epp op[2];
c4baa050
BS
3018 uint32_t ext_mask;
3019};
3020
d3eb5eae
BS
3021struct SSEOpHelper_eppi {
3022 SSEFunc_0_eppi op[2];
c4baa050 3023 uint32_t ext_mask;
222a3336 3024};
c4baa050 3025
222a3336 3026#define SSSE3_OP(x) { MMX_OP2(x), CPUID_EXT_SSSE3 }
a7812ae4
PB
3027#define SSE41_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_SSE41 }
3028#define SSE42_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_SSE42 }
222a3336 3029#define SSE41_SPECIAL { { NULL, SSE_SPECIAL }, CPUID_EXT_SSE41 }
c4baa050 3030
d3eb5eae 3031static const struct SSEOpHelper_epp sse_op_table6[256] = {
222a3336
AZ
3032 [0x00] = SSSE3_OP(pshufb),
3033 [0x01] = SSSE3_OP(phaddw),
3034 [0x02] = SSSE3_OP(phaddd),
3035 [0x03] = SSSE3_OP(phaddsw),
3036 [0x04] = SSSE3_OP(pmaddubsw),
3037 [0x05] = SSSE3_OP(phsubw),
3038 [0x06] = SSSE3_OP(phsubd),
3039 [0x07] = SSSE3_OP(phsubsw),
3040 [0x08] = SSSE3_OP(psignb),
3041 [0x09] = SSSE3_OP(psignw),
3042 [0x0a] = SSSE3_OP(psignd),
3043 [0x0b] = SSSE3_OP(pmulhrsw),
3044 [0x10] = SSE41_OP(pblendvb),
3045 [0x14] = SSE41_OP(blendvps),
3046 [0x15] = SSE41_OP(blendvpd),
3047 [0x17] = SSE41_OP(ptest),
3048 [0x1c] = SSSE3_OP(pabsb),
3049 [0x1d] = SSSE3_OP(pabsw),
3050 [0x1e] = SSSE3_OP(pabsd),
3051 [0x20] = SSE41_OP(pmovsxbw),
3052 [0x21] = SSE41_OP(pmovsxbd),
3053 [0x22] = SSE41_OP(pmovsxbq),
3054 [0x23] = SSE41_OP(pmovsxwd),
3055 [0x24] = SSE41_OP(pmovsxwq),
3056 [0x25] = SSE41_OP(pmovsxdq),
3057 [0x28] = SSE41_OP(pmuldq),
3058 [0x29] = SSE41_OP(pcmpeqq),
3059 [0x2a] = SSE41_SPECIAL, /* movntqda */
3060 [0x2b] = SSE41_OP(packusdw),
3061 [0x30] = SSE41_OP(pmovzxbw),
3062 [0x31] = SSE41_OP(pmovzxbd),
3063 [0x32] = SSE41_OP(pmovzxbq),
3064 [0x33] = SSE41_OP(pmovzxwd),
3065 [0x34] = SSE41_OP(pmovzxwq),
3066 [0x35] = SSE41_OP(pmovzxdq),
3067 [0x37] = SSE42_OP(pcmpgtq),
3068 [0x38] = SSE41_OP(pminsb),
3069 [0x39] = SSE41_OP(pminsd),
3070 [0x3a] = SSE41_OP(pminuw),
3071 [0x3b] = SSE41_OP(pminud),
3072 [0x3c] = SSE41_OP(pmaxsb),
3073 [0x3d] = SSE41_OP(pmaxsd),
3074 [0x3e] = SSE41_OP(pmaxuw),
3075 [0x3f] = SSE41_OP(pmaxud),
3076 [0x40] = SSE41_OP(pmulld),
3077 [0x41] = SSE41_OP(phminposuw),
4242b1bd
AZ
3078};
3079
d3eb5eae 3080static const struct SSEOpHelper_eppi sse_op_table7[256] = {
222a3336
AZ
3081 [0x08] = SSE41_OP(roundps),
3082 [0x09] = SSE41_OP(roundpd),
3083 [0x0a] = SSE41_OP(roundss),
3084 [0x0b] = SSE41_OP(roundsd),
3085 [0x0c] = SSE41_OP(blendps),
3086 [0x0d] = SSE41_OP(blendpd),
3087 [0x0e] = SSE41_OP(pblendw),
3088 [0x0f] = SSSE3_OP(palignr),
3089 [0x14] = SSE41_SPECIAL, /* pextrb */
3090 [0x15] = SSE41_SPECIAL, /* pextrw */
3091 [0x16] = SSE41_SPECIAL, /* pextrd/pextrq */
3092 [0x17] = SSE41_SPECIAL, /* extractps */
3093 [0x20] = SSE41_SPECIAL, /* pinsrb */
3094 [0x21] = SSE41_SPECIAL, /* insertps */
3095 [0x22] = SSE41_SPECIAL, /* pinsrd/pinsrq */
3096 [0x40] = SSE41_OP(dpps),
3097 [0x41] = SSE41_OP(dppd),
3098 [0x42] = SSE41_OP(mpsadbw),
3099 [0x60] = SSE42_OP(pcmpestrm),
3100 [0x61] = SSE42_OP(pcmpestri),
3101 [0x62] = SSE42_OP(pcmpistrm),
3102 [0x63] = SSE42_OP(pcmpistri),
4242b1bd
AZ
3103};
3104
0af10c86
BS
3105static void gen_sse(CPUX86State *env, DisasContext *s, int b,
3106 target_ulong pc_start, int rex_r)
664e0f19
FB
3107{
3108 int b1, op1_offset, op2_offset, is_xmm, val, ot;
3109 int modrm, mod, rm, reg, reg_addr, offset_addr;
d3eb5eae
BS
3110 SSEFunc_0_epp sse_fn_epp;
3111 SSEFunc_0_eppi sse_fn_eppi;
c4baa050 3112 SSEFunc_0_ppi sse_fn_ppi;
d3eb5eae 3113 SSEFunc_0_eppt sse_fn_eppt;
664e0f19
FB
3114
3115 b &= 0xff;
5fafdf24 3116 if (s->prefix & PREFIX_DATA)
664e0f19 3117 b1 = 1;
5fafdf24 3118 else if (s->prefix & PREFIX_REPZ)
664e0f19 3119 b1 = 2;
5fafdf24 3120 else if (s->prefix & PREFIX_REPNZ)
664e0f19
FB
3121 b1 = 3;
3122 else
3123 b1 = 0;
d3eb5eae
BS
3124 sse_fn_epp = sse_op_table1[b][b1];
3125 if (!sse_fn_epp) {
664e0f19 3126 goto illegal_op;
c4baa050 3127 }
a35f3ec7 3128 if ((b <= 0x5f && b >= 0x10) || b == 0xc6 || b == 0xc2) {
664e0f19
FB
3129 is_xmm = 1;
3130 } else {
3131 if (b1 == 0) {
3132 /* MMX case */
3133 is_xmm = 0;
3134 } else {
3135 is_xmm = 1;
3136 }
3137 }
3138 /* simple MMX/SSE operation */
3139 if (s->flags & HF_TS_MASK) {
3140 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
3141 return;
3142 }
3143 if (s->flags & HF_EM_MASK) {
3144 illegal_op:
3145 gen_exception(s, EXCP06_ILLOP, pc_start - s->cs_base);
3146 return;
3147 }
3148 if (is_xmm && !(s->flags & HF_OSFXSR_MASK))
4242b1bd
AZ
3149 if ((b != 0x38 && b != 0x3a) || (s->prefix & PREFIX_DATA))
3150 goto illegal_op;
e771edab
AJ
3151 if (b == 0x0e) {
3152 if (!(s->cpuid_ext2_features & CPUID_EXT2_3DNOW))
3153 goto illegal_op;
3154 /* femms */
d3eb5eae 3155 gen_helper_emms(cpu_env);
e771edab
AJ
3156 return;
3157 }
3158 if (b == 0x77) {
3159 /* emms */
d3eb5eae 3160 gen_helper_emms(cpu_env);
664e0f19
FB
3161 return;
3162 }
3163 /* prepare MMX state (XXX: optimize by storing fptt and fptags in
3164 the static cpu state) */
3165 if (!is_xmm) {
d3eb5eae 3166 gen_helper_enter_mmx(cpu_env);
664e0f19
FB
3167 }
3168
0af10c86 3169 modrm = cpu_ldub_code(env, s->pc++);
664e0f19
FB
3170 reg = ((modrm >> 3) & 7);
3171 if (is_xmm)
3172 reg |= rex_r;
3173 mod = (modrm >> 6) & 3;
d3eb5eae 3174 if (sse_fn_epp == SSE_SPECIAL) {
664e0f19
FB
3175 b |= (b1 << 8);
3176 switch(b) {
3177 case 0x0e7: /* movntq */
5fafdf24 3178 if (mod == 3)
664e0f19 3179 goto illegal_op;
0af10c86 3180 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3181 gen_stq_env_A0(s->mem_index, offsetof(CPUX86State,fpregs[reg].mmx));
664e0f19
FB
3182 break;
3183 case 0x1e7: /* movntdq */
3184 case 0x02b: /* movntps */
3185 case 0x12b: /* movntps */
2e21e749
T
3186 if (mod == 3)
3187 goto illegal_op;
0af10c86 3188 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
2e21e749
T
3189 gen_sto_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg]));
3190 break;
465e9838
FB
3191 case 0x3f0: /* lddqu */
3192 if (mod == 3)
664e0f19 3193 goto illegal_op;
0af10c86 3194 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
c2254920 3195 gen_ldo_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg]));
664e0f19 3196 break;
d9f4bb27
AP
3197 case 0x22b: /* movntss */
3198 case 0x32b: /* movntsd */
3199 if (mod == 3)
3200 goto illegal_op;
0af10c86 3201 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
d9f4bb27
AP
3202 if (b1 & 1) {
3203 gen_stq_env_A0(s->mem_index, offsetof(CPUX86State,
3204 xmm_regs[reg]));
3205 } else {
3206 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
3207 xmm_regs[reg].XMM_L(0)));
3208 gen_op_st_T0_A0(OT_LONG + s->mem_index);
3209 }
3210 break;
664e0f19 3211 case 0x6e: /* movd mm, ea */
dabd98dd
FB
3212#ifdef TARGET_X86_64
3213 if (s->dflag == 2) {
0af10c86 3214 gen_ldst_modrm(env, s, modrm, OT_QUAD, OR_TMP0, 0);
5af45186 3215 tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,fpregs[reg].mmx));
5fafdf24 3216 } else
dabd98dd
FB
3217#endif
3218 {
0af10c86 3219 gen_ldst_modrm(env, s, modrm, OT_LONG, OR_TMP0, 0);
5af45186
FB
3220 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3221 offsetof(CPUX86State,fpregs[reg].mmx));
a7812ae4
PB
3222 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
3223 gen_helper_movl_mm_T0_mmx(cpu_ptr0, cpu_tmp2_i32);
dabd98dd 3224 }
664e0f19
FB
3225 break;
3226 case 0x16e: /* movd xmm, ea */
dabd98dd
FB
3227#ifdef TARGET_X86_64
3228 if (s->dflag == 2) {
0af10c86 3229 gen_ldst_modrm(env, s, modrm, OT_QUAD, OR_TMP0, 0);
5af45186
FB
3230 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3231 offsetof(CPUX86State,xmm_regs[reg]));
a7812ae4 3232 gen_helper_movq_mm_T0_xmm(cpu_ptr0, cpu_T[0]);
5fafdf24 3233 } else
dabd98dd
FB
3234#endif
3235 {
0af10c86 3236 gen_ldst_modrm(env, s, modrm, OT_LONG, OR_TMP0, 0);
5af45186
FB
3237 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3238 offsetof(CPUX86State,xmm_regs[reg]));
b6abf97d 3239 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
a7812ae4 3240 gen_helper_movl_mm_T0_xmm(cpu_ptr0, cpu_tmp2_i32);
dabd98dd 3241 }
664e0f19
FB
3242 break;
3243 case 0x6f: /* movq mm, ea */
3244 if (mod != 3) {
0af10c86 3245 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3246 gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,fpregs[reg].mmx));
664e0f19
FB
3247 } else {
3248 rm = (modrm & 7);
b6abf97d 3249 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env,
5af45186 3250 offsetof(CPUX86State,fpregs[rm].mmx));
b6abf97d 3251 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env,
5af45186 3252 offsetof(CPUX86State,fpregs[reg].mmx));
664e0f19
FB
3253 }
3254 break;
3255 case 0x010: /* movups */
3256 case 0x110: /* movupd */
3257 case 0x028: /* movaps */
3258 case 0x128: /* movapd */
3259 case 0x16f: /* movdqa xmm, ea */
3260 case 0x26f: /* movdqu xmm, ea */
3261 if (mod != 3) {
0af10c86 3262 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3263 gen_ldo_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg]));
664e0f19
FB
3264 } else {
3265 rm = (modrm & 7) | REX_B(s);
3266 gen_op_movo(offsetof(CPUX86State,xmm_regs[reg]),
3267 offsetof(CPUX86State,xmm_regs[rm]));
3268 }
3269 break;
3270 case 0x210: /* movss xmm, ea */
3271 if (mod != 3) {
0af10c86 3272 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
57fec1fe 3273 gen_op_ld_T0_A0(OT_LONG + s->mem_index);
651ba608 3274 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
664e0f19 3275 gen_op_movl_T0_0();
651ba608
FB
3276 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(1)));
3277 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(2)));
3278 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(3)));
664e0f19
FB
3279 } else {
3280 rm = (modrm & 7) | REX_B(s);
3281 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)),
3282 offsetof(CPUX86State,xmm_regs[rm].XMM_L(0)));
3283 }
3284 break;
3285 case 0x310: /* movsd xmm, ea */
3286 if (mod != 3) {
0af10c86 3287 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3288 gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
664e0f19 3289 gen_op_movl_T0_0();
651ba608
FB
3290 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(2)));
3291 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(3)));
664e0f19
FB
3292 } else {
3293 rm = (modrm & 7) | REX_B(s);
3294 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)),
3295 offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)));
3296 }
3297 break;
3298 case 0x012: /* movlps */
3299 case 0x112: /* movlpd */
3300 if (mod != 3) {
0af10c86 3301 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3302 gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
664e0f19
FB
3303 } else {
3304 /* movhlps */
3305 rm = (modrm & 7) | REX_B(s);
3306 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)),
3307 offsetof(CPUX86State,xmm_regs[rm].XMM_Q(1)));
3308 }
3309 break;
465e9838
FB
3310 case 0x212: /* movsldup */
3311 if (mod != 3) {
0af10c86 3312 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3313 gen_ldo_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg]));
465e9838
FB
3314 } else {
3315 rm = (modrm & 7) | REX_B(s);
3316 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)),
3317 offsetof(CPUX86State,xmm_regs[rm].XMM_L(0)));
3318 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(2)),
3319 offsetof(CPUX86State,xmm_regs[rm].XMM_L(2)));
3320 }
3321 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(1)),
3322 offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
3323 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(3)),
3324 offsetof(CPUX86State,xmm_regs[reg].XMM_L(2)));
3325 break;
3326 case 0x312: /* movddup */
3327 if (mod != 3) {
0af10c86 3328 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3329 gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
465e9838
FB
3330 } else {
3331 rm = (modrm & 7) | REX_B(s);
3332 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)),
3333 offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)));
3334 }
3335 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(1)),
ba6526df 3336 offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
465e9838 3337 break;
664e0f19
FB
3338 case 0x016: /* movhps */
3339 case 0x116: /* movhpd */
3340 if (mod != 3) {
0af10c86 3341 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3342 gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(1)));
664e0f19
FB
3343 } else {
3344 /* movlhps */
3345 rm = (modrm & 7) | REX_B(s);
3346 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(1)),
3347 offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)));
3348 }
3349 break;
3350 case 0x216: /* movshdup */
3351 if (mod != 3) {
0af10c86 3352 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3353 gen_ldo_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg]));
664e0f19
FB
3354 } else {
3355 rm = (modrm & 7) | REX_B(s);
3356 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(1)),
3357 offsetof(CPUX86State,xmm_regs[rm].XMM_L(1)));
3358 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(3)),
3359 offsetof(CPUX86State,xmm_regs[rm].XMM_L(3)));
3360 }
3361 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)),
3362 offsetof(CPUX86State,xmm_regs[reg].XMM_L(1)));
3363 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(2)),
3364 offsetof(CPUX86State,xmm_regs[reg].XMM_L(3)));
3365 break;
d9f4bb27
AP
3366 case 0x178:
3367 case 0x378:
3368 {
3369 int bit_index, field_length;
3370
3371 if (b1 == 1 && reg != 0)
3372 goto illegal_op;
0af10c86
BS
3373 field_length = cpu_ldub_code(env, s->pc++) & 0x3F;
3374 bit_index = cpu_ldub_code(env, s->pc++) & 0x3F;
d9f4bb27
AP
3375 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3376 offsetof(CPUX86State,xmm_regs[reg]));
3377 if (b1 == 1)
d3eb5eae
BS
3378 gen_helper_extrq_i(cpu_env, cpu_ptr0,
3379 tcg_const_i32(bit_index),
3380 tcg_const_i32(field_length));
d9f4bb27 3381 else
d3eb5eae
BS
3382 gen_helper_insertq_i(cpu_env, cpu_ptr0,
3383 tcg_const_i32(bit_index),
3384 tcg_const_i32(field_length));
d9f4bb27
AP
3385 }
3386 break;
664e0f19 3387 case 0x7e: /* movd ea, mm */
dabd98dd
FB
3388#ifdef TARGET_X86_64
3389 if (s->dflag == 2) {
5af45186
FB
3390 tcg_gen_ld_i64(cpu_T[0], cpu_env,
3391 offsetof(CPUX86State,fpregs[reg].mmx));
0af10c86 3392 gen_ldst_modrm(env, s, modrm, OT_QUAD, OR_TMP0, 1);
5fafdf24 3393 } else
dabd98dd
FB
3394#endif
3395 {
5af45186
FB
3396 tcg_gen_ld32u_tl(cpu_T[0], cpu_env,
3397 offsetof(CPUX86State,fpregs[reg].mmx.MMX_L(0)));
0af10c86 3398 gen_ldst_modrm(env, s, modrm, OT_LONG, OR_TMP0, 1);
dabd98dd 3399 }
664e0f19
FB
3400 break;
3401 case 0x17e: /* movd ea, xmm */
dabd98dd
FB
3402#ifdef TARGET_X86_64
3403 if (s->dflag == 2) {
5af45186
FB
3404 tcg_gen_ld_i64(cpu_T[0], cpu_env,
3405 offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
0af10c86 3406 gen_ldst_modrm(env, s, modrm, OT_QUAD, OR_TMP0, 1);
5fafdf24 3407 } else
dabd98dd
FB
3408#endif
3409 {
5af45186
FB
3410 tcg_gen_ld32u_tl(cpu_T[0], cpu_env,
3411 offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
0af10c86 3412 gen_ldst_modrm(env, s, modrm, OT_LONG, OR_TMP0, 1);
dabd98dd 3413 }
664e0f19
FB
3414 break;
3415 case 0x27e: /* movq xmm, ea */
3416 if (mod != 3) {
0af10c86 3417 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3418 gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
664e0f19
FB
3419 } else {
3420 rm = (modrm & 7) | REX_B(s);
3421 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)),
3422 offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)));
3423 }
3424 gen_op_movq_env_0(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(1)));
3425 break;
3426 case 0x7f: /* movq ea, mm */
3427 if (mod != 3) {
0af10c86 3428 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3429 gen_stq_env_A0(s->mem_index, offsetof(CPUX86State,fpregs[reg].mmx));
664e0f19
FB
3430 } else {
3431 rm = (modrm & 7);
3432 gen_op_movq(offsetof(CPUX86State,fpregs[rm].mmx),
3433 offsetof(CPUX86State,fpregs[reg].mmx));
3434 }
3435 break;
3436 case 0x011: /* movups */
3437 case 0x111: /* movupd */
3438 case 0x029: /* movaps */
3439 case 0x129: /* movapd */
3440 case 0x17f: /* movdqa ea, xmm */
3441 case 0x27f: /* movdqu ea, xmm */
3442 if (mod != 3) {
0af10c86 3443 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3444 gen_sto_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg]));
664e0f19
FB
3445 } else {
3446 rm = (modrm & 7) | REX_B(s);
3447 gen_op_movo(offsetof(CPUX86State,xmm_regs[rm]),
3448 offsetof(CPUX86State,xmm_regs[reg]));
3449 }
3450 break;
3451 case 0x211: /* movss ea, xmm */
3452 if (mod != 3) {
0af10c86 3453 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
651ba608 3454 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
57fec1fe 3455 gen_op_st_T0_A0(OT_LONG + s->mem_index);
664e0f19
FB
3456 } else {
3457 rm = (modrm & 7) | REX_B(s);
3458 gen_op_movl(offsetof(CPUX86State,xmm_regs[rm].XMM_L(0)),
3459 offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
3460 }
3461 break;
3462 case 0x311: /* movsd ea, xmm */
3463 if (mod != 3) {
0af10c86 3464 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3465 gen_stq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
664e0f19
FB
3466 } else {
3467 rm = (modrm & 7) | REX_B(s);
3468 gen_op_movq(offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)),
3469 offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
3470 }
3471 break;
3472 case 0x013: /* movlps */
3473 case 0x113: /* movlpd */
3474 if (mod != 3) {
0af10c86 3475 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3476 gen_stq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
664e0f19
FB
3477 } else {
3478 goto illegal_op;
3479 }
3480 break;
3481 case 0x017: /* movhps */
3482 case 0x117: /* movhpd */
3483 if (mod != 3) {
0af10c86 3484 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3485 gen_stq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(1)));
664e0f19
FB
3486 } else {
3487 goto illegal_op;
3488 }
3489 break;
3490 case 0x71: /* shift mm, im */
3491 case 0x72:
3492 case 0x73:
3493 case 0x171: /* shift xmm, im */
3494 case 0x172:
3495 case 0x173:
c045af25
AK
3496 if (b1 >= 2) {
3497 goto illegal_op;
3498 }
0af10c86 3499 val = cpu_ldub_code(env, s->pc++);
664e0f19
FB
3500 if (is_xmm) {
3501 gen_op_movl_T0_im(val);
651ba608 3502 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(0)));
664e0f19 3503 gen_op_movl_T0_0();
651ba608 3504 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(1)));
664e0f19
FB
3505 op1_offset = offsetof(CPUX86State,xmm_t0);
3506 } else {
3507 gen_op_movl_T0_im(val);
651ba608 3508 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,mmx_t0.MMX_L(0)));
664e0f19 3509 gen_op_movl_T0_0();
651ba608 3510 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,mmx_t0.MMX_L(1)));
664e0f19
FB
3511 op1_offset = offsetof(CPUX86State,mmx_t0);
3512 }
d3eb5eae
BS
3513 sse_fn_epp = sse_op_table2[((b - 1) & 3) * 8 +
3514 (((modrm >> 3)) & 7)][b1];
3515 if (!sse_fn_epp) {
664e0f19 3516 goto illegal_op;
c4baa050 3517 }
664e0f19
FB
3518 if (is_xmm) {
3519 rm = (modrm & 7) | REX_B(s);
3520 op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
3521 } else {
3522 rm = (modrm & 7);
3523 op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
3524 }
5af45186
FB
3525 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op2_offset);
3526 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op1_offset);
d3eb5eae 3527 sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
3528 break;
3529 case 0x050: /* movmskps */
664e0f19 3530 rm = (modrm & 7) | REX_B(s);
5af45186
FB
3531 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3532 offsetof(CPUX86State,xmm_regs[rm]));
d3eb5eae 3533 gen_helper_movmskps(cpu_tmp2_i32, cpu_env, cpu_ptr0);
b6abf97d 3534 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
57fec1fe 3535 gen_op_mov_reg_T0(OT_LONG, reg);
664e0f19
FB
3536 break;
3537 case 0x150: /* movmskpd */
664e0f19 3538 rm = (modrm & 7) | REX_B(s);
5af45186
FB
3539 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3540 offsetof(CPUX86State,xmm_regs[rm]));
d3eb5eae 3541 gen_helper_movmskpd(cpu_tmp2_i32, cpu_env, cpu_ptr0);
b6abf97d 3542 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
57fec1fe 3543 gen_op_mov_reg_T0(OT_LONG, reg);
664e0f19
FB
3544 break;
3545 case 0x02a: /* cvtpi2ps */
3546 case 0x12a: /* cvtpi2pd */
d3eb5eae 3547 gen_helper_enter_mmx(cpu_env);
664e0f19 3548 if (mod != 3) {
0af10c86 3549 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
664e0f19 3550 op2_offset = offsetof(CPUX86State,mmx_t0);
8686c490 3551 gen_ldq_env_A0(s->mem_index, op2_offset);
664e0f19
FB
3552 } else {
3553 rm = (modrm & 7);
3554 op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
3555 }
3556 op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
5af45186
FB
3557 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
3558 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
664e0f19
FB
3559 switch(b >> 8) {
3560 case 0x0:
d3eb5eae 3561 gen_helper_cvtpi2ps(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
3562 break;
3563 default:
3564 case 0x1:
d3eb5eae 3565 gen_helper_cvtpi2pd(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
3566 break;
3567 }
3568 break;
3569 case 0x22a: /* cvtsi2ss */
3570 case 0x32a: /* cvtsi2sd */
3571 ot = (s->dflag == 2) ? OT_QUAD : OT_LONG;
0af10c86 3572 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
664e0f19 3573 op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
5af45186 3574 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
28e10711 3575 if (ot == OT_LONG) {
d3eb5eae 3576 SSEFunc_0_epi sse_fn_epi = sse_op_table3ai[(b >> 8) & 1];
28e10711 3577 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
d3eb5eae 3578 sse_fn_epi(cpu_env, cpu_ptr0, cpu_tmp2_i32);
28e10711 3579 } else {
11f8cdbc 3580#ifdef TARGET_X86_64
d3eb5eae
BS
3581 SSEFunc_0_epl sse_fn_epl = sse_op_table3aq[(b >> 8) & 1];
3582 sse_fn_epl(cpu_env, cpu_ptr0, cpu_T[0]);
11f8cdbc
SW
3583#else
3584 goto illegal_op;
3585#endif
28e10711 3586 }
664e0f19
FB
3587 break;
3588 case 0x02c: /* cvttps2pi */
3589 case 0x12c: /* cvttpd2pi */
3590 case 0x02d: /* cvtps2pi */
3591 case 0x12d: /* cvtpd2pi */
d3eb5eae 3592 gen_helper_enter_mmx(cpu_env);
664e0f19 3593 if (mod != 3) {
0af10c86 3594 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
664e0f19 3595 op2_offset = offsetof(CPUX86State,xmm_t0);
8686c490 3596 gen_ldo_env_A0(s->mem_index, op2_offset);
664e0f19
FB
3597 } else {
3598 rm = (modrm & 7) | REX_B(s);
3599 op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
3600 }
3601 op1_offset = offsetof(CPUX86State,fpregs[reg & 7].mmx);
5af45186
FB
3602 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
3603 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
664e0f19
FB
3604 switch(b) {
3605 case 0x02c:
d3eb5eae 3606 gen_helper_cvttps2pi(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
3607 break;
3608 case 0x12c:
d3eb5eae 3609 gen_helper_cvttpd2pi(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
3610 break;
3611 case 0x02d:
d3eb5eae 3612 gen_helper_cvtps2pi(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
3613 break;
3614 case 0x12d:
d3eb5eae 3615 gen_helper_cvtpd2pi(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
3616 break;
3617 }
3618 break;
3619 case 0x22c: /* cvttss2si */
3620 case 0x32c: /* cvttsd2si */
3621 case 0x22d: /* cvtss2si */
3622 case 0x32d: /* cvtsd2si */
3623 ot = (s->dflag == 2) ? OT_QUAD : OT_LONG;
31313213 3624 if (mod != 3) {
0af10c86 3625 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
31313213 3626 if ((b >> 8) & 1) {
8686c490 3627 gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_t0.XMM_Q(0)));
31313213 3628 } else {
57fec1fe 3629 gen_op_ld_T0_A0(OT_LONG + s->mem_index);
651ba608 3630 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(0)));
31313213
FB
3631 }
3632 op2_offset = offsetof(CPUX86State,xmm_t0);
3633 } else {
3634 rm = (modrm & 7) | REX_B(s);
3635 op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
3636 }
5af45186
FB
3637 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op2_offset);
3638 if (ot == OT_LONG) {
d3eb5eae 3639 SSEFunc_i_ep sse_fn_i_ep =
bedc2ac1 3640 sse_op_table3bi[((b >> 7) & 2) | (b & 1)];
d3eb5eae 3641 sse_fn_i_ep(cpu_tmp2_i32, cpu_env, cpu_ptr0);
b6abf97d 3642 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
5af45186 3643 } else {
11f8cdbc 3644#ifdef TARGET_X86_64
d3eb5eae 3645 SSEFunc_l_ep sse_fn_l_ep =
bedc2ac1 3646 sse_op_table3bq[((b >> 7) & 2) | (b & 1)];
d3eb5eae 3647 sse_fn_l_ep(cpu_T[0], cpu_env, cpu_ptr0);
11f8cdbc
SW
3648#else
3649 goto illegal_op;
3650#endif
5af45186 3651 }
57fec1fe 3652 gen_op_mov_reg_T0(ot, reg);
664e0f19
FB
3653 break;
3654 case 0xc4: /* pinsrw */
5fafdf24 3655 case 0x1c4:
d1e42c5c 3656 s->rip_offset = 1;
0af10c86
BS
3657 gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
3658 val = cpu_ldub_code(env, s->pc++);
664e0f19
FB
3659 if (b1) {
3660 val &= 7;
5af45186
FB
3661 tcg_gen_st16_tl(cpu_T[0], cpu_env,
3662 offsetof(CPUX86State,xmm_regs[reg].XMM_W(val)));
664e0f19
FB
3663 } else {
3664 val &= 3;
5af45186
FB
3665 tcg_gen_st16_tl(cpu_T[0], cpu_env,
3666 offsetof(CPUX86State,fpregs[reg].mmx.MMX_W(val)));
664e0f19
FB
3667 }
3668 break;
3669 case 0xc5: /* pextrw */
5fafdf24 3670 case 0x1c5:
664e0f19
FB
3671 if (mod != 3)
3672 goto illegal_op;
6dc2d0da 3673 ot = (s->dflag == 2) ? OT_QUAD : OT_LONG;
0af10c86 3674 val = cpu_ldub_code(env, s->pc++);
664e0f19
FB
3675 if (b1) {
3676 val &= 7;
3677 rm = (modrm & 7) | REX_B(s);
5af45186
FB
3678 tcg_gen_ld16u_tl(cpu_T[0], cpu_env,
3679 offsetof(CPUX86State,xmm_regs[rm].XMM_W(val)));
664e0f19
FB
3680 } else {
3681 val &= 3;
3682 rm = (modrm & 7);
5af45186
FB
3683 tcg_gen_ld16u_tl(cpu_T[0], cpu_env,
3684 offsetof(CPUX86State,fpregs[rm].mmx.MMX_W(val)));
664e0f19
FB
3685 }
3686 reg = ((modrm >> 3) & 7) | rex_r;
6dc2d0da 3687 gen_op_mov_reg_T0(ot, reg);
664e0f19
FB
3688 break;
3689 case 0x1d6: /* movq ea, xmm */
3690 if (mod != 3) {
0af10c86 3691 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3692 gen_stq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
664e0f19
FB
3693 } else {
3694 rm = (modrm & 7) | REX_B(s);
3695 gen_op_movq(offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)),
3696 offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
3697 gen_op_movq_env_0(offsetof(CPUX86State,xmm_regs[rm].XMM_Q(1)));
3698 }
3699 break;
3700 case 0x2d6: /* movq2dq */
d3eb5eae 3701 gen_helper_enter_mmx(cpu_env);
480c1cdb
FB
3702 rm = (modrm & 7);
3703 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)),
3704 offsetof(CPUX86State,fpregs[rm].mmx));
3705 gen_op_movq_env_0(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(1)));
664e0f19
FB
3706 break;
3707 case 0x3d6: /* movdq2q */
d3eb5eae 3708 gen_helper_enter_mmx(cpu_env);
480c1cdb
FB
3709 rm = (modrm & 7) | REX_B(s);
3710 gen_op_movq(offsetof(CPUX86State,fpregs[reg & 7].mmx),
3711 offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)));
664e0f19
FB
3712 break;
3713 case 0xd7: /* pmovmskb */
3714 case 0x1d7:
3715 if (mod != 3)
3716 goto illegal_op;
3717 if (b1) {
3718 rm = (modrm & 7) | REX_B(s);
5af45186 3719 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, offsetof(CPUX86State,xmm_regs[rm]));
d3eb5eae 3720 gen_helper_pmovmskb_xmm(cpu_tmp2_i32, cpu_env, cpu_ptr0);
664e0f19
FB
3721 } else {
3722 rm = (modrm & 7);
5af45186 3723 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, offsetof(CPUX86State,fpregs[rm].mmx));
d3eb5eae 3724 gen_helper_pmovmskb_mmx(cpu_tmp2_i32, cpu_env, cpu_ptr0);
664e0f19 3725 }
b6abf97d 3726 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
664e0f19 3727 reg = ((modrm >> 3) & 7) | rex_r;
57fec1fe 3728 gen_op_mov_reg_T0(OT_LONG, reg);
664e0f19 3729 break;
4242b1bd 3730 case 0x138:
000cacf6
AZ
3731 if (s->prefix & PREFIX_REPNZ)
3732 goto crc32;
3733 case 0x038:
4242b1bd 3734 b = modrm;
0af10c86 3735 modrm = cpu_ldub_code(env, s->pc++);
4242b1bd
AZ
3736 rm = modrm & 7;
3737 reg = ((modrm >> 3) & 7) | rex_r;
3738 mod = (modrm >> 6) & 3;
c045af25
AK
3739 if (b1 >= 2) {
3740 goto illegal_op;
3741 }
4242b1bd 3742
d3eb5eae
BS
3743 sse_fn_epp = sse_op_table6[b].op[b1];
3744 if (!sse_fn_epp) {
4242b1bd 3745 goto illegal_op;
c4baa050 3746 }
222a3336
AZ
3747 if (!(s->cpuid_ext_features & sse_op_table6[b].ext_mask))
3748 goto illegal_op;
4242b1bd
AZ
3749
3750 if (b1) {
3751 op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
3752 if (mod == 3) {
3753 op2_offset = offsetof(CPUX86State,xmm_regs[rm | REX_B(s)]);
3754 } else {
3755 op2_offset = offsetof(CPUX86State,xmm_t0);
0af10c86 3756 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
222a3336
AZ
3757 switch (b) {
3758 case 0x20: case 0x30: /* pmovsxbw, pmovzxbw */
3759 case 0x23: case 0x33: /* pmovsxwd, pmovzxwd */
3760 case 0x25: case 0x35: /* pmovsxdq, pmovzxdq */
3761 gen_ldq_env_A0(s->mem_index, op2_offset +
3762 offsetof(XMMReg, XMM_Q(0)));
3763 break;
3764 case 0x21: case 0x31: /* pmovsxbd, pmovzxbd */
3765 case 0x24: case 0x34: /* pmovsxwq, pmovzxwq */
a7812ae4 3766 tcg_gen_qemu_ld32u(cpu_tmp0, cpu_A0,
222a3336 3767 (s->mem_index >> 2) - 1);
a7812ae4 3768 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_tmp0);
222a3336
AZ
3769 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, op2_offset +
3770 offsetof(XMMReg, XMM_L(0)));
3771 break;
3772 case 0x22: case 0x32: /* pmovsxbq, pmovzxbq */
3773 tcg_gen_qemu_ld16u(cpu_tmp0, cpu_A0,
3774 (s->mem_index >> 2) - 1);
3775 tcg_gen_st16_tl(cpu_tmp0, cpu_env, op2_offset +
3776 offsetof(XMMReg, XMM_W(0)));
3777 break;
3778 case 0x2a: /* movntqda */
3779 gen_ldo_env_A0(s->mem_index, op1_offset);
3780 return;
3781 default:
3782 gen_ldo_env_A0(s->mem_index, op2_offset);
3783 }
4242b1bd
AZ
3784 }
3785 } else {
3786 op1_offset = offsetof(CPUX86State,fpregs[reg].mmx);
3787 if (mod == 3) {
3788 op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
3789 } else {
3790 op2_offset = offsetof(CPUX86State,mmx_t0);
0af10c86 3791 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
4242b1bd
AZ
3792 gen_ldq_env_A0(s->mem_index, op2_offset);
3793 }
3794 }
d3eb5eae 3795 if (sse_fn_epp == SSE_SPECIAL) {
222a3336 3796 goto illegal_op;
c4baa050 3797 }
222a3336 3798
4242b1bd
AZ
3799 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
3800 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
d3eb5eae 3801 sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
222a3336 3802
3ca51d07
RH
3803 if (b == 0x17) {
3804 set_cc_op(s, CC_OP_EFLAGS);
3805 }
4242b1bd 3806 break;
222a3336
AZ
3807 case 0x338: /* crc32 */
3808 crc32:
3809 b = modrm;
0af10c86 3810 modrm = cpu_ldub_code(env, s->pc++);
222a3336
AZ
3811 reg = ((modrm >> 3) & 7) | rex_r;
3812
3813 if (b != 0xf0 && b != 0xf1)
3814 goto illegal_op;
3815 if (!(s->cpuid_ext_features & CPUID_EXT_SSE42))
4242b1bd
AZ
3816 goto illegal_op;
3817
222a3336
AZ
3818 if (b == 0xf0)
3819 ot = OT_BYTE;
3820 else if (b == 0xf1 && s->dflag != 2)
3821 if (s->prefix & PREFIX_DATA)
3822 ot = OT_WORD;
3823 else
3824 ot = OT_LONG;
3825 else
3826 ot = OT_QUAD;
3827
3828 gen_op_mov_TN_reg(OT_LONG, 0, reg);
3829 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
0af10c86 3830 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
a7812ae4
PB
3831 gen_helper_crc32(cpu_T[0], cpu_tmp2_i32,
3832 cpu_T[0], tcg_const_i32(8 << ot));
222a3336
AZ
3833
3834 ot = (s->dflag == 2) ? OT_QUAD : OT_LONG;
3835 gen_op_mov_reg_T0(ot, reg);
3836 break;
3837 case 0x03a:
3838 case 0x13a:
4242b1bd 3839 b = modrm;
0af10c86 3840 modrm = cpu_ldub_code(env, s->pc++);
4242b1bd
AZ
3841 rm = modrm & 7;
3842 reg = ((modrm >> 3) & 7) | rex_r;
3843 mod = (modrm >> 6) & 3;
c045af25
AK
3844 if (b1 >= 2) {
3845 goto illegal_op;
3846 }
4242b1bd 3847
d3eb5eae
BS
3848 sse_fn_eppi = sse_op_table7[b].op[b1];
3849 if (!sse_fn_eppi) {
4242b1bd 3850 goto illegal_op;
c4baa050 3851 }
222a3336
AZ
3852 if (!(s->cpuid_ext_features & sse_op_table7[b].ext_mask))
3853 goto illegal_op;
3854
d3eb5eae 3855 if (sse_fn_eppi == SSE_SPECIAL) {
222a3336
AZ
3856 ot = (s->dflag == 2) ? OT_QUAD : OT_LONG;
3857 rm = (modrm & 7) | REX_B(s);
3858 if (mod != 3)
0af10c86 3859 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
222a3336 3860 reg = ((modrm >> 3) & 7) | rex_r;
0af10c86 3861 val = cpu_ldub_code(env, s->pc++);
222a3336
AZ
3862 switch (b) {
3863 case 0x14: /* pextrb */
3864 tcg_gen_ld8u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
3865 xmm_regs[reg].XMM_B(val & 15)));
3866 if (mod == 3)
3867 gen_op_mov_reg_T0(ot, rm);
3868 else
3869 tcg_gen_qemu_st8(cpu_T[0], cpu_A0,
3870 (s->mem_index >> 2) - 1);
3871 break;
3872 case 0x15: /* pextrw */
3873 tcg_gen_ld16u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
3874 xmm_regs[reg].XMM_W(val & 7)));
3875 if (mod == 3)
3876 gen_op_mov_reg_T0(ot, rm);
3877 else
3878 tcg_gen_qemu_st16(cpu_T[0], cpu_A0,
3879 (s->mem_index >> 2) - 1);
3880 break;
3881 case 0x16:
3882 if (ot == OT_LONG) { /* pextrd */
3883 tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env,
3884 offsetof(CPUX86State,
3885 xmm_regs[reg].XMM_L(val & 3)));
a7812ae4 3886 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
222a3336 3887 if (mod == 3)
a7812ae4 3888 gen_op_mov_reg_v(ot, rm, cpu_T[0]);
222a3336 3889 else
a7812ae4 3890 tcg_gen_qemu_st32(cpu_T[0], cpu_A0,
222a3336
AZ
3891 (s->mem_index >> 2) - 1);
3892 } else { /* pextrq */
a7812ae4 3893#ifdef TARGET_X86_64
222a3336
AZ
3894 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env,
3895 offsetof(CPUX86State,
3896 xmm_regs[reg].XMM_Q(val & 1)));
3897 if (mod == 3)
3898 gen_op_mov_reg_v(ot, rm, cpu_tmp1_i64);
3899 else
3900 tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_A0,
3901 (s->mem_index >> 2) - 1);
a7812ae4
PB
3902#else
3903 goto illegal_op;
3904#endif
222a3336
AZ
3905 }
3906 break;
3907 case 0x17: /* extractps */
3908 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
3909 xmm_regs[reg].XMM_L(val & 3)));
3910 if (mod == 3)
3911 gen_op_mov_reg_T0(ot, rm);
3912 else
3913 tcg_gen_qemu_st32(cpu_T[0], cpu_A0,
3914 (s->mem_index >> 2) - 1);
3915 break;
3916 case 0x20: /* pinsrb */
3917 if (mod == 3)
3918 gen_op_mov_TN_reg(OT_LONG, 0, rm);
3919 else
a7812ae4 3920 tcg_gen_qemu_ld8u(cpu_tmp0, cpu_A0,
222a3336 3921 (s->mem_index >> 2) - 1);
a7812ae4 3922 tcg_gen_st8_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State,
222a3336
AZ
3923 xmm_regs[reg].XMM_B(val & 15)));
3924 break;
3925 case 0x21: /* insertps */
a7812ae4 3926 if (mod == 3) {
222a3336
AZ
3927 tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env,
3928 offsetof(CPUX86State,xmm_regs[rm]
3929 .XMM_L((val >> 6) & 3)));
a7812ae4
PB
3930 } else {
3931 tcg_gen_qemu_ld32u(cpu_tmp0, cpu_A0,
222a3336 3932 (s->mem_index >> 2) - 1);
a7812ae4
PB
3933 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_tmp0);
3934 }
222a3336
AZ
3935 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env,
3936 offsetof(CPUX86State,xmm_regs[reg]
3937 .XMM_L((val >> 4) & 3)));
3938 if ((val >> 0) & 1)
3939 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
3940 cpu_env, offsetof(CPUX86State,
3941 xmm_regs[reg].XMM_L(0)));
3942 if ((val >> 1) & 1)
3943 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
3944 cpu_env, offsetof(CPUX86State,
3945 xmm_regs[reg].XMM_L(1)));
3946 if ((val >> 2) & 1)
3947 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
3948 cpu_env, offsetof(CPUX86State,
3949 xmm_regs[reg].XMM_L(2)));
3950 if ((val >> 3) & 1)
3951 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
3952 cpu_env, offsetof(CPUX86State,
3953 xmm_regs[reg].XMM_L(3)));
3954 break;
3955 case 0x22:
3956 if (ot == OT_LONG) { /* pinsrd */
3957 if (mod == 3)
a7812ae4 3958 gen_op_mov_v_reg(ot, cpu_tmp0, rm);
222a3336 3959 else
a7812ae4 3960 tcg_gen_qemu_ld32u(cpu_tmp0, cpu_A0,
222a3336 3961 (s->mem_index >> 2) - 1);
a7812ae4 3962 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_tmp0);
222a3336
AZ
3963 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env,
3964 offsetof(CPUX86State,
3965 xmm_regs[reg].XMM_L(val & 3)));
3966 } else { /* pinsrq */
a7812ae4 3967#ifdef TARGET_X86_64
222a3336
AZ
3968 if (mod == 3)
3969 gen_op_mov_v_reg(ot, cpu_tmp1_i64, rm);
3970 else
3971 tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_A0,
3972 (s->mem_index >> 2) - 1);
3973 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env,
3974 offsetof(CPUX86State,
3975 xmm_regs[reg].XMM_Q(val & 1)));
a7812ae4
PB
3976#else
3977 goto illegal_op;
3978#endif
222a3336
AZ
3979 }
3980 break;
3981 }
3982 return;
3983 }
4242b1bd
AZ
3984
3985 if (b1) {
3986 op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
3987 if (mod == 3) {
3988 op2_offset = offsetof(CPUX86State,xmm_regs[rm | REX_B(s)]);
3989 } else {
3990 op2_offset = offsetof(CPUX86State,xmm_t0);
0af10c86 3991 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
4242b1bd
AZ
3992 gen_ldo_env_A0(s->mem_index, op2_offset);
3993 }
3994 } else {
3995 op1_offset = offsetof(CPUX86State,fpregs[reg].mmx);
3996 if (mod == 3) {
3997 op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
3998 } else {
3999 op2_offset = offsetof(CPUX86State,mmx_t0);
0af10c86 4000 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
4242b1bd
AZ
4001 gen_ldq_env_A0(s->mem_index, op2_offset);
4002 }
4003 }
0af10c86 4004 val = cpu_ldub_code(env, s->pc++);
4242b1bd 4005
222a3336 4006 if ((b & 0xfc) == 0x60) { /* pcmpXstrX */
3ca51d07 4007 set_cc_op(s, CC_OP_EFLAGS);
222a3336
AZ
4008
4009 if (s->dflag == 2)
4010 /* The helper must use entire 64-bit gp registers */
4011 val |= 1 << 8;
4012 }
4013
4242b1bd
AZ
4014 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4015 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
d3eb5eae 4016 sse_fn_eppi(cpu_env, cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
4242b1bd 4017 break;
664e0f19
FB
4018 default:
4019 goto illegal_op;
4020 }
4021 } else {
4022 /* generic MMX or SSE operation */
d1e42c5c 4023 switch(b) {
d1e42c5c
FB
4024 case 0x70: /* pshufx insn */
4025 case 0xc6: /* pshufx insn */
4026 case 0xc2: /* compare insns */
4027 s->rip_offset = 1;
4028 break;
4029 default:
4030 break;
664e0f19
FB
4031 }
4032 if (is_xmm) {
4033 op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
4034 if (mod != 3) {
0af10c86 4035 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
664e0f19 4036 op2_offset = offsetof(CPUX86State,xmm_t0);
480c1cdb 4037 if (b1 >= 2 && ((b >= 0x50 && b <= 0x5f && b != 0x5b) ||
664e0f19
FB
4038 b == 0xc2)) {
4039 /* specific case for SSE single instructions */
4040 if (b1 == 2) {
4041 /* 32 bit access */
57fec1fe 4042 gen_op_ld_T0_A0(OT_LONG + s->mem_index);
651ba608 4043 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(0)));
664e0f19
FB
4044 } else {
4045 /* 64 bit access */
8686c490 4046 gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_t0.XMM_D(0)));
664e0f19
FB
4047 }
4048 } else {
8686c490 4049 gen_ldo_env_A0(s->mem_index, op2_offset);
664e0f19
FB
4050 }
4051 } else {
4052 rm = (modrm & 7) | REX_B(s);
4053 op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
4054 }
4055 } else {
4056 op1_offset = offsetof(CPUX86State,fpregs[reg].mmx);
4057 if (mod != 3) {
0af10c86 4058 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
664e0f19 4059 op2_offset = offsetof(CPUX86State,mmx_t0);
8686c490 4060 gen_ldq_env_A0(s->mem_index, op2_offset);
664e0f19
FB
4061 } else {
4062 rm = (modrm & 7);
4063 op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
4064 }
4065 }
4066 switch(b) {
a35f3ec7 4067 case 0x0f: /* 3DNow! data insns */
e771edab
AJ
4068 if (!(s->cpuid_ext2_features & CPUID_EXT2_3DNOW))
4069 goto illegal_op;
0af10c86 4070 val = cpu_ldub_code(env, s->pc++);
d3eb5eae
BS
4071 sse_fn_epp = sse_op_table5[val];
4072 if (!sse_fn_epp) {
a35f3ec7 4073 goto illegal_op;
c4baa050 4074 }
5af45186
FB
4075 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4076 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
d3eb5eae 4077 sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
a35f3ec7 4078 break;
664e0f19
FB
4079 case 0x70: /* pshufx insn */
4080 case 0xc6: /* pshufx insn */
0af10c86 4081 val = cpu_ldub_code(env, s->pc++);
5af45186
FB
4082 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4083 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
c4baa050 4084 /* XXX: introduce a new table? */
d3eb5eae 4085 sse_fn_ppi = (SSEFunc_0_ppi)sse_fn_epp;
c4baa050 4086 sse_fn_ppi(cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
664e0f19
FB
4087 break;
4088 case 0xc2:
4089 /* compare insns */
0af10c86 4090 val = cpu_ldub_code(env, s->pc++);
664e0f19
FB
4091 if (val >= 8)
4092 goto illegal_op;
d3eb5eae 4093 sse_fn_epp = sse_op_table4[val][b1];
c4baa050 4094
5af45186
FB
4095 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4096 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
d3eb5eae 4097 sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19 4098 break;
b8b6a50b
FB
4099 case 0xf7:
4100 /* maskmov : we must prepare A0 */
4101 if (mod != 3)
4102 goto illegal_op;
4103#ifdef TARGET_X86_64
4104 if (s->aflag == 2) {
4105 gen_op_movq_A0_reg(R_EDI);
4106 } else
4107#endif
4108 {
4109 gen_op_movl_A0_reg(R_EDI);
4110 if (s->aflag == 0)
4111 gen_op_andl_A0_ffff();
4112 }
4113 gen_add_A0_ds_seg(s);
4114
4115 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4116 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
c4baa050 4117 /* XXX: introduce a new table? */
d3eb5eae
BS
4118 sse_fn_eppt = (SSEFunc_0_eppt)sse_fn_epp;
4119 sse_fn_eppt(cpu_env, cpu_ptr0, cpu_ptr1, cpu_A0);
b8b6a50b 4120 break;
664e0f19 4121 default:
5af45186
FB
4122 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4123 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
d3eb5eae 4124 sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
4125 break;
4126 }
4127 if (b == 0x2e || b == 0x2f) {
3ca51d07 4128 set_cc_op(s, CC_OP_EFLAGS);
664e0f19
FB
4129 }
4130 }
4131}
4132
2c0262af
FB
4133/* convert one instruction. s->is_jmp is set if the translation must
4134 be stopped. Return the next pc value */
0af10c86
BS
4135static target_ulong disas_insn(CPUX86State *env, DisasContext *s,
4136 target_ulong pc_start)
2c0262af
FB
4137{
4138 int b, prefixes, aflag, dflag;
4139 int shift, ot;
4140 int modrm, reg, rm, mod, reg_addr, op, opreg, offset_addr, val;
14ce26e7
FB
4141 target_ulong next_eip, tval;
4142 int rex_w, rex_r;
2c0262af 4143
fdefe51c 4144 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
70cff25e 4145 tcg_gen_debug_insn_start(pc_start);
fdefe51c 4146 }
2c0262af
FB
4147 s->pc = pc_start;
4148 prefixes = 0;
4149 aflag = s->code32;
4150 dflag = s->code32;
4151 s->override = -1;
14ce26e7
FB
4152 rex_w = -1;
4153 rex_r = 0;
4154#ifdef TARGET_X86_64
4155 s->rex_x = 0;
4156 s->rex_b = 0;
5fafdf24 4157 x86_64_hregs = 0;
14ce26e7
FB
4158#endif
4159 s->rip_offset = 0; /* for relative ip address */
2c0262af 4160 next_byte:
0af10c86 4161 b = cpu_ldub_code(env, s->pc);
2c0262af
FB
4162 s->pc++;
4163 /* check prefixes */
14ce26e7
FB
4164#ifdef TARGET_X86_64
4165 if (CODE64(s)) {
4166 switch (b) {
4167 case 0xf3:
4168 prefixes |= PREFIX_REPZ;
4169 goto next_byte;
4170 case 0xf2:
4171 prefixes |= PREFIX_REPNZ;
4172 goto next_byte;
4173 case 0xf0:
4174 prefixes |= PREFIX_LOCK;
4175 goto next_byte;
4176 case 0x2e:
4177 s->override = R_CS;
4178 goto next_byte;
4179 case 0x36:
4180 s->override = R_SS;
4181 goto next_byte;
4182 case 0x3e:
4183 s->override = R_DS;
4184 goto next_byte;
4185 case 0x26:
4186 s->override = R_ES;
4187 goto next_byte;
4188 case 0x64:
4189 s->override = R_FS;
4190 goto next_byte;
4191 case 0x65:
4192 s->override = R_GS;
4193 goto next_byte;
4194 case 0x66:
4195 prefixes |= PREFIX_DATA;
4196 goto next_byte;
4197 case 0x67:
4198 prefixes |= PREFIX_ADR;
4199 goto next_byte;
4200 case 0x40 ... 0x4f:
4201 /* REX prefix */
4202 rex_w = (b >> 3) & 1;
4203 rex_r = (b & 0x4) << 1;
4204 s->rex_x = (b & 0x2) << 2;
4205 REX_B(s) = (b & 0x1) << 3;
4206 x86_64_hregs = 1; /* select uniform byte register addressing */
4207 goto next_byte;
4208 }
4209 if (rex_w == 1) {
4210 /* 0x66 is ignored if rex.w is set */
4211 dflag = 2;
4212 } else {
4213 if (prefixes & PREFIX_DATA)
4214 dflag ^= 1;
4215 }
4216 if (!(prefixes & PREFIX_ADR))
4217 aflag = 2;
5fafdf24 4218 } else
14ce26e7
FB
4219#endif
4220 {
4221 switch (b) {
4222 case 0xf3:
4223 prefixes |= PREFIX_REPZ;
4224 goto next_byte;
4225 case 0xf2:
4226 prefixes |= PREFIX_REPNZ;
4227 goto next_byte;
4228 case 0xf0:
4229 prefixes |= PREFIX_LOCK;
4230 goto next_byte;
4231 case 0x2e:
4232 s->override = R_CS;
4233 goto next_byte;
4234 case 0x36:
4235 s->override = R_SS;
4236 goto next_byte;
4237 case 0x3e:
4238 s->override = R_DS;
4239 goto next_byte;
4240 case 0x26:
4241 s->override = R_ES;
4242 goto next_byte;
4243 case 0x64:
4244 s->override = R_FS;
4245 goto next_byte;
4246 case 0x65:
4247 s->override = R_GS;
4248 goto next_byte;
4249 case 0x66:
4250 prefixes |= PREFIX_DATA;
4251 goto next_byte;
4252 case 0x67:
4253 prefixes |= PREFIX_ADR;
4254 goto next_byte;
4255 }
4256 if (prefixes & PREFIX_DATA)
4257 dflag ^= 1;
4258 if (prefixes & PREFIX_ADR)
4259 aflag ^= 1;
2c0262af
FB
4260 }
4261
2c0262af
FB
4262 s->prefix = prefixes;
4263 s->aflag = aflag;
4264 s->dflag = dflag;
4265
4266 /* lock generation */
4267 if (prefixes & PREFIX_LOCK)
a7812ae4 4268 gen_helper_lock();
2c0262af
FB
4269
4270 /* now check op code */
4271 reswitch:
4272 switch(b) {
4273 case 0x0f:
4274 /**************************/
4275 /* extended op code */
0af10c86 4276 b = cpu_ldub_code(env, s->pc++) | 0x100;
2c0262af 4277 goto reswitch;
3b46e624 4278
2c0262af
FB
4279 /**************************/
4280 /* arith & logic */
4281 case 0x00 ... 0x05:
4282 case 0x08 ... 0x0d:
4283 case 0x10 ... 0x15:
4284 case 0x18 ... 0x1d:
4285 case 0x20 ... 0x25:
4286 case 0x28 ... 0x2d:
4287 case 0x30 ... 0x35:
4288 case 0x38 ... 0x3d:
4289 {
4290 int op, f, val;
4291 op = (b >> 3) & 7;
4292 f = (b >> 1) & 3;
4293
4294 if ((b & 1) == 0)
4295 ot = OT_BYTE;
4296 else
14ce26e7 4297 ot = dflag + OT_WORD;
3b46e624 4298
2c0262af
FB
4299 switch(f) {
4300 case 0: /* OP Ev, Gv */
0af10c86 4301 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7 4302 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af 4303 mod = (modrm >> 6) & 3;
14ce26e7 4304 rm = (modrm & 7) | REX_B(s);
2c0262af 4305 if (mod != 3) {
0af10c86 4306 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
2c0262af
FB
4307 opreg = OR_TMP0;
4308 } else if (op == OP_XORL && rm == reg) {
4309 xor_zero:
4310 /* xor reg, reg optimisation */
4311 gen_op_movl_T0_0();
3ca51d07 4312 set_cc_op(s, CC_OP_LOGICB + ot);
57fec1fe 4313 gen_op_mov_reg_T0(ot, reg);
2c0262af
FB
4314 gen_op_update1_cc();
4315 break;
4316 } else {
4317 opreg = rm;
4318 }
57fec1fe 4319 gen_op_mov_TN_reg(ot, 1, reg);
2c0262af
FB
4320 gen_op(s, op, ot, opreg);
4321 break;
4322 case 1: /* OP Gv, Ev */
0af10c86 4323 modrm = cpu_ldub_code(env, s->pc++);
2c0262af 4324 mod = (modrm >> 6) & 3;
14ce26e7
FB
4325 reg = ((modrm >> 3) & 7) | rex_r;
4326 rm = (modrm & 7) | REX_B(s);
2c0262af 4327 if (mod != 3) {
0af10c86 4328 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
57fec1fe 4329 gen_op_ld_T1_A0(ot + s->mem_index);
2c0262af
FB
4330 } else if (op == OP_XORL && rm == reg) {
4331 goto xor_zero;
4332 } else {
57fec1fe 4333 gen_op_mov_TN_reg(ot, 1, rm);
2c0262af
FB
4334 }
4335 gen_op(s, op, ot, reg);
4336 break;
4337 case 2: /* OP A, Iv */
0af10c86 4338 val = insn_get(env, s, ot);
2c0262af
FB
4339 gen_op_movl_T1_im(val);
4340 gen_op(s, op, ot, OR_EAX);
4341 break;
4342 }
4343 }
4344 break;
4345
ec9d6075
FB
4346 case 0x82:
4347 if (CODE64(s))
4348 goto illegal_op;
2c0262af
FB
4349 case 0x80: /* GRP1 */
4350 case 0x81:
4351 case 0x83:
4352 {
4353 int val;
4354
4355 if ((b & 1) == 0)
4356 ot = OT_BYTE;
4357 else
14ce26e7 4358 ot = dflag + OT_WORD;
3b46e624 4359
0af10c86 4360 modrm = cpu_ldub_code(env, s->pc++);
2c0262af 4361 mod = (modrm >> 6) & 3;
14ce26e7 4362 rm = (modrm & 7) | REX_B(s);
2c0262af 4363 op = (modrm >> 3) & 7;
3b46e624 4364
2c0262af 4365 if (mod != 3) {
14ce26e7
FB
4366 if (b == 0x83)
4367 s->rip_offset = 1;
4368 else
4369 s->rip_offset = insn_const_size(ot);
0af10c86 4370 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
2c0262af
FB
4371 opreg = OR_TMP0;
4372 } else {
14ce26e7 4373 opreg = rm;
2c0262af
FB
4374 }
4375
4376 switch(b) {
4377 default:
4378 case 0x80:
4379 case 0x81:
d64477af 4380 case 0x82:
0af10c86 4381 val = insn_get(env, s, ot);
2c0262af
FB
4382 break;
4383 case 0x83:
0af10c86 4384 val = (int8_t)insn_get(env, s, OT_BYTE);
2c0262af
FB
4385 break;
4386 }
4387 gen_op_movl_T1_im(val);
4388 gen_op(s, op, ot, opreg);
4389 }
4390 break;
4391
4392 /**************************/
4393 /* inc, dec, and other misc arith */
4394 case 0x40 ... 0x47: /* inc Gv */
4395 ot = dflag ? OT_LONG : OT_WORD;
4396 gen_inc(s, ot, OR_EAX + (b & 7), 1);
4397 break;
4398 case 0x48 ... 0x4f: /* dec Gv */
4399 ot = dflag ? OT_LONG : OT_WORD;
4400 gen_inc(s, ot, OR_EAX + (b & 7), -1);
4401 break;
4402 case 0xf6: /* GRP3 */
4403 case 0xf7:
4404 if ((b & 1) == 0)
4405 ot = OT_BYTE;
4406 else
14ce26e7 4407 ot = dflag + OT_WORD;
2c0262af 4408
0af10c86 4409 modrm = cpu_ldub_code(env, s->pc++);
2c0262af 4410 mod = (modrm >> 6) & 3;
14ce26e7 4411 rm = (modrm & 7) | REX_B(s);
2c0262af
FB
4412 op = (modrm >> 3) & 7;
4413 if (mod != 3) {
14ce26e7
FB
4414 if (op == 0)
4415 s->rip_offset = insn_const_size(ot);
0af10c86 4416 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
57fec1fe 4417 gen_op_ld_T0_A0(ot + s->mem_index);
2c0262af 4418 } else {
57fec1fe 4419 gen_op_mov_TN_reg(ot, 0, rm);
2c0262af
FB
4420 }
4421
4422 switch(op) {
4423 case 0: /* test */
0af10c86 4424 val = insn_get(env, s, ot);
2c0262af
FB
4425 gen_op_movl_T1_im(val);
4426 gen_op_testl_T0_T1_cc();
3ca51d07 4427 set_cc_op(s, CC_OP_LOGICB + ot);
2c0262af
FB
4428 break;
4429 case 2: /* not */
b6abf97d 4430 tcg_gen_not_tl(cpu_T[0], cpu_T[0]);
2c0262af 4431 if (mod != 3) {
57fec1fe 4432 gen_op_st_T0_A0(ot + s->mem_index);
2c0262af 4433 } else {
57fec1fe 4434 gen_op_mov_reg_T0(ot, rm);
2c0262af
FB
4435 }
4436 break;
4437 case 3: /* neg */
b6abf97d 4438 tcg_gen_neg_tl(cpu_T[0], cpu_T[0]);
2c0262af 4439 if (mod != 3) {
57fec1fe 4440 gen_op_st_T0_A0(ot + s->mem_index);
2c0262af 4441 } else {
57fec1fe 4442 gen_op_mov_reg_T0(ot, rm);
2c0262af
FB
4443 }
4444 gen_op_update_neg_cc();
3ca51d07 4445 set_cc_op(s, CC_OP_SUBB + ot);
2c0262af
FB
4446 break;
4447 case 4: /* mul */
4448 switch(ot) {
4449 case OT_BYTE:
0211e5af
FB
4450 gen_op_mov_TN_reg(OT_BYTE, 1, R_EAX);
4451 tcg_gen_ext8u_tl(cpu_T[0], cpu_T[0]);
4452 tcg_gen_ext8u_tl(cpu_T[1], cpu_T[1]);
4453 /* XXX: use 32 bit mul which could be faster */
4454 tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
4455 gen_op_mov_reg_T0(OT_WORD, R_EAX);
4456 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
4457 tcg_gen_andi_tl(cpu_cc_src, cpu_T[0], 0xff00);
3ca51d07 4458 set_cc_op(s, CC_OP_MULB);
2c0262af
FB
4459 break;
4460 case OT_WORD:
0211e5af
FB
4461 gen_op_mov_TN_reg(OT_WORD, 1, R_EAX);
4462 tcg_gen_ext16u_tl(cpu_T[0], cpu_T[0]);
4463 tcg_gen_ext16u_tl(cpu_T[1], cpu_T[1]);
4464 /* XXX: use 32 bit mul which could be faster */
4465 tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
4466 gen_op_mov_reg_T0(OT_WORD, R_EAX);
4467 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
4468 tcg_gen_shri_tl(cpu_T[0], cpu_T[0], 16);
4469 gen_op_mov_reg_T0(OT_WORD, R_EDX);
4470 tcg_gen_mov_tl(cpu_cc_src, cpu_T[0]);
3ca51d07 4471 set_cc_op(s, CC_OP_MULW);
2c0262af
FB
4472 break;
4473 default:
4474 case OT_LONG:
0211e5af
FB
4475#ifdef TARGET_X86_64
4476 gen_op_mov_TN_reg(OT_LONG, 1, R_EAX);
4477 tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
4478 tcg_gen_ext32u_tl(cpu_T[1], cpu_T[1]);
4479 tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
4480 gen_op_mov_reg_T0(OT_LONG, R_EAX);
4481 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
4482 tcg_gen_shri_tl(cpu_T[0], cpu_T[0], 32);
4483 gen_op_mov_reg_T0(OT_LONG, R_EDX);
4484 tcg_gen_mov_tl(cpu_cc_src, cpu_T[0]);
4485#else
4486 {
a7812ae4
PB
4487 TCGv_i64 t0, t1;
4488 t0 = tcg_temp_new_i64();
4489 t1 = tcg_temp_new_i64();
0211e5af
FB
4490 gen_op_mov_TN_reg(OT_LONG, 1, R_EAX);
4491 tcg_gen_extu_i32_i64(t0, cpu_T[0]);
4492 tcg_gen_extu_i32_i64(t1, cpu_T[1]);
4493 tcg_gen_mul_i64(t0, t0, t1);
4494 tcg_gen_trunc_i64_i32(cpu_T[0], t0);
4495 gen_op_mov_reg_T0(OT_LONG, R_EAX);
4496 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
4497 tcg_gen_shri_i64(t0, t0, 32);
4498 tcg_gen_trunc_i64_i32(cpu_T[0], t0);
4499 gen_op_mov_reg_T0(OT_LONG, R_EDX);
4500 tcg_gen_mov_tl(cpu_cc_src, cpu_T[0]);
4501 }
4502#endif
3ca51d07 4503 set_cc_op(s, CC_OP_MULL);
2c0262af 4504 break;
14ce26e7
FB
4505#ifdef TARGET_X86_64
4506 case OT_QUAD:
7923057b 4507 gen_helper_mulq_EAX_T0(cpu_env, cpu_T[0]);
3ca51d07 4508 set_cc_op(s, CC_OP_MULQ);
14ce26e7
FB
4509 break;
4510#endif
2c0262af 4511 }
2c0262af
FB
4512 break;
4513 case 5: /* imul */
4514 switch(ot) {
4515 case OT_BYTE:
0211e5af
FB
4516 gen_op_mov_TN_reg(OT_BYTE, 1, R_EAX);
4517 tcg_gen_ext8s_tl(cpu_T[0], cpu_T[0]);
4518 tcg_gen_ext8s_tl(cpu_T[1], cpu_T[1]);
4519 /* XXX: use 32 bit mul which could be faster */
4520 tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
4521 gen_op_mov_reg_T0(OT_WORD, R_EAX);
4522 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
4523 tcg_gen_ext8s_tl(cpu_tmp0, cpu_T[0]);
4524 tcg_gen_sub_tl(cpu_cc_src, cpu_T[0], cpu_tmp0);
3ca51d07 4525 set_cc_op(s, CC_OP_MULB);
2c0262af
FB
4526 break;
4527 case OT_WORD:
0211e5af
FB
4528 gen_op_mov_TN_reg(OT_WORD, 1, R_EAX);
4529 tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
4530 tcg_gen_ext16s_tl(cpu_T[1], cpu_T[1]);
4531 /* XXX: use 32 bit mul which could be faster */
4532 tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
4533 gen_op_mov_reg_T0(OT_WORD, R_EAX);
4534 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
4535 tcg_gen_ext16s_tl(cpu_tmp0, cpu_T[0]);
4536 tcg_gen_sub_tl(cpu_cc_src, cpu_T[0], cpu_tmp0);
4537 tcg_gen_shri_tl(cpu_T[0], cpu_T[0], 16);
4538 gen_op_mov_reg_T0(OT_WORD, R_EDX);
3ca51d07 4539 set_cc_op(s, CC_OP_MULW);
2c0262af
FB
4540 break;
4541 default:
4542 case OT_LONG:
0211e5af
FB
4543#ifdef TARGET_X86_64
4544 gen_op_mov_TN_reg(OT_LONG, 1, R_EAX);
4545 tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
4546 tcg_gen_ext32s_tl(cpu_T[1], cpu_T[1]);
4547 tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
4548 gen_op_mov_reg_T0(OT_LONG, R_EAX);
4549 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
4550 tcg_gen_ext32s_tl(cpu_tmp0, cpu_T[0]);
4551 tcg_gen_sub_tl(cpu_cc_src, cpu_T[0], cpu_tmp0);
4552 tcg_gen_shri_tl(cpu_T[0], cpu_T[0], 32);
4553 gen_op_mov_reg_T0(OT_LONG, R_EDX);
4554#else
4555 {
a7812ae4
PB
4556 TCGv_i64 t0, t1;
4557 t0 = tcg_temp_new_i64();
4558 t1 = tcg_temp_new_i64();
0211e5af
FB
4559 gen_op_mov_TN_reg(OT_LONG, 1, R_EAX);
4560 tcg_gen_ext_i32_i64(t0, cpu_T[0]);
4561 tcg_gen_ext_i32_i64(t1, cpu_T[1]);
4562 tcg_gen_mul_i64(t0, t0, t1);
4563 tcg_gen_trunc_i64_i32(cpu_T[0], t0);
4564 gen_op_mov_reg_T0(OT_LONG, R_EAX);
4565 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
4566 tcg_gen_sari_tl(cpu_tmp0, cpu_T[0], 31);
4567 tcg_gen_shri_i64(t0, t0, 32);
4568 tcg_gen_trunc_i64_i32(cpu_T[0], t0);
4569 gen_op_mov_reg_T0(OT_LONG, R_EDX);
4570 tcg_gen_sub_tl(cpu_cc_src, cpu_T[0], cpu_tmp0);
4571 }
4572#endif
3ca51d07 4573 set_cc_op(s, CC_OP_MULL);
2c0262af 4574 break;
14ce26e7
FB
4575#ifdef TARGET_X86_64
4576 case OT_QUAD:
7923057b 4577 gen_helper_imulq_EAX_T0(cpu_env, cpu_T[0]);
3ca51d07 4578 set_cc_op(s, CC_OP_MULQ);
14ce26e7
FB
4579 break;
4580#endif
2c0262af 4581 }
2c0262af
FB
4582 break;
4583 case 6: /* div */
4584 switch(ot) {
4585 case OT_BYTE:
14ce26e7 4586 gen_jmp_im(pc_start - s->cs_base);
7923057b 4587 gen_helper_divb_AL(cpu_env, cpu_T[0]);
2c0262af
FB
4588 break;
4589 case OT_WORD:
14ce26e7 4590 gen_jmp_im(pc_start - s->cs_base);
7923057b 4591 gen_helper_divw_AX(cpu_env, cpu_T[0]);
2c0262af
FB
4592 break;
4593 default:
4594 case OT_LONG:
14ce26e7 4595 gen_jmp_im(pc_start - s->cs_base);
7923057b 4596 gen_helper_divl_EAX(cpu_env, cpu_T[0]);
14ce26e7
FB
4597 break;
4598#ifdef TARGET_X86_64
4599 case OT_QUAD:
4600 gen_jmp_im(pc_start - s->cs_base);
7923057b 4601 gen_helper_divq_EAX(cpu_env, cpu_T[0]);
2c0262af 4602 break;
14ce26e7 4603#endif
2c0262af
FB
4604 }
4605 break;
4606 case 7: /* idiv */
4607 switch(ot) {
4608 case OT_BYTE:
14ce26e7 4609 gen_jmp_im(pc_start - s->cs_base);
7923057b 4610 gen_helper_idivb_AL(cpu_env, cpu_T[0]);
2c0262af
FB
4611 break;
4612 case OT_WORD:
14ce26e7 4613 gen_jmp_im(pc_start - s->cs_base);
7923057b 4614 gen_helper_idivw_AX(cpu_env, cpu_T[0]);
2c0262af
FB
4615 break;
4616 default:
4617 case OT_LONG:
14ce26e7 4618 gen_jmp_im(pc_start - s->cs_base);
7923057b 4619 gen_helper_idivl_EAX(cpu_env, cpu_T[0]);
14ce26e7
FB
4620 break;
4621#ifdef TARGET_X86_64
4622 case OT_QUAD:
4623 gen_jmp_im(pc_start - s->cs_base);
7923057b 4624 gen_helper_idivq_EAX(cpu_env, cpu_T[0]);
2c0262af 4625 break;
14ce26e7 4626#endif
2c0262af
FB
4627 }
4628 break;
4629 default:
4630 goto illegal_op;
4631 }
4632 break;
4633
4634 case 0xfe: /* GRP4 */
4635 case 0xff: /* GRP5 */
4636 if ((b & 1) == 0)
4637 ot = OT_BYTE;
4638 else
14ce26e7 4639 ot = dflag + OT_WORD;
2c0262af 4640
0af10c86 4641 modrm = cpu_ldub_code(env, s->pc++);
2c0262af 4642 mod = (modrm >> 6) & 3;
14ce26e7 4643 rm = (modrm & 7) | REX_B(s);
2c0262af
FB
4644 op = (modrm >> 3) & 7;
4645 if (op >= 2 && b == 0xfe) {
4646 goto illegal_op;
4647 }
14ce26e7 4648 if (CODE64(s)) {
aba9d61e 4649 if (op == 2 || op == 4) {
14ce26e7
FB
4650 /* operand size for jumps is 64 bit */
4651 ot = OT_QUAD;
aba9d61e 4652 } else if (op == 3 || op == 5) {
41b1e61f 4653 ot = dflag ? OT_LONG + (rex_w == 1) : OT_WORD;
14ce26e7
FB
4654 } else if (op == 6) {
4655 /* default push size is 64 bit */
4656 ot = dflag ? OT_QUAD : OT_WORD;
4657 }
4658 }
2c0262af 4659 if (mod != 3) {
0af10c86 4660 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
2c0262af 4661 if (op >= 2 && op != 3 && op != 5)
57fec1fe 4662 gen_op_ld_T0_A0(ot + s->mem_index);
2c0262af 4663 } else {
57fec1fe 4664 gen_op_mov_TN_reg(ot, 0, rm);
2c0262af
FB
4665 }
4666
4667 switch(op) {
4668 case 0: /* inc Ev */
4669 if (mod != 3)
4670 opreg = OR_TMP0;
4671 else
4672 opreg = rm;
4673 gen_inc(s, ot, opreg, 1);
4674 break;
4675 case 1: /* dec Ev */
4676 if (mod != 3)
4677 opreg = OR_TMP0;
4678 else
4679 opreg = rm;
4680 gen_inc(s, ot, opreg, -1);
4681 break;
4682 case 2: /* call Ev */
4f31916f 4683 /* XXX: optimize if memory (no 'and' is necessary) */
2c0262af
FB
4684 if (s->dflag == 0)
4685 gen_op_andl_T0_ffff();
2c0262af 4686 next_eip = s->pc - s->cs_base;
1ef38687 4687 gen_movtl_T1_im(next_eip);
4f31916f
FB
4688 gen_push_T1(s);
4689 gen_op_jmp_T0();
2c0262af
FB
4690 gen_eob(s);
4691 break;
61382a50 4692 case 3: /* lcall Ev */
57fec1fe 4693 gen_op_ld_T1_A0(ot + s->mem_index);
aba9d61e 4694 gen_add_A0_im(s, 1 << (ot - OT_WORD + 1));
57fec1fe 4695 gen_op_ldu_T0_A0(OT_WORD + s->mem_index);
2c0262af
FB
4696 do_lcall:
4697 if (s->pe && !s->vm86) {
4698 if (s->cc_op != CC_OP_DYNAMIC)
4699 gen_op_set_cc_op(s->cc_op);
14ce26e7 4700 gen_jmp_im(pc_start - s->cs_base);
b6abf97d 4701 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
2999a0b2
BS
4702 gen_helper_lcall_protected(cpu_env, cpu_tmp2_i32, cpu_T[1],
4703 tcg_const_i32(dflag),
a7812ae4 4704 tcg_const_i32(s->pc - pc_start));
2c0262af 4705 } else {
b6abf97d 4706 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
2999a0b2
BS
4707 gen_helper_lcall_real(cpu_env, cpu_tmp2_i32, cpu_T[1],
4708 tcg_const_i32(dflag),
a7812ae4 4709 tcg_const_i32(s->pc - s->cs_base));
2c0262af
FB
4710 }
4711 gen_eob(s);
4712 break;
4713 case 4: /* jmp Ev */
4714 if (s->dflag == 0)
4715 gen_op_andl_T0_ffff();
4716 gen_op_jmp_T0();
4717 gen_eob(s);
4718 break;
4719 case 5: /* ljmp Ev */
57fec1fe 4720 gen_op_ld_T1_A0(ot + s->mem_index);
aba9d61e 4721 gen_add_A0_im(s, 1 << (ot - OT_WORD + 1));
57fec1fe 4722 gen_op_ldu_T0_A0(OT_WORD + s->mem_index);
2c0262af
FB
4723 do_ljmp:
4724 if (s->pe && !s->vm86) {
4725 if (s->cc_op != CC_OP_DYNAMIC)
4726 gen_op_set_cc_op(s->cc_op);
14ce26e7 4727 gen_jmp_im(pc_start - s->cs_base);
b6abf97d 4728 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
2999a0b2 4729 gen_helper_ljmp_protected(cpu_env, cpu_tmp2_i32, cpu_T[1],
a7812ae4 4730 tcg_const_i32(s->pc - pc_start));
2c0262af 4731 } else {
3bd7da9e 4732 gen_op_movl_seg_T0_vm(R_CS);
2c0262af
FB
4733 gen_op_movl_T0_T1();
4734 gen_op_jmp_T0();
4735 }
4736 gen_eob(s);
4737 break;
4738 case 6: /* push Ev */
4739 gen_push_T0(s);
4740 break;
4741 default:
4742 goto illegal_op;
4743 }
4744 break;
4745
4746 case 0x84: /* test Ev, Gv */
5fafdf24 4747 case 0x85:
2c0262af
FB
4748 if ((b & 1) == 0)
4749 ot = OT_BYTE;
4750 else
14ce26e7 4751 ot = dflag + OT_WORD;
2c0262af 4752
0af10c86 4753 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7 4754 reg = ((modrm >> 3) & 7) | rex_r;
3b46e624 4755
0af10c86 4756 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
57fec1fe 4757 gen_op_mov_TN_reg(ot, 1, reg);
2c0262af 4758 gen_op_testl_T0_T1_cc();
3ca51d07 4759 set_cc_op(s, CC_OP_LOGICB + ot);
2c0262af 4760 break;
3b46e624 4761
2c0262af
FB
4762 case 0xa8: /* test eAX, Iv */
4763 case 0xa9:
4764 if ((b & 1) == 0)
4765 ot = OT_BYTE;
4766 else
14ce26e7 4767 ot = dflag + OT_WORD;
0af10c86 4768 val = insn_get(env, s, ot);
2c0262af 4769
57fec1fe 4770 gen_op_mov_TN_reg(ot, 0, OR_EAX);
2c0262af
FB
4771 gen_op_movl_T1_im(val);
4772 gen_op_testl_T0_T1_cc();
3ca51d07 4773 set_cc_op(s, CC_OP_LOGICB + ot);
2c0262af 4774 break;
3b46e624 4775
2c0262af 4776 case 0x98: /* CWDE/CBW */
14ce26e7
FB
4777#ifdef TARGET_X86_64
4778 if (dflag == 2) {
e108dd01
FB
4779 gen_op_mov_TN_reg(OT_LONG, 0, R_EAX);
4780 tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
4781 gen_op_mov_reg_T0(OT_QUAD, R_EAX);
14ce26e7
FB
4782 } else
4783#endif
e108dd01
FB
4784 if (dflag == 1) {
4785 gen_op_mov_TN_reg(OT_WORD, 0, R_EAX);
4786 tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
4787 gen_op_mov_reg_T0(OT_LONG, R_EAX);
4788 } else {
4789 gen_op_mov_TN_reg(OT_BYTE, 0, R_EAX);
4790 tcg_gen_ext8s_tl(cpu_T[0], cpu_T[0]);
4791 gen_op_mov_reg_T0(OT_WORD, R_EAX);
4792 }
2c0262af
FB
4793 break;
4794 case 0x99: /* CDQ/CWD */
14ce26e7
FB
4795#ifdef TARGET_X86_64
4796 if (dflag == 2) {
e108dd01
FB
4797 gen_op_mov_TN_reg(OT_QUAD, 0, R_EAX);
4798 tcg_gen_sari_tl(cpu_T[0], cpu_T[0], 63);
4799 gen_op_mov_reg_T0(OT_QUAD, R_EDX);
14ce26e7
FB
4800 } else
4801#endif
e108dd01
FB
4802 if (dflag == 1) {
4803 gen_op_mov_TN_reg(OT_LONG, 0, R_EAX);
4804 tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
4805 tcg_gen_sari_tl(cpu_T[0], cpu_T[0], 31);
4806 gen_op_mov_reg_T0(OT_LONG, R_EDX);
4807 } else {
4808 gen_op_mov_TN_reg(OT_WORD, 0, R_EAX);
4809 tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
4810 tcg_gen_sari_tl(cpu_T[0], cpu_T[0], 15);
4811 gen_op_mov_reg_T0(OT_WORD, R_EDX);
4812 }
2c0262af
FB
4813 break;
4814 case 0x1af: /* imul Gv, Ev */
4815 case 0x69: /* imul Gv, Ev, I */
4816 case 0x6b:
14ce26e7 4817 ot = dflag + OT_WORD;
0af10c86 4818 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7
FB
4819 reg = ((modrm >> 3) & 7) | rex_r;
4820 if (b == 0x69)
4821 s->rip_offset = insn_const_size(ot);
4822 else if (b == 0x6b)
4823 s->rip_offset = 1;
0af10c86 4824 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
2c0262af 4825 if (b == 0x69) {
0af10c86 4826 val = insn_get(env, s, ot);
2c0262af
FB
4827 gen_op_movl_T1_im(val);
4828 } else if (b == 0x6b) {
0af10c86 4829 val = (int8_t)insn_get(env, s, OT_BYTE);
2c0262af
FB
4830 gen_op_movl_T1_im(val);
4831 } else {
57fec1fe 4832 gen_op_mov_TN_reg(ot, 1, reg);
2c0262af
FB
4833 }
4834
14ce26e7
FB
4835#ifdef TARGET_X86_64
4836 if (ot == OT_QUAD) {
7923057b 4837 gen_helper_imulq_T0_T1(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
14ce26e7
FB
4838 } else
4839#endif
2c0262af 4840 if (ot == OT_LONG) {
0211e5af
FB
4841#ifdef TARGET_X86_64
4842 tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
4843 tcg_gen_ext32s_tl(cpu_T[1], cpu_T[1]);
4844 tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
4845 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
4846 tcg_gen_ext32s_tl(cpu_tmp0, cpu_T[0]);
4847 tcg_gen_sub_tl(cpu_cc_src, cpu_T[0], cpu_tmp0);
4848#else
4849 {
a7812ae4
PB
4850 TCGv_i64 t0, t1;
4851 t0 = tcg_temp_new_i64();
4852 t1 = tcg_temp_new_i64();
0211e5af
FB
4853 tcg_gen_ext_i32_i64(t0, cpu_T[0]);
4854 tcg_gen_ext_i32_i64(t1, cpu_T[1]);
4855 tcg_gen_mul_i64(t0, t0, t1);
4856 tcg_gen_trunc_i64_i32(cpu_T[0], t0);
4857 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
4858 tcg_gen_sari_tl(cpu_tmp0, cpu_T[0], 31);
4859 tcg_gen_shri_i64(t0, t0, 32);
4860 tcg_gen_trunc_i64_i32(cpu_T[1], t0);
4861 tcg_gen_sub_tl(cpu_cc_src, cpu_T[1], cpu_tmp0);
4862 }
4863#endif
2c0262af 4864 } else {
0211e5af
FB
4865 tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
4866 tcg_gen_ext16s_tl(cpu_T[1], cpu_T[1]);
4867 /* XXX: use 32 bit mul which could be faster */
4868 tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
4869 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
4870 tcg_gen_ext16s_tl(cpu_tmp0, cpu_T[0]);
4871 tcg_gen_sub_tl(cpu_cc_src, cpu_T[0], cpu_tmp0);
2c0262af 4872 }
57fec1fe 4873 gen_op_mov_reg_T0(ot, reg);
3ca51d07 4874 set_cc_op(s, CC_OP_MULB + ot);
2c0262af
FB
4875 break;
4876 case 0x1c0:
4877 case 0x1c1: /* xadd Ev, Gv */
4878 if ((b & 1) == 0)
4879 ot = OT_BYTE;
4880 else
14ce26e7 4881 ot = dflag + OT_WORD;
0af10c86 4882 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7 4883 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af
FB
4884 mod = (modrm >> 6) & 3;
4885 if (mod == 3) {
14ce26e7 4886 rm = (modrm & 7) | REX_B(s);
57fec1fe
FB
4887 gen_op_mov_TN_reg(ot, 0, reg);
4888 gen_op_mov_TN_reg(ot, 1, rm);
2c0262af 4889 gen_op_addl_T0_T1();
57fec1fe
FB
4890 gen_op_mov_reg_T1(ot, reg);
4891 gen_op_mov_reg_T0(ot, rm);
2c0262af 4892 } else {
0af10c86 4893 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
57fec1fe
FB
4894 gen_op_mov_TN_reg(ot, 0, reg);
4895 gen_op_ld_T1_A0(ot + s->mem_index);
2c0262af 4896 gen_op_addl_T0_T1();
57fec1fe
FB
4897 gen_op_st_T0_A0(ot + s->mem_index);
4898 gen_op_mov_reg_T1(ot, reg);
2c0262af
FB
4899 }
4900 gen_op_update2_cc();
3ca51d07 4901 set_cc_op(s, CC_OP_ADDB + ot);
2c0262af
FB
4902 break;
4903 case 0x1b0:
4904 case 0x1b1: /* cmpxchg Ev, Gv */
cad3a37d 4905 {
1130328e 4906 int label1, label2;
1e4840bf 4907 TCGv t0, t1, t2, a0;
cad3a37d
FB
4908
4909 if ((b & 1) == 0)
4910 ot = OT_BYTE;
4911 else
4912 ot = dflag + OT_WORD;
0af10c86 4913 modrm = cpu_ldub_code(env, s->pc++);
cad3a37d
FB
4914 reg = ((modrm >> 3) & 7) | rex_r;
4915 mod = (modrm >> 6) & 3;
a7812ae4
PB
4916 t0 = tcg_temp_local_new();
4917 t1 = tcg_temp_local_new();
4918 t2 = tcg_temp_local_new();
4919 a0 = tcg_temp_local_new();
1e4840bf 4920 gen_op_mov_v_reg(ot, t1, reg);
cad3a37d
FB
4921 if (mod == 3) {
4922 rm = (modrm & 7) | REX_B(s);
1e4840bf 4923 gen_op_mov_v_reg(ot, t0, rm);
cad3a37d 4924 } else {
0af10c86 4925 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
1e4840bf
FB
4926 tcg_gen_mov_tl(a0, cpu_A0);
4927 gen_op_ld_v(ot + s->mem_index, t0, a0);
cad3a37d
FB
4928 rm = 0; /* avoid warning */
4929 }
4930 label1 = gen_new_label();
cc739bb0 4931 tcg_gen_sub_tl(t2, cpu_regs[R_EAX], t0);
1e4840bf
FB
4932 gen_extu(ot, t2);
4933 tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, label1);
f7e80adf 4934 label2 = gen_new_label();
cad3a37d 4935 if (mod == 3) {
1e4840bf 4936 gen_op_mov_reg_v(ot, R_EAX, t0);
1130328e
FB
4937 tcg_gen_br(label2);
4938 gen_set_label(label1);
1e4840bf 4939 gen_op_mov_reg_v(ot, rm, t1);
cad3a37d 4940 } else {
f7e80adf
AG
4941 /* perform no-op store cycle like physical cpu; must be
4942 before changing accumulator to ensure idempotency if
4943 the store faults and the instruction is restarted */
4944 gen_op_st_v(ot + s->mem_index, t0, a0);
1e4840bf 4945 gen_op_mov_reg_v(ot, R_EAX, t0);
f7e80adf 4946 tcg_gen_br(label2);
1130328e 4947 gen_set_label(label1);
1e4840bf 4948 gen_op_st_v(ot + s->mem_index, t1, a0);
cad3a37d 4949 }
f7e80adf 4950 gen_set_label(label2);
1e4840bf
FB
4951 tcg_gen_mov_tl(cpu_cc_src, t0);
4952 tcg_gen_mov_tl(cpu_cc_dst, t2);
3ca51d07 4953 set_cc_op(s, CC_OP_SUBB + ot);
1e4840bf
FB
4954 tcg_temp_free(t0);
4955 tcg_temp_free(t1);
4956 tcg_temp_free(t2);
4957 tcg_temp_free(a0);
2c0262af 4958 }
2c0262af
FB
4959 break;
4960 case 0x1c7: /* cmpxchg8b */
0af10c86 4961 modrm = cpu_ldub_code(env, s->pc++);
2c0262af 4962 mod = (modrm >> 6) & 3;
71c3558e 4963 if ((mod == 3) || ((modrm & 0x38) != 0x8))
2c0262af 4964 goto illegal_op;
1b9d9ebb
FB
4965#ifdef TARGET_X86_64
4966 if (dflag == 2) {
4967 if (!(s->cpuid_ext_features & CPUID_EXT_CX16))
4968 goto illegal_op;
4969 gen_jmp_im(pc_start - s->cs_base);
4970 if (s->cc_op != CC_OP_DYNAMIC)
4971 gen_op_set_cc_op(s->cc_op);
0af10c86 4972 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
92fc4b58 4973 gen_helper_cmpxchg16b(cpu_env, cpu_A0);
1b9d9ebb
FB
4974 } else
4975#endif
4976 {
4977 if (!(s->cpuid_features & CPUID_CX8))
4978 goto illegal_op;
4979 gen_jmp_im(pc_start - s->cs_base);
4980 if (s->cc_op != CC_OP_DYNAMIC)
4981 gen_op_set_cc_op(s->cc_op);
0af10c86 4982 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
92fc4b58 4983 gen_helper_cmpxchg8b(cpu_env, cpu_A0);
1b9d9ebb 4984 }
3ca51d07 4985 set_cc_op(s, CC_OP_EFLAGS);
2c0262af 4986 break;
3b46e624 4987
2c0262af
FB
4988 /**************************/
4989 /* push/pop */
4990 case 0x50 ... 0x57: /* push */
57fec1fe 4991 gen_op_mov_TN_reg(OT_LONG, 0, (b & 7) | REX_B(s));
2c0262af
FB
4992 gen_push_T0(s);
4993 break;
4994 case 0x58 ... 0x5f: /* pop */
14ce26e7
FB
4995 if (CODE64(s)) {
4996 ot = dflag ? OT_QUAD : OT_WORD;
4997 } else {
4998 ot = dflag + OT_WORD;
4999 }
2c0262af 5000 gen_pop_T0(s);
77729c24 5001 /* NOTE: order is important for pop %sp */
2c0262af 5002 gen_pop_update(s);
57fec1fe 5003 gen_op_mov_reg_T0(ot, (b & 7) | REX_B(s));
2c0262af
FB
5004 break;
5005 case 0x60: /* pusha */
14ce26e7
FB
5006 if (CODE64(s))
5007 goto illegal_op;
2c0262af
FB
5008 gen_pusha(s);
5009 break;
5010 case 0x61: /* popa */
14ce26e7
FB
5011 if (CODE64(s))
5012 goto illegal_op;
2c0262af
FB
5013 gen_popa(s);
5014 break;
5015 case 0x68: /* push Iv */
5016 case 0x6a:
14ce26e7
FB
5017 if (CODE64(s)) {
5018 ot = dflag ? OT_QUAD : OT_WORD;
5019 } else {
5020 ot = dflag + OT_WORD;
5021 }
2c0262af 5022 if (b == 0x68)
0af10c86 5023 val = insn_get(env, s, ot);
2c0262af 5024 else
0af10c86 5025 val = (int8_t)insn_get(env, s, OT_BYTE);
2c0262af
FB
5026 gen_op_movl_T0_im(val);
5027 gen_push_T0(s);
5028 break;
5029 case 0x8f: /* pop Ev */
14ce26e7
FB
5030 if (CODE64(s)) {
5031 ot = dflag ? OT_QUAD : OT_WORD;
5032 } else {
5033 ot = dflag + OT_WORD;
5034 }
0af10c86 5035 modrm = cpu_ldub_code(env, s->pc++);
77729c24 5036 mod = (modrm >> 6) & 3;
2c0262af 5037 gen_pop_T0(s);
77729c24
FB
5038 if (mod == 3) {
5039 /* NOTE: order is important for pop %sp */
5040 gen_pop_update(s);
14ce26e7 5041 rm = (modrm & 7) | REX_B(s);
57fec1fe 5042 gen_op_mov_reg_T0(ot, rm);
77729c24
FB
5043 } else {
5044 /* NOTE: order is important too for MMU exceptions */
14ce26e7 5045 s->popl_esp_hack = 1 << ot;
0af10c86 5046 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
77729c24
FB
5047 s->popl_esp_hack = 0;
5048 gen_pop_update(s);
5049 }
2c0262af
FB
5050 break;
5051 case 0xc8: /* enter */
5052 {
5053 int level;
0af10c86 5054 val = cpu_lduw_code(env, s->pc);
2c0262af 5055 s->pc += 2;
0af10c86 5056 level = cpu_ldub_code(env, s->pc++);
2c0262af
FB
5057 gen_enter(s, val, level);
5058 }
5059 break;
5060 case 0xc9: /* leave */
5061 /* XXX: exception not precise (ESP is updated before potential exception) */
14ce26e7 5062 if (CODE64(s)) {
57fec1fe
FB
5063 gen_op_mov_TN_reg(OT_QUAD, 0, R_EBP);
5064 gen_op_mov_reg_T0(OT_QUAD, R_ESP);
14ce26e7 5065 } else if (s->ss32) {
57fec1fe
FB
5066 gen_op_mov_TN_reg(OT_LONG, 0, R_EBP);
5067 gen_op_mov_reg_T0(OT_LONG, R_ESP);
2c0262af 5068 } else {
57fec1fe
FB
5069 gen_op_mov_TN_reg(OT_WORD, 0, R_EBP);
5070 gen_op_mov_reg_T0(OT_WORD, R_ESP);
2c0262af
FB
5071 }
5072 gen_pop_T0(s);
14ce26e7
FB
5073 if (CODE64(s)) {
5074 ot = dflag ? OT_QUAD : OT_WORD;
5075 } else {
5076 ot = dflag + OT_WORD;
5077 }
57fec1fe 5078 gen_op_mov_reg_T0(ot, R_EBP);
2c0262af
FB
5079 gen_pop_update(s);
5080 break;
5081 case 0x06: /* push es */
5082 case 0x0e: /* push cs */
5083 case 0x16: /* push ss */
5084 case 0x1e: /* push ds */
14ce26e7
FB
5085 if (CODE64(s))
5086 goto illegal_op;
2c0262af
FB
5087 gen_op_movl_T0_seg(b >> 3);
5088 gen_push_T0(s);
5089 break;
5090 case 0x1a0: /* push fs */
5091 case 0x1a8: /* push gs */
5092 gen_op_movl_T0_seg((b >> 3) & 7);
5093 gen_push_T0(s);
5094 break;
5095 case 0x07: /* pop es */
5096 case 0x17: /* pop ss */
5097 case 0x1f: /* pop ds */
14ce26e7
FB
5098 if (CODE64(s))
5099 goto illegal_op;
2c0262af
FB
5100 reg = b >> 3;
5101 gen_pop_T0(s);
5102 gen_movl_seg_T0(s, reg, pc_start - s->cs_base);
5103 gen_pop_update(s);
5104 if (reg == R_SS) {
a2cc3b24
FB
5105 /* if reg == SS, inhibit interrupts/trace. */
5106 /* If several instructions disable interrupts, only the
5107 _first_ does it */
5108 if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
f0967a1a 5109 gen_helper_set_inhibit_irq(cpu_env);
2c0262af
FB
5110 s->tf = 0;
5111 }
5112 if (s->is_jmp) {
14ce26e7 5113 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
5114 gen_eob(s);
5115 }
5116 break;
5117 case 0x1a1: /* pop fs */
5118 case 0x1a9: /* pop gs */
5119 gen_pop_T0(s);
5120 gen_movl_seg_T0(s, (b >> 3) & 7, pc_start - s->cs_base);
5121 gen_pop_update(s);
5122 if (s->is_jmp) {
14ce26e7 5123 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
5124 gen_eob(s);
5125 }
5126 break;
5127
5128 /**************************/
5129 /* mov */
5130 case 0x88:
5131 case 0x89: /* mov Gv, Ev */
5132 if ((b & 1) == 0)
5133 ot = OT_BYTE;
5134 else
14ce26e7 5135 ot = dflag + OT_WORD;
0af10c86 5136 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7 5137 reg = ((modrm >> 3) & 7) | rex_r;
3b46e624 5138
2c0262af 5139 /* generate a generic store */
0af10c86 5140 gen_ldst_modrm(env, s, modrm, ot, reg, 1);
2c0262af
FB
5141 break;
5142 case 0xc6:
5143 case 0xc7: /* mov Ev, Iv */
5144 if ((b & 1) == 0)
5145 ot = OT_BYTE;
5146 else
14ce26e7 5147 ot = dflag + OT_WORD;
0af10c86 5148 modrm = cpu_ldub_code(env, s->pc++);
2c0262af 5149 mod = (modrm >> 6) & 3;
14ce26e7
FB
5150 if (mod != 3) {
5151 s->rip_offset = insn_const_size(ot);
0af10c86 5152 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
14ce26e7 5153 }
0af10c86 5154 val = insn_get(env, s, ot);
2c0262af
FB
5155 gen_op_movl_T0_im(val);
5156 if (mod != 3)
57fec1fe 5157 gen_op_st_T0_A0(ot + s->mem_index);
2c0262af 5158 else
57fec1fe 5159 gen_op_mov_reg_T0(ot, (modrm & 7) | REX_B(s));
2c0262af
FB
5160 break;
5161 case 0x8a:
5162 case 0x8b: /* mov Ev, Gv */
5163 if ((b & 1) == 0)
5164 ot = OT_BYTE;
5165 else
14ce26e7 5166 ot = OT_WORD + dflag;
0af10c86 5167 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7 5168 reg = ((modrm >> 3) & 7) | rex_r;
3b46e624 5169
0af10c86 5170 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
57fec1fe 5171 gen_op_mov_reg_T0(ot, reg);
2c0262af
FB
5172 break;
5173 case 0x8e: /* mov seg, Gv */
0af10c86 5174 modrm = cpu_ldub_code(env, s->pc++);
2c0262af
FB
5175 reg = (modrm >> 3) & 7;
5176 if (reg >= 6 || reg == R_CS)
5177 goto illegal_op;
0af10c86 5178 gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
2c0262af
FB
5179 gen_movl_seg_T0(s, reg, pc_start - s->cs_base);
5180 if (reg == R_SS) {
5181 /* if reg == SS, inhibit interrupts/trace */
a2cc3b24
FB
5182 /* If several instructions disable interrupts, only the
5183 _first_ does it */
5184 if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
f0967a1a 5185 gen_helper_set_inhibit_irq(cpu_env);
2c0262af
FB
5186 s->tf = 0;
5187 }
5188 if (s->is_jmp) {
14ce26e7 5189 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
5190 gen_eob(s);
5191 }
5192 break;
5193 case 0x8c: /* mov Gv, seg */
0af10c86 5194 modrm = cpu_ldub_code(env, s->pc++);
2c0262af
FB
5195 reg = (modrm >> 3) & 7;
5196 mod = (modrm >> 6) & 3;
5197 if (reg >= 6)
5198 goto illegal_op;
5199 gen_op_movl_T0_seg(reg);
14ce26e7
FB
5200 if (mod == 3)
5201 ot = OT_WORD + dflag;
5202 else
5203 ot = OT_WORD;
0af10c86 5204 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
2c0262af
FB
5205 break;
5206
5207 case 0x1b6: /* movzbS Gv, Eb */
5208 case 0x1b7: /* movzwS Gv, Eb */
5209 case 0x1be: /* movsbS Gv, Eb */
5210 case 0x1bf: /* movswS Gv, Eb */
5211 {
5212 int d_ot;
5213 /* d_ot is the size of destination */
5214 d_ot = dflag + OT_WORD;
5215 /* ot is the size of source */
5216 ot = (b & 1) + OT_BYTE;
0af10c86 5217 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7 5218 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af 5219 mod = (modrm >> 6) & 3;
14ce26e7 5220 rm = (modrm & 7) | REX_B(s);
3b46e624 5221
2c0262af 5222 if (mod == 3) {
57fec1fe 5223 gen_op_mov_TN_reg(ot, 0, rm);
2c0262af
FB
5224 switch(ot | (b & 8)) {
5225 case OT_BYTE:
e108dd01 5226 tcg_gen_ext8u_tl(cpu_T[0], cpu_T[0]);
2c0262af
FB
5227 break;
5228 case OT_BYTE | 8:
e108dd01 5229 tcg_gen_ext8s_tl(cpu_T[0], cpu_T[0]);
2c0262af
FB
5230 break;
5231 case OT_WORD:
e108dd01 5232 tcg_gen_ext16u_tl(cpu_T[0], cpu_T[0]);
2c0262af
FB
5233 break;
5234 default:
5235 case OT_WORD | 8:
e108dd01 5236 tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
2c0262af
FB
5237 break;
5238 }
57fec1fe 5239 gen_op_mov_reg_T0(d_ot, reg);
2c0262af 5240 } else {
0af10c86 5241 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
2c0262af 5242 if (b & 8) {
57fec1fe 5243 gen_op_lds_T0_A0(ot + s->mem_index);
2c0262af 5244 } else {
57fec1fe 5245 gen_op_ldu_T0_A0(ot + s->mem_index);
2c0262af 5246 }
57fec1fe 5247 gen_op_mov_reg_T0(d_ot, reg);
2c0262af
FB
5248 }
5249 }
5250 break;
5251
5252 case 0x8d: /* lea */
14ce26e7 5253 ot = dflag + OT_WORD;
0af10c86 5254 modrm = cpu_ldub_code(env, s->pc++);
3a1d9b8b
FB
5255 mod = (modrm >> 6) & 3;
5256 if (mod == 3)
5257 goto illegal_op;
14ce26e7 5258 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af
FB
5259 /* we must ensure that no segment is added */
5260 s->override = -1;
5261 val = s->addseg;
5262 s->addseg = 0;
0af10c86 5263 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
2c0262af 5264 s->addseg = val;
57fec1fe 5265 gen_op_mov_reg_A0(ot - OT_WORD, reg);
2c0262af 5266 break;
3b46e624 5267
2c0262af
FB
5268 case 0xa0: /* mov EAX, Ov */
5269 case 0xa1:
5270 case 0xa2: /* mov Ov, EAX */
5271 case 0xa3:
2c0262af 5272 {
14ce26e7
FB
5273 target_ulong offset_addr;
5274
5275 if ((b & 1) == 0)
5276 ot = OT_BYTE;
5277 else
5278 ot = dflag + OT_WORD;
5279#ifdef TARGET_X86_64
8f091a59 5280 if (s->aflag == 2) {
0af10c86 5281 offset_addr = cpu_ldq_code(env, s->pc);
14ce26e7 5282 s->pc += 8;
57fec1fe 5283 gen_op_movq_A0_im(offset_addr);
5fafdf24 5284 } else
14ce26e7
FB
5285#endif
5286 {
5287 if (s->aflag) {
0af10c86 5288 offset_addr = insn_get(env, s, OT_LONG);
14ce26e7 5289 } else {
0af10c86 5290 offset_addr = insn_get(env, s, OT_WORD);
14ce26e7
FB
5291 }
5292 gen_op_movl_A0_im(offset_addr);
5293 }
664e0f19 5294 gen_add_A0_ds_seg(s);
14ce26e7 5295 if ((b & 2) == 0) {
57fec1fe
FB
5296 gen_op_ld_T0_A0(ot + s->mem_index);
5297 gen_op_mov_reg_T0(ot, R_EAX);
14ce26e7 5298 } else {
57fec1fe
FB
5299 gen_op_mov_TN_reg(ot, 0, R_EAX);
5300 gen_op_st_T0_A0(ot + s->mem_index);
2c0262af
FB
5301 }
5302 }
2c0262af
FB
5303 break;
5304 case 0xd7: /* xlat */
14ce26e7 5305#ifdef TARGET_X86_64
8f091a59 5306 if (s->aflag == 2) {
57fec1fe 5307 gen_op_movq_A0_reg(R_EBX);
bbf662ee
FB
5308 gen_op_mov_TN_reg(OT_QUAD, 0, R_EAX);
5309 tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xff);
5310 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_T[0]);
5fafdf24 5311 } else
14ce26e7
FB
5312#endif
5313 {
57fec1fe 5314 gen_op_movl_A0_reg(R_EBX);
bbf662ee
FB
5315 gen_op_mov_TN_reg(OT_LONG, 0, R_EAX);
5316 tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xff);
5317 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_T[0]);
14ce26e7
FB
5318 if (s->aflag == 0)
5319 gen_op_andl_A0_ffff();
bbf662ee
FB
5320 else
5321 tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffffffff);
14ce26e7 5322 }
664e0f19 5323 gen_add_A0_ds_seg(s);
57fec1fe
FB
5324 gen_op_ldu_T0_A0(OT_BYTE + s->mem_index);
5325 gen_op_mov_reg_T0(OT_BYTE, R_EAX);
2c0262af
FB
5326 break;
5327 case 0xb0 ... 0xb7: /* mov R, Ib */
0af10c86 5328 val = insn_get(env, s, OT_BYTE);
2c0262af 5329 gen_op_movl_T0_im(val);
57fec1fe 5330 gen_op_mov_reg_T0(OT_BYTE, (b & 7) | REX_B(s));
2c0262af
FB
5331 break;
5332 case 0xb8 ... 0xbf: /* mov R, Iv */
14ce26e7
FB
5333#ifdef TARGET_X86_64
5334 if (dflag == 2) {
5335 uint64_t tmp;
5336 /* 64 bit case */
0af10c86 5337 tmp = cpu_ldq_code(env, s->pc);
14ce26e7
FB
5338 s->pc += 8;
5339 reg = (b & 7) | REX_B(s);
5340 gen_movtl_T0_im(tmp);
57fec1fe 5341 gen_op_mov_reg_T0(OT_QUAD, reg);
5fafdf24 5342 } else
14ce26e7
FB
5343#endif
5344 {
5345 ot = dflag ? OT_LONG : OT_WORD;
0af10c86 5346 val = insn_get(env, s, ot);
14ce26e7
FB
5347 reg = (b & 7) | REX_B(s);
5348 gen_op_movl_T0_im(val);
57fec1fe 5349 gen_op_mov_reg_T0(ot, reg);
14ce26e7 5350 }
2c0262af
FB
5351 break;
5352
5353 case 0x91 ... 0x97: /* xchg R, EAX */
7418027e 5354 do_xchg_reg_eax:
14ce26e7
FB
5355 ot = dflag + OT_WORD;
5356 reg = (b & 7) | REX_B(s);
2c0262af
FB
5357 rm = R_EAX;
5358 goto do_xchg_reg;
5359 case 0x86:
5360 case 0x87: /* xchg Ev, Gv */
5361 if ((b & 1) == 0)
5362 ot = OT_BYTE;
5363 else
14ce26e7 5364 ot = dflag + OT_WORD;
0af10c86 5365 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7 5366 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af
FB
5367 mod = (modrm >> 6) & 3;
5368 if (mod == 3) {
14ce26e7 5369 rm = (modrm & 7) | REX_B(s);
2c0262af 5370 do_xchg_reg:
57fec1fe
FB
5371 gen_op_mov_TN_reg(ot, 0, reg);
5372 gen_op_mov_TN_reg(ot, 1, rm);
5373 gen_op_mov_reg_T0(ot, rm);
5374 gen_op_mov_reg_T1(ot, reg);
2c0262af 5375 } else {
0af10c86 5376 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
57fec1fe 5377 gen_op_mov_TN_reg(ot, 0, reg);
2c0262af
FB
5378 /* for xchg, lock is implicit */
5379 if (!(prefixes & PREFIX_LOCK))
a7812ae4 5380 gen_helper_lock();
57fec1fe
FB
5381 gen_op_ld_T1_A0(ot + s->mem_index);
5382 gen_op_st_T0_A0(ot + s->mem_index);
2c0262af 5383 if (!(prefixes & PREFIX_LOCK))
a7812ae4 5384 gen_helper_unlock();
57fec1fe 5385 gen_op_mov_reg_T1(ot, reg);
2c0262af
FB
5386 }
5387 break;
5388 case 0xc4: /* les Gv */
14ce26e7
FB
5389 if (CODE64(s))
5390 goto illegal_op;
2c0262af
FB
5391 op = R_ES;
5392 goto do_lxx;
5393 case 0xc5: /* lds Gv */
14ce26e7
FB
5394 if (CODE64(s))
5395 goto illegal_op;
2c0262af
FB
5396 op = R_DS;
5397 goto do_lxx;
5398 case 0x1b2: /* lss Gv */
5399 op = R_SS;
5400 goto do_lxx;
5401 case 0x1b4: /* lfs Gv */
5402 op = R_FS;
5403 goto do_lxx;
5404 case 0x1b5: /* lgs Gv */
5405 op = R_GS;
5406 do_lxx:
5407 ot = dflag ? OT_LONG : OT_WORD;
0af10c86 5408 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7 5409 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af
FB
5410 mod = (modrm >> 6) & 3;
5411 if (mod == 3)
5412 goto illegal_op;
0af10c86 5413 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
57fec1fe 5414 gen_op_ld_T1_A0(ot + s->mem_index);
aba9d61e 5415 gen_add_A0_im(s, 1 << (ot - OT_WORD + 1));
2c0262af 5416 /* load the segment first to handle exceptions properly */
57fec1fe 5417 gen_op_ldu_T0_A0(OT_WORD + s->mem_index);
2c0262af
FB
5418 gen_movl_seg_T0(s, op, pc_start - s->cs_base);
5419 /* then put the data */
57fec1fe 5420 gen_op_mov_reg_T1(ot, reg);
2c0262af 5421 if (s->is_jmp) {
14ce26e7 5422 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
5423 gen_eob(s);
5424 }
5425 break;
3b46e624 5426
2c0262af
FB
5427 /************************/
5428 /* shifts */
5429 case 0xc0:
5430 case 0xc1:
5431 /* shift Ev,Ib */
5432 shift = 2;
5433 grp2:
5434 {
5435 if ((b & 1) == 0)
5436 ot = OT_BYTE;
5437 else
14ce26e7 5438 ot = dflag + OT_WORD;
3b46e624 5439
0af10c86 5440 modrm = cpu_ldub_code(env, s->pc++);
2c0262af 5441 mod = (modrm >> 6) & 3;
2c0262af 5442 op = (modrm >> 3) & 7;
3b46e624 5443
2c0262af 5444 if (mod != 3) {
14ce26e7
FB
5445 if (shift == 2) {
5446 s->rip_offset = 1;
5447 }
0af10c86 5448 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
2c0262af
FB
5449 opreg = OR_TMP0;
5450 } else {
14ce26e7 5451 opreg = (modrm & 7) | REX_B(s);
2c0262af
FB
5452 }
5453
5454 /* simpler op */
5455 if (shift == 0) {
5456 gen_shift(s, op, ot, opreg, OR_ECX);
5457 } else {
5458 if (shift == 2) {
0af10c86 5459 shift = cpu_ldub_code(env, s->pc++);
2c0262af
FB
5460 }
5461 gen_shifti(s, op, ot, opreg, shift);
5462 }
5463 }
5464 break;
5465 case 0xd0:
5466 case 0xd1:
5467 /* shift Ev,1 */
5468 shift = 1;
5469 goto grp2;
5470 case 0xd2:
5471 case 0xd3:
5472 /* shift Ev,cl */
5473 shift = 0;
5474 goto grp2;
5475
5476 case 0x1a4: /* shld imm */
5477 op = 0;
5478 shift = 1;
5479 goto do_shiftd;
5480 case 0x1a5: /* shld cl */
5481 op = 0;
5482 shift = 0;
5483 goto do_shiftd;
5484 case 0x1ac: /* shrd imm */
5485 op = 1;
5486 shift = 1;
5487 goto do_shiftd;
5488 case 0x1ad: /* shrd cl */
5489 op = 1;
5490 shift = 0;
5491 do_shiftd:
14ce26e7 5492 ot = dflag + OT_WORD;
0af10c86 5493 modrm = cpu_ldub_code(env, s->pc++);
2c0262af 5494 mod = (modrm >> 6) & 3;
14ce26e7
FB
5495 rm = (modrm & 7) | REX_B(s);
5496 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af 5497 if (mod != 3) {
0af10c86 5498 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
b6abf97d 5499 opreg = OR_TMP0;
2c0262af 5500 } else {
b6abf97d 5501 opreg = rm;
2c0262af 5502 }
57fec1fe 5503 gen_op_mov_TN_reg(ot, 1, reg);
3b46e624 5504
2c0262af 5505 if (shift) {
0af10c86 5506 val = cpu_ldub_code(env, s->pc++);
b6abf97d 5507 tcg_gen_movi_tl(cpu_T3, val);
2c0262af 5508 } else {
cc739bb0 5509 tcg_gen_mov_tl(cpu_T3, cpu_regs[R_ECX]);
2c0262af 5510 }
b6abf97d 5511 gen_shiftd_rm_T1_T3(s, ot, opreg, op);
2c0262af
FB
5512 break;
5513
5514 /************************/
5515 /* floats */
5fafdf24 5516 case 0xd8 ... 0xdf:
7eee2a50
FB
5517 if (s->flags & (HF_EM_MASK | HF_TS_MASK)) {
5518 /* if CR0.EM or CR0.TS are set, generate an FPU exception */
5519 /* XXX: what to do if illegal op ? */
5520 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
5521 break;
5522 }
0af10c86 5523 modrm = cpu_ldub_code(env, s->pc++);
2c0262af
FB
5524 mod = (modrm >> 6) & 3;
5525 rm = modrm & 7;
5526 op = ((b & 7) << 3) | ((modrm >> 3) & 7);
2c0262af
FB
5527 if (mod != 3) {
5528 /* memory op */
0af10c86 5529 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
2c0262af
FB
5530 switch(op) {
5531 case 0x00 ... 0x07: /* fxxxs */
5532 case 0x10 ... 0x17: /* fixxxl */
5533 case 0x20 ... 0x27: /* fxxxl */
5534 case 0x30 ... 0x37: /* fixxx */
5535 {
5536 int op1;
5537 op1 = op & 7;
5538
5539 switch(op >> 4) {
5540 case 0:
ba7cd150 5541 gen_op_ld_T0_A0(OT_LONG + s->mem_index);
b6abf97d 5542 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
d3eb5eae 5543 gen_helper_flds_FT0(cpu_env, cpu_tmp2_i32);
2c0262af
FB
5544 break;
5545 case 1:
ba7cd150 5546 gen_op_ld_T0_A0(OT_LONG + s->mem_index);
b6abf97d 5547 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
d3eb5eae 5548 gen_helper_fildl_FT0(cpu_env, cpu_tmp2_i32);
2c0262af
FB
5549 break;
5550 case 2:
b6abf97d 5551 tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_A0,
19e6c4b8 5552 (s->mem_index >> 2) - 1);
d3eb5eae 5553 gen_helper_fldl_FT0(cpu_env, cpu_tmp1_i64);
2c0262af
FB
5554 break;
5555 case 3:
5556 default:
ba7cd150 5557 gen_op_lds_T0_A0(OT_WORD + s->mem_index);
b6abf97d 5558 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
d3eb5eae 5559 gen_helper_fildl_FT0(cpu_env, cpu_tmp2_i32);
2c0262af
FB
5560 break;
5561 }
3b46e624 5562
a7812ae4 5563 gen_helper_fp_arith_ST0_FT0(op1);
2c0262af
FB
5564 if (op1 == 3) {
5565 /* fcomp needs pop */
d3eb5eae 5566 gen_helper_fpop(cpu_env);
2c0262af
FB
5567 }
5568 }
5569 break;
5570 case 0x08: /* flds */
5571 case 0x0a: /* fsts */
5572 case 0x0b: /* fstps */
465e9838
FB
5573 case 0x18 ... 0x1b: /* fildl, fisttpl, fistl, fistpl */
5574 case 0x28 ... 0x2b: /* fldl, fisttpll, fstl, fstpl */
5575 case 0x38 ... 0x3b: /* filds, fisttps, fists, fistps */
2c0262af
FB
5576 switch(op & 7) {
5577 case 0:
5578 switch(op >> 4) {
5579 case 0:
ba7cd150 5580 gen_op_ld_T0_A0(OT_LONG + s->mem_index);
b6abf97d 5581 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
d3eb5eae 5582 gen_helper_flds_ST0(cpu_env, cpu_tmp2_i32);
2c0262af
FB
5583 break;
5584 case 1:
ba7cd150 5585 gen_op_ld_T0_A0(OT_LONG + s->mem_index);
b6abf97d 5586 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
d3eb5eae 5587 gen_helper_fildl_ST0(cpu_env, cpu_tmp2_i32);
2c0262af
FB
5588 break;
5589 case 2:
b6abf97d 5590 tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_A0,
19e6c4b8 5591 (s->mem_index >> 2) - 1);
d3eb5eae 5592 gen_helper_fldl_ST0(cpu_env, cpu_tmp1_i64);
2c0262af
FB
5593 break;
5594 case 3:
5595 default:
ba7cd150 5596 gen_op_lds_T0_A0(OT_WORD + s->mem_index);
b6abf97d 5597 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
d3eb5eae 5598 gen_helper_fildl_ST0(cpu_env, cpu_tmp2_i32);
2c0262af
FB
5599 break;
5600 }
5601 break;
465e9838 5602 case 1:
19e6c4b8 5603 /* XXX: the corresponding CPUID bit must be tested ! */
465e9838
FB
5604 switch(op >> 4) {
5605 case 1:
d3eb5eae 5606 gen_helper_fisttl_ST0(cpu_tmp2_i32, cpu_env);
b6abf97d 5607 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
ba7cd150 5608 gen_op_st_T0_A0(OT_LONG + s->mem_index);
465e9838
FB
5609 break;
5610 case 2:
d3eb5eae 5611 gen_helper_fisttll_ST0(cpu_tmp1_i64, cpu_env);
b6abf97d 5612 tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_A0,
19e6c4b8 5613 (s->mem_index >> 2) - 1);
465e9838
FB
5614 break;
5615 case 3:
5616 default:
d3eb5eae 5617 gen_helper_fistt_ST0(cpu_tmp2_i32, cpu_env);
b6abf97d 5618 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
ba7cd150 5619 gen_op_st_T0_A0(OT_WORD + s->mem_index);
19e6c4b8 5620 break;
465e9838 5621 }
d3eb5eae 5622 gen_helper_fpop(cpu_env);
465e9838 5623 break;
2c0262af
FB
5624 default:
5625 switch(op >> 4) {
5626 case 0:
d3eb5eae 5627 gen_helper_fsts_ST0(cpu_tmp2_i32, cpu_env);
b6abf97d 5628 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
ba7cd150 5629 gen_op_st_T0_A0(OT_LONG + s->mem_index);
2c0262af
FB
5630 break;
5631 case 1:
d3eb5eae 5632 gen_helper_fistl_ST0(cpu_tmp2_i32, cpu_env);
b6abf97d 5633 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
ba7cd150 5634 gen_op_st_T0_A0(OT_LONG + s->mem_index);
2c0262af
FB
5635 break;
5636 case 2:
d3eb5eae 5637 gen_helper_fstl_ST0(cpu_tmp1_i64, cpu_env);
b6abf97d 5638 tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_A0,
19e6c4b8 5639 (s->mem_index >> 2) - 1);
2c0262af
FB
5640 break;
5641 case 3:
5642 default:
d3eb5eae 5643 gen_helper_fist_ST0(cpu_tmp2_i32, cpu_env);
b6abf97d 5644 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
ba7cd150 5645 gen_op_st_T0_A0(OT_WORD + s->mem_index);
2c0262af
FB
5646 break;
5647 }
5648 if ((op & 7) == 3)
d3eb5eae 5649 gen_helper_fpop(cpu_env);
2c0262af
FB
5650 break;
5651 }
5652 break;
5653 case 0x0c: /* fldenv mem */
19e6c4b8
FB
5654 if (s->cc_op != CC_OP_DYNAMIC)
5655 gen_op_set_cc_op(s->cc_op);
5656 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae 5657 gen_helper_fldenv(cpu_env, cpu_A0, tcg_const_i32(s->dflag));
2c0262af
FB
5658 break;
5659 case 0x0d: /* fldcw mem */
19e6c4b8 5660 gen_op_ld_T0_A0(OT_WORD + s->mem_index);
b6abf97d 5661 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
d3eb5eae 5662 gen_helper_fldcw(cpu_env, cpu_tmp2_i32);
2c0262af
FB
5663 break;
5664 case 0x0e: /* fnstenv mem */
19e6c4b8
FB
5665 if (s->cc_op != CC_OP_DYNAMIC)
5666 gen_op_set_cc_op(s->cc_op);
5667 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae 5668 gen_helper_fstenv(cpu_env, cpu_A0, tcg_const_i32(s->dflag));
2c0262af
FB
5669 break;
5670 case 0x0f: /* fnstcw mem */
d3eb5eae 5671 gen_helper_fnstcw(cpu_tmp2_i32, cpu_env);
b6abf97d 5672 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
19e6c4b8 5673 gen_op_st_T0_A0(OT_WORD + s->mem_index);
2c0262af
FB
5674 break;
5675 case 0x1d: /* fldt mem */
19e6c4b8
FB
5676 if (s->cc_op != CC_OP_DYNAMIC)
5677 gen_op_set_cc_op(s->cc_op);
5678 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae 5679 gen_helper_fldt_ST0(cpu_env, cpu_A0);
2c0262af
FB
5680 break;
5681 case 0x1f: /* fstpt mem */
19e6c4b8
FB
5682 if (s->cc_op != CC_OP_DYNAMIC)
5683 gen_op_set_cc_op(s->cc_op);
5684 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae
BS
5685 gen_helper_fstt_ST0(cpu_env, cpu_A0);
5686 gen_helper_fpop(cpu_env);
2c0262af
FB
5687 break;
5688 case 0x2c: /* frstor mem */
19e6c4b8
FB
5689 if (s->cc_op != CC_OP_DYNAMIC)
5690 gen_op_set_cc_op(s->cc_op);
5691 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae 5692 gen_helper_frstor(cpu_env, cpu_A0, tcg_const_i32(s->dflag));
2c0262af
FB
5693 break;
5694 case 0x2e: /* fnsave mem */
19e6c4b8
FB
5695 if (s->cc_op != CC_OP_DYNAMIC)
5696 gen_op_set_cc_op(s->cc_op);
5697 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae 5698 gen_helper_fsave(cpu_env, cpu_A0, tcg_const_i32(s->dflag));
2c0262af
FB
5699 break;
5700 case 0x2f: /* fnstsw mem */
d3eb5eae 5701 gen_helper_fnstsw(cpu_tmp2_i32, cpu_env);
b6abf97d 5702 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
19e6c4b8 5703 gen_op_st_T0_A0(OT_WORD + s->mem_index);
2c0262af
FB
5704 break;
5705 case 0x3c: /* fbld */
19e6c4b8
FB
5706 if (s->cc_op != CC_OP_DYNAMIC)
5707 gen_op_set_cc_op(s->cc_op);
5708 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae 5709 gen_helper_fbld_ST0(cpu_env, cpu_A0);
2c0262af
FB
5710 break;
5711 case 0x3e: /* fbstp */
19e6c4b8
FB
5712 if (s->cc_op != CC_OP_DYNAMIC)
5713 gen_op_set_cc_op(s->cc_op);
5714 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae
BS
5715 gen_helper_fbst_ST0(cpu_env, cpu_A0);
5716 gen_helper_fpop(cpu_env);
2c0262af
FB
5717 break;
5718 case 0x3d: /* fildll */
b6abf97d 5719 tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_A0,
19e6c4b8 5720 (s->mem_index >> 2) - 1);
d3eb5eae 5721 gen_helper_fildll_ST0(cpu_env, cpu_tmp1_i64);
2c0262af
FB
5722 break;
5723 case 0x3f: /* fistpll */
d3eb5eae 5724 gen_helper_fistll_ST0(cpu_tmp1_i64, cpu_env);
b6abf97d 5725 tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_A0,
19e6c4b8 5726 (s->mem_index >> 2) - 1);
d3eb5eae 5727 gen_helper_fpop(cpu_env);
2c0262af
FB
5728 break;
5729 default:
5730 goto illegal_op;
5731 }
5732 } else {
5733 /* register float ops */
5734 opreg = rm;
5735
5736 switch(op) {
5737 case 0x08: /* fld sti */
d3eb5eae
BS
5738 gen_helper_fpush(cpu_env);
5739 gen_helper_fmov_ST0_STN(cpu_env,
5740 tcg_const_i32((opreg + 1) & 7));
2c0262af
FB
5741 break;
5742 case 0x09: /* fxchg sti */
c169c906
FB
5743 case 0x29: /* fxchg4 sti, undocumented op */
5744 case 0x39: /* fxchg7 sti, undocumented op */
d3eb5eae 5745 gen_helper_fxchg_ST0_STN(cpu_env, tcg_const_i32(opreg));
2c0262af
FB
5746 break;
5747 case 0x0a: /* grp d9/2 */
5748 switch(rm) {
5749 case 0: /* fnop */
023fe10d
FB
5750 /* check exceptions (FreeBSD FPU probe) */
5751 if (s->cc_op != CC_OP_DYNAMIC)
5752 gen_op_set_cc_op(s->cc_op);
14ce26e7 5753 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae 5754 gen_helper_fwait(cpu_env);
2c0262af
FB
5755 break;
5756 default:
5757 goto illegal_op;
5758 }
5759 break;
5760 case 0x0c: /* grp d9/4 */
5761 switch(rm) {
5762 case 0: /* fchs */
d3eb5eae 5763 gen_helper_fchs_ST0(cpu_env);
2c0262af
FB
5764 break;
5765 case 1: /* fabs */
d3eb5eae 5766 gen_helper_fabs_ST0(cpu_env);
2c0262af
FB
5767 break;
5768 case 4: /* ftst */
d3eb5eae
BS
5769 gen_helper_fldz_FT0(cpu_env);
5770 gen_helper_fcom_ST0_FT0(cpu_env);
2c0262af
FB
5771 break;
5772 case 5: /* fxam */
d3eb5eae 5773 gen_helper_fxam_ST0(cpu_env);
2c0262af
FB
5774 break;
5775 default:
5776 goto illegal_op;
5777 }
5778 break;
5779 case 0x0d: /* grp d9/5 */
5780 {
5781 switch(rm) {
5782 case 0:
d3eb5eae
BS
5783 gen_helper_fpush(cpu_env);
5784 gen_helper_fld1_ST0(cpu_env);
2c0262af
FB
5785 break;
5786 case 1:
d3eb5eae
BS
5787 gen_helper_fpush(cpu_env);
5788 gen_helper_fldl2t_ST0(cpu_env);
2c0262af
FB
5789 break;
5790 case 2:
d3eb5eae
BS
5791 gen_helper_fpush(cpu_env);
5792 gen_helper_fldl2e_ST0(cpu_env);
2c0262af
FB
5793 break;
5794 case 3:
d3eb5eae
BS
5795 gen_helper_fpush(cpu_env);
5796 gen_helper_fldpi_ST0(cpu_env);
2c0262af
FB
5797 break;
5798 case 4:
d3eb5eae
BS
5799 gen_helper_fpush(cpu_env);
5800 gen_helper_fldlg2_ST0(cpu_env);
2c0262af
FB
5801 break;
5802 case 5:
d3eb5eae
BS
5803 gen_helper_fpush(cpu_env);
5804 gen_helper_fldln2_ST0(cpu_env);
2c0262af
FB
5805 break;
5806 case 6:
d3eb5eae
BS
5807 gen_helper_fpush(cpu_env);
5808 gen_helper_fldz_ST0(cpu_env);
2c0262af
FB
5809 break;
5810 default:
5811 goto illegal_op;
5812 }
5813 }
5814 break;
5815 case 0x0e: /* grp d9/6 */
5816 switch(rm) {
5817 case 0: /* f2xm1 */
d3eb5eae 5818 gen_helper_f2xm1(cpu_env);
2c0262af
FB
5819 break;
5820 case 1: /* fyl2x */
d3eb5eae 5821 gen_helper_fyl2x(cpu_env);
2c0262af
FB
5822 break;
5823 case 2: /* fptan */
d3eb5eae 5824 gen_helper_fptan(cpu_env);
2c0262af
FB
5825 break;
5826 case 3: /* fpatan */
d3eb5eae 5827 gen_helper_fpatan(cpu_env);
2c0262af
FB
5828 break;
5829 case 4: /* fxtract */
d3eb5eae 5830 gen_helper_fxtract(cpu_env);
2c0262af
FB
5831 break;
5832 case 5: /* fprem1 */
d3eb5eae 5833 gen_helper_fprem1(cpu_env);
2c0262af
FB
5834 break;
5835 case 6: /* fdecstp */
d3eb5eae 5836 gen_helper_fdecstp(cpu_env);
2c0262af
FB
5837 break;
5838 default:
5839 case 7: /* fincstp */
d3eb5eae 5840 gen_helper_fincstp(cpu_env);
2c0262af
FB
5841 break;
5842 }
5843 break;
5844 case 0x0f: /* grp d9/7 */
5845 switch(rm) {
5846 case 0: /* fprem */
d3eb5eae 5847 gen_helper_fprem(cpu_env);
2c0262af
FB
5848 break;
5849 case 1: /* fyl2xp1 */
d3eb5eae 5850 gen_helper_fyl2xp1(cpu_env);
2c0262af
FB
5851 break;
5852 case 2: /* fsqrt */
d3eb5eae 5853 gen_helper_fsqrt(cpu_env);
2c0262af
FB
5854 break;
5855 case 3: /* fsincos */
d3eb5eae 5856 gen_helper_fsincos(cpu_env);
2c0262af
FB
5857 break;
5858 case 5: /* fscale */
d3eb5eae 5859 gen_helper_fscale(cpu_env);
2c0262af
FB
5860 break;
5861 case 4: /* frndint */
d3eb5eae 5862 gen_helper_frndint(cpu_env);
2c0262af
FB
5863 break;
5864 case 6: /* fsin */
d3eb5eae 5865 gen_helper_fsin(cpu_env);
2c0262af
FB
5866 break;
5867 default:
5868 case 7: /* fcos */
d3eb5eae 5869 gen_helper_fcos(cpu_env);
2c0262af
FB
5870 break;
5871 }
5872 break;
5873 case 0x00: case 0x01: case 0x04 ... 0x07: /* fxxx st, sti */
5874 case 0x20: case 0x21: case 0x24 ... 0x27: /* fxxx sti, st */
5875 case 0x30: case 0x31: case 0x34 ... 0x37: /* fxxxp sti, st */
5876 {
5877 int op1;
3b46e624 5878
2c0262af
FB
5879 op1 = op & 7;
5880 if (op >= 0x20) {
a7812ae4 5881 gen_helper_fp_arith_STN_ST0(op1, opreg);
2c0262af 5882 if (op >= 0x30)
d3eb5eae 5883 gen_helper_fpop(cpu_env);
2c0262af 5884 } else {
d3eb5eae 5885 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
a7812ae4 5886 gen_helper_fp_arith_ST0_FT0(op1);
2c0262af
FB
5887 }
5888 }
5889 break;
5890 case 0x02: /* fcom */
c169c906 5891 case 0x22: /* fcom2, undocumented op */
d3eb5eae
BS
5892 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
5893 gen_helper_fcom_ST0_FT0(cpu_env);
2c0262af
FB
5894 break;
5895 case 0x03: /* fcomp */
c169c906
FB
5896 case 0x23: /* fcomp3, undocumented op */
5897 case 0x32: /* fcomp5, undocumented op */
d3eb5eae
BS
5898 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
5899 gen_helper_fcom_ST0_FT0(cpu_env);
5900 gen_helper_fpop(cpu_env);
2c0262af
FB
5901 break;
5902 case 0x15: /* da/5 */
5903 switch(rm) {
5904 case 1: /* fucompp */
d3eb5eae
BS
5905 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(1));
5906 gen_helper_fucom_ST0_FT0(cpu_env);
5907 gen_helper_fpop(cpu_env);
5908 gen_helper_fpop(cpu_env);
2c0262af
FB
5909 break;
5910 default:
5911 goto illegal_op;
5912 }
5913 break;
5914 case 0x1c:
5915 switch(rm) {
5916 case 0: /* feni (287 only, just do nop here) */
5917 break;
5918 case 1: /* fdisi (287 only, just do nop here) */
5919 break;
5920 case 2: /* fclex */
d3eb5eae 5921 gen_helper_fclex(cpu_env);
2c0262af
FB
5922 break;
5923 case 3: /* fninit */
d3eb5eae 5924 gen_helper_fninit(cpu_env);
2c0262af
FB
5925 break;
5926 case 4: /* fsetpm (287 only, just do nop here) */
5927 break;
5928 default:
5929 goto illegal_op;
5930 }
5931 break;
5932 case 0x1d: /* fucomi */
5933 if (s->cc_op != CC_OP_DYNAMIC)
5934 gen_op_set_cc_op(s->cc_op);
d3eb5eae
BS
5935 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
5936 gen_helper_fucomi_ST0_FT0(cpu_env);
3ca51d07 5937 set_cc_op(s, CC_OP_EFLAGS);
2c0262af
FB
5938 break;
5939 case 0x1e: /* fcomi */
5940 if (s->cc_op != CC_OP_DYNAMIC)
5941 gen_op_set_cc_op(s->cc_op);
d3eb5eae
BS
5942 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
5943 gen_helper_fcomi_ST0_FT0(cpu_env);
3ca51d07 5944 set_cc_op(s, CC_OP_EFLAGS);
2c0262af 5945 break;
658c8bda 5946 case 0x28: /* ffree sti */
d3eb5eae 5947 gen_helper_ffree_STN(cpu_env, tcg_const_i32(opreg));
5fafdf24 5948 break;
2c0262af 5949 case 0x2a: /* fst sti */
d3eb5eae 5950 gen_helper_fmov_STN_ST0(cpu_env, tcg_const_i32(opreg));
2c0262af
FB
5951 break;
5952 case 0x2b: /* fstp sti */
c169c906
FB
5953 case 0x0b: /* fstp1 sti, undocumented op */
5954 case 0x3a: /* fstp8 sti, undocumented op */
5955 case 0x3b: /* fstp9 sti, undocumented op */
d3eb5eae
BS
5956 gen_helper_fmov_STN_ST0(cpu_env, tcg_const_i32(opreg));
5957 gen_helper_fpop(cpu_env);
2c0262af
FB
5958 break;
5959 case 0x2c: /* fucom st(i) */
d3eb5eae
BS
5960 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
5961 gen_helper_fucom_ST0_FT0(cpu_env);
2c0262af
FB
5962 break;
5963 case 0x2d: /* fucomp st(i) */
d3eb5eae
BS
5964 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
5965 gen_helper_fucom_ST0_FT0(cpu_env);
5966 gen_helper_fpop(cpu_env);
2c0262af
FB
5967 break;
5968 case 0x33: /* de/3 */
5969 switch(rm) {
5970 case 1: /* fcompp */
d3eb5eae
BS
5971 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(1));
5972 gen_helper_fcom_ST0_FT0(cpu_env);
5973 gen_helper_fpop(cpu_env);
5974 gen_helper_fpop(cpu_env);
2c0262af
FB
5975 break;
5976 default:
5977 goto illegal_op;
5978 }
5979 break;
c169c906 5980 case 0x38: /* ffreep sti, undocumented op */
d3eb5eae
BS
5981 gen_helper_ffree_STN(cpu_env, tcg_const_i32(opreg));
5982 gen_helper_fpop(cpu_env);
c169c906 5983 break;
2c0262af
FB
5984 case 0x3c: /* df/4 */
5985 switch(rm) {
5986 case 0:
d3eb5eae 5987 gen_helper_fnstsw(cpu_tmp2_i32, cpu_env);
b6abf97d 5988 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
19e6c4b8 5989 gen_op_mov_reg_T0(OT_WORD, R_EAX);
2c0262af
FB
5990 break;
5991 default:
5992 goto illegal_op;
5993 }
5994 break;
5995 case 0x3d: /* fucomip */
5996 if (s->cc_op != CC_OP_DYNAMIC)
5997 gen_op_set_cc_op(s->cc_op);
d3eb5eae
BS
5998 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
5999 gen_helper_fucomi_ST0_FT0(cpu_env);
6000 gen_helper_fpop(cpu_env);
3ca51d07 6001 set_cc_op(s, CC_OP_EFLAGS);
2c0262af
FB
6002 break;
6003 case 0x3e: /* fcomip */
6004 if (s->cc_op != CC_OP_DYNAMIC)
6005 gen_op_set_cc_op(s->cc_op);
d3eb5eae
BS
6006 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6007 gen_helper_fcomi_ST0_FT0(cpu_env);
6008 gen_helper_fpop(cpu_env);
3ca51d07 6009 set_cc_op(s, CC_OP_EFLAGS);
2c0262af 6010 break;
a2cc3b24
FB
6011 case 0x10 ... 0x13: /* fcmovxx */
6012 case 0x18 ... 0x1b:
6013 {
19e6c4b8 6014 int op1, l1;
d70040bc 6015 static const uint8_t fcmov_cc[8] = {
a2cc3b24
FB
6016 (JCC_B << 1),
6017 (JCC_Z << 1),
6018 (JCC_BE << 1),
6019 (JCC_P << 1),
6020 };
1e4840bf 6021 op1 = fcmov_cc[op & 3] | (((op >> 3) & 1) ^ 1);
19e6c4b8 6022 l1 = gen_new_label();
b27fc131 6023 gen_jcc1(s, op1, l1);
d3eb5eae 6024 gen_helper_fmov_ST0_STN(cpu_env, tcg_const_i32(opreg));
19e6c4b8 6025 gen_set_label(l1);
a2cc3b24
FB
6026 }
6027 break;
2c0262af
FB
6028 default:
6029 goto illegal_op;
6030 }
6031 }
6032 break;
6033 /************************/
6034 /* string ops */
6035
6036 case 0xa4: /* movsS */
6037 case 0xa5:
6038 if ((b & 1) == 0)
6039 ot = OT_BYTE;
6040 else
14ce26e7 6041 ot = dflag + OT_WORD;
2c0262af
FB
6042
6043 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
6044 gen_repz_movs(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
6045 } else {
6046 gen_movs(s, ot);
6047 }
6048 break;
3b46e624 6049
2c0262af
FB
6050 case 0xaa: /* stosS */
6051 case 0xab:
6052 if ((b & 1) == 0)
6053 ot = OT_BYTE;
6054 else
14ce26e7 6055 ot = dflag + OT_WORD;
2c0262af
FB
6056
6057 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
6058 gen_repz_stos(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
6059 } else {
6060 gen_stos(s, ot);
6061 }
6062 break;
6063 case 0xac: /* lodsS */
6064 case 0xad:
6065 if ((b & 1) == 0)
6066 ot = OT_BYTE;
6067 else
14ce26e7 6068 ot = dflag + OT_WORD;
2c0262af
FB
6069 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
6070 gen_repz_lods(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
6071 } else {
6072 gen_lods(s, ot);
6073 }
6074 break;
6075 case 0xae: /* scasS */
6076 case 0xaf:
6077 if ((b & 1) == 0)
6078 ot = OT_BYTE;
6079 else
14ce26e7 6080 ot = dflag + OT_WORD;
2c0262af
FB
6081 if (prefixes & PREFIX_REPNZ) {
6082 gen_repz_scas(s, ot, pc_start - s->cs_base, s->pc - s->cs_base, 1);
6083 } else if (prefixes & PREFIX_REPZ) {
6084 gen_repz_scas(s, ot, pc_start - s->cs_base, s->pc - s->cs_base, 0);
6085 } else {
6086 gen_scas(s, ot);
2c0262af
FB
6087 }
6088 break;
6089
6090 case 0xa6: /* cmpsS */
6091 case 0xa7:
6092 if ((b & 1) == 0)
6093 ot = OT_BYTE;
6094 else
14ce26e7 6095 ot = dflag + OT_WORD;
2c0262af
FB
6096 if (prefixes & PREFIX_REPNZ) {
6097 gen_repz_cmps(s, ot, pc_start - s->cs_base, s->pc - s->cs_base, 1);
6098 } else if (prefixes & PREFIX_REPZ) {
6099 gen_repz_cmps(s, ot, pc_start - s->cs_base, s->pc - s->cs_base, 0);
6100 } else {
6101 gen_cmps(s, ot);
2c0262af
FB
6102 }
6103 break;
6104 case 0x6c: /* insS */
6105 case 0x6d:
f115e911
FB
6106 if ((b & 1) == 0)
6107 ot = OT_BYTE;
6108 else
6109 ot = dflag ? OT_LONG : OT_WORD;
57fec1fe 6110 gen_op_mov_TN_reg(OT_WORD, 0, R_EDX);
0573fbfc 6111 gen_op_andl_T0_ffff();
b8b6a50b
FB
6112 gen_check_io(s, ot, pc_start - s->cs_base,
6113 SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes) | 4);
f115e911
FB
6114 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
6115 gen_repz_ins(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
2c0262af 6116 } else {
f115e911 6117 gen_ins(s, ot);
2e70f6ef
PB
6118 if (use_icount) {
6119 gen_jmp(s, s->pc - s->cs_base);
6120 }
2c0262af
FB
6121 }
6122 break;
6123 case 0x6e: /* outsS */
6124 case 0x6f:
f115e911
FB
6125 if ((b & 1) == 0)
6126 ot = OT_BYTE;
6127 else
6128 ot = dflag ? OT_LONG : OT_WORD;
57fec1fe 6129 gen_op_mov_TN_reg(OT_WORD, 0, R_EDX);
0573fbfc 6130 gen_op_andl_T0_ffff();
b8b6a50b
FB
6131 gen_check_io(s, ot, pc_start - s->cs_base,
6132 svm_is_rep(prefixes) | 4);
f115e911
FB
6133 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
6134 gen_repz_outs(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
2c0262af 6135 } else {
f115e911 6136 gen_outs(s, ot);
2e70f6ef
PB
6137 if (use_icount) {
6138 gen_jmp(s, s->pc - s->cs_base);
6139 }
2c0262af
FB
6140 }
6141 break;
6142
6143 /************************/
6144 /* port I/O */
0573fbfc 6145
2c0262af
FB
6146 case 0xe4:
6147 case 0xe5:
f115e911
FB
6148 if ((b & 1) == 0)
6149 ot = OT_BYTE;
6150 else
6151 ot = dflag ? OT_LONG : OT_WORD;
0af10c86 6152 val = cpu_ldub_code(env, s->pc++);
f115e911 6153 gen_op_movl_T0_im(val);
b8b6a50b
FB
6154 gen_check_io(s, ot, pc_start - s->cs_base,
6155 SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
2e70f6ef
PB
6156 if (use_icount)
6157 gen_io_start();
b6abf97d 6158 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
a7812ae4 6159 gen_helper_in_func(ot, cpu_T[1], cpu_tmp2_i32);
57fec1fe 6160 gen_op_mov_reg_T1(ot, R_EAX);
2e70f6ef
PB
6161 if (use_icount) {
6162 gen_io_end();
6163 gen_jmp(s, s->pc - s->cs_base);
6164 }
2c0262af
FB
6165 break;
6166 case 0xe6:
6167 case 0xe7:
f115e911
FB
6168 if ((b & 1) == 0)
6169 ot = OT_BYTE;
6170 else
6171 ot = dflag ? OT_LONG : OT_WORD;
0af10c86 6172 val = cpu_ldub_code(env, s->pc++);
f115e911 6173 gen_op_movl_T0_im(val);
b8b6a50b
FB
6174 gen_check_io(s, ot, pc_start - s->cs_base,
6175 svm_is_rep(prefixes));
57fec1fe 6176 gen_op_mov_TN_reg(ot, 1, R_EAX);
b8b6a50b 6177
2e70f6ef
PB
6178 if (use_icount)
6179 gen_io_start();
b6abf97d 6180 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
b6abf97d 6181 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
a7812ae4 6182 gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
2e70f6ef
PB
6183 if (use_icount) {
6184 gen_io_end();
6185 gen_jmp(s, s->pc - s->cs_base);
6186 }
2c0262af
FB
6187 break;
6188 case 0xec:
6189 case 0xed:
f115e911
FB
6190 if ((b & 1) == 0)
6191 ot = OT_BYTE;
6192 else
6193 ot = dflag ? OT_LONG : OT_WORD;
57fec1fe 6194 gen_op_mov_TN_reg(OT_WORD, 0, R_EDX);
4f31916f 6195 gen_op_andl_T0_ffff();
b8b6a50b
FB
6196 gen_check_io(s, ot, pc_start - s->cs_base,
6197 SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
2e70f6ef
PB
6198 if (use_icount)
6199 gen_io_start();
b6abf97d 6200 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
a7812ae4 6201 gen_helper_in_func(ot, cpu_T[1], cpu_tmp2_i32);
57fec1fe 6202 gen_op_mov_reg_T1(ot, R_EAX);
2e70f6ef
PB
6203 if (use_icount) {
6204 gen_io_end();
6205 gen_jmp(s, s->pc - s->cs_base);
6206 }
2c0262af
FB
6207 break;
6208 case 0xee:
6209 case 0xef:
f115e911
FB
6210 if ((b & 1) == 0)
6211 ot = OT_BYTE;
6212 else
6213 ot = dflag ? OT_LONG : OT_WORD;
57fec1fe 6214 gen_op_mov_TN_reg(OT_WORD, 0, R_EDX);
4f31916f 6215 gen_op_andl_T0_ffff();
b8b6a50b
FB
6216 gen_check_io(s, ot, pc_start - s->cs_base,
6217 svm_is_rep(prefixes));
57fec1fe 6218 gen_op_mov_TN_reg(ot, 1, R_EAX);
b8b6a50b 6219
2e70f6ef
PB
6220 if (use_icount)
6221 gen_io_start();
b6abf97d 6222 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
b6abf97d 6223 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
a7812ae4 6224 gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
2e70f6ef
PB
6225 if (use_icount) {
6226 gen_io_end();
6227 gen_jmp(s, s->pc - s->cs_base);
6228 }
2c0262af
FB
6229 break;
6230
6231 /************************/
6232 /* control */
6233 case 0xc2: /* ret im */
0af10c86 6234 val = cpu_ldsw_code(env, s->pc);
2c0262af
FB
6235 s->pc += 2;
6236 gen_pop_T0(s);
8f091a59
FB
6237 if (CODE64(s) && s->dflag)
6238 s->dflag = 2;
2c0262af
FB
6239 gen_stack_update(s, val + (2 << s->dflag));
6240 if (s->dflag == 0)
6241 gen_op_andl_T0_ffff();
6242 gen_op_jmp_T0();
6243 gen_eob(s);
6244 break;
6245 case 0xc3: /* ret */
6246 gen_pop_T0(s);
6247 gen_pop_update(s);
6248 if (s->dflag == 0)
6249 gen_op_andl_T0_ffff();
6250 gen_op_jmp_T0();
6251 gen_eob(s);
6252 break;
6253 case 0xca: /* lret im */
0af10c86 6254 val = cpu_ldsw_code(env, s->pc);
2c0262af
FB
6255 s->pc += 2;
6256 do_lret:
6257 if (s->pe && !s->vm86) {
6258 if (s->cc_op != CC_OP_DYNAMIC)
6259 gen_op_set_cc_op(s->cc_op);
14ce26e7 6260 gen_jmp_im(pc_start - s->cs_base);
2999a0b2 6261 gen_helper_lret_protected(cpu_env, tcg_const_i32(s->dflag),
a7812ae4 6262 tcg_const_i32(val));
2c0262af
FB
6263 } else {
6264 gen_stack_A0(s);
6265 /* pop offset */
57fec1fe 6266 gen_op_ld_T0_A0(1 + s->dflag + s->mem_index);
2c0262af
FB
6267 if (s->dflag == 0)
6268 gen_op_andl_T0_ffff();
6269 /* NOTE: keeping EIP updated is not a problem in case of
6270 exception */
6271 gen_op_jmp_T0();
6272 /* pop selector */
6273 gen_op_addl_A0_im(2 << s->dflag);
57fec1fe 6274 gen_op_ld_T0_A0(1 + s->dflag + s->mem_index);
3bd7da9e 6275 gen_op_movl_seg_T0_vm(R_CS);
2c0262af
FB
6276 /* add stack offset */
6277 gen_stack_update(s, val + (4 << s->dflag));
6278 }
6279 gen_eob(s);
6280 break;
6281 case 0xcb: /* lret */
6282 val = 0;
6283 goto do_lret;
6284 case 0xcf: /* iret */
872929aa 6285 gen_svm_check_intercept(s, pc_start, SVM_EXIT_IRET);
2c0262af
FB
6286 if (!s->pe) {
6287 /* real mode */
2999a0b2 6288 gen_helper_iret_real(cpu_env, tcg_const_i32(s->dflag));
3ca51d07 6289 set_cc_op(s, CC_OP_EFLAGS);
f115e911
FB
6290 } else if (s->vm86) {
6291 if (s->iopl != 3) {
6292 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6293 } else {
2999a0b2 6294 gen_helper_iret_real(cpu_env, tcg_const_i32(s->dflag));
3ca51d07 6295 set_cc_op(s, CC_OP_EFLAGS);
f115e911 6296 }
2c0262af
FB
6297 } else {
6298 if (s->cc_op != CC_OP_DYNAMIC)
6299 gen_op_set_cc_op(s->cc_op);
14ce26e7 6300 gen_jmp_im(pc_start - s->cs_base);
2999a0b2 6301 gen_helper_iret_protected(cpu_env, tcg_const_i32(s->dflag),
a7812ae4 6302 tcg_const_i32(s->pc - s->cs_base));
3ca51d07 6303 set_cc_op(s, CC_OP_EFLAGS);
2c0262af
FB
6304 }
6305 gen_eob(s);
6306 break;
6307 case 0xe8: /* call im */
6308 {
14ce26e7 6309 if (dflag)
0af10c86 6310 tval = (int32_t)insn_get(env, s, OT_LONG);
14ce26e7 6311 else
0af10c86 6312 tval = (int16_t)insn_get(env, s, OT_WORD);
2c0262af 6313 next_eip = s->pc - s->cs_base;
14ce26e7 6314 tval += next_eip;
2c0262af 6315 if (s->dflag == 0)
14ce26e7 6316 tval &= 0xffff;
99596385
AJ
6317 else if(!CODE64(s))
6318 tval &= 0xffffffff;
14ce26e7 6319 gen_movtl_T0_im(next_eip);
2c0262af 6320 gen_push_T0(s);
14ce26e7 6321 gen_jmp(s, tval);
2c0262af
FB
6322 }
6323 break;
6324 case 0x9a: /* lcall im */
6325 {
6326 unsigned int selector, offset;
3b46e624 6327
14ce26e7
FB
6328 if (CODE64(s))
6329 goto illegal_op;
2c0262af 6330 ot = dflag ? OT_LONG : OT_WORD;
0af10c86
BS
6331 offset = insn_get(env, s, ot);
6332 selector = insn_get(env, s, OT_WORD);
3b46e624 6333
2c0262af 6334 gen_op_movl_T0_im(selector);
14ce26e7 6335 gen_op_movl_T1_imu(offset);
2c0262af
FB
6336 }
6337 goto do_lcall;
ecada8a2 6338 case 0xe9: /* jmp im */
14ce26e7 6339 if (dflag)
0af10c86 6340 tval = (int32_t)insn_get(env, s, OT_LONG);
14ce26e7 6341 else
0af10c86 6342 tval = (int16_t)insn_get(env, s, OT_WORD);
14ce26e7 6343 tval += s->pc - s->cs_base;
2c0262af 6344 if (s->dflag == 0)
14ce26e7 6345 tval &= 0xffff;
32938e12
AJ
6346 else if(!CODE64(s))
6347 tval &= 0xffffffff;
14ce26e7 6348 gen_jmp(s, tval);
2c0262af
FB
6349 break;
6350 case 0xea: /* ljmp im */
6351 {
6352 unsigned int selector, offset;
6353
14ce26e7
FB
6354 if (CODE64(s))
6355 goto illegal_op;
2c0262af 6356 ot = dflag ? OT_LONG : OT_WORD;
0af10c86
BS
6357 offset = insn_get(env, s, ot);
6358 selector = insn_get(env, s, OT_WORD);
3b46e624 6359
2c0262af 6360 gen_op_movl_T0_im(selector);
14ce26e7 6361 gen_op_movl_T1_imu(offset);
2c0262af
FB
6362 }
6363 goto do_ljmp;
6364 case 0xeb: /* jmp Jb */
0af10c86 6365 tval = (int8_t)insn_get(env, s, OT_BYTE);
14ce26e7 6366 tval += s->pc - s->cs_base;
2c0262af 6367 if (s->dflag == 0)
14ce26e7
FB
6368 tval &= 0xffff;
6369 gen_jmp(s, tval);
2c0262af
FB
6370 break;
6371 case 0x70 ... 0x7f: /* jcc Jb */
0af10c86 6372 tval = (int8_t)insn_get(env, s, OT_BYTE);
2c0262af
FB
6373 goto do_jcc;
6374 case 0x180 ... 0x18f: /* jcc Jv */
6375 if (dflag) {
0af10c86 6376 tval = (int32_t)insn_get(env, s, OT_LONG);
2c0262af 6377 } else {
0af10c86 6378 tval = (int16_t)insn_get(env, s, OT_WORD);
2c0262af
FB
6379 }
6380 do_jcc:
6381 next_eip = s->pc - s->cs_base;
14ce26e7 6382 tval += next_eip;
2c0262af 6383 if (s->dflag == 0)
14ce26e7
FB
6384 tval &= 0xffff;
6385 gen_jcc(s, b, tval, next_eip);
2c0262af
FB
6386 break;
6387
6388 case 0x190 ... 0x19f: /* setcc Gv */
0af10c86 6389 modrm = cpu_ldub_code(env, s->pc++);
2c0262af 6390 gen_setcc(s, b);
0af10c86 6391 gen_ldst_modrm(env, s, modrm, OT_BYTE, OR_TMP0, 1);
2c0262af
FB
6392 break;
6393 case 0x140 ... 0x14f: /* cmov Gv, Ev */
8e1c85e3
FB
6394 {
6395 int l1;
1e4840bf
FB
6396 TCGv t0;
6397
8e1c85e3 6398 ot = dflag + OT_WORD;
0af10c86 6399 modrm = cpu_ldub_code(env, s->pc++);
8e1c85e3
FB
6400 reg = ((modrm >> 3) & 7) | rex_r;
6401 mod = (modrm >> 6) & 3;
a7812ae4 6402 t0 = tcg_temp_local_new();
8e1c85e3 6403 if (mod != 3) {
0af10c86 6404 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
1e4840bf 6405 gen_op_ld_v(ot + s->mem_index, t0, cpu_A0);
8e1c85e3
FB
6406 } else {
6407 rm = (modrm & 7) | REX_B(s);
1e4840bf 6408 gen_op_mov_v_reg(ot, t0, rm);
8e1c85e3 6409 }
8e1c85e3
FB
6410#ifdef TARGET_X86_64
6411 if (ot == OT_LONG) {
6412 /* XXX: specific Intel behaviour ? */
6413 l1 = gen_new_label();
b27fc131 6414 gen_jcc1(s, b ^ 1, l1);
cc739bb0 6415 tcg_gen_mov_tl(cpu_regs[reg], t0);
8e1c85e3 6416 gen_set_label(l1);
cc739bb0 6417 tcg_gen_ext32u_tl(cpu_regs[reg], cpu_regs[reg]);
8e1c85e3
FB
6418 } else
6419#endif
6420 {
6421 l1 = gen_new_label();
b27fc131 6422 gen_jcc1(s, b ^ 1, l1);
1e4840bf 6423 gen_op_mov_reg_v(ot, reg, t0);
8e1c85e3
FB
6424 gen_set_label(l1);
6425 }
1e4840bf 6426 tcg_temp_free(t0);
2c0262af 6427 }
2c0262af 6428 break;
3b46e624 6429
2c0262af
FB
6430 /************************/
6431 /* flags */
6432 case 0x9c: /* pushf */
872929aa 6433 gen_svm_check_intercept(s, pc_start, SVM_EXIT_PUSHF);
2c0262af
FB
6434 if (s->vm86 && s->iopl != 3) {
6435 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6436 } else {
6437 if (s->cc_op != CC_OP_DYNAMIC)
6438 gen_op_set_cc_op(s->cc_op);
f0967a1a 6439 gen_helper_read_eflags(cpu_T[0], cpu_env);
2c0262af
FB
6440 gen_push_T0(s);
6441 }
6442 break;
6443 case 0x9d: /* popf */
872929aa 6444 gen_svm_check_intercept(s, pc_start, SVM_EXIT_POPF);
2c0262af
FB
6445 if (s->vm86 && s->iopl != 3) {
6446 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6447 } else {
6448 gen_pop_T0(s);
6449 if (s->cpl == 0) {
6450 if (s->dflag) {
f0967a1a
BS
6451 gen_helper_write_eflags(cpu_env, cpu_T[0],
6452 tcg_const_i32((TF_MASK | AC_MASK |
6453 ID_MASK | NT_MASK |
6454 IF_MASK |
6455 IOPL_MASK)));
2c0262af 6456 } else {
f0967a1a
BS
6457 gen_helper_write_eflags(cpu_env, cpu_T[0],
6458 tcg_const_i32((TF_MASK | AC_MASK |
6459 ID_MASK | NT_MASK |
6460 IF_MASK | IOPL_MASK)
6461 & 0xffff));
2c0262af
FB
6462 }
6463 } else {
4136f33c
FB
6464 if (s->cpl <= s->iopl) {
6465 if (s->dflag) {
f0967a1a
BS
6466 gen_helper_write_eflags(cpu_env, cpu_T[0],
6467 tcg_const_i32((TF_MASK |
6468 AC_MASK |
6469 ID_MASK |
6470 NT_MASK |
6471 IF_MASK)));
4136f33c 6472 } else {
f0967a1a
BS
6473 gen_helper_write_eflags(cpu_env, cpu_T[0],
6474 tcg_const_i32((TF_MASK |
6475 AC_MASK |
6476 ID_MASK |
6477 NT_MASK |
6478 IF_MASK)
6479 & 0xffff));
4136f33c 6480 }
2c0262af 6481 } else {
4136f33c 6482 if (s->dflag) {
f0967a1a
BS
6483 gen_helper_write_eflags(cpu_env, cpu_T[0],
6484 tcg_const_i32((TF_MASK | AC_MASK |
6485 ID_MASK | NT_MASK)));
4136f33c 6486 } else {
f0967a1a
BS
6487 gen_helper_write_eflags(cpu_env, cpu_T[0],
6488 tcg_const_i32((TF_MASK | AC_MASK |
6489 ID_MASK | NT_MASK)
6490 & 0xffff));
4136f33c 6491 }
2c0262af
FB
6492 }
6493 }
6494 gen_pop_update(s);
3ca51d07 6495 set_cc_op(s, CC_OP_EFLAGS);
a9321a4d 6496 /* abort translation because TF/AC flag may change */
14ce26e7 6497 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
6498 gen_eob(s);
6499 }
6500 break;
6501 case 0x9e: /* sahf */
12e26b75 6502 if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM))
14ce26e7 6503 goto illegal_op;
57fec1fe 6504 gen_op_mov_TN_reg(OT_BYTE, 0, R_AH);
c7b3c873 6505 gen_compute_eflags(s, cpu_cc_src);
bd7a7b33
FB
6506 tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, CC_O);
6507 tcg_gen_andi_tl(cpu_T[0], cpu_T[0], CC_S | CC_Z | CC_A | CC_P | CC_C);
6508 tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, cpu_T[0]);
2c0262af
FB
6509 break;
6510 case 0x9f: /* lahf */
12e26b75 6511 if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM))
14ce26e7 6512 goto illegal_op;
c7b3c873 6513 gen_compute_eflags(s, cpu_T[0]);
bd7a7b33
FB
6514 /* Note: gen_compute_eflags() only gives the condition codes */
6515 tcg_gen_ori_tl(cpu_T[0], cpu_T[0], 0x02);
57fec1fe 6516 gen_op_mov_reg_T0(OT_BYTE, R_AH);
2c0262af
FB
6517 break;
6518 case 0xf5: /* cmc */
c7b3c873 6519 gen_compute_eflags(s, cpu_cc_src);
bd7a7b33 6520 tcg_gen_xori_tl(cpu_cc_src, cpu_cc_src, CC_C);
2c0262af
FB
6521 break;
6522 case 0xf8: /* clc */
c7b3c873 6523 gen_compute_eflags(s, cpu_cc_src);
bd7a7b33 6524 tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~CC_C);
2c0262af
FB
6525 break;
6526 case 0xf9: /* stc */
c7b3c873 6527 gen_compute_eflags(s, cpu_cc_src);
bd7a7b33 6528 tcg_gen_ori_tl(cpu_cc_src, cpu_cc_src, CC_C);
2c0262af
FB
6529 break;
6530 case 0xfc: /* cld */
b6abf97d 6531 tcg_gen_movi_i32(cpu_tmp2_i32, 1);
317ac620 6532 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, offsetof(CPUX86State, df));
2c0262af
FB
6533 break;
6534 case 0xfd: /* std */
b6abf97d 6535 tcg_gen_movi_i32(cpu_tmp2_i32, -1);
317ac620 6536 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, offsetof(CPUX86State, df));
2c0262af
FB
6537 break;
6538
6539 /************************/
6540 /* bit operations */
6541 case 0x1ba: /* bt/bts/btr/btc Gv, im */
14ce26e7 6542 ot = dflag + OT_WORD;
0af10c86 6543 modrm = cpu_ldub_code(env, s->pc++);
33698e5f 6544 op = (modrm >> 3) & 7;
2c0262af 6545 mod = (modrm >> 6) & 3;
14ce26e7 6546 rm = (modrm & 7) | REX_B(s);
2c0262af 6547 if (mod != 3) {
14ce26e7 6548 s->rip_offset = 1;
0af10c86 6549 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
57fec1fe 6550 gen_op_ld_T0_A0(ot + s->mem_index);
2c0262af 6551 } else {
57fec1fe 6552 gen_op_mov_TN_reg(ot, 0, rm);
2c0262af
FB
6553 }
6554 /* load shift */
0af10c86 6555 val = cpu_ldub_code(env, s->pc++);
2c0262af
FB
6556 gen_op_movl_T1_im(val);
6557 if (op < 4)
6558 goto illegal_op;
6559 op -= 4;
f484d386 6560 goto bt_op;
2c0262af
FB
6561 case 0x1a3: /* bt Gv, Ev */
6562 op = 0;
6563 goto do_btx;
6564 case 0x1ab: /* bts */
6565 op = 1;
6566 goto do_btx;
6567 case 0x1b3: /* btr */
6568 op = 2;
6569 goto do_btx;
6570 case 0x1bb: /* btc */
6571 op = 3;
6572 do_btx:
14ce26e7 6573 ot = dflag + OT_WORD;
0af10c86 6574 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7 6575 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af 6576 mod = (modrm >> 6) & 3;
14ce26e7 6577 rm = (modrm & 7) | REX_B(s);
57fec1fe 6578 gen_op_mov_TN_reg(OT_LONG, 1, reg);
2c0262af 6579 if (mod != 3) {
0af10c86 6580 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
2c0262af 6581 /* specific case: we need to add a displacement */
f484d386
FB
6582 gen_exts(ot, cpu_T[1]);
6583 tcg_gen_sari_tl(cpu_tmp0, cpu_T[1], 3 + ot);
6584 tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, ot);
6585 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
57fec1fe 6586 gen_op_ld_T0_A0(ot + s->mem_index);
2c0262af 6587 } else {
57fec1fe 6588 gen_op_mov_TN_reg(ot, 0, rm);
2c0262af 6589 }
f484d386
FB
6590 bt_op:
6591 tcg_gen_andi_tl(cpu_T[1], cpu_T[1], (1 << (3 + ot)) - 1);
6592 switch(op) {
6593 case 0:
6594 tcg_gen_shr_tl(cpu_cc_src, cpu_T[0], cpu_T[1]);
6595 tcg_gen_movi_tl(cpu_cc_dst, 0);
6596 break;
6597 case 1:
6598 tcg_gen_shr_tl(cpu_tmp4, cpu_T[0], cpu_T[1]);
6599 tcg_gen_movi_tl(cpu_tmp0, 1);
6600 tcg_gen_shl_tl(cpu_tmp0, cpu_tmp0, cpu_T[1]);
6601 tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
6602 break;
6603 case 2:
6604 tcg_gen_shr_tl(cpu_tmp4, cpu_T[0], cpu_T[1]);
6605 tcg_gen_movi_tl(cpu_tmp0, 1);
6606 tcg_gen_shl_tl(cpu_tmp0, cpu_tmp0, cpu_T[1]);
6607 tcg_gen_not_tl(cpu_tmp0, cpu_tmp0);
6608 tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
6609 break;
6610 default:
6611 case 3:
6612 tcg_gen_shr_tl(cpu_tmp4, cpu_T[0], cpu_T[1]);
6613 tcg_gen_movi_tl(cpu_tmp0, 1);
6614 tcg_gen_shl_tl(cpu_tmp0, cpu_tmp0, cpu_T[1]);
6615 tcg_gen_xor_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
6616 break;
6617 }
3ca51d07 6618 set_cc_op(s, CC_OP_SARB + ot);
2c0262af
FB
6619 if (op != 0) {
6620 if (mod != 3)
57fec1fe 6621 gen_op_st_T0_A0(ot + s->mem_index);
2c0262af 6622 else
57fec1fe 6623 gen_op_mov_reg_T0(ot, rm);
f484d386
FB
6624 tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
6625 tcg_gen_movi_tl(cpu_cc_dst, 0);
2c0262af
FB
6626 }
6627 break;
6628 case 0x1bc: /* bsf */
6629 case 0x1bd: /* bsr */
6191b059
FB
6630 {
6631 int label1;
1e4840bf
FB
6632 TCGv t0;
6633
6191b059 6634 ot = dflag + OT_WORD;
0af10c86 6635 modrm = cpu_ldub_code(env, s->pc++);
6191b059 6636 reg = ((modrm >> 3) & 7) | rex_r;
0af10c86 6637 gen_ldst_modrm(env, s,modrm, ot, OR_TMP0, 0);
6191b059 6638 gen_extu(ot, cpu_T[0]);
a7812ae4 6639 t0 = tcg_temp_local_new();
1e4840bf 6640 tcg_gen_mov_tl(t0, cpu_T[0]);
31501a71
AP
6641 if ((b & 1) && (prefixes & PREFIX_REPZ) &&
6642 (s->cpuid_ext3_features & CPUID_EXT3_ABM)) {
6643 switch(ot) {
6644 case OT_WORD: gen_helper_lzcnt(cpu_T[0], t0,
6645 tcg_const_i32(16)); break;
6646 case OT_LONG: gen_helper_lzcnt(cpu_T[0], t0,
6647 tcg_const_i32(32)); break;
6648 case OT_QUAD: gen_helper_lzcnt(cpu_T[0], t0,
6649 tcg_const_i32(64)); break;
6650 }
6651 gen_op_mov_reg_T0(ot, reg);
6191b059 6652 } else {
31501a71
AP
6653 label1 = gen_new_label();
6654 tcg_gen_movi_tl(cpu_cc_dst, 0);
6655 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, label1);
6656 if (b & 1) {
6657 gen_helper_bsr(cpu_T[0], t0);
6658 } else {
6659 gen_helper_bsf(cpu_T[0], t0);
6660 }
6661 gen_op_mov_reg_T0(ot, reg);
6662 tcg_gen_movi_tl(cpu_cc_dst, 1);
6663 gen_set_label(label1);
6664 tcg_gen_discard_tl(cpu_cc_src);
3ca51d07 6665 set_cc_op(s, CC_OP_LOGICB + ot);
6191b059 6666 }
1e4840bf 6667 tcg_temp_free(t0);
6191b059 6668 }
2c0262af
FB
6669 break;
6670 /************************/
6671 /* bcd */
6672 case 0x27: /* daa */
14ce26e7
FB
6673 if (CODE64(s))
6674 goto illegal_op;
2c0262af
FB
6675 if (s->cc_op != CC_OP_DYNAMIC)
6676 gen_op_set_cc_op(s->cc_op);
7923057b 6677 gen_helper_daa(cpu_env);
3ca51d07 6678 set_cc_op(s, CC_OP_EFLAGS);
2c0262af
FB
6679 break;
6680 case 0x2f: /* das */
14ce26e7
FB
6681 if (CODE64(s))
6682 goto illegal_op;
2c0262af
FB
6683 if (s->cc_op != CC_OP_DYNAMIC)
6684 gen_op_set_cc_op(s->cc_op);
7923057b 6685 gen_helper_das(cpu_env);
3ca51d07 6686 set_cc_op(s, CC_OP_EFLAGS);
2c0262af
FB
6687 break;
6688 case 0x37: /* aaa */
14ce26e7
FB
6689 if (CODE64(s))
6690 goto illegal_op;
2c0262af
FB
6691 if (s->cc_op != CC_OP_DYNAMIC)
6692 gen_op_set_cc_op(s->cc_op);
7923057b 6693 gen_helper_aaa(cpu_env);
3ca51d07 6694 set_cc_op(s, CC_OP_EFLAGS);
2c0262af
FB
6695 break;
6696 case 0x3f: /* aas */
14ce26e7
FB
6697 if (CODE64(s))
6698 goto illegal_op;
2c0262af
FB
6699 if (s->cc_op != CC_OP_DYNAMIC)
6700 gen_op_set_cc_op(s->cc_op);
7923057b 6701 gen_helper_aas(cpu_env);
3ca51d07 6702 set_cc_op(s, CC_OP_EFLAGS);
2c0262af
FB
6703 break;
6704 case 0xd4: /* aam */
14ce26e7
FB
6705 if (CODE64(s))
6706 goto illegal_op;
0af10c86 6707 val = cpu_ldub_code(env, s->pc++);
b6d7c3db
TS
6708 if (val == 0) {
6709 gen_exception(s, EXCP00_DIVZ, pc_start - s->cs_base);
6710 } else {
7923057b 6711 gen_helper_aam(cpu_env, tcg_const_i32(val));
3ca51d07 6712 set_cc_op(s, CC_OP_LOGICB);
b6d7c3db 6713 }
2c0262af
FB
6714 break;
6715 case 0xd5: /* aad */
14ce26e7
FB
6716 if (CODE64(s))
6717 goto illegal_op;
0af10c86 6718 val = cpu_ldub_code(env, s->pc++);
7923057b 6719 gen_helper_aad(cpu_env, tcg_const_i32(val));
3ca51d07 6720 set_cc_op(s, CC_OP_LOGICB);
2c0262af
FB
6721 break;
6722 /************************/
6723 /* misc */
6724 case 0x90: /* nop */
ab1f142b 6725 /* XXX: correct lock test for all insn */
7418027e 6726 if (prefixes & PREFIX_LOCK) {
ab1f142b 6727 goto illegal_op;
7418027e
RH
6728 }
6729 /* If REX_B is set, then this is xchg eax, r8d, not a nop. */
6730 if (REX_B(s)) {
6731 goto do_xchg_reg_eax;
6732 }
0573fbfc
TS
6733 if (prefixes & PREFIX_REPZ) {
6734 gen_svm_check_intercept(s, pc_start, SVM_EXIT_PAUSE);
6735 }
2c0262af
FB
6736 break;
6737 case 0x9b: /* fwait */
5fafdf24 6738 if ((s->flags & (HF_MP_MASK | HF_TS_MASK)) ==
7eee2a50
FB
6739 (HF_MP_MASK | HF_TS_MASK)) {
6740 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
2ee73ac3
FB
6741 } else {
6742 if (s->cc_op != CC_OP_DYNAMIC)
6743 gen_op_set_cc_op(s->cc_op);
14ce26e7 6744 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae 6745 gen_helper_fwait(cpu_env);
7eee2a50 6746 }
2c0262af
FB
6747 break;
6748 case 0xcc: /* int3 */
6749 gen_interrupt(s, EXCP03_INT3, pc_start - s->cs_base, s->pc - s->cs_base);
6750 break;
6751 case 0xcd: /* int N */
0af10c86 6752 val = cpu_ldub_code(env, s->pc++);
f115e911 6753 if (s->vm86 && s->iopl != 3) {
5fafdf24 6754 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
f115e911
FB
6755 } else {
6756 gen_interrupt(s, val, pc_start - s->cs_base, s->pc - s->cs_base);
6757 }
2c0262af
FB
6758 break;
6759 case 0xce: /* into */
14ce26e7
FB
6760 if (CODE64(s))
6761 goto illegal_op;
2c0262af
FB
6762 if (s->cc_op != CC_OP_DYNAMIC)
6763 gen_op_set_cc_op(s->cc_op);
a8ede8ba 6764 gen_jmp_im(pc_start - s->cs_base);
4a7443be 6765 gen_helper_into(cpu_env, tcg_const_i32(s->pc - pc_start));
2c0262af 6766 break;
0b97134b 6767#ifdef WANT_ICEBP
2c0262af 6768 case 0xf1: /* icebp (undocumented, exits to external debugger) */
872929aa 6769 gen_svm_check_intercept(s, pc_start, SVM_EXIT_ICEBP);
aba9d61e 6770#if 1
2c0262af 6771 gen_debug(s, pc_start - s->cs_base);
aba9d61e
FB
6772#else
6773 /* start debug */
0af10c86 6774 tb_flush(env);
24537a01 6775 qemu_set_log(CPU_LOG_INT | CPU_LOG_TB_IN_ASM);
aba9d61e 6776#endif
2c0262af 6777 break;
0b97134b 6778#endif
2c0262af
FB
6779 case 0xfa: /* cli */
6780 if (!s->vm86) {
6781 if (s->cpl <= s->iopl) {
f0967a1a 6782 gen_helper_cli(cpu_env);
2c0262af
FB
6783 } else {
6784 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6785 }
6786 } else {
6787 if (s->iopl == 3) {
f0967a1a 6788 gen_helper_cli(cpu_env);
2c0262af
FB
6789 } else {
6790 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6791 }
6792 }
6793 break;
6794 case 0xfb: /* sti */
6795 if (!s->vm86) {
6796 if (s->cpl <= s->iopl) {
6797 gen_sti:
f0967a1a 6798 gen_helper_sti(cpu_env);
2c0262af 6799 /* interruptions are enabled only the first insn after sti */
a2cc3b24
FB
6800 /* If several instructions disable interrupts, only the
6801 _first_ does it */
6802 if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
f0967a1a 6803 gen_helper_set_inhibit_irq(cpu_env);
2c0262af 6804 /* give a chance to handle pending irqs */
14ce26e7 6805 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
6806 gen_eob(s);
6807 } else {
6808 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6809 }
6810 } else {
6811 if (s->iopl == 3) {
6812 goto gen_sti;
6813 } else {
6814 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6815 }
6816 }
6817 break;
6818 case 0x62: /* bound */
14ce26e7
FB
6819 if (CODE64(s))
6820 goto illegal_op;
2c0262af 6821 ot = dflag ? OT_LONG : OT_WORD;
0af10c86 6822 modrm = cpu_ldub_code(env, s->pc++);
2c0262af
FB
6823 reg = (modrm >> 3) & 7;
6824 mod = (modrm >> 6) & 3;
6825 if (mod == 3)
6826 goto illegal_op;
57fec1fe 6827 gen_op_mov_TN_reg(ot, 0, reg);
0af10c86 6828 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
14ce26e7 6829 gen_jmp_im(pc_start - s->cs_base);
b6abf97d 6830 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
92fc4b58
BS
6831 if (ot == OT_WORD) {
6832 gen_helper_boundw(cpu_env, cpu_A0, cpu_tmp2_i32);
6833 } else {
6834 gen_helper_boundl(cpu_env, cpu_A0, cpu_tmp2_i32);
6835 }
2c0262af
FB
6836 break;
6837 case 0x1c8 ... 0x1cf: /* bswap reg */
14ce26e7
FB
6838 reg = (b & 7) | REX_B(s);
6839#ifdef TARGET_X86_64
6840 if (dflag == 2) {
57fec1fe 6841 gen_op_mov_TN_reg(OT_QUAD, 0, reg);
66896cb8 6842 tcg_gen_bswap64_i64(cpu_T[0], cpu_T[0]);
57fec1fe 6843 gen_op_mov_reg_T0(OT_QUAD, reg);
5fafdf24 6844 } else
8777643e 6845#endif
57fec1fe
FB
6846 {
6847 gen_op_mov_TN_reg(OT_LONG, 0, reg);
8777643e
AJ
6848 tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
6849 tcg_gen_bswap32_tl(cpu_T[0], cpu_T[0]);
57fec1fe 6850 gen_op_mov_reg_T0(OT_LONG, reg);
14ce26e7 6851 }
2c0262af
FB
6852 break;
6853 case 0xd6: /* salc */
14ce26e7
FB
6854 if (CODE64(s))
6855 goto illegal_op;
c7b3c873 6856 gen_compute_eflags_c(s, cpu_T[0]);
bd7a7b33
FB
6857 tcg_gen_neg_tl(cpu_T[0], cpu_T[0]);
6858 gen_op_mov_reg_T0(OT_BYTE, R_EAX);
2c0262af
FB
6859 break;
6860 case 0xe0: /* loopnz */
6861 case 0xe1: /* loopz */
2c0262af
FB
6862 case 0xe2: /* loop */
6863 case 0xe3: /* jecxz */
14ce26e7 6864 {
6e0d8677 6865 int l1, l2, l3;
14ce26e7 6866
0af10c86 6867 tval = (int8_t)insn_get(env, s, OT_BYTE);
14ce26e7
FB
6868 next_eip = s->pc - s->cs_base;
6869 tval += next_eip;
6870 if (s->dflag == 0)
6871 tval &= 0xffff;
3b46e624 6872
14ce26e7
FB
6873 l1 = gen_new_label();
6874 l2 = gen_new_label();
6e0d8677 6875 l3 = gen_new_label();
14ce26e7 6876 b &= 3;
6e0d8677
FB
6877 switch(b) {
6878 case 0: /* loopnz */
6879 case 1: /* loopz */
6e0d8677
FB
6880 gen_op_add_reg_im(s->aflag, R_ECX, -1);
6881 gen_op_jz_ecx(s->aflag, l3);
5bdb91b0 6882 gen_jcc1(s, (JCC_Z << 1) | (b ^ 1), l1);
6e0d8677
FB
6883 break;
6884 case 2: /* loop */
6885 gen_op_add_reg_im(s->aflag, R_ECX, -1);
6886 gen_op_jnz_ecx(s->aflag, l1);
6887 break;
6888 default:
6889 case 3: /* jcxz */
6890 gen_op_jz_ecx(s->aflag, l1);
6891 break;
14ce26e7
FB
6892 }
6893
6e0d8677 6894 gen_set_label(l3);
14ce26e7 6895 gen_jmp_im(next_eip);
8e1c85e3 6896 tcg_gen_br(l2);
6e0d8677 6897
14ce26e7
FB
6898 gen_set_label(l1);
6899 gen_jmp_im(tval);
6900 gen_set_label(l2);
6901 gen_eob(s);
6902 }
2c0262af
FB
6903 break;
6904 case 0x130: /* wrmsr */
6905 case 0x132: /* rdmsr */
6906 if (s->cpl != 0) {
6907 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6908 } else {
872929aa
FB
6909 if (s->cc_op != CC_OP_DYNAMIC)
6910 gen_op_set_cc_op(s->cc_op);
6911 gen_jmp_im(pc_start - s->cs_base);
0573fbfc 6912 if (b & 2) {
4a7443be 6913 gen_helper_rdmsr(cpu_env);
0573fbfc 6914 } else {
4a7443be 6915 gen_helper_wrmsr(cpu_env);
0573fbfc 6916 }
2c0262af
FB
6917 }
6918 break;
6919 case 0x131: /* rdtsc */
872929aa
FB
6920 if (s->cc_op != CC_OP_DYNAMIC)
6921 gen_op_set_cc_op(s->cc_op);
ecada8a2 6922 gen_jmp_im(pc_start - s->cs_base);
efade670
PB
6923 if (use_icount)
6924 gen_io_start();
4a7443be 6925 gen_helper_rdtsc(cpu_env);
efade670
PB
6926 if (use_icount) {
6927 gen_io_end();
6928 gen_jmp(s, s->pc - s->cs_base);
6929 }
2c0262af 6930 break;
df01e0fc 6931 case 0x133: /* rdpmc */
872929aa
FB
6932 if (s->cc_op != CC_OP_DYNAMIC)
6933 gen_op_set_cc_op(s->cc_op);
df01e0fc 6934 gen_jmp_im(pc_start - s->cs_base);
4a7443be 6935 gen_helper_rdpmc(cpu_env);
df01e0fc 6936 break;
023fe10d 6937 case 0x134: /* sysenter */
2436b61a 6938 /* For Intel SYSENTER is valid on 64-bit */
0af10c86 6939 if (CODE64(s) && env->cpuid_vendor1 != CPUID_VENDOR_INTEL_1)
14ce26e7 6940 goto illegal_op;
023fe10d
FB
6941 if (!s->pe) {
6942 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6943 } else {
728d803b 6944 gen_update_cc_op(s);
14ce26e7 6945 gen_jmp_im(pc_start - s->cs_base);
2999a0b2 6946 gen_helper_sysenter(cpu_env);
023fe10d
FB
6947 gen_eob(s);
6948 }
6949 break;
6950 case 0x135: /* sysexit */
2436b61a 6951 /* For Intel SYSEXIT is valid on 64-bit */
0af10c86 6952 if (CODE64(s) && env->cpuid_vendor1 != CPUID_VENDOR_INTEL_1)
14ce26e7 6953 goto illegal_op;
023fe10d
FB
6954 if (!s->pe) {
6955 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6956 } else {
728d803b 6957 gen_update_cc_op(s);
14ce26e7 6958 gen_jmp_im(pc_start - s->cs_base);
2999a0b2 6959 gen_helper_sysexit(cpu_env, tcg_const_i32(dflag));
023fe10d
FB
6960 gen_eob(s);
6961 }
6962 break;
14ce26e7
FB
6963#ifdef TARGET_X86_64
6964 case 0x105: /* syscall */
6965 /* XXX: is it usable in real mode ? */
728d803b 6966 gen_update_cc_op(s);
14ce26e7 6967 gen_jmp_im(pc_start - s->cs_base);
2999a0b2 6968 gen_helper_syscall(cpu_env, tcg_const_i32(s->pc - pc_start));
14ce26e7
FB
6969 gen_eob(s);
6970 break;
6971 case 0x107: /* sysret */
6972 if (!s->pe) {
6973 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6974 } else {
728d803b 6975 gen_update_cc_op(s);
14ce26e7 6976 gen_jmp_im(pc_start - s->cs_base);
2999a0b2 6977 gen_helper_sysret(cpu_env, tcg_const_i32(s->dflag));
aba9d61e 6978 /* condition codes are modified only in long mode */
3ca51d07
RH
6979 if (s->lma) {
6980 set_cc_op(s, CC_OP_EFLAGS);
6981 }
14ce26e7
FB
6982 gen_eob(s);
6983 }
6984 break;
6985#endif
2c0262af 6986 case 0x1a2: /* cpuid */
9575cb94
FB
6987 if (s->cc_op != CC_OP_DYNAMIC)
6988 gen_op_set_cc_op(s->cc_op);
6989 gen_jmp_im(pc_start - s->cs_base);
4a7443be 6990 gen_helper_cpuid(cpu_env);
2c0262af
FB
6991 break;
6992 case 0xf4: /* hlt */
6993 if (s->cpl != 0) {
6994 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6995 } else {
6996 if (s->cc_op != CC_OP_DYNAMIC)
6997 gen_op_set_cc_op(s->cc_op);
94451178 6998 gen_jmp_im(pc_start - s->cs_base);
4a7443be 6999 gen_helper_hlt(cpu_env, tcg_const_i32(s->pc - pc_start));
5779406a 7000 s->is_jmp = DISAS_TB_JUMP;
2c0262af
FB
7001 }
7002 break;
7003 case 0x100:
0af10c86 7004 modrm = cpu_ldub_code(env, s->pc++);
2c0262af
FB
7005 mod = (modrm >> 6) & 3;
7006 op = (modrm >> 3) & 7;
7007 switch(op) {
7008 case 0: /* sldt */
f115e911
FB
7009 if (!s->pe || s->vm86)
7010 goto illegal_op;
872929aa 7011 gen_svm_check_intercept(s, pc_start, SVM_EXIT_LDTR_READ);
651ba608 7012 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,ldt.selector));
2c0262af
FB
7013 ot = OT_WORD;
7014 if (mod == 3)
7015 ot += s->dflag;
0af10c86 7016 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
2c0262af
FB
7017 break;
7018 case 2: /* lldt */
f115e911
FB
7019 if (!s->pe || s->vm86)
7020 goto illegal_op;
2c0262af
FB
7021 if (s->cpl != 0) {
7022 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7023 } else {
872929aa 7024 gen_svm_check_intercept(s, pc_start, SVM_EXIT_LDTR_WRITE);
0af10c86 7025 gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
14ce26e7 7026 gen_jmp_im(pc_start - s->cs_base);
b6abf97d 7027 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
2999a0b2 7028 gen_helper_lldt(cpu_env, cpu_tmp2_i32);
2c0262af
FB
7029 }
7030 break;
7031 case 1: /* str */
f115e911
FB
7032 if (!s->pe || s->vm86)
7033 goto illegal_op;
872929aa 7034 gen_svm_check_intercept(s, pc_start, SVM_EXIT_TR_READ);
651ba608 7035 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,tr.selector));
2c0262af
FB
7036 ot = OT_WORD;
7037 if (mod == 3)
7038 ot += s->dflag;
0af10c86 7039 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
2c0262af
FB
7040 break;
7041 case 3: /* ltr */
f115e911
FB
7042 if (!s->pe || s->vm86)
7043 goto illegal_op;
2c0262af
FB
7044 if (s->cpl != 0) {
7045 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7046 } else {
872929aa 7047 gen_svm_check_intercept(s, pc_start, SVM_EXIT_TR_WRITE);
0af10c86 7048 gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
14ce26e7 7049 gen_jmp_im(pc_start - s->cs_base);
b6abf97d 7050 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
2999a0b2 7051 gen_helper_ltr(cpu_env, cpu_tmp2_i32);
2c0262af
FB
7052 }
7053 break;
7054 case 4: /* verr */
7055 case 5: /* verw */
f115e911
FB
7056 if (!s->pe || s->vm86)
7057 goto illegal_op;
0af10c86 7058 gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
f115e911
FB
7059 if (s->cc_op != CC_OP_DYNAMIC)
7060 gen_op_set_cc_op(s->cc_op);
2999a0b2
BS
7061 if (op == 4) {
7062 gen_helper_verr(cpu_env, cpu_T[0]);
7063 } else {
7064 gen_helper_verw(cpu_env, cpu_T[0]);
7065 }
3ca51d07 7066 set_cc_op(s, CC_OP_EFLAGS);
f115e911 7067 break;
2c0262af
FB
7068 default:
7069 goto illegal_op;
7070 }
7071 break;
7072 case 0x101:
0af10c86 7073 modrm = cpu_ldub_code(env, s->pc++);
2c0262af
FB
7074 mod = (modrm >> 6) & 3;
7075 op = (modrm >> 3) & 7;
3d7374c5 7076 rm = modrm & 7;
2c0262af
FB
7077 switch(op) {
7078 case 0: /* sgdt */
2c0262af
FB
7079 if (mod == 3)
7080 goto illegal_op;
872929aa 7081 gen_svm_check_intercept(s, pc_start, SVM_EXIT_GDTR_READ);
0af10c86 7082 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
651ba608 7083 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, gdt.limit));
57fec1fe 7084 gen_op_st_T0_A0(OT_WORD + s->mem_index);
aba9d61e 7085 gen_add_A0_im(s, 2);
651ba608 7086 tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, gdt.base));
2c0262af
FB
7087 if (!s->dflag)
7088 gen_op_andl_T0_im(0xffffff);
57fec1fe 7089 gen_op_st_T0_A0(CODE64(s) + OT_LONG + s->mem_index);
2c0262af 7090 break;
3d7374c5
FB
7091 case 1:
7092 if (mod == 3) {
7093 switch (rm) {
7094 case 0: /* monitor */
7095 if (!(s->cpuid_ext_features & CPUID_EXT_MONITOR) ||
7096 s->cpl != 0)
7097 goto illegal_op;
94451178
FB
7098 if (s->cc_op != CC_OP_DYNAMIC)
7099 gen_op_set_cc_op(s->cc_op);
3d7374c5
FB
7100 gen_jmp_im(pc_start - s->cs_base);
7101#ifdef TARGET_X86_64
7102 if (s->aflag == 2) {
bbf662ee 7103 gen_op_movq_A0_reg(R_EAX);
5fafdf24 7104 } else
3d7374c5
FB
7105#endif
7106 {
bbf662ee 7107 gen_op_movl_A0_reg(R_EAX);
3d7374c5
FB
7108 if (s->aflag == 0)
7109 gen_op_andl_A0_ffff();
7110 }
7111 gen_add_A0_ds_seg(s);
4a7443be 7112 gen_helper_monitor(cpu_env, cpu_A0);
3d7374c5
FB
7113 break;
7114 case 1: /* mwait */
7115 if (!(s->cpuid_ext_features & CPUID_EXT_MONITOR) ||
7116 s->cpl != 0)
7117 goto illegal_op;
728d803b 7118 gen_update_cc_op(s);
94451178 7119 gen_jmp_im(pc_start - s->cs_base);
4a7443be 7120 gen_helper_mwait(cpu_env, tcg_const_i32(s->pc - pc_start));
3d7374c5
FB
7121 gen_eob(s);
7122 break;
a9321a4d
PA
7123 case 2: /* clac */
7124 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_SMAP) ||
7125 s->cpl != 0) {
7126 goto illegal_op;
7127 }
7128 gen_helper_clac(cpu_env);
7129 gen_jmp_im(s->pc - s->cs_base);
7130 gen_eob(s);
7131 break;
7132 case 3: /* stac */
7133 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_SMAP) ||
7134 s->cpl != 0) {
7135 goto illegal_op;
7136 }
7137 gen_helper_stac(cpu_env);
7138 gen_jmp_im(s->pc - s->cs_base);
7139 gen_eob(s);
7140 break;
3d7374c5
FB
7141 default:
7142 goto illegal_op;
7143 }
7144 } else { /* sidt */
872929aa 7145 gen_svm_check_intercept(s, pc_start, SVM_EXIT_IDTR_READ);
0af10c86 7146 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
651ba608 7147 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, idt.limit));
57fec1fe 7148 gen_op_st_T0_A0(OT_WORD + s->mem_index);
3d7374c5 7149 gen_add_A0_im(s, 2);
651ba608 7150 tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, idt.base));
3d7374c5
FB
7151 if (!s->dflag)
7152 gen_op_andl_T0_im(0xffffff);
57fec1fe 7153 gen_op_st_T0_A0(CODE64(s) + OT_LONG + s->mem_index);
3d7374c5
FB
7154 }
7155 break;
2c0262af
FB
7156 case 2: /* lgdt */
7157 case 3: /* lidt */
0573fbfc 7158 if (mod == 3) {
872929aa
FB
7159 if (s->cc_op != CC_OP_DYNAMIC)
7160 gen_op_set_cc_op(s->cc_op);
7161 gen_jmp_im(pc_start - s->cs_base);
0573fbfc
TS
7162 switch(rm) {
7163 case 0: /* VMRUN */
872929aa
FB
7164 if (!(s->flags & HF_SVME_MASK) || !s->pe)
7165 goto illegal_op;
7166 if (s->cpl != 0) {
7167 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
0573fbfc 7168 break;
872929aa 7169 } else {
052e80d5 7170 gen_helper_vmrun(cpu_env, tcg_const_i32(s->aflag),
a7812ae4 7171 tcg_const_i32(s->pc - pc_start));
db620f46 7172 tcg_gen_exit_tb(0);
5779406a 7173 s->is_jmp = DISAS_TB_JUMP;
872929aa 7174 }
0573fbfc
TS
7175 break;
7176 case 1: /* VMMCALL */
872929aa
FB
7177 if (!(s->flags & HF_SVME_MASK))
7178 goto illegal_op;
052e80d5 7179 gen_helper_vmmcall(cpu_env);
0573fbfc
TS
7180 break;
7181 case 2: /* VMLOAD */
872929aa
FB
7182 if (!(s->flags & HF_SVME_MASK) || !s->pe)
7183 goto illegal_op;
7184 if (s->cpl != 0) {
7185 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7186 break;
7187 } else {
052e80d5 7188 gen_helper_vmload(cpu_env, tcg_const_i32(s->aflag));
872929aa 7189 }
0573fbfc
TS
7190 break;
7191 case 3: /* VMSAVE */
872929aa
FB
7192 if (!(s->flags & HF_SVME_MASK) || !s->pe)
7193 goto illegal_op;
7194 if (s->cpl != 0) {
7195 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7196 break;
7197 } else {
052e80d5 7198 gen_helper_vmsave(cpu_env, tcg_const_i32(s->aflag));
872929aa 7199 }
0573fbfc
TS
7200 break;
7201 case 4: /* STGI */
872929aa
FB
7202 if ((!(s->flags & HF_SVME_MASK) &&
7203 !(s->cpuid_ext3_features & CPUID_EXT3_SKINIT)) ||
7204 !s->pe)
7205 goto illegal_op;
7206 if (s->cpl != 0) {
7207 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7208 break;
7209 } else {
052e80d5 7210 gen_helper_stgi(cpu_env);
872929aa 7211 }
0573fbfc
TS
7212 break;
7213 case 5: /* CLGI */
872929aa
FB
7214 if (!(s->flags & HF_SVME_MASK) || !s->pe)
7215 goto illegal_op;
7216 if (s->cpl != 0) {
7217 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7218 break;
7219 } else {
052e80d5 7220 gen_helper_clgi(cpu_env);
872929aa 7221 }
0573fbfc
TS
7222 break;
7223 case 6: /* SKINIT */
872929aa
FB
7224 if ((!(s->flags & HF_SVME_MASK) &&
7225 !(s->cpuid_ext3_features & CPUID_EXT3_SKINIT)) ||
7226 !s->pe)
7227 goto illegal_op;
052e80d5 7228 gen_helper_skinit(cpu_env);
0573fbfc
TS
7229 break;
7230 case 7: /* INVLPGA */
872929aa
FB
7231 if (!(s->flags & HF_SVME_MASK) || !s->pe)
7232 goto illegal_op;
7233 if (s->cpl != 0) {
7234 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7235 break;
7236 } else {
052e80d5 7237 gen_helper_invlpga(cpu_env, tcg_const_i32(s->aflag));
872929aa 7238 }
0573fbfc
TS
7239 break;
7240 default:
7241 goto illegal_op;
7242 }
7243 } else if (s->cpl != 0) {
2c0262af
FB
7244 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7245 } else {
872929aa
FB
7246 gen_svm_check_intercept(s, pc_start,
7247 op==2 ? SVM_EXIT_GDTR_WRITE : SVM_EXIT_IDTR_WRITE);
0af10c86 7248 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
57fec1fe 7249 gen_op_ld_T1_A0(OT_WORD + s->mem_index);
aba9d61e 7250 gen_add_A0_im(s, 2);
57fec1fe 7251 gen_op_ld_T0_A0(CODE64(s) + OT_LONG + s->mem_index);
2c0262af
FB
7252 if (!s->dflag)
7253 gen_op_andl_T0_im(0xffffff);
7254 if (op == 2) {
651ba608
FB
7255 tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,gdt.base));
7256 tcg_gen_st32_tl(cpu_T[1], cpu_env, offsetof(CPUX86State,gdt.limit));
2c0262af 7257 } else {
651ba608
FB
7258 tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,idt.base));
7259 tcg_gen_st32_tl(cpu_T[1], cpu_env, offsetof(CPUX86State,idt.limit));
2c0262af
FB
7260 }
7261 }
7262 break;
7263 case 4: /* smsw */
872929aa 7264 gen_svm_check_intercept(s, pc_start, SVM_EXIT_READ_CR0);
e2542fe2 7265#if defined TARGET_X86_64 && defined HOST_WORDS_BIGENDIAN
f60d2728 7266 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,cr[0]) + 4);
7267#else
651ba608 7268 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,cr[0]));
f60d2728 7269#endif
0af10c86 7270 gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 1);
2c0262af
FB
7271 break;
7272 case 6: /* lmsw */
7273 if (s->cpl != 0) {
7274 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7275 } else {
872929aa 7276 gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_CR0);
0af10c86 7277 gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
4a7443be 7278 gen_helper_lmsw(cpu_env, cpu_T[0]);
14ce26e7 7279 gen_jmp_im(s->pc - s->cs_base);
d71b9a8b 7280 gen_eob(s);
2c0262af
FB
7281 }
7282 break;
1b050077
AP
7283 case 7:
7284 if (mod != 3) { /* invlpg */
7285 if (s->cpl != 0) {
7286 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7287 } else {
7288 if (s->cc_op != CC_OP_DYNAMIC)
7289 gen_op_set_cc_op(s->cc_op);
7290 gen_jmp_im(pc_start - s->cs_base);
0af10c86 7291 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
4a7443be 7292 gen_helper_invlpg(cpu_env, cpu_A0);
1b050077
AP
7293 gen_jmp_im(s->pc - s->cs_base);
7294 gen_eob(s);
7295 }
2c0262af 7296 } else {
1b050077
AP
7297 switch (rm) {
7298 case 0: /* swapgs */
14ce26e7 7299#ifdef TARGET_X86_64
1b050077
AP
7300 if (CODE64(s)) {
7301 if (s->cpl != 0) {
7302 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7303 } else {
7304 tcg_gen_ld_tl(cpu_T[0], cpu_env,
7305 offsetof(CPUX86State,segs[R_GS].base));
7306 tcg_gen_ld_tl(cpu_T[1], cpu_env,
7307 offsetof(CPUX86State,kernelgsbase));
7308 tcg_gen_st_tl(cpu_T[1], cpu_env,
7309 offsetof(CPUX86State,segs[R_GS].base));
7310 tcg_gen_st_tl(cpu_T[0], cpu_env,
7311 offsetof(CPUX86State,kernelgsbase));
7312 }
5fafdf24 7313 } else
14ce26e7
FB
7314#endif
7315 {
7316 goto illegal_op;
7317 }
1b050077
AP
7318 break;
7319 case 1: /* rdtscp */
7320 if (!(s->cpuid_ext2_features & CPUID_EXT2_RDTSCP))
7321 goto illegal_op;
9575cb94
FB
7322 if (s->cc_op != CC_OP_DYNAMIC)
7323 gen_op_set_cc_op(s->cc_op);
7324 gen_jmp_im(pc_start - s->cs_base);
1b050077
AP
7325 if (use_icount)
7326 gen_io_start();
4a7443be 7327 gen_helper_rdtscp(cpu_env);
1b050077
AP
7328 if (use_icount) {
7329 gen_io_end();
7330 gen_jmp(s, s->pc - s->cs_base);
7331 }
7332 break;
7333 default:
7334 goto illegal_op;
14ce26e7 7335 }
2c0262af
FB
7336 }
7337 break;
7338 default:
7339 goto illegal_op;
7340 }
7341 break;
3415a4dd
FB
7342 case 0x108: /* invd */
7343 case 0x109: /* wbinvd */
7344 if (s->cpl != 0) {
7345 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7346 } else {
872929aa 7347 gen_svm_check_intercept(s, pc_start, (b & 2) ? SVM_EXIT_INVD : SVM_EXIT_WBINVD);
3415a4dd
FB
7348 /* nothing to do */
7349 }
7350 break;
14ce26e7
FB
7351 case 0x63: /* arpl or movslS (x86_64) */
7352#ifdef TARGET_X86_64
7353 if (CODE64(s)) {
7354 int d_ot;
7355 /* d_ot is the size of destination */
7356 d_ot = dflag + OT_WORD;
7357
0af10c86 7358 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7
FB
7359 reg = ((modrm >> 3) & 7) | rex_r;
7360 mod = (modrm >> 6) & 3;
7361 rm = (modrm & 7) | REX_B(s);
3b46e624 7362
14ce26e7 7363 if (mod == 3) {
57fec1fe 7364 gen_op_mov_TN_reg(OT_LONG, 0, rm);
14ce26e7
FB
7365 /* sign extend */
7366 if (d_ot == OT_QUAD)
e108dd01 7367 tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
57fec1fe 7368 gen_op_mov_reg_T0(d_ot, reg);
14ce26e7 7369 } else {
0af10c86 7370 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
14ce26e7 7371 if (d_ot == OT_QUAD) {
57fec1fe 7372 gen_op_lds_T0_A0(OT_LONG + s->mem_index);
14ce26e7 7373 } else {
57fec1fe 7374 gen_op_ld_T0_A0(OT_LONG + s->mem_index);
14ce26e7 7375 }
57fec1fe 7376 gen_op_mov_reg_T0(d_ot, reg);
14ce26e7 7377 }
5fafdf24 7378 } else
14ce26e7
FB
7379#endif
7380 {
3bd7da9e 7381 int label1;
49d9fdcc 7382 TCGv t0, t1, t2, a0;
1e4840bf 7383
14ce26e7
FB
7384 if (!s->pe || s->vm86)
7385 goto illegal_op;
a7812ae4
PB
7386 t0 = tcg_temp_local_new();
7387 t1 = tcg_temp_local_new();
7388 t2 = tcg_temp_local_new();
3bd7da9e 7389 ot = OT_WORD;
0af10c86 7390 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7
FB
7391 reg = (modrm >> 3) & 7;
7392 mod = (modrm >> 6) & 3;
7393 rm = modrm & 7;
7394 if (mod != 3) {
0af10c86 7395 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
1e4840bf 7396 gen_op_ld_v(ot + s->mem_index, t0, cpu_A0);
49d9fdcc
LD
7397 a0 = tcg_temp_local_new();
7398 tcg_gen_mov_tl(a0, cpu_A0);
14ce26e7 7399 } else {
1e4840bf 7400 gen_op_mov_v_reg(ot, t0, rm);
49d9fdcc 7401 TCGV_UNUSED(a0);
14ce26e7 7402 }
1e4840bf
FB
7403 gen_op_mov_v_reg(ot, t1, reg);
7404 tcg_gen_andi_tl(cpu_tmp0, t0, 3);
7405 tcg_gen_andi_tl(t1, t1, 3);
7406 tcg_gen_movi_tl(t2, 0);
3bd7da9e 7407 label1 = gen_new_label();
1e4840bf
FB
7408 tcg_gen_brcond_tl(TCG_COND_GE, cpu_tmp0, t1, label1);
7409 tcg_gen_andi_tl(t0, t0, ~3);
7410 tcg_gen_or_tl(t0, t0, t1);
7411 tcg_gen_movi_tl(t2, CC_Z);
3bd7da9e 7412 gen_set_label(label1);
14ce26e7 7413 if (mod != 3) {
49d9fdcc
LD
7414 gen_op_st_v(ot + s->mem_index, t0, a0);
7415 tcg_temp_free(a0);
7416 } else {
1e4840bf 7417 gen_op_mov_reg_v(ot, rm, t0);
14ce26e7 7418 }
c7b3c873 7419 gen_compute_eflags(s, cpu_cc_src);
3bd7da9e 7420 tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~CC_Z);
1e4840bf 7421 tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, t2);
1e4840bf
FB
7422 tcg_temp_free(t0);
7423 tcg_temp_free(t1);
7424 tcg_temp_free(t2);
f115e911 7425 }
f115e911 7426 break;
2c0262af
FB
7427 case 0x102: /* lar */
7428 case 0x103: /* lsl */
cec6843e
FB
7429 {
7430 int label1;
1e4840bf 7431 TCGv t0;
cec6843e
FB
7432 if (!s->pe || s->vm86)
7433 goto illegal_op;
7434 ot = dflag ? OT_LONG : OT_WORD;
0af10c86 7435 modrm = cpu_ldub_code(env, s->pc++);
cec6843e 7436 reg = ((modrm >> 3) & 7) | rex_r;
0af10c86 7437 gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
a7812ae4 7438 t0 = tcg_temp_local_new();
cec6843e
FB
7439 if (s->cc_op != CC_OP_DYNAMIC)
7440 gen_op_set_cc_op(s->cc_op);
2999a0b2
BS
7441 if (b == 0x102) {
7442 gen_helper_lar(t0, cpu_env, cpu_T[0]);
7443 } else {
7444 gen_helper_lsl(t0, cpu_env, cpu_T[0]);
7445 }
cec6843e
FB
7446 tcg_gen_andi_tl(cpu_tmp0, cpu_cc_src, CC_Z);
7447 label1 = gen_new_label();
cb63669a 7448 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, label1);
1e4840bf 7449 gen_op_mov_reg_v(ot, reg, t0);
cec6843e 7450 gen_set_label(label1);
3ca51d07 7451 set_cc_op(s, CC_OP_EFLAGS);
1e4840bf 7452 tcg_temp_free(t0);
cec6843e 7453 }
2c0262af
FB
7454 break;
7455 case 0x118:
0af10c86 7456 modrm = cpu_ldub_code(env, s->pc++);
2c0262af
FB
7457 mod = (modrm >> 6) & 3;
7458 op = (modrm >> 3) & 7;
7459 switch(op) {
7460 case 0: /* prefetchnta */
7461 case 1: /* prefetchnt0 */
7462 case 2: /* prefetchnt0 */
7463 case 3: /* prefetchnt0 */
7464 if (mod == 3)
7465 goto illegal_op;
0af10c86 7466 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
2c0262af
FB
7467 /* nothing more to do */
7468 break;
e17a36ce 7469 default: /* nop (multi byte) */
0af10c86 7470 gen_nop_modrm(env, s, modrm);
e17a36ce 7471 break;
2c0262af
FB
7472 }
7473 break;
e17a36ce 7474 case 0x119 ... 0x11f: /* nop (multi byte) */
0af10c86
BS
7475 modrm = cpu_ldub_code(env, s->pc++);
7476 gen_nop_modrm(env, s, modrm);
e17a36ce 7477 break;
2c0262af
FB
7478 case 0x120: /* mov reg, crN */
7479 case 0x122: /* mov crN, reg */
7480 if (s->cpl != 0) {
7481 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7482 } else {
0af10c86 7483 modrm = cpu_ldub_code(env, s->pc++);
5c73b757
MO
7484 /* Ignore the mod bits (assume (modrm&0xc0)==0xc0).
7485 * AMD documentation (24594.pdf) and testing of
7486 * intel 386 and 486 processors all show that the mod bits
7487 * are assumed to be 1's, regardless of actual values.
7488 */
14ce26e7
FB
7489 rm = (modrm & 7) | REX_B(s);
7490 reg = ((modrm >> 3) & 7) | rex_r;
7491 if (CODE64(s))
7492 ot = OT_QUAD;
7493 else
7494 ot = OT_LONG;
ccd59d09
AP
7495 if ((prefixes & PREFIX_LOCK) && (reg == 0) &&
7496 (s->cpuid_ext3_features & CPUID_EXT3_CR8LEG)) {
7497 reg = 8;
7498 }
2c0262af
FB
7499 switch(reg) {
7500 case 0:
7501 case 2:
7502 case 3:
7503 case 4:
9230e66e 7504 case 8:
872929aa
FB
7505 if (s->cc_op != CC_OP_DYNAMIC)
7506 gen_op_set_cc_op(s->cc_op);
7507 gen_jmp_im(pc_start - s->cs_base);
2c0262af 7508 if (b & 2) {
57fec1fe 7509 gen_op_mov_TN_reg(ot, 0, rm);
4a7443be
BS
7510 gen_helper_write_crN(cpu_env, tcg_const_i32(reg),
7511 cpu_T[0]);
14ce26e7 7512 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
7513 gen_eob(s);
7514 } else {
4a7443be 7515 gen_helper_read_crN(cpu_T[0], cpu_env, tcg_const_i32(reg));
57fec1fe 7516 gen_op_mov_reg_T0(ot, rm);
2c0262af
FB
7517 }
7518 break;
7519 default:
7520 goto illegal_op;
7521 }
7522 }
7523 break;
7524 case 0x121: /* mov reg, drN */
7525 case 0x123: /* mov drN, reg */
7526 if (s->cpl != 0) {
7527 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7528 } else {
0af10c86 7529 modrm = cpu_ldub_code(env, s->pc++);
5c73b757
MO
7530 /* Ignore the mod bits (assume (modrm&0xc0)==0xc0).
7531 * AMD documentation (24594.pdf) and testing of
7532 * intel 386 and 486 processors all show that the mod bits
7533 * are assumed to be 1's, regardless of actual values.
7534 */
14ce26e7
FB
7535 rm = (modrm & 7) | REX_B(s);
7536 reg = ((modrm >> 3) & 7) | rex_r;
7537 if (CODE64(s))
7538 ot = OT_QUAD;
7539 else
7540 ot = OT_LONG;
2c0262af 7541 /* XXX: do it dynamically with CR4.DE bit */
14ce26e7 7542 if (reg == 4 || reg == 5 || reg >= 8)
2c0262af
FB
7543 goto illegal_op;
7544 if (b & 2) {
0573fbfc 7545 gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_DR0 + reg);
57fec1fe 7546 gen_op_mov_TN_reg(ot, 0, rm);
4a7443be 7547 gen_helper_movl_drN_T0(cpu_env, tcg_const_i32(reg), cpu_T[0]);
14ce26e7 7548 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
7549 gen_eob(s);
7550 } else {
0573fbfc 7551 gen_svm_check_intercept(s, pc_start, SVM_EXIT_READ_DR0 + reg);
651ba608 7552 tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,dr[reg]));
57fec1fe 7553 gen_op_mov_reg_T0(ot, rm);
2c0262af
FB
7554 }
7555 }
7556 break;
7557 case 0x106: /* clts */
7558 if (s->cpl != 0) {
7559 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7560 } else {
0573fbfc 7561 gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_CR0);
f0967a1a 7562 gen_helper_clts(cpu_env);
7eee2a50 7563 /* abort block because static cpu state changed */
14ce26e7 7564 gen_jmp_im(s->pc - s->cs_base);
7eee2a50 7565 gen_eob(s);
2c0262af
FB
7566 }
7567 break;
222a3336 7568 /* MMX/3DNow!/SSE/SSE2/SSE3/SSSE3/SSE4 support */
664e0f19
FB
7569 case 0x1c3: /* MOVNTI reg, mem */
7570 if (!(s->cpuid_features & CPUID_SSE2))
14ce26e7 7571 goto illegal_op;
664e0f19 7572 ot = s->dflag == 2 ? OT_QUAD : OT_LONG;
0af10c86 7573 modrm = cpu_ldub_code(env, s->pc++);
664e0f19
FB
7574 mod = (modrm >> 6) & 3;
7575 if (mod == 3)
7576 goto illegal_op;
7577 reg = ((modrm >> 3) & 7) | rex_r;
7578 /* generate a generic store */
0af10c86 7579 gen_ldst_modrm(env, s, modrm, ot, reg, 1);
14ce26e7 7580 break;
664e0f19 7581 case 0x1ae:
0af10c86 7582 modrm = cpu_ldub_code(env, s->pc++);
664e0f19
FB
7583 mod = (modrm >> 6) & 3;
7584 op = (modrm >> 3) & 7;
7585 switch(op) {
7586 case 0: /* fxsave */
5fafdf24 7587 if (mod == 3 || !(s->cpuid_features & CPUID_FXSR) ||
09d85fb8 7588 (s->prefix & PREFIX_LOCK))
14ce26e7 7589 goto illegal_op;
09d85fb8 7590 if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
0fd14b72
FB
7591 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
7592 break;
7593 }
0af10c86 7594 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
19e6c4b8
FB
7595 if (s->cc_op != CC_OP_DYNAMIC)
7596 gen_op_set_cc_op(s->cc_op);
7597 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae 7598 gen_helper_fxsave(cpu_env, cpu_A0, tcg_const_i32((s->dflag == 2)));
664e0f19
FB
7599 break;
7600 case 1: /* fxrstor */
5fafdf24 7601 if (mod == 3 || !(s->cpuid_features & CPUID_FXSR) ||
09d85fb8 7602 (s->prefix & PREFIX_LOCK))
14ce26e7 7603 goto illegal_op;
09d85fb8 7604 if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
0fd14b72
FB
7605 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
7606 break;
7607 }
0af10c86 7608 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
19e6c4b8
FB
7609 if (s->cc_op != CC_OP_DYNAMIC)
7610 gen_op_set_cc_op(s->cc_op);
7611 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae
BS
7612 gen_helper_fxrstor(cpu_env, cpu_A0,
7613 tcg_const_i32((s->dflag == 2)));
664e0f19
FB
7614 break;
7615 case 2: /* ldmxcsr */
7616 case 3: /* stmxcsr */
7617 if (s->flags & HF_TS_MASK) {
7618 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
7619 break;
14ce26e7 7620 }
664e0f19
FB
7621 if ((s->flags & HF_EM_MASK) || !(s->flags & HF_OSFXSR_MASK) ||
7622 mod == 3)
14ce26e7 7623 goto illegal_op;
0af10c86 7624 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
664e0f19 7625 if (op == 2) {
57fec1fe 7626 gen_op_ld_T0_A0(OT_LONG + s->mem_index);
20f8bd48 7627 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
d3eb5eae 7628 gen_helper_ldmxcsr(cpu_env, cpu_tmp2_i32);
14ce26e7 7629 } else {
651ba608 7630 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, mxcsr));
57fec1fe 7631 gen_op_st_T0_A0(OT_LONG + s->mem_index);
14ce26e7 7632 }
664e0f19
FB
7633 break;
7634 case 5: /* lfence */
7635 case 6: /* mfence */
8001c294 7636 if ((modrm & 0xc7) != 0xc0 || !(s->cpuid_features & CPUID_SSE2))
664e0f19
FB
7637 goto illegal_op;
7638 break;
8f091a59
FB
7639 case 7: /* sfence / clflush */
7640 if ((modrm & 0xc7) == 0xc0) {
7641 /* sfence */
a35f3ec7 7642 /* XXX: also check for cpuid_ext2_features & CPUID_EXT2_EMMX */
8f091a59
FB
7643 if (!(s->cpuid_features & CPUID_SSE))
7644 goto illegal_op;
7645 } else {
7646 /* clflush */
7647 if (!(s->cpuid_features & CPUID_CLFLUSH))
7648 goto illegal_op;
0af10c86 7649 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8f091a59
FB
7650 }
7651 break;
664e0f19 7652 default:
14ce26e7
FB
7653 goto illegal_op;
7654 }
7655 break;
a35f3ec7 7656 case 0x10d: /* 3DNow! prefetch(w) */
0af10c86 7657 modrm = cpu_ldub_code(env, s->pc++);
a35f3ec7
AJ
7658 mod = (modrm >> 6) & 3;
7659 if (mod == 3)
7660 goto illegal_op;
0af10c86 7661 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8f091a59
FB
7662 /* ignore for now */
7663 break;
3b21e03e 7664 case 0x1aa: /* rsm */
872929aa 7665 gen_svm_check_intercept(s, pc_start, SVM_EXIT_RSM);
3b21e03e
FB
7666 if (!(s->flags & HF_SMM_MASK))
7667 goto illegal_op;
728d803b 7668 gen_update_cc_op(s);
3b21e03e 7669 gen_jmp_im(s->pc - s->cs_base);
608badfc 7670 gen_helper_rsm(cpu_env);
3b21e03e
FB
7671 gen_eob(s);
7672 break;
222a3336
AZ
7673 case 0x1b8: /* SSE4.2 popcnt */
7674 if ((prefixes & (PREFIX_REPZ | PREFIX_LOCK | PREFIX_REPNZ)) !=
7675 PREFIX_REPZ)
7676 goto illegal_op;
7677 if (!(s->cpuid_ext_features & CPUID_EXT_POPCNT))
7678 goto illegal_op;
7679
0af10c86 7680 modrm = cpu_ldub_code(env, s->pc++);
8b4a3df8 7681 reg = ((modrm >> 3) & 7) | rex_r;
222a3336
AZ
7682
7683 if (s->prefix & PREFIX_DATA)
7684 ot = OT_WORD;
7685 else if (s->dflag != 2)
7686 ot = OT_LONG;
7687 else
7688 ot = OT_QUAD;
7689
0af10c86 7690 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
d3eb5eae 7691 gen_helper_popcnt(cpu_T[0], cpu_env, cpu_T[0], tcg_const_i32(ot));
222a3336 7692 gen_op_mov_reg_T0(ot, reg);
fdb0d09d 7693
3ca51d07 7694 set_cc_op(s, CC_OP_EFLAGS);
222a3336 7695 break;
a35f3ec7
AJ
7696 case 0x10e ... 0x10f:
7697 /* 3DNow! instructions, ignore prefixes */
7698 s->prefix &= ~(PREFIX_REPZ | PREFIX_REPNZ | PREFIX_DATA);
664e0f19
FB
7699 case 0x110 ... 0x117:
7700 case 0x128 ... 0x12f:
4242b1bd 7701 case 0x138 ... 0x13a:
d9f4bb27 7702 case 0x150 ... 0x179:
664e0f19
FB
7703 case 0x17c ... 0x17f:
7704 case 0x1c2:
7705 case 0x1c4 ... 0x1c6:
7706 case 0x1d0 ... 0x1fe:
0af10c86 7707 gen_sse(env, s, b, pc_start, rex_r);
664e0f19 7708 break;
2c0262af
FB
7709 default:
7710 goto illegal_op;
7711 }
7712 /* lock generation */
7713 if (s->prefix & PREFIX_LOCK)
a7812ae4 7714 gen_helper_unlock();
2c0262af
FB
7715 return s->pc;
7716 illegal_op:
ab1f142b 7717 if (s->prefix & PREFIX_LOCK)
a7812ae4 7718 gen_helper_unlock();
2c0262af
FB
7719 /* XXX: ensure that no lock was generated */
7720 gen_exception(s, EXCP06_ILLOP, pc_start - s->cs_base);
7721 return s->pc;
7722}
7723
2c0262af
FB
7724void optimize_flags_init(void)
7725{
a7812ae4
PB
7726 cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
7727 cpu_cc_op = tcg_global_mem_new_i32(TCG_AREG0,
317ac620
AF
7728 offsetof(CPUX86State, cc_op), "cc_op");
7729 cpu_cc_src = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_src),
a7812ae4 7730 "cc_src");
317ac620 7731 cpu_cc_dst = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_dst),
a7812ae4 7732 "cc_dst");
437a88a5 7733
cc739bb0
LD
7734#ifdef TARGET_X86_64
7735 cpu_regs[R_EAX] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7736 offsetof(CPUX86State, regs[R_EAX]), "rax");
cc739bb0 7737 cpu_regs[R_ECX] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7738 offsetof(CPUX86State, regs[R_ECX]), "rcx");
cc739bb0 7739 cpu_regs[R_EDX] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7740 offsetof(CPUX86State, regs[R_EDX]), "rdx");
cc739bb0 7741 cpu_regs[R_EBX] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7742 offsetof(CPUX86State, regs[R_EBX]), "rbx");
cc739bb0 7743 cpu_regs[R_ESP] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7744 offsetof(CPUX86State, regs[R_ESP]), "rsp");
cc739bb0 7745 cpu_regs[R_EBP] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7746 offsetof(CPUX86State, regs[R_EBP]), "rbp");
cc739bb0 7747 cpu_regs[R_ESI] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7748 offsetof(CPUX86State, regs[R_ESI]), "rsi");
cc739bb0 7749 cpu_regs[R_EDI] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7750 offsetof(CPUX86State, regs[R_EDI]), "rdi");
cc739bb0 7751 cpu_regs[8] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7752 offsetof(CPUX86State, regs[8]), "r8");
cc739bb0 7753 cpu_regs[9] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7754 offsetof(CPUX86State, regs[9]), "r9");
cc739bb0 7755 cpu_regs[10] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7756 offsetof(CPUX86State, regs[10]), "r10");
cc739bb0 7757 cpu_regs[11] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7758 offsetof(CPUX86State, regs[11]), "r11");
cc739bb0 7759 cpu_regs[12] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7760 offsetof(CPUX86State, regs[12]), "r12");
cc739bb0 7761 cpu_regs[13] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7762 offsetof(CPUX86State, regs[13]), "r13");
cc739bb0 7763 cpu_regs[14] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7764 offsetof(CPUX86State, regs[14]), "r14");
cc739bb0 7765 cpu_regs[15] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7766 offsetof(CPUX86State, regs[15]), "r15");
cc739bb0
LD
7767#else
7768 cpu_regs[R_EAX] = tcg_global_mem_new_i32(TCG_AREG0,
317ac620 7769 offsetof(CPUX86State, regs[R_EAX]), "eax");
cc739bb0 7770 cpu_regs[R_ECX] = tcg_global_mem_new_i32(TCG_AREG0,
317ac620 7771 offsetof(CPUX86State, regs[R_ECX]), "ecx");
cc739bb0 7772 cpu_regs[R_EDX] = tcg_global_mem_new_i32(TCG_AREG0,
317ac620 7773 offsetof(CPUX86State, regs[R_EDX]), "edx");
cc739bb0 7774 cpu_regs[R_EBX] = tcg_global_mem_new_i32(TCG_AREG0,
317ac620 7775 offsetof(CPUX86State, regs[R_EBX]), "ebx");
cc739bb0 7776 cpu_regs[R_ESP] = tcg_global_mem_new_i32(TCG_AREG0,
317ac620 7777 offsetof(CPUX86State, regs[R_ESP]), "esp");
cc739bb0 7778 cpu_regs[R_EBP] = tcg_global_mem_new_i32(TCG_AREG0,
317ac620 7779 offsetof(CPUX86State, regs[R_EBP]), "ebp");
cc739bb0 7780 cpu_regs[R_ESI] = tcg_global_mem_new_i32(TCG_AREG0,
317ac620 7781 offsetof(CPUX86State, regs[R_ESI]), "esi");
cc739bb0 7782 cpu_regs[R_EDI] = tcg_global_mem_new_i32(TCG_AREG0,
317ac620 7783 offsetof(CPUX86State, regs[R_EDI]), "edi");
cc739bb0
LD
7784#endif
7785
437a88a5 7786 /* register helpers */
a7812ae4 7787#define GEN_HELPER 2
437a88a5 7788#include "helper.h"
2c0262af
FB
7789}
7790
7791/* generate intermediate code in gen_opc_buf and gen_opparam_buf for
7792 basic block 'tb'. If search_pc is TRUE, also generate PC
7793 information for each intermediate instruction. */
317ac620 7794static inline void gen_intermediate_code_internal(CPUX86State *env,
2cfc5f17
TS
7795 TranslationBlock *tb,
7796 int search_pc)
2c0262af
FB
7797{
7798 DisasContext dc1, *dc = &dc1;
14ce26e7 7799 target_ulong pc_ptr;
2c0262af 7800 uint16_t *gen_opc_end;
a1d1bb31 7801 CPUBreakpoint *bp;
7f5b7d3e 7802 int j, lj;
c068688b 7803 uint64_t flags;
14ce26e7
FB
7804 target_ulong pc_start;
7805 target_ulong cs_base;
2e70f6ef
PB
7806 int num_insns;
7807 int max_insns;
3b46e624 7808
2c0262af 7809 /* generate intermediate code */
14ce26e7
FB
7810 pc_start = tb->pc;
7811 cs_base = tb->cs_base;
2c0262af 7812 flags = tb->flags;
3a1d9b8b 7813
4f31916f 7814 dc->pe = (flags >> HF_PE_SHIFT) & 1;
2c0262af
FB
7815 dc->code32 = (flags >> HF_CS32_SHIFT) & 1;
7816 dc->ss32 = (flags >> HF_SS32_SHIFT) & 1;
7817 dc->addseg = (flags >> HF_ADDSEG_SHIFT) & 1;
7818 dc->f_st = 0;
7819 dc->vm86 = (flags >> VM_SHIFT) & 1;
7820 dc->cpl = (flags >> HF_CPL_SHIFT) & 3;
7821 dc->iopl = (flags >> IOPL_SHIFT) & 3;
7822 dc->tf = (flags >> TF_SHIFT) & 1;
34865134 7823 dc->singlestep_enabled = env->singlestep_enabled;
2c0262af 7824 dc->cc_op = CC_OP_DYNAMIC;
e207582f 7825 dc->cc_op_dirty = false;
2c0262af
FB
7826 dc->cs_base = cs_base;
7827 dc->tb = tb;
7828 dc->popl_esp_hack = 0;
7829 /* select memory access functions */
7830 dc->mem_index = 0;
7831 if (flags & HF_SOFTMMU_MASK) {
a9321a4d 7832 dc->mem_index = (cpu_mmu_index(env) + 1) << 2;
2c0262af 7833 }
14ce26e7 7834 dc->cpuid_features = env->cpuid_features;
3d7374c5 7835 dc->cpuid_ext_features = env->cpuid_ext_features;
e771edab 7836 dc->cpuid_ext2_features = env->cpuid_ext2_features;
12e26b75 7837 dc->cpuid_ext3_features = env->cpuid_ext3_features;
a9321a4d 7838 dc->cpuid_7_0_ebx_features = env->cpuid_7_0_ebx_features;
14ce26e7
FB
7839#ifdef TARGET_X86_64
7840 dc->lma = (flags >> HF_LMA_SHIFT) & 1;
7841 dc->code64 = (flags >> HF_CS64_SHIFT) & 1;
7842#endif
7eee2a50 7843 dc->flags = flags;
a2cc3b24
FB
7844 dc->jmp_opt = !(dc->tf || env->singlestep_enabled ||
7845 (flags & HF_INHIBIT_IRQ_MASK)
415fa2ea 7846#ifndef CONFIG_SOFTMMU
2c0262af
FB
7847 || (flags & HF_SOFTMMU_MASK)
7848#endif
7849 );
4f31916f
FB
7850#if 0
7851 /* check addseg logic */
dc196a57 7852 if (!dc->addseg && (dc->vm86 || !dc->pe || !dc->code32))
4f31916f
FB
7853 printf("ERROR addseg\n");
7854#endif
7855
a7812ae4
PB
7856 cpu_T[0] = tcg_temp_new();
7857 cpu_T[1] = tcg_temp_new();
7858 cpu_A0 = tcg_temp_new();
7859 cpu_T3 = tcg_temp_new();
7860
7861 cpu_tmp0 = tcg_temp_new();
7862 cpu_tmp1_i64 = tcg_temp_new_i64();
7863 cpu_tmp2_i32 = tcg_temp_new_i32();
7864 cpu_tmp3_i32 = tcg_temp_new_i32();
7865 cpu_tmp4 = tcg_temp_new();
7866 cpu_tmp5 = tcg_temp_new();
a7812ae4
PB
7867 cpu_ptr0 = tcg_temp_new_ptr();
7868 cpu_ptr1 = tcg_temp_new_ptr();
57fec1fe 7869
92414b31 7870 gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
2c0262af
FB
7871
7872 dc->is_jmp = DISAS_NEXT;
7873 pc_ptr = pc_start;
7874 lj = -1;
2e70f6ef
PB
7875 num_insns = 0;
7876 max_insns = tb->cflags & CF_COUNT_MASK;
7877 if (max_insns == 0)
7878 max_insns = CF_COUNT_MASK;
2c0262af 7879
2e70f6ef 7880 gen_icount_start();
2c0262af 7881 for(;;) {
72cf2d4f
BS
7882 if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
7883 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
a2397807
JK
7884 if (bp->pc == pc_ptr &&
7885 !((bp->flags & BP_CPU) && (tb->flags & HF_RF_MASK))) {
2c0262af
FB
7886 gen_debug(dc, pc_ptr - dc->cs_base);
7887 break;
7888 }
7889 }
7890 }
7891 if (search_pc) {
92414b31 7892 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
2c0262af
FB
7893 if (lj < j) {
7894 lj++;
7895 while (lj < j)
ab1103de 7896 tcg_ctx.gen_opc_instr_start[lj++] = 0;
2c0262af 7897 }
25983cad 7898 tcg_ctx.gen_opc_pc[lj] = pc_ptr;
2c0262af 7899 gen_opc_cc_op[lj] = dc->cc_op;
ab1103de 7900 tcg_ctx.gen_opc_instr_start[lj] = 1;
c9c99c22 7901 tcg_ctx.gen_opc_icount[lj] = num_insns;
2c0262af 7902 }
2e70f6ef
PB
7903 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
7904 gen_io_start();
7905
0af10c86 7906 pc_ptr = disas_insn(env, dc, pc_ptr);
2e70f6ef 7907 num_insns++;
2c0262af
FB
7908 /* stop translation if indicated */
7909 if (dc->is_jmp)
7910 break;
7911 /* if single step mode, we generate only one instruction and
7912 generate an exception */
a2cc3b24
FB
7913 /* if irq were inhibited with HF_INHIBIT_IRQ_MASK, we clear
7914 the flag and abort the translation to give the irqs a
7915 change to be happen */
5fafdf24 7916 if (dc->tf || dc->singlestep_enabled ||
2e70f6ef 7917 (flags & HF_INHIBIT_IRQ_MASK)) {
14ce26e7 7918 gen_jmp_im(pc_ptr - dc->cs_base);
2c0262af
FB
7919 gen_eob(dc);
7920 break;
7921 }
7922 /* if too long translation, stop generation too */
efd7f486 7923 if (tcg_ctx.gen_opc_ptr >= gen_opc_end ||
2e70f6ef
PB
7924 (pc_ptr - pc_start) >= (TARGET_PAGE_SIZE - 32) ||
7925 num_insns >= max_insns) {
14ce26e7 7926 gen_jmp_im(pc_ptr - dc->cs_base);
2c0262af
FB
7927 gen_eob(dc);
7928 break;
7929 }
1b530a6d
AJ
7930 if (singlestep) {
7931 gen_jmp_im(pc_ptr - dc->cs_base);
7932 gen_eob(dc);
7933 break;
7934 }
2c0262af 7935 }
2e70f6ef
PB
7936 if (tb->cflags & CF_LAST_IO)
7937 gen_io_end();
7938 gen_icount_end(tb, num_insns);
efd7f486 7939 *tcg_ctx.gen_opc_ptr = INDEX_op_end;
2c0262af
FB
7940 /* we don't forget to fill the last values */
7941 if (search_pc) {
92414b31 7942 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
2c0262af
FB
7943 lj++;
7944 while (lj <= j)
ab1103de 7945 tcg_ctx.gen_opc_instr_start[lj++] = 0;
2c0262af 7946 }
3b46e624 7947
2c0262af 7948#ifdef DEBUG_DISAS
8fec2b8c 7949 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
14ce26e7 7950 int disas_flags;
93fcfe39
AL
7951 qemu_log("----------------\n");
7952 qemu_log("IN: %s\n", lookup_symbol(pc_start));
14ce26e7
FB
7953#ifdef TARGET_X86_64
7954 if (dc->code64)
7955 disas_flags = 2;
7956 else
7957#endif
7958 disas_flags = !dc->code32;
f4359b9f 7959 log_target_disas(env, pc_start, pc_ptr - pc_start, disas_flags);
93fcfe39 7960 qemu_log("\n");
2c0262af
FB
7961 }
7962#endif
7963
2e70f6ef 7964 if (!search_pc) {
2c0262af 7965 tb->size = pc_ptr - pc_start;
2e70f6ef
PB
7966 tb->icount = num_insns;
7967 }
2c0262af
FB
7968}
7969
317ac620 7970void gen_intermediate_code(CPUX86State *env, TranslationBlock *tb)
2c0262af 7971{
2cfc5f17 7972 gen_intermediate_code_internal(env, tb, 0);
2c0262af
FB
7973}
7974
317ac620 7975void gen_intermediate_code_pc(CPUX86State *env, TranslationBlock *tb)
2c0262af 7976{
2cfc5f17 7977 gen_intermediate_code_internal(env, tb, 1);
2c0262af
FB
7978}
7979
317ac620 7980void restore_state_to_opc(CPUX86State *env, TranslationBlock *tb, int pc_pos)
d2856f1a
AJ
7981{
7982 int cc_op;
7983#ifdef DEBUG_DISAS
8fec2b8c 7984 if (qemu_loglevel_mask(CPU_LOG_TB_OP)) {
d2856f1a 7985 int i;
93fcfe39 7986 qemu_log("RESTORE:\n");
d2856f1a 7987 for(i = 0;i <= pc_pos; i++) {
ab1103de 7988 if (tcg_ctx.gen_opc_instr_start[i]) {
25983cad
EV
7989 qemu_log("0x%04x: " TARGET_FMT_lx "\n", i,
7990 tcg_ctx.gen_opc_pc[i]);
d2856f1a
AJ
7991 }
7992 }
e87b7cb0 7993 qemu_log("pc_pos=0x%x eip=" TARGET_FMT_lx " cs_base=%x\n",
25983cad 7994 pc_pos, tcg_ctx.gen_opc_pc[pc_pos] - tb->cs_base,
d2856f1a
AJ
7995 (uint32_t)tb->cs_base);
7996 }
7997#endif
25983cad 7998 env->eip = tcg_ctx.gen_opc_pc[pc_pos] - tb->cs_base;
d2856f1a
AJ
7999 cc_op = gen_opc_cc_op[pc_pos];
8000 if (cc_op != CC_OP_DYNAMIC)
8001 env->cc_op = cc_op;
8002}