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

#include "cpu.h"
#include "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 54 55
static TCGv_ptr cpu_env;
static TCGv cpu_A0, cpu_cc_src, cpu_cc_dst, cpu_cc_tmp;
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];

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

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 92 93 94 95 96 97 98
    int ss32;   /* 32 bit stack segment */
    int cc_op;  /* current CC operation */
    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 */
99
    int singlestep_enabled; /* "hardware" single step enabled */
B
bellard 已提交
100 101
    int jmp_opt; /* use direct block chaining for direct jumps */
    int mem_index; /* select memory access functions */
102
    uint64_t flags; /* all execution flags */
B
bellard 已提交
103 104
    struct TranslationBlock *tb;
    int popl_esp_hack; /* for correct popl with esp base handling */
B
bellard 已提交
105 106
    int rip_offset; /* only used in x86_64, but left for simplicity */
    int cpuid_features;
B
bellard 已提交
107
    int cpuid_ext_features;
108
    int cpuid_ext2_features;
B
bellard 已提交
109
    int cpuid_ext3_features;
B
bellard 已提交
110 111 112
} DisasContext;

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

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

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

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

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

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

B
bellard 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
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 已提交
242 243 244 245 246 247 248 249 250 251
#ifdef TARGET_X86_64

#define NB_OP_SIZES 4

#else /* !TARGET_X86_64 */

#define NB_OP_SIZES 3

#endif /* !TARGET_X86_64 */

252
#if defined(HOST_WORDS_BIGENDIAN)
B
bellard 已提交
253 254 255 256 257
#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 已提交
258
#else
B
bellard 已提交
259 260 261 262 263
#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 已提交
264
#endif
B
bellard 已提交
265

266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
/* 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;
}

285
static inline void gen_op_mov_reg_v(int ot, int reg, TCGv t0)
B
bellard 已提交
286 287 288
{
    switch(ot) {
    case OT_BYTE:
289
        if (!byte_reg_is_xH(reg)) {
290
            tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], t0, 0, 8);
B
bellard 已提交
291
        } else {
292
            tcg_gen_deposit_tl(cpu_regs[reg - 4], cpu_regs[reg - 4], t0, 8, 8);
B
bellard 已提交
293 294 295
        }
        break;
    case OT_WORD:
296
        tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], t0, 0, 16);
B
bellard 已提交
297
        break;
298
    default: /* XXX this shouldn't be reached;  abort? */
B
bellard 已提交
299
    case OT_LONG:
300 301 302
        /* 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 已提交
303
        break;
304
#ifdef TARGET_X86_64
B
bellard 已提交
305
    case OT_QUAD:
306
        tcg_gen_mov_tl(cpu_regs[reg], t0);
B
bellard 已提交
307
        break;
B
bellard 已提交
308
#endif
B
bellard 已提交
309 310
    }
}
B
bellard 已提交
311

B
bellard 已提交
312 313
static inline void gen_op_mov_reg_T0(int ot, int reg)
{
314
    gen_op_mov_reg_v(ot, reg, cpu_T[0]);
B
bellard 已提交
315 316 317 318
}

static inline void gen_op_mov_reg_T1(int ot, int reg)
{
319
    gen_op_mov_reg_v(ot, reg, cpu_T[1]);
B
bellard 已提交
320 321 322 323 324 325
}

static inline void gen_op_mov_reg_A0(int size, int reg)
{
    switch(size) {
    case 0:
326
        tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], cpu_A0, 0, 16);
B
bellard 已提交
327
        break;
328
    default: /* XXX this shouldn't be reached;  abort? */
B
bellard 已提交
329
    case 1:
330 331 332
        /* 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 已提交
333
        break;
334
#ifdef TARGET_X86_64
B
bellard 已提交
335
    case 2:
336
        tcg_gen_mov_tl(cpu_regs[reg], cpu_A0);
B
bellard 已提交
337
        break;
B
bellard 已提交
338
#endif
B
bellard 已提交
339 340 341
    }
}

342
static inline void gen_op_mov_v_reg(int ot, TCGv t0, int reg)
B
bellard 已提交
343
{
344 345 346 347
    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 {
348
        tcg_gen_mov_tl(t0, cpu_regs[reg]);
B
bellard 已提交
349 350 351
    }
}

352 353 354 355 356
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 已提交
357 358
static inline void gen_op_movl_A0_reg(int reg)
{
359
    tcg_gen_mov_tl(cpu_A0, cpu_regs[reg]);
B
bellard 已提交
360 361 362 363 364
}

static inline void gen_op_addl_A0_im(int32_t val)
{
    tcg_gen_addi_tl(cpu_A0, cpu_A0, val);
B
bellard 已提交
365
#ifdef TARGET_X86_64
B
bellard 已提交
366
    tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffffffff);
B
bellard 已提交
367
#endif
B
bellard 已提交
368
}
B
bellard 已提交
369

B
bellard 已提交
370
#ifdef TARGET_X86_64
B
bellard 已提交
371 372 373 374
static inline void gen_op_addq_A0_im(int64_t val)
{
    tcg_gen_addi_tl(cpu_A0, cpu_A0, val);
}
B
bellard 已提交
375
#endif
B
bellard 已提交
376 377 378 379 380 381 382 383 384 385
    
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 已提交
386

B
bellard 已提交
387
static inline void gen_op_addl_T0_T1(void)
B
bellard 已提交
388
{
B
bellard 已提交
389 390 391 392 393
    tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
}

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

397
static inline void gen_op_add_reg_im(int size, int reg, int32_t val)
B
bellard 已提交
398
{
399 400
    switch(size) {
    case 0:
401
        tcg_gen_addi_tl(cpu_tmp0, cpu_regs[reg], val);
402
        tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], cpu_tmp0, 0, 16);
403 404
        break;
    case 1:
405 406 407 408 409
        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);
410 411 412
        break;
#ifdef TARGET_X86_64
    case 2:
413
        tcg_gen_addi_tl(cpu_regs[reg], cpu_regs[reg], val);
414 415 416
        break;
#endif
    }
B
bellard 已提交
417 418
}

419
static inline void gen_op_add_reg_T0(int size, int reg)
B
bellard 已提交
420
{
421 422
    switch(size) {
    case 0:
423
        tcg_gen_add_tl(cpu_tmp0, cpu_regs[reg], cpu_T[0]);
424
        tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], cpu_tmp0, 0, 16);
425 426
        break;
    case 1:
427 428 429 430 431
        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);
432
        break;
B
bellard 已提交
433
#ifdef TARGET_X86_64
434
    case 2:
435
        tcg_gen_add_tl(cpu_regs[reg], cpu_regs[reg], cpu_T[0]);
436
        break;
B
bellard 已提交
437
#endif
438 439
    }
}
B
bellard 已提交
440 441 442

static inline void gen_op_set_cc_op(int32_t val)
{
443
    tcg_gen_movi_i32(cpu_cc_op, val);
B
bellard 已提交
444 445 446 447
}

static inline void gen_op_addl_A0_reg_sN(int shift, int reg)
{
448 449
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[reg]);
    if (shift != 0)
B
bellard 已提交
450 451
        tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, shift);
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
452 453 454
    /* 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 已提交
455
}
B
bellard 已提交
456

B
bellard 已提交
457 458
static inline void gen_op_movl_A0_seg(int reg)
{
459
    tcg_gen_ld32u_tl(cpu_A0, cpu_env, offsetof(CPUX86State, segs[reg].base) + REG_L_OFFSET);
B
bellard 已提交
460
}
B
bellard 已提交
461

462
static inline void gen_op_addl_A0_seg(DisasContext *s, int reg)
B
bellard 已提交
463
{
464
    tcg_gen_ld_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, segs[reg].base));
B
bellard 已提交
465
#ifdef TARGET_X86_64
466 467 468 469 470 471 472 473 474
    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 已提交
475 476
#endif
}
B
bellard 已提交
477

B
bellard 已提交
478
#ifdef TARGET_X86_64
B
bellard 已提交
479 480
static inline void gen_op_movq_A0_seg(int reg)
{
481
    tcg_gen_ld_tl(cpu_A0, cpu_env, offsetof(CPUX86State, segs[reg].base));
B
bellard 已提交
482
}
B
bellard 已提交
483

B
bellard 已提交
484 485
static inline void gen_op_addq_A0_seg(int reg)
{
486
    tcg_gen_ld_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, segs[reg].base));
B
bellard 已提交
487 488 489 490 491
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
}

static inline void gen_op_movq_A0_reg(int reg)
{
492
    tcg_gen_mov_tl(cpu_A0, cpu_regs[reg]);
B
bellard 已提交
493 494 495 496
}

static inline void gen_op_addq_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 501
        tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, shift);
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
}
B
bellard 已提交
502 503
#endif

B
bellard 已提交
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
static inline void gen_op_lds_T0_A0(int idx)
{
    int mem_index = (idx >> 2) - 1;
    switch(idx & 3) {
    case 0:
        tcg_gen_qemu_ld8s(cpu_T[0], cpu_A0, mem_index);
        break;
    case 1:
        tcg_gen_qemu_ld16s(cpu_T[0], cpu_A0, mem_index);
        break;
    default:
    case 2:
        tcg_gen_qemu_ld32s(cpu_T[0], cpu_A0, mem_index);
        break;
    }
}
B
bellard 已提交
520

521
static inline void gen_op_ld_v(int idx, TCGv t0, TCGv a0)
B
bellard 已提交
522 523 524 525
{
    int mem_index = (idx >> 2) - 1;
    switch(idx & 3) {
    case 0:
526
        tcg_gen_qemu_ld8u(t0, a0, mem_index);
B
bellard 已提交
527 528
        break;
    case 1:
529
        tcg_gen_qemu_ld16u(t0, a0, mem_index);
B
bellard 已提交
530 531
        break;
    case 2:
532
        tcg_gen_qemu_ld32u(t0, a0, mem_index);
B
bellard 已提交
533 534 535
        break;
    default:
    case 3:
P
pbrook 已提交
536 537
        /* Should never happen on 32-bit targets.  */
#ifdef TARGET_X86_64
538
        tcg_gen_qemu_ld64(t0, a0, mem_index);
P
pbrook 已提交
539
#endif
B
bellard 已提交
540 541 542
        break;
    }
}
B
bellard 已提交
543

544 545 546 547 548 549
/* 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 已提交
550 551
static inline void gen_op_ldu_T0_A0(int idx)
{
552
    gen_op_ld_v(idx, cpu_T[0], cpu_A0);
B
bellard 已提交
553
}
B
bellard 已提交
554

B
bellard 已提交
555
static inline void gen_op_ld_T1_A0(int idx)
556 557 558 559 560
{
    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 已提交
561 562 563 564
{
    int mem_index = (idx >> 2) - 1;
    switch(idx & 3) {
    case 0:
565
        tcg_gen_qemu_st8(t0, a0, mem_index);
B
bellard 已提交
566 567
        break;
    case 1:
568
        tcg_gen_qemu_st16(t0, a0, mem_index);
B
bellard 已提交
569 570
        break;
    case 2:
571
        tcg_gen_qemu_st32(t0, a0, mem_index);
B
bellard 已提交
572 573 574
        break;
    default:
    case 3:
P
pbrook 已提交
575 576
        /* Should never happen on 32-bit targets.  */
#ifdef TARGET_X86_64
577
        tcg_gen_qemu_st64(t0, a0, mem_index);
P
pbrook 已提交
578
#endif
B
bellard 已提交
579 580 581
        break;
    }
}
582

B
bellard 已提交
583 584
static inline void gen_op_st_T0_A0(int idx)
{
585
    gen_op_st_v(idx, cpu_T[0], cpu_A0);
B
bellard 已提交
586
}
587

B
bellard 已提交
588 589
static inline void gen_op_st_T1_A0(int idx)
{
590
    gen_op_st_v(idx, cpu_T[1], cpu_A0);
B
bellard 已提交
591
}
592

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

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

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

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

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

661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
static void gen_extu(int ot, TCGv reg)
{
    switch(ot) {
    case OT_BYTE:
        tcg_gen_ext8u_tl(reg, reg);
        break;
    case OT_WORD:
        tcg_gen_ext16u_tl(reg, reg);
        break;
    case OT_LONG:
        tcg_gen_ext32u_tl(reg, reg);
        break;
    default:
        break;
    }
}
677

678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
static void gen_exts(int ot, TCGv reg)
{
    switch(ot) {
    case OT_BYTE:
        tcg_gen_ext8s_tl(reg, reg);
        break;
    case OT_WORD:
        tcg_gen_ext16s_tl(reg, reg);
        break;
    case OT_LONG:
        tcg_gen_ext32s_tl(reg, reg);
        break;
    default:
        break;
    }
}
B
bellard 已提交
694

695 696
static inline void gen_op_jnz_ecx(int size, int label1)
{
697
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
698
    gen_extu(size + 1, cpu_tmp0);
P
pbrook 已提交
699
    tcg_gen_brcondi_tl(TCG_COND_NE, cpu_tmp0, 0, label1);
700 701 702 703
}

static inline void gen_op_jz_ecx(int size, int label1)
{
704
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
705
    gen_extu(size + 1, cpu_tmp0);
P
pbrook 已提交
706
    tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, label1);
707
}
B
bellard 已提交
708

P
pbrook 已提交
709 710 711 712 713 714 715
static void gen_helper_in_func(int ot, TCGv v, TCGv_i32 n)
{
    switch (ot) {
    case 0: gen_helper_inb(v, n); break;
    case 1: gen_helper_inw(v, n); break;
    case 2: gen_helper_inl(v, n); break;
    }
B
bellard 已提交
716

P
pbrook 已提交
717
}
B
bellard 已提交
718

P
pbrook 已提交
719 720 721 722 723 724 725 726 727
static void gen_helper_out_func(int ot, TCGv_i32 v, TCGv_i32 n)
{
    switch (ot) {
    case 0: gen_helper_outb(v, n); break;
    case 1: gen_helper_outw(v, n); break;
    case 2: gen_helper_outl(v, n); break;
    }

}
728

729 730
static void gen_check_io(DisasContext *s, int ot, target_ulong cur_eip,
                         uint32_t svm_flags)
731
{
732 733 734 735
    int state_saved;
    target_ulong next_eip;

    state_saved = 0;
736 737 738
    if (s->pe && (s->cpl > s->iopl || s->vm86)) {
        if (s->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s->cc_op);
B
bellard 已提交
739
        gen_jmp_im(cur_eip);
740
        state_saved = 1;
741
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
742 743 744 745 746
        switch (ot) {
        case 0: gen_helper_check_iob(cpu_tmp2_i32); break;
        case 1: gen_helper_check_iow(cpu_tmp2_i32); break;
        case 2: gen_helper_check_iol(cpu_tmp2_i32); break;
        }
747
    }
B
bellard 已提交
748
    if(s->flags & HF_SVMI_MASK) {
749 750 751 752 753 754 755
        if (!state_saved) {
            if (s->cc_op != CC_OP_DYNAMIC)
                gen_op_set_cc_op(s->cc_op);
            gen_jmp_im(cur_eip);
        }
        svm_flags |= (1 << (4 + ot));
        next_eip = s->pc - s->cs_base;
756
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
757 758
        gen_helper_svm_check_io(cpu_tmp2_i32, tcg_const_i32(svm_flags),
                                tcg_const_i32(next_eip - cur_eip));
759 760 761
    }
}

B
bellard 已提交
762 763 764
static inline void gen_movs(DisasContext *s, int ot)
{
    gen_string_movl_A0_ESI(s);
B
bellard 已提交
765
    gen_op_ld_T0_A0(ot + s->mem_index);
B
bellard 已提交
766
    gen_string_movl_A0_EDI(s);
B
bellard 已提交
767
    gen_op_st_T0_A0(ot + s->mem_index);
768 769 770
    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 已提交
771 772 773 774 775 776 777 778 779 780
}

static inline void gen_update_cc_op(DisasContext *s)
{
    if (s->cc_op != CC_OP_DYNAMIC) {
        gen_op_set_cc_op(s->cc_op);
        s->cc_op = CC_OP_DYNAMIC;
    }
}

781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
static void gen_op_update1_cc(void)
{
    tcg_gen_discard_tl(cpu_cc_src);
    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_discard_tl(cpu_cc_src);
    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]);
}

811 812 813
/* compute eflags.C to reg */
static void gen_compute_eflags_c(TCGv reg)
{
814
    gen_helper_cc_compute_c(cpu_tmp2_i32, cpu_env, cpu_cc_op);
815 816 817 818 819 820
    tcg_gen_extu_i32_tl(reg, cpu_tmp2_i32);
}

/* compute all eflags to cc_src */
static void gen_compute_eflags(TCGv reg)
{
821
    gen_helper_cc_compute_all(cpu_tmp2_i32, cpu_env, cpu_cc_op);
822 823 824
    tcg_gen_extu_i32_tl(reg, cpu_tmp2_i32);
}

825
static inline void gen_setcc_slow_T0(DisasContext *s, int jcc_op)
826
{
827 828 829
    if (s->cc_op != CC_OP_DYNAMIC)
        gen_op_set_cc_op(s->cc_op);
    switch(jcc_op) {
830 831 832 833 834 835 836 837 838 839 840 841 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 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
    case JCC_O:
        gen_compute_eflags(cpu_T[0]);
        tcg_gen_shri_tl(cpu_T[0], cpu_T[0], 11);
        tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 1);
        break;
    case JCC_B:
        gen_compute_eflags_c(cpu_T[0]);
        break;
    case JCC_Z:
        gen_compute_eflags(cpu_T[0]);
        tcg_gen_shri_tl(cpu_T[0], cpu_T[0], 6);
        tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 1);
        break;
    case JCC_BE:
        gen_compute_eflags(cpu_tmp0);
        tcg_gen_shri_tl(cpu_T[0], cpu_tmp0, 6);
        tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
        tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 1);
        break;
    case JCC_S:
        gen_compute_eflags(cpu_T[0]);
        tcg_gen_shri_tl(cpu_T[0], cpu_T[0], 7);
        tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 1);
        break;
    case JCC_P:
        gen_compute_eflags(cpu_T[0]);
        tcg_gen_shri_tl(cpu_T[0], cpu_T[0], 2);
        tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 1);
        break;
    case JCC_L:
        gen_compute_eflags(cpu_tmp0);
        tcg_gen_shri_tl(cpu_T[0], cpu_tmp0, 11); /* CC_O */
        tcg_gen_shri_tl(cpu_tmp0, cpu_tmp0, 7); /* CC_S */
        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:
        gen_compute_eflags(cpu_tmp0);
        tcg_gen_shri_tl(cpu_T[0], cpu_tmp0, 11); /* CC_O */
        tcg_gen_shri_tl(cpu_tmp4, cpu_tmp0, 7); /* CC_S */
        tcg_gen_shri_tl(cpu_tmp0, cpu_tmp0, 6); /* CC_Z */
        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. */
static inline void gen_jcc1(DisasContext *s, int cc_op, int b, int l1)
{
    int inv, jcc_op, size, cond;
    TCGv t0;

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

    switch(cc_op) {
        /* we optimize the cmp/jcc case */
    case CC_OP_SUBB:
    case CC_OP_SUBW:
    case CC_OP_SUBL:
    case CC_OP_SUBQ:
        
        size = cc_op - CC_OP_SUBB;
        switch(jcc_op) {
        case JCC_Z:
        fast_jcc_z:
            switch(size) {
            case 0:
                tcg_gen_andi_tl(cpu_tmp0, cpu_cc_dst, 0xff);
                t0 = cpu_tmp0;
                break;
            case 1:
                tcg_gen_andi_tl(cpu_tmp0, cpu_cc_dst, 0xffff);
                t0 = cpu_tmp0;
                break;
#ifdef TARGET_X86_64
            case 2:
                tcg_gen_andi_tl(cpu_tmp0, cpu_cc_dst, 0xffffffff);
                t0 = cpu_tmp0;
                break;
#endif
            default:
                t0 = cpu_cc_dst;
                break;
            }
P
pbrook 已提交
970
            tcg_gen_brcondi_tl(inv ? TCG_COND_NE : TCG_COND_EQ, t0, 0, l1);
971 972 973 974 975 976
            break;
        case JCC_S:
        fast_jcc_s:
            switch(size) {
            case 0:
                tcg_gen_andi_tl(cpu_tmp0, cpu_cc_dst, 0x80);
P
pbrook 已提交
977 978
                tcg_gen_brcondi_tl(inv ? TCG_COND_EQ : TCG_COND_NE, cpu_tmp0, 
                                   0, l1);
979 980 981
                break;
            case 1:
                tcg_gen_andi_tl(cpu_tmp0, cpu_cc_dst, 0x8000);
P
pbrook 已提交
982 983
                tcg_gen_brcondi_tl(inv ? TCG_COND_EQ : TCG_COND_NE, cpu_tmp0, 
                                   0, l1);
984 985 986 987
                break;
#ifdef TARGET_X86_64
            case 2:
                tcg_gen_andi_tl(cpu_tmp0, cpu_cc_dst, 0x80000000);
P
pbrook 已提交
988 989
                tcg_gen_brcondi_tl(inv ? TCG_COND_EQ : TCG_COND_NE, cpu_tmp0, 
                                   0, l1);
990 991 992
                break;
#endif
            default:
P
pbrook 已提交
993 994
                tcg_gen_brcondi_tl(inv ? TCG_COND_GE : TCG_COND_LT, cpu_cc_dst, 
                                   0, l1);
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 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
                break;
            }
            break;
            
        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);
            switch(size) {
            case 0:
                t0 = cpu_tmp0;
                tcg_gen_andi_tl(cpu_tmp4, cpu_tmp4, 0xff);
                tcg_gen_andi_tl(t0, cpu_cc_src, 0xff);
                break;
            case 1:
                t0 = cpu_tmp0;
                tcg_gen_andi_tl(cpu_tmp4, cpu_tmp4, 0xffff);
                tcg_gen_andi_tl(t0, cpu_cc_src, 0xffff);
                break;
#ifdef TARGET_X86_64
            case 2:
                t0 = cpu_tmp0;
                tcg_gen_andi_tl(cpu_tmp4, cpu_tmp4, 0xffffffff);
                tcg_gen_andi_tl(t0, cpu_cc_src, 0xffffffff);
                break;
#endif
            default:
                t0 = cpu_cc_src;
                break;
            }
            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);
            switch(size) {
            case 0:
                t0 = cpu_tmp0;
                tcg_gen_ext8s_tl(cpu_tmp4, cpu_tmp4);
                tcg_gen_ext8s_tl(t0, cpu_cc_src);
                break;
            case 1:
                t0 = cpu_tmp0;
                tcg_gen_ext16s_tl(cpu_tmp4, cpu_tmp4);
                tcg_gen_ext16s_tl(t0, cpu_cc_src);
                break;
#ifdef TARGET_X86_64
            case 2:
                t0 = cpu_tmp0;
                tcg_gen_ext32s_tl(cpu_tmp4, cpu_tmp4);
                tcg_gen_ext32s_tl(t0, cpu_cc_src);
                break;
#endif
            default:
                t0 = cpu_cc_src;
                break;
            }
            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:
            size = (cc_op - CC_OP_ADDB) & 3;
            goto fast_jcc_z;
        case JCC_S:
            size = (cc_op - CC_OP_ADDB) & 3;
            goto fast_jcc_s;
        default:
            goto slow_jcc;
        }
        break;
    default:
    slow_jcc:
1121
        gen_setcc_slow_T0(s, jcc_op);
P
pbrook 已提交
1122 1123
        tcg_gen_brcondi_tl(inv ? TCG_COND_EQ : TCG_COND_NE, 
                           cpu_T[0], 0, l1);
1124 1125 1126 1127
        break;
    }
}

B
bellard 已提交
1128 1129 1130
/* 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 已提交
1131
{
B
bellard 已提交
1132 1133 1134 1135
    int l1, l2;

    l1 = gen_new_label();
    l2 = gen_new_label();
1136
    gen_op_jnz_ecx(s->aflag, l1);
B
bellard 已提交
1137 1138 1139 1140
    gen_set_label(l2);
    gen_jmp_tb(s, next_eip, 1);
    gen_set_label(l1);
    return l2;
B
bellard 已提交
1141 1142 1143 1144
}

static inline void gen_stos(DisasContext *s, int ot)
{
B
bellard 已提交
1145
    gen_op_mov_TN_reg(OT_LONG, 0, R_EAX);
B
bellard 已提交
1146
    gen_string_movl_A0_EDI(s);
B
bellard 已提交
1147
    gen_op_st_T0_A0(ot + s->mem_index);
1148 1149
    gen_op_movl_T0_Dshift(ot);
    gen_op_add_reg_T0(s->aflag, R_EDI);
B
bellard 已提交
1150 1151 1152 1153 1154
}

static inline void gen_lods(DisasContext *s, int ot)
{
    gen_string_movl_A0_ESI(s);
B
bellard 已提交
1155 1156
    gen_op_ld_T0_A0(ot + s->mem_index);
    gen_op_mov_reg_T0(ot, R_EAX);
1157 1158
    gen_op_movl_T0_Dshift(ot);
    gen_op_add_reg_T0(s->aflag, R_ESI);
B
bellard 已提交
1159 1160 1161 1162
}

static inline void gen_scas(DisasContext *s, int ot)
{
B
bellard 已提交
1163
    gen_op_mov_TN_reg(OT_LONG, 0, R_EAX);
B
bellard 已提交
1164
    gen_string_movl_A0_EDI(s);
B
bellard 已提交
1165
    gen_op_ld_T1_A0(ot + s->mem_index);
B
bellard 已提交
1166
    gen_op_cmpl_T0_T1_cc();
1167 1168
    gen_op_movl_T0_Dshift(ot);
    gen_op_add_reg_T0(s->aflag, R_EDI);
B
bellard 已提交
1169 1170 1171 1172 1173
}

static inline void gen_cmps(DisasContext *s, int ot)
{
    gen_string_movl_A0_ESI(s);
B
bellard 已提交
1174
    gen_op_ld_T0_A0(ot + s->mem_index);
B
bellard 已提交
1175
    gen_string_movl_A0_EDI(s);
B
bellard 已提交
1176
    gen_op_ld_T1_A0(ot + s->mem_index);
B
bellard 已提交
1177
    gen_op_cmpl_T0_T1_cc();
1178 1179 1180
    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 已提交
1181 1182 1183 1184
}

static inline void gen_ins(DisasContext *s, int ot)
{
P
pbrook 已提交
1185 1186
    if (use_icount)
        gen_io_start();
B
bellard 已提交
1187
    gen_string_movl_A0_EDI(s);
1188 1189
    /* Note: we must do this dummy write first to be restartable in
       case of page fault. */
B
bellard 已提交
1190
    gen_op_movl_T0_0();
B
bellard 已提交
1191
    gen_op_st_T0_A0(ot + s->mem_index);
1192
    gen_op_mov_TN_reg(OT_WORD, 1, R_EDX);
1193 1194
    tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[1]);
    tcg_gen_andi_i32(cpu_tmp2_i32, cpu_tmp2_i32, 0xffff);
P
pbrook 已提交
1195
    gen_helper_in_func(ot, cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
1196
    gen_op_st_T0_A0(ot + s->mem_index);
1197 1198
    gen_op_movl_T0_Dshift(ot);
    gen_op_add_reg_T0(s->aflag, R_EDI);
P
pbrook 已提交
1199 1200
    if (use_icount)
        gen_io_end();
B
bellard 已提交
1201 1202 1203 1204
}

static inline void gen_outs(DisasContext *s, int ot)
{
P
pbrook 已提交
1205 1206
    if (use_icount)
        gen_io_start();
B
bellard 已提交
1207
    gen_string_movl_A0_ESI(s);
B
bellard 已提交
1208
    gen_op_ld_T0_A0(ot + s->mem_index);
1209 1210

    gen_op_mov_TN_reg(OT_WORD, 1, R_EDX);
1211 1212 1213
    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 已提交
1214
    gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
1215

1216 1217
    gen_op_movl_T0_Dshift(ot);
    gen_op_add_reg_T0(s->aflag, R_ESI);
P
pbrook 已提交
1218 1219
    if (use_icount)
        gen_io_end();
B
bellard 已提交
1220 1221 1222 1223 1224 1225
}

/* 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 已提交
1226
                                 target_ulong cur_eip, target_ulong next_eip) \
B
bellard 已提交
1227
{                                                                             \
B
bellard 已提交
1228
    int l2;\
B
bellard 已提交
1229
    gen_update_cc_op(s);                                                      \
B
bellard 已提交
1230
    l2 = gen_jz_ecx_string(s, next_eip);                                      \
B
bellard 已提交
1231
    gen_ ## op(s, ot);                                                        \
1232
    gen_op_add_reg_im(s->aflag, R_ECX, -1);                                   \
B
bellard 已提交
1233 1234 1235
    /* a loop would cause two single step exceptions if ECX = 1               \
       before rep string_insn */                                              \
    if (!s->jmp_opt)                                                          \
1236
        gen_op_jz_ecx(s->aflag, l2);                                          \
B
bellard 已提交
1237 1238 1239 1240 1241
    gen_jmp(s, cur_eip);                                                      \
}

#define GEN_REPZ2(op)                                                         \
static inline void gen_repz_ ## op(DisasContext *s, int ot,                   \
B
bellard 已提交
1242 1243
                                   target_ulong cur_eip,                      \
                                   target_ulong next_eip,                     \
B
bellard 已提交
1244 1245
                                   int nz)                                    \
{                                                                             \
B
bellard 已提交
1246
    int l2;\
B
bellard 已提交
1247
    gen_update_cc_op(s);                                                      \
B
bellard 已提交
1248
    l2 = gen_jz_ecx_string(s, next_eip);                                      \
B
bellard 已提交
1249
    gen_ ## op(s, ot);                                                        \
1250
    gen_op_add_reg_im(s->aflag, R_ECX, -1);                                   \
B
bellard 已提交
1251
    gen_op_set_cc_op(CC_OP_SUBB + ot);                                        \
1252
    gen_jcc1(s, CC_OP_SUBB + ot, (JCC_Z << 1) | (nz ^ 1), l2);                \
B
bellard 已提交
1253
    if (!s->jmp_opt)                                                          \
1254
        gen_op_jz_ecx(s->aflag, l2);                                          \
B
bellard 已提交
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
    gen_jmp(s, cur_eip);                                                      \
}

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

P
pbrook 已提交
1266 1267 1268
static void gen_helper_fp_arith_ST0_FT0(int op)
{
    switch (op) {
B
Blue Swirl 已提交
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
    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 已提交
1293 1294
    }
}
B
bellard 已提交
1295 1296

/* NOTE the exception in "r" op ordering */
P
pbrook 已提交
1297 1298 1299 1300
static void gen_helper_fp_arith_STN_ST0(int op, int opreg)
{
    TCGv_i32 tmp = tcg_const_i32(opreg);
    switch (op) {
B
Blue Swirl 已提交
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
    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 已提交
1319 1320
    }
}
B
bellard 已提交
1321 1322 1323 1324 1325

/* 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 已提交
1326
        gen_op_mov_TN_reg(ot, 0, d);
B
bellard 已提交
1327
    } else {
B
bellard 已提交
1328
        gen_op_ld_T0_A0(ot + s1->mem_index);
B
bellard 已提交
1329 1330 1331
    }
    switch(op) {
    case OP_ADCL:
B
bellard 已提交
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
        if (s1->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s1->cc_op);
        gen_compute_eflags_c(cpu_tmp4);
        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);
        s1->cc_op = CC_OP_DYNAMIC;
        break;
B
bellard 已提交
1348 1349 1350
    case OP_SBBL:
        if (s1->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s1->cc_op);
B
bellard 已提交
1351 1352 1353 1354
        gen_compute_eflags_c(cpu_tmp4);
        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 已提交
1355
            gen_op_mov_reg_T0(ot, d);
B
bellard 已提交
1356 1357 1358 1359 1360 1361 1362
        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);
B
bellard 已提交
1363
        s1->cc_op = CC_OP_DYNAMIC;
B
bellard 已提交
1364
        break;
B
bellard 已提交
1365 1366
    case OP_ADDL:
        gen_op_addl_T0_T1();
B
bellard 已提交
1367 1368 1369 1370 1371
        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();
B
bellard 已提交
1372 1373 1374
        s1->cc_op = CC_OP_ADDB + ot;
        break;
    case OP_SUBL:
B
bellard 已提交
1375
        tcg_gen_sub_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
B
bellard 已提交
1376 1377 1378 1379 1380
        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();
B
bellard 已提交
1381 1382 1383 1384
        s1->cc_op = CC_OP_SUBB + ot;
        break;
    default:
    case OP_ANDL:
B
bellard 已提交
1385
        tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
B
bellard 已提交
1386 1387 1388 1389 1390
        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();
B
bellard 已提交
1391 1392
        s1->cc_op = CC_OP_LOGICB + ot;
        break;
B
bellard 已提交
1393
    case OP_ORL:
B
bellard 已提交
1394
        tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
B
bellard 已提交
1395 1396 1397 1398 1399
        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();
B
bellard 已提交
1400 1401
        s1->cc_op = CC_OP_LOGICB + ot;
        break;
B
bellard 已提交
1402
    case OP_XORL:
B
bellard 已提交
1403
        tcg_gen_xor_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
B
bellard 已提交
1404 1405 1406 1407 1408
        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();
B
bellard 已提交
1409 1410 1411 1412 1413 1414 1415
        s1->cc_op = CC_OP_LOGICB + ot;
        break;
    case OP_CMPL:
        gen_op_cmpl_T0_T1_cc();
        s1->cc_op = CC_OP_SUBB + ot;
        break;
    }
1416 1417
}

B
bellard 已提交
1418 1419 1420 1421
/* 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 已提交
1422
        gen_op_mov_TN_reg(ot, 0, d);
B
bellard 已提交
1423
    else
B
bellard 已提交
1424
        gen_op_ld_T0_A0(ot + s1->mem_index);
B
bellard 已提交
1425 1426 1427
    if (s1->cc_op != CC_OP_DYNAMIC)
        gen_op_set_cc_op(s1->cc_op);
    if (c > 0) {
1428
        tcg_gen_addi_tl(cpu_T[0], cpu_T[0], 1);
B
bellard 已提交
1429 1430
        s1->cc_op = CC_OP_INCB + ot;
    } else {
1431
        tcg_gen_addi_tl(cpu_T[0], cpu_T[0], -1);
B
bellard 已提交
1432 1433 1434
        s1->cc_op = CC_OP_DECB + ot;
    }
    if (d != OR_TMP0)
B
bellard 已提交
1435
        gen_op_mov_reg_T0(ot, d);
B
bellard 已提交
1436
    else
B
bellard 已提交
1437
        gen_op_st_T0_A0(ot + s1->mem_index);
1438
    gen_compute_eflags_c(cpu_cc_src);
B
bellard 已提交
1439
    tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
B
bellard 已提交
1440 1441
}

1442 1443
static void gen_shift_rm_T1(DisasContext *s, int ot, int op1, 
                            int is_right, int is_arith)
B
bellard 已提交
1444
{
1445 1446
    target_ulong mask;
    int shift_label;
1447
    TCGv t0, t1, t2;
1448

1449
    if (ot == OT_QUAD) {
1450
        mask = 0x3f;
1451
    } else {
1452
        mask = 0x1f;
1453
    }
1454

1455
    /* load */
1456
    if (op1 == OR_TMP0) {
1457
        gen_op_ld_T0_A0(ot + s->mem_index);
1458
    } else {
1459
        gen_op_mov_TN_reg(ot, 0, op1);
1460
    }
1461

1462 1463 1464
    t0 = tcg_temp_local_new();
    t1 = tcg_temp_local_new();
    t2 = tcg_temp_local_new();
1465

1466
    tcg_gen_andi_tl(t2, cpu_T[1], mask);
1467 1468 1469

    if (is_right) {
        if (is_arith) {
B
bellard 已提交
1470
            gen_exts(ot, cpu_T[0]);
1471 1472
            tcg_gen_mov_tl(t0, cpu_T[0]);
            tcg_gen_sar_tl(cpu_T[0], cpu_T[0], t2);
1473
        } else {
B
bellard 已提交
1474
            gen_extu(ot, cpu_T[0]);
1475 1476
            tcg_gen_mov_tl(t0, cpu_T[0]);
            tcg_gen_shr_tl(cpu_T[0], cpu_T[0], t2);
1477 1478
        }
    } else {
1479 1480
        tcg_gen_mov_tl(t0, cpu_T[0]);
        tcg_gen_shl_tl(cpu_T[0], cpu_T[0], t2);
1481 1482 1483
    }

    /* store */
1484
    if (op1 == OR_TMP0) {
1485
        gen_op_st_T0_A0(ot + s->mem_index);
1486
    } else {
1487
        gen_op_mov_reg_T0(ot, op1);
1488 1489
    }

1490
    /* update eflags if non zero shift */
1491
    if (s->cc_op != CC_OP_DYNAMIC) {
1492
        gen_op_set_cc_op(s->cc_op);
1493
    }
1494

1495
    tcg_gen_mov_tl(t1, cpu_T[0]);
1496

1497
    shift_label = gen_new_label();
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
    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);
    }
1512

1513
    if (is_right) {
1514
        tcg_gen_movi_i32(cpu_cc_op, CC_OP_SARB + ot);
1515
    } else {
1516
        tcg_gen_movi_i32(cpu_cc_op, CC_OP_SHLB + ot);
1517 1518
    }

1519 1520
    gen_set_label(shift_label);
    s->cc_op = CC_OP_DYNAMIC; /* cannot predict flags after */
1521 1522 1523

    tcg_temp_free(t0);
    tcg_temp_free(t1);
1524
    tcg_temp_free(t2);
1525 1526
}

B
bellard 已提交
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
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 已提交
1548
                tcg_gen_sari_tl(cpu_tmp4, cpu_T[0], op2 - 1);
B
bellard 已提交
1549 1550 1551
                tcg_gen_sari_tl(cpu_T[0], cpu_T[0], op2);
            } else {
                gen_extu(ot, cpu_T[0]);
B
bellard 已提交
1552
                tcg_gen_shri_tl(cpu_tmp4, cpu_T[0], op2 - 1);
B
bellard 已提交
1553 1554 1555
                tcg_gen_shri_tl(cpu_T[0], cpu_T[0], op2);
            }
        } else {
B
bellard 已提交
1556
            tcg_gen_shli_tl(cpu_tmp4, cpu_T[0], op2 - 1);
B
bellard 已提交
1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
            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 已提交
1569
        tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
B
bellard 已提交
1570 1571 1572 1573 1574 1575 1576 1577
        tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
        if (is_right)
            s->cc_op = CC_OP_SARB + ot;
        else
            s->cc_op = CC_OP_SHLB + ot;
    }
}

1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590
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;
1591 1592 1593
    TCGv t0, t1, t2, a0;

    /* XXX: inefficient, but we must use local temps */
P
pbrook 已提交
1594 1595 1596 1597
    t0 = tcg_temp_local_new();
    t1 = tcg_temp_local_new();
    t2 = tcg_temp_local_new();
    a0 = tcg_temp_local_new();
1598

1599 1600 1601 1602 1603 1604
    if (ot == OT_QUAD)
        mask = 0x3f;
    else
        mask = 0x1f;

    /* load */
1605 1606 1607 1608 1609 1610
    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);
    }
1611

1612 1613 1614
    tcg_gen_mov_tl(t1, cpu_T[1]);

    tcg_gen_andi_tl(t1, t1, mask);
1615 1616 1617 1618

    /* Must test zero case to avoid using undefined behaviour in TCG
       shifts. */
    label1 = gen_new_label();
1619
    tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, label1);
1620 1621
    
    if (ot <= OT_WORD)
1622
        tcg_gen_andi_tl(cpu_tmp0, t1, (1 << (3 + ot)) - 1);
1623
    else
1624
        tcg_gen_mov_tl(cpu_tmp0, t1);
1625
    
1626 1627
    gen_extu(ot, t0);
    tcg_gen_mov_tl(t2, t0);
1628 1629 1630 1631 1632

    data_bits = 8 << ot;
    /* XXX: rely on behaviour of shifts when operand 2 overflows (XXX:
       fix TCG definition) */
    if (is_right) {
1633
        tcg_gen_shr_tl(cpu_tmp4, t0, cpu_tmp0);
1634
        tcg_gen_subfi_tl(cpu_tmp0, data_bits, cpu_tmp0);
1635
        tcg_gen_shl_tl(t0, t0, cpu_tmp0);
1636
    } else {
1637
        tcg_gen_shl_tl(cpu_tmp4, t0, cpu_tmp0);
1638
        tcg_gen_subfi_tl(cpu_tmp0, data_bits, cpu_tmp0);
1639
        tcg_gen_shr_tl(t0, t0, cpu_tmp0);
1640
    }
1641
    tcg_gen_or_tl(t0, t0, cpu_tmp4);
1642 1643 1644

    gen_set_label(label1);
    /* store */
1645 1646 1647 1648 1649
    if (op1 == OR_TMP0) {
        gen_op_st_v(ot + s->mem_index, t0, a0);
    } else {
        gen_op_mov_reg_v(ot, op1, t0);
    }
1650 1651 1652 1653 1654 1655
    
    /* update eflags */
    if (s->cc_op != CC_OP_DYNAMIC)
        gen_op_set_cc_op(s->cc_op);

    label2 = gen_new_label();
1656
    tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, label2);
1657 1658 1659

    gen_compute_eflags(cpu_cc_src);
    tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~(CC_O | CC_C));
1660
    tcg_gen_xor_tl(cpu_tmp0, t2, t0);
1661 1662 1663 1664
    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) {
1665
        tcg_gen_shri_tl(t0, t0, data_bits - 1);
1666
    }
1667 1668
    tcg_gen_andi_tl(t0, t0, CC_C);
    tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, t0);
1669 1670 1671 1672 1673 1674
    
    tcg_gen_discard_tl(cpu_cc_dst);
    tcg_gen_movi_i32(cpu_cc_op, CC_OP_EFLAGS);
        
    gen_set_label(label2);
    s->cc_op = CC_OP_DYNAMIC; /* cannot predict flags after */
1675 1676 1677 1678 1679

    tcg_temp_free(t0);
    tcg_temp_free(t1);
    tcg_temp_free(t2);
    tcg_temp_free(a0);
1680 1681
}

M
malc 已提交
1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 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 1756 1757 1758
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 */
        if (s->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s->cc_op);

        gen_compute_eflags(cpu_cc_src);
        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_gen_discard_tl(cpu_cc_dst);
        tcg_gen_movi_i32(cpu_cc_op, CC_OP_EFLAGS);
        s->cc_op = CC_OP_EFLAGS;
    }

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

1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
/* XXX: add faster immediate = 1 case */
static void gen_rotc_rm_T1(DisasContext *s, int ot, int op1, 
                           int is_right)
{
    int label1;

    if (s->cc_op != CC_OP_DYNAMIC)
        gen_op_set_cc_op(s->cc_op);

    /* 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 已提交
1774 1775
    if (is_right) {
        switch (ot) {
1776 1777 1778 1779 1780 1781 1782 1783 1784
        case 0:
            gen_helper_rcrb(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
        case 1:
            gen_helper_rcrw(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
        case 2:
            gen_helper_rcrl(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1785
#ifdef TARGET_X86_64
1786 1787 1788
        case 3:
            gen_helper_rcrq(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1789 1790 1791 1792
#endif
        }
    } else {
        switch (ot) {
1793 1794 1795 1796 1797 1798 1799 1800 1801
        case 0:
            gen_helper_rclb(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
        case 1:
            gen_helper_rclw(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
        case 2:
            gen_helper_rcll(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1802
#ifdef TARGET_X86_64
1803 1804 1805
        case 3:
            gen_helper_rclq(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1806 1807 1808
#endif
        }
    }
1809 1810 1811 1812 1813 1814 1815 1816
    /* store */
    if (op1 == OR_TMP0)
        gen_op_st_T0_A0(ot + s->mem_index);
    else
        gen_op_mov_reg_T0(ot, op1);

    /* update eflags */
    label1 = gen_new_label();
1817
    tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_cc_tmp, -1, label1);
1818

1819
    tcg_gen_mov_tl(cpu_cc_src, cpu_cc_tmp);
1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
    tcg_gen_discard_tl(cpu_cc_dst);
    tcg_gen_movi_i32(cpu_cc_op, CC_OP_EFLAGS);
        
    gen_set_label(label1);
    s->cc_op = CC_OP_DYNAMIC; /* cannot predict flags after */
}

/* 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;
1833 1834
    TCGv t0, t1, t2, a0;

P
pbrook 已提交
1835 1836 1837 1838
    t0 = tcg_temp_local_new();
    t1 = tcg_temp_local_new();
    t2 = tcg_temp_local_new();
    a0 = tcg_temp_local_new();
1839 1840 1841 1842 1843 1844 1845

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

    /* load */
1846 1847 1848 1849 1850 1851
    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);
    }
1852 1853

    tcg_gen_andi_tl(cpu_T3, cpu_T3, mask);
1854 1855 1856 1857

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

1858 1859 1860
    /* Must test zero case to avoid using undefined behaviour in TCG
       shifts. */
    label1 = gen_new_label();
1861
    tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, label1);
1862
    
1863
    tcg_gen_addi_tl(cpu_tmp5, t2, -1);
1864 1865 1866
    if (ot == OT_WORD) {
        /* Note: we implement the Intel behaviour for shift count > 16 */
        if (is_right) {
1867 1868 1869 1870
            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);
1871

1872
            tcg_gen_shr_tl(cpu_tmp4, t0, cpu_tmp5);
1873 1874
            
            /* only needed if count > 16, but a test would complicate */
1875
            tcg_gen_subfi_tl(cpu_tmp5, 32, t2);
1876
            tcg_gen_shl_tl(cpu_tmp0, t0, cpu_tmp5);
1877

1878
            tcg_gen_shr_tl(t0, t0, t2);
1879

1880
            tcg_gen_or_tl(t0, t0, cpu_tmp0);
1881 1882
        } else {
            /* XXX: not optimal */
1883 1884 1885 1886
            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);
1887
            
1888
            tcg_gen_shl_tl(cpu_tmp4, t0, cpu_tmp5);
1889
            tcg_gen_subfi_tl(cpu_tmp0, 32, cpu_tmp5);
1890 1891
            tcg_gen_shr_tl(cpu_tmp5, t1, cpu_tmp0);
            tcg_gen_or_tl(cpu_tmp4, cpu_tmp4, cpu_tmp5);
1892

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

1904
            tcg_gen_shr_tl(cpu_tmp4, t0, cpu_tmp5);
1905

1906
            tcg_gen_shr_tl(t0, t0, t2);
1907
            tcg_gen_subfi_tl(cpu_tmp5, data_bits, t2);
1908 1909
            tcg_gen_shl_tl(t1, t1, cpu_tmp5);
            tcg_gen_or_tl(t0, t0, t1);
1910 1911 1912
            
        } else {
            if (ot == OT_LONG)
1913
                tcg_gen_ext32u_tl(t1, t1);
1914

1915
            tcg_gen_shl_tl(cpu_tmp4, t0, cpu_tmp5);
1916
            
1917
            tcg_gen_shl_tl(t0, t0, t2);
1918
            tcg_gen_subfi_tl(cpu_tmp5, data_bits, t2);
1919 1920
            tcg_gen_shr_tl(t1, t1, cpu_tmp5);
            tcg_gen_or_tl(t0, t0, t1);
1921 1922
        }
    }
1923
    tcg_gen_mov_tl(t1, cpu_tmp4);
1924 1925 1926

    gen_set_label(label1);
    /* store */
1927 1928 1929 1930 1931
    if (op1 == OR_TMP0) {
        gen_op_st_v(ot + s->mem_index, t0, a0);
    } else {
        gen_op_mov_reg_v(ot, op1, t0);
    }
1932 1933 1934 1935 1936 1937
    
    /* update eflags */
    if (s->cc_op != CC_OP_DYNAMIC)
        gen_op_set_cc_op(s->cc_op);

    label2 = gen_new_label();
1938
    tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, label2);
1939

1940 1941
    tcg_gen_mov_tl(cpu_cc_src, t1);
    tcg_gen_mov_tl(cpu_cc_dst, t0);
1942 1943 1944 1945 1946 1947 1948
    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);
    s->cc_op = CC_OP_DYNAMIC; /* cannot predict flags after */
1949 1950 1951 1952 1953

    tcg_temp_free(t0);
    tcg_temp_free(t1);
    tcg_temp_free(t2);
    tcg_temp_free(a0);
1954 1955 1956 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
}

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 已提交
1984 1985 1986 1987
}

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

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

B
bellard 已提交
2037 2038
        if (base == 4) {
            havesib = 1;
B
bellard 已提交
2039
            code = ldub_code(s->pc++);
B
bellard 已提交
2040
            scale = (code >> 6) & 3;
B
bellard 已提交
2041 2042
            index = ((code >> 3) & 7) | REX_X(s);
            base = (code & 7);
B
bellard 已提交
2043
        }
B
bellard 已提交
2044
        base |= REX_B(s);
B
bellard 已提交
2045 2046 2047

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

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

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

B
bellard 已提交
2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208
static void gen_nop_modrm(DisasContext *s, int modrm)
{
    int mod, rm, base, code;

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

    if (s->aflag) {

        base = rm;
2209

B
bellard 已提交
2210 2211 2212 2213
        if (base == 4) {
            code = ldub_code(s->pc++);
            base = (code & 7);
        }
2214

B
bellard 已提交
2215 2216 2217 2218 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
        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 已提交
2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257
/* 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) {
2258 2259
#ifdef TARGET_X86_64
        if (CODE64(s)) {
B
bellard 已提交
2260
            gen_op_addq_A0_seg(override);
2261
        } else
2262 2263
#endif
        {
2264
            gen_op_addl_A0_seg(s, override);
2265
        }
B
bellard 已提交
2266 2267 2268
    }
}

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

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

static inline uint32_t insn_get(DisasContext *s, int ot)
{
    uint32_t ret;

    switch(ot) {
    case OT_BYTE:
B
bellard 已提交
2307
        ret = ldub_code(s->pc);
B
bellard 已提交
2308 2309 2310
        s->pc++;
        break;
    case OT_WORD:
B
bellard 已提交
2311
        ret = lduw_code(s->pc);
B
bellard 已提交
2312 2313 2314 2315
        s->pc += 2;
        break;
    default:
    case OT_LONG:
B
bellard 已提交
2316
        ret = ldl_code(s->pc);
B
bellard 已提交
2317 2318 2319 2320 2321 2322
        s->pc += 4;
        break;
    }
    return ret;
}

B
bellard 已提交
2323 2324 2325 2326 2327 2328 2329 2330
static inline int insn_const_size(unsigned int ot)
{
    if (ot <= OT_LONG)
        return 1 << ot;
    else
        return 4;
}

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

2352
static inline void gen_jcc(DisasContext *s, int b,
B
bellard 已提交
2353
                           target_ulong val, target_ulong next_eip)
B
bellard 已提交
2354
{
2355
    int l1, l2, cc_op;
2356

2357
    cc_op = s->cc_op;
J
Jun Koi 已提交
2358
    gen_update_cc_op(s);
B
bellard 已提交
2359
    if (s->jmp_opt) {
B
bellard 已提交
2360
        l1 = gen_new_label();
2361 2362
        gen_jcc1(s, cc_op, b, l1);
        
2363
        gen_goto_tb(s, 0, next_eip);
B
bellard 已提交
2364 2365

        gen_set_label(l1);
2366
        gen_goto_tb(s, 1, val);
J
Jun Koi 已提交
2367
        s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2368
    } else {
B
bellard 已提交
2369 2370 2371

        l1 = gen_new_label();
        l2 = gen_new_label();
2372 2373
        gen_jcc1(s, cc_op, b, l1);

B
bellard 已提交
2374
        gen_jmp_im(next_eip);
2375 2376
        tcg_gen_br(l2);

B
bellard 已提交
2377 2378 2379
        gen_set_label(l1);
        gen_jmp_im(val);
        gen_set_label(l2);
B
bellard 已提交
2380 2381 2382 2383 2384 2385
        gen_eob(s);
    }
}

static void gen_setcc(DisasContext *s, int b)
{
2386
    int inv, jcc_op, l1;
2387
    TCGv t0;
B
bellard 已提交
2388

2389 2390
    if (is_fast_jcc_case(s, b)) {
        /* nominal case: we use a jump */
2391
        /* XXX: make it faster by adding new instructions in TCG */
P
pbrook 已提交
2392
        t0 = tcg_temp_local_new();
2393
        tcg_gen_movi_tl(t0, 0);
2394 2395
        l1 = gen_new_label();
        gen_jcc1(s, s->cc_op, b ^ 1, l1);
2396
        tcg_gen_movi_tl(t0, 1);
2397
        gen_set_label(l1);
2398 2399
        tcg_gen_mov_tl(cpu_T[0], t0);
        tcg_temp_free(t0);
2400 2401 2402 2403 2404 2405
    } 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;
2406
        gen_setcc_slow_T0(s, jcc_op);
2407 2408 2409
        if (inv) {
            tcg_gen_xori_tl(cpu_T[0], cpu_T[0], 1);
        }
B
bellard 已提交
2410 2411 2412
    }
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

/* an interrupt is different from an exception because of the
B
blueswir1 已提交
2721
   privilege checks */
2722
static void gen_interrupt(DisasContext *s, int intno,
B
bellard 已提交
2723
                          target_ulong cur_eip, target_ulong next_eip)
B
bellard 已提交
2724 2725 2726
{
    if (s->cc_op != CC_OP_DYNAMIC)
        gen_op_set_cc_op(s->cc_op);
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 2736
{
    if (s->cc_op != CC_OP_DYNAMIC)
        gen_op_set_cc_op(s->cc_op);
B
bellard 已提交
2737
    gen_jmp_im(cur_eip);
P
pbrook 已提交
2738
    gen_helper_debug();
J
Jun Koi 已提交
2739
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2740 2741 2742 2743 2744 2745 2746 2747
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

B
Blue Swirl 已提交
3088
static const struct SSEOpHelper_epp sse_op_table6[256] = {
B
balrog 已提交
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 3133 3134
    [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 已提交
3135 3136
};

B
Blue Swirl 已提交
3137
static const struct SSEOpHelper_eppi sse_op_table7[256] = {
B
balrog 已提交
3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159
    [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 已提交
3160 3161
};

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

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

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

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

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

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

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

            if (b == 0x17)
                s->cc_op = CC_OP_EFLAGS;
B
balrog 已提交
3861
            break;
B
balrog 已提交
3862 3863 3864 3865 3866 3867 3868 3869 3870
        case 0x338: /* crc32 */
        crc32:
            b = modrm;
            modrm = ldub_code(s->pc++);
            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 3885
            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]);
            gen_ldst_modrm(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 3895 3896 3897 3898
            b = modrm;
            modrm = ldub_code(s->pc++);
            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 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940
                ot = (s->dflag == 2) ? OT_QUAD : OT_LONG;
                rm = (modrm & 7) | REX_B(s);
                if (mod != 3)
                    gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
                reg = ((modrm >> 3) & 7) | rex_r;
                val = ldub_code(s->pc++);
                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 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060

            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);
                    gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
                    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);
                    gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
                    gen_ldq_env_A0(s->mem_index, op2_offset);
                }
            }
            val = ldub_code(s->pc++);

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

                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 4090 4091
        }
        if (is_xmm) {
            op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
            if (mod != 3) {
                gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
                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 4113 4114
                }
            } 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) {
                gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
                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;
A
aurel32 已提交
4125
            val = ldub_code(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 4136
        case 0x70: /* pshufx insn */
        case 0xc6: /* pshufx insn */
            val = ldub_code(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 4145 4146 4147
            break;
        case 0xc2:
            /* compare insns */
            val = ldub_code(s->pc++);
            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 4183 4184 4185 4186 4187
            break;
        }
        if (b == 0x2e || b == 0x2f) {
            s->cc_op = CC_OP_EFLAGS;
        }
    }
}

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

4198
    if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP)))
4199
        tcg_gen_debug_insn_start(pc_start);
B
bellard 已提交
4200 4201 4202 4203 4204
    s->pc = pc_start;
    prefixes = 0;
    aflag = s->code32;
    dflag = s->code32;
    s->override = -1;
B
bellard 已提交
4205 4206 4207 4208 4209
    rex_w = -1;
    rex_r = 0;
#ifdef TARGET_X86_64
    s->rex_x = 0;
    s->rex_b = 0;
4210
    x86_64_hregs = 0;
B
bellard 已提交
4211 4212
#endif
    s->rip_offset = 0; /* for relative ip address */
B
bellard 已提交
4213
 next_byte:
B
bellard 已提交
4214
    b = ldub_code(s->pc);
B
bellard 已提交
4215 4216
    s->pc++;
    /* check prefixes */
B
bellard 已提交
4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270
#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;
4271
    } else
B
bellard 已提交
4272 4273 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
#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 已提交
4313 4314 4315 4316 4317 4318 4319 4320
    }

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

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

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

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

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

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

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

B
bellard 已提交
4413
            modrm = ldub_code(s->pc++);
B
bellard 已提交
4414
            mod = (modrm >> 6) & 3;
B
bellard 已提交
4415
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4416
            op = (modrm >> 3) & 7;
4417

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

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

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

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

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

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

        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 */
4736
            /* XXX: optimize if memory (no 'and' is necessary) */
B
bellard 已提交
4737 4738 4739
            if (s->dflag == 0)
                gen_op_andl_T0_ffff();
            next_eip = s->pc - s->cs_base;
B
bellard 已提交
4740
            gen_movtl_T1_im(next_eip);
4741 4742
            gen_push_T1(s);
            gen_op_jmp_T0();
B
bellard 已提交
4743 4744
            gen_eob(s);
            break;
B
bellard 已提交
4745
        case 3: /* lcall Ev */
B
bellard 已提交
4746
            gen_op_ld_T1_A0(ot + s->mem_index);
4747
            gen_add_A0_im(s, 1 << (ot - OT_WORD + 1));
B
bellard 已提交
4748
            gen_op_ldu_T0_A0(OT_WORD + s->mem_index);
B
bellard 已提交
4749 4750 4751 4752
        do_lcall:
            if (s->pe && !s->vm86) {
                if (s->cc_op != CC_OP_DYNAMIC)
                    gen_op_set_cc_op(s->cc_op);
B
bellard 已提交
4753
                gen_jmp_im(pc_start - s->cs_base);
4754
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
4755 4756 4757
                gen_helper_lcall_protected(cpu_tmp2_i32, cpu_T[1],
                                           tcg_const_i32(dflag), 
                                           tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
4758
            } else {
4759
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
4760 4761 4762
                gen_helper_lcall_real(cpu_tmp2_i32, cpu_T[1],
                                      tcg_const_i32(dflag), 
                                      tcg_const_i32(s->pc - s->cs_base));
B
bellard 已提交
4763 4764 4765 4766 4767 4768 4769 4770 4771 4772
            }
            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 已提交
4773
            gen_op_ld_T1_A0(ot + s->mem_index);
4774
            gen_add_A0_im(s, 1 << (ot - OT_WORD + 1));
B
bellard 已提交
4775
            gen_op_ldu_T0_A0(OT_WORD + s->mem_index);
B
bellard 已提交
4776 4777 4778 4779
        do_ljmp:
            if (s->pe && !s->vm86) {
                if (s->cc_op != CC_OP_DYNAMIC)
                    gen_op_set_cc_op(s->cc_op);
B
bellard 已提交
4780
                gen_jmp_im(pc_start - s->cs_base);
4781
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
4782 4783
                gen_helper_ljmp_protected(cpu_tmp2_i32, cpu_T[1],
                                          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

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

B
bellard 已提交
4809
        gen_ldst_modrm(s, modrm, ot, OR_TMP0, 0);
B
bellard 已提交
4810
        gen_op_mov_TN_reg(ot, 1, reg);
B
bellard 已提交
4811 4812 4813
        gen_op_testl_T0_T1_cc();
        s->cc_op = CC_OP_LOGICB + ot;
        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;
B
bellard 已提交
4821 4822
        val = insn_get(s, ot);

B
bellard 已提交
4823
        gen_op_mov_TN_reg(ot, 0, OR_EAX);
B
bellard 已提交
4824 4825 4826 4827
        gen_op_movl_T1_im(val);
        gen_op_testl_T0_T1_cc();
        s->cc_op = CC_OP_LOGICB + ot;
        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;
B
bellard 已提交
4871
        modrm = ldub_code(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;
B
bellard 已提交
4877 4878 4879 4880 4881
        gen_ldst_modrm(s, modrm, ot, OR_TMP0, 0);
        if (b == 0x69) {
            val = insn_get(s, ot);
            gen_op_movl_T1_im(val);
        } else if (b == 0x6b) {
4882
            val = (int8_t)insn_get(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);
B
bellard 已提交
4927
        s->cc_op = 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;
B
bellard 已提交
4935
        modrm = ldub_code(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 4946
        } else {
            gen_lea_modrm(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 4954 4955 4956 4957
        }
        gen_op_update2_cc();
        s->cc_op = CC_OP_ADDB + ot;
        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 4966 4967 4968

            if ((b & 1) == 0)
                ot = OT_BYTE;
            else
                ot = dflag + OT_WORD;
            modrm = ldub_code(s->pc++);
            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 4978
            } else {
                gen_lea_modrm(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);
B
bellard 已提交
5006
            s->cc_op = 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 */
B
bellard 已提交
5014
        modrm = ldub_code(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 5023 5024 5025
#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);
            if (s->cc_op != CC_OP_DYNAMIC)
                gen_op_set_cc_op(s->cc_op);
            gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
P
pbrook 已提交
5026
            gen_helper_cmpxchg16b(cpu_A0);
B
bellard 已提交
5027 5028 5029 5030 5031 5032 5033 5034 5035
        } else
#endif        
        {
            if (!(s->cpuid_features & CPUID_CX8))
                goto illegal_op;
            gen_jmp_im(pc_start - s->cs_base);
            if (s->cc_op != CC_OP_DYNAMIC)
                gen_op_set_cc_op(s->cc_op);
            gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
P
pbrook 已提交
5036
            gen_helper_cmpxchg8b(cpu_A0);
B
bellard 已提交
5037
        }
B
bellard 已提交
5038 5039
        s->cc_op = CC_OP_EFLAGS;
        break;
5040

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

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

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

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

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

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

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

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

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

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

B
bellard 已提交
5493
            modrm = ldub_code(s->pc++);
B
bellard 已提交
5494 5495
            mod = (modrm >> 6) & 3;
            op = (modrm >> 3) & 7;
5496

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

            /* simpler op */
            if (shift == 0) {
                gen_shift(s, op, ot, opreg, OR_ECX);
            } else {
                if (shift == 2) {
B
bellard 已提交
5512
                    shift = ldub_code(s->pc++);
B
bellard 已提交
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 5543 5544
                }
                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 已提交
5545
        ot = dflag + OT_WORD;
B
bellard 已提交
5546
        modrm = ldub_code(s->pc++);
B
bellard 已提交
5547
        mod = (modrm >> 6) & 3;
B
bellard 已提交
5548 5549
        rm = (modrm & 7) | REX_B(s);
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5550 5551
        if (mod != 3) {
            gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
5552
            opreg = OR_TMP0;
B
bellard 已提交
5553
        } else {
5554
            opreg = rm;
B
bellard 已提交
5555
        }
B
bellard 已提交
5556
        gen_op_mov_TN_reg(ot, 1, reg);
5557

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

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

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

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

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

    case 0xa4: /* movsS */
    case 0xa5:
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
6094
            ot = dflag + OT_WORD;
B
bellard 已提交
6095 6096 6097 6098 6099 6100 6101

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

B
bellard 已提交
6103 6104 6105 6106 6107
    case 0xaa: /* stosS */
    case 0xab:
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
6108
            ot = dflag + OT_WORD;
B
bellard 已提交
6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120

        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 已提交
6121
            ot = dflag + OT_WORD;
B
bellard 已提交
6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132
        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 已提交
6133
            ot = dflag + OT_WORD;
B
bellard 已提交
6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148
        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);
            s->cc_op = CC_OP_SUBB + ot;
        }
        break;

    case 0xa6: /* cmpsS */
    case 0xa7:
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
6149
            ot = dflag + OT_WORD;
B
bellard 已提交
6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160
        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);
            s->cc_op = CC_OP_SUBB + ot;
        }
        break;
    case 0x6c: /* insS */
    case 0x6d:
6161 6162 6163 6164
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
B
bellard 已提交
6165
        gen_op_mov_TN_reg(OT_WORD, 0, R_EDX);
T
ths 已提交
6166
        gen_op_andl_T0_ffff();
6167 6168
        gen_check_io(s, ot, pc_start - s->cs_base, 
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes) | 4);
6169 6170
        if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
            gen_repz_ins(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
B
bellard 已提交
6171
        } else {
6172
            gen_ins(s, ot);
P
pbrook 已提交
6173 6174 6175
            if (use_icount) {
                gen_jmp(s, s->pc - s->cs_base);
            }
B
bellard 已提交
6176 6177 6178 6179
        }
        break;
    case 0x6e: /* outsS */
    case 0x6f:
6180 6181 6182 6183
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
B
bellard 已提交
6184
        gen_op_mov_TN_reg(OT_WORD, 0, R_EDX);
T
ths 已提交
6185
        gen_op_andl_T0_ffff();
6186 6187
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes) | 4);
6188 6189
        if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
            gen_repz_outs(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
B
bellard 已提交
6190
        } else {
6191
            gen_outs(s, ot);
P
pbrook 已提交
6192 6193 6194
            if (use_icount) {
                gen_jmp(s, s->pc - s->cs_base);
            }
B
bellard 已提交
6195 6196 6197 6198 6199
        }
        break;

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

B
bellard 已提交
6201 6202
    case 0xe4:
    case 0xe5:
6203 6204 6205 6206 6207 6208
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
        val = ldub_code(s->pc++);
        gen_op_movl_T0_im(val);
6209 6210
        gen_check_io(s, ot, pc_start - s->cs_base,
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
P
pbrook 已提交
6211 6212
        if (use_icount)
            gen_io_start();
6213
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
6214
        gen_helper_in_func(ot, cpu_T[1], cpu_tmp2_i32);
B
bellard 已提交
6215
        gen_op_mov_reg_T1(ot, R_EAX);
P
pbrook 已提交
6216 6217 6218 6219
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6220 6221 6222
        break;
    case 0xe6:
    case 0xe7:
6223 6224 6225 6226 6227 6228
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
        val = ldub_code(s->pc++);
        gen_op_movl_T0_im(val);
6229 6230
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes));
B
bellard 已提交
6231
        gen_op_mov_TN_reg(ot, 1, R_EAX);
6232

P
pbrook 已提交
6233 6234
        if (use_icount)
            gen_io_start();
6235 6236
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
        tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
P
pbrook 已提交
6237
        gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
P
pbrook 已提交
6238 6239 6240 6241
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6242 6243 6244
        break;
    case 0xec:
    case 0xed:
6245 6246 6247 6248
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
B
bellard 已提交
6249
        gen_op_mov_TN_reg(OT_WORD, 0, R_EDX);
6250
        gen_op_andl_T0_ffff();
6251 6252
        gen_check_io(s, ot, pc_start - s->cs_base,
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
P
pbrook 已提交
6253 6254
        if (use_icount)
            gen_io_start();
6255
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
6256
        gen_helper_in_func(ot, cpu_T[1], cpu_tmp2_i32);
B
bellard 已提交
6257
        gen_op_mov_reg_T1(ot, R_EAX);
P
pbrook 已提交
6258 6259 6260 6261
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6262 6263 6264
        break;
    case 0xee:
    case 0xef:
6265 6266 6267 6268
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
B
bellard 已提交
6269
        gen_op_mov_TN_reg(OT_WORD, 0, R_EDX);
6270
        gen_op_andl_T0_ffff();
6271 6272
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes));
B
bellard 已提交
6273
        gen_op_mov_TN_reg(ot, 1, R_EAX);
6274

P
pbrook 已提交
6275 6276
        if (use_icount)
            gen_io_start();
6277 6278
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
        tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
P
pbrook 已提交
6279
        gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
P
pbrook 已提交
6280 6281 6282 6283
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6284 6285 6286 6287 6288
        break;

        /************************/
        /* control */
    case 0xc2: /* ret im */
B
bellard 已提交
6289
        val = ldsw_code(s->pc);
B
bellard 已提交
6290 6291
        s->pc += 2;
        gen_pop_T0(s);
6292 6293
        if (CODE64(s) && s->dflag)
            s->dflag = 2;
B
bellard 已提交
6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308
        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 */
B
bellard 已提交
6309
        val = ldsw_code(s->pc);
B
bellard 已提交
6310 6311 6312 6313 6314
        s->pc += 2;
    do_lret:
        if (s->pe && !s->vm86) {
            if (s->cc_op != CC_OP_DYNAMIC)
                gen_op_set_cc_op(s->cc_op);
B
bellard 已提交
6315
            gen_jmp_im(pc_start - s->cs_base);
P
pbrook 已提交
6316 6317
            gen_helper_lret_protected(tcg_const_i32(s->dflag),
                                      tcg_const_i32(val));
B
bellard 已提交
6318 6319 6320
        } else {
            gen_stack_A0(s);
            /* pop offset */
B
bellard 已提交
6321
            gen_op_ld_T0_A0(1 + s->dflag + s->mem_index);
B
bellard 已提交
6322 6323 6324 6325 6326 6327 6328
            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 已提交
6329
            gen_op_ld_T0_A0(1 + s->dflag + s->mem_index);
6330
            gen_op_movl_seg_T0_vm(R_CS);
B
bellard 已提交
6331 6332 6333 6334 6335 6336 6337 6338 6339
            /* 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 已提交
6340
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_IRET);
B
bellard 已提交
6341 6342
        if (!s->pe) {
            /* real mode */
P
pbrook 已提交
6343
            gen_helper_iret_real(tcg_const_i32(s->dflag));
B
bellard 已提交
6344
            s->cc_op = CC_OP_EFLAGS;
6345 6346 6347 6348
        } else if (s->vm86) {
            if (s->iopl != 3) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
P
pbrook 已提交
6349
                gen_helper_iret_real(tcg_const_i32(s->dflag));
6350 6351
                s->cc_op = CC_OP_EFLAGS;
            }
B
bellard 已提交
6352 6353 6354
        } else {
            if (s->cc_op != CC_OP_DYNAMIC)
                gen_op_set_cc_op(s->cc_op);
B
bellard 已提交
6355
            gen_jmp_im(pc_start - s->cs_base);
P
pbrook 已提交
6356 6357
            gen_helper_iret_protected(tcg_const_i32(s->dflag), 
                                      tcg_const_i32(s->pc - s->cs_base));
B
bellard 已提交
6358 6359 6360 6361 6362 6363
            s->cc_op = CC_OP_EFLAGS;
        }
        gen_eob(s);
        break;
    case 0xe8: /* call im */
        {
B
bellard 已提交
6364 6365 6366 6367
            if (dflag)
                tval = (int32_t)insn_get(s, OT_LONG);
            else
                tval = (int16_t)insn_get(s, OT_WORD);
B
bellard 已提交
6368
            next_eip = s->pc - s->cs_base;
B
bellard 已提交
6369
            tval += next_eip;
B
bellard 已提交
6370
            if (s->dflag == 0)
B
bellard 已提交
6371
                tval &= 0xffff;
6372 6373
            else if(!CODE64(s))
                tval &= 0xffffffff;
B
bellard 已提交
6374
            gen_movtl_T0_im(next_eip);
B
bellard 已提交
6375
            gen_push_T0(s);
B
bellard 已提交
6376
            gen_jmp(s, tval);
B
bellard 已提交
6377 6378 6379 6380 6381
        }
        break;
    case 0x9a: /* lcall im */
        {
            unsigned int selector, offset;
6382

B
bellard 已提交
6383 6384
            if (CODE64(s))
                goto illegal_op;
B
bellard 已提交
6385 6386 6387
            ot = dflag ? OT_LONG : OT_WORD;
            offset = insn_get(s, ot);
            selector = insn_get(s, OT_WORD);
6388

B
bellard 已提交
6389
            gen_op_movl_T0_im(selector);
B
bellard 已提交
6390
            gen_op_movl_T1_imu(offset);
B
bellard 已提交
6391 6392
        }
        goto do_lcall;
B
bellard 已提交
6393
    case 0xe9: /* jmp im */
B
bellard 已提交
6394 6395 6396 6397 6398
        if (dflag)
            tval = (int32_t)insn_get(s, OT_LONG);
        else
            tval = (int16_t)insn_get(s, OT_WORD);
        tval += s->pc - s->cs_base;
B
bellard 已提交
6399
        if (s->dflag == 0)
B
bellard 已提交
6400
            tval &= 0xffff;
6401 6402
        else if(!CODE64(s))
            tval &= 0xffffffff;
B
bellard 已提交
6403
        gen_jmp(s, tval);
B
bellard 已提交
6404 6405 6406 6407 6408
        break;
    case 0xea: /* ljmp im */
        {
            unsigned int selector, offset;

B
bellard 已提交
6409 6410
            if (CODE64(s))
                goto illegal_op;
B
bellard 已提交
6411 6412 6413
            ot = dflag ? OT_LONG : OT_WORD;
            offset = insn_get(s, ot);
            selector = insn_get(s, OT_WORD);
6414

B
bellard 已提交
6415
            gen_op_movl_T0_im(selector);
B
bellard 已提交
6416
            gen_op_movl_T1_imu(offset);
B
bellard 已提交
6417 6418 6419
        }
        goto do_ljmp;
    case 0xeb: /* jmp Jb */
B
bellard 已提交
6420 6421
        tval = (int8_t)insn_get(s, OT_BYTE);
        tval += s->pc - s->cs_base;
B
bellard 已提交
6422
        if (s->dflag == 0)
B
bellard 已提交
6423 6424
            tval &= 0xffff;
        gen_jmp(s, tval);
B
bellard 已提交
6425 6426
        break;
    case 0x70 ... 0x7f: /* jcc Jb */
B
bellard 已提交
6427
        tval = (int8_t)insn_get(s, OT_BYTE);
B
bellard 已提交
6428 6429 6430
        goto do_jcc;
    case 0x180 ... 0x18f: /* jcc Jv */
        if (dflag) {
B
bellard 已提交
6431
            tval = (int32_t)insn_get(s, OT_LONG);
B
bellard 已提交
6432
        } else {
6433
            tval = (int16_t)insn_get(s, OT_WORD);
B
bellard 已提交
6434 6435 6436
        }
    do_jcc:
        next_eip = s->pc - s->cs_base;
B
bellard 已提交
6437
        tval += next_eip;
B
bellard 已提交
6438
        if (s->dflag == 0)
B
bellard 已提交
6439 6440
            tval &= 0xffff;
        gen_jcc(s, b, tval, next_eip);
B
bellard 已提交
6441 6442 6443
        break;

    case 0x190 ... 0x19f: /* setcc Gv */
B
bellard 已提交
6444
        modrm = ldub_code(s->pc++);
B
bellard 已提交
6445 6446 6447 6448
        gen_setcc(s, b);
        gen_ldst_modrm(s, modrm, OT_BYTE, OR_TMP0, 1);
        break;
    case 0x140 ... 0x14f: /* cmov Gv, Ev */
6449 6450
        {
            int l1;
6451 6452
            TCGv t0;

6453 6454 6455 6456
            ot = dflag + OT_WORD;
            modrm = ldub_code(s->pc++);
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
P
pbrook 已提交
6457
            t0 = tcg_temp_local_new();
6458 6459
            if (mod != 3) {
                gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
6460
                gen_op_ld_v(ot + s->mem_index, t0, cpu_A0);
6461 6462
            } else {
                rm = (modrm & 7) | REX_B(s);
6463
                gen_op_mov_v_reg(ot, t0, rm);
6464 6465 6466 6467 6468 6469
            }
#ifdef TARGET_X86_64
            if (ot == OT_LONG) {
                /* XXX: specific Intel behaviour ? */
                l1 = gen_new_label();
                gen_jcc1(s, s->cc_op, b ^ 1, l1);
6470
                tcg_gen_mov_tl(cpu_regs[reg], t0);
6471
                gen_set_label(l1);
6472
                tcg_gen_ext32u_tl(cpu_regs[reg], cpu_regs[reg]);
6473 6474 6475 6476 6477
            } else
#endif
            {
                l1 = gen_new_label();
                gen_jcc1(s, s->cc_op, b ^ 1, l1);
6478
                gen_op_mov_reg_v(ot, reg, t0);
6479 6480
                gen_set_label(l1);
            }
6481
            tcg_temp_free(t0);
B
bellard 已提交
6482 6483
        }
        break;
6484

B
bellard 已提交
6485 6486 6487
        /************************/
        /* flags */
    case 0x9c: /* pushf */
B
bellard 已提交
6488
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_PUSHF);
B
bellard 已提交
6489 6490 6491 6492 6493
        if (s->vm86 && s->iopl != 3) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
            if (s->cc_op != CC_OP_DYNAMIC)
                gen_op_set_cc_op(s->cc_op);
6494
            gen_helper_read_eflags(cpu_T[0], cpu_env);
B
bellard 已提交
6495 6496 6497 6498
            gen_push_T0(s);
        }
        break;
    case 0x9d: /* popf */
B
bellard 已提交
6499
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_POPF);
B
bellard 已提交
6500 6501 6502 6503 6504 6505
        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) {
6506 6507 6508 6509 6510
                    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 已提交
6511
                } else {
6512 6513 6514 6515 6516
                    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 已提交
6517 6518
                }
            } else {
B
bellard 已提交
6519 6520
                if (s->cpl <= s->iopl) {
                    if (s->dflag) {
6521 6522 6523 6524 6525 6526
                        gen_helper_write_eflags(cpu_env, cpu_T[0],
                                                tcg_const_i32((TF_MASK |
                                                               AC_MASK |
                                                               ID_MASK |
                                                               NT_MASK |
                                                               IF_MASK)));
B
bellard 已提交
6527
                    } else {
6528 6529 6530 6531 6532 6533 6534
                        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 已提交
6535
                    }
B
bellard 已提交
6536
                } else {
B
bellard 已提交
6537
                    if (s->dflag) {
6538 6539 6540
                        gen_helper_write_eflags(cpu_env, cpu_T[0],
                                           tcg_const_i32((TF_MASK | AC_MASK |
                                                          ID_MASK | NT_MASK)));
B
bellard 已提交
6541
                    } else {
6542 6543 6544 6545
                        gen_helper_write_eflags(cpu_env, cpu_T[0],
                                           tcg_const_i32((TF_MASK | AC_MASK |
                                                          ID_MASK | NT_MASK)
                                                         & 0xffff));
B
bellard 已提交
6546
                    }
B
bellard 已提交
6547 6548 6549 6550 6551
                }
            }
            gen_pop_update(s);
            s->cc_op = CC_OP_EFLAGS;
            /* abort translation because TF flag may change */
B
bellard 已提交
6552
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
6553 6554 6555 6556
            gen_eob(s);
        }
        break;
    case 0x9e: /* sahf */
B
bellard 已提交
6557
        if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM))
B
bellard 已提交
6558
            goto illegal_op;
B
bellard 已提交
6559
        gen_op_mov_TN_reg(OT_BYTE, 0, R_AH);
B
bellard 已提交
6560 6561
        if (s->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s->cc_op);
6562 6563 6564 6565
        gen_compute_eflags(cpu_cc_src);
        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 已提交
6566 6567 6568
        s->cc_op = CC_OP_EFLAGS;
        break;
    case 0x9f: /* lahf */
B
bellard 已提交
6569
        if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM))
B
bellard 已提交
6570
            goto illegal_op;
B
bellard 已提交
6571 6572
        if (s->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s->cc_op);
6573 6574 6575
        gen_compute_eflags(cpu_T[0]);
        /* Note: gen_compute_eflags() only gives the condition codes */
        tcg_gen_ori_tl(cpu_T[0], cpu_T[0], 0x02);
B
bellard 已提交
6576
        gen_op_mov_reg_T0(OT_BYTE, R_AH);
B
bellard 已提交
6577 6578 6579 6580
        break;
    case 0xf5: /* cmc */
        if (s->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s->cc_op);
6581 6582
        gen_compute_eflags(cpu_cc_src);
        tcg_gen_xori_tl(cpu_cc_src, cpu_cc_src, CC_C);
B
bellard 已提交
6583 6584 6585 6586 6587
        s->cc_op = CC_OP_EFLAGS;
        break;
    case 0xf8: /* clc */
        if (s->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s->cc_op);
6588 6589
        gen_compute_eflags(cpu_cc_src);
        tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~CC_C);
B
bellard 已提交
6590 6591 6592 6593 6594
        s->cc_op = CC_OP_EFLAGS;
        break;
    case 0xf9: /* stc */
        if (s->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s->cc_op);
6595 6596
        gen_compute_eflags(cpu_cc_src);
        tcg_gen_ori_tl(cpu_cc_src, cpu_cc_src, CC_C);
B
bellard 已提交
6597 6598 6599
        s->cc_op = CC_OP_EFLAGS;
        break;
    case 0xfc: /* cld */
6600
        tcg_gen_movi_i32(cpu_tmp2_i32, 1);
6601
        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, offsetof(CPUX86State, df));
B
bellard 已提交
6602 6603
        break;
    case 0xfd: /* std */
6604
        tcg_gen_movi_i32(cpu_tmp2_i32, -1);
6605
        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, offsetof(CPUX86State, df));
B
bellard 已提交
6606 6607 6608 6609 6610
        break;

        /************************/
        /* bit operations */
    case 0x1ba: /* bt/bts/btr/btc Gv, im */
B
bellard 已提交
6611
        ot = dflag + OT_WORD;
B
bellard 已提交
6612
        modrm = ldub_code(s->pc++);
B
bellard 已提交
6613
        op = (modrm >> 3) & 7;
B
bellard 已提交
6614
        mod = (modrm >> 6) & 3;
B
bellard 已提交
6615
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
6616
        if (mod != 3) {
B
bellard 已提交
6617
            s->rip_offset = 1;
B
bellard 已提交
6618
            gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
6619
            gen_op_ld_T0_A0(ot + s->mem_index);
B
bellard 已提交
6620
        } else {
B
bellard 已提交
6621
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
6622 6623
        }
        /* load shift */
B
bellard 已提交
6624
        val = ldub_code(s->pc++);
B
bellard 已提交
6625 6626 6627 6628
        gen_op_movl_T1_im(val);
        if (op < 4)
            goto illegal_op;
        op -= 4;
B
bellard 已提交
6629
        goto bt_op;
B
bellard 已提交
6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641
    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 已提交
6642
        ot = dflag + OT_WORD;
B
bellard 已提交
6643
        modrm = ldub_code(s->pc++);
B
bellard 已提交
6644
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
6645
        mod = (modrm >> 6) & 3;
B
bellard 已提交
6646
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
6647
        gen_op_mov_TN_reg(OT_LONG, 1, reg);
B
bellard 已提交
6648 6649 6650
        if (mod != 3) {
            gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
            /* specific case: we need to add a displacement */
B
bellard 已提交
6651 6652 6653 6654
            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 已提交
6655
            gen_op_ld_T0_A0(ot + s->mem_index);
B
bellard 已提交
6656
        } else {
B
bellard 已提交
6657
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
6658
        }
B
bellard 已提交
6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686
    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;
        }
B
bellard 已提交
6687 6688 6689
        s->cc_op = CC_OP_SARB + ot;
        if (op != 0) {
            if (mod != 3)
B
bellard 已提交
6690
                gen_op_st_T0_A0(ot + s->mem_index);
B
bellard 已提交
6691
            else
B
bellard 已提交
6692
                gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
6693 6694
            tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
            tcg_gen_movi_tl(cpu_cc_dst, 0);
B
bellard 已提交
6695 6696 6697 6698
        }
        break;
    case 0x1bc: /* bsf */
    case 0x1bd: /* bsr */
B
bellard 已提交
6699 6700
        {
            int label1;
6701 6702
            TCGv t0;

B
bellard 已提交
6703 6704 6705
            ot = dflag + OT_WORD;
            modrm = ldub_code(s->pc++);
            reg = ((modrm >> 3) & 7) | rex_r;
6706
            gen_ldst_modrm(s,modrm, ot, OR_TMP0, 0);
B
bellard 已提交
6707
            gen_extu(ot, cpu_T[0]);
P
pbrook 已提交
6708
            t0 = tcg_temp_local_new();
6709
            tcg_gen_mov_tl(t0, cpu_T[0]);
6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720
            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 已提交
6721
            } else {
6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734
                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);
                tcg_gen_discard_tl(cpu_cc_src);
                s->cc_op = CC_OP_LOGICB + ot;
B
bellard 已提交
6735
            }
6736
            tcg_temp_free(t0);
B
bellard 已提交
6737
        }
B
bellard 已提交
6738 6739 6740 6741
        break;
        /************************/
        /* bcd */
    case 0x27: /* daa */
B
bellard 已提交
6742 6743
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
6744 6745
        if (s->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s->cc_op);
6746
        gen_helper_daa(cpu_env);
B
bellard 已提交
6747 6748 6749
        s->cc_op = CC_OP_EFLAGS;
        break;
    case 0x2f: /* das */
B
bellard 已提交
6750 6751
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
6752 6753
        if (s->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s->cc_op);
6754
        gen_helper_das(cpu_env);
B
bellard 已提交
6755 6756 6757
        s->cc_op = CC_OP_EFLAGS;
        break;
    case 0x37: /* aaa */
B
bellard 已提交
6758 6759
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
6760 6761
        if (s->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s->cc_op);
6762
        gen_helper_aaa(cpu_env);
B
bellard 已提交
6763 6764 6765
        s->cc_op = CC_OP_EFLAGS;
        break;
    case 0x3f: /* aas */
B
bellard 已提交
6766 6767
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
6768 6769
        if (s->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s->cc_op);
6770
        gen_helper_aas(cpu_env);
B
bellard 已提交
6771 6772 6773
        s->cc_op = CC_OP_EFLAGS;
        break;
    case 0xd4: /* aam */
B
bellard 已提交
6774 6775
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
6776
        val = ldub_code(s->pc++);
6777 6778 6779
        if (val == 0) {
            gen_exception(s, EXCP00_DIVZ, pc_start - s->cs_base);
        } else {
6780
            gen_helper_aam(cpu_env, tcg_const_i32(val));
6781 6782
            s->cc_op = CC_OP_LOGICB;
        }
B
bellard 已提交
6783 6784
        break;
    case 0xd5: /* aad */
B
bellard 已提交
6785 6786
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
6787
        val = ldub_code(s->pc++);
6788
        gen_helper_aad(cpu_env, tcg_const_i32(val));
B
bellard 已提交
6789 6790 6791 6792 6793
        s->cc_op = CC_OP_LOGICB;
        break;
        /************************/
        /* misc */
    case 0x90: /* nop */
6794
        /* XXX: correct lock test for all insn */
R
Richard Henderson 已提交
6795
        if (prefixes & PREFIX_LOCK) {
6796
            goto illegal_op;
R
Richard Henderson 已提交
6797 6798 6799 6800 6801
        }
        /* 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 已提交
6802 6803 6804
        if (prefixes & PREFIX_REPZ) {
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_PAUSE);
        }
B
bellard 已提交
6805 6806
        break;
    case 0x9b: /* fwait */
6807
        if ((s->flags & (HF_MP_MASK | HF_TS_MASK)) ==
B
bellard 已提交
6808 6809
            (HF_MP_MASK | HF_TS_MASK)) {
            gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
B
bellard 已提交
6810 6811 6812
        } else {
            if (s->cc_op != CC_OP_DYNAMIC)
                gen_op_set_cc_op(s->cc_op);
B
bellard 已提交
6813
            gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6814
            gen_helper_fwait(cpu_env);
B
bellard 已提交
6815
        }
B
bellard 已提交
6816 6817 6818 6819 6820
        break;
    case 0xcc: /* int3 */
        gen_interrupt(s, EXCP03_INT3, pc_start - s->cs_base, s->pc - s->cs_base);
        break;
    case 0xcd: /* int N */
B
bellard 已提交
6821
        val = ldub_code(s->pc++);
6822
        if (s->vm86 && s->iopl != 3) {
6823
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6824 6825 6826
        } else {
            gen_interrupt(s, val, pc_start - s->cs_base, s->pc - s->cs_base);
        }
B
bellard 已提交
6827 6828
        break;
    case 0xce: /* into */
B
bellard 已提交
6829 6830
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
6831 6832
        if (s->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s->cc_op);
6833
        gen_jmp_im(pc_start - s->cs_base);
P
pbrook 已提交
6834
        gen_helper_into(tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
6835
        break;
A
aurel32 已提交
6836
#ifdef WANT_ICEBP
B
bellard 已提交
6837
    case 0xf1: /* icebp (undocumented, exits to external debugger) */
B
bellard 已提交
6838
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_ICEBP);
6839
#if 1
B
bellard 已提交
6840
        gen_debug(s, pc_start - s->cs_base);
6841 6842 6843 6844 6845
#else
        /* start debug */
        tb_flush(cpu_single_env);
        cpu_set_log(CPU_LOG_INT | CPU_LOG_TB_IN_ASM);
#endif
B
bellard 已提交
6846
        break;
A
aurel32 已提交
6847
#endif
B
bellard 已提交
6848 6849 6850
    case 0xfa: /* cli */
        if (!s->vm86) {
            if (s->cpl <= s->iopl) {
6851
                gen_helper_cli(cpu_env);
B
bellard 已提交
6852 6853 6854 6855 6856
            } else {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            }
        } else {
            if (s->iopl == 3) {
6857
                gen_helper_cli(cpu_env);
B
bellard 已提交
6858 6859 6860 6861 6862 6863 6864 6865 6866
            } 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:
6867
                gen_helper_sti(cpu_env);
B
bellard 已提交
6868
                /* interruptions are enabled only the first insn after sti */
6869 6870 6871
                /* If several instructions disable interrupts, only the
                   _first_ does it */
                if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
6872
                    gen_helper_set_inhibit_irq(cpu_env);
B
bellard 已提交
6873
                /* give a chance to handle pending irqs */
B
bellard 已提交
6874
                gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887
                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 已提交
6888 6889
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
6890
        ot = dflag ? OT_LONG : OT_WORD;
B
bellard 已提交
6891
        modrm = ldub_code(s->pc++);
B
bellard 已提交
6892 6893 6894 6895
        reg = (modrm >> 3) & 7;
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
B
bellard 已提交
6896
        gen_op_mov_TN_reg(ot, 0, reg);
B
bellard 已提交
6897
        gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
6898
        gen_jmp_im(pc_start - s->cs_base);
6899
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
bellard 已提交
6900
        if (ot == OT_WORD)
P
pbrook 已提交
6901
            gen_helper_boundw(cpu_A0, cpu_tmp2_i32);
B
bellard 已提交
6902
        else
P
pbrook 已提交
6903
            gen_helper_boundl(cpu_A0, cpu_tmp2_i32);
B
bellard 已提交
6904 6905
        break;
    case 0x1c8 ... 0x1cf: /* bswap reg */
B
bellard 已提交
6906 6907 6908
        reg = (b & 7) | REX_B(s);
#ifdef TARGET_X86_64
        if (dflag == 2) {
B
bellard 已提交
6909
            gen_op_mov_TN_reg(OT_QUAD, 0, reg);
A
aurel32 已提交
6910
            tcg_gen_bswap64_i64(cpu_T[0], cpu_T[0]);
B
bellard 已提交
6911
            gen_op_mov_reg_T0(OT_QUAD, reg);
6912
        } else
6913
#endif
B
bellard 已提交
6914 6915
        {
            gen_op_mov_TN_reg(OT_LONG, 0, reg);
6916 6917
            tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
            tcg_gen_bswap32_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
6918
            gen_op_mov_reg_T0(OT_LONG, reg);
B
bellard 已提交
6919
        }
B
bellard 已提交
6920 6921
        break;
    case 0xd6: /* salc */
B
bellard 已提交
6922 6923
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
6924 6925
        if (s->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s->cc_op);
6926 6927 6928
        gen_compute_eflags_c(cpu_T[0]);
        tcg_gen_neg_tl(cpu_T[0], cpu_T[0]);
        gen_op_mov_reg_T0(OT_BYTE, R_EAX);
B
bellard 已提交
6929 6930 6931 6932 6933
        break;
    case 0xe0: /* loopnz */
    case 0xe1: /* loopz */
    case 0xe2: /* loop */
    case 0xe3: /* jecxz */
B
bellard 已提交
6934
        {
6935
            int l1, l2, l3;
B
bellard 已提交
6936 6937 6938 6939 6940 6941

            tval = (int8_t)insn_get(s, OT_BYTE);
            next_eip = s->pc - s->cs_base;
            tval += next_eip;
            if (s->dflag == 0)
                tval &= 0xffff;
6942

B
bellard 已提交
6943 6944
            l1 = gen_new_label();
            l2 = gen_new_label();
6945
            l3 = gen_new_label();
B
bellard 已提交
6946
            b &= 3;
6947 6948 6949 6950 6951 6952 6953 6954 6955 6956
            switch(b) {
            case 0: /* loopnz */
            case 1: /* loopz */
                if (s->cc_op != CC_OP_DYNAMIC)
                    gen_op_set_cc_op(s->cc_op);
                gen_op_add_reg_im(s->aflag, R_ECX, -1);
                gen_op_jz_ecx(s->aflag, l3);
                gen_compute_eflags(cpu_tmp0);
                tcg_gen_andi_tl(cpu_tmp0, cpu_tmp0, CC_Z);
                if (b == 0) {
P
pbrook 已提交
6957
                    tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, l1);
6958
                } else {
P
pbrook 已提交
6959
                    tcg_gen_brcondi_tl(TCG_COND_NE, cpu_tmp0, 0, l1);
6960 6961 6962 6963 6964 6965 6966 6967 6968 6969
                }
                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 已提交
6970 6971
            }

6972
            gen_set_label(l3);
B
bellard 已提交
6973
            gen_jmp_im(next_eip);
6974
            tcg_gen_br(l2);
6975

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

            modrm = ldub_code(s->pc++);
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
            rm = (modrm & 7) | REX_B(s);
7420

B
bellard 已提交
7421
            if (mod == 3) {
B
bellard 已提交
7422
                gen_op_mov_TN_reg(OT_LONG, 0, rm);
B
bellard 已提交
7423 7424
                /* sign extend */
                if (d_ot == OT_QUAD)
B
bellard 已提交
7425
                    tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
7426
                gen_op_mov_reg_T0(d_ot, reg);
B
bellard 已提交
7427 7428 7429
            } else {
                gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
                if (d_ot == OT_QUAD) {
B
bellard 已提交
7430
                    gen_op_lds_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
7431
                } else {
B
bellard 已提交
7432
                    gen_op_ld_T0_A0(OT_LONG + s->mem_index);
B
bellard 已提交
7433
                }
B
bellard 已提交
7434
                gen_op_mov_reg_T0(d_ot, reg);
B
bellard 已提交
7435
            }
7436
        } else
B
bellard 已提交
7437 7438
#endif
        {
7439
            int label1;
L
Laurent Desnogues 已提交
7440
            TCGv t0, t1, t2, a0;
7441

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

        modrm = ldub_code(s->pc++);
        reg = ((modrm >> 3) & 7);

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

        gen_ldst_modrm(s, modrm, ot, OR_TMP0, 0);
B
Blue Swirl 已提交
7744
        gen_helper_popcnt(cpu_T[0], cpu_env, cpu_T[0], tcg_const_i32(ot));
B
balrog 已提交
7745
        gen_op_mov_reg_T0(ot, reg);
B
balrog 已提交
7746 7747

        s->cc_op = CC_OP_EFLAGS;
B
balrog 已提交
7748
        break;
A
aurel32 已提交
7749 7750 7751
    case 0x10e ... 0x10f:
        /* 3DNow! instructions, ignore prefixes */
        s->prefix &= ~(PREFIX_REPZ | PREFIX_REPNZ | PREFIX_DATA);
B
bellard 已提交
7752 7753
    case 0x110 ... 0x117:
    case 0x128 ... 0x12f:
B
balrog 已提交
7754
    case 0x138 ... 0x13a:
7755
    case 0x150 ... 0x179:
B
bellard 已提交
7756 7757 7758 7759 7760 7761
    case 0x17c ... 0x17f:
    case 0x1c2:
    case 0x1c4 ... 0x1c6:
    case 0x1d0 ... 0x1fe:
        gen_sse(s, b, pc_start, rex_r);
        break;
B
bellard 已提交
7762 7763 7764 7765 7766
    default:
        goto illegal_op;
    }
    /* lock generation */
    if (s->prefix & PREFIX_LOCK)
P
pbrook 已提交
7767
        gen_helper_unlock();
B
bellard 已提交
7768 7769
    return s->pc;
 illegal_op:
7770
    if (s->prefix & PREFIX_LOCK)
P
pbrook 已提交
7771
        gen_helper_unlock();
B
bellard 已提交
7772 7773 7774 7775 7776 7777 7778
    /* 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 已提交
7779 7780
    cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
    cpu_cc_op = tcg_global_mem_new_i32(TCG_AREG0,
7781 7782
                                       offsetof(CPUX86State, cc_op), "cc_op");
    cpu_cc_src = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_src),
P
pbrook 已提交
7783
                                    "cc_src");
7784
    cpu_cc_dst = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_dst),
P
pbrook 已提交
7785
                                    "cc_dst");
7786
    cpu_cc_tmp = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_tmp),
P
pbrook 已提交
7787
                                    "cc_tmp");
7788

7789 7790
#ifdef TARGET_X86_64
    cpu_regs[R_EAX] = tcg_global_mem_new_i64(TCG_AREG0,
7791
                                             offsetof(CPUX86State, regs[R_EAX]), "rax");
7792
    cpu_regs[R_ECX] = tcg_global_mem_new_i64(TCG_AREG0,
7793
                                             offsetof(CPUX86State, regs[R_ECX]), "rcx");
7794
    cpu_regs[R_EDX] = tcg_global_mem_new_i64(TCG_AREG0,
7795
                                             offsetof(CPUX86State, regs[R_EDX]), "rdx");
7796
    cpu_regs[R_EBX] = tcg_global_mem_new_i64(TCG_AREG0,
7797
                                             offsetof(CPUX86State, regs[R_EBX]), "rbx");
7798
    cpu_regs[R_ESP] = tcg_global_mem_new_i64(TCG_AREG0,
7799
                                             offsetof(CPUX86State, regs[R_ESP]), "rsp");
7800
    cpu_regs[R_EBP] = tcg_global_mem_new_i64(TCG_AREG0,
7801
                                             offsetof(CPUX86State, regs[R_EBP]), "rbp");
7802
    cpu_regs[R_ESI] = tcg_global_mem_new_i64(TCG_AREG0,
7803
                                             offsetof(CPUX86State, regs[R_ESI]), "rsi");
7804
    cpu_regs[R_EDI] = tcg_global_mem_new_i64(TCG_AREG0,
7805
                                             offsetof(CPUX86State, regs[R_EDI]), "rdi");
7806
    cpu_regs[8] = tcg_global_mem_new_i64(TCG_AREG0,
7807
                                         offsetof(CPUX86State, regs[8]), "r8");
7808
    cpu_regs[9] = tcg_global_mem_new_i64(TCG_AREG0,
7809
                                          offsetof(CPUX86State, regs[9]), "r9");
7810
    cpu_regs[10] = tcg_global_mem_new_i64(TCG_AREG0,
7811
                                          offsetof(CPUX86State, regs[10]), "r10");
7812
    cpu_regs[11] = tcg_global_mem_new_i64(TCG_AREG0,
7813
                                          offsetof(CPUX86State, regs[11]), "r11");
7814
    cpu_regs[12] = tcg_global_mem_new_i64(TCG_AREG0,
7815
                                          offsetof(CPUX86State, regs[12]), "r12");
7816
    cpu_regs[13] = tcg_global_mem_new_i64(TCG_AREG0,
7817
                                          offsetof(CPUX86State, regs[13]), "r13");
7818
    cpu_regs[14] = tcg_global_mem_new_i64(TCG_AREG0,
7819
                                          offsetof(CPUX86State, regs[14]), "r14");
7820
    cpu_regs[15] = tcg_global_mem_new_i64(TCG_AREG0,
7821
                                          offsetof(CPUX86State, regs[15]), "r15");
7822 7823
#else
    cpu_regs[R_EAX] = tcg_global_mem_new_i32(TCG_AREG0,
7824
                                             offsetof(CPUX86State, regs[R_EAX]), "eax");
7825
    cpu_regs[R_ECX] = tcg_global_mem_new_i32(TCG_AREG0,
7826
                                             offsetof(CPUX86State, regs[R_ECX]), "ecx");
7827
    cpu_regs[R_EDX] = tcg_global_mem_new_i32(TCG_AREG0,
7828
                                             offsetof(CPUX86State, regs[R_EDX]), "edx");
7829
    cpu_regs[R_EBX] = tcg_global_mem_new_i32(TCG_AREG0,
7830
                                             offsetof(CPUX86State, regs[R_EBX]), "ebx");
7831
    cpu_regs[R_ESP] = tcg_global_mem_new_i32(TCG_AREG0,
7832
                                             offsetof(CPUX86State, regs[R_ESP]), "esp");
7833
    cpu_regs[R_EBP] = tcg_global_mem_new_i32(TCG_AREG0,
7834
                                             offsetof(CPUX86State, regs[R_EBP]), "ebp");
7835
    cpu_regs[R_ESI] = tcg_global_mem_new_i32(TCG_AREG0,
7836
                                             offsetof(CPUX86State, regs[R_ESI]), "esi");
7837
    cpu_regs[R_EDI] = tcg_global_mem_new_i32(TCG_AREG0,
7838
                                             offsetof(CPUX86State, regs[R_EDI]), "edi");
7839 7840
#endif

7841
    /* register helpers */
P
pbrook 已提交
7842
#define GEN_HELPER 2
7843
#include "helper.h"
B
bellard 已提交
7844 7845 7846 7847 7848
}

/* 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. */
7849
static inline void gen_intermediate_code_internal(CPUX86State *env,
7850 7851
                                                  TranslationBlock *tb,
                                                  int search_pc)
B
bellard 已提交
7852 7853
{
    DisasContext dc1, *dc = &dc1;
B
bellard 已提交
7854
    target_ulong pc_ptr;
B
bellard 已提交
7855
    uint16_t *gen_opc_end;
7856
    CPUBreakpoint *bp;
7857
    int j, lj;
7858
    uint64_t flags;
B
bellard 已提交
7859 7860
    target_ulong pc_start;
    target_ulong cs_base;
P
pbrook 已提交
7861 7862
    int num_insns;
    int max_insns;
7863

B
bellard 已提交
7864
    /* generate intermediate code */
B
bellard 已提交
7865 7866
    pc_start = tb->pc;
    cs_base = tb->cs_base;
B
bellard 已提交
7867
    flags = tb->flags;
B
bellard 已提交
7868

7869
    dc->pe = (flags >> HF_PE_SHIFT) & 1;
B
bellard 已提交
7870 7871 7872 7873 7874 7875 7876 7877
    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;
7878
    dc->singlestep_enabled = env->singlestep_enabled;
B
bellard 已提交
7879 7880 7881 7882 7883 7884 7885 7886
    dc->cc_op = CC_OP_DYNAMIC;
    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) {
        if (dc->cpl == 3)
B
bellard 已提交
7887
            dc->mem_index = 2 * 4;
B
bellard 已提交
7888
        else
B
bellard 已提交
7889
            dc->mem_index = 1 * 4;
B
bellard 已提交
7890
    }
B
bellard 已提交
7891
    dc->cpuid_features = env->cpuid_features;
B
bellard 已提交
7892
    dc->cpuid_ext_features = env->cpuid_ext_features;
7893
    dc->cpuid_ext2_features = env->cpuid_ext2_features;
B
bellard 已提交
7894
    dc->cpuid_ext3_features = env->cpuid_ext3_features;
B
bellard 已提交
7895 7896 7897 7898
#ifdef TARGET_X86_64
    dc->lma = (flags >> HF_LMA_SHIFT) & 1;
    dc->code64 = (flags >> HF_CS64_SHIFT) & 1;
#endif
B
bellard 已提交
7899
    dc->flags = flags;
7900 7901
    dc->jmp_opt = !(dc->tf || env->singlestep_enabled ||
                    (flags & HF_INHIBIT_IRQ_MASK)
B
bellard 已提交
7902
#ifndef CONFIG_SOFTMMU
B
bellard 已提交
7903 7904 7905
                    || (flags & HF_SOFTMMU_MASK)
#endif
                    );
7906 7907
#if 0
    /* check addseg logic */
B
bellard 已提交
7908
    if (!dc->addseg && (dc->vm86 || !dc->pe || !dc->code32))
7909 7910 7911
        printf("ERROR addseg\n");
#endif

P
pbrook 已提交
7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924
    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 已提交
7925

B
bellard 已提交
7926 7927 7928 7929 7930
    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;

    dc->is_jmp = DISAS_NEXT;
    pc_ptr = pc_start;
    lj = -1;
P
pbrook 已提交
7931 7932 7933 7934
    num_insns = 0;
    max_insns = tb->cflags & CF_COUNT_MASK;
    if (max_insns == 0)
        max_insns = CF_COUNT_MASK;
B
bellard 已提交
7935

P
pbrook 已提交
7936
    gen_icount_start();
B
bellard 已提交
7937
    for(;;) {
B
Blue Swirl 已提交
7938 7939
        if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
            QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
J
Jan Kiszka 已提交
7940 7941
                if (bp->pc == pc_ptr &&
                    !((bp->flags & BP_CPU) && (tb->flags & HF_RF_MASK))) {
B
bellard 已提交
7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953
                    gen_debug(dc, pc_ptr - dc->cs_base);
                    break;
                }
            }
        }
        if (search_pc) {
            j = gen_opc_ptr - gen_opc_buf;
            if (lj < j) {
                lj++;
                while (lj < j)
                    gen_opc_instr_start[lj++] = 0;
            }
B
bellard 已提交
7954
            gen_opc_pc[lj] = pc_ptr;
B
bellard 已提交
7955 7956
            gen_opc_cc_op[lj] = dc->cc_op;
            gen_opc_instr_start[lj] = 1;
P
pbrook 已提交
7957
            gen_opc_icount[lj] = num_insns;
B
bellard 已提交
7958
        }
P
pbrook 已提交
7959 7960 7961
        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
            gen_io_start();

B
bellard 已提交
7962
        pc_ptr = disas_insn(dc, pc_ptr);
P
pbrook 已提交
7963
        num_insns++;
B
bellard 已提交
7964 7965 7966 7967 7968
        /* stop translation if indicated */
        if (dc->is_jmp)
            break;
        /* if single step mode, we generate only one instruction and
           generate an exception */
7969 7970 7971
        /* 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 */
7972
        if (dc->tf || dc->singlestep_enabled ||
P
pbrook 已提交
7973
            (flags & HF_INHIBIT_IRQ_MASK)) {
B
bellard 已提交
7974
            gen_jmp_im(pc_ptr - dc->cs_base);
B
bellard 已提交
7975 7976 7977 7978 7979
            gen_eob(dc);
            break;
        }
        /* if too long translation, stop generation too */
        if (gen_opc_ptr >= gen_opc_end ||
P
pbrook 已提交
7980 7981
            (pc_ptr - pc_start) >= (TARGET_PAGE_SIZE - 32) ||
            num_insns >= max_insns) {
B
bellard 已提交
7982
            gen_jmp_im(pc_ptr - dc->cs_base);
B
bellard 已提交
7983 7984 7985
            gen_eob(dc);
            break;
        }
7986 7987 7988 7989 7990
        if (singlestep) {
            gen_jmp_im(pc_ptr - dc->cs_base);
            gen_eob(dc);
            break;
        }
B
bellard 已提交
7991
    }
P
pbrook 已提交
7992 7993 7994
    if (tb->cflags & CF_LAST_IO)
        gen_io_end();
    gen_icount_end(tb, num_insns);
B
bellard 已提交
7995 7996 7997 7998 7999 8000 8001 8002
    *gen_opc_ptr = INDEX_op_end;
    /* we don't forget to fill the last values */
    if (search_pc) {
        j = gen_opc_ptr - gen_opc_buf;
        lj++;
        while (lj <= j)
            gen_opc_instr_start[lj++] = 0;
    }
8003

B
bellard 已提交
8004
#ifdef DEBUG_DISAS
8005
    if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
B
bellard 已提交
8006
        int disas_flags;
8007 8008
        qemu_log("----------------\n");
        qemu_log("IN: %s\n", lookup_symbol(pc_start));
B
bellard 已提交
8009 8010 8011 8012 8013 8014
#ifdef TARGET_X86_64
        if (dc->code64)
            disas_flags = 2;
        else
#endif
            disas_flags = !dc->code32;
8015 8016
        log_target_disas(pc_start, pc_ptr - pc_start, disas_flags);
        qemu_log("\n");
B
bellard 已提交
8017 8018 8019
    }
#endif

P
pbrook 已提交
8020
    if (!search_pc) {
B
bellard 已提交
8021
        tb->size = pc_ptr - pc_start;
P
pbrook 已提交
8022 8023
        tb->icount = num_insns;
    }
B
bellard 已提交
8024 8025
}

8026
void gen_intermediate_code(CPUX86State *env, TranslationBlock *tb)
B
bellard 已提交
8027
{
8028
    gen_intermediate_code_internal(env, tb, 0);
B
bellard 已提交
8029 8030
}

8031
void gen_intermediate_code_pc(CPUX86State *env, TranslationBlock *tb)
B
bellard 已提交
8032
{
8033
    gen_intermediate_code_internal(env, tb, 1);
B
bellard 已提交
8034 8035
}

8036
void restore_state_to_opc(CPUX86State *env, TranslationBlock *tb, int pc_pos)
A
aurel32 已提交
8037 8038 8039
{
    int cc_op;
#ifdef DEBUG_DISAS
8040
    if (qemu_loglevel_mask(CPU_LOG_TB_OP)) {
A
aurel32 已提交
8041
        int i;
8042
        qemu_log("RESTORE:\n");
A
aurel32 已提交
8043 8044
        for(i = 0;i <= pc_pos; i++) {
            if (gen_opc_instr_start[i]) {
8045
                qemu_log("0x%04x: " TARGET_FMT_lx "\n", i, gen_opc_pc[i]);
A
aurel32 已提交
8046 8047
            }
        }
8048 8049
        qemu_log("pc_pos=0x%x eip=" TARGET_FMT_lx " cs_base=%x\n",
                pc_pos, gen_opc_pc[pc_pos] - tb->cs_base,
A
aurel32 已提交
8050 8051 8052 8053 8054 8055 8056 8057
                (uint32_t)tb->cs_base);
    }
#endif
    env->eip = gen_opc_pc[pc_pos] - tb->cs_base;
    cc_op = gen_opc_cc_op[pc_pos];
    if (cc_op != CC_OP_DYNAMIC)
        env->cc_op = cc_op;
}