translate.c 273.1 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"
B
bellard 已提交
30

P
pbrook 已提交
31 32 33 34
#include "helper.h"
#define GEN_HELPER 1
#include "helper.h"

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

B
bellard 已提交
42 43 44 45 46 47 48 49 50 51
#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

52 53 54 55 56 57 58 59
#ifdef TARGET_X86_64
# define ctztl  ctz64
# define clztl  clz64
#else
# define ctztl  ctz32
# define clztl  clz32
#endif

B
bellard 已提交
60 61 62
//#define MACRO_TEST   1

/* global register indexes */
P
pbrook 已提交
63
static TCGv_ptr cpu_env;
64
static TCGv cpu_A0;
65
static TCGv cpu_cc_dst, cpu_cc_src, cpu_cc_src2, cpu_cc_srcT;
P
pbrook 已提交
66
static TCGv_i32 cpu_cc_op;
67
static TCGv cpu_regs[CPU_NB_REGS];
68
/* local temps */
P
Paolo Bonzini 已提交
69
static TCGv cpu_T[2];
B
bellard 已提交
70
/* local register indexes (only used inside old micro ops) */
P
pbrook 已提交
71 72 73 74
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 已提交
75

76 77
static uint8_t gen_opc_cc_op[OPC_BUF_SIZE];

78
#include "exec/gen-icount.h"
P
pbrook 已提交
79

B
bellard 已提交
80 81
#ifdef TARGET_X86_64
static int x86_64_hregs;
B
bellard 已提交
82 83
#endif

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

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

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

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

156 157 158 159 160 161 162 163 164 165 166
enum {
    JCC_O,
    JCC_B,
    JCC_Z,
    JCC_BE,
    JCC_S,
    JCC_P,
    JCC_L,
    JCC_LE,
};

B
bellard 已提交
167 168 169 170 171 172 173 174 175 176
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 已提交
177 178

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

183
enum {
184 185
    USES_CC_DST  = 1,
    USES_CC_SRC  = 2,
186 187
    USES_CC_SRC2 = 4,
    USES_CC_SRCT = 8,
188 189 190 191
};

/* Bit set if the global variable is live after setting CC_OP to X.  */
static const uint8_t cc_op_live[CC_OP_NB] = {
192
    [CC_OP_DYNAMIC] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
193 194 195
    [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,
196
    [CC_OP_ADCB ... CC_OP_ADCQ] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
197
    [CC_OP_SUBB ... CC_OP_SUBQ] = USES_CC_DST | USES_CC_SRC | USES_CC_SRCT,
198
    [CC_OP_SBBB ... CC_OP_SBBQ] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
199 200 201 202 203
    [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,
204
    [CC_OP_BMILGB ... CC_OP_BMILGQ] = USES_CC_DST | USES_CC_SRC,
205 206 207
    [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 已提交
208
    [CC_OP_CLR] = 0,
209 210
};

211
static void set_cc_op(DisasContext *s, CCOp op)
212
{
213 214 215 216 217 218 219 220 221 222
    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);
223
    }
224 225 226
    if (dead & USES_CC_SRC) {
        tcg_gen_discard_tl(cpu_cc_src);
    }
227 228 229
    if (dead & USES_CC_SRC2) {
        tcg_gen_discard_tl(cpu_cc_src2);
    }
230 231 232
    if (dead & USES_CC_SRCT) {
        tcg_gen_discard_tl(cpu_cc_srcT);
    }
233

234 235 236 237 238 239 240 241 242 243 244
    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;
    }
245
    s->cc_op = op;
246 247 248 249 250
}

static void gen_update_cc_op(DisasContext *s)
{
    if (s->cc_op_dirty) {
251
        tcg_gen_movi_i32(cpu_cc_op, s->cc_op);
252 253
        s->cc_op_dirty = false;
    }
254 255
}

B
bellard 已提交
256 257 258 259 260 261 262 263 264 265
#ifdef TARGET_X86_64

#define NB_OP_SIZES 4

#else /* !TARGET_X86_64 */

#define NB_OP_SIZES 3

#endif /* !TARGET_X86_64 */

266
#if defined(HOST_WORDS_BIGENDIAN)
B
bellard 已提交
267 268 269 270 271
#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 已提交
272
#else
B
bellard 已提交
273 274 275 276 277
#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 已提交
278
#endif
B
bellard 已提交
279

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
/* 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;
}

299 300 301 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
/* 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;
}

333
static void gen_op_mov_reg_v(TCGMemOp ot, int reg, TCGv t0)
B
bellard 已提交
334 335
{
    switch(ot) {
336
    case MO_8:
337
        if (!byte_reg_is_xH(reg)) {
338
            tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], t0, 0, 8);
B
bellard 已提交
339
        } else {
340
            tcg_gen_deposit_tl(cpu_regs[reg - 4], cpu_regs[reg - 4], t0, 8, 8);
B
bellard 已提交
341 342
        }
        break;
343
    case MO_16:
344
        tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], t0, 0, 16);
B
bellard 已提交
345
        break;
346
    case MO_32:
347 348 349
        /* 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 已提交
350
        break;
351
#ifdef TARGET_X86_64
352
    case MO_64:
353
        tcg_gen_mov_tl(cpu_regs[reg], t0);
B
bellard 已提交
354
        break;
B
bellard 已提交
355
#endif
356 357
    default:
        tcg_abort();
B
bellard 已提交
358 359
    }
}
B
bellard 已提交
360

361
static inline void gen_op_mov_reg_T0(TCGMemOp ot, int reg)
B
bellard 已提交
362
{
363
    gen_op_mov_reg_v(ot, reg, cpu_T[0]);
B
bellard 已提交
364 365
}

366
static inline void gen_op_mov_reg_T1(TCGMemOp ot, int reg)
B
bellard 已提交
367
{
368
    gen_op_mov_reg_v(ot, reg, cpu_T[1]);
B
bellard 已提交
369 370
}

371
static inline void gen_op_mov_reg_A0(TCGMemOp size, int reg)
B
bellard 已提交
372
{
373
    gen_op_mov_reg_v(size, reg, cpu_A0);
B
bellard 已提交
374 375
}

376
static inline void gen_op_mov_v_reg(TCGMemOp ot, TCGv t0, int reg)
B
bellard 已提交
377
{
378
    if (ot == MO_8 && byte_reg_is_xH(reg)) {
379 380 381
        tcg_gen_shri_tl(t0, cpu_regs[reg - 4], 8);
        tcg_gen_ext8u_tl(t0, t0);
    } else {
382
        tcg_gen_mov_tl(t0, cpu_regs[reg]);
B
bellard 已提交
383 384 385
    }
}

386
static inline void gen_op_mov_TN_reg(TCGMemOp ot, int t_index, int reg)
387 388 389 390
{
    gen_op_mov_v_reg(ot, cpu_T[t_index], reg);
}

B
bellard 已提交
391 392
static inline void gen_op_movl_A0_reg(int reg)
{
393
    tcg_gen_mov_tl(cpu_A0, cpu_regs[reg]);
B
bellard 已提交
394 395 396 397 398
}

static inline void gen_op_addl_A0_im(int32_t val)
{
    tcg_gen_addi_tl(cpu_A0, cpu_A0, val);
B
bellard 已提交
399
#ifdef TARGET_X86_64
B
bellard 已提交
400
    tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffffffff);
B
bellard 已提交
401
#endif
B
bellard 已提交
402
}
B
bellard 已提交
403

B
bellard 已提交
404
#ifdef TARGET_X86_64
B
bellard 已提交
405 406 407 408
static inline void gen_op_addq_A0_im(int64_t val)
{
    tcg_gen_addi_tl(cpu_A0, cpu_A0, val);
}
B
bellard 已提交
409
#endif
B
bellard 已提交
410 411 412 413 414 415 416 417 418 419
    
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 已提交
420

B
bellard 已提交
421
static inline void gen_op_addl_T0_T1(void)
B
bellard 已提交
422
{
B
bellard 已提交
423 424 425 426 427
    tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
}

static inline void gen_op_jmp_T0(void)
{
428
    tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, eip));
B
bellard 已提交
429 430
}

431
static inline void gen_op_add_reg_im(TCGMemOp size, int reg, int32_t val)
B
bellard 已提交
432
{
433 434
    tcg_gen_addi_tl(cpu_tmp0, cpu_regs[reg], val);
    gen_op_mov_reg_v(size, reg, cpu_tmp0);
B
bellard 已提交
435 436
}

437
static inline void gen_op_add_reg_T0(TCGMemOp size, int reg)
B
bellard 已提交
438
{
439 440
    tcg_gen_add_tl(cpu_tmp0, cpu_regs[reg], cpu_T[0]);
    gen_op_mov_reg_v(size, reg, cpu_tmp0);
441
}
B
bellard 已提交
442 443 444

static inline void gen_op_addl_A0_reg_sN(int shift, int reg)
{
445 446
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[reg]);
    if (shift != 0)
B
bellard 已提交
447 448
        tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, shift);
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
449 450 451
    /* 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 已提交
452
}
B
bellard 已提交
453

B
bellard 已提交
454 455
static inline void gen_op_movl_A0_seg(int reg)
{
456
    tcg_gen_ld32u_tl(cpu_A0, cpu_env, offsetof(CPUX86State, segs[reg].base) + REG_L_OFFSET);
B
bellard 已提交
457
}
B
bellard 已提交
458

459
static inline void gen_op_addl_A0_seg(DisasContext *s, int reg)
B
bellard 已提交
460
{
461
    tcg_gen_ld_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, segs[reg].base));
B
bellard 已提交
462
#ifdef TARGET_X86_64
463 464 465 466 467 468 469 470 471
    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 已提交
472 473
#endif
}
B
bellard 已提交
474

B
bellard 已提交
475
#ifdef TARGET_X86_64
B
bellard 已提交
476 477
static inline void gen_op_movq_A0_seg(int reg)
{
478
    tcg_gen_ld_tl(cpu_A0, cpu_env, offsetof(CPUX86State, segs[reg].base));
B
bellard 已提交
479
}
B
bellard 已提交
480

B
bellard 已提交
481 482
static inline void gen_op_addq_A0_seg(int reg)
{
483
    tcg_gen_ld_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, segs[reg].base));
B
bellard 已提交
484 485 486 487 488
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
}

static inline void gen_op_movq_A0_reg(int reg)
{
489
    tcg_gen_mov_tl(cpu_A0, cpu_regs[reg]);
B
bellard 已提交
490 491 492 493
}

static inline void gen_op_addq_A0_reg_sN(int shift, int reg)
{
494 495
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[reg]);
    if (shift != 0)
B
bellard 已提交
496 497 498
        tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, shift);
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
}
B
bellard 已提交
499 500
#endif

501
static inline void gen_op_ld_v(DisasContext *s, int idx, TCGv t0, TCGv a0)
B
bellard 已提交
502
{
503
    tcg_gen_qemu_ld_tl(t0, a0, s->mem_index, idx | MO_LE);
B
bellard 已提交
504
}
B
bellard 已提交
505

506
static inline void gen_op_st_v(DisasContext *s, int idx, TCGv t0, TCGv a0)
B
bellard 已提交
507
{
508
    tcg_gen_qemu_st_tl(t0, a0, s->mem_index, idx | MO_LE);
B
bellard 已提交
509
}
510

511 512 513
static inline void gen_op_st_rm_T0_A0(DisasContext *s, int idx, int d)
{
    if (d == OR_TMP0) {
514
        gen_op_st_v(s, idx, cpu_T[0], cpu_A0);
515 516 517 518 519
    } else {
        gen_op_mov_reg_T0(idx, d);
    }
}

B
bellard 已提交
520 521
static inline void gen_jmp_im(target_ulong pc)
{
B
bellard 已提交
522
    tcg_gen_movi_tl(cpu_tmp0, pc);
523
    tcg_gen_st_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, eip));
B
bellard 已提交
524 525
}

B
bellard 已提交
526 527 528 529 530
static inline void gen_string_movl_A0_ESI(DisasContext *s)
{
    int override;

    override = s->override;
531
    switch (s->aflag) {
B
bellard 已提交
532
#ifdef TARGET_X86_64
533
    case MO_64:
B
bellard 已提交
534
        if (override >= 0) {
B
bellard 已提交
535 536
            gen_op_movq_A0_seg(override);
            gen_op_addq_A0_reg_sN(0, R_ESI);
B
bellard 已提交
537
        } else {
B
bellard 已提交
538
            gen_op_movq_A0_reg(R_ESI);
B
bellard 已提交
539
        }
540
        break;
B
bellard 已提交
541
#endif
542
    case MO_32:
B
bellard 已提交
543 544 545 546
        /* 32 bit address */
        if (s->addseg && override < 0)
            override = R_DS;
        if (override >= 0) {
B
bellard 已提交
547 548
            gen_op_movl_A0_seg(override);
            gen_op_addl_A0_reg_sN(0, R_ESI);
B
bellard 已提交
549
        } else {
B
bellard 已提交
550
            gen_op_movl_A0_reg(R_ESI);
B
bellard 已提交
551
        }
552 553
        break;
    case MO_16:
B
bellard 已提交
554 555 556
        /* 16 address, always override */
        if (override < 0)
            override = R_DS;
557
        tcg_gen_ext16u_tl(cpu_A0, cpu_regs[R_ESI]);
558
        gen_op_addl_A0_seg(s, override);
559 560 561
        break;
    default:
        tcg_abort();
B
bellard 已提交
562 563 564 565 566
    }
}

static inline void gen_string_movl_A0_EDI(DisasContext *s)
{
567
    switch (s->aflag) {
B
bellard 已提交
568
#ifdef TARGET_X86_64
569
    case MO_64:
B
bellard 已提交
570
        gen_op_movq_A0_reg(R_EDI);
571
        break;
B
bellard 已提交
572
#endif
573
    case MO_32:
B
bellard 已提交
574
        if (s->addseg) {
B
bellard 已提交
575 576
            gen_op_movl_A0_seg(R_ES);
            gen_op_addl_A0_reg_sN(0, R_EDI);
B
bellard 已提交
577
        } else {
B
bellard 已提交
578
            gen_op_movl_A0_reg(R_EDI);
B
bellard 已提交
579
        }
580 581
        break;
    case MO_16:
582
        tcg_gen_ext16u_tl(cpu_A0, cpu_regs[R_EDI]);
583
        gen_op_addl_A0_seg(s, R_ES);
584 585 586
        break;
    default:
        tcg_abort();
B
bellard 已提交
587 588 589
    }
}

590
static inline void gen_op_movl_T0_Dshift(TCGMemOp ot)
591
{
592
    tcg_gen_ld32s_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, df));
593
    tcg_gen_shli_tl(cpu_T[0], cpu_T[0], ot);
B
bellard 已提交
594 595
};

596
static TCGv gen_ext_tl(TCGv dst, TCGv src, TCGMemOp size, bool sign)
597
{
598
    switch (size) {
599
    case MO_8:
600 601 602 603 604 605
        if (sign) {
            tcg_gen_ext8s_tl(dst, src);
        } else {
            tcg_gen_ext8u_tl(dst, src);
        }
        return dst;
606
    case MO_16:
607 608 609 610 611 612 613
        if (sign) {
            tcg_gen_ext16s_tl(dst, src);
        } else {
            tcg_gen_ext16u_tl(dst, src);
        }
        return dst;
#ifdef TARGET_X86_64
614
    case MO_32:
615 616 617 618 619 620 621
        if (sign) {
            tcg_gen_ext32s_tl(dst, src);
        } else {
            tcg_gen_ext32u_tl(dst, src);
        }
        return dst;
#endif
622
    default:
623
        return src;
624 625
    }
}
626

627
static void gen_extu(TCGMemOp ot, TCGv reg)
628 629 630 631
{
    gen_ext_tl(reg, reg, ot, false);
}

632
static void gen_exts(TCGMemOp ot, TCGv reg)
633
{
634
    gen_ext_tl(reg, reg, ot, true);
635
}
B
bellard 已提交
636

637
static inline void gen_op_jnz_ecx(TCGMemOp size, int label1)
638
{
639
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
640
    gen_extu(size, cpu_tmp0);
P
pbrook 已提交
641
    tcg_gen_brcondi_tl(TCG_COND_NE, cpu_tmp0, 0, label1);
642 643
}

644
static inline void gen_op_jz_ecx(TCGMemOp size, int label1)
645
{
646
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
647
    gen_extu(size, cpu_tmp0);
P
pbrook 已提交
648
    tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, label1);
649
}
B
bellard 已提交
650

651
static void gen_helper_in_func(TCGMemOp ot, TCGv v, TCGv_i32 n)
P
pbrook 已提交
652 653
{
    switch (ot) {
654
    case MO_8:
655 656
        gen_helper_inb(v, n);
        break;
657
    case MO_16:
658 659
        gen_helper_inw(v, n);
        break;
660
    case MO_32:
661 662
        gen_helper_inl(v, n);
        break;
663 664
    default:
        tcg_abort();
P
pbrook 已提交
665 666
    }
}
B
bellard 已提交
667

668
static void gen_helper_out_func(TCGMemOp ot, TCGv_i32 v, TCGv_i32 n)
P
pbrook 已提交
669 670
{
    switch (ot) {
671
    case MO_8:
672 673
        gen_helper_outb(v, n);
        break;
674
    case MO_16:
675 676
        gen_helper_outw(v, n);
        break;
677
    case MO_32:
678 679
        gen_helper_outl(v, n);
        break;
680 681
    default:
        tcg_abort();
P
pbrook 已提交
682 683
    }
}
684

685
static void gen_check_io(DisasContext *s, TCGMemOp ot, target_ulong cur_eip,
686
                         uint32_t svm_flags)
687
{
688 689 690 691
    int state_saved;
    target_ulong next_eip;

    state_saved = 0;
692
    if (s->pe && (s->cpl > s->iopl || s->vm86)) {
693
        gen_update_cc_op(s);
B
bellard 已提交
694
        gen_jmp_im(cur_eip);
695
        state_saved = 1;
696
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
697
        switch (ot) {
698
        case MO_8:
B
Blue Swirl 已提交
699 700
            gen_helper_check_iob(cpu_env, cpu_tmp2_i32);
            break;
701
        case MO_16:
B
Blue Swirl 已提交
702 703
            gen_helper_check_iow(cpu_env, cpu_tmp2_i32);
            break;
704
        case MO_32:
B
Blue Swirl 已提交
705 706
            gen_helper_check_iol(cpu_env, cpu_tmp2_i32);
            break;
707 708
        default:
            tcg_abort();
P
pbrook 已提交
709
        }
710
    }
B
bellard 已提交
711
    if(s->flags & HF_SVMI_MASK) {
712
        if (!state_saved) {
713
            gen_update_cc_op(s);
714 715 716 717
            gen_jmp_im(cur_eip);
        }
        svm_flags |= (1 << (4 + ot));
        next_eip = s->pc - s->cs_base;
718
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
719 720
        gen_helper_svm_check_io(cpu_env, cpu_tmp2_i32,
                                tcg_const_i32(svm_flags),
P
pbrook 已提交
721
                                tcg_const_i32(next_eip - cur_eip));
722 723 724
    }
}

725
static inline void gen_movs(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
726 727
{
    gen_string_movl_A0_ESI(s);
728
    gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
729
    gen_string_movl_A0_EDI(s);
730
    gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
731
    gen_op_movl_T0_Dshift(ot);
732 733
    gen_op_add_reg_T0(s->aflag, R_ESI);
    gen_op_add_reg_T0(s->aflag, R_EDI);
B
bellard 已提交
734 735
}

736 737 738 739 740 741 742 743 744 745 746
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]);
}

747 748 749 750 751 752 753
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]);
}

754 755 756 757 758 759 760 761
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]);
762 763
    tcg_gen_neg_tl(cpu_cc_src, cpu_T[0]);
    tcg_gen_movi_tl(cpu_cc_srcT, 0);
764 765
}

766 767
/* compute all eflags to cc_src */
static void gen_compute_eflags(DisasContext *s)
768
{
769
    TCGv zero, dst, src1, src2;
770 771
    int live, dead;

772 773 774
    if (s->cc_op == CC_OP_EFLAGS) {
        return;
    }
R
Richard Henderson 已提交
775 776 777 778 779
    if (s->cc_op == CC_OP_CLR) {
        tcg_gen_movi_tl(cpu_cc_src, CC_Z);
        set_cc_op(s, CC_OP_EFLAGS);
        return;
    }
780 781 782 783

    TCGV_UNUSED(zero);
    dst = cpu_cc_dst;
    src1 = cpu_cc_src;
784
    src2 = cpu_cc_src2;
785 786 787

    /* Take care to not read values that are not live.  */
    live = cc_op_live[s->cc_op] & ~USES_CC_SRCT;
788
    dead = live ^ (USES_CC_DST | USES_CC_SRC | USES_CC_SRC2);
789 790 791 792 793 794 795 796
    if (dead) {
        zero = tcg_const_tl(0);
        if (dead & USES_CC_DST) {
            dst = zero;
        }
        if (dead & USES_CC_SRC) {
            src1 = zero;
        }
797 798 799
        if (dead & USES_CC_SRC2) {
            src2 = zero;
        }
800 801
    }

802
    gen_update_cc_op(s);
803
    gen_helper_cc_compute_all(cpu_cc_src, dst, src1, src2, cpu_cc_op);
804
    set_cc_op(s, CC_OP_EFLAGS);
805 806 807 808

    if (dead) {
        tcg_temp_free(zero);
    }
809 810
}

811 812 813 814 815 816 817 818 819 820
typedef struct CCPrepare {
    TCGCond cond;
    TCGv reg;
    TCGv reg2;
    target_ulong imm;
    target_ulong mask;
    bool use_reg2;
    bool no_setcond;
} CCPrepare;

821
/* compute eflags.C to reg */
822
static CCPrepare gen_prepare_eflags_c(DisasContext *s, TCGv reg)
823 824
{
    TCGv t0, t1;
825
    int size, shift;
826 827 828

    switch (s->cc_op) {
    case CC_OP_SUBB ... CC_OP_SUBQ:
829
        /* (DATA_TYPE)CC_SRCT < (DATA_TYPE)CC_SRC */
830 831 832 833
        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;
834
        tcg_gen_mov_tl(t0, cpu_cc_srcT);
835 836 837 838 839 840 841 842 843
        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:
844 845
        return (CCPrepare) { .cond = TCG_COND_LTU, .reg = t0,
                             .reg2 = t1, .mask = -1, .use_reg2 = true };
846 847

    case CC_OP_LOGICB ... CC_OP_LOGICQ:
R
Richard Henderson 已提交
848
    case CC_OP_CLR:
849
        return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
850 851 852

    case CC_OP_INCB ... CC_OP_INCQ:
    case CC_OP_DECB ... CC_OP_DECQ:
853 854
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = -1, .no_setcond = true };
855 856 857 858

    case CC_OP_SHLB ... CC_OP_SHLQ:
        /* (CC_SRC >> (DATA_BITS - 1)) & 1 */
        size = s->cc_op - CC_OP_SHLB;
859 860 861
        shift = (8 << size) - 1;
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = (target_ulong)1 << shift };
862 863

    case CC_OP_MULB ... CC_OP_MULQ:
864 865
        return (CCPrepare) { .cond = TCG_COND_NE,
                             .reg = cpu_cc_src, .mask = -1 };
866

867 868 869 870 871
    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 };

872 873 874 875 876
    case CC_OP_ADCX:
    case CC_OP_ADCOX:
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_dst,
                             .mask = -1, .no_setcond = true };

877 878 879
    case CC_OP_EFLAGS:
    case CC_OP_SARB ... CC_OP_SARQ:
        /* CC_SRC & 1 */
880 881
        return (CCPrepare) { .cond = TCG_COND_NE,
                             .reg = cpu_cc_src, .mask = CC_C };
882 883 884 885 886

    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);
887 888
       gen_helper_cc_compute_c(reg, cpu_cc_dst, cpu_cc_src,
                               cpu_cc_src2, cpu_cc_op);
889 890
       return (CCPrepare) { .cond = TCG_COND_NE, .reg = reg,
                            .mask = -1, .no_setcond = true };
891 892 893
    }
}

894
/* compute eflags.P to reg */
895
static CCPrepare gen_prepare_eflags_p(DisasContext *s, TCGv reg)
896
{
897
    gen_compute_eflags(s);
898 899
    return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                         .mask = CC_P };
900 901 902
}

/* compute eflags.S to reg */
903
static CCPrepare gen_prepare_eflags_s(DisasContext *s, TCGv reg)
904
{
905 906 907 908 909
    switch (s->cc_op) {
    case CC_OP_DYNAMIC:
        gen_compute_eflags(s);
        /* FALLTHRU */
    case CC_OP_EFLAGS:
910 911 912
    case CC_OP_ADCX:
    case CC_OP_ADOX:
    case CC_OP_ADCOX:
913 914
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = CC_S };
R
Richard Henderson 已提交
915 916
    case CC_OP_CLR:
        return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
917 918
    default:
        {
919
            TCGMemOp size = (s->cc_op - CC_OP_ADDB) & 3;
920
            TCGv t0 = gen_ext_tl(reg, cpu_cc_dst, size, true);
921
            return (CCPrepare) { .cond = TCG_COND_LT, .reg = t0, .mask = -1 };
922 923
        }
    }
924 925 926
}

/* compute eflags.O to reg */
927
static CCPrepare gen_prepare_eflags_o(DisasContext *s, TCGv reg)
928
{
929 930 931 932 933
    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 已提交
934 935
    case CC_OP_CLR:
        return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
936 937 938 939 940
    default:
        gen_compute_eflags(s);
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = CC_O };
    }
941 942 943
}

/* compute eflags.Z to reg */
944
static CCPrepare gen_prepare_eflags_z(DisasContext *s, TCGv reg)
945
{
946 947 948 949 950
    switch (s->cc_op) {
    case CC_OP_DYNAMIC:
        gen_compute_eflags(s);
        /* FALLTHRU */
    case CC_OP_EFLAGS:
951 952 953
    case CC_OP_ADCX:
    case CC_OP_ADOX:
    case CC_OP_ADCOX:
954 955
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = CC_Z };
R
Richard Henderson 已提交
956 957
    case CC_OP_CLR:
        return (CCPrepare) { .cond = TCG_COND_ALWAYS, .mask = -1 };
958 959
    default:
        {
960
            TCGMemOp size = (s->cc_op - CC_OP_ADDB) & 3;
961
            TCGv t0 = gen_ext_tl(reg, cpu_cc_dst, size, false);
962
            return (CCPrepare) { .cond = TCG_COND_EQ, .reg = t0, .mask = -1 };
963
        }
964 965 966
    }
}

967 968
/* perform a conditional store into register 'reg' according to jump opcode
   value 'b'. In the fast case, T0 is guaranted not to be used. */
969
static CCPrepare gen_prepare_cc(DisasContext *s, int b, TCGv reg)
970
{
971 972
    int inv, jcc_op, cond;
    TCGMemOp size;
973
    CCPrepare cc;
974 975 976
    TCGv t0;

    inv = b & 1;
977
    jcc_op = (b >> 1) & 7;
978 979

    switch (s->cc_op) {
980 981
    case CC_OP_SUBB ... CC_OP_SUBQ:
        /* We optimize relational operators for the cmp/jcc case.  */
982 983 984
        size = s->cc_op - CC_OP_SUBB;
        switch (jcc_op) {
        case JCC_BE:
985
            tcg_gen_mov_tl(cpu_tmp4, cpu_cc_srcT);
986 987
            gen_extu(size, cpu_tmp4);
            t0 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, false);
988 989
            cc = (CCPrepare) { .cond = TCG_COND_LEU, .reg = cpu_tmp4,
                               .reg2 = t0, .mask = -1, .use_reg2 = true };
990
            break;
991

992
        case JCC_L:
993
            cond = TCG_COND_LT;
994 995
            goto fast_jcc_l;
        case JCC_LE:
996
            cond = TCG_COND_LE;
997
        fast_jcc_l:
998
            tcg_gen_mov_tl(cpu_tmp4, cpu_cc_srcT);
999 1000
            gen_exts(size, cpu_tmp4);
            t0 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, true);
1001 1002
            cc = (CCPrepare) { .cond = cond, .reg = cpu_tmp4,
                               .reg2 = t0, .mask = -1, .use_reg2 = true };
1003
            break;
1004

1005
        default:
1006
            goto slow_jcc;
1007
        }
1008
        break;
1009

1010 1011
    default:
    slow_jcc:
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
        /* 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;
        }
1056
        break;
1057
    }
1058 1059 1060 1061 1062

    if (inv) {
        cc.cond = tcg_invert_cond(cc.cond);
    }
    return cc;
1063 1064
}

1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
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);
}
1099

1100 1101
/* generate a conditional jump to label 'l1' according to jump opcode
   value 'b'. In the fast case, T0 is guaranted not to be used. */
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
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.  */
1120
static inline void gen_jcc1(DisasContext *s, int b, int l1)
1121
{
1122
    CCPrepare cc = gen_prepare_cc(s, b, cpu_T[0]);
1123

1124
    gen_update_cc_op(s);
1125 1126 1127 1128
    if (cc.mask != -1) {
        tcg_gen_andi_tl(cpu_T[0], cc.reg, cc.mask);
        cc.reg = cpu_T[0];
    }
1129
    set_cc_op(s, CC_OP_DYNAMIC);
1130 1131 1132 1133
    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);
1134 1135 1136
    }
}

B
bellard 已提交
1137 1138 1139
/* 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 已提交
1140
{
B
bellard 已提交
1141 1142 1143 1144
    int l1, l2;

    l1 = gen_new_label();
    l2 = gen_new_label();
1145
    gen_op_jnz_ecx(s->aflag, l1);
B
bellard 已提交
1146 1147 1148 1149
    gen_set_label(l2);
    gen_jmp_tb(s, next_eip, 1);
    gen_set_label(l1);
    return l2;
B
bellard 已提交
1150 1151
}

1152
static inline void gen_stos(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
1153
{
1154
    gen_op_mov_TN_reg(MO_32, 0, R_EAX);
B
bellard 已提交
1155
    gen_string_movl_A0_EDI(s);
1156
    gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
1157
    gen_op_movl_T0_Dshift(ot);
1158
    gen_op_add_reg_T0(s->aflag, R_EDI);
B
bellard 已提交
1159 1160
}

1161
static inline void gen_lods(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
1162 1163
{
    gen_string_movl_A0_ESI(s);
1164
    gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
1165
    gen_op_mov_reg_T0(ot, R_EAX);
1166
    gen_op_movl_T0_Dshift(ot);
1167
    gen_op_add_reg_T0(s->aflag, R_ESI);
B
bellard 已提交
1168 1169
}

1170
static inline void gen_scas(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
1171 1172
{
    gen_string_movl_A0_EDI(s);
1173
    gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
1174
    gen_op(s, OP_CMPL, ot, R_EAX);
1175
    gen_op_movl_T0_Dshift(ot);
1176
    gen_op_add_reg_T0(s->aflag, R_EDI);
B
bellard 已提交
1177 1178
}

1179
static inline void gen_cmps(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
1180 1181
{
    gen_string_movl_A0_EDI(s);
1182
    gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
1183 1184
    gen_string_movl_A0_ESI(s);
    gen_op(s, OP_CMPL, ot, OR_TMP0);
1185
    gen_op_movl_T0_Dshift(ot);
1186 1187
    gen_op_add_reg_T0(s->aflag, R_ESI);
    gen_op_add_reg_T0(s->aflag, R_EDI);
B
bellard 已提交
1188 1189
}

1190
static inline void gen_ins(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
1191
{
P
pbrook 已提交
1192 1193
    if (use_icount)
        gen_io_start();
B
bellard 已提交
1194
    gen_string_movl_A0_EDI(s);
1195 1196
    /* Note: we must do this dummy write first to be restartable in
       case of page fault. */
1197
    tcg_gen_movi_tl(cpu_T[0], 0);
1198
    gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
1199
    tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_EDX]);
1200
    tcg_gen_andi_i32(cpu_tmp2_i32, cpu_tmp2_i32, 0xffff);
P
pbrook 已提交
1201
    gen_helper_in_func(ot, cpu_T[0], cpu_tmp2_i32);
1202
    gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
1203
    gen_op_movl_T0_Dshift(ot);
1204
    gen_op_add_reg_T0(s->aflag, R_EDI);
P
pbrook 已提交
1205 1206
    if (use_icount)
        gen_io_end();
B
bellard 已提交
1207 1208
}

1209
static inline void gen_outs(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
1210
{
P
pbrook 已提交
1211 1212
    if (use_icount)
        gen_io_start();
B
bellard 已提交
1213
    gen_string_movl_A0_ESI(s);
1214
    gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
1215

1216
    tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_EDX]);
1217 1218
    tcg_gen_andi_i32(cpu_tmp2_i32, cpu_tmp2_i32, 0xffff);
    tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[0]);
P
pbrook 已提交
1219
    gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
1220

1221
    gen_op_movl_T0_Dshift(ot);
1222
    gen_op_add_reg_T0(s->aflag, R_ESI);
P
pbrook 已提交
1223 1224
    if (use_icount)
        gen_io_end();
B
bellard 已提交
1225 1226 1227 1228 1229
}

/* same method as Valgrind : we generate jumps to current or next
   instruction */
#define GEN_REPZ(op)                                                          \
1230
static inline void gen_repz_ ## op(DisasContext *s, TCGMemOp ot,              \
B
bellard 已提交
1231
                                 target_ulong cur_eip, target_ulong next_eip) \
B
bellard 已提交
1232
{                                                                             \
B
bellard 已提交
1233
    int l2;\
B
bellard 已提交
1234
    gen_update_cc_op(s);                                                      \
B
bellard 已提交
1235
    l2 = gen_jz_ecx_string(s, next_eip);                                      \
B
bellard 已提交
1236
    gen_ ## op(s, ot);                                                        \
1237
    gen_op_add_reg_im(s->aflag, R_ECX, -1);                                   \
B
bellard 已提交
1238 1239 1240
    /* a loop would cause two single step exceptions if ECX = 1               \
       before rep string_insn */                                              \
    if (!s->jmp_opt)                                                          \
1241
        gen_op_jz_ecx(s->aflag, l2);                                          \
B
bellard 已提交
1242 1243 1244 1245
    gen_jmp(s, cur_eip);                                                      \
}

#define GEN_REPZ2(op)                                                         \
1246
static inline void gen_repz_ ## op(DisasContext *s, TCGMemOp ot,              \
B
bellard 已提交
1247 1248
                                   target_ulong cur_eip,                      \
                                   target_ulong next_eip,                     \
B
bellard 已提交
1249 1250
                                   int nz)                                    \
{                                                                             \
B
bellard 已提交
1251
    int l2;\
B
bellard 已提交
1252
    gen_update_cc_op(s);                                                      \
B
bellard 已提交
1253
    l2 = gen_jz_ecx_string(s, next_eip);                                      \
B
bellard 已提交
1254
    gen_ ## op(s, ot);                                                        \
1255
    gen_op_add_reg_im(s->aflag, R_ECX, -1);                                   \
1256
    gen_update_cc_op(s);                                                      \
1257
    gen_jcc1(s, (JCC_Z << 1) | (nz ^ 1), l2);                                 \
B
bellard 已提交
1258
    if (!s->jmp_opt)                                                          \
1259
        gen_op_jz_ecx(s->aflag, l2);                                          \
B
bellard 已提交
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
    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 已提交
1271 1272 1273
static void gen_helper_fp_arith_ST0_FT0(int op)
{
    switch (op) {
B
Blue Swirl 已提交
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
    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 已提交
1298 1299
    }
}
B
bellard 已提交
1300 1301

/* NOTE the exception in "r" op ordering */
P
pbrook 已提交
1302 1303 1304 1305
static void gen_helper_fp_arith_STN_ST0(int op, int opreg)
{
    TCGv_i32 tmp = tcg_const_i32(opreg);
    switch (op) {
B
Blue Swirl 已提交
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
    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 已提交
1324 1325
    }
}
B
bellard 已提交
1326 1327

/* if d == OR_TMP0, it means memory operand (address in A0) */
1328
static void gen_op(DisasContext *s1, int op, TCGMemOp ot, int d)
B
bellard 已提交
1329 1330
{
    if (d != OR_TMP0) {
B
bellard 已提交
1331
        gen_op_mov_TN_reg(ot, 0, d);
B
bellard 已提交
1332
    } else {
1333
        gen_op_ld_v(s1, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
1334 1335 1336
    }
    switch(op) {
    case OP_ADCL:
1337
        gen_compute_eflags_c(s1, cpu_tmp4);
B
bellard 已提交
1338 1339
        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);
1340
        gen_op_st_rm_T0_A0(s1, ot, d);
1341 1342
        gen_op_update3_cc(cpu_tmp4);
        set_cc_op(s1, CC_OP_ADCB + ot);
B
bellard 已提交
1343
        break;
B
bellard 已提交
1344
    case OP_SBBL:
1345
        gen_compute_eflags_c(s1, cpu_tmp4);
B
bellard 已提交
1346 1347
        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);
1348
        gen_op_st_rm_T0_A0(s1, ot, d);
1349 1350
        gen_op_update3_cc(cpu_tmp4);
        set_cc_op(s1, CC_OP_SBBB + ot);
B
bellard 已提交
1351
        break;
B
bellard 已提交
1352 1353
    case OP_ADDL:
        gen_op_addl_T0_T1();
1354
        gen_op_st_rm_T0_A0(s1, ot, d);
B
bellard 已提交
1355
        gen_op_update2_cc();
1356
        set_cc_op(s1, CC_OP_ADDB + ot);
B
bellard 已提交
1357 1358
        break;
    case OP_SUBL:
1359
        tcg_gen_mov_tl(cpu_cc_srcT, cpu_T[0]);
B
bellard 已提交
1360
        tcg_gen_sub_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1361
        gen_op_st_rm_T0_A0(s1, ot, d);
B
bellard 已提交
1362
        gen_op_update2_cc();
1363
        set_cc_op(s1, CC_OP_SUBB + ot);
B
bellard 已提交
1364 1365 1366
        break;
    default:
    case OP_ANDL:
B
bellard 已提交
1367
        tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1368
        gen_op_st_rm_T0_A0(s1, ot, d);
B
bellard 已提交
1369
        gen_op_update1_cc();
1370
        set_cc_op(s1, CC_OP_LOGICB + ot);
B
bellard 已提交
1371
        break;
B
bellard 已提交
1372
    case OP_ORL:
B
bellard 已提交
1373
        tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1374
        gen_op_st_rm_T0_A0(s1, ot, d);
B
bellard 已提交
1375
        gen_op_update1_cc();
1376
        set_cc_op(s1, CC_OP_LOGICB + ot);
B
bellard 已提交
1377
        break;
B
bellard 已提交
1378
    case OP_XORL:
B
bellard 已提交
1379
        tcg_gen_xor_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1380
        gen_op_st_rm_T0_A0(s1, ot, d);
B
bellard 已提交
1381
        gen_op_update1_cc();
1382
        set_cc_op(s1, CC_OP_LOGICB + ot);
B
bellard 已提交
1383 1384
        break;
    case OP_CMPL:
1385
        tcg_gen_mov_tl(cpu_cc_src, cpu_T[1]);
1386
        tcg_gen_mov_tl(cpu_cc_srcT, cpu_T[0]);
1387
        tcg_gen_sub_tl(cpu_cc_dst, cpu_T[0], cpu_T[1]);
1388
        set_cc_op(s1, CC_OP_SUBB + ot);
B
bellard 已提交
1389 1390
        break;
    }
1391 1392
}

B
bellard 已提交
1393
/* if d == OR_TMP0, it means memory operand (address in A0) */
1394
static void gen_inc(DisasContext *s1, TCGMemOp ot, int d, int c)
B
bellard 已提交
1395
{
1396
    if (d != OR_TMP0) {
B
bellard 已提交
1397
        gen_op_mov_TN_reg(ot, 0, d);
1398 1399 1400
    } else {
        gen_op_ld_v(s1, ot, cpu_T[0], cpu_A0);
    }
1401
    gen_compute_eflags_c(s1, cpu_cc_src);
B
bellard 已提交
1402
    if (c > 0) {
1403
        tcg_gen_addi_tl(cpu_T[0], cpu_T[0], 1);
1404
        set_cc_op(s1, CC_OP_INCB + ot);
B
bellard 已提交
1405
    } else {
1406
        tcg_gen_addi_tl(cpu_T[0], cpu_T[0], -1);
1407
        set_cc_op(s1, CC_OP_DECB + ot);
B
bellard 已提交
1408
    }
1409
    gen_op_st_rm_T0_A0(s1, ot, d);
B
bellard 已提交
1410
    tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
B
bellard 已提交
1411 1412
}

1413 1414
static void gen_shift_flags(DisasContext *s, TCGMemOp ot, TCGv result,
                            TCGv shm1, TCGv count, bool is_right)
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
{
    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);
}

1458
static void gen_shift_rm_T1(DisasContext *s, TCGMemOp ot, int op1,
1459
                            int is_right, int is_arith)
B
bellard 已提交
1460
{
1461
    target_ulong mask = (ot == MO_64 ? 0x3f : 0x1f);
1462

1463
    /* load */
1464
    if (op1 == OR_TMP0) {
1465
        gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
1466
    } else {
1467
        gen_op_mov_TN_reg(ot, 0, op1);
1468
    }
1469

1470 1471
    tcg_gen_andi_tl(cpu_T[1], cpu_T[1], mask);
    tcg_gen_subi_tl(cpu_tmp0, cpu_T[1], 1);
1472 1473 1474

    if (is_right) {
        if (is_arith) {
B
bellard 已提交
1475
            gen_exts(ot, cpu_T[0]);
1476 1477
            tcg_gen_sar_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
            tcg_gen_sar_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1478
        } else {
B
bellard 已提交
1479
            gen_extu(ot, cpu_T[0]);
1480 1481
            tcg_gen_shr_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
            tcg_gen_shr_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1482 1483
        }
    } else {
1484 1485
        tcg_gen_shl_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
        tcg_gen_shl_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1486 1487 1488
    }

    /* store */
1489
    gen_op_st_rm_T0_A0(s, ot, op1);
1490

1491
    gen_shift_flags(s, ot, cpu_T[0], cpu_tmp0, cpu_T[1], is_right);
1492 1493
}

1494
static void gen_shift_rm_im(DisasContext *s, TCGMemOp ot, int op1, int op2,
B
bellard 已提交
1495 1496
                            int is_right, int is_arith)
{
1497
    int mask = (ot == MO_64 ? 0x3f : 0x1f);
B
bellard 已提交
1498 1499 1500

    /* load */
    if (op1 == OR_TMP0)
1501
        gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
1502 1503 1504 1505 1506 1507 1508 1509
    else
        gen_op_mov_TN_reg(ot, 0, op1);

    op2 &= mask;
    if (op2 != 0) {
        if (is_right) {
            if (is_arith) {
                gen_exts(ot, cpu_T[0]);
B
bellard 已提交
1510
                tcg_gen_sari_tl(cpu_tmp4, cpu_T[0], op2 - 1);
B
bellard 已提交
1511 1512 1513
                tcg_gen_sari_tl(cpu_T[0], cpu_T[0], op2);
            } else {
                gen_extu(ot, cpu_T[0]);
B
bellard 已提交
1514
                tcg_gen_shri_tl(cpu_tmp4, cpu_T[0], op2 - 1);
B
bellard 已提交
1515 1516 1517
                tcg_gen_shri_tl(cpu_T[0], cpu_T[0], op2);
            }
        } else {
B
bellard 已提交
1518
            tcg_gen_shli_tl(cpu_tmp4, cpu_T[0], op2 - 1);
B
bellard 已提交
1519 1520 1521 1522 1523
            tcg_gen_shli_tl(cpu_T[0], cpu_T[0], op2);
        }
    }

    /* store */
1524 1525
    gen_op_st_rm_T0_A0(s, ot, op1);

B
bellard 已提交
1526 1527
    /* update eflags if non zero shift */
    if (op2 != 0) {
B
bellard 已提交
1528
        tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
B
bellard 已提交
1529
        tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
1530
        set_cc_op(s, (is_right ? CC_OP_SARB : CC_OP_SHLB) + ot);
B
bellard 已提交
1531 1532 1533
    }
}

1534 1535 1536 1537 1538 1539 1540 1541
static inline void tcg_gen_lshift(TCGv ret, TCGv arg1, target_long arg2)
{
    if (arg2 >= 0)
        tcg_gen_shli_tl(ret, arg1, arg2);
    else
        tcg_gen_shri_tl(ret, arg1, -arg2);
}

1542
static void gen_rot_rm_T1(DisasContext *s, TCGMemOp ot, int op1, int is_right)
1543
{
1544
    target_ulong mask = (ot == MO_64 ? 0x3f : 0x1f);
1545
    TCGv_i32 t0, t1;
1546 1547

    /* load */
1548
    if (op1 == OR_TMP0) {
1549
        gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
1550
    } else {
1551
        gen_op_mov_TN_reg(ot, 0, op1);
1552
    }
1553

1554
    tcg_gen_andi_tl(cpu_T[1], cpu_T[1], mask);
1555

1556
    switch (ot) {
1557
    case MO_8:
1558 1559 1560 1561
        /* 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;
1562
    case MO_16:
1563 1564 1565 1566 1567
        /* 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
1568
    case MO_32:
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
        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;
1586 1587 1588
    }

    /* store */
1589
    gen_op_st_rm_T0_A0(s, ot, op1);
1590

1591 1592
    /* We'll need the flags computed into CC_SRC.  */
    gen_compute_eflags(s);
1593

1594 1595 1596 1597
    /* 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.  */
1598
    if (is_right) {
1599 1600
        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 已提交
1601
        tcg_gen_andi_tl(cpu_cc_dst, cpu_cc_dst, 1);
1602 1603 1604
    } else {
        tcg_gen_shri_tl(cpu_cc_src2, cpu_T[0], mask);
        tcg_gen_andi_tl(cpu_cc_dst, cpu_T[0], 1);
1605
    }
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
    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);
1625 1626
}

1627
static void gen_rot_rm_im(DisasContext *s, TCGMemOp ot, int op1, int op2,
M
malc 已提交
1628 1629
                          int is_right)
{
1630
    int mask = (ot == MO_64 ? 0x3f : 0x1f);
1631
    int shift;
M
malc 已提交
1632 1633 1634

    /* load */
    if (op1 == OR_TMP0) {
1635
        gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
M
malc 已提交
1636
    } else {
1637
        gen_op_mov_TN_reg(ot, 0, op1);
M
malc 已提交
1638 1639 1640 1641
    }

    op2 &= mask;
    if (op2 != 0) {
1642 1643
        switch (ot) {
#ifdef TARGET_X86_64
1644
        case MO_32:
1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660
            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;
1661
        case MO_8:
1662 1663
            mask = 7;
            goto do_shifts;
1664
        case MO_16:
1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675
            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 已提交
1676 1677 1678 1679
        }
    }

    /* store */
1680
    gen_op_st_rm_T0_A0(s, ot, op1);
M
malc 已提交
1681 1682

    if (op2 != 0) {
1683
        /* Compute the flags into CC_SRC.  */
1684
        gen_compute_eflags(s);
1685

1686 1687 1688 1689
        /* 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 已提交
1690
        if (is_right) {
1691 1692
            tcg_gen_shri_tl(cpu_cc_src2, cpu_T[0], mask - 1);
            tcg_gen_shri_tl(cpu_cc_dst, cpu_T[0], mask);
1693
            tcg_gen_andi_tl(cpu_cc_dst, cpu_cc_dst, 1);
1694 1695 1696
        } 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 已提交
1697
        }
1698 1699 1700
        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 已提交
1701 1702 1703
    }
}

1704
/* XXX: add faster immediate = 1 case */
1705
static void gen_rotc_rm_T1(DisasContext *s, TCGMemOp ot, int op1,
1706 1707
                           int is_right)
{
1708
    gen_compute_eflags(s);
1709
    assert(s->cc_op == CC_OP_EFLAGS);
1710 1711 1712

    /* load */
    if (op1 == OR_TMP0)
1713
        gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
1714 1715 1716
    else
        gen_op_mov_TN_reg(ot, 0, op1);
    
P
pbrook 已提交
1717 1718
    if (is_right) {
        switch (ot) {
1719
        case MO_8:
1720 1721
            gen_helper_rcrb(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
1722
        case MO_16:
1723 1724
            gen_helper_rcrw(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
1725
        case MO_32:
1726 1727
            gen_helper_rcrl(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1728
#ifdef TARGET_X86_64
1729
        case MO_64:
1730 1731
            gen_helper_rcrq(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1732
#endif
1733 1734
        default:
            tcg_abort();
P
pbrook 已提交
1735 1736 1737
        }
    } else {
        switch (ot) {
1738
        case MO_8:
1739 1740
            gen_helper_rclb(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
1741
        case MO_16:
1742 1743
            gen_helper_rclw(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
1744
        case MO_32:
1745 1746
            gen_helper_rcll(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1747
#ifdef TARGET_X86_64
1748
        case MO_64:
1749 1750
            gen_helper_rclq(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1751
#endif
1752 1753
        default:
            tcg_abort();
P
pbrook 已提交
1754 1755
        }
    }
1756
    /* store */
1757
    gen_op_st_rm_T0_A0(s, ot, op1);
1758 1759 1760
}

/* XXX: add faster immediate case */
1761
static void gen_shiftd_rm_T1(DisasContext *s, TCGMemOp ot, int op1,
1762
                             bool is_right, TCGv count_in)
1763
{
1764
    target_ulong mask = (ot == MO_64 ? 63 : 31);
1765
    TCGv count;
1766 1767

    /* load */
1768
    if (op1 == OR_TMP0) {
1769
        gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
1770
    } else {
1771
        gen_op_mov_TN_reg(ot, 0, op1);
1772
    }
1773

1774 1775
    count = tcg_temp_new();
    tcg_gen_andi_tl(count, count_in, mask);
1776

1777
    switch (ot) {
1778
    case MO_16:
1779 1780 1781
        /* 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.  */
1782
        if (is_right) {
1783 1784 1785
            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);
1786
        } else {
1787
            tcg_gen_deposit_tl(cpu_T[1], cpu_T[0], cpu_T[1], 16, 16);
1788
        }
1789 1790
        /* FALLTHRU */
#ifdef TARGET_X86_64
1791
    case MO_32:
1792 1793
        /* Concatenate the two 32-bit values and use a 64-bit shift.  */
        tcg_gen_subi_tl(cpu_tmp0, count, 1);
1794
        if (is_right) {
1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810
            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);
1811

1812 1813 1814
            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);
1815
        } else {
1816
            tcg_gen_shl_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
1817
            if (ot == MO_16) {
1818 1819 1820 1821 1822 1823 1824 1825 1826
                /* 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);
1827
        }
1828 1829 1830 1831 1832
        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;
1833 1834 1835
    }

    /* store */
1836
    gen_op_st_rm_T0_A0(s, ot, op1);
1837

1838 1839
    gen_shift_flags(s, ot, cpu_T[0], cpu_tmp0, count, is_right);
    tcg_temp_free(count);
1840 1841
}

1842
static void gen_shift(DisasContext *s1, int op, TCGMemOp ot, int d, int s)
1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869
{
    if (s != OR_TMP1)
        gen_op_mov_TN_reg(ot, 1, s);
    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 已提交
1870 1871
}

1872
static void gen_shifti(DisasContext *s1, int op, TCGMemOp ot, int d, int c)
B
bellard 已提交
1873
{
B
bellard 已提交
1874
    switch(op) {
M
malc 已提交
1875 1876 1877 1878 1879 1880
    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 已提交
1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892
    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 */
1893
        tcg_gen_movi_tl(cpu_T[1], c);
B
bellard 已提交
1894 1895 1896
        gen_shift(s1, op, ot, d, OR_TMP1);
        break;
    }
B
bellard 已提交
1897 1898
}

1899
static void gen_lea_modrm(CPUX86State *env, DisasContext *s, int modrm)
B
bellard 已提交
1900
{
B
bellard 已提交
1901
    target_long disp;
B
bellard 已提交
1902
    int havesib;
B
bellard 已提交
1903
    int base;
B
bellard 已提交
1904 1905 1906
    int index;
    int scale;
    int mod, rm, code, override, must_add_seg;
1907
    TCGv sum;
B
bellard 已提交
1908 1909 1910 1911 1912 1913 1914 1915

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

1916 1917 1918
    switch (s->aflag) {
    case MO_64:
    case MO_32:
B
bellard 已提交
1919 1920
        havesib = 0;
        base = rm;
1921
        index = -1;
B
bellard 已提交
1922
        scale = 0;
1923

B
bellard 已提交
1924 1925
        if (base == 4) {
            havesib = 1;
1926
            code = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
1927
            scale = (code >> 6) & 3;
B
bellard 已提交
1928
            index = ((code >> 3) & 7) | REX_X(s);
1929 1930 1931
            if (index == 4) {
                index = -1;  /* no index */
            }
B
bellard 已提交
1932
            base = (code & 7);
B
bellard 已提交
1933
        }
B
bellard 已提交
1934
        base |= REX_B(s);
B
bellard 已提交
1935 1936 1937

        switch (mod) {
        case 0:
B
bellard 已提交
1938
            if ((base & 7) == 5) {
B
bellard 已提交
1939
                base = -1;
1940
                disp = (int32_t)cpu_ldl_code(env, s->pc);
B
bellard 已提交
1941
                s->pc += 4;
B
bellard 已提交
1942 1943 1944
                if (CODE64(s) && !havesib) {
                    disp += s->pc + s->rip_offset;
                }
B
bellard 已提交
1945 1946 1947 1948 1949
            } else {
                disp = 0;
            }
            break;
        case 1:
1950
            disp = (int8_t)cpu_ldub_code(env, s->pc++);
B
bellard 已提交
1951 1952 1953
            break;
        default:
        case 2:
1954
            disp = (int32_t)cpu_ldl_code(env, s->pc);
B
bellard 已提交
1955 1956 1957
            s->pc += 4;
            break;
        }
1958

1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
        /* 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 已提交
1972
            }
1973 1974 1975
            if (base >= 0) {
                tcg_gen_add_tl(cpu_A0, sum, cpu_regs[base]);
                sum = cpu_A0;
B
bellard 已提交
1976
            }
1977 1978
        } else if (base >= 0) {
            sum = cpu_regs[base];
B
bellard 已提交
1979
        }
1980 1981 1982 1983
        if (TCGV_IS_UNUSED(sum)) {
            tcg_gen_movi_tl(cpu_A0, disp);
        } else {
            tcg_gen_addi_tl(cpu_A0, sum, disp);
B
bellard 已提交
1984
        }
1985

B
bellard 已提交
1986 1987
        if (must_add_seg) {
            if (override < 0) {
1988
                if (base == R_EBP || base == R_ESP) {
B
bellard 已提交
1989
                    override = R_SS;
1990
                } else {
B
bellard 已提交
1991
                    override = R_DS;
1992
                }
B
bellard 已提交
1993
            }
1994 1995 1996 1997

            tcg_gen_ld_tl(cpu_tmp0, cpu_env,
                          offsetof(CPUX86State, segs[override].base));
            if (CODE64(s)) {
1998
                if (s->aflag == MO_32) {
1999 2000 2001
                    tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
                }
                tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
2002
                return;
B
bellard 已提交
2003
            }
2004 2005 2006 2007

            tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
        }

2008
        if (s->aflag == MO_32) {
2009
            tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
B
bellard 已提交
2010
        }
2011 2012 2013
        break;

    case MO_16:
B
bellard 已提交
2014 2015 2016
        switch (mod) {
        case 0:
            if (rm == 6) {
2017
                disp = cpu_lduw_code(env, s->pc);
B
bellard 已提交
2018
                s->pc += 2;
2019
                tcg_gen_movi_tl(cpu_A0, disp);
B
bellard 已提交
2020 2021 2022 2023 2024 2025 2026
                rm = 0; /* avoid SS override */
                goto no_rm;
            } else {
                disp = 0;
            }
            break;
        case 1:
2027
            disp = (int8_t)cpu_ldub_code(env, s->pc++);
B
bellard 已提交
2028 2029 2030
            break;
        default:
        case 2:
2031
            disp = (int16_t)cpu_lduw_code(env, s->pc);
B
bellard 已提交
2032 2033 2034
            s->pc += 2;
            break;
        }
2035 2036 2037

        sum = cpu_A0;
        switch (rm) {
B
bellard 已提交
2038
        case 0:
2039
            tcg_gen_add_tl(cpu_A0, cpu_regs[R_EBX], cpu_regs[R_ESI]);
B
bellard 已提交
2040 2041
            break;
        case 1:
2042
            tcg_gen_add_tl(cpu_A0, cpu_regs[R_EBX], cpu_regs[R_EDI]);
B
bellard 已提交
2043 2044
            break;
        case 2:
2045
            tcg_gen_add_tl(cpu_A0, cpu_regs[R_EBP], cpu_regs[R_ESI]);
B
bellard 已提交
2046 2047
            break;
        case 3:
2048
            tcg_gen_add_tl(cpu_A0, cpu_regs[R_EBP], cpu_regs[R_EDI]);
B
bellard 已提交
2049 2050
            break;
        case 4:
2051
            sum = cpu_regs[R_ESI];
B
bellard 已提交
2052 2053
            break;
        case 5:
2054
            sum = cpu_regs[R_EDI];
B
bellard 已提交
2055 2056
            break;
        case 6:
2057
            sum = cpu_regs[R_EBP];
B
bellard 已提交
2058 2059 2060
            break;
        default:
        case 7:
2061
            sum = cpu_regs[R_EBX];
B
bellard 已提交
2062 2063
            break;
        }
2064
        tcg_gen_addi_tl(cpu_A0, sum, disp);
2065
        tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
B
bellard 已提交
2066 2067 2068
    no_rm:
        if (must_add_seg) {
            if (override < 0) {
2069
                if (rm == 2 || rm == 3 || rm == 6) {
B
bellard 已提交
2070
                    override = R_SS;
2071
                } else {
B
bellard 已提交
2072
                    override = R_DS;
2073
                }
B
bellard 已提交
2074
            }
2075
            gen_op_addl_A0_seg(s, override);
B
bellard 已提交
2076
        }
2077 2078 2079 2080
        break;

    default:
        tcg_abort();
B
bellard 已提交
2081 2082 2083
    }
}

2084
static void gen_nop_modrm(CPUX86State *env, DisasContext *s, int modrm)
B
bellard 已提交
2085 2086 2087 2088 2089 2090 2091 2092
{
    int mod, rm, base, code;

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

2093 2094 2095
    switch (s->aflag) {
    case MO_64:
    case MO_32:
B
bellard 已提交
2096
        base = rm;
2097

B
bellard 已提交
2098
        if (base == 4) {
2099
            code = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
2100 2101
            base = (code & 7);
        }
2102

B
bellard 已提交
2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116
        switch (mod) {
        case 0:
            if (base == 5) {
                s->pc += 4;
            }
            break;
        case 1:
            s->pc++;
            break;
        default:
        case 2:
            s->pc += 4;
            break;
        }
2117 2118 2119
        break;

    case MO_16:
B
bellard 已提交
2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133
        switch (mod) {
        case 0:
            if (rm == 6) {
                s->pc += 2;
            }
            break;
        case 1:
            s->pc++;
            break;
        default:
        case 2:
            s->pc += 2;
            break;
        }
2134 2135 2136 2137
        break;

    default:
        tcg_abort();
B
bellard 已提交
2138 2139 2140
    }
}

B
bellard 已提交
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151
/* 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) {
2152 2153
#ifdef TARGET_X86_64
        if (CODE64(s)) {
B
bellard 已提交
2154
            gen_op_addq_A0_seg(override);
2155
        } else
2156 2157
#endif
        {
2158
            gen_op_addl_A0_seg(s, override);
2159
        }
B
bellard 已提交
2160 2161 2162
    }
}

B
balrog 已提交
2163
/* generate modrm memory load or store of 'reg'. TMP0 is used if reg ==
B
bellard 已提交
2164
   OR_TMP0 */
2165
static void gen_ldst_modrm(CPUX86State *env, DisasContext *s, int modrm,
2166
                           TCGMemOp ot, int reg, int is_store)
B
bellard 已提交
2167
{
2168
    int mod, rm;
B
bellard 已提交
2169 2170

    mod = (modrm >> 6) & 3;
B
bellard 已提交
2171
    rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
2172 2173 2174
    if (mod == 3) {
        if (is_store) {
            if (reg != OR_TMP0)
B
bellard 已提交
2175 2176
                gen_op_mov_TN_reg(ot, 0, reg);
            gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
2177
        } else {
B
bellard 已提交
2178
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
2179
            if (reg != OR_TMP0)
B
bellard 已提交
2180
                gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
2181 2182
        }
    } else {
2183
        gen_lea_modrm(env, s, modrm);
B
bellard 已提交
2184 2185
        if (is_store) {
            if (reg != OR_TMP0)
B
bellard 已提交
2186
                gen_op_mov_TN_reg(ot, 0, reg);
2187
            gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
2188
        } else {
2189
            gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
2190
            if (reg != OR_TMP0)
B
bellard 已提交
2191
                gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
2192 2193 2194 2195
        }
    }
}

2196
static inline uint32_t insn_get(CPUX86State *env, DisasContext *s, TCGMemOp ot)
B
bellard 已提交
2197 2198 2199
{
    uint32_t ret;

2200
    switch (ot) {
2201
    case MO_8:
2202
        ret = cpu_ldub_code(env, s->pc);
B
bellard 已提交
2203 2204
        s->pc++;
        break;
2205
    case MO_16:
2206
        ret = cpu_lduw_code(env, s->pc);
B
bellard 已提交
2207 2208
        s->pc += 2;
        break;
2209
    case MO_32:
2210 2211 2212
#ifdef TARGET_X86_64
    case MO_64:
#endif
2213
        ret = cpu_ldl_code(env, s->pc);
B
bellard 已提交
2214 2215
        s->pc += 4;
        break;
2216 2217
    default:
        tcg_abort();
B
bellard 已提交
2218 2219 2220 2221
    }
    return ret;
}

2222
static inline int insn_const_size(TCGMemOp ot)
B
bellard 已提交
2223
{
2224
    if (ot <= MO_32) {
B
bellard 已提交
2225
        return 1 << ot;
2226
    } else {
B
bellard 已提交
2227
        return 4;
2228
    }
B
bellard 已提交
2229 2230
}

2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241
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 已提交
2242
        tcg_gen_goto_tb(tb_num);
2243
        gen_jmp_im(eip);
2244
        tcg_gen_exit_tb((uintptr_t)tb + tb_num);
2245 2246 2247 2248 2249 2250 2251
    } else {
        /* jump to another page: currently not optimized */
        gen_jmp_im(eip);
        gen_eob(s);
    }
}

2252
static inline void gen_jcc(DisasContext *s, int b,
B
bellard 已提交
2253
                           target_ulong val, target_ulong next_eip)
B
bellard 已提交
2254
{
2255
    int l1, l2;
2256

B
bellard 已提交
2257
    if (s->jmp_opt) {
B
bellard 已提交
2258
        l1 = gen_new_label();
2259
        gen_jcc1(s, b, l1);
2260

2261
        gen_goto_tb(s, 0, next_eip);
B
bellard 已提交
2262 2263

        gen_set_label(l1);
2264
        gen_goto_tb(s, 1, val);
J
Jun Koi 已提交
2265
        s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2266
    } else {
B
bellard 已提交
2267 2268
        l1 = gen_new_label();
        l2 = gen_new_label();
2269
        gen_jcc1(s, b, l1);
2270

B
bellard 已提交
2271
        gen_jmp_im(next_eip);
2272 2273
        tcg_gen_br(l2);

B
bellard 已提交
2274 2275 2276
        gen_set_label(l1);
        gen_jmp_im(val);
        gen_set_label(l2);
B
bellard 已提交
2277 2278 2279 2280
        gen_eob(s);
    }
}

2281
static void gen_cmovcc1(CPUX86State *env, DisasContext *s, TCGMemOp ot, int b,
2282 2283
                        int modrm, int reg)
{
2284
    CCPrepare cc;
2285

2286
    gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
2287

2288 2289 2290 2291 2292 2293 2294 2295
    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);
2296 2297
    }

2298 2299 2300 2301 2302 2303 2304 2305 2306 2307
    tcg_gen_movcond_tl(cc.cond, cpu_T[0], cc.reg, cc.reg2,
                       cpu_T[0], cpu_regs[reg]);
    gen_op_mov_reg_T0(ot, reg);

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

2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325
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 已提交
2326 2327
/* move T0 to seg_reg and compute if the CPU state may change. Never
   call this function with seg_reg == R_CS */
B
bellard 已提交
2328
static void gen_movl_seg_T0(DisasContext *s, int seg_reg, target_ulong cur_eip)
B
bellard 已提交
2329
{
2330 2331
    if (s->pe && !s->vm86) {
        /* XXX: optimize by finding processor state dynamically */
2332
        gen_update_cc_op(s);
B
bellard 已提交
2333
        gen_jmp_im(cur_eip);
2334
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
2335
        gen_helper_load_seg(cpu_env, tcg_const_i32(seg_reg), cpu_tmp2_i32);
B
bellard 已提交
2336 2337 2338 2339 2340
        /* 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 已提交
2341
            s->is_jmp = DISAS_TB_JUMP;
2342
    } else {
2343
        gen_op_movl_seg_T0_vm(seg_reg);
B
bellard 已提交
2344
        if (seg_reg == R_SS)
J
Jun Koi 已提交
2345
            s->is_jmp = DISAS_TB_JUMP;
2346
    }
B
bellard 已提交
2347 2348
}

T
ths 已提交
2349 2350 2351 2352 2353
static inline int svm_is_rep(int prefixes)
{
    return ((prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) ? 8 : 0);
}

B
bellard 已提交
2354
static inline void
T
ths 已提交
2355
gen_svm_check_intercept_param(DisasContext *s, target_ulong pc_start,
2356
                              uint32_t type, uint64_t param)
T
ths 已提交
2357
{
B
bellard 已提交
2358 2359 2360
    /* no SVM activated; fast case */
    if (likely(!(s->flags & HF_SVMI_MASK)))
        return;
2361
    gen_update_cc_op(s);
B
bellard 已提交
2362
    gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
2363
    gen_helper_svm_check_intercept_param(cpu_env, tcg_const_i32(type),
P
pbrook 已提交
2364
                                         tcg_const_i64(param));
T
ths 已提交
2365 2366
}

B
bellard 已提交
2367
static inline void
T
ths 已提交
2368 2369
gen_svm_check_intercept(DisasContext *s, target_ulong pc_start, uint64_t type)
{
B
bellard 已提交
2370
    gen_svm_check_intercept_param(s, pc_start, type, 0);
T
ths 已提交
2371 2372
}

2373 2374
static inline void gen_stack_update(DisasContext *s, int addend)
{
B
bellard 已提交
2375 2376
#ifdef TARGET_X86_64
    if (CODE64(s)) {
2377
        gen_op_add_reg_im(MO_64, R_ESP, addend);
B
bellard 已提交
2378 2379
    } else
#endif
2380
    if (s->ss32) {
2381
        gen_op_add_reg_im(MO_32, R_ESP, addend);
2382
    } else {
2383
        gen_op_add_reg_im(MO_16, R_ESP, addend);
2384 2385 2386
    }
}

2387 2388
/* Generate a push. It depends on ss32, addseg and dflag.  */
static void gen_push_v(DisasContext *s, TCGv val)
B
bellard 已提交
2389
{
2390 2391 2392 2393 2394
    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 已提交
2395

B
bellard 已提交
2396
    if (CODE64(s)) {
2397 2398 2399 2400 2401 2402
        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);
2403
            gen_op_addl_A0_seg(s, R_SS);
2404 2405
        } else {
            tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
B
bellard 已提交
2406
        }
2407 2408 2409 2410 2411 2412
    } 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 已提交
2413
    }
2414 2415 2416

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

2419 2420
/* two step pop is necessary for precise exceptions */
static void gen_pop_T0(DisasContext *s)
B
bellard 已提交
2421
{
B
bellard 已提交
2422 2423
#ifdef TARGET_X86_64
    if (CODE64(s)) {
B
bellard 已提交
2424
        gen_op_movq_A0_reg(R_ESP);
2425
        gen_op_ld_v(s, mo_pushpop(s, s->dflag), cpu_T[0], cpu_A0);
2426
    } else
B
bellard 已提交
2427 2428
#endif
    {
B
bellard 已提交
2429
        gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2430 2431
        if (s->ss32) {
            if (s->addseg)
2432
                gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2433
        } else {
2434
            tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
2435
            gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2436
        }
2437
        gen_op_ld_v(s, s->dflag, cpu_T[0], cpu_A0);
B
bellard 已提交
2438 2439 2440 2441 2442
    }
}

static void gen_pop_update(DisasContext *s)
{
2443
    gen_stack_update(s, 1 << mo_pushpop(s, s->dflag));
B
bellard 已提交
2444 2445 2446 2447
}

static void gen_stack_A0(DisasContext *s)
{
B
bellard 已提交
2448
    gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2449
    if (!s->ss32)
2450
        tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
2451
    tcg_gen_mov_tl(cpu_T[1], cpu_A0);
B
bellard 已提交
2452
    if (s->addseg)
2453
        gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2454 2455 2456 2457 2458 2459
}

/* NOTE: wrap around in 16 bit not fully handled */
static void gen_pusha(DisasContext *s)
{
    int i;
B
bellard 已提交
2460
    gen_op_movl_A0_reg(R_ESP);
2461
    gen_op_addl_A0_im(-8 << s->dflag);
B
bellard 已提交
2462
    if (!s->ss32)
2463
        tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
2464
    tcg_gen_mov_tl(cpu_T[1], cpu_A0);
B
bellard 已提交
2465
    if (s->addseg)
2466
        gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2467
    for(i = 0;i < 8; i++) {
2468
        gen_op_mov_TN_reg(MO_32, 0, 7 - i);
2469 2470
        gen_op_st_v(s, s->dflag, cpu_T[0], cpu_A0);
        gen_op_addl_A0_im(1 << s->dflag);
B
bellard 已提交
2471
    }
2472
    gen_op_mov_reg_T1(MO_16 + s->ss32, R_ESP);
B
bellard 已提交
2473 2474 2475 2476 2477 2478
}

/* NOTE: wrap around in 16 bit not fully handled */
static void gen_popa(DisasContext *s)
{
    int i;
B
bellard 已提交
2479
    gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2480
    if (!s->ss32)
2481
        tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
2482
    tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2483
    tcg_gen_addi_tl(cpu_T[1], cpu_T[1], 8 << s->dflag);
B
bellard 已提交
2484
    if (s->addseg)
2485
        gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2486 2487 2488
    for(i = 0;i < 8; i++) {
        /* ESP is not reloaded */
        if (i != 3) {
2489 2490
            gen_op_ld_v(s, s->dflag, cpu_T[0], cpu_A0);
            gen_op_mov_reg_T0(s->dflag, 7 - i);
B
bellard 已提交
2491
        }
2492
        gen_op_addl_A0_im(1 << s->dflag);
B
bellard 已提交
2493
    }
2494
    gen_op_mov_reg_T1(MO_16 + s->ss32, R_ESP);
B
bellard 已提交
2495 2496 2497 2498
}

static void gen_enter(DisasContext *s, int esp_addend, int level)
{
2499 2500
    TCGMemOp ot = mo_pushpop(s, s->dflag);
    int opsize = 1 << ot;
B
bellard 已提交
2501 2502

    level &= 0x1f;
2503 2504
#ifdef TARGET_X86_64
    if (CODE64(s)) {
B
bellard 已提交
2505
        gen_op_movl_A0_reg(R_ESP);
2506
        gen_op_addq_A0_im(-opsize);
2507
        tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2508 2509

        /* push bp */
2510
        gen_op_mov_TN_reg(MO_32, 0, R_EBP);
2511
        gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
2512
        if (level) {
B
bellard 已提交
2513
            /* XXX: must save state */
2514
            gen_helper_enter64_level(cpu_env, tcg_const_i32(level),
2515
                                     tcg_const_i32((ot == MO_64)),
P
pbrook 已提交
2516
                                     cpu_T[1]);
2517
        }
B
bellard 已提交
2518
        gen_op_mov_reg_T1(ot, R_EBP);
2519
        tcg_gen_addi_tl(cpu_T[1], cpu_T[1], -esp_addend + (-opsize * level));
2520
        gen_op_mov_reg_T1(MO_64, R_ESP);
2521
    } else
2522 2523
#endif
    {
B
bellard 已提交
2524
        gen_op_movl_A0_reg(R_ESP);
2525 2526
        gen_op_addl_A0_im(-opsize);
        if (!s->ss32)
2527
            tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
2528
        tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2529
        if (s->addseg)
2530
            gen_op_addl_A0_seg(s, R_SS);
2531
        /* push bp */
2532
        gen_op_mov_TN_reg(MO_32, 0, R_EBP);
2533
        gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
2534
        if (level) {
B
bellard 已提交
2535
            /* XXX: must save state */
2536
            gen_helper_enter_level(cpu_env, tcg_const_i32(level),
2537
                                   tcg_const_i32(s->dflag - 1),
P
pbrook 已提交
2538
                                   cpu_T[1]);
2539
        }
B
bellard 已提交
2540
        gen_op_mov_reg_T1(ot, R_EBP);
2541
        tcg_gen_addi_tl(cpu_T[1], cpu_T[1], -esp_addend + (-opsize * level));
2542
        gen_op_mov_reg_T1(MO_16 + s->ss32, R_ESP);
B
bellard 已提交
2543 2544 2545
    }
}

B
bellard 已提交
2546
static void gen_exception(DisasContext *s, int trapno, target_ulong cur_eip)
B
bellard 已提交
2547
{
2548
    gen_update_cc_op(s);
B
bellard 已提交
2549
    gen_jmp_im(cur_eip);
B
Blue Swirl 已提交
2550
    gen_helper_raise_exception(cpu_env, tcg_const_i32(trapno));
J
Jun Koi 已提交
2551
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2552 2553 2554
}

/* an interrupt is different from an exception because of the
B
blueswir1 已提交
2555
   privilege checks */
2556
static void gen_interrupt(DisasContext *s, int intno,
B
bellard 已提交
2557
                          target_ulong cur_eip, target_ulong next_eip)
B
bellard 已提交
2558
{
2559
    gen_update_cc_op(s);
B
bellard 已提交
2560
    gen_jmp_im(cur_eip);
B
Blue Swirl 已提交
2561
    gen_helper_raise_interrupt(cpu_env, tcg_const_i32(intno),
P
pbrook 已提交
2562
                               tcg_const_i32(next_eip - cur_eip));
J
Jun Koi 已提交
2563
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2564 2565
}

B
bellard 已提交
2566
static void gen_debug(DisasContext *s, target_ulong cur_eip)
B
bellard 已提交
2567
{
2568
    gen_update_cc_op(s);
B
bellard 已提交
2569
    gen_jmp_im(cur_eip);
B
Blue Swirl 已提交
2570
    gen_helper_debug(cpu_env);
J
Jun Koi 已提交
2571
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2572 2573 2574 2575 2576 2577
}

/* generate a generic end of block. Trace exception is also generated
   if needed */
static void gen_eob(DisasContext *s)
{
2578
    gen_update_cc_op(s);
2579
    if (s->tb->flags & HF_INHIBIT_IRQ_MASK) {
2580
        gen_helper_reset_inhibit_irq(cpu_env);
2581
    }
J
Jan Kiszka 已提交
2582
    if (s->tb->flags & HF_RF_MASK) {
2583
        gen_helper_reset_rf(cpu_env);
J
Jan Kiszka 已提交
2584
    }
2585
    if (s->singlestep_enabled) {
B
Blue Swirl 已提交
2586
        gen_helper_debug(cpu_env);
2587
    } else if (s->tf) {
B
Blue Swirl 已提交
2588
        gen_helper_single_step(cpu_env);
B
bellard 已提交
2589
    } else {
B
bellard 已提交
2590
        tcg_gen_exit_tb(0);
B
bellard 已提交
2591
    }
J
Jun Koi 已提交
2592
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2593 2594 2595 2596
}

/* generate a jump to eip. No segment change must happen before as a
   direct call to the next block may occur */
B
bellard 已提交
2597
static void gen_jmp_tb(DisasContext *s, target_ulong eip, int tb_num)
B
bellard 已提交
2598
{
2599 2600
    gen_update_cc_op(s);
    set_cc_op(s, CC_OP_DYNAMIC);
B
bellard 已提交
2601
    if (s->jmp_opt) {
2602
        gen_goto_tb(s, tb_num, eip);
J
Jun Koi 已提交
2603
        s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2604
    } else {
B
bellard 已提交
2605
        gen_jmp_im(eip);
B
bellard 已提交
2606 2607 2608 2609
        gen_eob(s);
    }
}

B
bellard 已提交
2610 2611 2612 2613 2614
static void gen_jmp(DisasContext *s, target_ulong eip)
{
    gen_jmp_tb(s, eip, 0);
}

2615
static inline void gen_ldq_env_A0(DisasContext *s, int offset)
B
bellard 已提交
2616
{
2617
    tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
2618
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset);
B
bellard 已提交
2619
}
B
bellard 已提交
2620

2621
static inline void gen_stq_env_A0(DisasContext *s, int offset)
B
bellard 已提交
2622
{
2623
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset);
2624
    tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
B
bellard 已提交
2625
}
B
bellard 已提交
2626

2627
static inline void gen_ldo_env_A0(DisasContext *s, int offset)
B
bellard 已提交
2628
{
2629
    int mem_index = s->mem_index;
2630
    tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0, mem_index, MO_LEQ);
2631
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(0)));
B
bellard 已提交
2632
    tcg_gen_addi_tl(cpu_tmp0, cpu_A0, 8);
2633
    tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_tmp0, mem_index, MO_LEQ);
2634
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(1)));
B
bellard 已提交
2635
}
B
bellard 已提交
2636

2637
static inline void gen_sto_env_A0(DisasContext *s, int offset)
B
bellard 已提交
2638
{
2639
    int mem_index = s->mem_index;
2640
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(0)));
2641
    tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0, mem_index, MO_LEQ);
B
bellard 已提交
2642
    tcg_gen_addi_tl(cpu_tmp0, cpu_A0, 8);
2643
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(1)));
2644
    tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_tmp0, mem_index, MO_LEQ);
B
bellard 已提交
2645
}
B
bellard 已提交
2646

B
bellard 已提交
2647 2648
static inline void gen_op_movo(int d_offset, int s_offset)
{
2649 2650 2651 2652
    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 已提交
2653 2654 2655 2656
}

static inline void gen_op_movq(int d_offset, int s_offset)
{
2657 2658
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
B
bellard 已提交
2659 2660 2661 2662
}

static inline void gen_op_movl(int d_offset, int s_offset)
{
2663 2664
    tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env, s_offset);
    tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, d_offset);
B
bellard 已提交
2665 2666 2667 2668
}

static inline void gen_op_movq_env_0(int d_offset)
{
2669 2670
    tcg_gen_movi_i64(cpu_tmp1_i64, 0);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
B
bellard 已提交
2671
}
B
bellard 已提交
2672

B
Blue Swirl 已提交
2673 2674 2675 2676 2677 2678 2679
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 已提交
2680
typedef void (*SSEFunc_0_ppi)(TCGv_ptr reg_a, TCGv_ptr reg_b, TCGv_i32 val);
B
Blue Swirl 已提交
2681 2682
typedef void (*SSEFunc_0_eppt)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
                               TCGv val);
B
Blue Swirl 已提交
2683

B
bellard 已提交
2684 2685
#define SSE_SPECIAL ((void *)1)
#define SSE_DUMMY ((void *)2)
B
bellard 已提交
2686

P
pbrook 已提交
2687 2688 2689
#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 已提交
2690

B
Blue Swirl 已提交
2691
static const SSEFunc_0_epp sse_op_table1[256][4] = {
A
aurel32 已提交
2692 2693 2694
    /* 3DNow! extensions */
    [0x0e] = { SSE_DUMMY }, /* femms */
    [0x0f] = { SSE_DUMMY }, /* pf... */
B
bellard 已提交
2695 2696 2697
    /* 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 已提交
2698
    [0x12] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movlps, movlpd, movsldup, movddup */
B
bellard 已提交
2699
    [0x13] = { SSE_SPECIAL, SSE_SPECIAL },  /* movlps, movlpd */
P
pbrook 已提交
2700 2701
    [0x14] = { gen_helper_punpckldq_xmm, gen_helper_punpcklqdq_xmm },
    [0x15] = { gen_helper_punpckhdq_xmm, gen_helper_punpckhqdq_xmm },
B
bellard 已提交
2702 2703 2704 2705 2706 2707
    [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 */
2708
    [0x2b] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movntps, movntpd, movntss, movntsd */
B
bellard 已提交
2709 2710
    [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 已提交
2711 2712
    [0x2e] = { gen_helper_ucomiss, gen_helper_ucomisd },
    [0x2f] = { gen_helper_comiss, gen_helper_comisd },
B
bellard 已提交
2713 2714
    [0x50] = { SSE_SPECIAL, SSE_SPECIAL }, /* movmskps, movmskpd */
    [0x51] = SSE_FOP(sqrt),
P
pbrook 已提交
2715 2716 2717 2718 2719 2720
    [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 已提交
2721 2722
    [0x58] = SSE_FOP(add),
    [0x59] = SSE_FOP(mul),
P
pbrook 已提交
2723 2724 2725
    [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 已提交
2726 2727 2728 2729 2730 2731
    [0x5c] = SSE_FOP(sub),
    [0x5d] = SSE_FOP(min),
    [0x5e] = SSE_FOP(div),
    [0x5f] = SSE_FOP(max),

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

R
Richard Henderson 已提交
2735 2736 2737
    /* 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 已提交
2738

B
bellard 已提交
2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751
    /* 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 已提交
2752 2753
    [0x6c] = { NULL, gen_helper_punpcklqdq_xmm },
    [0x6d] = { NULL, gen_helper_punpckhqdq_xmm },
B
bellard 已提交
2754 2755
    [0x6e] = { SSE_SPECIAL, SSE_SPECIAL }, /* movd mm, ea */
    [0x6f] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movq, movdqa, , movqdu */
B
Blue Swirl 已提交
2756 2757 2758 2759
    [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 已提交
2760 2761 2762 2763 2764 2765
    [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 已提交
2766
    [0x77] = { SSE_DUMMY }, /* emms */
2767 2768
    [0x78] = { NULL, SSE_SPECIAL, NULL, SSE_SPECIAL }, /* extrq_i, insertq_i */
    [0x79] = { NULL, gen_helper_extrq_r, NULL, gen_helper_insertq_r },
P
pbrook 已提交
2769 2770
    [0x7c] = { NULL, gen_helper_haddpd, NULL, gen_helper_haddps },
    [0x7d] = { NULL, gen_helper_hsubpd, NULL, gen_helper_hsubps },
B
bellard 已提交
2771 2772 2773 2774
    [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 已提交
2775
    [0xd0] = { NULL, gen_helper_addsubpd, NULL, gen_helper_addsubps },
B
bellard 已提交
2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796
    [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 已提交
2797
    [0xe6] = { NULL, gen_helper_cvttpd2dq, gen_helper_cvtdq2pd, gen_helper_cvtpd2dq },
B
bellard 已提交
2798 2799 2800 2801 2802 2803 2804 2805 2806
    [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 已提交
2807
    [0xf0] = { NULL, NULL, NULL, SSE_SPECIAL }, /* lddqu */
B
bellard 已提交
2808 2809 2810 2811 2812 2813
    [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 已提交
2814 2815
    [0xf7] = { (SSEFunc_0_epp)gen_helper_maskmov_mmx,
               (SSEFunc_0_epp)gen_helper_maskmov_xmm }, /* XXX: casts */
B
bellard 已提交
2816 2817 2818 2819 2820 2821 2822 2823 2824
    [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 已提交
2825
static const SSEFunc_0_epp sse_op_table2[3 * 8][2] = {
B
bellard 已提交
2826 2827 2828 2829 2830 2831 2832
    [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 已提交
2833
    [16 + 3] = { NULL, gen_helper_psrldq_xmm },
B
bellard 已提交
2834
    [16 + 6] = MMX_OP2(psllq),
P
pbrook 已提交
2835
    [16 + 7] = { NULL, gen_helper_pslldq_xmm },
B
bellard 已提交
2836 2837
};

B
Blue Swirl 已提交
2838
static const SSEFunc_0_epi sse_op_table3ai[] = {
P
pbrook 已提交
2839
    gen_helper_cvtsi2ss,
2840
    gen_helper_cvtsi2sd
B
Blue Swirl 已提交
2841
};
P
pbrook 已提交
2842

2843
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
2844
static const SSEFunc_0_epl sse_op_table3aq[] = {
2845 2846 2847 2848 2849
    gen_helper_cvtsq2ss,
    gen_helper_cvtsq2sd
};
#endif

B
Blue Swirl 已提交
2850
static const SSEFunc_i_ep sse_op_table3bi[] = {
P
pbrook 已提交
2851 2852
    gen_helper_cvttss2si,
    gen_helper_cvtss2si,
2853
    gen_helper_cvttsd2si,
2854
    gen_helper_cvtsd2si
B
bellard 已提交
2855
};
2856

2857
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
2858
static const SSEFunc_l_ep sse_op_table3bq[] = {
2859 2860
    gen_helper_cvttss2sq,
    gen_helper_cvtss2sq,
2861
    gen_helper_cvttsd2sq,
2862 2863 2864 2865
    gen_helper_cvtsd2sq
};
#endif

B
Blue Swirl 已提交
2866
static const SSEFunc_0_epp sse_op_table4[8][4] = {
B
bellard 已提交
2867 2868 2869 2870 2871 2872 2873 2874 2875
    SSE_FOP(cmpeq),
    SSE_FOP(cmplt),
    SSE_FOP(cmple),
    SSE_FOP(cmpunord),
    SSE_FOP(cmpneq),
    SSE_FOP(cmpnlt),
    SSE_FOP(cmpnle),
    SSE_FOP(cmpord),
};
2876

B
Blue Swirl 已提交
2877
static const SSEFunc_0_epp sse_op_table5[256] = {
P
pbrook 已提交
2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901
    [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 已提交
2902 2903
};

B
Blue Swirl 已提交
2904 2905
struct SSEOpHelper_epp {
    SSEFunc_0_epp op[2];
B
Blue Swirl 已提交
2906 2907 2908
    uint32_t ext_mask;
};

B
Blue Swirl 已提交
2909 2910
struct SSEOpHelper_eppi {
    SSEFunc_0_eppi op[2];
B
Blue Swirl 已提交
2911
    uint32_t ext_mask;
B
balrog 已提交
2912
};
B
Blue Swirl 已提交
2913

B
balrog 已提交
2914
#define SSSE3_OP(x) { MMX_OP2(x), CPUID_EXT_SSSE3 }
P
pbrook 已提交
2915 2916
#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 已提交
2917
#define SSE41_SPECIAL { { NULL, SSE_SPECIAL }, CPUID_EXT_SSE41 }
2918 2919
#define PCLMULQDQ_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, \
        CPUID_EXT_PCLMULQDQ }
2920
#define AESNI_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_AES }
B
Blue Swirl 已提交
2921

B
Blue Swirl 已提交
2922
static const struct SSEOpHelper_epp sse_op_table6[256] = {
B
balrog 已提交
2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968
    [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),
2969 2970 2971 2972 2973
    [0xdb] = AESNI_OP(aesimc),
    [0xdc] = AESNI_OP(aesenc),
    [0xdd] = AESNI_OP(aesenclast),
    [0xde] = AESNI_OP(aesdec),
    [0xdf] = AESNI_OP(aesdeclast),
B
balrog 已提交
2974 2975
};

B
Blue Swirl 已提交
2976
static const struct SSEOpHelper_eppi sse_op_table7[256] = {
B
balrog 已提交
2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994
    [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),
2995
    [0x44] = PCLMULQDQ_OP(pclmulqdq),
B
balrog 已提交
2996 2997 2998 2999
    [0x60] = SSE42_OP(pcmpestrm),
    [0x61] = SSE42_OP(pcmpestri),
    [0x62] = SSE42_OP(pcmpistrm),
    [0x63] = SSE42_OP(pcmpistri),
3000
    [0xdf] = AESNI_OP(aeskeygenassist),
B
balrog 已提交
3001 3002
};

3003 3004
static void gen_sse(CPUX86State *env, DisasContext *s, int b,
                    target_ulong pc_start, int rex_r)
B
bellard 已提交
3005
{
3006
    int b1, op1_offset, op2_offset, is_xmm, val;
3007
    int modrm, mod, rm, reg;
B
Blue Swirl 已提交
3008 3009
    SSEFunc_0_epp sse_fn_epp;
    SSEFunc_0_eppi sse_fn_eppi;
B
Blue Swirl 已提交
3010
    SSEFunc_0_ppi sse_fn_ppi;
B
Blue Swirl 已提交
3011
    SSEFunc_0_eppt sse_fn_eppt;
3012
    TCGMemOp ot;
B
bellard 已提交
3013 3014

    b &= 0xff;
3015
    if (s->prefix & PREFIX_DATA)
B
bellard 已提交
3016
        b1 = 1;
3017
    else if (s->prefix & PREFIX_REPZ)
B
bellard 已提交
3018
        b1 = 2;
3019
    else if (s->prefix & PREFIX_REPNZ)
B
bellard 已提交
3020 3021 3022
        b1 = 3;
    else
        b1 = 0;
B
Blue Swirl 已提交
3023 3024
    sse_fn_epp = sse_op_table1[b][b1];
    if (!sse_fn_epp) {
B
bellard 已提交
3025
        goto illegal_op;
B
Blue Swirl 已提交
3026
    }
A
aurel32 已提交
3027
    if ((b <= 0x5f && b >= 0x10) || b == 0xc6 || b == 0xc2) {
B
bellard 已提交
3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047
        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 已提交
3048 3049
        if ((b != 0x38 && b != 0x3a) || (s->prefix & PREFIX_DATA))
            goto illegal_op;
3050 3051 3052 3053
    if (b == 0x0e) {
        if (!(s->cpuid_ext2_features & CPUID_EXT2_3DNOW))
            goto illegal_op;
        /* femms */
B
Blue Swirl 已提交
3054
        gen_helper_emms(cpu_env);
3055 3056 3057 3058
        return;
    }
    if (b == 0x77) {
        /* emms */
B
Blue Swirl 已提交
3059
        gen_helper_emms(cpu_env);
B
bellard 已提交
3060 3061 3062 3063 3064
        return;
    }
    /* prepare MMX state (XXX: optimize by storing fptt and fptags in
       the static cpu state) */
    if (!is_xmm) {
B
Blue Swirl 已提交
3065
        gen_helper_enter_mmx(cpu_env);
B
bellard 已提交
3066 3067
    }

3068
    modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
3069 3070 3071 3072
    reg = ((modrm >> 3) & 7);
    if (is_xmm)
        reg |= rex_r;
    mod = (modrm >> 6) & 3;
B
Blue Swirl 已提交
3073
    if (sse_fn_epp == SSE_SPECIAL) {
B
bellard 已提交
3074 3075 3076
        b |= (b1 << 8);
        switch(b) {
        case 0x0e7: /* movntq */
3077
            if (mod == 3)
B
bellard 已提交
3078
                goto illegal_op;
3079
            gen_lea_modrm(env, s, modrm);
3080
            gen_stq_env_A0(s, offsetof(CPUX86State, fpregs[reg].mmx));
B
bellard 已提交
3081 3082 3083 3084
            break;
        case 0x1e7: /* movntdq */
        case 0x02b: /* movntps */
        case 0x12b: /* movntps */
3085 3086
            if (mod == 3)
                goto illegal_op;
3087
            gen_lea_modrm(env, s, modrm);
3088
            gen_sto_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
3089
            break;
B
bellard 已提交
3090 3091
        case 0x3f0: /* lddqu */
            if (mod == 3)
B
bellard 已提交
3092
                goto illegal_op;
3093
            gen_lea_modrm(env, s, modrm);
3094
            gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
B
bellard 已提交
3095
            break;
3096 3097 3098 3099
        case 0x22b: /* movntss */
        case 0x32b: /* movntsd */
            if (mod == 3)
                goto illegal_op;
3100
            gen_lea_modrm(env, s, modrm);
3101
            if (b1 & 1) {
3102
                gen_stq_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
3103 3104 3105
            } else {
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
                    xmm_regs[reg].XMM_L(0)));
3106
                gen_op_st_v(s, MO_32, cpu_T[0], cpu_A0);
3107 3108
            }
            break;
B
bellard 已提交
3109
        case 0x6e: /* movd mm, ea */
B
bellard 已提交
3110
#ifdef TARGET_X86_64
3111
            if (s->dflag == MO_64) {
3112
                gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 0);
B
bellard 已提交
3113
                tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,fpregs[reg].mmx));
3114
            } else
B
bellard 已提交
3115 3116
#endif
            {
3117
                gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 0);
B
bellard 已提交
3118 3119
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                                 offsetof(CPUX86State,fpregs[reg].mmx));
P
pbrook 已提交
3120 3121
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
                gen_helper_movl_mm_T0_mmx(cpu_ptr0, cpu_tmp2_i32);
B
bellard 已提交
3122
            }
B
bellard 已提交
3123 3124
            break;
        case 0x16e: /* movd xmm, ea */
B
bellard 已提交
3125
#ifdef TARGET_X86_64
3126
            if (s->dflag == MO_64) {
3127
                gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 0);
B
bellard 已提交
3128 3129
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                                 offsetof(CPUX86State,xmm_regs[reg]));
P
pbrook 已提交
3130
                gen_helper_movq_mm_T0_xmm(cpu_ptr0, cpu_T[0]);
3131
            } else
B
bellard 已提交
3132 3133
#endif
            {
3134
                gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 0);
B
bellard 已提交
3135 3136
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                                 offsetof(CPUX86State,xmm_regs[reg]));
3137
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
3138
                gen_helper_movl_mm_T0_xmm(cpu_ptr0, cpu_tmp2_i32);
B
bellard 已提交
3139
            }
B
bellard 已提交
3140 3141 3142
            break;
        case 0x6f: /* movq mm, ea */
            if (mod != 3) {
3143
                gen_lea_modrm(env, s, modrm);
3144
                gen_ldq_env_A0(s, offsetof(CPUX86State, fpregs[reg].mmx));
B
bellard 已提交
3145 3146
            } else {
                rm = (modrm & 7);
3147
                tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env,
B
bellard 已提交
3148
                               offsetof(CPUX86State,fpregs[rm].mmx));
3149
                tcg_gen_st_i64(cpu_tmp1_i64, cpu_env,
B
bellard 已提交
3150
                               offsetof(CPUX86State,fpregs[reg].mmx));
B
bellard 已提交
3151 3152 3153 3154 3155 3156 3157 3158 3159
            }
            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) {
3160
                gen_lea_modrm(env, s, modrm);
3161
                gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
B
bellard 已提交
3162 3163 3164 3165 3166 3167 3168 3169
            } 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) {
3170
                gen_lea_modrm(env, s, modrm);
3171
                gen_op_ld_v(s, MO_32, cpu_T[0], cpu_A0);
B
bellard 已提交
3172
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
3173
                tcg_gen_movi_tl(cpu_T[0], 0);
B
bellard 已提交
3174 3175 3176
                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 已提交
3177 3178 3179 3180 3181 3182 3183 3184
            } 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) {
3185
                gen_lea_modrm(env, s, modrm);
3186 3187
                gen_ldq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(0)));
3188
                tcg_gen_movi_tl(cpu_T[0], 0);
B
bellard 已提交
3189 3190
                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 已提交
3191 3192 3193 3194 3195 3196 3197 3198 3199
            } 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) {
3200
                gen_lea_modrm(env, s, modrm);
3201 3202
                gen_ldq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3203 3204 3205 3206 3207 3208 3209
            } 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 已提交
3210 3211
        case 0x212: /* movsldup */
            if (mod != 3) {
3212
                gen_lea_modrm(env, s, modrm);
3213
                gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
B
bellard 已提交
3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227
            } 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) {
3228
                gen_lea_modrm(env, s, modrm);
3229 3230
                gen_ldq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3231 3232 3233 3234 3235 3236
            } 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 已提交
3237
                        offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3238
            break;
B
bellard 已提交
3239 3240 3241
        case 0x016: /* movhps */
        case 0x116: /* movhpd */
            if (mod != 3) {
3242
                gen_lea_modrm(env, s, modrm);
3243 3244
                gen_ldq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(1)));
B
bellard 已提交
3245 3246 3247 3248 3249 3250 3251 3252 3253
            } 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) {
3254
                gen_lea_modrm(env, s, modrm);
3255
                gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
B
bellard 已提交
3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267
            } 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;
3268 3269 3270 3271 3272 3273 3274
        case 0x178:
        case 0x378:
            {
                int bit_index, field_length;

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

B
balrog 已提交
3635
        case 0x138:
3636
        case 0x038:
B
balrog 已提交
3637
            b = modrm;
R
Richard Henderson 已提交
3638 3639 3640
            if ((b & 0xf0) == 0xf0) {
                goto do_0f_38_fx;
            }
3641
            modrm = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
3642 3643 3644
            rm = modrm & 7;
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
3645 3646 3647
            if (b1 >= 2) {
                goto illegal_op;
            }
B
balrog 已提交
3648

B
Blue Swirl 已提交
3649 3650
            sse_fn_epp = sse_op_table6[b].op[b1];
            if (!sse_fn_epp) {
B
balrog 已提交
3651
                goto illegal_op;
B
Blue Swirl 已提交
3652
            }
B
balrog 已提交
3653 3654
            if (!(s->cpuid_ext_features & sse_op_table6[b].ext_mask))
                goto illegal_op;
B
balrog 已提交
3655 3656 3657 3658 3659 3660 3661

            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);
3662
                    gen_lea_modrm(env, s, modrm);
B
balrog 已提交
3663 3664 3665 3666
                    switch (b) {
                    case 0x20: case 0x30: /* pmovsxbw, pmovzxbw */
                    case 0x23: case 0x33: /* pmovsxwd, pmovzxwd */
                    case 0x25: case 0x35: /* pmovsxdq, pmovzxdq */
3667
                        gen_ldq_env_A0(s, op2_offset +
B
balrog 已提交
3668 3669 3670 3671
                                        offsetof(XMMReg, XMM_Q(0)));
                        break;
                    case 0x21: case 0x31: /* pmovsxbd, pmovzxbd */
                    case 0x24: case 0x34: /* pmovsxwq, pmovzxwq */
3672 3673
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
balrog 已提交
3674 3675 3676 3677
                        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, op2_offset +
                                        offsetof(XMMReg, XMM_L(0)));
                        break;
                    case 0x22: case 0x32: /* pmovsxbq, pmovzxbq */
3678 3679
                        tcg_gen_qemu_ld_tl(cpu_tmp0, cpu_A0,
                                           s->mem_index, MO_LEUW);
B
balrog 已提交
3680 3681 3682 3683
                        tcg_gen_st16_tl(cpu_tmp0, cpu_env, op2_offset +
                                        offsetof(XMMReg, XMM_W(0)));
                        break;
                    case 0x2a:            /* movntqda */
3684
                        gen_ldo_env_A0(s, op1_offset);
B
balrog 已提交
3685 3686
                        return;
                    default:
3687
                        gen_ldo_env_A0(s, op2_offset);
B
balrog 已提交
3688
                    }
B
balrog 已提交
3689 3690 3691 3692 3693 3694 3695
                }
            } 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);
3696
                    gen_lea_modrm(env, s, modrm);
3697
                    gen_ldq_env_A0(s, op2_offset);
B
balrog 已提交
3698 3699
                }
            }
B
Blue Swirl 已提交
3700
            if (sse_fn_epp == SSE_SPECIAL) {
B
balrog 已提交
3701
                goto illegal_op;
B
Blue Swirl 已提交
3702
            }
B
balrog 已提交
3703

B
balrog 已提交
3704 3705
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
3706
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
balrog 已提交
3707

3708 3709 3710
            if (b == 0x17) {
                set_cc_op(s, CC_OP_EFLAGS);
            }
B
balrog 已提交
3711
            break;
R
Richard Henderson 已提交
3712 3713 3714 3715 3716 3717

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

R
Richard Henderson 已提交
3721 3722 3723 3724 3725 3726 3727 3728
            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) {
3729
                    ot = MO_8;
3730
                } else if (s->dflag != MO_64) {
3731
                    ot = (s->prefix & PREFIX_DATA ? MO_16 : MO_32);
R
Richard Henderson 已提交
3732
                } else {
3733
                    ot = MO_64;
R
Richard Henderson 已提交
3734
                }
B
balrog 已提交
3735

3736
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[reg]);
R
Richard Henderson 已提交
3737 3738 3739
                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 已提交
3740

3741
                ot = mo_64_32(s->dflag);
R
Richard Henderson 已提交
3742 3743
                gen_op_mov_reg_T0(ot, reg);
                break;
B
balrog 已提交
3744

R
Richard Henderson 已提交
3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758
            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;
                }
3759
                if (s->dflag != MO_64) {
3760
                    ot = (s->prefix & PREFIX_DATA ? MO_16 : MO_32);
R
Richard Henderson 已提交
3761
                } else {
3762
                    ot = MO_64;
R
Richard Henderson 已提交
3763 3764
                }

3765
                gen_lea_modrm(env, s, modrm);
R
Richard Henderson 已提交
3766
                if ((b & 1) == 0) {
3767 3768
                    tcg_gen_qemu_ld_tl(cpu_T[0], cpu_A0,
                                       s->mem_index, ot | MO_BE);
R
Richard Henderson 已提交
3769 3770
                    gen_op_mov_reg_T0(ot, reg);
                } else {
3771 3772
                    tcg_gen_qemu_st_tl(cpu_regs[reg], cpu_A0,
                                       s->mem_index, ot | MO_BE);
R
Richard Henderson 已提交
3773 3774 3775
                }
                break;

R
Richard Henderson 已提交
3776 3777 3778 3779 3780 3781
            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;
                }
3782
                ot = mo_64_32(s->dflag);
R
Richard Henderson 已提交
3783 3784 3785 3786 3787 3788 3789
                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]);
                gen_op_mov_reg_T0(ot, reg);
                gen_op_update1_cc();
                set_cc_op(s, CC_OP_LOGICB + ot);
                break;

R
Richard Henderson 已提交
3790 3791 3792 3793 3794 3795
            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;
                }
3796
                ot = mo_64_32(s->dflag);
R
Richard Henderson 已提交
3797 3798 3799 3800 3801 3802 3803 3804 3805
                {
                    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);

3806
                    bound = tcg_const_tl(ot == MO_64 ? 63 : 31);
R
Richard Henderson 已提交
3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829
                    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]);

                    gen_op_mov_reg_T0(ot, reg);
                    gen_op_update1_cc();
                    set_cc_op(s, CC_OP_LOGICB + ot);
                }
                break;

R
Richard Henderson 已提交
3830 3831 3832 3833 3834 3835
            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;
                }
3836
                ot = mo_64_32(s->dflag);
R
Richard Henderson 已提交
3837 3838 3839
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                tcg_gen_ext8u_tl(cpu_T[1], cpu_regs[s->vex_v]);
                {
3840
                    TCGv bound = tcg_const_tl(ot == MO_64 ? 63 : 31);
R
Richard Henderson 已提交
3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856
                    /* 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);
                gen_op_mov_reg_T0(ot, reg);
                gen_op_update1_cc();
                set_cc_op(s, CC_OP_BMILGB + ot);
                break;

R
Richard Henderson 已提交
3857 3858 3859 3860 3861 3862
            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;
                }
3863
                ot = mo_64_32(s->dflag);
R
Richard Henderson 已提交
3864 3865 3866
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                switch (ot) {
                default:
3867 3868 3869 3870 3871 3872
                    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 已提交
3873 3874
                    break;
#ifdef TARGET_X86_64
3875
                case MO_64:
3876 3877
                    tcg_gen_mulu2_i64(cpu_regs[s->vex_v], cpu_regs[reg],
                                      cpu_T[0], cpu_regs[R_EDX]);
R
Richard Henderson 已提交
3878 3879 3880 3881 3882
                    break;
#endif
                }
                break;

3883 3884 3885 3886 3887 3888
            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;
                }
3889
                ot = mo_64_32(s->dflag);
3890 3891 3892
                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.  */
3893
                if (ot == MO_64) {
3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906
                    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;
                }
3907
                ot = mo_64_32(s->dflag);
3908 3909 3910
                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.  */
3911
                if (ot == MO_64) {
3912 3913 3914 3915 3916 3917 3918
                    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;

3919 3920 3921 3922 3923
            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 {
3924
                    TCGv carry_in, carry_out, zero;
3925 3926
                    int end_op;

3927
                    ot = mo_64_32(s->dflag);
3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954
                    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:
3955
                        end_op = (b == 0x1f6 ? CC_OP_ADCX : CC_OP_ADOX);
3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970
                        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
3971
                    case MO_32:
3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983
                        /* 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.  */
3984 3985 3986 3987 3988 3989 3990 3991
                        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);
3992 3993 3994 3995 3996 3997
                        break;
                    }
                    set_cc_op(s, end_op);
                }
                break;

3998 3999 4000 4001 4002 4003 4004 4005
            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;
                }
4006
                ot = mo_64_32(s->dflag);
4007
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
4008
                if (ot == MO_64) {
4009 4010 4011 4012 4013 4014 4015
                    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) {
4016
                    if (ot != MO_64) {
4017 4018 4019 4020
                        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
                    }
                    tcg_gen_sar_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
                } else {
4021
                    if (ot != MO_64) {
4022 4023 4024 4025 4026 4027 4028
                        tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
                    }
                    tcg_gen_shr_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
                }
                gen_op_mov_reg_T0(ot, reg);
                break;

4029 4030 4031 4032 4033 4034 4035 4036 4037
            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;
                }
4038
                ot = mo_64_32(s->dflag);
4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070
                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]);
                    gen_op_mov_reg_T0(ot, s->vex_v);
                    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 已提交
4071 4072 4073
            default:
                goto illegal_op;
            }
B
balrog 已提交
4074
            break;
R
Richard Henderson 已提交
4075

B
balrog 已提交
4076 4077
        case 0x03a:
        case 0x13a:
B
balrog 已提交
4078
            b = modrm;
4079
            modrm = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
4080 4081 4082
            rm = modrm & 7;
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
4083 4084 4085
            if (b1 >= 2) {
                goto illegal_op;
            }
B
balrog 已提交
4086

B
Blue Swirl 已提交
4087 4088
            sse_fn_eppi = sse_op_table7[b].op[b1];
            if (!sse_fn_eppi) {
B
balrog 已提交
4089
                goto illegal_op;
B
Blue Swirl 已提交
4090
            }
B
balrog 已提交
4091 4092 4093
            if (!(s->cpuid_ext_features & sse_op_table7[b].ext_mask))
                goto illegal_op;

B
Blue Swirl 已提交
4094
            if (sse_fn_eppi == SSE_SPECIAL) {
4095
                ot = mo_64_32(s->dflag);
B
balrog 已提交
4096 4097
                rm = (modrm & 7) | REX_B(s);
                if (mod != 3)
4098
                    gen_lea_modrm(env, s, modrm);
B
balrog 已提交
4099
                reg = ((modrm >> 3) & 7) | rex_r;
4100
                val = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
4101 4102 4103 4104
                switch (b) {
                case 0x14: /* pextrb */
                    tcg_gen_ld8u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
                                            xmm_regs[reg].XMM_B(val & 15)));
4105
                    if (mod == 3) {
B
balrog 已提交
4106
                        gen_op_mov_reg_T0(ot, rm);
4107 4108 4109 4110
                    } else {
                        tcg_gen_qemu_st_tl(cpu_T[0], cpu_A0,
                                           s->mem_index, MO_UB);
                    }
B
balrog 已提交
4111 4112 4113 4114
                    break;
                case 0x15: /* pextrw */
                    tcg_gen_ld16u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
                                            xmm_regs[reg].XMM_W(val & 7)));
4115
                    if (mod == 3) {
B
balrog 已提交
4116
                        gen_op_mov_reg_T0(ot, rm);
4117 4118 4119 4120
                    } else {
                        tcg_gen_qemu_st_tl(cpu_T[0], cpu_A0,
                                           s->mem_index, MO_LEUW);
                    }
B
balrog 已提交
4121 4122
                    break;
                case 0x16:
4123
                    if (ot == MO_32) { /* pextrd */
B
balrog 已提交
4124 4125 4126
                        tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env,
                                        offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_L(val & 3)));
4127
                        if (mod == 3) {
4128
                            tcg_gen_extu_i32_tl(cpu_regs[rm], cpu_tmp2_i32);
4129
                        } else {
4130 4131
                            tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                                s->mem_index, MO_LEUL);
4132
                        }
B
balrog 已提交
4133
                    } else { /* pextrq */
P
pbrook 已提交
4134
#ifdef TARGET_X86_64
B
balrog 已提交
4135 4136 4137
                        tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env,
                                        offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_Q(val & 1)));
4138
                        if (mod == 3) {
4139
                            tcg_gen_mov_i64(cpu_regs[rm], cpu_tmp1_i64);
4140 4141 4142 4143
                        } else {
                            tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0,
                                                s->mem_index, MO_LEQ);
                        }
P
pbrook 已提交
4144 4145 4146
#else
                        goto illegal_op;
#endif
B
balrog 已提交
4147 4148 4149 4150 4151
                    }
                    break;
                case 0x17: /* extractps */
                    tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
                                            xmm_regs[reg].XMM_L(val & 3)));
4152
                    if (mod == 3) {
B
balrog 已提交
4153
                        gen_op_mov_reg_T0(ot, rm);
4154 4155 4156 4157
                    } else {
                        tcg_gen_qemu_st_tl(cpu_T[0], cpu_A0,
                                           s->mem_index, MO_LEUL);
                    }
B
balrog 已提交
4158 4159
                    break;
                case 0x20: /* pinsrb */
4160
                    if (mod == 3) {
4161
                        gen_op_mov_TN_reg(MO_32, 0, rm);
4162 4163 4164 4165
                    } else {
                        tcg_gen_qemu_ld_tl(cpu_T[0], cpu_A0,
                                           s->mem_index, MO_UB);
                    }
4166
                    tcg_gen_st8_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
B
balrog 已提交
4167 4168 4169
                                            xmm_regs[reg].XMM_B(val & 15)));
                    break;
                case 0x21: /* insertps */
P
pbrook 已提交
4170
                    if (mod == 3) {
B
balrog 已提交
4171 4172 4173
                        tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env,
                                        offsetof(CPUX86State,xmm_regs[rm]
                                                .XMM_L((val >> 6) & 3)));
P
pbrook 已提交
4174
                    } else {
4175 4176
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
P
pbrook 已提交
4177
                    }
B
balrog 已提交
4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198
                    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:
4199
                    if (ot == MO_32) { /* pinsrd */
4200
                        if (mod == 3) {
4201
                            tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[rm]);
4202
                        } else {
4203 4204
                            tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                                s->mem_index, MO_LEUL);
4205
                        }
B
balrog 已提交
4206 4207 4208 4209
                        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env,
                                        offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_L(val & 3)));
                    } else { /* pinsrq */
P
pbrook 已提交
4210
#ifdef TARGET_X86_64
4211
                        if (mod == 3) {
B
balrog 已提交
4212
                            gen_op_mov_v_reg(ot, cpu_tmp1_i64, rm);
4213 4214 4215 4216
                        } else {
                            tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0,
                                                s->mem_index, MO_LEQ);
                        }
B
balrog 已提交
4217 4218 4219
                        tcg_gen_st_i64(cpu_tmp1_i64, cpu_env,
                                        offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_Q(val & 1)));
P
pbrook 已提交
4220 4221 4222
#else
                        goto illegal_op;
#endif
B
balrog 已提交
4223 4224 4225 4226 4227
                    }
                    break;
                }
                return;
            }
B
balrog 已提交
4228 4229 4230 4231 4232 4233 4234

            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);
4235
                    gen_lea_modrm(env, s, modrm);
4236
                    gen_ldo_env_A0(s, op2_offset);
B
balrog 已提交
4237 4238 4239 4240 4241 4242 4243
                }
            } 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);
4244
                    gen_lea_modrm(env, s, modrm);
4245
                    gen_ldq_env_A0(s, op2_offset);
B
balrog 已提交
4246 4247
                }
            }
4248
            val = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
4249

B
balrog 已提交
4250
            if ((b & 0xfc) == 0x60) { /* pcmpXstrX */
4251
                set_cc_op(s, CC_OP_EFLAGS);
B
balrog 已提交
4252

4253
                if (s->dflag == MO_64) {
B
balrog 已提交
4254 4255
                    /* The helper must use entire 64-bit gp registers */
                    val |= 1 << 8;
4256
                }
B
balrog 已提交
4257 4258
            }

B
balrog 已提交
4259 4260
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4261
            sse_fn_eppi(cpu_env, cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
B
balrog 已提交
4262
            break;
R
Richard Henderson 已提交
4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276

        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;
                }
4277
                ot = mo_64_32(s->dflag);
R
Richard Henderson 已提交
4278 4279
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                b = cpu_ldub_code(env, s->pc++);
4280
                if (ot == MO_64) {
R
Richard Henderson 已提交
4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294
                    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);
                }
                gen_op_mov_reg_T0(ot, reg);
                break;

            default:
                goto illegal_op;
            }
            break;

B
bellard 已提交
4295 4296 4297 4298 4299
        default:
            goto illegal_op;
        }
    } else {
        /* generic MMX or SSE operation */
B
bellard 已提交
4300 4301 4302 4303 4304 4305 4306 4307
        switch(b) {
        case 0x70: /* pshufx insn */
        case 0xc6: /* pshufx insn */
        case 0xc2: /* compare insns */
            s->rip_offset = 1;
            break;
        default:
            break;
B
bellard 已提交
4308 4309 4310 4311
        }
        if (is_xmm) {
            op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
            if (mod != 3) {
4312
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
4313
                op2_offset = offsetof(CPUX86State,xmm_t0);
4314
                if (b1 >= 2 && ((b >= 0x50 && b <= 0x5f && b != 0x5b) ||
B
bellard 已提交
4315 4316 4317 4318
                                b == 0xc2)) {
                    /* specific case for SSE single instructions */
                    if (b1 == 2) {
                        /* 32 bit access */
4319
                        gen_op_ld_v(s, MO_32, cpu_T[0], cpu_A0);
B
bellard 已提交
4320
                        tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(0)));
B
bellard 已提交
4321 4322
                    } else {
                        /* 64 bit access */
4323 4324
                        gen_ldq_env_A0(s, offsetof(CPUX86State,
                                                   xmm_t0.XMM_D(0)));
B
bellard 已提交
4325 4326
                    }
                } else {
4327
                    gen_ldo_env_A0(s, op2_offset);
B
bellard 已提交
4328 4329 4330 4331 4332 4333 4334 4335
                }
            } 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) {
4336
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
4337
                op2_offset = offsetof(CPUX86State,mmx_t0);
4338
                gen_ldq_env_A0(s, op2_offset);
B
bellard 已提交
4339 4340 4341 4342 4343 4344
            } else {
                rm = (modrm & 7);
                op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
            }
        }
        switch(b) {
A
aurel32 已提交
4345
        case 0x0f: /* 3DNow! data insns */
4346 4347
            if (!(s->cpuid_ext2_features & CPUID_EXT2_3DNOW))
                goto illegal_op;
4348
            val = cpu_ldub_code(env, s->pc++);
B
Blue Swirl 已提交
4349 4350
            sse_fn_epp = sse_op_table5[val];
            if (!sse_fn_epp) {
A
aurel32 已提交
4351
                goto illegal_op;
B
Blue Swirl 已提交
4352
            }
B
bellard 已提交
4353 4354
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4355
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
A
aurel32 已提交
4356
            break;
B
bellard 已提交
4357 4358
        case 0x70: /* pshufx insn */
        case 0xc6: /* pshufx insn */
4359
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4360 4361
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4362
            /* XXX: introduce a new table? */
B
Blue Swirl 已提交
4363
            sse_fn_ppi = (SSEFunc_0_ppi)sse_fn_epp;
B
Blue Swirl 已提交
4364
            sse_fn_ppi(cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
B
bellard 已提交
4365 4366 4367
            break;
        case 0xc2:
            /* compare insns */
4368
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4369 4370
            if (val >= 8)
                goto illegal_op;
B
Blue Swirl 已提交
4371
            sse_fn_epp = sse_op_table4[val][b1];
B
Blue Swirl 已提交
4372

B
bellard 已提交
4373 4374
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4375
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
4376
            break;
4377 4378 4379 4380
        case 0xf7:
            /* maskmov : we must prepare A0 */
            if (mod != 3)
                goto illegal_op;
4381 4382
            tcg_gen_mov_tl(cpu_A0, cpu_regs[R_EDI]);
            gen_extu(s->aflag, cpu_A0);
4383 4384 4385 4386
            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 已提交
4387
            /* XXX: introduce a new table? */
B
Blue Swirl 已提交
4388 4389
            sse_fn_eppt = (SSEFunc_0_eppt)sse_fn_epp;
            sse_fn_eppt(cpu_env, cpu_ptr0, cpu_ptr1, cpu_A0);
4390
            break;
B
bellard 已提交
4391
        default:
B
bellard 已提交
4392 4393
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4394
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
4395 4396 4397
            break;
        }
        if (b == 0x2e || b == 0x2f) {
4398
            set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
4399 4400 4401 4402
        }
    }
}

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

4415
    if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
4416
        tcg_gen_debug_insn_start(pc_start);
4417
    }
B
bellard 已提交
4418 4419 4420
    s->pc = pc_start;
    prefixes = 0;
    s->override = -1;
B
bellard 已提交
4421 4422 4423 4424 4425
    rex_w = -1;
    rex_r = 0;
#ifdef TARGET_X86_64
    s->rex_x = 0;
    s->rex_b = 0;
4426
    x86_64_hregs = 0;
B
bellard 已提交
4427 4428
#endif
    s->rip_offset = 0; /* for relative ip address */
4429 4430
    s->vex_l = 0;
    s->vex_v = 0;
B
bellard 已提交
4431
 next_byte:
4432
    b = cpu_ldub_code(env, s->pc);
B
bellard 已提交
4433
    s->pc++;
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 4465 4466 4467 4468
    /* 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 已提交
4469
#ifdef TARGET_X86_64
4470 4471
    case 0x40 ... 0x4f:
        if (CODE64(s)) {
B
bellard 已提交
4472 4473 4474 4475 4476 4477 4478 4479
            /* 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;
        }
4480 4481
        break;
#endif
4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498
    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 已提交
4499
            /* 4.1.1-4.1.3: No preceding lock, 66, f2, f3, or rex prefixes. */
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 4535 4536 4537 4538
            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;
4539 4540 4541 4542
    }

    /* Post-process prefixes.  */
    if (CODE64(s)) {
4543 4544 4545
        /* 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.  */
4546
        dflag = (rex_w > 0 ? MO_64 : prefixes & PREFIX_DATA ? MO_16 : MO_32);
4547
        /* In 64-bit mode, 0x67 selects 32-bit addressing.  */
4548
        aflag = (prefixes & PREFIX_ADR ? MO_32 : MO_64);
4549 4550
    } else {
        /* In 16/32-bit mode, 0x66 selects the opposite data size.  */
4551 4552 4553 4554
        if (s->code32 ^ ((prefixes & PREFIX_DATA) != 0)) {
            dflag = MO_32;
        } else {
            dflag = MO_16;
B
bellard 已提交
4555
        }
4556
        /* In 16/32-bit mode, 0x67 selects the opposite addressing.  */
4557 4558 4559 4560
        if (s->code32 ^ ((prefixes & PREFIX_ADR) != 0)) {
            aflag = MO_32;
        }  else {
            aflag = MO_16;
B
bellard 已提交
4561
        }
B
bellard 已提交
4562 4563 4564 4565 4566 4567 4568 4569
    }

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

    /* lock generation */
    if (prefixes & PREFIX_LOCK)
P
pbrook 已提交
4570
        gen_helper_lock();
B
bellard 已提交
4571 4572 4573 4574 4575 4576 4577

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

B
bellard 已提交
4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595
        /**************************/
        /* 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;

4596
            ot = mo_b_d(b, dflag);
4597

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

4644 4645 4646
    case 0x82:
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
4647 4648 4649 4650 4651 4652
    case 0x80: /* GRP1 */
    case 0x81:
    case 0x83:
        {
            int val;

4653
            ot = mo_b_d(b, dflag);
4654

4655
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4656
            mod = (modrm >> 6) & 3;
B
bellard 已提交
4657
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4658
            op = (modrm >> 3) & 7;
4659

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

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

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

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

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

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

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

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

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

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

5009
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
bellard 已提交
5010
        gen_op_mov_TN_reg(ot, 1, reg);
B
bellard 已提交
5011
        gen_op_testl_T0_T1_cc();
5012
        set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
5013
        break;
5014

B
bellard 已提交
5015 5016
    case 0xa8: /* test eAX, Iv */
    case 0xa9:
5017
        ot = mo_b_d(b, dflag);
5018
        val = insn_get(env, s, ot);
B
bellard 已提交
5019

B
bellard 已提交
5020
        gen_op_mov_TN_reg(ot, 0, OR_EAX);
5021
        tcg_gen_movi_tl(cpu_T[1], val);
B
bellard 已提交
5022
        gen_op_testl_T0_T1_cc();
5023
        set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
5024
        break;
5025

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

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

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

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

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

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

    case 0x1b6: /* movzbS Gv, Eb */
    case 0x1b7: /* movzwS Gv, Eb */
    case 0x1be: /* movsbS Gv, Eb */
    case 0x1bf: /* movswS Gv, Eb */
        {
5430 5431 5432
            TCGMemOp d_ot;
            TCGMemOp s_ot;

B
bellard 已提交
5433
            /* d_ot is the size of destination */
5434
            d_ot = dflag;
B
bellard 已提交
5435
            /* ot is the size of source */
5436
            ot = (b & 1) + MO_8;
5437 5438 5439
            /* s_ot is the sign+size of source */
            s_ot = b & 8 ? MO_SIGN | ot : ot;

5440
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5441
            reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5442
            mod = (modrm >> 6) & 3;
B
bellard 已提交
5443
            rm = (modrm & 7) | REX_B(s);
5444

B
bellard 已提交
5445
            if (mod == 3) {
B
bellard 已提交
5446
                gen_op_mov_TN_reg(ot, 0, rm);
5447 5448
                switch (s_ot) {
                case MO_UB:
B
bellard 已提交
5449
                    tcg_gen_ext8u_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5450
                    break;
5451
                case MO_SB:
B
bellard 已提交
5452
                    tcg_gen_ext8s_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5453
                    break;
5454
                case MO_UW:
B
bellard 已提交
5455
                    tcg_gen_ext16u_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5456 5457
                    break;
                default:
5458
                case MO_SW:
B
bellard 已提交
5459
                    tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5460 5461
                    break;
                }
B
bellard 已提交
5462
                gen_op_mov_reg_T0(d_ot, reg);
B
bellard 已提交
5463
            } else {
5464
                gen_lea_modrm(env, s, modrm);
5465
                gen_op_ld_v(s, s_ot, cpu_T[0], cpu_A0);
B
bellard 已提交
5466
                gen_op_mov_reg_T0(d_ot, reg);
B
bellard 已提交
5467 5468 5469 5470 5471
            }
        }
        break;

    case 0x8d: /* lea */
5472
        ot = dflag;
5473
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5474 5475 5476
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
B
bellard 已提交
5477
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5478 5479 5480 5481
        /* we must ensure that no segment is added */
        s->override = -1;
        val = s->addseg;
        s->addseg = 0;
5482
        gen_lea_modrm(env, s, modrm);
B
bellard 已提交
5483
        s->addseg = val;
5484
        gen_op_mov_reg_A0(ot, reg);
B
bellard 已提交
5485
        break;
5486

B
bellard 已提交
5487 5488 5489 5490 5491
    case 0xa0: /* mov EAX, Ov */
    case 0xa1:
    case 0xa2: /* mov Ov, EAX */
    case 0xa3:
        {
B
bellard 已提交
5492 5493
            target_ulong offset_addr;

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

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

B
bellard 已提交
5621 5622 5623 5624 5625 5626 5627 5628
        /************************/
        /* shifts */
    case 0xc0:
    case 0xc1:
        /* shift Ev,Ib */
        shift = 2;
    grp2:
        {
5629
            ot = mo_b_d(b, dflag);
5630
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5631 5632
            mod = (modrm >> 6) & 3;
            op = (modrm >> 3) & 7;
5633

B
bellard 已提交
5634
            if (mod != 3) {
B
bellard 已提交
5635 5636 5637
                if (shift == 2) {
                    s->rip_offset = 1;
                }
5638
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
5639 5640
                opreg = OR_TMP0;
            } else {
B
bellard 已提交
5641
                opreg = (modrm & 7) | REX_B(s);
B
bellard 已提交
5642 5643 5644 5645 5646 5647 5648
            }

            /* simpler op */
            if (shift == 0) {
                gen_shift(s, op, ot, opreg, OR_ECX);
            } else {
                if (shift == 2) {
5649
                    shift = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681
                }
                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:
5682
        ot = dflag;
5683
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5684
        mod = (modrm >> 6) & 3;
B
bellard 已提交
5685 5686
        rm = (modrm & 7) | REX_B(s);
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5687
        if (mod != 3) {
5688
            gen_lea_modrm(env, s, modrm);
5689
            opreg = OR_TMP0;
B
bellard 已提交
5690
        } else {
5691
            opreg = rm;
B
bellard 已提交
5692
        }
B
bellard 已提交
5693
        gen_op_mov_TN_reg(ot, 1, reg);
5694

B
bellard 已提交
5695
        if (shift) {
P
Paolo Bonzini 已提交
5696 5697 5698
            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 已提交
5699
        } else {
P
Paolo Bonzini 已提交
5700
            gen_shiftd_rm_T1(s, ot, opreg, op, cpu_regs[R_ECX]);
B
bellard 已提交
5701 5702 5703 5704 5705
        }
        break;

        /************************/
        /* floats */
5706
    case 0xd8 ... 0xdf:
B
bellard 已提交
5707 5708 5709 5710 5711 5712
        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;
        }
5713
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5714 5715 5716 5717 5718
        mod = (modrm >> 6) & 3;
        rm = modrm & 7;
        op = ((b & 7) << 3) | ((modrm >> 3) & 7);
        if (mod != 3) {
            /* memory op */
5719
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730
            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:
5731 5732
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
Blue Swirl 已提交
5733
                        gen_helper_flds_FT0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5734 5735
                        break;
                    case 1:
5736 5737
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
Blue Swirl 已提交
5738
                        gen_helper_fildl_FT0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5739 5740
                        break;
                    case 2:
5741 5742
                        tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0,
                                            s->mem_index, MO_LEQ);
B
Blue Swirl 已提交
5743
                        gen_helper_fldl_FT0(cpu_env, cpu_tmp1_i64);
B
bellard 已提交
5744 5745 5746
                        break;
                    case 3:
                    default:
5747 5748
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LESW);
B
Blue Swirl 已提交
5749
                        gen_helper_fildl_FT0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5750 5751
                        break;
                    }
5752

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

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

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

                    if (!(s->cpuid_features & CPUID_CMOV)) {
                        goto illegal_op;
                    }
6212
                    op1 = fcmov_cc[op & 3] | (((op >> 3) & 1) ^ 1);
B
bellard 已提交
6213
                    l1 = gen_new_label();
6214
                    gen_jcc1_noeob(s, op1, l1);
B
Blue Swirl 已提交
6215
                    gen_helper_fmov_ST0_STN(cpu_env, tcg_const_i32(opreg));
B
bellard 已提交
6216
                    gen_set_label(l1);
6217 6218
                }
                break;
B
bellard 已提交
6219 6220 6221 6222 6223 6224 6225 6226 6227 6228
            default:
                goto illegal_op;
            }
        }
        break;
        /************************/
        /* string ops */

    case 0xa4: /* movsS */
    case 0xa5:
6229
        ot = mo_b_d(b, dflag);
B
bellard 已提交
6230 6231 6232 6233 6234 6235
        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;
6236

B
bellard 已提交
6237 6238
    case 0xaa: /* stosS */
    case 0xab:
6239
        ot = mo_b_d(b, dflag);
B
bellard 已提交
6240 6241 6242 6243 6244 6245 6246 6247
        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:
6248
        ot = mo_b_d(b, dflag);
B
bellard 已提交
6249 6250 6251 6252 6253 6254 6255 6256
        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:
6257
        ot = mo_b_d(b, dflag);
B
bellard 已提交
6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268
        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:
6269
        ot = mo_b_d(b, dflag);
B
bellard 已提交
6270 6271 6272 6273 6274 6275 6276 6277 6278 6279
        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:
6280
        ot = mo_b_d32(b, dflag);
6281
        tcg_gen_ext16u_tl(cpu_T[0], cpu_regs[R_EDX]);
6282 6283
        gen_check_io(s, ot, pc_start - s->cs_base, 
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes) | 4);
6284 6285
        if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
            gen_repz_ins(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
B
bellard 已提交
6286
        } else {
6287
            gen_ins(s, ot);
P
pbrook 已提交
6288 6289 6290
            if (use_icount) {
                gen_jmp(s, s->pc - s->cs_base);
            }
B
bellard 已提交
6291 6292 6293 6294
        }
        break;
    case 0x6e: /* outsS */
    case 0x6f:
6295
        ot = mo_b_d32(b, dflag);
6296
        tcg_gen_ext16u_tl(cpu_T[0], cpu_regs[R_EDX]);
6297 6298
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes) | 4);
6299 6300
        if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
            gen_repz_outs(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
B
bellard 已提交
6301
        } else {
6302
            gen_outs(s, ot);
P
pbrook 已提交
6303 6304 6305
            if (use_icount) {
                gen_jmp(s, s->pc - s->cs_base);
            }
B
bellard 已提交
6306 6307 6308 6309 6310
        }
        break;

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

B
bellard 已提交
6312 6313
    case 0xe4:
    case 0xe5:
6314
        ot = mo_b_d32(b, dflag);
6315
        val = cpu_ldub_code(env, s->pc++);
6316 6317
        gen_check_io(s, ot, pc_start - s->cs_base,
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
P
pbrook 已提交
6318 6319
        if (use_icount)
            gen_io_start();
6320
        tcg_gen_movi_i32(cpu_tmp2_i32, val);
P
pbrook 已提交
6321
        gen_helper_in_func(ot, cpu_T[1], cpu_tmp2_i32);
B
bellard 已提交
6322
        gen_op_mov_reg_T1(ot, R_EAX);
P
pbrook 已提交
6323 6324 6325 6326
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6327 6328 6329
        break;
    case 0xe6:
    case 0xe7:
6330
        ot = mo_b_d32(b, dflag);
6331
        val = cpu_ldub_code(env, s->pc++);
6332 6333
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes));
B
bellard 已提交
6334
        gen_op_mov_TN_reg(ot, 1, R_EAX);
6335

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

P
pbrook 已提交
6370 6371
        if (use_icount)
            gen_io_start();
6372 6373
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
        tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
P
pbrook 已提交
6374
        gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
P
pbrook 已提交
6375 6376 6377 6378
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6379 6380 6381 6382 6383
        break;

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

B
bellard 已提交
6479 6480
            if (CODE64(s))
                goto illegal_op;
6481
            ot = dflag;
6482
            offset = insn_get(env, s, ot);
6483
            selector = insn_get(env, s, MO_16);
6484

6485
            tcg_gen_movi_tl(cpu_T[0], selector);
6486
            tcg_gen_movi_tl(cpu_T[1], offset);
B
bellard 已提交
6487 6488
        }
        goto do_lcall;
B
bellard 已提交
6489
    case 0xe9: /* jmp im */
6490
        if (dflag != MO_16) {
6491
            tval = (int32_t)insn_get(env, s, MO_32);
6492
        } else {
6493
            tval = (int16_t)insn_get(env, s, MO_16);
6494
        }
B
bellard 已提交
6495
        tval += s->pc - s->cs_base;
6496
        if (dflag == MO_16) {
B
bellard 已提交
6497
            tval &= 0xffff;
6498
        } else if (!CODE64(s)) {
6499
            tval &= 0xffffffff;
6500
        }
B
bellard 已提交
6501
        gen_jmp(s, tval);
B
bellard 已提交
6502 6503 6504 6505 6506
        break;
    case 0xea: /* ljmp im */
        {
            unsigned int selector, offset;

B
bellard 已提交
6507 6508
            if (CODE64(s))
                goto illegal_op;
6509
            ot = dflag;
6510
            offset = insn_get(env, s, ot);
6511
            selector = insn_get(env, s, MO_16);
6512

6513
            tcg_gen_movi_tl(cpu_T[0], selector);
6514
            tcg_gen_movi_tl(cpu_T[1], offset);
B
bellard 已提交
6515 6516 6517
        }
        goto do_ljmp;
    case 0xeb: /* jmp Jb */
6518
        tval = (int8_t)insn_get(env, s, MO_8);
B
bellard 已提交
6519
        tval += s->pc - s->cs_base;
6520
        if (dflag == MO_16) {
B
bellard 已提交
6521
            tval &= 0xffff;
6522
        }
B
bellard 已提交
6523
        gen_jmp(s, tval);
B
bellard 已提交
6524 6525
        break;
    case 0x70 ... 0x7f: /* jcc Jb */
6526
        tval = (int8_t)insn_get(env, s, MO_8);
B
bellard 已提交
6527 6528
        goto do_jcc;
    case 0x180 ... 0x18f: /* jcc Jv */
6529
        if (dflag != MO_16) {
6530
            tval = (int32_t)insn_get(env, s, MO_32);
B
bellard 已提交
6531
        } else {
6532
            tval = (int16_t)insn_get(env, s, MO_16);
B
bellard 已提交
6533 6534 6535
        }
    do_jcc:
        next_eip = s->pc - s->cs_base;
B
bellard 已提交
6536
        tval += next_eip;
6537
        if (dflag == MO_16) {
B
bellard 已提交
6538
            tval &= 0xffff;
6539
        }
B
bellard 已提交
6540
        gen_jcc(s, b, tval, next_eip);
B
bellard 已提交
6541 6542 6543
        break;

    case 0x190 ... 0x19f: /* setcc Gv */
6544
        modrm = cpu_ldub_code(env, s->pc++);
6545
        gen_setcc1(s, b, cpu_T[0]);
6546
        gen_ldst_modrm(env, s, modrm, MO_8, OR_TMP0, 1);
B
bellard 已提交
6547 6548
        break;
    case 0x140 ... 0x14f: /* cmov Gv, Ev */
6549 6550 6551
        if (!(s->cpuid_features & CPUID_CMOV)) {
            goto illegal_op;
        }
6552
        ot = dflag;
6553 6554 6555
        modrm = cpu_ldub_code(env, s->pc++);
        reg = ((modrm >> 3) & 7) | rex_r;
        gen_cmovcc1(env, s, ot, b, modrm, reg);
B
bellard 已提交
6556
        break;
6557

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

        /************************/
        /* bit operations */
    case 0x1ba: /* bt/bts/btr/btc Gv, im */
6669
        ot = dflag;
6670
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
6671
        op = (modrm >> 3) & 7;
B
bellard 已提交
6672
        mod = (modrm >> 6) & 3;
B
bellard 已提交
6673
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
6674
        if (mod != 3) {
B
bellard 已提交
6675
            s->rip_offset = 1;
6676
            gen_lea_modrm(env, s, modrm);
6677
            gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
6678
        } else {
B
bellard 已提交
6679
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
6680 6681
        }
        /* load shift */
6682
        val = cpu_ldub_code(env, s->pc++);
6683
        tcg_gen_movi_tl(cpu_T[1], val);
B
bellard 已提交
6684 6685 6686
        if (op < 4)
            goto illegal_op;
        op -= 4;
B
bellard 已提交
6687
        goto bt_op;
B
bellard 已提交
6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699
    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:
6700
        ot = dflag;
6701
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
6702
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
6703
        mod = (modrm >> 6) & 3;
B
bellard 已提交
6704
        rm = (modrm & 7) | REX_B(s);
6705
        gen_op_mov_TN_reg(MO_32, 1, reg);
B
bellard 已提交
6706
        if (mod != 3) {
6707
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
6708
            /* specific case: we need to add a displacement */
B
bellard 已提交
6709 6710 6711 6712
            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);
6713
            gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
6714
        } else {
B
bellard 已提交
6715
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
6716
        }
B
bellard 已提交
6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744
    bt_op:
        tcg_gen_andi_tl(cpu_T[1], cpu_T[1], (1 << (3 + ot)) - 1);
        switch(op) {
        case 0:
            tcg_gen_shr_tl(cpu_cc_src, cpu_T[0], cpu_T[1]);
            tcg_gen_movi_tl(cpu_cc_dst, 0);
            break;
        case 1:
            tcg_gen_shr_tl(cpu_tmp4, cpu_T[0], cpu_T[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_shr_tl(cpu_tmp4, cpu_T[0], cpu_T[1]);
            tcg_gen_movi_tl(cpu_tmp0, 1);
            tcg_gen_shl_tl(cpu_tmp0, cpu_tmp0, cpu_T[1]);
            tcg_gen_not_tl(cpu_tmp0, cpu_tmp0);
            tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
            break;
        default:
        case 3:
            tcg_gen_shr_tl(cpu_tmp4, cpu_T[0], cpu_T[1]);
            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;
        }
6745
        set_cc_op(s, CC_OP_SARB + ot);
B
bellard 已提交
6746
        if (op != 0) {
6747 6748 6749
            if (mod != 3) {
                gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
            } else {
B
bellard 已提交
6750
                gen_op_mov_reg_T0(ot, rm);
6751
            }
B
bellard 已提交
6752 6753
            tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
            tcg_gen_movi_tl(cpu_cc_dst, 0);
B
bellard 已提交
6754 6755
        }
        break;
6756 6757
    case 0x1bc: /* bsf / tzcnt */
    case 0x1bd: /* bsr / lzcnt */
6758
        ot = dflag;
6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775
        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 已提交
6776
            } else {
6777 6778 6779 6780 6781
                /* 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 已提交
6782
            }
6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805
            /* 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 已提交
6806
        }
6807
        gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
6808 6809 6810 6811
        break;
        /************************/
        /* bcd */
    case 0x27: /* daa */
B
bellard 已提交
6812 6813
        if (CODE64(s))
            goto illegal_op;
6814
        gen_update_cc_op(s);
6815
        gen_helper_daa(cpu_env);
6816
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6817 6818
        break;
    case 0x2f: /* das */
B
bellard 已提交
6819 6820
        if (CODE64(s))
            goto illegal_op;
6821
        gen_update_cc_op(s);
6822
        gen_helper_das(cpu_env);
6823
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6824 6825
        break;
    case 0x37: /* aaa */
B
bellard 已提交
6826 6827
        if (CODE64(s))
            goto illegal_op;
6828
        gen_update_cc_op(s);
6829
        gen_helper_aaa(cpu_env);
6830
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6831 6832
        break;
    case 0x3f: /* aas */
B
bellard 已提交
6833 6834
        if (CODE64(s))
            goto illegal_op;
6835
        gen_update_cc_op(s);
6836
        gen_helper_aas(cpu_env);
6837
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6838 6839
        break;
    case 0xd4: /* aam */
B
bellard 已提交
6840 6841
        if (CODE64(s))
            goto illegal_op;
6842
        val = cpu_ldub_code(env, s->pc++);
6843 6844 6845
        if (val == 0) {
            gen_exception(s, EXCP00_DIVZ, pc_start - s->cs_base);
        } else {
6846
            gen_helper_aam(cpu_env, tcg_const_i32(val));
6847
            set_cc_op(s, CC_OP_LOGICB);
6848
        }
B
bellard 已提交
6849 6850
        break;
    case 0xd5: /* aad */
B
bellard 已提交
6851 6852
        if (CODE64(s))
            goto illegal_op;
6853
        val = cpu_ldub_code(env, s->pc++);
6854
        gen_helper_aad(cpu_env, tcg_const_i32(val));
6855
        set_cc_op(s, CC_OP_LOGICB);
B
bellard 已提交
6856 6857 6858 6859
        break;
        /************************/
        /* misc */
    case 0x90: /* nop */
6860
        /* XXX: correct lock test for all insn */
R
Richard Henderson 已提交
6861
        if (prefixes & PREFIX_LOCK) {
6862
            goto illegal_op;
R
Richard Henderson 已提交
6863 6864 6865 6866 6867
        }
        /* 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 已提交
6868
        if (prefixes & PREFIX_REPZ) {
6869 6870 6871 6872
            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 已提交
6873
        }
B
bellard 已提交
6874 6875
        break;
    case 0x9b: /* fwait */
6876
        if ((s->flags & (HF_MP_MASK | HF_TS_MASK)) ==
B
bellard 已提交
6877 6878
            (HF_MP_MASK | HF_TS_MASK)) {
            gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
B
bellard 已提交
6879
        } else {
6880
            gen_update_cc_op(s);
B
bellard 已提交
6881
            gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6882
            gen_helper_fwait(cpu_env);
B
bellard 已提交
6883
        }
B
bellard 已提交
6884 6885 6886 6887 6888
        break;
    case 0xcc: /* int3 */
        gen_interrupt(s, EXCP03_INT3, pc_start - s->cs_base, s->pc - s->cs_base);
        break;
    case 0xcd: /* int N */
6889
        val = cpu_ldub_code(env, s->pc++);
6890
        if (s->vm86 && s->iopl != 3) {
6891
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6892 6893 6894
        } else {
            gen_interrupt(s, val, pc_start - s->cs_base, s->pc - s->cs_base);
        }
B
bellard 已提交
6895 6896
        break;
    case 0xce: /* into */
B
bellard 已提交
6897 6898
        if (CODE64(s))
            goto illegal_op;
6899
        gen_update_cc_op(s);
6900
        gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6901
        gen_helper_into(cpu_env, tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
6902
        break;
A
aurel32 已提交
6903
#ifdef WANT_ICEBP
B
bellard 已提交
6904
    case 0xf1: /* icebp (undocumented, exits to external debugger) */
B
bellard 已提交
6905
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_ICEBP);
6906
#if 1
B
bellard 已提交
6907
        gen_debug(s, pc_start - s->cs_base);
6908 6909
#else
        /* start debug */
6910
        tb_flush(env);
6911
        qemu_set_log(CPU_LOG_INT | CPU_LOG_TB_IN_ASM);
6912
#endif
B
bellard 已提交
6913
        break;
A
aurel32 已提交
6914
#endif
B
bellard 已提交
6915 6916 6917
    case 0xfa: /* cli */
        if (!s->vm86) {
            if (s->cpl <= s->iopl) {
6918
                gen_helper_cli(cpu_env);
B
bellard 已提交
6919 6920 6921 6922 6923
            } else {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            }
        } else {
            if (s->iopl == 3) {
6924
                gen_helper_cli(cpu_env);
B
bellard 已提交
6925 6926 6927 6928 6929 6930 6931 6932 6933
            } 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:
6934
                gen_helper_sti(cpu_env);
B
bellard 已提交
6935
                /* interruptions are enabled only the first insn after sti */
6936 6937 6938
                /* If several instructions disable interrupts, only the
                   _first_ does it */
                if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
6939
                    gen_helper_set_inhibit_irq(cpu_env);
B
bellard 已提交
6940
                /* give a chance to handle pending irqs */
B
bellard 已提交
6941
                gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954
                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 已提交
6955 6956
        if (CODE64(s))
            goto illegal_op;
6957
        ot = dflag;
6958
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
6959 6960 6961 6962
        reg = (modrm >> 3) & 7;
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
B
bellard 已提交
6963
        gen_op_mov_TN_reg(ot, 0, reg);
6964
        gen_lea_modrm(env, s, modrm);
B
bellard 已提交
6965
        gen_jmp_im(pc_start - s->cs_base);
6966
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
6967
        if (ot == MO_16) {
B
Blue Swirl 已提交
6968 6969 6970 6971
            gen_helper_boundw(cpu_env, cpu_A0, cpu_tmp2_i32);
        } else {
            gen_helper_boundl(cpu_env, cpu_A0, cpu_tmp2_i32);
        }
B
bellard 已提交
6972 6973
        break;
    case 0x1c8 ... 0x1cf: /* bswap reg */
B
bellard 已提交
6974 6975
        reg = (b & 7) | REX_B(s);
#ifdef TARGET_X86_64
6976
        if (dflag == MO_64) {
6977
            gen_op_mov_TN_reg(MO_64, 0, reg);
A
aurel32 已提交
6978
            tcg_gen_bswap64_i64(cpu_T[0], cpu_T[0]);
6979
            gen_op_mov_reg_T0(MO_64, reg);
6980
        } else
6981
#endif
B
bellard 已提交
6982
        {
6983
            gen_op_mov_TN_reg(MO_32, 0, reg);
6984 6985
            tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
            tcg_gen_bswap32_tl(cpu_T[0], cpu_T[0]);
6986
            gen_op_mov_reg_T0(MO_32, reg);
B
bellard 已提交
6987
        }
B
bellard 已提交
6988 6989
        break;
    case 0xd6: /* salc */
B
bellard 已提交
6990 6991
        if (CODE64(s))
            goto illegal_op;
6992
        gen_compute_eflags_c(s, cpu_T[0]);
6993
        tcg_gen_neg_tl(cpu_T[0], cpu_T[0]);
6994
        gen_op_mov_reg_T0(MO_8, R_EAX);
B
bellard 已提交
6995 6996 6997 6998 6999
        break;
    case 0xe0: /* loopnz */
    case 0xe1: /* loopz */
    case 0xe2: /* loop */
    case 0xe3: /* jecxz */
B
bellard 已提交
7000
        {
7001
            int l1, l2, l3;
B
bellard 已提交
7002

7003
            tval = (int8_t)insn_get(env, s, MO_8);
B
bellard 已提交
7004 7005
            next_eip = s->pc - s->cs_base;
            tval += next_eip;
7006
            if (dflag == MO_16) {
B
bellard 已提交
7007
                tval &= 0xffff;
7008
            }
7009

B
bellard 已提交
7010 7011
            l1 = gen_new_label();
            l2 = gen_new_label();
7012
            l3 = gen_new_label();
B
bellard 已提交
7013
            b &= 3;
7014 7015 7016
            switch(b) {
            case 0: /* loopnz */
            case 1: /* loopz */
7017 7018
                gen_op_add_reg_im(s->aflag, R_ECX, -1);
                gen_op_jz_ecx(s->aflag, l3);
7019
                gen_jcc1(s, (JCC_Z << 1) | (b ^ 1), l1);
7020 7021
                break;
            case 2: /* loop */
7022 7023
                gen_op_add_reg_im(s->aflag, R_ECX, -1);
                gen_op_jnz_ecx(s->aflag, l1);
7024 7025 7026
                break;
            default:
            case 3: /* jcxz */
7027
                gen_op_jz_ecx(s->aflag, l1);
7028
                break;
B
bellard 已提交
7029 7030
            }

7031
            gen_set_label(l3);
B
bellard 已提交
7032
            gen_jmp_im(next_eip);
7033
            tcg_gen_br(l2);
7034

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

7477
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7478 7479 7480
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
            rm = (modrm & 7) | REX_B(s);
7481

B
bellard 已提交
7482
            if (mod == 3) {
7483
                gen_op_mov_TN_reg(MO_32, 0, rm);
B
bellard 已提交
7484
                /* sign extend */
7485
                if (d_ot == MO_64) {
B
bellard 已提交
7486
                    tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
7487
                }
B
bellard 已提交
7488
                gen_op_mov_reg_T0(d_ot, reg);
B
bellard 已提交
7489
            } else {
7490
                gen_lea_modrm(env, s, modrm);
R
Richard Henderson 已提交
7491
                gen_op_ld_v(s, MO_32 | MO_SIGN, cpu_T[0], cpu_A0);
B
bellard 已提交
7492
                gen_op_mov_reg_T0(d_ot, reg);
B
bellard 已提交
7493
            }
7494
        } else
B
bellard 已提交
7495 7496
#endif
        {
7497
            int label1;
L
Laurent Desnogues 已提交
7498
            TCGv t0, t1, t2, a0;
7499

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

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

7794
        if (s->prefix & PREFIX_DATA) {
7795
            ot = MO_16;
7796 7797 7798
        } else {
            ot = mo_64_32(dflag);
        }
B
balrog 已提交
7799

7800
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
Blue Swirl 已提交
7801
        gen_helper_popcnt(cpu_T[0], cpu_env, cpu_T[0], tcg_const_i32(ot));
B
balrog 已提交
7802
        gen_op_mov_reg_T0(ot, reg);
B
balrog 已提交
7803

7804
        set_cc_op(s, CC_OP_EFLAGS);
B
balrog 已提交
7805
        break;
A
aurel32 已提交
7806 7807 7808
    case 0x10e ... 0x10f:
        /* 3DNow! instructions, ignore prefixes */
        s->prefix &= ~(PREFIX_REPZ | PREFIX_REPNZ | PREFIX_DATA);
B
bellard 已提交
7809 7810
    case 0x110 ... 0x117:
    case 0x128 ... 0x12f:
B
balrog 已提交
7811
    case 0x138 ... 0x13a:
7812
    case 0x150 ... 0x179:
B
bellard 已提交
7813 7814 7815 7816
    case 0x17c ... 0x17f:
    case 0x1c2:
    case 0x1c4 ... 0x1c6:
    case 0x1d0 ... 0x1fe:
7817
        gen_sse(env, s, b, pc_start, rex_r);
B
bellard 已提交
7818
        break;
B
bellard 已提交
7819 7820 7821 7822 7823
    default:
        goto illegal_op;
    }
    /* lock generation */
    if (s->prefix & PREFIX_LOCK)
P
pbrook 已提交
7824
        gen_helper_unlock();
B
bellard 已提交
7825 7826
    return s->pc;
 illegal_op:
7827
    if (s->prefix & PREFIX_LOCK)
P
pbrook 已提交
7828
        gen_helper_unlock();
B
bellard 已提交
7829 7830 7831 7832 7833 7834 7835
    /* 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)
{
P
pbrook 已提交
7836 7837
    cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
    cpu_cc_op = tcg_global_mem_new_i32(TCG_AREG0,
7838 7839
                                       offsetof(CPUX86State, cc_op), "cc_op");
    cpu_cc_dst = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_dst),
P
pbrook 已提交
7840
                                    "cc_dst");
7841 7842
    cpu_cc_src = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_src),
                                    "cc_src");
7843 7844
    cpu_cc_src2 = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_src2),
                                     "cc_src2");
7845

7846 7847
#ifdef TARGET_X86_64
    cpu_regs[R_EAX] = tcg_global_mem_new_i64(TCG_AREG0,
7848
                                             offsetof(CPUX86State, regs[R_EAX]), "rax");
7849
    cpu_regs[R_ECX] = tcg_global_mem_new_i64(TCG_AREG0,
7850
                                             offsetof(CPUX86State, regs[R_ECX]), "rcx");
7851
    cpu_regs[R_EDX] = tcg_global_mem_new_i64(TCG_AREG0,
7852
                                             offsetof(CPUX86State, regs[R_EDX]), "rdx");
7853
    cpu_regs[R_EBX] = tcg_global_mem_new_i64(TCG_AREG0,
7854
                                             offsetof(CPUX86State, regs[R_EBX]), "rbx");
7855
    cpu_regs[R_ESP] = tcg_global_mem_new_i64(TCG_AREG0,
7856
                                             offsetof(CPUX86State, regs[R_ESP]), "rsp");
7857
    cpu_regs[R_EBP] = tcg_global_mem_new_i64(TCG_AREG0,
7858
                                             offsetof(CPUX86State, regs[R_EBP]), "rbp");
7859
    cpu_regs[R_ESI] = tcg_global_mem_new_i64(TCG_AREG0,
7860
                                             offsetof(CPUX86State, regs[R_ESI]), "rsi");
7861
    cpu_regs[R_EDI] = tcg_global_mem_new_i64(TCG_AREG0,
7862
                                             offsetof(CPUX86State, regs[R_EDI]), "rdi");
7863
    cpu_regs[8] = tcg_global_mem_new_i64(TCG_AREG0,
7864
                                         offsetof(CPUX86State, regs[8]), "r8");
7865
    cpu_regs[9] = tcg_global_mem_new_i64(TCG_AREG0,
7866
                                          offsetof(CPUX86State, regs[9]), "r9");
7867
    cpu_regs[10] = tcg_global_mem_new_i64(TCG_AREG0,
7868
                                          offsetof(CPUX86State, regs[10]), "r10");
7869
    cpu_regs[11] = tcg_global_mem_new_i64(TCG_AREG0,
7870
                                          offsetof(CPUX86State, regs[11]), "r11");
7871
    cpu_regs[12] = tcg_global_mem_new_i64(TCG_AREG0,
7872
                                          offsetof(CPUX86State, regs[12]), "r12");
7873
    cpu_regs[13] = tcg_global_mem_new_i64(TCG_AREG0,
7874
                                          offsetof(CPUX86State, regs[13]), "r13");
7875
    cpu_regs[14] = tcg_global_mem_new_i64(TCG_AREG0,
7876
                                          offsetof(CPUX86State, regs[14]), "r14");
7877
    cpu_regs[15] = tcg_global_mem_new_i64(TCG_AREG0,
7878
                                          offsetof(CPUX86State, regs[15]), "r15");
7879 7880
#else
    cpu_regs[R_EAX] = tcg_global_mem_new_i32(TCG_AREG0,
7881
                                             offsetof(CPUX86State, regs[R_EAX]), "eax");
7882
    cpu_regs[R_ECX] = tcg_global_mem_new_i32(TCG_AREG0,
7883
                                             offsetof(CPUX86State, regs[R_ECX]), "ecx");
7884
    cpu_regs[R_EDX] = tcg_global_mem_new_i32(TCG_AREG0,
7885
                                             offsetof(CPUX86State, regs[R_EDX]), "edx");
7886
    cpu_regs[R_EBX] = tcg_global_mem_new_i32(TCG_AREG0,
7887
                                             offsetof(CPUX86State, regs[R_EBX]), "ebx");
7888
    cpu_regs[R_ESP] = tcg_global_mem_new_i32(TCG_AREG0,
7889
                                             offsetof(CPUX86State, regs[R_ESP]), "esp");
7890
    cpu_regs[R_EBP] = tcg_global_mem_new_i32(TCG_AREG0,
7891
                                             offsetof(CPUX86State, regs[R_EBP]), "ebp");
7892
    cpu_regs[R_ESI] = tcg_global_mem_new_i32(TCG_AREG0,
7893
                                             offsetof(CPUX86State, regs[R_ESI]), "esi");
7894
    cpu_regs[R_EDI] = tcg_global_mem_new_i32(TCG_AREG0,
7895
                                             offsetof(CPUX86State, regs[R_EDI]), "edi");
7896
#endif
B
bellard 已提交
7897 7898 7899 7900 7901
}

/* 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. */
7902
static inline void gen_intermediate_code_internal(X86CPU *cpu,
7903
                                                  TranslationBlock *tb,
7904
                                                  bool search_pc)
B
bellard 已提交
7905
{
7906
    CPUState *cs = CPU(cpu);
7907
    CPUX86State *env = &cpu->env;
B
bellard 已提交
7908
    DisasContext dc1, *dc = &dc1;
B
bellard 已提交
7909
    target_ulong pc_ptr;
B
bellard 已提交
7910
    uint16_t *gen_opc_end;
7911
    CPUBreakpoint *bp;
7912
    int j, lj;
7913
    uint64_t flags;
B
bellard 已提交
7914 7915
    target_ulong pc_start;
    target_ulong cs_base;
P
pbrook 已提交
7916 7917
    int num_insns;
    int max_insns;
7918

B
bellard 已提交
7919
    /* generate intermediate code */
B
bellard 已提交
7920 7921
    pc_start = tb->pc;
    cs_base = tb->cs_base;
B
bellard 已提交
7922
    flags = tb->flags;
B
bellard 已提交
7923

7924
    dc->pe = (flags >> HF_PE_SHIFT) & 1;
B
bellard 已提交
7925 7926 7927 7928 7929 7930 7931 7932
    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;
7933
    dc->singlestep_enabled = cs->singlestep_enabled;
B
bellard 已提交
7934
    dc->cc_op = CC_OP_DYNAMIC;
7935
    dc->cc_op_dirty = false;
B
bellard 已提交
7936 7937 7938 7939 7940 7941
    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) {
7942
        dc->mem_index = cpu_mmu_index(env);
B
bellard 已提交
7943
    }
7944 7945 7946 7947 7948
    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 已提交
7949 7950 7951 7952
#ifdef TARGET_X86_64
    dc->lma = (flags >> HF_LMA_SHIFT) & 1;
    dc->code64 = (flags >> HF_CS64_SHIFT) & 1;
#endif
B
bellard 已提交
7953
    dc->flags = flags;
7954
    dc->jmp_opt = !(dc->tf || cs->singlestep_enabled ||
7955
                    (flags & HF_INHIBIT_IRQ_MASK)
B
bellard 已提交
7956
#ifndef CONFIG_SOFTMMU
B
bellard 已提交
7957 7958 7959
                    || (flags & HF_SOFTMMU_MASK)
#endif
                    );
7960 7961
#if 0
    /* check addseg logic */
B
bellard 已提交
7962
    if (!dc->addseg && (dc->vm86 || !dc->pe || !dc->code32))
7963 7964 7965
        printf("ERROR addseg\n");
#endif

P
pbrook 已提交
7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976
    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();
7977
    cpu_cc_srcT = tcg_temp_local_new();
B
bellard 已提交
7978

7979
    gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
B
bellard 已提交
7980 7981 7982 7983

    dc->is_jmp = DISAS_NEXT;
    pc_ptr = pc_start;
    lj = -1;
P
pbrook 已提交
7984 7985 7986 7987
    num_insns = 0;
    max_insns = tb->cflags & CF_COUNT_MASK;
    if (max_insns == 0)
        max_insns = CF_COUNT_MASK;
B
bellard 已提交
7988

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

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

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

P
pbrook 已提交
8073
    if (!search_pc) {
B
bellard 已提交
8074
        tb->size = pc_ptr - pc_start;
P
pbrook 已提交
8075 8076
        tb->icount = num_insns;
    }
B
bellard 已提交
8077 8078
}

8079
void gen_intermediate_code(CPUX86State *env, TranslationBlock *tb)
B
bellard 已提交
8080
{
8081
    gen_intermediate_code_internal(x86_env_get_cpu(env), tb, false);
B
bellard 已提交
8082 8083
}

8084
void gen_intermediate_code_pc(CPUX86State *env, TranslationBlock *tb)
B
bellard 已提交
8085
{
8086
    gen_intermediate_code_internal(x86_env_get_cpu(env), tb, true);
B
bellard 已提交
8087 8088
}

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