translate.c 274.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"
P
Paolo Bonzini 已提交
30
#include "exec/cpu_ldst.h"
B
bellard 已提交
31

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

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


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

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

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

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

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

79 80
static uint8_t gen_opc_cc_op[OPC_BUF_SIZE];

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

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

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

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

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

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

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

B
bellard 已提交
171 172 173 174 175 176 177 178 179 180
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 已提交
181 182

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

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

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

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

238 239 240 241 242 243 244 245 246 247 248
    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;
    }
249
    s->cc_op = op;
250 251 252 253 254
}

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

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

#define NB_OP_SIZES 4

#else /* !TARGET_X86_64 */

#define NB_OP_SIZES 3

#endif /* !TARGET_X86_64 */

270
#if defined(HOST_WORDS_BIGENDIAN)
B
bellard 已提交
271 272 273 274 275
#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 已提交
276
#else
B
bellard 已提交
277 278 279 280 281
#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 已提交
282
#endif
B
bellard 已提交
283

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
/* 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;
}

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
/* 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;
}

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

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

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

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

B
bellard 已提交
388
#ifdef TARGET_X86_64
B
bellard 已提交
389 390 391 392
static inline void gen_op_addq_A0_im(int64_t val)
{
    tcg_gen_addi_tl(cpu_A0, cpu_A0, val);
}
B
bellard 已提交
393
#endif
B
bellard 已提交
394 395 396 397 398 399 400 401 402 403
    
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 已提交
404

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

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

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

static inline void gen_op_addl_A0_reg_sN(int shift, int reg)
{
424 425
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[reg]);
    if (shift != 0)
B
bellard 已提交
426 427
        tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, shift);
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
428 429 430
    /* 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 已提交
431
}
B
bellard 已提交
432

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

438
static inline void gen_op_addl_A0_seg(DisasContext *s, int reg)
B
bellard 已提交
439
{
440
    tcg_gen_ld_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, segs[reg].base));
B
bellard 已提交
441
#ifdef TARGET_X86_64
442 443 444 445 446 447 448 449 450
    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 已提交
451 452
#endif
}
B
bellard 已提交
453

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

715 716 717 718 719 720 721 722 723 724 725
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]);
}

726 727 728 729 730 731 732
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]);
}

733 734 735 736 737 738 739 740
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]);
741 742
    tcg_gen_neg_tl(cpu_cc_src, cpu_T[0]);
    tcg_gen_movi_tl(cpu_cc_srcT, 0);
743 744
}

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

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

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

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

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

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

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

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

    switch (s->cc_op) {
    case CC_OP_SUBB ... CC_OP_SUBQ:
808
        /* (DATA_TYPE)CC_SRCT < (DATA_TYPE)CC_SRC */
809 810 811 812
        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;
813
        tcg_gen_mov_tl(t0, cpu_cc_srcT);
814 815 816 817 818 819 820 821 822
        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:
823 824
        return (CCPrepare) { .cond = TCG_COND_LTU, .reg = t0,
                             .reg2 = t1, .mask = -1, .use_reg2 = true };
825 826

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

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

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

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

846 847 848 849 850
    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 };

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

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

    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);
866 867
       gen_helper_cc_compute_c(reg, cpu_cc_dst, cpu_cc_src,
                               cpu_cc_src2, cpu_cc_op);
868 869
       return (CCPrepare) { .cond = TCG_COND_NE, .reg = reg,
                            .mask = -1, .no_setcond = true };
870 871 872
    }
}

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

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

/* compute eflags.O to reg */
906
static CCPrepare gen_prepare_eflags_o(DisasContext *s, TCGv reg)
907
{
908 909 910 911 912
    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 已提交
913 914
    case CC_OP_CLR:
        return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
915 916 917 918 919
    default:
        gen_compute_eflags(s);
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = CC_O };
    }
920 921 922
}

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

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

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

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

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

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

989 990
    default:
    slow_jcc:
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
        /* 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;
        }
1035
        break;
1036
    }
1037 1038 1039 1040 1041

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

1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
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);
}
1078

1079 1080
/* generate a conditional jump to label 'l1' according to jump opcode
   value 'b'. In the fast case, T0 is guaranted not to be used. */
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
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.  */
1099
static inline void gen_jcc1(DisasContext *s, int b, int l1)
1100
{
1101
    CCPrepare cc = gen_prepare_cc(s, b, cpu_T[0]);
1102

1103
    gen_update_cc_op(s);
1104 1105 1106 1107
    if (cc.mask != -1) {
        tcg_gen_andi_tl(cpu_T[0], cc.reg, cc.mask);
        cc.reg = cpu_T[0];
    }
1108
    set_cc_op(s, CC_OP_DYNAMIC);
1109 1110 1111 1112
    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);
1113 1114 1115
    }
}

B
bellard 已提交
1116 1117 1118
/* 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 已提交
1119
{
B
bellard 已提交
1120 1121 1122 1123
    int l1, l2;

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

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

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

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

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

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

1190
static inline void gen_outs(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
1191
{
1192
    if (s->tb->cflags & CF_USE_ICOUNT) {
P
pbrook 已提交
1193
        gen_io_start();
1194
    }
B
bellard 已提交
1195
    gen_string_movl_A0_ESI(s);
1196
    gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
1197

1198
    tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_EDX]);
1199 1200
    tcg_gen_andi_i32(cpu_tmp2_i32, cpu_tmp2_i32, 0xffff);
    tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[0]);
P
pbrook 已提交
1201
    gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
1202

1203
    gen_op_movl_T0_Dshift(ot);
1204
    gen_op_add_reg_T0(s->aflag, R_ESI);
1205
    if (s->tb->cflags & CF_USE_ICOUNT) {
P
pbrook 已提交
1206
        gen_io_end();
1207
    }
B
bellard 已提交
1208 1209 1210 1211 1212
}

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

#define GEN_REPZ2(op)                                                         \
1229
static inline void gen_repz_ ## op(DisasContext *s, TCGMemOp ot,              \
B
bellard 已提交
1230 1231
                                   target_ulong cur_eip,                      \
                                   target_ulong next_eip,                     \
B
bellard 已提交
1232 1233
                                   int nz)                                    \
{                                                                             \
B
bellard 已提交
1234
    int l2;\
B
bellard 已提交
1235
    gen_update_cc_op(s);                                                      \
B
bellard 已提交
1236
    l2 = gen_jz_ecx_string(s, next_eip);                                      \
B
bellard 已提交
1237
    gen_ ## op(s, ot);                                                        \
1238
    gen_op_add_reg_im(s->aflag, R_ECX, -1);                                   \
1239
    gen_update_cc_op(s);                                                      \
1240
    gen_jcc1(s, (JCC_Z << 1) | (nz ^ 1), l2);                                 \
1241
    if (s->repz_opt)                                                          \
1242
        gen_op_jz_ecx(s->aflag, l2);                                          \
B
bellard 已提交
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
    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 已提交
1254 1255 1256
static void gen_helper_fp_arith_ST0_FT0(int op)
{
    switch (op) {
B
Blue Swirl 已提交
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
    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 已提交
1281 1282
    }
}
B
bellard 已提交
1283 1284

/* NOTE the exception in "r" op ordering */
P
pbrook 已提交
1285 1286 1287 1288
static void gen_helper_fp_arith_STN_ST0(int op, int opreg)
{
    TCGv_i32 tmp = tcg_const_i32(opreg);
    switch (op) {
B
Blue Swirl 已提交
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
    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 已提交
1307 1308
    }
}
B
bellard 已提交
1309 1310

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

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

1396 1397
static void gen_shift_flags(DisasContext *s, TCGMemOp ot, TCGv result,
                            TCGv shm1, TCGv count, bool is_right)
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
{
    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);
}

1441
static void gen_shift_rm_T1(DisasContext *s, TCGMemOp ot, int op1,
1442
                            int is_right, int is_arith)
B
bellard 已提交
1443
{
1444
    target_ulong mask = (ot == MO_64 ? 0x3f : 0x1f);
1445

1446
    /* load */
1447
    if (op1 == OR_TMP0) {
1448
        gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
1449
    } else {
1450
        gen_op_mov_v_reg(ot, cpu_T[0], op1);
1451
    }
1452

1453 1454
    tcg_gen_andi_tl(cpu_T[1], cpu_T[1], mask);
    tcg_gen_subi_tl(cpu_tmp0, cpu_T[1], 1);
1455 1456 1457

    if (is_right) {
        if (is_arith) {
B
bellard 已提交
1458
            gen_exts(ot, cpu_T[0]);
1459 1460
            tcg_gen_sar_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
            tcg_gen_sar_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1461
        } else {
B
bellard 已提交
1462
            gen_extu(ot, cpu_T[0]);
1463 1464
            tcg_gen_shr_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
            tcg_gen_shr_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1465 1466
        }
    } else {
1467 1468
        tcg_gen_shl_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
        tcg_gen_shl_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1469 1470 1471
    }

    /* store */
1472
    gen_op_st_rm_T0_A0(s, ot, op1);
1473

1474
    gen_shift_flags(s, ot, cpu_T[0], cpu_tmp0, cpu_T[1], is_right);
1475 1476
}

1477
static void gen_shift_rm_im(DisasContext *s, TCGMemOp ot, int op1, int op2,
B
bellard 已提交
1478 1479
                            int is_right, int is_arith)
{
1480
    int mask = (ot == MO_64 ? 0x3f : 0x1f);
B
bellard 已提交
1481 1482 1483

    /* load */
    if (op1 == OR_TMP0)
1484
        gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
1485
    else
1486
        gen_op_mov_v_reg(ot, cpu_T[0], op1);
B
bellard 已提交
1487 1488 1489 1490 1491 1492

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

    /* store */
1507 1508
    gen_op_st_rm_T0_A0(s, ot, op1);

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

1517
static void gen_rot_rm_T1(DisasContext *s, TCGMemOp ot, int op1, int is_right)
1518
{
1519
    target_ulong mask = (ot == MO_64 ? 0x3f : 0x1f);
1520
    TCGv_i32 t0, t1;
1521 1522

    /* load */
1523
    if (op1 == OR_TMP0) {
1524
        gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
1525
    } else {
1526
        gen_op_mov_v_reg(ot, cpu_T[0], op1);
1527
    }
1528

1529
    tcg_gen_andi_tl(cpu_T[1], cpu_T[1], mask);
1530

1531
    switch (ot) {
1532
    case MO_8:
1533 1534 1535 1536
        /* 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;
1537
    case MO_16:
1538 1539 1540 1541 1542
        /* 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
1543
    case MO_32:
1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
        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;
1561 1562 1563
    }

    /* store */
1564
    gen_op_st_rm_T0_A0(s, ot, op1);
1565

1566 1567
    /* We'll need the flags computed into CC_SRC.  */
    gen_compute_eflags(s);
1568

1569 1570 1571 1572
    /* 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.  */
1573
    if (is_right) {
1574 1575
        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 已提交
1576
        tcg_gen_andi_tl(cpu_cc_dst, cpu_cc_dst, 1);
1577 1578 1579
    } else {
        tcg_gen_shri_tl(cpu_cc_src2, cpu_T[0], mask);
        tcg_gen_andi_tl(cpu_cc_dst, cpu_T[0], 1);
1580
    }
1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
    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);
1600 1601
}

1602
static void gen_rot_rm_im(DisasContext *s, TCGMemOp ot, int op1, int op2,
M
malc 已提交
1603 1604
                          int is_right)
{
1605
    int mask = (ot == MO_64 ? 0x3f : 0x1f);
1606
    int shift;
M
malc 已提交
1607 1608 1609

    /* load */
    if (op1 == OR_TMP0) {
1610
        gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
M
malc 已提交
1611
    } else {
1612
        gen_op_mov_v_reg(ot, cpu_T[0], op1);
M
malc 已提交
1613 1614 1615 1616
    }

    op2 &= mask;
    if (op2 != 0) {
1617 1618
        switch (ot) {
#ifdef TARGET_X86_64
1619
        case MO_32:
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
            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;
1636
        case MO_8:
1637 1638
            mask = 7;
            goto do_shifts;
1639
        case MO_16:
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650
            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 已提交
1651 1652 1653 1654
        }
    }

    /* store */
1655
    gen_op_st_rm_T0_A0(s, ot, op1);
M
malc 已提交
1656 1657

    if (op2 != 0) {
1658
        /* Compute the flags into CC_SRC.  */
1659
        gen_compute_eflags(s);
1660

1661 1662 1663 1664
        /* 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 已提交
1665
        if (is_right) {
1666 1667
            tcg_gen_shri_tl(cpu_cc_src2, cpu_T[0], mask - 1);
            tcg_gen_shri_tl(cpu_cc_dst, cpu_T[0], mask);
1668
            tcg_gen_andi_tl(cpu_cc_dst, cpu_cc_dst, 1);
1669 1670 1671
        } 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 已提交
1672
        }
1673 1674 1675
        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 已提交
1676 1677 1678
    }
}

1679
/* XXX: add faster immediate = 1 case */
1680
static void gen_rotc_rm_T1(DisasContext *s, TCGMemOp ot, int op1,
1681 1682
                           int is_right)
{
1683
    gen_compute_eflags(s);
1684
    assert(s->cc_op == CC_OP_EFLAGS);
1685 1686 1687

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

/* XXX: add faster immediate case */
1736
static void gen_shiftd_rm_T1(DisasContext *s, TCGMemOp ot, int op1,
1737
                             bool is_right, TCGv count_in)
1738
{
1739
    target_ulong mask = (ot == MO_64 ? 63 : 31);
1740
    TCGv count;
1741 1742

    /* load */
1743
    if (op1 == OR_TMP0) {
1744
        gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
1745
    } else {
1746
        gen_op_mov_v_reg(ot, cpu_T[0], op1);
1747
    }
1748

1749 1750
    count = tcg_temp_new();
    tcg_gen_andi_tl(count, count_in, mask);
1751

1752
    switch (ot) {
1753
    case MO_16:
1754 1755 1756
        /* 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.  */
1757
        if (is_right) {
1758 1759 1760
            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);
1761
        } else {
1762
            tcg_gen_deposit_tl(cpu_T[1], cpu_T[0], cpu_T[1], 16, 16);
1763
        }
1764 1765
        /* FALLTHRU */
#ifdef TARGET_X86_64
1766
    case MO_32:
1767 1768
        /* Concatenate the two 32-bit values and use a 64-bit shift.  */
        tcg_gen_subi_tl(cpu_tmp0, count, 1);
1769
        if (is_right) {
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785
            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);
1786

1787 1788 1789
            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);
1790
        } else {
1791
            tcg_gen_shl_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
1792
            if (ot == MO_16) {
1793 1794 1795 1796 1797 1798 1799 1800 1801
                /* 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);
1802
        }
1803 1804 1805 1806 1807
        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;
1808 1809 1810
    }

    /* store */
1811
    gen_op_st_rm_T0_A0(s, ot, op1);
1812

1813 1814
    gen_shift_flags(s, ot, cpu_T[0], cpu_tmp0, count, is_right);
    tcg_temp_free(count);
1815 1816
}

1817
static void gen_shift(DisasContext *s1, int op, TCGMemOp ot, int d, int s)
1818 1819
{
    if (s != OR_TMP1)
1820
        gen_op_mov_v_reg(ot, cpu_T[1], s);
1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844
    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 已提交
1845 1846
}

1847
static void gen_shifti(DisasContext *s1, int op, TCGMemOp ot, int d, int c)
B
bellard 已提交
1848
{
B
bellard 已提交
1849
    switch(op) {
M
malc 已提交
1850 1851 1852 1853 1854 1855
    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 已提交
1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867
    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 */
1868
        tcg_gen_movi_tl(cpu_T[1], c);
B
bellard 已提交
1869 1870 1871
        gen_shift(s1, op, ot, d, OR_TMP1);
        break;
    }
B
bellard 已提交
1872 1873
}

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

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

1891 1892 1893
    switch (s->aflag) {
    case MO_64:
    case MO_32:
B
bellard 已提交
1894 1895
        havesib = 0;
        base = rm;
1896
        index = -1;
B
bellard 已提交
1897
        scale = 0;
1898

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

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

1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946
        /* 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 已提交
1947
            }
1948 1949 1950
            if (base >= 0) {
                tcg_gen_add_tl(cpu_A0, sum, cpu_regs[base]);
                sum = cpu_A0;
B
bellard 已提交
1951
            }
1952 1953
        } else if (base >= 0) {
            sum = cpu_regs[base];
B
bellard 已提交
1954
        }
1955 1956 1957 1958
        if (TCGV_IS_UNUSED(sum)) {
            tcg_gen_movi_tl(cpu_A0, disp);
        } else {
            tcg_gen_addi_tl(cpu_A0, sum, disp);
B
bellard 已提交
1959
        }
1960

B
bellard 已提交
1961 1962
        if (must_add_seg) {
            if (override < 0) {
1963
                if (base == R_EBP || base == R_ESP) {
B
bellard 已提交
1964
                    override = R_SS;
1965
                } else {
B
bellard 已提交
1966
                    override = R_DS;
1967
                }
B
bellard 已提交
1968
            }
1969 1970 1971 1972

            tcg_gen_ld_tl(cpu_tmp0, cpu_env,
                          offsetof(CPUX86State, segs[override].base));
            if (CODE64(s)) {
1973
                if (s->aflag == MO_32) {
1974 1975 1976
                    tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
                }
                tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
1977
                return;
B
bellard 已提交
1978
            }
1979 1980 1981 1982

            tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
        }

1983
        if (s->aflag == MO_32) {
1984
            tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
B
bellard 已提交
1985
        }
1986 1987 1988
        break;

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

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

    default:
        tcg_abort();
B
bellard 已提交
2056 2057 2058
    }
}

2059
static void gen_nop_modrm(CPUX86State *env, DisasContext *s, int modrm)
B
bellard 已提交
2060 2061 2062 2063 2064 2065 2066 2067
{
    int mod, rm, base, code;

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

2068 2069 2070
    switch (s->aflag) {
    case MO_64:
    case MO_32:
B
bellard 已提交
2071
        base = rm;
2072

B
bellard 已提交
2073
        if (base == 4) {
2074
            code = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
2075 2076
            base = (code & 7);
        }
2077

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

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

    default:
        tcg_abort();
B
bellard 已提交
2113 2114 2115
    }
}

B
bellard 已提交
2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126
/* 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) {
2127 2128
#ifdef TARGET_X86_64
        if (CODE64(s)) {
B
bellard 已提交
2129
            gen_op_addq_A0_seg(override);
2130
        } else
2131 2132
#endif
        {
2133
            gen_op_addl_A0_seg(s, override);
2134
        }
B
bellard 已提交
2135 2136 2137
    }
}

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

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

2171
static inline uint32_t insn_get(CPUX86State *env, DisasContext *s, TCGMemOp ot)
B
bellard 已提交
2172 2173 2174
{
    uint32_t ret;

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

2197
static inline int insn_const_size(TCGMemOp ot)
B
bellard 已提交
2198
{
2199
    if (ot <= MO_32) {
B
bellard 已提交
2200
        return 1 << ot;
2201
    } else {
B
bellard 已提交
2202
        return 4;
2203
    }
B
bellard 已提交
2204 2205
}

2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216
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 已提交
2217
        tcg_gen_goto_tb(tb_num);
2218
        gen_jmp_im(eip);
2219
        tcg_gen_exit_tb((uintptr_t)tb + tb_num);
2220 2221 2222 2223 2224 2225 2226
    } else {
        /* jump to another page: currently not optimized */
        gen_jmp_im(eip);
        gen_eob(s);
    }
}

2227
static inline void gen_jcc(DisasContext *s, int b,
B
bellard 已提交
2228
                           target_ulong val, target_ulong next_eip)
B
bellard 已提交
2229
{
2230
    int l1, l2;
2231

B
bellard 已提交
2232
    if (s->jmp_opt) {
B
bellard 已提交
2233
        l1 = gen_new_label();
2234
        gen_jcc1(s, b, l1);
2235

2236
        gen_goto_tb(s, 0, next_eip);
B
bellard 已提交
2237 2238

        gen_set_label(l1);
2239
        gen_goto_tb(s, 1, val);
J
Jun Koi 已提交
2240
        s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2241
    } else {
B
bellard 已提交
2242 2243
        l1 = gen_new_label();
        l2 = gen_new_label();
2244
        gen_jcc1(s, b, l1);
2245

B
bellard 已提交
2246
        gen_jmp_im(next_eip);
2247 2248
        tcg_gen_br(l2);

B
bellard 已提交
2249 2250 2251
        gen_set_label(l1);
        gen_jmp_im(val);
        gen_set_label(l2);
B
bellard 已提交
2252 2253 2254 2255
        gen_eob(s);
    }
}

2256
static void gen_cmovcc1(CPUX86State *env, DisasContext *s, TCGMemOp ot, int b,
2257 2258
                        int modrm, int reg)
{
2259
    CCPrepare cc;
2260

2261
    gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
2262

2263 2264 2265 2266 2267 2268 2269 2270
    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);
2271 2272
    }

2273 2274
    tcg_gen_movcond_tl(cc.cond, cpu_T[0], cc.reg, cc.reg2,
                       cpu_T[0], cpu_regs[reg]);
2275
    gen_op_mov_reg_v(ot, reg, cpu_T[0]);
2276 2277 2278 2279 2280 2281 2282

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

2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300
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 已提交
2301 2302
/* move T0 to seg_reg and compute if the CPU state may change. Never
   call this function with seg_reg == R_CS */
B
bellard 已提交
2303
static void gen_movl_seg_T0(DisasContext *s, int seg_reg, target_ulong cur_eip)
B
bellard 已提交
2304
{
2305 2306
    if (s->pe && !s->vm86) {
        /* XXX: optimize by finding processor state dynamically */
2307
        gen_update_cc_op(s);
B
bellard 已提交
2308
        gen_jmp_im(cur_eip);
2309
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
2310
        gen_helper_load_seg(cpu_env, tcg_const_i32(seg_reg), cpu_tmp2_i32);
B
bellard 已提交
2311 2312 2313 2314 2315
        /* 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 已提交
2316
            s->is_jmp = DISAS_TB_JUMP;
2317
    } else {
2318
        gen_op_movl_seg_T0_vm(seg_reg);
B
bellard 已提交
2319
        if (seg_reg == R_SS)
J
Jun Koi 已提交
2320
            s->is_jmp = DISAS_TB_JUMP;
2321
    }
B
bellard 已提交
2322 2323
}

T
ths 已提交
2324 2325 2326 2327 2328
static inline int svm_is_rep(int prefixes)
{
    return ((prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) ? 8 : 0);
}

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

B
bellard 已提交
2342
static inline void
T
ths 已提交
2343 2344
gen_svm_check_intercept(DisasContext *s, target_ulong pc_start, uint64_t type)
{
B
bellard 已提交
2345
    gen_svm_check_intercept_param(s, pc_start, type, 0);
T
ths 已提交
2346 2347
}

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

2362 2363
/* Generate a push. It depends on ss32, addseg and dflag.  */
static void gen_push_v(DisasContext *s, TCGv val)
B
bellard 已提交
2364
{
2365 2366 2367 2368 2369
    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 已提交
2370

B
bellard 已提交
2371
    if (CODE64(s)) {
2372 2373 2374 2375 2376 2377
        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);
2378
            gen_op_addl_A0_seg(s, R_SS);
2379 2380
        } else {
            tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
B
bellard 已提交
2381
        }
2382 2383 2384 2385 2386 2387
    } 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 已提交
2388
    }
2389 2390 2391

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

2394
/* two step pop is necessary for precise exceptions */
2395
static TCGMemOp gen_pop_T0(DisasContext *s)
B
bellard 已提交
2396
{
2397 2398 2399
    TCGMemOp d_ot = mo_pushpop(s, s->dflag);
    TCGv addr = cpu_A0;

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

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

2416
static void gen_pop_update(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
2417
{
2418
    gen_stack_update(s, 1 << ot);
B
bellard 已提交
2419 2420 2421 2422
}

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

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

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

static void gen_enter(DisasContext *s, int esp_addend, int level)
{
2474 2475
    TCGMemOp ot = mo_pushpop(s, s->dflag);
    int opsize = 1 << ot;
B
bellard 已提交
2476 2477

    level &= 0x1f;
2478 2479
#ifdef TARGET_X86_64
    if (CODE64(s)) {
B
bellard 已提交
2480
        gen_op_movl_A0_reg(R_ESP);
2481
        gen_op_addq_A0_im(-opsize);
2482
        tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2483 2484

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

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

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

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

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

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

B
bellard 已提交
2585 2586 2587 2588 2589
static void gen_jmp(DisasContext *s, target_ulong eip)
{
    gen_jmp_tb(s, eip, 0);
}

2590
static inline void gen_ldq_env_A0(DisasContext *s, int offset)
B
bellard 已提交
2591
{
2592
    tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
2593
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset);
B
bellard 已提交
2594
}
B
bellard 已提交
2595

2596
static inline void gen_stq_env_A0(DisasContext *s, int offset)
B
bellard 已提交
2597
{
2598
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset);
2599
    tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
B
bellard 已提交
2600
}
B
bellard 已提交
2601

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

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

B
bellard 已提交
2622 2623
static inline void gen_op_movo(int d_offset, int s_offset)
{
2624 2625 2626 2627
    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 已提交
2628 2629 2630 2631
}

static inline void gen_op_movq(int d_offset, int s_offset)
{
2632 2633
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
B
bellard 已提交
2634 2635 2636 2637
}

static inline void gen_op_movl(int d_offset, int s_offset)
{
2638 2639
    tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env, s_offset);
    tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, d_offset);
B
bellard 已提交
2640 2641 2642 2643
}

static inline void gen_op_movq_env_0(int d_offset)
{
2644 2645
    tcg_gen_movi_i64(cpu_tmp1_i64, 0);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
B
bellard 已提交
2646
}
B
bellard 已提交
2647

B
Blue Swirl 已提交
2648 2649 2650 2651 2652 2653 2654
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 已提交
2655
typedef void (*SSEFunc_0_ppi)(TCGv_ptr reg_a, TCGv_ptr reg_b, TCGv_i32 val);
B
Blue Swirl 已提交
2656 2657
typedef void (*SSEFunc_0_eppt)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
                               TCGv val);
B
Blue Swirl 已提交
2658

B
bellard 已提交
2659 2660
#define SSE_SPECIAL ((void *)1)
#define SSE_DUMMY ((void *)2)
B
bellard 已提交
2661

P
pbrook 已提交
2662 2663 2664
#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 已提交
2665

B
Blue Swirl 已提交
2666
static const SSEFunc_0_epp sse_op_table1[256][4] = {
A
aurel32 已提交
2667 2668 2669
    /* 3DNow! extensions */
    [0x0e] = { SSE_DUMMY }, /* femms */
    [0x0f] = { SSE_DUMMY }, /* pf... */
B
bellard 已提交
2670 2671 2672
    /* 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 已提交
2673
    [0x12] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movlps, movlpd, movsldup, movddup */
B
bellard 已提交
2674
    [0x13] = { SSE_SPECIAL, SSE_SPECIAL },  /* movlps, movlpd */
P
pbrook 已提交
2675 2676
    [0x14] = { gen_helper_punpckldq_xmm, gen_helper_punpcklqdq_xmm },
    [0x15] = { gen_helper_punpckhdq_xmm, gen_helper_punpckhqdq_xmm },
B
bellard 已提交
2677 2678 2679 2680 2681 2682
    [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 */
2683
    [0x2b] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movntps, movntpd, movntss, movntsd */
B
bellard 已提交
2684 2685
    [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 已提交
2686 2687
    [0x2e] = { gen_helper_ucomiss, gen_helper_ucomisd },
    [0x2f] = { gen_helper_comiss, gen_helper_comisd },
B
bellard 已提交
2688 2689
    [0x50] = { SSE_SPECIAL, SSE_SPECIAL }, /* movmskps, movmskpd */
    [0x51] = SSE_FOP(sqrt),
P
pbrook 已提交
2690 2691 2692 2693 2694 2695
    [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 已提交
2696 2697
    [0x58] = SSE_FOP(add),
    [0x59] = SSE_FOP(mul),
P
pbrook 已提交
2698 2699 2700
    [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 已提交
2701 2702 2703 2704 2705 2706
    [0x5c] = SSE_FOP(sub),
    [0x5d] = SSE_FOP(min),
    [0x5e] = SSE_FOP(div),
    [0x5f] = SSE_FOP(max),

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

R
Richard Henderson 已提交
2710 2711 2712
    /* 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 已提交
2713

B
bellard 已提交
2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726
    /* 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 已提交
2727 2728
    [0x6c] = { NULL, gen_helper_punpcklqdq_xmm },
    [0x6d] = { NULL, gen_helper_punpckhqdq_xmm },
B
bellard 已提交
2729 2730
    [0x6e] = { SSE_SPECIAL, SSE_SPECIAL }, /* movd mm, ea */
    [0x6f] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movq, movdqa, , movqdu */
B
Blue Swirl 已提交
2731 2732 2733 2734
    [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 已提交
2735 2736 2737 2738 2739 2740
    [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 已提交
2741
    [0x77] = { SSE_DUMMY }, /* emms */
2742 2743
    [0x78] = { NULL, SSE_SPECIAL, NULL, SSE_SPECIAL }, /* extrq_i, insertq_i */
    [0x79] = { NULL, gen_helper_extrq_r, NULL, gen_helper_insertq_r },
P
pbrook 已提交
2744 2745
    [0x7c] = { NULL, gen_helper_haddpd, NULL, gen_helper_haddps },
    [0x7d] = { NULL, gen_helper_hsubpd, NULL, gen_helper_hsubps },
B
bellard 已提交
2746 2747 2748 2749
    [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 已提交
2750
    [0xd0] = { NULL, gen_helper_addsubpd, NULL, gen_helper_addsubps },
B
bellard 已提交
2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771
    [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 已提交
2772
    [0xe6] = { NULL, gen_helper_cvttpd2dq, gen_helper_cvtdq2pd, gen_helper_cvtpd2dq },
B
bellard 已提交
2773 2774 2775 2776 2777 2778 2779 2780 2781
    [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 已提交
2782
    [0xf0] = { NULL, NULL, NULL, SSE_SPECIAL }, /* lddqu */
B
bellard 已提交
2783 2784 2785 2786 2787 2788
    [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 已提交
2789 2790
    [0xf7] = { (SSEFunc_0_epp)gen_helper_maskmov_mmx,
               (SSEFunc_0_epp)gen_helper_maskmov_xmm }, /* XXX: casts */
B
bellard 已提交
2791 2792 2793 2794 2795 2796 2797 2798 2799
    [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 已提交
2800
static const SSEFunc_0_epp sse_op_table2[3 * 8][2] = {
B
bellard 已提交
2801 2802 2803 2804 2805 2806 2807
    [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 已提交
2808
    [16 + 3] = { NULL, gen_helper_psrldq_xmm },
B
bellard 已提交
2809
    [16 + 6] = MMX_OP2(psllq),
P
pbrook 已提交
2810
    [16 + 7] = { NULL, gen_helper_pslldq_xmm },
B
bellard 已提交
2811 2812
};

B
Blue Swirl 已提交
2813
static const SSEFunc_0_epi sse_op_table3ai[] = {
P
pbrook 已提交
2814
    gen_helper_cvtsi2ss,
2815
    gen_helper_cvtsi2sd
B
Blue Swirl 已提交
2816
};
P
pbrook 已提交
2817

2818
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
2819
static const SSEFunc_0_epl sse_op_table3aq[] = {
2820 2821 2822 2823 2824
    gen_helper_cvtsq2ss,
    gen_helper_cvtsq2sd
};
#endif

B
Blue Swirl 已提交
2825
static const SSEFunc_i_ep sse_op_table3bi[] = {
P
pbrook 已提交
2826 2827
    gen_helper_cvttss2si,
    gen_helper_cvtss2si,
2828
    gen_helper_cvttsd2si,
2829
    gen_helper_cvtsd2si
B
bellard 已提交
2830
};
2831

2832
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
2833
static const SSEFunc_l_ep sse_op_table3bq[] = {
2834 2835
    gen_helper_cvttss2sq,
    gen_helper_cvtss2sq,
2836
    gen_helper_cvttsd2sq,
2837 2838 2839 2840
    gen_helper_cvtsd2sq
};
#endif

B
Blue Swirl 已提交
2841
static const SSEFunc_0_epp sse_op_table4[8][4] = {
B
bellard 已提交
2842 2843 2844 2845 2846 2847 2848 2849 2850
    SSE_FOP(cmpeq),
    SSE_FOP(cmplt),
    SSE_FOP(cmple),
    SSE_FOP(cmpunord),
    SSE_FOP(cmpneq),
    SSE_FOP(cmpnlt),
    SSE_FOP(cmpnle),
    SSE_FOP(cmpord),
};
2851

B
Blue Swirl 已提交
2852
static const SSEFunc_0_epp sse_op_table5[256] = {
P
pbrook 已提交
2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876
    [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 已提交
2877 2878
};

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

B
Blue Swirl 已提交
2884 2885
struct SSEOpHelper_eppi {
    SSEFunc_0_eppi op[2];
B
Blue Swirl 已提交
2886
    uint32_t ext_mask;
B
balrog 已提交
2887
};
B
Blue Swirl 已提交
2888

B
balrog 已提交
2889
#define SSSE3_OP(x) { MMX_OP2(x), CPUID_EXT_SSSE3 }
P
pbrook 已提交
2890 2891
#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 已提交
2892
#define SSE41_SPECIAL { { NULL, SSE_SPECIAL }, CPUID_EXT_SSE41 }
2893 2894
#define PCLMULQDQ_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, \
        CPUID_EXT_PCLMULQDQ }
2895
#define AESNI_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_AES }
B
Blue Swirl 已提交
2896

B
Blue Swirl 已提交
2897
static const struct SSEOpHelper_epp sse_op_table6[256] = {
B
balrog 已提交
2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943
    [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),
2944 2945 2946 2947 2948
    [0xdb] = AESNI_OP(aesimc),
    [0xdc] = AESNI_OP(aesenc),
    [0xdd] = AESNI_OP(aesenclast),
    [0xde] = AESNI_OP(aesdec),
    [0xdf] = AESNI_OP(aesdeclast),
B
balrog 已提交
2949 2950
};

B
Blue Swirl 已提交
2951
static const struct SSEOpHelper_eppi sse_op_table7[256] = {
B
balrog 已提交
2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969
    [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),
2970
    [0x44] = PCLMULQDQ_OP(pclmulqdq),
B
balrog 已提交
2971 2972 2973 2974
    [0x60] = SSE42_OP(pcmpestrm),
    [0x61] = SSE42_OP(pcmpestri),
    [0x62] = SSE42_OP(pcmpistrm),
    [0x63] = SSE42_OP(pcmpistri),
2975
    [0xdf] = AESNI_OP(aeskeygenassist),
B
balrog 已提交
2976 2977
};

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

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

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

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

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

B
Blue Swirl 已提交
3624 3625
            sse_fn_epp = sse_op_table6[b].op[b1];
            if (!sse_fn_epp) {
B
balrog 已提交
3626
                goto illegal_op;
B
Blue Swirl 已提交
3627
            }
B
balrog 已提交
3628 3629
            if (!(s->cpuid_ext_features & sse_op_table6[b].ext_mask))
                goto illegal_op;
B
balrog 已提交
3630 3631 3632 3633 3634 3635 3636

            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);
3637
                    gen_lea_modrm(env, s, modrm);
B
balrog 已提交
3638 3639 3640 3641
                    switch (b) {
                    case 0x20: case 0x30: /* pmovsxbw, pmovzxbw */
                    case 0x23: case 0x33: /* pmovsxwd, pmovzxwd */
                    case 0x25: case 0x35: /* pmovsxdq, pmovzxdq */
3642
                        gen_ldq_env_A0(s, op2_offset +
B
balrog 已提交
3643 3644 3645 3646
                                        offsetof(XMMReg, XMM_Q(0)));
                        break;
                    case 0x21: case 0x31: /* pmovsxbd, pmovzxbd */
                    case 0x24: case 0x34: /* pmovsxwq, pmovzxwq */
3647 3648
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
balrog 已提交
3649 3650 3651 3652
                        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, op2_offset +
                                        offsetof(XMMReg, XMM_L(0)));
                        break;
                    case 0x22: case 0x32: /* pmovsxbq, pmovzxbq */
3653 3654
                        tcg_gen_qemu_ld_tl(cpu_tmp0, cpu_A0,
                                           s->mem_index, MO_LEUW);
B
balrog 已提交
3655 3656 3657 3658
                        tcg_gen_st16_tl(cpu_tmp0, cpu_env, op2_offset +
                                        offsetof(XMMReg, XMM_W(0)));
                        break;
                    case 0x2a:            /* movntqda */
3659
                        gen_ldo_env_A0(s, op1_offset);
B
balrog 已提交
3660 3661
                        return;
                    default:
3662
                        gen_ldo_env_A0(s, op2_offset);
B
balrog 已提交
3663
                    }
B
balrog 已提交
3664 3665 3666 3667 3668 3669 3670
                }
            } 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);
3671
                    gen_lea_modrm(env, s, modrm);
3672
                    gen_ldq_env_A0(s, op2_offset);
B
balrog 已提交
3673 3674
                }
            }
B
Blue Swirl 已提交
3675
            if (sse_fn_epp == SSE_SPECIAL) {
B
balrog 已提交
3676
                goto illegal_op;
B
Blue Swirl 已提交
3677
            }
B
balrog 已提交
3678

B
balrog 已提交
3679 3680
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
3681
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
balrog 已提交
3682

3683 3684 3685
            if (b == 0x17) {
                set_cc_op(s, CC_OP_EFLAGS);
            }
B
balrog 已提交
3686
            break;
R
Richard Henderson 已提交
3687 3688 3689 3690 3691 3692

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

R
Richard Henderson 已提交
3696 3697 3698 3699 3700 3701 3702 3703
            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) {
3704
                    ot = MO_8;
3705
                } else if (s->dflag != MO_64) {
3706
                    ot = (s->prefix & PREFIX_DATA ? MO_16 : MO_32);
R
Richard Henderson 已提交
3707
                } else {
3708
                    ot = MO_64;
R
Richard Henderson 已提交
3709
                }
B
balrog 已提交
3710

3711
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[reg]);
R
Richard Henderson 已提交
3712 3713 3714
                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 已提交
3715

3716
                ot = mo_64_32(s->dflag);
3717
                gen_op_mov_reg_v(ot, reg, cpu_T[0]);
R
Richard Henderson 已提交
3718
                break;
B
balrog 已提交
3719

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

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

R
Richard Henderson 已提交
3751 3752 3753 3754 3755 3756
            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;
                }
3757
                ot = mo_64_32(s->dflag);
R
Richard Henderson 已提交
3758 3759
                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]);
3760
                gen_op_mov_reg_v(ot, reg, cpu_T[0]);
R
Richard Henderson 已提交
3761 3762 3763 3764
                gen_op_update1_cc();
                set_cc_op(s, CC_OP_LOGICB + ot);
                break;

R
Richard Henderson 已提交
3765 3766 3767 3768 3769 3770
            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;
                }
3771
                ot = mo_64_32(s->dflag);
R
Richard Henderson 已提交
3772 3773 3774 3775 3776 3777 3778 3779 3780
                {
                    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);

3781
                    bound = tcg_const_tl(ot == MO_64 ? 63 : 31);
R
Richard Henderson 已提交
3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798
                    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]);

3799
                    gen_op_mov_reg_v(ot, reg, cpu_T[0]);
R
Richard Henderson 已提交
3800 3801 3802 3803 3804
                    gen_op_update1_cc();
                    set_cc_op(s, CC_OP_LOGICB + ot);
                }
                break;

R
Richard Henderson 已提交
3805 3806 3807 3808 3809 3810
            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;
                }
3811
                ot = mo_64_32(s->dflag);
R
Richard Henderson 已提交
3812 3813 3814
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                tcg_gen_ext8u_tl(cpu_T[1], cpu_regs[s->vex_v]);
                {
3815
                    TCGv bound = tcg_const_tl(ot == MO_64 ? 63 : 31);
R
Richard Henderson 已提交
3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826
                    /* 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);
3827
                gen_op_mov_reg_v(ot, reg, cpu_T[0]);
R
Richard Henderson 已提交
3828 3829 3830 3831
                gen_op_update1_cc();
                set_cc_op(s, CC_OP_BMILGB + ot);
                break;

R
Richard Henderson 已提交
3832 3833 3834 3835 3836 3837
            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;
                }
3838
                ot = mo_64_32(s->dflag);
R
Richard Henderson 已提交
3839 3840 3841
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                switch (ot) {
                default:
3842 3843 3844 3845 3846 3847
                    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 已提交
3848 3849
                    break;
#ifdef TARGET_X86_64
3850
                case MO_64:
3851 3852
                    tcg_gen_mulu2_i64(cpu_regs[s->vex_v], cpu_regs[reg],
                                      cpu_T[0], cpu_regs[R_EDX]);
R
Richard Henderson 已提交
3853 3854 3855 3856 3857
                    break;
#endif
                }
                break;

3858 3859 3860 3861 3862 3863
            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;
                }
3864
                ot = mo_64_32(s->dflag);
3865 3866 3867
                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.  */
3868
                if (ot == MO_64) {
3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881
                    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;
                }
3882
                ot = mo_64_32(s->dflag);
3883 3884 3885
                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.  */
3886
                if (ot == MO_64) {
3887 3888 3889 3890 3891 3892 3893
                    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;

3894 3895 3896 3897 3898
            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 {
3899
                    TCGv carry_in, carry_out, zero;
3900 3901
                    int end_op;

3902
                    ot = mo_64_32(s->dflag);
3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929
                    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:
3930
                        end_op = (b == 0x1f6 ? CC_OP_ADCX : CC_OP_ADOX);
3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945
                        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
3946
                    case MO_32:
3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958
                        /* 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.  */
3959 3960 3961 3962 3963 3964 3965 3966
                        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);
3967 3968 3969 3970 3971 3972
                        break;
                    }
                    set_cc_op(s, end_op);
                }
                break;

3973 3974 3975 3976 3977 3978 3979 3980
            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;
                }
3981
                ot = mo_64_32(s->dflag);
3982
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
3983
                if (ot == MO_64) {
3984 3985 3986 3987 3988 3989 3990
                    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) {
3991
                    if (ot != MO_64) {
3992 3993 3994 3995
                        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
                    }
                    tcg_gen_sar_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
                } else {
3996
                    if (ot != MO_64) {
3997 3998 3999 4000
                        tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
                    }
                    tcg_gen_shr_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
                }
4001
                gen_op_mov_reg_v(ot, reg, cpu_T[0]);
4002 4003
                break;

4004 4005 4006 4007 4008 4009 4010 4011 4012
            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;
                }
4013
                ot = mo_64_32(s->dflag);
4014 4015 4016 4017 4018 4019
                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]);
4020
                    gen_op_mov_reg_v(ot, s->vex_v, cpu_T[0]);
4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045
                    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 已提交
4046 4047 4048
            default:
                goto illegal_op;
            }
B
balrog 已提交
4049
            break;
R
Richard Henderson 已提交
4050

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

B
Blue Swirl 已提交
4062 4063
            sse_fn_eppi = sse_op_table7[b].op[b1];
            if (!sse_fn_eppi) {
B
balrog 已提交
4064
                goto illegal_op;
B
Blue Swirl 已提交
4065
            }
B
balrog 已提交
4066 4067 4068
            if (!(s->cpuid_ext_features & sse_op_table7[b].ext_mask))
                goto illegal_op;

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

            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);
4210
                    gen_lea_modrm(env, s, modrm);
4211
                    gen_ldo_env_A0(s, op2_offset);
B
balrog 已提交
4212 4213 4214 4215 4216 4217 4218
                }
            } 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);
4219
                    gen_lea_modrm(env, s, modrm);
4220
                    gen_ldq_env_A0(s, op2_offset);
B
balrog 已提交
4221 4222
                }
            }
4223
            val = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
4224

B
balrog 已提交
4225
            if ((b & 0xfc) == 0x60) { /* pcmpXstrX */
4226
                set_cc_op(s, CC_OP_EFLAGS);
B
balrog 已提交
4227

4228
                if (s->dflag == MO_64) {
B
balrog 已提交
4229 4230
                    /* The helper must use entire 64-bit gp registers */
                    val |= 1 << 8;
4231
                }
B
balrog 已提交
4232 4233
            }

B
balrog 已提交
4234 4235
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4236
            sse_fn_eppi(cpu_env, cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
B
balrog 已提交
4237
            break;
R
Richard Henderson 已提交
4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251

        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;
                }
4252
                ot = mo_64_32(s->dflag);
R
Richard Henderson 已提交
4253 4254
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                b = cpu_ldub_code(env, s->pc++);
4255
                if (ot == MO_64) {
R
Richard Henderson 已提交
4256 4257 4258 4259 4260 4261
                    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);
                }
4262
                gen_op_mov_reg_v(ot, reg, cpu_T[0]);
R
Richard Henderson 已提交
4263 4264 4265 4266 4267 4268 4269
                break;

            default:
                goto illegal_op;
            }
            break;

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

4289
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
4290
                op2_offset = offsetof(CPUX86State,xmm_t0);
4291 4292 4293 4294 4295 4296

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

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

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

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

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

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

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

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

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

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

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

4597
            ot = mo_b_d(b, dflag);
4598

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

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

4654
            ot = mo_b_d(b, dflag);
4655

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

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

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

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

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

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

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

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

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

5020
        gen_op_mov_v_reg(ot, cpu_T[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_v_reg(MO_32, cpu_T[0], R_EAX);
B
bellard 已提交
5031
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
5032
            gen_op_mov_reg_v(MO_64, R_EAX, cpu_T[0]);
5033
            break;
B
bellard 已提交
5034
#endif
5035
        case MO_32:
5036
            gen_op_mov_v_reg(MO_16, cpu_T[0], R_EAX);
B
bellard 已提交
5037
            tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
5038
            gen_op_mov_reg_v(MO_32, R_EAX, cpu_T[0]);
5039 5040
            break;
        case MO_16:
5041
            gen_op_mov_v_reg(MO_8, cpu_T[0], R_EAX);
B
bellard 已提交
5042
            tcg_gen_ext8s_tl(cpu_T[0], cpu_T[0]);
5043
            gen_op_mov_reg_v(MO_16, R_EAX, cpu_T[0]);
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_v_reg(MO_64, cpu_T[0], R_EAX);
B
bellard 已提交
5054
            tcg_gen_sari_tl(cpu_T[0], cpu_T[0], 63);
5055
            gen_op_mov_reg_v(MO_64, R_EDX, cpu_T[0]);
5056
            break;
B
bellard 已提交
5057
#endif
5058
        case MO_32:
5059
            gen_op_mov_v_reg(MO_32, cpu_T[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_v(MO_32, R_EDX, cpu_T[0]);
5063 5064
            break;
        case MO_16:
5065
            gen_op_mov_v_reg(MO_16, cpu_T[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_v(MO_16, R_EDX, cpu_T[0]);
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 {
5092
            gen_op_mov_v_reg(ot, cpu_T[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
            gen_op_mov_reg_v(ot, reg, cpu_T[0]);
5123
            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);
5135 5136
            gen_op_mov_v_reg(ot, cpu_T[0], reg);
            gen_op_mov_v_reg(ot, cpu_T[1], rm);
5137
            tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
5138
            gen_op_mov_reg_v(ot, reg, cpu_T[1]);
5139
            gen_op_mov_reg_v(ot, rm, cpu_T[0]);
B
bellard 已提交
5140
        } else {
5141
            gen_lea_modrm(env, s, modrm);
5142
            gen_op_mov_v_reg(ot, cpu_T[0], reg);
5143
            gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
5144
            tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
5145
            gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
5146
            gen_op_mov_reg_v(ot, reg, cpu_T[1]);
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_v_reg(MO_32, cpu_T[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 = gen_pop_T0(s);
B
bellard 已提交
5241
        /* NOTE: order is important for pop %sp */
5242
        gen_pop_update(s, ot);
5243
        gen_op_mov_reg_v(ot, (b & 7) | REX_B(s), cpu_T[0]);
B
bellard 已提交
5244 5245
        break;
    case 0x60: /* pusha */
B
bellard 已提交
5246 5247
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5248 5249 5250
        gen_pusha(s);
        break;
    case 0x61: /* popa */
B
bellard 已提交
5251 5252
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5253 5254 5255 5256
        gen_popa(s);
        break;
    case 0x68: /* push Iv */
    case 0x6a:
5257
        ot = mo_pushpop(s, dflag);
B
bellard 已提交
5258
        if (b == 0x68)
5259
            val = insn_get(env, s, ot);
B
bellard 已提交
5260
        else
5261
            val = (int8_t)insn_get(env, s, MO_8);
5262
        tcg_gen_movi_tl(cpu_T[0], val);
5263
        gen_push_v(s, cpu_T[0]);
B
bellard 已提交
5264 5265
        break;
    case 0x8f: /* pop Ev */
5266
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5267
        mod = (modrm >> 6) & 3;
5268
        ot = gen_pop_T0(s);
B
bellard 已提交
5269 5270
        if (mod == 3) {
            /* NOTE: order is important for pop %sp */
5271
            gen_pop_update(s, ot);
B
bellard 已提交
5272
            rm = (modrm & 7) | REX_B(s);
5273
            gen_op_mov_reg_v(ot, rm, cpu_T[0]);
B
bellard 已提交
5274 5275
        } else {
            /* NOTE: order is important too for MMU exceptions */
B
bellard 已提交
5276
            s->popl_esp_hack = 1 << ot;
5277
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
5278
            s->popl_esp_hack = 0;
5279
            gen_pop_update(s, ot);
B
bellard 已提交
5280
        }
B
bellard 已提交
5281 5282 5283 5284
        break;
    case 0xc8: /* enter */
        {
            int level;
5285
            val = cpu_lduw_code(env, s->pc);
B
bellard 已提交
5286
            s->pc += 2;
5287
            level = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5288 5289 5290 5291 5292
            gen_enter(s, val, level);
        }
        break;
    case 0xc9: /* leave */
        /* XXX: exception not precise (ESP is updated before potential exception) */
B
bellard 已提交
5293
        if (CODE64(s)) {
5294
            gen_op_mov_v_reg(MO_64, cpu_T[0], R_EBP);
5295
            gen_op_mov_reg_v(MO_64, R_ESP, cpu_T[0]);
B
bellard 已提交
5296
        } else if (s->ss32) {
5297
            gen_op_mov_v_reg(MO_32, cpu_T[0], R_EBP);
5298
            gen_op_mov_reg_v(MO_32, R_ESP, cpu_T[0]);
B
bellard 已提交
5299
        } else {
5300
            gen_op_mov_v_reg(MO_16, cpu_T[0], R_EBP);
5301
            gen_op_mov_reg_v(MO_16, R_ESP, cpu_T[0]);
B
bellard 已提交
5302
        }
5303
        ot = gen_pop_T0(s);
5304
        gen_op_mov_reg_v(ot, R_EBP, cpu_T[0]);
5305
        gen_pop_update(s, ot);
B
bellard 已提交
5306 5307 5308 5309 5310
        break;
    case 0x06: /* push es */
    case 0x0e: /* push cs */
    case 0x16: /* push ss */
    case 0x1e: /* push ds */
B
bellard 已提交
5311 5312
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5313
        gen_op_movl_T0_seg(b >> 3);
5314
        gen_push_v(s, cpu_T[0]);
B
bellard 已提交
5315 5316 5317 5318
        break;
    case 0x1a0: /* push fs */
    case 0x1a8: /* push gs */
        gen_op_movl_T0_seg((b >> 3) & 7);
5319
        gen_push_v(s, cpu_T[0]);
B
bellard 已提交
5320 5321 5322 5323
        break;
    case 0x07: /* pop es */
    case 0x17: /* pop ss */
    case 0x1f: /* pop ds */
B
bellard 已提交
5324 5325
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5326
        reg = b >> 3;
5327
        ot = gen_pop_T0(s);
B
bellard 已提交
5328
        gen_movl_seg_T0(s, reg, pc_start - s->cs_base);
5329
        gen_pop_update(s, ot);
B
bellard 已提交
5330
        if (reg == R_SS) {
5331 5332 5333 5334
            /* if reg == SS, inhibit interrupts/trace. */
            /* If several instructions disable interrupts, only the
               _first_ does it */
            if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
5335
                gen_helper_set_inhibit_irq(cpu_env);
B
bellard 已提交
5336 5337 5338
            s->tf = 0;
        }
        if (s->is_jmp) {
B
bellard 已提交
5339
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5340 5341 5342 5343 5344
            gen_eob(s);
        }
        break;
    case 0x1a1: /* pop fs */
    case 0x1a9: /* pop gs */
5345
        ot = gen_pop_T0(s);
B
bellard 已提交
5346
        gen_movl_seg_T0(s, (b >> 3) & 7, pc_start - s->cs_base);
5347
        gen_pop_update(s, ot);
B
bellard 已提交
5348
        if (s->is_jmp) {
B
bellard 已提交
5349
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5350 5351 5352 5353 5354 5355 5356 5357
            gen_eob(s);
        }
        break;

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

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

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

    case 0x1b6: /* movzbS Gv, Eb */
    case 0x1b7: /* movzwS Gv, Eb */
    case 0x1be: /* movsbS Gv, Eb */
    case 0x1bf: /* movswS Gv, Eb */
        {
5427 5428 5429
            TCGMemOp d_ot;
            TCGMemOp s_ot;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

B
bellard 已提交
6475 6476
            if (CODE64(s))
                goto illegal_op;
6477
            ot = dflag;
6478
            offset = insn_get(env, s, ot);
6479
            selector = insn_get(env, s, MO_16);
6480

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

B
bellard 已提交
6503 6504
            if (CODE64(s))
                goto illegal_op;
6505
            ot = dflag;
6506
            offset = insn_get(env, s, ot);
6507
            selector = insn_get(env, s, MO_16);
6508

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

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

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

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

        /* Delay all CC updates until after the store above.  Note that
           C is the result of the test, Z is unchanged, and the others
           are all undefined.  */
        switch (s->cc_op) {
        case CC_OP_MULB ... CC_OP_MULQ:
        case CC_OP_ADDB ... CC_OP_ADDQ:
        case CC_OP_ADCB ... CC_OP_ADCQ:
        case CC_OP_SUBB ... CC_OP_SUBQ:
        case CC_OP_SBBB ... CC_OP_SBBQ:
        case CC_OP_LOGICB ... CC_OP_LOGICQ:
        case CC_OP_INCB ... CC_OP_INCQ:
        case CC_OP_DECB ... CC_OP_DECQ:
        case CC_OP_SHLB ... CC_OP_SHLQ:
        case CC_OP_SARB ... CC_OP_SARQ:
        case CC_OP_BMILGB ... CC_OP_BMILGQ:
            /* Z was going to be computed from the non-zero status of CC_DST.
               We can get that same Z value (and the new C value) by leaving
               CC_DST alone, setting CC_SRC, and using a CC_OP_SAR of the
               same width.  */
B
bellard 已提交
6763
            tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
6764 6765 6766 6767 6768 6769 6770 6771
            set_cc_op(s, ((s->cc_op - CC_OP_MULB) & 3) + CC_OP_SARB);
            break;
        default:
            /* Otherwise, generate EFLAGS and replace the C bit.  */
            gen_compute_eflags(s);
            tcg_gen_deposit_tl(cpu_cc_src, cpu_cc_src, cpu_tmp4,
                               ctz32(CC_C), 1);
            break;
B
bellard 已提交
6772 6773
        }
        break;
6774 6775
    case 0x1bc: /* bsf / tzcnt */
    case 0x1bd: /* bsr / lzcnt */
6776
        ot = dflag;
6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793
        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 已提交
6794
            } else {
6795 6796 6797 6798 6799
                /* 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 已提交
6800
            }
6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823
            /* 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 已提交
6824
        }
6825
        gen_op_mov_reg_v(ot, reg, cpu_T[0]);
B
bellard 已提交
6826 6827 6828 6829
        break;
        /************************/
        /* bcd */
    case 0x27: /* daa */
B
bellard 已提交
6830 6831
        if (CODE64(s))
            goto illegal_op;
6832
        gen_update_cc_op(s);
6833
        gen_helper_daa(cpu_env);
6834
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6835 6836
        break;
    case 0x2f: /* das */
B
bellard 已提交
6837 6838
        if (CODE64(s))
            goto illegal_op;
6839
        gen_update_cc_op(s);
6840
        gen_helper_das(cpu_env);
6841
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6842 6843
        break;
    case 0x37: /* aaa */
B
bellard 已提交
6844 6845
        if (CODE64(s))
            goto illegal_op;
6846
        gen_update_cc_op(s);
6847
        gen_helper_aaa(cpu_env);
6848
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6849 6850
        break;
    case 0x3f: /* aas */
B
bellard 已提交
6851 6852
        if (CODE64(s))
            goto illegal_op;
6853
        gen_update_cc_op(s);
6854
        gen_helper_aas(cpu_env);
6855
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6856 6857
        break;
    case 0xd4: /* aam */
B
bellard 已提交
6858 6859
        if (CODE64(s))
            goto illegal_op;
6860
        val = cpu_ldub_code(env, s->pc++);
6861 6862 6863
        if (val == 0) {
            gen_exception(s, EXCP00_DIVZ, pc_start - s->cs_base);
        } else {
6864
            gen_helper_aam(cpu_env, tcg_const_i32(val));
6865
            set_cc_op(s, CC_OP_LOGICB);
6866
        }
B
bellard 已提交
6867 6868
        break;
    case 0xd5: /* aad */
B
bellard 已提交
6869 6870
        if (CODE64(s))
            goto illegal_op;
6871
        val = cpu_ldub_code(env, s->pc++);
6872
        gen_helper_aad(cpu_env, tcg_const_i32(val));
6873
        set_cc_op(s, CC_OP_LOGICB);
B
bellard 已提交
6874 6875 6876 6877
        break;
        /************************/
        /* misc */
    case 0x90: /* nop */
6878
        /* XXX: correct lock test for all insn */
R
Richard Henderson 已提交
6879
        if (prefixes & PREFIX_LOCK) {
6880
            goto illegal_op;
R
Richard Henderson 已提交
6881 6882 6883 6884 6885
        }
        /* 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 已提交
6886
        if (prefixes & PREFIX_REPZ) {
6887 6888 6889 6890
            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 已提交
6891
        }
B
bellard 已提交
6892 6893
        break;
    case 0x9b: /* fwait */
6894
        if ((s->flags & (HF_MP_MASK | HF_TS_MASK)) ==
B
bellard 已提交
6895 6896
            (HF_MP_MASK | HF_TS_MASK)) {
            gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
B
bellard 已提交
6897
        } else {
6898
            gen_update_cc_op(s);
B
bellard 已提交
6899
            gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6900
            gen_helper_fwait(cpu_env);
B
bellard 已提交
6901
        }
B
bellard 已提交
6902 6903 6904 6905 6906
        break;
    case 0xcc: /* int3 */
        gen_interrupt(s, EXCP03_INT3, pc_start - s->cs_base, s->pc - s->cs_base);
        break;
    case 0xcd: /* int N */
6907
        val = cpu_ldub_code(env, s->pc++);
6908
        if (s->vm86 && s->iopl != 3) {
6909
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6910 6911 6912
        } else {
            gen_interrupt(s, val, pc_start - s->cs_base, s->pc - s->cs_base);
        }
B
bellard 已提交
6913 6914
        break;
    case 0xce: /* into */
B
bellard 已提交
6915 6916
        if (CODE64(s))
            goto illegal_op;
6917
        gen_update_cc_op(s);
6918
        gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6919
        gen_helper_into(cpu_env, tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
6920
        break;
A
aurel32 已提交
6921
#ifdef WANT_ICEBP
B
bellard 已提交
6922
    case 0xf1: /* icebp (undocumented, exits to external debugger) */
B
bellard 已提交
6923
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_ICEBP);
6924
#if 1
B
bellard 已提交
6925
        gen_debug(s, pc_start - s->cs_base);
6926 6927
#else
        /* start debug */
6928
        tb_flush(env);
6929
        qemu_set_log(CPU_LOG_INT | CPU_LOG_TB_IN_ASM);
6930
#endif
B
bellard 已提交
6931
        break;
A
aurel32 已提交
6932
#endif
B
bellard 已提交
6933 6934 6935
    case 0xfa: /* cli */
        if (!s->vm86) {
            if (s->cpl <= s->iopl) {
6936
                gen_helper_cli(cpu_env);
B
bellard 已提交
6937 6938 6939 6940 6941
            } else {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            }
        } else {
            if (s->iopl == 3) {
6942
                gen_helper_cli(cpu_env);
B
bellard 已提交
6943 6944 6945 6946 6947 6948 6949 6950 6951
            } 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:
6952
                gen_helper_sti(cpu_env);
B
bellard 已提交
6953
                /* interruptions are enabled only the first insn after sti */
6954 6955 6956
                /* If several instructions disable interrupts, only the
                   _first_ does it */
                if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
6957
                    gen_helper_set_inhibit_irq(cpu_env);
B
bellard 已提交
6958
                /* give a chance to handle pending irqs */
B
bellard 已提交
6959
                gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972
                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 已提交
6973 6974
        if (CODE64(s))
            goto illegal_op;
6975
        ot = dflag;
6976
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
6977 6978 6979 6980
        reg = (modrm >> 3) & 7;
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
6981
        gen_op_mov_v_reg(ot, cpu_T[0], reg);
6982
        gen_lea_modrm(env, s, modrm);
B
bellard 已提交
6983
        gen_jmp_im(pc_start - s->cs_base);
6984
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
6985
        if (ot == MO_16) {
B
Blue Swirl 已提交
6986 6987 6988 6989
            gen_helper_boundw(cpu_env, cpu_A0, cpu_tmp2_i32);
        } else {
            gen_helper_boundl(cpu_env, cpu_A0, cpu_tmp2_i32);
        }
B
bellard 已提交
6990 6991
        break;
    case 0x1c8 ... 0x1cf: /* bswap reg */
B
bellard 已提交
6992 6993
        reg = (b & 7) | REX_B(s);
#ifdef TARGET_X86_64
6994
        if (dflag == MO_64) {
6995
            gen_op_mov_v_reg(MO_64, cpu_T[0], reg);
A
aurel32 已提交
6996
            tcg_gen_bswap64_i64(cpu_T[0], cpu_T[0]);
6997
            gen_op_mov_reg_v(MO_64, reg, cpu_T[0]);
6998
        } else
6999
#endif
B
bellard 已提交
7000
        {
7001
            gen_op_mov_v_reg(MO_32, cpu_T[0], reg);
7002 7003
            tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
            tcg_gen_bswap32_tl(cpu_T[0], cpu_T[0]);
7004
            gen_op_mov_reg_v(MO_32, reg, cpu_T[0]);
B
bellard 已提交
7005
        }
B
bellard 已提交
7006 7007
        break;
    case 0xd6: /* salc */
B
bellard 已提交
7008 7009
        if (CODE64(s))
            goto illegal_op;
7010
        gen_compute_eflags_c(s, cpu_T[0]);
7011
        tcg_gen_neg_tl(cpu_T[0], cpu_T[0]);
7012
        gen_op_mov_reg_v(MO_8, R_EAX, cpu_T[0]);
B
bellard 已提交
7013 7014 7015 7016 7017
        break;
    case 0xe0: /* loopnz */
    case 0xe1: /* loopz */
    case 0xe2: /* loop */
    case 0xe3: /* jecxz */
B
bellard 已提交
7018
        {
7019
            int l1, l2, l3;
B
bellard 已提交
7020

7021
            tval = (int8_t)insn_get(env, s, MO_8);
B
bellard 已提交
7022 7023
            next_eip = s->pc - s->cs_base;
            tval += next_eip;
7024
            if (dflag == MO_16) {
B
bellard 已提交
7025
                tval &= 0xffff;
7026
            }
7027

B
bellard 已提交
7028 7029
            l1 = gen_new_label();
            l2 = gen_new_label();
7030
            l3 = gen_new_label();
B
bellard 已提交
7031
            b &= 3;
7032 7033 7034
            switch(b) {
            case 0: /* loopnz */
            case 1: /* loopz */
7035 7036
                gen_op_add_reg_im(s->aflag, R_ECX, -1);
                gen_op_jz_ecx(s->aflag, l3);
7037
                gen_jcc1(s, (JCC_Z << 1) | (b ^ 1), l1);
7038 7039
                break;
            case 2: /* loop */
7040 7041
                gen_op_add_reg_im(s->aflag, R_ECX, -1);
                gen_op_jnz_ecx(s->aflag, l1);
7042 7043 7044
                break;
            default:
            case 3: /* jcxz */
7045
                gen_op_jz_ecx(s->aflag, l1);
7046
                break;
B
bellard 已提交
7047 7048
            }

7049
            gen_set_label(l3);
B
bellard 已提交
7050
            gen_jmp_im(next_eip);
7051
            tcg_gen_br(l2);
7052

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

7497
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7498 7499 7500
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
            rm = (modrm & 7) | REX_B(s);
7501

B
bellard 已提交
7502
            if (mod == 3) {
7503
                gen_op_mov_v_reg(MO_32, cpu_T[0], rm);
B
bellard 已提交
7504
                /* sign extend */
7505
                if (d_ot == MO_64) {
B
bellard 已提交
7506
                    tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
7507
                }
7508
                gen_op_mov_reg_v(d_ot, reg, cpu_T[0]);
B
bellard 已提交
7509
            } else {
7510
                gen_lea_modrm(env, s, modrm);
R
Richard Henderson 已提交
7511
                gen_op_ld_v(s, MO_32 | MO_SIGN, cpu_T[0], cpu_A0);
7512
                gen_op_mov_reg_v(d_ot, reg, cpu_T[0]);
B
bellard 已提交
7513
            }
7514
        } else
B
bellard 已提交
7515 7516
#endif
        {
7517
            int label1;
L
Laurent Desnogues 已提交
7518
            TCGv t0, t1, t2, a0;
7519

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

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

7814
        if (s->prefix & PREFIX_DATA) {
7815
            ot = MO_16;
7816 7817 7818
        } else {
            ot = mo_64_32(dflag);
        }
B
balrog 已提交
7819

7820
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
Blue Swirl 已提交
7821
        gen_helper_popcnt(cpu_T[0], cpu_env, cpu_T[0], tcg_const_i32(ot));
7822
        gen_op_mov_reg_v(ot, reg, cpu_T[0]);
B
balrog 已提交
7823

7824
        set_cc_op(s, CC_OP_EFLAGS);
B
balrog 已提交
7825
        break;
A
aurel32 已提交
7826 7827 7828
    case 0x10e ... 0x10f:
        /* 3DNow! instructions, ignore prefixes */
        s->prefix &= ~(PREFIX_REPZ | PREFIX_REPNZ | PREFIX_DATA);
B
bellard 已提交
7829 7830
    case 0x110 ... 0x117:
    case 0x128 ... 0x12f:
B
balrog 已提交
7831
    case 0x138 ... 0x13a:
7832
    case 0x150 ... 0x179:
B
bellard 已提交
7833 7834 7835 7836
    case 0x17c ... 0x17f:
    case 0x1c2:
    case 0x1c4 ... 0x1c6:
    case 0x1d0 ... 0x1fe:
7837
        gen_sse(env, s, b, pc_start, rex_r);
B
bellard 已提交
7838
        break;
B
bellard 已提交
7839 7840 7841 7842 7843
    default:
        goto illegal_op;
    }
    /* lock generation */
    if (s->prefix & PREFIX_LOCK)
P
pbrook 已提交
7844
        gen_helper_unlock();
B
bellard 已提交
7845 7846
    return s->pc;
 illegal_op:
7847
    if (s->prefix & PREFIX_LOCK)
P
pbrook 已提交
7848
        gen_helper_unlock();
B
bellard 已提交
7849 7850 7851 7852 7853 7854 7855
    /* 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)
{
7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886
    static const char reg_names[CPU_NB_REGS][4] = {
#ifdef TARGET_X86_64
        [R_EAX] = "rax",
        [R_EBX] = "rbx",
        [R_ECX] = "rcx",
        [R_EDX] = "rdx",
        [R_ESI] = "rsi",
        [R_EDI] = "rdi",
        [R_EBP] = "rbp",
        [R_ESP] = "rsp",
        [8]  = "r8",
        [9]  = "r9",
        [10] = "r10",
        [11] = "r11",
        [12] = "r12",
        [13] = "r13",
        [14] = "r14",
        [15] = "r15",
#else
        [R_EAX] = "eax",
        [R_EBX] = "ebx",
        [R_ECX] = "ecx",
        [R_EDX] = "edx",
        [R_ESI] = "esi",
        [R_EDI] = "edi",
        [R_EBP] = "ebp",
        [R_ESP] = "esp",
#endif
    };
    int i;

P
pbrook 已提交
7887 7888
    cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
    cpu_cc_op = tcg_global_mem_new_i32(TCG_AREG0,
7889 7890
                                       offsetof(CPUX86State, cc_op), "cc_op");
    cpu_cc_dst = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_dst),
P
pbrook 已提交
7891
                                    "cc_dst");
7892 7893
    cpu_cc_src = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_src),
                                    "cc_src");
7894 7895
    cpu_cc_src2 = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_src2),
                                     "cc_src2");
7896

7897 7898 7899 7900 7901
    for (i = 0; i < CPU_NB_REGS; ++i) {
        cpu_regs[i] = tcg_global_mem_new(TCG_AREG0,
                                         offsetof(CPUX86State, regs[i]),
                                         reg_names[i]);
    }
B
bellard 已提交
7902 7903 7904 7905 7906
}

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

B
bellard 已提交
7924
    /* generate intermediate code */
B
bellard 已提交
7925 7926
    pc_start = tb->pc;
    cs_base = tb->cs_base;
B
bellard 已提交
7927
    flags = tb->flags;
B
bellard 已提交
7928

7929
    dc->pe = (flags >> HF_PE_SHIFT) & 1;
B
bellard 已提交
7930 7931 7932 7933 7934 7935 7936 7937
    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;
7938
    dc->singlestep_enabled = cs->singlestep_enabled;
B
bellard 已提交
7939
    dc->cc_op = CC_OP_DYNAMIC;
7940
    dc->cc_op_dirty = false;
B
bellard 已提交
7941 7942 7943 7944 7945 7946
    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) {
7947
        dc->mem_index = cpu_mmu_index(env);
B
bellard 已提交
7948
    }
7949 7950 7951 7952 7953
    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 已提交
7954 7955 7956 7957
#ifdef TARGET_X86_64
    dc->lma = (flags >> HF_LMA_SHIFT) & 1;
    dc->code64 = (flags >> HF_CS64_SHIFT) & 1;
#endif
B
bellard 已提交
7958
    dc->flags = flags;
7959
    dc->jmp_opt = !(dc->tf || cs->singlestep_enabled ||
7960
                    (flags & HF_INHIBIT_IRQ_MASK)
B
bellard 已提交
7961
#ifndef CONFIG_SOFTMMU
B
bellard 已提交
7962 7963 7964
                    || (flags & HF_SOFTMMU_MASK)
#endif
                    );
7965 7966 7967 7968 7969 7970 7971 7972 7973 7974
    /* Do not optimize repz jumps at all in icount mode, because
       rep movsS instructions are execured with different paths
       in !repz_opt and repz_opt modes. The first one was used
       always except single step mode. And this setting
       disables jumps optimization and control paths become
       equivalent in run and single step modes.
       Now there will be no jump optimization for repz in
       record/replay modes and there will always be an
       additional step for ecx=0 when icount is enabled.
     */
7975
    dc->repz_opt = !dc->jmp_opt && !(tb->cflags & CF_USE_ICOUNT);
7976 7977
#if 0
    /* check addseg logic */
B
bellard 已提交
7978
    if (!dc->addseg && (dc->vm86 || !dc->pe || !dc->code32))
7979 7980 7981
        printf("ERROR addseg\n");
#endif

P
pbrook 已提交
7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992
    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();
7993
    cpu_cc_srcT = tcg_temp_local_new();
B
bellard 已提交
7994

7995
    gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
B
bellard 已提交
7996 7997 7998 7999

    dc->is_jmp = DISAS_NEXT;
    pc_ptr = pc_start;
    lj = -1;
P
pbrook 已提交
8000 8001 8002 8003
    num_insns = 0;
    max_insns = tb->cflags & CF_COUNT_MASK;
    if (max_insns == 0)
        max_insns = CF_COUNT_MASK;
B
bellard 已提交
8004

8005
    gen_tb_start();
B
bellard 已提交
8006
    for(;;) {
8007 8008
        if (unlikely(!QTAILQ_EMPTY(&cs->breakpoints))) {
            QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {
J
Jan Kiszka 已提交
8009 8010
                if (bp->pc == pc_ptr &&
                    !((bp->flags & BP_CPU) && (tb->flags & HF_RF_MASK))) {
B
bellard 已提交
8011
                    gen_debug(dc, pc_ptr - dc->cs_base);
8012
                    goto done_generating;
B
bellard 已提交
8013 8014 8015 8016
                }
            }
        }
        if (search_pc) {
8017
            j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
B
bellard 已提交
8018 8019 8020
            if (lj < j) {
                lj++;
                while (lj < j)
8021
                    tcg_ctx.gen_opc_instr_start[lj++] = 0;
B
bellard 已提交
8022
            }
8023
            tcg_ctx.gen_opc_pc[lj] = pc_ptr;
B
bellard 已提交
8024
            gen_opc_cc_op[lj] = dc->cc_op;
8025
            tcg_ctx.gen_opc_instr_start[lj] = 1;
8026
            tcg_ctx.gen_opc_icount[lj] = num_insns;
B
bellard 已提交
8027
        }
P
pbrook 已提交
8028 8029 8030
        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
            gen_io_start();

8031
        pc_ptr = disas_insn(env, dc, pc_ptr);
P
pbrook 已提交
8032
        num_insns++;
B
bellard 已提交
8033 8034 8035 8036 8037
        /* stop translation if indicated */
        if (dc->is_jmp)
            break;
        /* if single step mode, we generate only one instruction and
           generate an exception */
8038 8039 8040
        /* 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 */
8041
        if (dc->tf || dc->singlestep_enabled ||
P
pbrook 已提交
8042
            (flags & HF_INHIBIT_IRQ_MASK)) {
B
bellard 已提交
8043
            gen_jmp_im(pc_ptr - dc->cs_base);
B
bellard 已提交
8044 8045 8046
            gen_eob(dc);
            break;
        }
8047 8048 8049 8050 8051 8052
        /* Do not cross the boundary of the pages in icount mode,
           it can cause an exception. Do it only when boundary is
           crossed by the first instruction in the block.
           If current instruction already crossed the bound - it's ok,
           because an exception hasn't stopped this code.
         */
8053
        if ((tb->cflags & CF_USE_ICOUNT)
8054 8055 8056 8057 8058 8059 8060
            && ((pc_ptr & TARGET_PAGE_MASK)
                != ((pc_ptr + TARGET_MAX_INSN_SIZE - 1) & TARGET_PAGE_MASK)
                || (pc_ptr & ~TARGET_PAGE_MASK) == 0)) {
            gen_jmp_im(pc_ptr - dc->cs_base);
            gen_eob(dc);
            break;
        }
B
bellard 已提交
8061
        /* if too long translation, stop generation too */
8062
        if (tcg_ctx.gen_opc_ptr >= gen_opc_end ||
P
pbrook 已提交
8063 8064
            (pc_ptr - pc_start) >= (TARGET_PAGE_SIZE - 32) ||
            num_insns >= max_insns) {
B
bellard 已提交
8065
            gen_jmp_im(pc_ptr - dc->cs_base);
B
bellard 已提交
8066 8067 8068
            gen_eob(dc);
            break;
        }
8069 8070 8071 8072 8073
        if (singlestep) {
            gen_jmp_im(pc_ptr - dc->cs_base);
            gen_eob(dc);
            break;
        }
B
bellard 已提交
8074
    }
P
pbrook 已提交
8075 8076
    if (tb->cflags & CF_LAST_IO)
        gen_io_end();
8077
done_generating:
8078
    gen_tb_end(tb, num_insns);
8079
    *tcg_ctx.gen_opc_ptr = INDEX_op_end;
B
bellard 已提交
8080 8081
    /* we don't forget to fill the last values */
    if (search_pc) {
8082
        j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
B
bellard 已提交
8083 8084
        lj++;
        while (lj <= j)
8085
            tcg_ctx.gen_opc_instr_start[lj++] = 0;
B
bellard 已提交
8086
    }
8087

B
bellard 已提交
8088
#ifdef DEBUG_DISAS
8089
    if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
B
bellard 已提交
8090
        int disas_flags;
8091 8092
        qemu_log("----------------\n");
        qemu_log("IN: %s\n", lookup_symbol(pc_start));
B
bellard 已提交
8093 8094 8095 8096 8097 8098
#ifdef TARGET_X86_64
        if (dc->code64)
            disas_flags = 2;
        else
#endif
            disas_flags = !dc->code32;
B
Blue Swirl 已提交
8099
        log_target_disas(env, pc_start, pc_ptr - pc_start, disas_flags);
8100
        qemu_log("\n");
B
bellard 已提交
8101 8102 8103
    }
#endif

P
pbrook 已提交
8104
    if (!search_pc) {
B
bellard 已提交
8105
        tb->size = pc_ptr - pc_start;
P
pbrook 已提交
8106 8107
        tb->icount = num_insns;
    }
B
bellard 已提交
8108 8109
}

8110
void gen_intermediate_code(CPUX86State *env, TranslationBlock *tb)
B
bellard 已提交
8111
{
8112
    gen_intermediate_code_internal(x86_env_get_cpu(env), tb, false);
B
bellard 已提交
8113 8114
}

8115
void gen_intermediate_code_pc(CPUX86State *env, TranslationBlock *tb)
B
bellard 已提交
8116
{
8117
    gen_intermediate_code_internal(x86_env_get_cpu(env), tb, true);
B
bellard 已提交
8118 8119
}

8120
void restore_state_to_opc(CPUX86State *env, TranslationBlock *tb, int pc_pos)
A
aurel32 已提交
8121 8122 8123
{
    int cc_op;
#ifdef DEBUG_DISAS
8124
    if (qemu_loglevel_mask(CPU_LOG_TB_OP)) {
A
aurel32 已提交
8125
        int i;
8126
        qemu_log("RESTORE:\n");
A
aurel32 已提交
8127
        for(i = 0;i <= pc_pos; i++) {
8128
            if (tcg_ctx.gen_opc_instr_start[i]) {
8129 8130
                qemu_log("0x%04x: " TARGET_FMT_lx "\n", i,
                        tcg_ctx.gen_opc_pc[i]);
A
aurel32 已提交
8131 8132
            }
        }
8133
        qemu_log("pc_pos=0x%x eip=" TARGET_FMT_lx " cs_base=%x\n",
8134
                pc_pos, tcg_ctx.gen_opc_pc[pc_pos] - tb->cs_base,
A
aurel32 已提交
8135 8136 8137
                (uint32_t)tb->cs_base);
    }
#endif
8138
    env->eip = tcg_ctx.gen_opc_pc[pc_pos] - tb->cs_base;
A
aurel32 已提交
8139 8140 8141 8142
    cc_op = gen_opc_cc_op[pc_pos];
    if (cc_op != CC_OP_DYNAMIC)
        env->cc_op = cc_op;
}