translate.c 272.4 KB
Newer Older
B
bellard 已提交
1 2
/*
 *  i386 translation
3
 *
B
bellard 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16
 *  Copyright (c) 2003 Fabrice Bellard
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
B
bellard 已提交
18 19 20 21 22 23 24 25
 */
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <signal.h>

26
#include "qemu/host-utils.h"
B
bellard 已提交
27
#include "cpu.h"
28
#include "disas/disas.h"
B
bellard 已提交
29
#include "tcg-op.h"
P
Paolo Bonzini 已提交
30
#include "exec/cpu_ldst.h"
B
bellard 已提交
31

32 33
#include "exec/helper-proto.h"
#include "exec/helper-gen.h"
P
pbrook 已提交
34

35 36 37
#include "trace-tcg.h"


B
bellard 已提交
38 39 40 41 42
#define PREFIX_REPZ   0x01
#define PREFIX_REPNZ  0x02
#define PREFIX_LOCK   0x04
#define PREFIX_DATA   0x08
#define PREFIX_ADR    0x10
43
#define PREFIX_VEX    0x20
B
bellard 已提交
44

B
bellard 已提交
45 46 47 48 49 50 51 52 53 54
#ifdef TARGET_X86_64
#define CODE64(s) ((s)->code64)
#define REX_X(s) ((s)->rex_x)
#define REX_B(s) ((s)->rex_b)
#else
#define CODE64(s) 0
#define REX_X(s) 0
#define REX_B(s) 0
#endif

55 56 57 58 59 60 61 62
#ifdef TARGET_X86_64
# define ctztl  ctz64
# define clztl  clz64
#else
# define ctztl  ctz32
# define clztl  clz32
#endif

B
bellard 已提交
63 64 65
//#define MACRO_TEST   1

/* global register indexes */
P
pbrook 已提交
66
static TCGv_ptr cpu_env;
67
static TCGv cpu_A0;
68
static TCGv cpu_cc_dst, cpu_cc_src, cpu_cc_src2, cpu_cc_srcT;
P
pbrook 已提交
69
static TCGv_i32 cpu_cc_op;
70
static TCGv cpu_regs[CPU_NB_REGS];
71
/* local temps */
P
Paolo Bonzini 已提交
72
static TCGv cpu_T[2];
B
bellard 已提交
73
/* local register indexes (only used inside old micro ops) */
P
pbrook 已提交
74 75 76 77
static TCGv cpu_tmp0, cpu_tmp4;
static TCGv_ptr cpu_ptr0, cpu_ptr1;
static TCGv_i32 cpu_tmp2_i32, cpu_tmp3_i32;
static TCGv_i64 cpu_tmp1_i64;
B
bellard 已提交
78

79 80
static uint8_t gen_opc_cc_op[OPC_BUF_SIZE];

81
#include "exec/gen-icount.h"
P
pbrook 已提交
82

B
bellard 已提交
83 84
#ifdef TARGET_X86_64
static int x86_64_hregs;
B
bellard 已提交
85 86
#endif

B
bellard 已提交
87 88 89 90
typedef struct DisasContext {
    /* current insn context */
    int override; /* -1 if no override */
    int prefix;
91
    TCGMemOp aflag;
92
    TCGMemOp dflag;
B
bellard 已提交
93
    target_ulong pc; /* pc = eip + cs_base */
B
bellard 已提交
94 95 96
    int is_jmp; /* 1 = means jump (stop translation), 2 means CPU
                   static state change (stop translation) */
    /* current block context */
B
bellard 已提交
97
    target_ulong cs_base; /* base of CS segment */
B
bellard 已提交
98 99
    int pe;     /* protected mode */
    int code32; /* 32 bit code segment */
B
bellard 已提交
100 101 102 103 104
#ifdef TARGET_X86_64
    int lma;    /* long mode active */
    int code64; /* 64 bit code segment */
    int rex_x, rex_b;
#endif
105 106
    int vex_l;  /* vex vector length */
    int vex_v;  /* vex vvvv register, without 1's compliment.  */
B
bellard 已提交
107
    int ss32;   /* 32 bit stack segment */
108
    CCOp cc_op;  /* current CC operation */
109
    bool cc_op_dirty;
B
bellard 已提交
110 111 112 113 114 115
    int addseg; /* non zero if either DS/ES/SS have a non zero base */
    int f_st;   /* currently unused */
    int vm86;   /* vm86 mode */
    int cpl;
    int iopl;
    int tf;     /* TF cpu flag */
116
    int singlestep_enabled; /* "hardware" single step enabled */
B
bellard 已提交
117 118
    int jmp_opt; /* use direct block chaining for direct jumps */
    int mem_index; /* select memory access functions */
119
    uint64_t flags; /* all execution flags */
B
bellard 已提交
120 121
    struct TranslationBlock *tb;
    int popl_esp_hack; /* for correct popl with esp base handling */
B
bellard 已提交
122 123
    int rip_offset; /* only used in x86_64, but left for simplicity */
    int cpuid_features;
B
bellard 已提交
124
    int cpuid_ext_features;
125
    int cpuid_ext2_features;
B
bellard 已提交
126
    int cpuid_ext3_features;
H
H. Peter Anvin 已提交
127
    int cpuid_7_0_ebx_features;
B
bellard 已提交
128 129 130
} DisasContext;

static void gen_eob(DisasContext *s);
B
bellard 已提交
131 132
static void gen_jmp(DisasContext *s, target_ulong eip);
static void gen_jmp_tb(DisasContext *s, target_ulong eip, int tb_num);
133
static void gen_op(DisasContext *s1, int op, TCGMemOp ot, int d);
B
bellard 已提交
134 135 136

/* i386 arith/logic operations */
enum {
137 138 139
    OP_ADDL,
    OP_ORL,
    OP_ADCL,
B
bellard 已提交
140
    OP_SBBL,
141 142 143
    OP_ANDL,
    OP_SUBL,
    OP_XORL,
B
bellard 已提交
144 145 146 147 148
    OP_CMPL,
};

/* i386 shift ops */
enum {
149 150 151 152 153 154
    OP_ROL,
    OP_ROR,
    OP_RCL,
    OP_RCR,
    OP_SHL,
    OP_SHR,
B
bellard 已提交
155 156 157 158
    OP_SHL1, /* undocumented */
    OP_SAR = 7,
};

159 160 161 162 163 164 165 166 167 168 169
enum {
    JCC_O,
    JCC_B,
    JCC_Z,
    JCC_BE,
    JCC_S,
    JCC_P,
    JCC_L,
    JCC_LE,
};

B
bellard 已提交
170 171 172 173 174 175 176 177 178 179
enum {
    /* I386 int registers */
    OR_EAX,   /* MUST be even numbered */
    OR_ECX,
    OR_EDX,
    OR_EBX,
    OR_ESP,
    OR_EBP,
    OR_ESI,
    OR_EDI,
B
bellard 已提交
180 181

    OR_TMP0 = 16,    /* temporary operand register */
B
bellard 已提交
182 183 184 185
    OR_TMP1,
    OR_A0, /* temporary register used when doing address evaluation */
};

186
enum {
187 188
    USES_CC_DST  = 1,
    USES_CC_SRC  = 2,
189 190
    USES_CC_SRC2 = 4,
    USES_CC_SRCT = 8,
191 192 193 194
};

/* Bit set if the global variable is live after setting CC_OP to X.  */
static const uint8_t cc_op_live[CC_OP_NB] = {
195
    [CC_OP_DYNAMIC] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
196 197 198
    [CC_OP_EFLAGS] = USES_CC_SRC,
    [CC_OP_MULB ... CC_OP_MULQ] = USES_CC_DST | USES_CC_SRC,
    [CC_OP_ADDB ... CC_OP_ADDQ] = USES_CC_DST | USES_CC_SRC,
199
    [CC_OP_ADCB ... CC_OP_ADCQ] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
200
    [CC_OP_SUBB ... CC_OP_SUBQ] = USES_CC_DST | USES_CC_SRC | USES_CC_SRCT,
201
    [CC_OP_SBBB ... CC_OP_SBBQ] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
202 203 204 205 206
    [CC_OP_LOGICB ... CC_OP_LOGICQ] = USES_CC_DST,
    [CC_OP_INCB ... CC_OP_INCQ] = USES_CC_DST | USES_CC_SRC,
    [CC_OP_DECB ... CC_OP_DECQ] = USES_CC_DST | USES_CC_SRC,
    [CC_OP_SHLB ... CC_OP_SHLQ] = USES_CC_DST | USES_CC_SRC,
    [CC_OP_SARB ... CC_OP_SARQ] = USES_CC_DST | USES_CC_SRC,
207
    [CC_OP_BMILGB ... CC_OP_BMILGQ] = USES_CC_DST | USES_CC_SRC,
208 209 210
    [CC_OP_ADCX] = USES_CC_DST | USES_CC_SRC,
    [CC_OP_ADOX] = USES_CC_SRC | USES_CC_SRC2,
    [CC_OP_ADCOX] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
R
Richard Henderson 已提交
211
    [CC_OP_CLR] = 0,
212 213
};

214
static void set_cc_op(DisasContext *s, CCOp op)
215
{
216 217 218 219 220 221 222 223 224 225
    int dead;

    if (s->cc_op == op) {
        return;
    }

    /* Discard CC computation that will no longer be used.  */
    dead = cc_op_live[s->cc_op] & ~cc_op_live[op];
    if (dead & USES_CC_DST) {
        tcg_gen_discard_tl(cpu_cc_dst);
226
    }
227 228 229
    if (dead & USES_CC_SRC) {
        tcg_gen_discard_tl(cpu_cc_src);
    }
230 231 232
    if (dead & USES_CC_SRC2) {
        tcg_gen_discard_tl(cpu_cc_src2);
    }
233 234 235
    if (dead & USES_CC_SRCT) {
        tcg_gen_discard_tl(cpu_cc_srcT);
    }
236

237 238 239 240 241 242 243 244 245 246 247
    if (op == CC_OP_DYNAMIC) {
        /* The DYNAMIC setting is translator only, and should never be
           stored.  Thus we always consider it clean.  */
        s->cc_op_dirty = false;
    } else {
        /* Discard any computed CC_OP value (see shifts).  */
        if (s->cc_op == CC_OP_DYNAMIC) {
            tcg_gen_discard_i32(cpu_cc_op);
        }
        s->cc_op_dirty = true;
    }
248
    s->cc_op = op;
249 250 251 252 253
}

static void gen_update_cc_op(DisasContext *s)
{
    if (s->cc_op_dirty) {
254
        tcg_gen_movi_i32(cpu_cc_op, s->cc_op);
255 256
        s->cc_op_dirty = false;
    }
257 258
}

B
bellard 已提交
259 260 261 262 263 264 265 266 267 268
#ifdef TARGET_X86_64

#define NB_OP_SIZES 4

#else /* !TARGET_X86_64 */

#define NB_OP_SIZES 3

#endif /* !TARGET_X86_64 */

269
#if defined(HOST_WORDS_BIGENDIAN)
B
bellard 已提交
270 271 272 273 274
#define REG_B_OFFSET (sizeof(target_ulong) - 1)
#define REG_H_OFFSET (sizeof(target_ulong) - 2)
#define REG_W_OFFSET (sizeof(target_ulong) - 2)
#define REG_L_OFFSET (sizeof(target_ulong) - 4)
#define REG_LH_OFFSET (sizeof(target_ulong) - 8)
B
bellard 已提交
275
#else
B
bellard 已提交
276 277 278 279 280
#define REG_B_OFFSET 0
#define REG_H_OFFSET 1
#define REG_W_OFFSET 0
#define REG_L_OFFSET 0
#define REG_LH_OFFSET 4
B
bellard 已提交
281
#endif
B
bellard 已提交
282

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
/* In instruction encodings for byte register accesses the
 * register number usually indicates "low 8 bits of register N";
 * however there are some special cases where N 4..7 indicates
 * [AH, CH, DH, BH], ie "bits 15..8 of register N-4". Return
 * true for this special case, false otherwise.
 */
static inline bool byte_reg_is_xH(int reg)
{
    if (reg < 4) {
        return false;
    }
#ifdef TARGET_X86_64
    if (reg >= 8 || x86_64_hregs) {
        return false;
    }
#endif
    return true;
}

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
/* Select the size of a push/pop operation.  */
static inline TCGMemOp mo_pushpop(DisasContext *s, TCGMemOp ot)
{
    if (CODE64(s)) {
        return ot == MO_16 ? MO_16 : MO_64;
    } else {
        return ot;
    }
}

/* Select only size 64 else 32.  Used for SSE operand sizes.  */
static inline TCGMemOp mo_64_32(TCGMemOp ot)
{
#ifdef TARGET_X86_64
    return ot == MO_64 ? MO_64 : MO_32;
#else
    return MO_32;
#endif
}

/* Select size 8 if lsb of B is clear, else OT.  Used for decoding
   byte vs word opcodes.  */
static inline TCGMemOp mo_b_d(int b, TCGMemOp ot)
{
    return b & 1 ? ot : MO_8;
}

/* Select size 8 if lsb of B is clear, else OT capped at 32.
   Used for decoding operand size of port opcodes.  */
static inline TCGMemOp mo_b_d32(int b, TCGMemOp ot)
{
    return b & 1 ? (ot == MO_16 ? MO_16 : MO_32) : MO_8;
}

336
static void gen_op_mov_reg_v(TCGMemOp ot, int reg, TCGv t0)
B
bellard 已提交
337 338
{
    switch(ot) {
339
    case MO_8:
340
        if (!byte_reg_is_xH(reg)) {
341
            tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], t0, 0, 8);
B
bellard 已提交
342
        } else {
343
            tcg_gen_deposit_tl(cpu_regs[reg - 4], cpu_regs[reg - 4], t0, 8, 8);
B
bellard 已提交
344 345
        }
        break;
346
    case MO_16:
347
        tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], t0, 0, 16);
B
bellard 已提交
348
        break;
349
    case MO_32:
350 351 352
        /* For x86_64, this sets the higher half of register to zero.
           For i386, this is equivalent to a mov. */
        tcg_gen_ext32u_tl(cpu_regs[reg], t0);
B
bellard 已提交
353
        break;
354
#ifdef TARGET_X86_64
355
    case MO_64:
356
        tcg_gen_mov_tl(cpu_regs[reg], t0);
B
bellard 已提交
357
        break;
B
bellard 已提交
358
#endif
359 360
    default:
        tcg_abort();
B
bellard 已提交
361 362
    }
}
B
bellard 已提交
363

364
static inline void gen_op_mov_v_reg(TCGMemOp ot, TCGv t0, int reg)
B
bellard 已提交
365
{
366
    if (ot == MO_8 && byte_reg_is_xH(reg)) {
367 368 369
        tcg_gen_shri_tl(t0, cpu_regs[reg - 4], 8);
        tcg_gen_ext8u_tl(t0, t0);
    } else {
370
        tcg_gen_mov_tl(t0, cpu_regs[reg]);
B
bellard 已提交
371 372 373 374 375
    }
}

static inline void gen_op_movl_A0_reg(int reg)
{
376
    tcg_gen_mov_tl(cpu_A0, cpu_regs[reg]);
B
bellard 已提交
377 378 379 380 381
}

static inline void gen_op_addl_A0_im(int32_t val)
{
    tcg_gen_addi_tl(cpu_A0, cpu_A0, val);
B
bellard 已提交
382
#ifdef TARGET_X86_64
B
bellard 已提交
383
    tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffffffff);
B
bellard 已提交
384
#endif
B
bellard 已提交
385
}
B
bellard 已提交
386

B
bellard 已提交
387
#ifdef TARGET_X86_64
B
bellard 已提交
388 389 390 391
static inline void gen_op_addq_A0_im(int64_t val)
{
    tcg_gen_addi_tl(cpu_A0, cpu_A0, val);
}
B
bellard 已提交
392
#endif
B
bellard 已提交
393 394 395 396 397 398 399 400 401 402
    
static void gen_add_A0_im(DisasContext *s, int val)
{
#ifdef TARGET_X86_64
    if (CODE64(s))
        gen_op_addq_A0_im(val);
    else
#endif
        gen_op_addl_A0_im(val);
}
B
bellard 已提交
403

404
static inline void gen_op_jmp_v(TCGv dest)
B
bellard 已提交
405
{
406
    tcg_gen_st_tl(dest, cpu_env, offsetof(CPUX86State, eip));
B
bellard 已提交
407 408
}

409
static inline void gen_op_add_reg_im(TCGMemOp size, int reg, int32_t val)
B
bellard 已提交
410
{
411 412
    tcg_gen_addi_tl(cpu_tmp0, cpu_regs[reg], val);
    gen_op_mov_reg_v(size, reg, cpu_tmp0);
B
bellard 已提交
413 414
}

415
static inline void gen_op_add_reg_T0(TCGMemOp size, int reg)
B
bellard 已提交
416
{
417 418
    tcg_gen_add_tl(cpu_tmp0, cpu_regs[reg], cpu_T[0]);
    gen_op_mov_reg_v(size, reg, cpu_tmp0);
419
}
B
bellard 已提交
420 421 422

static inline void gen_op_addl_A0_reg_sN(int shift, int reg)
{
423 424
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[reg]);
    if (shift != 0)
B
bellard 已提交
425 426
        tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, shift);
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
427 428 429
    /* For x86_64, this sets the higher half of register to zero.
       For i386, this is equivalent to a nop. */
    tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
B
bellard 已提交
430
}
B
bellard 已提交
431

B
bellard 已提交
432 433
static inline void gen_op_movl_A0_seg(int reg)
{
434
    tcg_gen_ld32u_tl(cpu_A0, cpu_env, offsetof(CPUX86State, segs[reg].base) + REG_L_OFFSET);
B
bellard 已提交
435
}
B
bellard 已提交
436

437
static inline void gen_op_addl_A0_seg(DisasContext *s, int reg)
B
bellard 已提交
438
{
439
    tcg_gen_ld_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, segs[reg].base));
B
bellard 已提交
440
#ifdef TARGET_X86_64
441 442 443 444 445 446 447 448 449
    if (CODE64(s)) {
        tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffffffff);
        tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
    } else {
        tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
        tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffffffff);
    }
#else
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
B
bellard 已提交
450 451
#endif
}
B
bellard 已提交
452

B
bellard 已提交
453
#ifdef TARGET_X86_64
B
bellard 已提交
454 455
static inline void gen_op_movq_A0_seg(int reg)
{
456
    tcg_gen_ld_tl(cpu_A0, cpu_env, offsetof(CPUX86State, segs[reg].base));
B
bellard 已提交
457
}
B
bellard 已提交
458

B
bellard 已提交
459 460
static inline void gen_op_addq_A0_seg(int reg)
{
461
    tcg_gen_ld_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, segs[reg].base));
B
bellard 已提交
462 463 464 465 466
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
}

static inline void gen_op_movq_A0_reg(int reg)
{
467
    tcg_gen_mov_tl(cpu_A0, cpu_regs[reg]);
B
bellard 已提交
468 469 470 471
}

static inline void gen_op_addq_A0_reg_sN(int shift, int reg)
{
472 473
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[reg]);
    if (shift != 0)
B
bellard 已提交
474 475 476
        tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, shift);
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
}
B
bellard 已提交
477 478
#endif

479
static inline void gen_op_ld_v(DisasContext *s, int idx, TCGv t0, TCGv a0)
B
bellard 已提交
480
{
481
    tcg_gen_qemu_ld_tl(t0, a0, s->mem_index, idx | MO_LE);
B
bellard 已提交
482
}
B
bellard 已提交
483

484
static inline void gen_op_st_v(DisasContext *s, int idx, TCGv t0, TCGv a0)
B
bellard 已提交
485
{
486
    tcg_gen_qemu_st_tl(t0, a0, s->mem_index, idx | MO_LE);
B
bellard 已提交
487
}
488

489 490 491
static inline void gen_op_st_rm_T0_A0(DisasContext *s, int idx, int d)
{
    if (d == OR_TMP0) {
492
        gen_op_st_v(s, idx, cpu_T[0], cpu_A0);
493
    } else {
494
        gen_op_mov_reg_v(idx, d, cpu_T[0]);
495 496 497
    }
}

B
bellard 已提交
498 499
static inline void gen_jmp_im(target_ulong pc)
{
B
bellard 已提交
500
    tcg_gen_movi_tl(cpu_tmp0, pc);
501
    gen_op_jmp_v(cpu_tmp0);
B
bellard 已提交
502 503
}

B
bellard 已提交
504 505 506 507 508
static inline void gen_string_movl_A0_ESI(DisasContext *s)
{
    int override;

    override = s->override;
509
    switch (s->aflag) {
B
bellard 已提交
510
#ifdef TARGET_X86_64
511
    case MO_64:
B
bellard 已提交
512
        if (override >= 0) {
B
bellard 已提交
513 514
            gen_op_movq_A0_seg(override);
            gen_op_addq_A0_reg_sN(0, R_ESI);
B
bellard 已提交
515
        } else {
B
bellard 已提交
516
            gen_op_movq_A0_reg(R_ESI);
B
bellard 已提交
517
        }
518
        break;
B
bellard 已提交
519
#endif
520
    case MO_32:
B
bellard 已提交
521 522 523 524
        /* 32 bit address */
        if (s->addseg && override < 0)
            override = R_DS;
        if (override >= 0) {
B
bellard 已提交
525 526
            gen_op_movl_A0_seg(override);
            gen_op_addl_A0_reg_sN(0, R_ESI);
B
bellard 已提交
527
        } else {
B
bellard 已提交
528
            gen_op_movl_A0_reg(R_ESI);
B
bellard 已提交
529
        }
530 531
        break;
    case MO_16:
B
bellard 已提交
532 533 534
        /* 16 address, always override */
        if (override < 0)
            override = R_DS;
535
        tcg_gen_ext16u_tl(cpu_A0, cpu_regs[R_ESI]);
536
        gen_op_addl_A0_seg(s, override);
537 538 539
        break;
    default:
        tcg_abort();
B
bellard 已提交
540 541 542 543 544
    }
}

static inline void gen_string_movl_A0_EDI(DisasContext *s)
{
545
    switch (s->aflag) {
B
bellard 已提交
546
#ifdef TARGET_X86_64
547
    case MO_64:
B
bellard 已提交
548
        gen_op_movq_A0_reg(R_EDI);
549
        break;
B
bellard 已提交
550
#endif
551
    case MO_32:
B
bellard 已提交
552
        if (s->addseg) {
B
bellard 已提交
553 554
            gen_op_movl_A0_seg(R_ES);
            gen_op_addl_A0_reg_sN(0, R_EDI);
B
bellard 已提交
555
        } else {
B
bellard 已提交
556
            gen_op_movl_A0_reg(R_EDI);
B
bellard 已提交
557
        }
558 559
        break;
    case MO_16:
560
        tcg_gen_ext16u_tl(cpu_A0, cpu_regs[R_EDI]);
561
        gen_op_addl_A0_seg(s, R_ES);
562 563 564
        break;
    default:
        tcg_abort();
B
bellard 已提交
565 566 567
    }
}

568
static inline void gen_op_movl_T0_Dshift(TCGMemOp ot)
569
{
570
    tcg_gen_ld32s_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, df));
571
    tcg_gen_shli_tl(cpu_T[0], cpu_T[0], ot);
B
bellard 已提交
572 573
};

574
static TCGv gen_ext_tl(TCGv dst, TCGv src, TCGMemOp size, bool sign)
575
{
576
    switch (size) {
577
    case MO_8:
578 579 580 581 582 583
        if (sign) {
            tcg_gen_ext8s_tl(dst, src);
        } else {
            tcg_gen_ext8u_tl(dst, src);
        }
        return dst;
584
    case MO_16:
585 586 587 588 589 590 591
        if (sign) {
            tcg_gen_ext16s_tl(dst, src);
        } else {
            tcg_gen_ext16u_tl(dst, src);
        }
        return dst;
#ifdef TARGET_X86_64
592
    case MO_32:
593 594 595 596 597 598 599
        if (sign) {
            tcg_gen_ext32s_tl(dst, src);
        } else {
            tcg_gen_ext32u_tl(dst, src);
        }
        return dst;
#endif
600
    default:
601
        return src;
602 603
    }
}
604

605
static void gen_extu(TCGMemOp ot, TCGv reg)
606 607 608 609
{
    gen_ext_tl(reg, reg, ot, false);
}

610
static void gen_exts(TCGMemOp ot, TCGv reg)
611
{
612
    gen_ext_tl(reg, reg, ot, true);
613
}
B
bellard 已提交
614

615
static inline void gen_op_jnz_ecx(TCGMemOp size, int label1)
616
{
617
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
618
    gen_extu(size, cpu_tmp0);
P
pbrook 已提交
619
    tcg_gen_brcondi_tl(TCG_COND_NE, cpu_tmp0, 0, label1);
620 621
}

622
static inline void gen_op_jz_ecx(TCGMemOp size, int label1)
623
{
624
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
625
    gen_extu(size, cpu_tmp0);
P
pbrook 已提交
626
    tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, label1);
627
}
B
bellard 已提交
628

629
static void gen_helper_in_func(TCGMemOp ot, TCGv v, TCGv_i32 n)
P
pbrook 已提交
630 631
{
    switch (ot) {
632
    case MO_8:
633 634
        gen_helper_inb(v, n);
        break;
635
    case MO_16:
636 637
        gen_helper_inw(v, n);
        break;
638
    case MO_32:
639 640
        gen_helper_inl(v, n);
        break;
641 642
    default:
        tcg_abort();
P
pbrook 已提交
643 644
    }
}
B
bellard 已提交
645

646
static void gen_helper_out_func(TCGMemOp ot, TCGv_i32 v, TCGv_i32 n)
P
pbrook 已提交
647 648
{
    switch (ot) {
649
    case MO_8:
650 651
        gen_helper_outb(v, n);
        break;
652
    case MO_16:
653 654
        gen_helper_outw(v, n);
        break;
655
    case MO_32:
656 657
        gen_helper_outl(v, n);
        break;
658 659
    default:
        tcg_abort();
P
pbrook 已提交
660 661
    }
}
662

663
static void gen_check_io(DisasContext *s, TCGMemOp ot, target_ulong cur_eip,
664
                         uint32_t svm_flags)
665
{
666 667 668 669
    int state_saved;
    target_ulong next_eip;

    state_saved = 0;
670
    if (s->pe && (s->cpl > s->iopl || s->vm86)) {
671
        gen_update_cc_op(s);
B
bellard 已提交
672
        gen_jmp_im(cur_eip);
673
        state_saved = 1;
674
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
675
        switch (ot) {
676
        case MO_8:
B
Blue Swirl 已提交
677 678
            gen_helper_check_iob(cpu_env, cpu_tmp2_i32);
            break;
679
        case MO_16:
B
Blue Swirl 已提交
680 681
            gen_helper_check_iow(cpu_env, cpu_tmp2_i32);
            break;
682
        case MO_32:
B
Blue Swirl 已提交
683 684
            gen_helper_check_iol(cpu_env, cpu_tmp2_i32);
            break;
685 686
        default:
            tcg_abort();
P
pbrook 已提交
687
        }
688
    }
B
bellard 已提交
689
    if(s->flags & HF_SVMI_MASK) {
690
        if (!state_saved) {
691
            gen_update_cc_op(s);
692 693 694 695
            gen_jmp_im(cur_eip);
        }
        svm_flags |= (1 << (4 + ot));
        next_eip = s->pc - s->cs_base;
696
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
697 698
        gen_helper_svm_check_io(cpu_env, cpu_tmp2_i32,
                                tcg_const_i32(svm_flags),
P
pbrook 已提交
699
                                tcg_const_i32(next_eip - cur_eip));
700 701 702
    }
}

703
static inline void gen_movs(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
704 705
{
    gen_string_movl_A0_ESI(s);
706
    gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
707
    gen_string_movl_A0_EDI(s);
708
    gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
709
    gen_op_movl_T0_Dshift(ot);
710 711
    gen_op_add_reg_T0(s->aflag, R_ESI);
    gen_op_add_reg_T0(s->aflag, R_EDI);
B
bellard 已提交
712 713
}

714 715 716 717 718 719 720 721 722 723 724
static void gen_op_update1_cc(void)
{
    tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
}

static void gen_op_update2_cc(void)
{
    tcg_gen_mov_tl(cpu_cc_src, cpu_T[1]);
    tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
}

725 726 727 728 729 730 731
static void gen_op_update3_cc(TCGv reg)
{
    tcg_gen_mov_tl(cpu_cc_src2, reg);
    tcg_gen_mov_tl(cpu_cc_src, cpu_T[1]);
    tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
}

732 733 734 735 736 737 738 739
static inline void gen_op_testl_T0_T1_cc(void)
{
    tcg_gen_and_tl(cpu_cc_dst, cpu_T[0], cpu_T[1]);
}

static void gen_op_update_neg_cc(void)
{
    tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
740 741
    tcg_gen_neg_tl(cpu_cc_src, cpu_T[0]);
    tcg_gen_movi_tl(cpu_cc_srcT, 0);
742 743
}

744 745
/* compute all eflags to cc_src */
static void gen_compute_eflags(DisasContext *s)
746
{
747
    TCGv zero, dst, src1, src2;
748 749
    int live, dead;

750 751 752
    if (s->cc_op == CC_OP_EFLAGS) {
        return;
    }
R
Richard Henderson 已提交
753
    if (s->cc_op == CC_OP_CLR) {
754
        tcg_gen_movi_tl(cpu_cc_src, CC_Z | CC_P);
R
Richard Henderson 已提交
755 756 757
        set_cc_op(s, CC_OP_EFLAGS);
        return;
    }
758 759 760 761

    TCGV_UNUSED(zero);
    dst = cpu_cc_dst;
    src1 = cpu_cc_src;
762
    src2 = cpu_cc_src2;
763 764 765

    /* Take care to not read values that are not live.  */
    live = cc_op_live[s->cc_op] & ~USES_CC_SRCT;
766
    dead = live ^ (USES_CC_DST | USES_CC_SRC | USES_CC_SRC2);
767 768 769 770 771 772 773 774
    if (dead) {
        zero = tcg_const_tl(0);
        if (dead & USES_CC_DST) {
            dst = zero;
        }
        if (dead & USES_CC_SRC) {
            src1 = zero;
        }
775 776 777
        if (dead & USES_CC_SRC2) {
            src2 = zero;
        }
778 779
    }

780
    gen_update_cc_op(s);
781
    gen_helper_cc_compute_all(cpu_cc_src, dst, src1, src2, cpu_cc_op);
782
    set_cc_op(s, CC_OP_EFLAGS);
783 784 785 786

    if (dead) {
        tcg_temp_free(zero);
    }
787 788
}

789 790 791 792 793 794 795 796 797 798
typedef struct CCPrepare {
    TCGCond cond;
    TCGv reg;
    TCGv reg2;
    target_ulong imm;
    target_ulong mask;
    bool use_reg2;
    bool no_setcond;
} CCPrepare;

799
/* compute eflags.C to reg */
800
static CCPrepare gen_prepare_eflags_c(DisasContext *s, TCGv reg)
801 802
{
    TCGv t0, t1;
803
    int size, shift;
804 805 806

    switch (s->cc_op) {
    case CC_OP_SUBB ... CC_OP_SUBQ:
807
        /* (DATA_TYPE)CC_SRCT < (DATA_TYPE)CC_SRC */
808 809 810 811
        size = s->cc_op - CC_OP_SUBB;
        t1 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, false);
        /* If no temporary was used, be careful not to alias t1 and t0.  */
        t0 = TCGV_EQUAL(t1, cpu_cc_src) ? cpu_tmp0 : reg;
812
        tcg_gen_mov_tl(t0, cpu_cc_srcT);
813 814 815 816 817 818 819 820 821
        gen_extu(size, t0);
        goto add_sub;

    case CC_OP_ADDB ... CC_OP_ADDQ:
        /* (DATA_TYPE)CC_DST < (DATA_TYPE)CC_SRC */
        size = s->cc_op - CC_OP_ADDB;
        t1 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, false);
        t0 = gen_ext_tl(reg, cpu_cc_dst, size, false);
    add_sub:
822 823
        return (CCPrepare) { .cond = TCG_COND_LTU, .reg = t0,
                             .reg2 = t1, .mask = -1, .use_reg2 = true };
824 825

    case CC_OP_LOGICB ... CC_OP_LOGICQ:
R
Richard Henderson 已提交
826
    case CC_OP_CLR:
827
        return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
828 829 830

    case CC_OP_INCB ... CC_OP_INCQ:
    case CC_OP_DECB ... CC_OP_DECQ:
831 832
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = -1, .no_setcond = true };
833 834 835 836

    case CC_OP_SHLB ... CC_OP_SHLQ:
        /* (CC_SRC >> (DATA_BITS - 1)) & 1 */
        size = s->cc_op - CC_OP_SHLB;
837 838 839
        shift = (8 << size) - 1;
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = (target_ulong)1 << shift };
840 841

    case CC_OP_MULB ... CC_OP_MULQ:
842 843
        return (CCPrepare) { .cond = TCG_COND_NE,
                             .reg = cpu_cc_src, .mask = -1 };
844

845 846 847 848 849
    case CC_OP_BMILGB ... CC_OP_BMILGQ:
        size = s->cc_op - CC_OP_BMILGB;
        t0 = gen_ext_tl(reg, cpu_cc_src, size, false);
        return (CCPrepare) { .cond = TCG_COND_EQ, .reg = t0, .mask = -1 };

850 851 852 853 854
    case CC_OP_ADCX:
    case CC_OP_ADCOX:
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_dst,
                             .mask = -1, .no_setcond = true };

855 856 857
    case CC_OP_EFLAGS:
    case CC_OP_SARB ... CC_OP_SARQ:
        /* CC_SRC & 1 */
858 859
        return (CCPrepare) { .cond = TCG_COND_NE,
                             .reg = cpu_cc_src, .mask = CC_C };
860 861 862 863 864

    default:
       /* The need to compute only C from CC_OP_DYNAMIC is important
          in efficiently implementing e.g. INC at the start of a TB.  */
       gen_update_cc_op(s);
865 866
       gen_helper_cc_compute_c(reg, cpu_cc_dst, cpu_cc_src,
                               cpu_cc_src2, cpu_cc_op);
867 868
       return (CCPrepare) { .cond = TCG_COND_NE, .reg = reg,
                            .mask = -1, .no_setcond = true };
869 870 871
    }
}

872
/* compute eflags.P to reg */
873
static CCPrepare gen_prepare_eflags_p(DisasContext *s, TCGv reg)
874
{
875
    gen_compute_eflags(s);
876 877
    return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                         .mask = CC_P };
878 879 880
}

/* compute eflags.S to reg */
881
static CCPrepare gen_prepare_eflags_s(DisasContext *s, TCGv reg)
882
{
883 884 885 886 887
    switch (s->cc_op) {
    case CC_OP_DYNAMIC:
        gen_compute_eflags(s);
        /* FALLTHRU */
    case CC_OP_EFLAGS:
888 889 890
    case CC_OP_ADCX:
    case CC_OP_ADOX:
    case CC_OP_ADCOX:
891 892
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = CC_S };
R
Richard Henderson 已提交
893 894
    case CC_OP_CLR:
        return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
895 896
    default:
        {
897
            TCGMemOp size = (s->cc_op - CC_OP_ADDB) & 3;
898
            TCGv t0 = gen_ext_tl(reg, cpu_cc_dst, size, true);
899
            return (CCPrepare) { .cond = TCG_COND_LT, .reg = t0, .mask = -1 };
900 901
        }
    }
902 903 904
}

/* compute eflags.O to reg */
905
static CCPrepare gen_prepare_eflags_o(DisasContext *s, TCGv reg)
906
{
907 908 909 910 911
    switch (s->cc_op) {
    case CC_OP_ADOX:
    case CC_OP_ADCOX:
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src2,
                             .mask = -1, .no_setcond = true };
R
Richard Henderson 已提交
912 913
    case CC_OP_CLR:
        return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
914 915 916 917 918
    default:
        gen_compute_eflags(s);
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = CC_O };
    }
919 920 921
}

/* compute eflags.Z to reg */
922
static CCPrepare gen_prepare_eflags_z(DisasContext *s, TCGv reg)
923
{
924 925 926 927 928
    switch (s->cc_op) {
    case CC_OP_DYNAMIC:
        gen_compute_eflags(s);
        /* FALLTHRU */
    case CC_OP_EFLAGS:
929 930 931
    case CC_OP_ADCX:
    case CC_OP_ADOX:
    case CC_OP_ADCOX:
932 933
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = CC_Z };
R
Richard Henderson 已提交
934 935
    case CC_OP_CLR:
        return (CCPrepare) { .cond = TCG_COND_ALWAYS, .mask = -1 };
936 937
    default:
        {
938
            TCGMemOp size = (s->cc_op - CC_OP_ADDB) & 3;
939
            TCGv t0 = gen_ext_tl(reg, cpu_cc_dst, size, false);
940
            return (CCPrepare) { .cond = TCG_COND_EQ, .reg = t0, .mask = -1 };
941
        }
942 943 944
    }
}

945 946
/* perform a conditional store into register 'reg' according to jump opcode
   value 'b'. In the fast case, T0 is guaranted not to be used. */
947
static CCPrepare gen_prepare_cc(DisasContext *s, int b, TCGv reg)
948
{
949 950
    int inv, jcc_op, cond;
    TCGMemOp size;
951
    CCPrepare cc;
952 953 954
    TCGv t0;

    inv = b & 1;
955
    jcc_op = (b >> 1) & 7;
956 957

    switch (s->cc_op) {
958 959
    case CC_OP_SUBB ... CC_OP_SUBQ:
        /* We optimize relational operators for the cmp/jcc case.  */
960 961 962
        size = s->cc_op - CC_OP_SUBB;
        switch (jcc_op) {
        case JCC_BE:
963
            tcg_gen_mov_tl(cpu_tmp4, cpu_cc_srcT);
964 965
            gen_extu(size, cpu_tmp4);
            t0 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, false);
966 967
            cc = (CCPrepare) { .cond = TCG_COND_LEU, .reg = cpu_tmp4,
                               .reg2 = t0, .mask = -1, .use_reg2 = true };
968
            break;
969

970
        case JCC_L:
971
            cond = TCG_COND_LT;
972 973
            goto fast_jcc_l;
        case JCC_LE:
974
            cond = TCG_COND_LE;
975
        fast_jcc_l:
976
            tcg_gen_mov_tl(cpu_tmp4, cpu_cc_srcT);
977 978
            gen_exts(size, cpu_tmp4);
            t0 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, true);
979 980
            cc = (CCPrepare) { .cond = cond, .reg = cpu_tmp4,
                               .reg2 = t0, .mask = -1, .use_reg2 = true };
981
            break;
982

983
        default:
984
            goto slow_jcc;
985
        }
986
        break;
987

988 989
    default:
    slow_jcc:
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
        /* This actually generates good code for JC, JZ and JS.  */
        switch (jcc_op) {
        case JCC_O:
            cc = gen_prepare_eflags_o(s, reg);
            break;
        case JCC_B:
            cc = gen_prepare_eflags_c(s, reg);
            break;
        case JCC_Z:
            cc = gen_prepare_eflags_z(s, reg);
            break;
        case JCC_BE:
            gen_compute_eflags(s);
            cc = (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                               .mask = CC_Z | CC_C };
            break;
        case JCC_S:
            cc = gen_prepare_eflags_s(s, reg);
            break;
        case JCC_P:
            cc = gen_prepare_eflags_p(s, reg);
            break;
        case JCC_L:
            gen_compute_eflags(s);
            if (TCGV_EQUAL(reg, cpu_cc_src)) {
                reg = cpu_tmp0;
            }
            tcg_gen_shri_tl(reg, cpu_cc_src, 4); /* CC_O -> CC_S */
            tcg_gen_xor_tl(reg, reg, cpu_cc_src);
            cc = (CCPrepare) { .cond = TCG_COND_NE, .reg = reg,
                               .mask = CC_S };
            break;
        default:
        case JCC_LE:
            gen_compute_eflags(s);
            if (TCGV_EQUAL(reg, cpu_cc_src)) {
                reg = cpu_tmp0;
            }
            tcg_gen_shri_tl(reg, cpu_cc_src, 4); /* CC_O -> CC_S */
            tcg_gen_xor_tl(reg, reg, cpu_cc_src);
            cc = (CCPrepare) { .cond = TCG_COND_NE, .reg = reg,
                               .mask = CC_S | CC_Z };
            break;
        }
1034
        break;
1035
    }
1036 1037 1038 1039 1040

    if (inv) {
        cc.cond = tcg_invert_cond(cc.cond);
    }
    return cc;
1041 1042
}

1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
static void gen_setcc1(DisasContext *s, int b, TCGv reg)
{
    CCPrepare cc = gen_prepare_cc(s, b, reg);

    if (cc.no_setcond) {
        if (cc.cond == TCG_COND_EQ) {
            tcg_gen_xori_tl(reg, cc.reg, 1);
        } else {
            tcg_gen_mov_tl(reg, cc.reg);
        }
        return;
    }

    if (cc.cond == TCG_COND_NE && !cc.use_reg2 && cc.imm == 0 &&
        cc.mask != 0 && (cc.mask & (cc.mask - 1)) == 0) {
        tcg_gen_shri_tl(reg, cc.reg, ctztl(cc.mask));
        tcg_gen_andi_tl(reg, reg, 1);
        return;
    }
    if (cc.mask != -1) {
        tcg_gen_andi_tl(reg, cc.reg, cc.mask);
        cc.reg = reg;
    }
    if (cc.use_reg2) {
        tcg_gen_setcond_tl(cc.cond, reg, cc.reg, cc.reg2);
    } else {
        tcg_gen_setcondi_tl(cc.cond, reg, cc.reg, cc.imm);
    }
}

static inline void gen_compute_eflags_c(DisasContext *s, TCGv reg)
{
    gen_setcc1(s, JCC_B << 1, reg);
}
1077

1078 1079
/* generate a conditional jump to label 'l1' according to jump opcode
   value 'b'. In the fast case, T0 is guaranted not to be used. */
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
static inline void gen_jcc1_noeob(DisasContext *s, int b, int l1)
{
    CCPrepare cc = gen_prepare_cc(s, b, cpu_T[0]);

    if (cc.mask != -1) {
        tcg_gen_andi_tl(cpu_T[0], cc.reg, cc.mask);
        cc.reg = cpu_T[0];
    }
    if (cc.use_reg2) {
        tcg_gen_brcond_tl(cc.cond, cc.reg, cc.reg2, l1);
    } else {
        tcg_gen_brcondi_tl(cc.cond, cc.reg, cc.imm, l1);
    }
}

/* Generate a conditional jump to label 'l1' according to jump opcode
   value 'b'. In the fast case, T0 is guaranted not to be used.
   A translation block must end soon.  */
1098
static inline void gen_jcc1(DisasContext *s, int b, int l1)
1099
{
1100
    CCPrepare cc = gen_prepare_cc(s, b, cpu_T[0]);
1101

1102
    gen_update_cc_op(s);
1103 1104 1105 1106
    if (cc.mask != -1) {
        tcg_gen_andi_tl(cpu_T[0], cc.reg, cc.mask);
        cc.reg = cpu_T[0];
    }
1107
    set_cc_op(s, CC_OP_DYNAMIC);
1108 1109 1110 1111
    if (cc.use_reg2) {
        tcg_gen_brcond_tl(cc.cond, cc.reg, cc.reg2, l1);
    } else {
        tcg_gen_brcondi_tl(cc.cond, cc.reg, cc.imm, l1);
1112 1113 1114
    }
}

B
bellard 已提交
1115 1116 1117
/* XXX: does not work with gdbstub "ice" single step - not a
   serious problem */
static int gen_jz_ecx_string(DisasContext *s, target_ulong next_eip)
B
bellard 已提交
1118
{
B
bellard 已提交
1119 1120 1121 1122
    int l1, l2;

    l1 = gen_new_label();
    l2 = gen_new_label();
1123
    gen_op_jnz_ecx(s->aflag, l1);
B
bellard 已提交
1124 1125 1126 1127
    gen_set_label(l2);
    gen_jmp_tb(s, next_eip, 1);
    gen_set_label(l1);
    return l2;
B
bellard 已提交
1128 1129
}

1130
static inline void gen_stos(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
1131
{
1132
    gen_op_mov_v_reg(MO_32, cpu_T[0], R_EAX);
B
bellard 已提交
1133
    gen_string_movl_A0_EDI(s);
1134
    gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
1135
    gen_op_movl_T0_Dshift(ot);
1136
    gen_op_add_reg_T0(s->aflag, R_EDI);
B
bellard 已提交
1137 1138
}

1139
static inline void gen_lods(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
1140 1141
{
    gen_string_movl_A0_ESI(s);
1142
    gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
1143
    gen_op_mov_reg_v(ot, R_EAX, cpu_T[0]);
1144
    gen_op_movl_T0_Dshift(ot);
1145
    gen_op_add_reg_T0(s->aflag, R_ESI);
B
bellard 已提交
1146 1147
}

1148
static inline void gen_scas(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
1149 1150
{
    gen_string_movl_A0_EDI(s);
1151
    gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
1152
    gen_op(s, OP_CMPL, ot, R_EAX);
1153
    gen_op_movl_T0_Dshift(ot);
1154
    gen_op_add_reg_T0(s->aflag, R_EDI);
B
bellard 已提交
1155 1156
}

1157
static inline void gen_cmps(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
1158 1159
{
    gen_string_movl_A0_EDI(s);
1160
    gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
1161 1162
    gen_string_movl_A0_ESI(s);
    gen_op(s, OP_CMPL, ot, OR_TMP0);
1163
    gen_op_movl_T0_Dshift(ot);
1164 1165
    gen_op_add_reg_T0(s->aflag, R_ESI);
    gen_op_add_reg_T0(s->aflag, R_EDI);
B
bellard 已提交
1166 1167
}

1168
static inline void gen_ins(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
1169
{
P
pbrook 已提交
1170 1171
    if (use_icount)
        gen_io_start();
B
bellard 已提交
1172
    gen_string_movl_A0_EDI(s);
1173 1174
    /* Note: we must do this dummy write first to be restartable in
       case of page fault. */
1175
    tcg_gen_movi_tl(cpu_T[0], 0);
1176
    gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
1177
    tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_EDX]);
1178
    tcg_gen_andi_i32(cpu_tmp2_i32, cpu_tmp2_i32, 0xffff);
P
pbrook 已提交
1179
    gen_helper_in_func(ot, cpu_T[0], cpu_tmp2_i32);
1180
    gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
1181
    gen_op_movl_T0_Dshift(ot);
1182
    gen_op_add_reg_T0(s->aflag, R_EDI);
P
pbrook 已提交
1183 1184
    if (use_icount)
        gen_io_end();
B
bellard 已提交
1185 1186
}

1187
static inline void gen_outs(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
1188
{
P
pbrook 已提交
1189 1190
    if (use_icount)
        gen_io_start();
B
bellard 已提交
1191
    gen_string_movl_A0_ESI(s);
1192
    gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
1193

1194
    tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_EDX]);
1195 1196
    tcg_gen_andi_i32(cpu_tmp2_i32, cpu_tmp2_i32, 0xffff);
    tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[0]);
P
pbrook 已提交
1197
    gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
1198

1199
    gen_op_movl_T0_Dshift(ot);
1200
    gen_op_add_reg_T0(s->aflag, R_ESI);
P
pbrook 已提交
1201 1202
    if (use_icount)
        gen_io_end();
B
bellard 已提交
1203 1204 1205 1206 1207
}

/* same method as Valgrind : we generate jumps to current or next
   instruction */
#define GEN_REPZ(op)                                                          \
1208
static inline void gen_repz_ ## op(DisasContext *s, TCGMemOp ot,              \
B
bellard 已提交
1209
                                 target_ulong cur_eip, target_ulong next_eip) \
B
bellard 已提交
1210
{                                                                             \
B
bellard 已提交
1211
    int l2;\
B
bellard 已提交
1212
    gen_update_cc_op(s);                                                      \
B
bellard 已提交
1213
    l2 = gen_jz_ecx_string(s, next_eip);                                      \
B
bellard 已提交
1214
    gen_ ## op(s, ot);                                                        \
1215
    gen_op_add_reg_im(s->aflag, R_ECX, -1);                                   \
B
bellard 已提交
1216 1217 1218
    /* a loop would cause two single step exceptions if ECX = 1               \
       before rep string_insn */                                              \
    if (!s->jmp_opt)                                                          \
1219
        gen_op_jz_ecx(s->aflag, l2);                                          \
B
bellard 已提交
1220 1221 1222 1223
    gen_jmp(s, cur_eip);                                                      \
}

#define GEN_REPZ2(op)                                                         \
1224
static inline void gen_repz_ ## op(DisasContext *s, TCGMemOp ot,              \
B
bellard 已提交
1225 1226
                                   target_ulong cur_eip,                      \
                                   target_ulong next_eip,                     \
B
bellard 已提交
1227 1228
                                   int nz)                                    \
{                                                                             \
B
bellard 已提交
1229
    int l2;\
B
bellard 已提交
1230
    gen_update_cc_op(s);                                                      \
B
bellard 已提交
1231
    l2 = gen_jz_ecx_string(s, next_eip);                                      \
B
bellard 已提交
1232
    gen_ ## op(s, ot);                                                        \
1233
    gen_op_add_reg_im(s->aflag, R_ECX, -1);                                   \
1234
    gen_update_cc_op(s);                                                      \
1235
    gen_jcc1(s, (JCC_Z << 1) | (nz ^ 1), l2);                                 \
B
bellard 已提交
1236
    if (!s->jmp_opt)                                                          \
1237
        gen_op_jz_ecx(s->aflag, l2);                                          \
B
bellard 已提交
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
    gen_jmp(s, cur_eip);                                                      \
}

GEN_REPZ(movs)
GEN_REPZ(stos)
GEN_REPZ(lods)
GEN_REPZ(ins)
GEN_REPZ(outs)
GEN_REPZ2(scas)
GEN_REPZ2(cmps)

P
pbrook 已提交
1249 1250 1251
static void gen_helper_fp_arith_ST0_FT0(int op)
{
    switch (op) {
B
Blue Swirl 已提交
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
    case 0:
        gen_helper_fadd_ST0_FT0(cpu_env);
        break;
    case 1:
        gen_helper_fmul_ST0_FT0(cpu_env);
        break;
    case 2:
        gen_helper_fcom_ST0_FT0(cpu_env);
        break;
    case 3:
        gen_helper_fcom_ST0_FT0(cpu_env);
        break;
    case 4:
        gen_helper_fsub_ST0_FT0(cpu_env);
        break;
    case 5:
        gen_helper_fsubr_ST0_FT0(cpu_env);
        break;
    case 6:
        gen_helper_fdiv_ST0_FT0(cpu_env);
        break;
    case 7:
        gen_helper_fdivr_ST0_FT0(cpu_env);
        break;
P
pbrook 已提交
1276 1277
    }
}
B
bellard 已提交
1278 1279

/* NOTE the exception in "r" op ordering */
P
pbrook 已提交
1280 1281 1282 1283
static void gen_helper_fp_arith_STN_ST0(int op, int opreg)
{
    TCGv_i32 tmp = tcg_const_i32(opreg);
    switch (op) {
B
Blue Swirl 已提交
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
    case 0:
        gen_helper_fadd_STN_ST0(cpu_env, tmp);
        break;
    case 1:
        gen_helper_fmul_STN_ST0(cpu_env, tmp);
        break;
    case 4:
        gen_helper_fsubr_STN_ST0(cpu_env, tmp);
        break;
    case 5:
        gen_helper_fsub_STN_ST0(cpu_env, tmp);
        break;
    case 6:
        gen_helper_fdivr_STN_ST0(cpu_env, tmp);
        break;
    case 7:
        gen_helper_fdiv_STN_ST0(cpu_env, tmp);
        break;
P
pbrook 已提交
1302 1303
    }
}
B
bellard 已提交
1304 1305

/* if d == OR_TMP0, it means memory operand (address in A0) */
1306
static void gen_op(DisasContext *s1, int op, TCGMemOp ot, int d)
B
bellard 已提交
1307 1308
{
    if (d != OR_TMP0) {
1309
        gen_op_mov_v_reg(ot, cpu_T[0], d);
B
bellard 已提交
1310
    } else {
1311
        gen_op_ld_v(s1, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
1312 1313 1314
    }
    switch(op) {
    case OP_ADCL:
1315
        gen_compute_eflags_c(s1, cpu_tmp4);
B
bellard 已提交
1316 1317
        tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
        tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_tmp4);
1318
        gen_op_st_rm_T0_A0(s1, ot, d);
1319 1320
        gen_op_update3_cc(cpu_tmp4);
        set_cc_op(s1, CC_OP_ADCB + ot);
B
bellard 已提交
1321
        break;
B
bellard 已提交
1322
    case OP_SBBL:
1323
        gen_compute_eflags_c(s1, cpu_tmp4);
B
bellard 已提交
1324 1325
        tcg_gen_sub_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
        tcg_gen_sub_tl(cpu_T[0], cpu_T[0], cpu_tmp4);
1326
        gen_op_st_rm_T0_A0(s1, ot, d);
1327 1328
        gen_op_update3_cc(cpu_tmp4);
        set_cc_op(s1, CC_OP_SBBB + ot);
B
bellard 已提交
1329
        break;
B
bellard 已提交
1330
    case OP_ADDL:
1331
        tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1332
        gen_op_st_rm_T0_A0(s1, ot, d);
B
bellard 已提交
1333
        gen_op_update2_cc();
1334
        set_cc_op(s1, CC_OP_ADDB + ot);
B
bellard 已提交
1335 1336
        break;
    case OP_SUBL:
1337
        tcg_gen_mov_tl(cpu_cc_srcT, cpu_T[0]);
B
bellard 已提交
1338
        tcg_gen_sub_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1339
        gen_op_st_rm_T0_A0(s1, ot, d);
B
bellard 已提交
1340
        gen_op_update2_cc();
1341
        set_cc_op(s1, CC_OP_SUBB + ot);
B
bellard 已提交
1342 1343 1344
        break;
    default:
    case OP_ANDL:
B
bellard 已提交
1345
        tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1346
        gen_op_st_rm_T0_A0(s1, ot, d);
B
bellard 已提交
1347
        gen_op_update1_cc();
1348
        set_cc_op(s1, CC_OP_LOGICB + ot);
B
bellard 已提交
1349
        break;
B
bellard 已提交
1350
    case OP_ORL:
B
bellard 已提交
1351
        tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1352
        gen_op_st_rm_T0_A0(s1, ot, d);
B
bellard 已提交
1353
        gen_op_update1_cc();
1354
        set_cc_op(s1, CC_OP_LOGICB + ot);
B
bellard 已提交
1355
        break;
B
bellard 已提交
1356
    case OP_XORL:
B
bellard 已提交
1357
        tcg_gen_xor_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1358
        gen_op_st_rm_T0_A0(s1, ot, d);
B
bellard 已提交
1359
        gen_op_update1_cc();
1360
        set_cc_op(s1, CC_OP_LOGICB + ot);
B
bellard 已提交
1361 1362
        break;
    case OP_CMPL:
1363
        tcg_gen_mov_tl(cpu_cc_src, cpu_T[1]);
1364
        tcg_gen_mov_tl(cpu_cc_srcT, cpu_T[0]);
1365
        tcg_gen_sub_tl(cpu_cc_dst, cpu_T[0], cpu_T[1]);
1366
        set_cc_op(s1, CC_OP_SUBB + ot);
B
bellard 已提交
1367 1368
        break;
    }
1369 1370
}

B
bellard 已提交
1371
/* if d == OR_TMP0, it means memory operand (address in A0) */
1372
static void gen_inc(DisasContext *s1, TCGMemOp ot, int d, int c)
B
bellard 已提交
1373
{
1374
    if (d != OR_TMP0) {
1375
        gen_op_mov_v_reg(ot, cpu_T[0], d);
1376 1377 1378
    } else {
        gen_op_ld_v(s1, ot, cpu_T[0], cpu_A0);
    }
1379
    gen_compute_eflags_c(s1, cpu_cc_src);
B
bellard 已提交
1380
    if (c > 0) {
1381
        tcg_gen_addi_tl(cpu_T[0], cpu_T[0], 1);
1382
        set_cc_op(s1, CC_OP_INCB + ot);
B
bellard 已提交
1383
    } else {
1384
        tcg_gen_addi_tl(cpu_T[0], cpu_T[0], -1);
1385
        set_cc_op(s1, CC_OP_DECB + ot);
B
bellard 已提交
1386
    }
1387
    gen_op_st_rm_T0_A0(s1, ot, d);
B
bellard 已提交
1388
    tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
B
bellard 已提交
1389 1390
}

1391 1392
static void gen_shift_flags(DisasContext *s, TCGMemOp ot, TCGv result,
                            TCGv shm1, TCGv count, bool is_right)
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
{
    TCGv_i32 z32, s32, oldop;
    TCGv z_tl;

    /* Store the results into the CC variables.  If we know that the
       variable must be dead, store unconditionally.  Otherwise we'll
       need to not disrupt the current contents.  */
    z_tl = tcg_const_tl(0);
    if (cc_op_live[s->cc_op] & USES_CC_DST) {
        tcg_gen_movcond_tl(TCG_COND_NE, cpu_cc_dst, count, z_tl,
                           result, cpu_cc_dst);
    } else {
        tcg_gen_mov_tl(cpu_cc_dst, result);
    }
    if (cc_op_live[s->cc_op] & USES_CC_SRC) {
        tcg_gen_movcond_tl(TCG_COND_NE, cpu_cc_src, count, z_tl,
                           shm1, cpu_cc_src);
    } else {
        tcg_gen_mov_tl(cpu_cc_src, shm1);
    }
    tcg_temp_free(z_tl);

    /* Get the two potential CC_OP values into temporaries.  */
    tcg_gen_movi_i32(cpu_tmp2_i32, (is_right ? CC_OP_SARB : CC_OP_SHLB) + ot);
    if (s->cc_op == CC_OP_DYNAMIC) {
        oldop = cpu_cc_op;
    } else {
        tcg_gen_movi_i32(cpu_tmp3_i32, s->cc_op);
        oldop = cpu_tmp3_i32;
    }

    /* Conditionally store the CC_OP value.  */
    z32 = tcg_const_i32(0);
    s32 = tcg_temp_new_i32();
    tcg_gen_trunc_tl_i32(s32, count);
    tcg_gen_movcond_i32(TCG_COND_NE, cpu_cc_op, s32, z32, cpu_tmp2_i32, oldop);
    tcg_temp_free_i32(z32);
    tcg_temp_free_i32(s32);

    /* The CC_OP value is no longer predictable.  */
    set_cc_op(s, CC_OP_DYNAMIC);
}

1436
static void gen_shift_rm_T1(DisasContext *s, TCGMemOp ot, int op1,
1437
                            int is_right, int is_arith)
B
bellard 已提交
1438
{
1439
    target_ulong mask = (ot == MO_64 ? 0x3f : 0x1f);
1440

1441
    /* load */
1442
    if (op1 == OR_TMP0) {
1443
        gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
1444
    } else {
1445
        gen_op_mov_v_reg(ot, cpu_T[0], op1);
1446
    }
1447

1448 1449
    tcg_gen_andi_tl(cpu_T[1], cpu_T[1], mask);
    tcg_gen_subi_tl(cpu_tmp0, cpu_T[1], 1);
1450 1451 1452

    if (is_right) {
        if (is_arith) {
B
bellard 已提交
1453
            gen_exts(ot, cpu_T[0]);
1454 1455
            tcg_gen_sar_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
            tcg_gen_sar_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1456
        } else {
B
bellard 已提交
1457
            gen_extu(ot, cpu_T[0]);
1458 1459
            tcg_gen_shr_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
            tcg_gen_shr_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1460 1461
        }
    } else {
1462 1463
        tcg_gen_shl_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
        tcg_gen_shl_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1464 1465 1466
    }

    /* store */
1467
    gen_op_st_rm_T0_A0(s, ot, op1);
1468

1469
    gen_shift_flags(s, ot, cpu_T[0], cpu_tmp0, cpu_T[1], is_right);
1470 1471
}

1472
static void gen_shift_rm_im(DisasContext *s, TCGMemOp ot, int op1, int op2,
B
bellard 已提交
1473 1474
                            int is_right, int is_arith)
{
1475
    int mask = (ot == MO_64 ? 0x3f : 0x1f);
B
bellard 已提交
1476 1477 1478

    /* load */
    if (op1 == OR_TMP0)
1479
        gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
1480
    else
1481
        gen_op_mov_v_reg(ot, cpu_T[0], op1);
B
bellard 已提交
1482 1483 1484 1485 1486 1487

    op2 &= mask;
    if (op2 != 0) {
        if (is_right) {
            if (is_arith) {
                gen_exts(ot, cpu_T[0]);
B
bellard 已提交
1488
                tcg_gen_sari_tl(cpu_tmp4, cpu_T[0], op2 - 1);
B
bellard 已提交
1489 1490 1491
                tcg_gen_sari_tl(cpu_T[0], cpu_T[0], op2);
            } else {
                gen_extu(ot, cpu_T[0]);
B
bellard 已提交
1492
                tcg_gen_shri_tl(cpu_tmp4, cpu_T[0], op2 - 1);
B
bellard 已提交
1493 1494 1495
                tcg_gen_shri_tl(cpu_T[0], cpu_T[0], op2);
            }
        } else {
B
bellard 已提交
1496
            tcg_gen_shli_tl(cpu_tmp4, cpu_T[0], op2 - 1);
B
bellard 已提交
1497 1498 1499 1500 1501
            tcg_gen_shli_tl(cpu_T[0], cpu_T[0], op2);
        }
    }

    /* store */
1502 1503
    gen_op_st_rm_T0_A0(s, ot, op1);

B
bellard 已提交
1504 1505
    /* update eflags if non zero shift */
    if (op2 != 0) {
B
bellard 已提交
1506
        tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
B
bellard 已提交
1507
        tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
1508
        set_cc_op(s, (is_right ? CC_OP_SARB : CC_OP_SHLB) + ot);
B
bellard 已提交
1509 1510 1511
    }
}

1512
static void gen_rot_rm_T1(DisasContext *s, TCGMemOp ot, int op1, int is_right)
1513
{
1514
    target_ulong mask = (ot == MO_64 ? 0x3f : 0x1f);
1515
    TCGv_i32 t0, t1;
1516 1517

    /* load */
1518
    if (op1 == OR_TMP0) {
1519
        gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
1520
    } else {
1521
        gen_op_mov_v_reg(ot, cpu_T[0], op1);
1522
    }
1523

1524
    tcg_gen_andi_tl(cpu_T[1], cpu_T[1], mask);
1525

1526
    switch (ot) {
1527
    case MO_8:
1528 1529 1530 1531
        /* Replicate the 8-bit input so that a 32-bit rotate works.  */
        tcg_gen_ext8u_tl(cpu_T[0], cpu_T[0]);
        tcg_gen_muli_tl(cpu_T[0], cpu_T[0], 0x01010101);
        goto do_long;
1532
    case MO_16:
1533 1534 1535 1536 1537
        /* Replicate the 16-bit input so that a 32-bit rotate works.  */
        tcg_gen_deposit_tl(cpu_T[0], cpu_T[0], cpu_T[0], 16, 16);
        goto do_long;
    do_long:
#ifdef TARGET_X86_64
1538
    case MO_32:
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
        tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
        if (is_right) {
            tcg_gen_rotr_i32(cpu_tmp2_i32, cpu_tmp2_i32, cpu_tmp3_i32);
        } else {
            tcg_gen_rotl_i32(cpu_tmp2_i32, cpu_tmp2_i32, cpu_tmp3_i32);
        }
        tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
        break;
#endif
    default:
        if (is_right) {
            tcg_gen_rotr_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
        } else {
            tcg_gen_rotl_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
        }
        break;
1556 1557 1558
    }

    /* store */
1559
    gen_op_st_rm_T0_A0(s, ot, op1);
1560

1561 1562
    /* We'll need the flags computed into CC_SRC.  */
    gen_compute_eflags(s);
1563

1564 1565 1566 1567
    /* The value that was "rotated out" is now present at the other end
       of the word.  Compute C into CC_DST and O into CC_SRC2.  Note that
       since we've computed the flags into CC_SRC, these variables are
       currently dead.  */
1568
    if (is_right) {
1569 1570
        tcg_gen_shri_tl(cpu_cc_src2, cpu_T[0], mask - 1);
        tcg_gen_shri_tl(cpu_cc_dst, cpu_T[0], mask);
P
Pavel Dovgaluk 已提交
1571
        tcg_gen_andi_tl(cpu_cc_dst, cpu_cc_dst, 1);
1572 1573 1574
    } else {
        tcg_gen_shri_tl(cpu_cc_src2, cpu_T[0], mask);
        tcg_gen_andi_tl(cpu_cc_dst, cpu_T[0], 1);
1575
    }
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
    tcg_gen_andi_tl(cpu_cc_src2, cpu_cc_src2, 1);
    tcg_gen_xor_tl(cpu_cc_src2, cpu_cc_src2, cpu_cc_dst);

    /* Now conditionally store the new CC_OP value.  If the shift count
       is 0 we keep the CC_OP_EFLAGS setting so that only CC_SRC is live.
       Otherwise reuse CC_OP_ADCOX which have the C and O flags split out
       exactly as we computed above.  */
    t0 = tcg_const_i32(0);
    t1 = tcg_temp_new_i32();
    tcg_gen_trunc_tl_i32(t1, cpu_T[1]);
    tcg_gen_movi_i32(cpu_tmp2_i32, CC_OP_ADCOX); 
    tcg_gen_movi_i32(cpu_tmp3_i32, CC_OP_EFLAGS);
    tcg_gen_movcond_i32(TCG_COND_NE, cpu_cc_op, t1, t0,
                        cpu_tmp2_i32, cpu_tmp3_i32);
    tcg_temp_free_i32(t0);
    tcg_temp_free_i32(t1);

    /* The CC_OP value is no longer predictable.  */ 
    set_cc_op(s, CC_OP_DYNAMIC);
1595 1596
}

1597
static void gen_rot_rm_im(DisasContext *s, TCGMemOp ot, int op1, int op2,
M
malc 已提交
1598 1599
                          int is_right)
{
1600
    int mask = (ot == MO_64 ? 0x3f : 0x1f);
1601
    int shift;
M
malc 已提交
1602 1603 1604

    /* load */
    if (op1 == OR_TMP0) {
1605
        gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
M
malc 已提交
1606
    } else {
1607
        gen_op_mov_v_reg(ot, cpu_T[0], op1);
M
malc 已提交
1608 1609 1610 1611
    }

    op2 &= mask;
    if (op2 != 0) {
1612 1613
        switch (ot) {
#ifdef TARGET_X86_64
1614
        case MO_32:
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
            tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
            if (is_right) {
                tcg_gen_rotri_i32(cpu_tmp2_i32, cpu_tmp2_i32, op2);
            } else {
                tcg_gen_rotli_i32(cpu_tmp2_i32, cpu_tmp2_i32, op2);
            }
            tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
            break;
#endif
        default:
            if (is_right) {
                tcg_gen_rotri_tl(cpu_T[0], cpu_T[0], op2);
            } else {
                tcg_gen_rotli_tl(cpu_T[0], cpu_T[0], op2);
            }
            break;
1631
        case MO_8:
1632 1633
            mask = 7;
            goto do_shifts;
1634
        case MO_16:
1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645
            mask = 15;
        do_shifts:
            shift = op2 & mask;
            if (is_right) {
                shift = mask + 1 - shift;
            }
            gen_extu(ot, cpu_T[0]);
            tcg_gen_shli_tl(cpu_tmp0, cpu_T[0], shift);
            tcg_gen_shri_tl(cpu_T[0], cpu_T[0], mask + 1 - shift);
            tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
            break;
M
malc 已提交
1646 1647 1648 1649
        }
    }

    /* store */
1650
    gen_op_st_rm_T0_A0(s, ot, op1);
M
malc 已提交
1651 1652

    if (op2 != 0) {
1653
        /* Compute the flags into CC_SRC.  */
1654
        gen_compute_eflags(s);
1655

1656 1657 1658 1659
        /* The value that was "rotated out" is now present at the other end
           of the word.  Compute C into CC_DST and O into CC_SRC2.  Note that
           since we've computed the flags into CC_SRC, these variables are
           currently dead.  */
M
malc 已提交
1660
        if (is_right) {
1661 1662
            tcg_gen_shri_tl(cpu_cc_src2, cpu_T[0], mask - 1);
            tcg_gen_shri_tl(cpu_cc_dst, cpu_T[0], mask);
1663
            tcg_gen_andi_tl(cpu_cc_dst, cpu_cc_dst, 1);
1664 1665 1666
        } else {
            tcg_gen_shri_tl(cpu_cc_src2, cpu_T[0], mask);
            tcg_gen_andi_tl(cpu_cc_dst, cpu_T[0], 1);
M
malc 已提交
1667
        }
1668 1669 1670
        tcg_gen_andi_tl(cpu_cc_src2, cpu_cc_src2, 1);
        tcg_gen_xor_tl(cpu_cc_src2, cpu_cc_src2, cpu_cc_dst);
        set_cc_op(s, CC_OP_ADCOX);
M
malc 已提交
1671 1672 1673
    }
}

1674
/* XXX: add faster immediate = 1 case */
1675
static void gen_rotc_rm_T1(DisasContext *s, TCGMemOp ot, int op1,
1676 1677
                           int is_right)
{
1678
    gen_compute_eflags(s);
1679
    assert(s->cc_op == CC_OP_EFLAGS);
1680 1681 1682

    /* load */
    if (op1 == OR_TMP0)
1683
        gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
1684
    else
1685
        gen_op_mov_v_reg(ot, cpu_T[0], op1);
1686
    
P
pbrook 已提交
1687 1688
    if (is_right) {
        switch (ot) {
1689
        case MO_8:
1690 1691
            gen_helper_rcrb(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
1692
        case MO_16:
1693 1694
            gen_helper_rcrw(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
1695
        case MO_32:
1696 1697
            gen_helper_rcrl(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1698
#ifdef TARGET_X86_64
1699
        case MO_64:
1700 1701
            gen_helper_rcrq(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1702
#endif
1703 1704
        default:
            tcg_abort();
P
pbrook 已提交
1705 1706 1707
        }
    } else {
        switch (ot) {
1708
        case MO_8:
1709 1710
            gen_helper_rclb(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
1711
        case MO_16:
1712 1713
            gen_helper_rclw(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
1714
        case MO_32:
1715 1716
            gen_helper_rcll(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1717
#ifdef TARGET_X86_64
1718
        case MO_64:
1719 1720
            gen_helper_rclq(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1721
#endif
1722 1723
        default:
            tcg_abort();
P
pbrook 已提交
1724 1725
        }
    }
1726
    /* store */
1727
    gen_op_st_rm_T0_A0(s, ot, op1);
1728 1729 1730
}

/* XXX: add faster immediate case */
1731
static void gen_shiftd_rm_T1(DisasContext *s, TCGMemOp ot, int op1,
1732
                             bool is_right, TCGv count_in)
1733
{
1734
    target_ulong mask = (ot == MO_64 ? 63 : 31);
1735
    TCGv count;
1736 1737

    /* load */
1738
    if (op1 == OR_TMP0) {
1739
        gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
1740
    } else {
1741
        gen_op_mov_v_reg(ot, cpu_T[0], op1);
1742
    }
1743

1744 1745
    count = tcg_temp_new();
    tcg_gen_andi_tl(count, count_in, mask);
1746

1747
    switch (ot) {
1748
    case MO_16:
1749 1750 1751
        /* Note: we implement the Intel behaviour for shift count > 16.
           This means "shrdw C, B, A" shifts A:B:A >> C.  Build the B:A
           portion by constructing it as a 32-bit value.  */
1752
        if (is_right) {
1753 1754 1755
            tcg_gen_deposit_tl(cpu_tmp0, cpu_T[0], cpu_T[1], 16, 16);
            tcg_gen_mov_tl(cpu_T[1], cpu_T[0]);
            tcg_gen_mov_tl(cpu_T[0], cpu_tmp0);
1756
        } else {
1757
            tcg_gen_deposit_tl(cpu_T[1], cpu_T[0], cpu_T[1], 16, 16);
1758
        }
1759 1760
        /* FALLTHRU */
#ifdef TARGET_X86_64
1761
    case MO_32:
1762 1763
        /* Concatenate the two 32-bit values and use a 64-bit shift.  */
        tcg_gen_subi_tl(cpu_tmp0, count, 1);
1764
        if (is_right) {
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780
            tcg_gen_concat_tl_i64(cpu_T[0], cpu_T[0], cpu_T[1]);
            tcg_gen_shr_i64(cpu_tmp0, cpu_T[0], cpu_tmp0);
            tcg_gen_shr_i64(cpu_T[0], cpu_T[0], count);
        } else {
            tcg_gen_concat_tl_i64(cpu_T[0], cpu_T[1], cpu_T[0]);
            tcg_gen_shl_i64(cpu_tmp0, cpu_T[0], cpu_tmp0);
            tcg_gen_shl_i64(cpu_T[0], cpu_T[0], count);
            tcg_gen_shri_i64(cpu_tmp0, cpu_tmp0, 32);
            tcg_gen_shri_i64(cpu_T[0], cpu_T[0], 32);
        }
        break;
#endif
    default:
        tcg_gen_subi_tl(cpu_tmp0, count, 1);
        if (is_right) {
            tcg_gen_shr_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
1781

1782 1783 1784
            tcg_gen_subfi_tl(cpu_tmp4, mask + 1, count);
            tcg_gen_shr_tl(cpu_T[0], cpu_T[0], count);
            tcg_gen_shl_tl(cpu_T[1], cpu_T[1], cpu_tmp4);
1785
        } else {
1786
            tcg_gen_shl_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
1787
            if (ot == MO_16) {
1788 1789 1790 1791 1792 1793 1794 1795 1796
                /* Only needed if count > 16, for Intel behaviour.  */
                tcg_gen_subfi_tl(cpu_tmp4, 33, count);
                tcg_gen_shr_tl(cpu_tmp4, cpu_T[1], cpu_tmp4);
                tcg_gen_or_tl(cpu_tmp0, cpu_tmp0, cpu_tmp4);
            }

            tcg_gen_subfi_tl(cpu_tmp4, mask + 1, count);
            tcg_gen_shl_tl(cpu_T[0], cpu_T[0], count);
            tcg_gen_shr_tl(cpu_T[1], cpu_T[1], cpu_tmp4);
1797
        }
1798 1799 1800 1801 1802
        tcg_gen_movi_tl(cpu_tmp4, 0);
        tcg_gen_movcond_tl(TCG_COND_EQ, cpu_T[1], count, cpu_tmp4,
                           cpu_tmp4, cpu_T[1]);
        tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
        break;
1803 1804 1805
    }

    /* store */
1806
    gen_op_st_rm_T0_A0(s, ot, op1);
1807

1808 1809
    gen_shift_flags(s, ot, cpu_T[0], cpu_tmp0, count, is_right);
    tcg_temp_free(count);
1810 1811
}

1812
static void gen_shift(DisasContext *s1, int op, TCGMemOp ot, int d, int s)
1813 1814
{
    if (s != OR_TMP1)
1815
        gen_op_mov_v_reg(ot, cpu_T[1], s);
1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839
    switch(op) {
    case OP_ROL:
        gen_rot_rm_T1(s1, ot, d, 0);
        break;
    case OP_ROR:
        gen_rot_rm_T1(s1, ot, d, 1);
        break;
    case OP_SHL:
    case OP_SHL1:
        gen_shift_rm_T1(s1, ot, d, 0, 0);
        break;
    case OP_SHR:
        gen_shift_rm_T1(s1, ot, d, 1, 0);
        break;
    case OP_SAR:
        gen_shift_rm_T1(s1, ot, d, 1, 1);
        break;
    case OP_RCL:
        gen_rotc_rm_T1(s1, ot, d, 0);
        break;
    case OP_RCR:
        gen_rotc_rm_T1(s1, ot, d, 1);
        break;
    }
B
bellard 已提交
1840 1841
}

1842
static void gen_shifti(DisasContext *s1, int op, TCGMemOp ot, int d, int c)
B
bellard 已提交
1843
{
B
bellard 已提交
1844
    switch(op) {
M
malc 已提交
1845 1846 1847 1848 1849 1850
    case OP_ROL:
        gen_rot_rm_im(s1, ot, d, c, 0);
        break;
    case OP_ROR:
        gen_rot_rm_im(s1, ot, d, c, 1);
        break;
B
bellard 已提交
1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862
    case OP_SHL:
    case OP_SHL1:
        gen_shift_rm_im(s1, ot, d, c, 0, 0);
        break;
    case OP_SHR:
        gen_shift_rm_im(s1, ot, d, c, 1, 0);
        break;
    case OP_SAR:
        gen_shift_rm_im(s1, ot, d, c, 1, 1);
        break;
    default:
        /* currently not optimized */
1863
        tcg_gen_movi_tl(cpu_T[1], c);
B
bellard 已提交
1864 1865 1866
        gen_shift(s1, op, ot, d, OR_TMP1);
        break;
    }
B
bellard 已提交
1867 1868
}

1869
static void gen_lea_modrm(CPUX86State *env, DisasContext *s, int modrm)
B
bellard 已提交
1870
{
B
bellard 已提交
1871
    target_long disp;
B
bellard 已提交
1872
    int havesib;
B
bellard 已提交
1873
    int base;
B
bellard 已提交
1874 1875 1876
    int index;
    int scale;
    int mod, rm, code, override, must_add_seg;
1877
    TCGv sum;
B
bellard 已提交
1878 1879 1880 1881 1882 1883 1884 1885

    override = s->override;
    must_add_seg = s->addseg;
    if (override >= 0)
        must_add_seg = 1;
    mod = (modrm >> 6) & 3;
    rm = modrm & 7;

1886 1887 1888
    switch (s->aflag) {
    case MO_64:
    case MO_32:
B
bellard 已提交
1889 1890
        havesib = 0;
        base = rm;
1891
        index = -1;
B
bellard 已提交
1892
        scale = 0;
1893

B
bellard 已提交
1894 1895
        if (base == 4) {
            havesib = 1;
1896
            code = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
1897
            scale = (code >> 6) & 3;
B
bellard 已提交
1898
            index = ((code >> 3) & 7) | REX_X(s);
1899 1900 1901
            if (index == 4) {
                index = -1;  /* no index */
            }
B
bellard 已提交
1902
            base = (code & 7);
B
bellard 已提交
1903
        }
B
bellard 已提交
1904
        base |= REX_B(s);
B
bellard 已提交
1905 1906 1907

        switch (mod) {
        case 0:
B
bellard 已提交
1908
            if ((base & 7) == 5) {
B
bellard 已提交
1909
                base = -1;
1910
                disp = (int32_t)cpu_ldl_code(env, s->pc);
B
bellard 已提交
1911
                s->pc += 4;
B
bellard 已提交
1912 1913 1914
                if (CODE64(s) && !havesib) {
                    disp += s->pc + s->rip_offset;
                }
B
bellard 已提交
1915 1916 1917 1918 1919
            } else {
                disp = 0;
            }
            break;
        case 1:
1920
            disp = (int8_t)cpu_ldub_code(env, s->pc++);
B
bellard 已提交
1921 1922 1923
            break;
        default:
        case 2:
1924
            disp = (int32_t)cpu_ldl_code(env, s->pc);
B
bellard 已提交
1925 1926 1927
            s->pc += 4;
            break;
        }
1928

1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941
        /* For correct popl handling with esp.  */
        if (base == R_ESP && s->popl_esp_hack) {
            disp += s->popl_esp_hack;
        }

        /* Compute the address, with a minimum number of TCG ops.  */
        TCGV_UNUSED(sum);
        if (index >= 0) {
            if (scale == 0) {
                sum = cpu_regs[index];
            } else {
                tcg_gen_shli_tl(cpu_A0, cpu_regs[index], scale);
                sum = cpu_A0;
B
bellard 已提交
1942
            }
1943 1944 1945
            if (base >= 0) {
                tcg_gen_add_tl(cpu_A0, sum, cpu_regs[base]);
                sum = cpu_A0;
B
bellard 已提交
1946
            }
1947 1948
        } else if (base >= 0) {
            sum = cpu_regs[base];
B
bellard 已提交
1949
        }
1950 1951 1952 1953
        if (TCGV_IS_UNUSED(sum)) {
            tcg_gen_movi_tl(cpu_A0, disp);
        } else {
            tcg_gen_addi_tl(cpu_A0, sum, disp);
B
bellard 已提交
1954
        }
1955

B
bellard 已提交
1956 1957
        if (must_add_seg) {
            if (override < 0) {
1958
                if (base == R_EBP || base == R_ESP) {
B
bellard 已提交
1959
                    override = R_SS;
1960
                } else {
B
bellard 已提交
1961
                    override = R_DS;
1962
                }
B
bellard 已提交
1963
            }
1964 1965 1966 1967

            tcg_gen_ld_tl(cpu_tmp0, cpu_env,
                          offsetof(CPUX86State, segs[override].base));
            if (CODE64(s)) {
1968
                if (s->aflag == MO_32) {
1969 1970 1971
                    tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
                }
                tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
1972
                return;
B
bellard 已提交
1973
            }
1974 1975 1976 1977

            tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
        }

1978
        if (s->aflag == MO_32) {
1979
            tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
B
bellard 已提交
1980
        }
1981 1982 1983
        break;

    case MO_16:
B
bellard 已提交
1984 1985 1986
        switch (mod) {
        case 0:
            if (rm == 6) {
1987
                disp = cpu_lduw_code(env, s->pc);
B
bellard 已提交
1988
                s->pc += 2;
1989
                tcg_gen_movi_tl(cpu_A0, disp);
B
bellard 已提交
1990 1991 1992 1993 1994 1995 1996
                rm = 0; /* avoid SS override */
                goto no_rm;
            } else {
                disp = 0;
            }
            break;
        case 1:
1997
            disp = (int8_t)cpu_ldub_code(env, s->pc++);
B
bellard 已提交
1998 1999 2000
            break;
        default:
        case 2:
2001
            disp = (int16_t)cpu_lduw_code(env, s->pc);
B
bellard 已提交
2002 2003 2004
            s->pc += 2;
            break;
        }
2005 2006 2007

        sum = cpu_A0;
        switch (rm) {
B
bellard 已提交
2008
        case 0:
2009
            tcg_gen_add_tl(cpu_A0, cpu_regs[R_EBX], cpu_regs[R_ESI]);
B
bellard 已提交
2010 2011
            break;
        case 1:
2012
            tcg_gen_add_tl(cpu_A0, cpu_regs[R_EBX], cpu_regs[R_EDI]);
B
bellard 已提交
2013 2014
            break;
        case 2:
2015
            tcg_gen_add_tl(cpu_A0, cpu_regs[R_EBP], cpu_regs[R_ESI]);
B
bellard 已提交
2016 2017
            break;
        case 3:
2018
            tcg_gen_add_tl(cpu_A0, cpu_regs[R_EBP], cpu_regs[R_EDI]);
B
bellard 已提交
2019 2020
            break;
        case 4:
2021
            sum = cpu_regs[R_ESI];
B
bellard 已提交
2022 2023
            break;
        case 5:
2024
            sum = cpu_regs[R_EDI];
B
bellard 已提交
2025 2026
            break;
        case 6:
2027
            sum = cpu_regs[R_EBP];
B
bellard 已提交
2028 2029 2030
            break;
        default:
        case 7:
2031
            sum = cpu_regs[R_EBX];
B
bellard 已提交
2032 2033
            break;
        }
2034
        tcg_gen_addi_tl(cpu_A0, sum, disp);
2035
        tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
B
bellard 已提交
2036 2037 2038
    no_rm:
        if (must_add_seg) {
            if (override < 0) {
2039
                if (rm == 2 || rm == 3 || rm == 6) {
B
bellard 已提交
2040
                    override = R_SS;
2041
                } else {
B
bellard 已提交
2042
                    override = R_DS;
2043
                }
B
bellard 已提交
2044
            }
2045
            gen_op_addl_A0_seg(s, override);
B
bellard 已提交
2046
        }
2047 2048 2049 2050
        break;

    default:
        tcg_abort();
B
bellard 已提交
2051 2052 2053
    }
}

2054
static void gen_nop_modrm(CPUX86State *env, DisasContext *s, int modrm)
B
bellard 已提交
2055 2056 2057 2058 2059 2060 2061 2062
{
    int mod, rm, base, code;

    mod = (modrm >> 6) & 3;
    if (mod == 3)
        return;
    rm = modrm & 7;

2063 2064 2065
    switch (s->aflag) {
    case MO_64:
    case MO_32:
B
bellard 已提交
2066
        base = rm;
2067

B
bellard 已提交
2068
        if (base == 4) {
2069
            code = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
2070 2071
            base = (code & 7);
        }
2072

B
bellard 已提交
2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086
        switch (mod) {
        case 0:
            if (base == 5) {
                s->pc += 4;
            }
            break;
        case 1:
            s->pc++;
            break;
        default:
        case 2:
            s->pc += 4;
            break;
        }
2087 2088 2089
        break;

    case MO_16:
B
bellard 已提交
2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103
        switch (mod) {
        case 0:
            if (rm == 6) {
                s->pc += 2;
            }
            break;
        case 1:
            s->pc++;
            break;
        default:
        case 2:
            s->pc += 2;
            break;
        }
2104 2105 2106 2107
        break;

    default:
        tcg_abort();
B
bellard 已提交
2108 2109 2110
    }
}

B
bellard 已提交
2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121
/* used for LEA and MOV AX, mem */
static void gen_add_A0_ds_seg(DisasContext *s)
{
    int override, must_add_seg;
    must_add_seg = s->addseg;
    override = R_DS;
    if (s->override >= 0) {
        override = s->override;
        must_add_seg = 1;
    }
    if (must_add_seg) {
2122 2123
#ifdef TARGET_X86_64
        if (CODE64(s)) {
B
bellard 已提交
2124
            gen_op_addq_A0_seg(override);
2125
        } else
2126 2127
#endif
        {
2128
            gen_op_addl_A0_seg(s, override);
2129
        }
B
bellard 已提交
2130 2131 2132
    }
}

B
balrog 已提交
2133
/* generate modrm memory load or store of 'reg'. TMP0 is used if reg ==
B
bellard 已提交
2134
   OR_TMP0 */
2135
static void gen_ldst_modrm(CPUX86State *env, DisasContext *s, int modrm,
2136
                           TCGMemOp ot, int reg, int is_store)
B
bellard 已提交
2137
{
2138
    int mod, rm;
B
bellard 已提交
2139 2140

    mod = (modrm >> 6) & 3;
B
bellard 已提交
2141
    rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
2142 2143 2144
    if (mod == 3) {
        if (is_store) {
            if (reg != OR_TMP0)
2145
                gen_op_mov_v_reg(ot, cpu_T[0], reg);
2146
            gen_op_mov_reg_v(ot, rm, cpu_T[0]);
B
bellard 已提交
2147
        } else {
2148
            gen_op_mov_v_reg(ot, cpu_T[0], rm);
B
bellard 已提交
2149
            if (reg != OR_TMP0)
2150
                gen_op_mov_reg_v(ot, reg, cpu_T[0]);
B
bellard 已提交
2151 2152
        }
    } else {
2153
        gen_lea_modrm(env, s, modrm);
B
bellard 已提交
2154 2155
        if (is_store) {
            if (reg != OR_TMP0)
2156
                gen_op_mov_v_reg(ot, cpu_T[0], reg);
2157
            gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
2158
        } else {
2159
            gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
2160
            if (reg != OR_TMP0)
2161
                gen_op_mov_reg_v(ot, reg, cpu_T[0]);
B
bellard 已提交
2162 2163 2164 2165
        }
    }
}

2166
static inline uint32_t insn_get(CPUX86State *env, DisasContext *s, TCGMemOp ot)
B
bellard 已提交
2167 2168 2169
{
    uint32_t ret;

2170
    switch (ot) {
2171
    case MO_8:
2172
        ret = cpu_ldub_code(env, s->pc);
B
bellard 已提交
2173 2174
        s->pc++;
        break;
2175
    case MO_16:
2176
        ret = cpu_lduw_code(env, s->pc);
B
bellard 已提交
2177 2178
        s->pc += 2;
        break;
2179
    case MO_32:
2180 2181 2182
#ifdef TARGET_X86_64
    case MO_64:
#endif
2183
        ret = cpu_ldl_code(env, s->pc);
B
bellard 已提交
2184 2185
        s->pc += 4;
        break;
2186 2187
    default:
        tcg_abort();
B
bellard 已提交
2188 2189 2190 2191
    }
    return ret;
}

2192
static inline int insn_const_size(TCGMemOp ot)
B
bellard 已提交
2193
{
2194
    if (ot <= MO_32) {
B
bellard 已提交
2195
        return 1 << ot;
2196
    } else {
B
bellard 已提交
2197
        return 4;
2198
    }
B
bellard 已提交
2199 2200
}

2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211
static inline void gen_goto_tb(DisasContext *s, int tb_num, target_ulong eip)
{
    TranslationBlock *tb;
    target_ulong pc;

    pc = s->cs_base + eip;
    tb = s->tb;
    /* NOTE: we handle the case where the TB spans two pages here */
    if ((pc & TARGET_PAGE_MASK) == (tb->pc & TARGET_PAGE_MASK) ||
        (pc & TARGET_PAGE_MASK) == ((s->pc - 1) & TARGET_PAGE_MASK))  {
        /* jump to same page: we can use a direct jump */
B
bellard 已提交
2212
        tcg_gen_goto_tb(tb_num);
2213
        gen_jmp_im(eip);
2214
        tcg_gen_exit_tb((uintptr_t)tb + tb_num);
2215 2216 2217 2218 2219 2220 2221
    } else {
        /* jump to another page: currently not optimized */
        gen_jmp_im(eip);
        gen_eob(s);
    }
}

2222
static inline void gen_jcc(DisasContext *s, int b,
B
bellard 已提交
2223
                           target_ulong val, target_ulong next_eip)
B
bellard 已提交
2224
{
2225
    int l1, l2;
2226

B
bellard 已提交
2227
    if (s->jmp_opt) {
B
bellard 已提交
2228
        l1 = gen_new_label();
2229
        gen_jcc1(s, b, l1);
2230

2231
        gen_goto_tb(s, 0, next_eip);
B
bellard 已提交
2232 2233

        gen_set_label(l1);
2234
        gen_goto_tb(s, 1, val);
J
Jun Koi 已提交
2235
        s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2236
    } else {
B
bellard 已提交
2237 2238
        l1 = gen_new_label();
        l2 = gen_new_label();
2239
        gen_jcc1(s, b, l1);
2240

B
bellard 已提交
2241
        gen_jmp_im(next_eip);
2242 2243
        tcg_gen_br(l2);

B
bellard 已提交
2244 2245 2246
        gen_set_label(l1);
        gen_jmp_im(val);
        gen_set_label(l2);
B
bellard 已提交
2247 2248 2249 2250
        gen_eob(s);
    }
}

2251
static void gen_cmovcc1(CPUX86State *env, DisasContext *s, TCGMemOp ot, int b,
2252 2253
                        int modrm, int reg)
{
2254
    CCPrepare cc;
2255

2256
    gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
2257

2258 2259 2260 2261 2262 2263 2264 2265
    cc = gen_prepare_cc(s, b, cpu_T[1]);
    if (cc.mask != -1) {
        TCGv t0 = tcg_temp_new();
        tcg_gen_andi_tl(t0, cc.reg, cc.mask);
        cc.reg = t0;
    }
    if (!cc.use_reg2) {
        cc.reg2 = tcg_const_tl(cc.imm);
2266 2267
    }

2268 2269
    tcg_gen_movcond_tl(cc.cond, cpu_T[0], cc.reg, cc.reg2,
                       cpu_T[0], cpu_regs[reg]);
2270
    gen_op_mov_reg_v(ot, reg, cpu_T[0]);
2271 2272 2273 2274 2275 2276 2277

    if (cc.mask != -1) {
        tcg_temp_free(cc.reg);
    }
    if (!cc.use_reg2) {
        tcg_temp_free(cc.reg2);
    }
2278 2279
}

2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295
static inline void gen_op_movl_T0_seg(int seg_reg)
{
    tcg_gen_ld32u_tl(cpu_T[0], cpu_env, 
                     offsetof(CPUX86State,segs[seg_reg].selector));
}

static inline void gen_op_movl_seg_T0_vm(int seg_reg)
{
    tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xffff);
    tcg_gen_st32_tl(cpu_T[0], cpu_env, 
                    offsetof(CPUX86State,segs[seg_reg].selector));
    tcg_gen_shli_tl(cpu_T[0], cpu_T[0], 4);
    tcg_gen_st_tl(cpu_T[0], cpu_env, 
                  offsetof(CPUX86State,segs[seg_reg].base));
}

B
bellard 已提交
2296 2297
/* move T0 to seg_reg and compute if the CPU state may change. Never
   call this function with seg_reg == R_CS */
B
bellard 已提交
2298
static void gen_movl_seg_T0(DisasContext *s, int seg_reg, target_ulong cur_eip)
B
bellard 已提交
2299
{
2300 2301
    if (s->pe && !s->vm86) {
        /* XXX: optimize by finding processor state dynamically */
2302
        gen_update_cc_op(s);
B
bellard 已提交
2303
        gen_jmp_im(cur_eip);
2304
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
2305
        gen_helper_load_seg(cpu_env, tcg_const_i32(seg_reg), cpu_tmp2_i32);
B
bellard 已提交
2306 2307 2308 2309 2310
        /* abort translation because the addseg value may change or
           because ss32 may change. For R_SS, translation must always
           stop as a special handling must be done to disable hardware
           interrupts for the next instruction */
        if (seg_reg == R_SS || (s->code32 && seg_reg < R_FS))
J
Jun Koi 已提交
2311
            s->is_jmp = DISAS_TB_JUMP;
2312
    } else {
2313
        gen_op_movl_seg_T0_vm(seg_reg);
B
bellard 已提交
2314
        if (seg_reg == R_SS)
J
Jun Koi 已提交
2315
            s->is_jmp = DISAS_TB_JUMP;
2316
    }
B
bellard 已提交
2317 2318
}

T
ths 已提交
2319 2320 2321 2322 2323
static inline int svm_is_rep(int prefixes)
{
    return ((prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) ? 8 : 0);
}

B
bellard 已提交
2324
static inline void
T
ths 已提交
2325
gen_svm_check_intercept_param(DisasContext *s, target_ulong pc_start,
2326
                              uint32_t type, uint64_t param)
T
ths 已提交
2327
{
B
bellard 已提交
2328 2329 2330
    /* no SVM activated; fast case */
    if (likely(!(s->flags & HF_SVMI_MASK)))
        return;
2331
    gen_update_cc_op(s);
B
bellard 已提交
2332
    gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
2333
    gen_helper_svm_check_intercept_param(cpu_env, tcg_const_i32(type),
P
pbrook 已提交
2334
                                         tcg_const_i64(param));
T
ths 已提交
2335 2336
}

B
bellard 已提交
2337
static inline void
T
ths 已提交
2338 2339
gen_svm_check_intercept(DisasContext *s, target_ulong pc_start, uint64_t type)
{
B
bellard 已提交
2340
    gen_svm_check_intercept_param(s, pc_start, type, 0);
T
ths 已提交
2341 2342
}

2343 2344
static inline void gen_stack_update(DisasContext *s, int addend)
{
B
bellard 已提交
2345 2346
#ifdef TARGET_X86_64
    if (CODE64(s)) {
2347
        gen_op_add_reg_im(MO_64, R_ESP, addend);
B
bellard 已提交
2348 2349
    } else
#endif
2350
    if (s->ss32) {
2351
        gen_op_add_reg_im(MO_32, R_ESP, addend);
2352
    } else {
2353
        gen_op_add_reg_im(MO_16, R_ESP, addend);
2354 2355 2356
    }
}

2357 2358
/* Generate a push. It depends on ss32, addseg and dflag.  */
static void gen_push_v(DisasContext *s, TCGv val)
B
bellard 已提交
2359
{
2360 2361 2362 2363 2364
    TCGMemOp a_ot, d_ot = mo_pushpop(s, s->dflag);
    int size = 1 << d_ot;
    TCGv new_esp = cpu_A0;

    tcg_gen_subi_tl(cpu_A0, cpu_regs[R_ESP], size);
B
bellard 已提交
2365

B
bellard 已提交
2366
    if (CODE64(s)) {
2367 2368 2369 2370 2371 2372
        a_ot = MO_64;
    } else if (s->ss32) {
        a_ot = MO_32;
        if (s->addseg) {
            new_esp = cpu_tmp4;
            tcg_gen_mov_tl(new_esp, cpu_A0);
2373
            gen_op_addl_A0_seg(s, R_SS);
2374 2375
        } else {
            tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
B
bellard 已提交
2376
        }
2377 2378 2379 2380 2381 2382
    } else {
        a_ot = MO_16;
        new_esp = cpu_tmp4;
        tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
        tcg_gen_mov_tl(new_esp, cpu_A0);
        gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2383
    }
2384 2385 2386

    gen_op_st_v(s, d_ot, val, cpu_A0);
    gen_op_mov_reg_v(a_ot, R_ESP, new_esp);
B
bellard 已提交
2387 2388
}

2389
/* two step pop is necessary for precise exceptions */
2390
static TCGMemOp gen_pop_T0(DisasContext *s)
B
bellard 已提交
2391
{
2392 2393 2394
    TCGMemOp d_ot = mo_pushpop(s, s->dflag);
    TCGv addr = cpu_A0;

B
bellard 已提交
2395
    if (CODE64(s)) {
2396 2397 2398 2399 2400 2401 2402 2403 2404
        addr = cpu_regs[R_ESP];
    } else if (!s->ss32) {
        tcg_gen_ext16u_tl(cpu_A0, cpu_regs[R_ESP]);
        gen_op_addl_A0_seg(s, R_SS);
    } else if (s->addseg) {
        tcg_gen_mov_tl(cpu_A0, cpu_regs[R_ESP]);
        gen_op_addl_A0_seg(s, R_SS);
    } else {
        tcg_gen_ext32u_tl(cpu_A0, cpu_regs[R_ESP]);
B
bellard 已提交
2405
    }
2406 2407 2408

    gen_op_ld_v(s, d_ot, cpu_T[0], addr);
    return d_ot;
B
bellard 已提交
2409 2410
}

2411
static void gen_pop_update(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
2412
{
2413
    gen_stack_update(s, 1 << ot);
B
bellard 已提交
2414 2415 2416 2417
}

static void gen_stack_A0(DisasContext *s)
{
B
bellard 已提交
2418
    gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2419
    if (!s->ss32)
2420
        tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
2421
    tcg_gen_mov_tl(cpu_T[1], cpu_A0);
B
bellard 已提交
2422
    if (s->addseg)
2423
        gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2424 2425 2426 2427 2428 2429
}

/* NOTE: wrap around in 16 bit not fully handled */
static void gen_pusha(DisasContext *s)
{
    int i;
B
bellard 已提交
2430
    gen_op_movl_A0_reg(R_ESP);
2431
    gen_op_addl_A0_im(-8 << s->dflag);
B
bellard 已提交
2432
    if (!s->ss32)
2433
        tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
2434
    tcg_gen_mov_tl(cpu_T[1], cpu_A0);
B
bellard 已提交
2435
    if (s->addseg)
2436
        gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2437
    for(i = 0;i < 8; i++) {
2438
        gen_op_mov_v_reg(MO_32, cpu_T[0], 7 - i);
2439 2440
        gen_op_st_v(s, s->dflag, cpu_T[0], cpu_A0);
        gen_op_addl_A0_im(1 << s->dflag);
B
bellard 已提交
2441
    }
2442
    gen_op_mov_reg_v(MO_16 + s->ss32, R_ESP, cpu_T[1]);
B
bellard 已提交
2443 2444 2445 2446 2447 2448
}

/* NOTE: wrap around in 16 bit not fully handled */
static void gen_popa(DisasContext *s)
{
    int i;
B
bellard 已提交
2449
    gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2450
    if (!s->ss32)
2451
        tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
2452
    tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2453
    tcg_gen_addi_tl(cpu_T[1], cpu_T[1], 8 << s->dflag);
B
bellard 已提交
2454
    if (s->addseg)
2455
        gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2456 2457 2458
    for(i = 0;i < 8; i++) {
        /* ESP is not reloaded */
        if (i != 3) {
2459
            gen_op_ld_v(s, s->dflag, cpu_T[0], cpu_A0);
2460
            gen_op_mov_reg_v(s->dflag, 7 - i, cpu_T[0]);
B
bellard 已提交
2461
        }
2462
        gen_op_addl_A0_im(1 << s->dflag);
B
bellard 已提交
2463
    }
2464
    gen_op_mov_reg_v(MO_16 + s->ss32, R_ESP, cpu_T[1]);
B
bellard 已提交
2465 2466 2467 2468
}

static void gen_enter(DisasContext *s, int esp_addend, int level)
{
2469 2470
    TCGMemOp ot = mo_pushpop(s, s->dflag);
    int opsize = 1 << ot;
B
bellard 已提交
2471 2472

    level &= 0x1f;
2473 2474
#ifdef TARGET_X86_64
    if (CODE64(s)) {
B
bellard 已提交
2475
        gen_op_movl_A0_reg(R_ESP);
2476
        gen_op_addq_A0_im(-opsize);
2477
        tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2478 2479

        /* push bp */
2480
        gen_op_mov_v_reg(MO_32, cpu_T[0], R_EBP);
2481
        gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
2482
        if (level) {
B
bellard 已提交
2483
            /* XXX: must save state */
2484
            gen_helper_enter64_level(cpu_env, tcg_const_i32(level),
2485
                                     tcg_const_i32((ot == MO_64)),
P
pbrook 已提交
2486
                                     cpu_T[1]);
2487
        }
2488
        gen_op_mov_reg_v(ot, R_EBP, cpu_T[1]);
2489
        tcg_gen_addi_tl(cpu_T[1], cpu_T[1], -esp_addend + (-opsize * level));
2490
        gen_op_mov_reg_v(MO_64, R_ESP, cpu_T[1]);
2491
    } else
2492 2493
#endif
    {
B
bellard 已提交
2494
        gen_op_movl_A0_reg(R_ESP);
2495 2496
        gen_op_addl_A0_im(-opsize);
        if (!s->ss32)
2497
            tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
2498
        tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2499
        if (s->addseg)
2500
            gen_op_addl_A0_seg(s, R_SS);
2501
        /* push bp */
2502
        gen_op_mov_v_reg(MO_32, cpu_T[0], R_EBP);
2503
        gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
2504
        if (level) {
B
bellard 已提交
2505
            /* XXX: must save state */
2506
            gen_helper_enter_level(cpu_env, tcg_const_i32(level),
2507
                                   tcg_const_i32(s->dflag - 1),
P
pbrook 已提交
2508
                                   cpu_T[1]);
2509
        }
2510
        gen_op_mov_reg_v(ot, R_EBP, cpu_T[1]);
2511
        tcg_gen_addi_tl(cpu_T[1], cpu_T[1], -esp_addend + (-opsize * level));
2512
        gen_op_mov_reg_v(MO_16 + s->ss32, R_ESP, cpu_T[1]);
B
bellard 已提交
2513 2514 2515
    }
}

B
bellard 已提交
2516
static void gen_exception(DisasContext *s, int trapno, target_ulong cur_eip)
B
bellard 已提交
2517
{
2518
    gen_update_cc_op(s);
B
bellard 已提交
2519
    gen_jmp_im(cur_eip);
B
Blue Swirl 已提交
2520
    gen_helper_raise_exception(cpu_env, tcg_const_i32(trapno));
J
Jun Koi 已提交
2521
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2522 2523 2524
}

/* an interrupt is different from an exception because of the
B
blueswir1 已提交
2525
   privilege checks */
2526
static void gen_interrupt(DisasContext *s, int intno,
B
bellard 已提交
2527
                          target_ulong cur_eip, target_ulong next_eip)
B
bellard 已提交
2528
{
2529
    gen_update_cc_op(s);
B
bellard 已提交
2530
    gen_jmp_im(cur_eip);
B
Blue Swirl 已提交
2531
    gen_helper_raise_interrupt(cpu_env, tcg_const_i32(intno),
P
pbrook 已提交
2532
                               tcg_const_i32(next_eip - cur_eip));
J
Jun Koi 已提交
2533
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2534 2535
}

B
bellard 已提交
2536
static void gen_debug(DisasContext *s, target_ulong cur_eip)
B
bellard 已提交
2537
{
2538
    gen_update_cc_op(s);
B
bellard 已提交
2539
    gen_jmp_im(cur_eip);
B
Blue Swirl 已提交
2540
    gen_helper_debug(cpu_env);
J
Jun Koi 已提交
2541
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2542 2543 2544 2545 2546 2547
}

/* generate a generic end of block. Trace exception is also generated
   if needed */
static void gen_eob(DisasContext *s)
{
2548
    gen_update_cc_op(s);
2549
    if (s->tb->flags & HF_INHIBIT_IRQ_MASK) {
2550
        gen_helper_reset_inhibit_irq(cpu_env);
2551
    }
J
Jan Kiszka 已提交
2552
    if (s->tb->flags & HF_RF_MASK) {
2553
        gen_helper_reset_rf(cpu_env);
J
Jan Kiszka 已提交
2554
    }
2555
    if (s->singlestep_enabled) {
B
Blue Swirl 已提交
2556
        gen_helper_debug(cpu_env);
2557
    } else if (s->tf) {
B
Blue Swirl 已提交
2558
        gen_helper_single_step(cpu_env);
B
bellard 已提交
2559
    } else {
B
bellard 已提交
2560
        tcg_gen_exit_tb(0);
B
bellard 已提交
2561
    }
J
Jun Koi 已提交
2562
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2563 2564 2565 2566
}

/* generate a jump to eip. No segment change must happen before as a
   direct call to the next block may occur */
B
bellard 已提交
2567
static void gen_jmp_tb(DisasContext *s, target_ulong eip, int tb_num)
B
bellard 已提交
2568
{
2569 2570
    gen_update_cc_op(s);
    set_cc_op(s, CC_OP_DYNAMIC);
B
bellard 已提交
2571
    if (s->jmp_opt) {
2572
        gen_goto_tb(s, tb_num, eip);
J
Jun Koi 已提交
2573
        s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2574
    } else {
B
bellard 已提交
2575
        gen_jmp_im(eip);
B
bellard 已提交
2576 2577 2578 2579
        gen_eob(s);
    }
}

B
bellard 已提交
2580 2581 2582 2583 2584
static void gen_jmp(DisasContext *s, target_ulong eip)
{
    gen_jmp_tb(s, eip, 0);
}

2585
static inline void gen_ldq_env_A0(DisasContext *s, int offset)
B
bellard 已提交
2586
{
2587
    tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
2588
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset);
B
bellard 已提交
2589
}
B
bellard 已提交
2590

2591
static inline void gen_stq_env_A0(DisasContext *s, int offset)
B
bellard 已提交
2592
{
2593
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset);
2594
    tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
B
bellard 已提交
2595
}
B
bellard 已提交
2596

2597
static inline void gen_ldo_env_A0(DisasContext *s, int offset)
B
bellard 已提交
2598
{
2599
    int mem_index = s->mem_index;
2600
    tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0, mem_index, MO_LEQ);
2601
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(0)));
B
bellard 已提交
2602
    tcg_gen_addi_tl(cpu_tmp0, cpu_A0, 8);
2603
    tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_tmp0, mem_index, MO_LEQ);
2604
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(1)));
B
bellard 已提交
2605
}
B
bellard 已提交
2606

2607
static inline void gen_sto_env_A0(DisasContext *s, int offset)
B
bellard 已提交
2608
{
2609
    int mem_index = s->mem_index;
2610
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(0)));
2611
    tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0, mem_index, MO_LEQ);
B
bellard 已提交
2612
    tcg_gen_addi_tl(cpu_tmp0, cpu_A0, 8);
2613
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(1)));
2614
    tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_tmp0, mem_index, MO_LEQ);
B
bellard 已提交
2615
}
B
bellard 已提交
2616

B
bellard 已提交
2617 2618
static inline void gen_op_movo(int d_offset, int s_offset)
{
2619 2620 2621 2622
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset + 8);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset + 8);
B
bellard 已提交
2623 2624 2625 2626
}

static inline void gen_op_movq(int d_offset, int s_offset)
{
2627 2628
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
B
bellard 已提交
2629 2630 2631 2632
}

static inline void gen_op_movl(int d_offset, int s_offset)
{
2633 2634
    tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env, s_offset);
    tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, d_offset);
B
bellard 已提交
2635 2636 2637 2638
}

static inline void gen_op_movq_env_0(int d_offset)
{
2639 2640
    tcg_gen_movi_i64(cpu_tmp1_i64, 0);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
B
bellard 已提交
2641
}
B
bellard 已提交
2642

B
Blue Swirl 已提交
2643 2644 2645 2646 2647 2648 2649
typedef void (*SSEFunc_i_ep)(TCGv_i32 val, TCGv_ptr env, TCGv_ptr reg);
typedef void (*SSEFunc_l_ep)(TCGv_i64 val, TCGv_ptr env, TCGv_ptr reg);
typedef void (*SSEFunc_0_epi)(TCGv_ptr env, TCGv_ptr reg, TCGv_i32 val);
typedef void (*SSEFunc_0_epl)(TCGv_ptr env, TCGv_ptr reg, TCGv_i64 val);
typedef void (*SSEFunc_0_epp)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b);
typedef void (*SSEFunc_0_eppi)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
                               TCGv_i32 val);
B
Blue Swirl 已提交
2650
typedef void (*SSEFunc_0_ppi)(TCGv_ptr reg_a, TCGv_ptr reg_b, TCGv_i32 val);
B
Blue Swirl 已提交
2651 2652
typedef void (*SSEFunc_0_eppt)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
                               TCGv val);
B
Blue Swirl 已提交
2653

B
bellard 已提交
2654 2655
#define SSE_SPECIAL ((void *)1)
#define SSE_DUMMY ((void *)2)
B
bellard 已提交
2656

P
pbrook 已提交
2657 2658 2659
#define MMX_OP2(x) { gen_helper_ ## x ## _mmx, gen_helper_ ## x ## _xmm }
#define SSE_FOP(x) { gen_helper_ ## x ## ps, gen_helper_ ## x ## pd, \
                     gen_helper_ ## x ## ss, gen_helper_ ## x ## sd, }
B
bellard 已提交
2660

B
Blue Swirl 已提交
2661
static const SSEFunc_0_epp sse_op_table1[256][4] = {
A
aurel32 已提交
2662 2663 2664
    /* 3DNow! extensions */
    [0x0e] = { SSE_DUMMY }, /* femms */
    [0x0f] = { SSE_DUMMY }, /* pf... */
B
bellard 已提交
2665 2666 2667
    /* pure SSE operations */
    [0x10] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movups, movupd, movss, movsd */
    [0x11] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movups, movupd, movss, movsd */
B
bellard 已提交
2668
    [0x12] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movlps, movlpd, movsldup, movddup */
B
bellard 已提交
2669
    [0x13] = { SSE_SPECIAL, SSE_SPECIAL },  /* movlps, movlpd */
P
pbrook 已提交
2670 2671
    [0x14] = { gen_helper_punpckldq_xmm, gen_helper_punpcklqdq_xmm },
    [0x15] = { gen_helper_punpckhdq_xmm, gen_helper_punpckhqdq_xmm },
B
bellard 已提交
2672 2673 2674 2675 2676 2677
    [0x16] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL },  /* movhps, movhpd, movshdup */
    [0x17] = { SSE_SPECIAL, SSE_SPECIAL },  /* movhps, movhpd */

    [0x28] = { SSE_SPECIAL, SSE_SPECIAL },  /* movaps, movapd */
    [0x29] = { SSE_SPECIAL, SSE_SPECIAL },  /* movaps, movapd */
    [0x2a] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* cvtpi2ps, cvtpi2pd, cvtsi2ss, cvtsi2sd */
2678
    [0x2b] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movntps, movntpd, movntss, movntsd */
B
bellard 已提交
2679 2680
    [0x2c] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* cvttps2pi, cvttpd2pi, cvttsd2si, cvttss2si */
    [0x2d] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* cvtps2pi, cvtpd2pi, cvtsd2si, cvtss2si */
P
pbrook 已提交
2681 2682
    [0x2e] = { gen_helper_ucomiss, gen_helper_ucomisd },
    [0x2f] = { gen_helper_comiss, gen_helper_comisd },
B
bellard 已提交
2683 2684
    [0x50] = { SSE_SPECIAL, SSE_SPECIAL }, /* movmskps, movmskpd */
    [0x51] = SSE_FOP(sqrt),
P
pbrook 已提交
2685 2686 2687 2688 2689 2690
    [0x52] = { gen_helper_rsqrtps, NULL, gen_helper_rsqrtss, NULL },
    [0x53] = { gen_helper_rcpps, NULL, gen_helper_rcpss, NULL },
    [0x54] = { gen_helper_pand_xmm, gen_helper_pand_xmm }, /* andps, andpd */
    [0x55] = { gen_helper_pandn_xmm, gen_helper_pandn_xmm }, /* andnps, andnpd */
    [0x56] = { gen_helper_por_xmm, gen_helper_por_xmm }, /* orps, orpd */
    [0x57] = { gen_helper_pxor_xmm, gen_helper_pxor_xmm }, /* xorps, xorpd */
B
bellard 已提交
2691 2692
    [0x58] = SSE_FOP(add),
    [0x59] = SSE_FOP(mul),
P
pbrook 已提交
2693 2694 2695
    [0x5a] = { gen_helper_cvtps2pd, gen_helper_cvtpd2ps,
               gen_helper_cvtss2sd, gen_helper_cvtsd2ss },
    [0x5b] = { gen_helper_cvtdq2ps, gen_helper_cvtps2dq, gen_helper_cvttps2dq },
B
bellard 已提交
2696 2697 2698 2699 2700 2701
    [0x5c] = SSE_FOP(sub),
    [0x5d] = SSE_FOP(min),
    [0x5e] = SSE_FOP(div),
    [0x5f] = SSE_FOP(max),

    [0xc2] = SSE_FOP(cmpeq),
B
Blue Swirl 已提交
2702 2703
    [0xc6] = { (SSEFunc_0_epp)gen_helper_shufps,
               (SSEFunc_0_epp)gen_helper_shufpd }, /* XXX: casts */
B
bellard 已提交
2704

R
Richard Henderson 已提交
2705 2706 2707
    /* SSSE3, SSE4, MOVBE, CRC32, BMI1, BMI2, ADX.  */
    [0x38] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL },
    [0x3a] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL },
B
balrog 已提交
2708

B
bellard 已提交
2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721
    /* MMX ops and their SSE extensions */
    [0x60] = MMX_OP2(punpcklbw),
    [0x61] = MMX_OP2(punpcklwd),
    [0x62] = MMX_OP2(punpckldq),
    [0x63] = MMX_OP2(packsswb),
    [0x64] = MMX_OP2(pcmpgtb),
    [0x65] = MMX_OP2(pcmpgtw),
    [0x66] = MMX_OP2(pcmpgtl),
    [0x67] = MMX_OP2(packuswb),
    [0x68] = MMX_OP2(punpckhbw),
    [0x69] = MMX_OP2(punpckhwd),
    [0x6a] = MMX_OP2(punpckhdq),
    [0x6b] = MMX_OP2(packssdw),
P
pbrook 已提交
2722 2723
    [0x6c] = { NULL, gen_helper_punpcklqdq_xmm },
    [0x6d] = { NULL, gen_helper_punpckhqdq_xmm },
B
bellard 已提交
2724 2725
    [0x6e] = { SSE_SPECIAL, SSE_SPECIAL }, /* movd mm, ea */
    [0x6f] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movq, movdqa, , movqdu */
B
Blue Swirl 已提交
2726 2727 2728 2729
    [0x70] = { (SSEFunc_0_epp)gen_helper_pshufw_mmx,
               (SSEFunc_0_epp)gen_helper_pshufd_xmm,
               (SSEFunc_0_epp)gen_helper_pshufhw_xmm,
               (SSEFunc_0_epp)gen_helper_pshuflw_xmm }, /* XXX: casts */
B
bellard 已提交
2730 2731 2732 2733 2734 2735
    [0x71] = { SSE_SPECIAL, SSE_SPECIAL }, /* shiftw */
    [0x72] = { SSE_SPECIAL, SSE_SPECIAL }, /* shiftd */
    [0x73] = { SSE_SPECIAL, SSE_SPECIAL }, /* shiftq */
    [0x74] = MMX_OP2(pcmpeqb),
    [0x75] = MMX_OP2(pcmpeqw),
    [0x76] = MMX_OP2(pcmpeql),
A
aurel32 已提交
2736
    [0x77] = { SSE_DUMMY }, /* emms */
2737 2738
    [0x78] = { NULL, SSE_SPECIAL, NULL, SSE_SPECIAL }, /* extrq_i, insertq_i */
    [0x79] = { NULL, gen_helper_extrq_r, NULL, gen_helper_insertq_r },
P
pbrook 已提交
2739 2740
    [0x7c] = { NULL, gen_helper_haddpd, NULL, gen_helper_haddps },
    [0x7d] = { NULL, gen_helper_hsubpd, NULL, gen_helper_hsubps },
B
bellard 已提交
2741 2742 2743 2744
    [0x7e] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movd, movd, , movq */
    [0x7f] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movq, movdqa, movdqu */
    [0xc4] = { SSE_SPECIAL, SSE_SPECIAL }, /* pinsrw */
    [0xc5] = { SSE_SPECIAL, SSE_SPECIAL }, /* pextrw */
P
pbrook 已提交
2745
    [0xd0] = { NULL, gen_helper_addsubpd, NULL, gen_helper_addsubps },
B
bellard 已提交
2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766
    [0xd1] = MMX_OP2(psrlw),
    [0xd2] = MMX_OP2(psrld),
    [0xd3] = MMX_OP2(psrlq),
    [0xd4] = MMX_OP2(paddq),
    [0xd5] = MMX_OP2(pmullw),
    [0xd6] = { NULL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL },
    [0xd7] = { SSE_SPECIAL, SSE_SPECIAL }, /* pmovmskb */
    [0xd8] = MMX_OP2(psubusb),
    [0xd9] = MMX_OP2(psubusw),
    [0xda] = MMX_OP2(pminub),
    [0xdb] = MMX_OP2(pand),
    [0xdc] = MMX_OP2(paddusb),
    [0xdd] = MMX_OP2(paddusw),
    [0xde] = MMX_OP2(pmaxub),
    [0xdf] = MMX_OP2(pandn),
    [0xe0] = MMX_OP2(pavgb),
    [0xe1] = MMX_OP2(psraw),
    [0xe2] = MMX_OP2(psrad),
    [0xe3] = MMX_OP2(pavgw),
    [0xe4] = MMX_OP2(pmulhuw),
    [0xe5] = MMX_OP2(pmulhw),
P
pbrook 已提交
2767
    [0xe6] = { NULL, gen_helper_cvttpd2dq, gen_helper_cvtdq2pd, gen_helper_cvtpd2dq },
B
bellard 已提交
2768 2769 2770 2771 2772 2773 2774 2775 2776
    [0xe7] = { SSE_SPECIAL , SSE_SPECIAL },  /* movntq, movntq */
    [0xe8] = MMX_OP2(psubsb),
    [0xe9] = MMX_OP2(psubsw),
    [0xea] = MMX_OP2(pminsw),
    [0xeb] = MMX_OP2(por),
    [0xec] = MMX_OP2(paddsb),
    [0xed] = MMX_OP2(paddsw),
    [0xee] = MMX_OP2(pmaxsw),
    [0xef] = MMX_OP2(pxor),
B
bellard 已提交
2777
    [0xf0] = { NULL, NULL, NULL, SSE_SPECIAL }, /* lddqu */
B
bellard 已提交
2778 2779 2780 2781 2782 2783
    [0xf1] = MMX_OP2(psllw),
    [0xf2] = MMX_OP2(pslld),
    [0xf3] = MMX_OP2(psllq),
    [0xf4] = MMX_OP2(pmuludq),
    [0xf5] = MMX_OP2(pmaddwd),
    [0xf6] = MMX_OP2(psadbw),
B
Blue Swirl 已提交
2784 2785
    [0xf7] = { (SSEFunc_0_epp)gen_helper_maskmov_mmx,
               (SSEFunc_0_epp)gen_helper_maskmov_xmm }, /* XXX: casts */
B
bellard 已提交
2786 2787 2788 2789 2790 2791 2792 2793 2794
    [0xf8] = MMX_OP2(psubb),
    [0xf9] = MMX_OP2(psubw),
    [0xfa] = MMX_OP2(psubl),
    [0xfb] = MMX_OP2(psubq),
    [0xfc] = MMX_OP2(paddb),
    [0xfd] = MMX_OP2(paddw),
    [0xfe] = MMX_OP2(paddl),
};

B
Blue Swirl 已提交
2795
static const SSEFunc_0_epp sse_op_table2[3 * 8][2] = {
B
bellard 已提交
2796 2797 2798 2799 2800 2801 2802
    [0 + 2] = MMX_OP2(psrlw),
    [0 + 4] = MMX_OP2(psraw),
    [0 + 6] = MMX_OP2(psllw),
    [8 + 2] = MMX_OP2(psrld),
    [8 + 4] = MMX_OP2(psrad),
    [8 + 6] = MMX_OP2(pslld),
    [16 + 2] = MMX_OP2(psrlq),
P
pbrook 已提交
2803
    [16 + 3] = { NULL, gen_helper_psrldq_xmm },
B
bellard 已提交
2804
    [16 + 6] = MMX_OP2(psllq),
P
pbrook 已提交
2805
    [16 + 7] = { NULL, gen_helper_pslldq_xmm },
B
bellard 已提交
2806 2807
};

B
Blue Swirl 已提交
2808
static const SSEFunc_0_epi sse_op_table3ai[] = {
P
pbrook 已提交
2809
    gen_helper_cvtsi2ss,
2810
    gen_helper_cvtsi2sd
B
Blue Swirl 已提交
2811
};
P
pbrook 已提交
2812

2813
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
2814
static const SSEFunc_0_epl sse_op_table3aq[] = {
2815 2816 2817 2818 2819
    gen_helper_cvtsq2ss,
    gen_helper_cvtsq2sd
};
#endif

B
Blue Swirl 已提交
2820
static const SSEFunc_i_ep sse_op_table3bi[] = {
P
pbrook 已提交
2821 2822
    gen_helper_cvttss2si,
    gen_helper_cvtss2si,
2823
    gen_helper_cvttsd2si,
2824
    gen_helper_cvtsd2si
B
bellard 已提交
2825
};
2826

2827
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
2828
static const SSEFunc_l_ep sse_op_table3bq[] = {
2829 2830
    gen_helper_cvttss2sq,
    gen_helper_cvtss2sq,
2831
    gen_helper_cvttsd2sq,
2832 2833 2834 2835
    gen_helper_cvtsd2sq
};
#endif

B
Blue Swirl 已提交
2836
static const SSEFunc_0_epp sse_op_table4[8][4] = {
B
bellard 已提交
2837 2838 2839 2840 2841 2842 2843 2844 2845
    SSE_FOP(cmpeq),
    SSE_FOP(cmplt),
    SSE_FOP(cmple),
    SSE_FOP(cmpunord),
    SSE_FOP(cmpneq),
    SSE_FOP(cmpnlt),
    SSE_FOP(cmpnle),
    SSE_FOP(cmpord),
};
2846

B
Blue Swirl 已提交
2847
static const SSEFunc_0_epp sse_op_table5[256] = {
P
pbrook 已提交
2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871
    [0x0c] = gen_helper_pi2fw,
    [0x0d] = gen_helper_pi2fd,
    [0x1c] = gen_helper_pf2iw,
    [0x1d] = gen_helper_pf2id,
    [0x8a] = gen_helper_pfnacc,
    [0x8e] = gen_helper_pfpnacc,
    [0x90] = gen_helper_pfcmpge,
    [0x94] = gen_helper_pfmin,
    [0x96] = gen_helper_pfrcp,
    [0x97] = gen_helper_pfrsqrt,
    [0x9a] = gen_helper_pfsub,
    [0x9e] = gen_helper_pfadd,
    [0xa0] = gen_helper_pfcmpgt,
    [0xa4] = gen_helper_pfmax,
    [0xa6] = gen_helper_movq, /* pfrcpit1; no need to actually increase precision */
    [0xa7] = gen_helper_movq, /* pfrsqit1 */
    [0xaa] = gen_helper_pfsubr,
    [0xae] = gen_helper_pfacc,
    [0xb0] = gen_helper_pfcmpeq,
    [0xb4] = gen_helper_pfmul,
    [0xb6] = gen_helper_movq, /* pfrcpit2 */
    [0xb7] = gen_helper_pmulhrw_mmx,
    [0xbb] = gen_helper_pswapd,
    [0xbf] = gen_helper_pavgb_mmx /* pavgusb */
A
aurel32 已提交
2872 2873
};

B
Blue Swirl 已提交
2874 2875
struct SSEOpHelper_epp {
    SSEFunc_0_epp op[2];
B
Blue Swirl 已提交
2876 2877 2878
    uint32_t ext_mask;
};

B
Blue Swirl 已提交
2879 2880
struct SSEOpHelper_eppi {
    SSEFunc_0_eppi op[2];
B
Blue Swirl 已提交
2881
    uint32_t ext_mask;
B
balrog 已提交
2882
};
B
Blue Swirl 已提交
2883

B
balrog 已提交
2884
#define SSSE3_OP(x) { MMX_OP2(x), CPUID_EXT_SSSE3 }
P
pbrook 已提交
2885 2886
#define SSE41_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_SSE41 }
#define SSE42_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_SSE42 }
B
balrog 已提交
2887
#define SSE41_SPECIAL { { NULL, SSE_SPECIAL }, CPUID_EXT_SSE41 }
2888 2889
#define PCLMULQDQ_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, \
        CPUID_EXT_PCLMULQDQ }
2890
#define AESNI_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_AES }
B
Blue Swirl 已提交
2891

B
Blue Swirl 已提交
2892
static const struct SSEOpHelper_epp sse_op_table6[256] = {
B
balrog 已提交
2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938
    [0x00] = SSSE3_OP(pshufb),
    [0x01] = SSSE3_OP(phaddw),
    [0x02] = SSSE3_OP(phaddd),
    [0x03] = SSSE3_OP(phaddsw),
    [0x04] = SSSE3_OP(pmaddubsw),
    [0x05] = SSSE3_OP(phsubw),
    [0x06] = SSSE3_OP(phsubd),
    [0x07] = SSSE3_OP(phsubsw),
    [0x08] = SSSE3_OP(psignb),
    [0x09] = SSSE3_OP(psignw),
    [0x0a] = SSSE3_OP(psignd),
    [0x0b] = SSSE3_OP(pmulhrsw),
    [0x10] = SSE41_OP(pblendvb),
    [0x14] = SSE41_OP(blendvps),
    [0x15] = SSE41_OP(blendvpd),
    [0x17] = SSE41_OP(ptest),
    [0x1c] = SSSE3_OP(pabsb),
    [0x1d] = SSSE3_OP(pabsw),
    [0x1e] = SSSE3_OP(pabsd),
    [0x20] = SSE41_OP(pmovsxbw),
    [0x21] = SSE41_OP(pmovsxbd),
    [0x22] = SSE41_OP(pmovsxbq),
    [0x23] = SSE41_OP(pmovsxwd),
    [0x24] = SSE41_OP(pmovsxwq),
    [0x25] = SSE41_OP(pmovsxdq),
    [0x28] = SSE41_OP(pmuldq),
    [0x29] = SSE41_OP(pcmpeqq),
    [0x2a] = SSE41_SPECIAL, /* movntqda */
    [0x2b] = SSE41_OP(packusdw),
    [0x30] = SSE41_OP(pmovzxbw),
    [0x31] = SSE41_OP(pmovzxbd),
    [0x32] = SSE41_OP(pmovzxbq),
    [0x33] = SSE41_OP(pmovzxwd),
    [0x34] = SSE41_OP(pmovzxwq),
    [0x35] = SSE41_OP(pmovzxdq),
    [0x37] = SSE42_OP(pcmpgtq),
    [0x38] = SSE41_OP(pminsb),
    [0x39] = SSE41_OP(pminsd),
    [0x3a] = SSE41_OP(pminuw),
    [0x3b] = SSE41_OP(pminud),
    [0x3c] = SSE41_OP(pmaxsb),
    [0x3d] = SSE41_OP(pmaxsd),
    [0x3e] = SSE41_OP(pmaxuw),
    [0x3f] = SSE41_OP(pmaxud),
    [0x40] = SSE41_OP(pmulld),
    [0x41] = SSE41_OP(phminposuw),
2939 2940 2941 2942 2943
    [0xdb] = AESNI_OP(aesimc),
    [0xdc] = AESNI_OP(aesenc),
    [0xdd] = AESNI_OP(aesenclast),
    [0xde] = AESNI_OP(aesdec),
    [0xdf] = AESNI_OP(aesdeclast),
B
balrog 已提交
2944 2945
};

B
Blue Swirl 已提交
2946
static const struct SSEOpHelper_eppi sse_op_table7[256] = {
B
balrog 已提交
2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964
    [0x08] = SSE41_OP(roundps),
    [0x09] = SSE41_OP(roundpd),
    [0x0a] = SSE41_OP(roundss),
    [0x0b] = SSE41_OP(roundsd),
    [0x0c] = SSE41_OP(blendps),
    [0x0d] = SSE41_OP(blendpd),
    [0x0e] = SSE41_OP(pblendw),
    [0x0f] = SSSE3_OP(palignr),
    [0x14] = SSE41_SPECIAL, /* pextrb */
    [0x15] = SSE41_SPECIAL, /* pextrw */
    [0x16] = SSE41_SPECIAL, /* pextrd/pextrq */
    [0x17] = SSE41_SPECIAL, /* extractps */
    [0x20] = SSE41_SPECIAL, /* pinsrb */
    [0x21] = SSE41_SPECIAL, /* insertps */
    [0x22] = SSE41_SPECIAL, /* pinsrd/pinsrq */
    [0x40] = SSE41_OP(dpps),
    [0x41] = SSE41_OP(dppd),
    [0x42] = SSE41_OP(mpsadbw),
2965
    [0x44] = PCLMULQDQ_OP(pclmulqdq),
B
balrog 已提交
2966 2967 2968 2969
    [0x60] = SSE42_OP(pcmpestrm),
    [0x61] = SSE42_OP(pcmpestri),
    [0x62] = SSE42_OP(pcmpistrm),
    [0x63] = SSE42_OP(pcmpistri),
2970
    [0xdf] = AESNI_OP(aeskeygenassist),
B
balrog 已提交
2971 2972
};

2973 2974
static void gen_sse(CPUX86State *env, DisasContext *s, int b,
                    target_ulong pc_start, int rex_r)
B
bellard 已提交
2975
{
2976
    int b1, op1_offset, op2_offset, is_xmm, val;
2977
    int modrm, mod, rm, reg;
B
Blue Swirl 已提交
2978 2979
    SSEFunc_0_epp sse_fn_epp;
    SSEFunc_0_eppi sse_fn_eppi;
B
Blue Swirl 已提交
2980
    SSEFunc_0_ppi sse_fn_ppi;
B
Blue Swirl 已提交
2981
    SSEFunc_0_eppt sse_fn_eppt;
2982
    TCGMemOp ot;
B
bellard 已提交
2983 2984

    b &= 0xff;
2985
    if (s->prefix & PREFIX_DATA)
B
bellard 已提交
2986
        b1 = 1;
2987
    else if (s->prefix & PREFIX_REPZ)
B
bellard 已提交
2988
        b1 = 2;
2989
    else if (s->prefix & PREFIX_REPNZ)
B
bellard 已提交
2990 2991 2992
        b1 = 3;
    else
        b1 = 0;
B
Blue Swirl 已提交
2993 2994
    sse_fn_epp = sse_op_table1[b][b1];
    if (!sse_fn_epp) {
B
bellard 已提交
2995
        goto illegal_op;
B
Blue Swirl 已提交
2996
    }
A
aurel32 已提交
2997
    if ((b <= 0x5f && b >= 0x10) || b == 0xc6 || b == 0xc2) {
B
bellard 已提交
2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017
        is_xmm = 1;
    } else {
        if (b1 == 0) {
            /* MMX case */
            is_xmm = 0;
        } else {
            is_xmm = 1;
        }
    }
    /* simple MMX/SSE operation */
    if (s->flags & HF_TS_MASK) {
        gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
        return;
    }
    if (s->flags & HF_EM_MASK) {
    illegal_op:
        gen_exception(s, EXCP06_ILLOP, pc_start - s->cs_base);
        return;
    }
    if (is_xmm && !(s->flags & HF_OSFXSR_MASK))
B
balrog 已提交
3018 3019
        if ((b != 0x38 && b != 0x3a) || (s->prefix & PREFIX_DATA))
            goto illegal_op;
3020 3021 3022 3023
    if (b == 0x0e) {
        if (!(s->cpuid_ext2_features & CPUID_EXT2_3DNOW))
            goto illegal_op;
        /* femms */
B
Blue Swirl 已提交
3024
        gen_helper_emms(cpu_env);
3025 3026 3027 3028
        return;
    }
    if (b == 0x77) {
        /* emms */
B
Blue Swirl 已提交
3029
        gen_helper_emms(cpu_env);
B
bellard 已提交
3030 3031 3032 3033 3034
        return;
    }
    /* prepare MMX state (XXX: optimize by storing fptt and fptags in
       the static cpu state) */
    if (!is_xmm) {
B
Blue Swirl 已提交
3035
        gen_helper_enter_mmx(cpu_env);
B
bellard 已提交
3036 3037
    }

3038
    modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
3039 3040 3041 3042
    reg = ((modrm >> 3) & 7);
    if (is_xmm)
        reg |= rex_r;
    mod = (modrm >> 6) & 3;
B
Blue Swirl 已提交
3043
    if (sse_fn_epp == SSE_SPECIAL) {
B
bellard 已提交
3044 3045 3046
        b |= (b1 << 8);
        switch(b) {
        case 0x0e7: /* movntq */
3047
            if (mod == 3)
B
bellard 已提交
3048
                goto illegal_op;
3049
            gen_lea_modrm(env, s, modrm);
3050
            gen_stq_env_A0(s, offsetof(CPUX86State, fpregs[reg].mmx));
B
bellard 已提交
3051 3052 3053 3054
            break;
        case 0x1e7: /* movntdq */
        case 0x02b: /* movntps */
        case 0x12b: /* movntps */
3055 3056
            if (mod == 3)
                goto illegal_op;
3057
            gen_lea_modrm(env, s, modrm);
3058
            gen_sto_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
3059
            break;
B
bellard 已提交
3060 3061
        case 0x3f0: /* lddqu */
            if (mod == 3)
B
bellard 已提交
3062
                goto illegal_op;
3063
            gen_lea_modrm(env, s, modrm);
3064
            gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
B
bellard 已提交
3065
            break;
3066 3067 3068 3069
        case 0x22b: /* movntss */
        case 0x32b: /* movntsd */
            if (mod == 3)
                goto illegal_op;
3070
            gen_lea_modrm(env, s, modrm);
3071
            if (b1 & 1) {
3072
                gen_stq_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
3073 3074 3075
            } else {
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
                    xmm_regs[reg].XMM_L(0)));
3076
                gen_op_st_v(s, MO_32, cpu_T[0], cpu_A0);
3077 3078
            }
            break;
B
bellard 已提交
3079
        case 0x6e: /* movd mm, ea */
B
bellard 已提交
3080
#ifdef TARGET_X86_64
3081
            if (s->dflag == MO_64) {
3082
                gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 0);
B
bellard 已提交
3083
                tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,fpregs[reg].mmx));
3084
            } else
B
bellard 已提交
3085 3086
#endif
            {
3087
                gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 0);
B
bellard 已提交
3088 3089
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                                 offsetof(CPUX86State,fpregs[reg].mmx));
P
pbrook 已提交
3090 3091
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
                gen_helper_movl_mm_T0_mmx(cpu_ptr0, cpu_tmp2_i32);
B
bellard 已提交
3092
            }
B
bellard 已提交
3093 3094
            break;
        case 0x16e: /* movd xmm, ea */
B
bellard 已提交
3095
#ifdef TARGET_X86_64
3096
            if (s->dflag == MO_64) {
3097
                gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 0);
B
bellard 已提交
3098 3099
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                                 offsetof(CPUX86State,xmm_regs[reg]));
P
pbrook 已提交
3100
                gen_helper_movq_mm_T0_xmm(cpu_ptr0, cpu_T[0]);
3101
            } else
B
bellard 已提交
3102 3103
#endif
            {
3104
                gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 0);
B
bellard 已提交
3105 3106
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                                 offsetof(CPUX86State,xmm_regs[reg]));
3107
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
3108
                gen_helper_movl_mm_T0_xmm(cpu_ptr0, cpu_tmp2_i32);
B
bellard 已提交
3109
            }
B
bellard 已提交
3110 3111 3112
            break;
        case 0x6f: /* movq mm, ea */
            if (mod != 3) {
3113
                gen_lea_modrm(env, s, modrm);
3114
                gen_ldq_env_A0(s, offsetof(CPUX86State, fpregs[reg].mmx));
B
bellard 已提交
3115 3116
            } else {
                rm = (modrm & 7);
3117
                tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env,
B
bellard 已提交
3118
                               offsetof(CPUX86State,fpregs[rm].mmx));
3119
                tcg_gen_st_i64(cpu_tmp1_i64, cpu_env,
B
bellard 已提交
3120
                               offsetof(CPUX86State,fpregs[reg].mmx));
B
bellard 已提交
3121 3122 3123 3124 3125 3126 3127 3128 3129
            }
            break;
        case 0x010: /* movups */
        case 0x110: /* movupd */
        case 0x028: /* movaps */
        case 0x128: /* movapd */
        case 0x16f: /* movdqa xmm, ea */
        case 0x26f: /* movdqu xmm, ea */
            if (mod != 3) {
3130
                gen_lea_modrm(env, s, modrm);
3131
                gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
B
bellard 已提交
3132 3133 3134 3135 3136 3137 3138 3139
            } else {
                rm = (modrm & 7) | REX_B(s);
                gen_op_movo(offsetof(CPUX86State,xmm_regs[reg]),
                            offsetof(CPUX86State,xmm_regs[rm]));
            }
            break;
        case 0x210: /* movss xmm, ea */
            if (mod != 3) {
3140
                gen_lea_modrm(env, s, modrm);
3141
                gen_op_ld_v(s, MO_32, cpu_T[0], cpu_A0);
B
bellard 已提交
3142
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
3143
                tcg_gen_movi_tl(cpu_T[0], 0);
B
bellard 已提交
3144 3145 3146
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(1)));
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(2)));
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(3)));
B
bellard 已提交
3147 3148 3149 3150 3151 3152 3153 3154
            } else {
                rm = (modrm & 7) | REX_B(s);
                gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)),
                            offsetof(CPUX86State,xmm_regs[rm].XMM_L(0)));
            }
            break;
        case 0x310: /* movsd xmm, ea */
            if (mod != 3) {
3155
                gen_lea_modrm(env, s, modrm);
3156 3157
                gen_ldq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(0)));
3158
                tcg_gen_movi_tl(cpu_T[0], 0);
B
bellard 已提交
3159 3160
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(2)));
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(3)));
B
bellard 已提交
3161 3162 3163 3164 3165 3166 3167 3168 3169
            } else {
                rm = (modrm & 7) | REX_B(s);
                gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)),
                            offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)));
            }
            break;
        case 0x012: /* movlps */
        case 0x112: /* movlpd */
            if (mod != 3) {
3170
                gen_lea_modrm(env, s, modrm);
3171 3172
                gen_ldq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3173 3174 3175 3176 3177 3178 3179
            } else {
                /* movhlps */
                rm = (modrm & 7) | REX_B(s);
                gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)),
                            offsetof(CPUX86State,xmm_regs[rm].XMM_Q(1)));
            }
            break;
B
bellard 已提交
3180 3181
        case 0x212: /* movsldup */
            if (mod != 3) {
3182
                gen_lea_modrm(env, s, modrm);
3183
                gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
B
bellard 已提交
3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197
            } else {
                rm = (modrm & 7) | REX_B(s);
                gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)),
                            offsetof(CPUX86State,xmm_regs[rm].XMM_L(0)));
                gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(2)),
                            offsetof(CPUX86State,xmm_regs[rm].XMM_L(2)));
            }
            gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(1)),
                        offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
            gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(3)),
                        offsetof(CPUX86State,xmm_regs[reg].XMM_L(2)));
            break;
        case 0x312: /* movddup */
            if (mod != 3) {
3198
                gen_lea_modrm(env, s, modrm);
3199 3200
                gen_ldq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3201 3202 3203 3204 3205 3206
            } else {
                rm = (modrm & 7) | REX_B(s);
                gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)),
                            offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)));
            }
            gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(1)),
B
bellard 已提交
3207
                        offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3208
            break;
B
bellard 已提交
3209 3210 3211
        case 0x016: /* movhps */
        case 0x116: /* movhpd */
            if (mod != 3) {
3212
                gen_lea_modrm(env, s, modrm);
3213 3214
                gen_ldq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(1)));
B
bellard 已提交
3215 3216 3217 3218 3219 3220 3221 3222 3223
            } else {
                /* movlhps */
                rm = (modrm & 7) | REX_B(s);
                gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(1)),
                            offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)));
            }
            break;
        case 0x216: /* movshdup */
            if (mod != 3) {
3224
                gen_lea_modrm(env, s, modrm);
3225
                gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
B
bellard 已提交
3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237
            } else {
                rm = (modrm & 7) | REX_B(s);
                gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(1)),
                            offsetof(CPUX86State,xmm_regs[rm].XMM_L(1)));
                gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(3)),
                            offsetof(CPUX86State,xmm_regs[rm].XMM_L(3)));
            }
            gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)),
                        offsetof(CPUX86State,xmm_regs[reg].XMM_L(1)));
            gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(2)),
                        offsetof(CPUX86State,xmm_regs[reg].XMM_L(3)));
            break;
3238 3239 3240 3241 3242 3243 3244
        case 0x178:
        case 0x378:
            {
                int bit_index, field_length;

                if (b1 == 1 && reg != 0)
                    goto illegal_op;
3245 3246
                field_length = cpu_ldub_code(env, s->pc++) & 0x3F;
                bit_index = cpu_ldub_code(env, s->pc++) & 0x3F;
3247 3248 3249
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
                    offsetof(CPUX86State,xmm_regs[reg]));
                if (b1 == 1)
B
Blue Swirl 已提交
3250 3251 3252
                    gen_helper_extrq_i(cpu_env, cpu_ptr0,
                                       tcg_const_i32(bit_index),
                                       tcg_const_i32(field_length));
3253
                else
B
Blue Swirl 已提交
3254 3255 3256
                    gen_helper_insertq_i(cpu_env, cpu_ptr0,
                                         tcg_const_i32(bit_index),
                                         tcg_const_i32(field_length));
3257 3258
            }
            break;
B
bellard 已提交
3259
        case 0x7e: /* movd ea, mm */
B
bellard 已提交
3260
#ifdef TARGET_X86_64
3261
            if (s->dflag == MO_64) {
B
bellard 已提交
3262 3263
                tcg_gen_ld_i64(cpu_T[0], cpu_env, 
                               offsetof(CPUX86State,fpregs[reg].mmx));
3264
                gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 1);
3265
            } else
B
bellard 已提交
3266 3267
#endif
            {
B
bellard 已提交
3268 3269
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, 
                                 offsetof(CPUX86State,fpregs[reg].mmx.MMX_L(0)));
3270
                gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 1);
B
bellard 已提交
3271
            }
B
bellard 已提交
3272 3273
            break;
        case 0x17e: /* movd ea, xmm */
B
bellard 已提交
3274
#ifdef TARGET_X86_64
3275
            if (s->dflag == MO_64) {
B
bellard 已提交
3276 3277
                tcg_gen_ld_i64(cpu_T[0], cpu_env, 
                               offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
3278
                gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 1);
3279
            } else
B
bellard 已提交
3280 3281
#endif
            {
B
bellard 已提交
3282 3283
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, 
                                 offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
3284
                gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 1);
B
bellard 已提交
3285
            }
B
bellard 已提交
3286 3287 3288
            break;
        case 0x27e: /* movq xmm, ea */
            if (mod != 3) {
3289
                gen_lea_modrm(env, s, modrm);
3290 3291
                gen_ldq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3292 3293 3294 3295 3296 3297 3298 3299 3300
            } else {
                rm = (modrm & 7) | REX_B(s);
                gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)),
                            offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)));
            }
            gen_op_movq_env_0(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(1)));
            break;
        case 0x7f: /* movq ea, mm */
            if (mod != 3) {
3301
                gen_lea_modrm(env, s, modrm);
3302
                gen_stq_env_A0(s, offsetof(CPUX86State, fpregs[reg].mmx));
B
bellard 已提交
3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315
            } else {
                rm = (modrm & 7);
                gen_op_movq(offsetof(CPUX86State,fpregs[rm].mmx),
                            offsetof(CPUX86State,fpregs[reg].mmx));
            }
            break;
        case 0x011: /* movups */
        case 0x111: /* movupd */
        case 0x029: /* movaps */
        case 0x129: /* movapd */
        case 0x17f: /* movdqa ea, xmm */
        case 0x27f: /* movdqu ea, xmm */
            if (mod != 3) {
3316
                gen_lea_modrm(env, s, modrm);
3317
                gen_sto_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
B
bellard 已提交
3318 3319 3320 3321 3322 3323 3324 3325
            } else {
                rm = (modrm & 7) | REX_B(s);
                gen_op_movo(offsetof(CPUX86State,xmm_regs[rm]),
                            offsetof(CPUX86State,xmm_regs[reg]));
            }
            break;
        case 0x211: /* movss ea, xmm */
            if (mod != 3) {
3326
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
3327
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
3328
                gen_op_st_v(s, MO_32, cpu_T[0], cpu_A0);
B
bellard 已提交
3329 3330 3331 3332 3333 3334 3335 3336
            } else {
                rm = (modrm & 7) | REX_B(s);
                gen_op_movl(offsetof(CPUX86State,xmm_regs[rm].XMM_L(0)),
                            offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
            }
            break;
        case 0x311: /* movsd ea, xmm */
            if (mod != 3) {
3337
                gen_lea_modrm(env, s, modrm);
3338 3339
                gen_stq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3340 3341 3342 3343 3344 3345 3346 3347 3348
            } else {
                rm = (modrm & 7) | REX_B(s);
                gen_op_movq(offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)),
                            offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
            }
            break;
        case 0x013: /* movlps */
        case 0x113: /* movlpd */
            if (mod != 3) {
3349
                gen_lea_modrm(env, s, modrm);
3350 3351
                gen_stq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3352 3353 3354 3355 3356 3357 3358
            } else {
                goto illegal_op;
            }
            break;
        case 0x017: /* movhps */
        case 0x117: /* movhpd */
            if (mod != 3) {
3359
                gen_lea_modrm(env, s, modrm);
3360 3361
                gen_stq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(1)));
B
bellard 已提交
3362 3363 3364 3365 3366 3367 3368 3369 3370 3371
            } else {
                goto illegal_op;
            }
            break;
        case 0x71: /* shift mm, im */
        case 0x72:
        case 0x73:
        case 0x171: /* shift xmm, im */
        case 0x172:
        case 0x173:
3372 3373 3374
            if (b1 >= 2) {
	        goto illegal_op;
            }
3375
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
3376
            if (is_xmm) {
3377
                tcg_gen_movi_tl(cpu_T[0], val);
B
bellard 已提交
3378
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(0)));
3379
                tcg_gen_movi_tl(cpu_T[0], 0);
B
bellard 已提交
3380
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(1)));
B
bellard 已提交
3381 3382
                op1_offset = offsetof(CPUX86State,xmm_t0);
            } else {
3383
                tcg_gen_movi_tl(cpu_T[0], val);
B
bellard 已提交
3384
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,mmx_t0.MMX_L(0)));
3385
                tcg_gen_movi_tl(cpu_T[0], 0);
B
bellard 已提交
3386
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,mmx_t0.MMX_L(1)));
B
bellard 已提交
3387 3388
                op1_offset = offsetof(CPUX86State,mmx_t0);
            }
B
Blue Swirl 已提交
3389 3390 3391
            sse_fn_epp = sse_op_table2[((b - 1) & 3) * 8 +
                                       (((modrm >> 3)) & 7)][b1];
            if (!sse_fn_epp) {
B
bellard 已提交
3392
                goto illegal_op;
B
Blue Swirl 已提交
3393
            }
B
bellard 已提交
3394 3395 3396 3397 3398 3399 3400
            if (is_xmm) {
                rm = (modrm & 7) | REX_B(s);
                op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
            } else {
                rm = (modrm & 7);
                op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
            }
B
bellard 已提交
3401 3402
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op2_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op1_offset);
B
Blue Swirl 已提交
3403
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3404 3405 3406
            break;
        case 0x050: /* movmskps */
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
3407 3408
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                             offsetof(CPUX86State,xmm_regs[rm]));
B
Blue Swirl 已提交
3409
            gen_helper_movmskps(cpu_tmp2_i32, cpu_env, cpu_ptr0);
3410
            tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp2_i32);
B
bellard 已提交
3411 3412 3413
            break;
        case 0x150: /* movmskpd */
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
3414 3415
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                             offsetof(CPUX86State,xmm_regs[rm]));
B
Blue Swirl 已提交
3416
            gen_helper_movmskpd(cpu_tmp2_i32, cpu_env, cpu_ptr0);
3417
            tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp2_i32);
B
bellard 已提交
3418 3419 3420
            break;
        case 0x02a: /* cvtpi2ps */
        case 0x12a: /* cvtpi2pd */
B
Blue Swirl 已提交
3421
            gen_helper_enter_mmx(cpu_env);
B
bellard 已提交
3422
            if (mod != 3) {
3423
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
3424
                op2_offset = offsetof(CPUX86State,mmx_t0);
3425
                gen_ldq_env_A0(s, op2_offset);
B
bellard 已提交
3426 3427 3428 3429 3430
            } else {
                rm = (modrm & 7);
                op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
            }
            op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
B
bellard 已提交
3431 3432
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
bellard 已提交
3433 3434
            switch(b >> 8) {
            case 0x0:
B
Blue Swirl 已提交
3435
                gen_helper_cvtpi2ps(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3436 3437 3438
                break;
            default:
            case 0x1:
B
Blue Swirl 已提交
3439
                gen_helper_cvtpi2pd(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3440 3441 3442 3443 3444
                break;
            }
            break;
        case 0x22a: /* cvtsi2ss */
        case 0x32a: /* cvtsi2sd */
3445
            ot = mo_64_32(s->dflag);
3446
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
bellard 已提交
3447
            op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
B
bellard 已提交
3448
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
3449
            if (ot == MO_32) {
B
Blue Swirl 已提交
3450
                SSEFunc_0_epi sse_fn_epi = sse_op_table3ai[(b >> 8) & 1];
B
bellard 已提交
3451
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
3452
                sse_fn_epi(cpu_env, cpu_ptr0, cpu_tmp2_i32);
B
bellard 已提交
3453
            } else {
3454
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
3455 3456
                SSEFunc_0_epl sse_fn_epl = sse_op_table3aq[(b >> 8) & 1];
                sse_fn_epl(cpu_env, cpu_ptr0, cpu_T[0]);
3457 3458 3459
#else
                goto illegal_op;
#endif
B
bellard 已提交
3460
            }
B
bellard 已提交
3461 3462 3463 3464 3465
            break;
        case 0x02c: /* cvttps2pi */
        case 0x12c: /* cvttpd2pi */
        case 0x02d: /* cvtps2pi */
        case 0x12d: /* cvtpd2pi */
B
Blue Swirl 已提交
3466
            gen_helper_enter_mmx(cpu_env);
B
bellard 已提交
3467
            if (mod != 3) {
3468
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
3469
                op2_offset = offsetof(CPUX86State,xmm_t0);
3470
                gen_ldo_env_A0(s, op2_offset);
B
bellard 已提交
3471 3472 3473 3474 3475
            } else {
                rm = (modrm & 7) | REX_B(s);
                op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
            }
            op1_offset = offsetof(CPUX86State,fpregs[reg & 7].mmx);
B
bellard 已提交
3476 3477
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
bellard 已提交
3478 3479
            switch(b) {
            case 0x02c:
B
Blue Swirl 已提交
3480
                gen_helper_cvttps2pi(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3481 3482
                break;
            case 0x12c:
B
Blue Swirl 已提交
3483
                gen_helper_cvttpd2pi(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3484 3485
                break;
            case 0x02d:
B
Blue Swirl 已提交
3486
                gen_helper_cvtps2pi(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3487 3488
                break;
            case 0x12d:
B
Blue Swirl 已提交
3489
                gen_helper_cvtpd2pi(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3490 3491 3492 3493 3494 3495 3496
                break;
            }
            break;
        case 0x22c: /* cvttss2si */
        case 0x32c: /* cvttsd2si */
        case 0x22d: /* cvtss2si */
        case 0x32d: /* cvtsd2si */
3497
            ot = mo_64_32(s->dflag);
B
bellard 已提交
3498
            if (mod != 3) {
3499
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
3500
                if ((b >> 8) & 1) {
3501
                    gen_ldq_env_A0(s, offsetof(CPUX86State, xmm_t0.XMM_Q(0)));
B
bellard 已提交
3502
                } else {
3503
                    gen_op_ld_v(s, MO_32, cpu_T[0], cpu_A0);
B
bellard 已提交
3504
                    tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(0)));
B
bellard 已提交
3505 3506 3507 3508 3509 3510
                }
                op2_offset = offsetof(CPUX86State,xmm_t0);
            } else {
                rm = (modrm & 7) | REX_B(s);
                op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
            }
B
bellard 已提交
3511
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op2_offset);
3512
            if (ot == MO_32) {
B
Blue Swirl 已提交
3513
                SSEFunc_i_ep sse_fn_i_ep =
3514
                    sse_op_table3bi[((b >> 7) & 2) | (b & 1)];
B
Blue Swirl 已提交
3515
                sse_fn_i_ep(cpu_tmp2_i32, cpu_env, cpu_ptr0);
3516
                tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
3517
            } else {
3518
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
3519
                SSEFunc_l_ep sse_fn_l_ep =
3520
                    sse_op_table3bq[((b >> 7) & 2) | (b & 1)];
B
Blue Swirl 已提交
3521
                sse_fn_l_ep(cpu_T[0], cpu_env, cpu_ptr0);
3522 3523 3524
#else
                goto illegal_op;
#endif
B
bellard 已提交
3525
            }
3526
            gen_op_mov_reg_v(ot, reg, cpu_T[0]);
B
bellard 已提交
3527 3528
            break;
        case 0xc4: /* pinsrw */
3529
        case 0x1c4:
B
bellard 已提交
3530
            s->rip_offset = 1;
3531
            gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
3532
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
3533 3534
            if (b1) {
                val &= 7;
B
bellard 已提交
3535 3536
                tcg_gen_st16_tl(cpu_T[0], cpu_env,
                                offsetof(CPUX86State,xmm_regs[reg].XMM_W(val)));
B
bellard 已提交
3537 3538
            } else {
                val &= 3;
B
bellard 已提交
3539 3540
                tcg_gen_st16_tl(cpu_T[0], cpu_env,
                                offsetof(CPUX86State,fpregs[reg].mmx.MMX_W(val)));
B
bellard 已提交
3541 3542 3543
            }
            break;
        case 0xc5: /* pextrw */
3544
        case 0x1c5:
B
bellard 已提交
3545 3546
            if (mod != 3)
                goto illegal_op;
3547
            ot = mo_64_32(s->dflag);
3548
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
3549 3550 3551
            if (b1) {
                val &= 7;
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
3552 3553
                tcg_gen_ld16u_tl(cpu_T[0], cpu_env,
                                 offsetof(CPUX86State,xmm_regs[rm].XMM_W(val)));
B
bellard 已提交
3554 3555 3556
            } else {
                val &= 3;
                rm = (modrm & 7);
B
bellard 已提交
3557 3558
                tcg_gen_ld16u_tl(cpu_T[0], cpu_env,
                                offsetof(CPUX86State,fpregs[rm].mmx.MMX_W(val)));
B
bellard 已提交
3559 3560
            }
            reg = ((modrm >> 3) & 7) | rex_r;
3561
            gen_op_mov_reg_v(ot, reg, cpu_T[0]);
B
bellard 已提交
3562 3563 3564
            break;
        case 0x1d6: /* movq ea, xmm */
            if (mod != 3) {
3565
                gen_lea_modrm(env, s, modrm);
3566 3567
                gen_stq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3568 3569 3570 3571 3572 3573 3574 3575
            } else {
                rm = (modrm & 7) | REX_B(s);
                gen_op_movq(offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)),
                            offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
                gen_op_movq_env_0(offsetof(CPUX86State,xmm_regs[rm].XMM_Q(1)));
            }
            break;
        case 0x2d6: /* movq2dq */
B
Blue Swirl 已提交
3576
            gen_helper_enter_mmx(cpu_env);
3577 3578 3579 3580
            rm = (modrm & 7);
            gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)),
                        offsetof(CPUX86State,fpregs[rm].mmx));
            gen_op_movq_env_0(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(1)));
B
bellard 已提交
3581 3582
            break;
        case 0x3d6: /* movdq2q */
B
Blue Swirl 已提交
3583
            gen_helper_enter_mmx(cpu_env);
3584 3585 3586
            rm = (modrm & 7) | REX_B(s);
            gen_op_movq(offsetof(CPUX86State,fpregs[reg & 7].mmx),
                        offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)));
B
bellard 已提交
3587 3588 3589 3590 3591 3592 3593
            break;
        case 0xd7: /* pmovmskb */
        case 0x1d7:
            if (mod != 3)
                goto illegal_op;
            if (b1) {
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
3594
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, offsetof(CPUX86State,xmm_regs[rm]));
B
Blue Swirl 已提交
3595
                gen_helper_pmovmskb_xmm(cpu_tmp2_i32, cpu_env, cpu_ptr0);
B
bellard 已提交
3596 3597
            } else {
                rm = (modrm & 7);
B
bellard 已提交
3598
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, offsetof(CPUX86State,fpregs[rm].mmx));
B
Blue Swirl 已提交
3599
                gen_helper_pmovmskb_mmx(cpu_tmp2_i32, cpu_env, cpu_ptr0);
B
bellard 已提交
3600 3601
            }
            reg = ((modrm >> 3) & 7) | rex_r;
3602
            tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp2_i32);
B
bellard 已提交
3603
            break;
R
Richard Henderson 已提交
3604

B
balrog 已提交
3605
        case 0x138:
3606
        case 0x038:
B
balrog 已提交
3607
            b = modrm;
R
Richard Henderson 已提交
3608 3609 3610
            if ((b & 0xf0) == 0xf0) {
                goto do_0f_38_fx;
            }
3611
            modrm = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
3612 3613 3614
            rm = modrm & 7;
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
3615 3616 3617
            if (b1 >= 2) {
                goto illegal_op;
            }
B
balrog 已提交
3618

B
Blue Swirl 已提交
3619 3620
            sse_fn_epp = sse_op_table6[b].op[b1];
            if (!sse_fn_epp) {
B
balrog 已提交
3621
                goto illegal_op;
B
Blue Swirl 已提交
3622
            }
B
balrog 已提交
3623 3624
            if (!(s->cpuid_ext_features & sse_op_table6[b].ext_mask))
                goto illegal_op;
B
balrog 已提交
3625 3626 3627 3628 3629 3630 3631

            if (b1) {
                op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
                if (mod == 3) {
                    op2_offset = offsetof(CPUX86State,xmm_regs[rm | REX_B(s)]);
                } else {
                    op2_offset = offsetof(CPUX86State,xmm_t0);
3632
                    gen_lea_modrm(env, s, modrm);
B
balrog 已提交
3633 3634 3635 3636
                    switch (b) {
                    case 0x20: case 0x30: /* pmovsxbw, pmovzxbw */
                    case 0x23: case 0x33: /* pmovsxwd, pmovzxwd */
                    case 0x25: case 0x35: /* pmovsxdq, pmovzxdq */
3637
                        gen_ldq_env_A0(s, op2_offset +
B
balrog 已提交
3638 3639 3640 3641
                                        offsetof(XMMReg, XMM_Q(0)));
                        break;
                    case 0x21: case 0x31: /* pmovsxbd, pmovzxbd */
                    case 0x24: case 0x34: /* pmovsxwq, pmovzxwq */
3642 3643
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
balrog 已提交
3644 3645 3646 3647
                        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, op2_offset +
                                        offsetof(XMMReg, XMM_L(0)));
                        break;
                    case 0x22: case 0x32: /* pmovsxbq, pmovzxbq */
3648 3649
                        tcg_gen_qemu_ld_tl(cpu_tmp0, cpu_A0,
                                           s->mem_index, MO_LEUW);
B
balrog 已提交
3650 3651 3652 3653
                        tcg_gen_st16_tl(cpu_tmp0, cpu_env, op2_offset +
                                        offsetof(XMMReg, XMM_W(0)));
                        break;
                    case 0x2a:            /* movntqda */
3654
                        gen_ldo_env_A0(s, op1_offset);
B
balrog 已提交
3655 3656
                        return;
                    default:
3657
                        gen_ldo_env_A0(s, op2_offset);
B
balrog 已提交
3658
                    }
B
balrog 已提交
3659 3660 3661 3662 3663 3664 3665
                }
            } else {
                op1_offset = offsetof(CPUX86State,fpregs[reg].mmx);
                if (mod == 3) {
                    op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
                } else {
                    op2_offset = offsetof(CPUX86State,mmx_t0);
3666
                    gen_lea_modrm(env, s, modrm);
3667
                    gen_ldq_env_A0(s, op2_offset);
B
balrog 已提交
3668 3669
                }
            }
B
Blue Swirl 已提交
3670
            if (sse_fn_epp == SSE_SPECIAL) {
B
balrog 已提交
3671
                goto illegal_op;
B
Blue Swirl 已提交
3672
            }
B
balrog 已提交
3673

B
balrog 已提交
3674 3675
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
3676
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
balrog 已提交
3677

3678 3679 3680
            if (b == 0x17) {
                set_cc_op(s, CC_OP_EFLAGS);
            }
B
balrog 已提交
3681
            break;
R
Richard Henderson 已提交
3682 3683 3684 3685 3686 3687

        case 0x238:
        case 0x338:
        do_0f_38_fx:
            /* Various integer extensions at 0f 38 f[0-f].  */
            b = modrm | (b1 << 8);
3688
            modrm = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
3689 3690
            reg = ((modrm >> 3) & 7) | rex_r;

R
Richard Henderson 已提交
3691 3692 3693 3694 3695 3696 3697 3698
            switch (b) {
            case 0x3f0: /* crc32 Gd,Eb */
            case 0x3f1: /* crc32 Gd,Ey */
            do_crc32:
                if (!(s->cpuid_ext_features & CPUID_EXT_SSE42)) {
                    goto illegal_op;
                }
                if ((b & 0xff) == 0xf0) {
3699
                    ot = MO_8;
3700
                } else if (s->dflag != MO_64) {
3701
                    ot = (s->prefix & PREFIX_DATA ? MO_16 : MO_32);
R
Richard Henderson 已提交
3702
                } else {
3703
                    ot = MO_64;
R
Richard Henderson 已提交
3704
                }
B
balrog 已提交
3705

3706
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[reg]);
R
Richard Henderson 已提交
3707 3708 3709
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                gen_helper_crc32(cpu_T[0], cpu_tmp2_i32,
                                 cpu_T[0], tcg_const_i32(8 << ot));
B
balrog 已提交
3710

3711
                ot = mo_64_32(s->dflag);
3712
                gen_op_mov_reg_v(ot, reg, cpu_T[0]);
R
Richard Henderson 已提交
3713
                break;
B
balrog 已提交
3714

R
Richard Henderson 已提交
3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728
            case 0x1f0: /* crc32 or movbe */
            case 0x1f1:
                /* For these insns, the f3 prefix is supposed to have priority
                   over the 66 prefix, but that's not what we implement above
                   setting b1.  */
                if (s->prefix & PREFIX_REPNZ) {
                    goto do_crc32;
                }
                /* FALLTHRU */
            case 0x0f0: /* movbe Gy,My */
            case 0x0f1: /* movbe My,Gy */
                if (!(s->cpuid_ext_features & CPUID_EXT_MOVBE)) {
                    goto illegal_op;
                }
3729
                if (s->dflag != MO_64) {
3730
                    ot = (s->prefix & PREFIX_DATA ? MO_16 : MO_32);
R
Richard Henderson 已提交
3731
                } else {
3732
                    ot = MO_64;
R
Richard Henderson 已提交
3733 3734
                }

3735
                gen_lea_modrm(env, s, modrm);
R
Richard Henderson 已提交
3736
                if ((b & 1) == 0) {
3737 3738
                    tcg_gen_qemu_ld_tl(cpu_T[0], cpu_A0,
                                       s->mem_index, ot | MO_BE);
3739
                    gen_op_mov_reg_v(ot, reg, cpu_T[0]);
R
Richard Henderson 已提交
3740
                } else {
3741 3742
                    tcg_gen_qemu_st_tl(cpu_regs[reg], cpu_A0,
                                       s->mem_index, ot | MO_BE);
R
Richard Henderson 已提交
3743 3744 3745
                }
                break;

R
Richard Henderson 已提交
3746 3747 3748 3749 3750 3751
            case 0x0f2: /* andn Gy, By, Ey */
                if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI1)
                    || !(s->prefix & PREFIX_VEX)
                    || s->vex_l != 0) {
                    goto illegal_op;
                }
3752
                ot = mo_64_32(s->dflag);
R
Richard Henderson 已提交
3753 3754
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                tcg_gen_andc_tl(cpu_T[0], cpu_regs[s->vex_v], cpu_T[0]);
3755
                gen_op_mov_reg_v(ot, reg, cpu_T[0]);
R
Richard Henderson 已提交
3756 3757 3758 3759
                gen_op_update1_cc();
                set_cc_op(s, CC_OP_LOGICB + ot);
                break;

R
Richard Henderson 已提交
3760 3761 3762 3763 3764 3765
            case 0x0f7: /* bextr Gy, Ey, By */
                if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI1)
                    || !(s->prefix & PREFIX_VEX)
                    || s->vex_l != 0) {
                    goto illegal_op;
                }
3766
                ot = mo_64_32(s->dflag);
R
Richard Henderson 已提交
3767 3768 3769 3770 3771 3772 3773 3774 3775
                {
                    TCGv bound, zero;

                    gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                    /* Extract START, and shift the operand.
                       Shifts larger than operand size get zeros.  */
                    tcg_gen_ext8u_tl(cpu_A0, cpu_regs[s->vex_v]);
                    tcg_gen_shr_tl(cpu_T[0], cpu_T[0], cpu_A0);

3776
                    bound = tcg_const_tl(ot == MO_64 ? 63 : 31);
R
Richard Henderson 已提交
3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793
                    zero = tcg_const_tl(0);
                    tcg_gen_movcond_tl(TCG_COND_LEU, cpu_T[0], cpu_A0, bound,
                                       cpu_T[0], zero);
                    tcg_temp_free(zero);

                    /* Extract the LEN into a mask.  Lengths larger than
                       operand size get all ones.  */
                    tcg_gen_shri_tl(cpu_A0, cpu_regs[s->vex_v], 8);
                    tcg_gen_ext8u_tl(cpu_A0, cpu_A0);
                    tcg_gen_movcond_tl(TCG_COND_LEU, cpu_A0, cpu_A0, bound,
                                       cpu_A0, bound);
                    tcg_temp_free(bound);
                    tcg_gen_movi_tl(cpu_T[1], 1);
                    tcg_gen_shl_tl(cpu_T[1], cpu_T[1], cpu_A0);
                    tcg_gen_subi_tl(cpu_T[1], cpu_T[1], 1);
                    tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_T[1]);

3794
                    gen_op_mov_reg_v(ot, reg, cpu_T[0]);
R
Richard Henderson 已提交
3795 3796 3797 3798 3799
                    gen_op_update1_cc();
                    set_cc_op(s, CC_OP_LOGICB + ot);
                }
                break;

R
Richard Henderson 已提交
3800 3801 3802 3803 3804 3805
            case 0x0f5: /* bzhi Gy, Ey, By */
                if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI2)
                    || !(s->prefix & PREFIX_VEX)
                    || s->vex_l != 0) {
                    goto illegal_op;
                }
3806
                ot = mo_64_32(s->dflag);
R
Richard Henderson 已提交
3807 3808 3809
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                tcg_gen_ext8u_tl(cpu_T[1], cpu_regs[s->vex_v]);
                {
3810
                    TCGv bound = tcg_const_tl(ot == MO_64 ? 63 : 31);
R
Richard Henderson 已提交
3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821
                    /* Note that since we're using BMILG (in order to get O
                       cleared) we need to store the inverse into C.  */
                    tcg_gen_setcond_tl(TCG_COND_LT, cpu_cc_src,
                                       cpu_T[1], bound);
                    tcg_gen_movcond_tl(TCG_COND_GT, cpu_T[1], cpu_T[1],
                                       bound, bound, cpu_T[1]);
                    tcg_temp_free(bound);
                }
                tcg_gen_movi_tl(cpu_A0, -1);
                tcg_gen_shl_tl(cpu_A0, cpu_A0, cpu_T[1]);
                tcg_gen_andc_tl(cpu_T[0], cpu_T[0], cpu_A0);
3822
                gen_op_mov_reg_v(ot, reg, cpu_T[0]);
R
Richard Henderson 已提交
3823 3824 3825 3826
                gen_op_update1_cc();
                set_cc_op(s, CC_OP_BMILGB + ot);
                break;

R
Richard Henderson 已提交
3827 3828 3829 3830 3831 3832
            case 0x3f6: /* mulx By, Gy, rdx, Ey */
                if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI2)
                    || !(s->prefix & PREFIX_VEX)
                    || s->vex_l != 0) {
                    goto illegal_op;
                }
3833
                ot = mo_64_32(s->dflag);
R
Richard Henderson 已提交
3834 3835 3836
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                switch (ot) {
                default:
3837 3838 3839 3840 3841 3842
                    tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
                    tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_regs[R_EDX]);
                    tcg_gen_mulu2_i32(cpu_tmp2_i32, cpu_tmp3_i32,
                                      cpu_tmp2_i32, cpu_tmp3_i32);
                    tcg_gen_extu_i32_tl(cpu_regs[s->vex_v], cpu_tmp2_i32);
                    tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp3_i32);
R
Richard Henderson 已提交
3843 3844
                    break;
#ifdef TARGET_X86_64
3845
                case MO_64:
3846 3847
                    tcg_gen_mulu2_i64(cpu_regs[s->vex_v], cpu_regs[reg],
                                      cpu_T[0], cpu_regs[R_EDX]);
R
Richard Henderson 已提交
3848 3849 3850 3851 3852
                    break;
#endif
                }
                break;

3853 3854 3855 3856 3857 3858
            case 0x3f5: /* pdep Gy, By, Ey */
                if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI2)
                    || !(s->prefix & PREFIX_VEX)
                    || s->vex_l != 0) {
                    goto illegal_op;
                }
3859
                ot = mo_64_32(s->dflag);
3860 3861 3862
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                /* Note that by zero-extending the mask operand, we
                   automatically handle zero-extending the result.  */
3863
                if (ot == MO_64) {
3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876
                    tcg_gen_mov_tl(cpu_T[1], cpu_regs[s->vex_v]);
                } else {
                    tcg_gen_ext32u_tl(cpu_T[1], cpu_regs[s->vex_v]);
                }
                gen_helper_pdep(cpu_regs[reg], cpu_T[0], cpu_T[1]);
                break;

            case 0x2f5: /* pext Gy, By, Ey */
                if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI2)
                    || !(s->prefix & PREFIX_VEX)
                    || s->vex_l != 0) {
                    goto illegal_op;
                }
3877
                ot = mo_64_32(s->dflag);
3878 3879 3880
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                /* Note that by zero-extending the mask operand, we
                   automatically handle zero-extending the result.  */
3881
                if (ot == MO_64) {
3882 3883 3884 3885 3886 3887 3888
                    tcg_gen_mov_tl(cpu_T[1], cpu_regs[s->vex_v]);
                } else {
                    tcg_gen_ext32u_tl(cpu_T[1], cpu_regs[s->vex_v]);
                }
                gen_helper_pext(cpu_regs[reg], cpu_T[0], cpu_T[1]);
                break;

3889 3890 3891 3892 3893
            case 0x1f6: /* adcx Gy, Ey */
            case 0x2f6: /* adox Gy, Ey */
                if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_ADX)) {
                    goto illegal_op;
                } else {
3894
                    TCGv carry_in, carry_out, zero;
3895 3896
                    int end_op;

3897
                    ot = mo_64_32(s->dflag);
3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924
                    gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);

                    /* Re-use the carry-out from a previous round.  */
                    TCGV_UNUSED(carry_in);
                    carry_out = (b == 0x1f6 ? cpu_cc_dst : cpu_cc_src2);
                    switch (s->cc_op) {
                    case CC_OP_ADCX:
                        if (b == 0x1f6) {
                            carry_in = cpu_cc_dst;
                            end_op = CC_OP_ADCX;
                        } else {
                            end_op = CC_OP_ADCOX;
                        }
                        break;
                    case CC_OP_ADOX:
                        if (b == 0x1f6) {
                            end_op = CC_OP_ADCOX;
                        } else {
                            carry_in = cpu_cc_src2;
                            end_op = CC_OP_ADOX;
                        }
                        break;
                    case CC_OP_ADCOX:
                        end_op = CC_OP_ADCOX;
                        carry_in = carry_out;
                        break;
                    default:
3925
                        end_op = (b == 0x1f6 ? CC_OP_ADCX : CC_OP_ADOX);
3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940
                        break;
                    }
                    /* If we can't reuse carry-out, get it out of EFLAGS.  */
                    if (TCGV_IS_UNUSED(carry_in)) {
                        if (s->cc_op != CC_OP_ADCX && s->cc_op != CC_OP_ADOX) {
                            gen_compute_eflags(s);
                        }
                        carry_in = cpu_tmp0;
                        tcg_gen_shri_tl(carry_in, cpu_cc_src,
                                        ctz32(b == 0x1f6 ? CC_C : CC_O));
                        tcg_gen_andi_tl(carry_in, carry_in, 1);
                    }

                    switch (ot) {
#ifdef TARGET_X86_64
3941
                    case MO_32:
3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953
                        /* If we know TL is 64-bit, and we want a 32-bit
                           result, just do everything in 64-bit arithmetic.  */
                        tcg_gen_ext32u_i64(cpu_regs[reg], cpu_regs[reg]);
                        tcg_gen_ext32u_i64(cpu_T[0], cpu_T[0]);
                        tcg_gen_add_i64(cpu_T[0], cpu_T[0], cpu_regs[reg]);
                        tcg_gen_add_i64(cpu_T[0], cpu_T[0], carry_in);
                        tcg_gen_ext32u_i64(cpu_regs[reg], cpu_T[0]);
                        tcg_gen_shri_i64(carry_out, cpu_T[0], 32);
                        break;
#endif
                    default:
                        /* Otherwise compute the carry-out in two steps.  */
3954 3955 3956 3957 3958 3959 3960 3961
                        zero = tcg_const_tl(0);
                        tcg_gen_add2_tl(cpu_T[0], carry_out,
                                        cpu_T[0], zero,
                                        carry_in, zero);
                        tcg_gen_add2_tl(cpu_regs[reg], carry_out,
                                        cpu_regs[reg], carry_out,
                                        cpu_T[0], zero);
                        tcg_temp_free(zero);
3962 3963 3964 3965 3966 3967
                        break;
                    }
                    set_cc_op(s, end_op);
                }
                break;

3968 3969 3970 3971 3972 3973 3974 3975
            case 0x1f7: /* shlx Gy, Ey, By */
            case 0x2f7: /* sarx Gy, Ey, By */
            case 0x3f7: /* shrx Gy, Ey, By */
                if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI2)
                    || !(s->prefix & PREFIX_VEX)
                    || s->vex_l != 0) {
                    goto illegal_op;
                }
3976
                ot = mo_64_32(s->dflag);
3977
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
3978
                if (ot == MO_64) {
3979 3980 3981 3982 3983 3984 3985
                    tcg_gen_andi_tl(cpu_T[1], cpu_regs[s->vex_v], 63);
                } else {
                    tcg_gen_andi_tl(cpu_T[1], cpu_regs[s->vex_v], 31);
                }
                if (b == 0x1f7) {
                    tcg_gen_shl_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
                } else if (b == 0x2f7) {
3986
                    if (ot != MO_64) {
3987 3988 3989 3990
                        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
                    }
                    tcg_gen_sar_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
                } else {
3991
                    if (ot != MO_64) {
3992 3993 3994 3995
                        tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
                    }
                    tcg_gen_shr_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
                }
3996
                gen_op_mov_reg_v(ot, reg, cpu_T[0]);
3997 3998
                break;

3999 4000 4001 4002 4003 4004 4005 4006 4007
            case 0x0f3:
            case 0x1f3:
            case 0x2f3:
            case 0x3f3: /* Group 17 */
                if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI1)
                    || !(s->prefix & PREFIX_VEX)
                    || s->vex_l != 0) {
                    goto illegal_op;
                }
4008
                ot = mo_64_32(s->dflag);
4009 4010 4011 4012 4013 4014
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);

                switch (reg & 7) {
                case 1: /* blsr By,Ey */
                    tcg_gen_neg_tl(cpu_T[1], cpu_T[0]);
                    tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
4015
                    gen_op_mov_reg_v(ot, s->vex_v, cpu_T[0]);
4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040
                    gen_op_update2_cc();
                    set_cc_op(s, CC_OP_BMILGB + ot);
                    break;

                case 2: /* blsmsk By,Ey */
                    tcg_gen_mov_tl(cpu_cc_src, cpu_T[0]);
                    tcg_gen_subi_tl(cpu_T[0], cpu_T[0], 1);
                    tcg_gen_xor_tl(cpu_T[0], cpu_T[0], cpu_cc_src);
                    tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                    set_cc_op(s, CC_OP_BMILGB + ot);
                    break;

                case 3: /* blsi By, Ey */
                    tcg_gen_mov_tl(cpu_cc_src, cpu_T[0]);
                    tcg_gen_subi_tl(cpu_T[0], cpu_T[0], 1);
                    tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_cc_src);
                    tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                    set_cc_op(s, CC_OP_BMILGB + ot);
                    break;

                default:
                    goto illegal_op;
                }
                break;

R
Richard Henderson 已提交
4041 4042 4043
            default:
                goto illegal_op;
            }
B
balrog 已提交
4044
            break;
R
Richard Henderson 已提交
4045

B
balrog 已提交
4046 4047
        case 0x03a:
        case 0x13a:
B
balrog 已提交
4048
            b = modrm;
4049
            modrm = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
4050 4051 4052
            rm = modrm & 7;
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
4053 4054 4055
            if (b1 >= 2) {
                goto illegal_op;
            }
B
balrog 已提交
4056

B
Blue Swirl 已提交
4057 4058
            sse_fn_eppi = sse_op_table7[b].op[b1];
            if (!sse_fn_eppi) {
B
balrog 已提交
4059
                goto illegal_op;
B
Blue Swirl 已提交
4060
            }
B
balrog 已提交
4061 4062 4063
            if (!(s->cpuid_ext_features & sse_op_table7[b].ext_mask))
                goto illegal_op;

B
Blue Swirl 已提交
4064
            if (sse_fn_eppi == SSE_SPECIAL) {
4065
                ot = mo_64_32(s->dflag);
B
balrog 已提交
4066 4067
                rm = (modrm & 7) | REX_B(s);
                if (mod != 3)
4068
                    gen_lea_modrm(env, s, modrm);
B
balrog 已提交
4069
                reg = ((modrm >> 3) & 7) | rex_r;
4070
                val = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
4071 4072 4073 4074
                switch (b) {
                case 0x14: /* pextrb */
                    tcg_gen_ld8u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
                                            xmm_regs[reg].XMM_B(val & 15)));
4075
                    if (mod == 3) {
4076
                        gen_op_mov_reg_v(ot, rm, cpu_T[0]);
4077 4078 4079 4080
                    } else {
                        tcg_gen_qemu_st_tl(cpu_T[0], cpu_A0,
                                           s->mem_index, MO_UB);
                    }
B
balrog 已提交
4081 4082 4083 4084
                    break;
                case 0x15: /* pextrw */
                    tcg_gen_ld16u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
                                            xmm_regs[reg].XMM_W(val & 7)));
4085
                    if (mod == 3) {
4086
                        gen_op_mov_reg_v(ot, rm, cpu_T[0]);
4087 4088 4089 4090
                    } else {
                        tcg_gen_qemu_st_tl(cpu_T[0], cpu_A0,
                                           s->mem_index, MO_LEUW);
                    }
B
balrog 已提交
4091 4092
                    break;
                case 0x16:
4093
                    if (ot == MO_32) { /* pextrd */
B
balrog 已提交
4094 4095 4096
                        tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env,
                                        offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_L(val & 3)));
4097
                        if (mod == 3) {
4098
                            tcg_gen_extu_i32_tl(cpu_regs[rm], cpu_tmp2_i32);
4099
                        } else {
4100 4101
                            tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                                s->mem_index, MO_LEUL);
4102
                        }
B
balrog 已提交
4103
                    } else { /* pextrq */
P
pbrook 已提交
4104
#ifdef TARGET_X86_64
B
balrog 已提交
4105 4106 4107
                        tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env,
                                        offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_Q(val & 1)));
4108
                        if (mod == 3) {
4109
                            tcg_gen_mov_i64(cpu_regs[rm], cpu_tmp1_i64);
4110 4111 4112 4113
                        } else {
                            tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0,
                                                s->mem_index, MO_LEQ);
                        }
P
pbrook 已提交
4114 4115 4116
#else
                        goto illegal_op;
#endif
B
balrog 已提交
4117 4118 4119 4120 4121
                    }
                    break;
                case 0x17: /* extractps */
                    tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
                                            xmm_regs[reg].XMM_L(val & 3)));
4122
                    if (mod == 3) {
4123
                        gen_op_mov_reg_v(ot, rm, cpu_T[0]);
4124 4125 4126 4127
                    } else {
                        tcg_gen_qemu_st_tl(cpu_T[0], cpu_A0,
                                           s->mem_index, MO_LEUL);
                    }
B
balrog 已提交
4128 4129
                    break;
                case 0x20: /* pinsrb */
4130
                    if (mod == 3) {
4131
                        gen_op_mov_v_reg(MO_32, cpu_T[0], rm);
4132 4133 4134 4135
                    } else {
                        tcg_gen_qemu_ld_tl(cpu_T[0], cpu_A0,
                                           s->mem_index, MO_UB);
                    }
4136
                    tcg_gen_st8_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
B
balrog 已提交
4137 4138 4139
                                            xmm_regs[reg].XMM_B(val & 15)));
                    break;
                case 0x21: /* insertps */
P
pbrook 已提交
4140
                    if (mod == 3) {
B
balrog 已提交
4141 4142 4143
                        tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env,
                                        offsetof(CPUX86State,xmm_regs[rm]
                                                .XMM_L((val >> 6) & 3)));
P
pbrook 已提交
4144
                    } else {
4145 4146
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
P
pbrook 已提交
4147
                    }
B
balrog 已提交
4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168
                    tcg_gen_st_i32(cpu_tmp2_i32, cpu_env,
                                    offsetof(CPUX86State,xmm_regs[reg]
                                            .XMM_L((val >> 4) & 3)));
                    if ((val >> 0) & 1)
                        tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
                                        cpu_env, offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_L(0)));
                    if ((val >> 1) & 1)
                        tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
                                        cpu_env, offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_L(1)));
                    if ((val >> 2) & 1)
                        tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
                                        cpu_env, offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_L(2)));
                    if ((val >> 3) & 1)
                        tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
                                        cpu_env, offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_L(3)));
                    break;
                case 0x22:
4169
                    if (ot == MO_32) { /* pinsrd */
4170
                        if (mod == 3) {
4171
                            tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[rm]);
4172
                        } else {
4173 4174
                            tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                                s->mem_index, MO_LEUL);
4175
                        }
B
balrog 已提交
4176 4177 4178 4179
                        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env,
                                        offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_L(val & 3)));
                    } else { /* pinsrq */
P
pbrook 已提交
4180
#ifdef TARGET_X86_64
4181
                        if (mod == 3) {
B
balrog 已提交
4182
                            gen_op_mov_v_reg(ot, cpu_tmp1_i64, rm);
4183 4184 4185 4186
                        } else {
                            tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0,
                                                s->mem_index, MO_LEQ);
                        }
B
balrog 已提交
4187 4188 4189
                        tcg_gen_st_i64(cpu_tmp1_i64, cpu_env,
                                        offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_Q(val & 1)));
P
pbrook 已提交
4190 4191 4192
#else
                        goto illegal_op;
#endif
B
balrog 已提交
4193 4194 4195 4196 4197
                    }
                    break;
                }
                return;
            }
B
balrog 已提交
4198 4199 4200 4201 4202 4203 4204

            if (b1) {
                op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
                if (mod == 3) {
                    op2_offset = offsetof(CPUX86State,xmm_regs[rm | REX_B(s)]);
                } else {
                    op2_offset = offsetof(CPUX86State,xmm_t0);
4205
                    gen_lea_modrm(env, s, modrm);
4206
                    gen_ldo_env_A0(s, op2_offset);
B
balrog 已提交
4207 4208 4209 4210 4211 4212 4213
                }
            } else {
                op1_offset = offsetof(CPUX86State,fpregs[reg].mmx);
                if (mod == 3) {
                    op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
                } else {
                    op2_offset = offsetof(CPUX86State,mmx_t0);
4214
                    gen_lea_modrm(env, s, modrm);
4215
                    gen_ldq_env_A0(s, op2_offset);
B
balrog 已提交
4216 4217
                }
            }
4218
            val = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
4219

B
balrog 已提交
4220
            if ((b & 0xfc) == 0x60) { /* pcmpXstrX */
4221
                set_cc_op(s, CC_OP_EFLAGS);
B
balrog 已提交
4222

4223
                if (s->dflag == MO_64) {
B
balrog 已提交
4224 4225
                    /* The helper must use entire 64-bit gp registers */
                    val |= 1 << 8;
4226
                }
B
balrog 已提交
4227 4228
            }

B
balrog 已提交
4229 4230
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4231
            sse_fn_eppi(cpu_env, cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
B
balrog 已提交
4232
            break;
R
Richard Henderson 已提交
4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246

        case 0x33a:
            /* Various integer extensions at 0f 3a f[0-f].  */
            b = modrm | (b1 << 8);
            modrm = cpu_ldub_code(env, s->pc++);
            reg = ((modrm >> 3) & 7) | rex_r;

            switch (b) {
            case 0x3f0: /* rorx Gy,Ey, Ib */
                if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI2)
                    || !(s->prefix & PREFIX_VEX)
                    || s->vex_l != 0) {
                    goto illegal_op;
                }
4247
                ot = mo_64_32(s->dflag);
R
Richard Henderson 已提交
4248 4249
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                b = cpu_ldub_code(env, s->pc++);
4250
                if (ot == MO_64) {
R
Richard Henderson 已提交
4251 4252 4253 4254 4255 4256
                    tcg_gen_rotri_tl(cpu_T[0], cpu_T[0], b & 63);
                } else {
                    tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
                    tcg_gen_rotri_i32(cpu_tmp2_i32, cpu_tmp2_i32, b & 31);
                    tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
                }
4257
                gen_op_mov_reg_v(ot, reg, cpu_T[0]);
R
Richard Henderson 已提交
4258 4259 4260 4261 4262 4263 4264
                break;

            default:
                goto illegal_op;
            }
            break;

B
bellard 已提交
4265 4266 4267 4268 4269
        default:
            goto illegal_op;
        }
    } else {
        /* generic MMX or SSE operation */
B
bellard 已提交
4270 4271 4272 4273 4274 4275 4276 4277
        switch(b) {
        case 0x70: /* pshufx insn */
        case 0xc6: /* pshufx insn */
        case 0xc2: /* compare insns */
            s->rip_offset = 1;
            break;
        default:
            break;
B
bellard 已提交
4278 4279 4280 4281
        }
        if (is_xmm) {
            op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
            if (mod != 3) {
4282 4283
                int sz = 4;

4284
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
4285
                op2_offset = offsetof(CPUX86State,xmm_t0);
4286 4287 4288 4289 4290 4291

                switch (b) {
                case 0x50 ... 0x5a:
                case 0x5c ... 0x5f:
                case 0xc2:
                    /* Most sse scalar operations.  */
B
bellard 已提交
4292
                    if (b1 == 2) {
4293 4294 4295 4296 4297 4298 4299 4300 4301 4302
                        sz = 2;
                    } else if (b1 == 3) {
                        sz = 3;
                    }
                    break;

                case 0x2e:  /* ucomis[sd] */
                case 0x2f:  /* comis[sd] */
                    if (b1 == 0) {
                        sz = 2;
B
bellard 已提交
4303
                    } else {
4304
                        sz = 3;
B
bellard 已提交
4305
                    }
4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321
                    break;
                }

                switch (sz) {
                case 2:
                    /* 32 bit access */
                    gen_op_ld_v(s, MO_32, cpu_T[0], cpu_A0);
                    tcg_gen_st32_tl(cpu_T[0], cpu_env,
                                    offsetof(CPUX86State,xmm_t0.XMM_L(0)));
                    break;
                case 3:
                    /* 64 bit access */
                    gen_ldq_env_A0(s, offsetof(CPUX86State, xmm_t0.XMM_D(0)));
                    break;
                default:
                    /* 128 bit access */
4322
                    gen_ldo_env_A0(s, op2_offset);
4323
                    break;
B
bellard 已提交
4324 4325 4326 4327 4328 4329 4330 4331
                }
            } else {
                rm = (modrm & 7) | REX_B(s);
                op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
            }
        } else {
            op1_offset = offsetof(CPUX86State,fpregs[reg].mmx);
            if (mod != 3) {
4332
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
4333
                op2_offset = offsetof(CPUX86State,mmx_t0);
4334
                gen_ldq_env_A0(s, op2_offset);
B
bellard 已提交
4335 4336 4337 4338 4339 4340
            } else {
                rm = (modrm & 7);
                op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
            }
        }
        switch(b) {
A
aurel32 已提交
4341
        case 0x0f: /* 3DNow! data insns */
4342 4343
            if (!(s->cpuid_ext2_features & CPUID_EXT2_3DNOW))
                goto illegal_op;
4344
            val = cpu_ldub_code(env, s->pc++);
B
Blue Swirl 已提交
4345 4346
            sse_fn_epp = sse_op_table5[val];
            if (!sse_fn_epp) {
A
aurel32 已提交
4347
                goto illegal_op;
B
Blue Swirl 已提交
4348
            }
B
bellard 已提交
4349 4350
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4351
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
A
aurel32 已提交
4352
            break;
B
bellard 已提交
4353 4354
        case 0x70: /* pshufx insn */
        case 0xc6: /* pshufx insn */
4355
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4356 4357
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4358
            /* XXX: introduce a new table? */
B
Blue Swirl 已提交
4359
            sse_fn_ppi = (SSEFunc_0_ppi)sse_fn_epp;
B
Blue Swirl 已提交
4360
            sse_fn_ppi(cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
B
bellard 已提交
4361 4362 4363
            break;
        case 0xc2:
            /* compare insns */
4364
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4365 4366
            if (val >= 8)
                goto illegal_op;
B
Blue Swirl 已提交
4367
            sse_fn_epp = sse_op_table4[val][b1];
B
Blue Swirl 已提交
4368

B
bellard 已提交
4369 4370
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4371
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
4372
            break;
4373 4374 4375 4376
        case 0xf7:
            /* maskmov : we must prepare A0 */
            if (mod != 3)
                goto illegal_op;
4377 4378
            tcg_gen_mov_tl(cpu_A0, cpu_regs[R_EDI]);
            gen_extu(s->aflag, cpu_A0);
4379 4380 4381 4382
            gen_add_A0_ds_seg(s);

            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4383
            /* XXX: introduce a new table? */
B
Blue Swirl 已提交
4384 4385
            sse_fn_eppt = (SSEFunc_0_eppt)sse_fn_epp;
            sse_fn_eppt(cpu_env, cpu_ptr0, cpu_ptr1, cpu_A0);
4386
            break;
B
bellard 已提交
4387
        default:
B
bellard 已提交
4388 4389
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4390
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
4391 4392 4393
            break;
        }
        if (b == 0x2e || b == 0x2f) {
4394
            set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
4395 4396 4397 4398
        }
    }
}

B
bellard 已提交
4399 4400
/* convert one instruction. s->is_jmp is set if the translation must
   be stopped. Return the next pc value */
4401 4402
static target_ulong disas_insn(CPUX86State *env, DisasContext *s,
                               target_ulong pc_start)
B
bellard 已提交
4403
{
4404
    int b, prefixes;
4405
    int shift;
4406
    TCGMemOp ot, aflag, dflag;
4407
    int modrm, reg, rm, mod, op, opreg, val;
B
bellard 已提交
4408 4409
    target_ulong next_eip, tval;
    int rex_w, rex_r;
B
bellard 已提交
4410

4411
    if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
4412
        tcg_gen_debug_insn_start(pc_start);
4413
    }
B
bellard 已提交
4414 4415 4416
    s->pc = pc_start;
    prefixes = 0;
    s->override = -1;
B
bellard 已提交
4417 4418 4419 4420 4421
    rex_w = -1;
    rex_r = 0;
#ifdef TARGET_X86_64
    s->rex_x = 0;
    s->rex_b = 0;
4422
    x86_64_hregs = 0;
B
bellard 已提交
4423 4424
#endif
    s->rip_offset = 0; /* for relative ip address */
4425 4426
    s->vex_l = 0;
    s->vex_v = 0;
B
bellard 已提交
4427
 next_byte:
4428
    b = cpu_ldub_code(env, s->pc);
B
bellard 已提交
4429
    s->pc++;
4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464
    /* Collect prefixes.  */
    switch (b) {
    case 0xf3:
        prefixes |= PREFIX_REPZ;
        goto next_byte;
    case 0xf2:
        prefixes |= PREFIX_REPNZ;
        goto next_byte;
    case 0xf0:
        prefixes |= PREFIX_LOCK;
        goto next_byte;
    case 0x2e:
        s->override = R_CS;
        goto next_byte;
    case 0x36:
        s->override = R_SS;
        goto next_byte;
    case 0x3e:
        s->override = R_DS;
        goto next_byte;
    case 0x26:
        s->override = R_ES;
        goto next_byte;
    case 0x64:
        s->override = R_FS;
        goto next_byte;
    case 0x65:
        s->override = R_GS;
        goto next_byte;
    case 0x66:
        prefixes |= PREFIX_DATA;
        goto next_byte;
    case 0x67:
        prefixes |= PREFIX_ADR;
        goto next_byte;
B
bellard 已提交
4465
#ifdef TARGET_X86_64
4466 4467
    case 0x40 ... 0x4f:
        if (CODE64(s)) {
B
bellard 已提交
4468 4469 4470 4471 4472 4473 4474 4475
            /* REX prefix */
            rex_w = (b >> 3) & 1;
            rex_r = (b & 0x4) << 1;
            s->rex_x = (b & 0x2) << 2;
            REX_B(s) = (b & 0x1) << 3;
            x86_64_hregs = 1; /* select uniform byte register addressing */
            goto next_byte;
        }
4476 4477
        break;
#endif
4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494
    case 0xc5: /* 2-byte VEX */
    case 0xc4: /* 3-byte VEX */
        /* VEX prefixes cannot be used except in 32-bit mode.
           Otherwise the instruction is LES or LDS.  */
        if (s->code32 && !s->vm86) {
            static const int pp_prefix[4] = {
                0, PREFIX_DATA, PREFIX_REPZ, PREFIX_REPNZ
            };
            int vex3, vex2 = cpu_ldub_code(env, s->pc);

            if (!CODE64(s) && (vex2 & 0xc0) != 0xc0) {
                /* 4.1.4.6: In 32-bit mode, bits [7:6] must be 11b,
                   otherwise the instruction is LES or LDS.  */
                break;
            }
            s->pc++;

P
Peter Maydell 已提交
4495
            /* 4.1.1-4.1.3: No preceding lock, 66, f2, f3, or rex prefixes. */
4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534
            if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ
                            | PREFIX_LOCK | PREFIX_DATA)) {
                goto illegal_op;
            }
#ifdef TARGET_X86_64
            if (x86_64_hregs) {
                goto illegal_op;
            }
#endif
            rex_r = (~vex2 >> 4) & 8;
            if (b == 0xc5) {
                vex3 = vex2;
                b = cpu_ldub_code(env, s->pc++);
            } else {
#ifdef TARGET_X86_64
                s->rex_x = (~vex2 >> 3) & 8;
                s->rex_b = (~vex2 >> 2) & 8;
#endif
                vex3 = cpu_ldub_code(env, s->pc++);
                rex_w = (vex3 >> 7) & 1;
                switch (vex2 & 0x1f) {
                case 0x01: /* Implied 0f leading opcode bytes.  */
                    b = cpu_ldub_code(env, s->pc++) | 0x100;
                    break;
                case 0x02: /* Implied 0f 38 leading opcode bytes.  */
                    b = 0x138;
                    break;
                case 0x03: /* Implied 0f 3a leading opcode bytes.  */
                    b = 0x13a;
                    break;
                default:   /* Reserved for future use.  */
                    goto illegal_op;
                }
            }
            s->vex_v = (~vex3 >> 3) & 0xf;
            s->vex_l = (vex3 >> 2) & 1;
            prefixes |= pp_prefix[vex3 & 3] | PREFIX_VEX;
        }
        break;
4535 4536 4537 4538
    }

    /* Post-process prefixes.  */
    if (CODE64(s)) {
4539 4540 4541
        /* In 64-bit mode, the default data size is 32-bit.  Select 64-bit
           data with rex_w, and 16-bit data with 0x66; rex_w takes precedence
           over 0x66 if both are present.  */
4542
        dflag = (rex_w > 0 ? MO_64 : prefixes & PREFIX_DATA ? MO_16 : MO_32);
4543
        /* In 64-bit mode, 0x67 selects 32-bit addressing.  */
4544
        aflag = (prefixes & PREFIX_ADR ? MO_32 : MO_64);
4545 4546
    } else {
        /* In 16/32-bit mode, 0x66 selects the opposite data size.  */
4547 4548 4549 4550
        if (s->code32 ^ ((prefixes & PREFIX_DATA) != 0)) {
            dflag = MO_32;
        } else {
            dflag = MO_16;
B
bellard 已提交
4551
        }
4552
        /* In 16/32-bit mode, 0x67 selects the opposite addressing.  */
4553 4554 4555 4556
        if (s->code32 ^ ((prefixes & PREFIX_ADR) != 0)) {
            aflag = MO_32;
        }  else {
            aflag = MO_16;
B
bellard 已提交
4557
        }
B
bellard 已提交
4558 4559 4560 4561 4562 4563 4564 4565
    }

    s->prefix = prefixes;
    s->aflag = aflag;
    s->dflag = dflag;

    /* lock generation */
    if (prefixes & PREFIX_LOCK)
P
pbrook 已提交
4566
        gen_helper_lock();
B
bellard 已提交
4567 4568 4569 4570 4571 4572 4573

    /* now check op code */
 reswitch:
    switch(b) {
    case 0x0f:
        /**************************/
        /* extended op code */
4574
        b = cpu_ldub_code(env, s->pc++) | 0x100;
B
bellard 已提交
4575
        goto reswitch;
4576

B
bellard 已提交
4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591
        /**************************/
        /* arith & logic */
    case 0x00 ... 0x05:
    case 0x08 ... 0x0d:
    case 0x10 ... 0x15:
    case 0x18 ... 0x1d:
    case 0x20 ... 0x25:
    case 0x28 ... 0x2d:
    case 0x30 ... 0x35:
    case 0x38 ... 0x3d:
        {
            int op, f, val;
            op = (b >> 3) & 7;
            f = (b >> 1) & 3;

4592
            ot = mo_b_d(b, dflag);
4593

B
bellard 已提交
4594 4595
            switch(f) {
            case 0: /* OP Ev, Gv */
4596
                modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4597
                reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
4598
                mod = (modrm >> 6) & 3;
B
bellard 已提交
4599
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4600
                if (mod != 3) {
4601
                    gen_lea_modrm(env, s, modrm);
B
bellard 已提交
4602 4603 4604 4605
                    opreg = OR_TMP0;
                } else if (op == OP_XORL && rm == reg) {
                xor_zero:
                    /* xor reg, reg optimisation */
R
Richard Henderson 已提交
4606
                    set_cc_op(s, CC_OP_CLR);
4607
                    tcg_gen_movi_tl(cpu_T[0], 0);
4608
                    gen_op_mov_reg_v(ot, reg, cpu_T[0]);
B
bellard 已提交
4609 4610 4611 4612
                    break;
                } else {
                    opreg = rm;
                }
4613
                gen_op_mov_v_reg(ot, cpu_T[1], reg);
B
bellard 已提交
4614 4615 4616
                gen_op(s, op, ot, opreg);
                break;
            case 1: /* OP Gv, Ev */
4617
                modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4618
                mod = (modrm >> 6) & 3;
B
bellard 已提交
4619 4620
                reg = ((modrm >> 3) & 7) | rex_r;
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4621
                if (mod != 3) {
4622
                    gen_lea_modrm(env, s, modrm);
4623
                    gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
B
bellard 已提交
4624 4625 4626
                } else if (op == OP_XORL && rm == reg) {
                    goto xor_zero;
                } else {
4627
                    gen_op_mov_v_reg(ot, cpu_T[1], rm);
B
bellard 已提交
4628 4629 4630 4631
                }
                gen_op(s, op, ot, reg);
                break;
            case 2: /* OP A, Iv */
4632
                val = insn_get(env, s, ot);
4633
                tcg_gen_movi_tl(cpu_T[1], val);
B
bellard 已提交
4634 4635 4636 4637 4638 4639
                gen_op(s, op, ot, OR_EAX);
                break;
            }
        }
        break;

4640 4641 4642
    case 0x82:
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
4643 4644 4645 4646 4647 4648
    case 0x80: /* GRP1 */
    case 0x81:
    case 0x83:
        {
            int val;

4649
            ot = mo_b_d(b, dflag);
4650

4651
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4652
            mod = (modrm >> 6) & 3;
B
bellard 已提交
4653
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4654
            op = (modrm >> 3) & 7;
4655

B
bellard 已提交
4656
            if (mod != 3) {
B
bellard 已提交
4657 4658 4659 4660
                if (b == 0x83)
                    s->rip_offset = 1;
                else
                    s->rip_offset = insn_const_size(ot);
4661
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
4662 4663
                opreg = OR_TMP0;
            } else {
B
bellard 已提交
4664
                opreg = rm;
B
bellard 已提交
4665 4666 4667 4668 4669 4670
            }

            switch(b) {
            default:
            case 0x80:
            case 0x81:
4671
            case 0x82:
4672
                val = insn_get(env, s, ot);
B
bellard 已提交
4673 4674
                break;
            case 0x83:
4675
                val = (int8_t)insn_get(env, s, MO_8);
B
bellard 已提交
4676 4677
                break;
            }
4678
            tcg_gen_movi_tl(cpu_T[1], val);
B
bellard 已提交
4679 4680 4681 4682 4683 4684 4685
            gen_op(s, op, ot, opreg);
        }
        break;

        /**************************/
        /* inc, dec, and other misc arith */
    case 0x40 ... 0x47: /* inc Gv */
4686
        ot = dflag;
B
bellard 已提交
4687 4688 4689
        gen_inc(s, ot, OR_EAX + (b & 7), 1);
        break;
    case 0x48 ... 0x4f: /* dec Gv */
4690
        ot = dflag;
B
bellard 已提交
4691 4692 4693 4694
        gen_inc(s, ot, OR_EAX + (b & 7), -1);
        break;
    case 0xf6: /* GRP3 */
    case 0xf7:
4695
        ot = mo_b_d(b, dflag);
B
bellard 已提交
4696

4697
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4698
        mod = (modrm >> 6) & 3;
B
bellard 已提交
4699
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4700 4701
        op = (modrm >> 3) & 7;
        if (mod != 3) {
B
bellard 已提交
4702 4703
            if (op == 0)
                s->rip_offset = insn_const_size(ot);
4704
            gen_lea_modrm(env, s, modrm);
4705
            gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
4706
        } else {
4707
            gen_op_mov_v_reg(ot, cpu_T[0], rm);
B
bellard 已提交
4708 4709 4710 4711
        }

        switch(op) {
        case 0: /* test */
4712
            val = insn_get(env, s, ot);
4713
            tcg_gen_movi_tl(cpu_T[1], val);
B
bellard 已提交
4714
            gen_op_testl_T0_T1_cc();
4715
            set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
4716 4717
            break;
        case 2: /* not */
4718
            tcg_gen_not_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
4719
            if (mod != 3) {
4720
                gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
4721
            } else {
4722
                gen_op_mov_reg_v(ot, rm, cpu_T[0]);
B
bellard 已提交
4723 4724 4725
            }
            break;
        case 3: /* neg */
4726
            tcg_gen_neg_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
4727
            if (mod != 3) {
4728
                gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
4729
            } else {
4730
                gen_op_mov_reg_v(ot, rm, cpu_T[0]);
B
bellard 已提交
4731 4732
            }
            gen_op_update_neg_cc();
4733
            set_cc_op(s, CC_OP_SUBB + ot);
B
bellard 已提交
4734 4735 4736
            break;
        case 4: /* mul */
            switch(ot) {
4737
            case MO_8:
4738
                gen_op_mov_v_reg(MO_8, cpu_T[1], R_EAX);
B
bellard 已提交
4739 4740 4741 4742
                tcg_gen_ext8u_tl(cpu_T[0], cpu_T[0]);
                tcg_gen_ext8u_tl(cpu_T[1], cpu_T[1]);
                /* XXX: use 32 bit mul which could be faster */
                tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
4743
                gen_op_mov_reg_v(MO_16, R_EAX, cpu_T[0]);
B
bellard 已提交
4744 4745
                tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                tcg_gen_andi_tl(cpu_cc_src, cpu_T[0], 0xff00);
4746
                set_cc_op(s, CC_OP_MULB);
B
bellard 已提交
4747
                break;
4748
            case MO_16:
4749
                gen_op_mov_v_reg(MO_16, cpu_T[1], R_EAX);
B
bellard 已提交
4750 4751 4752 4753
                tcg_gen_ext16u_tl(cpu_T[0], cpu_T[0]);
                tcg_gen_ext16u_tl(cpu_T[1], cpu_T[1]);
                /* XXX: use 32 bit mul which could be faster */
                tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
4754
                gen_op_mov_reg_v(MO_16, R_EAX, cpu_T[0]);
B
bellard 已提交
4755 4756
                tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                tcg_gen_shri_tl(cpu_T[0], cpu_T[0], 16);
4757
                gen_op_mov_reg_v(MO_16, R_EDX, cpu_T[0]);
B
bellard 已提交
4758
                tcg_gen_mov_tl(cpu_cc_src, cpu_T[0]);
4759
                set_cc_op(s, CC_OP_MULW);
B
bellard 已提交
4760 4761
                break;
            default:
4762
            case MO_32:
4763 4764 4765 4766 4767 4768 4769 4770
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
                tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_regs[R_EAX]);
                tcg_gen_mulu2_i32(cpu_tmp2_i32, cpu_tmp3_i32,
                                  cpu_tmp2_i32, cpu_tmp3_i32);
                tcg_gen_extu_i32_tl(cpu_regs[R_EAX], cpu_tmp2_i32);
                tcg_gen_extu_i32_tl(cpu_regs[R_EDX], cpu_tmp3_i32);
                tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[R_EAX]);
                tcg_gen_mov_tl(cpu_cc_src, cpu_regs[R_EDX]);
4771
                set_cc_op(s, CC_OP_MULL);
B
bellard 已提交
4772
                break;
B
bellard 已提交
4773
#ifdef TARGET_X86_64
4774
            case MO_64:
4775 4776 4777 4778
                tcg_gen_mulu2_i64(cpu_regs[R_EAX], cpu_regs[R_EDX],
                                  cpu_T[0], cpu_regs[R_EAX]);
                tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[R_EAX]);
                tcg_gen_mov_tl(cpu_cc_src, cpu_regs[R_EDX]);
4779
                set_cc_op(s, CC_OP_MULQ);
B
bellard 已提交
4780 4781
                break;
#endif
B
bellard 已提交
4782 4783 4784 4785
            }
            break;
        case 5: /* imul */
            switch(ot) {
4786
            case MO_8:
4787
                gen_op_mov_v_reg(MO_8, cpu_T[1], R_EAX);
B
bellard 已提交
4788 4789 4790 4791
                tcg_gen_ext8s_tl(cpu_T[0], cpu_T[0]);
                tcg_gen_ext8s_tl(cpu_T[1], cpu_T[1]);
                /* XXX: use 32 bit mul which could be faster */
                tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
4792
                gen_op_mov_reg_v(MO_16, R_EAX, cpu_T[0]);
B
bellard 已提交
4793 4794 4795
                tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                tcg_gen_ext8s_tl(cpu_tmp0, cpu_T[0]);
                tcg_gen_sub_tl(cpu_cc_src, cpu_T[0], cpu_tmp0);
4796
                set_cc_op(s, CC_OP_MULB);
B
bellard 已提交
4797
                break;
4798
            case MO_16:
4799
                gen_op_mov_v_reg(MO_16, cpu_T[1], R_EAX);
B
bellard 已提交
4800 4801 4802 4803
                tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
                tcg_gen_ext16s_tl(cpu_T[1], cpu_T[1]);
                /* XXX: use 32 bit mul which could be faster */
                tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
4804
                gen_op_mov_reg_v(MO_16, R_EAX, cpu_T[0]);
B
bellard 已提交
4805 4806 4807 4808
                tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                tcg_gen_ext16s_tl(cpu_tmp0, cpu_T[0]);
                tcg_gen_sub_tl(cpu_cc_src, cpu_T[0], cpu_tmp0);
                tcg_gen_shri_tl(cpu_T[0], cpu_T[0], 16);
4809
                gen_op_mov_reg_v(MO_16, R_EDX, cpu_T[0]);
4810
                set_cc_op(s, CC_OP_MULW);
B
bellard 已提交
4811 4812
                break;
            default:
4813
            case MO_32:
4814 4815 4816 4817 4818 4819 4820 4821 4822 4823
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
                tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_regs[R_EAX]);
                tcg_gen_muls2_i32(cpu_tmp2_i32, cpu_tmp3_i32,
                                  cpu_tmp2_i32, cpu_tmp3_i32);
                tcg_gen_extu_i32_tl(cpu_regs[R_EAX], cpu_tmp2_i32);
                tcg_gen_extu_i32_tl(cpu_regs[R_EDX], cpu_tmp3_i32);
                tcg_gen_sari_i32(cpu_tmp2_i32, cpu_tmp2_i32, 31);
                tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[R_EAX]);
                tcg_gen_sub_i32(cpu_tmp2_i32, cpu_tmp2_i32, cpu_tmp3_i32);
                tcg_gen_extu_i32_tl(cpu_cc_src, cpu_tmp2_i32);
4824
                set_cc_op(s, CC_OP_MULL);
B
bellard 已提交
4825
                break;
B
bellard 已提交
4826
#ifdef TARGET_X86_64
4827
            case MO_64:
4828 4829 4830 4831 4832
                tcg_gen_muls2_i64(cpu_regs[R_EAX], cpu_regs[R_EDX],
                                  cpu_T[0], cpu_regs[R_EAX]);
                tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[R_EAX]);
                tcg_gen_sari_tl(cpu_cc_src, cpu_regs[R_EAX], 63);
                tcg_gen_sub_tl(cpu_cc_src, cpu_cc_src, cpu_regs[R_EDX]);
4833
                set_cc_op(s, CC_OP_MULQ);
B
bellard 已提交
4834 4835
                break;
#endif
B
bellard 已提交
4836 4837 4838 4839
            }
            break;
        case 6: /* div */
            switch(ot) {
4840
            case MO_8:
B
bellard 已提交
4841
                gen_jmp_im(pc_start - s->cs_base);
4842
                gen_helper_divb_AL(cpu_env, cpu_T[0]);
B
bellard 已提交
4843
                break;
4844
            case MO_16:
B
bellard 已提交
4845
                gen_jmp_im(pc_start - s->cs_base);
4846
                gen_helper_divw_AX(cpu_env, cpu_T[0]);
B
bellard 已提交
4847 4848
                break;
            default:
4849
            case MO_32:
B
bellard 已提交
4850
                gen_jmp_im(pc_start - s->cs_base);
4851
                gen_helper_divl_EAX(cpu_env, cpu_T[0]);
B
bellard 已提交
4852 4853
                break;
#ifdef TARGET_X86_64
4854
            case MO_64:
B
bellard 已提交
4855
                gen_jmp_im(pc_start - s->cs_base);
4856
                gen_helper_divq_EAX(cpu_env, cpu_T[0]);
B
bellard 已提交
4857
                break;
B
bellard 已提交
4858
#endif
B
bellard 已提交
4859 4860 4861 4862
            }
            break;
        case 7: /* idiv */
            switch(ot) {
4863
            case MO_8:
B
bellard 已提交
4864
                gen_jmp_im(pc_start - s->cs_base);
4865
                gen_helper_idivb_AL(cpu_env, cpu_T[0]);
B
bellard 已提交
4866
                break;
4867
            case MO_16:
B
bellard 已提交
4868
                gen_jmp_im(pc_start - s->cs_base);
4869
                gen_helper_idivw_AX(cpu_env, cpu_T[0]);
B
bellard 已提交
4870 4871
                break;
            default:
4872
            case MO_32:
B
bellard 已提交
4873
                gen_jmp_im(pc_start - s->cs_base);
4874
                gen_helper_idivl_EAX(cpu_env, cpu_T[0]);
B
bellard 已提交
4875 4876
                break;
#ifdef TARGET_X86_64
4877
            case MO_64:
B
bellard 已提交
4878
                gen_jmp_im(pc_start - s->cs_base);
4879
                gen_helper_idivq_EAX(cpu_env, cpu_T[0]);
B
bellard 已提交
4880
                break;
B
bellard 已提交
4881
#endif
B
bellard 已提交
4882 4883 4884 4885 4886 4887 4888 4889 4890
            }
            break;
        default:
            goto illegal_op;
        }
        break;

    case 0xfe: /* GRP4 */
    case 0xff: /* GRP5 */
4891
        ot = mo_b_d(b, dflag);
B
bellard 已提交
4892

4893
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4894
        mod = (modrm >> 6) & 3;
B
bellard 已提交
4895
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4896 4897 4898 4899
        op = (modrm >> 3) & 7;
        if (op >= 2 && b == 0xfe) {
            goto illegal_op;
        }
B
bellard 已提交
4900
        if (CODE64(s)) {
4901
            if (op == 2 || op == 4) {
B
bellard 已提交
4902
                /* operand size for jumps is 64 bit */
4903
                ot = MO_64;
4904
            } else if (op == 3 || op == 5) {
4905
                ot = dflag != MO_16 ? MO_32 + (rex_w == 1) : MO_16;
B
bellard 已提交
4906 4907
            } else if (op == 6) {
                /* default push size is 64 bit */
4908
                ot = mo_pushpop(s, dflag);
B
bellard 已提交
4909 4910
            }
        }
B
bellard 已提交
4911
        if (mod != 3) {
4912
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
4913
            if (op >= 2 && op != 3 && op != 5)
4914
                gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
4915
        } else {
4916
            gen_op_mov_v_reg(ot, cpu_T[0], rm);
B
bellard 已提交
4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934
        }

        switch(op) {
        case 0: /* inc Ev */
            if (mod != 3)
                opreg = OR_TMP0;
            else
                opreg = rm;
            gen_inc(s, ot, opreg, 1);
            break;
        case 1: /* dec Ev */
            if (mod != 3)
                opreg = OR_TMP0;
            else
                opreg = rm;
            gen_inc(s, ot, opreg, -1);
            break;
        case 2: /* call Ev */
4935
            /* XXX: optimize if memory (no 'and' is necessary) */
4936
            if (dflag == MO_16) {
4937 4938
                tcg_gen_ext16u_tl(cpu_T[0], cpu_T[0]);
            }
B
bellard 已提交
4939
            next_eip = s->pc - s->cs_base;
4940
            tcg_gen_movi_tl(cpu_T[1], next_eip);
4941
            gen_push_v(s, cpu_T[1]);
4942
            gen_op_jmp_v(cpu_T[0]);
B
bellard 已提交
4943 4944
            gen_eob(s);
            break;
B
bellard 已提交
4945
        case 3: /* lcall Ev */
4946
            gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
4947
            gen_add_A0_im(s, 1 << ot);
4948
            gen_op_ld_v(s, MO_16, cpu_T[0], cpu_A0);
B
bellard 已提交
4949 4950
        do_lcall:
            if (s->pe && !s->vm86) {
4951
                gen_update_cc_op(s);
B
bellard 已提交
4952
                gen_jmp_im(pc_start - s->cs_base);
4953
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
4954
                gen_helper_lcall_protected(cpu_env, cpu_tmp2_i32, cpu_T[1],
4955
                                           tcg_const_i32(dflag - 1),
P
pbrook 已提交
4956
                                           tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
4957
            } else {
4958
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
4959
                gen_helper_lcall_real(cpu_env, cpu_tmp2_i32, cpu_T[1],
4960
                                      tcg_const_i32(dflag - 1),
P
pbrook 已提交
4961
                                      tcg_const_i32(s->pc - s->cs_base));
B
bellard 已提交
4962 4963 4964 4965
            }
            gen_eob(s);
            break;
        case 4: /* jmp Ev */
4966
            if (dflag == MO_16) {
4967 4968
                tcg_gen_ext16u_tl(cpu_T[0], cpu_T[0]);
            }
4969
            gen_op_jmp_v(cpu_T[0]);
B
bellard 已提交
4970 4971 4972
            gen_eob(s);
            break;
        case 5: /* ljmp Ev */
4973
            gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
4974
            gen_add_A0_im(s, 1 << ot);
4975
            gen_op_ld_v(s, MO_16, cpu_T[0], cpu_A0);
B
bellard 已提交
4976 4977
        do_ljmp:
            if (s->pe && !s->vm86) {
4978
                gen_update_cc_op(s);
B
bellard 已提交
4979
                gen_jmp_im(pc_start - s->cs_base);
4980
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
4981
                gen_helper_ljmp_protected(cpu_env, cpu_tmp2_i32, cpu_T[1],
P
pbrook 已提交
4982
                                          tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
4983
            } else {
4984
                gen_op_movl_seg_T0_vm(R_CS);
R
Richard Henderson 已提交
4985
                gen_op_jmp_v(cpu_T[1]);
B
bellard 已提交
4986 4987 4988 4989
            }
            gen_eob(s);
            break;
        case 6: /* push Ev */
4990
            gen_push_v(s, cpu_T[0]);
B
bellard 已提交
4991 4992 4993 4994 4995 4996 4997
            break;
        default:
            goto illegal_op;
        }
        break;

    case 0x84: /* test Ev, Gv */
4998
    case 0x85:
4999
        ot = mo_b_d(b, dflag);
B
bellard 已提交
5000

5001
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5002
        reg = ((modrm >> 3) & 7) | rex_r;
5003

5004
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
5005
        gen_op_mov_v_reg(ot, cpu_T[1], reg);
B
bellard 已提交
5006
        gen_op_testl_T0_T1_cc();
5007
        set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
5008
        break;
5009

B
bellard 已提交
5010 5011
    case 0xa8: /* test eAX, Iv */
    case 0xa9:
5012
        ot = mo_b_d(b, dflag);
5013
        val = insn_get(env, s, ot);
B
bellard 已提交
5014

5015
        gen_op_mov_v_reg(ot, cpu_T[0], OR_EAX);
5016
        tcg_gen_movi_tl(cpu_T[1], val);
B
bellard 已提交
5017
        gen_op_testl_T0_T1_cc();
5018
        set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
5019
        break;
5020

B
bellard 已提交
5021
    case 0x98: /* CWDE/CBW */
5022
        switch (dflag) {
B
bellard 已提交
5023
#ifdef TARGET_X86_64
5024
        case MO_64:
5025
            gen_op_mov_v_reg(MO_32, cpu_T[0], R_EAX);
B
bellard 已提交
5026
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
5027
            gen_op_mov_reg_v(MO_64, R_EAX, cpu_T[0]);
5028
            break;
B
bellard 已提交
5029
#endif
5030
        case MO_32:
5031
            gen_op_mov_v_reg(MO_16, cpu_T[0], R_EAX);
B
bellard 已提交
5032
            tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
5033
            gen_op_mov_reg_v(MO_32, R_EAX, cpu_T[0]);
5034 5035
            break;
        case MO_16:
5036
            gen_op_mov_v_reg(MO_8, cpu_T[0], R_EAX);
B
bellard 已提交
5037
            tcg_gen_ext8s_tl(cpu_T[0], cpu_T[0]);
5038
            gen_op_mov_reg_v(MO_16, R_EAX, cpu_T[0]);
5039 5040 5041
            break;
        default:
            tcg_abort();
B
bellard 已提交
5042
        }
B
bellard 已提交
5043 5044
        break;
    case 0x99: /* CDQ/CWD */
5045
        switch (dflag) {
B
bellard 已提交
5046
#ifdef TARGET_X86_64
5047
        case MO_64:
5048
            gen_op_mov_v_reg(MO_64, cpu_T[0], R_EAX);
B
bellard 已提交
5049
            tcg_gen_sari_tl(cpu_T[0], cpu_T[0], 63);
5050
            gen_op_mov_reg_v(MO_64, R_EDX, cpu_T[0]);
5051
            break;
B
bellard 已提交
5052
#endif
5053
        case MO_32:
5054
            gen_op_mov_v_reg(MO_32, cpu_T[0], R_EAX);
B
bellard 已提交
5055 5056
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
            tcg_gen_sari_tl(cpu_T[0], cpu_T[0], 31);
5057
            gen_op_mov_reg_v(MO_32, R_EDX, cpu_T[0]);
5058 5059
            break;
        case MO_16:
5060
            gen_op_mov_v_reg(MO_16, cpu_T[0], R_EAX);
B
bellard 已提交
5061 5062
            tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
            tcg_gen_sari_tl(cpu_T[0], cpu_T[0], 15);
5063
            gen_op_mov_reg_v(MO_16, R_EDX, cpu_T[0]);
5064 5065 5066
            break;
        default:
            tcg_abort();
B
bellard 已提交
5067
        }
B
bellard 已提交
5068 5069 5070 5071
        break;
    case 0x1af: /* imul Gv, Ev */
    case 0x69: /* imul Gv, Ev, I */
    case 0x6b:
5072
        ot = dflag;
5073
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5074 5075 5076 5077 5078
        reg = ((modrm >> 3) & 7) | rex_r;
        if (b == 0x69)
            s->rip_offset = insn_const_size(ot);
        else if (b == 0x6b)
            s->rip_offset = 1;
5079
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
bellard 已提交
5080
        if (b == 0x69) {
5081
            val = insn_get(env, s, ot);
5082
            tcg_gen_movi_tl(cpu_T[1], val);
B
bellard 已提交
5083
        } else if (b == 0x6b) {
5084
            val = (int8_t)insn_get(env, s, MO_8);
5085
            tcg_gen_movi_tl(cpu_T[1], val);
B
bellard 已提交
5086
        } else {
5087
            gen_op_mov_v_reg(ot, cpu_T[1], reg);
B
bellard 已提交
5088
        }
5089
        switch (ot) {
B
bellard 已提交
5090
#ifdef TARGET_X86_64
5091
        case MO_64:
5092 5093 5094 5095 5096
            tcg_gen_muls2_i64(cpu_regs[reg], cpu_T[1], cpu_T[0], cpu_T[1]);
            tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[reg]);
            tcg_gen_sari_tl(cpu_cc_src, cpu_cc_dst, 63);
            tcg_gen_sub_tl(cpu_cc_src, cpu_cc_src, cpu_T[1]);
            break;
B
bellard 已提交
5097
#endif
5098
        case MO_32:
5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109
            tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
            tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
            tcg_gen_muls2_i32(cpu_tmp2_i32, cpu_tmp3_i32,
                              cpu_tmp2_i32, cpu_tmp3_i32);
            tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp2_i32);
            tcg_gen_sari_i32(cpu_tmp2_i32, cpu_tmp2_i32, 31);
            tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[reg]);
            tcg_gen_sub_i32(cpu_tmp2_i32, cpu_tmp2_i32, cpu_tmp3_i32);
            tcg_gen_extu_i32_tl(cpu_cc_src, cpu_tmp2_i32);
            break;
        default:
B
bellard 已提交
5110 5111 5112 5113 5114 5115 5116
            tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
            tcg_gen_ext16s_tl(cpu_T[1], cpu_T[1]);
            /* XXX: use 32 bit mul which could be faster */
            tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
            tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
            tcg_gen_ext16s_tl(cpu_tmp0, cpu_T[0]);
            tcg_gen_sub_tl(cpu_cc_src, cpu_T[0], cpu_tmp0);
5117
            gen_op_mov_reg_v(ot, reg, cpu_T[0]);
5118
            break;
B
bellard 已提交
5119
        }
5120
        set_cc_op(s, CC_OP_MULB + ot);
B
bellard 已提交
5121 5122 5123
        break;
    case 0x1c0:
    case 0x1c1: /* xadd Ev, Gv */
5124
        ot = mo_b_d(b, dflag);
5125
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5126
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5127 5128
        mod = (modrm >> 6) & 3;
        if (mod == 3) {
B
bellard 已提交
5129
            rm = (modrm & 7) | REX_B(s);
5130 5131
            gen_op_mov_v_reg(ot, cpu_T[0], reg);
            gen_op_mov_v_reg(ot, cpu_T[1], rm);
5132
            tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
5133
            gen_op_mov_reg_v(ot, reg, cpu_T[1]);
5134
            gen_op_mov_reg_v(ot, rm, cpu_T[0]);
B
bellard 已提交
5135
        } else {
5136
            gen_lea_modrm(env, s, modrm);
5137
            gen_op_mov_v_reg(ot, cpu_T[0], reg);
5138
            gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
5139
            tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
5140
            gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
5141
            gen_op_mov_reg_v(ot, reg, cpu_T[1]);
B
bellard 已提交
5142 5143
        }
        gen_op_update2_cc();
5144
        set_cc_op(s, CC_OP_ADDB + ot);
B
bellard 已提交
5145 5146 5147
        break;
    case 0x1b0:
    case 0x1b1: /* cmpxchg Ev, Gv */
B
bellard 已提交
5148
        {
B
bellard 已提交
5149
            int label1, label2;
5150
            TCGv t0, t1, t2, a0;
B
bellard 已提交
5151

5152
            ot = mo_b_d(b, dflag);
5153
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5154 5155
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
P
pbrook 已提交
5156 5157 5158 5159
            t0 = tcg_temp_local_new();
            t1 = tcg_temp_local_new();
            t2 = tcg_temp_local_new();
            a0 = tcg_temp_local_new();
5160
            gen_op_mov_v_reg(ot, t1, reg);
B
bellard 已提交
5161 5162
            if (mod == 3) {
                rm = (modrm & 7) | REX_B(s);
5163
                gen_op_mov_v_reg(ot, t0, rm);
B
bellard 已提交
5164
            } else {
5165
                gen_lea_modrm(env, s, modrm);
5166
                tcg_gen_mov_tl(a0, cpu_A0);
5167
                gen_op_ld_v(s, ot, t0, a0);
B
bellard 已提交
5168 5169 5170
                rm = 0; /* avoid warning */
            }
            label1 = gen_new_label();
5171 5172
            tcg_gen_mov_tl(t2, cpu_regs[R_EAX]);
            gen_extu(ot, t0);
5173
            gen_extu(ot, t2);
5174
            tcg_gen_brcond_tl(TCG_COND_EQ, t2, t0, label1);
5175
            label2 = gen_new_label();
B
bellard 已提交
5176
            if (mod == 3) {
5177
                gen_op_mov_reg_v(ot, R_EAX, t0);
B
bellard 已提交
5178 5179
                tcg_gen_br(label2);
                gen_set_label(label1);
5180
                gen_op_mov_reg_v(ot, rm, t1);
B
bellard 已提交
5181
            } else {
5182 5183 5184
                /* perform no-op store cycle like physical cpu; must be
                   before changing accumulator to ensure idempotency if
                   the store faults and the instruction is restarted */
5185
                gen_op_st_v(s, ot, t0, a0);
5186
                gen_op_mov_reg_v(ot, R_EAX, t0);
5187
                tcg_gen_br(label2);
B
bellard 已提交
5188
                gen_set_label(label1);
5189
                gen_op_st_v(s, ot, t1, a0);
B
bellard 已提交
5190
            }
5191
            gen_set_label(label2);
5192
            tcg_gen_mov_tl(cpu_cc_src, t0);
5193 5194
            tcg_gen_mov_tl(cpu_cc_srcT, t2);
            tcg_gen_sub_tl(cpu_cc_dst, t2, t0);
5195
            set_cc_op(s, CC_OP_SUBB + ot);
5196 5197 5198 5199
            tcg_temp_free(t0);
            tcg_temp_free(t1);
            tcg_temp_free(t2);
            tcg_temp_free(a0);
B
bellard 已提交
5200 5201 5202
        }
        break;
    case 0x1c7: /* cmpxchg8b */
5203
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5204
        mod = (modrm >> 6) & 3;
5205
        if ((mod == 3) || ((modrm & 0x38) != 0x8))
B
bellard 已提交
5206
            goto illegal_op;
B
bellard 已提交
5207
#ifdef TARGET_X86_64
5208
        if (dflag == MO_64) {
B
bellard 已提交
5209 5210 5211
            if (!(s->cpuid_ext_features & CPUID_EXT_CX16))
                goto illegal_op;
            gen_jmp_im(pc_start - s->cs_base);
5212
            gen_update_cc_op(s);
5213
            gen_lea_modrm(env, s, modrm);
B
Blue Swirl 已提交
5214
            gen_helper_cmpxchg16b(cpu_env, cpu_A0);
B
bellard 已提交
5215 5216 5217 5218 5219 5220
        } else
#endif        
        {
            if (!(s->cpuid_features & CPUID_CX8))
                goto illegal_op;
            gen_jmp_im(pc_start - s->cs_base);
5221
            gen_update_cc_op(s);
5222
            gen_lea_modrm(env, s, modrm);
B
Blue Swirl 已提交
5223
            gen_helper_cmpxchg8b(cpu_env, cpu_A0);
B
bellard 已提交
5224
        }
5225
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
5226
        break;
5227

B
bellard 已提交
5228 5229 5230
        /**************************/
        /* push/pop */
    case 0x50 ... 0x57: /* push */
5231
        gen_op_mov_v_reg(MO_32, cpu_T[0], (b & 7) | REX_B(s));
5232
        gen_push_v(s, cpu_T[0]);
B
bellard 已提交
5233 5234
        break;
    case 0x58 ... 0x5f: /* pop */
5235
        ot = gen_pop_T0(s);
B
bellard 已提交
5236
        /* NOTE: order is important for pop %sp */
5237
        gen_pop_update(s, ot);
5238
        gen_op_mov_reg_v(ot, (b & 7) | REX_B(s), cpu_T[0]);
B
bellard 已提交
5239 5240
        break;
    case 0x60: /* pusha */
B
bellard 已提交
5241 5242
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5243 5244 5245
        gen_pusha(s);
        break;
    case 0x61: /* popa */
B
bellard 已提交
5246 5247
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5248 5249 5250 5251
        gen_popa(s);
        break;
    case 0x68: /* push Iv */
    case 0x6a:
5252
        ot = mo_pushpop(s, dflag);
B
bellard 已提交
5253
        if (b == 0x68)
5254
            val = insn_get(env, s, ot);
B
bellard 已提交
5255
        else
5256
            val = (int8_t)insn_get(env, s, MO_8);
5257
        tcg_gen_movi_tl(cpu_T[0], val);
5258
        gen_push_v(s, cpu_T[0]);
B
bellard 已提交
5259 5260
        break;
    case 0x8f: /* pop Ev */
5261
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5262
        mod = (modrm >> 6) & 3;
5263
        ot = gen_pop_T0(s);
B
bellard 已提交
5264 5265
        if (mod == 3) {
            /* NOTE: order is important for pop %sp */
5266
            gen_pop_update(s, ot);
B
bellard 已提交
5267
            rm = (modrm & 7) | REX_B(s);
5268
            gen_op_mov_reg_v(ot, rm, cpu_T[0]);
B
bellard 已提交
5269 5270
        } else {
            /* NOTE: order is important too for MMU exceptions */
B
bellard 已提交
5271
            s->popl_esp_hack = 1 << ot;
5272
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
5273
            s->popl_esp_hack = 0;
5274
            gen_pop_update(s, ot);
B
bellard 已提交
5275
        }
B
bellard 已提交
5276 5277 5278 5279
        break;
    case 0xc8: /* enter */
        {
            int level;
5280
            val = cpu_lduw_code(env, s->pc);
B
bellard 已提交
5281
            s->pc += 2;
5282
            level = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5283 5284 5285 5286 5287
            gen_enter(s, val, level);
        }
        break;
    case 0xc9: /* leave */
        /* XXX: exception not precise (ESP is updated before potential exception) */
B
bellard 已提交
5288
        if (CODE64(s)) {
5289
            gen_op_mov_v_reg(MO_64, cpu_T[0], R_EBP);
5290
            gen_op_mov_reg_v(MO_64, R_ESP, cpu_T[0]);
B
bellard 已提交
5291
        } else if (s->ss32) {
5292
            gen_op_mov_v_reg(MO_32, cpu_T[0], R_EBP);
5293
            gen_op_mov_reg_v(MO_32, R_ESP, cpu_T[0]);
B
bellard 已提交
5294
        } else {
5295
            gen_op_mov_v_reg(MO_16, cpu_T[0], R_EBP);
5296
            gen_op_mov_reg_v(MO_16, R_ESP, cpu_T[0]);
B
bellard 已提交
5297
        }
5298
        ot = gen_pop_T0(s);
5299
        gen_op_mov_reg_v(ot, R_EBP, cpu_T[0]);
5300
        gen_pop_update(s, ot);
B
bellard 已提交
5301 5302 5303 5304 5305
        break;
    case 0x06: /* push es */
    case 0x0e: /* push cs */
    case 0x16: /* push ss */
    case 0x1e: /* push ds */
B
bellard 已提交
5306 5307
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5308
        gen_op_movl_T0_seg(b >> 3);
5309
        gen_push_v(s, cpu_T[0]);
B
bellard 已提交
5310 5311 5312 5313
        break;
    case 0x1a0: /* push fs */
    case 0x1a8: /* push gs */
        gen_op_movl_T0_seg((b >> 3) & 7);
5314
        gen_push_v(s, cpu_T[0]);
B
bellard 已提交
5315 5316 5317 5318
        break;
    case 0x07: /* pop es */
    case 0x17: /* pop ss */
    case 0x1f: /* pop ds */
B
bellard 已提交
5319 5320
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5321
        reg = b >> 3;
5322
        ot = gen_pop_T0(s);
B
bellard 已提交
5323
        gen_movl_seg_T0(s, reg, pc_start - s->cs_base);
5324
        gen_pop_update(s, ot);
B
bellard 已提交
5325
        if (reg == R_SS) {
5326 5327 5328 5329
            /* if reg == SS, inhibit interrupts/trace. */
            /* If several instructions disable interrupts, only the
               _first_ does it */
            if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
5330
                gen_helper_set_inhibit_irq(cpu_env);
B
bellard 已提交
5331 5332 5333
            s->tf = 0;
        }
        if (s->is_jmp) {
B
bellard 已提交
5334
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5335 5336 5337 5338 5339
            gen_eob(s);
        }
        break;
    case 0x1a1: /* pop fs */
    case 0x1a9: /* pop gs */
5340
        ot = gen_pop_T0(s);
B
bellard 已提交
5341
        gen_movl_seg_T0(s, (b >> 3) & 7, pc_start - s->cs_base);
5342
        gen_pop_update(s, ot);
B
bellard 已提交
5343
        if (s->is_jmp) {
B
bellard 已提交
5344
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5345 5346 5347 5348 5349 5350 5351 5352
            gen_eob(s);
        }
        break;

        /**************************/
        /* mov */
    case 0x88:
    case 0x89: /* mov Gv, Ev */
5353
        ot = mo_b_d(b, dflag);
5354
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5355
        reg = ((modrm >> 3) & 7) | rex_r;
5356

B
bellard 已提交
5357
        /* generate a generic store */
5358
        gen_ldst_modrm(env, s, modrm, ot, reg, 1);
B
bellard 已提交
5359 5360 5361
        break;
    case 0xc6:
    case 0xc7: /* mov Ev, Iv */
5362
        ot = mo_b_d(b, dflag);
5363
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5364
        mod = (modrm >> 6) & 3;
B
bellard 已提交
5365 5366
        if (mod != 3) {
            s->rip_offset = insn_const_size(ot);
5367
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
5368
        }
5369
        val = insn_get(env, s, ot);
5370
        tcg_gen_movi_tl(cpu_T[0], val);
5371 5372 5373
        if (mod != 3) {
            gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
        } else {
5374
            gen_op_mov_reg_v(ot, (modrm & 7) | REX_B(s), cpu_T[0]);
5375
        }
B
bellard 已提交
5376 5377 5378
        break;
    case 0x8a:
    case 0x8b: /* mov Ev, Gv */
5379
        ot = mo_b_d(b, dflag);
5380
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5381
        reg = ((modrm >> 3) & 7) | rex_r;
5382

5383
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
5384
        gen_op_mov_reg_v(ot, reg, cpu_T[0]);
B
bellard 已提交
5385 5386
        break;
    case 0x8e: /* mov seg, Gv */
5387
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5388 5389 5390
        reg = (modrm >> 3) & 7;
        if (reg >= 6 || reg == R_CS)
            goto illegal_op;
5391
        gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
B
bellard 已提交
5392 5393 5394
        gen_movl_seg_T0(s, reg, pc_start - s->cs_base);
        if (reg == R_SS) {
            /* if reg == SS, inhibit interrupts/trace */
5395 5396 5397
            /* If several instructions disable interrupts, only the
               _first_ does it */
            if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
5398
                gen_helper_set_inhibit_irq(cpu_env);
B
bellard 已提交
5399 5400 5401
            s->tf = 0;
        }
        if (s->is_jmp) {
B
bellard 已提交
5402
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5403 5404 5405 5406
            gen_eob(s);
        }
        break;
    case 0x8c: /* mov Gv, seg */
5407
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5408 5409 5410 5411 5412
        reg = (modrm >> 3) & 7;
        mod = (modrm >> 6) & 3;
        if (reg >= 6)
            goto illegal_op;
        gen_op_movl_T0_seg(reg);
5413
        ot = mod == 3 ? dflag : MO_16;
5414
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
5415 5416 5417 5418 5419 5420 5421
        break;

    case 0x1b6: /* movzbS Gv, Eb */
    case 0x1b7: /* movzwS Gv, Eb */
    case 0x1be: /* movsbS Gv, Eb */
    case 0x1bf: /* movswS Gv, Eb */
        {
5422 5423 5424
            TCGMemOp d_ot;
            TCGMemOp s_ot;

B
bellard 已提交
5425
            /* d_ot is the size of destination */
5426
            d_ot = dflag;
B
bellard 已提交
5427
            /* ot is the size of source */
5428
            ot = (b & 1) + MO_8;
5429 5430 5431
            /* s_ot is the sign+size of source */
            s_ot = b & 8 ? MO_SIGN | ot : ot;

5432
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5433
            reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5434
            mod = (modrm >> 6) & 3;
B
bellard 已提交
5435
            rm = (modrm & 7) | REX_B(s);
5436

B
bellard 已提交
5437
            if (mod == 3) {
5438
                gen_op_mov_v_reg(ot, cpu_T[0], rm);
5439 5440
                switch (s_ot) {
                case MO_UB:
B
bellard 已提交
5441
                    tcg_gen_ext8u_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5442
                    break;
5443
                case MO_SB:
B
bellard 已提交
5444
                    tcg_gen_ext8s_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5445
                    break;
5446
                case MO_UW:
B
bellard 已提交
5447
                    tcg_gen_ext16u_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5448 5449
                    break;
                default:
5450
                case MO_SW:
B
bellard 已提交
5451
                    tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5452 5453
                    break;
                }
5454
                gen_op_mov_reg_v(d_ot, reg, cpu_T[0]);
B
bellard 已提交
5455
            } else {
5456
                gen_lea_modrm(env, s, modrm);
5457
                gen_op_ld_v(s, s_ot, cpu_T[0], cpu_A0);
5458
                gen_op_mov_reg_v(d_ot, reg, cpu_T[0]);
B
bellard 已提交
5459 5460 5461 5462 5463
            }
        }
        break;

    case 0x8d: /* lea */
5464
        ot = dflag;
5465
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5466 5467 5468
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
B
bellard 已提交
5469
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5470 5471 5472 5473
        /* we must ensure that no segment is added */
        s->override = -1;
        val = s->addseg;
        s->addseg = 0;
5474
        gen_lea_modrm(env, s, modrm);
B
bellard 已提交
5475
        s->addseg = val;
5476
        gen_op_mov_reg_v(ot, reg, cpu_A0);
B
bellard 已提交
5477
        break;
5478

B
bellard 已提交
5479 5480 5481 5482 5483
    case 0xa0: /* mov EAX, Ov */
    case 0xa1:
    case 0xa2: /* mov Ov, EAX */
    case 0xa3:
        {
B
bellard 已提交
5484 5485
            target_ulong offset_addr;

5486
            ot = mo_b_d(b, dflag);
5487
            switch (s->aflag) {
B
bellard 已提交
5488
#ifdef TARGET_X86_64
5489
            case MO_64:
5490
                offset_addr = cpu_ldq_code(env, s->pc);
B
bellard 已提交
5491
                s->pc += 8;
5492
                break;
B
bellard 已提交
5493
#endif
5494 5495 5496
            default:
                offset_addr = insn_get(env, s, s->aflag);
                break;
B
bellard 已提交
5497
            }
5498
            tcg_gen_movi_tl(cpu_A0, offset_addr);
B
bellard 已提交
5499
            gen_add_A0_ds_seg(s);
B
bellard 已提交
5500
            if ((b & 2) == 0) {
5501
                gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
5502
                gen_op_mov_reg_v(ot, R_EAX, cpu_T[0]);
B
bellard 已提交
5503
            } else {
5504
                gen_op_mov_v_reg(ot, cpu_T[0], R_EAX);
5505
                gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
5506 5507 5508 5509
            }
        }
        break;
    case 0xd7: /* xlat */
5510 5511 5512 5513
        tcg_gen_mov_tl(cpu_A0, cpu_regs[R_EBX]);
        tcg_gen_ext8u_tl(cpu_T[0], cpu_regs[R_EAX]);
        tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_T[0]);
        gen_extu(s->aflag, cpu_A0);
B
bellard 已提交
5514
        gen_add_A0_ds_seg(s);
5515
        gen_op_ld_v(s, MO_8, cpu_T[0], cpu_A0);
5516
        gen_op_mov_reg_v(MO_8, R_EAX, cpu_T[0]);
B
bellard 已提交
5517 5518
        break;
    case 0xb0 ... 0xb7: /* mov R, Ib */
5519
        val = insn_get(env, s, MO_8);
5520
        tcg_gen_movi_tl(cpu_T[0], val);
5521
        gen_op_mov_reg_v(MO_8, (b & 7) | REX_B(s), cpu_T[0]);
B
bellard 已提交
5522 5523
        break;
    case 0xb8 ... 0xbf: /* mov R, Iv */
B
bellard 已提交
5524
#ifdef TARGET_X86_64
5525
        if (dflag == MO_64) {
B
bellard 已提交
5526 5527
            uint64_t tmp;
            /* 64 bit case */
5528
            tmp = cpu_ldq_code(env, s->pc);
B
bellard 已提交
5529 5530
            s->pc += 8;
            reg = (b & 7) | REX_B(s);
5531
            tcg_gen_movi_tl(cpu_T[0], tmp);
5532
            gen_op_mov_reg_v(MO_64, reg, cpu_T[0]);
5533
        } else
B
bellard 已提交
5534 5535
#endif
        {
5536
            ot = dflag;
5537
            val = insn_get(env, s, ot);
B
bellard 已提交
5538
            reg = (b & 7) | REX_B(s);
5539
            tcg_gen_movi_tl(cpu_T[0], val);
5540
            gen_op_mov_reg_v(ot, reg, cpu_T[0]);
B
bellard 已提交
5541
        }
B
bellard 已提交
5542 5543 5544
        break;

    case 0x91 ... 0x97: /* xchg R, EAX */
R
Richard Henderson 已提交
5545
    do_xchg_reg_eax:
5546
        ot = dflag;
B
bellard 已提交
5547
        reg = (b & 7) | REX_B(s);
B
bellard 已提交
5548 5549 5550 5551
        rm = R_EAX;
        goto do_xchg_reg;
    case 0x86:
    case 0x87: /* xchg Ev, Gv */
5552
        ot = mo_b_d(b, dflag);
5553
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5554
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5555 5556
        mod = (modrm >> 6) & 3;
        if (mod == 3) {
B
bellard 已提交
5557
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
5558
        do_xchg_reg:
5559 5560
            gen_op_mov_v_reg(ot, cpu_T[0], reg);
            gen_op_mov_v_reg(ot, cpu_T[1], rm);
5561
            gen_op_mov_reg_v(ot, rm, cpu_T[0]);
5562
            gen_op_mov_reg_v(ot, reg, cpu_T[1]);
B
bellard 已提交
5563
        } else {
5564
            gen_lea_modrm(env, s, modrm);
5565
            gen_op_mov_v_reg(ot, cpu_T[0], reg);
B
bellard 已提交
5566 5567
            /* for xchg, lock is implicit */
            if (!(prefixes & PREFIX_LOCK))
P
pbrook 已提交
5568
                gen_helper_lock();
5569
            gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
5570
            gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
5571
            if (!(prefixes & PREFIX_LOCK))
P
pbrook 已提交
5572
                gen_helper_unlock();
5573
            gen_op_mov_reg_v(ot, reg, cpu_T[1]);
B
bellard 已提交
5574 5575 5576
        }
        break;
    case 0xc4: /* les Gv */
5577
        /* In CODE64 this is VEX3; see above.  */
B
bellard 已提交
5578 5579 5580
        op = R_ES;
        goto do_lxx;
    case 0xc5: /* lds Gv */
5581
        /* In CODE64 this is VEX2; see above.  */
B
bellard 已提交
5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592
        op = R_DS;
        goto do_lxx;
    case 0x1b2: /* lss Gv */
        op = R_SS;
        goto do_lxx;
    case 0x1b4: /* lfs Gv */
        op = R_FS;
        goto do_lxx;
    case 0x1b5: /* lgs Gv */
        op = R_GS;
    do_lxx:
5593
        ot = dflag != MO_16 ? MO_32 : MO_16;
5594
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5595
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5596 5597 5598
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
5599
        gen_lea_modrm(env, s, modrm);
5600
        gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
5601
        gen_add_A0_im(s, 1 << ot);
B
bellard 已提交
5602
        /* load the segment first to handle exceptions properly */
5603
        gen_op_ld_v(s, MO_16, cpu_T[0], cpu_A0);
B
bellard 已提交
5604 5605
        gen_movl_seg_T0(s, op, pc_start - s->cs_base);
        /* then put the data */
5606
        gen_op_mov_reg_v(ot, reg, cpu_T[1]);
B
bellard 已提交
5607
        if (s->is_jmp) {
B
bellard 已提交
5608
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5609 5610 5611
            gen_eob(s);
        }
        break;
5612

B
bellard 已提交
5613 5614 5615 5616 5617 5618 5619 5620
        /************************/
        /* shifts */
    case 0xc0:
    case 0xc1:
        /* shift Ev,Ib */
        shift = 2;
    grp2:
        {
5621
            ot = mo_b_d(b, dflag);
5622
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5623 5624
            mod = (modrm >> 6) & 3;
            op = (modrm >> 3) & 7;
5625

B
bellard 已提交
5626
            if (mod != 3) {
B
bellard 已提交
5627 5628 5629
                if (shift == 2) {
                    s->rip_offset = 1;
                }
5630
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
5631 5632
                opreg = OR_TMP0;
            } else {
B
bellard 已提交
5633
                opreg = (modrm & 7) | REX_B(s);
B
bellard 已提交
5634 5635 5636 5637 5638 5639 5640
            }

            /* simpler op */
            if (shift == 0) {
                gen_shift(s, op, ot, opreg, OR_ECX);
            } else {
                if (shift == 2) {
5641
                    shift = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673
                }
                gen_shifti(s, op, ot, opreg, shift);
            }
        }
        break;
    case 0xd0:
    case 0xd1:
        /* shift Ev,1 */
        shift = 1;
        goto grp2;
    case 0xd2:
    case 0xd3:
        /* shift Ev,cl */
        shift = 0;
        goto grp2;

    case 0x1a4: /* shld imm */
        op = 0;
        shift = 1;
        goto do_shiftd;
    case 0x1a5: /* shld cl */
        op = 0;
        shift = 0;
        goto do_shiftd;
    case 0x1ac: /* shrd imm */
        op = 1;
        shift = 1;
        goto do_shiftd;
    case 0x1ad: /* shrd cl */
        op = 1;
        shift = 0;
    do_shiftd:
5674
        ot = dflag;
5675
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5676
        mod = (modrm >> 6) & 3;
B
bellard 已提交
5677 5678
        rm = (modrm & 7) | REX_B(s);
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5679
        if (mod != 3) {
5680
            gen_lea_modrm(env, s, modrm);
5681
            opreg = OR_TMP0;
B
bellard 已提交
5682
        } else {
5683
            opreg = rm;
B
bellard 已提交
5684
        }
5685
        gen_op_mov_v_reg(ot, cpu_T[1], reg);
5686

B
bellard 已提交
5687
        if (shift) {
P
Paolo Bonzini 已提交
5688 5689 5690
            TCGv imm = tcg_const_tl(cpu_ldub_code(env, s->pc++));
            gen_shiftd_rm_T1(s, ot, opreg, op, imm);
            tcg_temp_free(imm);
B
bellard 已提交
5691
        } else {
P
Paolo Bonzini 已提交
5692
            gen_shiftd_rm_T1(s, ot, opreg, op, cpu_regs[R_ECX]);
B
bellard 已提交
5693 5694 5695 5696 5697
        }
        break;

        /************************/
        /* floats */
5698
    case 0xd8 ... 0xdf:
B
bellard 已提交
5699 5700 5701 5702 5703 5704
        if (s->flags & (HF_EM_MASK | HF_TS_MASK)) {
            /* if CR0.EM or CR0.TS are set, generate an FPU exception */
            /* XXX: what to do if illegal op ? */
            gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
            break;
        }
5705
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5706 5707 5708 5709 5710
        mod = (modrm >> 6) & 3;
        rm = modrm & 7;
        op = ((b & 7) << 3) | ((modrm >> 3) & 7);
        if (mod != 3) {
            /* memory op */
5711
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722
            switch(op) {
            case 0x00 ... 0x07: /* fxxxs */
            case 0x10 ... 0x17: /* fixxxl */
            case 0x20 ... 0x27: /* fxxxl */
            case 0x30 ... 0x37: /* fixxx */
                {
                    int op1;
                    op1 = op & 7;

                    switch(op >> 4) {
                    case 0:
5723 5724
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
Blue Swirl 已提交
5725
                        gen_helper_flds_FT0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5726 5727
                        break;
                    case 1:
5728 5729
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
Blue Swirl 已提交
5730
                        gen_helper_fildl_FT0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5731 5732
                        break;
                    case 2:
5733 5734
                        tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0,
                                            s->mem_index, MO_LEQ);
B
Blue Swirl 已提交
5735
                        gen_helper_fldl_FT0(cpu_env, cpu_tmp1_i64);
B
bellard 已提交
5736 5737 5738
                        break;
                    case 3:
                    default:
5739 5740
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LESW);
B
Blue Swirl 已提交
5741
                        gen_helper_fildl_FT0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5742 5743
                        break;
                    }
5744

P
pbrook 已提交
5745
                    gen_helper_fp_arith_ST0_FT0(op1);
B
bellard 已提交
5746 5747
                    if (op1 == 3) {
                        /* fcomp needs pop */
B
Blue Swirl 已提交
5748
                        gen_helper_fpop(cpu_env);
B
bellard 已提交
5749 5750 5751 5752 5753 5754
                    }
                }
                break;
            case 0x08: /* flds */
            case 0x0a: /* fsts */
            case 0x0b: /* fstps */
B
bellard 已提交
5755 5756 5757
            case 0x18 ... 0x1b: /* fildl, fisttpl, fistl, fistpl */
            case 0x28 ... 0x2b: /* fldl, fisttpll, fstl, fstpl */
            case 0x38 ... 0x3b: /* filds, fisttps, fists, fistps */
B
bellard 已提交
5758 5759 5760 5761
                switch(op & 7) {
                case 0:
                    switch(op >> 4) {
                    case 0:
5762 5763
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
Blue Swirl 已提交
5764
                        gen_helper_flds_ST0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5765 5766
                        break;
                    case 1:
5767 5768
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
Blue Swirl 已提交
5769
                        gen_helper_fildl_ST0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5770 5771
                        break;
                    case 2:
5772 5773
                        tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0,
                                            s->mem_index, MO_LEQ);
B
Blue Swirl 已提交
5774
                        gen_helper_fldl_ST0(cpu_env, cpu_tmp1_i64);
B
bellard 已提交
5775 5776 5777
                        break;
                    case 3:
                    default:
5778 5779
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LESW);
B
Blue Swirl 已提交
5780
                        gen_helper_fildl_ST0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5781 5782 5783
                        break;
                    }
                    break;
B
bellard 已提交
5784
                case 1:
B
bellard 已提交
5785
                    /* XXX: the corresponding CPUID bit must be tested ! */
B
bellard 已提交
5786 5787
                    switch(op >> 4) {
                    case 1:
B
Blue Swirl 已提交
5788
                        gen_helper_fisttl_ST0(cpu_tmp2_i32, cpu_env);
5789 5790
                        tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
bellard 已提交
5791 5792
                        break;
                    case 2:
B
Blue Swirl 已提交
5793
                        gen_helper_fisttll_ST0(cpu_tmp1_i64, cpu_env);
5794 5795
                        tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0,
                                            s->mem_index, MO_LEQ);
B
bellard 已提交
5796 5797 5798
                        break;
                    case 3:
                    default:
B
Blue Swirl 已提交
5799
                        gen_helper_fistt_ST0(cpu_tmp2_i32, cpu_env);
5800 5801
                        tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUW);
B
bellard 已提交
5802
                        break;
B
bellard 已提交
5803
                    }
B
Blue Swirl 已提交
5804
                    gen_helper_fpop(cpu_env);
B
bellard 已提交
5805
                    break;
B
bellard 已提交
5806 5807 5808
                default:
                    switch(op >> 4) {
                    case 0:
B
Blue Swirl 已提交
5809
                        gen_helper_fsts_ST0(cpu_tmp2_i32, cpu_env);
5810 5811
                        tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
bellard 已提交
5812 5813
                        break;
                    case 1:
B
Blue Swirl 已提交
5814
                        gen_helper_fistl_ST0(cpu_tmp2_i32, cpu_env);
5815 5816
                        tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
bellard 已提交
5817 5818
                        break;
                    case 2:
B
Blue Swirl 已提交
5819
                        gen_helper_fstl_ST0(cpu_tmp1_i64, cpu_env);
5820 5821
                        tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0,
                                            s->mem_index, MO_LEQ);
B
bellard 已提交
5822 5823 5824
                        break;
                    case 3:
                    default:
B
Blue Swirl 已提交
5825
                        gen_helper_fist_ST0(cpu_tmp2_i32, cpu_env);
5826 5827
                        tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUW);
B
bellard 已提交
5828 5829 5830
                        break;
                    }
                    if ((op & 7) == 3)
B
Blue Swirl 已提交
5831
                        gen_helper_fpop(cpu_env);
B
bellard 已提交
5832 5833 5834 5835
                    break;
                }
                break;
            case 0x0c: /* fldenv mem */
5836
                gen_update_cc_op(s);
B
bellard 已提交
5837
                gen_jmp_im(pc_start - s->cs_base);
5838
                gen_helper_fldenv(cpu_env, cpu_A0, tcg_const_i32(dflag - 1));
B
bellard 已提交
5839 5840
                break;
            case 0x0d: /* fldcw mem */
5841 5842
                tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                    s->mem_index, MO_LEUW);
B
Blue Swirl 已提交
5843
                gen_helper_fldcw(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5844 5845
                break;
            case 0x0e: /* fnstenv mem */
5846
                gen_update_cc_op(s);
B
bellard 已提交
5847
                gen_jmp_im(pc_start - s->cs_base);
5848
                gen_helper_fstenv(cpu_env, cpu_A0, tcg_const_i32(dflag - 1));
B
bellard 已提交
5849 5850
                break;
            case 0x0f: /* fnstcw mem */
B
Blue Swirl 已提交
5851
                gen_helper_fnstcw(cpu_tmp2_i32, cpu_env);
5852 5853
                tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                    s->mem_index, MO_LEUW);
B
bellard 已提交
5854 5855
                break;
            case 0x1d: /* fldt mem */
5856
                gen_update_cc_op(s);
B
bellard 已提交
5857
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
5858
                gen_helper_fldt_ST0(cpu_env, cpu_A0);
B
bellard 已提交
5859 5860
                break;
            case 0x1f: /* fstpt mem */
5861
                gen_update_cc_op(s);
B
bellard 已提交
5862
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
5863 5864
                gen_helper_fstt_ST0(cpu_env, cpu_A0);
                gen_helper_fpop(cpu_env);
B
bellard 已提交
5865 5866
                break;
            case 0x2c: /* frstor mem */
5867
                gen_update_cc_op(s);
B
bellard 已提交
5868
                gen_jmp_im(pc_start - s->cs_base);
5869
                gen_helper_frstor(cpu_env, cpu_A0, tcg_const_i32(dflag - 1));
B
bellard 已提交
5870 5871
                break;
            case 0x2e: /* fnsave mem */
5872
                gen_update_cc_op(s);
B
bellard 已提交
5873
                gen_jmp_im(pc_start - s->cs_base);
5874
                gen_helper_fsave(cpu_env, cpu_A0, tcg_const_i32(dflag - 1));
B
bellard 已提交
5875 5876
                break;
            case 0x2f: /* fnstsw mem */
B
Blue Swirl 已提交
5877
                gen_helper_fnstsw(cpu_tmp2_i32, cpu_env);
5878 5879
                tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                    s->mem_index, MO_LEUW);
B
bellard 已提交
5880 5881
                break;
            case 0x3c: /* fbld */
5882
                gen_update_cc_op(s);
B
bellard 已提交
5883
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
5884
                gen_helper_fbld_ST0(cpu_env, cpu_A0);
B
bellard 已提交
5885 5886
                break;
            case 0x3e: /* fbstp */
5887
                gen_update_cc_op(s);
B
bellard 已提交
5888
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
5889 5890
                gen_helper_fbst_ST0(cpu_env, cpu_A0);
                gen_helper_fpop(cpu_env);
B
bellard 已提交
5891 5892
                break;
            case 0x3d: /* fildll */
5893
                tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
B
Blue Swirl 已提交
5894
                gen_helper_fildll_ST0(cpu_env, cpu_tmp1_i64);
B
bellard 已提交
5895 5896
                break;
            case 0x3f: /* fistpll */
B
Blue Swirl 已提交
5897
                gen_helper_fistll_ST0(cpu_tmp1_i64, cpu_env);
5898
                tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
B
Blue Swirl 已提交
5899
                gen_helper_fpop(cpu_env);
B
bellard 已提交
5900 5901 5902 5903 5904 5905 5906 5907 5908 5909
                break;
            default:
                goto illegal_op;
            }
        } else {
            /* register float ops */
            opreg = rm;

            switch(op) {
            case 0x08: /* fld sti */
B
Blue Swirl 已提交
5910 5911 5912
                gen_helper_fpush(cpu_env);
                gen_helper_fmov_ST0_STN(cpu_env,
                                        tcg_const_i32((opreg + 1) & 7));
B
bellard 已提交
5913 5914
                break;
            case 0x09: /* fxchg sti */
B
bellard 已提交
5915 5916
            case 0x29: /* fxchg4 sti, undocumented op */
            case 0x39: /* fxchg7 sti, undocumented op */
B
Blue Swirl 已提交
5917
                gen_helper_fxchg_ST0_STN(cpu_env, tcg_const_i32(opreg));
B
bellard 已提交
5918 5919 5920 5921
                break;
            case 0x0a: /* grp d9/2 */
                switch(rm) {
                case 0: /* fnop */
5922
                    /* check exceptions (FreeBSD FPU probe) */
5923
                    gen_update_cc_op(s);
B
bellard 已提交
5924
                    gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
5925
                    gen_helper_fwait(cpu_env);
B
bellard 已提交
5926 5927 5928 5929 5930 5931 5932 5933
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x0c: /* grp d9/4 */
                switch(rm) {
                case 0: /* fchs */
B
Blue Swirl 已提交
5934
                    gen_helper_fchs_ST0(cpu_env);
B
bellard 已提交
5935 5936
                    break;
                case 1: /* fabs */
B
Blue Swirl 已提交
5937
                    gen_helper_fabs_ST0(cpu_env);
B
bellard 已提交
5938 5939
                    break;
                case 4: /* ftst */
B
Blue Swirl 已提交
5940 5941
                    gen_helper_fldz_FT0(cpu_env);
                    gen_helper_fcom_ST0_FT0(cpu_env);
B
bellard 已提交
5942 5943
                    break;
                case 5: /* fxam */
B
Blue Swirl 已提交
5944
                    gen_helper_fxam_ST0(cpu_env);
B
bellard 已提交
5945 5946 5947 5948 5949 5950 5951 5952 5953
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x0d: /* grp d9/5 */
                {
                    switch(rm) {
                    case 0:
B
Blue Swirl 已提交
5954 5955
                        gen_helper_fpush(cpu_env);
                        gen_helper_fld1_ST0(cpu_env);
B
bellard 已提交
5956 5957
                        break;
                    case 1:
B
Blue Swirl 已提交
5958 5959
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldl2t_ST0(cpu_env);
B
bellard 已提交
5960 5961
                        break;
                    case 2:
B
Blue Swirl 已提交
5962 5963
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldl2e_ST0(cpu_env);
B
bellard 已提交
5964 5965
                        break;
                    case 3:
B
Blue Swirl 已提交
5966 5967
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldpi_ST0(cpu_env);
B
bellard 已提交
5968 5969
                        break;
                    case 4:
B
Blue Swirl 已提交
5970 5971
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldlg2_ST0(cpu_env);
B
bellard 已提交
5972 5973
                        break;
                    case 5:
B
Blue Swirl 已提交
5974 5975
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldln2_ST0(cpu_env);
B
bellard 已提交
5976 5977
                        break;
                    case 6:
B
Blue Swirl 已提交
5978 5979
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldz_ST0(cpu_env);
B
bellard 已提交
5980 5981 5982 5983 5984 5985 5986 5987 5988
                        break;
                    default:
                        goto illegal_op;
                    }
                }
                break;
            case 0x0e: /* grp d9/6 */
                switch(rm) {
                case 0: /* f2xm1 */
B
Blue Swirl 已提交
5989
                    gen_helper_f2xm1(cpu_env);
B
bellard 已提交
5990 5991
                    break;
                case 1: /* fyl2x */
B
Blue Swirl 已提交
5992
                    gen_helper_fyl2x(cpu_env);
B
bellard 已提交
5993 5994
                    break;
                case 2: /* fptan */
B
Blue Swirl 已提交
5995
                    gen_helper_fptan(cpu_env);
B
bellard 已提交
5996 5997
                    break;
                case 3: /* fpatan */
B
Blue Swirl 已提交
5998
                    gen_helper_fpatan(cpu_env);
B
bellard 已提交
5999 6000
                    break;
                case 4: /* fxtract */
B
Blue Swirl 已提交
6001
                    gen_helper_fxtract(cpu_env);
B
bellard 已提交
6002 6003
                    break;
                case 5: /* fprem1 */
B
Blue Swirl 已提交
6004
                    gen_helper_fprem1(cpu_env);
B
bellard 已提交
6005 6006
                    break;
                case 6: /* fdecstp */
B
Blue Swirl 已提交
6007
                    gen_helper_fdecstp(cpu_env);
B
bellard 已提交
6008 6009 6010
                    break;
                default:
                case 7: /* fincstp */
B
Blue Swirl 已提交
6011
                    gen_helper_fincstp(cpu_env);
B
bellard 已提交
6012 6013 6014 6015 6016 6017
                    break;
                }
                break;
            case 0x0f: /* grp d9/7 */
                switch(rm) {
                case 0: /* fprem */
B
Blue Swirl 已提交
6018
                    gen_helper_fprem(cpu_env);
B
bellard 已提交
6019 6020
                    break;
                case 1: /* fyl2xp1 */
B
Blue Swirl 已提交
6021
                    gen_helper_fyl2xp1(cpu_env);
B
bellard 已提交
6022 6023
                    break;
                case 2: /* fsqrt */
B
Blue Swirl 已提交
6024
                    gen_helper_fsqrt(cpu_env);
B
bellard 已提交
6025 6026
                    break;
                case 3: /* fsincos */
B
Blue Swirl 已提交
6027
                    gen_helper_fsincos(cpu_env);
B
bellard 已提交
6028 6029
                    break;
                case 5: /* fscale */
B
Blue Swirl 已提交
6030
                    gen_helper_fscale(cpu_env);
B
bellard 已提交
6031 6032
                    break;
                case 4: /* frndint */
B
Blue Swirl 已提交
6033
                    gen_helper_frndint(cpu_env);
B
bellard 已提交
6034 6035
                    break;
                case 6: /* fsin */
B
Blue Swirl 已提交
6036
                    gen_helper_fsin(cpu_env);
B
bellard 已提交
6037 6038 6039
                    break;
                default:
                case 7: /* fcos */
B
Blue Swirl 已提交
6040
                    gen_helper_fcos(cpu_env);
B
bellard 已提交
6041 6042 6043 6044 6045 6046 6047 6048
                    break;
                }
                break;
            case 0x00: case 0x01: case 0x04 ... 0x07: /* fxxx st, sti */
            case 0x20: case 0x21: case 0x24 ... 0x27: /* fxxx sti, st */
            case 0x30: case 0x31: case 0x34 ... 0x37: /* fxxxp sti, st */
                {
                    int op1;
6049

B
bellard 已提交
6050 6051
                    op1 = op & 7;
                    if (op >= 0x20) {
P
pbrook 已提交
6052
                        gen_helper_fp_arith_STN_ST0(op1, opreg);
B
bellard 已提交
6053
                        if (op >= 0x30)
B
Blue Swirl 已提交
6054
                            gen_helper_fpop(cpu_env);
B
bellard 已提交
6055
                    } else {
B
Blue Swirl 已提交
6056
                        gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
P
pbrook 已提交
6057
                        gen_helper_fp_arith_ST0_FT0(op1);
B
bellard 已提交
6058 6059 6060 6061
                    }
                }
                break;
            case 0x02: /* fcom */
B
bellard 已提交
6062
            case 0x22: /* fcom2, undocumented op */
B
Blue Swirl 已提交
6063 6064
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fcom_ST0_FT0(cpu_env);
B
bellard 已提交
6065 6066
                break;
            case 0x03: /* fcomp */
B
bellard 已提交
6067 6068
            case 0x23: /* fcomp3, undocumented op */
            case 0x32: /* fcomp5, undocumented op */
B
Blue Swirl 已提交
6069 6070 6071
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fcom_ST0_FT0(cpu_env);
                gen_helper_fpop(cpu_env);
B
bellard 已提交
6072 6073 6074 6075
                break;
            case 0x15: /* da/5 */
                switch(rm) {
                case 1: /* fucompp */
B
Blue Swirl 已提交
6076 6077 6078 6079
                    gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(1));
                    gen_helper_fucom_ST0_FT0(cpu_env);
                    gen_helper_fpop(cpu_env);
                    gen_helper_fpop(cpu_env);
B
bellard 已提交
6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x1c:
                switch(rm) {
                case 0: /* feni (287 only, just do nop here) */
                    break;
                case 1: /* fdisi (287 only, just do nop here) */
                    break;
                case 2: /* fclex */
B
Blue Swirl 已提交
6092
                    gen_helper_fclex(cpu_env);
B
bellard 已提交
6093 6094
                    break;
                case 3: /* fninit */
B
Blue Swirl 已提交
6095
                    gen_helper_fninit(cpu_env);
B
bellard 已提交
6096 6097 6098 6099 6100 6101 6102 6103
                    break;
                case 4: /* fsetpm (287 only, just do nop here) */
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x1d: /* fucomi */
6104 6105 6106
                if (!(s->cpuid_features & CPUID_CMOV)) {
                    goto illegal_op;
                }
6107
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6108 6109
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fucomi_ST0_FT0(cpu_env);
6110
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6111 6112
                break;
            case 0x1e: /* fcomi */
6113 6114 6115
                if (!(s->cpuid_features & CPUID_CMOV)) {
                    goto illegal_op;
                }
6116
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6117 6118
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fcomi_ST0_FT0(cpu_env);
6119
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6120
                break;
B
bellard 已提交
6121
            case 0x28: /* ffree sti */
B
Blue Swirl 已提交
6122
                gen_helper_ffree_STN(cpu_env, tcg_const_i32(opreg));
6123
                break;
B
bellard 已提交
6124
            case 0x2a: /* fst sti */
B
Blue Swirl 已提交
6125
                gen_helper_fmov_STN_ST0(cpu_env, tcg_const_i32(opreg));
B
bellard 已提交
6126 6127
                break;
            case 0x2b: /* fstp sti */
B
bellard 已提交
6128 6129 6130
            case 0x0b: /* fstp1 sti, undocumented op */
            case 0x3a: /* fstp8 sti, undocumented op */
            case 0x3b: /* fstp9 sti, undocumented op */
B
Blue Swirl 已提交
6131 6132
                gen_helper_fmov_STN_ST0(cpu_env, tcg_const_i32(opreg));
                gen_helper_fpop(cpu_env);
B
bellard 已提交
6133 6134
                break;
            case 0x2c: /* fucom st(i) */
B
Blue Swirl 已提交
6135 6136
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fucom_ST0_FT0(cpu_env);
B
bellard 已提交
6137 6138
                break;
            case 0x2d: /* fucomp st(i) */
B
Blue Swirl 已提交
6139 6140 6141
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fucom_ST0_FT0(cpu_env);
                gen_helper_fpop(cpu_env);
B
bellard 已提交
6142 6143 6144 6145
                break;
            case 0x33: /* de/3 */
                switch(rm) {
                case 1: /* fcompp */
B
Blue Swirl 已提交
6146 6147 6148 6149
                    gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(1));
                    gen_helper_fcom_ST0_FT0(cpu_env);
                    gen_helper_fpop(cpu_env);
                    gen_helper_fpop(cpu_env);
B
bellard 已提交
6150 6151 6152 6153 6154
                    break;
                default:
                    goto illegal_op;
                }
                break;
B
bellard 已提交
6155
            case 0x38: /* ffreep sti, undocumented op */
B
Blue Swirl 已提交
6156 6157
                gen_helper_ffree_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fpop(cpu_env);
B
bellard 已提交
6158
                break;
B
bellard 已提交
6159 6160 6161
            case 0x3c: /* df/4 */
                switch(rm) {
                case 0:
B
Blue Swirl 已提交
6162
                    gen_helper_fnstsw(cpu_tmp2_i32, cpu_env);
6163
                    tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
6164
                    gen_op_mov_reg_v(MO_16, R_EAX, cpu_T[0]);
B
bellard 已提交
6165 6166 6167 6168 6169 6170
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x3d: /* fucomip */
6171 6172 6173
                if (!(s->cpuid_features & CPUID_CMOV)) {
                    goto illegal_op;
                }
6174
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6175 6176 6177
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fucomi_ST0_FT0(cpu_env);
                gen_helper_fpop(cpu_env);
6178
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6179 6180
                break;
            case 0x3e: /* fcomip */
6181 6182 6183
                if (!(s->cpuid_features & CPUID_CMOV)) {
                    goto illegal_op;
                }
6184
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6185 6186 6187
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fcomi_ST0_FT0(cpu_env);
                gen_helper_fpop(cpu_env);
6188
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6189
                break;
6190 6191 6192
            case 0x10 ... 0x13: /* fcmovxx */
            case 0x18 ... 0x1b:
                {
B
bellard 已提交
6193
                    int op1, l1;
6194
                    static const uint8_t fcmov_cc[8] = {
6195 6196 6197 6198 6199
                        (JCC_B << 1),
                        (JCC_Z << 1),
                        (JCC_BE << 1),
                        (JCC_P << 1),
                    };
6200 6201 6202 6203

                    if (!(s->cpuid_features & CPUID_CMOV)) {
                        goto illegal_op;
                    }
6204
                    op1 = fcmov_cc[op & 3] | (((op >> 3) & 1) ^ 1);
B
bellard 已提交
6205
                    l1 = gen_new_label();
6206
                    gen_jcc1_noeob(s, op1, l1);
B
Blue Swirl 已提交
6207
                    gen_helper_fmov_ST0_STN(cpu_env, tcg_const_i32(opreg));
B
bellard 已提交
6208
                    gen_set_label(l1);
6209 6210
                }
                break;
B
bellard 已提交
6211 6212 6213 6214 6215 6216 6217 6218 6219 6220
            default:
                goto illegal_op;
            }
        }
        break;
        /************************/
        /* string ops */

    case 0xa4: /* movsS */
    case 0xa5:
6221
        ot = mo_b_d(b, dflag);
B
bellard 已提交
6222 6223 6224 6225 6226 6227
        if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
            gen_repz_movs(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
        } else {
            gen_movs(s, ot);
        }
        break;
6228

B
bellard 已提交
6229 6230
    case 0xaa: /* stosS */
    case 0xab:
6231
        ot = mo_b_d(b, dflag);
B
bellard 已提交
6232 6233 6234 6235 6236 6237 6238 6239
        if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
            gen_repz_stos(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
        } else {
            gen_stos(s, ot);
        }
        break;
    case 0xac: /* lodsS */
    case 0xad:
6240
        ot = mo_b_d(b, dflag);
B
bellard 已提交
6241 6242 6243 6244 6245 6246 6247 6248
        if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
            gen_repz_lods(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
        } else {
            gen_lods(s, ot);
        }
        break;
    case 0xae: /* scasS */
    case 0xaf:
6249
        ot = mo_b_d(b, dflag);
B
bellard 已提交
6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260
        if (prefixes & PREFIX_REPNZ) {
            gen_repz_scas(s, ot, pc_start - s->cs_base, s->pc - s->cs_base, 1);
        } else if (prefixes & PREFIX_REPZ) {
            gen_repz_scas(s, ot, pc_start - s->cs_base, s->pc - s->cs_base, 0);
        } else {
            gen_scas(s, ot);
        }
        break;

    case 0xa6: /* cmpsS */
    case 0xa7:
6261
        ot = mo_b_d(b, dflag);
B
bellard 已提交
6262 6263 6264 6265 6266 6267 6268 6269 6270 6271
        if (prefixes & PREFIX_REPNZ) {
            gen_repz_cmps(s, ot, pc_start - s->cs_base, s->pc - s->cs_base, 1);
        } else if (prefixes & PREFIX_REPZ) {
            gen_repz_cmps(s, ot, pc_start - s->cs_base, s->pc - s->cs_base, 0);
        } else {
            gen_cmps(s, ot);
        }
        break;
    case 0x6c: /* insS */
    case 0x6d:
6272
        ot = mo_b_d32(b, dflag);
6273
        tcg_gen_ext16u_tl(cpu_T[0], cpu_regs[R_EDX]);
6274 6275
        gen_check_io(s, ot, pc_start - s->cs_base, 
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes) | 4);
6276 6277
        if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
            gen_repz_ins(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
B
bellard 已提交
6278
        } else {
6279
            gen_ins(s, ot);
P
pbrook 已提交
6280 6281 6282
            if (use_icount) {
                gen_jmp(s, s->pc - s->cs_base);
            }
B
bellard 已提交
6283 6284 6285 6286
        }
        break;
    case 0x6e: /* outsS */
    case 0x6f:
6287
        ot = mo_b_d32(b, dflag);
6288
        tcg_gen_ext16u_tl(cpu_T[0], cpu_regs[R_EDX]);
6289 6290
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes) | 4);
6291 6292
        if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
            gen_repz_outs(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
B
bellard 已提交
6293
        } else {
6294
            gen_outs(s, ot);
P
pbrook 已提交
6295 6296 6297
            if (use_icount) {
                gen_jmp(s, s->pc - s->cs_base);
            }
B
bellard 已提交
6298 6299 6300 6301 6302
        }
        break;

        /************************/
        /* port I/O */
T
ths 已提交
6303

B
bellard 已提交
6304 6305
    case 0xe4:
    case 0xe5:
6306
        ot = mo_b_d32(b, dflag);
6307
        val = cpu_ldub_code(env, s->pc++);
6308
        tcg_gen_movi_tl(cpu_T[0], val);
6309 6310
        gen_check_io(s, ot, pc_start - s->cs_base,
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
P
pbrook 已提交
6311 6312
        if (use_icount)
            gen_io_start();
6313
        tcg_gen_movi_i32(cpu_tmp2_i32, val);
P
pbrook 已提交
6314
        gen_helper_in_func(ot, cpu_T[1], cpu_tmp2_i32);
6315
        gen_op_mov_reg_v(ot, R_EAX, cpu_T[1]);
P
pbrook 已提交
6316 6317 6318 6319
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6320 6321 6322
        break;
    case 0xe6:
    case 0xe7:
6323
        ot = mo_b_d32(b, dflag);
6324
        val = cpu_ldub_code(env, s->pc++);
6325
        tcg_gen_movi_tl(cpu_T[0], val);
6326 6327
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes));
6328
        gen_op_mov_v_reg(ot, cpu_T[1], R_EAX);
6329

P
pbrook 已提交
6330 6331
        if (use_icount)
            gen_io_start();
6332
        tcg_gen_movi_i32(cpu_tmp2_i32, val);
6333
        tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
P
pbrook 已提交
6334
        gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
P
pbrook 已提交
6335 6336 6337 6338
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6339 6340 6341
        break;
    case 0xec:
    case 0xed:
6342
        ot = mo_b_d32(b, dflag);
6343
        tcg_gen_ext16u_tl(cpu_T[0], cpu_regs[R_EDX]);
6344 6345
        gen_check_io(s, ot, pc_start - s->cs_base,
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
P
pbrook 已提交
6346 6347
        if (use_icount)
            gen_io_start();
6348
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
6349
        gen_helper_in_func(ot, cpu_T[1], cpu_tmp2_i32);
6350
        gen_op_mov_reg_v(ot, R_EAX, cpu_T[1]);
P
pbrook 已提交
6351 6352 6353 6354
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6355 6356 6357
        break;
    case 0xee:
    case 0xef:
6358
        ot = mo_b_d32(b, dflag);
6359
        tcg_gen_ext16u_tl(cpu_T[0], cpu_regs[R_EDX]);
6360 6361
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes));
6362
        gen_op_mov_v_reg(ot, cpu_T[1], R_EAX);
6363

P
pbrook 已提交
6364 6365
        if (use_icount)
            gen_io_start();
6366 6367
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
        tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
P
pbrook 已提交
6368
        gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
P
pbrook 已提交
6369 6370 6371 6372
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6373 6374 6375 6376 6377
        break;

        /************************/
        /* control */
    case 0xc2: /* ret im */
6378
        val = cpu_ldsw_code(env, s->pc);
B
bellard 已提交
6379
        s->pc += 2;
6380 6381 6382
        ot = gen_pop_T0(s);
        gen_stack_update(s, val + (1 << ot));
        /* Note that gen_pop_T0 uses a zero-extending load.  */
6383
        gen_op_jmp_v(cpu_T[0]);
B
bellard 已提交
6384 6385 6386
        gen_eob(s);
        break;
    case 0xc3: /* ret */
6387 6388 6389
        ot = gen_pop_T0(s);
        gen_pop_update(s, ot);
        /* Note that gen_pop_T0 uses a zero-extending load.  */
6390
        gen_op_jmp_v(cpu_T[0]);
B
bellard 已提交
6391 6392 6393
        gen_eob(s);
        break;
    case 0xca: /* lret im */
6394
        val = cpu_ldsw_code(env, s->pc);
B
bellard 已提交
6395 6396 6397
        s->pc += 2;
    do_lret:
        if (s->pe && !s->vm86) {
6398
            gen_update_cc_op(s);
B
bellard 已提交
6399
            gen_jmp_im(pc_start - s->cs_base);
6400
            gen_helper_lret_protected(cpu_env, tcg_const_i32(dflag - 1),
P
pbrook 已提交
6401
                                      tcg_const_i32(val));
B
bellard 已提交
6402 6403 6404
        } else {
            gen_stack_A0(s);
            /* pop offset */
6405
            gen_op_ld_v(s, dflag, cpu_T[0], cpu_A0);
B
bellard 已提交
6406 6407
            /* NOTE: keeping EIP updated is not a problem in case of
               exception */
6408
            gen_op_jmp_v(cpu_T[0]);
B
bellard 已提交
6409
            /* pop selector */
6410 6411
            gen_op_addl_A0_im(1 << dflag);
            gen_op_ld_v(s, dflag, cpu_T[0], cpu_A0);
6412
            gen_op_movl_seg_T0_vm(R_CS);
B
bellard 已提交
6413
            /* add stack offset */
6414
            gen_stack_update(s, val + (2 << dflag));
B
bellard 已提交
6415 6416 6417 6418 6419 6420 6421
        }
        gen_eob(s);
        break;
    case 0xcb: /* lret */
        val = 0;
        goto do_lret;
    case 0xcf: /* iret */
B
bellard 已提交
6422
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_IRET);
B
bellard 已提交
6423 6424
        if (!s->pe) {
            /* real mode */
6425
            gen_helper_iret_real(cpu_env, tcg_const_i32(dflag - 1));
6426
            set_cc_op(s, CC_OP_EFLAGS);
6427 6428 6429 6430
        } else if (s->vm86) {
            if (s->iopl != 3) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
6431
                gen_helper_iret_real(cpu_env, tcg_const_i32(dflag - 1));
6432
                set_cc_op(s, CC_OP_EFLAGS);
6433
            }
B
bellard 已提交
6434
        } else {
6435
            gen_update_cc_op(s);
B
bellard 已提交
6436
            gen_jmp_im(pc_start - s->cs_base);
6437
            gen_helper_iret_protected(cpu_env, tcg_const_i32(dflag - 1),
P
pbrook 已提交
6438
                                      tcg_const_i32(s->pc - s->cs_base));
6439
            set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6440 6441 6442 6443 6444
        }
        gen_eob(s);
        break;
    case 0xe8: /* call im */
        {
6445
            if (dflag != MO_16) {
6446
                tval = (int32_t)insn_get(env, s, MO_32);
6447
            } else {
6448
                tval = (int16_t)insn_get(env, s, MO_16);
6449
            }
B
bellard 已提交
6450
            next_eip = s->pc - s->cs_base;
B
bellard 已提交
6451
            tval += next_eip;
6452
            if (dflag == MO_16) {
B
bellard 已提交
6453
                tval &= 0xffff;
6454
            } else if (!CODE64(s)) {
6455
                tval &= 0xffffffff;
6456
            }
6457
            tcg_gen_movi_tl(cpu_T[0], next_eip);
6458
            gen_push_v(s, cpu_T[0]);
B
bellard 已提交
6459
            gen_jmp(s, tval);
B
bellard 已提交
6460 6461 6462 6463 6464
        }
        break;
    case 0x9a: /* lcall im */
        {
            unsigned int selector, offset;
6465

B
bellard 已提交
6466 6467
            if (CODE64(s))
                goto illegal_op;
6468
            ot = dflag;
6469
            offset = insn_get(env, s, ot);
6470
            selector = insn_get(env, s, MO_16);
6471

6472
            tcg_gen_movi_tl(cpu_T[0], selector);
6473
            tcg_gen_movi_tl(cpu_T[1], offset);
B
bellard 已提交
6474 6475
        }
        goto do_lcall;
B
bellard 已提交
6476
    case 0xe9: /* jmp im */
6477
        if (dflag != MO_16) {
6478
            tval = (int32_t)insn_get(env, s, MO_32);
6479
        } else {
6480
            tval = (int16_t)insn_get(env, s, MO_16);
6481
        }
B
bellard 已提交
6482
        tval += s->pc - s->cs_base;
6483
        if (dflag == MO_16) {
B
bellard 已提交
6484
            tval &= 0xffff;
6485
        } else if (!CODE64(s)) {
6486
            tval &= 0xffffffff;
6487
        }
B
bellard 已提交
6488
        gen_jmp(s, tval);
B
bellard 已提交
6489 6490 6491 6492 6493
        break;
    case 0xea: /* ljmp im */
        {
            unsigned int selector, offset;

B
bellard 已提交
6494 6495
            if (CODE64(s))
                goto illegal_op;
6496
            ot = dflag;
6497
            offset = insn_get(env, s, ot);
6498
            selector = insn_get(env, s, MO_16);
6499

6500
            tcg_gen_movi_tl(cpu_T[0], selector);
6501
            tcg_gen_movi_tl(cpu_T[1], offset);
B
bellard 已提交
6502 6503 6504
        }
        goto do_ljmp;
    case 0xeb: /* jmp Jb */
6505
        tval = (int8_t)insn_get(env, s, MO_8);
B
bellard 已提交
6506
        tval += s->pc - s->cs_base;
6507
        if (dflag == MO_16) {
B
bellard 已提交
6508
            tval &= 0xffff;
6509
        }
B
bellard 已提交
6510
        gen_jmp(s, tval);
B
bellard 已提交
6511 6512
        break;
    case 0x70 ... 0x7f: /* jcc Jb */
6513
        tval = (int8_t)insn_get(env, s, MO_8);
B
bellard 已提交
6514 6515
        goto do_jcc;
    case 0x180 ... 0x18f: /* jcc Jv */
6516
        if (dflag != MO_16) {
6517
            tval = (int32_t)insn_get(env, s, MO_32);
B
bellard 已提交
6518
        } else {
6519
            tval = (int16_t)insn_get(env, s, MO_16);
B
bellard 已提交
6520 6521 6522
        }
    do_jcc:
        next_eip = s->pc - s->cs_base;
B
bellard 已提交
6523
        tval += next_eip;
6524
        if (dflag == MO_16) {
B
bellard 已提交
6525
            tval &= 0xffff;
6526
        }
B
bellard 已提交
6527
        gen_jcc(s, b, tval, next_eip);
B
bellard 已提交
6528 6529 6530
        break;

    case 0x190 ... 0x19f: /* setcc Gv */
6531
        modrm = cpu_ldub_code(env, s->pc++);
6532
        gen_setcc1(s, b, cpu_T[0]);
6533
        gen_ldst_modrm(env, s, modrm, MO_8, OR_TMP0, 1);
B
bellard 已提交
6534 6535
        break;
    case 0x140 ... 0x14f: /* cmov Gv, Ev */
6536 6537 6538
        if (!(s->cpuid_features & CPUID_CMOV)) {
            goto illegal_op;
        }
6539
        ot = dflag;
6540 6541 6542
        modrm = cpu_ldub_code(env, s->pc++);
        reg = ((modrm >> 3) & 7) | rex_r;
        gen_cmovcc1(env, s, ot, b, modrm, reg);
B
bellard 已提交
6543
        break;
6544

B
bellard 已提交
6545 6546 6547
        /************************/
        /* flags */
    case 0x9c: /* pushf */
B
bellard 已提交
6548
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_PUSHF);
B
bellard 已提交
6549 6550 6551
        if (s->vm86 && s->iopl != 3) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
6552
            gen_update_cc_op(s);
6553
            gen_helper_read_eflags(cpu_T[0], cpu_env);
6554
            gen_push_v(s, cpu_T[0]);
B
bellard 已提交
6555 6556 6557
        }
        break;
    case 0x9d: /* popf */
B
bellard 已提交
6558
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_POPF);
B
bellard 已提交
6559 6560 6561
        if (s->vm86 && s->iopl != 3) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
6562
            ot = gen_pop_T0(s);
B
bellard 已提交
6563
            if (s->cpl == 0) {
6564
                if (dflag != MO_16) {
6565 6566 6567 6568 6569
                    gen_helper_write_eflags(cpu_env, cpu_T[0],
                                            tcg_const_i32((TF_MASK | AC_MASK |
                                                           ID_MASK | NT_MASK |
                                                           IF_MASK |
                                                           IOPL_MASK)));
B
bellard 已提交
6570
                } else {
6571 6572 6573 6574 6575
                    gen_helper_write_eflags(cpu_env, cpu_T[0],
                                            tcg_const_i32((TF_MASK | AC_MASK |
                                                           ID_MASK | NT_MASK |
                                                           IF_MASK | IOPL_MASK)
                                                          & 0xffff));
B
bellard 已提交
6576 6577
                }
            } else {
B
bellard 已提交
6578
                if (s->cpl <= s->iopl) {
6579
                    if (dflag != MO_16) {
6580 6581 6582 6583 6584 6585
                        gen_helper_write_eflags(cpu_env, cpu_T[0],
                                                tcg_const_i32((TF_MASK |
                                                               AC_MASK |
                                                               ID_MASK |
                                                               NT_MASK |
                                                               IF_MASK)));
B
bellard 已提交
6586
                    } else {
6587 6588 6589 6590 6591 6592 6593
                        gen_helper_write_eflags(cpu_env, cpu_T[0],
                                                tcg_const_i32((TF_MASK |
                                                               AC_MASK |
                                                               ID_MASK |
                                                               NT_MASK |
                                                               IF_MASK)
                                                              & 0xffff));
B
bellard 已提交
6594
                    }
B
bellard 已提交
6595
                } else {
6596
                    if (dflag != MO_16) {
6597 6598 6599
                        gen_helper_write_eflags(cpu_env, cpu_T[0],
                                           tcg_const_i32((TF_MASK | AC_MASK |
                                                          ID_MASK | NT_MASK)));
B
bellard 已提交
6600
                    } else {
6601 6602 6603 6604
                        gen_helper_write_eflags(cpu_env, cpu_T[0],
                                           tcg_const_i32((TF_MASK | AC_MASK |
                                                          ID_MASK | NT_MASK)
                                                         & 0xffff));
B
bellard 已提交
6605
                    }
B
bellard 已提交
6606 6607
                }
            }
6608
            gen_pop_update(s, ot);
6609
            set_cc_op(s, CC_OP_EFLAGS);
H
H. Peter Anvin 已提交
6610
            /* abort translation because TF/AC flag may change */
B
bellard 已提交
6611
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
6612 6613 6614 6615
            gen_eob(s);
        }
        break;
    case 0x9e: /* sahf */
B
bellard 已提交
6616
        if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM))
B
bellard 已提交
6617
            goto illegal_op;
6618
        gen_op_mov_v_reg(MO_8, cpu_T[0], R_AH);
6619
        gen_compute_eflags(s);
6620 6621 6622
        tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, CC_O);
        tcg_gen_andi_tl(cpu_T[0], cpu_T[0], CC_S | CC_Z | CC_A | CC_P | CC_C);
        tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, cpu_T[0]);
B
bellard 已提交
6623 6624
        break;
    case 0x9f: /* lahf */
B
bellard 已提交
6625
        if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM))
B
bellard 已提交
6626
            goto illegal_op;
6627
        gen_compute_eflags(s);
6628
        /* Note: gen_compute_eflags() only gives the condition codes */
6629
        tcg_gen_ori_tl(cpu_T[0], cpu_cc_src, 0x02);
6630
        gen_op_mov_reg_v(MO_8, R_AH, cpu_T[0]);
B
bellard 已提交
6631 6632
        break;
    case 0xf5: /* cmc */
6633
        gen_compute_eflags(s);
6634
        tcg_gen_xori_tl(cpu_cc_src, cpu_cc_src, CC_C);
B
bellard 已提交
6635 6636
        break;
    case 0xf8: /* clc */
6637
        gen_compute_eflags(s);
6638
        tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~CC_C);
B
bellard 已提交
6639 6640
        break;
    case 0xf9: /* stc */
6641
        gen_compute_eflags(s);
6642
        tcg_gen_ori_tl(cpu_cc_src, cpu_cc_src, CC_C);
B
bellard 已提交
6643 6644
        break;
    case 0xfc: /* cld */
6645
        tcg_gen_movi_i32(cpu_tmp2_i32, 1);
6646
        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, offsetof(CPUX86State, df));
B
bellard 已提交
6647 6648
        break;
    case 0xfd: /* std */
6649
        tcg_gen_movi_i32(cpu_tmp2_i32, -1);
6650
        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, offsetof(CPUX86State, df));
B
bellard 已提交
6651 6652 6653 6654 6655
        break;

        /************************/
        /* bit operations */
    case 0x1ba: /* bt/bts/btr/btc Gv, im */
6656
        ot = dflag;
6657
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
6658
        op = (modrm >> 3) & 7;
B
bellard 已提交
6659
        mod = (modrm >> 6) & 3;
B
bellard 已提交
6660
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
6661
        if (mod != 3) {
B
bellard 已提交
6662
            s->rip_offset = 1;
6663
            gen_lea_modrm(env, s, modrm);
6664
            gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
6665
        } else {
6666
            gen_op_mov_v_reg(ot, cpu_T[0], rm);
B
bellard 已提交
6667 6668
        }
        /* load shift */
6669
        val = cpu_ldub_code(env, s->pc++);
6670
        tcg_gen_movi_tl(cpu_T[1], val);
B
bellard 已提交
6671 6672 6673
        if (op < 4)
            goto illegal_op;
        op -= 4;
B
bellard 已提交
6674
        goto bt_op;
B
bellard 已提交
6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686
    case 0x1a3: /* bt Gv, Ev */
        op = 0;
        goto do_btx;
    case 0x1ab: /* bts */
        op = 1;
        goto do_btx;
    case 0x1b3: /* btr */
        op = 2;
        goto do_btx;
    case 0x1bb: /* btc */
        op = 3;
    do_btx:
6687
        ot = dflag;
6688
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
6689
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
6690
        mod = (modrm >> 6) & 3;
B
bellard 已提交
6691
        rm = (modrm & 7) | REX_B(s);
6692
        gen_op_mov_v_reg(MO_32, cpu_T[1], reg);
B
bellard 已提交
6693
        if (mod != 3) {
6694
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
6695
            /* specific case: we need to add a displacement */
B
bellard 已提交
6696 6697 6698 6699
            gen_exts(ot, cpu_T[1]);
            tcg_gen_sari_tl(cpu_tmp0, cpu_T[1], 3 + ot);
            tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, ot);
            tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
6700
            gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
6701
        } else {
6702
            gen_op_mov_v_reg(ot, cpu_T[0], rm);
B
bellard 已提交
6703
        }
B
bellard 已提交
6704 6705
    bt_op:
        tcg_gen_andi_tl(cpu_T[1], cpu_T[1], (1 << (3 + ot)) - 1);
6706
        tcg_gen_shr_tl(cpu_tmp4, cpu_T[0], cpu_T[1]);
B
bellard 已提交
6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717
        switch(op) {
        case 0:
            break;
        case 1:
            tcg_gen_movi_tl(cpu_tmp0, 1);
            tcg_gen_shl_tl(cpu_tmp0, cpu_tmp0, cpu_T[1]);
            tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
            break;
        case 2:
            tcg_gen_movi_tl(cpu_tmp0, 1);
            tcg_gen_shl_tl(cpu_tmp0, cpu_tmp0, cpu_T[1]);
6718
            tcg_gen_andc_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
B
bellard 已提交
6719 6720 6721 6722 6723 6724 6725 6726
            break;
        default:
        case 3:
            tcg_gen_movi_tl(cpu_tmp0, 1);
            tcg_gen_shl_tl(cpu_tmp0, cpu_tmp0, cpu_T[1]);
            tcg_gen_xor_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
            break;
        }
B
bellard 已提交
6727
        if (op != 0) {
6728 6729 6730
            if (mod != 3) {
                gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
            } else {
6731
                gen_op_mov_reg_v(ot, rm, cpu_T[0]);
6732
            }
6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753
        }

        /* Delay all CC updates until after the store above.  Note that
           C is the result of the test, Z is unchanged, and the others
           are all undefined.  */
        switch (s->cc_op) {
        case CC_OP_MULB ... CC_OP_MULQ:
        case CC_OP_ADDB ... CC_OP_ADDQ:
        case CC_OP_ADCB ... CC_OP_ADCQ:
        case CC_OP_SUBB ... CC_OP_SUBQ:
        case CC_OP_SBBB ... CC_OP_SBBQ:
        case CC_OP_LOGICB ... CC_OP_LOGICQ:
        case CC_OP_INCB ... CC_OP_INCQ:
        case CC_OP_DECB ... CC_OP_DECQ:
        case CC_OP_SHLB ... CC_OP_SHLQ:
        case CC_OP_SARB ... CC_OP_SARQ:
        case CC_OP_BMILGB ... CC_OP_BMILGQ:
            /* Z was going to be computed from the non-zero status of CC_DST.
               We can get that same Z value (and the new C value) by leaving
               CC_DST alone, setting CC_SRC, and using a CC_OP_SAR of the
               same width.  */
B
bellard 已提交
6754
            tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
6755 6756 6757 6758 6759 6760 6761 6762
            set_cc_op(s, ((s->cc_op - CC_OP_MULB) & 3) + CC_OP_SARB);
            break;
        default:
            /* Otherwise, generate EFLAGS and replace the C bit.  */
            gen_compute_eflags(s);
            tcg_gen_deposit_tl(cpu_cc_src, cpu_cc_src, cpu_tmp4,
                               ctz32(CC_C), 1);
            break;
B
bellard 已提交
6763 6764
        }
        break;
6765 6766
    case 0x1bc: /* bsf / tzcnt */
    case 0x1bd: /* bsr / lzcnt */
6767
        ot = dflag;
6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784
        modrm = cpu_ldub_code(env, s->pc++);
        reg = ((modrm >> 3) & 7) | rex_r;
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
        gen_extu(ot, cpu_T[0]);

        /* Note that lzcnt and tzcnt are in different extensions.  */
        if ((prefixes & PREFIX_REPZ)
            && (b & 1
                ? s->cpuid_ext3_features & CPUID_EXT3_ABM
                : s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI1)) {
            int size = 8 << ot;
            tcg_gen_mov_tl(cpu_cc_src, cpu_T[0]);
            if (b & 1) {
                /* For lzcnt, reduce the target_ulong result by the
                   number of zeros that we expect to find at the top.  */
                gen_helper_clz(cpu_T[0], cpu_T[0]);
                tcg_gen_subi_tl(cpu_T[0], cpu_T[0], TARGET_LONG_BITS - size);
B
bellard 已提交
6785
            } else {
6786 6787 6788 6789 6790
                /* For tzcnt, a zero input must return the operand size:
                   force all bits outside the operand size to 1.  */
                target_ulong mask = (target_ulong)-2 << (size - 1);
                tcg_gen_ori_tl(cpu_T[0], cpu_T[0], mask);
                gen_helper_ctz(cpu_T[0], cpu_T[0]);
B
bellard 已提交
6791
            }
6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814
            /* For lzcnt/tzcnt, C and Z bits are defined and are
               related to the result.  */
            gen_op_update1_cc();
            set_cc_op(s, CC_OP_BMILGB + ot);
        } else {
            /* For bsr/bsf, only the Z bit is defined and it is related
               to the input and not the result.  */
            tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
            set_cc_op(s, CC_OP_LOGICB + ot);
            if (b & 1) {
                /* For bsr, return the bit index of the first 1 bit,
                   not the count of leading zeros.  */
                gen_helper_clz(cpu_T[0], cpu_T[0]);
                tcg_gen_xori_tl(cpu_T[0], cpu_T[0], TARGET_LONG_BITS - 1);
            } else {
                gen_helper_ctz(cpu_T[0], cpu_T[0]);
            }
            /* ??? The manual says that the output is undefined when the
               input is zero, but real hardware leaves it unchanged, and
               real programs appear to depend on that.  */
            tcg_gen_movi_tl(cpu_tmp0, 0);
            tcg_gen_movcond_tl(TCG_COND_EQ, cpu_T[0], cpu_cc_dst, cpu_tmp0,
                               cpu_regs[reg], cpu_T[0]);
B
bellard 已提交
6815
        }
6816
        gen_op_mov_reg_v(ot, reg, cpu_T[0]);
B
bellard 已提交
6817 6818 6819 6820
        break;
        /************************/
        /* bcd */
    case 0x27: /* daa */
B
bellard 已提交
6821 6822
        if (CODE64(s))
            goto illegal_op;
6823
        gen_update_cc_op(s);
6824
        gen_helper_daa(cpu_env);
6825
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6826 6827
        break;
    case 0x2f: /* das */
B
bellard 已提交
6828 6829
        if (CODE64(s))
            goto illegal_op;
6830
        gen_update_cc_op(s);
6831
        gen_helper_das(cpu_env);
6832
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6833 6834
        break;
    case 0x37: /* aaa */
B
bellard 已提交
6835 6836
        if (CODE64(s))
            goto illegal_op;
6837
        gen_update_cc_op(s);
6838
        gen_helper_aaa(cpu_env);
6839
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6840 6841
        break;
    case 0x3f: /* aas */
B
bellard 已提交
6842 6843
        if (CODE64(s))
            goto illegal_op;
6844
        gen_update_cc_op(s);
6845
        gen_helper_aas(cpu_env);
6846
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6847 6848
        break;
    case 0xd4: /* aam */
B
bellard 已提交
6849 6850
        if (CODE64(s))
            goto illegal_op;
6851
        val = cpu_ldub_code(env, s->pc++);
6852 6853 6854
        if (val == 0) {
            gen_exception(s, EXCP00_DIVZ, pc_start - s->cs_base);
        } else {
6855
            gen_helper_aam(cpu_env, tcg_const_i32(val));
6856
            set_cc_op(s, CC_OP_LOGICB);
6857
        }
B
bellard 已提交
6858 6859
        break;
    case 0xd5: /* aad */
B
bellard 已提交
6860 6861
        if (CODE64(s))
            goto illegal_op;
6862
        val = cpu_ldub_code(env, s->pc++);
6863
        gen_helper_aad(cpu_env, tcg_const_i32(val));
6864
        set_cc_op(s, CC_OP_LOGICB);
B
bellard 已提交
6865 6866 6867 6868
        break;
        /************************/
        /* misc */
    case 0x90: /* nop */
6869
        /* XXX: correct lock test for all insn */
R
Richard Henderson 已提交
6870
        if (prefixes & PREFIX_LOCK) {
6871
            goto illegal_op;
R
Richard Henderson 已提交
6872 6873 6874 6875 6876
        }
        /* If REX_B is set, then this is xchg eax, r8d, not a nop.  */
        if (REX_B(s)) {
            goto do_xchg_reg_eax;
        }
T
ths 已提交
6877
        if (prefixes & PREFIX_REPZ) {
6878 6879 6880 6881
            gen_update_cc_op(s);
            gen_jmp_im(pc_start - s->cs_base);
            gen_helper_pause(cpu_env, tcg_const_i32(s->pc - pc_start));
            s->is_jmp = DISAS_TB_JUMP;
T
ths 已提交
6882
        }
B
bellard 已提交
6883 6884
        break;
    case 0x9b: /* fwait */
6885
        if ((s->flags & (HF_MP_MASK | HF_TS_MASK)) ==
B
bellard 已提交
6886 6887
            (HF_MP_MASK | HF_TS_MASK)) {
            gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
B
bellard 已提交
6888
        } else {
6889
            gen_update_cc_op(s);
B
bellard 已提交
6890
            gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6891
            gen_helper_fwait(cpu_env);
B
bellard 已提交
6892
        }
B
bellard 已提交
6893 6894 6895 6896 6897
        break;
    case 0xcc: /* int3 */
        gen_interrupt(s, EXCP03_INT3, pc_start - s->cs_base, s->pc - s->cs_base);
        break;
    case 0xcd: /* int N */
6898
        val = cpu_ldub_code(env, s->pc++);
6899
        if (s->vm86 && s->iopl != 3) {
6900
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6901 6902 6903
        } else {
            gen_interrupt(s, val, pc_start - s->cs_base, s->pc - s->cs_base);
        }
B
bellard 已提交
6904 6905
        break;
    case 0xce: /* into */
B
bellard 已提交
6906 6907
        if (CODE64(s))
            goto illegal_op;
6908
        gen_update_cc_op(s);
6909
        gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6910
        gen_helper_into(cpu_env, tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
6911
        break;
A
aurel32 已提交
6912
#ifdef WANT_ICEBP
B
bellard 已提交
6913
    case 0xf1: /* icebp (undocumented, exits to external debugger) */
B
bellard 已提交
6914
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_ICEBP);
6915
#if 1
B
bellard 已提交
6916
        gen_debug(s, pc_start - s->cs_base);
6917 6918
#else
        /* start debug */
6919
        tb_flush(env);
6920
        qemu_set_log(CPU_LOG_INT | CPU_LOG_TB_IN_ASM);
6921
#endif
B
bellard 已提交
6922
        break;
A
aurel32 已提交
6923
#endif
B
bellard 已提交
6924 6925 6926
    case 0xfa: /* cli */
        if (!s->vm86) {
            if (s->cpl <= s->iopl) {
6927
                gen_helper_cli(cpu_env);
B
bellard 已提交
6928 6929 6930 6931 6932
            } else {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            }
        } else {
            if (s->iopl == 3) {
6933
                gen_helper_cli(cpu_env);
B
bellard 已提交
6934 6935 6936 6937 6938 6939 6940 6941 6942
            } else {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            }
        }
        break;
    case 0xfb: /* sti */
        if (!s->vm86) {
            if (s->cpl <= s->iopl) {
            gen_sti:
6943
                gen_helper_sti(cpu_env);
B
bellard 已提交
6944
                /* interruptions are enabled only the first insn after sti */
6945 6946 6947
                /* If several instructions disable interrupts, only the
                   _first_ does it */
                if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
6948
                    gen_helper_set_inhibit_irq(cpu_env);
B
bellard 已提交
6949
                /* give a chance to handle pending irqs */
B
bellard 已提交
6950
                gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963
                gen_eob(s);
            } else {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            }
        } else {
            if (s->iopl == 3) {
                goto gen_sti;
            } else {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            }
        }
        break;
    case 0x62: /* bound */
B
bellard 已提交
6964 6965
        if (CODE64(s))
            goto illegal_op;
6966
        ot = dflag;
6967
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
6968 6969 6970 6971
        reg = (modrm >> 3) & 7;
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
6972
        gen_op_mov_v_reg(ot, cpu_T[0], reg);
6973
        gen_lea_modrm(env, s, modrm);
B
bellard 已提交
6974
        gen_jmp_im(pc_start - s->cs_base);
6975
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
6976
        if (ot == MO_16) {
B
Blue Swirl 已提交
6977 6978 6979 6980
            gen_helper_boundw(cpu_env, cpu_A0, cpu_tmp2_i32);
        } else {
            gen_helper_boundl(cpu_env, cpu_A0, cpu_tmp2_i32);
        }
B
bellard 已提交
6981 6982
        break;
    case 0x1c8 ... 0x1cf: /* bswap reg */
B
bellard 已提交
6983 6984
        reg = (b & 7) | REX_B(s);
#ifdef TARGET_X86_64
6985
        if (dflag == MO_64) {
6986
            gen_op_mov_v_reg(MO_64, cpu_T[0], reg);
A
aurel32 已提交
6987
            tcg_gen_bswap64_i64(cpu_T[0], cpu_T[0]);
6988
            gen_op_mov_reg_v(MO_64, reg, cpu_T[0]);
6989
        } else
6990
#endif
B
bellard 已提交
6991
        {
6992
            gen_op_mov_v_reg(MO_32, cpu_T[0], reg);
6993 6994
            tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
            tcg_gen_bswap32_tl(cpu_T[0], cpu_T[0]);
6995
            gen_op_mov_reg_v(MO_32, reg, cpu_T[0]);
B
bellard 已提交
6996
        }
B
bellard 已提交
6997 6998
        break;
    case 0xd6: /* salc */
B
bellard 已提交
6999 7000
        if (CODE64(s))
            goto illegal_op;
7001
        gen_compute_eflags_c(s, cpu_T[0]);
7002
        tcg_gen_neg_tl(cpu_T[0], cpu_T[0]);
7003
        gen_op_mov_reg_v(MO_8, R_EAX, cpu_T[0]);
B
bellard 已提交
7004 7005 7006 7007 7008
        break;
    case 0xe0: /* loopnz */
    case 0xe1: /* loopz */
    case 0xe2: /* loop */
    case 0xe3: /* jecxz */
B
bellard 已提交
7009
        {
7010
            int l1, l2, l3;
B
bellard 已提交
7011

7012
            tval = (int8_t)insn_get(env, s, MO_8);
B
bellard 已提交
7013 7014
            next_eip = s->pc - s->cs_base;
            tval += next_eip;
7015
            if (dflag == MO_16) {
B
bellard 已提交
7016
                tval &= 0xffff;
7017
            }
7018

B
bellard 已提交
7019 7020
            l1 = gen_new_label();
            l2 = gen_new_label();
7021
            l3 = gen_new_label();
B
bellard 已提交
7022
            b &= 3;
7023 7024 7025
            switch(b) {
            case 0: /* loopnz */
            case 1: /* loopz */
7026 7027
                gen_op_add_reg_im(s->aflag, R_ECX, -1);
                gen_op_jz_ecx(s->aflag, l3);
7028
                gen_jcc1(s, (JCC_Z << 1) | (b ^ 1), l1);
7029 7030
                break;
            case 2: /* loop */
7031 7032
                gen_op_add_reg_im(s->aflag, R_ECX, -1);
                gen_op_jnz_ecx(s->aflag, l1);
7033 7034 7035
                break;
            default:
            case 3: /* jcxz */
7036
                gen_op_jz_ecx(s->aflag, l1);
7037
                break;
B
bellard 已提交
7038 7039
            }

7040
            gen_set_label(l3);
B
bellard 已提交
7041
            gen_jmp_im(next_eip);
7042
            tcg_gen_br(l2);
7043

B
bellard 已提交
7044 7045 7046 7047 7048
            gen_set_label(l1);
            gen_jmp_im(tval);
            gen_set_label(l2);
            gen_eob(s);
        }
B
bellard 已提交
7049 7050 7051 7052 7053 7054
        break;
    case 0x130: /* wrmsr */
    case 0x132: /* rdmsr */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
7055
            gen_update_cc_op(s);
B
bellard 已提交
7056
            gen_jmp_im(pc_start - s->cs_base);
T
ths 已提交
7057
            if (b & 2) {
B
Blue Swirl 已提交
7058
                gen_helper_rdmsr(cpu_env);
T
ths 已提交
7059
            } else {
B
Blue Swirl 已提交
7060
                gen_helper_wrmsr(cpu_env);
T
ths 已提交
7061
            }
B
bellard 已提交
7062 7063 7064
        }
        break;
    case 0x131: /* rdtsc */
7065
        gen_update_cc_op(s);
B
bellard 已提交
7066
        gen_jmp_im(pc_start - s->cs_base);
P
pbrook 已提交
7067 7068
        if (use_icount)
            gen_io_start();
B
Blue Swirl 已提交
7069
        gen_helper_rdtsc(cpu_env);
P
pbrook 已提交
7070 7071 7072 7073
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
7074
        break;
7075
    case 0x133: /* rdpmc */
7076
        gen_update_cc_op(s);
7077
        gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7078
        gen_helper_rdpmc(cpu_env);
7079
        break;
7080
    case 0x134: /* sysenter */
7081
        /* For Intel SYSENTER is valid on 64-bit */
7082
        if (CODE64(s) && env->cpuid_vendor1 != CPUID_VENDOR_INTEL_1)
B
bellard 已提交
7083
            goto illegal_op;
7084 7085 7086
        if (!s->pe) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
J
Jun Koi 已提交
7087
            gen_update_cc_op(s);
B
bellard 已提交
7088
            gen_jmp_im(pc_start - s->cs_base);
7089
            gen_helper_sysenter(cpu_env);
7090 7091 7092 7093
            gen_eob(s);
        }
        break;
    case 0x135: /* sysexit */
7094
        /* For Intel SYSEXIT is valid on 64-bit */
7095
        if (CODE64(s) && env->cpuid_vendor1 != CPUID_VENDOR_INTEL_1)
B
bellard 已提交
7096
            goto illegal_op;
7097 7098 7099
        if (!s->pe) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
J
Jun Koi 已提交
7100
            gen_update_cc_op(s);
B
bellard 已提交
7101
            gen_jmp_im(pc_start - s->cs_base);
7102
            gen_helper_sysexit(cpu_env, tcg_const_i32(dflag - 1));
7103 7104 7105
            gen_eob(s);
        }
        break;
B
bellard 已提交
7106 7107 7108
#ifdef TARGET_X86_64
    case 0x105: /* syscall */
        /* XXX: is it usable in real mode ? */
J
Jun Koi 已提交
7109
        gen_update_cc_op(s);
B
bellard 已提交
7110
        gen_jmp_im(pc_start - s->cs_base);
7111
        gen_helper_syscall(cpu_env, tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
7112 7113 7114 7115 7116 7117
        gen_eob(s);
        break;
    case 0x107: /* sysret */
        if (!s->pe) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
J
Jun Koi 已提交
7118
            gen_update_cc_op(s);
B
bellard 已提交
7119
            gen_jmp_im(pc_start - s->cs_base);
7120
            gen_helper_sysret(cpu_env, tcg_const_i32(dflag - 1));
7121
            /* condition codes are modified only in long mode */
7122 7123 7124
            if (s->lma) {
                set_cc_op(s, CC_OP_EFLAGS);
            }
B
bellard 已提交
7125 7126 7127 7128
            gen_eob(s);
        }
        break;
#endif
B
bellard 已提交
7129
    case 0x1a2: /* cpuid */
7130
        gen_update_cc_op(s);
B
bellard 已提交
7131
        gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7132
        gen_helper_cpuid(cpu_env);
B
bellard 已提交
7133 7134 7135 7136 7137
        break;
    case 0xf4: /* hlt */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
7138
            gen_update_cc_op(s);
7139
            gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7140
            gen_helper_hlt(cpu_env, tcg_const_i32(s->pc - pc_start));
J
Jun Koi 已提交
7141
            s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
7142 7143 7144
        }
        break;
    case 0x100:
7145
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7146 7147 7148 7149
        mod = (modrm >> 6) & 3;
        op = (modrm >> 3) & 7;
        switch(op) {
        case 0: /* sldt */
7150 7151
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
7152
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_LDTR_READ);
B
bellard 已提交
7153
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,ldt.selector));
7154
            ot = mod == 3 ? dflag : MO_16;
7155
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
7156 7157
            break;
        case 2: /* lldt */
7158 7159
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
7160 7161 7162
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
B
bellard 已提交
7163
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_LDTR_WRITE);
7164
                gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
B
bellard 已提交
7165
                gen_jmp_im(pc_start - s->cs_base);
7166
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
7167
                gen_helper_lldt(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
7168 7169 7170
            }
            break;
        case 1: /* str */
7171 7172
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
7173
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_TR_READ);
B
bellard 已提交
7174
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,tr.selector));
7175
            ot = mod == 3 ? dflag : MO_16;
7176
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
7177 7178
            break;
        case 3: /* ltr */
7179 7180
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
7181 7182 7183
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
B
bellard 已提交
7184
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_TR_WRITE);
7185
                gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
B
bellard 已提交
7186
                gen_jmp_im(pc_start - s->cs_base);
7187
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
7188
                gen_helper_ltr(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
7189 7190 7191 7192
            }
            break;
        case 4: /* verr */
        case 5: /* verw */
7193 7194
            if (!s->pe || s->vm86)
                goto illegal_op;
7195
            gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
7196
            gen_update_cc_op(s);
7197 7198 7199 7200 7201
            if (op == 4) {
                gen_helper_verr(cpu_env, cpu_T[0]);
            } else {
                gen_helper_verw(cpu_env, cpu_T[0]);
            }
7202
            set_cc_op(s, CC_OP_EFLAGS);
7203
            break;
B
bellard 已提交
7204 7205 7206 7207 7208
        default:
            goto illegal_op;
        }
        break;
    case 0x101:
7209
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7210 7211
        mod = (modrm >> 6) & 3;
        op = (modrm >> 3) & 7;
B
bellard 已提交
7212
        rm = modrm & 7;
B
bellard 已提交
7213 7214 7215 7216
        switch(op) {
        case 0: /* sgdt */
            if (mod == 3)
                goto illegal_op;
B
bellard 已提交
7217
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_GDTR_READ);
7218
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
7219
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, gdt.limit));
7220
            gen_op_st_v(s, MO_16, cpu_T[0], cpu_A0);
7221
            gen_add_A0_im(s, 2);
B
bellard 已提交
7222
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, gdt.base));
7223
            if (dflag == MO_16) {
7224 7225
                tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xffffff);
            }
7226
            gen_op_st_v(s, CODE64(s) + MO_32, cpu_T[0], cpu_A0);
B
bellard 已提交
7227
            break;
B
bellard 已提交
7228 7229 7230 7231 7232 7233 7234
        case 1:
            if (mod == 3) {
                switch (rm) {
                case 0: /* monitor */
                    if (!(s->cpuid_ext_features & CPUID_EXT_MONITOR) ||
                        s->cpl != 0)
                        goto illegal_op;
7235
                    gen_update_cc_op(s);
B
bellard 已提交
7236
                    gen_jmp_im(pc_start - s->cs_base);
7237 7238
                    tcg_gen_mov_tl(cpu_A0, cpu_regs[R_EAX]);
                    gen_extu(s->aflag, cpu_A0);
B
bellard 已提交
7239
                    gen_add_A0_ds_seg(s);
B
Blue Swirl 已提交
7240
                    gen_helper_monitor(cpu_env, cpu_A0);
B
bellard 已提交
7241 7242 7243 7244 7245
                    break;
                case 1: /* mwait */
                    if (!(s->cpuid_ext_features & CPUID_EXT_MONITOR) ||
                        s->cpl != 0)
                        goto illegal_op;
J
Jun Koi 已提交
7246
                    gen_update_cc_op(s);
7247
                    gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7248
                    gen_helper_mwait(cpu_env, tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
7249 7250
                    gen_eob(s);
                    break;
H
H. Peter Anvin 已提交
7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268
                case 2: /* clac */
                    if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_SMAP) ||
                        s->cpl != 0) {
                        goto illegal_op;
                    }
                    gen_helper_clac(cpu_env);
                    gen_jmp_im(s->pc - s->cs_base);
                    gen_eob(s);
                    break;
                case 3: /* stac */
                    if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_SMAP) ||
                        s->cpl != 0) {
                        goto illegal_op;
                    }
                    gen_helper_stac(cpu_env);
                    gen_jmp_im(s->pc - s->cs_base);
                    gen_eob(s);
                    break;
B
bellard 已提交
7269 7270 7271 7272
                default:
                    goto illegal_op;
                }
            } else { /* sidt */
B
bellard 已提交
7273
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_IDTR_READ);
7274
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
7275
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, idt.limit));
7276
                gen_op_st_v(s, MO_16, cpu_T[0], cpu_A0);
B
bellard 已提交
7277
                gen_add_A0_im(s, 2);
B
bellard 已提交
7278
                tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, idt.base));
7279
                if (dflag == MO_16) {
7280 7281
                    tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xffffff);
                }
7282
                gen_op_st_v(s, CODE64(s) + MO_32, cpu_T[0], cpu_A0);
B
bellard 已提交
7283 7284
            }
            break;
B
bellard 已提交
7285 7286
        case 2: /* lgdt */
        case 3: /* lidt */
T
ths 已提交
7287
            if (mod == 3) {
7288
                gen_update_cc_op(s);
B
bellard 已提交
7289
                gen_jmp_im(pc_start - s->cs_base);
T
ths 已提交
7290 7291
                switch(rm) {
                case 0: /* VMRUN */
B
bellard 已提交
7292 7293 7294 7295
                    if (!(s->flags & HF_SVME_MASK) || !s->pe)
                        goto illegal_op;
                    if (s->cpl != 0) {
                        gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
T
ths 已提交
7296
                        break;
B
bellard 已提交
7297
                    } else {
7298
                        gen_helper_vmrun(cpu_env, tcg_const_i32(s->aflag - 1),
P
pbrook 已提交
7299
                                         tcg_const_i32(s->pc - pc_start));
7300
                        tcg_gen_exit_tb(0);
J
Jun Koi 已提交
7301
                        s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
7302
                    }
T
ths 已提交
7303 7304
                    break;
                case 1: /* VMMCALL */
B
bellard 已提交
7305 7306
                    if (!(s->flags & HF_SVME_MASK))
                        goto illegal_op;
B
Blue Swirl 已提交
7307
                    gen_helper_vmmcall(cpu_env);
T
ths 已提交
7308 7309
                    break;
                case 2: /* VMLOAD */
B
bellard 已提交
7310 7311 7312 7313 7314 7315
                    if (!(s->flags & HF_SVME_MASK) || !s->pe)
                        goto illegal_op;
                    if (s->cpl != 0) {
                        gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
                        break;
                    } else {
7316
                        gen_helper_vmload(cpu_env, tcg_const_i32(s->aflag - 1));
B
bellard 已提交
7317
                    }
T
ths 已提交
7318 7319
                    break;
                case 3: /* VMSAVE */
B
bellard 已提交
7320 7321 7322 7323 7324 7325
                    if (!(s->flags & HF_SVME_MASK) || !s->pe)
                        goto illegal_op;
                    if (s->cpl != 0) {
                        gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
                        break;
                    } else {
7326
                        gen_helper_vmsave(cpu_env, tcg_const_i32(s->aflag - 1));
B
bellard 已提交
7327
                    }
T
ths 已提交
7328 7329
                    break;
                case 4: /* STGI */
B
bellard 已提交
7330 7331 7332 7333 7334 7335 7336 7337
                    if ((!(s->flags & HF_SVME_MASK) &&
                         !(s->cpuid_ext3_features & CPUID_EXT3_SKINIT)) || 
                        !s->pe)
                        goto illegal_op;
                    if (s->cpl != 0) {
                        gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
                        break;
                    } else {
B
Blue Swirl 已提交
7338
                        gen_helper_stgi(cpu_env);
B
bellard 已提交
7339
                    }
T
ths 已提交
7340 7341
                    break;
                case 5: /* CLGI */
B
bellard 已提交
7342 7343 7344 7345 7346 7347
                    if (!(s->flags & HF_SVME_MASK) || !s->pe)
                        goto illegal_op;
                    if (s->cpl != 0) {
                        gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
                        break;
                    } else {
B
Blue Swirl 已提交
7348
                        gen_helper_clgi(cpu_env);
B
bellard 已提交
7349
                    }
T
ths 已提交
7350 7351
                    break;
                case 6: /* SKINIT */
B
bellard 已提交
7352 7353 7354 7355
                    if ((!(s->flags & HF_SVME_MASK) && 
                         !(s->cpuid_ext3_features & CPUID_EXT3_SKINIT)) || 
                        !s->pe)
                        goto illegal_op;
B
Blue Swirl 已提交
7356
                    gen_helper_skinit(cpu_env);
T
ths 已提交
7357 7358
                    break;
                case 7: /* INVLPGA */
B
bellard 已提交
7359 7360 7361 7362 7363 7364
                    if (!(s->flags & HF_SVME_MASK) || !s->pe)
                        goto illegal_op;
                    if (s->cpl != 0) {
                        gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
                        break;
                    } else {
7365 7366
                        gen_helper_invlpga(cpu_env,
                                           tcg_const_i32(s->aflag - 1));
B
bellard 已提交
7367
                    }
T
ths 已提交
7368 7369 7370 7371 7372
                    break;
                default:
                    goto illegal_op;
                }
            } else if (s->cpl != 0) {
B
bellard 已提交
7373 7374
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
B
bellard 已提交
7375 7376
                gen_svm_check_intercept(s, pc_start,
                                        op==2 ? SVM_EXIT_GDTR_WRITE : SVM_EXIT_IDTR_WRITE);
7377
                gen_lea_modrm(env, s, modrm);
7378
                gen_op_ld_v(s, MO_16, cpu_T[1], cpu_A0);
7379
                gen_add_A0_im(s, 2);
7380
                gen_op_ld_v(s, CODE64(s) + MO_32, cpu_T[0], cpu_A0);
7381
                if (dflag == MO_16) {
7382 7383
                    tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xffffff);
                }
B
bellard 已提交
7384
                if (op == 2) {
B
bellard 已提交
7385 7386
                    tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,gdt.base));
                    tcg_gen_st32_tl(cpu_T[1], cpu_env, offsetof(CPUX86State,gdt.limit));
B
bellard 已提交
7387
                } else {
B
bellard 已提交
7388 7389
                    tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,idt.base));
                    tcg_gen_st32_tl(cpu_T[1], cpu_env, offsetof(CPUX86State,idt.limit));
B
bellard 已提交
7390 7391 7392 7393
                }
            }
            break;
        case 4: /* smsw */
B
bellard 已提交
7394
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_READ_CR0);
7395
#if defined TARGET_X86_64 && defined HOST_WORDS_BIGENDIAN
7396 7397
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,cr[0]) + 4);
#else
B
bellard 已提交
7398
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,cr[0]));
7399
#endif
7400
            gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 1);
B
bellard 已提交
7401 7402 7403 7404 7405
            break;
        case 6: /* lmsw */
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
B
bellard 已提交
7406
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_CR0);
7407
                gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
B
Blue Swirl 已提交
7408
                gen_helper_lmsw(cpu_env, cpu_T[0]);
B
bellard 已提交
7409
                gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
7410
                gen_eob(s);
B
bellard 已提交
7411 7412
            }
            break;
A
Andre Przywara 已提交
7413 7414 7415 7416 7417
        case 7:
            if (mod != 3) { /* invlpg */
                if (s->cpl != 0) {
                    gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
                } else {
7418
                    gen_update_cc_op(s);
A
Andre Przywara 已提交
7419
                    gen_jmp_im(pc_start - s->cs_base);
7420
                    gen_lea_modrm(env, s, modrm);
B
Blue Swirl 已提交
7421
                    gen_helper_invlpg(cpu_env, cpu_A0);
A
Andre Przywara 已提交
7422 7423 7424
                    gen_jmp_im(s->pc - s->cs_base);
                    gen_eob(s);
                }
B
bellard 已提交
7425
            } else {
A
Andre Przywara 已提交
7426 7427
                switch (rm) {
                case 0: /* swapgs */
B
bellard 已提交
7428
#ifdef TARGET_X86_64
A
Andre Przywara 已提交
7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441
                    if (CODE64(s)) {
                        if (s->cpl != 0) {
                            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
                        } else {
                            tcg_gen_ld_tl(cpu_T[0], cpu_env,
                                offsetof(CPUX86State,segs[R_GS].base));
                            tcg_gen_ld_tl(cpu_T[1], cpu_env,
                                offsetof(CPUX86State,kernelgsbase));
                            tcg_gen_st_tl(cpu_T[1], cpu_env,
                                offsetof(CPUX86State,segs[R_GS].base));
                            tcg_gen_st_tl(cpu_T[0], cpu_env,
                                offsetof(CPUX86State,kernelgsbase));
                        }
7442
                    } else
B
bellard 已提交
7443 7444 7445 7446
#endif
                    {
                        goto illegal_op;
                    }
A
Andre Przywara 已提交
7447 7448 7449 7450
                    break;
                case 1: /* rdtscp */
                    if (!(s->cpuid_ext2_features & CPUID_EXT2_RDTSCP))
                        goto illegal_op;
7451
                    gen_update_cc_op(s);
B
bellard 已提交
7452
                    gen_jmp_im(pc_start - s->cs_base);
A
Andre Przywara 已提交
7453 7454
                    if (use_icount)
                        gen_io_start();
B
Blue Swirl 已提交
7455
                    gen_helper_rdtscp(cpu_env);
A
Andre Przywara 已提交
7456 7457 7458 7459 7460 7461 7462
                    if (use_icount) {
                        gen_io_end();
                        gen_jmp(s, s->pc - s->cs_base);
                    }
                    break;
                default:
                    goto illegal_op;
B
bellard 已提交
7463
                }
B
bellard 已提交
7464 7465 7466 7467 7468 7469
            }
            break;
        default:
            goto illegal_op;
        }
        break;
7470 7471 7472 7473 7474
    case 0x108: /* invd */
    case 0x109: /* wbinvd */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
B
bellard 已提交
7475
            gen_svm_check_intercept(s, pc_start, (b & 2) ? SVM_EXIT_INVD : SVM_EXIT_WBINVD);
7476 7477 7478
            /* nothing to do */
        }
        break;
B
bellard 已提交
7479 7480 7481 7482 7483
    case 0x63: /* arpl or movslS (x86_64) */
#ifdef TARGET_X86_64
        if (CODE64(s)) {
            int d_ot;
            /* d_ot is the size of destination */
7484
            d_ot = dflag;
B
bellard 已提交
7485

7486
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7487 7488 7489
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
            rm = (modrm & 7) | REX_B(s);
7490

B
bellard 已提交
7491
            if (mod == 3) {
7492
                gen_op_mov_v_reg(MO_32, cpu_T[0], rm);
B
bellard 已提交
7493
                /* sign extend */
7494
                if (d_ot == MO_64) {
B
bellard 已提交
7495
                    tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
7496
                }
7497
                gen_op_mov_reg_v(d_ot, reg, cpu_T[0]);
B
bellard 已提交
7498
            } else {
7499
                gen_lea_modrm(env, s, modrm);
R
Richard Henderson 已提交
7500
                gen_op_ld_v(s, MO_32 | MO_SIGN, cpu_T[0], cpu_A0);
7501
                gen_op_mov_reg_v(d_ot, reg, cpu_T[0]);
B
bellard 已提交
7502
            }
7503
        } else
B
bellard 已提交
7504 7505
#endif
        {
7506
            int label1;
L
Laurent Desnogues 已提交
7507
            TCGv t0, t1, t2, a0;
7508

B
bellard 已提交
7509 7510
            if (!s->pe || s->vm86)
                goto illegal_op;
P
pbrook 已提交
7511 7512 7513
            t0 = tcg_temp_local_new();
            t1 = tcg_temp_local_new();
            t2 = tcg_temp_local_new();
7514
            ot = MO_16;
7515
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7516 7517 7518 7519
            reg = (modrm >> 3) & 7;
            mod = (modrm >> 6) & 3;
            rm = modrm & 7;
            if (mod != 3) {
7520
                gen_lea_modrm(env, s, modrm);
7521
                gen_op_ld_v(s, ot, t0, cpu_A0);
L
Laurent Desnogues 已提交
7522 7523
                a0 = tcg_temp_local_new();
                tcg_gen_mov_tl(a0, cpu_A0);
B
bellard 已提交
7524
            } else {
7525
                gen_op_mov_v_reg(ot, t0, rm);
L
Laurent Desnogues 已提交
7526
                TCGV_UNUSED(a0);
B
bellard 已提交
7527
            }
7528 7529 7530 7531
            gen_op_mov_v_reg(ot, t1, reg);
            tcg_gen_andi_tl(cpu_tmp0, t0, 3);
            tcg_gen_andi_tl(t1, t1, 3);
            tcg_gen_movi_tl(t2, 0);
7532
            label1 = gen_new_label();
7533 7534 7535 7536
            tcg_gen_brcond_tl(TCG_COND_GE, cpu_tmp0, t1, label1);
            tcg_gen_andi_tl(t0, t0, ~3);
            tcg_gen_or_tl(t0, t0, t1);
            tcg_gen_movi_tl(t2, CC_Z);
7537
            gen_set_label(label1);
B
bellard 已提交
7538
            if (mod != 3) {
7539
                gen_op_st_v(s, ot, t0, a0);
L
Laurent Desnogues 已提交
7540 7541
                tcg_temp_free(a0);
           } else {
7542
                gen_op_mov_reg_v(ot, rm, t0);
B
bellard 已提交
7543
            }
7544
            gen_compute_eflags(s);
7545
            tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~CC_Z);
7546 7547 7548 7549
            tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, t2);
            tcg_temp_free(t0);
            tcg_temp_free(t1);
            tcg_temp_free(t2);
7550 7551
        }
        break;
B
bellard 已提交
7552 7553
    case 0x102: /* lar */
    case 0x103: /* lsl */
7554 7555
        {
            int label1;
7556
            TCGv t0;
7557 7558
            if (!s->pe || s->vm86)
                goto illegal_op;
7559
            ot = dflag != MO_16 ? MO_32 : MO_16;
7560
            modrm = cpu_ldub_code(env, s->pc++);
7561
            reg = ((modrm >> 3) & 7) | rex_r;
7562
            gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
P
pbrook 已提交
7563
            t0 = tcg_temp_local_new();
7564
            gen_update_cc_op(s);
7565 7566 7567 7568 7569
            if (b == 0x102) {
                gen_helper_lar(t0, cpu_env, cpu_T[0]);
            } else {
                gen_helper_lsl(t0, cpu_env, cpu_T[0]);
            }
7570 7571
            tcg_gen_andi_tl(cpu_tmp0, cpu_cc_src, CC_Z);
            label1 = gen_new_label();
P
pbrook 已提交
7572
            tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, label1);
7573
            gen_op_mov_reg_v(ot, reg, t0);
7574
            gen_set_label(label1);
7575
            set_cc_op(s, CC_OP_EFLAGS);
7576
            tcg_temp_free(t0);
7577
        }
B
bellard 已提交
7578 7579
        break;
    case 0x118:
7580
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7581 7582 7583 7584 7585 7586 7587 7588 7589
        mod = (modrm >> 6) & 3;
        op = (modrm >> 3) & 7;
        switch(op) {
        case 0: /* prefetchnta */
        case 1: /* prefetchnt0 */
        case 2: /* prefetchnt0 */
        case 3: /* prefetchnt0 */
            if (mod == 3)
                goto illegal_op;
7590
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
7591 7592
            /* nothing more to do */
            break;
B
bellard 已提交
7593
        default: /* nop (multi byte) */
7594
            gen_nop_modrm(env, s, modrm);
B
bellard 已提交
7595
            break;
B
bellard 已提交
7596 7597
        }
        break;
B
bellard 已提交
7598
    case 0x119 ... 0x11f: /* nop (multi byte) */
7599 7600
        modrm = cpu_ldub_code(env, s->pc++);
        gen_nop_modrm(env, s, modrm);
B
bellard 已提交
7601
        break;
B
bellard 已提交
7602 7603 7604 7605 7606
    case 0x120: /* mov reg, crN */
    case 0x122: /* mov crN, reg */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
7607
            modrm = cpu_ldub_code(env, s->pc++);
7608 7609 7610 7611 7612
            /* Ignore the mod bits (assume (modrm&0xc0)==0xc0).
             * AMD documentation (24594.pdf) and testing of
             * intel 386 and 486 processors all show that the mod bits
             * are assumed to be 1's, regardless of actual values.
             */
B
bellard 已提交
7613 7614 7615
            rm = (modrm & 7) | REX_B(s);
            reg = ((modrm >> 3) & 7) | rex_r;
            if (CODE64(s))
7616
                ot = MO_64;
B
bellard 已提交
7617
            else
7618
                ot = MO_32;
7619 7620 7621 7622
            if ((prefixes & PREFIX_LOCK) && (reg == 0) &&
                (s->cpuid_ext3_features & CPUID_EXT3_CR8LEG)) {
                reg = 8;
            }
B
bellard 已提交
7623 7624 7625 7626 7627
            switch(reg) {
            case 0:
            case 2:
            case 3:
            case 4:
B
bellard 已提交
7628
            case 8:
7629
                gen_update_cc_op(s);
B
bellard 已提交
7630
                gen_jmp_im(pc_start - s->cs_base);
B
bellard 已提交
7631
                if (b & 2) {
7632
                    gen_op_mov_v_reg(ot, cpu_T[0], rm);
B
Blue Swirl 已提交
7633 7634
                    gen_helper_write_crN(cpu_env, tcg_const_i32(reg),
                                         cpu_T[0]);
B
bellard 已提交
7635
                    gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
7636 7637
                    gen_eob(s);
                } else {
B
Blue Swirl 已提交
7638
                    gen_helper_read_crN(cpu_T[0], cpu_env, tcg_const_i32(reg));
7639
                    gen_op_mov_reg_v(ot, rm, cpu_T[0]);
B
bellard 已提交
7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651
                }
                break;
            default:
                goto illegal_op;
            }
        }
        break;
    case 0x121: /* mov reg, drN */
    case 0x123: /* mov drN, reg */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
7652
            modrm = cpu_ldub_code(env, s->pc++);
7653 7654 7655 7656 7657
            /* Ignore the mod bits (assume (modrm&0xc0)==0xc0).
             * AMD documentation (24594.pdf) and testing of
             * intel 386 and 486 processors all show that the mod bits
             * are assumed to be 1's, regardless of actual values.
             */
B
bellard 已提交
7658 7659 7660
            rm = (modrm & 7) | REX_B(s);
            reg = ((modrm >> 3) & 7) | rex_r;
            if (CODE64(s))
7661
                ot = MO_64;
B
bellard 已提交
7662
            else
7663
                ot = MO_32;
B
bellard 已提交
7664
            /* XXX: do it dynamically with CR4.DE bit */
B
bellard 已提交
7665
            if (reg == 4 || reg == 5 || reg >= 8)
B
bellard 已提交
7666 7667
                goto illegal_op;
            if (b & 2) {
T
ths 已提交
7668
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_DR0 + reg);
7669
                gen_op_mov_v_reg(ot, cpu_T[0], rm);
B
Blue Swirl 已提交
7670
                gen_helper_movl_drN_T0(cpu_env, tcg_const_i32(reg), cpu_T[0]);
B
bellard 已提交
7671
                gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
7672 7673
                gen_eob(s);
            } else {
T
ths 已提交
7674
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_READ_DR0 + reg);
B
bellard 已提交
7675
                tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,dr[reg]));
7676
                gen_op_mov_reg_v(ot, rm, cpu_T[0]);
B
bellard 已提交
7677 7678 7679 7680 7681 7682 7683
            }
        }
        break;
    case 0x106: /* clts */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
T
ths 已提交
7684
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_CR0);
7685
            gen_helper_clts(cpu_env);
B
bellard 已提交
7686
            /* abort block because static cpu state changed */
B
bellard 已提交
7687
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
7688
            gen_eob(s);
B
bellard 已提交
7689 7690
        }
        break;
B
balrog 已提交
7691
    /* MMX/3DNow!/SSE/SSE2/SSE3/SSSE3/SSE4 support */
B
bellard 已提交
7692 7693
    case 0x1c3: /* MOVNTI reg, mem */
        if (!(s->cpuid_features & CPUID_SSE2))
B
bellard 已提交
7694
            goto illegal_op;
7695
        ot = mo_64_32(dflag);
7696
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7697 7698 7699 7700 7701
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
        reg = ((modrm >> 3) & 7) | rex_r;
        /* generate a generic store */
7702
        gen_ldst_modrm(env, s, modrm, ot, reg, 1);
B
bellard 已提交
7703
        break;
B
bellard 已提交
7704
    case 0x1ae:
7705
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7706 7707 7708 7709
        mod = (modrm >> 6) & 3;
        op = (modrm >> 3) & 7;
        switch(op) {
        case 0: /* fxsave */
7710
            if (mod == 3 || !(s->cpuid_features & CPUID_FXSR) ||
7711
                (s->prefix & PREFIX_LOCK))
B
bellard 已提交
7712
                goto illegal_op;
7713
            if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
B
bellard 已提交
7714 7715 7716
                gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
                break;
            }
7717
            gen_lea_modrm(env, s, modrm);
7718
            gen_update_cc_op(s);
B
bellard 已提交
7719
            gen_jmp_im(pc_start - s->cs_base);
7720
            gen_helper_fxsave(cpu_env, cpu_A0, tcg_const_i32(dflag == MO_64));
B
bellard 已提交
7721 7722
            break;
        case 1: /* fxrstor */
7723
            if (mod == 3 || !(s->cpuid_features & CPUID_FXSR) ||
7724
                (s->prefix & PREFIX_LOCK))
B
bellard 已提交
7725
                goto illegal_op;
7726
            if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
B
bellard 已提交
7727 7728 7729
                gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
                break;
            }
7730
            gen_lea_modrm(env, s, modrm);
7731
            gen_update_cc_op(s);
B
bellard 已提交
7732
            gen_jmp_im(pc_start - s->cs_base);
7733
            gen_helper_fxrstor(cpu_env, cpu_A0, tcg_const_i32(dflag == MO_64));
B
bellard 已提交
7734 7735 7736 7737 7738 7739
            break;
        case 2: /* ldmxcsr */
        case 3: /* stmxcsr */
            if (s->flags & HF_TS_MASK) {
                gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
                break;
B
bellard 已提交
7740
            }
B
bellard 已提交
7741 7742
            if ((s->flags & HF_EM_MASK) || !(s->flags & HF_OSFXSR_MASK) ||
                mod == 3)
B
bellard 已提交
7743
                goto illegal_op;
7744
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
7745
            if (op == 2) {
7746 7747
                tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                    s->mem_index, MO_LEUL);
B
Blue Swirl 已提交
7748
                gen_helper_ldmxcsr(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
7749
            } else {
B
bellard 已提交
7750
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, mxcsr));
7751
                gen_op_st_v(s, MO_32, cpu_T[0], cpu_A0);
B
bellard 已提交
7752
            }
B
bellard 已提交
7753 7754 7755
            break;
        case 5: /* lfence */
        case 6: /* mfence */
7756
            if ((modrm & 0xc7) != 0xc0 || !(s->cpuid_features & CPUID_SSE2))
B
bellard 已提交
7757 7758
                goto illegal_op;
            break;
7759 7760 7761
        case 7: /* sfence / clflush */
            if ((modrm & 0xc7) == 0xc0) {
                /* sfence */
A
aurel32 已提交
7762
                /* XXX: also check for cpuid_ext2_features & CPUID_EXT2_EMMX */
7763 7764 7765 7766 7767 7768
                if (!(s->cpuid_features & CPUID_SSE))
                    goto illegal_op;
            } else {
                /* clflush */
                if (!(s->cpuid_features & CPUID_CLFLUSH))
                    goto illegal_op;
7769
                gen_lea_modrm(env, s, modrm);
7770 7771
            }
            break;
B
bellard 已提交
7772
        default:
B
bellard 已提交
7773 7774 7775
            goto illegal_op;
        }
        break;
A
aurel32 已提交
7776
    case 0x10d: /* 3DNow! prefetch(w) */
7777
        modrm = cpu_ldub_code(env, s->pc++);
A
aurel32 已提交
7778 7779 7780
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
7781
        gen_lea_modrm(env, s, modrm);
7782 7783
        /* ignore for now */
        break;
B
bellard 已提交
7784
    case 0x1aa: /* rsm */
B
bellard 已提交
7785
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_RSM);
B
bellard 已提交
7786 7787
        if (!(s->flags & HF_SMM_MASK))
            goto illegal_op;
J
Jun Koi 已提交
7788
        gen_update_cc_op(s);
B
bellard 已提交
7789
        gen_jmp_im(s->pc - s->cs_base);
B
Blue Swirl 已提交
7790
        gen_helper_rsm(cpu_env);
B
bellard 已提交
7791 7792
        gen_eob(s);
        break;
B
balrog 已提交
7793 7794 7795 7796 7797 7798 7799
    case 0x1b8: /* SSE4.2 popcnt */
        if ((prefixes & (PREFIX_REPZ | PREFIX_LOCK | PREFIX_REPNZ)) !=
             PREFIX_REPZ)
            goto illegal_op;
        if (!(s->cpuid_ext_features & CPUID_EXT_POPCNT))
            goto illegal_op;

7800
        modrm = cpu_ldub_code(env, s->pc++);
M
malc 已提交
7801
        reg = ((modrm >> 3) & 7) | rex_r;
B
balrog 已提交
7802

7803
        if (s->prefix & PREFIX_DATA) {
7804
            ot = MO_16;
7805 7806 7807
        } else {
            ot = mo_64_32(dflag);
        }
B
balrog 已提交
7808

7809
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
Blue Swirl 已提交
7810
        gen_helper_popcnt(cpu_T[0], cpu_env, cpu_T[0], tcg_const_i32(ot));
7811
        gen_op_mov_reg_v(ot, reg, cpu_T[0]);
B
balrog 已提交
7812

7813
        set_cc_op(s, CC_OP_EFLAGS);
B
balrog 已提交
7814
        break;
A
aurel32 已提交
7815 7816 7817
    case 0x10e ... 0x10f:
        /* 3DNow! instructions, ignore prefixes */
        s->prefix &= ~(PREFIX_REPZ | PREFIX_REPNZ | PREFIX_DATA);
B
bellard 已提交
7818 7819
    case 0x110 ... 0x117:
    case 0x128 ... 0x12f:
B
balrog 已提交
7820
    case 0x138 ... 0x13a:
7821
    case 0x150 ... 0x179:
B
bellard 已提交
7822 7823 7824 7825
    case 0x17c ... 0x17f:
    case 0x1c2:
    case 0x1c4 ... 0x1c6:
    case 0x1d0 ... 0x1fe:
7826
        gen_sse(env, s, b, pc_start, rex_r);
B
bellard 已提交
7827
        break;
B
bellard 已提交
7828 7829 7830 7831 7832
    default:
        goto illegal_op;
    }
    /* lock generation */
    if (s->prefix & PREFIX_LOCK)
P
pbrook 已提交
7833
        gen_helper_unlock();
B
bellard 已提交
7834 7835
    return s->pc;
 illegal_op:
7836
    if (s->prefix & PREFIX_LOCK)
P
pbrook 已提交
7837
        gen_helper_unlock();
B
bellard 已提交
7838 7839 7840 7841 7842 7843 7844
    /* XXX: ensure that no lock was generated */
    gen_exception(s, EXCP06_ILLOP, pc_start - s->cs_base);
    return s->pc;
}

void optimize_flags_init(void)
{
7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875
    static const char reg_names[CPU_NB_REGS][4] = {
#ifdef TARGET_X86_64
        [R_EAX] = "rax",
        [R_EBX] = "rbx",
        [R_ECX] = "rcx",
        [R_EDX] = "rdx",
        [R_ESI] = "rsi",
        [R_EDI] = "rdi",
        [R_EBP] = "rbp",
        [R_ESP] = "rsp",
        [8]  = "r8",
        [9]  = "r9",
        [10] = "r10",
        [11] = "r11",
        [12] = "r12",
        [13] = "r13",
        [14] = "r14",
        [15] = "r15",
#else
        [R_EAX] = "eax",
        [R_EBX] = "ebx",
        [R_ECX] = "ecx",
        [R_EDX] = "edx",
        [R_ESI] = "esi",
        [R_EDI] = "edi",
        [R_EBP] = "ebp",
        [R_ESP] = "esp",
#endif
    };
    int i;

P
pbrook 已提交
7876 7877
    cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
    cpu_cc_op = tcg_global_mem_new_i32(TCG_AREG0,
7878 7879
                                       offsetof(CPUX86State, cc_op), "cc_op");
    cpu_cc_dst = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_dst),
P
pbrook 已提交
7880
                                    "cc_dst");
7881 7882
    cpu_cc_src = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_src),
                                    "cc_src");
7883 7884
    cpu_cc_src2 = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_src2),
                                     "cc_src2");
7885

7886 7887 7888 7889 7890
    for (i = 0; i < CPU_NB_REGS; ++i) {
        cpu_regs[i] = tcg_global_mem_new(TCG_AREG0,
                                         offsetof(CPUX86State, regs[i]),
                                         reg_names[i]);
    }
B
bellard 已提交
7891 7892 7893 7894 7895
}

/* generate intermediate code in gen_opc_buf and gen_opparam_buf for
   basic block 'tb'. If search_pc is TRUE, also generate PC
   information for each intermediate instruction. */
7896
static inline void gen_intermediate_code_internal(X86CPU *cpu,
7897
                                                  TranslationBlock *tb,
7898
                                                  bool search_pc)
B
bellard 已提交
7899
{
7900
    CPUState *cs = CPU(cpu);
7901
    CPUX86State *env = &cpu->env;
B
bellard 已提交
7902
    DisasContext dc1, *dc = &dc1;
B
bellard 已提交
7903
    target_ulong pc_ptr;
B
bellard 已提交
7904
    uint16_t *gen_opc_end;
7905
    CPUBreakpoint *bp;
7906
    int j, lj;
7907
    uint64_t flags;
B
bellard 已提交
7908 7909
    target_ulong pc_start;
    target_ulong cs_base;
P
pbrook 已提交
7910 7911
    int num_insns;
    int max_insns;
7912

B
bellard 已提交
7913
    /* generate intermediate code */
B
bellard 已提交
7914 7915
    pc_start = tb->pc;
    cs_base = tb->cs_base;
B
bellard 已提交
7916
    flags = tb->flags;
B
bellard 已提交
7917

7918
    dc->pe = (flags >> HF_PE_SHIFT) & 1;
B
bellard 已提交
7919 7920 7921 7922 7923 7924 7925 7926
    dc->code32 = (flags >> HF_CS32_SHIFT) & 1;
    dc->ss32 = (flags >> HF_SS32_SHIFT) & 1;
    dc->addseg = (flags >> HF_ADDSEG_SHIFT) & 1;
    dc->f_st = 0;
    dc->vm86 = (flags >> VM_SHIFT) & 1;
    dc->cpl = (flags >> HF_CPL_SHIFT) & 3;
    dc->iopl = (flags >> IOPL_SHIFT) & 3;
    dc->tf = (flags >> TF_SHIFT) & 1;
7927
    dc->singlestep_enabled = cs->singlestep_enabled;
B
bellard 已提交
7928
    dc->cc_op = CC_OP_DYNAMIC;
7929
    dc->cc_op_dirty = false;
B
bellard 已提交
7930 7931 7932 7933 7934 7935
    dc->cs_base = cs_base;
    dc->tb = tb;
    dc->popl_esp_hack = 0;
    /* select memory access functions */
    dc->mem_index = 0;
    if (flags & HF_SOFTMMU_MASK) {
7936
        dc->mem_index = cpu_mmu_index(env);
B
bellard 已提交
7937
    }
7938 7939 7940 7941 7942
    dc->cpuid_features = env->features[FEAT_1_EDX];
    dc->cpuid_ext_features = env->features[FEAT_1_ECX];
    dc->cpuid_ext2_features = env->features[FEAT_8000_0001_EDX];
    dc->cpuid_ext3_features = env->features[FEAT_8000_0001_ECX];
    dc->cpuid_7_0_ebx_features = env->features[FEAT_7_0_EBX];
B
bellard 已提交
7943 7944 7945 7946
#ifdef TARGET_X86_64
    dc->lma = (flags >> HF_LMA_SHIFT) & 1;
    dc->code64 = (flags >> HF_CS64_SHIFT) & 1;
#endif
B
bellard 已提交
7947
    dc->flags = flags;
7948
    dc->jmp_opt = !(dc->tf || cs->singlestep_enabled ||
7949
                    (flags & HF_INHIBIT_IRQ_MASK)
B
bellard 已提交
7950
#ifndef CONFIG_SOFTMMU
B
bellard 已提交
7951 7952 7953
                    || (flags & HF_SOFTMMU_MASK)
#endif
                    );
7954 7955
#if 0
    /* check addseg logic */
B
bellard 已提交
7956
    if (!dc->addseg && (dc->vm86 || !dc->pe || !dc->code32))
7957 7958 7959
        printf("ERROR addseg\n");
#endif

P
pbrook 已提交
7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970
    cpu_T[0] = tcg_temp_new();
    cpu_T[1] = tcg_temp_new();
    cpu_A0 = tcg_temp_new();

    cpu_tmp0 = tcg_temp_new();
    cpu_tmp1_i64 = tcg_temp_new_i64();
    cpu_tmp2_i32 = tcg_temp_new_i32();
    cpu_tmp3_i32 = tcg_temp_new_i32();
    cpu_tmp4 = tcg_temp_new();
    cpu_ptr0 = tcg_temp_new_ptr();
    cpu_ptr1 = tcg_temp_new_ptr();
7971
    cpu_cc_srcT = tcg_temp_local_new();
B
bellard 已提交
7972

7973
    gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
B
bellard 已提交
7974 7975 7976 7977

    dc->is_jmp = DISAS_NEXT;
    pc_ptr = pc_start;
    lj = -1;
P
pbrook 已提交
7978 7979 7980 7981
    num_insns = 0;
    max_insns = tb->cflags & CF_COUNT_MASK;
    if (max_insns == 0)
        max_insns = CF_COUNT_MASK;
B
bellard 已提交
7982

7983
    gen_tb_start();
B
bellard 已提交
7984
    for(;;) {
7985 7986
        if (unlikely(!QTAILQ_EMPTY(&cs->breakpoints))) {
            QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {
J
Jan Kiszka 已提交
7987 7988
                if (bp->pc == pc_ptr &&
                    !((bp->flags & BP_CPU) && (tb->flags & HF_RF_MASK))) {
B
bellard 已提交
7989 7990 7991 7992 7993 7994
                    gen_debug(dc, pc_ptr - dc->cs_base);
                    break;
                }
            }
        }
        if (search_pc) {
7995
            j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
B
bellard 已提交
7996 7997 7998
            if (lj < j) {
                lj++;
                while (lj < j)
7999
                    tcg_ctx.gen_opc_instr_start[lj++] = 0;
B
bellard 已提交
8000
            }
8001
            tcg_ctx.gen_opc_pc[lj] = pc_ptr;
B
bellard 已提交
8002
            gen_opc_cc_op[lj] = dc->cc_op;
8003
            tcg_ctx.gen_opc_instr_start[lj] = 1;
8004
            tcg_ctx.gen_opc_icount[lj] = num_insns;
B
bellard 已提交
8005
        }
P
pbrook 已提交
8006 8007 8008
        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
            gen_io_start();

8009
        pc_ptr = disas_insn(env, dc, pc_ptr);
P
pbrook 已提交
8010
        num_insns++;
B
bellard 已提交
8011 8012 8013 8014 8015
        /* stop translation if indicated */
        if (dc->is_jmp)
            break;
        /* if single step mode, we generate only one instruction and
           generate an exception */
8016 8017 8018
        /* if irq were inhibited with HF_INHIBIT_IRQ_MASK, we clear
           the flag and abort the translation to give the irqs a
           change to be happen */
8019
        if (dc->tf || dc->singlestep_enabled ||
P
pbrook 已提交
8020
            (flags & HF_INHIBIT_IRQ_MASK)) {
B
bellard 已提交
8021
            gen_jmp_im(pc_ptr - dc->cs_base);
B
bellard 已提交
8022 8023 8024 8025
            gen_eob(dc);
            break;
        }
        /* if too long translation, stop generation too */
8026
        if (tcg_ctx.gen_opc_ptr >= gen_opc_end ||
P
pbrook 已提交
8027 8028
            (pc_ptr - pc_start) >= (TARGET_PAGE_SIZE - 32) ||
            num_insns >= max_insns) {
B
bellard 已提交
8029
            gen_jmp_im(pc_ptr - dc->cs_base);
B
bellard 已提交
8030 8031 8032
            gen_eob(dc);
            break;
        }
8033 8034 8035 8036 8037
        if (singlestep) {
            gen_jmp_im(pc_ptr - dc->cs_base);
            gen_eob(dc);
            break;
        }
B
bellard 已提交
8038
    }
P
pbrook 已提交
8039 8040
    if (tb->cflags & CF_LAST_IO)
        gen_io_end();
8041
    gen_tb_end(tb, num_insns);
8042
    *tcg_ctx.gen_opc_ptr = INDEX_op_end;
B
bellard 已提交
8043 8044
    /* we don't forget to fill the last values */
    if (search_pc) {
8045
        j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
B
bellard 已提交
8046 8047
        lj++;
        while (lj <= j)
8048
            tcg_ctx.gen_opc_instr_start[lj++] = 0;
B
bellard 已提交
8049
    }
8050

B
bellard 已提交
8051
#ifdef DEBUG_DISAS
8052
    if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
B
bellard 已提交
8053
        int disas_flags;
8054 8055
        qemu_log("----------------\n");
        qemu_log("IN: %s\n", lookup_symbol(pc_start));
B
bellard 已提交
8056 8057 8058 8059 8060 8061
#ifdef TARGET_X86_64
        if (dc->code64)
            disas_flags = 2;
        else
#endif
            disas_flags = !dc->code32;
B
Blue Swirl 已提交
8062
        log_target_disas(env, pc_start, pc_ptr - pc_start, disas_flags);
8063
        qemu_log("\n");
B
bellard 已提交
8064 8065 8066
    }
#endif

P
pbrook 已提交
8067
    if (!search_pc) {
B
bellard 已提交
8068
        tb->size = pc_ptr - pc_start;
P
pbrook 已提交
8069 8070
        tb->icount = num_insns;
    }
B
bellard 已提交
8071 8072
}

8073
void gen_intermediate_code(CPUX86State *env, TranslationBlock *tb)
B
bellard 已提交
8074
{
8075
    gen_intermediate_code_internal(x86_env_get_cpu(env), tb, false);
B
bellard 已提交
8076 8077
}

8078
void gen_intermediate_code_pc(CPUX86State *env, TranslationBlock *tb)
B
bellard 已提交
8079
{
8080
    gen_intermediate_code_internal(x86_env_get_cpu(env), tb, true);
B
bellard 已提交
8081 8082
}

8083
void restore_state_to_opc(CPUX86State *env, TranslationBlock *tb, int pc_pos)
A
aurel32 已提交
8084 8085 8086
{
    int cc_op;
#ifdef DEBUG_DISAS
8087
    if (qemu_loglevel_mask(CPU_LOG_TB_OP)) {
A
aurel32 已提交
8088
        int i;
8089
        qemu_log("RESTORE:\n");
A
aurel32 已提交
8090
        for(i = 0;i <= pc_pos; i++) {
8091
            if (tcg_ctx.gen_opc_instr_start[i]) {
8092 8093
                qemu_log("0x%04x: " TARGET_FMT_lx "\n", i,
                        tcg_ctx.gen_opc_pc[i]);
A
aurel32 已提交
8094 8095
            }
        }
8096
        qemu_log("pc_pos=0x%x eip=" TARGET_FMT_lx " cs_base=%x\n",
8097
                pc_pos, tcg_ctx.gen_opc_pc[pc_pos] - tb->cs_base,
A
aurel32 已提交
8098 8099 8100
                (uint32_t)tb->cs_base);
    }
#endif
8101
    env->eip = tcg_ctx.gen_opc_pc[pc_pos] - tb->cs_base;
A
aurel32 已提交
8102 8103 8104 8105
    cc_op = gen_opc_cc_op[pc_pos];
    if (cc_op != CC_OP_DYNAMIC)
        env->cc_op = cc_op;
}