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