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;
B
bellard 已提交
75

76 77
static uint8_t gen_opc_cc_op[OPC_BUF_SIZE];

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

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

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

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

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

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

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

B
bellard 已提交
166 167 168 169
/* operand size */
enum {
    OT_BYTE = 0,
    OT_WORD,
170
    OT_LONG,
B
bellard 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183
    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 已提交
184 185

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

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

/* Bit set if the global variable is live after setting CC_OP to X.  */
static const uint8_t cc_op_live[CC_OP_NB] = {
199
    [CC_OP_DYNAMIC] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
200 201 202
    [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,
203
    [CC_OP_ADCB ... CC_OP_ADCQ] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
204
    [CC_OP_SUBB ... CC_OP_SUBQ] = USES_CC_DST | USES_CC_SRC | USES_CC_SRCT,
205
    [CC_OP_SBBB ... CC_OP_SBBQ] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
206 207 208 209 210
    [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,
211
    [CC_OP_BMILGB ... CC_OP_BMILGQ] = USES_CC_DST | USES_CC_SRC,
212 213 214
    [CC_OP_ADCX] = USES_CC_DST | USES_CC_SRC,
    [CC_OP_ADOX] = USES_CC_SRC | USES_CC_SRC2,
    [CC_OP_ADCOX] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
R
Richard Henderson 已提交
215
    [CC_OP_CLR] = 0,
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 245 246 247 248 249 250 251
    if (op == CC_OP_DYNAMIC) {
        /* The DYNAMIC setting is translator only, and should never be
           stored.  Thus we always consider it clean.  */
        s->cc_op_dirty = false;
    } else {
        /* Discard any computed CC_OP value (see shifts).  */
        if (s->cc_op == CC_OP_DYNAMIC) {
            tcg_gen_discard_i32(cpu_cc_op);
        }
        s->cc_op_dirty = true;
    }
252
    s->cc_op = op;
253 254 255 256 257
}

static void gen_update_cc_op(DisasContext *s)
{
    if (s->cc_op_dirty) {
258
        tcg_gen_movi_i32(cpu_cc_op, s->cc_op);
259 260
        s->cc_op_dirty = false;
    }
261 262
}

B
bellard 已提交
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 322 323 324 325 326 327 328 329
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 已提交
330 331 332 333 334 335 336 337 338 339
#ifdef TARGET_X86_64

#define NB_OP_SIZES 4

#else /* !TARGET_X86_64 */

#define NB_OP_SIZES 3

#endif /* !TARGET_X86_64 */

340
#if defined(HOST_WORDS_BIGENDIAN)
B
bellard 已提交
341 342 343 344 345
#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 已提交
346
#else
B
bellard 已提交
347 348 349 350 351
#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 已提交
352
#endif
B
bellard 已提交
353

354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
/* 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;
}

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

B
bellard 已提交
400 401
static inline void gen_op_mov_reg_T0(int ot, int reg)
{
402
    gen_op_mov_reg_v(ot, reg, cpu_T[0]);
B
bellard 已提交
403 404 405 406
}

static inline void gen_op_mov_reg_T1(int ot, int reg)
{
407
    gen_op_mov_reg_v(ot, reg, cpu_T[1]);
B
bellard 已提交
408 409 410 411 412
}

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

430
static inline void gen_op_mov_v_reg(int ot, TCGv t0, int reg)
B
bellard 已提交
431
{
432 433 434 435
    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 {
436
        tcg_gen_mov_tl(t0, cpu_regs[reg]);
B
bellard 已提交
437 438 439
    }
}

440 441 442 443 444
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 已提交
445 446
static inline void gen_op_movl_A0_reg(int reg)
{
447
    tcg_gen_mov_tl(cpu_A0, cpu_regs[reg]);
B
bellard 已提交
448 449 450 451 452
}

static inline void gen_op_addl_A0_im(int32_t val)
{
    tcg_gen_addi_tl(cpu_A0, cpu_A0, val);
B
bellard 已提交
453
#ifdef TARGET_X86_64
B
bellard 已提交
454
    tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffffffff);
B
bellard 已提交
455
#endif
B
bellard 已提交
456
}
B
bellard 已提交
457

B
bellard 已提交
458
#ifdef TARGET_X86_64
B
bellard 已提交
459 460 461 462
static inline void gen_op_addq_A0_im(int64_t val)
{
    tcg_gen_addi_tl(cpu_A0, cpu_A0, val);
}
B
bellard 已提交
463
#endif
B
bellard 已提交
464 465 466 467 468 469 470 471 472 473
    
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 已提交
474

B
bellard 已提交
475
static inline void gen_op_addl_T0_T1(void)
B
bellard 已提交
476
{
B
bellard 已提交
477 478 479 480 481
    tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
}

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

485
static inline void gen_op_add_reg_im(int size, int reg, int32_t val)
B
bellard 已提交
486
{
487
    switch(size) {
488
    case OT_BYTE:
489
        tcg_gen_addi_tl(cpu_tmp0, cpu_regs[reg], val);
490
        tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], cpu_tmp0, 0, 16);
491
        break;
492
    case OT_WORD:
493 494 495 496 497
        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);
498 499
        break;
#ifdef TARGET_X86_64
500
    case OT_LONG:
501
        tcg_gen_addi_tl(cpu_regs[reg], cpu_regs[reg], val);
502 503 504
        break;
#endif
    }
B
bellard 已提交
505 506
}

507
static inline void gen_op_add_reg_T0(int size, int reg)
B
bellard 已提交
508
{
509
    switch(size) {
510
    case OT_BYTE:
511
        tcg_gen_add_tl(cpu_tmp0, cpu_regs[reg], cpu_T[0]);
512
        tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], cpu_tmp0, 0, 16);
513
        break;
514
    case OT_WORD:
515 516 517 518 519
        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);
520
        break;
B
bellard 已提交
521
#ifdef TARGET_X86_64
522
    case OT_LONG:
523
        tcg_gen_add_tl(cpu_regs[reg], cpu_regs[reg], cpu_T[0]);
524
        break;
B
bellard 已提交
525
#endif
526 527
    }
}
B
bellard 已提交
528 529 530

static inline void gen_op_addl_A0_reg_sN(int shift, int reg)
{
531 532
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[reg]);
    if (shift != 0)
B
bellard 已提交
533 534
        tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, shift);
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
535 536 537
    /* 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 已提交
538
}
B
bellard 已提交
539

B
bellard 已提交
540 541
static inline void gen_op_movl_A0_seg(int reg)
{
542
    tcg_gen_ld32u_tl(cpu_A0, cpu_env, offsetof(CPUX86State, segs[reg].base) + REG_L_OFFSET);
B
bellard 已提交
543
}
B
bellard 已提交
544

545
static inline void gen_op_addl_A0_seg(DisasContext *s, int reg)
B
bellard 已提交
546
{
547
    tcg_gen_ld_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, segs[reg].base));
B
bellard 已提交
548
#ifdef TARGET_X86_64
549 550 551 552 553 554 555 556 557
    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 已提交
558 559
#endif
}
B
bellard 已提交
560

B
bellard 已提交
561
#ifdef TARGET_X86_64
B
bellard 已提交
562 563
static inline void gen_op_movq_A0_seg(int reg)
{
564
    tcg_gen_ld_tl(cpu_A0, cpu_env, offsetof(CPUX86State, segs[reg].base));
B
bellard 已提交
565
}
B
bellard 已提交
566

B
bellard 已提交
567 568
static inline void gen_op_addq_A0_seg(int reg)
{
569
    tcg_gen_ld_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, segs[reg].base));
B
bellard 已提交
570 571 572 573 574
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
}

static inline void gen_op_movq_A0_reg(int reg)
{
575
    tcg_gen_mov_tl(cpu_A0, cpu_regs[reg]);
B
bellard 已提交
576 577 578 579
}

static inline void gen_op_addq_A0_reg_sN(int shift, int reg)
{
580 581
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[reg]);
    if (shift != 0)
B
bellard 已提交
582 583 584
        tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, shift);
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
}
B
bellard 已提交
585 586
#endif

B
bellard 已提交
587 588 589 590
static inline void gen_op_lds_T0_A0(int idx)
{
    int mem_index = (idx >> 2) - 1;
    switch(idx & 3) {
591
    case OT_BYTE:
B
bellard 已提交
592 593
        tcg_gen_qemu_ld8s(cpu_T[0], cpu_A0, mem_index);
        break;
594
    case OT_WORD:
B
bellard 已提交
595 596 597
        tcg_gen_qemu_ld16s(cpu_T[0], cpu_A0, mem_index);
        break;
    default:
598
    case OT_LONG:
B
bellard 已提交
599 600 601 602
        tcg_gen_qemu_ld32s(cpu_T[0], cpu_A0, mem_index);
        break;
    }
}
B
bellard 已提交
603

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

627 628 629 630 631 632
/* 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 已提交
633 634
static inline void gen_op_ldu_T0_A0(int idx)
{
635
    gen_op_ld_v(idx, cpu_T[0], cpu_A0);
B
bellard 已提交
636
}
B
bellard 已提交
637

B
bellard 已提交
638
static inline void gen_op_ld_T1_A0(int idx)
639 640 641 642 643
{
    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 已提交
644 645 646
{
    int mem_index = (idx >> 2) - 1;
    switch(idx & 3) {
647
    case OT_BYTE:
648
        tcg_gen_qemu_st8(t0, a0, mem_index);
B
bellard 已提交
649
        break;
650
    case OT_WORD:
651
        tcg_gen_qemu_st16(t0, a0, mem_index);
B
bellard 已提交
652
        break;
653
    case OT_LONG:
654
        tcg_gen_qemu_st32(t0, a0, mem_index);
B
bellard 已提交
655 656
        break;
    default:
657
    case OT_QUAD:
P
pbrook 已提交
658 659
        /* Should never happen on 32-bit targets.  */
#ifdef TARGET_X86_64
660
        tcg_gen_qemu_st64(t0, a0, mem_index);
P
pbrook 已提交
661
#endif
B
bellard 已提交
662 663 664
        break;
    }
}
665

B
bellard 已提交
666 667
static inline void gen_op_st_T0_A0(int idx)
{
668
    gen_op_st_v(idx, cpu_T[0], cpu_A0);
B
bellard 已提交
669
}
670

B
bellard 已提交
671 672
static inline void gen_op_st_T1_A0(int idx)
{
673
    gen_op_st_v(idx, cpu_T[1], cpu_A0);
B
bellard 已提交
674
}
675

B
bellard 已提交
676 677
static inline void gen_jmp_im(target_ulong pc)
{
B
bellard 已提交
678
    tcg_gen_movi_tl(cpu_tmp0, pc);
679
    tcg_gen_st_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, eip));
B
bellard 已提交
680 681
}

B
bellard 已提交
682 683 684 685 686
static inline void gen_string_movl_A0_ESI(DisasContext *s)
{
    int override;

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

static inline void gen_string_movl_A0_EDI(DisasContext *s)
{
B
bellard 已提交
719 720
#ifdef TARGET_X86_64
    if (s->aflag == 2) {
B
bellard 已提交
721
        gen_op_movq_A0_reg(R_EDI);
B
bellard 已提交
722 723
    } else
#endif
B
bellard 已提交
724 725
    if (s->aflag) {
        if (s->addseg) {
B
bellard 已提交
726 727
            gen_op_movl_A0_seg(R_ES);
            gen_op_addl_A0_reg_sN(0, R_EDI);
B
bellard 已提交
728
        } else {
B
bellard 已提交
729
            gen_op_movl_A0_reg(R_EDI);
B
bellard 已提交
730 731
        }
    } else {
B
bellard 已提交
732
        gen_op_movl_A0_reg(R_EDI);
B
bellard 已提交
733
        gen_op_andl_A0_ffff();
734
        gen_op_addl_A0_seg(s, R_ES);
B
bellard 已提交
735 736 737
    }
}

738 739
static inline void gen_op_movl_T0_Dshift(int ot) 
{
740
    tcg_gen_ld32s_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, df));
741
    tcg_gen_shli_tl(cpu_T[0], cpu_T[0], ot);
B
bellard 已提交
742 743
};

744
static TCGv gen_ext_tl(TCGv dst, TCGv src, int size, bool sign)
745
{
746
    switch (size) {
747
    case OT_BYTE:
748 749 750 751 752 753
        if (sign) {
            tcg_gen_ext8s_tl(dst, src);
        } else {
            tcg_gen_ext8u_tl(dst, src);
        }
        return dst;
754
    case OT_WORD:
755 756 757 758 759 760 761
        if (sign) {
            tcg_gen_ext16s_tl(dst, src);
        } else {
            tcg_gen_ext16u_tl(dst, src);
        }
        return dst;
#ifdef TARGET_X86_64
762
    case OT_LONG:
763 764 765 766 767 768 769
        if (sign) {
            tcg_gen_ext32s_tl(dst, src);
        } else {
            tcg_gen_ext32u_tl(dst, src);
        }
        return dst;
#endif
770
    default:
771
        return src;
772 773
    }
}
774

775 776 777 778 779
static void gen_extu(int ot, TCGv reg)
{
    gen_ext_tl(reg, reg, ot, false);
}

780 781
static void gen_exts(int ot, TCGv reg)
{
782
    gen_ext_tl(reg, reg, ot, true);
783
}
B
bellard 已提交
784

785 786
static inline void gen_op_jnz_ecx(int size, int label1)
{
787
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
788
    gen_extu(size + 1, cpu_tmp0);
P
pbrook 已提交
789
    tcg_gen_brcondi_tl(TCG_COND_NE, cpu_tmp0, 0, label1);
790 791 792 793
}

static inline void gen_op_jz_ecx(int size, int label1)
{
794
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
795
    gen_extu(size + 1, cpu_tmp0);
P
pbrook 已提交
796
    tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, label1);
797
}
B
bellard 已提交
798

P
pbrook 已提交
799 800 801
static void gen_helper_in_func(int ot, TCGv v, TCGv_i32 n)
{
    switch (ot) {
802 803 804 805 806 807 808 809 810
    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 已提交
811 812
    }
}
B
bellard 已提交
813

P
pbrook 已提交
814 815 816
static void gen_helper_out_func(int ot, TCGv_i32 v, TCGv_i32 n)
{
    switch (ot) {
817 818 819 820 821 822 823 824 825
    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 已提交
826 827
    }
}
828

829 830
static void gen_check_io(DisasContext *s, int ot, target_ulong cur_eip,
                         uint32_t svm_flags)
831
{
832 833 834 835
    int state_saved;
    target_ulong next_eip;

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

B
bellard 已提交
867 868 869
static inline void gen_movs(DisasContext *s, int ot)
{
    gen_string_movl_A0_ESI(s);
B
bellard 已提交
870
    gen_op_ld_T0_A0(ot + s->mem_index);
B
bellard 已提交
871
    gen_string_movl_A0_EDI(s);
B
bellard 已提交
872
    gen_op_st_T0_A0(ot + s->mem_index);
873 874 875
    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 已提交
876 877
}

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

889 890 891 892 893 894 895
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]);
}

896 897 898 899 900 901 902 903
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]);
904 905
    tcg_gen_neg_tl(cpu_cc_src, cpu_T[0]);
    tcg_gen_movi_tl(cpu_cc_srcT, 0);
906 907
}

908 909
/* compute all eflags to cc_src */
static void gen_compute_eflags(DisasContext *s)
910
{
911
    TCGv zero, dst, src1, src2;
912 913
    int live, dead;

914 915 916
    if (s->cc_op == CC_OP_EFLAGS) {
        return;
    }
R
Richard Henderson 已提交
917 918 919 920 921
    if (s->cc_op == CC_OP_CLR) {
        tcg_gen_movi_tl(cpu_cc_src, CC_Z);
        set_cc_op(s, CC_OP_EFLAGS);
        return;
    }
922 923 924 925

    TCGV_UNUSED(zero);
    dst = cpu_cc_dst;
    src1 = cpu_cc_src;
926
    src2 = cpu_cc_src2;
927 928 929

    /* Take care to not read values that are not live.  */
    live = cc_op_live[s->cc_op] & ~USES_CC_SRCT;
930
    dead = live ^ (USES_CC_DST | USES_CC_SRC | USES_CC_SRC2);
931 932 933 934 935 936 937 938
    if (dead) {
        zero = tcg_const_tl(0);
        if (dead & USES_CC_DST) {
            dst = zero;
        }
        if (dead & USES_CC_SRC) {
            src1 = zero;
        }
939 940 941
        if (dead & USES_CC_SRC2) {
            src2 = zero;
        }
942 943
    }

944
    gen_update_cc_op(s);
945
    gen_helper_cc_compute_all(cpu_cc_src, dst, src1, src2, cpu_cc_op);
946
    set_cc_op(s, CC_OP_EFLAGS);
947 948 949 950

    if (dead) {
        tcg_temp_free(zero);
    }
951 952
}

953 954 955 956 957 958 959 960 961 962
typedef struct CCPrepare {
    TCGCond cond;
    TCGv reg;
    TCGv reg2;
    target_ulong imm;
    target_ulong mask;
    bool use_reg2;
    bool no_setcond;
} CCPrepare;

963
/* compute eflags.C to reg */
964
static CCPrepare gen_prepare_eflags_c(DisasContext *s, TCGv reg)
965 966
{
    TCGv t0, t1;
967
    int size, shift;
968 969 970

    switch (s->cc_op) {
    case CC_OP_SUBB ... CC_OP_SUBQ:
971
        /* (DATA_TYPE)CC_SRCT < (DATA_TYPE)CC_SRC */
972 973 974 975
        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;
976
        tcg_gen_mov_tl(t0, cpu_cc_srcT);
977 978 979 980 981 982 983 984 985
        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:
986 987
        return (CCPrepare) { .cond = TCG_COND_LTU, .reg = t0,
                             .reg2 = t1, .mask = -1, .use_reg2 = true };
988 989

    case CC_OP_LOGICB ... CC_OP_LOGICQ:
R
Richard Henderson 已提交
990
    case CC_OP_CLR:
991
        return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
992 993 994

    case CC_OP_INCB ... CC_OP_INCQ:
    case CC_OP_DECB ... CC_OP_DECQ:
995 996
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = -1, .no_setcond = true };
997 998 999 1000

    case CC_OP_SHLB ... CC_OP_SHLQ:
        /* (CC_SRC >> (DATA_BITS - 1)) & 1 */
        size = s->cc_op - CC_OP_SHLB;
1001 1002 1003
        shift = (8 << size) - 1;
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = (target_ulong)1 << shift };
1004 1005

    case CC_OP_MULB ... CC_OP_MULQ:
1006 1007
        return (CCPrepare) { .cond = TCG_COND_NE,
                             .reg = cpu_cc_src, .mask = -1 };
1008

1009 1010 1011 1012 1013
    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 };

1014 1015 1016 1017 1018
    case CC_OP_ADCX:
    case CC_OP_ADCOX:
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_dst,
                             .mask = -1, .no_setcond = true };

1019 1020 1021
    case CC_OP_EFLAGS:
    case CC_OP_SARB ... CC_OP_SARQ:
        /* CC_SRC & 1 */
1022 1023
        return (CCPrepare) { .cond = TCG_COND_NE,
                             .reg = cpu_cc_src, .mask = CC_C };
1024 1025 1026 1027 1028

    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);
1029 1030
       gen_helper_cc_compute_c(reg, cpu_cc_dst, cpu_cc_src,
                               cpu_cc_src2, cpu_cc_op);
1031 1032
       return (CCPrepare) { .cond = TCG_COND_NE, .reg = reg,
                            .mask = -1, .no_setcond = true };
1033 1034 1035
    }
}

1036
/* compute eflags.P to reg */
1037
static CCPrepare gen_prepare_eflags_p(DisasContext *s, TCGv reg)
1038
{
1039
    gen_compute_eflags(s);
1040 1041
    return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                         .mask = CC_P };
1042 1043 1044
}

/* compute eflags.S to reg */
1045
static CCPrepare gen_prepare_eflags_s(DisasContext *s, TCGv reg)
1046
{
1047 1048 1049 1050 1051
    switch (s->cc_op) {
    case CC_OP_DYNAMIC:
        gen_compute_eflags(s);
        /* FALLTHRU */
    case CC_OP_EFLAGS:
1052 1053 1054
    case CC_OP_ADCX:
    case CC_OP_ADOX:
    case CC_OP_ADCOX:
1055 1056
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = CC_S };
R
Richard Henderson 已提交
1057 1058
    case CC_OP_CLR:
        return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
1059 1060 1061 1062
    default:
        {
            int size = (s->cc_op - CC_OP_ADDB) & 3;
            TCGv t0 = gen_ext_tl(reg, cpu_cc_dst, size, true);
1063
            return (CCPrepare) { .cond = TCG_COND_LT, .reg = t0, .mask = -1 };
1064 1065
        }
    }
1066 1067 1068
}

/* compute eflags.O to reg */
1069
static CCPrepare gen_prepare_eflags_o(DisasContext *s, TCGv reg)
1070
{
1071 1072 1073 1074 1075
    switch (s->cc_op) {
    case CC_OP_ADOX:
    case CC_OP_ADCOX:
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src2,
                             .mask = -1, .no_setcond = true };
R
Richard Henderson 已提交
1076 1077
    case CC_OP_CLR:
        return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
1078 1079 1080 1081 1082
    default:
        gen_compute_eflags(s);
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = CC_O };
    }
1083 1084 1085
}

/* compute eflags.Z to reg */
1086
static CCPrepare gen_prepare_eflags_z(DisasContext *s, TCGv reg)
1087
{
1088 1089 1090 1091 1092
    switch (s->cc_op) {
    case CC_OP_DYNAMIC:
        gen_compute_eflags(s);
        /* FALLTHRU */
    case CC_OP_EFLAGS:
1093 1094 1095
    case CC_OP_ADCX:
    case CC_OP_ADOX:
    case CC_OP_ADCOX:
1096 1097
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = CC_Z };
R
Richard Henderson 已提交
1098 1099
    case CC_OP_CLR:
        return (CCPrepare) { .cond = TCG_COND_ALWAYS, .mask = -1 };
1100 1101 1102 1103
    default:
        {
            int size = (s->cc_op - CC_OP_ADDB) & 3;
            TCGv t0 = gen_ext_tl(reg, cpu_cc_dst, size, false);
1104
            return (CCPrepare) { .cond = TCG_COND_EQ, .reg = t0, .mask = -1 };
1105
        }
1106 1107 1108
    }
}

1109 1110
/* perform a conditional store into register 'reg' according to jump opcode
   value 'b'. In the fast case, T0 is guaranted not to be used. */
1111
static CCPrepare gen_prepare_cc(DisasContext *s, int b, TCGv reg)
1112
{
1113
    int inv, jcc_op, size, cond;
1114
    CCPrepare cc;
1115 1116 1117
    TCGv t0;

    inv = b & 1;
1118
    jcc_op = (b >> 1) & 7;
1119 1120

    switch (s->cc_op) {
1121 1122
    case CC_OP_SUBB ... CC_OP_SUBQ:
        /* We optimize relational operators for the cmp/jcc case.  */
1123 1124 1125
        size = s->cc_op - CC_OP_SUBB;
        switch (jcc_op) {
        case JCC_BE:
1126
            tcg_gen_mov_tl(cpu_tmp4, cpu_cc_srcT);
1127 1128
            gen_extu(size, cpu_tmp4);
            t0 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, false);
1129 1130
            cc = (CCPrepare) { .cond = TCG_COND_LEU, .reg = cpu_tmp4,
                               .reg2 = t0, .mask = -1, .use_reg2 = true };
1131
            break;
1132

1133
        case JCC_L:
1134
            cond = TCG_COND_LT;
1135 1136
            goto fast_jcc_l;
        case JCC_LE:
1137
            cond = TCG_COND_LE;
1138
        fast_jcc_l:
1139
            tcg_gen_mov_tl(cpu_tmp4, cpu_cc_srcT);
1140 1141
            gen_exts(size, cpu_tmp4);
            t0 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, true);
1142 1143
            cc = (CCPrepare) { .cond = cond, .reg = cpu_tmp4,
                               .reg2 = t0, .mask = -1, .use_reg2 = true };
1144
            break;
1145

1146
        default:
1147
            goto slow_jcc;
1148
        }
1149
        break;
1150

1151 1152
    default:
    slow_jcc:
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 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
        /* 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;
        }
1197
        break;
1198
    }
1199 1200 1201 1202 1203

    if (inv) {
        cc.cond = tcg_invert_cond(cc.cond);
    }
    return cc;
1204 1205
}

1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
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);
}
1240

1241 1242
/* generate a conditional jump to label 'l1' according to jump opcode
   value 'b'. In the fast case, T0 is guaranted not to be used. */
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
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.  */
1261
static inline void gen_jcc1(DisasContext *s, int b, int l1)
1262
{
1263
    CCPrepare cc = gen_prepare_cc(s, b, cpu_T[0]);
1264

1265
    gen_update_cc_op(s);
1266 1267 1268 1269
    if (cc.mask != -1) {
        tcg_gen_andi_tl(cpu_T[0], cc.reg, cc.mask);
        cc.reg = cpu_T[0];
    }
1270
    set_cc_op(s, CC_OP_DYNAMIC);
1271 1272 1273 1274
    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);
1275 1276 1277
    }
}

B
bellard 已提交
1278 1279 1280
/* 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 已提交
1281
{
B
bellard 已提交
1282 1283 1284 1285
    int l1, l2;

    l1 = gen_new_label();
    l2 = gen_new_label();
1286
    gen_op_jnz_ecx(s->aflag, l1);
B
bellard 已提交
1287 1288 1289 1290
    gen_set_label(l2);
    gen_jmp_tb(s, next_eip, 1);
    gen_set_label(l1);
    return l2;
B
bellard 已提交
1291 1292 1293 1294
}

static inline void gen_stos(DisasContext *s, int ot)
{
B
bellard 已提交
1295
    gen_op_mov_TN_reg(OT_LONG, 0, R_EAX);
B
bellard 已提交
1296
    gen_string_movl_A0_EDI(s);
B
bellard 已提交
1297
    gen_op_st_T0_A0(ot + s->mem_index);
1298 1299
    gen_op_movl_T0_Dshift(ot);
    gen_op_add_reg_T0(s->aflag, R_EDI);
B
bellard 已提交
1300 1301 1302 1303 1304
}

static inline void gen_lods(DisasContext *s, int ot)
{
    gen_string_movl_A0_ESI(s);
B
bellard 已提交
1305 1306
    gen_op_ld_T0_A0(ot + s->mem_index);
    gen_op_mov_reg_T0(ot, R_EAX);
1307 1308
    gen_op_movl_T0_Dshift(ot);
    gen_op_add_reg_T0(s->aflag, R_ESI);
B
bellard 已提交
1309 1310 1311 1312 1313
}

static inline void gen_scas(DisasContext *s, int ot)
{
    gen_string_movl_A0_EDI(s);
B
bellard 已提交
1314
    gen_op_ld_T1_A0(ot + s->mem_index);
1315
    gen_op(s, OP_CMPL, ot, R_EAX);
1316 1317
    gen_op_movl_T0_Dshift(ot);
    gen_op_add_reg_T0(s->aflag, R_EDI);
B
bellard 已提交
1318 1319 1320 1321 1322
}

static inline void gen_cmps(DisasContext *s, int ot)
{
    gen_string_movl_A0_EDI(s);
B
bellard 已提交
1323
    gen_op_ld_T1_A0(ot + s->mem_index);
1324 1325
    gen_string_movl_A0_ESI(s);
    gen_op(s, OP_CMPL, ot, OR_TMP0);
1326 1327 1328
    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 已提交
1329 1330 1331 1332
}

static inline void gen_ins(DisasContext *s, int ot)
{
P
pbrook 已提交
1333 1334
    if (use_icount)
        gen_io_start();
B
bellard 已提交
1335
    gen_string_movl_A0_EDI(s);
1336 1337
    /* Note: we must do this dummy write first to be restartable in
       case of page fault. */
B
bellard 已提交
1338
    gen_op_movl_T0_0();
B
bellard 已提交
1339
    gen_op_st_T0_A0(ot + s->mem_index);
1340
    gen_op_mov_TN_reg(OT_WORD, 1, R_EDX);
1341 1342
    tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[1]);
    tcg_gen_andi_i32(cpu_tmp2_i32, cpu_tmp2_i32, 0xffff);
P
pbrook 已提交
1343
    gen_helper_in_func(ot, cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
1344
    gen_op_st_T0_A0(ot + s->mem_index);
1345 1346
    gen_op_movl_T0_Dshift(ot);
    gen_op_add_reg_T0(s->aflag, R_EDI);
P
pbrook 已提交
1347 1348
    if (use_icount)
        gen_io_end();
B
bellard 已提交
1349 1350 1351 1352
}

static inline void gen_outs(DisasContext *s, int ot)
{
P
pbrook 已提交
1353 1354
    if (use_icount)
        gen_io_start();
B
bellard 已提交
1355
    gen_string_movl_A0_ESI(s);
B
bellard 已提交
1356
    gen_op_ld_T0_A0(ot + s->mem_index);
1357 1358

    gen_op_mov_TN_reg(OT_WORD, 1, R_EDX);
1359 1360 1361
    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 已提交
1362
    gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
1363

1364 1365
    gen_op_movl_T0_Dshift(ot);
    gen_op_add_reg_T0(s->aflag, R_ESI);
P
pbrook 已提交
1366 1367
    if (use_icount)
        gen_io_end();
B
bellard 已提交
1368 1369 1370 1371 1372 1373
}

/* 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 已提交
1374
                                 target_ulong cur_eip, target_ulong next_eip) \
B
bellard 已提交
1375
{                                                                             \
B
bellard 已提交
1376
    int l2;\
B
bellard 已提交
1377
    gen_update_cc_op(s);                                                      \
B
bellard 已提交
1378
    l2 = gen_jz_ecx_string(s, next_eip);                                      \
B
bellard 已提交
1379
    gen_ ## op(s, ot);                                                        \
1380
    gen_op_add_reg_im(s->aflag, R_ECX, -1);                                   \
B
bellard 已提交
1381 1382 1383
    /* a loop would cause two single step exceptions if ECX = 1               \
       before rep string_insn */                                              \
    if (!s->jmp_opt)                                                          \
1384
        gen_op_jz_ecx(s->aflag, l2);                                          \
B
bellard 已提交
1385 1386 1387 1388 1389
    gen_jmp(s, cur_eip);                                                      \
}

#define GEN_REPZ2(op)                                                         \
static inline void gen_repz_ ## op(DisasContext *s, int ot,                   \
B
bellard 已提交
1390 1391
                                   target_ulong cur_eip,                      \
                                   target_ulong next_eip,                     \
B
bellard 已提交
1392 1393
                                   int nz)                                    \
{                                                                             \
B
bellard 已提交
1394
    int l2;\
B
bellard 已提交
1395
    gen_update_cc_op(s);                                                      \
B
bellard 已提交
1396
    l2 = gen_jz_ecx_string(s, next_eip);                                      \
B
bellard 已提交
1397
    gen_ ## op(s, ot);                                                        \
1398
    gen_op_add_reg_im(s->aflag, R_ECX, -1);                                   \
1399
    gen_update_cc_op(s);                                                      \
1400
    gen_jcc1(s, (JCC_Z << 1) | (nz ^ 1), l2);                                 \
B
bellard 已提交
1401
    if (!s->jmp_opt)                                                          \
1402
        gen_op_jz_ecx(s->aflag, l2);                                          \
B
bellard 已提交
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
    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 已提交
1414 1415 1416
static void gen_helper_fp_arith_ST0_FT0(int op)
{
    switch (op) {
B
Blue Swirl 已提交
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
    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 已提交
1441 1442
    }
}
B
bellard 已提交
1443 1444

/* NOTE the exception in "r" op ordering */
P
pbrook 已提交
1445 1446 1447 1448
static void gen_helper_fp_arith_STN_ST0(int op, int opreg)
{
    TCGv_i32 tmp = tcg_const_i32(opreg);
    switch (op) {
B
Blue Swirl 已提交
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
    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 已提交
1467 1468
    }
}
B
bellard 已提交
1469 1470 1471 1472 1473

/* 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 已提交
1474
        gen_op_mov_TN_reg(ot, 0, d);
B
bellard 已提交
1475
    } else {
B
bellard 已提交
1476
        gen_op_ld_T0_A0(ot + s1->mem_index);
B
bellard 已提交
1477 1478 1479
    }
    switch(op) {
    case OP_ADCL:
1480
        gen_compute_eflags_c(s1, cpu_tmp4);
B
bellard 已提交
1481 1482 1483 1484 1485 1486
        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);
1487 1488
        gen_op_update3_cc(cpu_tmp4);
        set_cc_op(s1, CC_OP_ADCB + ot);
B
bellard 已提交
1489
        break;
B
bellard 已提交
1490
    case OP_SBBL:
1491
        gen_compute_eflags_c(s1, cpu_tmp4);
B
bellard 已提交
1492 1493 1494
        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 已提交
1495
            gen_op_mov_reg_T0(ot, d);
B
bellard 已提交
1496 1497
        else
            gen_op_st_T0_A0(ot + s1->mem_index);
1498 1499
        gen_op_update3_cc(cpu_tmp4);
        set_cc_op(s1, CC_OP_SBBB + ot);
B
bellard 已提交
1500
        break;
B
bellard 已提交
1501 1502
    case OP_ADDL:
        gen_op_addl_T0_T1();
B
bellard 已提交
1503 1504 1505 1506 1507
        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();
1508
        set_cc_op(s1, CC_OP_ADDB + ot);
B
bellard 已提交
1509 1510
        break;
    case OP_SUBL:
1511
        tcg_gen_mov_tl(cpu_cc_srcT, cpu_T[0]);
B
bellard 已提交
1512
        tcg_gen_sub_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_update2_cc();
1518
        set_cc_op(s1, CC_OP_SUBB + ot);
B
bellard 已提交
1519 1520 1521
        break;
    default:
    case OP_ANDL:
B
bellard 已提交
1522
        tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
B
bellard 已提交
1523 1524 1525 1526 1527
        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();
1528
        set_cc_op(s1, CC_OP_LOGICB + ot);
B
bellard 已提交
1529
        break;
B
bellard 已提交
1530
    case OP_ORL:
B
bellard 已提交
1531
        tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
B
bellard 已提交
1532 1533 1534 1535 1536
        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();
1537
        set_cc_op(s1, CC_OP_LOGICB + ot);
B
bellard 已提交
1538
        break;
B
bellard 已提交
1539
    case OP_XORL:
B
bellard 已提交
1540
        tcg_gen_xor_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
B
bellard 已提交
1541 1542 1543 1544 1545
        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();
1546
        set_cc_op(s1, CC_OP_LOGICB + ot);
B
bellard 已提交
1547 1548
        break;
    case OP_CMPL:
1549
        tcg_gen_mov_tl(cpu_cc_src, cpu_T[1]);
1550
        tcg_gen_mov_tl(cpu_cc_srcT, cpu_T[0]);
1551
        tcg_gen_sub_tl(cpu_cc_dst, cpu_T[0], cpu_T[1]);
1552
        set_cc_op(s1, CC_OP_SUBB + ot);
B
bellard 已提交
1553 1554
        break;
    }
1555 1556
}

B
bellard 已提交
1557 1558 1559 1560
/* 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 已提交
1561
        gen_op_mov_TN_reg(ot, 0, d);
B
bellard 已提交
1562
    else
B
bellard 已提交
1563
        gen_op_ld_T0_A0(ot + s1->mem_index);
1564
    gen_compute_eflags_c(s1, cpu_cc_src);
B
bellard 已提交
1565
    if (c > 0) {
1566
        tcg_gen_addi_tl(cpu_T[0], cpu_T[0], 1);
1567
        set_cc_op(s1, CC_OP_INCB + ot);
B
bellard 已提交
1568
    } else {
1569
        tcg_gen_addi_tl(cpu_T[0], cpu_T[0], -1);
1570
        set_cc_op(s1, CC_OP_DECB + ot);
B
bellard 已提交
1571 1572
    }
    if (d != OR_TMP0)
B
bellard 已提交
1573
        gen_op_mov_reg_T0(ot, d);
B
bellard 已提交
1574
    else
B
bellard 已提交
1575
        gen_op_st_T0_A0(ot + s1->mem_index);
B
bellard 已提交
1576
    tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
B
bellard 已提交
1577 1578
}

1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623
static void gen_shift_flags(DisasContext *s, int ot, TCGv result, TCGv shm1,
                            TCGv count, bool is_right)
{
    TCGv_i32 z32, s32, oldop;
    TCGv z_tl;

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

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

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

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

1624 1625
static void gen_shift_rm_T1(DisasContext *s, int ot, int op1, 
                            int is_right, int is_arith)
B
bellard 已提交
1626
{
1627
    target_ulong mask = (ot == OT_QUAD ? 0x3f : 0x1f);
1628

1629
    /* load */
1630
    if (op1 == OR_TMP0) {
1631
        gen_op_ld_T0_A0(ot + s->mem_index);
1632
    } else {
1633
        gen_op_mov_TN_reg(ot, 0, op1);
1634
    }
1635

1636 1637
    tcg_gen_andi_tl(cpu_T[1], cpu_T[1], mask);
    tcg_gen_subi_tl(cpu_tmp0, cpu_T[1], 1);
1638 1639 1640

    if (is_right) {
        if (is_arith) {
B
bellard 已提交
1641
            gen_exts(ot, cpu_T[0]);
1642 1643
            tcg_gen_sar_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
            tcg_gen_sar_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1644
        } else {
B
bellard 已提交
1645
            gen_extu(ot, cpu_T[0]);
1646 1647
            tcg_gen_shr_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
            tcg_gen_shr_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1648 1649
        }
    } else {
1650 1651
        tcg_gen_shl_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
        tcg_gen_shl_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1652 1653 1654
    }

    /* store */
1655
    if (op1 == OR_TMP0) {
1656
        gen_op_st_T0_A0(ot + s->mem_index);
1657
    } else {
1658
        gen_op_mov_reg_T0(ot, op1);
1659 1660
    }

1661
    gen_shift_flags(s, ot, cpu_T[0], cpu_tmp0, cpu_T[1], is_right);
1662 1663
}

B
bellard 已提交
1664 1665 1666
static void gen_shift_rm_im(DisasContext *s, int ot, int op1, int op2,
                            int is_right, int is_arith)
{
1667
    int mask = (ot == OT_QUAD ? 0x3f : 0x1f);
B
bellard 已提交
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679

    /* 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 已提交
1680
                tcg_gen_sari_tl(cpu_tmp4, cpu_T[0], op2 - 1);
B
bellard 已提交
1681 1682 1683
                tcg_gen_sari_tl(cpu_T[0], cpu_T[0], op2);
            } else {
                gen_extu(ot, cpu_T[0]);
B
bellard 已提交
1684
                tcg_gen_shri_tl(cpu_tmp4, cpu_T[0], op2 - 1);
B
bellard 已提交
1685 1686 1687
                tcg_gen_shri_tl(cpu_T[0], cpu_T[0], op2);
            }
        } else {
B
bellard 已提交
1688
            tcg_gen_shli_tl(cpu_tmp4, cpu_T[0], op2 - 1);
B
bellard 已提交
1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700
            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 已提交
1701
        tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
B
bellard 已提交
1702
        tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
1703
        set_cc_op(s, (is_right ? CC_OP_SARB : CC_OP_SHLB) + ot);
B
bellard 已提交
1704 1705 1706
    }
}

1707 1708 1709 1710 1711 1712 1713 1714
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);
}

1715
static void gen_rot_rm_T1(DisasContext *s, int ot, int op1, int is_right)
1716
{
1717 1718
    target_ulong mask = (ot == OT_QUAD ? 0x3f : 0x1f);
    TCGv_i32 t0, t1;
1719 1720

    /* load */
1721
    if (op1 == OR_TMP0) {
1722
        gen_op_ld_T0_A0(ot + s->mem_index);
1723
    } else {
1724
        gen_op_mov_TN_reg(ot, 0, op1);
1725
    }
1726

1727
    tcg_gen_andi_tl(cpu_T[1], cpu_T[1], mask);
1728

1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758
    switch (ot) {
    case OT_BYTE:
        /* Replicate the 8-bit input so that a 32-bit rotate works.  */
        tcg_gen_ext8u_tl(cpu_T[0], cpu_T[0]);
        tcg_gen_muli_tl(cpu_T[0], cpu_T[0], 0x01010101);
        goto do_long;
    case OT_WORD:
        /* Replicate the 16-bit input so that a 32-bit rotate works.  */
        tcg_gen_deposit_tl(cpu_T[0], cpu_T[0], cpu_T[0], 16, 16);
        goto do_long;
    do_long:
#ifdef TARGET_X86_64
    case OT_LONG:
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
        tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
        if (is_right) {
            tcg_gen_rotr_i32(cpu_tmp2_i32, cpu_tmp2_i32, cpu_tmp3_i32);
        } else {
            tcg_gen_rotl_i32(cpu_tmp2_i32, cpu_tmp2_i32, cpu_tmp3_i32);
        }
        tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
        break;
#endif
    default:
        if (is_right) {
            tcg_gen_rotr_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
        } else {
            tcg_gen_rotl_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
        }
        break;
1759 1760 1761
    }

    /* store */
1762
    if (op1 == OR_TMP0) {
1763
        gen_op_st_T0_A0(ot + s->mem_index);
1764
    } else {
1765
        gen_op_mov_reg_T0(ot, op1);
1766
    }
1767

1768 1769
    /* We'll need the flags computed into CC_SRC.  */
    gen_compute_eflags(s);
1770

1771 1772 1773 1774
    /* The value that was "rotated out" is now present at the other end
       of the word.  Compute C into CC_DST and O into CC_SRC2.  Note that
       since we've computed the flags into CC_SRC, these variables are
       currently dead.  */
1775
    if (is_right) {
1776 1777
        tcg_gen_shri_tl(cpu_cc_src2, cpu_T[0], mask - 1);
        tcg_gen_shri_tl(cpu_cc_dst, cpu_T[0], mask);
P
Pavel Dovgaluk 已提交
1778
        tcg_gen_andi_tl(cpu_cc_dst, cpu_cc_dst, 1);
1779 1780 1781
    } else {
        tcg_gen_shri_tl(cpu_cc_src2, cpu_T[0], mask);
        tcg_gen_andi_tl(cpu_cc_dst, cpu_T[0], 1);
1782
    }
1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801
    tcg_gen_andi_tl(cpu_cc_src2, cpu_cc_src2, 1);
    tcg_gen_xor_tl(cpu_cc_src2, cpu_cc_src2, cpu_cc_dst);

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

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

M
malc 已提交
1804 1805 1806
static void gen_rot_rm_im(DisasContext *s, int ot, int op1, int op2,
                          int is_right)
{
1807 1808
    int mask = (ot == OT_QUAD ? 0x3f : 0x1f);
    int shift;
M
malc 已提交
1809 1810 1811

    /* load */
    if (op1 == OR_TMP0) {
1812
        gen_op_ld_T0_A0(ot + s->mem_index);
M
malc 已提交
1813
    } else {
1814
        gen_op_mov_TN_reg(ot, 0, op1);
M
malc 已提交
1815 1816 1817 1818
    }

    op2 &= mask;
    if (op2 != 0) {
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852
        switch (ot) {
#ifdef TARGET_X86_64
        case OT_LONG:
            tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
            if (is_right) {
                tcg_gen_rotri_i32(cpu_tmp2_i32, cpu_tmp2_i32, op2);
            } else {
                tcg_gen_rotli_i32(cpu_tmp2_i32, cpu_tmp2_i32, op2);
            }
            tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
            break;
#endif
        default:
            if (is_right) {
                tcg_gen_rotri_tl(cpu_T[0], cpu_T[0], op2);
            } else {
                tcg_gen_rotli_tl(cpu_T[0], cpu_T[0], op2);
            }
            break;
        case OT_BYTE:
            mask = 7;
            goto do_shifts;
        case OT_WORD:
            mask = 15;
        do_shifts:
            shift = op2 & mask;
            if (is_right) {
                shift = mask + 1 - shift;
            }
            gen_extu(ot, cpu_T[0]);
            tcg_gen_shli_tl(cpu_tmp0, cpu_T[0], shift);
            tcg_gen_shri_tl(cpu_T[0], cpu_T[0], mask + 1 - shift);
            tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
            break;
M
malc 已提交
1853 1854 1855 1856 1857
        }
    }

    /* store */
    if (op1 == OR_TMP0) {
1858
        gen_op_st_T0_A0(ot + s->mem_index);
M
malc 已提交
1859
    } else {
1860
        gen_op_mov_reg_T0(ot, op1);
M
malc 已提交
1861 1862 1863
    }

    if (op2 != 0) {
1864
        /* Compute the flags into CC_SRC.  */
1865
        gen_compute_eflags(s);
1866

1867 1868 1869 1870
        /* The value that was "rotated out" is now present at the other end
           of the word.  Compute C into CC_DST and O into CC_SRC2.  Note that
           since we've computed the flags into CC_SRC, these variables are
           currently dead.  */
M
malc 已提交
1871
        if (is_right) {
1872 1873 1874 1875 1876
            tcg_gen_shri_tl(cpu_cc_src2, cpu_T[0], mask - 1);
            tcg_gen_shri_tl(cpu_cc_dst, cpu_T[0], mask);
        } else {
            tcg_gen_shri_tl(cpu_cc_src2, cpu_T[0], mask);
            tcg_gen_andi_tl(cpu_cc_dst, cpu_T[0], 1);
M
malc 已提交
1877
        }
1878 1879 1880
        tcg_gen_andi_tl(cpu_cc_src2, cpu_cc_src2, 1);
        tcg_gen_xor_tl(cpu_cc_src2, cpu_cc_src2, cpu_cc_dst);
        set_cc_op(s, CC_OP_ADCOX);
M
malc 已提交
1881 1882 1883
    }
}

1884 1885 1886 1887
/* XXX: add faster immediate = 1 case */
static void gen_rotc_rm_T1(DisasContext *s, int ot, int op1, 
                           int is_right)
{
1888
    gen_compute_eflags(s);
1889
    assert(s->cc_op == CC_OP_EFLAGS);
1890 1891 1892 1893 1894 1895 1896

    /* 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 已提交
1897 1898
    if (is_right) {
        switch (ot) {
1899
        case OT_BYTE:
1900 1901
            gen_helper_rcrb(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
1902
        case OT_WORD:
1903 1904
            gen_helper_rcrw(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
1905
        case OT_LONG:
1906 1907
            gen_helper_rcrl(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1908
#ifdef TARGET_X86_64
1909
        case OT_QUAD:
1910 1911
            gen_helper_rcrq(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1912 1913 1914 1915
#endif
        }
    } else {
        switch (ot) {
1916
        case OT_BYTE:
1917 1918
            gen_helper_rclb(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
1919
        case OT_WORD:
1920 1921
            gen_helper_rclw(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
1922
        case OT_LONG:
1923 1924
            gen_helper_rcll(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1925
#ifdef TARGET_X86_64
1926
        case OT_QUAD:
1927 1928
            gen_helper_rclq(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1929 1930 1931
#endif
        }
    }
1932 1933 1934 1935 1936 1937 1938 1939
    /* 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 已提交
1940
static void gen_shiftd_rm_T1(DisasContext *s, int ot, int op1,
1941
                             bool is_right, TCGv count_in)
1942
{
1943 1944
    target_ulong mask = (ot == OT_QUAD ? 63 : 31);
    TCGv count;
1945 1946

    /* load */
1947
    if (op1 == OR_TMP0) {
1948
        gen_op_ld_T0_A0(ot + s->mem_index);
1949
    } else {
1950
        gen_op_mov_TN_reg(ot, 0, op1);
1951
    }
1952

1953 1954
    count = tcg_temp_new();
    tcg_gen_andi_tl(count, count_in, mask);
1955

1956 1957 1958 1959 1960
    switch (ot) {
    case OT_WORD:
        /* Note: we implement the Intel behaviour for shift count > 16.
           This means "shrdw C, B, A" shifts A:B:A >> C.  Build the B:A
           portion by constructing it as a 32-bit value.  */
1961
        if (is_right) {
1962 1963 1964
            tcg_gen_deposit_tl(cpu_tmp0, cpu_T[0], cpu_T[1], 16, 16);
            tcg_gen_mov_tl(cpu_T[1], cpu_T[0]);
            tcg_gen_mov_tl(cpu_T[0], cpu_tmp0);
1965
        } else {
1966
            tcg_gen_deposit_tl(cpu_T[1], cpu_T[0], cpu_T[1], 16, 16);
1967
        }
1968 1969 1970 1971 1972
        /* FALLTHRU */
#ifdef TARGET_X86_64
    case OT_LONG:
        /* Concatenate the two 32-bit values and use a 64-bit shift.  */
        tcg_gen_subi_tl(cpu_tmp0, count, 1);
1973
        if (is_right) {
1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989
            tcg_gen_concat_tl_i64(cpu_T[0], cpu_T[0], cpu_T[1]);
            tcg_gen_shr_i64(cpu_tmp0, cpu_T[0], cpu_tmp0);
            tcg_gen_shr_i64(cpu_T[0], cpu_T[0], count);
        } else {
            tcg_gen_concat_tl_i64(cpu_T[0], cpu_T[1], cpu_T[0]);
            tcg_gen_shl_i64(cpu_tmp0, cpu_T[0], cpu_tmp0);
            tcg_gen_shl_i64(cpu_T[0], cpu_T[0], count);
            tcg_gen_shri_i64(cpu_tmp0, cpu_tmp0, 32);
            tcg_gen_shri_i64(cpu_T[0], cpu_T[0], 32);
        }
        break;
#endif
    default:
        tcg_gen_subi_tl(cpu_tmp0, count, 1);
        if (is_right) {
            tcg_gen_shr_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
1990

1991 1992 1993
            tcg_gen_subfi_tl(cpu_tmp4, mask + 1, count);
            tcg_gen_shr_tl(cpu_T[0], cpu_T[0], count);
            tcg_gen_shl_tl(cpu_T[1], cpu_T[1], cpu_tmp4);
1994
        } else {
1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005
            tcg_gen_shl_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
            if (ot == OT_WORD) {
                /* Only needed if count > 16, for Intel behaviour.  */
                tcg_gen_subfi_tl(cpu_tmp4, 33, count);
                tcg_gen_shr_tl(cpu_tmp4, cpu_T[1], cpu_tmp4);
                tcg_gen_or_tl(cpu_tmp0, cpu_tmp0, cpu_tmp4);
            }

            tcg_gen_subfi_tl(cpu_tmp4, mask + 1, count);
            tcg_gen_shl_tl(cpu_T[0], cpu_T[0], count);
            tcg_gen_shr_tl(cpu_T[1], cpu_T[1], cpu_tmp4);
2006
        }
2007 2008 2009 2010 2011
        tcg_gen_movi_tl(cpu_tmp4, 0);
        tcg_gen_movcond_tl(TCG_COND_EQ, cpu_T[1], count, cpu_tmp4,
                           cpu_tmp4, cpu_T[1]);
        tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
        break;
2012 2013 2014
    }

    /* store */
2015
    if (op1 == OR_TMP0) {
2016
        gen_op_st_T0_A0(ot + s->mem_index);
2017
    } else {
2018
        gen_op_mov_reg_T0(ot, op1);
2019
    }
2020

2021 2022
    gen_shift_flags(s, ot, cpu_T[0], cpu_tmp0, count, is_right);
    tcg_temp_free(count);
2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052
}

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 已提交
2053 2054 2055 2056
}

static void gen_shifti(DisasContext *s1, int op, int ot, int d, int c)
{
B
bellard 已提交
2057
    switch(op) {
M
malc 已提交
2058 2059 2060 2061 2062 2063
    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 已提交
2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079
    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 已提交
2080 2081
}

2082 2083
static void gen_lea_modrm(CPUX86State *env, DisasContext *s, int modrm,
                          int *reg_ptr, int *offset_ptr)
B
bellard 已提交
2084
{
B
bellard 已提交
2085
    target_long disp;
B
bellard 已提交
2086
    int havesib;
B
bellard 已提交
2087
    int base;
B
bellard 已提交
2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105
    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;
2106

B
bellard 已提交
2107 2108
        if (base == 4) {
            havesib = 1;
2109
            code = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
2110
            scale = (code >> 6) & 3;
B
bellard 已提交
2111 2112
            index = ((code >> 3) & 7) | REX_X(s);
            base = (code & 7);
B
bellard 已提交
2113
        }
B
bellard 已提交
2114
        base |= REX_B(s);
B
bellard 已提交
2115 2116 2117

        switch (mod) {
        case 0:
B
bellard 已提交
2118
            if ((base & 7) == 5) {
B
bellard 已提交
2119
                base = -1;
2120
                disp = (int32_t)cpu_ldl_code(env, s->pc);
B
bellard 已提交
2121
                s->pc += 4;
B
bellard 已提交
2122 2123 2124
                if (CODE64(s) && !havesib) {
                    disp += s->pc + s->rip_offset;
                }
B
bellard 已提交
2125 2126 2127 2128 2129
            } else {
                disp = 0;
            }
            break;
        case 1:
2130
            disp = (int8_t)cpu_ldub_code(env, s->pc++);
B
bellard 已提交
2131 2132 2133
            break;
        default:
        case 2:
2134
            disp = (int32_t)cpu_ldl_code(env, s->pc);
B
bellard 已提交
2135 2136 2137
            s->pc += 4;
            break;
        }
2138

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

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

2267
static void gen_nop_modrm(CPUX86State *env, DisasContext *s, int modrm)
B
bellard 已提交
2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278
{
    int mod, rm, base, code;

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

    if (s->aflag) {

        base = rm;
2279

B
bellard 已提交
2280
        if (base == 4) {
2281
            code = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
2282 2283
            base = (code & 7);
        }
2284

B
bellard 已提交
2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316
        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 已提交
2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327
/* 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) {
2328 2329
#ifdef TARGET_X86_64
        if (CODE64(s)) {
B
bellard 已提交
2330
            gen_op_addq_A0_seg(override);
2331
        } else
2332 2333
#endif
        {
2334
            gen_op_addl_A0_seg(s, override);
2335
        }
B
bellard 已提交
2336 2337 2338
    }
}

B
balrog 已提交
2339
/* generate modrm memory load or store of 'reg'. TMP0 is used if reg ==
B
bellard 已提交
2340
   OR_TMP0 */
2341 2342
static void gen_ldst_modrm(CPUX86State *env, DisasContext *s, int modrm,
                           int ot, int reg, int is_store)
B
bellard 已提交
2343 2344 2345 2346
{
    int mod, rm, opreg, disp;

    mod = (modrm >> 6) & 3;
B
bellard 已提交
2347
    rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
2348 2349 2350
    if (mod == 3) {
        if (is_store) {
            if (reg != OR_TMP0)
B
bellard 已提交
2351 2352
                gen_op_mov_TN_reg(ot, 0, reg);
            gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
2353
        } else {
B
bellard 已提交
2354
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
2355
            if (reg != OR_TMP0)
B
bellard 已提交
2356
                gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
2357 2358
        }
    } else {
2359
        gen_lea_modrm(env, s, modrm, &opreg, &disp);
B
bellard 已提交
2360 2361
        if (is_store) {
            if (reg != OR_TMP0)
B
bellard 已提交
2362 2363
                gen_op_mov_TN_reg(ot, 0, reg);
            gen_op_st_T0_A0(ot + s->mem_index);
B
bellard 已提交
2364
        } else {
B
bellard 已提交
2365
            gen_op_ld_T0_A0(ot + s->mem_index);
B
bellard 已提交
2366
            if (reg != OR_TMP0)
B
bellard 已提交
2367
                gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
2368 2369 2370 2371
        }
    }
}

2372
static inline uint32_t insn_get(CPUX86State *env, DisasContext *s, int ot)
B
bellard 已提交
2373 2374 2375 2376 2377
{
    uint32_t ret;

    switch(ot) {
    case OT_BYTE:
2378
        ret = cpu_ldub_code(env, s->pc);
B
bellard 已提交
2379 2380 2381
        s->pc++;
        break;
    case OT_WORD:
2382
        ret = cpu_lduw_code(env, s->pc);
B
bellard 已提交
2383 2384 2385 2386
        s->pc += 2;
        break;
    default:
    case OT_LONG:
2387
        ret = cpu_ldl_code(env, s->pc);
B
bellard 已提交
2388 2389 2390 2391 2392 2393
        s->pc += 4;
        break;
    }
    return ret;
}

B
bellard 已提交
2394 2395 2396 2397 2398 2399 2400 2401
static inline int insn_const_size(unsigned int ot)
{
    if (ot <= OT_LONG)
        return 1 << ot;
    else
        return 4;
}

2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412
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 已提交
2413
        tcg_gen_goto_tb(tb_num);
2414
        gen_jmp_im(eip);
2415
        tcg_gen_exit_tb((tcg_target_long)tb + tb_num);
2416 2417 2418 2419 2420 2421 2422
    } else {
        /* jump to another page: currently not optimized */
        gen_jmp_im(eip);
        gen_eob(s);
    }
}

2423
static inline void gen_jcc(DisasContext *s, int b,
B
bellard 已提交
2424
                           target_ulong val, target_ulong next_eip)
B
bellard 已提交
2425
{
2426
    int l1, l2;
2427

B
bellard 已提交
2428
    if (s->jmp_opt) {
B
bellard 已提交
2429
        l1 = gen_new_label();
2430
        gen_jcc1(s, b, l1);
2431

2432
        gen_goto_tb(s, 0, next_eip);
B
bellard 已提交
2433 2434

        gen_set_label(l1);
2435
        gen_goto_tb(s, 1, val);
J
Jun Koi 已提交
2436
        s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2437
    } else {
B
bellard 已提交
2438 2439
        l1 = gen_new_label();
        l2 = gen_new_label();
2440
        gen_jcc1(s, b, l1);
2441

B
bellard 已提交
2442
        gen_jmp_im(next_eip);
2443 2444
        tcg_gen_br(l2);

B
bellard 已提交
2445 2446 2447
        gen_set_label(l1);
        gen_jmp_im(val);
        gen_set_label(l2);
B
bellard 已提交
2448 2449 2450 2451
        gen_eob(s);
    }
}

2452 2453 2454
static void gen_cmovcc1(CPUX86State *env, DisasContext *s, int ot, int b,
                        int modrm, int reg)
{
2455
    CCPrepare cc;
2456

2457
    gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
2458

2459 2460 2461 2462 2463 2464 2465 2466
    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);
2467 2468
    }

2469 2470 2471 2472 2473 2474 2475 2476 2477 2478
    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);
    }
2479 2480
}

2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496
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 已提交
2497 2498
/* move T0 to seg_reg and compute if the CPU state may change. Never
   call this function with seg_reg == R_CS */
B
bellard 已提交
2499
static void gen_movl_seg_T0(DisasContext *s, int seg_reg, target_ulong cur_eip)
B
bellard 已提交
2500
{
2501 2502
    if (s->pe && !s->vm86) {
        /* XXX: optimize by finding processor state dynamically */
2503
        gen_update_cc_op(s);
B
bellard 已提交
2504
        gen_jmp_im(cur_eip);
2505
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
2506
        gen_helper_load_seg(cpu_env, tcg_const_i32(seg_reg), cpu_tmp2_i32);
B
bellard 已提交
2507 2508 2509 2510 2511
        /* 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 已提交
2512
            s->is_jmp = DISAS_TB_JUMP;
2513
    } else {
2514
        gen_op_movl_seg_T0_vm(seg_reg);
B
bellard 已提交
2515
        if (seg_reg == R_SS)
J
Jun Koi 已提交
2516
            s->is_jmp = DISAS_TB_JUMP;
2517
    }
B
bellard 已提交
2518 2519
}

T
ths 已提交
2520 2521 2522 2523 2524
static inline int svm_is_rep(int prefixes)
{
    return ((prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) ? 8 : 0);
}

B
bellard 已提交
2525
static inline void
T
ths 已提交
2526
gen_svm_check_intercept_param(DisasContext *s, target_ulong pc_start,
2527
                              uint32_t type, uint64_t param)
T
ths 已提交
2528
{
B
bellard 已提交
2529 2530 2531
    /* no SVM activated; fast case */
    if (likely(!(s->flags & HF_SVMI_MASK)))
        return;
2532
    gen_update_cc_op(s);
B
bellard 已提交
2533
    gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
2534
    gen_helper_svm_check_intercept_param(cpu_env, tcg_const_i32(type),
P
pbrook 已提交
2535
                                         tcg_const_i64(param));
T
ths 已提交
2536 2537
}

B
bellard 已提交
2538
static inline void
T
ths 已提交
2539 2540
gen_svm_check_intercept(DisasContext *s, target_ulong pc_start, uint64_t type)
{
B
bellard 已提交
2541
    gen_svm_check_intercept_param(s, pc_start, type, 0);
T
ths 已提交
2542 2543
}

2544 2545
static inline void gen_stack_update(DisasContext *s, int addend)
{
B
bellard 已提交
2546 2547
#ifdef TARGET_X86_64
    if (CODE64(s)) {
2548
        gen_op_add_reg_im(2, R_ESP, addend);
B
bellard 已提交
2549 2550
    } else
#endif
2551
    if (s->ss32) {
2552
        gen_op_add_reg_im(1, R_ESP, addend);
2553
    } else {
2554
        gen_op_add_reg_im(0, R_ESP, addend);
2555 2556 2557
    }
}

B
bellard 已提交
2558 2559 2560
/* generate a push. It depends on ss32, addseg and dflag */
static void gen_push_T0(DisasContext *s)
{
B
bellard 已提交
2561 2562
#ifdef TARGET_X86_64
    if (CODE64(s)) {
B
bellard 已提交
2563
        gen_op_movq_A0_reg(R_ESP);
2564
        if (s->dflag) {
B
bellard 已提交
2565 2566
            gen_op_addq_A0_im(-8);
            gen_op_st_T0_A0(OT_QUAD + s->mem_index);
2567
        } else {
B
bellard 已提交
2568 2569
            gen_op_addq_A0_im(-2);
            gen_op_st_T0_A0(OT_WORD + s->mem_index);
2570
        }
B
bellard 已提交
2571
        gen_op_mov_reg_A0(2, R_ESP);
2572
    } else
B
bellard 已提交
2573 2574
#endif
    {
B
bellard 已提交
2575
        gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2576
        if (!s->dflag)
B
bellard 已提交
2577
            gen_op_addl_A0_im(-2);
B
bellard 已提交
2578
        else
B
bellard 已提交
2579
            gen_op_addl_A0_im(-4);
B
bellard 已提交
2580 2581
        if (s->ss32) {
            if (s->addseg) {
2582
                tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2583
                gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2584 2585 2586
            }
        } else {
            gen_op_andl_A0_ffff();
2587
            tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2588
            gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2589
        }
B
bellard 已提交
2590
        gen_op_st_T0_A0(s->dflag + 1 + s->mem_index);
B
bellard 已提交
2591
        if (s->ss32 && !s->addseg)
B
bellard 已提交
2592
            gen_op_mov_reg_A0(1, R_ESP);
B
bellard 已提交
2593
        else
B
bellard 已提交
2594
            gen_op_mov_reg_T1(s->ss32 + 1, R_ESP);
B
bellard 已提交
2595 2596 2597
    }
}

2598 2599 2600
/* 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 已提交
2601
{
B
bellard 已提交
2602 2603
#ifdef TARGET_X86_64
    if (CODE64(s)) {
B
bellard 已提交
2604
        gen_op_movq_A0_reg(R_ESP);
2605
        if (s->dflag) {
B
bellard 已提交
2606 2607
            gen_op_addq_A0_im(-8);
            gen_op_st_T1_A0(OT_QUAD + s->mem_index);
2608
        } else {
B
bellard 已提交
2609 2610
            gen_op_addq_A0_im(-2);
            gen_op_st_T0_A0(OT_WORD + s->mem_index);
2611
        }
B
bellard 已提交
2612
        gen_op_mov_reg_A0(2, R_ESP);
2613
    } else
B
bellard 已提交
2614 2615
#endif
    {
B
bellard 已提交
2616
        gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2617
        if (!s->dflag)
B
bellard 已提交
2618
            gen_op_addl_A0_im(-2);
B
bellard 已提交
2619
        else
B
bellard 已提交
2620
            gen_op_addl_A0_im(-4);
B
bellard 已提交
2621 2622
        if (s->ss32) {
            if (s->addseg) {
2623
                gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2624 2625 2626
            }
        } else {
            gen_op_andl_A0_ffff();
2627
            gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2628
        }
B
bellard 已提交
2629
        gen_op_st_T1_A0(s->dflag + 1 + s->mem_index);
2630

B
bellard 已提交
2631
        if (s->ss32 && !s->addseg)
B
bellard 已提交
2632
            gen_op_mov_reg_A0(1, R_ESP);
B
bellard 已提交
2633 2634
        else
            gen_stack_update(s, (-2) << s->dflag);
B
bellard 已提交
2635 2636 2637
    }
}

2638 2639
/* two step pop is necessary for precise exceptions */
static void gen_pop_T0(DisasContext *s)
B
bellard 已提交
2640
{
B
bellard 已提交
2641 2642
#ifdef TARGET_X86_64
    if (CODE64(s)) {
B
bellard 已提交
2643 2644
        gen_op_movq_A0_reg(R_ESP);
        gen_op_ld_T0_A0((s->dflag ? OT_QUAD : OT_WORD) + s->mem_index);
2645
    } else
B
bellard 已提交
2646 2647
#endif
    {
B
bellard 已提交
2648
        gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2649 2650
        if (s->ss32) {
            if (s->addseg)
2651
                gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2652 2653
        } else {
            gen_op_andl_A0_ffff();
2654
            gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2655
        }
B
bellard 已提交
2656
        gen_op_ld_T0_A0(s->dflag + 1 + s->mem_index);
B
bellard 已提交
2657 2658 2659 2660 2661
    }
}

static void gen_pop_update(DisasContext *s)
{
B
bellard 已提交
2662
#ifdef TARGET_X86_64
2663
    if (CODE64(s) && s->dflag) {
B
bellard 已提交
2664 2665 2666 2667 2668 2669
        gen_stack_update(s, 8);
    } else
#endif
    {
        gen_stack_update(s, 2 << s->dflag);
    }
B
bellard 已提交
2670 2671 2672 2673
}

static void gen_stack_A0(DisasContext *s)
{
B
bellard 已提交
2674
    gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2675 2676
    if (!s->ss32)
        gen_op_andl_A0_ffff();
2677
    tcg_gen_mov_tl(cpu_T[1], cpu_A0);
B
bellard 已提交
2678
    if (s->addseg)
2679
        gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2680 2681 2682 2683 2684 2685
}

/* NOTE: wrap around in 16 bit not fully handled */
static void gen_pusha(DisasContext *s)
{
    int i;
B
bellard 已提交
2686
    gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2687 2688 2689
    gen_op_addl_A0_im(-16 <<  s->dflag);
    if (!s->ss32)
        gen_op_andl_A0_ffff();
2690
    tcg_gen_mov_tl(cpu_T[1], cpu_A0);
B
bellard 已提交
2691
    if (s->addseg)
2692
        gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2693
    for(i = 0;i < 8; i++) {
B
bellard 已提交
2694 2695
        gen_op_mov_TN_reg(OT_LONG, 0, 7 - i);
        gen_op_st_T0_A0(OT_WORD + s->dflag + s->mem_index);
B
bellard 已提交
2696 2697
        gen_op_addl_A0_im(2 <<  s->dflag);
    }
B
bellard 已提交
2698
    gen_op_mov_reg_T1(OT_WORD + s->ss32, R_ESP);
B
bellard 已提交
2699 2700 2701 2702 2703 2704
}

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

static void gen_enter(DisasContext *s, int esp_addend, int level)
{
B
bellard 已提交
2725
    int ot, opsize;
B
bellard 已提交
2726 2727

    level &= 0x1f;
2728 2729 2730 2731
#ifdef TARGET_X86_64
    if (CODE64(s)) {
        ot = s->dflag ? OT_QUAD : OT_WORD;
        opsize = 1 << ot;
2732

B
bellard 已提交
2733
        gen_op_movl_A0_reg(R_ESP);
2734
        gen_op_addq_A0_im(-opsize);
2735
        tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2736 2737

        /* push bp */
B
bellard 已提交
2738 2739
        gen_op_mov_TN_reg(OT_LONG, 0, R_EBP);
        gen_op_st_T0_A0(ot + s->mem_index);
2740
        if (level) {
B
bellard 已提交
2741
            /* XXX: must save state */
2742
            gen_helper_enter64_level(cpu_env, tcg_const_i32(level),
P
pbrook 已提交
2743 2744
                                     tcg_const_i32((ot == OT_QUAD)),
                                     cpu_T[1]);
2745
        }
B
bellard 已提交
2746
        gen_op_mov_reg_T1(ot, R_EBP);
2747
        tcg_gen_addi_tl(cpu_T[1], cpu_T[1], -esp_addend + (-opsize * level));
B
bellard 已提交
2748
        gen_op_mov_reg_T1(OT_QUAD, R_ESP);
2749
    } else
2750 2751 2752 2753
#endif
    {
        ot = s->dflag + OT_WORD;
        opsize = 2 << s->dflag;
2754

B
bellard 已提交
2755
        gen_op_movl_A0_reg(R_ESP);
2756 2757 2758
        gen_op_addl_A0_im(-opsize);
        if (!s->ss32)
            gen_op_andl_A0_ffff();
2759
        tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2760
        if (s->addseg)
2761
            gen_op_addl_A0_seg(s, R_SS);
2762
        /* push bp */
B
bellard 已提交
2763 2764
        gen_op_mov_TN_reg(OT_LONG, 0, R_EBP);
        gen_op_st_T0_A0(ot + s->mem_index);
2765
        if (level) {
B
bellard 已提交
2766
            /* XXX: must save state */
2767
            gen_helper_enter_level(cpu_env, tcg_const_i32(level),
P
pbrook 已提交
2768 2769
                                   tcg_const_i32(s->dflag),
                                   cpu_T[1]);
2770
        }
B
bellard 已提交
2771
        gen_op_mov_reg_T1(ot, R_EBP);
2772
        tcg_gen_addi_tl(cpu_T[1], cpu_T[1], -esp_addend + (-opsize * level));
B
bellard 已提交
2773
        gen_op_mov_reg_T1(OT_WORD + s->ss32, R_ESP);
B
bellard 已提交
2774 2775 2776
    }
}

B
bellard 已提交
2777
static void gen_exception(DisasContext *s, int trapno, target_ulong cur_eip)
B
bellard 已提交
2778
{
2779
    gen_update_cc_op(s);
B
bellard 已提交
2780
    gen_jmp_im(cur_eip);
B
Blue Swirl 已提交
2781
    gen_helper_raise_exception(cpu_env, tcg_const_i32(trapno));
J
Jun Koi 已提交
2782
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2783 2784 2785
}

/* an interrupt is different from an exception because of the
B
blueswir1 已提交
2786
   privilege checks */
2787
static void gen_interrupt(DisasContext *s, int intno,
B
bellard 已提交
2788
                          target_ulong cur_eip, target_ulong next_eip)
B
bellard 已提交
2789
{
2790
    gen_update_cc_op(s);
B
bellard 已提交
2791
    gen_jmp_im(cur_eip);
B
Blue Swirl 已提交
2792
    gen_helper_raise_interrupt(cpu_env, tcg_const_i32(intno),
P
pbrook 已提交
2793
                               tcg_const_i32(next_eip - cur_eip));
J
Jun Koi 已提交
2794
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2795 2796
}

B
bellard 已提交
2797
static void gen_debug(DisasContext *s, target_ulong cur_eip)
B
bellard 已提交
2798
{
2799
    gen_update_cc_op(s);
B
bellard 已提交
2800
    gen_jmp_im(cur_eip);
B
Blue Swirl 已提交
2801
    gen_helper_debug(cpu_env);
J
Jun Koi 已提交
2802
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2803 2804 2805 2806 2807 2808
}

/* generate a generic end of block. Trace exception is also generated
   if needed */
static void gen_eob(DisasContext *s)
{
2809
    gen_update_cc_op(s);
2810
    if (s->tb->flags & HF_INHIBIT_IRQ_MASK) {
2811
        gen_helper_reset_inhibit_irq(cpu_env);
2812
    }
J
Jan Kiszka 已提交
2813
    if (s->tb->flags & HF_RF_MASK) {
2814
        gen_helper_reset_rf(cpu_env);
J
Jan Kiszka 已提交
2815
    }
2816
    if (s->singlestep_enabled) {
B
Blue Swirl 已提交
2817
        gen_helper_debug(cpu_env);
2818
    } else if (s->tf) {
B
Blue Swirl 已提交
2819
        gen_helper_single_step(cpu_env);
B
bellard 已提交
2820
    } else {
B
bellard 已提交
2821
        tcg_gen_exit_tb(0);
B
bellard 已提交
2822
    }
J
Jun Koi 已提交
2823
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2824 2825 2826 2827
}

/* generate a jump to eip. No segment change must happen before as a
   direct call to the next block may occur */
B
bellard 已提交
2828
static void gen_jmp_tb(DisasContext *s, target_ulong eip, int tb_num)
B
bellard 已提交
2829
{
2830 2831
    gen_update_cc_op(s);
    set_cc_op(s, CC_OP_DYNAMIC);
B
bellard 已提交
2832
    if (s->jmp_opt) {
2833
        gen_goto_tb(s, tb_num, eip);
J
Jun Koi 已提交
2834
        s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2835
    } else {
B
bellard 已提交
2836
        gen_jmp_im(eip);
B
bellard 已提交
2837 2838 2839 2840
        gen_eob(s);
    }
}

B
bellard 已提交
2841 2842 2843 2844 2845
static void gen_jmp(DisasContext *s, target_ulong eip)
{
    gen_jmp_tb(s, eip, 0);
}

B
bellard 已提交
2846 2847 2848
static inline void gen_ldq_env_A0(int idx, int offset)
{
    int mem_index = (idx >> 2) - 1;
2849 2850
    tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_A0, mem_index);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset);
B
bellard 已提交
2851
}
B
bellard 已提交
2852

B
bellard 已提交
2853 2854 2855
static inline void gen_stq_env_A0(int idx, int offset)
{
    int mem_index = (idx >> 2) - 1;
2856 2857
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset);
    tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_A0, mem_index);
B
bellard 已提交
2858
}
B
bellard 已提交
2859

B
bellard 已提交
2860 2861 2862
static inline void gen_ldo_env_A0(int idx, int offset)
{
    int mem_index = (idx >> 2) - 1;
2863 2864
    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 已提交
2865
    tcg_gen_addi_tl(cpu_tmp0, cpu_A0, 8);
2866 2867
    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 已提交
2868
}
B
bellard 已提交
2869

B
bellard 已提交
2870 2871 2872
static inline void gen_sto_env_A0(int idx, int offset)
{
    int mem_index = (idx >> 2) - 1;
2873 2874
    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 已提交
2875
    tcg_gen_addi_tl(cpu_tmp0, cpu_A0, 8);
2876 2877
    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 已提交
2878
}
B
bellard 已提交
2879

B
bellard 已提交
2880 2881
static inline void gen_op_movo(int d_offset, int s_offset)
{
2882 2883 2884 2885
    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 已提交
2886 2887 2888 2889
}

static inline void gen_op_movq(int d_offset, int s_offset)
{
2890 2891
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
B
bellard 已提交
2892 2893 2894 2895
}

static inline void gen_op_movl(int d_offset, int s_offset)
{
2896 2897
    tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env, s_offset);
    tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, d_offset);
B
bellard 已提交
2898 2899 2900 2901
}

static inline void gen_op_movq_env_0(int d_offset)
{
2902 2903
    tcg_gen_movi_i64(cpu_tmp1_i64, 0);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
B
bellard 已提交
2904
}
B
bellard 已提交
2905

B
Blue Swirl 已提交
2906 2907 2908 2909 2910 2911 2912
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 已提交
2913
typedef void (*SSEFunc_0_ppi)(TCGv_ptr reg_a, TCGv_ptr reg_b, TCGv_i32 val);
B
Blue Swirl 已提交
2914 2915
typedef void (*SSEFunc_0_eppt)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
                               TCGv val);
B
Blue Swirl 已提交
2916

B
bellard 已提交
2917 2918
#define SSE_SPECIAL ((void *)1)
#define SSE_DUMMY ((void *)2)
B
bellard 已提交
2919

P
pbrook 已提交
2920 2921 2922
#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 已提交
2923

B
Blue Swirl 已提交
2924
static const SSEFunc_0_epp sse_op_table1[256][4] = {
A
aurel32 已提交
2925 2926 2927
    /* 3DNow! extensions */
    [0x0e] = { SSE_DUMMY }, /* femms */
    [0x0f] = { SSE_DUMMY }, /* pf... */
B
bellard 已提交
2928 2929 2930
    /* 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 已提交
2931
    [0x12] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movlps, movlpd, movsldup, movddup */
B
bellard 已提交
2932
    [0x13] = { SSE_SPECIAL, SSE_SPECIAL },  /* movlps, movlpd */
P
pbrook 已提交
2933 2934
    [0x14] = { gen_helper_punpckldq_xmm, gen_helper_punpcklqdq_xmm },
    [0x15] = { gen_helper_punpckhdq_xmm, gen_helper_punpckhqdq_xmm },
B
bellard 已提交
2935 2936 2937 2938 2939 2940
    [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 */
2941
    [0x2b] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movntps, movntpd, movntss, movntsd */
B
bellard 已提交
2942 2943
    [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 已提交
2944 2945
    [0x2e] = { gen_helper_ucomiss, gen_helper_ucomisd },
    [0x2f] = { gen_helper_comiss, gen_helper_comisd },
B
bellard 已提交
2946 2947
    [0x50] = { SSE_SPECIAL, SSE_SPECIAL }, /* movmskps, movmskpd */
    [0x51] = SSE_FOP(sqrt),
P
pbrook 已提交
2948 2949 2950 2951 2952 2953
    [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 已提交
2954 2955
    [0x58] = SSE_FOP(add),
    [0x59] = SSE_FOP(mul),
P
pbrook 已提交
2956 2957 2958
    [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 已提交
2959 2960 2961 2962 2963 2964
    [0x5c] = SSE_FOP(sub),
    [0x5d] = SSE_FOP(min),
    [0x5e] = SSE_FOP(div),
    [0x5f] = SSE_FOP(max),

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

R
Richard Henderson 已提交
2968 2969 2970
    /* 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 已提交
2971

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

B
Blue Swirl 已提交
3071
static const SSEFunc_0_epi sse_op_table3ai[] = {
P
pbrook 已提交
3072
    gen_helper_cvtsi2ss,
3073
    gen_helper_cvtsi2sd
B
Blue Swirl 已提交
3074
};
P
pbrook 已提交
3075

3076
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
3077
static const SSEFunc_0_epl sse_op_table3aq[] = {
3078 3079 3080 3081 3082
    gen_helper_cvtsq2ss,
    gen_helper_cvtsq2sd
};
#endif

B
Blue Swirl 已提交
3083
static const SSEFunc_i_ep sse_op_table3bi[] = {
P
pbrook 已提交
3084 3085
    gen_helper_cvttss2si,
    gen_helper_cvtss2si,
3086
    gen_helper_cvttsd2si,
3087
    gen_helper_cvtsd2si
B
bellard 已提交
3088
};
3089

3090
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
3091
static const SSEFunc_l_ep sse_op_table3bq[] = {
3092 3093
    gen_helper_cvttss2sq,
    gen_helper_cvtss2sq,
3094
    gen_helper_cvttsd2sq,
3095 3096 3097 3098
    gen_helper_cvtsd2sq
};
#endif

B
Blue Swirl 已提交
3099
static const SSEFunc_0_epp sse_op_table4[8][4] = {
B
bellard 已提交
3100 3101 3102 3103 3104 3105 3106 3107 3108
    SSE_FOP(cmpeq),
    SSE_FOP(cmplt),
    SSE_FOP(cmple),
    SSE_FOP(cmpunord),
    SSE_FOP(cmpneq),
    SSE_FOP(cmpnlt),
    SSE_FOP(cmpnle),
    SSE_FOP(cmpord),
};
3109

B
Blue Swirl 已提交
3110
static const SSEFunc_0_epp sse_op_table5[256] = {
P
pbrook 已提交
3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134
    [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 已提交
3135 3136
};

B
Blue Swirl 已提交
3137 3138
struct SSEOpHelper_epp {
    SSEFunc_0_epp op[2];
B
Blue Swirl 已提交
3139 3140 3141
    uint32_t ext_mask;
};

B
Blue Swirl 已提交
3142 3143
struct SSEOpHelper_eppi {
    SSEFunc_0_eppi op[2];
B
Blue Swirl 已提交
3144
    uint32_t ext_mask;
B
balrog 已提交
3145
};
B
Blue Swirl 已提交
3146

B
balrog 已提交
3147
#define SSSE3_OP(x) { MMX_OP2(x), CPUID_EXT_SSSE3 }
P
pbrook 已提交
3148 3149
#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 已提交
3150
#define SSE41_SPECIAL { { NULL, SSE_SPECIAL }, CPUID_EXT_SSE41 }
3151 3152
#define PCLMULQDQ_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, \
        CPUID_EXT_PCLMULQDQ }
3153
#define AESNI_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_AES }
B
Blue Swirl 已提交
3154

B
Blue Swirl 已提交
3155
static const struct SSEOpHelper_epp sse_op_table6[256] = {
B
balrog 已提交
3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 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
    [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),
3202 3203 3204 3205 3206
    [0xdb] = AESNI_OP(aesimc),
    [0xdc] = AESNI_OP(aesenc),
    [0xdd] = AESNI_OP(aesenclast),
    [0xde] = AESNI_OP(aesdec),
    [0xdf] = AESNI_OP(aesdeclast),
B
balrog 已提交
3207 3208
};

B
Blue Swirl 已提交
3209
static const struct SSEOpHelper_eppi sse_op_table7[256] = {
B
balrog 已提交
3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227
    [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),
3228
    [0x44] = PCLMULQDQ_OP(pclmulqdq),
B
balrog 已提交
3229 3230 3231 3232
    [0x60] = SSE42_OP(pcmpestrm),
    [0x61] = SSE42_OP(pcmpestri),
    [0x62] = SSE42_OP(pcmpistrm),
    [0x63] = SSE42_OP(pcmpistri),
3233
    [0xdf] = AESNI_OP(aeskeygenassist),
B
balrog 已提交
3234 3235
};

3236 3237
static void gen_sse(CPUX86State *env, DisasContext *s, int b,
                    target_ulong pc_start, int rex_r)
B
bellard 已提交
3238 3239 3240
{
    int b1, op1_offset, op2_offset, is_xmm, val, ot;
    int modrm, mod, rm, reg, reg_addr, offset_addr;
B
Blue Swirl 已提交
3241 3242
    SSEFunc_0_epp sse_fn_epp;
    SSEFunc_0_eppi sse_fn_eppi;
B
Blue Swirl 已提交
3243
    SSEFunc_0_ppi sse_fn_ppi;
B
Blue Swirl 已提交
3244
    SSEFunc_0_eppt sse_fn_eppt;
B
bellard 已提交
3245 3246

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

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

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

B
balrog 已提交
3862
        case 0x138:
3863
        case 0x038:
B
balrog 已提交
3864
            b = modrm;
R
Richard Henderson 已提交
3865 3866 3867
            if ((b & 0xf0) == 0xf0) {
                goto do_0f_38_fx;
            }
3868
            modrm = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
3869 3870 3871
            rm = modrm & 7;
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
3872 3873 3874
            if (b1 >= 2) {
                goto illegal_op;
            }
B
balrog 已提交
3875

B
Blue Swirl 已提交
3876 3877
            sse_fn_epp = sse_op_table6[b].op[b1];
            if (!sse_fn_epp) {
B
balrog 已提交
3878
                goto illegal_op;
B
Blue Swirl 已提交
3879
            }
B
balrog 已提交
3880 3881
            if (!(s->cpuid_ext_features & sse_op_table6[b].ext_mask))
                goto illegal_op;
B
balrog 已提交
3882 3883 3884 3885 3886 3887 3888

            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);
3889
                    gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
balrog 已提交
3890 3891 3892 3893 3894 3895 3896 3897 3898
                    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 已提交
3899
                        tcg_gen_qemu_ld32u(cpu_tmp0, cpu_A0,
B
balrog 已提交
3900
                                          (s->mem_index >> 2) - 1);
P
pbrook 已提交
3901
                        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_tmp0);
B
balrog 已提交
3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916
                        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 已提交
3917 3918 3919 3920 3921 3922 3923
                }
            } 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);
3924
                    gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
balrog 已提交
3925 3926 3927
                    gen_ldq_env_A0(s->mem_index, op2_offset);
                }
            }
B
Blue Swirl 已提交
3928
            if (sse_fn_epp == SSE_SPECIAL) {
B
balrog 已提交
3929
                goto illegal_op;
B
Blue Swirl 已提交
3930
            }
B
balrog 已提交
3931

B
balrog 已提交
3932 3933
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
3934
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
balrog 已提交
3935

3936 3937 3938
            if (b == 0x17) {
                set_cc_op(s, CC_OP_EFLAGS);
            }
B
balrog 已提交
3939
            break;
R
Richard Henderson 已提交
3940 3941 3942 3943 3944 3945

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

R
Richard Henderson 已提交
3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962
            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 已提交
3963

R
Richard Henderson 已提交
3964 3965 3966 3967 3968
                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 已提交
3969

R
Richard Henderson 已提交
3970 3971 3972
                ot = (s->dflag == 2) ? OT_QUAD : OT_LONG;
                gen_op_mov_reg_T0(ot, reg);
                break;
B
balrog 已提交
3973

R
Richard Henderson 已提交
3974 3975 3976 3977 3978 3979 3980 3981 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
            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 已提交
4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048
            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 已提交
4049 4050 4051 4052 4053 4054 4055 4056 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
            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 已提交
4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115
            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 已提交
4116 4117 4118 4119 4120 4121 4122 4123 4124 4125
            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) {
                default:
4126 4127 4128 4129 4130 4131
                    tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
                    tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_regs[R_EDX]);
                    tcg_gen_mulu2_i32(cpu_tmp2_i32, cpu_tmp3_i32,
                                      cpu_tmp2_i32, cpu_tmp3_i32);
                    tcg_gen_extu_i32_tl(cpu_regs[s->vex_v], cpu_tmp2_i32);
                    tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp3_i32);
R
Richard Henderson 已提交
4132 4133 4134
                    break;
#ifdef TARGET_X86_64
                case OT_QUAD:
4135 4136
                    tcg_gen_mulu2_i64(cpu_regs[s->vex_v], cpu_regs[reg],
                                      cpu_T[0], cpu_regs[R_EDX]);
R
Richard Henderson 已提交
4137 4138 4139 4140 4141
                    break;
#endif
                }
                break;

4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177
            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;

4178 4179 4180 4181 4182
            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 {
4183
                    TCGv carry_in, carry_out, zero;
4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213
                    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:
4214
                        end_op = (b == 0x1f6 ? CC_OP_ADCX : CC_OP_ADOX);
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
                        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.  */
4243 4244 4245 4246 4247 4248 4249 4250
                        zero = tcg_const_tl(0);
                        tcg_gen_add2_tl(cpu_T[0], carry_out,
                                        cpu_T[0], zero,
                                        carry_in, zero);
                        tcg_gen_add2_tl(cpu_regs[reg], carry_out,
                                        cpu_regs[reg], carry_out,
                                        cpu_T[0], zero);
                        tcg_temp_free(zero);
4251 4252 4253 4254 4255 4256
                        break;
                    }
                    set_cc_op(s, end_op);
                }
                break;

4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287
            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;

4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329
            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 已提交
4330 4331 4332
            default:
                goto illegal_op;
            }
B
balrog 已提交
4333
            break;
R
Richard Henderson 已提交
4334

B
balrog 已提交
4335 4336
        case 0x03a:
        case 0x13a:
B
balrog 已提交
4337
            b = modrm;
4338
            modrm = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
4339 4340 4341
            rm = modrm & 7;
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
4342 4343 4344
            if (b1 >= 2) {
                goto illegal_op;
            }
B
balrog 已提交
4345

B
Blue Swirl 已提交
4346 4347
            sse_fn_eppi = sse_op_table7[b].op[b1];
            if (!sse_fn_eppi) {
B
balrog 已提交
4348
                goto illegal_op;
B
Blue Swirl 已提交
4349
            }
B
balrog 已提交
4350 4351 4352
            if (!(s->cpuid_ext_features & sse_op_table7[b].ext_mask))
                goto illegal_op;

B
Blue Swirl 已提交
4353
            if (sse_fn_eppi == SSE_SPECIAL) {
B
balrog 已提交
4354 4355 4356
                ot = (s->dflag == 2) ? OT_QUAD : OT_LONG;
                rm = (modrm & 7) | REX_B(s);
                if (mod != 3)
4357
                    gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
balrog 已提交
4358
                reg = ((modrm >> 3) & 7) | rex_r;
4359
                val = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383
                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 已提交
4384
                        tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
balrog 已提交
4385
                        if (mod == 3)
P
pbrook 已提交
4386
                            gen_op_mov_reg_v(ot, rm, cpu_T[0]);
B
balrog 已提交
4387
                        else
P
pbrook 已提交
4388
                            tcg_gen_qemu_st32(cpu_T[0], cpu_A0,
B
balrog 已提交
4389 4390
                                            (s->mem_index >> 2) - 1);
                    } else { /* pextrq */
P
pbrook 已提交
4391
#ifdef TARGET_X86_64
B
balrog 已提交
4392 4393 4394 4395 4396 4397 4398 4399
                        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 已提交
4400 4401 4402
#else
                        goto illegal_op;
#endif
B
balrog 已提交
4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417
                    }
                    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
4418
                        tcg_gen_qemu_ld8u(cpu_T[0], cpu_A0,
B
balrog 已提交
4419
                                        (s->mem_index >> 2) - 1);
4420
                    tcg_gen_st8_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
B
balrog 已提交
4421 4422 4423
                                            xmm_regs[reg].XMM_B(val & 15)));
                    break;
                case 0x21: /* insertps */
P
pbrook 已提交
4424
                    if (mod == 3) {
B
balrog 已提交
4425 4426 4427
                        tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env,
                                        offsetof(CPUX86State,xmm_regs[rm]
                                                .XMM_L((val >> 6) & 3)));
P
pbrook 已提交
4428 4429
                    } else {
                        tcg_gen_qemu_ld32u(cpu_tmp0, cpu_A0,
B
balrog 已提交
4430
                                        (s->mem_index >> 2) - 1);
P
pbrook 已提交
4431 4432
                        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_tmp0);
                    }
B
balrog 已提交
4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455
                    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 已提交
4456
                            gen_op_mov_v_reg(ot, cpu_tmp0, rm);
B
balrog 已提交
4457
                        else
P
pbrook 已提交
4458
                            tcg_gen_qemu_ld32u(cpu_tmp0, cpu_A0,
B
balrog 已提交
4459
                                            (s->mem_index >> 2) - 1);
P
pbrook 已提交
4460
                        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_tmp0);
B
balrog 已提交
4461 4462 4463 4464
                        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env,
                                        offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_L(val & 3)));
                    } else { /* pinsrq */
P
pbrook 已提交
4465
#ifdef TARGET_X86_64
B
balrog 已提交
4466 4467 4468 4469 4470 4471 4472 4473
                        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 已提交
4474 4475 4476
#else
                        goto illegal_op;
#endif
B
balrog 已提交
4477 4478 4479 4480 4481
                    }
                    break;
                }
                return;
            }
B
balrog 已提交
4482 4483 4484 4485 4486 4487 4488

            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);
4489
                    gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
balrog 已提交
4490 4491 4492 4493 4494 4495 4496 4497
                    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);
4498
                    gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
balrog 已提交
4499 4500 4501
                    gen_ldq_env_A0(s->mem_index, op2_offset);
                }
            }
4502
            val = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
4503

B
balrog 已提交
4504
            if ((b & 0xfc) == 0x60) { /* pcmpXstrX */
4505
                set_cc_op(s, CC_OP_EFLAGS);
B
balrog 已提交
4506 4507 4508 4509 4510 4511

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

B
balrog 已提交
4512 4513
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4514
            sse_fn_eppi(cpu_env, cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
B
balrog 已提交
4515
            break;
R
Richard Henderson 已提交
4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547

        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 已提交
4548 4549 4550 4551 4552
        default:
            goto illegal_op;
        }
    } else {
        /* generic MMX or SSE operation */
B
bellard 已提交
4553 4554 4555 4556 4557 4558 4559 4560
        switch(b) {
        case 0x70: /* pshufx insn */
        case 0xc6: /* pshufx insn */
        case 0xc2: /* compare insns */
            s->rip_offset = 1;
            break;
        default:
            break;
B
bellard 已提交
4561 4562 4563 4564
        }
        if (is_xmm) {
            op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
            if (mod != 3) {
4565
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
4566
                op2_offset = offsetof(CPUX86State,xmm_t0);
4567
                if (b1 >= 2 && ((b >= 0x50 && b <= 0x5f && b != 0x5b) ||
B
bellard 已提交
4568 4569 4570 4571
                                b == 0xc2)) {
                    /* specific case for SSE single instructions */
                    if (b1 == 2) {
                        /* 32 bit access */
B
bellard 已提交
4572
                        gen_op_ld_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
4573
                        tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(0)));
B
bellard 已提交
4574 4575
                    } else {
                        /* 64 bit access */
B
bellard 已提交
4576
                        gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_t0.XMM_D(0)));
B
bellard 已提交
4577 4578
                    }
                } else {
B
bellard 已提交
4579
                    gen_ldo_env_A0(s->mem_index, op2_offset);
B
bellard 已提交
4580 4581 4582 4583 4584 4585 4586 4587
                }
            } 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) {
4588
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
4589
                op2_offset = offsetof(CPUX86State,mmx_t0);
B
bellard 已提交
4590
                gen_ldq_env_A0(s->mem_index, op2_offset);
B
bellard 已提交
4591 4592 4593 4594 4595 4596
            } else {
                rm = (modrm & 7);
                op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
            }
        }
        switch(b) {
A
aurel32 已提交
4597
        case 0x0f: /* 3DNow! data insns */
4598 4599
            if (!(s->cpuid_ext2_features & CPUID_EXT2_3DNOW))
                goto illegal_op;
4600
            val = cpu_ldub_code(env, s->pc++);
B
Blue Swirl 已提交
4601 4602
            sse_fn_epp = sse_op_table5[val];
            if (!sse_fn_epp) {
A
aurel32 已提交
4603
                goto illegal_op;
B
Blue Swirl 已提交
4604
            }
B
bellard 已提交
4605 4606
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4607
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
A
aurel32 已提交
4608
            break;
B
bellard 已提交
4609 4610
        case 0x70: /* pshufx insn */
        case 0xc6: /* pshufx insn */
4611
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4612 4613
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4614
            /* XXX: introduce a new table? */
B
Blue Swirl 已提交
4615
            sse_fn_ppi = (SSEFunc_0_ppi)sse_fn_epp;
B
Blue Swirl 已提交
4616
            sse_fn_ppi(cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
B
bellard 已提交
4617 4618 4619
            break;
        case 0xc2:
            /* compare insns */
4620
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4621 4622
            if (val >= 8)
                goto illegal_op;
B
Blue Swirl 已提交
4623
            sse_fn_epp = sse_op_table4[val][b1];
B
Blue Swirl 已提交
4624

B
bellard 已提交
4625 4626
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4627
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
4628
            break;
4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646
        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 已提交
4647
            /* XXX: introduce a new table? */
B
Blue Swirl 已提交
4648 4649
            sse_fn_eppt = (SSEFunc_0_eppt)sse_fn_epp;
            sse_fn_eppt(cpu_env, cpu_ptr0, cpu_ptr1, cpu_A0);
4650
            break;
B
bellard 已提交
4651
        default:
B
bellard 已提交
4652 4653
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4654
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
4655 4656 4657
            break;
        }
        if (b == 0x2e || b == 0x2f) {
4658
            set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
4659 4660 4661 4662
        }
    }
}

B
bellard 已提交
4663 4664
/* convert one instruction. s->is_jmp is set if the translation must
   be stopped. Return the next pc value */
4665 4666
static target_ulong disas_insn(CPUX86State *env, DisasContext *s,
                               target_ulong pc_start)
B
bellard 已提交
4667 4668 4669 4670
{
    int b, prefixes, aflag, dflag;
    int shift, ot;
    int modrm, reg, rm, mod, reg_addr, op, opreg, offset_addr, val;
B
bellard 已提交
4671 4672
    target_ulong next_eip, tval;
    int rex_w, rex_r;
B
bellard 已提交
4673

4674
    if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
4675
        tcg_gen_debug_insn_start(pc_start);
4676
    }
B
bellard 已提交
4677 4678 4679 4680 4681
    s->pc = pc_start;
    prefixes = 0;
    aflag = s->code32;
    dflag = s->code32;
    s->override = -1;
B
bellard 已提交
4682 4683 4684 4685 4686
    rex_w = -1;
    rex_r = 0;
#ifdef TARGET_X86_64
    s->rex_x = 0;
    s->rex_b = 0;
4687
    x86_64_hregs = 0;
B
bellard 已提交
4688 4689
#endif
    s->rip_offset = 0; /* for relative ip address */
4690 4691
    s->vex_l = 0;
    s->vex_v = 0;
B
bellard 已提交
4692
 next_byte:
4693
    b = cpu_ldub_code(env, s->pc);
B
bellard 已提交
4694
    s->pc++;
4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729
    /* 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 已提交
4730
#ifdef TARGET_X86_64
4731 4732
    case 0x40 ... 0x4f:
        if (CODE64(s)) {
B
bellard 已提交
4733 4734 4735 4736 4737 4738 4739 4740
            /* 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;
        }
4741 4742
        break;
#endif
4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759
    case 0xc5: /* 2-byte VEX */
    case 0xc4: /* 3-byte VEX */
        /* VEX prefixes cannot be used except in 32-bit mode.
           Otherwise the instruction is LES or LDS.  */
        if (s->code32 && !s->vm86) {
            static const int pp_prefix[4] = {
                0, PREFIX_DATA, PREFIX_REPZ, PREFIX_REPNZ
            };
            int vex3, vex2 = cpu_ldub_code(env, s->pc);

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

P
Peter Maydell 已提交
4760
            /* 4.1.1-4.1.3: No preceding lock, 66, f2, f3, or rex prefixes. */
4761 4762 4763 4764 4765 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
            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;
4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810
    }

    /* Post-process prefixes.  */
    if (prefixes & PREFIX_DATA) {
        dflag ^= 1;
    }
    if (prefixes & PREFIX_ADR) {
        aflag ^= 1;
    }
#ifdef TARGET_X86_64
    if (CODE64(s)) {
B
bellard 已提交
4811 4812 4813 4814
        if (rex_w == 1) {
            /* 0x66 is ignored if rex.w is set */
            dflag = 2;
        }
4815
        if (!(prefixes & PREFIX_ADR)) {
B
bellard 已提交
4816 4817
            aflag = 2;
        }
B
bellard 已提交
4818
    }
4819
#endif
B
bellard 已提交
4820 4821 4822 4823 4824 4825 4826

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

    /* lock generation */
    if (prefixes & PREFIX_LOCK)
P
pbrook 已提交
4827
        gen_helper_lock();
B
bellard 已提交
4828 4829 4830 4831 4832 4833 4834

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

B
bellard 已提交
4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855
        /**************************/
        /* 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 已提交
4856
                ot = dflag + OT_WORD;
4857

B
bellard 已提交
4858 4859
            switch(f) {
            case 0: /* OP Ev, Gv */
4860
                modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4861
                reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
4862
                mod = (modrm >> 6) & 3;
B
bellard 已提交
4863
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4864
                if (mod != 3) {
4865
                    gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
4866 4867 4868 4869
                    opreg = OR_TMP0;
                } else if (op == OP_XORL && rm == reg) {
                xor_zero:
                    /* xor reg, reg optimisation */
R
Richard Henderson 已提交
4870
                    set_cc_op(s, CC_OP_CLR);
B
bellard 已提交
4871
                    gen_op_movl_T0_0();
B
bellard 已提交
4872
                    gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
4873 4874 4875 4876
                    break;
                } else {
                    opreg = rm;
                }
B
bellard 已提交
4877
                gen_op_mov_TN_reg(ot, 1, reg);
B
bellard 已提交
4878 4879 4880
                gen_op(s, op, ot, opreg);
                break;
            case 1: /* OP Gv, Ev */
4881
                modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4882
                mod = (modrm >> 6) & 3;
B
bellard 已提交
4883 4884
                reg = ((modrm >> 3) & 7) | rex_r;
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4885
                if (mod != 3) {
4886
                    gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
4887
                    gen_op_ld_T1_A0(ot + s->mem_index);
B
bellard 已提交
4888 4889 4890
                } else if (op == OP_XORL && rm == reg) {
                    goto xor_zero;
                } else {
B
bellard 已提交
4891
                    gen_op_mov_TN_reg(ot, 1, rm);
B
bellard 已提交
4892 4893 4894 4895
                }
                gen_op(s, op, ot, reg);
                break;
            case 2: /* OP A, Iv */
4896
                val = insn_get(env, s, ot);
B
bellard 已提交
4897 4898 4899 4900 4901 4902 4903
                gen_op_movl_T1_im(val);
                gen_op(s, op, ot, OR_EAX);
                break;
            }
        }
        break;

4904 4905 4906
    case 0x82:
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
4907 4908 4909 4910 4911 4912 4913 4914 4915
    case 0x80: /* GRP1 */
    case 0x81:
    case 0x83:
        {
            int val;

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

4918
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4919
            mod = (modrm >> 6) & 3;
B
bellard 已提交
4920
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4921
            op = (modrm >> 3) & 7;
4922

B
bellard 已提交
4923
            if (mod != 3) {
B
bellard 已提交
4924 4925 4926 4927
                if (b == 0x83)
                    s->rip_offset = 1;
                else
                    s->rip_offset = insn_const_size(ot);
4928
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
4929 4930
                opreg = OR_TMP0;
            } else {
B
bellard 已提交
4931
                opreg = rm;
B
bellard 已提交
4932 4933 4934 4935 4936 4937
            }

            switch(b) {
            default:
            case 0x80:
            case 0x81:
4938
            case 0x82:
4939
                val = insn_get(env, s, ot);
B
bellard 已提交
4940 4941
                break;
            case 0x83:
4942
                val = (int8_t)insn_get(env, s, OT_BYTE);
B
bellard 已提交
4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964
                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 已提交
4965
            ot = dflag + OT_WORD;
B
bellard 已提交
4966

4967
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4968
        mod = (modrm >> 6) & 3;
B
bellard 已提交
4969
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4970 4971
        op = (modrm >> 3) & 7;
        if (mod != 3) {
B
bellard 已提交
4972 4973
            if (op == 0)
                s->rip_offset = insn_const_size(ot);
4974
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
4975
            gen_op_ld_T0_A0(ot + s->mem_index);
B
bellard 已提交
4976
        } else {
B
bellard 已提交
4977
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
4978 4979 4980 4981
        }

        switch(op) {
        case 0: /* test */
4982
            val = insn_get(env, s, ot);
B
bellard 已提交
4983 4984
            gen_op_movl_T1_im(val);
            gen_op_testl_T0_T1_cc();
4985
            set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
4986 4987
            break;
        case 2: /* not */
4988
            tcg_gen_not_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
4989
            if (mod != 3) {
B
bellard 已提交
4990
                gen_op_st_T0_A0(ot + s->mem_index);
B
bellard 已提交
4991
            } else {
B
bellard 已提交
4992
                gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
4993 4994 4995
            }
            break;
        case 3: /* neg */
4996
            tcg_gen_neg_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
4997
            if (mod != 3) {
B
bellard 已提交
4998
                gen_op_st_T0_A0(ot + s->mem_index);
B
bellard 已提交
4999
            } else {
B
bellard 已提交
5000
                gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
5001 5002
            }
            gen_op_update_neg_cc();
5003
            set_cc_op(s, CC_OP_SUBB + ot);
B
bellard 已提交
5004 5005 5006 5007
            break;
        case 4: /* mul */
            switch(ot) {
            case OT_BYTE:
B
bellard 已提交
5008 5009 5010 5011 5012 5013 5014 5015
                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);
5016
                set_cc_op(s, CC_OP_MULB);
B
bellard 已提交
5017 5018
                break;
            case OT_WORD:
B
bellard 已提交
5019 5020 5021 5022 5023 5024 5025 5026 5027 5028
                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]);
5029
                set_cc_op(s, CC_OP_MULW);
B
bellard 已提交
5030 5031 5032
                break;
            default:
            case OT_LONG:
5033 5034 5035 5036 5037 5038 5039 5040
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
                tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_regs[R_EAX]);
                tcg_gen_mulu2_i32(cpu_tmp2_i32, cpu_tmp3_i32,
                                  cpu_tmp2_i32, cpu_tmp3_i32);
                tcg_gen_extu_i32_tl(cpu_regs[R_EAX], cpu_tmp2_i32);
                tcg_gen_extu_i32_tl(cpu_regs[R_EDX], cpu_tmp3_i32);
                tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[R_EAX]);
                tcg_gen_mov_tl(cpu_cc_src, cpu_regs[R_EDX]);
5041
                set_cc_op(s, CC_OP_MULL);
B
bellard 已提交
5042
                break;
B
bellard 已提交
5043 5044
#ifdef TARGET_X86_64
            case OT_QUAD:
5045 5046 5047 5048
                tcg_gen_mulu2_i64(cpu_regs[R_EAX], cpu_regs[R_EDX],
                                  cpu_T[0], cpu_regs[R_EAX]);
                tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[R_EAX]);
                tcg_gen_mov_tl(cpu_cc_src, cpu_regs[R_EDX]);
5049
                set_cc_op(s, CC_OP_MULQ);
B
bellard 已提交
5050 5051
                break;
#endif
B
bellard 已提交
5052 5053 5054 5055 5056
            }
            break;
        case 5: /* imul */
            switch(ot) {
            case OT_BYTE:
B
bellard 已提交
5057 5058 5059 5060 5061 5062 5063 5064 5065
                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);
5066
                set_cc_op(s, CC_OP_MULB);
B
bellard 已提交
5067 5068
                break;
            case OT_WORD:
B
bellard 已提交
5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079
                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);
5080
                set_cc_op(s, CC_OP_MULW);
B
bellard 已提交
5081 5082 5083
                break;
            default:
            case OT_LONG:
5084 5085 5086 5087 5088 5089 5090 5091 5092 5093
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
                tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_regs[R_EAX]);
                tcg_gen_muls2_i32(cpu_tmp2_i32, cpu_tmp3_i32,
                                  cpu_tmp2_i32, cpu_tmp3_i32);
                tcg_gen_extu_i32_tl(cpu_regs[R_EAX], cpu_tmp2_i32);
                tcg_gen_extu_i32_tl(cpu_regs[R_EDX], cpu_tmp3_i32);
                tcg_gen_sari_i32(cpu_tmp2_i32, cpu_tmp2_i32, 31);
                tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[R_EAX]);
                tcg_gen_sub_i32(cpu_tmp2_i32, cpu_tmp2_i32, cpu_tmp3_i32);
                tcg_gen_extu_i32_tl(cpu_cc_src, cpu_tmp2_i32);
5094
                set_cc_op(s, CC_OP_MULL);
B
bellard 已提交
5095
                break;
B
bellard 已提交
5096 5097
#ifdef TARGET_X86_64
            case OT_QUAD:
5098 5099 5100 5101 5102
                tcg_gen_muls2_i64(cpu_regs[R_EAX], cpu_regs[R_EDX],
                                  cpu_T[0], cpu_regs[R_EAX]);
                tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[R_EAX]);
                tcg_gen_sari_tl(cpu_cc_src, cpu_regs[R_EAX], 63);
                tcg_gen_sub_tl(cpu_cc_src, cpu_cc_src, cpu_regs[R_EDX]);
5103
                set_cc_op(s, CC_OP_MULQ);
B
bellard 已提交
5104 5105
                break;
#endif
B
bellard 已提交
5106 5107 5108 5109 5110
            }
            break;
        case 6: /* div */
            switch(ot) {
            case OT_BYTE:
B
bellard 已提交
5111
                gen_jmp_im(pc_start - s->cs_base);
5112
                gen_helper_divb_AL(cpu_env, cpu_T[0]);
B
bellard 已提交
5113 5114
                break;
            case OT_WORD:
B
bellard 已提交
5115
                gen_jmp_im(pc_start - s->cs_base);
5116
                gen_helper_divw_AX(cpu_env, cpu_T[0]);
B
bellard 已提交
5117 5118 5119
                break;
            default:
            case OT_LONG:
B
bellard 已提交
5120
                gen_jmp_im(pc_start - s->cs_base);
5121
                gen_helper_divl_EAX(cpu_env, cpu_T[0]);
B
bellard 已提交
5122 5123 5124 5125
                break;
#ifdef TARGET_X86_64
            case OT_QUAD:
                gen_jmp_im(pc_start - s->cs_base);
5126
                gen_helper_divq_EAX(cpu_env, cpu_T[0]);
B
bellard 已提交
5127
                break;
B
bellard 已提交
5128
#endif
B
bellard 已提交
5129 5130 5131 5132 5133
            }
            break;
        case 7: /* idiv */
            switch(ot) {
            case OT_BYTE:
B
bellard 已提交
5134
                gen_jmp_im(pc_start - s->cs_base);
5135
                gen_helper_idivb_AL(cpu_env, cpu_T[0]);
B
bellard 已提交
5136 5137
                break;
            case OT_WORD:
B
bellard 已提交
5138
                gen_jmp_im(pc_start - s->cs_base);
5139
                gen_helper_idivw_AX(cpu_env, cpu_T[0]);
B
bellard 已提交
5140 5141 5142
                break;
            default:
            case OT_LONG:
B
bellard 已提交
5143
                gen_jmp_im(pc_start - s->cs_base);
5144
                gen_helper_idivl_EAX(cpu_env, cpu_T[0]);
B
bellard 已提交
5145 5146 5147 5148
                break;
#ifdef TARGET_X86_64
            case OT_QUAD:
                gen_jmp_im(pc_start - s->cs_base);
5149
                gen_helper_idivq_EAX(cpu_env, cpu_T[0]);
B
bellard 已提交
5150
                break;
B
bellard 已提交
5151
#endif
B
bellard 已提交
5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163
            }
            break;
        default:
            goto illegal_op;
        }
        break;

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

5166
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5167
        mod = (modrm >> 6) & 3;
B
bellard 已提交
5168
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
5169 5170 5171 5172
        op = (modrm >> 3) & 7;
        if (op >= 2 && b == 0xfe) {
            goto illegal_op;
        }
B
bellard 已提交
5173
        if (CODE64(s)) {
5174
            if (op == 2 || op == 4) {
B
bellard 已提交
5175 5176
                /* operand size for jumps is 64 bit */
                ot = OT_QUAD;
5177
            } else if (op == 3 || op == 5) {
5178
                ot = dflag ? OT_LONG + (rex_w == 1) : OT_WORD;
B
bellard 已提交
5179 5180 5181 5182 5183
            } else if (op == 6) {
                /* default push size is 64 bit */
                ot = dflag ? OT_QUAD : OT_WORD;
            }
        }
B
bellard 已提交
5184
        if (mod != 3) {
5185
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
5186
            if (op >= 2 && op != 3 && op != 5)
B
bellard 已提交
5187
                gen_op_ld_T0_A0(ot + s->mem_index);
B
bellard 已提交
5188
        } else {
B
bellard 已提交
5189
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207
        }

        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 */
5208
            /* XXX: optimize if memory (no 'and' is necessary) */
B
bellard 已提交
5209 5210 5211
            if (s->dflag == 0)
                gen_op_andl_T0_ffff();
            next_eip = s->pc - s->cs_base;
B
bellard 已提交
5212
            gen_movtl_T1_im(next_eip);
5213 5214
            gen_push_T1(s);
            gen_op_jmp_T0();
B
bellard 已提交
5215 5216
            gen_eob(s);
            break;
B
bellard 已提交
5217
        case 3: /* lcall Ev */
B
bellard 已提交
5218
            gen_op_ld_T1_A0(ot + s->mem_index);
5219
            gen_add_A0_im(s, 1 << (ot - OT_WORD + 1));
B
bellard 已提交
5220
            gen_op_ldu_T0_A0(OT_WORD + s->mem_index);
B
bellard 已提交
5221 5222
        do_lcall:
            if (s->pe && !s->vm86) {
5223
                gen_update_cc_op(s);
B
bellard 已提交
5224
                gen_jmp_im(pc_start - s->cs_base);
5225
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
5226 5227
                gen_helper_lcall_protected(cpu_env, cpu_tmp2_i32, cpu_T[1],
                                           tcg_const_i32(dflag),
P
pbrook 已提交
5228
                                           tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
5229
            } else {
5230
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
5231 5232
                gen_helper_lcall_real(cpu_env, cpu_tmp2_i32, cpu_T[1],
                                      tcg_const_i32(dflag),
P
pbrook 已提交
5233
                                      tcg_const_i32(s->pc - s->cs_base));
B
bellard 已提交
5234 5235 5236 5237 5238 5239 5240 5241 5242 5243
            }
            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 已提交
5244
            gen_op_ld_T1_A0(ot + s->mem_index);
5245
            gen_add_A0_im(s, 1 << (ot - OT_WORD + 1));
B
bellard 已提交
5246
            gen_op_ldu_T0_A0(OT_WORD + s->mem_index);
B
bellard 已提交
5247 5248
        do_ljmp:
            if (s->pe && !s->vm86) {
5249
                gen_update_cc_op(s);
B
bellard 已提交
5250
                gen_jmp_im(pc_start - s->cs_base);
5251
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
5252
                gen_helper_ljmp_protected(cpu_env, cpu_tmp2_i32, cpu_T[1],
P
pbrook 已提交
5253
                                          tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
5254
            } else {
5255
                gen_op_movl_seg_T0_vm(R_CS);
B
bellard 已提交
5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269
                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 */
5270
    case 0x85:
B
bellard 已提交
5271 5272 5273
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
5274
            ot = dflag + OT_WORD;
B
bellard 已提交
5275

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

5279
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
bellard 已提交
5280
        gen_op_mov_TN_reg(ot, 1, reg);
B
bellard 已提交
5281
        gen_op_testl_T0_T1_cc();
5282
        set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
5283
        break;
5284

B
bellard 已提交
5285 5286 5287 5288 5289
    case 0xa8: /* test eAX, Iv */
    case 0xa9:
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
5290
            ot = dflag + OT_WORD;
5291
        val = insn_get(env, s, ot);
B
bellard 已提交
5292

B
bellard 已提交
5293
        gen_op_mov_TN_reg(ot, 0, OR_EAX);
B
bellard 已提交
5294 5295
        gen_op_movl_T1_im(val);
        gen_op_testl_T0_T1_cc();
5296
        set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
5297
        break;
5298

B
bellard 已提交
5299
    case 0x98: /* CWDE/CBW */
B
bellard 已提交
5300 5301
#ifdef TARGET_X86_64
        if (dflag == 2) {
B
bellard 已提交
5302 5303 5304
            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 已提交
5305 5306
        } else
#endif
B
bellard 已提交
5307 5308 5309 5310 5311 5312 5313 5314 5315
        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 已提交
5316 5317
        break;
    case 0x99: /* CDQ/CWD */
B
bellard 已提交
5318 5319
#ifdef TARGET_X86_64
        if (dflag == 2) {
B
bellard 已提交
5320 5321 5322
            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 已提交
5323 5324
        } else
#endif
B
bellard 已提交
5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335
        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 已提交
5336 5337 5338 5339
        break;
    case 0x1af: /* imul Gv, Ev */
    case 0x69: /* imul Gv, Ev, I */
    case 0x6b:
B
bellard 已提交
5340
        ot = dflag + OT_WORD;
5341
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5342 5343 5344 5345 5346
        reg = ((modrm >> 3) & 7) | rex_r;
        if (b == 0x69)
            s->rip_offset = insn_const_size(ot);
        else if (b == 0x6b)
            s->rip_offset = 1;
5347
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
bellard 已提交
5348
        if (b == 0x69) {
5349
            val = insn_get(env, s, ot);
B
bellard 已提交
5350 5351
            gen_op_movl_T1_im(val);
        } else if (b == 0x6b) {
5352
            val = (int8_t)insn_get(env, s, OT_BYTE);
B
bellard 已提交
5353 5354
            gen_op_movl_T1_im(val);
        } else {
B
bellard 已提交
5355
            gen_op_mov_TN_reg(ot, 1, reg);
B
bellard 已提交
5356
        }
5357
        switch (ot) {
B
bellard 已提交
5358
#ifdef TARGET_X86_64
5359 5360 5361 5362 5363 5364
        case OT_QUAD:
            tcg_gen_muls2_i64(cpu_regs[reg], cpu_T[1], cpu_T[0], cpu_T[1]);
            tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[reg]);
            tcg_gen_sari_tl(cpu_cc_src, cpu_cc_dst, 63);
            tcg_gen_sub_tl(cpu_cc_src, cpu_cc_src, cpu_T[1]);
            break;
B
bellard 已提交
5365
#endif
5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377
        case OT_LONG:
            tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
            tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
            tcg_gen_muls2_i32(cpu_tmp2_i32, cpu_tmp3_i32,
                              cpu_tmp2_i32, cpu_tmp3_i32);
            tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp2_i32);
            tcg_gen_sari_i32(cpu_tmp2_i32, cpu_tmp2_i32, 31);
            tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[reg]);
            tcg_gen_sub_i32(cpu_tmp2_i32, cpu_tmp2_i32, cpu_tmp3_i32);
            tcg_gen_extu_i32_tl(cpu_cc_src, cpu_tmp2_i32);
            break;
        default:
B
bellard 已提交
5378 5379 5380 5381 5382 5383 5384
            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);
5385 5386
            gen_op_mov_reg_T0(ot, reg);
            break;
B
bellard 已提交
5387
        }
5388
        set_cc_op(s, CC_OP_MULB + ot);
B
bellard 已提交
5389 5390 5391 5392 5393 5394
        break;
    case 0x1c0:
    case 0x1c1: /* xadd Ev, Gv */
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
5395
            ot = dflag + OT_WORD;
5396
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5397
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5398 5399
        mod = (modrm >> 6) & 3;
        if (mod == 3) {
B
bellard 已提交
5400
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
5401 5402
            gen_op_mov_TN_reg(ot, 0, reg);
            gen_op_mov_TN_reg(ot, 1, rm);
B
bellard 已提交
5403
            gen_op_addl_T0_T1();
B
bellard 已提交
5404 5405
            gen_op_mov_reg_T1(ot, reg);
            gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
5406
        } else {
5407
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
5408 5409
            gen_op_mov_TN_reg(ot, 0, reg);
            gen_op_ld_T1_A0(ot + s->mem_index);
B
bellard 已提交
5410
            gen_op_addl_T0_T1();
B
bellard 已提交
5411 5412
            gen_op_st_T0_A0(ot + s->mem_index);
            gen_op_mov_reg_T1(ot, reg);
B
bellard 已提交
5413 5414
        }
        gen_op_update2_cc();
5415
        set_cc_op(s, CC_OP_ADDB + ot);
B
bellard 已提交
5416 5417 5418
        break;
    case 0x1b0:
    case 0x1b1: /* cmpxchg Ev, Gv */
B
bellard 已提交
5419
        {
B
bellard 已提交
5420
            int label1, label2;
5421
            TCGv t0, t1, t2, a0;
B
bellard 已提交
5422 5423 5424 5425 5426

            if ((b & 1) == 0)
                ot = OT_BYTE;
            else
                ot = dflag + OT_WORD;
5427
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5428 5429
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
P
pbrook 已提交
5430 5431 5432 5433
            t0 = tcg_temp_local_new();
            t1 = tcg_temp_local_new();
            t2 = tcg_temp_local_new();
            a0 = tcg_temp_local_new();
5434
            gen_op_mov_v_reg(ot, t1, reg);
B
bellard 已提交
5435 5436
            if (mod == 3) {
                rm = (modrm & 7) | REX_B(s);
5437
                gen_op_mov_v_reg(ot, t0, rm);
B
bellard 已提交
5438
            } else {
5439
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
5440 5441
                tcg_gen_mov_tl(a0, cpu_A0);
                gen_op_ld_v(ot + s->mem_index, t0, a0);
B
bellard 已提交
5442 5443 5444
                rm = 0; /* avoid warning */
            }
            label1 = gen_new_label();
5445 5446
            tcg_gen_mov_tl(t2, cpu_regs[R_EAX]);
            gen_extu(ot, t0);
5447
            gen_extu(ot, t2);
5448
            tcg_gen_brcond_tl(TCG_COND_EQ, t2, t0, label1);
5449
            label2 = gen_new_label();
B
bellard 已提交
5450
            if (mod == 3) {
5451
                gen_op_mov_reg_v(ot, R_EAX, t0);
B
bellard 已提交
5452 5453
                tcg_gen_br(label2);
                gen_set_label(label1);
5454
                gen_op_mov_reg_v(ot, rm, t1);
B
bellard 已提交
5455
            } else {
5456 5457 5458 5459
                /* 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);
5460
                gen_op_mov_reg_v(ot, R_EAX, t0);
5461
                tcg_gen_br(label2);
B
bellard 已提交
5462
                gen_set_label(label1);
5463
                gen_op_st_v(ot + s->mem_index, t1, a0);
B
bellard 已提交
5464
            }
5465
            gen_set_label(label2);
5466
            tcg_gen_mov_tl(cpu_cc_src, t0);
5467 5468
            tcg_gen_mov_tl(cpu_cc_srcT, t2);
            tcg_gen_sub_tl(cpu_cc_dst, t2, t0);
5469
            set_cc_op(s, CC_OP_SUBB + ot);
5470 5471 5472 5473
            tcg_temp_free(t0);
            tcg_temp_free(t1);
            tcg_temp_free(t2);
            tcg_temp_free(a0);
B
bellard 已提交
5474 5475 5476
        }
        break;
    case 0x1c7: /* cmpxchg8b */
5477
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5478
        mod = (modrm >> 6) & 3;
5479
        if ((mod == 3) || ((modrm & 0x38) != 0x8))
B
bellard 已提交
5480
            goto illegal_op;
B
bellard 已提交
5481 5482 5483 5484 5485
#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);
5486
            gen_update_cc_op(s);
5487
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
Blue Swirl 已提交
5488
            gen_helper_cmpxchg16b(cpu_env, cpu_A0);
B
bellard 已提交
5489 5490 5491 5492 5493 5494
        } else
#endif        
        {
            if (!(s->cpuid_features & CPUID_CX8))
                goto illegal_op;
            gen_jmp_im(pc_start - s->cs_base);
5495
            gen_update_cc_op(s);
5496
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
Blue Swirl 已提交
5497
            gen_helper_cmpxchg8b(cpu_env, cpu_A0);
B
bellard 已提交
5498
        }
5499
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
5500
        break;
5501

B
bellard 已提交
5502 5503 5504
        /**************************/
        /* push/pop */
    case 0x50 ... 0x57: /* push */
B
bellard 已提交
5505
        gen_op_mov_TN_reg(OT_LONG, 0, (b & 7) | REX_B(s));
B
bellard 已提交
5506 5507 5508
        gen_push_T0(s);
        break;
    case 0x58 ... 0x5f: /* pop */
B
bellard 已提交
5509 5510 5511 5512 5513
        if (CODE64(s)) {
            ot = dflag ? OT_QUAD : OT_WORD;
        } else {
            ot = dflag + OT_WORD;
        }
B
bellard 已提交
5514
        gen_pop_T0(s);
B
bellard 已提交
5515
        /* NOTE: order is important for pop %sp */
B
bellard 已提交
5516
        gen_pop_update(s);
B
bellard 已提交
5517
        gen_op_mov_reg_T0(ot, (b & 7) | REX_B(s));
B
bellard 已提交
5518 5519
        break;
    case 0x60: /* pusha */
B
bellard 已提交
5520 5521
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5522 5523 5524
        gen_pusha(s);
        break;
    case 0x61: /* popa */
B
bellard 已提交
5525 5526
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5527 5528 5529 5530
        gen_popa(s);
        break;
    case 0x68: /* push Iv */
    case 0x6a:
B
bellard 已提交
5531 5532 5533 5534 5535
        if (CODE64(s)) {
            ot = dflag ? OT_QUAD : OT_WORD;
        } else {
            ot = dflag + OT_WORD;
        }
B
bellard 已提交
5536
        if (b == 0x68)
5537
            val = insn_get(env, s, ot);
B
bellard 已提交
5538
        else
5539
            val = (int8_t)insn_get(env, s, OT_BYTE);
B
bellard 已提交
5540 5541 5542 5543
        gen_op_movl_T0_im(val);
        gen_push_T0(s);
        break;
    case 0x8f: /* pop Ev */
B
bellard 已提交
5544 5545 5546 5547 5548
        if (CODE64(s)) {
            ot = dflag ? OT_QUAD : OT_WORD;
        } else {
            ot = dflag + OT_WORD;
        }
5549
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5550
        mod = (modrm >> 6) & 3;
B
bellard 已提交
5551
        gen_pop_T0(s);
B
bellard 已提交
5552 5553 5554
        if (mod == 3) {
            /* NOTE: order is important for pop %sp */
            gen_pop_update(s);
B
bellard 已提交
5555
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
5556
            gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
5557 5558
        } else {
            /* NOTE: order is important too for MMU exceptions */
B
bellard 已提交
5559
            s->popl_esp_hack = 1 << ot;
5560
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
5561 5562 5563
            s->popl_esp_hack = 0;
            gen_pop_update(s);
        }
B
bellard 已提交
5564 5565 5566 5567
        break;
    case 0xc8: /* enter */
        {
            int level;
5568
            val = cpu_lduw_code(env, s->pc);
B
bellard 已提交
5569
            s->pc += 2;
5570
            level = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5571 5572 5573 5574 5575
            gen_enter(s, val, level);
        }
        break;
    case 0xc9: /* leave */
        /* XXX: exception not precise (ESP is updated before potential exception) */
B
bellard 已提交
5576
        if (CODE64(s)) {
B
bellard 已提交
5577 5578
            gen_op_mov_TN_reg(OT_QUAD, 0, R_EBP);
            gen_op_mov_reg_T0(OT_QUAD, R_ESP);
B
bellard 已提交
5579
        } else if (s->ss32) {
B
bellard 已提交
5580 5581
            gen_op_mov_TN_reg(OT_LONG, 0, R_EBP);
            gen_op_mov_reg_T0(OT_LONG, R_ESP);
B
bellard 已提交
5582
        } else {
B
bellard 已提交
5583 5584
            gen_op_mov_TN_reg(OT_WORD, 0, R_EBP);
            gen_op_mov_reg_T0(OT_WORD, R_ESP);
B
bellard 已提交
5585 5586
        }
        gen_pop_T0(s);
B
bellard 已提交
5587 5588 5589 5590 5591
        if (CODE64(s)) {
            ot = dflag ? OT_QUAD : OT_WORD;
        } else {
            ot = dflag + OT_WORD;
        }
B
bellard 已提交
5592
        gen_op_mov_reg_T0(ot, R_EBP);
B
bellard 已提交
5593 5594 5595 5596 5597 5598
        gen_pop_update(s);
        break;
    case 0x06: /* push es */
    case 0x0e: /* push cs */
    case 0x16: /* push ss */
    case 0x1e: /* push ds */
B
bellard 已提交
5599 5600
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611
        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 已提交
5612 5613
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5614 5615 5616 5617 5618
        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) {
5619 5620 5621 5622
            /* if reg == SS, inhibit interrupts/trace. */
            /* If several instructions disable interrupts, only the
               _first_ does it */
            if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
5623
                gen_helper_set_inhibit_irq(cpu_env);
B
bellard 已提交
5624 5625 5626
            s->tf = 0;
        }
        if (s->is_jmp) {
B
bellard 已提交
5627
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5628 5629 5630 5631 5632 5633 5634 5635 5636
            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 已提交
5637
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648
            gen_eob(s);
        }
        break;

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

B
bellard 已提交
5653
        /* generate a generic store */
5654
        gen_ldst_modrm(env, s, modrm, ot, reg, 1);
B
bellard 已提交
5655 5656 5657 5658 5659 5660
        break;
    case 0xc6:
    case 0xc7: /* mov Ev, Iv */
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
5661
            ot = dflag + OT_WORD;
5662
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5663
        mod = (modrm >> 6) & 3;
B
bellard 已提交
5664 5665
        if (mod != 3) {
            s->rip_offset = insn_const_size(ot);
5666
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
5667
        }
5668
        val = insn_get(env, s, ot);
B
bellard 已提交
5669 5670
        gen_op_movl_T0_im(val);
        if (mod != 3)
B
bellard 已提交
5671
            gen_op_st_T0_A0(ot + s->mem_index);
B
bellard 已提交
5672
        else
B
bellard 已提交
5673
            gen_op_mov_reg_T0(ot, (modrm & 7) | REX_B(s));
B
bellard 已提交
5674 5675 5676 5677 5678 5679
        break;
    case 0x8a:
    case 0x8b: /* mov Ev, Gv */
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
5680
            ot = OT_WORD + dflag;
5681
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5682
        reg = ((modrm >> 3) & 7) | rex_r;
5683

5684
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
bellard 已提交
5685
        gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
5686 5687
        break;
    case 0x8e: /* mov seg, Gv */
5688
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5689 5690 5691
        reg = (modrm >> 3) & 7;
        if (reg >= 6 || reg == R_CS)
            goto illegal_op;
5692
        gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
B
bellard 已提交
5693 5694 5695
        gen_movl_seg_T0(s, reg, pc_start - s->cs_base);
        if (reg == R_SS) {
            /* if reg == SS, inhibit interrupts/trace */
5696 5697 5698
            /* If several instructions disable interrupts, only the
               _first_ does it */
            if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
5699
                gen_helper_set_inhibit_irq(cpu_env);
B
bellard 已提交
5700 5701 5702
            s->tf = 0;
        }
        if (s->is_jmp) {
B
bellard 已提交
5703
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5704 5705 5706 5707
            gen_eob(s);
        }
        break;
    case 0x8c: /* mov Gv, seg */
5708
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5709 5710 5711 5712 5713
        reg = (modrm >> 3) & 7;
        mod = (modrm >> 6) & 3;
        if (reg >= 6)
            goto illegal_op;
        gen_op_movl_T0_seg(reg);
B
bellard 已提交
5714 5715 5716 5717
        if (mod == 3)
            ot = OT_WORD + dflag;
        else
            ot = OT_WORD;
5718
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730
        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;
5731
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5732
            reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5733
            mod = (modrm >> 6) & 3;
B
bellard 已提交
5734
            rm = (modrm & 7) | REX_B(s);
5735

B
bellard 已提交
5736
            if (mod == 3) {
B
bellard 已提交
5737
                gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
5738 5739
                switch(ot | (b & 8)) {
                case OT_BYTE:
B
bellard 已提交
5740
                    tcg_gen_ext8u_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5741 5742
                    break;
                case OT_BYTE | 8:
B
bellard 已提交
5743
                    tcg_gen_ext8s_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5744 5745
                    break;
                case OT_WORD:
B
bellard 已提交
5746
                    tcg_gen_ext16u_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5747 5748 5749
                    break;
                default:
                case OT_WORD | 8:
B
bellard 已提交
5750
                    tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5751 5752
                    break;
                }
B
bellard 已提交
5753
                gen_op_mov_reg_T0(d_ot, reg);
B
bellard 已提交
5754
            } else {
5755
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
5756
                if (b & 8) {
B
bellard 已提交
5757
                    gen_op_lds_T0_A0(ot + s->mem_index);
B
bellard 已提交
5758
                } else {
B
bellard 已提交
5759
                    gen_op_ldu_T0_A0(ot + s->mem_index);
B
bellard 已提交
5760
                }
B
bellard 已提交
5761
                gen_op_mov_reg_T0(d_ot, reg);
B
bellard 已提交
5762 5763 5764 5765 5766
            }
        }
        break;

    case 0x8d: /* lea */
B
bellard 已提交
5767
        ot = dflag + OT_WORD;
5768
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5769 5770 5771
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
B
bellard 已提交
5772
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5773 5774 5775 5776
        /* we must ensure that no segment is added */
        s->override = -1;
        val = s->addseg;
        s->addseg = 0;
5777
        gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
5778
        s->addseg = val;
B
bellard 已提交
5779
        gen_op_mov_reg_A0(ot - OT_WORD, reg);
B
bellard 已提交
5780
        break;
5781

B
bellard 已提交
5782 5783 5784 5785 5786
    case 0xa0: /* mov EAX, Ov */
    case 0xa1:
    case 0xa2: /* mov Ov, EAX */
    case 0xa3:
        {
B
bellard 已提交
5787 5788 5789 5790 5791 5792 5793
            target_ulong offset_addr;

            if ((b & 1) == 0)
                ot = OT_BYTE;
            else
                ot = dflag + OT_WORD;
#ifdef TARGET_X86_64
5794
            if (s->aflag == 2) {
5795
                offset_addr = cpu_ldq_code(env, s->pc);
B
bellard 已提交
5796
                s->pc += 8;
B
bellard 已提交
5797
                gen_op_movq_A0_im(offset_addr);
5798
            } else
B
bellard 已提交
5799 5800 5801
#endif
            {
                if (s->aflag) {
5802
                    offset_addr = insn_get(env, s, OT_LONG);
B
bellard 已提交
5803
                } else {
5804
                    offset_addr = insn_get(env, s, OT_WORD);
B
bellard 已提交
5805 5806 5807
                }
                gen_op_movl_A0_im(offset_addr);
            }
B
bellard 已提交
5808
            gen_add_A0_ds_seg(s);
B
bellard 已提交
5809
            if ((b & 2) == 0) {
B
bellard 已提交
5810 5811
                gen_op_ld_T0_A0(ot + s->mem_index);
                gen_op_mov_reg_T0(ot, R_EAX);
B
bellard 已提交
5812
            } else {
B
bellard 已提交
5813 5814
                gen_op_mov_TN_reg(ot, 0, R_EAX);
                gen_op_st_T0_A0(ot + s->mem_index);
B
bellard 已提交
5815 5816 5817 5818
            }
        }
        break;
    case 0xd7: /* xlat */
B
bellard 已提交
5819
#ifdef TARGET_X86_64
5820
        if (s->aflag == 2) {
B
bellard 已提交
5821
            gen_op_movq_A0_reg(R_EBX);
5822 5823 5824
            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]);
5825
        } else
B
bellard 已提交
5826 5827
#endif
        {
B
bellard 已提交
5828
            gen_op_movl_A0_reg(R_EBX);
5829 5830 5831
            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 已提交
5832 5833
            if (s->aflag == 0)
                gen_op_andl_A0_ffff();
5834 5835
            else
                tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffffffff);
B
bellard 已提交
5836
        }
B
bellard 已提交
5837
        gen_add_A0_ds_seg(s);
B
bellard 已提交
5838 5839
        gen_op_ldu_T0_A0(OT_BYTE + s->mem_index);
        gen_op_mov_reg_T0(OT_BYTE, R_EAX);
B
bellard 已提交
5840 5841
        break;
    case 0xb0 ... 0xb7: /* mov R, Ib */
5842
        val = insn_get(env, s, OT_BYTE);
B
bellard 已提交
5843
        gen_op_movl_T0_im(val);
B
bellard 已提交
5844
        gen_op_mov_reg_T0(OT_BYTE, (b & 7) | REX_B(s));
B
bellard 已提交
5845 5846
        break;
    case 0xb8 ... 0xbf: /* mov R, Iv */
B
bellard 已提交
5847 5848 5849 5850
#ifdef TARGET_X86_64
        if (dflag == 2) {
            uint64_t tmp;
            /* 64 bit case */
5851
            tmp = cpu_ldq_code(env, s->pc);
B
bellard 已提交
5852 5853 5854
            s->pc += 8;
            reg = (b & 7) | REX_B(s);
            gen_movtl_T0_im(tmp);
B
bellard 已提交
5855
            gen_op_mov_reg_T0(OT_QUAD, reg);
5856
        } else
B
bellard 已提交
5857 5858 5859
#endif
        {
            ot = dflag ? OT_LONG : OT_WORD;
5860
            val = insn_get(env, s, ot);
B
bellard 已提交
5861 5862
            reg = (b & 7) | REX_B(s);
            gen_op_movl_T0_im(val);
B
bellard 已提交
5863
            gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
5864
        }
B
bellard 已提交
5865 5866 5867
        break;

    case 0x91 ... 0x97: /* xchg R, EAX */
R
Richard Henderson 已提交
5868
    do_xchg_reg_eax:
B
bellard 已提交
5869 5870
        ot = dflag + OT_WORD;
        reg = (b & 7) | REX_B(s);
B
bellard 已提交
5871 5872 5873 5874 5875 5876 5877
        rm = R_EAX;
        goto do_xchg_reg;
    case 0x86:
    case 0x87: /* xchg Ev, Gv */
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
5878
            ot = dflag + OT_WORD;
5879
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5880
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5881 5882
        mod = (modrm >> 6) & 3;
        if (mod == 3) {
B
bellard 已提交
5883
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
5884
        do_xchg_reg:
B
bellard 已提交
5885 5886 5887 5888
            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 已提交
5889
        } else {
5890
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
5891
            gen_op_mov_TN_reg(ot, 0, reg);
B
bellard 已提交
5892 5893
            /* for xchg, lock is implicit */
            if (!(prefixes & PREFIX_LOCK))
P
pbrook 已提交
5894
                gen_helper_lock();
B
bellard 已提交
5895 5896
            gen_op_ld_T1_A0(ot + s->mem_index);
            gen_op_st_T0_A0(ot + s->mem_index);
B
bellard 已提交
5897
            if (!(prefixes & PREFIX_LOCK))
P
pbrook 已提交
5898
                gen_helper_unlock();
B
bellard 已提交
5899
            gen_op_mov_reg_T1(ot, reg);
B
bellard 已提交
5900 5901 5902
        }
        break;
    case 0xc4: /* les Gv */
5903
        /* In CODE64 this is VEX3; see above.  */
B
bellard 已提交
5904 5905 5906
        op = R_ES;
        goto do_lxx;
    case 0xc5: /* lds Gv */
5907
        /* In CODE64 this is VEX2; see above.  */
B
bellard 已提交
5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919
        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;
5920
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5921
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5922 5923 5924
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
5925
        gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
5926
        gen_op_ld_T1_A0(ot + s->mem_index);
5927
        gen_add_A0_im(s, 1 << (ot - OT_WORD + 1));
B
bellard 已提交
5928
        /* load the segment first to handle exceptions properly */
B
bellard 已提交
5929
        gen_op_ldu_T0_A0(OT_WORD + s->mem_index);
B
bellard 已提交
5930 5931
        gen_movl_seg_T0(s, op, pc_start - s->cs_base);
        /* then put the data */
B
bellard 已提交
5932
        gen_op_mov_reg_T1(ot, reg);
B
bellard 已提交
5933
        if (s->is_jmp) {
B
bellard 已提交
5934
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5935 5936 5937
            gen_eob(s);
        }
        break;
5938

B
bellard 已提交
5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949
        /************************/
        /* shifts */
    case 0xc0:
    case 0xc1:
        /* shift Ev,Ib */
        shift = 2;
    grp2:
        {
            if ((b & 1) == 0)
                ot = OT_BYTE;
            else
B
bellard 已提交
5950
                ot = dflag + OT_WORD;
5951

5952
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5953 5954
            mod = (modrm >> 6) & 3;
            op = (modrm >> 3) & 7;
5955

B
bellard 已提交
5956
            if (mod != 3) {
B
bellard 已提交
5957 5958 5959
                if (shift == 2) {
                    s->rip_offset = 1;
                }
5960
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
5961 5962
                opreg = OR_TMP0;
            } else {
B
bellard 已提交
5963
                opreg = (modrm & 7) | REX_B(s);
B
bellard 已提交
5964 5965 5966 5967 5968 5969 5970
            }

            /* simpler op */
            if (shift == 0) {
                gen_shift(s, op, ot, opreg, OR_ECX);
            } else {
                if (shift == 2) {
5971
                    shift = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003
                }
                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 已提交
6004
        ot = dflag + OT_WORD;
6005
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
6006
        mod = (modrm >> 6) & 3;
B
bellard 已提交
6007 6008
        rm = (modrm & 7) | REX_B(s);
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
6009
        if (mod != 3) {
6010
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
6011
            opreg = OR_TMP0;
B
bellard 已提交
6012
        } else {
6013
            opreg = rm;
B
bellard 已提交
6014
        }
B
bellard 已提交
6015
        gen_op_mov_TN_reg(ot, 1, reg);
6016

B
bellard 已提交
6017
        if (shift) {
P
Paolo Bonzini 已提交
6018 6019 6020
            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 已提交
6021
        } else {
P
Paolo Bonzini 已提交
6022
            gen_shiftd_rm_T1(s, ot, opreg, op, cpu_regs[R_ECX]);
B
bellard 已提交
6023 6024 6025 6026 6027
        }
        break;

        /************************/
        /* floats */
6028
    case 0xd8 ... 0xdf:
B
bellard 已提交
6029 6030 6031 6032 6033 6034
        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;
        }
6035
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
6036 6037 6038 6039 6040
        mod = (modrm >> 6) & 3;
        rm = modrm & 7;
        op = ((b & 7) << 3) | ((modrm >> 3) & 7);
        if (mod != 3) {
            /* memory op */
6041
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052
            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 已提交
6053
                        gen_op_ld_T0_A0(OT_LONG + s->mem_index);
6054
                        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
6055
                        gen_helper_flds_FT0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
6056 6057
                        break;
                    case 1:
B
bellard 已提交
6058
                        gen_op_ld_T0_A0(OT_LONG + s->mem_index);
6059
                        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
6060
                        gen_helper_fildl_FT0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
6061 6062
                        break;
                    case 2:
6063
                        tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_A0, 
B
bellard 已提交
6064
                                          (s->mem_index >> 2) - 1);
B
Blue Swirl 已提交
6065
                        gen_helper_fldl_FT0(cpu_env, cpu_tmp1_i64);
B
bellard 已提交
6066 6067 6068
                        break;
                    case 3:
                    default:
B
bellard 已提交
6069
                        gen_op_lds_T0_A0(OT_WORD + s->mem_index);
6070
                        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
6071
                        gen_helper_fildl_FT0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
6072 6073
                        break;
                    }
6074

P
pbrook 已提交
6075
                    gen_helper_fp_arith_ST0_FT0(op1);
B
bellard 已提交
6076 6077
                    if (op1 == 3) {
                        /* fcomp needs pop */
B
Blue Swirl 已提交
6078
                        gen_helper_fpop(cpu_env);
B
bellard 已提交
6079 6080 6081 6082 6083 6084
                    }
                }
                break;
            case 0x08: /* flds */
            case 0x0a: /* fsts */
            case 0x0b: /* fstps */
B
bellard 已提交
6085 6086 6087
            case 0x18 ... 0x1b: /* fildl, fisttpl, fistl, fistpl */
            case 0x28 ... 0x2b: /* fldl, fisttpll, fstl, fstpl */
            case 0x38 ... 0x3b: /* filds, fisttps, fists, fistps */
B
bellard 已提交
6088 6089 6090 6091
                switch(op & 7) {
                case 0:
                    switch(op >> 4) {
                    case 0:
B
bellard 已提交
6092
                        gen_op_ld_T0_A0(OT_LONG + s->mem_index);
6093
                        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
6094
                        gen_helper_flds_ST0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
6095 6096
                        break;
                    case 1:
B
bellard 已提交
6097
                        gen_op_ld_T0_A0(OT_LONG + s->mem_index);
6098
                        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
6099
                        gen_helper_fildl_ST0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
6100 6101
                        break;
                    case 2:
6102
                        tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_A0, 
B
bellard 已提交
6103
                                          (s->mem_index >> 2) - 1);
B
Blue Swirl 已提交
6104
                        gen_helper_fldl_ST0(cpu_env, cpu_tmp1_i64);
B
bellard 已提交
6105 6106 6107
                        break;
                    case 3:
                    default:
B
bellard 已提交
6108
                        gen_op_lds_T0_A0(OT_WORD + s->mem_index);
6109
                        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
6110
                        gen_helper_fildl_ST0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
6111 6112 6113
                        break;
                    }
                    break;
B
bellard 已提交
6114
                case 1:
B
bellard 已提交
6115
                    /* XXX: the corresponding CPUID bit must be tested ! */
B
bellard 已提交
6116 6117
                    switch(op >> 4) {
                    case 1:
B
Blue Swirl 已提交
6118
                        gen_helper_fisttl_ST0(cpu_tmp2_i32, cpu_env);
6119
                        tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
6120
                        gen_op_st_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
6121 6122
                        break;
                    case 2:
B
Blue Swirl 已提交
6123
                        gen_helper_fisttll_ST0(cpu_tmp1_i64, cpu_env);
6124
                        tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_A0, 
B
bellard 已提交
6125
                                          (s->mem_index >> 2) - 1);
B
bellard 已提交
6126 6127 6128
                        break;
                    case 3:
                    default:
B
Blue Swirl 已提交
6129
                        gen_helper_fistt_ST0(cpu_tmp2_i32, cpu_env);
6130
                        tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
6131
                        gen_op_st_T0_A0(OT_WORD + s->mem_index);
B
bellard 已提交
6132
                        break;
B
bellard 已提交
6133
                    }
B
Blue Swirl 已提交
6134
                    gen_helper_fpop(cpu_env);
B
bellard 已提交
6135
                    break;
B
bellard 已提交
6136 6137 6138
                default:
                    switch(op >> 4) {
                    case 0:
B
Blue Swirl 已提交
6139
                        gen_helper_fsts_ST0(cpu_tmp2_i32, cpu_env);
6140
                        tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
6141
                        gen_op_st_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
6142 6143
                        break;
                    case 1:
B
Blue Swirl 已提交
6144
                        gen_helper_fistl_ST0(cpu_tmp2_i32, cpu_env);
6145
                        tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
6146
                        gen_op_st_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
6147 6148
                        break;
                    case 2:
B
Blue Swirl 已提交
6149
                        gen_helper_fstl_ST0(cpu_tmp1_i64, cpu_env);
6150
                        tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_A0, 
B
bellard 已提交
6151
                                          (s->mem_index >> 2) - 1);
B
bellard 已提交
6152 6153 6154
                        break;
                    case 3:
                    default:
B
Blue Swirl 已提交
6155
                        gen_helper_fist_ST0(cpu_tmp2_i32, cpu_env);
6156
                        tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
6157
                        gen_op_st_T0_A0(OT_WORD + s->mem_index);
B
bellard 已提交
6158 6159 6160
                        break;
                    }
                    if ((op & 7) == 3)
B
Blue Swirl 已提交
6161
                        gen_helper_fpop(cpu_env);
B
bellard 已提交
6162 6163 6164 6165
                    break;
                }
                break;
            case 0x0c: /* fldenv mem */
6166
                gen_update_cc_op(s);
B
bellard 已提交
6167
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6168
                gen_helper_fldenv(cpu_env, cpu_A0, tcg_const_i32(s->dflag));
B
bellard 已提交
6169 6170
                break;
            case 0x0d: /* fldcw mem */
B
bellard 已提交
6171
                gen_op_ld_T0_A0(OT_WORD + s->mem_index);
6172
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
6173
                gen_helper_fldcw(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
6174 6175
                break;
            case 0x0e: /* fnstenv mem */
6176
                gen_update_cc_op(s);
B
bellard 已提交
6177
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6178
                gen_helper_fstenv(cpu_env, cpu_A0, tcg_const_i32(s->dflag));
B
bellard 已提交
6179 6180
                break;
            case 0x0f: /* fnstcw mem */
B
Blue Swirl 已提交
6181
                gen_helper_fnstcw(cpu_tmp2_i32, cpu_env);
6182
                tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
6183
                gen_op_st_T0_A0(OT_WORD + s->mem_index);
B
bellard 已提交
6184 6185
                break;
            case 0x1d: /* fldt mem */
6186
                gen_update_cc_op(s);
B
bellard 已提交
6187
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6188
                gen_helper_fldt_ST0(cpu_env, cpu_A0);
B
bellard 已提交
6189 6190
                break;
            case 0x1f: /* fstpt mem */
6191
                gen_update_cc_op(s);
B
bellard 已提交
6192
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6193 6194
                gen_helper_fstt_ST0(cpu_env, cpu_A0);
                gen_helper_fpop(cpu_env);
B
bellard 已提交
6195 6196
                break;
            case 0x2c: /* frstor mem */
6197
                gen_update_cc_op(s);
B
bellard 已提交
6198
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6199
                gen_helper_frstor(cpu_env, cpu_A0, tcg_const_i32(s->dflag));
B
bellard 已提交
6200 6201
                break;
            case 0x2e: /* fnsave mem */
6202
                gen_update_cc_op(s);
B
bellard 已提交
6203
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6204
                gen_helper_fsave(cpu_env, cpu_A0, tcg_const_i32(s->dflag));
B
bellard 已提交
6205 6206
                break;
            case 0x2f: /* fnstsw mem */
B
Blue Swirl 已提交
6207
                gen_helper_fnstsw(cpu_tmp2_i32, cpu_env);
6208
                tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
6209
                gen_op_st_T0_A0(OT_WORD + s->mem_index);
B
bellard 已提交
6210 6211
                break;
            case 0x3c: /* fbld */
6212
                gen_update_cc_op(s);
B
bellard 已提交
6213
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6214
                gen_helper_fbld_ST0(cpu_env, cpu_A0);
B
bellard 已提交
6215 6216
                break;
            case 0x3e: /* fbstp */
6217
                gen_update_cc_op(s);
B
bellard 已提交
6218
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6219 6220
                gen_helper_fbst_ST0(cpu_env, cpu_A0);
                gen_helper_fpop(cpu_env);
B
bellard 已提交
6221 6222
                break;
            case 0x3d: /* fildll */
6223
                tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_A0, 
B
bellard 已提交
6224
                                  (s->mem_index >> 2) - 1);
B
Blue Swirl 已提交
6225
                gen_helper_fildll_ST0(cpu_env, cpu_tmp1_i64);
B
bellard 已提交
6226 6227
                break;
            case 0x3f: /* fistpll */
B
Blue Swirl 已提交
6228
                gen_helper_fistll_ST0(cpu_tmp1_i64, cpu_env);
6229
                tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_A0, 
B
bellard 已提交
6230
                                  (s->mem_index >> 2) - 1);
B
Blue Swirl 已提交
6231
                gen_helper_fpop(cpu_env);
B
bellard 已提交
6232 6233 6234 6235 6236 6237 6238 6239 6240 6241
                break;
            default:
                goto illegal_op;
            }
        } else {
            /* register float ops */
            opreg = rm;

            switch(op) {
            case 0x08: /* fld sti */
B
Blue Swirl 已提交
6242 6243 6244
                gen_helper_fpush(cpu_env);
                gen_helper_fmov_ST0_STN(cpu_env,
                                        tcg_const_i32((opreg + 1) & 7));
B
bellard 已提交
6245 6246
                break;
            case 0x09: /* fxchg sti */
B
bellard 已提交
6247 6248
            case 0x29: /* fxchg4 sti, undocumented op */
            case 0x39: /* fxchg7 sti, undocumented op */
B
Blue Swirl 已提交
6249
                gen_helper_fxchg_ST0_STN(cpu_env, tcg_const_i32(opreg));
B
bellard 已提交
6250 6251 6252 6253
                break;
            case 0x0a: /* grp d9/2 */
                switch(rm) {
                case 0: /* fnop */
6254
                    /* check exceptions (FreeBSD FPU probe) */
6255
                    gen_update_cc_op(s);
B
bellard 已提交
6256
                    gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6257
                    gen_helper_fwait(cpu_env);
B
bellard 已提交
6258 6259 6260 6261 6262 6263 6264 6265
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x0c: /* grp d9/4 */
                switch(rm) {
                case 0: /* fchs */
B
Blue Swirl 已提交
6266
                    gen_helper_fchs_ST0(cpu_env);
B
bellard 已提交
6267 6268
                    break;
                case 1: /* fabs */
B
Blue Swirl 已提交
6269
                    gen_helper_fabs_ST0(cpu_env);
B
bellard 已提交
6270 6271
                    break;
                case 4: /* ftst */
B
Blue Swirl 已提交
6272 6273
                    gen_helper_fldz_FT0(cpu_env);
                    gen_helper_fcom_ST0_FT0(cpu_env);
B
bellard 已提交
6274 6275
                    break;
                case 5: /* fxam */
B
Blue Swirl 已提交
6276
                    gen_helper_fxam_ST0(cpu_env);
B
bellard 已提交
6277 6278 6279 6280 6281 6282 6283 6284 6285
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x0d: /* grp d9/5 */
                {
                    switch(rm) {
                    case 0:
B
Blue Swirl 已提交
6286 6287
                        gen_helper_fpush(cpu_env);
                        gen_helper_fld1_ST0(cpu_env);
B
bellard 已提交
6288 6289
                        break;
                    case 1:
B
Blue Swirl 已提交
6290 6291
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldl2t_ST0(cpu_env);
B
bellard 已提交
6292 6293
                        break;
                    case 2:
B
Blue Swirl 已提交
6294 6295
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldl2e_ST0(cpu_env);
B
bellard 已提交
6296 6297
                        break;
                    case 3:
B
Blue Swirl 已提交
6298 6299
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldpi_ST0(cpu_env);
B
bellard 已提交
6300 6301
                        break;
                    case 4:
B
Blue Swirl 已提交
6302 6303
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldlg2_ST0(cpu_env);
B
bellard 已提交
6304 6305
                        break;
                    case 5:
B
Blue Swirl 已提交
6306 6307
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldln2_ST0(cpu_env);
B
bellard 已提交
6308 6309
                        break;
                    case 6:
B
Blue Swirl 已提交
6310 6311
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldz_ST0(cpu_env);
B
bellard 已提交
6312 6313 6314 6315 6316 6317 6318 6319 6320
                        break;
                    default:
                        goto illegal_op;
                    }
                }
                break;
            case 0x0e: /* grp d9/6 */
                switch(rm) {
                case 0: /* f2xm1 */
B
Blue Swirl 已提交
6321
                    gen_helper_f2xm1(cpu_env);
B
bellard 已提交
6322 6323
                    break;
                case 1: /* fyl2x */
B
Blue Swirl 已提交
6324
                    gen_helper_fyl2x(cpu_env);
B
bellard 已提交
6325 6326
                    break;
                case 2: /* fptan */
B
Blue Swirl 已提交
6327
                    gen_helper_fptan(cpu_env);
B
bellard 已提交
6328 6329
                    break;
                case 3: /* fpatan */
B
Blue Swirl 已提交
6330
                    gen_helper_fpatan(cpu_env);
B
bellard 已提交
6331 6332
                    break;
                case 4: /* fxtract */
B
Blue Swirl 已提交
6333
                    gen_helper_fxtract(cpu_env);
B
bellard 已提交
6334 6335
                    break;
                case 5: /* fprem1 */
B
Blue Swirl 已提交
6336
                    gen_helper_fprem1(cpu_env);
B
bellard 已提交
6337 6338
                    break;
                case 6: /* fdecstp */
B
Blue Swirl 已提交
6339
                    gen_helper_fdecstp(cpu_env);
B
bellard 已提交
6340 6341 6342
                    break;
                default:
                case 7: /* fincstp */
B
Blue Swirl 已提交
6343
                    gen_helper_fincstp(cpu_env);
B
bellard 已提交
6344 6345 6346 6347 6348 6349
                    break;
                }
                break;
            case 0x0f: /* grp d9/7 */
                switch(rm) {
                case 0: /* fprem */
B
Blue Swirl 已提交
6350
                    gen_helper_fprem(cpu_env);
B
bellard 已提交
6351 6352
                    break;
                case 1: /* fyl2xp1 */
B
Blue Swirl 已提交
6353
                    gen_helper_fyl2xp1(cpu_env);
B
bellard 已提交
6354 6355
                    break;
                case 2: /* fsqrt */
B
Blue Swirl 已提交
6356
                    gen_helper_fsqrt(cpu_env);
B
bellard 已提交
6357 6358
                    break;
                case 3: /* fsincos */
B
Blue Swirl 已提交
6359
                    gen_helper_fsincos(cpu_env);
B
bellard 已提交
6360 6361
                    break;
                case 5: /* fscale */
B
Blue Swirl 已提交
6362
                    gen_helper_fscale(cpu_env);
B
bellard 已提交
6363 6364
                    break;
                case 4: /* frndint */
B
Blue Swirl 已提交
6365
                    gen_helper_frndint(cpu_env);
B
bellard 已提交
6366 6367
                    break;
                case 6: /* fsin */
B
Blue Swirl 已提交
6368
                    gen_helper_fsin(cpu_env);
B
bellard 已提交
6369 6370 6371
                    break;
                default:
                case 7: /* fcos */
B
Blue Swirl 已提交
6372
                    gen_helper_fcos(cpu_env);
B
bellard 已提交
6373 6374 6375 6376 6377 6378 6379 6380
                    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;
6381

B
bellard 已提交
6382 6383
                    op1 = op & 7;
                    if (op >= 0x20) {
P
pbrook 已提交
6384
                        gen_helper_fp_arith_STN_ST0(op1, opreg);
B
bellard 已提交
6385
                        if (op >= 0x30)
B
Blue Swirl 已提交
6386
                            gen_helper_fpop(cpu_env);
B
bellard 已提交
6387
                    } else {
B
Blue Swirl 已提交
6388
                        gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
P
pbrook 已提交
6389
                        gen_helper_fp_arith_ST0_FT0(op1);
B
bellard 已提交
6390 6391 6392 6393
                    }
                }
                break;
            case 0x02: /* fcom */
B
bellard 已提交
6394
            case 0x22: /* fcom2, undocumented op */
B
Blue Swirl 已提交
6395 6396
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fcom_ST0_FT0(cpu_env);
B
bellard 已提交
6397 6398
                break;
            case 0x03: /* fcomp */
B
bellard 已提交
6399 6400
            case 0x23: /* fcomp3, undocumented op */
            case 0x32: /* fcomp5, undocumented op */
B
Blue Swirl 已提交
6401 6402 6403
                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 已提交
6404 6405 6406 6407
                break;
            case 0x15: /* da/5 */
                switch(rm) {
                case 1: /* fucompp */
B
Blue Swirl 已提交
6408 6409 6410 6411
                    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 已提交
6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423
                    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 已提交
6424
                    gen_helper_fclex(cpu_env);
B
bellard 已提交
6425 6426
                    break;
                case 3: /* fninit */
B
Blue Swirl 已提交
6427
                    gen_helper_fninit(cpu_env);
B
bellard 已提交
6428 6429 6430 6431 6432 6433 6434 6435
                    break;
                case 4: /* fsetpm (287 only, just do nop here) */
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x1d: /* fucomi */
6436
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6437 6438
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fucomi_ST0_FT0(cpu_env);
6439
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6440 6441
                break;
            case 0x1e: /* fcomi */
6442
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6443 6444
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fcomi_ST0_FT0(cpu_env);
6445
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6446
                break;
B
bellard 已提交
6447
            case 0x28: /* ffree sti */
B
Blue Swirl 已提交
6448
                gen_helper_ffree_STN(cpu_env, tcg_const_i32(opreg));
6449
                break;
B
bellard 已提交
6450
            case 0x2a: /* fst sti */
B
Blue Swirl 已提交
6451
                gen_helper_fmov_STN_ST0(cpu_env, tcg_const_i32(opreg));
B
bellard 已提交
6452 6453
                break;
            case 0x2b: /* fstp sti */
B
bellard 已提交
6454 6455 6456
            case 0x0b: /* fstp1 sti, undocumented op */
            case 0x3a: /* fstp8 sti, undocumented op */
            case 0x3b: /* fstp9 sti, undocumented op */
B
Blue Swirl 已提交
6457 6458
                gen_helper_fmov_STN_ST0(cpu_env, tcg_const_i32(opreg));
                gen_helper_fpop(cpu_env);
B
bellard 已提交
6459 6460
                break;
            case 0x2c: /* fucom st(i) */
B
Blue Swirl 已提交
6461 6462
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fucom_ST0_FT0(cpu_env);
B
bellard 已提交
6463 6464
                break;
            case 0x2d: /* fucomp st(i) */
B
Blue Swirl 已提交
6465 6466 6467
                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 已提交
6468 6469 6470 6471
                break;
            case 0x33: /* de/3 */
                switch(rm) {
                case 1: /* fcompp */
B
Blue Swirl 已提交
6472 6473 6474 6475
                    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 已提交
6476 6477 6478 6479 6480
                    break;
                default:
                    goto illegal_op;
                }
                break;
B
bellard 已提交
6481
            case 0x38: /* ffreep sti, undocumented op */
B
Blue Swirl 已提交
6482 6483
                gen_helper_ffree_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fpop(cpu_env);
B
bellard 已提交
6484
                break;
B
bellard 已提交
6485 6486 6487
            case 0x3c: /* df/4 */
                switch(rm) {
                case 0:
B
Blue Swirl 已提交
6488
                    gen_helper_fnstsw(cpu_tmp2_i32, cpu_env);
6489
                    tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
6490
                    gen_op_mov_reg_T0(OT_WORD, R_EAX);
B
bellard 已提交
6491 6492 6493 6494 6495 6496
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x3d: /* fucomip */
6497
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6498 6499 6500
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fucomi_ST0_FT0(cpu_env);
                gen_helper_fpop(cpu_env);
6501
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6502 6503
                break;
            case 0x3e: /* fcomip */
6504
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6505 6506 6507
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fcomi_ST0_FT0(cpu_env);
                gen_helper_fpop(cpu_env);
6508
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6509
                break;
6510 6511 6512
            case 0x10 ... 0x13: /* fcmovxx */
            case 0x18 ... 0x1b:
                {
B
bellard 已提交
6513
                    int op1, l1;
6514
                    static const uint8_t fcmov_cc[8] = {
6515 6516 6517 6518 6519
                        (JCC_B << 1),
                        (JCC_Z << 1),
                        (JCC_BE << 1),
                        (JCC_P << 1),
                    };
6520
                    op1 = fcmov_cc[op & 3] | (((op >> 3) & 1) ^ 1);
B
bellard 已提交
6521
                    l1 = gen_new_label();
6522
                    gen_jcc1_noeob(s, op1, l1);
B
Blue Swirl 已提交
6523
                    gen_helper_fmov_ST0_STN(cpu_env, tcg_const_i32(opreg));
B
bellard 已提交
6524
                    gen_set_label(l1);
6525 6526
                }
                break;
B
bellard 已提交
6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539
            default:
                goto illegal_op;
            }
        }
        break;
        /************************/
        /* string ops */

    case 0xa4: /* movsS */
    case 0xa5:
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
6540
            ot = dflag + OT_WORD;
B
bellard 已提交
6541 6542 6543 6544 6545 6546 6547

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

B
bellard 已提交
6549 6550 6551 6552 6553
    case 0xaa: /* stosS */
    case 0xab:
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
6554
            ot = dflag + OT_WORD;
B
bellard 已提交
6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566

        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 已提交
6567
            ot = dflag + OT_WORD;
B
bellard 已提交
6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578
        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 已提交
6579
            ot = dflag + OT_WORD;
B
bellard 已提交
6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593
        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 已提交
6594
            ot = dflag + OT_WORD;
B
bellard 已提交
6595 6596 6597 6598 6599 6600 6601 6602 6603 6604
        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:
6605 6606 6607 6608
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
B
bellard 已提交
6609
        gen_op_mov_TN_reg(OT_WORD, 0, R_EDX);
T
ths 已提交
6610
        gen_op_andl_T0_ffff();
6611 6612
        gen_check_io(s, ot, pc_start - s->cs_base, 
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes) | 4);
6613 6614
        if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
            gen_repz_ins(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
B
bellard 已提交
6615
        } else {
6616
            gen_ins(s, ot);
P
pbrook 已提交
6617 6618 6619
            if (use_icount) {
                gen_jmp(s, s->pc - s->cs_base);
            }
B
bellard 已提交
6620 6621 6622 6623
        }
        break;
    case 0x6e: /* outsS */
    case 0x6f:
6624 6625 6626 6627
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
B
bellard 已提交
6628
        gen_op_mov_TN_reg(OT_WORD, 0, R_EDX);
T
ths 已提交
6629
        gen_op_andl_T0_ffff();
6630 6631
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes) | 4);
6632 6633
        if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
            gen_repz_outs(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
B
bellard 已提交
6634
        } else {
6635
            gen_outs(s, ot);
P
pbrook 已提交
6636 6637 6638
            if (use_icount) {
                gen_jmp(s, s->pc - s->cs_base);
            }
B
bellard 已提交
6639 6640 6641 6642 6643
        }
        break;

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

B
bellard 已提交
6645 6646
    case 0xe4:
    case 0xe5:
6647 6648 6649 6650
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
6651
        val = cpu_ldub_code(env, s->pc++);
6652
        gen_op_movl_T0_im(val);
6653 6654
        gen_check_io(s, ot, pc_start - s->cs_base,
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
P
pbrook 已提交
6655 6656
        if (use_icount)
            gen_io_start();
6657
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
6658
        gen_helper_in_func(ot, cpu_T[1], cpu_tmp2_i32);
B
bellard 已提交
6659
        gen_op_mov_reg_T1(ot, R_EAX);
P
pbrook 已提交
6660 6661 6662 6663
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6664 6665 6666
        break;
    case 0xe6:
    case 0xe7:
6667 6668 6669 6670
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
6671
        val = cpu_ldub_code(env, s->pc++);
6672
        gen_op_movl_T0_im(val);
6673 6674
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes));
B
bellard 已提交
6675
        gen_op_mov_TN_reg(ot, 1, R_EAX);
6676

P
pbrook 已提交
6677 6678
        if (use_icount)
            gen_io_start();
6679 6680
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
        tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
P
pbrook 已提交
6681
        gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
P
pbrook 已提交
6682 6683 6684 6685
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6686 6687 6688
        break;
    case 0xec:
    case 0xed:
6689 6690 6691 6692
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
B
bellard 已提交
6693
        gen_op_mov_TN_reg(OT_WORD, 0, R_EDX);
6694
        gen_op_andl_T0_ffff();
6695 6696
        gen_check_io(s, ot, pc_start - s->cs_base,
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
P
pbrook 已提交
6697 6698
        if (use_icount)
            gen_io_start();
6699
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
6700
        gen_helper_in_func(ot, cpu_T[1], cpu_tmp2_i32);
B
bellard 已提交
6701
        gen_op_mov_reg_T1(ot, R_EAX);
P
pbrook 已提交
6702 6703 6704 6705
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6706 6707 6708
        break;
    case 0xee:
    case 0xef:
6709 6710 6711 6712
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
B
bellard 已提交
6713
        gen_op_mov_TN_reg(OT_WORD, 0, R_EDX);
6714
        gen_op_andl_T0_ffff();
6715 6716
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes));
B
bellard 已提交
6717
        gen_op_mov_TN_reg(ot, 1, R_EAX);
6718

P
pbrook 已提交
6719 6720
        if (use_icount)
            gen_io_start();
6721 6722
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
        tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
P
pbrook 已提交
6723
        gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
P
pbrook 已提交
6724 6725 6726 6727
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6728 6729 6730 6731 6732
        break;

        /************************/
        /* control */
    case 0xc2: /* ret im */
6733
        val = cpu_ldsw_code(env, s->pc);
B
bellard 已提交
6734 6735
        s->pc += 2;
        gen_pop_T0(s);
6736 6737
        if (CODE64(s) && s->dflag)
            s->dflag = 2;
B
bellard 已提交
6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752
        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 */
6753
        val = cpu_ldsw_code(env, s->pc);
B
bellard 已提交
6754 6755 6756
        s->pc += 2;
    do_lret:
        if (s->pe && !s->vm86) {
6757
            gen_update_cc_op(s);
B
bellard 已提交
6758
            gen_jmp_im(pc_start - s->cs_base);
6759
            gen_helper_lret_protected(cpu_env, tcg_const_i32(s->dflag),
P
pbrook 已提交
6760
                                      tcg_const_i32(val));
B
bellard 已提交
6761 6762 6763
        } else {
            gen_stack_A0(s);
            /* pop offset */
B
bellard 已提交
6764
            gen_op_ld_T0_A0(1 + s->dflag + s->mem_index);
B
bellard 已提交
6765 6766 6767 6768 6769 6770 6771
            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 已提交
6772
            gen_op_ld_T0_A0(1 + s->dflag + s->mem_index);
6773
            gen_op_movl_seg_T0_vm(R_CS);
B
bellard 已提交
6774 6775 6776 6777 6778 6779 6780 6781 6782
            /* 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 已提交
6783
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_IRET);
B
bellard 已提交
6784 6785
        if (!s->pe) {
            /* real mode */
6786
            gen_helper_iret_real(cpu_env, tcg_const_i32(s->dflag));
6787
            set_cc_op(s, CC_OP_EFLAGS);
6788 6789 6790 6791
        } else if (s->vm86) {
            if (s->iopl != 3) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
6792
                gen_helper_iret_real(cpu_env, tcg_const_i32(s->dflag));
6793
                set_cc_op(s, CC_OP_EFLAGS);
6794
            }
B
bellard 已提交
6795
        } else {
6796
            gen_update_cc_op(s);
B
bellard 已提交
6797
            gen_jmp_im(pc_start - s->cs_base);
6798
            gen_helper_iret_protected(cpu_env, tcg_const_i32(s->dflag),
P
pbrook 已提交
6799
                                      tcg_const_i32(s->pc - s->cs_base));
6800
            set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6801 6802 6803 6804 6805
        }
        gen_eob(s);
        break;
    case 0xe8: /* call im */
        {
B
bellard 已提交
6806
            if (dflag)
6807
                tval = (int32_t)insn_get(env, s, OT_LONG);
B
bellard 已提交
6808
            else
6809
                tval = (int16_t)insn_get(env, s, OT_WORD);
B
bellard 已提交
6810
            next_eip = s->pc - s->cs_base;
B
bellard 已提交
6811
            tval += next_eip;
B
bellard 已提交
6812
            if (s->dflag == 0)
B
bellard 已提交
6813
                tval &= 0xffff;
6814 6815
            else if(!CODE64(s))
                tval &= 0xffffffff;
B
bellard 已提交
6816
            gen_movtl_T0_im(next_eip);
B
bellard 已提交
6817
            gen_push_T0(s);
B
bellard 已提交
6818
            gen_jmp(s, tval);
B
bellard 已提交
6819 6820 6821 6822 6823
        }
        break;
    case 0x9a: /* lcall im */
        {
            unsigned int selector, offset;
6824

B
bellard 已提交
6825 6826
            if (CODE64(s))
                goto illegal_op;
B
bellard 已提交
6827
            ot = dflag ? OT_LONG : OT_WORD;
6828 6829
            offset = insn_get(env, s, ot);
            selector = insn_get(env, s, OT_WORD);
6830

B
bellard 已提交
6831
            gen_op_movl_T0_im(selector);
B
bellard 已提交
6832
            gen_op_movl_T1_imu(offset);
B
bellard 已提交
6833 6834
        }
        goto do_lcall;
B
bellard 已提交
6835
    case 0xe9: /* jmp im */
B
bellard 已提交
6836
        if (dflag)
6837
            tval = (int32_t)insn_get(env, s, OT_LONG);
B
bellard 已提交
6838
        else
6839
            tval = (int16_t)insn_get(env, s, OT_WORD);
B
bellard 已提交
6840
        tval += s->pc - s->cs_base;
B
bellard 已提交
6841
        if (s->dflag == 0)
B
bellard 已提交
6842
            tval &= 0xffff;
6843 6844
        else if(!CODE64(s))
            tval &= 0xffffffff;
B
bellard 已提交
6845
        gen_jmp(s, tval);
B
bellard 已提交
6846 6847 6848 6849 6850
        break;
    case 0xea: /* ljmp im */
        {
            unsigned int selector, offset;

B
bellard 已提交
6851 6852
            if (CODE64(s))
                goto illegal_op;
B
bellard 已提交
6853
            ot = dflag ? OT_LONG : OT_WORD;
6854 6855
            offset = insn_get(env, s, ot);
            selector = insn_get(env, s, OT_WORD);
6856

B
bellard 已提交
6857
            gen_op_movl_T0_im(selector);
B
bellard 已提交
6858
            gen_op_movl_T1_imu(offset);
B
bellard 已提交
6859 6860 6861
        }
        goto do_ljmp;
    case 0xeb: /* jmp Jb */
6862
        tval = (int8_t)insn_get(env, s, OT_BYTE);
B
bellard 已提交
6863
        tval += s->pc - s->cs_base;
B
bellard 已提交
6864
        if (s->dflag == 0)
B
bellard 已提交
6865 6866
            tval &= 0xffff;
        gen_jmp(s, tval);
B
bellard 已提交
6867 6868
        break;
    case 0x70 ... 0x7f: /* jcc Jb */
6869
        tval = (int8_t)insn_get(env, s, OT_BYTE);
B
bellard 已提交
6870 6871 6872
        goto do_jcc;
    case 0x180 ... 0x18f: /* jcc Jv */
        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 6877 6878
        }
    do_jcc:
        next_eip = s->pc - s->cs_base;
B
bellard 已提交
6879
        tval += next_eip;
B
bellard 已提交
6880
        if (s->dflag == 0)
B
bellard 已提交
6881 6882
            tval &= 0xffff;
        gen_jcc(s, b, tval, next_eip);
B
bellard 已提交
6883 6884 6885
        break;

    case 0x190 ... 0x19f: /* setcc Gv */
6886
        modrm = cpu_ldub_code(env, s->pc++);
6887
        gen_setcc1(s, b, cpu_T[0]);
6888
        gen_ldst_modrm(env, s, modrm, OT_BYTE, OR_TMP0, 1);
B
bellard 已提交
6889 6890
        break;
    case 0x140 ... 0x14f: /* cmov Gv, Ev */
6891 6892 6893 6894
        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 已提交
6895
        break;
6896

B
bellard 已提交
6897 6898 6899
        /************************/
        /* flags */
    case 0x9c: /* pushf */
B
bellard 已提交
6900
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_PUSHF);
B
bellard 已提交
6901 6902 6903
        if (s->vm86 && s->iopl != 3) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
6904
            gen_update_cc_op(s);
6905
            gen_helper_read_eflags(cpu_T[0], cpu_env);
B
bellard 已提交
6906 6907 6908 6909
            gen_push_T0(s);
        }
        break;
    case 0x9d: /* popf */
B
bellard 已提交
6910
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_POPF);
B
bellard 已提交
6911 6912 6913 6914 6915 6916
        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) {
6917 6918 6919 6920 6921
                    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 已提交
6922
                } else {
6923 6924 6925 6926 6927
                    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 已提交
6928 6929
                }
            } else {
B
bellard 已提交
6930 6931
                if (s->cpl <= s->iopl) {
                    if (s->dflag) {
6932 6933 6934 6935 6936 6937
                        gen_helper_write_eflags(cpu_env, cpu_T[0],
                                                tcg_const_i32((TF_MASK |
                                                               AC_MASK |
                                                               ID_MASK |
                                                               NT_MASK |
                                                               IF_MASK)));
B
bellard 已提交
6938
                    } else {
6939 6940 6941 6942 6943 6944 6945
                        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 已提交
6946
                    }
B
bellard 已提交
6947
                } else {
B
bellard 已提交
6948
                    if (s->dflag) {
6949 6950 6951
                        gen_helper_write_eflags(cpu_env, cpu_T[0],
                                           tcg_const_i32((TF_MASK | AC_MASK |
                                                          ID_MASK | NT_MASK)));
B
bellard 已提交
6952
                    } else {
6953 6954 6955 6956
                        gen_helper_write_eflags(cpu_env, cpu_T[0],
                                           tcg_const_i32((TF_MASK | AC_MASK |
                                                          ID_MASK | NT_MASK)
                                                         & 0xffff));
B
bellard 已提交
6957
                    }
B
bellard 已提交
6958 6959 6960
                }
            }
            gen_pop_update(s);
6961
            set_cc_op(s, CC_OP_EFLAGS);
H
H. Peter Anvin 已提交
6962
            /* abort translation because TF/AC flag may change */
B
bellard 已提交
6963
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
6964 6965 6966 6967
            gen_eob(s);
        }
        break;
    case 0x9e: /* sahf */
B
bellard 已提交
6968
        if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM))
B
bellard 已提交
6969
            goto illegal_op;
B
bellard 已提交
6970
        gen_op_mov_TN_reg(OT_BYTE, 0, R_AH);
6971
        gen_compute_eflags(s);
6972 6973 6974
        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 已提交
6975 6976
        break;
    case 0x9f: /* lahf */
B
bellard 已提交
6977
        if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM))
B
bellard 已提交
6978
            goto illegal_op;
6979
        gen_compute_eflags(s);
6980
        /* Note: gen_compute_eflags() only gives the condition codes */
6981
        tcg_gen_ori_tl(cpu_T[0], cpu_cc_src, 0x02);
B
bellard 已提交
6982
        gen_op_mov_reg_T0(OT_BYTE, R_AH);
B
bellard 已提交
6983 6984
        break;
    case 0xf5: /* cmc */
6985
        gen_compute_eflags(s);
6986
        tcg_gen_xori_tl(cpu_cc_src, cpu_cc_src, CC_C);
B
bellard 已提交
6987 6988
        break;
    case 0xf8: /* clc */
6989
        gen_compute_eflags(s);
6990
        tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~CC_C);
B
bellard 已提交
6991 6992
        break;
    case 0xf9: /* stc */
6993
        gen_compute_eflags(s);
6994
        tcg_gen_ori_tl(cpu_cc_src, cpu_cc_src, CC_C);
B
bellard 已提交
6995 6996
        break;
    case 0xfc: /* cld */
6997
        tcg_gen_movi_i32(cpu_tmp2_i32, 1);
6998
        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, offsetof(CPUX86State, df));
B
bellard 已提交
6999 7000
        break;
    case 0xfd: /* std */
7001
        tcg_gen_movi_i32(cpu_tmp2_i32, -1);
7002
        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, offsetof(CPUX86State, df));
B
bellard 已提交
7003 7004 7005 7006 7007
        break;

        /************************/
        /* bit operations */
    case 0x1ba: /* bt/bts/btr/btc Gv, im */
B
bellard 已提交
7008
        ot = dflag + OT_WORD;
7009
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7010
        op = (modrm >> 3) & 7;
B
bellard 已提交
7011
        mod = (modrm >> 6) & 3;
B
bellard 已提交
7012
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
7013
        if (mod != 3) {
B
bellard 已提交
7014
            s->rip_offset = 1;
7015
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
7016
            gen_op_ld_T0_A0(ot + s->mem_index);
B
bellard 已提交
7017
        } else {
B
bellard 已提交
7018
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
7019 7020
        }
        /* load shift */
7021
        val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7022 7023 7024 7025
        gen_op_movl_T1_im(val);
        if (op < 4)
            goto illegal_op;
        op -= 4;
B
bellard 已提交
7026
        goto bt_op;
B
bellard 已提交
7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038
    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 已提交
7039
        ot = dflag + OT_WORD;
7040
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7041
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
7042
        mod = (modrm >> 6) & 3;
B
bellard 已提交
7043
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
7044
        gen_op_mov_TN_reg(OT_LONG, 1, reg);
B
bellard 已提交
7045
        if (mod != 3) {
7046
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
7047
            /* specific case: we need to add a displacement */
B
bellard 已提交
7048 7049 7050 7051
            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 已提交
7052
            gen_op_ld_T0_A0(ot + s->mem_index);
B
bellard 已提交
7053
        } else {
B
bellard 已提交
7054
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
7055
        }
B
bellard 已提交
7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083
    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;
        }
7084
        set_cc_op(s, CC_OP_SARB + ot);
B
bellard 已提交
7085 7086
        if (op != 0) {
            if (mod != 3)
B
bellard 已提交
7087
                gen_op_st_T0_A0(ot + s->mem_index);
B
bellard 已提交
7088
            else
B
bellard 已提交
7089
                gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
7090 7091
            tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
            tcg_gen_movi_tl(cpu_cc_dst, 0);
B
bellard 已提交
7092 7093
        }
        break;
7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113
    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 已提交
7114
            } else {
7115 7116 7117 7118 7119
                /* 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 已提交
7120
            }
7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143
            /* 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 已提交
7144
        }
7145
        gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
7146 7147 7148 7149
        break;
        /************************/
        /* bcd */
    case 0x27: /* daa */
B
bellard 已提交
7150 7151
        if (CODE64(s))
            goto illegal_op;
7152
        gen_update_cc_op(s);
7153
        gen_helper_daa(cpu_env);
7154
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
7155 7156
        break;
    case 0x2f: /* das */
B
bellard 已提交
7157 7158
        if (CODE64(s))
            goto illegal_op;
7159
        gen_update_cc_op(s);
7160
        gen_helper_das(cpu_env);
7161
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
7162 7163
        break;
    case 0x37: /* aaa */
B
bellard 已提交
7164 7165
        if (CODE64(s))
            goto illegal_op;
7166
        gen_update_cc_op(s);
7167
        gen_helper_aaa(cpu_env);
7168
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
7169 7170
        break;
    case 0x3f: /* aas */
B
bellard 已提交
7171 7172
        if (CODE64(s))
            goto illegal_op;
7173
        gen_update_cc_op(s);
7174
        gen_helper_aas(cpu_env);
7175
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
7176 7177
        break;
    case 0xd4: /* aam */
B
bellard 已提交
7178 7179
        if (CODE64(s))
            goto illegal_op;
7180
        val = cpu_ldub_code(env, s->pc++);
7181 7182 7183
        if (val == 0) {
            gen_exception(s, EXCP00_DIVZ, pc_start - s->cs_base);
        } else {
7184
            gen_helper_aam(cpu_env, tcg_const_i32(val));
7185
            set_cc_op(s, CC_OP_LOGICB);
7186
        }
B
bellard 已提交
7187 7188
        break;
    case 0xd5: /* aad */
B
bellard 已提交
7189 7190
        if (CODE64(s))
            goto illegal_op;
7191
        val = cpu_ldub_code(env, s->pc++);
7192
        gen_helper_aad(cpu_env, tcg_const_i32(val));
7193
        set_cc_op(s, CC_OP_LOGICB);
B
bellard 已提交
7194 7195 7196 7197
        break;
        /************************/
        /* misc */
    case 0x90: /* nop */
7198
        /* XXX: correct lock test for all insn */
R
Richard Henderson 已提交
7199
        if (prefixes & PREFIX_LOCK) {
7200
            goto illegal_op;
R
Richard Henderson 已提交
7201 7202 7203 7204 7205
        }
        /* 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 已提交
7206 7207 7208
        if (prefixes & PREFIX_REPZ) {
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_PAUSE);
        }
B
bellard 已提交
7209 7210
        break;
    case 0x9b: /* fwait */
7211
        if ((s->flags & (HF_MP_MASK | HF_TS_MASK)) ==
B
bellard 已提交
7212 7213
            (HF_MP_MASK | HF_TS_MASK)) {
            gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
B
bellard 已提交
7214
        } else {
7215
            gen_update_cc_op(s);
B
bellard 已提交
7216
            gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7217
            gen_helper_fwait(cpu_env);
B
bellard 已提交
7218
        }
B
bellard 已提交
7219 7220 7221 7222 7223
        break;
    case 0xcc: /* int3 */
        gen_interrupt(s, EXCP03_INT3, pc_start - s->cs_base, s->pc - s->cs_base);
        break;
    case 0xcd: /* int N */
7224
        val = cpu_ldub_code(env, s->pc++);
7225
        if (s->vm86 && s->iopl != 3) {
7226
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7227 7228 7229
        } else {
            gen_interrupt(s, val, pc_start - s->cs_base, s->pc - s->cs_base);
        }
B
bellard 已提交
7230 7231
        break;
    case 0xce: /* into */
B
bellard 已提交
7232 7233
        if (CODE64(s))
            goto illegal_op;
7234
        gen_update_cc_op(s);
7235
        gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7236
        gen_helper_into(cpu_env, tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
7237
        break;
A
aurel32 已提交
7238
#ifdef WANT_ICEBP
B
bellard 已提交
7239
    case 0xf1: /* icebp (undocumented, exits to external debugger) */
B
bellard 已提交
7240
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_ICEBP);
7241
#if 1
B
bellard 已提交
7242
        gen_debug(s, pc_start - s->cs_base);
7243 7244
#else
        /* start debug */
7245
        tb_flush(env);
7246
        qemu_set_log(CPU_LOG_INT | CPU_LOG_TB_IN_ASM);
7247
#endif
B
bellard 已提交
7248
        break;
A
aurel32 已提交
7249
#endif
B
bellard 已提交
7250 7251 7252
    case 0xfa: /* cli */
        if (!s->vm86) {
            if (s->cpl <= s->iopl) {
7253
                gen_helper_cli(cpu_env);
B
bellard 已提交
7254 7255 7256 7257 7258
            } else {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            }
        } else {
            if (s->iopl == 3) {
7259
                gen_helper_cli(cpu_env);
B
bellard 已提交
7260 7261 7262 7263 7264 7265 7266 7267 7268
            } 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:
7269
                gen_helper_sti(cpu_env);
B
bellard 已提交
7270
                /* interruptions are enabled only the first insn after sti */
7271 7272 7273
                /* If several instructions disable interrupts, only the
                   _first_ does it */
                if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
7274
                    gen_helper_set_inhibit_irq(cpu_env);
B
bellard 已提交
7275
                /* give a chance to handle pending irqs */
B
bellard 已提交
7276
                gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289
                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 已提交
7290 7291
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
7292
        ot = dflag ? OT_LONG : OT_WORD;
7293
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7294 7295 7296 7297
        reg = (modrm >> 3) & 7;
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
B
bellard 已提交
7298
        gen_op_mov_TN_reg(ot, 0, reg);
7299
        gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
7300
        gen_jmp_im(pc_start - s->cs_base);
7301
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
7302 7303 7304 7305 7306
        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 已提交
7307 7308
        break;
    case 0x1c8 ... 0x1cf: /* bswap reg */
B
bellard 已提交
7309 7310 7311
        reg = (b & 7) | REX_B(s);
#ifdef TARGET_X86_64
        if (dflag == 2) {
B
bellard 已提交
7312
            gen_op_mov_TN_reg(OT_QUAD, 0, reg);
A
aurel32 已提交
7313
            tcg_gen_bswap64_i64(cpu_T[0], cpu_T[0]);
B
bellard 已提交
7314
            gen_op_mov_reg_T0(OT_QUAD, reg);
7315
        } else
7316
#endif
B
bellard 已提交
7317 7318
        {
            gen_op_mov_TN_reg(OT_LONG, 0, reg);
7319 7320
            tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
            tcg_gen_bswap32_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
7321
            gen_op_mov_reg_T0(OT_LONG, reg);
B
bellard 已提交
7322
        }
B
bellard 已提交
7323 7324
        break;
    case 0xd6: /* salc */
B
bellard 已提交
7325 7326
        if (CODE64(s))
            goto illegal_op;
7327
        gen_compute_eflags_c(s, cpu_T[0]);
7328 7329
        tcg_gen_neg_tl(cpu_T[0], cpu_T[0]);
        gen_op_mov_reg_T0(OT_BYTE, R_EAX);
B
bellard 已提交
7330 7331 7332 7333 7334
        break;
    case 0xe0: /* loopnz */
    case 0xe1: /* loopz */
    case 0xe2: /* loop */
    case 0xe3: /* jecxz */
B
bellard 已提交
7335
        {
7336
            int l1, l2, l3;
B
bellard 已提交
7337

7338
            tval = (int8_t)insn_get(env, s, OT_BYTE);
B
bellard 已提交
7339 7340 7341 7342
            next_eip = s->pc - s->cs_base;
            tval += next_eip;
            if (s->dflag == 0)
                tval &= 0xffff;
7343

B
bellard 已提交
7344 7345
            l1 = gen_new_label();
            l2 = gen_new_label();
7346
            l3 = gen_new_label();
B
bellard 已提交
7347
            b &= 3;
7348 7349 7350 7351 7352
            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);
7353
                gen_jcc1(s, (JCC_Z << 1) | (b ^ 1), l1);
7354 7355 7356 7357 7358 7359 7360 7361 7362
                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 已提交
7363 7364
            }

7365
            gen_set_label(l3);
B
bellard 已提交
7366
            gen_jmp_im(next_eip);
7367
            tcg_gen_br(l2);
7368

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

7819
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7820 7821 7822
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
            rm = (modrm & 7) | REX_B(s);
7823

B
bellard 已提交
7824
            if (mod == 3) {
B
bellard 已提交
7825
                gen_op_mov_TN_reg(OT_LONG, 0, rm);
B
bellard 已提交
7826 7827
                /* sign extend */
                if (d_ot == OT_QUAD)
B
bellard 已提交
7828
                    tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
7829
                gen_op_mov_reg_T0(d_ot, reg);
B
bellard 已提交
7830
            } else {
7831
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
7832
                if (d_ot == OT_QUAD) {
B
bellard 已提交
7833
                    gen_op_lds_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
7834
                } else {
B
bellard 已提交
7835
                    gen_op_ld_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
7836
                }
B
bellard 已提交
7837
                gen_op_mov_reg_T0(d_ot, reg);
B
bellard 已提交
7838
            }
7839
        } else
B
bellard 已提交
7840 7841
#endif
        {
7842
            int label1;
L
Laurent Desnogues 已提交
7843
            TCGv t0, t1, t2, a0;
7844

B
bellard 已提交
7845 7846
            if (!s->pe || s->vm86)
                goto illegal_op;
P
pbrook 已提交
7847 7848 7849
            t0 = tcg_temp_local_new();
            t1 = tcg_temp_local_new();
            t2 = tcg_temp_local_new();
7850
            ot = OT_WORD;
7851
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7852 7853 7854 7855
            reg = (modrm >> 3) & 7;
            mod = (modrm >> 6) & 3;
            rm = modrm & 7;
            if (mod != 3) {
7856
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
7857
                gen_op_ld_v(ot + s->mem_index, t0, cpu_A0);
L
Laurent Desnogues 已提交
7858 7859
                a0 = tcg_temp_local_new();
                tcg_gen_mov_tl(a0, cpu_A0);
B
bellard 已提交
7860
            } else {
7861
                gen_op_mov_v_reg(ot, t0, rm);
L
Laurent Desnogues 已提交
7862
                TCGV_UNUSED(a0);
B
bellard 已提交
7863
            }
7864 7865 7866 7867
            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);
7868
            label1 = gen_new_label();
7869 7870 7871 7872
            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);
7873
            gen_set_label(label1);
B
bellard 已提交
7874
            if (mod != 3) {
L
Laurent Desnogues 已提交
7875 7876 7877
                gen_op_st_v(ot + s->mem_index, t0, a0);
                tcg_temp_free(a0);
           } else {
7878
                gen_op_mov_reg_v(ot, rm, t0);
B
bellard 已提交
7879
            }
7880
            gen_compute_eflags(s);
7881
            tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~CC_Z);
7882 7883 7884 7885
            tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, t2);
            tcg_temp_free(t0);
            tcg_temp_free(t1);
            tcg_temp_free(t2);
7886 7887
        }
        break;
B
bellard 已提交
7888 7889
    case 0x102: /* lar */
    case 0x103: /* lsl */
7890 7891
        {
            int label1;
7892
            TCGv t0;
7893 7894 7895
            if (!s->pe || s->vm86)
                goto illegal_op;
            ot = dflag ? OT_LONG : OT_WORD;
7896
            modrm = cpu_ldub_code(env, s->pc++);
7897
            reg = ((modrm >> 3) & 7) | rex_r;
7898
            gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
P
pbrook 已提交
7899
            t0 = tcg_temp_local_new();
7900
            gen_update_cc_op(s);
7901 7902 7903 7904 7905
            if (b == 0x102) {
                gen_helper_lar(t0, cpu_env, cpu_T[0]);
            } else {
                gen_helper_lsl(t0, cpu_env, cpu_T[0]);
            }
7906 7907
            tcg_gen_andi_tl(cpu_tmp0, cpu_cc_src, CC_Z);
            label1 = gen_new_label();
P
pbrook 已提交
7908
            tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, label1);
7909
            gen_op_mov_reg_v(ot, reg, t0);
7910
            gen_set_label(label1);
7911
            set_cc_op(s, CC_OP_EFLAGS);
7912
            tcg_temp_free(t0);
7913
        }
B
bellard 已提交
7914 7915
        break;
    case 0x118:
7916
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7917 7918 7919 7920 7921 7922 7923 7924 7925
        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;
7926
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
7927 7928
            /* nothing more to do */
            break;
B
bellard 已提交
7929
        default: /* nop (multi byte) */
7930
            gen_nop_modrm(env, s, modrm);
B
bellard 已提交
7931
            break;
B
bellard 已提交
7932 7933
        }
        break;
B
bellard 已提交
7934
    case 0x119 ... 0x11f: /* nop (multi byte) */
7935 7936
        modrm = cpu_ldub_code(env, s->pc++);
        gen_nop_modrm(env, s, modrm);
B
bellard 已提交
7937
        break;
B
bellard 已提交
7938 7939 7940 7941 7942
    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 {
7943
            modrm = cpu_ldub_code(env, s->pc++);
7944 7945 7946 7947 7948
            /* 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 已提交
7949 7950 7951 7952 7953 7954
            rm = (modrm & 7) | REX_B(s);
            reg = ((modrm >> 3) & 7) | rex_r;
            if (CODE64(s))
                ot = OT_QUAD;
            else
                ot = OT_LONG;
7955 7956 7957 7958
            if ((prefixes & PREFIX_LOCK) && (reg == 0) &&
                (s->cpuid_ext3_features & CPUID_EXT3_CR8LEG)) {
                reg = 8;
            }
B
bellard 已提交
7959 7960 7961 7962 7963
            switch(reg) {
            case 0:
            case 2:
            case 3:
            case 4:
B
bellard 已提交
7964
            case 8:
7965
                gen_update_cc_op(s);
B
bellard 已提交
7966
                gen_jmp_im(pc_start - s->cs_base);
B
bellard 已提交
7967
                if (b & 2) {
B
bellard 已提交
7968
                    gen_op_mov_TN_reg(ot, 0, rm);
B
Blue Swirl 已提交
7969 7970
                    gen_helper_write_crN(cpu_env, tcg_const_i32(reg),
                                         cpu_T[0]);
B
bellard 已提交
7971
                    gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
7972 7973
                    gen_eob(s);
                } else {
B
Blue Swirl 已提交
7974
                    gen_helper_read_crN(cpu_T[0], cpu_env, tcg_const_i32(reg));
B
bellard 已提交
7975
                    gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987
                }
                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 {
7988
            modrm = cpu_ldub_code(env, s->pc++);
7989 7990 7991 7992 7993
            /* 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 已提交
7994 7995 7996 7997 7998 7999
            rm = (modrm & 7) | REX_B(s);
            reg = ((modrm >> 3) & 7) | rex_r;
            if (CODE64(s))
                ot = OT_QUAD;
            else
                ot = OT_LONG;
B
bellard 已提交
8000
            /* XXX: do it dynamically with CR4.DE bit */
B
bellard 已提交
8001
            if (reg == 4 || reg == 5 || reg >= 8)
B
bellard 已提交
8002 8003
                goto illegal_op;
            if (b & 2) {
T
ths 已提交
8004
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_DR0 + reg);
B
bellard 已提交
8005
                gen_op_mov_TN_reg(ot, 0, rm);
B
Blue Swirl 已提交
8006
                gen_helper_movl_drN_T0(cpu_env, tcg_const_i32(reg), cpu_T[0]);
B
bellard 已提交
8007
                gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
8008 8009
                gen_eob(s);
            } else {
T
ths 已提交
8010
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_READ_DR0 + reg);
B
bellard 已提交
8011
                tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,dr[reg]));
B
bellard 已提交
8012
                gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
8013 8014 8015 8016 8017 8018 8019
            }
        }
        break;
    case 0x106: /* clts */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
T
ths 已提交
8020
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_CR0);
8021
            gen_helper_clts(cpu_env);
B
bellard 已提交
8022
            /* abort block because static cpu state changed */
B
bellard 已提交
8023
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
8024
            gen_eob(s);
B
bellard 已提交
8025 8026
        }
        break;
B
balrog 已提交
8027
    /* MMX/3DNow!/SSE/SSE2/SSE3/SSSE3/SSE4 support */
B
bellard 已提交
8028 8029
    case 0x1c3: /* MOVNTI reg, mem */
        if (!(s->cpuid_features & CPUID_SSE2))
B
bellard 已提交
8030
            goto illegal_op;
B
bellard 已提交
8031
        ot = s->dflag == 2 ? OT_QUAD : OT_LONG;
8032
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
8033 8034 8035 8036 8037
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
        reg = ((modrm >> 3) & 7) | rex_r;
        /* generate a generic store */
8038
        gen_ldst_modrm(env, s, modrm, ot, reg, 1);
B
bellard 已提交
8039
        break;
B
bellard 已提交
8040
    case 0x1ae:
8041
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
8042 8043 8044 8045
        mod = (modrm >> 6) & 3;
        op = (modrm >> 3) & 7;
        switch(op) {
        case 0: /* fxsave */
8046
            if (mod == 3 || !(s->cpuid_features & CPUID_FXSR) ||
8047
                (s->prefix & PREFIX_LOCK))
B
bellard 已提交
8048
                goto illegal_op;
8049
            if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
B
bellard 已提交
8050 8051 8052
                gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
                break;
            }
8053
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8054
            gen_update_cc_op(s);
B
bellard 已提交
8055
            gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
8056
            gen_helper_fxsave(cpu_env, cpu_A0, tcg_const_i32((s->dflag == 2)));
B
bellard 已提交
8057 8058
            break;
        case 1: /* fxrstor */
8059
            if (mod == 3 || !(s->cpuid_features & CPUID_FXSR) ||
8060
                (s->prefix & PREFIX_LOCK))
B
bellard 已提交
8061
                goto illegal_op;
8062
            if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
B
bellard 已提交
8063 8064 8065
                gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
                break;
            }
8066
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8067
            gen_update_cc_op(s);
B
bellard 已提交
8068
            gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
8069 8070
            gen_helper_fxrstor(cpu_env, cpu_A0,
                               tcg_const_i32((s->dflag == 2)));
B
bellard 已提交
8071 8072 8073 8074 8075 8076
            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 已提交
8077
            }
B
bellard 已提交
8078 8079
            if ((s->flags & HF_EM_MASK) || !(s->flags & HF_OSFXSR_MASK) ||
                mod == 3)
B
bellard 已提交
8080
                goto illegal_op;
8081
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
8082
            if (op == 2) {
B
bellard 已提交
8083
                gen_op_ld_T0_A0(OT_LONG + s->mem_index);
8084
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
8085
                gen_helper_ldmxcsr(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
8086
            } else {
B
bellard 已提交
8087
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, mxcsr));
B
bellard 已提交
8088
                gen_op_st_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
8089
            }
B
bellard 已提交
8090 8091 8092
            break;
        case 5: /* lfence */
        case 6: /* mfence */
8093
            if ((modrm & 0xc7) != 0xc0 || !(s->cpuid_features & CPUID_SSE2))
B
bellard 已提交
8094 8095
                goto illegal_op;
            break;
8096 8097 8098
        case 7: /* sfence / clflush */
            if ((modrm & 0xc7) == 0xc0) {
                /* sfence */
A
aurel32 已提交
8099
                /* XXX: also check for cpuid_ext2_features & CPUID_EXT2_EMMX */
8100 8101 8102 8103 8104 8105
                if (!(s->cpuid_features & CPUID_SSE))
                    goto illegal_op;
            } else {
                /* clflush */
                if (!(s->cpuid_features & CPUID_CLFLUSH))
                    goto illegal_op;
8106
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8107 8108
            }
            break;
B
bellard 已提交
8109
        default:
B
bellard 已提交
8110 8111 8112
            goto illegal_op;
        }
        break;
A
aurel32 已提交
8113
    case 0x10d: /* 3DNow! prefetch(w) */
8114
        modrm = cpu_ldub_code(env, s->pc++);
A
aurel32 已提交
8115 8116 8117
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
8118
        gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8119 8120
        /* ignore for now */
        break;
B
bellard 已提交
8121
    case 0x1aa: /* rsm */
B
bellard 已提交
8122
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_RSM);
B
bellard 已提交
8123 8124
        if (!(s->flags & HF_SMM_MASK))
            goto illegal_op;
J
Jun Koi 已提交
8125
        gen_update_cc_op(s);
B
bellard 已提交
8126
        gen_jmp_im(s->pc - s->cs_base);
B
Blue Swirl 已提交
8127
        gen_helper_rsm(cpu_env);
B
bellard 已提交
8128 8129
        gen_eob(s);
        break;
B
balrog 已提交
8130 8131 8132 8133 8134 8135 8136
    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;

8137
        modrm = cpu_ldub_code(env, s->pc++);
M
malc 已提交
8138
        reg = ((modrm >> 3) & 7) | rex_r;
B
balrog 已提交
8139 8140 8141 8142 8143 8144 8145 8146

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

8147
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
Blue Swirl 已提交
8148
        gen_helper_popcnt(cpu_T[0], cpu_env, cpu_T[0], tcg_const_i32(ot));
B
balrog 已提交
8149
        gen_op_mov_reg_T0(ot, reg);
B
balrog 已提交
8150

8151
        set_cc_op(s, CC_OP_EFLAGS);
B
balrog 已提交
8152
        break;
A
aurel32 已提交
8153 8154 8155
    case 0x10e ... 0x10f:
        /* 3DNow! instructions, ignore prefixes */
        s->prefix &= ~(PREFIX_REPZ | PREFIX_REPNZ | PREFIX_DATA);
B
bellard 已提交
8156 8157
    case 0x110 ... 0x117:
    case 0x128 ... 0x12f:
B
balrog 已提交
8158
    case 0x138 ... 0x13a:
8159
    case 0x150 ... 0x179:
B
bellard 已提交
8160 8161 8162 8163
    case 0x17c ... 0x17f:
    case 0x1c2:
    case 0x1c4 ... 0x1c6:
    case 0x1d0 ... 0x1fe:
8164
        gen_sse(env, s, b, pc_start, rex_r);
B
bellard 已提交
8165
        break;
B
bellard 已提交
8166 8167 8168 8169 8170
    default:
        goto illegal_op;
    }
    /* lock generation */
    if (s->prefix & PREFIX_LOCK)
P
pbrook 已提交
8171
        gen_helper_unlock();
B
bellard 已提交
8172 8173
    return s->pc;
 illegal_op:
8174
    if (s->prefix & PREFIX_LOCK)
P
pbrook 已提交
8175
        gen_helper_unlock();
B
bellard 已提交
8176 8177 8178 8179 8180 8181 8182
    /* 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 已提交
8183 8184
    cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
    cpu_cc_op = tcg_global_mem_new_i32(TCG_AREG0,
8185 8186
                                       offsetof(CPUX86State, cc_op), "cc_op");
    cpu_cc_dst = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_dst),
P
pbrook 已提交
8187
                                    "cc_dst");
8188 8189
    cpu_cc_src = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_src),
                                    "cc_src");
8190 8191
    cpu_cc_src2 = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_src2),
                                     "cc_src2");
8192

8193 8194
#ifdef TARGET_X86_64
    cpu_regs[R_EAX] = tcg_global_mem_new_i64(TCG_AREG0,
8195
                                             offsetof(CPUX86State, regs[R_EAX]), "rax");
8196
    cpu_regs[R_ECX] = tcg_global_mem_new_i64(TCG_AREG0,
8197
                                             offsetof(CPUX86State, regs[R_ECX]), "rcx");
8198
    cpu_regs[R_EDX] = tcg_global_mem_new_i64(TCG_AREG0,
8199
                                             offsetof(CPUX86State, regs[R_EDX]), "rdx");
8200
    cpu_regs[R_EBX] = tcg_global_mem_new_i64(TCG_AREG0,
8201
                                             offsetof(CPUX86State, regs[R_EBX]), "rbx");
8202
    cpu_regs[R_ESP] = tcg_global_mem_new_i64(TCG_AREG0,
8203
                                             offsetof(CPUX86State, regs[R_ESP]), "rsp");
8204
    cpu_regs[R_EBP] = tcg_global_mem_new_i64(TCG_AREG0,
8205
                                             offsetof(CPUX86State, regs[R_EBP]), "rbp");
8206
    cpu_regs[R_ESI] = tcg_global_mem_new_i64(TCG_AREG0,
8207
                                             offsetof(CPUX86State, regs[R_ESI]), "rsi");
8208
    cpu_regs[R_EDI] = tcg_global_mem_new_i64(TCG_AREG0,
8209
                                             offsetof(CPUX86State, regs[R_EDI]), "rdi");
8210
    cpu_regs[8] = tcg_global_mem_new_i64(TCG_AREG0,
8211
                                         offsetof(CPUX86State, regs[8]), "r8");
8212
    cpu_regs[9] = tcg_global_mem_new_i64(TCG_AREG0,
8213
                                          offsetof(CPUX86State, regs[9]), "r9");
8214
    cpu_regs[10] = tcg_global_mem_new_i64(TCG_AREG0,
8215
                                          offsetof(CPUX86State, regs[10]), "r10");
8216
    cpu_regs[11] = tcg_global_mem_new_i64(TCG_AREG0,
8217
                                          offsetof(CPUX86State, regs[11]), "r11");
8218
    cpu_regs[12] = tcg_global_mem_new_i64(TCG_AREG0,
8219
                                          offsetof(CPUX86State, regs[12]), "r12");
8220
    cpu_regs[13] = tcg_global_mem_new_i64(TCG_AREG0,
8221
                                          offsetof(CPUX86State, regs[13]), "r13");
8222
    cpu_regs[14] = tcg_global_mem_new_i64(TCG_AREG0,
8223
                                          offsetof(CPUX86State, regs[14]), "r14");
8224
    cpu_regs[15] = tcg_global_mem_new_i64(TCG_AREG0,
8225
                                          offsetof(CPUX86State, regs[15]), "r15");
8226 8227
#else
    cpu_regs[R_EAX] = tcg_global_mem_new_i32(TCG_AREG0,
8228
                                             offsetof(CPUX86State, regs[R_EAX]), "eax");
8229
    cpu_regs[R_ECX] = tcg_global_mem_new_i32(TCG_AREG0,
8230
                                             offsetof(CPUX86State, regs[R_ECX]), "ecx");
8231
    cpu_regs[R_EDX] = tcg_global_mem_new_i32(TCG_AREG0,
8232
                                             offsetof(CPUX86State, regs[R_EDX]), "edx");
8233
    cpu_regs[R_EBX] = tcg_global_mem_new_i32(TCG_AREG0,
8234
                                             offsetof(CPUX86State, regs[R_EBX]), "ebx");
8235
    cpu_regs[R_ESP] = tcg_global_mem_new_i32(TCG_AREG0,
8236
                                             offsetof(CPUX86State, regs[R_ESP]), "esp");
8237
    cpu_regs[R_EBP] = tcg_global_mem_new_i32(TCG_AREG0,
8238
                                             offsetof(CPUX86State, regs[R_EBP]), "ebp");
8239
    cpu_regs[R_ESI] = tcg_global_mem_new_i32(TCG_AREG0,
8240
                                             offsetof(CPUX86State, regs[R_ESI]), "esi");
8241
    cpu_regs[R_EDI] = tcg_global_mem_new_i32(TCG_AREG0,
8242
                                             offsetof(CPUX86State, regs[R_EDI]), "edi");
8243 8244
#endif

8245
    /* register helpers */
P
pbrook 已提交
8246
#define GEN_HELPER 2
8247
#include "helper.h"
B
bellard 已提交
8248 8249 8250 8251 8252
}

/* 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. */
8253
static inline void gen_intermediate_code_internal(CPUX86State *env,
8254 8255
                                                  TranslationBlock *tb,
                                                  int search_pc)
B
bellard 已提交
8256 8257
{
    DisasContext dc1, *dc = &dc1;
B
bellard 已提交
8258
    target_ulong pc_ptr;
B
bellard 已提交
8259
    uint16_t *gen_opc_end;
8260
    CPUBreakpoint *bp;
8261
    int j, lj;
8262
    uint64_t flags;
B
bellard 已提交
8263 8264
    target_ulong pc_start;
    target_ulong cs_base;
P
pbrook 已提交
8265 8266
    int num_insns;
    int max_insns;
8267

B
bellard 已提交
8268
    /* generate intermediate code */
B
bellard 已提交
8269 8270
    pc_start = tb->pc;
    cs_base = tb->cs_base;
B
bellard 已提交
8271
    flags = tb->flags;
B
bellard 已提交
8272

8273
    dc->pe = (flags >> HF_PE_SHIFT) & 1;
B
bellard 已提交
8274 8275 8276 8277 8278 8279 8280 8281
    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;
8282
    dc->singlestep_enabled = env->singlestep_enabled;
B
bellard 已提交
8283
    dc->cc_op = CC_OP_DYNAMIC;
8284
    dc->cc_op_dirty = false;
B
bellard 已提交
8285 8286 8287 8288 8289 8290
    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 已提交
8291
        dc->mem_index = (cpu_mmu_index(env) + 1) << 2;
B
bellard 已提交
8292
    }
8293 8294 8295 8296 8297
    dc->cpuid_features = env->features[FEAT_1_EDX];
    dc->cpuid_ext_features = env->features[FEAT_1_ECX];
    dc->cpuid_ext2_features = env->features[FEAT_8000_0001_EDX];
    dc->cpuid_ext3_features = env->features[FEAT_8000_0001_ECX];
    dc->cpuid_7_0_ebx_features = env->features[FEAT_7_0_EBX];
B
bellard 已提交
8298 8299 8300 8301
#ifdef TARGET_X86_64
    dc->lma = (flags >> HF_LMA_SHIFT) & 1;
    dc->code64 = (flags >> HF_CS64_SHIFT) & 1;
#endif
B
bellard 已提交
8302
    dc->flags = flags;
8303 8304
    dc->jmp_opt = !(dc->tf || env->singlestep_enabled ||
                    (flags & HF_INHIBIT_IRQ_MASK)
B
bellard 已提交
8305
#ifndef CONFIG_SOFTMMU
B
bellard 已提交
8306 8307 8308
                    || (flags & HF_SOFTMMU_MASK)
#endif
                    );
8309 8310
#if 0
    /* check addseg logic */
B
bellard 已提交
8311
    if (!dc->addseg && (dc->vm86 || !dc->pe || !dc->code32))
8312 8313 8314
        printf("ERROR addseg\n");
#endif

P
pbrook 已提交
8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325
    cpu_T[0] = tcg_temp_new();
    cpu_T[1] = tcg_temp_new();
    cpu_A0 = tcg_temp_new();

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

8328
    gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
B
bellard 已提交
8329 8330 8331 8332

    dc->is_jmp = DISAS_NEXT;
    pc_ptr = pc_start;
    lj = -1;
P
pbrook 已提交
8333 8334 8335 8336
    num_insns = 0;
    max_insns = tb->cflags & CF_COUNT_MASK;
    if (max_insns == 0)
        max_insns = CF_COUNT_MASK;
B
bellard 已提交
8337

8338
    gen_tb_start();
B
bellard 已提交
8339
    for(;;) {
B
Blue Swirl 已提交
8340 8341
        if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
            QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
J
Jan Kiszka 已提交
8342 8343
                if (bp->pc == pc_ptr &&
                    !((bp->flags & BP_CPU) && (tb->flags & HF_RF_MASK))) {
B
bellard 已提交
8344 8345 8346 8347 8348 8349
                    gen_debug(dc, pc_ptr - dc->cs_base);
                    break;
                }
            }
        }
        if (search_pc) {
8350
            j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
B
bellard 已提交
8351 8352 8353
            if (lj < j) {
                lj++;
                while (lj < j)
8354
                    tcg_ctx.gen_opc_instr_start[lj++] = 0;
B
bellard 已提交
8355
            }
8356
            tcg_ctx.gen_opc_pc[lj] = pc_ptr;
B
bellard 已提交
8357
            gen_opc_cc_op[lj] = dc->cc_op;
8358
            tcg_ctx.gen_opc_instr_start[lj] = 1;
8359
            tcg_ctx.gen_opc_icount[lj] = num_insns;
B
bellard 已提交
8360
        }
P
pbrook 已提交
8361 8362 8363
        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
            gen_io_start();

8364
        pc_ptr = disas_insn(env, dc, pc_ptr);
P
pbrook 已提交
8365
        num_insns++;
B
bellard 已提交
8366 8367 8368 8369 8370
        /* stop translation if indicated */
        if (dc->is_jmp)
            break;
        /* if single step mode, we generate only one instruction and
           generate an exception */
8371 8372 8373
        /* 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 */
8374
        if (dc->tf || dc->singlestep_enabled ||
P
pbrook 已提交
8375
            (flags & HF_INHIBIT_IRQ_MASK)) {
B
bellard 已提交
8376
            gen_jmp_im(pc_ptr - dc->cs_base);
B
bellard 已提交
8377 8378 8379 8380
            gen_eob(dc);
            break;
        }
        /* if too long translation, stop generation too */
8381
        if (tcg_ctx.gen_opc_ptr >= gen_opc_end ||
P
pbrook 已提交
8382 8383
            (pc_ptr - pc_start) >= (TARGET_PAGE_SIZE - 32) ||
            num_insns >= max_insns) {
B
bellard 已提交
8384
            gen_jmp_im(pc_ptr - dc->cs_base);
B
bellard 已提交
8385 8386 8387
            gen_eob(dc);
            break;
        }
8388 8389 8390 8391 8392
        if (singlestep) {
            gen_jmp_im(pc_ptr - dc->cs_base);
            gen_eob(dc);
            break;
        }
B
bellard 已提交
8393
    }
P
pbrook 已提交
8394 8395
    if (tb->cflags & CF_LAST_IO)
        gen_io_end();
8396
    gen_tb_end(tb, num_insns);
8397
    *tcg_ctx.gen_opc_ptr = INDEX_op_end;
B
bellard 已提交
8398 8399
    /* we don't forget to fill the last values */
    if (search_pc) {
8400
        j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
B
bellard 已提交
8401 8402
        lj++;
        while (lj <= j)
8403
            tcg_ctx.gen_opc_instr_start[lj++] = 0;
B
bellard 已提交
8404
    }
8405

B
bellard 已提交
8406
#ifdef DEBUG_DISAS
8407
    if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
B
bellard 已提交
8408
        int disas_flags;
8409 8410
        qemu_log("----------------\n");
        qemu_log("IN: %s\n", lookup_symbol(pc_start));
B
bellard 已提交
8411 8412 8413 8414 8415 8416
#ifdef TARGET_X86_64
        if (dc->code64)
            disas_flags = 2;
        else
#endif
            disas_flags = !dc->code32;
B
Blue Swirl 已提交
8417
        log_target_disas(env, pc_start, pc_ptr - pc_start, disas_flags);
8418
        qemu_log("\n");
B
bellard 已提交
8419 8420 8421
    }
#endif

P
pbrook 已提交
8422
    if (!search_pc) {
B
bellard 已提交
8423
        tb->size = pc_ptr - pc_start;
P
pbrook 已提交
8424 8425
        tb->icount = num_insns;
    }
B
bellard 已提交
8426 8427
}

8428
void gen_intermediate_code(CPUX86State *env, TranslationBlock *tb)
B
bellard 已提交
8429
{
8430
    gen_intermediate_code_internal(env, tb, 0);
B
bellard 已提交
8431 8432
}

8433
void gen_intermediate_code_pc(CPUX86State *env, TranslationBlock *tb)
B
bellard 已提交
8434
{
8435
    gen_intermediate_code_internal(env, tb, 1);
B
bellard 已提交
8436 8437
}

8438
void restore_state_to_opc(CPUX86State *env, TranslationBlock *tb, int pc_pos)
A
aurel32 已提交
8439 8440 8441
{
    int cc_op;
#ifdef DEBUG_DISAS
8442
    if (qemu_loglevel_mask(CPU_LOG_TB_OP)) {
A
aurel32 已提交
8443
        int i;
8444
        qemu_log("RESTORE:\n");
A
aurel32 已提交
8445
        for(i = 0;i <= pc_pos; i++) {
8446
            if (tcg_ctx.gen_opc_instr_start[i]) {
8447 8448
                qemu_log("0x%04x: " TARGET_FMT_lx "\n", i,
                        tcg_ctx.gen_opc_pc[i]);
A
aurel32 已提交
8449 8450
            }
        }
8451
        qemu_log("pc_pos=0x%x eip=" TARGET_FMT_lx " cs_base=%x\n",
8452
                pc_pos, tcg_ctx.gen_opc_pc[pc_pos] - tb->cs_base,
A
aurel32 已提交
8453 8454 8455
                (uint32_t)tb->cs_base);
    }
#endif
8456
    env->eip = tcg_ctx.gen_opc_pc[pc_pos] - tb->cs_base;
A
aurel32 已提交
8457 8458 8459 8460
    cc_op = gen_opc_cc_op[pc_pos];
    if (cc_op != CC_OP_DYNAMIC)
        env->cc_op = cc_op;
}