translate.c 283.2 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;
75
static TCGv cpu_tmp5;
B
bellard 已提交
76

77 78
static uint8_t gen_opc_cc_op[OPC_BUF_SIZE];

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

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

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

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

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

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

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

B
bellard 已提交
167 168 169 170
/* operand size */
enum {
    OT_BYTE = 0,
    OT_WORD,
171
    OT_LONG,
B
bellard 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184
    OT_QUAD,
};

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 已提交
185 186

    OR_TMP0 = 16,    /* temporary operand register */
B
bellard 已提交
187 188 189 190
    OR_TMP1,
    OR_A0, /* temporary register used when doing address evaluation */
};

191
enum {
192 193
    USES_CC_DST  = 1,
    USES_CC_SRC  = 2,
194 195
    USES_CC_SRC2 = 4,
    USES_CC_SRCT = 8,
196 197 198 199
};

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

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

    s->cc_op = op;
    /* The DYNAMIC setting is translator only, and should never be
       stored.  Thus we always consider it clean.  */
    s->cc_op_dirty = (op != CC_OP_DYNAMIC);
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 317 318 319 320 321
static inline void gen_op_movl_T0_0(void)
{
    tcg_gen_movi_tl(cpu_T[0], 0);
}

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 已提交
322 323 324 325 326 327 328 329 330 331
#ifdef TARGET_X86_64

#define NB_OP_SIZES 4

#else /* !TARGET_X86_64 */

#define NB_OP_SIZES 3

#endif /* !TARGET_X86_64 */

332
#if defined(HOST_WORDS_BIGENDIAN)
B
bellard 已提交
333 334 335 336 337
#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 已提交
338
#else
B
bellard 已提交
339 340 341 342 343
#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 已提交
344
#endif
B
bellard 已提交
345

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
/* 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;
}

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

B
bellard 已提交
392 393
static inline void gen_op_mov_reg_T0(int ot, int reg)
{
394
    gen_op_mov_reg_v(ot, reg, cpu_T[0]);
B
bellard 已提交
395 396 397 398
}

static inline void gen_op_mov_reg_T1(int ot, int reg)
{
399
    gen_op_mov_reg_v(ot, reg, cpu_T[1]);
B
bellard 已提交
400 401 402 403 404
}

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

422
static inline void gen_op_mov_v_reg(int ot, TCGv t0, int reg)
B
bellard 已提交
423
{
424 425 426 427
    if (ot == OT_BYTE && byte_reg_is_xH(reg)) {
        tcg_gen_shri_tl(t0, cpu_regs[reg - 4], 8);
        tcg_gen_ext8u_tl(t0, t0);
    } else {
428
        tcg_gen_mov_tl(t0, cpu_regs[reg]);
B
bellard 已提交
429 430 431
    }
}

432 433 434 435 436
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 已提交
437 438
static inline void gen_op_movl_A0_reg(int reg)
{
439
    tcg_gen_mov_tl(cpu_A0, cpu_regs[reg]);
B
bellard 已提交
440 441 442 443 444
}

static inline void gen_op_addl_A0_im(int32_t val)
{
    tcg_gen_addi_tl(cpu_A0, cpu_A0, val);
B
bellard 已提交
445
#ifdef TARGET_X86_64
B
bellard 已提交
446
    tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffffffff);
B
bellard 已提交
447
#endif
B
bellard 已提交
448
}
B
bellard 已提交
449

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

B
bellard 已提交
467
static inline void gen_op_addl_T0_T1(void)
B
bellard 已提交
468
{
B
bellard 已提交
469 470 471 472 473
    tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
}

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

477
static inline void gen_op_add_reg_im(int size, int reg, int32_t val)
B
bellard 已提交
478
{
479
    switch(size) {
480
    case OT_BYTE:
481
        tcg_gen_addi_tl(cpu_tmp0, cpu_regs[reg], val);
482
        tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], cpu_tmp0, 0, 16);
483
        break;
484
    case OT_WORD:
485 486 487 488 489
        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);
490 491
        break;
#ifdef TARGET_X86_64
492
    case OT_LONG:
493
        tcg_gen_addi_tl(cpu_regs[reg], cpu_regs[reg], val);
494 495 496
        break;
#endif
    }
B
bellard 已提交
497 498
}

499
static inline void gen_op_add_reg_T0(int size, int reg)
B
bellard 已提交
500
{
501
    switch(size) {
502
    case OT_BYTE:
503
        tcg_gen_add_tl(cpu_tmp0, cpu_regs[reg], cpu_T[0]);
504
        tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], cpu_tmp0, 0, 16);
505
        break;
506
    case OT_WORD:
507 508 509 510 511
        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);
512
        break;
B
bellard 已提交
513
#ifdef TARGET_X86_64
514
    case OT_LONG:
515
        tcg_gen_add_tl(cpu_regs[reg], cpu_regs[reg], cpu_T[0]);
516
        break;
B
bellard 已提交
517
#endif
518 519
    }
}
B
bellard 已提交
520 521 522

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

B
bellard 已提交
532 533
static inline void gen_op_movl_A0_seg(int reg)
{
534
    tcg_gen_ld32u_tl(cpu_A0, cpu_env, offsetof(CPUX86State, segs[reg].base) + REG_L_OFFSET);
B
bellard 已提交
535
}
B
bellard 已提交
536

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

B
bellard 已提交
553
#ifdef TARGET_X86_64
B
bellard 已提交
554 555
static inline void gen_op_movq_A0_seg(int reg)
{
556
    tcg_gen_ld_tl(cpu_A0, cpu_env, offsetof(CPUX86State, segs[reg].base));
B
bellard 已提交
557
}
B
bellard 已提交
558

B
bellard 已提交
559 560
static inline void gen_op_addq_A0_seg(int reg)
{
561
    tcg_gen_ld_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, segs[reg].base));
B
bellard 已提交
562 563 564 565 566
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
}

static inline void gen_op_movq_A0_reg(int reg)
{
567
    tcg_gen_mov_tl(cpu_A0, cpu_regs[reg]);
B
bellard 已提交
568 569 570 571
}

static inline void gen_op_addq_A0_reg_sN(int shift, int reg)
{
572 573
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[reg]);
    if (shift != 0)
B
bellard 已提交
574 575 576
        tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, shift);
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
}
B
bellard 已提交
577 578
#endif

B
bellard 已提交
579 580 581 582
static inline void gen_op_lds_T0_A0(int idx)
{
    int mem_index = (idx >> 2) - 1;
    switch(idx & 3) {
583
    case OT_BYTE:
B
bellard 已提交
584 585
        tcg_gen_qemu_ld8s(cpu_T[0], cpu_A0, mem_index);
        break;
586
    case OT_WORD:
B
bellard 已提交
587 588 589
        tcg_gen_qemu_ld16s(cpu_T[0], cpu_A0, mem_index);
        break;
    default:
590
    case OT_LONG:
B
bellard 已提交
591 592 593 594
        tcg_gen_qemu_ld32s(cpu_T[0], cpu_A0, mem_index);
        break;
    }
}
B
bellard 已提交
595

596
static inline void gen_op_ld_v(int idx, TCGv t0, TCGv a0)
B
bellard 已提交
597 598 599
{
    int mem_index = (idx >> 2) - 1;
    switch(idx & 3) {
600
    case OT_BYTE:
601
        tcg_gen_qemu_ld8u(t0, a0, mem_index);
B
bellard 已提交
602
        break;
603
    case OT_WORD:
604
        tcg_gen_qemu_ld16u(t0, a0, mem_index);
B
bellard 已提交
605
        break;
606
    case OT_LONG:
607
        tcg_gen_qemu_ld32u(t0, a0, mem_index);
B
bellard 已提交
608 609
        break;
    default:
610
    case OT_QUAD:
P
pbrook 已提交
611 612
        /* Should never happen on 32-bit targets.  */
#ifdef TARGET_X86_64
613
        tcg_gen_qemu_ld64(t0, a0, mem_index);
P
pbrook 已提交
614
#endif
B
bellard 已提交
615 616 617
        break;
    }
}
B
bellard 已提交
618

619 620 621 622 623 624
/* XXX: always use ldu or lds */
static inline void gen_op_ld_T0_A0(int idx)
{
    gen_op_ld_v(idx, cpu_T[0], cpu_A0);
}

B
bellard 已提交
625 626
static inline void gen_op_ldu_T0_A0(int idx)
{
627
    gen_op_ld_v(idx, cpu_T[0], cpu_A0);
B
bellard 已提交
628
}
B
bellard 已提交
629

B
bellard 已提交
630
static inline void gen_op_ld_T1_A0(int idx)
631 632 633 634 635
{
    gen_op_ld_v(idx, cpu_T[1], cpu_A0);
}

static inline void gen_op_st_v(int idx, TCGv t0, TCGv a0)
B
bellard 已提交
636 637 638
{
    int mem_index = (idx >> 2) - 1;
    switch(idx & 3) {
639
    case OT_BYTE:
640
        tcg_gen_qemu_st8(t0, a0, mem_index);
B
bellard 已提交
641
        break;
642
    case OT_WORD:
643
        tcg_gen_qemu_st16(t0, a0, mem_index);
B
bellard 已提交
644
        break;
645
    case OT_LONG:
646
        tcg_gen_qemu_st32(t0, a0, mem_index);
B
bellard 已提交
647 648
        break;
    default:
649
    case OT_QUAD:
P
pbrook 已提交
650 651
        /* Should never happen on 32-bit targets.  */
#ifdef TARGET_X86_64
652
        tcg_gen_qemu_st64(t0, a0, mem_index);
P
pbrook 已提交
653
#endif
B
bellard 已提交
654 655 656
        break;
    }
}
657

B
bellard 已提交
658 659
static inline void gen_op_st_T0_A0(int idx)
{
660
    gen_op_st_v(idx, cpu_T[0], cpu_A0);
B
bellard 已提交
661
}
662

B
bellard 已提交
663 664
static inline void gen_op_st_T1_A0(int idx)
{
665
    gen_op_st_v(idx, cpu_T[1], cpu_A0);
B
bellard 已提交
666
}
667

B
bellard 已提交
668 669
static inline void gen_jmp_im(target_ulong pc)
{
B
bellard 已提交
670
    tcg_gen_movi_tl(cpu_tmp0, pc);
671
    tcg_gen_st_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, eip));
B
bellard 已提交
672 673
}

B
bellard 已提交
674 675 676 677 678
static inline void gen_string_movl_A0_ESI(DisasContext *s)
{
    int override;

    override = s->override;
B
bellard 已提交
679 680 681
#ifdef TARGET_X86_64
    if (s->aflag == 2) {
        if (override >= 0) {
B
bellard 已提交
682 683
            gen_op_movq_A0_seg(override);
            gen_op_addq_A0_reg_sN(0, R_ESI);
B
bellard 已提交
684
        } else {
B
bellard 已提交
685
            gen_op_movq_A0_reg(R_ESI);
B
bellard 已提交
686 687 688
        }
    } else
#endif
B
bellard 已提交
689 690 691 692 693
    if (s->aflag) {
        /* 32 bit address */
        if (s->addseg && override < 0)
            override = R_DS;
        if (override >= 0) {
B
bellard 已提交
694 695
            gen_op_movl_A0_seg(override);
            gen_op_addl_A0_reg_sN(0, R_ESI);
B
bellard 已提交
696
        } else {
B
bellard 已提交
697
            gen_op_movl_A0_reg(R_ESI);
B
bellard 已提交
698 699 700 701 702
        }
    } else {
        /* 16 address, always override */
        if (override < 0)
            override = R_DS;
B
bellard 已提交
703
        gen_op_movl_A0_reg(R_ESI);
B
bellard 已提交
704
        gen_op_andl_A0_ffff();
705
        gen_op_addl_A0_seg(s, override);
B
bellard 已提交
706 707 708 709 710
    }
}

static inline void gen_string_movl_A0_EDI(DisasContext *s)
{
B
bellard 已提交
711 712
#ifdef TARGET_X86_64
    if (s->aflag == 2) {
B
bellard 已提交
713
        gen_op_movq_A0_reg(R_EDI);
B
bellard 已提交
714 715
    } else
#endif
B
bellard 已提交
716 717
    if (s->aflag) {
        if (s->addseg) {
B
bellard 已提交
718 719
            gen_op_movl_A0_seg(R_ES);
            gen_op_addl_A0_reg_sN(0, R_EDI);
B
bellard 已提交
720
        } else {
B
bellard 已提交
721
            gen_op_movl_A0_reg(R_EDI);
B
bellard 已提交
722 723
        }
    } else {
B
bellard 已提交
724
        gen_op_movl_A0_reg(R_EDI);
B
bellard 已提交
725
        gen_op_andl_A0_ffff();
726
        gen_op_addl_A0_seg(s, R_ES);
B
bellard 已提交
727 728 729
    }
}

730 731
static inline void gen_op_movl_T0_Dshift(int ot) 
{
732
    tcg_gen_ld32s_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, df));
733
    tcg_gen_shli_tl(cpu_T[0], cpu_T[0], ot);
B
bellard 已提交
734 735
};

736
static TCGv gen_ext_tl(TCGv dst, TCGv src, int size, bool sign)
737
{
738
    switch (size) {
739
    case OT_BYTE:
740 741 742 743 744 745
        if (sign) {
            tcg_gen_ext8s_tl(dst, src);
        } else {
            tcg_gen_ext8u_tl(dst, src);
        }
        return dst;
746
    case OT_WORD:
747 748 749 750 751 752 753
        if (sign) {
            tcg_gen_ext16s_tl(dst, src);
        } else {
            tcg_gen_ext16u_tl(dst, src);
        }
        return dst;
#ifdef TARGET_X86_64
754
    case OT_LONG:
755 756 757 758 759 760 761
        if (sign) {
            tcg_gen_ext32s_tl(dst, src);
        } else {
            tcg_gen_ext32u_tl(dst, src);
        }
        return dst;
#endif
762
    default:
763
        return src;
764 765
    }
}
766

767 768 769 770 771
static void gen_extu(int ot, TCGv reg)
{
    gen_ext_tl(reg, reg, ot, false);
}

772 773
static void gen_exts(int ot, TCGv reg)
{
774
    gen_ext_tl(reg, reg, ot, true);
775
}
B
bellard 已提交
776

777 778
static inline void gen_op_jnz_ecx(int size, int label1)
{
779
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
780
    gen_extu(size + 1, cpu_tmp0);
P
pbrook 已提交
781
    tcg_gen_brcondi_tl(TCG_COND_NE, cpu_tmp0, 0, label1);
782 783 784 785
}

static inline void gen_op_jz_ecx(int size, int label1)
{
786
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
787
    gen_extu(size + 1, cpu_tmp0);
P
pbrook 已提交
788
    tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, label1);
789
}
B
bellard 已提交
790

P
pbrook 已提交
791 792 793
static void gen_helper_in_func(int ot, TCGv v, TCGv_i32 n)
{
    switch (ot) {
794 795 796 797 798 799 800 801 802
    case OT_BYTE:
        gen_helper_inb(v, n);
        break;
    case OT_WORD:
        gen_helper_inw(v, n);
        break;
    case OT_LONG:
        gen_helper_inl(v, n);
        break;
P
pbrook 已提交
803 804
    }
}
B
bellard 已提交
805

P
pbrook 已提交
806 807 808
static void gen_helper_out_func(int ot, TCGv_i32 v, TCGv_i32 n)
{
    switch (ot) {
809 810 811 812 813 814 815 816 817
    case OT_BYTE:
        gen_helper_outb(v, n);
        break;
    case OT_WORD:
        gen_helper_outw(v, n);
        break;
    case OT_LONG:
        gen_helper_outl(v, n);
        break;
P
pbrook 已提交
818 819
    }
}
820

821 822
static void gen_check_io(DisasContext *s, int ot, target_ulong cur_eip,
                         uint32_t svm_flags)
823
{
824 825 826 827
    int state_saved;
    target_ulong next_eip;

    state_saved = 0;
828
    if (s->pe && (s->cpl > s->iopl || s->vm86)) {
829
        gen_update_cc_op(s);
B
bellard 已提交
830
        gen_jmp_im(cur_eip);
831
        state_saved = 1;
832
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
833
        switch (ot) {
834
        case OT_BYTE:
B
Blue Swirl 已提交
835 836
            gen_helper_check_iob(cpu_env, cpu_tmp2_i32);
            break;
837
        case OT_WORD:
B
Blue Swirl 已提交
838 839
            gen_helper_check_iow(cpu_env, cpu_tmp2_i32);
            break;
840
        case OT_LONG:
B
Blue Swirl 已提交
841 842
            gen_helper_check_iol(cpu_env, cpu_tmp2_i32);
            break;
P
pbrook 已提交
843
        }
844
    }
B
bellard 已提交
845
    if(s->flags & HF_SVMI_MASK) {
846
        if (!state_saved) {
847
            gen_update_cc_op(s);
848 849 850 851
            gen_jmp_im(cur_eip);
        }
        svm_flags |= (1 << (4 + ot));
        next_eip = s->pc - s->cs_base;
852
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
853 854
        gen_helper_svm_check_io(cpu_env, cpu_tmp2_i32,
                                tcg_const_i32(svm_flags),
P
pbrook 已提交
855
                                tcg_const_i32(next_eip - cur_eip));
856 857 858
    }
}

B
bellard 已提交
859 860 861
static inline void gen_movs(DisasContext *s, int ot)
{
    gen_string_movl_A0_ESI(s);
B
bellard 已提交
862
    gen_op_ld_T0_A0(ot + s->mem_index);
B
bellard 已提交
863
    gen_string_movl_A0_EDI(s);
B
bellard 已提交
864
    gen_op_st_T0_A0(ot + s->mem_index);
865 866 867
    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 已提交
868 869
}

870 871 872 873 874 875 876 877 878 879 880
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]);
}

881 882 883 884 885 886 887
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]);
}

888 889 890 891 892 893 894 895
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]);
896 897
    tcg_gen_neg_tl(cpu_cc_src, cpu_T[0]);
    tcg_gen_movi_tl(cpu_cc_srcT, 0);
898 899
}

900 901
/* compute all eflags to cc_src */
static void gen_compute_eflags(DisasContext *s)
902
{
903
    TCGv zero, dst, src1, src2;
904 905
    int live, dead;

906 907 908
    if (s->cc_op == CC_OP_EFLAGS) {
        return;
    }
909 910 911 912

    TCGV_UNUSED(zero);
    dst = cpu_cc_dst;
    src1 = cpu_cc_src;
913
    src2 = cpu_cc_src2;
914 915 916

    /* Take care to not read values that are not live.  */
    live = cc_op_live[s->cc_op] & ~USES_CC_SRCT;
917
    dead = live ^ (USES_CC_DST | USES_CC_SRC | USES_CC_SRC2);
918 919 920 921 922 923 924 925
    if (dead) {
        zero = tcg_const_tl(0);
        if (dead & USES_CC_DST) {
            dst = zero;
        }
        if (dead & USES_CC_SRC) {
            src1 = zero;
        }
926 927 928
        if (dead & USES_CC_SRC2) {
            src2 = zero;
        }
929 930
    }

931
    gen_update_cc_op(s);
932
    gen_helper_cc_compute_all(cpu_cc_src, dst, src1, src2, cpu_cc_op);
933
    set_cc_op(s, CC_OP_EFLAGS);
934 935 936 937

    if (dead) {
        tcg_temp_free(zero);
    }
938 939
}

940 941 942 943 944 945 946 947 948 949
typedef struct CCPrepare {
    TCGCond cond;
    TCGv reg;
    TCGv reg2;
    target_ulong imm;
    target_ulong mask;
    bool use_reg2;
    bool no_setcond;
} CCPrepare;

950
/* compute eflags.C to reg */
951
static CCPrepare gen_prepare_eflags_c(DisasContext *s, TCGv reg)
952 953
{
    TCGv t0, t1;
954
    int size, shift;
955 956 957

    switch (s->cc_op) {
    case CC_OP_SUBB ... CC_OP_SUBQ:
958
        /* (DATA_TYPE)CC_SRCT < (DATA_TYPE)CC_SRC */
959 960 961 962
        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;
963
        tcg_gen_mov_tl(t0, cpu_cc_srcT);
964 965 966 967 968 969 970 971 972
        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:
973 974
        return (CCPrepare) { .cond = TCG_COND_LTU, .reg = t0,
                             .reg2 = t1, .mask = -1, .use_reg2 = true };
975 976

    case CC_OP_LOGICB ... CC_OP_LOGICQ:
977
        return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
978 979 980

    case CC_OP_INCB ... CC_OP_INCQ:
    case CC_OP_DECB ... CC_OP_DECQ:
981 982
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = -1, .no_setcond = true };
983 984 985 986

    case CC_OP_SHLB ... CC_OP_SHLQ:
        /* (CC_SRC >> (DATA_BITS - 1)) & 1 */
        size = s->cc_op - CC_OP_SHLB;
987 988 989
        shift = (8 << size) - 1;
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = (target_ulong)1 << shift };
990 991

    case CC_OP_MULB ... CC_OP_MULQ:
992 993
        return (CCPrepare) { .cond = TCG_COND_NE,
                             .reg = cpu_cc_src, .mask = -1 };
994

995 996 997 998 999
    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 };

1000 1001 1002 1003 1004
    case CC_OP_ADCX:
    case CC_OP_ADCOX:
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_dst,
                             .mask = -1, .no_setcond = true };

1005 1006 1007
    case CC_OP_EFLAGS:
    case CC_OP_SARB ... CC_OP_SARQ:
        /* CC_SRC & 1 */
1008 1009
        return (CCPrepare) { .cond = TCG_COND_NE,
                             .reg = cpu_cc_src, .mask = CC_C };
1010 1011 1012 1013 1014

    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);
1015 1016
       gen_helper_cc_compute_c(reg, cpu_cc_dst, cpu_cc_src,
                               cpu_cc_src2, cpu_cc_op);
1017 1018
       return (CCPrepare) { .cond = TCG_COND_NE, .reg = reg,
                            .mask = -1, .no_setcond = true };
1019 1020 1021
    }
}

1022
/* compute eflags.P to reg */
1023
static CCPrepare gen_prepare_eflags_p(DisasContext *s, TCGv reg)
1024
{
1025
    gen_compute_eflags(s);
1026 1027
    return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                         .mask = CC_P };
1028 1029 1030
}

/* compute eflags.S to reg */
1031
static CCPrepare gen_prepare_eflags_s(DisasContext *s, TCGv reg)
1032
{
1033 1034 1035 1036 1037
    switch (s->cc_op) {
    case CC_OP_DYNAMIC:
        gen_compute_eflags(s);
        /* FALLTHRU */
    case CC_OP_EFLAGS:
1038 1039 1040
    case CC_OP_ADCX:
    case CC_OP_ADOX:
    case CC_OP_ADCOX:
1041 1042
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = CC_S };
1043 1044 1045 1046
    default:
        {
            int size = (s->cc_op - CC_OP_ADDB) & 3;
            TCGv t0 = gen_ext_tl(reg, cpu_cc_dst, size, true);
1047
            return (CCPrepare) { .cond = TCG_COND_LT, .reg = t0, .mask = -1 };
1048 1049
        }
    }
1050 1051 1052
}

/* compute eflags.O to reg */
1053
static CCPrepare gen_prepare_eflags_o(DisasContext *s, TCGv reg)
1054
{
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
    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 };

    default:
        gen_compute_eflags(s);
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = CC_O };
    }
1066 1067 1068
}

/* compute eflags.Z to reg */
1069
static CCPrepare gen_prepare_eflags_z(DisasContext *s, TCGv reg)
1070
{
1071 1072 1073 1074 1075
    switch (s->cc_op) {
    case CC_OP_DYNAMIC:
        gen_compute_eflags(s);
        /* FALLTHRU */
    case CC_OP_EFLAGS:
1076 1077 1078
    case CC_OP_ADCX:
    case CC_OP_ADOX:
    case CC_OP_ADCOX:
1079 1080
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = CC_Z };
1081 1082 1083 1084
    default:
        {
            int size = (s->cc_op - CC_OP_ADDB) & 3;
            TCGv t0 = gen_ext_tl(reg, cpu_cc_dst, size, false);
1085
            return (CCPrepare) { .cond = TCG_COND_EQ, .reg = t0, .mask = -1 };
1086
        }
1087 1088 1089
    }
}

1090 1091
/* perform a conditional store into register 'reg' according to jump opcode
   value 'b'. In the fast case, T0 is guaranted not to be used. */
1092
static CCPrepare gen_prepare_cc(DisasContext *s, int b, TCGv reg)
1093
{
1094
    int inv, jcc_op, size, cond;
1095
    CCPrepare cc;
1096 1097 1098
    TCGv t0;

    inv = b & 1;
1099
    jcc_op = (b >> 1) & 7;
1100 1101

    switch (s->cc_op) {
1102 1103
    case CC_OP_SUBB ... CC_OP_SUBQ:
        /* We optimize relational operators for the cmp/jcc case.  */
1104 1105 1106
        size = s->cc_op - CC_OP_SUBB;
        switch (jcc_op) {
        case JCC_BE:
1107
            tcg_gen_mov_tl(cpu_tmp4, cpu_cc_srcT);
1108 1109
            gen_extu(size, cpu_tmp4);
            t0 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, false);
1110 1111
            cc = (CCPrepare) { .cond = TCG_COND_LEU, .reg = cpu_tmp4,
                               .reg2 = t0, .mask = -1, .use_reg2 = true };
1112
            break;
1113

1114
        case JCC_L:
1115
            cond = TCG_COND_LT;
1116 1117
            goto fast_jcc_l;
        case JCC_LE:
1118
            cond = TCG_COND_LE;
1119
        fast_jcc_l:
1120
            tcg_gen_mov_tl(cpu_tmp4, cpu_cc_srcT);
1121 1122
            gen_exts(size, cpu_tmp4);
            t0 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, true);
1123 1124
            cc = (CCPrepare) { .cond = cond, .reg = cpu_tmp4,
                               .reg2 = t0, .mask = -1, .use_reg2 = true };
1125
            break;
1126

1127
        default:
1128
            goto slow_jcc;
1129
        }
1130
        break;
1131

1132 1133
    default:
    slow_jcc:
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
        /* 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;
        }
1178
        break;
1179
    }
1180 1181 1182 1183 1184

    if (inv) {
        cc.cond = tcg_invert_cond(cc.cond);
    }
    return cc;
1185 1186
}

1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
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);
}
1221

1222 1223
/* generate a conditional jump to label 'l1' according to jump opcode
   value 'b'. In the fast case, T0 is guaranted not to be used. */
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
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.  */
1242
static inline void gen_jcc1(DisasContext *s, int b, int l1)
1243
{
1244
    CCPrepare cc = gen_prepare_cc(s, b, cpu_T[0]);
1245

1246
    gen_update_cc_op(s);
1247 1248 1249 1250
    if (cc.mask != -1) {
        tcg_gen_andi_tl(cpu_T[0], cc.reg, cc.mask);
        cc.reg = cpu_T[0];
    }
1251
    set_cc_op(s, CC_OP_DYNAMIC);
1252 1253 1254 1255
    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);
1256 1257 1258
    }
}

B
bellard 已提交
1259 1260 1261
/* 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 已提交
1262
{
B
bellard 已提交
1263 1264 1265 1266
    int l1, l2;

    l1 = gen_new_label();
    l2 = gen_new_label();
1267
    gen_op_jnz_ecx(s->aflag, l1);
B
bellard 已提交
1268 1269 1270 1271
    gen_set_label(l2);
    gen_jmp_tb(s, next_eip, 1);
    gen_set_label(l1);
    return l2;
B
bellard 已提交
1272 1273 1274 1275
}

static inline void gen_stos(DisasContext *s, int ot)
{
B
bellard 已提交
1276
    gen_op_mov_TN_reg(OT_LONG, 0, R_EAX);
B
bellard 已提交
1277
    gen_string_movl_A0_EDI(s);
B
bellard 已提交
1278
    gen_op_st_T0_A0(ot + s->mem_index);
1279 1280
    gen_op_movl_T0_Dshift(ot);
    gen_op_add_reg_T0(s->aflag, R_EDI);
B
bellard 已提交
1281 1282 1283 1284 1285
}

static inline void gen_lods(DisasContext *s, int ot)
{
    gen_string_movl_A0_ESI(s);
B
bellard 已提交
1286 1287
    gen_op_ld_T0_A0(ot + s->mem_index);
    gen_op_mov_reg_T0(ot, R_EAX);
1288 1289
    gen_op_movl_T0_Dshift(ot);
    gen_op_add_reg_T0(s->aflag, R_ESI);
B
bellard 已提交
1290 1291 1292 1293 1294
}

static inline void gen_scas(DisasContext *s, int ot)
{
    gen_string_movl_A0_EDI(s);
B
bellard 已提交
1295
    gen_op_ld_T1_A0(ot + s->mem_index);
1296
    gen_op(s, OP_CMPL, ot, R_EAX);
1297 1298
    gen_op_movl_T0_Dshift(ot);
    gen_op_add_reg_T0(s->aflag, R_EDI);
B
bellard 已提交
1299 1300 1301 1302 1303
}

static inline void gen_cmps(DisasContext *s, int ot)
{
    gen_string_movl_A0_EDI(s);
B
bellard 已提交
1304
    gen_op_ld_T1_A0(ot + s->mem_index);
1305 1306
    gen_string_movl_A0_ESI(s);
    gen_op(s, OP_CMPL, ot, OR_TMP0);
1307 1308 1309
    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 已提交
1310 1311 1312 1313
}

static inline void gen_ins(DisasContext *s, int ot)
{
P
pbrook 已提交
1314 1315
    if (use_icount)
        gen_io_start();
B
bellard 已提交
1316
    gen_string_movl_A0_EDI(s);
1317 1318
    /* Note: we must do this dummy write first to be restartable in
       case of page fault. */
B
bellard 已提交
1319
    gen_op_movl_T0_0();
B
bellard 已提交
1320
    gen_op_st_T0_A0(ot + s->mem_index);
1321
    gen_op_mov_TN_reg(OT_WORD, 1, R_EDX);
1322 1323
    tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[1]);
    tcg_gen_andi_i32(cpu_tmp2_i32, cpu_tmp2_i32, 0xffff);
P
pbrook 已提交
1324
    gen_helper_in_func(ot, cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
1325
    gen_op_st_T0_A0(ot + s->mem_index);
1326 1327
    gen_op_movl_T0_Dshift(ot);
    gen_op_add_reg_T0(s->aflag, R_EDI);
P
pbrook 已提交
1328 1329
    if (use_icount)
        gen_io_end();
B
bellard 已提交
1330 1331 1332 1333
}

static inline void gen_outs(DisasContext *s, int ot)
{
P
pbrook 已提交
1334 1335
    if (use_icount)
        gen_io_start();
B
bellard 已提交
1336
    gen_string_movl_A0_ESI(s);
B
bellard 已提交
1337
    gen_op_ld_T0_A0(ot + s->mem_index);
1338 1339

    gen_op_mov_TN_reg(OT_WORD, 1, R_EDX);
1340 1341 1342
    tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[1]);
    tcg_gen_andi_i32(cpu_tmp2_i32, cpu_tmp2_i32, 0xffff);
    tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[0]);
P
pbrook 已提交
1343
    gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
1344

1345 1346
    gen_op_movl_T0_Dshift(ot);
    gen_op_add_reg_T0(s->aflag, R_ESI);
P
pbrook 已提交
1347 1348
    if (use_icount)
        gen_io_end();
B
bellard 已提交
1349 1350 1351 1352 1353 1354
}

/* 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 已提交
1355
                                 target_ulong cur_eip, target_ulong next_eip) \
B
bellard 已提交
1356
{                                                                             \
B
bellard 已提交
1357
    int l2;\
B
bellard 已提交
1358
    gen_update_cc_op(s);                                                      \
B
bellard 已提交
1359
    l2 = gen_jz_ecx_string(s, next_eip);                                      \
B
bellard 已提交
1360
    gen_ ## op(s, ot);                                                        \
1361
    gen_op_add_reg_im(s->aflag, R_ECX, -1);                                   \
B
bellard 已提交
1362 1363 1364
    /* a loop would cause two single step exceptions if ECX = 1               \
       before rep string_insn */                                              \
    if (!s->jmp_opt)                                                          \
1365
        gen_op_jz_ecx(s->aflag, l2);                                          \
B
bellard 已提交
1366 1367 1368 1369 1370
    gen_jmp(s, cur_eip);                                                      \
}

#define GEN_REPZ2(op)                                                         \
static inline void gen_repz_ ## op(DisasContext *s, int ot,                   \
B
bellard 已提交
1371 1372
                                   target_ulong cur_eip,                      \
                                   target_ulong next_eip,                     \
B
bellard 已提交
1373 1374
                                   int nz)                                    \
{                                                                             \
B
bellard 已提交
1375
    int l2;\
B
bellard 已提交
1376
    gen_update_cc_op(s);                                                      \
B
bellard 已提交
1377
    l2 = gen_jz_ecx_string(s, next_eip);                                      \
B
bellard 已提交
1378
    gen_ ## op(s, ot);                                                        \
1379
    gen_op_add_reg_im(s->aflag, R_ECX, -1);                                   \
1380
    gen_update_cc_op(s);                                                      \
1381
    gen_jcc1(s, (JCC_Z << 1) | (nz ^ 1), l2);                                 \
B
bellard 已提交
1382
    if (!s->jmp_opt)                                                          \
1383
        gen_op_jz_ecx(s->aflag, l2);                                          \
B
bellard 已提交
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
    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 已提交
1395 1396 1397
static void gen_helper_fp_arith_ST0_FT0(int op)
{
    switch (op) {
B
Blue Swirl 已提交
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
    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 已提交
1422 1423
    }
}
B
bellard 已提交
1424 1425

/* NOTE the exception in "r" op ordering */
P
pbrook 已提交
1426 1427 1428 1429
static void gen_helper_fp_arith_STN_ST0(int op, int opreg)
{
    TCGv_i32 tmp = tcg_const_i32(opreg);
    switch (op) {
B
Blue Swirl 已提交
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
    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 已提交
1448 1449
    }
}
B
bellard 已提交
1450 1451 1452 1453 1454

/* 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 已提交
1455
        gen_op_mov_TN_reg(ot, 0, d);
B
bellard 已提交
1456
    } else {
B
bellard 已提交
1457
        gen_op_ld_T0_A0(ot + s1->mem_index);
B
bellard 已提交
1458 1459 1460
    }
    switch(op) {
    case OP_ADCL:
1461
        gen_compute_eflags_c(s1, cpu_tmp4);
B
bellard 已提交
1462 1463 1464 1465 1466 1467
        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);
        if (d != OR_TMP0)
            gen_op_mov_reg_T0(ot, d);
        else
            gen_op_st_T0_A0(ot + s1->mem_index);
1468 1469
        gen_op_update3_cc(cpu_tmp4);
        set_cc_op(s1, CC_OP_ADCB + ot);
B
bellard 已提交
1470
        break;
B
bellard 已提交
1471
    case OP_SBBL:
1472
        gen_compute_eflags_c(s1, cpu_tmp4);
B
bellard 已提交
1473 1474 1475
        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);
        if (d != OR_TMP0)
B
bellard 已提交
1476
            gen_op_mov_reg_T0(ot, d);
B
bellard 已提交
1477 1478
        else
            gen_op_st_T0_A0(ot + s1->mem_index);
1479 1480
        gen_op_update3_cc(cpu_tmp4);
        set_cc_op(s1, CC_OP_SBBB + ot);
B
bellard 已提交
1481
        break;
B
bellard 已提交
1482 1483
    case OP_ADDL:
        gen_op_addl_T0_T1();
B
bellard 已提交
1484 1485 1486 1487 1488
        if (d != OR_TMP0)
            gen_op_mov_reg_T0(ot, d);
        else
            gen_op_st_T0_A0(ot + s1->mem_index);
        gen_op_update2_cc();
1489
        set_cc_op(s1, CC_OP_ADDB + ot);
B
bellard 已提交
1490 1491
        break;
    case OP_SUBL:
1492
        tcg_gen_mov_tl(cpu_cc_srcT, cpu_T[0]);
B
bellard 已提交
1493
        tcg_gen_sub_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
B
bellard 已提交
1494 1495 1496 1497 1498
        if (d != OR_TMP0)
            gen_op_mov_reg_T0(ot, d);
        else
            gen_op_st_T0_A0(ot + s1->mem_index);
        gen_op_update2_cc();
1499
        set_cc_op(s1, CC_OP_SUBB + ot);
B
bellard 已提交
1500 1501 1502
        break;
    default:
    case OP_ANDL:
B
bellard 已提交
1503
        tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
B
bellard 已提交
1504 1505 1506 1507 1508
        if (d != OR_TMP0)
            gen_op_mov_reg_T0(ot, d);
        else
            gen_op_st_T0_A0(ot + s1->mem_index);
        gen_op_update1_cc();
1509
        set_cc_op(s1, CC_OP_LOGICB + ot);
B
bellard 已提交
1510
        break;
B
bellard 已提交
1511
    case OP_ORL:
B
bellard 已提交
1512
        tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
B
bellard 已提交
1513 1514 1515 1516 1517
        if (d != OR_TMP0)
            gen_op_mov_reg_T0(ot, d);
        else
            gen_op_st_T0_A0(ot + s1->mem_index);
        gen_op_update1_cc();
1518
        set_cc_op(s1, CC_OP_LOGICB + ot);
B
bellard 已提交
1519
        break;
B
bellard 已提交
1520
    case OP_XORL:
B
bellard 已提交
1521
        tcg_gen_xor_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
B
bellard 已提交
1522 1523 1524 1525 1526
        if (d != OR_TMP0)
            gen_op_mov_reg_T0(ot, d);
        else
            gen_op_st_T0_A0(ot + s1->mem_index);
        gen_op_update1_cc();
1527
        set_cc_op(s1, CC_OP_LOGICB + ot);
B
bellard 已提交
1528 1529
        break;
    case OP_CMPL:
1530
        tcg_gen_mov_tl(cpu_cc_src, cpu_T[1]);
1531
        tcg_gen_mov_tl(cpu_cc_srcT, cpu_T[0]);
1532
        tcg_gen_sub_tl(cpu_cc_dst, cpu_T[0], cpu_T[1]);
1533
        set_cc_op(s1, CC_OP_SUBB + ot);
B
bellard 已提交
1534 1535
        break;
    }
1536 1537
}

B
bellard 已提交
1538 1539 1540 1541
/* if d == OR_TMP0, it means memory operand (address in A0) */
static void gen_inc(DisasContext *s1, int ot, int d, int c)
{
    if (d != OR_TMP0)
B
bellard 已提交
1542
        gen_op_mov_TN_reg(ot, 0, d);
B
bellard 已提交
1543
    else
B
bellard 已提交
1544
        gen_op_ld_T0_A0(ot + s1->mem_index);
1545
    gen_compute_eflags_c(s1, cpu_cc_src);
B
bellard 已提交
1546
    if (c > 0) {
1547
        tcg_gen_addi_tl(cpu_T[0], cpu_T[0], 1);
1548
        set_cc_op(s1, CC_OP_INCB + ot);
B
bellard 已提交
1549
    } else {
1550
        tcg_gen_addi_tl(cpu_T[0], cpu_T[0], -1);
1551
        set_cc_op(s1, CC_OP_DECB + ot);
B
bellard 已提交
1552 1553
    }
    if (d != OR_TMP0)
B
bellard 已提交
1554
        gen_op_mov_reg_T0(ot, d);
B
bellard 已提交
1555
    else
B
bellard 已提交
1556
        gen_op_st_T0_A0(ot + s1->mem_index);
B
bellard 已提交
1557
    tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
B
bellard 已提交
1558 1559
}

1560 1561
static void gen_shift_rm_T1(DisasContext *s, int ot, int op1, 
                            int is_right, int is_arith)
B
bellard 已提交
1562
{
1563 1564
    target_ulong mask;
    int shift_label;
1565
    TCGv t0, t1, t2;
1566

1567
    if (ot == OT_QUAD) {
1568
        mask = 0x3f;
1569
    } else {
1570
        mask = 0x1f;
1571
    }
1572

1573
    /* load */
1574
    if (op1 == OR_TMP0) {
1575
        gen_op_ld_T0_A0(ot + s->mem_index);
1576
    } else {
1577
        gen_op_mov_TN_reg(ot, 0, op1);
1578
    }
1579

1580 1581 1582
    t0 = tcg_temp_local_new();
    t1 = tcg_temp_local_new();
    t2 = tcg_temp_local_new();
1583

1584
    tcg_gen_andi_tl(t2, cpu_T[1], mask);
1585 1586 1587

    if (is_right) {
        if (is_arith) {
B
bellard 已提交
1588
            gen_exts(ot, cpu_T[0]);
1589 1590
            tcg_gen_mov_tl(t0, cpu_T[0]);
            tcg_gen_sar_tl(cpu_T[0], cpu_T[0], t2);
1591
        } else {
B
bellard 已提交
1592
            gen_extu(ot, cpu_T[0]);
1593 1594
            tcg_gen_mov_tl(t0, cpu_T[0]);
            tcg_gen_shr_tl(cpu_T[0], cpu_T[0], t2);
1595 1596
        }
    } else {
1597 1598
        tcg_gen_mov_tl(t0, cpu_T[0]);
        tcg_gen_shl_tl(cpu_T[0], cpu_T[0], t2);
1599 1600 1601
    }

    /* store */
1602
    if (op1 == OR_TMP0) {
1603
        gen_op_st_T0_A0(ot + s->mem_index);
1604
    } else {
1605
        gen_op_mov_reg_T0(ot, op1);
1606 1607
    }

1608
    /* Update eflags data because we cannot predict flags afterward.  */
1609
    gen_update_cc_op(s);
1610
    set_cc_op(s, CC_OP_DYNAMIC);
1611

1612
    tcg_gen_mov_tl(t1, cpu_T[0]);
1613

1614
    shift_label = gen_new_label();
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
    tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, shift_label);

    tcg_gen_addi_tl(t2, t2, -1);
    tcg_gen_mov_tl(cpu_cc_dst, t1);

    if (is_right) {
        if (is_arith) {
            tcg_gen_sar_tl(cpu_cc_src, t0, t2);
        } else {
            tcg_gen_shr_tl(cpu_cc_src, t0, t2);
        }
    } else {
        tcg_gen_shl_tl(cpu_cc_src, t0, t2);
    }
1629

1630
    if (is_right) {
1631
        tcg_gen_movi_i32(cpu_cc_op, CC_OP_SARB + ot);
1632
    } else {
1633
        tcg_gen_movi_i32(cpu_cc_op, CC_OP_SHLB + ot);
1634 1635
    }

1636
    gen_set_label(shift_label);
1637 1638 1639

    tcg_temp_free(t0);
    tcg_temp_free(t1);
1640
    tcg_temp_free(t2);
1641 1642
}

B
bellard 已提交
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
static void gen_shift_rm_im(DisasContext *s, int ot, int op1, int op2,
                            int is_right, int is_arith)
{
    int mask;
    
    if (ot == OT_QUAD)
        mask = 0x3f;
    else
        mask = 0x1f;

    /* load */
    if (op1 == OR_TMP0)
        gen_op_ld_T0_A0(ot + s->mem_index);
    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 已提交
1664
                tcg_gen_sari_tl(cpu_tmp4, cpu_T[0], op2 - 1);
B
bellard 已提交
1665 1666 1667
                tcg_gen_sari_tl(cpu_T[0], cpu_T[0], op2);
            } else {
                gen_extu(ot, cpu_T[0]);
B
bellard 已提交
1668
                tcg_gen_shri_tl(cpu_tmp4, cpu_T[0], op2 - 1);
B
bellard 已提交
1669 1670 1671
                tcg_gen_shri_tl(cpu_T[0], cpu_T[0], op2);
            }
        } else {
B
bellard 已提交
1672
            tcg_gen_shli_tl(cpu_tmp4, cpu_T[0], op2 - 1);
B
bellard 已提交
1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684
            tcg_gen_shli_tl(cpu_T[0], cpu_T[0], op2);
        }
    }

    /* store */
    if (op1 == OR_TMP0)
        gen_op_st_T0_A0(ot + s->mem_index);
    else
        gen_op_mov_reg_T0(ot, op1);
        
    /* update eflags if non zero shift */
    if (op2 != 0) {
B
bellard 已提交
1685
        tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
B
bellard 已提交
1686
        tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
1687
        set_cc_op(s, (is_right ? CC_OP_SARB : CC_OP_SHLB) + ot);
B
bellard 已提交
1688 1689 1690
    }
}

1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
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);
}

static void gen_rot_rm_T1(DisasContext *s, int ot, int op1, 
                          int is_right)
{
    target_ulong mask;
    int label1, label2, data_bits;
1704 1705 1706
    TCGv t0, t1, t2, a0;

    /* XXX: inefficient, but we must use local temps */
P
pbrook 已提交
1707 1708 1709 1710
    t0 = tcg_temp_local_new();
    t1 = tcg_temp_local_new();
    t2 = tcg_temp_local_new();
    a0 = tcg_temp_local_new();
1711

1712 1713 1714 1715 1716 1717
    if (ot == OT_QUAD)
        mask = 0x3f;
    else
        mask = 0x1f;

    /* load */
1718 1719 1720 1721 1722 1723
    if (op1 == OR_TMP0) {
        tcg_gen_mov_tl(a0, cpu_A0);
        gen_op_ld_v(ot + s->mem_index, t0, a0);
    } else {
        gen_op_mov_v_reg(ot, t0, op1);
    }
1724

1725 1726 1727
    tcg_gen_mov_tl(t1, cpu_T[1]);

    tcg_gen_andi_tl(t1, t1, mask);
1728 1729 1730 1731

    /* Must test zero case to avoid using undefined behaviour in TCG
       shifts. */
    label1 = gen_new_label();
1732
    tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, label1);
1733 1734
    
    if (ot <= OT_WORD)
1735
        tcg_gen_andi_tl(cpu_tmp0, t1, (1 << (3 + ot)) - 1);
1736
    else
1737
        tcg_gen_mov_tl(cpu_tmp0, t1);
1738
    
1739 1740
    gen_extu(ot, t0);
    tcg_gen_mov_tl(t2, t0);
1741 1742 1743 1744 1745

    data_bits = 8 << ot;
    /* XXX: rely on behaviour of shifts when operand 2 overflows (XXX:
       fix TCG definition) */
    if (is_right) {
1746
        tcg_gen_shr_tl(cpu_tmp4, t0, cpu_tmp0);
1747
        tcg_gen_subfi_tl(cpu_tmp0, data_bits, cpu_tmp0);
1748
        tcg_gen_shl_tl(t0, t0, cpu_tmp0);
1749
    } else {
1750
        tcg_gen_shl_tl(cpu_tmp4, t0, cpu_tmp0);
1751
        tcg_gen_subfi_tl(cpu_tmp0, data_bits, cpu_tmp0);
1752
        tcg_gen_shr_tl(t0, t0, cpu_tmp0);
1753
    }
1754
    tcg_gen_or_tl(t0, t0, cpu_tmp4);
1755 1756 1757

    gen_set_label(label1);
    /* store */
1758 1759 1760 1761 1762
    if (op1 == OR_TMP0) {
        gen_op_st_v(ot + s->mem_index, t0, a0);
    } else {
        gen_op_mov_reg_v(ot, op1, t0);
    }
1763
    
1764
    /* update eflags.  It is needed anyway most of the time, do it always.  */
1765
    gen_compute_eflags(s);
1766
    assert(s->cc_op == CC_OP_EFLAGS);
1767 1768

    label2 = gen_new_label();
1769
    tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, label2);
1770 1771

    tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~(CC_O | CC_C));
1772
    tcg_gen_xor_tl(cpu_tmp0, t2, t0);
1773 1774 1775 1776
    tcg_gen_lshift(cpu_tmp0, cpu_tmp0, 11 - (data_bits - 1));
    tcg_gen_andi_tl(cpu_tmp0, cpu_tmp0, CC_O);
    tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, cpu_tmp0);
    if (is_right) {
1777
        tcg_gen_shri_tl(t0, t0, data_bits - 1);
1778
    }
1779 1780
    tcg_gen_andi_tl(t0, t0, CC_C);
    tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, t0);
1781

1782
    gen_set_label(label2);
1783 1784 1785 1786 1787

    tcg_temp_free(t0);
    tcg_temp_free(t1);
    tcg_temp_free(t2);
    tcg_temp_free(a0);
1788 1789
}

M
malc 已提交
1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841
static void gen_rot_rm_im(DisasContext *s, int ot, int op1, int op2,
                          int is_right)
{
    int mask;
    int data_bits;
    TCGv t0, t1, a0;

    /* XXX: inefficient, but we must use local temps */
    t0 = tcg_temp_local_new();
    t1 = tcg_temp_local_new();
    a0 = tcg_temp_local_new();

    if (ot == OT_QUAD)
        mask = 0x3f;
    else
        mask = 0x1f;

    /* load */
    if (op1 == OR_TMP0) {
        tcg_gen_mov_tl(a0, cpu_A0);
        gen_op_ld_v(ot + s->mem_index, t0, a0);
    } else {
        gen_op_mov_v_reg(ot, t0, op1);
    }

    gen_extu(ot, t0);
    tcg_gen_mov_tl(t1, t0);

    op2 &= mask;
    data_bits = 8 << ot;
    if (op2 != 0) {
        int shift = op2 & ((1 << (3 + ot)) - 1);
        if (is_right) {
            tcg_gen_shri_tl(cpu_tmp4, t0, shift);
            tcg_gen_shli_tl(t0, t0, data_bits - shift);
        }
        else {
            tcg_gen_shli_tl(cpu_tmp4, t0, shift);
            tcg_gen_shri_tl(t0, t0, data_bits - shift);
        }
        tcg_gen_or_tl(t0, t0, cpu_tmp4);
    }

    /* store */
    if (op1 == OR_TMP0) {
        gen_op_st_v(ot + s->mem_index, t0, a0);
    } else {
        gen_op_mov_reg_v(ot, op1, t0);
    }

    if (op2 != 0) {
        /* update eflags */
1842
        gen_compute_eflags(s);
1843
        assert(s->cc_op == CC_OP_EFLAGS);
1844

M
malc 已提交
1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861
        tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~(CC_O | CC_C));
        tcg_gen_xor_tl(cpu_tmp0, t1, t0);
        tcg_gen_lshift(cpu_tmp0, cpu_tmp0, 11 - (data_bits - 1));
        tcg_gen_andi_tl(cpu_tmp0, cpu_tmp0, CC_O);
        tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, cpu_tmp0);
        if (is_right) {
            tcg_gen_shri_tl(t0, t0, data_bits - 1);
        }
        tcg_gen_andi_tl(t0, t0, CC_C);
        tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, t0);
    }

    tcg_temp_free(t0);
    tcg_temp_free(t1);
    tcg_temp_free(a0);
}

1862 1863 1864 1865
/* XXX: add faster immediate = 1 case */
static void gen_rotc_rm_T1(DisasContext *s, int ot, int op1, 
                           int is_right)
{
1866
    gen_compute_eflags(s);
1867
    assert(s->cc_op == CC_OP_EFLAGS);
1868 1869 1870 1871 1872 1873 1874

    /* load */
    if (op1 == OR_TMP0)
        gen_op_ld_T0_A0(ot + s->mem_index);
    else
        gen_op_mov_TN_reg(ot, 0, op1);
    
P
pbrook 已提交
1875 1876
    if (is_right) {
        switch (ot) {
1877
        case OT_BYTE:
1878 1879
            gen_helper_rcrb(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
1880
        case OT_WORD:
1881 1882
            gen_helper_rcrw(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
1883
        case OT_LONG:
1884 1885
            gen_helper_rcrl(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1886
#ifdef TARGET_X86_64
1887
        case OT_QUAD:
1888 1889
            gen_helper_rcrq(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1890 1891 1892 1893
#endif
        }
    } else {
        switch (ot) {
1894
        case OT_BYTE:
1895 1896
            gen_helper_rclb(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
1897
        case OT_WORD:
1898 1899
            gen_helper_rclw(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
1900
        case OT_LONG:
1901 1902
            gen_helper_rcll(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1903
#ifdef TARGET_X86_64
1904
        case OT_QUAD:
1905 1906
            gen_helper_rclq(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1907 1908 1909
#endif
        }
    }
1910 1911 1912 1913 1914 1915 1916 1917
    /* store */
    if (op1 == OR_TMP0)
        gen_op_st_T0_A0(ot + s->mem_index);
    else
        gen_op_mov_reg_T0(ot, op1);
}

/* XXX: add faster immediate case */
P
Paolo Bonzini 已提交
1918 1919
static void gen_shiftd_rm_T1(DisasContext *s, int ot, int op1,
                             int is_right, TCGv count)
1920 1921 1922
{
    int label1, label2, data_bits;
    target_ulong mask;
1923 1924
    TCGv t0, t1, t2, a0;

P
pbrook 已提交
1925 1926 1927 1928
    t0 = tcg_temp_local_new();
    t1 = tcg_temp_local_new();
    t2 = tcg_temp_local_new();
    a0 = tcg_temp_local_new();
1929 1930 1931 1932 1933 1934 1935

    if (ot == OT_QUAD)
        mask = 0x3f;
    else
        mask = 0x1f;

    /* load */
1936 1937 1938 1939 1940 1941
    if (op1 == OR_TMP0) {
        tcg_gen_mov_tl(a0, cpu_A0);
        gen_op_ld_v(ot + s->mem_index, t0, a0);
    } else {
        gen_op_mov_v_reg(ot, t0, op1);
    }
1942

P
Paolo Bonzini 已提交
1943
    tcg_gen_andi_tl(t2, count, mask);
1944 1945
    tcg_gen_mov_tl(t1, cpu_T[1]);

1946 1947 1948
    /* Must test zero case to avoid using undefined behaviour in TCG
       shifts. */
    label1 = gen_new_label();
1949
    tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, label1);
1950
    
1951
    tcg_gen_addi_tl(cpu_tmp5, t2, -1);
1952 1953 1954
    if (ot == OT_WORD) {
        /* Note: we implement the Intel behaviour for shift count > 16 */
        if (is_right) {
1955 1956 1957 1958
            tcg_gen_andi_tl(t0, t0, 0xffff);
            tcg_gen_shli_tl(cpu_tmp0, t1, 16);
            tcg_gen_or_tl(t0, t0, cpu_tmp0);
            tcg_gen_ext32u_tl(t0, t0);
1959

1960
            tcg_gen_shr_tl(cpu_tmp4, t0, cpu_tmp5);
1961 1962
            
            /* only needed if count > 16, but a test would complicate */
1963
            tcg_gen_subfi_tl(cpu_tmp5, 32, t2);
1964
            tcg_gen_shl_tl(cpu_tmp0, t0, cpu_tmp5);
1965

1966
            tcg_gen_shr_tl(t0, t0, t2);
1967

1968
            tcg_gen_or_tl(t0, t0, cpu_tmp0);
1969 1970
        } else {
            /* XXX: not optimal */
1971 1972 1973 1974
            tcg_gen_andi_tl(t0, t0, 0xffff);
            tcg_gen_shli_tl(t1, t1, 16);
            tcg_gen_or_tl(t1, t1, t0);
            tcg_gen_ext32u_tl(t1, t1);
1975
            
1976
            tcg_gen_shl_tl(cpu_tmp4, t0, cpu_tmp5);
1977
            tcg_gen_subfi_tl(cpu_tmp0, 32, cpu_tmp5);
1978 1979
            tcg_gen_shr_tl(cpu_tmp5, t1, cpu_tmp0);
            tcg_gen_or_tl(cpu_tmp4, cpu_tmp4, cpu_tmp5);
1980

1981
            tcg_gen_shl_tl(t0, t0, t2);
1982
            tcg_gen_subfi_tl(cpu_tmp5, 32, t2);
1983 1984
            tcg_gen_shr_tl(t1, t1, cpu_tmp5);
            tcg_gen_or_tl(t0, t0, t1);
1985 1986 1987 1988 1989
        }
    } else {
        data_bits = 8 << ot;
        if (is_right) {
            if (ot == OT_LONG)
1990
                tcg_gen_ext32u_tl(t0, t0);
1991

1992
            tcg_gen_shr_tl(cpu_tmp4, t0, cpu_tmp5);
1993

1994
            tcg_gen_shr_tl(t0, t0, t2);
1995
            tcg_gen_subfi_tl(cpu_tmp5, data_bits, t2);
1996 1997
            tcg_gen_shl_tl(t1, t1, cpu_tmp5);
            tcg_gen_or_tl(t0, t0, t1);
1998 1999 2000
            
        } else {
            if (ot == OT_LONG)
2001
                tcg_gen_ext32u_tl(t1, t1);
2002

2003
            tcg_gen_shl_tl(cpu_tmp4, t0, cpu_tmp5);
2004
            
2005
            tcg_gen_shl_tl(t0, t0, t2);
2006
            tcg_gen_subfi_tl(cpu_tmp5, data_bits, t2);
2007 2008
            tcg_gen_shr_tl(t1, t1, cpu_tmp5);
            tcg_gen_or_tl(t0, t0, t1);
2009 2010
        }
    }
2011
    tcg_gen_mov_tl(t1, cpu_tmp4);
2012 2013 2014

    gen_set_label(label1);
    /* store */
2015 2016 2017 2018 2019
    if (op1 == OR_TMP0) {
        gen_op_st_v(ot + s->mem_index, t0, a0);
    } else {
        gen_op_mov_reg_v(ot, op1, t0);
    }
2020
    
2021
    /* Update eflags data because we cannot predict flags afterward.  */
2022
    gen_update_cc_op(s);
2023
    set_cc_op(s, CC_OP_DYNAMIC);
2024 2025

    label2 = gen_new_label();
2026
    tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, label2);
2027

2028 2029
    tcg_gen_mov_tl(cpu_cc_src, t1);
    tcg_gen_mov_tl(cpu_cc_dst, t0);
2030 2031 2032 2033 2034 2035
    if (is_right) {
        tcg_gen_movi_i32(cpu_cc_op, CC_OP_SARB + ot);
    } else {
        tcg_gen_movi_i32(cpu_cc_op, CC_OP_SHLB + ot);
    }
    gen_set_label(label2);
2036 2037 2038 2039 2040

    tcg_temp_free(t0);
    tcg_temp_free(t1);
    tcg_temp_free(t2);
    tcg_temp_free(a0);
2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070
}

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 已提交
2071 2072 2073 2074
}

static void gen_shifti(DisasContext *s1, int op, int ot, int d, int c)
{
B
bellard 已提交
2075
    switch(op) {
M
malc 已提交
2076 2077 2078 2079 2080 2081
    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 已提交
2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097
    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 已提交
2098 2099
}

2100 2101
static void gen_lea_modrm(CPUX86State *env, DisasContext *s, int modrm,
                          int *reg_ptr, int *offset_ptr)
B
bellard 已提交
2102
{
B
bellard 已提交
2103
    target_long disp;
B
bellard 已提交
2104
    int havesib;
B
bellard 已提交
2105
    int base;
B
bellard 已提交
2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123
    int index;
    int scale;
    int opreg;
    int mod, rm, code, override, must_add_seg;

    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;
        index = 0;
        scale = 0;
2124

B
bellard 已提交
2125 2126
        if (base == 4) {
            havesib = 1;
2127
            code = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
2128
            scale = (code >> 6) & 3;
B
bellard 已提交
2129 2130
            index = ((code >> 3) & 7) | REX_X(s);
            base = (code & 7);
B
bellard 已提交
2131
        }
B
bellard 已提交
2132
        base |= REX_B(s);
B
bellard 已提交
2133 2134 2135

        switch (mod) {
        case 0:
B
bellard 已提交
2136
            if ((base & 7) == 5) {
B
bellard 已提交
2137
                base = -1;
2138
                disp = (int32_t)cpu_ldl_code(env, s->pc);
B
bellard 已提交
2139
                s->pc += 4;
B
bellard 已提交
2140 2141 2142
                if (CODE64(s) && !havesib) {
                    disp += s->pc + s->rip_offset;
                }
B
bellard 已提交
2143 2144 2145 2146 2147
            } else {
                disp = 0;
            }
            break;
        case 1:
2148
            disp = (int8_t)cpu_ldub_code(env, s->pc++);
B
bellard 已提交
2149 2150 2151
            break;
        default:
        case 2:
2152
            disp = (int32_t)cpu_ldl_code(env, s->pc);
B
bellard 已提交
2153 2154 2155
            s->pc += 4;
            break;
        }
2156

B
bellard 已提交
2157 2158 2159 2160
        if (base >= 0) {
            /* for correct popl handling with esp */
            if (base == 4 && s->popl_esp_hack)
                disp += s->popl_esp_hack;
B
bellard 已提交
2161 2162
#ifdef TARGET_X86_64
            if (s->aflag == 2) {
B
bellard 已提交
2163
                gen_op_movq_A0_reg(base);
B
bellard 已提交
2164
                if (disp != 0) {
B
bellard 已提交
2165
                    gen_op_addq_A0_im(disp);
B
bellard 已提交
2166
                }
2167
            } else
B
bellard 已提交
2168 2169
#endif
            {
B
bellard 已提交
2170
                gen_op_movl_A0_reg(base);
B
bellard 已提交
2171 2172 2173
                if (disp != 0)
                    gen_op_addl_A0_im(disp);
            }
B
bellard 已提交
2174
        } else {
B
bellard 已提交
2175 2176
#ifdef TARGET_X86_64
            if (s->aflag == 2) {
B
bellard 已提交
2177
                gen_op_movq_A0_im(disp);
2178
            } else
B
bellard 已提交
2179 2180 2181 2182
#endif
            {
                gen_op_movl_A0_im(disp);
            }
B
bellard 已提交
2183
        }
2184 2185
        /* index == 4 means no index */
        if (havesib && (index != 4)) {
B
bellard 已提交
2186 2187
#ifdef TARGET_X86_64
            if (s->aflag == 2) {
B
bellard 已提交
2188
                gen_op_addq_A0_reg_sN(scale, index);
2189
            } else
B
bellard 已提交
2190 2191
#endif
            {
B
bellard 已提交
2192
                gen_op_addl_A0_reg_sN(scale, index);
B
bellard 已提交
2193
            }
B
bellard 已提交
2194 2195 2196 2197 2198 2199 2200 2201
        }
        if (must_add_seg) {
            if (override < 0) {
                if (base == R_EBP || base == R_ESP)
                    override = R_SS;
                else
                    override = R_DS;
            }
B
bellard 已提交
2202 2203
#ifdef TARGET_X86_64
            if (s->aflag == 2) {
B
bellard 已提交
2204
                gen_op_addq_A0_seg(override);
2205
            } else
B
bellard 已提交
2206 2207
#endif
            {
2208
                gen_op_addl_A0_seg(s, override);
B
bellard 已提交
2209
            }
B
bellard 已提交
2210 2211 2212 2213 2214
        }
    } else {
        switch (mod) {
        case 0:
            if (rm == 6) {
2215
                disp = cpu_lduw_code(env, s->pc);
B
bellard 已提交
2216 2217 2218 2219 2220 2221 2222 2223 2224
                s->pc += 2;
                gen_op_movl_A0_im(disp);
                rm = 0; /* avoid SS override */
                goto no_rm;
            } else {
                disp = 0;
            }
            break;
        case 1:
2225
            disp = (int8_t)cpu_ldub_code(env, s->pc++);
B
bellard 已提交
2226 2227 2228
            break;
        default:
        case 2:
2229
            disp = cpu_lduw_code(env, s->pc);
B
bellard 已提交
2230 2231 2232 2233 2234
            s->pc += 2;
            break;
        }
        switch(rm) {
        case 0:
B
bellard 已提交
2235 2236
            gen_op_movl_A0_reg(R_EBX);
            gen_op_addl_A0_reg_sN(0, R_ESI);
B
bellard 已提交
2237 2238
            break;
        case 1:
B
bellard 已提交
2239 2240
            gen_op_movl_A0_reg(R_EBX);
            gen_op_addl_A0_reg_sN(0, R_EDI);
B
bellard 已提交
2241 2242
            break;
        case 2:
B
bellard 已提交
2243 2244
            gen_op_movl_A0_reg(R_EBP);
            gen_op_addl_A0_reg_sN(0, R_ESI);
B
bellard 已提交
2245 2246
            break;
        case 3:
B
bellard 已提交
2247 2248
            gen_op_movl_A0_reg(R_EBP);
            gen_op_addl_A0_reg_sN(0, R_EDI);
B
bellard 已提交
2249 2250
            break;
        case 4:
B
bellard 已提交
2251
            gen_op_movl_A0_reg(R_ESI);
B
bellard 已提交
2252 2253
            break;
        case 5:
B
bellard 已提交
2254
            gen_op_movl_A0_reg(R_EDI);
B
bellard 已提交
2255 2256
            break;
        case 6:
B
bellard 已提交
2257
            gen_op_movl_A0_reg(R_EBP);
B
bellard 已提交
2258 2259 2260
            break;
        default:
        case 7:
B
bellard 已提交
2261
            gen_op_movl_A0_reg(R_EBX);
B
bellard 已提交
2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274
            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;
            }
2275
            gen_op_addl_A0_seg(s, override);
B
bellard 已提交
2276 2277 2278 2279 2280 2281 2282 2283 2284
        }
    }

    opreg = OR_A0;
    disp = 0;
    *reg_ptr = opreg;
    *offset_ptr = disp;
}

2285
static void gen_nop_modrm(CPUX86State *env, DisasContext *s, int modrm)
B
bellard 已提交
2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296
{
    int mod, rm, base, code;

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

    if (s->aflag) {

        base = rm;
2297

B
bellard 已提交
2298
        if (base == 4) {
2299
            code = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
2300 2301
            base = (code & 7);
        }
2302

B
bellard 已提交
2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334
        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 已提交
2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345
/* 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) {
2346 2347
#ifdef TARGET_X86_64
        if (CODE64(s)) {
B
bellard 已提交
2348
            gen_op_addq_A0_seg(override);
2349
        } else
2350 2351
#endif
        {
2352
            gen_op_addl_A0_seg(s, override);
2353
        }
B
bellard 已提交
2354 2355 2356
    }
}

B
balrog 已提交
2357
/* generate modrm memory load or store of 'reg'. TMP0 is used if reg ==
B
bellard 已提交
2358
   OR_TMP0 */
2359 2360
static void gen_ldst_modrm(CPUX86State *env, DisasContext *s, int modrm,
                           int ot, int reg, int is_store)
B
bellard 已提交
2361 2362 2363 2364
{
    int mod, rm, opreg, disp;

    mod = (modrm >> 6) & 3;
B
bellard 已提交
2365
    rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
2366 2367 2368
    if (mod == 3) {
        if (is_store) {
            if (reg != OR_TMP0)
B
bellard 已提交
2369 2370
                gen_op_mov_TN_reg(ot, 0, reg);
            gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
2371
        } else {
B
bellard 已提交
2372
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
2373
            if (reg != OR_TMP0)
B
bellard 已提交
2374
                gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
2375 2376
        }
    } else {
2377
        gen_lea_modrm(env, s, modrm, &opreg, &disp);
B
bellard 已提交
2378 2379
        if (is_store) {
            if (reg != OR_TMP0)
B
bellard 已提交
2380 2381
                gen_op_mov_TN_reg(ot, 0, reg);
            gen_op_st_T0_A0(ot + s->mem_index);
B
bellard 已提交
2382
        } else {
B
bellard 已提交
2383
            gen_op_ld_T0_A0(ot + s->mem_index);
B
bellard 已提交
2384
            if (reg != OR_TMP0)
B
bellard 已提交
2385
                gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
2386 2387 2388 2389
        }
    }
}

2390
static inline uint32_t insn_get(CPUX86State *env, DisasContext *s, int ot)
B
bellard 已提交
2391 2392 2393 2394 2395
{
    uint32_t ret;

    switch(ot) {
    case OT_BYTE:
2396
        ret = cpu_ldub_code(env, s->pc);
B
bellard 已提交
2397 2398 2399
        s->pc++;
        break;
    case OT_WORD:
2400
        ret = cpu_lduw_code(env, s->pc);
B
bellard 已提交
2401 2402 2403 2404
        s->pc += 2;
        break;
    default:
    case OT_LONG:
2405
        ret = cpu_ldl_code(env, s->pc);
B
bellard 已提交
2406 2407 2408 2409 2410 2411
        s->pc += 4;
        break;
    }
    return ret;
}

B
bellard 已提交
2412 2413 2414 2415 2416 2417 2418 2419
static inline int insn_const_size(unsigned int ot)
{
    if (ot <= OT_LONG)
        return 1 << ot;
    else
        return 4;
}

2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430
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 已提交
2431
        tcg_gen_goto_tb(tb_num);
2432
        gen_jmp_im(eip);
2433
        tcg_gen_exit_tb((tcg_target_long)tb + tb_num);
2434 2435 2436 2437 2438 2439 2440
    } else {
        /* jump to another page: currently not optimized */
        gen_jmp_im(eip);
        gen_eob(s);
    }
}

2441
static inline void gen_jcc(DisasContext *s, int b,
B
bellard 已提交
2442
                           target_ulong val, target_ulong next_eip)
B
bellard 已提交
2443
{
2444
    int l1, l2;
2445

B
bellard 已提交
2446
    if (s->jmp_opt) {
B
bellard 已提交
2447
        l1 = gen_new_label();
2448
        gen_jcc1(s, b, l1);
2449

2450
        gen_goto_tb(s, 0, next_eip);
B
bellard 已提交
2451 2452

        gen_set_label(l1);
2453
        gen_goto_tb(s, 1, val);
J
Jun Koi 已提交
2454
        s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2455
    } else {
B
bellard 已提交
2456 2457
        l1 = gen_new_label();
        l2 = gen_new_label();
2458
        gen_jcc1(s, b, l1);
2459

B
bellard 已提交
2460
        gen_jmp_im(next_eip);
2461 2462
        tcg_gen_br(l2);

B
bellard 已提交
2463 2464 2465
        gen_set_label(l1);
        gen_jmp_im(val);
        gen_set_label(l2);
B
bellard 已提交
2466 2467 2468 2469
        gen_eob(s);
    }
}

2470 2471 2472
static void gen_cmovcc1(CPUX86State *env, DisasContext *s, int ot, int b,
                        int modrm, int reg)
{
2473
    CCPrepare cc;
2474

2475
    gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
2476

2477 2478 2479 2480 2481 2482 2483 2484
    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);
2485 2486
    }

2487 2488 2489 2490 2491 2492 2493 2494 2495 2496
    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);
    }
2497 2498
}

2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514
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 已提交
2515 2516
/* move T0 to seg_reg and compute if the CPU state may change. Never
   call this function with seg_reg == R_CS */
B
bellard 已提交
2517
static void gen_movl_seg_T0(DisasContext *s, int seg_reg, target_ulong cur_eip)
B
bellard 已提交
2518
{
2519 2520
    if (s->pe && !s->vm86) {
        /* XXX: optimize by finding processor state dynamically */
2521
        gen_update_cc_op(s);
B
bellard 已提交
2522
        gen_jmp_im(cur_eip);
2523
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
2524
        gen_helper_load_seg(cpu_env, tcg_const_i32(seg_reg), cpu_tmp2_i32);
B
bellard 已提交
2525 2526 2527 2528 2529
        /* 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 已提交
2530
            s->is_jmp = DISAS_TB_JUMP;
2531
    } else {
2532
        gen_op_movl_seg_T0_vm(seg_reg);
B
bellard 已提交
2533
        if (seg_reg == R_SS)
J
Jun Koi 已提交
2534
            s->is_jmp = DISAS_TB_JUMP;
2535
    }
B
bellard 已提交
2536 2537
}

T
ths 已提交
2538 2539 2540 2541 2542
static inline int svm_is_rep(int prefixes)
{
    return ((prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) ? 8 : 0);
}

B
bellard 已提交
2543
static inline void
T
ths 已提交
2544
gen_svm_check_intercept_param(DisasContext *s, target_ulong pc_start,
2545
                              uint32_t type, uint64_t param)
T
ths 已提交
2546
{
B
bellard 已提交
2547 2548 2549
    /* no SVM activated; fast case */
    if (likely(!(s->flags & HF_SVMI_MASK)))
        return;
2550
    gen_update_cc_op(s);
B
bellard 已提交
2551
    gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
2552
    gen_helper_svm_check_intercept_param(cpu_env, tcg_const_i32(type),
P
pbrook 已提交
2553
                                         tcg_const_i64(param));
T
ths 已提交
2554 2555
}

B
bellard 已提交
2556
static inline void
T
ths 已提交
2557 2558
gen_svm_check_intercept(DisasContext *s, target_ulong pc_start, uint64_t type)
{
B
bellard 已提交
2559
    gen_svm_check_intercept_param(s, pc_start, type, 0);
T
ths 已提交
2560 2561
}

2562 2563
static inline void gen_stack_update(DisasContext *s, int addend)
{
B
bellard 已提交
2564 2565
#ifdef TARGET_X86_64
    if (CODE64(s)) {
2566
        gen_op_add_reg_im(2, R_ESP, addend);
B
bellard 已提交
2567 2568
    } else
#endif
2569
    if (s->ss32) {
2570
        gen_op_add_reg_im(1, R_ESP, addend);
2571
    } else {
2572
        gen_op_add_reg_im(0, R_ESP, addend);
2573 2574 2575
    }
}

B
bellard 已提交
2576 2577 2578
/* generate a push. It depends on ss32, addseg and dflag */
static void gen_push_T0(DisasContext *s)
{
B
bellard 已提交
2579 2580
#ifdef TARGET_X86_64
    if (CODE64(s)) {
B
bellard 已提交
2581
        gen_op_movq_A0_reg(R_ESP);
2582
        if (s->dflag) {
B
bellard 已提交
2583 2584
            gen_op_addq_A0_im(-8);
            gen_op_st_T0_A0(OT_QUAD + s->mem_index);
2585
        } else {
B
bellard 已提交
2586 2587
            gen_op_addq_A0_im(-2);
            gen_op_st_T0_A0(OT_WORD + s->mem_index);
2588
        }
B
bellard 已提交
2589
        gen_op_mov_reg_A0(2, R_ESP);
2590
    } else
B
bellard 已提交
2591 2592
#endif
    {
B
bellard 已提交
2593
        gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2594
        if (!s->dflag)
B
bellard 已提交
2595
            gen_op_addl_A0_im(-2);
B
bellard 已提交
2596
        else
B
bellard 已提交
2597
            gen_op_addl_A0_im(-4);
B
bellard 已提交
2598 2599
        if (s->ss32) {
            if (s->addseg) {
2600
                tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2601
                gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2602 2603 2604
            }
        } else {
            gen_op_andl_A0_ffff();
2605
            tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2606
            gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2607
        }
B
bellard 已提交
2608
        gen_op_st_T0_A0(s->dflag + 1 + s->mem_index);
B
bellard 已提交
2609
        if (s->ss32 && !s->addseg)
B
bellard 已提交
2610
            gen_op_mov_reg_A0(1, R_ESP);
B
bellard 已提交
2611
        else
B
bellard 已提交
2612
            gen_op_mov_reg_T1(s->ss32 + 1, R_ESP);
B
bellard 已提交
2613 2614 2615
    }
}

2616 2617 2618
/* 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 已提交
2619
{
B
bellard 已提交
2620 2621
#ifdef TARGET_X86_64
    if (CODE64(s)) {
B
bellard 已提交
2622
        gen_op_movq_A0_reg(R_ESP);
2623
        if (s->dflag) {
B
bellard 已提交
2624 2625
            gen_op_addq_A0_im(-8);
            gen_op_st_T1_A0(OT_QUAD + s->mem_index);
2626
        } else {
B
bellard 已提交
2627 2628
            gen_op_addq_A0_im(-2);
            gen_op_st_T0_A0(OT_WORD + s->mem_index);
2629
        }
B
bellard 已提交
2630
        gen_op_mov_reg_A0(2, R_ESP);
2631
    } else
B
bellard 已提交
2632 2633
#endif
    {
B
bellard 已提交
2634
        gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2635
        if (!s->dflag)
B
bellard 已提交
2636
            gen_op_addl_A0_im(-2);
B
bellard 已提交
2637
        else
B
bellard 已提交
2638
            gen_op_addl_A0_im(-4);
B
bellard 已提交
2639 2640
        if (s->ss32) {
            if (s->addseg) {
2641
                gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2642 2643 2644
            }
        } else {
            gen_op_andl_A0_ffff();
2645
            gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2646
        }
B
bellard 已提交
2647
        gen_op_st_T1_A0(s->dflag + 1 + s->mem_index);
2648

B
bellard 已提交
2649
        if (s->ss32 && !s->addseg)
B
bellard 已提交
2650
            gen_op_mov_reg_A0(1, R_ESP);
B
bellard 已提交
2651 2652
        else
            gen_stack_update(s, (-2) << s->dflag);
B
bellard 已提交
2653 2654 2655
    }
}

2656 2657
/* two step pop is necessary for precise exceptions */
static void gen_pop_T0(DisasContext *s)
B
bellard 已提交
2658
{
B
bellard 已提交
2659 2660
#ifdef TARGET_X86_64
    if (CODE64(s)) {
B
bellard 已提交
2661 2662
        gen_op_movq_A0_reg(R_ESP);
        gen_op_ld_T0_A0((s->dflag ? OT_QUAD : OT_WORD) + s->mem_index);
2663
    } else
B
bellard 已提交
2664 2665
#endif
    {
B
bellard 已提交
2666
        gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2667 2668
        if (s->ss32) {
            if (s->addseg)
2669
                gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2670 2671
        } else {
            gen_op_andl_A0_ffff();
2672
            gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2673
        }
B
bellard 已提交
2674
        gen_op_ld_T0_A0(s->dflag + 1 + s->mem_index);
B
bellard 已提交
2675 2676 2677 2678 2679
    }
}

static void gen_pop_update(DisasContext *s)
{
B
bellard 已提交
2680
#ifdef TARGET_X86_64
2681
    if (CODE64(s) && s->dflag) {
B
bellard 已提交
2682 2683 2684 2685 2686 2687
        gen_stack_update(s, 8);
    } else
#endif
    {
        gen_stack_update(s, 2 << s->dflag);
    }
B
bellard 已提交
2688 2689 2690 2691
}

static void gen_stack_A0(DisasContext *s)
{
B
bellard 已提交
2692
    gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2693 2694
    if (!s->ss32)
        gen_op_andl_A0_ffff();
2695
    tcg_gen_mov_tl(cpu_T[1], cpu_A0);
B
bellard 已提交
2696
    if (s->addseg)
2697
        gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2698 2699 2700 2701 2702 2703
}

/* NOTE: wrap around in 16 bit not fully handled */
static void gen_pusha(DisasContext *s)
{
    int i;
B
bellard 已提交
2704
    gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2705 2706 2707
    gen_op_addl_A0_im(-16 <<  s->dflag);
    if (!s->ss32)
        gen_op_andl_A0_ffff();
2708
    tcg_gen_mov_tl(cpu_T[1], cpu_A0);
B
bellard 已提交
2709
    if (s->addseg)
2710
        gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2711
    for(i = 0;i < 8; i++) {
B
bellard 已提交
2712 2713
        gen_op_mov_TN_reg(OT_LONG, 0, 7 - i);
        gen_op_st_T0_A0(OT_WORD + s->dflag + s->mem_index);
B
bellard 已提交
2714 2715
        gen_op_addl_A0_im(2 <<  s->dflag);
    }
B
bellard 已提交
2716
    gen_op_mov_reg_T1(OT_WORD + s->ss32, R_ESP);
B
bellard 已提交
2717 2718 2719 2720 2721 2722
}

/* NOTE: wrap around in 16 bit not fully handled */
static void gen_popa(DisasContext *s)
{
    int i;
B
bellard 已提交
2723
    gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2724 2725
    if (!s->ss32)
        gen_op_andl_A0_ffff();
2726 2727
    tcg_gen_mov_tl(cpu_T[1], cpu_A0);
    tcg_gen_addi_tl(cpu_T[1], cpu_T[1], 16 <<  s->dflag);
B
bellard 已提交
2728
    if (s->addseg)
2729
        gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2730 2731 2732
    for(i = 0;i < 8; i++) {
        /* ESP is not reloaded */
        if (i != 3) {
B
bellard 已提交
2733 2734
            gen_op_ld_T0_A0(OT_WORD + s->dflag + s->mem_index);
            gen_op_mov_reg_T0(OT_WORD + s->dflag, 7 - i);
B
bellard 已提交
2735 2736 2737
        }
        gen_op_addl_A0_im(2 <<  s->dflag);
    }
B
bellard 已提交
2738
    gen_op_mov_reg_T1(OT_WORD + s->ss32, R_ESP);
B
bellard 已提交
2739 2740 2741 2742
}

static void gen_enter(DisasContext *s, int esp_addend, int level)
{
B
bellard 已提交
2743
    int ot, opsize;
B
bellard 已提交
2744 2745

    level &= 0x1f;
2746 2747 2748 2749
#ifdef TARGET_X86_64
    if (CODE64(s)) {
        ot = s->dflag ? OT_QUAD : OT_WORD;
        opsize = 1 << ot;
2750

B
bellard 已提交
2751
        gen_op_movl_A0_reg(R_ESP);
2752
        gen_op_addq_A0_im(-opsize);
2753
        tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2754 2755

        /* push bp */
B
bellard 已提交
2756 2757
        gen_op_mov_TN_reg(OT_LONG, 0, R_EBP);
        gen_op_st_T0_A0(ot + s->mem_index);
2758
        if (level) {
B
bellard 已提交
2759
            /* XXX: must save state */
2760
            gen_helper_enter64_level(cpu_env, tcg_const_i32(level),
P
pbrook 已提交
2761 2762
                                     tcg_const_i32((ot == OT_QUAD)),
                                     cpu_T[1]);
2763
        }
B
bellard 已提交
2764
        gen_op_mov_reg_T1(ot, R_EBP);
2765
        tcg_gen_addi_tl(cpu_T[1], cpu_T[1], -esp_addend + (-opsize * level));
B
bellard 已提交
2766
        gen_op_mov_reg_T1(OT_QUAD, R_ESP);
2767
    } else
2768 2769 2770 2771
#endif
    {
        ot = s->dflag + OT_WORD;
        opsize = 2 << s->dflag;
2772

B
bellard 已提交
2773
        gen_op_movl_A0_reg(R_ESP);
2774 2775 2776
        gen_op_addl_A0_im(-opsize);
        if (!s->ss32)
            gen_op_andl_A0_ffff();
2777
        tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2778
        if (s->addseg)
2779
            gen_op_addl_A0_seg(s, R_SS);
2780
        /* push bp */
B
bellard 已提交
2781 2782
        gen_op_mov_TN_reg(OT_LONG, 0, R_EBP);
        gen_op_st_T0_A0(ot + s->mem_index);
2783
        if (level) {
B
bellard 已提交
2784
            /* XXX: must save state */
2785
            gen_helper_enter_level(cpu_env, tcg_const_i32(level),
P
pbrook 已提交
2786 2787
                                   tcg_const_i32(s->dflag),
                                   cpu_T[1]);
2788
        }
B
bellard 已提交
2789
        gen_op_mov_reg_T1(ot, R_EBP);
2790
        tcg_gen_addi_tl(cpu_T[1], cpu_T[1], -esp_addend + (-opsize * level));
B
bellard 已提交
2791
        gen_op_mov_reg_T1(OT_WORD + s->ss32, R_ESP);
B
bellard 已提交
2792 2793 2794
    }
}

B
bellard 已提交
2795
static void gen_exception(DisasContext *s, int trapno, target_ulong cur_eip)
B
bellard 已提交
2796
{
2797
    gen_update_cc_op(s);
B
bellard 已提交
2798
    gen_jmp_im(cur_eip);
B
Blue Swirl 已提交
2799
    gen_helper_raise_exception(cpu_env, tcg_const_i32(trapno));
J
Jun Koi 已提交
2800
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2801 2802 2803
}

/* an interrupt is different from an exception because of the
B
blueswir1 已提交
2804
   privilege checks */
2805
static void gen_interrupt(DisasContext *s, int intno,
B
bellard 已提交
2806
                          target_ulong cur_eip, target_ulong next_eip)
B
bellard 已提交
2807
{
2808
    gen_update_cc_op(s);
B
bellard 已提交
2809
    gen_jmp_im(cur_eip);
B
Blue Swirl 已提交
2810
    gen_helper_raise_interrupt(cpu_env, tcg_const_i32(intno),
P
pbrook 已提交
2811
                               tcg_const_i32(next_eip - cur_eip));
J
Jun Koi 已提交
2812
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2813 2814
}

B
bellard 已提交
2815
static void gen_debug(DisasContext *s, target_ulong cur_eip)
B
bellard 已提交
2816
{
2817
    gen_update_cc_op(s);
B
bellard 已提交
2818
    gen_jmp_im(cur_eip);
B
Blue Swirl 已提交
2819
    gen_helper_debug(cpu_env);
J
Jun Koi 已提交
2820
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2821 2822 2823 2824 2825 2826
}

/* generate a generic end of block. Trace exception is also generated
   if needed */
static void gen_eob(DisasContext *s)
{
2827
    gen_update_cc_op(s);
2828
    if (s->tb->flags & HF_INHIBIT_IRQ_MASK) {
2829
        gen_helper_reset_inhibit_irq(cpu_env);
2830
    }
J
Jan Kiszka 已提交
2831
    if (s->tb->flags & HF_RF_MASK) {
2832
        gen_helper_reset_rf(cpu_env);
J
Jan Kiszka 已提交
2833
    }
2834
    if (s->singlestep_enabled) {
B
Blue Swirl 已提交
2835
        gen_helper_debug(cpu_env);
2836
    } else if (s->tf) {
B
Blue Swirl 已提交
2837
        gen_helper_single_step(cpu_env);
B
bellard 已提交
2838
    } else {
B
bellard 已提交
2839
        tcg_gen_exit_tb(0);
B
bellard 已提交
2840
    }
J
Jun Koi 已提交
2841
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2842 2843 2844 2845
}

/* generate a jump to eip. No segment change must happen before as a
   direct call to the next block may occur */
B
bellard 已提交
2846
static void gen_jmp_tb(DisasContext *s, target_ulong eip, int tb_num)
B
bellard 已提交
2847
{
2848 2849
    gen_update_cc_op(s);
    set_cc_op(s, CC_OP_DYNAMIC);
B
bellard 已提交
2850
    if (s->jmp_opt) {
2851
        gen_goto_tb(s, tb_num, eip);
J
Jun Koi 已提交
2852
        s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2853
    } else {
B
bellard 已提交
2854
        gen_jmp_im(eip);
B
bellard 已提交
2855 2856 2857 2858
        gen_eob(s);
    }
}

B
bellard 已提交
2859 2860 2861 2862 2863
static void gen_jmp(DisasContext *s, target_ulong eip)
{
    gen_jmp_tb(s, eip, 0);
}

B
bellard 已提交
2864 2865 2866
static inline void gen_ldq_env_A0(int idx, int offset)
{
    int mem_index = (idx >> 2) - 1;
2867 2868
    tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_A0, mem_index);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset);
B
bellard 已提交
2869
}
B
bellard 已提交
2870

B
bellard 已提交
2871 2872 2873
static inline void gen_stq_env_A0(int idx, int offset)
{
    int mem_index = (idx >> 2) - 1;
2874 2875
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset);
    tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_A0, mem_index);
B
bellard 已提交
2876
}
B
bellard 已提交
2877

B
bellard 已提交
2878 2879 2880
static inline void gen_ldo_env_A0(int idx, int offset)
{
    int mem_index = (idx >> 2) - 1;
2881 2882
    tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_A0, mem_index);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(0)));
B
bellard 已提交
2883
    tcg_gen_addi_tl(cpu_tmp0, cpu_A0, 8);
2884 2885
    tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_tmp0, mem_index);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(1)));
B
bellard 已提交
2886
}
B
bellard 已提交
2887

B
bellard 已提交
2888 2889 2890
static inline void gen_sto_env_A0(int idx, int offset)
{
    int mem_index = (idx >> 2) - 1;
2891 2892
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(0)));
    tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_A0, mem_index);
B
bellard 已提交
2893
    tcg_gen_addi_tl(cpu_tmp0, cpu_A0, 8);
2894 2895
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(1)));
    tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_tmp0, mem_index);
B
bellard 已提交
2896
}
B
bellard 已提交
2897

B
bellard 已提交
2898 2899
static inline void gen_op_movo(int d_offset, int s_offset)
{
2900 2901 2902 2903
    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 已提交
2904 2905 2906 2907
}

static inline void gen_op_movq(int d_offset, int s_offset)
{
2908 2909
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
B
bellard 已提交
2910 2911 2912 2913
}

static inline void gen_op_movl(int d_offset, int s_offset)
{
2914 2915
    tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env, s_offset);
    tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, d_offset);
B
bellard 已提交
2916 2917 2918 2919
}

static inline void gen_op_movq_env_0(int d_offset)
{
2920 2921
    tcg_gen_movi_i64(cpu_tmp1_i64, 0);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
B
bellard 已提交
2922
}
B
bellard 已提交
2923

B
Blue Swirl 已提交
2924 2925 2926 2927 2928 2929 2930
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 已提交
2931
typedef void (*SSEFunc_0_ppi)(TCGv_ptr reg_a, TCGv_ptr reg_b, TCGv_i32 val);
B
Blue Swirl 已提交
2932 2933
typedef void (*SSEFunc_0_eppt)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
                               TCGv val);
B
Blue Swirl 已提交
2934

B
bellard 已提交
2935 2936
#define SSE_SPECIAL ((void *)1)
#define SSE_DUMMY ((void *)2)
B
bellard 已提交
2937

P
pbrook 已提交
2938 2939 2940
#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 已提交
2941

B
Blue Swirl 已提交
2942
static const SSEFunc_0_epp sse_op_table1[256][4] = {
A
aurel32 已提交
2943 2944 2945
    /* 3DNow! extensions */
    [0x0e] = { SSE_DUMMY }, /* femms */
    [0x0f] = { SSE_DUMMY }, /* pf... */
B
bellard 已提交
2946 2947 2948
    /* 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 已提交
2949
    [0x12] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movlps, movlpd, movsldup, movddup */
B
bellard 已提交
2950
    [0x13] = { SSE_SPECIAL, SSE_SPECIAL },  /* movlps, movlpd */
P
pbrook 已提交
2951 2952
    [0x14] = { gen_helper_punpckldq_xmm, gen_helper_punpcklqdq_xmm },
    [0x15] = { gen_helper_punpckhdq_xmm, gen_helper_punpckhqdq_xmm },
B
bellard 已提交
2953 2954 2955 2956 2957 2958
    [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 */
2959
    [0x2b] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movntps, movntpd, movntss, movntsd */
B
bellard 已提交
2960 2961
    [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 已提交
2962 2963
    [0x2e] = { gen_helper_ucomiss, gen_helper_ucomisd },
    [0x2f] = { gen_helper_comiss, gen_helper_comisd },
B
bellard 已提交
2964 2965
    [0x50] = { SSE_SPECIAL, SSE_SPECIAL }, /* movmskps, movmskpd */
    [0x51] = SSE_FOP(sqrt),
P
pbrook 已提交
2966 2967 2968 2969 2970 2971
    [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 已提交
2972 2973
    [0x58] = SSE_FOP(add),
    [0x59] = SSE_FOP(mul),
P
pbrook 已提交
2974 2975 2976
    [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 已提交
2977 2978 2979 2980 2981 2982
    [0x5c] = SSE_FOP(sub),
    [0x5d] = SSE_FOP(min),
    [0x5e] = SSE_FOP(div),
    [0x5f] = SSE_FOP(max),

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

R
Richard Henderson 已提交
2986 2987 2988
    /* 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 已提交
2989

B
bellard 已提交
2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002
    /* 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 已提交
3003 3004
    [0x6c] = { NULL, gen_helper_punpcklqdq_xmm },
    [0x6d] = { NULL, gen_helper_punpckhqdq_xmm },
B
bellard 已提交
3005 3006
    [0x6e] = { SSE_SPECIAL, SSE_SPECIAL }, /* movd mm, ea */
    [0x6f] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movq, movdqa, , movqdu */
B
Blue Swirl 已提交
3007 3008 3009 3010
    [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 已提交
3011 3012 3013 3014 3015 3016
    [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 已提交
3017
    [0x77] = { SSE_DUMMY }, /* emms */
3018 3019
    [0x78] = { NULL, SSE_SPECIAL, NULL, SSE_SPECIAL }, /* extrq_i, insertq_i */
    [0x79] = { NULL, gen_helper_extrq_r, NULL, gen_helper_insertq_r },
P
pbrook 已提交
3020 3021
    [0x7c] = { NULL, gen_helper_haddpd, NULL, gen_helper_haddps },
    [0x7d] = { NULL, gen_helper_hsubpd, NULL, gen_helper_hsubps },
B
bellard 已提交
3022 3023 3024 3025
    [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 已提交
3026
    [0xd0] = { NULL, gen_helper_addsubpd, NULL, gen_helper_addsubps },
B
bellard 已提交
3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047
    [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 已提交
3048
    [0xe6] = { NULL, gen_helper_cvttpd2dq, gen_helper_cvtdq2pd, gen_helper_cvtpd2dq },
B
bellard 已提交
3049 3050 3051 3052 3053 3054 3055 3056 3057
    [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 已提交
3058
    [0xf0] = { NULL, NULL, NULL, SSE_SPECIAL }, /* lddqu */
B
bellard 已提交
3059 3060 3061 3062 3063 3064
    [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 已提交
3065 3066
    [0xf7] = { (SSEFunc_0_epp)gen_helper_maskmov_mmx,
               (SSEFunc_0_epp)gen_helper_maskmov_xmm }, /* XXX: casts */
B
bellard 已提交
3067 3068 3069 3070 3071 3072 3073 3074 3075
    [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 已提交
3076
static const SSEFunc_0_epp sse_op_table2[3 * 8][2] = {
B
bellard 已提交
3077 3078 3079 3080 3081 3082 3083
    [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 已提交
3084
    [16 + 3] = { NULL, gen_helper_psrldq_xmm },
B
bellard 已提交
3085
    [16 + 6] = MMX_OP2(psllq),
P
pbrook 已提交
3086
    [16 + 7] = { NULL, gen_helper_pslldq_xmm },
B
bellard 已提交
3087 3088
};

B
Blue Swirl 已提交
3089
static const SSEFunc_0_epi sse_op_table3ai[] = {
P
pbrook 已提交
3090
    gen_helper_cvtsi2ss,
3091
    gen_helper_cvtsi2sd
B
Blue Swirl 已提交
3092
};
P
pbrook 已提交
3093

3094
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
3095
static const SSEFunc_0_epl sse_op_table3aq[] = {
3096 3097 3098 3099 3100
    gen_helper_cvtsq2ss,
    gen_helper_cvtsq2sd
};
#endif

B
Blue Swirl 已提交
3101
static const SSEFunc_i_ep sse_op_table3bi[] = {
P
pbrook 已提交
3102 3103
    gen_helper_cvttss2si,
    gen_helper_cvtss2si,
3104
    gen_helper_cvttsd2si,
3105
    gen_helper_cvtsd2si
B
bellard 已提交
3106
};
3107

3108
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
3109
static const SSEFunc_l_ep sse_op_table3bq[] = {
3110 3111
    gen_helper_cvttss2sq,
    gen_helper_cvtss2sq,
3112
    gen_helper_cvttsd2sq,
3113 3114 3115 3116
    gen_helper_cvtsd2sq
};
#endif

B
Blue Swirl 已提交
3117
static const SSEFunc_0_epp sse_op_table4[8][4] = {
B
bellard 已提交
3118 3119 3120 3121 3122 3123 3124 3125 3126
    SSE_FOP(cmpeq),
    SSE_FOP(cmplt),
    SSE_FOP(cmple),
    SSE_FOP(cmpunord),
    SSE_FOP(cmpneq),
    SSE_FOP(cmpnlt),
    SSE_FOP(cmpnle),
    SSE_FOP(cmpord),
};
3127

B
Blue Swirl 已提交
3128
static const SSEFunc_0_epp sse_op_table5[256] = {
P
pbrook 已提交
3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152
    [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 已提交
3153 3154
};

B
Blue Swirl 已提交
3155 3156
struct SSEOpHelper_epp {
    SSEFunc_0_epp op[2];
B
Blue Swirl 已提交
3157 3158 3159
    uint32_t ext_mask;
};

B
Blue Swirl 已提交
3160 3161
struct SSEOpHelper_eppi {
    SSEFunc_0_eppi op[2];
B
Blue Swirl 已提交
3162
    uint32_t ext_mask;
B
balrog 已提交
3163
};
B
Blue Swirl 已提交
3164

B
balrog 已提交
3165
#define SSSE3_OP(x) { MMX_OP2(x), CPUID_EXT_SSSE3 }
P
pbrook 已提交
3166 3167
#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 已提交
3168
#define SSE41_SPECIAL { { NULL, SSE_SPECIAL }, CPUID_EXT_SSE41 }
B
Blue Swirl 已提交
3169

B
Blue Swirl 已提交
3170
static const struct SSEOpHelper_epp sse_op_table6[256] = {
B
balrog 已提交
3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216
    [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),
B
balrog 已提交
3217 3218
};

B
Blue Swirl 已提交
3219
static const struct SSEOpHelper_eppi sse_op_table7[256] = {
B
balrog 已提交
3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241
    [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),
    [0x60] = SSE42_OP(pcmpestrm),
    [0x61] = SSE42_OP(pcmpestri),
    [0x62] = SSE42_OP(pcmpistrm),
    [0x63] = SSE42_OP(pcmpistri),
B
balrog 已提交
3242 3243
};

3244 3245
static void gen_sse(CPUX86State *env, DisasContext *s, int b,
                    target_ulong pc_start, int rex_r)
B
bellard 已提交
3246 3247 3248
{
    int b1, op1_offset, op2_offset, is_xmm, val, ot;
    int modrm, mod, rm, reg, reg_addr, offset_addr;
B
Blue Swirl 已提交
3249 3250
    SSEFunc_0_epp sse_fn_epp;
    SSEFunc_0_eppi sse_fn_eppi;
B
Blue Swirl 已提交
3251
    SSEFunc_0_ppi sse_fn_ppi;
B
Blue Swirl 已提交
3252
    SSEFunc_0_eppt sse_fn_eppt;
B
bellard 已提交
3253 3254

    b &= 0xff;
3255
    if (s->prefix & PREFIX_DATA)
B
bellard 已提交
3256
        b1 = 1;
3257
    else if (s->prefix & PREFIX_REPZ)
B
bellard 已提交
3258
        b1 = 2;
3259
    else if (s->prefix & PREFIX_REPNZ)
B
bellard 已提交
3260 3261 3262
        b1 = 3;
    else
        b1 = 0;
B
Blue Swirl 已提交
3263 3264
    sse_fn_epp = sse_op_table1[b][b1];
    if (!sse_fn_epp) {
B
bellard 已提交
3265
        goto illegal_op;
B
Blue Swirl 已提交
3266
    }
A
aurel32 已提交
3267
    if ((b <= 0x5f && b >= 0x10) || b == 0xc6 || b == 0xc2) {
B
bellard 已提交
3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287
        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 已提交
3288 3289
        if ((b != 0x38 && b != 0x3a) || (s->prefix & PREFIX_DATA))
            goto illegal_op;
3290 3291 3292 3293
    if (b == 0x0e) {
        if (!(s->cpuid_ext2_features & CPUID_EXT2_3DNOW))
            goto illegal_op;
        /* femms */
B
Blue Swirl 已提交
3294
        gen_helper_emms(cpu_env);
3295 3296 3297 3298
        return;
    }
    if (b == 0x77) {
        /* emms */
B
Blue Swirl 已提交
3299
        gen_helper_emms(cpu_env);
B
bellard 已提交
3300 3301 3302 3303 3304
        return;
    }
    /* prepare MMX state (XXX: optimize by storing fptt and fptags in
       the static cpu state) */
    if (!is_xmm) {
B
Blue Swirl 已提交
3305
        gen_helper_enter_mmx(cpu_env);
B
bellard 已提交
3306 3307
    }

3308
    modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
3309 3310 3311 3312
    reg = ((modrm >> 3) & 7);
    if (is_xmm)
        reg |= rex_r;
    mod = (modrm >> 6) & 3;
B
Blue Swirl 已提交
3313
    if (sse_fn_epp == SSE_SPECIAL) {
B
bellard 已提交
3314 3315 3316
        b |= (b1 << 8);
        switch(b) {
        case 0x0e7: /* movntq */
3317
            if (mod == 3)
B
bellard 已提交
3318
                goto illegal_op;
3319
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3320
            gen_stq_env_A0(s->mem_index, offsetof(CPUX86State,fpregs[reg].mmx));
B
bellard 已提交
3321 3322 3323 3324
            break;
        case 0x1e7: /* movntdq */
        case 0x02b: /* movntps */
        case 0x12b: /* movntps */
3325 3326
            if (mod == 3)
                goto illegal_op;
3327
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
3328 3329
            gen_sto_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg]));
            break;
B
bellard 已提交
3330 3331
        case 0x3f0: /* lddqu */
            if (mod == 3)
B
bellard 已提交
3332
                goto illegal_op;
3333
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
3334
            gen_ldo_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg]));
B
bellard 已提交
3335
            break;
3336 3337 3338 3339
        case 0x22b: /* movntss */
        case 0x32b: /* movntsd */
            if (mod == 3)
                goto illegal_op;
3340
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
3341 3342 3343 3344 3345 3346 3347 3348 3349
            if (b1 & 1) {
                gen_stq_env_A0(s->mem_index, offsetof(CPUX86State,
                    xmm_regs[reg]));
            } else {
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
                    xmm_regs[reg].XMM_L(0)));
                gen_op_st_T0_A0(OT_LONG + s->mem_index);
            }
            break;
B
bellard 已提交
3350
        case 0x6e: /* movd mm, ea */
B
bellard 已提交
3351 3352
#ifdef TARGET_X86_64
            if (s->dflag == 2) {
3353
                gen_ldst_modrm(env, s, modrm, OT_QUAD, OR_TMP0, 0);
B
bellard 已提交
3354
                tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,fpregs[reg].mmx));
3355
            } else
B
bellard 已提交
3356 3357
#endif
            {
3358
                gen_ldst_modrm(env, s, modrm, OT_LONG, OR_TMP0, 0);
B
bellard 已提交
3359 3360
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                                 offsetof(CPUX86State,fpregs[reg].mmx));
P
pbrook 已提交
3361 3362
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
                gen_helper_movl_mm_T0_mmx(cpu_ptr0, cpu_tmp2_i32);
B
bellard 已提交
3363
            }
B
bellard 已提交
3364 3365
            break;
        case 0x16e: /* movd xmm, ea */
B
bellard 已提交
3366 3367
#ifdef TARGET_X86_64
            if (s->dflag == 2) {
3368
                gen_ldst_modrm(env, s, modrm, OT_QUAD, OR_TMP0, 0);
B
bellard 已提交
3369 3370
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                                 offsetof(CPUX86State,xmm_regs[reg]));
P
pbrook 已提交
3371
                gen_helper_movq_mm_T0_xmm(cpu_ptr0, cpu_T[0]);
3372
            } else
B
bellard 已提交
3373 3374
#endif
            {
3375
                gen_ldst_modrm(env, s, modrm, OT_LONG, OR_TMP0, 0);
B
bellard 已提交
3376 3377
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                                 offsetof(CPUX86State,xmm_regs[reg]));
3378
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
3379
                gen_helper_movl_mm_T0_xmm(cpu_ptr0, cpu_tmp2_i32);
B
bellard 已提交
3380
            }
B
bellard 已提交
3381 3382 3383
            break;
        case 0x6f: /* movq mm, ea */
            if (mod != 3) {
3384
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3385
                gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,fpregs[reg].mmx));
B
bellard 已提交
3386 3387
            } else {
                rm = (modrm & 7);
3388
                tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env,
B
bellard 已提交
3389
                               offsetof(CPUX86State,fpregs[rm].mmx));
3390
                tcg_gen_st_i64(cpu_tmp1_i64, cpu_env,
B
bellard 已提交
3391
                               offsetof(CPUX86State,fpregs[reg].mmx));
B
bellard 已提交
3392 3393 3394 3395 3396 3397 3398 3399 3400
            }
            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) {
3401
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3402
                gen_ldo_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg]));
B
bellard 已提交
3403 3404 3405 3406 3407 3408 3409 3410
            } 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) {
3411
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3412
                gen_op_ld_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
3413
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
B
bellard 已提交
3414
                gen_op_movl_T0_0();
B
bellard 已提交
3415 3416 3417
                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 已提交
3418 3419 3420 3421 3422 3423 3424 3425
            } 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) {
3426
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3427
                gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3428
                gen_op_movl_T0_0();
B
bellard 已提交
3429 3430
                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 已提交
3431 3432 3433 3434 3435 3436 3437 3438 3439
            } 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) {
3440
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3441
                gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3442 3443 3444 3445 3446 3447 3448
            } 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 已提交
3449 3450
        case 0x212: /* movsldup */
            if (mod != 3) {
3451
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3452
                gen_ldo_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg]));
B
bellard 已提交
3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466
            } 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) {
3467
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3468
                gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3469 3470 3471 3472 3473 3474
            } 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 已提交
3475
                        offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3476
            break;
B
bellard 已提交
3477 3478 3479
        case 0x016: /* movhps */
        case 0x116: /* movhpd */
            if (mod != 3) {
3480
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3481
                gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(1)));
B
bellard 已提交
3482 3483 3484 3485 3486 3487 3488 3489 3490
            } 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) {
3491
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3492
                gen_ldo_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg]));
B
bellard 已提交
3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504
            } 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;
3505 3506 3507 3508 3509 3510 3511
        case 0x178:
        case 0x378:
            {
                int bit_index, field_length;

                if (b1 == 1 && reg != 0)
                    goto illegal_op;
3512 3513
                field_length = cpu_ldub_code(env, s->pc++) & 0x3F;
                bit_index = cpu_ldub_code(env, s->pc++) & 0x3F;
3514 3515 3516
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
                    offsetof(CPUX86State,xmm_regs[reg]));
                if (b1 == 1)
B
Blue Swirl 已提交
3517 3518 3519
                    gen_helper_extrq_i(cpu_env, cpu_ptr0,
                                       tcg_const_i32(bit_index),
                                       tcg_const_i32(field_length));
3520
                else
B
Blue Swirl 已提交
3521 3522 3523
                    gen_helper_insertq_i(cpu_env, cpu_ptr0,
                                         tcg_const_i32(bit_index),
                                         tcg_const_i32(field_length));
3524 3525
            }
            break;
B
bellard 已提交
3526
        case 0x7e: /* movd ea, mm */
B
bellard 已提交
3527 3528
#ifdef TARGET_X86_64
            if (s->dflag == 2) {
B
bellard 已提交
3529 3530
                tcg_gen_ld_i64(cpu_T[0], cpu_env, 
                               offsetof(CPUX86State,fpregs[reg].mmx));
3531
                gen_ldst_modrm(env, s, modrm, OT_QUAD, OR_TMP0, 1);
3532
            } else
B
bellard 已提交
3533 3534
#endif
            {
B
bellard 已提交
3535 3536
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, 
                                 offsetof(CPUX86State,fpregs[reg].mmx.MMX_L(0)));
3537
                gen_ldst_modrm(env, s, modrm, OT_LONG, OR_TMP0, 1);
B
bellard 已提交
3538
            }
B
bellard 已提交
3539 3540
            break;
        case 0x17e: /* movd ea, xmm */
B
bellard 已提交
3541 3542
#ifdef TARGET_X86_64
            if (s->dflag == 2) {
B
bellard 已提交
3543 3544
                tcg_gen_ld_i64(cpu_T[0], cpu_env, 
                               offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
3545
                gen_ldst_modrm(env, s, modrm, OT_QUAD, OR_TMP0, 1);
3546
            } else
B
bellard 已提交
3547 3548
#endif
            {
B
bellard 已提交
3549 3550
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, 
                                 offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
3551
                gen_ldst_modrm(env, s, modrm, OT_LONG, OR_TMP0, 1);
B
bellard 已提交
3552
            }
B
bellard 已提交
3553 3554 3555
            break;
        case 0x27e: /* movq xmm, ea */
            if (mod != 3) {
3556
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3557
                gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3558 3559 3560 3561 3562 3563 3564 3565 3566
            } 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) {
3567
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3568
                gen_stq_env_A0(s->mem_index, offsetof(CPUX86State,fpregs[reg].mmx));
B
bellard 已提交
3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581
            } 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) {
3582
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3583
                gen_sto_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg]));
B
bellard 已提交
3584 3585 3586 3587 3588 3589 3590 3591
            } 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) {
3592
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3593
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
B
bellard 已提交
3594
                gen_op_st_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
3595 3596 3597 3598 3599 3600 3601 3602
            } 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) {
3603
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3604
                gen_stq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3605 3606 3607 3608 3609 3610 3611 3612 3613
            } 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) {
3614
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3615
                gen_stq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3616 3617 3618 3619 3620 3621 3622
            } else {
                goto illegal_op;
            }
            break;
        case 0x017: /* movhps */
        case 0x117: /* movhpd */
            if (mod != 3) {
3623
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3624
                gen_stq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(1)));
B
bellard 已提交
3625 3626 3627 3628 3629 3630 3631 3632 3633 3634
            } else {
                goto illegal_op;
            }
            break;
        case 0x71: /* shift mm, im */
        case 0x72:
        case 0x73:
        case 0x171: /* shift xmm, im */
        case 0x172:
        case 0x173:
3635 3636 3637
            if (b1 >= 2) {
	        goto illegal_op;
            }
3638
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
3639 3640
            if (is_xmm) {
                gen_op_movl_T0_im(val);
B
bellard 已提交
3641
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(0)));
B
bellard 已提交
3642
                gen_op_movl_T0_0();
B
bellard 已提交
3643
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(1)));
B
bellard 已提交
3644 3645 3646
                op1_offset = offsetof(CPUX86State,xmm_t0);
            } else {
                gen_op_movl_T0_im(val);
B
bellard 已提交
3647
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,mmx_t0.MMX_L(0)));
B
bellard 已提交
3648
                gen_op_movl_T0_0();
B
bellard 已提交
3649
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,mmx_t0.MMX_L(1)));
B
bellard 已提交
3650 3651
                op1_offset = offsetof(CPUX86State,mmx_t0);
            }
B
Blue Swirl 已提交
3652 3653 3654
            sse_fn_epp = sse_op_table2[((b - 1) & 3) * 8 +
                                       (((modrm >> 3)) & 7)][b1];
            if (!sse_fn_epp) {
B
bellard 已提交
3655
                goto illegal_op;
B
Blue Swirl 已提交
3656
            }
B
bellard 已提交
3657 3658 3659 3660 3661 3662 3663
            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 已提交
3664 3665
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op2_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op1_offset);
B
Blue Swirl 已提交
3666
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3667 3668 3669
            break;
        case 0x050: /* movmskps */
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
3670 3671
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                             offsetof(CPUX86State,xmm_regs[rm]));
B
Blue Swirl 已提交
3672
            gen_helper_movmskps(cpu_tmp2_i32, cpu_env, cpu_ptr0);
3673
            tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
3674
            gen_op_mov_reg_T0(OT_LONG, reg);
B
bellard 已提交
3675 3676 3677
            break;
        case 0x150: /* movmskpd */
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
3678 3679
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                             offsetof(CPUX86State,xmm_regs[rm]));
B
Blue Swirl 已提交
3680
            gen_helper_movmskpd(cpu_tmp2_i32, cpu_env, cpu_ptr0);
3681
            tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
3682
            gen_op_mov_reg_T0(OT_LONG, reg);
B
bellard 已提交
3683 3684 3685
            break;
        case 0x02a: /* cvtpi2ps */
        case 0x12a: /* cvtpi2pd */
B
Blue Swirl 已提交
3686
            gen_helper_enter_mmx(cpu_env);
B
bellard 已提交
3687
            if (mod != 3) {
3688
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3689
                op2_offset = offsetof(CPUX86State,mmx_t0);
B
bellard 已提交
3690
                gen_ldq_env_A0(s->mem_index, op2_offset);
B
bellard 已提交
3691 3692 3693 3694 3695
            } else {
                rm = (modrm & 7);
                op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
            }
            op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
B
bellard 已提交
3696 3697
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
bellard 已提交
3698 3699
            switch(b >> 8) {
            case 0x0:
B
Blue Swirl 已提交
3700
                gen_helper_cvtpi2ps(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3701 3702 3703
                break;
            default:
            case 0x1:
B
Blue Swirl 已提交
3704
                gen_helper_cvtpi2pd(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3705 3706 3707 3708 3709 3710
                break;
            }
            break;
        case 0x22a: /* cvtsi2ss */
        case 0x32a: /* cvtsi2sd */
            ot = (s->dflag == 2) ? OT_QUAD : OT_LONG;
3711
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
bellard 已提交
3712
            op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
B
bellard 已提交
3713
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
B
bellard 已提交
3714
            if (ot == OT_LONG) {
B
Blue Swirl 已提交
3715
                SSEFunc_0_epi sse_fn_epi = sse_op_table3ai[(b >> 8) & 1];
B
bellard 已提交
3716
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
3717
                sse_fn_epi(cpu_env, cpu_ptr0, cpu_tmp2_i32);
B
bellard 已提交
3718
            } else {
3719
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
3720 3721
                SSEFunc_0_epl sse_fn_epl = sse_op_table3aq[(b >> 8) & 1];
                sse_fn_epl(cpu_env, cpu_ptr0, cpu_T[0]);
3722 3723 3724
#else
                goto illegal_op;
#endif
B
bellard 已提交
3725
            }
B
bellard 已提交
3726 3727 3728 3729 3730
            break;
        case 0x02c: /* cvttps2pi */
        case 0x12c: /* cvttpd2pi */
        case 0x02d: /* cvtps2pi */
        case 0x12d: /* cvtpd2pi */
B
Blue Swirl 已提交
3731
            gen_helper_enter_mmx(cpu_env);
B
bellard 已提交
3732
            if (mod != 3) {
3733
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3734
                op2_offset = offsetof(CPUX86State,xmm_t0);
B
bellard 已提交
3735
                gen_ldo_env_A0(s->mem_index, op2_offset);
B
bellard 已提交
3736 3737 3738 3739 3740
            } else {
                rm = (modrm & 7) | REX_B(s);
                op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
            }
            op1_offset = offsetof(CPUX86State,fpregs[reg & 7].mmx);
B
bellard 已提交
3741 3742
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
bellard 已提交
3743 3744
            switch(b) {
            case 0x02c:
B
Blue Swirl 已提交
3745
                gen_helper_cvttps2pi(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3746 3747
                break;
            case 0x12c:
B
Blue Swirl 已提交
3748
                gen_helper_cvttpd2pi(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3749 3750
                break;
            case 0x02d:
B
Blue Swirl 已提交
3751
                gen_helper_cvtps2pi(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3752 3753
                break;
            case 0x12d:
B
Blue Swirl 已提交
3754
                gen_helper_cvtpd2pi(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3755 3756 3757 3758 3759 3760 3761 3762
                break;
            }
            break;
        case 0x22c: /* cvttss2si */
        case 0x32c: /* cvttsd2si */
        case 0x22d: /* cvtss2si */
        case 0x32d: /* cvtsd2si */
            ot = (s->dflag == 2) ? OT_QUAD : OT_LONG;
B
bellard 已提交
3763
            if (mod != 3) {
3764
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3765
                if ((b >> 8) & 1) {
B
bellard 已提交
3766
                    gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_t0.XMM_Q(0)));
B
bellard 已提交
3767
                } else {
B
bellard 已提交
3768
                    gen_op_ld_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
3769
                    tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(0)));
B
bellard 已提交
3770 3771 3772 3773 3774 3775
                }
                op2_offset = offsetof(CPUX86State,xmm_t0);
            } else {
                rm = (modrm & 7) | REX_B(s);
                op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
            }
B
bellard 已提交
3776 3777
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op2_offset);
            if (ot == OT_LONG) {
B
Blue Swirl 已提交
3778
                SSEFunc_i_ep sse_fn_i_ep =
3779
                    sse_op_table3bi[((b >> 7) & 2) | (b & 1)];
B
Blue Swirl 已提交
3780
                sse_fn_i_ep(cpu_tmp2_i32, cpu_env, cpu_ptr0);
3781
                tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
3782
            } else {
3783
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
3784
                SSEFunc_l_ep sse_fn_l_ep =
3785
                    sse_op_table3bq[((b >> 7) & 2) | (b & 1)];
B
Blue Swirl 已提交
3786
                sse_fn_l_ep(cpu_T[0], cpu_env, cpu_ptr0);
3787 3788 3789
#else
                goto illegal_op;
#endif
B
bellard 已提交
3790
            }
B
bellard 已提交
3791
            gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
3792 3793
            break;
        case 0xc4: /* pinsrw */
3794
        case 0x1c4:
B
bellard 已提交
3795
            s->rip_offset = 1;
3796 3797
            gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
3798 3799
            if (b1) {
                val &= 7;
B
bellard 已提交
3800 3801
                tcg_gen_st16_tl(cpu_T[0], cpu_env,
                                offsetof(CPUX86State,xmm_regs[reg].XMM_W(val)));
B
bellard 已提交
3802 3803
            } else {
                val &= 3;
B
bellard 已提交
3804 3805
                tcg_gen_st16_tl(cpu_T[0], cpu_env,
                                offsetof(CPUX86State,fpregs[reg].mmx.MMX_W(val)));
B
bellard 已提交
3806 3807 3808
            }
            break;
        case 0xc5: /* pextrw */
3809
        case 0x1c5:
B
bellard 已提交
3810 3811
            if (mod != 3)
                goto illegal_op;
3812
            ot = (s->dflag == 2) ? OT_QUAD : OT_LONG;
3813
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
3814 3815 3816
            if (b1) {
                val &= 7;
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
3817 3818
                tcg_gen_ld16u_tl(cpu_T[0], cpu_env,
                                 offsetof(CPUX86State,xmm_regs[rm].XMM_W(val)));
B
bellard 已提交
3819 3820 3821
            } else {
                val &= 3;
                rm = (modrm & 7);
B
bellard 已提交
3822 3823
                tcg_gen_ld16u_tl(cpu_T[0], cpu_env,
                                offsetof(CPUX86State,fpregs[rm].mmx.MMX_W(val)));
B
bellard 已提交
3824 3825
            }
            reg = ((modrm >> 3) & 7) | rex_r;
3826
            gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
3827 3828 3829
            break;
        case 0x1d6: /* movq ea, xmm */
            if (mod != 3) {
3830
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3831
                gen_stq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3832 3833 3834 3835 3836 3837 3838 3839
            } 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 已提交
3840
            gen_helper_enter_mmx(cpu_env);
3841 3842 3843 3844
            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 已提交
3845 3846
            break;
        case 0x3d6: /* movdq2q */
B
Blue Swirl 已提交
3847
            gen_helper_enter_mmx(cpu_env);
3848 3849 3850
            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 已提交
3851 3852 3853 3854 3855 3856 3857
            break;
        case 0xd7: /* pmovmskb */
        case 0x1d7:
            if (mod != 3)
                goto illegal_op;
            if (b1) {
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
3858
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, offsetof(CPUX86State,xmm_regs[rm]));
B
Blue Swirl 已提交
3859
                gen_helper_pmovmskb_xmm(cpu_tmp2_i32, cpu_env, cpu_ptr0);
B
bellard 已提交
3860 3861
            } else {
                rm = (modrm & 7);
B
bellard 已提交
3862
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, offsetof(CPUX86State,fpregs[rm].mmx));
B
Blue Swirl 已提交
3863
                gen_helper_pmovmskb_mmx(cpu_tmp2_i32, cpu_env, cpu_ptr0);
B
bellard 已提交
3864
            }
3865
            tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
3866
            reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
3867
            gen_op_mov_reg_T0(OT_LONG, reg);
B
bellard 已提交
3868
            break;
R
Richard Henderson 已提交
3869

B
balrog 已提交
3870
        case 0x138:
3871
        case 0x038:
B
balrog 已提交
3872
            b = modrm;
R
Richard Henderson 已提交
3873 3874 3875
            if ((b & 0xf0) == 0xf0) {
                goto do_0f_38_fx;
            }
3876
            modrm = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
3877 3878 3879
            rm = modrm & 7;
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
3880 3881 3882
            if (b1 >= 2) {
                goto illegal_op;
            }
B
balrog 已提交
3883

B
Blue Swirl 已提交
3884 3885
            sse_fn_epp = sse_op_table6[b].op[b1];
            if (!sse_fn_epp) {
B
balrog 已提交
3886
                goto illegal_op;
B
Blue Swirl 已提交
3887
            }
B
balrog 已提交
3888 3889
            if (!(s->cpuid_ext_features & sse_op_table6[b].ext_mask))
                goto illegal_op;
B
balrog 已提交
3890 3891 3892 3893 3894 3895 3896

            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);
3897
                    gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
balrog 已提交
3898 3899 3900 3901 3902 3903 3904 3905 3906
                    switch (b) {
                    case 0x20: case 0x30: /* pmovsxbw, pmovzxbw */
                    case 0x23: case 0x33: /* pmovsxwd, pmovzxwd */
                    case 0x25: case 0x35: /* pmovsxdq, pmovzxdq */
                        gen_ldq_env_A0(s->mem_index, op2_offset +
                                        offsetof(XMMReg, XMM_Q(0)));
                        break;
                    case 0x21: case 0x31: /* pmovsxbd, pmovzxbd */
                    case 0x24: case 0x34: /* pmovsxwq, pmovzxwq */
P
pbrook 已提交
3907
                        tcg_gen_qemu_ld32u(cpu_tmp0, cpu_A0,
B
balrog 已提交
3908
                                          (s->mem_index >> 2) - 1);
P
pbrook 已提交
3909
                        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_tmp0);
B
balrog 已提交
3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924
                        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, op2_offset +
                                        offsetof(XMMReg, XMM_L(0)));
                        break;
                    case 0x22: case 0x32: /* pmovsxbq, pmovzxbq */
                        tcg_gen_qemu_ld16u(cpu_tmp0, cpu_A0,
                                          (s->mem_index >> 2) - 1);
                        tcg_gen_st16_tl(cpu_tmp0, cpu_env, op2_offset +
                                        offsetof(XMMReg, XMM_W(0)));
                        break;
                    case 0x2a:            /* movntqda */
                        gen_ldo_env_A0(s->mem_index, op1_offset);
                        return;
                    default:
                        gen_ldo_env_A0(s->mem_index, op2_offset);
                    }
B
balrog 已提交
3925 3926 3927 3928 3929 3930 3931
                }
            } 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);
3932
                    gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
balrog 已提交
3933 3934 3935
                    gen_ldq_env_A0(s->mem_index, op2_offset);
                }
            }
B
Blue Swirl 已提交
3936
            if (sse_fn_epp == SSE_SPECIAL) {
B
balrog 已提交
3937
                goto illegal_op;
B
Blue Swirl 已提交
3938
            }
B
balrog 已提交
3939

B
balrog 已提交
3940 3941
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
3942
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
balrog 已提交
3943

3944 3945 3946
            if (b == 0x17) {
                set_cc_op(s, CC_OP_EFLAGS);
            }
B
balrog 已提交
3947
            break;
R
Richard Henderson 已提交
3948 3949 3950 3951 3952 3953

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

R
Richard Henderson 已提交
3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970
            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) {
                    ot = OT_BYTE;
                } else if (s->dflag != 2) {
                    ot = (s->prefix & PREFIX_DATA ? OT_WORD : OT_LONG);
                } else {
                    ot = OT_QUAD;
                }
B
balrog 已提交
3971

R
Richard Henderson 已提交
3972 3973 3974 3975 3976
                gen_op_mov_TN_reg(OT_LONG, 0, reg);
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
                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 已提交
3977

R
Richard Henderson 已提交
3978 3979 3980
                ot = (s->dflag == 2) ? OT_QUAD : OT_LONG;
                gen_op_mov_reg_T0(ot, reg);
                break;
B
balrog 已提交
3981

R
Richard Henderson 已提交
3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042
            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) {
                    ot = (s->prefix & PREFIX_DATA ? OT_WORD : OT_LONG);
                } else {
                    ot = OT_QUAD;
                }

                /* Load the data incoming to the bswap.  Note that the TCG
                   implementation of bswap requires the input be zero
                   extended.  In the case of the loads, we simply know that
                   gen_op_ld_v via gen_ldst_modrm does that already.  */
                if ((b & 1) == 0) {
                    gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                } else {
                    switch (ot) {
                    case OT_WORD:
                        tcg_gen_ext16u_tl(cpu_T[0], cpu_regs[reg]);
                        break;
                    default:
                        tcg_gen_ext32u_tl(cpu_T[0], cpu_regs[reg]);
                        break;
                    case OT_QUAD:
                        tcg_gen_mov_tl(cpu_T[0], cpu_regs[reg]);
                        break;
                    }
                }

                switch (ot) {
                case OT_WORD:
                    tcg_gen_bswap16_tl(cpu_T[0], cpu_T[0]);
                    break;
                default:
                    tcg_gen_bswap32_tl(cpu_T[0], cpu_T[0]);
                    break;
#ifdef TARGET_X86_64
                case OT_QUAD:
                    tcg_gen_bswap64_tl(cpu_T[0], cpu_T[0]);
                    break;
#endif
                }

                if ((b & 1) == 0) {
                    gen_op_mov_reg_T0(ot, reg);
                } else {
                    gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
                }
                break;

R
Richard Henderson 已提交
4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056
            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;
                }
                ot = s->dflag == 2 ? OT_QUAD : OT_LONG;
                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 已提交
4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096
            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;
                }
                ot = s->dflag == 2 ? OT_QUAD : OT_LONG;
                {
                    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);

                    bound = tcg_const_tl(ot == OT_QUAD ? 63 : 31);
                    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 已提交
4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123
            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;
                }
                ot = s->dflag == 2 ? OT_QUAD : OT_LONG;
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                tcg_gen_ext8u_tl(cpu_T[1], cpu_regs[s->vex_v]);
                {
                    TCGv bound = tcg_const_tl(ot == OT_QUAD ? 63 : 31);
                    /* 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 已提交
4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 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
            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;
                }
                ot = s->dflag == 2 ? OT_QUAD : OT_LONG;
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                switch (ot) {
                    TCGv_i64 t0, t1;
                default:
                    t0 = tcg_temp_new_i64();
                    t1 = tcg_temp_new_i64();
#ifdef TARGET_X86_64
                    tcg_gen_ext32u_i64(t0, cpu_T[0]);
                    tcg_gen_ext32u_i64(t1, cpu_regs[R_EDX]);
#else
                    tcg_gen_extu_i32_i64(t0, cpu_T[0]);
                    tcg_gen_extu_i32_i64(t0, cpu_regs[R_EDX]);
#endif
                    tcg_gen_mul_i64(t0, t0, t1);
                    tcg_gen_trunc_i64_tl(cpu_T[0], t0);
                    tcg_gen_shri_i64(t0, t0, 32);
                    tcg_gen_trunc_i64_tl(cpu_T[1], t0);
                    tcg_temp_free_i64(t0);
                    tcg_temp_free_i64(t1);
                    gen_op_mov_reg_T0(OT_LONG, s->vex_v);
                    gen_op_mov_reg_T1(OT_LONG, reg);
                    break;
#ifdef TARGET_X86_64
                case OT_QUAD:
                    tcg_gen_mov_tl(cpu_T[1], cpu_regs[R_EDX]);
                    tcg_gen_mul_tl(cpu_regs[s->vex_v], cpu_T[0], cpu_T[1]);
                    gen_helper_umulh(cpu_regs[reg], cpu_T[0], cpu_T[1]);
                    break;
#endif
                }
                break;

4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198
            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;
                }
                ot = s->dflag == 2 ? OT_QUAD : OT_LONG;
                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;
                }
                ot = s->dflag == 2 ? OT_QUAD : OT_LONG;
                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;

4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279
            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 {
                    TCGv carry_in, carry_out;
                    int end_op;

                    ot = (s->dflag == 2 ? OT_QUAD : OT_LONG);
                    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:
                        end_op = (b == 0x1f6 ? CC_OP_ADCX : CC_OP_ADCOX);
                        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
                    case OT_LONG:
                        /* 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.  */
                        tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_regs[reg]);
                        tcg_gen_setcond_tl(TCG_COND_LTU, cpu_tmp4,
                                           cpu_T[0], cpu_regs[reg]);
                        tcg_gen_add_tl(cpu_regs[reg], cpu_T[0], carry_in);
                        tcg_gen_setcond_tl(TCG_COND_LTU, carry_out,
                                           cpu_regs[reg], cpu_T[0]);
                        tcg_gen_or_tl(carry_out, carry_out, cpu_tmp4);
                        break;
                    }
                    /* We began with all flags computed to CC_SRC, and we
                       have now placed the carry-out in CC_DST.  All that
                       is left is to record the CC_OP.  */
                    set_cc_op(s, end_op);
                }
                break;

4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310
            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;
                }
                ot = (s->dflag == 2 ? OT_QUAD : OT_LONG);
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                if (ot == OT_QUAD) {
                    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) {
                    if (ot != OT_QUAD) {
                        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
                    }
                    tcg_gen_sar_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
                } else {
                    if (ot != OT_QUAD) {
                        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;

4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352
            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;
                }
                ot = s->dflag == 2 ? OT_QUAD : OT_LONG;
                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 已提交
4353 4354 4355
            default:
                goto illegal_op;
            }
B
balrog 已提交
4356
            break;
R
Richard Henderson 已提交
4357

B
balrog 已提交
4358 4359
        case 0x03a:
        case 0x13a:
B
balrog 已提交
4360
            b = modrm;
4361
            modrm = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
4362 4363 4364
            rm = modrm & 7;
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
4365 4366 4367
            if (b1 >= 2) {
                goto illegal_op;
            }
B
balrog 已提交
4368

B
Blue Swirl 已提交
4369 4370
            sse_fn_eppi = sse_op_table7[b].op[b1];
            if (!sse_fn_eppi) {
B
balrog 已提交
4371
                goto illegal_op;
B
Blue Swirl 已提交
4372
            }
B
balrog 已提交
4373 4374 4375
            if (!(s->cpuid_ext_features & sse_op_table7[b].ext_mask))
                goto illegal_op;

B
Blue Swirl 已提交
4376
            if (sse_fn_eppi == SSE_SPECIAL) {
B
balrog 已提交
4377 4378 4379
                ot = (s->dflag == 2) ? OT_QUAD : OT_LONG;
                rm = (modrm & 7) | REX_B(s);
                if (mod != 3)
4380
                    gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
balrog 已提交
4381
                reg = ((modrm >> 3) & 7) | rex_r;
4382
                val = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406
                switch (b) {
                case 0x14: /* pextrb */
                    tcg_gen_ld8u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
                                            xmm_regs[reg].XMM_B(val & 15)));
                    if (mod == 3)
                        gen_op_mov_reg_T0(ot, rm);
                    else
                        tcg_gen_qemu_st8(cpu_T[0], cpu_A0,
                                        (s->mem_index >> 2) - 1);
                    break;
                case 0x15: /* pextrw */
                    tcg_gen_ld16u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
                                            xmm_regs[reg].XMM_W(val & 7)));
                    if (mod == 3)
                        gen_op_mov_reg_T0(ot, rm);
                    else
                        tcg_gen_qemu_st16(cpu_T[0], cpu_A0,
                                        (s->mem_index >> 2) - 1);
                    break;
                case 0x16:
                    if (ot == OT_LONG) { /* pextrd */
                        tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env,
                                        offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_L(val & 3)));
P
pbrook 已提交
4407
                        tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
balrog 已提交
4408
                        if (mod == 3)
P
pbrook 已提交
4409
                            gen_op_mov_reg_v(ot, rm, cpu_T[0]);
B
balrog 已提交
4410
                        else
P
pbrook 已提交
4411
                            tcg_gen_qemu_st32(cpu_T[0], cpu_A0,
B
balrog 已提交
4412 4413
                                            (s->mem_index >> 2) - 1);
                    } else { /* pextrq */
P
pbrook 已提交
4414
#ifdef TARGET_X86_64
B
balrog 已提交
4415 4416 4417 4418 4419 4420 4421 4422
                        tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env,
                                        offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_Q(val & 1)));
                        if (mod == 3)
                            gen_op_mov_reg_v(ot, rm, cpu_tmp1_i64);
                        else
                            tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_A0,
                                            (s->mem_index >> 2) - 1);
P
pbrook 已提交
4423 4424 4425
#else
                        goto illegal_op;
#endif
B
balrog 已提交
4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440
                    }
                    break;
                case 0x17: /* extractps */
                    tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
                                            xmm_regs[reg].XMM_L(val & 3)));
                    if (mod == 3)
                        gen_op_mov_reg_T0(ot, rm);
                    else
                        tcg_gen_qemu_st32(cpu_T[0], cpu_A0,
                                        (s->mem_index >> 2) - 1);
                    break;
                case 0x20: /* pinsrb */
                    if (mod == 3)
                        gen_op_mov_TN_reg(OT_LONG, 0, rm);
                    else
P
pbrook 已提交
4441
                        tcg_gen_qemu_ld8u(cpu_tmp0, cpu_A0,
B
balrog 已提交
4442
                                        (s->mem_index >> 2) - 1);
P
pbrook 已提交
4443
                    tcg_gen_st8_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State,
B
balrog 已提交
4444 4445 4446
                                            xmm_regs[reg].XMM_B(val & 15)));
                    break;
                case 0x21: /* insertps */
P
pbrook 已提交
4447
                    if (mod == 3) {
B
balrog 已提交
4448 4449 4450
                        tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env,
                                        offsetof(CPUX86State,xmm_regs[rm]
                                                .XMM_L((val >> 6) & 3)));
P
pbrook 已提交
4451 4452
                    } else {
                        tcg_gen_qemu_ld32u(cpu_tmp0, cpu_A0,
B
balrog 已提交
4453
                                        (s->mem_index >> 2) - 1);
P
pbrook 已提交
4454 4455
                        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_tmp0);
                    }
B
balrog 已提交
4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478
                    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:
                    if (ot == OT_LONG) { /* pinsrd */
                        if (mod == 3)
P
pbrook 已提交
4479
                            gen_op_mov_v_reg(ot, cpu_tmp0, rm);
B
balrog 已提交
4480
                        else
P
pbrook 已提交
4481
                            tcg_gen_qemu_ld32u(cpu_tmp0, cpu_A0,
B
balrog 已提交
4482
                                            (s->mem_index >> 2) - 1);
P
pbrook 已提交
4483
                        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_tmp0);
B
balrog 已提交
4484 4485 4486 4487
                        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env,
                                        offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_L(val & 3)));
                    } else { /* pinsrq */
P
pbrook 已提交
4488
#ifdef TARGET_X86_64
B
balrog 已提交
4489 4490 4491 4492 4493 4494 4495 4496
                        if (mod == 3)
                            gen_op_mov_v_reg(ot, cpu_tmp1_i64, rm);
                        else
                            tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_A0,
                                            (s->mem_index >> 2) - 1);
                        tcg_gen_st_i64(cpu_tmp1_i64, cpu_env,
                                        offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_Q(val & 1)));
P
pbrook 已提交
4497 4498 4499
#else
                        goto illegal_op;
#endif
B
balrog 已提交
4500 4501 4502 4503 4504
                    }
                    break;
                }
                return;
            }
B
balrog 已提交
4505 4506 4507 4508 4509 4510 4511

            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);
4512
                    gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
balrog 已提交
4513 4514 4515 4516 4517 4518 4519 4520
                    gen_ldo_env_A0(s->mem_index, op2_offset);
                }
            } 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);
4521
                    gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
balrog 已提交
4522 4523 4524
                    gen_ldq_env_A0(s->mem_index, op2_offset);
                }
            }
4525
            val = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
4526

B
balrog 已提交
4527
            if ((b & 0xfc) == 0x60) { /* pcmpXstrX */
4528
                set_cc_op(s, CC_OP_EFLAGS);
B
balrog 已提交
4529 4530 4531 4532 4533 4534

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

B
balrog 已提交
4535 4536
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4537
            sse_fn_eppi(cpu_env, cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
B
balrog 已提交
4538
            break;
R
Richard Henderson 已提交
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

        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;
                }
                ot = s->dflag == 2 ? OT_QUAD : OT_LONG;
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                b = cpu_ldub_code(env, s->pc++);
                if (ot == OT_QUAD) {
                    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 已提交
4571 4572 4573 4574 4575
        default:
            goto illegal_op;
        }
    } else {
        /* generic MMX or SSE operation */
B
bellard 已提交
4576 4577 4578 4579 4580 4581 4582 4583
        switch(b) {
        case 0x70: /* pshufx insn */
        case 0xc6: /* pshufx insn */
        case 0xc2: /* compare insns */
            s->rip_offset = 1;
            break;
        default:
            break;
B
bellard 已提交
4584 4585 4586 4587
        }
        if (is_xmm) {
            op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
            if (mod != 3) {
4588
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
4589
                op2_offset = offsetof(CPUX86State,xmm_t0);
4590
                if (b1 >= 2 && ((b >= 0x50 && b <= 0x5f && b != 0x5b) ||
B
bellard 已提交
4591 4592 4593 4594
                                b == 0xc2)) {
                    /* specific case for SSE single instructions */
                    if (b1 == 2) {
                        /* 32 bit access */
B
bellard 已提交
4595
                        gen_op_ld_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
4596
                        tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(0)));
B
bellard 已提交
4597 4598
                    } else {
                        /* 64 bit access */
B
bellard 已提交
4599
                        gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_t0.XMM_D(0)));
B
bellard 已提交
4600 4601
                    }
                } else {
B
bellard 已提交
4602
                    gen_ldo_env_A0(s->mem_index, op2_offset);
B
bellard 已提交
4603 4604 4605 4606 4607 4608 4609 4610
                }
            } 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) {
4611
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
4612
                op2_offset = offsetof(CPUX86State,mmx_t0);
B
bellard 已提交
4613
                gen_ldq_env_A0(s->mem_index, op2_offset);
B
bellard 已提交
4614 4615 4616 4617 4618 4619
            } else {
                rm = (modrm & 7);
                op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
            }
        }
        switch(b) {
A
aurel32 已提交
4620
        case 0x0f: /* 3DNow! data insns */
4621 4622
            if (!(s->cpuid_ext2_features & CPUID_EXT2_3DNOW))
                goto illegal_op;
4623
            val = cpu_ldub_code(env, s->pc++);
B
Blue Swirl 已提交
4624 4625
            sse_fn_epp = sse_op_table5[val];
            if (!sse_fn_epp) {
A
aurel32 已提交
4626
                goto illegal_op;
B
Blue Swirl 已提交
4627
            }
B
bellard 已提交
4628 4629
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4630
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
A
aurel32 已提交
4631
            break;
B
bellard 已提交
4632 4633
        case 0x70: /* pshufx insn */
        case 0xc6: /* pshufx insn */
4634
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4635 4636
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4637
            /* XXX: introduce a new table? */
B
Blue Swirl 已提交
4638
            sse_fn_ppi = (SSEFunc_0_ppi)sse_fn_epp;
B
Blue Swirl 已提交
4639
            sse_fn_ppi(cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
B
bellard 已提交
4640 4641 4642
            break;
        case 0xc2:
            /* compare insns */
4643
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4644 4645
            if (val >= 8)
                goto illegal_op;
B
Blue Swirl 已提交
4646
            sse_fn_epp = sse_op_table4[val][b1];
B
Blue Swirl 已提交
4647

B
bellard 已提交
4648 4649
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4650
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
4651
            break;
4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669
        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 已提交
4670
            /* XXX: introduce a new table? */
B
Blue Swirl 已提交
4671 4672
            sse_fn_eppt = (SSEFunc_0_eppt)sse_fn_epp;
            sse_fn_eppt(cpu_env, cpu_ptr0, cpu_ptr1, cpu_A0);
4673
            break;
B
bellard 已提交
4674
        default:
B
bellard 已提交
4675 4676
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4677
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
4678 4679 4680
            break;
        }
        if (b == 0x2e || b == 0x2f) {
4681
            set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
4682 4683 4684 4685
        }
    }
}

B
bellard 已提交
4686 4687
/* convert one instruction. s->is_jmp is set if the translation must
   be stopped. Return the next pc value */
4688 4689
static target_ulong disas_insn(CPUX86State *env, DisasContext *s,
                               target_ulong pc_start)
B
bellard 已提交
4690 4691 4692 4693
{
    int b, prefixes, aflag, dflag;
    int shift, ot;
    int modrm, reg, rm, mod, reg_addr, op, opreg, offset_addr, val;
B
bellard 已提交
4694 4695
    target_ulong next_eip, tval;
    int rex_w, rex_r;
B
bellard 已提交
4696

4697
    if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
4698
        tcg_gen_debug_insn_start(pc_start);
4699
    }
B
bellard 已提交
4700 4701 4702 4703 4704
    s->pc = pc_start;
    prefixes = 0;
    aflag = s->code32;
    dflag = s->code32;
    s->override = -1;
B
bellard 已提交
4705 4706 4707 4708 4709
    rex_w = -1;
    rex_r = 0;
#ifdef TARGET_X86_64
    s->rex_x = 0;
    s->rex_b = 0;
4710
    x86_64_hregs = 0;
B
bellard 已提交
4711 4712
#endif
    s->rip_offset = 0; /* for relative ip address */
4713 4714
    s->vex_l = 0;
    s->vex_v = 0;
B
bellard 已提交
4715
 next_byte:
4716
    b = cpu_ldub_code(env, s->pc);
B
bellard 已提交
4717
    s->pc++;
4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752
    /* 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 已提交
4753
#ifdef TARGET_X86_64
4754 4755
    case 0x40 ... 0x4f:
        if (CODE64(s)) {
B
bellard 已提交
4756 4757 4758 4759 4760 4761 4762 4763
            /* 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;
        }
4764 4765
        break;
#endif
4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822
    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++;

            /* 4.1.1-4.1.3: No preceeding lock, 66, f2, f3, or rex prefixes. */
            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;
4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833
    }

    /* Post-process prefixes.  */
    if (prefixes & PREFIX_DATA) {
        dflag ^= 1;
    }
    if (prefixes & PREFIX_ADR) {
        aflag ^= 1;
    }
#ifdef TARGET_X86_64
    if (CODE64(s)) {
B
bellard 已提交
4834 4835 4836 4837
        if (rex_w == 1) {
            /* 0x66 is ignored if rex.w is set */
            dflag = 2;
        }
4838
        if (!(prefixes & PREFIX_ADR)) {
B
bellard 已提交
4839 4840
            aflag = 2;
        }
B
bellard 已提交
4841
    }
4842
#endif
B
bellard 已提交
4843 4844 4845 4846 4847 4848 4849

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

    /* lock generation */
    if (prefixes & PREFIX_LOCK)
P
pbrook 已提交
4850
        gen_helper_lock();
B
bellard 已提交
4851 4852 4853 4854 4855 4856 4857

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

B
bellard 已提交
4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878
        /**************************/
        /* 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)
                ot = OT_BYTE;
            else
B
bellard 已提交
4879
                ot = dflag + OT_WORD;
4880

B
bellard 已提交
4881 4882
            switch(f) {
            case 0: /* OP Ev, Gv */
4883
                modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4884
                reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
4885
                mod = (modrm >> 6) & 3;
B
bellard 已提交
4886
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4887
                if (mod != 3) {
4888
                    gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
4889 4890 4891 4892 4893
                    opreg = OR_TMP0;
                } else if (op == OP_XORL && rm == reg) {
                xor_zero:
                    /* xor reg, reg optimisation */
                    gen_op_movl_T0_0();
4894
                    set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
4895
                    gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
4896 4897 4898 4899 4900
                    gen_op_update1_cc();
                    break;
                } else {
                    opreg = rm;
                }
B
bellard 已提交
4901
                gen_op_mov_TN_reg(ot, 1, reg);
B
bellard 已提交
4902 4903 4904
                gen_op(s, op, ot, opreg);
                break;
            case 1: /* OP Gv, Ev */
4905
                modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4906
                mod = (modrm >> 6) & 3;
B
bellard 已提交
4907 4908
                reg = ((modrm >> 3) & 7) | rex_r;
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4909
                if (mod != 3) {
4910
                    gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
4911
                    gen_op_ld_T1_A0(ot + s->mem_index);
B
bellard 已提交
4912 4913 4914
                } else if (op == OP_XORL && rm == reg) {
                    goto xor_zero;
                } else {
B
bellard 已提交
4915
                    gen_op_mov_TN_reg(ot, 1, rm);
B
bellard 已提交
4916 4917 4918 4919
                }
                gen_op(s, op, ot, reg);
                break;
            case 2: /* OP A, Iv */
4920
                val = insn_get(env, s, ot);
B
bellard 已提交
4921 4922 4923 4924 4925 4926 4927
                gen_op_movl_T1_im(val);
                gen_op(s, op, ot, OR_EAX);
                break;
            }
        }
        break;

4928 4929 4930
    case 0x82:
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
4931 4932 4933 4934 4935 4936 4937 4938 4939
    case 0x80: /* GRP1 */
    case 0x81:
    case 0x83:
        {
            int val;

            if ((b & 1) == 0)
                ot = OT_BYTE;
            else
B
bellard 已提交
4940
                ot = dflag + OT_WORD;
4941

4942
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4943
            mod = (modrm >> 6) & 3;
B
bellard 已提交
4944
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4945
            op = (modrm >> 3) & 7;
4946

B
bellard 已提交
4947
            if (mod != 3) {
B
bellard 已提交
4948 4949 4950 4951
                if (b == 0x83)
                    s->rip_offset = 1;
                else
                    s->rip_offset = insn_const_size(ot);
4952
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
4953 4954
                opreg = OR_TMP0;
            } else {
B
bellard 已提交
4955
                opreg = rm;
B
bellard 已提交
4956 4957 4958 4959 4960 4961
            }

            switch(b) {
            default:
            case 0x80:
            case 0x81:
4962
            case 0x82:
4963
                val = insn_get(env, s, ot);
B
bellard 已提交
4964 4965
                break;
            case 0x83:
4966
                val = (int8_t)insn_get(env, s, OT_BYTE);
B
bellard 已提交
4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988
                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 */
        ot = dflag ? OT_LONG : OT_WORD;
        gen_inc(s, ot, OR_EAX + (b & 7), 1);
        break;
    case 0x48 ... 0x4f: /* dec Gv */
        ot = dflag ? OT_LONG : OT_WORD;
        gen_inc(s, ot, OR_EAX + (b & 7), -1);
        break;
    case 0xf6: /* GRP3 */
    case 0xf7:
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
4989
            ot = dflag + OT_WORD;
B
bellard 已提交
4990

4991
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4992
        mod = (modrm >> 6) & 3;
B
bellard 已提交
4993
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4994 4995
        op = (modrm >> 3) & 7;
        if (mod != 3) {
B
bellard 已提交
4996 4997
            if (op == 0)
                s->rip_offset = insn_const_size(ot);
4998
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
4999
            gen_op_ld_T0_A0(ot + s->mem_index);
B
bellard 已提交
5000
        } else {
B
bellard 已提交
5001
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
5002 5003 5004 5005
        }

        switch(op) {
        case 0: /* test */
5006
            val = insn_get(env, s, ot);
B
bellard 已提交
5007 5008
            gen_op_movl_T1_im(val);
            gen_op_testl_T0_T1_cc();
5009
            set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
5010 5011
            break;
        case 2: /* not */
5012
            tcg_gen_not_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5013
            if (mod != 3) {
B
bellard 已提交
5014
                gen_op_st_T0_A0(ot + s->mem_index);
B
bellard 已提交
5015
            } else {
B
bellard 已提交
5016
                gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
5017 5018 5019
            }
            break;
        case 3: /* neg */
5020
            tcg_gen_neg_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5021
            if (mod != 3) {
B
bellard 已提交
5022
                gen_op_st_T0_A0(ot + s->mem_index);
B
bellard 已提交
5023
            } else {
B
bellard 已提交
5024
                gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
5025 5026
            }
            gen_op_update_neg_cc();
5027
            set_cc_op(s, CC_OP_SUBB + ot);
B
bellard 已提交
5028 5029 5030 5031
            break;
        case 4: /* mul */
            switch(ot) {
            case OT_BYTE:
B
bellard 已提交
5032 5033 5034 5035 5036 5037 5038 5039
                gen_op_mov_TN_reg(OT_BYTE, 1, R_EAX);
                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]);
                gen_op_mov_reg_T0(OT_WORD, R_EAX);
                tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                tcg_gen_andi_tl(cpu_cc_src, cpu_T[0], 0xff00);
5040
                set_cc_op(s, CC_OP_MULB);
B
bellard 已提交
5041 5042
                break;
            case OT_WORD:
B
bellard 已提交
5043 5044 5045 5046 5047 5048 5049 5050 5051 5052
                gen_op_mov_TN_reg(OT_WORD, 1, R_EAX);
                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]);
                gen_op_mov_reg_T0(OT_WORD, R_EAX);
                tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                tcg_gen_shri_tl(cpu_T[0], cpu_T[0], 16);
                gen_op_mov_reg_T0(OT_WORD, R_EDX);
                tcg_gen_mov_tl(cpu_cc_src, cpu_T[0]);
5053
                set_cc_op(s, CC_OP_MULW);
B
bellard 已提交
5054 5055 5056
                break;
            default:
            case OT_LONG:
B
bellard 已提交
5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068
#ifdef TARGET_X86_64
                gen_op_mov_TN_reg(OT_LONG, 1, R_EAX);
                tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
                tcg_gen_ext32u_tl(cpu_T[1], cpu_T[1]);
                tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
                gen_op_mov_reg_T0(OT_LONG, R_EAX);
                tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                tcg_gen_shri_tl(cpu_T[0], cpu_T[0], 32);
                gen_op_mov_reg_T0(OT_LONG, R_EDX);
                tcg_gen_mov_tl(cpu_cc_src, cpu_T[0]);
#else
                {
P
pbrook 已提交
5069 5070 5071
                    TCGv_i64 t0, t1;
                    t0 = tcg_temp_new_i64();
                    t1 = tcg_temp_new_i64();
B
bellard 已提交
5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084
                    gen_op_mov_TN_reg(OT_LONG, 1, R_EAX);
                    tcg_gen_extu_i32_i64(t0, cpu_T[0]);
                    tcg_gen_extu_i32_i64(t1, cpu_T[1]);
                    tcg_gen_mul_i64(t0, t0, t1);
                    tcg_gen_trunc_i64_i32(cpu_T[0], t0);
                    gen_op_mov_reg_T0(OT_LONG, R_EAX);
                    tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                    tcg_gen_shri_i64(t0, t0, 32);
                    tcg_gen_trunc_i64_i32(cpu_T[0], t0);
                    gen_op_mov_reg_T0(OT_LONG, R_EDX);
                    tcg_gen_mov_tl(cpu_cc_src, cpu_T[0]);
                }
#endif
5085
                set_cc_op(s, CC_OP_MULL);
B
bellard 已提交
5086
                break;
B
bellard 已提交
5087 5088
#ifdef TARGET_X86_64
            case OT_QUAD:
5089
                gen_helper_mulq_EAX_T0(cpu_env, cpu_T[0]);
5090
                set_cc_op(s, CC_OP_MULQ);
B
bellard 已提交
5091 5092
                break;
#endif
B
bellard 已提交
5093 5094 5095 5096 5097
            }
            break;
        case 5: /* imul */
            switch(ot) {
            case OT_BYTE:
B
bellard 已提交
5098 5099 5100 5101 5102 5103 5104 5105 5106
                gen_op_mov_TN_reg(OT_BYTE, 1, R_EAX);
                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]);
                gen_op_mov_reg_T0(OT_WORD, R_EAX);
                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);
5107
                set_cc_op(s, CC_OP_MULB);
B
bellard 已提交
5108 5109
                break;
            case OT_WORD:
B
bellard 已提交
5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120
                gen_op_mov_TN_reg(OT_WORD, 1, R_EAX);
                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]);
                gen_op_mov_reg_T0(OT_WORD, R_EAX);
                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);
                gen_op_mov_reg_T0(OT_WORD, R_EDX);
5121
                set_cc_op(s, CC_OP_MULW);
B
bellard 已提交
5122 5123 5124
                break;
            default:
            case OT_LONG:
B
bellard 已提交
5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137
#ifdef TARGET_X86_64
                gen_op_mov_TN_reg(OT_LONG, 1, R_EAX);
                tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
                tcg_gen_ext32s_tl(cpu_T[1], cpu_T[1]);
                tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
                gen_op_mov_reg_T0(OT_LONG, R_EAX);
                tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                tcg_gen_ext32s_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], 32);
                gen_op_mov_reg_T0(OT_LONG, R_EDX);
#else
                {
P
pbrook 已提交
5138 5139 5140
                    TCGv_i64 t0, t1;
                    t0 = tcg_temp_new_i64();
                    t1 = tcg_temp_new_i64();
B
bellard 已提交
5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154
                    gen_op_mov_TN_reg(OT_LONG, 1, R_EAX);
                    tcg_gen_ext_i32_i64(t0, cpu_T[0]);
                    tcg_gen_ext_i32_i64(t1, cpu_T[1]);
                    tcg_gen_mul_i64(t0, t0, t1);
                    tcg_gen_trunc_i64_i32(cpu_T[0], t0);
                    gen_op_mov_reg_T0(OT_LONG, R_EAX);
                    tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                    tcg_gen_sari_tl(cpu_tmp0, cpu_T[0], 31);
                    tcg_gen_shri_i64(t0, t0, 32);
                    tcg_gen_trunc_i64_i32(cpu_T[0], t0);
                    gen_op_mov_reg_T0(OT_LONG, R_EDX);
                    tcg_gen_sub_tl(cpu_cc_src, cpu_T[0], cpu_tmp0);
                }
#endif
5155
                set_cc_op(s, CC_OP_MULL);
B
bellard 已提交
5156
                break;
B
bellard 已提交
5157 5158
#ifdef TARGET_X86_64
            case OT_QUAD:
5159
                gen_helper_imulq_EAX_T0(cpu_env, cpu_T[0]);
5160
                set_cc_op(s, CC_OP_MULQ);
B
bellard 已提交
5161 5162
                break;
#endif
B
bellard 已提交
5163 5164 5165 5166 5167
            }
            break;
        case 6: /* div */
            switch(ot) {
            case OT_BYTE:
B
bellard 已提交
5168
                gen_jmp_im(pc_start - s->cs_base);
5169
                gen_helper_divb_AL(cpu_env, cpu_T[0]);
B
bellard 已提交
5170 5171
                break;
            case OT_WORD:
B
bellard 已提交
5172
                gen_jmp_im(pc_start - s->cs_base);
5173
                gen_helper_divw_AX(cpu_env, cpu_T[0]);
B
bellard 已提交
5174 5175 5176
                break;
            default:
            case OT_LONG:
B
bellard 已提交
5177
                gen_jmp_im(pc_start - s->cs_base);
5178
                gen_helper_divl_EAX(cpu_env, cpu_T[0]);
B
bellard 已提交
5179 5180 5181 5182
                break;
#ifdef TARGET_X86_64
            case OT_QUAD:
                gen_jmp_im(pc_start - s->cs_base);
5183
                gen_helper_divq_EAX(cpu_env, cpu_T[0]);
B
bellard 已提交
5184
                break;
B
bellard 已提交
5185
#endif
B
bellard 已提交
5186 5187 5188 5189 5190
            }
            break;
        case 7: /* idiv */
            switch(ot) {
            case OT_BYTE:
B
bellard 已提交
5191
                gen_jmp_im(pc_start - s->cs_base);
5192
                gen_helper_idivb_AL(cpu_env, cpu_T[0]);
B
bellard 已提交
5193 5194
                break;
            case OT_WORD:
B
bellard 已提交
5195
                gen_jmp_im(pc_start - s->cs_base);
5196
                gen_helper_idivw_AX(cpu_env, cpu_T[0]);
B
bellard 已提交
5197 5198 5199
                break;
            default:
            case OT_LONG:
B
bellard 已提交
5200
                gen_jmp_im(pc_start - s->cs_base);
5201
                gen_helper_idivl_EAX(cpu_env, cpu_T[0]);
B
bellard 已提交
5202 5203 5204 5205
                break;
#ifdef TARGET_X86_64
            case OT_QUAD:
                gen_jmp_im(pc_start - s->cs_base);
5206
                gen_helper_idivq_EAX(cpu_env, cpu_T[0]);
B
bellard 已提交
5207
                break;
B
bellard 已提交
5208
#endif
B
bellard 已提交
5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220
            }
            break;
        default:
            goto illegal_op;
        }
        break;

    case 0xfe: /* GRP4 */
    case 0xff: /* GRP5 */
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
5221
            ot = dflag + OT_WORD;
B
bellard 已提交
5222

5223
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5224
        mod = (modrm >> 6) & 3;
B
bellard 已提交
5225
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
5226 5227 5228 5229
        op = (modrm >> 3) & 7;
        if (op >= 2 && b == 0xfe) {
            goto illegal_op;
        }
B
bellard 已提交
5230
        if (CODE64(s)) {
5231
            if (op == 2 || op == 4) {
B
bellard 已提交
5232 5233
                /* operand size for jumps is 64 bit */
                ot = OT_QUAD;
5234
            } else if (op == 3 || op == 5) {
5235
                ot = dflag ? OT_LONG + (rex_w == 1) : OT_WORD;
B
bellard 已提交
5236 5237 5238 5239 5240
            } else if (op == 6) {
                /* default push size is 64 bit */
                ot = dflag ? OT_QUAD : OT_WORD;
            }
        }
B
bellard 已提交
5241
        if (mod != 3) {
5242
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
5243
            if (op >= 2 && op != 3 && op != 5)
B
bellard 已提交
5244
                gen_op_ld_T0_A0(ot + s->mem_index);
B
bellard 已提交
5245
        } else {
B
bellard 已提交
5246
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264
        }

        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 */
5265
            /* XXX: optimize if memory (no 'and' is necessary) */
B
bellard 已提交
5266 5267 5268
            if (s->dflag == 0)
                gen_op_andl_T0_ffff();
            next_eip = s->pc - s->cs_base;
B
bellard 已提交
5269
            gen_movtl_T1_im(next_eip);
5270 5271
            gen_push_T1(s);
            gen_op_jmp_T0();
B
bellard 已提交
5272 5273
            gen_eob(s);
            break;
B
bellard 已提交
5274
        case 3: /* lcall Ev */
B
bellard 已提交
5275
            gen_op_ld_T1_A0(ot + s->mem_index);
5276
            gen_add_A0_im(s, 1 << (ot - OT_WORD + 1));
B
bellard 已提交
5277
            gen_op_ldu_T0_A0(OT_WORD + s->mem_index);
B
bellard 已提交
5278 5279
        do_lcall:
            if (s->pe && !s->vm86) {
5280
                gen_update_cc_op(s);
B
bellard 已提交
5281
                gen_jmp_im(pc_start - s->cs_base);
5282
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
5283 5284
                gen_helper_lcall_protected(cpu_env, cpu_tmp2_i32, cpu_T[1],
                                           tcg_const_i32(dflag),
P
pbrook 已提交
5285
                                           tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
5286
            } else {
5287
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
5288 5289
                gen_helper_lcall_real(cpu_env, cpu_tmp2_i32, cpu_T[1],
                                      tcg_const_i32(dflag),
P
pbrook 已提交
5290
                                      tcg_const_i32(s->pc - s->cs_base));
B
bellard 已提交
5291 5292 5293 5294 5295 5296 5297 5298 5299 5300
            }
            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 */
B
bellard 已提交
5301
            gen_op_ld_T1_A0(ot + s->mem_index);
5302
            gen_add_A0_im(s, 1 << (ot - OT_WORD + 1));
B
bellard 已提交
5303
            gen_op_ldu_T0_A0(OT_WORD + s->mem_index);
B
bellard 已提交
5304 5305
        do_ljmp:
            if (s->pe && !s->vm86) {
5306
                gen_update_cc_op(s);
B
bellard 已提交
5307
                gen_jmp_im(pc_start - s->cs_base);
5308
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
5309
                gen_helper_ljmp_protected(cpu_env, cpu_tmp2_i32, cpu_T[1],
P
pbrook 已提交
5310
                                          tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
5311
            } else {
5312
                gen_op_movl_seg_T0_vm(R_CS);
B
bellard 已提交
5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326
                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 */
5327
    case 0x85:
B
bellard 已提交
5328 5329 5330
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
5331
            ot = dflag + OT_WORD;
B
bellard 已提交
5332

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

5336
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
bellard 已提交
5337
        gen_op_mov_TN_reg(ot, 1, reg);
B
bellard 已提交
5338
        gen_op_testl_T0_T1_cc();
5339
        set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
5340
        break;
5341

B
bellard 已提交
5342 5343 5344 5345 5346
    case 0xa8: /* test eAX, Iv */
    case 0xa9:
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
5347
            ot = dflag + OT_WORD;
5348
        val = insn_get(env, s, ot);
B
bellard 已提交
5349

B
bellard 已提交
5350
        gen_op_mov_TN_reg(ot, 0, OR_EAX);
B
bellard 已提交
5351 5352
        gen_op_movl_T1_im(val);
        gen_op_testl_T0_T1_cc();
5353
        set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
5354
        break;
5355

B
bellard 已提交
5356
    case 0x98: /* CWDE/CBW */
B
bellard 已提交
5357 5358
#ifdef TARGET_X86_64
        if (dflag == 2) {
B
bellard 已提交
5359 5360 5361
            gen_op_mov_TN_reg(OT_LONG, 0, R_EAX);
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
            gen_op_mov_reg_T0(OT_QUAD, R_EAX);
B
bellard 已提交
5362 5363
        } else
#endif
B
bellard 已提交
5364 5365 5366 5367 5368 5369 5370 5371 5372
        if (dflag == 1) {
            gen_op_mov_TN_reg(OT_WORD, 0, R_EAX);
            tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
            gen_op_mov_reg_T0(OT_LONG, R_EAX);
        } else {
            gen_op_mov_TN_reg(OT_BYTE, 0, R_EAX);
            tcg_gen_ext8s_tl(cpu_T[0], cpu_T[0]);
            gen_op_mov_reg_T0(OT_WORD, R_EAX);
        }
B
bellard 已提交
5373 5374
        break;
    case 0x99: /* CDQ/CWD */
B
bellard 已提交
5375 5376
#ifdef TARGET_X86_64
        if (dflag == 2) {
B
bellard 已提交
5377 5378 5379
            gen_op_mov_TN_reg(OT_QUAD, 0, R_EAX);
            tcg_gen_sari_tl(cpu_T[0], cpu_T[0], 63);
            gen_op_mov_reg_T0(OT_QUAD, R_EDX);
B
bellard 已提交
5380 5381
        } else
#endif
B
bellard 已提交
5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392
        if (dflag == 1) {
            gen_op_mov_TN_reg(OT_LONG, 0, R_EAX);
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
            tcg_gen_sari_tl(cpu_T[0], cpu_T[0], 31);
            gen_op_mov_reg_T0(OT_LONG, R_EDX);
        } else {
            gen_op_mov_TN_reg(OT_WORD, 0, R_EAX);
            tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
            tcg_gen_sari_tl(cpu_T[0], cpu_T[0], 15);
            gen_op_mov_reg_T0(OT_WORD, R_EDX);
        }
B
bellard 已提交
5393 5394 5395 5396
        break;
    case 0x1af: /* imul Gv, Ev */
    case 0x69: /* imul Gv, Ev, I */
    case 0x6b:
B
bellard 已提交
5397
        ot = dflag + OT_WORD;
5398
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5399 5400 5401 5402 5403
        reg = ((modrm >> 3) & 7) | rex_r;
        if (b == 0x69)
            s->rip_offset = insn_const_size(ot);
        else if (b == 0x6b)
            s->rip_offset = 1;
5404
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
bellard 已提交
5405
        if (b == 0x69) {
5406
            val = insn_get(env, s, ot);
B
bellard 已提交
5407 5408
            gen_op_movl_T1_im(val);
        } else if (b == 0x6b) {
5409
            val = (int8_t)insn_get(env, s, OT_BYTE);
B
bellard 已提交
5410 5411
            gen_op_movl_T1_im(val);
        } else {
B
bellard 已提交
5412
            gen_op_mov_TN_reg(ot, 1, reg);
B
bellard 已提交
5413 5414
        }

B
bellard 已提交
5415 5416
#ifdef TARGET_X86_64
        if (ot == OT_QUAD) {
5417
            gen_helper_imulq_T0_T1(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
B
bellard 已提交
5418 5419
        } else
#endif
B
bellard 已提交
5420
        if (ot == OT_LONG) {
B
bellard 已提交
5421 5422 5423 5424 5425 5426 5427 5428 5429
#ifdef TARGET_X86_64
                tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
                tcg_gen_ext32s_tl(cpu_T[1], cpu_T[1]);
                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_ext32s_tl(cpu_tmp0, cpu_T[0]);
                tcg_gen_sub_tl(cpu_cc_src, cpu_T[0], cpu_tmp0);
#else
                {
P
pbrook 已提交
5430 5431 5432
                    TCGv_i64 t0, t1;
                    t0 = tcg_temp_new_i64();
                    t1 = tcg_temp_new_i64();
B
bellard 已提交
5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443
                    tcg_gen_ext_i32_i64(t0, cpu_T[0]);
                    tcg_gen_ext_i32_i64(t1, cpu_T[1]);
                    tcg_gen_mul_i64(t0, t0, t1);
                    tcg_gen_trunc_i64_i32(cpu_T[0], t0);
                    tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                    tcg_gen_sari_tl(cpu_tmp0, cpu_T[0], 31);
                    tcg_gen_shri_i64(t0, t0, 32);
                    tcg_gen_trunc_i64_i32(cpu_T[1], t0);
                    tcg_gen_sub_tl(cpu_cc_src, cpu_T[1], cpu_tmp0);
                }
#endif
B
bellard 已提交
5444
        } else {
B
bellard 已提交
5445 5446 5447 5448 5449 5450 5451
            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);
B
bellard 已提交
5452
        }
B
bellard 已提交
5453
        gen_op_mov_reg_T0(ot, reg);
5454
        set_cc_op(s, CC_OP_MULB + ot);
B
bellard 已提交
5455 5456 5457 5458 5459 5460
        break;
    case 0x1c0:
    case 0x1c1: /* xadd Ev, Gv */
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
5461
            ot = dflag + OT_WORD;
5462
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5463
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5464 5465
        mod = (modrm >> 6) & 3;
        if (mod == 3) {
B
bellard 已提交
5466
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
5467 5468
            gen_op_mov_TN_reg(ot, 0, reg);
            gen_op_mov_TN_reg(ot, 1, rm);
B
bellard 已提交
5469
            gen_op_addl_T0_T1();
B
bellard 已提交
5470 5471
            gen_op_mov_reg_T1(ot, reg);
            gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
5472
        } else {
5473
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
5474 5475
            gen_op_mov_TN_reg(ot, 0, reg);
            gen_op_ld_T1_A0(ot + s->mem_index);
B
bellard 已提交
5476
            gen_op_addl_T0_T1();
B
bellard 已提交
5477 5478
            gen_op_st_T0_A0(ot + s->mem_index);
            gen_op_mov_reg_T1(ot, reg);
B
bellard 已提交
5479 5480
        }
        gen_op_update2_cc();
5481
        set_cc_op(s, CC_OP_ADDB + ot);
B
bellard 已提交
5482 5483 5484
        break;
    case 0x1b0:
    case 0x1b1: /* cmpxchg Ev, Gv */
B
bellard 已提交
5485
        {
B
bellard 已提交
5486
            int label1, label2;
5487
            TCGv t0, t1, t2, a0;
B
bellard 已提交
5488 5489 5490 5491 5492

            if ((b & 1) == 0)
                ot = OT_BYTE;
            else
                ot = dflag + OT_WORD;
5493
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5494 5495
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
P
pbrook 已提交
5496 5497 5498 5499
            t0 = tcg_temp_local_new();
            t1 = tcg_temp_local_new();
            t2 = tcg_temp_local_new();
            a0 = tcg_temp_local_new();
5500
            gen_op_mov_v_reg(ot, t1, reg);
B
bellard 已提交
5501 5502
            if (mod == 3) {
                rm = (modrm & 7) | REX_B(s);
5503
                gen_op_mov_v_reg(ot, t0, rm);
B
bellard 已提交
5504
            } else {
5505
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
5506 5507
                tcg_gen_mov_tl(a0, cpu_A0);
                gen_op_ld_v(ot + s->mem_index, t0, a0);
B
bellard 已提交
5508 5509 5510
                rm = 0; /* avoid warning */
            }
            label1 = gen_new_label();
5511 5512
            tcg_gen_mov_tl(t2, cpu_regs[R_EAX]);
            gen_extu(ot, t0);
5513
            gen_extu(ot, t2);
5514
            tcg_gen_brcond_tl(TCG_COND_EQ, t2, t0, label1);
5515
            label2 = gen_new_label();
B
bellard 已提交
5516
            if (mod == 3) {
5517
                gen_op_mov_reg_v(ot, R_EAX, t0);
B
bellard 已提交
5518 5519
                tcg_gen_br(label2);
                gen_set_label(label1);
5520
                gen_op_mov_reg_v(ot, rm, t1);
B
bellard 已提交
5521
            } else {
5522 5523 5524 5525
                /* 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 */
                gen_op_st_v(ot + s->mem_index, t0, a0);
5526
                gen_op_mov_reg_v(ot, R_EAX, t0);
5527
                tcg_gen_br(label2);
B
bellard 已提交
5528
                gen_set_label(label1);
5529
                gen_op_st_v(ot + s->mem_index, t1, a0);
B
bellard 已提交
5530
            }
5531
            gen_set_label(label2);
5532
            tcg_gen_mov_tl(cpu_cc_src, t0);
5533 5534
            tcg_gen_mov_tl(cpu_cc_srcT, t2);
            tcg_gen_sub_tl(cpu_cc_dst, t2, t0);
5535
            set_cc_op(s, CC_OP_SUBB + ot);
5536 5537 5538 5539
            tcg_temp_free(t0);
            tcg_temp_free(t1);
            tcg_temp_free(t2);
            tcg_temp_free(a0);
B
bellard 已提交
5540 5541 5542
        }
        break;
    case 0x1c7: /* cmpxchg8b */
5543
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5544
        mod = (modrm >> 6) & 3;
5545
        if ((mod == 3) || ((modrm & 0x38) != 0x8))
B
bellard 已提交
5546
            goto illegal_op;
B
bellard 已提交
5547 5548 5549 5550 5551
#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);
5552
            gen_update_cc_op(s);
5553
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
Blue Swirl 已提交
5554
            gen_helper_cmpxchg16b(cpu_env, cpu_A0);
B
bellard 已提交
5555 5556 5557 5558 5559 5560
        } else
#endif        
        {
            if (!(s->cpuid_features & CPUID_CX8))
                goto illegal_op;
            gen_jmp_im(pc_start - s->cs_base);
5561
            gen_update_cc_op(s);
5562
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
Blue Swirl 已提交
5563
            gen_helper_cmpxchg8b(cpu_env, cpu_A0);
B
bellard 已提交
5564
        }
5565
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
5566
        break;
5567

B
bellard 已提交
5568 5569 5570
        /**************************/
        /* push/pop */
    case 0x50 ... 0x57: /* push */
B
bellard 已提交
5571
        gen_op_mov_TN_reg(OT_LONG, 0, (b & 7) | REX_B(s));
B
bellard 已提交
5572 5573 5574
        gen_push_T0(s);
        break;
    case 0x58 ... 0x5f: /* pop */
B
bellard 已提交
5575 5576 5577 5578 5579
        if (CODE64(s)) {
            ot = dflag ? OT_QUAD : OT_WORD;
        } else {
            ot = dflag + OT_WORD;
        }
B
bellard 已提交
5580
        gen_pop_T0(s);
B
bellard 已提交
5581
        /* NOTE: order is important for pop %sp */
B
bellard 已提交
5582
        gen_pop_update(s);
B
bellard 已提交
5583
        gen_op_mov_reg_T0(ot, (b & 7) | REX_B(s));
B
bellard 已提交
5584 5585
        break;
    case 0x60: /* pusha */
B
bellard 已提交
5586 5587
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5588 5589 5590
        gen_pusha(s);
        break;
    case 0x61: /* popa */
B
bellard 已提交
5591 5592
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5593 5594 5595 5596
        gen_popa(s);
        break;
    case 0x68: /* push Iv */
    case 0x6a:
B
bellard 已提交
5597 5598 5599 5600 5601
        if (CODE64(s)) {
            ot = dflag ? OT_QUAD : OT_WORD;
        } else {
            ot = dflag + OT_WORD;
        }
B
bellard 已提交
5602
        if (b == 0x68)
5603
            val = insn_get(env, s, ot);
B
bellard 已提交
5604
        else
5605
            val = (int8_t)insn_get(env, s, OT_BYTE);
B
bellard 已提交
5606 5607 5608 5609
        gen_op_movl_T0_im(val);
        gen_push_T0(s);
        break;
    case 0x8f: /* pop Ev */
B
bellard 已提交
5610 5611 5612 5613 5614
        if (CODE64(s)) {
            ot = dflag ? OT_QUAD : OT_WORD;
        } else {
            ot = dflag + OT_WORD;
        }
5615
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5616
        mod = (modrm >> 6) & 3;
B
bellard 已提交
5617
        gen_pop_T0(s);
B
bellard 已提交
5618 5619 5620
        if (mod == 3) {
            /* NOTE: order is important for pop %sp */
            gen_pop_update(s);
B
bellard 已提交
5621
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
5622
            gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
5623 5624
        } else {
            /* NOTE: order is important too for MMU exceptions */
B
bellard 已提交
5625
            s->popl_esp_hack = 1 << ot;
5626
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
5627 5628 5629
            s->popl_esp_hack = 0;
            gen_pop_update(s);
        }
B
bellard 已提交
5630 5631 5632 5633
        break;
    case 0xc8: /* enter */
        {
            int level;
5634
            val = cpu_lduw_code(env, s->pc);
B
bellard 已提交
5635
            s->pc += 2;
5636
            level = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5637 5638 5639 5640 5641
            gen_enter(s, val, level);
        }
        break;
    case 0xc9: /* leave */
        /* XXX: exception not precise (ESP is updated before potential exception) */
B
bellard 已提交
5642
        if (CODE64(s)) {
B
bellard 已提交
5643 5644
            gen_op_mov_TN_reg(OT_QUAD, 0, R_EBP);
            gen_op_mov_reg_T0(OT_QUAD, R_ESP);
B
bellard 已提交
5645
        } else if (s->ss32) {
B
bellard 已提交
5646 5647
            gen_op_mov_TN_reg(OT_LONG, 0, R_EBP);
            gen_op_mov_reg_T0(OT_LONG, R_ESP);
B
bellard 已提交
5648
        } else {
B
bellard 已提交
5649 5650
            gen_op_mov_TN_reg(OT_WORD, 0, R_EBP);
            gen_op_mov_reg_T0(OT_WORD, R_ESP);
B
bellard 已提交
5651 5652
        }
        gen_pop_T0(s);
B
bellard 已提交
5653 5654 5655 5656 5657
        if (CODE64(s)) {
            ot = dflag ? OT_QUAD : OT_WORD;
        } else {
            ot = dflag + OT_WORD;
        }
B
bellard 已提交
5658
        gen_op_mov_reg_T0(ot, R_EBP);
B
bellard 已提交
5659 5660 5661 5662 5663 5664
        gen_pop_update(s);
        break;
    case 0x06: /* push es */
    case 0x0e: /* push cs */
    case 0x16: /* push ss */
    case 0x1e: /* push ds */
B
bellard 已提交
5665 5666
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677
        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 已提交
5678 5679
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5680 5681 5682 5683 5684
        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) {
5685 5686 5687 5688
            /* if reg == SS, inhibit interrupts/trace. */
            /* If several instructions disable interrupts, only the
               _first_ does it */
            if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
5689
                gen_helper_set_inhibit_irq(cpu_env);
B
bellard 已提交
5690 5691 5692
            s->tf = 0;
        }
        if (s->is_jmp) {
B
bellard 已提交
5693
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5694 5695 5696 5697 5698 5699 5700 5701 5702
            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 已提交
5703
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714
            gen_eob(s);
        }
        break;

        /**************************/
        /* mov */
    case 0x88:
    case 0x89: /* mov Gv, Ev */
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
5715
            ot = dflag + OT_WORD;
5716
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5717
        reg = ((modrm >> 3) & 7) | rex_r;
5718

B
bellard 已提交
5719
        /* generate a generic store */
5720
        gen_ldst_modrm(env, s, modrm, ot, reg, 1);
B
bellard 已提交
5721 5722 5723 5724 5725 5726
        break;
    case 0xc6:
    case 0xc7: /* mov Ev, Iv */
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
5727
            ot = dflag + OT_WORD;
5728
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5729
        mod = (modrm >> 6) & 3;
B
bellard 已提交
5730 5731
        if (mod != 3) {
            s->rip_offset = insn_const_size(ot);
5732
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
5733
        }
5734
        val = insn_get(env, s, ot);
B
bellard 已提交
5735 5736
        gen_op_movl_T0_im(val);
        if (mod != 3)
B
bellard 已提交
5737
            gen_op_st_T0_A0(ot + s->mem_index);
B
bellard 已提交
5738
        else
B
bellard 已提交
5739
            gen_op_mov_reg_T0(ot, (modrm & 7) | REX_B(s));
B
bellard 已提交
5740 5741 5742 5743 5744 5745
        break;
    case 0x8a:
    case 0x8b: /* mov Ev, Gv */
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
5746
            ot = OT_WORD + dflag;
5747
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5748
        reg = ((modrm >> 3) & 7) | rex_r;
5749

5750
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
bellard 已提交
5751
        gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
5752 5753
        break;
    case 0x8e: /* mov seg, Gv */
5754
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5755 5756 5757
        reg = (modrm >> 3) & 7;
        if (reg >= 6 || reg == R_CS)
            goto illegal_op;
5758
        gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
B
bellard 已提交
5759 5760 5761
        gen_movl_seg_T0(s, reg, pc_start - s->cs_base);
        if (reg == R_SS) {
            /* if reg == SS, inhibit interrupts/trace */
5762 5763 5764
            /* If several instructions disable interrupts, only the
               _first_ does it */
            if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
5765
                gen_helper_set_inhibit_irq(cpu_env);
B
bellard 已提交
5766 5767 5768
            s->tf = 0;
        }
        if (s->is_jmp) {
B
bellard 已提交
5769
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5770 5771 5772 5773
            gen_eob(s);
        }
        break;
    case 0x8c: /* mov Gv, seg */
5774
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5775 5776 5777 5778 5779
        reg = (modrm >> 3) & 7;
        mod = (modrm >> 6) & 3;
        if (reg >= 6)
            goto illegal_op;
        gen_op_movl_T0_seg(reg);
B
bellard 已提交
5780 5781 5782 5783
        if (mod == 3)
            ot = OT_WORD + dflag;
        else
            ot = OT_WORD;
5784
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796
        break;

    case 0x1b6: /* movzbS Gv, Eb */
    case 0x1b7: /* movzwS Gv, Eb */
    case 0x1be: /* movsbS Gv, Eb */
    case 0x1bf: /* movswS Gv, Eb */
        {
            int d_ot;
            /* d_ot is the size of destination */
            d_ot = dflag + OT_WORD;
            /* ot is the size of source */
            ot = (b & 1) + OT_BYTE;
5797
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5798
            reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5799
            mod = (modrm >> 6) & 3;
B
bellard 已提交
5800
            rm = (modrm & 7) | REX_B(s);
5801

B
bellard 已提交
5802
            if (mod == 3) {
B
bellard 已提交
5803
                gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
5804 5805
                switch(ot | (b & 8)) {
                case OT_BYTE:
B
bellard 已提交
5806
                    tcg_gen_ext8u_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5807 5808
                    break;
                case OT_BYTE | 8:
B
bellard 已提交
5809
                    tcg_gen_ext8s_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5810 5811
                    break;
                case OT_WORD:
B
bellard 已提交
5812
                    tcg_gen_ext16u_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5813 5814 5815
                    break;
                default:
                case OT_WORD | 8:
B
bellard 已提交
5816
                    tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5817 5818
                    break;
                }
B
bellard 已提交
5819
                gen_op_mov_reg_T0(d_ot, reg);
B
bellard 已提交
5820
            } else {
5821
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
5822
                if (b & 8) {
B
bellard 已提交
5823
                    gen_op_lds_T0_A0(ot + s->mem_index);
B
bellard 已提交
5824
                } else {
B
bellard 已提交
5825
                    gen_op_ldu_T0_A0(ot + s->mem_index);
B
bellard 已提交
5826
                }
B
bellard 已提交
5827
                gen_op_mov_reg_T0(d_ot, reg);
B
bellard 已提交
5828 5829 5830 5831 5832
            }
        }
        break;

    case 0x8d: /* lea */
B
bellard 已提交
5833
        ot = dflag + OT_WORD;
5834
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5835 5836 5837
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
B
bellard 已提交
5838
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5839 5840 5841 5842
        /* we must ensure that no segment is added */
        s->override = -1;
        val = s->addseg;
        s->addseg = 0;
5843
        gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
5844
        s->addseg = val;
B
bellard 已提交
5845
        gen_op_mov_reg_A0(ot - OT_WORD, reg);
B
bellard 已提交
5846
        break;
5847

B
bellard 已提交
5848 5849 5850 5851 5852
    case 0xa0: /* mov EAX, Ov */
    case 0xa1:
    case 0xa2: /* mov Ov, EAX */
    case 0xa3:
        {
B
bellard 已提交
5853 5854 5855 5856 5857 5858 5859
            target_ulong offset_addr;

            if ((b & 1) == 0)
                ot = OT_BYTE;
            else
                ot = dflag + OT_WORD;
#ifdef TARGET_X86_64
5860
            if (s->aflag == 2) {
5861
                offset_addr = cpu_ldq_code(env, s->pc);
B
bellard 已提交
5862
                s->pc += 8;
B
bellard 已提交
5863
                gen_op_movq_A0_im(offset_addr);
5864
            } else
B
bellard 已提交
5865 5866 5867
#endif
            {
                if (s->aflag) {
5868
                    offset_addr = insn_get(env, s, OT_LONG);
B
bellard 已提交
5869
                } else {
5870
                    offset_addr = insn_get(env, s, OT_WORD);
B
bellard 已提交
5871 5872 5873
                }
                gen_op_movl_A0_im(offset_addr);
            }
B
bellard 已提交
5874
            gen_add_A0_ds_seg(s);
B
bellard 已提交
5875
            if ((b & 2) == 0) {
B
bellard 已提交
5876 5877
                gen_op_ld_T0_A0(ot + s->mem_index);
                gen_op_mov_reg_T0(ot, R_EAX);
B
bellard 已提交
5878
            } else {
B
bellard 已提交
5879 5880
                gen_op_mov_TN_reg(ot, 0, R_EAX);
                gen_op_st_T0_A0(ot + s->mem_index);
B
bellard 已提交
5881 5882 5883 5884
            }
        }
        break;
    case 0xd7: /* xlat */
B
bellard 已提交
5885
#ifdef TARGET_X86_64
5886
        if (s->aflag == 2) {
B
bellard 已提交
5887
            gen_op_movq_A0_reg(R_EBX);
5888 5889 5890
            gen_op_mov_TN_reg(OT_QUAD, 0, R_EAX);
            tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xff);
            tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_T[0]);
5891
        } else
B
bellard 已提交
5892 5893
#endif
        {
B
bellard 已提交
5894
            gen_op_movl_A0_reg(R_EBX);
5895 5896 5897
            gen_op_mov_TN_reg(OT_LONG, 0, R_EAX);
            tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xff);
            tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_T[0]);
B
bellard 已提交
5898 5899
            if (s->aflag == 0)
                gen_op_andl_A0_ffff();
5900 5901
            else
                tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffffffff);
B
bellard 已提交
5902
        }
B
bellard 已提交
5903
        gen_add_A0_ds_seg(s);
B
bellard 已提交
5904 5905
        gen_op_ldu_T0_A0(OT_BYTE + s->mem_index);
        gen_op_mov_reg_T0(OT_BYTE, R_EAX);
B
bellard 已提交
5906 5907
        break;
    case 0xb0 ... 0xb7: /* mov R, Ib */
5908
        val = insn_get(env, s, OT_BYTE);
B
bellard 已提交
5909
        gen_op_movl_T0_im(val);
B
bellard 已提交
5910
        gen_op_mov_reg_T0(OT_BYTE, (b & 7) | REX_B(s));
B
bellard 已提交
5911 5912
        break;
    case 0xb8 ... 0xbf: /* mov R, Iv */
B
bellard 已提交
5913 5914 5915 5916
#ifdef TARGET_X86_64
        if (dflag == 2) {
            uint64_t tmp;
            /* 64 bit case */
5917
            tmp = cpu_ldq_code(env, s->pc);
B
bellard 已提交
5918 5919 5920
            s->pc += 8;
            reg = (b & 7) | REX_B(s);
            gen_movtl_T0_im(tmp);
B
bellard 已提交
5921
            gen_op_mov_reg_T0(OT_QUAD, reg);
5922
        } else
B
bellard 已提交
5923 5924 5925
#endif
        {
            ot = dflag ? OT_LONG : OT_WORD;
5926
            val = insn_get(env, s, ot);
B
bellard 已提交
5927 5928
            reg = (b & 7) | REX_B(s);
            gen_op_movl_T0_im(val);
B
bellard 已提交
5929
            gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
5930
        }
B
bellard 已提交
5931 5932 5933
        break;

    case 0x91 ... 0x97: /* xchg R, EAX */
R
Richard Henderson 已提交
5934
    do_xchg_reg_eax:
B
bellard 已提交
5935 5936
        ot = dflag + OT_WORD;
        reg = (b & 7) | REX_B(s);
B
bellard 已提交
5937 5938 5939 5940 5941 5942 5943
        rm = R_EAX;
        goto do_xchg_reg;
    case 0x86:
    case 0x87: /* xchg Ev, Gv */
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
5944
            ot = dflag + OT_WORD;
5945
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5946
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5947 5948
        mod = (modrm >> 6) & 3;
        if (mod == 3) {
B
bellard 已提交
5949
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
5950
        do_xchg_reg:
B
bellard 已提交
5951 5952 5953 5954
            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 已提交
5955
        } else {
5956
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
5957
            gen_op_mov_TN_reg(ot, 0, reg);
B
bellard 已提交
5958 5959
            /* for xchg, lock is implicit */
            if (!(prefixes & PREFIX_LOCK))
P
pbrook 已提交
5960
                gen_helper_lock();
B
bellard 已提交
5961 5962
            gen_op_ld_T1_A0(ot + s->mem_index);
            gen_op_st_T0_A0(ot + s->mem_index);
B
bellard 已提交
5963
            if (!(prefixes & PREFIX_LOCK))
P
pbrook 已提交
5964
                gen_helper_unlock();
B
bellard 已提交
5965
            gen_op_mov_reg_T1(ot, reg);
B
bellard 已提交
5966 5967 5968
        }
        break;
    case 0xc4: /* les Gv */
5969
        /* In CODE64 this is VEX3; see above.  */
B
bellard 已提交
5970 5971 5972
        op = R_ES;
        goto do_lxx;
    case 0xc5: /* lds Gv */
5973
        /* In CODE64 this is VEX2; see above.  */
B
bellard 已提交
5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985
        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:
        ot = dflag ? OT_LONG : OT_WORD;
5986
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5987
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5988 5989 5990
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
5991
        gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
5992
        gen_op_ld_T1_A0(ot + s->mem_index);
5993
        gen_add_A0_im(s, 1 << (ot - OT_WORD + 1));
B
bellard 已提交
5994
        /* load the segment first to handle exceptions properly */
B
bellard 已提交
5995
        gen_op_ldu_T0_A0(OT_WORD + s->mem_index);
B
bellard 已提交
5996 5997
        gen_movl_seg_T0(s, op, pc_start - s->cs_base);
        /* then put the data */
B
bellard 已提交
5998
        gen_op_mov_reg_T1(ot, reg);
B
bellard 已提交
5999
        if (s->is_jmp) {
B
bellard 已提交
6000
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
6001 6002 6003
            gen_eob(s);
        }
        break;
6004

B
bellard 已提交
6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015
        /************************/
        /* shifts */
    case 0xc0:
    case 0xc1:
        /* shift Ev,Ib */
        shift = 2;
    grp2:
        {
            if ((b & 1) == 0)
                ot = OT_BYTE;
            else
B
bellard 已提交
6016
                ot = dflag + OT_WORD;
6017

6018
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
6019 6020
            mod = (modrm >> 6) & 3;
            op = (modrm >> 3) & 7;
6021

B
bellard 已提交
6022
            if (mod != 3) {
B
bellard 已提交
6023 6024 6025
                if (shift == 2) {
                    s->rip_offset = 1;
                }
6026
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
6027 6028
                opreg = OR_TMP0;
            } else {
B
bellard 已提交
6029
                opreg = (modrm & 7) | REX_B(s);
B
bellard 已提交
6030 6031 6032 6033 6034 6035 6036
            }

            /* simpler op */
            if (shift == 0) {
                gen_shift(s, op, ot, opreg, OR_ECX);
            } else {
                if (shift == 2) {
6037
                    shift = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069
                }
                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:
B
bellard 已提交
6070
        ot = dflag + OT_WORD;
6071
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
6072
        mod = (modrm >> 6) & 3;
B
bellard 已提交
6073 6074
        rm = (modrm & 7) | REX_B(s);
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
6075
        if (mod != 3) {
6076
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
6077
            opreg = OR_TMP0;
B
bellard 已提交
6078
        } else {
6079
            opreg = rm;
B
bellard 已提交
6080
        }
B
bellard 已提交
6081
        gen_op_mov_TN_reg(ot, 1, reg);
6082

B
bellard 已提交
6083
        if (shift) {
P
Paolo Bonzini 已提交
6084 6085 6086
            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 已提交
6087
        } else {
P
Paolo Bonzini 已提交
6088
            gen_shiftd_rm_T1(s, ot, opreg, op, cpu_regs[R_ECX]);
B
bellard 已提交
6089 6090 6091 6092 6093
        }
        break;

        /************************/
        /* floats */
6094
    case 0xd8 ... 0xdf:
B
bellard 已提交
6095 6096 6097 6098 6099 6100
        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;
        }
6101
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
6102 6103 6104 6105 6106
        mod = (modrm >> 6) & 3;
        rm = modrm & 7;
        op = ((b & 7) << 3) | ((modrm >> 3) & 7);
        if (mod != 3) {
            /* memory op */
6107
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118
            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:
B
bellard 已提交
6119
                        gen_op_ld_T0_A0(OT_LONG + s->mem_index);
6120
                        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
6121
                        gen_helper_flds_FT0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
6122 6123
                        break;
                    case 1:
B
bellard 已提交
6124
                        gen_op_ld_T0_A0(OT_LONG + s->mem_index);
6125
                        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
6126
                        gen_helper_fildl_FT0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
6127 6128
                        break;
                    case 2:
6129
                        tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_A0, 
B
bellard 已提交
6130
                                          (s->mem_index >> 2) - 1);
B
Blue Swirl 已提交
6131
                        gen_helper_fldl_FT0(cpu_env, cpu_tmp1_i64);
B
bellard 已提交
6132 6133 6134
                        break;
                    case 3:
                    default:
B
bellard 已提交
6135
                        gen_op_lds_T0_A0(OT_WORD + s->mem_index);
6136
                        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
6137
                        gen_helper_fildl_FT0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
6138 6139
                        break;
                    }
6140

P
pbrook 已提交
6141
                    gen_helper_fp_arith_ST0_FT0(op1);
B
bellard 已提交
6142 6143
                    if (op1 == 3) {
                        /* fcomp needs pop */
B
Blue Swirl 已提交
6144
                        gen_helper_fpop(cpu_env);
B
bellard 已提交
6145 6146 6147 6148 6149 6150
                    }
                }
                break;
            case 0x08: /* flds */
            case 0x0a: /* fsts */
            case 0x0b: /* fstps */
B
bellard 已提交
6151 6152 6153
            case 0x18 ... 0x1b: /* fildl, fisttpl, fistl, fistpl */
            case 0x28 ... 0x2b: /* fldl, fisttpll, fstl, fstpl */
            case 0x38 ... 0x3b: /* filds, fisttps, fists, fistps */
B
bellard 已提交
6154 6155 6156 6157
                switch(op & 7) {
                case 0:
                    switch(op >> 4) {
                    case 0:
B
bellard 已提交
6158
                        gen_op_ld_T0_A0(OT_LONG + s->mem_index);
6159
                        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
6160
                        gen_helper_flds_ST0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
6161 6162
                        break;
                    case 1:
B
bellard 已提交
6163
                        gen_op_ld_T0_A0(OT_LONG + s->mem_index);
6164
                        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
6165
                        gen_helper_fildl_ST0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
6166 6167
                        break;
                    case 2:
6168
                        tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_A0, 
B
bellard 已提交
6169
                                          (s->mem_index >> 2) - 1);
B
Blue Swirl 已提交
6170
                        gen_helper_fldl_ST0(cpu_env, cpu_tmp1_i64);
B
bellard 已提交
6171 6172 6173
                        break;
                    case 3:
                    default:
B
bellard 已提交
6174
                        gen_op_lds_T0_A0(OT_WORD + s->mem_index);
6175
                        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
6176
                        gen_helper_fildl_ST0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
6177 6178 6179
                        break;
                    }
                    break;
B
bellard 已提交
6180
                case 1:
B
bellard 已提交
6181
                    /* XXX: the corresponding CPUID bit must be tested ! */
B
bellard 已提交
6182 6183
                    switch(op >> 4) {
                    case 1:
B
Blue Swirl 已提交
6184
                        gen_helper_fisttl_ST0(cpu_tmp2_i32, cpu_env);
6185
                        tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
6186
                        gen_op_st_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
6187 6188
                        break;
                    case 2:
B
Blue Swirl 已提交
6189
                        gen_helper_fisttll_ST0(cpu_tmp1_i64, cpu_env);
6190
                        tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_A0, 
B
bellard 已提交
6191
                                          (s->mem_index >> 2) - 1);
B
bellard 已提交
6192 6193 6194
                        break;
                    case 3:
                    default:
B
Blue Swirl 已提交
6195
                        gen_helper_fistt_ST0(cpu_tmp2_i32, cpu_env);
6196
                        tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
6197
                        gen_op_st_T0_A0(OT_WORD + s->mem_index);
B
bellard 已提交
6198
                        break;
B
bellard 已提交
6199
                    }
B
Blue Swirl 已提交
6200
                    gen_helper_fpop(cpu_env);
B
bellard 已提交
6201
                    break;
B
bellard 已提交
6202 6203 6204
                default:
                    switch(op >> 4) {
                    case 0:
B
Blue Swirl 已提交
6205
                        gen_helper_fsts_ST0(cpu_tmp2_i32, cpu_env);
6206
                        tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
6207
                        gen_op_st_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
6208 6209
                        break;
                    case 1:
B
Blue Swirl 已提交
6210
                        gen_helper_fistl_ST0(cpu_tmp2_i32, cpu_env);
6211
                        tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
6212
                        gen_op_st_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
6213 6214
                        break;
                    case 2:
B
Blue Swirl 已提交
6215
                        gen_helper_fstl_ST0(cpu_tmp1_i64, cpu_env);
6216
                        tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_A0, 
B
bellard 已提交
6217
                                          (s->mem_index >> 2) - 1);
B
bellard 已提交
6218 6219 6220
                        break;
                    case 3:
                    default:
B
Blue Swirl 已提交
6221
                        gen_helper_fist_ST0(cpu_tmp2_i32, cpu_env);
6222
                        tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
6223
                        gen_op_st_T0_A0(OT_WORD + s->mem_index);
B
bellard 已提交
6224 6225 6226
                        break;
                    }
                    if ((op & 7) == 3)
B
Blue Swirl 已提交
6227
                        gen_helper_fpop(cpu_env);
B
bellard 已提交
6228 6229 6230 6231
                    break;
                }
                break;
            case 0x0c: /* fldenv mem */
6232
                gen_update_cc_op(s);
B
bellard 已提交
6233
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6234
                gen_helper_fldenv(cpu_env, cpu_A0, tcg_const_i32(s->dflag));
B
bellard 已提交
6235 6236
                break;
            case 0x0d: /* fldcw mem */
B
bellard 已提交
6237
                gen_op_ld_T0_A0(OT_WORD + s->mem_index);
6238
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
6239
                gen_helper_fldcw(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
6240 6241
                break;
            case 0x0e: /* fnstenv mem */
6242
                gen_update_cc_op(s);
B
bellard 已提交
6243
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6244
                gen_helper_fstenv(cpu_env, cpu_A0, tcg_const_i32(s->dflag));
B
bellard 已提交
6245 6246
                break;
            case 0x0f: /* fnstcw mem */
B
Blue Swirl 已提交
6247
                gen_helper_fnstcw(cpu_tmp2_i32, cpu_env);
6248
                tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
6249
                gen_op_st_T0_A0(OT_WORD + s->mem_index);
B
bellard 已提交
6250 6251
                break;
            case 0x1d: /* fldt mem */
6252
                gen_update_cc_op(s);
B
bellard 已提交
6253
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6254
                gen_helper_fldt_ST0(cpu_env, cpu_A0);
B
bellard 已提交
6255 6256
                break;
            case 0x1f: /* fstpt mem */
6257
                gen_update_cc_op(s);
B
bellard 已提交
6258
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6259 6260
                gen_helper_fstt_ST0(cpu_env, cpu_A0);
                gen_helper_fpop(cpu_env);
B
bellard 已提交
6261 6262
                break;
            case 0x2c: /* frstor mem */
6263
                gen_update_cc_op(s);
B
bellard 已提交
6264
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6265
                gen_helper_frstor(cpu_env, cpu_A0, tcg_const_i32(s->dflag));
B
bellard 已提交
6266 6267
                break;
            case 0x2e: /* fnsave mem */
6268
                gen_update_cc_op(s);
B
bellard 已提交
6269
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6270
                gen_helper_fsave(cpu_env, cpu_A0, tcg_const_i32(s->dflag));
B
bellard 已提交
6271 6272
                break;
            case 0x2f: /* fnstsw mem */
B
Blue Swirl 已提交
6273
                gen_helper_fnstsw(cpu_tmp2_i32, cpu_env);
6274
                tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
6275
                gen_op_st_T0_A0(OT_WORD + s->mem_index);
B
bellard 已提交
6276 6277
                break;
            case 0x3c: /* fbld */
6278
                gen_update_cc_op(s);
B
bellard 已提交
6279
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6280
                gen_helper_fbld_ST0(cpu_env, cpu_A0);
B
bellard 已提交
6281 6282
                break;
            case 0x3e: /* fbstp */
6283
                gen_update_cc_op(s);
B
bellard 已提交
6284
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6285 6286
                gen_helper_fbst_ST0(cpu_env, cpu_A0);
                gen_helper_fpop(cpu_env);
B
bellard 已提交
6287 6288
                break;
            case 0x3d: /* fildll */
6289
                tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_A0, 
B
bellard 已提交
6290
                                  (s->mem_index >> 2) - 1);
B
Blue Swirl 已提交
6291
                gen_helper_fildll_ST0(cpu_env, cpu_tmp1_i64);
B
bellard 已提交
6292 6293
                break;
            case 0x3f: /* fistpll */
B
Blue Swirl 已提交
6294
                gen_helper_fistll_ST0(cpu_tmp1_i64, cpu_env);
6295
                tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_A0, 
B
bellard 已提交
6296
                                  (s->mem_index >> 2) - 1);
B
Blue Swirl 已提交
6297
                gen_helper_fpop(cpu_env);
B
bellard 已提交
6298 6299 6300 6301 6302 6303 6304 6305 6306 6307
                break;
            default:
                goto illegal_op;
            }
        } else {
            /* register float ops */
            opreg = rm;

            switch(op) {
            case 0x08: /* fld sti */
B
Blue Swirl 已提交
6308 6309 6310
                gen_helper_fpush(cpu_env);
                gen_helper_fmov_ST0_STN(cpu_env,
                                        tcg_const_i32((opreg + 1) & 7));
B
bellard 已提交
6311 6312
                break;
            case 0x09: /* fxchg sti */
B
bellard 已提交
6313 6314
            case 0x29: /* fxchg4 sti, undocumented op */
            case 0x39: /* fxchg7 sti, undocumented op */
B
Blue Swirl 已提交
6315
                gen_helper_fxchg_ST0_STN(cpu_env, tcg_const_i32(opreg));
B
bellard 已提交
6316 6317 6318 6319
                break;
            case 0x0a: /* grp d9/2 */
                switch(rm) {
                case 0: /* fnop */
6320
                    /* check exceptions (FreeBSD FPU probe) */
6321
                    gen_update_cc_op(s);
B
bellard 已提交
6322
                    gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6323
                    gen_helper_fwait(cpu_env);
B
bellard 已提交
6324 6325 6326 6327 6328 6329 6330 6331
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x0c: /* grp d9/4 */
                switch(rm) {
                case 0: /* fchs */
B
Blue Swirl 已提交
6332
                    gen_helper_fchs_ST0(cpu_env);
B
bellard 已提交
6333 6334
                    break;
                case 1: /* fabs */
B
Blue Swirl 已提交
6335
                    gen_helper_fabs_ST0(cpu_env);
B
bellard 已提交
6336 6337
                    break;
                case 4: /* ftst */
B
Blue Swirl 已提交
6338 6339
                    gen_helper_fldz_FT0(cpu_env);
                    gen_helper_fcom_ST0_FT0(cpu_env);
B
bellard 已提交
6340 6341
                    break;
                case 5: /* fxam */
B
Blue Swirl 已提交
6342
                    gen_helper_fxam_ST0(cpu_env);
B
bellard 已提交
6343 6344 6345 6346 6347 6348 6349 6350 6351
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x0d: /* grp d9/5 */
                {
                    switch(rm) {
                    case 0:
B
Blue Swirl 已提交
6352 6353
                        gen_helper_fpush(cpu_env);
                        gen_helper_fld1_ST0(cpu_env);
B
bellard 已提交
6354 6355
                        break;
                    case 1:
B
Blue Swirl 已提交
6356 6357
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldl2t_ST0(cpu_env);
B
bellard 已提交
6358 6359
                        break;
                    case 2:
B
Blue Swirl 已提交
6360 6361
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldl2e_ST0(cpu_env);
B
bellard 已提交
6362 6363
                        break;
                    case 3:
B
Blue Swirl 已提交
6364 6365
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldpi_ST0(cpu_env);
B
bellard 已提交
6366 6367
                        break;
                    case 4:
B
Blue Swirl 已提交
6368 6369
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldlg2_ST0(cpu_env);
B
bellard 已提交
6370 6371
                        break;
                    case 5:
B
Blue Swirl 已提交
6372 6373
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldln2_ST0(cpu_env);
B
bellard 已提交
6374 6375
                        break;
                    case 6:
B
Blue Swirl 已提交
6376 6377
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldz_ST0(cpu_env);
B
bellard 已提交
6378 6379 6380 6381 6382 6383 6384 6385 6386
                        break;
                    default:
                        goto illegal_op;
                    }
                }
                break;
            case 0x0e: /* grp d9/6 */
                switch(rm) {
                case 0: /* f2xm1 */
B
Blue Swirl 已提交
6387
                    gen_helper_f2xm1(cpu_env);
B
bellard 已提交
6388 6389
                    break;
                case 1: /* fyl2x */
B
Blue Swirl 已提交
6390
                    gen_helper_fyl2x(cpu_env);
B
bellard 已提交
6391 6392
                    break;
                case 2: /* fptan */
B
Blue Swirl 已提交
6393
                    gen_helper_fptan(cpu_env);
B
bellard 已提交
6394 6395
                    break;
                case 3: /* fpatan */
B
Blue Swirl 已提交
6396
                    gen_helper_fpatan(cpu_env);
B
bellard 已提交
6397 6398
                    break;
                case 4: /* fxtract */
B
Blue Swirl 已提交
6399
                    gen_helper_fxtract(cpu_env);
B
bellard 已提交
6400 6401
                    break;
                case 5: /* fprem1 */
B
Blue Swirl 已提交
6402
                    gen_helper_fprem1(cpu_env);
B
bellard 已提交
6403 6404
                    break;
                case 6: /* fdecstp */
B
Blue Swirl 已提交
6405
                    gen_helper_fdecstp(cpu_env);
B
bellard 已提交
6406 6407 6408
                    break;
                default:
                case 7: /* fincstp */
B
Blue Swirl 已提交
6409
                    gen_helper_fincstp(cpu_env);
B
bellard 已提交
6410 6411 6412 6413 6414 6415
                    break;
                }
                break;
            case 0x0f: /* grp d9/7 */
                switch(rm) {
                case 0: /* fprem */
B
Blue Swirl 已提交
6416
                    gen_helper_fprem(cpu_env);
B
bellard 已提交
6417 6418
                    break;
                case 1: /* fyl2xp1 */
B
Blue Swirl 已提交
6419
                    gen_helper_fyl2xp1(cpu_env);
B
bellard 已提交
6420 6421
                    break;
                case 2: /* fsqrt */
B
Blue Swirl 已提交
6422
                    gen_helper_fsqrt(cpu_env);
B
bellard 已提交
6423 6424
                    break;
                case 3: /* fsincos */
B
Blue Swirl 已提交
6425
                    gen_helper_fsincos(cpu_env);
B
bellard 已提交
6426 6427
                    break;
                case 5: /* fscale */
B
Blue Swirl 已提交
6428
                    gen_helper_fscale(cpu_env);
B
bellard 已提交
6429 6430
                    break;
                case 4: /* frndint */
B
Blue Swirl 已提交
6431
                    gen_helper_frndint(cpu_env);
B
bellard 已提交
6432 6433
                    break;
                case 6: /* fsin */
B
Blue Swirl 已提交
6434
                    gen_helper_fsin(cpu_env);
B
bellard 已提交
6435 6436 6437
                    break;
                default:
                case 7: /* fcos */
B
Blue Swirl 已提交
6438
                    gen_helper_fcos(cpu_env);
B
bellard 已提交
6439 6440 6441 6442 6443 6444 6445 6446
                    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;
6447

B
bellard 已提交
6448 6449
                    op1 = op & 7;
                    if (op >= 0x20) {
P
pbrook 已提交
6450
                        gen_helper_fp_arith_STN_ST0(op1, opreg);
B
bellard 已提交
6451
                        if (op >= 0x30)
B
Blue Swirl 已提交
6452
                            gen_helper_fpop(cpu_env);
B
bellard 已提交
6453
                    } else {
B
Blue Swirl 已提交
6454
                        gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
P
pbrook 已提交
6455
                        gen_helper_fp_arith_ST0_FT0(op1);
B
bellard 已提交
6456 6457 6458 6459
                    }
                }
                break;
            case 0x02: /* fcom */
B
bellard 已提交
6460
            case 0x22: /* fcom2, undocumented op */
B
Blue Swirl 已提交
6461 6462
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fcom_ST0_FT0(cpu_env);
B
bellard 已提交
6463 6464
                break;
            case 0x03: /* fcomp */
B
bellard 已提交
6465 6466
            case 0x23: /* fcomp3, undocumented op */
            case 0x32: /* fcomp5, undocumented op */
B
Blue Swirl 已提交
6467 6468 6469
                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 已提交
6470 6471 6472 6473
                break;
            case 0x15: /* da/5 */
                switch(rm) {
                case 1: /* fucompp */
B
Blue Swirl 已提交
6474 6475 6476 6477
                    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 已提交
6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489
                    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 已提交
6490
                    gen_helper_fclex(cpu_env);
B
bellard 已提交
6491 6492
                    break;
                case 3: /* fninit */
B
Blue Swirl 已提交
6493
                    gen_helper_fninit(cpu_env);
B
bellard 已提交
6494 6495 6496 6497 6498 6499 6500 6501
                    break;
                case 4: /* fsetpm (287 only, just do nop here) */
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x1d: /* fucomi */
6502
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6503 6504
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fucomi_ST0_FT0(cpu_env);
6505
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6506 6507
                break;
            case 0x1e: /* fcomi */
6508
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6509 6510
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fcomi_ST0_FT0(cpu_env);
6511
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6512
                break;
B
bellard 已提交
6513
            case 0x28: /* ffree sti */
B
Blue Swirl 已提交
6514
                gen_helper_ffree_STN(cpu_env, tcg_const_i32(opreg));
6515
                break;
B
bellard 已提交
6516
            case 0x2a: /* fst sti */
B
Blue Swirl 已提交
6517
                gen_helper_fmov_STN_ST0(cpu_env, tcg_const_i32(opreg));
B
bellard 已提交
6518 6519
                break;
            case 0x2b: /* fstp sti */
B
bellard 已提交
6520 6521 6522
            case 0x0b: /* fstp1 sti, undocumented op */
            case 0x3a: /* fstp8 sti, undocumented op */
            case 0x3b: /* fstp9 sti, undocumented op */
B
Blue Swirl 已提交
6523 6524
                gen_helper_fmov_STN_ST0(cpu_env, tcg_const_i32(opreg));
                gen_helper_fpop(cpu_env);
B
bellard 已提交
6525 6526
                break;
            case 0x2c: /* fucom st(i) */
B
Blue Swirl 已提交
6527 6528
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fucom_ST0_FT0(cpu_env);
B
bellard 已提交
6529 6530
                break;
            case 0x2d: /* fucomp st(i) */
B
Blue Swirl 已提交
6531 6532 6533
                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 已提交
6534 6535 6536 6537
                break;
            case 0x33: /* de/3 */
                switch(rm) {
                case 1: /* fcompp */
B
Blue Swirl 已提交
6538 6539 6540 6541
                    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 已提交
6542 6543 6544 6545 6546
                    break;
                default:
                    goto illegal_op;
                }
                break;
B
bellard 已提交
6547
            case 0x38: /* ffreep sti, undocumented op */
B
Blue Swirl 已提交
6548 6549
                gen_helper_ffree_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fpop(cpu_env);
B
bellard 已提交
6550
                break;
B
bellard 已提交
6551 6552 6553
            case 0x3c: /* df/4 */
                switch(rm) {
                case 0:
B
Blue Swirl 已提交
6554
                    gen_helper_fnstsw(cpu_tmp2_i32, cpu_env);
6555
                    tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
6556
                    gen_op_mov_reg_T0(OT_WORD, R_EAX);
B
bellard 已提交
6557 6558 6559 6560 6561 6562
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x3d: /* fucomip */
6563
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6564 6565 6566
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fucomi_ST0_FT0(cpu_env);
                gen_helper_fpop(cpu_env);
6567
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6568 6569
                break;
            case 0x3e: /* fcomip */
6570
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6571 6572 6573
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fcomi_ST0_FT0(cpu_env);
                gen_helper_fpop(cpu_env);
6574
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6575
                break;
6576 6577 6578
            case 0x10 ... 0x13: /* fcmovxx */
            case 0x18 ... 0x1b:
                {
B
bellard 已提交
6579
                    int op1, l1;
6580
                    static const uint8_t fcmov_cc[8] = {
6581 6582 6583 6584 6585
                        (JCC_B << 1),
                        (JCC_Z << 1),
                        (JCC_BE << 1),
                        (JCC_P << 1),
                    };
6586
                    op1 = fcmov_cc[op & 3] | (((op >> 3) & 1) ^ 1);
B
bellard 已提交
6587
                    l1 = gen_new_label();
6588
                    gen_jcc1_noeob(s, op1, l1);
B
Blue Swirl 已提交
6589
                    gen_helper_fmov_ST0_STN(cpu_env, tcg_const_i32(opreg));
B
bellard 已提交
6590
                    gen_set_label(l1);
6591 6592
                }
                break;
B
bellard 已提交
6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605
            default:
                goto illegal_op;
            }
        }
        break;
        /************************/
        /* string ops */

    case 0xa4: /* movsS */
    case 0xa5:
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
6606
            ot = dflag + OT_WORD;
B
bellard 已提交
6607 6608 6609 6610 6611 6612 6613

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

B
bellard 已提交
6615 6616 6617 6618 6619
    case 0xaa: /* stosS */
    case 0xab:
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
6620
            ot = dflag + OT_WORD;
B
bellard 已提交
6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632

        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)
            ot = OT_BYTE;
        else
B
bellard 已提交
6633
            ot = dflag + OT_WORD;
B
bellard 已提交
6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644
        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)
            ot = OT_BYTE;
        else
B
bellard 已提交
6645
            ot = dflag + OT_WORD;
B
bellard 已提交
6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659
        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)
            ot = OT_BYTE;
        else
B
bellard 已提交
6660
            ot = dflag + OT_WORD;
B
bellard 已提交
6661 6662 6663 6664 6665 6666 6667 6668 6669 6670
        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:
6671 6672 6673 6674
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
B
bellard 已提交
6675
        gen_op_mov_TN_reg(OT_WORD, 0, R_EDX);
T
ths 已提交
6676
        gen_op_andl_T0_ffff();
6677 6678
        gen_check_io(s, ot, pc_start - s->cs_base, 
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes) | 4);
6679 6680
        if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
            gen_repz_ins(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
B
bellard 已提交
6681
        } else {
6682
            gen_ins(s, ot);
P
pbrook 已提交
6683 6684 6685
            if (use_icount) {
                gen_jmp(s, s->pc - s->cs_base);
            }
B
bellard 已提交
6686 6687 6688 6689
        }
        break;
    case 0x6e: /* outsS */
    case 0x6f:
6690 6691 6692 6693
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
B
bellard 已提交
6694
        gen_op_mov_TN_reg(OT_WORD, 0, R_EDX);
T
ths 已提交
6695
        gen_op_andl_T0_ffff();
6696 6697
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes) | 4);
6698 6699
        if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
            gen_repz_outs(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
B
bellard 已提交
6700
        } else {
6701
            gen_outs(s, ot);
P
pbrook 已提交
6702 6703 6704
            if (use_icount) {
                gen_jmp(s, s->pc - s->cs_base);
            }
B
bellard 已提交
6705 6706 6707 6708 6709
        }
        break;

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

B
bellard 已提交
6711 6712
    case 0xe4:
    case 0xe5:
6713 6714 6715 6716
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
6717
        val = cpu_ldub_code(env, s->pc++);
6718
        gen_op_movl_T0_im(val);
6719 6720
        gen_check_io(s, ot, pc_start - s->cs_base,
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
P
pbrook 已提交
6721 6722
        if (use_icount)
            gen_io_start();
6723
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
6724
        gen_helper_in_func(ot, cpu_T[1], cpu_tmp2_i32);
B
bellard 已提交
6725
        gen_op_mov_reg_T1(ot, R_EAX);
P
pbrook 已提交
6726 6727 6728 6729
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6730 6731 6732
        break;
    case 0xe6:
    case 0xe7:
6733 6734 6735 6736
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
6737
        val = cpu_ldub_code(env, s->pc++);
6738
        gen_op_movl_T0_im(val);
6739 6740
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes));
B
bellard 已提交
6741
        gen_op_mov_TN_reg(ot, 1, R_EAX);
6742

P
pbrook 已提交
6743 6744
        if (use_icount)
            gen_io_start();
6745 6746
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
        tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
P
pbrook 已提交
6747
        gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
P
pbrook 已提交
6748 6749 6750 6751
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6752 6753 6754
        break;
    case 0xec:
    case 0xed:
6755 6756 6757 6758
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
B
bellard 已提交
6759
        gen_op_mov_TN_reg(OT_WORD, 0, R_EDX);
6760
        gen_op_andl_T0_ffff();
6761 6762
        gen_check_io(s, ot, pc_start - s->cs_base,
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
P
pbrook 已提交
6763 6764
        if (use_icount)
            gen_io_start();
6765
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
6766
        gen_helper_in_func(ot, cpu_T[1], cpu_tmp2_i32);
B
bellard 已提交
6767
        gen_op_mov_reg_T1(ot, R_EAX);
P
pbrook 已提交
6768 6769 6770 6771
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6772 6773 6774
        break;
    case 0xee:
    case 0xef:
6775 6776 6777 6778
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
B
bellard 已提交
6779
        gen_op_mov_TN_reg(OT_WORD, 0, R_EDX);
6780
        gen_op_andl_T0_ffff();
6781 6782
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes));
B
bellard 已提交
6783
        gen_op_mov_TN_reg(ot, 1, R_EAX);
6784

P
pbrook 已提交
6785 6786
        if (use_icount)
            gen_io_start();
6787 6788
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
        tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
P
pbrook 已提交
6789
        gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
P
pbrook 已提交
6790 6791 6792 6793
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6794 6795 6796 6797 6798
        break;

        /************************/
        /* control */
    case 0xc2: /* ret im */
6799
        val = cpu_ldsw_code(env, s->pc);
B
bellard 已提交
6800 6801
        s->pc += 2;
        gen_pop_T0(s);
6802 6803
        if (CODE64(s) && s->dflag)
            s->dflag = 2;
B
bellard 已提交
6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818
        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 */
6819
        val = cpu_ldsw_code(env, s->pc);
B
bellard 已提交
6820 6821 6822
        s->pc += 2;
    do_lret:
        if (s->pe && !s->vm86) {
6823
            gen_update_cc_op(s);
B
bellard 已提交
6824
            gen_jmp_im(pc_start - s->cs_base);
6825
            gen_helper_lret_protected(cpu_env, tcg_const_i32(s->dflag),
P
pbrook 已提交
6826
                                      tcg_const_i32(val));
B
bellard 已提交
6827 6828 6829
        } else {
            gen_stack_A0(s);
            /* pop offset */
B
bellard 已提交
6830
            gen_op_ld_T0_A0(1 + s->dflag + s->mem_index);
B
bellard 已提交
6831 6832 6833 6834 6835 6836 6837
            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);
B
bellard 已提交
6838
            gen_op_ld_T0_A0(1 + s->dflag + s->mem_index);
6839
            gen_op_movl_seg_T0_vm(R_CS);
B
bellard 已提交
6840 6841 6842 6843 6844 6845 6846 6847 6848
            /* 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 已提交
6849
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_IRET);
B
bellard 已提交
6850 6851
        if (!s->pe) {
            /* real mode */
6852
            gen_helper_iret_real(cpu_env, tcg_const_i32(s->dflag));
6853
            set_cc_op(s, CC_OP_EFLAGS);
6854 6855 6856 6857
        } else if (s->vm86) {
            if (s->iopl != 3) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
6858
                gen_helper_iret_real(cpu_env, tcg_const_i32(s->dflag));
6859
                set_cc_op(s, CC_OP_EFLAGS);
6860
            }
B
bellard 已提交
6861
        } else {
6862
            gen_update_cc_op(s);
B
bellard 已提交
6863
            gen_jmp_im(pc_start - s->cs_base);
6864
            gen_helper_iret_protected(cpu_env, tcg_const_i32(s->dflag),
P
pbrook 已提交
6865
                                      tcg_const_i32(s->pc - s->cs_base));
6866
            set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6867 6868 6869 6870 6871
        }
        gen_eob(s);
        break;
    case 0xe8: /* call im */
        {
B
bellard 已提交
6872
            if (dflag)
6873
                tval = (int32_t)insn_get(env, s, OT_LONG);
B
bellard 已提交
6874
            else
6875
                tval = (int16_t)insn_get(env, s, OT_WORD);
B
bellard 已提交
6876
            next_eip = s->pc - s->cs_base;
B
bellard 已提交
6877
            tval += next_eip;
B
bellard 已提交
6878
            if (s->dflag == 0)
B
bellard 已提交
6879
                tval &= 0xffff;
6880 6881
            else if(!CODE64(s))
                tval &= 0xffffffff;
B
bellard 已提交
6882
            gen_movtl_T0_im(next_eip);
B
bellard 已提交
6883
            gen_push_T0(s);
B
bellard 已提交
6884
            gen_jmp(s, tval);
B
bellard 已提交
6885 6886 6887 6888 6889
        }
        break;
    case 0x9a: /* lcall im */
        {
            unsigned int selector, offset;
6890

B
bellard 已提交
6891 6892
            if (CODE64(s))
                goto illegal_op;
B
bellard 已提交
6893
            ot = dflag ? OT_LONG : OT_WORD;
6894 6895
            offset = insn_get(env, s, ot);
            selector = insn_get(env, s, OT_WORD);
6896

B
bellard 已提交
6897
            gen_op_movl_T0_im(selector);
B
bellard 已提交
6898
            gen_op_movl_T1_imu(offset);
B
bellard 已提交
6899 6900
        }
        goto do_lcall;
B
bellard 已提交
6901
    case 0xe9: /* jmp im */
B
bellard 已提交
6902
        if (dflag)
6903
            tval = (int32_t)insn_get(env, s, OT_LONG);
B
bellard 已提交
6904
        else
6905
            tval = (int16_t)insn_get(env, s, OT_WORD);
B
bellard 已提交
6906
        tval += s->pc - s->cs_base;
B
bellard 已提交
6907
        if (s->dflag == 0)
B
bellard 已提交
6908
            tval &= 0xffff;
6909 6910
        else if(!CODE64(s))
            tval &= 0xffffffff;
B
bellard 已提交
6911
        gen_jmp(s, tval);
B
bellard 已提交
6912 6913 6914 6915 6916
        break;
    case 0xea: /* ljmp im */
        {
            unsigned int selector, offset;

B
bellard 已提交
6917 6918
            if (CODE64(s))
                goto illegal_op;
B
bellard 已提交
6919
            ot = dflag ? OT_LONG : OT_WORD;
6920 6921
            offset = insn_get(env, s, ot);
            selector = insn_get(env, s, OT_WORD);
6922

B
bellard 已提交
6923
            gen_op_movl_T0_im(selector);
B
bellard 已提交
6924
            gen_op_movl_T1_imu(offset);
B
bellard 已提交
6925 6926 6927
        }
        goto do_ljmp;
    case 0xeb: /* jmp Jb */
6928
        tval = (int8_t)insn_get(env, s, OT_BYTE);
B
bellard 已提交
6929
        tval += s->pc - s->cs_base;
B
bellard 已提交
6930
        if (s->dflag == 0)
B
bellard 已提交
6931 6932
            tval &= 0xffff;
        gen_jmp(s, tval);
B
bellard 已提交
6933 6934
        break;
    case 0x70 ... 0x7f: /* jcc Jb */
6935
        tval = (int8_t)insn_get(env, s, OT_BYTE);
B
bellard 已提交
6936 6937 6938
        goto do_jcc;
    case 0x180 ... 0x18f: /* jcc Jv */
        if (dflag) {
6939
            tval = (int32_t)insn_get(env, s, OT_LONG);
B
bellard 已提交
6940
        } else {
6941
            tval = (int16_t)insn_get(env, s, OT_WORD);
B
bellard 已提交
6942 6943 6944
        }
    do_jcc:
        next_eip = s->pc - s->cs_base;
B
bellard 已提交
6945
        tval += next_eip;
B
bellard 已提交
6946
        if (s->dflag == 0)
B
bellard 已提交
6947 6948
            tval &= 0xffff;
        gen_jcc(s, b, tval, next_eip);
B
bellard 已提交
6949 6950 6951
        break;

    case 0x190 ... 0x19f: /* setcc Gv */
6952
        modrm = cpu_ldub_code(env, s->pc++);
6953
        gen_setcc1(s, b, cpu_T[0]);
6954
        gen_ldst_modrm(env, s, modrm, OT_BYTE, OR_TMP0, 1);
B
bellard 已提交
6955 6956
        break;
    case 0x140 ... 0x14f: /* cmov Gv, Ev */
6957 6958 6959 6960
        ot = dflag + OT_WORD;
        modrm = cpu_ldub_code(env, s->pc++);
        reg = ((modrm >> 3) & 7) | rex_r;
        gen_cmovcc1(env, s, ot, b, modrm, reg);
B
bellard 已提交
6961
        break;
6962

B
bellard 已提交
6963 6964 6965
        /************************/
        /* flags */
    case 0x9c: /* pushf */
B
bellard 已提交
6966
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_PUSHF);
B
bellard 已提交
6967 6968 6969
        if (s->vm86 && s->iopl != 3) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
6970
            gen_update_cc_op(s);
6971
            gen_helper_read_eflags(cpu_T[0], cpu_env);
B
bellard 已提交
6972 6973 6974 6975
            gen_push_T0(s);
        }
        break;
    case 0x9d: /* popf */
B
bellard 已提交
6976
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_POPF);
B
bellard 已提交
6977 6978 6979 6980 6981 6982
        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) {
6983 6984 6985 6986 6987
                    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 已提交
6988
                } else {
6989 6990 6991 6992 6993
                    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 已提交
6994 6995
                }
            } else {
B
bellard 已提交
6996 6997
                if (s->cpl <= s->iopl) {
                    if (s->dflag) {
6998 6999 7000 7001 7002 7003
                        gen_helper_write_eflags(cpu_env, cpu_T[0],
                                                tcg_const_i32((TF_MASK |
                                                               AC_MASK |
                                                               ID_MASK |
                                                               NT_MASK |
                                                               IF_MASK)));
B
bellard 已提交
7004
                    } else {
7005 7006 7007 7008 7009 7010 7011
                        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 已提交
7012
                    }
B
bellard 已提交
7013
                } else {
B
bellard 已提交
7014
                    if (s->dflag) {
7015 7016 7017
                        gen_helper_write_eflags(cpu_env, cpu_T[0],
                                           tcg_const_i32((TF_MASK | AC_MASK |
                                                          ID_MASK | NT_MASK)));
B
bellard 已提交
7018
                    } else {
7019 7020 7021 7022
                        gen_helper_write_eflags(cpu_env, cpu_T[0],
                                           tcg_const_i32((TF_MASK | AC_MASK |
                                                          ID_MASK | NT_MASK)
                                                         & 0xffff));
B
bellard 已提交
7023
                    }
B
bellard 已提交
7024 7025 7026
                }
            }
            gen_pop_update(s);
7027
            set_cc_op(s, CC_OP_EFLAGS);
H
H. Peter Anvin 已提交
7028
            /* abort translation because TF/AC flag may change */
B
bellard 已提交
7029
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
7030 7031 7032 7033
            gen_eob(s);
        }
        break;
    case 0x9e: /* sahf */
B
bellard 已提交
7034
        if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM))
B
bellard 已提交
7035
            goto illegal_op;
B
bellard 已提交
7036
        gen_op_mov_TN_reg(OT_BYTE, 0, R_AH);
7037
        gen_compute_eflags(s);
7038 7039 7040
        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 已提交
7041 7042
        break;
    case 0x9f: /* lahf */
B
bellard 已提交
7043
        if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM))
B
bellard 已提交
7044
            goto illegal_op;
7045
        gen_compute_eflags(s);
7046
        /* Note: gen_compute_eflags() only gives the condition codes */
7047
        tcg_gen_ori_tl(cpu_T[0], cpu_cc_src, 0x02);
B
bellard 已提交
7048
        gen_op_mov_reg_T0(OT_BYTE, R_AH);
B
bellard 已提交
7049 7050
        break;
    case 0xf5: /* cmc */
7051
        gen_compute_eflags(s);
7052
        tcg_gen_xori_tl(cpu_cc_src, cpu_cc_src, CC_C);
B
bellard 已提交
7053 7054
        break;
    case 0xf8: /* clc */
7055
        gen_compute_eflags(s);
7056
        tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~CC_C);
B
bellard 已提交
7057 7058
        break;
    case 0xf9: /* stc */
7059
        gen_compute_eflags(s);
7060
        tcg_gen_ori_tl(cpu_cc_src, cpu_cc_src, CC_C);
B
bellard 已提交
7061 7062
        break;
    case 0xfc: /* cld */
7063
        tcg_gen_movi_i32(cpu_tmp2_i32, 1);
7064
        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, offsetof(CPUX86State, df));
B
bellard 已提交
7065 7066
        break;
    case 0xfd: /* std */
7067
        tcg_gen_movi_i32(cpu_tmp2_i32, -1);
7068
        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, offsetof(CPUX86State, df));
B
bellard 已提交
7069 7070 7071 7072 7073
        break;

        /************************/
        /* bit operations */
    case 0x1ba: /* bt/bts/btr/btc Gv, im */
B
bellard 已提交
7074
        ot = dflag + OT_WORD;
7075
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7076
        op = (modrm >> 3) & 7;
B
bellard 已提交
7077
        mod = (modrm >> 6) & 3;
B
bellard 已提交
7078
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
7079
        if (mod != 3) {
B
bellard 已提交
7080
            s->rip_offset = 1;
7081
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
7082
            gen_op_ld_T0_A0(ot + s->mem_index);
B
bellard 已提交
7083
        } else {
B
bellard 已提交
7084
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
7085 7086
        }
        /* load shift */
7087
        val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7088 7089 7090 7091
        gen_op_movl_T1_im(val);
        if (op < 4)
            goto illegal_op;
        op -= 4;
B
bellard 已提交
7092
        goto bt_op;
B
bellard 已提交
7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104
    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:
B
bellard 已提交
7105
        ot = dflag + OT_WORD;
7106
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7107
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
7108
        mod = (modrm >> 6) & 3;
B
bellard 已提交
7109
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
7110
        gen_op_mov_TN_reg(OT_LONG, 1, reg);
B
bellard 已提交
7111
        if (mod != 3) {
7112
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
7113
            /* specific case: we need to add a displacement */
B
bellard 已提交
7114 7115 7116 7117
            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);
B
bellard 已提交
7118
            gen_op_ld_T0_A0(ot + s->mem_index);
B
bellard 已提交
7119
        } else {
B
bellard 已提交
7120
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
7121
        }
B
bellard 已提交
7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149
    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;
        }
7150
        set_cc_op(s, CC_OP_SARB + ot);
B
bellard 已提交
7151 7152
        if (op != 0) {
            if (mod != 3)
B
bellard 已提交
7153
                gen_op_st_T0_A0(ot + s->mem_index);
B
bellard 已提交
7154
            else
B
bellard 已提交
7155
                gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
7156 7157
            tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
            tcg_gen_movi_tl(cpu_cc_dst, 0);
B
bellard 已提交
7158 7159
        }
        break;
7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179
    case 0x1bc: /* bsf / tzcnt */
    case 0x1bd: /* bsr / lzcnt */
        ot = dflag + OT_WORD;
        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 已提交
7180
            } else {
7181 7182 7183 7184 7185
                /* 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 已提交
7186
            }
7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209
            /* 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 已提交
7210
        }
7211
        gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
7212 7213 7214 7215
        break;
        /************************/
        /* bcd */
    case 0x27: /* daa */
B
bellard 已提交
7216 7217
        if (CODE64(s))
            goto illegal_op;
7218
        gen_update_cc_op(s);
7219
        gen_helper_daa(cpu_env);
7220
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
7221 7222
        break;
    case 0x2f: /* das */
B
bellard 已提交
7223 7224
        if (CODE64(s))
            goto illegal_op;
7225
        gen_update_cc_op(s);
7226
        gen_helper_das(cpu_env);
7227
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
7228 7229
        break;
    case 0x37: /* aaa */
B
bellard 已提交
7230 7231
        if (CODE64(s))
            goto illegal_op;
7232
        gen_update_cc_op(s);
7233
        gen_helper_aaa(cpu_env);
7234
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
7235 7236
        break;
    case 0x3f: /* aas */
B
bellard 已提交
7237 7238
        if (CODE64(s))
            goto illegal_op;
7239
        gen_update_cc_op(s);
7240
        gen_helper_aas(cpu_env);
7241
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
7242 7243
        break;
    case 0xd4: /* aam */
B
bellard 已提交
7244 7245
        if (CODE64(s))
            goto illegal_op;
7246
        val = cpu_ldub_code(env, s->pc++);
7247 7248 7249
        if (val == 0) {
            gen_exception(s, EXCP00_DIVZ, pc_start - s->cs_base);
        } else {
7250
            gen_helper_aam(cpu_env, tcg_const_i32(val));
7251
            set_cc_op(s, CC_OP_LOGICB);
7252
        }
B
bellard 已提交
7253 7254
        break;
    case 0xd5: /* aad */
B
bellard 已提交
7255 7256
        if (CODE64(s))
            goto illegal_op;
7257
        val = cpu_ldub_code(env, s->pc++);
7258
        gen_helper_aad(cpu_env, tcg_const_i32(val));
7259
        set_cc_op(s, CC_OP_LOGICB);
B
bellard 已提交
7260 7261 7262 7263
        break;
        /************************/
        /* misc */
    case 0x90: /* nop */
7264
        /* XXX: correct lock test for all insn */
R
Richard Henderson 已提交
7265
        if (prefixes & PREFIX_LOCK) {
7266
            goto illegal_op;
R
Richard Henderson 已提交
7267 7268 7269 7270 7271
        }
        /* 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 已提交
7272 7273 7274
        if (prefixes & PREFIX_REPZ) {
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_PAUSE);
        }
B
bellard 已提交
7275 7276
        break;
    case 0x9b: /* fwait */
7277
        if ((s->flags & (HF_MP_MASK | HF_TS_MASK)) ==
B
bellard 已提交
7278 7279
            (HF_MP_MASK | HF_TS_MASK)) {
            gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
B
bellard 已提交
7280
        } else {
7281
            gen_update_cc_op(s);
B
bellard 已提交
7282
            gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7283
            gen_helper_fwait(cpu_env);
B
bellard 已提交
7284
        }
B
bellard 已提交
7285 7286 7287 7288 7289
        break;
    case 0xcc: /* int3 */
        gen_interrupt(s, EXCP03_INT3, pc_start - s->cs_base, s->pc - s->cs_base);
        break;
    case 0xcd: /* int N */
7290
        val = cpu_ldub_code(env, s->pc++);
7291
        if (s->vm86 && s->iopl != 3) {
7292
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7293 7294 7295
        } else {
            gen_interrupt(s, val, pc_start - s->cs_base, s->pc - s->cs_base);
        }
B
bellard 已提交
7296 7297
        break;
    case 0xce: /* into */
B
bellard 已提交
7298 7299
        if (CODE64(s))
            goto illegal_op;
7300
        gen_update_cc_op(s);
7301
        gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7302
        gen_helper_into(cpu_env, tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
7303
        break;
A
aurel32 已提交
7304
#ifdef WANT_ICEBP
B
bellard 已提交
7305
    case 0xf1: /* icebp (undocumented, exits to external debugger) */
B
bellard 已提交
7306
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_ICEBP);
7307
#if 1
B
bellard 已提交
7308
        gen_debug(s, pc_start - s->cs_base);
7309 7310
#else
        /* start debug */
7311
        tb_flush(env);
7312
        qemu_set_log(CPU_LOG_INT | CPU_LOG_TB_IN_ASM);
7313
#endif
B
bellard 已提交
7314
        break;
A
aurel32 已提交
7315
#endif
B
bellard 已提交
7316 7317 7318
    case 0xfa: /* cli */
        if (!s->vm86) {
            if (s->cpl <= s->iopl) {
7319
                gen_helper_cli(cpu_env);
B
bellard 已提交
7320 7321 7322 7323 7324
            } else {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            }
        } else {
            if (s->iopl == 3) {
7325
                gen_helper_cli(cpu_env);
B
bellard 已提交
7326 7327 7328 7329 7330 7331 7332 7333 7334
            } 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:
7335
                gen_helper_sti(cpu_env);
B
bellard 已提交
7336
                /* interruptions are enabled only the first insn after sti */
7337 7338 7339
                /* If several instructions disable interrupts, only the
                   _first_ does it */
                if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
7340
                    gen_helper_set_inhibit_irq(cpu_env);
B
bellard 已提交
7341
                /* give a chance to handle pending irqs */
B
bellard 已提交
7342
                gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355
                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 已提交
7356 7357
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
7358
        ot = dflag ? OT_LONG : OT_WORD;
7359
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7360 7361 7362 7363
        reg = (modrm >> 3) & 7;
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
B
bellard 已提交
7364
        gen_op_mov_TN_reg(ot, 0, reg);
7365
        gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
7366
        gen_jmp_im(pc_start - s->cs_base);
7367
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
7368 7369 7370 7371 7372
        if (ot == OT_WORD) {
            gen_helper_boundw(cpu_env, cpu_A0, cpu_tmp2_i32);
        } else {
            gen_helper_boundl(cpu_env, cpu_A0, cpu_tmp2_i32);
        }
B
bellard 已提交
7373 7374
        break;
    case 0x1c8 ... 0x1cf: /* bswap reg */
B
bellard 已提交
7375 7376 7377
        reg = (b & 7) | REX_B(s);
#ifdef TARGET_X86_64
        if (dflag == 2) {
B
bellard 已提交
7378
            gen_op_mov_TN_reg(OT_QUAD, 0, reg);
A
aurel32 已提交
7379
            tcg_gen_bswap64_i64(cpu_T[0], cpu_T[0]);
B
bellard 已提交
7380
            gen_op_mov_reg_T0(OT_QUAD, reg);
7381
        } else
7382
#endif
B
bellard 已提交
7383 7384
        {
            gen_op_mov_TN_reg(OT_LONG, 0, reg);
7385 7386
            tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
            tcg_gen_bswap32_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
7387
            gen_op_mov_reg_T0(OT_LONG, reg);
B
bellard 已提交
7388
        }
B
bellard 已提交
7389 7390
        break;
    case 0xd6: /* salc */
B
bellard 已提交
7391 7392
        if (CODE64(s))
            goto illegal_op;
7393
        gen_compute_eflags_c(s, cpu_T[0]);
7394 7395
        tcg_gen_neg_tl(cpu_T[0], cpu_T[0]);
        gen_op_mov_reg_T0(OT_BYTE, R_EAX);
B
bellard 已提交
7396 7397 7398 7399 7400
        break;
    case 0xe0: /* loopnz */
    case 0xe1: /* loopz */
    case 0xe2: /* loop */
    case 0xe3: /* jecxz */
B
bellard 已提交
7401
        {
7402
            int l1, l2, l3;
B
bellard 已提交
7403

7404
            tval = (int8_t)insn_get(env, s, OT_BYTE);
B
bellard 已提交
7405 7406 7407 7408
            next_eip = s->pc - s->cs_base;
            tval += next_eip;
            if (s->dflag == 0)
                tval &= 0xffff;
7409

B
bellard 已提交
7410 7411
            l1 = gen_new_label();
            l2 = gen_new_label();
7412
            l3 = gen_new_label();
B
bellard 已提交
7413
            b &= 3;
7414 7415 7416 7417 7418
            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);
7419
                gen_jcc1(s, (JCC_Z << 1) | (b ^ 1), l1);
7420 7421 7422 7423 7424 7425 7426 7427 7428
                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 已提交
7429 7430
            }

7431
            gen_set_label(l3);
B
bellard 已提交
7432
            gen_jmp_im(next_eip);
7433
            tcg_gen_br(l2);
7434

B
bellard 已提交
7435 7436 7437 7438 7439
            gen_set_label(l1);
            gen_jmp_im(tval);
            gen_set_label(l2);
            gen_eob(s);
        }
B
bellard 已提交
7440 7441 7442 7443 7444 7445
        break;
    case 0x130: /* wrmsr */
    case 0x132: /* rdmsr */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
7446
            gen_update_cc_op(s);
B
bellard 已提交
7447
            gen_jmp_im(pc_start - s->cs_base);
T
ths 已提交
7448
            if (b & 2) {
B
Blue Swirl 已提交
7449
                gen_helper_rdmsr(cpu_env);
T
ths 已提交
7450
            } else {
B
Blue Swirl 已提交
7451
                gen_helper_wrmsr(cpu_env);
T
ths 已提交
7452
            }
B
bellard 已提交
7453 7454 7455
        }
        break;
    case 0x131: /* rdtsc */
7456
        gen_update_cc_op(s);
B
bellard 已提交
7457
        gen_jmp_im(pc_start - s->cs_base);
P
pbrook 已提交
7458 7459
        if (use_icount)
            gen_io_start();
B
Blue Swirl 已提交
7460
        gen_helper_rdtsc(cpu_env);
P
pbrook 已提交
7461 7462 7463 7464
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
7465
        break;
7466
    case 0x133: /* rdpmc */
7467
        gen_update_cc_op(s);
7468
        gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7469
        gen_helper_rdpmc(cpu_env);
7470
        break;
7471
    case 0x134: /* sysenter */
7472
        /* For Intel SYSENTER is valid on 64-bit */
7473
        if (CODE64(s) && env->cpuid_vendor1 != CPUID_VENDOR_INTEL_1)
B
bellard 已提交
7474
            goto illegal_op;
7475 7476 7477
        if (!s->pe) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
J
Jun Koi 已提交
7478
            gen_update_cc_op(s);
B
bellard 已提交
7479
            gen_jmp_im(pc_start - s->cs_base);
7480
            gen_helper_sysenter(cpu_env);
7481 7482 7483 7484
            gen_eob(s);
        }
        break;
    case 0x135: /* sysexit */
7485
        /* For Intel SYSEXIT is valid on 64-bit */
7486
        if (CODE64(s) && env->cpuid_vendor1 != CPUID_VENDOR_INTEL_1)
B
bellard 已提交
7487
            goto illegal_op;
7488 7489 7490
        if (!s->pe) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
J
Jun Koi 已提交
7491
            gen_update_cc_op(s);
B
bellard 已提交
7492
            gen_jmp_im(pc_start - s->cs_base);
7493
            gen_helper_sysexit(cpu_env, tcg_const_i32(dflag));
7494 7495 7496
            gen_eob(s);
        }
        break;
B
bellard 已提交
7497 7498 7499
#ifdef TARGET_X86_64
    case 0x105: /* syscall */
        /* XXX: is it usable in real mode ? */
J
Jun Koi 已提交
7500
        gen_update_cc_op(s);
B
bellard 已提交
7501
        gen_jmp_im(pc_start - s->cs_base);
7502
        gen_helper_syscall(cpu_env, tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
7503 7504 7505 7506 7507 7508
        gen_eob(s);
        break;
    case 0x107: /* sysret */
        if (!s->pe) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
J
Jun Koi 已提交
7509
            gen_update_cc_op(s);
B
bellard 已提交
7510
            gen_jmp_im(pc_start - s->cs_base);
7511
            gen_helper_sysret(cpu_env, tcg_const_i32(s->dflag));
7512
            /* condition codes are modified only in long mode */
7513 7514 7515
            if (s->lma) {
                set_cc_op(s, CC_OP_EFLAGS);
            }
B
bellard 已提交
7516 7517 7518 7519
            gen_eob(s);
        }
        break;
#endif
B
bellard 已提交
7520
    case 0x1a2: /* cpuid */
7521
        gen_update_cc_op(s);
B
bellard 已提交
7522
        gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7523
        gen_helper_cpuid(cpu_env);
B
bellard 已提交
7524 7525 7526 7527 7528
        break;
    case 0xf4: /* hlt */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
7529
            gen_update_cc_op(s);
7530
            gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7531
            gen_helper_hlt(cpu_env, tcg_const_i32(s->pc - pc_start));
J
Jun Koi 已提交
7532
            s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
7533 7534 7535
        }
        break;
    case 0x100:
7536
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7537 7538 7539 7540
        mod = (modrm >> 6) & 3;
        op = (modrm >> 3) & 7;
        switch(op) {
        case 0: /* sldt */
7541 7542
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
7543
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_LDTR_READ);
B
bellard 已提交
7544
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,ldt.selector));
B
bellard 已提交
7545 7546 7547
            ot = OT_WORD;
            if (mod == 3)
                ot += s->dflag;
7548
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
7549 7550
            break;
        case 2: /* lldt */
7551 7552
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
7553 7554 7555
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
B
bellard 已提交
7556
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_LDTR_WRITE);
7557
                gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
B
bellard 已提交
7558
                gen_jmp_im(pc_start - s->cs_base);
7559
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
7560
                gen_helper_lldt(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
7561 7562 7563
            }
            break;
        case 1: /* str */
7564 7565
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
7566
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_TR_READ);
B
bellard 已提交
7567
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,tr.selector));
B
bellard 已提交
7568 7569 7570
            ot = OT_WORD;
            if (mod == 3)
                ot += s->dflag;
7571
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
7572 7573
            break;
        case 3: /* ltr */
7574 7575
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
7576 7577 7578
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
B
bellard 已提交
7579
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_TR_WRITE);
7580
                gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
B
bellard 已提交
7581
                gen_jmp_im(pc_start - s->cs_base);
7582
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
7583
                gen_helper_ltr(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
7584 7585 7586 7587
            }
            break;
        case 4: /* verr */
        case 5: /* verw */
7588 7589
            if (!s->pe || s->vm86)
                goto illegal_op;
7590
            gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
7591
            gen_update_cc_op(s);
7592 7593 7594 7595 7596
            if (op == 4) {
                gen_helper_verr(cpu_env, cpu_T[0]);
            } else {
                gen_helper_verw(cpu_env, cpu_T[0]);
            }
7597
            set_cc_op(s, CC_OP_EFLAGS);
7598
            break;
B
bellard 已提交
7599 7600 7601 7602 7603
        default:
            goto illegal_op;
        }
        break;
    case 0x101:
7604
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7605 7606
        mod = (modrm >> 6) & 3;
        op = (modrm >> 3) & 7;
B
bellard 已提交
7607
        rm = modrm & 7;
B
bellard 已提交
7608 7609 7610 7611
        switch(op) {
        case 0: /* sgdt */
            if (mod == 3)
                goto illegal_op;
B
bellard 已提交
7612
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_GDTR_READ);
7613
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
7614
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, gdt.limit));
B
bellard 已提交
7615
            gen_op_st_T0_A0(OT_WORD + s->mem_index);
7616
            gen_add_A0_im(s, 2);
B
bellard 已提交
7617
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, gdt.base));
B
bellard 已提交
7618 7619
            if (!s->dflag)
                gen_op_andl_T0_im(0xffffff);
B
bellard 已提交
7620
            gen_op_st_T0_A0(CODE64(s) + OT_LONG + s->mem_index);
B
bellard 已提交
7621
            break;
B
bellard 已提交
7622 7623 7624 7625 7626 7627 7628
        case 1:
            if (mod == 3) {
                switch (rm) {
                case 0: /* monitor */
                    if (!(s->cpuid_ext_features & CPUID_EXT_MONITOR) ||
                        s->cpl != 0)
                        goto illegal_op;
7629
                    gen_update_cc_op(s);
B
bellard 已提交
7630 7631 7632
                    gen_jmp_im(pc_start - s->cs_base);
#ifdef TARGET_X86_64
                    if (s->aflag == 2) {
7633
                        gen_op_movq_A0_reg(R_EAX);
7634
                    } else
B
bellard 已提交
7635 7636
#endif
                    {
7637
                        gen_op_movl_A0_reg(R_EAX);
B
bellard 已提交
7638 7639 7640 7641
                        if (s->aflag == 0)
                            gen_op_andl_A0_ffff();
                    }
                    gen_add_A0_ds_seg(s);
B
Blue Swirl 已提交
7642
                    gen_helper_monitor(cpu_env, cpu_A0);
B
bellard 已提交
7643 7644 7645 7646 7647
                    break;
                case 1: /* mwait */
                    if (!(s->cpuid_ext_features & CPUID_EXT_MONITOR) ||
                        s->cpl != 0)
                        goto illegal_op;
J
Jun Koi 已提交
7648
                    gen_update_cc_op(s);
7649
                    gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7650
                    gen_helper_mwait(cpu_env, tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
7651 7652
                    gen_eob(s);
                    break;
H
H. Peter Anvin 已提交
7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670
                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 已提交
7671 7672 7673 7674
                default:
                    goto illegal_op;
                }
            } else { /* sidt */
B
bellard 已提交
7675
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_IDTR_READ);
7676
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
7677
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, idt.limit));
B
bellard 已提交
7678
                gen_op_st_T0_A0(OT_WORD + s->mem_index);
B
bellard 已提交
7679
                gen_add_A0_im(s, 2);
B
bellard 已提交
7680
                tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, idt.base));
B
bellard 已提交
7681 7682
                if (!s->dflag)
                    gen_op_andl_T0_im(0xffffff);
B
bellard 已提交
7683
                gen_op_st_T0_A0(CODE64(s) + OT_LONG + s->mem_index);
B
bellard 已提交
7684 7685
            }
            break;
B
bellard 已提交
7686 7687
        case 2: /* lgdt */
        case 3: /* lidt */
T
ths 已提交
7688
            if (mod == 3) {
7689
                gen_update_cc_op(s);
B
bellard 已提交
7690
                gen_jmp_im(pc_start - s->cs_base);
T
ths 已提交
7691 7692
                switch(rm) {
                case 0: /* VMRUN */
B
bellard 已提交
7693 7694 7695 7696
                    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 已提交
7697
                        break;
B
bellard 已提交
7698
                    } else {
B
Blue Swirl 已提交
7699
                        gen_helper_vmrun(cpu_env, tcg_const_i32(s->aflag),
P
pbrook 已提交
7700
                                         tcg_const_i32(s->pc - pc_start));
7701
                        tcg_gen_exit_tb(0);
J
Jun Koi 已提交
7702
                        s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
7703
                    }
T
ths 已提交
7704 7705
                    break;
                case 1: /* VMMCALL */
B
bellard 已提交
7706 7707
                    if (!(s->flags & HF_SVME_MASK))
                        goto illegal_op;
B
Blue Swirl 已提交
7708
                    gen_helper_vmmcall(cpu_env);
T
ths 已提交
7709 7710
                    break;
                case 2: /* VMLOAD */
B
bellard 已提交
7711 7712 7713 7714 7715 7716
                    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 已提交
7717
                        gen_helper_vmload(cpu_env, tcg_const_i32(s->aflag));
B
bellard 已提交
7718
                    }
T
ths 已提交
7719 7720
                    break;
                case 3: /* VMSAVE */
B
bellard 已提交
7721 7722 7723 7724 7725 7726
                    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 已提交
7727
                        gen_helper_vmsave(cpu_env, tcg_const_i32(s->aflag));
B
bellard 已提交
7728
                    }
T
ths 已提交
7729 7730
                    break;
                case 4: /* STGI */
B
bellard 已提交
7731 7732 7733 7734 7735 7736 7737 7738
                    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 已提交
7739
                        gen_helper_stgi(cpu_env);
B
bellard 已提交
7740
                    }
T
ths 已提交
7741 7742
                    break;
                case 5: /* CLGI */
B
bellard 已提交
7743 7744 7745 7746 7747 7748
                    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 已提交
7749
                        gen_helper_clgi(cpu_env);
B
bellard 已提交
7750
                    }
T
ths 已提交
7751 7752
                    break;
                case 6: /* SKINIT */
B
bellard 已提交
7753 7754 7755 7756
                    if ((!(s->flags & HF_SVME_MASK) && 
                         !(s->cpuid_ext3_features & CPUID_EXT3_SKINIT)) || 
                        !s->pe)
                        goto illegal_op;
B
Blue Swirl 已提交
7757
                    gen_helper_skinit(cpu_env);
T
ths 已提交
7758 7759
                    break;
                case 7: /* INVLPGA */
B
bellard 已提交
7760 7761 7762 7763 7764 7765
                    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 已提交
7766
                        gen_helper_invlpga(cpu_env, tcg_const_i32(s->aflag));
B
bellard 已提交
7767
                    }
T
ths 已提交
7768 7769 7770 7771 7772
                    break;
                default:
                    goto illegal_op;
                }
            } else if (s->cpl != 0) {
B
bellard 已提交
7773 7774
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
B
bellard 已提交
7775 7776
                gen_svm_check_intercept(s, pc_start,
                                        op==2 ? SVM_EXIT_GDTR_WRITE : SVM_EXIT_IDTR_WRITE);
7777
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
7778
                gen_op_ld_T1_A0(OT_WORD + s->mem_index);
7779
                gen_add_A0_im(s, 2);
B
bellard 已提交
7780
                gen_op_ld_T0_A0(CODE64(s) + OT_LONG + s->mem_index);
B
bellard 已提交
7781 7782 7783
                if (!s->dflag)
                    gen_op_andl_T0_im(0xffffff);
                if (op == 2) {
B
bellard 已提交
7784 7785
                    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 已提交
7786
                } else {
B
bellard 已提交
7787 7788
                    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 已提交
7789 7790 7791 7792
                }
            }
            break;
        case 4: /* smsw */
B
bellard 已提交
7793
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_READ_CR0);
7794
#if defined TARGET_X86_64 && defined HOST_WORDS_BIGENDIAN
7795 7796
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,cr[0]) + 4);
#else
B
bellard 已提交
7797
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,cr[0]));
7798
#endif
7799
            gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 1);
B
bellard 已提交
7800 7801 7802 7803 7804
            break;
        case 6: /* lmsw */
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
B
bellard 已提交
7805
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_CR0);
7806
                gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
B
Blue Swirl 已提交
7807
                gen_helper_lmsw(cpu_env, cpu_T[0]);
B
bellard 已提交
7808
                gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
7809
                gen_eob(s);
B
bellard 已提交
7810 7811
            }
            break;
A
Andre Przywara 已提交
7812 7813 7814 7815 7816
        case 7:
            if (mod != 3) { /* invlpg */
                if (s->cpl != 0) {
                    gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
                } else {
7817
                    gen_update_cc_op(s);
A
Andre Przywara 已提交
7818
                    gen_jmp_im(pc_start - s->cs_base);
7819
                    gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
Blue Swirl 已提交
7820
                    gen_helper_invlpg(cpu_env, cpu_A0);
A
Andre Przywara 已提交
7821 7822 7823
                    gen_jmp_im(s->pc - s->cs_base);
                    gen_eob(s);
                }
B
bellard 已提交
7824
            } else {
A
Andre Przywara 已提交
7825 7826
                switch (rm) {
                case 0: /* swapgs */
B
bellard 已提交
7827
#ifdef TARGET_X86_64
A
Andre Przywara 已提交
7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840
                    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));
                        }
7841
                    } else
B
bellard 已提交
7842 7843 7844 7845
#endif
                    {
                        goto illegal_op;
                    }
A
Andre Przywara 已提交
7846 7847 7848 7849
                    break;
                case 1: /* rdtscp */
                    if (!(s->cpuid_ext2_features & CPUID_EXT2_RDTSCP))
                        goto illegal_op;
7850
                    gen_update_cc_op(s);
B
bellard 已提交
7851
                    gen_jmp_im(pc_start - s->cs_base);
A
Andre Przywara 已提交
7852 7853
                    if (use_icount)
                        gen_io_start();
B
Blue Swirl 已提交
7854
                    gen_helper_rdtscp(cpu_env);
A
Andre Przywara 已提交
7855 7856 7857 7858 7859 7860 7861
                    if (use_icount) {
                        gen_io_end();
                        gen_jmp(s, s->pc - s->cs_base);
                    }
                    break;
                default:
                    goto illegal_op;
B
bellard 已提交
7862
                }
B
bellard 已提交
7863 7864 7865 7866 7867 7868
            }
            break;
        default:
            goto illegal_op;
        }
        break;
7869 7870 7871 7872 7873
    case 0x108: /* invd */
    case 0x109: /* wbinvd */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
B
bellard 已提交
7874
            gen_svm_check_intercept(s, pc_start, (b & 2) ? SVM_EXIT_INVD : SVM_EXIT_WBINVD);
7875 7876 7877
            /* nothing to do */
        }
        break;
B
bellard 已提交
7878 7879 7880 7881 7882 7883 7884
    case 0x63: /* arpl or movslS (x86_64) */
#ifdef TARGET_X86_64
        if (CODE64(s)) {
            int d_ot;
            /* d_ot is the size of destination */
            d_ot = dflag + OT_WORD;

7885
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7886 7887 7888
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
            rm = (modrm & 7) | REX_B(s);
7889

B
bellard 已提交
7890
            if (mod == 3) {
B
bellard 已提交
7891
                gen_op_mov_TN_reg(OT_LONG, 0, rm);
B
bellard 已提交
7892 7893
                /* sign extend */
                if (d_ot == OT_QUAD)
B
bellard 已提交
7894
                    tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
7895
                gen_op_mov_reg_T0(d_ot, reg);
B
bellard 已提交
7896
            } else {
7897
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
7898
                if (d_ot == OT_QUAD) {
B
bellard 已提交
7899
                    gen_op_lds_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
7900
                } else {
B
bellard 已提交
7901
                    gen_op_ld_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
7902
                }
B
bellard 已提交
7903
                gen_op_mov_reg_T0(d_ot, reg);
B
bellard 已提交
7904
            }
7905
        } else
B
bellard 已提交
7906 7907
#endif
        {
7908
            int label1;
L
Laurent Desnogues 已提交
7909
            TCGv t0, t1, t2, a0;
7910

B
bellard 已提交
7911 7912
            if (!s->pe || s->vm86)
                goto illegal_op;
P
pbrook 已提交
7913 7914 7915
            t0 = tcg_temp_local_new();
            t1 = tcg_temp_local_new();
            t2 = tcg_temp_local_new();
7916
            ot = OT_WORD;
7917
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7918 7919 7920 7921
            reg = (modrm >> 3) & 7;
            mod = (modrm >> 6) & 3;
            rm = modrm & 7;
            if (mod != 3) {
7922
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
7923
                gen_op_ld_v(ot + s->mem_index, t0, cpu_A0);
L
Laurent Desnogues 已提交
7924 7925
                a0 = tcg_temp_local_new();
                tcg_gen_mov_tl(a0, cpu_A0);
B
bellard 已提交
7926
            } else {
7927
                gen_op_mov_v_reg(ot, t0, rm);
L
Laurent Desnogues 已提交
7928
                TCGV_UNUSED(a0);
B
bellard 已提交
7929
            }
7930 7931 7932 7933
            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);
7934
            label1 = gen_new_label();
7935 7936 7937 7938
            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);
7939
            gen_set_label(label1);
B
bellard 已提交
7940
            if (mod != 3) {
L
Laurent Desnogues 已提交
7941 7942 7943
                gen_op_st_v(ot + s->mem_index, t0, a0);
                tcg_temp_free(a0);
           } else {
7944
                gen_op_mov_reg_v(ot, rm, t0);
B
bellard 已提交
7945
            }
7946
            gen_compute_eflags(s);
7947
            tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~CC_Z);
7948 7949 7950 7951
            tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, t2);
            tcg_temp_free(t0);
            tcg_temp_free(t1);
            tcg_temp_free(t2);
7952 7953
        }
        break;
B
bellard 已提交
7954 7955
    case 0x102: /* lar */
    case 0x103: /* lsl */
7956 7957
        {
            int label1;
7958
            TCGv t0;
7959 7960 7961
            if (!s->pe || s->vm86)
                goto illegal_op;
            ot = dflag ? OT_LONG : OT_WORD;
7962
            modrm = cpu_ldub_code(env, s->pc++);
7963
            reg = ((modrm >> 3) & 7) | rex_r;
7964
            gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
P
pbrook 已提交
7965
            t0 = tcg_temp_local_new();
7966
            gen_update_cc_op(s);
7967 7968 7969 7970 7971
            if (b == 0x102) {
                gen_helper_lar(t0, cpu_env, cpu_T[0]);
            } else {
                gen_helper_lsl(t0, cpu_env, cpu_T[0]);
            }
7972 7973
            tcg_gen_andi_tl(cpu_tmp0, cpu_cc_src, CC_Z);
            label1 = gen_new_label();
P
pbrook 已提交
7974
            tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, label1);
7975
            gen_op_mov_reg_v(ot, reg, t0);
7976
            gen_set_label(label1);
7977
            set_cc_op(s, CC_OP_EFLAGS);
7978
            tcg_temp_free(t0);
7979
        }
B
bellard 已提交
7980 7981
        break;
    case 0x118:
7982
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7983 7984 7985 7986 7987 7988 7989 7990 7991
        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;
7992
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
7993 7994
            /* nothing more to do */
            break;
B
bellard 已提交
7995
        default: /* nop (multi byte) */
7996
            gen_nop_modrm(env, s, modrm);
B
bellard 已提交
7997
            break;
B
bellard 已提交
7998 7999
        }
        break;
B
bellard 已提交
8000
    case 0x119 ... 0x11f: /* nop (multi byte) */
8001 8002
        modrm = cpu_ldub_code(env, s->pc++);
        gen_nop_modrm(env, s, modrm);
B
bellard 已提交
8003
        break;
B
bellard 已提交
8004 8005 8006 8007 8008
    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 {
8009
            modrm = cpu_ldub_code(env, s->pc++);
8010 8011 8012 8013 8014
            /* 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 已提交
8015 8016 8017 8018 8019 8020
            rm = (modrm & 7) | REX_B(s);
            reg = ((modrm >> 3) & 7) | rex_r;
            if (CODE64(s))
                ot = OT_QUAD;
            else
                ot = OT_LONG;
8021 8022 8023 8024
            if ((prefixes & PREFIX_LOCK) && (reg == 0) &&
                (s->cpuid_ext3_features & CPUID_EXT3_CR8LEG)) {
                reg = 8;
            }
B
bellard 已提交
8025 8026 8027 8028 8029
            switch(reg) {
            case 0:
            case 2:
            case 3:
            case 4:
B
bellard 已提交
8030
            case 8:
8031
                gen_update_cc_op(s);
B
bellard 已提交
8032
                gen_jmp_im(pc_start - s->cs_base);
B
bellard 已提交
8033
                if (b & 2) {
B
bellard 已提交
8034
                    gen_op_mov_TN_reg(ot, 0, rm);
B
Blue Swirl 已提交
8035 8036
                    gen_helper_write_crN(cpu_env, tcg_const_i32(reg),
                                         cpu_T[0]);
B
bellard 已提交
8037
                    gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
8038 8039
                    gen_eob(s);
                } else {
B
Blue Swirl 已提交
8040
                    gen_helper_read_crN(cpu_T[0], cpu_env, tcg_const_i32(reg));
B
bellard 已提交
8041
                    gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053
                }
                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 {
8054
            modrm = cpu_ldub_code(env, s->pc++);
8055 8056 8057 8058 8059
            /* 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 已提交
8060 8061 8062 8063 8064 8065
            rm = (modrm & 7) | REX_B(s);
            reg = ((modrm >> 3) & 7) | rex_r;
            if (CODE64(s))
                ot = OT_QUAD;
            else
                ot = OT_LONG;
B
bellard 已提交
8066
            /* XXX: do it dynamically with CR4.DE bit */
B
bellard 已提交
8067
            if (reg == 4 || reg == 5 || reg >= 8)
B
bellard 已提交
8068 8069
                goto illegal_op;
            if (b & 2) {
T
ths 已提交
8070
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_DR0 + reg);
B
bellard 已提交
8071
                gen_op_mov_TN_reg(ot, 0, rm);
B
Blue Swirl 已提交
8072
                gen_helper_movl_drN_T0(cpu_env, tcg_const_i32(reg), cpu_T[0]);
B
bellard 已提交
8073
                gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
8074 8075
                gen_eob(s);
            } else {
T
ths 已提交
8076
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_READ_DR0 + reg);
B
bellard 已提交
8077
                tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,dr[reg]));
B
bellard 已提交
8078
                gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
8079 8080 8081 8082 8083 8084 8085
            }
        }
        break;
    case 0x106: /* clts */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
T
ths 已提交
8086
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_CR0);
8087
            gen_helper_clts(cpu_env);
B
bellard 已提交
8088
            /* abort block because static cpu state changed */
B
bellard 已提交
8089
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
8090
            gen_eob(s);
B
bellard 已提交
8091 8092
        }
        break;
B
balrog 已提交
8093
    /* MMX/3DNow!/SSE/SSE2/SSE3/SSSE3/SSE4 support */
B
bellard 已提交
8094 8095
    case 0x1c3: /* MOVNTI reg, mem */
        if (!(s->cpuid_features & CPUID_SSE2))
B
bellard 已提交
8096
            goto illegal_op;
B
bellard 已提交
8097
        ot = s->dflag == 2 ? OT_QUAD : OT_LONG;
8098
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
8099 8100 8101 8102 8103
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
        reg = ((modrm >> 3) & 7) | rex_r;
        /* generate a generic store */
8104
        gen_ldst_modrm(env, s, modrm, ot, reg, 1);
B
bellard 已提交
8105
        break;
B
bellard 已提交
8106
    case 0x1ae:
8107
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
8108 8109 8110 8111
        mod = (modrm >> 6) & 3;
        op = (modrm >> 3) & 7;
        switch(op) {
        case 0: /* fxsave */
8112
            if (mod == 3 || !(s->cpuid_features & CPUID_FXSR) ||
8113
                (s->prefix & PREFIX_LOCK))
B
bellard 已提交
8114
                goto illegal_op;
8115
            if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
B
bellard 已提交
8116 8117 8118
                gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
                break;
            }
8119
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8120
            gen_update_cc_op(s);
B
bellard 已提交
8121
            gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
8122
            gen_helper_fxsave(cpu_env, cpu_A0, tcg_const_i32((s->dflag == 2)));
B
bellard 已提交
8123 8124
            break;
        case 1: /* fxrstor */
8125
            if (mod == 3 || !(s->cpuid_features & CPUID_FXSR) ||
8126
                (s->prefix & PREFIX_LOCK))
B
bellard 已提交
8127
                goto illegal_op;
8128
            if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
B
bellard 已提交
8129 8130 8131
                gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
                break;
            }
8132
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8133
            gen_update_cc_op(s);
B
bellard 已提交
8134
            gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
8135 8136
            gen_helper_fxrstor(cpu_env, cpu_A0,
                               tcg_const_i32((s->dflag == 2)));
B
bellard 已提交
8137 8138 8139 8140 8141 8142
            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 已提交
8143
            }
B
bellard 已提交
8144 8145
            if ((s->flags & HF_EM_MASK) || !(s->flags & HF_OSFXSR_MASK) ||
                mod == 3)
B
bellard 已提交
8146
                goto illegal_op;
8147
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
8148
            if (op == 2) {
B
bellard 已提交
8149
                gen_op_ld_T0_A0(OT_LONG + s->mem_index);
8150
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
8151
                gen_helper_ldmxcsr(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
8152
            } else {
B
bellard 已提交
8153
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, mxcsr));
B
bellard 已提交
8154
                gen_op_st_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
8155
            }
B
bellard 已提交
8156 8157 8158
            break;
        case 5: /* lfence */
        case 6: /* mfence */
8159
            if ((modrm & 0xc7) != 0xc0 || !(s->cpuid_features & CPUID_SSE2))
B
bellard 已提交
8160 8161
                goto illegal_op;
            break;
8162 8163 8164
        case 7: /* sfence / clflush */
            if ((modrm & 0xc7) == 0xc0) {
                /* sfence */
A
aurel32 已提交
8165
                /* XXX: also check for cpuid_ext2_features & CPUID_EXT2_EMMX */
8166 8167 8168 8169 8170 8171
                if (!(s->cpuid_features & CPUID_SSE))
                    goto illegal_op;
            } else {
                /* clflush */
                if (!(s->cpuid_features & CPUID_CLFLUSH))
                    goto illegal_op;
8172
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8173 8174
            }
            break;
B
bellard 已提交
8175
        default:
B
bellard 已提交
8176 8177 8178
            goto illegal_op;
        }
        break;
A
aurel32 已提交
8179
    case 0x10d: /* 3DNow! prefetch(w) */
8180
        modrm = cpu_ldub_code(env, s->pc++);
A
aurel32 已提交
8181 8182 8183
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
8184
        gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8185 8186
        /* ignore for now */
        break;
B
bellard 已提交
8187
    case 0x1aa: /* rsm */
B
bellard 已提交
8188
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_RSM);
B
bellard 已提交
8189 8190
        if (!(s->flags & HF_SMM_MASK))
            goto illegal_op;
J
Jun Koi 已提交
8191
        gen_update_cc_op(s);
B
bellard 已提交
8192
        gen_jmp_im(s->pc - s->cs_base);
B
Blue Swirl 已提交
8193
        gen_helper_rsm(cpu_env);
B
bellard 已提交
8194 8195
        gen_eob(s);
        break;
B
balrog 已提交
8196 8197 8198 8199 8200 8201 8202
    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;

8203
        modrm = cpu_ldub_code(env, s->pc++);
M
malc 已提交
8204
        reg = ((modrm >> 3) & 7) | rex_r;
B
balrog 已提交
8205 8206 8207 8208 8209 8210 8211 8212

        if (s->prefix & PREFIX_DATA)
            ot = OT_WORD;
        else if (s->dflag != 2)
            ot = OT_LONG;
        else
            ot = OT_QUAD;

8213
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
Blue Swirl 已提交
8214
        gen_helper_popcnt(cpu_T[0], cpu_env, cpu_T[0], tcg_const_i32(ot));
B
balrog 已提交
8215
        gen_op_mov_reg_T0(ot, reg);
B
balrog 已提交
8216

8217
        set_cc_op(s, CC_OP_EFLAGS);
B
balrog 已提交
8218
        break;
A
aurel32 已提交
8219 8220 8221
    case 0x10e ... 0x10f:
        /* 3DNow! instructions, ignore prefixes */
        s->prefix &= ~(PREFIX_REPZ | PREFIX_REPNZ | PREFIX_DATA);
B
bellard 已提交
8222 8223
    case 0x110 ... 0x117:
    case 0x128 ... 0x12f:
B
balrog 已提交
8224
    case 0x138 ... 0x13a:
8225
    case 0x150 ... 0x179:
B
bellard 已提交
8226 8227 8228 8229
    case 0x17c ... 0x17f:
    case 0x1c2:
    case 0x1c4 ... 0x1c6:
    case 0x1d0 ... 0x1fe:
8230
        gen_sse(env, s, b, pc_start, rex_r);
B
bellard 已提交
8231
        break;
B
bellard 已提交
8232 8233 8234 8235 8236
    default:
        goto illegal_op;
    }
    /* lock generation */
    if (s->prefix & PREFIX_LOCK)
P
pbrook 已提交
8237
        gen_helper_unlock();
B
bellard 已提交
8238 8239
    return s->pc;
 illegal_op:
8240
    if (s->prefix & PREFIX_LOCK)
P
pbrook 已提交
8241
        gen_helper_unlock();
B
bellard 已提交
8242 8243 8244 8245 8246 8247 8248
    /* 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 已提交
8249 8250
    cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
    cpu_cc_op = tcg_global_mem_new_i32(TCG_AREG0,
8251 8252
                                       offsetof(CPUX86State, cc_op), "cc_op");
    cpu_cc_dst = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_dst),
P
pbrook 已提交
8253
                                    "cc_dst");
8254 8255
    cpu_cc_src = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_src),
                                    "cc_src");
8256 8257
    cpu_cc_src2 = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_src2),
                                     "cc_src2");
8258

8259 8260
#ifdef TARGET_X86_64
    cpu_regs[R_EAX] = tcg_global_mem_new_i64(TCG_AREG0,
8261
                                             offsetof(CPUX86State, regs[R_EAX]), "rax");
8262
    cpu_regs[R_ECX] = tcg_global_mem_new_i64(TCG_AREG0,
8263
                                             offsetof(CPUX86State, regs[R_ECX]), "rcx");
8264
    cpu_regs[R_EDX] = tcg_global_mem_new_i64(TCG_AREG0,
8265
                                             offsetof(CPUX86State, regs[R_EDX]), "rdx");
8266
    cpu_regs[R_EBX] = tcg_global_mem_new_i64(TCG_AREG0,
8267
                                             offsetof(CPUX86State, regs[R_EBX]), "rbx");
8268
    cpu_regs[R_ESP] = tcg_global_mem_new_i64(TCG_AREG0,
8269
                                             offsetof(CPUX86State, regs[R_ESP]), "rsp");
8270
    cpu_regs[R_EBP] = tcg_global_mem_new_i64(TCG_AREG0,
8271
                                             offsetof(CPUX86State, regs[R_EBP]), "rbp");
8272
    cpu_regs[R_ESI] = tcg_global_mem_new_i64(TCG_AREG0,
8273
                                             offsetof(CPUX86State, regs[R_ESI]), "rsi");
8274
    cpu_regs[R_EDI] = tcg_global_mem_new_i64(TCG_AREG0,
8275
                                             offsetof(CPUX86State, regs[R_EDI]), "rdi");
8276
    cpu_regs[8] = tcg_global_mem_new_i64(TCG_AREG0,
8277
                                         offsetof(CPUX86State, regs[8]), "r8");
8278
    cpu_regs[9] = tcg_global_mem_new_i64(TCG_AREG0,
8279
                                          offsetof(CPUX86State, regs[9]), "r9");
8280
    cpu_regs[10] = tcg_global_mem_new_i64(TCG_AREG0,
8281
                                          offsetof(CPUX86State, regs[10]), "r10");
8282
    cpu_regs[11] = tcg_global_mem_new_i64(TCG_AREG0,
8283
                                          offsetof(CPUX86State, regs[11]), "r11");
8284
    cpu_regs[12] = tcg_global_mem_new_i64(TCG_AREG0,
8285
                                          offsetof(CPUX86State, regs[12]), "r12");
8286
    cpu_regs[13] = tcg_global_mem_new_i64(TCG_AREG0,
8287
                                          offsetof(CPUX86State, regs[13]), "r13");
8288
    cpu_regs[14] = tcg_global_mem_new_i64(TCG_AREG0,
8289
                                          offsetof(CPUX86State, regs[14]), "r14");
8290
    cpu_regs[15] = tcg_global_mem_new_i64(TCG_AREG0,
8291
                                          offsetof(CPUX86State, regs[15]), "r15");
8292 8293
#else
    cpu_regs[R_EAX] = tcg_global_mem_new_i32(TCG_AREG0,
8294
                                             offsetof(CPUX86State, regs[R_EAX]), "eax");
8295
    cpu_regs[R_ECX] = tcg_global_mem_new_i32(TCG_AREG0,
8296
                                             offsetof(CPUX86State, regs[R_ECX]), "ecx");
8297
    cpu_regs[R_EDX] = tcg_global_mem_new_i32(TCG_AREG0,
8298
                                             offsetof(CPUX86State, regs[R_EDX]), "edx");
8299
    cpu_regs[R_EBX] = tcg_global_mem_new_i32(TCG_AREG0,
8300
                                             offsetof(CPUX86State, regs[R_EBX]), "ebx");
8301
    cpu_regs[R_ESP] = tcg_global_mem_new_i32(TCG_AREG0,
8302
                                             offsetof(CPUX86State, regs[R_ESP]), "esp");
8303
    cpu_regs[R_EBP] = tcg_global_mem_new_i32(TCG_AREG0,
8304
                                             offsetof(CPUX86State, regs[R_EBP]), "ebp");
8305
    cpu_regs[R_ESI] = tcg_global_mem_new_i32(TCG_AREG0,
8306
                                             offsetof(CPUX86State, regs[R_ESI]), "esi");
8307
    cpu_regs[R_EDI] = tcg_global_mem_new_i32(TCG_AREG0,
8308
                                             offsetof(CPUX86State, regs[R_EDI]), "edi");
8309 8310
#endif

8311
    /* register helpers */
P
pbrook 已提交
8312
#define GEN_HELPER 2
8313
#include "helper.h"
B
bellard 已提交
8314 8315 8316 8317 8318
}

/* 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. */
8319
static inline void gen_intermediate_code_internal(CPUX86State *env,
8320 8321
                                                  TranslationBlock *tb,
                                                  int search_pc)
B
bellard 已提交
8322 8323
{
    DisasContext dc1, *dc = &dc1;
B
bellard 已提交
8324
    target_ulong pc_ptr;
B
bellard 已提交
8325
    uint16_t *gen_opc_end;
8326
    CPUBreakpoint *bp;
8327
    int j, lj;
8328
    uint64_t flags;
B
bellard 已提交
8329 8330
    target_ulong pc_start;
    target_ulong cs_base;
P
pbrook 已提交
8331 8332
    int num_insns;
    int max_insns;
8333

B
bellard 已提交
8334
    /* generate intermediate code */
B
bellard 已提交
8335 8336
    pc_start = tb->pc;
    cs_base = tb->cs_base;
B
bellard 已提交
8337
    flags = tb->flags;
B
bellard 已提交
8338

8339
    dc->pe = (flags >> HF_PE_SHIFT) & 1;
B
bellard 已提交
8340 8341 8342 8343 8344 8345 8346 8347
    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;
8348
    dc->singlestep_enabled = env->singlestep_enabled;
B
bellard 已提交
8349
    dc->cc_op = CC_OP_DYNAMIC;
8350
    dc->cc_op_dirty = false;
B
bellard 已提交
8351 8352 8353 8354 8355 8356
    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) {
H
H. Peter Anvin 已提交
8357
        dc->mem_index = (cpu_mmu_index(env) + 1) << 2;
B
bellard 已提交
8358
    }
B
bellard 已提交
8359
    dc->cpuid_features = env->cpuid_features;
B
bellard 已提交
8360
    dc->cpuid_ext_features = env->cpuid_ext_features;
8361
    dc->cpuid_ext2_features = env->cpuid_ext2_features;
B
bellard 已提交
8362
    dc->cpuid_ext3_features = env->cpuid_ext3_features;
H
H. Peter Anvin 已提交
8363
    dc->cpuid_7_0_ebx_features = env->cpuid_7_0_ebx_features;
B
bellard 已提交
8364 8365 8366 8367
#ifdef TARGET_X86_64
    dc->lma = (flags >> HF_LMA_SHIFT) & 1;
    dc->code64 = (flags >> HF_CS64_SHIFT) & 1;
#endif
B
bellard 已提交
8368
    dc->flags = flags;
8369 8370
    dc->jmp_opt = !(dc->tf || env->singlestep_enabled ||
                    (flags & HF_INHIBIT_IRQ_MASK)
B
bellard 已提交
8371
#ifndef CONFIG_SOFTMMU
B
bellard 已提交
8372 8373 8374
                    || (flags & HF_SOFTMMU_MASK)
#endif
                    );
8375 8376
#if 0
    /* check addseg logic */
B
bellard 已提交
8377
    if (!dc->addseg && (dc->vm86 || !dc->pe || !dc->code32))
8378 8379 8380
        printf("ERROR addseg\n");
#endif

P
pbrook 已提交
8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392
    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_tmp5 = tcg_temp_new();
    cpu_ptr0 = tcg_temp_new_ptr();
    cpu_ptr1 = tcg_temp_new_ptr();
8393
    cpu_cc_srcT = tcg_temp_local_new();
B
bellard 已提交
8394

8395
    gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
B
bellard 已提交
8396 8397 8398 8399

    dc->is_jmp = DISAS_NEXT;
    pc_ptr = pc_start;
    lj = -1;
P
pbrook 已提交
8400 8401 8402 8403
    num_insns = 0;
    max_insns = tb->cflags & CF_COUNT_MASK;
    if (max_insns == 0)
        max_insns = CF_COUNT_MASK;
B
bellard 已提交
8404

P
pbrook 已提交
8405
    gen_icount_start();
B
bellard 已提交
8406
    for(;;) {
B
Blue Swirl 已提交
8407 8408
        if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
            QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
J
Jan Kiszka 已提交
8409 8410
                if (bp->pc == pc_ptr &&
                    !((bp->flags & BP_CPU) && (tb->flags & HF_RF_MASK))) {
B
bellard 已提交
8411 8412 8413 8414 8415 8416
                    gen_debug(dc, pc_ptr - dc->cs_base);
                    break;
                }
            }
        }
        if (search_pc) {
8417
            j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
B
bellard 已提交
8418 8419 8420
            if (lj < j) {
                lj++;
                while (lj < j)
8421
                    tcg_ctx.gen_opc_instr_start[lj++] = 0;
B
bellard 已提交
8422
            }
8423
            tcg_ctx.gen_opc_pc[lj] = pc_ptr;
B
bellard 已提交
8424
            gen_opc_cc_op[lj] = dc->cc_op;
8425
            tcg_ctx.gen_opc_instr_start[lj] = 1;
8426
            tcg_ctx.gen_opc_icount[lj] = num_insns;
B
bellard 已提交
8427
        }
P
pbrook 已提交
8428 8429 8430
        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
            gen_io_start();

8431
        pc_ptr = disas_insn(env, dc, pc_ptr);
P
pbrook 已提交
8432
        num_insns++;
B
bellard 已提交
8433 8434 8435 8436 8437
        /* stop translation if indicated */
        if (dc->is_jmp)
            break;
        /* if single step mode, we generate only one instruction and
           generate an exception */
8438 8439 8440
        /* 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 */
8441
        if (dc->tf || dc->singlestep_enabled ||
P
pbrook 已提交
8442
            (flags & HF_INHIBIT_IRQ_MASK)) {
B
bellard 已提交
8443
            gen_jmp_im(pc_ptr - dc->cs_base);
B
bellard 已提交
8444 8445 8446 8447
            gen_eob(dc);
            break;
        }
        /* if too long translation, stop generation too */
8448
        if (tcg_ctx.gen_opc_ptr >= gen_opc_end ||
P
pbrook 已提交
8449 8450
            (pc_ptr - pc_start) >= (TARGET_PAGE_SIZE - 32) ||
            num_insns >= max_insns) {
B
bellard 已提交
8451
            gen_jmp_im(pc_ptr - dc->cs_base);
B
bellard 已提交
8452 8453 8454
            gen_eob(dc);
            break;
        }
8455 8456 8457 8458 8459
        if (singlestep) {
            gen_jmp_im(pc_ptr - dc->cs_base);
            gen_eob(dc);
            break;
        }
B
bellard 已提交
8460
    }
P
pbrook 已提交
8461 8462 8463
    if (tb->cflags & CF_LAST_IO)
        gen_io_end();
    gen_icount_end(tb, num_insns);
8464
    *tcg_ctx.gen_opc_ptr = INDEX_op_end;
B
bellard 已提交
8465 8466
    /* we don't forget to fill the last values */
    if (search_pc) {
8467
        j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
B
bellard 已提交
8468 8469
        lj++;
        while (lj <= j)
8470
            tcg_ctx.gen_opc_instr_start[lj++] = 0;
B
bellard 已提交
8471
    }
8472

B
bellard 已提交
8473
#ifdef DEBUG_DISAS
8474
    if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
B
bellard 已提交
8475
        int disas_flags;
8476 8477
        qemu_log("----------------\n");
        qemu_log("IN: %s\n", lookup_symbol(pc_start));
B
bellard 已提交
8478 8479 8480 8481 8482 8483
#ifdef TARGET_X86_64
        if (dc->code64)
            disas_flags = 2;
        else
#endif
            disas_flags = !dc->code32;
B
Blue Swirl 已提交
8484
        log_target_disas(env, pc_start, pc_ptr - pc_start, disas_flags);
8485
        qemu_log("\n");
B
bellard 已提交
8486 8487 8488
    }
#endif

P
pbrook 已提交
8489
    if (!search_pc) {
B
bellard 已提交
8490
        tb->size = pc_ptr - pc_start;
P
pbrook 已提交
8491 8492
        tb->icount = num_insns;
    }
B
bellard 已提交
8493 8494
}

8495
void gen_intermediate_code(CPUX86State *env, TranslationBlock *tb)
B
bellard 已提交
8496
{
8497
    gen_intermediate_code_internal(env, tb, 0);
B
bellard 已提交
8498 8499
}

8500
void gen_intermediate_code_pc(CPUX86State *env, TranslationBlock *tb)
B
bellard 已提交
8501
{
8502
    gen_intermediate_code_internal(env, tb, 1);
B
bellard 已提交
8503 8504
}

8505
void restore_state_to_opc(CPUX86State *env, TranslationBlock *tb, int pc_pos)
A
aurel32 已提交
8506 8507 8508
{
    int cc_op;
#ifdef DEBUG_DISAS
8509
    if (qemu_loglevel_mask(CPU_LOG_TB_OP)) {
A
aurel32 已提交
8510
        int i;
8511
        qemu_log("RESTORE:\n");
A
aurel32 已提交
8512
        for(i = 0;i <= pc_pos; i++) {
8513
            if (tcg_ctx.gen_opc_instr_start[i]) {
8514 8515
                qemu_log("0x%04x: " TARGET_FMT_lx "\n", i,
                        tcg_ctx.gen_opc_pc[i]);
A
aurel32 已提交
8516 8517
            }
        }
8518
        qemu_log("pc_pos=0x%x eip=" TARGET_FMT_lx " cs_base=%x\n",
8519
                pc_pos, tcg_ctx.gen_opc_pc[pc_pos] - tb->cs_base,
A
aurel32 已提交
8520 8521 8522
                (uint32_t)tb->cs_base);
    }
#endif
8523
    env->eip = tcg_ctx.gen_opc_pc[pc_pos] - tb->cs_base;
A
aurel32 已提交
8524 8525 8526 8527
    cc_op = gen_opc_cc_op[pc_pos];
    if (cc_op != CC_OP_DYNAMIC)
        env->cc_op = cc_op;
}