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