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