]> git.proxmox.com Git - mirror_qemu.git/blame - target-i386/translate.c
target-i386: inline gen_prepare_cc_slow
[mirror_qemu.git] / target-i386 / translate.c
CommitLineData
2c0262af
FB
1/*
2 * i386 translation
5fafdf24 3 *
2c0262af
FB
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
8167ee88 17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
2c0262af
FB
18 */
19#include <stdarg.h>
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
23#include <inttypes.h>
24#include <signal.h>
2c0262af 25
bec93d72 26#include "qemu/host-utils.h"
2c0262af 27#include "cpu.h"
76cad711 28#include "disas/disas.h"
57fec1fe 29#include "tcg-op.h"
2c0262af 30
a7812ae4
PB
31#include "helper.h"
32#define GEN_HELPER 1
33#include "helper.h"
34
2c0262af
FB
35#define PREFIX_REPZ 0x01
36#define PREFIX_REPNZ 0x02
37#define PREFIX_LOCK 0x04
38#define PREFIX_DATA 0x08
39#define PREFIX_ADR 0x10
40
14ce26e7 41#ifdef TARGET_X86_64
14ce26e7
FB
42#define CODE64(s) ((s)->code64)
43#define REX_X(s) ((s)->rex_x)
44#define REX_B(s) ((s)->rex_b)
14ce26e7 45#else
14ce26e7
FB
46#define CODE64(s) 0
47#define REX_X(s) 0
48#define REX_B(s) 0
49#endif
50
bec93d72
RH
51#ifdef TARGET_X86_64
52# define ctztl ctz64
53# define clztl clz64
54#else
55# define ctztl ctz32
56# define clztl clz32
57#endif
58
57fec1fe
FB
59//#define MACRO_TEST 1
60
57fec1fe 61/* global register indexes */
a7812ae4 62static TCGv_ptr cpu_env;
f5847c91 63static TCGv cpu_A0, cpu_cc_src, cpu_cc_dst;
a7812ae4 64static TCGv_i32 cpu_cc_op;
cc739bb0 65static TCGv cpu_regs[CPU_NB_REGS];
1e4840bf
FB
66/* local temps */
67static TCGv cpu_T[2], cpu_T3;
57fec1fe 68/* local register indexes (only used inside old micro ops) */
a7812ae4
PB
69static TCGv cpu_tmp0, cpu_tmp4;
70static TCGv_ptr cpu_ptr0, cpu_ptr1;
71static TCGv_i32 cpu_tmp2_i32, cpu_tmp3_i32;
72static TCGv_i64 cpu_tmp1_i64;
bedda79c 73static TCGv cpu_tmp5;
57fec1fe 74
1a7ff922
PB
75static uint8_t gen_opc_cc_op[OPC_BUF_SIZE];
76
022c62cb 77#include "exec/gen-icount.h"
2e70f6ef 78
57fec1fe
FB
79#ifdef TARGET_X86_64
80static int x86_64_hregs;
ae063a68
FB
81#endif
82
2c0262af
FB
83typedef struct DisasContext {
84 /* current insn context */
85 int override; /* -1 if no override */
86 int prefix;
87 int aflag, dflag;
14ce26e7 88 target_ulong pc; /* pc = eip + cs_base */
2c0262af
FB
89 int is_jmp; /* 1 = means jump (stop translation), 2 means CPU
90 static state change (stop translation) */
91 /* current block context */
14ce26e7 92 target_ulong cs_base; /* base of CS segment */
2c0262af
FB
93 int pe; /* protected mode */
94 int code32; /* 32 bit code segment */
14ce26e7
FB
95#ifdef TARGET_X86_64
96 int lma; /* long mode active */
97 int code64; /* 64 bit code segment */
98 int rex_x, rex_b;
99#endif
2c0262af 100 int ss32; /* 32 bit stack segment */
fee71888 101 CCOp cc_op; /* current CC operation */
e207582f 102 bool cc_op_dirty;
2c0262af
FB
103 int addseg; /* non zero if either DS/ES/SS have a non zero base */
104 int f_st; /* currently unused */
105 int vm86; /* vm86 mode */
106 int cpl;
107 int iopl;
108 int tf; /* TF cpu flag */
34865134 109 int singlestep_enabled; /* "hardware" single step enabled */
2c0262af
FB
110 int jmp_opt; /* use direct block chaining for direct jumps */
111 int mem_index; /* select memory access functions */
c068688b 112 uint64_t flags; /* all execution flags */
2c0262af
FB
113 struct TranslationBlock *tb;
114 int popl_esp_hack; /* for correct popl with esp base handling */
14ce26e7
FB
115 int rip_offset; /* only used in x86_64, but left for simplicity */
116 int cpuid_features;
3d7374c5 117 int cpuid_ext_features;
e771edab 118 int cpuid_ext2_features;
12e26b75 119 int cpuid_ext3_features;
a9321a4d 120 int cpuid_7_0_ebx_features;
2c0262af
FB
121} DisasContext;
122
123static void gen_eob(DisasContext *s);
14ce26e7
FB
124static void gen_jmp(DisasContext *s, target_ulong eip);
125static void gen_jmp_tb(DisasContext *s, target_ulong eip, int tb_num);
2c0262af
FB
126
127/* i386 arith/logic operations */
128enum {
5fafdf24
TS
129 OP_ADDL,
130 OP_ORL,
131 OP_ADCL,
2c0262af 132 OP_SBBL,
5fafdf24
TS
133 OP_ANDL,
134 OP_SUBL,
135 OP_XORL,
2c0262af
FB
136 OP_CMPL,
137};
138
139/* i386 shift ops */
140enum {
5fafdf24
TS
141 OP_ROL,
142 OP_ROR,
143 OP_RCL,
144 OP_RCR,
145 OP_SHL,
146 OP_SHR,
2c0262af
FB
147 OP_SHL1, /* undocumented */
148 OP_SAR = 7,
149};
150
8e1c85e3
FB
151enum {
152 JCC_O,
153 JCC_B,
154 JCC_Z,
155 JCC_BE,
156 JCC_S,
157 JCC_P,
158 JCC_L,
159 JCC_LE,
160};
161
2c0262af
FB
162/* operand size */
163enum {
164 OT_BYTE = 0,
165 OT_WORD,
5fafdf24 166 OT_LONG,
2c0262af
FB
167 OT_QUAD,
168};
169
170enum {
171 /* I386 int registers */
172 OR_EAX, /* MUST be even numbered */
173 OR_ECX,
174 OR_EDX,
175 OR_EBX,
176 OR_ESP,
177 OR_EBP,
178 OR_ESI,
179 OR_EDI,
14ce26e7
FB
180
181 OR_TMP0 = 16, /* temporary operand register */
2c0262af
FB
182 OR_TMP1,
183 OR_A0, /* temporary register used when doing address evaluation */
2c0262af
FB
184};
185
b666265b
RH
186enum {
187 USES_CC_DST = 1,
188 USES_CC_SRC = 2,
189};
190
191/* Bit set if the global variable is live after setting CC_OP to X. */
192static const uint8_t cc_op_live[CC_OP_NB] = {
193 [CC_OP_DYNAMIC] = USES_CC_DST | USES_CC_SRC,
194 [CC_OP_EFLAGS] = USES_CC_SRC,
195 [CC_OP_MULB ... CC_OP_MULQ] = USES_CC_DST | USES_CC_SRC,
196 [CC_OP_ADDB ... CC_OP_ADDQ] = USES_CC_DST | USES_CC_SRC,
197 [CC_OP_ADCB ... CC_OP_ADCQ] = USES_CC_DST | USES_CC_SRC,
198 [CC_OP_SUBB ... CC_OP_SUBQ] = USES_CC_DST | USES_CC_SRC,
199 [CC_OP_SBBB ... CC_OP_SBBQ] = USES_CC_DST | USES_CC_SRC,
200 [CC_OP_LOGICB ... CC_OP_LOGICQ] = USES_CC_DST,
201 [CC_OP_INCB ... CC_OP_INCQ] = USES_CC_DST | USES_CC_SRC,
202 [CC_OP_DECB ... CC_OP_DECQ] = USES_CC_DST | USES_CC_SRC,
203 [CC_OP_SHLB ... CC_OP_SHLQ] = USES_CC_DST | USES_CC_SRC,
204 [CC_OP_SARB ... CC_OP_SARQ] = USES_CC_DST | USES_CC_SRC,
205};
206
e207582f 207static void set_cc_op(DisasContext *s, CCOp op)
3ca51d07 208{
b666265b
RH
209 int dead;
210
211 if (s->cc_op == op) {
212 return;
213 }
214
215 /* Discard CC computation that will no longer be used. */
216 dead = cc_op_live[s->cc_op] & ~cc_op_live[op];
217 if (dead & USES_CC_DST) {
218 tcg_gen_discard_tl(cpu_cc_dst);
e207582f 219 }
b666265b
RH
220 if (dead & USES_CC_SRC) {
221 tcg_gen_discard_tl(cpu_cc_src);
222 }
223
224 s->cc_op = op;
225 /* The DYNAMIC setting is translator only, and should never be
226 stored. Thus we always consider it clean. */
227 s->cc_op_dirty = (op != CC_OP_DYNAMIC);
e207582f
RH
228}
229
e207582f
RH
230static void gen_update_cc_op(DisasContext *s)
231{
232 if (s->cc_op_dirty) {
773cdfcc 233 tcg_gen_movi_i32(cpu_cc_op, s->cc_op);
e207582f
RH
234 s->cc_op_dirty = false;
235 }
3ca51d07
RH
236}
237
57fec1fe
FB
238static inline void gen_op_movl_T0_0(void)
239{
240 tcg_gen_movi_tl(cpu_T[0], 0);
241}
242
243static inline void gen_op_movl_T0_im(int32_t val)
244{
245 tcg_gen_movi_tl(cpu_T[0], val);
246}
247
248static inline void gen_op_movl_T0_imu(uint32_t val)
249{
250 tcg_gen_movi_tl(cpu_T[0], val);
251}
252
253static inline void gen_op_movl_T1_im(int32_t val)
254{
255 tcg_gen_movi_tl(cpu_T[1], val);
256}
257
258static inline void gen_op_movl_T1_imu(uint32_t val)
259{
260 tcg_gen_movi_tl(cpu_T[1], val);
261}
262
263static inline void gen_op_movl_A0_im(uint32_t val)
264{
265 tcg_gen_movi_tl(cpu_A0, val);
266}
267
268#ifdef TARGET_X86_64
269static inline void gen_op_movq_A0_im(int64_t val)
270{
271 tcg_gen_movi_tl(cpu_A0, val);
272}
273#endif
274
275static inline void gen_movtl_T0_im(target_ulong val)
276{
277 tcg_gen_movi_tl(cpu_T[0], val);
278}
279
280static inline void gen_movtl_T1_im(target_ulong val)
281{
282 tcg_gen_movi_tl(cpu_T[1], val);
283}
284
285static inline void gen_op_andl_T0_ffff(void)
286{
287 tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xffff);
288}
289
290static inline void gen_op_andl_T0_im(uint32_t val)
291{
292 tcg_gen_andi_tl(cpu_T[0], cpu_T[0], val);
293}
294
295static inline void gen_op_movl_T0_T1(void)
296{
297 tcg_gen_mov_tl(cpu_T[0], cpu_T[1]);
298}
299
300static inline void gen_op_andl_A0_ffff(void)
301{
302 tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffff);
303}
304
14ce26e7
FB
305#ifdef TARGET_X86_64
306
307#define NB_OP_SIZES 4
308
14ce26e7
FB
309#else /* !TARGET_X86_64 */
310
311#define NB_OP_SIZES 3
312
14ce26e7
FB
313#endif /* !TARGET_X86_64 */
314
e2542fe2 315#if defined(HOST_WORDS_BIGENDIAN)
57fec1fe
FB
316#define REG_B_OFFSET (sizeof(target_ulong) - 1)
317#define REG_H_OFFSET (sizeof(target_ulong) - 2)
318#define REG_W_OFFSET (sizeof(target_ulong) - 2)
319#define REG_L_OFFSET (sizeof(target_ulong) - 4)
320#define REG_LH_OFFSET (sizeof(target_ulong) - 8)
14ce26e7 321#else
57fec1fe
FB
322#define REG_B_OFFSET 0
323#define REG_H_OFFSET 1
324#define REG_W_OFFSET 0
325#define REG_L_OFFSET 0
326#define REG_LH_OFFSET 4
14ce26e7 327#endif
57fec1fe 328
96d7073f
PM
329/* In instruction encodings for byte register accesses the
330 * register number usually indicates "low 8 bits of register N";
331 * however there are some special cases where N 4..7 indicates
332 * [AH, CH, DH, BH], ie "bits 15..8 of register N-4". Return
333 * true for this special case, false otherwise.
334 */
335static inline bool byte_reg_is_xH(int reg)
336{
337 if (reg < 4) {
338 return false;
339 }
340#ifdef TARGET_X86_64
341 if (reg >= 8 || x86_64_hregs) {
342 return false;
343 }
344#endif
345 return true;
346}
347
1e4840bf 348static inline void gen_op_mov_reg_v(int ot, int reg, TCGv t0)
57fec1fe
FB
349{
350 switch(ot) {
351 case OT_BYTE:
96d7073f 352 if (!byte_reg_is_xH(reg)) {
c832e3de 353 tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], t0, 0, 8);
57fec1fe 354 } else {
c832e3de 355 tcg_gen_deposit_tl(cpu_regs[reg - 4], cpu_regs[reg - 4], t0, 8, 8);
57fec1fe
FB
356 }
357 break;
358 case OT_WORD:
c832e3de 359 tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], t0, 0, 16);
57fec1fe 360 break;
cc739bb0 361 default: /* XXX this shouldn't be reached; abort? */
57fec1fe 362 case OT_LONG:
cc739bb0
LD
363 /* For x86_64, this sets the higher half of register to zero.
364 For i386, this is equivalent to a mov. */
365 tcg_gen_ext32u_tl(cpu_regs[reg], t0);
57fec1fe 366 break;
cc739bb0 367#ifdef TARGET_X86_64
57fec1fe 368 case OT_QUAD:
cc739bb0 369 tcg_gen_mov_tl(cpu_regs[reg], t0);
57fec1fe 370 break;
14ce26e7 371#endif
57fec1fe
FB
372 }
373}
2c0262af 374
57fec1fe
FB
375static inline void gen_op_mov_reg_T0(int ot, int reg)
376{
1e4840bf 377 gen_op_mov_reg_v(ot, reg, cpu_T[0]);
57fec1fe
FB
378}
379
380static inline void gen_op_mov_reg_T1(int ot, int reg)
381{
1e4840bf 382 gen_op_mov_reg_v(ot, reg, cpu_T[1]);
57fec1fe
FB
383}
384
385static inline void gen_op_mov_reg_A0(int size, int reg)
386{
387 switch(size) {
93ab25d7 388 case OT_BYTE:
c832e3de 389 tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], cpu_A0, 0, 16);
57fec1fe 390 break;
cc739bb0 391 default: /* XXX this shouldn't be reached; abort? */
93ab25d7 392 case OT_WORD:
cc739bb0
LD
393 /* For x86_64, this sets the higher half of register to zero.
394 For i386, this is equivalent to a mov. */
395 tcg_gen_ext32u_tl(cpu_regs[reg], cpu_A0);
57fec1fe 396 break;
cc739bb0 397#ifdef TARGET_X86_64
93ab25d7 398 case OT_LONG:
cc739bb0 399 tcg_gen_mov_tl(cpu_regs[reg], cpu_A0);
57fec1fe 400 break;
14ce26e7 401#endif
57fec1fe
FB
402 }
403}
404
1e4840bf 405static inline void gen_op_mov_v_reg(int ot, TCGv t0, int reg)
57fec1fe 406{
96d7073f
PM
407 if (ot == OT_BYTE && byte_reg_is_xH(reg)) {
408 tcg_gen_shri_tl(t0, cpu_regs[reg - 4], 8);
409 tcg_gen_ext8u_tl(t0, t0);
410 } else {
cc739bb0 411 tcg_gen_mov_tl(t0, cpu_regs[reg]);
57fec1fe
FB
412 }
413}
414
1e4840bf
FB
415static inline void gen_op_mov_TN_reg(int ot, int t_index, int reg)
416{
417 gen_op_mov_v_reg(ot, cpu_T[t_index], reg);
418}
419
57fec1fe
FB
420static inline void gen_op_movl_A0_reg(int reg)
421{
cc739bb0 422 tcg_gen_mov_tl(cpu_A0, cpu_regs[reg]);
57fec1fe
FB
423}
424
425static inline void gen_op_addl_A0_im(int32_t val)
426{
427 tcg_gen_addi_tl(cpu_A0, cpu_A0, val);
14ce26e7 428#ifdef TARGET_X86_64
57fec1fe 429 tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffffffff);
14ce26e7 430#endif
57fec1fe 431}
2c0262af 432
14ce26e7 433#ifdef TARGET_X86_64
57fec1fe
FB
434static inline void gen_op_addq_A0_im(int64_t val)
435{
436 tcg_gen_addi_tl(cpu_A0, cpu_A0, val);
437}
14ce26e7 438#endif
57fec1fe
FB
439
440static void gen_add_A0_im(DisasContext *s, int val)
441{
442#ifdef TARGET_X86_64
443 if (CODE64(s))
444 gen_op_addq_A0_im(val);
445 else
446#endif
447 gen_op_addl_A0_im(val);
448}
2c0262af 449
57fec1fe 450static inline void gen_op_addl_T0_T1(void)
2c0262af 451{
57fec1fe
FB
452 tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
453}
454
455static inline void gen_op_jmp_T0(void)
456{
317ac620 457 tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, eip));
57fec1fe
FB
458}
459
6e0d8677 460static inline void gen_op_add_reg_im(int size, int reg, int32_t val)
57fec1fe 461{
6e0d8677 462 switch(size) {
93ab25d7 463 case OT_BYTE:
cc739bb0 464 tcg_gen_addi_tl(cpu_tmp0, cpu_regs[reg], val);
c832e3de 465 tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], cpu_tmp0, 0, 16);
6e0d8677 466 break;
93ab25d7 467 case OT_WORD:
cc739bb0
LD
468 tcg_gen_addi_tl(cpu_tmp0, cpu_regs[reg], val);
469 /* For x86_64, this sets the higher half of register to zero.
470 For i386, this is equivalent to a nop. */
471 tcg_gen_ext32u_tl(cpu_tmp0, cpu_tmp0);
472 tcg_gen_mov_tl(cpu_regs[reg], cpu_tmp0);
6e0d8677
FB
473 break;
474#ifdef TARGET_X86_64
93ab25d7 475 case OT_LONG:
cc739bb0 476 tcg_gen_addi_tl(cpu_regs[reg], cpu_regs[reg], val);
6e0d8677
FB
477 break;
478#endif
479 }
57fec1fe
FB
480}
481
6e0d8677 482static inline void gen_op_add_reg_T0(int size, int reg)
57fec1fe 483{
6e0d8677 484 switch(size) {
93ab25d7 485 case OT_BYTE:
cc739bb0 486 tcg_gen_add_tl(cpu_tmp0, cpu_regs[reg], cpu_T[0]);
c832e3de 487 tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], cpu_tmp0, 0, 16);
6e0d8677 488 break;
93ab25d7 489 case OT_WORD:
cc739bb0
LD
490 tcg_gen_add_tl(cpu_tmp0, cpu_regs[reg], cpu_T[0]);
491 /* For x86_64, this sets the higher half of register to zero.
492 For i386, this is equivalent to a nop. */
493 tcg_gen_ext32u_tl(cpu_tmp0, cpu_tmp0);
494 tcg_gen_mov_tl(cpu_regs[reg], cpu_tmp0);
6e0d8677 495 break;
14ce26e7 496#ifdef TARGET_X86_64
93ab25d7 497 case OT_LONG:
cc739bb0 498 tcg_gen_add_tl(cpu_regs[reg], cpu_regs[reg], cpu_T[0]);
6e0d8677 499 break;
14ce26e7 500#endif
6e0d8677
FB
501 }
502}
57fec1fe 503
57fec1fe
FB
504static inline void gen_op_addl_A0_reg_sN(int shift, int reg)
505{
cc739bb0
LD
506 tcg_gen_mov_tl(cpu_tmp0, cpu_regs[reg]);
507 if (shift != 0)
57fec1fe
FB
508 tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, shift);
509 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
cc739bb0
LD
510 /* For x86_64, this sets the higher half of register to zero.
511 For i386, this is equivalent to a nop. */
512 tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
57fec1fe 513}
2c0262af 514
57fec1fe
FB
515static inline void gen_op_movl_A0_seg(int reg)
516{
317ac620 517 tcg_gen_ld32u_tl(cpu_A0, cpu_env, offsetof(CPUX86State, segs[reg].base) + REG_L_OFFSET);
57fec1fe 518}
2c0262af 519
7162ab21 520static inline void gen_op_addl_A0_seg(DisasContext *s, int reg)
57fec1fe 521{
317ac620 522 tcg_gen_ld_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, segs[reg].base));
57fec1fe 523#ifdef TARGET_X86_64
7162ab21
VC
524 if (CODE64(s)) {
525 tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffffffff);
526 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
527 } else {
528 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
529 tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffffffff);
530 }
531#else
532 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
57fec1fe
FB
533#endif
534}
2c0262af 535
14ce26e7 536#ifdef TARGET_X86_64
57fec1fe
FB
537static inline void gen_op_movq_A0_seg(int reg)
538{
317ac620 539 tcg_gen_ld_tl(cpu_A0, cpu_env, offsetof(CPUX86State, segs[reg].base));
57fec1fe 540}
14ce26e7 541
57fec1fe
FB
542static inline void gen_op_addq_A0_seg(int reg)
543{
317ac620 544 tcg_gen_ld_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, segs[reg].base));
57fec1fe
FB
545 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
546}
547
548static inline void gen_op_movq_A0_reg(int reg)
549{
cc739bb0 550 tcg_gen_mov_tl(cpu_A0, cpu_regs[reg]);
57fec1fe
FB
551}
552
553static inline void gen_op_addq_A0_reg_sN(int shift, int reg)
554{
cc739bb0
LD
555 tcg_gen_mov_tl(cpu_tmp0, cpu_regs[reg]);
556 if (shift != 0)
57fec1fe
FB
557 tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, shift);
558 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
559}
14ce26e7
FB
560#endif
561
57fec1fe
FB
562static inline void gen_op_lds_T0_A0(int idx)
563{
564 int mem_index = (idx >> 2) - 1;
565 switch(idx & 3) {
93ab25d7 566 case OT_BYTE:
57fec1fe
FB
567 tcg_gen_qemu_ld8s(cpu_T[0], cpu_A0, mem_index);
568 break;
93ab25d7 569 case OT_WORD:
57fec1fe
FB
570 tcg_gen_qemu_ld16s(cpu_T[0], cpu_A0, mem_index);
571 break;
572 default:
93ab25d7 573 case OT_LONG:
57fec1fe
FB
574 tcg_gen_qemu_ld32s(cpu_T[0], cpu_A0, mem_index);
575 break;
576 }
577}
2c0262af 578
1e4840bf 579static inline void gen_op_ld_v(int idx, TCGv t0, TCGv a0)
57fec1fe
FB
580{
581 int mem_index = (idx >> 2) - 1;
582 switch(idx & 3) {
93ab25d7 583 case OT_BYTE:
1e4840bf 584 tcg_gen_qemu_ld8u(t0, a0, mem_index);
57fec1fe 585 break;
93ab25d7 586 case OT_WORD:
1e4840bf 587 tcg_gen_qemu_ld16u(t0, a0, mem_index);
57fec1fe 588 break;
93ab25d7 589 case OT_LONG:
1e4840bf 590 tcg_gen_qemu_ld32u(t0, a0, mem_index);
57fec1fe
FB
591 break;
592 default:
93ab25d7 593 case OT_QUAD:
a7812ae4
PB
594 /* Should never happen on 32-bit targets. */
595#ifdef TARGET_X86_64
1e4840bf 596 tcg_gen_qemu_ld64(t0, a0, mem_index);
a7812ae4 597#endif
57fec1fe
FB
598 break;
599 }
600}
2c0262af 601
1e4840bf
FB
602/* XXX: always use ldu or lds */
603static inline void gen_op_ld_T0_A0(int idx)
604{
605 gen_op_ld_v(idx, cpu_T[0], cpu_A0);
606}
607
57fec1fe
FB
608static inline void gen_op_ldu_T0_A0(int idx)
609{
1e4840bf 610 gen_op_ld_v(idx, cpu_T[0], cpu_A0);
57fec1fe 611}
2c0262af 612
57fec1fe 613static inline void gen_op_ld_T1_A0(int idx)
1e4840bf
FB
614{
615 gen_op_ld_v(idx, cpu_T[1], cpu_A0);
616}
617
618static inline void gen_op_st_v(int idx, TCGv t0, TCGv a0)
57fec1fe
FB
619{
620 int mem_index = (idx >> 2) - 1;
621 switch(idx & 3) {
93ab25d7 622 case OT_BYTE:
1e4840bf 623 tcg_gen_qemu_st8(t0, a0, mem_index);
57fec1fe 624 break;
93ab25d7 625 case OT_WORD:
1e4840bf 626 tcg_gen_qemu_st16(t0, a0, mem_index);
57fec1fe 627 break;
93ab25d7 628 case OT_LONG:
1e4840bf 629 tcg_gen_qemu_st32(t0, a0, mem_index);
57fec1fe
FB
630 break;
631 default:
93ab25d7 632 case OT_QUAD:
a7812ae4
PB
633 /* Should never happen on 32-bit targets. */
634#ifdef TARGET_X86_64
1e4840bf 635 tcg_gen_qemu_st64(t0, a0, mem_index);
a7812ae4 636#endif
57fec1fe
FB
637 break;
638 }
639}
4f31916f 640
57fec1fe
FB
641static inline void gen_op_st_T0_A0(int idx)
642{
1e4840bf 643 gen_op_st_v(idx, cpu_T[0], cpu_A0);
57fec1fe 644}
4f31916f 645
57fec1fe
FB
646static inline void gen_op_st_T1_A0(int idx)
647{
1e4840bf 648 gen_op_st_v(idx, cpu_T[1], cpu_A0);
57fec1fe 649}
4f31916f 650
14ce26e7
FB
651static inline void gen_jmp_im(target_ulong pc)
652{
57fec1fe 653 tcg_gen_movi_tl(cpu_tmp0, pc);
317ac620 654 tcg_gen_st_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, eip));
14ce26e7
FB
655}
656
2c0262af
FB
657static inline void gen_string_movl_A0_ESI(DisasContext *s)
658{
659 int override;
660
661 override = s->override;
14ce26e7
FB
662#ifdef TARGET_X86_64
663 if (s->aflag == 2) {
664 if (override >= 0) {
57fec1fe
FB
665 gen_op_movq_A0_seg(override);
666 gen_op_addq_A0_reg_sN(0, R_ESI);
14ce26e7 667 } else {
57fec1fe 668 gen_op_movq_A0_reg(R_ESI);
14ce26e7
FB
669 }
670 } else
671#endif
2c0262af
FB
672 if (s->aflag) {
673 /* 32 bit address */
674 if (s->addseg && override < 0)
675 override = R_DS;
676 if (override >= 0) {
57fec1fe
FB
677 gen_op_movl_A0_seg(override);
678 gen_op_addl_A0_reg_sN(0, R_ESI);
2c0262af 679 } else {
57fec1fe 680 gen_op_movl_A0_reg(R_ESI);
2c0262af
FB
681 }
682 } else {
683 /* 16 address, always override */
684 if (override < 0)
685 override = R_DS;
57fec1fe 686 gen_op_movl_A0_reg(R_ESI);
2c0262af 687 gen_op_andl_A0_ffff();
7162ab21 688 gen_op_addl_A0_seg(s, override);
2c0262af
FB
689 }
690}
691
692static inline void gen_string_movl_A0_EDI(DisasContext *s)
693{
14ce26e7
FB
694#ifdef TARGET_X86_64
695 if (s->aflag == 2) {
57fec1fe 696 gen_op_movq_A0_reg(R_EDI);
14ce26e7
FB
697 } else
698#endif
2c0262af
FB
699 if (s->aflag) {
700 if (s->addseg) {
57fec1fe
FB
701 gen_op_movl_A0_seg(R_ES);
702 gen_op_addl_A0_reg_sN(0, R_EDI);
2c0262af 703 } else {
57fec1fe 704 gen_op_movl_A0_reg(R_EDI);
2c0262af
FB
705 }
706 } else {
57fec1fe 707 gen_op_movl_A0_reg(R_EDI);
2c0262af 708 gen_op_andl_A0_ffff();
7162ab21 709 gen_op_addl_A0_seg(s, R_ES);
2c0262af
FB
710 }
711}
712
6e0d8677
FB
713static inline void gen_op_movl_T0_Dshift(int ot)
714{
317ac620 715 tcg_gen_ld32s_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, df));
6e0d8677 716 tcg_gen_shli_tl(cpu_T[0], cpu_T[0], ot);
2c0262af
FB
717};
718
d824df34 719static TCGv gen_ext_tl(TCGv dst, TCGv src, int size, bool sign)
6e0d8677 720{
d824df34 721 switch (size) {
6e0d8677 722 case OT_BYTE:
d824df34
PB
723 if (sign) {
724 tcg_gen_ext8s_tl(dst, src);
725 } else {
726 tcg_gen_ext8u_tl(dst, src);
727 }
728 return dst;
6e0d8677 729 case OT_WORD:
d824df34
PB
730 if (sign) {
731 tcg_gen_ext16s_tl(dst, src);
732 } else {
733 tcg_gen_ext16u_tl(dst, src);
734 }
735 return dst;
736#ifdef TARGET_X86_64
6e0d8677 737 case OT_LONG:
d824df34
PB
738 if (sign) {
739 tcg_gen_ext32s_tl(dst, src);
740 } else {
741 tcg_gen_ext32u_tl(dst, src);
742 }
743 return dst;
744#endif
6e0d8677 745 default:
d824df34 746 return src;
6e0d8677
FB
747 }
748}
3b46e624 749
d824df34
PB
750static void gen_extu(int ot, TCGv reg)
751{
752 gen_ext_tl(reg, reg, ot, false);
753}
754
6e0d8677
FB
755static void gen_exts(int ot, TCGv reg)
756{
d824df34 757 gen_ext_tl(reg, reg, ot, true);
6e0d8677 758}
2c0262af 759
6e0d8677
FB
760static inline void gen_op_jnz_ecx(int size, int label1)
761{
cc739bb0 762 tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
6e0d8677 763 gen_extu(size + 1, cpu_tmp0);
cb63669a 764 tcg_gen_brcondi_tl(TCG_COND_NE, cpu_tmp0, 0, label1);
6e0d8677
FB
765}
766
767static inline void gen_op_jz_ecx(int size, int label1)
768{
cc739bb0 769 tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
6e0d8677 770 gen_extu(size + 1, cpu_tmp0);
cb63669a 771 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, label1);
6e0d8677 772}
2c0262af 773
a7812ae4
PB
774static void gen_helper_in_func(int ot, TCGv v, TCGv_i32 n)
775{
776 switch (ot) {
93ab25d7
PB
777 case OT_BYTE:
778 gen_helper_inb(v, n);
779 break;
780 case OT_WORD:
781 gen_helper_inw(v, n);
782 break;
783 case OT_LONG:
784 gen_helper_inl(v, n);
785 break;
a7812ae4 786 }
a7812ae4 787}
2c0262af 788
a7812ae4
PB
789static void gen_helper_out_func(int ot, TCGv_i32 v, TCGv_i32 n)
790{
791 switch (ot) {
93ab25d7
PB
792 case OT_BYTE:
793 gen_helper_outb(v, n);
794 break;
795 case OT_WORD:
796 gen_helper_outw(v, n);
797 break;
798 case OT_LONG:
799 gen_helper_outl(v, n);
800 break;
a7812ae4 801 }
a7812ae4 802}
f115e911 803
b8b6a50b
FB
804static void gen_check_io(DisasContext *s, int ot, target_ulong cur_eip,
805 uint32_t svm_flags)
f115e911 806{
b8b6a50b
FB
807 int state_saved;
808 target_ulong next_eip;
809
810 state_saved = 0;
f115e911 811 if (s->pe && (s->cpl > s->iopl || s->vm86)) {
773cdfcc 812 gen_update_cc_op(s);
14ce26e7 813 gen_jmp_im(cur_eip);
b8b6a50b 814 state_saved = 1;
b6abf97d 815 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
a7812ae4 816 switch (ot) {
93ab25d7 817 case OT_BYTE:
4a7443be
BS
818 gen_helper_check_iob(cpu_env, cpu_tmp2_i32);
819 break;
93ab25d7 820 case OT_WORD:
4a7443be
BS
821 gen_helper_check_iow(cpu_env, cpu_tmp2_i32);
822 break;
93ab25d7 823 case OT_LONG:
4a7443be
BS
824 gen_helper_check_iol(cpu_env, cpu_tmp2_i32);
825 break;
a7812ae4 826 }
b8b6a50b 827 }
872929aa 828 if(s->flags & HF_SVMI_MASK) {
b8b6a50b 829 if (!state_saved) {
773cdfcc 830 gen_update_cc_op(s);
b8b6a50b 831 gen_jmp_im(cur_eip);
b8b6a50b
FB
832 }
833 svm_flags |= (1 << (4 + ot));
834 next_eip = s->pc - s->cs_base;
b6abf97d 835 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
052e80d5
BS
836 gen_helper_svm_check_io(cpu_env, cpu_tmp2_i32,
837 tcg_const_i32(svm_flags),
a7812ae4 838 tcg_const_i32(next_eip - cur_eip));
f115e911
FB
839 }
840}
841
2c0262af
FB
842static inline void gen_movs(DisasContext *s, int ot)
843{
844 gen_string_movl_A0_ESI(s);
57fec1fe 845 gen_op_ld_T0_A0(ot + s->mem_index);
2c0262af 846 gen_string_movl_A0_EDI(s);
57fec1fe 847 gen_op_st_T0_A0(ot + s->mem_index);
6e0d8677
FB
848 gen_op_movl_T0_Dshift(ot);
849 gen_op_add_reg_T0(s->aflag, R_ESI);
850 gen_op_add_reg_T0(s->aflag, R_EDI);
2c0262af
FB
851}
852
b6abf97d
FB
853static void gen_op_update1_cc(void)
854{
b6abf97d
FB
855 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
856}
857
858static void gen_op_update2_cc(void)
859{
860 tcg_gen_mov_tl(cpu_cc_src, cpu_T[1]);
861 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
862}
863
864static inline void gen_op_cmpl_T0_T1_cc(void)
865{
866 tcg_gen_mov_tl(cpu_cc_src, cpu_T[1]);
867 tcg_gen_sub_tl(cpu_cc_dst, cpu_T[0], cpu_T[1]);
868}
869
870static inline void gen_op_testl_T0_T1_cc(void)
871{
b6abf97d
FB
872 tcg_gen_and_tl(cpu_cc_dst, cpu_T[0], cpu_T[1]);
873}
874
875static void gen_op_update_neg_cc(void)
876{
877 tcg_gen_neg_tl(cpu_cc_src, cpu_T[0]);
878 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
879}
880
d229edce
RH
881/* compute all eflags to cc_src */
882static void gen_compute_eflags(DisasContext *s)
8e1c85e3 883{
d229edce
RH
884 if (s->cc_op == CC_OP_EFLAGS) {
885 return;
886 }
773cdfcc 887 gen_update_cc_op(s);
f0967a1a 888 gen_helper_cc_compute_all(cpu_tmp2_i32, cpu_env, cpu_cc_op);
d229edce
RH
889 set_cc_op(s, CC_OP_EFLAGS);
890 tcg_gen_extu_i32_tl(cpu_cc_src, cpu_tmp2_i32);
8e1c85e3
FB
891}
892
bec93d72
RH
893typedef struct CCPrepare {
894 TCGCond cond;
895 TCGv reg;
896 TCGv reg2;
897 target_ulong imm;
898 target_ulong mask;
899 bool use_reg2;
900 bool no_setcond;
901} CCPrepare;
902
06847f1f 903/* compute eflags.C to reg */
bec93d72 904static CCPrepare gen_prepare_eflags_c(DisasContext *s, TCGv reg)
06847f1f
RH
905{
906 TCGv t0, t1;
bec93d72 907 int size, shift;
06847f1f
RH
908
909 switch (s->cc_op) {
910 case CC_OP_SUBB ... CC_OP_SUBQ:
911 /* (DATA_TYPE)(CC_DST + CC_SRC) < (DATA_TYPE)CC_SRC */
912 size = s->cc_op - CC_OP_SUBB;
913 t1 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, false);
914 /* If no temporary was used, be careful not to alias t1 and t0. */
915 t0 = TCGV_EQUAL(t1, cpu_cc_src) ? cpu_tmp0 : reg;
916 tcg_gen_add_tl(t0, cpu_cc_dst, cpu_cc_src);
917 gen_extu(size, t0);
918 goto add_sub;
919
920 case CC_OP_ADDB ... CC_OP_ADDQ:
921 /* (DATA_TYPE)CC_DST < (DATA_TYPE)CC_SRC */
922 size = s->cc_op - CC_OP_ADDB;
923 t1 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, false);
924 t0 = gen_ext_tl(reg, cpu_cc_dst, size, false);
925 add_sub:
bec93d72
RH
926 return (CCPrepare) { .cond = TCG_COND_LTU, .reg = t0,
927 .reg2 = t1, .mask = -1, .use_reg2 = true };
06847f1f
RH
928
929 case CC_OP_SBBB ... CC_OP_SBBQ:
930 /* (DATA_TYPE)(CC_DST + CC_SRC + 1) <= (DATA_TYPE)CC_SRC */
931 size = s->cc_op - CC_OP_SBBB;
932 t1 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, false);
933 if (TCGV_EQUAL(t1, reg) && TCGV_EQUAL(reg, cpu_cc_src)) {
934 tcg_gen_mov_tl(cpu_tmp0, cpu_cc_src);
935 t1 = cpu_tmp0;
936 }
937
938 tcg_gen_add_tl(reg, cpu_cc_dst, cpu_cc_src);
939 tcg_gen_addi_tl(reg, reg, 1);
940 gen_extu(size, reg);
941 t0 = reg;
942 goto adc_sbb;
943
944 case CC_OP_ADCB ... CC_OP_ADCQ:
945 /* (DATA_TYPE)CC_DST <= (DATA_TYPE)CC_SRC */
946 size = s->cc_op - CC_OP_ADCB;
947 t1 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, false);
948 t0 = gen_ext_tl(reg, cpu_cc_dst, size, false);
949 adc_sbb:
bec93d72
RH
950 return (CCPrepare) { .cond = TCG_COND_LEU, .reg = t0,
951 .reg2 = t1, .mask = -1, .use_reg2 = true };
06847f1f
RH
952
953 case CC_OP_LOGICB ... CC_OP_LOGICQ:
bec93d72 954 return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
06847f1f
RH
955
956 case CC_OP_INCB ... CC_OP_INCQ:
957 case CC_OP_DECB ... CC_OP_DECQ:
bec93d72
RH
958 return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
959 .mask = -1, .no_setcond = true };
06847f1f
RH
960
961 case CC_OP_SHLB ... CC_OP_SHLQ:
962 /* (CC_SRC >> (DATA_BITS - 1)) & 1 */
963 size = s->cc_op - CC_OP_SHLB;
bec93d72
RH
964 shift = (8 << size) - 1;
965 return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
966 .mask = (target_ulong)1 << shift };
06847f1f
RH
967
968 case CC_OP_MULB ... CC_OP_MULQ:
bec93d72
RH
969 return (CCPrepare) { .cond = TCG_COND_NE,
970 .reg = cpu_cc_src, .mask = -1 };
06847f1f
RH
971
972 case CC_OP_EFLAGS:
973 case CC_OP_SARB ... CC_OP_SARQ:
974 /* CC_SRC & 1 */
bec93d72
RH
975 return (CCPrepare) { .cond = TCG_COND_NE,
976 .reg = cpu_cc_src, .mask = CC_C };
06847f1f
RH
977
978 default:
979 /* The need to compute only C from CC_OP_DYNAMIC is important
980 in efficiently implementing e.g. INC at the start of a TB. */
981 gen_update_cc_op(s);
982 gen_helper_cc_compute_c(cpu_tmp2_i32, cpu_env, cpu_cc_op);
983 tcg_gen_extu_i32_tl(reg, cpu_tmp2_i32);
bec93d72
RH
984 return (CCPrepare) { .cond = TCG_COND_NE, .reg = reg,
985 .mask = -1, .no_setcond = true };
06847f1f
RH
986 }
987}
988
1608ecca 989/* compute eflags.P to reg */
bec93d72 990static CCPrepare gen_prepare_eflags_p(DisasContext *s, TCGv reg)
1608ecca 991{
d229edce 992 gen_compute_eflags(s);
bec93d72
RH
993 return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
994 .mask = CC_P };
1608ecca
PB
995}
996
997/* compute eflags.S to reg */
bec93d72 998static CCPrepare gen_prepare_eflags_s(DisasContext *s, TCGv reg)
1608ecca 999{
086c4077
RH
1000 switch (s->cc_op) {
1001 case CC_OP_DYNAMIC:
1002 gen_compute_eflags(s);
1003 /* FALLTHRU */
1004 case CC_OP_EFLAGS:
bec93d72
RH
1005 return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
1006 .mask = CC_S };
086c4077
RH
1007 default:
1008 {
1009 int size = (s->cc_op - CC_OP_ADDB) & 3;
1010 TCGv t0 = gen_ext_tl(reg, cpu_cc_dst, size, true);
bec93d72 1011 return (CCPrepare) { .cond = TCG_COND_LT, .reg = t0, .mask = -1 };
086c4077 1012 }
086c4077 1013 }
1608ecca
PB
1014}
1015
1016/* compute eflags.O to reg */
bec93d72 1017static CCPrepare gen_prepare_eflags_o(DisasContext *s, TCGv reg)
1608ecca 1018{
d229edce 1019 gen_compute_eflags(s);
bec93d72
RH
1020 return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
1021 .mask = CC_O };
1608ecca
PB
1022}
1023
1024/* compute eflags.Z to reg */
bec93d72 1025static CCPrepare gen_prepare_eflags_z(DisasContext *s, TCGv reg)
1608ecca 1026{
086c4077
RH
1027 switch (s->cc_op) {
1028 case CC_OP_DYNAMIC:
1029 gen_compute_eflags(s);
1030 /* FALLTHRU */
1031 case CC_OP_EFLAGS:
bec93d72
RH
1032 return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
1033 .mask = CC_Z };
086c4077
RH
1034 default:
1035 {
1036 int size = (s->cc_op - CC_OP_ADDB) & 3;
1037 TCGv t0 = gen_ext_tl(reg, cpu_cc_dst, size, false);
bec93d72 1038 return (CCPrepare) { .cond = TCG_COND_EQ, .reg = t0, .mask = -1 };
086c4077 1039 }
bec93d72
RH
1040 }
1041}
1042
1043#define gen_compute_eflags_c(s, reg, inv) \
1044 gen_do_setcc(reg, gen_prepare_eflags_c(s, reg), inv)
bec93d72
RH
1045
1046static void gen_do_setcc(TCGv reg, struct CCPrepare cc, bool inv)
1047{
1048 if (inv) {
1049 cc.cond = tcg_invert_cond(cc.cond);
1050 }
1051
1052 if (cc.no_setcond) {
1053 if (cc.cond == TCG_COND_EQ) {
1054 tcg_gen_xori_tl(reg, cc.reg, 1);
1055 } else {
1056 tcg_gen_mov_tl(reg, cc.reg);
1057 }
1058 return;
1059 }
1060
1061 if (cc.cond == TCG_COND_NE && !cc.use_reg2 && cc.imm == 0 &&
1062 cc.mask != 0 && (cc.mask & (cc.mask - 1)) == 0) {
1063 tcg_gen_shri_tl(reg, cc.reg, ctztl(cc.mask));
1064 tcg_gen_andi_tl(reg, reg, 1);
1065 return;
1066 }
1067 if (cc.mask != -1) {
1068 tcg_gen_andi_tl(reg, cc.reg, cc.mask);
276e6b5f 1069 cc.reg = reg;
bec93d72
RH
1070 }
1071 if (cc.use_reg2) {
1072 tcg_gen_setcond_tl(cc.cond, reg, cc.reg, cc.reg2);
1073 } else {
1074 tcg_gen_setcondi_tl(cc.cond, reg, cc.reg, cc.imm);
086c4077 1075 }
1608ecca
PB
1076}
1077
c365395e
PB
1078/* perform a conditional store into register 'reg' according to jump opcode
1079 value 'b'. In the fast case, T0 is guaranted not to be used. */
276e6b5f 1080static CCPrepare gen_prepare_cc(DisasContext *s, int b, TCGv reg)
8e1c85e3 1081{
c365395e 1082 int inv, jcc_op, size, cond;
276e6b5f 1083 CCPrepare cc;
c365395e
PB
1084 TCGv t0;
1085
1086 inv = b & 1;
8e1c85e3 1087 jcc_op = (b >> 1) & 7;
c365395e
PB
1088
1089 switch (s->cc_op) {
69d1aa31
RH
1090 case CC_OP_SUBB ... CC_OP_SUBQ:
1091 /* We optimize relational operators for the cmp/jcc case. */
c365395e
PB
1092 size = s->cc_op - CC_OP_SUBB;
1093 switch (jcc_op) {
1094 case JCC_BE:
c365395e
PB
1095 tcg_gen_add_tl(cpu_tmp4, cpu_cc_dst, cpu_cc_src);
1096 gen_extu(size, cpu_tmp4);
1097 t0 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, false);
276e6b5f
RH
1098 cc = (CCPrepare) { .cond = TCG_COND_LEU, .reg = cpu_tmp4,
1099 .reg2 = t0, .mask = -1, .use_reg2 = true };
c365395e 1100 break;
8e1c85e3 1101
c365395e 1102 case JCC_L:
276e6b5f 1103 cond = TCG_COND_LT;
c365395e
PB
1104 goto fast_jcc_l;
1105 case JCC_LE:
276e6b5f 1106 cond = TCG_COND_LE;
c365395e
PB
1107 fast_jcc_l:
1108 tcg_gen_add_tl(cpu_tmp4, cpu_cc_dst, cpu_cc_src);
1109 gen_exts(size, cpu_tmp4);
1110 t0 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, true);
276e6b5f
RH
1111 cc = (CCPrepare) { .cond = cond, .reg = cpu_tmp4,
1112 .reg2 = t0, .mask = -1, .use_reg2 = true };
c365395e 1113 break;
8e1c85e3 1114
c365395e 1115 default:
8e1c85e3 1116 goto slow_jcc;
c365395e 1117 }
8e1c85e3 1118 break;
c365395e 1119
8e1c85e3
FB
1120 default:
1121 slow_jcc:
69d1aa31
RH
1122 /* This actually generates good code for JC, JZ and JS. */
1123 switch (jcc_op) {
1124 case JCC_O:
1125 cc = gen_prepare_eflags_o(s, reg);
1126 break;
1127 case JCC_B:
1128 cc = gen_prepare_eflags_c(s, reg);
1129 break;
1130 case JCC_Z:
1131 cc = gen_prepare_eflags_z(s, reg);
1132 break;
1133 case JCC_BE:
1134 gen_compute_eflags(s);
1135 cc = (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
1136 .mask = CC_Z | CC_C };
1137 break;
1138 case JCC_S:
1139 cc = gen_prepare_eflags_s(s, reg);
1140 break;
1141 case JCC_P:
1142 cc = gen_prepare_eflags_p(s, reg);
1143 break;
1144 case JCC_L:
1145 gen_compute_eflags(s);
1146 if (TCGV_EQUAL(reg, cpu_cc_src)) {
1147 reg = cpu_tmp0;
1148 }
1149 tcg_gen_shri_tl(reg, cpu_cc_src, 4); /* CC_O -> CC_S */
1150 tcg_gen_xor_tl(reg, reg, cpu_cc_src);
1151 cc = (CCPrepare) { .cond = TCG_COND_NE, .reg = reg,
1152 .mask = CC_S };
1153 break;
1154 default:
1155 case JCC_LE:
1156 gen_compute_eflags(s);
1157 if (TCGV_EQUAL(reg, cpu_cc_src)) {
1158 reg = cpu_tmp0;
1159 }
1160 tcg_gen_shri_tl(reg, cpu_cc_src, 4); /* CC_O -> CC_S */
1161 tcg_gen_xor_tl(reg, reg, cpu_cc_src);
1162 cc = (CCPrepare) { .cond = TCG_COND_NE, .reg = reg,
1163 .mask = CC_S | CC_Z };
1164 break;
1165 }
c365395e 1166 break;
8e1c85e3 1167 }
276e6b5f
RH
1168
1169 if (inv) {
1170 cc.cond = tcg_invert_cond(cc.cond);
1171 }
1172 return cc;
8e1c85e3
FB
1173}
1174
276e6b5f
RH
1175#define gen_setcc1(s, b, reg) \
1176 gen_do_setcc(reg, gen_prepare_cc(s, b, reg), false)
1177
8e1c85e3
FB
1178/* generate a conditional jump to label 'l1' according to jump opcode
1179 value 'b'. In the fast case, T0 is guaranted not to be used. */
b27fc131 1180static inline void gen_jcc1(DisasContext *s, int b, int l1)
8e1c85e3 1181{
943131ca 1182 CCPrepare cc = gen_prepare_cc(s, b, cpu_T[0]);
8e1c85e3 1183
943131ca
PB
1184 if (cc.mask != -1) {
1185 tcg_gen_andi_tl(cpu_T[0], cc.reg, cc.mask);
1186 cc.reg = cpu_T[0];
1187 }
1188 if (cc.use_reg2) {
1189 tcg_gen_brcond_tl(cc.cond, cc.reg, cc.reg2, l1);
1190 } else {
1191 tcg_gen_brcondi_tl(cc.cond, cc.reg, cc.imm, l1);
8e1c85e3
FB
1192 }
1193}
1194
14ce26e7
FB
1195/* XXX: does not work with gdbstub "ice" single step - not a
1196 serious problem */
1197static int gen_jz_ecx_string(DisasContext *s, target_ulong next_eip)
2c0262af 1198{
14ce26e7
FB
1199 int l1, l2;
1200
1201 l1 = gen_new_label();
1202 l2 = gen_new_label();
6e0d8677 1203 gen_op_jnz_ecx(s->aflag, l1);
14ce26e7
FB
1204 gen_set_label(l2);
1205 gen_jmp_tb(s, next_eip, 1);
1206 gen_set_label(l1);
1207 return l2;
2c0262af
FB
1208}
1209
1210static inline void gen_stos(DisasContext *s, int ot)
1211{
57fec1fe 1212 gen_op_mov_TN_reg(OT_LONG, 0, R_EAX);
2c0262af 1213 gen_string_movl_A0_EDI(s);
57fec1fe 1214 gen_op_st_T0_A0(ot + s->mem_index);
6e0d8677
FB
1215 gen_op_movl_T0_Dshift(ot);
1216 gen_op_add_reg_T0(s->aflag, R_EDI);
2c0262af
FB
1217}
1218
1219static inline void gen_lods(DisasContext *s, int ot)
1220{
1221 gen_string_movl_A0_ESI(s);
57fec1fe
FB
1222 gen_op_ld_T0_A0(ot + s->mem_index);
1223 gen_op_mov_reg_T0(ot, R_EAX);
6e0d8677
FB
1224 gen_op_movl_T0_Dshift(ot);
1225 gen_op_add_reg_T0(s->aflag, R_ESI);
2c0262af
FB
1226}
1227
1228static inline void gen_scas(DisasContext *s, int ot)
1229{
57fec1fe 1230 gen_op_mov_TN_reg(OT_LONG, 0, R_EAX);
2c0262af 1231 gen_string_movl_A0_EDI(s);
57fec1fe 1232 gen_op_ld_T1_A0(ot + s->mem_index);
2c0262af 1233 gen_op_cmpl_T0_T1_cc();
6e0d8677
FB
1234 gen_op_movl_T0_Dshift(ot);
1235 gen_op_add_reg_T0(s->aflag, R_EDI);
3ca51d07 1236 set_cc_op(s, CC_OP_SUBB + ot);
2c0262af
FB
1237}
1238
1239static inline void gen_cmps(DisasContext *s, int ot)
1240{
1241 gen_string_movl_A0_ESI(s);
57fec1fe 1242 gen_op_ld_T0_A0(ot + s->mem_index);
2c0262af 1243 gen_string_movl_A0_EDI(s);
57fec1fe 1244 gen_op_ld_T1_A0(ot + s->mem_index);
2c0262af 1245 gen_op_cmpl_T0_T1_cc();
6e0d8677
FB
1246 gen_op_movl_T0_Dshift(ot);
1247 gen_op_add_reg_T0(s->aflag, R_ESI);
1248 gen_op_add_reg_T0(s->aflag, R_EDI);
3ca51d07 1249 set_cc_op(s, CC_OP_SUBB + ot);
2c0262af
FB
1250}
1251
1252static inline void gen_ins(DisasContext *s, int ot)
1253{
2e70f6ef
PB
1254 if (use_icount)
1255 gen_io_start();
2c0262af 1256 gen_string_movl_A0_EDI(s);
6e0d8677
FB
1257 /* Note: we must do this dummy write first to be restartable in
1258 case of page fault. */
9772c73b 1259 gen_op_movl_T0_0();
57fec1fe 1260 gen_op_st_T0_A0(ot + s->mem_index);
b8b6a50b 1261 gen_op_mov_TN_reg(OT_WORD, 1, R_EDX);
b6abf97d
FB
1262 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[1]);
1263 tcg_gen_andi_i32(cpu_tmp2_i32, cpu_tmp2_i32, 0xffff);
a7812ae4 1264 gen_helper_in_func(ot, cpu_T[0], cpu_tmp2_i32);
57fec1fe 1265 gen_op_st_T0_A0(ot + s->mem_index);
6e0d8677
FB
1266 gen_op_movl_T0_Dshift(ot);
1267 gen_op_add_reg_T0(s->aflag, R_EDI);
2e70f6ef
PB
1268 if (use_icount)
1269 gen_io_end();
2c0262af
FB
1270}
1271
1272static inline void gen_outs(DisasContext *s, int ot)
1273{
2e70f6ef
PB
1274 if (use_icount)
1275 gen_io_start();
2c0262af 1276 gen_string_movl_A0_ESI(s);
57fec1fe 1277 gen_op_ld_T0_A0(ot + s->mem_index);
b8b6a50b
FB
1278
1279 gen_op_mov_TN_reg(OT_WORD, 1, R_EDX);
b6abf97d
FB
1280 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[1]);
1281 tcg_gen_andi_i32(cpu_tmp2_i32, cpu_tmp2_i32, 0xffff);
1282 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[0]);
a7812ae4 1283 gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
b8b6a50b 1284
6e0d8677
FB
1285 gen_op_movl_T0_Dshift(ot);
1286 gen_op_add_reg_T0(s->aflag, R_ESI);
2e70f6ef
PB
1287 if (use_icount)
1288 gen_io_end();
2c0262af
FB
1289}
1290
1291/* same method as Valgrind : we generate jumps to current or next
1292 instruction */
1293#define GEN_REPZ(op) \
1294static inline void gen_repz_ ## op(DisasContext *s, int ot, \
14ce26e7 1295 target_ulong cur_eip, target_ulong next_eip) \
2c0262af 1296{ \
14ce26e7 1297 int l2;\
2c0262af 1298 gen_update_cc_op(s); \
14ce26e7 1299 l2 = gen_jz_ecx_string(s, next_eip); \
2c0262af 1300 gen_ ## op(s, ot); \
6e0d8677 1301 gen_op_add_reg_im(s->aflag, R_ECX, -1); \
2c0262af
FB
1302 /* a loop would cause two single step exceptions if ECX = 1 \
1303 before rep string_insn */ \
1304 if (!s->jmp_opt) \
6e0d8677 1305 gen_op_jz_ecx(s->aflag, l2); \
2c0262af
FB
1306 gen_jmp(s, cur_eip); \
1307}
1308
1309#define GEN_REPZ2(op) \
1310static inline void gen_repz_ ## op(DisasContext *s, int ot, \
14ce26e7
FB
1311 target_ulong cur_eip, \
1312 target_ulong next_eip, \
2c0262af
FB
1313 int nz) \
1314{ \
14ce26e7 1315 int l2;\
2c0262af 1316 gen_update_cc_op(s); \
14ce26e7 1317 l2 = gen_jz_ecx_string(s, next_eip); \
2c0262af 1318 gen_ ## op(s, ot); \
6e0d8677 1319 gen_op_add_reg_im(s->aflag, R_ECX, -1); \
773cdfcc 1320 gen_update_cc_op(s); \
b27fc131 1321 gen_jcc1(s, (JCC_Z << 1) | (nz ^ 1), l2); \
2c0262af 1322 if (!s->jmp_opt) \
6e0d8677 1323 gen_op_jz_ecx(s->aflag, l2); \
2c0262af 1324 gen_jmp(s, cur_eip); \
3ca51d07 1325 set_cc_op(s, CC_OP_DYNAMIC); \
2c0262af
FB
1326}
1327
1328GEN_REPZ(movs)
1329GEN_REPZ(stos)
1330GEN_REPZ(lods)
1331GEN_REPZ(ins)
1332GEN_REPZ(outs)
1333GEN_REPZ2(scas)
1334GEN_REPZ2(cmps)
1335
a7812ae4
PB
1336static void gen_helper_fp_arith_ST0_FT0(int op)
1337{
1338 switch (op) {
d3eb5eae
BS
1339 case 0:
1340 gen_helper_fadd_ST0_FT0(cpu_env);
1341 break;
1342 case 1:
1343 gen_helper_fmul_ST0_FT0(cpu_env);
1344 break;
1345 case 2:
1346 gen_helper_fcom_ST0_FT0(cpu_env);
1347 break;
1348 case 3:
1349 gen_helper_fcom_ST0_FT0(cpu_env);
1350 break;
1351 case 4:
1352 gen_helper_fsub_ST0_FT0(cpu_env);
1353 break;
1354 case 5:
1355 gen_helper_fsubr_ST0_FT0(cpu_env);
1356 break;
1357 case 6:
1358 gen_helper_fdiv_ST0_FT0(cpu_env);
1359 break;
1360 case 7:
1361 gen_helper_fdivr_ST0_FT0(cpu_env);
1362 break;
a7812ae4
PB
1363 }
1364}
2c0262af
FB
1365
1366/* NOTE the exception in "r" op ordering */
a7812ae4
PB
1367static void gen_helper_fp_arith_STN_ST0(int op, int opreg)
1368{
1369 TCGv_i32 tmp = tcg_const_i32(opreg);
1370 switch (op) {
d3eb5eae
BS
1371 case 0:
1372 gen_helper_fadd_STN_ST0(cpu_env, tmp);
1373 break;
1374 case 1:
1375 gen_helper_fmul_STN_ST0(cpu_env, tmp);
1376 break;
1377 case 4:
1378 gen_helper_fsubr_STN_ST0(cpu_env, tmp);
1379 break;
1380 case 5:
1381 gen_helper_fsub_STN_ST0(cpu_env, tmp);
1382 break;
1383 case 6:
1384 gen_helper_fdivr_STN_ST0(cpu_env, tmp);
1385 break;
1386 case 7:
1387 gen_helper_fdiv_STN_ST0(cpu_env, tmp);
1388 break;
a7812ae4
PB
1389 }
1390}
2c0262af
FB
1391
1392/* if d == OR_TMP0, it means memory operand (address in A0) */
1393static void gen_op(DisasContext *s1, int op, int ot, int d)
1394{
2c0262af 1395 if (d != OR_TMP0) {
57fec1fe 1396 gen_op_mov_TN_reg(ot, 0, d);
2c0262af 1397 } else {
57fec1fe 1398 gen_op_ld_T0_A0(ot + s1->mem_index);
2c0262af
FB
1399 }
1400 switch(op) {
1401 case OP_ADCL:
8115f117 1402 gen_compute_eflags_c(s1, cpu_tmp4, false);
cad3a37d
FB
1403 tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1404 tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_tmp4);
1405 if (d != OR_TMP0)
1406 gen_op_mov_reg_T0(ot, d);
1407 else
1408 gen_op_st_T0_A0(ot + s1->mem_index);
1409 tcg_gen_mov_tl(cpu_cc_src, cpu_T[1]);
1410 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
1411 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_tmp4);
1412 tcg_gen_shli_i32(cpu_tmp2_i32, cpu_tmp2_i32, 2);
1413 tcg_gen_addi_i32(cpu_cc_op, cpu_tmp2_i32, CC_OP_ADDB + ot);
3ca51d07 1414 set_cc_op(s1, CC_OP_DYNAMIC);
cad3a37d 1415 break;
2c0262af 1416 case OP_SBBL:
8115f117 1417 gen_compute_eflags_c(s1, cpu_tmp4, false);
cad3a37d
FB
1418 tcg_gen_sub_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1419 tcg_gen_sub_tl(cpu_T[0], cpu_T[0], cpu_tmp4);
1420 if (d != OR_TMP0)
57fec1fe 1421 gen_op_mov_reg_T0(ot, d);
cad3a37d
FB
1422 else
1423 gen_op_st_T0_A0(ot + s1->mem_index);
1424 tcg_gen_mov_tl(cpu_cc_src, cpu_T[1]);
1425 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
1426 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_tmp4);
1427 tcg_gen_shli_i32(cpu_tmp2_i32, cpu_tmp2_i32, 2);
1428 tcg_gen_addi_i32(cpu_cc_op, cpu_tmp2_i32, CC_OP_SUBB + ot);
3ca51d07 1429 set_cc_op(s1, CC_OP_DYNAMIC);
cad3a37d 1430 break;
2c0262af
FB
1431 case OP_ADDL:
1432 gen_op_addl_T0_T1();
cad3a37d
FB
1433 if (d != OR_TMP0)
1434 gen_op_mov_reg_T0(ot, d);
1435 else
1436 gen_op_st_T0_A0(ot + s1->mem_index);
1437 gen_op_update2_cc();
3ca51d07 1438 set_cc_op(s1, CC_OP_ADDB + ot);
2c0262af
FB
1439 break;
1440 case OP_SUBL:
57fec1fe 1441 tcg_gen_sub_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
cad3a37d
FB
1442 if (d != OR_TMP0)
1443 gen_op_mov_reg_T0(ot, d);
1444 else
1445 gen_op_st_T0_A0(ot + s1->mem_index);
1446 gen_op_update2_cc();
3ca51d07 1447 set_cc_op(s1, CC_OP_SUBB + ot);
2c0262af
FB
1448 break;
1449 default:
1450 case OP_ANDL:
57fec1fe 1451 tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
cad3a37d
FB
1452 if (d != OR_TMP0)
1453 gen_op_mov_reg_T0(ot, d);
1454 else
1455 gen_op_st_T0_A0(ot + s1->mem_index);
1456 gen_op_update1_cc();
3ca51d07 1457 set_cc_op(s1, CC_OP_LOGICB + ot);
57fec1fe 1458 break;
2c0262af 1459 case OP_ORL:
57fec1fe 1460 tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
cad3a37d
FB
1461 if (d != OR_TMP0)
1462 gen_op_mov_reg_T0(ot, d);
1463 else
1464 gen_op_st_T0_A0(ot + s1->mem_index);
1465 gen_op_update1_cc();
3ca51d07 1466 set_cc_op(s1, CC_OP_LOGICB + ot);
57fec1fe 1467 break;
2c0262af 1468 case OP_XORL:
57fec1fe 1469 tcg_gen_xor_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
cad3a37d
FB
1470 if (d != OR_TMP0)
1471 gen_op_mov_reg_T0(ot, d);
1472 else
1473 gen_op_st_T0_A0(ot + s1->mem_index);
1474 gen_op_update1_cc();
3ca51d07 1475 set_cc_op(s1, CC_OP_LOGICB + ot);
2c0262af
FB
1476 break;
1477 case OP_CMPL:
1478 gen_op_cmpl_T0_T1_cc();
3ca51d07 1479 set_cc_op(s1, CC_OP_SUBB + ot);
2c0262af
FB
1480 break;
1481 }
b6abf97d
FB
1482}
1483
2c0262af
FB
1484/* if d == OR_TMP0, it means memory operand (address in A0) */
1485static void gen_inc(DisasContext *s1, int ot, int d, int c)
1486{
1487 if (d != OR_TMP0)
57fec1fe 1488 gen_op_mov_TN_reg(ot, 0, d);
2c0262af 1489 else
57fec1fe 1490 gen_op_ld_T0_A0(ot + s1->mem_index);
8115f117 1491 gen_compute_eflags_c(s1, cpu_cc_src, false);
2c0262af 1492 if (c > 0) {
b6abf97d 1493 tcg_gen_addi_tl(cpu_T[0], cpu_T[0], 1);
3ca51d07 1494 set_cc_op(s1, CC_OP_INCB + ot);
2c0262af 1495 } else {
b6abf97d 1496 tcg_gen_addi_tl(cpu_T[0], cpu_T[0], -1);
3ca51d07 1497 set_cc_op(s1, CC_OP_DECB + ot);
2c0262af
FB
1498 }
1499 if (d != OR_TMP0)
57fec1fe 1500 gen_op_mov_reg_T0(ot, d);
2c0262af 1501 else
57fec1fe 1502 gen_op_st_T0_A0(ot + s1->mem_index);
cd31fefa 1503 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
2c0262af
FB
1504}
1505
b6abf97d
FB
1506static void gen_shift_rm_T1(DisasContext *s, int ot, int op1,
1507 int is_right, int is_arith)
2c0262af 1508{
b6abf97d
FB
1509 target_ulong mask;
1510 int shift_label;
82786041 1511 TCGv t0, t1, t2;
1e4840bf 1512
82786041 1513 if (ot == OT_QUAD) {
b6abf97d 1514 mask = 0x3f;
82786041 1515 } else {
b6abf97d 1516 mask = 0x1f;
82786041 1517 }
3b46e624 1518
b6abf97d 1519 /* load */
82786041 1520 if (op1 == OR_TMP0) {
b6abf97d 1521 gen_op_ld_T0_A0(ot + s->mem_index);
82786041 1522 } else {
b6abf97d 1523 gen_op_mov_TN_reg(ot, 0, op1);
82786041 1524 }
b6abf97d 1525
82786041
RH
1526 t0 = tcg_temp_local_new();
1527 t1 = tcg_temp_local_new();
1528 t2 = tcg_temp_local_new();
b6abf97d 1529
82786041 1530 tcg_gen_andi_tl(t2, cpu_T[1], mask);
b6abf97d
FB
1531
1532 if (is_right) {
1533 if (is_arith) {
f484d386 1534 gen_exts(ot, cpu_T[0]);
82786041
RH
1535 tcg_gen_mov_tl(t0, cpu_T[0]);
1536 tcg_gen_sar_tl(cpu_T[0], cpu_T[0], t2);
b6abf97d 1537 } else {
cad3a37d 1538 gen_extu(ot, cpu_T[0]);
82786041
RH
1539 tcg_gen_mov_tl(t0, cpu_T[0]);
1540 tcg_gen_shr_tl(cpu_T[0], cpu_T[0], t2);
b6abf97d
FB
1541 }
1542 } else {
82786041
RH
1543 tcg_gen_mov_tl(t0, cpu_T[0]);
1544 tcg_gen_shl_tl(cpu_T[0], cpu_T[0], t2);
b6abf97d
FB
1545 }
1546
1547 /* store */
82786041 1548 if (op1 == OR_TMP0) {
b6abf97d 1549 gen_op_st_T0_A0(ot + s->mem_index);
82786041 1550 } else {
b6abf97d 1551 gen_op_mov_reg_T0(ot, op1);
82786041
RH
1552 }
1553
773cdfcc
RH
1554 /* update eflags */
1555 gen_update_cc_op(s);
b6abf97d 1556
82786041 1557 tcg_gen_mov_tl(t1, cpu_T[0]);
1e4840bf 1558
b6abf97d 1559 shift_label = gen_new_label();
82786041
RH
1560 tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, shift_label);
1561
1562 tcg_gen_addi_tl(t2, t2, -1);
1563 tcg_gen_mov_tl(cpu_cc_dst, t1);
1564
1565 if (is_right) {
1566 if (is_arith) {
1567 tcg_gen_sar_tl(cpu_cc_src, t0, t2);
1568 } else {
1569 tcg_gen_shr_tl(cpu_cc_src, t0, t2);
1570 }
1571 } else {
1572 tcg_gen_shl_tl(cpu_cc_src, t0, t2);
1573 }
b6abf97d 1574
82786041 1575 if (is_right) {
b6abf97d 1576 tcg_gen_movi_i32(cpu_cc_op, CC_OP_SARB + ot);
82786041 1577 } else {
b6abf97d 1578 tcg_gen_movi_i32(cpu_cc_op, CC_OP_SHLB + ot);
82786041
RH
1579 }
1580
b6abf97d 1581 gen_set_label(shift_label);
3ca51d07 1582 set_cc_op(s, CC_OP_DYNAMIC); /* cannot predict flags after */
1e4840bf
FB
1583
1584 tcg_temp_free(t0);
1585 tcg_temp_free(t1);
82786041 1586 tcg_temp_free(t2);
b6abf97d
FB
1587}
1588
c1c37968
FB
1589static void gen_shift_rm_im(DisasContext *s, int ot, int op1, int op2,
1590 int is_right, int is_arith)
1591{
1592 int mask;
1593
1594 if (ot == OT_QUAD)
1595 mask = 0x3f;
1596 else
1597 mask = 0x1f;
1598
1599 /* load */
1600 if (op1 == OR_TMP0)
1601 gen_op_ld_T0_A0(ot + s->mem_index);
1602 else
1603 gen_op_mov_TN_reg(ot, 0, op1);
1604
1605 op2 &= mask;
1606 if (op2 != 0) {
1607 if (is_right) {
1608 if (is_arith) {
1609 gen_exts(ot, cpu_T[0]);
2a449d14 1610 tcg_gen_sari_tl(cpu_tmp4, cpu_T[0], op2 - 1);
c1c37968
FB
1611 tcg_gen_sari_tl(cpu_T[0], cpu_T[0], op2);
1612 } else {
1613 gen_extu(ot, cpu_T[0]);
2a449d14 1614 tcg_gen_shri_tl(cpu_tmp4, cpu_T[0], op2 - 1);
c1c37968
FB
1615 tcg_gen_shri_tl(cpu_T[0], cpu_T[0], op2);
1616 }
1617 } else {
2a449d14 1618 tcg_gen_shli_tl(cpu_tmp4, cpu_T[0], op2 - 1);
c1c37968
FB
1619 tcg_gen_shli_tl(cpu_T[0], cpu_T[0], op2);
1620 }
1621 }
1622
1623 /* store */
1624 if (op1 == OR_TMP0)
1625 gen_op_st_T0_A0(ot + s->mem_index);
1626 else
1627 gen_op_mov_reg_T0(ot, op1);
1628
1629 /* update eflags if non zero shift */
1630 if (op2 != 0) {
2a449d14 1631 tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
c1c37968 1632 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
3ca51d07 1633 set_cc_op(s, (is_right ? CC_OP_SARB : CC_OP_SHLB) + ot);
c1c37968
FB
1634 }
1635}
1636
b6abf97d
FB
1637static inline void tcg_gen_lshift(TCGv ret, TCGv arg1, target_long arg2)
1638{
1639 if (arg2 >= 0)
1640 tcg_gen_shli_tl(ret, arg1, arg2);
1641 else
1642 tcg_gen_shri_tl(ret, arg1, -arg2);
1643}
1644
b6abf97d
FB
1645static void gen_rot_rm_T1(DisasContext *s, int ot, int op1,
1646 int is_right)
1647{
1648 target_ulong mask;
1649 int label1, label2, data_bits;
1e4840bf
FB
1650 TCGv t0, t1, t2, a0;
1651
1652 /* XXX: inefficient, but we must use local temps */
a7812ae4
PB
1653 t0 = tcg_temp_local_new();
1654 t1 = tcg_temp_local_new();
1655 t2 = tcg_temp_local_new();
1656 a0 = tcg_temp_local_new();
1e4840bf 1657
b6abf97d
FB
1658 if (ot == OT_QUAD)
1659 mask = 0x3f;
1660 else
1661 mask = 0x1f;
1662
1663 /* load */
1e4840bf
FB
1664 if (op1 == OR_TMP0) {
1665 tcg_gen_mov_tl(a0, cpu_A0);
1666 gen_op_ld_v(ot + s->mem_index, t0, a0);
1667 } else {
1668 gen_op_mov_v_reg(ot, t0, op1);
1669 }
b6abf97d 1670
1e4840bf
FB
1671 tcg_gen_mov_tl(t1, cpu_T[1]);
1672
1673 tcg_gen_andi_tl(t1, t1, mask);
b6abf97d
FB
1674
1675 /* Must test zero case to avoid using undefined behaviour in TCG
1676 shifts. */
1677 label1 = gen_new_label();
1e4840bf 1678 tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, label1);
b6abf97d
FB
1679
1680 if (ot <= OT_WORD)
1e4840bf 1681 tcg_gen_andi_tl(cpu_tmp0, t1, (1 << (3 + ot)) - 1);
b6abf97d 1682 else
1e4840bf 1683 tcg_gen_mov_tl(cpu_tmp0, t1);
b6abf97d 1684
1e4840bf
FB
1685 gen_extu(ot, t0);
1686 tcg_gen_mov_tl(t2, t0);
b6abf97d
FB
1687
1688 data_bits = 8 << ot;
1689 /* XXX: rely on behaviour of shifts when operand 2 overflows (XXX:
1690 fix TCG definition) */
1691 if (is_right) {
1e4840bf 1692 tcg_gen_shr_tl(cpu_tmp4, t0, cpu_tmp0);
5b207c00 1693 tcg_gen_subfi_tl(cpu_tmp0, data_bits, cpu_tmp0);
1e4840bf 1694 tcg_gen_shl_tl(t0, t0, cpu_tmp0);
b6abf97d 1695 } else {
1e4840bf 1696 tcg_gen_shl_tl(cpu_tmp4, t0, cpu_tmp0);
5b207c00 1697 tcg_gen_subfi_tl(cpu_tmp0, data_bits, cpu_tmp0);
1e4840bf 1698 tcg_gen_shr_tl(t0, t0, cpu_tmp0);
b6abf97d 1699 }
1e4840bf 1700 tcg_gen_or_tl(t0, t0, cpu_tmp4);
b6abf97d
FB
1701
1702 gen_set_label(label1);
1703 /* store */
1e4840bf
FB
1704 if (op1 == OR_TMP0) {
1705 gen_op_st_v(ot + s->mem_index, t0, a0);
1706 } else {
1707 gen_op_mov_reg_v(ot, op1, t0);
1708 }
b6abf97d 1709
0ff6addd 1710 /* update eflags. It is needed anyway most of the time, do it always. */
d229edce 1711 gen_compute_eflags(s);
c7b3c873 1712 assert(s->cc_op == CC_OP_EFLAGS);
b6abf97d
FB
1713
1714 label2 = gen_new_label();
1e4840bf 1715 tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, label2);
b6abf97d 1716
b6abf97d 1717 tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~(CC_O | CC_C));
1e4840bf 1718 tcg_gen_xor_tl(cpu_tmp0, t2, t0);
b6abf97d
FB
1719 tcg_gen_lshift(cpu_tmp0, cpu_tmp0, 11 - (data_bits - 1));
1720 tcg_gen_andi_tl(cpu_tmp0, cpu_tmp0, CC_O);
1721 tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, cpu_tmp0);
1722 if (is_right) {
1e4840bf 1723 tcg_gen_shri_tl(t0, t0, data_bits - 1);
b6abf97d 1724 }
1e4840bf
FB
1725 tcg_gen_andi_tl(t0, t0, CC_C);
1726 tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, t0);
0ff6addd 1727
b6abf97d 1728 gen_set_label(label2);
1e4840bf
FB
1729
1730 tcg_temp_free(t0);
1731 tcg_temp_free(t1);
1732 tcg_temp_free(t2);
1733 tcg_temp_free(a0);
b6abf97d
FB
1734}
1735
8cd6345d 1736static void gen_rot_rm_im(DisasContext *s, int ot, int op1, int op2,
1737 int is_right)
1738{
1739 int mask;
1740 int data_bits;
1741 TCGv t0, t1, a0;
1742
1743 /* XXX: inefficient, but we must use local temps */
1744 t0 = tcg_temp_local_new();
1745 t1 = tcg_temp_local_new();
1746 a0 = tcg_temp_local_new();
1747
1748 if (ot == OT_QUAD)
1749 mask = 0x3f;
1750 else
1751 mask = 0x1f;
1752
1753 /* load */
1754 if (op1 == OR_TMP0) {
1755 tcg_gen_mov_tl(a0, cpu_A0);
1756 gen_op_ld_v(ot + s->mem_index, t0, a0);
1757 } else {
1758 gen_op_mov_v_reg(ot, t0, op1);
1759 }
1760
1761 gen_extu(ot, t0);
1762 tcg_gen_mov_tl(t1, t0);
1763
1764 op2 &= mask;
1765 data_bits = 8 << ot;
1766 if (op2 != 0) {
1767 int shift = op2 & ((1 << (3 + ot)) - 1);
1768 if (is_right) {
1769 tcg_gen_shri_tl(cpu_tmp4, t0, shift);
1770 tcg_gen_shli_tl(t0, t0, data_bits - shift);
1771 }
1772 else {
1773 tcg_gen_shli_tl(cpu_tmp4, t0, shift);
1774 tcg_gen_shri_tl(t0, t0, data_bits - shift);
1775 }
1776 tcg_gen_or_tl(t0, t0, cpu_tmp4);
1777 }
1778
1779 /* store */
1780 if (op1 == OR_TMP0) {
1781 gen_op_st_v(ot + s->mem_index, t0, a0);
1782 } else {
1783 gen_op_mov_reg_v(ot, op1, t0);
1784 }
1785
1786 if (op2 != 0) {
1787 /* update eflags */
d229edce 1788 gen_compute_eflags(s);
c7b3c873 1789 assert(s->cc_op == CC_OP_EFLAGS);
0ff6addd 1790
8cd6345d 1791 tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~(CC_O | CC_C));
1792 tcg_gen_xor_tl(cpu_tmp0, t1, t0);
1793 tcg_gen_lshift(cpu_tmp0, cpu_tmp0, 11 - (data_bits - 1));
1794 tcg_gen_andi_tl(cpu_tmp0, cpu_tmp0, CC_O);
1795 tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, cpu_tmp0);
1796 if (is_right) {
1797 tcg_gen_shri_tl(t0, t0, data_bits - 1);
1798 }
1799 tcg_gen_andi_tl(t0, t0, CC_C);
1800 tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, t0);
8cd6345d 1801 }
1802
1803 tcg_temp_free(t0);
1804 tcg_temp_free(t1);
1805 tcg_temp_free(a0);
1806}
1807
b6abf97d
FB
1808/* XXX: add faster immediate = 1 case */
1809static void gen_rotc_rm_T1(DisasContext *s, int ot, int op1,
1810 int is_right)
1811{
d229edce 1812 gen_compute_eflags(s);
c7b3c873 1813 assert(s->cc_op == CC_OP_EFLAGS);
b6abf97d
FB
1814
1815 /* load */
1816 if (op1 == OR_TMP0)
1817 gen_op_ld_T0_A0(ot + s->mem_index);
1818 else
1819 gen_op_mov_TN_reg(ot, 0, op1);
1820
a7812ae4
PB
1821 if (is_right) {
1822 switch (ot) {
93ab25d7 1823 case OT_BYTE:
7923057b
BS
1824 gen_helper_rcrb(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
1825 break;
93ab25d7 1826 case OT_WORD:
7923057b
BS
1827 gen_helper_rcrw(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
1828 break;
93ab25d7 1829 case OT_LONG:
7923057b
BS
1830 gen_helper_rcrl(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
1831 break;
a7812ae4 1832#ifdef TARGET_X86_64
93ab25d7 1833 case OT_QUAD:
7923057b
BS
1834 gen_helper_rcrq(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
1835 break;
a7812ae4
PB
1836#endif
1837 }
1838 } else {
1839 switch (ot) {
93ab25d7 1840 case OT_BYTE:
7923057b
BS
1841 gen_helper_rclb(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
1842 break;
93ab25d7 1843 case OT_WORD:
7923057b
BS
1844 gen_helper_rclw(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
1845 break;
93ab25d7 1846 case OT_LONG:
7923057b
BS
1847 gen_helper_rcll(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
1848 break;
a7812ae4 1849#ifdef TARGET_X86_64
93ab25d7 1850 case OT_QUAD:
7923057b
BS
1851 gen_helper_rclq(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
1852 break;
a7812ae4
PB
1853#endif
1854 }
1855 }
b6abf97d
FB
1856 /* store */
1857 if (op1 == OR_TMP0)
1858 gen_op_st_T0_A0(ot + s->mem_index);
1859 else
1860 gen_op_mov_reg_T0(ot, op1);
b6abf97d
FB
1861}
1862
1863/* XXX: add faster immediate case */
1864static void gen_shiftd_rm_T1_T3(DisasContext *s, int ot, int op1,
1865 int is_right)
1866{
1867 int label1, label2, data_bits;
1868 target_ulong mask;
1e4840bf
FB
1869 TCGv t0, t1, t2, a0;
1870
a7812ae4
PB
1871 t0 = tcg_temp_local_new();
1872 t1 = tcg_temp_local_new();
1873 t2 = tcg_temp_local_new();
1874 a0 = tcg_temp_local_new();
b6abf97d
FB
1875
1876 if (ot == OT_QUAD)
1877 mask = 0x3f;
1878 else
1879 mask = 0x1f;
1880
1881 /* load */
1e4840bf
FB
1882 if (op1 == OR_TMP0) {
1883 tcg_gen_mov_tl(a0, cpu_A0);
1884 gen_op_ld_v(ot + s->mem_index, t0, a0);
1885 } else {
1886 gen_op_mov_v_reg(ot, t0, op1);
1887 }
b6abf97d
FB
1888
1889 tcg_gen_andi_tl(cpu_T3, cpu_T3, mask);
1e4840bf
FB
1890
1891 tcg_gen_mov_tl(t1, cpu_T[1]);
1892 tcg_gen_mov_tl(t2, cpu_T3);
1893
b6abf97d
FB
1894 /* Must test zero case to avoid using undefined behaviour in TCG
1895 shifts. */
1896 label1 = gen_new_label();
1e4840bf 1897 tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, label1);
b6abf97d 1898
1e4840bf 1899 tcg_gen_addi_tl(cpu_tmp5, t2, -1);
b6abf97d
FB
1900 if (ot == OT_WORD) {
1901 /* Note: we implement the Intel behaviour for shift count > 16 */
1902 if (is_right) {
1e4840bf
FB
1903 tcg_gen_andi_tl(t0, t0, 0xffff);
1904 tcg_gen_shli_tl(cpu_tmp0, t1, 16);
1905 tcg_gen_or_tl(t0, t0, cpu_tmp0);
1906 tcg_gen_ext32u_tl(t0, t0);
b6abf97d 1907
1e4840bf 1908 tcg_gen_shr_tl(cpu_tmp4, t0, cpu_tmp5);
b6abf97d
FB
1909
1910 /* only needed if count > 16, but a test would complicate */
5b207c00 1911 tcg_gen_subfi_tl(cpu_tmp5, 32, t2);
1e4840bf 1912 tcg_gen_shl_tl(cpu_tmp0, t0, cpu_tmp5);
b6abf97d 1913
1e4840bf 1914 tcg_gen_shr_tl(t0, t0, t2);
b6abf97d 1915
1e4840bf 1916 tcg_gen_or_tl(t0, t0, cpu_tmp0);
b6abf97d
FB
1917 } else {
1918 /* XXX: not optimal */
1e4840bf
FB
1919 tcg_gen_andi_tl(t0, t0, 0xffff);
1920 tcg_gen_shli_tl(t1, t1, 16);
1921 tcg_gen_or_tl(t1, t1, t0);
1922 tcg_gen_ext32u_tl(t1, t1);
b6abf97d 1923
1e4840bf 1924 tcg_gen_shl_tl(cpu_tmp4, t0, cpu_tmp5);
5b207c00 1925 tcg_gen_subfi_tl(cpu_tmp0, 32, cpu_tmp5);
bedda79c
AJ
1926 tcg_gen_shr_tl(cpu_tmp5, t1, cpu_tmp0);
1927 tcg_gen_or_tl(cpu_tmp4, cpu_tmp4, cpu_tmp5);
b6abf97d 1928
1e4840bf 1929 tcg_gen_shl_tl(t0, t0, t2);
5b207c00 1930 tcg_gen_subfi_tl(cpu_tmp5, 32, t2);
1e4840bf
FB
1931 tcg_gen_shr_tl(t1, t1, cpu_tmp5);
1932 tcg_gen_or_tl(t0, t0, t1);
b6abf97d
FB
1933 }
1934 } else {
1935 data_bits = 8 << ot;
1936 if (is_right) {
1937 if (ot == OT_LONG)
1e4840bf 1938 tcg_gen_ext32u_tl(t0, t0);
b6abf97d 1939
1e4840bf 1940 tcg_gen_shr_tl(cpu_tmp4, t0, cpu_tmp5);
b6abf97d 1941
1e4840bf 1942 tcg_gen_shr_tl(t0, t0, t2);
5b207c00 1943 tcg_gen_subfi_tl(cpu_tmp5, data_bits, t2);
1e4840bf
FB
1944 tcg_gen_shl_tl(t1, t1, cpu_tmp5);
1945 tcg_gen_or_tl(t0, t0, t1);
b6abf97d
FB
1946
1947 } else {
1948 if (ot == OT_LONG)
1e4840bf 1949 tcg_gen_ext32u_tl(t1, t1);
b6abf97d 1950
1e4840bf 1951 tcg_gen_shl_tl(cpu_tmp4, t0, cpu_tmp5);
b6abf97d 1952
1e4840bf 1953 tcg_gen_shl_tl(t0, t0, t2);
5b207c00 1954 tcg_gen_subfi_tl(cpu_tmp5, data_bits, t2);
1e4840bf
FB
1955 tcg_gen_shr_tl(t1, t1, cpu_tmp5);
1956 tcg_gen_or_tl(t0, t0, t1);
b6abf97d
FB
1957 }
1958 }
1e4840bf 1959 tcg_gen_mov_tl(t1, cpu_tmp4);
b6abf97d
FB
1960
1961 gen_set_label(label1);
1962 /* store */
1e4840bf
FB
1963 if (op1 == OR_TMP0) {
1964 gen_op_st_v(ot + s->mem_index, t0, a0);
1965 } else {
1966 gen_op_mov_reg_v(ot, op1, t0);
1967 }
b6abf97d
FB
1968
1969 /* update eflags */
773cdfcc 1970 gen_update_cc_op(s);
b6abf97d
FB
1971
1972 label2 = gen_new_label();
1e4840bf 1973 tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, label2);
b6abf97d 1974
1e4840bf
FB
1975 tcg_gen_mov_tl(cpu_cc_src, t1);
1976 tcg_gen_mov_tl(cpu_cc_dst, t0);
b6abf97d
FB
1977 if (is_right) {
1978 tcg_gen_movi_i32(cpu_cc_op, CC_OP_SARB + ot);
1979 } else {
1980 tcg_gen_movi_i32(cpu_cc_op, CC_OP_SHLB + ot);
1981 }
1982 gen_set_label(label2);
3ca51d07 1983 set_cc_op(s, CC_OP_DYNAMIC); /* cannot predict flags after */
1e4840bf
FB
1984
1985 tcg_temp_free(t0);
1986 tcg_temp_free(t1);
1987 tcg_temp_free(t2);
1988 tcg_temp_free(a0);
b6abf97d
FB
1989}
1990
1991static void gen_shift(DisasContext *s1, int op, int ot, int d, int s)
1992{
1993 if (s != OR_TMP1)
1994 gen_op_mov_TN_reg(ot, 1, s);
1995 switch(op) {
1996 case OP_ROL:
1997 gen_rot_rm_T1(s1, ot, d, 0);
1998 break;
1999 case OP_ROR:
2000 gen_rot_rm_T1(s1, ot, d, 1);
2001 break;
2002 case OP_SHL:
2003 case OP_SHL1:
2004 gen_shift_rm_T1(s1, ot, d, 0, 0);
2005 break;
2006 case OP_SHR:
2007 gen_shift_rm_T1(s1, ot, d, 1, 0);
2008 break;
2009 case OP_SAR:
2010 gen_shift_rm_T1(s1, ot, d, 1, 1);
2011 break;
2012 case OP_RCL:
2013 gen_rotc_rm_T1(s1, ot, d, 0);
2014 break;
2015 case OP_RCR:
2016 gen_rotc_rm_T1(s1, ot, d, 1);
2017 break;
2018 }
2c0262af
FB
2019}
2020
2021static void gen_shifti(DisasContext *s1, int op, int ot, int d, int c)
2022{
c1c37968 2023 switch(op) {
8cd6345d 2024 case OP_ROL:
2025 gen_rot_rm_im(s1, ot, d, c, 0);
2026 break;
2027 case OP_ROR:
2028 gen_rot_rm_im(s1, ot, d, c, 1);
2029 break;
c1c37968
FB
2030 case OP_SHL:
2031 case OP_SHL1:
2032 gen_shift_rm_im(s1, ot, d, c, 0, 0);
2033 break;
2034 case OP_SHR:
2035 gen_shift_rm_im(s1, ot, d, c, 1, 0);
2036 break;
2037 case OP_SAR:
2038 gen_shift_rm_im(s1, ot, d, c, 1, 1);
2039 break;
2040 default:
2041 /* currently not optimized */
2042 gen_op_movl_T1_im(c);
2043 gen_shift(s1, op, ot, d, OR_TMP1);
2044 break;
2045 }
2c0262af
FB
2046}
2047
0af10c86
BS
2048static void gen_lea_modrm(CPUX86State *env, DisasContext *s, int modrm,
2049 int *reg_ptr, int *offset_ptr)
2c0262af 2050{
14ce26e7 2051 target_long disp;
2c0262af 2052 int havesib;
14ce26e7 2053 int base;
2c0262af
FB
2054 int index;
2055 int scale;
2056 int opreg;
2057 int mod, rm, code, override, must_add_seg;
2058
2059 override = s->override;
2060 must_add_seg = s->addseg;
2061 if (override >= 0)
2062 must_add_seg = 1;
2063 mod = (modrm >> 6) & 3;
2064 rm = modrm & 7;
2065
2066 if (s->aflag) {
2067
2068 havesib = 0;
2069 base = rm;
2070 index = 0;
2071 scale = 0;
3b46e624 2072
2c0262af
FB
2073 if (base == 4) {
2074 havesib = 1;
0af10c86 2075 code = cpu_ldub_code(env, s->pc++);
2c0262af 2076 scale = (code >> 6) & 3;
14ce26e7
FB
2077 index = ((code >> 3) & 7) | REX_X(s);
2078 base = (code & 7);
2c0262af 2079 }
14ce26e7 2080 base |= REX_B(s);
2c0262af
FB
2081
2082 switch (mod) {
2083 case 0:
14ce26e7 2084 if ((base & 7) == 5) {
2c0262af 2085 base = -1;
0af10c86 2086 disp = (int32_t)cpu_ldl_code(env, s->pc);
2c0262af 2087 s->pc += 4;
14ce26e7
FB
2088 if (CODE64(s) && !havesib) {
2089 disp += s->pc + s->rip_offset;
2090 }
2c0262af
FB
2091 } else {
2092 disp = 0;
2093 }
2094 break;
2095 case 1:
0af10c86 2096 disp = (int8_t)cpu_ldub_code(env, s->pc++);
2c0262af
FB
2097 break;
2098 default:
2099 case 2:
0af10c86 2100 disp = (int32_t)cpu_ldl_code(env, s->pc);
2c0262af
FB
2101 s->pc += 4;
2102 break;
2103 }
3b46e624 2104
2c0262af
FB
2105 if (base >= 0) {
2106 /* for correct popl handling with esp */
2107 if (base == 4 && s->popl_esp_hack)
2108 disp += s->popl_esp_hack;
14ce26e7
FB
2109#ifdef TARGET_X86_64
2110 if (s->aflag == 2) {
57fec1fe 2111 gen_op_movq_A0_reg(base);
14ce26e7 2112 if (disp != 0) {
57fec1fe 2113 gen_op_addq_A0_im(disp);
14ce26e7 2114 }
5fafdf24 2115 } else
14ce26e7
FB
2116#endif
2117 {
57fec1fe 2118 gen_op_movl_A0_reg(base);
14ce26e7
FB
2119 if (disp != 0)
2120 gen_op_addl_A0_im(disp);
2121 }
2c0262af 2122 } else {
14ce26e7
FB
2123#ifdef TARGET_X86_64
2124 if (s->aflag == 2) {
57fec1fe 2125 gen_op_movq_A0_im(disp);
5fafdf24 2126 } else
14ce26e7
FB
2127#endif
2128 {
2129 gen_op_movl_A0_im(disp);
2130 }
2c0262af 2131 }
b16f827b
AJ
2132 /* index == 4 means no index */
2133 if (havesib && (index != 4)) {
14ce26e7
FB
2134#ifdef TARGET_X86_64
2135 if (s->aflag == 2) {
57fec1fe 2136 gen_op_addq_A0_reg_sN(scale, index);
5fafdf24 2137 } else
14ce26e7
FB
2138#endif
2139 {
57fec1fe 2140 gen_op_addl_A0_reg_sN(scale, index);
14ce26e7 2141 }
2c0262af
FB
2142 }
2143 if (must_add_seg) {
2144 if (override < 0) {
2145 if (base == R_EBP || base == R_ESP)
2146 override = R_SS;
2147 else
2148 override = R_DS;
2149 }
14ce26e7
FB
2150#ifdef TARGET_X86_64
2151 if (s->aflag == 2) {
57fec1fe 2152 gen_op_addq_A0_seg(override);
5fafdf24 2153 } else
14ce26e7
FB
2154#endif
2155 {
7162ab21 2156 gen_op_addl_A0_seg(s, override);
14ce26e7 2157 }
2c0262af
FB
2158 }
2159 } else {
2160 switch (mod) {
2161 case 0:
2162 if (rm == 6) {
0af10c86 2163 disp = cpu_lduw_code(env, s->pc);
2c0262af
FB
2164 s->pc += 2;
2165 gen_op_movl_A0_im(disp);
2166 rm = 0; /* avoid SS override */
2167 goto no_rm;
2168 } else {
2169 disp = 0;
2170 }
2171 break;
2172 case 1:
0af10c86 2173 disp = (int8_t)cpu_ldub_code(env, s->pc++);
2c0262af
FB
2174 break;
2175 default:
2176 case 2:
0af10c86 2177 disp = cpu_lduw_code(env, s->pc);
2c0262af
FB
2178 s->pc += 2;
2179 break;
2180 }
2181 switch(rm) {
2182 case 0:
57fec1fe
FB
2183 gen_op_movl_A0_reg(R_EBX);
2184 gen_op_addl_A0_reg_sN(0, R_ESI);
2c0262af
FB
2185 break;
2186 case 1:
57fec1fe
FB
2187 gen_op_movl_A0_reg(R_EBX);
2188 gen_op_addl_A0_reg_sN(0, R_EDI);
2c0262af
FB
2189 break;
2190 case 2:
57fec1fe
FB
2191 gen_op_movl_A0_reg(R_EBP);
2192 gen_op_addl_A0_reg_sN(0, R_ESI);
2c0262af
FB
2193 break;
2194 case 3:
57fec1fe
FB
2195 gen_op_movl_A0_reg(R_EBP);
2196 gen_op_addl_A0_reg_sN(0, R_EDI);
2c0262af
FB
2197 break;
2198 case 4:
57fec1fe 2199 gen_op_movl_A0_reg(R_ESI);
2c0262af
FB
2200 break;
2201 case 5:
57fec1fe 2202 gen_op_movl_A0_reg(R_EDI);
2c0262af
FB
2203 break;
2204 case 6:
57fec1fe 2205 gen_op_movl_A0_reg(R_EBP);
2c0262af
FB
2206 break;
2207 default:
2208 case 7:
57fec1fe 2209 gen_op_movl_A0_reg(R_EBX);
2c0262af
FB
2210 break;
2211 }
2212 if (disp != 0)
2213 gen_op_addl_A0_im(disp);
2214 gen_op_andl_A0_ffff();
2215 no_rm:
2216 if (must_add_seg) {
2217 if (override < 0) {
2218 if (rm == 2 || rm == 3 || rm == 6)
2219 override = R_SS;
2220 else
2221 override = R_DS;
2222 }
7162ab21 2223 gen_op_addl_A0_seg(s, override);
2c0262af
FB
2224 }
2225 }
2226
2227 opreg = OR_A0;
2228 disp = 0;
2229 *reg_ptr = opreg;
2230 *offset_ptr = disp;
2231}
2232
0af10c86 2233static void gen_nop_modrm(CPUX86State *env, DisasContext *s, int modrm)
e17a36ce
FB
2234{
2235 int mod, rm, base, code;
2236
2237 mod = (modrm >> 6) & 3;
2238 if (mod == 3)
2239 return;
2240 rm = modrm & 7;
2241
2242 if (s->aflag) {
2243
2244 base = rm;
3b46e624 2245
e17a36ce 2246 if (base == 4) {
0af10c86 2247 code = cpu_ldub_code(env, s->pc++);
e17a36ce
FB
2248 base = (code & 7);
2249 }
3b46e624 2250
e17a36ce
FB
2251 switch (mod) {
2252 case 0:
2253 if (base == 5) {
2254 s->pc += 4;
2255 }
2256 break;
2257 case 1:
2258 s->pc++;
2259 break;
2260 default:
2261 case 2:
2262 s->pc += 4;
2263 break;
2264 }
2265 } else {
2266 switch (mod) {
2267 case 0:
2268 if (rm == 6) {
2269 s->pc += 2;
2270 }
2271 break;
2272 case 1:
2273 s->pc++;
2274 break;
2275 default:
2276 case 2:
2277 s->pc += 2;
2278 break;
2279 }
2280 }
2281}
2282
664e0f19
FB
2283/* used for LEA and MOV AX, mem */
2284static void gen_add_A0_ds_seg(DisasContext *s)
2285{
2286 int override, must_add_seg;
2287 must_add_seg = s->addseg;
2288 override = R_DS;
2289 if (s->override >= 0) {
2290 override = s->override;
2291 must_add_seg = 1;
664e0f19
FB
2292 }
2293 if (must_add_seg) {
8f091a59
FB
2294#ifdef TARGET_X86_64
2295 if (CODE64(s)) {
57fec1fe 2296 gen_op_addq_A0_seg(override);
5fafdf24 2297 } else
8f091a59
FB
2298#endif
2299 {
7162ab21 2300 gen_op_addl_A0_seg(s, override);
8f091a59 2301 }
664e0f19
FB
2302 }
2303}
2304
222a3336 2305/* generate modrm memory load or store of 'reg'. TMP0 is used if reg ==
2c0262af 2306 OR_TMP0 */
0af10c86
BS
2307static void gen_ldst_modrm(CPUX86State *env, DisasContext *s, int modrm,
2308 int ot, int reg, int is_store)
2c0262af
FB
2309{
2310 int mod, rm, opreg, disp;
2311
2312 mod = (modrm >> 6) & 3;
14ce26e7 2313 rm = (modrm & 7) | REX_B(s);
2c0262af
FB
2314 if (mod == 3) {
2315 if (is_store) {
2316 if (reg != OR_TMP0)
57fec1fe
FB
2317 gen_op_mov_TN_reg(ot, 0, reg);
2318 gen_op_mov_reg_T0(ot, rm);
2c0262af 2319 } else {
57fec1fe 2320 gen_op_mov_TN_reg(ot, 0, rm);
2c0262af 2321 if (reg != OR_TMP0)
57fec1fe 2322 gen_op_mov_reg_T0(ot, reg);
2c0262af
FB
2323 }
2324 } else {
0af10c86 2325 gen_lea_modrm(env, s, modrm, &opreg, &disp);
2c0262af
FB
2326 if (is_store) {
2327 if (reg != OR_TMP0)
57fec1fe
FB
2328 gen_op_mov_TN_reg(ot, 0, reg);
2329 gen_op_st_T0_A0(ot + s->mem_index);
2c0262af 2330 } else {
57fec1fe 2331 gen_op_ld_T0_A0(ot + s->mem_index);
2c0262af 2332 if (reg != OR_TMP0)
57fec1fe 2333 gen_op_mov_reg_T0(ot, reg);
2c0262af
FB
2334 }
2335 }
2336}
2337
0af10c86 2338static inline uint32_t insn_get(CPUX86State *env, DisasContext *s, int ot)
2c0262af
FB
2339{
2340 uint32_t ret;
2341
2342 switch(ot) {
2343 case OT_BYTE:
0af10c86 2344 ret = cpu_ldub_code(env, s->pc);
2c0262af
FB
2345 s->pc++;
2346 break;
2347 case OT_WORD:
0af10c86 2348 ret = cpu_lduw_code(env, s->pc);
2c0262af
FB
2349 s->pc += 2;
2350 break;
2351 default:
2352 case OT_LONG:
0af10c86 2353 ret = cpu_ldl_code(env, s->pc);
2c0262af
FB
2354 s->pc += 4;
2355 break;
2356 }
2357 return ret;
2358}
2359
14ce26e7
FB
2360static inline int insn_const_size(unsigned int ot)
2361{
2362 if (ot <= OT_LONG)
2363 return 1 << ot;
2364 else
2365 return 4;
2366}
2367
6e256c93
FB
2368static inline void gen_goto_tb(DisasContext *s, int tb_num, target_ulong eip)
2369{
2370 TranslationBlock *tb;
2371 target_ulong pc;
2372
2373 pc = s->cs_base + eip;
2374 tb = s->tb;
2375 /* NOTE: we handle the case where the TB spans two pages here */
2376 if ((pc & TARGET_PAGE_MASK) == (tb->pc & TARGET_PAGE_MASK) ||
2377 (pc & TARGET_PAGE_MASK) == ((s->pc - 1) & TARGET_PAGE_MASK)) {
2378 /* jump to same page: we can use a direct jump */
57fec1fe 2379 tcg_gen_goto_tb(tb_num);
6e256c93 2380 gen_jmp_im(eip);
4b4a72e5 2381 tcg_gen_exit_tb((tcg_target_long)tb + tb_num);
6e256c93
FB
2382 } else {
2383 /* jump to another page: currently not optimized */
2384 gen_jmp_im(eip);
2385 gen_eob(s);
2386 }
2387}
2388
5fafdf24 2389static inline void gen_jcc(DisasContext *s, int b,
14ce26e7 2390 target_ulong val, target_ulong next_eip)
2c0262af 2391{
b27fc131 2392 int l1, l2;
3b46e624 2393
2c0262af 2394 if (s->jmp_opt) {
ccfcdd09 2395 gen_update_cc_op(s);
14ce26e7 2396 l1 = gen_new_label();
b27fc131 2397 gen_jcc1(s, b, l1);
3ca51d07 2398 set_cc_op(s, CC_OP_DYNAMIC);
8e1c85e3 2399
6e256c93 2400 gen_goto_tb(s, 0, next_eip);
14ce26e7
FB
2401
2402 gen_set_label(l1);
6e256c93 2403 gen_goto_tb(s, 1, val);
5779406a 2404 s->is_jmp = DISAS_TB_JUMP;
2c0262af 2405 } else {
14ce26e7
FB
2406 l1 = gen_new_label();
2407 l2 = gen_new_label();
b27fc131 2408 gen_jcc1(s, b, l1);
8e1c85e3 2409
14ce26e7 2410 gen_jmp_im(next_eip);
8e1c85e3
FB
2411 tcg_gen_br(l2);
2412
14ce26e7
FB
2413 gen_set_label(l1);
2414 gen_jmp_im(val);
2415 gen_set_label(l2);
2c0262af
FB
2416 gen_eob(s);
2417 }
2418}
2419
2420static void gen_setcc(DisasContext *s, int b)
2421{
c365395e 2422 gen_setcc1(s, b, cpu_T[0]);
2c0262af
FB
2423}
2424
3bd7da9e
FB
2425static inline void gen_op_movl_T0_seg(int seg_reg)
2426{
2427 tcg_gen_ld32u_tl(cpu_T[0], cpu_env,
2428 offsetof(CPUX86State,segs[seg_reg].selector));
2429}
2430
2431static inline void gen_op_movl_seg_T0_vm(int seg_reg)
2432{
2433 tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xffff);
2434 tcg_gen_st32_tl(cpu_T[0], cpu_env,
2435 offsetof(CPUX86State,segs[seg_reg].selector));
2436 tcg_gen_shli_tl(cpu_T[0], cpu_T[0], 4);
2437 tcg_gen_st_tl(cpu_T[0], cpu_env,
2438 offsetof(CPUX86State,segs[seg_reg].base));
2439}
2440
2c0262af
FB
2441/* move T0 to seg_reg and compute if the CPU state may change. Never
2442 call this function with seg_reg == R_CS */
14ce26e7 2443static void gen_movl_seg_T0(DisasContext *s, int seg_reg, target_ulong cur_eip)
2c0262af 2444{
3415a4dd
FB
2445 if (s->pe && !s->vm86) {
2446 /* XXX: optimize by finding processor state dynamically */
773cdfcc 2447 gen_update_cc_op(s);
14ce26e7 2448 gen_jmp_im(cur_eip);
b6abf97d 2449 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
2999a0b2 2450 gen_helper_load_seg(cpu_env, tcg_const_i32(seg_reg), cpu_tmp2_i32);
dc196a57
FB
2451 /* abort translation because the addseg value may change or
2452 because ss32 may change. For R_SS, translation must always
2453 stop as a special handling must be done to disable hardware
2454 interrupts for the next instruction */
2455 if (seg_reg == R_SS || (s->code32 && seg_reg < R_FS))
5779406a 2456 s->is_jmp = DISAS_TB_JUMP;
3415a4dd 2457 } else {
3bd7da9e 2458 gen_op_movl_seg_T0_vm(seg_reg);
dc196a57 2459 if (seg_reg == R_SS)
5779406a 2460 s->is_jmp = DISAS_TB_JUMP;
3415a4dd 2461 }
2c0262af
FB
2462}
2463
0573fbfc
TS
2464static inline int svm_is_rep(int prefixes)
2465{
2466 return ((prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) ? 8 : 0);
2467}
2468
872929aa 2469static inline void
0573fbfc 2470gen_svm_check_intercept_param(DisasContext *s, target_ulong pc_start,
b8b6a50b 2471 uint32_t type, uint64_t param)
0573fbfc 2472{
872929aa
FB
2473 /* no SVM activated; fast case */
2474 if (likely(!(s->flags & HF_SVMI_MASK)))
2475 return;
773cdfcc 2476 gen_update_cc_op(s);
872929aa 2477 gen_jmp_im(pc_start - s->cs_base);
052e80d5 2478 gen_helper_svm_check_intercept_param(cpu_env, tcg_const_i32(type),
a7812ae4 2479 tcg_const_i64(param));
0573fbfc
TS
2480}
2481
872929aa 2482static inline void
0573fbfc
TS
2483gen_svm_check_intercept(DisasContext *s, target_ulong pc_start, uint64_t type)
2484{
872929aa 2485 gen_svm_check_intercept_param(s, pc_start, type, 0);
0573fbfc
TS
2486}
2487
4f31916f
FB
2488static inline void gen_stack_update(DisasContext *s, int addend)
2489{
14ce26e7
FB
2490#ifdef TARGET_X86_64
2491 if (CODE64(s)) {
6e0d8677 2492 gen_op_add_reg_im(2, R_ESP, addend);
14ce26e7
FB
2493 } else
2494#endif
4f31916f 2495 if (s->ss32) {
6e0d8677 2496 gen_op_add_reg_im(1, R_ESP, addend);
4f31916f 2497 } else {
6e0d8677 2498 gen_op_add_reg_im(0, R_ESP, addend);
4f31916f
FB
2499 }
2500}
2501
2c0262af
FB
2502/* generate a push. It depends on ss32, addseg and dflag */
2503static void gen_push_T0(DisasContext *s)
2504{
14ce26e7
FB
2505#ifdef TARGET_X86_64
2506 if (CODE64(s)) {
57fec1fe 2507 gen_op_movq_A0_reg(R_ESP);
8f091a59 2508 if (s->dflag) {
57fec1fe
FB
2509 gen_op_addq_A0_im(-8);
2510 gen_op_st_T0_A0(OT_QUAD + s->mem_index);
8f091a59 2511 } else {
57fec1fe
FB
2512 gen_op_addq_A0_im(-2);
2513 gen_op_st_T0_A0(OT_WORD + s->mem_index);
8f091a59 2514 }
57fec1fe 2515 gen_op_mov_reg_A0(2, R_ESP);
5fafdf24 2516 } else
14ce26e7
FB
2517#endif
2518 {
57fec1fe 2519 gen_op_movl_A0_reg(R_ESP);
14ce26e7 2520 if (!s->dflag)
57fec1fe 2521 gen_op_addl_A0_im(-2);
14ce26e7 2522 else
57fec1fe 2523 gen_op_addl_A0_im(-4);
14ce26e7
FB
2524 if (s->ss32) {
2525 if (s->addseg) {
bbf662ee 2526 tcg_gen_mov_tl(cpu_T[1], cpu_A0);
7162ab21 2527 gen_op_addl_A0_seg(s, R_SS);
14ce26e7
FB
2528 }
2529 } else {
2530 gen_op_andl_A0_ffff();
bbf662ee 2531 tcg_gen_mov_tl(cpu_T[1], cpu_A0);
7162ab21 2532 gen_op_addl_A0_seg(s, R_SS);
2c0262af 2533 }
57fec1fe 2534 gen_op_st_T0_A0(s->dflag + 1 + s->mem_index);
14ce26e7 2535 if (s->ss32 && !s->addseg)
57fec1fe 2536 gen_op_mov_reg_A0(1, R_ESP);
14ce26e7 2537 else
57fec1fe 2538 gen_op_mov_reg_T1(s->ss32 + 1, R_ESP);
2c0262af
FB
2539 }
2540}
2541
4f31916f
FB
2542/* generate a push. It depends on ss32, addseg and dflag */
2543/* slower version for T1, only used for call Ev */
2544static void gen_push_T1(DisasContext *s)
2c0262af 2545{
14ce26e7
FB
2546#ifdef TARGET_X86_64
2547 if (CODE64(s)) {
57fec1fe 2548 gen_op_movq_A0_reg(R_ESP);
8f091a59 2549 if (s->dflag) {
57fec1fe
FB
2550 gen_op_addq_A0_im(-8);
2551 gen_op_st_T1_A0(OT_QUAD + s->mem_index);
8f091a59 2552 } else {
57fec1fe
FB
2553 gen_op_addq_A0_im(-2);
2554 gen_op_st_T0_A0(OT_WORD + s->mem_index);
8f091a59 2555 }
57fec1fe 2556 gen_op_mov_reg_A0(2, R_ESP);
5fafdf24 2557 } else
14ce26e7
FB
2558#endif
2559 {
57fec1fe 2560 gen_op_movl_A0_reg(R_ESP);
14ce26e7 2561 if (!s->dflag)
57fec1fe 2562 gen_op_addl_A0_im(-2);
14ce26e7 2563 else
57fec1fe 2564 gen_op_addl_A0_im(-4);
14ce26e7
FB
2565 if (s->ss32) {
2566 if (s->addseg) {
7162ab21 2567 gen_op_addl_A0_seg(s, R_SS);
14ce26e7
FB
2568 }
2569 } else {
2570 gen_op_andl_A0_ffff();
7162ab21 2571 gen_op_addl_A0_seg(s, R_SS);
2c0262af 2572 }
57fec1fe 2573 gen_op_st_T1_A0(s->dflag + 1 + s->mem_index);
3b46e624 2574
14ce26e7 2575 if (s->ss32 && !s->addseg)
57fec1fe 2576 gen_op_mov_reg_A0(1, R_ESP);
14ce26e7
FB
2577 else
2578 gen_stack_update(s, (-2) << s->dflag);
2c0262af
FB
2579 }
2580}
2581
4f31916f
FB
2582/* two step pop is necessary for precise exceptions */
2583static void gen_pop_T0(DisasContext *s)
2c0262af 2584{
14ce26e7
FB
2585#ifdef TARGET_X86_64
2586 if (CODE64(s)) {
57fec1fe
FB
2587 gen_op_movq_A0_reg(R_ESP);
2588 gen_op_ld_T0_A0((s->dflag ? OT_QUAD : OT_WORD) + s->mem_index);
5fafdf24 2589 } else
14ce26e7
FB
2590#endif
2591 {
57fec1fe 2592 gen_op_movl_A0_reg(R_ESP);
14ce26e7
FB
2593 if (s->ss32) {
2594 if (s->addseg)
7162ab21 2595 gen_op_addl_A0_seg(s, R_SS);
14ce26e7
FB
2596 } else {
2597 gen_op_andl_A0_ffff();
7162ab21 2598 gen_op_addl_A0_seg(s, R_SS);
14ce26e7 2599 }
57fec1fe 2600 gen_op_ld_T0_A0(s->dflag + 1 + s->mem_index);
2c0262af
FB
2601 }
2602}
2603
2604static void gen_pop_update(DisasContext *s)
2605{
14ce26e7 2606#ifdef TARGET_X86_64
8f091a59 2607 if (CODE64(s) && s->dflag) {
14ce26e7
FB
2608 gen_stack_update(s, 8);
2609 } else
2610#endif
2611 {
2612 gen_stack_update(s, 2 << s->dflag);
2613 }
2c0262af
FB
2614}
2615
2616static void gen_stack_A0(DisasContext *s)
2617{
57fec1fe 2618 gen_op_movl_A0_reg(R_ESP);
2c0262af
FB
2619 if (!s->ss32)
2620 gen_op_andl_A0_ffff();
bbf662ee 2621 tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2c0262af 2622 if (s->addseg)
7162ab21 2623 gen_op_addl_A0_seg(s, R_SS);
2c0262af
FB
2624}
2625
2626/* NOTE: wrap around in 16 bit not fully handled */
2627static void gen_pusha(DisasContext *s)
2628{
2629 int i;
57fec1fe 2630 gen_op_movl_A0_reg(R_ESP);
2c0262af
FB
2631 gen_op_addl_A0_im(-16 << s->dflag);
2632 if (!s->ss32)
2633 gen_op_andl_A0_ffff();
bbf662ee 2634 tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2c0262af 2635 if (s->addseg)
7162ab21 2636 gen_op_addl_A0_seg(s, R_SS);
2c0262af 2637 for(i = 0;i < 8; i++) {
57fec1fe
FB
2638 gen_op_mov_TN_reg(OT_LONG, 0, 7 - i);
2639 gen_op_st_T0_A0(OT_WORD + s->dflag + s->mem_index);
2c0262af
FB
2640 gen_op_addl_A0_im(2 << s->dflag);
2641 }
57fec1fe 2642 gen_op_mov_reg_T1(OT_WORD + s->ss32, R_ESP);
2c0262af
FB
2643}
2644
2645/* NOTE: wrap around in 16 bit not fully handled */
2646static void gen_popa(DisasContext *s)
2647{
2648 int i;
57fec1fe 2649 gen_op_movl_A0_reg(R_ESP);
2c0262af
FB
2650 if (!s->ss32)
2651 gen_op_andl_A0_ffff();
bbf662ee
FB
2652 tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2653 tcg_gen_addi_tl(cpu_T[1], cpu_T[1], 16 << s->dflag);
2c0262af 2654 if (s->addseg)
7162ab21 2655 gen_op_addl_A0_seg(s, R_SS);
2c0262af
FB
2656 for(i = 0;i < 8; i++) {
2657 /* ESP is not reloaded */
2658 if (i != 3) {
57fec1fe
FB
2659 gen_op_ld_T0_A0(OT_WORD + s->dflag + s->mem_index);
2660 gen_op_mov_reg_T0(OT_WORD + s->dflag, 7 - i);
2c0262af
FB
2661 }
2662 gen_op_addl_A0_im(2 << s->dflag);
2663 }
57fec1fe 2664 gen_op_mov_reg_T1(OT_WORD + s->ss32, R_ESP);
2c0262af
FB
2665}
2666
2c0262af
FB
2667static void gen_enter(DisasContext *s, int esp_addend, int level)
2668{
61a8c4ec 2669 int ot, opsize;
2c0262af 2670
2c0262af 2671 level &= 0x1f;
8f091a59
FB
2672#ifdef TARGET_X86_64
2673 if (CODE64(s)) {
2674 ot = s->dflag ? OT_QUAD : OT_WORD;
2675 opsize = 1 << ot;
3b46e624 2676
57fec1fe 2677 gen_op_movl_A0_reg(R_ESP);
8f091a59 2678 gen_op_addq_A0_im(-opsize);
bbf662ee 2679 tcg_gen_mov_tl(cpu_T[1], cpu_A0);
8f091a59
FB
2680
2681 /* push bp */
57fec1fe
FB
2682 gen_op_mov_TN_reg(OT_LONG, 0, R_EBP);
2683 gen_op_st_T0_A0(ot + s->mem_index);
8f091a59 2684 if (level) {
b5b38f61 2685 /* XXX: must save state */
2999a0b2 2686 gen_helper_enter64_level(cpu_env, tcg_const_i32(level),
a7812ae4
PB
2687 tcg_const_i32((ot == OT_QUAD)),
2688 cpu_T[1]);
8f091a59 2689 }
57fec1fe 2690 gen_op_mov_reg_T1(ot, R_EBP);
bbf662ee 2691 tcg_gen_addi_tl(cpu_T[1], cpu_T[1], -esp_addend + (-opsize * level));
57fec1fe 2692 gen_op_mov_reg_T1(OT_QUAD, R_ESP);
5fafdf24 2693 } else
8f091a59
FB
2694#endif
2695 {
2696 ot = s->dflag + OT_WORD;
2697 opsize = 2 << s->dflag;
3b46e624 2698
57fec1fe 2699 gen_op_movl_A0_reg(R_ESP);
8f091a59
FB
2700 gen_op_addl_A0_im(-opsize);
2701 if (!s->ss32)
2702 gen_op_andl_A0_ffff();
bbf662ee 2703 tcg_gen_mov_tl(cpu_T[1], cpu_A0);
8f091a59 2704 if (s->addseg)
7162ab21 2705 gen_op_addl_A0_seg(s, R_SS);
8f091a59 2706 /* push bp */
57fec1fe
FB
2707 gen_op_mov_TN_reg(OT_LONG, 0, R_EBP);
2708 gen_op_st_T0_A0(ot + s->mem_index);
8f091a59 2709 if (level) {
b5b38f61 2710 /* XXX: must save state */
2999a0b2 2711 gen_helper_enter_level(cpu_env, tcg_const_i32(level),
a7812ae4
PB
2712 tcg_const_i32(s->dflag),
2713 cpu_T[1]);
8f091a59 2714 }
57fec1fe 2715 gen_op_mov_reg_T1(ot, R_EBP);
bbf662ee 2716 tcg_gen_addi_tl(cpu_T[1], cpu_T[1], -esp_addend + (-opsize * level));
57fec1fe 2717 gen_op_mov_reg_T1(OT_WORD + s->ss32, R_ESP);
2c0262af 2718 }
2c0262af
FB
2719}
2720
14ce26e7 2721static void gen_exception(DisasContext *s, int trapno, target_ulong cur_eip)
2c0262af 2722{
773cdfcc 2723 gen_update_cc_op(s);
14ce26e7 2724 gen_jmp_im(cur_eip);
77b2bc2c 2725 gen_helper_raise_exception(cpu_env, tcg_const_i32(trapno));
5779406a 2726 s->is_jmp = DISAS_TB_JUMP;
2c0262af
FB
2727}
2728
2729/* an interrupt is different from an exception because of the
7f75ffd3 2730 privilege checks */
5fafdf24 2731static void gen_interrupt(DisasContext *s, int intno,
14ce26e7 2732 target_ulong cur_eip, target_ulong next_eip)
2c0262af 2733{
773cdfcc 2734 gen_update_cc_op(s);
14ce26e7 2735 gen_jmp_im(cur_eip);
77b2bc2c 2736 gen_helper_raise_interrupt(cpu_env, tcg_const_i32(intno),
a7812ae4 2737 tcg_const_i32(next_eip - cur_eip));
5779406a 2738 s->is_jmp = DISAS_TB_JUMP;
2c0262af
FB
2739}
2740
14ce26e7 2741static void gen_debug(DisasContext *s, target_ulong cur_eip)
2c0262af 2742{
773cdfcc 2743 gen_update_cc_op(s);
14ce26e7 2744 gen_jmp_im(cur_eip);
4a7443be 2745 gen_helper_debug(cpu_env);
5779406a 2746 s->is_jmp = DISAS_TB_JUMP;
2c0262af
FB
2747}
2748
2749/* generate a generic end of block. Trace exception is also generated
2750 if needed */
2751static void gen_eob(DisasContext *s)
2752{
773cdfcc 2753 gen_update_cc_op(s);
a2cc3b24 2754 if (s->tb->flags & HF_INHIBIT_IRQ_MASK) {
f0967a1a 2755 gen_helper_reset_inhibit_irq(cpu_env);
a2cc3b24 2756 }
a2397807 2757 if (s->tb->flags & HF_RF_MASK) {
f0967a1a 2758 gen_helper_reset_rf(cpu_env);
a2397807 2759 }
34865134 2760 if (s->singlestep_enabled) {
4a7443be 2761 gen_helper_debug(cpu_env);
34865134 2762 } else if (s->tf) {
4a7443be 2763 gen_helper_single_step(cpu_env);
2c0262af 2764 } else {
57fec1fe 2765 tcg_gen_exit_tb(0);
2c0262af 2766 }
5779406a 2767 s->is_jmp = DISAS_TB_JUMP;
2c0262af
FB
2768}
2769
2770/* generate a jump to eip. No segment change must happen before as a
2771 direct call to the next block may occur */
14ce26e7 2772static void gen_jmp_tb(DisasContext *s, target_ulong eip, int tb_num)
2c0262af 2773{
2c0262af 2774 if (s->jmp_opt) {
728d803b 2775 gen_update_cc_op(s);
6e256c93 2776 gen_goto_tb(s, tb_num, eip);
5779406a 2777 s->is_jmp = DISAS_TB_JUMP;
2c0262af 2778 } else {
14ce26e7 2779 gen_jmp_im(eip);
2c0262af
FB
2780 gen_eob(s);
2781 }
2782}
2783
14ce26e7
FB
2784static void gen_jmp(DisasContext *s, target_ulong eip)
2785{
2786 gen_jmp_tb(s, eip, 0);
2787}
2788
8686c490
FB
2789static inline void gen_ldq_env_A0(int idx, int offset)
2790{
2791 int mem_index = (idx >> 2) - 1;
b6abf97d
FB
2792 tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_A0, mem_index);
2793 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset);
8686c490 2794}
664e0f19 2795
8686c490
FB
2796static inline void gen_stq_env_A0(int idx, int offset)
2797{
2798 int mem_index = (idx >> 2) - 1;
b6abf97d
FB
2799 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset);
2800 tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_A0, mem_index);
8686c490 2801}
664e0f19 2802
8686c490
FB
2803static inline void gen_ldo_env_A0(int idx, int offset)
2804{
2805 int mem_index = (idx >> 2) - 1;
b6abf97d
FB
2806 tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_A0, mem_index);
2807 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(0)));
8686c490 2808 tcg_gen_addi_tl(cpu_tmp0, cpu_A0, 8);
b6abf97d
FB
2809 tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_tmp0, mem_index);
2810 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(1)));
8686c490 2811}
14ce26e7 2812
8686c490
FB
2813static inline void gen_sto_env_A0(int idx, int offset)
2814{
2815 int mem_index = (idx >> 2) - 1;
b6abf97d
FB
2816 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(0)));
2817 tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_A0, mem_index);
8686c490 2818 tcg_gen_addi_tl(cpu_tmp0, cpu_A0, 8);
b6abf97d
FB
2819 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(1)));
2820 tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_tmp0, mem_index);
8686c490 2821}
14ce26e7 2822
5af45186
FB
2823static inline void gen_op_movo(int d_offset, int s_offset)
2824{
b6abf97d
FB
2825 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset);
2826 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
2827 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset + 8);
2828 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset + 8);
5af45186
FB
2829}
2830
2831static inline void gen_op_movq(int d_offset, int s_offset)
2832{
b6abf97d
FB
2833 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset);
2834 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
5af45186
FB
2835}
2836
2837static inline void gen_op_movl(int d_offset, int s_offset)
2838{
b6abf97d
FB
2839 tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env, s_offset);
2840 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, d_offset);
5af45186
FB
2841}
2842
2843static inline void gen_op_movq_env_0(int d_offset)
2844{
b6abf97d
FB
2845 tcg_gen_movi_i64(cpu_tmp1_i64, 0);
2846 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
5af45186 2847}
664e0f19 2848
d3eb5eae
BS
2849typedef void (*SSEFunc_i_ep)(TCGv_i32 val, TCGv_ptr env, TCGv_ptr reg);
2850typedef void (*SSEFunc_l_ep)(TCGv_i64 val, TCGv_ptr env, TCGv_ptr reg);
2851typedef void (*SSEFunc_0_epi)(TCGv_ptr env, TCGv_ptr reg, TCGv_i32 val);
2852typedef void (*SSEFunc_0_epl)(TCGv_ptr env, TCGv_ptr reg, TCGv_i64 val);
2853typedef void (*SSEFunc_0_epp)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b);
2854typedef void (*SSEFunc_0_eppi)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
2855 TCGv_i32 val);
c4baa050 2856typedef void (*SSEFunc_0_ppi)(TCGv_ptr reg_a, TCGv_ptr reg_b, TCGv_i32 val);
d3eb5eae
BS
2857typedef void (*SSEFunc_0_eppt)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
2858 TCGv val);
c4baa050 2859
5af45186
FB
2860#define SSE_SPECIAL ((void *)1)
2861#define SSE_DUMMY ((void *)2)
664e0f19 2862
a7812ae4
PB
2863#define MMX_OP2(x) { gen_helper_ ## x ## _mmx, gen_helper_ ## x ## _xmm }
2864#define SSE_FOP(x) { gen_helper_ ## x ## ps, gen_helper_ ## x ## pd, \
2865 gen_helper_ ## x ## ss, gen_helper_ ## x ## sd, }
5af45186 2866
d3eb5eae 2867static const SSEFunc_0_epp sse_op_table1[256][4] = {
a35f3ec7
AJ
2868 /* 3DNow! extensions */
2869 [0x0e] = { SSE_DUMMY }, /* femms */
2870 [0x0f] = { SSE_DUMMY }, /* pf... */
664e0f19
FB
2871 /* pure SSE operations */
2872 [0x10] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movups, movupd, movss, movsd */
2873 [0x11] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movups, movupd, movss, movsd */
465e9838 2874 [0x12] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movlps, movlpd, movsldup, movddup */
664e0f19 2875 [0x13] = { SSE_SPECIAL, SSE_SPECIAL }, /* movlps, movlpd */
a7812ae4
PB
2876 [0x14] = { gen_helper_punpckldq_xmm, gen_helper_punpcklqdq_xmm },
2877 [0x15] = { gen_helper_punpckhdq_xmm, gen_helper_punpckhqdq_xmm },
664e0f19
FB
2878 [0x16] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movhps, movhpd, movshdup */
2879 [0x17] = { SSE_SPECIAL, SSE_SPECIAL }, /* movhps, movhpd */
2880
2881 [0x28] = { SSE_SPECIAL, SSE_SPECIAL }, /* movaps, movapd */
2882 [0x29] = { SSE_SPECIAL, SSE_SPECIAL }, /* movaps, movapd */
2883 [0x2a] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* cvtpi2ps, cvtpi2pd, cvtsi2ss, cvtsi2sd */
d9f4bb27 2884 [0x2b] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movntps, movntpd, movntss, movntsd */
664e0f19
FB
2885 [0x2c] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* cvttps2pi, cvttpd2pi, cvttsd2si, cvttss2si */
2886 [0x2d] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* cvtps2pi, cvtpd2pi, cvtsd2si, cvtss2si */
a7812ae4
PB
2887 [0x2e] = { gen_helper_ucomiss, gen_helper_ucomisd },
2888 [0x2f] = { gen_helper_comiss, gen_helper_comisd },
664e0f19
FB
2889 [0x50] = { SSE_SPECIAL, SSE_SPECIAL }, /* movmskps, movmskpd */
2890 [0x51] = SSE_FOP(sqrt),
a7812ae4
PB
2891 [0x52] = { gen_helper_rsqrtps, NULL, gen_helper_rsqrtss, NULL },
2892 [0x53] = { gen_helper_rcpps, NULL, gen_helper_rcpss, NULL },
2893 [0x54] = { gen_helper_pand_xmm, gen_helper_pand_xmm }, /* andps, andpd */
2894 [0x55] = { gen_helper_pandn_xmm, gen_helper_pandn_xmm }, /* andnps, andnpd */
2895 [0x56] = { gen_helper_por_xmm, gen_helper_por_xmm }, /* orps, orpd */
2896 [0x57] = { gen_helper_pxor_xmm, gen_helper_pxor_xmm }, /* xorps, xorpd */
664e0f19
FB
2897 [0x58] = SSE_FOP(add),
2898 [0x59] = SSE_FOP(mul),
a7812ae4
PB
2899 [0x5a] = { gen_helper_cvtps2pd, gen_helper_cvtpd2ps,
2900 gen_helper_cvtss2sd, gen_helper_cvtsd2ss },
2901 [0x5b] = { gen_helper_cvtdq2ps, gen_helper_cvtps2dq, gen_helper_cvttps2dq },
664e0f19
FB
2902 [0x5c] = SSE_FOP(sub),
2903 [0x5d] = SSE_FOP(min),
2904 [0x5e] = SSE_FOP(div),
2905 [0x5f] = SSE_FOP(max),
2906
2907 [0xc2] = SSE_FOP(cmpeq),
d3eb5eae
BS
2908 [0xc6] = { (SSEFunc_0_epp)gen_helper_shufps,
2909 (SSEFunc_0_epp)gen_helper_shufpd }, /* XXX: casts */
664e0f19 2910
222a3336
AZ
2911 [0x38] = { SSE_SPECIAL, SSE_SPECIAL, NULL, SSE_SPECIAL }, /* SSSE3/SSE4 */
2912 [0x3a] = { SSE_SPECIAL, SSE_SPECIAL }, /* SSSE3/SSE4 */
4242b1bd 2913
664e0f19
FB
2914 /* MMX ops and their SSE extensions */
2915 [0x60] = MMX_OP2(punpcklbw),
2916 [0x61] = MMX_OP2(punpcklwd),
2917 [0x62] = MMX_OP2(punpckldq),
2918 [0x63] = MMX_OP2(packsswb),
2919 [0x64] = MMX_OP2(pcmpgtb),
2920 [0x65] = MMX_OP2(pcmpgtw),
2921 [0x66] = MMX_OP2(pcmpgtl),
2922 [0x67] = MMX_OP2(packuswb),
2923 [0x68] = MMX_OP2(punpckhbw),
2924 [0x69] = MMX_OP2(punpckhwd),
2925 [0x6a] = MMX_OP2(punpckhdq),
2926 [0x6b] = MMX_OP2(packssdw),
a7812ae4
PB
2927 [0x6c] = { NULL, gen_helper_punpcklqdq_xmm },
2928 [0x6d] = { NULL, gen_helper_punpckhqdq_xmm },
664e0f19
FB
2929 [0x6e] = { SSE_SPECIAL, SSE_SPECIAL }, /* movd mm, ea */
2930 [0x6f] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movq, movdqa, , movqdu */
d3eb5eae
BS
2931 [0x70] = { (SSEFunc_0_epp)gen_helper_pshufw_mmx,
2932 (SSEFunc_0_epp)gen_helper_pshufd_xmm,
2933 (SSEFunc_0_epp)gen_helper_pshufhw_xmm,
2934 (SSEFunc_0_epp)gen_helper_pshuflw_xmm }, /* XXX: casts */
664e0f19
FB
2935 [0x71] = { SSE_SPECIAL, SSE_SPECIAL }, /* shiftw */
2936 [0x72] = { SSE_SPECIAL, SSE_SPECIAL }, /* shiftd */
2937 [0x73] = { SSE_SPECIAL, SSE_SPECIAL }, /* shiftq */
2938 [0x74] = MMX_OP2(pcmpeqb),
2939 [0x75] = MMX_OP2(pcmpeqw),
2940 [0x76] = MMX_OP2(pcmpeql),
a35f3ec7 2941 [0x77] = { SSE_DUMMY }, /* emms */
d9f4bb27
AP
2942 [0x78] = { NULL, SSE_SPECIAL, NULL, SSE_SPECIAL }, /* extrq_i, insertq_i */
2943 [0x79] = { NULL, gen_helper_extrq_r, NULL, gen_helper_insertq_r },
a7812ae4
PB
2944 [0x7c] = { NULL, gen_helper_haddpd, NULL, gen_helper_haddps },
2945 [0x7d] = { NULL, gen_helper_hsubpd, NULL, gen_helper_hsubps },
664e0f19
FB
2946 [0x7e] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movd, movd, , movq */
2947 [0x7f] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movq, movdqa, movdqu */
2948 [0xc4] = { SSE_SPECIAL, SSE_SPECIAL }, /* pinsrw */
2949 [0xc5] = { SSE_SPECIAL, SSE_SPECIAL }, /* pextrw */
a7812ae4 2950 [0xd0] = { NULL, gen_helper_addsubpd, NULL, gen_helper_addsubps },
664e0f19
FB
2951 [0xd1] = MMX_OP2(psrlw),
2952 [0xd2] = MMX_OP2(psrld),
2953 [0xd3] = MMX_OP2(psrlq),
2954 [0xd4] = MMX_OP2(paddq),
2955 [0xd5] = MMX_OP2(pmullw),
2956 [0xd6] = { NULL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL },
2957 [0xd7] = { SSE_SPECIAL, SSE_SPECIAL }, /* pmovmskb */
2958 [0xd8] = MMX_OP2(psubusb),
2959 [0xd9] = MMX_OP2(psubusw),
2960 [0xda] = MMX_OP2(pminub),
2961 [0xdb] = MMX_OP2(pand),
2962 [0xdc] = MMX_OP2(paddusb),
2963 [0xdd] = MMX_OP2(paddusw),
2964 [0xde] = MMX_OP2(pmaxub),
2965 [0xdf] = MMX_OP2(pandn),
2966 [0xe0] = MMX_OP2(pavgb),
2967 [0xe1] = MMX_OP2(psraw),
2968 [0xe2] = MMX_OP2(psrad),
2969 [0xe3] = MMX_OP2(pavgw),
2970 [0xe4] = MMX_OP2(pmulhuw),
2971 [0xe5] = MMX_OP2(pmulhw),
a7812ae4 2972 [0xe6] = { NULL, gen_helper_cvttpd2dq, gen_helper_cvtdq2pd, gen_helper_cvtpd2dq },
664e0f19
FB
2973 [0xe7] = { SSE_SPECIAL , SSE_SPECIAL }, /* movntq, movntq */
2974 [0xe8] = MMX_OP2(psubsb),
2975 [0xe9] = MMX_OP2(psubsw),
2976 [0xea] = MMX_OP2(pminsw),
2977 [0xeb] = MMX_OP2(por),
2978 [0xec] = MMX_OP2(paddsb),
2979 [0xed] = MMX_OP2(paddsw),
2980 [0xee] = MMX_OP2(pmaxsw),
2981 [0xef] = MMX_OP2(pxor),
465e9838 2982 [0xf0] = { NULL, NULL, NULL, SSE_SPECIAL }, /* lddqu */
664e0f19
FB
2983 [0xf1] = MMX_OP2(psllw),
2984 [0xf2] = MMX_OP2(pslld),
2985 [0xf3] = MMX_OP2(psllq),
2986 [0xf4] = MMX_OP2(pmuludq),
2987 [0xf5] = MMX_OP2(pmaddwd),
2988 [0xf6] = MMX_OP2(psadbw),
d3eb5eae
BS
2989 [0xf7] = { (SSEFunc_0_epp)gen_helper_maskmov_mmx,
2990 (SSEFunc_0_epp)gen_helper_maskmov_xmm }, /* XXX: casts */
664e0f19
FB
2991 [0xf8] = MMX_OP2(psubb),
2992 [0xf9] = MMX_OP2(psubw),
2993 [0xfa] = MMX_OP2(psubl),
2994 [0xfb] = MMX_OP2(psubq),
2995 [0xfc] = MMX_OP2(paddb),
2996 [0xfd] = MMX_OP2(paddw),
2997 [0xfe] = MMX_OP2(paddl),
2998};
2999
d3eb5eae 3000static const SSEFunc_0_epp sse_op_table2[3 * 8][2] = {
664e0f19
FB
3001 [0 + 2] = MMX_OP2(psrlw),
3002 [0 + 4] = MMX_OP2(psraw),
3003 [0 + 6] = MMX_OP2(psllw),
3004 [8 + 2] = MMX_OP2(psrld),
3005 [8 + 4] = MMX_OP2(psrad),
3006 [8 + 6] = MMX_OP2(pslld),
3007 [16 + 2] = MMX_OP2(psrlq),
a7812ae4 3008 [16 + 3] = { NULL, gen_helper_psrldq_xmm },
664e0f19 3009 [16 + 6] = MMX_OP2(psllq),
a7812ae4 3010 [16 + 7] = { NULL, gen_helper_pslldq_xmm },
664e0f19
FB
3011};
3012
d3eb5eae 3013static const SSEFunc_0_epi sse_op_table3ai[] = {
a7812ae4 3014 gen_helper_cvtsi2ss,
11f8cdbc 3015 gen_helper_cvtsi2sd
c4baa050 3016};
a7812ae4 3017
11f8cdbc 3018#ifdef TARGET_X86_64
d3eb5eae 3019static const SSEFunc_0_epl sse_op_table3aq[] = {
11f8cdbc
SW
3020 gen_helper_cvtsq2ss,
3021 gen_helper_cvtsq2sd
3022};
3023#endif
3024
d3eb5eae 3025static const SSEFunc_i_ep sse_op_table3bi[] = {
a7812ae4 3026 gen_helper_cvttss2si,
a7812ae4 3027 gen_helper_cvtss2si,
bedc2ac1 3028 gen_helper_cvttsd2si,
11f8cdbc 3029 gen_helper_cvtsd2si
664e0f19 3030};
3b46e624 3031
11f8cdbc 3032#ifdef TARGET_X86_64
d3eb5eae 3033static const SSEFunc_l_ep sse_op_table3bq[] = {
11f8cdbc 3034 gen_helper_cvttss2sq,
11f8cdbc 3035 gen_helper_cvtss2sq,
bedc2ac1 3036 gen_helper_cvttsd2sq,
11f8cdbc
SW
3037 gen_helper_cvtsd2sq
3038};
3039#endif
3040
d3eb5eae 3041static const SSEFunc_0_epp sse_op_table4[8][4] = {
664e0f19
FB
3042 SSE_FOP(cmpeq),
3043 SSE_FOP(cmplt),
3044 SSE_FOP(cmple),
3045 SSE_FOP(cmpunord),
3046 SSE_FOP(cmpneq),
3047 SSE_FOP(cmpnlt),
3048 SSE_FOP(cmpnle),
3049 SSE_FOP(cmpord),
3050};
3b46e624 3051
d3eb5eae 3052static const SSEFunc_0_epp sse_op_table5[256] = {
a7812ae4
PB
3053 [0x0c] = gen_helper_pi2fw,
3054 [0x0d] = gen_helper_pi2fd,
3055 [0x1c] = gen_helper_pf2iw,
3056 [0x1d] = gen_helper_pf2id,
3057 [0x8a] = gen_helper_pfnacc,
3058 [0x8e] = gen_helper_pfpnacc,
3059 [0x90] = gen_helper_pfcmpge,
3060 [0x94] = gen_helper_pfmin,
3061 [0x96] = gen_helper_pfrcp,
3062 [0x97] = gen_helper_pfrsqrt,
3063 [0x9a] = gen_helper_pfsub,
3064 [0x9e] = gen_helper_pfadd,
3065 [0xa0] = gen_helper_pfcmpgt,
3066 [0xa4] = gen_helper_pfmax,
3067 [0xa6] = gen_helper_movq, /* pfrcpit1; no need to actually increase precision */
3068 [0xa7] = gen_helper_movq, /* pfrsqit1 */
3069 [0xaa] = gen_helper_pfsubr,
3070 [0xae] = gen_helper_pfacc,
3071 [0xb0] = gen_helper_pfcmpeq,
3072 [0xb4] = gen_helper_pfmul,
3073 [0xb6] = gen_helper_movq, /* pfrcpit2 */
3074 [0xb7] = gen_helper_pmulhrw_mmx,
3075 [0xbb] = gen_helper_pswapd,
3076 [0xbf] = gen_helper_pavgb_mmx /* pavgusb */
a35f3ec7
AJ
3077};
3078
d3eb5eae
BS
3079struct SSEOpHelper_epp {
3080 SSEFunc_0_epp op[2];
c4baa050
BS
3081 uint32_t ext_mask;
3082};
3083
d3eb5eae
BS
3084struct SSEOpHelper_eppi {
3085 SSEFunc_0_eppi op[2];
c4baa050 3086 uint32_t ext_mask;
222a3336 3087};
c4baa050 3088
222a3336 3089#define SSSE3_OP(x) { MMX_OP2(x), CPUID_EXT_SSSE3 }
a7812ae4
PB
3090#define SSE41_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_SSE41 }
3091#define SSE42_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_SSE42 }
222a3336 3092#define SSE41_SPECIAL { { NULL, SSE_SPECIAL }, CPUID_EXT_SSE41 }
c4baa050 3093
d3eb5eae 3094static const struct SSEOpHelper_epp sse_op_table6[256] = {
222a3336
AZ
3095 [0x00] = SSSE3_OP(pshufb),
3096 [0x01] = SSSE3_OP(phaddw),
3097 [0x02] = SSSE3_OP(phaddd),
3098 [0x03] = SSSE3_OP(phaddsw),
3099 [0x04] = SSSE3_OP(pmaddubsw),
3100 [0x05] = SSSE3_OP(phsubw),
3101 [0x06] = SSSE3_OP(phsubd),
3102 [0x07] = SSSE3_OP(phsubsw),
3103 [0x08] = SSSE3_OP(psignb),
3104 [0x09] = SSSE3_OP(psignw),
3105 [0x0a] = SSSE3_OP(psignd),
3106 [0x0b] = SSSE3_OP(pmulhrsw),
3107 [0x10] = SSE41_OP(pblendvb),
3108 [0x14] = SSE41_OP(blendvps),
3109 [0x15] = SSE41_OP(blendvpd),
3110 [0x17] = SSE41_OP(ptest),
3111 [0x1c] = SSSE3_OP(pabsb),
3112 [0x1d] = SSSE3_OP(pabsw),
3113 [0x1e] = SSSE3_OP(pabsd),
3114 [0x20] = SSE41_OP(pmovsxbw),
3115 [0x21] = SSE41_OP(pmovsxbd),
3116 [0x22] = SSE41_OP(pmovsxbq),
3117 [0x23] = SSE41_OP(pmovsxwd),
3118 [0x24] = SSE41_OP(pmovsxwq),
3119 [0x25] = SSE41_OP(pmovsxdq),
3120 [0x28] = SSE41_OP(pmuldq),
3121 [0x29] = SSE41_OP(pcmpeqq),
3122 [0x2a] = SSE41_SPECIAL, /* movntqda */
3123 [0x2b] = SSE41_OP(packusdw),
3124 [0x30] = SSE41_OP(pmovzxbw),
3125 [0x31] = SSE41_OP(pmovzxbd),
3126 [0x32] = SSE41_OP(pmovzxbq),
3127 [0x33] = SSE41_OP(pmovzxwd),
3128 [0x34] = SSE41_OP(pmovzxwq),
3129 [0x35] = SSE41_OP(pmovzxdq),
3130 [0x37] = SSE42_OP(pcmpgtq),
3131 [0x38] = SSE41_OP(pminsb),
3132 [0x39] = SSE41_OP(pminsd),
3133 [0x3a] = SSE41_OP(pminuw),
3134 [0x3b] = SSE41_OP(pminud),
3135 [0x3c] = SSE41_OP(pmaxsb),
3136 [0x3d] = SSE41_OP(pmaxsd),
3137 [0x3e] = SSE41_OP(pmaxuw),
3138 [0x3f] = SSE41_OP(pmaxud),
3139 [0x40] = SSE41_OP(pmulld),
3140 [0x41] = SSE41_OP(phminposuw),
4242b1bd
AZ
3141};
3142
d3eb5eae 3143static const struct SSEOpHelper_eppi sse_op_table7[256] = {
222a3336
AZ
3144 [0x08] = SSE41_OP(roundps),
3145 [0x09] = SSE41_OP(roundpd),
3146 [0x0a] = SSE41_OP(roundss),
3147 [0x0b] = SSE41_OP(roundsd),
3148 [0x0c] = SSE41_OP(blendps),
3149 [0x0d] = SSE41_OP(blendpd),
3150 [0x0e] = SSE41_OP(pblendw),
3151 [0x0f] = SSSE3_OP(palignr),
3152 [0x14] = SSE41_SPECIAL, /* pextrb */
3153 [0x15] = SSE41_SPECIAL, /* pextrw */
3154 [0x16] = SSE41_SPECIAL, /* pextrd/pextrq */
3155 [0x17] = SSE41_SPECIAL, /* extractps */
3156 [0x20] = SSE41_SPECIAL, /* pinsrb */
3157 [0x21] = SSE41_SPECIAL, /* insertps */
3158 [0x22] = SSE41_SPECIAL, /* pinsrd/pinsrq */
3159 [0x40] = SSE41_OP(dpps),
3160 [0x41] = SSE41_OP(dppd),
3161 [0x42] = SSE41_OP(mpsadbw),
3162 [0x60] = SSE42_OP(pcmpestrm),
3163 [0x61] = SSE42_OP(pcmpestri),
3164 [0x62] = SSE42_OP(pcmpistrm),
3165 [0x63] = SSE42_OP(pcmpistri),
4242b1bd
AZ
3166};
3167
0af10c86
BS
3168static void gen_sse(CPUX86State *env, DisasContext *s, int b,
3169 target_ulong pc_start, int rex_r)
664e0f19
FB
3170{
3171 int b1, op1_offset, op2_offset, is_xmm, val, ot;
3172 int modrm, mod, rm, reg, reg_addr, offset_addr;
d3eb5eae
BS
3173 SSEFunc_0_epp sse_fn_epp;
3174 SSEFunc_0_eppi sse_fn_eppi;
c4baa050 3175 SSEFunc_0_ppi sse_fn_ppi;
d3eb5eae 3176 SSEFunc_0_eppt sse_fn_eppt;
664e0f19
FB
3177
3178 b &= 0xff;
5fafdf24 3179 if (s->prefix & PREFIX_DATA)
664e0f19 3180 b1 = 1;
5fafdf24 3181 else if (s->prefix & PREFIX_REPZ)
664e0f19 3182 b1 = 2;
5fafdf24 3183 else if (s->prefix & PREFIX_REPNZ)
664e0f19
FB
3184 b1 = 3;
3185 else
3186 b1 = 0;
d3eb5eae
BS
3187 sse_fn_epp = sse_op_table1[b][b1];
3188 if (!sse_fn_epp) {
664e0f19 3189 goto illegal_op;
c4baa050 3190 }
a35f3ec7 3191 if ((b <= 0x5f && b >= 0x10) || b == 0xc6 || b == 0xc2) {
664e0f19
FB
3192 is_xmm = 1;
3193 } else {
3194 if (b1 == 0) {
3195 /* MMX case */
3196 is_xmm = 0;
3197 } else {
3198 is_xmm = 1;
3199 }
3200 }
3201 /* simple MMX/SSE operation */
3202 if (s->flags & HF_TS_MASK) {
3203 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
3204 return;
3205 }
3206 if (s->flags & HF_EM_MASK) {
3207 illegal_op:
3208 gen_exception(s, EXCP06_ILLOP, pc_start - s->cs_base);
3209 return;
3210 }
3211 if (is_xmm && !(s->flags & HF_OSFXSR_MASK))
4242b1bd
AZ
3212 if ((b != 0x38 && b != 0x3a) || (s->prefix & PREFIX_DATA))
3213 goto illegal_op;
e771edab
AJ
3214 if (b == 0x0e) {
3215 if (!(s->cpuid_ext2_features & CPUID_EXT2_3DNOW))
3216 goto illegal_op;
3217 /* femms */
d3eb5eae 3218 gen_helper_emms(cpu_env);
e771edab
AJ
3219 return;
3220 }
3221 if (b == 0x77) {
3222 /* emms */
d3eb5eae 3223 gen_helper_emms(cpu_env);
664e0f19
FB
3224 return;
3225 }
3226 /* prepare MMX state (XXX: optimize by storing fptt and fptags in
3227 the static cpu state) */
3228 if (!is_xmm) {
d3eb5eae 3229 gen_helper_enter_mmx(cpu_env);
664e0f19
FB
3230 }
3231
0af10c86 3232 modrm = cpu_ldub_code(env, s->pc++);
664e0f19
FB
3233 reg = ((modrm >> 3) & 7);
3234 if (is_xmm)
3235 reg |= rex_r;
3236 mod = (modrm >> 6) & 3;
d3eb5eae 3237 if (sse_fn_epp == SSE_SPECIAL) {
664e0f19
FB
3238 b |= (b1 << 8);
3239 switch(b) {
3240 case 0x0e7: /* movntq */
5fafdf24 3241 if (mod == 3)
664e0f19 3242 goto illegal_op;
0af10c86 3243 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3244 gen_stq_env_A0(s->mem_index, offsetof(CPUX86State,fpregs[reg].mmx));
664e0f19
FB
3245 break;
3246 case 0x1e7: /* movntdq */
3247 case 0x02b: /* movntps */
3248 case 0x12b: /* movntps */
2e21e749
T
3249 if (mod == 3)
3250 goto illegal_op;
0af10c86 3251 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
2e21e749
T
3252 gen_sto_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg]));
3253 break;
465e9838
FB
3254 case 0x3f0: /* lddqu */
3255 if (mod == 3)
664e0f19 3256 goto illegal_op;
0af10c86 3257 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
c2254920 3258 gen_ldo_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg]));
664e0f19 3259 break;
d9f4bb27
AP
3260 case 0x22b: /* movntss */
3261 case 0x32b: /* movntsd */
3262 if (mod == 3)
3263 goto illegal_op;
0af10c86 3264 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
d9f4bb27
AP
3265 if (b1 & 1) {
3266 gen_stq_env_A0(s->mem_index, offsetof(CPUX86State,
3267 xmm_regs[reg]));
3268 } else {
3269 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
3270 xmm_regs[reg].XMM_L(0)));
3271 gen_op_st_T0_A0(OT_LONG + s->mem_index);
3272 }
3273 break;
664e0f19 3274 case 0x6e: /* movd mm, ea */
dabd98dd
FB
3275#ifdef TARGET_X86_64
3276 if (s->dflag == 2) {
0af10c86 3277 gen_ldst_modrm(env, s, modrm, OT_QUAD, OR_TMP0, 0);
5af45186 3278 tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,fpregs[reg].mmx));
5fafdf24 3279 } else
dabd98dd
FB
3280#endif
3281 {
0af10c86 3282 gen_ldst_modrm(env, s, modrm, OT_LONG, OR_TMP0, 0);
5af45186
FB
3283 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3284 offsetof(CPUX86State,fpregs[reg].mmx));
a7812ae4
PB
3285 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
3286 gen_helper_movl_mm_T0_mmx(cpu_ptr0, cpu_tmp2_i32);
dabd98dd 3287 }
664e0f19
FB
3288 break;
3289 case 0x16e: /* movd xmm, ea */
dabd98dd
FB
3290#ifdef TARGET_X86_64
3291 if (s->dflag == 2) {
0af10c86 3292 gen_ldst_modrm(env, s, modrm, OT_QUAD, OR_TMP0, 0);
5af45186
FB
3293 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3294 offsetof(CPUX86State,xmm_regs[reg]));
a7812ae4 3295 gen_helper_movq_mm_T0_xmm(cpu_ptr0, cpu_T[0]);
5fafdf24 3296 } else
dabd98dd
FB
3297#endif
3298 {
0af10c86 3299 gen_ldst_modrm(env, s, modrm, OT_LONG, OR_TMP0, 0);
5af45186
FB
3300 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3301 offsetof(CPUX86State,xmm_regs[reg]));
b6abf97d 3302 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
a7812ae4 3303 gen_helper_movl_mm_T0_xmm(cpu_ptr0, cpu_tmp2_i32);
dabd98dd 3304 }
664e0f19
FB
3305 break;
3306 case 0x6f: /* movq mm, ea */
3307 if (mod != 3) {
0af10c86 3308 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3309 gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,fpregs[reg].mmx));
664e0f19
FB
3310 } else {
3311 rm = (modrm & 7);
b6abf97d 3312 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env,
5af45186 3313 offsetof(CPUX86State,fpregs[rm].mmx));
b6abf97d 3314 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env,
5af45186 3315 offsetof(CPUX86State,fpregs[reg].mmx));
664e0f19
FB
3316 }
3317 break;
3318 case 0x010: /* movups */
3319 case 0x110: /* movupd */
3320 case 0x028: /* movaps */
3321 case 0x128: /* movapd */
3322 case 0x16f: /* movdqa xmm, ea */
3323 case 0x26f: /* movdqu xmm, ea */
3324 if (mod != 3) {
0af10c86 3325 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3326 gen_ldo_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg]));
664e0f19
FB
3327 } else {
3328 rm = (modrm & 7) | REX_B(s);
3329 gen_op_movo(offsetof(CPUX86State,xmm_regs[reg]),
3330 offsetof(CPUX86State,xmm_regs[rm]));
3331 }
3332 break;
3333 case 0x210: /* movss xmm, ea */
3334 if (mod != 3) {
0af10c86 3335 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
57fec1fe 3336 gen_op_ld_T0_A0(OT_LONG + s->mem_index);
651ba608 3337 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
664e0f19 3338 gen_op_movl_T0_0();
651ba608
FB
3339 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(1)));
3340 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(2)));
3341 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(3)));
664e0f19
FB
3342 } else {
3343 rm = (modrm & 7) | REX_B(s);
3344 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)),
3345 offsetof(CPUX86State,xmm_regs[rm].XMM_L(0)));
3346 }
3347 break;
3348 case 0x310: /* movsd xmm, ea */
3349 if (mod != 3) {
0af10c86 3350 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3351 gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
664e0f19 3352 gen_op_movl_T0_0();
651ba608
FB
3353 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(2)));
3354 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(3)));
664e0f19
FB
3355 } else {
3356 rm = (modrm & 7) | REX_B(s);
3357 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)),
3358 offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)));
3359 }
3360 break;
3361 case 0x012: /* movlps */
3362 case 0x112: /* movlpd */
3363 if (mod != 3) {
0af10c86 3364 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3365 gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
664e0f19
FB
3366 } else {
3367 /* movhlps */
3368 rm = (modrm & 7) | REX_B(s);
3369 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)),
3370 offsetof(CPUX86State,xmm_regs[rm].XMM_Q(1)));
3371 }
3372 break;
465e9838
FB
3373 case 0x212: /* movsldup */
3374 if (mod != 3) {
0af10c86 3375 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3376 gen_ldo_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg]));
465e9838
FB
3377 } else {
3378 rm = (modrm & 7) | REX_B(s);
3379 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)),
3380 offsetof(CPUX86State,xmm_regs[rm].XMM_L(0)));
3381 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(2)),
3382 offsetof(CPUX86State,xmm_regs[rm].XMM_L(2)));
3383 }
3384 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(1)),
3385 offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
3386 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(3)),
3387 offsetof(CPUX86State,xmm_regs[reg].XMM_L(2)));
3388 break;
3389 case 0x312: /* movddup */
3390 if (mod != 3) {
0af10c86 3391 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3392 gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
465e9838
FB
3393 } else {
3394 rm = (modrm & 7) | REX_B(s);
3395 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)),
3396 offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)));
3397 }
3398 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(1)),
ba6526df 3399 offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
465e9838 3400 break;
664e0f19
FB
3401 case 0x016: /* movhps */
3402 case 0x116: /* movhpd */
3403 if (mod != 3) {
0af10c86 3404 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3405 gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(1)));
664e0f19
FB
3406 } else {
3407 /* movlhps */
3408 rm = (modrm & 7) | REX_B(s);
3409 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(1)),
3410 offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)));
3411 }
3412 break;
3413 case 0x216: /* movshdup */
3414 if (mod != 3) {
0af10c86 3415 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3416 gen_ldo_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg]));
664e0f19
FB
3417 } else {
3418 rm = (modrm & 7) | REX_B(s);
3419 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(1)),
3420 offsetof(CPUX86State,xmm_regs[rm].XMM_L(1)));
3421 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(3)),
3422 offsetof(CPUX86State,xmm_regs[rm].XMM_L(3)));
3423 }
3424 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)),
3425 offsetof(CPUX86State,xmm_regs[reg].XMM_L(1)));
3426 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(2)),
3427 offsetof(CPUX86State,xmm_regs[reg].XMM_L(3)));
3428 break;
d9f4bb27
AP
3429 case 0x178:
3430 case 0x378:
3431 {
3432 int bit_index, field_length;
3433
3434 if (b1 == 1 && reg != 0)
3435 goto illegal_op;
0af10c86
BS
3436 field_length = cpu_ldub_code(env, s->pc++) & 0x3F;
3437 bit_index = cpu_ldub_code(env, s->pc++) & 0x3F;
d9f4bb27
AP
3438 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3439 offsetof(CPUX86State,xmm_regs[reg]));
3440 if (b1 == 1)
d3eb5eae
BS
3441 gen_helper_extrq_i(cpu_env, cpu_ptr0,
3442 tcg_const_i32(bit_index),
3443 tcg_const_i32(field_length));
d9f4bb27 3444 else
d3eb5eae
BS
3445 gen_helper_insertq_i(cpu_env, cpu_ptr0,
3446 tcg_const_i32(bit_index),
3447 tcg_const_i32(field_length));
d9f4bb27
AP
3448 }
3449 break;
664e0f19 3450 case 0x7e: /* movd ea, mm */
dabd98dd
FB
3451#ifdef TARGET_X86_64
3452 if (s->dflag == 2) {
5af45186
FB
3453 tcg_gen_ld_i64(cpu_T[0], cpu_env,
3454 offsetof(CPUX86State,fpregs[reg].mmx));
0af10c86 3455 gen_ldst_modrm(env, s, modrm, OT_QUAD, OR_TMP0, 1);
5fafdf24 3456 } else
dabd98dd
FB
3457#endif
3458 {
5af45186
FB
3459 tcg_gen_ld32u_tl(cpu_T[0], cpu_env,
3460 offsetof(CPUX86State,fpregs[reg].mmx.MMX_L(0)));
0af10c86 3461 gen_ldst_modrm(env, s, modrm, OT_LONG, OR_TMP0, 1);
dabd98dd 3462 }
664e0f19
FB
3463 break;
3464 case 0x17e: /* movd ea, xmm */
dabd98dd
FB
3465#ifdef TARGET_X86_64
3466 if (s->dflag == 2) {
5af45186
FB
3467 tcg_gen_ld_i64(cpu_T[0], cpu_env,
3468 offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
0af10c86 3469 gen_ldst_modrm(env, s, modrm, OT_QUAD, OR_TMP0, 1);
5fafdf24 3470 } else
dabd98dd
FB
3471#endif
3472 {
5af45186
FB
3473 tcg_gen_ld32u_tl(cpu_T[0], cpu_env,
3474 offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
0af10c86 3475 gen_ldst_modrm(env, s, modrm, OT_LONG, OR_TMP0, 1);
dabd98dd 3476 }
664e0f19
FB
3477 break;
3478 case 0x27e: /* movq xmm, ea */
3479 if (mod != 3) {
0af10c86 3480 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3481 gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
664e0f19
FB
3482 } else {
3483 rm = (modrm & 7) | REX_B(s);
3484 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)),
3485 offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)));
3486 }
3487 gen_op_movq_env_0(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(1)));
3488 break;
3489 case 0x7f: /* movq ea, mm */
3490 if (mod != 3) {
0af10c86 3491 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3492 gen_stq_env_A0(s->mem_index, offsetof(CPUX86State,fpregs[reg].mmx));
664e0f19
FB
3493 } else {
3494 rm = (modrm & 7);
3495 gen_op_movq(offsetof(CPUX86State,fpregs[rm].mmx),
3496 offsetof(CPUX86State,fpregs[reg].mmx));
3497 }
3498 break;
3499 case 0x011: /* movups */
3500 case 0x111: /* movupd */
3501 case 0x029: /* movaps */
3502 case 0x129: /* movapd */
3503 case 0x17f: /* movdqa ea, xmm */
3504 case 0x27f: /* movdqu ea, xmm */
3505 if (mod != 3) {
0af10c86 3506 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3507 gen_sto_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg]));
664e0f19
FB
3508 } else {
3509 rm = (modrm & 7) | REX_B(s);
3510 gen_op_movo(offsetof(CPUX86State,xmm_regs[rm]),
3511 offsetof(CPUX86State,xmm_regs[reg]));
3512 }
3513 break;
3514 case 0x211: /* movss ea, xmm */
3515 if (mod != 3) {
0af10c86 3516 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
651ba608 3517 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
57fec1fe 3518 gen_op_st_T0_A0(OT_LONG + s->mem_index);
664e0f19
FB
3519 } else {
3520 rm = (modrm & 7) | REX_B(s);
3521 gen_op_movl(offsetof(CPUX86State,xmm_regs[rm].XMM_L(0)),
3522 offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
3523 }
3524 break;
3525 case 0x311: /* movsd ea, xmm */
3526 if (mod != 3) {
0af10c86 3527 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3528 gen_stq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
664e0f19
FB
3529 } else {
3530 rm = (modrm & 7) | REX_B(s);
3531 gen_op_movq(offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)),
3532 offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
3533 }
3534 break;
3535 case 0x013: /* movlps */
3536 case 0x113: /* movlpd */
3537 if (mod != 3) {
0af10c86 3538 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3539 gen_stq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
664e0f19
FB
3540 } else {
3541 goto illegal_op;
3542 }
3543 break;
3544 case 0x017: /* movhps */
3545 case 0x117: /* movhpd */
3546 if (mod != 3) {
0af10c86 3547 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3548 gen_stq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(1)));
664e0f19
FB
3549 } else {
3550 goto illegal_op;
3551 }
3552 break;
3553 case 0x71: /* shift mm, im */
3554 case 0x72:
3555 case 0x73:
3556 case 0x171: /* shift xmm, im */
3557 case 0x172:
3558 case 0x173:
c045af25
AK
3559 if (b1 >= 2) {
3560 goto illegal_op;
3561 }
0af10c86 3562 val = cpu_ldub_code(env, s->pc++);
664e0f19
FB
3563 if (is_xmm) {
3564 gen_op_movl_T0_im(val);
651ba608 3565 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(0)));
664e0f19 3566 gen_op_movl_T0_0();
651ba608 3567 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(1)));
664e0f19
FB
3568 op1_offset = offsetof(CPUX86State,xmm_t0);
3569 } else {
3570 gen_op_movl_T0_im(val);
651ba608 3571 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,mmx_t0.MMX_L(0)));
664e0f19 3572 gen_op_movl_T0_0();
651ba608 3573 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,mmx_t0.MMX_L(1)));
664e0f19
FB
3574 op1_offset = offsetof(CPUX86State,mmx_t0);
3575 }
d3eb5eae
BS
3576 sse_fn_epp = sse_op_table2[((b - 1) & 3) * 8 +
3577 (((modrm >> 3)) & 7)][b1];
3578 if (!sse_fn_epp) {
664e0f19 3579 goto illegal_op;
c4baa050 3580 }
664e0f19
FB
3581 if (is_xmm) {
3582 rm = (modrm & 7) | REX_B(s);
3583 op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
3584 } else {
3585 rm = (modrm & 7);
3586 op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
3587 }
5af45186
FB
3588 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op2_offset);
3589 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op1_offset);
d3eb5eae 3590 sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
3591 break;
3592 case 0x050: /* movmskps */
664e0f19 3593 rm = (modrm & 7) | REX_B(s);
5af45186
FB
3594 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3595 offsetof(CPUX86State,xmm_regs[rm]));
d3eb5eae 3596 gen_helper_movmskps(cpu_tmp2_i32, cpu_env, cpu_ptr0);
b6abf97d 3597 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
57fec1fe 3598 gen_op_mov_reg_T0(OT_LONG, reg);
664e0f19
FB
3599 break;
3600 case 0x150: /* movmskpd */
664e0f19 3601 rm = (modrm & 7) | REX_B(s);
5af45186
FB
3602 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3603 offsetof(CPUX86State,xmm_regs[rm]));
d3eb5eae 3604 gen_helper_movmskpd(cpu_tmp2_i32, cpu_env, cpu_ptr0);
b6abf97d 3605 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
57fec1fe 3606 gen_op_mov_reg_T0(OT_LONG, reg);
664e0f19
FB
3607 break;
3608 case 0x02a: /* cvtpi2ps */
3609 case 0x12a: /* cvtpi2pd */
d3eb5eae 3610 gen_helper_enter_mmx(cpu_env);
664e0f19 3611 if (mod != 3) {
0af10c86 3612 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
664e0f19 3613 op2_offset = offsetof(CPUX86State,mmx_t0);
8686c490 3614 gen_ldq_env_A0(s->mem_index, op2_offset);
664e0f19
FB
3615 } else {
3616 rm = (modrm & 7);
3617 op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
3618 }
3619 op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
5af45186
FB
3620 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
3621 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
664e0f19
FB
3622 switch(b >> 8) {
3623 case 0x0:
d3eb5eae 3624 gen_helper_cvtpi2ps(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
3625 break;
3626 default:
3627 case 0x1:
d3eb5eae 3628 gen_helper_cvtpi2pd(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
3629 break;
3630 }
3631 break;
3632 case 0x22a: /* cvtsi2ss */
3633 case 0x32a: /* cvtsi2sd */
3634 ot = (s->dflag == 2) ? OT_QUAD : OT_LONG;
0af10c86 3635 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
664e0f19 3636 op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
5af45186 3637 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
28e10711 3638 if (ot == OT_LONG) {
d3eb5eae 3639 SSEFunc_0_epi sse_fn_epi = sse_op_table3ai[(b >> 8) & 1];
28e10711 3640 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
d3eb5eae 3641 sse_fn_epi(cpu_env, cpu_ptr0, cpu_tmp2_i32);
28e10711 3642 } else {
11f8cdbc 3643#ifdef TARGET_X86_64
d3eb5eae
BS
3644 SSEFunc_0_epl sse_fn_epl = sse_op_table3aq[(b >> 8) & 1];
3645 sse_fn_epl(cpu_env, cpu_ptr0, cpu_T[0]);
11f8cdbc
SW
3646#else
3647 goto illegal_op;
3648#endif
28e10711 3649 }
664e0f19
FB
3650 break;
3651 case 0x02c: /* cvttps2pi */
3652 case 0x12c: /* cvttpd2pi */
3653 case 0x02d: /* cvtps2pi */
3654 case 0x12d: /* cvtpd2pi */
d3eb5eae 3655 gen_helper_enter_mmx(cpu_env);
664e0f19 3656 if (mod != 3) {
0af10c86 3657 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
664e0f19 3658 op2_offset = offsetof(CPUX86State,xmm_t0);
8686c490 3659 gen_ldo_env_A0(s->mem_index, op2_offset);
664e0f19
FB
3660 } else {
3661 rm = (modrm & 7) | REX_B(s);
3662 op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
3663 }
3664 op1_offset = offsetof(CPUX86State,fpregs[reg & 7].mmx);
5af45186
FB
3665 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
3666 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
664e0f19
FB
3667 switch(b) {
3668 case 0x02c:
d3eb5eae 3669 gen_helper_cvttps2pi(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
3670 break;
3671 case 0x12c:
d3eb5eae 3672 gen_helper_cvttpd2pi(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
3673 break;
3674 case 0x02d:
d3eb5eae 3675 gen_helper_cvtps2pi(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
3676 break;
3677 case 0x12d:
d3eb5eae 3678 gen_helper_cvtpd2pi(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
3679 break;
3680 }
3681 break;
3682 case 0x22c: /* cvttss2si */
3683 case 0x32c: /* cvttsd2si */
3684 case 0x22d: /* cvtss2si */
3685 case 0x32d: /* cvtsd2si */
3686 ot = (s->dflag == 2) ? OT_QUAD : OT_LONG;
31313213 3687 if (mod != 3) {
0af10c86 3688 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
31313213 3689 if ((b >> 8) & 1) {
8686c490 3690 gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_t0.XMM_Q(0)));
31313213 3691 } else {
57fec1fe 3692 gen_op_ld_T0_A0(OT_LONG + s->mem_index);
651ba608 3693 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(0)));
31313213
FB
3694 }
3695 op2_offset = offsetof(CPUX86State,xmm_t0);
3696 } else {
3697 rm = (modrm & 7) | REX_B(s);
3698 op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
3699 }
5af45186
FB
3700 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op2_offset);
3701 if (ot == OT_LONG) {
d3eb5eae 3702 SSEFunc_i_ep sse_fn_i_ep =
bedc2ac1 3703 sse_op_table3bi[((b >> 7) & 2) | (b & 1)];
d3eb5eae 3704 sse_fn_i_ep(cpu_tmp2_i32, cpu_env, cpu_ptr0);
b6abf97d 3705 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
5af45186 3706 } else {
11f8cdbc 3707#ifdef TARGET_X86_64
d3eb5eae 3708 SSEFunc_l_ep sse_fn_l_ep =
bedc2ac1 3709 sse_op_table3bq[((b >> 7) & 2) | (b & 1)];
d3eb5eae 3710 sse_fn_l_ep(cpu_T[0], cpu_env, cpu_ptr0);
11f8cdbc
SW
3711#else
3712 goto illegal_op;
3713#endif
5af45186 3714 }
57fec1fe 3715 gen_op_mov_reg_T0(ot, reg);
664e0f19
FB
3716 break;
3717 case 0xc4: /* pinsrw */
5fafdf24 3718 case 0x1c4:
d1e42c5c 3719 s->rip_offset = 1;
0af10c86
BS
3720 gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
3721 val = cpu_ldub_code(env, s->pc++);
664e0f19
FB
3722 if (b1) {
3723 val &= 7;
5af45186
FB
3724 tcg_gen_st16_tl(cpu_T[0], cpu_env,
3725 offsetof(CPUX86State,xmm_regs[reg].XMM_W(val)));
664e0f19
FB
3726 } else {
3727 val &= 3;
5af45186
FB
3728 tcg_gen_st16_tl(cpu_T[0], cpu_env,
3729 offsetof(CPUX86State,fpregs[reg].mmx.MMX_W(val)));
664e0f19
FB
3730 }
3731 break;
3732 case 0xc5: /* pextrw */
5fafdf24 3733 case 0x1c5:
664e0f19
FB
3734 if (mod != 3)
3735 goto illegal_op;
6dc2d0da 3736 ot = (s->dflag == 2) ? OT_QUAD : OT_LONG;
0af10c86 3737 val = cpu_ldub_code(env, s->pc++);
664e0f19
FB
3738 if (b1) {
3739 val &= 7;
3740 rm = (modrm & 7) | REX_B(s);
5af45186
FB
3741 tcg_gen_ld16u_tl(cpu_T[0], cpu_env,
3742 offsetof(CPUX86State,xmm_regs[rm].XMM_W(val)));
664e0f19
FB
3743 } else {
3744 val &= 3;
3745 rm = (modrm & 7);
5af45186
FB
3746 tcg_gen_ld16u_tl(cpu_T[0], cpu_env,
3747 offsetof(CPUX86State,fpregs[rm].mmx.MMX_W(val)));
664e0f19
FB
3748 }
3749 reg = ((modrm >> 3) & 7) | rex_r;
6dc2d0da 3750 gen_op_mov_reg_T0(ot, reg);
664e0f19
FB
3751 break;
3752 case 0x1d6: /* movq ea, xmm */
3753 if (mod != 3) {
0af10c86 3754 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8686c490 3755 gen_stq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
664e0f19
FB
3756 } else {
3757 rm = (modrm & 7) | REX_B(s);
3758 gen_op_movq(offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)),
3759 offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
3760 gen_op_movq_env_0(offsetof(CPUX86State,xmm_regs[rm].XMM_Q(1)));
3761 }
3762 break;
3763 case 0x2d6: /* movq2dq */
d3eb5eae 3764 gen_helper_enter_mmx(cpu_env);
480c1cdb
FB
3765 rm = (modrm & 7);
3766 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)),
3767 offsetof(CPUX86State,fpregs[rm].mmx));
3768 gen_op_movq_env_0(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(1)));
664e0f19
FB
3769 break;
3770 case 0x3d6: /* movdq2q */
d3eb5eae 3771 gen_helper_enter_mmx(cpu_env);
480c1cdb
FB
3772 rm = (modrm & 7) | REX_B(s);
3773 gen_op_movq(offsetof(CPUX86State,fpregs[reg & 7].mmx),
3774 offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)));
664e0f19
FB
3775 break;
3776 case 0xd7: /* pmovmskb */
3777 case 0x1d7:
3778 if (mod != 3)
3779 goto illegal_op;
3780 if (b1) {
3781 rm = (modrm & 7) | REX_B(s);
5af45186 3782 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, offsetof(CPUX86State,xmm_regs[rm]));
d3eb5eae 3783 gen_helper_pmovmskb_xmm(cpu_tmp2_i32, cpu_env, cpu_ptr0);
664e0f19
FB
3784 } else {
3785 rm = (modrm & 7);
5af45186 3786 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, offsetof(CPUX86State,fpregs[rm].mmx));
d3eb5eae 3787 gen_helper_pmovmskb_mmx(cpu_tmp2_i32, cpu_env, cpu_ptr0);
664e0f19 3788 }
b6abf97d 3789 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
664e0f19 3790 reg = ((modrm >> 3) & 7) | rex_r;
57fec1fe 3791 gen_op_mov_reg_T0(OT_LONG, reg);
664e0f19 3792 break;
4242b1bd 3793 case 0x138:
000cacf6
AZ
3794 if (s->prefix & PREFIX_REPNZ)
3795 goto crc32;
3796 case 0x038:
4242b1bd 3797 b = modrm;
0af10c86 3798 modrm = cpu_ldub_code(env, s->pc++);
4242b1bd
AZ
3799 rm = modrm & 7;
3800 reg = ((modrm >> 3) & 7) | rex_r;
3801 mod = (modrm >> 6) & 3;
c045af25
AK
3802 if (b1 >= 2) {
3803 goto illegal_op;
3804 }
4242b1bd 3805
d3eb5eae
BS
3806 sse_fn_epp = sse_op_table6[b].op[b1];
3807 if (!sse_fn_epp) {
4242b1bd 3808 goto illegal_op;
c4baa050 3809 }
222a3336
AZ
3810 if (!(s->cpuid_ext_features & sse_op_table6[b].ext_mask))
3811 goto illegal_op;
4242b1bd
AZ
3812
3813 if (b1) {
3814 op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
3815 if (mod == 3) {
3816 op2_offset = offsetof(CPUX86State,xmm_regs[rm | REX_B(s)]);
3817 } else {
3818 op2_offset = offsetof(CPUX86State,xmm_t0);
0af10c86 3819 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
222a3336
AZ
3820 switch (b) {
3821 case 0x20: case 0x30: /* pmovsxbw, pmovzxbw */
3822 case 0x23: case 0x33: /* pmovsxwd, pmovzxwd */
3823 case 0x25: case 0x35: /* pmovsxdq, pmovzxdq */
3824 gen_ldq_env_A0(s->mem_index, op2_offset +
3825 offsetof(XMMReg, XMM_Q(0)));
3826 break;
3827 case 0x21: case 0x31: /* pmovsxbd, pmovzxbd */
3828 case 0x24: case 0x34: /* pmovsxwq, pmovzxwq */
a7812ae4 3829 tcg_gen_qemu_ld32u(cpu_tmp0, cpu_A0,
222a3336 3830 (s->mem_index >> 2) - 1);
a7812ae4 3831 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_tmp0);
222a3336
AZ
3832 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, op2_offset +
3833 offsetof(XMMReg, XMM_L(0)));
3834 break;
3835 case 0x22: case 0x32: /* pmovsxbq, pmovzxbq */
3836 tcg_gen_qemu_ld16u(cpu_tmp0, cpu_A0,
3837 (s->mem_index >> 2) - 1);
3838 tcg_gen_st16_tl(cpu_tmp0, cpu_env, op2_offset +
3839 offsetof(XMMReg, XMM_W(0)));
3840 break;
3841 case 0x2a: /* movntqda */
3842 gen_ldo_env_A0(s->mem_index, op1_offset);
3843 return;
3844 default:
3845 gen_ldo_env_A0(s->mem_index, op2_offset);
3846 }
4242b1bd
AZ
3847 }
3848 } else {
3849 op1_offset = offsetof(CPUX86State,fpregs[reg].mmx);
3850 if (mod == 3) {
3851 op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
3852 } else {
3853 op2_offset = offsetof(CPUX86State,mmx_t0);
0af10c86 3854 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
4242b1bd
AZ
3855 gen_ldq_env_A0(s->mem_index, op2_offset);
3856 }
3857 }
d3eb5eae 3858 if (sse_fn_epp == SSE_SPECIAL) {
222a3336 3859 goto illegal_op;
c4baa050 3860 }
222a3336 3861
4242b1bd
AZ
3862 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
3863 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
d3eb5eae 3864 sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
222a3336 3865
3ca51d07
RH
3866 if (b == 0x17) {
3867 set_cc_op(s, CC_OP_EFLAGS);
3868 }
4242b1bd 3869 break;
222a3336
AZ
3870 case 0x338: /* crc32 */
3871 crc32:
3872 b = modrm;
0af10c86 3873 modrm = cpu_ldub_code(env, s->pc++);
222a3336
AZ
3874 reg = ((modrm >> 3) & 7) | rex_r;
3875
3876 if (b != 0xf0 && b != 0xf1)
3877 goto illegal_op;
3878 if (!(s->cpuid_ext_features & CPUID_EXT_SSE42))
4242b1bd
AZ
3879 goto illegal_op;
3880
222a3336
AZ
3881 if (b == 0xf0)
3882 ot = OT_BYTE;
3883 else if (b == 0xf1 && s->dflag != 2)
3884 if (s->prefix & PREFIX_DATA)
3885 ot = OT_WORD;
3886 else
3887 ot = OT_LONG;
3888 else
3889 ot = OT_QUAD;
3890
3891 gen_op_mov_TN_reg(OT_LONG, 0, reg);
3892 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
0af10c86 3893 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
a7812ae4
PB
3894 gen_helper_crc32(cpu_T[0], cpu_tmp2_i32,
3895 cpu_T[0], tcg_const_i32(8 << ot));
222a3336
AZ
3896
3897 ot = (s->dflag == 2) ? OT_QUAD : OT_LONG;
3898 gen_op_mov_reg_T0(ot, reg);
3899 break;
3900 case 0x03a:
3901 case 0x13a:
4242b1bd 3902 b = modrm;
0af10c86 3903 modrm = cpu_ldub_code(env, s->pc++);
4242b1bd
AZ
3904 rm = modrm & 7;
3905 reg = ((modrm >> 3) & 7) | rex_r;
3906 mod = (modrm >> 6) & 3;
c045af25
AK
3907 if (b1 >= 2) {
3908 goto illegal_op;
3909 }
4242b1bd 3910
d3eb5eae
BS
3911 sse_fn_eppi = sse_op_table7[b].op[b1];
3912 if (!sse_fn_eppi) {
4242b1bd 3913 goto illegal_op;
c4baa050 3914 }
222a3336
AZ
3915 if (!(s->cpuid_ext_features & sse_op_table7[b].ext_mask))
3916 goto illegal_op;
3917
d3eb5eae 3918 if (sse_fn_eppi == SSE_SPECIAL) {
222a3336
AZ
3919 ot = (s->dflag == 2) ? OT_QUAD : OT_LONG;
3920 rm = (modrm & 7) | REX_B(s);
3921 if (mod != 3)
0af10c86 3922 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
222a3336 3923 reg = ((modrm >> 3) & 7) | rex_r;
0af10c86 3924 val = cpu_ldub_code(env, s->pc++);
222a3336
AZ
3925 switch (b) {
3926 case 0x14: /* pextrb */
3927 tcg_gen_ld8u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
3928 xmm_regs[reg].XMM_B(val & 15)));
3929 if (mod == 3)
3930 gen_op_mov_reg_T0(ot, rm);
3931 else
3932 tcg_gen_qemu_st8(cpu_T[0], cpu_A0,
3933 (s->mem_index >> 2) - 1);
3934 break;
3935 case 0x15: /* pextrw */
3936 tcg_gen_ld16u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
3937 xmm_regs[reg].XMM_W(val & 7)));
3938 if (mod == 3)
3939 gen_op_mov_reg_T0(ot, rm);
3940 else
3941 tcg_gen_qemu_st16(cpu_T[0], cpu_A0,
3942 (s->mem_index >> 2) - 1);
3943 break;
3944 case 0x16:
3945 if (ot == OT_LONG) { /* pextrd */
3946 tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env,
3947 offsetof(CPUX86State,
3948 xmm_regs[reg].XMM_L(val & 3)));
a7812ae4 3949 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
222a3336 3950 if (mod == 3)
a7812ae4 3951 gen_op_mov_reg_v(ot, rm, cpu_T[0]);
222a3336 3952 else
a7812ae4 3953 tcg_gen_qemu_st32(cpu_T[0], cpu_A0,
222a3336
AZ
3954 (s->mem_index >> 2) - 1);
3955 } else { /* pextrq */
a7812ae4 3956#ifdef TARGET_X86_64
222a3336
AZ
3957 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env,
3958 offsetof(CPUX86State,
3959 xmm_regs[reg].XMM_Q(val & 1)));
3960 if (mod == 3)
3961 gen_op_mov_reg_v(ot, rm, cpu_tmp1_i64);
3962 else
3963 tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_A0,
3964 (s->mem_index >> 2) - 1);
a7812ae4
PB
3965#else
3966 goto illegal_op;
3967#endif
222a3336
AZ
3968 }
3969 break;
3970 case 0x17: /* extractps */
3971 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
3972 xmm_regs[reg].XMM_L(val & 3)));
3973 if (mod == 3)
3974 gen_op_mov_reg_T0(ot, rm);
3975 else
3976 tcg_gen_qemu_st32(cpu_T[0], cpu_A0,
3977 (s->mem_index >> 2) - 1);
3978 break;
3979 case 0x20: /* pinsrb */
3980 if (mod == 3)
3981 gen_op_mov_TN_reg(OT_LONG, 0, rm);
3982 else
a7812ae4 3983 tcg_gen_qemu_ld8u(cpu_tmp0, cpu_A0,
222a3336 3984 (s->mem_index >> 2) - 1);
a7812ae4 3985 tcg_gen_st8_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State,
222a3336
AZ
3986 xmm_regs[reg].XMM_B(val & 15)));
3987 break;
3988 case 0x21: /* insertps */
a7812ae4 3989 if (mod == 3) {
222a3336
AZ
3990 tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env,
3991 offsetof(CPUX86State,xmm_regs[rm]
3992 .XMM_L((val >> 6) & 3)));
a7812ae4
PB
3993 } else {
3994 tcg_gen_qemu_ld32u(cpu_tmp0, cpu_A0,
222a3336 3995 (s->mem_index >> 2) - 1);
a7812ae4
PB
3996 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_tmp0);
3997 }
222a3336
AZ
3998 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env,
3999 offsetof(CPUX86State,xmm_regs[reg]
4000 .XMM_L((val >> 4) & 3)));
4001 if ((val >> 0) & 1)
4002 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
4003 cpu_env, offsetof(CPUX86State,
4004 xmm_regs[reg].XMM_L(0)));
4005 if ((val >> 1) & 1)
4006 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
4007 cpu_env, offsetof(CPUX86State,
4008 xmm_regs[reg].XMM_L(1)));
4009 if ((val >> 2) & 1)
4010 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
4011 cpu_env, offsetof(CPUX86State,
4012 xmm_regs[reg].XMM_L(2)));
4013 if ((val >> 3) & 1)
4014 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
4015 cpu_env, offsetof(CPUX86State,
4016 xmm_regs[reg].XMM_L(3)));
4017 break;
4018 case 0x22:
4019 if (ot == OT_LONG) { /* pinsrd */
4020 if (mod == 3)
a7812ae4 4021 gen_op_mov_v_reg(ot, cpu_tmp0, rm);
222a3336 4022 else
a7812ae4 4023 tcg_gen_qemu_ld32u(cpu_tmp0, cpu_A0,
222a3336 4024 (s->mem_index >> 2) - 1);
a7812ae4 4025 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_tmp0);
222a3336
AZ
4026 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env,
4027 offsetof(CPUX86State,
4028 xmm_regs[reg].XMM_L(val & 3)));
4029 } else { /* pinsrq */
a7812ae4 4030#ifdef TARGET_X86_64
222a3336
AZ
4031 if (mod == 3)
4032 gen_op_mov_v_reg(ot, cpu_tmp1_i64, rm);
4033 else
4034 tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_A0,
4035 (s->mem_index >> 2) - 1);
4036 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env,
4037 offsetof(CPUX86State,
4038 xmm_regs[reg].XMM_Q(val & 1)));
a7812ae4
PB
4039#else
4040 goto illegal_op;
4041#endif
222a3336
AZ
4042 }
4043 break;
4044 }
4045 return;
4046 }
4242b1bd
AZ
4047
4048 if (b1) {
4049 op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
4050 if (mod == 3) {
4051 op2_offset = offsetof(CPUX86State,xmm_regs[rm | REX_B(s)]);
4052 } else {
4053 op2_offset = offsetof(CPUX86State,xmm_t0);
0af10c86 4054 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
4242b1bd
AZ
4055 gen_ldo_env_A0(s->mem_index, op2_offset);
4056 }
4057 } else {
4058 op1_offset = offsetof(CPUX86State,fpregs[reg].mmx);
4059 if (mod == 3) {
4060 op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
4061 } else {
4062 op2_offset = offsetof(CPUX86State,mmx_t0);
0af10c86 4063 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
4242b1bd
AZ
4064 gen_ldq_env_A0(s->mem_index, op2_offset);
4065 }
4066 }
0af10c86 4067 val = cpu_ldub_code(env, s->pc++);
4242b1bd 4068
222a3336 4069 if ((b & 0xfc) == 0x60) { /* pcmpXstrX */
3ca51d07 4070 set_cc_op(s, CC_OP_EFLAGS);
222a3336
AZ
4071
4072 if (s->dflag == 2)
4073 /* The helper must use entire 64-bit gp registers */
4074 val |= 1 << 8;
4075 }
4076
4242b1bd
AZ
4077 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4078 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
d3eb5eae 4079 sse_fn_eppi(cpu_env, cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
4242b1bd 4080 break;
664e0f19
FB
4081 default:
4082 goto illegal_op;
4083 }
4084 } else {
4085 /* generic MMX or SSE operation */
d1e42c5c 4086 switch(b) {
d1e42c5c
FB
4087 case 0x70: /* pshufx insn */
4088 case 0xc6: /* pshufx insn */
4089 case 0xc2: /* compare insns */
4090 s->rip_offset = 1;
4091 break;
4092 default:
4093 break;
664e0f19
FB
4094 }
4095 if (is_xmm) {
4096 op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
4097 if (mod != 3) {
0af10c86 4098 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
664e0f19 4099 op2_offset = offsetof(CPUX86State,xmm_t0);
480c1cdb 4100 if (b1 >= 2 && ((b >= 0x50 && b <= 0x5f && b != 0x5b) ||
664e0f19
FB
4101 b == 0xc2)) {
4102 /* specific case for SSE single instructions */
4103 if (b1 == 2) {
4104 /* 32 bit access */
57fec1fe 4105 gen_op_ld_T0_A0(OT_LONG + s->mem_index);
651ba608 4106 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(0)));
664e0f19
FB
4107 } else {
4108 /* 64 bit access */
8686c490 4109 gen_ldq_env_A0(s->mem_index, offsetof(CPUX86State,xmm_t0.XMM_D(0)));
664e0f19
FB
4110 }
4111 } else {
8686c490 4112 gen_ldo_env_A0(s->mem_index, op2_offset);
664e0f19
FB
4113 }
4114 } else {
4115 rm = (modrm & 7) | REX_B(s);
4116 op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
4117 }
4118 } else {
4119 op1_offset = offsetof(CPUX86State,fpregs[reg].mmx);
4120 if (mod != 3) {
0af10c86 4121 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
664e0f19 4122 op2_offset = offsetof(CPUX86State,mmx_t0);
8686c490 4123 gen_ldq_env_A0(s->mem_index, op2_offset);
664e0f19
FB
4124 } else {
4125 rm = (modrm & 7);
4126 op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
4127 }
4128 }
4129 switch(b) {
a35f3ec7 4130 case 0x0f: /* 3DNow! data insns */
e771edab
AJ
4131 if (!(s->cpuid_ext2_features & CPUID_EXT2_3DNOW))
4132 goto illegal_op;
0af10c86 4133 val = cpu_ldub_code(env, s->pc++);
d3eb5eae
BS
4134 sse_fn_epp = sse_op_table5[val];
4135 if (!sse_fn_epp) {
a35f3ec7 4136 goto illegal_op;
c4baa050 4137 }
5af45186
FB
4138 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4139 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
d3eb5eae 4140 sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
a35f3ec7 4141 break;
664e0f19
FB
4142 case 0x70: /* pshufx insn */
4143 case 0xc6: /* pshufx insn */
0af10c86 4144 val = cpu_ldub_code(env, s->pc++);
5af45186
FB
4145 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4146 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
c4baa050 4147 /* XXX: introduce a new table? */
d3eb5eae 4148 sse_fn_ppi = (SSEFunc_0_ppi)sse_fn_epp;
c4baa050 4149 sse_fn_ppi(cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
664e0f19
FB
4150 break;
4151 case 0xc2:
4152 /* compare insns */
0af10c86 4153 val = cpu_ldub_code(env, s->pc++);
664e0f19
FB
4154 if (val >= 8)
4155 goto illegal_op;
d3eb5eae 4156 sse_fn_epp = sse_op_table4[val][b1];
c4baa050 4157
5af45186
FB
4158 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4159 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
d3eb5eae 4160 sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19 4161 break;
b8b6a50b
FB
4162 case 0xf7:
4163 /* maskmov : we must prepare A0 */
4164 if (mod != 3)
4165 goto illegal_op;
4166#ifdef TARGET_X86_64
4167 if (s->aflag == 2) {
4168 gen_op_movq_A0_reg(R_EDI);
4169 } else
4170#endif
4171 {
4172 gen_op_movl_A0_reg(R_EDI);
4173 if (s->aflag == 0)
4174 gen_op_andl_A0_ffff();
4175 }
4176 gen_add_A0_ds_seg(s);
4177
4178 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4179 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
c4baa050 4180 /* XXX: introduce a new table? */
d3eb5eae
BS
4181 sse_fn_eppt = (SSEFunc_0_eppt)sse_fn_epp;
4182 sse_fn_eppt(cpu_env, cpu_ptr0, cpu_ptr1, cpu_A0);
b8b6a50b 4183 break;
664e0f19 4184 default:
5af45186
FB
4185 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4186 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
d3eb5eae 4187 sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
4188 break;
4189 }
4190 if (b == 0x2e || b == 0x2f) {
3ca51d07 4191 set_cc_op(s, CC_OP_EFLAGS);
664e0f19
FB
4192 }
4193 }
4194}
4195
2c0262af
FB
4196/* convert one instruction. s->is_jmp is set if the translation must
4197 be stopped. Return the next pc value */
0af10c86
BS
4198static target_ulong disas_insn(CPUX86State *env, DisasContext *s,
4199 target_ulong pc_start)
2c0262af
FB
4200{
4201 int b, prefixes, aflag, dflag;
4202 int shift, ot;
4203 int modrm, reg, rm, mod, reg_addr, op, opreg, offset_addr, val;
14ce26e7
FB
4204 target_ulong next_eip, tval;
4205 int rex_w, rex_r;
2c0262af 4206
fdefe51c 4207 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
70cff25e 4208 tcg_gen_debug_insn_start(pc_start);
fdefe51c 4209 }
2c0262af
FB
4210 s->pc = pc_start;
4211 prefixes = 0;
4212 aflag = s->code32;
4213 dflag = s->code32;
4214 s->override = -1;
14ce26e7
FB
4215 rex_w = -1;
4216 rex_r = 0;
4217#ifdef TARGET_X86_64
4218 s->rex_x = 0;
4219 s->rex_b = 0;
5fafdf24 4220 x86_64_hregs = 0;
14ce26e7
FB
4221#endif
4222 s->rip_offset = 0; /* for relative ip address */
2c0262af 4223 next_byte:
0af10c86 4224 b = cpu_ldub_code(env, s->pc);
2c0262af
FB
4225 s->pc++;
4226 /* check prefixes */
14ce26e7
FB
4227#ifdef TARGET_X86_64
4228 if (CODE64(s)) {
4229 switch (b) {
4230 case 0xf3:
4231 prefixes |= PREFIX_REPZ;
4232 goto next_byte;
4233 case 0xf2:
4234 prefixes |= PREFIX_REPNZ;
4235 goto next_byte;
4236 case 0xf0:
4237 prefixes |= PREFIX_LOCK;
4238 goto next_byte;
4239 case 0x2e:
4240 s->override = R_CS;
4241 goto next_byte;
4242 case 0x36:
4243 s->override = R_SS;
4244 goto next_byte;
4245 case 0x3e:
4246 s->override = R_DS;
4247 goto next_byte;
4248 case 0x26:
4249 s->override = R_ES;
4250 goto next_byte;
4251 case 0x64:
4252 s->override = R_FS;
4253 goto next_byte;
4254 case 0x65:
4255 s->override = R_GS;
4256 goto next_byte;
4257 case 0x66:
4258 prefixes |= PREFIX_DATA;
4259 goto next_byte;
4260 case 0x67:
4261 prefixes |= PREFIX_ADR;
4262 goto next_byte;
4263 case 0x40 ... 0x4f:
4264 /* REX prefix */
4265 rex_w = (b >> 3) & 1;
4266 rex_r = (b & 0x4) << 1;
4267 s->rex_x = (b & 0x2) << 2;
4268 REX_B(s) = (b & 0x1) << 3;
4269 x86_64_hregs = 1; /* select uniform byte register addressing */
4270 goto next_byte;
4271 }
4272 if (rex_w == 1) {
4273 /* 0x66 is ignored if rex.w is set */
4274 dflag = 2;
4275 } else {
4276 if (prefixes & PREFIX_DATA)
4277 dflag ^= 1;
4278 }
4279 if (!(prefixes & PREFIX_ADR))
4280 aflag = 2;
5fafdf24 4281 } else
14ce26e7
FB
4282#endif
4283 {
4284 switch (b) {
4285 case 0xf3:
4286 prefixes |= PREFIX_REPZ;
4287 goto next_byte;
4288 case 0xf2:
4289 prefixes |= PREFIX_REPNZ;
4290 goto next_byte;
4291 case 0xf0:
4292 prefixes |= PREFIX_LOCK;
4293 goto next_byte;
4294 case 0x2e:
4295 s->override = R_CS;
4296 goto next_byte;
4297 case 0x36:
4298 s->override = R_SS;
4299 goto next_byte;
4300 case 0x3e:
4301 s->override = R_DS;
4302 goto next_byte;
4303 case 0x26:
4304 s->override = R_ES;
4305 goto next_byte;
4306 case 0x64:
4307 s->override = R_FS;
4308 goto next_byte;
4309 case 0x65:
4310 s->override = R_GS;
4311 goto next_byte;
4312 case 0x66:
4313 prefixes |= PREFIX_DATA;
4314 goto next_byte;
4315 case 0x67:
4316 prefixes |= PREFIX_ADR;
4317 goto next_byte;
4318 }
4319 if (prefixes & PREFIX_DATA)
4320 dflag ^= 1;
4321 if (prefixes & PREFIX_ADR)
4322 aflag ^= 1;
2c0262af
FB
4323 }
4324
2c0262af
FB
4325 s->prefix = prefixes;
4326 s->aflag = aflag;
4327 s->dflag = dflag;
4328
4329 /* lock generation */
4330 if (prefixes & PREFIX_LOCK)
a7812ae4 4331 gen_helper_lock();
2c0262af
FB
4332
4333 /* now check op code */
4334 reswitch:
4335 switch(b) {
4336 case 0x0f:
4337 /**************************/
4338 /* extended op code */
0af10c86 4339 b = cpu_ldub_code(env, s->pc++) | 0x100;
2c0262af 4340 goto reswitch;
3b46e624 4341
2c0262af
FB
4342 /**************************/
4343 /* arith & logic */
4344 case 0x00 ... 0x05:
4345 case 0x08 ... 0x0d:
4346 case 0x10 ... 0x15:
4347 case 0x18 ... 0x1d:
4348 case 0x20 ... 0x25:
4349 case 0x28 ... 0x2d:
4350 case 0x30 ... 0x35:
4351 case 0x38 ... 0x3d:
4352 {
4353 int op, f, val;
4354 op = (b >> 3) & 7;
4355 f = (b >> 1) & 3;
4356
4357 if ((b & 1) == 0)
4358 ot = OT_BYTE;
4359 else
14ce26e7 4360 ot = dflag + OT_WORD;
3b46e624 4361
2c0262af
FB
4362 switch(f) {
4363 case 0: /* OP Ev, Gv */
0af10c86 4364 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7 4365 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af 4366 mod = (modrm >> 6) & 3;
14ce26e7 4367 rm = (modrm & 7) | REX_B(s);
2c0262af 4368 if (mod != 3) {
0af10c86 4369 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
2c0262af
FB
4370 opreg = OR_TMP0;
4371 } else if (op == OP_XORL && rm == reg) {
4372 xor_zero:
4373 /* xor reg, reg optimisation */
4374 gen_op_movl_T0_0();
3ca51d07 4375 set_cc_op(s, CC_OP_LOGICB + ot);
57fec1fe 4376 gen_op_mov_reg_T0(ot, reg);
2c0262af
FB
4377 gen_op_update1_cc();
4378 break;
4379 } else {
4380 opreg = rm;
4381 }
57fec1fe 4382 gen_op_mov_TN_reg(ot, 1, reg);
2c0262af
FB
4383 gen_op(s, op, ot, opreg);
4384 break;
4385 case 1: /* OP Gv, Ev */
0af10c86 4386 modrm = cpu_ldub_code(env, s->pc++);
2c0262af 4387 mod = (modrm >> 6) & 3;
14ce26e7
FB
4388 reg = ((modrm >> 3) & 7) | rex_r;
4389 rm = (modrm & 7) | REX_B(s);
2c0262af 4390 if (mod != 3) {
0af10c86 4391 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
57fec1fe 4392 gen_op_ld_T1_A0(ot + s->mem_index);
2c0262af
FB
4393 } else if (op == OP_XORL && rm == reg) {
4394 goto xor_zero;
4395 } else {
57fec1fe 4396 gen_op_mov_TN_reg(ot, 1, rm);
2c0262af
FB
4397 }
4398 gen_op(s, op, ot, reg);
4399 break;
4400 case 2: /* OP A, Iv */
0af10c86 4401 val = insn_get(env, s, ot);
2c0262af
FB
4402 gen_op_movl_T1_im(val);
4403 gen_op(s, op, ot, OR_EAX);
4404 break;
4405 }
4406 }
4407 break;
4408
ec9d6075
FB
4409 case 0x82:
4410 if (CODE64(s))
4411 goto illegal_op;
2c0262af
FB
4412 case 0x80: /* GRP1 */
4413 case 0x81:
4414 case 0x83:
4415 {
4416 int val;
4417
4418 if ((b & 1) == 0)
4419 ot = OT_BYTE;
4420 else
14ce26e7 4421 ot = dflag + OT_WORD;
3b46e624 4422
0af10c86 4423 modrm = cpu_ldub_code(env, s->pc++);
2c0262af 4424 mod = (modrm >> 6) & 3;
14ce26e7 4425 rm = (modrm & 7) | REX_B(s);
2c0262af 4426 op = (modrm >> 3) & 7;
3b46e624 4427
2c0262af 4428 if (mod != 3) {
14ce26e7
FB
4429 if (b == 0x83)
4430 s->rip_offset = 1;
4431 else
4432 s->rip_offset = insn_const_size(ot);
0af10c86 4433 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
2c0262af
FB
4434 opreg = OR_TMP0;
4435 } else {
14ce26e7 4436 opreg = rm;
2c0262af
FB
4437 }
4438
4439 switch(b) {
4440 default:
4441 case 0x80:
4442 case 0x81:
d64477af 4443 case 0x82:
0af10c86 4444 val = insn_get(env, s, ot);
2c0262af
FB
4445 break;
4446 case 0x83:
0af10c86 4447 val = (int8_t)insn_get(env, s, OT_BYTE);
2c0262af
FB
4448 break;
4449 }
4450 gen_op_movl_T1_im(val);
4451 gen_op(s, op, ot, opreg);
4452 }
4453 break;
4454
4455 /**************************/
4456 /* inc, dec, and other misc arith */
4457 case 0x40 ... 0x47: /* inc Gv */
4458 ot = dflag ? OT_LONG : OT_WORD;
4459 gen_inc(s, ot, OR_EAX + (b & 7), 1);
4460 break;
4461 case 0x48 ... 0x4f: /* dec Gv */
4462 ot = dflag ? OT_LONG : OT_WORD;
4463 gen_inc(s, ot, OR_EAX + (b & 7), -1);
4464 break;
4465 case 0xf6: /* GRP3 */
4466 case 0xf7:
4467 if ((b & 1) == 0)
4468 ot = OT_BYTE;
4469 else
14ce26e7 4470 ot = dflag + OT_WORD;
2c0262af 4471
0af10c86 4472 modrm = cpu_ldub_code(env, s->pc++);
2c0262af 4473 mod = (modrm >> 6) & 3;
14ce26e7 4474 rm = (modrm & 7) | REX_B(s);
2c0262af
FB
4475 op = (modrm >> 3) & 7;
4476 if (mod != 3) {
14ce26e7
FB
4477 if (op == 0)
4478 s->rip_offset = insn_const_size(ot);
0af10c86 4479 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
57fec1fe 4480 gen_op_ld_T0_A0(ot + s->mem_index);
2c0262af 4481 } else {
57fec1fe 4482 gen_op_mov_TN_reg(ot, 0, rm);
2c0262af
FB
4483 }
4484
4485 switch(op) {
4486 case 0: /* test */
0af10c86 4487 val = insn_get(env, s, ot);
2c0262af
FB
4488 gen_op_movl_T1_im(val);
4489 gen_op_testl_T0_T1_cc();
3ca51d07 4490 set_cc_op(s, CC_OP_LOGICB + ot);
2c0262af
FB
4491 break;
4492 case 2: /* not */
b6abf97d 4493 tcg_gen_not_tl(cpu_T[0], cpu_T[0]);
2c0262af 4494 if (mod != 3) {
57fec1fe 4495 gen_op_st_T0_A0(ot + s->mem_index);
2c0262af 4496 } else {
57fec1fe 4497 gen_op_mov_reg_T0(ot, rm);
2c0262af
FB
4498 }
4499 break;
4500 case 3: /* neg */
b6abf97d 4501 tcg_gen_neg_tl(cpu_T[0], cpu_T[0]);
2c0262af 4502 if (mod != 3) {
57fec1fe 4503 gen_op_st_T0_A0(ot + s->mem_index);
2c0262af 4504 } else {
57fec1fe 4505 gen_op_mov_reg_T0(ot, rm);
2c0262af
FB
4506 }
4507 gen_op_update_neg_cc();
3ca51d07 4508 set_cc_op(s, CC_OP_SUBB + ot);
2c0262af
FB
4509 break;
4510 case 4: /* mul */
4511 switch(ot) {
4512 case OT_BYTE:
0211e5af
FB
4513 gen_op_mov_TN_reg(OT_BYTE, 1, R_EAX);
4514 tcg_gen_ext8u_tl(cpu_T[0], cpu_T[0]);
4515 tcg_gen_ext8u_tl(cpu_T[1], cpu_T[1]);
4516 /* XXX: use 32 bit mul which could be faster */
4517 tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
4518 gen_op_mov_reg_T0(OT_WORD, R_EAX);
4519 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
4520 tcg_gen_andi_tl(cpu_cc_src, cpu_T[0], 0xff00);
3ca51d07 4521 set_cc_op(s, CC_OP_MULB);
2c0262af
FB
4522 break;
4523 case OT_WORD:
0211e5af
FB
4524 gen_op_mov_TN_reg(OT_WORD, 1, R_EAX);
4525 tcg_gen_ext16u_tl(cpu_T[0], cpu_T[0]);
4526 tcg_gen_ext16u_tl(cpu_T[1], cpu_T[1]);
4527 /* XXX: use 32 bit mul which could be faster */
4528 tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
4529 gen_op_mov_reg_T0(OT_WORD, R_EAX);
4530 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
4531 tcg_gen_shri_tl(cpu_T[0], cpu_T[0], 16);
4532 gen_op_mov_reg_T0(OT_WORD, R_EDX);
4533 tcg_gen_mov_tl(cpu_cc_src, cpu_T[0]);
3ca51d07 4534 set_cc_op(s, CC_OP_MULW);
2c0262af
FB
4535 break;
4536 default:
4537 case OT_LONG:
0211e5af
FB
4538#ifdef TARGET_X86_64
4539 gen_op_mov_TN_reg(OT_LONG, 1, R_EAX);
4540 tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
4541 tcg_gen_ext32u_tl(cpu_T[1], cpu_T[1]);
4542 tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
4543 gen_op_mov_reg_T0(OT_LONG, R_EAX);
4544 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
4545 tcg_gen_shri_tl(cpu_T[0], cpu_T[0], 32);
4546 gen_op_mov_reg_T0(OT_LONG, R_EDX);
4547 tcg_gen_mov_tl(cpu_cc_src, cpu_T[0]);
4548#else
4549 {
a7812ae4
PB
4550 TCGv_i64 t0, t1;
4551 t0 = tcg_temp_new_i64();
4552 t1 = tcg_temp_new_i64();
0211e5af
FB
4553 gen_op_mov_TN_reg(OT_LONG, 1, R_EAX);
4554 tcg_gen_extu_i32_i64(t0, cpu_T[0]);
4555 tcg_gen_extu_i32_i64(t1, cpu_T[1]);
4556 tcg_gen_mul_i64(t0, t0, t1);
4557 tcg_gen_trunc_i64_i32(cpu_T[0], t0);
4558 gen_op_mov_reg_T0(OT_LONG, R_EAX);
4559 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
4560 tcg_gen_shri_i64(t0, t0, 32);
4561 tcg_gen_trunc_i64_i32(cpu_T[0], t0);
4562 gen_op_mov_reg_T0(OT_LONG, R_EDX);
4563 tcg_gen_mov_tl(cpu_cc_src, cpu_T[0]);
4564 }
4565#endif
3ca51d07 4566 set_cc_op(s, CC_OP_MULL);
2c0262af 4567 break;
14ce26e7
FB
4568#ifdef TARGET_X86_64
4569 case OT_QUAD:
7923057b 4570 gen_helper_mulq_EAX_T0(cpu_env, cpu_T[0]);
3ca51d07 4571 set_cc_op(s, CC_OP_MULQ);
14ce26e7
FB
4572 break;
4573#endif
2c0262af 4574 }
2c0262af
FB
4575 break;
4576 case 5: /* imul */
4577 switch(ot) {
4578 case OT_BYTE:
0211e5af
FB
4579 gen_op_mov_TN_reg(OT_BYTE, 1, R_EAX);
4580 tcg_gen_ext8s_tl(cpu_T[0], cpu_T[0]);
4581 tcg_gen_ext8s_tl(cpu_T[1], cpu_T[1]);
4582 /* XXX: use 32 bit mul which could be faster */
4583 tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
4584 gen_op_mov_reg_T0(OT_WORD, R_EAX);
4585 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
4586 tcg_gen_ext8s_tl(cpu_tmp0, cpu_T[0]);
4587 tcg_gen_sub_tl(cpu_cc_src, cpu_T[0], cpu_tmp0);
3ca51d07 4588 set_cc_op(s, CC_OP_MULB);
2c0262af
FB
4589 break;
4590 case OT_WORD:
0211e5af
FB
4591 gen_op_mov_TN_reg(OT_WORD, 1, R_EAX);
4592 tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
4593 tcg_gen_ext16s_tl(cpu_T[1], cpu_T[1]);
4594 /* XXX: use 32 bit mul which could be faster */
4595 tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
4596 gen_op_mov_reg_T0(OT_WORD, R_EAX);
4597 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
4598 tcg_gen_ext16s_tl(cpu_tmp0, cpu_T[0]);
4599 tcg_gen_sub_tl(cpu_cc_src, cpu_T[0], cpu_tmp0);
4600 tcg_gen_shri_tl(cpu_T[0], cpu_T[0], 16);
4601 gen_op_mov_reg_T0(OT_WORD, R_EDX);
3ca51d07 4602 set_cc_op(s, CC_OP_MULW);
2c0262af
FB
4603 break;
4604 default:
4605 case OT_LONG:
0211e5af
FB
4606#ifdef TARGET_X86_64
4607 gen_op_mov_TN_reg(OT_LONG, 1, R_EAX);
4608 tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
4609 tcg_gen_ext32s_tl(cpu_T[1], cpu_T[1]);
4610 tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
4611 gen_op_mov_reg_T0(OT_LONG, R_EAX);
4612 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
4613 tcg_gen_ext32s_tl(cpu_tmp0, cpu_T[0]);
4614 tcg_gen_sub_tl(cpu_cc_src, cpu_T[0], cpu_tmp0);
4615 tcg_gen_shri_tl(cpu_T[0], cpu_T[0], 32);
4616 gen_op_mov_reg_T0(OT_LONG, R_EDX);
4617#else
4618 {
a7812ae4
PB
4619 TCGv_i64 t0, t1;
4620 t0 = tcg_temp_new_i64();
4621 t1 = tcg_temp_new_i64();
0211e5af
FB
4622 gen_op_mov_TN_reg(OT_LONG, 1, R_EAX);
4623 tcg_gen_ext_i32_i64(t0, cpu_T[0]);
4624 tcg_gen_ext_i32_i64(t1, cpu_T[1]);
4625 tcg_gen_mul_i64(t0, t0, t1);
4626 tcg_gen_trunc_i64_i32(cpu_T[0], t0);
4627 gen_op_mov_reg_T0(OT_LONG, R_EAX);
4628 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
4629 tcg_gen_sari_tl(cpu_tmp0, cpu_T[0], 31);
4630 tcg_gen_shri_i64(t0, t0, 32);
4631 tcg_gen_trunc_i64_i32(cpu_T[0], t0);
4632 gen_op_mov_reg_T0(OT_LONG, R_EDX);
4633 tcg_gen_sub_tl(cpu_cc_src, cpu_T[0], cpu_tmp0);
4634 }
4635#endif
3ca51d07 4636 set_cc_op(s, CC_OP_MULL);
2c0262af 4637 break;
14ce26e7
FB
4638#ifdef TARGET_X86_64
4639 case OT_QUAD:
7923057b 4640 gen_helper_imulq_EAX_T0(cpu_env, cpu_T[0]);
3ca51d07 4641 set_cc_op(s, CC_OP_MULQ);
14ce26e7
FB
4642 break;
4643#endif
2c0262af 4644 }
2c0262af
FB
4645 break;
4646 case 6: /* div */
4647 switch(ot) {
4648 case OT_BYTE:
14ce26e7 4649 gen_jmp_im(pc_start - s->cs_base);
7923057b 4650 gen_helper_divb_AL(cpu_env, cpu_T[0]);
2c0262af
FB
4651 break;
4652 case OT_WORD:
14ce26e7 4653 gen_jmp_im(pc_start - s->cs_base);
7923057b 4654 gen_helper_divw_AX(cpu_env, cpu_T[0]);
2c0262af
FB
4655 break;
4656 default:
4657 case OT_LONG:
14ce26e7 4658 gen_jmp_im(pc_start - s->cs_base);
7923057b 4659 gen_helper_divl_EAX(cpu_env, cpu_T[0]);
14ce26e7
FB
4660 break;
4661#ifdef TARGET_X86_64
4662 case OT_QUAD:
4663 gen_jmp_im(pc_start - s->cs_base);
7923057b 4664 gen_helper_divq_EAX(cpu_env, cpu_T[0]);
2c0262af 4665 break;
14ce26e7 4666#endif
2c0262af
FB
4667 }
4668 break;
4669 case 7: /* idiv */
4670 switch(ot) {
4671 case OT_BYTE:
14ce26e7 4672 gen_jmp_im(pc_start - s->cs_base);
7923057b 4673 gen_helper_idivb_AL(cpu_env, cpu_T[0]);
2c0262af
FB
4674 break;
4675 case OT_WORD:
14ce26e7 4676 gen_jmp_im(pc_start - s->cs_base);
7923057b 4677 gen_helper_idivw_AX(cpu_env, cpu_T[0]);
2c0262af
FB
4678 break;
4679 default:
4680 case OT_LONG:
14ce26e7 4681 gen_jmp_im(pc_start - s->cs_base);
7923057b 4682 gen_helper_idivl_EAX(cpu_env, cpu_T[0]);
14ce26e7
FB
4683 break;
4684#ifdef TARGET_X86_64
4685 case OT_QUAD:
4686 gen_jmp_im(pc_start - s->cs_base);
7923057b 4687 gen_helper_idivq_EAX(cpu_env, cpu_T[0]);
2c0262af 4688 break;
14ce26e7 4689#endif
2c0262af
FB
4690 }
4691 break;
4692 default:
4693 goto illegal_op;
4694 }
4695 break;
4696
4697 case 0xfe: /* GRP4 */
4698 case 0xff: /* GRP5 */
4699 if ((b & 1) == 0)
4700 ot = OT_BYTE;
4701 else
14ce26e7 4702 ot = dflag + OT_WORD;
2c0262af 4703
0af10c86 4704 modrm = cpu_ldub_code(env, s->pc++);
2c0262af 4705 mod = (modrm >> 6) & 3;
14ce26e7 4706 rm = (modrm & 7) | REX_B(s);
2c0262af
FB
4707 op = (modrm >> 3) & 7;
4708 if (op >= 2 && b == 0xfe) {
4709 goto illegal_op;
4710 }
14ce26e7 4711 if (CODE64(s)) {
aba9d61e 4712 if (op == 2 || op == 4) {
14ce26e7
FB
4713 /* operand size for jumps is 64 bit */
4714 ot = OT_QUAD;
aba9d61e 4715 } else if (op == 3 || op == 5) {
41b1e61f 4716 ot = dflag ? OT_LONG + (rex_w == 1) : OT_WORD;
14ce26e7
FB
4717 } else if (op == 6) {
4718 /* default push size is 64 bit */
4719 ot = dflag ? OT_QUAD : OT_WORD;
4720 }
4721 }
2c0262af 4722 if (mod != 3) {
0af10c86 4723 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
2c0262af 4724 if (op >= 2 && op != 3 && op != 5)
57fec1fe 4725 gen_op_ld_T0_A0(ot + s->mem_index);
2c0262af 4726 } else {
57fec1fe 4727 gen_op_mov_TN_reg(ot, 0, rm);
2c0262af
FB
4728 }
4729
4730 switch(op) {
4731 case 0: /* inc Ev */
4732 if (mod != 3)
4733 opreg = OR_TMP0;
4734 else
4735 opreg = rm;
4736 gen_inc(s, ot, opreg, 1);
4737 break;
4738 case 1: /* dec Ev */
4739 if (mod != 3)
4740 opreg = OR_TMP0;
4741 else
4742 opreg = rm;
4743 gen_inc(s, ot, opreg, -1);
4744 break;
4745 case 2: /* call Ev */
4f31916f 4746 /* XXX: optimize if memory (no 'and' is necessary) */
2c0262af
FB
4747 if (s->dflag == 0)
4748 gen_op_andl_T0_ffff();
2c0262af 4749 next_eip = s->pc - s->cs_base;
1ef38687 4750 gen_movtl_T1_im(next_eip);
4f31916f
FB
4751 gen_push_T1(s);
4752 gen_op_jmp_T0();
2c0262af
FB
4753 gen_eob(s);
4754 break;
61382a50 4755 case 3: /* lcall Ev */
57fec1fe 4756 gen_op_ld_T1_A0(ot + s->mem_index);
aba9d61e 4757 gen_add_A0_im(s, 1 << (ot - OT_WORD + 1));
57fec1fe 4758 gen_op_ldu_T0_A0(OT_WORD + s->mem_index);
2c0262af
FB
4759 do_lcall:
4760 if (s->pe && !s->vm86) {
773cdfcc 4761 gen_update_cc_op(s);
14ce26e7 4762 gen_jmp_im(pc_start - s->cs_base);
b6abf97d 4763 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
2999a0b2
BS
4764 gen_helper_lcall_protected(cpu_env, cpu_tmp2_i32, cpu_T[1],
4765 tcg_const_i32(dflag),
a7812ae4 4766 tcg_const_i32(s->pc - pc_start));
2c0262af 4767 } else {
b6abf97d 4768 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
2999a0b2
BS
4769 gen_helper_lcall_real(cpu_env, cpu_tmp2_i32, cpu_T[1],
4770 tcg_const_i32(dflag),
a7812ae4 4771 tcg_const_i32(s->pc - s->cs_base));
2c0262af
FB
4772 }
4773 gen_eob(s);
4774 break;
4775 case 4: /* jmp Ev */
4776 if (s->dflag == 0)
4777 gen_op_andl_T0_ffff();
4778 gen_op_jmp_T0();
4779 gen_eob(s);
4780 break;
4781 case 5: /* ljmp Ev */
57fec1fe 4782 gen_op_ld_T1_A0(ot + s->mem_index);
aba9d61e 4783 gen_add_A0_im(s, 1 << (ot - OT_WORD + 1));
57fec1fe 4784 gen_op_ldu_T0_A0(OT_WORD + s->mem_index);
2c0262af
FB
4785 do_ljmp:
4786 if (s->pe && !s->vm86) {
773cdfcc 4787 gen_update_cc_op(s);
14ce26e7 4788 gen_jmp_im(pc_start - s->cs_base);
b6abf97d 4789 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
2999a0b2 4790 gen_helper_ljmp_protected(cpu_env, cpu_tmp2_i32, cpu_T[1],
a7812ae4 4791 tcg_const_i32(s->pc - pc_start));
2c0262af 4792 } else {
3bd7da9e 4793 gen_op_movl_seg_T0_vm(R_CS);
2c0262af
FB
4794 gen_op_movl_T0_T1();
4795 gen_op_jmp_T0();
4796 }
4797 gen_eob(s);
4798 break;
4799 case 6: /* push Ev */
4800 gen_push_T0(s);
4801 break;
4802 default:
4803 goto illegal_op;
4804 }
4805 break;
4806
4807 case 0x84: /* test Ev, Gv */
5fafdf24 4808 case 0x85:
2c0262af
FB
4809 if ((b & 1) == 0)
4810 ot = OT_BYTE;
4811 else
14ce26e7 4812 ot = dflag + OT_WORD;
2c0262af 4813
0af10c86 4814 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7 4815 reg = ((modrm >> 3) & 7) | rex_r;
3b46e624 4816
0af10c86 4817 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
57fec1fe 4818 gen_op_mov_TN_reg(ot, 1, reg);
2c0262af 4819 gen_op_testl_T0_T1_cc();
3ca51d07 4820 set_cc_op(s, CC_OP_LOGICB + ot);
2c0262af 4821 break;
3b46e624 4822
2c0262af
FB
4823 case 0xa8: /* test eAX, Iv */
4824 case 0xa9:
4825 if ((b & 1) == 0)
4826 ot = OT_BYTE;
4827 else
14ce26e7 4828 ot = dflag + OT_WORD;
0af10c86 4829 val = insn_get(env, s, ot);
2c0262af 4830
57fec1fe 4831 gen_op_mov_TN_reg(ot, 0, OR_EAX);
2c0262af
FB
4832 gen_op_movl_T1_im(val);
4833 gen_op_testl_T0_T1_cc();
3ca51d07 4834 set_cc_op(s, CC_OP_LOGICB + ot);
2c0262af 4835 break;
3b46e624 4836
2c0262af 4837 case 0x98: /* CWDE/CBW */
14ce26e7
FB
4838#ifdef TARGET_X86_64
4839 if (dflag == 2) {
e108dd01
FB
4840 gen_op_mov_TN_reg(OT_LONG, 0, R_EAX);
4841 tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
4842 gen_op_mov_reg_T0(OT_QUAD, R_EAX);
14ce26e7
FB
4843 } else
4844#endif
e108dd01
FB
4845 if (dflag == 1) {
4846 gen_op_mov_TN_reg(OT_WORD, 0, R_EAX);
4847 tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
4848 gen_op_mov_reg_T0(OT_LONG, R_EAX);
4849 } else {
4850 gen_op_mov_TN_reg(OT_BYTE, 0, R_EAX);
4851 tcg_gen_ext8s_tl(cpu_T[0], cpu_T[0]);
4852 gen_op_mov_reg_T0(OT_WORD, R_EAX);
4853 }
2c0262af
FB
4854 break;
4855 case 0x99: /* CDQ/CWD */
14ce26e7
FB
4856#ifdef TARGET_X86_64
4857 if (dflag == 2) {
e108dd01
FB
4858 gen_op_mov_TN_reg(OT_QUAD, 0, R_EAX);
4859 tcg_gen_sari_tl(cpu_T[0], cpu_T[0], 63);
4860 gen_op_mov_reg_T0(OT_QUAD, R_EDX);
14ce26e7
FB
4861 } else
4862#endif
e108dd01
FB
4863 if (dflag == 1) {
4864 gen_op_mov_TN_reg(OT_LONG, 0, R_EAX);
4865 tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
4866 tcg_gen_sari_tl(cpu_T[0], cpu_T[0], 31);
4867 gen_op_mov_reg_T0(OT_LONG, R_EDX);
4868 } else {
4869 gen_op_mov_TN_reg(OT_WORD, 0, R_EAX);
4870 tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
4871 tcg_gen_sari_tl(cpu_T[0], cpu_T[0], 15);
4872 gen_op_mov_reg_T0(OT_WORD, R_EDX);
4873 }
2c0262af
FB
4874 break;
4875 case 0x1af: /* imul Gv, Ev */
4876 case 0x69: /* imul Gv, Ev, I */
4877 case 0x6b:
14ce26e7 4878 ot = dflag + OT_WORD;
0af10c86 4879 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7
FB
4880 reg = ((modrm >> 3) & 7) | rex_r;
4881 if (b == 0x69)
4882 s->rip_offset = insn_const_size(ot);
4883 else if (b == 0x6b)
4884 s->rip_offset = 1;
0af10c86 4885 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
2c0262af 4886 if (b == 0x69) {
0af10c86 4887 val = insn_get(env, s, ot);
2c0262af
FB
4888 gen_op_movl_T1_im(val);
4889 } else if (b == 0x6b) {
0af10c86 4890 val = (int8_t)insn_get(env, s, OT_BYTE);
2c0262af
FB
4891 gen_op_movl_T1_im(val);
4892 } else {
57fec1fe 4893 gen_op_mov_TN_reg(ot, 1, reg);
2c0262af
FB
4894 }
4895
14ce26e7
FB
4896#ifdef TARGET_X86_64
4897 if (ot == OT_QUAD) {
7923057b 4898 gen_helper_imulq_T0_T1(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
14ce26e7
FB
4899 } else
4900#endif
2c0262af 4901 if (ot == OT_LONG) {
0211e5af
FB
4902#ifdef TARGET_X86_64
4903 tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
4904 tcg_gen_ext32s_tl(cpu_T[1], cpu_T[1]);
4905 tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
4906 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
4907 tcg_gen_ext32s_tl(cpu_tmp0, cpu_T[0]);
4908 tcg_gen_sub_tl(cpu_cc_src, cpu_T[0], cpu_tmp0);
4909#else
4910 {
a7812ae4
PB
4911 TCGv_i64 t0, t1;
4912 t0 = tcg_temp_new_i64();
4913 t1 = tcg_temp_new_i64();
0211e5af
FB
4914 tcg_gen_ext_i32_i64(t0, cpu_T[0]);
4915 tcg_gen_ext_i32_i64(t1, cpu_T[1]);
4916 tcg_gen_mul_i64(t0, t0, t1);
4917 tcg_gen_trunc_i64_i32(cpu_T[0], t0);
4918 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
4919 tcg_gen_sari_tl(cpu_tmp0, cpu_T[0], 31);
4920 tcg_gen_shri_i64(t0, t0, 32);
4921 tcg_gen_trunc_i64_i32(cpu_T[1], t0);
4922 tcg_gen_sub_tl(cpu_cc_src, cpu_T[1], cpu_tmp0);
4923 }
4924#endif
2c0262af 4925 } else {
0211e5af
FB
4926 tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
4927 tcg_gen_ext16s_tl(cpu_T[1], cpu_T[1]);
4928 /* XXX: use 32 bit mul which could be faster */
4929 tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
4930 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
4931 tcg_gen_ext16s_tl(cpu_tmp0, cpu_T[0]);
4932 tcg_gen_sub_tl(cpu_cc_src, cpu_T[0], cpu_tmp0);
2c0262af 4933 }
57fec1fe 4934 gen_op_mov_reg_T0(ot, reg);
3ca51d07 4935 set_cc_op(s, CC_OP_MULB + ot);
2c0262af
FB
4936 break;
4937 case 0x1c0:
4938 case 0x1c1: /* xadd Ev, Gv */
4939 if ((b & 1) == 0)
4940 ot = OT_BYTE;
4941 else
14ce26e7 4942 ot = dflag + OT_WORD;
0af10c86 4943 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7 4944 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af
FB
4945 mod = (modrm >> 6) & 3;
4946 if (mod == 3) {
14ce26e7 4947 rm = (modrm & 7) | REX_B(s);
57fec1fe
FB
4948 gen_op_mov_TN_reg(ot, 0, reg);
4949 gen_op_mov_TN_reg(ot, 1, rm);
2c0262af 4950 gen_op_addl_T0_T1();
57fec1fe
FB
4951 gen_op_mov_reg_T1(ot, reg);
4952 gen_op_mov_reg_T0(ot, rm);
2c0262af 4953 } else {
0af10c86 4954 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
57fec1fe
FB
4955 gen_op_mov_TN_reg(ot, 0, reg);
4956 gen_op_ld_T1_A0(ot + s->mem_index);
2c0262af 4957 gen_op_addl_T0_T1();
57fec1fe
FB
4958 gen_op_st_T0_A0(ot + s->mem_index);
4959 gen_op_mov_reg_T1(ot, reg);
2c0262af
FB
4960 }
4961 gen_op_update2_cc();
3ca51d07 4962 set_cc_op(s, CC_OP_ADDB + ot);
2c0262af
FB
4963 break;
4964 case 0x1b0:
4965 case 0x1b1: /* cmpxchg Ev, Gv */
cad3a37d 4966 {
1130328e 4967 int label1, label2;
1e4840bf 4968 TCGv t0, t1, t2, a0;
cad3a37d
FB
4969
4970 if ((b & 1) == 0)
4971 ot = OT_BYTE;
4972 else
4973 ot = dflag + OT_WORD;
0af10c86 4974 modrm = cpu_ldub_code(env, s->pc++);
cad3a37d
FB
4975 reg = ((modrm >> 3) & 7) | rex_r;
4976 mod = (modrm >> 6) & 3;
a7812ae4
PB
4977 t0 = tcg_temp_local_new();
4978 t1 = tcg_temp_local_new();
4979 t2 = tcg_temp_local_new();
4980 a0 = tcg_temp_local_new();
1e4840bf 4981 gen_op_mov_v_reg(ot, t1, reg);
cad3a37d
FB
4982 if (mod == 3) {
4983 rm = (modrm & 7) | REX_B(s);
1e4840bf 4984 gen_op_mov_v_reg(ot, t0, rm);
cad3a37d 4985 } else {
0af10c86 4986 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
1e4840bf
FB
4987 tcg_gen_mov_tl(a0, cpu_A0);
4988 gen_op_ld_v(ot + s->mem_index, t0, a0);
cad3a37d
FB
4989 rm = 0; /* avoid warning */
4990 }
4991 label1 = gen_new_label();
cc739bb0 4992 tcg_gen_sub_tl(t2, cpu_regs[R_EAX], t0);
1e4840bf
FB
4993 gen_extu(ot, t2);
4994 tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, label1);
f7e80adf 4995 label2 = gen_new_label();
cad3a37d 4996 if (mod == 3) {
1e4840bf 4997 gen_op_mov_reg_v(ot, R_EAX, t0);
1130328e
FB
4998 tcg_gen_br(label2);
4999 gen_set_label(label1);
1e4840bf 5000 gen_op_mov_reg_v(ot, rm, t1);
cad3a37d 5001 } else {
f7e80adf
AG
5002 /* perform no-op store cycle like physical cpu; must be
5003 before changing accumulator to ensure idempotency if
5004 the store faults and the instruction is restarted */
5005 gen_op_st_v(ot + s->mem_index, t0, a0);
1e4840bf 5006 gen_op_mov_reg_v(ot, R_EAX, t0);
f7e80adf 5007 tcg_gen_br(label2);
1130328e 5008 gen_set_label(label1);
1e4840bf 5009 gen_op_st_v(ot + s->mem_index, t1, a0);
cad3a37d 5010 }
f7e80adf 5011 gen_set_label(label2);
1e4840bf
FB
5012 tcg_gen_mov_tl(cpu_cc_src, t0);
5013 tcg_gen_mov_tl(cpu_cc_dst, t2);
3ca51d07 5014 set_cc_op(s, CC_OP_SUBB + ot);
1e4840bf
FB
5015 tcg_temp_free(t0);
5016 tcg_temp_free(t1);
5017 tcg_temp_free(t2);
5018 tcg_temp_free(a0);
2c0262af 5019 }
2c0262af
FB
5020 break;
5021 case 0x1c7: /* cmpxchg8b */
0af10c86 5022 modrm = cpu_ldub_code(env, s->pc++);
2c0262af 5023 mod = (modrm >> 6) & 3;
71c3558e 5024 if ((mod == 3) || ((modrm & 0x38) != 0x8))
2c0262af 5025 goto illegal_op;
1b9d9ebb
FB
5026#ifdef TARGET_X86_64
5027 if (dflag == 2) {
5028 if (!(s->cpuid_ext_features & CPUID_EXT_CX16))
5029 goto illegal_op;
5030 gen_jmp_im(pc_start - s->cs_base);
773cdfcc 5031 gen_update_cc_op(s);
0af10c86 5032 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
92fc4b58 5033 gen_helper_cmpxchg16b(cpu_env, cpu_A0);
1b9d9ebb
FB
5034 } else
5035#endif
5036 {
5037 if (!(s->cpuid_features & CPUID_CX8))
5038 goto illegal_op;
5039 gen_jmp_im(pc_start - s->cs_base);
773cdfcc 5040 gen_update_cc_op(s);
0af10c86 5041 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
92fc4b58 5042 gen_helper_cmpxchg8b(cpu_env, cpu_A0);
1b9d9ebb 5043 }
3ca51d07 5044 set_cc_op(s, CC_OP_EFLAGS);
2c0262af 5045 break;
3b46e624 5046
2c0262af
FB
5047 /**************************/
5048 /* push/pop */
5049 case 0x50 ... 0x57: /* push */
57fec1fe 5050 gen_op_mov_TN_reg(OT_LONG, 0, (b & 7) | REX_B(s));
2c0262af
FB
5051 gen_push_T0(s);
5052 break;
5053 case 0x58 ... 0x5f: /* pop */
14ce26e7
FB
5054 if (CODE64(s)) {
5055 ot = dflag ? OT_QUAD : OT_WORD;
5056 } else {
5057 ot = dflag + OT_WORD;
5058 }
2c0262af 5059 gen_pop_T0(s);
77729c24 5060 /* NOTE: order is important for pop %sp */
2c0262af 5061 gen_pop_update(s);
57fec1fe 5062 gen_op_mov_reg_T0(ot, (b & 7) | REX_B(s));
2c0262af
FB
5063 break;
5064 case 0x60: /* pusha */
14ce26e7
FB
5065 if (CODE64(s))
5066 goto illegal_op;
2c0262af
FB
5067 gen_pusha(s);
5068 break;
5069 case 0x61: /* popa */
14ce26e7
FB
5070 if (CODE64(s))
5071 goto illegal_op;
2c0262af
FB
5072 gen_popa(s);
5073 break;
5074 case 0x68: /* push Iv */
5075 case 0x6a:
14ce26e7
FB
5076 if (CODE64(s)) {
5077 ot = dflag ? OT_QUAD : OT_WORD;
5078 } else {
5079 ot = dflag + OT_WORD;
5080 }
2c0262af 5081 if (b == 0x68)
0af10c86 5082 val = insn_get(env, s, ot);
2c0262af 5083 else
0af10c86 5084 val = (int8_t)insn_get(env, s, OT_BYTE);
2c0262af
FB
5085 gen_op_movl_T0_im(val);
5086 gen_push_T0(s);
5087 break;
5088 case 0x8f: /* pop Ev */
14ce26e7
FB
5089 if (CODE64(s)) {
5090 ot = dflag ? OT_QUAD : OT_WORD;
5091 } else {
5092 ot = dflag + OT_WORD;
5093 }
0af10c86 5094 modrm = cpu_ldub_code(env, s->pc++);
77729c24 5095 mod = (modrm >> 6) & 3;
2c0262af 5096 gen_pop_T0(s);
77729c24
FB
5097 if (mod == 3) {
5098 /* NOTE: order is important for pop %sp */
5099 gen_pop_update(s);
14ce26e7 5100 rm = (modrm & 7) | REX_B(s);
57fec1fe 5101 gen_op_mov_reg_T0(ot, rm);
77729c24
FB
5102 } else {
5103 /* NOTE: order is important too for MMU exceptions */
14ce26e7 5104 s->popl_esp_hack = 1 << ot;
0af10c86 5105 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
77729c24
FB
5106 s->popl_esp_hack = 0;
5107 gen_pop_update(s);
5108 }
2c0262af
FB
5109 break;
5110 case 0xc8: /* enter */
5111 {
5112 int level;
0af10c86 5113 val = cpu_lduw_code(env, s->pc);
2c0262af 5114 s->pc += 2;
0af10c86 5115 level = cpu_ldub_code(env, s->pc++);
2c0262af
FB
5116 gen_enter(s, val, level);
5117 }
5118 break;
5119 case 0xc9: /* leave */
5120 /* XXX: exception not precise (ESP is updated before potential exception) */
14ce26e7 5121 if (CODE64(s)) {
57fec1fe
FB
5122 gen_op_mov_TN_reg(OT_QUAD, 0, R_EBP);
5123 gen_op_mov_reg_T0(OT_QUAD, R_ESP);
14ce26e7 5124 } else if (s->ss32) {
57fec1fe
FB
5125 gen_op_mov_TN_reg(OT_LONG, 0, R_EBP);
5126 gen_op_mov_reg_T0(OT_LONG, R_ESP);
2c0262af 5127 } else {
57fec1fe
FB
5128 gen_op_mov_TN_reg(OT_WORD, 0, R_EBP);
5129 gen_op_mov_reg_T0(OT_WORD, R_ESP);
2c0262af
FB
5130 }
5131 gen_pop_T0(s);
14ce26e7
FB
5132 if (CODE64(s)) {
5133 ot = dflag ? OT_QUAD : OT_WORD;
5134 } else {
5135 ot = dflag + OT_WORD;
5136 }
57fec1fe 5137 gen_op_mov_reg_T0(ot, R_EBP);
2c0262af
FB
5138 gen_pop_update(s);
5139 break;
5140 case 0x06: /* push es */
5141 case 0x0e: /* push cs */
5142 case 0x16: /* push ss */
5143 case 0x1e: /* push ds */
14ce26e7
FB
5144 if (CODE64(s))
5145 goto illegal_op;
2c0262af
FB
5146 gen_op_movl_T0_seg(b >> 3);
5147 gen_push_T0(s);
5148 break;
5149 case 0x1a0: /* push fs */
5150 case 0x1a8: /* push gs */
5151 gen_op_movl_T0_seg((b >> 3) & 7);
5152 gen_push_T0(s);
5153 break;
5154 case 0x07: /* pop es */
5155 case 0x17: /* pop ss */
5156 case 0x1f: /* pop ds */
14ce26e7
FB
5157 if (CODE64(s))
5158 goto illegal_op;
2c0262af
FB
5159 reg = b >> 3;
5160 gen_pop_T0(s);
5161 gen_movl_seg_T0(s, reg, pc_start - s->cs_base);
5162 gen_pop_update(s);
5163 if (reg == R_SS) {
a2cc3b24
FB
5164 /* if reg == SS, inhibit interrupts/trace. */
5165 /* If several instructions disable interrupts, only the
5166 _first_ does it */
5167 if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
f0967a1a 5168 gen_helper_set_inhibit_irq(cpu_env);
2c0262af
FB
5169 s->tf = 0;
5170 }
5171 if (s->is_jmp) {
14ce26e7 5172 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
5173 gen_eob(s);
5174 }
5175 break;
5176 case 0x1a1: /* pop fs */
5177 case 0x1a9: /* pop gs */
5178 gen_pop_T0(s);
5179 gen_movl_seg_T0(s, (b >> 3) & 7, pc_start - s->cs_base);
5180 gen_pop_update(s);
5181 if (s->is_jmp) {
14ce26e7 5182 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
5183 gen_eob(s);
5184 }
5185 break;
5186
5187 /**************************/
5188 /* mov */
5189 case 0x88:
5190 case 0x89: /* mov Gv, Ev */
5191 if ((b & 1) == 0)
5192 ot = OT_BYTE;
5193 else
14ce26e7 5194 ot = dflag + OT_WORD;
0af10c86 5195 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7 5196 reg = ((modrm >> 3) & 7) | rex_r;
3b46e624 5197
2c0262af 5198 /* generate a generic store */
0af10c86 5199 gen_ldst_modrm(env, s, modrm, ot, reg, 1);
2c0262af
FB
5200 break;
5201 case 0xc6:
5202 case 0xc7: /* mov Ev, Iv */
5203 if ((b & 1) == 0)
5204 ot = OT_BYTE;
5205 else
14ce26e7 5206 ot = dflag + OT_WORD;
0af10c86 5207 modrm = cpu_ldub_code(env, s->pc++);
2c0262af 5208 mod = (modrm >> 6) & 3;
14ce26e7
FB
5209 if (mod != 3) {
5210 s->rip_offset = insn_const_size(ot);
0af10c86 5211 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
14ce26e7 5212 }
0af10c86 5213 val = insn_get(env, s, ot);
2c0262af
FB
5214 gen_op_movl_T0_im(val);
5215 if (mod != 3)
57fec1fe 5216 gen_op_st_T0_A0(ot + s->mem_index);
2c0262af 5217 else
57fec1fe 5218 gen_op_mov_reg_T0(ot, (modrm & 7) | REX_B(s));
2c0262af
FB
5219 break;
5220 case 0x8a:
5221 case 0x8b: /* mov Ev, Gv */
5222 if ((b & 1) == 0)
5223 ot = OT_BYTE;
5224 else
14ce26e7 5225 ot = OT_WORD + dflag;
0af10c86 5226 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7 5227 reg = ((modrm >> 3) & 7) | rex_r;
3b46e624 5228
0af10c86 5229 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
57fec1fe 5230 gen_op_mov_reg_T0(ot, reg);
2c0262af
FB
5231 break;
5232 case 0x8e: /* mov seg, Gv */
0af10c86 5233 modrm = cpu_ldub_code(env, s->pc++);
2c0262af
FB
5234 reg = (modrm >> 3) & 7;
5235 if (reg >= 6 || reg == R_CS)
5236 goto illegal_op;
0af10c86 5237 gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
2c0262af
FB
5238 gen_movl_seg_T0(s, reg, pc_start - s->cs_base);
5239 if (reg == R_SS) {
5240 /* if reg == SS, inhibit interrupts/trace */
a2cc3b24
FB
5241 /* If several instructions disable interrupts, only the
5242 _first_ does it */
5243 if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
f0967a1a 5244 gen_helper_set_inhibit_irq(cpu_env);
2c0262af
FB
5245 s->tf = 0;
5246 }
5247 if (s->is_jmp) {
14ce26e7 5248 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
5249 gen_eob(s);
5250 }
5251 break;
5252 case 0x8c: /* mov Gv, seg */
0af10c86 5253 modrm = cpu_ldub_code(env, s->pc++);
2c0262af
FB
5254 reg = (modrm >> 3) & 7;
5255 mod = (modrm >> 6) & 3;
5256 if (reg >= 6)
5257 goto illegal_op;
5258 gen_op_movl_T0_seg(reg);
14ce26e7
FB
5259 if (mod == 3)
5260 ot = OT_WORD + dflag;
5261 else
5262 ot = OT_WORD;
0af10c86 5263 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
2c0262af
FB
5264 break;
5265
5266 case 0x1b6: /* movzbS Gv, Eb */
5267 case 0x1b7: /* movzwS Gv, Eb */
5268 case 0x1be: /* movsbS Gv, Eb */
5269 case 0x1bf: /* movswS Gv, Eb */
5270 {
5271 int d_ot;
5272 /* d_ot is the size of destination */
5273 d_ot = dflag + OT_WORD;
5274 /* ot is the size of source */
5275 ot = (b & 1) + OT_BYTE;
0af10c86 5276 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7 5277 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af 5278 mod = (modrm >> 6) & 3;
14ce26e7 5279 rm = (modrm & 7) | REX_B(s);
3b46e624 5280
2c0262af 5281 if (mod == 3) {
57fec1fe 5282 gen_op_mov_TN_reg(ot, 0, rm);
2c0262af
FB
5283 switch(ot | (b & 8)) {
5284 case OT_BYTE:
e108dd01 5285 tcg_gen_ext8u_tl(cpu_T[0], cpu_T[0]);
2c0262af
FB
5286 break;
5287 case OT_BYTE | 8:
e108dd01 5288 tcg_gen_ext8s_tl(cpu_T[0], cpu_T[0]);
2c0262af
FB
5289 break;
5290 case OT_WORD:
e108dd01 5291 tcg_gen_ext16u_tl(cpu_T[0], cpu_T[0]);
2c0262af
FB
5292 break;
5293 default:
5294 case OT_WORD | 8:
e108dd01 5295 tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
2c0262af
FB
5296 break;
5297 }
57fec1fe 5298 gen_op_mov_reg_T0(d_ot, reg);
2c0262af 5299 } else {
0af10c86 5300 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
2c0262af 5301 if (b & 8) {
57fec1fe 5302 gen_op_lds_T0_A0(ot + s->mem_index);
2c0262af 5303 } else {
57fec1fe 5304 gen_op_ldu_T0_A0(ot + s->mem_index);
2c0262af 5305 }
57fec1fe 5306 gen_op_mov_reg_T0(d_ot, reg);
2c0262af
FB
5307 }
5308 }
5309 break;
5310
5311 case 0x8d: /* lea */
14ce26e7 5312 ot = dflag + OT_WORD;
0af10c86 5313 modrm = cpu_ldub_code(env, s->pc++);
3a1d9b8b
FB
5314 mod = (modrm >> 6) & 3;
5315 if (mod == 3)
5316 goto illegal_op;
14ce26e7 5317 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af
FB
5318 /* we must ensure that no segment is added */
5319 s->override = -1;
5320 val = s->addseg;
5321 s->addseg = 0;
0af10c86 5322 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
2c0262af 5323 s->addseg = val;
57fec1fe 5324 gen_op_mov_reg_A0(ot - OT_WORD, reg);
2c0262af 5325 break;
3b46e624 5326
2c0262af
FB
5327 case 0xa0: /* mov EAX, Ov */
5328 case 0xa1:
5329 case 0xa2: /* mov Ov, EAX */
5330 case 0xa3:
2c0262af 5331 {
14ce26e7
FB
5332 target_ulong offset_addr;
5333
5334 if ((b & 1) == 0)
5335 ot = OT_BYTE;
5336 else
5337 ot = dflag + OT_WORD;
5338#ifdef TARGET_X86_64
8f091a59 5339 if (s->aflag == 2) {
0af10c86 5340 offset_addr = cpu_ldq_code(env, s->pc);
14ce26e7 5341 s->pc += 8;
57fec1fe 5342 gen_op_movq_A0_im(offset_addr);
5fafdf24 5343 } else
14ce26e7
FB
5344#endif
5345 {
5346 if (s->aflag) {
0af10c86 5347 offset_addr = insn_get(env, s, OT_LONG);
14ce26e7 5348 } else {
0af10c86 5349 offset_addr = insn_get(env, s, OT_WORD);
14ce26e7
FB
5350 }
5351 gen_op_movl_A0_im(offset_addr);
5352 }
664e0f19 5353 gen_add_A0_ds_seg(s);
14ce26e7 5354 if ((b & 2) == 0) {
57fec1fe
FB
5355 gen_op_ld_T0_A0(ot + s->mem_index);
5356 gen_op_mov_reg_T0(ot, R_EAX);
14ce26e7 5357 } else {
57fec1fe
FB
5358 gen_op_mov_TN_reg(ot, 0, R_EAX);
5359 gen_op_st_T0_A0(ot + s->mem_index);
2c0262af
FB
5360 }
5361 }
2c0262af
FB
5362 break;
5363 case 0xd7: /* xlat */
14ce26e7 5364#ifdef TARGET_X86_64
8f091a59 5365 if (s->aflag == 2) {
57fec1fe 5366 gen_op_movq_A0_reg(R_EBX);
bbf662ee
FB
5367 gen_op_mov_TN_reg(OT_QUAD, 0, R_EAX);
5368 tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xff);
5369 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_T[0]);
5fafdf24 5370 } else
14ce26e7
FB
5371#endif
5372 {
57fec1fe 5373 gen_op_movl_A0_reg(R_EBX);
bbf662ee
FB
5374 gen_op_mov_TN_reg(OT_LONG, 0, R_EAX);
5375 tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xff);
5376 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_T[0]);
14ce26e7
FB
5377 if (s->aflag == 0)
5378 gen_op_andl_A0_ffff();
bbf662ee
FB
5379 else
5380 tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffffffff);
14ce26e7 5381 }
664e0f19 5382 gen_add_A0_ds_seg(s);
57fec1fe
FB
5383 gen_op_ldu_T0_A0(OT_BYTE + s->mem_index);
5384 gen_op_mov_reg_T0(OT_BYTE, R_EAX);
2c0262af
FB
5385 break;
5386 case 0xb0 ... 0xb7: /* mov R, Ib */
0af10c86 5387 val = insn_get(env, s, OT_BYTE);
2c0262af 5388 gen_op_movl_T0_im(val);
57fec1fe 5389 gen_op_mov_reg_T0(OT_BYTE, (b & 7) | REX_B(s));
2c0262af
FB
5390 break;
5391 case 0xb8 ... 0xbf: /* mov R, Iv */
14ce26e7
FB
5392#ifdef TARGET_X86_64
5393 if (dflag == 2) {
5394 uint64_t tmp;
5395 /* 64 bit case */
0af10c86 5396 tmp = cpu_ldq_code(env, s->pc);
14ce26e7
FB
5397 s->pc += 8;
5398 reg = (b & 7) | REX_B(s);
5399 gen_movtl_T0_im(tmp);
57fec1fe 5400 gen_op_mov_reg_T0(OT_QUAD, reg);
5fafdf24 5401 } else
14ce26e7
FB
5402#endif
5403 {
5404 ot = dflag ? OT_LONG : OT_WORD;
0af10c86 5405 val = insn_get(env, s, ot);
14ce26e7
FB
5406 reg = (b & 7) | REX_B(s);
5407 gen_op_movl_T0_im(val);
57fec1fe 5408 gen_op_mov_reg_T0(ot, reg);
14ce26e7 5409 }
2c0262af
FB
5410 break;
5411
5412 case 0x91 ... 0x97: /* xchg R, EAX */
7418027e 5413 do_xchg_reg_eax:
14ce26e7
FB
5414 ot = dflag + OT_WORD;
5415 reg = (b & 7) | REX_B(s);
2c0262af
FB
5416 rm = R_EAX;
5417 goto do_xchg_reg;
5418 case 0x86:
5419 case 0x87: /* xchg Ev, Gv */
5420 if ((b & 1) == 0)
5421 ot = OT_BYTE;
5422 else
14ce26e7 5423 ot = dflag + OT_WORD;
0af10c86 5424 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7 5425 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af
FB
5426 mod = (modrm >> 6) & 3;
5427 if (mod == 3) {
14ce26e7 5428 rm = (modrm & 7) | REX_B(s);
2c0262af 5429 do_xchg_reg:
57fec1fe
FB
5430 gen_op_mov_TN_reg(ot, 0, reg);
5431 gen_op_mov_TN_reg(ot, 1, rm);
5432 gen_op_mov_reg_T0(ot, rm);
5433 gen_op_mov_reg_T1(ot, reg);
2c0262af 5434 } else {
0af10c86 5435 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
57fec1fe 5436 gen_op_mov_TN_reg(ot, 0, reg);
2c0262af
FB
5437 /* for xchg, lock is implicit */
5438 if (!(prefixes & PREFIX_LOCK))
a7812ae4 5439 gen_helper_lock();
57fec1fe
FB
5440 gen_op_ld_T1_A0(ot + s->mem_index);
5441 gen_op_st_T0_A0(ot + s->mem_index);
2c0262af 5442 if (!(prefixes & PREFIX_LOCK))
a7812ae4 5443 gen_helper_unlock();
57fec1fe 5444 gen_op_mov_reg_T1(ot, reg);
2c0262af
FB
5445 }
5446 break;
5447 case 0xc4: /* les Gv */
14ce26e7
FB
5448 if (CODE64(s))
5449 goto illegal_op;
2c0262af
FB
5450 op = R_ES;
5451 goto do_lxx;
5452 case 0xc5: /* lds Gv */
14ce26e7
FB
5453 if (CODE64(s))
5454 goto illegal_op;
2c0262af
FB
5455 op = R_DS;
5456 goto do_lxx;
5457 case 0x1b2: /* lss Gv */
5458 op = R_SS;
5459 goto do_lxx;
5460 case 0x1b4: /* lfs Gv */
5461 op = R_FS;
5462 goto do_lxx;
5463 case 0x1b5: /* lgs Gv */
5464 op = R_GS;
5465 do_lxx:
5466 ot = dflag ? OT_LONG : OT_WORD;
0af10c86 5467 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7 5468 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af
FB
5469 mod = (modrm >> 6) & 3;
5470 if (mod == 3)
5471 goto illegal_op;
0af10c86 5472 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
57fec1fe 5473 gen_op_ld_T1_A0(ot + s->mem_index);
aba9d61e 5474 gen_add_A0_im(s, 1 << (ot - OT_WORD + 1));
2c0262af 5475 /* load the segment first to handle exceptions properly */
57fec1fe 5476 gen_op_ldu_T0_A0(OT_WORD + s->mem_index);
2c0262af
FB
5477 gen_movl_seg_T0(s, op, pc_start - s->cs_base);
5478 /* then put the data */
57fec1fe 5479 gen_op_mov_reg_T1(ot, reg);
2c0262af 5480 if (s->is_jmp) {
14ce26e7 5481 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
5482 gen_eob(s);
5483 }
5484 break;
3b46e624 5485
2c0262af
FB
5486 /************************/
5487 /* shifts */
5488 case 0xc0:
5489 case 0xc1:
5490 /* shift Ev,Ib */
5491 shift = 2;
5492 grp2:
5493 {
5494 if ((b & 1) == 0)
5495 ot = OT_BYTE;
5496 else
14ce26e7 5497 ot = dflag + OT_WORD;
3b46e624 5498
0af10c86 5499 modrm = cpu_ldub_code(env, s->pc++);
2c0262af 5500 mod = (modrm >> 6) & 3;
2c0262af 5501 op = (modrm >> 3) & 7;
3b46e624 5502
2c0262af 5503 if (mod != 3) {
14ce26e7
FB
5504 if (shift == 2) {
5505 s->rip_offset = 1;
5506 }
0af10c86 5507 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
2c0262af
FB
5508 opreg = OR_TMP0;
5509 } else {
14ce26e7 5510 opreg = (modrm & 7) | REX_B(s);
2c0262af
FB
5511 }
5512
5513 /* simpler op */
5514 if (shift == 0) {
5515 gen_shift(s, op, ot, opreg, OR_ECX);
5516 } else {
5517 if (shift == 2) {
0af10c86 5518 shift = cpu_ldub_code(env, s->pc++);
2c0262af
FB
5519 }
5520 gen_shifti(s, op, ot, opreg, shift);
5521 }
5522 }
5523 break;
5524 case 0xd0:
5525 case 0xd1:
5526 /* shift Ev,1 */
5527 shift = 1;
5528 goto grp2;
5529 case 0xd2:
5530 case 0xd3:
5531 /* shift Ev,cl */
5532 shift = 0;
5533 goto grp2;
5534
5535 case 0x1a4: /* shld imm */
5536 op = 0;
5537 shift = 1;
5538 goto do_shiftd;
5539 case 0x1a5: /* shld cl */
5540 op = 0;
5541 shift = 0;
5542 goto do_shiftd;
5543 case 0x1ac: /* shrd imm */
5544 op = 1;
5545 shift = 1;
5546 goto do_shiftd;
5547 case 0x1ad: /* shrd cl */
5548 op = 1;
5549 shift = 0;
5550 do_shiftd:
14ce26e7 5551 ot = dflag + OT_WORD;
0af10c86 5552 modrm = cpu_ldub_code(env, s->pc++);
2c0262af 5553 mod = (modrm >> 6) & 3;
14ce26e7
FB
5554 rm = (modrm & 7) | REX_B(s);
5555 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af 5556 if (mod != 3) {
0af10c86 5557 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
b6abf97d 5558 opreg = OR_TMP0;
2c0262af 5559 } else {
b6abf97d 5560 opreg = rm;
2c0262af 5561 }
57fec1fe 5562 gen_op_mov_TN_reg(ot, 1, reg);
3b46e624 5563
2c0262af 5564 if (shift) {
0af10c86 5565 val = cpu_ldub_code(env, s->pc++);
b6abf97d 5566 tcg_gen_movi_tl(cpu_T3, val);
2c0262af 5567 } else {
cc739bb0 5568 tcg_gen_mov_tl(cpu_T3, cpu_regs[R_ECX]);
2c0262af 5569 }
b6abf97d 5570 gen_shiftd_rm_T1_T3(s, ot, opreg, op);
2c0262af
FB
5571 break;
5572
5573 /************************/
5574 /* floats */
5fafdf24 5575 case 0xd8 ... 0xdf:
7eee2a50
FB
5576 if (s->flags & (HF_EM_MASK | HF_TS_MASK)) {
5577 /* if CR0.EM or CR0.TS are set, generate an FPU exception */
5578 /* XXX: what to do if illegal op ? */
5579 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
5580 break;
5581 }
0af10c86 5582 modrm = cpu_ldub_code(env, s->pc++);
2c0262af
FB
5583 mod = (modrm >> 6) & 3;
5584 rm = modrm & 7;
5585 op = ((b & 7) << 3) | ((modrm >> 3) & 7);
2c0262af
FB
5586 if (mod != 3) {
5587 /* memory op */
0af10c86 5588 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
2c0262af
FB
5589 switch(op) {
5590 case 0x00 ... 0x07: /* fxxxs */
5591 case 0x10 ... 0x17: /* fixxxl */
5592 case 0x20 ... 0x27: /* fxxxl */
5593 case 0x30 ... 0x37: /* fixxx */
5594 {
5595 int op1;
5596 op1 = op & 7;
5597
5598 switch(op >> 4) {
5599 case 0:
ba7cd150 5600 gen_op_ld_T0_A0(OT_LONG + s->mem_index);
b6abf97d 5601 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
d3eb5eae 5602 gen_helper_flds_FT0(cpu_env, cpu_tmp2_i32);
2c0262af
FB
5603 break;
5604 case 1:
ba7cd150 5605 gen_op_ld_T0_A0(OT_LONG + s->mem_index);
b6abf97d 5606 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
d3eb5eae 5607 gen_helper_fildl_FT0(cpu_env, cpu_tmp2_i32);
2c0262af
FB
5608 break;
5609 case 2:
b6abf97d 5610 tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_A0,
19e6c4b8 5611 (s->mem_index >> 2) - 1);
d3eb5eae 5612 gen_helper_fldl_FT0(cpu_env, cpu_tmp1_i64);
2c0262af
FB
5613 break;
5614 case 3:
5615 default:
ba7cd150 5616 gen_op_lds_T0_A0(OT_WORD + s->mem_index);
b6abf97d 5617 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
d3eb5eae 5618 gen_helper_fildl_FT0(cpu_env, cpu_tmp2_i32);
2c0262af
FB
5619 break;
5620 }
3b46e624 5621
a7812ae4 5622 gen_helper_fp_arith_ST0_FT0(op1);
2c0262af
FB
5623 if (op1 == 3) {
5624 /* fcomp needs pop */
d3eb5eae 5625 gen_helper_fpop(cpu_env);
2c0262af
FB
5626 }
5627 }
5628 break;
5629 case 0x08: /* flds */
5630 case 0x0a: /* fsts */
5631 case 0x0b: /* fstps */
465e9838
FB
5632 case 0x18 ... 0x1b: /* fildl, fisttpl, fistl, fistpl */
5633 case 0x28 ... 0x2b: /* fldl, fisttpll, fstl, fstpl */
5634 case 0x38 ... 0x3b: /* filds, fisttps, fists, fistps */
2c0262af
FB
5635 switch(op & 7) {
5636 case 0:
5637 switch(op >> 4) {
5638 case 0:
ba7cd150 5639 gen_op_ld_T0_A0(OT_LONG + s->mem_index);
b6abf97d 5640 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
d3eb5eae 5641 gen_helper_flds_ST0(cpu_env, cpu_tmp2_i32);
2c0262af
FB
5642 break;
5643 case 1:
ba7cd150 5644 gen_op_ld_T0_A0(OT_LONG + s->mem_index);
b6abf97d 5645 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
d3eb5eae 5646 gen_helper_fildl_ST0(cpu_env, cpu_tmp2_i32);
2c0262af
FB
5647 break;
5648 case 2:
b6abf97d 5649 tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_A0,
19e6c4b8 5650 (s->mem_index >> 2) - 1);
d3eb5eae 5651 gen_helper_fldl_ST0(cpu_env, cpu_tmp1_i64);
2c0262af
FB
5652 break;
5653 case 3:
5654 default:
ba7cd150 5655 gen_op_lds_T0_A0(OT_WORD + s->mem_index);
b6abf97d 5656 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
d3eb5eae 5657 gen_helper_fildl_ST0(cpu_env, cpu_tmp2_i32);
2c0262af
FB
5658 break;
5659 }
5660 break;
465e9838 5661 case 1:
19e6c4b8 5662 /* XXX: the corresponding CPUID bit must be tested ! */
465e9838
FB
5663 switch(op >> 4) {
5664 case 1:
d3eb5eae 5665 gen_helper_fisttl_ST0(cpu_tmp2_i32, cpu_env);
b6abf97d 5666 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
ba7cd150 5667 gen_op_st_T0_A0(OT_LONG + s->mem_index);
465e9838
FB
5668 break;
5669 case 2:
d3eb5eae 5670 gen_helper_fisttll_ST0(cpu_tmp1_i64, cpu_env);
b6abf97d 5671 tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_A0,
19e6c4b8 5672 (s->mem_index >> 2) - 1);
465e9838
FB
5673 break;
5674 case 3:
5675 default:
d3eb5eae 5676 gen_helper_fistt_ST0(cpu_tmp2_i32, cpu_env);
b6abf97d 5677 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
ba7cd150 5678 gen_op_st_T0_A0(OT_WORD + s->mem_index);
19e6c4b8 5679 break;
465e9838 5680 }
d3eb5eae 5681 gen_helper_fpop(cpu_env);
465e9838 5682 break;
2c0262af
FB
5683 default:
5684 switch(op >> 4) {
5685 case 0:
d3eb5eae 5686 gen_helper_fsts_ST0(cpu_tmp2_i32, cpu_env);
b6abf97d 5687 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
ba7cd150 5688 gen_op_st_T0_A0(OT_LONG + s->mem_index);
2c0262af
FB
5689 break;
5690 case 1:
d3eb5eae 5691 gen_helper_fistl_ST0(cpu_tmp2_i32, cpu_env);
b6abf97d 5692 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
ba7cd150 5693 gen_op_st_T0_A0(OT_LONG + s->mem_index);
2c0262af
FB
5694 break;
5695 case 2:
d3eb5eae 5696 gen_helper_fstl_ST0(cpu_tmp1_i64, cpu_env);
b6abf97d 5697 tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_A0,
19e6c4b8 5698 (s->mem_index >> 2) - 1);
2c0262af
FB
5699 break;
5700 case 3:
5701 default:
d3eb5eae 5702 gen_helper_fist_ST0(cpu_tmp2_i32, cpu_env);
b6abf97d 5703 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
ba7cd150 5704 gen_op_st_T0_A0(OT_WORD + s->mem_index);
2c0262af
FB
5705 break;
5706 }
5707 if ((op & 7) == 3)
d3eb5eae 5708 gen_helper_fpop(cpu_env);
2c0262af
FB
5709 break;
5710 }
5711 break;
5712 case 0x0c: /* fldenv mem */
773cdfcc 5713 gen_update_cc_op(s);
19e6c4b8 5714 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae 5715 gen_helper_fldenv(cpu_env, cpu_A0, tcg_const_i32(s->dflag));
2c0262af
FB
5716 break;
5717 case 0x0d: /* fldcw mem */
19e6c4b8 5718 gen_op_ld_T0_A0(OT_WORD + s->mem_index);
b6abf97d 5719 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
d3eb5eae 5720 gen_helper_fldcw(cpu_env, cpu_tmp2_i32);
2c0262af
FB
5721 break;
5722 case 0x0e: /* fnstenv mem */
773cdfcc 5723 gen_update_cc_op(s);
19e6c4b8 5724 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae 5725 gen_helper_fstenv(cpu_env, cpu_A0, tcg_const_i32(s->dflag));
2c0262af
FB
5726 break;
5727 case 0x0f: /* fnstcw mem */
d3eb5eae 5728 gen_helper_fnstcw(cpu_tmp2_i32, cpu_env);
b6abf97d 5729 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
19e6c4b8 5730 gen_op_st_T0_A0(OT_WORD + s->mem_index);
2c0262af
FB
5731 break;
5732 case 0x1d: /* fldt mem */
773cdfcc 5733 gen_update_cc_op(s);
19e6c4b8 5734 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae 5735 gen_helper_fldt_ST0(cpu_env, cpu_A0);
2c0262af
FB
5736 break;
5737 case 0x1f: /* fstpt mem */
773cdfcc 5738 gen_update_cc_op(s);
19e6c4b8 5739 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae
BS
5740 gen_helper_fstt_ST0(cpu_env, cpu_A0);
5741 gen_helper_fpop(cpu_env);
2c0262af
FB
5742 break;
5743 case 0x2c: /* frstor mem */
773cdfcc 5744 gen_update_cc_op(s);
19e6c4b8 5745 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae 5746 gen_helper_frstor(cpu_env, cpu_A0, tcg_const_i32(s->dflag));
2c0262af
FB
5747 break;
5748 case 0x2e: /* fnsave mem */
773cdfcc 5749 gen_update_cc_op(s);
19e6c4b8 5750 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae 5751 gen_helper_fsave(cpu_env, cpu_A0, tcg_const_i32(s->dflag));
2c0262af
FB
5752 break;
5753 case 0x2f: /* fnstsw mem */
d3eb5eae 5754 gen_helper_fnstsw(cpu_tmp2_i32, cpu_env);
b6abf97d 5755 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
19e6c4b8 5756 gen_op_st_T0_A0(OT_WORD + s->mem_index);
2c0262af
FB
5757 break;
5758 case 0x3c: /* fbld */
773cdfcc 5759 gen_update_cc_op(s);
19e6c4b8 5760 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae 5761 gen_helper_fbld_ST0(cpu_env, cpu_A0);
2c0262af
FB
5762 break;
5763 case 0x3e: /* fbstp */
773cdfcc 5764 gen_update_cc_op(s);
19e6c4b8 5765 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae
BS
5766 gen_helper_fbst_ST0(cpu_env, cpu_A0);
5767 gen_helper_fpop(cpu_env);
2c0262af
FB
5768 break;
5769 case 0x3d: /* fildll */
b6abf97d 5770 tcg_gen_qemu_ld64(cpu_tmp1_i64, cpu_A0,
19e6c4b8 5771 (s->mem_index >> 2) - 1);
d3eb5eae 5772 gen_helper_fildll_ST0(cpu_env, cpu_tmp1_i64);
2c0262af
FB
5773 break;
5774 case 0x3f: /* fistpll */
d3eb5eae 5775 gen_helper_fistll_ST0(cpu_tmp1_i64, cpu_env);
b6abf97d 5776 tcg_gen_qemu_st64(cpu_tmp1_i64, cpu_A0,
19e6c4b8 5777 (s->mem_index >> 2) - 1);
d3eb5eae 5778 gen_helper_fpop(cpu_env);
2c0262af
FB
5779 break;
5780 default:
5781 goto illegal_op;
5782 }
5783 } else {
5784 /* register float ops */
5785 opreg = rm;
5786
5787 switch(op) {
5788 case 0x08: /* fld sti */
d3eb5eae
BS
5789 gen_helper_fpush(cpu_env);
5790 gen_helper_fmov_ST0_STN(cpu_env,
5791 tcg_const_i32((opreg + 1) & 7));
2c0262af
FB
5792 break;
5793 case 0x09: /* fxchg sti */
c169c906
FB
5794 case 0x29: /* fxchg4 sti, undocumented op */
5795 case 0x39: /* fxchg7 sti, undocumented op */
d3eb5eae 5796 gen_helper_fxchg_ST0_STN(cpu_env, tcg_const_i32(opreg));
2c0262af
FB
5797 break;
5798 case 0x0a: /* grp d9/2 */
5799 switch(rm) {
5800 case 0: /* fnop */
023fe10d 5801 /* check exceptions (FreeBSD FPU probe) */
773cdfcc 5802 gen_update_cc_op(s);
14ce26e7 5803 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae 5804 gen_helper_fwait(cpu_env);
2c0262af
FB
5805 break;
5806 default:
5807 goto illegal_op;
5808 }
5809 break;
5810 case 0x0c: /* grp d9/4 */
5811 switch(rm) {
5812 case 0: /* fchs */
d3eb5eae 5813 gen_helper_fchs_ST0(cpu_env);
2c0262af
FB
5814 break;
5815 case 1: /* fabs */
d3eb5eae 5816 gen_helper_fabs_ST0(cpu_env);
2c0262af
FB
5817 break;
5818 case 4: /* ftst */
d3eb5eae
BS
5819 gen_helper_fldz_FT0(cpu_env);
5820 gen_helper_fcom_ST0_FT0(cpu_env);
2c0262af
FB
5821 break;
5822 case 5: /* fxam */
d3eb5eae 5823 gen_helper_fxam_ST0(cpu_env);
2c0262af
FB
5824 break;
5825 default:
5826 goto illegal_op;
5827 }
5828 break;
5829 case 0x0d: /* grp d9/5 */
5830 {
5831 switch(rm) {
5832 case 0:
d3eb5eae
BS
5833 gen_helper_fpush(cpu_env);
5834 gen_helper_fld1_ST0(cpu_env);
2c0262af
FB
5835 break;
5836 case 1:
d3eb5eae
BS
5837 gen_helper_fpush(cpu_env);
5838 gen_helper_fldl2t_ST0(cpu_env);
2c0262af
FB
5839 break;
5840 case 2:
d3eb5eae
BS
5841 gen_helper_fpush(cpu_env);
5842 gen_helper_fldl2e_ST0(cpu_env);
2c0262af
FB
5843 break;
5844 case 3:
d3eb5eae
BS
5845 gen_helper_fpush(cpu_env);
5846 gen_helper_fldpi_ST0(cpu_env);
2c0262af
FB
5847 break;
5848 case 4:
d3eb5eae
BS
5849 gen_helper_fpush(cpu_env);
5850 gen_helper_fldlg2_ST0(cpu_env);
2c0262af
FB
5851 break;
5852 case 5:
d3eb5eae
BS
5853 gen_helper_fpush(cpu_env);
5854 gen_helper_fldln2_ST0(cpu_env);
2c0262af
FB
5855 break;
5856 case 6:
d3eb5eae
BS
5857 gen_helper_fpush(cpu_env);
5858 gen_helper_fldz_ST0(cpu_env);
2c0262af
FB
5859 break;
5860 default:
5861 goto illegal_op;
5862 }
5863 }
5864 break;
5865 case 0x0e: /* grp d9/6 */
5866 switch(rm) {
5867 case 0: /* f2xm1 */
d3eb5eae 5868 gen_helper_f2xm1(cpu_env);
2c0262af
FB
5869 break;
5870 case 1: /* fyl2x */
d3eb5eae 5871 gen_helper_fyl2x(cpu_env);
2c0262af
FB
5872 break;
5873 case 2: /* fptan */
d3eb5eae 5874 gen_helper_fptan(cpu_env);
2c0262af
FB
5875 break;
5876 case 3: /* fpatan */
d3eb5eae 5877 gen_helper_fpatan(cpu_env);
2c0262af
FB
5878 break;
5879 case 4: /* fxtract */
d3eb5eae 5880 gen_helper_fxtract(cpu_env);
2c0262af
FB
5881 break;
5882 case 5: /* fprem1 */
d3eb5eae 5883 gen_helper_fprem1(cpu_env);
2c0262af
FB
5884 break;
5885 case 6: /* fdecstp */
d3eb5eae 5886 gen_helper_fdecstp(cpu_env);
2c0262af
FB
5887 break;
5888 default:
5889 case 7: /* fincstp */
d3eb5eae 5890 gen_helper_fincstp(cpu_env);
2c0262af
FB
5891 break;
5892 }
5893 break;
5894 case 0x0f: /* grp d9/7 */
5895 switch(rm) {
5896 case 0: /* fprem */
d3eb5eae 5897 gen_helper_fprem(cpu_env);
2c0262af
FB
5898 break;
5899 case 1: /* fyl2xp1 */
d3eb5eae 5900 gen_helper_fyl2xp1(cpu_env);
2c0262af
FB
5901 break;
5902 case 2: /* fsqrt */
d3eb5eae 5903 gen_helper_fsqrt(cpu_env);
2c0262af
FB
5904 break;
5905 case 3: /* fsincos */
d3eb5eae 5906 gen_helper_fsincos(cpu_env);
2c0262af
FB
5907 break;
5908 case 5: /* fscale */
d3eb5eae 5909 gen_helper_fscale(cpu_env);
2c0262af
FB
5910 break;
5911 case 4: /* frndint */
d3eb5eae 5912 gen_helper_frndint(cpu_env);
2c0262af
FB
5913 break;
5914 case 6: /* fsin */
d3eb5eae 5915 gen_helper_fsin(cpu_env);
2c0262af
FB
5916 break;
5917 default:
5918 case 7: /* fcos */
d3eb5eae 5919 gen_helper_fcos(cpu_env);
2c0262af
FB
5920 break;
5921 }
5922 break;
5923 case 0x00: case 0x01: case 0x04 ... 0x07: /* fxxx st, sti */
5924 case 0x20: case 0x21: case 0x24 ... 0x27: /* fxxx sti, st */
5925 case 0x30: case 0x31: case 0x34 ... 0x37: /* fxxxp sti, st */
5926 {
5927 int op1;
3b46e624 5928
2c0262af
FB
5929 op1 = op & 7;
5930 if (op >= 0x20) {
a7812ae4 5931 gen_helper_fp_arith_STN_ST0(op1, opreg);
2c0262af 5932 if (op >= 0x30)
d3eb5eae 5933 gen_helper_fpop(cpu_env);
2c0262af 5934 } else {
d3eb5eae 5935 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
a7812ae4 5936 gen_helper_fp_arith_ST0_FT0(op1);
2c0262af
FB
5937 }
5938 }
5939 break;
5940 case 0x02: /* fcom */
c169c906 5941 case 0x22: /* fcom2, undocumented op */
d3eb5eae
BS
5942 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
5943 gen_helper_fcom_ST0_FT0(cpu_env);
2c0262af
FB
5944 break;
5945 case 0x03: /* fcomp */
c169c906
FB
5946 case 0x23: /* fcomp3, undocumented op */
5947 case 0x32: /* fcomp5, undocumented op */
d3eb5eae
BS
5948 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
5949 gen_helper_fcom_ST0_FT0(cpu_env);
5950 gen_helper_fpop(cpu_env);
2c0262af
FB
5951 break;
5952 case 0x15: /* da/5 */
5953 switch(rm) {
5954 case 1: /* fucompp */
d3eb5eae
BS
5955 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(1));
5956 gen_helper_fucom_ST0_FT0(cpu_env);
5957 gen_helper_fpop(cpu_env);
5958 gen_helper_fpop(cpu_env);
2c0262af
FB
5959 break;
5960 default:
5961 goto illegal_op;
5962 }
5963 break;
5964 case 0x1c:
5965 switch(rm) {
5966 case 0: /* feni (287 only, just do nop here) */
5967 break;
5968 case 1: /* fdisi (287 only, just do nop here) */
5969 break;
5970 case 2: /* fclex */
d3eb5eae 5971 gen_helper_fclex(cpu_env);
2c0262af
FB
5972 break;
5973 case 3: /* fninit */
d3eb5eae 5974 gen_helper_fninit(cpu_env);
2c0262af
FB
5975 break;
5976 case 4: /* fsetpm (287 only, just do nop here) */
5977 break;
5978 default:
5979 goto illegal_op;
5980 }
5981 break;
5982 case 0x1d: /* fucomi */
773cdfcc 5983 gen_update_cc_op(s);
d3eb5eae
BS
5984 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
5985 gen_helper_fucomi_ST0_FT0(cpu_env);
3ca51d07 5986 set_cc_op(s, CC_OP_EFLAGS);
2c0262af
FB
5987 break;
5988 case 0x1e: /* fcomi */
773cdfcc 5989 gen_update_cc_op(s);
d3eb5eae
BS
5990 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
5991 gen_helper_fcomi_ST0_FT0(cpu_env);
3ca51d07 5992 set_cc_op(s, CC_OP_EFLAGS);
2c0262af 5993 break;
658c8bda 5994 case 0x28: /* ffree sti */
d3eb5eae 5995 gen_helper_ffree_STN(cpu_env, tcg_const_i32(opreg));
5fafdf24 5996 break;
2c0262af 5997 case 0x2a: /* fst sti */
d3eb5eae 5998 gen_helper_fmov_STN_ST0(cpu_env, tcg_const_i32(opreg));
2c0262af
FB
5999 break;
6000 case 0x2b: /* fstp sti */
c169c906
FB
6001 case 0x0b: /* fstp1 sti, undocumented op */
6002 case 0x3a: /* fstp8 sti, undocumented op */
6003 case 0x3b: /* fstp9 sti, undocumented op */
d3eb5eae
BS
6004 gen_helper_fmov_STN_ST0(cpu_env, tcg_const_i32(opreg));
6005 gen_helper_fpop(cpu_env);
2c0262af
FB
6006 break;
6007 case 0x2c: /* fucom st(i) */
d3eb5eae
BS
6008 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6009 gen_helper_fucom_ST0_FT0(cpu_env);
2c0262af
FB
6010 break;
6011 case 0x2d: /* fucomp st(i) */
d3eb5eae
BS
6012 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6013 gen_helper_fucom_ST0_FT0(cpu_env);
6014 gen_helper_fpop(cpu_env);
2c0262af
FB
6015 break;
6016 case 0x33: /* de/3 */
6017 switch(rm) {
6018 case 1: /* fcompp */
d3eb5eae
BS
6019 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(1));
6020 gen_helper_fcom_ST0_FT0(cpu_env);
6021 gen_helper_fpop(cpu_env);
6022 gen_helper_fpop(cpu_env);
2c0262af
FB
6023 break;
6024 default:
6025 goto illegal_op;
6026 }
6027 break;
c169c906 6028 case 0x38: /* ffreep sti, undocumented op */
d3eb5eae
BS
6029 gen_helper_ffree_STN(cpu_env, tcg_const_i32(opreg));
6030 gen_helper_fpop(cpu_env);
c169c906 6031 break;
2c0262af
FB
6032 case 0x3c: /* df/4 */
6033 switch(rm) {
6034 case 0:
d3eb5eae 6035 gen_helper_fnstsw(cpu_tmp2_i32, cpu_env);
b6abf97d 6036 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
19e6c4b8 6037 gen_op_mov_reg_T0(OT_WORD, R_EAX);
2c0262af
FB
6038 break;
6039 default:
6040 goto illegal_op;
6041 }
6042 break;
6043 case 0x3d: /* fucomip */
773cdfcc 6044 gen_update_cc_op(s);
d3eb5eae
BS
6045 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6046 gen_helper_fucomi_ST0_FT0(cpu_env);
6047 gen_helper_fpop(cpu_env);
3ca51d07 6048 set_cc_op(s, CC_OP_EFLAGS);
2c0262af
FB
6049 break;
6050 case 0x3e: /* fcomip */
773cdfcc 6051 gen_update_cc_op(s);
d3eb5eae
BS
6052 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6053 gen_helper_fcomi_ST0_FT0(cpu_env);
6054 gen_helper_fpop(cpu_env);
3ca51d07 6055 set_cc_op(s, CC_OP_EFLAGS);
2c0262af 6056 break;
a2cc3b24
FB
6057 case 0x10 ... 0x13: /* fcmovxx */
6058 case 0x18 ... 0x1b:
6059 {
19e6c4b8 6060 int op1, l1;
d70040bc 6061 static const uint8_t fcmov_cc[8] = {
a2cc3b24
FB
6062 (JCC_B << 1),
6063 (JCC_Z << 1),
6064 (JCC_BE << 1),
6065 (JCC_P << 1),
6066 };
1e4840bf 6067 op1 = fcmov_cc[op & 3] | (((op >> 3) & 1) ^ 1);
19e6c4b8 6068 l1 = gen_new_label();
b27fc131 6069 gen_jcc1(s, op1, l1);
d3eb5eae 6070 gen_helper_fmov_ST0_STN(cpu_env, tcg_const_i32(opreg));
19e6c4b8 6071 gen_set_label(l1);
a2cc3b24
FB
6072 }
6073 break;
2c0262af
FB
6074 default:
6075 goto illegal_op;
6076 }
6077 }
6078 break;
6079 /************************/
6080 /* string ops */
6081
6082 case 0xa4: /* movsS */
6083 case 0xa5:
6084 if ((b & 1) == 0)
6085 ot = OT_BYTE;
6086 else
14ce26e7 6087 ot = dflag + OT_WORD;
2c0262af
FB
6088
6089 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
6090 gen_repz_movs(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
6091 } else {
6092 gen_movs(s, ot);
6093 }
6094 break;
3b46e624 6095
2c0262af
FB
6096 case 0xaa: /* stosS */
6097 case 0xab:
6098 if ((b & 1) == 0)
6099 ot = OT_BYTE;
6100 else
14ce26e7 6101 ot = dflag + OT_WORD;
2c0262af
FB
6102
6103 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
6104 gen_repz_stos(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
6105 } else {
6106 gen_stos(s, ot);
6107 }
6108 break;
6109 case 0xac: /* lodsS */
6110 case 0xad:
6111 if ((b & 1) == 0)
6112 ot = OT_BYTE;
6113 else
14ce26e7 6114 ot = dflag + OT_WORD;
2c0262af
FB
6115 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
6116 gen_repz_lods(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
6117 } else {
6118 gen_lods(s, ot);
6119 }
6120 break;
6121 case 0xae: /* scasS */
6122 case 0xaf:
6123 if ((b & 1) == 0)
6124 ot = OT_BYTE;
6125 else
14ce26e7 6126 ot = dflag + OT_WORD;
2c0262af
FB
6127 if (prefixes & PREFIX_REPNZ) {
6128 gen_repz_scas(s, ot, pc_start - s->cs_base, s->pc - s->cs_base, 1);
6129 } else if (prefixes & PREFIX_REPZ) {
6130 gen_repz_scas(s, ot, pc_start - s->cs_base, s->pc - s->cs_base, 0);
6131 } else {
6132 gen_scas(s, ot);
2c0262af
FB
6133 }
6134 break;
6135
6136 case 0xa6: /* cmpsS */
6137 case 0xa7:
6138 if ((b & 1) == 0)
6139 ot = OT_BYTE;
6140 else
14ce26e7 6141 ot = dflag + OT_WORD;
2c0262af
FB
6142 if (prefixes & PREFIX_REPNZ) {
6143 gen_repz_cmps(s, ot, pc_start - s->cs_base, s->pc - s->cs_base, 1);
6144 } else if (prefixes & PREFIX_REPZ) {
6145 gen_repz_cmps(s, ot, pc_start - s->cs_base, s->pc - s->cs_base, 0);
6146 } else {
6147 gen_cmps(s, ot);
2c0262af
FB
6148 }
6149 break;
6150 case 0x6c: /* insS */
6151 case 0x6d:
f115e911
FB
6152 if ((b & 1) == 0)
6153 ot = OT_BYTE;
6154 else
6155 ot = dflag ? OT_LONG : OT_WORD;
57fec1fe 6156 gen_op_mov_TN_reg(OT_WORD, 0, R_EDX);
0573fbfc 6157 gen_op_andl_T0_ffff();
b8b6a50b
FB
6158 gen_check_io(s, ot, pc_start - s->cs_base,
6159 SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes) | 4);
f115e911
FB
6160 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
6161 gen_repz_ins(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
2c0262af 6162 } else {
f115e911 6163 gen_ins(s, ot);
2e70f6ef
PB
6164 if (use_icount) {
6165 gen_jmp(s, s->pc - s->cs_base);
6166 }
2c0262af
FB
6167 }
6168 break;
6169 case 0x6e: /* outsS */
6170 case 0x6f:
f115e911
FB
6171 if ((b & 1) == 0)
6172 ot = OT_BYTE;
6173 else
6174 ot = dflag ? OT_LONG : OT_WORD;
57fec1fe 6175 gen_op_mov_TN_reg(OT_WORD, 0, R_EDX);
0573fbfc 6176 gen_op_andl_T0_ffff();
b8b6a50b
FB
6177 gen_check_io(s, ot, pc_start - s->cs_base,
6178 svm_is_rep(prefixes) | 4);
f115e911
FB
6179 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
6180 gen_repz_outs(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
2c0262af 6181 } else {
f115e911 6182 gen_outs(s, ot);
2e70f6ef
PB
6183 if (use_icount) {
6184 gen_jmp(s, s->pc - s->cs_base);
6185 }
2c0262af
FB
6186 }
6187 break;
6188
6189 /************************/
6190 /* port I/O */
0573fbfc 6191
2c0262af
FB
6192 case 0xe4:
6193 case 0xe5:
f115e911
FB
6194 if ((b & 1) == 0)
6195 ot = OT_BYTE;
6196 else
6197 ot = dflag ? OT_LONG : OT_WORD;
0af10c86 6198 val = cpu_ldub_code(env, s->pc++);
f115e911 6199 gen_op_movl_T0_im(val);
b8b6a50b
FB
6200 gen_check_io(s, ot, pc_start - s->cs_base,
6201 SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
2e70f6ef
PB
6202 if (use_icount)
6203 gen_io_start();
b6abf97d 6204 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
a7812ae4 6205 gen_helper_in_func(ot, cpu_T[1], cpu_tmp2_i32);
57fec1fe 6206 gen_op_mov_reg_T1(ot, R_EAX);
2e70f6ef
PB
6207 if (use_icount) {
6208 gen_io_end();
6209 gen_jmp(s, s->pc - s->cs_base);
6210 }
2c0262af
FB
6211 break;
6212 case 0xe6:
6213 case 0xe7:
f115e911
FB
6214 if ((b & 1) == 0)
6215 ot = OT_BYTE;
6216 else
6217 ot = dflag ? OT_LONG : OT_WORD;
0af10c86 6218 val = cpu_ldub_code(env, s->pc++);
f115e911 6219 gen_op_movl_T0_im(val);
b8b6a50b
FB
6220 gen_check_io(s, ot, pc_start - s->cs_base,
6221 svm_is_rep(prefixes));
57fec1fe 6222 gen_op_mov_TN_reg(ot, 1, R_EAX);
b8b6a50b 6223
2e70f6ef
PB
6224 if (use_icount)
6225 gen_io_start();
b6abf97d 6226 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
b6abf97d 6227 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
a7812ae4 6228 gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
2e70f6ef
PB
6229 if (use_icount) {
6230 gen_io_end();
6231 gen_jmp(s, s->pc - s->cs_base);
6232 }
2c0262af
FB
6233 break;
6234 case 0xec:
6235 case 0xed:
f115e911
FB
6236 if ((b & 1) == 0)
6237 ot = OT_BYTE;
6238 else
6239 ot = dflag ? OT_LONG : OT_WORD;
57fec1fe 6240 gen_op_mov_TN_reg(OT_WORD, 0, R_EDX);
4f31916f 6241 gen_op_andl_T0_ffff();
b8b6a50b
FB
6242 gen_check_io(s, ot, pc_start - s->cs_base,
6243 SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
2e70f6ef
PB
6244 if (use_icount)
6245 gen_io_start();
b6abf97d 6246 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
a7812ae4 6247 gen_helper_in_func(ot, cpu_T[1], cpu_tmp2_i32);
57fec1fe 6248 gen_op_mov_reg_T1(ot, R_EAX);
2e70f6ef
PB
6249 if (use_icount) {
6250 gen_io_end();
6251 gen_jmp(s, s->pc - s->cs_base);
6252 }
2c0262af
FB
6253 break;
6254 case 0xee:
6255 case 0xef:
f115e911
FB
6256 if ((b & 1) == 0)
6257 ot = OT_BYTE;
6258 else
6259 ot = dflag ? OT_LONG : OT_WORD;
57fec1fe 6260 gen_op_mov_TN_reg(OT_WORD, 0, R_EDX);
4f31916f 6261 gen_op_andl_T0_ffff();
b8b6a50b
FB
6262 gen_check_io(s, ot, pc_start - s->cs_base,
6263 svm_is_rep(prefixes));
57fec1fe 6264 gen_op_mov_TN_reg(ot, 1, R_EAX);
b8b6a50b 6265
2e70f6ef
PB
6266 if (use_icount)
6267 gen_io_start();
b6abf97d 6268 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
b6abf97d 6269 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
a7812ae4 6270 gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
2e70f6ef
PB
6271 if (use_icount) {
6272 gen_io_end();
6273 gen_jmp(s, s->pc - s->cs_base);
6274 }
2c0262af
FB
6275 break;
6276
6277 /************************/
6278 /* control */
6279 case 0xc2: /* ret im */
0af10c86 6280 val = cpu_ldsw_code(env, s->pc);
2c0262af
FB
6281 s->pc += 2;
6282 gen_pop_T0(s);
8f091a59
FB
6283 if (CODE64(s) && s->dflag)
6284 s->dflag = 2;
2c0262af
FB
6285 gen_stack_update(s, val + (2 << s->dflag));
6286 if (s->dflag == 0)
6287 gen_op_andl_T0_ffff();
6288 gen_op_jmp_T0();
6289 gen_eob(s);
6290 break;
6291 case 0xc3: /* ret */
6292 gen_pop_T0(s);
6293 gen_pop_update(s);
6294 if (s->dflag == 0)
6295 gen_op_andl_T0_ffff();
6296 gen_op_jmp_T0();
6297 gen_eob(s);
6298 break;
6299 case 0xca: /* lret im */
0af10c86 6300 val = cpu_ldsw_code(env, s->pc);
2c0262af
FB
6301 s->pc += 2;
6302 do_lret:
6303 if (s->pe && !s->vm86) {
773cdfcc 6304 gen_update_cc_op(s);
14ce26e7 6305 gen_jmp_im(pc_start - s->cs_base);
2999a0b2 6306 gen_helper_lret_protected(cpu_env, tcg_const_i32(s->dflag),
a7812ae4 6307 tcg_const_i32(val));
2c0262af
FB
6308 } else {
6309 gen_stack_A0(s);
6310 /* pop offset */
57fec1fe 6311 gen_op_ld_T0_A0(1 + s->dflag + s->mem_index);
2c0262af
FB
6312 if (s->dflag == 0)
6313 gen_op_andl_T0_ffff();
6314 /* NOTE: keeping EIP updated is not a problem in case of
6315 exception */
6316 gen_op_jmp_T0();
6317 /* pop selector */
6318 gen_op_addl_A0_im(2 << s->dflag);
57fec1fe 6319 gen_op_ld_T0_A0(1 + s->dflag + s->mem_index);
3bd7da9e 6320 gen_op_movl_seg_T0_vm(R_CS);
2c0262af
FB
6321 /* add stack offset */
6322 gen_stack_update(s, val + (4 << s->dflag));
6323 }
6324 gen_eob(s);
6325 break;
6326 case 0xcb: /* lret */
6327 val = 0;
6328 goto do_lret;
6329 case 0xcf: /* iret */
872929aa 6330 gen_svm_check_intercept(s, pc_start, SVM_EXIT_IRET);
2c0262af
FB
6331 if (!s->pe) {
6332 /* real mode */
2999a0b2 6333 gen_helper_iret_real(cpu_env, tcg_const_i32(s->dflag));
3ca51d07 6334 set_cc_op(s, CC_OP_EFLAGS);
f115e911
FB
6335 } else if (s->vm86) {
6336 if (s->iopl != 3) {
6337 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6338 } else {
2999a0b2 6339 gen_helper_iret_real(cpu_env, tcg_const_i32(s->dflag));
3ca51d07 6340 set_cc_op(s, CC_OP_EFLAGS);
f115e911 6341 }
2c0262af 6342 } else {
773cdfcc 6343 gen_update_cc_op(s);
14ce26e7 6344 gen_jmp_im(pc_start - s->cs_base);
2999a0b2 6345 gen_helper_iret_protected(cpu_env, tcg_const_i32(s->dflag),
a7812ae4 6346 tcg_const_i32(s->pc - s->cs_base));
3ca51d07 6347 set_cc_op(s, CC_OP_EFLAGS);
2c0262af
FB
6348 }
6349 gen_eob(s);
6350 break;
6351 case 0xe8: /* call im */
6352 {
14ce26e7 6353 if (dflag)
0af10c86 6354 tval = (int32_t)insn_get(env, s, OT_LONG);
14ce26e7 6355 else
0af10c86 6356 tval = (int16_t)insn_get(env, s, OT_WORD);
2c0262af 6357 next_eip = s->pc - s->cs_base;
14ce26e7 6358 tval += next_eip;
2c0262af 6359 if (s->dflag == 0)
14ce26e7 6360 tval &= 0xffff;
99596385
AJ
6361 else if(!CODE64(s))
6362 tval &= 0xffffffff;
14ce26e7 6363 gen_movtl_T0_im(next_eip);
2c0262af 6364 gen_push_T0(s);
14ce26e7 6365 gen_jmp(s, tval);
2c0262af
FB
6366 }
6367 break;
6368 case 0x9a: /* lcall im */
6369 {
6370 unsigned int selector, offset;
3b46e624 6371
14ce26e7
FB
6372 if (CODE64(s))
6373 goto illegal_op;
2c0262af 6374 ot = dflag ? OT_LONG : OT_WORD;
0af10c86
BS
6375 offset = insn_get(env, s, ot);
6376 selector = insn_get(env, s, OT_WORD);
3b46e624 6377
2c0262af 6378 gen_op_movl_T0_im(selector);
14ce26e7 6379 gen_op_movl_T1_imu(offset);
2c0262af
FB
6380 }
6381 goto do_lcall;
ecada8a2 6382 case 0xe9: /* jmp im */
14ce26e7 6383 if (dflag)
0af10c86 6384 tval = (int32_t)insn_get(env, s, OT_LONG);
14ce26e7 6385 else
0af10c86 6386 tval = (int16_t)insn_get(env, s, OT_WORD);
14ce26e7 6387 tval += s->pc - s->cs_base;
2c0262af 6388 if (s->dflag == 0)
14ce26e7 6389 tval &= 0xffff;
32938e12
AJ
6390 else if(!CODE64(s))
6391 tval &= 0xffffffff;
14ce26e7 6392 gen_jmp(s, tval);
2c0262af
FB
6393 break;
6394 case 0xea: /* ljmp im */
6395 {
6396 unsigned int selector, offset;
6397
14ce26e7
FB
6398 if (CODE64(s))
6399 goto illegal_op;
2c0262af 6400 ot = dflag ? OT_LONG : OT_WORD;
0af10c86
BS
6401 offset = insn_get(env, s, ot);
6402 selector = insn_get(env, s, OT_WORD);
3b46e624 6403
2c0262af 6404 gen_op_movl_T0_im(selector);
14ce26e7 6405 gen_op_movl_T1_imu(offset);
2c0262af
FB
6406 }
6407 goto do_ljmp;
6408 case 0xeb: /* jmp Jb */
0af10c86 6409 tval = (int8_t)insn_get(env, s, OT_BYTE);
14ce26e7 6410 tval += s->pc - s->cs_base;
2c0262af 6411 if (s->dflag == 0)
14ce26e7
FB
6412 tval &= 0xffff;
6413 gen_jmp(s, tval);
2c0262af
FB
6414 break;
6415 case 0x70 ... 0x7f: /* jcc Jb */
0af10c86 6416 tval = (int8_t)insn_get(env, s, OT_BYTE);
2c0262af
FB
6417 goto do_jcc;
6418 case 0x180 ... 0x18f: /* jcc Jv */
6419 if (dflag) {
0af10c86 6420 tval = (int32_t)insn_get(env, s, OT_LONG);
2c0262af 6421 } else {
0af10c86 6422 tval = (int16_t)insn_get(env, s, OT_WORD);
2c0262af
FB
6423 }
6424 do_jcc:
6425 next_eip = s->pc - s->cs_base;
14ce26e7 6426 tval += next_eip;
2c0262af 6427 if (s->dflag == 0)
14ce26e7
FB
6428 tval &= 0xffff;
6429 gen_jcc(s, b, tval, next_eip);
2c0262af
FB
6430 break;
6431
6432 case 0x190 ... 0x19f: /* setcc Gv */
0af10c86 6433 modrm = cpu_ldub_code(env, s->pc++);
2c0262af 6434 gen_setcc(s, b);
0af10c86 6435 gen_ldst_modrm(env, s, modrm, OT_BYTE, OR_TMP0, 1);
2c0262af
FB
6436 break;
6437 case 0x140 ... 0x14f: /* cmov Gv, Ev */
8e1c85e3
FB
6438 {
6439 int l1;
1e4840bf
FB
6440 TCGv t0;
6441
8e1c85e3 6442 ot = dflag + OT_WORD;
0af10c86 6443 modrm = cpu_ldub_code(env, s->pc++);
8e1c85e3
FB
6444 reg = ((modrm >> 3) & 7) | rex_r;
6445 mod = (modrm >> 6) & 3;
a7812ae4 6446 t0 = tcg_temp_local_new();
8e1c85e3 6447 if (mod != 3) {
0af10c86 6448 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
1e4840bf 6449 gen_op_ld_v(ot + s->mem_index, t0, cpu_A0);
8e1c85e3
FB
6450 } else {
6451 rm = (modrm & 7) | REX_B(s);
1e4840bf 6452 gen_op_mov_v_reg(ot, t0, rm);
8e1c85e3 6453 }
8e1c85e3
FB
6454#ifdef TARGET_X86_64
6455 if (ot == OT_LONG) {
6456 /* XXX: specific Intel behaviour ? */
6457 l1 = gen_new_label();
b27fc131 6458 gen_jcc1(s, b ^ 1, l1);
cc739bb0 6459 tcg_gen_mov_tl(cpu_regs[reg], t0);
8e1c85e3 6460 gen_set_label(l1);
cc739bb0 6461 tcg_gen_ext32u_tl(cpu_regs[reg], cpu_regs[reg]);
8e1c85e3
FB
6462 } else
6463#endif
6464 {
6465 l1 = gen_new_label();
b27fc131 6466 gen_jcc1(s, b ^ 1, l1);
1e4840bf 6467 gen_op_mov_reg_v(ot, reg, t0);
8e1c85e3
FB
6468 gen_set_label(l1);
6469 }
1e4840bf 6470 tcg_temp_free(t0);
2c0262af 6471 }
2c0262af 6472 break;
3b46e624 6473
2c0262af
FB
6474 /************************/
6475 /* flags */
6476 case 0x9c: /* pushf */
872929aa 6477 gen_svm_check_intercept(s, pc_start, SVM_EXIT_PUSHF);
2c0262af
FB
6478 if (s->vm86 && s->iopl != 3) {
6479 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6480 } else {
773cdfcc 6481 gen_update_cc_op(s);
f0967a1a 6482 gen_helper_read_eflags(cpu_T[0], cpu_env);
2c0262af
FB
6483 gen_push_T0(s);
6484 }
6485 break;
6486 case 0x9d: /* popf */
872929aa 6487 gen_svm_check_intercept(s, pc_start, SVM_EXIT_POPF);
2c0262af
FB
6488 if (s->vm86 && s->iopl != 3) {
6489 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6490 } else {
6491 gen_pop_T0(s);
6492 if (s->cpl == 0) {
6493 if (s->dflag) {
f0967a1a
BS
6494 gen_helper_write_eflags(cpu_env, cpu_T[0],
6495 tcg_const_i32((TF_MASK | AC_MASK |
6496 ID_MASK | NT_MASK |
6497 IF_MASK |
6498 IOPL_MASK)));
2c0262af 6499 } else {
f0967a1a
BS
6500 gen_helper_write_eflags(cpu_env, cpu_T[0],
6501 tcg_const_i32((TF_MASK | AC_MASK |
6502 ID_MASK | NT_MASK |
6503 IF_MASK | IOPL_MASK)
6504 & 0xffff));
2c0262af
FB
6505 }
6506 } else {
4136f33c
FB
6507 if (s->cpl <= s->iopl) {
6508 if (s->dflag) {
f0967a1a
BS
6509 gen_helper_write_eflags(cpu_env, cpu_T[0],
6510 tcg_const_i32((TF_MASK |
6511 AC_MASK |
6512 ID_MASK |
6513 NT_MASK |
6514 IF_MASK)));
4136f33c 6515 } else {
f0967a1a
BS
6516 gen_helper_write_eflags(cpu_env, cpu_T[0],
6517 tcg_const_i32((TF_MASK |
6518 AC_MASK |
6519 ID_MASK |
6520 NT_MASK |
6521 IF_MASK)
6522 & 0xffff));
4136f33c 6523 }
2c0262af 6524 } else {
4136f33c 6525 if (s->dflag) {
f0967a1a
BS
6526 gen_helper_write_eflags(cpu_env, cpu_T[0],
6527 tcg_const_i32((TF_MASK | AC_MASK |
6528 ID_MASK | NT_MASK)));
4136f33c 6529 } else {
f0967a1a
BS
6530 gen_helper_write_eflags(cpu_env, cpu_T[0],
6531 tcg_const_i32((TF_MASK | AC_MASK |
6532 ID_MASK | NT_MASK)
6533 & 0xffff));
4136f33c 6534 }
2c0262af
FB
6535 }
6536 }
6537 gen_pop_update(s);
3ca51d07 6538 set_cc_op(s, CC_OP_EFLAGS);
a9321a4d 6539 /* abort translation because TF/AC flag may change */
14ce26e7 6540 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
6541 gen_eob(s);
6542 }
6543 break;
6544 case 0x9e: /* sahf */
12e26b75 6545 if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM))
14ce26e7 6546 goto illegal_op;
57fec1fe 6547 gen_op_mov_TN_reg(OT_BYTE, 0, R_AH);
d229edce 6548 gen_compute_eflags(s);
bd7a7b33
FB
6549 tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, CC_O);
6550 tcg_gen_andi_tl(cpu_T[0], cpu_T[0], CC_S | CC_Z | CC_A | CC_P | CC_C);
6551 tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, cpu_T[0]);
2c0262af
FB
6552 break;
6553 case 0x9f: /* lahf */
12e26b75 6554 if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM))
14ce26e7 6555 goto illegal_op;
d229edce 6556 gen_compute_eflags(s);
bd7a7b33 6557 /* Note: gen_compute_eflags() only gives the condition codes */
d229edce 6558 tcg_gen_ori_tl(cpu_T[0], cpu_cc_src, 0x02);
57fec1fe 6559 gen_op_mov_reg_T0(OT_BYTE, R_AH);
2c0262af
FB
6560 break;
6561 case 0xf5: /* cmc */
d229edce 6562 gen_compute_eflags(s);
bd7a7b33 6563 tcg_gen_xori_tl(cpu_cc_src, cpu_cc_src, CC_C);
2c0262af
FB
6564 break;
6565 case 0xf8: /* clc */
d229edce 6566 gen_compute_eflags(s);
bd7a7b33 6567 tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~CC_C);
2c0262af
FB
6568 break;
6569 case 0xf9: /* stc */
d229edce 6570 gen_compute_eflags(s);
bd7a7b33 6571 tcg_gen_ori_tl(cpu_cc_src, cpu_cc_src, CC_C);
2c0262af
FB
6572 break;
6573 case 0xfc: /* cld */
b6abf97d 6574 tcg_gen_movi_i32(cpu_tmp2_i32, 1);
317ac620 6575 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, offsetof(CPUX86State, df));
2c0262af
FB
6576 break;
6577 case 0xfd: /* std */
b6abf97d 6578 tcg_gen_movi_i32(cpu_tmp2_i32, -1);
317ac620 6579 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, offsetof(CPUX86State, df));
2c0262af
FB
6580 break;
6581
6582 /************************/
6583 /* bit operations */
6584 case 0x1ba: /* bt/bts/btr/btc Gv, im */
14ce26e7 6585 ot = dflag + OT_WORD;
0af10c86 6586 modrm = cpu_ldub_code(env, s->pc++);
33698e5f 6587 op = (modrm >> 3) & 7;
2c0262af 6588 mod = (modrm >> 6) & 3;
14ce26e7 6589 rm = (modrm & 7) | REX_B(s);
2c0262af 6590 if (mod != 3) {
14ce26e7 6591 s->rip_offset = 1;
0af10c86 6592 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
57fec1fe 6593 gen_op_ld_T0_A0(ot + s->mem_index);
2c0262af 6594 } else {
57fec1fe 6595 gen_op_mov_TN_reg(ot, 0, rm);
2c0262af
FB
6596 }
6597 /* load shift */
0af10c86 6598 val = cpu_ldub_code(env, s->pc++);
2c0262af
FB
6599 gen_op_movl_T1_im(val);
6600 if (op < 4)
6601 goto illegal_op;
6602 op -= 4;
f484d386 6603 goto bt_op;
2c0262af
FB
6604 case 0x1a3: /* bt Gv, Ev */
6605 op = 0;
6606 goto do_btx;
6607 case 0x1ab: /* bts */
6608 op = 1;
6609 goto do_btx;
6610 case 0x1b3: /* btr */
6611 op = 2;
6612 goto do_btx;
6613 case 0x1bb: /* btc */
6614 op = 3;
6615 do_btx:
14ce26e7 6616 ot = dflag + OT_WORD;
0af10c86 6617 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7 6618 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af 6619 mod = (modrm >> 6) & 3;
14ce26e7 6620 rm = (modrm & 7) | REX_B(s);
57fec1fe 6621 gen_op_mov_TN_reg(OT_LONG, 1, reg);
2c0262af 6622 if (mod != 3) {
0af10c86 6623 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
2c0262af 6624 /* specific case: we need to add a displacement */
f484d386
FB
6625 gen_exts(ot, cpu_T[1]);
6626 tcg_gen_sari_tl(cpu_tmp0, cpu_T[1], 3 + ot);
6627 tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, ot);
6628 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
57fec1fe 6629 gen_op_ld_T0_A0(ot + s->mem_index);
2c0262af 6630 } else {
57fec1fe 6631 gen_op_mov_TN_reg(ot, 0, rm);
2c0262af 6632 }
f484d386
FB
6633 bt_op:
6634 tcg_gen_andi_tl(cpu_T[1], cpu_T[1], (1 << (3 + ot)) - 1);
6635 switch(op) {
6636 case 0:
6637 tcg_gen_shr_tl(cpu_cc_src, cpu_T[0], cpu_T[1]);
6638 tcg_gen_movi_tl(cpu_cc_dst, 0);
6639 break;
6640 case 1:
6641 tcg_gen_shr_tl(cpu_tmp4, cpu_T[0], cpu_T[1]);
6642 tcg_gen_movi_tl(cpu_tmp0, 1);
6643 tcg_gen_shl_tl(cpu_tmp0, cpu_tmp0, cpu_T[1]);
6644 tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
6645 break;
6646 case 2:
6647 tcg_gen_shr_tl(cpu_tmp4, cpu_T[0], cpu_T[1]);
6648 tcg_gen_movi_tl(cpu_tmp0, 1);
6649 tcg_gen_shl_tl(cpu_tmp0, cpu_tmp0, cpu_T[1]);
6650 tcg_gen_not_tl(cpu_tmp0, cpu_tmp0);
6651 tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
6652 break;
6653 default:
6654 case 3:
6655 tcg_gen_shr_tl(cpu_tmp4, cpu_T[0], cpu_T[1]);
6656 tcg_gen_movi_tl(cpu_tmp0, 1);
6657 tcg_gen_shl_tl(cpu_tmp0, cpu_tmp0, cpu_T[1]);
6658 tcg_gen_xor_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
6659 break;
6660 }
3ca51d07 6661 set_cc_op(s, CC_OP_SARB + ot);
2c0262af
FB
6662 if (op != 0) {
6663 if (mod != 3)
57fec1fe 6664 gen_op_st_T0_A0(ot + s->mem_index);
2c0262af 6665 else
57fec1fe 6666 gen_op_mov_reg_T0(ot, rm);
f484d386
FB
6667 tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
6668 tcg_gen_movi_tl(cpu_cc_dst, 0);
2c0262af
FB
6669 }
6670 break;
6671 case 0x1bc: /* bsf */
6672 case 0x1bd: /* bsr */
6191b059
FB
6673 {
6674 int label1;
1e4840bf
FB
6675 TCGv t0;
6676
6191b059 6677 ot = dflag + OT_WORD;
0af10c86 6678 modrm = cpu_ldub_code(env, s->pc++);
6191b059 6679 reg = ((modrm >> 3) & 7) | rex_r;
0af10c86 6680 gen_ldst_modrm(env, s,modrm, ot, OR_TMP0, 0);
6191b059 6681 gen_extu(ot, cpu_T[0]);
a7812ae4 6682 t0 = tcg_temp_local_new();
1e4840bf 6683 tcg_gen_mov_tl(t0, cpu_T[0]);
31501a71
AP
6684 if ((b & 1) && (prefixes & PREFIX_REPZ) &&
6685 (s->cpuid_ext3_features & CPUID_EXT3_ABM)) {
6686 switch(ot) {
6687 case OT_WORD: gen_helper_lzcnt(cpu_T[0], t0,
6688 tcg_const_i32(16)); break;
6689 case OT_LONG: gen_helper_lzcnt(cpu_T[0], t0,
6690 tcg_const_i32(32)); break;
6691 case OT_QUAD: gen_helper_lzcnt(cpu_T[0], t0,
6692 tcg_const_i32(64)); break;
6693 }
6694 gen_op_mov_reg_T0(ot, reg);
6191b059 6695 } else {
31501a71
AP
6696 label1 = gen_new_label();
6697 tcg_gen_movi_tl(cpu_cc_dst, 0);
6698 tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, label1);
6699 if (b & 1) {
6700 gen_helper_bsr(cpu_T[0], t0);
6701 } else {
6702 gen_helper_bsf(cpu_T[0], t0);
6703 }
6704 gen_op_mov_reg_T0(ot, reg);
6705 tcg_gen_movi_tl(cpu_cc_dst, 1);
6706 gen_set_label(label1);
3ca51d07 6707 set_cc_op(s, CC_OP_LOGICB + ot);
6191b059 6708 }
1e4840bf 6709 tcg_temp_free(t0);
6191b059 6710 }
2c0262af
FB
6711 break;
6712 /************************/
6713 /* bcd */
6714 case 0x27: /* daa */
14ce26e7
FB
6715 if (CODE64(s))
6716 goto illegal_op;
773cdfcc 6717 gen_update_cc_op(s);
7923057b 6718 gen_helper_daa(cpu_env);
3ca51d07 6719 set_cc_op(s, CC_OP_EFLAGS);
2c0262af
FB
6720 break;
6721 case 0x2f: /* das */
14ce26e7
FB
6722 if (CODE64(s))
6723 goto illegal_op;
773cdfcc 6724 gen_update_cc_op(s);
7923057b 6725 gen_helper_das(cpu_env);
3ca51d07 6726 set_cc_op(s, CC_OP_EFLAGS);
2c0262af
FB
6727 break;
6728 case 0x37: /* aaa */
14ce26e7
FB
6729 if (CODE64(s))
6730 goto illegal_op;
773cdfcc 6731 gen_update_cc_op(s);
7923057b 6732 gen_helper_aaa(cpu_env);
3ca51d07 6733 set_cc_op(s, CC_OP_EFLAGS);
2c0262af
FB
6734 break;
6735 case 0x3f: /* aas */
14ce26e7
FB
6736 if (CODE64(s))
6737 goto illegal_op;
773cdfcc 6738 gen_update_cc_op(s);
7923057b 6739 gen_helper_aas(cpu_env);
3ca51d07 6740 set_cc_op(s, CC_OP_EFLAGS);
2c0262af
FB
6741 break;
6742 case 0xd4: /* aam */
14ce26e7
FB
6743 if (CODE64(s))
6744 goto illegal_op;
0af10c86 6745 val = cpu_ldub_code(env, s->pc++);
b6d7c3db
TS
6746 if (val == 0) {
6747 gen_exception(s, EXCP00_DIVZ, pc_start - s->cs_base);
6748 } else {
7923057b 6749 gen_helper_aam(cpu_env, tcg_const_i32(val));
3ca51d07 6750 set_cc_op(s, CC_OP_LOGICB);
b6d7c3db 6751 }
2c0262af
FB
6752 break;
6753 case 0xd5: /* aad */
14ce26e7
FB
6754 if (CODE64(s))
6755 goto illegal_op;
0af10c86 6756 val = cpu_ldub_code(env, s->pc++);
7923057b 6757 gen_helper_aad(cpu_env, tcg_const_i32(val));
3ca51d07 6758 set_cc_op(s, CC_OP_LOGICB);
2c0262af
FB
6759 break;
6760 /************************/
6761 /* misc */
6762 case 0x90: /* nop */
ab1f142b 6763 /* XXX: correct lock test for all insn */
7418027e 6764 if (prefixes & PREFIX_LOCK) {
ab1f142b 6765 goto illegal_op;
7418027e
RH
6766 }
6767 /* If REX_B is set, then this is xchg eax, r8d, not a nop. */
6768 if (REX_B(s)) {
6769 goto do_xchg_reg_eax;
6770 }
0573fbfc
TS
6771 if (prefixes & PREFIX_REPZ) {
6772 gen_svm_check_intercept(s, pc_start, SVM_EXIT_PAUSE);
6773 }
2c0262af
FB
6774 break;
6775 case 0x9b: /* fwait */
5fafdf24 6776 if ((s->flags & (HF_MP_MASK | HF_TS_MASK)) ==
7eee2a50
FB
6777 (HF_MP_MASK | HF_TS_MASK)) {
6778 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
2ee73ac3 6779 } else {
773cdfcc 6780 gen_update_cc_op(s);
14ce26e7 6781 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae 6782 gen_helper_fwait(cpu_env);
7eee2a50 6783 }
2c0262af
FB
6784 break;
6785 case 0xcc: /* int3 */
6786 gen_interrupt(s, EXCP03_INT3, pc_start - s->cs_base, s->pc - s->cs_base);
6787 break;
6788 case 0xcd: /* int N */
0af10c86 6789 val = cpu_ldub_code(env, s->pc++);
f115e911 6790 if (s->vm86 && s->iopl != 3) {
5fafdf24 6791 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
f115e911
FB
6792 } else {
6793 gen_interrupt(s, val, pc_start - s->cs_base, s->pc - s->cs_base);
6794 }
2c0262af
FB
6795 break;
6796 case 0xce: /* into */
14ce26e7
FB
6797 if (CODE64(s))
6798 goto illegal_op;
773cdfcc 6799 gen_update_cc_op(s);
a8ede8ba 6800 gen_jmp_im(pc_start - s->cs_base);
4a7443be 6801 gen_helper_into(cpu_env, tcg_const_i32(s->pc - pc_start));
2c0262af 6802 break;
0b97134b 6803#ifdef WANT_ICEBP
2c0262af 6804 case 0xf1: /* icebp (undocumented, exits to external debugger) */
872929aa 6805 gen_svm_check_intercept(s, pc_start, SVM_EXIT_ICEBP);
aba9d61e 6806#if 1
2c0262af 6807 gen_debug(s, pc_start - s->cs_base);
aba9d61e
FB
6808#else
6809 /* start debug */
0af10c86 6810 tb_flush(env);
24537a01 6811 qemu_set_log(CPU_LOG_INT | CPU_LOG_TB_IN_ASM);
aba9d61e 6812#endif
2c0262af 6813 break;
0b97134b 6814#endif
2c0262af
FB
6815 case 0xfa: /* cli */
6816 if (!s->vm86) {
6817 if (s->cpl <= s->iopl) {
f0967a1a 6818 gen_helper_cli(cpu_env);
2c0262af
FB
6819 } else {
6820 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6821 }
6822 } else {
6823 if (s->iopl == 3) {
f0967a1a 6824 gen_helper_cli(cpu_env);
2c0262af
FB
6825 } else {
6826 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6827 }
6828 }
6829 break;
6830 case 0xfb: /* sti */
6831 if (!s->vm86) {
6832 if (s->cpl <= s->iopl) {
6833 gen_sti:
f0967a1a 6834 gen_helper_sti(cpu_env);
2c0262af 6835 /* interruptions are enabled only the first insn after sti */
a2cc3b24
FB
6836 /* If several instructions disable interrupts, only the
6837 _first_ does it */
6838 if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
f0967a1a 6839 gen_helper_set_inhibit_irq(cpu_env);
2c0262af 6840 /* give a chance to handle pending irqs */
14ce26e7 6841 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
6842 gen_eob(s);
6843 } else {
6844 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6845 }
6846 } else {
6847 if (s->iopl == 3) {
6848 goto gen_sti;
6849 } else {
6850 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6851 }
6852 }
6853 break;
6854 case 0x62: /* bound */
14ce26e7
FB
6855 if (CODE64(s))
6856 goto illegal_op;
2c0262af 6857 ot = dflag ? OT_LONG : OT_WORD;
0af10c86 6858 modrm = cpu_ldub_code(env, s->pc++);
2c0262af
FB
6859 reg = (modrm >> 3) & 7;
6860 mod = (modrm >> 6) & 3;
6861 if (mod == 3)
6862 goto illegal_op;
57fec1fe 6863 gen_op_mov_TN_reg(ot, 0, reg);
0af10c86 6864 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
14ce26e7 6865 gen_jmp_im(pc_start - s->cs_base);
b6abf97d 6866 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
92fc4b58
BS
6867 if (ot == OT_WORD) {
6868 gen_helper_boundw(cpu_env, cpu_A0, cpu_tmp2_i32);
6869 } else {
6870 gen_helper_boundl(cpu_env, cpu_A0, cpu_tmp2_i32);
6871 }
2c0262af
FB
6872 break;
6873 case 0x1c8 ... 0x1cf: /* bswap reg */
14ce26e7
FB
6874 reg = (b & 7) | REX_B(s);
6875#ifdef TARGET_X86_64
6876 if (dflag == 2) {
57fec1fe 6877 gen_op_mov_TN_reg(OT_QUAD, 0, reg);
66896cb8 6878 tcg_gen_bswap64_i64(cpu_T[0], cpu_T[0]);
57fec1fe 6879 gen_op_mov_reg_T0(OT_QUAD, reg);
5fafdf24 6880 } else
8777643e 6881#endif
57fec1fe
FB
6882 {
6883 gen_op_mov_TN_reg(OT_LONG, 0, reg);
8777643e
AJ
6884 tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
6885 tcg_gen_bswap32_tl(cpu_T[0], cpu_T[0]);
57fec1fe 6886 gen_op_mov_reg_T0(OT_LONG, reg);
14ce26e7 6887 }
2c0262af
FB
6888 break;
6889 case 0xd6: /* salc */
14ce26e7
FB
6890 if (CODE64(s))
6891 goto illegal_op;
8115f117 6892 gen_compute_eflags_c(s, cpu_T[0], false);
bd7a7b33
FB
6893 tcg_gen_neg_tl(cpu_T[0], cpu_T[0]);
6894 gen_op_mov_reg_T0(OT_BYTE, R_EAX);
2c0262af
FB
6895 break;
6896 case 0xe0: /* loopnz */
6897 case 0xe1: /* loopz */
2c0262af
FB
6898 case 0xe2: /* loop */
6899 case 0xe3: /* jecxz */
14ce26e7 6900 {
6e0d8677 6901 int l1, l2, l3;
14ce26e7 6902
0af10c86 6903 tval = (int8_t)insn_get(env, s, OT_BYTE);
14ce26e7
FB
6904 next_eip = s->pc - s->cs_base;
6905 tval += next_eip;
6906 if (s->dflag == 0)
6907 tval &= 0xffff;
3b46e624 6908
14ce26e7
FB
6909 l1 = gen_new_label();
6910 l2 = gen_new_label();
6e0d8677 6911 l3 = gen_new_label();
14ce26e7 6912 b &= 3;
6e0d8677
FB
6913 switch(b) {
6914 case 0: /* loopnz */
6915 case 1: /* loopz */
6e0d8677
FB
6916 gen_op_add_reg_im(s->aflag, R_ECX, -1);
6917 gen_op_jz_ecx(s->aflag, l3);
5bdb91b0 6918 gen_jcc1(s, (JCC_Z << 1) | (b ^ 1), l1);
6e0d8677
FB
6919 break;
6920 case 2: /* loop */
6921 gen_op_add_reg_im(s->aflag, R_ECX, -1);
6922 gen_op_jnz_ecx(s->aflag, l1);
6923 break;
6924 default:
6925 case 3: /* jcxz */
6926 gen_op_jz_ecx(s->aflag, l1);
6927 break;
14ce26e7
FB
6928 }
6929
6e0d8677 6930 gen_set_label(l3);
14ce26e7 6931 gen_jmp_im(next_eip);
8e1c85e3 6932 tcg_gen_br(l2);
6e0d8677 6933
14ce26e7
FB
6934 gen_set_label(l1);
6935 gen_jmp_im(tval);
6936 gen_set_label(l2);
6937 gen_eob(s);
6938 }
2c0262af
FB
6939 break;
6940 case 0x130: /* wrmsr */
6941 case 0x132: /* rdmsr */
6942 if (s->cpl != 0) {
6943 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6944 } else {
773cdfcc 6945 gen_update_cc_op(s);
872929aa 6946 gen_jmp_im(pc_start - s->cs_base);
0573fbfc 6947 if (b & 2) {
4a7443be 6948 gen_helper_rdmsr(cpu_env);
0573fbfc 6949 } else {
4a7443be 6950 gen_helper_wrmsr(cpu_env);
0573fbfc 6951 }
2c0262af
FB
6952 }
6953 break;
6954 case 0x131: /* rdtsc */
773cdfcc 6955 gen_update_cc_op(s);
ecada8a2 6956 gen_jmp_im(pc_start - s->cs_base);
efade670
PB
6957 if (use_icount)
6958 gen_io_start();
4a7443be 6959 gen_helper_rdtsc(cpu_env);
efade670
PB
6960 if (use_icount) {
6961 gen_io_end();
6962 gen_jmp(s, s->pc - s->cs_base);
6963 }
2c0262af 6964 break;
df01e0fc 6965 case 0x133: /* rdpmc */
773cdfcc 6966 gen_update_cc_op(s);
df01e0fc 6967 gen_jmp_im(pc_start - s->cs_base);
4a7443be 6968 gen_helper_rdpmc(cpu_env);
df01e0fc 6969 break;
023fe10d 6970 case 0x134: /* sysenter */
2436b61a 6971 /* For Intel SYSENTER is valid on 64-bit */
0af10c86 6972 if (CODE64(s) && env->cpuid_vendor1 != CPUID_VENDOR_INTEL_1)
14ce26e7 6973 goto illegal_op;
023fe10d
FB
6974 if (!s->pe) {
6975 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6976 } else {
728d803b 6977 gen_update_cc_op(s);
14ce26e7 6978 gen_jmp_im(pc_start - s->cs_base);
2999a0b2 6979 gen_helper_sysenter(cpu_env);
023fe10d
FB
6980 gen_eob(s);
6981 }
6982 break;
6983 case 0x135: /* sysexit */
2436b61a 6984 /* For Intel SYSEXIT is valid on 64-bit */
0af10c86 6985 if (CODE64(s) && env->cpuid_vendor1 != CPUID_VENDOR_INTEL_1)
14ce26e7 6986 goto illegal_op;
023fe10d
FB
6987 if (!s->pe) {
6988 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6989 } else {
728d803b 6990 gen_update_cc_op(s);
14ce26e7 6991 gen_jmp_im(pc_start - s->cs_base);
2999a0b2 6992 gen_helper_sysexit(cpu_env, tcg_const_i32(dflag));
023fe10d
FB
6993 gen_eob(s);
6994 }
6995 break;
14ce26e7
FB
6996#ifdef TARGET_X86_64
6997 case 0x105: /* syscall */
6998 /* XXX: is it usable in real mode ? */
728d803b 6999 gen_update_cc_op(s);
14ce26e7 7000 gen_jmp_im(pc_start - s->cs_base);
2999a0b2 7001 gen_helper_syscall(cpu_env, tcg_const_i32(s->pc - pc_start));
14ce26e7
FB
7002 gen_eob(s);
7003 break;
7004 case 0x107: /* sysret */
7005 if (!s->pe) {
7006 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7007 } else {
728d803b 7008 gen_update_cc_op(s);
14ce26e7 7009 gen_jmp_im(pc_start - s->cs_base);
2999a0b2 7010 gen_helper_sysret(cpu_env, tcg_const_i32(s->dflag));
aba9d61e 7011 /* condition codes are modified only in long mode */
3ca51d07
RH
7012 if (s->lma) {
7013 set_cc_op(s, CC_OP_EFLAGS);
7014 }
14ce26e7
FB
7015 gen_eob(s);
7016 }
7017 break;
7018#endif
2c0262af 7019 case 0x1a2: /* cpuid */
773cdfcc 7020 gen_update_cc_op(s);
9575cb94 7021 gen_jmp_im(pc_start - s->cs_base);
4a7443be 7022 gen_helper_cpuid(cpu_env);
2c0262af
FB
7023 break;
7024 case 0xf4: /* hlt */
7025 if (s->cpl != 0) {
7026 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7027 } else {
773cdfcc 7028 gen_update_cc_op(s);
94451178 7029 gen_jmp_im(pc_start - s->cs_base);
4a7443be 7030 gen_helper_hlt(cpu_env, tcg_const_i32(s->pc - pc_start));
5779406a 7031 s->is_jmp = DISAS_TB_JUMP;
2c0262af
FB
7032 }
7033 break;
7034 case 0x100:
0af10c86 7035 modrm = cpu_ldub_code(env, s->pc++);
2c0262af
FB
7036 mod = (modrm >> 6) & 3;
7037 op = (modrm >> 3) & 7;
7038 switch(op) {
7039 case 0: /* sldt */
f115e911
FB
7040 if (!s->pe || s->vm86)
7041 goto illegal_op;
872929aa 7042 gen_svm_check_intercept(s, pc_start, SVM_EXIT_LDTR_READ);
651ba608 7043 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,ldt.selector));
2c0262af
FB
7044 ot = OT_WORD;
7045 if (mod == 3)
7046 ot += s->dflag;
0af10c86 7047 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
2c0262af
FB
7048 break;
7049 case 2: /* lldt */
f115e911
FB
7050 if (!s->pe || s->vm86)
7051 goto illegal_op;
2c0262af
FB
7052 if (s->cpl != 0) {
7053 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7054 } else {
872929aa 7055 gen_svm_check_intercept(s, pc_start, SVM_EXIT_LDTR_WRITE);
0af10c86 7056 gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
14ce26e7 7057 gen_jmp_im(pc_start - s->cs_base);
b6abf97d 7058 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
2999a0b2 7059 gen_helper_lldt(cpu_env, cpu_tmp2_i32);
2c0262af
FB
7060 }
7061 break;
7062 case 1: /* str */
f115e911
FB
7063 if (!s->pe || s->vm86)
7064 goto illegal_op;
872929aa 7065 gen_svm_check_intercept(s, pc_start, SVM_EXIT_TR_READ);
651ba608 7066 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,tr.selector));
2c0262af
FB
7067 ot = OT_WORD;
7068 if (mod == 3)
7069 ot += s->dflag;
0af10c86 7070 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
2c0262af
FB
7071 break;
7072 case 3: /* ltr */
f115e911
FB
7073 if (!s->pe || s->vm86)
7074 goto illegal_op;
2c0262af
FB
7075 if (s->cpl != 0) {
7076 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7077 } else {
872929aa 7078 gen_svm_check_intercept(s, pc_start, SVM_EXIT_TR_WRITE);
0af10c86 7079 gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
14ce26e7 7080 gen_jmp_im(pc_start - s->cs_base);
b6abf97d 7081 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
2999a0b2 7082 gen_helper_ltr(cpu_env, cpu_tmp2_i32);
2c0262af
FB
7083 }
7084 break;
7085 case 4: /* verr */
7086 case 5: /* verw */
f115e911
FB
7087 if (!s->pe || s->vm86)
7088 goto illegal_op;
0af10c86 7089 gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
773cdfcc 7090 gen_update_cc_op(s);
2999a0b2
BS
7091 if (op == 4) {
7092 gen_helper_verr(cpu_env, cpu_T[0]);
7093 } else {
7094 gen_helper_verw(cpu_env, cpu_T[0]);
7095 }
3ca51d07 7096 set_cc_op(s, CC_OP_EFLAGS);
f115e911 7097 break;
2c0262af
FB
7098 default:
7099 goto illegal_op;
7100 }
7101 break;
7102 case 0x101:
0af10c86 7103 modrm = cpu_ldub_code(env, s->pc++);
2c0262af
FB
7104 mod = (modrm >> 6) & 3;
7105 op = (modrm >> 3) & 7;
3d7374c5 7106 rm = modrm & 7;
2c0262af
FB
7107 switch(op) {
7108 case 0: /* sgdt */
2c0262af
FB
7109 if (mod == 3)
7110 goto illegal_op;
872929aa 7111 gen_svm_check_intercept(s, pc_start, SVM_EXIT_GDTR_READ);
0af10c86 7112 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
651ba608 7113 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, gdt.limit));
57fec1fe 7114 gen_op_st_T0_A0(OT_WORD + s->mem_index);
aba9d61e 7115 gen_add_A0_im(s, 2);
651ba608 7116 tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, gdt.base));
2c0262af
FB
7117 if (!s->dflag)
7118 gen_op_andl_T0_im(0xffffff);
57fec1fe 7119 gen_op_st_T0_A0(CODE64(s) + OT_LONG + s->mem_index);
2c0262af 7120 break;
3d7374c5
FB
7121 case 1:
7122 if (mod == 3) {
7123 switch (rm) {
7124 case 0: /* monitor */
7125 if (!(s->cpuid_ext_features & CPUID_EXT_MONITOR) ||
7126 s->cpl != 0)
7127 goto illegal_op;
773cdfcc 7128 gen_update_cc_op(s);
3d7374c5
FB
7129 gen_jmp_im(pc_start - s->cs_base);
7130#ifdef TARGET_X86_64
7131 if (s->aflag == 2) {
bbf662ee 7132 gen_op_movq_A0_reg(R_EAX);
5fafdf24 7133 } else
3d7374c5
FB
7134#endif
7135 {
bbf662ee 7136 gen_op_movl_A0_reg(R_EAX);
3d7374c5
FB
7137 if (s->aflag == 0)
7138 gen_op_andl_A0_ffff();
7139 }
7140 gen_add_A0_ds_seg(s);
4a7443be 7141 gen_helper_monitor(cpu_env, cpu_A0);
3d7374c5
FB
7142 break;
7143 case 1: /* mwait */
7144 if (!(s->cpuid_ext_features & CPUID_EXT_MONITOR) ||
7145 s->cpl != 0)
7146 goto illegal_op;
728d803b 7147 gen_update_cc_op(s);
94451178 7148 gen_jmp_im(pc_start - s->cs_base);
4a7443be 7149 gen_helper_mwait(cpu_env, tcg_const_i32(s->pc - pc_start));
3d7374c5
FB
7150 gen_eob(s);
7151 break;
a9321a4d
PA
7152 case 2: /* clac */
7153 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_SMAP) ||
7154 s->cpl != 0) {
7155 goto illegal_op;
7156 }
7157 gen_helper_clac(cpu_env);
7158 gen_jmp_im(s->pc - s->cs_base);
7159 gen_eob(s);
7160 break;
7161 case 3: /* stac */
7162 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_SMAP) ||
7163 s->cpl != 0) {
7164 goto illegal_op;
7165 }
7166 gen_helper_stac(cpu_env);
7167 gen_jmp_im(s->pc - s->cs_base);
7168 gen_eob(s);
7169 break;
3d7374c5
FB
7170 default:
7171 goto illegal_op;
7172 }
7173 } else { /* sidt */
872929aa 7174 gen_svm_check_intercept(s, pc_start, SVM_EXIT_IDTR_READ);
0af10c86 7175 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
651ba608 7176 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, idt.limit));
57fec1fe 7177 gen_op_st_T0_A0(OT_WORD + s->mem_index);
3d7374c5 7178 gen_add_A0_im(s, 2);
651ba608 7179 tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, idt.base));
3d7374c5
FB
7180 if (!s->dflag)
7181 gen_op_andl_T0_im(0xffffff);
57fec1fe 7182 gen_op_st_T0_A0(CODE64(s) + OT_LONG + s->mem_index);
3d7374c5
FB
7183 }
7184 break;
2c0262af
FB
7185 case 2: /* lgdt */
7186 case 3: /* lidt */
0573fbfc 7187 if (mod == 3) {
773cdfcc 7188 gen_update_cc_op(s);
872929aa 7189 gen_jmp_im(pc_start - s->cs_base);
0573fbfc
TS
7190 switch(rm) {
7191 case 0: /* VMRUN */
872929aa
FB
7192 if (!(s->flags & HF_SVME_MASK) || !s->pe)
7193 goto illegal_op;
7194 if (s->cpl != 0) {
7195 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
0573fbfc 7196 break;
872929aa 7197 } else {
052e80d5 7198 gen_helper_vmrun(cpu_env, tcg_const_i32(s->aflag),
a7812ae4 7199 tcg_const_i32(s->pc - pc_start));
db620f46 7200 tcg_gen_exit_tb(0);
5779406a 7201 s->is_jmp = DISAS_TB_JUMP;
872929aa 7202 }
0573fbfc
TS
7203 break;
7204 case 1: /* VMMCALL */
872929aa
FB
7205 if (!(s->flags & HF_SVME_MASK))
7206 goto illegal_op;
052e80d5 7207 gen_helper_vmmcall(cpu_env);
0573fbfc
TS
7208 break;
7209 case 2: /* VMLOAD */
872929aa
FB
7210 if (!(s->flags & HF_SVME_MASK) || !s->pe)
7211 goto illegal_op;
7212 if (s->cpl != 0) {
7213 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7214 break;
7215 } else {
052e80d5 7216 gen_helper_vmload(cpu_env, tcg_const_i32(s->aflag));
872929aa 7217 }
0573fbfc
TS
7218 break;
7219 case 3: /* VMSAVE */
872929aa
FB
7220 if (!(s->flags & HF_SVME_MASK) || !s->pe)
7221 goto illegal_op;
7222 if (s->cpl != 0) {
7223 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7224 break;
7225 } else {
052e80d5 7226 gen_helper_vmsave(cpu_env, tcg_const_i32(s->aflag));
872929aa 7227 }
0573fbfc
TS
7228 break;
7229 case 4: /* STGI */
872929aa
FB
7230 if ((!(s->flags & HF_SVME_MASK) &&
7231 !(s->cpuid_ext3_features & CPUID_EXT3_SKINIT)) ||
7232 !s->pe)
7233 goto illegal_op;
7234 if (s->cpl != 0) {
7235 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7236 break;
7237 } else {
052e80d5 7238 gen_helper_stgi(cpu_env);
872929aa 7239 }
0573fbfc
TS
7240 break;
7241 case 5: /* CLGI */
872929aa
FB
7242 if (!(s->flags & HF_SVME_MASK) || !s->pe)
7243 goto illegal_op;
7244 if (s->cpl != 0) {
7245 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7246 break;
7247 } else {
052e80d5 7248 gen_helper_clgi(cpu_env);
872929aa 7249 }
0573fbfc
TS
7250 break;
7251 case 6: /* SKINIT */
872929aa
FB
7252 if ((!(s->flags & HF_SVME_MASK) &&
7253 !(s->cpuid_ext3_features & CPUID_EXT3_SKINIT)) ||
7254 !s->pe)
7255 goto illegal_op;
052e80d5 7256 gen_helper_skinit(cpu_env);
0573fbfc
TS
7257 break;
7258 case 7: /* INVLPGA */
872929aa
FB
7259 if (!(s->flags & HF_SVME_MASK) || !s->pe)
7260 goto illegal_op;
7261 if (s->cpl != 0) {
7262 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7263 break;
7264 } else {
052e80d5 7265 gen_helper_invlpga(cpu_env, tcg_const_i32(s->aflag));
872929aa 7266 }
0573fbfc
TS
7267 break;
7268 default:
7269 goto illegal_op;
7270 }
7271 } else if (s->cpl != 0) {
2c0262af
FB
7272 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7273 } else {
872929aa
FB
7274 gen_svm_check_intercept(s, pc_start,
7275 op==2 ? SVM_EXIT_GDTR_WRITE : SVM_EXIT_IDTR_WRITE);
0af10c86 7276 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
57fec1fe 7277 gen_op_ld_T1_A0(OT_WORD + s->mem_index);
aba9d61e 7278 gen_add_A0_im(s, 2);
57fec1fe 7279 gen_op_ld_T0_A0(CODE64(s) + OT_LONG + s->mem_index);
2c0262af
FB
7280 if (!s->dflag)
7281 gen_op_andl_T0_im(0xffffff);
7282 if (op == 2) {
651ba608
FB
7283 tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,gdt.base));
7284 tcg_gen_st32_tl(cpu_T[1], cpu_env, offsetof(CPUX86State,gdt.limit));
2c0262af 7285 } else {
651ba608
FB
7286 tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,idt.base));
7287 tcg_gen_st32_tl(cpu_T[1], cpu_env, offsetof(CPUX86State,idt.limit));
2c0262af
FB
7288 }
7289 }
7290 break;
7291 case 4: /* smsw */
872929aa 7292 gen_svm_check_intercept(s, pc_start, SVM_EXIT_READ_CR0);
e2542fe2 7293#if defined TARGET_X86_64 && defined HOST_WORDS_BIGENDIAN
f60d2728 7294 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,cr[0]) + 4);
7295#else
651ba608 7296 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,cr[0]));
f60d2728 7297#endif
0af10c86 7298 gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 1);
2c0262af
FB
7299 break;
7300 case 6: /* lmsw */
7301 if (s->cpl != 0) {
7302 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7303 } else {
872929aa 7304 gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_CR0);
0af10c86 7305 gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
4a7443be 7306 gen_helper_lmsw(cpu_env, cpu_T[0]);
14ce26e7 7307 gen_jmp_im(s->pc - s->cs_base);
d71b9a8b 7308 gen_eob(s);
2c0262af
FB
7309 }
7310 break;
1b050077
AP
7311 case 7:
7312 if (mod != 3) { /* invlpg */
7313 if (s->cpl != 0) {
7314 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7315 } else {
773cdfcc 7316 gen_update_cc_op(s);
1b050077 7317 gen_jmp_im(pc_start - s->cs_base);
0af10c86 7318 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
4a7443be 7319 gen_helper_invlpg(cpu_env, cpu_A0);
1b050077
AP
7320 gen_jmp_im(s->pc - s->cs_base);
7321 gen_eob(s);
7322 }
2c0262af 7323 } else {
1b050077
AP
7324 switch (rm) {
7325 case 0: /* swapgs */
14ce26e7 7326#ifdef TARGET_X86_64
1b050077
AP
7327 if (CODE64(s)) {
7328 if (s->cpl != 0) {
7329 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7330 } else {
7331 tcg_gen_ld_tl(cpu_T[0], cpu_env,
7332 offsetof(CPUX86State,segs[R_GS].base));
7333 tcg_gen_ld_tl(cpu_T[1], cpu_env,
7334 offsetof(CPUX86State,kernelgsbase));
7335 tcg_gen_st_tl(cpu_T[1], cpu_env,
7336 offsetof(CPUX86State,segs[R_GS].base));
7337 tcg_gen_st_tl(cpu_T[0], cpu_env,
7338 offsetof(CPUX86State,kernelgsbase));
7339 }
5fafdf24 7340 } else
14ce26e7
FB
7341#endif
7342 {
7343 goto illegal_op;
7344 }
1b050077
AP
7345 break;
7346 case 1: /* rdtscp */
7347 if (!(s->cpuid_ext2_features & CPUID_EXT2_RDTSCP))
7348 goto illegal_op;
773cdfcc 7349 gen_update_cc_op(s);
9575cb94 7350 gen_jmp_im(pc_start - s->cs_base);
1b050077
AP
7351 if (use_icount)
7352 gen_io_start();
4a7443be 7353 gen_helper_rdtscp(cpu_env);
1b050077
AP
7354 if (use_icount) {
7355 gen_io_end();
7356 gen_jmp(s, s->pc - s->cs_base);
7357 }
7358 break;
7359 default:
7360 goto illegal_op;
14ce26e7 7361 }
2c0262af
FB
7362 }
7363 break;
7364 default:
7365 goto illegal_op;
7366 }
7367 break;
3415a4dd
FB
7368 case 0x108: /* invd */
7369 case 0x109: /* wbinvd */
7370 if (s->cpl != 0) {
7371 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7372 } else {
872929aa 7373 gen_svm_check_intercept(s, pc_start, (b & 2) ? SVM_EXIT_INVD : SVM_EXIT_WBINVD);
3415a4dd
FB
7374 /* nothing to do */
7375 }
7376 break;
14ce26e7
FB
7377 case 0x63: /* arpl or movslS (x86_64) */
7378#ifdef TARGET_X86_64
7379 if (CODE64(s)) {
7380 int d_ot;
7381 /* d_ot is the size of destination */
7382 d_ot = dflag + OT_WORD;
7383
0af10c86 7384 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7
FB
7385 reg = ((modrm >> 3) & 7) | rex_r;
7386 mod = (modrm >> 6) & 3;
7387 rm = (modrm & 7) | REX_B(s);
3b46e624 7388
14ce26e7 7389 if (mod == 3) {
57fec1fe 7390 gen_op_mov_TN_reg(OT_LONG, 0, rm);
14ce26e7
FB
7391 /* sign extend */
7392 if (d_ot == OT_QUAD)
e108dd01 7393 tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
57fec1fe 7394 gen_op_mov_reg_T0(d_ot, reg);
14ce26e7 7395 } else {
0af10c86 7396 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
14ce26e7 7397 if (d_ot == OT_QUAD) {
57fec1fe 7398 gen_op_lds_T0_A0(OT_LONG + s->mem_index);
14ce26e7 7399 } else {
57fec1fe 7400 gen_op_ld_T0_A0(OT_LONG + s->mem_index);
14ce26e7 7401 }
57fec1fe 7402 gen_op_mov_reg_T0(d_ot, reg);
14ce26e7 7403 }
5fafdf24 7404 } else
14ce26e7
FB
7405#endif
7406 {
3bd7da9e 7407 int label1;
49d9fdcc 7408 TCGv t0, t1, t2, a0;
1e4840bf 7409
14ce26e7
FB
7410 if (!s->pe || s->vm86)
7411 goto illegal_op;
a7812ae4
PB
7412 t0 = tcg_temp_local_new();
7413 t1 = tcg_temp_local_new();
7414 t2 = tcg_temp_local_new();
3bd7da9e 7415 ot = OT_WORD;
0af10c86 7416 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7
FB
7417 reg = (modrm >> 3) & 7;
7418 mod = (modrm >> 6) & 3;
7419 rm = modrm & 7;
7420 if (mod != 3) {
0af10c86 7421 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
1e4840bf 7422 gen_op_ld_v(ot + s->mem_index, t0, cpu_A0);
49d9fdcc
LD
7423 a0 = tcg_temp_local_new();
7424 tcg_gen_mov_tl(a0, cpu_A0);
14ce26e7 7425 } else {
1e4840bf 7426 gen_op_mov_v_reg(ot, t0, rm);
49d9fdcc 7427 TCGV_UNUSED(a0);
14ce26e7 7428 }
1e4840bf
FB
7429 gen_op_mov_v_reg(ot, t1, reg);
7430 tcg_gen_andi_tl(cpu_tmp0, t0, 3);
7431 tcg_gen_andi_tl(t1, t1, 3);
7432 tcg_gen_movi_tl(t2, 0);
3bd7da9e 7433 label1 = gen_new_label();
1e4840bf
FB
7434 tcg_gen_brcond_tl(TCG_COND_GE, cpu_tmp0, t1, label1);
7435 tcg_gen_andi_tl(t0, t0, ~3);
7436 tcg_gen_or_tl(t0, t0, t1);
7437 tcg_gen_movi_tl(t2, CC_Z);
3bd7da9e 7438 gen_set_label(label1);
14ce26e7 7439 if (mod != 3) {
49d9fdcc
LD
7440 gen_op_st_v(ot + s->mem_index, t0, a0);
7441 tcg_temp_free(a0);
7442 } else {
1e4840bf 7443 gen_op_mov_reg_v(ot, rm, t0);
14ce26e7 7444 }
d229edce 7445 gen_compute_eflags(s);
3bd7da9e 7446 tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~CC_Z);
1e4840bf 7447 tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, t2);
1e4840bf
FB
7448 tcg_temp_free(t0);
7449 tcg_temp_free(t1);
7450 tcg_temp_free(t2);
f115e911 7451 }
f115e911 7452 break;
2c0262af
FB
7453 case 0x102: /* lar */
7454 case 0x103: /* lsl */
cec6843e
FB
7455 {
7456 int label1;
1e4840bf 7457 TCGv t0;
cec6843e
FB
7458 if (!s->pe || s->vm86)
7459 goto illegal_op;
7460 ot = dflag ? OT_LONG : OT_WORD;
0af10c86 7461 modrm = cpu_ldub_code(env, s->pc++);
cec6843e 7462 reg = ((modrm >> 3) & 7) | rex_r;
0af10c86 7463 gen_ldst_modrm(env, s, modrm, OT_WORD, OR_TMP0, 0);
a7812ae4 7464 t0 = tcg_temp_local_new();
773cdfcc 7465 gen_update_cc_op(s);
2999a0b2
BS
7466 if (b == 0x102) {
7467 gen_helper_lar(t0, cpu_env, cpu_T[0]);
7468 } else {
7469 gen_helper_lsl(t0, cpu_env, cpu_T[0]);
7470 }
cec6843e
FB
7471 tcg_gen_andi_tl(cpu_tmp0, cpu_cc_src, CC_Z);
7472 label1 = gen_new_label();
cb63669a 7473 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, label1);
1e4840bf 7474 gen_op_mov_reg_v(ot, reg, t0);
cec6843e 7475 gen_set_label(label1);
3ca51d07 7476 set_cc_op(s, CC_OP_EFLAGS);
1e4840bf 7477 tcg_temp_free(t0);
cec6843e 7478 }
2c0262af
FB
7479 break;
7480 case 0x118:
0af10c86 7481 modrm = cpu_ldub_code(env, s->pc++);
2c0262af
FB
7482 mod = (modrm >> 6) & 3;
7483 op = (modrm >> 3) & 7;
7484 switch(op) {
7485 case 0: /* prefetchnta */
7486 case 1: /* prefetchnt0 */
7487 case 2: /* prefetchnt0 */
7488 case 3: /* prefetchnt0 */
7489 if (mod == 3)
7490 goto illegal_op;
0af10c86 7491 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
2c0262af
FB
7492 /* nothing more to do */
7493 break;
e17a36ce 7494 default: /* nop (multi byte) */
0af10c86 7495 gen_nop_modrm(env, s, modrm);
e17a36ce 7496 break;
2c0262af
FB
7497 }
7498 break;
e17a36ce 7499 case 0x119 ... 0x11f: /* nop (multi byte) */
0af10c86
BS
7500 modrm = cpu_ldub_code(env, s->pc++);
7501 gen_nop_modrm(env, s, modrm);
e17a36ce 7502 break;
2c0262af
FB
7503 case 0x120: /* mov reg, crN */
7504 case 0x122: /* mov crN, reg */
7505 if (s->cpl != 0) {
7506 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7507 } else {
0af10c86 7508 modrm = cpu_ldub_code(env, s->pc++);
5c73b757
MO
7509 /* Ignore the mod bits (assume (modrm&0xc0)==0xc0).
7510 * AMD documentation (24594.pdf) and testing of
7511 * intel 386 and 486 processors all show that the mod bits
7512 * are assumed to be 1's, regardless of actual values.
7513 */
14ce26e7
FB
7514 rm = (modrm & 7) | REX_B(s);
7515 reg = ((modrm >> 3) & 7) | rex_r;
7516 if (CODE64(s))
7517 ot = OT_QUAD;
7518 else
7519 ot = OT_LONG;
ccd59d09
AP
7520 if ((prefixes & PREFIX_LOCK) && (reg == 0) &&
7521 (s->cpuid_ext3_features & CPUID_EXT3_CR8LEG)) {
7522 reg = 8;
7523 }
2c0262af
FB
7524 switch(reg) {
7525 case 0:
7526 case 2:
7527 case 3:
7528 case 4:
9230e66e 7529 case 8:
773cdfcc 7530 gen_update_cc_op(s);
872929aa 7531 gen_jmp_im(pc_start - s->cs_base);
2c0262af 7532 if (b & 2) {
57fec1fe 7533 gen_op_mov_TN_reg(ot, 0, rm);
4a7443be
BS
7534 gen_helper_write_crN(cpu_env, tcg_const_i32(reg),
7535 cpu_T[0]);
14ce26e7 7536 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
7537 gen_eob(s);
7538 } else {
4a7443be 7539 gen_helper_read_crN(cpu_T[0], cpu_env, tcg_const_i32(reg));
57fec1fe 7540 gen_op_mov_reg_T0(ot, rm);
2c0262af
FB
7541 }
7542 break;
7543 default:
7544 goto illegal_op;
7545 }
7546 }
7547 break;
7548 case 0x121: /* mov reg, drN */
7549 case 0x123: /* mov drN, reg */
7550 if (s->cpl != 0) {
7551 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7552 } else {
0af10c86 7553 modrm = cpu_ldub_code(env, s->pc++);
5c73b757
MO
7554 /* Ignore the mod bits (assume (modrm&0xc0)==0xc0).
7555 * AMD documentation (24594.pdf) and testing of
7556 * intel 386 and 486 processors all show that the mod bits
7557 * are assumed to be 1's, regardless of actual values.
7558 */
14ce26e7
FB
7559 rm = (modrm & 7) | REX_B(s);
7560 reg = ((modrm >> 3) & 7) | rex_r;
7561 if (CODE64(s))
7562 ot = OT_QUAD;
7563 else
7564 ot = OT_LONG;
2c0262af 7565 /* XXX: do it dynamically with CR4.DE bit */
14ce26e7 7566 if (reg == 4 || reg == 5 || reg >= 8)
2c0262af
FB
7567 goto illegal_op;
7568 if (b & 2) {
0573fbfc 7569 gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_DR0 + reg);
57fec1fe 7570 gen_op_mov_TN_reg(ot, 0, rm);
4a7443be 7571 gen_helper_movl_drN_T0(cpu_env, tcg_const_i32(reg), cpu_T[0]);
14ce26e7 7572 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
7573 gen_eob(s);
7574 } else {
0573fbfc 7575 gen_svm_check_intercept(s, pc_start, SVM_EXIT_READ_DR0 + reg);
651ba608 7576 tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,dr[reg]));
57fec1fe 7577 gen_op_mov_reg_T0(ot, rm);
2c0262af
FB
7578 }
7579 }
7580 break;
7581 case 0x106: /* clts */
7582 if (s->cpl != 0) {
7583 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7584 } else {
0573fbfc 7585 gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_CR0);
f0967a1a 7586 gen_helper_clts(cpu_env);
7eee2a50 7587 /* abort block because static cpu state changed */
14ce26e7 7588 gen_jmp_im(s->pc - s->cs_base);
7eee2a50 7589 gen_eob(s);
2c0262af
FB
7590 }
7591 break;
222a3336 7592 /* MMX/3DNow!/SSE/SSE2/SSE3/SSSE3/SSE4 support */
664e0f19
FB
7593 case 0x1c3: /* MOVNTI reg, mem */
7594 if (!(s->cpuid_features & CPUID_SSE2))
14ce26e7 7595 goto illegal_op;
664e0f19 7596 ot = s->dflag == 2 ? OT_QUAD : OT_LONG;
0af10c86 7597 modrm = cpu_ldub_code(env, s->pc++);
664e0f19
FB
7598 mod = (modrm >> 6) & 3;
7599 if (mod == 3)
7600 goto illegal_op;
7601 reg = ((modrm >> 3) & 7) | rex_r;
7602 /* generate a generic store */
0af10c86 7603 gen_ldst_modrm(env, s, modrm, ot, reg, 1);
14ce26e7 7604 break;
664e0f19 7605 case 0x1ae:
0af10c86 7606 modrm = cpu_ldub_code(env, s->pc++);
664e0f19
FB
7607 mod = (modrm >> 6) & 3;
7608 op = (modrm >> 3) & 7;
7609 switch(op) {
7610 case 0: /* fxsave */
5fafdf24 7611 if (mod == 3 || !(s->cpuid_features & CPUID_FXSR) ||
09d85fb8 7612 (s->prefix & PREFIX_LOCK))
14ce26e7 7613 goto illegal_op;
09d85fb8 7614 if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
0fd14b72
FB
7615 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
7616 break;
7617 }
0af10c86 7618 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
773cdfcc 7619 gen_update_cc_op(s);
19e6c4b8 7620 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae 7621 gen_helper_fxsave(cpu_env, cpu_A0, tcg_const_i32((s->dflag == 2)));
664e0f19
FB
7622 break;
7623 case 1: /* fxrstor */
5fafdf24 7624 if (mod == 3 || !(s->cpuid_features & CPUID_FXSR) ||
09d85fb8 7625 (s->prefix & PREFIX_LOCK))
14ce26e7 7626 goto illegal_op;
09d85fb8 7627 if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
0fd14b72
FB
7628 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
7629 break;
7630 }
0af10c86 7631 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
773cdfcc 7632 gen_update_cc_op(s);
19e6c4b8 7633 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae
BS
7634 gen_helper_fxrstor(cpu_env, cpu_A0,
7635 tcg_const_i32((s->dflag == 2)));
664e0f19
FB
7636 break;
7637 case 2: /* ldmxcsr */
7638 case 3: /* stmxcsr */
7639 if (s->flags & HF_TS_MASK) {
7640 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
7641 break;
14ce26e7 7642 }
664e0f19
FB
7643 if ((s->flags & HF_EM_MASK) || !(s->flags & HF_OSFXSR_MASK) ||
7644 mod == 3)
14ce26e7 7645 goto illegal_op;
0af10c86 7646 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
664e0f19 7647 if (op == 2) {
57fec1fe 7648 gen_op_ld_T0_A0(OT_LONG + s->mem_index);
20f8bd48 7649 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
d3eb5eae 7650 gen_helper_ldmxcsr(cpu_env, cpu_tmp2_i32);
14ce26e7 7651 } else {
651ba608 7652 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, mxcsr));
57fec1fe 7653 gen_op_st_T0_A0(OT_LONG + s->mem_index);
14ce26e7 7654 }
664e0f19
FB
7655 break;
7656 case 5: /* lfence */
7657 case 6: /* mfence */
8001c294 7658 if ((modrm & 0xc7) != 0xc0 || !(s->cpuid_features & CPUID_SSE2))
664e0f19
FB
7659 goto illegal_op;
7660 break;
8f091a59
FB
7661 case 7: /* sfence / clflush */
7662 if ((modrm & 0xc7) == 0xc0) {
7663 /* sfence */
a35f3ec7 7664 /* XXX: also check for cpuid_ext2_features & CPUID_EXT2_EMMX */
8f091a59
FB
7665 if (!(s->cpuid_features & CPUID_SSE))
7666 goto illegal_op;
7667 } else {
7668 /* clflush */
7669 if (!(s->cpuid_features & CPUID_CLFLUSH))
7670 goto illegal_op;
0af10c86 7671 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8f091a59
FB
7672 }
7673 break;
664e0f19 7674 default:
14ce26e7
FB
7675 goto illegal_op;
7676 }
7677 break;
a35f3ec7 7678 case 0x10d: /* 3DNow! prefetch(w) */
0af10c86 7679 modrm = cpu_ldub_code(env, s->pc++);
a35f3ec7
AJ
7680 mod = (modrm >> 6) & 3;
7681 if (mod == 3)
7682 goto illegal_op;
0af10c86 7683 gen_lea_modrm(env, s, modrm, &reg_addr, &offset_addr);
8f091a59
FB
7684 /* ignore for now */
7685 break;
3b21e03e 7686 case 0x1aa: /* rsm */
872929aa 7687 gen_svm_check_intercept(s, pc_start, SVM_EXIT_RSM);
3b21e03e
FB
7688 if (!(s->flags & HF_SMM_MASK))
7689 goto illegal_op;
728d803b 7690 gen_update_cc_op(s);
3b21e03e 7691 gen_jmp_im(s->pc - s->cs_base);
608badfc 7692 gen_helper_rsm(cpu_env);
3b21e03e
FB
7693 gen_eob(s);
7694 break;
222a3336
AZ
7695 case 0x1b8: /* SSE4.2 popcnt */
7696 if ((prefixes & (PREFIX_REPZ | PREFIX_LOCK | PREFIX_REPNZ)) !=
7697 PREFIX_REPZ)
7698 goto illegal_op;
7699 if (!(s->cpuid_ext_features & CPUID_EXT_POPCNT))
7700 goto illegal_op;
7701
0af10c86 7702 modrm = cpu_ldub_code(env, s->pc++);
8b4a3df8 7703 reg = ((modrm >> 3) & 7) | rex_r;
222a3336
AZ
7704
7705 if (s->prefix & PREFIX_DATA)
7706 ot = OT_WORD;
7707 else if (s->dflag != 2)
7708 ot = OT_LONG;
7709 else
7710 ot = OT_QUAD;
7711
0af10c86 7712 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
d3eb5eae 7713 gen_helper_popcnt(cpu_T[0], cpu_env, cpu_T[0], tcg_const_i32(ot));
222a3336 7714 gen_op_mov_reg_T0(ot, reg);
fdb0d09d 7715
3ca51d07 7716 set_cc_op(s, CC_OP_EFLAGS);
222a3336 7717 break;
a35f3ec7
AJ
7718 case 0x10e ... 0x10f:
7719 /* 3DNow! instructions, ignore prefixes */
7720 s->prefix &= ~(PREFIX_REPZ | PREFIX_REPNZ | PREFIX_DATA);
664e0f19
FB
7721 case 0x110 ... 0x117:
7722 case 0x128 ... 0x12f:
4242b1bd 7723 case 0x138 ... 0x13a:
d9f4bb27 7724 case 0x150 ... 0x179:
664e0f19
FB
7725 case 0x17c ... 0x17f:
7726 case 0x1c2:
7727 case 0x1c4 ... 0x1c6:
7728 case 0x1d0 ... 0x1fe:
0af10c86 7729 gen_sse(env, s, b, pc_start, rex_r);
664e0f19 7730 break;
2c0262af
FB
7731 default:
7732 goto illegal_op;
7733 }
7734 /* lock generation */
7735 if (s->prefix & PREFIX_LOCK)
a7812ae4 7736 gen_helper_unlock();
2c0262af
FB
7737 return s->pc;
7738 illegal_op:
ab1f142b 7739 if (s->prefix & PREFIX_LOCK)
a7812ae4 7740 gen_helper_unlock();
2c0262af
FB
7741 /* XXX: ensure that no lock was generated */
7742 gen_exception(s, EXCP06_ILLOP, pc_start - s->cs_base);
7743 return s->pc;
7744}
7745
2c0262af
FB
7746void optimize_flags_init(void)
7747{
a7812ae4
PB
7748 cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
7749 cpu_cc_op = tcg_global_mem_new_i32(TCG_AREG0,
317ac620
AF
7750 offsetof(CPUX86State, cc_op), "cc_op");
7751 cpu_cc_src = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_src),
a7812ae4 7752 "cc_src");
317ac620 7753 cpu_cc_dst = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_dst),
a7812ae4 7754 "cc_dst");
437a88a5 7755
cc739bb0
LD
7756#ifdef TARGET_X86_64
7757 cpu_regs[R_EAX] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7758 offsetof(CPUX86State, regs[R_EAX]), "rax");
cc739bb0 7759 cpu_regs[R_ECX] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7760 offsetof(CPUX86State, regs[R_ECX]), "rcx");
cc739bb0 7761 cpu_regs[R_EDX] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7762 offsetof(CPUX86State, regs[R_EDX]), "rdx");
cc739bb0 7763 cpu_regs[R_EBX] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7764 offsetof(CPUX86State, regs[R_EBX]), "rbx");
cc739bb0 7765 cpu_regs[R_ESP] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7766 offsetof(CPUX86State, regs[R_ESP]), "rsp");
cc739bb0 7767 cpu_regs[R_EBP] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7768 offsetof(CPUX86State, regs[R_EBP]), "rbp");
cc739bb0 7769 cpu_regs[R_ESI] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7770 offsetof(CPUX86State, regs[R_ESI]), "rsi");
cc739bb0 7771 cpu_regs[R_EDI] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7772 offsetof(CPUX86State, regs[R_EDI]), "rdi");
cc739bb0 7773 cpu_regs[8] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7774 offsetof(CPUX86State, regs[8]), "r8");
cc739bb0 7775 cpu_regs[9] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7776 offsetof(CPUX86State, regs[9]), "r9");
cc739bb0 7777 cpu_regs[10] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7778 offsetof(CPUX86State, regs[10]), "r10");
cc739bb0 7779 cpu_regs[11] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7780 offsetof(CPUX86State, regs[11]), "r11");
cc739bb0 7781 cpu_regs[12] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7782 offsetof(CPUX86State, regs[12]), "r12");
cc739bb0 7783 cpu_regs[13] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7784 offsetof(CPUX86State, regs[13]), "r13");
cc739bb0 7785 cpu_regs[14] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7786 offsetof(CPUX86State, regs[14]), "r14");
cc739bb0 7787 cpu_regs[15] = tcg_global_mem_new_i64(TCG_AREG0,
317ac620 7788 offsetof(CPUX86State, regs[15]), "r15");
cc739bb0
LD
7789#else
7790 cpu_regs[R_EAX] = tcg_global_mem_new_i32(TCG_AREG0,
317ac620 7791 offsetof(CPUX86State, regs[R_EAX]), "eax");
cc739bb0 7792 cpu_regs[R_ECX] = tcg_global_mem_new_i32(TCG_AREG0,
317ac620 7793 offsetof(CPUX86State, regs[R_ECX]), "ecx");
cc739bb0 7794 cpu_regs[R_EDX] = tcg_global_mem_new_i32(TCG_AREG0,
317ac620 7795 offsetof(CPUX86State, regs[R_EDX]), "edx");
cc739bb0 7796 cpu_regs[R_EBX] = tcg_global_mem_new_i32(TCG_AREG0,
317ac620 7797 offsetof(CPUX86State, regs[R_EBX]), "ebx");
cc739bb0 7798 cpu_regs[R_ESP] = tcg_global_mem_new_i32(TCG_AREG0,
317ac620 7799 offsetof(CPUX86State, regs[R_ESP]), "esp");
cc739bb0 7800 cpu_regs[R_EBP] = tcg_global_mem_new_i32(TCG_AREG0,
317ac620 7801 offsetof(CPUX86State, regs[R_EBP]), "ebp");
cc739bb0 7802 cpu_regs[R_ESI] = tcg_global_mem_new_i32(TCG_AREG0,
317ac620 7803 offsetof(CPUX86State, regs[R_ESI]), "esi");
cc739bb0 7804 cpu_regs[R_EDI] = tcg_global_mem_new_i32(TCG_AREG0,
317ac620 7805 offsetof(CPUX86State, regs[R_EDI]), "edi");
cc739bb0
LD
7806#endif
7807
437a88a5 7808 /* register helpers */
a7812ae4 7809#define GEN_HELPER 2
437a88a5 7810#include "helper.h"
2c0262af
FB
7811}
7812
7813/* generate intermediate code in gen_opc_buf and gen_opparam_buf for
7814 basic block 'tb'. If search_pc is TRUE, also generate PC
7815 information for each intermediate instruction. */
317ac620 7816static inline void gen_intermediate_code_internal(CPUX86State *env,
2cfc5f17
TS
7817 TranslationBlock *tb,
7818 int search_pc)
2c0262af
FB
7819{
7820 DisasContext dc1, *dc = &dc1;
14ce26e7 7821 target_ulong pc_ptr;
2c0262af 7822 uint16_t *gen_opc_end;
a1d1bb31 7823 CPUBreakpoint *bp;
7f5b7d3e 7824 int j, lj;
c068688b 7825 uint64_t flags;
14ce26e7
FB
7826 target_ulong pc_start;
7827 target_ulong cs_base;
2e70f6ef
PB
7828 int num_insns;
7829 int max_insns;
3b46e624 7830
2c0262af 7831 /* generate intermediate code */
14ce26e7
FB
7832 pc_start = tb->pc;
7833 cs_base = tb->cs_base;
2c0262af 7834 flags = tb->flags;
3a1d9b8b 7835
4f31916f 7836 dc->pe = (flags >> HF_PE_SHIFT) & 1;
2c0262af
FB
7837 dc->code32 = (flags >> HF_CS32_SHIFT) & 1;
7838 dc->ss32 = (flags >> HF_SS32_SHIFT) & 1;
7839 dc->addseg = (flags >> HF_ADDSEG_SHIFT) & 1;
7840 dc->f_st = 0;
7841 dc->vm86 = (flags >> VM_SHIFT) & 1;
7842 dc->cpl = (flags >> HF_CPL_SHIFT) & 3;
7843 dc->iopl = (flags >> IOPL_SHIFT) & 3;
7844 dc->tf = (flags >> TF_SHIFT) & 1;
34865134 7845 dc->singlestep_enabled = env->singlestep_enabled;
2c0262af 7846 dc->cc_op = CC_OP_DYNAMIC;
e207582f 7847 dc->cc_op_dirty = false;
2c0262af
FB
7848 dc->cs_base = cs_base;
7849 dc->tb = tb;
7850 dc->popl_esp_hack = 0;
7851 /* select memory access functions */
7852 dc->mem_index = 0;
7853 if (flags & HF_SOFTMMU_MASK) {
a9321a4d 7854 dc->mem_index = (cpu_mmu_index(env) + 1) << 2;
2c0262af 7855 }
14ce26e7 7856 dc->cpuid_features = env->cpuid_features;
3d7374c5 7857 dc->cpuid_ext_features = env->cpuid_ext_features;
e771edab 7858 dc->cpuid_ext2_features = env->cpuid_ext2_features;
12e26b75 7859 dc->cpuid_ext3_features = env->cpuid_ext3_features;
a9321a4d 7860 dc->cpuid_7_0_ebx_features = env->cpuid_7_0_ebx_features;
14ce26e7
FB
7861#ifdef TARGET_X86_64
7862 dc->lma = (flags >> HF_LMA_SHIFT) & 1;
7863 dc->code64 = (flags >> HF_CS64_SHIFT) & 1;
7864#endif
7eee2a50 7865 dc->flags = flags;
a2cc3b24
FB
7866 dc->jmp_opt = !(dc->tf || env->singlestep_enabled ||
7867 (flags & HF_INHIBIT_IRQ_MASK)
415fa2ea 7868#ifndef CONFIG_SOFTMMU
2c0262af
FB
7869 || (flags & HF_SOFTMMU_MASK)
7870#endif
7871 );
4f31916f
FB
7872#if 0
7873 /* check addseg logic */
dc196a57 7874 if (!dc->addseg && (dc->vm86 || !dc->pe || !dc->code32))
4f31916f
FB
7875 printf("ERROR addseg\n");
7876#endif
7877
a7812ae4
PB
7878 cpu_T[0] = tcg_temp_new();
7879 cpu_T[1] = tcg_temp_new();
7880 cpu_A0 = tcg_temp_new();
7881 cpu_T3 = tcg_temp_new();
7882
7883 cpu_tmp0 = tcg_temp_new();
7884 cpu_tmp1_i64 = tcg_temp_new_i64();
7885 cpu_tmp2_i32 = tcg_temp_new_i32();
7886 cpu_tmp3_i32 = tcg_temp_new_i32();
7887 cpu_tmp4 = tcg_temp_new();
7888 cpu_tmp5 = tcg_temp_new();
a7812ae4
PB
7889 cpu_ptr0 = tcg_temp_new_ptr();
7890 cpu_ptr1 = tcg_temp_new_ptr();
57fec1fe 7891
92414b31 7892 gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
2c0262af
FB
7893
7894 dc->is_jmp = DISAS_NEXT;
7895 pc_ptr = pc_start;
7896 lj = -1;
2e70f6ef
PB
7897 num_insns = 0;
7898 max_insns = tb->cflags & CF_COUNT_MASK;
7899 if (max_insns == 0)
7900 max_insns = CF_COUNT_MASK;
2c0262af 7901
2e70f6ef 7902 gen_icount_start();
2c0262af 7903 for(;;) {
72cf2d4f
BS
7904 if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
7905 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
a2397807
JK
7906 if (bp->pc == pc_ptr &&
7907 !((bp->flags & BP_CPU) && (tb->flags & HF_RF_MASK))) {
2c0262af
FB
7908 gen_debug(dc, pc_ptr - dc->cs_base);
7909 break;
7910 }
7911 }
7912 }
7913 if (search_pc) {
92414b31 7914 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
2c0262af
FB
7915 if (lj < j) {
7916 lj++;
7917 while (lj < j)
ab1103de 7918 tcg_ctx.gen_opc_instr_start[lj++] = 0;
2c0262af 7919 }
25983cad 7920 tcg_ctx.gen_opc_pc[lj] = pc_ptr;
2c0262af 7921 gen_opc_cc_op[lj] = dc->cc_op;
ab1103de 7922 tcg_ctx.gen_opc_instr_start[lj] = 1;
c9c99c22 7923 tcg_ctx.gen_opc_icount[lj] = num_insns;
2c0262af 7924 }
2e70f6ef
PB
7925 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
7926 gen_io_start();
7927
0af10c86 7928 pc_ptr = disas_insn(env, dc, pc_ptr);
2e70f6ef 7929 num_insns++;
2c0262af
FB
7930 /* stop translation if indicated */
7931 if (dc->is_jmp)
7932 break;
7933 /* if single step mode, we generate only one instruction and
7934 generate an exception */
a2cc3b24
FB
7935 /* if irq were inhibited with HF_INHIBIT_IRQ_MASK, we clear
7936 the flag and abort the translation to give the irqs a
7937 change to be happen */
5fafdf24 7938 if (dc->tf || dc->singlestep_enabled ||
2e70f6ef 7939 (flags & HF_INHIBIT_IRQ_MASK)) {
14ce26e7 7940 gen_jmp_im(pc_ptr - dc->cs_base);
2c0262af
FB
7941 gen_eob(dc);
7942 break;
7943 }
7944 /* if too long translation, stop generation too */
efd7f486 7945 if (tcg_ctx.gen_opc_ptr >= gen_opc_end ||
2e70f6ef
PB
7946 (pc_ptr - pc_start) >= (TARGET_PAGE_SIZE - 32) ||
7947 num_insns >= max_insns) {
14ce26e7 7948 gen_jmp_im(pc_ptr - dc->cs_base);
2c0262af
FB
7949 gen_eob(dc);
7950 break;
7951 }
1b530a6d
AJ
7952 if (singlestep) {
7953 gen_jmp_im(pc_ptr - dc->cs_base);
7954 gen_eob(dc);
7955 break;
7956 }
2c0262af 7957 }
2e70f6ef
PB
7958 if (tb->cflags & CF_LAST_IO)
7959 gen_io_end();
7960 gen_icount_end(tb, num_insns);
efd7f486 7961 *tcg_ctx.gen_opc_ptr = INDEX_op_end;
2c0262af
FB
7962 /* we don't forget to fill the last values */
7963 if (search_pc) {
92414b31 7964 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
2c0262af
FB
7965 lj++;
7966 while (lj <= j)
ab1103de 7967 tcg_ctx.gen_opc_instr_start[lj++] = 0;
2c0262af 7968 }
3b46e624 7969
2c0262af 7970#ifdef DEBUG_DISAS
8fec2b8c 7971 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
14ce26e7 7972 int disas_flags;
93fcfe39
AL
7973 qemu_log("----------------\n");
7974 qemu_log("IN: %s\n", lookup_symbol(pc_start));
14ce26e7
FB
7975#ifdef TARGET_X86_64
7976 if (dc->code64)
7977 disas_flags = 2;
7978 else
7979#endif
7980 disas_flags = !dc->code32;
f4359b9f 7981 log_target_disas(env, pc_start, pc_ptr - pc_start, disas_flags);
93fcfe39 7982 qemu_log("\n");
2c0262af
FB
7983 }
7984#endif
7985
2e70f6ef 7986 if (!search_pc) {
2c0262af 7987 tb->size = pc_ptr - pc_start;
2e70f6ef
PB
7988 tb->icount = num_insns;
7989 }
2c0262af
FB
7990}
7991
317ac620 7992void gen_intermediate_code(CPUX86State *env, TranslationBlock *tb)
2c0262af 7993{
2cfc5f17 7994 gen_intermediate_code_internal(env, tb, 0);
2c0262af
FB
7995}
7996
317ac620 7997void gen_intermediate_code_pc(CPUX86State *env, TranslationBlock *tb)
2c0262af 7998{
2cfc5f17 7999 gen_intermediate_code_internal(env, tb, 1);
2c0262af
FB
8000}
8001
317ac620 8002void restore_state_to_opc(CPUX86State *env, TranslationBlock *tb, int pc_pos)
d2856f1a
AJ
8003{
8004 int cc_op;
8005#ifdef DEBUG_DISAS
8fec2b8c 8006 if (qemu_loglevel_mask(CPU_LOG_TB_OP)) {
d2856f1a 8007 int i;
93fcfe39 8008 qemu_log("RESTORE:\n");
d2856f1a 8009 for(i = 0;i <= pc_pos; i++) {
ab1103de 8010 if (tcg_ctx.gen_opc_instr_start[i]) {
25983cad
EV
8011 qemu_log("0x%04x: " TARGET_FMT_lx "\n", i,
8012 tcg_ctx.gen_opc_pc[i]);
d2856f1a
AJ
8013 }
8014 }
e87b7cb0 8015 qemu_log("pc_pos=0x%x eip=" TARGET_FMT_lx " cs_base=%x\n",
25983cad 8016 pc_pos, tcg_ctx.gen_opc_pc[pc_pos] - tb->cs_base,
d2856f1a
AJ
8017 (uint32_t)tb->cs_base);
8018 }
8019#endif
25983cad 8020 env->eip = tcg_ctx.gen_opc_pc[pc_pos] - tb->cs_base;
d2856f1a
AJ
8021 cc_op = gen_opc_cc_op[pc_pos];
8022 if (cc_op != CC_OP_DYNAMIC)
8023 env->cc_op = cc_op;
8024}