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