translate.c 261.3 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 26
 */
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <signal.h>

#include "cpu.h"
27
#include "disas/disas.h"
B
bellard 已提交
28
#include "tcg-op.h"
B
bellard 已提交
29

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

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

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

B
bellard 已提交
50 51 52
//#define MACRO_TEST   1

/* global register indexes */
P
pbrook 已提交
53
static TCGv_ptr cpu_env;
54
static TCGv cpu_A0, cpu_cc_src, cpu_cc_dst;
P
pbrook 已提交
55
static TCGv_i32 cpu_cc_op;
56
static TCGv cpu_regs[CPU_NB_REGS];
57 58
/* local temps */
static TCGv cpu_T[2], cpu_T3;
B
bellard 已提交
59
/* local register indexes (only used inside old micro ops) */
P
pbrook 已提交
60 61 62 63
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;
64
static TCGv cpu_tmp5;
B
bellard 已提交
65

66 67
static uint8_t gen_opc_cc_op[OPC_BUF_SIZE];

68
#include "exec/gen-icount.h"
P
pbrook 已提交
69

B
bellard 已提交
70 71
#ifdef TARGET_X86_64
static int x86_64_hregs;
B
bellard 已提交
72 73
#endif

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

static void gen_eob(DisasContext *s);
B
bellard 已提交
115 116
static void gen_jmp(DisasContext *s, target_ulong eip);
static void gen_jmp_tb(DisasContext *s, target_ulong eip, int tb_num);
B
bellard 已提交
117 118 119

/* i386 arith/logic operations */
enum {
120 121 122
    OP_ADDL,
    OP_ORL,
    OP_ADCL,
B
bellard 已提交
123
    OP_SBBL,
124 125 126
    OP_ANDL,
    OP_SUBL,
    OP_XORL,
B
bellard 已提交
127 128 129 130 131
    OP_CMPL,
};

/* i386 shift ops */
enum {
132 133 134 135 136 137
    OP_ROL,
    OP_ROR,
    OP_RCL,
    OP_RCR,
    OP_SHL,
    OP_SHR,
B
bellard 已提交
138 139 140 141
    OP_SHL1, /* undocumented */
    OP_SAR = 7,
};

142 143 144 145 146 147 148 149 150 151 152
enum {
    JCC_O,
    JCC_B,
    JCC_Z,
    JCC_BE,
    JCC_S,
    JCC_P,
    JCC_L,
    JCC_LE,
};

B
bellard 已提交
153 154 155 156
/* operand size */
enum {
    OT_BYTE = 0,
    OT_WORD,
157
    OT_LONG,
B
bellard 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170
    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 已提交
171 172

    OR_TMP0 = 16,    /* temporary operand register */
B
bellard 已提交
173 174 175 176
    OR_TMP1,
    OR_A0, /* temporary register used when doing address evaluation */
};

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
enum {
    USES_CC_DST = 1,
    USES_CC_SRC = 2,
};

/* Bit set if the global variable is live after setting CC_OP to X.  */
static const uint8_t cc_op_live[CC_OP_NB] = {
    [CC_OP_DYNAMIC] = USES_CC_DST | USES_CC_SRC,
    [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,
    [CC_OP_ADCB ... CC_OP_ADCQ] = USES_CC_DST | USES_CC_SRC,
    [CC_OP_SUBB ... CC_OP_SUBQ] = USES_CC_DST | USES_CC_SRC,
    [CC_OP_SBBB ... CC_OP_SBBQ] = USES_CC_DST | USES_CC_SRC,
    [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,
};

198
static void set_cc_op(DisasContext *s, CCOp op)
199
{
200 201 202 203 204 205 206 207 208 209
    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);
210
    }
211 212 213 214 215 216 217 218
    if (dead & USES_CC_SRC) {
        tcg_gen_discard_tl(cpu_cc_src);
    }

    s->cc_op = op;
    /* The DYNAMIC setting is translator only, and should never be
       stored.  Thus we always consider it clean.  */
    s->cc_op_dirty = (op != CC_OP_DYNAMIC);
219 220 221 222 223
}

static void gen_update_cc_op(DisasContext *s)
{
    if (s->cc_op_dirty) {
224
        tcg_gen_movi_i32(cpu_cc_op, s->cc_op);
225 226
        s->cc_op_dirty = false;
    }
227 228
}

B
bellard 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
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 已提交
296 297 298 299 300 301 302 303 304 305
#ifdef TARGET_X86_64

#define NB_OP_SIZES 4

#else /* !TARGET_X86_64 */

#define NB_OP_SIZES 3

#endif /* !TARGET_X86_64 */

306
#if defined(HOST_WORDS_BIGENDIAN)
B
bellard 已提交
307 308 309 310 311
#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 已提交
312
#else
B
bellard 已提交
313 314 315 316 317
#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 已提交
318
#endif
B
bellard 已提交
319

320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
/* 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;
}

339
static inline void gen_op_mov_reg_v(int ot, int reg, TCGv t0)
B
bellard 已提交
340 341 342
{
    switch(ot) {
    case OT_BYTE:
343
        if (!byte_reg_is_xH(reg)) {
344
            tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], t0, 0, 8);
B
bellard 已提交
345
        } else {
346
            tcg_gen_deposit_tl(cpu_regs[reg - 4], cpu_regs[reg - 4], t0, 8, 8);
B
bellard 已提交
347 348 349
        }
        break;
    case OT_WORD:
350
        tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], t0, 0, 16);
B
bellard 已提交
351
        break;
352
    default: /* XXX this shouldn't be reached;  abort? */
B
bellard 已提交
353
    case OT_LONG:
354 355 356
        /* 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 已提交
357
        break;
358
#ifdef TARGET_X86_64
B
bellard 已提交
359
    case OT_QUAD:
360
        tcg_gen_mov_tl(cpu_regs[reg], t0);
B
bellard 已提交
361
        break;
B
bellard 已提交
362
#endif
B
bellard 已提交
363 364
    }
}
B
bellard 已提交
365

B
bellard 已提交
366 367
static inline void gen_op_mov_reg_T0(int ot, int reg)
{
368
    gen_op_mov_reg_v(ot, reg, cpu_T[0]);
B
bellard 已提交
369 370 371 372
}

static inline void gen_op_mov_reg_T1(int ot, int reg)
{
373
    gen_op_mov_reg_v(ot, reg, cpu_T[1]);
B
bellard 已提交
374 375 376 377 378
}

static inline void gen_op_mov_reg_A0(int size, int reg)
{
    switch(size) {
379
    case OT_BYTE:
380
        tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], cpu_A0, 0, 16);
B
bellard 已提交
381
        break;
382
    default: /* XXX this shouldn't be reached;  abort? */
383
    case OT_WORD:
384 385 386
        /* 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 已提交
387
        break;
388
#ifdef TARGET_X86_64
389
    case OT_LONG:
390
        tcg_gen_mov_tl(cpu_regs[reg], cpu_A0);
B
bellard 已提交
391
        break;
B
bellard 已提交
392
#endif
B
bellard 已提交
393 394 395
    }
}

396
static inline void gen_op_mov_v_reg(int ot, TCGv t0, int reg)
B
bellard 已提交
397
{
398 399 400 401
    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 {
402
        tcg_gen_mov_tl(t0, cpu_regs[reg]);
B
bellard 已提交
403 404 405
    }
}

406 407 408 409 410
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 已提交
411 412
static inline void gen_op_movl_A0_reg(int reg)
{
413
    tcg_gen_mov_tl(cpu_A0, cpu_regs[reg]);
B
bellard 已提交
414 415 416 417 418
}

static inline void gen_op_addl_A0_im(int32_t val)
{
    tcg_gen_addi_tl(cpu_A0, cpu_A0, val);
B
bellard 已提交
419
#ifdef TARGET_X86_64
B
bellard 已提交
420
    tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffffffff);
B
bellard 已提交
421
#endif
B
bellard 已提交
422
}
B
bellard 已提交
423

B
bellard 已提交
424
#ifdef TARGET_X86_64
B
bellard 已提交
425 426 427 428
static inline void gen_op_addq_A0_im(int64_t val)
{
    tcg_gen_addi_tl(cpu_A0, cpu_A0, val);
}
B
bellard 已提交
429
#endif
B
bellard 已提交
430 431 432 433 434 435 436 437 438 439
    
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 已提交
440

B
bellard 已提交
441
static inline void gen_op_addl_T0_T1(void)
B
bellard 已提交
442
{
B
bellard 已提交
443 444 445 446 447
    tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
}

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

451
static inline void gen_op_add_reg_im(int size, int reg, int32_t val)
B
bellard 已提交
452
{
453
    switch(size) {
454
    case OT_BYTE:
455
        tcg_gen_addi_tl(cpu_tmp0, cpu_regs[reg], val);
456
        tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], cpu_tmp0, 0, 16);
457
        break;
458
    case OT_WORD:
459 460 461 462 463
        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);
464 465
        break;
#ifdef TARGET_X86_64
466
    case OT_LONG:
467
        tcg_gen_addi_tl(cpu_regs[reg], cpu_regs[reg], val);
468 469 470
        break;
#endif
    }
B
bellard 已提交
471 472
}

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

static inline void gen_op_addl_A0_reg_sN(int shift, int reg)
{
497 498
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[reg]);
    if (shift != 0)
B
bellard 已提交
499 500
        tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, shift);
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
501 502 503
    /* 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 已提交
504
}
B
bellard 已提交
505

B
bellard 已提交
506 507
static inline void gen_op_movl_A0_seg(int reg)
{
508
    tcg_gen_ld32u_tl(cpu_A0, cpu_env, offsetof(CPUX86State, segs[reg].base) + REG_L_OFFSET);
B
bellard 已提交
509
}
B
bellard 已提交
510

511
static inline void gen_op_addl_A0_seg(DisasContext *s, int reg)
B
bellard 已提交
512
{
513
    tcg_gen_ld_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, segs[reg].base));
B
bellard 已提交
514
#ifdef TARGET_X86_64
515 516 517 518 519 520 521 522 523
    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 已提交
524 525
#endif
}
B
bellard 已提交
526

B
bellard 已提交
527
#ifdef TARGET_X86_64
B
bellard 已提交
528 529
static inline void gen_op_movq_A0_seg(int reg)
{
530
    tcg_gen_ld_tl(cpu_A0, cpu_env, offsetof(CPUX86State, segs[reg].base));
B
bellard 已提交
531
}
B
bellard 已提交
532

B
bellard 已提交
533 534
static inline void gen_op_addq_A0_seg(int reg)
{
535
    tcg_gen_ld_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, segs[reg].base));
B
bellard 已提交
536 537 538 539 540
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
}

static inline void gen_op_movq_A0_reg(int reg)
{
541
    tcg_gen_mov_tl(cpu_A0, cpu_regs[reg]);
B
bellard 已提交
542 543 544 545
}

static inline void gen_op_addq_A0_reg_sN(int shift, int reg)
{
546 547
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[reg]);
    if (shift != 0)
B
bellard 已提交
548 549 550
        tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, shift);
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
}
B
bellard 已提交
551 552
#endif

B
bellard 已提交
553 554 555 556
static inline void gen_op_lds_T0_A0(int idx)
{
    int mem_index = (idx >> 2) - 1;
    switch(idx & 3) {
557
    case OT_BYTE:
B
bellard 已提交
558 559
        tcg_gen_qemu_ld8s(cpu_T[0], cpu_A0, mem_index);
        break;
560
    case OT_WORD:
B
bellard 已提交
561 562 563
        tcg_gen_qemu_ld16s(cpu_T[0], cpu_A0, mem_index);
        break;
    default:
564
    case OT_LONG:
B
bellard 已提交
565 566 567 568
        tcg_gen_qemu_ld32s(cpu_T[0], cpu_A0, mem_index);
        break;
    }
}
B
bellard 已提交
569

570
static inline void gen_op_ld_v(int idx, TCGv t0, TCGv a0)
B
bellard 已提交
571 572 573
{
    int mem_index = (idx >> 2) - 1;
    switch(idx & 3) {
574
    case OT_BYTE:
575
        tcg_gen_qemu_ld8u(t0, a0, mem_index);
B
bellard 已提交
576
        break;
577
    case OT_WORD:
578
        tcg_gen_qemu_ld16u(t0, a0, mem_index);
B
bellard 已提交
579
        break;
580
    case OT_LONG:
581
        tcg_gen_qemu_ld32u(t0, a0, mem_index);
B
bellard 已提交
582 583
        break;
    default:
584
    case OT_QUAD:
P
pbrook 已提交
585 586
        /* Should never happen on 32-bit targets.  */
#ifdef TARGET_X86_64
587
        tcg_gen_qemu_ld64(t0, a0, mem_index);
P
pbrook 已提交
588
#endif
B
bellard 已提交
589 590 591
        break;
    }
}
B
bellard 已提交
592

593 594 595 596 597 598
/* 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 已提交
599 600
static inline void gen_op_ldu_T0_A0(int idx)
{
601
    gen_op_ld_v(idx, cpu_T[0], cpu_A0);
B
bellard 已提交
602
}
B
bellard 已提交
603

B
bellard 已提交
604
static inline void gen_op_ld_T1_A0(int idx)
605 606 607 608 609
{
    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 已提交
610 611 612
{
    int mem_index = (idx >> 2) - 1;
    switch(idx & 3) {
613
    case OT_BYTE:
614
        tcg_gen_qemu_st8(t0, a0, mem_index);
B
bellard 已提交
615
        break;
616
    case OT_WORD:
617
        tcg_gen_qemu_st16(t0, a0, mem_index);
B
bellard 已提交
618
        break;
619
    case OT_LONG:
620
        tcg_gen_qemu_st32(t0, a0, mem_index);
B
bellard 已提交
621 622
        break;
    default:
623
    case OT_QUAD:
P
pbrook 已提交
624 625
        /* Should never happen on 32-bit targets.  */
#ifdef TARGET_X86_64
626
        tcg_gen_qemu_st64(t0, a0, mem_index);
P
pbrook 已提交
627
#endif
B
bellard 已提交
628 629 630
        break;
    }
}
631

B
bellard 已提交
632 633
static inline void gen_op_st_T0_A0(int idx)
{
634
    gen_op_st_v(idx, cpu_T[0], cpu_A0);
B
bellard 已提交
635
}
636

B
bellard 已提交
637 638
static inline void gen_op_st_T1_A0(int idx)
{
639
    gen_op_st_v(idx, cpu_T[1], cpu_A0);
B
bellard 已提交
640
}
641

B
bellard 已提交
642 643
static inline void gen_jmp_im(target_ulong pc)
{
B
bellard 已提交
644
    tcg_gen_movi_tl(cpu_tmp0, pc);
645
    tcg_gen_st_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, eip));
B
bellard 已提交
646 647
}

B
bellard 已提交
648 649 650 651 652
static inline void gen_string_movl_A0_ESI(DisasContext *s)
{
    int override;

    override = s->override;
B
bellard 已提交
653 654 655
#ifdef TARGET_X86_64
    if (s->aflag == 2) {
        if (override >= 0) {
B
bellard 已提交
656 657
            gen_op_movq_A0_seg(override);
            gen_op_addq_A0_reg_sN(0, R_ESI);
B
bellard 已提交
658
        } else {
B
bellard 已提交
659
            gen_op_movq_A0_reg(R_ESI);
B
bellard 已提交
660 661 662
        }
    } else
#endif
B
bellard 已提交
663 664 665 666 667
    if (s->aflag) {
        /* 32 bit address */
        if (s->addseg && override < 0)
            override = R_DS;
        if (override >= 0) {
B
bellard 已提交
668 669
            gen_op_movl_A0_seg(override);
            gen_op_addl_A0_reg_sN(0, R_ESI);
B
bellard 已提交
670
        } else {
B
bellard 已提交
671
            gen_op_movl_A0_reg(R_ESI);
B
bellard 已提交
672 673 674 675 676
        }
    } else {
        /* 16 address, always override */
        if (override < 0)
            override = R_DS;
B
bellard 已提交
677
        gen_op_movl_A0_reg(R_ESI);
B
bellard 已提交
678
        gen_op_andl_A0_ffff();
679
        gen_op_addl_A0_seg(s, override);
B
bellard 已提交
680 681 682 683 684
    }
}

static inline void gen_string_movl_A0_EDI(DisasContext *s)
{
B
bellard 已提交
685 686
#ifdef TARGET_X86_64
    if (s->aflag == 2) {
B
bellard 已提交
687
        gen_op_movq_A0_reg(R_EDI);
B
bellard 已提交
688 689
    } else
#endif
B
bellard 已提交
690 691
    if (s->aflag) {
        if (s->addseg) {
B
bellard 已提交
692 693
            gen_op_movl_A0_seg(R_ES);
            gen_op_addl_A0_reg_sN(0, R_EDI);
B
bellard 已提交
694
        } else {
B
bellard 已提交
695
            gen_op_movl_A0_reg(R_EDI);
B
bellard 已提交
696 697
        }
    } else {
B
bellard 已提交
698
        gen_op_movl_A0_reg(R_EDI);
B
bellard 已提交
699
        gen_op_andl_A0_ffff();
700
        gen_op_addl_A0_seg(s, R_ES);
B
bellard 已提交
701 702 703
    }
}

704 705
static inline void gen_op_movl_T0_Dshift(int ot) 
{
706
    tcg_gen_ld32s_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, df));
707
    tcg_gen_shli_tl(cpu_T[0], cpu_T[0], ot);
B
bellard 已提交
708 709
};

710
static TCGv gen_ext_tl(TCGv dst, TCGv src, int size, bool sign)
711
{
712
    switch (size) {
713
    case OT_BYTE:
714 715 716 717 718 719
        if (sign) {
            tcg_gen_ext8s_tl(dst, src);
        } else {
            tcg_gen_ext8u_tl(dst, src);
        }
        return dst;
720
    case OT_WORD:
721 722 723 724 725 726 727
        if (sign) {
            tcg_gen_ext16s_tl(dst, src);
        } else {
            tcg_gen_ext16u_tl(dst, src);
        }
        return dst;
#ifdef TARGET_X86_64
728
    case OT_LONG:
729 730 731 732 733 734 735
        if (sign) {
            tcg_gen_ext32s_tl(dst, src);
        } else {
            tcg_gen_ext32u_tl(dst, src);
        }
        return dst;
#endif
736
    default:
737
        return src;
738 739
    }
}
740

741 742 743 744 745
static void gen_extu(int ot, TCGv reg)
{
    gen_ext_tl(reg, reg, ot, false);
}

746 747
static void gen_exts(int ot, TCGv reg)
{
748
    gen_ext_tl(reg, reg, ot, true);
749
}
B
bellard 已提交
750

751 752
static inline void gen_op_jnz_ecx(int size, int label1)
{
753
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
754
    gen_extu(size + 1, cpu_tmp0);
P
pbrook 已提交
755
    tcg_gen_brcondi_tl(TCG_COND_NE, cpu_tmp0, 0, label1);
756 757 758 759
}

static inline void gen_op_jz_ecx(int size, int label1)
{
760
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
761
    gen_extu(size + 1, cpu_tmp0);
P
pbrook 已提交
762
    tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, label1);
763
}
B
bellard 已提交
764

P
pbrook 已提交
765 766 767
static void gen_helper_in_func(int ot, TCGv v, TCGv_i32 n)
{
    switch (ot) {
768 769 770 771 772 773 774 775 776
    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 已提交
777 778
    }
}
B
bellard 已提交
779

P
pbrook 已提交
780 781 782
static void gen_helper_out_func(int ot, TCGv_i32 v, TCGv_i32 n)
{
    switch (ot) {
783 784 785 786 787 788 789 790 791
    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 已提交
792 793
    }
}
794

795 796
static void gen_check_io(DisasContext *s, int ot, target_ulong cur_eip,
                         uint32_t svm_flags)
797
{
798 799 800 801
    int state_saved;
    target_ulong next_eip;

    state_saved = 0;
802
    if (s->pe && (s->cpl > s->iopl || s->vm86)) {
803
        gen_update_cc_op(s);
B
bellard 已提交
804
        gen_jmp_im(cur_eip);
805
        state_saved = 1;
806
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
807
        switch (ot) {
808
        case OT_BYTE:
B
Blue Swirl 已提交
809 810
            gen_helper_check_iob(cpu_env, cpu_tmp2_i32);
            break;
811
        case OT_WORD:
B
Blue Swirl 已提交
812 813
            gen_helper_check_iow(cpu_env, cpu_tmp2_i32);
            break;
814
        case OT_LONG:
B
Blue Swirl 已提交
815 816
            gen_helper_check_iol(cpu_env, cpu_tmp2_i32);
            break;
P
pbrook 已提交
817
        }
818
    }
B
bellard 已提交
819
    if(s->flags & HF_SVMI_MASK) {
820
        if (!state_saved) {
821
            gen_update_cc_op(s);
822 823 824 825
            gen_jmp_im(cur_eip);
        }
        svm_flags |= (1 << (4 + ot));
        next_eip = s->pc - s->cs_base;
826
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
827 828
        gen_helper_svm_check_io(cpu_env, cpu_tmp2_i32,
                                tcg_const_i32(svm_flags),
P
pbrook 已提交
829
                                tcg_const_i32(next_eip - cur_eip));
830 831 832
    }
}

B
bellard 已提交
833 834 835
static inline void gen_movs(DisasContext *s, int ot)
{
    gen_string_movl_A0_ESI(s);
B
bellard 已提交
836
    gen_op_ld_T0_A0(ot + s->mem_index);
B
bellard 已提交
837
    gen_string_movl_A0_EDI(s);
B
bellard 已提交
838
    gen_op_st_T0_A0(ot + s->mem_index);
839 840 841
    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 已提交
842 843
}

844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
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]);
}

static inline void gen_op_cmpl_T0_T1_cc(void)
{
    tcg_gen_mov_tl(cpu_cc_src, cpu_T[1]);
    tcg_gen_sub_tl(cpu_cc_dst, cpu_T[0], cpu_T[1]);
}

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_neg_tl(cpu_cc_src, cpu_T[0]);
    tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
}

872
/* compute eflags.C to reg */
873
static void gen_compute_eflags_c(DisasContext *s, TCGv reg)
874
{
875
    gen_update_cc_op(s);
876
    gen_helper_cc_compute_c(cpu_tmp2_i32, cpu_env, cpu_cc_op);
877 878 879
    tcg_gen_extu_i32_tl(reg, cpu_tmp2_i32);
}

880 881
/* compute all eflags to cc_src */
static void gen_compute_eflags(DisasContext *s)
882
{
883 884 885
    if (s->cc_op == CC_OP_EFLAGS) {
        return;
    }
886
    gen_update_cc_op(s);
887
    gen_helper_cc_compute_all(cpu_tmp2_i32, cpu_env, cpu_cc_op);
888 889
    set_cc_op(s, CC_OP_EFLAGS);
    tcg_gen_extu_i32_tl(cpu_cc_src, cpu_tmp2_i32);
890 891
}

892 893 894
/* compute eflags.P to reg */
static void gen_compute_eflags_p(DisasContext *s, TCGv reg)
{
895 896
    gen_compute_eflags(s);
    tcg_gen_shri_tl(reg, cpu_cc_src, 2);
897 898 899 900 901 902
    tcg_gen_andi_tl(reg, reg, 1);
}

/* compute eflags.S to reg */
static void gen_compute_eflags_s(DisasContext *s, TCGv reg)
{
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
    switch (s->cc_op) {
    case CC_OP_DYNAMIC:
        gen_compute_eflags(s);
        /* FALLTHRU */
    case CC_OP_EFLAGS:
        tcg_gen_shri_tl(reg, cpu_cc_src, 7);
        tcg_gen_andi_tl(reg, reg, 1);
        break;
    default:
        {
            int size = (s->cc_op - CC_OP_ADDB) & 3;
            TCGv t0 = gen_ext_tl(reg, cpu_cc_dst, size, true);
            tcg_gen_setcondi_tl(TCG_COND_LT, reg, t0, 0);
        }
        break;
    }
919 920 921 922 923
}

/* compute eflags.O to reg */
static void gen_compute_eflags_o(DisasContext *s, TCGv reg)
{
924 925
    gen_compute_eflags(s);
    tcg_gen_shri_tl(reg, cpu_cc_src, 11);
926 927 928 929 930 931
    tcg_gen_andi_tl(reg, reg, 1);
}

/* compute eflags.Z to reg */
static void gen_compute_eflags_z(DisasContext *s, TCGv reg)
{
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
    switch (s->cc_op) {
    case CC_OP_DYNAMIC:
        gen_compute_eflags(s);
        /* FALLTHRU */
    case CC_OP_EFLAGS:
        tcg_gen_shri_tl(reg, cpu_cc_src, 6);
        tcg_gen_andi_tl(reg, reg, 1);
        break;
    default:
        {
            int size = (s->cc_op - CC_OP_ADDB) & 3;
            TCGv t0 = gen_ext_tl(reg, cpu_cc_dst, size, false);
            tcg_gen_setcondi_tl(TCG_COND_EQ, reg, t0, 0);
        }
    }
947 948
}

949
static inline void gen_setcc_slow_T0(DisasContext *s, int jcc_op)
950
{
951
    switch(jcc_op) {
952
    case JCC_O:
953
        gen_compute_eflags_o(s, cpu_T[0]);
954 955
        break;
    case JCC_B:
956
        gen_compute_eflags_c(s, cpu_T[0]);
957 958
        break;
    case JCC_Z:
959
        gen_compute_eflags_z(s, cpu_T[0]);
960 961
        break;
    case JCC_BE:
962 963 964
        gen_compute_eflags(s);
        tcg_gen_shri_tl(cpu_T[0], cpu_cc_src, 6);
        tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_cc_src);
965 966 967
        tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 1);
        break;
    case JCC_S:
968
        gen_compute_eflags_s(s, cpu_T[0]);
969 970
        break;
    case JCC_P:
971
        gen_compute_eflags_p(s, cpu_T[0]);
972 973
        break;
    case JCC_L:
974 975 976
        gen_compute_eflags(s);
        tcg_gen_shri_tl(cpu_T[0], cpu_cc_src, 11); /* CC_O */
        tcg_gen_shri_tl(cpu_tmp0, cpu_cc_src, 7); /* CC_S */
977 978 979 980 981
        tcg_gen_xor_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
        tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 1);
        break;
    default:
    case JCC_LE:
982 983 984 985
        gen_compute_eflags(s);
        tcg_gen_shri_tl(cpu_T[0], cpu_cc_src, 11); /* CC_O */
        tcg_gen_shri_tl(cpu_tmp4, cpu_cc_src, 7); /* CC_S */
        tcg_gen_shri_tl(cpu_tmp0, cpu_cc_src, 6); /* CC_Z */
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
        tcg_gen_xor_tl(cpu_T[0], cpu_T[0], cpu_tmp4);
        tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
        tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 1);
        break;
    }
}

/* return true if setcc_slow is not needed (WARNING: must be kept in
   sync with gen_jcc1) */
static int is_fast_jcc_case(DisasContext *s, int b)
{
    int jcc_op;
    jcc_op = (b >> 1) & 7;
    switch(s->cc_op) {
        /* we optimize the cmp/jcc case */
    case CC_OP_SUBB:
    case CC_OP_SUBW:
    case CC_OP_SUBL:
    case CC_OP_SUBQ:
        if (jcc_op == JCC_O || jcc_op == JCC_P)
            goto slow_jcc;
        break;

        /* some jumps are easy to compute */
    case CC_OP_ADDB:
    case CC_OP_ADDW:
    case CC_OP_ADDL:
    case CC_OP_ADDQ:

    case CC_OP_LOGICB:
    case CC_OP_LOGICW:
    case CC_OP_LOGICL:
    case CC_OP_LOGICQ:

    case CC_OP_INCB:
    case CC_OP_INCW:
    case CC_OP_INCL:
    case CC_OP_INCQ:

    case CC_OP_DECB:
    case CC_OP_DECW:
    case CC_OP_DECL:
    case CC_OP_DECQ:

    case CC_OP_SHLB:
    case CC_OP_SHLW:
    case CC_OP_SHLL:
    case CC_OP_SHLQ:
        if (jcc_op != JCC_Z && jcc_op != JCC_S)
            goto slow_jcc;
        break;
    default:
    slow_jcc:
        return 0;
    }
    return 1;
}

/* generate a conditional jump to label 'l1' according to jump opcode
   value 'b'. In the fast case, T0 is guaranted not to be used. */
1046
static inline void gen_jcc1(DisasContext *s, int b, int l1)
1047 1048 1049 1050 1051 1052 1053
{
    int inv, jcc_op, size, cond;
    TCGv t0;

    inv = b & 1;
    jcc_op = (b >> 1) & 7;

1054
    switch (s->cc_op) {
1055 1056 1057 1058 1059 1060
        /* we optimize the cmp/jcc case */
    case CC_OP_SUBB:
    case CC_OP_SUBW:
    case CC_OP_SUBL:
    case CC_OP_SUBQ:
        
1061
        size = s->cc_op - CC_OP_SUBB;
1062 1063 1064
        switch(jcc_op) {
        case JCC_Z:
        fast_jcc_z:
1065
            t0 = gen_ext_tl(cpu_tmp0, cpu_cc_dst, size, false);
P
pbrook 已提交
1066
            tcg_gen_brcondi_tl(inv ? TCG_COND_NE : TCG_COND_EQ, t0, 0, l1);
1067 1068 1069
            break;
        case JCC_S:
        fast_jcc_s:
1070 1071
            t0 = gen_ext_tl(cpu_tmp0, cpu_cc_dst, size, true);
            tcg_gen_brcondi_tl(inv ? TCG_COND_GE : TCG_COND_LT, t0, 0, l1);
1072
            break;
1073

1074 1075 1076 1077 1078 1079 1080
        case JCC_B:
            cond = inv ? TCG_COND_GEU : TCG_COND_LTU;
            goto fast_jcc_b;
        case JCC_BE:
            cond = inv ? TCG_COND_GTU : TCG_COND_LEU;
        fast_jcc_b:
            tcg_gen_add_tl(cpu_tmp4, cpu_cc_dst, cpu_cc_src);
1081 1082
            gen_extu(size, cpu_tmp4);
            t0 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, false);
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
            tcg_gen_brcond_tl(cond, cpu_tmp4, t0, l1);
            break;
            
        case JCC_L:
            cond = inv ? TCG_COND_GE : TCG_COND_LT;
            goto fast_jcc_l;
        case JCC_LE:
            cond = inv ? TCG_COND_GT : TCG_COND_LE;
        fast_jcc_l:
            tcg_gen_add_tl(cpu_tmp4, cpu_cc_dst, cpu_cc_src);
1093 1094
            gen_exts(size, cpu_tmp4);
            t0 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, true);
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
            tcg_gen_brcond_tl(cond, cpu_tmp4, t0, l1);
            break;
            
        default:
            goto slow_jcc;
        }
        break;
        
        /* some jumps are easy to compute */
    case CC_OP_ADDB:
    case CC_OP_ADDW:
    case CC_OP_ADDL:
    case CC_OP_ADDQ:
        
    case CC_OP_ADCB:
    case CC_OP_ADCW:
    case CC_OP_ADCL:
    case CC_OP_ADCQ:
        
    case CC_OP_SBBB:
    case CC_OP_SBBW:
    case CC_OP_SBBL:
    case CC_OP_SBBQ:
        
    case CC_OP_LOGICB:
    case CC_OP_LOGICW:
    case CC_OP_LOGICL:
    case CC_OP_LOGICQ:
        
    case CC_OP_INCB:
    case CC_OP_INCW:
    case CC_OP_INCL:
    case CC_OP_INCQ:
        
    case CC_OP_DECB:
    case CC_OP_DECW:
    case CC_OP_DECL:
    case CC_OP_DECQ:
        
    case CC_OP_SHLB:
    case CC_OP_SHLW:
    case CC_OP_SHLL:
    case CC_OP_SHLQ:
        
    case CC_OP_SARB:
    case CC_OP_SARW:
    case CC_OP_SARL:
    case CC_OP_SARQ:
        switch(jcc_op) {
        case JCC_Z:
1145
            size = (s->cc_op - CC_OP_ADDB) & 3;
1146 1147
            goto fast_jcc_z;
        case JCC_S:
1148
            size = (s->cc_op - CC_OP_ADDB) & 3;
1149 1150 1151 1152 1153 1154 1155
            goto fast_jcc_s;
        default:
            goto slow_jcc;
        }
        break;
    default:
    slow_jcc:
1156
        gen_setcc_slow_T0(s, jcc_op);
P
pbrook 已提交
1157 1158
        tcg_gen_brcondi_tl(inv ? TCG_COND_EQ : TCG_COND_NE, 
                           cpu_T[0], 0, l1);
1159 1160 1161 1162
        break;
    }
}

B
bellard 已提交
1163 1164 1165
/* 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 已提交
1166
{
B
bellard 已提交
1167 1168 1169 1170
    int l1, l2;

    l1 = gen_new_label();
    l2 = gen_new_label();
1171
    gen_op_jnz_ecx(s->aflag, l1);
B
bellard 已提交
1172 1173 1174 1175
    gen_set_label(l2);
    gen_jmp_tb(s, next_eip, 1);
    gen_set_label(l1);
    return l2;
B
bellard 已提交
1176 1177 1178 1179
}

static inline void gen_stos(DisasContext *s, int ot)
{
B
bellard 已提交
1180
    gen_op_mov_TN_reg(OT_LONG, 0, R_EAX);
B
bellard 已提交
1181
    gen_string_movl_A0_EDI(s);
B
bellard 已提交
1182
    gen_op_st_T0_A0(ot + s->mem_index);
1183 1184
    gen_op_movl_T0_Dshift(ot);
    gen_op_add_reg_T0(s->aflag, R_EDI);
B
bellard 已提交
1185 1186 1187 1188 1189
}

static inline void gen_lods(DisasContext *s, int ot)
{
    gen_string_movl_A0_ESI(s);
B
bellard 已提交
1190 1191
    gen_op_ld_T0_A0(ot + s->mem_index);
    gen_op_mov_reg_T0(ot, R_EAX);
1192 1193
    gen_op_movl_T0_Dshift(ot);
    gen_op_add_reg_T0(s->aflag, R_ESI);
B
bellard 已提交
1194 1195 1196 1197
}

static inline void gen_scas(DisasContext *s, int ot)
{
B
bellard 已提交
1198
    gen_op_mov_TN_reg(OT_LONG, 0, R_EAX);
B
bellard 已提交
1199
    gen_string_movl_A0_EDI(s);
B
bellard 已提交
1200
    gen_op_ld_T1_A0(ot + s->mem_index);
B
bellard 已提交
1201
    gen_op_cmpl_T0_T1_cc();
1202 1203
    gen_op_movl_T0_Dshift(ot);
    gen_op_add_reg_T0(s->aflag, R_EDI);
1204
    set_cc_op(s, CC_OP_SUBB + ot);
B
bellard 已提交
1205 1206 1207 1208 1209
}

static inline void gen_cmps(DisasContext *s, int ot)
{
    gen_string_movl_A0_ESI(s);
B
bellard 已提交
1210
    gen_op_ld_T0_A0(ot + s->mem_index);
B
bellard 已提交
1211
    gen_string_movl_A0_EDI(s);
B
bellard 已提交
1212
    gen_op_ld_T1_A0(ot + s->mem_index);
B
bellard 已提交
1213
    gen_op_cmpl_T0_T1_cc();
1214 1215 1216
    gen_op_movl_T0_Dshift(ot);
    gen_op_add_reg_T0(s->aflag, R_ESI);
    gen_op_add_reg_T0(s->aflag, R_EDI);
1217
    set_cc_op(s, CC_OP_SUBB + ot);
B
bellard 已提交
1218 1219 1220 1221
}

static inline void gen_ins(DisasContext *s, int ot)
{
P
pbrook 已提交
1222 1223
    if (use_icount)
        gen_io_start();
B
bellard 已提交
1224
    gen_string_movl_A0_EDI(s);
1225 1226
    /* Note: we must do this dummy write first to be restartable in
       case of page fault. */
B
bellard 已提交
1227
    gen_op_movl_T0_0();
B
bellard 已提交
1228
    gen_op_st_T0_A0(ot + s->mem_index);
1229
    gen_op_mov_TN_reg(OT_WORD, 1, R_EDX);
1230 1231
    tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[1]);
    tcg_gen_andi_i32(cpu_tmp2_i32, cpu_tmp2_i32, 0xffff);
P
pbrook 已提交
1232
    gen_helper_in_func(ot, cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
1233
    gen_op_st_T0_A0(ot + s->mem_index);
1234 1235
    gen_op_movl_T0_Dshift(ot);
    gen_op_add_reg_T0(s->aflag, R_EDI);
P
pbrook 已提交
1236 1237
    if (use_icount)
        gen_io_end();
B
bellard 已提交
1238 1239 1240 1241
}

static inline void gen_outs(DisasContext *s, int ot)
{
P
pbrook 已提交
1242 1243
    if (use_icount)
        gen_io_start();
B
bellard 已提交
1244
    gen_string_movl_A0_ESI(s);
B
bellard 已提交
1245
    gen_op_ld_T0_A0(ot + s->mem_index);
1246 1247

    gen_op_mov_TN_reg(OT_WORD, 1, R_EDX);
1248 1249 1250
    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 已提交
1251
    gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
1252

1253 1254
    gen_op_movl_T0_Dshift(ot);
    gen_op_add_reg_T0(s->aflag, R_ESI);
P
pbrook 已提交
1255 1256
    if (use_icount)
        gen_io_end();
B
bellard 已提交
1257 1258 1259 1260 1261 1262
}

/* 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 已提交
1263
                                 target_ulong cur_eip, target_ulong next_eip) \
B
bellard 已提交
1264
{                                                                             \
B
bellard 已提交
1265
    int l2;\
B
bellard 已提交
1266
    gen_update_cc_op(s);                                                      \
B
bellard 已提交
1267
    l2 = gen_jz_ecx_string(s, next_eip);                                      \
B
bellard 已提交
1268
    gen_ ## op(s, ot);                                                        \
1269
    gen_op_add_reg_im(s->aflag, R_ECX, -1);                                   \
B
bellard 已提交
1270 1271 1272
    /* a loop would cause two single step exceptions if ECX = 1               \
       before rep string_insn */                                              \
    if (!s->jmp_opt)                                                          \
1273
        gen_op_jz_ecx(s->aflag, l2);                                          \
B
bellard 已提交
1274 1275 1276 1277 1278
    gen_jmp(s, cur_eip);                                                      \
}

#define GEN_REPZ2(op)                                                         \
static inline void gen_repz_ ## op(DisasContext *s, int ot,                   \
B
bellard 已提交
1279 1280
                                   target_ulong cur_eip,                      \
                                   target_ulong next_eip,                     \
B
bellard 已提交
1281 1282
                                   int nz)                                    \
{                                                                             \
B
bellard 已提交
1283
    int l2;\
B
bellard 已提交
1284
    gen_update_cc_op(s);                                                      \
B
bellard 已提交
1285
    l2 = gen_jz_ecx_string(s, next_eip);                                      \
B
bellard 已提交
1286
    gen_ ## op(s, ot);                                                        \
1287
    gen_op_add_reg_im(s->aflag, R_ECX, -1);                                   \
1288
    gen_update_cc_op(s);                                                      \
1289
    gen_jcc1(s, (JCC_Z << 1) | (nz ^ 1), l2);                                 \
B
bellard 已提交
1290
    if (!s->jmp_opt)                                                          \
1291
        gen_op_jz_ecx(s->aflag, l2);                                          \
B
bellard 已提交
1292
    gen_jmp(s, cur_eip);                                                      \
1293
    set_cc_op(s, CC_OP_DYNAMIC);                                              \
B
bellard 已提交
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
}

GEN_REPZ(movs)
GEN_REPZ(stos)
GEN_REPZ(lods)
GEN_REPZ(ins)
GEN_REPZ(outs)
GEN_REPZ2(scas)
GEN_REPZ2(cmps)

P
pbrook 已提交
1304 1305 1306
static void gen_helper_fp_arith_ST0_FT0(int op)
{
    switch (op) {
B
Blue Swirl 已提交
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
    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 已提交
1331 1332
    }
}
B
bellard 已提交
1333 1334

/* NOTE the exception in "r" op ordering */
P
pbrook 已提交
1335 1336 1337 1338
static void gen_helper_fp_arith_STN_ST0(int op, int opreg)
{
    TCGv_i32 tmp = tcg_const_i32(opreg);
    switch (op) {
B
Blue Swirl 已提交
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
    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 已提交
1357 1358
    }
}
B
bellard 已提交
1359 1360 1361 1362 1363

/* 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 已提交
1364
        gen_op_mov_TN_reg(ot, 0, d);
B
bellard 已提交
1365
    } else {
B
bellard 已提交
1366
        gen_op_ld_T0_A0(ot + s1->mem_index);
B
bellard 已提交
1367 1368 1369
    }
    switch(op) {
    case OP_ADCL:
1370
        gen_compute_eflags_c(s1, cpu_tmp4);
B
bellard 已提交
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
        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);
        tcg_gen_mov_tl(cpu_cc_src, cpu_T[1]);
        tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_tmp4);
        tcg_gen_shli_i32(cpu_tmp2_i32, cpu_tmp2_i32, 2);
        tcg_gen_addi_i32(cpu_cc_op, cpu_tmp2_i32, CC_OP_ADDB + ot);
1382
        set_cc_op(s1, CC_OP_DYNAMIC);
B
bellard 已提交
1383
        break;
B
bellard 已提交
1384
    case OP_SBBL:
1385
        gen_compute_eflags_c(s1, cpu_tmp4);
B
bellard 已提交
1386 1387 1388
        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 已提交
1389
            gen_op_mov_reg_T0(ot, d);
B
bellard 已提交
1390 1391 1392 1393 1394 1395 1396
        else
            gen_op_st_T0_A0(ot + s1->mem_index);
        tcg_gen_mov_tl(cpu_cc_src, cpu_T[1]);
        tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_tmp4);
        tcg_gen_shli_i32(cpu_tmp2_i32, cpu_tmp2_i32, 2);
        tcg_gen_addi_i32(cpu_cc_op, cpu_tmp2_i32, CC_OP_SUBB + ot);
1397
        set_cc_op(s1, CC_OP_DYNAMIC);
B
bellard 已提交
1398
        break;
B
bellard 已提交
1399 1400
    case OP_ADDL:
        gen_op_addl_T0_T1();
B
bellard 已提交
1401 1402 1403 1404 1405
        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();
1406
        set_cc_op(s1, CC_OP_ADDB + ot);
B
bellard 已提交
1407 1408
        break;
    case OP_SUBL:
B
bellard 已提交
1409
        tcg_gen_sub_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
B
bellard 已提交
1410 1411 1412 1413 1414
        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();
1415
        set_cc_op(s1, CC_OP_SUBB + ot);
B
bellard 已提交
1416 1417 1418
        break;
    default:
    case OP_ANDL:
B
bellard 已提交
1419
        tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
B
bellard 已提交
1420 1421 1422 1423 1424
        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();
1425
        set_cc_op(s1, CC_OP_LOGICB + ot);
B
bellard 已提交
1426
        break;
B
bellard 已提交
1427
    case OP_ORL:
B
bellard 已提交
1428
        tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
B
bellard 已提交
1429 1430 1431 1432 1433
        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();
1434
        set_cc_op(s1, CC_OP_LOGICB + ot);
B
bellard 已提交
1435
        break;
B
bellard 已提交
1436
    case OP_XORL:
B
bellard 已提交
1437
        tcg_gen_xor_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
B
bellard 已提交
1438 1439 1440 1441 1442
        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();
1443
        set_cc_op(s1, CC_OP_LOGICB + ot);
B
bellard 已提交
1444 1445 1446
        break;
    case OP_CMPL:
        gen_op_cmpl_T0_T1_cc();
1447
        set_cc_op(s1, CC_OP_SUBB + ot);
B
bellard 已提交
1448 1449
        break;
    }
1450 1451
}

B
bellard 已提交
1452 1453 1454 1455
/* 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 已提交
1456
        gen_op_mov_TN_reg(ot, 0, d);
B
bellard 已提交
1457
    else
B
bellard 已提交
1458
        gen_op_ld_T0_A0(ot + s1->mem_index);
1459
    gen_compute_eflags_c(s1, cpu_cc_src);
B
bellard 已提交
1460
    if (c > 0) {
1461
        tcg_gen_addi_tl(cpu_T[0], cpu_T[0], 1);
1462
        set_cc_op(s1, CC_OP_INCB + ot);
B
bellard 已提交
1463
    } else {
1464
        tcg_gen_addi_tl(cpu_T[0], cpu_T[0], -1);
1465
        set_cc_op(s1, CC_OP_DECB + ot);
B
bellard 已提交
1466 1467
    }
    if (d != OR_TMP0)
B
bellard 已提交
1468
        gen_op_mov_reg_T0(ot, d);
B
bellard 已提交
1469
    else
B
bellard 已提交
1470
        gen_op_st_T0_A0(ot + s1->mem_index);
B
bellard 已提交
1471
    tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
B
bellard 已提交
1472 1473
}

1474 1475
static void gen_shift_rm_T1(DisasContext *s, int ot, int op1, 
                            int is_right, int is_arith)
B
bellard 已提交
1476
{
1477 1478
    target_ulong mask;
    int shift_label;
1479
    TCGv t0, t1, t2;
1480

1481
    if (ot == OT_QUAD) {
1482
        mask = 0x3f;
1483
    } else {
1484
        mask = 0x1f;
1485
    }
1486

1487
    /* load */
1488
    if (op1 == OR_TMP0) {
1489
        gen_op_ld_T0_A0(ot + s->mem_index);
1490
    } else {
1491
        gen_op_mov_TN_reg(ot, 0, op1);
1492
    }
1493

1494 1495 1496
    t0 = tcg_temp_local_new();
    t1 = tcg_temp_local_new();
    t2 = tcg_temp_local_new();
1497

1498
    tcg_gen_andi_tl(t2, cpu_T[1], mask);
1499 1500 1501

    if (is_right) {
        if (is_arith) {
B
bellard 已提交
1502
            gen_exts(ot, cpu_T[0]);
1503 1504
            tcg_gen_mov_tl(t0, cpu_T[0]);
            tcg_gen_sar_tl(cpu_T[0], cpu_T[0], t2);
1505
        } else {
B
bellard 已提交
1506
            gen_extu(ot, cpu_T[0]);
1507 1508
            tcg_gen_mov_tl(t0, cpu_T[0]);
            tcg_gen_shr_tl(cpu_T[0], cpu_T[0], t2);
1509 1510
        }
    } else {
1511 1512
        tcg_gen_mov_tl(t0, cpu_T[0]);
        tcg_gen_shl_tl(cpu_T[0], cpu_T[0], t2);
1513 1514 1515
    }

    /* store */
1516
    if (op1 == OR_TMP0) {
1517
        gen_op_st_T0_A0(ot + s->mem_index);
1518
    } else {
1519
        gen_op_mov_reg_T0(ot, op1);
1520 1521
    }

1522 1523
    /* update eflags */
    gen_update_cc_op(s);
1524

1525
    tcg_gen_mov_tl(t1, cpu_T[0]);
1526

1527
    shift_label = gen_new_label();
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
    tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, shift_label);

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

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

1543
    if (is_right) {
1544
        tcg_gen_movi_i32(cpu_cc_op, CC_OP_SARB + ot);
1545
    } else {
1546
        tcg_gen_movi_i32(cpu_cc_op, CC_OP_SHLB + ot);
1547 1548
    }

1549
    gen_set_label(shift_label);
1550
    set_cc_op(s, CC_OP_DYNAMIC); /* cannot predict flags after */
1551 1552 1553

    tcg_temp_free(t0);
    tcg_temp_free(t1);
1554
    tcg_temp_free(t2);
1555 1556
}

B
bellard 已提交
1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
static void gen_shift_rm_im(DisasContext *s, int ot, int op1, int op2,
                            int is_right, int is_arith)
{
    int mask;
    
    if (ot == OT_QUAD)
        mask = 0x3f;
    else
        mask = 0x1f;

    /* load */
    if (op1 == OR_TMP0)
        gen_op_ld_T0_A0(ot + s->mem_index);
    else
        gen_op_mov_TN_reg(ot, 0, op1);

    op2 &= mask;
    if (op2 != 0) {
        if (is_right) {
            if (is_arith) {
                gen_exts(ot, cpu_T[0]);
B
bellard 已提交
1578
                tcg_gen_sari_tl(cpu_tmp4, cpu_T[0], op2 - 1);
B
bellard 已提交
1579 1580 1581
                tcg_gen_sari_tl(cpu_T[0], cpu_T[0], op2);
            } else {
                gen_extu(ot, cpu_T[0]);
B
bellard 已提交
1582
                tcg_gen_shri_tl(cpu_tmp4, cpu_T[0], op2 - 1);
B
bellard 已提交
1583 1584 1585
                tcg_gen_shri_tl(cpu_T[0], cpu_T[0], op2);
            }
        } else {
B
bellard 已提交
1586
            tcg_gen_shli_tl(cpu_tmp4, cpu_T[0], op2 - 1);
B
bellard 已提交
1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
            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 已提交
1599
        tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
B
bellard 已提交
1600
        tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
1601
        set_cc_op(s, (is_right ? CC_OP_SARB : CC_OP_SHLB) + ot);
B
bellard 已提交
1602 1603 1604
    }
}

1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
static inline void tcg_gen_lshift(TCGv ret, TCGv arg1, target_long arg2)
{
    if (arg2 >= 0)
        tcg_gen_shli_tl(ret, arg1, arg2);
    else
        tcg_gen_shri_tl(ret, arg1, -arg2);
}

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

    /* XXX: inefficient, but we must use local temps */
P
pbrook 已提交
1621 1622 1623 1624
    t0 = tcg_temp_local_new();
    t1 = tcg_temp_local_new();
    t2 = tcg_temp_local_new();
    a0 = tcg_temp_local_new();
1625

1626 1627 1628 1629 1630 1631
    if (ot == OT_QUAD)
        mask = 0x3f;
    else
        mask = 0x1f;

    /* load */
1632 1633 1634 1635 1636 1637
    if (op1 == OR_TMP0) {
        tcg_gen_mov_tl(a0, cpu_A0);
        gen_op_ld_v(ot + s->mem_index, t0, a0);
    } else {
        gen_op_mov_v_reg(ot, t0, op1);
    }
1638

1639 1640 1641
    tcg_gen_mov_tl(t1, cpu_T[1]);

    tcg_gen_andi_tl(t1, t1, mask);
1642 1643 1644 1645

    /* Must test zero case to avoid using undefined behaviour in TCG
       shifts. */
    label1 = gen_new_label();
1646
    tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, label1);
1647 1648
    
    if (ot <= OT_WORD)
1649
        tcg_gen_andi_tl(cpu_tmp0, t1, (1 << (3 + ot)) - 1);
1650
    else
1651
        tcg_gen_mov_tl(cpu_tmp0, t1);
1652
    
1653 1654
    gen_extu(ot, t0);
    tcg_gen_mov_tl(t2, t0);
1655 1656 1657 1658 1659

    data_bits = 8 << ot;
    /* XXX: rely on behaviour of shifts when operand 2 overflows (XXX:
       fix TCG definition) */
    if (is_right) {
1660
        tcg_gen_shr_tl(cpu_tmp4, t0, cpu_tmp0);
1661
        tcg_gen_subfi_tl(cpu_tmp0, data_bits, cpu_tmp0);
1662
        tcg_gen_shl_tl(t0, t0, cpu_tmp0);
1663
    } else {
1664
        tcg_gen_shl_tl(cpu_tmp4, t0, cpu_tmp0);
1665
        tcg_gen_subfi_tl(cpu_tmp0, data_bits, cpu_tmp0);
1666
        tcg_gen_shr_tl(t0, t0, cpu_tmp0);
1667
    }
1668
    tcg_gen_or_tl(t0, t0, cpu_tmp4);
1669 1670 1671

    gen_set_label(label1);
    /* store */
1672 1673 1674 1675 1676
    if (op1 == OR_TMP0) {
        gen_op_st_v(ot + s->mem_index, t0, a0);
    } else {
        gen_op_mov_reg_v(ot, op1, t0);
    }
1677
    
1678
    /* update eflags.  It is needed anyway most of the time, do it always.  */
1679
    gen_compute_eflags(s);
1680
    assert(s->cc_op == CC_OP_EFLAGS);
1681 1682

    label2 = gen_new_label();
1683
    tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, label2);
1684 1685

    tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~(CC_O | CC_C));
1686
    tcg_gen_xor_tl(cpu_tmp0, t2, t0);
1687 1688 1689 1690
    tcg_gen_lshift(cpu_tmp0, cpu_tmp0, 11 - (data_bits - 1));
    tcg_gen_andi_tl(cpu_tmp0, cpu_tmp0, CC_O);
    tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, cpu_tmp0);
    if (is_right) {
1691
        tcg_gen_shri_tl(t0, t0, data_bits - 1);
1692
    }
1693 1694
    tcg_gen_andi_tl(t0, t0, CC_C);
    tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, t0);
1695

1696
    gen_set_label(label2);
1697 1698 1699 1700 1701

    tcg_temp_free(t0);
    tcg_temp_free(t1);
    tcg_temp_free(t2);
    tcg_temp_free(a0);
1702 1703
}

M
malc 已提交
1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 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
static void gen_rot_rm_im(DisasContext *s, int ot, int op1, int op2,
                          int is_right)
{
    int mask;
    int data_bits;
    TCGv t0, t1, a0;

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

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

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

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

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

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

    if (op2 != 0) {
        /* update eflags */
1756
        gen_compute_eflags(s);
1757
        assert(s->cc_op == CC_OP_EFLAGS);
1758

M
malc 已提交
1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775
        tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~(CC_O | CC_C));
        tcg_gen_xor_tl(cpu_tmp0, t1, t0);
        tcg_gen_lshift(cpu_tmp0, cpu_tmp0, 11 - (data_bits - 1));
        tcg_gen_andi_tl(cpu_tmp0, cpu_tmp0, CC_O);
        tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, cpu_tmp0);
        if (is_right) {
            tcg_gen_shri_tl(t0, t0, data_bits - 1);
        }
        tcg_gen_andi_tl(t0, t0, CC_C);
        tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, t0);
    }

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

1776 1777 1778 1779
/* XXX: add faster immediate = 1 case */
static void gen_rotc_rm_T1(DisasContext *s, int ot, int op1, 
                           int is_right)
{
1780
    gen_compute_eflags(s);
1781
    assert(s->cc_op == CC_OP_EFLAGS);
1782 1783 1784 1785 1786 1787 1788

    /* 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 已提交
1789 1790
    if (is_right) {
        switch (ot) {
1791
        case OT_BYTE:
1792 1793
            gen_helper_rcrb(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
1794
        case OT_WORD:
1795 1796
            gen_helper_rcrw(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
1797
        case OT_LONG:
1798 1799
            gen_helper_rcrl(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1800
#ifdef TARGET_X86_64
1801
        case OT_QUAD:
1802 1803
            gen_helper_rcrq(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1804 1805 1806 1807
#endif
        }
    } else {
        switch (ot) {
1808
        case OT_BYTE:
1809 1810
            gen_helper_rclb(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
1811
        case OT_WORD:
1812 1813
            gen_helper_rclw(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
1814
        case OT_LONG:
1815 1816
            gen_helper_rcll(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1817
#ifdef TARGET_X86_64
1818
        case OT_QUAD:
1819 1820
            gen_helper_rclq(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1821 1822 1823
#endif
        }
    }
1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836
    /* 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 */
static void gen_shiftd_rm_T1_T3(DisasContext *s, int ot, int op1, 
                                int is_right)
{
    int label1, label2, data_bits;
    target_ulong mask;
1837 1838
    TCGv t0, t1, t2, a0;

P
pbrook 已提交
1839 1840 1841 1842
    t0 = tcg_temp_local_new();
    t1 = tcg_temp_local_new();
    t2 = tcg_temp_local_new();
    a0 = tcg_temp_local_new();
1843 1844 1845 1846 1847 1848 1849

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

    /* load */
1850 1851 1852 1853 1854 1855
    if (op1 == OR_TMP0) {
        tcg_gen_mov_tl(a0, cpu_A0);
        gen_op_ld_v(ot + s->mem_index, t0, a0);
    } else {
        gen_op_mov_v_reg(ot, t0, op1);
    }
1856 1857

    tcg_gen_andi_tl(cpu_T3, cpu_T3, mask);
1858 1859 1860 1861

    tcg_gen_mov_tl(t1, cpu_T[1]);
    tcg_gen_mov_tl(t2, cpu_T3);

1862 1863 1864
    /* Must test zero case to avoid using undefined behaviour in TCG
       shifts. */
    label1 = gen_new_label();
1865
    tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, label1);
1866
    
1867
    tcg_gen_addi_tl(cpu_tmp5, t2, -1);
1868 1869 1870
    if (ot == OT_WORD) {
        /* Note: we implement the Intel behaviour for shift count > 16 */
        if (is_right) {
1871 1872 1873 1874
            tcg_gen_andi_tl(t0, t0, 0xffff);
            tcg_gen_shli_tl(cpu_tmp0, t1, 16);
            tcg_gen_or_tl(t0, t0, cpu_tmp0);
            tcg_gen_ext32u_tl(t0, t0);
1875

1876
            tcg_gen_shr_tl(cpu_tmp4, t0, cpu_tmp5);
1877 1878
            
            /* only needed if count > 16, but a test would complicate */
1879
            tcg_gen_subfi_tl(cpu_tmp5, 32, t2);
1880
            tcg_gen_shl_tl(cpu_tmp0, t0, cpu_tmp5);
1881

1882
            tcg_gen_shr_tl(t0, t0, t2);
1883

1884
            tcg_gen_or_tl(t0, t0, cpu_tmp0);
1885 1886
        } else {
            /* XXX: not optimal */
1887 1888 1889 1890
            tcg_gen_andi_tl(t0, t0, 0xffff);
            tcg_gen_shli_tl(t1, t1, 16);
            tcg_gen_or_tl(t1, t1, t0);
            tcg_gen_ext32u_tl(t1, t1);
1891
            
1892
            tcg_gen_shl_tl(cpu_tmp4, t0, cpu_tmp5);
1893
            tcg_gen_subfi_tl(cpu_tmp0, 32, cpu_tmp5);
1894 1895
            tcg_gen_shr_tl(cpu_tmp5, t1, cpu_tmp0);
            tcg_gen_or_tl(cpu_tmp4, cpu_tmp4, cpu_tmp5);
1896

1897
            tcg_gen_shl_tl(t0, t0, t2);
1898
            tcg_gen_subfi_tl(cpu_tmp5, 32, t2);
1899 1900
            tcg_gen_shr_tl(t1, t1, cpu_tmp5);
            tcg_gen_or_tl(t0, t0, t1);
1901 1902 1903 1904 1905
        }
    } else {
        data_bits = 8 << ot;
        if (is_right) {
            if (ot == OT_LONG)
1906
                tcg_gen_ext32u_tl(t0, t0);
1907

1908
            tcg_gen_shr_tl(cpu_tmp4, t0, cpu_tmp5);
1909

1910
            tcg_gen_shr_tl(t0, t0, t2);
1911
            tcg_gen_subfi_tl(cpu_tmp5, data_bits, t2);
1912 1913
            tcg_gen_shl_tl(t1, t1, cpu_tmp5);
            tcg_gen_or_tl(t0, t0, t1);
1914 1915 1916
            
        } else {
            if (ot == OT_LONG)
1917
                tcg_gen_ext32u_tl(t1, t1);
1918

1919
            tcg_gen_shl_tl(cpu_tmp4, t0, cpu_tmp5);
1920
            
1921
            tcg_gen_shl_tl(t0, t0, t2);
1922
            tcg_gen_subfi_tl(cpu_tmp5, data_bits, t2);
1923 1924
            tcg_gen_shr_tl(t1, t1, cpu_tmp5);
            tcg_gen_or_tl(t0, t0, t1);
1925 1926
        }
    }
1927
    tcg_gen_mov_tl(t1, cpu_tmp4);
1928 1929 1930

    gen_set_label(label1);
    /* store */
1931 1932 1933 1934 1935
    if (op1 == OR_TMP0) {
        gen_op_st_v(ot + s->mem_index, t0, a0);
    } else {
        gen_op_mov_reg_v(ot, op1, t0);
    }
1936 1937
    
    /* update eflags */
1938
    gen_update_cc_op(s);
1939 1940

    label2 = gen_new_label();
1941
    tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, label2);
1942

1943 1944
    tcg_gen_mov_tl(cpu_cc_src, t1);
    tcg_gen_mov_tl(cpu_cc_dst, t0);
1945 1946 1947 1948 1949 1950
    if (is_right) {
        tcg_gen_movi_i32(cpu_cc_op, CC_OP_SARB + ot);
    } else {
        tcg_gen_movi_i32(cpu_cc_op, CC_OP_SHLB + ot);
    }
    gen_set_label(label2);
1951
    set_cc_op(s, CC_OP_DYNAMIC); /* cannot predict flags after */
1952 1953 1954 1955 1956

    tcg_temp_free(t0);
    tcg_temp_free(t1);
    tcg_temp_free(t2);
    tcg_temp_free(a0);
1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986
}

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 已提交
1987 1988 1989 1990
}

static void gen_shifti(DisasContext *s1, int op, int ot, int d, int c)
{
B
bellard 已提交
1991
    switch(op) {
M
malc 已提交
1992 1993 1994 1995 1996 1997
    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 已提交
1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
    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 已提交
2014 2015
}

2016 2017
static void gen_lea_modrm(CPUX86State *env, DisasContext *s, int modrm,
                          int *reg_ptr, int *offset_ptr)
B
bellard 已提交
2018
{
B
bellard 已提交
2019
    target_long disp;
B
bellard 已提交
2020
    int havesib;
B
bellard 已提交
2021
    int base;
B
bellard 已提交
2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039
    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;
2040

B
bellard 已提交
2041 2042
        if (base == 4) {
            havesib = 1;
2043
            code = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
2044
            scale = (code >> 6) & 3;
B
bellard 已提交
2045 2046
            index = ((code >> 3) & 7) | REX_X(s);
            base = (code & 7);
B
bellard 已提交
2047
        }
B
bellard 已提交
2048
        base |= REX_B(s);
B
bellard 已提交
2049 2050 2051

        switch (mod) {
        case 0:
B
bellard 已提交
2052
            if ((base & 7) == 5) {
B
bellard 已提交
2053
                base = -1;
2054
                disp = (int32_t)cpu_ldl_code(env, s->pc);
B
bellard 已提交
2055
                s->pc += 4;
B
bellard 已提交
2056 2057 2058
                if (CODE64(s) && !havesib) {
                    disp += s->pc + s->rip_offset;
                }
B
bellard 已提交
2059 2060 2061 2062 2063
            } else {
                disp = 0;
            }
            break;
        case 1:
2064
            disp = (int8_t)cpu_ldub_code(env, s->pc++);
B
bellard 已提交
2065 2066 2067
            break;
        default:
        case 2:
2068
            disp = (int32_t)cpu_ldl_code(env, s->pc);
B
bellard 已提交
2069 2070 2071
            s->pc += 4;
            break;
        }
2072

B
bellard 已提交
2073 2074 2075 2076
        if (base >= 0) {
            /* for correct popl handling with esp */
            if (base == 4 && s->popl_esp_hack)
                disp += s->popl_esp_hack;
B
bellard 已提交
2077 2078
#ifdef TARGET_X86_64
            if (s->aflag == 2) {
B
bellard 已提交
2079
                gen_op_movq_A0_reg(base);
B
bellard 已提交
2080
                if (disp != 0) {
B
bellard 已提交
2081
                    gen_op_addq_A0_im(disp);
B
bellard 已提交
2082
                }
2083
            } else
B
bellard 已提交
2084 2085
#endif
            {
B
bellard 已提交
2086
                gen_op_movl_A0_reg(base);
B
bellard 已提交
2087 2088 2089
                if (disp != 0)
                    gen_op_addl_A0_im(disp);
            }
B
bellard 已提交
2090
        } else {
B
bellard 已提交
2091 2092
#ifdef TARGET_X86_64
            if (s->aflag == 2) {
B
bellard 已提交
2093
                gen_op_movq_A0_im(disp);
2094
            } else
B
bellard 已提交
2095 2096 2097 2098
#endif
            {
                gen_op_movl_A0_im(disp);
            }
B
bellard 已提交
2099
        }
2100 2101
        /* index == 4 means no index */
        if (havesib && (index != 4)) {
B
bellard 已提交
2102 2103
#ifdef TARGET_X86_64
            if (s->aflag == 2) {
B
bellard 已提交
2104
                gen_op_addq_A0_reg_sN(scale, index);
2105
            } else
B
bellard 已提交
2106 2107
#endif
            {
B
bellard 已提交
2108
                gen_op_addl_A0_reg_sN(scale, index);
B
bellard 已提交
2109
            }
B
bellard 已提交
2110 2111 2112 2113 2114 2115 2116 2117
        }
        if (must_add_seg) {
            if (override < 0) {
                if (base == R_EBP || base == R_ESP)
                    override = R_SS;
                else
                    override = R_DS;
            }
B
bellard 已提交
2118 2119
#ifdef TARGET_X86_64
            if (s->aflag == 2) {
B
bellard 已提交
2120
                gen_op_addq_A0_seg(override);
2121
            } else
B
bellard 已提交
2122 2123
#endif
            {
2124
                gen_op_addl_A0_seg(s, override);
B
bellard 已提交
2125
            }
B
bellard 已提交
2126 2127 2128 2129 2130
        }
    } else {
        switch (mod) {
        case 0:
            if (rm == 6) {
2131
                disp = cpu_lduw_code(env, s->pc);
B
bellard 已提交
2132 2133 2134 2135 2136 2137 2138 2139 2140
                s->pc += 2;
                gen_op_movl_A0_im(disp);
                rm = 0; /* avoid SS override */
                goto no_rm;
            } else {
                disp = 0;
            }
            break;
        case 1:
2141
            disp = (int8_t)cpu_ldub_code(env, s->pc++);
B
bellard 已提交
2142 2143 2144
            break;
        default:
        case 2:
2145
            disp = cpu_lduw_code(env, s->pc);
B
bellard 已提交
2146 2147 2148 2149 2150
            s->pc += 2;
            break;
        }
        switch(rm) {
        case 0:
B
bellard 已提交
2151 2152
            gen_op_movl_A0_reg(R_EBX);
            gen_op_addl_A0_reg_sN(0, R_ESI);
B
bellard 已提交
2153 2154
            break;
        case 1:
B
bellard 已提交
2155 2156
            gen_op_movl_A0_reg(R_EBX);
            gen_op_addl_A0_reg_sN(0, R_EDI);
B
bellard 已提交
2157 2158
            break;
        case 2:
B
bellard 已提交
2159 2160
            gen_op_movl_A0_reg(R_EBP);
            gen_op_addl_A0_reg_sN(0, R_ESI);
B
bellard 已提交
2161 2162
            break;
        case 3:
B
bellard 已提交
2163 2164
            gen_op_movl_A0_reg(R_EBP);
            gen_op_addl_A0_reg_sN(0, R_EDI);
B
bellard 已提交
2165 2166
            break;
        case 4:
B
bellard 已提交
2167
            gen_op_movl_A0_reg(R_ESI);
B
bellard 已提交
2168 2169
            break;
        case 5:
B
bellard 已提交
2170
            gen_op_movl_A0_reg(R_EDI);
B
bellard 已提交
2171 2172
            break;
        case 6:
B
bellard 已提交
2173
            gen_op_movl_A0_reg(R_EBP);
B
bellard 已提交
2174 2175 2176
            break;
        default:
        case 7:
B
bellard 已提交
2177
            gen_op_movl_A0_reg(R_EBX);
B
bellard 已提交
2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190
            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;
            }
2191
            gen_op_addl_A0_seg(s, override);
B
bellard 已提交
2192 2193 2194 2195 2196 2197 2198 2199 2200
        }
    }

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

2201
static void gen_nop_modrm(CPUX86State *env, DisasContext *s, int modrm)
B
bellard 已提交
2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212
{
    int mod, rm, base, code;

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

    if (s->aflag) {

        base = rm;
2213

B
bellard 已提交
2214
        if (base == 4) {
2215
            code = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
2216 2217
            base = (code & 7);
        }
2218

B
bellard 已提交
2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250
        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 已提交
2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261
/* 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) {
2262 2263
#ifdef TARGET_X86_64
        if (CODE64(s)) {
B
bellard 已提交
2264
            gen_op_addq_A0_seg(override);
2265
        } else
2266 2267
#endif
        {
2268
            gen_op_addl_A0_seg(s, override);
2269
        }
B
bellard 已提交
2270 2271 2272
    }
}

B
balrog 已提交
2273
/* generate modrm memory load or store of 'reg'. TMP0 is used if reg ==
B
bellard 已提交
2274
   OR_TMP0 */
2275 2276
static void gen_ldst_modrm(CPUX86State *env, DisasContext *s, int modrm,
                           int ot, int reg, int is_store)
B
bellard 已提交
2277 2278 2279 2280
{
    int mod, rm, opreg, disp;

    mod = (modrm >> 6) & 3;
B
bellard 已提交
2281
    rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
2282 2283 2284
    if (mod == 3) {
        if (is_store) {
            if (reg != OR_TMP0)
B
bellard 已提交
2285 2286
                gen_op_mov_TN_reg(ot, 0, reg);
            gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
2287
        } else {
B
bellard 已提交
2288
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
2289
            if (reg != OR_TMP0)
B
bellard 已提交
2290
                gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
2291 2292
        }
    } else {
2293
        gen_lea_modrm(env, s, modrm, &opreg, &disp);
B
bellard 已提交
2294 2295
        if (is_store) {
            if (reg != OR_TMP0)
B
bellard 已提交
2296 2297
                gen_op_mov_TN_reg(ot, 0, reg);
            gen_op_st_T0_A0(ot + s->mem_index);
B
bellard 已提交
2298
        } else {
B
bellard 已提交
2299
            gen_op_ld_T0_A0(ot + s->mem_index);
B
bellard 已提交
2300
            if (reg != OR_TMP0)
B
bellard 已提交
2301
                gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
2302 2303 2304 2305
        }
    }
}

2306
static inline uint32_t insn_get(CPUX86State *env, DisasContext *s, int ot)
B
bellard 已提交
2307 2308 2309 2310 2311
{
    uint32_t ret;

    switch(ot) {
    case OT_BYTE:
2312
        ret = cpu_ldub_code(env, s->pc);
B
bellard 已提交
2313 2314 2315
        s->pc++;
        break;
    case OT_WORD:
2316
        ret = cpu_lduw_code(env, s->pc);
B
bellard 已提交
2317 2318 2319 2320
        s->pc += 2;
        break;
    default:
    case OT_LONG:
2321
        ret = cpu_ldl_code(env, s->pc);
B
bellard 已提交
2322 2323 2324 2325 2326 2327
        s->pc += 4;
        break;
    }
    return ret;
}

B
bellard 已提交
2328 2329 2330 2331 2332 2333 2334 2335
static inline int insn_const_size(unsigned int ot)
{
    if (ot <= OT_LONG)
        return 1 << ot;
    else
        return 4;
}

2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346
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 已提交
2347
        tcg_gen_goto_tb(tb_num);
2348
        gen_jmp_im(eip);
2349
        tcg_gen_exit_tb((tcg_target_long)tb + tb_num);
2350 2351 2352 2353 2354 2355 2356
    } else {
        /* jump to another page: currently not optimized */
        gen_jmp_im(eip);
        gen_eob(s);
    }
}

2357
static inline void gen_jcc(DisasContext *s, int b,
B
bellard 已提交
2358
                           target_ulong val, target_ulong next_eip)
B
bellard 已提交
2359
{
2360
    int l1, l2;
2361

B
bellard 已提交
2362
    if (s->jmp_opt) {
2363
        gen_update_cc_op(s);
B
bellard 已提交
2364
        l1 = gen_new_label();
2365
        gen_jcc1(s, b, l1);
2366
        set_cc_op(s, CC_OP_DYNAMIC);
2367
        
2368
        gen_goto_tb(s, 0, next_eip);
B
bellard 已提交
2369 2370

        gen_set_label(l1);
2371
        gen_goto_tb(s, 1, val);
J
Jun Koi 已提交
2372
        s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2373
    } else {
B
bellard 已提交
2374 2375
        l1 = gen_new_label();
        l2 = gen_new_label();
2376
        gen_jcc1(s, b, l1);
2377

B
bellard 已提交
2378
        gen_jmp_im(next_eip);
2379 2380
        tcg_gen_br(l2);

B
bellard 已提交
2381 2382 2383
        gen_set_label(l1);
        gen_jmp_im(val);
        gen_set_label(l2);
B
bellard 已提交
2384 2385 2386 2387 2388 2389
        gen_eob(s);
    }
}

static void gen_setcc(DisasContext *s, int b)
{
2390
    int inv, jcc_op, l1;
2391
    TCGv t0;
B
bellard 已提交
2392

2393 2394
    if (is_fast_jcc_case(s, b)) {
        /* nominal case: we use a jump */
2395
        /* XXX: make it faster by adding new instructions in TCG */
P
pbrook 已提交
2396
        t0 = tcg_temp_local_new();
2397
        tcg_gen_movi_tl(t0, 0);
2398
        l1 = gen_new_label();
2399
        gen_jcc1(s, b ^ 1, l1);
2400
        tcg_gen_movi_tl(t0, 1);
2401
        gen_set_label(l1);
2402 2403
        tcg_gen_mov_tl(cpu_T[0], t0);
        tcg_temp_free(t0);
2404 2405 2406 2407 2408 2409
    } else {
        /* slow case: it is more efficient not to generate a jump,
           although it is questionnable whether this optimization is
           worth to */
        inv = b & 1;
        jcc_op = (b >> 1) & 7;
2410
        gen_setcc_slow_T0(s, jcc_op);
2411 2412 2413
        if (inv) {
            tcg_gen_xori_tl(cpu_T[0], cpu_T[0], 1);
        }
B
bellard 已提交
2414 2415 2416
    }
}

2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432
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 已提交
2433 2434
/* move T0 to seg_reg and compute if the CPU state may change. Never
   call this function with seg_reg == R_CS */
B
bellard 已提交
2435
static void gen_movl_seg_T0(DisasContext *s, int seg_reg, target_ulong cur_eip)
B
bellard 已提交
2436
{
2437 2438
    if (s->pe && !s->vm86) {
        /* XXX: optimize by finding processor state dynamically */
2439
        gen_update_cc_op(s);
B
bellard 已提交
2440
        gen_jmp_im(cur_eip);
2441
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
2442
        gen_helper_load_seg(cpu_env, tcg_const_i32(seg_reg), cpu_tmp2_i32);
B
bellard 已提交
2443 2444 2445 2446 2447
        /* 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 已提交
2448
            s->is_jmp = DISAS_TB_JUMP;
2449
    } else {
2450
        gen_op_movl_seg_T0_vm(seg_reg);
B
bellard 已提交
2451
        if (seg_reg == R_SS)
J
Jun Koi 已提交
2452
            s->is_jmp = DISAS_TB_JUMP;
2453
    }
B
bellard 已提交
2454 2455
}

T
ths 已提交
2456 2457 2458 2459 2460
static inline int svm_is_rep(int prefixes)
{
    return ((prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) ? 8 : 0);
}

B
bellard 已提交
2461
static inline void
T
ths 已提交
2462
gen_svm_check_intercept_param(DisasContext *s, target_ulong pc_start,
2463
                              uint32_t type, uint64_t param)
T
ths 已提交
2464
{
B
bellard 已提交
2465 2466 2467
    /* no SVM activated; fast case */
    if (likely(!(s->flags & HF_SVMI_MASK)))
        return;
2468
    gen_update_cc_op(s);
B
bellard 已提交
2469
    gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
2470
    gen_helper_svm_check_intercept_param(cpu_env, tcg_const_i32(type),
P
pbrook 已提交
2471
                                         tcg_const_i64(param));
T
ths 已提交
2472 2473
}

B
bellard 已提交
2474
static inline void
T
ths 已提交
2475 2476
gen_svm_check_intercept(DisasContext *s, target_ulong pc_start, uint64_t type)
{
B
bellard 已提交
2477
    gen_svm_check_intercept_param(s, pc_start, type, 0);
T
ths 已提交
2478 2479
}

2480 2481
static inline void gen_stack_update(DisasContext *s, int addend)
{
B
bellard 已提交
2482 2483
#ifdef TARGET_X86_64
    if (CODE64(s)) {
2484
        gen_op_add_reg_im(2, R_ESP, addend);
B
bellard 已提交
2485 2486
    } else
#endif
2487
    if (s->ss32) {
2488
        gen_op_add_reg_im(1, R_ESP, addend);
2489
    } else {
2490
        gen_op_add_reg_im(0, R_ESP, addend);
2491 2492 2493
    }
}

B
bellard 已提交
2494 2495 2496
/* generate a push. It depends on ss32, addseg and dflag */
static void gen_push_T0(DisasContext *s)
{
B
bellard 已提交
2497 2498
#ifdef TARGET_X86_64
    if (CODE64(s)) {
B
bellard 已提交
2499
        gen_op_movq_A0_reg(R_ESP);
2500
        if (s->dflag) {
B
bellard 已提交
2501 2502
            gen_op_addq_A0_im(-8);
            gen_op_st_T0_A0(OT_QUAD + s->mem_index);
2503
        } else {
B
bellard 已提交
2504 2505
            gen_op_addq_A0_im(-2);
            gen_op_st_T0_A0(OT_WORD + s->mem_index);
2506
        }
B
bellard 已提交
2507
        gen_op_mov_reg_A0(2, R_ESP);
2508
    } else
B
bellard 已提交
2509 2510
#endif
    {
B
bellard 已提交
2511
        gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2512
        if (!s->dflag)
B
bellard 已提交
2513
            gen_op_addl_A0_im(-2);
B
bellard 已提交
2514
        else
B
bellard 已提交
2515
            gen_op_addl_A0_im(-4);
B
bellard 已提交
2516 2517
        if (s->ss32) {
            if (s->addseg) {
2518
                tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2519
                gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2520 2521 2522
            }
        } else {
            gen_op_andl_A0_ffff();
2523
            tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2524
            gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2525
        }
B
bellard 已提交
2526
        gen_op_st_T0_A0(s->dflag + 1 + s->mem_index);
B
bellard 已提交
2527
        if (s->ss32 && !s->addseg)
B
bellard 已提交
2528
            gen_op_mov_reg_A0(1, R_ESP);
B
bellard 已提交
2529
        else
B
bellard 已提交
2530
            gen_op_mov_reg_T1(s->ss32 + 1, R_ESP);
B
bellard 已提交
2531 2532 2533
    }
}

2534 2535 2536
/* 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 已提交
2537
{
B
bellard 已提交
2538 2539
#ifdef TARGET_X86_64
    if (CODE64(s)) {
B
bellard 已提交
2540
        gen_op_movq_A0_reg(R_ESP);
2541
        if (s->dflag) {
B
bellard 已提交
2542 2543
            gen_op_addq_A0_im(-8);
            gen_op_st_T1_A0(OT_QUAD + s->mem_index);
2544
        } else {
B
bellard 已提交
2545 2546
            gen_op_addq_A0_im(-2);
            gen_op_st_T0_A0(OT_WORD + s->mem_index);
2547
        }
B
bellard 已提交
2548
        gen_op_mov_reg_A0(2, R_ESP);
2549
    } else
B
bellard 已提交
2550 2551
#endif
    {
B
bellard 已提交
2552
        gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2553
        if (!s->dflag)
B
bellard 已提交
2554
            gen_op_addl_A0_im(-2);
B
bellard 已提交
2555
        else
B
bellard 已提交
2556
            gen_op_addl_A0_im(-4);
B
bellard 已提交
2557 2558
        if (s->ss32) {
            if (s->addseg) {
2559
                gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2560 2561 2562
            }
        } else {
            gen_op_andl_A0_ffff();
2563
            gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2564
        }
B
bellard 已提交
2565
        gen_op_st_T1_A0(s->dflag + 1 + s->mem_index);
2566

B
bellard 已提交
2567
        if (s->ss32 && !s->addseg)
B
bellard 已提交
2568
            gen_op_mov_reg_A0(1, R_ESP);
B
bellard 已提交
2569 2570
        else
            gen_stack_update(s, (-2) << s->dflag);
B
bellard 已提交
2571 2572 2573
    }
}

2574 2575
/* two step pop is necessary for precise exceptions */
static void gen_pop_T0(DisasContext *s)
B
bellard 已提交
2576
{
B
bellard 已提交
2577 2578
#ifdef TARGET_X86_64
    if (CODE64(s)) {
B
bellard 已提交
2579 2580
        gen_op_movq_A0_reg(R_ESP);
        gen_op_ld_T0_A0((s->dflag ? OT_QUAD : OT_WORD) + s->mem_index);
2581
    } else
B
bellard 已提交
2582 2583
#endif
    {
B
bellard 已提交
2584
        gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2585 2586
        if (s->ss32) {
            if (s->addseg)
2587
                gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2588 2589
        } else {
            gen_op_andl_A0_ffff();
2590
            gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2591
        }
B
bellard 已提交
2592
        gen_op_ld_T0_A0(s->dflag + 1 + s->mem_index);
B
bellard 已提交
2593 2594 2595 2596 2597
    }
}

static void gen_pop_update(DisasContext *s)
{
B
bellard 已提交
2598
#ifdef TARGET_X86_64
2599
    if (CODE64(s) && s->dflag) {
B
bellard 已提交
2600 2601 2602 2603 2604 2605
        gen_stack_update(s, 8);
    } else
#endif
    {
        gen_stack_update(s, 2 << s->dflag);
    }
B
bellard 已提交
2606 2607 2608 2609
}

static void gen_stack_A0(DisasContext *s)
{
B
bellard 已提交
2610
    gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2611 2612
    if (!s->ss32)
        gen_op_andl_A0_ffff();
2613
    tcg_gen_mov_tl(cpu_T[1], cpu_A0);
B
bellard 已提交
2614
    if (s->addseg)
2615
        gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2616 2617 2618 2619 2620 2621
}

/* NOTE: wrap around in 16 bit not fully handled */
static void gen_pusha(DisasContext *s)
{
    int i;
B
bellard 已提交
2622
    gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2623 2624 2625
    gen_op_addl_A0_im(-16 <<  s->dflag);
    if (!s->ss32)
        gen_op_andl_A0_ffff();
2626
    tcg_gen_mov_tl(cpu_T[1], cpu_A0);
B
bellard 已提交
2627
    if (s->addseg)
2628
        gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2629
    for(i = 0;i < 8; i++) {
B
bellard 已提交
2630 2631
        gen_op_mov_TN_reg(OT_LONG, 0, 7 - i);
        gen_op_st_T0_A0(OT_WORD + s->dflag + s->mem_index);
B
bellard 已提交
2632 2633
        gen_op_addl_A0_im(2 <<  s->dflag);
    }
B
bellard 已提交
2634
    gen_op_mov_reg_T1(OT_WORD + s->ss32, R_ESP);
B
bellard 已提交
2635 2636 2637 2638 2639 2640
}

/* NOTE: wrap around in 16 bit not fully handled */
static void gen_popa(DisasContext *s)
{
    int i;
B
bellard 已提交
2641
    gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2642 2643
    if (!s->ss32)
        gen_op_andl_A0_ffff();
2644 2645
    tcg_gen_mov_tl(cpu_T[1], cpu_A0);
    tcg_gen_addi_tl(cpu_T[1], cpu_T[1], 16 <<  s->dflag);
B
bellard 已提交
2646
    if (s->addseg)
2647
        gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2648 2649 2650
    for(i = 0;i < 8; i++) {
        /* ESP is not reloaded */
        if (i != 3) {
B
bellard 已提交
2651 2652
            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 已提交
2653 2654 2655
        }
        gen_op_addl_A0_im(2 <<  s->dflag);
    }
B
bellard 已提交
2656
    gen_op_mov_reg_T1(OT_WORD + s->ss32, R_ESP);
B
bellard 已提交
2657 2658 2659 2660
}

static void gen_enter(DisasContext *s, int esp_addend, int level)
{
B
bellard 已提交
2661
    int ot, opsize;
B
bellard 已提交
2662 2663

    level &= 0x1f;
2664 2665 2666 2667
#ifdef TARGET_X86_64
    if (CODE64(s)) {
        ot = s->dflag ? OT_QUAD : OT_WORD;
        opsize = 1 << ot;
2668

B
bellard 已提交
2669
        gen_op_movl_A0_reg(R_ESP);
2670
        gen_op_addq_A0_im(-opsize);
2671
        tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2672 2673

        /* push bp */
B
bellard 已提交
2674 2675
        gen_op_mov_TN_reg(OT_LONG, 0, R_EBP);
        gen_op_st_T0_A0(ot + s->mem_index);
2676
        if (level) {
B
bellard 已提交
2677
            /* XXX: must save state */
2678
            gen_helper_enter64_level(cpu_env, tcg_const_i32(level),
P
pbrook 已提交
2679 2680
                                     tcg_const_i32((ot == OT_QUAD)),
                                     cpu_T[1]);
2681
        }
B
bellard 已提交
2682
        gen_op_mov_reg_T1(ot, R_EBP);
2683
        tcg_gen_addi_tl(cpu_T[1], cpu_T[1], -esp_addend + (-opsize * level));
B
bellard 已提交
2684
        gen_op_mov_reg_T1(OT_QUAD, R_ESP);
2685
    } else
2686 2687 2688 2689
#endif
    {
        ot = s->dflag + OT_WORD;
        opsize = 2 << s->dflag;
2690

B
bellard 已提交
2691
        gen_op_movl_A0_reg(R_ESP);
2692 2693 2694
        gen_op_addl_A0_im(-opsize);
        if (!s->ss32)
            gen_op_andl_A0_ffff();
2695
        tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2696
        if (s->addseg)
2697
            gen_op_addl_A0_seg(s, R_SS);
2698
        /* push bp */
B
bellard 已提交
2699 2700
        gen_op_mov_TN_reg(OT_LONG, 0, R_EBP);
        gen_op_st_T0_A0(ot + s->mem_index);
2701
        if (level) {
B
bellard 已提交
2702
            /* XXX: must save state */
2703
            gen_helper_enter_level(cpu_env, tcg_const_i32(level),
P
pbrook 已提交
2704 2705
                                   tcg_const_i32(s->dflag),
                                   cpu_T[1]);
2706
        }
B
bellard 已提交
2707
        gen_op_mov_reg_T1(ot, R_EBP);
2708
        tcg_gen_addi_tl(cpu_T[1], cpu_T[1], -esp_addend + (-opsize * level));
B
bellard 已提交
2709
        gen_op_mov_reg_T1(OT_WORD + s->ss32, R_ESP);
B
bellard 已提交
2710 2711 2712
    }
}

B
bellard 已提交
2713
static void gen_exception(DisasContext *s, int trapno, target_ulong cur_eip)
B
bellard 已提交
2714
{
2715
    gen_update_cc_op(s);
B
bellard 已提交
2716
    gen_jmp_im(cur_eip);
B
Blue Swirl 已提交
2717
    gen_helper_raise_exception(cpu_env, tcg_const_i32(trapno));
J
Jun Koi 已提交
2718
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2719 2720 2721
}

/* an interrupt is different from an exception because of the
B
blueswir1 已提交
2722
   privilege checks */
2723
static void gen_interrupt(DisasContext *s, int intno,
B
bellard 已提交
2724
                          target_ulong cur_eip, target_ulong next_eip)
B
bellard 已提交
2725
{
2726
    gen_update_cc_op(s);
B
bellard 已提交
2727
    gen_jmp_im(cur_eip);
B
Blue Swirl 已提交
2728
    gen_helper_raise_interrupt(cpu_env, tcg_const_i32(intno),
P
pbrook 已提交
2729
                               tcg_const_i32(next_eip - cur_eip));
J
Jun Koi 已提交
2730
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2731 2732
}

B
bellard 已提交
2733
static void gen_debug(DisasContext *s, target_ulong cur_eip)
B
bellard 已提交
2734
{
2735
    gen_update_cc_op(s);
B
bellard 已提交
2736
    gen_jmp_im(cur_eip);
B
Blue Swirl 已提交
2737
    gen_helper_debug(cpu_env);
J
Jun Koi 已提交
2738
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2739 2740 2741 2742 2743 2744
}

/* generate a generic end of block. Trace exception is also generated
   if needed */
static void gen_eob(DisasContext *s)
{
2745
    gen_update_cc_op(s);
2746
    if (s->tb->flags & HF_INHIBIT_IRQ_MASK) {
2747
        gen_helper_reset_inhibit_irq(cpu_env);
2748
    }
J
Jan Kiszka 已提交
2749
    if (s->tb->flags & HF_RF_MASK) {
2750
        gen_helper_reset_rf(cpu_env);
J
Jan Kiszka 已提交
2751
    }
2752
    if (s->singlestep_enabled) {
B
Blue Swirl 已提交
2753
        gen_helper_debug(cpu_env);
2754
    } else if (s->tf) {
B
Blue Swirl 已提交
2755
        gen_helper_single_step(cpu_env);
B
bellard 已提交
2756
    } else {
B
bellard 已提交
2757
        tcg_gen_exit_tb(0);
B
bellard 已提交
2758
    }
J
Jun Koi 已提交
2759
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2760 2761 2762 2763
}

/* generate a jump to eip. No segment change must happen before as a
   direct call to the next block may occur */
B
bellard 已提交
2764
static void gen_jmp_tb(DisasContext *s, target_ulong eip, int tb_num)
B
bellard 已提交
2765 2766
{
    if (s->jmp_opt) {
J
Jun Koi 已提交
2767
        gen_update_cc_op(s);
2768
        gen_goto_tb(s, tb_num, eip);
J
Jun Koi 已提交
2769
        s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2770
    } else {
B
bellard 已提交
2771
        gen_jmp_im(eip);
B
bellard 已提交
2772 2773 2774 2775
        gen_eob(s);
    }
}

B
bellard 已提交
2776 2777 2778 2779 2780
static void gen_jmp(DisasContext *s, target_ulong eip)
{
    gen_jmp_tb(s, eip, 0);
}

B
bellard 已提交
2781 2782 2783
static inline void gen_ldq_env_A0(int idx, int offset)
{
    int mem_index = (idx >> 2) - 1;
2784 2785
    tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_A0, mem_index);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset);
B
bellard 已提交
2786
}
B
bellard 已提交
2787

B
bellard 已提交
2788 2789 2790
static inline void gen_stq_env_A0(int idx, int offset)
{
    int mem_index = (idx >> 2) - 1;
2791 2792
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset);
    tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_A0, mem_index);
B
bellard 已提交
2793
}
B
bellard 已提交
2794

B
bellard 已提交
2795 2796 2797
static inline void gen_ldo_env_A0(int idx, int offset)
{
    int mem_index = (idx >> 2) - 1;
2798 2799
    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 已提交
2800
    tcg_gen_addi_tl(cpu_tmp0, cpu_A0, 8);
2801 2802
    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 已提交
2803
}
B
bellard 已提交
2804

B
bellard 已提交
2805 2806 2807
static inline void gen_sto_env_A0(int idx, int offset)
{
    int mem_index = (idx >> 2) - 1;
2808 2809
    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 已提交
2810
    tcg_gen_addi_tl(cpu_tmp0, cpu_A0, 8);
2811 2812
    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 已提交
2813
}
B
bellard 已提交
2814

B
bellard 已提交
2815 2816
static inline void gen_op_movo(int d_offset, int s_offset)
{
2817 2818 2819 2820
    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 已提交
2821 2822 2823 2824
}

static inline void gen_op_movq(int d_offset, int s_offset)
{
2825 2826
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
B
bellard 已提交
2827 2828 2829 2830
}

static inline void gen_op_movl(int d_offset, int s_offset)
{
2831 2832
    tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env, s_offset);
    tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, d_offset);
B
bellard 已提交
2833 2834 2835 2836
}

static inline void gen_op_movq_env_0(int d_offset)
{
2837 2838
    tcg_gen_movi_i64(cpu_tmp1_i64, 0);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
B
bellard 已提交
2839
}
B
bellard 已提交
2840

B
Blue Swirl 已提交
2841 2842 2843 2844 2845 2846 2847
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 已提交
2848
typedef void (*SSEFunc_0_ppi)(TCGv_ptr reg_a, TCGv_ptr reg_b, TCGv_i32 val);
B
Blue Swirl 已提交
2849 2850
typedef void (*SSEFunc_0_eppt)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
                               TCGv val);
B
Blue Swirl 已提交
2851

B
bellard 已提交
2852 2853
#define SSE_SPECIAL ((void *)1)
#define SSE_DUMMY ((void *)2)
B
bellard 已提交
2854

P
pbrook 已提交
2855 2856 2857
#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 已提交
2858

B
Blue Swirl 已提交
2859
static const SSEFunc_0_epp sse_op_table1[256][4] = {
A
aurel32 已提交
2860 2861 2862
    /* 3DNow! extensions */
    [0x0e] = { SSE_DUMMY }, /* femms */
    [0x0f] = { SSE_DUMMY }, /* pf... */
B
bellard 已提交
2863 2864 2865
    /* 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 已提交
2866
    [0x12] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movlps, movlpd, movsldup, movddup */
B
bellard 已提交
2867
    [0x13] = { SSE_SPECIAL, SSE_SPECIAL },  /* movlps, movlpd */
P
pbrook 已提交
2868 2869
    [0x14] = { gen_helper_punpckldq_xmm, gen_helper_punpcklqdq_xmm },
    [0x15] = { gen_helper_punpckhdq_xmm, gen_helper_punpckhqdq_xmm },
B
bellard 已提交
2870 2871 2872 2873 2874 2875
    [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 */
2876
    [0x2b] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movntps, movntpd, movntss, movntsd */
B
bellard 已提交
2877 2878
    [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 已提交
2879 2880
    [0x2e] = { gen_helper_ucomiss, gen_helper_ucomisd },
    [0x2f] = { gen_helper_comiss, gen_helper_comisd },
B
bellard 已提交
2881 2882
    [0x50] = { SSE_SPECIAL, SSE_SPECIAL }, /* movmskps, movmskpd */
    [0x51] = SSE_FOP(sqrt),
P
pbrook 已提交
2883 2884 2885 2886 2887 2888
    [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 已提交
2889 2890
    [0x58] = SSE_FOP(add),
    [0x59] = SSE_FOP(mul),
P
pbrook 已提交
2891 2892 2893
    [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 已提交
2894 2895 2896 2897 2898 2899
    [0x5c] = SSE_FOP(sub),
    [0x5d] = SSE_FOP(min),
    [0x5e] = SSE_FOP(div),
    [0x5f] = SSE_FOP(max),

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

B
balrog 已提交
2903 2904
    [0x38] = { SSE_SPECIAL, SSE_SPECIAL, NULL, SSE_SPECIAL }, /* SSSE3/SSE4 */
    [0x3a] = { SSE_SPECIAL, SSE_SPECIAL }, /* SSSE3/SSE4 */
B
balrog 已提交
2905

B
bellard 已提交
2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918
    /* 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 已提交
2919 2920
    [0x6c] = { NULL, gen_helper_punpcklqdq_xmm },
    [0x6d] = { NULL, gen_helper_punpckhqdq_xmm },
B
bellard 已提交
2921 2922
    [0x6e] = { SSE_SPECIAL, SSE_SPECIAL }, /* movd mm, ea */
    [0x6f] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movq, movdqa, , movqdu */
B
Blue Swirl 已提交
2923 2924 2925 2926
    [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 已提交
2927 2928 2929 2930 2931 2932
    [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 已提交
2933
    [0x77] = { SSE_DUMMY }, /* emms */
2934 2935
    [0x78] = { NULL, SSE_SPECIAL, NULL, SSE_SPECIAL }, /* extrq_i, insertq_i */
    [0x79] = { NULL, gen_helper_extrq_r, NULL, gen_helper_insertq_r },
P
pbrook 已提交
2936 2937
    [0x7c] = { NULL, gen_helper_haddpd, NULL, gen_helper_haddps },
    [0x7d] = { NULL, gen_helper_hsubpd, NULL, gen_helper_hsubps },
B
bellard 已提交
2938 2939 2940 2941
    [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 已提交
2942
    [0xd0] = { NULL, gen_helper_addsubpd, NULL, gen_helper_addsubps },
B
bellard 已提交
2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963
    [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 已提交
2964
    [0xe6] = { NULL, gen_helper_cvttpd2dq, gen_helper_cvtdq2pd, gen_helper_cvtpd2dq },
B
bellard 已提交
2965 2966 2967 2968 2969 2970 2971 2972 2973
    [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 已提交
2974
    [0xf0] = { NULL, NULL, NULL, SSE_SPECIAL }, /* lddqu */
B
bellard 已提交
2975 2976 2977 2978 2979 2980
    [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 已提交
2981 2982
    [0xf7] = { (SSEFunc_0_epp)gen_helper_maskmov_mmx,
               (SSEFunc_0_epp)gen_helper_maskmov_xmm }, /* XXX: casts */
B
bellard 已提交
2983 2984 2985 2986 2987 2988 2989 2990 2991
    [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 已提交
2992
static const SSEFunc_0_epp sse_op_table2[3 * 8][2] = {
B
bellard 已提交
2993 2994 2995 2996 2997 2998 2999
    [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 已提交
3000
    [16 + 3] = { NULL, gen_helper_psrldq_xmm },
B
bellard 已提交
3001
    [16 + 6] = MMX_OP2(psllq),
P
pbrook 已提交
3002
    [16 + 7] = { NULL, gen_helper_pslldq_xmm },
B
bellard 已提交
3003 3004
};

B
Blue Swirl 已提交
3005
static const SSEFunc_0_epi sse_op_table3ai[] = {
P
pbrook 已提交
3006
    gen_helper_cvtsi2ss,
3007
    gen_helper_cvtsi2sd
B
Blue Swirl 已提交
3008
};
P
pbrook 已提交
3009

3010
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
3011
static const SSEFunc_0_epl sse_op_table3aq[] = {
3012 3013 3014 3015 3016
    gen_helper_cvtsq2ss,
    gen_helper_cvtsq2sd
};
#endif

B
Blue Swirl 已提交
3017
static const SSEFunc_i_ep sse_op_table3bi[] = {
P
pbrook 已提交
3018 3019
    gen_helper_cvttss2si,
    gen_helper_cvtss2si,
3020
    gen_helper_cvttsd2si,
3021
    gen_helper_cvtsd2si
B
bellard 已提交
3022
};
3023

3024
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
3025
static const SSEFunc_l_ep sse_op_table3bq[] = {
3026 3027
    gen_helper_cvttss2sq,
    gen_helper_cvtss2sq,
3028
    gen_helper_cvttsd2sq,
3029 3030 3031 3032
    gen_helper_cvtsd2sq
};
#endif

B
Blue Swirl 已提交
3033
static const SSEFunc_0_epp sse_op_table4[8][4] = {
B
bellard 已提交
3034 3035 3036 3037 3038 3039 3040 3041 3042
    SSE_FOP(cmpeq),
    SSE_FOP(cmplt),
    SSE_FOP(cmple),
    SSE_FOP(cmpunord),
    SSE_FOP(cmpneq),
    SSE_FOP(cmpnlt),
    SSE_FOP(cmpnle),
    SSE_FOP(cmpord),
};
3043

B
Blue Swirl 已提交
3044
static const SSEFunc_0_epp sse_op_table5[256] = {
P
pbrook 已提交
3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068
    [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 已提交
3069 3070
};

B
Blue Swirl 已提交
3071 3072
struct SSEOpHelper_epp {
    SSEFunc_0_epp op[2];
B
Blue Swirl 已提交
3073 3074 3075
    uint32_t ext_mask;
};

B
Blue Swirl 已提交
3076 3077
struct SSEOpHelper_eppi {
    SSEFunc_0_eppi op[2];
B
Blue Swirl 已提交
3078
    uint32_t ext_mask;
B
balrog 已提交
3079
};
B
Blue Swirl 已提交
3080

B
balrog 已提交
3081
#define SSSE3_OP(x) { MMX_OP2(x), CPUID_EXT_SSSE3 }
P
pbrook 已提交
3082 3083
#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 已提交
3084
#define SSE41_SPECIAL { { NULL, SSE_SPECIAL }, CPUID_EXT_SSE41 }
B
Blue Swirl 已提交
3085

B
Blue Swirl 已提交
3086
static const struct SSEOpHelper_epp sse_op_table6[256] = {
B
balrog 已提交
3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132
    [0x00] = SSSE3_OP(pshufb),
    [0x01] = SSSE3_OP(phaddw),
    [0x02] = SSSE3_OP(phaddd),
    [0x03] = SSSE3_OP(phaddsw),
    [0x04] = SSSE3_OP(pmaddubsw),
    [0x05] = SSSE3_OP(phsubw),
    [0x06] = SSSE3_OP(phsubd),
    [0x07] = SSSE3_OP(phsubsw),
    [0x08] = SSSE3_OP(psignb),
    [0x09] = SSSE3_OP(psignw),
    [0x0a] = SSSE3_OP(psignd),
    [0x0b] = SSSE3_OP(pmulhrsw),
    [0x10] = SSE41_OP(pblendvb),
    [0x14] = SSE41_OP(blendvps),
    [0x15] = SSE41_OP(blendvpd),
    [0x17] = SSE41_OP(ptest),
    [0x1c] = SSSE3_OP(pabsb),
    [0x1d] = SSSE3_OP(pabsw),
    [0x1e] = SSSE3_OP(pabsd),
    [0x20] = SSE41_OP(pmovsxbw),
    [0x21] = SSE41_OP(pmovsxbd),
    [0x22] = SSE41_OP(pmovsxbq),
    [0x23] = SSE41_OP(pmovsxwd),
    [0x24] = SSE41_OP(pmovsxwq),
    [0x25] = SSE41_OP(pmovsxdq),
    [0x28] = SSE41_OP(pmuldq),
    [0x29] = SSE41_OP(pcmpeqq),
    [0x2a] = SSE41_SPECIAL, /* movntqda */
    [0x2b] = SSE41_OP(packusdw),
    [0x30] = SSE41_OP(pmovzxbw),
    [0x31] = SSE41_OP(pmovzxbd),
    [0x32] = SSE41_OP(pmovzxbq),
    [0x33] = SSE41_OP(pmovzxwd),
    [0x34] = SSE41_OP(pmovzxwq),
    [0x35] = SSE41_OP(pmovzxdq),
    [0x37] = SSE42_OP(pcmpgtq),
    [0x38] = SSE41_OP(pminsb),
    [0x39] = SSE41_OP(pminsd),
    [0x3a] = SSE41_OP(pminuw),
    [0x3b] = SSE41_OP(pminud),
    [0x3c] = SSE41_OP(pmaxsb),
    [0x3d] = SSE41_OP(pmaxsd),
    [0x3e] = SSE41_OP(pmaxuw),
    [0x3f] = SSE41_OP(pmaxud),
    [0x40] = SSE41_OP(pmulld),
    [0x41] = SSE41_OP(phminposuw),
B
balrog 已提交
3133 3134
};

B
Blue Swirl 已提交
3135
static const struct SSEOpHelper_eppi sse_op_table7[256] = {
B
balrog 已提交
3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157
    [0x08] = SSE41_OP(roundps),
    [0x09] = SSE41_OP(roundpd),
    [0x0a] = SSE41_OP(roundss),
    [0x0b] = SSE41_OP(roundsd),
    [0x0c] = SSE41_OP(blendps),
    [0x0d] = SSE41_OP(blendpd),
    [0x0e] = SSE41_OP(pblendw),
    [0x0f] = SSSE3_OP(palignr),
    [0x14] = SSE41_SPECIAL, /* pextrb */
    [0x15] = SSE41_SPECIAL, /* pextrw */
    [0x16] = SSE41_SPECIAL, /* pextrd/pextrq */
    [0x17] = SSE41_SPECIAL, /* extractps */
    [0x20] = SSE41_SPECIAL, /* pinsrb */
    [0x21] = SSE41_SPECIAL, /* insertps */
    [0x22] = SSE41_SPECIAL, /* pinsrd/pinsrq */
    [0x40] = SSE41_OP(dpps),
    [0x41] = SSE41_OP(dppd),
    [0x42] = SSE41_OP(mpsadbw),
    [0x60] = SSE42_OP(pcmpestrm),
    [0x61] = SSE42_OP(pcmpestri),
    [0x62] = SSE42_OP(pcmpistrm),
    [0x63] = SSE42_OP(pcmpistri),
B
balrog 已提交
3158 3159
};

3160 3161
static void gen_sse(CPUX86State *env, DisasContext *s, int b,
                    target_ulong pc_start, int rex_r)
B
bellard 已提交
3162 3163 3164
{
    int b1, op1_offset, op2_offset, is_xmm, val, ot;
    int modrm, mod, rm, reg, reg_addr, offset_addr;
B
Blue Swirl 已提交
3165 3166
    SSEFunc_0_epp sse_fn_epp;
    SSEFunc_0_eppi sse_fn_eppi;
B
Blue Swirl 已提交
3167
    SSEFunc_0_ppi sse_fn_ppi;
B
Blue Swirl 已提交
3168
    SSEFunc_0_eppt sse_fn_eppt;
B
bellard 已提交
3169 3170

    b &= 0xff;
3171
    if (s->prefix & PREFIX_DATA)
B
bellard 已提交
3172
        b1 = 1;
3173
    else if (s->prefix & PREFIX_REPZ)
B
bellard 已提交
3174
        b1 = 2;
3175
    else if (s->prefix & PREFIX_REPNZ)
B
bellard 已提交
3176 3177 3178
        b1 = 3;
    else
        b1 = 0;
B
Blue Swirl 已提交
3179 3180
    sse_fn_epp = sse_op_table1[b][b1];
    if (!sse_fn_epp) {
B
bellard 已提交
3181
        goto illegal_op;
B
Blue Swirl 已提交
3182
    }
A
aurel32 已提交
3183
    if ((b <= 0x5f && b >= 0x10) || b == 0xc6 || b == 0xc2) {
B
bellard 已提交
3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203
        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 已提交
3204 3205
        if ((b != 0x38 && b != 0x3a) || (s->prefix & PREFIX_DATA))
            goto illegal_op;
3206 3207 3208 3209
    if (b == 0x0e) {
        if (!(s->cpuid_ext2_features & CPUID_EXT2_3DNOW))
            goto illegal_op;
        /* femms */
B
Blue Swirl 已提交
3210
        gen_helper_emms(cpu_env);
3211 3212 3213 3214
        return;
    }
    if (b == 0x77) {
        /* emms */
B
Blue Swirl 已提交
3215
        gen_helper_emms(cpu_env);
B
bellard 已提交
3216 3217 3218 3219 3220
        return;
    }
    /* prepare MMX state (XXX: optimize by storing fptt and fptags in
       the static cpu state) */
    if (!is_xmm) {
B
Blue Swirl 已提交
3221
        gen_helper_enter_mmx(cpu_env);
B
bellard 已提交
3222 3223
    }

3224
    modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
3225 3226 3227 3228
    reg = ((modrm >> 3) & 7);
    if (is_xmm)
        reg |= rex_r;
    mod = (modrm >> 6) & 3;
B
Blue Swirl 已提交
3229
    if (sse_fn_epp == SSE_SPECIAL) {
B
bellard 已提交
3230 3231 3232
        b |= (b1 << 8);
        switch(b) {
        case 0x0e7: /* movntq */
3233
            if (mod == 3)
B
bellard 已提交
3234
                goto illegal_op;
3235
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3236
            gen_stq_env_A0(s->mem_index, offsetof(CPUX86State,fpregs[reg].mmx));
B
bellard 已提交
3237 3238 3239 3240
            break;
        case 0x1e7: /* movntdq */
        case 0x02b: /* movntps */
        case 0x12b: /* movntps */
3241 3242
            if (mod == 3)
                goto illegal_op;
3243
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
3244 3245
            gen_sto_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg]));
            break;
B
bellard 已提交
3246 3247
        case 0x3f0: /* lddqu */
            if (mod == 3)
B
bellard 已提交
3248
                goto illegal_op;
3249
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
3250
            gen_ldo_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg]));
B
bellard 已提交
3251
            break;
3252 3253 3254 3255
        case 0x22b: /* movntss */
        case 0x32b: /* movntsd */
            if (mod == 3)
                goto illegal_op;
3256
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
3257 3258 3259 3260 3261 3262 3263 3264 3265
            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 已提交
3266
        case 0x6e: /* movd mm, ea */
B
bellard 已提交
3267 3268
#ifdef TARGET_X86_64
            if (s->dflag == 2) {
3269
                gen_ldst_modrm(env, s, modrm, OT_QUAD, OR_TMP0, 0);
B
bellard 已提交
3270
                tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,fpregs[reg].mmx));
3271
            } else
B
bellard 已提交
3272 3273
#endif
            {
3274
                gen_ldst_modrm(env, s, modrm, OT_LONG, OR_TMP0, 0);
B
bellard 已提交
3275 3276
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                                 offsetof(CPUX86State,fpregs[reg].mmx));
P
pbrook 已提交
3277 3278
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
                gen_helper_movl_mm_T0_mmx(cpu_ptr0, cpu_tmp2_i32);
B
bellard 已提交
3279
            }
B
bellard 已提交
3280 3281
            break;
        case 0x16e: /* movd xmm, ea */
B
bellard 已提交
3282 3283
#ifdef TARGET_X86_64
            if (s->dflag == 2) {
3284
                gen_ldst_modrm(env, s, modrm, OT_QUAD, OR_TMP0, 0);
B
bellard 已提交
3285 3286
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                                 offsetof(CPUX86State,xmm_regs[reg]));
P
pbrook 已提交
3287
                gen_helper_movq_mm_T0_xmm(cpu_ptr0, cpu_T[0]);
3288
            } else
B
bellard 已提交
3289 3290
#endif
            {
3291
                gen_ldst_modrm(env, s, modrm, OT_LONG, OR_TMP0, 0);
B
bellard 已提交
3292 3293
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                                 offsetof(CPUX86State,xmm_regs[reg]));
3294
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
3295
                gen_helper_movl_mm_T0_xmm(cpu_ptr0, cpu_tmp2_i32);
B
bellard 已提交
3296
            }
B
bellard 已提交
3297 3298 3299
            break;
        case 0x6f: /* movq mm, ea */
            if (mod != 3) {
3300
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3301
                gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,fpregs[reg].mmx));
B
bellard 已提交
3302 3303
            } else {
                rm = (modrm & 7);
3304
                tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env,
B
bellard 已提交
3305
                               offsetof(CPUX86State,fpregs[rm].mmx));
3306
                tcg_gen_st_i64(cpu_tmp1_i64, cpu_env,
B
bellard 已提交
3307
                               offsetof(CPUX86State,fpregs[reg].mmx));
B
bellard 已提交
3308 3309 3310 3311 3312 3313 3314 3315 3316
            }
            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) {
3317
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3318
                gen_ldo_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg]));
B
bellard 已提交
3319 3320 3321 3322 3323 3324 3325 3326
            } 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) {
3327
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3328
                gen_op_ld_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
3329
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
B
bellard 已提交
3330
                gen_op_movl_T0_0();
B
bellard 已提交
3331 3332 3333
                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 已提交
3334 3335 3336 3337 3338 3339 3340 3341
            } 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) {
3342
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3343
                gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3344
                gen_op_movl_T0_0();
B
bellard 已提交
3345 3346
                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 已提交
3347 3348 3349 3350 3351 3352 3353 3354 3355
            } 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) {
3356
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3357
                gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3358 3359 3360 3361 3362 3363 3364
            } 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 已提交
3365 3366
        case 0x212: /* movsldup */
            if (mod != 3) {
3367
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3368
                gen_ldo_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg]));
B
bellard 已提交
3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382
            } 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) {
3383
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3384
                gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3385 3386 3387 3388 3389 3390
            } 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 已提交
3391
                        offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3392
            break;
B
bellard 已提交
3393 3394 3395
        case 0x016: /* movhps */
        case 0x116: /* movhpd */
            if (mod != 3) {
3396
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3397
                gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(1)));
B
bellard 已提交
3398 3399 3400 3401 3402 3403 3404 3405 3406
            } 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) {
3407
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3408
                gen_ldo_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg]));
B
bellard 已提交
3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420
            } 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;
3421 3422 3423 3424 3425 3426 3427
        case 0x178:
        case 0x378:
            {
                int bit_index, field_length;

                if (b1 == 1 && reg != 0)
                    goto illegal_op;
3428 3429
                field_length = cpu_ldub_code(env, s->pc++) & 0x3F;
                bit_index = cpu_ldub_code(env, s->pc++) & 0x3F;
3430 3431 3432
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
                    offsetof(CPUX86State,xmm_regs[reg]));
                if (b1 == 1)
B
Blue Swirl 已提交
3433 3434 3435
                    gen_helper_extrq_i(cpu_env, cpu_ptr0,
                                       tcg_const_i32(bit_index),
                                       tcg_const_i32(field_length));
3436
                else
B
Blue Swirl 已提交
3437 3438 3439
                    gen_helper_insertq_i(cpu_env, cpu_ptr0,
                                         tcg_const_i32(bit_index),
                                         tcg_const_i32(field_length));
3440 3441
            }
            break;
B
bellard 已提交
3442
        case 0x7e: /* movd ea, mm */
B
bellard 已提交
3443 3444
#ifdef TARGET_X86_64
            if (s->dflag == 2) {
B
bellard 已提交
3445 3446
                tcg_gen_ld_i64(cpu_T[0], cpu_env, 
                               offsetof(CPUX86State,fpregs[reg].mmx));
3447
                gen_ldst_modrm(env, s, modrm, OT_QUAD, OR_TMP0, 1);
3448
            } else
B
bellard 已提交
3449 3450
#endif
            {
B
bellard 已提交
3451 3452
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, 
                                 offsetof(CPUX86State,fpregs[reg].mmx.MMX_L(0)));
3453
                gen_ldst_modrm(env, s, modrm, OT_LONG, OR_TMP0, 1);
B
bellard 已提交
3454
            }
B
bellard 已提交
3455 3456
            break;
        case 0x17e: /* movd ea, xmm */
B
bellard 已提交
3457 3458
#ifdef TARGET_X86_64
            if (s->dflag == 2) {
B
bellard 已提交
3459 3460
                tcg_gen_ld_i64(cpu_T[0], cpu_env, 
                               offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
3461
                gen_ldst_modrm(env, s, modrm, OT_QUAD, OR_TMP0, 1);
3462
            } else
B
bellard 已提交
3463 3464
#endif
            {
B
bellard 已提交
3465 3466
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, 
                                 offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
3467
                gen_ldst_modrm(env, s, modrm, OT_LONG, OR_TMP0, 1);
B
bellard 已提交
3468
            }
B
bellard 已提交
3469 3470 3471
            break;
        case 0x27e: /* movq xmm, ea */
            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(0)));
B
bellard 已提交
3474 3475 3476 3477 3478 3479 3480 3481 3482
            } 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) {
3483
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3484
                gen_stq_env_A0(s->mem_index, offsetof(CPUX86State,fpregs[reg].mmx));
B
bellard 已提交
3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497
            } 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) {
3498
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3499
                gen_sto_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg]));
B
bellard 已提交
3500 3501 3502 3503 3504 3505 3506 3507
            } 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) {
3508
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3509
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
B
bellard 已提交
3510
                gen_op_st_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
3511 3512 3513 3514 3515 3516 3517 3518
            } 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) {
3519
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3520
                gen_stq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3521 3522 3523 3524 3525 3526 3527 3528 3529
            } 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) {
3530
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3531
                gen_stq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3532 3533 3534 3535 3536 3537 3538
            } else {
                goto illegal_op;
            }
            break;
        case 0x017: /* movhps */
        case 0x117: /* movhpd */
            if (mod != 3) {
3539
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3540
                gen_stq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(1)));
B
bellard 已提交
3541 3542 3543 3544 3545 3546 3547 3548 3549 3550
            } else {
                goto illegal_op;
            }
            break;
        case 0x71: /* shift mm, im */
        case 0x72:
        case 0x73:
        case 0x171: /* shift xmm, im */
        case 0x172:
        case 0x173:
3551 3552 3553
            if (b1 >= 2) {
	        goto illegal_op;
            }
3554
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
3555 3556
            if (is_xmm) {
                gen_op_movl_T0_im(val);
B
bellard 已提交
3557
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(0)));
B
bellard 已提交
3558
                gen_op_movl_T0_0();
B
bellard 已提交
3559
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(1)));
B
bellard 已提交
3560 3561 3562
                op1_offset = offsetof(CPUX86State,xmm_t0);
            } else {
                gen_op_movl_T0_im(val);
B
bellard 已提交
3563
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,mmx_t0.MMX_L(0)));
B
bellard 已提交
3564
                gen_op_movl_T0_0();
B
bellard 已提交
3565
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,mmx_t0.MMX_L(1)));
B
bellard 已提交
3566 3567
                op1_offset = offsetof(CPUX86State,mmx_t0);
            }
B
Blue Swirl 已提交
3568 3569 3570
            sse_fn_epp = sse_op_table2[((b - 1) & 3) * 8 +
                                       (((modrm >> 3)) & 7)][b1];
            if (!sse_fn_epp) {
B
bellard 已提交
3571
                goto illegal_op;
B
Blue Swirl 已提交
3572
            }
B
bellard 已提交
3573 3574 3575 3576 3577 3578 3579
            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 已提交
3580 3581
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op2_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op1_offset);
B
Blue Swirl 已提交
3582
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3583 3584 3585
            break;
        case 0x050: /* movmskps */
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
3586 3587
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                             offsetof(CPUX86State,xmm_regs[rm]));
B
Blue Swirl 已提交
3588
            gen_helper_movmskps(cpu_tmp2_i32, cpu_env, cpu_ptr0);
3589
            tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
3590
            gen_op_mov_reg_T0(OT_LONG, reg);
B
bellard 已提交
3591 3592 3593
            break;
        case 0x150: /* movmskpd */
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
3594 3595
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                             offsetof(CPUX86State,xmm_regs[rm]));
B
Blue Swirl 已提交
3596
            gen_helper_movmskpd(cpu_tmp2_i32, cpu_env, cpu_ptr0);
3597
            tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
3598
            gen_op_mov_reg_T0(OT_LONG, reg);
B
bellard 已提交
3599 3600 3601
            break;
        case 0x02a: /* cvtpi2ps */
        case 0x12a: /* cvtpi2pd */
B
Blue Swirl 已提交
3602
            gen_helper_enter_mmx(cpu_env);
B
bellard 已提交
3603
            if (mod != 3) {
3604
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3605
                op2_offset = offsetof(CPUX86State,mmx_t0);
B
bellard 已提交
3606
                gen_ldq_env_A0(s->mem_index, op2_offset);
B
bellard 已提交
3607 3608 3609 3610 3611
            } else {
                rm = (modrm & 7);
                op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
            }
            op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
B
bellard 已提交
3612 3613
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
bellard 已提交
3614 3615
            switch(b >> 8) {
            case 0x0:
B
Blue Swirl 已提交
3616
                gen_helper_cvtpi2ps(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3617 3618 3619
                break;
            default:
            case 0x1:
B
Blue Swirl 已提交
3620
                gen_helper_cvtpi2pd(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3621 3622 3623 3624 3625 3626
                break;
            }
            break;
        case 0x22a: /* cvtsi2ss */
        case 0x32a: /* cvtsi2sd */
            ot = (s->dflag == 2) ? OT_QUAD : OT_LONG;
3627
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
bellard 已提交
3628
            op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
B
bellard 已提交
3629
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
B
bellard 已提交
3630
            if (ot == OT_LONG) {
B
Blue Swirl 已提交
3631
                SSEFunc_0_epi sse_fn_epi = sse_op_table3ai[(b >> 8) & 1];
B
bellard 已提交
3632
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
3633
                sse_fn_epi(cpu_env, cpu_ptr0, cpu_tmp2_i32);
B
bellard 已提交
3634
            } else {
3635
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
3636 3637
                SSEFunc_0_epl sse_fn_epl = sse_op_table3aq[(b >> 8) & 1];
                sse_fn_epl(cpu_env, cpu_ptr0, cpu_T[0]);
3638 3639 3640
#else
                goto illegal_op;
#endif
B
bellard 已提交
3641
            }
B
bellard 已提交
3642 3643 3644 3645 3646
            break;
        case 0x02c: /* cvttps2pi */
        case 0x12c: /* cvttpd2pi */
        case 0x02d: /* cvtps2pi */
        case 0x12d: /* cvtpd2pi */
B
Blue Swirl 已提交
3647
            gen_helper_enter_mmx(cpu_env);
B
bellard 已提交
3648
            if (mod != 3) {
3649
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3650
                op2_offset = offsetof(CPUX86State,xmm_t0);
B
bellard 已提交
3651
                gen_ldo_env_A0(s->mem_index, op2_offset);
B
bellard 已提交
3652 3653 3654 3655 3656
            } else {
                rm = (modrm & 7) | REX_B(s);
                op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
            }
            op1_offset = offsetof(CPUX86State,fpregs[reg & 7].mmx);
B
bellard 已提交
3657 3658
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
bellard 已提交
3659 3660
            switch(b) {
            case 0x02c:
B
Blue Swirl 已提交
3661
                gen_helper_cvttps2pi(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3662 3663
                break;
            case 0x12c:
B
Blue Swirl 已提交
3664
                gen_helper_cvttpd2pi(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3665 3666
                break;
            case 0x02d:
B
Blue Swirl 已提交
3667
                gen_helper_cvtps2pi(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3668 3669
                break;
            case 0x12d:
B
Blue Swirl 已提交
3670
                gen_helper_cvtpd2pi(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3671 3672 3673 3674 3675 3676 3677 3678
                break;
            }
            break;
        case 0x22c: /* cvttss2si */
        case 0x32c: /* cvttsd2si */
        case 0x22d: /* cvtss2si */
        case 0x32d: /* cvtsd2si */
            ot = (s->dflag == 2) ? OT_QUAD : OT_LONG;
B
bellard 已提交
3679
            if (mod != 3) {
3680
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3681
                if ((b >> 8) & 1) {
B
bellard 已提交
3682
                    gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_t0.XMM_Q(0)));
B
bellard 已提交
3683
                } else {
B
bellard 已提交
3684
                    gen_op_ld_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
3685
                    tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(0)));
B
bellard 已提交
3686 3687 3688 3689 3690 3691
                }
                op2_offset = offsetof(CPUX86State,xmm_t0);
            } else {
                rm = (modrm & 7) | REX_B(s);
                op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
            }
B
bellard 已提交
3692 3693
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op2_offset);
            if (ot == OT_LONG) {
B
Blue Swirl 已提交
3694
                SSEFunc_i_ep sse_fn_i_ep =
3695
                    sse_op_table3bi[((b >> 7) & 2) | (b & 1)];
B
Blue Swirl 已提交
3696
                sse_fn_i_ep(cpu_tmp2_i32, cpu_env, cpu_ptr0);
3697
                tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
3698
            } else {
3699
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
3700
                SSEFunc_l_ep sse_fn_l_ep =
3701
                    sse_op_table3bq[((b >> 7) & 2) | (b & 1)];
B
Blue Swirl 已提交
3702
                sse_fn_l_ep(cpu_T[0], cpu_env, cpu_ptr0);
3703 3704 3705
#else
                goto illegal_op;
#endif
B
bellard 已提交
3706
            }
B
bellard 已提交
3707
            gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
3708 3709
            break;
        case 0xc4: /* pinsrw */
3710
        case 0x1c4:
B
bellard 已提交
3711
            s->rip_offset = 1;
3712 3713
            gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
3714 3715
            if (b1) {
                val &= 7;
B
bellard 已提交
3716 3717
                tcg_gen_st16_tl(cpu_T[0], cpu_env,
                                offsetof(CPUX86State,xmm_regs[reg].XMM_W(val)));
B
bellard 已提交
3718 3719
            } else {
                val &= 3;
B
bellard 已提交
3720 3721
                tcg_gen_st16_tl(cpu_T[0], cpu_env,
                                offsetof(CPUX86State,fpregs[reg].mmx.MMX_W(val)));
B
bellard 已提交
3722 3723 3724
            }
            break;
        case 0xc5: /* pextrw */
3725
        case 0x1c5:
B
bellard 已提交
3726 3727
            if (mod != 3)
                goto illegal_op;
3728
            ot = (s->dflag == 2) ? OT_QUAD : OT_LONG;
3729
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
3730 3731 3732
            if (b1) {
                val &= 7;
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
3733 3734
                tcg_gen_ld16u_tl(cpu_T[0], cpu_env,
                                 offsetof(CPUX86State,xmm_regs[rm].XMM_W(val)));
B
bellard 已提交
3735 3736 3737
            } else {
                val &= 3;
                rm = (modrm & 7);
B
bellard 已提交
3738 3739
                tcg_gen_ld16u_tl(cpu_T[0], cpu_env,
                                offsetof(CPUX86State,fpregs[rm].mmx.MMX_W(val)));
B
bellard 已提交
3740 3741
            }
            reg = ((modrm >> 3) & 7) | rex_r;
3742
            gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
3743 3744 3745
            break;
        case 0x1d6: /* movq ea, xmm */
            if (mod != 3) {
3746
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3747
                gen_stq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3748 3749 3750 3751 3752 3753 3754 3755
            } 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 已提交
3756
            gen_helper_enter_mmx(cpu_env);
3757 3758 3759 3760
            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 已提交
3761 3762
            break;
        case 0x3d6: /* movdq2q */
B
Blue Swirl 已提交
3763
            gen_helper_enter_mmx(cpu_env);
3764 3765 3766
            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 已提交
3767 3768 3769 3770 3771 3772 3773
            break;
        case 0xd7: /* pmovmskb */
        case 0x1d7:
            if (mod != 3)
                goto illegal_op;
            if (b1) {
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
3774
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, offsetof(CPUX86State,xmm_regs[rm]));
B
Blue Swirl 已提交
3775
                gen_helper_pmovmskb_xmm(cpu_tmp2_i32, cpu_env, cpu_ptr0);
B
bellard 已提交
3776 3777
            } else {
                rm = (modrm & 7);
B
bellard 已提交
3778
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, offsetof(CPUX86State,fpregs[rm].mmx));
B
Blue Swirl 已提交
3779
                gen_helper_pmovmskb_mmx(cpu_tmp2_i32, cpu_env, cpu_ptr0);
B
bellard 已提交
3780
            }
3781
            tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
3782
            reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
3783
            gen_op_mov_reg_T0(OT_LONG, reg);
B
bellard 已提交
3784
            break;
B
balrog 已提交
3785
        case 0x138:
3786 3787 3788
            if (s->prefix & PREFIX_REPNZ)
                goto crc32;
        case 0x038:
B
balrog 已提交
3789
            b = modrm;
3790
            modrm = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
3791 3792 3793
            rm = modrm & 7;
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
3794 3795 3796
            if (b1 >= 2) {
                goto illegal_op;
            }
B
balrog 已提交
3797

B
Blue Swirl 已提交
3798 3799
            sse_fn_epp = sse_op_table6[b].op[b1];
            if (!sse_fn_epp) {
B
balrog 已提交
3800
                goto illegal_op;
B
Blue Swirl 已提交
3801
            }
B
balrog 已提交
3802 3803
            if (!(s->cpuid_ext_features & sse_op_table6[b].ext_mask))
                goto illegal_op;
B
balrog 已提交
3804 3805 3806 3807 3808 3809 3810

            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);
3811
                    gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
balrog 已提交
3812 3813 3814 3815 3816 3817 3818 3819 3820
                    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 已提交
3821
                        tcg_gen_qemu_ld32u(cpu_tmp0, cpu_A0,
B
balrog 已提交
3822
                                          (s->mem_index >> 2) - 1);
P
pbrook 已提交
3823
                        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_tmp0);
B
balrog 已提交
3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838
                        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 已提交
3839 3840 3841 3842 3843 3844 3845
                }
            } 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);
3846
                    gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
balrog 已提交
3847 3848 3849
                    gen_ldq_env_A0(s->mem_index, op2_offset);
                }
            }
B
Blue Swirl 已提交
3850
            if (sse_fn_epp == SSE_SPECIAL) {
B
balrog 已提交
3851
                goto illegal_op;
B
Blue Swirl 已提交
3852
            }
B
balrog 已提交
3853

B
balrog 已提交
3854 3855
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
3856
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
balrog 已提交
3857

3858 3859 3860
            if (b == 0x17) {
                set_cc_op(s, CC_OP_EFLAGS);
            }
B
balrog 已提交
3861
            break;
B
balrog 已提交
3862 3863 3864
        case 0x338: /* crc32 */
        crc32:
            b = modrm;
3865
            modrm = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
3866 3867 3868 3869 3870
            reg = ((modrm >> 3) & 7) | rex_r;

            if (b != 0xf0 && b != 0xf1)
                goto illegal_op;
            if (!(s->cpuid_ext_features & CPUID_EXT_SSE42))
B
balrog 已提交
3871 3872
                goto illegal_op;

B
balrog 已提交
3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884
            if (b == 0xf0)
                ot = OT_BYTE;
            else if (b == 0xf1 && s->dflag != 2)
                if (s->prefix & PREFIX_DATA)
                    ot = OT_WORD;
                else
                    ot = OT_LONG;
            else
                ot = OT_QUAD;

            gen_op_mov_TN_reg(OT_LONG, 0, reg);
            tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
3885
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
P
pbrook 已提交
3886 3887
            gen_helper_crc32(cpu_T[0], cpu_tmp2_i32,
                             cpu_T[0], tcg_const_i32(8 << ot));
B
balrog 已提交
3888 3889 3890 3891 3892 3893

            ot = (s->dflag == 2) ? OT_QUAD : OT_LONG;
            gen_op_mov_reg_T0(ot, reg);
            break;
        case 0x03a:
        case 0x13a:
B
balrog 已提交
3894
            b = modrm;
3895
            modrm = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
3896 3897 3898
            rm = modrm & 7;
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
3899 3900 3901
            if (b1 >= 2) {
                goto illegal_op;
            }
B
balrog 已提交
3902

B
Blue Swirl 已提交
3903 3904
            sse_fn_eppi = sse_op_table7[b].op[b1];
            if (!sse_fn_eppi) {
B
balrog 已提交
3905
                goto illegal_op;
B
Blue Swirl 已提交
3906
            }
B
balrog 已提交
3907 3908 3909
            if (!(s->cpuid_ext_features & sse_op_table7[b].ext_mask))
                goto illegal_op;

B
Blue Swirl 已提交
3910
            if (sse_fn_eppi == SSE_SPECIAL) {
B
balrog 已提交
3911 3912 3913
                ot = (s->dflag == 2) ? OT_QUAD : OT_LONG;
                rm = (modrm & 7) | REX_B(s);
                if (mod != 3)
3914
                    gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
balrog 已提交
3915
                reg = ((modrm >> 3) & 7) | rex_r;
3916
                val = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940
                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 已提交
3941
                        tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
balrog 已提交
3942
                        if (mod == 3)
P
pbrook 已提交
3943
                            gen_op_mov_reg_v(ot, rm, cpu_T[0]);
B
balrog 已提交
3944
                        else
P
pbrook 已提交
3945
                            tcg_gen_qemu_st32(cpu_T[0], cpu_A0,
B
balrog 已提交
3946 3947
                                            (s->mem_index >> 2) - 1);
                    } else { /* pextrq */
P
pbrook 已提交
3948
#ifdef TARGET_X86_64
B
balrog 已提交
3949 3950 3951 3952 3953 3954 3955 3956
                        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 已提交
3957 3958 3959
#else
                        goto illegal_op;
#endif
B
balrog 已提交
3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974
                    }
                    break;
                case 0x17: /* extractps */
                    tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
                                            xmm_regs[reg].XMM_L(val & 3)));
                    if (mod == 3)
                        gen_op_mov_reg_T0(ot, rm);
                    else
                        tcg_gen_qemu_st32(cpu_T[0], cpu_A0,
                                        (s->mem_index >> 2) - 1);
                    break;
                case 0x20: /* pinsrb */
                    if (mod == 3)
                        gen_op_mov_TN_reg(OT_LONG, 0, rm);
                    else
P
pbrook 已提交
3975
                        tcg_gen_qemu_ld8u(cpu_tmp0, cpu_A0,
B
balrog 已提交
3976
                                        (s->mem_index >> 2) - 1);
P
pbrook 已提交
3977
                    tcg_gen_st8_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State,
B
balrog 已提交
3978 3979 3980
                                            xmm_regs[reg].XMM_B(val & 15)));
                    break;
                case 0x21: /* insertps */
P
pbrook 已提交
3981
                    if (mod == 3) {
B
balrog 已提交
3982 3983 3984
                        tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env,
                                        offsetof(CPUX86State,xmm_regs[rm]
                                                .XMM_L((val >> 6) & 3)));
P
pbrook 已提交
3985 3986
                    } else {
                        tcg_gen_qemu_ld32u(cpu_tmp0, cpu_A0,
B
balrog 已提交
3987
                                        (s->mem_index >> 2) - 1);
P
pbrook 已提交
3988 3989
                        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_tmp0);
                    }
B
balrog 已提交
3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012
                    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 已提交
4013
                            gen_op_mov_v_reg(ot, cpu_tmp0, rm);
B
balrog 已提交
4014
                        else
P
pbrook 已提交
4015
                            tcg_gen_qemu_ld32u(cpu_tmp0, cpu_A0,
B
balrog 已提交
4016
                                            (s->mem_index >> 2) - 1);
P
pbrook 已提交
4017
                        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_tmp0);
B
balrog 已提交
4018 4019 4020 4021
                        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env,
                                        offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_L(val & 3)));
                    } else { /* pinsrq */
P
pbrook 已提交
4022
#ifdef TARGET_X86_64
B
balrog 已提交
4023 4024 4025 4026 4027 4028 4029 4030
                        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 已提交
4031 4032 4033
#else
                        goto illegal_op;
#endif
B
balrog 已提交
4034 4035 4036 4037 4038
                    }
                    break;
                }
                return;
            }
B
balrog 已提交
4039 4040 4041 4042 4043 4044 4045

            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);
4046
                    gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
balrog 已提交
4047 4048 4049 4050 4051 4052 4053 4054
                    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);
4055
                    gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
balrog 已提交
4056 4057 4058
                    gen_ldq_env_A0(s->mem_index, op2_offset);
                }
            }
4059
            val = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
4060

B
balrog 已提交
4061
            if ((b & 0xfc) == 0x60) { /* pcmpXstrX */
4062
                set_cc_op(s, CC_OP_EFLAGS);
B
balrog 已提交
4063 4064 4065 4066 4067 4068

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

B
balrog 已提交
4069 4070
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4071
            sse_fn_eppi(cpu_env, cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
B
balrog 已提交
4072
            break;
B
bellard 已提交
4073 4074 4075 4076 4077
        default:
            goto illegal_op;
        }
    } else {
        /* generic MMX or SSE operation */
B
bellard 已提交
4078 4079 4080 4081 4082 4083 4084 4085
        switch(b) {
        case 0x70: /* pshufx insn */
        case 0xc6: /* pshufx insn */
        case 0xc2: /* compare insns */
            s->rip_offset = 1;
            break;
        default:
            break;
B
bellard 已提交
4086 4087 4088 4089
        }
        if (is_xmm) {
            op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
            if (mod != 3) {
4090
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
4091
                op2_offset = offsetof(CPUX86State,xmm_t0);
4092
                if (b1 >= 2 && ((b >= 0x50 && b <= 0x5f && b != 0x5b) ||
B
bellard 已提交
4093 4094 4095 4096
                                b == 0xc2)) {
                    /* specific case for SSE single instructions */
                    if (b1 == 2) {
                        /* 32 bit access */
B
bellard 已提交
4097
                        gen_op_ld_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
4098
                        tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(0)));
B
bellard 已提交
4099 4100
                    } else {
                        /* 64 bit access */
B
bellard 已提交
4101
                        gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_t0.XMM_D(0)));
B
bellard 已提交
4102 4103
                    }
                } else {
B
bellard 已提交
4104
                    gen_ldo_env_A0(s->mem_index, op2_offset);
B
bellard 已提交
4105 4106 4107 4108 4109 4110 4111 4112
                }
            } 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) {
4113
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
4114
                op2_offset = offsetof(CPUX86State,mmx_t0);
B
bellard 已提交
4115
                gen_ldq_env_A0(s->mem_index, op2_offset);
B
bellard 已提交
4116 4117 4118 4119 4120 4121
            } else {
                rm = (modrm & 7);
                op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
            }
        }
        switch(b) {
A
aurel32 已提交
4122
        case 0x0f: /* 3DNow! data insns */
4123 4124
            if (!(s->cpuid_ext2_features & CPUID_EXT2_3DNOW))
                goto illegal_op;
4125
            val = cpu_ldub_code(env, s->pc++);
B
Blue Swirl 已提交
4126 4127
            sse_fn_epp = sse_op_table5[val];
            if (!sse_fn_epp) {
A
aurel32 已提交
4128
                goto illegal_op;
B
Blue Swirl 已提交
4129
            }
B
bellard 已提交
4130 4131
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4132
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
A
aurel32 已提交
4133
            break;
B
bellard 已提交
4134 4135
        case 0x70: /* pshufx insn */
        case 0xc6: /* pshufx insn */
4136
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4137 4138
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4139
            /* XXX: introduce a new table? */
B
Blue Swirl 已提交
4140
            sse_fn_ppi = (SSEFunc_0_ppi)sse_fn_epp;
B
Blue Swirl 已提交
4141
            sse_fn_ppi(cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
B
bellard 已提交
4142 4143 4144
            break;
        case 0xc2:
            /* compare insns */
4145
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4146 4147
            if (val >= 8)
                goto illegal_op;
B
Blue Swirl 已提交
4148
            sse_fn_epp = sse_op_table4[val][b1];
B
Blue Swirl 已提交
4149

B
bellard 已提交
4150 4151
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4152
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
4153
            break;
4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171
        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 已提交
4172
            /* XXX: introduce a new table? */
B
Blue Swirl 已提交
4173 4174
            sse_fn_eppt = (SSEFunc_0_eppt)sse_fn_epp;
            sse_fn_eppt(cpu_env, cpu_ptr0, cpu_ptr1, cpu_A0);
4175
            break;
B
bellard 已提交
4176
        default:
B
bellard 已提交
4177 4178
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4179
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
4180 4181 4182
            break;
        }
        if (b == 0x2e || b == 0x2f) {
4183
            set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
4184 4185 4186 4187
        }
    }
}

B
bellard 已提交
4188 4189
/* convert one instruction. s->is_jmp is set if the translation must
   be stopped. Return the next pc value */
4190 4191
static target_ulong disas_insn(CPUX86State *env, DisasContext *s,
                               target_ulong pc_start)
B
bellard 已提交
4192 4193 4194 4195
{
    int b, prefixes, aflag, dflag;
    int shift, ot;
    int modrm, reg, rm, mod, reg_addr, op, opreg, offset_addr, val;
B
bellard 已提交
4196 4197
    target_ulong next_eip, tval;
    int rex_w, rex_r;
B
bellard 已提交
4198

4199
    if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
4200
        tcg_gen_debug_insn_start(pc_start);
4201
    }
B
bellard 已提交
4202 4203 4204 4205 4206
    s->pc = pc_start;
    prefixes = 0;
    aflag = s->code32;
    dflag = s->code32;
    s->override = -1;
B
bellard 已提交
4207 4208 4209 4210 4211
    rex_w = -1;
    rex_r = 0;
#ifdef TARGET_X86_64
    s->rex_x = 0;
    s->rex_b = 0;
4212
    x86_64_hregs = 0;
B
bellard 已提交
4213 4214
#endif
    s->rip_offset = 0; /* for relative ip address */
B
bellard 已提交
4215
 next_byte:
4216
    b = cpu_ldub_code(env, s->pc);
B
bellard 已提交
4217 4218
    s->pc++;
    /* check prefixes */
B
bellard 已提交
4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272
#ifdef TARGET_X86_64
    if (CODE64(s)) {
        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;
        case 0x40 ... 0x4f:
            /* 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;
        }
        if (rex_w == 1) {
            /* 0x66 is ignored if rex.w is set */
            dflag = 2;
        } else {
            if (prefixes & PREFIX_DATA)
                dflag ^= 1;
        }
        if (!(prefixes & PREFIX_ADR))
            aflag = 2;
4273
    } else
B
bellard 已提交
4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314
#endif
    {
        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;
        }
        if (prefixes & PREFIX_DATA)
            dflag ^= 1;
        if (prefixes & PREFIX_ADR)
            aflag ^= 1;
B
bellard 已提交
4315 4316 4317 4318 4319 4320 4321 4322
    }

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

    /* lock generation */
    if (prefixes & PREFIX_LOCK)
P
pbrook 已提交
4323
        gen_helper_lock();
B
bellard 已提交
4324 4325 4326 4327 4328 4329 4330

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

B
bellard 已提交
4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351
        /**************************/
        /* 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 已提交
4352
                ot = dflag + OT_WORD;
4353

B
bellard 已提交
4354 4355
            switch(f) {
            case 0: /* OP Ev, Gv */
4356
                modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4357
                reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
4358
                mod = (modrm >> 6) & 3;
B
bellard 已提交
4359
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4360
                if (mod != 3) {
4361
                    gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
4362 4363 4364 4365 4366
                    opreg = OR_TMP0;
                } else if (op == OP_XORL && rm == reg) {
                xor_zero:
                    /* xor reg, reg optimisation */
                    gen_op_movl_T0_0();
4367
                    set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
4368
                    gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
4369 4370 4371 4372 4373
                    gen_op_update1_cc();
                    break;
                } else {
                    opreg = rm;
                }
B
bellard 已提交
4374
                gen_op_mov_TN_reg(ot, 1, reg);
B
bellard 已提交
4375 4376 4377
                gen_op(s, op, ot, opreg);
                break;
            case 1: /* OP Gv, Ev */
4378
                modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4379
                mod = (modrm >> 6) & 3;
B
bellard 已提交
4380 4381
                reg = ((modrm >> 3) & 7) | rex_r;
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4382
                if (mod != 3) {
4383
                    gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
4384
                    gen_op_ld_T1_A0(ot + s->mem_index);
B
bellard 已提交
4385 4386 4387
                } else if (op == OP_XORL && rm == reg) {
                    goto xor_zero;
                } else {
B
bellard 已提交
4388
                    gen_op_mov_TN_reg(ot, 1, rm);
B
bellard 已提交
4389 4390 4391 4392
                }
                gen_op(s, op, ot, reg);
                break;
            case 2: /* OP A, Iv */
4393
                val = insn_get(env, s, ot);
B
bellard 已提交
4394 4395 4396 4397 4398 4399 4400
                gen_op_movl_T1_im(val);
                gen_op(s, op, ot, OR_EAX);
                break;
            }
        }
        break;

4401 4402 4403
    case 0x82:
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
4404 4405 4406 4407 4408 4409 4410 4411 4412
    case 0x80: /* GRP1 */
    case 0x81:
    case 0x83:
        {
            int val;

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

4415
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4416
            mod = (modrm >> 6) & 3;
B
bellard 已提交
4417
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4418
            op = (modrm >> 3) & 7;
4419

B
bellard 已提交
4420
            if (mod != 3) {
B
bellard 已提交
4421 4422 4423 4424
                if (b == 0x83)
                    s->rip_offset = 1;
                else
                    s->rip_offset = insn_const_size(ot);
4425
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
4426 4427
                opreg = OR_TMP0;
            } else {
B
bellard 已提交
4428
                opreg = rm;
B
bellard 已提交
4429 4430 4431 4432 4433 4434
            }

            switch(b) {
            default:
            case 0x80:
            case 0x81:
4435
            case 0x82:
4436
                val = insn_get(env, s, ot);
B
bellard 已提交
4437 4438
                break;
            case 0x83:
4439
                val = (int8_t)insn_get(env, s, OT_BYTE);
B
bellard 已提交
4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461
                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 已提交
4462
            ot = dflag + OT_WORD;
B
bellard 已提交
4463

4464
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4465
        mod = (modrm >> 6) & 3;
B
bellard 已提交
4466
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4467 4468
        op = (modrm >> 3) & 7;
        if (mod != 3) {
B
bellard 已提交
4469 4470
            if (op == 0)
                s->rip_offset = insn_const_size(ot);
4471
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
4472
            gen_op_ld_T0_A0(ot + s->mem_index);
B
bellard 已提交
4473
        } else {
B
bellard 已提交
4474
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
4475 4476 4477 4478
        }

        switch(op) {
        case 0: /* test */
4479
            val = insn_get(env, s, ot);
B
bellard 已提交
4480 4481
            gen_op_movl_T1_im(val);
            gen_op_testl_T0_T1_cc();
4482
            set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
4483 4484
            break;
        case 2: /* not */
4485
            tcg_gen_not_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
4486
            if (mod != 3) {
B
bellard 已提交
4487
                gen_op_st_T0_A0(ot + s->mem_index);
B
bellard 已提交
4488
            } else {
B
bellard 已提交
4489
                gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
4490 4491 4492
            }
            break;
        case 3: /* neg */
4493
            tcg_gen_neg_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
4494
            if (mod != 3) {
B
bellard 已提交
4495
                gen_op_st_T0_A0(ot + s->mem_index);
B
bellard 已提交
4496
            } else {
B
bellard 已提交
4497
                gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
4498 4499
            }
            gen_op_update_neg_cc();
4500
            set_cc_op(s, CC_OP_SUBB + ot);
B
bellard 已提交
4501 4502 4503 4504
            break;
        case 4: /* mul */
            switch(ot) {
            case OT_BYTE:
B
bellard 已提交
4505 4506 4507 4508 4509 4510 4511 4512
                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);
4513
                set_cc_op(s, CC_OP_MULB);
B
bellard 已提交
4514 4515
                break;
            case OT_WORD:
B
bellard 已提交
4516 4517 4518 4519 4520 4521 4522 4523 4524 4525
                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]);
4526
                set_cc_op(s, CC_OP_MULW);
B
bellard 已提交
4527 4528 4529
                break;
            default:
            case OT_LONG:
B
bellard 已提交
4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541
#ifdef TARGET_X86_64
                gen_op_mov_TN_reg(OT_LONG, 1, R_EAX);
                tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
                tcg_gen_ext32u_tl(cpu_T[1], cpu_T[1]);
                tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
                gen_op_mov_reg_T0(OT_LONG, R_EAX);
                tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                tcg_gen_shri_tl(cpu_T[0], cpu_T[0], 32);
                gen_op_mov_reg_T0(OT_LONG, R_EDX);
                tcg_gen_mov_tl(cpu_cc_src, cpu_T[0]);
#else
                {
P
pbrook 已提交
4542 4543 4544
                    TCGv_i64 t0, t1;
                    t0 = tcg_temp_new_i64();
                    t1 = tcg_temp_new_i64();
B
bellard 已提交
4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557
                    gen_op_mov_TN_reg(OT_LONG, 1, R_EAX);
                    tcg_gen_extu_i32_i64(t0, cpu_T[0]);
                    tcg_gen_extu_i32_i64(t1, cpu_T[1]);
                    tcg_gen_mul_i64(t0, t0, t1);
                    tcg_gen_trunc_i64_i32(cpu_T[0], t0);
                    gen_op_mov_reg_T0(OT_LONG, R_EAX);
                    tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                    tcg_gen_shri_i64(t0, t0, 32);
                    tcg_gen_trunc_i64_i32(cpu_T[0], t0);
                    gen_op_mov_reg_T0(OT_LONG, R_EDX);
                    tcg_gen_mov_tl(cpu_cc_src, cpu_T[0]);
                }
#endif
4558
                set_cc_op(s, CC_OP_MULL);
B
bellard 已提交
4559
                break;
B
bellard 已提交
4560 4561
#ifdef TARGET_X86_64
            case OT_QUAD:
4562
                gen_helper_mulq_EAX_T0(cpu_env, cpu_T[0]);
4563
                set_cc_op(s, CC_OP_MULQ);
B
bellard 已提交
4564 4565
                break;
#endif
B
bellard 已提交
4566 4567 4568 4569 4570
            }
            break;
        case 5: /* imul */
            switch(ot) {
            case OT_BYTE:
B
bellard 已提交
4571 4572 4573 4574 4575 4576 4577 4578 4579
                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);
4580
                set_cc_op(s, CC_OP_MULB);
B
bellard 已提交
4581 4582
                break;
            case OT_WORD:
B
bellard 已提交
4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593
                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);
4594
                set_cc_op(s, CC_OP_MULW);
B
bellard 已提交
4595 4596 4597
                break;
            default:
            case OT_LONG:
B
bellard 已提交
4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610
#ifdef TARGET_X86_64
                gen_op_mov_TN_reg(OT_LONG, 1, R_EAX);
                tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
                tcg_gen_ext32s_tl(cpu_T[1], cpu_T[1]);
                tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
                gen_op_mov_reg_T0(OT_LONG, R_EAX);
                tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                tcg_gen_ext32s_tl(cpu_tmp0, cpu_T[0]);
                tcg_gen_sub_tl(cpu_cc_src, cpu_T[0], cpu_tmp0);
                tcg_gen_shri_tl(cpu_T[0], cpu_T[0], 32);
                gen_op_mov_reg_T0(OT_LONG, R_EDX);
#else
                {
P
pbrook 已提交
4611 4612 4613
                    TCGv_i64 t0, t1;
                    t0 = tcg_temp_new_i64();
                    t1 = tcg_temp_new_i64();
B
bellard 已提交
4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627
                    gen_op_mov_TN_reg(OT_LONG, 1, R_EAX);
                    tcg_gen_ext_i32_i64(t0, cpu_T[0]);
                    tcg_gen_ext_i32_i64(t1, cpu_T[1]);
                    tcg_gen_mul_i64(t0, t0, t1);
                    tcg_gen_trunc_i64_i32(cpu_T[0], t0);
                    gen_op_mov_reg_T0(OT_LONG, R_EAX);
                    tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                    tcg_gen_sari_tl(cpu_tmp0, cpu_T[0], 31);
                    tcg_gen_shri_i64(t0, t0, 32);
                    tcg_gen_trunc_i64_i32(cpu_T[0], t0);
                    gen_op_mov_reg_T0(OT_LONG, R_EDX);
                    tcg_gen_sub_tl(cpu_cc_src, cpu_T[0], cpu_tmp0);
                }
#endif
4628
                set_cc_op(s, CC_OP_MULL);
B
bellard 已提交
4629
                break;
B
bellard 已提交
4630 4631
#ifdef TARGET_X86_64
            case OT_QUAD:
4632
                gen_helper_imulq_EAX_T0(cpu_env, cpu_T[0]);
4633
                set_cc_op(s, CC_OP_MULQ);
B
bellard 已提交
4634 4635
                break;
#endif
B
bellard 已提交
4636 4637 4638 4639 4640
            }
            break;
        case 6: /* div */
            switch(ot) {
            case OT_BYTE:
B
bellard 已提交
4641
                gen_jmp_im(pc_start - s->cs_base);
4642
                gen_helper_divb_AL(cpu_env, cpu_T[0]);
B
bellard 已提交
4643 4644
                break;
            case OT_WORD:
B
bellard 已提交
4645
                gen_jmp_im(pc_start - s->cs_base);
4646
                gen_helper_divw_AX(cpu_env, cpu_T[0]);
B
bellard 已提交
4647 4648 4649
                break;
            default:
            case OT_LONG:
B
bellard 已提交
4650
                gen_jmp_im(pc_start - s->cs_base);
4651
                gen_helper_divl_EAX(cpu_env, cpu_T[0]);
B
bellard 已提交
4652 4653 4654 4655
                break;
#ifdef TARGET_X86_64
            case OT_QUAD:
                gen_jmp_im(pc_start - s->cs_base);
4656
                gen_helper_divq_EAX(cpu_env, cpu_T[0]);
B
bellard 已提交
4657
                break;
B
bellard 已提交
4658
#endif
B
bellard 已提交
4659 4660 4661 4662 4663
            }
            break;
        case 7: /* idiv */
            switch(ot) {
            case OT_BYTE:
B
bellard 已提交
4664
                gen_jmp_im(pc_start - s->cs_base);
4665
                gen_helper_idivb_AL(cpu_env, cpu_T[0]);
B
bellard 已提交
4666 4667
                break;
            case OT_WORD:
B
bellard 已提交
4668
                gen_jmp_im(pc_start - s->cs_base);
4669
                gen_helper_idivw_AX(cpu_env, cpu_T[0]);
B
bellard 已提交
4670 4671 4672
                break;
            default:
            case OT_LONG:
B
bellard 已提交
4673
                gen_jmp_im(pc_start - s->cs_base);
4674
                gen_helper_idivl_EAX(cpu_env, cpu_T[0]);
B
bellard 已提交
4675 4676 4677 4678
                break;
#ifdef TARGET_X86_64
            case OT_QUAD:
                gen_jmp_im(pc_start - s->cs_base);
4679
                gen_helper_idivq_EAX(cpu_env, cpu_T[0]);
B
bellard 已提交
4680
                break;
B
bellard 已提交
4681
#endif
B
bellard 已提交
4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693
            }
            break;
        default:
            goto illegal_op;
        }
        break;

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

4696
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4697
        mod = (modrm >> 6) & 3;
B
bellard 已提交
4698
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4699 4700 4701 4702
        op = (modrm >> 3) & 7;
        if (op >= 2 && b == 0xfe) {
            goto illegal_op;
        }
B
bellard 已提交
4703
        if (CODE64(s)) {
4704
            if (op == 2 || op == 4) {
B
bellard 已提交
4705 4706
                /* operand size for jumps is 64 bit */
                ot = OT_QUAD;
4707
            } else if (op == 3 || op == 5) {
4708
                ot = dflag ? OT_LONG + (rex_w == 1) : OT_WORD;
B
bellard 已提交
4709 4710 4711 4712 4713
            } else if (op == 6) {
                /* default push size is 64 bit */
                ot = dflag ? OT_QUAD : OT_WORD;
            }
        }
B
bellard 已提交
4714
        if (mod != 3) {
4715
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
4716
            if (op >= 2 && op != 3 && op != 5)
B
bellard 已提交
4717
                gen_op_ld_T0_A0(ot + s->mem_index);
B
bellard 已提交
4718
        } else {
B
bellard 已提交
4719
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737
        }

        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 */
4738
            /* XXX: optimize if memory (no 'and' is necessary) */
B
bellard 已提交
4739 4740 4741
            if (s->dflag == 0)
                gen_op_andl_T0_ffff();
            next_eip = s->pc - s->cs_base;
B
bellard 已提交
4742
            gen_movtl_T1_im(next_eip);
4743 4744
            gen_push_T1(s);
            gen_op_jmp_T0();
B
bellard 已提交
4745 4746
            gen_eob(s);
            break;
B
bellard 已提交
4747
        case 3: /* lcall Ev */
B
bellard 已提交
4748
            gen_op_ld_T1_A0(ot + s->mem_index);
4749
            gen_add_A0_im(s, 1 << (ot - OT_WORD + 1));
B
bellard 已提交
4750
            gen_op_ldu_T0_A0(OT_WORD + s->mem_index);
B
bellard 已提交
4751 4752
        do_lcall:
            if (s->pe && !s->vm86) {
4753
                gen_update_cc_op(s);
B
bellard 已提交
4754
                gen_jmp_im(pc_start - s->cs_base);
4755
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
4756 4757
                gen_helper_lcall_protected(cpu_env, cpu_tmp2_i32, cpu_T[1],
                                           tcg_const_i32(dflag),
P
pbrook 已提交
4758
                                           tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
4759
            } else {
4760
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
4761 4762
                gen_helper_lcall_real(cpu_env, cpu_tmp2_i32, cpu_T[1],
                                      tcg_const_i32(dflag),
P
pbrook 已提交
4763
                                      tcg_const_i32(s->pc - s->cs_base));
B
bellard 已提交
4764 4765 4766 4767 4768 4769 4770 4771 4772 4773
            }
            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 已提交
4774
            gen_op_ld_T1_A0(ot + s->mem_index);
4775
            gen_add_A0_im(s, 1 << (ot - OT_WORD + 1));
B
bellard 已提交
4776
            gen_op_ldu_T0_A0(OT_WORD + s->mem_index);
B
bellard 已提交
4777 4778
        do_ljmp:
            if (s->pe && !s->vm86) {
4779
                gen_update_cc_op(s);
B
bellard 已提交
4780
                gen_jmp_im(pc_start - s->cs_base);
4781
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
4782
                gen_helper_ljmp_protected(cpu_env, cpu_tmp2_i32, cpu_T[1],
P
pbrook 已提交
4783
                                          tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
4784
            } else {
4785
                gen_op_movl_seg_T0_vm(R_CS);
B
bellard 已提交
4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799
                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 */
4800
    case 0x85:
B
bellard 已提交
4801 4802 4803
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
4804
            ot = dflag + OT_WORD;
B
bellard 已提交
4805

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

4809
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
bellard 已提交
4810
        gen_op_mov_TN_reg(ot, 1, reg);
B
bellard 已提交
4811
        gen_op_testl_T0_T1_cc();
4812
        set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
4813
        break;
4814

B
bellard 已提交
4815 4816 4817 4818 4819
    case 0xa8: /* test eAX, Iv */
    case 0xa9:
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
4820
            ot = dflag + OT_WORD;
4821
        val = insn_get(env, s, ot);
B
bellard 已提交
4822

B
bellard 已提交
4823
        gen_op_mov_TN_reg(ot, 0, OR_EAX);
B
bellard 已提交
4824 4825
        gen_op_movl_T1_im(val);
        gen_op_testl_T0_T1_cc();
4826
        set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
4827
        break;
4828

B
bellard 已提交
4829
    case 0x98: /* CWDE/CBW */
B
bellard 已提交
4830 4831
#ifdef TARGET_X86_64
        if (dflag == 2) {
B
bellard 已提交
4832 4833 4834
            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 已提交
4835 4836
        } else
#endif
B
bellard 已提交
4837 4838 4839 4840 4841 4842 4843 4844 4845
        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 已提交
4846 4847
        break;
    case 0x99: /* CDQ/CWD */
B
bellard 已提交
4848 4849
#ifdef TARGET_X86_64
        if (dflag == 2) {
B
bellard 已提交
4850 4851 4852
            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 已提交
4853 4854
        } else
#endif
B
bellard 已提交
4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865
        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 已提交
4866 4867 4868 4869
        break;
    case 0x1af: /* imul Gv, Ev */
    case 0x69: /* imul Gv, Ev, I */
    case 0x6b:
B
bellard 已提交
4870
        ot = dflag + OT_WORD;
4871
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4872 4873 4874 4875 4876
        reg = ((modrm >> 3) & 7) | rex_r;
        if (b == 0x69)
            s->rip_offset = insn_const_size(ot);
        else if (b == 0x6b)
            s->rip_offset = 1;
4877
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
bellard 已提交
4878
        if (b == 0x69) {
4879
            val = insn_get(env, s, ot);
B
bellard 已提交
4880 4881
            gen_op_movl_T1_im(val);
        } else if (b == 0x6b) {
4882
            val = (int8_t)insn_get(env, s, OT_BYTE);
B
bellard 已提交
4883 4884
            gen_op_movl_T1_im(val);
        } else {
B
bellard 已提交
4885
            gen_op_mov_TN_reg(ot, 1, reg);
B
bellard 已提交
4886 4887
        }

B
bellard 已提交
4888 4889
#ifdef TARGET_X86_64
        if (ot == OT_QUAD) {
4890
            gen_helper_imulq_T0_T1(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
B
bellard 已提交
4891 4892
        } else
#endif
B
bellard 已提交
4893
        if (ot == OT_LONG) {
B
bellard 已提交
4894 4895 4896 4897 4898 4899 4900 4901 4902
#ifdef TARGET_X86_64
                tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
                tcg_gen_ext32s_tl(cpu_T[1], cpu_T[1]);
                tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
                tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                tcg_gen_ext32s_tl(cpu_tmp0, cpu_T[0]);
                tcg_gen_sub_tl(cpu_cc_src, cpu_T[0], cpu_tmp0);
#else
                {
P
pbrook 已提交
4903 4904 4905
                    TCGv_i64 t0, t1;
                    t0 = tcg_temp_new_i64();
                    t1 = tcg_temp_new_i64();
B
bellard 已提交
4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916
                    tcg_gen_ext_i32_i64(t0, cpu_T[0]);
                    tcg_gen_ext_i32_i64(t1, cpu_T[1]);
                    tcg_gen_mul_i64(t0, t0, t1);
                    tcg_gen_trunc_i64_i32(cpu_T[0], t0);
                    tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                    tcg_gen_sari_tl(cpu_tmp0, cpu_T[0], 31);
                    tcg_gen_shri_i64(t0, t0, 32);
                    tcg_gen_trunc_i64_i32(cpu_T[1], t0);
                    tcg_gen_sub_tl(cpu_cc_src, cpu_T[1], cpu_tmp0);
                }
#endif
B
bellard 已提交
4917
        } else {
B
bellard 已提交
4918 4919 4920 4921 4922 4923 4924
            tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
            tcg_gen_ext16s_tl(cpu_T[1], cpu_T[1]);
            /* XXX: use 32 bit mul which could be faster */
            tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
            tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
            tcg_gen_ext16s_tl(cpu_tmp0, cpu_T[0]);
            tcg_gen_sub_tl(cpu_cc_src, cpu_T[0], cpu_tmp0);
B
bellard 已提交
4925
        }
B
bellard 已提交
4926
        gen_op_mov_reg_T0(ot, reg);
4927
        set_cc_op(s, CC_OP_MULB + ot);
B
bellard 已提交
4928 4929 4930 4931 4932 4933
        break;
    case 0x1c0:
    case 0x1c1: /* xadd Ev, Gv */
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
4934
            ot = dflag + OT_WORD;
4935
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4936
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
4937 4938
        mod = (modrm >> 6) & 3;
        if (mod == 3) {
B
bellard 已提交
4939
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4940 4941
            gen_op_mov_TN_reg(ot, 0, reg);
            gen_op_mov_TN_reg(ot, 1, rm);
B
bellard 已提交
4942
            gen_op_addl_T0_T1();
B
bellard 已提交
4943 4944
            gen_op_mov_reg_T1(ot, reg);
            gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
4945
        } else {
4946
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
4947 4948
            gen_op_mov_TN_reg(ot, 0, reg);
            gen_op_ld_T1_A0(ot + s->mem_index);
B
bellard 已提交
4949
            gen_op_addl_T0_T1();
B
bellard 已提交
4950 4951
            gen_op_st_T0_A0(ot + s->mem_index);
            gen_op_mov_reg_T1(ot, reg);
B
bellard 已提交
4952 4953
        }
        gen_op_update2_cc();
4954
        set_cc_op(s, CC_OP_ADDB + ot);
B
bellard 已提交
4955 4956 4957
        break;
    case 0x1b0:
    case 0x1b1: /* cmpxchg Ev, Gv */
B
bellard 已提交
4958
        {
B
bellard 已提交
4959
            int label1, label2;
4960
            TCGv t0, t1, t2, a0;
B
bellard 已提交
4961 4962 4963 4964 4965

            if ((b & 1) == 0)
                ot = OT_BYTE;
            else
                ot = dflag + OT_WORD;
4966
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4967 4968
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
P
pbrook 已提交
4969 4970 4971 4972
            t0 = tcg_temp_local_new();
            t1 = tcg_temp_local_new();
            t2 = tcg_temp_local_new();
            a0 = tcg_temp_local_new();
4973
            gen_op_mov_v_reg(ot, t1, reg);
B
bellard 已提交
4974 4975
            if (mod == 3) {
                rm = (modrm & 7) | REX_B(s);
4976
                gen_op_mov_v_reg(ot, t0, rm);
B
bellard 已提交
4977
            } else {
4978
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
4979 4980
                tcg_gen_mov_tl(a0, cpu_A0);
                gen_op_ld_v(ot + s->mem_index, t0, a0);
B
bellard 已提交
4981 4982 4983
                rm = 0; /* avoid warning */
            }
            label1 = gen_new_label();
4984
            tcg_gen_sub_tl(t2, cpu_regs[R_EAX], t0);
4985 4986
            gen_extu(ot, t2);
            tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, label1);
4987
            label2 = gen_new_label();
B
bellard 已提交
4988
            if (mod == 3) {
4989
                gen_op_mov_reg_v(ot, R_EAX, t0);
B
bellard 已提交
4990 4991
                tcg_gen_br(label2);
                gen_set_label(label1);
4992
                gen_op_mov_reg_v(ot, rm, t1);
B
bellard 已提交
4993
            } else {
4994 4995 4996 4997
                /* 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);
4998
                gen_op_mov_reg_v(ot, R_EAX, t0);
4999
                tcg_gen_br(label2);
B
bellard 已提交
5000
                gen_set_label(label1);
5001
                gen_op_st_v(ot + s->mem_index, t1, a0);
B
bellard 已提交
5002
            }
5003
            gen_set_label(label2);
5004 5005
            tcg_gen_mov_tl(cpu_cc_src, t0);
            tcg_gen_mov_tl(cpu_cc_dst, t2);
5006
            set_cc_op(s, CC_OP_SUBB + ot);
5007 5008 5009 5010
            tcg_temp_free(t0);
            tcg_temp_free(t1);
            tcg_temp_free(t2);
            tcg_temp_free(a0);
B
bellard 已提交
5011 5012 5013
        }
        break;
    case 0x1c7: /* cmpxchg8b */
5014
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5015
        mod = (modrm >> 6) & 3;
5016
        if ((mod == 3) || ((modrm & 0x38) != 0x8))
B
bellard 已提交
5017
            goto illegal_op;
B
bellard 已提交
5018 5019 5020 5021 5022
#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);
5023
            gen_update_cc_op(s);
5024
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
Blue Swirl 已提交
5025
            gen_helper_cmpxchg16b(cpu_env, cpu_A0);
B
bellard 已提交
5026 5027 5028 5029 5030 5031
        } else
#endif        
        {
            if (!(s->cpuid_features & CPUID_CX8))
                goto illegal_op;
            gen_jmp_im(pc_start - s->cs_base);
5032
            gen_update_cc_op(s);
5033
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
Blue Swirl 已提交
5034
            gen_helper_cmpxchg8b(cpu_env, cpu_A0);
B
bellard 已提交
5035
        }
5036
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
5037
        break;
5038

B
bellard 已提交
5039 5040 5041
        /**************************/
        /* push/pop */
    case 0x50 ... 0x57: /* push */
B
bellard 已提交
5042
        gen_op_mov_TN_reg(OT_LONG, 0, (b & 7) | REX_B(s));
B
bellard 已提交
5043 5044 5045
        gen_push_T0(s);
        break;
    case 0x58 ... 0x5f: /* pop */
B
bellard 已提交
5046 5047 5048 5049 5050
        if (CODE64(s)) {
            ot = dflag ? OT_QUAD : OT_WORD;
        } else {
            ot = dflag + OT_WORD;
        }
B
bellard 已提交
5051
        gen_pop_T0(s);
B
bellard 已提交
5052
        /* NOTE: order is important for pop %sp */
B
bellard 已提交
5053
        gen_pop_update(s);
B
bellard 已提交
5054
        gen_op_mov_reg_T0(ot, (b & 7) | REX_B(s));
B
bellard 已提交
5055 5056
        break;
    case 0x60: /* pusha */
B
bellard 已提交
5057 5058
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5059 5060 5061
        gen_pusha(s);
        break;
    case 0x61: /* popa */
B
bellard 已提交
5062 5063
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5064 5065 5066 5067
        gen_popa(s);
        break;
    case 0x68: /* push Iv */
    case 0x6a:
B
bellard 已提交
5068 5069 5070 5071 5072
        if (CODE64(s)) {
            ot = dflag ? OT_QUAD : OT_WORD;
        } else {
            ot = dflag + OT_WORD;
        }
B
bellard 已提交
5073
        if (b == 0x68)
5074
            val = insn_get(env, s, ot);
B
bellard 已提交
5075
        else
5076
            val = (int8_t)insn_get(env, s, OT_BYTE);
B
bellard 已提交
5077 5078 5079 5080
        gen_op_movl_T0_im(val);
        gen_push_T0(s);
        break;
    case 0x8f: /* pop Ev */
B
bellard 已提交
5081 5082 5083 5084 5085
        if (CODE64(s)) {
            ot = dflag ? OT_QUAD : OT_WORD;
        } else {
            ot = dflag + OT_WORD;
        }
5086
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5087
        mod = (modrm >> 6) & 3;
B
bellard 已提交
5088
        gen_pop_T0(s);
B
bellard 已提交
5089 5090 5091
        if (mod == 3) {
            /* NOTE: order is important for pop %sp */
            gen_pop_update(s);
B
bellard 已提交
5092
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
5093
            gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
5094 5095
        } else {
            /* NOTE: order is important too for MMU exceptions */
B
bellard 已提交
5096
            s->popl_esp_hack = 1 << ot;
5097
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
5098 5099 5100
            s->popl_esp_hack = 0;
            gen_pop_update(s);
        }
B
bellard 已提交
5101 5102 5103 5104
        break;
    case 0xc8: /* enter */
        {
            int level;
5105
            val = cpu_lduw_code(env, s->pc);
B
bellard 已提交
5106
            s->pc += 2;
5107
            level = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5108 5109 5110 5111 5112
            gen_enter(s, val, level);
        }
        break;
    case 0xc9: /* leave */
        /* XXX: exception not precise (ESP is updated before potential exception) */
B
bellard 已提交
5113
        if (CODE64(s)) {
B
bellard 已提交
5114 5115
            gen_op_mov_TN_reg(OT_QUAD, 0, R_EBP);
            gen_op_mov_reg_T0(OT_QUAD, R_ESP);
B
bellard 已提交
5116
        } else if (s->ss32) {
B
bellard 已提交
5117 5118
            gen_op_mov_TN_reg(OT_LONG, 0, R_EBP);
            gen_op_mov_reg_T0(OT_LONG, R_ESP);
B
bellard 已提交
5119
        } else {
B
bellard 已提交
5120 5121
            gen_op_mov_TN_reg(OT_WORD, 0, R_EBP);
            gen_op_mov_reg_T0(OT_WORD, R_ESP);
B
bellard 已提交
5122 5123
        }
        gen_pop_T0(s);
B
bellard 已提交
5124 5125 5126 5127 5128
        if (CODE64(s)) {
            ot = dflag ? OT_QUAD : OT_WORD;
        } else {
            ot = dflag + OT_WORD;
        }
B
bellard 已提交
5129
        gen_op_mov_reg_T0(ot, R_EBP);
B
bellard 已提交
5130 5131 5132 5133 5134 5135
        gen_pop_update(s);
        break;
    case 0x06: /* push es */
    case 0x0e: /* push cs */
    case 0x16: /* push ss */
    case 0x1e: /* push ds */
B
bellard 已提交
5136 5137
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148
        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 已提交
5149 5150
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5151 5152 5153 5154 5155
        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) {
5156 5157 5158 5159
            /* if reg == SS, inhibit interrupts/trace. */
            /* If several instructions disable interrupts, only the
               _first_ does it */
            if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
5160
                gen_helper_set_inhibit_irq(cpu_env);
B
bellard 已提交
5161 5162 5163
            s->tf = 0;
        }
        if (s->is_jmp) {
B
bellard 已提交
5164
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5165 5166 5167 5168 5169 5170 5171 5172 5173
            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 已提交
5174
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185
            gen_eob(s);
        }
        break;

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

B
bellard 已提交
5190
        /* generate a generic store */
5191
        gen_ldst_modrm(env, s, modrm, ot, reg, 1);
B
bellard 已提交
5192 5193 5194 5195 5196 5197
        break;
    case 0xc6:
    case 0xc7: /* mov Ev, Iv */
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
5198
            ot = dflag + OT_WORD;
5199
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5200
        mod = (modrm >> 6) & 3;
B
bellard 已提交
5201 5202
        if (mod != 3) {
            s->rip_offset = insn_const_size(ot);
5203
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
5204
        }
5205
        val = insn_get(env, s, ot);
B
bellard 已提交
5206 5207
        gen_op_movl_T0_im(val);
        if (mod != 3)
B
bellard 已提交
5208
            gen_op_st_T0_A0(ot + s->mem_index);
B
bellard 已提交
5209
        else
B
bellard 已提交
5210
            gen_op_mov_reg_T0(ot, (modrm & 7) | REX_B(s));
B
bellard 已提交
5211 5212 5213 5214 5215 5216
        break;
    case 0x8a:
    case 0x8b: /* mov Ev, Gv */
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
5217
            ot = OT_WORD + dflag;
5218
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5219
        reg = ((modrm >> 3) & 7) | rex_r;
5220

5221
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
bellard 已提交
5222
        gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
5223 5224
        break;
    case 0x8e: /* mov seg, Gv */
5225
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5226 5227 5228
        reg = (modrm >> 3) & 7;
        if (reg >= 6 || reg == R_CS)
            goto illegal_op;
5229
        gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
B
bellard 已提交
5230 5231 5232
        gen_movl_seg_T0(s, reg, pc_start - s->cs_base);
        if (reg == R_SS) {
            /* if reg == SS, inhibit interrupts/trace */
5233 5234 5235
            /* If several instructions disable interrupts, only the
               _first_ does it */
            if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
5236
                gen_helper_set_inhibit_irq(cpu_env);
B
bellard 已提交
5237 5238 5239
            s->tf = 0;
        }
        if (s->is_jmp) {
B
bellard 已提交
5240
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5241 5242 5243 5244
            gen_eob(s);
        }
        break;
    case 0x8c: /* mov Gv, seg */
5245
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5246 5247 5248 5249 5250
        reg = (modrm >> 3) & 7;
        mod = (modrm >> 6) & 3;
        if (reg >= 6)
            goto illegal_op;
        gen_op_movl_T0_seg(reg);
B
bellard 已提交
5251 5252 5253 5254
        if (mod == 3)
            ot = OT_WORD + dflag;
        else
            ot = OT_WORD;
5255
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267
        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;
5268
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5269
            reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5270
            mod = (modrm >> 6) & 3;
B
bellard 已提交
5271
            rm = (modrm & 7) | REX_B(s);
5272

B
bellard 已提交
5273
            if (mod == 3) {
B
bellard 已提交
5274
                gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
5275 5276
                switch(ot | (b & 8)) {
                case OT_BYTE:
B
bellard 已提交
5277
                    tcg_gen_ext8u_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5278 5279
                    break;
                case OT_BYTE | 8:
B
bellard 已提交
5280
                    tcg_gen_ext8s_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5281 5282
                    break;
                case OT_WORD:
B
bellard 已提交
5283
                    tcg_gen_ext16u_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5284 5285 5286
                    break;
                default:
                case OT_WORD | 8:
B
bellard 已提交
5287
                    tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5288 5289
                    break;
                }
B
bellard 已提交
5290
                gen_op_mov_reg_T0(d_ot, reg);
B
bellard 已提交
5291
            } else {
5292
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
5293
                if (b & 8) {
B
bellard 已提交
5294
                    gen_op_lds_T0_A0(ot + s->mem_index);
B
bellard 已提交
5295
                } else {
B
bellard 已提交
5296
                    gen_op_ldu_T0_A0(ot + s->mem_index);
B
bellard 已提交
5297
                }
B
bellard 已提交
5298
                gen_op_mov_reg_T0(d_ot, reg);
B
bellard 已提交
5299 5300 5301 5302 5303
            }
        }
        break;

    case 0x8d: /* lea */
B
bellard 已提交
5304
        ot = dflag + OT_WORD;
5305
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5306 5307 5308
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
B
bellard 已提交
5309
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5310 5311 5312 5313
        /* we must ensure that no segment is added */
        s->override = -1;
        val = s->addseg;
        s->addseg = 0;
5314
        gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
5315
        s->addseg = val;
B
bellard 已提交
5316
        gen_op_mov_reg_A0(ot - OT_WORD, reg);
B
bellard 已提交
5317
        break;
5318

B
bellard 已提交
5319 5320 5321 5322 5323
    case 0xa0: /* mov EAX, Ov */
    case 0xa1:
    case 0xa2: /* mov Ov, EAX */
    case 0xa3:
        {
B
bellard 已提交
5324 5325 5326 5327 5328 5329 5330
            target_ulong offset_addr;

            if ((b & 1) == 0)
                ot = OT_BYTE;
            else
                ot = dflag + OT_WORD;
#ifdef TARGET_X86_64
5331
            if (s->aflag == 2) {
5332
                offset_addr = cpu_ldq_code(env, s->pc);
B
bellard 已提交
5333
                s->pc += 8;
B
bellard 已提交
5334
                gen_op_movq_A0_im(offset_addr);
5335
            } else
B
bellard 已提交
5336 5337 5338
#endif
            {
                if (s->aflag) {
5339
                    offset_addr = insn_get(env, s, OT_LONG);
B
bellard 已提交
5340
                } else {
5341
                    offset_addr = insn_get(env, s, OT_WORD);
B
bellard 已提交
5342 5343 5344
                }
                gen_op_movl_A0_im(offset_addr);
            }
B
bellard 已提交
5345
            gen_add_A0_ds_seg(s);
B
bellard 已提交
5346
            if ((b & 2) == 0) {
B
bellard 已提交
5347 5348
                gen_op_ld_T0_A0(ot + s->mem_index);
                gen_op_mov_reg_T0(ot, R_EAX);
B
bellard 已提交
5349
            } else {
B
bellard 已提交
5350 5351
                gen_op_mov_TN_reg(ot, 0, R_EAX);
                gen_op_st_T0_A0(ot + s->mem_index);
B
bellard 已提交
5352 5353 5354 5355
            }
        }
        break;
    case 0xd7: /* xlat */
B
bellard 已提交
5356
#ifdef TARGET_X86_64
5357
        if (s->aflag == 2) {
B
bellard 已提交
5358
            gen_op_movq_A0_reg(R_EBX);
5359 5360 5361
            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]);
5362
        } else
B
bellard 已提交
5363 5364
#endif
        {
B
bellard 已提交
5365
            gen_op_movl_A0_reg(R_EBX);
5366 5367 5368
            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 已提交
5369 5370
            if (s->aflag == 0)
                gen_op_andl_A0_ffff();
5371 5372
            else
                tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffffffff);
B
bellard 已提交
5373
        }
B
bellard 已提交
5374
        gen_add_A0_ds_seg(s);
B
bellard 已提交
5375 5376
        gen_op_ldu_T0_A0(OT_BYTE + s->mem_index);
        gen_op_mov_reg_T0(OT_BYTE, R_EAX);
B
bellard 已提交
5377 5378
        break;
    case 0xb0 ... 0xb7: /* mov R, Ib */
5379
        val = insn_get(env, s, OT_BYTE);
B
bellard 已提交
5380
        gen_op_movl_T0_im(val);
B
bellard 已提交
5381
        gen_op_mov_reg_T0(OT_BYTE, (b & 7) | REX_B(s));
B
bellard 已提交
5382 5383
        break;
    case 0xb8 ... 0xbf: /* mov R, Iv */
B
bellard 已提交
5384 5385 5386 5387
#ifdef TARGET_X86_64
        if (dflag == 2) {
            uint64_t tmp;
            /* 64 bit case */
5388
            tmp = cpu_ldq_code(env, s->pc);
B
bellard 已提交
5389 5390 5391
            s->pc += 8;
            reg = (b & 7) | REX_B(s);
            gen_movtl_T0_im(tmp);
B
bellard 已提交
5392
            gen_op_mov_reg_T0(OT_QUAD, reg);
5393
        } else
B
bellard 已提交
5394 5395 5396
#endif
        {
            ot = dflag ? OT_LONG : OT_WORD;
5397
            val = insn_get(env, s, ot);
B
bellard 已提交
5398 5399
            reg = (b & 7) | REX_B(s);
            gen_op_movl_T0_im(val);
B
bellard 已提交
5400
            gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
5401
        }
B
bellard 已提交
5402 5403 5404
        break;

    case 0x91 ... 0x97: /* xchg R, EAX */
R
Richard Henderson 已提交
5405
    do_xchg_reg_eax:
B
bellard 已提交
5406 5407
        ot = dflag + OT_WORD;
        reg = (b & 7) | REX_B(s);
B
bellard 已提交
5408 5409 5410 5411 5412 5413 5414
        rm = R_EAX;
        goto do_xchg_reg;
    case 0x86:
    case 0x87: /* xchg Ev, Gv */
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
5415
            ot = dflag + OT_WORD;
5416
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5417
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5418 5419
        mod = (modrm >> 6) & 3;
        if (mod == 3) {
B
bellard 已提交
5420
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
5421
        do_xchg_reg:
B
bellard 已提交
5422 5423 5424 5425
            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 已提交
5426
        } else {
5427
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
5428
            gen_op_mov_TN_reg(ot, 0, reg);
B
bellard 已提交
5429 5430
            /* for xchg, lock is implicit */
            if (!(prefixes & PREFIX_LOCK))
P
pbrook 已提交
5431
                gen_helper_lock();
B
bellard 已提交
5432 5433
            gen_op_ld_T1_A0(ot + s->mem_index);
            gen_op_st_T0_A0(ot + s->mem_index);
B
bellard 已提交
5434
            if (!(prefixes & PREFIX_LOCK))
P
pbrook 已提交
5435
                gen_helper_unlock();
B
bellard 已提交
5436
            gen_op_mov_reg_T1(ot, reg);
B
bellard 已提交
5437 5438 5439
        }
        break;
    case 0xc4: /* les Gv */
B
bellard 已提交
5440 5441
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5442 5443 5444
        op = R_ES;
        goto do_lxx;
    case 0xc5: /* lds Gv */
B
bellard 已提交
5445 5446
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458
        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;
5459
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5460
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5461 5462 5463
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
5464
        gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
5465
        gen_op_ld_T1_A0(ot + s->mem_index);
5466
        gen_add_A0_im(s, 1 << (ot - OT_WORD + 1));
B
bellard 已提交
5467
        /* load the segment first to handle exceptions properly */
B
bellard 已提交
5468
        gen_op_ldu_T0_A0(OT_WORD + s->mem_index);
B
bellard 已提交
5469 5470
        gen_movl_seg_T0(s, op, pc_start - s->cs_base);
        /* then put the data */
B
bellard 已提交
5471
        gen_op_mov_reg_T1(ot, reg);
B
bellard 已提交
5472
        if (s->is_jmp) {
B
bellard 已提交
5473
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5474 5475 5476
            gen_eob(s);
        }
        break;
5477

B
bellard 已提交
5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488
        /************************/
        /* shifts */
    case 0xc0:
    case 0xc1:
        /* shift Ev,Ib */
        shift = 2;
    grp2:
        {
            if ((b & 1) == 0)
                ot = OT_BYTE;
            else
B
bellard 已提交
5489
                ot = dflag + OT_WORD;
5490

5491
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5492 5493
            mod = (modrm >> 6) & 3;
            op = (modrm >> 3) & 7;
5494

B
bellard 已提交
5495
            if (mod != 3) {
B
bellard 已提交
5496 5497 5498
                if (shift == 2) {
                    s->rip_offset = 1;
                }
5499
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
5500 5501
                opreg = OR_TMP0;
            } else {
B
bellard 已提交
5502
                opreg = (modrm & 7) | REX_B(s);
B
bellard 已提交
5503 5504 5505 5506 5507 5508 5509
            }

            /* simpler op */
            if (shift == 0) {
                gen_shift(s, op, ot, opreg, OR_ECX);
            } else {
                if (shift == 2) {
5510
                    shift = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542
                }
                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 已提交
5543
        ot = dflag + OT_WORD;
5544
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5545
        mod = (modrm >> 6) & 3;
B
bellard 已提交
5546 5547
        rm = (modrm & 7) | REX_B(s);
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5548
        if (mod != 3) {
5549
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
5550
            opreg = OR_TMP0;
B
bellard 已提交
5551
        } else {
5552
            opreg = rm;
B
bellard 已提交
5553
        }
B
bellard 已提交
5554
        gen_op_mov_TN_reg(ot, 1, reg);
5555

B
bellard 已提交
5556
        if (shift) {
5557
            val = cpu_ldub_code(env, s->pc++);
5558
            tcg_gen_movi_tl(cpu_T3, val);
B
bellard 已提交
5559
        } else {
5560
            tcg_gen_mov_tl(cpu_T3, cpu_regs[R_ECX]);
B
bellard 已提交
5561
        }
5562
        gen_shiftd_rm_T1_T3(s, ot, opreg, op);
B
bellard 已提交
5563 5564 5565 5566
        break;

        /************************/
        /* floats */
5567
    case 0xd8 ... 0xdf:
B
bellard 已提交
5568 5569 5570 5571 5572 5573
        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;
        }
5574
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5575 5576 5577 5578 5579
        mod = (modrm >> 6) & 3;
        rm = modrm & 7;
        op = ((b & 7) << 3) | ((modrm >> 3) & 7);
        if (mod != 3) {
            /* memory op */
5580
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591
            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 已提交
5592
                        gen_op_ld_T0_A0(OT_LONG + s->mem_index);
5593
                        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
5594
                        gen_helper_flds_FT0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5595 5596
                        break;
                    case 1:
B
bellard 已提交
5597
                        gen_op_ld_T0_A0(OT_LONG + s->mem_index);
5598
                        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
5599
                        gen_helper_fildl_FT0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5600 5601
                        break;
                    case 2:
5602
                        tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_A0, 
B
bellard 已提交
5603
                                          (s->mem_index >> 2) - 1);
B
Blue Swirl 已提交
5604
                        gen_helper_fldl_FT0(cpu_env, cpu_tmp1_i64);
B
bellard 已提交
5605 5606 5607
                        break;
                    case 3:
                    default:
B
bellard 已提交
5608
                        gen_op_lds_T0_A0(OT_WORD + s->mem_index);
5609
                        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
5610
                        gen_helper_fildl_FT0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5611 5612
                        break;
                    }
5613

P
pbrook 已提交
5614
                    gen_helper_fp_arith_ST0_FT0(op1);
B
bellard 已提交
5615 5616
                    if (op1 == 3) {
                        /* fcomp needs pop */
B
Blue Swirl 已提交
5617
                        gen_helper_fpop(cpu_env);
B
bellard 已提交
5618 5619 5620 5621 5622 5623
                    }
                }
                break;
            case 0x08: /* flds */
            case 0x0a: /* fsts */
            case 0x0b: /* fstps */
B
bellard 已提交
5624 5625 5626
            case 0x18 ... 0x1b: /* fildl, fisttpl, fistl, fistpl */
            case 0x28 ... 0x2b: /* fldl, fisttpll, fstl, fstpl */
            case 0x38 ... 0x3b: /* filds, fisttps, fists, fistps */
B
bellard 已提交
5627 5628 5629 5630
                switch(op & 7) {
                case 0:
                    switch(op >> 4) {
                    case 0:
B
bellard 已提交
5631
                        gen_op_ld_T0_A0(OT_LONG + s->mem_index);
5632
                        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
5633
                        gen_helper_flds_ST0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5634 5635
                        break;
                    case 1:
B
bellard 已提交
5636
                        gen_op_ld_T0_A0(OT_LONG + s->mem_index);
5637
                        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
5638
                        gen_helper_fildl_ST0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5639 5640
                        break;
                    case 2:
5641
                        tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_A0, 
B
bellard 已提交
5642
                                          (s->mem_index >> 2) - 1);
B
Blue Swirl 已提交
5643
                        gen_helper_fldl_ST0(cpu_env, cpu_tmp1_i64);
B
bellard 已提交
5644 5645 5646
                        break;
                    case 3:
                    default:
B
bellard 已提交
5647
                        gen_op_lds_T0_A0(OT_WORD + s->mem_index);
5648
                        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
5649
                        gen_helper_fildl_ST0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5650 5651 5652
                        break;
                    }
                    break;
B
bellard 已提交
5653
                case 1:
B
bellard 已提交
5654
                    /* XXX: the corresponding CPUID bit must be tested ! */
B
bellard 已提交
5655 5656
                    switch(op >> 4) {
                    case 1:
B
Blue Swirl 已提交
5657
                        gen_helper_fisttl_ST0(cpu_tmp2_i32, cpu_env);
5658
                        tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
5659
                        gen_op_st_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
5660 5661
                        break;
                    case 2:
B
Blue Swirl 已提交
5662
                        gen_helper_fisttll_ST0(cpu_tmp1_i64, cpu_env);
5663
                        tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_A0, 
B
bellard 已提交
5664
                                          (s->mem_index >> 2) - 1);
B
bellard 已提交
5665 5666 5667
                        break;
                    case 3:
                    default:
B
Blue Swirl 已提交
5668
                        gen_helper_fistt_ST0(cpu_tmp2_i32, cpu_env);
5669
                        tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
5670
                        gen_op_st_T0_A0(OT_WORD + s->mem_index);
B
bellard 已提交
5671
                        break;
B
bellard 已提交
5672
                    }
B
Blue Swirl 已提交
5673
                    gen_helper_fpop(cpu_env);
B
bellard 已提交
5674
                    break;
B
bellard 已提交
5675 5676 5677
                default:
                    switch(op >> 4) {
                    case 0:
B
Blue Swirl 已提交
5678
                        gen_helper_fsts_ST0(cpu_tmp2_i32, cpu_env);
5679
                        tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
5680
                        gen_op_st_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
5681 5682
                        break;
                    case 1:
B
Blue Swirl 已提交
5683
                        gen_helper_fistl_ST0(cpu_tmp2_i32, cpu_env);
5684
                        tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
5685
                        gen_op_st_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
5686 5687
                        break;
                    case 2:
B
Blue Swirl 已提交
5688
                        gen_helper_fstl_ST0(cpu_tmp1_i64, cpu_env);
5689
                        tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_A0, 
B
bellard 已提交
5690
                                          (s->mem_index >> 2) - 1);
B
bellard 已提交
5691 5692 5693
                        break;
                    case 3:
                    default:
B
Blue Swirl 已提交
5694
                        gen_helper_fist_ST0(cpu_tmp2_i32, cpu_env);
5695
                        tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
5696
                        gen_op_st_T0_A0(OT_WORD + s->mem_index);
B
bellard 已提交
5697 5698 5699
                        break;
                    }
                    if ((op & 7) == 3)
B
Blue Swirl 已提交
5700
                        gen_helper_fpop(cpu_env);
B
bellard 已提交
5701 5702 5703 5704
                    break;
                }
                break;
            case 0x0c: /* fldenv mem */
5705
                gen_update_cc_op(s);
B
bellard 已提交
5706
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
5707
                gen_helper_fldenv(cpu_env, cpu_A0, tcg_const_i32(s->dflag));
B
bellard 已提交
5708 5709
                break;
            case 0x0d: /* fldcw mem */
B
bellard 已提交
5710
                gen_op_ld_T0_A0(OT_WORD + s->mem_index);
5711
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
5712
                gen_helper_fldcw(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5713 5714
                break;
            case 0x0e: /* fnstenv mem */
5715
                gen_update_cc_op(s);
B
bellard 已提交
5716
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
5717
                gen_helper_fstenv(cpu_env, cpu_A0, tcg_const_i32(s->dflag));
B
bellard 已提交
5718 5719
                break;
            case 0x0f: /* fnstcw mem */
B
Blue Swirl 已提交
5720
                gen_helper_fnstcw(cpu_tmp2_i32, cpu_env);
5721
                tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
5722
                gen_op_st_T0_A0(OT_WORD + s->mem_index);
B
bellard 已提交
5723 5724
                break;
            case 0x1d: /* fldt mem */
5725
                gen_update_cc_op(s);
B
bellard 已提交
5726
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
5727
                gen_helper_fldt_ST0(cpu_env, cpu_A0);
B
bellard 已提交
5728 5729
                break;
            case 0x1f: /* fstpt mem */
5730
                gen_update_cc_op(s);
B
bellard 已提交
5731
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
5732 5733
                gen_helper_fstt_ST0(cpu_env, cpu_A0);
                gen_helper_fpop(cpu_env);
B
bellard 已提交
5734 5735
                break;
            case 0x2c: /* frstor mem */
5736
                gen_update_cc_op(s);
B
bellard 已提交
5737
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
5738
                gen_helper_frstor(cpu_env, cpu_A0, tcg_const_i32(s->dflag));
B
bellard 已提交
5739 5740
                break;
            case 0x2e: /* fnsave mem */
5741
                gen_update_cc_op(s);
B
bellard 已提交
5742
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
5743
                gen_helper_fsave(cpu_env, cpu_A0, tcg_const_i32(s->dflag));
B
bellard 已提交
5744 5745
                break;
            case 0x2f: /* fnstsw mem */
B
Blue Swirl 已提交
5746
                gen_helper_fnstsw(cpu_tmp2_i32, cpu_env);
5747
                tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
5748
                gen_op_st_T0_A0(OT_WORD + s->mem_index);
B
bellard 已提交
5749 5750
                break;
            case 0x3c: /* fbld */
5751
                gen_update_cc_op(s);
B
bellard 已提交
5752
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
5753
                gen_helper_fbld_ST0(cpu_env, cpu_A0);
B
bellard 已提交
5754 5755
                break;
            case 0x3e: /* fbstp */
5756
                gen_update_cc_op(s);
B
bellard 已提交
5757
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
5758 5759
                gen_helper_fbst_ST0(cpu_env, cpu_A0);
                gen_helper_fpop(cpu_env);
B
bellard 已提交
5760 5761
                break;
            case 0x3d: /* fildll */
5762
                tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_A0, 
B
bellard 已提交
5763
                                  (s->mem_index >> 2) - 1);
B
Blue Swirl 已提交
5764
                gen_helper_fildll_ST0(cpu_env, cpu_tmp1_i64);
B
bellard 已提交
5765 5766
                break;
            case 0x3f: /* fistpll */
B
Blue Swirl 已提交
5767
                gen_helper_fistll_ST0(cpu_tmp1_i64, cpu_env);
5768
                tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_A0, 
B
bellard 已提交
5769
                                  (s->mem_index >> 2) - 1);
B
Blue Swirl 已提交
5770
                gen_helper_fpop(cpu_env);
B
bellard 已提交
5771 5772 5773 5774 5775 5776 5777 5778 5779 5780
                break;
            default:
                goto illegal_op;
            }
        } else {
            /* register float ops */
            opreg = rm;

            switch(op) {
            case 0x08: /* fld sti */
B
Blue Swirl 已提交
5781 5782 5783
                gen_helper_fpush(cpu_env);
                gen_helper_fmov_ST0_STN(cpu_env,
                                        tcg_const_i32((opreg + 1) & 7));
B
bellard 已提交
5784 5785
                break;
            case 0x09: /* fxchg sti */
B
bellard 已提交
5786 5787
            case 0x29: /* fxchg4 sti, undocumented op */
            case 0x39: /* fxchg7 sti, undocumented op */
B
Blue Swirl 已提交
5788
                gen_helper_fxchg_ST0_STN(cpu_env, tcg_const_i32(opreg));
B
bellard 已提交
5789 5790 5791 5792
                break;
            case 0x0a: /* grp d9/2 */
                switch(rm) {
                case 0: /* fnop */
5793
                    /* check exceptions (FreeBSD FPU probe) */
5794
                    gen_update_cc_op(s);
B
bellard 已提交
5795
                    gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
5796
                    gen_helper_fwait(cpu_env);
B
bellard 已提交
5797 5798 5799 5800 5801 5802 5803 5804
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x0c: /* grp d9/4 */
                switch(rm) {
                case 0: /* fchs */
B
Blue Swirl 已提交
5805
                    gen_helper_fchs_ST0(cpu_env);
B
bellard 已提交
5806 5807
                    break;
                case 1: /* fabs */
B
Blue Swirl 已提交
5808
                    gen_helper_fabs_ST0(cpu_env);
B
bellard 已提交
5809 5810
                    break;
                case 4: /* ftst */
B
Blue Swirl 已提交
5811 5812
                    gen_helper_fldz_FT0(cpu_env);
                    gen_helper_fcom_ST0_FT0(cpu_env);
B
bellard 已提交
5813 5814
                    break;
                case 5: /* fxam */
B
Blue Swirl 已提交
5815
                    gen_helper_fxam_ST0(cpu_env);
B
bellard 已提交
5816 5817 5818 5819 5820 5821 5822 5823 5824
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x0d: /* grp d9/5 */
                {
                    switch(rm) {
                    case 0:
B
Blue Swirl 已提交
5825 5826
                        gen_helper_fpush(cpu_env);
                        gen_helper_fld1_ST0(cpu_env);
B
bellard 已提交
5827 5828
                        break;
                    case 1:
B
Blue Swirl 已提交
5829 5830
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldl2t_ST0(cpu_env);
B
bellard 已提交
5831 5832
                        break;
                    case 2:
B
Blue Swirl 已提交
5833 5834
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldl2e_ST0(cpu_env);
B
bellard 已提交
5835 5836
                        break;
                    case 3:
B
Blue Swirl 已提交
5837 5838
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldpi_ST0(cpu_env);
B
bellard 已提交
5839 5840
                        break;
                    case 4:
B
Blue Swirl 已提交
5841 5842
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldlg2_ST0(cpu_env);
B
bellard 已提交
5843 5844
                        break;
                    case 5:
B
Blue Swirl 已提交
5845 5846
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldln2_ST0(cpu_env);
B
bellard 已提交
5847 5848
                        break;
                    case 6:
B
Blue Swirl 已提交
5849 5850
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldz_ST0(cpu_env);
B
bellard 已提交
5851 5852 5853 5854 5855 5856 5857 5858 5859
                        break;
                    default:
                        goto illegal_op;
                    }
                }
                break;
            case 0x0e: /* grp d9/6 */
                switch(rm) {
                case 0: /* f2xm1 */
B
Blue Swirl 已提交
5860
                    gen_helper_f2xm1(cpu_env);
B
bellard 已提交
5861 5862
                    break;
                case 1: /* fyl2x */
B
Blue Swirl 已提交
5863
                    gen_helper_fyl2x(cpu_env);
B
bellard 已提交
5864 5865
                    break;
                case 2: /* fptan */
B
Blue Swirl 已提交
5866
                    gen_helper_fptan(cpu_env);
B
bellard 已提交
5867 5868
                    break;
                case 3: /* fpatan */
B
Blue Swirl 已提交
5869
                    gen_helper_fpatan(cpu_env);
B
bellard 已提交
5870 5871
                    break;
                case 4: /* fxtract */
B
Blue Swirl 已提交
5872
                    gen_helper_fxtract(cpu_env);
B
bellard 已提交
5873 5874
                    break;
                case 5: /* fprem1 */
B
Blue Swirl 已提交
5875
                    gen_helper_fprem1(cpu_env);
B
bellard 已提交
5876 5877
                    break;
                case 6: /* fdecstp */
B
Blue Swirl 已提交
5878
                    gen_helper_fdecstp(cpu_env);
B
bellard 已提交
5879 5880 5881
                    break;
                default:
                case 7: /* fincstp */
B
Blue Swirl 已提交
5882
                    gen_helper_fincstp(cpu_env);
B
bellard 已提交
5883 5884 5885 5886 5887 5888
                    break;
                }
                break;
            case 0x0f: /* grp d9/7 */
                switch(rm) {
                case 0: /* fprem */
B
Blue Swirl 已提交
5889
                    gen_helper_fprem(cpu_env);
B
bellard 已提交
5890 5891
                    break;
                case 1: /* fyl2xp1 */
B
Blue Swirl 已提交
5892
                    gen_helper_fyl2xp1(cpu_env);
B
bellard 已提交
5893 5894
                    break;
                case 2: /* fsqrt */
B
Blue Swirl 已提交
5895
                    gen_helper_fsqrt(cpu_env);
B
bellard 已提交
5896 5897
                    break;
                case 3: /* fsincos */
B
Blue Swirl 已提交
5898
                    gen_helper_fsincos(cpu_env);
B
bellard 已提交
5899 5900
                    break;
                case 5: /* fscale */
B
Blue Swirl 已提交
5901
                    gen_helper_fscale(cpu_env);
B
bellard 已提交
5902 5903
                    break;
                case 4: /* frndint */
B
Blue Swirl 已提交
5904
                    gen_helper_frndint(cpu_env);
B
bellard 已提交
5905 5906
                    break;
                case 6: /* fsin */
B
Blue Swirl 已提交
5907
                    gen_helper_fsin(cpu_env);
B
bellard 已提交
5908 5909 5910
                    break;
                default:
                case 7: /* fcos */
B
Blue Swirl 已提交
5911
                    gen_helper_fcos(cpu_env);
B
bellard 已提交
5912 5913 5914 5915 5916 5917 5918 5919
                    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;
5920

B
bellard 已提交
5921 5922
                    op1 = op & 7;
                    if (op >= 0x20) {
P
pbrook 已提交
5923
                        gen_helper_fp_arith_STN_ST0(op1, opreg);
B
bellard 已提交
5924
                        if (op >= 0x30)
B
Blue Swirl 已提交
5925
                            gen_helper_fpop(cpu_env);
B
bellard 已提交
5926
                    } else {
B
Blue Swirl 已提交
5927
                        gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
P
pbrook 已提交
5928
                        gen_helper_fp_arith_ST0_FT0(op1);
B
bellard 已提交
5929 5930 5931 5932
                    }
                }
                break;
            case 0x02: /* fcom */
B
bellard 已提交
5933
            case 0x22: /* fcom2, undocumented op */
B
Blue Swirl 已提交
5934 5935
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fcom_ST0_FT0(cpu_env);
B
bellard 已提交
5936 5937
                break;
            case 0x03: /* fcomp */
B
bellard 已提交
5938 5939
            case 0x23: /* fcomp3, undocumented op */
            case 0x32: /* fcomp5, undocumented op */
B
Blue Swirl 已提交
5940 5941 5942
                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 已提交
5943 5944 5945 5946
                break;
            case 0x15: /* da/5 */
                switch(rm) {
                case 1: /* fucompp */
B
Blue Swirl 已提交
5947 5948 5949 5950
                    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 已提交
5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962
                    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 已提交
5963
                    gen_helper_fclex(cpu_env);
B
bellard 已提交
5964 5965
                    break;
                case 3: /* fninit */
B
Blue Swirl 已提交
5966
                    gen_helper_fninit(cpu_env);
B
bellard 已提交
5967 5968 5969 5970 5971 5972 5973 5974
                    break;
                case 4: /* fsetpm (287 only, just do nop here) */
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x1d: /* fucomi */
5975
                gen_update_cc_op(s);
B
Blue Swirl 已提交
5976 5977
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fucomi_ST0_FT0(cpu_env);
5978
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
5979 5980
                break;
            case 0x1e: /* fcomi */
5981
                gen_update_cc_op(s);
B
Blue Swirl 已提交
5982 5983
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fcomi_ST0_FT0(cpu_env);
5984
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
5985
                break;
B
bellard 已提交
5986
            case 0x28: /* ffree sti */
B
Blue Swirl 已提交
5987
                gen_helper_ffree_STN(cpu_env, tcg_const_i32(opreg));
5988
                break;
B
bellard 已提交
5989
            case 0x2a: /* fst sti */
B
Blue Swirl 已提交
5990
                gen_helper_fmov_STN_ST0(cpu_env, tcg_const_i32(opreg));
B
bellard 已提交
5991 5992
                break;
            case 0x2b: /* fstp sti */
B
bellard 已提交
5993 5994 5995
            case 0x0b: /* fstp1 sti, undocumented op */
            case 0x3a: /* fstp8 sti, undocumented op */
            case 0x3b: /* fstp9 sti, undocumented op */
B
Blue Swirl 已提交
5996 5997
                gen_helper_fmov_STN_ST0(cpu_env, tcg_const_i32(opreg));
                gen_helper_fpop(cpu_env);
B
bellard 已提交
5998 5999
                break;
            case 0x2c: /* fucom st(i) */
B
Blue Swirl 已提交
6000 6001
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fucom_ST0_FT0(cpu_env);
B
bellard 已提交
6002 6003
                break;
            case 0x2d: /* fucomp st(i) */
B
Blue Swirl 已提交
6004 6005 6006
                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 已提交
6007 6008 6009 6010
                break;
            case 0x33: /* de/3 */
                switch(rm) {
                case 1: /* fcompp */
B
Blue Swirl 已提交
6011 6012 6013 6014
                    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 已提交
6015 6016 6017 6018 6019
                    break;
                default:
                    goto illegal_op;
                }
                break;
B
bellard 已提交
6020
            case 0x38: /* ffreep sti, undocumented op */
B
Blue Swirl 已提交
6021 6022
                gen_helper_ffree_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fpop(cpu_env);
B
bellard 已提交
6023
                break;
B
bellard 已提交
6024 6025 6026
            case 0x3c: /* df/4 */
                switch(rm) {
                case 0:
B
Blue Swirl 已提交
6027
                    gen_helper_fnstsw(cpu_tmp2_i32, cpu_env);
6028
                    tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
6029
                    gen_op_mov_reg_T0(OT_WORD, R_EAX);
B
bellard 已提交
6030 6031 6032 6033 6034 6035
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x3d: /* fucomip */
6036
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6037 6038 6039
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fucomi_ST0_FT0(cpu_env);
                gen_helper_fpop(cpu_env);
6040
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6041 6042
                break;
            case 0x3e: /* fcomip */
6043
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6044 6045 6046
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fcomi_ST0_FT0(cpu_env);
                gen_helper_fpop(cpu_env);
6047
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6048
                break;
6049 6050 6051
            case 0x10 ... 0x13: /* fcmovxx */
            case 0x18 ... 0x1b:
                {
B
bellard 已提交
6052
                    int op1, l1;
6053
                    static const uint8_t fcmov_cc[8] = {
6054 6055 6056 6057 6058
                        (JCC_B << 1),
                        (JCC_Z << 1),
                        (JCC_BE << 1),
                        (JCC_P << 1),
                    };
6059
                    op1 = fcmov_cc[op & 3] | (((op >> 3) & 1) ^ 1);
B
bellard 已提交
6060
                    l1 = gen_new_label();
6061
                    gen_jcc1(s, op1, l1);
B
Blue Swirl 已提交
6062
                    gen_helper_fmov_ST0_STN(cpu_env, tcg_const_i32(opreg));
B
bellard 已提交
6063
                    gen_set_label(l1);
6064 6065
                }
                break;
B
bellard 已提交
6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078
            default:
                goto illegal_op;
            }
        }
        break;
        /************************/
        /* string ops */

    case 0xa4: /* movsS */
    case 0xa5:
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
6079
            ot = dflag + OT_WORD;
B
bellard 已提交
6080 6081 6082 6083 6084 6085 6086

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

B
bellard 已提交
6088 6089 6090 6091 6092
    case 0xaa: /* stosS */
    case 0xab:
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
6093
            ot = dflag + OT_WORD;
B
bellard 已提交
6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105

        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 已提交
6106
            ot = dflag + OT_WORD;
B
bellard 已提交
6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117
        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 已提交
6118
            ot = dflag + OT_WORD;
B
bellard 已提交
6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132
        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 已提交
6133
            ot = dflag + OT_WORD;
B
bellard 已提交
6134 6135 6136 6137 6138 6139 6140 6141 6142 6143
        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:
6144 6145 6146 6147
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
B
bellard 已提交
6148
        gen_op_mov_TN_reg(OT_WORD, 0, R_EDX);
T
ths 已提交
6149
        gen_op_andl_T0_ffff();
6150 6151
        gen_check_io(s, ot, pc_start - s->cs_base, 
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes) | 4);
6152 6153
        if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
            gen_repz_ins(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
B
bellard 已提交
6154
        } else {
6155
            gen_ins(s, ot);
P
pbrook 已提交
6156 6157 6158
            if (use_icount) {
                gen_jmp(s, s->pc - s->cs_base);
            }
B
bellard 已提交
6159 6160 6161 6162
        }
        break;
    case 0x6e: /* outsS */
    case 0x6f:
6163 6164 6165 6166
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
B
bellard 已提交
6167
        gen_op_mov_TN_reg(OT_WORD, 0, R_EDX);
T
ths 已提交
6168
        gen_op_andl_T0_ffff();
6169 6170
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes) | 4);
6171 6172
        if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
            gen_repz_outs(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
B
bellard 已提交
6173
        } else {
6174
            gen_outs(s, ot);
P
pbrook 已提交
6175 6176 6177
            if (use_icount) {
                gen_jmp(s, s->pc - s->cs_base);
            }
B
bellard 已提交
6178 6179 6180 6181 6182
        }
        break;

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

B
bellard 已提交
6184 6185
    case 0xe4:
    case 0xe5:
6186 6187 6188 6189
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
6190
        val = cpu_ldub_code(env, s->pc++);
6191
        gen_op_movl_T0_im(val);
6192 6193
        gen_check_io(s, ot, pc_start - s->cs_base,
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
P
pbrook 已提交
6194 6195
        if (use_icount)
            gen_io_start();
6196
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
6197
        gen_helper_in_func(ot, cpu_T[1], cpu_tmp2_i32);
B
bellard 已提交
6198
        gen_op_mov_reg_T1(ot, R_EAX);
P
pbrook 已提交
6199 6200 6201 6202
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6203 6204 6205
        break;
    case 0xe6:
    case 0xe7:
6206 6207 6208 6209
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
6210
        val = cpu_ldub_code(env, s->pc++);
6211
        gen_op_movl_T0_im(val);
6212 6213
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes));
B
bellard 已提交
6214
        gen_op_mov_TN_reg(ot, 1, R_EAX);
6215

P
pbrook 已提交
6216 6217
        if (use_icount)
            gen_io_start();
6218 6219
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
        tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
P
pbrook 已提交
6220
        gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
P
pbrook 已提交
6221 6222 6223 6224
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6225 6226 6227
        break;
    case 0xec:
    case 0xed:
6228 6229 6230 6231
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
B
bellard 已提交
6232
        gen_op_mov_TN_reg(OT_WORD, 0, R_EDX);
6233
        gen_op_andl_T0_ffff();
6234 6235
        gen_check_io(s, ot, pc_start - s->cs_base,
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
P
pbrook 已提交
6236 6237
        if (use_icount)
            gen_io_start();
6238
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
6239
        gen_helper_in_func(ot, cpu_T[1], cpu_tmp2_i32);
B
bellard 已提交
6240
        gen_op_mov_reg_T1(ot, R_EAX);
P
pbrook 已提交
6241 6242 6243 6244
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6245 6246 6247
        break;
    case 0xee:
    case 0xef:
6248 6249 6250 6251
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
B
bellard 已提交
6252
        gen_op_mov_TN_reg(OT_WORD, 0, R_EDX);
6253
        gen_op_andl_T0_ffff();
6254 6255
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes));
B
bellard 已提交
6256
        gen_op_mov_TN_reg(ot, 1, R_EAX);
6257

P
pbrook 已提交
6258 6259
        if (use_icount)
            gen_io_start();
6260 6261
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
        tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
P
pbrook 已提交
6262
        gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
P
pbrook 已提交
6263 6264 6265 6266
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6267 6268 6269 6270 6271
        break;

        /************************/
        /* control */
    case 0xc2: /* ret im */
6272
        val = cpu_ldsw_code(env, s->pc);
B
bellard 已提交
6273 6274
        s->pc += 2;
        gen_pop_T0(s);
6275 6276
        if (CODE64(s) && s->dflag)
            s->dflag = 2;
B
bellard 已提交
6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291
        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 */
6292
        val = cpu_ldsw_code(env, s->pc);
B
bellard 已提交
6293 6294 6295
        s->pc += 2;
    do_lret:
        if (s->pe && !s->vm86) {
6296
            gen_update_cc_op(s);
B
bellard 已提交
6297
            gen_jmp_im(pc_start - s->cs_base);
6298
            gen_helper_lret_protected(cpu_env, tcg_const_i32(s->dflag),
P
pbrook 已提交
6299
                                      tcg_const_i32(val));
B
bellard 已提交
6300 6301 6302
        } else {
            gen_stack_A0(s);
            /* pop offset */
B
bellard 已提交
6303
            gen_op_ld_T0_A0(1 + s->dflag + s->mem_index);
B
bellard 已提交
6304 6305 6306 6307 6308 6309 6310
            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 已提交
6311
            gen_op_ld_T0_A0(1 + s->dflag + s->mem_index);
6312
            gen_op_movl_seg_T0_vm(R_CS);
B
bellard 已提交
6313 6314 6315 6316 6317 6318 6319 6320 6321
            /* 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 已提交
6322
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_IRET);
B
bellard 已提交
6323 6324
        if (!s->pe) {
            /* real mode */
6325
            gen_helper_iret_real(cpu_env, tcg_const_i32(s->dflag));
6326
            set_cc_op(s, CC_OP_EFLAGS);
6327 6328 6329 6330
        } else if (s->vm86) {
            if (s->iopl != 3) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
6331
                gen_helper_iret_real(cpu_env, tcg_const_i32(s->dflag));
6332
                set_cc_op(s, CC_OP_EFLAGS);
6333
            }
B
bellard 已提交
6334
        } else {
6335
            gen_update_cc_op(s);
B
bellard 已提交
6336
            gen_jmp_im(pc_start - s->cs_base);
6337
            gen_helper_iret_protected(cpu_env, tcg_const_i32(s->dflag),
P
pbrook 已提交
6338
                                      tcg_const_i32(s->pc - s->cs_base));
6339
            set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6340 6341 6342 6343 6344
        }
        gen_eob(s);
        break;
    case 0xe8: /* call im */
        {
B
bellard 已提交
6345
            if (dflag)
6346
                tval = (int32_t)insn_get(env, s, OT_LONG);
B
bellard 已提交
6347
            else
6348
                tval = (int16_t)insn_get(env, s, OT_WORD);
B
bellard 已提交
6349
            next_eip = s->pc - s->cs_base;
B
bellard 已提交
6350
            tval += next_eip;
B
bellard 已提交
6351
            if (s->dflag == 0)
B
bellard 已提交
6352
                tval &= 0xffff;
6353 6354
            else if(!CODE64(s))
                tval &= 0xffffffff;
B
bellard 已提交
6355
            gen_movtl_T0_im(next_eip);
B
bellard 已提交
6356
            gen_push_T0(s);
B
bellard 已提交
6357
            gen_jmp(s, tval);
B
bellard 已提交
6358 6359 6360 6361 6362
        }
        break;
    case 0x9a: /* lcall im */
        {
            unsigned int selector, offset;
6363

B
bellard 已提交
6364 6365
            if (CODE64(s))
                goto illegal_op;
B
bellard 已提交
6366
            ot = dflag ? OT_LONG : OT_WORD;
6367 6368
            offset = insn_get(env, s, ot);
            selector = insn_get(env, s, OT_WORD);
6369

B
bellard 已提交
6370
            gen_op_movl_T0_im(selector);
B
bellard 已提交
6371
            gen_op_movl_T1_imu(offset);
B
bellard 已提交
6372 6373
        }
        goto do_lcall;
B
bellard 已提交
6374
    case 0xe9: /* jmp im */
B
bellard 已提交
6375
        if (dflag)
6376
            tval = (int32_t)insn_get(env, s, OT_LONG);
B
bellard 已提交
6377
        else
6378
            tval = (int16_t)insn_get(env, s, OT_WORD);
B
bellard 已提交
6379
        tval += s->pc - s->cs_base;
B
bellard 已提交
6380
        if (s->dflag == 0)
B
bellard 已提交
6381
            tval &= 0xffff;
6382 6383
        else if(!CODE64(s))
            tval &= 0xffffffff;
B
bellard 已提交
6384
        gen_jmp(s, tval);
B
bellard 已提交
6385 6386 6387 6388 6389
        break;
    case 0xea: /* ljmp im */
        {
            unsigned int selector, offset;

B
bellard 已提交
6390 6391
            if (CODE64(s))
                goto illegal_op;
B
bellard 已提交
6392
            ot = dflag ? OT_LONG : OT_WORD;
6393 6394
            offset = insn_get(env, s, ot);
            selector = insn_get(env, s, OT_WORD);
6395

B
bellard 已提交
6396
            gen_op_movl_T0_im(selector);
B
bellard 已提交
6397
            gen_op_movl_T1_imu(offset);
B
bellard 已提交
6398 6399 6400
        }
        goto do_ljmp;
    case 0xeb: /* jmp Jb */
6401
        tval = (int8_t)insn_get(env, s, OT_BYTE);
B
bellard 已提交
6402
        tval += s->pc - s->cs_base;
B
bellard 已提交
6403
        if (s->dflag == 0)
B
bellard 已提交
6404 6405
            tval &= 0xffff;
        gen_jmp(s, tval);
B
bellard 已提交
6406 6407
        break;
    case 0x70 ... 0x7f: /* jcc Jb */
6408
        tval = (int8_t)insn_get(env, s, OT_BYTE);
B
bellard 已提交
6409 6410 6411
        goto do_jcc;
    case 0x180 ... 0x18f: /* jcc Jv */
        if (dflag) {
6412
            tval = (int32_t)insn_get(env, s, OT_LONG);
B
bellard 已提交
6413
        } else {
6414
            tval = (int16_t)insn_get(env, s, OT_WORD);
B
bellard 已提交
6415 6416 6417
        }
    do_jcc:
        next_eip = s->pc - s->cs_base;
B
bellard 已提交
6418
        tval += next_eip;
B
bellard 已提交
6419
        if (s->dflag == 0)
B
bellard 已提交
6420 6421
            tval &= 0xffff;
        gen_jcc(s, b, tval, next_eip);
B
bellard 已提交
6422 6423 6424
        break;

    case 0x190 ... 0x19f: /* setcc Gv */
6425
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
6426
        gen_setcc(s, b);
6427
        gen_ldst_modrm(env, s, modrm, OT_BYTE, OR_TMP0, 1);
B
bellard 已提交
6428 6429
        break;
    case 0x140 ... 0x14f: /* cmov Gv, Ev */
6430 6431
        {
            int l1;
6432 6433
            TCGv t0;

6434
            ot = dflag + OT_WORD;
6435
            modrm = cpu_ldub_code(env, s->pc++);
6436 6437
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
P
pbrook 已提交
6438
            t0 = tcg_temp_local_new();
6439
            if (mod != 3) {
6440
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
6441
                gen_op_ld_v(ot + s->mem_index, t0, cpu_A0);
6442 6443
            } else {
                rm = (modrm & 7) | REX_B(s);
6444
                gen_op_mov_v_reg(ot, t0, rm);
6445 6446 6447 6448 6449
            }
#ifdef TARGET_X86_64
            if (ot == OT_LONG) {
                /* XXX: specific Intel behaviour ? */
                l1 = gen_new_label();
6450
                gen_jcc1(s, b ^ 1, l1);
6451
                tcg_gen_mov_tl(cpu_regs[reg], t0);
6452
                gen_set_label(l1);
6453
                tcg_gen_ext32u_tl(cpu_regs[reg], cpu_regs[reg]);
6454 6455 6456 6457
            } else
#endif
            {
                l1 = gen_new_label();
6458
                gen_jcc1(s, b ^ 1, l1);
6459
                gen_op_mov_reg_v(ot, reg, t0);
6460 6461
                gen_set_label(l1);
            }
6462
            tcg_temp_free(t0);
B
bellard 已提交
6463 6464
        }
        break;
6465

B
bellard 已提交
6466 6467 6468
        /************************/
        /* flags */
    case 0x9c: /* pushf */
B
bellard 已提交
6469
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_PUSHF);
B
bellard 已提交
6470 6471 6472
        if (s->vm86 && s->iopl != 3) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
6473
            gen_update_cc_op(s);
6474
            gen_helper_read_eflags(cpu_T[0], cpu_env);
B
bellard 已提交
6475 6476 6477 6478
            gen_push_T0(s);
        }
        break;
    case 0x9d: /* popf */
B
bellard 已提交
6479
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_POPF);
B
bellard 已提交
6480 6481 6482 6483 6484 6485
        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) {
6486 6487 6488 6489 6490
                    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 已提交
6491
                } else {
6492 6493 6494 6495 6496
                    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 已提交
6497 6498
                }
            } else {
B
bellard 已提交
6499 6500
                if (s->cpl <= s->iopl) {
                    if (s->dflag) {
6501 6502 6503 6504 6505 6506
                        gen_helper_write_eflags(cpu_env, cpu_T[0],
                                                tcg_const_i32((TF_MASK |
                                                               AC_MASK |
                                                               ID_MASK |
                                                               NT_MASK |
                                                               IF_MASK)));
B
bellard 已提交
6507
                    } else {
6508 6509 6510 6511 6512 6513 6514
                        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 已提交
6515
                    }
B
bellard 已提交
6516
                } else {
B
bellard 已提交
6517
                    if (s->dflag) {
6518 6519 6520
                        gen_helper_write_eflags(cpu_env, cpu_T[0],
                                           tcg_const_i32((TF_MASK | AC_MASK |
                                                          ID_MASK | NT_MASK)));
B
bellard 已提交
6521
                    } else {
6522 6523 6524 6525
                        gen_helper_write_eflags(cpu_env, cpu_T[0],
                                           tcg_const_i32((TF_MASK | AC_MASK |
                                                          ID_MASK | NT_MASK)
                                                         & 0xffff));
B
bellard 已提交
6526
                    }
B
bellard 已提交
6527 6528 6529
                }
            }
            gen_pop_update(s);
6530
            set_cc_op(s, CC_OP_EFLAGS);
H
H. Peter Anvin 已提交
6531
            /* abort translation because TF/AC flag may change */
B
bellard 已提交
6532
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
6533 6534 6535 6536
            gen_eob(s);
        }
        break;
    case 0x9e: /* sahf */
B
bellard 已提交
6537
        if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM))
B
bellard 已提交
6538
            goto illegal_op;
B
bellard 已提交
6539
        gen_op_mov_TN_reg(OT_BYTE, 0, R_AH);
6540
        gen_compute_eflags(s);
6541 6542 6543
        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 已提交
6544 6545
        break;
    case 0x9f: /* lahf */
B
bellard 已提交
6546
        if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM))
B
bellard 已提交
6547
            goto illegal_op;
6548
        gen_compute_eflags(s);
6549
        /* Note: gen_compute_eflags() only gives the condition codes */
6550
        tcg_gen_ori_tl(cpu_T[0], cpu_cc_src, 0x02);
B
bellard 已提交
6551
        gen_op_mov_reg_T0(OT_BYTE, R_AH);
B
bellard 已提交
6552 6553
        break;
    case 0xf5: /* cmc */
6554
        gen_compute_eflags(s);
6555
        tcg_gen_xori_tl(cpu_cc_src, cpu_cc_src, CC_C);
B
bellard 已提交
6556 6557
        break;
    case 0xf8: /* clc */
6558
        gen_compute_eflags(s);
6559
        tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~CC_C);
B
bellard 已提交
6560 6561
        break;
    case 0xf9: /* stc */
6562
        gen_compute_eflags(s);
6563
        tcg_gen_ori_tl(cpu_cc_src, cpu_cc_src, CC_C);
B
bellard 已提交
6564 6565
        break;
    case 0xfc: /* cld */
6566
        tcg_gen_movi_i32(cpu_tmp2_i32, 1);
6567
        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, offsetof(CPUX86State, df));
B
bellard 已提交
6568 6569
        break;
    case 0xfd: /* std */
6570
        tcg_gen_movi_i32(cpu_tmp2_i32, -1);
6571
        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, offsetof(CPUX86State, df));
B
bellard 已提交
6572 6573 6574 6575 6576
        break;

        /************************/
        /* bit operations */
    case 0x1ba: /* bt/bts/btr/btc Gv, im */
B
bellard 已提交
6577
        ot = dflag + OT_WORD;
6578
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
6579
        op = (modrm >> 3) & 7;
B
bellard 已提交
6580
        mod = (modrm >> 6) & 3;
B
bellard 已提交
6581
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
6582
        if (mod != 3) {
B
bellard 已提交
6583
            s->rip_offset = 1;
6584
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
6585
            gen_op_ld_T0_A0(ot + s->mem_index);
B
bellard 已提交
6586
        } else {
B
bellard 已提交
6587
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
6588 6589
        }
        /* load shift */
6590
        val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
6591 6592 6593 6594
        gen_op_movl_T1_im(val);
        if (op < 4)
            goto illegal_op;
        op -= 4;
B
bellard 已提交
6595
        goto bt_op;
B
bellard 已提交
6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607
    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 已提交
6608
        ot = dflag + OT_WORD;
6609
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
6610
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
6611
        mod = (modrm >> 6) & 3;
B
bellard 已提交
6612
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
6613
        gen_op_mov_TN_reg(OT_LONG, 1, reg);
B
bellard 已提交
6614
        if (mod != 3) {
6615
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
6616
            /* specific case: we need to add a displacement */
B
bellard 已提交
6617 6618 6619 6620
            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 已提交
6621
            gen_op_ld_T0_A0(ot + s->mem_index);
B
bellard 已提交
6622
        } else {
B
bellard 已提交
6623
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
6624
        }
B
bellard 已提交
6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652
    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;
        }
6653
        set_cc_op(s, CC_OP_SARB + ot);
B
bellard 已提交
6654 6655
        if (op != 0) {
            if (mod != 3)
B
bellard 已提交
6656
                gen_op_st_T0_A0(ot + s->mem_index);
B
bellard 已提交
6657
            else
B
bellard 已提交
6658
                gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
6659 6660
            tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
            tcg_gen_movi_tl(cpu_cc_dst, 0);
B
bellard 已提交
6661 6662 6663 6664
        }
        break;
    case 0x1bc: /* bsf */
    case 0x1bd: /* bsr */
B
bellard 已提交
6665 6666
        {
            int label1;
6667 6668
            TCGv t0;

B
bellard 已提交
6669
            ot = dflag + OT_WORD;
6670
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
6671
            reg = ((modrm >> 3) & 7) | rex_r;
6672
            gen_ldst_modrm(env, s,modrm, ot, OR_TMP0, 0);
B
bellard 已提交
6673
            gen_extu(ot, cpu_T[0]);
P
pbrook 已提交
6674
            t0 = tcg_temp_local_new();
6675
            tcg_gen_mov_tl(t0, cpu_T[0]);
6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686
            if ((b & 1) && (prefixes & PREFIX_REPZ) &&
                (s->cpuid_ext3_features & CPUID_EXT3_ABM)) {
                switch(ot) {
                case OT_WORD: gen_helper_lzcnt(cpu_T[0], t0,
                    tcg_const_i32(16)); break;
                case OT_LONG: gen_helper_lzcnt(cpu_T[0], t0,
                    tcg_const_i32(32)); break;
                case OT_QUAD: gen_helper_lzcnt(cpu_T[0], t0,
                    tcg_const_i32(64)); break;
                }
                gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
6687
            } else {
6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698
                label1 = gen_new_label();
                tcg_gen_movi_tl(cpu_cc_dst, 0);
                tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, label1);
                if (b & 1) {
                    gen_helper_bsr(cpu_T[0], t0);
                } else {
                    gen_helper_bsf(cpu_T[0], t0);
                }
                gen_op_mov_reg_T0(ot, reg);
                tcg_gen_movi_tl(cpu_cc_dst, 1);
                gen_set_label(label1);
6699
                set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
6700
            }
6701
            tcg_temp_free(t0);
B
bellard 已提交
6702
        }
B
bellard 已提交
6703 6704 6705 6706
        break;
        /************************/
        /* bcd */
    case 0x27: /* daa */
B
bellard 已提交
6707 6708
        if (CODE64(s))
            goto illegal_op;
6709
        gen_update_cc_op(s);
6710
        gen_helper_daa(cpu_env);
6711
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6712 6713
        break;
    case 0x2f: /* das */
B
bellard 已提交
6714 6715
        if (CODE64(s))
            goto illegal_op;
6716
        gen_update_cc_op(s);
6717
        gen_helper_das(cpu_env);
6718
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6719 6720
        break;
    case 0x37: /* aaa */
B
bellard 已提交
6721 6722
        if (CODE64(s))
            goto illegal_op;
6723
        gen_update_cc_op(s);
6724
        gen_helper_aaa(cpu_env);
6725
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6726 6727
        break;
    case 0x3f: /* aas */
B
bellard 已提交
6728 6729
        if (CODE64(s))
            goto illegal_op;
6730
        gen_update_cc_op(s);
6731
        gen_helper_aas(cpu_env);
6732
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6733 6734
        break;
    case 0xd4: /* aam */
B
bellard 已提交
6735 6736
        if (CODE64(s))
            goto illegal_op;
6737
        val = cpu_ldub_code(env, s->pc++);
6738 6739 6740
        if (val == 0) {
            gen_exception(s, EXCP00_DIVZ, pc_start - s->cs_base);
        } else {
6741
            gen_helper_aam(cpu_env, tcg_const_i32(val));
6742
            set_cc_op(s, CC_OP_LOGICB);
6743
        }
B
bellard 已提交
6744 6745
        break;
    case 0xd5: /* aad */
B
bellard 已提交
6746 6747
        if (CODE64(s))
            goto illegal_op;
6748
        val = cpu_ldub_code(env, s->pc++);
6749
        gen_helper_aad(cpu_env, tcg_const_i32(val));
6750
        set_cc_op(s, CC_OP_LOGICB);
B
bellard 已提交
6751 6752 6753 6754
        break;
        /************************/
        /* misc */
    case 0x90: /* nop */
6755
        /* XXX: correct lock test for all insn */
R
Richard Henderson 已提交
6756
        if (prefixes & PREFIX_LOCK) {
6757
            goto illegal_op;
R
Richard Henderson 已提交
6758 6759 6760 6761 6762
        }
        /* 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 已提交
6763 6764 6765
        if (prefixes & PREFIX_REPZ) {
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_PAUSE);
        }
B
bellard 已提交
6766 6767
        break;
    case 0x9b: /* fwait */
6768
        if ((s->flags & (HF_MP_MASK | HF_TS_MASK)) ==
B
bellard 已提交
6769 6770
            (HF_MP_MASK | HF_TS_MASK)) {
            gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
B
bellard 已提交
6771
        } else {
6772
            gen_update_cc_op(s);
B
bellard 已提交
6773
            gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6774
            gen_helper_fwait(cpu_env);
B
bellard 已提交
6775
        }
B
bellard 已提交
6776 6777 6778 6779 6780
        break;
    case 0xcc: /* int3 */
        gen_interrupt(s, EXCP03_INT3, pc_start - s->cs_base, s->pc - s->cs_base);
        break;
    case 0xcd: /* int N */
6781
        val = cpu_ldub_code(env, s->pc++);
6782
        if (s->vm86 && s->iopl != 3) {
6783
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6784 6785 6786
        } else {
            gen_interrupt(s, val, pc_start - s->cs_base, s->pc - s->cs_base);
        }
B
bellard 已提交
6787 6788
        break;
    case 0xce: /* into */
B
bellard 已提交
6789 6790
        if (CODE64(s))
            goto illegal_op;
6791
        gen_update_cc_op(s);
6792
        gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6793
        gen_helper_into(cpu_env, tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
6794
        break;
A
aurel32 已提交
6795
#ifdef WANT_ICEBP
B
bellard 已提交
6796
    case 0xf1: /* icebp (undocumented, exits to external debugger) */
B
bellard 已提交
6797
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_ICEBP);
6798
#if 1
B
bellard 已提交
6799
        gen_debug(s, pc_start - s->cs_base);
6800 6801
#else
        /* start debug */
6802
        tb_flush(env);
6803
        qemu_set_log(CPU_LOG_INT | CPU_LOG_TB_IN_ASM);
6804
#endif
B
bellard 已提交
6805
        break;
A
aurel32 已提交
6806
#endif
B
bellard 已提交
6807 6808 6809
    case 0xfa: /* cli */
        if (!s->vm86) {
            if (s->cpl <= s->iopl) {
6810
                gen_helper_cli(cpu_env);
B
bellard 已提交
6811 6812 6813 6814 6815
            } else {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            }
        } else {
            if (s->iopl == 3) {
6816
                gen_helper_cli(cpu_env);
B
bellard 已提交
6817 6818 6819 6820 6821 6822 6823 6824 6825
            } 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:
6826
                gen_helper_sti(cpu_env);
B
bellard 已提交
6827
                /* interruptions are enabled only the first insn after sti */
6828 6829 6830
                /* If several instructions disable interrupts, only the
                   _first_ does it */
                if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
6831
                    gen_helper_set_inhibit_irq(cpu_env);
B
bellard 已提交
6832
                /* give a chance to handle pending irqs */
B
bellard 已提交
6833
                gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846
                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 已提交
6847 6848
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
6849
        ot = dflag ? OT_LONG : OT_WORD;
6850
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
6851 6852 6853 6854
        reg = (modrm >> 3) & 7;
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
B
bellard 已提交
6855
        gen_op_mov_TN_reg(ot, 0, reg);
6856
        gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
6857
        gen_jmp_im(pc_start - s->cs_base);
6858
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
6859 6860 6861 6862 6863
        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 已提交
6864 6865
        break;
    case 0x1c8 ... 0x1cf: /* bswap reg */
B
bellard 已提交
6866 6867 6868
        reg = (b & 7) | REX_B(s);
#ifdef TARGET_X86_64
        if (dflag == 2) {
B
bellard 已提交
6869
            gen_op_mov_TN_reg(OT_QUAD, 0, reg);
A
aurel32 已提交
6870
            tcg_gen_bswap64_i64(cpu_T[0], cpu_T[0]);
B
bellard 已提交
6871
            gen_op_mov_reg_T0(OT_QUAD, reg);
6872
        } else
6873
#endif
B
bellard 已提交
6874 6875
        {
            gen_op_mov_TN_reg(OT_LONG, 0, reg);
6876 6877
            tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
            tcg_gen_bswap32_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
6878
            gen_op_mov_reg_T0(OT_LONG, reg);
B
bellard 已提交
6879
        }
B
bellard 已提交
6880 6881
        break;
    case 0xd6: /* salc */
B
bellard 已提交
6882 6883
        if (CODE64(s))
            goto illegal_op;
6884
        gen_compute_eflags_c(s, cpu_T[0]);
6885 6886
        tcg_gen_neg_tl(cpu_T[0], cpu_T[0]);
        gen_op_mov_reg_T0(OT_BYTE, R_EAX);
B
bellard 已提交
6887 6888 6889 6890 6891
        break;
    case 0xe0: /* loopnz */
    case 0xe1: /* loopz */
    case 0xe2: /* loop */
    case 0xe3: /* jecxz */
B
bellard 已提交
6892
        {
6893
            int l1, l2, l3;
B
bellard 已提交
6894

6895
            tval = (int8_t)insn_get(env, s, OT_BYTE);
B
bellard 已提交
6896 6897 6898 6899
            next_eip = s->pc - s->cs_base;
            tval += next_eip;
            if (s->dflag == 0)
                tval &= 0xffff;
6900

B
bellard 已提交
6901 6902
            l1 = gen_new_label();
            l2 = gen_new_label();
6903
            l3 = gen_new_label();
B
bellard 已提交
6904
            b &= 3;
6905 6906 6907 6908 6909
            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);
6910
                gen_jcc1(s, (JCC_Z << 1) | (b ^ 1), l1);
6911 6912 6913 6914 6915 6916 6917 6918 6919
                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 已提交
6920 6921
            }

6922
            gen_set_label(l3);
B
bellard 已提交
6923
            gen_jmp_im(next_eip);
6924
            tcg_gen_br(l2);
6925

B
bellard 已提交
6926 6927 6928 6929 6930
            gen_set_label(l1);
            gen_jmp_im(tval);
            gen_set_label(l2);
            gen_eob(s);
        }
B
bellard 已提交
6931 6932 6933 6934 6935 6936
        break;
    case 0x130: /* wrmsr */
    case 0x132: /* rdmsr */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
6937
            gen_update_cc_op(s);
B
bellard 已提交
6938
            gen_jmp_im(pc_start - s->cs_base);
T
ths 已提交
6939
            if (b & 2) {
B
Blue Swirl 已提交
6940
                gen_helper_rdmsr(cpu_env);
T
ths 已提交
6941
            } else {
B
Blue Swirl 已提交
6942
                gen_helper_wrmsr(cpu_env);
T
ths 已提交
6943
            }
B
bellard 已提交
6944 6945 6946
        }
        break;
    case 0x131: /* rdtsc */
6947
        gen_update_cc_op(s);
B
bellard 已提交
6948
        gen_jmp_im(pc_start - s->cs_base);
P
pbrook 已提交
6949 6950
        if (use_icount)
            gen_io_start();
B
Blue Swirl 已提交
6951
        gen_helper_rdtsc(cpu_env);
P
pbrook 已提交
6952 6953 6954 6955
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6956
        break;
6957
    case 0x133: /* rdpmc */
6958
        gen_update_cc_op(s);
6959
        gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6960
        gen_helper_rdpmc(cpu_env);
6961
        break;
6962
    case 0x134: /* sysenter */
6963
        /* For Intel SYSENTER is valid on 64-bit */
6964
        if (CODE64(s) && env->cpuid_vendor1 != CPUID_VENDOR_INTEL_1)
B
bellard 已提交
6965
            goto illegal_op;
6966 6967 6968
        if (!s->pe) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
J
Jun Koi 已提交
6969
            gen_update_cc_op(s);
B
bellard 已提交
6970
            gen_jmp_im(pc_start - s->cs_base);
6971
            gen_helper_sysenter(cpu_env);
6972 6973 6974 6975
            gen_eob(s);
        }
        break;
    case 0x135: /* sysexit */
6976
        /* For Intel SYSEXIT is valid on 64-bit */
6977
        if (CODE64(s) && env->cpuid_vendor1 != CPUID_VENDOR_INTEL_1)
B
bellard 已提交
6978
            goto illegal_op;
6979 6980 6981
        if (!s->pe) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
J
Jun Koi 已提交
6982
            gen_update_cc_op(s);
B
bellard 已提交
6983
            gen_jmp_im(pc_start - s->cs_base);
6984
            gen_helper_sysexit(cpu_env, tcg_const_i32(dflag));
6985 6986 6987
            gen_eob(s);
        }
        break;
B
bellard 已提交
6988 6989 6990
#ifdef TARGET_X86_64
    case 0x105: /* syscall */
        /* XXX: is it usable in real mode ? */
J
Jun Koi 已提交
6991
        gen_update_cc_op(s);
B
bellard 已提交
6992
        gen_jmp_im(pc_start - s->cs_base);
6993
        gen_helper_syscall(cpu_env, tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
6994 6995 6996 6997 6998 6999
        gen_eob(s);
        break;
    case 0x107: /* sysret */
        if (!s->pe) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
J
Jun Koi 已提交
7000
            gen_update_cc_op(s);
B
bellard 已提交
7001
            gen_jmp_im(pc_start - s->cs_base);
7002
            gen_helper_sysret(cpu_env, tcg_const_i32(s->dflag));
7003
            /* condition codes are modified only in long mode */
7004 7005 7006
            if (s->lma) {
                set_cc_op(s, CC_OP_EFLAGS);
            }
B
bellard 已提交
7007 7008 7009 7010
            gen_eob(s);
        }
        break;
#endif
B
bellard 已提交
7011
    case 0x1a2: /* cpuid */
7012
        gen_update_cc_op(s);
B
bellard 已提交
7013
        gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7014
        gen_helper_cpuid(cpu_env);
B
bellard 已提交
7015 7016 7017 7018 7019
        break;
    case 0xf4: /* hlt */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
7020
            gen_update_cc_op(s);
7021
            gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7022
            gen_helper_hlt(cpu_env, tcg_const_i32(s->pc - pc_start));
J
Jun Koi 已提交
7023
            s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
7024 7025 7026
        }
        break;
    case 0x100:
7027
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7028 7029 7030 7031
        mod = (modrm >> 6) & 3;
        op = (modrm >> 3) & 7;
        switch(op) {
        case 0: /* sldt */
7032 7033
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
7034
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_LDTR_READ);
B
bellard 已提交
7035
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,ldt.selector));
B
bellard 已提交
7036 7037 7038
            ot = OT_WORD;
            if (mod == 3)
                ot += s->dflag;
7039
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
7040 7041
            break;
        case 2: /* lldt */
7042 7043
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
7044 7045 7046
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
B
bellard 已提交
7047
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_LDTR_WRITE);
7048
                gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
B
bellard 已提交
7049
                gen_jmp_im(pc_start - s->cs_base);
7050
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
7051
                gen_helper_lldt(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
7052 7053 7054
            }
            break;
        case 1: /* str */
7055 7056
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
7057
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_TR_READ);
B
bellard 已提交
7058
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,tr.selector));
B
bellard 已提交
7059 7060 7061
            ot = OT_WORD;
            if (mod == 3)
                ot += s->dflag;
7062
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
7063 7064
            break;
        case 3: /* ltr */
7065 7066
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
7067 7068 7069
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
B
bellard 已提交
7070
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_TR_WRITE);
7071
                gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
B
bellard 已提交
7072
                gen_jmp_im(pc_start - s->cs_base);
7073
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
7074
                gen_helper_ltr(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
7075 7076 7077 7078
            }
            break;
        case 4: /* verr */
        case 5: /* verw */
7079 7080
            if (!s->pe || s->vm86)
                goto illegal_op;
7081
            gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
7082
            gen_update_cc_op(s);
7083 7084 7085 7086 7087
            if (op == 4) {
                gen_helper_verr(cpu_env, cpu_T[0]);
            } else {
                gen_helper_verw(cpu_env, cpu_T[0]);
            }
7088
            set_cc_op(s, CC_OP_EFLAGS);
7089
            break;
B
bellard 已提交
7090 7091 7092 7093 7094
        default:
            goto illegal_op;
        }
        break;
    case 0x101:
7095
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7096 7097
        mod = (modrm >> 6) & 3;
        op = (modrm >> 3) & 7;
B
bellard 已提交
7098
        rm = modrm & 7;
B
bellard 已提交
7099 7100 7101 7102
        switch(op) {
        case 0: /* sgdt */
            if (mod == 3)
                goto illegal_op;
B
bellard 已提交
7103
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_GDTR_READ);
7104
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
7105
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, gdt.limit));
B
bellard 已提交
7106
            gen_op_st_T0_A0(OT_WORD + s->mem_index);
7107
            gen_add_A0_im(s, 2);
B
bellard 已提交
7108
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, gdt.base));
B
bellard 已提交
7109 7110
            if (!s->dflag)
                gen_op_andl_T0_im(0xffffff);
B
bellard 已提交
7111
            gen_op_st_T0_A0(CODE64(s) + OT_LONG + s->mem_index);
B
bellard 已提交
7112
            break;
B
bellard 已提交
7113 7114 7115 7116 7117 7118 7119
        case 1:
            if (mod == 3) {
                switch (rm) {
                case 0: /* monitor */
                    if (!(s->cpuid_ext_features & CPUID_EXT_MONITOR) ||
                        s->cpl != 0)
                        goto illegal_op;
7120
                    gen_update_cc_op(s);
B
bellard 已提交
7121 7122 7123
                    gen_jmp_im(pc_start - s->cs_base);
#ifdef TARGET_X86_64
                    if (s->aflag == 2) {
7124
                        gen_op_movq_A0_reg(R_EAX);
7125
                    } else
B
bellard 已提交
7126 7127
#endif
                    {
7128
                        gen_op_movl_A0_reg(R_EAX);
B
bellard 已提交
7129 7130 7131 7132
                        if (s->aflag == 0)
                            gen_op_andl_A0_ffff();
                    }
                    gen_add_A0_ds_seg(s);
B
Blue Swirl 已提交
7133
                    gen_helper_monitor(cpu_env, cpu_A0);
B
bellard 已提交
7134 7135 7136 7137 7138
                    break;
                case 1: /* mwait */
                    if (!(s->cpuid_ext_features & CPUID_EXT_MONITOR) ||
                        s->cpl != 0)
                        goto illegal_op;
J
Jun Koi 已提交
7139
                    gen_update_cc_op(s);
7140
                    gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7141
                    gen_helper_mwait(cpu_env, tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
7142 7143
                    gen_eob(s);
                    break;
H
H. Peter Anvin 已提交
7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161
                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 已提交
7162 7163 7164 7165
                default:
                    goto illegal_op;
                }
            } else { /* sidt */
B
bellard 已提交
7166
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_IDTR_READ);
7167
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
7168
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, idt.limit));
B
bellard 已提交
7169
                gen_op_st_T0_A0(OT_WORD + s->mem_index);
B
bellard 已提交
7170
                gen_add_A0_im(s, 2);
B
bellard 已提交
7171
                tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, idt.base));
B
bellard 已提交
7172 7173
                if (!s->dflag)
                    gen_op_andl_T0_im(0xffffff);
B
bellard 已提交
7174
                gen_op_st_T0_A0(CODE64(s) + OT_LONG + s->mem_index);
B
bellard 已提交
7175 7176
            }
            break;
B
bellard 已提交
7177 7178
        case 2: /* lgdt */
        case 3: /* lidt */
T
ths 已提交
7179
            if (mod == 3) {
7180
                gen_update_cc_op(s);
B
bellard 已提交
7181
                gen_jmp_im(pc_start - s->cs_base);
T
ths 已提交
7182 7183
                switch(rm) {
                case 0: /* VMRUN */
B
bellard 已提交
7184 7185 7186 7187
                    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 已提交
7188
                        break;
B
bellard 已提交
7189
                    } else {
B
Blue Swirl 已提交
7190
                        gen_helper_vmrun(cpu_env, tcg_const_i32(s->aflag),
P
pbrook 已提交
7191
                                         tcg_const_i32(s->pc - pc_start));
7192
                        tcg_gen_exit_tb(0);
J
Jun Koi 已提交
7193
                        s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
7194
                    }
T
ths 已提交
7195 7196
                    break;
                case 1: /* VMMCALL */
B
bellard 已提交
7197 7198
                    if (!(s->flags & HF_SVME_MASK))
                        goto illegal_op;
B
Blue Swirl 已提交
7199
                    gen_helper_vmmcall(cpu_env);
T
ths 已提交
7200 7201
                    break;
                case 2: /* VMLOAD */
B
bellard 已提交
7202 7203 7204 7205 7206 7207
                    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 已提交
7208
                        gen_helper_vmload(cpu_env, tcg_const_i32(s->aflag));
B
bellard 已提交
7209
                    }
T
ths 已提交
7210 7211
                    break;
                case 3: /* VMSAVE */
B
bellard 已提交
7212 7213 7214 7215 7216 7217
                    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 已提交
7218
                        gen_helper_vmsave(cpu_env, tcg_const_i32(s->aflag));
B
bellard 已提交
7219
                    }
T
ths 已提交
7220 7221
                    break;
                case 4: /* STGI */
B
bellard 已提交
7222 7223 7224 7225 7226 7227 7228 7229
                    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 已提交
7230
                        gen_helper_stgi(cpu_env);
B
bellard 已提交
7231
                    }
T
ths 已提交
7232 7233
                    break;
                case 5: /* CLGI */
B
bellard 已提交
7234 7235 7236 7237 7238 7239
                    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 已提交
7240
                        gen_helper_clgi(cpu_env);
B
bellard 已提交
7241
                    }
T
ths 已提交
7242 7243
                    break;
                case 6: /* SKINIT */
B
bellard 已提交
7244 7245 7246 7247
                    if ((!(s->flags & HF_SVME_MASK) && 
                         !(s->cpuid_ext3_features & CPUID_EXT3_SKINIT)) || 
                        !s->pe)
                        goto illegal_op;
B
Blue Swirl 已提交
7248
                    gen_helper_skinit(cpu_env);
T
ths 已提交
7249 7250
                    break;
                case 7: /* INVLPGA */
B
bellard 已提交
7251 7252 7253 7254 7255 7256
                    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 已提交
7257
                        gen_helper_invlpga(cpu_env, tcg_const_i32(s->aflag));
B
bellard 已提交
7258
                    }
T
ths 已提交
7259 7260 7261 7262 7263
                    break;
                default:
                    goto illegal_op;
                }
            } else if (s->cpl != 0) {
B
bellard 已提交
7264 7265
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
B
bellard 已提交
7266 7267
                gen_svm_check_intercept(s, pc_start,
                                        op==2 ? SVM_EXIT_GDTR_WRITE : SVM_EXIT_IDTR_WRITE);
7268
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
7269
                gen_op_ld_T1_A0(OT_WORD + s->mem_index);
7270
                gen_add_A0_im(s, 2);
B
bellard 已提交
7271
                gen_op_ld_T0_A0(CODE64(s) + OT_LONG + s->mem_index);
B
bellard 已提交
7272 7273 7274
                if (!s->dflag)
                    gen_op_andl_T0_im(0xffffff);
                if (op == 2) {
B
bellard 已提交
7275 7276
                    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 已提交
7277
                } else {
B
bellard 已提交
7278 7279
                    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 已提交
7280 7281 7282 7283
                }
            }
            break;
        case 4: /* smsw */
B
bellard 已提交
7284
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_READ_CR0);
7285
#if defined TARGET_X86_64 && defined HOST_WORDS_BIGENDIAN
7286 7287
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,cr[0]) + 4);
#else
B
bellard 已提交
7288
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,cr[0]));
7289
#endif
7290
            gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 1);
B
bellard 已提交
7291 7292 7293 7294 7295
            break;
        case 6: /* lmsw */
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
B
bellard 已提交
7296
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_CR0);
7297
                gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
B
Blue Swirl 已提交
7298
                gen_helper_lmsw(cpu_env, cpu_T[0]);
B
bellard 已提交
7299
                gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
7300
                gen_eob(s);
B
bellard 已提交
7301 7302
            }
            break;
A
Andre Przywara 已提交
7303 7304 7305 7306 7307
        case 7:
            if (mod != 3) { /* invlpg */
                if (s->cpl != 0) {
                    gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
                } else {
7308
                    gen_update_cc_op(s);
A
Andre Przywara 已提交
7309
                    gen_jmp_im(pc_start - s->cs_base);
7310
                    gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
Blue Swirl 已提交
7311
                    gen_helper_invlpg(cpu_env, cpu_A0);
A
Andre Przywara 已提交
7312 7313 7314
                    gen_jmp_im(s->pc - s->cs_base);
                    gen_eob(s);
                }
B
bellard 已提交
7315
            } else {
A
Andre Przywara 已提交
7316 7317
                switch (rm) {
                case 0: /* swapgs */
B
bellard 已提交
7318
#ifdef TARGET_X86_64
A
Andre Przywara 已提交
7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331
                    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));
                        }
7332
                    } else
B
bellard 已提交
7333 7334 7335 7336
#endif
                    {
                        goto illegal_op;
                    }
A
Andre Przywara 已提交
7337 7338 7339 7340
                    break;
                case 1: /* rdtscp */
                    if (!(s->cpuid_ext2_features & CPUID_EXT2_RDTSCP))
                        goto illegal_op;
7341
                    gen_update_cc_op(s);
B
bellard 已提交
7342
                    gen_jmp_im(pc_start - s->cs_base);
A
Andre Przywara 已提交
7343 7344
                    if (use_icount)
                        gen_io_start();
B
Blue Swirl 已提交
7345
                    gen_helper_rdtscp(cpu_env);
A
Andre Przywara 已提交
7346 7347 7348 7349 7350 7351 7352
                    if (use_icount) {
                        gen_io_end();
                        gen_jmp(s, s->pc - s->cs_base);
                    }
                    break;
                default:
                    goto illegal_op;
B
bellard 已提交
7353
                }
B
bellard 已提交
7354 7355 7356 7357 7358 7359
            }
            break;
        default:
            goto illegal_op;
        }
        break;
7360 7361 7362 7363 7364
    case 0x108: /* invd */
    case 0x109: /* wbinvd */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
B
bellard 已提交
7365
            gen_svm_check_intercept(s, pc_start, (b & 2) ? SVM_EXIT_INVD : SVM_EXIT_WBINVD);
7366 7367 7368
            /* nothing to do */
        }
        break;
B
bellard 已提交
7369 7370 7371 7372 7373 7374 7375
    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;

7376
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7377 7378 7379
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
            rm = (modrm & 7) | REX_B(s);
7380

B
bellard 已提交
7381
            if (mod == 3) {
B
bellard 已提交
7382
                gen_op_mov_TN_reg(OT_LONG, 0, rm);
B
bellard 已提交
7383 7384
                /* sign extend */
                if (d_ot == OT_QUAD)
B
bellard 已提交
7385
                    tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
7386
                gen_op_mov_reg_T0(d_ot, reg);
B
bellard 已提交
7387
            } else {
7388
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
7389
                if (d_ot == OT_QUAD) {
B
bellard 已提交
7390
                    gen_op_lds_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
7391
                } else {
B
bellard 已提交
7392
                    gen_op_ld_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
7393
                }
B
bellard 已提交
7394
                gen_op_mov_reg_T0(d_ot, reg);
B
bellard 已提交
7395
            }
7396
        } else
B
bellard 已提交
7397 7398
#endif
        {
7399
            int label1;
L
Laurent Desnogues 已提交
7400
            TCGv t0, t1, t2, a0;
7401

B
bellard 已提交
7402 7403
            if (!s->pe || s->vm86)
                goto illegal_op;
P
pbrook 已提交
7404 7405 7406
            t0 = tcg_temp_local_new();
            t1 = tcg_temp_local_new();
            t2 = tcg_temp_local_new();
7407
            ot = OT_WORD;
7408
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7409 7410 7411 7412
            reg = (modrm >> 3) & 7;
            mod = (modrm >> 6) & 3;
            rm = modrm & 7;
            if (mod != 3) {
7413
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
7414
                gen_op_ld_v(ot + s->mem_index, t0, cpu_A0);
L
Laurent Desnogues 已提交
7415 7416
                a0 = tcg_temp_local_new();
                tcg_gen_mov_tl(a0, cpu_A0);
B
bellard 已提交
7417
            } else {
7418
                gen_op_mov_v_reg(ot, t0, rm);
L
Laurent Desnogues 已提交
7419
                TCGV_UNUSED(a0);
B
bellard 已提交
7420
            }
7421 7422 7423 7424
            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);
7425
            label1 = gen_new_label();
7426 7427 7428 7429
            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);
7430
            gen_set_label(label1);
B
bellard 已提交
7431
            if (mod != 3) {
L
Laurent Desnogues 已提交
7432 7433 7434
                gen_op_st_v(ot + s->mem_index, t0, a0);
                tcg_temp_free(a0);
           } else {
7435
                gen_op_mov_reg_v(ot, rm, t0);
B
bellard 已提交
7436
            }
7437
            gen_compute_eflags(s);
7438
            tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~CC_Z);
7439 7440 7441 7442
            tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, t2);
            tcg_temp_free(t0);
            tcg_temp_free(t1);
            tcg_temp_free(t2);
7443 7444
        }
        break;
B
bellard 已提交
7445 7446
    case 0x102: /* lar */
    case 0x103: /* lsl */
7447 7448
        {
            int label1;
7449
            TCGv t0;
7450 7451 7452
            if (!s->pe || s->vm86)
                goto illegal_op;
            ot = dflag ? OT_LONG : OT_WORD;
7453
            modrm = cpu_ldub_code(env, s->pc++);
7454
            reg = ((modrm >> 3) & 7) | rex_r;
7455
            gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
P
pbrook 已提交
7456
            t0 = tcg_temp_local_new();
7457
            gen_update_cc_op(s);
7458 7459 7460 7461 7462
            if (b == 0x102) {
                gen_helper_lar(t0, cpu_env, cpu_T[0]);
            } else {
                gen_helper_lsl(t0, cpu_env, cpu_T[0]);
            }
7463 7464
            tcg_gen_andi_tl(cpu_tmp0, cpu_cc_src, CC_Z);
            label1 = gen_new_label();
P
pbrook 已提交
7465
            tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, label1);
7466
            gen_op_mov_reg_v(ot, reg, t0);
7467
            gen_set_label(label1);
7468
            set_cc_op(s, CC_OP_EFLAGS);
7469
            tcg_temp_free(t0);
7470
        }
B
bellard 已提交
7471 7472
        break;
    case 0x118:
7473
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7474 7475 7476 7477 7478 7479 7480 7481 7482
        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;
7483
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
7484 7485
            /* nothing more to do */
            break;
B
bellard 已提交
7486
        default: /* nop (multi byte) */
7487
            gen_nop_modrm(env, s, modrm);
B
bellard 已提交
7488
            break;
B
bellard 已提交
7489 7490
        }
        break;
B
bellard 已提交
7491
    case 0x119 ... 0x11f: /* nop (multi byte) */
7492 7493
        modrm = cpu_ldub_code(env, s->pc++);
        gen_nop_modrm(env, s, modrm);
B
bellard 已提交
7494
        break;
B
bellard 已提交
7495 7496 7497 7498 7499
    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 {
7500
            modrm = cpu_ldub_code(env, s->pc++);
7501 7502 7503 7504 7505
            /* 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 已提交
7506 7507 7508 7509 7510 7511
            rm = (modrm & 7) | REX_B(s);
            reg = ((modrm >> 3) & 7) | rex_r;
            if (CODE64(s))
                ot = OT_QUAD;
            else
                ot = OT_LONG;
7512 7513 7514 7515
            if ((prefixes & PREFIX_LOCK) && (reg == 0) &&
                (s->cpuid_ext3_features & CPUID_EXT3_CR8LEG)) {
                reg = 8;
            }
B
bellard 已提交
7516 7517 7518 7519 7520
            switch(reg) {
            case 0:
            case 2:
            case 3:
            case 4:
B
bellard 已提交
7521
            case 8:
7522
                gen_update_cc_op(s);
B
bellard 已提交
7523
                gen_jmp_im(pc_start - s->cs_base);
B
bellard 已提交
7524
                if (b & 2) {
B
bellard 已提交
7525
                    gen_op_mov_TN_reg(ot, 0, rm);
B
Blue Swirl 已提交
7526 7527
                    gen_helper_write_crN(cpu_env, tcg_const_i32(reg),
                                         cpu_T[0]);
B
bellard 已提交
7528
                    gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
7529 7530
                    gen_eob(s);
                } else {
B
Blue Swirl 已提交
7531
                    gen_helper_read_crN(cpu_T[0], cpu_env, tcg_const_i32(reg));
B
bellard 已提交
7532
                    gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544
                }
                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 {
7545
            modrm = cpu_ldub_code(env, s->pc++);
7546 7547 7548 7549 7550
            /* 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 已提交
7551 7552 7553 7554 7555 7556
            rm = (modrm & 7) | REX_B(s);
            reg = ((modrm >> 3) & 7) | rex_r;
            if (CODE64(s))
                ot = OT_QUAD;
            else
                ot = OT_LONG;
B
bellard 已提交
7557
            /* XXX: do it dynamically with CR4.DE bit */
B
bellard 已提交
7558
            if (reg == 4 || reg == 5 || reg >= 8)
B
bellard 已提交
7559 7560
                goto illegal_op;
            if (b & 2) {
T
ths 已提交
7561
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_DR0 + reg);
B
bellard 已提交
7562
                gen_op_mov_TN_reg(ot, 0, rm);
B
Blue Swirl 已提交
7563
                gen_helper_movl_drN_T0(cpu_env, tcg_const_i32(reg), cpu_T[0]);
B
bellard 已提交
7564
                gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
7565 7566
                gen_eob(s);
            } else {
T
ths 已提交
7567
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_READ_DR0 + reg);
B
bellard 已提交
7568
                tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,dr[reg]));
B
bellard 已提交
7569
                gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
7570 7571 7572 7573 7574 7575 7576
            }
        }
        break;
    case 0x106: /* clts */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
T
ths 已提交
7577
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_CR0);
7578
            gen_helper_clts(cpu_env);
B
bellard 已提交
7579
            /* abort block because static cpu state changed */
B
bellard 已提交
7580
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
7581
            gen_eob(s);
B
bellard 已提交
7582 7583
        }
        break;
B
balrog 已提交
7584
    /* MMX/3DNow!/SSE/SSE2/SSE3/SSSE3/SSE4 support */
B
bellard 已提交
7585 7586
    case 0x1c3: /* MOVNTI reg, mem */
        if (!(s->cpuid_features & CPUID_SSE2))
B
bellard 已提交
7587
            goto illegal_op;
B
bellard 已提交
7588
        ot = s->dflag == 2 ? OT_QUAD : OT_LONG;
7589
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7590 7591 7592 7593 7594
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
        reg = ((modrm >> 3) & 7) | rex_r;
        /* generate a generic store */
7595
        gen_ldst_modrm(env, s, modrm, ot, reg, 1);
B
bellard 已提交
7596
        break;
B
bellard 已提交
7597
    case 0x1ae:
7598
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7599 7600 7601 7602
        mod = (modrm >> 6) & 3;
        op = (modrm >> 3) & 7;
        switch(op) {
        case 0: /* fxsave */
7603
            if (mod == 3 || !(s->cpuid_features & CPUID_FXSR) ||
7604
                (s->prefix & PREFIX_LOCK))
B
bellard 已提交
7605
                goto illegal_op;
7606
            if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
B
bellard 已提交
7607 7608 7609
                gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
                break;
            }
7610
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
7611
            gen_update_cc_op(s);
B
bellard 已提交
7612
            gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7613
            gen_helper_fxsave(cpu_env, cpu_A0, tcg_const_i32((s->dflag == 2)));
B
bellard 已提交
7614 7615
            break;
        case 1: /* fxrstor */
7616
            if (mod == 3 || !(s->cpuid_features & CPUID_FXSR) ||
7617
                (s->prefix & PREFIX_LOCK))
B
bellard 已提交
7618
                goto illegal_op;
7619
            if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
B
bellard 已提交
7620 7621 7622
                gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
                break;
            }
7623
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
7624
            gen_update_cc_op(s);
B
bellard 已提交
7625
            gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7626 7627
            gen_helper_fxrstor(cpu_env, cpu_A0,
                               tcg_const_i32((s->dflag == 2)));
B
bellard 已提交
7628 7629 7630 7631 7632 7633
            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 已提交
7634
            }
B
bellard 已提交
7635 7636
            if ((s->flags & HF_EM_MASK) || !(s->flags & HF_OSFXSR_MASK) ||
                mod == 3)
B
bellard 已提交
7637
                goto illegal_op;
7638
            gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
7639
            if (op == 2) {
B
bellard 已提交
7640
                gen_op_ld_T0_A0(OT_LONG + s->mem_index);
7641
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
7642
                gen_helper_ldmxcsr(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
7643
            } else {
B
bellard 已提交
7644
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, mxcsr));
B
bellard 已提交
7645
                gen_op_st_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
7646
            }
B
bellard 已提交
7647 7648 7649
            break;
        case 5: /* lfence */
        case 6: /* mfence */
7650
            if ((modrm & 0xc7) != 0xc0 || !(s->cpuid_features & CPUID_SSE2))
B
bellard 已提交
7651 7652
                goto illegal_op;
            break;
7653 7654 7655
        case 7: /* sfence / clflush */
            if ((modrm & 0xc7) == 0xc0) {
                /* sfence */
A
aurel32 已提交
7656
                /* XXX: also check for cpuid_ext2_features & CPUID_EXT2_EMMX */
7657 7658 7659 7660 7661 7662
                if (!(s->cpuid_features & CPUID_SSE))
                    goto illegal_op;
            } else {
                /* clflush */
                if (!(s->cpuid_features & CPUID_CLFLUSH))
                    goto illegal_op;
7663
                gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
7664 7665
            }
            break;
B
bellard 已提交
7666
        default:
B
bellard 已提交
7667 7668 7669
            goto illegal_op;
        }
        break;
A
aurel32 已提交
7670
    case 0x10d: /* 3DNow! prefetch(w) */
7671
        modrm = cpu_ldub_code(env, s->pc++);
A
aurel32 已提交
7672 7673 7674
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
7675
        gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
7676 7677
        /* ignore for now */
        break;
B
bellard 已提交
7678
    case 0x1aa: /* rsm */
B
bellard 已提交
7679
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_RSM);
B
bellard 已提交
7680 7681
        if (!(s->flags & HF_SMM_MASK))
            goto illegal_op;
J
Jun Koi 已提交
7682
        gen_update_cc_op(s);
B
bellard 已提交
7683
        gen_jmp_im(s->pc - s->cs_base);
B
Blue Swirl 已提交
7684
        gen_helper_rsm(cpu_env);
B
bellard 已提交
7685 7686
        gen_eob(s);
        break;
B
balrog 已提交
7687 7688 7689 7690 7691 7692 7693
    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;

7694
        modrm = cpu_ldub_code(env, s->pc++);
M
malc 已提交
7695
        reg = ((modrm >> 3) & 7) | rex_r;
B
balrog 已提交
7696 7697 7698 7699 7700 7701 7702 7703

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

7704
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
Blue Swirl 已提交
7705
        gen_helper_popcnt(cpu_T[0], cpu_env, cpu_T[0], tcg_const_i32(ot));
B
balrog 已提交
7706
        gen_op_mov_reg_T0(ot, reg);
B
balrog 已提交
7707

7708
        set_cc_op(s, CC_OP_EFLAGS);
B
balrog 已提交
7709
        break;
A
aurel32 已提交
7710 7711 7712
    case 0x10e ... 0x10f:
        /* 3DNow! instructions, ignore prefixes */
        s->prefix &= ~(PREFIX_REPZ | PREFIX_REPNZ | PREFIX_DATA);
B
bellard 已提交
7713 7714
    case 0x110 ... 0x117:
    case 0x128 ... 0x12f:
B
balrog 已提交
7715
    case 0x138 ... 0x13a:
7716
    case 0x150 ... 0x179:
B
bellard 已提交
7717 7718 7719 7720
    case 0x17c ... 0x17f:
    case 0x1c2:
    case 0x1c4 ... 0x1c6:
    case 0x1d0 ... 0x1fe:
7721
        gen_sse(env, s, b, pc_start, rex_r);
B
bellard 已提交
7722
        break;
B
bellard 已提交
7723 7724 7725 7726 7727
    default:
        goto illegal_op;
    }
    /* lock generation */
    if (s->prefix & PREFIX_LOCK)
P
pbrook 已提交
7728
        gen_helper_unlock();
B
bellard 已提交
7729 7730
    return s->pc;
 illegal_op:
7731
    if (s->prefix & PREFIX_LOCK)
P
pbrook 已提交
7732
        gen_helper_unlock();
B
bellard 已提交
7733 7734 7735 7736 7737 7738 7739
    /* 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 已提交
7740 7741
    cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
    cpu_cc_op = tcg_global_mem_new_i32(TCG_AREG0,
7742 7743
                                       offsetof(CPUX86State, cc_op), "cc_op");
    cpu_cc_src = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_src),
P
pbrook 已提交
7744
                                    "cc_src");
7745
    cpu_cc_dst = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_dst),
P
pbrook 已提交
7746
                                    "cc_dst");
7747

7748 7749
#ifdef TARGET_X86_64
    cpu_regs[R_EAX] = tcg_global_mem_new_i64(TCG_AREG0,
7750
                                             offsetof(CPUX86State, regs[R_EAX]), "rax");
7751
    cpu_regs[R_ECX] = tcg_global_mem_new_i64(TCG_AREG0,
7752
                                             offsetof(CPUX86State, regs[R_ECX]), "rcx");
7753
    cpu_regs[R_EDX] = tcg_global_mem_new_i64(TCG_AREG0,
7754
                                             offsetof(CPUX86State, regs[R_EDX]), "rdx");
7755
    cpu_regs[R_EBX] = tcg_global_mem_new_i64(TCG_AREG0,
7756
                                             offsetof(CPUX86State, regs[R_EBX]), "rbx");
7757
    cpu_regs[R_ESP] = tcg_global_mem_new_i64(TCG_AREG0,
7758
                                             offsetof(CPUX86State, regs[R_ESP]), "rsp");
7759
    cpu_regs[R_EBP] = tcg_global_mem_new_i64(TCG_AREG0,
7760
                                             offsetof(CPUX86State, regs[R_EBP]), "rbp");
7761
    cpu_regs[R_ESI] = tcg_global_mem_new_i64(TCG_AREG0,
7762
                                             offsetof(CPUX86State, regs[R_ESI]), "rsi");
7763
    cpu_regs[R_EDI] = tcg_global_mem_new_i64(TCG_AREG0,
7764
                                             offsetof(CPUX86State, regs[R_EDI]), "rdi");
7765
    cpu_regs[8] = tcg_global_mem_new_i64(TCG_AREG0,
7766
                                         offsetof(CPUX86State, regs[8]), "r8");
7767
    cpu_regs[9] = tcg_global_mem_new_i64(TCG_AREG0,
7768
                                          offsetof(CPUX86State, regs[9]), "r9");
7769
    cpu_regs[10] = tcg_global_mem_new_i64(TCG_AREG0,
7770
                                          offsetof(CPUX86State, regs[10]), "r10");
7771
    cpu_regs[11] = tcg_global_mem_new_i64(TCG_AREG0,
7772
                                          offsetof(CPUX86State, regs[11]), "r11");
7773
    cpu_regs[12] = tcg_global_mem_new_i64(TCG_AREG0,
7774
                                          offsetof(CPUX86State, regs[12]), "r12");
7775
    cpu_regs[13] = tcg_global_mem_new_i64(TCG_AREG0,
7776
                                          offsetof(CPUX86State, regs[13]), "r13");
7777
    cpu_regs[14] = tcg_global_mem_new_i64(TCG_AREG0,
7778
                                          offsetof(CPUX86State, regs[14]), "r14");
7779
    cpu_regs[15] = tcg_global_mem_new_i64(TCG_AREG0,
7780
                                          offsetof(CPUX86State, regs[15]), "r15");
7781 7782
#else
    cpu_regs[R_EAX] = tcg_global_mem_new_i32(TCG_AREG0,
7783
                                             offsetof(CPUX86State, regs[R_EAX]), "eax");
7784
    cpu_regs[R_ECX] = tcg_global_mem_new_i32(TCG_AREG0,
7785
                                             offsetof(CPUX86State, regs[R_ECX]), "ecx");
7786
    cpu_regs[R_EDX] = tcg_global_mem_new_i32(TCG_AREG0,
7787
                                             offsetof(CPUX86State, regs[R_EDX]), "edx");
7788
    cpu_regs[R_EBX] = tcg_global_mem_new_i32(TCG_AREG0,
7789
                                             offsetof(CPUX86State, regs[R_EBX]), "ebx");
7790
    cpu_regs[R_ESP] = tcg_global_mem_new_i32(TCG_AREG0,
7791
                                             offsetof(CPUX86State, regs[R_ESP]), "esp");
7792
    cpu_regs[R_EBP] = tcg_global_mem_new_i32(TCG_AREG0,
7793
                                             offsetof(CPUX86State, regs[R_EBP]), "ebp");
7794
    cpu_regs[R_ESI] = tcg_global_mem_new_i32(TCG_AREG0,
7795
                                             offsetof(CPUX86State, regs[R_ESI]), "esi");
7796
    cpu_regs[R_EDI] = tcg_global_mem_new_i32(TCG_AREG0,
7797
                                             offsetof(CPUX86State, regs[R_EDI]), "edi");
7798 7799
#endif

7800
    /* register helpers */
P
pbrook 已提交
7801
#define GEN_HELPER 2
7802
#include "helper.h"
B
bellard 已提交
7803 7804 7805 7806 7807
}

/* 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. */
7808
static inline void gen_intermediate_code_internal(CPUX86State *env,
7809 7810
                                                  TranslationBlock *tb,
                                                  int search_pc)
B
bellard 已提交
7811 7812
{
    DisasContext dc1, *dc = &dc1;
B
bellard 已提交
7813
    target_ulong pc_ptr;
B
bellard 已提交
7814
    uint16_t *gen_opc_end;
7815
    CPUBreakpoint *bp;
7816
    int j, lj;
7817
    uint64_t flags;
B
bellard 已提交
7818 7819
    target_ulong pc_start;
    target_ulong cs_base;
P
pbrook 已提交
7820 7821
    int num_insns;
    int max_insns;
7822

B
bellard 已提交
7823
    /* generate intermediate code */
B
bellard 已提交
7824 7825
    pc_start = tb->pc;
    cs_base = tb->cs_base;
B
bellard 已提交
7826
    flags = tb->flags;
B
bellard 已提交
7827

7828
    dc->pe = (flags >> HF_PE_SHIFT) & 1;
B
bellard 已提交
7829 7830 7831 7832 7833 7834 7835 7836
    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;
7837
    dc->singlestep_enabled = env->singlestep_enabled;
B
bellard 已提交
7838
    dc->cc_op = CC_OP_DYNAMIC;
7839
    dc->cc_op_dirty = false;
B
bellard 已提交
7840 7841 7842 7843 7844 7845
    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 已提交
7846
        dc->mem_index = (cpu_mmu_index(env) + 1) << 2;
B
bellard 已提交
7847
    }
B
bellard 已提交
7848
    dc->cpuid_features = env->cpuid_features;
B
bellard 已提交
7849
    dc->cpuid_ext_features = env->cpuid_ext_features;
7850
    dc->cpuid_ext2_features = env->cpuid_ext2_features;
B
bellard 已提交
7851
    dc->cpuid_ext3_features = env->cpuid_ext3_features;
H
H. Peter Anvin 已提交
7852
    dc->cpuid_7_0_ebx_features = env->cpuid_7_0_ebx_features;
B
bellard 已提交
7853 7854 7855 7856
#ifdef TARGET_X86_64
    dc->lma = (flags >> HF_LMA_SHIFT) & 1;
    dc->code64 = (flags >> HF_CS64_SHIFT) & 1;
#endif
B
bellard 已提交
7857
    dc->flags = flags;
7858 7859
    dc->jmp_opt = !(dc->tf || env->singlestep_enabled ||
                    (flags & HF_INHIBIT_IRQ_MASK)
B
bellard 已提交
7860
#ifndef CONFIG_SOFTMMU
B
bellard 已提交
7861 7862 7863
                    || (flags & HF_SOFTMMU_MASK)
#endif
                    );
7864 7865
#if 0
    /* check addseg logic */
B
bellard 已提交
7866
    if (!dc->addseg && (dc->vm86 || !dc->pe || !dc->code32))
7867 7868 7869
        printf("ERROR addseg\n");
#endif

P
pbrook 已提交
7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882
    cpu_T[0] = tcg_temp_new();
    cpu_T[1] = tcg_temp_new();
    cpu_A0 = tcg_temp_new();
    cpu_T3 = tcg_temp_new();

    cpu_tmp0 = tcg_temp_new();
    cpu_tmp1_i64 = tcg_temp_new_i64();
    cpu_tmp2_i32 = tcg_temp_new_i32();
    cpu_tmp3_i32 = tcg_temp_new_i32();
    cpu_tmp4 = tcg_temp_new();
    cpu_tmp5 = tcg_temp_new();
    cpu_ptr0 = tcg_temp_new_ptr();
    cpu_ptr1 = tcg_temp_new_ptr();
B
bellard 已提交
7883

7884
    gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
B
bellard 已提交
7885 7886 7887 7888

    dc->is_jmp = DISAS_NEXT;
    pc_ptr = pc_start;
    lj = -1;
P
pbrook 已提交
7889 7890 7891 7892
    num_insns = 0;
    max_insns = tb->cflags & CF_COUNT_MASK;
    if (max_insns == 0)
        max_insns = CF_COUNT_MASK;
B
bellard 已提交
7893

P
pbrook 已提交
7894
    gen_icount_start();
B
bellard 已提交
7895
    for(;;) {
B
Blue Swirl 已提交
7896 7897
        if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
            QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
J
Jan Kiszka 已提交
7898 7899
                if (bp->pc == pc_ptr &&
                    !((bp->flags & BP_CPU) && (tb->flags & HF_RF_MASK))) {
B
bellard 已提交
7900 7901 7902 7903 7904 7905
                    gen_debug(dc, pc_ptr - dc->cs_base);
                    break;
                }
            }
        }
        if (search_pc) {
7906
            j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
B
bellard 已提交
7907 7908 7909
            if (lj < j) {
                lj++;
                while (lj < j)
7910
                    tcg_ctx.gen_opc_instr_start[lj++] = 0;
B
bellard 已提交
7911
            }
7912
            tcg_ctx.gen_opc_pc[lj] = pc_ptr;
B
bellard 已提交
7913
            gen_opc_cc_op[lj] = dc->cc_op;
7914
            tcg_ctx.gen_opc_instr_start[lj] = 1;
7915
            tcg_ctx.gen_opc_icount[lj] = num_insns;
B
bellard 已提交
7916
        }
P
pbrook 已提交
7917 7918 7919
        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
            gen_io_start();

7920
        pc_ptr = disas_insn(env, dc, pc_ptr);
P
pbrook 已提交
7921
        num_insns++;
B
bellard 已提交
7922 7923 7924 7925 7926
        /* stop translation if indicated */
        if (dc->is_jmp)
            break;
        /* if single step mode, we generate only one instruction and
           generate an exception */
7927 7928 7929
        /* 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 */
7930
        if (dc->tf || dc->singlestep_enabled ||
P
pbrook 已提交
7931
            (flags & HF_INHIBIT_IRQ_MASK)) {
B
bellard 已提交
7932
            gen_jmp_im(pc_ptr - dc->cs_base);
B
bellard 已提交
7933 7934 7935 7936
            gen_eob(dc);
            break;
        }
        /* if too long translation, stop generation too */
7937
        if (tcg_ctx.gen_opc_ptr >= gen_opc_end ||
P
pbrook 已提交
7938 7939
            (pc_ptr - pc_start) >= (TARGET_PAGE_SIZE - 32) ||
            num_insns >= max_insns) {
B
bellard 已提交
7940
            gen_jmp_im(pc_ptr - dc->cs_base);
B
bellard 已提交
7941 7942 7943
            gen_eob(dc);
            break;
        }
7944 7945 7946 7947 7948
        if (singlestep) {
            gen_jmp_im(pc_ptr - dc->cs_base);
            gen_eob(dc);
            break;
        }
B
bellard 已提交
7949
    }
P
pbrook 已提交
7950 7951 7952
    if (tb->cflags & CF_LAST_IO)
        gen_io_end();
    gen_icount_end(tb, num_insns);
7953
    *tcg_ctx.gen_opc_ptr = INDEX_op_end;
B
bellard 已提交
7954 7955
    /* we don't forget to fill the last values */
    if (search_pc) {
7956
        j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
B
bellard 已提交
7957 7958
        lj++;
        while (lj <= j)
7959
            tcg_ctx.gen_opc_instr_start[lj++] = 0;
B
bellard 已提交
7960
    }
7961

B
bellard 已提交
7962
#ifdef DEBUG_DISAS
7963
    if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
B
bellard 已提交
7964
        int disas_flags;
7965 7966
        qemu_log("----------------\n");
        qemu_log("IN: %s\n", lookup_symbol(pc_start));
B
bellard 已提交
7967 7968 7969 7970 7971 7972
#ifdef TARGET_X86_64
        if (dc->code64)
            disas_flags = 2;
        else
#endif
            disas_flags = !dc->code32;
B
Blue Swirl 已提交
7973
        log_target_disas(env, pc_start, pc_ptr - pc_start, disas_flags);
7974
        qemu_log("\n");
B
bellard 已提交
7975 7976 7977
    }
#endif

P
pbrook 已提交
7978
    if (!search_pc) {
B
bellard 已提交
7979
        tb->size = pc_ptr - pc_start;
P
pbrook 已提交
7980 7981
        tb->icount = num_insns;
    }
B
bellard 已提交
7982 7983
}

7984
void gen_intermediate_code(CPUX86State *env, TranslationBlock *tb)
B
bellard 已提交
7985
{
7986
    gen_intermediate_code_internal(env, tb, 0);
B
bellard 已提交
7987 7988
}

7989
void gen_intermediate_code_pc(CPUX86State *env, TranslationBlock *tb)
B
bellard 已提交
7990
{
7991
    gen_intermediate_code_internal(env, tb, 1);
B
bellard 已提交
7992 7993
}

7994
void restore_state_to_opc(CPUX86State *env, TranslationBlock *tb, int pc_pos)
A
aurel32 已提交
7995 7996 7997
{
    int cc_op;
#ifdef DEBUG_DISAS
7998
    if (qemu_loglevel_mask(CPU_LOG_TB_OP)) {
A
aurel32 已提交
7999
        int i;
8000
        qemu_log("RESTORE:\n");
A
aurel32 已提交
8001
        for(i = 0;i <= pc_pos; i++) {
8002
            if (tcg_ctx.gen_opc_instr_start[i]) {
8003 8004
                qemu_log("0x%04x: " TARGET_FMT_lx "\n", i,
                        tcg_ctx.gen_opc_pc[i]);
A
aurel32 已提交
8005 8006
            }
        }
8007
        qemu_log("pc_pos=0x%x eip=" TARGET_FMT_lx " cs_base=%x\n",
8008
                pc_pos, tcg_ctx.gen_opc_pc[pc_pos] - tb->cs_base,
A
aurel32 已提交
8009 8010 8011
                (uint32_t)tb->cs_base);
    }
#endif
8012
    env->eip = tcg_ctx.gen_opc_pc[pc_pos] - tb->cs_base;
A
aurel32 已提交
8013 8014 8015 8016
    cc_op = gen_opc_cc_op[pc_pos];
    if (cc_op != CC_OP_DYNAMIC)
        env->cc_op = cc_op;
}