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

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

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

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

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

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

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

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

76 77
static uint8_t gen_opc_cc_op[OPC_BUF_SIZE];

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

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

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

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

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

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

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

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

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

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

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

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

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

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

B
bellard 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
static inline void gen_op_movl_T0_im(int32_t val)
{
    tcg_gen_movi_tl(cpu_T[0], val);
}

static inline void gen_op_movl_T0_imu(uint32_t val)
{
    tcg_gen_movi_tl(cpu_T[0], val);
}

static inline void gen_op_movl_T1_im(int32_t val)
{
    tcg_gen_movi_tl(cpu_T[1], val);
}

static inline void gen_op_movl_T1_imu(uint32_t val)
{
    tcg_gen_movi_tl(cpu_T[1], val);
}

static inline void gen_op_movl_A0_im(uint32_t val)
{
    tcg_gen_movi_tl(cpu_A0, val);
}

#ifdef TARGET_X86_64
static inline void gen_op_movq_A0_im(int64_t val)
{
    tcg_gen_movi_tl(cpu_A0, val);
}
#endif

static inline void gen_movtl_T0_im(target_ulong val)
{
    tcg_gen_movi_tl(cpu_T[0], val);
}

static inline void gen_movtl_T1_im(target_ulong val)
{
    tcg_gen_movi_tl(cpu_T[1], val);
}

static inline void gen_op_andl_T0_ffff(void)
{
    tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xffff);
}

static inline void gen_op_andl_T0_im(uint32_t val)
{
    tcg_gen_andi_tl(cpu_T[0], cpu_T[0], val);
}

static inline void gen_op_movl_T0_T1(void)
{
    tcg_gen_mov_tl(cpu_T[0], cpu_T[1]);
}

static inline void gen_op_andl_A0_ffff(void)
{
    tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffff);
}

B
bellard 已提交
317 318 319 320 321 322 323 324 325 326
#ifdef TARGET_X86_64

#define NB_OP_SIZES 4

#else /* !TARGET_X86_64 */

#define NB_OP_SIZES 3

#endif /* !TARGET_X86_64 */

327
#if defined(HOST_WORDS_BIGENDIAN)
B
bellard 已提交
328 329 330 331 332
#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 已提交
333
#else
B
bellard 已提交
334 335 336 337 338
#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 已提交
339
#endif
B
bellard 已提交
340

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
/* 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;
}

360
static inline void gen_op_mov_reg_v(int ot, int reg, TCGv t0)
B
bellard 已提交
361 362
{
    switch(ot) {
363
    case MO_8:
364
        if (!byte_reg_is_xH(reg)) {
365
            tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], t0, 0, 8);
B
bellard 已提交
366
        } else {
367
            tcg_gen_deposit_tl(cpu_regs[reg - 4], cpu_regs[reg - 4], t0, 8, 8);
B
bellard 已提交
368 369
        }
        break;
370
    case MO_16:
371
        tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], t0, 0, 16);
B
bellard 已提交
372
        break;
373
    default: /* XXX this shouldn't be reached;  abort? */
374
    case MO_32:
375 376 377
        /* 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 已提交
378
        break;
379
#ifdef TARGET_X86_64
380
    case MO_64:
381
        tcg_gen_mov_tl(cpu_regs[reg], t0);
B
bellard 已提交
382
        break;
B
bellard 已提交
383
#endif
B
bellard 已提交
384 385
    }
}
B
bellard 已提交
386

B
bellard 已提交
387 388
static inline void gen_op_mov_reg_T0(int ot, int reg)
{
389
    gen_op_mov_reg_v(ot, reg, cpu_T[0]);
B
bellard 已提交
390 391 392 393
}

static inline void gen_op_mov_reg_T1(int ot, int reg)
{
394
    gen_op_mov_reg_v(ot, reg, cpu_T[1]);
B
bellard 已提交
395 396 397 398 399
}

static inline void gen_op_mov_reg_A0(int size, int reg)
{
    switch(size) {
400
    case MO_8:
401
        tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], cpu_A0, 0, 16);
B
bellard 已提交
402
        break;
403
    default: /* XXX this shouldn't be reached;  abort? */
404
    case MO_16:
405 406 407
        /* 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], cpu_A0);
B
bellard 已提交
408
        break;
409
#ifdef TARGET_X86_64
410
    case MO_32:
411
        tcg_gen_mov_tl(cpu_regs[reg], cpu_A0);
B
bellard 已提交
412
        break;
B
bellard 已提交
413
#endif
B
bellard 已提交
414 415 416
    }
}

417
static inline void gen_op_mov_v_reg(int ot, TCGv t0, int reg)
B
bellard 已提交
418
{
419
    if (ot == MO_8 && byte_reg_is_xH(reg)) {
420 421 422
        tcg_gen_shri_tl(t0, cpu_regs[reg - 4], 8);
        tcg_gen_ext8u_tl(t0, t0);
    } else {
423
        tcg_gen_mov_tl(t0, cpu_regs[reg]);
B
bellard 已提交
424 425 426
    }
}

427 428 429 430 431
static inline void gen_op_mov_TN_reg(int ot, int t_index, int reg)
{
    gen_op_mov_v_reg(ot, cpu_T[t_index], reg);
}

B
bellard 已提交
432 433
static inline void gen_op_movl_A0_reg(int reg)
{
434
    tcg_gen_mov_tl(cpu_A0, cpu_regs[reg]);
B
bellard 已提交
435 436 437 438 439
}

static inline void gen_op_addl_A0_im(int32_t val)
{
    tcg_gen_addi_tl(cpu_A0, cpu_A0, val);
B
bellard 已提交
440
#ifdef TARGET_X86_64
B
bellard 已提交
441
    tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffffffff);
B
bellard 已提交
442
#endif
B
bellard 已提交
443
}
B
bellard 已提交
444

B
bellard 已提交
445
#ifdef TARGET_X86_64
B
bellard 已提交
446 447 448 449
static inline void gen_op_addq_A0_im(int64_t val)
{
    tcg_gen_addi_tl(cpu_A0, cpu_A0, val);
}
B
bellard 已提交
450
#endif
B
bellard 已提交
451 452 453 454 455 456 457 458 459 460
    
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 已提交
461

B
bellard 已提交
462
static inline void gen_op_addl_T0_T1(void)
B
bellard 已提交
463
{
B
bellard 已提交
464 465 466 467 468
    tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
}

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

472
static inline void gen_op_add_reg_im(int size, int reg, int32_t val)
B
bellard 已提交
473
{
474
    switch(size) {
475
    case MO_8:
476
        tcg_gen_addi_tl(cpu_tmp0, cpu_regs[reg], val);
477
        tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], cpu_tmp0, 0, 16);
478
        break;
479
    case MO_16:
480 481 482 483 484
        tcg_gen_addi_tl(cpu_tmp0, cpu_regs[reg], val);
        /* 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_tmp0, cpu_tmp0);
        tcg_gen_mov_tl(cpu_regs[reg], cpu_tmp0);
485 486
        break;
#ifdef TARGET_X86_64
487
    case MO_32:
488
        tcg_gen_addi_tl(cpu_regs[reg], cpu_regs[reg], val);
489 490 491
        break;
#endif
    }
B
bellard 已提交
492 493
}

494
static inline void gen_op_add_reg_T0(int size, int reg)
B
bellard 已提交
495
{
496
    switch(size) {
497
    case MO_8:
498
        tcg_gen_add_tl(cpu_tmp0, cpu_regs[reg], cpu_T[0]);
499
        tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], cpu_tmp0, 0, 16);
500
        break;
501
    case MO_16:
502 503 504 505 506
        tcg_gen_add_tl(cpu_tmp0, cpu_regs[reg], cpu_T[0]);
        /* 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_tmp0, cpu_tmp0);
        tcg_gen_mov_tl(cpu_regs[reg], cpu_tmp0);
507
        break;
B
bellard 已提交
508
#ifdef TARGET_X86_64
509
    case MO_32:
510
        tcg_gen_add_tl(cpu_regs[reg], cpu_regs[reg], cpu_T[0]);
511
        break;
B
bellard 已提交
512
#endif
513 514
    }
}
B
bellard 已提交
515 516 517

static inline void gen_op_addl_A0_reg_sN(int shift, int reg)
{
518 519
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[reg]);
    if (shift != 0)
B
bellard 已提交
520 521
        tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, shift);
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
522 523 524
    /* 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 已提交
525
}
B
bellard 已提交
526

B
bellard 已提交
527 528
static inline void gen_op_movl_A0_seg(int reg)
{
529
    tcg_gen_ld32u_tl(cpu_A0, cpu_env, offsetof(CPUX86State, segs[reg].base) + REG_L_OFFSET);
B
bellard 已提交
530
}
B
bellard 已提交
531

532
static inline void gen_op_addl_A0_seg(DisasContext *s, int reg)
B
bellard 已提交
533
{
534
    tcg_gen_ld_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, segs[reg].base));
B
bellard 已提交
535
#ifdef TARGET_X86_64
536 537 538 539 540 541 542 543 544
    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 已提交
545 546
#endif
}
B
bellard 已提交
547

B
bellard 已提交
548
#ifdef TARGET_X86_64
B
bellard 已提交
549 550
static inline void gen_op_movq_A0_seg(int reg)
{
551
    tcg_gen_ld_tl(cpu_A0, cpu_env, offsetof(CPUX86State, segs[reg].base));
B
bellard 已提交
552
}
B
bellard 已提交
553

B
bellard 已提交
554 555
static inline void gen_op_addq_A0_seg(int reg)
{
556
    tcg_gen_ld_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, segs[reg].base));
B
bellard 已提交
557 558 559 560 561
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
}

static inline void gen_op_movq_A0_reg(int reg)
{
562
    tcg_gen_mov_tl(cpu_A0, cpu_regs[reg]);
B
bellard 已提交
563 564 565 566
}

static inline void gen_op_addq_A0_reg_sN(int shift, int reg)
{
567 568
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[reg]);
    if (shift != 0)
B
bellard 已提交
569 570 571
        tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, shift);
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
}
B
bellard 已提交
572 573
#endif

574
static inline void gen_op_ld_v(DisasContext *s, int idx, TCGv t0, TCGv a0)
B
bellard 已提交
575
{
576
    tcg_gen_qemu_ld_tl(t0, a0, s->mem_index, idx | MO_LE);
B
bellard 已提交
577
}
B
bellard 已提交
578

579
static inline void gen_op_st_v(DisasContext *s, int idx, TCGv t0, TCGv a0)
B
bellard 已提交
580
{
581
    tcg_gen_qemu_st_tl(t0, a0, s->mem_index, idx | MO_LE);
B
bellard 已提交
582
}
583

584 585 586
static inline void gen_op_st_rm_T0_A0(DisasContext *s, int idx, int d)
{
    if (d == OR_TMP0) {
587
        gen_op_st_v(s, idx, cpu_T[0], cpu_A0);
588 589 590 591 592
    } else {
        gen_op_mov_reg_T0(idx, d);
    }
}

B
bellard 已提交
593 594
static inline void gen_jmp_im(target_ulong pc)
{
B
bellard 已提交
595
    tcg_gen_movi_tl(cpu_tmp0, pc);
596
    tcg_gen_st_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, eip));
B
bellard 已提交
597 598
}

B
bellard 已提交
599 600 601 602 603
static inline void gen_string_movl_A0_ESI(DisasContext *s)
{
    int override;

    override = s->override;
B
bellard 已提交
604 605 606
#ifdef TARGET_X86_64
    if (s->aflag == 2) {
        if (override >= 0) {
B
bellard 已提交
607 608
            gen_op_movq_A0_seg(override);
            gen_op_addq_A0_reg_sN(0, R_ESI);
B
bellard 已提交
609
        } else {
B
bellard 已提交
610
            gen_op_movq_A0_reg(R_ESI);
B
bellard 已提交
611 612 613
        }
    } else
#endif
B
bellard 已提交
614 615 616 617 618
    if (s->aflag) {
        /* 32 bit address */
        if (s->addseg && override < 0)
            override = R_DS;
        if (override >= 0) {
B
bellard 已提交
619 620
            gen_op_movl_A0_seg(override);
            gen_op_addl_A0_reg_sN(0, R_ESI);
B
bellard 已提交
621
        } else {
B
bellard 已提交
622
            gen_op_movl_A0_reg(R_ESI);
B
bellard 已提交
623 624 625 626 627
        }
    } else {
        /* 16 address, always override */
        if (override < 0)
            override = R_DS;
B
bellard 已提交
628
        gen_op_movl_A0_reg(R_ESI);
B
bellard 已提交
629
        gen_op_andl_A0_ffff();
630
        gen_op_addl_A0_seg(s, override);
B
bellard 已提交
631 632 633 634 635
    }
}

static inline void gen_string_movl_A0_EDI(DisasContext *s)
{
B
bellard 已提交
636 637
#ifdef TARGET_X86_64
    if (s->aflag == 2) {
B
bellard 已提交
638
        gen_op_movq_A0_reg(R_EDI);
B
bellard 已提交
639 640
    } else
#endif
B
bellard 已提交
641 642
    if (s->aflag) {
        if (s->addseg) {
B
bellard 已提交
643 644
            gen_op_movl_A0_seg(R_ES);
            gen_op_addl_A0_reg_sN(0, R_EDI);
B
bellard 已提交
645
        } else {
B
bellard 已提交
646
            gen_op_movl_A0_reg(R_EDI);
B
bellard 已提交
647 648
        }
    } else {
B
bellard 已提交
649
        gen_op_movl_A0_reg(R_EDI);
B
bellard 已提交
650
        gen_op_andl_A0_ffff();
651
        gen_op_addl_A0_seg(s, R_ES);
B
bellard 已提交
652 653 654
    }
}

655 656
static inline void gen_op_movl_T0_Dshift(int ot) 
{
657
    tcg_gen_ld32s_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, df));
658
    tcg_gen_shli_tl(cpu_T[0], cpu_T[0], ot);
B
bellard 已提交
659 660
};

661
static TCGv gen_ext_tl(TCGv dst, TCGv src, int size, bool sign)
662
{
663
    switch (size) {
664
    case MO_8:
665 666 667 668 669 670
        if (sign) {
            tcg_gen_ext8s_tl(dst, src);
        } else {
            tcg_gen_ext8u_tl(dst, src);
        }
        return dst;
671
    case MO_16:
672 673 674 675 676 677 678
        if (sign) {
            tcg_gen_ext16s_tl(dst, src);
        } else {
            tcg_gen_ext16u_tl(dst, src);
        }
        return dst;
#ifdef TARGET_X86_64
679
    case MO_32:
680 681 682 683 684 685 686
        if (sign) {
            tcg_gen_ext32s_tl(dst, src);
        } else {
            tcg_gen_ext32u_tl(dst, src);
        }
        return dst;
#endif
687
    default:
688
        return src;
689 690
    }
}
691

692 693 694 695 696
static void gen_extu(int ot, TCGv reg)
{
    gen_ext_tl(reg, reg, ot, false);
}

697 698
static void gen_exts(int ot, TCGv reg)
{
699
    gen_ext_tl(reg, reg, ot, true);
700
}
B
bellard 已提交
701

702 703
static inline void gen_op_jnz_ecx(int size, int label1)
{
704
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
705
    gen_extu(size + 1, cpu_tmp0);
P
pbrook 已提交
706
    tcg_gen_brcondi_tl(TCG_COND_NE, cpu_tmp0, 0, label1);
707 708 709 710
}

static inline void gen_op_jz_ecx(int size, int label1)
{
711
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
712
    gen_extu(size + 1, cpu_tmp0);
P
pbrook 已提交
713
    tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, label1);
714
}
B
bellard 已提交
715

P
pbrook 已提交
716 717 718
static void gen_helper_in_func(int ot, TCGv v, TCGv_i32 n)
{
    switch (ot) {
719
    case MO_8:
720 721
        gen_helper_inb(v, n);
        break;
722
    case MO_16:
723 724
        gen_helper_inw(v, n);
        break;
725
    case MO_32:
726 727
        gen_helper_inl(v, n);
        break;
P
pbrook 已提交
728 729
    }
}
B
bellard 已提交
730

P
pbrook 已提交
731 732 733
static void gen_helper_out_func(int ot, TCGv_i32 v, TCGv_i32 n)
{
    switch (ot) {
734
    case MO_8:
735 736
        gen_helper_outb(v, n);
        break;
737
    case MO_16:
738 739
        gen_helper_outw(v, n);
        break;
740
    case MO_32:
741 742
        gen_helper_outl(v, n);
        break;
P
pbrook 已提交
743 744
    }
}
745

746 747
static void gen_check_io(DisasContext *s, int ot, target_ulong cur_eip,
                         uint32_t svm_flags)
748
{
749 750 751 752
    int state_saved;
    target_ulong next_eip;

    state_saved = 0;
753
    if (s->pe && (s->cpl > s->iopl || s->vm86)) {
754
        gen_update_cc_op(s);
B
bellard 已提交
755
        gen_jmp_im(cur_eip);
756
        state_saved = 1;
757
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
758
        switch (ot) {
759
        case MO_8:
B
Blue Swirl 已提交
760 761
            gen_helper_check_iob(cpu_env, cpu_tmp2_i32);
            break;
762
        case MO_16:
B
Blue Swirl 已提交
763 764
            gen_helper_check_iow(cpu_env, cpu_tmp2_i32);
            break;
765
        case MO_32:
B
Blue Swirl 已提交
766 767
            gen_helper_check_iol(cpu_env, cpu_tmp2_i32);
            break;
P
pbrook 已提交
768
        }
769
    }
B
bellard 已提交
770
    if(s->flags & HF_SVMI_MASK) {
771
        if (!state_saved) {
772
            gen_update_cc_op(s);
773 774 775 776
            gen_jmp_im(cur_eip);
        }
        svm_flags |= (1 << (4 + ot));
        next_eip = s->pc - s->cs_base;
777
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
778 779
        gen_helper_svm_check_io(cpu_env, cpu_tmp2_i32,
                                tcg_const_i32(svm_flags),
P
pbrook 已提交
780
                                tcg_const_i32(next_eip - cur_eip));
781 782 783
    }
}

B
bellard 已提交
784 785 786
static inline void gen_movs(DisasContext *s, int ot)
{
    gen_string_movl_A0_ESI(s);
787
    gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
788
    gen_string_movl_A0_EDI(s);
789
    gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
790 791 792
    gen_op_movl_T0_Dshift(ot);
    gen_op_add_reg_T0(s->aflag, R_ESI);
    gen_op_add_reg_T0(s->aflag, R_EDI);
B
bellard 已提交
793 794
}

795 796 797 798 799 800 801 802 803 804 805
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]);
}

806 807 808 809 810 811 812
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]);
}

813 814 815 816 817 818 819 820
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]);
821 822
    tcg_gen_neg_tl(cpu_cc_src, cpu_T[0]);
    tcg_gen_movi_tl(cpu_cc_srcT, 0);
823 824
}

825 826
/* compute all eflags to cc_src */
static void gen_compute_eflags(DisasContext *s)
827
{
828
    TCGv zero, dst, src1, src2;
829 830
    int live, dead;

831 832 833
    if (s->cc_op == CC_OP_EFLAGS) {
        return;
    }
R
Richard Henderson 已提交
834 835 836 837 838
    if (s->cc_op == CC_OP_CLR) {
        tcg_gen_movi_tl(cpu_cc_src, CC_Z);
        set_cc_op(s, CC_OP_EFLAGS);
        return;
    }
839 840 841 842

    TCGV_UNUSED(zero);
    dst = cpu_cc_dst;
    src1 = cpu_cc_src;
843
    src2 = cpu_cc_src2;
844 845 846

    /* Take care to not read values that are not live.  */
    live = cc_op_live[s->cc_op] & ~USES_CC_SRCT;
847
    dead = live ^ (USES_CC_DST | USES_CC_SRC | USES_CC_SRC2);
848 849 850 851 852 853 854 855
    if (dead) {
        zero = tcg_const_tl(0);
        if (dead & USES_CC_DST) {
            dst = zero;
        }
        if (dead & USES_CC_SRC) {
            src1 = zero;
        }
856 857 858
        if (dead & USES_CC_SRC2) {
            src2 = zero;
        }
859 860
    }

861
    gen_update_cc_op(s);
862
    gen_helper_cc_compute_all(cpu_cc_src, dst, src1, src2, cpu_cc_op);
863
    set_cc_op(s, CC_OP_EFLAGS);
864 865 866 867

    if (dead) {
        tcg_temp_free(zero);
    }
868 869
}

870 871 872 873 874 875 876 877 878 879
typedef struct CCPrepare {
    TCGCond cond;
    TCGv reg;
    TCGv reg2;
    target_ulong imm;
    target_ulong mask;
    bool use_reg2;
    bool no_setcond;
} CCPrepare;

880
/* compute eflags.C to reg */
881
static CCPrepare gen_prepare_eflags_c(DisasContext *s, TCGv reg)
882 883
{
    TCGv t0, t1;
884
    int size, shift;
885 886 887

    switch (s->cc_op) {
    case CC_OP_SUBB ... CC_OP_SUBQ:
888
        /* (DATA_TYPE)CC_SRCT < (DATA_TYPE)CC_SRC */
889 890 891 892
        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;
893
        tcg_gen_mov_tl(t0, cpu_cc_srcT);
894 895 896 897 898 899 900 901 902
        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:
903 904
        return (CCPrepare) { .cond = TCG_COND_LTU, .reg = t0,
                             .reg2 = t1, .mask = -1, .use_reg2 = true };
905 906

    case CC_OP_LOGICB ... CC_OP_LOGICQ:
R
Richard Henderson 已提交
907
    case CC_OP_CLR:
908
        return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
909 910 911

    case CC_OP_INCB ... CC_OP_INCQ:
    case CC_OP_DECB ... CC_OP_DECQ:
912 913
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = -1, .no_setcond = true };
914 915 916 917

    case CC_OP_SHLB ... CC_OP_SHLQ:
        /* (CC_SRC >> (DATA_BITS - 1)) & 1 */
        size = s->cc_op - CC_OP_SHLB;
918 919 920
        shift = (8 << size) - 1;
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = (target_ulong)1 << shift };
921 922

    case CC_OP_MULB ... CC_OP_MULQ:
923 924
        return (CCPrepare) { .cond = TCG_COND_NE,
                             .reg = cpu_cc_src, .mask = -1 };
925

926 927 928 929 930
    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 };

931 932 933 934 935
    case CC_OP_ADCX:
    case CC_OP_ADCOX:
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_dst,
                             .mask = -1, .no_setcond = true };

936 937 938
    case CC_OP_EFLAGS:
    case CC_OP_SARB ... CC_OP_SARQ:
        /* CC_SRC & 1 */
939 940
        return (CCPrepare) { .cond = TCG_COND_NE,
                             .reg = cpu_cc_src, .mask = CC_C };
941 942 943 944 945

    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);
946 947
       gen_helper_cc_compute_c(reg, cpu_cc_dst, cpu_cc_src,
                               cpu_cc_src2, cpu_cc_op);
948 949
       return (CCPrepare) { .cond = TCG_COND_NE, .reg = reg,
                            .mask = -1, .no_setcond = true };
950 951 952
    }
}

953
/* compute eflags.P to reg */
954
static CCPrepare gen_prepare_eflags_p(DisasContext *s, TCGv reg)
955
{
956
    gen_compute_eflags(s);
957 958
    return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                         .mask = CC_P };
959 960 961
}

/* compute eflags.S to reg */
962
static CCPrepare gen_prepare_eflags_s(DisasContext *s, TCGv reg)
963
{
964 965 966 967 968
    switch (s->cc_op) {
    case CC_OP_DYNAMIC:
        gen_compute_eflags(s);
        /* FALLTHRU */
    case CC_OP_EFLAGS:
969 970 971
    case CC_OP_ADCX:
    case CC_OP_ADOX:
    case CC_OP_ADCOX:
972 973
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = CC_S };
R
Richard Henderson 已提交
974 975
    case CC_OP_CLR:
        return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
976 977 978 979
    default:
        {
            int size = (s->cc_op - CC_OP_ADDB) & 3;
            TCGv t0 = gen_ext_tl(reg, cpu_cc_dst, size, true);
980
            return (CCPrepare) { .cond = TCG_COND_LT, .reg = t0, .mask = -1 };
981 982
        }
    }
983 984 985
}

/* compute eflags.O to reg */
986
static CCPrepare gen_prepare_eflags_o(DisasContext *s, TCGv reg)
987
{
988 989 990 991 992
    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 已提交
993 994
    case CC_OP_CLR:
        return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
995 996 997 998 999
    default:
        gen_compute_eflags(s);
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = CC_O };
    }
1000 1001 1002
}

/* compute eflags.Z to reg */
1003
static CCPrepare gen_prepare_eflags_z(DisasContext *s, TCGv reg)
1004
{
1005 1006 1007 1008 1009
    switch (s->cc_op) {
    case CC_OP_DYNAMIC:
        gen_compute_eflags(s);
        /* FALLTHRU */
    case CC_OP_EFLAGS:
1010 1011 1012
    case CC_OP_ADCX:
    case CC_OP_ADOX:
    case CC_OP_ADCOX:
1013 1014
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = CC_Z };
R
Richard Henderson 已提交
1015 1016
    case CC_OP_CLR:
        return (CCPrepare) { .cond = TCG_COND_ALWAYS, .mask = -1 };
1017 1018 1019 1020
    default:
        {
            int size = (s->cc_op - CC_OP_ADDB) & 3;
            TCGv t0 = gen_ext_tl(reg, cpu_cc_dst, size, false);
1021
            return (CCPrepare) { .cond = TCG_COND_EQ, .reg = t0, .mask = -1 };
1022
        }
1023 1024 1025
    }
}

1026 1027
/* perform a conditional store into register 'reg' according to jump opcode
   value 'b'. In the fast case, T0 is guaranted not to be used. */
1028
static CCPrepare gen_prepare_cc(DisasContext *s, int b, TCGv reg)
1029
{
1030
    int inv, jcc_op, size, cond;
1031
    CCPrepare cc;
1032 1033 1034
    TCGv t0;

    inv = b & 1;
1035
    jcc_op = (b >> 1) & 7;
1036 1037

    switch (s->cc_op) {
1038 1039
    case CC_OP_SUBB ... CC_OP_SUBQ:
        /* We optimize relational operators for the cmp/jcc case.  */
1040 1041 1042
        size = s->cc_op - CC_OP_SUBB;
        switch (jcc_op) {
        case JCC_BE:
1043
            tcg_gen_mov_tl(cpu_tmp4, cpu_cc_srcT);
1044 1045
            gen_extu(size, cpu_tmp4);
            t0 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, false);
1046 1047
            cc = (CCPrepare) { .cond = TCG_COND_LEU, .reg = cpu_tmp4,
                               .reg2 = t0, .mask = -1, .use_reg2 = true };
1048
            break;
1049

1050
        case JCC_L:
1051
            cond = TCG_COND_LT;
1052 1053
            goto fast_jcc_l;
        case JCC_LE:
1054
            cond = TCG_COND_LE;
1055
        fast_jcc_l:
1056
            tcg_gen_mov_tl(cpu_tmp4, cpu_cc_srcT);
1057 1058
            gen_exts(size, cpu_tmp4);
            t0 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, true);
1059 1060
            cc = (CCPrepare) { .cond = cond, .reg = cpu_tmp4,
                               .reg2 = t0, .mask = -1, .use_reg2 = true };
1061
            break;
1062

1063
        default:
1064
            goto slow_jcc;
1065
        }
1066
        break;
1067

1068 1069
    default:
    slow_jcc:
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
        /* 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;
        }
1114
        break;
1115
    }
1116 1117 1118 1119 1120

    if (inv) {
        cc.cond = tcg_invert_cond(cc.cond);
    }
    return cc;
1121 1122
}

1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
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);
}
1157

1158 1159
/* generate a conditional jump to label 'l1' according to jump opcode
   value 'b'. In the fast case, T0 is guaranted not to be used. */
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
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.  */
1178
static inline void gen_jcc1(DisasContext *s, int b, int l1)
1179
{
1180
    CCPrepare cc = gen_prepare_cc(s, b, cpu_T[0]);
1181

1182
    gen_update_cc_op(s);
1183 1184 1185 1186
    if (cc.mask != -1) {
        tcg_gen_andi_tl(cpu_T[0], cc.reg, cc.mask);
        cc.reg = cpu_T[0];
    }
1187
    set_cc_op(s, CC_OP_DYNAMIC);
1188 1189 1190 1191
    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);
1192 1193 1194
    }
}

B
bellard 已提交
1195 1196 1197
/* 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 已提交
1198
{
B
bellard 已提交
1199 1200 1201 1202
    int l1, l2;

    l1 = gen_new_label();
    l2 = gen_new_label();
1203
    gen_op_jnz_ecx(s->aflag, l1);
B
bellard 已提交
1204 1205 1206 1207
    gen_set_label(l2);
    gen_jmp_tb(s, next_eip, 1);
    gen_set_label(l1);
    return l2;
B
bellard 已提交
1208 1209 1210 1211
}

static inline void gen_stos(DisasContext *s, int ot)
{
1212
    gen_op_mov_TN_reg(MO_32, 0, R_EAX);
B
bellard 已提交
1213
    gen_string_movl_A0_EDI(s);
1214
    gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
1215 1216
    gen_op_movl_T0_Dshift(ot);
    gen_op_add_reg_T0(s->aflag, R_EDI);
B
bellard 已提交
1217 1218 1219 1220 1221
}

static inline void gen_lods(DisasContext *s, int ot)
{
    gen_string_movl_A0_ESI(s);
1222
    gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
1223
    gen_op_mov_reg_T0(ot, R_EAX);
1224 1225
    gen_op_movl_T0_Dshift(ot);
    gen_op_add_reg_T0(s->aflag, R_ESI);
B
bellard 已提交
1226 1227 1228 1229 1230
}

static inline void gen_scas(DisasContext *s, int ot)
{
    gen_string_movl_A0_EDI(s);
1231
    gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
1232
    gen_op(s, OP_CMPL, ot, R_EAX);
1233 1234
    gen_op_movl_T0_Dshift(ot);
    gen_op_add_reg_T0(s->aflag, R_EDI);
B
bellard 已提交
1235 1236 1237 1238 1239
}

static inline void gen_cmps(DisasContext *s, int ot)
{
    gen_string_movl_A0_EDI(s);
1240
    gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
1241 1242
    gen_string_movl_A0_ESI(s);
    gen_op(s, OP_CMPL, ot, OR_TMP0);
1243 1244 1245
    gen_op_movl_T0_Dshift(ot);
    gen_op_add_reg_T0(s->aflag, R_ESI);
    gen_op_add_reg_T0(s->aflag, R_EDI);
B
bellard 已提交
1246 1247 1248 1249
}

static inline void gen_ins(DisasContext *s, int ot)
{
P
pbrook 已提交
1250 1251
    if (use_icount)
        gen_io_start();
B
bellard 已提交
1252
    gen_string_movl_A0_EDI(s);
1253 1254
    /* Note: we must do this dummy write first to be restartable in
       case of page fault. */
1255
    tcg_gen_movi_tl(cpu_T[0], 0);
1256
    gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
1257
    tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_EDX]);
1258
    tcg_gen_andi_i32(cpu_tmp2_i32, cpu_tmp2_i32, 0xffff);
P
pbrook 已提交
1259
    gen_helper_in_func(ot, cpu_T[0], cpu_tmp2_i32);
1260
    gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
1261 1262
    gen_op_movl_T0_Dshift(ot);
    gen_op_add_reg_T0(s->aflag, R_EDI);
P
pbrook 已提交
1263 1264
    if (use_icount)
        gen_io_end();
B
bellard 已提交
1265 1266 1267 1268
}

static inline void gen_outs(DisasContext *s, int ot)
{
P
pbrook 已提交
1269 1270
    if (use_icount)
        gen_io_start();
B
bellard 已提交
1271
    gen_string_movl_A0_ESI(s);
1272
    gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
1273

1274
    tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_EDX]);
1275 1276
    tcg_gen_andi_i32(cpu_tmp2_i32, cpu_tmp2_i32, 0xffff);
    tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[0]);
P
pbrook 已提交
1277
    gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
1278

1279 1280
    gen_op_movl_T0_Dshift(ot);
    gen_op_add_reg_T0(s->aflag, R_ESI);
P
pbrook 已提交
1281 1282
    if (use_icount)
        gen_io_end();
B
bellard 已提交
1283 1284 1285 1286 1287 1288
}

/* same method as Valgrind : we generate jumps to current or next
   instruction */
#define GEN_REPZ(op)                                                          \
static inline void gen_repz_ ## op(DisasContext *s, int ot,                   \
B
bellard 已提交
1289
                                 target_ulong cur_eip, target_ulong next_eip) \
B
bellard 已提交
1290
{                                                                             \
B
bellard 已提交
1291
    int l2;\
B
bellard 已提交
1292
    gen_update_cc_op(s);                                                      \
B
bellard 已提交
1293
    l2 = gen_jz_ecx_string(s, next_eip);                                      \
B
bellard 已提交
1294
    gen_ ## op(s, ot);                                                        \
1295
    gen_op_add_reg_im(s->aflag, R_ECX, -1);                                   \
B
bellard 已提交
1296 1297 1298
    /* a loop would cause two single step exceptions if ECX = 1               \
       before rep string_insn */                                              \
    if (!s->jmp_opt)                                                          \
1299
        gen_op_jz_ecx(s->aflag, l2);                                          \
B
bellard 已提交
1300 1301 1302 1303 1304
    gen_jmp(s, cur_eip);                                                      \
}

#define GEN_REPZ2(op)                                                         \
static inline void gen_repz_ ## op(DisasContext *s, int ot,                   \
B
bellard 已提交
1305 1306
                                   target_ulong cur_eip,                      \
                                   target_ulong next_eip,                     \
B
bellard 已提交
1307 1308
                                   int nz)                                    \
{                                                                             \
B
bellard 已提交
1309
    int l2;\
B
bellard 已提交
1310
    gen_update_cc_op(s);                                                      \
B
bellard 已提交
1311
    l2 = gen_jz_ecx_string(s, next_eip);                                      \
B
bellard 已提交
1312
    gen_ ## op(s, ot);                                                        \
1313
    gen_op_add_reg_im(s->aflag, R_ECX, -1);                                   \
1314
    gen_update_cc_op(s);                                                      \
1315
    gen_jcc1(s, (JCC_Z << 1) | (nz ^ 1), l2);                                 \
B
bellard 已提交
1316
    if (!s->jmp_opt)                                                          \
1317
        gen_op_jz_ecx(s->aflag, l2);                                          \
B
bellard 已提交
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
    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 已提交
1329 1330 1331
static void gen_helper_fp_arith_ST0_FT0(int op)
{
    switch (op) {
B
Blue Swirl 已提交
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
    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 已提交
1356 1357
    }
}
B
bellard 已提交
1358 1359

/* NOTE the exception in "r" op ordering */
P
pbrook 已提交
1360 1361 1362 1363
static void gen_helper_fp_arith_STN_ST0(int op, int opreg)
{
    TCGv_i32 tmp = tcg_const_i32(opreg);
    switch (op) {
B
Blue Swirl 已提交
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
    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 已提交
1382 1383
    }
}
B
bellard 已提交
1384 1385 1386 1387 1388

/* if d == OR_TMP0, it means memory operand (address in A0) */
static void gen_op(DisasContext *s1, int op, int ot, int d)
{
    if (d != OR_TMP0) {
B
bellard 已提交
1389
        gen_op_mov_TN_reg(ot, 0, d);
B
bellard 已提交
1390
    } else {
1391
        gen_op_ld_v(s1, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
1392 1393 1394
    }
    switch(op) {
    case OP_ADCL:
1395
        gen_compute_eflags_c(s1, cpu_tmp4);
B
bellard 已提交
1396 1397
        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);
1398
        gen_op_st_rm_T0_A0(s1, ot, d);
1399 1400
        gen_op_update3_cc(cpu_tmp4);
        set_cc_op(s1, CC_OP_ADCB + ot);
B
bellard 已提交
1401
        break;
B
bellard 已提交
1402
    case OP_SBBL:
1403
        gen_compute_eflags_c(s1, cpu_tmp4);
B
bellard 已提交
1404 1405
        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);
1406
        gen_op_st_rm_T0_A0(s1, ot, d);
1407 1408
        gen_op_update3_cc(cpu_tmp4);
        set_cc_op(s1, CC_OP_SBBB + ot);
B
bellard 已提交
1409
        break;
B
bellard 已提交
1410 1411
    case OP_ADDL:
        gen_op_addl_T0_T1();
1412
        gen_op_st_rm_T0_A0(s1, ot, d);
B
bellard 已提交
1413
        gen_op_update2_cc();
1414
        set_cc_op(s1, CC_OP_ADDB + ot);
B
bellard 已提交
1415 1416
        break;
    case OP_SUBL:
1417
        tcg_gen_mov_tl(cpu_cc_srcT, cpu_T[0]);
B
bellard 已提交
1418
        tcg_gen_sub_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1419
        gen_op_st_rm_T0_A0(s1, ot, d);
B
bellard 已提交
1420
        gen_op_update2_cc();
1421
        set_cc_op(s1, CC_OP_SUBB + ot);
B
bellard 已提交
1422 1423 1424
        break;
    default:
    case OP_ANDL:
B
bellard 已提交
1425
        tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1426
        gen_op_st_rm_T0_A0(s1, ot, d);
B
bellard 已提交
1427
        gen_op_update1_cc();
1428
        set_cc_op(s1, CC_OP_LOGICB + ot);
B
bellard 已提交
1429
        break;
B
bellard 已提交
1430
    case OP_ORL:
B
bellard 已提交
1431
        tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1432
        gen_op_st_rm_T0_A0(s1, ot, d);
B
bellard 已提交
1433
        gen_op_update1_cc();
1434
        set_cc_op(s1, CC_OP_LOGICB + ot);
B
bellard 已提交
1435
        break;
B
bellard 已提交
1436
    case OP_XORL:
B
bellard 已提交
1437
        tcg_gen_xor_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1438
        gen_op_st_rm_T0_A0(s1, ot, d);
B
bellard 已提交
1439
        gen_op_update1_cc();
1440
        set_cc_op(s1, CC_OP_LOGICB + ot);
B
bellard 已提交
1441 1442
        break;
    case OP_CMPL:
1443
        tcg_gen_mov_tl(cpu_cc_src, cpu_T[1]);
1444
        tcg_gen_mov_tl(cpu_cc_srcT, cpu_T[0]);
1445
        tcg_gen_sub_tl(cpu_cc_dst, cpu_T[0], cpu_T[1]);
1446
        set_cc_op(s1, CC_OP_SUBB + ot);
B
bellard 已提交
1447 1448
        break;
    }
1449 1450
}

B
bellard 已提交
1451 1452 1453
/* if d == OR_TMP0, it means memory operand (address in A0) */
static void gen_inc(DisasContext *s1, int ot, int d, int c)
{
1454
    if (d != OR_TMP0) {
B
bellard 已提交
1455
        gen_op_mov_TN_reg(ot, 0, d);
1456 1457 1458
    } else {
        gen_op_ld_v(s1, ot, cpu_T[0], cpu_A0);
    }
1459
    gen_compute_eflags_c(s1, cpu_cc_src);
B
bellard 已提交
1460
    if (c > 0) {
1461
        tcg_gen_addi_tl(cpu_T[0], cpu_T[0], 1);
1462
        set_cc_op(s1, CC_OP_INCB + ot);
B
bellard 已提交
1463
    } else {
1464
        tcg_gen_addi_tl(cpu_T[0], cpu_T[0], -1);
1465
        set_cc_op(s1, CC_OP_DECB + ot);
B
bellard 已提交
1466
    }
1467
    gen_op_st_rm_T0_A0(s1, ot, d);
B
bellard 已提交
1468
    tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
B
bellard 已提交
1469 1470
}

1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
static void gen_shift_flags(DisasContext *s, int ot, TCGv result, TCGv shm1,
                            TCGv count, bool is_right)
{
    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);
}

1516 1517
static void gen_shift_rm_T1(DisasContext *s, int ot, int op1, 
                            int is_right, int is_arith)
B
bellard 已提交
1518
{
1519
    target_ulong mask = (ot == MO_64 ? 0x3f : 0x1f);
1520

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

1528 1529
    tcg_gen_andi_tl(cpu_T[1], cpu_T[1], mask);
    tcg_gen_subi_tl(cpu_tmp0, cpu_T[1], 1);
1530 1531 1532

    if (is_right) {
        if (is_arith) {
B
bellard 已提交
1533
            gen_exts(ot, cpu_T[0]);
1534 1535
            tcg_gen_sar_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
            tcg_gen_sar_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1536
        } else {
B
bellard 已提交
1537
            gen_extu(ot, cpu_T[0]);
1538 1539
            tcg_gen_shr_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
            tcg_gen_shr_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1540 1541
        }
    } else {
1542 1543
        tcg_gen_shl_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
        tcg_gen_shl_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1544 1545 1546
    }

    /* store */
1547
    gen_op_st_rm_T0_A0(s, ot, op1);
1548

1549
    gen_shift_flags(s, ot, cpu_T[0], cpu_tmp0, cpu_T[1], is_right);
1550 1551
}

B
bellard 已提交
1552 1553 1554
static void gen_shift_rm_im(DisasContext *s, int ot, int op1, int op2,
                            int is_right, int is_arith)
{
1555
    int mask = (ot == MO_64 ? 0x3f : 0x1f);
B
bellard 已提交
1556 1557 1558

    /* load */
    if (op1 == OR_TMP0)
1559
        gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
1560 1561 1562 1563 1564 1565 1566 1567
    else
        gen_op_mov_TN_reg(ot, 0, op1);

    op2 &= mask;
    if (op2 != 0) {
        if (is_right) {
            if (is_arith) {
                gen_exts(ot, cpu_T[0]);
B
bellard 已提交
1568
                tcg_gen_sari_tl(cpu_tmp4, cpu_T[0], op2 - 1);
B
bellard 已提交
1569 1570 1571
                tcg_gen_sari_tl(cpu_T[0], cpu_T[0], op2);
            } else {
                gen_extu(ot, cpu_T[0]);
B
bellard 已提交
1572
                tcg_gen_shri_tl(cpu_tmp4, cpu_T[0], op2 - 1);
B
bellard 已提交
1573 1574 1575
                tcg_gen_shri_tl(cpu_T[0], cpu_T[0], op2);
            }
        } else {
B
bellard 已提交
1576
            tcg_gen_shli_tl(cpu_tmp4, cpu_T[0], op2 - 1);
B
bellard 已提交
1577 1578 1579 1580 1581
            tcg_gen_shli_tl(cpu_T[0], cpu_T[0], op2);
        }
    }

    /* store */
1582 1583
    gen_op_st_rm_T0_A0(s, ot, op1);

B
bellard 已提交
1584 1585
    /* update eflags if non zero shift */
    if (op2 != 0) {
B
bellard 已提交
1586
        tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
B
bellard 已提交
1587
        tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
1588
        set_cc_op(s, (is_right ? CC_OP_SARB : CC_OP_SHLB) + ot);
B
bellard 已提交
1589 1590 1591
    }
}

1592 1593 1594 1595 1596 1597 1598 1599
static inline void tcg_gen_lshift(TCGv ret, TCGv arg1, target_long arg2)
{
    if (arg2 >= 0)
        tcg_gen_shli_tl(ret, arg1, arg2);
    else
        tcg_gen_shri_tl(ret, arg1, -arg2);
}

1600
static void gen_rot_rm_T1(DisasContext *s, int ot, int op1, int is_right)
1601
{
1602
    target_ulong mask = (ot == MO_64 ? 0x3f : 0x1f);
1603
    TCGv_i32 t0, t1;
1604 1605

    /* load */
1606
    if (op1 == OR_TMP0) {
1607
        gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
1608
    } else {
1609
        gen_op_mov_TN_reg(ot, 0, op1);
1610
    }
1611

1612
    tcg_gen_andi_tl(cpu_T[1], cpu_T[1], mask);
1613

1614
    switch (ot) {
1615
    case MO_8:
1616 1617 1618 1619
        /* 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;
1620
    case MO_16:
1621 1622 1623 1624 1625
        /* 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
1626
    case MO_32:
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
        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;
1644 1645 1646
    }

    /* store */
1647
    gen_op_st_rm_T0_A0(s, ot, op1);
1648

1649 1650
    /* We'll need the flags computed into CC_SRC.  */
    gen_compute_eflags(s);
1651

1652 1653 1654 1655
    /* 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.  */
1656
    if (is_right) {
1657 1658
        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 已提交
1659
        tcg_gen_andi_tl(cpu_cc_dst, cpu_cc_dst, 1);
1660 1661 1662
    } else {
        tcg_gen_shri_tl(cpu_cc_src2, cpu_T[0], mask);
        tcg_gen_andi_tl(cpu_cc_dst, cpu_T[0], 1);
1663
    }
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
    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);
1683 1684
}

M
malc 已提交
1685 1686 1687
static void gen_rot_rm_im(DisasContext *s, int ot, int op1, int op2,
                          int is_right)
{
1688
    int mask = (ot == MO_64 ? 0x3f : 0x1f);
1689
    int shift;
M
malc 已提交
1690 1691 1692

    /* load */
    if (op1 == OR_TMP0) {
1693
        gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
M
malc 已提交
1694
    } else {
1695
        gen_op_mov_TN_reg(ot, 0, op1);
M
malc 已提交
1696 1697 1698 1699
    }

    op2 &= mask;
    if (op2 != 0) {
1700 1701
        switch (ot) {
#ifdef TARGET_X86_64
1702
        case MO_32:
1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718
            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;
1719
        case MO_8:
1720 1721
            mask = 7;
            goto do_shifts;
1722
        case MO_16:
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733
            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 已提交
1734 1735 1736 1737
        }
    }

    /* store */
1738
    gen_op_st_rm_T0_A0(s, ot, op1);
M
malc 已提交
1739 1740

    if (op2 != 0) {
1741
        /* Compute the flags into CC_SRC.  */
1742
        gen_compute_eflags(s);
1743

1744 1745 1746 1747
        /* 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 已提交
1748
        if (is_right) {
1749 1750
            tcg_gen_shri_tl(cpu_cc_src2, cpu_T[0], mask - 1);
            tcg_gen_shri_tl(cpu_cc_dst, cpu_T[0], mask);
1751
            tcg_gen_andi_tl(cpu_cc_dst, cpu_cc_dst, 1);
1752 1753 1754
        } 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 已提交
1755
        }
1756 1757 1758
        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 已提交
1759 1760 1761
    }
}

1762 1763 1764 1765
/* XXX: add faster immediate = 1 case */
static void gen_rotc_rm_T1(DisasContext *s, int ot, int op1, 
                           int is_right)
{
1766
    gen_compute_eflags(s);
1767
    assert(s->cc_op == CC_OP_EFLAGS);
1768 1769 1770

    /* load */
    if (op1 == OR_TMP0)
1771
        gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
1772 1773 1774
    else
        gen_op_mov_TN_reg(ot, 0, op1);
    
P
pbrook 已提交
1775 1776
    if (is_right) {
        switch (ot) {
1777
        case MO_8:
1778 1779
            gen_helper_rcrb(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
1780
        case MO_16:
1781 1782
            gen_helper_rcrw(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
1783
        case MO_32:
1784 1785
            gen_helper_rcrl(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1786
#ifdef TARGET_X86_64
1787
        case MO_64:
1788 1789
            gen_helper_rcrq(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1790 1791 1792 1793
#endif
        }
    } else {
        switch (ot) {
1794
        case MO_8:
1795 1796
            gen_helper_rclb(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
1797
        case MO_16:
1798 1799
            gen_helper_rclw(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
1800
        case MO_32:
1801 1802
            gen_helper_rcll(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1803
#ifdef TARGET_X86_64
1804
        case MO_64:
1805 1806
            gen_helper_rclq(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1807 1808 1809
#endif
        }
    }
1810
    /* store */
1811
    gen_op_st_rm_T0_A0(s, ot, op1);
1812 1813 1814
}

/* XXX: add faster immediate case */
P
Paolo Bonzini 已提交
1815
static void gen_shiftd_rm_T1(DisasContext *s, int ot, int op1,
1816
                             bool is_right, TCGv count_in)
1817
{
1818
    target_ulong mask = (ot == MO_64 ? 63 : 31);
1819
    TCGv count;
1820 1821

    /* load */
1822
    if (op1 == OR_TMP0) {
1823
        gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
1824
    } else {
1825
        gen_op_mov_TN_reg(ot, 0, op1);
1826
    }
1827

1828 1829
    count = tcg_temp_new();
    tcg_gen_andi_tl(count, count_in, mask);
1830

1831
    switch (ot) {
1832
    case MO_16:
1833 1834 1835
        /* 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.  */
1836
        if (is_right) {
1837 1838 1839
            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);
1840
        } else {
1841
            tcg_gen_deposit_tl(cpu_T[1], cpu_T[0], cpu_T[1], 16, 16);
1842
        }
1843 1844
        /* FALLTHRU */
#ifdef TARGET_X86_64
1845
    case MO_32:
1846 1847
        /* Concatenate the two 32-bit values and use a 64-bit shift.  */
        tcg_gen_subi_tl(cpu_tmp0, count, 1);
1848
        if (is_right) {
1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864
            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);
1865

1866 1867 1868
            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);
1869
        } else {
1870
            tcg_gen_shl_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
1871
            if (ot == MO_16) {
1872 1873 1874 1875 1876 1877 1878 1879 1880
                /* 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);
1881
        }
1882 1883 1884 1885 1886
        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;
1887 1888 1889
    }

    /* store */
1890
    gen_op_st_rm_T0_A0(s, ot, op1);
1891

1892 1893
    gen_shift_flags(s, ot, cpu_T[0], cpu_tmp0, count, is_right);
    tcg_temp_free(count);
1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923
}

static void gen_shift(DisasContext *s1, int op, int ot, int d, int s)
{
    if (s != OR_TMP1)
        gen_op_mov_TN_reg(ot, 1, s);
    switch(op) {
    case OP_ROL:
        gen_rot_rm_T1(s1, ot, d, 0);
        break;
    case OP_ROR:
        gen_rot_rm_T1(s1, ot, d, 1);
        break;
    case OP_SHL:
    case OP_SHL1:
        gen_shift_rm_T1(s1, ot, d, 0, 0);
        break;
    case OP_SHR:
        gen_shift_rm_T1(s1, ot, d, 1, 0);
        break;
    case OP_SAR:
        gen_shift_rm_T1(s1, ot, d, 1, 1);
        break;
    case OP_RCL:
        gen_rotc_rm_T1(s1, ot, d, 0);
        break;
    case OP_RCR:
        gen_rotc_rm_T1(s1, ot, d, 1);
        break;
    }
B
bellard 已提交
1924 1925 1926 1927
}

static void gen_shifti(DisasContext *s1, int op, int ot, int d, int c)
{
B
bellard 已提交
1928
    switch(op) {
M
malc 已提交
1929 1930 1931 1932 1933 1934
    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 已提交
1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950
    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 */
        gen_op_movl_T1_im(c);
        gen_shift(s1, op, ot, d, OR_TMP1);
        break;
    }
B
bellard 已提交
1951 1952
}

1953
static void gen_lea_modrm(CPUX86State *env, DisasContext *s, int modrm)
B
bellard 已提交
1954
{
B
bellard 已提交
1955
    target_long disp;
B
bellard 已提交
1956
    int havesib;
B
bellard 已提交
1957
    int base;
B
bellard 已提交
1958 1959 1960
    int index;
    int scale;
    int mod, rm, code, override, must_add_seg;
1961
    TCGv sum;
B
bellard 已提交
1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972

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

    if (s->aflag) {
        havesib = 0;
        base = rm;
1973
        index = -1;
B
bellard 已提交
1974
        scale = 0;
1975

B
bellard 已提交
1976 1977
        if (base == 4) {
            havesib = 1;
1978
            code = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
1979
            scale = (code >> 6) & 3;
B
bellard 已提交
1980
            index = ((code >> 3) & 7) | REX_X(s);
1981 1982 1983
            if (index == 4) {
                index = -1;  /* no index */
            }
B
bellard 已提交
1984
            base = (code & 7);
B
bellard 已提交
1985
        }
B
bellard 已提交
1986
        base |= REX_B(s);
B
bellard 已提交
1987 1988 1989

        switch (mod) {
        case 0:
B
bellard 已提交
1990
            if ((base & 7) == 5) {
B
bellard 已提交
1991
                base = -1;
1992
                disp = (int32_t)cpu_ldl_code(env, s->pc);
B
bellard 已提交
1993
                s->pc += 4;
B
bellard 已提交
1994 1995 1996
                if (CODE64(s) && !havesib) {
                    disp += s->pc + s->rip_offset;
                }
B
bellard 已提交
1997 1998 1999 2000 2001
            } 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 = (int32_t)cpu_ldl_code(env, s->pc);
B
bellard 已提交
2007 2008 2009
            s->pc += 4;
            break;
        }
2010

2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023
        /* 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 已提交
2024
            }
2025 2026 2027
            if (base >= 0) {
                tcg_gen_add_tl(cpu_A0, sum, cpu_regs[base]);
                sum = cpu_A0;
B
bellard 已提交
2028
            }
2029 2030
        } else if (base >= 0) {
            sum = cpu_regs[base];
B
bellard 已提交
2031
        }
2032 2033 2034 2035
        if (TCGV_IS_UNUSED(sum)) {
            tcg_gen_movi_tl(cpu_A0, disp);
        } else {
            tcg_gen_addi_tl(cpu_A0, sum, disp);
B
bellard 已提交
2036
        }
2037

B
bellard 已提交
2038 2039
        if (must_add_seg) {
            if (override < 0) {
2040
                if (base == R_EBP || base == R_ESP) {
B
bellard 已提交
2041
                    override = R_SS;
2042
                } else {
B
bellard 已提交
2043
                    override = R_DS;
2044
                }
B
bellard 已提交
2045
            }
2046 2047 2048 2049 2050 2051 2052 2053

            tcg_gen_ld_tl(cpu_tmp0, cpu_env,
                          offsetof(CPUX86State, segs[override].base));
            if (CODE64(s)) {
                if (s->aflag != 2) {
                    tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
                }
                tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
2054
                return;
B
bellard 已提交
2055
            }
2056 2057 2058 2059 2060 2061

            tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
        }

        if (s->aflag != 2) {
            tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
B
bellard 已提交
2062 2063 2064 2065 2066
        }
    } else {
        switch (mod) {
        case 0:
            if (rm == 6) {
2067
                disp = cpu_lduw_code(env, s->pc);
B
bellard 已提交
2068 2069 2070 2071 2072 2073 2074 2075 2076
                s->pc += 2;
                gen_op_movl_A0_im(disp);
                rm = 0; /* avoid SS override */
                goto no_rm;
            } else {
                disp = 0;
            }
            break;
        case 1:
2077
            disp = (int8_t)cpu_ldub_code(env, s->pc++);
B
bellard 已提交
2078 2079 2080
            break;
        default:
        case 2:
2081
            disp = cpu_lduw_code(env, s->pc);
B
bellard 已提交
2082 2083 2084 2085 2086
            s->pc += 2;
            break;
        }
        switch(rm) {
        case 0:
B
bellard 已提交
2087 2088
            gen_op_movl_A0_reg(R_EBX);
            gen_op_addl_A0_reg_sN(0, R_ESI);
B
bellard 已提交
2089 2090
            break;
        case 1:
B
bellard 已提交
2091 2092
            gen_op_movl_A0_reg(R_EBX);
            gen_op_addl_A0_reg_sN(0, R_EDI);
B
bellard 已提交
2093 2094
            break;
        case 2:
B
bellard 已提交
2095 2096
            gen_op_movl_A0_reg(R_EBP);
            gen_op_addl_A0_reg_sN(0, R_ESI);
B
bellard 已提交
2097 2098
            break;
        case 3:
B
bellard 已提交
2099 2100
            gen_op_movl_A0_reg(R_EBP);
            gen_op_addl_A0_reg_sN(0, R_EDI);
B
bellard 已提交
2101 2102
            break;
        case 4:
B
bellard 已提交
2103
            gen_op_movl_A0_reg(R_ESI);
B
bellard 已提交
2104 2105
            break;
        case 5:
B
bellard 已提交
2106
            gen_op_movl_A0_reg(R_EDI);
B
bellard 已提交
2107 2108
            break;
        case 6:
B
bellard 已提交
2109
            gen_op_movl_A0_reg(R_EBP);
B
bellard 已提交
2110 2111 2112
            break;
        default:
        case 7:
B
bellard 已提交
2113
            gen_op_movl_A0_reg(R_EBX);
B
bellard 已提交
2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126
            break;
        }
        if (disp != 0)
            gen_op_addl_A0_im(disp);
        gen_op_andl_A0_ffff();
    no_rm:
        if (must_add_seg) {
            if (override < 0) {
                if (rm == 2 || rm == 3 || rm == 6)
                    override = R_SS;
                else
                    override = R_DS;
            }
2127
            gen_op_addl_A0_seg(s, override);
B
bellard 已提交
2128 2129 2130 2131
        }
    }
}

2132
static void gen_nop_modrm(CPUX86State *env, DisasContext *s, int modrm)
B
bellard 已提交
2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143
{
    int mod, rm, base, code;

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

    if (s->aflag) {

        base = rm;
2144

B
bellard 已提交
2145
        if (base == 4) {
2146
            code = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
2147 2148
            base = (code & 7);
        }
2149

B
bellard 已提交
2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181
        switch (mod) {
        case 0:
            if (base == 5) {
                s->pc += 4;
            }
            break;
        case 1:
            s->pc++;
            break;
        default:
        case 2:
            s->pc += 4;
            break;
        }
    } else {
        switch (mod) {
        case 0:
            if (rm == 6) {
                s->pc += 2;
            }
            break;
        case 1:
            s->pc++;
            break;
        default:
        case 2:
            s->pc += 2;
            break;
        }
    }
}

B
bellard 已提交
2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192
/* 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) {
2193 2194
#ifdef TARGET_X86_64
        if (CODE64(s)) {
B
bellard 已提交
2195
            gen_op_addq_A0_seg(override);
2196
        } else
2197 2198
#endif
        {
2199
            gen_op_addl_A0_seg(s, override);
2200
        }
B
bellard 已提交
2201 2202 2203
    }
}

B
balrog 已提交
2204
/* generate modrm memory load or store of 'reg'. TMP0 is used if reg ==
B
bellard 已提交
2205
   OR_TMP0 */
2206 2207
static void gen_ldst_modrm(CPUX86State *env, DisasContext *s, int modrm,
                           int ot, int reg, int is_store)
B
bellard 已提交
2208
{
2209
    int mod, rm;
B
bellard 已提交
2210 2211

    mod = (modrm >> 6) & 3;
B
bellard 已提交
2212
    rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
2213 2214 2215
    if (mod == 3) {
        if (is_store) {
            if (reg != OR_TMP0)
B
bellard 已提交
2216 2217
                gen_op_mov_TN_reg(ot, 0, reg);
            gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
2218
        } else {
B
bellard 已提交
2219
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
2220
            if (reg != OR_TMP0)
B
bellard 已提交
2221
                gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
2222 2223
        }
    } else {
2224
        gen_lea_modrm(env, s, modrm);
B
bellard 已提交
2225 2226
        if (is_store) {
            if (reg != OR_TMP0)
B
bellard 已提交
2227
                gen_op_mov_TN_reg(ot, 0, reg);
2228
            gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
2229
        } else {
2230
            gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
2231
            if (reg != OR_TMP0)
B
bellard 已提交
2232
                gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
2233 2234 2235 2236
        }
    }
}

2237
static inline uint32_t insn_get(CPUX86State *env, DisasContext *s, int ot)
B
bellard 已提交
2238 2239 2240 2241
{
    uint32_t ret;

    switch(ot) {
2242
    case MO_8:
2243
        ret = cpu_ldub_code(env, s->pc);
B
bellard 已提交
2244 2245
        s->pc++;
        break;
2246
    case MO_16:
2247
        ret = cpu_lduw_code(env, s->pc);
B
bellard 已提交
2248 2249 2250
        s->pc += 2;
        break;
    default:
2251
    case MO_32:
2252
        ret = cpu_ldl_code(env, s->pc);
B
bellard 已提交
2253 2254 2255 2256 2257 2258
        s->pc += 4;
        break;
    }
    return ret;
}

B
bellard 已提交
2259 2260
static inline int insn_const_size(unsigned int ot)
{
2261
    if (ot <= MO_32) {
B
bellard 已提交
2262
        return 1 << ot;
2263
    } else {
B
bellard 已提交
2264
        return 4;
2265
    }
B
bellard 已提交
2266 2267
}

2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278
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 已提交
2279
        tcg_gen_goto_tb(tb_num);
2280
        gen_jmp_im(eip);
2281
        tcg_gen_exit_tb((uintptr_t)tb + tb_num);
2282 2283 2284 2285 2286 2287 2288
    } else {
        /* jump to another page: currently not optimized */
        gen_jmp_im(eip);
        gen_eob(s);
    }
}

2289
static inline void gen_jcc(DisasContext *s, int b,
B
bellard 已提交
2290
                           target_ulong val, target_ulong next_eip)
B
bellard 已提交
2291
{
2292
    int l1, l2;
2293

B
bellard 已提交
2294
    if (s->jmp_opt) {
B
bellard 已提交
2295
        l1 = gen_new_label();
2296
        gen_jcc1(s, b, l1);
2297

2298
        gen_goto_tb(s, 0, next_eip);
B
bellard 已提交
2299 2300

        gen_set_label(l1);
2301
        gen_goto_tb(s, 1, val);
J
Jun Koi 已提交
2302
        s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2303
    } else {
B
bellard 已提交
2304 2305
        l1 = gen_new_label();
        l2 = gen_new_label();
2306
        gen_jcc1(s, b, l1);
2307

B
bellard 已提交
2308
        gen_jmp_im(next_eip);
2309 2310
        tcg_gen_br(l2);

B
bellard 已提交
2311 2312 2313
        gen_set_label(l1);
        gen_jmp_im(val);
        gen_set_label(l2);
B
bellard 已提交
2314 2315 2316 2317
        gen_eob(s);
    }
}

2318 2319 2320
static void gen_cmovcc1(CPUX86State *env, DisasContext *s, int ot, int b,
                        int modrm, int reg)
{
2321
    CCPrepare cc;
2322

2323
    gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
2324

2325 2326 2327 2328 2329 2330 2331 2332
    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);
2333 2334
    }

2335 2336 2337 2338 2339 2340 2341 2342 2343 2344
    tcg_gen_movcond_tl(cc.cond, cpu_T[0], cc.reg, cc.reg2,
                       cpu_T[0], cpu_regs[reg]);
    gen_op_mov_reg_T0(ot, reg);

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

2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362
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 已提交
2363 2364
/* move T0 to seg_reg and compute if the CPU state may change. Never
   call this function with seg_reg == R_CS */
B
bellard 已提交
2365
static void gen_movl_seg_T0(DisasContext *s, int seg_reg, target_ulong cur_eip)
B
bellard 已提交
2366
{
2367 2368
    if (s->pe && !s->vm86) {
        /* XXX: optimize by finding processor state dynamically */
2369
        gen_update_cc_op(s);
B
bellard 已提交
2370
        gen_jmp_im(cur_eip);
2371
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
2372
        gen_helper_load_seg(cpu_env, tcg_const_i32(seg_reg), cpu_tmp2_i32);
B
bellard 已提交
2373 2374 2375 2376 2377
        /* 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 已提交
2378
            s->is_jmp = DISAS_TB_JUMP;
2379
    } else {
2380
        gen_op_movl_seg_T0_vm(seg_reg);
B
bellard 已提交
2381
        if (seg_reg == R_SS)
J
Jun Koi 已提交
2382
            s->is_jmp = DISAS_TB_JUMP;
2383
    }
B
bellard 已提交
2384 2385
}

T
ths 已提交
2386 2387 2388 2389 2390
static inline int svm_is_rep(int prefixes)
{
    return ((prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) ? 8 : 0);
}

B
bellard 已提交
2391
static inline void
T
ths 已提交
2392
gen_svm_check_intercept_param(DisasContext *s, target_ulong pc_start,
2393
                              uint32_t type, uint64_t param)
T
ths 已提交
2394
{
B
bellard 已提交
2395 2396 2397
    /* no SVM activated; fast case */
    if (likely(!(s->flags & HF_SVMI_MASK)))
        return;
2398
    gen_update_cc_op(s);
B
bellard 已提交
2399
    gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
2400
    gen_helper_svm_check_intercept_param(cpu_env, tcg_const_i32(type),
P
pbrook 已提交
2401
                                         tcg_const_i64(param));
T
ths 已提交
2402 2403
}

B
bellard 已提交
2404
static inline void
T
ths 已提交
2405 2406
gen_svm_check_intercept(DisasContext *s, target_ulong pc_start, uint64_t type)
{
B
bellard 已提交
2407
    gen_svm_check_intercept_param(s, pc_start, type, 0);
T
ths 已提交
2408 2409
}

2410 2411
static inline void gen_stack_update(DisasContext *s, int addend)
{
B
bellard 已提交
2412 2413
#ifdef TARGET_X86_64
    if (CODE64(s)) {
2414
        gen_op_add_reg_im(2, R_ESP, addend);
B
bellard 已提交
2415 2416
    } else
#endif
2417
    if (s->ss32) {
2418
        gen_op_add_reg_im(1, R_ESP, addend);
2419
    } else {
2420
        gen_op_add_reg_im(0, R_ESP, addend);
2421 2422 2423
    }
}

B
bellard 已提交
2424 2425 2426
/* generate a push. It depends on ss32, addseg and dflag */
static void gen_push_T0(DisasContext *s)
{
B
bellard 已提交
2427 2428
#ifdef TARGET_X86_64
    if (CODE64(s)) {
B
bellard 已提交
2429
        gen_op_movq_A0_reg(R_ESP);
2430
        if (s->dflag) {
B
bellard 已提交
2431
            gen_op_addq_A0_im(-8);
2432
            gen_op_st_v(s, MO_64, cpu_T[0], cpu_A0);
2433
        } else {
B
bellard 已提交
2434
            gen_op_addq_A0_im(-2);
2435
            gen_op_st_v(s, MO_16, cpu_T[0], cpu_A0);
2436
        }
B
bellard 已提交
2437
        gen_op_mov_reg_A0(2, R_ESP);
2438
    } else
B
bellard 已提交
2439 2440
#endif
    {
B
bellard 已提交
2441
        gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2442
        if (!s->dflag)
B
bellard 已提交
2443
            gen_op_addl_A0_im(-2);
B
bellard 已提交
2444
        else
B
bellard 已提交
2445
            gen_op_addl_A0_im(-4);
B
bellard 已提交
2446 2447
        if (s->ss32) {
            if (s->addseg) {
2448
                tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2449
                gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2450 2451 2452
            }
        } else {
            gen_op_andl_A0_ffff();
2453
            tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2454
            gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2455
        }
2456
        gen_op_st_v(s, s->dflag + 1, cpu_T[0], cpu_A0);
B
bellard 已提交
2457
        if (s->ss32 && !s->addseg)
B
bellard 已提交
2458
            gen_op_mov_reg_A0(1, R_ESP);
B
bellard 已提交
2459
        else
B
bellard 已提交
2460
            gen_op_mov_reg_T1(s->ss32 + 1, R_ESP);
B
bellard 已提交
2461 2462 2463
    }
}

2464 2465 2466
/* generate a push. It depends on ss32, addseg and dflag */
/* slower version for T1, only used for call Ev */
static void gen_push_T1(DisasContext *s)
B
bellard 已提交
2467
{
B
bellard 已提交
2468 2469
#ifdef TARGET_X86_64
    if (CODE64(s)) {
B
bellard 已提交
2470
        gen_op_movq_A0_reg(R_ESP);
2471
        if (s->dflag) {
B
bellard 已提交
2472
            gen_op_addq_A0_im(-8);
2473
            gen_op_st_v(s, MO_64, cpu_T[1], cpu_A0);
2474
        } else {
B
bellard 已提交
2475
            gen_op_addq_A0_im(-2);
2476
            gen_op_st_v(s, MO_16, cpu_T[1], cpu_A0);
2477
        }
B
bellard 已提交
2478
        gen_op_mov_reg_A0(2, R_ESP);
2479
    } else
B
bellard 已提交
2480 2481
#endif
    {
B
bellard 已提交
2482
        gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2483
        if (!s->dflag)
B
bellard 已提交
2484
            gen_op_addl_A0_im(-2);
B
bellard 已提交
2485
        else
B
bellard 已提交
2486
            gen_op_addl_A0_im(-4);
B
bellard 已提交
2487 2488
        if (s->ss32) {
            if (s->addseg) {
2489
                gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2490 2491 2492
            }
        } else {
            gen_op_andl_A0_ffff();
2493
            gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2494
        }
2495
        gen_op_st_v(s, s->dflag + 1, cpu_T[1], cpu_A0);
2496

B
bellard 已提交
2497
        if (s->ss32 && !s->addseg)
B
bellard 已提交
2498
            gen_op_mov_reg_A0(1, R_ESP);
B
bellard 已提交
2499 2500
        else
            gen_stack_update(s, (-2) << s->dflag);
B
bellard 已提交
2501 2502 2503
    }
}

2504 2505
/* two step pop is necessary for precise exceptions */
static void gen_pop_T0(DisasContext *s)
B
bellard 已提交
2506
{
B
bellard 已提交
2507 2508
#ifdef TARGET_X86_64
    if (CODE64(s)) {
B
bellard 已提交
2509
        gen_op_movq_A0_reg(R_ESP);
2510
        gen_op_ld_v(s, s->dflag ? MO_64 : MO_16, cpu_T[0], cpu_A0);
2511
    } else
B
bellard 已提交
2512 2513
#endif
    {
B
bellard 已提交
2514
        gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2515 2516
        if (s->ss32) {
            if (s->addseg)
2517
                gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2518 2519
        } else {
            gen_op_andl_A0_ffff();
2520
            gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2521
        }
2522
        gen_op_ld_v(s, s->dflag + 1, cpu_T[0], cpu_A0);
B
bellard 已提交
2523 2524 2525 2526 2527
    }
}

static void gen_pop_update(DisasContext *s)
{
B
bellard 已提交
2528
#ifdef TARGET_X86_64
2529
    if (CODE64(s) && s->dflag) {
B
bellard 已提交
2530 2531 2532 2533 2534 2535
        gen_stack_update(s, 8);
    } else
#endif
    {
        gen_stack_update(s, 2 << s->dflag);
    }
B
bellard 已提交
2536 2537 2538 2539
}

static void gen_stack_A0(DisasContext *s)
{
B
bellard 已提交
2540
    gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2541 2542
    if (!s->ss32)
        gen_op_andl_A0_ffff();
2543
    tcg_gen_mov_tl(cpu_T[1], cpu_A0);
B
bellard 已提交
2544
    if (s->addseg)
2545
        gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2546 2547 2548 2549 2550 2551
}

/* NOTE: wrap around in 16 bit not fully handled */
static void gen_pusha(DisasContext *s)
{
    int i;
B
bellard 已提交
2552
    gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2553 2554 2555
    gen_op_addl_A0_im(-16 <<  s->dflag);
    if (!s->ss32)
        gen_op_andl_A0_ffff();
2556
    tcg_gen_mov_tl(cpu_T[1], cpu_A0);
B
bellard 已提交
2557
    if (s->addseg)
2558
        gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2559
    for(i = 0;i < 8; i++) {
2560
        gen_op_mov_TN_reg(MO_32, 0, 7 - i);
2561
        gen_op_st_v(s, MO_16 + s->dflag, cpu_T[0], cpu_A0);
B
bellard 已提交
2562 2563
        gen_op_addl_A0_im(2 <<  s->dflag);
    }
2564
    gen_op_mov_reg_T1(MO_16 + s->ss32, R_ESP);
B
bellard 已提交
2565 2566 2567 2568 2569 2570
}

/* NOTE: wrap around in 16 bit not fully handled */
static void gen_popa(DisasContext *s)
{
    int i;
B
bellard 已提交
2571
    gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2572 2573
    if (!s->ss32)
        gen_op_andl_A0_ffff();
2574 2575
    tcg_gen_mov_tl(cpu_T[1], cpu_A0);
    tcg_gen_addi_tl(cpu_T[1], cpu_T[1], 16 <<  s->dflag);
B
bellard 已提交
2576
    if (s->addseg)
2577
        gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2578 2579 2580
    for(i = 0;i < 8; i++) {
        /* ESP is not reloaded */
        if (i != 3) {
2581
            gen_op_ld_v(s, MO_16 + s->dflag, cpu_T[0], cpu_A0);
2582
            gen_op_mov_reg_T0(MO_16 + s->dflag, 7 - i);
B
bellard 已提交
2583 2584 2585
        }
        gen_op_addl_A0_im(2 <<  s->dflag);
    }
2586
    gen_op_mov_reg_T1(MO_16 + s->ss32, R_ESP);
B
bellard 已提交
2587 2588 2589 2590
}

static void gen_enter(DisasContext *s, int esp_addend, int level)
{
B
bellard 已提交
2591
    int ot, opsize;
B
bellard 已提交
2592 2593

    level &= 0x1f;
2594 2595
#ifdef TARGET_X86_64
    if (CODE64(s)) {
2596
        ot = s->dflag ? MO_64 : MO_16;
2597
        opsize = 1 << ot;
2598

B
bellard 已提交
2599
        gen_op_movl_A0_reg(R_ESP);
2600
        gen_op_addq_A0_im(-opsize);
2601
        tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2602 2603

        /* push bp */
2604
        gen_op_mov_TN_reg(MO_32, 0, R_EBP);
2605
        gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
2606
        if (level) {
B
bellard 已提交
2607
            /* XXX: must save state */
2608
            gen_helper_enter64_level(cpu_env, tcg_const_i32(level),
2609
                                     tcg_const_i32((ot == MO_64)),
P
pbrook 已提交
2610
                                     cpu_T[1]);
2611
        }
B
bellard 已提交
2612
        gen_op_mov_reg_T1(ot, R_EBP);
2613
        tcg_gen_addi_tl(cpu_T[1], cpu_T[1], -esp_addend + (-opsize * level));
2614
        gen_op_mov_reg_T1(MO_64, R_ESP);
2615
    } else
2616 2617
#endif
    {
2618
        ot = s->dflag + MO_16;
2619
        opsize = 2 << s->dflag;
2620

B
bellard 已提交
2621
        gen_op_movl_A0_reg(R_ESP);
2622 2623 2624
        gen_op_addl_A0_im(-opsize);
        if (!s->ss32)
            gen_op_andl_A0_ffff();
2625
        tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2626
        if (s->addseg)
2627
            gen_op_addl_A0_seg(s, R_SS);
2628
        /* push bp */
2629
        gen_op_mov_TN_reg(MO_32, 0, R_EBP);
2630
        gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
2631
        if (level) {
B
bellard 已提交
2632
            /* XXX: must save state */
2633
            gen_helper_enter_level(cpu_env, tcg_const_i32(level),
P
pbrook 已提交
2634 2635
                                   tcg_const_i32(s->dflag),
                                   cpu_T[1]);
2636
        }
B
bellard 已提交
2637
        gen_op_mov_reg_T1(ot, R_EBP);
2638
        tcg_gen_addi_tl(cpu_T[1], cpu_T[1], -esp_addend + (-opsize * level));
2639
        gen_op_mov_reg_T1(MO_16 + s->ss32, R_ESP);
B
bellard 已提交
2640 2641 2642
    }
}

B
bellard 已提交
2643
static void gen_exception(DisasContext *s, int trapno, target_ulong cur_eip)
B
bellard 已提交
2644
{
2645
    gen_update_cc_op(s);
B
bellard 已提交
2646
    gen_jmp_im(cur_eip);
B
Blue Swirl 已提交
2647
    gen_helper_raise_exception(cpu_env, tcg_const_i32(trapno));
J
Jun Koi 已提交
2648
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2649 2650 2651
}

/* an interrupt is different from an exception because of the
B
blueswir1 已提交
2652
   privilege checks */
2653
static void gen_interrupt(DisasContext *s, int intno,
B
bellard 已提交
2654
                          target_ulong cur_eip, target_ulong next_eip)
B
bellard 已提交
2655
{
2656
    gen_update_cc_op(s);
B
bellard 已提交
2657
    gen_jmp_im(cur_eip);
B
Blue Swirl 已提交
2658
    gen_helper_raise_interrupt(cpu_env, tcg_const_i32(intno),
P
pbrook 已提交
2659
                               tcg_const_i32(next_eip - cur_eip));
J
Jun Koi 已提交
2660
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2661 2662
}

B
bellard 已提交
2663
static void gen_debug(DisasContext *s, target_ulong cur_eip)
B
bellard 已提交
2664
{
2665
    gen_update_cc_op(s);
B
bellard 已提交
2666
    gen_jmp_im(cur_eip);
B
Blue Swirl 已提交
2667
    gen_helper_debug(cpu_env);
J
Jun Koi 已提交
2668
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2669 2670 2671 2672 2673 2674
}

/* generate a generic end of block. Trace exception is also generated
   if needed */
static void gen_eob(DisasContext *s)
{
2675
    gen_update_cc_op(s);
2676
    if (s->tb->flags & HF_INHIBIT_IRQ_MASK) {
2677
        gen_helper_reset_inhibit_irq(cpu_env);
2678
    }
J
Jan Kiszka 已提交
2679
    if (s->tb->flags & HF_RF_MASK) {
2680
        gen_helper_reset_rf(cpu_env);
J
Jan Kiszka 已提交
2681
    }
2682
    if (s->singlestep_enabled) {
B
Blue Swirl 已提交
2683
        gen_helper_debug(cpu_env);
2684
    } else if (s->tf) {
B
Blue Swirl 已提交
2685
        gen_helper_single_step(cpu_env);
B
bellard 已提交
2686
    } else {
B
bellard 已提交
2687
        tcg_gen_exit_tb(0);
B
bellard 已提交
2688
    }
J
Jun Koi 已提交
2689
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2690 2691 2692 2693
}

/* generate a jump to eip. No segment change must happen before as a
   direct call to the next block may occur */
B
bellard 已提交
2694
static void gen_jmp_tb(DisasContext *s, target_ulong eip, int tb_num)
B
bellard 已提交
2695
{
2696 2697
    gen_update_cc_op(s);
    set_cc_op(s, CC_OP_DYNAMIC);
B
bellard 已提交
2698
    if (s->jmp_opt) {
2699
        gen_goto_tb(s, tb_num, eip);
J
Jun Koi 已提交
2700
        s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2701
    } else {
B
bellard 已提交
2702
        gen_jmp_im(eip);
B
bellard 已提交
2703 2704 2705 2706
        gen_eob(s);
    }
}

B
bellard 已提交
2707 2708 2709 2710 2711
static void gen_jmp(DisasContext *s, target_ulong eip)
{
    gen_jmp_tb(s, eip, 0);
}

2712
static inline void gen_ldq_env_A0(DisasContext *s, int offset)
B
bellard 已提交
2713
{
2714
    tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
2715
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset);
B
bellard 已提交
2716
}
B
bellard 已提交
2717

2718
static inline void gen_stq_env_A0(DisasContext *s, int offset)
B
bellard 已提交
2719
{
2720
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset);
2721
    tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
B
bellard 已提交
2722
}
B
bellard 已提交
2723

2724
static inline void gen_ldo_env_A0(DisasContext *s, int offset)
B
bellard 已提交
2725
{
2726
    int mem_index = s->mem_index;
2727
    tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0, mem_index, MO_LEQ);
2728
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(0)));
B
bellard 已提交
2729
    tcg_gen_addi_tl(cpu_tmp0, cpu_A0, 8);
2730
    tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_tmp0, mem_index, MO_LEQ);
2731
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(1)));
B
bellard 已提交
2732
}
B
bellard 已提交
2733

2734
static inline void gen_sto_env_A0(DisasContext *s, int offset)
B
bellard 已提交
2735
{
2736
    int mem_index = s->mem_index;
2737
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(0)));
2738
    tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0, mem_index, MO_LEQ);
B
bellard 已提交
2739
    tcg_gen_addi_tl(cpu_tmp0, cpu_A0, 8);
2740
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(1)));
2741
    tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_tmp0, mem_index, MO_LEQ);
B
bellard 已提交
2742
}
B
bellard 已提交
2743

B
bellard 已提交
2744 2745
static inline void gen_op_movo(int d_offset, int s_offset)
{
2746 2747 2748 2749
    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 已提交
2750 2751 2752 2753
}

static inline void gen_op_movq(int d_offset, int s_offset)
{
2754 2755
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
B
bellard 已提交
2756 2757 2758 2759
}

static inline void gen_op_movl(int d_offset, int s_offset)
{
2760 2761
    tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env, s_offset);
    tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, d_offset);
B
bellard 已提交
2762 2763 2764 2765
}

static inline void gen_op_movq_env_0(int d_offset)
{
2766 2767
    tcg_gen_movi_i64(cpu_tmp1_i64, 0);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
B
bellard 已提交
2768
}
B
bellard 已提交
2769

B
Blue Swirl 已提交
2770 2771 2772 2773 2774 2775 2776
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 已提交
2777
typedef void (*SSEFunc_0_ppi)(TCGv_ptr reg_a, TCGv_ptr reg_b, TCGv_i32 val);
B
Blue Swirl 已提交
2778 2779
typedef void (*SSEFunc_0_eppt)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
                               TCGv val);
B
Blue Swirl 已提交
2780

B
bellard 已提交
2781 2782
#define SSE_SPECIAL ((void *)1)
#define SSE_DUMMY ((void *)2)
B
bellard 已提交
2783

P
pbrook 已提交
2784 2785 2786
#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 已提交
2787

B
Blue Swirl 已提交
2788
static const SSEFunc_0_epp sse_op_table1[256][4] = {
A
aurel32 已提交
2789 2790 2791
    /* 3DNow! extensions */
    [0x0e] = { SSE_DUMMY }, /* femms */
    [0x0f] = { SSE_DUMMY }, /* pf... */
B
bellard 已提交
2792 2793 2794
    /* 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 已提交
2795
    [0x12] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movlps, movlpd, movsldup, movddup */
B
bellard 已提交
2796
    [0x13] = { SSE_SPECIAL, SSE_SPECIAL },  /* movlps, movlpd */
P
pbrook 已提交
2797 2798
    [0x14] = { gen_helper_punpckldq_xmm, gen_helper_punpcklqdq_xmm },
    [0x15] = { gen_helper_punpckhdq_xmm, gen_helper_punpckhqdq_xmm },
B
bellard 已提交
2799 2800 2801 2802 2803 2804
    [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 */
2805
    [0x2b] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movntps, movntpd, movntss, movntsd */
B
bellard 已提交
2806 2807
    [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 已提交
2808 2809
    [0x2e] = { gen_helper_ucomiss, gen_helper_ucomisd },
    [0x2f] = { gen_helper_comiss, gen_helper_comisd },
B
bellard 已提交
2810 2811
    [0x50] = { SSE_SPECIAL, SSE_SPECIAL }, /* movmskps, movmskpd */
    [0x51] = SSE_FOP(sqrt),
P
pbrook 已提交
2812 2813 2814 2815 2816 2817
    [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 已提交
2818 2819
    [0x58] = SSE_FOP(add),
    [0x59] = SSE_FOP(mul),
P
pbrook 已提交
2820 2821 2822
    [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 已提交
2823 2824 2825 2826 2827 2828
    [0x5c] = SSE_FOP(sub),
    [0x5d] = SSE_FOP(min),
    [0x5e] = SSE_FOP(div),
    [0x5f] = SSE_FOP(max),

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

R
Richard Henderson 已提交
2832 2833 2834
    /* 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 已提交
2835

B
bellard 已提交
2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848
    /* 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 已提交
2849 2850
    [0x6c] = { NULL, gen_helper_punpcklqdq_xmm },
    [0x6d] = { NULL, gen_helper_punpckhqdq_xmm },
B
bellard 已提交
2851 2852
    [0x6e] = { SSE_SPECIAL, SSE_SPECIAL }, /* movd mm, ea */
    [0x6f] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movq, movdqa, , movqdu */
B
Blue Swirl 已提交
2853 2854 2855 2856
    [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 已提交
2857 2858 2859 2860 2861 2862
    [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 已提交
2863
    [0x77] = { SSE_DUMMY }, /* emms */
2864 2865
    [0x78] = { NULL, SSE_SPECIAL, NULL, SSE_SPECIAL }, /* extrq_i, insertq_i */
    [0x79] = { NULL, gen_helper_extrq_r, NULL, gen_helper_insertq_r },
P
pbrook 已提交
2866 2867
    [0x7c] = { NULL, gen_helper_haddpd, NULL, gen_helper_haddps },
    [0x7d] = { NULL, gen_helper_hsubpd, NULL, gen_helper_hsubps },
B
bellard 已提交
2868 2869 2870 2871
    [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 已提交
2872
    [0xd0] = { NULL, gen_helper_addsubpd, NULL, gen_helper_addsubps },
B
bellard 已提交
2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893
    [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 已提交
2894
    [0xe6] = { NULL, gen_helper_cvttpd2dq, gen_helper_cvtdq2pd, gen_helper_cvtpd2dq },
B
bellard 已提交
2895 2896 2897 2898 2899 2900 2901 2902 2903
    [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 已提交
2904
    [0xf0] = { NULL, NULL, NULL, SSE_SPECIAL }, /* lddqu */
B
bellard 已提交
2905 2906 2907 2908 2909 2910
    [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 已提交
2911 2912
    [0xf7] = { (SSEFunc_0_epp)gen_helper_maskmov_mmx,
               (SSEFunc_0_epp)gen_helper_maskmov_xmm }, /* XXX: casts */
B
bellard 已提交
2913 2914 2915 2916 2917 2918 2919 2920 2921
    [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 已提交
2922
static const SSEFunc_0_epp sse_op_table2[3 * 8][2] = {
B
bellard 已提交
2923 2924 2925 2926 2927 2928 2929
    [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 已提交
2930
    [16 + 3] = { NULL, gen_helper_psrldq_xmm },
B
bellard 已提交
2931
    [16 + 6] = MMX_OP2(psllq),
P
pbrook 已提交
2932
    [16 + 7] = { NULL, gen_helper_pslldq_xmm },
B
bellard 已提交
2933 2934
};

B
Blue Swirl 已提交
2935
static const SSEFunc_0_epi sse_op_table3ai[] = {
P
pbrook 已提交
2936
    gen_helper_cvtsi2ss,
2937
    gen_helper_cvtsi2sd
B
Blue Swirl 已提交
2938
};
P
pbrook 已提交
2939

2940
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
2941
static const SSEFunc_0_epl sse_op_table3aq[] = {
2942 2943 2944 2945 2946
    gen_helper_cvtsq2ss,
    gen_helper_cvtsq2sd
};
#endif

B
Blue Swirl 已提交
2947
static const SSEFunc_i_ep sse_op_table3bi[] = {
P
pbrook 已提交
2948 2949
    gen_helper_cvttss2si,
    gen_helper_cvtss2si,
2950
    gen_helper_cvttsd2si,
2951
    gen_helper_cvtsd2si
B
bellard 已提交
2952
};
2953

2954
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
2955
static const SSEFunc_l_ep sse_op_table3bq[] = {
2956 2957
    gen_helper_cvttss2sq,
    gen_helper_cvtss2sq,
2958
    gen_helper_cvttsd2sq,
2959 2960 2961 2962
    gen_helper_cvtsd2sq
};
#endif

B
Blue Swirl 已提交
2963
static const SSEFunc_0_epp sse_op_table4[8][4] = {
B
bellard 已提交
2964 2965 2966 2967 2968 2969 2970 2971 2972
    SSE_FOP(cmpeq),
    SSE_FOP(cmplt),
    SSE_FOP(cmple),
    SSE_FOP(cmpunord),
    SSE_FOP(cmpneq),
    SSE_FOP(cmpnlt),
    SSE_FOP(cmpnle),
    SSE_FOP(cmpord),
};
2973

B
Blue Swirl 已提交
2974
static const SSEFunc_0_epp sse_op_table5[256] = {
P
pbrook 已提交
2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998
    [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 已提交
2999 3000
};

B
Blue Swirl 已提交
3001 3002
struct SSEOpHelper_epp {
    SSEFunc_0_epp op[2];
B
Blue Swirl 已提交
3003 3004 3005
    uint32_t ext_mask;
};

B
Blue Swirl 已提交
3006 3007
struct SSEOpHelper_eppi {
    SSEFunc_0_eppi op[2];
B
Blue Swirl 已提交
3008
    uint32_t ext_mask;
B
balrog 已提交
3009
};
B
Blue Swirl 已提交
3010

B
balrog 已提交
3011
#define SSSE3_OP(x) { MMX_OP2(x), CPUID_EXT_SSSE3 }
P
pbrook 已提交
3012 3013
#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 已提交
3014
#define SSE41_SPECIAL { { NULL, SSE_SPECIAL }, CPUID_EXT_SSE41 }
3015 3016
#define PCLMULQDQ_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, \
        CPUID_EXT_PCLMULQDQ }
3017
#define AESNI_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_AES }
B
Blue Swirl 已提交
3018

B
Blue Swirl 已提交
3019
static const struct SSEOpHelper_epp sse_op_table6[256] = {
B
balrog 已提交
3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065
    [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),
3066 3067 3068 3069 3070
    [0xdb] = AESNI_OP(aesimc),
    [0xdc] = AESNI_OP(aesenc),
    [0xdd] = AESNI_OP(aesenclast),
    [0xde] = AESNI_OP(aesdec),
    [0xdf] = AESNI_OP(aesdeclast),
B
balrog 已提交
3071 3072
};

B
Blue Swirl 已提交
3073
static const struct SSEOpHelper_eppi sse_op_table7[256] = {
B
balrog 已提交
3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091
    [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),
3092
    [0x44] = PCLMULQDQ_OP(pclmulqdq),
B
balrog 已提交
3093 3094 3095 3096
    [0x60] = SSE42_OP(pcmpestrm),
    [0x61] = SSE42_OP(pcmpestri),
    [0x62] = SSE42_OP(pcmpistrm),
    [0x63] = SSE42_OP(pcmpistri),
3097
    [0xdf] = AESNI_OP(aeskeygenassist),
B
balrog 已提交
3098 3099
};

3100 3101
static void gen_sse(CPUX86State *env, DisasContext *s, int b,
                    target_ulong pc_start, int rex_r)
B
bellard 已提交
3102 3103
{
    int b1, op1_offset, op2_offset, is_xmm, val, ot;
3104
    int modrm, mod, rm, reg;
B
Blue Swirl 已提交
3105 3106
    SSEFunc_0_epp sse_fn_epp;
    SSEFunc_0_eppi sse_fn_eppi;
B
Blue Swirl 已提交
3107
    SSEFunc_0_ppi sse_fn_ppi;
B
Blue Swirl 已提交
3108
    SSEFunc_0_eppt sse_fn_eppt;
B
bellard 已提交
3109 3110

    b &= 0xff;
3111
    if (s->prefix & PREFIX_DATA)
B
bellard 已提交
3112
        b1 = 1;
3113
    else if (s->prefix & PREFIX_REPZ)
B
bellard 已提交
3114
        b1 = 2;
3115
    else if (s->prefix & PREFIX_REPNZ)
B
bellard 已提交
3116 3117 3118
        b1 = 3;
    else
        b1 = 0;
B
Blue Swirl 已提交
3119 3120
    sse_fn_epp = sse_op_table1[b][b1];
    if (!sse_fn_epp) {
B
bellard 已提交
3121
        goto illegal_op;
B
Blue Swirl 已提交
3122
    }
A
aurel32 已提交
3123
    if ((b <= 0x5f && b >= 0x10) || b == 0xc6 || b == 0xc2) {
B
bellard 已提交
3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143
        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 已提交
3144 3145
        if ((b != 0x38 && b != 0x3a) || (s->prefix & PREFIX_DATA))
            goto illegal_op;
3146 3147 3148 3149
    if (b == 0x0e) {
        if (!(s->cpuid_ext2_features & CPUID_EXT2_3DNOW))
            goto illegal_op;
        /* femms */
B
Blue Swirl 已提交
3150
        gen_helper_emms(cpu_env);
3151 3152 3153 3154
        return;
    }
    if (b == 0x77) {
        /* emms */
B
Blue Swirl 已提交
3155
        gen_helper_emms(cpu_env);
B
bellard 已提交
3156 3157 3158 3159 3160
        return;
    }
    /* prepare MMX state (XXX: optimize by storing fptt and fptags in
       the static cpu state) */
    if (!is_xmm) {
B
Blue Swirl 已提交
3161
        gen_helper_enter_mmx(cpu_env);
B
bellard 已提交
3162 3163
    }

3164
    modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
3165 3166 3167 3168
    reg = ((modrm >> 3) & 7);
    if (is_xmm)
        reg |= rex_r;
    mod = (modrm >> 6) & 3;
B
Blue Swirl 已提交
3169
    if (sse_fn_epp == SSE_SPECIAL) {
B
bellard 已提交
3170 3171 3172
        b |= (b1 << 8);
        switch(b) {
        case 0x0e7: /* movntq */
3173
            if (mod == 3)
B
bellard 已提交
3174
                goto illegal_op;
3175
            gen_lea_modrm(env, s, modrm);
3176
            gen_stq_env_A0(s, offsetof(CPUX86State, fpregs[reg].mmx));
B
bellard 已提交
3177 3178 3179 3180
            break;
        case 0x1e7: /* movntdq */
        case 0x02b: /* movntps */
        case 0x12b: /* movntps */
3181 3182
            if (mod == 3)
                goto illegal_op;
3183
            gen_lea_modrm(env, s, modrm);
3184
            gen_sto_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
3185
            break;
B
bellard 已提交
3186 3187
        case 0x3f0: /* lddqu */
            if (mod == 3)
B
bellard 已提交
3188
                goto illegal_op;
3189
            gen_lea_modrm(env, s, modrm);
3190
            gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
B
bellard 已提交
3191
            break;
3192 3193 3194 3195
        case 0x22b: /* movntss */
        case 0x32b: /* movntsd */
            if (mod == 3)
                goto illegal_op;
3196
            gen_lea_modrm(env, s, modrm);
3197
            if (b1 & 1) {
3198
                gen_stq_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
3199 3200 3201
            } else {
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
                    xmm_regs[reg].XMM_L(0)));
3202
                gen_op_st_v(s, MO_32, cpu_T[0], cpu_A0);
3203 3204
            }
            break;
B
bellard 已提交
3205
        case 0x6e: /* movd mm, ea */
B
bellard 已提交
3206 3207
#ifdef TARGET_X86_64
            if (s->dflag == 2) {
3208
                gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 0);
B
bellard 已提交
3209
                tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,fpregs[reg].mmx));
3210
            } else
B
bellard 已提交
3211 3212
#endif
            {
3213
                gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 0);
B
bellard 已提交
3214 3215
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                                 offsetof(CPUX86State,fpregs[reg].mmx));
P
pbrook 已提交
3216 3217
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
                gen_helper_movl_mm_T0_mmx(cpu_ptr0, cpu_tmp2_i32);
B
bellard 已提交
3218
            }
B
bellard 已提交
3219 3220
            break;
        case 0x16e: /* movd xmm, ea */
B
bellard 已提交
3221 3222
#ifdef TARGET_X86_64
            if (s->dflag == 2) {
3223
                gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 0);
B
bellard 已提交
3224 3225
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                                 offsetof(CPUX86State,xmm_regs[reg]));
P
pbrook 已提交
3226
                gen_helper_movq_mm_T0_xmm(cpu_ptr0, cpu_T[0]);
3227
            } else
B
bellard 已提交
3228 3229
#endif
            {
3230
                gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 0);
B
bellard 已提交
3231 3232
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                                 offsetof(CPUX86State,xmm_regs[reg]));
3233
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
3234
                gen_helper_movl_mm_T0_xmm(cpu_ptr0, cpu_tmp2_i32);
B
bellard 已提交
3235
            }
B
bellard 已提交
3236 3237 3238
            break;
        case 0x6f: /* movq mm, ea */
            if (mod != 3) {
3239
                gen_lea_modrm(env, s, modrm);
3240
                gen_ldq_env_A0(s, offsetof(CPUX86State, fpregs[reg].mmx));
B
bellard 已提交
3241 3242
            } else {
                rm = (modrm & 7);
3243
                tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env,
B
bellard 已提交
3244
                               offsetof(CPUX86State,fpregs[rm].mmx));
3245
                tcg_gen_st_i64(cpu_tmp1_i64, cpu_env,
B
bellard 已提交
3246
                               offsetof(CPUX86State,fpregs[reg].mmx));
B
bellard 已提交
3247 3248 3249 3250 3251 3252 3253 3254 3255
            }
            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) {
3256
                gen_lea_modrm(env, s, modrm);
3257
                gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
B
bellard 已提交
3258 3259 3260 3261 3262 3263 3264 3265
            } 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) {
3266
                gen_lea_modrm(env, s, modrm);
3267
                gen_op_ld_v(s, MO_32, cpu_T[0], cpu_A0);
B
bellard 已提交
3268
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
3269
                tcg_gen_movi_tl(cpu_T[0], 0);
B
bellard 已提交
3270 3271 3272
                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 已提交
3273 3274 3275 3276 3277 3278 3279 3280
            } 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) {
3281
                gen_lea_modrm(env, s, modrm);
3282 3283
                gen_ldq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(0)));
3284
                tcg_gen_movi_tl(cpu_T[0], 0);
B
bellard 已提交
3285 3286
                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 已提交
3287 3288 3289 3290 3291 3292 3293 3294 3295
            } 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) {
3296
                gen_lea_modrm(env, s, modrm);
3297 3298
                gen_ldq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3299 3300 3301 3302 3303 3304 3305
            } 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 已提交
3306 3307
        case 0x212: /* movsldup */
            if (mod != 3) {
3308
                gen_lea_modrm(env, s, modrm);
3309
                gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
B
bellard 已提交
3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323
            } 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) {
3324
                gen_lea_modrm(env, s, modrm);
3325 3326
                gen_ldq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3327 3328 3329 3330 3331 3332
            } 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 已提交
3333
                        offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3334
            break;
B
bellard 已提交
3335 3336 3337
        case 0x016: /* movhps */
        case 0x116: /* movhpd */
            if (mod != 3) {
3338
                gen_lea_modrm(env, s, modrm);
3339 3340
                gen_ldq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(1)));
B
bellard 已提交
3341 3342 3343 3344 3345 3346 3347 3348 3349
            } 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) {
3350
                gen_lea_modrm(env, s, modrm);
3351
                gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
B
bellard 已提交
3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363
            } 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;
3364 3365 3366 3367 3368 3369 3370
        case 0x178:
        case 0x378:
            {
                int bit_index, field_length;

                if (b1 == 1 && reg != 0)
                    goto illegal_op;
3371 3372
                field_length = cpu_ldub_code(env, s->pc++) & 0x3F;
                bit_index = cpu_ldub_code(env, s->pc++) & 0x3F;
3373 3374 3375
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
                    offsetof(CPUX86State,xmm_regs[reg]));
                if (b1 == 1)
B
Blue Swirl 已提交
3376 3377 3378
                    gen_helper_extrq_i(cpu_env, cpu_ptr0,
                                       tcg_const_i32(bit_index),
                                       tcg_const_i32(field_length));
3379
                else
B
Blue Swirl 已提交
3380 3381 3382
                    gen_helper_insertq_i(cpu_env, cpu_ptr0,
                                         tcg_const_i32(bit_index),
                                         tcg_const_i32(field_length));
3383 3384
            }
            break;
B
bellard 已提交
3385
        case 0x7e: /* movd ea, mm */
B
bellard 已提交
3386 3387
#ifdef TARGET_X86_64
            if (s->dflag == 2) {
B
bellard 已提交
3388 3389
                tcg_gen_ld_i64(cpu_T[0], cpu_env, 
                               offsetof(CPUX86State,fpregs[reg].mmx));
3390
                gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 1);
3391
            } else
B
bellard 已提交
3392 3393
#endif
            {
B
bellard 已提交
3394 3395
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, 
                                 offsetof(CPUX86State,fpregs[reg].mmx.MMX_L(0)));
3396
                gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 1);
B
bellard 已提交
3397
            }
B
bellard 已提交
3398 3399
            break;
        case 0x17e: /* movd ea, xmm */
B
bellard 已提交
3400 3401
#ifdef TARGET_X86_64
            if (s->dflag == 2) {
B
bellard 已提交
3402 3403
                tcg_gen_ld_i64(cpu_T[0], cpu_env, 
                               offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
3404
                gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 1);
3405
            } else
B
bellard 已提交
3406 3407
#endif
            {
B
bellard 已提交
3408 3409
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, 
                                 offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
3410
                gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 1);
B
bellard 已提交
3411
            }
B
bellard 已提交
3412 3413 3414
            break;
        case 0x27e: /* movq xmm, ea */
            if (mod != 3) {
3415
                gen_lea_modrm(env, s, modrm);
3416 3417
                gen_ldq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3418 3419 3420 3421 3422 3423 3424 3425 3426
            } 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) {
3427
                gen_lea_modrm(env, s, modrm);
3428
                gen_stq_env_A0(s, offsetof(CPUX86State, fpregs[reg].mmx));
B
bellard 已提交
3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441
            } 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) {
3442
                gen_lea_modrm(env, s, modrm);
3443
                gen_sto_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
B
bellard 已提交
3444 3445 3446 3447 3448 3449 3450 3451
            } 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) {
3452
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
3453
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
3454
                gen_op_st_v(s, MO_32, cpu_T[0], cpu_A0);
B
bellard 已提交
3455 3456 3457 3458 3459 3460 3461 3462
            } 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) {
3463
                gen_lea_modrm(env, s, modrm);
3464 3465
                gen_stq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3466 3467 3468 3469 3470 3471 3472 3473 3474
            } 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) {
3475
                gen_lea_modrm(env, s, modrm);
3476 3477
                gen_stq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3478 3479 3480 3481 3482 3483 3484
            } else {
                goto illegal_op;
            }
            break;
        case 0x017: /* movhps */
        case 0x117: /* movhpd */
            if (mod != 3) {
3485
                gen_lea_modrm(env, s, modrm);
3486 3487
                gen_stq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(1)));
B
bellard 已提交
3488 3489 3490 3491 3492 3493 3494 3495 3496 3497
            } else {
                goto illegal_op;
            }
            break;
        case 0x71: /* shift mm, im */
        case 0x72:
        case 0x73:
        case 0x171: /* shift xmm, im */
        case 0x172:
        case 0x173:
3498 3499 3500
            if (b1 >= 2) {
	        goto illegal_op;
            }
3501
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
3502 3503
            if (is_xmm) {
                gen_op_movl_T0_im(val);
B
bellard 已提交
3504
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(0)));
3505
                tcg_gen_movi_tl(cpu_T[0], 0);
B
bellard 已提交
3506
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(1)));
B
bellard 已提交
3507 3508 3509
                op1_offset = offsetof(CPUX86State,xmm_t0);
            } else {
                gen_op_movl_T0_im(val);
B
bellard 已提交
3510
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,mmx_t0.MMX_L(0)));
3511
                tcg_gen_movi_tl(cpu_T[0], 0);
B
bellard 已提交
3512
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,mmx_t0.MMX_L(1)));
B
bellard 已提交
3513 3514
                op1_offset = offsetof(CPUX86State,mmx_t0);
            }
B
Blue Swirl 已提交
3515 3516 3517
            sse_fn_epp = sse_op_table2[((b - 1) & 3) * 8 +
                                       (((modrm >> 3)) & 7)][b1];
            if (!sse_fn_epp) {
B
bellard 已提交
3518
                goto illegal_op;
B
Blue Swirl 已提交
3519
            }
B
bellard 已提交
3520 3521 3522 3523 3524 3525 3526
            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 已提交
3527 3528
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op2_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op1_offset);
B
Blue Swirl 已提交
3529
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3530 3531 3532
            break;
        case 0x050: /* movmskps */
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
3533 3534
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                             offsetof(CPUX86State,xmm_regs[rm]));
B
Blue Swirl 已提交
3535
            gen_helper_movmskps(cpu_tmp2_i32, cpu_env, cpu_ptr0);
3536
            tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp2_i32);
B
bellard 已提交
3537 3538 3539
            break;
        case 0x150: /* movmskpd */
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
3540 3541
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                             offsetof(CPUX86State,xmm_regs[rm]));
B
Blue Swirl 已提交
3542
            gen_helper_movmskpd(cpu_tmp2_i32, cpu_env, cpu_ptr0);
3543
            tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp2_i32);
B
bellard 已提交
3544 3545 3546
            break;
        case 0x02a: /* cvtpi2ps */
        case 0x12a: /* cvtpi2pd */
B
Blue Swirl 已提交
3547
            gen_helper_enter_mmx(cpu_env);
B
bellard 已提交
3548
            if (mod != 3) {
3549
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
3550
                op2_offset = offsetof(CPUX86State,mmx_t0);
3551
                gen_ldq_env_A0(s, op2_offset);
B
bellard 已提交
3552 3553 3554 3555 3556
            } else {
                rm = (modrm & 7);
                op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
            }
            op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
B
bellard 已提交
3557 3558
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
bellard 已提交
3559 3560
            switch(b >> 8) {
            case 0x0:
B
Blue Swirl 已提交
3561
                gen_helper_cvtpi2ps(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3562 3563 3564
                break;
            default:
            case 0x1:
B
Blue Swirl 已提交
3565
                gen_helper_cvtpi2pd(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3566 3567 3568 3569 3570
                break;
            }
            break;
        case 0x22a: /* cvtsi2ss */
        case 0x32a: /* cvtsi2sd */
3571
            ot = (s->dflag == 2) ? MO_64 : MO_32;
3572
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
bellard 已提交
3573
            op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
B
bellard 已提交
3574
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
3575
            if (ot == MO_32) {
B
Blue Swirl 已提交
3576
                SSEFunc_0_epi sse_fn_epi = sse_op_table3ai[(b >> 8) & 1];
B
bellard 已提交
3577
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
3578
                sse_fn_epi(cpu_env, cpu_ptr0, cpu_tmp2_i32);
B
bellard 已提交
3579
            } else {
3580
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
3581 3582
                SSEFunc_0_epl sse_fn_epl = sse_op_table3aq[(b >> 8) & 1];
                sse_fn_epl(cpu_env, cpu_ptr0, cpu_T[0]);
3583 3584 3585
#else
                goto illegal_op;
#endif
B
bellard 已提交
3586
            }
B
bellard 已提交
3587 3588 3589 3590 3591
            break;
        case 0x02c: /* cvttps2pi */
        case 0x12c: /* cvttpd2pi */
        case 0x02d: /* cvtps2pi */
        case 0x12d: /* cvtpd2pi */
B
Blue Swirl 已提交
3592
            gen_helper_enter_mmx(cpu_env);
B
bellard 已提交
3593
            if (mod != 3) {
3594
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
3595
                op2_offset = offsetof(CPUX86State,xmm_t0);
3596
                gen_ldo_env_A0(s, op2_offset);
B
bellard 已提交
3597 3598 3599 3600 3601
            } else {
                rm = (modrm & 7) | REX_B(s);
                op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
            }
            op1_offset = offsetof(CPUX86State,fpregs[reg & 7].mmx);
B
bellard 已提交
3602 3603
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
bellard 已提交
3604 3605
            switch(b) {
            case 0x02c:
B
Blue Swirl 已提交
3606
                gen_helper_cvttps2pi(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3607 3608
                break;
            case 0x12c:
B
Blue Swirl 已提交
3609
                gen_helper_cvttpd2pi(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3610 3611
                break;
            case 0x02d:
B
Blue Swirl 已提交
3612
                gen_helper_cvtps2pi(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3613 3614
                break;
            case 0x12d:
B
Blue Swirl 已提交
3615
                gen_helper_cvtpd2pi(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3616 3617 3618 3619 3620 3621 3622
                break;
            }
            break;
        case 0x22c: /* cvttss2si */
        case 0x32c: /* cvttsd2si */
        case 0x22d: /* cvtss2si */
        case 0x32d: /* cvtsd2si */
3623
            ot = (s->dflag == 2) ? MO_64 : MO_32;
B
bellard 已提交
3624
            if (mod != 3) {
3625
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
3626
                if ((b >> 8) & 1) {
3627
                    gen_ldq_env_A0(s, offsetof(CPUX86State, xmm_t0.XMM_Q(0)));
B
bellard 已提交
3628
                } else {
3629
                    gen_op_ld_v(s, MO_32, cpu_T[0], cpu_A0);
B
bellard 已提交
3630
                    tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(0)));
B
bellard 已提交
3631 3632 3633 3634 3635 3636
                }
                op2_offset = offsetof(CPUX86State,xmm_t0);
            } else {
                rm = (modrm & 7) | REX_B(s);
                op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
            }
B
bellard 已提交
3637
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op2_offset);
3638
            if (ot == MO_32) {
B
Blue Swirl 已提交
3639
                SSEFunc_i_ep sse_fn_i_ep =
3640
                    sse_op_table3bi[((b >> 7) & 2) | (b & 1)];
B
Blue Swirl 已提交
3641
                sse_fn_i_ep(cpu_tmp2_i32, cpu_env, cpu_ptr0);
3642
                tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
3643
            } else {
3644
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
3645
                SSEFunc_l_ep sse_fn_l_ep =
3646
                    sse_op_table3bq[((b >> 7) & 2) | (b & 1)];
B
Blue Swirl 已提交
3647
                sse_fn_l_ep(cpu_T[0], cpu_env, cpu_ptr0);
3648 3649 3650
#else
                goto illegal_op;
#endif
B
bellard 已提交
3651
            }
B
bellard 已提交
3652
            gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
3653 3654
            break;
        case 0xc4: /* pinsrw */
3655
        case 0x1c4:
B
bellard 已提交
3656
            s->rip_offset = 1;
3657
            gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
3658
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
3659 3660
            if (b1) {
                val &= 7;
B
bellard 已提交
3661 3662
                tcg_gen_st16_tl(cpu_T[0], cpu_env,
                                offsetof(CPUX86State,xmm_regs[reg].XMM_W(val)));
B
bellard 已提交
3663 3664
            } else {
                val &= 3;
B
bellard 已提交
3665 3666
                tcg_gen_st16_tl(cpu_T[0], cpu_env,
                                offsetof(CPUX86State,fpregs[reg].mmx.MMX_W(val)));
B
bellard 已提交
3667 3668 3669
            }
            break;
        case 0xc5: /* pextrw */
3670
        case 0x1c5:
B
bellard 已提交
3671 3672
            if (mod != 3)
                goto illegal_op;
3673
            ot = (s->dflag == 2) ? MO_64 : MO_32;
3674
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
3675 3676 3677
            if (b1) {
                val &= 7;
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
3678 3679
                tcg_gen_ld16u_tl(cpu_T[0], cpu_env,
                                 offsetof(CPUX86State,xmm_regs[rm].XMM_W(val)));
B
bellard 已提交
3680 3681 3682
            } else {
                val &= 3;
                rm = (modrm & 7);
B
bellard 已提交
3683 3684
                tcg_gen_ld16u_tl(cpu_T[0], cpu_env,
                                offsetof(CPUX86State,fpregs[rm].mmx.MMX_W(val)));
B
bellard 已提交
3685 3686
            }
            reg = ((modrm >> 3) & 7) | rex_r;
3687
            gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
3688 3689 3690
            break;
        case 0x1d6: /* movq ea, xmm */
            if (mod != 3) {
3691
                gen_lea_modrm(env, s, modrm);
3692 3693
                gen_stq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3694 3695 3696 3697 3698 3699 3700 3701
            } 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 已提交
3702
            gen_helper_enter_mmx(cpu_env);
3703 3704 3705 3706
            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 已提交
3707 3708
            break;
        case 0x3d6: /* movdq2q */
B
Blue Swirl 已提交
3709
            gen_helper_enter_mmx(cpu_env);
3710 3711 3712
            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 已提交
3713 3714 3715 3716 3717 3718 3719
            break;
        case 0xd7: /* pmovmskb */
        case 0x1d7:
            if (mod != 3)
                goto illegal_op;
            if (b1) {
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
3720
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, offsetof(CPUX86State,xmm_regs[rm]));
B
Blue Swirl 已提交
3721
                gen_helper_pmovmskb_xmm(cpu_tmp2_i32, cpu_env, cpu_ptr0);
B
bellard 已提交
3722 3723
            } else {
                rm = (modrm & 7);
B
bellard 已提交
3724
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, offsetof(CPUX86State,fpregs[rm].mmx));
B
Blue Swirl 已提交
3725
                gen_helper_pmovmskb_mmx(cpu_tmp2_i32, cpu_env, cpu_ptr0);
B
bellard 已提交
3726 3727
            }
            reg = ((modrm >> 3) & 7) | rex_r;
3728
            tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp2_i32);
B
bellard 已提交
3729
            break;
R
Richard Henderson 已提交
3730

B
balrog 已提交
3731
        case 0x138:
3732
        case 0x038:
B
balrog 已提交
3733
            b = modrm;
R
Richard Henderson 已提交
3734 3735 3736
            if ((b & 0xf0) == 0xf0) {
                goto do_0f_38_fx;
            }
3737
            modrm = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
3738 3739 3740
            rm = modrm & 7;
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
3741 3742 3743
            if (b1 >= 2) {
                goto illegal_op;
            }
B
balrog 已提交
3744

B
Blue Swirl 已提交
3745 3746
            sse_fn_epp = sse_op_table6[b].op[b1];
            if (!sse_fn_epp) {
B
balrog 已提交
3747
                goto illegal_op;
B
Blue Swirl 已提交
3748
            }
B
balrog 已提交
3749 3750
            if (!(s->cpuid_ext_features & sse_op_table6[b].ext_mask))
                goto illegal_op;
B
balrog 已提交
3751 3752 3753 3754 3755 3756 3757

            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);
3758
                    gen_lea_modrm(env, s, modrm);
B
balrog 已提交
3759 3760 3761 3762
                    switch (b) {
                    case 0x20: case 0x30: /* pmovsxbw, pmovzxbw */
                    case 0x23: case 0x33: /* pmovsxwd, pmovzxwd */
                    case 0x25: case 0x35: /* pmovsxdq, pmovzxdq */
3763
                        gen_ldq_env_A0(s, op2_offset +
B
balrog 已提交
3764 3765 3766 3767
                                        offsetof(XMMReg, XMM_Q(0)));
                        break;
                    case 0x21: case 0x31: /* pmovsxbd, pmovzxbd */
                    case 0x24: case 0x34: /* pmovsxwq, pmovzxwq */
3768 3769
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
balrog 已提交
3770 3771 3772 3773
                        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, op2_offset +
                                        offsetof(XMMReg, XMM_L(0)));
                        break;
                    case 0x22: case 0x32: /* pmovsxbq, pmovzxbq */
3774 3775
                        tcg_gen_qemu_ld_tl(cpu_tmp0, cpu_A0,
                                           s->mem_index, MO_LEUW);
B
balrog 已提交
3776 3777 3778 3779
                        tcg_gen_st16_tl(cpu_tmp0, cpu_env, op2_offset +
                                        offsetof(XMMReg, XMM_W(0)));
                        break;
                    case 0x2a:            /* movntqda */
3780
                        gen_ldo_env_A0(s, op1_offset);
B
balrog 已提交
3781 3782
                        return;
                    default:
3783
                        gen_ldo_env_A0(s, op2_offset);
B
balrog 已提交
3784
                    }
B
balrog 已提交
3785 3786 3787 3788 3789 3790 3791
                }
            } 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);
3792
                    gen_lea_modrm(env, s, modrm);
3793
                    gen_ldq_env_A0(s, op2_offset);
B
balrog 已提交
3794 3795
                }
            }
B
Blue Swirl 已提交
3796
            if (sse_fn_epp == SSE_SPECIAL) {
B
balrog 已提交
3797
                goto illegal_op;
B
Blue Swirl 已提交
3798
            }
B
balrog 已提交
3799

B
balrog 已提交
3800 3801
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
3802
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
balrog 已提交
3803

3804 3805 3806
            if (b == 0x17) {
                set_cc_op(s, CC_OP_EFLAGS);
            }
B
balrog 已提交
3807
            break;
R
Richard Henderson 已提交
3808 3809 3810 3811 3812 3813

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

R
Richard Henderson 已提交
3817 3818 3819 3820 3821 3822 3823 3824
            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) {
3825
                    ot = MO_8;
R
Richard Henderson 已提交
3826
                } else if (s->dflag != 2) {
3827
                    ot = (s->prefix & PREFIX_DATA ? MO_16 : MO_32);
R
Richard Henderson 已提交
3828
                } else {
3829
                    ot = MO_64;
R
Richard Henderson 已提交
3830
                }
B
balrog 已提交
3831

3832
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[reg]);
R
Richard Henderson 已提交
3833 3834 3835
                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 已提交
3836

3837
                ot = (s->dflag == 2) ? MO_64 : MO_32;
R
Richard Henderson 已提交
3838 3839
                gen_op_mov_reg_T0(ot, reg);
                break;
B
balrog 已提交
3840

R
Richard Henderson 已提交
3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855
            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;
                }
                if (s->dflag != 2) {
3856
                    ot = (s->prefix & PREFIX_DATA ? MO_16 : MO_32);
R
Richard Henderson 已提交
3857
                } else {
3858
                    ot = MO_64;
R
Richard Henderson 已提交
3859 3860
                }

3861
                gen_lea_modrm(env, s, modrm);
R
Richard Henderson 已提交
3862
                if ((b & 1) == 0) {
3863 3864
                    tcg_gen_qemu_ld_tl(cpu_T[0], cpu_A0,
                                       s->mem_index, ot | MO_BE);
R
Richard Henderson 已提交
3865 3866
                    gen_op_mov_reg_T0(ot, reg);
                } else {
3867 3868
                    tcg_gen_qemu_st_tl(cpu_regs[reg], cpu_A0,
                                       s->mem_index, ot | MO_BE);
R
Richard Henderson 已提交
3869 3870 3871
                }
                break;

R
Richard Henderson 已提交
3872 3873 3874 3875 3876 3877
            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;
                }
3878
                ot = s->dflag == 2 ? MO_64 : MO_32;
R
Richard Henderson 已提交
3879 3880 3881 3882 3883 3884 3885
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                tcg_gen_andc_tl(cpu_T[0], cpu_regs[s->vex_v], cpu_T[0]);
                gen_op_mov_reg_T0(ot, reg);
                gen_op_update1_cc();
                set_cc_op(s, CC_OP_LOGICB + ot);
                break;

R
Richard Henderson 已提交
3886 3887 3888 3889 3890 3891
            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;
                }
3892
                ot = s->dflag == 2 ? MO_64 : MO_32;
R
Richard Henderson 已提交
3893 3894 3895 3896 3897 3898 3899 3900 3901
                {
                    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);

3902
                    bound = tcg_const_tl(ot == MO_64 ? 63 : 31);
R
Richard Henderson 已提交
3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925
                    zero = tcg_const_tl(0);
                    tcg_gen_movcond_tl(TCG_COND_LEU, cpu_T[0], cpu_A0, bound,
                                       cpu_T[0], zero);
                    tcg_temp_free(zero);

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

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

R
Richard Henderson 已提交
3926 3927 3928 3929 3930 3931
            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;
                }
3932
                ot = s->dflag == 2 ? MO_64 : MO_32;
R
Richard Henderson 已提交
3933 3934 3935
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                tcg_gen_ext8u_tl(cpu_T[1], cpu_regs[s->vex_v]);
                {
3936
                    TCGv bound = tcg_const_tl(ot == MO_64 ? 63 : 31);
R
Richard Henderson 已提交
3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952
                    /* Note that since we're using BMILG (in order to get O
                       cleared) we need to store the inverse into C.  */
                    tcg_gen_setcond_tl(TCG_COND_LT, cpu_cc_src,
                                       cpu_T[1], bound);
                    tcg_gen_movcond_tl(TCG_COND_GT, cpu_T[1], cpu_T[1],
                                       bound, bound, cpu_T[1]);
                    tcg_temp_free(bound);
                }
                tcg_gen_movi_tl(cpu_A0, -1);
                tcg_gen_shl_tl(cpu_A0, cpu_A0, cpu_T[1]);
                tcg_gen_andc_tl(cpu_T[0], cpu_T[0], cpu_A0);
                gen_op_mov_reg_T0(ot, reg);
                gen_op_update1_cc();
                set_cc_op(s, CC_OP_BMILGB + ot);
                break;

R
Richard Henderson 已提交
3953 3954 3955 3956 3957 3958
            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;
                }
3959
                ot = s->dflag == 2 ? MO_64 : MO_32;
R
Richard Henderson 已提交
3960 3961 3962
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                switch (ot) {
                default:
3963 3964 3965 3966 3967 3968
                    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 已提交
3969 3970
                    break;
#ifdef TARGET_X86_64
3971
                case MO_64:
3972 3973
                    tcg_gen_mulu2_i64(cpu_regs[s->vex_v], cpu_regs[reg],
                                      cpu_T[0], cpu_regs[R_EDX]);
R
Richard Henderson 已提交
3974 3975 3976 3977 3978
                    break;
#endif
                }
                break;

3979 3980 3981 3982 3983 3984
            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;
                }
3985
                ot = s->dflag == 2 ? MO_64 : MO_32;
3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002
                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.  */
                if (s->dflag == 2) {
                    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;
                }
4003
                ot = s->dflag == 2 ? MO_64 : MO_32;
4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014
                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.  */
                if (s->dflag == 2) {
                    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;

4015 4016 4017 4018 4019
            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 {
4020
                    TCGv carry_in, carry_out, zero;
4021 4022
                    int end_op;

4023
                    ot = (s->dflag == 2 ? MO_64 : MO_32);
4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050
                    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:
4051
                        end_op = (b == 0x1f6 ? CC_OP_ADCX : CC_OP_ADOX);
4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066
                        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
4067
                    case MO_32:
4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079
                        /* 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.  */
4080 4081 4082 4083 4084 4085 4086 4087
                        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);
4088 4089 4090 4091 4092 4093
                        break;
                    }
                    set_cc_op(s, end_op);
                }
                break;

4094 4095 4096 4097 4098 4099 4100 4101
            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;
                }
4102
                ot = (s->dflag == 2 ? MO_64 : MO_32);
4103
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
4104
                if (ot == MO_64) {
4105 4106 4107 4108 4109 4110 4111
                    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) {
4112
                    if (ot != MO_64) {
4113 4114 4115 4116
                        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
                    }
                    tcg_gen_sar_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
                } else {
4117
                    if (ot != MO_64) {
4118 4119 4120 4121 4122 4123 4124
                        tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
                    }
                    tcg_gen_shr_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
                }
                gen_op_mov_reg_T0(ot, reg);
                break;

4125 4126 4127 4128 4129 4130 4131 4132 4133
            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;
                }
4134
                ot = s->dflag == 2 ? MO_64 : MO_32;
4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);

                switch (reg & 7) {
                case 1: /* blsr By,Ey */
                    tcg_gen_neg_tl(cpu_T[1], cpu_T[0]);
                    tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
                    gen_op_mov_reg_T0(ot, s->vex_v);
                    gen_op_update2_cc();
                    set_cc_op(s, CC_OP_BMILGB + ot);
                    break;

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

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

                default:
                    goto illegal_op;
                }
                break;

R
Richard Henderson 已提交
4167 4168 4169
            default:
                goto illegal_op;
            }
B
balrog 已提交
4170
            break;
R
Richard Henderson 已提交
4171

B
balrog 已提交
4172 4173
        case 0x03a:
        case 0x13a:
B
balrog 已提交
4174
            b = modrm;
4175
            modrm = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
4176 4177 4178
            rm = modrm & 7;
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
4179 4180 4181
            if (b1 >= 2) {
                goto illegal_op;
            }
B
balrog 已提交
4182

B
Blue Swirl 已提交
4183 4184
            sse_fn_eppi = sse_op_table7[b].op[b1];
            if (!sse_fn_eppi) {
B
balrog 已提交
4185
                goto illegal_op;
B
Blue Swirl 已提交
4186
            }
B
balrog 已提交
4187 4188 4189
            if (!(s->cpuid_ext_features & sse_op_table7[b].ext_mask))
                goto illegal_op;

B
Blue Swirl 已提交
4190
            if (sse_fn_eppi == SSE_SPECIAL) {
4191
                ot = (s->dflag == 2) ? MO_64 : MO_32;
B
balrog 已提交
4192 4193
                rm = (modrm & 7) | REX_B(s);
                if (mod != 3)
4194
                    gen_lea_modrm(env, s, modrm);
B
balrog 已提交
4195
                reg = ((modrm >> 3) & 7) | rex_r;
4196
                val = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
4197 4198 4199 4200
                switch (b) {
                case 0x14: /* pextrb */
                    tcg_gen_ld8u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
                                            xmm_regs[reg].XMM_B(val & 15)));
4201
                    if (mod == 3) {
B
balrog 已提交
4202
                        gen_op_mov_reg_T0(ot, rm);
4203 4204 4205 4206
                    } else {
                        tcg_gen_qemu_st_tl(cpu_T[0], cpu_A0,
                                           s->mem_index, MO_UB);
                    }
B
balrog 已提交
4207 4208 4209 4210
                    break;
                case 0x15: /* pextrw */
                    tcg_gen_ld16u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
                                            xmm_regs[reg].XMM_W(val & 7)));
4211
                    if (mod == 3) {
B
balrog 已提交
4212
                        gen_op_mov_reg_T0(ot, rm);
4213 4214 4215 4216
                    } else {
                        tcg_gen_qemu_st_tl(cpu_T[0], cpu_A0,
                                           s->mem_index, MO_LEUW);
                    }
B
balrog 已提交
4217 4218
                    break;
                case 0x16:
4219
                    if (ot == MO_32) { /* pextrd */
B
balrog 已提交
4220 4221 4222
                        tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env,
                                        offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_L(val & 3)));
4223
                        if (mod == 3) {
4224
                            tcg_gen_extu_i32_tl(cpu_regs[rm], cpu_tmp2_i32);
4225
                        } else {
4226 4227
                            tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                                s->mem_index, MO_LEUL);
4228
                        }
B
balrog 已提交
4229
                    } else { /* pextrq */
P
pbrook 已提交
4230
#ifdef TARGET_X86_64
B
balrog 已提交
4231 4232 4233
                        tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env,
                                        offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_Q(val & 1)));
4234
                        if (mod == 3) {
4235
                            tcg_gen_mov_i64(cpu_regs[rm], cpu_tmp1_i64);
4236 4237 4238 4239
                        } else {
                            tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0,
                                                s->mem_index, MO_LEQ);
                        }
P
pbrook 已提交
4240 4241 4242
#else
                        goto illegal_op;
#endif
B
balrog 已提交
4243 4244 4245 4246 4247
                    }
                    break;
                case 0x17: /* extractps */
                    tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
                                            xmm_regs[reg].XMM_L(val & 3)));
4248
                    if (mod == 3) {
B
balrog 已提交
4249
                        gen_op_mov_reg_T0(ot, rm);
4250 4251 4252 4253
                    } else {
                        tcg_gen_qemu_st_tl(cpu_T[0], cpu_A0,
                                           s->mem_index, MO_LEUL);
                    }
B
balrog 已提交
4254 4255
                    break;
                case 0x20: /* pinsrb */
4256
                    if (mod == 3) {
4257
                        gen_op_mov_TN_reg(MO_32, 0, rm);
4258 4259 4260 4261
                    } else {
                        tcg_gen_qemu_ld_tl(cpu_T[0], cpu_A0,
                                           s->mem_index, MO_UB);
                    }
4262
                    tcg_gen_st8_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
B
balrog 已提交
4263 4264 4265
                                            xmm_regs[reg].XMM_B(val & 15)));
                    break;
                case 0x21: /* insertps */
P
pbrook 已提交
4266
                    if (mod == 3) {
B
balrog 已提交
4267 4268 4269
                        tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env,
                                        offsetof(CPUX86State,xmm_regs[rm]
                                                .XMM_L((val >> 6) & 3)));
P
pbrook 已提交
4270
                    } else {
4271 4272
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
P
pbrook 已提交
4273
                    }
B
balrog 已提交
4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294
                    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:
4295
                    if (ot == MO_32) { /* pinsrd */
4296
                        if (mod == 3) {
4297
                            tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[rm]);
4298
                        } else {
4299 4300
                            tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                                s->mem_index, MO_LEUL);
4301
                        }
B
balrog 已提交
4302 4303 4304 4305
                        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env,
                                        offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_L(val & 3)));
                    } else { /* pinsrq */
P
pbrook 已提交
4306
#ifdef TARGET_X86_64
4307
                        if (mod == 3) {
B
balrog 已提交
4308
                            gen_op_mov_v_reg(ot, cpu_tmp1_i64, rm);
4309 4310 4311 4312
                        } else {
                            tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0,
                                                s->mem_index, MO_LEQ);
                        }
B
balrog 已提交
4313 4314 4315
                        tcg_gen_st_i64(cpu_tmp1_i64, cpu_env,
                                        offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_Q(val & 1)));
P
pbrook 已提交
4316 4317 4318
#else
                        goto illegal_op;
#endif
B
balrog 已提交
4319 4320 4321 4322 4323
                    }
                    break;
                }
                return;
            }
B
balrog 已提交
4324 4325 4326 4327 4328 4329 4330

            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);
4331
                    gen_lea_modrm(env, s, modrm);
4332
                    gen_ldo_env_A0(s, op2_offset);
B
balrog 已提交
4333 4334 4335 4336 4337 4338 4339
                }
            } 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);
4340
                    gen_lea_modrm(env, s, modrm);
4341
                    gen_ldq_env_A0(s, op2_offset);
B
balrog 已提交
4342 4343
                }
            }
4344
            val = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
4345

B
balrog 已提交
4346
            if ((b & 0xfc) == 0x60) { /* pcmpXstrX */
4347
                set_cc_op(s, CC_OP_EFLAGS);
B
balrog 已提交
4348 4349 4350 4351 4352 4353

                if (s->dflag == 2)
                    /* The helper must use entire 64-bit gp registers */
                    val |= 1 << 8;
            }

B
balrog 已提交
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_eppi(cpu_env, cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
B
balrog 已提交
4357
            break;
R
Richard Henderson 已提交
4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371

        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;
                }
4372
                ot = s->dflag == 2 ? MO_64 : MO_32;
R
Richard Henderson 已提交
4373 4374
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                b = cpu_ldub_code(env, s->pc++);
4375
                if (ot == MO_64) {
R
Richard Henderson 已提交
4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389
                    tcg_gen_rotri_tl(cpu_T[0], cpu_T[0], b & 63);
                } else {
                    tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
                    tcg_gen_rotri_i32(cpu_tmp2_i32, cpu_tmp2_i32, b & 31);
                    tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
                }
                gen_op_mov_reg_T0(ot, reg);
                break;

            default:
                goto illegal_op;
            }
            break;

B
bellard 已提交
4390 4391 4392 4393 4394
        default:
            goto illegal_op;
        }
    } else {
        /* generic MMX or SSE operation */
B
bellard 已提交
4395 4396 4397 4398 4399 4400 4401 4402
        switch(b) {
        case 0x70: /* pshufx insn */
        case 0xc6: /* pshufx insn */
        case 0xc2: /* compare insns */
            s->rip_offset = 1;
            break;
        default:
            break;
B
bellard 已提交
4403 4404 4405 4406
        }
        if (is_xmm) {
            op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
            if (mod != 3) {
4407
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
4408
                op2_offset = offsetof(CPUX86State,xmm_t0);
4409
                if (b1 >= 2 && ((b >= 0x50 && b <= 0x5f && b != 0x5b) ||
B
bellard 已提交
4410 4411 4412 4413
                                b == 0xc2)) {
                    /* specific case for SSE single instructions */
                    if (b1 == 2) {
                        /* 32 bit access */
4414
                        gen_op_ld_v(s, MO_32, cpu_T[0], cpu_A0);
B
bellard 已提交
4415
                        tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(0)));
B
bellard 已提交
4416 4417
                    } else {
                        /* 64 bit access */
4418 4419
                        gen_ldq_env_A0(s, offsetof(CPUX86State,
                                                   xmm_t0.XMM_D(0)));
B
bellard 已提交
4420 4421
                    }
                } else {
4422
                    gen_ldo_env_A0(s, op2_offset);
B
bellard 已提交
4423 4424 4425 4426 4427 4428 4429 4430
                }
            } 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) {
4431
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
4432
                op2_offset = offsetof(CPUX86State,mmx_t0);
4433
                gen_ldq_env_A0(s, op2_offset);
B
bellard 已提交
4434 4435 4436 4437 4438 4439
            } else {
                rm = (modrm & 7);
                op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
            }
        }
        switch(b) {
A
aurel32 已提交
4440
        case 0x0f: /* 3DNow! data insns */
4441 4442
            if (!(s->cpuid_ext2_features & CPUID_EXT2_3DNOW))
                goto illegal_op;
4443
            val = cpu_ldub_code(env, s->pc++);
B
Blue Swirl 已提交
4444 4445
            sse_fn_epp = sse_op_table5[val];
            if (!sse_fn_epp) {
A
aurel32 已提交
4446
                goto illegal_op;
B
Blue Swirl 已提交
4447
            }
B
bellard 已提交
4448 4449
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4450
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
A
aurel32 已提交
4451
            break;
B
bellard 已提交
4452 4453
        case 0x70: /* pshufx insn */
        case 0xc6: /* pshufx insn */
4454
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4455 4456
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4457
            /* XXX: introduce a new table? */
B
Blue Swirl 已提交
4458
            sse_fn_ppi = (SSEFunc_0_ppi)sse_fn_epp;
B
Blue Swirl 已提交
4459
            sse_fn_ppi(cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
B
bellard 已提交
4460 4461 4462
            break;
        case 0xc2:
            /* compare insns */
4463
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4464 4465
            if (val >= 8)
                goto illegal_op;
B
Blue Swirl 已提交
4466
            sse_fn_epp = sse_op_table4[val][b1];
B
Blue Swirl 已提交
4467

B
bellard 已提交
4468 4469
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4470
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
4471
            break;
4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489
        case 0xf7:
            /* maskmov : we must prepare A0 */
            if (mod != 3)
                goto illegal_op;
#ifdef TARGET_X86_64
            if (s->aflag == 2) {
                gen_op_movq_A0_reg(R_EDI);
            } else
#endif
            {
                gen_op_movl_A0_reg(R_EDI);
                if (s->aflag == 0)
                    gen_op_andl_A0_ffff();
            }
            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 已提交
4490
            /* XXX: introduce a new table? */
B
Blue Swirl 已提交
4491 4492
            sse_fn_eppt = (SSEFunc_0_eppt)sse_fn_epp;
            sse_fn_eppt(cpu_env, cpu_ptr0, cpu_ptr1, cpu_A0);
4493
            break;
B
bellard 已提交
4494
        default:
B
bellard 已提交
4495 4496
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4497
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
4498 4499 4500
            break;
        }
        if (b == 0x2e || b == 0x2f) {
4501
            set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
4502 4503 4504 4505
        }
    }
}

B
bellard 已提交
4506 4507
/* convert one instruction. s->is_jmp is set if the translation must
   be stopped. Return the next pc value */
4508 4509
static target_ulong disas_insn(CPUX86State *env, DisasContext *s,
                               target_ulong pc_start)
B
bellard 已提交
4510 4511 4512
{
    int b, prefixes, aflag, dflag;
    int shift, ot;
4513
    int modrm, reg, rm, mod, op, opreg, val;
B
bellard 已提交
4514 4515
    target_ulong next_eip, tval;
    int rex_w, rex_r;
B
bellard 已提交
4516

4517
    if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
4518
        tcg_gen_debug_insn_start(pc_start);
4519
    }
B
bellard 已提交
4520 4521 4522
    s->pc = pc_start;
    prefixes = 0;
    s->override = -1;
B
bellard 已提交
4523 4524 4525 4526 4527
    rex_w = -1;
    rex_r = 0;
#ifdef TARGET_X86_64
    s->rex_x = 0;
    s->rex_b = 0;
4528
    x86_64_hregs = 0;
B
bellard 已提交
4529 4530
#endif
    s->rip_offset = 0; /* for relative ip address */
4531 4532
    s->vex_l = 0;
    s->vex_v = 0;
B
bellard 已提交
4533
 next_byte:
4534
    b = cpu_ldub_code(env, s->pc);
B
bellard 已提交
4535
    s->pc++;
4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570
    /* 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 已提交
4571
#ifdef TARGET_X86_64
4572 4573
    case 0x40 ... 0x4f:
        if (CODE64(s)) {
B
bellard 已提交
4574 4575 4576 4577 4578 4579 4580 4581
            /* 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;
        }
4582 4583
        break;
#endif
4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600
    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 已提交
4601
            /* 4.1.1-4.1.3: No preceding lock, 66, f2, f3, or rex prefixes. */
4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640
            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;
4641 4642 4643 4644
    }

    /* Post-process prefixes.  */
    if (CODE64(s)) {
4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655
        /* 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.  */
        dflag = (rex_w > 0 ? 2 : prefixes & PREFIX_DATA ? 0 : 1);
        /* In 64-bit mode, 0x67 selects 32-bit addressing.  */
        aflag = (prefixes & PREFIX_ADR ? 1 : 2);
    } else {
        /* In 16/32-bit mode, 0x66 selects the opposite data size.  */
        dflag = s->code32;
        if (prefixes & PREFIX_DATA) {
            dflag ^= 1;
B
bellard 已提交
4656
        }
4657 4658 4659 4660
        /* In 16/32-bit mode, 0x67 selects the opposite addressing.  */
        aflag = s->code32;
        if (prefixes & PREFIX_ADR) {
            aflag ^= 1;
B
bellard 已提交
4661
        }
B
bellard 已提交
4662 4663 4664 4665 4666 4667 4668 4669
    }

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

    /* lock generation */
    if (prefixes & PREFIX_LOCK)
P
pbrook 已提交
4670
        gen_helper_lock();
B
bellard 已提交
4671 4672 4673 4674 4675 4676 4677

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

B
bellard 已提交
4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696
        /**************************/
        /* 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;

            if ((b & 1) == 0)
4697
                ot = MO_8;
B
bellard 已提交
4698
            else
4699
                ot = dflag + MO_16;
4700

B
bellard 已提交
4701 4702
            switch(f) {
            case 0: /* OP Ev, Gv */
4703
                modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4704
                reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
4705
                mod = (modrm >> 6) & 3;
B
bellard 已提交
4706
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4707
                if (mod != 3) {
4708
                    gen_lea_modrm(env, s, modrm);
B
bellard 已提交
4709 4710 4711 4712
                    opreg = OR_TMP0;
                } else if (op == OP_XORL && rm == reg) {
                xor_zero:
                    /* xor reg, reg optimisation */
R
Richard Henderson 已提交
4713
                    set_cc_op(s, CC_OP_CLR);
4714
                    tcg_gen_movi_tl(cpu_T[0], 0);
B
bellard 已提交
4715
                    gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
4716 4717 4718 4719
                    break;
                } else {
                    opreg = rm;
                }
B
bellard 已提交
4720
                gen_op_mov_TN_reg(ot, 1, reg);
B
bellard 已提交
4721 4722 4723
                gen_op(s, op, ot, opreg);
                break;
            case 1: /* OP Gv, Ev */
4724
                modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4725
                mod = (modrm >> 6) & 3;
B
bellard 已提交
4726 4727
                reg = ((modrm >> 3) & 7) | rex_r;
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4728
                if (mod != 3) {
4729
                    gen_lea_modrm(env, s, modrm);
4730
                    gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
B
bellard 已提交
4731 4732 4733
                } else if (op == OP_XORL && rm == reg) {
                    goto xor_zero;
                } else {
B
bellard 已提交
4734
                    gen_op_mov_TN_reg(ot, 1, rm);
B
bellard 已提交
4735 4736 4737 4738
                }
                gen_op(s, op, ot, reg);
                break;
            case 2: /* OP A, Iv */
4739
                val = insn_get(env, s, ot);
B
bellard 已提交
4740 4741 4742 4743 4744 4745 4746
                gen_op_movl_T1_im(val);
                gen_op(s, op, ot, OR_EAX);
                break;
            }
        }
        break;

4747 4748 4749
    case 0x82:
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
4750 4751 4752 4753 4754 4755 4756
    case 0x80: /* GRP1 */
    case 0x81:
    case 0x83:
        {
            int val;

            if ((b & 1) == 0)
4757
                ot = MO_8;
B
bellard 已提交
4758
            else
4759
                ot = dflag + MO_16;
4760

4761
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4762
            mod = (modrm >> 6) & 3;
B
bellard 已提交
4763
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4764
            op = (modrm >> 3) & 7;
4765

B
bellard 已提交
4766
            if (mod != 3) {
B
bellard 已提交
4767 4768 4769 4770
                if (b == 0x83)
                    s->rip_offset = 1;
                else
                    s->rip_offset = insn_const_size(ot);
4771
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
4772 4773
                opreg = OR_TMP0;
            } else {
B
bellard 已提交
4774
                opreg = rm;
B
bellard 已提交
4775 4776 4777 4778 4779 4780
            }

            switch(b) {
            default:
            case 0x80:
            case 0x81:
4781
            case 0x82:
4782
                val = insn_get(env, s, ot);
B
bellard 已提交
4783 4784
                break;
            case 0x83:
4785
                val = (int8_t)insn_get(env, s, MO_8);
B
bellard 已提交
4786 4787 4788 4789 4790 4791 4792 4793 4794 4795
                break;
            }
            gen_op_movl_T1_im(val);
            gen_op(s, op, ot, opreg);
        }
        break;

        /**************************/
        /* inc, dec, and other misc arith */
    case 0x40 ... 0x47: /* inc Gv */
4796
        ot = dflag ? MO_32 : MO_16;
B
bellard 已提交
4797 4798 4799
        gen_inc(s, ot, OR_EAX + (b & 7), 1);
        break;
    case 0x48 ... 0x4f: /* dec Gv */
4800
        ot = dflag ? MO_32 : MO_16;
B
bellard 已提交
4801 4802 4803 4804 4805
        gen_inc(s, ot, OR_EAX + (b & 7), -1);
        break;
    case 0xf6: /* GRP3 */
    case 0xf7:
        if ((b & 1) == 0)
4806
            ot = MO_8;
B
bellard 已提交
4807
        else
4808
            ot = dflag + MO_16;
B
bellard 已提交
4809

4810
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4811
        mod = (modrm >> 6) & 3;
B
bellard 已提交
4812
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4813 4814
        op = (modrm >> 3) & 7;
        if (mod != 3) {
B
bellard 已提交
4815 4816
            if (op == 0)
                s->rip_offset = insn_const_size(ot);
4817
            gen_lea_modrm(env, s, modrm);
4818
            gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
4819
        } else {
B
bellard 已提交
4820
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
4821 4822 4823 4824
        }

        switch(op) {
        case 0: /* test */
4825
            val = insn_get(env, s, ot);
B
bellard 已提交
4826 4827
            gen_op_movl_T1_im(val);
            gen_op_testl_T0_T1_cc();
4828
            set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
4829 4830
            break;
        case 2: /* not */
4831
            tcg_gen_not_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
4832
            if (mod != 3) {
4833
                gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
4834
            } else {
B
bellard 已提交
4835
                gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
4836 4837 4838
            }
            break;
        case 3: /* neg */
4839
            tcg_gen_neg_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
4840
            if (mod != 3) {
4841
                gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
4842
            } else {
B
bellard 已提交
4843
                gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
4844 4845
            }
            gen_op_update_neg_cc();
4846
            set_cc_op(s, CC_OP_SUBB + ot);
B
bellard 已提交
4847 4848 4849
            break;
        case 4: /* mul */
            switch(ot) {
4850 4851
            case MO_8:
                gen_op_mov_TN_reg(MO_8, 1, R_EAX);
B
bellard 已提交
4852 4853 4854 4855
                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]);
4856
                gen_op_mov_reg_T0(MO_16, R_EAX);
B
bellard 已提交
4857 4858
                tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                tcg_gen_andi_tl(cpu_cc_src, cpu_T[0], 0xff00);
4859
                set_cc_op(s, CC_OP_MULB);
B
bellard 已提交
4860
                break;
4861 4862
            case MO_16:
                gen_op_mov_TN_reg(MO_16, 1, R_EAX);
B
bellard 已提交
4863 4864 4865 4866
                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]);
4867
                gen_op_mov_reg_T0(MO_16, R_EAX);
B
bellard 已提交
4868 4869
                tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                tcg_gen_shri_tl(cpu_T[0], cpu_T[0], 16);
4870
                gen_op_mov_reg_T0(MO_16, R_EDX);
B
bellard 已提交
4871
                tcg_gen_mov_tl(cpu_cc_src, cpu_T[0]);
4872
                set_cc_op(s, CC_OP_MULW);
B
bellard 已提交
4873 4874
                break;
            default:
4875
            case MO_32:
4876 4877 4878 4879 4880 4881 4882 4883
                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]);
4884
                set_cc_op(s, CC_OP_MULL);
B
bellard 已提交
4885
                break;
B
bellard 已提交
4886
#ifdef TARGET_X86_64
4887
            case MO_64:
4888 4889 4890 4891
                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]);
4892
                set_cc_op(s, CC_OP_MULQ);
B
bellard 已提交
4893 4894
                break;
#endif
B
bellard 已提交
4895 4896 4897 4898
            }
            break;
        case 5: /* imul */
            switch(ot) {
4899 4900
            case MO_8:
                gen_op_mov_TN_reg(MO_8, 1, R_EAX);
B
bellard 已提交
4901 4902 4903 4904
                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]);
4905
                gen_op_mov_reg_T0(MO_16, R_EAX);
B
bellard 已提交
4906 4907 4908
                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);
4909
                set_cc_op(s, CC_OP_MULB);
B
bellard 已提交
4910
                break;
4911 4912
            case MO_16:
                gen_op_mov_TN_reg(MO_16, 1, R_EAX);
B
bellard 已提交
4913 4914 4915 4916
                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]);
4917
                gen_op_mov_reg_T0(MO_16, R_EAX);
B
bellard 已提交
4918 4919 4920 4921
                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);
4922
                gen_op_mov_reg_T0(MO_16, R_EDX);
4923
                set_cc_op(s, CC_OP_MULW);
B
bellard 已提交
4924 4925
                break;
            default:
4926
            case MO_32:
4927 4928 4929 4930 4931 4932 4933 4934 4935 4936
                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);
4937
                set_cc_op(s, CC_OP_MULL);
B
bellard 已提交
4938
                break;
B
bellard 已提交
4939
#ifdef TARGET_X86_64
4940
            case MO_64:
4941 4942 4943 4944 4945
                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]);
4946
                set_cc_op(s, CC_OP_MULQ);
B
bellard 已提交
4947 4948
                break;
#endif
B
bellard 已提交
4949 4950 4951 4952
            }
            break;
        case 6: /* div */
            switch(ot) {
4953
            case MO_8:
B
bellard 已提交
4954
                gen_jmp_im(pc_start - s->cs_base);
4955
                gen_helper_divb_AL(cpu_env, cpu_T[0]);
B
bellard 已提交
4956
                break;
4957
            case MO_16:
B
bellard 已提交
4958
                gen_jmp_im(pc_start - s->cs_base);
4959
                gen_helper_divw_AX(cpu_env, cpu_T[0]);
B
bellard 已提交
4960 4961
                break;
            default:
4962
            case MO_32:
B
bellard 已提交
4963
                gen_jmp_im(pc_start - s->cs_base);
4964
                gen_helper_divl_EAX(cpu_env, cpu_T[0]);
B
bellard 已提交
4965 4966
                break;
#ifdef TARGET_X86_64
4967
            case MO_64:
B
bellard 已提交
4968
                gen_jmp_im(pc_start - s->cs_base);
4969
                gen_helper_divq_EAX(cpu_env, cpu_T[0]);
B
bellard 已提交
4970
                break;
B
bellard 已提交
4971
#endif
B
bellard 已提交
4972 4973 4974 4975
            }
            break;
        case 7: /* idiv */
            switch(ot) {
4976
            case MO_8:
B
bellard 已提交
4977
                gen_jmp_im(pc_start - s->cs_base);
4978
                gen_helper_idivb_AL(cpu_env, cpu_T[0]);
B
bellard 已提交
4979
                break;
4980
            case MO_16:
B
bellard 已提交
4981
                gen_jmp_im(pc_start - s->cs_base);
4982
                gen_helper_idivw_AX(cpu_env, cpu_T[0]);
B
bellard 已提交
4983 4984
                break;
            default:
4985
            case MO_32:
B
bellard 已提交
4986
                gen_jmp_im(pc_start - s->cs_base);
4987
                gen_helper_idivl_EAX(cpu_env, cpu_T[0]);
B
bellard 已提交
4988 4989
                break;
#ifdef TARGET_X86_64
4990
            case MO_64:
B
bellard 已提交
4991
                gen_jmp_im(pc_start - s->cs_base);
4992
                gen_helper_idivq_EAX(cpu_env, cpu_T[0]);
B
bellard 已提交
4993
                break;
B
bellard 已提交
4994
#endif
B
bellard 已提交
4995 4996 4997 4998 4999 5000 5001 5002 5003 5004
            }
            break;
        default:
            goto illegal_op;
        }
        break;

    case 0xfe: /* GRP4 */
    case 0xff: /* GRP5 */
        if ((b & 1) == 0)
5005
            ot = MO_8;
B
bellard 已提交
5006
        else
5007
            ot = dflag + MO_16;
B
bellard 已提交
5008

5009
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5010
        mod = (modrm >> 6) & 3;
B
bellard 已提交
5011
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
5012 5013 5014 5015
        op = (modrm >> 3) & 7;
        if (op >= 2 && b == 0xfe) {
            goto illegal_op;
        }
B
bellard 已提交
5016
        if (CODE64(s)) {
5017
            if (op == 2 || op == 4) {
B
bellard 已提交
5018
                /* operand size for jumps is 64 bit */
5019
                ot = MO_64;
5020
            } else if (op == 3 || op == 5) {
5021
                ot = dflag ? MO_32 + (rex_w == 1) : MO_16;
B
bellard 已提交
5022 5023
            } else if (op == 6) {
                /* default push size is 64 bit */
5024
                ot = dflag ? MO_64 : MO_16;
B
bellard 已提交
5025 5026
            }
        }
B
bellard 已提交
5027
        if (mod != 3) {
5028
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
5029
            if (op >= 2 && op != 3 && op != 5)
5030
                gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
5031
        } else {
B
bellard 已提交
5032
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050
        }

        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 */
5051
            /* XXX: optimize if memory (no 'and' is necessary) */
B
bellard 已提交
5052 5053 5054
            if (s->dflag == 0)
                gen_op_andl_T0_ffff();
            next_eip = s->pc - s->cs_base;
B
bellard 已提交
5055
            gen_movtl_T1_im(next_eip);
5056 5057
            gen_push_T1(s);
            gen_op_jmp_T0();
B
bellard 已提交
5058 5059
            gen_eob(s);
            break;
B
bellard 已提交
5060
        case 3: /* lcall Ev */
5061
            gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
5062
            gen_add_A0_im(s, 1 << (ot - MO_16 + 1));
5063
            gen_op_ld_v(s, MO_16, cpu_T[0], cpu_A0);
B
bellard 已提交
5064 5065
        do_lcall:
            if (s->pe && !s->vm86) {
5066
                gen_update_cc_op(s);
B
bellard 已提交
5067
                gen_jmp_im(pc_start - s->cs_base);
5068
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
5069 5070
                gen_helper_lcall_protected(cpu_env, cpu_tmp2_i32, cpu_T[1],
                                           tcg_const_i32(dflag),
P
pbrook 已提交
5071
                                           tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
5072
            } else {
5073
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
5074 5075
                gen_helper_lcall_real(cpu_env, cpu_tmp2_i32, cpu_T[1],
                                      tcg_const_i32(dflag),
P
pbrook 已提交
5076
                                      tcg_const_i32(s->pc - s->cs_base));
B
bellard 已提交
5077 5078 5079 5080 5081 5082 5083 5084 5085 5086
            }
            gen_eob(s);
            break;
        case 4: /* jmp Ev */
            if (s->dflag == 0)
                gen_op_andl_T0_ffff();
            gen_op_jmp_T0();
            gen_eob(s);
            break;
        case 5: /* ljmp Ev */
5087
            gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
5088
            gen_add_A0_im(s, 1 << (ot - MO_16 + 1));
5089
            gen_op_ld_v(s, MO_16, cpu_T[0], cpu_A0);
B
bellard 已提交
5090 5091
        do_ljmp:
            if (s->pe && !s->vm86) {
5092
                gen_update_cc_op(s);
B
bellard 已提交
5093
                gen_jmp_im(pc_start - s->cs_base);
5094
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
5095
                gen_helper_ljmp_protected(cpu_env, cpu_tmp2_i32, cpu_T[1],
P
pbrook 已提交
5096
                                          tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
5097
            } else {
5098
                gen_op_movl_seg_T0_vm(R_CS);
B
bellard 已提交
5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112
                gen_op_movl_T0_T1();
                gen_op_jmp_T0();
            }
            gen_eob(s);
            break;
        case 6: /* push Ev */
            gen_push_T0(s);
            break;
        default:
            goto illegal_op;
        }
        break;

    case 0x84: /* test Ev, Gv */
5113
    case 0x85:
B
bellard 已提交
5114
        if ((b & 1) == 0)
5115
            ot = MO_8;
B
bellard 已提交
5116
        else
5117
            ot = dflag + MO_16;
B
bellard 已提交
5118

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

5122
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
bellard 已提交
5123
        gen_op_mov_TN_reg(ot, 1, reg);
B
bellard 已提交
5124
        gen_op_testl_T0_T1_cc();
5125
        set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
5126
        break;
5127

B
bellard 已提交
5128 5129 5130
    case 0xa8: /* test eAX, Iv */
    case 0xa9:
        if ((b & 1) == 0)
5131
            ot = MO_8;
B
bellard 已提交
5132
        else
5133
            ot = dflag + MO_16;
5134
        val = insn_get(env, s, ot);
B
bellard 已提交
5135

B
bellard 已提交
5136
        gen_op_mov_TN_reg(ot, 0, OR_EAX);
B
bellard 已提交
5137 5138
        gen_op_movl_T1_im(val);
        gen_op_testl_T0_T1_cc();
5139
        set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
5140
        break;
5141

B
bellard 已提交
5142
    case 0x98: /* CWDE/CBW */
B
bellard 已提交
5143 5144
#ifdef TARGET_X86_64
        if (dflag == 2) {
5145
            gen_op_mov_TN_reg(MO_32, 0, R_EAX);
B
bellard 已提交
5146
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
5147
            gen_op_mov_reg_T0(MO_64, R_EAX);
B
bellard 已提交
5148 5149
        } else
#endif
B
bellard 已提交
5150
        if (dflag == 1) {
5151
            gen_op_mov_TN_reg(MO_16, 0, R_EAX);
B
bellard 已提交
5152
            tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
5153
            gen_op_mov_reg_T0(MO_32, R_EAX);
B
bellard 已提交
5154
        } else {
5155
            gen_op_mov_TN_reg(MO_8, 0, R_EAX);
B
bellard 已提交
5156
            tcg_gen_ext8s_tl(cpu_T[0], cpu_T[0]);
5157
            gen_op_mov_reg_T0(MO_16, R_EAX);
B
bellard 已提交
5158
        }
B
bellard 已提交
5159 5160
        break;
    case 0x99: /* CDQ/CWD */
B
bellard 已提交
5161 5162
#ifdef TARGET_X86_64
        if (dflag == 2) {
5163
            gen_op_mov_TN_reg(MO_64, 0, R_EAX);
B
bellard 已提交
5164
            tcg_gen_sari_tl(cpu_T[0], cpu_T[0], 63);
5165
            gen_op_mov_reg_T0(MO_64, R_EDX);
B
bellard 已提交
5166 5167
        } else
#endif
B
bellard 已提交
5168
        if (dflag == 1) {
5169
            gen_op_mov_TN_reg(MO_32, 0, R_EAX);
B
bellard 已提交
5170 5171
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
            tcg_gen_sari_tl(cpu_T[0], cpu_T[0], 31);
5172
            gen_op_mov_reg_T0(MO_32, R_EDX);
B
bellard 已提交
5173
        } else {
5174
            gen_op_mov_TN_reg(MO_16, 0, R_EAX);
B
bellard 已提交
5175 5176
            tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
            tcg_gen_sari_tl(cpu_T[0], cpu_T[0], 15);
5177
            gen_op_mov_reg_T0(MO_16, R_EDX);
B
bellard 已提交
5178
        }
B
bellard 已提交
5179 5180 5181 5182
        break;
    case 0x1af: /* imul Gv, Ev */
    case 0x69: /* imul Gv, Ev, I */
    case 0x6b:
5183
        ot = dflag + MO_16;
5184
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5185 5186 5187 5188 5189
        reg = ((modrm >> 3) & 7) | rex_r;
        if (b == 0x69)
            s->rip_offset = insn_const_size(ot);
        else if (b == 0x6b)
            s->rip_offset = 1;
5190
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
bellard 已提交
5191
        if (b == 0x69) {
5192
            val = insn_get(env, s, ot);
B
bellard 已提交
5193 5194
            gen_op_movl_T1_im(val);
        } else if (b == 0x6b) {
5195
            val = (int8_t)insn_get(env, s, MO_8);
B
bellard 已提交
5196 5197
            gen_op_movl_T1_im(val);
        } else {
B
bellard 已提交
5198
            gen_op_mov_TN_reg(ot, 1, reg);
B
bellard 已提交
5199
        }
5200
        switch (ot) {
B
bellard 已提交
5201
#ifdef TARGET_X86_64
5202
        case MO_64:
5203 5204 5205 5206 5207
            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 已提交
5208
#endif
5209
        case MO_32:
5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220
            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 已提交
5221 5222 5223 5224 5225 5226 5227
            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);
5228 5229
            gen_op_mov_reg_T0(ot, reg);
            break;
B
bellard 已提交
5230
        }
5231
        set_cc_op(s, CC_OP_MULB + ot);
B
bellard 已提交
5232 5233 5234 5235
        break;
    case 0x1c0:
    case 0x1c1: /* xadd Ev, Gv */
        if ((b & 1) == 0)
5236
            ot = MO_8;
B
bellard 已提交
5237
        else
5238
            ot = dflag + MO_16;
5239
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5240
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5241 5242
        mod = (modrm >> 6) & 3;
        if (mod == 3) {
B
bellard 已提交
5243
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
5244 5245
            gen_op_mov_TN_reg(ot, 0, reg);
            gen_op_mov_TN_reg(ot, 1, rm);
B
bellard 已提交
5246
            gen_op_addl_T0_T1();
B
bellard 已提交
5247 5248
            gen_op_mov_reg_T1(ot, reg);
            gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
5249
        } else {
5250
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
5251
            gen_op_mov_TN_reg(ot, 0, reg);
5252
            gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
B
bellard 已提交
5253
            gen_op_addl_T0_T1();
5254
            gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
5255
            gen_op_mov_reg_T1(ot, reg);
B
bellard 已提交
5256 5257
        }
        gen_op_update2_cc();
5258
        set_cc_op(s, CC_OP_ADDB + ot);
B
bellard 已提交
5259 5260 5261
        break;
    case 0x1b0:
    case 0x1b1: /* cmpxchg Ev, Gv */
B
bellard 已提交
5262
        {
B
bellard 已提交
5263
            int label1, label2;
5264
            TCGv t0, t1, t2, a0;
B
bellard 已提交
5265 5266

            if ((b & 1) == 0)
5267
                ot = MO_8;
B
bellard 已提交
5268
            else
5269
                ot = dflag + MO_16;
5270
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5271 5272
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
P
pbrook 已提交
5273 5274 5275 5276
            t0 = tcg_temp_local_new();
            t1 = tcg_temp_local_new();
            t2 = tcg_temp_local_new();
            a0 = tcg_temp_local_new();
5277
            gen_op_mov_v_reg(ot, t1, reg);
B
bellard 已提交
5278 5279
            if (mod == 3) {
                rm = (modrm & 7) | REX_B(s);
5280
                gen_op_mov_v_reg(ot, t0, rm);
B
bellard 已提交
5281
            } else {
5282
                gen_lea_modrm(env, s, modrm);
5283
                tcg_gen_mov_tl(a0, cpu_A0);
5284
                gen_op_ld_v(s, ot, t0, a0);
B
bellard 已提交
5285 5286 5287
                rm = 0; /* avoid warning */
            }
            label1 = gen_new_label();
5288 5289
            tcg_gen_mov_tl(t2, cpu_regs[R_EAX]);
            gen_extu(ot, t0);
5290
            gen_extu(ot, t2);
5291
            tcg_gen_brcond_tl(TCG_COND_EQ, t2, t0, label1);
5292
            label2 = gen_new_label();
B
bellard 已提交
5293
            if (mod == 3) {
5294
                gen_op_mov_reg_v(ot, R_EAX, t0);
B
bellard 已提交
5295 5296
                tcg_gen_br(label2);
                gen_set_label(label1);
5297
                gen_op_mov_reg_v(ot, rm, t1);
B
bellard 已提交
5298
            } else {
5299 5300 5301
                /* 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 */
5302
                gen_op_st_v(s, ot, t0, a0);
5303
                gen_op_mov_reg_v(ot, R_EAX, t0);
5304
                tcg_gen_br(label2);
B
bellard 已提交
5305
                gen_set_label(label1);
5306
                gen_op_st_v(s, ot, t1, a0);
B
bellard 已提交
5307
            }
5308
            gen_set_label(label2);
5309
            tcg_gen_mov_tl(cpu_cc_src, t0);
5310 5311
            tcg_gen_mov_tl(cpu_cc_srcT, t2);
            tcg_gen_sub_tl(cpu_cc_dst, t2, t0);
5312
            set_cc_op(s, CC_OP_SUBB + ot);
5313 5314 5315 5316
            tcg_temp_free(t0);
            tcg_temp_free(t1);
            tcg_temp_free(t2);
            tcg_temp_free(a0);
B
bellard 已提交
5317 5318 5319
        }
        break;
    case 0x1c7: /* cmpxchg8b */
5320
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5321
        mod = (modrm >> 6) & 3;
5322
        if ((mod == 3) || ((modrm & 0x38) != 0x8))
B
bellard 已提交
5323
            goto illegal_op;
B
bellard 已提交
5324 5325 5326 5327 5328
#ifdef TARGET_X86_64
        if (dflag == 2) {
            if (!(s->cpuid_ext_features & CPUID_EXT_CX16))
                goto illegal_op;
            gen_jmp_im(pc_start - s->cs_base);
5329
            gen_update_cc_op(s);
5330
            gen_lea_modrm(env, s, modrm);
B
Blue Swirl 已提交
5331
            gen_helper_cmpxchg16b(cpu_env, cpu_A0);
B
bellard 已提交
5332 5333 5334 5335 5336 5337
        } else
#endif        
        {
            if (!(s->cpuid_features & CPUID_CX8))
                goto illegal_op;
            gen_jmp_im(pc_start - s->cs_base);
5338
            gen_update_cc_op(s);
5339
            gen_lea_modrm(env, s, modrm);
B
Blue Swirl 已提交
5340
            gen_helper_cmpxchg8b(cpu_env, cpu_A0);
B
bellard 已提交
5341
        }
5342
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
5343
        break;
5344

B
bellard 已提交
5345 5346 5347
        /**************************/
        /* push/pop */
    case 0x50 ... 0x57: /* push */
5348
        gen_op_mov_TN_reg(MO_32, 0, (b & 7) | REX_B(s));
B
bellard 已提交
5349 5350 5351
        gen_push_T0(s);
        break;
    case 0x58 ... 0x5f: /* pop */
B
bellard 已提交
5352
        if (CODE64(s)) {
5353
            ot = dflag ? MO_64 : MO_16;
B
bellard 已提交
5354
        } else {
5355
            ot = dflag + MO_16;
B
bellard 已提交
5356
        }
B
bellard 已提交
5357
        gen_pop_T0(s);
B
bellard 已提交
5358
        /* NOTE: order is important for pop %sp */
B
bellard 已提交
5359
        gen_pop_update(s);
B
bellard 已提交
5360
        gen_op_mov_reg_T0(ot, (b & 7) | REX_B(s));
B
bellard 已提交
5361 5362
        break;
    case 0x60: /* pusha */
B
bellard 已提交
5363 5364
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5365 5366 5367
        gen_pusha(s);
        break;
    case 0x61: /* popa */
B
bellard 已提交
5368 5369
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5370 5371 5372 5373
        gen_popa(s);
        break;
    case 0x68: /* push Iv */
    case 0x6a:
B
bellard 已提交
5374
        if (CODE64(s)) {
5375
            ot = dflag ? MO_64 : MO_16;
B
bellard 已提交
5376
        } else {
5377
            ot = dflag + MO_16;
B
bellard 已提交
5378
        }
B
bellard 已提交
5379
        if (b == 0x68)
5380
            val = insn_get(env, s, ot);
B
bellard 已提交
5381
        else
5382
            val = (int8_t)insn_get(env, s, MO_8);
B
bellard 已提交
5383 5384 5385 5386
        gen_op_movl_T0_im(val);
        gen_push_T0(s);
        break;
    case 0x8f: /* pop Ev */
B
bellard 已提交
5387
        if (CODE64(s)) {
5388
            ot = dflag ? MO_64 : MO_16;
B
bellard 已提交
5389
        } else {
5390
            ot = dflag + MO_16;
B
bellard 已提交
5391
        }
5392
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5393
        mod = (modrm >> 6) & 3;
B
bellard 已提交
5394
        gen_pop_T0(s);
B
bellard 已提交
5395 5396 5397
        if (mod == 3) {
            /* NOTE: order is important for pop %sp */
            gen_pop_update(s);
B
bellard 已提交
5398
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
5399
            gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
5400 5401
        } else {
            /* NOTE: order is important too for MMU exceptions */
B
bellard 已提交
5402
            s->popl_esp_hack = 1 << ot;
5403
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
5404 5405 5406
            s->popl_esp_hack = 0;
            gen_pop_update(s);
        }
B
bellard 已提交
5407 5408 5409 5410
        break;
    case 0xc8: /* enter */
        {
            int level;
5411
            val = cpu_lduw_code(env, s->pc);
B
bellard 已提交
5412
            s->pc += 2;
5413
            level = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5414 5415 5416 5417 5418
            gen_enter(s, val, level);
        }
        break;
    case 0xc9: /* leave */
        /* XXX: exception not precise (ESP is updated before potential exception) */
B
bellard 已提交
5419
        if (CODE64(s)) {
5420 5421
            gen_op_mov_TN_reg(MO_64, 0, R_EBP);
            gen_op_mov_reg_T0(MO_64, R_ESP);
B
bellard 已提交
5422
        } else if (s->ss32) {
5423 5424
            gen_op_mov_TN_reg(MO_32, 0, R_EBP);
            gen_op_mov_reg_T0(MO_32, R_ESP);
B
bellard 已提交
5425
        } else {
5426 5427
            gen_op_mov_TN_reg(MO_16, 0, R_EBP);
            gen_op_mov_reg_T0(MO_16, R_ESP);
B
bellard 已提交
5428 5429
        }
        gen_pop_T0(s);
B
bellard 已提交
5430
        if (CODE64(s)) {
5431
            ot = dflag ? MO_64 : MO_16;
B
bellard 已提交
5432
        } else {
5433
            ot = dflag + MO_16;
B
bellard 已提交
5434
        }
B
bellard 已提交
5435
        gen_op_mov_reg_T0(ot, R_EBP);
B
bellard 已提交
5436 5437 5438 5439 5440 5441
        gen_pop_update(s);
        break;
    case 0x06: /* push es */
    case 0x0e: /* push cs */
    case 0x16: /* push ss */
    case 0x1e: /* push ds */
B
bellard 已提交
5442 5443
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454
        gen_op_movl_T0_seg(b >> 3);
        gen_push_T0(s);
        break;
    case 0x1a0: /* push fs */
    case 0x1a8: /* push gs */
        gen_op_movl_T0_seg((b >> 3) & 7);
        gen_push_T0(s);
        break;
    case 0x07: /* pop es */
    case 0x17: /* pop ss */
    case 0x1f: /* pop ds */
B
bellard 已提交
5455 5456
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5457 5458 5459 5460 5461
        reg = b >> 3;
        gen_pop_T0(s);
        gen_movl_seg_T0(s, reg, pc_start - s->cs_base);
        gen_pop_update(s);
        if (reg == R_SS) {
5462 5463 5464 5465
            /* if reg == SS, inhibit interrupts/trace. */
            /* If several instructions disable interrupts, only the
               _first_ does it */
            if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
5466
                gen_helper_set_inhibit_irq(cpu_env);
B
bellard 已提交
5467 5468 5469
            s->tf = 0;
        }
        if (s->is_jmp) {
B
bellard 已提交
5470
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5471 5472 5473 5474 5475 5476 5477 5478 5479
            gen_eob(s);
        }
        break;
    case 0x1a1: /* pop fs */
    case 0x1a9: /* pop gs */
        gen_pop_T0(s);
        gen_movl_seg_T0(s, (b >> 3) & 7, pc_start - s->cs_base);
        gen_pop_update(s);
        if (s->is_jmp) {
B
bellard 已提交
5480
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5481 5482 5483 5484 5485 5486 5487 5488 5489
            gen_eob(s);
        }
        break;

        /**************************/
        /* mov */
    case 0x88:
    case 0x89: /* mov Gv, Ev */
        if ((b & 1) == 0)
5490
            ot = MO_8;
B
bellard 已提交
5491
        else
5492
            ot = dflag + MO_16;
5493
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5494
        reg = ((modrm >> 3) & 7) | rex_r;
5495

B
bellard 已提交
5496
        /* generate a generic store */
5497
        gen_ldst_modrm(env, s, modrm, ot, reg, 1);
B
bellard 已提交
5498 5499 5500 5501
        break;
    case 0xc6:
    case 0xc7: /* mov Ev, Iv */
        if ((b & 1) == 0)
5502
            ot = MO_8;
B
bellard 已提交
5503
        else
5504
            ot = dflag + MO_16;
5505
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5506
        mod = (modrm >> 6) & 3;
B
bellard 已提交
5507 5508
        if (mod != 3) {
            s->rip_offset = insn_const_size(ot);
5509
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
5510
        }
5511
        val = insn_get(env, s, ot);
B
bellard 已提交
5512
        gen_op_movl_T0_im(val);
5513 5514 5515
        if (mod != 3) {
            gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
        } else {
B
bellard 已提交
5516
            gen_op_mov_reg_T0(ot, (modrm & 7) | REX_B(s));
5517
        }
B
bellard 已提交
5518 5519 5520 5521
        break;
    case 0x8a:
    case 0x8b: /* mov Ev, Gv */
        if ((b & 1) == 0)
5522
            ot = MO_8;
B
bellard 已提交
5523
        else
5524
            ot = MO_16 + dflag;
5525
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5526
        reg = ((modrm >> 3) & 7) | rex_r;
5527

5528
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
bellard 已提交
5529
        gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
5530 5531
        break;
    case 0x8e: /* mov seg, Gv */
5532
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5533 5534 5535
        reg = (modrm >> 3) & 7;
        if (reg >= 6 || reg == R_CS)
            goto illegal_op;
5536
        gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
B
bellard 已提交
5537 5538 5539
        gen_movl_seg_T0(s, reg, pc_start - s->cs_base);
        if (reg == R_SS) {
            /* if reg == SS, inhibit interrupts/trace */
5540 5541 5542
            /* If several instructions disable interrupts, only the
               _first_ does it */
            if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
5543
                gen_helper_set_inhibit_irq(cpu_env);
B
bellard 已提交
5544 5545 5546
            s->tf = 0;
        }
        if (s->is_jmp) {
B
bellard 已提交
5547
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5548 5549 5550 5551
            gen_eob(s);
        }
        break;
    case 0x8c: /* mov Gv, seg */
5552
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5553 5554 5555 5556 5557
        reg = (modrm >> 3) & 7;
        mod = (modrm >> 6) & 3;
        if (reg >= 6)
            goto illegal_op;
        gen_op_movl_T0_seg(reg);
B
bellard 已提交
5558
        if (mod == 3)
5559
            ot = MO_16 + dflag;
B
bellard 已提交
5560
        else
5561
            ot = MO_16;
5562
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
5563 5564 5565 5566 5567 5568 5569
        break;

    case 0x1b6: /* movzbS Gv, Eb */
    case 0x1b7: /* movzwS Gv, Eb */
    case 0x1be: /* movsbS Gv, Eb */
    case 0x1bf: /* movswS Gv, Eb */
        {
5570 5571 5572
            TCGMemOp d_ot;
            TCGMemOp s_ot;

B
bellard 已提交
5573
            /* d_ot is the size of destination */
5574
            d_ot = dflag + MO_16;
B
bellard 已提交
5575
            /* ot is the size of source */
5576
            ot = (b & 1) + MO_8;
5577 5578 5579
            /* s_ot is the sign+size of source */
            s_ot = b & 8 ? MO_SIGN | ot : ot;

5580
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5581
            reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5582
            mod = (modrm >> 6) & 3;
B
bellard 已提交
5583
            rm = (modrm & 7) | REX_B(s);
5584

B
bellard 已提交
5585
            if (mod == 3) {
B
bellard 已提交
5586
                gen_op_mov_TN_reg(ot, 0, rm);
5587 5588
                switch (s_ot) {
                case MO_UB:
B
bellard 已提交
5589
                    tcg_gen_ext8u_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5590
                    break;
5591
                case MO_SB:
B
bellard 已提交
5592
                    tcg_gen_ext8s_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5593
                    break;
5594
                case MO_UW:
B
bellard 已提交
5595
                    tcg_gen_ext16u_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5596 5597
                    break;
                default:
5598
                case MO_SW:
B
bellard 已提交
5599
                    tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5600 5601
                    break;
                }
B
bellard 已提交
5602
                gen_op_mov_reg_T0(d_ot, reg);
B
bellard 已提交
5603
            } else {
5604
                gen_lea_modrm(env, s, modrm);
5605
                gen_op_ld_v(s, s_ot, cpu_T[0], cpu_A0);
B
bellard 已提交
5606
                gen_op_mov_reg_T0(d_ot, reg);
B
bellard 已提交
5607 5608 5609 5610 5611
            }
        }
        break;

    case 0x8d: /* lea */
5612
        ot = dflag + MO_16;
5613
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5614 5615 5616
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
B
bellard 已提交
5617
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5618 5619 5620 5621
        /* we must ensure that no segment is added */
        s->override = -1;
        val = s->addseg;
        s->addseg = 0;
5622
        gen_lea_modrm(env, s, modrm);
B
bellard 已提交
5623
        s->addseg = val;
5624
        gen_op_mov_reg_A0(ot - MO_16, reg);
B
bellard 已提交
5625
        break;
5626

B
bellard 已提交
5627 5628 5629 5630 5631
    case 0xa0: /* mov EAX, Ov */
    case 0xa1:
    case 0xa2: /* mov Ov, EAX */
    case 0xa3:
        {
B
bellard 已提交
5632 5633 5634
            target_ulong offset_addr;

            if ((b & 1) == 0)
5635
                ot = MO_8;
B
bellard 已提交
5636
            else
5637
                ot = dflag + MO_16;
B
bellard 已提交
5638
#ifdef TARGET_X86_64
5639
            if (s->aflag == 2) {
5640
                offset_addr = cpu_ldq_code(env, s->pc);
B
bellard 已提交
5641
                s->pc += 8;
B
bellard 已提交
5642
                gen_op_movq_A0_im(offset_addr);
5643
            } else
B
bellard 已提交
5644 5645 5646
#endif
            {
                if (s->aflag) {
5647
                    offset_addr = insn_get(env, s, MO_32);
B
bellard 已提交
5648
                } else {
5649
                    offset_addr = insn_get(env, s, MO_16);
B
bellard 已提交
5650 5651 5652
                }
                gen_op_movl_A0_im(offset_addr);
            }
B
bellard 已提交
5653
            gen_add_A0_ds_seg(s);
B
bellard 已提交
5654
            if ((b & 2) == 0) {
5655
                gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
5656
                gen_op_mov_reg_T0(ot, R_EAX);
B
bellard 已提交
5657
            } else {
B
bellard 已提交
5658
                gen_op_mov_TN_reg(ot, 0, R_EAX);
5659
                gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
5660 5661 5662 5663
            }
        }
        break;
    case 0xd7: /* xlat */
B
bellard 已提交
5664
#ifdef TARGET_X86_64
5665
        if (s->aflag == 2) {
B
bellard 已提交
5666
            gen_op_movq_A0_reg(R_EBX);
5667
            gen_op_mov_TN_reg(MO_64, 0, R_EAX);
5668 5669
            tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xff);
            tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_T[0]);
5670
        } else
B
bellard 已提交
5671 5672
#endif
        {
B
bellard 已提交
5673
            gen_op_movl_A0_reg(R_EBX);
5674
            gen_op_mov_TN_reg(MO_32, 0, R_EAX);
5675 5676
            tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xff);
            tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_T[0]);
B
bellard 已提交
5677 5678
            if (s->aflag == 0)
                gen_op_andl_A0_ffff();
5679 5680
            else
                tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffffffff);
B
bellard 已提交
5681
        }
B
bellard 已提交
5682
        gen_add_A0_ds_seg(s);
5683
        gen_op_ld_v(s, MO_8, cpu_T[0], cpu_A0);
5684
        gen_op_mov_reg_T0(MO_8, R_EAX);
B
bellard 已提交
5685 5686
        break;
    case 0xb0 ... 0xb7: /* mov R, Ib */
5687
        val = insn_get(env, s, MO_8);
B
bellard 已提交
5688
        gen_op_movl_T0_im(val);
5689
        gen_op_mov_reg_T0(MO_8, (b & 7) | REX_B(s));
B
bellard 已提交
5690 5691
        break;
    case 0xb8 ... 0xbf: /* mov R, Iv */
B
bellard 已提交
5692 5693 5694 5695
#ifdef TARGET_X86_64
        if (dflag == 2) {
            uint64_t tmp;
            /* 64 bit case */
5696
            tmp = cpu_ldq_code(env, s->pc);
B
bellard 已提交
5697 5698 5699
            s->pc += 8;
            reg = (b & 7) | REX_B(s);
            gen_movtl_T0_im(tmp);
5700
            gen_op_mov_reg_T0(MO_64, reg);
5701
        } else
B
bellard 已提交
5702 5703
#endif
        {
5704
            ot = dflag ? MO_32 : MO_16;
5705
            val = insn_get(env, s, ot);
B
bellard 已提交
5706 5707
            reg = (b & 7) | REX_B(s);
            gen_op_movl_T0_im(val);
B
bellard 已提交
5708
            gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
5709
        }
B
bellard 已提交
5710 5711 5712
        break;

    case 0x91 ... 0x97: /* xchg R, EAX */
R
Richard Henderson 已提交
5713
    do_xchg_reg_eax:
5714
        ot = dflag + MO_16;
B
bellard 已提交
5715
        reg = (b & 7) | REX_B(s);
B
bellard 已提交
5716 5717 5718 5719 5720
        rm = R_EAX;
        goto do_xchg_reg;
    case 0x86:
    case 0x87: /* xchg Ev, Gv */
        if ((b & 1) == 0)
5721
            ot = MO_8;
B
bellard 已提交
5722
        else
5723
            ot = dflag + MO_16;
5724
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5725
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5726 5727
        mod = (modrm >> 6) & 3;
        if (mod == 3) {
B
bellard 已提交
5728
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
5729
        do_xchg_reg:
B
bellard 已提交
5730 5731 5732 5733
            gen_op_mov_TN_reg(ot, 0, reg);
            gen_op_mov_TN_reg(ot, 1, rm);
            gen_op_mov_reg_T0(ot, rm);
            gen_op_mov_reg_T1(ot, reg);
B
bellard 已提交
5734
        } else {
5735
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
5736
            gen_op_mov_TN_reg(ot, 0, reg);
B
bellard 已提交
5737 5738
            /* for xchg, lock is implicit */
            if (!(prefixes & PREFIX_LOCK))
P
pbrook 已提交
5739
                gen_helper_lock();
5740
            gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
5741
            gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
5742
            if (!(prefixes & PREFIX_LOCK))
P
pbrook 已提交
5743
                gen_helper_unlock();
B
bellard 已提交
5744
            gen_op_mov_reg_T1(ot, reg);
B
bellard 已提交
5745 5746 5747
        }
        break;
    case 0xc4: /* les Gv */
5748
        /* In CODE64 this is VEX3; see above.  */
B
bellard 已提交
5749 5750 5751
        op = R_ES;
        goto do_lxx;
    case 0xc5: /* lds Gv */
5752
        /* In CODE64 this is VEX2; see above.  */
B
bellard 已提交
5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763
        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:
5764
        ot = dflag ? MO_32 : MO_16;
5765
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5766
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5767 5768 5769
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
5770
        gen_lea_modrm(env, s, modrm);
5771
        gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
5772
        gen_add_A0_im(s, 1 << (ot - MO_16 + 1));
B
bellard 已提交
5773
        /* load the segment first to handle exceptions properly */
5774
        gen_op_ld_v(s, MO_16, cpu_T[0], cpu_A0);
B
bellard 已提交
5775 5776
        gen_movl_seg_T0(s, op, pc_start - s->cs_base);
        /* then put the data */
B
bellard 已提交
5777
        gen_op_mov_reg_T1(ot, reg);
B
bellard 已提交
5778
        if (s->is_jmp) {
B
bellard 已提交
5779
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5780 5781 5782
            gen_eob(s);
        }
        break;
5783

B
bellard 已提交
5784 5785 5786 5787 5788 5789 5790 5791 5792
        /************************/
        /* shifts */
    case 0xc0:
    case 0xc1:
        /* shift Ev,Ib */
        shift = 2;
    grp2:
        {
            if ((b & 1) == 0)
5793
                ot = MO_8;
B
bellard 已提交
5794
            else
5795
                ot = dflag + MO_16;
5796

5797
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5798 5799
            mod = (modrm >> 6) & 3;
            op = (modrm >> 3) & 7;
5800

B
bellard 已提交
5801
            if (mod != 3) {
B
bellard 已提交
5802 5803 5804
                if (shift == 2) {
                    s->rip_offset = 1;
                }
5805
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
5806 5807
                opreg = OR_TMP0;
            } else {
B
bellard 已提交
5808
                opreg = (modrm & 7) | REX_B(s);
B
bellard 已提交
5809 5810 5811 5812 5813 5814 5815
            }

            /* simpler op */
            if (shift == 0) {
                gen_shift(s, op, ot, opreg, OR_ECX);
            } else {
                if (shift == 2) {
5816
                    shift = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848
                }
                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:
5849
        ot = dflag + MO_16;
5850
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5851
        mod = (modrm >> 6) & 3;
B
bellard 已提交
5852 5853
        rm = (modrm & 7) | REX_B(s);
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5854
        if (mod != 3) {
5855
            gen_lea_modrm(env, s, modrm);
5856
            opreg = OR_TMP0;
B
bellard 已提交
5857
        } else {
5858
            opreg = rm;
B
bellard 已提交
5859
        }
B
bellard 已提交
5860
        gen_op_mov_TN_reg(ot, 1, reg);
5861

B
bellard 已提交
5862
        if (shift) {
P
Paolo Bonzini 已提交
5863 5864 5865
            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 已提交
5866
        } else {
P
Paolo Bonzini 已提交
5867
            gen_shiftd_rm_T1(s, ot, opreg, op, cpu_regs[R_ECX]);
B
bellard 已提交
5868 5869 5870 5871 5872
        }
        break;

        /************************/
        /* floats */
5873
    case 0xd8 ... 0xdf:
B
bellard 已提交
5874 5875 5876 5877 5878 5879
        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;
        }
5880
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5881 5882 5883 5884 5885
        mod = (modrm >> 6) & 3;
        rm = modrm & 7;
        op = ((b & 7) << 3) | ((modrm >> 3) & 7);
        if (mod != 3) {
            /* memory op */
5886
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897
            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:
5898 5899
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
Blue Swirl 已提交
5900
                        gen_helper_flds_FT0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5901 5902
                        break;
                    case 1:
5903 5904
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
Blue Swirl 已提交
5905
                        gen_helper_fildl_FT0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5906 5907
                        break;
                    case 2:
5908 5909
                        tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0,
                                            s->mem_index, MO_LEQ);
B
Blue Swirl 已提交
5910
                        gen_helper_fldl_FT0(cpu_env, cpu_tmp1_i64);
B
bellard 已提交
5911 5912 5913
                        break;
                    case 3:
                    default:
5914 5915
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LESW);
B
Blue Swirl 已提交
5916
                        gen_helper_fildl_FT0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5917 5918
                        break;
                    }
5919

P
pbrook 已提交
5920
                    gen_helper_fp_arith_ST0_FT0(op1);
B
bellard 已提交
5921 5922
                    if (op1 == 3) {
                        /* fcomp needs pop */
B
Blue Swirl 已提交
5923
                        gen_helper_fpop(cpu_env);
B
bellard 已提交
5924 5925 5926 5927 5928 5929
                    }
                }
                break;
            case 0x08: /* flds */
            case 0x0a: /* fsts */
            case 0x0b: /* fstps */
B
bellard 已提交
5930 5931 5932
            case 0x18 ... 0x1b: /* fildl, fisttpl, fistl, fistpl */
            case 0x28 ... 0x2b: /* fldl, fisttpll, fstl, fstpl */
            case 0x38 ... 0x3b: /* filds, fisttps, fists, fistps */
B
bellard 已提交
5933 5934 5935 5936
                switch(op & 7) {
                case 0:
                    switch(op >> 4) {
                    case 0:
5937 5938
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
Blue Swirl 已提交
5939
                        gen_helper_flds_ST0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5940 5941
                        break;
                    case 1:
5942 5943
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
Blue Swirl 已提交
5944
                        gen_helper_fildl_ST0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5945 5946
                        break;
                    case 2:
5947 5948
                        tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0,
                                            s->mem_index, MO_LEQ);
B
Blue Swirl 已提交
5949
                        gen_helper_fldl_ST0(cpu_env, cpu_tmp1_i64);
B
bellard 已提交
5950 5951 5952
                        break;
                    case 3:
                    default:
5953 5954
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LESW);
B
Blue Swirl 已提交
5955
                        gen_helper_fildl_ST0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5956 5957 5958
                        break;
                    }
                    break;
B
bellard 已提交
5959
                case 1:
B
bellard 已提交
5960
                    /* XXX: the corresponding CPUID bit must be tested ! */
B
bellard 已提交
5961 5962
                    switch(op >> 4) {
                    case 1:
B
Blue Swirl 已提交
5963
                        gen_helper_fisttl_ST0(cpu_tmp2_i32, cpu_env);
5964 5965
                        tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
bellard 已提交
5966 5967
                        break;
                    case 2:
B
Blue Swirl 已提交
5968
                        gen_helper_fisttll_ST0(cpu_tmp1_i64, cpu_env);
5969 5970
                        tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0,
                                            s->mem_index, MO_LEQ);
B
bellard 已提交
5971 5972 5973
                        break;
                    case 3:
                    default:
B
Blue Swirl 已提交
5974
                        gen_helper_fistt_ST0(cpu_tmp2_i32, cpu_env);
5975 5976
                        tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUW);
B
bellard 已提交
5977
                        break;
B
bellard 已提交
5978
                    }
B
Blue Swirl 已提交
5979
                    gen_helper_fpop(cpu_env);
B
bellard 已提交
5980
                    break;
B
bellard 已提交
5981 5982 5983
                default:
                    switch(op >> 4) {
                    case 0:
B
Blue Swirl 已提交
5984
                        gen_helper_fsts_ST0(cpu_tmp2_i32, cpu_env);
5985 5986
                        tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
bellard 已提交
5987 5988
                        break;
                    case 1:
B
Blue Swirl 已提交
5989
                        gen_helper_fistl_ST0(cpu_tmp2_i32, cpu_env);
5990 5991
                        tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
bellard 已提交
5992 5993
                        break;
                    case 2:
B
Blue Swirl 已提交
5994
                        gen_helper_fstl_ST0(cpu_tmp1_i64, cpu_env);
5995 5996
                        tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0,
                                            s->mem_index, MO_LEQ);
B
bellard 已提交
5997 5998 5999
                        break;
                    case 3:
                    default:
B
Blue Swirl 已提交
6000
                        gen_helper_fist_ST0(cpu_tmp2_i32, cpu_env);
6001 6002
                        tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUW);
B
bellard 已提交
6003 6004 6005
                        break;
                    }
                    if ((op & 7) == 3)
B
Blue Swirl 已提交
6006
                        gen_helper_fpop(cpu_env);
B
bellard 已提交
6007 6008 6009 6010
                    break;
                }
                break;
            case 0x0c: /* fldenv mem */
6011
                gen_update_cc_op(s);
B
bellard 已提交
6012
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6013
                gen_helper_fldenv(cpu_env, cpu_A0, tcg_const_i32(s->dflag));
B
bellard 已提交
6014 6015
                break;
            case 0x0d: /* fldcw mem */
6016 6017
                tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                    s->mem_index, MO_LEUW);
B
Blue Swirl 已提交
6018
                gen_helper_fldcw(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
6019 6020
                break;
            case 0x0e: /* fnstenv mem */
6021
                gen_update_cc_op(s);
B
bellard 已提交
6022
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6023
                gen_helper_fstenv(cpu_env, cpu_A0, tcg_const_i32(s->dflag));
B
bellard 已提交
6024 6025
                break;
            case 0x0f: /* fnstcw mem */
B
Blue Swirl 已提交
6026
                gen_helper_fnstcw(cpu_tmp2_i32, cpu_env);
6027 6028
                tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                    s->mem_index, MO_LEUW);
B
bellard 已提交
6029 6030
                break;
            case 0x1d: /* fldt mem */
6031
                gen_update_cc_op(s);
B
bellard 已提交
6032
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6033
                gen_helper_fldt_ST0(cpu_env, cpu_A0);
B
bellard 已提交
6034 6035
                break;
            case 0x1f: /* fstpt mem */
6036
                gen_update_cc_op(s);
B
bellard 已提交
6037
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6038 6039
                gen_helper_fstt_ST0(cpu_env, cpu_A0);
                gen_helper_fpop(cpu_env);
B
bellard 已提交
6040 6041
                break;
            case 0x2c: /* frstor mem */
6042
                gen_update_cc_op(s);
B
bellard 已提交
6043
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6044
                gen_helper_frstor(cpu_env, cpu_A0, tcg_const_i32(s->dflag));
B
bellard 已提交
6045 6046
                break;
            case 0x2e: /* fnsave mem */
6047
                gen_update_cc_op(s);
B
bellard 已提交
6048
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6049
                gen_helper_fsave(cpu_env, cpu_A0, tcg_const_i32(s->dflag));
B
bellard 已提交
6050 6051
                break;
            case 0x2f: /* fnstsw mem */
B
Blue Swirl 已提交
6052
                gen_helper_fnstsw(cpu_tmp2_i32, cpu_env);
6053 6054
                tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                    s->mem_index, MO_LEUW);
B
bellard 已提交
6055 6056
                break;
            case 0x3c: /* fbld */
6057
                gen_update_cc_op(s);
B
bellard 已提交
6058
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6059
                gen_helper_fbld_ST0(cpu_env, cpu_A0);
B
bellard 已提交
6060 6061
                break;
            case 0x3e: /* fbstp */
6062
                gen_update_cc_op(s);
B
bellard 已提交
6063
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6064 6065
                gen_helper_fbst_ST0(cpu_env, cpu_A0);
                gen_helper_fpop(cpu_env);
B
bellard 已提交
6066 6067
                break;
            case 0x3d: /* fildll */
6068
                tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
B
Blue Swirl 已提交
6069
                gen_helper_fildll_ST0(cpu_env, cpu_tmp1_i64);
B
bellard 已提交
6070 6071
                break;
            case 0x3f: /* fistpll */
B
Blue Swirl 已提交
6072
                gen_helper_fistll_ST0(cpu_tmp1_i64, cpu_env);
6073
                tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
B
Blue Swirl 已提交
6074
                gen_helper_fpop(cpu_env);
B
bellard 已提交
6075 6076 6077 6078 6079 6080 6081 6082 6083 6084
                break;
            default:
                goto illegal_op;
            }
        } else {
            /* register float ops */
            opreg = rm;

            switch(op) {
            case 0x08: /* fld sti */
B
Blue Swirl 已提交
6085 6086 6087
                gen_helper_fpush(cpu_env);
                gen_helper_fmov_ST0_STN(cpu_env,
                                        tcg_const_i32((opreg + 1) & 7));
B
bellard 已提交
6088 6089
                break;
            case 0x09: /* fxchg sti */
B
bellard 已提交
6090 6091
            case 0x29: /* fxchg4 sti, undocumented op */
            case 0x39: /* fxchg7 sti, undocumented op */
B
Blue Swirl 已提交
6092
                gen_helper_fxchg_ST0_STN(cpu_env, tcg_const_i32(opreg));
B
bellard 已提交
6093 6094 6095 6096
                break;
            case 0x0a: /* grp d9/2 */
                switch(rm) {
                case 0: /* fnop */
6097
                    /* check exceptions (FreeBSD FPU probe) */
6098
                    gen_update_cc_op(s);
B
bellard 已提交
6099
                    gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6100
                    gen_helper_fwait(cpu_env);
B
bellard 已提交
6101 6102 6103 6104 6105 6106 6107 6108
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x0c: /* grp d9/4 */
                switch(rm) {
                case 0: /* fchs */
B
Blue Swirl 已提交
6109
                    gen_helper_fchs_ST0(cpu_env);
B
bellard 已提交
6110 6111
                    break;
                case 1: /* fabs */
B
Blue Swirl 已提交
6112
                    gen_helper_fabs_ST0(cpu_env);
B
bellard 已提交
6113 6114
                    break;
                case 4: /* ftst */
B
Blue Swirl 已提交
6115 6116
                    gen_helper_fldz_FT0(cpu_env);
                    gen_helper_fcom_ST0_FT0(cpu_env);
B
bellard 已提交
6117 6118
                    break;
                case 5: /* fxam */
B
Blue Swirl 已提交
6119
                    gen_helper_fxam_ST0(cpu_env);
B
bellard 已提交
6120 6121 6122 6123 6124 6125 6126 6127 6128
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x0d: /* grp d9/5 */
                {
                    switch(rm) {
                    case 0:
B
Blue Swirl 已提交
6129 6130
                        gen_helper_fpush(cpu_env);
                        gen_helper_fld1_ST0(cpu_env);
B
bellard 已提交
6131 6132
                        break;
                    case 1:
B
Blue Swirl 已提交
6133 6134
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldl2t_ST0(cpu_env);
B
bellard 已提交
6135 6136
                        break;
                    case 2:
B
Blue Swirl 已提交
6137 6138
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldl2e_ST0(cpu_env);
B
bellard 已提交
6139 6140
                        break;
                    case 3:
B
Blue Swirl 已提交
6141 6142
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldpi_ST0(cpu_env);
B
bellard 已提交
6143 6144
                        break;
                    case 4:
B
Blue Swirl 已提交
6145 6146
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldlg2_ST0(cpu_env);
B
bellard 已提交
6147 6148
                        break;
                    case 5:
B
Blue Swirl 已提交
6149 6150
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldln2_ST0(cpu_env);
B
bellard 已提交
6151 6152
                        break;
                    case 6:
B
Blue Swirl 已提交
6153 6154
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldz_ST0(cpu_env);
B
bellard 已提交
6155 6156 6157 6158 6159 6160 6161 6162 6163
                        break;
                    default:
                        goto illegal_op;
                    }
                }
                break;
            case 0x0e: /* grp d9/6 */
                switch(rm) {
                case 0: /* f2xm1 */
B
Blue Swirl 已提交
6164
                    gen_helper_f2xm1(cpu_env);
B
bellard 已提交
6165 6166
                    break;
                case 1: /* fyl2x */
B
Blue Swirl 已提交
6167
                    gen_helper_fyl2x(cpu_env);
B
bellard 已提交
6168 6169
                    break;
                case 2: /* fptan */
B
Blue Swirl 已提交
6170
                    gen_helper_fptan(cpu_env);
B
bellard 已提交
6171 6172
                    break;
                case 3: /* fpatan */
B
Blue Swirl 已提交
6173
                    gen_helper_fpatan(cpu_env);
B
bellard 已提交
6174 6175
                    break;
                case 4: /* fxtract */
B
Blue Swirl 已提交
6176
                    gen_helper_fxtract(cpu_env);
B
bellard 已提交
6177 6178
                    break;
                case 5: /* fprem1 */
B
Blue Swirl 已提交
6179
                    gen_helper_fprem1(cpu_env);
B
bellard 已提交
6180 6181
                    break;
                case 6: /* fdecstp */
B
Blue Swirl 已提交
6182
                    gen_helper_fdecstp(cpu_env);
B
bellard 已提交
6183 6184 6185
                    break;
                default:
                case 7: /* fincstp */
B
Blue Swirl 已提交
6186
                    gen_helper_fincstp(cpu_env);
B
bellard 已提交
6187 6188 6189 6190 6191 6192
                    break;
                }
                break;
            case 0x0f: /* grp d9/7 */
                switch(rm) {
                case 0: /* fprem */
B
Blue Swirl 已提交
6193
                    gen_helper_fprem(cpu_env);
B
bellard 已提交
6194 6195
                    break;
                case 1: /* fyl2xp1 */
B
Blue Swirl 已提交
6196
                    gen_helper_fyl2xp1(cpu_env);
B
bellard 已提交
6197 6198
                    break;
                case 2: /* fsqrt */
B
Blue Swirl 已提交
6199
                    gen_helper_fsqrt(cpu_env);
B
bellard 已提交
6200 6201
                    break;
                case 3: /* fsincos */
B
Blue Swirl 已提交
6202
                    gen_helper_fsincos(cpu_env);
B
bellard 已提交
6203 6204
                    break;
                case 5: /* fscale */
B
Blue Swirl 已提交
6205
                    gen_helper_fscale(cpu_env);
B
bellard 已提交
6206 6207
                    break;
                case 4: /* frndint */
B
Blue Swirl 已提交
6208
                    gen_helper_frndint(cpu_env);
B
bellard 已提交
6209 6210
                    break;
                case 6: /* fsin */
B
Blue Swirl 已提交
6211
                    gen_helper_fsin(cpu_env);
B
bellard 已提交
6212 6213 6214
                    break;
                default:
                case 7: /* fcos */
B
Blue Swirl 已提交
6215
                    gen_helper_fcos(cpu_env);
B
bellard 已提交
6216 6217 6218 6219 6220 6221 6222 6223
                    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;
6224

B
bellard 已提交
6225 6226
                    op1 = op & 7;
                    if (op >= 0x20) {
P
pbrook 已提交
6227
                        gen_helper_fp_arith_STN_ST0(op1, opreg);
B
bellard 已提交
6228
                        if (op >= 0x30)
B
Blue Swirl 已提交
6229
                            gen_helper_fpop(cpu_env);
B
bellard 已提交
6230
                    } else {
B
Blue Swirl 已提交
6231
                        gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
P
pbrook 已提交
6232
                        gen_helper_fp_arith_ST0_FT0(op1);
B
bellard 已提交
6233 6234 6235 6236
                    }
                }
                break;
            case 0x02: /* fcom */
B
bellard 已提交
6237
            case 0x22: /* fcom2, undocumented op */
B
Blue Swirl 已提交
6238 6239
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fcom_ST0_FT0(cpu_env);
B
bellard 已提交
6240 6241
                break;
            case 0x03: /* fcomp */
B
bellard 已提交
6242 6243
            case 0x23: /* fcomp3, undocumented op */
            case 0x32: /* fcomp5, undocumented op */
B
Blue Swirl 已提交
6244 6245 6246
                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 已提交
6247 6248 6249 6250
                break;
            case 0x15: /* da/5 */
                switch(rm) {
                case 1: /* fucompp */
B
Blue Swirl 已提交
6251 6252 6253 6254
                    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 已提交
6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266
                    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 已提交
6267
                    gen_helper_fclex(cpu_env);
B
bellard 已提交
6268 6269
                    break;
                case 3: /* fninit */
B
Blue Swirl 已提交
6270
                    gen_helper_fninit(cpu_env);
B
bellard 已提交
6271 6272 6273 6274 6275 6276 6277 6278
                    break;
                case 4: /* fsetpm (287 only, just do nop here) */
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x1d: /* fucomi */
6279 6280 6281
                if (!(s->cpuid_features & CPUID_CMOV)) {
                    goto illegal_op;
                }
6282
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6283 6284
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fucomi_ST0_FT0(cpu_env);
6285
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6286 6287
                break;
            case 0x1e: /* fcomi */
6288 6289 6290
                if (!(s->cpuid_features & CPUID_CMOV)) {
                    goto illegal_op;
                }
6291
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6292 6293
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fcomi_ST0_FT0(cpu_env);
6294
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6295
                break;
B
bellard 已提交
6296
            case 0x28: /* ffree sti */
B
Blue Swirl 已提交
6297
                gen_helper_ffree_STN(cpu_env, tcg_const_i32(opreg));
6298
                break;
B
bellard 已提交
6299
            case 0x2a: /* fst sti */
B
Blue Swirl 已提交
6300
                gen_helper_fmov_STN_ST0(cpu_env, tcg_const_i32(opreg));
B
bellard 已提交
6301 6302
                break;
            case 0x2b: /* fstp sti */
B
bellard 已提交
6303 6304 6305
            case 0x0b: /* fstp1 sti, undocumented op */
            case 0x3a: /* fstp8 sti, undocumented op */
            case 0x3b: /* fstp9 sti, undocumented op */
B
Blue Swirl 已提交
6306 6307
                gen_helper_fmov_STN_ST0(cpu_env, tcg_const_i32(opreg));
                gen_helper_fpop(cpu_env);
B
bellard 已提交
6308 6309
                break;
            case 0x2c: /* fucom st(i) */
B
Blue Swirl 已提交
6310 6311
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fucom_ST0_FT0(cpu_env);
B
bellard 已提交
6312 6313
                break;
            case 0x2d: /* fucomp st(i) */
B
Blue Swirl 已提交
6314 6315 6316
                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 已提交
6317 6318 6319 6320
                break;
            case 0x33: /* de/3 */
                switch(rm) {
                case 1: /* fcompp */
B
Blue Swirl 已提交
6321 6322 6323 6324
                    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 已提交
6325 6326 6327 6328 6329
                    break;
                default:
                    goto illegal_op;
                }
                break;
B
bellard 已提交
6330
            case 0x38: /* ffreep sti, undocumented op */
B
Blue Swirl 已提交
6331 6332
                gen_helper_ffree_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fpop(cpu_env);
B
bellard 已提交
6333
                break;
B
bellard 已提交
6334 6335 6336
            case 0x3c: /* df/4 */
                switch(rm) {
                case 0:
B
Blue Swirl 已提交
6337
                    gen_helper_fnstsw(cpu_tmp2_i32, cpu_env);
6338
                    tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
6339
                    gen_op_mov_reg_T0(MO_16, R_EAX);
B
bellard 已提交
6340 6341 6342 6343 6344 6345
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x3d: /* fucomip */
6346 6347 6348
                if (!(s->cpuid_features & CPUID_CMOV)) {
                    goto illegal_op;
                }
6349
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6350 6351 6352
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fucomi_ST0_FT0(cpu_env);
                gen_helper_fpop(cpu_env);
6353
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6354 6355
                break;
            case 0x3e: /* fcomip */
6356 6357 6358
                if (!(s->cpuid_features & CPUID_CMOV)) {
                    goto illegal_op;
                }
6359
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6360 6361 6362
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fcomi_ST0_FT0(cpu_env);
                gen_helper_fpop(cpu_env);
6363
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6364
                break;
6365 6366 6367
            case 0x10 ... 0x13: /* fcmovxx */
            case 0x18 ... 0x1b:
                {
B
bellard 已提交
6368
                    int op1, l1;
6369
                    static const uint8_t fcmov_cc[8] = {
6370 6371 6372 6373 6374
                        (JCC_B << 1),
                        (JCC_Z << 1),
                        (JCC_BE << 1),
                        (JCC_P << 1),
                    };
6375 6376 6377 6378

                    if (!(s->cpuid_features & CPUID_CMOV)) {
                        goto illegal_op;
                    }
6379
                    op1 = fcmov_cc[op & 3] | (((op >> 3) & 1) ^ 1);
B
bellard 已提交
6380
                    l1 = gen_new_label();
6381
                    gen_jcc1_noeob(s, op1, l1);
B
Blue Swirl 已提交
6382
                    gen_helper_fmov_ST0_STN(cpu_env, tcg_const_i32(opreg));
B
bellard 已提交
6383
                    gen_set_label(l1);
6384 6385
                }
                break;
B
bellard 已提交
6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396
            default:
                goto illegal_op;
            }
        }
        break;
        /************************/
        /* string ops */

    case 0xa4: /* movsS */
    case 0xa5:
        if ((b & 1) == 0)
6397
            ot = MO_8;
B
bellard 已提交
6398
        else
6399
            ot = dflag + MO_16;
B
bellard 已提交
6400 6401 6402 6403 6404 6405 6406

        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;
6407

B
bellard 已提交
6408 6409 6410
    case 0xaa: /* stosS */
    case 0xab:
        if ((b & 1) == 0)
6411
            ot = MO_8;
B
bellard 已提交
6412
        else
6413
            ot = dflag + MO_16;
B
bellard 已提交
6414 6415 6416 6417 6418 6419 6420 6421 6422 6423

        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:
        if ((b & 1) == 0)
6424
            ot = MO_8;
B
bellard 已提交
6425
        else
6426
            ot = dflag + MO_16;
B
bellard 已提交
6427 6428 6429 6430 6431 6432 6433 6434 6435
        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:
        if ((b & 1) == 0)
6436
            ot = MO_8;
B
bellard 已提交
6437
        else
6438
            ot = dflag + MO_16;
B
bellard 已提交
6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450
        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:
        if ((b & 1) == 0)
6451
            ot = MO_8;
B
bellard 已提交
6452
        else
6453
            ot = dflag + MO_16;
B
bellard 已提交
6454 6455 6456 6457 6458 6459 6460 6461 6462 6463
        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:
6464
        if ((b & 1) == 0)
6465
            ot = MO_8;
6466
        else
6467 6468
            ot = dflag ? MO_32 : MO_16;
        gen_op_mov_TN_reg(MO_16, 0, R_EDX);
T
ths 已提交
6469
        gen_op_andl_T0_ffff();
6470 6471
        gen_check_io(s, ot, pc_start - s->cs_base, 
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes) | 4);
6472 6473
        if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
            gen_repz_ins(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
B
bellard 已提交
6474
        } else {
6475
            gen_ins(s, ot);
P
pbrook 已提交
6476 6477 6478
            if (use_icount) {
                gen_jmp(s, s->pc - s->cs_base);
            }
B
bellard 已提交
6479 6480 6481 6482
        }
        break;
    case 0x6e: /* outsS */
    case 0x6f:
6483
        if ((b & 1) == 0)
6484
            ot = MO_8;
6485
        else
6486 6487
            ot = dflag ? MO_32 : MO_16;
        gen_op_mov_TN_reg(MO_16, 0, R_EDX);
T
ths 已提交
6488
        gen_op_andl_T0_ffff();
6489 6490
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes) | 4);
6491 6492
        if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
            gen_repz_outs(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
B
bellard 已提交
6493
        } else {
6494
            gen_outs(s, ot);
P
pbrook 已提交
6495 6496 6497
            if (use_icount) {
                gen_jmp(s, s->pc - s->cs_base);
            }
B
bellard 已提交
6498 6499 6500 6501 6502
        }
        break;

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

B
bellard 已提交
6504 6505
    case 0xe4:
    case 0xe5:
6506
        if ((b & 1) == 0)
6507
            ot = MO_8;
6508
        else
6509
            ot = dflag ? MO_32 : MO_16;
6510
        val = cpu_ldub_code(env, s->pc++);
6511
        gen_op_movl_T0_im(val);
6512 6513
        gen_check_io(s, ot, pc_start - s->cs_base,
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
P
pbrook 已提交
6514 6515
        if (use_icount)
            gen_io_start();
6516
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
6517
        gen_helper_in_func(ot, cpu_T[1], cpu_tmp2_i32);
B
bellard 已提交
6518
        gen_op_mov_reg_T1(ot, R_EAX);
P
pbrook 已提交
6519 6520 6521 6522
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6523 6524 6525
        break;
    case 0xe6:
    case 0xe7:
6526
        if ((b & 1) == 0)
6527
            ot = MO_8;
6528
        else
6529
            ot = dflag ? MO_32 : MO_16;
6530
        val = cpu_ldub_code(env, s->pc++);
6531
        gen_op_movl_T0_im(val);
6532 6533
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes));
B
bellard 已提交
6534
        gen_op_mov_TN_reg(ot, 1, R_EAX);
6535

P
pbrook 已提交
6536 6537
        if (use_icount)
            gen_io_start();
6538 6539
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
        tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
P
pbrook 已提交
6540
        gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
P
pbrook 已提交
6541 6542 6543 6544
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6545 6546 6547
        break;
    case 0xec:
    case 0xed:
6548
        if ((b & 1) == 0)
6549
            ot = MO_8;
6550
        else
6551 6552
            ot = dflag ? MO_32 : MO_16;
        gen_op_mov_TN_reg(MO_16, 0, R_EDX);
6553
        gen_op_andl_T0_ffff();
6554 6555
        gen_check_io(s, ot, pc_start - s->cs_base,
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
P
pbrook 已提交
6556 6557
        if (use_icount)
            gen_io_start();
6558
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
6559
        gen_helper_in_func(ot, cpu_T[1], cpu_tmp2_i32);
B
bellard 已提交
6560
        gen_op_mov_reg_T1(ot, R_EAX);
P
pbrook 已提交
6561 6562 6563 6564
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6565 6566 6567
        break;
    case 0xee:
    case 0xef:
6568
        if ((b & 1) == 0)
6569
            ot = MO_8;
6570
        else
6571 6572
            ot = dflag ? MO_32 : MO_16;
        gen_op_mov_TN_reg(MO_16, 0, R_EDX);
6573
        gen_op_andl_T0_ffff();
6574 6575
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes));
B
bellard 已提交
6576
        gen_op_mov_TN_reg(ot, 1, R_EAX);
6577

P
pbrook 已提交
6578 6579
        if (use_icount)
            gen_io_start();
6580 6581
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
        tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
P
pbrook 已提交
6582
        gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
P
pbrook 已提交
6583 6584 6585 6586
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6587 6588 6589 6590 6591
        break;

        /************************/
        /* control */
    case 0xc2: /* ret im */
6592
        val = cpu_ldsw_code(env, s->pc);
B
bellard 已提交
6593 6594
        s->pc += 2;
        gen_pop_T0(s);
6595 6596
        if (CODE64(s) && s->dflag)
            s->dflag = 2;
B
bellard 已提交
6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611
        gen_stack_update(s, val + (2 << s->dflag));
        if (s->dflag == 0)
            gen_op_andl_T0_ffff();
        gen_op_jmp_T0();
        gen_eob(s);
        break;
    case 0xc3: /* ret */
        gen_pop_T0(s);
        gen_pop_update(s);
        if (s->dflag == 0)
            gen_op_andl_T0_ffff();
        gen_op_jmp_T0();
        gen_eob(s);
        break;
    case 0xca: /* lret im */
6612
        val = cpu_ldsw_code(env, s->pc);
B
bellard 已提交
6613 6614 6615
        s->pc += 2;
    do_lret:
        if (s->pe && !s->vm86) {
6616
            gen_update_cc_op(s);
B
bellard 已提交
6617
            gen_jmp_im(pc_start - s->cs_base);
6618
            gen_helper_lret_protected(cpu_env, tcg_const_i32(s->dflag),
P
pbrook 已提交
6619
                                      tcg_const_i32(val));
B
bellard 已提交
6620 6621 6622
        } else {
            gen_stack_A0(s);
            /* pop offset */
6623
            gen_op_ld_v(s, 1 + s->dflag, cpu_T[0], cpu_A0);
B
bellard 已提交
6624 6625 6626 6627 6628 6629 6630
            if (s->dflag == 0)
                gen_op_andl_T0_ffff();
            /* NOTE: keeping EIP updated is not a problem in case of
               exception */
            gen_op_jmp_T0();
            /* pop selector */
            gen_op_addl_A0_im(2 << s->dflag);
6631
            gen_op_ld_v(s, 1 + s->dflag, cpu_T[0], cpu_A0);
6632
            gen_op_movl_seg_T0_vm(R_CS);
B
bellard 已提交
6633 6634 6635 6636 6637 6638 6639 6640 6641
            /* add stack offset */
            gen_stack_update(s, val + (4 << s->dflag));
        }
        gen_eob(s);
        break;
    case 0xcb: /* lret */
        val = 0;
        goto do_lret;
    case 0xcf: /* iret */
B
bellard 已提交
6642
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_IRET);
B
bellard 已提交
6643 6644
        if (!s->pe) {
            /* real mode */
6645
            gen_helper_iret_real(cpu_env, tcg_const_i32(s->dflag));
6646
            set_cc_op(s, CC_OP_EFLAGS);
6647 6648 6649 6650
        } else if (s->vm86) {
            if (s->iopl != 3) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
6651
                gen_helper_iret_real(cpu_env, tcg_const_i32(s->dflag));
6652
                set_cc_op(s, CC_OP_EFLAGS);
6653
            }
B
bellard 已提交
6654
        } else {
6655
            gen_update_cc_op(s);
B
bellard 已提交
6656
            gen_jmp_im(pc_start - s->cs_base);
6657
            gen_helper_iret_protected(cpu_env, tcg_const_i32(s->dflag),
P
pbrook 已提交
6658
                                      tcg_const_i32(s->pc - s->cs_base));
6659
            set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6660 6661 6662 6663 6664
        }
        gen_eob(s);
        break;
    case 0xe8: /* call im */
        {
B
bellard 已提交
6665
            if (dflag)
6666
                tval = (int32_t)insn_get(env, s, MO_32);
B
bellard 已提交
6667
            else
6668
                tval = (int16_t)insn_get(env, s, MO_16);
B
bellard 已提交
6669
            next_eip = s->pc - s->cs_base;
B
bellard 已提交
6670
            tval += next_eip;
B
bellard 已提交
6671
            if (s->dflag == 0)
B
bellard 已提交
6672
                tval &= 0xffff;
6673 6674
            else if(!CODE64(s))
                tval &= 0xffffffff;
B
bellard 已提交
6675
            gen_movtl_T0_im(next_eip);
B
bellard 已提交
6676
            gen_push_T0(s);
B
bellard 已提交
6677
            gen_jmp(s, tval);
B
bellard 已提交
6678 6679 6680 6681 6682
        }
        break;
    case 0x9a: /* lcall im */
        {
            unsigned int selector, offset;
6683

B
bellard 已提交
6684 6685
            if (CODE64(s))
                goto illegal_op;
6686
            ot = dflag ? MO_32 : MO_16;
6687
            offset = insn_get(env, s, ot);
6688
            selector = insn_get(env, s, MO_16);
6689

B
bellard 已提交
6690
            gen_op_movl_T0_im(selector);
B
bellard 已提交
6691
            gen_op_movl_T1_imu(offset);
B
bellard 已提交
6692 6693
        }
        goto do_lcall;
B
bellard 已提交
6694
    case 0xe9: /* jmp im */
B
bellard 已提交
6695
        if (dflag)
6696
            tval = (int32_t)insn_get(env, s, MO_32);
B
bellard 已提交
6697
        else
6698
            tval = (int16_t)insn_get(env, s, MO_16);
B
bellard 已提交
6699
        tval += s->pc - s->cs_base;
B
bellard 已提交
6700
        if (s->dflag == 0)
B
bellard 已提交
6701
            tval &= 0xffff;
6702 6703
        else if(!CODE64(s))
            tval &= 0xffffffff;
B
bellard 已提交
6704
        gen_jmp(s, tval);
B
bellard 已提交
6705 6706 6707 6708 6709
        break;
    case 0xea: /* ljmp im */
        {
            unsigned int selector, offset;

B
bellard 已提交
6710 6711
            if (CODE64(s))
                goto illegal_op;
6712
            ot = dflag ? MO_32 : MO_16;
6713
            offset = insn_get(env, s, ot);
6714
            selector = insn_get(env, s, MO_16);
6715

B
bellard 已提交
6716
            gen_op_movl_T0_im(selector);
B
bellard 已提交
6717
            gen_op_movl_T1_imu(offset);
B
bellard 已提交
6718 6719 6720
        }
        goto do_ljmp;
    case 0xeb: /* jmp Jb */
6721
        tval = (int8_t)insn_get(env, s, MO_8);
B
bellard 已提交
6722
        tval += s->pc - s->cs_base;
B
bellard 已提交
6723
        if (s->dflag == 0)
B
bellard 已提交
6724 6725
            tval &= 0xffff;
        gen_jmp(s, tval);
B
bellard 已提交
6726 6727
        break;
    case 0x70 ... 0x7f: /* jcc Jb */
6728
        tval = (int8_t)insn_get(env, s, MO_8);
B
bellard 已提交
6729 6730 6731
        goto do_jcc;
    case 0x180 ... 0x18f: /* jcc Jv */
        if (dflag) {
6732
            tval = (int32_t)insn_get(env, s, MO_32);
B
bellard 已提交
6733
        } else {
6734
            tval = (int16_t)insn_get(env, s, MO_16);
B
bellard 已提交
6735 6736 6737
        }
    do_jcc:
        next_eip = s->pc - s->cs_base;
B
bellard 已提交
6738
        tval += next_eip;
B
bellard 已提交
6739
        if (s->dflag == 0)
B
bellard 已提交
6740 6741
            tval &= 0xffff;
        gen_jcc(s, b, tval, next_eip);
B
bellard 已提交
6742 6743 6744
        break;

    case 0x190 ... 0x19f: /* setcc Gv */
6745
        modrm = cpu_ldub_code(env, s->pc++);
6746
        gen_setcc1(s, b, cpu_T[0]);
6747
        gen_ldst_modrm(env, s, modrm, MO_8, OR_TMP0, 1);
B
bellard 已提交
6748 6749
        break;
    case 0x140 ... 0x14f: /* cmov Gv, Ev */
6750 6751 6752
        if (!(s->cpuid_features & CPUID_CMOV)) {
            goto illegal_op;
        }
6753
        ot = dflag + MO_16;
6754 6755 6756
        modrm = cpu_ldub_code(env, s->pc++);
        reg = ((modrm >> 3) & 7) | rex_r;
        gen_cmovcc1(env, s, ot, b, modrm, reg);
B
bellard 已提交
6757
        break;
6758

B
bellard 已提交
6759 6760 6761
        /************************/
        /* flags */
    case 0x9c: /* pushf */
B
bellard 已提交
6762
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_PUSHF);
B
bellard 已提交
6763 6764 6765
        if (s->vm86 && s->iopl != 3) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
6766
            gen_update_cc_op(s);
6767
            gen_helper_read_eflags(cpu_T[0], cpu_env);
B
bellard 已提交
6768 6769 6770 6771
            gen_push_T0(s);
        }
        break;
    case 0x9d: /* popf */
B
bellard 已提交
6772
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_POPF);
B
bellard 已提交
6773 6774 6775 6776 6777 6778
        if (s->vm86 && s->iopl != 3) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
            gen_pop_T0(s);
            if (s->cpl == 0) {
                if (s->dflag) {
6779 6780 6781 6782 6783
                    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 已提交
6784
                } else {
6785 6786 6787 6788 6789
                    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 已提交
6790 6791
                }
            } else {
B
bellard 已提交
6792 6793
                if (s->cpl <= s->iopl) {
                    if (s->dflag) {
6794 6795 6796 6797 6798 6799
                        gen_helper_write_eflags(cpu_env, cpu_T[0],
                                                tcg_const_i32((TF_MASK |
                                                               AC_MASK |
                                                               ID_MASK |
                                                               NT_MASK |
                                                               IF_MASK)));
B
bellard 已提交
6800
                    } else {
6801 6802 6803 6804 6805 6806 6807
                        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 已提交
6808
                    }
B
bellard 已提交
6809
                } else {
B
bellard 已提交
6810
                    if (s->dflag) {
6811 6812 6813
                        gen_helper_write_eflags(cpu_env, cpu_T[0],
                                           tcg_const_i32((TF_MASK | AC_MASK |
                                                          ID_MASK | NT_MASK)));
B
bellard 已提交
6814
                    } else {
6815 6816 6817 6818
                        gen_helper_write_eflags(cpu_env, cpu_T[0],
                                           tcg_const_i32((TF_MASK | AC_MASK |
                                                          ID_MASK | NT_MASK)
                                                         & 0xffff));
B
bellard 已提交
6819
                    }
B
bellard 已提交
6820 6821 6822
                }
            }
            gen_pop_update(s);
6823
            set_cc_op(s, CC_OP_EFLAGS);
H
H. Peter Anvin 已提交
6824
            /* abort translation because TF/AC flag may change */
B
bellard 已提交
6825
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
6826 6827 6828 6829
            gen_eob(s);
        }
        break;
    case 0x9e: /* sahf */
B
bellard 已提交
6830
        if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM))
B
bellard 已提交
6831
            goto illegal_op;
6832
        gen_op_mov_TN_reg(MO_8, 0, R_AH);
6833
        gen_compute_eflags(s);
6834 6835 6836
        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 已提交
6837 6838
        break;
    case 0x9f: /* lahf */
B
bellard 已提交
6839
        if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM))
B
bellard 已提交
6840
            goto illegal_op;
6841
        gen_compute_eflags(s);
6842
        /* Note: gen_compute_eflags() only gives the condition codes */
6843
        tcg_gen_ori_tl(cpu_T[0], cpu_cc_src, 0x02);
6844
        gen_op_mov_reg_T0(MO_8, R_AH);
B
bellard 已提交
6845 6846
        break;
    case 0xf5: /* cmc */
6847
        gen_compute_eflags(s);
6848
        tcg_gen_xori_tl(cpu_cc_src, cpu_cc_src, CC_C);
B
bellard 已提交
6849 6850
        break;
    case 0xf8: /* clc */
6851
        gen_compute_eflags(s);
6852
        tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~CC_C);
B
bellard 已提交
6853 6854
        break;
    case 0xf9: /* stc */
6855
        gen_compute_eflags(s);
6856
        tcg_gen_ori_tl(cpu_cc_src, cpu_cc_src, CC_C);
B
bellard 已提交
6857 6858
        break;
    case 0xfc: /* cld */
6859
        tcg_gen_movi_i32(cpu_tmp2_i32, 1);
6860
        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, offsetof(CPUX86State, df));
B
bellard 已提交
6861 6862
        break;
    case 0xfd: /* std */
6863
        tcg_gen_movi_i32(cpu_tmp2_i32, -1);
6864
        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, offsetof(CPUX86State, df));
B
bellard 已提交
6865 6866 6867 6868 6869
        break;

        /************************/
        /* bit operations */
    case 0x1ba: /* bt/bts/btr/btc Gv, im */
6870
        ot = dflag + MO_16;
6871
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
6872
        op = (modrm >> 3) & 7;
B
bellard 已提交
6873
        mod = (modrm >> 6) & 3;
B
bellard 已提交
6874
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
6875
        if (mod != 3) {
B
bellard 已提交
6876
            s->rip_offset = 1;
6877
            gen_lea_modrm(env, s, modrm);
6878
            gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
6879
        } else {
B
bellard 已提交
6880
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
6881 6882
        }
        /* load shift */
6883
        val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
6884 6885 6886 6887
        gen_op_movl_T1_im(val);
        if (op < 4)
            goto illegal_op;
        op -= 4;
B
bellard 已提交
6888
        goto bt_op;
B
bellard 已提交
6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900
    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:
6901
        ot = dflag + MO_16;
6902
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
6903
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
6904
        mod = (modrm >> 6) & 3;
B
bellard 已提交
6905
        rm = (modrm & 7) | REX_B(s);
6906
        gen_op_mov_TN_reg(MO_32, 1, reg);
B
bellard 已提交
6907
        if (mod != 3) {
6908
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
6909
            /* specific case: we need to add a displacement */
B
bellard 已提交
6910 6911 6912 6913
            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);
6914
            gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
6915
        } else {
B
bellard 已提交
6916
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
6917
        }
B
bellard 已提交
6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945
    bt_op:
        tcg_gen_andi_tl(cpu_T[1], cpu_T[1], (1 << (3 + ot)) - 1);
        switch(op) {
        case 0:
            tcg_gen_shr_tl(cpu_cc_src, cpu_T[0], cpu_T[1]);
            tcg_gen_movi_tl(cpu_cc_dst, 0);
            break;
        case 1:
            tcg_gen_shr_tl(cpu_tmp4, cpu_T[0], cpu_T[1]);
            tcg_gen_movi_tl(cpu_tmp0, 1);
            tcg_gen_shl_tl(cpu_tmp0, cpu_tmp0, cpu_T[1]);
            tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
            break;
        case 2:
            tcg_gen_shr_tl(cpu_tmp4, cpu_T[0], cpu_T[1]);
            tcg_gen_movi_tl(cpu_tmp0, 1);
            tcg_gen_shl_tl(cpu_tmp0, cpu_tmp0, cpu_T[1]);
            tcg_gen_not_tl(cpu_tmp0, cpu_tmp0);
            tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
            break;
        default:
        case 3:
            tcg_gen_shr_tl(cpu_tmp4, cpu_T[0], cpu_T[1]);
            tcg_gen_movi_tl(cpu_tmp0, 1);
            tcg_gen_shl_tl(cpu_tmp0, cpu_tmp0, cpu_T[1]);
            tcg_gen_xor_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
            break;
        }
6946
        set_cc_op(s, CC_OP_SARB + ot);
B
bellard 已提交
6947
        if (op != 0) {
6948 6949 6950
            if (mod != 3) {
                gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
            } else {
B
bellard 已提交
6951
                gen_op_mov_reg_T0(ot, rm);
6952
            }
B
bellard 已提交
6953 6954
            tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
            tcg_gen_movi_tl(cpu_cc_dst, 0);
B
bellard 已提交
6955 6956
        }
        break;
6957 6958
    case 0x1bc: /* bsf / tzcnt */
    case 0x1bd: /* bsr / lzcnt */
6959
        ot = dflag + MO_16;
6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976
        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 已提交
6977
            } else {
6978 6979 6980 6981 6982
                /* 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 已提交
6983
            }
6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006
            /* 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 已提交
7007
        }
7008
        gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
7009 7010 7011 7012
        break;
        /************************/
        /* bcd */
    case 0x27: /* daa */
B
bellard 已提交
7013 7014
        if (CODE64(s))
            goto illegal_op;
7015
        gen_update_cc_op(s);
7016
        gen_helper_daa(cpu_env);
7017
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
7018 7019
        break;
    case 0x2f: /* das */
B
bellard 已提交
7020 7021
        if (CODE64(s))
            goto illegal_op;
7022
        gen_update_cc_op(s);
7023
        gen_helper_das(cpu_env);
7024
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
7025 7026
        break;
    case 0x37: /* aaa */
B
bellard 已提交
7027 7028
        if (CODE64(s))
            goto illegal_op;
7029
        gen_update_cc_op(s);
7030
        gen_helper_aaa(cpu_env);
7031
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
7032 7033
        break;
    case 0x3f: /* aas */
B
bellard 已提交
7034 7035
        if (CODE64(s))
            goto illegal_op;
7036
        gen_update_cc_op(s);
7037
        gen_helper_aas(cpu_env);
7038
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
7039 7040
        break;
    case 0xd4: /* aam */
B
bellard 已提交
7041 7042
        if (CODE64(s))
            goto illegal_op;
7043
        val = cpu_ldub_code(env, s->pc++);
7044 7045 7046
        if (val == 0) {
            gen_exception(s, EXCP00_DIVZ, pc_start - s->cs_base);
        } else {
7047
            gen_helper_aam(cpu_env, tcg_const_i32(val));
7048
            set_cc_op(s, CC_OP_LOGICB);
7049
        }
B
bellard 已提交
7050 7051
        break;
    case 0xd5: /* aad */
B
bellard 已提交
7052 7053
        if (CODE64(s))
            goto illegal_op;
7054
        val = cpu_ldub_code(env, s->pc++);
7055
        gen_helper_aad(cpu_env, tcg_const_i32(val));
7056
        set_cc_op(s, CC_OP_LOGICB);
B
bellard 已提交
7057 7058 7059 7060
        break;
        /************************/
        /* misc */
    case 0x90: /* nop */
7061
        /* XXX: correct lock test for all insn */
R
Richard Henderson 已提交
7062
        if (prefixes & PREFIX_LOCK) {
7063
            goto illegal_op;
R
Richard Henderson 已提交
7064 7065 7066 7067 7068
        }
        /* 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 已提交
7069
        if (prefixes & PREFIX_REPZ) {
7070 7071 7072 7073
            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 已提交
7074
        }
B
bellard 已提交
7075 7076
        break;
    case 0x9b: /* fwait */
7077
        if ((s->flags & (HF_MP_MASK | HF_TS_MASK)) ==
B
bellard 已提交
7078 7079
            (HF_MP_MASK | HF_TS_MASK)) {
            gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
B
bellard 已提交
7080
        } else {
7081
            gen_update_cc_op(s);
B
bellard 已提交
7082
            gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7083
            gen_helper_fwait(cpu_env);
B
bellard 已提交
7084
        }
B
bellard 已提交
7085 7086 7087 7088 7089
        break;
    case 0xcc: /* int3 */
        gen_interrupt(s, EXCP03_INT3, pc_start - s->cs_base, s->pc - s->cs_base);
        break;
    case 0xcd: /* int N */
7090
        val = cpu_ldub_code(env, s->pc++);
7091
        if (s->vm86 && s->iopl != 3) {
7092
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7093 7094 7095
        } else {
            gen_interrupt(s, val, pc_start - s->cs_base, s->pc - s->cs_base);
        }
B
bellard 已提交
7096 7097
        break;
    case 0xce: /* into */
B
bellard 已提交
7098 7099
        if (CODE64(s))
            goto illegal_op;
7100
        gen_update_cc_op(s);
7101
        gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7102
        gen_helper_into(cpu_env, tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
7103
        break;
A
aurel32 已提交
7104
#ifdef WANT_ICEBP
B
bellard 已提交
7105
    case 0xf1: /* icebp (undocumented, exits to external debugger) */
B
bellard 已提交
7106
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_ICEBP);
7107
#if 1
B
bellard 已提交
7108
        gen_debug(s, pc_start - s->cs_base);
7109 7110
#else
        /* start debug */
7111
        tb_flush(env);
7112
        qemu_set_log(CPU_LOG_INT | CPU_LOG_TB_IN_ASM);
7113
#endif
B
bellard 已提交
7114
        break;
A
aurel32 已提交
7115
#endif
B
bellard 已提交
7116 7117 7118
    case 0xfa: /* cli */
        if (!s->vm86) {
            if (s->cpl <= s->iopl) {
7119
                gen_helper_cli(cpu_env);
B
bellard 已提交
7120 7121 7122 7123 7124
            } else {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            }
        } else {
            if (s->iopl == 3) {
7125
                gen_helper_cli(cpu_env);
B
bellard 已提交
7126 7127 7128 7129 7130 7131 7132 7133 7134
            } 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:
7135
                gen_helper_sti(cpu_env);
B
bellard 已提交
7136
                /* interruptions are enabled only the first insn after sti */
7137 7138 7139
                /* If several instructions disable interrupts, only the
                   _first_ does it */
                if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
7140
                    gen_helper_set_inhibit_irq(cpu_env);
B
bellard 已提交
7141
                /* give a chance to handle pending irqs */
B
bellard 已提交
7142
                gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155
                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 已提交
7156 7157
        if (CODE64(s))
            goto illegal_op;
7158
        ot = dflag ? MO_32 : MO_16;
7159
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7160 7161 7162 7163
        reg = (modrm >> 3) & 7;
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
B
bellard 已提交
7164
        gen_op_mov_TN_reg(ot, 0, reg);
7165
        gen_lea_modrm(env, s, modrm);
B
bellard 已提交
7166
        gen_jmp_im(pc_start - s->cs_base);
7167
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
7168
        if (ot == MO_16) {
B
Blue Swirl 已提交
7169 7170 7171 7172
            gen_helper_boundw(cpu_env, cpu_A0, cpu_tmp2_i32);
        } else {
            gen_helper_boundl(cpu_env, cpu_A0, cpu_tmp2_i32);
        }
B
bellard 已提交
7173 7174
        break;
    case 0x1c8 ... 0x1cf: /* bswap reg */
B
bellard 已提交
7175 7176 7177
        reg = (b & 7) | REX_B(s);
#ifdef TARGET_X86_64
        if (dflag == 2) {
7178
            gen_op_mov_TN_reg(MO_64, 0, reg);
A
aurel32 已提交
7179
            tcg_gen_bswap64_i64(cpu_T[0], cpu_T[0]);
7180
            gen_op_mov_reg_T0(MO_64, reg);
7181
        } else
7182
#endif
B
bellard 已提交
7183
        {
7184
            gen_op_mov_TN_reg(MO_32, 0, reg);
7185 7186
            tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
            tcg_gen_bswap32_tl(cpu_T[0], cpu_T[0]);
7187
            gen_op_mov_reg_T0(MO_32, reg);
B
bellard 已提交
7188
        }
B
bellard 已提交
7189 7190
        break;
    case 0xd6: /* salc */
B
bellard 已提交
7191 7192
        if (CODE64(s))
            goto illegal_op;
7193
        gen_compute_eflags_c(s, cpu_T[0]);
7194
        tcg_gen_neg_tl(cpu_T[0], cpu_T[0]);
7195
        gen_op_mov_reg_T0(MO_8, R_EAX);
B
bellard 已提交
7196 7197 7198 7199 7200
        break;
    case 0xe0: /* loopnz */
    case 0xe1: /* loopz */
    case 0xe2: /* loop */
    case 0xe3: /* jecxz */
B
bellard 已提交
7201
        {
7202
            int l1, l2, l3;
B
bellard 已提交
7203

7204
            tval = (int8_t)insn_get(env, s, MO_8);
B
bellard 已提交
7205 7206 7207 7208
            next_eip = s->pc - s->cs_base;
            tval += next_eip;
            if (s->dflag == 0)
                tval &= 0xffff;
7209

B
bellard 已提交
7210 7211
            l1 = gen_new_label();
            l2 = gen_new_label();
7212
            l3 = gen_new_label();
B
bellard 已提交
7213
            b &= 3;
7214 7215 7216 7217 7218
            switch(b) {
            case 0: /* loopnz */
            case 1: /* loopz */
                gen_op_add_reg_im(s->aflag, R_ECX, -1);
                gen_op_jz_ecx(s->aflag, l3);
7219
                gen_jcc1(s, (JCC_Z << 1) | (b ^ 1), l1);
7220 7221 7222 7223 7224 7225 7226 7227 7228
                break;
            case 2: /* loop */
                gen_op_add_reg_im(s->aflag, R_ECX, -1);
                gen_op_jnz_ecx(s->aflag, l1);
                break;
            default:
            case 3: /* jcxz */
                gen_op_jz_ecx(s->aflag, l1);
                break;
B
bellard 已提交
7229 7230
            }

7231
            gen_set_label(l3);
B
bellard 已提交
7232
            gen_jmp_im(next_eip);
7233
            tcg_gen_br(l2);
7234

B
bellard 已提交
7235 7236 7237 7238 7239
            gen_set_label(l1);
            gen_jmp_im(tval);
            gen_set_label(l2);
            gen_eob(s);
        }
B
bellard 已提交
7240 7241 7242 7243 7244 7245
        break;
    case 0x130: /* wrmsr */
    case 0x132: /* rdmsr */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
7246
            gen_update_cc_op(s);
B
bellard 已提交
7247
            gen_jmp_im(pc_start - s->cs_base);
T
ths 已提交
7248
            if (b & 2) {
B
Blue Swirl 已提交
7249
                gen_helper_rdmsr(cpu_env);
T
ths 已提交
7250
            } else {
B
Blue Swirl 已提交
7251
                gen_helper_wrmsr(cpu_env);
T
ths 已提交
7252
            }
B
bellard 已提交
7253 7254 7255
        }
        break;
    case 0x131: /* rdtsc */
7256
        gen_update_cc_op(s);
B
bellard 已提交
7257
        gen_jmp_im(pc_start - s->cs_base);
P
pbrook 已提交
7258 7259
        if (use_icount)
            gen_io_start();
B
Blue Swirl 已提交
7260
        gen_helper_rdtsc(cpu_env);
P
pbrook 已提交
7261 7262 7263 7264
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
7265
        break;
7266
    case 0x133: /* rdpmc */
7267
        gen_update_cc_op(s);
7268
        gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7269
        gen_helper_rdpmc(cpu_env);
7270
        break;
7271
    case 0x134: /* sysenter */
7272
        /* For Intel SYSENTER is valid on 64-bit */
7273
        if (CODE64(s) && env->cpuid_vendor1 != CPUID_VENDOR_INTEL_1)
B
bellard 已提交
7274
            goto illegal_op;
7275 7276 7277
        if (!s->pe) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
J
Jun Koi 已提交
7278
            gen_update_cc_op(s);
B
bellard 已提交
7279
            gen_jmp_im(pc_start - s->cs_base);
7280
            gen_helper_sysenter(cpu_env);
7281 7282 7283 7284
            gen_eob(s);
        }
        break;
    case 0x135: /* sysexit */
7285
        /* For Intel SYSEXIT is valid on 64-bit */
7286
        if (CODE64(s) && env->cpuid_vendor1 != CPUID_VENDOR_INTEL_1)
B
bellard 已提交
7287
            goto illegal_op;
7288 7289 7290
        if (!s->pe) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
J
Jun Koi 已提交
7291
            gen_update_cc_op(s);
B
bellard 已提交
7292
            gen_jmp_im(pc_start - s->cs_base);
7293
            gen_helper_sysexit(cpu_env, tcg_const_i32(dflag));
7294 7295 7296
            gen_eob(s);
        }
        break;
B
bellard 已提交
7297 7298 7299
#ifdef TARGET_X86_64
    case 0x105: /* syscall */
        /* XXX: is it usable in real mode ? */
J
Jun Koi 已提交
7300
        gen_update_cc_op(s);
B
bellard 已提交
7301
        gen_jmp_im(pc_start - s->cs_base);
7302
        gen_helper_syscall(cpu_env, tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
7303 7304 7305 7306 7307 7308
        gen_eob(s);
        break;
    case 0x107: /* sysret */
        if (!s->pe) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
J
Jun Koi 已提交
7309
            gen_update_cc_op(s);
B
bellard 已提交
7310
            gen_jmp_im(pc_start - s->cs_base);
7311
            gen_helper_sysret(cpu_env, tcg_const_i32(s->dflag));
7312
            /* condition codes are modified only in long mode */
7313 7314 7315
            if (s->lma) {
                set_cc_op(s, CC_OP_EFLAGS);
            }
B
bellard 已提交
7316 7317 7318 7319
            gen_eob(s);
        }
        break;
#endif
B
bellard 已提交
7320
    case 0x1a2: /* cpuid */
7321
        gen_update_cc_op(s);
B
bellard 已提交
7322
        gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7323
        gen_helper_cpuid(cpu_env);
B
bellard 已提交
7324 7325 7326 7327 7328
        break;
    case 0xf4: /* hlt */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
7329
            gen_update_cc_op(s);
7330
            gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7331
            gen_helper_hlt(cpu_env, tcg_const_i32(s->pc - pc_start));
J
Jun Koi 已提交
7332
            s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
7333 7334 7335
        }
        break;
    case 0x100:
7336
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7337 7338 7339 7340
        mod = (modrm >> 6) & 3;
        op = (modrm >> 3) & 7;
        switch(op) {
        case 0: /* sldt */
7341 7342
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
7343
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_LDTR_READ);
B
bellard 已提交
7344
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,ldt.selector));
7345
            ot = MO_16;
B
bellard 已提交
7346 7347
            if (mod == 3)
                ot += s->dflag;
7348
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
7349 7350
            break;
        case 2: /* lldt */
7351 7352
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
7353 7354 7355
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
B
bellard 已提交
7356
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_LDTR_WRITE);
7357
                gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
B
bellard 已提交
7358
                gen_jmp_im(pc_start - s->cs_base);
7359
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
7360
                gen_helper_lldt(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
7361 7362 7363
            }
            break;
        case 1: /* str */
7364 7365
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
7366
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_TR_READ);
B
bellard 已提交
7367
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,tr.selector));
7368
            ot = MO_16;
B
bellard 已提交
7369 7370
            if (mod == 3)
                ot += s->dflag;
7371
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
7372 7373
            break;
        case 3: /* ltr */
7374 7375
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
7376 7377 7378
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
B
bellard 已提交
7379
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_TR_WRITE);
7380
                gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
B
bellard 已提交
7381
                gen_jmp_im(pc_start - s->cs_base);
7382
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
7383
                gen_helper_ltr(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
7384 7385 7386 7387
            }
            break;
        case 4: /* verr */
        case 5: /* verw */
7388 7389
            if (!s->pe || s->vm86)
                goto illegal_op;
7390
            gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
7391
            gen_update_cc_op(s);
7392 7393 7394 7395 7396
            if (op == 4) {
                gen_helper_verr(cpu_env, cpu_T[0]);
            } else {
                gen_helper_verw(cpu_env, cpu_T[0]);
            }
7397
            set_cc_op(s, CC_OP_EFLAGS);
7398
            break;
B
bellard 已提交
7399 7400 7401 7402 7403
        default:
            goto illegal_op;
        }
        break;
    case 0x101:
7404
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7405 7406
        mod = (modrm >> 6) & 3;
        op = (modrm >> 3) & 7;
B
bellard 已提交
7407
        rm = modrm & 7;
B
bellard 已提交
7408 7409 7410 7411
        switch(op) {
        case 0: /* sgdt */
            if (mod == 3)
                goto illegal_op;
B
bellard 已提交
7412
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_GDTR_READ);
7413
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
7414
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, gdt.limit));
7415
            gen_op_st_v(s, MO_16, cpu_T[0], cpu_A0);
7416
            gen_add_A0_im(s, 2);
B
bellard 已提交
7417
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, gdt.base));
B
bellard 已提交
7418 7419
            if (!s->dflag)
                gen_op_andl_T0_im(0xffffff);
7420
            gen_op_st_v(s, CODE64(s) + MO_32, cpu_T[0], cpu_A0);
B
bellard 已提交
7421
            break;
B
bellard 已提交
7422 7423 7424 7425 7426 7427 7428
        case 1:
            if (mod == 3) {
                switch (rm) {
                case 0: /* monitor */
                    if (!(s->cpuid_ext_features & CPUID_EXT_MONITOR) ||
                        s->cpl != 0)
                        goto illegal_op;
7429
                    gen_update_cc_op(s);
B
bellard 已提交
7430 7431 7432
                    gen_jmp_im(pc_start - s->cs_base);
#ifdef TARGET_X86_64
                    if (s->aflag == 2) {
7433
                        gen_op_movq_A0_reg(R_EAX);
7434
                    } else
B
bellard 已提交
7435 7436
#endif
                    {
7437
                        gen_op_movl_A0_reg(R_EAX);
B
bellard 已提交
7438 7439 7440 7441
                        if (s->aflag == 0)
                            gen_op_andl_A0_ffff();
                    }
                    gen_add_A0_ds_seg(s);
B
Blue Swirl 已提交
7442
                    gen_helper_monitor(cpu_env, cpu_A0);
B
bellard 已提交
7443 7444 7445 7446 7447
                    break;
                case 1: /* mwait */
                    if (!(s->cpuid_ext_features & CPUID_EXT_MONITOR) ||
                        s->cpl != 0)
                        goto illegal_op;
J
Jun Koi 已提交
7448
                    gen_update_cc_op(s);
7449
                    gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7450
                    gen_helper_mwait(cpu_env, tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
7451 7452
                    gen_eob(s);
                    break;
H
H. Peter Anvin 已提交
7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470
                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 已提交
7471 7472 7473 7474
                default:
                    goto illegal_op;
                }
            } else { /* sidt */
B
bellard 已提交
7475
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_IDTR_READ);
7476
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
7477
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, idt.limit));
7478
                gen_op_st_v(s, MO_16, cpu_T[0], cpu_A0);
B
bellard 已提交
7479
                gen_add_A0_im(s, 2);
B
bellard 已提交
7480
                tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, idt.base));
B
bellard 已提交
7481 7482
                if (!s->dflag)
                    gen_op_andl_T0_im(0xffffff);
7483
                gen_op_st_v(s, CODE64(s) + MO_32, cpu_T[0], cpu_A0);
B
bellard 已提交
7484 7485
            }
            break;
B
bellard 已提交
7486 7487
        case 2: /* lgdt */
        case 3: /* lidt */
T
ths 已提交
7488
            if (mod == 3) {
7489
                gen_update_cc_op(s);
B
bellard 已提交
7490
                gen_jmp_im(pc_start - s->cs_base);
T
ths 已提交
7491 7492
                switch(rm) {
                case 0: /* VMRUN */
B
bellard 已提交
7493 7494 7495 7496
                    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 已提交
7497
                        break;
B
bellard 已提交
7498
                    } else {
B
Blue Swirl 已提交
7499
                        gen_helper_vmrun(cpu_env, tcg_const_i32(s->aflag),
P
pbrook 已提交
7500
                                         tcg_const_i32(s->pc - pc_start));
7501
                        tcg_gen_exit_tb(0);
J
Jun Koi 已提交
7502
                        s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
7503
                    }
T
ths 已提交
7504 7505
                    break;
                case 1: /* VMMCALL */
B
bellard 已提交
7506 7507
                    if (!(s->flags & HF_SVME_MASK))
                        goto illegal_op;
B
Blue Swirl 已提交
7508
                    gen_helper_vmmcall(cpu_env);
T
ths 已提交
7509 7510
                    break;
                case 2: /* VMLOAD */
B
bellard 已提交
7511 7512 7513 7514 7515 7516
                    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 已提交
7517
                        gen_helper_vmload(cpu_env, tcg_const_i32(s->aflag));
B
bellard 已提交
7518
                    }
T
ths 已提交
7519 7520
                    break;
                case 3: /* VMSAVE */
B
bellard 已提交
7521 7522 7523 7524 7525 7526
                    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 已提交
7527
                        gen_helper_vmsave(cpu_env, tcg_const_i32(s->aflag));
B
bellard 已提交
7528
                    }
T
ths 已提交
7529 7530
                    break;
                case 4: /* STGI */
B
bellard 已提交
7531 7532 7533 7534 7535 7536 7537 7538
                    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 已提交
7539
                        gen_helper_stgi(cpu_env);
B
bellard 已提交
7540
                    }
T
ths 已提交
7541 7542
                    break;
                case 5: /* CLGI */
B
bellard 已提交
7543 7544 7545 7546 7547 7548
                    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 已提交
7549
                        gen_helper_clgi(cpu_env);
B
bellard 已提交
7550
                    }
T
ths 已提交
7551 7552
                    break;
                case 6: /* SKINIT */
B
bellard 已提交
7553 7554 7555 7556
                    if ((!(s->flags & HF_SVME_MASK) && 
                         !(s->cpuid_ext3_features & CPUID_EXT3_SKINIT)) || 
                        !s->pe)
                        goto illegal_op;
B
Blue Swirl 已提交
7557
                    gen_helper_skinit(cpu_env);
T
ths 已提交
7558 7559
                    break;
                case 7: /* INVLPGA */
B
bellard 已提交
7560 7561 7562 7563 7564 7565
                    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 已提交
7566
                        gen_helper_invlpga(cpu_env, tcg_const_i32(s->aflag));
B
bellard 已提交
7567
                    }
T
ths 已提交
7568 7569 7570 7571 7572
                    break;
                default:
                    goto illegal_op;
                }
            } else if (s->cpl != 0) {
B
bellard 已提交
7573 7574
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
B
bellard 已提交
7575 7576
                gen_svm_check_intercept(s, pc_start,
                                        op==2 ? SVM_EXIT_GDTR_WRITE : SVM_EXIT_IDTR_WRITE);
7577
                gen_lea_modrm(env, s, modrm);
7578
                gen_op_ld_v(s, MO_16, cpu_T[1], cpu_A0);
7579
                gen_add_A0_im(s, 2);
7580
                gen_op_ld_v(s, CODE64(s) + MO_32, cpu_T[0], cpu_A0);
B
bellard 已提交
7581 7582 7583
                if (!s->dflag)
                    gen_op_andl_T0_im(0xffffff);
                if (op == 2) {
B
bellard 已提交
7584 7585
                    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 已提交
7586
                } else {
B
bellard 已提交
7587 7588
                    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 已提交
7589 7590 7591 7592
                }
            }
            break;
        case 4: /* smsw */
B
bellard 已提交
7593
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_READ_CR0);
7594
#if defined TARGET_X86_64 && defined HOST_WORDS_BIGENDIAN
7595 7596
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,cr[0]) + 4);
#else
B
bellard 已提交
7597
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,cr[0]));
7598
#endif
7599
            gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 1);
B
bellard 已提交
7600 7601 7602 7603 7604
            break;
        case 6: /* lmsw */
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
B
bellard 已提交
7605
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_CR0);
7606
                gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
B
Blue Swirl 已提交
7607
                gen_helper_lmsw(cpu_env, cpu_T[0]);
B
bellard 已提交
7608
                gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
7609
                gen_eob(s);
B
bellard 已提交
7610 7611
            }
            break;
A
Andre Przywara 已提交
7612 7613 7614 7615 7616
        case 7:
            if (mod != 3) { /* invlpg */
                if (s->cpl != 0) {
                    gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
                } else {
7617
                    gen_update_cc_op(s);
A
Andre Przywara 已提交
7618
                    gen_jmp_im(pc_start - s->cs_base);
7619
                    gen_lea_modrm(env, s, modrm);
B
Blue Swirl 已提交
7620
                    gen_helper_invlpg(cpu_env, cpu_A0);
A
Andre Przywara 已提交
7621 7622 7623
                    gen_jmp_im(s->pc - s->cs_base);
                    gen_eob(s);
                }
B
bellard 已提交
7624
            } else {
A
Andre Przywara 已提交
7625 7626
                switch (rm) {
                case 0: /* swapgs */
B
bellard 已提交
7627
#ifdef TARGET_X86_64
A
Andre Przywara 已提交
7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640
                    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));
                        }
7641
                    } else
B
bellard 已提交
7642 7643 7644 7645
#endif
                    {
                        goto illegal_op;
                    }
A
Andre Przywara 已提交
7646 7647 7648 7649
                    break;
                case 1: /* rdtscp */
                    if (!(s->cpuid_ext2_features & CPUID_EXT2_RDTSCP))
                        goto illegal_op;
7650
                    gen_update_cc_op(s);
B
bellard 已提交
7651
                    gen_jmp_im(pc_start - s->cs_base);
A
Andre Przywara 已提交
7652 7653
                    if (use_icount)
                        gen_io_start();
B
Blue Swirl 已提交
7654
                    gen_helper_rdtscp(cpu_env);
A
Andre Przywara 已提交
7655 7656 7657 7658 7659 7660 7661
                    if (use_icount) {
                        gen_io_end();
                        gen_jmp(s, s->pc - s->cs_base);
                    }
                    break;
                default:
                    goto illegal_op;
B
bellard 已提交
7662
                }
B
bellard 已提交
7663 7664 7665 7666 7667 7668
            }
            break;
        default:
            goto illegal_op;
        }
        break;
7669 7670 7671 7672 7673
    case 0x108: /* invd */
    case 0x109: /* wbinvd */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
B
bellard 已提交
7674
            gen_svm_check_intercept(s, pc_start, (b & 2) ? SVM_EXIT_INVD : SVM_EXIT_WBINVD);
7675 7676 7677
            /* nothing to do */
        }
        break;
B
bellard 已提交
7678 7679 7680 7681 7682
    case 0x63: /* arpl or movslS (x86_64) */
#ifdef TARGET_X86_64
        if (CODE64(s)) {
            int d_ot;
            /* d_ot is the size of destination */
7683
            d_ot = dflag + MO_16;
B
bellard 已提交
7684

7685
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7686 7687 7688
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
            rm = (modrm & 7) | REX_B(s);
7689

B
bellard 已提交
7690
            if (mod == 3) {
7691
                gen_op_mov_TN_reg(MO_32, 0, rm);
B
bellard 已提交
7692
                /* sign extend */
7693
                if (d_ot == MO_64) {
B
bellard 已提交
7694
                    tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
7695
                }
B
bellard 已提交
7696
                gen_op_mov_reg_T0(d_ot, reg);
B
bellard 已提交
7697
            } else {
7698
                gen_lea_modrm(env, s, modrm);
R
Richard Henderson 已提交
7699
                gen_op_ld_v(s, MO_32 | MO_SIGN, cpu_T[0], cpu_A0);
B
bellard 已提交
7700
                gen_op_mov_reg_T0(d_ot, reg);
B
bellard 已提交
7701
            }
7702
        } else
B
bellard 已提交
7703 7704
#endif
        {
7705
            int label1;
L
Laurent Desnogues 已提交
7706
            TCGv t0, t1, t2, a0;
7707

B
bellard 已提交
7708 7709
            if (!s->pe || s->vm86)
                goto illegal_op;
P
pbrook 已提交
7710 7711 7712
            t0 = tcg_temp_local_new();
            t1 = tcg_temp_local_new();
            t2 = tcg_temp_local_new();
7713
            ot = MO_16;
7714
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7715 7716 7717 7718
            reg = (modrm >> 3) & 7;
            mod = (modrm >> 6) & 3;
            rm = modrm & 7;
            if (mod != 3) {
7719
                gen_lea_modrm(env, s, modrm);
7720
                gen_op_ld_v(s, ot, t0, cpu_A0);
L
Laurent Desnogues 已提交
7721 7722
                a0 = tcg_temp_local_new();
                tcg_gen_mov_tl(a0, cpu_A0);
B
bellard 已提交
7723
            } else {
7724
                gen_op_mov_v_reg(ot, t0, rm);
L
Laurent Desnogues 已提交
7725
                TCGV_UNUSED(a0);
B
bellard 已提交
7726
            }
7727 7728 7729 7730
            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);
7731
            label1 = gen_new_label();
7732 7733 7734 7735
            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);
7736
            gen_set_label(label1);
B
bellard 已提交
7737
            if (mod != 3) {
7738
                gen_op_st_v(s, ot, t0, a0);
L
Laurent Desnogues 已提交
7739 7740
                tcg_temp_free(a0);
           } else {
7741
                gen_op_mov_reg_v(ot, rm, t0);
B
bellard 已提交
7742
            }
7743
            gen_compute_eflags(s);
7744
            tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~CC_Z);
7745 7746 7747 7748
            tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, t2);
            tcg_temp_free(t0);
            tcg_temp_free(t1);
            tcg_temp_free(t2);
7749 7750
        }
        break;
B
bellard 已提交
7751 7752
    case 0x102: /* lar */
    case 0x103: /* lsl */
7753 7754
        {
            int label1;
7755
            TCGv t0;
7756 7757
            if (!s->pe || s->vm86)
                goto illegal_op;
7758
            ot = dflag ? MO_32 : MO_16;
7759
            modrm = cpu_ldub_code(env, s->pc++);
7760
            reg = ((modrm >> 3) & 7) | rex_r;
7761
            gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
P
pbrook 已提交
7762
            t0 = tcg_temp_local_new();
7763
            gen_update_cc_op(s);
7764 7765 7766 7767 7768
            if (b == 0x102) {
                gen_helper_lar(t0, cpu_env, cpu_T[0]);
            } else {
                gen_helper_lsl(t0, cpu_env, cpu_T[0]);
            }
7769 7770
            tcg_gen_andi_tl(cpu_tmp0, cpu_cc_src, CC_Z);
            label1 = gen_new_label();
P
pbrook 已提交
7771
            tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, label1);
7772
            gen_op_mov_reg_v(ot, reg, t0);
7773
            gen_set_label(label1);
7774
            set_cc_op(s, CC_OP_EFLAGS);
7775
            tcg_temp_free(t0);
7776
        }
B
bellard 已提交
7777 7778
        break;
    case 0x118:
7779
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7780 7781 7782 7783 7784 7785 7786 7787 7788
        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;
7789
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
7790 7791
            /* nothing more to do */
            break;
B
bellard 已提交
7792
        default: /* nop (multi byte) */
7793
            gen_nop_modrm(env, s, modrm);
B
bellard 已提交
7794
            break;
B
bellard 已提交
7795 7796
        }
        break;
B
bellard 已提交
7797
    case 0x119 ... 0x11f: /* nop (multi byte) */
7798 7799
        modrm = cpu_ldub_code(env, s->pc++);
        gen_nop_modrm(env, s, modrm);
B
bellard 已提交
7800
        break;
B
bellard 已提交
7801 7802 7803 7804 7805
    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 {
7806
            modrm = cpu_ldub_code(env, s->pc++);
7807 7808 7809 7810 7811
            /* 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 已提交
7812 7813 7814
            rm = (modrm & 7) | REX_B(s);
            reg = ((modrm >> 3) & 7) | rex_r;
            if (CODE64(s))
7815
                ot = MO_64;
B
bellard 已提交
7816
            else
7817
                ot = MO_32;
7818 7819 7820 7821
            if ((prefixes & PREFIX_LOCK) && (reg == 0) &&
                (s->cpuid_ext3_features & CPUID_EXT3_CR8LEG)) {
                reg = 8;
            }
B
bellard 已提交
7822 7823 7824 7825 7826
            switch(reg) {
            case 0:
            case 2:
            case 3:
            case 4:
B
bellard 已提交
7827
            case 8:
7828
                gen_update_cc_op(s);
B
bellard 已提交
7829
                gen_jmp_im(pc_start - s->cs_base);
B
bellard 已提交
7830
                if (b & 2) {
B
bellard 已提交
7831
                    gen_op_mov_TN_reg(ot, 0, rm);
B
Blue Swirl 已提交
7832 7833
                    gen_helper_write_crN(cpu_env, tcg_const_i32(reg),
                                         cpu_T[0]);
B
bellard 已提交
7834
                    gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
7835 7836
                    gen_eob(s);
                } else {
B
Blue Swirl 已提交
7837
                    gen_helper_read_crN(cpu_T[0], cpu_env, tcg_const_i32(reg));
B
bellard 已提交
7838
                    gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850
                }
                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 {
7851
            modrm = cpu_ldub_code(env, s->pc++);
7852 7853 7854 7855 7856
            /* 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 已提交
7857 7858 7859
            rm = (modrm & 7) | REX_B(s);
            reg = ((modrm >> 3) & 7) | rex_r;
            if (CODE64(s))
7860
                ot = MO_64;
B
bellard 已提交
7861
            else
7862
                ot = MO_32;
B
bellard 已提交
7863
            /* XXX: do it dynamically with CR4.DE bit */
B
bellard 已提交
7864
            if (reg == 4 || reg == 5 || reg >= 8)
B
bellard 已提交
7865 7866
                goto illegal_op;
            if (b & 2) {
T
ths 已提交
7867
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_DR0 + reg);
B
bellard 已提交
7868
                gen_op_mov_TN_reg(ot, 0, rm);
B
Blue Swirl 已提交
7869
                gen_helper_movl_drN_T0(cpu_env, tcg_const_i32(reg), cpu_T[0]);
B
bellard 已提交
7870
                gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
7871 7872
                gen_eob(s);
            } else {
T
ths 已提交
7873
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_READ_DR0 + reg);
B
bellard 已提交
7874
                tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,dr[reg]));
B
bellard 已提交
7875
                gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
7876 7877 7878 7879 7880 7881 7882
            }
        }
        break;
    case 0x106: /* clts */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
T
ths 已提交
7883
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_CR0);
7884
            gen_helper_clts(cpu_env);
B
bellard 已提交
7885
            /* abort block because static cpu state changed */
B
bellard 已提交
7886
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
7887
            gen_eob(s);
B
bellard 已提交
7888 7889
        }
        break;
B
balrog 已提交
7890
    /* MMX/3DNow!/SSE/SSE2/SSE3/SSSE3/SSE4 support */
B
bellard 已提交
7891 7892
    case 0x1c3: /* MOVNTI reg, mem */
        if (!(s->cpuid_features & CPUID_SSE2))
B
bellard 已提交
7893
            goto illegal_op;
7894
        ot = s->dflag == 2 ? MO_64 : MO_32;
7895
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7896 7897 7898 7899 7900
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
        reg = ((modrm >> 3) & 7) | rex_r;
        /* generate a generic store */
7901
        gen_ldst_modrm(env, s, modrm, ot, reg, 1);
B
bellard 已提交
7902
        break;
B
bellard 已提交
7903
    case 0x1ae:
7904
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7905 7906 7907 7908
        mod = (modrm >> 6) & 3;
        op = (modrm >> 3) & 7;
        switch(op) {
        case 0: /* fxsave */
7909
            if (mod == 3 || !(s->cpuid_features & CPUID_FXSR) ||
7910
                (s->prefix & PREFIX_LOCK))
B
bellard 已提交
7911
                goto illegal_op;
7912
            if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
B
bellard 已提交
7913 7914 7915
                gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
                break;
            }
7916
            gen_lea_modrm(env, s, modrm);
7917
            gen_update_cc_op(s);
B
bellard 已提交
7918
            gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7919
            gen_helper_fxsave(cpu_env, cpu_A0, tcg_const_i32((s->dflag == 2)));
B
bellard 已提交
7920 7921
            break;
        case 1: /* fxrstor */
7922
            if (mod == 3 || !(s->cpuid_features & CPUID_FXSR) ||
7923
                (s->prefix & PREFIX_LOCK))
B
bellard 已提交
7924
                goto illegal_op;
7925
            if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
B
bellard 已提交
7926 7927 7928
                gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
                break;
            }
7929
            gen_lea_modrm(env, s, modrm);
7930
            gen_update_cc_op(s);
B
bellard 已提交
7931
            gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7932 7933
            gen_helper_fxrstor(cpu_env, cpu_A0,
                               tcg_const_i32((s->dflag == 2)));
B
bellard 已提交
7934 7935 7936 7937 7938 7939
            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 已提交
7940
            }
B
bellard 已提交
7941 7942
            if ((s->flags & HF_EM_MASK) || !(s->flags & HF_OSFXSR_MASK) ||
                mod == 3)
B
bellard 已提交
7943
                goto illegal_op;
7944
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
7945
            if (op == 2) {
7946 7947
                tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                    s->mem_index, MO_LEUL);
B
Blue Swirl 已提交
7948
                gen_helper_ldmxcsr(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
7949
            } else {
B
bellard 已提交
7950
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, mxcsr));
7951
                gen_op_st_v(s, MO_32, cpu_T[0], cpu_A0);
B
bellard 已提交
7952
            }
B
bellard 已提交
7953 7954 7955
            break;
        case 5: /* lfence */
        case 6: /* mfence */
7956
            if ((modrm & 0xc7) != 0xc0 || !(s->cpuid_features & CPUID_SSE2))
B
bellard 已提交
7957 7958
                goto illegal_op;
            break;
7959 7960 7961
        case 7: /* sfence / clflush */
            if ((modrm & 0xc7) == 0xc0) {
                /* sfence */
A
aurel32 已提交
7962
                /* XXX: also check for cpuid_ext2_features & CPUID_EXT2_EMMX */
7963 7964 7965 7966 7967 7968
                if (!(s->cpuid_features & CPUID_SSE))
                    goto illegal_op;
            } else {
                /* clflush */
                if (!(s->cpuid_features & CPUID_CLFLUSH))
                    goto illegal_op;
7969
                gen_lea_modrm(env, s, modrm);
7970 7971
            }
            break;
B
bellard 已提交
7972
        default:
B
bellard 已提交
7973 7974 7975
            goto illegal_op;
        }
        break;
A
aurel32 已提交
7976
    case 0x10d: /* 3DNow! prefetch(w) */
7977
        modrm = cpu_ldub_code(env, s->pc++);
A
aurel32 已提交
7978 7979 7980
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
7981
        gen_lea_modrm(env, s, modrm);
7982 7983
        /* ignore for now */
        break;
B
bellard 已提交
7984
    case 0x1aa: /* rsm */
B
bellard 已提交
7985
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_RSM);
B
bellard 已提交
7986 7987
        if (!(s->flags & HF_SMM_MASK))
            goto illegal_op;
J
Jun Koi 已提交
7988
        gen_update_cc_op(s);
B
bellard 已提交
7989
        gen_jmp_im(s->pc - s->cs_base);
B
Blue Swirl 已提交
7990
        gen_helper_rsm(cpu_env);
B
bellard 已提交
7991 7992
        gen_eob(s);
        break;
B
balrog 已提交
7993 7994 7995 7996 7997 7998 7999
    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;

8000
        modrm = cpu_ldub_code(env, s->pc++);
M
malc 已提交
8001
        reg = ((modrm >> 3) & 7) | rex_r;
B
balrog 已提交
8002 8003

        if (s->prefix & PREFIX_DATA)
8004
            ot = MO_16;
B
balrog 已提交
8005
        else if (s->dflag != 2)
8006
            ot = MO_32;
B
balrog 已提交
8007
        else
8008
            ot = MO_64;
B
balrog 已提交
8009

8010
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
Blue Swirl 已提交
8011
        gen_helper_popcnt(cpu_T[0], cpu_env, cpu_T[0], tcg_const_i32(ot));
B
balrog 已提交
8012
        gen_op_mov_reg_T0(ot, reg);
B
balrog 已提交
8013

8014
        set_cc_op(s, CC_OP_EFLAGS);
B
balrog 已提交
8015
        break;
A
aurel32 已提交
8016 8017 8018
    case 0x10e ... 0x10f:
        /* 3DNow! instructions, ignore prefixes */
        s->prefix &= ~(PREFIX_REPZ | PREFIX_REPNZ | PREFIX_DATA);
B
bellard 已提交
8019 8020
    case 0x110 ... 0x117:
    case 0x128 ... 0x12f:
B
balrog 已提交
8021
    case 0x138 ... 0x13a:
8022
    case 0x150 ... 0x179:
B
bellard 已提交
8023 8024 8025 8026
    case 0x17c ... 0x17f:
    case 0x1c2:
    case 0x1c4 ... 0x1c6:
    case 0x1d0 ... 0x1fe:
8027
        gen_sse(env, s, b, pc_start, rex_r);
B
bellard 已提交
8028
        break;
B
bellard 已提交
8029 8030 8031 8032 8033
    default:
        goto illegal_op;
    }
    /* lock generation */
    if (s->prefix & PREFIX_LOCK)
P
pbrook 已提交
8034
        gen_helper_unlock();
B
bellard 已提交
8035 8036
    return s->pc;
 illegal_op:
8037
    if (s->prefix & PREFIX_LOCK)
P
pbrook 已提交
8038
        gen_helper_unlock();
B
bellard 已提交
8039 8040 8041 8042 8043 8044 8045
    /* XXX: ensure that no lock was generated */
    gen_exception(s, EXCP06_ILLOP, pc_start - s->cs_base);
    return s->pc;
}

void optimize_flags_init(void)
{
P
pbrook 已提交
8046 8047
    cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
    cpu_cc_op = tcg_global_mem_new_i32(TCG_AREG0,
8048 8049
                                       offsetof(CPUX86State, cc_op), "cc_op");
    cpu_cc_dst = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_dst),
P
pbrook 已提交
8050
                                    "cc_dst");
8051 8052
    cpu_cc_src = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_src),
                                    "cc_src");
8053 8054
    cpu_cc_src2 = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_src2),
                                     "cc_src2");
8055

8056 8057
#ifdef TARGET_X86_64
    cpu_regs[R_EAX] = tcg_global_mem_new_i64(TCG_AREG0,
8058
                                             offsetof(CPUX86State, regs[R_EAX]), "rax");
8059
    cpu_regs[R_ECX] = tcg_global_mem_new_i64(TCG_AREG0,
8060
                                             offsetof(CPUX86State, regs[R_ECX]), "rcx");
8061
    cpu_regs[R_EDX] = tcg_global_mem_new_i64(TCG_AREG0,
8062
                                             offsetof(CPUX86State, regs[R_EDX]), "rdx");
8063
    cpu_regs[R_EBX] = tcg_global_mem_new_i64(TCG_AREG0,
8064
                                             offsetof(CPUX86State, regs[R_EBX]), "rbx");
8065
    cpu_regs[R_ESP] = tcg_global_mem_new_i64(TCG_AREG0,
8066
                                             offsetof(CPUX86State, regs[R_ESP]), "rsp");
8067
    cpu_regs[R_EBP] = tcg_global_mem_new_i64(TCG_AREG0,
8068
                                             offsetof(CPUX86State, regs[R_EBP]), "rbp");
8069
    cpu_regs[R_ESI] = tcg_global_mem_new_i64(TCG_AREG0,
8070
                                             offsetof(CPUX86State, regs[R_ESI]), "rsi");
8071
    cpu_regs[R_EDI] = tcg_global_mem_new_i64(TCG_AREG0,
8072
                                             offsetof(CPUX86State, regs[R_EDI]), "rdi");
8073
    cpu_regs[8] = tcg_global_mem_new_i64(TCG_AREG0,
8074
                                         offsetof(CPUX86State, regs[8]), "r8");
8075
    cpu_regs[9] = tcg_global_mem_new_i64(TCG_AREG0,
8076
                                          offsetof(CPUX86State, regs[9]), "r9");
8077
    cpu_regs[10] = tcg_global_mem_new_i64(TCG_AREG0,
8078
                                          offsetof(CPUX86State, regs[10]), "r10");
8079
    cpu_regs[11] = tcg_global_mem_new_i64(TCG_AREG0,
8080
                                          offsetof(CPUX86State, regs[11]), "r11");
8081
    cpu_regs[12] = tcg_global_mem_new_i64(TCG_AREG0,
8082
                                          offsetof(CPUX86State, regs[12]), "r12");
8083
    cpu_regs[13] = tcg_global_mem_new_i64(TCG_AREG0,
8084
                                          offsetof(CPUX86State, regs[13]), "r13");
8085
    cpu_regs[14] = tcg_global_mem_new_i64(TCG_AREG0,
8086
                                          offsetof(CPUX86State, regs[14]), "r14");
8087
    cpu_regs[15] = tcg_global_mem_new_i64(TCG_AREG0,
8088
                                          offsetof(CPUX86State, regs[15]), "r15");
8089 8090
#else
    cpu_regs[R_EAX] = tcg_global_mem_new_i32(TCG_AREG0,
8091
                                             offsetof(CPUX86State, regs[R_EAX]), "eax");
8092
    cpu_regs[R_ECX] = tcg_global_mem_new_i32(TCG_AREG0,
8093
                                             offsetof(CPUX86State, regs[R_ECX]), "ecx");
8094
    cpu_regs[R_EDX] = tcg_global_mem_new_i32(TCG_AREG0,
8095
                                             offsetof(CPUX86State, regs[R_EDX]), "edx");
8096
    cpu_regs[R_EBX] = tcg_global_mem_new_i32(TCG_AREG0,
8097
                                             offsetof(CPUX86State, regs[R_EBX]), "ebx");
8098
    cpu_regs[R_ESP] = tcg_global_mem_new_i32(TCG_AREG0,
8099
                                             offsetof(CPUX86State, regs[R_ESP]), "esp");
8100
    cpu_regs[R_EBP] = tcg_global_mem_new_i32(TCG_AREG0,
8101
                                             offsetof(CPUX86State, regs[R_EBP]), "ebp");
8102
    cpu_regs[R_ESI] = tcg_global_mem_new_i32(TCG_AREG0,
8103
                                             offsetof(CPUX86State, regs[R_ESI]), "esi");
8104
    cpu_regs[R_EDI] = tcg_global_mem_new_i32(TCG_AREG0,
8105
                                             offsetof(CPUX86State, regs[R_EDI]), "edi");
8106
#endif
B
bellard 已提交
8107 8108 8109 8110 8111
}

/* 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. */
8112
static inline void gen_intermediate_code_internal(X86CPU *cpu,
8113
                                                  TranslationBlock *tb,
8114
                                                  bool search_pc)
B
bellard 已提交
8115
{
8116
    CPUState *cs = CPU(cpu);
8117
    CPUX86State *env = &cpu->env;
B
bellard 已提交
8118
    DisasContext dc1, *dc = &dc1;
B
bellard 已提交
8119
    target_ulong pc_ptr;
B
bellard 已提交
8120
    uint16_t *gen_opc_end;
8121
    CPUBreakpoint *bp;
8122
    int j, lj;
8123
    uint64_t flags;
B
bellard 已提交
8124 8125
    target_ulong pc_start;
    target_ulong cs_base;
P
pbrook 已提交
8126 8127
    int num_insns;
    int max_insns;
8128

B
bellard 已提交
8129
    /* generate intermediate code */
B
bellard 已提交
8130 8131
    pc_start = tb->pc;
    cs_base = tb->cs_base;
B
bellard 已提交
8132
    flags = tb->flags;
B
bellard 已提交
8133

8134
    dc->pe = (flags >> HF_PE_SHIFT) & 1;
B
bellard 已提交
8135 8136 8137 8138 8139 8140 8141 8142
    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;
8143
    dc->singlestep_enabled = cs->singlestep_enabled;
B
bellard 已提交
8144
    dc->cc_op = CC_OP_DYNAMIC;
8145
    dc->cc_op_dirty = false;
B
bellard 已提交
8146 8147 8148 8149 8150 8151
    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) {
8152
        dc->mem_index = cpu_mmu_index(env);
B
bellard 已提交
8153
    }
8154 8155 8156 8157 8158
    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 已提交
8159 8160 8161 8162
#ifdef TARGET_X86_64
    dc->lma = (flags >> HF_LMA_SHIFT) & 1;
    dc->code64 = (flags >> HF_CS64_SHIFT) & 1;
#endif
B
bellard 已提交
8163
    dc->flags = flags;
8164
    dc->jmp_opt = !(dc->tf || cs->singlestep_enabled ||
8165
                    (flags & HF_INHIBIT_IRQ_MASK)
B
bellard 已提交
8166
#ifndef CONFIG_SOFTMMU
B
bellard 已提交
8167 8168 8169
                    || (flags & HF_SOFTMMU_MASK)
#endif
                    );
8170 8171
#if 0
    /* check addseg logic */
B
bellard 已提交
8172
    if (!dc->addseg && (dc->vm86 || !dc->pe || !dc->code32))
8173 8174 8175
        printf("ERROR addseg\n");
#endif

P
pbrook 已提交
8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186
    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();
8187
    cpu_cc_srcT = tcg_temp_local_new();
B
bellard 已提交
8188

8189
    gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
B
bellard 已提交
8190 8191 8192 8193

    dc->is_jmp = DISAS_NEXT;
    pc_ptr = pc_start;
    lj = -1;
P
pbrook 已提交
8194 8195 8196 8197
    num_insns = 0;
    max_insns = tb->cflags & CF_COUNT_MASK;
    if (max_insns == 0)
        max_insns = CF_COUNT_MASK;
B
bellard 已提交
8198

8199
    gen_tb_start();
B
bellard 已提交
8200
    for(;;) {
B
Blue Swirl 已提交
8201 8202
        if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
            QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
J
Jan Kiszka 已提交
8203 8204
                if (bp->pc == pc_ptr &&
                    !((bp->flags & BP_CPU) && (tb->flags & HF_RF_MASK))) {
B
bellard 已提交
8205 8206 8207 8208 8209 8210
                    gen_debug(dc, pc_ptr - dc->cs_base);
                    break;
                }
            }
        }
        if (search_pc) {
8211
            j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
B
bellard 已提交
8212 8213 8214
            if (lj < j) {
                lj++;
                while (lj < j)
8215
                    tcg_ctx.gen_opc_instr_start[lj++] = 0;
B
bellard 已提交
8216
            }
8217
            tcg_ctx.gen_opc_pc[lj] = pc_ptr;
B
bellard 已提交
8218
            gen_opc_cc_op[lj] = dc->cc_op;
8219
            tcg_ctx.gen_opc_instr_start[lj] = 1;
8220
            tcg_ctx.gen_opc_icount[lj] = num_insns;
B
bellard 已提交
8221
        }
P
pbrook 已提交
8222 8223 8224
        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
            gen_io_start();

8225
        pc_ptr = disas_insn(env, dc, pc_ptr);
P
pbrook 已提交
8226
        num_insns++;
B
bellard 已提交
8227 8228 8229 8230 8231
        /* stop translation if indicated */
        if (dc->is_jmp)
            break;
        /* if single step mode, we generate only one instruction and
           generate an exception */
8232 8233 8234
        /* 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 */
8235
        if (dc->tf || dc->singlestep_enabled ||
P
pbrook 已提交
8236
            (flags & HF_INHIBIT_IRQ_MASK)) {
B
bellard 已提交
8237
            gen_jmp_im(pc_ptr - dc->cs_base);
B
bellard 已提交
8238 8239 8240 8241
            gen_eob(dc);
            break;
        }
        /* if too long translation, stop generation too */
8242
        if (tcg_ctx.gen_opc_ptr >= gen_opc_end ||
P
pbrook 已提交
8243 8244
            (pc_ptr - pc_start) >= (TARGET_PAGE_SIZE - 32) ||
            num_insns >= max_insns) {
B
bellard 已提交
8245
            gen_jmp_im(pc_ptr - dc->cs_base);
B
bellard 已提交
8246 8247 8248
            gen_eob(dc);
            break;
        }
8249 8250 8251 8252 8253
        if (singlestep) {
            gen_jmp_im(pc_ptr - dc->cs_base);
            gen_eob(dc);
            break;
        }
B
bellard 已提交
8254
    }
P
pbrook 已提交
8255 8256
    if (tb->cflags & CF_LAST_IO)
        gen_io_end();
8257
    gen_tb_end(tb, num_insns);
8258
    *tcg_ctx.gen_opc_ptr = INDEX_op_end;
B
bellard 已提交
8259 8260
    /* we don't forget to fill the last values */
    if (search_pc) {
8261
        j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
B
bellard 已提交
8262 8263
        lj++;
        while (lj <= j)
8264
            tcg_ctx.gen_opc_instr_start[lj++] = 0;
B
bellard 已提交
8265
    }
8266

B
bellard 已提交
8267
#ifdef DEBUG_DISAS
8268
    if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
B
bellard 已提交
8269
        int disas_flags;
8270 8271
        qemu_log("----------------\n");
        qemu_log("IN: %s\n", lookup_symbol(pc_start));
B
bellard 已提交
8272 8273 8274 8275 8276 8277
#ifdef TARGET_X86_64
        if (dc->code64)
            disas_flags = 2;
        else
#endif
            disas_flags = !dc->code32;
B
Blue Swirl 已提交
8278
        log_target_disas(env, pc_start, pc_ptr - pc_start, disas_flags);
8279
        qemu_log("\n");
B
bellard 已提交
8280 8281 8282
    }
#endif

P
pbrook 已提交
8283
    if (!search_pc) {
B
bellard 已提交
8284
        tb->size = pc_ptr - pc_start;
P
pbrook 已提交
8285 8286
        tb->icount = num_insns;
    }
B
bellard 已提交
8287 8288
}

8289
void gen_intermediate_code(CPUX86State *env, TranslationBlock *tb)
B
bellard 已提交
8290
{
8291
    gen_intermediate_code_internal(x86_env_get_cpu(env), tb, false);
B
bellard 已提交
8292 8293
}

8294
void gen_intermediate_code_pc(CPUX86State *env, TranslationBlock *tb)
B
bellard 已提交
8295
{
8296
    gen_intermediate_code_internal(x86_env_get_cpu(env), tb, true);
B
bellard 已提交
8297 8298
}

8299
void restore_state_to_opc(CPUX86State *env, TranslationBlock *tb, int pc_pos)
A
aurel32 已提交
8300 8301 8302
{
    int cc_op;
#ifdef DEBUG_DISAS
8303
    if (qemu_loglevel_mask(CPU_LOG_TB_OP)) {
A
aurel32 已提交
8304
        int i;
8305
        qemu_log("RESTORE:\n");
A
aurel32 已提交
8306
        for(i = 0;i <= pc_pos; i++) {
8307
            if (tcg_ctx.gen_opc_instr_start[i]) {
8308 8309
                qemu_log("0x%04x: " TARGET_FMT_lx "\n", i,
                        tcg_ctx.gen_opc_pc[i]);
A
aurel32 已提交
8310 8311
            }
        }
8312
        qemu_log("pc_pos=0x%x eip=" TARGET_FMT_lx " cs_base=%x\n",
8313
                pc_pos, tcg_ctx.gen_opc_pc[pc_pos] - tb->cs_base,
A
aurel32 已提交
8314 8315 8316
                (uint32_t)tb->cs_base);
    }
#endif
8317
    env->eip = tcg_ctx.gen_opc_pc[pc_pos] - tb->cs_base;
A
aurel32 已提交
8318 8319 8320 8321
    cc_op = gen_opc_cc_op[pc_pos];
    if (cc_op != CC_OP_DYNAMIC)
        env->cc_op = cc_op;
}