translate.c 160.8 KB
Newer Older
B
bellard 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/*
 *  i386 translation
 * 
 *  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
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <signal.h>
#include <assert.h>

#include "cpu.h"
#include "exec-all.h"
#include "disas.h"

/* XXX: move that elsewhere */
static uint16_t *gen_opc_ptr;
static uint32_t *gen_opparam_ptr;

#define PREFIX_REPZ   0x01
#define PREFIX_REPNZ  0x02
#define PREFIX_LOCK   0x04
#define PREFIX_DATA   0x08
#define PREFIX_ADR    0x10

B
bellard 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#ifdef TARGET_X86_64
#define X86_64_ONLY(x) x
#define X86_64_DEF(x...) x
#define CODE64(s) ((s)->code64)
#define REX_X(s) ((s)->rex_x)
#define REX_B(s) ((s)->rex_b)
/* XXX: gcc generates push/pop in some opcodes, so we cannot use them */
#if 1
#define BUGGY_64(x) NULL
#endif
#else
#define X86_64_ONLY(x) NULL
#define X86_64_DEF(x...)
#define CODE64(s) 0
#define REX_X(s) 0
#define REX_B(s) 0
#endif

#ifdef TARGET_X86_64
static int x86_64_hregs;
#endif

B
bellard 已提交
64 65 66 67 68
typedef struct DisasContext {
    /* current insn context */
    int override; /* -1 if no override */
    int prefix;
    int aflag, dflag;
B
bellard 已提交
69
    target_ulong pc; /* pc = eip + cs_base */
B
bellard 已提交
70 71 72
    int is_jmp; /* 1 = means jump (stop translation), 2 means CPU
                   static state change (stop translation) */
    /* current block context */
B
bellard 已提交
73
    target_ulong cs_base; /* base of CS segment */
B
bellard 已提交
74 75
    int pe;     /* protected mode */
    int code32; /* 32 bit code segment */
B
bellard 已提交
76 77 78 79 80
#ifdef TARGET_X86_64
    int lma;    /* long mode active */
    int code64; /* 64 bit code segment */
    int rex_x, rex_b;
#endif
B
bellard 已提交
81 82 83 84 85 86 87 88
    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 */
89
    int singlestep_enabled; /* "hardware" single step enabled */
B
bellard 已提交
90 91
    int jmp_opt; /* use direct block chaining for direct jumps */
    int mem_index; /* select memory access functions */
B
bellard 已提交
92
    int flags; /* all execution flags */
B
bellard 已提交
93 94
    struct TranslationBlock *tb;
    int popl_esp_hack; /* for correct popl with esp base handling */
B
bellard 已提交
95 96
    int rip_offset; /* only used in x86_64, but left for simplicity */
    int cpuid_features;
B
bellard 已提交
97 98 99
} DisasContext;

static void gen_eob(DisasContext *s);
B
bellard 已提交
100 101
static void gen_jmp(DisasContext *s, target_ulong eip);
static void gen_jmp_tb(DisasContext *s, target_ulong eip, int tb_num);
B
bellard 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

/* i386 arith/logic operations */
enum {
    OP_ADDL, 
    OP_ORL, 
    OP_ADCL, 
    OP_SBBL,
    OP_ANDL, 
    OP_SUBL, 
    OP_XORL, 
    OP_CMPL,
};

/* i386 shift ops */
enum {
    OP_ROL, 
    OP_ROR, 
    OP_RCL, 
    OP_RCR, 
    OP_SHL, 
    OP_SHR, 
    OP_SHL1, /* undocumented */
    OP_SAR = 7,
};

enum {
#define DEF(s, n, copy_size) INDEX_op_ ## s,
#include "opc.h"
#undef DEF
    NB_OPS,
};

#include "gen-op.h"

/* operand size */
enum {
    OT_BYTE = 0,
    OT_WORD,
    OT_LONG, 
    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 已提交
154 155

    OR_TMP0 = 16,    /* temporary operand register */
B
bellard 已提交
156 157 158 159
    OR_TMP1,
    OR_A0, /* temporary register used when doing address evaluation */
};

B
bellard 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 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
#ifdef TARGET_X86_64

#define NB_OP_SIZES 4

#define DEF_REGS(prefix, suffix) \
  prefix ## EAX ## suffix,\
  prefix ## ECX ## suffix,\
  prefix ## EDX ## suffix,\
  prefix ## EBX ## suffix,\
  prefix ## ESP ## suffix,\
  prefix ## EBP ## suffix,\
  prefix ## ESI ## suffix,\
  prefix ## EDI ## suffix,\
  prefix ## R8 ## suffix,\
  prefix ## R9 ## suffix,\
  prefix ## R10 ## suffix,\
  prefix ## R11 ## suffix,\
  prefix ## R12 ## suffix,\
  prefix ## R13 ## suffix,\
  prefix ## R14 ## suffix,\
  prefix ## R15 ## suffix,

#define DEF_BREGS(prefixb, prefixh, suffix)             \
                                                        \
static void prefixb ## ESP ## suffix ## _wrapper(void)  \
{                                                       \
    if (x86_64_hregs)                                 \
        prefixb ## ESP ## suffix ();                    \
    else                                                \
        prefixh ## EAX ## suffix ();                    \
}                                                       \
                                                        \
static void prefixb ## EBP ## suffix ## _wrapper(void)  \
{                                                       \
    if (x86_64_hregs)                                 \
        prefixb ## EBP ## suffix ();                    \
    else                                                \
        prefixh ## ECX ## suffix ();                    \
}                                                       \
                                                        \
static void prefixb ## ESI ## suffix ## _wrapper(void)  \
{                                                       \
    if (x86_64_hregs)                                 \
        prefixb ## ESI ## suffix ();                    \
    else                                                \
        prefixh ## EDX ## suffix ();                    \
}                                                       \
                                                        \
static void prefixb ## EDI ## suffix ## _wrapper(void)  \
{                                                       \
    if (x86_64_hregs)                                 \
        prefixb ## EDI ## suffix ();                    \
    else                                                \
        prefixh ## EBX ## suffix ();                    \
}

DEF_BREGS(gen_op_movb_, gen_op_movh_, _T0)
DEF_BREGS(gen_op_movb_, gen_op_movh_, _T1)
DEF_BREGS(gen_op_movl_T0_, gen_op_movh_T0_, )
DEF_BREGS(gen_op_movl_T1_, gen_op_movh_T1_, )

#else /* !TARGET_X86_64 */

#define NB_OP_SIZES 3

#define DEF_REGS(prefix, suffix) \
  prefix ## EAX ## suffix,\
  prefix ## ECX ## suffix,\
  prefix ## EDX ## suffix,\
  prefix ## EBX ## suffix,\
  prefix ## ESP ## suffix,\
  prefix ## EBP ## suffix,\
  prefix ## ESI ## suffix,\
  prefix ## EDI ## suffix,

#endif /* !TARGET_X86_64 */

static GenOpFunc *gen_op_mov_reg_T0[NB_OP_SIZES][CPU_NB_REGS] = {
B
bellard 已提交
238 239 240 241 242
    [OT_BYTE] = {
        gen_op_movb_EAX_T0,
        gen_op_movb_ECX_T0,
        gen_op_movb_EDX_T0,
        gen_op_movb_EBX_T0,
B
bellard 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256
#ifdef TARGET_X86_64
        gen_op_movb_ESP_T0_wrapper,
        gen_op_movb_EBP_T0_wrapper,
        gen_op_movb_ESI_T0_wrapper,
        gen_op_movb_EDI_T0_wrapper,
        gen_op_movb_R8_T0,
        gen_op_movb_R9_T0,
        gen_op_movb_R10_T0,
        gen_op_movb_R11_T0,
        gen_op_movb_R12_T0,
        gen_op_movb_R13_T0,
        gen_op_movb_R14_T0,
        gen_op_movb_R15_T0,
#else
B
bellard 已提交
257 258 259 260
        gen_op_movh_EAX_T0,
        gen_op_movh_ECX_T0,
        gen_op_movh_EDX_T0,
        gen_op_movh_EBX_T0,
B
bellard 已提交
261
#endif
B
bellard 已提交
262 263
    },
    [OT_WORD] = {
B
bellard 已提交
264
        DEF_REGS(gen_op_movw_, _T0)
B
bellard 已提交
265 266
    },
    [OT_LONG] = {
B
bellard 已提交
267
        DEF_REGS(gen_op_movl_, _T0)
B
bellard 已提交
268
    },
B
bellard 已提交
269 270 271 272 273
#ifdef TARGET_X86_64
    [OT_QUAD] = {
        DEF_REGS(gen_op_movq_, _T0)
    },
#endif
B
bellard 已提交
274 275
};

B
bellard 已提交
276
static GenOpFunc *gen_op_mov_reg_T1[NB_OP_SIZES][CPU_NB_REGS] = {
B
bellard 已提交
277 278 279 280 281
    [OT_BYTE] = {
        gen_op_movb_EAX_T1,
        gen_op_movb_ECX_T1,
        gen_op_movb_EDX_T1,
        gen_op_movb_EBX_T1,
B
bellard 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295
#ifdef TARGET_X86_64
        gen_op_movb_ESP_T1_wrapper,
        gen_op_movb_EBP_T1_wrapper,
        gen_op_movb_ESI_T1_wrapper,
        gen_op_movb_EDI_T1_wrapper,
        gen_op_movb_R8_T1,
        gen_op_movb_R9_T1,
        gen_op_movb_R10_T1,
        gen_op_movb_R11_T1,
        gen_op_movb_R12_T1,
        gen_op_movb_R13_T1,
        gen_op_movb_R14_T1,
        gen_op_movb_R15_T1,
#else
B
bellard 已提交
296 297 298 299
        gen_op_movh_EAX_T1,
        gen_op_movh_ECX_T1,
        gen_op_movh_EDX_T1,
        gen_op_movh_EBX_T1,
B
bellard 已提交
300
#endif
B
bellard 已提交
301 302
    },
    [OT_WORD] = {
B
bellard 已提交
303
        DEF_REGS(gen_op_movw_, _T1)
B
bellard 已提交
304 305
    },
    [OT_LONG] = {
B
bellard 已提交
306 307 308 309 310
        DEF_REGS(gen_op_movl_, _T1)
    },
#ifdef TARGET_X86_64
    [OT_QUAD] = {
        DEF_REGS(gen_op_movq_, _T1)
B
bellard 已提交
311
    },
B
bellard 已提交
312
#endif
B
bellard 已提交
313 314
};

B
bellard 已提交
315
static GenOpFunc *gen_op_mov_reg_A0[NB_OP_SIZES - 1][CPU_NB_REGS] = {
B
bellard 已提交
316
    [0] = {
B
bellard 已提交
317
        DEF_REGS(gen_op_movw_, _A0)
B
bellard 已提交
318 319
    },
    [1] = {
B
bellard 已提交
320 321 322 323 324
        DEF_REGS(gen_op_movl_, _A0)
    },
#ifdef TARGET_X86_64
    [2] = {
        DEF_REGS(gen_op_movq_, _A0)
B
bellard 已提交
325
    },
B
bellard 已提交
326
#endif
B
bellard 已提交
327 328
};

B
bellard 已提交
329
static GenOpFunc *gen_op_mov_TN_reg[NB_OP_SIZES][2][CPU_NB_REGS] = 
B
bellard 已提交
330 331 332 333 334 335 336
{
    [OT_BYTE] = {
        {
            gen_op_movl_T0_EAX,
            gen_op_movl_T0_ECX,
            gen_op_movl_T0_EDX,
            gen_op_movl_T0_EBX,
B
bellard 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349 350
#ifdef TARGET_X86_64
            gen_op_movl_T0_ESP_wrapper,
            gen_op_movl_T0_EBP_wrapper,
            gen_op_movl_T0_ESI_wrapper,
            gen_op_movl_T0_EDI_wrapper,
            gen_op_movl_T0_R8,
            gen_op_movl_T0_R9,
            gen_op_movl_T0_R10,
            gen_op_movl_T0_R11,
            gen_op_movl_T0_R12,
            gen_op_movl_T0_R13,
            gen_op_movl_T0_R14,
            gen_op_movl_T0_R15,
#else
B
bellard 已提交
351 352 353 354
            gen_op_movh_T0_EAX,
            gen_op_movh_T0_ECX,
            gen_op_movh_T0_EDX,
            gen_op_movh_T0_EBX,
B
bellard 已提交
355
#endif
B
bellard 已提交
356 357 358 359 360 361
        },
        {
            gen_op_movl_T1_EAX,
            gen_op_movl_T1_ECX,
            gen_op_movl_T1_EDX,
            gen_op_movl_T1_EBX,
B
bellard 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374 375
#ifdef TARGET_X86_64
            gen_op_movl_T1_ESP_wrapper,
            gen_op_movl_T1_EBP_wrapper,
            gen_op_movl_T1_ESI_wrapper,
            gen_op_movl_T1_EDI_wrapper,
            gen_op_movl_T1_R8,
            gen_op_movl_T1_R9,
            gen_op_movl_T1_R10,
            gen_op_movl_T1_R11,
            gen_op_movl_T1_R12,
            gen_op_movl_T1_R13,
            gen_op_movl_T1_R14,
            gen_op_movl_T1_R15,
#else
B
bellard 已提交
376 377 378 379
            gen_op_movh_T1_EAX,
            gen_op_movh_T1_ECX,
            gen_op_movh_T1_EDX,
            gen_op_movh_T1_EBX,
B
bellard 已提交
380
#endif
B
bellard 已提交
381 382 383 384
        },
    },
    [OT_WORD] = {
        {
B
bellard 已提交
385
            DEF_REGS(gen_op_movl_T0_, )
B
bellard 已提交
386 387
        },
        {
B
bellard 已提交
388
            DEF_REGS(gen_op_movl_T1_, )
B
bellard 已提交
389 390 391 392
        },
    },
    [OT_LONG] = {
        {
B
bellard 已提交
393
            DEF_REGS(gen_op_movl_T0_, )
B
bellard 已提交
394 395
        },
        {
B
bellard 已提交
396
            DEF_REGS(gen_op_movl_T1_, )
B
bellard 已提交
397 398
        },
    },
B
bellard 已提交
399 400 401 402 403 404 405 406 407 408
#ifdef TARGET_X86_64
    [OT_QUAD] = {
        {
            DEF_REGS(gen_op_movl_T0_, )
        },
        {
            DEF_REGS(gen_op_movl_T1_, )
        },
    },
#endif
B
bellard 已提交
409 410
};

B
bellard 已提交
411 412
static GenOpFunc *gen_op_movl_A0_reg[CPU_NB_REGS] = {
    DEF_REGS(gen_op_movl_A0_, )
B
bellard 已提交
413 414
};

B
bellard 已提交
415
static GenOpFunc *gen_op_addl_A0_reg_sN[4][CPU_NB_REGS] = {
B
bellard 已提交
416
    [0] = {
B
bellard 已提交
417
        DEF_REGS(gen_op_addl_A0_, )
B
bellard 已提交
418 419
    },
    [1] = {
B
bellard 已提交
420
        DEF_REGS(gen_op_addl_A0_, _s1)
B
bellard 已提交
421 422
    },
    [2] = {
B
bellard 已提交
423
        DEF_REGS(gen_op_addl_A0_, _s2)
B
bellard 已提交
424 425
    },
    [3] = {
B
bellard 已提交
426
        DEF_REGS(gen_op_addl_A0_, _s3)
B
bellard 已提交
427 428 429
    },
};

B
bellard 已提交
430 431 432 433 434 435
#ifdef TARGET_X86_64
static GenOpFunc *gen_op_movq_A0_reg[CPU_NB_REGS] = {
    DEF_REGS(gen_op_movq_A0_, )
};

static GenOpFunc *gen_op_addq_A0_reg_sN[4][CPU_NB_REGS] = {
B
bellard 已提交
436
    [0] = {
B
bellard 已提交
437
        DEF_REGS(gen_op_addq_A0_, )
B
bellard 已提交
438 439
    },
    [1] = {
B
bellard 已提交
440 441 442 443 444 445 446
        DEF_REGS(gen_op_addq_A0_, _s1)
    },
    [2] = {
        DEF_REGS(gen_op_addq_A0_, _s2)
    },
    [3] = {
        DEF_REGS(gen_op_addq_A0_, _s3)
B
bellard 已提交
447 448
    },
};
B
bellard 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
#endif

static GenOpFunc *gen_op_cmov_reg_T1_T0[NB_OP_SIZES - 1][CPU_NB_REGS] = {
    [0] = {
        DEF_REGS(gen_op_cmovw_, _T1_T0)
    },
    [1] = {
        DEF_REGS(gen_op_cmovl_, _T1_T0)
    },
#ifdef TARGET_X86_64
    [2] = {
        DEF_REGS(gen_op_cmovq_, _T1_T0)
    },
#endif
};
B
bellard 已提交
464 465 466 467 468 469 470 471 472 473 474 475

static GenOpFunc *gen_op_arith_T0_T1_cc[8] = {
    NULL,
    gen_op_orl_T0_T1,
    NULL,
    NULL,
    gen_op_andl_T0_T1,
    NULL,
    gen_op_xorl_T0_T1,
    NULL,
};

476 477 478 479 480 481 482 483 484 485 486 487
#define DEF_ARITHC(SUFFIX)\
    {\
        gen_op_adcb ## SUFFIX ## _T0_T1_cc,\
        gen_op_sbbb ## SUFFIX ## _T0_T1_cc,\
    },\
    {\
        gen_op_adcw ## SUFFIX ## _T0_T1_cc,\
        gen_op_sbbw ## SUFFIX ## _T0_T1_cc,\
    },\
    {\
        gen_op_adcl ## SUFFIX ## _T0_T1_cc,\
        gen_op_sbbl ## SUFFIX ## _T0_T1_cc,\
B
bellard 已提交
488 489 490 491
    },\
    {\
        X86_64_ONLY(gen_op_adcq ## SUFFIX ## _T0_T1_cc),\
        X86_64_ONLY(gen_op_sbbq ## SUFFIX ## _T0_T1_cc),\
B
bellard 已提交
492
    },
493

B
bellard 已提交
494
static GenOpFunc *gen_op_arithc_T0_T1_cc[4][2] = {
495
    DEF_ARITHC( )
B
bellard 已提交
496 497
};

B
bellard 已提交
498
static GenOpFunc *gen_op_arithc_mem_T0_T1_cc[3 * 4][2] = {
499 500 501 502 503
    DEF_ARITHC(_raw)
#ifndef CONFIG_USER_ONLY
    DEF_ARITHC(_kernel)
    DEF_ARITHC(_user)
#endif
B
bellard 已提交
504 505 506 507 508 509 510 511 512 513 514 515 516
};

static const int cc_op_arithb[8] = {
    CC_OP_ADDB,
    CC_OP_LOGICB,
    CC_OP_ADDB,
    CC_OP_SUBB,
    CC_OP_LOGICB,
    CC_OP_SUBB,
    CC_OP_LOGICB,
    CC_OP_SUBB,
};

517 518 519
#define DEF_CMPXCHG(SUFFIX)\
    gen_op_cmpxchgb ## SUFFIX ## _T0_T1_EAX_cc,\
    gen_op_cmpxchgw ## SUFFIX ## _T0_T1_EAX_cc,\
B
bellard 已提交
520 521
    gen_op_cmpxchgl ## SUFFIX ## _T0_T1_EAX_cc,\
    X86_64_ONLY(gen_op_cmpxchgq ## SUFFIX ## _T0_T1_EAX_cc),
522

B
bellard 已提交
523
static GenOpFunc *gen_op_cmpxchg_T0_T1_EAX_cc[4] = {
524
    DEF_CMPXCHG( )
B
bellard 已提交
525 526
};

B
bellard 已提交
527
static GenOpFunc *gen_op_cmpxchg_mem_T0_T1_EAX_cc[3 * 4] = {
528 529 530 531 532
    DEF_CMPXCHG(_raw)
#ifndef CONFIG_USER_ONLY
    DEF_CMPXCHG(_kernel)
    DEF_CMPXCHG(_user)
#endif
B
bellard 已提交
533 534
};

535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
#define DEF_SHIFT(SUFFIX)\
    {\
        gen_op_rolb ## SUFFIX ## _T0_T1_cc,\
        gen_op_rorb ## SUFFIX ## _T0_T1_cc,\
        gen_op_rclb ## SUFFIX ## _T0_T1_cc,\
        gen_op_rcrb ## SUFFIX ## _T0_T1_cc,\
        gen_op_shlb ## SUFFIX ## _T0_T1_cc,\
        gen_op_shrb ## SUFFIX ## _T0_T1_cc,\
        gen_op_shlb ## SUFFIX ## _T0_T1_cc,\
        gen_op_sarb ## SUFFIX ## _T0_T1_cc,\
    },\
    {\
        gen_op_rolw ## SUFFIX ## _T0_T1_cc,\
        gen_op_rorw ## SUFFIX ## _T0_T1_cc,\
        gen_op_rclw ## SUFFIX ## _T0_T1_cc,\
        gen_op_rcrw ## SUFFIX ## _T0_T1_cc,\
        gen_op_shlw ## SUFFIX ## _T0_T1_cc,\
        gen_op_shrw ## SUFFIX ## _T0_T1_cc,\
        gen_op_shlw ## SUFFIX ## _T0_T1_cc,\
        gen_op_sarw ## SUFFIX ## _T0_T1_cc,\
    },\
    {\
        gen_op_roll ## SUFFIX ## _T0_T1_cc,\
        gen_op_rorl ## SUFFIX ## _T0_T1_cc,\
        gen_op_rcll ## SUFFIX ## _T0_T1_cc,\
        gen_op_rcrl ## SUFFIX ## _T0_T1_cc,\
        gen_op_shll ## SUFFIX ## _T0_T1_cc,\
        gen_op_shrl ## SUFFIX ## _T0_T1_cc,\
        gen_op_shll ## SUFFIX ## _T0_T1_cc,\
        gen_op_sarl ## SUFFIX ## _T0_T1_cc,\
B
bellard 已提交
565 566 567 568 569 570 571 572 573 574
    },\
    {\
        X86_64_ONLY(gen_op_rolq ## SUFFIX ## _T0_T1_cc),\
        X86_64_ONLY(gen_op_rorq ## SUFFIX ## _T0_T1_cc),\
        X86_64_ONLY(gen_op_rclq ## SUFFIX ## _T0_T1_cc),\
        X86_64_ONLY(gen_op_rcrq ## SUFFIX ## _T0_T1_cc),\
        X86_64_ONLY(gen_op_shlq ## SUFFIX ## _T0_T1_cc),\
        X86_64_ONLY(gen_op_shrq ## SUFFIX ## _T0_T1_cc),\
        X86_64_ONLY(gen_op_shlq ## SUFFIX ## _T0_T1_cc),\
        X86_64_ONLY(gen_op_sarq ## SUFFIX ## _T0_T1_cc),\
B
bellard 已提交
575
    },
576

B
bellard 已提交
577
static GenOpFunc *gen_op_shift_T0_T1_cc[4][8] = {
578
    DEF_SHIFT( )
B
bellard 已提交
579 580
};

B
bellard 已提交
581
static GenOpFunc *gen_op_shift_mem_T0_T1_cc[3 * 4][8] = {
582 583 584 585 586
    DEF_SHIFT(_raw)
#ifndef CONFIG_USER_ONLY
    DEF_SHIFT(_kernel)
    DEF_SHIFT(_user)
#endif
B
bellard 已提交
587 588
};

589 590 591 592 593 594 595 596 597 598 599 600
#define DEF_SHIFTD(SUFFIX, op)\
    {\
        NULL,\
        NULL,\
    },\
    {\
        gen_op_shldw ## SUFFIX ## _T0_T1_ ## op ## _cc,\
        gen_op_shrdw ## SUFFIX ## _T0_T1_ ## op ## _cc,\
    },\
    {\
        gen_op_shldl ## SUFFIX ## _T0_T1_ ## op ## _cc,\
        gen_op_shrdl ## SUFFIX ## _T0_T1_ ## op ## _cc,\
B
bellard 已提交
601 602
    },\
    {\
B
bellard 已提交
603
    },
604

B
bellard 已提交
605
static GenOpFunc1 *gen_op_shiftd_T0_T1_im_cc[4][2] = {
606
    DEF_SHIFTD(, im)
B
bellard 已提交
607 608
};

B
bellard 已提交
609
static GenOpFunc *gen_op_shiftd_T0_T1_ECX_cc[4][2] = {
610
    DEF_SHIFTD(, ECX)
B
bellard 已提交
611 612
};

B
bellard 已提交
613
static GenOpFunc1 *gen_op_shiftd_mem_T0_T1_im_cc[3 * 4][2] = {
614 615 616 617 618
    DEF_SHIFTD(_raw, im)
#ifndef CONFIG_USER_ONLY
    DEF_SHIFTD(_kernel, im)
    DEF_SHIFTD(_user, im)
#endif
B
bellard 已提交
619 620
};

B
bellard 已提交
621
static GenOpFunc *gen_op_shiftd_mem_T0_T1_ECX_cc[3 * 4][2] = {
622 623 624 625 626
    DEF_SHIFTD(_raw, ECX)
#ifndef CONFIG_USER_ONLY
    DEF_SHIFTD(_kernel, ECX)
    DEF_SHIFTD(_user, ECX)
#endif
B
bellard 已提交
627 628
};

B
bellard 已提交
629
static GenOpFunc *gen_op_btx_T0_T1_cc[3][4] = {
B
bellard 已提交
630 631 632 633 634 635 636 637 638 639 640 641
    [0] = {
        gen_op_btw_T0_T1_cc,
        gen_op_btsw_T0_T1_cc,
        gen_op_btrw_T0_T1_cc,
        gen_op_btcw_T0_T1_cc,
    },
    [1] = {
        gen_op_btl_T0_T1_cc,
        gen_op_btsl_T0_T1_cc,
        gen_op_btrl_T0_T1_cc,
        gen_op_btcl_T0_T1_cc,
    },
B
bellard 已提交
642 643 644 645 646 647 648 649 650 651 652 653 654 655
#ifdef TARGET_X86_64
    [2] = {
        gen_op_btq_T0_T1_cc,
        gen_op_btsq_T0_T1_cc,
        gen_op_btrq_T0_T1_cc,
        gen_op_btcq_T0_T1_cc,
    },
#endif
};

static GenOpFunc *gen_op_add_bit_A0_T1[3] = {
    gen_op_add_bitw_A0_T1,
    gen_op_add_bitl_A0_T1,
    X86_64_ONLY(gen_op_add_bitq_A0_T1),
B
bellard 已提交
656 657
};

B
bellard 已提交
658
static GenOpFunc *gen_op_bsx_T0_cc[3][2] = {
B
bellard 已提交
659 660 661 662 663 664 665 666
    [0] = {
        gen_op_bsfw_T0_cc,
        gen_op_bsrw_T0_cc,
    },
    [1] = {
        gen_op_bsfl_T0_cc,
        gen_op_bsrl_T0_cc,
    },
B
bellard 已提交
667 668 669 670 671 672
#ifdef TARGET_X86_64
    [2] = {
        gen_op_bsfq_T0_cc,
        gen_op_bsrq_T0_cc,
    },
#endif
B
bellard 已提交
673 674
};

B
bellard 已提交
675
static GenOpFunc *gen_op_lds_T0_A0[3 * 4] = {
B
bellard 已提交
676 677
    gen_op_ldsb_raw_T0_A0,
    gen_op_ldsw_raw_T0_A0,
B
bellard 已提交
678
    X86_64_ONLY(gen_op_ldsl_raw_T0_A0),
B
bellard 已提交
679
    NULL,
B
bellard 已提交
680
#ifndef CONFIG_USER_ONLY
B
bellard 已提交
681 682
    gen_op_ldsb_kernel_T0_A0,
    gen_op_ldsw_kernel_T0_A0,
B
bellard 已提交
683
    X86_64_ONLY(gen_op_ldsl_kernel_T0_A0),
B
bellard 已提交
684 685 686 687
    NULL,

    gen_op_ldsb_user_T0_A0,
    gen_op_ldsw_user_T0_A0,
B
bellard 已提交
688
    X86_64_ONLY(gen_op_ldsl_user_T0_A0),
B
bellard 已提交
689
    NULL,
B
bellard 已提交
690
#endif
B
bellard 已提交
691 692
};

B
bellard 已提交
693
static GenOpFunc *gen_op_ldu_T0_A0[3 * 4] = {
B
bellard 已提交
694 695
    gen_op_ldub_raw_T0_A0,
    gen_op_lduw_raw_T0_A0,
B
bellard 已提交
696
    NULL,
B
bellard 已提交
697
    NULL,
B
bellard 已提交
698

B
bellard 已提交
699
#ifndef CONFIG_USER_ONLY
B
bellard 已提交
700 701 702
    gen_op_ldub_kernel_T0_A0,
    gen_op_lduw_kernel_T0_A0,
    NULL,
B
bellard 已提交
703
    NULL,
B
bellard 已提交
704 705 706 707

    gen_op_ldub_user_T0_A0,
    gen_op_lduw_user_T0_A0,
    NULL,
B
bellard 已提交
708
    NULL,
B
bellard 已提交
709
#endif
B
bellard 已提交
710 711 712
};

/* sign does not matter, except for lidt/lgdt call (TODO: fix it) */
B
bellard 已提交
713
static GenOpFunc *gen_op_ld_T0_A0[3 * 4] = {
B
bellard 已提交
714 715 716
    gen_op_ldub_raw_T0_A0,
    gen_op_lduw_raw_T0_A0,
    gen_op_ldl_raw_T0_A0,
B
bellard 已提交
717
    X86_64_ONLY(gen_op_ldq_raw_T0_A0),
B
bellard 已提交
718

B
bellard 已提交
719
#ifndef CONFIG_USER_ONLY
B
bellard 已提交
720 721 722
    gen_op_ldub_kernel_T0_A0,
    gen_op_lduw_kernel_T0_A0,
    gen_op_ldl_kernel_T0_A0,
B
bellard 已提交
723
    X86_64_ONLY(gen_op_ldq_kernel_T0_A0),
B
bellard 已提交
724 725 726 727

    gen_op_ldub_user_T0_A0,
    gen_op_lduw_user_T0_A0,
    gen_op_ldl_user_T0_A0,
B
bellard 已提交
728
    X86_64_ONLY(gen_op_ldq_user_T0_A0),
B
bellard 已提交
729
#endif
B
bellard 已提交
730 731
};

B
bellard 已提交
732
static GenOpFunc *gen_op_ld_T1_A0[3 * 4] = {
B
bellard 已提交
733 734 735
    gen_op_ldub_raw_T1_A0,
    gen_op_lduw_raw_T1_A0,
    gen_op_ldl_raw_T1_A0,
B
bellard 已提交
736
    X86_64_ONLY(gen_op_ldq_raw_T1_A0),
B
bellard 已提交
737

B
bellard 已提交
738
#ifndef CONFIG_USER_ONLY
B
bellard 已提交
739 740 741
    gen_op_ldub_kernel_T1_A0,
    gen_op_lduw_kernel_T1_A0,
    gen_op_ldl_kernel_T1_A0,
B
bellard 已提交
742
    X86_64_ONLY(gen_op_ldq_kernel_T1_A0),
B
bellard 已提交
743 744 745 746

    gen_op_ldub_user_T1_A0,
    gen_op_lduw_user_T1_A0,
    gen_op_ldl_user_T1_A0,
B
bellard 已提交
747
    X86_64_ONLY(gen_op_ldq_user_T1_A0),
B
bellard 已提交
748
#endif
B
bellard 已提交
749 750
};

B
bellard 已提交
751
static GenOpFunc *gen_op_st_T0_A0[3 * 4] = {
B
bellard 已提交
752 753 754
    gen_op_stb_raw_T0_A0,
    gen_op_stw_raw_T0_A0,
    gen_op_stl_raw_T0_A0,
B
bellard 已提交
755
    X86_64_ONLY(gen_op_stq_raw_T0_A0),
B
bellard 已提交
756

B
bellard 已提交
757
#ifndef CONFIG_USER_ONLY
B
bellard 已提交
758 759 760
    gen_op_stb_kernel_T0_A0,
    gen_op_stw_kernel_T0_A0,
    gen_op_stl_kernel_T0_A0,
B
bellard 已提交
761
    X86_64_ONLY(gen_op_stq_kernel_T0_A0),
B
bellard 已提交
762 763 764 765

    gen_op_stb_user_T0_A0,
    gen_op_stw_user_T0_A0,
    gen_op_stl_user_T0_A0,
B
bellard 已提交
766
    X86_64_ONLY(gen_op_stq_user_T0_A0),
B
bellard 已提交
767
#endif
B
bellard 已提交
768 769
};

B
bellard 已提交
770
static GenOpFunc *gen_op_st_T1_A0[3 * 4] = {
771 772 773
    NULL,
    gen_op_stw_raw_T1_A0,
    gen_op_stl_raw_T1_A0,
B
bellard 已提交
774
    X86_64_ONLY(gen_op_stq_raw_T1_A0),
775 776 777 778 779

#ifndef CONFIG_USER_ONLY
    NULL,
    gen_op_stw_kernel_T1_A0,
    gen_op_stl_kernel_T1_A0,
B
bellard 已提交
780
    X86_64_ONLY(gen_op_stq_kernel_T1_A0),
781 782 783 784

    NULL,
    gen_op_stw_user_T1_A0,
    gen_op_stl_user_T1_A0,
B
bellard 已提交
785
    X86_64_ONLY(gen_op_stq_user_T1_A0),
786 787 788
#endif
};

B
bellard 已提交
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
static inline void gen_jmp_im(target_ulong pc)
{
#ifdef TARGET_X86_64
    if (pc == (uint32_t)pc) {
        gen_op_movl_eip_im(pc);
    } else if (pc == (int32_t)pc) {
        gen_op_movq_eip_im(pc);
    } else {
        gen_op_movq_eip_im64(pc >> 32, pc);
    }
#else
    gen_op_movl_eip_im(pc);
#endif
}

B
bellard 已提交
804 805 806 807 808
static inline void gen_string_movl_A0_ESI(DisasContext *s)
{
    int override;

    override = s->override;
B
bellard 已提交
809 810 811 812 813 814 815 816 817 818
#ifdef TARGET_X86_64
    if (s->aflag == 2) {
        if (override >= 0) {
            gen_op_movq_A0_seg(offsetof(CPUX86State,segs[override].base));
            gen_op_addq_A0_reg_sN[0][R_ESI]();
        } else {
            gen_op_movq_A0_reg[R_ESI]();
        }
    } else
#endif
B
bellard 已提交
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
    if (s->aflag) {
        /* 32 bit address */
        if (s->addseg && override < 0)
            override = R_DS;
        if (override >= 0) {
            gen_op_movl_A0_seg(offsetof(CPUX86State,segs[override].base));
            gen_op_addl_A0_reg_sN[0][R_ESI]();
        } else {
            gen_op_movl_A0_reg[R_ESI]();
        }
    } else {
        /* 16 address, always override */
        if (override < 0)
            override = R_DS;
        gen_op_movl_A0_reg[R_ESI]();
        gen_op_andl_A0_ffff();
        gen_op_addl_A0_seg(offsetof(CPUX86State,segs[override].base));
    }
}

static inline void gen_string_movl_A0_EDI(DisasContext *s)
{
B
bellard 已提交
841 842 843 844 845
#ifdef TARGET_X86_64
    if (s->aflag == 2) {
        gen_op_movq_A0_reg[R_EDI]();
    } else
#endif
B
bellard 已提交
846 847 848 849 850 851 852 853 854 855 856 857 858 859
    if (s->aflag) {
        if (s->addseg) {
            gen_op_movl_A0_seg(offsetof(CPUX86State,segs[R_ES].base));
            gen_op_addl_A0_reg_sN[0][R_EDI]();
        } else {
            gen_op_movl_A0_reg[R_EDI]();
        }
    } else {
        gen_op_movl_A0_reg[R_EDI]();
        gen_op_andl_A0_ffff();
        gen_op_addl_A0_seg(offsetof(CPUX86State,segs[R_ES].base));
    }
}

B
bellard 已提交
860
static GenOpFunc *gen_op_movl_T0_Dshift[4] = {
B
bellard 已提交
861 862 863
    gen_op_movl_T0_Dshiftb,
    gen_op_movl_T0_Dshiftw,
    gen_op_movl_T0_Dshiftl,
B
bellard 已提交
864
    X86_64_ONLY(gen_op_movl_T0_Dshiftq),
B
bellard 已提交
865 866
};

B
bellard 已提交
867 868 869 870
static GenOpFunc1 *gen_op_jnz_ecx[3] = {
    gen_op_jnz_ecxw,
    gen_op_jnz_ecxl,
    X86_64_ONLY(gen_op_jnz_ecxq),
B
bellard 已提交
871 872
};
    
B
bellard 已提交
873 874 875 876
static GenOpFunc1 *gen_op_jz_ecx[3] = {
    gen_op_jz_ecxw,
    gen_op_jz_ecxl,
    X86_64_ONLY(gen_op_jz_ecxq),
B
bellard 已提交
877 878
};

B
bellard 已提交
879
static GenOpFunc *gen_op_dec_ECX[3] = {
B
bellard 已提交
880 881
    gen_op_decw_ECX,
    gen_op_decl_ECX,
B
bellard 已提交
882
    X86_64_ONLY(gen_op_decq_ECX),
B
bellard 已提交
883 884
};

B
bellard 已提交
885
static GenOpFunc1 *gen_op_string_jnz_sub[2][4] = {
B
bellard 已提交
886
    {
B
bellard 已提交
887 888 889 890
        gen_op_jnz_subb,
        gen_op_jnz_subw,
        gen_op_jnz_subl,
        X86_64_ONLY(gen_op_jnz_subq),
B
bellard 已提交
891 892
    },
    {
B
bellard 已提交
893 894 895 896
        gen_op_jz_subb,
        gen_op_jz_subw,
        gen_op_jz_subl,
        X86_64_ONLY(gen_op_jz_subq),
B
bellard 已提交
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
    },
};

static GenOpFunc *gen_op_in_DX_T0[3] = {
    gen_op_inb_DX_T0,
    gen_op_inw_DX_T0,
    gen_op_inl_DX_T0,
};

static GenOpFunc *gen_op_out_DX_T0[3] = {
    gen_op_outb_DX_T0,
    gen_op_outw_DX_T0,
    gen_op_outl_DX_T0,
};

912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
static GenOpFunc *gen_op_in[3] = {
    gen_op_inb_T0_T1,
    gen_op_inw_T0_T1,
    gen_op_inl_T0_T1,
};

static GenOpFunc *gen_op_out[3] = {
    gen_op_outb_T0_T1,
    gen_op_outw_T0_T1,
    gen_op_outl_T0_T1,
};

static GenOpFunc *gen_check_io_T0[3] = {
    gen_op_check_iob_T0,
    gen_op_check_iow_T0,
    gen_op_check_iol_T0,
};

static GenOpFunc *gen_check_io_DX[3] = {
    gen_op_check_iob_DX,
    gen_op_check_iow_DX,
    gen_op_check_iol_DX,
};

B
bellard 已提交
936
static void gen_check_io(DisasContext *s, int ot, int use_dx, target_ulong cur_eip)
937 938 939 940
{
    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 已提交
941
        gen_jmp_im(cur_eip);
942 943 944 945 946 947 948
        if (use_dx)
            gen_check_io_DX[ot]();
        else
            gen_check_io_T0[ot]();
    }
}

B
bellard 已提交
949 950 951 952 953 954 955
static inline void gen_movs(DisasContext *s, int ot)
{
    gen_string_movl_A0_ESI(s);
    gen_op_ld_T0_A0[ot + s->mem_index]();
    gen_string_movl_A0_EDI(s);
    gen_op_st_T0_A0[ot + s->mem_index]();
    gen_op_movl_T0_Dshift[ot]();
B
bellard 已提交
956 957 958 959 960 961
#ifdef TARGET_X86_64
    if (s->aflag == 2) {
        gen_op_addq_ESI_T0();
        gen_op_addq_EDI_T0();
    } else 
#endif
B
bellard 已提交
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
    if (s->aflag) {
        gen_op_addl_ESI_T0();
        gen_op_addl_EDI_T0();
    } else {
        gen_op_addw_ESI_T0();
        gen_op_addw_EDI_T0();
    }
}

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

B
bellard 已提交
979 980 981
/* 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 已提交
982
{
B
bellard 已提交
983 984 985 986 987 988 989 990 991
    int l1, l2;

    l1 = gen_new_label();
    l2 = gen_new_label();
    gen_op_jnz_ecx[s->aflag](l1);
    gen_set_label(l2);
    gen_jmp_tb(s, next_eip, 1);
    gen_set_label(l1);
    return l2;
B
bellard 已提交
992 993 994 995 996 997 998 999
}

static inline void gen_stos(DisasContext *s, int ot)
{
    gen_op_mov_TN_reg[OT_LONG][0][R_EAX]();
    gen_string_movl_A0_EDI(s);
    gen_op_st_T0_A0[ot + s->mem_index]();
    gen_op_movl_T0_Dshift[ot]();
B
bellard 已提交
1000 1001 1002 1003 1004
#ifdef TARGET_X86_64
    if (s->aflag == 2) {
        gen_op_addq_EDI_T0();
    } else 
#endif
B
bellard 已提交
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
    if (s->aflag) {
        gen_op_addl_EDI_T0();
    } else {
        gen_op_addw_EDI_T0();
    }
}

static inline void gen_lods(DisasContext *s, int ot)
{
    gen_string_movl_A0_ESI(s);
    gen_op_ld_T0_A0[ot + s->mem_index]();
    gen_op_mov_reg_T0[ot][R_EAX]();
    gen_op_movl_T0_Dshift[ot]();
B
bellard 已提交
1018 1019 1020 1021 1022
#ifdef TARGET_X86_64
    if (s->aflag == 2) {
        gen_op_addq_ESI_T0();
    } else 
#endif
B
bellard 已提交
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
    if (s->aflag) {
        gen_op_addl_ESI_T0();
    } else {
        gen_op_addw_ESI_T0();
    }
}

static inline void gen_scas(DisasContext *s, int ot)
{
    gen_op_mov_TN_reg[OT_LONG][0][R_EAX]();
    gen_string_movl_A0_EDI(s);
    gen_op_ld_T1_A0[ot + s->mem_index]();
    gen_op_cmpl_T0_T1_cc();
    gen_op_movl_T0_Dshift[ot]();
B
bellard 已提交
1037 1038 1039 1040 1041
#ifdef TARGET_X86_64
    if (s->aflag == 2) {
        gen_op_addq_EDI_T0();
    } else 
#endif
B
bellard 已提交
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
    if (s->aflag) {
        gen_op_addl_EDI_T0();
    } else {
        gen_op_addw_EDI_T0();
    }
}

static inline void gen_cmps(DisasContext *s, int ot)
{
    gen_string_movl_A0_ESI(s);
    gen_op_ld_T0_A0[ot + s->mem_index]();
    gen_string_movl_A0_EDI(s);
    gen_op_ld_T1_A0[ot + s->mem_index]();
    gen_op_cmpl_T0_T1_cc();
    gen_op_movl_T0_Dshift[ot]();
B
bellard 已提交
1057 1058 1059 1060 1061 1062
#ifdef TARGET_X86_64
    if (s->aflag == 2) {
        gen_op_addq_ESI_T0();
        gen_op_addq_EDI_T0();
    } else 
#endif
B
bellard 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
    if (s->aflag) {
        gen_op_addl_ESI_T0();
        gen_op_addl_EDI_T0();
    } else {
        gen_op_addw_ESI_T0();
        gen_op_addw_EDI_T0();
    }
}

static inline void gen_ins(DisasContext *s, int ot)
{
    gen_string_movl_A0_EDI(s);
B
bellard 已提交
1075 1076 1077
    gen_op_movl_T0_0();
    gen_op_st_T0_A0[ot + s->mem_index]();
    gen_op_in_DX_T0[ot]();
B
bellard 已提交
1078 1079
    gen_op_st_T0_A0[ot + s->mem_index]();
    gen_op_movl_T0_Dshift[ot]();
B
bellard 已提交
1080 1081 1082 1083 1084
#ifdef TARGET_X86_64
    if (s->aflag == 2) {
        gen_op_addq_EDI_T0();
    } else 
#endif
B
bellard 已提交
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
    if (s->aflag) {
        gen_op_addl_EDI_T0();
    } else {
        gen_op_addw_EDI_T0();
    }
}

static inline void gen_outs(DisasContext *s, int ot)
{
    gen_string_movl_A0_ESI(s);
    gen_op_ld_T0_A0[ot + s->mem_index]();
    gen_op_out_DX_T0[ot]();
    gen_op_movl_T0_Dshift[ot]();
B
bellard 已提交
1098 1099 1100 1101 1102
#ifdef TARGET_X86_64
    if (s->aflag == 2) {
        gen_op_addq_ESI_T0();
    } else 
#endif
B
bellard 已提交
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
    if (s->aflag) {
        gen_op_addl_ESI_T0();
    } else {
        gen_op_addw_ESI_T0();
    }
}

/* 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 已提交
1114
                                 target_ulong cur_eip, target_ulong next_eip) \
B
bellard 已提交
1115
{                                                                             \
B
bellard 已提交
1116
    int l2;\
B
bellard 已提交
1117
    gen_update_cc_op(s);                                                      \
B
bellard 已提交
1118
    l2 = gen_jz_ecx_string(s, next_eip);                                      \
B
bellard 已提交
1119 1120 1121 1122 1123
    gen_ ## op(s, ot);                                                        \
    gen_op_dec_ECX[s->aflag]();                                               \
    /* a loop would cause two single step exceptions if ECX = 1               \
       before rep string_insn */                                              \
    if (!s->jmp_opt)                                                          \
B
bellard 已提交
1124
        gen_op_jz_ecx[s->aflag](l2);                                          \
B
bellard 已提交
1125 1126 1127 1128 1129
    gen_jmp(s, cur_eip);                                                      \
}

#define GEN_REPZ2(op)                                                         \
static inline void gen_repz_ ## op(DisasContext *s, int ot,                   \
B
bellard 已提交
1130 1131
                                   target_ulong cur_eip,                      \
                                   target_ulong next_eip,                     \
B
bellard 已提交
1132 1133
                                   int nz)                                    \
{                                                                             \
B
bellard 已提交
1134
    int l2;\
B
bellard 已提交
1135
    gen_update_cc_op(s);                                                      \
B
bellard 已提交
1136
    l2 = gen_jz_ecx_string(s, next_eip);                                      \
B
bellard 已提交
1137 1138 1139
    gen_ ## op(s, ot);                                                        \
    gen_op_dec_ECX[s->aflag]();                                               \
    gen_op_set_cc_op(CC_OP_SUBB + ot);                                        \
B
bellard 已提交
1140
    gen_op_string_jnz_sub[nz][ot](l2);\
B
bellard 已提交
1141
    if (!s->jmp_opt)                                                          \
B
bellard 已提交
1142
        gen_op_jz_ecx[s->aflag](l2);                                          \
B
bellard 已提交
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
    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)

enum {
    JCC_O,
    JCC_B,
    JCC_Z,
    JCC_BE,
    JCC_S,
    JCC_P,
    JCC_L,
    JCC_LE,
};

B
bellard 已提交
1165
static GenOpFunc1 *gen_jcc_sub[4][8] = {
B
bellard 已提交
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
    [OT_BYTE] = {
        NULL,
        gen_op_jb_subb,
        gen_op_jz_subb,
        gen_op_jbe_subb,
        gen_op_js_subb,
        NULL,
        gen_op_jl_subb,
        gen_op_jle_subb,
    },
    [OT_WORD] = {
        NULL,
        gen_op_jb_subw,
        gen_op_jz_subw,
        gen_op_jbe_subw,
        gen_op_js_subw,
        NULL,
        gen_op_jl_subw,
        gen_op_jle_subw,
    },
    [OT_LONG] = {
        NULL,
        gen_op_jb_subl,
        gen_op_jz_subl,
        gen_op_jbe_subl,
        gen_op_js_subl,
        NULL,
        gen_op_jl_subl,
        gen_op_jle_subl,
    },
B
bellard 已提交
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
#ifdef TARGET_X86_64
    [OT_QUAD] = {
        NULL,
        BUGGY_64(gen_op_jb_subq),
        gen_op_jz_subq,
        BUGGY_64(gen_op_jbe_subq),
        gen_op_js_subq,
        NULL,
        BUGGY_64(gen_op_jl_subq),
        BUGGY_64(gen_op_jle_subq),
    },
#endif
B
bellard 已提交
1208
};
B
bellard 已提交
1209
static GenOpFunc1 *gen_op_loop[3][4] = {
B
bellard 已提交
1210 1211 1212
    [0] = {
        gen_op_loopnzw,
        gen_op_loopzw,
B
bellard 已提交
1213
        gen_op_jnz_ecxw,
B
bellard 已提交
1214 1215 1216 1217
    },
    [1] = {
        gen_op_loopnzl,
        gen_op_loopzl,
B
bellard 已提交
1218 1219 1220 1221 1222 1223 1224
        gen_op_jnz_ecxl,
    },
#ifdef TARGET_X86_64
    [2] = {
        gen_op_loopnzq,
        gen_op_loopzq,
        gen_op_jnz_ecxq,
B
bellard 已提交
1225
    },
B
bellard 已提交
1226
#endif
B
bellard 已提交
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
};

static GenOpFunc *gen_setcc_slow[8] = {
    gen_op_seto_T0_cc,
    gen_op_setb_T0_cc,
    gen_op_setz_T0_cc,
    gen_op_setbe_T0_cc,
    gen_op_sets_T0_cc,
    gen_op_setp_T0_cc,
    gen_op_setl_T0_cc,
    gen_op_setle_T0_cc,
};

B
bellard 已提交
1240
static GenOpFunc *gen_setcc_sub[4][8] = {
B
bellard 已提交
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
    [OT_BYTE] = {
        NULL,
        gen_op_setb_T0_subb,
        gen_op_setz_T0_subb,
        gen_op_setbe_T0_subb,
        gen_op_sets_T0_subb,
        NULL,
        gen_op_setl_T0_subb,
        gen_op_setle_T0_subb,
    },
    [OT_WORD] = {
        NULL,
        gen_op_setb_T0_subw,
        gen_op_setz_T0_subw,
        gen_op_setbe_T0_subw,
        gen_op_sets_T0_subw,
        NULL,
        gen_op_setl_T0_subw,
        gen_op_setle_T0_subw,
    },
    [OT_LONG] = {
        NULL,
        gen_op_setb_T0_subl,
        gen_op_setz_T0_subl,
        gen_op_setbe_T0_subl,
        gen_op_sets_T0_subl,
        NULL,
        gen_op_setl_T0_subl,
        gen_op_setle_T0_subl,
    },
B
bellard 已提交
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
#ifdef TARGET_X86_64
    [OT_QUAD] = {
        NULL,
        gen_op_setb_T0_subq,
        gen_op_setz_T0_subq,
        gen_op_setbe_T0_subq,
        gen_op_sets_T0_subq,
        NULL,
        gen_op_setl_T0_subq,
        gen_op_setle_T0_subq,
    },
#endif
B
bellard 已提交
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
};

static GenOpFunc *gen_op_fp_arith_ST0_FT0[8] = {
    gen_op_fadd_ST0_FT0,
    gen_op_fmul_ST0_FT0,
    gen_op_fcom_ST0_FT0,
    gen_op_fcom_ST0_FT0,
    gen_op_fsub_ST0_FT0,
    gen_op_fsubr_ST0_FT0,
    gen_op_fdiv_ST0_FT0,
    gen_op_fdivr_ST0_FT0,
};

/* NOTE the exception in "r" op ordering */
static GenOpFunc1 *gen_op_fp_arith_STN_ST0[8] = {
    gen_op_fadd_STN_ST0,
    gen_op_fmul_STN_ST0,
    NULL,
    NULL,
    gen_op_fsubr_STN_ST0,
    gen_op_fsub_STN_ST0,
    gen_op_fdivr_STN_ST0,
    gen_op_fdiv_STN_ST0,
};

/* if d == OR_TMP0, it means memory operand (address in A0) */
static void gen_op(DisasContext *s1, int op, int ot, int d)
{
    GenOpFunc *gen_update_cc;
    
    if (d != OR_TMP0) {
        gen_op_mov_TN_reg[ot][0][d]();
    } else {
        gen_op_ld_T0_A0[ot + s1->mem_index]();
    }
    switch(op) {
    case OP_ADCL:
    case OP_SBBL:
        if (s1->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s1->cc_op);
        if (d != OR_TMP0) {
            gen_op_arithc_T0_T1_cc[ot][op - OP_ADCL]();
            gen_op_mov_reg_T0[ot][d]();
        } else {
1327
            gen_op_arithc_mem_T0_T1_cc[ot + s1->mem_index][op - OP_ADCL]();
B
bellard 已提交
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
        }
        s1->cc_op = CC_OP_DYNAMIC;
        goto the_end;
    case OP_ADDL:
        gen_op_addl_T0_T1();
        s1->cc_op = CC_OP_ADDB + ot;
        gen_update_cc = gen_op_update2_cc;
        break;
    case OP_SUBL:
        gen_op_subl_T0_T1();
        s1->cc_op = CC_OP_SUBB + ot;
        gen_update_cc = gen_op_update2_cc;
        break;
    default:
    case OP_ANDL:
    case OP_ORL:
    case OP_XORL:
        gen_op_arith_T0_T1_cc[op]();
        s1->cc_op = CC_OP_LOGICB + ot;
        gen_update_cc = gen_op_update1_cc;
        break;
    case OP_CMPL:
        gen_op_cmpl_T0_T1_cc();
        s1->cc_op = CC_OP_SUBB + ot;
        gen_update_cc = NULL;
        break;
    }
    if (op != OP_CMPL) {
        if (d != OR_TMP0)
            gen_op_mov_reg_T0[ot][d]();
        else
            gen_op_st_T0_A0[ot + s1->mem_index]();
    }
    /* the flags update must happen after the memory write (precise
       exception support) */
    if (gen_update_cc)
        gen_update_cc();
 the_end: ;
}

/* 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)
        gen_op_mov_TN_reg[ot][0][d]();
    else
        gen_op_ld_T0_A0[ot + s1->mem_index]();
    if (s1->cc_op != CC_OP_DYNAMIC)
        gen_op_set_cc_op(s1->cc_op);
    if (c > 0) {
        gen_op_incl_T0();
        s1->cc_op = CC_OP_INCB + ot;
    } else {
        gen_op_decl_T0();
        s1->cc_op = CC_OP_DECB + ot;
    }
    if (d != OR_TMP0)
        gen_op_mov_reg_T0[ot][d]();
    else
        gen_op_st_T0_A0[ot + s1->mem_index]();
    gen_op_update_inc_cc();
}

static void gen_shift(DisasContext *s1, int op, int ot, int d, int s)
{
    if (d != OR_TMP0)
        gen_op_mov_TN_reg[ot][0][d]();
    else
        gen_op_ld_T0_A0[ot + s1->mem_index]();
    if (s != OR_TMP1)
        gen_op_mov_TN_reg[ot][1][s]();
    /* for zero counts, flags are not updated, so must do it dynamically */
    if (s1->cc_op != CC_OP_DYNAMIC)
        gen_op_set_cc_op(s1->cc_op);
    
    if (d != OR_TMP0)
        gen_op_shift_T0_T1_cc[ot][op]();
    else
1406
        gen_op_shift_mem_T0_T1_cc[ot + s1->mem_index][op]();
B
bellard 已提交
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
    if (d != OR_TMP0)
        gen_op_mov_reg_T0[ot][d]();
    s1->cc_op = CC_OP_DYNAMIC; /* cannot predict flags after */
}

static void gen_shifti(DisasContext *s1, int op, int ot, int d, int c)
{
    /* currently not optimized */
    gen_op_movl_T1_im(c);
    gen_shift(s1, op, ot, d, OR_TMP1);
}

static void gen_lea_modrm(DisasContext *s, int modrm, int *reg_ptr, int *offset_ptr)
{
B
bellard 已提交
1421
    target_long disp;
B
bellard 已提交
1422
    int havesib;
B
bellard 已提交
1423
    int base;
B
bellard 已提交
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
    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;
        
        if (base == 4) {
            havesib = 1;
B
bellard 已提交
1445
            code = ldub_code(s->pc++);
B
bellard 已提交
1446
            scale = (code >> 6) & 3;
B
bellard 已提交
1447 1448
            index = ((code >> 3) & 7) | REX_X(s);
            base = (code & 7);
B
bellard 已提交
1449
        }
B
bellard 已提交
1450
        base |= REX_B(s);
B
bellard 已提交
1451 1452 1453

        switch (mod) {
        case 0:
B
bellard 已提交
1454
            if ((base & 7) == 5) {
B
bellard 已提交
1455
                base = -1;
B
bellard 已提交
1456
                disp = (int32_t)ldl_code(s->pc);
B
bellard 已提交
1457
                s->pc += 4;
B
bellard 已提交
1458 1459 1460
                if (CODE64(s) && !havesib) {
                    disp += s->pc + s->rip_offset;
                }
B
bellard 已提交
1461 1462 1463 1464 1465
            } else {
                disp = 0;
            }
            break;
        case 1:
B
bellard 已提交
1466
            disp = (int8_t)ldub_code(s->pc++);
B
bellard 已提交
1467 1468 1469
            break;
        default:
        case 2:
B
bellard 已提交
1470
            disp = ldl_code(s->pc);
B
bellard 已提交
1471 1472 1473 1474 1475 1476 1477 1478
            s->pc += 4;
            break;
        }
        
        if (base >= 0) {
            /* for correct popl handling with esp */
            if (base == 4 && s->popl_esp_hack)
                disp += s->popl_esp_hack;
B
bellard 已提交
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
#ifdef TARGET_X86_64
            if (s->aflag == 2) {
                gen_op_movq_A0_reg[base]();
                if (disp != 0) {
                    if ((int32_t)disp == disp)
                        gen_op_addq_A0_im(disp);
                    else
                        gen_op_addq_A0_im64(disp >> 32, disp);
                }
            } else 
#endif
            {
                gen_op_movl_A0_reg[base]();
                if (disp != 0)
                    gen_op_addl_A0_im(disp);
            }
B
bellard 已提交
1495
        } else {
B
bellard 已提交
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
#ifdef TARGET_X86_64
            if (s->aflag == 2) {
                if ((int32_t)disp == disp)
                    gen_op_movq_A0_im(disp);
                else
                    gen_op_movq_A0_im64(disp >> 32, disp);
            } else 
#endif
            {
                gen_op_movl_A0_im(disp);
            }
B
bellard 已提交
1507 1508 1509
        }
        /* XXX: index == 4 is always invalid */
        if (havesib && (index != 4 || scale != 0)) {
B
bellard 已提交
1510 1511 1512 1513 1514 1515 1516 1517
#ifdef TARGET_X86_64
            if (s->aflag == 2) {
                gen_op_addq_A0_reg_sN[scale][index]();
            } else 
#endif
            {
                gen_op_addl_A0_reg_sN[scale][index]();
            }
B
bellard 已提交
1518 1519 1520 1521 1522 1523 1524 1525
        }
        if (must_add_seg) {
            if (override < 0) {
                if (base == R_EBP || base == R_ESP)
                    override = R_SS;
                else
                    override = R_DS;
            }
B
bellard 已提交
1526 1527 1528 1529 1530 1531 1532 1533
#ifdef TARGET_X86_64
            if (s->aflag == 2) {
                gen_op_addq_A0_seg(offsetof(CPUX86State,segs[override].base));
            } else 
#endif
            {
                gen_op_addl_A0_seg(offsetof(CPUX86State,segs[override].base));
            }
B
bellard 已提交
1534 1535 1536 1537 1538
        }
    } else {
        switch (mod) {
        case 0:
            if (rm == 6) {
B
bellard 已提交
1539
                disp = lduw_code(s->pc);
B
bellard 已提交
1540 1541 1542 1543 1544 1545 1546 1547 1548
                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 已提交
1549
            disp = (int8_t)ldub_code(s->pc++);
B
bellard 已提交
1550 1551 1552
            break;
        default:
        case 2:
B
bellard 已提交
1553
            disp = lduw_code(s->pc);
B
bellard 已提交
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
            s->pc += 2;
            break;
        }
        switch(rm) {
        case 0:
            gen_op_movl_A0_reg[R_EBX]();
            gen_op_addl_A0_reg_sN[0][R_ESI]();
            break;
        case 1:
            gen_op_movl_A0_reg[R_EBX]();
            gen_op_addl_A0_reg_sN[0][R_EDI]();
            break;
        case 2:
            gen_op_movl_A0_reg[R_EBP]();
            gen_op_addl_A0_reg_sN[0][R_ESI]();
            break;
        case 3:
            gen_op_movl_A0_reg[R_EBP]();
            gen_op_addl_A0_reg_sN[0][R_EDI]();
            break;
        case 4:
            gen_op_movl_A0_reg[R_ESI]();
            break;
        case 5:
            gen_op_movl_A0_reg[R_EDI]();
            break;
        case 6:
            gen_op_movl_A0_reg[R_EBP]();
            break;
        default:
        case 7:
            gen_op_movl_A0_reg[R_EBX]();
            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;
            }
            gen_op_addl_A0_seg(offsetof(CPUX86State,segs[override].base));
        }
    }

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

/* generate modrm memory load or store of 'reg'. TMP0 is used if reg !=
   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 已提交
1616
    rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646
    if (mod == 3) {
        if (is_store) {
            if (reg != OR_TMP0)
                gen_op_mov_TN_reg[ot][0][reg]();
            gen_op_mov_reg_T0[ot][rm]();
        } else {
            gen_op_mov_TN_reg[ot][0][rm]();
            if (reg != OR_TMP0)
                gen_op_mov_reg_T0[ot][reg]();
        }
    } else {
        gen_lea_modrm(s, modrm, &opreg, &disp);
        if (is_store) {
            if (reg != OR_TMP0)
                gen_op_mov_TN_reg[ot][0][reg]();
            gen_op_st_T0_A0[ot + s->mem_index]();
        } else {
            gen_op_ld_T0_A0[ot + s->mem_index]();
            if (reg != OR_TMP0)
                gen_op_mov_reg_T0[ot][reg]();
        }
    }
}

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

    switch(ot) {
    case OT_BYTE:
B
bellard 已提交
1647
        ret = ldub_code(s->pc);
B
bellard 已提交
1648 1649 1650
        s->pc++;
        break;
    case OT_WORD:
B
bellard 已提交
1651
        ret = lduw_code(s->pc);
B
bellard 已提交
1652 1653 1654 1655
        s->pc += 2;
        break;
    default:
    case OT_LONG:
B
bellard 已提交
1656
        ret = ldl_code(s->pc);
B
bellard 已提交
1657 1658 1659 1660 1661 1662
        s->pc += 4;
        break;
    }
    return ret;
}

B
bellard 已提交
1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
static inline int insn_const_size(unsigned int ot)
{
    if (ot <= OT_LONG)
        return 1 << ot;
    else
        return 4;
}

static inline void gen_jcc(DisasContext *s, int b, 
                           target_ulong val, target_ulong next_eip)
B
bellard 已提交
1673 1674 1675
{
    TranslationBlock *tb;
    int inv, jcc_op;
B
bellard 已提交
1676 1677 1678
    GenOpFunc1 *func;
    target_ulong tmp;
    int l1, l2;
B
bellard 已提交
1679 1680 1681 1682 1683 1684 1685 1686 1687 1688

    inv = b & 1;
    jcc_op = (b >> 1) & 7;
    
    if (s->jmp_opt) {
        switch(s->cc_op) {
            /* we optimize the cmp/jcc case */
        case CC_OP_SUBB:
        case CC_OP_SUBW:
        case CC_OP_SUBL:
B
bellard 已提交
1689
        case CC_OP_SUBQ:
B
bellard 已提交
1690 1691 1692 1693 1694 1695 1696
            func = gen_jcc_sub[s->cc_op - CC_OP_SUBB][jcc_op];
            break;
            
            /* some jumps are easy to compute */
        case CC_OP_ADDB:
        case CC_OP_ADDW:
        case CC_OP_ADDL:
B
bellard 已提交
1697 1698
        case CC_OP_ADDQ:

B
bellard 已提交
1699 1700 1701
        case CC_OP_ADCB:
        case CC_OP_ADCW:
        case CC_OP_ADCL:
B
bellard 已提交
1702 1703
        case CC_OP_ADCQ:

B
bellard 已提交
1704 1705 1706
        case CC_OP_SBBB:
        case CC_OP_SBBW:
        case CC_OP_SBBL:
B
bellard 已提交
1707 1708
        case CC_OP_SBBQ:

B
bellard 已提交
1709 1710 1711
        case CC_OP_LOGICB:
        case CC_OP_LOGICW:
        case CC_OP_LOGICL:
B
bellard 已提交
1712 1713
        case CC_OP_LOGICQ:

B
bellard 已提交
1714 1715 1716
        case CC_OP_INCB:
        case CC_OP_INCW:
        case CC_OP_INCL:
B
bellard 已提交
1717 1718
        case CC_OP_INCQ:

B
bellard 已提交
1719 1720 1721
        case CC_OP_DECB:
        case CC_OP_DECW:
        case CC_OP_DECL:
B
bellard 已提交
1722 1723
        case CC_OP_DECQ:

B
bellard 已提交
1724 1725 1726
        case CC_OP_SHLB:
        case CC_OP_SHLW:
        case CC_OP_SHLL:
B
bellard 已提交
1727 1728
        case CC_OP_SHLQ:

B
bellard 已提交
1729 1730 1731
        case CC_OP_SARB:
        case CC_OP_SARW:
        case CC_OP_SARL:
B
bellard 已提交
1732
        case CC_OP_SARQ:
B
bellard 已提交
1733 1734
            switch(jcc_op) {
            case JCC_Z:
B
bellard 已提交
1735
                func = gen_jcc_sub[(s->cc_op - CC_OP_ADDB) % 4][jcc_op];
B
bellard 已提交
1736 1737
                break;
            case JCC_S:
B
bellard 已提交
1738
                func = gen_jcc_sub[(s->cc_op - CC_OP_ADDB) % 4][jcc_op];
B
bellard 已提交
1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754
                break;
            default:
                func = NULL;
                break;
            }
            break;
        default:
            func = NULL;
            break;
        }

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

        if (!func) {
            gen_setcc_slow[jcc_op]();
B
bellard 已提交
1755
            func = gen_op_jnz_T0_label;
B
bellard 已提交
1756 1757
        }
    
B
bellard 已提交
1758 1759 1760 1761
        if (inv) {
            tmp = val;
            val = next_eip;
            next_eip = tmp;
B
bellard 已提交
1762
        }
B
bellard 已提交
1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778
        tb = s->tb;

        l1 = gen_new_label();
        func(l1);

        gen_op_goto_tb0();
        gen_jmp_im(next_eip);
        gen_op_movl_T0_im((long)tb + 0);
        gen_op_exit_tb();

        gen_set_label(l1);
        gen_op_goto_tb1();
        gen_jmp_im(val);
        gen_op_movl_T0_im((long)tb + 1);
        gen_op_exit_tb();

B
bellard 已提交
1779 1780
        s->is_jmp = 3;
    } else {
B
bellard 已提交
1781

B
bellard 已提交
1782 1783 1784 1785 1786
        if (s->cc_op != CC_OP_DYNAMIC) {
            gen_op_set_cc_op(s->cc_op);
            s->cc_op = CC_OP_DYNAMIC;
        }
        gen_setcc_slow[jcc_op]();
B
bellard 已提交
1787 1788 1789 1790
        if (inv) {
            tmp = val;
            val = next_eip;
            next_eip = tmp;
B
bellard 已提交
1791
        }
B
bellard 已提交
1792 1793 1794 1795 1796 1797 1798 1799
        l1 = gen_new_label();
        l2 = gen_new_label();
        gen_op_jnz_T0_label(l1);
        gen_jmp_im(next_eip);
        gen_op_jmp_label(l2);
        gen_set_label(l1);
        gen_jmp_im(val);
        gen_set_label(l2);
B
bellard 已提交
1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
        gen_eob(s);
    }
}

static void gen_setcc(DisasContext *s, int b)
{
    int inv, jcc_op;
    GenOpFunc *func;

    inv = b & 1;
    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:
B
bellard 已提交
1816
    case CC_OP_SUBQ:
B
bellard 已提交
1817 1818 1819 1820 1821 1822 1823 1824 1825
        func = gen_setcc_sub[s->cc_op - CC_OP_SUBB][jcc_op];
        if (!func)
            goto slow_jcc;
        break;
        
        /* some jumps are easy to compute */
    case CC_OP_ADDB:
    case CC_OP_ADDW:
    case CC_OP_ADDL:
B
bellard 已提交
1826 1827
    case CC_OP_ADDQ:

B
bellard 已提交
1828 1829 1830
    case CC_OP_LOGICB:
    case CC_OP_LOGICW:
    case CC_OP_LOGICL:
B
bellard 已提交
1831 1832
    case CC_OP_LOGICQ:

B
bellard 已提交
1833 1834 1835
    case CC_OP_INCB:
    case CC_OP_INCW:
    case CC_OP_INCL:
B
bellard 已提交
1836 1837
    case CC_OP_INCQ:

B
bellard 已提交
1838 1839 1840
    case CC_OP_DECB:
    case CC_OP_DECW:
    case CC_OP_DECL:
B
bellard 已提交
1841 1842
    case CC_OP_DECQ:

B
bellard 已提交
1843 1844 1845
    case CC_OP_SHLB:
    case CC_OP_SHLW:
    case CC_OP_SHLL:
B
bellard 已提交
1846
    case CC_OP_SHLQ:
B
bellard 已提交
1847 1848
        switch(jcc_op) {
        case JCC_Z:
B
bellard 已提交
1849
            func = gen_setcc_sub[(s->cc_op - CC_OP_ADDB) % 4][jcc_op];
B
bellard 已提交
1850 1851
            break;
        case JCC_S:
B
bellard 已提交
1852
            func = gen_setcc_sub[(s->cc_op - CC_OP_ADDB) % 4][jcc_op];
B
bellard 已提交
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872
            break;
        default:
            goto slow_jcc;
        }
        break;
    default:
    slow_jcc:
        if (s->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s->cc_op);
        func = gen_setcc_slow[jcc_op];
        break;
    }
    func();
    if (inv) {
        gen_op_xor_T0_1();
    }
}

/* move T0 to seg_reg and compute if the CPU state may change. Never
   call this function with seg_reg == R_CS */
B
bellard 已提交
1873
static void gen_movl_seg_T0(DisasContext *s, int seg_reg, target_ulong cur_eip)
B
bellard 已提交
1874
{
1875 1876 1877 1878
    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 已提交
1879
        gen_jmp_im(cur_eip);
1880
        gen_op_movl_seg_T0(seg_reg);
B
bellard 已提交
1881 1882 1883 1884 1885 1886
        /* 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))
            s->is_jmp = 3;
1887
    } else {
B
bellard 已提交
1888
        gen_op_movl_seg_T0_vm(offsetof(CPUX86State,segs[seg_reg]));
B
bellard 已提交
1889 1890
        if (seg_reg == R_SS)
            s->is_jmp = 3;
1891
    }
B
bellard 已提交
1892 1893
}

1894 1895
static inline void gen_stack_update(DisasContext *s, int addend)
{
B
bellard 已提交
1896 1897 1898 1899 1900 1901 1902 1903
#ifdef TARGET_X86_64
    if (CODE64(s)) {
        if (addend == 8)
            gen_op_addq_ESP_8();
        else 
            gen_op_addq_ESP_im(addend);
    } else
#endif
1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920
    if (s->ss32) {
        if (addend == 2)
            gen_op_addl_ESP_2();
        else if (addend == 4)
            gen_op_addl_ESP_4();
        else 
            gen_op_addl_ESP_im(addend);
    } else {
        if (addend == 2)
            gen_op_addw_ESP_2();
        else if (addend == 4)
            gen_op_addw_ESP_4();
        else
            gen_op_addw_ESP_im(addend);
    }
}

B
bellard 已提交
1921 1922 1923
/* generate a push. It depends on ss32, addseg and dflag */
static void gen_push_T0(DisasContext *s)
{
B
bellard 已提交
1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
#ifdef TARGET_X86_64
    if (CODE64(s)) {
        /* XXX: check 16 bit behaviour */
        gen_op_movq_A0_reg[R_ESP]();
        gen_op_subq_A0_8();
        gen_op_st_T0_A0[OT_QUAD + s->mem_index]();
        gen_op_movq_ESP_A0();
    } else 
#endif
    {
        gen_op_movl_A0_reg[R_ESP]();
        if (!s->dflag)
            gen_op_subl_A0_2();
        else
            gen_op_subl_A0_4();
        if (s->ss32) {
            if (s->addseg) {
                gen_op_movl_T1_A0();
                gen_op_addl_A0_SS();
            }
        } else {
            gen_op_andl_A0_ffff();
1946 1947
            gen_op_movl_T1_A0();
            gen_op_addl_A0_SS();
B
bellard 已提交
1948
        }
B
bellard 已提交
1949 1950 1951 1952 1953
        gen_op_st_T0_A0[s->dflag + 1 + s->mem_index]();
        if (s->ss32 && !s->addseg)
            gen_op_movl_ESP_A0();
        else
            gen_op_mov_reg_T1[s->ss32 + 1][R_ESP]();
B
bellard 已提交
1954 1955 1956
    }
}

1957 1958 1959
/* 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 已提交
1960
{
B
bellard 已提交
1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981
#ifdef TARGET_X86_64
    if (CODE64(s)) {
        /* XXX: check 16 bit behaviour */
        gen_op_movq_A0_reg[R_ESP]();
        gen_op_subq_A0_8();
        gen_op_st_T1_A0[OT_QUAD + s->mem_index]();
        gen_op_movq_ESP_A0();
    } else 
#endif
    {
        gen_op_movl_A0_reg[R_ESP]();
        if (!s->dflag)
            gen_op_subl_A0_2();
        else
            gen_op_subl_A0_4();
        if (s->ss32) {
            if (s->addseg) {
                gen_op_addl_A0_SS();
            }
        } else {
            gen_op_andl_A0_ffff();
1982
            gen_op_addl_A0_SS();
B
bellard 已提交
1983
        }
B
bellard 已提交
1984 1985 1986 1987 1988 1989
        gen_op_st_T1_A0[s->dflag + 1 + s->mem_index]();
        
        if (s->ss32 && !s->addseg)
            gen_op_movl_ESP_A0();
        else
            gen_stack_update(s, (-2) << s->dflag);
B
bellard 已提交
1990 1991 1992
    }
}

1993 1994
/* two step pop is necessary for precise exceptions */
static void gen_pop_T0(DisasContext *s)
B
bellard 已提交
1995
{
B
bellard 已提交
1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
#ifdef TARGET_X86_64
    if (CODE64(s)) {
        /* XXX: check 16 bit behaviour */
        gen_op_movq_A0_reg[R_ESP]();
        gen_op_ld_T0_A0[OT_QUAD + s->mem_index]();
    } else 
#endif
    {
        gen_op_movl_A0_reg[R_ESP]();
        if (s->ss32) {
            if (s->addseg)
                gen_op_addl_A0_SS();
        } else {
            gen_op_andl_A0_ffff();
2010
            gen_op_addl_A0_SS();
B
bellard 已提交
2011 2012
        }
        gen_op_ld_T0_A0[s->dflag + 1 + s->mem_index]();
B
bellard 已提交
2013 2014 2015 2016 2017
    }
}

static void gen_pop_update(DisasContext *s)
{
B
bellard 已提交
2018 2019 2020 2021 2022 2023 2024 2025
#ifdef TARGET_X86_64
    if (CODE64(s)) {
        gen_stack_update(s, 8);
    } else
#endif
    {
        gen_stack_update(s, 2 << s->dflag);
    }
B
bellard 已提交
2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080
}

static void gen_stack_A0(DisasContext *s)
{
    gen_op_movl_A0_ESP();
    if (!s->ss32)
        gen_op_andl_A0_ffff();
    gen_op_movl_T1_A0();
    if (s->addseg)
        gen_op_addl_A0_seg(offsetof(CPUX86State,segs[R_SS].base));
}

/* NOTE: wrap around in 16 bit not fully handled */
static void gen_pusha(DisasContext *s)
{
    int i;
    gen_op_movl_A0_ESP();
    gen_op_addl_A0_im(-16 <<  s->dflag);
    if (!s->ss32)
        gen_op_andl_A0_ffff();
    gen_op_movl_T1_A0();
    if (s->addseg)
        gen_op_addl_A0_seg(offsetof(CPUX86State,segs[R_SS].base));
    for(i = 0;i < 8; i++) {
        gen_op_mov_TN_reg[OT_LONG][0][7 - i]();
        gen_op_st_T0_A0[OT_WORD + s->dflag + s->mem_index]();
        gen_op_addl_A0_im(2 <<  s->dflag);
    }
    gen_op_mov_reg_T1[OT_WORD + s->dflag][R_ESP]();
}

/* NOTE: wrap around in 16 bit not fully handled */
static void gen_popa(DisasContext *s)
{
    int i;
    gen_op_movl_A0_ESP();
    if (!s->ss32)
        gen_op_andl_A0_ffff();
    gen_op_movl_T1_A0();
    gen_op_addl_T1_im(16 <<  s->dflag);
    if (s->addseg)
        gen_op_addl_A0_seg(offsetof(CPUX86State,segs[R_SS].base));
    for(i = 0;i < 8; i++) {
        /* ESP is not reloaded */
        if (i != 3) {
            gen_op_ld_T0_A0[OT_WORD + s->dflag + s->mem_index]();
            gen_op_mov_reg_T0[OT_WORD + s->dflag][7 - i]();
        }
        gen_op_addl_A0_im(2 <<  s->dflag);
    }
    gen_op_mov_reg_T1[OT_WORD + s->dflag][R_ESP]();
}

static void gen_enter(DisasContext *s, int esp_addend, int level)
{
B
bellard 已提交
2081
    int ot, opsize;
B
bellard 已提交
2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097

    ot = s->dflag + OT_WORD;
    level &= 0x1f;
    opsize = 2 << s->dflag;

    gen_op_movl_A0_ESP();
    gen_op_addl_A0_im(-opsize);
    if (!s->ss32)
        gen_op_andl_A0_ffff();
    gen_op_movl_T1_A0();
    if (s->addseg)
        gen_op_addl_A0_seg(offsetof(CPUX86State,segs[R_SS].base));
    /* push bp */
    gen_op_mov_TN_reg[OT_LONG][0][R_EBP]();
    gen_op_st_T0_A0[ot + s->mem_index]();
    if (level) {
B
bellard 已提交
2098
        gen_op_enter_level(level, s->dflag);
B
bellard 已提交
2099 2100
    }
    gen_op_mov_reg_T1[ot][R_EBP]();
B
bellard 已提交
2101
    gen_op_addl_T1_im( -esp_addend + (-opsize * level) );
B
bellard 已提交
2102 2103 2104
    gen_op_mov_reg_T1[ot][R_ESP]();
}

B
bellard 已提交
2105
static void gen_exception(DisasContext *s, int trapno, target_ulong cur_eip)
B
bellard 已提交
2106 2107 2108
{
    if (s->cc_op != CC_OP_DYNAMIC)
        gen_op_set_cc_op(s->cc_op);
B
bellard 已提交
2109
    gen_jmp_im(cur_eip);
B
bellard 已提交
2110 2111 2112 2113 2114 2115 2116
    gen_op_raise_exception(trapno);
    s->is_jmp = 3;
}

/* an interrupt is different from an exception because of the
   priviledge checks */
static void gen_interrupt(DisasContext *s, int intno, 
B
bellard 已提交
2117
                          target_ulong cur_eip, target_ulong next_eip)
B
bellard 已提交
2118 2119 2120
{
    if (s->cc_op != CC_OP_DYNAMIC)
        gen_op_set_cc_op(s->cc_op);
B
bellard 已提交
2121
    gen_jmp_im(cur_eip);
2122
    gen_op_raise_interrupt(intno, (int)(next_eip - cur_eip));
B
bellard 已提交
2123 2124 2125
    s->is_jmp = 3;
}

B
bellard 已提交
2126
static void gen_debug(DisasContext *s, target_ulong cur_eip)
B
bellard 已提交
2127 2128 2129
{
    if (s->cc_op != CC_OP_DYNAMIC)
        gen_op_set_cc_op(s->cc_op);
B
bellard 已提交
2130
    gen_jmp_im(cur_eip);
B
bellard 已提交
2131 2132 2133 2134 2135 2136 2137 2138 2139 2140
    gen_op_debug();
    s->is_jmp = 3;
}

/* 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);
2141 2142 2143
    if (s->tb->flags & HF_INHIBIT_IRQ_MASK) {
        gen_op_reset_inhibit_irq();
    }
2144 2145 2146
    if (s->singlestep_enabled) {
        gen_op_debug();
    } else if (s->tf) {
B
bellard 已提交
2147 2148 2149 2150 2151 2152 2153 2154 2155 2156
        gen_op_raise_exception(EXCP01_SSTP);
    } else {
        gen_op_movl_T0_0();
        gen_op_exit_tb();
    }
    s->is_jmp = 3;
}

/* generate a jump to eip. No segment change must happen before as a
   direct call to the next block may occur */
B
bellard 已提交
2157
static void gen_jmp_tb(DisasContext *s, target_ulong eip, int tb_num)
B
bellard 已提交
2158 2159 2160 2161 2162 2163
{
    TranslationBlock *tb = s->tb;

    if (s->jmp_opt) {
        if (s->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s->cc_op);
B
bellard 已提交
2164 2165 2166 2167 2168 2169 2170
        if (tb_num)
            gen_op_goto_tb1();
        else
            gen_op_goto_tb0();
        gen_jmp_im(eip);
        gen_op_movl_T0_im((long)tb + tb_num);
        gen_op_exit_tb();
B
bellard 已提交
2171 2172
        s->is_jmp = 3;
    } else {
B
bellard 已提交
2173
        gen_jmp_im(eip);
B
bellard 已提交
2174 2175 2176 2177
        gen_eob(s);
    }
}

B
bellard 已提交
2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211
static void gen_jmp(DisasContext *s, target_ulong eip)
{
    gen_jmp_tb(s, eip, 0);
}

static void gen_movtl_T0_im(target_ulong val)
{
#ifdef TARGET_X86_64    
    if ((int32_t)val == val) {
        gen_op_movl_T0_im(val);
    } else {
        gen_op_movq_T0_im64(val >> 32, val);
    }
#else
    gen_op_movl_T0_im(val);
#endif
}

static GenOpFunc1 *gen_ldo_env_A0[3] = {
    gen_op_ldo_raw_env_A0,
#ifndef CONFIG_USER_ONLY
    gen_op_ldo_kernel_env_A0,
    gen_op_ldo_user_env_A0,
#endif
};

static GenOpFunc1 *gen_sto_env_A0[3] = {
    gen_op_sto_raw_env_A0,
#ifndef CONFIG_USER_ONLY
    gen_op_sto_kernel_env_A0,
    gen_op_sto_user_env_A0,
#endif
};

B
bellard 已提交
2212 2213
/* convert one instruction. s->is_jmp is set if the translation must
   be stopped. Return the next pc value */
B
bellard 已提交
2214
static target_ulong disas_insn(DisasContext *s, target_ulong pc_start)
B
bellard 已提交
2215 2216 2217 2218
{
    int b, prefixes, aflag, dflag;
    int shift, ot;
    int modrm, reg, rm, mod, reg_addr, op, opreg, offset_addr, val;
B
bellard 已提交
2219 2220
    target_ulong next_eip, tval;
    int rex_w, rex_r;
B
bellard 已提交
2221 2222 2223 2224 2225 2226

    s->pc = pc_start;
    prefixes = 0;
    aflag = s->code32;
    dflag = s->code32;
    s->override = -1;
B
bellard 已提交
2227 2228 2229 2230 2231 2232 2233 2234
    rex_w = -1;
    rex_r = 0;
#ifdef TARGET_X86_64
    s->rex_x = 0;
    s->rex_b = 0;
    x86_64_hregs = 0; 
#endif
    s->rip_offset = 0; /* for relative ip address */
B
bellard 已提交
2235
 next_byte:
B
bellard 已提交
2236
    b = ldub_code(s->pc);
B
bellard 已提交
2237 2238
    s->pc++;
    /* check prefixes */
B
bellard 已提交
2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334
#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;
    } else 
#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 已提交
2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350
    }

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

    /* lock generation */
    if (prefixes & PREFIX_LOCK)
        gen_op_lock();

    /* now check op code */
 reswitch:
    switch(b) {
    case 0x0f:
        /**************************/
        /* extended op code */
B
bellard 已提交
2351
        b = ldub_code(s->pc++) | 0x100;
B
bellard 已提交
2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371
        goto reswitch;
        
        /**************************/
        /* 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 已提交
2372
                ot = dflag + OT_WORD;
B
bellard 已提交
2373 2374 2375
            
            switch(f) {
            case 0: /* OP Ev, Gv */
B
bellard 已提交
2376
                modrm = ldub_code(s->pc++);
B
bellard 已提交
2377
                reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
2378
                mod = (modrm >> 6) & 3;
B
bellard 已提交
2379
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397
                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;
                    gen_op_mov_reg_T0[ot][reg]();
                    gen_op_update1_cc();
                    break;
                } else {
                    opreg = rm;
                }
                gen_op_mov_TN_reg[ot][1][reg]();
                gen_op(s, op, ot, opreg);
                break;
            case 1: /* OP Gv, Ev */
B
bellard 已提交
2398
                modrm = ldub_code(s->pc++);
B
bellard 已提交
2399
                mod = (modrm >> 6) & 3;
B
bellard 已提交
2400 2401
                reg = ((modrm >> 3) & 7) | rex_r;
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422
                if (mod != 3) {
                    gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
                    gen_op_ld_T1_A0[ot + s->mem_index]();
                } else if (op == OP_XORL && rm == reg) {
                    goto xor_zero;
                } else {
                    gen_op_mov_TN_reg[ot][1][rm]();
                }
                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;

    case 0x80: /* GRP1 */
    case 0x81:
2423
    case 0x82:
B
bellard 已提交
2424 2425 2426 2427 2428 2429 2430
    case 0x83:
        {
            int val;

            if ((b & 1) == 0)
                ot = OT_BYTE;
            else
B
bellard 已提交
2431
                ot = dflag + OT_WORD;
B
bellard 已提交
2432
            
B
bellard 已提交
2433
            modrm = ldub_code(s->pc++);
B
bellard 已提交
2434
            mod = (modrm >> 6) & 3;
B
bellard 已提交
2435
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
2436 2437 2438
            op = (modrm >> 3) & 7;
            
            if (mod != 3) {
B
bellard 已提交
2439 2440 2441 2442
                if (b == 0x83)
                    s->rip_offset = 1;
                else
                    s->rip_offset = insn_const_size(ot);
B
bellard 已提交
2443 2444 2445
                gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
                opreg = OR_TMP0;
            } else {
B
bellard 已提交
2446
                opreg = rm;
B
bellard 已提交
2447 2448 2449 2450 2451 2452
            }

            switch(b) {
            default:
            case 0x80:
            case 0x81:
2453
            case 0x82:
B
bellard 已提交
2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479
                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 已提交
2480
            ot = dflag + OT_WORD;
B
bellard 已提交
2481

B
bellard 已提交
2482
        modrm = ldub_code(s->pc++);
B
bellard 已提交
2483
        mod = (modrm >> 6) & 3;
B
bellard 已提交
2484
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
2485 2486
        op = (modrm >> 3) & 7;
        if (mod != 3) {
B
bellard 已提交
2487 2488
            if (op == 0)
                s->rip_offset = insn_const_size(ot);
B
bellard 已提交
2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523
            gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
            gen_op_ld_T0_A0[ot + s->mem_index]();
        } else {
            gen_op_mov_TN_reg[ot][0][rm]();
        }

        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 */
            gen_op_notl_T0();
            if (mod != 3) {
                gen_op_st_T0_A0[ot + s->mem_index]();
            } else {
                gen_op_mov_reg_T0[ot][rm]();
            }
            break;
        case 3: /* neg */
            gen_op_negl_T0();
            if (mod != 3) {
                gen_op_st_T0_A0[ot + s->mem_index]();
            } else {
                gen_op_mov_reg_T0[ot][rm]();
            }
            gen_op_update_neg_cc();
            s->cc_op = CC_OP_SUBB + ot;
            break;
        case 4: /* mul */
            switch(ot) {
            case OT_BYTE:
                gen_op_mulb_AL_T0();
B
bellard 已提交
2524
                s->cc_op = CC_OP_MULB;
B
bellard 已提交
2525 2526 2527
                break;
            case OT_WORD:
                gen_op_mulw_AX_T0();
B
bellard 已提交
2528
                s->cc_op = CC_OP_MULW;
B
bellard 已提交
2529 2530 2531 2532
                break;
            default:
            case OT_LONG:
                gen_op_mull_EAX_T0();
B
bellard 已提交
2533
                s->cc_op = CC_OP_MULL;
B
bellard 已提交
2534
                break;
B
bellard 已提交
2535 2536 2537 2538 2539 2540
#ifdef TARGET_X86_64
            case OT_QUAD:
                gen_op_mulq_EAX_T0();
                s->cc_op = CC_OP_MULQ;
                break;
#endif
B
bellard 已提交
2541 2542 2543 2544 2545 2546
            }
            break;
        case 5: /* imul */
            switch(ot) {
            case OT_BYTE:
                gen_op_imulb_AL_T0();
B
bellard 已提交
2547
                s->cc_op = CC_OP_MULB;
B
bellard 已提交
2548 2549 2550
                break;
            case OT_WORD:
                gen_op_imulw_AX_T0();
B
bellard 已提交
2551
                s->cc_op = CC_OP_MULW;
B
bellard 已提交
2552 2553 2554 2555
                break;
            default:
            case OT_LONG:
                gen_op_imull_EAX_T0();
B
bellard 已提交
2556
                s->cc_op = CC_OP_MULL;
B
bellard 已提交
2557
                break;
B
bellard 已提交
2558 2559 2560 2561 2562 2563
#ifdef TARGET_X86_64
            case OT_QUAD:
                gen_op_imulq_EAX_T0();
                s->cc_op = CC_OP_MULQ;
                break;
#endif
B
bellard 已提交
2564 2565 2566 2567 2568
            }
            break;
        case 6: /* div */
            switch(ot) {
            case OT_BYTE:
B
bellard 已提交
2569 2570
                gen_jmp_im(pc_start - s->cs_base);
                gen_op_divb_AL_T0();
B
bellard 已提交
2571 2572
                break;
            case OT_WORD:
B
bellard 已提交
2573 2574
                gen_jmp_im(pc_start - s->cs_base);
                gen_op_divw_AX_T0();
B
bellard 已提交
2575 2576 2577
                break;
            default:
            case OT_LONG:
B
bellard 已提交
2578 2579 2580 2581 2582 2583 2584
                gen_jmp_im(pc_start - s->cs_base);
                gen_op_divl_EAX_T0();
                break;
#ifdef TARGET_X86_64
            case OT_QUAD:
                gen_jmp_im(pc_start - s->cs_base);
                gen_op_divq_EAX_T0();
B
bellard 已提交
2585
                break;
B
bellard 已提交
2586
#endif
B
bellard 已提交
2587 2588 2589 2590 2591
            }
            break;
        case 7: /* idiv */
            switch(ot) {
            case OT_BYTE:
B
bellard 已提交
2592 2593
                gen_jmp_im(pc_start - s->cs_base);
                gen_op_idivb_AL_T0();
B
bellard 已提交
2594 2595
                break;
            case OT_WORD:
B
bellard 已提交
2596 2597
                gen_jmp_im(pc_start - s->cs_base);
                gen_op_idivw_AX_T0();
B
bellard 已提交
2598 2599 2600
                break;
            default:
            case OT_LONG:
B
bellard 已提交
2601 2602 2603 2604 2605 2606 2607
                gen_jmp_im(pc_start - s->cs_base);
                gen_op_idivl_EAX_T0();
                break;
#ifdef TARGET_X86_64
            case OT_QUAD:
                gen_jmp_im(pc_start - s->cs_base);
                gen_op_idivq_EAX_T0();
B
bellard 已提交
2608
                break;
B
bellard 已提交
2609
#endif
B
bellard 已提交
2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621
            }
            break;
        default:
            goto illegal_op;
        }
        break;

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

B
bellard 已提交
2624
        modrm = ldub_code(s->pc++);
B
bellard 已提交
2625
        mod = (modrm >> 6) & 3;
B
bellard 已提交
2626
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
2627 2628 2629 2630
        op = (modrm >> 3) & 7;
        if (op >= 2 && b == 0xfe) {
            goto illegal_op;
        }
B
bellard 已提交
2631 2632 2633 2634 2635 2636 2637 2638 2639
        if (CODE64(s)) {
            if (op >= 2 && op <= 5) {
                /* operand size for jumps is 64 bit */
                ot = OT_QUAD;
            } else if (op == 6) {
                /* default push size is 64 bit */
                ot = dflag ? OT_QUAD : OT_WORD;
            }
        }
B
bellard 已提交
2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663
        if (mod != 3) {
            gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
            if (op >= 2 && op != 3 && op != 5)
                gen_op_ld_T0_A0[ot + s->mem_index]();
        } else {
            gen_op_mov_TN_reg[ot][0][rm]();
        }

        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 */
2664
            /* XXX: optimize if memory (no 'and' is necessary) */
B
bellard 已提交
2665 2666 2667
            if (s->dflag == 0)
                gen_op_andl_T0_ffff();
            next_eip = s->pc - s->cs_base;
2668 2669 2670
            gen_op_movl_T1_im(next_eip);
            gen_push_T1(s);
            gen_op_jmp_T0();
B
bellard 已提交
2671 2672
            gen_eob(s);
            break;
B
bellard 已提交
2673
        case 3: /* lcall Ev */
B
bellard 已提交
2674 2675
            gen_op_ld_T1_A0[ot + s->mem_index]();
            gen_op_addl_A0_im(1 << (ot - OT_WORD + 1));
B
bellard 已提交
2676
            gen_op_ldu_T0_A0[OT_WORD + s->mem_index]();
B
bellard 已提交
2677 2678 2679 2680
        do_lcall:
            if (s->pe && !s->vm86) {
                if (s->cc_op != CC_OP_DYNAMIC)
                    gen_op_set_cc_op(s->cc_op);
B
bellard 已提交
2681
                gen_jmp_im(pc_start - s->cs_base);
B
bellard 已提交
2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696
                gen_op_lcall_protected_T0_T1(dflag, s->pc - s->cs_base);
            } else {
                gen_op_lcall_real_T0_T1(dflag, s->pc - s->cs_base);
            }
            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 */
            gen_op_ld_T1_A0[ot + s->mem_index]();
            gen_op_addl_A0_im(1 << (ot - OT_WORD + 1));
B
bellard 已提交
2697
            gen_op_ldu_T0_A0[OT_WORD + s->mem_index]();
B
bellard 已提交
2698 2699 2700 2701
        do_ljmp:
            if (s->pe && !s->vm86) {
                if (s->cc_op != CC_OP_DYNAMIC)
                    gen_op_set_cc_op(s->cc_op);
B
bellard 已提交
2702
                gen_jmp_im(pc_start - s->cs_base);
B
bellard 已提交
2703
                gen_op_ljmp_protected_T0_T1(s->pc - s->cs_base);
B
bellard 已提交
2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723
            } else {
                gen_op_movl_seg_T0_vm(offsetof(CPUX86State,segs[R_CS]));
                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 */
    case 0x85: 
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
2724
            ot = dflag + OT_WORD;
B
bellard 已提交
2725

B
bellard 已提交
2726
        modrm = ldub_code(s->pc++);
B
bellard 已提交
2727
        mod = (modrm >> 6) & 3;
B
bellard 已提交
2728 2729
        rm = (modrm & 7) | REX_B(s);
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
2730 2731
        
        gen_ldst_modrm(s, modrm, ot, OR_TMP0, 0);
B
bellard 已提交
2732
        gen_op_mov_TN_reg[ot][1][reg]();
B
bellard 已提交
2733 2734 2735 2736 2737 2738 2739 2740 2741
        gen_op_testl_T0_T1_cc();
        s->cc_op = CC_OP_LOGICB + ot;
        break;
        
    case 0xa8: /* test eAX, Iv */
    case 0xa9:
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
2742
            ot = dflag + OT_WORD;
B
bellard 已提交
2743 2744 2745 2746 2747 2748 2749 2750 2751
        val = insn_get(s, ot);

        gen_op_mov_TN_reg[ot][0][OR_EAX]();
        gen_op_movl_T1_im(val);
        gen_op_testl_T0_T1_cc();
        s->cc_op = CC_OP_LOGICB + ot;
        break;
        
    case 0x98: /* CWDE/CBW */
B
bellard 已提交
2752 2753 2754 2755 2756 2757
#ifdef TARGET_X86_64
        if (dflag == 2) {
            gen_op_movslq_RAX_EAX();
        } else
#endif
        if (dflag == 1)
B
bellard 已提交
2758 2759 2760 2761 2762
            gen_op_movswl_EAX_AX();
        else
            gen_op_movsbw_AX_AL();
        break;
    case 0x99: /* CDQ/CWD */
B
bellard 已提交
2763 2764 2765 2766 2767 2768
#ifdef TARGET_X86_64
        if (dflag == 2) {
            gen_op_movsqo_RDX_RAX();
        } else
#endif
        if (dflag == 1)
B
bellard 已提交
2769 2770 2771 2772 2773 2774 2775
            gen_op_movslq_EDX_EAX();
        else
            gen_op_movswl_DX_AX();
        break;
    case 0x1af: /* imul Gv, Ev */
    case 0x69: /* imul Gv, Ev, I */
    case 0x6b:
B
bellard 已提交
2776
        ot = dflag + OT_WORD;
B
bellard 已提交
2777
        modrm = ldub_code(s->pc++);
B
bellard 已提交
2778 2779 2780 2781 2782
        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 已提交
2783 2784 2785 2786 2787
        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) {
2788
            val = (int8_t)insn_get(s, OT_BYTE);
B
bellard 已提交
2789 2790 2791 2792 2793
            gen_op_movl_T1_im(val);
        } else {
            gen_op_mov_TN_reg[ot][1][reg]();
        }

B
bellard 已提交
2794 2795 2796 2797 2798
#ifdef TARGET_X86_64
        if (ot == OT_QUAD) {
            gen_op_imulq_T0_T1();
        } else
#endif
B
bellard 已提交
2799 2800 2801 2802 2803 2804
        if (ot == OT_LONG) {
            gen_op_imull_T0_T1();
        } else {
            gen_op_imulw_T0_T1();
        }
        gen_op_mov_reg_T0[ot][reg]();
B
bellard 已提交
2805
        s->cc_op = CC_OP_MULB + ot;
B
bellard 已提交
2806 2807 2808 2809 2810 2811
        break;
    case 0x1c0:
    case 0x1c1: /* xadd Ev, Gv */
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
2812
            ot = dflag + OT_WORD;
B
bellard 已提交
2813
        modrm = ldub_code(s->pc++);
B
bellard 已提交
2814
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
2815 2816
        mod = (modrm >> 6) & 3;
        if (mod == 3) {
B
bellard 已提交
2817
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
2818 2819 2820 2821
            gen_op_mov_TN_reg[ot][0][reg]();
            gen_op_mov_TN_reg[ot][1][rm]();
            gen_op_addl_T0_T1();
            gen_op_mov_reg_T1[ot][reg]();
B
bellard 已提交
2822
            gen_op_mov_reg_T0[ot][rm]();
B
bellard 已提交
2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838
        } else {
            gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
            gen_op_mov_TN_reg[ot][0][reg]();
            gen_op_ld_T1_A0[ot + s->mem_index]();
            gen_op_addl_T0_T1();
            gen_op_st_T0_A0[ot + s->mem_index]();
            gen_op_mov_reg_T1[ot][reg]();
        }
        gen_op_update2_cc();
        s->cc_op = CC_OP_ADDB + ot;
        break;
    case 0x1b0:
    case 0x1b1: /* cmpxchg Ev, Gv */
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
2839
            ot = dflag + OT_WORD;
B
bellard 已提交
2840
        modrm = ldub_code(s->pc++);
B
bellard 已提交
2841
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
2842 2843 2844
        mod = (modrm >> 6) & 3;
        gen_op_mov_TN_reg[ot][1][reg]();
        if (mod == 3) {
B
bellard 已提交
2845
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
2846 2847 2848 2849 2850 2851
            gen_op_mov_TN_reg[ot][0][rm]();
            gen_op_cmpxchg_T0_T1_EAX_cc[ot]();
            gen_op_mov_reg_T0[ot][rm]();
        } else {
            gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
            gen_op_ld_T0_A0[ot + s->mem_index]();
2852
            gen_op_cmpxchg_mem_T0_T1_EAX_cc[ot + s->mem_index]();
B
bellard 已提交
2853 2854 2855 2856
        }
        s->cc_op = CC_OP_SUBB + ot;
        break;
    case 0x1c7: /* cmpxchg8b */
B
bellard 已提交
2857
        modrm = ldub_code(s->pc++);
B
bellard 已提交
2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
        if (s->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s->cc_op);
        gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
        gen_op_cmpxchg8b();
        s->cc_op = CC_OP_EFLAGS;
        break;
        
        /**************************/
        /* push/pop */
    case 0x50 ... 0x57: /* push */
B
bellard 已提交
2871
        gen_op_mov_TN_reg[OT_LONG][0][(b & 7) | REX_B(s)]();
B
bellard 已提交
2872 2873 2874
        gen_push_T0(s);
        break;
    case 0x58 ... 0x5f: /* pop */
B
bellard 已提交
2875 2876 2877 2878 2879
        if (CODE64(s)) {
            ot = dflag ? OT_QUAD : OT_WORD;
        } else {
            ot = dflag + OT_WORD;
        }
B
bellard 已提交
2880
        gen_pop_T0(s);
B
bellard 已提交
2881
        /* NOTE: order is important for pop %sp */
B
bellard 已提交
2882
        gen_pop_update(s);
B
bellard 已提交
2883
        gen_op_mov_reg_T0[ot][(b & 7) | REX_B(s)]();
B
bellard 已提交
2884 2885
        break;
    case 0x60: /* pusha */
B
bellard 已提交
2886 2887
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
2888 2889 2890
        gen_pusha(s);
        break;
    case 0x61: /* popa */
B
bellard 已提交
2891 2892
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
2893 2894 2895 2896
        gen_popa(s);
        break;
    case 0x68: /* push Iv */
    case 0x6a:
B
bellard 已提交
2897 2898 2899 2900 2901
        if (CODE64(s)) {
            ot = dflag ? OT_QUAD : OT_WORD;
        } else {
            ot = dflag + OT_WORD;
        }
B
bellard 已提交
2902 2903 2904 2905 2906 2907 2908 2909
        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 已提交
2910 2911 2912 2913 2914
        if (CODE64(s)) {
            ot = dflag ? OT_QUAD : OT_WORD;
        } else {
            ot = dflag + OT_WORD;
        }
B
bellard 已提交
2915
        modrm = ldub_code(s->pc++);
B
bellard 已提交
2916
        mod = (modrm >> 6) & 3;
B
bellard 已提交
2917
        gen_pop_T0(s);
B
bellard 已提交
2918 2919 2920
        if (mod == 3) {
            /* NOTE: order is important for pop %sp */
            gen_pop_update(s);
B
bellard 已提交
2921
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
2922 2923 2924
            gen_op_mov_reg_T0[ot][rm]();
        } else {
            /* NOTE: order is important too for MMU exceptions */
B
bellard 已提交
2925
            s->popl_esp_hack = 1 << ot;
B
bellard 已提交
2926 2927 2928 2929
            gen_ldst_modrm(s, modrm, ot, OR_TMP0, 1);
            s->popl_esp_hack = 0;
            gen_pop_update(s);
        }
B
bellard 已提交
2930 2931 2932
        break;
    case 0xc8: /* enter */
        {
B
bellard 已提交
2933
            /* XXX: long mode support */
B
bellard 已提交
2934
            int level;
B
bellard 已提交
2935
            val = lduw_code(s->pc);
B
bellard 已提交
2936
            s->pc += 2;
B
bellard 已提交
2937
            level = ldub_code(s->pc++);
B
bellard 已提交
2938 2939 2940 2941 2942
            gen_enter(s, val, level);
        }
        break;
    case 0xc9: /* leave */
        /* XXX: exception not precise (ESP is updated before potential exception) */
B
bellard 已提交
2943 2944 2945 2946 2947
        /* XXX: may be invalid for 16 bit in long mode */
        if (CODE64(s)) {
            gen_op_mov_TN_reg[OT_QUAD][0][R_EBP]();
            gen_op_mov_reg_T0[OT_QUAD][R_ESP]();
        } else if (s->ss32) {
B
bellard 已提交
2948 2949 2950 2951 2952 2953 2954
            gen_op_mov_TN_reg[OT_LONG][0][R_EBP]();
            gen_op_mov_reg_T0[OT_LONG][R_ESP]();
        } else {
            gen_op_mov_TN_reg[OT_WORD][0][R_EBP]();
            gen_op_mov_reg_T0[OT_WORD][R_ESP]();
        }
        gen_pop_T0(s);
B
bellard 已提交
2955 2956 2957 2958 2959
        if (CODE64(s)) {
            ot = dflag ? OT_QUAD : OT_WORD;
        } else {
            ot = dflag + OT_WORD;
        }
B
bellard 已提交
2960 2961 2962 2963 2964 2965 2966
        gen_op_mov_reg_T0[ot][R_EBP]();
        gen_pop_update(s);
        break;
    case 0x06: /* push es */
    case 0x0e: /* push cs */
    case 0x16: /* push ss */
    case 0x1e: /* push ds */
B
bellard 已提交
2967 2968
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979
        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 已提交
2980 2981
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
2982 2983 2984 2985 2986
        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) {
2987 2988 2989 2990 2991
            /* if reg == SS, inhibit interrupts/trace. */
            /* If several instructions disable interrupts, only the
               _first_ does it */
            if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
                gen_op_set_inhibit_irq();
B
bellard 已提交
2992 2993 2994
            s->tf = 0;
        }
        if (s->is_jmp) {
B
bellard 已提交
2995
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
2996 2997 2998 2999 3000 3001 3002 3003 3004
            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 已提交
3005
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016
            gen_eob(s);
        }
        break;

        /**************************/
        /* mov */
    case 0x88:
    case 0x89: /* mov Gv, Ev */
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
3017
            ot = dflag + OT_WORD;
B
bellard 已提交
3018
        modrm = ldub_code(s->pc++);
B
bellard 已提交
3019
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
3020 3021
        
        /* generate a generic store */
B
bellard 已提交
3022
        gen_ldst_modrm(s, modrm, ot, reg, 1);
B
bellard 已提交
3023 3024 3025 3026 3027 3028
        break;
    case 0xc6:
    case 0xc7: /* mov Ev, Iv */
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
3029
            ot = dflag + OT_WORD;
B
bellard 已提交
3030
        modrm = ldub_code(s->pc++);
B
bellard 已提交
3031
        mod = (modrm >> 6) & 3;
B
bellard 已提交
3032 3033
        if (mod != 3) {
            s->rip_offset = insn_const_size(ot);
B
bellard 已提交
3034
            gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
3035
        }
B
bellard 已提交
3036 3037 3038 3039 3040
        val = insn_get(s, ot);
        gen_op_movl_T0_im(val);
        if (mod != 3)
            gen_op_st_T0_A0[ot + s->mem_index]();
        else
B
bellard 已提交
3041
            gen_op_mov_reg_T0[ot][(modrm & 7) | REX_B(s)]();
B
bellard 已提交
3042 3043 3044 3045 3046 3047
        break;
    case 0x8a:
    case 0x8b: /* mov Ev, Gv */
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
3048
            ot = OT_WORD + dflag;
B
bellard 已提交
3049
        modrm = ldub_code(s->pc++);
B
bellard 已提交
3050
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
3051 3052 3053 3054 3055
        
        gen_ldst_modrm(s, modrm, ot, OR_TMP0, 0);
        gen_op_mov_reg_T0[ot][reg]();
        break;
    case 0x8e: /* mov seg, Gv */
B
bellard 已提交
3056
        modrm = ldub_code(s->pc++);
B
bellard 已提交
3057 3058 3059 3060 3061 3062 3063
        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 */
3064 3065 3066 3067
            /* If several instructions disable interrupts, only the
               _first_ does it */
            if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
                gen_op_set_inhibit_irq();
B
bellard 已提交
3068 3069 3070
            s->tf = 0;
        }
        if (s->is_jmp) {
B
bellard 已提交
3071
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
3072 3073 3074 3075
            gen_eob(s);
        }
        break;
    case 0x8c: /* mov Gv, seg */
B
bellard 已提交
3076
        modrm = ldub_code(s->pc++);
B
bellard 已提交
3077 3078 3079 3080 3081
        reg = (modrm >> 3) & 7;
        mod = (modrm >> 6) & 3;
        if (reg >= 6)
            goto illegal_op;
        gen_op_movl_T0_seg(reg);
B
bellard 已提交
3082 3083 3084 3085
        if (mod == 3)
            ot = OT_WORD + dflag;
        else
            ot = OT_WORD;
B
bellard 已提交
3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098
        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 已提交
3099
            modrm = ldub_code(s->pc++);
B
bellard 已提交
3100
            reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
3101
            mod = (modrm >> 6) & 3;
B
bellard 已提交
3102
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
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
            
            if (mod == 3) {
                gen_op_mov_TN_reg[ot][0][rm]();
                switch(ot | (b & 8)) {
                case OT_BYTE:
                    gen_op_movzbl_T0_T0();
                    break;
                case OT_BYTE | 8:
                    gen_op_movsbl_T0_T0();
                    break;
                case OT_WORD:
                    gen_op_movzwl_T0_T0();
                    break;
                default:
                case OT_WORD | 8:
                    gen_op_movswl_T0_T0();
                    break;
                }
                gen_op_mov_reg_T0[d_ot][reg]();
            } else {
                gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
                if (b & 8) {
                    gen_op_lds_T0_A0[ot + s->mem_index]();
                } else {
                    gen_op_ldu_T0_A0[ot + s->mem_index]();
                }
                gen_op_mov_reg_T0[d_ot][reg]();
            }
        }
        break;

    case 0x8d: /* lea */
B
bellard 已提交
3135
        ot = dflag + OT_WORD;
B
bellard 已提交
3136
        modrm = ldub_code(s->pc++);
B
bellard 已提交
3137 3138 3139
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
B
bellard 已提交
3140
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154
        /* 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;
        gen_op_mov_reg_A0[ot - OT_WORD][reg]();
        break;
        
    case 0xa0: /* mov EAX, Ov */
    case 0xa1:
    case 0xa2: /* mov Ov, EAX */
    case 0xa3:
        {
B
bellard 已提交
3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191
            target_ulong offset_addr;

            if ((b & 1) == 0)
                ot = OT_BYTE;
            else
                ot = dflag + OT_WORD;
#ifdef TARGET_X86_64
            if (CODE64(s)) {
                offset_addr = ldq_code(s->pc);
                s->pc += 8;
                if (offset_addr == (int32_t)offset_addr)
                    gen_op_movq_A0_im(offset_addr);
                else
                    gen_op_movq_A0_im64(offset_addr >> 32, offset_addr);
            } else 
#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);
            }
            /* handle override */
            {
                int override, must_add_seg;
                must_add_seg = s->addseg;
                if (s->override >= 0) {
                    override = s->override;
                    must_add_seg = 1;
                } else {
                    override = R_DS;
                }
                if (must_add_seg) {
                    gen_op_addl_A0_seg(offsetof(CPUX86State,segs[override].base));
                }
B
bellard 已提交
3192
            }
B
bellard 已提交
3193 3194 3195 3196 3197 3198
            if ((b & 2) == 0) {
                gen_op_ld_T0_A0[ot + s->mem_index]();
                gen_op_mov_reg_T0[ot][R_EAX]();
            } else {
                gen_op_mov_TN_reg[ot][0][R_EAX]();
                gen_op_st_T0_A0[ot + s->mem_index]();
B
bellard 已提交
3199 3200 3201 3202
            }
        }
        break;
    case 0xd7: /* xlat */
B
bellard 已提交
3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214
#ifdef TARGET_X86_64
        if (CODE64(s)) {
            gen_op_movq_A0_reg[R_EBX]();
            gen_op_addq_A0_AL();
        } else 
#endif
        {
            gen_op_movl_A0_reg[R_EBX]();
            gen_op_addl_A0_AL();
            if (s->aflag == 0)
                gen_op_andl_A0_ffff();
        }
B
bellard 已提交
3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235
        /* handle override */
        {
            int override, must_add_seg;
            must_add_seg = s->addseg;
            override = R_DS;
            if (s->override >= 0) {
                override = s->override;
                must_add_seg = 1;
            } else {
                override = R_DS;
            }
            if (must_add_seg) {
                gen_op_addl_A0_seg(offsetof(CPUX86State,segs[override].base));
            }
        }
        gen_op_ldu_T0_A0[OT_BYTE + s->mem_index]();
        gen_op_mov_reg_T0[OT_BYTE][R_EAX]();
        break;
    case 0xb0 ... 0xb7: /* mov R, Ib */
        val = insn_get(s, OT_BYTE);
        gen_op_movl_T0_im(val);
B
bellard 已提交
3236
        gen_op_mov_reg_T0[OT_BYTE][(b & 7) | REX_B(s)]();
B
bellard 已提交
3237 3238
        break;
    case 0xb8 ... 0xbf: /* mov R, Iv */
B
bellard 已提交
3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256
#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);
            gen_op_mov_reg_T0[OT_QUAD][reg]();
        } else 
#endif
        {
            ot = dflag ? OT_LONG : OT_WORD;
            val = insn_get(s, ot);
            reg = (b & 7) | REX_B(s);
            gen_op_movl_T0_im(val);
            gen_op_mov_reg_T0[ot][reg]();
        }
B
bellard 已提交
3257 3258 3259
        break;

    case 0x91 ... 0x97: /* xchg R, EAX */
B
bellard 已提交
3260 3261
        ot = dflag + OT_WORD;
        reg = (b & 7) | REX_B(s);
B
bellard 已提交
3262 3263 3264 3265 3266 3267 3268
        rm = R_EAX;
        goto do_xchg_reg;
    case 0x86:
    case 0x87: /* xchg Ev, Gv */
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
3269
            ot = dflag + OT_WORD;
B
bellard 已提交
3270
        modrm = ldub_code(s->pc++);
B
bellard 已提交
3271
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
3272 3273
        mod = (modrm >> 6) & 3;
        if (mod == 3) {
B
bellard 已提交
3274
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293
        do_xchg_reg:
            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]();
        } else {
            gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
            gen_op_mov_TN_reg[ot][0][reg]();
            /* for xchg, lock is implicit */
            if (!(prefixes & PREFIX_LOCK))
                gen_op_lock();
            gen_op_ld_T1_A0[ot + s->mem_index]();
            gen_op_st_T0_A0[ot + s->mem_index]();
            if (!(prefixes & PREFIX_LOCK))
                gen_op_unlock();
            gen_op_mov_reg_T1[ot][reg]();
        }
        break;
    case 0xc4: /* les Gv */
B
bellard 已提交
3294 3295
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
3296 3297 3298
        op = R_ES;
        goto do_lxx;
    case 0xc5: /* lds Gv */
B
bellard 已提交
3299 3300
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312
        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 已提交
3313
        modrm = ldub_code(s->pc++);
B
bellard 已提交
3314
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
3315 3316 3317 3318 3319 3320 3321
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
        gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
        gen_op_ld_T1_A0[ot + s->mem_index]();
        gen_op_addl_A0_im(1 << (ot - OT_WORD + 1));
        /* load the segment first to handle exceptions properly */
B
bellard 已提交
3322
        gen_op_ldu_T0_A0[OT_WORD + s->mem_index]();
B
bellard 已提交
3323 3324 3325 3326
        gen_movl_seg_T0(s, op, pc_start - s->cs_base);
        /* then put the data */
        gen_op_mov_reg_T1[ot][reg]();
        if (s->is_jmp) {
B
bellard 已提交
3327
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342
            gen_eob(s);
        }
        break;
        
        /************************/
        /* shifts */
    case 0xc0:
    case 0xc1:
        /* shift Ev,Ib */
        shift = 2;
    grp2:
        {
            if ((b & 1) == 0)
                ot = OT_BYTE;
            else
B
bellard 已提交
3343
                ot = dflag + OT_WORD;
B
bellard 已提交
3344
            
B
bellard 已提交
3345
            modrm = ldub_code(s->pc++);
B
bellard 已提交
3346 3347 3348 3349
            mod = (modrm >> 6) & 3;
            op = (modrm >> 3) & 7;
            
            if (mod != 3) {
B
bellard 已提交
3350 3351 3352
                if (shift == 2) {
                    s->rip_offset = 1;
                }
B
bellard 已提交
3353 3354 3355
                gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
                opreg = OR_TMP0;
            } else {
B
bellard 已提交
3356
                opreg = (modrm & 7) | REX_B(s);
B
bellard 已提交
3357 3358 3359 3360 3361 3362 3363
            }

            /* simpler op */
            if (shift == 0) {
                gen_shift(s, op, ot, opreg, OR_ECX);
            } else {
                if (shift == 2) {
B
bellard 已提交
3364
                    shift = ldub_code(s->pc++);
B
bellard 已提交
3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396
                }
                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 已提交
3397
        ot = dflag + OT_WORD;
B
bellard 已提交
3398
        modrm = ldub_code(s->pc++);
B
bellard 已提交
3399
        mod = (modrm >> 6) & 3;
B
bellard 已提交
3400 3401
        rm = (modrm & 7) | REX_B(s);
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
3402 3403 3404 3405 3406 3407 3408 3409 3410 3411
        
        if (mod != 3) {
            gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
            gen_op_ld_T0_A0[ot + s->mem_index]();
        } else {
            gen_op_mov_TN_reg[ot][0][rm]();
        }
        gen_op_mov_TN_reg[ot][1][reg]();
        
        if (shift) {
B
bellard 已提交
3412
            val = ldub_code(s->pc++);
B
bellard 已提交
3413 3414 3415 3416
            if (ot == OT_QUAD)
                val &= 0x3f;
            else
                val &= 0x1f;
B
bellard 已提交
3417 3418
            if (val) {
                if (mod == 3)
3419
                    gen_op_shiftd_T0_T1_im_cc[ot][op](val);
B
bellard 已提交
3420
                else
3421
                    gen_op_shiftd_mem_T0_T1_im_cc[ot + s->mem_index][op](val);
B
bellard 已提交
3422 3423 3424 3425 3426 3427 3428 3429 3430
                if (op == 0 && ot != OT_WORD)
                    s->cc_op = CC_OP_SHLB + ot;
                else
                    s->cc_op = CC_OP_SARB + ot;
            }
        } else {
            if (s->cc_op != CC_OP_DYNAMIC)
                gen_op_set_cc_op(s->cc_op);
            if (mod == 3)
3431
                gen_op_shiftd_T0_T1_ECX_cc[ot][op]();
B
bellard 已提交
3432
            else
3433
                gen_op_shiftd_mem_T0_T1_ECX_cc[ot + s->mem_index][op]();
B
bellard 已提交
3434 3435 3436 3437 3438 3439 3440 3441 3442 3443
            s->cc_op = CC_OP_DYNAMIC; /* cannot predict flags after */
        }
        if (mod == 3) {
            gen_op_mov_reg_T0[ot][rm]();
        }
        break;

        /************************/
        /* floats */
    case 0xd8 ... 0xdf: 
B
bellard 已提交
3444 3445 3446 3447 3448 3449
        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 已提交
3450
        modrm = ldub_code(s->pc++);
B
bellard 已提交
3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595
        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:
                        gen_op_flds_FT0_A0();
                        break;
                    case 1:
                        gen_op_fildl_FT0_A0();
                        break;
                    case 2:
                        gen_op_fldl_FT0_A0();
                        break;
                    case 3:
                    default:
                        gen_op_fild_FT0_A0();
                        break;
                    }
                    
                    gen_op_fp_arith_ST0_FT0[op1]();
                    if (op1 == 3) {
                        /* fcomp needs pop */
                        gen_op_fpop();
                    }
                }
                break;
            case 0x08: /* flds */
            case 0x0a: /* fsts */
            case 0x0b: /* fstps */
            case 0x18: /* fildl */
            case 0x1a: /* fistl */
            case 0x1b: /* fistpl */
            case 0x28: /* fldl */
            case 0x2a: /* fstl */
            case 0x2b: /* fstpl */
            case 0x38: /* filds */
            case 0x3a: /* fists */
            case 0x3b: /* fistps */
                
                switch(op & 7) {
                case 0:
                    switch(op >> 4) {
                    case 0:
                        gen_op_flds_ST0_A0();
                        break;
                    case 1:
                        gen_op_fildl_ST0_A0();
                        break;
                    case 2:
                        gen_op_fldl_ST0_A0();
                        break;
                    case 3:
                    default:
                        gen_op_fild_ST0_A0();
                        break;
                    }
                    break;
                default:
                    switch(op >> 4) {
                    case 0:
                        gen_op_fsts_ST0_A0();
                        break;
                    case 1:
                        gen_op_fistl_ST0_A0();
                        break;
                    case 2:
                        gen_op_fstl_ST0_A0();
                        break;
                    case 3:
                    default:
                        gen_op_fist_ST0_A0();
                        break;
                    }
                    if ((op & 7) == 3)
                        gen_op_fpop();
                    break;
                }
                break;
            case 0x0c: /* fldenv mem */
                gen_op_fldenv_A0(s->dflag);
                break;
            case 0x0d: /* fldcw mem */
                gen_op_fldcw_A0();
                break;
            case 0x0e: /* fnstenv mem */
                gen_op_fnstenv_A0(s->dflag);
                break;
            case 0x0f: /* fnstcw mem */
                gen_op_fnstcw_A0();
                break;
            case 0x1d: /* fldt mem */
                gen_op_fldt_ST0_A0();
                break;
            case 0x1f: /* fstpt mem */
                gen_op_fstt_ST0_A0();
                gen_op_fpop();
                break;
            case 0x2c: /* frstor mem */
                gen_op_frstor_A0(s->dflag);
                break;
            case 0x2e: /* fnsave mem */
                gen_op_fnsave_A0(s->dflag);
                break;
            case 0x2f: /* fnstsw mem */
                gen_op_fnstsw_A0();
                break;
            case 0x3c: /* fbld */
                gen_op_fbld_ST0_A0();
                break;
            case 0x3e: /* fbstp */
                gen_op_fbst_ST0_A0();
                gen_op_fpop();
                break;
            case 0x3d: /* fildll */
                gen_op_fildll_ST0_A0();
                break;
            case 0x3f: /* fistpll */
                gen_op_fistll_ST0_A0();
                gen_op_fpop();
                break;
            default:
                goto illegal_op;
            }
        } else {
            /* register float ops */
            opreg = rm;

            switch(op) {
            case 0x08: /* fld sti */
                gen_op_fpush();
                gen_op_fmov_ST0_STN((opreg + 1) & 7);
                break;
            case 0x09: /* fxchg sti */
B
bellard 已提交
3596 3597
            case 0x29: /* fxchg4 sti, undocumented op */
            case 0x39: /* fxchg7 sti, undocumented op */
B
bellard 已提交
3598 3599 3600 3601 3602
                gen_op_fxchg_ST0_STN(opreg);
                break;
            case 0x0a: /* grp d9/2 */
                switch(rm) {
                case 0: /* fnop */
3603 3604 3605
                    /* check exceptions (FreeBSD FPU probe) */
                    if (s->cc_op != CC_OP_DYNAMIC)
                        gen_op_set_cc_op(s->cc_op);
B
bellard 已提交
3606
                    gen_jmp_im(pc_start - s->cs_base);
3607
                    gen_op_fwait();
B
bellard 已提交
3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x0c: /* grp d9/4 */
                switch(rm) {
                case 0: /* fchs */
                    gen_op_fchs_ST0();
                    break;
                case 1: /* fabs */
                    gen_op_fabs_ST0();
                    break;
                case 4: /* ftst */
                    gen_op_fldz_FT0();
                    gen_op_fcom_ST0_FT0();
                    break;
                case 5: /* fxam */
                    gen_op_fxam_ST0();
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x0d: /* grp d9/5 */
                {
                    switch(rm) {
                    case 0:
                        gen_op_fpush();
                        gen_op_fld1_ST0();
                        break;
                    case 1:
                        gen_op_fpush();
                        gen_op_fldl2t_ST0();
                        break;
                    case 2:
                        gen_op_fpush();
                        gen_op_fldl2e_ST0();
                        break;
                    case 3:
                        gen_op_fpush();
                        gen_op_fldpi_ST0();
                        break;
                    case 4:
                        gen_op_fpush();
                        gen_op_fldlg2_ST0();
                        break;
                    case 5:
                        gen_op_fpush();
                        gen_op_fldln2_ST0();
                        break;
                    case 6:
                        gen_op_fpush();
                        gen_op_fldz_ST0();
                        break;
                    default:
                        goto illegal_op;
                    }
                }
                break;
            case 0x0e: /* grp d9/6 */
                switch(rm) {
                case 0: /* f2xm1 */
                    gen_op_f2xm1();
                    break;
                case 1: /* fyl2x */
                    gen_op_fyl2x();
                    break;
                case 2: /* fptan */
                    gen_op_fptan();
                    break;
                case 3: /* fpatan */
                    gen_op_fpatan();
                    break;
                case 4: /* fxtract */
                    gen_op_fxtract();
                    break;
                case 5: /* fprem1 */
                    gen_op_fprem1();
                    break;
                case 6: /* fdecstp */
                    gen_op_fdecstp();
                    break;
                default:
                case 7: /* fincstp */
                    gen_op_fincstp();
                    break;
                }
                break;
            case 0x0f: /* grp d9/7 */
                switch(rm) {
                case 0: /* fprem */
                    gen_op_fprem();
                    break;
                case 1: /* fyl2xp1 */
                    gen_op_fyl2xp1();
                    break;
                case 2: /* fsqrt */
                    gen_op_fsqrt();
                    break;
                case 3: /* fsincos */
                    gen_op_fsincos();
                    break;
                case 5: /* fscale */
                    gen_op_fscale();
                    break;
                case 4: /* frndint */
                    gen_op_frndint();
                    break;
                case 6: /* fsin */
                    gen_op_fsin();
                    break;
                default:
                case 7: /* fcos */
                    gen_op_fcos();
                    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;
                    
                    op1 = op & 7;
                    if (op >= 0x20) {
                        gen_op_fp_arith_STN_ST0[op1](opreg);
                        if (op >= 0x30)
                            gen_op_fpop();
                    } else {
                        gen_op_fmov_FT0_STN(opreg);
                        gen_op_fp_arith_ST0_FT0[op1]();
                    }
                }
                break;
            case 0x02: /* fcom */
B
bellard 已提交
3744
            case 0x22: /* fcom2, undocumented op */
B
bellard 已提交
3745 3746 3747 3748
                gen_op_fmov_FT0_STN(opreg);
                gen_op_fcom_ST0_FT0();
                break;
            case 0x03: /* fcomp */
B
bellard 已提交
3749 3750
            case 0x23: /* fcomp3, undocumented op */
            case 0x32: /* fcomp5, undocumented op */
B
bellard 已提交
3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798
                gen_op_fmov_FT0_STN(opreg);
                gen_op_fcom_ST0_FT0();
                gen_op_fpop();
                break;
            case 0x15: /* da/5 */
                switch(rm) {
                case 1: /* fucompp */
                    gen_op_fmov_FT0_STN(1);
                    gen_op_fucom_ST0_FT0();
                    gen_op_fpop();
                    gen_op_fpop();
                    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 */
                    gen_op_fclex();
                    break;
                case 3: /* fninit */
                    gen_op_fninit();
                    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);
                gen_op_fmov_FT0_STN(opreg);
                gen_op_fucomi_ST0_FT0();
                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);
                gen_op_fmov_FT0_STN(opreg);
                gen_op_fcomi_ST0_FT0();
                s->cc_op = CC_OP_EFLAGS;
                break;
B
bellard 已提交
3799 3800 3801
            case 0x28: /* ffree sti */
                gen_op_ffree_STN(opreg);
                break; 
B
bellard 已提交
3802 3803 3804 3805
            case 0x2a: /* fst sti */
                gen_op_fmov_STN_ST0(opreg);
                break;
            case 0x2b: /* fstp sti */
B
bellard 已提交
3806 3807 3808
            case 0x0b: /* fstp1 sti, undocumented op */
            case 0x3a: /* fstp8 sti, undocumented op */
            case 0x3b: /* fstp9 sti, undocumented op */
B
bellard 已提交
3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832
                gen_op_fmov_STN_ST0(opreg);
                gen_op_fpop();
                break;
            case 0x2c: /* fucom st(i) */
                gen_op_fmov_FT0_STN(opreg);
                gen_op_fucom_ST0_FT0();
                break;
            case 0x2d: /* fucomp st(i) */
                gen_op_fmov_FT0_STN(opreg);
                gen_op_fucom_ST0_FT0();
                gen_op_fpop();
                break;
            case 0x33: /* de/3 */
                switch(rm) {
                case 1: /* fcompp */
                    gen_op_fmov_FT0_STN(1);
                    gen_op_fcom_ST0_FT0();
                    gen_op_fpop();
                    gen_op_fpop();
                    break;
                default:
                    goto illegal_op;
                }
                break;
B
bellard 已提交
3833 3834 3835 3836
            case 0x38: /* ffreep sti, undocumented op */
                gen_op_ffree_STN(opreg);
                gen_op_fpop();
                break;
B
bellard 已提交
3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861
            case 0x3c: /* df/4 */
                switch(rm) {
                case 0:
                    gen_op_fnstsw_EAX();
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x3d: /* fucomip */
                if (s->cc_op != CC_OP_DYNAMIC)
                    gen_op_set_cc_op(s->cc_op);
                gen_op_fmov_FT0_STN(opreg);
                gen_op_fucomi_ST0_FT0();
                gen_op_fpop();
                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);
                gen_op_fmov_FT0_STN(opreg);
                gen_op_fcomi_ST0_FT0();
                gen_op_fpop();
                s->cc_op = CC_OP_EFLAGS;
                break;
3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876
            case 0x10 ... 0x13: /* fcmovxx */
            case 0x18 ... 0x1b:
                {
                    int op1;
                    const static uint8_t fcmov_cc[8] = {
                        (JCC_B << 1),
                        (JCC_Z << 1),
                        (JCC_BE << 1),
                        (JCC_P << 1),
                    };
                    op1 = fcmov_cc[op & 3] | ((op >> 3) & 1);
                    gen_setcc(s, op1);
                    gen_op_fcmov_ST0_STN_T0(opreg);
                }
                break;
B
bellard 已提交
3877 3878 3879 3880
            default:
                goto illegal_op;
            }
        }
B
bellard 已提交
3881 3882 3883
#ifdef USE_CODE_COPY
        s->tb->cflags |= CF_TB_FP_USED;
#endif
B
bellard 已提交
3884 3885 3886 3887 3888 3889 3890 3891 3892
        break;
        /************************/
        /* string ops */

    case 0xa4: /* movsS */
    case 0xa5:
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
3893
            ot = dflag + OT_WORD;
B
bellard 已提交
3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906

        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;
        
    case 0xaa: /* stosS */
    case 0xab:
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
B
bellard 已提交
3907
            ot = dflag + OT_WORD;
B
bellard 已提交
3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919

        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 已提交
3920
            ot = dflag + OT_WORD;
B
bellard 已提交
3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931
        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 已提交
3932
            ot = dflag + OT_WORD;
B
bellard 已提交
3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947
        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 已提交
3948
            ot = dflag + OT_WORD;
B
bellard 已提交
3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959
        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:
3960 3961 3962 3963 3964 3965 3966
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
        gen_check_io(s, ot, 1, pc_start - s->cs_base);
        if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
            gen_repz_ins(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
B
bellard 已提交
3967
        } else {
3968
            gen_ins(s, ot);
B
bellard 已提交
3969 3970 3971 3972
        }
        break;
    case 0x6e: /* outsS */
    case 0x6f:
3973 3974 3975 3976 3977 3978 3979
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
        gen_check_io(s, ot, 1, pc_start - s->cs_base);
        if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
            gen_repz_outs(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
B
bellard 已提交
3980
        } else {
3981
            gen_outs(s, ot);
B
bellard 已提交
3982 3983 3984 3985 3986 3987 3988
        }
        break;

        /************************/
        /* port I/O */
    case 0xe4:
    case 0xe5:
3989 3990 3991 3992 3993 3994 3995 3996 3997
        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);
        gen_check_io(s, ot, 0, pc_start - s->cs_base);
        gen_op_in[ot]();
        gen_op_mov_reg_T1[ot][R_EAX]();
B
bellard 已提交
3998 3999 4000
        break;
    case 0xe6:
    case 0xe7:
4001 4002 4003 4004 4005 4006 4007 4008 4009
        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);
        gen_check_io(s, ot, 0, pc_start - s->cs_base);
        gen_op_mov_TN_reg[ot][1][R_EAX]();
        gen_op_out[ot]();
B
bellard 已提交
4010 4011 4012
        break;
    case 0xec:
    case 0xed:
4013 4014 4015 4016 4017
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
        gen_op_mov_TN_reg[OT_WORD][0][R_EDX]();
4018
        gen_op_andl_T0_ffff();
4019 4020 4021
        gen_check_io(s, ot, 0, pc_start - s->cs_base);
        gen_op_in[ot]();
        gen_op_mov_reg_T1[ot][R_EAX]();
B
bellard 已提交
4022 4023 4024
        break;
    case 0xee:
    case 0xef:
4025 4026 4027 4028 4029
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
        gen_op_mov_TN_reg[OT_WORD][0][R_EDX]();
4030
        gen_op_andl_T0_ffff();
4031 4032 4033
        gen_check_io(s, ot, 0, pc_start - s->cs_base);
        gen_op_mov_TN_reg[ot][1][R_EAX]();
        gen_op_out[ot]();
B
bellard 已提交
4034 4035 4036 4037 4038
        break;

        /************************/
        /* control */
    case 0xc2: /* ret im */
B
bellard 已提交
4039
        val = ldsw_code(s->pc);
B
bellard 已提交
4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056
        s->pc += 2;
        gen_pop_T0(s);
        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 已提交
4057
        val = ldsw_code(s->pc);
B
bellard 已提交
4058 4059 4060 4061 4062
        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 已提交
4063
            gen_jmp_im(pc_start - s->cs_base);
B
bellard 已提交
4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090
            gen_op_lret_protected(s->dflag, val);
        } else {
            gen_stack_A0(s);
            /* pop offset */
            gen_op_ld_T0_A0[1 + s->dflag + s->mem_index]();
            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);
            gen_op_ld_T0_A0[1 + s->dflag + s->mem_index]();
            gen_op_movl_seg_T0_vm(offsetof(CPUX86State,segs[R_CS]));
            /* 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 */
        if (!s->pe) {
            /* real mode */
            gen_op_iret_real(s->dflag);
            s->cc_op = CC_OP_EFLAGS;
4091 4092 4093 4094 4095 4096 4097
        } else if (s->vm86) {
            if (s->iopl != 3) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
                gen_op_iret_real(s->dflag);
                s->cc_op = CC_OP_EFLAGS;
            }
B
bellard 已提交
4098 4099 4100
        } else {
            if (s->cc_op != CC_OP_DYNAMIC)
                gen_op_set_cc_op(s->cc_op);
B
bellard 已提交
4101
            gen_jmp_im(pc_start - s->cs_base);
B
bellard 已提交
4102
            gen_op_iret_protected(s->dflag, s->pc - s->cs_base);
B
bellard 已提交
4103 4104 4105 4106 4107 4108
            s->cc_op = CC_OP_EFLAGS;
        }
        gen_eob(s);
        break;
    case 0xe8: /* call im */
        {
B
bellard 已提交
4109 4110 4111 4112
            if (dflag)
                tval = (int32_t)insn_get(s, OT_LONG);
            else
                tval = (int16_t)insn_get(s, OT_WORD);
B
bellard 已提交
4113
            next_eip = s->pc - s->cs_base;
B
bellard 已提交
4114
            tval += next_eip;
B
bellard 已提交
4115
            if (s->dflag == 0)
B
bellard 已提交
4116 4117
                tval &= 0xffff;
            gen_movtl_T0_im(next_eip);
B
bellard 已提交
4118
            gen_push_T0(s);
B
bellard 已提交
4119
            gen_jmp(s, tval);
B
bellard 已提交
4120 4121 4122 4123 4124
        }
        break;
    case 0x9a: /* lcall im */
        {
            unsigned int selector, offset;
B
bellard 已提交
4125 4126 4127
            
            if (CODE64(s))
                goto illegal_op;
B
bellard 已提交
4128 4129 4130 4131 4132
            ot = dflag ? OT_LONG : OT_WORD;
            offset = insn_get(s, ot);
            selector = insn_get(s, OT_WORD);
            
            gen_op_movl_T0_im(selector);
B
bellard 已提交
4133
            gen_op_movl_T1_imu(offset);
B
bellard 已提交
4134 4135 4136
        }
        goto do_lcall;
    case 0xe9: /* jmp */
B
bellard 已提交
4137 4138 4139 4140 4141
        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 已提交
4142
        if (s->dflag == 0)
B
bellard 已提交
4143 4144
            tval &= 0xffff;
        gen_jmp(s, tval);
B
bellard 已提交
4145 4146 4147 4148 4149
        break;
    case 0xea: /* ljmp im */
        {
            unsigned int selector, offset;

B
bellard 已提交
4150 4151
            if (CODE64(s))
                goto illegal_op;
B
bellard 已提交
4152 4153 4154 4155 4156
            ot = dflag ? OT_LONG : OT_WORD;
            offset = insn_get(s, ot);
            selector = insn_get(s, OT_WORD);
            
            gen_op_movl_T0_im(selector);
B
bellard 已提交
4157
            gen_op_movl_T1_imu(offset);
B
bellard 已提交
4158 4159 4160
        }
        goto do_ljmp;
    case 0xeb: /* jmp Jb */
B
bellard 已提交
4161 4162
        tval = (int8_t)insn_get(s, OT_BYTE);
        tval += s->pc - s->cs_base;
B
bellard 已提交
4163
        if (s->dflag == 0)
B
bellard 已提交
4164 4165
            tval &= 0xffff;
        gen_jmp(s, tval);
B
bellard 已提交
4166 4167
        break;
    case 0x70 ... 0x7f: /* jcc Jb */
B
bellard 已提交
4168
        tval = (int8_t)insn_get(s, OT_BYTE);
B
bellard 已提交
4169 4170 4171
        goto do_jcc;
    case 0x180 ... 0x18f: /* jcc Jv */
        if (dflag) {
B
bellard 已提交
4172
            tval = (int32_t)insn_get(s, OT_LONG);
B
bellard 已提交
4173
        } else {
B
bellard 已提交
4174
            tval = (int16_t)insn_get(s, OT_WORD); 
B
bellard 已提交
4175 4176 4177
        }
    do_jcc:
        next_eip = s->pc - s->cs_base;
B
bellard 已提交
4178
        tval += next_eip;
B
bellard 已提交
4179
        if (s->dflag == 0)
B
bellard 已提交
4180 4181
            tval &= 0xffff;
        gen_jcc(s, b, tval, next_eip);
B
bellard 已提交
4182 4183 4184
        break;

    case 0x190 ... 0x19f: /* setcc Gv */
B
bellard 已提交
4185
        modrm = ldub_code(s->pc++);
B
bellard 已提交
4186 4187 4188 4189
        gen_setcc(s, b);
        gen_ldst_modrm(s, modrm, OT_BYTE, OR_TMP0, 1);
        break;
    case 0x140 ... 0x14f: /* cmov Gv, Ev */
B
bellard 已提交
4190
        ot = dflag + OT_WORD;
B
bellard 已提交
4191
        modrm = ldub_code(s->pc++);
B
bellard 已提交
4192
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
4193 4194 4195 4196 4197 4198
        mod = (modrm >> 6) & 3;
        gen_setcc(s, b);
        if (mod != 3) {
            gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
            gen_op_ld_T1_A0[ot + s->mem_index]();
        } else {
B
bellard 已提交
4199
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228
            gen_op_mov_TN_reg[ot][1][rm]();
        }
        gen_op_cmov_reg_T1_T0[ot - OT_WORD][reg]();
        break;
        
        /************************/
        /* flags */
    case 0x9c: /* pushf */
        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);
            gen_op_movl_T0_eflags();
            gen_push_T0(s);
        }
        break;
    case 0x9d: /* popf */
        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) {
                    gen_op_movl_eflags_T0_cpl0();
                } else {
                    gen_op_movw_eflags_T0_cpl0();
                }
            } else {
B
bellard 已提交
4229 4230 4231 4232 4233 4234
                if (s->cpl <= s->iopl) {
                    if (s->dflag) {
                        gen_op_movl_eflags_T0_io();
                    } else {
                        gen_op_movw_eflags_T0_io();
                    }
B
bellard 已提交
4235
                } else {
B
bellard 已提交
4236 4237 4238 4239 4240
                    if (s->dflag) {
                        gen_op_movl_eflags_T0();
                    } else {
                        gen_op_movw_eflags_T0();
                    }
B
bellard 已提交
4241 4242 4243 4244 4245
                }
            }
            gen_pop_update(s);
            s->cc_op = CC_OP_EFLAGS;
            /* abort translation because TF flag may change */
B
bellard 已提交
4246
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
4247 4248 4249 4250
            gen_eob(s);
        }
        break;
    case 0x9e: /* sahf */
B
bellard 已提交
4251 4252
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
4253 4254 4255 4256 4257 4258 4259
        gen_op_mov_TN_reg[OT_BYTE][0][R_AH]();
        if (s->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s->cc_op);
        gen_op_movb_eflags_T0();
        s->cc_op = CC_OP_EFLAGS;
        break;
    case 0x9f: /* lahf */
B
bellard 已提交
4260 4261
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294
        if (s->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s->cc_op);
        gen_op_movl_T0_eflags();
        gen_op_mov_reg_T0[OT_BYTE][R_AH]();
        break;
    case 0xf5: /* cmc */
        if (s->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s->cc_op);
        gen_op_cmc();
        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);
        gen_op_clc();
        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);
        gen_op_stc();
        s->cc_op = CC_OP_EFLAGS;
        break;
    case 0xfc: /* cld */
        gen_op_cld();
        break;
    case 0xfd: /* std */
        gen_op_std();
        break;

        /************************/
        /* bit operations */
    case 0x1ba: /* bt/bts/btr/btc Gv, im */
B
bellard 已提交
4295
        ot = dflag + OT_WORD;
B
bellard 已提交
4296
        modrm = ldub_code(s->pc++);
B
bellard 已提交
4297
        op = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
4298
        mod = (modrm >> 6) & 3;
B
bellard 已提交
4299
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4300
        if (mod != 3) {
B
bellard 已提交
4301
            s->rip_offset = 1;
B
bellard 已提交
4302 4303 4304 4305 4306 4307
            gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
            gen_op_ld_T0_A0[ot + s->mem_index]();
        } else {
            gen_op_mov_TN_reg[ot][0][rm]();
        }
        /* load shift */
B
bellard 已提交
4308
        val = ldub_code(s->pc++);
B
bellard 已提交
4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334
        gen_op_movl_T1_im(val);
        if (op < 4)
            goto illegal_op;
        op -= 4;
        gen_op_btx_T0_T1_cc[ot - OT_WORD][op]();
        s->cc_op = CC_OP_SARB + ot;
        if (op != 0) {
            if (mod != 3)
                gen_op_st_T0_A0[ot + s->mem_index]();
            else
                gen_op_mov_reg_T0[ot][rm]();
            gen_op_update_bt_cc();
        }
        break;
    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 已提交
4335
        ot = dflag + OT_WORD;
B
bellard 已提交
4336
        modrm = ldub_code(s->pc++);
B
bellard 已提交
4337
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
4338
        mod = (modrm >> 6) & 3;
B
bellard 已提交
4339
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4340 4341 4342 4343
        gen_op_mov_TN_reg[OT_LONG][1][reg]();
        if (mod != 3) {
            gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
            /* specific case: we need to add a displacement */
B
bellard 已提交
4344
            gen_op_add_bit_A0_T1[ot - OT_WORD]();
B
bellard 已提交
4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360
            gen_op_ld_T0_A0[ot + s->mem_index]();
        } else {
            gen_op_mov_TN_reg[ot][0][rm]();
        }
        gen_op_btx_T0_T1_cc[ot - OT_WORD][op]();
        s->cc_op = CC_OP_SARB + ot;
        if (op != 0) {
            if (mod != 3)
                gen_op_st_T0_A0[ot + s->mem_index]();
            else
                gen_op_mov_reg_T0[ot][rm]();
            gen_op_update_bt_cc();
        }
        break;
    case 0x1bc: /* bsf */
    case 0x1bd: /* bsr */
B
bellard 已提交
4361
        ot = dflag + OT_WORD;
B
bellard 已提交
4362
        modrm = ldub_code(s->pc++);
B
bellard 已提交
4363
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
4364
        gen_ldst_modrm(s, modrm, ot, OR_TMP0, 0);
B
bellard 已提交
4365 4366 4367
        /* NOTE: in order to handle the 0 case, we must load the
           result. It could be optimized with a generated jump */
        gen_op_mov_TN_reg[ot][1][reg]();
B
bellard 已提交
4368
        gen_op_bsx_T0_cc[ot - OT_WORD][b & 1]();
B
bellard 已提交
4369
        gen_op_mov_reg_T1[ot][reg]();
B
bellard 已提交
4370 4371 4372 4373 4374
        s->cc_op = CC_OP_LOGICB + ot;
        break;
        /************************/
        /* bcd */
    case 0x27: /* daa */
B
bellard 已提交
4375 4376
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
4377 4378 4379 4380 4381 4382
        if (s->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s->cc_op);
        gen_op_daa();
        s->cc_op = CC_OP_EFLAGS;
        break;
    case 0x2f: /* das */
B
bellard 已提交
4383 4384
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
4385 4386 4387 4388 4389 4390
        if (s->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s->cc_op);
        gen_op_das();
        s->cc_op = CC_OP_EFLAGS;
        break;
    case 0x37: /* aaa */
B
bellard 已提交
4391 4392
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
4393 4394 4395 4396 4397 4398
        if (s->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s->cc_op);
        gen_op_aaa();
        s->cc_op = CC_OP_EFLAGS;
        break;
    case 0x3f: /* aas */
B
bellard 已提交
4399 4400
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
4401 4402 4403 4404 4405 4406
        if (s->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s->cc_op);
        gen_op_aas();
        s->cc_op = CC_OP_EFLAGS;
        break;
    case 0xd4: /* aam */
B
bellard 已提交
4407 4408
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
4409
        val = ldub_code(s->pc++);
B
bellard 已提交
4410 4411 4412 4413
        gen_op_aam(val);
        s->cc_op = CC_OP_LOGICB;
        break;
    case 0xd5: /* aad */
B
bellard 已提交
4414 4415
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
4416
        val = ldub_code(s->pc++);
B
bellard 已提交
4417 4418 4419 4420 4421 4422
        gen_op_aad(val);
        s->cc_op = CC_OP_LOGICB;
        break;
        /************************/
        /* misc */
    case 0x90: /* nop */
B
bellard 已提交
4423
        /* XXX: xchg + rex handling */
4424 4425 4426
        /* XXX: correct lock test for all insn */
        if (prefixes & PREFIX_LOCK)
            goto illegal_op;
B
bellard 已提交
4427 4428
        break;
    case 0x9b: /* fwait */
B
bellard 已提交
4429 4430 4431
        if ((s->flags & (HF_MP_MASK | HF_TS_MASK)) == 
            (HF_MP_MASK | HF_TS_MASK)) {
            gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
B
bellard 已提交
4432 4433 4434
        } else {
            if (s->cc_op != CC_OP_DYNAMIC)
                gen_op_set_cc_op(s->cc_op);
B
bellard 已提交
4435
            gen_jmp_im(pc_start - s->cs_base);
B
bellard 已提交
4436
            gen_op_fwait();
B
bellard 已提交
4437
        }
B
bellard 已提交
4438 4439 4440 4441 4442
        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 已提交
4443
        val = ldub_code(s->pc++);
4444
        if (s->vm86 && s->iopl != 3) {
B
bellard 已提交
4445
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base); 
4446 4447 4448
        } else {
            gen_interrupt(s, val, pc_start - s->cs_base, s->pc - s->cs_base);
        }
B
bellard 已提交
4449 4450
        break;
    case 0xce: /* into */
B
bellard 已提交
4451 4452
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
4453 4454
        if (s->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s->cc_op);
4455 4456
        gen_jmp_im(pc_start - s->cs_base);
        gen_op_into(s->pc - pc_start);
B
bellard 已提交
4457 4458
        break;
    case 0xf1: /* icebp (undocumented, exits to external debugger) */
B
bellard 已提交
4459
#if 0
B
bellard 已提交
4460
        gen_debug(s, pc_start - s->cs_base);
B
bellard 已提交
4461 4462 4463 4464
#else
        /* test ! */
        cpu_set_log(CPU_LOG_TB_IN_ASM | CPU_LOG_PCALL);
#endif
B
bellard 已提交
4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486
        break;
    case 0xfa: /* cli */
        if (!s->vm86) {
            if (s->cpl <= s->iopl) {
                gen_op_cli();
            } else {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            }
        } else {
            if (s->iopl == 3) {
                gen_op_cli();
            } 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:
                gen_op_sti();
                /* interruptions are enabled only the first insn after sti */
4487 4488 4489 4490
                /* If several instructions disable interrupts, only the
                   _first_ does it */
                if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
                    gen_op_set_inhibit_irq();
B
bellard 已提交
4491
                /* give a chance to handle pending irqs */
B
bellard 已提交
4492
                gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505
                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 已提交
4506 4507
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
4508
        ot = dflag ? OT_LONG : OT_WORD;
B
bellard 已提交
4509
        modrm = ldub_code(s->pc++);
B
bellard 已提交
4510 4511 4512 4513
        reg = (modrm >> 3) & 7;
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
B
bellard 已提交
4514
        gen_op_mov_TN_reg[ot][0][reg]();
B
bellard 已提交
4515
        gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
4516
        gen_jmp_im(pc_start - s->cs_base);
B
bellard 已提交
4517
        if (ot == OT_WORD)
B
bellard 已提交
4518
            gen_op_boundw();
B
bellard 已提交
4519
        else
B
bellard 已提交
4520
            gen_op_boundl();
B
bellard 已提交
4521 4522
        break;
    case 0x1c8 ... 0x1cf: /* bswap reg */
B
bellard 已提交
4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535
        reg = (b & 7) | REX_B(s);
#ifdef TARGET_X86_64
        if (dflag == 2) {
            gen_op_mov_TN_reg[OT_QUAD][0][reg]();
            gen_op_bswapq_T0();
            gen_op_mov_reg_T0[OT_QUAD][reg]();
        } else 
#endif
        {
            gen_op_mov_TN_reg[OT_LONG][0][reg]();
            gen_op_bswapl_T0();
            gen_op_mov_reg_T0[OT_LONG][reg]();
        }
B
bellard 已提交
4536 4537
        break;
    case 0xd6: /* salc */
B
bellard 已提交
4538 4539
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550
        if (s->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s->cc_op);
        gen_op_salc();
        break;
    case 0xe0: /* loopnz */
    case 0xe1: /* loopz */
        if (s->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s->cc_op);
        /* FALL THRU */
    case 0xe2: /* loop */
    case 0xe3: /* jecxz */
B
bellard 已提交
4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576
        {
            int l1, l2;

            tval = (int8_t)insn_get(s, OT_BYTE);
            next_eip = s->pc - s->cs_base;
            tval += next_eip;
            if (s->dflag == 0)
                tval &= 0xffff;
            
            l1 = gen_new_label();
            l2 = gen_new_label();
            b &= 3;
            if (b == 3) {
                gen_op_jz_ecx[s->aflag](l1);
            } else {
                gen_op_dec_ECX[s->aflag]();
                gen_op_loop[s->aflag][b](l1);
            }

            gen_jmp_im(next_eip);
            gen_op_jmp_label(l2);
            gen_set_label(l1);
            gen_jmp_im(tval);
            gen_set_label(l2);
            gen_eob(s);
        }
B
bellard 已提交
4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591
        break;
    case 0x130: /* wrmsr */
    case 0x132: /* rdmsr */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
            if (b & 2)
                gen_op_rdmsr();
            else
                gen_op_wrmsr();
        }
        break;
    case 0x131: /* rdtsc */
        gen_op_rdtsc();
        break;
4592
    case 0x134: /* sysenter */
B
bellard 已提交
4593 4594
        if (CODE64(s))
            goto illegal_op;
4595 4596 4597 4598 4599 4600 4601
        if (!s->pe) {
            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);
                s->cc_op = CC_OP_DYNAMIC;
            }
B
bellard 已提交
4602
            gen_jmp_im(pc_start - s->cs_base);
4603 4604 4605 4606 4607
            gen_op_sysenter();
            gen_eob(s);
        }
        break;
    case 0x135: /* sysexit */
B
bellard 已提交
4608 4609
        if (CODE64(s))
            goto illegal_op;
4610 4611 4612 4613 4614 4615 4616
        if (!s->pe) {
            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);
                s->cc_op = CC_OP_DYNAMIC;
            }
B
bellard 已提交
4617
            gen_jmp_im(pc_start - s->cs_base);
4618 4619 4620 4621
            gen_op_sysexit();
            gen_eob(s);
        }
        break;
B
bellard 已提交
4622 4623 4624 4625 4626 4627 4628 4629
#ifdef TARGET_X86_64
    case 0x105: /* syscall */
        /* XXX: is it usable in real mode ? */
        if (s->cc_op != CC_OP_DYNAMIC) {
            gen_op_set_cc_op(s->cc_op);
            s->cc_op = CC_OP_DYNAMIC;
        }
        gen_jmp_im(pc_start - s->cs_base);
B
bellard 已提交
4630
        gen_op_syscall(s->pc - pc_start);
B
bellard 已提交
4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646
        gen_eob(s);
        break;
    case 0x107: /* sysret */
        if (!s->pe) {
            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);
                s->cc_op = CC_OP_DYNAMIC;
            }
            gen_jmp_im(pc_start - s->cs_base);
            gen_op_sysret(s->dflag);
            gen_eob(s);
        }
        break;
#endif
B
bellard 已提交
4647 4648 4649 4650 4651 4652 4653 4654 4655
    case 0x1a2: /* cpuid */
        gen_op_cpuid();
        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);
B
bellard 已提交
4656
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
4657 4658 4659 4660 4661
            gen_op_hlt();
            s->is_jmp = 3;
        }
        break;
    case 0x100:
B
bellard 已提交
4662
        modrm = ldub_code(s->pc++);
B
bellard 已提交
4663 4664 4665 4666
        mod = (modrm >> 6) & 3;
        op = (modrm >> 3) & 7;
        switch(op) {
        case 0: /* sldt */
4667 4668
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
4669 4670 4671 4672 4673 4674 4675
            gen_op_movl_T0_env(offsetof(CPUX86State,ldt.selector));
            ot = OT_WORD;
            if (mod == 3)
                ot += s->dflag;
            gen_ldst_modrm(s, modrm, ot, OR_TMP0, 1);
            break;
        case 2: /* lldt */
4676 4677
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
4678 4679 4680 4681
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
                gen_ldst_modrm(s, modrm, OT_WORD, OR_TMP0, 0);
B
bellard 已提交
4682
                gen_jmp_im(pc_start - s->cs_base);
B
bellard 已提交
4683 4684 4685 4686
                gen_op_lldt_T0();
            }
            break;
        case 1: /* str */
4687 4688
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
4689 4690 4691 4692 4693 4694 4695
            gen_op_movl_T0_env(offsetof(CPUX86State,tr.selector));
            ot = OT_WORD;
            if (mod == 3)
                ot += s->dflag;
            gen_ldst_modrm(s, modrm, ot, OR_TMP0, 1);
            break;
        case 3: /* ltr */
4696 4697
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
4698 4699 4700 4701
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
                gen_ldst_modrm(s, modrm, OT_WORD, OR_TMP0, 0);
B
bellard 已提交
4702
                gen_jmp_im(pc_start - s->cs_base);
B
bellard 已提交
4703 4704 4705 4706 4707
                gen_op_ltr_T0();
            }
            break;
        case 4: /* verr */
        case 5: /* verw */
4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718
            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)
                gen_op_verr();
            else
                gen_op_verw();
            s->cc_op = CC_OP_EFLAGS;
            break;
B
bellard 已提交
4719 4720 4721 4722 4723
        default:
            goto illegal_op;
        }
        break;
    case 0x101:
B
bellard 已提交
4724
        modrm = ldub_code(s->pc++);
B
bellard 已提交
4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737
        mod = (modrm >> 6) & 3;
        op = (modrm >> 3) & 7;
        switch(op) {
        case 0: /* sgdt */
        case 1: /* sidt */
            if (mod == 3)
                goto illegal_op;
            gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
            if (op == 0)
                gen_op_movl_T0_env(offsetof(CPUX86State,gdt.limit));
            else
                gen_op_movl_T0_env(offsetof(CPUX86State,idt.limit));
            gen_op_st_T0_A0[OT_WORD + s->mem_index]();
B
bellard 已提交
4738 4739 4740 4741 4742 4743
#ifdef TARGET_X86_64
            if (CODE64(s)) 
                gen_op_addq_A0_im(2);
            else
#endif
                gen_op_addl_A0_im(2);
B
bellard 已提交
4744
            if (op == 0)
B
bellard 已提交
4745
                gen_op_movtl_T0_env(offsetof(CPUX86State,gdt.base));
B
bellard 已提交
4746
            else
B
bellard 已提交
4747
                gen_op_movtl_T0_env(offsetof(CPUX86State,idt.base));
B
bellard 已提交
4748 4749
            if (!s->dflag)
                gen_op_andl_T0_im(0xffffff);
B
bellard 已提交
4750
            gen_op_st_T0_A0[CODE64(s) + OT_LONG + s->mem_index]();
B
bellard 已提交
4751 4752 4753 4754 4755 4756 4757 4758 4759 4760
            break;
        case 2: /* lgdt */
        case 3: /* lidt */
            if (mod == 3)
                goto illegal_op;
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
                gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
                gen_op_ld_T1_A0[OT_WORD + s->mem_index]();
B
bellard 已提交
4761 4762 4763 4764 4765 4766 4767
#ifdef TARGET_X86_64
                if (CODE64(s))
                    gen_op_addq_A0_im(2);
                else
#endif
                    gen_op_addl_A0_im(2);
                gen_op_ld_T0_A0[CODE64(s) + OT_LONG + s->mem_index]();
B
bellard 已提交
4768 4769 4770
                if (!s->dflag)
                    gen_op_andl_T0_im(0xffffff);
                if (op == 2) {
B
bellard 已提交
4771
                    gen_op_movtl_env_T0(offsetof(CPUX86State,gdt.base));
B
bellard 已提交
4772 4773
                    gen_op_movl_env_T1(offsetof(CPUX86State,gdt.limit));
                } else {
B
bellard 已提交
4774
                    gen_op_movtl_env_T0(offsetof(CPUX86State,idt.base));
B
bellard 已提交
4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788
                    gen_op_movl_env_T1(offsetof(CPUX86State,idt.limit));
                }
            }
            break;
        case 4: /* smsw */
            gen_op_movl_T0_env(offsetof(CPUX86State,cr[0]));
            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 {
                gen_ldst_modrm(s, modrm, OT_WORD, OR_TMP0, 0);
                gen_op_lmsw_T0();
B
bellard 已提交
4789
                gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
4790
                gen_eob(s);
B
bellard 已提交
4791 4792 4793 4794 4795 4796
            }
            break;
        case 7: /* invlpg */
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
B
bellard 已提交
4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815
                if (mod == 3) {
#ifdef TARGET_X86_64
                    if (CODE64(s) && (modrm & 7) == 0) {
                        /* swapgs */
                        gen_op_movtl_T0_env(offsetof(CPUX86State,segs[R_GS].base));
                        gen_op_movtl_T1_env(offsetof(CPUX86State,kernelgsbase));
                        gen_op_movtl_env_T1(offsetof(CPUX86State,segs[R_GS].base));
                        gen_op_movtl_env_T0(offsetof(CPUX86State,kernelgsbase));
                    } else 
#endif
                    {
                        goto illegal_op;
                    }
                } else {
                    gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
                    gen_op_invlpg_A0();
                    gen_jmp_im(s->pc - s->cs_base);
                    gen_eob(s);
                }
B
bellard 已提交
4816 4817 4818 4819 4820 4821
            }
            break;
        default:
            goto illegal_op;
        }
        break;
4822 4823 4824 4825 4826 4827 4828 4829
    case 0x108: /* invd */
    case 0x109: /* wbinvd */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
            /* nothing to do */
        }
        break;
4830
    case 0x1ae:
4831 4832
        modrm = ldub_code(s->pc++);
        mod = (modrm >> 6) & 3;
B
bellard 已提交
4833 4834 4835 4836 4837
        op = (modrm >> 3) & 7;
        switch(op) {
        case 0: /* fxsave */
            if (mod == 3 || !(s->cpuid_features & CPUID_FXSR))
                goto illegal_op;
4838
            gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
B
bellard 已提交
4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854
            gen_op_fxsave_A0((s->dflag == 2));
            break;
        case 1: /* fxrstor */
            if (mod == 3 || !(s->cpuid_features & CPUID_FXSR))
                goto illegal_op;
            gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
            gen_op_fxrstor_A0((s->dflag == 2));
            break;
        case 5: /* lfence */
        case 6: /* mfence */
        case 7: /* sfence */
            if ((modrm & 0xc7) != 0xc0 || !(s->cpuid_features & CPUID_SSE))
                goto illegal_op;
            break;
        default:
            goto illegal_op;
4855
        }
B
bellard 已提交
4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909
        break;
    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);
            
            if (mod == 3) {
                gen_op_mov_TN_reg[OT_LONG][0][rm]();
                /* sign extend */
                if (d_ot == OT_QUAD)
                    gen_op_movslq_T0_T0();
                gen_op_mov_reg_T0[d_ot][reg]();
            } else {
                gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
                if (d_ot == OT_QUAD) {
                    gen_op_lds_T0_A0[OT_LONG + s->mem_index]();
                } else {
                    gen_op_ld_T0_A0[OT_LONG + s->mem_index]();
                }
                gen_op_mov_reg_T0[d_ot][reg]();
            }
        } else 
#endif
        {
            if (!s->pe || s->vm86)
                goto illegal_op;
            ot = dflag ? OT_LONG : OT_WORD;
            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);
                gen_op_ld_T0_A0[ot + s->mem_index]();
            } else {
                gen_op_mov_TN_reg[ot][0][rm]();
            }
            if (s->cc_op != CC_OP_DYNAMIC)
                gen_op_set_cc_op(s->cc_op);
            gen_op_arpl();
            s->cc_op = CC_OP_EFLAGS;
            if (mod != 3) {
                gen_op_st_T0_A0[ot + s->mem_index]();
            } else {
                gen_op_mov_reg_T0[ot][rm]();
            }
            gen_op_arpl_update();
4910 4911
        }
        break;
B
bellard 已提交
4912 4913 4914 4915 4916
    case 0x102: /* lar */
    case 0x103: /* lsl */
        if (!s->pe || s->vm86)
            goto illegal_op;
        ot = dflag ? OT_LONG : OT_WORD;
B
bellard 已提交
4917
        modrm = ldub_code(s->pc++);
B
bellard 已提交
4918
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930
        gen_ldst_modrm(s, modrm, ot, OR_TMP0, 0);
        gen_op_mov_TN_reg[ot][1][reg]();
        if (s->cc_op != CC_OP_DYNAMIC)
            gen_op_set_cc_op(s->cc_op);
        if (b == 0x102)
            gen_op_lar();
        else
            gen_op_lsl();
        s->cc_op = CC_OP_EFLAGS;
        gen_op_mov_reg_T1[ot][reg]();
        break;
    case 0x118:
B
bellard 已提交
4931
        modrm = ldub_code(s->pc++);
B
bellard 已提交
4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952
        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;
        default:
            goto illegal_op;
        }
        break;
    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 已提交
4953
            modrm = ldub_code(s->pc++);
B
bellard 已提交
4954 4955
            if ((modrm & 0xc0) != 0xc0)
                goto illegal_op;
B
bellard 已提交
4956 4957 4958 4959 4960 4961
            rm = (modrm & 7) | REX_B(s);
            reg = ((modrm >> 3) & 7) | rex_r;
            if (CODE64(s))
                ot = OT_QUAD;
            else
                ot = OT_LONG;
B
bellard 已提交
4962 4963 4964 4965 4966 4967
            switch(reg) {
            case 0:
            case 2:
            case 3:
            case 4:
                if (b & 2) {
B
bellard 已提交
4968
                    gen_op_mov_TN_reg[ot][0][rm]();
B
bellard 已提交
4969
                    gen_op_movl_crN_T0(reg);
B
bellard 已提交
4970
                    gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
4971 4972
                    gen_eob(s);
                } else {
B
bellard 已提交
4973 4974
                    gen_op_movtl_T0_env(offsetof(CPUX86State,cr[reg]));
                    gen_op_mov_reg_T0[ot][rm]();
B
bellard 已提交
4975 4976
                }
                break;
B
bellard 已提交
4977
                /* XXX: add CR8 for x86_64 */
B
bellard 已提交
4978 4979 4980 4981 4982 4983 4984 4985 4986 4987
            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 已提交
4988
            modrm = ldub_code(s->pc++);
B
bellard 已提交
4989 4990
            if ((modrm & 0xc0) != 0xc0)
                goto illegal_op;
B
bellard 已提交
4991 4992 4993 4994 4995 4996
            rm = (modrm & 7) | REX_B(s);
            reg = ((modrm >> 3) & 7) | rex_r;
            if (CODE64(s))
                ot = OT_QUAD;
            else
                ot = OT_LONG;
B
bellard 已提交
4997
            /* XXX: do it dynamically with CR4.DE bit */
B
bellard 已提交
4998
            if (reg == 4 || reg == 5 || reg >= 8)
B
bellard 已提交
4999 5000
                goto illegal_op;
            if (b & 2) {
B
bellard 已提交
5001
                gen_op_mov_TN_reg[ot][0][rm]();
B
bellard 已提交
5002
                gen_op_movl_drN_T0(reg);
B
bellard 已提交
5003
                gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5004 5005
                gen_eob(s);
            } else {
B
bellard 已提交
5006 5007
                gen_op_movtl_T0_env(offsetof(CPUX86State,dr[reg]));
                gen_op_mov_reg_T0[ot][rm]();
B
bellard 已提交
5008 5009 5010 5011 5012 5013 5014 5015
            }
        }
        break;
    case 0x106: /* clts */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
            gen_op_clts();
B
bellard 已提交
5016
            /* abort block because static cpu state changed */
B
bellard 已提交
5017
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5018
            gen_eob(s);
B
bellard 已提交
5019 5020
        }
        break;
B
bellard 已提交
5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079
    /* SSE support */
    case 0x16f:
        if (prefixes & PREFIX_DATA) {
            /* movdqa xmm1, xmm2/mem128 */
            if (!(s->cpuid_features & CPUID_SSE))
                goto illegal_op;
            modrm = ldub_code(s->pc++);
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
            if (mod != 3) {
                gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
                gen_ldo_env_A0[s->mem_index >> 2](offsetof(CPUX86State,xmm_regs[reg]));
            } else {
                rm = (modrm & 7) | REX_B(s);
                gen_op_movo(offsetof(CPUX86State,xmm_regs[reg]),
                            offsetof(CPUX86State,xmm_regs[rm]));
            }
        } else {
            goto illegal_op;
        }
        break;
    case 0x1e7:
        if (prefixes & PREFIX_DATA) {
            /* movntdq mem128, xmm1 */
            if (!(s->cpuid_features & CPUID_SSE))
                goto illegal_op;
            modrm = ldub_code(s->pc++);
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
            if (mod != 3) {
                gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
                gen_sto_env_A0[s->mem_index >> 2](offsetof(CPUX86State,xmm_regs[reg]));
            } else {
                goto illegal_op;
            }
        } else {
            goto illegal_op;
        }
        break;
    case 0x17f:
        if (prefixes & PREFIX_DATA) {
            /* movdqa xmm2/mem128, xmm1 */
            if (!(s->cpuid_features & CPUID_SSE))
                goto illegal_op;
            modrm = ldub_code(s->pc++);
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
            if (mod != 3) {
                gen_lea_modrm(s, modrm, &reg_addr, &offset_addr);
                gen_sto_env_A0[s->mem_index >> 2](offsetof(CPUX86State,xmm_regs[reg]));
            } else {
                rm = (modrm & 7) | REX_B(s);
                gen_op_movo(offsetof(CPUX86State,xmm_regs[rm]),
                            offsetof(CPUX86State,xmm_regs[reg]));
            }
        } else {
            goto illegal_op;
        }
        break;
B
bellard 已提交
5080 5081 5082 5083 5084 5085 5086 5087
    default:
        goto illegal_op;
    }
    /* lock generation */
    if (s->prefix & PREFIX_LOCK)
        gen_op_unlock();
    return s->pc;
 illegal_op:
5088 5089
    if (s->prefix & PREFIX_LOCK)
        gen_op_unlock();
B
bellard 已提交
5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175
    /* XXX: ensure that no lock was generated */
    gen_exception(s, EXCP06_ILLOP, pc_start - s->cs_base);
    return s->pc;
}

#define CC_OSZAPC (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C)
#define CC_OSZAP (CC_O | CC_S | CC_Z | CC_A | CC_P)

/* flags read by an operation */
static uint16_t opc_read_flags[NB_OPS] = { 
    [INDEX_op_aas] = CC_A,
    [INDEX_op_aaa] = CC_A,
    [INDEX_op_das] = CC_A | CC_C,
    [INDEX_op_daa] = CC_A | CC_C,

    /* subtle: due to the incl/decl implementation, C is used */
    [INDEX_op_update_inc_cc] = CC_C, 

    [INDEX_op_into] = CC_O,

    [INDEX_op_jb_subb] = CC_C,
    [INDEX_op_jb_subw] = CC_C,
    [INDEX_op_jb_subl] = CC_C,

    [INDEX_op_jz_subb] = CC_Z,
    [INDEX_op_jz_subw] = CC_Z,
    [INDEX_op_jz_subl] = CC_Z,

    [INDEX_op_jbe_subb] = CC_Z | CC_C,
    [INDEX_op_jbe_subw] = CC_Z | CC_C,
    [INDEX_op_jbe_subl] = CC_Z | CC_C,

    [INDEX_op_js_subb] = CC_S,
    [INDEX_op_js_subw] = CC_S,
    [INDEX_op_js_subl] = CC_S,

    [INDEX_op_jl_subb] = CC_O | CC_S,
    [INDEX_op_jl_subw] = CC_O | CC_S,
    [INDEX_op_jl_subl] = CC_O | CC_S,

    [INDEX_op_jle_subb] = CC_O | CC_S | CC_Z,
    [INDEX_op_jle_subw] = CC_O | CC_S | CC_Z,
    [INDEX_op_jle_subl] = CC_O | CC_S | CC_Z,

    [INDEX_op_loopnzw] = CC_Z,
    [INDEX_op_loopnzl] = CC_Z,
    [INDEX_op_loopzw] = CC_Z,
    [INDEX_op_loopzl] = CC_Z,

    [INDEX_op_seto_T0_cc] = CC_O,
    [INDEX_op_setb_T0_cc] = CC_C,
    [INDEX_op_setz_T0_cc] = CC_Z,
    [INDEX_op_setbe_T0_cc] = CC_Z | CC_C,
    [INDEX_op_sets_T0_cc] = CC_S,
    [INDEX_op_setp_T0_cc] = CC_P,
    [INDEX_op_setl_T0_cc] = CC_O | CC_S,
    [INDEX_op_setle_T0_cc] = CC_O | CC_S | CC_Z,

    [INDEX_op_setb_T0_subb] = CC_C,
    [INDEX_op_setb_T0_subw] = CC_C,
    [INDEX_op_setb_T0_subl] = CC_C,

    [INDEX_op_setz_T0_subb] = CC_Z,
    [INDEX_op_setz_T0_subw] = CC_Z,
    [INDEX_op_setz_T0_subl] = CC_Z,

    [INDEX_op_setbe_T0_subb] = CC_Z | CC_C,
    [INDEX_op_setbe_T0_subw] = CC_Z | CC_C,
    [INDEX_op_setbe_T0_subl] = CC_Z | CC_C,

    [INDEX_op_sets_T0_subb] = CC_S,
    [INDEX_op_sets_T0_subw] = CC_S,
    [INDEX_op_sets_T0_subl] = CC_S,

    [INDEX_op_setl_T0_subb] = CC_O | CC_S,
    [INDEX_op_setl_T0_subw] = CC_O | CC_S,
    [INDEX_op_setl_T0_subl] = CC_O | CC_S,

    [INDEX_op_setle_T0_subb] = CC_O | CC_S | CC_Z,
    [INDEX_op_setle_T0_subw] = CC_O | CC_S | CC_Z,
    [INDEX_op_setle_T0_subl] = CC_O | CC_S | CC_Z,

    [INDEX_op_movl_T0_eflags] = CC_OSZAPC,
    [INDEX_op_cmc] = CC_C,
    [INDEX_op_salc] = CC_C,

5176
    /* needed for correct flag optimisation before string ops */
B
bellard 已提交
5177 5178
    [INDEX_op_jnz_ecxw] = CC_OSZAPC,
    [INDEX_op_jnz_ecxl] = CC_OSZAPC,
5179 5180
    [INDEX_op_jz_ecxw] = CC_OSZAPC,
    [INDEX_op_jz_ecxl] = CC_OSZAPC,
B
bellard 已提交
5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202

#ifdef TARGET_X86_64
    [INDEX_op_jb_subq] = CC_C,
    [INDEX_op_jz_subq] = CC_Z,
    [INDEX_op_jbe_subq] = CC_Z | CC_C,
    [INDEX_op_js_subq] = CC_S,
    [INDEX_op_jl_subq] = CC_O | CC_S,
    [INDEX_op_jle_subq] = CC_O | CC_S | CC_Z,

    [INDEX_op_loopnzq] = CC_Z,
    [INDEX_op_loopzq] = CC_Z,

    [INDEX_op_setb_T0_subq] = CC_C,
    [INDEX_op_setz_T0_subq] = CC_Z,
    [INDEX_op_setbe_T0_subq] = CC_Z | CC_C,
    [INDEX_op_sets_T0_subq] = CC_S,
    [INDEX_op_setl_T0_subq] = CC_O | CC_S,
    [INDEX_op_setle_T0_subq] = CC_O | CC_S | CC_Z,

    [INDEX_op_jnz_ecxq] = CC_OSZAPC,
    [INDEX_op_jz_ecxq] = CC_OSZAPC,
#endif
5203

5204 5205 5206 5207
#define DEF_READF(SUFFIX)\
    [INDEX_op_adcb ## SUFFIX ## _T0_T1_cc] = CC_C,\
    [INDEX_op_adcw ## SUFFIX ## _T0_T1_cc] = CC_C,\
    [INDEX_op_adcl ## SUFFIX ## _T0_T1_cc] = CC_C,\
B
bellard 已提交
5208
    X86_64_DEF([INDEX_op_adcq ## SUFFIX ## _T0_T1_cc] = CC_C,)\
5209 5210 5211
    [INDEX_op_sbbb ## SUFFIX ## _T0_T1_cc] = CC_C,\
    [INDEX_op_sbbw ## SUFFIX ## _T0_T1_cc] = CC_C,\
    [INDEX_op_sbbl ## SUFFIX ## _T0_T1_cc] = CC_C,\
B
bellard 已提交
5212
    X86_64_DEF([INDEX_op_sbbq ## SUFFIX ## _T0_T1_cc] = CC_C,)\
5213 5214 5215 5216
\
    [INDEX_op_rclb ## SUFFIX ## _T0_T1_cc] = CC_C,\
    [INDEX_op_rclw ## SUFFIX ## _T0_T1_cc] = CC_C,\
    [INDEX_op_rcll ## SUFFIX ## _T0_T1_cc] = CC_C,\
B
bellard 已提交
5217
    X86_64_DEF([INDEX_op_rclq ## SUFFIX ## _T0_T1_cc] = CC_C,)\
5218 5219
    [INDEX_op_rcrb ## SUFFIX ## _T0_T1_cc] = CC_C,\
    [INDEX_op_rcrw ## SUFFIX ## _T0_T1_cc] = CC_C,\
B
bellard 已提交
5220 5221
    [INDEX_op_rcrl ## SUFFIX ## _T0_T1_cc] = CC_C,\
    X86_64_DEF([INDEX_op_rcrq ## SUFFIX ## _T0_T1_cc] = CC_C,)
5222

5223
    DEF_READF( )
5224 5225 5226 5227 5228
    DEF_READF(_raw)
#ifndef CONFIG_USER_ONLY
    DEF_READF(_kernel)
    DEF_READF(_user)
#endif
B
bellard 已提交
5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243
};

/* flags written by an operation */
static uint16_t opc_write_flags[NB_OPS] = { 
    [INDEX_op_update2_cc] = CC_OSZAPC,
    [INDEX_op_update1_cc] = CC_OSZAPC,
    [INDEX_op_cmpl_T0_T1_cc] = CC_OSZAPC,
    [INDEX_op_update_neg_cc] = CC_OSZAPC,
    /* subtle: due to the incl/decl implementation, C is used */
    [INDEX_op_update_inc_cc] = CC_OSZAPC, 
    [INDEX_op_testl_T0_T1_cc] = CC_OSZAPC,

    [INDEX_op_mulb_AL_T0] = CC_OSZAPC,
    [INDEX_op_mulw_AX_T0] = CC_OSZAPC,
    [INDEX_op_mull_EAX_T0] = CC_OSZAPC,
B
bellard 已提交
5244 5245 5246
    X86_64_DEF([INDEX_op_mulq_EAX_T0] = CC_OSZAPC,)
    [INDEX_op_imulb_AL_T0] = CC_OSZAPC,
    [INDEX_op_imulw_AX_T0] = CC_OSZAPC,
B
bellard 已提交
5247
    [INDEX_op_imull_EAX_T0] = CC_OSZAPC,
B
bellard 已提交
5248
    X86_64_DEF([INDEX_op_imulq_EAX_T0] = CC_OSZAPC,)
B
bellard 已提交
5249 5250
    [INDEX_op_imulw_T0_T1] = CC_OSZAPC,
    [INDEX_op_imull_T0_T1] = CC_OSZAPC,
B
bellard 已提交
5251 5252
    X86_64_DEF([INDEX_op_imulq_T0_T1] = CC_OSZAPC,)

B
bellard 已提交
5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263
    /* bcd */
    [INDEX_op_aam] = CC_OSZAPC,
    [INDEX_op_aad] = CC_OSZAPC,
    [INDEX_op_aas] = CC_OSZAPC,
    [INDEX_op_aaa] = CC_OSZAPC,
    [INDEX_op_das] = CC_OSZAPC,
    [INDEX_op_daa] = CC_OSZAPC,

    [INDEX_op_movb_eflags_T0] = CC_S | CC_Z | CC_A | CC_P | CC_C,
    [INDEX_op_movw_eflags_T0] = CC_OSZAPC,
    [INDEX_op_movl_eflags_T0] = CC_OSZAPC,
B
bellard 已提交
5264 5265 5266 5267
    [INDEX_op_movw_eflags_T0_io] = CC_OSZAPC,
    [INDEX_op_movl_eflags_T0_io] = CC_OSZAPC,
    [INDEX_op_movw_eflags_T0_cpl0] = CC_OSZAPC,
    [INDEX_op_movl_eflags_T0_cpl0] = CC_OSZAPC,
B
bellard 已提交
5268 5269 5270 5271 5272 5273
    [INDEX_op_clc] = CC_C,
    [INDEX_op_stc] = CC_C,
    [INDEX_op_cmc] = CC_C,

    [INDEX_op_btw_T0_T1_cc] = CC_OSZAPC,
    [INDEX_op_btl_T0_T1_cc] = CC_OSZAPC,
B
bellard 已提交
5274
    X86_64_DEF([INDEX_op_btq_T0_T1_cc] = CC_OSZAPC,)
B
bellard 已提交
5275 5276
    [INDEX_op_btsw_T0_T1_cc] = CC_OSZAPC,
    [INDEX_op_btsl_T0_T1_cc] = CC_OSZAPC,
B
bellard 已提交
5277
    X86_64_DEF([INDEX_op_btsq_T0_T1_cc] = CC_OSZAPC,)
B
bellard 已提交
5278 5279
    [INDEX_op_btrw_T0_T1_cc] = CC_OSZAPC,
    [INDEX_op_btrl_T0_T1_cc] = CC_OSZAPC,
B
bellard 已提交
5280
    X86_64_DEF([INDEX_op_btrq_T0_T1_cc] = CC_OSZAPC,)
B
bellard 已提交
5281 5282
    [INDEX_op_btcw_T0_T1_cc] = CC_OSZAPC,
    [INDEX_op_btcl_T0_T1_cc] = CC_OSZAPC,
B
bellard 已提交
5283
    X86_64_DEF([INDEX_op_btcq_T0_T1_cc] = CC_OSZAPC,)
B
bellard 已提交
5284 5285 5286

    [INDEX_op_bsfw_T0_cc] = CC_OSZAPC,
    [INDEX_op_bsfl_T0_cc] = CC_OSZAPC,
B
bellard 已提交
5287
    X86_64_DEF([INDEX_op_bsfq_T0_cc] = CC_OSZAPC,)
B
bellard 已提交
5288 5289
    [INDEX_op_bsrw_T0_cc] = CC_OSZAPC,
    [INDEX_op_bsrl_T0_cc] = CC_OSZAPC,
B
bellard 已提交
5290
    X86_64_DEF([INDEX_op_bsrq_T0_cc] = CC_OSZAPC,)
B
bellard 已提交
5291 5292 5293 5294

    [INDEX_op_cmpxchgb_T0_T1_EAX_cc] = CC_OSZAPC,
    [INDEX_op_cmpxchgw_T0_T1_EAX_cc] = CC_OSZAPC,
    [INDEX_op_cmpxchgl_T0_T1_EAX_cc] = CC_OSZAPC,
B
bellard 已提交
5295
    X86_64_DEF([INDEX_op_cmpxchgq_T0_T1_EAX_cc] = CC_OSZAPC,)
B
bellard 已提交
5296 5297 5298 5299 5300 5301

    [INDEX_op_cmpxchg8b] = CC_Z,
    [INDEX_op_lar] = CC_Z,
    [INDEX_op_lsl] = CC_Z,
    [INDEX_op_fcomi_ST0_FT0] = CC_Z | CC_P | CC_C,
    [INDEX_op_fucomi_ST0_FT0] = CC_Z | CC_P | CC_C,
5302 5303 5304 5305 5306

#define DEF_WRITEF(SUFFIX)\
    [INDEX_op_adcb ## SUFFIX ## _T0_T1_cc] = CC_OSZAPC,\
    [INDEX_op_adcw ## SUFFIX ## _T0_T1_cc] = CC_OSZAPC,\
    [INDEX_op_adcl ## SUFFIX ## _T0_T1_cc] = CC_OSZAPC,\
B
bellard 已提交
5307
    X86_64_DEF([INDEX_op_adcq ## SUFFIX ## _T0_T1_cc] = CC_OSZAPC,)\
5308 5309 5310
    [INDEX_op_sbbb ## SUFFIX ## _T0_T1_cc] = CC_OSZAPC,\
    [INDEX_op_sbbw ## SUFFIX ## _T0_T1_cc] = CC_OSZAPC,\
    [INDEX_op_sbbl ## SUFFIX ## _T0_T1_cc] = CC_OSZAPC,\
B
bellard 已提交
5311
    X86_64_DEF([INDEX_op_sbbq ## SUFFIX ## _T0_T1_cc] = CC_OSZAPC,)\
5312 5313 5314 5315
\
    [INDEX_op_rolb ## SUFFIX ## _T0_T1_cc] = CC_O | CC_C,\
    [INDEX_op_rolw ## SUFFIX ## _T0_T1_cc] = CC_O | CC_C,\
    [INDEX_op_roll ## SUFFIX ## _T0_T1_cc] = CC_O | CC_C,\
B
bellard 已提交
5316
    X86_64_DEF([INDEX_op_rolq ## SUFFIX ## _T0_T1_cc] = CC_O | CC_C,)\
5317 5318 5319
    [INDEX_op_rorb ## SUFFIX ## _T0_T1_cc] = CC_O | CC_C,\
    [INDEX_op_rorw ## SUFFIX ## _T0_T1_cc] = CC_O | CC_C,\
    [INDEX_op_rorl ## SUFFIX ## _T0_T1_cc] = CC_O | CC_C,\
B
bellard 已提交
5320
    X86_64_DEF([INDEX_op_rorq ## SUFFIX ## _T0_T1_cc] = CC_O | CC_C,)\
5321 5322 5323 5324
\
    [INDEX_op_rclb ## SUFFIX ## _T0_T1_cc] = CC_O | CC_C,\
    [INDEX_op_rclw ## SUFFIX ## _T0_T1_cc] = CC_O | CC_C,\
    [INDEX_op_rcll ## SUFFIX ## _T0_T1_cc] = CC_O | CC_C,\
B
bellard 已提交
5325
    X86_64_DEF([INDEX_op_rclq ## SUFFIX ## _T0_T1_cc] = CC_O | CC_C,)\
5326 5327 5328
    [INDEX_op_rcrb ## SUFFIX ## _T0_T1_cc] = CC_O | CC_C,\
    [INDEX_op_rcrw ## SUFFIX ## _T0_T1_cc] = CC_O | CC_C,\
    [INDEX_op_rcrl ## SUFFIX ## _T0_T1_cc] = CC_O | CC_C,\
B
bellard 已提交
5329
    X86_64_DEF([INDEX_op_rcrq ## SUFFIX ## _T0_T1_cc] = CC_O | CC_C,)\
5330 5331 5332 5333
\
    [INDEX_op_shlb ## SUFFIX ## _T0_T1_cc] = CC_OSZAPC,\
    [INDEX_op_shlw ## SUFFIX ## _T0_T1_cc] = CC_OSZAPC,\
    [INDEX_op_shll ## SUFFIX ## _T0_T1_cc] = CC_OSZAPC,\
B
bellard 已提交
5334
    X86_64_DEF([INDEX_op_shlq ## SUFFIX ## _T0_T1_cc] = CC_OSZAPC,)\
5335 5336 5337 5338
\
    [INDEX_op_shrb ## SUFFIX ## _T0_T1_cc] = CC_OSZAPC,\
    [INDEX_op_shrw ## SUFFIX ## _T0_T1_cc] = CC_OSZAPC,\
    [INDEX_op_shrl ## SUFFIX ## _T0_T1_cc] = CC_OSZAPC,\
B
bellard 已提交
5339
    X86_64_DEF([INDEX_op_shrq ## SUFFIX ## _T0_T1_cc] = CC_OSZAPC,)\
5340 5341 5342 5343
\
    [INDEX_op_sarb ## SUFFIX ## _T0_T1_cc] = CC_OSZAPC,\
    [INDEX_op_sarw ## SUFFIX ## _T0_T1_cc] = CC_OSZAPC,\
    [INDEX_op_sarl ## SUFFIX ## _T0_T1_cc] = CC_OSZAPC,\
B
bellard 已提交
5344
    X86_64_DEF([INDEX_op_sarq ## SUFFIX ## _T0_T1_cc] = CC_OSZAPC,)\
5345 5346 5347
\
    [INDEX_op_shldw ## SUFFIX ## _T0_T1_ECX_cc] = CC_OSZAPC,\
    [INDEX_op_shldl ## SUFFIX ## _T0_T1_ECX_cc] = CC_OSZAPC,\
B
bellard 已提交
5348
    X86_64_DEF([INDEX_op_shldq ## SUFFIX ## _T0_T1_ECX_cc] = CC_OSZAPC,)\
5349 5350
    [INDEX_op_shldw ## SUFFIX ## _T0_T1_im_cc] = CC_OSZAPC,\
    [INDEX_op_shldl ## SUFFIX ## _T0_T1_im_cc] = CC_OSZAPC,\
B
bellard 已提交
5351
    X86_64_DEF([INDEX_op_shldq ## SUFFIX ## _T0_T1_im_cc] = CC_OSZAPC,)\
5352 5353 5354
\
    [INDEX_op_shrdw ## SUFFIX ## _T0_T1_ECX_cc] = CC_OSZAPC,\
    [INDEX_op_shrdl ## SUFFIX ## _T0_T1_ECX_cc] = CC_OSZAPC,\
B
bellard 已提交
5355
    X86_64_DEF([INDEX_op_shrdq ## SUFFIX ## _T0_T1_ECX_cc] = CC_OSZAPC,)\
5356 5357
    [INDEX_op_shrdw ## SUFFIX ## _T0_T1_im_cc] = CC_OSZAPC,\
    [INDEX_op_shrdl ## SUFFIX ## _T0_T1_im_cc] = CC_OSZAPC,\
B
bellard 已提交
5358
    X86_64_DEF([INDEX_op_shrdq ## SUFFIX ## _T0_T1_im_cc] = CC_OSZAPC,)\
5359 5360 5361
\
    [INDEX_op_cmpxchgb ## SUFFIX ## _T0_T1_EAX_cc] = CC_OSZAPC,\
    [INDEX_op_cmpxchgw ## SUFFIX ## _T0_T1_EAX_cc] = CC_OSZAPC,\
B
bellard 已提交
5362 5363
    [INDEX_op_cmpxchgl ## SUFFIX ## _T0_T1_EAX_cc] = CC_OSZAPC,\
    X86_64_DEF([INDEX_op_cmpxchgq ## SUFFIX ## _T0_T1_EAX_cc] = CC_OSZAPC,)
5364 5365


5366
    DEF_WRITEF( )
5367 5368 5369 5370 5371
    DEF_WRITEF(_raw)
#ifndef CONFIG_USER_ONLY
    DEF_WRITEF(_kernel)
    DEF_WRITEF(_user)
#endif
B
bellard 已提交
5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386
};

/* simpler form of an operation if no flags need to be generated */
static uint16_t opc_simpler[NB_OPS] = { 
    [INDEX_op_update2_cc] = INDEX_op_nop,
    [INDEX_op_update1_cc] = INDEX_op_nop,
    [INDEX_op_update_neg_cc] = INDEX_op_nop,
#if 0
    /* broken: CC_OP logic must be rewritten */
    [INDEX_op_update_inc_cc] = INDEX_op_nop,
#endif

    [INDEX_op_shlb_T0_T1_cc] = INDEX_op_shlb_T0_T1,
    [INDEX_op_shlw_T0_T1_cc] = INDEX_op_shlw_T0_T1,
    [INDEX_op_shll_T0_T1_cc] = INDEX_op_shll_T0_T1,
B
bellard 已提交
5387
    X86_64_DEF([INDEX_op_shlq_T0_T1_cc] = INDEX_op_shlq_T0_T1,)
B
bellard 已提交
5388 5389 5390 5391

    [INDEX_op_shrb_T0_T1_cc] = INDEX_op_shrb_T0_T1,
    [INDEX_op_shrw_T0_T1_cc] = INDEX_op_shrw_T0_T1,
    [INDEX_op_shrl_T0_T1_cc] = INDEX_op_shrl_T0_T1,
B
bellard 已提交
5392
    X86_64_DEF([INDEX_op_shrq_T0_T1_cc] = INDEX_op_shrq_T0_T1,)
B
bellard 已提交
5393 5394 5395 5396

    [INDEX_op_sarb_T0_T1_cc] = INDEX_op_sarb_T0_T1,
    [INDEX_op_sarw_T0_T1_cc] = INDEX_op_sarw_T0_T1,
    [INDEX_op_sarl_T0_T1_cc] = INDEX_op_sarl_T0_T1,
B
bellard 已提交
5397
    X86_64_DEF([INDEX_op_sarq_T0_T1_cc] = INDEX_op_sarq_T0_T1,)
5398 5399 5400 5401 5402

#define DEF_SIMPLER(SUFFIX)\
    [INDEX_op_rolb ## SUFFIX ## _T0_T1_cc] = INDEX_op_rolb ## SUFFIX ## _T0_T1,\
    [INDEX_op_rolw ## SUFFIX ## _T0_T1_cc] = INDEX_op_rolw ## SUFFIX ## _T0_T1,\
    [INDEX_op_roll ## SUFFIX ## _T0_T1_cc] = INDEX_op_roll ## SUFFIX ## _T0_T1,\
B
bellard 已提交
5403
    X86_64_DEF([INDEX_op_rolq ## SUFFIX ## _T0_T1_cc] = INDEX_op_rolq ## SUFFIX ## _T0_T1,)\
5404 5405 5406
\
    [INDEX_op_rorb ## SUFFIX ## _T0_T1_cc] = INDEX_op_rorb ## SUFFIX ## _T0_T1,\
    [INDEX_op_rorw ## SUFFIX ## _T0_T1_cc] = INDEX_op_rorw ## SUFFIX ## _T0_T1,\
B
bellard 已提交
5407 5408
    [INDEX_op_rorl ## SUFFIX ## _T0_T1_cc] = INDEX_op_rorl ## SUFFIX ## _T0_T1,\
    X86_64_DEF([INDEX_op_rorq ## SUFFIX ## _T0_T1_cc] = INDEX_op_rorq ## SUFFIX ## _T0_T1,)
5409

5410
    DEF_SIMPLER( )
5411 5412 5413 5414 5415
    DEF_SIMPLER(_raw)
#ifndef CONFIG_USER_ONLY
    DEF_SIMPLER(_kernel)
    DEF_SIMPLER(_user)
#endif
B
bellard 已提交
5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462
};

void optimize_flags_init(void)
{
    int i;
    /* put default values in arrays */
    for(i = 0; i < NB_OPS; i++) {
        if (opc_simpler[i] == 0)
            opc_simpler[i] = i;
    }
}

/* CPU flags computation optimization: we move backward thru the
   generated code to see which flags are needed. The operation is
   modified if suitable */
static void optimize_flags(uint16_t *opc_buf, int opc_buf_len)
{
    uint16_t *opc_ptr;
    int live_flags, write_flags, op;

    opc_ptr = opc_buf + opc_buf_len;
    /* live_flags contains the flags needed by the next instructions
       in the code. At the end of the bloc, we consider that all the
       flags are live. */
    live_flags = CC_OSZAPC;
    while (opc_ptr > opc_buf) {
        op = *--opc_ptr;
        /* if none of the flags written by the instruction is used,
           then we can try to find a simpler instruction */
        write_flags = opc_write_flags[op];
        if ((live_flags & write_flags) == 0) {
            *opc_ptr = opc_simpler[op];
        }
        /* compute the live flags before the instruction */
        live_flags &= ~write_flags;
        live_flags |= opc_read_flags[op];
    }
}

/* 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. */
static inline int gen_intermediate_code_internal(CPUState *env,
                                                 TranslationBlock *tb, 
                                                 int search_pc)
{
    DisasContext dc1, *dc = &dc1;
B
bellard 已提交
5463
    target_ulong pc_ptr;
B
bellard 已提交
5464
    uint16_t *gen_opc_end;
B
bellard 已提交
5465
    int flags, j, lj, cflags;
B
bellard 已提交
5466 5467
    target_ulong pc_start;
    target_ulong cs_base;
B
bellard 已提交
5468 5469
    
    /* generate intermediate code */
B
bellard 已提交
5470 5471
    pc_start = tb->pc;
    cs_base = tb->cs_base;
B
bellard 已提交
5472
    flags = tb->flags;
B
bellard 已提交
5473
    cflags = tb->cflags;
B
bellard 已提交
5474

5475
    dc->pe = (flags >> HF_PE_SHIFT) & 1;
B
bellard 已提交
5476 5477 5478 5479 5480 5481 5482 5483
    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;
5484
    dc->singlestep_enabled = env->singlestep_enabled;
B
bellard 已提交
5485 5486 5487 5488 5489 5490 5491 5492
    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 已提交
5493
            dc->mem_index = 2 * 4;
B
bellard 已提交
5494
        else
B
bellard 已提交
5495
            dc->mem_index = 1 * 4;
B
bellard 已提交
5496
    }
B
bellard 已提交
5497 5498 5499 5500 5501
    dc->cpuid_features = env->cpuid_features;
#ifdef TARGET_X86_64
    dc->lma = (flags >> HF_LMA_SHIFT) & 1;
    dc->code64 = (flags >> HF_CS64_SHIFT) & 1;
#endif
B
bellard 已提交
5502
    dc->flags = flags;
5503 5504
    dc->jmp_opt = !(dc->tf || env->singlestep_enabled ||
                    (flags & HF_INHIBIT_IRQ_MASK)
B
bellard 已提交
5505
#ifndef CONFIG_SOFTMMU
B
bellard 已提交
5506 5507 5508
                    || (flags & HF_SOFTMMU_MASK)
#endif
                    );
5509 5510
#if 0
    /* check addseg logic */
B
bellard 已提交
5511
    if (!dc->addseg && (dc->vm86 || !dc->pe || !dc->code32))
5512 5513 5514
        printf("ERROR addseg\n");
#endif

B
bellard 已提交
5515 5516 5517
    gen_opc_ptr = gen_opc_buf;
    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
    gen_opparam_ptr = gen_opparam_buf;
B
bellard 已提交
5518
    nb_gen_labels = 0;
B
bellard 已提交
5519 5520 5521 5522 5523 5524 5525 5526

    dc->is_jmp = DISAS_NEXT;
    pc_ptr = pc_start;
    lj = -1;

    for(;;) {
        if (env->nb_breakpoints > 0) {
            for(j = 0; j < env->nb_breakpoints; j++) {
B
bellard 已提交
5527
                if (env->breakpoints[j] == pc_ptr) {
B
bellard 已提交
5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539
                    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 已提交
5540
            gen_opc_pc[lj] = pc_ptr;
B
bellard 已提交
5541 5542 5543 5544 5545 5546 5547 5548 5549
            gen_opc_cc_op[lj] = dc->cc_op;
            gen_opc_instr_start[lj] = 1;
        }
        pc_ptr = disas_insn(dc, pc_ptr);
        /* stop translation if indicated */
        if (dc->is_jmp)
            break;
        /* if single step mode, we generate only one instruction and
           generate an exception */
5550 5551 5552 5553
        /* 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 */
        if (dc->tf || dc->singlestep_enabled || 
B
bellard 已提交
5554 5555
            (flags & HF_INHIBIT_IRQ_MASK) ||
            (cflags & CF_SINGLE_INSN)) {
B
bellard 已提交
5556
            gen_jmp_im(pc_ptr - dc->cs_base);
B
bellard 已提交
5557 5558 5559 5560 5561 5562
            gen_eob(dc);
            break;
        }
        /* if too long translation, stop generation too */
        if (gen_opc_ptr >= gen_opc_end ||
            (pc_ptr - pc_start) >= (TARGET_PAGE_SIZE - 32)) {
B
bellard 已提交
5563
            gen_jmp_im(pc_ptr - dc->cs_base);
B
bellard 已提交
5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577
            gen_eob(dc);
            break;
        }
    }
    *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;
    }
        
#ifdef DEBUG_DISAS
B
bellard 已提交
5578
    if (loglevel & CPU_LOG_TB_CPU) {
B
bellard 已提交
5579
        cpu_dump_state(env, logfile, fprintf, X86_DUMP_CCOP);
B
bellard 已提交
5580
    }
B
bellard 已提交
5581
    if (loglevel & CPU_LOG_TB_IN_ASM) {
B
bellard 已提交
5582
        int disas_flags;
B
bellard 已提交
5583 5584
        fprintf(logfile, "----------------\n");
        fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
B
bellard 已提交
5585 5586 5587 5588 5589 5590 5591
#ifdef TARGET_X86_64
        if (dc->code64)
            disas_flags = 2;
        else
#endif
            disas_flags = !dc->code32;
	target_disas(logfile, pc_start, pc_ptr - pc_start, disas_flags);
B
bellard 已提交
5592
        fprintf(logfile, "\n");
B
bellard 已提交
5593 5594 5595 5596 5597
        if (loglevel & CPU_LOG_TB_OP) {
            fprintf(logfile, "OP:\n");
            dump_ops(gen_opc_buf, gen_opparam_buf);
            fprintf(logfile, "\n");
        }
B
bellard 已提交
5598 5599 5600 5601 5602 5603 5604
    }
#endif

    /* optimize flag computations */
    optimize_flags(gen_opc_buf, gen_opc_ptr - gen_opc_buf);

#ifdef DEBUG_DISAS
B
bellard 已提交
5605
    if (loglevel & CPU_LOG_TB_OP_OPT) {
B
bellard 已提交
5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625
        fprintf(logfile, "AFTER FLAGS OPT:\n");
        dump_ops(gen_opc_buf, gen_opparam_buf);
        fprintf(logfile, "\n");
    }
#endif
    if (!search_pc)
        tb->size = pc_ptr - pc_start;
    return 0;
}

int gen_intermediate_code(CPUState *env, TranslationBlock *tb)
{
    return gen_intermediate_code_internal(env, tb, 0);
}

int gen_intermediate_code_pc(CPUState *env, TranslationBlock *tb)
{
    return gen_intermediate_code_internal(env, tb, 1);
}