2 * New-style TCG opcode generator for i386 instructions
4 * Copyright (c) 2022 Red Hat, Inc.
6 * Author: Paolo Bonzini <pbonzini@redhat.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 * Sometimes, knowing what the backend has can produce better code.
24 * The exact opcode to check depends on 32- vs. 64-bit.
27 #define TCG_TARGET_HAS_extract2_tl TCG_TARGET_HAS_extract2_i64
28 #define TCG_TARGET_deposit_tl_valid TCG_TARGET_deposit_i64_valid
29 #define TCG_TARGET_extract_tl_valid TCG_TARGET_extract_i64_valid
31 #define TCG_TARGET_HAS_extract2_tl TCG_TARGET_HAS_extract2_i32
32 #define TCG_TARGET_deposit_tl_valid TCG_TARGET_deposit_i32_valid
33 #define TCG_TARGET_extract_tl_valid TCG_TARGET_extract_i32_valid
37 #define ZMM_OFFSET(reg) offsetof(CPUX86State, xmm_regs[reg])
39 typedef void (*SSEFunc_i_ep)(TCGv_i32 val, TCGv_ptr env, TCGv_ptr reg);
40 typedef void (*SSEFunc_l_ep)(TCGv_i64 val, TCGv_ptr env, TCGv_ptr reg);
41 typedef void (*SSEFunc_0_epp)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b);
42 typedef void (*SSEFunc_0_eppp)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
44 typedef void (*SSEFunc_0_epppp)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
45 TCGv_ptr reg_c, TCGv_ptr reg_d);
46 typedef void (*SSEFunc_0_eppi)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
48 typedef void (*SSEFunc_0_epppi)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
49 TCGv_ptr reg_c, TCGv_i32 val);
50 typedef void (*SSEFunc_0_ppi)(TCGv_ptr reg_a, TCGv_ptr reg_b, TCGv_i32 val);
51 typedef void (*SSEFunc_0_pppi)(TCGv_ptr reg_a, TCGv_ptr reg_b, TCGv_ptr reg_c,
53 typedef void (*SSEFunc_0_eppt)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
55 typedef void (*SSEFunc_0_epppti)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
56 TCGv_ptr reg_c, TCGv a0, TCGv_i32 scale);
57 typedef void (*SSEFunc_0_eppppi)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
58 TCGv_ptr reg_c, TCGv_ptr reg_d, TCGv_i32 flags);
59 typedef void (*SSEFunc_0_eppppii)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
60 TCGv_ptr reg_c, TCGv_ptr reg_d, TCGv_i32 even,
63 static void gen_JMP_m(DisasContext *s, X86DecodedInsn *decode);
64 static void gen_JMP(DisasContext *s, X86DecodedInsn *decode);
66 static inline TCGv_i32 tcg_constant8u_i32(uint8_t val)
68 return tcg_constant_i32(val);
71 static void gen_NM_exception(DisasContext *s)
73 gen_exception(s, EXCP07_PREX);
76 static void gen_load_ea(DisasContext *s, AddressParts *mem, bool is_vsib)
78 TCGv ea = gen_lea_modrm_1(s, *mem, is_vsib);
79 gen_lea_v_seg(s, ea, mem->def_seg, s->override);
82 static inline int mmx_offset(MemOp ot)
86 return offsetof(MMXReg, MMX_B(0));
88 return offsetof(MMXReg, MMX_W(0));
90 return offsetof(MMXReg, MMX_L(0));
92 return offsetof(MMXReg, MMX_Q(0));
94 g_assert_not_reached();
98 static inline int xmm_offset(MemOp ot)
102 return offsetof(ZMMReg, ZMM_B(0));
104 return offsetof(ZMMReg, ZMM_W(0));
106 return offsetof(ZMMReg, ZMM_L(0));
108 return offsetof(ZMMReg, ZMM_Q(0));
110 return offsetof(ZMMReg, ZMM_X(0));
112 return offsetof(ZMMReg, ZMM_Y(0));
114 g_assert_not_reached();
118 static int vector_reg_offset(X86DecodedOp *op)
120 assert(op->unit == X86_OP_MMX || op->unit == X86_OP_SSE);
122 if (op->unit == X86_OP_MMX) {
123 return op->offset - mmx_offset(op->ot);
125 return op->offset - xmm_offset(op->ot);
129 static int vector_elem_offset(X86DecodedOp *op, MemOp ot, int n)
131 int base_ofs = vector_reg_offset(op);
134 if (op->unit == X86_OP_MMX) {
135 return base_ofs + offsetof(MMXReg, MMX_B(n));
137 return base_ofs + offsetof(ZMMReg, ZMM_B(n));
140 if (op->unit == X86_OP_MMX) {
141 return base_ofs + offsetof(MMXReg, MMX_W(n));
143 return base_ofs + offsetof(ZMMReg, ZMM_W(n));
146 if (op->unit == X86_OP_MMX) {
147 return base_ofs + offsetof(MMXReg, MMX_L(n));
149 return base_ofs + offsetof(ZMMReg, ZMM_L(n));
152 if (op->unit == X86_OP_MMX) {
155 return base_ofs + offsetof(ZMMReg, ZMM_Q(n));
158 assert(op->unit == X86_OP_SSE);
159 return base_ofs + offsetof(ZMMReg, ZMM_X(n));
161 assert(op->unit == X86_OP_SSE);
162 return base_ofs + offsetof(ZMMReg, ZMM_Y(n));
164 g_assert_not_reached();
168 static void compute_mmx_offset(X86DecodedOp *op)
171 op->offset = offsetof(CPUX86State, fpregs[op->n].mmx) + mmx_offset(op->ot);
173 op->offset = offsetof(CPUX86State, mmx_t0) + mmx_offset(op->ot);
177 static void compute_xmm_offset(X86DecodedOp *op)
180 op->offset = ZMM_OFFSET(op->n) + xmm_offset(op->ot);
182 op->offset = offsetof(CPUX86State, xmm_t0) + xmm_offset(op->ot);
186 static void gen_load_sse(DisasContext *s, TCGv temp, MemOp ot, int dest_ofs, bool aligned)
190 gen_op_ld_v(s, MO_8, temp, s->A0);
191 tcg_gen_st8_tl(temp, tcg_env, dest_ofs);
194 gen_op_ld_v(s, MO_16, temp, s->A0);
195 tcg_gen_st16_tl(temp, tcg_env, dest_ofs);
198 gen_op_ld_v(s, MO_32, temp, s->A0);
199 tcg_gen_st32_tl(temp, tcg_env, dest_ofs);
202 gen_ldq_env_A0(s, dest_ofs);
205 gen_ldo_env_A0(s, dest_ofs, aligned);
208 gen_ldy_env_A0(s, dest_ofs, aligned);
211 g_assert_not_reached();
215 static bool sse_needs_alignment(DisasContext *s, X86DecodedInsn *decode, MemOp ot)
217 switch (decode->e.vex_class) {
220 if ((s->prefix & PREFIX_VEX) ||
221 decode->e.vex_special == X86_VEX_SSEUnaligned) {
222 /* MOST legacy SSE instructions require aligned memory operands, but not all. */
234 static void gen_load(DisasContext *s, X86DecodedInsn *decode, int opn, TCGv v)
236 X86DecodedOp *op = &decode->op[opn];
242 tcg_gen_ld32u_tl(v, tcg_env,
243 offsetof(CPUX86State,segs[op->n].selector));
245 #ifndef CONFIG_USER_ONLY
248 translator_io_start(&s->base);
249 gen_helper_read_cr8(v, tcg_env);
251 tcg_gen_ld_tl(v, tcg_env, offsetof(CPUX86State, cr[op->n]));
255 /* CR4.DE tested in the helper. */
256 gen_helper_get_dr(v, tcg_env, tcg_constant_i32(op->n));
261 if (v == s->T0 && decode->e.special == X86_SPECIAL_SExtT0) {
262 gen_op_ld_v(s, op->ot | MO_SIGN, v, s->A0);
264 gen_op_ld_v(s, op->ot, v, s->A0);
267 } else if (op->ot == MO_8 && byte_reg_is_xH(s, op->n)) {
268 if (v == s->T0 && decode->e.special == X86_SPECIAL_SExtT0) {
269 tcg_gen_sextract_tl(v, cpu_regs[op->n - 4], 8, 8);
271 tcg_gen_extract_tl(v, cpu_regs[op->n - 4], 8, 8);
274 } else if (op->ot < MO_TL && v == s->T0 &&
275 (decode->e.special == X86_SPECIAL_SExtT0 ||
276 decode->e.special == X86_SPECIAL_ZExtT0)) {
277 if (decode->e.special == X86_SPECIAL_SExtT0) {
278 tcg_gen_ext_tl(v, cpu_regs[op->n], op->ot | MO_SIGN);
280 tcg_gen_ext_tl(v, cpu_regs[op->n], op->ot);
284 tcg_gen_mov_tl(v, cpu_regs[op->n]);
288 tcg_gen_movi_tl(v, op->imm);
292 compute_mmx_offset(op);
296 compute_xmm_offset(op);
299 bool aligned = sse_needs_alignment(s, decode, op->ot);
300 gen_load_sse(s, v, op->ot, op->offset, aligned);
305 g_assert_not_reached();
309 static TCGv_ptr op_ptr(X86DecodedInsn *decode, int opn)
311 X86DecodedOp *op = &decode->op[opn];
313 assert(op->unit == X86_OP_MMX || op->unit == X86_OP_SSE);
317 op->v_ptr = tcg_temp_new_ptr();
319 /* The temporary points to the MMXReg or ZMMReg. */
320 tcg_gen_addi_ptr(op->v_ptr, tcg_env, vector_reg_offset(op));
324 #define OP_PTR0 op_ptr(decode, 0)
325 #define OP_PTR1 op_ptr(decode, 1)
326 #define OP_PTR2 op_ptr(decode, 2)
328 static void gen_writeback(DisasContext *s, X86DecodedInsn *decode, int opn, TCGv v)
330 X86DecodedOp *op = &decode->op[opn];
335 /* Note that gen_movl_seg takes care of interrupt shadow and TF. */
336 gen_movl_seg(s, op->n, s->T0);
340 gen_op_st_v(s, op->ot, v, s->A0);
342 gen_op_mov_reg_v(s, op->ot, op->n, v);
348 if (!op->has_ea && (s->prefix & PREFIX_VEX) && op->ot <= MO_128) {
349 tcg_gen_gvec_dup_imm(MO_64,
350 offsetof(CPUX86State, xmm_regs[op->n].ZMM_X(1)),
354 #ifndef CONFIG_USER_ONLY
357 translator_io_start(&s->base);
359 gen_helper_write_crN(tcg_env, tcg_constant_i32(op->n), v);
360 s->base.is_jmp = DISAS_EOB_NEXT;
363 /* CR4.DE tested in the helper. */
364 gen_helper_set_dr(tcg_env, tcg_constant_i32(op->n), v);
365 s->base.is_jmp = DISAS_EOB_NEXT;
369 g_assert_not_reached();
371 op->unit = X86_OP_SKIP;
374 static inline int vector_len(DisasContext *s, X86DecodedInsn *decode)
376 if (decode->e.special == X86_SPECIAL_MMX &&
377 !(s->prefix & (PREFIX_DATA | PREFIX_REPZ | PREFIX_REPNZ))) {
380 return s->vex_l ? 32 : 16;
383 static void prepare_update1_cc(X86DecodedInsn *decode, DisasContext *s, CCOp op)
385 decode->cc_dst = s->T0;
389 static void prepare_update2_cc(X86DecodedInsn *decode, DisasContext *s, CCOp op)
391 decode->cc_src = s->T1;
392 decode->cc_dst = s->T0;
396 static void prepare_update_cc_incdec(X86DecodedInsn *decode, DisasContext *s, CCOp op)
398 gen_compute_eflags_c(s, s->T1);
399 prepare_update2_cc(decode, s, op);
402 static void prepare_update3_cc(X86DecodedInsn *decode, DisasContext *s, CCOp op, TCGv reg)
404 decode->cc_src2 = reg;
405 decode->cc_src = s->T1;
406 decode->cc_dst = s->T0;
410 static void gen_store_sse(DisasContext *s, X86DecodedInsn *decode, int src_ofs)
412 MemOp ot = decode->op[0].ot;
413 int vec_len = vector_len(s, decode);
414 bool aligned = sse_needs_alignment(s, decode, ot);
416 if (!decode->op[0].has_ea) {
417 tcg_gen_gvec_mov(MO_64, decode->op[0].offset, src_ofs, vec_len, vec_len);
423 gen_stq_env_A0(s, src_ofs);
426 gen_sto_env_A0(s, src_ofs, aligned);
429 gen_sty_env_A0(s, src_ofs, aligned);
432 g_assert_not_reached();
436 static void gen_helper_pavgusb(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b)
438 gen_helper_pavgb_mmx(env, reg_a, reg_a, reg_b);
441 #define FN_3DNOW_MOVE ((SSEFunc_0_epp) (uintptr_t) 1)
442 static const SSEFunc_0_epp fns_3dnow[] = {
443 [0x0c] = gen_helper_pi2fw,
444 [0x0d] = gen_helper_pi2fd,
445 [0x1c] = gen_helper_pf2iw,
446 [0x1d] = gen_helper_pf2id,
447 [0x8a] = gen_helper_pfnacc,
448 [0x8e] = gen_helper_pfpnacc,
449 [0x90] = gen_helper_pfcmpge,
450 [0x94] = gen_helper_pfmin,
451 [0x96] = gen_helper_pfrcp,
452 [0x97] = gen_helper_pfrsqrt,
453 [0x9a] = gen_helper_pfsub,
454 [0x9e] = gen_helper_pfadd,
455 [0xa0] = gen_helper_pfcmpgt,
456 [0xa4] = gen_helper_pfmax,
457 [0xa6] = FN_3DNOW_MOVE, /* PFRCPIT1; no need to actually increase precision */
458 [0xa7] = FN_3DNOW_MOVE, /* PFRSQIT1 */
459 [0xb6] = FN_3DNOW_MOVE, /* PFRCPIT2 */
460 [0xaa] = gen_helper_pfsubr,
461 [0xae] = gen_helper_pfacc,
462 [0xb0] = gen_helper_pfcmpeq,
463 [0xb4] = gen_helper_pfmul,
464 [0xb7] = gen_helper_pmulhrw_mmx,
465 [0xbb] = gen_helper_pswapd,
466 [0xbf] = gen_helper_pavgusb,
469 static void gen_3dnow(DisasContext *s, X86DecodedInsn *decode)
471 uint8_t b = decode->immediate;
472 SSEFunc_0_epp fn = b < ARRAY_SIZE(fns_3dnow) ? fns_3dnow[b] : NULL;
475 gen_illegal_opcode(s);
478 if (s->flags & HF_TS_MASK) {
482 if (s->flags & HF_EM_MASK) {
483 gen_illegal_opcode(s);
487 gen_helper_enter_mmx(tcg_env);
488 if (fn == FN_3DNOW_MOVE) {
489 tcg_gen_ld_i64(s->tmp1_i64, tcg_env, decode->op[1].offset);
490 tcg_gen_st_i64(s->tmp1_i64, tcg_env, decode->op[0].offset);
492 fn(tcg_env, OP_PTR0, OP_PTR1);
497 * 00 = v*ps Vps, Hps, Wpd
498 * 66 = v*pd Vpd, Hpd, Wps
499 * f3 = v*ss Vss, Hss, Wps
500 * f2 = v*sd Vsd, Hsd, Wps
502 static inline void gen_unary_fp_sse(DisasContext *s, X86DecodedInsn *decode,
503 SSEFunc_0_epp pd_xmm, SSEFunc_0_epp ps_xmm,
504 SSEFunc_0_epp pd_ymm, SSEFunc_0_epp ps_ymm,
505 SSEFunc_0_eppp sd, SSEFunc_0_eppp ss)
507 if ((s->prefix & (PREFIX_REPZ | PREFIX_REPNZ)) != 0) {
508 SSEFunc_0_eppp fn = s->prefix & PREFIX_REPZ ? ss : sd;
510 gen_illegal_opcode(s);
513 fn(tcg_env, OP_PTR0, OP_PTR1, OP_PTR2);
515 SSEFunc_0_epp ps, pd, fn;
516 ps = s->vex_l ? ps_ymm : ps_xmm;
517 pd = s->vex_l ? pd_ymm : pd_xmm;
518 fn = s->prefix & PREFIX_DATA ? pd : ps;
520 gen_illegal_opcode(s);
523 fn(tcg_env, OP_PTR0, OP_PTR2);
526 #define UNARY_FP_SSE(uname, lname) \
527 static void gen_##uname(DisasContext *s, X86DecodedInsn *decode) \
529 gen_unary_fp_sse(s, decode, \
530 gen_helper_##lname##pd_xmm, \
531 gen_helper_##lname##ps_xmm, \
532 gen_helper_##lname##pd_ymm, \
533 gen_helper_##lname##ps_ymm, \
534 gen_helper_##lname##sd, \
535 gen_helper_##lname##ss); \
537 UNARY_FP_SSE(VSQRT, sqrt)
540 * 00 = v*ps Vps, Hps, Wpd
541 * 66 = v*pd Vpd, Hpd, Wps
542 * f3 = v*ss Vss, Hss, Wps
543 * f2 = v*sd Vsd, Hsd, Wps
545 static inline void gen_fp_sse(DisasContext *s, X86DecodedInsn *decode,
546 SSEFunc_0_eppp pd_xmm, SSEFunc_0_eppp ps_xmm,
547 SSEFunc_0_eppp pd_ymm, SSEFunc_0_eppp ps_ymm,
548 SSEFunc_0_eppp sd, SSEFunc_0_eppp ss)
550 SSEFunc_0_eppp ps, pd, fn;
551 if ((s->prefix & (PREFIX_REPZ | PREFIX_REPNZ)) != 0) {
552 fn = s->prefix & PREFIX_REPZ ? ss : sd;
554 ps = s->vex_l ? ps_ymm : ps_xmm;
555 pd = s->vex_l ? pd_ymm : pd_xmm;
556 fn = s->prefix & PREFIX_DATA ? pd : ps;
559 fn(tcg_env, OP_PTR0, OP_PTR1, OP_PTR2);
561 gen_illegal_opcode(s);
565 #define FP_SSE(uname, lname) \
566 static void gen_##uname(DisasContext *s, X86DecodedInsn *decode) \
568 gen_fp_sse(s, decode, \
569 gen_helper_##lname##pd_xmm, \
570 gen_helper_##lname##ps_xmm, \
571 gen_helper_##lname##pd_ymm, \
572 gen_helper_##lname##ps_ymm, \
573 gen_helper_##lname##sd, \
574 gen_helper_##lname##ss); \
583 #define FMA_SSE_PACKED(uname, ptr0, ptr1, ptr2, even, odd) \
584 static void gen_##uname##Px(DisasContext *s, X86DecodedInsn *decode) \
586 SSEFunc_0_eppppii xmm = s->vex_w ? gen_helper_fma4pd_xmm : gen_helper_fma4ps_xmm; \
587 SSEFunc_0_eppppii ymm = s->vex_w ? gen_helper_fma4pd_ymm : gen_helper_fma4ps_ymm; \
588 SSEFunc_0_eppppii fn = s->vex_l ? ymm : xmm; \
590 fn(tcg_env, OP_PTR0, ptr0, ptr1, ptr2, \
591 tcg_constant_i32(even), \
592 tcg_constant_i32((even) ^ (odd))); \
595 #define FMA_SSE(uname, ptr0, ptr1, ptr2, flags) \
596 FMA_SSE_PACKED(uname, ptr0, ptr1, ptr2, flags, flags) \
597 static void gen_##uname##Sx(DisasContext *s, X86DecodedInsn *decode) \
599 SSEFunc_0_eppppi fn = s->vex_w ? gen_helper_fma4sd : gen_helper_fma4ss; \
601 fn(tcg_env, OP_PTR0, ptr0, ptr1, ptr2, \
602 tcg_constant_i32(flags)); \
605 FMA_SSE(VFMADD231, OP_PTR1, OP_PTR2, OP_PTR0, 0)
606 FMA_SSE(VFMADD213, OP_PTR1, OP_PTR0, OP_PTR2, 0)
607 FMA_SSE(VFMADD132, OP_PTR0, OP_PTR2, OP_PTR1, 0)
609 FMA_SSE(VFNMADD231, OP_PTR1, OP_PTR2, OP_PTR0, float_muladd_negate_product)
610 FMA_SSE(VFNMADD213, OP_PTR1, OP_PTR0, OP_PTR2, float_muladd_negate_product)
611 FMA_SSE(VFNMADD132, OP_PTR0, OP_PTR2, OP_PTR1, float_muladd_negate_product)
613 FMA_SSE(VFMSUB231, OP_PTR1, OP_PTR2, OP_PTR0, float_muladd_negate_c)
614 FMA_SSE(VFMSUB213, OP_PTR1, OP_PTR0, OP_PTR2, float_muladd_negate_c)
615 FMA_SSE(VFMSUB132, OP_PTR0, OP_PTR2, OP_PTR1, float_muladd_negate_c)
617 FMA_SSE(VFNMSUB231, OP_PTR1, OP_PTR2, OP_PTR0, float_muladd_negate_c|float_muladd_negate_product)
618 FMA_SSE(VFNMSUB213, OP_PTR1, OP_PTR0, OP_PTR2, float_muladd_negate_c|float_muladd_negate_product)
619 FMA_SSE(VFNMSUB132, OP_PTR0, OP_PTR2, OP_PTR1, float_muladd_negate_c|float_muladd_negate_product)
621 FMA_SSE_PACKED(VFMADDSUB231, OP_PTR1, OP_PTR2, OP_PTR0, float_muladd_negate_c, 0)
622 FMA_SSE_PACKED(VFMADDSUB213, OP_PTR1, OP_PTR0, OP_PTR2, float_muladd_negate_c, 0)
623 FMA_SSE_PACKED(VFMADDSUB132, OP_PTR0, OP_PTR2, OP_PTR1, float_muladd_negate_c, 0)
625 FMA_SSE_PACKED(VFMSUBADD231, OP_PTR1, OP_PTR2, OP_PTR0, 0, float_muladd_negate_c)
626 FMA_SSE_PACKED(VFMSUBADD213, OP_PTR1, OP_PTR0, OP_PTR2, 0, float_muladd_negate_c)
627 FMA_SSE_PACKED(VFMSUBADD132, OP_PTR0, OP_PTR2, OP_PTR1, 0, float_muladd_negate_c)
629 #define FP_UNPACK_SSE(uname, lname) \
630 static void gen_##uname(DisasContext *s, X86DecodedInsn *decode) \
632 /* PS maps to the DQ integer instruction, PD maps to QDQ. */ \
633 gen_fp_sse(s, decode, \
634 gen_helper_##lname##qdq_xmm, \
635 gen_helper_##lname##dq_xmm, \
636 gen_helper_##lname##qdq_ymm, \
637 gen_helper_##lname##dq_ymm, \
640 FP_UNPACK_SSE(VUNPCKLPx, punpckl)
641 FP_UNPACK_SSE(VUNPCKHPx, punpckh)
647 static inline void gen_unary_fp32_sse(DisasContext *s, X86DecodedInsn *decode,
648 SSEFunc_0_epp ps_xmm,
649 SSEFunc_0_epp ps_ymm,
652 if ((s->prefix & (PREFIX_DATA | PREFIX_REPNZ)) != 0) {
654 } else if (s->prefix & PREFIX_REPZ) {
658 ss(tcg_env, OP_PTR0, OP_PTR1, OP_PTR2);
660 SSEFunc_0_epp fn = s->vex_l ? ps_ymm : ps_xmm;
664 fn(tcg_env, OP_PTR0, OP_PTR2);
669 gen_illegal_opcode(s);
671 #define UNARY_FP32_SSE(uname, lname) \
672 static void gen_##uname(DisasContext *s, X86DecodedInsn *decode) \
674 gen_unary_fp32_sse(s, decode, \
675 gen_helper_##lname##ps_xmm, \
676 gen_helper_##lname##ps_ymm, \
677 gen_helper_##lname##ss); \
679 UNARY_FP32_SSE(VRSQRT, rsqrt)
680 UNARY_FP32_SSE(VRCP, rcp)
683 * 66 = v*pd Vpd, Hpd, Wpd
684 * f2 = v*ps Vps, Hps, Wps
686 static inline void gen_horizontal_fp_sse(DisasContext *s, X86DecodedInsn *decode,
687 SSEFunc_0_eppp pd_xmm, SSEFunc_0_eppp ps_xmm,
688 SSEFunc_0_eppp pd_ymm, SSEFunc_0_eppp ps_ymm)
690 SSEFunc_0_eppp ps, pd, fn;
691 ps = s->vex_l ? ps_ymm : ps_xmm;
692 pd = s->vex_l ? pd_ymm : pd_xmm;
693 fn = s->prefix & PREFIX_DATA ? pd : ps;
694 fn(tcg_env, OP_PTR0, OP_PTR1, OP_PTR2);
696 #define HORIZONTAL_FP_SSE(uname, lname) \
697 static void gen_##uname(DisasContext *s, X86DecodedInsn *decode) \
699 gen_horizontal_fp_sse(s, decode, \
700 gen_helper_##lname##pd_xmm, gen_helper_##lname##ps_xmm, \
701 gen_helper_##lname##pd_ymm, gen_helper_##lname##ps_ymm); \
703 HORIZONTAL_FP_SSE(VHADD, hadd)
704 HORIZONTAL_FP_SSE(VHSUB, hsub)
705 HORIZONTAL_FP_SSE(VADDSUB, addsub)
707 static inline void gen_ternary_sse(DisasContext *s, X86DecodedInsn *decode,
708 int op3, SSEFunc_0_epppp xmm, SSEFunc_0_epppp ymm)
710 SSEFunc_0_epppp fn = s->vex_l ? ymm : xmm;
711 TCGv_ptr ptr3 = tcg_temp_new_ptr();
713 /* The format of the fourth input is Lx */
714 tcg_gen_addi_ptr(ptr3, tcg_env, ZMM_OFFSET(op3));
715 fn(tcg_env, OP_PTR0, OP_PTR1, OP_PTR2, ptr3);
717 #define TERNARY_SSE(uname, uvname, lname) \
718 static void gen_##uvname(DisasContext *s, X86DecodedInsn *decode) \
720 gen_ternary_sse(s, decode, (uint8_t)decode->immediate >> 4, \
721 gen_helper_##lname##_xmm, gen_helper_##lname##_ymm); \
723 static void gen_##uname(DisasContext *s, X86DecodedInsn *decode) \
725 gen_ternary_sse(s, decode, 0, \
726 gen_helper_##lname##_xmm, gen_helper_##lname##_ymm); \
728 TERNARY_SSE(BLENDVPS, VBLENDVPS, blendvps)
729 TERNARY_SSE(BLENDVPD, VBLENDVPD, blendvpd)
730 TERNARY_SSE(PBLENDVB, VPBLENDVB, pblendvb)
732 static inline void gen_binary_imm_sse(DisasContext *s, X86DecodedInsn *decode,
733 SSEFunc_0_epppi xmm, SSEFunc_0_epppi ymm)
735 TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
737 xmm(tcg_env, OP_PTR0, OP_PTR1, OP_PTR2, imm);
739 ymm(tcg_env, OP_PTR0, OP_PTR1, OP_PTR2, imm);
743 #define BINARY_IMM_SSE(uname, lname) \
744 static void gen_##uname(DisasContext *s, X86DecodedInsn *decode) \
746 gen_binary_imm_sse(s, decode, \
747 gen_helper_##lname##_xmm, \
748 gen_helper_##lname##_ymm); \
751 BINARY_IMM_SSE(VBLENDPD, blendpd)
752 BINARY_IMM_SSE(VBLENDPS, blendps)
753 BINARY_IMM_SSE(VPBLENDW, pblendw)
754 BINARY_IMM_SSE(VDDPS, dpps)
755 #define gen_helper_dppd_ymm NULL
756 BINARY_IMM_SSE(VDDPD, dppd)
757 BINARY_IMM_SSE(VMPSADBW, mpsadbw)
758 BINARY_IMM_SSE(PCLMULQDQ, pclmulqdq)
761 #define UNARY_INT_GVEC(uname, func, ...) \
762 static void gen_##uname(DisasContext *s, X86DecodedInsn *decode) \
764 int vec_len = vector_len(s, decode); \
766 func(__VA_ARGS__, decode->op[0].offset, \
767 decode->op[2].offset, vec_len, vec_len); \
769 UNARY_INT_GVEC(PABSB, tcg_gen_gvec_abs, MO_8)
770 UNARY_INT_GVEC(PABSW, tcg_gen_gvec_abs, MO_16)
771 UNARY_INT_GVEC(PABSD, tcg_gen_gvec_abs, MO_32)
772 UNARY_INT_GVEC(VBROADCASTx128, tcg_gen_gvec_dup_mem, MO_128)
773 UNARY_INT_GVEC(VPBROADCASTB, tcg_gen_gvec_dup_mem, MO_8)
774 UNARY_INT_GVEC(VPBROADCASTW, tcg_gen_gvec_dup_mem, MO_16)
775 UNARY_INT_GVEC(VPBROADCASTD, tcg_gen_gvec_dup_mem, MO_32)
776 UNARY_INT_GVEC(VPBROADCASTQ, tcg_gen_gvec_dup_mem, MO_64)
779 #define BINARY_INT_GVEC(uname, func, ...) \
780 static void gen_##uname(DisasContext *s, X86DecodedInsn *decode) \
782 int vec_len = vector_len(s, decode); \
785 decode->op[0].offset, decode->op[1].offset, \
786 decode->op[2].offset, vec_len, vec_len); \
789 BINARY_INT_GVEC(PADDB, tcg_gen_gvec_add, MO_8)
790 BINARY_INT_GVEC(PADDW, tcg_gen_gvec_add, MO_16)
791 BINARY_INT_GVEC(PADDD, tcg_gen_gvec_add, MO_32)
792 BINARY_INT_GVEC(PADDQ, tcg_gen_gvec_add, MO_64)
793 BINARY_INT_GVEC(PADDSB, tcg_gen_gvec_ssadd, MO_8)
794 BINARY_INT_GVEC(PADDSW, tcg_gen_gvec_ssadd, MO_16)
795 BINARY_INT_GVEC(PADDUSB, tcg_gen_gvec_usadd, MO_8)
796 BINARY_INT_GVEC(PADDUSW, tcg_gen_gvec_usadd, MO_16)
797 BINARY_INT_GVEC(PAND, tcg_gen_gvec_and, MO_64)
798 BINARY_INT_GVEC(PCMPEQB, tcg_gen_gvec_cmp, TCG_COND_EQ, MO_8)
799 BINARY_INT_GVEC(PCMPEQD, tcg_gen_gvec_cmp, TCG_COND_EQ, MO_32)
800 BINARY_INT_GVEC(PCMPEQW, tcg_gen_gvec_cmp, TCG_COND_EQ, MO_16)
801 BINARY_INT_GVEC(PCMPEQQ, tcg_gen_gvec_cmp, TCG_COND_EQ, MO_64)
802 BINARY_INT_GVEC(PCMPGTB, tcg_gen_gvec_cmp, TCG_COND_GT, MO_8)
803 BINARY_INT_GVEC(PCMPGTW, tcg_gen_gvec_cmp, TCG_COND_GT, MO_16)
804 BINARY_INT_GVEC(PCMPGTD, tcg_gen_gvec_cmp, TCG_COND_GT, MO_32)
805 BINARY_INT_GVEC(PCMPGTQ, tcg_gen_gvec_cmp, TCG_COND_GT, MO_64)
806 BINARY_INT_GVEC(PMAXSB, tcg_gen_gvec_smax, MO_8)
807 BINARY_INT_GVEC(PMAXSW, tcg_gen_gvec_smax, MO_16)
808 BINARY_INT_GVEC(PMAXSD, tcg_gen_gvec_smax, MO_32)
809 BINARY_INT_GVEC(PMAXUB, tcg_gen_gvec_umax, MO_8)
810 BINARY_INT_GVEC(PMAXUW, tcg_gen_gvec_umax, MO_16)
811 BINARY_INT_GVEC(PMAXUD, tcg_gen_gvec_umax, MO_32)
812 BINARY_INT_GVEC(PMINSB, tcg_gen_gvec_smin, MO_8)
813 BINARY_INT_GVEC(PMINSW, tcg_gen_gvec_smin, MO_16)
814 BINARY_INT_GVEC(PMINSD, tcg_gen_gvec_smin, MO_32)
815 BINARY_INT_GVEC(PMINUB, tcg_gen_gvec_umin, MO_8)
816 BINARY_INT_GVEC(PMINUW, tcg_gen_gvec_umin, MO_16)
817 BINARY_INT_GVEC(PMINUD, tcg_gen_gvec_umin, MO_32)
818 BINARY_INT_GVEC(PMULLW, tcg_gen_gvec_mul, MO_16)
819 BINARY_INT_GVEC(PMULLD, tcg_gen_gvec_mul, MO_32)
820 BINARY_INT_GVEC(POR, tcg_gen_gvec_or, MO_64)
821 BINARY_INT_GVEC(PSUBB, tcg_gen_gvec_sub, MO_8)
822 BINARY_INT_GVEC(PSUBW, tcg_gen_gvec_sub, MO_16)
823 BINARY_INT_GVEC(PSUBD, tcg_gen_gvec_sub, MO_32)
824 BINARY_INT_GVEC(PSUBQ, tcg_gen_gvec_sub, MO_64)
825 BINARY_INT_GVEC(PSUBSB, tcg_gen_gvec_sssub, MO_8)
826 BINARY_INT_GVEC(PSUBSW, tcg_gen_gvec_sssub, MO_16)
827 BINARY_INT_GVEC(PSUBUSB, tcg_gen_gvec_ussub, MO_8)
828 BINARY_INT_GVEC(PSUBUSW, tcg_gen_gvec_ussub, MO_16)
829 BINARY_INT_GVEC(PXOR, tcg_gen_gvec_xor, MO_64)
833 * 00 = p* Pq, Qq (if mmx not NULL; no VEX)
834 * 66 = vp* Vx, Hx, Wx
836 * These are really the same encoding, because 1) V is the same as P when VEX.V
837 * is not present 2) P and Q are the same as H and W apart from MM/XMM
839 static inline void gen_binary_int_sse(DisasContext *s, X86DecodedInsn *decode,
840 SSEFunc_0_eppp mmx, SSEFunc_0_eppp xmm, SSEFunc_0_eppp ymm)
842 assert(!!mmx == !!(decode->e.special == X86_SPECIAL_MMX));
844 if (mmx && (s->prefix & PREFIX_VEX) && !(s->prefix & PREFIX_DATA)) {
845 /* VEX encoding is not applicable to MMX instructions. */
846 gen_illegal_opcode(s);
849 if (!(s->prefix & PREFIX_DATA)) {
850 mmx(tcg_env, OP_PTR0, OP_PTR1, OP_PTR2);
851 } else if (!s->vex_l) {
852 xmm(tcg_env, OP_PTR0, OP_PTR1, OP_PTR2);
854 ymm(tcg_env, OP_PTR0, OP_PTR1, OP_PTR2);
859 #define BINARY_INT_MMX(uname, lname) \
860 static void gen_##uname(DisasContext *s, X86DecodedInsn *decode) \
862 gen_binary_int_sse(s, decode, \
863 gen_helper_##lname##_mmx, \
864 gen_helper_##lname##_xmm, \
865 gen_helper_##lname##_ymm); \
867 BINARY_INT_MMX(PUNPCKLBW, punpcklbw)
868 BINARY_INT_MMX(PUNPCKLWD, punpcklwd)
869 BINARY_INT_MMX(PUNPCKLDQ, punpckldq)
870 BINARY_INT_MMX(PACKSSWB, packsswb)
871 BINARY_INT_MMX(PACKUSWB, packuswb)
872 BINARY_INT_MMX(PUNPCKHBW, punpckhbw)
873 BINARY_INT_MMX(PUNPCKHWD, punpckhwd)
874 BINARY_INT_MMX(PUNPCKHDQ, punpckhdq)
875 BINARY_INT_MMX(PACKSSDW, packssdw)
877 BINARY_INT_MMX(PAVGB, pavgb)
878 BINARY_INT_MMX(PAVGW, pavgw)
879 BINARY_INT_MMX(PMADDWD, pmaddwd)
880 BINARY_INT_MMX(PMULHUW, pmulhuw)
881 BINARY_INT_MMX(PMULHW, pmulhw)
882 BINARY_INT_MMX(PMULUDQ, pmuludq)
883 BINARY_INT_MMX(PSADBW, psadbw)
885 BINARY_INT_MMX(PSLLW_r, psllw)
886 BINARY_INT_MMX(PSLLD_r, pslld)
887 BINARY_INT_MMX(PSLLQ_r, psllq)
888 BINARY_INT_MMX(PSRLW_r, psrlw)
889 BINARY_INT_MMX(PSRLD_r, psrld)
890 BINARY_INT_MMX(PSRLQ_r, psrlq)
891 BINARY_INT_MMX(PSRAW_r, psraw)
892 BINARY_INT_MMX(PSRAD_r, psrad)
894 BINARY_INT_MMX(PHADDW, phaddw)
895 BINARY_INT_MMX(PHADDSW, phaddsw)
896 BINARY_INT_MMX(PHADDD, phaddd)
897 BINARY_INT_MMX(PHSUBW, phsubw)
898 BINARY_INT_MMX(PHSUBSW, phsubsw)
899 BINARY_INT_MMX(PHSUBD, phsubd)
900 BINARY_INT_MMX(PMADDUBSW, pmaddubsw)
901 BINARY_INT_MMX(PSHUFB, pshufb)
902 BINARY_INT_MMX(PSIGNB, psignb)
903 BINARY_INT_MMX(PSIGNW, psignw)
904 BINARY_INT_MMX(PSIGND, psignd)
905 BINARY_INT_MMX(PMULHRSW, pmulhrsw)
907 /* Instructions with no MMX equivalent. */
908 #define BINARY_INT_SSE(uname, lname) \
909 static void gen_##uname(DisasContext *s, X86DecodedInsn *decode) \
911 gen_binary_int_sse(s, decode, \
913 gen_helper_##lname##_xmm, \
914 gen_helper_##lname##_ymm); \
917 /* Instructions with no MMX equivalent. */
918 BINARY_INT_SSE(PUNPCKLQDQ, punpcklqdq)
919 BINARY_INT_SSE(PUNPCKHQDQ, punpckhqdq)
920 BINARY_INT_SSE(VPACKUSDW, packusdw)
921 BINARY_INT_SSE(VPERMILPS, vpermilps)
922 BINARY_INT_SSE(VPERMILPD, vpermilpd)
923 BINARY_INT_SSE(VMASKMOVPS, vpmaskmovd)
924 BINARY_INT_SSE(VMASKMOVPD, vpmaskmovq)
926 BINARY_INT_SSE(PMULDQ, pmuldq)
928 BINARY_INT_SSE(VAESDEC, aesdec)
929 BINARY_INT_SSE(VAESDECLAST, aesdeclast)
930 BINARY_INT_SSE(VAESENC, aesenc)
931 BINARY_INT_SSE(VAESENCLAST, aesenclast)
933 #define UNARY_CMP_SSE(uname, lname) \
934 static void gen_##uname(DisasContext *s, X86DecodedInsn *decode) \
937 gen_helper_##lname##_xmm(tcg_env, OP_PTR1, OP_PTR2); \
939 gen_helper_##lname##_ymm(tcg_env, OP_PTR1, OP_PTR2); \
941 assume_cc_op(s, CC_OP_EFLAGS); \
943 UNARY_CMP_SSE(VPTEST, ptest)
944 UNARY_CMP_SSE(VTESTPS, vtestps)
945 UNARY_CMP_SSE(VTESTPD, vtestpd)
947 static inline void gen_unary_int_sse(DisasContext *s, X86DecodedInsn *decode,
948 SSEFunc_0_epp xmm, SSEFunc_0_epp ymm)
951 xmm(tcg_env, OP_PTR0, OP_PTR2);
953 ymm(tcg_env, OP_PTR0, OP_PTR2);
957 #define UNARY_INT_SSE(uname, lname) \
958 static void gen_##uname(DisasContext *s, X86DecodedInsn *decode) \
960 gen_unary_int_sse(s, decode, \
961 gen_helper_##lname##_xmm, \
962 gen_helper_##lname##_ymm); \
965 UNARY_INT_SSE(VPMOVSXBW, pmovsxbw)
966 UNARY_INT_SSE(VPMOVSXBD, pmovsxbd)
967 UNARY_INT_SSE(VPMOVSXBQ, pmovsxbq)
968 UNARY_INT_SSE(VPMOVSXWD, pmovsxwd)
969 UNARY_INT_SSE(VPMOVSXWQ, pmovsxwq)
970 UNARY_INT_SSE(VPMOVSXDQ, pmovsxdq)
972 UNARY_INT_SSE(VPMOVZXBW, pmovzxbw)
973 UNARY_INT_SSE(VPMOVZXBD, pmovzxbd)
974 UNARY_INT_SSE(VPMOVZXBQ, pmovzxbq)
975 UNARY_INT_SSE(VPMOVZXWD, pmovzxwd)
976 UNARY_INT_SSE(VPMOVZXWQ, pmovzxwq)
977 UNARY_INT_SSE(VPMOVZXDQ, pmovzxdq)
979 UNARY_INT_SSE(VMOVSLDUP, pmovsldup)
980 UNARY_INT_SSE(VMOVSHDUP, pmovshdup)
981 UNARY_INT_SSE(VMOVDDUP, pmovdldup)
983 UNARY_INT_SSE(VCVTDQ2PD, cvtdq2pd)
984 UNARY_INT_SSE(VCVTPD2DQ, cvtpd2dq)
985 UNARY_INT_SSE(VCVTTPD2DQ, cvttpd2dq)
986 UNARY_INT_SSE(VCVTDQ2PS, cvtdq2ps)
987 UNARY_INT_SSE(VCVTPS2DQ, cvtps2dq)
988 UNARY_INT_SSE(VCVTTPS2DQ, cvttps2dq)
989 UNARY_INT_SSE(VCVTPH2PS, cvtph2ps)
992 static inline void gen_unary_imm_sse(DisasContext *s, X86DecodedInsn *decode,
993 SSEFunc_0_ppi xmm, SSEFunc_0_ppi ymm)
995 TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
997 xmm(OP_PTR0, OP_PTR1, imm);
999 ymm(OP_PTR0, OP_PTR1, imm);
1003 #define UNARY_IMM_SSE(uname, lname) \
1004 static void gen_##uname(DisasContext *s, X86DecodedInsn *decode) \
1006 gen_unary_imm_sse(s, decode, \
1007 gen_helper_##lname##_xmm, \
1008 gen_helper_##lname##_ymm); \
1011 UNARY_IMM_SSE(PSHUFD, pshufd)
1012 UNARY_IMM_SSE(PSHUFHW, pshufhw)
1013 UNARY_IMM_SSE(PSHUFLW, pshuflw)
1014 #define gen_helper_vpermq_xmm NULL
1015 UNARY_IMM_SSE(VPERMQ, vpermq)
1016 UNARY_IMM_SSE(VPERMILPS_i, vpermilps_imm)
1017 UNARY_IMM_SSE(VPERMILPD_i, vpermilpd_imm)
1019 static inline void gen_unary_imm_fp_sse(DisasContext *s, X86DecodedInsn *decode,
1020 SSEFunc_0_eppi xmm, SSEFunc_0_eppi ymm)
1022 TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
1024 xmm(tcg_env, OP_PTR0, OP_PTR1, imm);
1026 ymm(tcg_env, OP_PTR0, OP_PTR1, imm);
1030 #define UNARY_IMM_FP_SSE(uname, lname) \
1031 static void gen_##uname(DisasContext *s, X86DecodedInsn *decode) \
1033 gen_unary_imm_fp_sse(s, decode, \
1034 gen_helper_##lname##_xmm, \
1035 gen_helper_##lname##_ymm); \
1038 UNARY_IMM_FP_SSE(VROUNDPS, roundps)
1039 UNARY_IMM_FP_SSE(VROUNDPD, roundpd)
1041 static inline void gen_vexw_avx(DisasContext *s, X86DecodedInsn *decode,
1042 SSEFunc_0_eppp d_xmm, SSEFunc_0_eppp q_xmm,
1043 SSEFunc_0_eppp d_ymm, SSEFunc_0_eppp q_ymm)
1045 SSEFunc_0_eppp d = s->vex_l ? d_ymm : d_xmm;
1046 SSEFunc_0_eppp q = s->vex_l ? q_ymm : q_xmm;
1047 SSEFunc_0_eppp fn = s->vex_w ? q : d;
1048 fn(tcg_env, OP_PTR0, OP_PTR1, OP_PTR2);
1051 /* VEX.W affects whether to operate on 32- or 64-bit elements. */
1052 #define VEXW_AVX(uname, lname) \
1053 static void gen_##uname(DisasContext *s, X86DecodedInsn *decode) \
1055 gen_vexw_avx(s, decode, \
1056 gen_helper_##lname##d_xmm, gen_helper_##lname##q_xmm, \
1057 gen_helper_##lname##d_ymm, gen_helper_##lname##q_ymm); \
1059 VEXW_AVX(VPSLLV, vpsllv)
1060 VEXW_AVX(VPSRLV, vpsrlv)
1061 VEXW_AVX(VPSRAV, vpsrav)
1062 VEXW_AVX(VPMASKMOV, vpmaskmov)
1064 /* Same as above, but with extra arguments to the helper. */
1065 static inline void gen_vsib_avx(DisasContext *s, X86DecodedInsn *decode,
1066 SSEFunc_0_epppti d_xmm, SSEFunc_0_epppti q_xmm,
1067 SSEFunc_0_epppti d_ymm, SSEFunc_0_epppti q_ymm)
1069 SSEFunc_0_epppti d = s->vex_l ? d_ymm : d_xmm;
1070 SSEFunc_0_epppti q = s->vex_l ? q_ymm : q_xmm;
1071 SSEFunc_0_epppti fn = s->vex_w ? q : d;
1072 TCGv_i32 scale = tcg_constant_i32(decode->mem.scale);
1073 TCGv_ptr index = tcg_temp_new_ptr();
1075 /* Pass third input as (index, base, scale) */
1076 tcg_gen_addi_ptr(index, tcg_env, ZMM_OFFSET(decode->mem.index));
1077 fn(tcg_env, OP_PTR0, OP_PTR1, index, s->A0, scale);
1080 * There are two output operands, so zero OP1's high 128 bits
1081 * in the VEX.128 case.
1084 int ymmh_ofs = vector_elem_offset(&decode->op[1], MO_128, 1);
1085 tcg_gen_gvec_dup_imm(MO_64, ymmh_ofs, 16, 16, 0);
1088 #define VSIB_AVX(uname, lname) \
1089 static void gen_##uname(DisasContext *s, X86DecodedInsn *decode) \
1091 gen_vsib_avx(s, decode, \
1092 gen_helper_##lname##d_xmm, gen_helper_##lname##q_xmm, \
1093 gen_helper_##lname##d_ymm, gen_helper_##lname##q_ymm); \
1095 VSIB_AVX(VPGATHERD, vpgatherd)
1096 VSIB_AVX(VPGATHERQ, vpgatherq)
1098 static void gen_AAA(DisasContext *s, X86DecodedInsn *decode)
1100 gen_update_cc_op(s);
1101 gen_helper_aaa(tcg_env);
1102 assume_cc_op(s, CC_OP_EFLAGS);
1105 static void gen_AAD(DisasContext *s, X86DecodedInsn *decode)
1107 gen_helper_aad(s->T0, s->T0, s->T1);
1108 prepare_update1_cc(decode, s, CC_OP_LOGICB);
1111 static void gen_AAM(DisasContext *s, X86DecodedInsn *decode)
1113 if (decode->immediate == 0) {
1114 gen_exception(s, EXCP00_DIVZ);
1116 gen_helper_aam(s->T0, s->T0, s->T1);
1117 prepare_update1_cc(decode, s, CC_OP_LOGICB);
1121 static void gen_AAS(DisasContext *s, X86DecodedInsn *decode)
1123 gen_update_cc_op(s);
1124 gen_helper_aas(tcg_env);
1125 assume_cc_op(s, CC_OP_EFLAGS);
1128 static void gen_ADC(DisasContext *s, X86DecodedInsn *decode)
1130 MemOp ot = decode->op[1].ot;
1131 TCGv c_in = tcg_temp_new();
1133 gen_compute_eflags_c(s, c_in);
1134 if (s->prefix & PREFIX_LOCK) {
1135 tcg_gen_add_tl(s->T0, c_in, s->T1);
1136 tcg_gen_atomic_add_fetch_tl(s->T0, s->A0, s->T0,
1137 s->mem_index, ot | MO_LE);
1139 tcg_gen_add_tl(s->T0, s->T0, s->T1);
1140 tcg_gen_add_tl(s->T0, s->T0, c_in);
1142 prepare_update3_cc(decode, s, CC_OP_ADCB + ot, c_in);
1145 static void gen_ADCOX(DisasContext *s, X86DecodedInsn *decode, int cc_op)
1147 MemOp ot = decode->op[0].ot;
1148 TCGv carry_in = NULL;
1149 TCGv *carry_out = (cc_op == CC_OP_ADCX ? &decode->cc_dst : &decode->cc_src2);
1152 decode->cc_op = cc_op;
1153 *carry_out = tcg_temp_new();
1154 if (CC_OP_HAS_EFLAGS(s->cc_op)) {
1155 decode->cc_src = cpu_cc_src;
1157 /* Re-use the carry-out from a previous round? */
1158 if (s->cc_op == cc_op || s->cc_op == CC_OP_ADCOX) {
1159 carry_in = (cc_op == CC_OP_ADCX ? cpu_cc_dst : cpu_cc_src2);
1162 /* Preserve the opposite carry from previous rounds? */
1163 if (s->cc_op != cc_op && s->cc_op != CC_OP_EFLAGS) {
1164 decode->cc_op = CC_OP_ADCOX;
1165 if (carry_out == &decode->cc_dst) {
1166 decode->cc_src2 = cpu_cc_src2;
1168 decode->cc_dst = cpu_cc_dst;
1172 decode->cc_src = tcg_temp_new();
1173 gen_mov_eflags(s, decode->cc_src);
1177 /* Get carry_in out of EFLAGS. */
1178 carry_in = tcg_temp_new();
1179 tcg_gen_extract_tl(carry_in, decode->cc_src,
1180 ctz32(cc_op == CC_OP_ADCX ? CC_C : CC_O), 1);
1184 #ifdef TARGET_X86_64
1186 /* If TL is 64-bit just do everything in 64-bit arithmetic. */
1187 tcg_gen_ext32u_tl(s->T0, s->T0);
1188 tcg_gen_ext32u_tl(s->T1, s->T1);
1189 tcg_gen_add_i64(s->T0, s->T0, s->T1);
1190 tcg_gen_add_i64(s->T0, s->T0, carry_in);
1191 tcg_gen_shri_i64(*carry_out, s->T0, 32);
1195 zero = tcg_constant_tl(0);
1196 tcg_gen_add2_tl(s->T0, *carry_out, s->T0, zero, carry_in, zero);
1197 tcg_gen_add2_tl(s->T0, *carry_out, s->T0, *carry_out, s->T1, zero);
1202 static void gen_ADCX(DisasContext *s, X86DecodedInsn *decode)
1204 gen_ADCOX(s, decode, CC_OP_ADCX);
1207 static void gen_ADD(DisasContext *s, X86DecodedInsn *decode)
1209 MemOp ot = decode->op[1].ot;
1211 if (s->prefix & PREFIX_LOCK) {
1212 tcg_gen_atomic_add_fetch_tl(s->T0, s->A0, s->T1,
1213 s->mem_index, ot | MO_LE);
1215 tcg_gen_add_tl(s->T0, s->T0, s->T1);
1217 prepare_update2_cc(decode, s, CC_OP_ADDB + ot);
1220 static void gen_ADOX(DisasContext *s, X86DecodedInsn *decode)
1222 gen_ADCOX(s, decode, CC_OP_ADOX);
1225 static void gen_AND(DisasContext *s, X86DecodedInsn *decode)
1227 MemOp ot = decode->op[1].ot;
1229 if (s->prefix & PREFIX_LOCK) {
1230 tcg_gen_atomic_and_fetch_tl(s->T0, s->A0, s->T1,
1231 s->mem_index, ot | MO_LE);
1233 tcg_gen_and_tl(s->T0, s->T0, s->T1);
1235 prepare_update1_cc(decode, s, CC_OP_LOGICB + ot);
1238 static void gen_ANDN(DisasContext *s, X86DecodedInsn *decode)
1240 MemOp ot = decode->op[0].ot;
1242 tcg_gen_andc_tl(s->T0, s->T1, s->T0);
1243 prepare_update1_cc(decode, s, CC_OP_LOGICB + ot);
1246 static void gen_ARPL(DisasContext *s, X86DecodedInsn *decode)
1248 TCGv zf = tcg_temp_new();
1249 TCGv flags = tcg_temp_new();
1251 gen_mov_eflags(s, flags);
1253 /* Compute adjusted DST in T1, merging in SRC[RPL]. */
1254 tcg_gen_deposit_tl(s->T1, s->T0, s->T1, 0, 2);
1256 /* Z flag set if DST[RPL] < SRC[RPL] */
1257 tcg_gen_setcond_tl(TCG_COND_LTU, zf, s->T0, s->T1);
1258 tcg_gen_deposit_tl(flags, flags, zf, ctz32(CC_Z), 1);
1260 /* Place maximum RPL in DST */
1261 tcg_gen_umax_tl(s->T0, s->T0, s->T1);
1263 decode->cc_src = flags;
1264 decode->cc_op = CC_OP_EFLAGS;
1267 static void gen_BEXTR(DisasContext *s, X86DecodedInsn *decode)
1269 MemOp ot = decode->op[0].ot;
1270 TCGv bound = tcg_constant_tl(ot == MO_64 ? 63 : 31);
1271 TCGv zero = tcg_constant_tl(0);
1272 TCGv mone = tcg_constant_tl(-1);
1275 * Extract START, and shift the operand.
1276 * Shifts larger than operand size get zeros.
1278 tcg_gen_ext8u_tl(s->A0, s->T1);
1279 tcg_gen_shr_tl(s->T0, s->T0, s->A0);
1281 tcg_gen_movcond_tl(TCG_COND_LEU, s->T0, s->A0, bound, s->T0, zero);
1284 * Extract the LEN into an inverse mask. Lengths larger than
1285 * operand size get all zeros, length 0 gets all ones.
1287 tcg_gen_extract_tl(s->A0, s->T1, 8, 8);
1288 tcg_gen_shl_tl(s->T1, mone, s->A0);
1289 tcg_gen_movcond_tl(TCG_COND_LEU, s->T1, s->A0, bound, s->T1, zero);
1290 tcg_gen_andc_tl(s->T0, s->T0, s->T1);
1292 prepare_update1_cc(decode, s, CC_OP_LOGICB + ot);
1295 static void gen_BLSI(DisasContext *s, X86DecodedInsn *decode)
1297 MemOp ot = decode->op[0].ot;
1299 /* input in T1, which is ready for prepare_update2_cc */
1300 tcg_gen_neg_tl(s->T0, s->T1);
1301 tcg_gen_and_tl(s->T0, s->T0, s->T1);
1302 prepare_update2_cc(decode, s, CC_OP_BMILGB + ot);
1305 static void gen_BLSMSK(DisasContext *s, X86DecodedInsn *decode)
1307 MemOp ot = decode->op[0].ot;
1309 /* input in T1, which is ready for prepare_update2_cc */
1310 tcg_gen_subi_tl(s->T0, s->T1, 1);
1311 tcg_gen_xor_tl(s->T0, s->T0, s->T1);
1312 prepare_update2_cc(decode, s, CC_OP_BMILGB + ot);
1315 static void gen_BLSR(DisasContext *s, X86DecodedInsn *decode)
1317 MemOp ot = decode->op[0].ot;
1319 /* input in T1, which is ready for prepare_update2_cc */
1320 tcg_gen_subi_tl(s->T0, s->T1, 1);
1321 tcg_gen_and_tl(s->T0, s->T0, s->T1);
1322 prepare_update2_cc(decode, s, CC_OP_BMILGB + ot);
1325 static void gen_BOUND(DisasContext *s, X86DecodedInsn *decode)
1327 TCGv_i32 op = tcg_temp_new_i32();
1328 tcg_gen_trunc_tl_i32(op, s->T0);
1329 if (decode->op[1].ot == MO_16) {
1330 gen_helper_boundw(tcg_env, s->A0, op);
1332 gen_helper_boundl(tcg_env, s->A0, op);
1336 /* Non-standard convention - on entry T0 is zero-extended input, T1 is the output. */
1337 static void gen_BSF(DisasContext *s, X86DecodedInsn *decode)
1339 MemOp ot = decode->op[0].ot;
1341 /* Only the Z bit is defined and it is related to the input. */
1342 decode->cc_dst = tcg_temp_new();
1343 decode->cc_op = CC_OP_LOGICB + ot;
1344 tcg_gen_mov_tl(decode->cc_dst, s->T0);
1347 * The manual says that the output is undefined when the
1348 * input is zero, but real hardware leaves it unchanged, and
1349 * real programs appear to depend on that. Accomplish this
1350 * by passing the output as the value to return upon zero.
1352 tcg_gen_ctz_tl(s->T0, s->T0, s->T1);
1355 /* Non-standard convention - on entry T0 is zero-extended input, T1 is the output. */
1356 static void gen_BSR(DisasContext *s, X86DecodedInsn *decode)
1358 MemOp ot = decode->op[0].ot;
1360 /* Only the Z bit is defined and it is related to the input. */
1361 decode->cc_dst = tcg_temp_new();
1362 decode->cc_op = CC_OP_LOGICB + ot;
1363 tcg_gen_mov_tl(decode->cc_dst, s->T0);
1366 * The manual says that the output is undefined when the
1367 * input is zero, but real hardware leaves it unchanged, and
1368 * real programs appear to depend on that. Accomplish this
1369 * by passing the output as the value to return upon zero.
1370 * Plus, return the bit index of the first 1 bit.
1372 tcg_gen_xori_tl(s->T1, s->T1, TARGET_LONG_BITS - 1);
1373 tcg_gen_clz_tl(s->T0, s->T0, s->T1);
1374 tcg_gen_xori_tl(s->T0, s->T0, TARGET_LONG_BITS - 1);
1377 static void gen_BSWAP(DisasContext *s, X86DecodedInsn *decode)
1379 #ifdef TARGET_X86_64
1380 if (s->dflag == MO_64) {
1381 tcg_gen_bswap64_i64(s->T0, s->T0);
1385 tcg_gen_bswap32_tl(s->T0, s->T0, TCG_BSWAP_OZ);
1388 static void gen_BZHI(DisasContext *s, X86DecodedInsn *decode)
1390 MemOp ot = decode->op[0].ot;
1391 TCGv bound = tcg_constant_tl(ot == MO_64 ? 63 : 31);
1392 TCGv zero = tcg_constant_tl(0);
1393 TCGv mone = tcg_constant_tl(-1);
1395 tcg_gen_ext8u_tl(s->T1, s->T1);
1397 tcg_gen_shl_tl(s->A0, mone, s->T1);
1398 tcg_gen_movcond_tl(TCG_COND_LEU, s->A0, s->T1, bound, s->A0, zero);
1399 tcg_gen_andc_tl(s->T0, s->T0, s->A0);
1401 * Note that since we're using BMILG (in order to get O
1402 * cleared) we need to store the inverse into C.
1404 tcg_gen_setcond_tl(TCG_COND_LEU, s->T1, s->T1, bound);
1405 prepare_update2_cc(decode, s, CC_OP_BMILGB + ot);
1408 static void gen_CALL(DisasContext *s, X86DecodedInsn *decode)
1410 gen_push_v(s, eip_next_tl(s));
1414 static void gen_CALL_m(DisasContext *s, X86DecodedInsn *decode)
1416 gen_push_v(s, eip_next_tl(s));
1417 gen_JMP_m(s, decode);
1420 static void gen_CALLF(DisasContext *s, X86DecodedInsn *decode)
1425 static void gen_CALLF_m(DisasContext *s, X86DecodedInsn *decode)
1427 MemOp ot = decode->op[1].ot;
1429 gen_op_ld_v(s, ot, s->T0, s->A0);
1430 gen_add_A0_im(s, 1 << ot);
1431 gen_op_ld_v(s, MO_16, s->T1, s->A0);
1435 static void gen_CBW(DisasContext *s, X86DecodedInsn *decode)
1437 MemOp src_ot = decode->op[0].ot - 1;
1439 tcg_gen_ext_tl(s->T0, s->T0, src_ot | MO_SIGN);
1442 static void gen_CLC(DisasContext *s, X86DecodedInsn *decode)
1444 gen_compute_eflags(s);
1445 tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~CC_C);
1448 static void gen_CLD(DisasContext *s, X86DecodedInsn *decode)
1450 tcg_gen_st_i32(tcg_constant_i32(1), tcg_env, offsetof(CPUX86State, df));
1453 static void gen_CLI(DisasContext *s, X86DecodedInsn *decode)
1455 gen_reset_eflags(s, IF_MASK);
1458 static void gen_CLTS(DisasContext *s, X86DecodedInsn *decode)
1460 gen_helper_clts(tcg_env);
1461 /* abort block because static cpu state changed */
1462 s->base.is_jmp = DISAS_EOB_NEXT;
1465 static void gen_CMC(DisasContext *s, X86DecodedInsn *decode)
1467 gen_compute_eflags(s);
1468 tcg_gen_xori_tl(cpu_cc_src, cpu_cc_src, CC_C);
1471 static void gen_CMOVcc(DisasContext *s, X86DecodedInsn *decode)
1473 gen_cmovcc1(s, decode->b & 0xf, s->T0, s->T1);
1476 static void gen_CMPccXADD(DisasContext *s, X86DecodedInsn *decode)
1478 TCGLabel *label_top = gen_new_label();
1479 TCGLabel *label_bottom = gen_new_label();
1480 TCGv oldv = tcg_temp_new();
1481 TCGv newv = tcg_temp_new();
1482 TCGv cmpv = tcg_temp_new();
1485 TCGv cmp_lhs, cmp_rhs;
1488 int jcc_op = (decode->b >> 1) & 7;
1489 static const TCGCond cond_table[8] = {
1490 [JCC_O] = TCG_COND_LT, /* test sign bit by comparing against 0 */
1491 [JCC_B] = TCG_COND_LTU,
1492 [JCC_Z] = TCG_COND_EQ,
1493 [JCC_BE] = TCG_COND_LEU,
1494 [JCC_S] = TCG_COND_LT, /* test sign bit by comparing against 0 */
1495 [JCC_P] = TCG_COND_TSTEQ, /* even parity - tests low bit of popcount */
1496 [JCC_L] = TCG_COND_LT,
1497 [JCC_LE] = TCG_COND_LE,
1500 cond = cond_table[jcc_op];
1501 if (decode->b & 1) {
1502 cond = tcg_invert_cond(cond);
1505 ot = decode->op[0].ot;
1506 ot_full = ot | MO_LE;
1507 if (jcc_op >= JCC_S) {
1509 * Sign-extend values before subtracting for S, P (zero/sign extension
1510 * does not matter there) L, LE and their inverses.
1516 * cmpv will be moved to cc_src *after* cpu_regs[] is written back, so use
1517 * tcg_gen_ext_tl instead of gen_ext_tl.
1519 tcg_gen_ext_tl(cmpv, cpu_regs[decode->op[1].n], ot_full);
1522 * Cmpxchg loop starts here.
1523 * - s->T1: addition operand (from decoder)
1524 * - s->A0: dest address (from decoder)
1525 * - s->cc_srcT: memory operand (lhs for comparison)
1526 * - cmpv: rhs for comparison
1528 gen_set_label(label_top);
1529 gen_op_ld_v(s, ot_full, s->cc_srcT, s->A0);
1530 tcg_gen_sub_tl(s->T0, s->cc_srcT, cmpv);
1532 /* Compute the comparison result by hand, to avoid clobbering cc_*. */
1535 /* (src1 ^ src2) & (src1 ^ dst). newv is only used here for a moment */
1536 tcg_gen_xor_tl(newv, s->cc_srcT, s->T0);
1537 tcg_gen_xor_tl(s->tmp0, s->cc_srcT, cmpv);
1538 tcg_gen_and_tl(s->tmp0, s->tmp0, newv);
1539 tcg_gen_sextract_tl(s->tmp0, s->tmp0, 0, 8 << ot);
1540 cmp_lhs = s->tmp0, cmp_rhs = tcg_constant_tl(0);
1544 tcg_gen_ext8u_tl(s->tmp0, s->T0);
1545 tcg_gen_ctpop_tl(s->tmp0, s->tmp0);
1546 cmp_lhs = s->tmp0, cmp_rhs = tcg_constant_tl(1);
1550 tcg_gen_sextract_tl(s->tmp0, s->T0, 0, 8 << ot);
1551 cmp_lhs = s->tmp0, cmp_rhs = tcg_constant_tl(0);
1555 cmp_lhs = s->cc_srcT, cmp_rhs = cmpv;
1559 /* Compute new value: if condition does not hold, just store back s->cc_srcT */
1560 tcg_gen_add_tl(newv, s->cc_srcT, s->T1);
1561 tcg_gen_movcond_tl(cond, newv, cmp_lhs, cmp_rhs, newv, s->cc_srcT);
1562 tcg_gen_atomic_cmpxchg_tl(oldv, s->A0, s->cc_srcT, newv, s->mem_index, ot_full);
1564 /* Exit unconditionally if cmpxchg succeeded. */
1565 tcg_gen_brcond_tl(TCG_COND_EQ, oldv, s->cc_srcT, label_bottom);
1567 /* Try again if there was actually a store to make. */
1568 tcg_gen_brcond_tl(cond, cmp_lhs, cmp_rhs, label_top);
1569 gen_set_label(label_bottom);
1571 /* Store old value to registers only after a successful store. */
1572 gen_writeback(s, decode, 1, s->cc_srcT);
1574 decode->cc_dst = s->T0;
1575 decode->cc_src = cmpv;
1576 decode->cc_op = CC_OP_SUBB + ot;
1579 static void gen_CMPS(DisasContext *s, X86DecodedInsn *decode)
1581 MemOp ot = decode->op[2].ot;
1582 if (s->prefix & (PREFIX_REPZ | PREFIX_REPNZ)) {
1583 gen_repz_nz(s, ot, gen_cmps);
1589 static void gen_CMPXCHG(DisasContext *s, X86DecodedInsn *decode)
1591 MemOp ot = decode->op[2].ot;
1592 TCGv cmpv = tcg_temp_new();
1593 TCGv oldv = tcg_temp_new();
1594 TCGv newv = tcg_temp_new();
1597 tcg_gen_ext_tl(cmpv, cpu_regs[R_EAX], ot);
1598 tcg_gen_ext_tl(newv, s->T1, ot);
1599 if (s->prefix & PREFIX_LOCK) {
1600 tcg_gen_atomic_cmpxchg_tl(oldv, s->A0, cmpv, newv,
1601 s->mem_index, ot | MO_LE);
1603 tcg_gen_ext_tl(oldv, s->T0, ot);
1604 if (decode->op[0].has_ea) {
1606 * Perform an unconditional store cycle like physical cpu;
1607 * must be before changing accumulator to ensure
1608 * idempotency if the store faults and the instruction
1611 tcg_gen_movcond_tl(TCG_COND_EQ, newv, oldv, cmpv, newv, oldv);
1612 gen_op_st_v(s, ot, newv, s->A0);
1615 * Unlike the memory case, where "the destination operand receives
1616 * a write cycle without regard to the result of the comparison",
1617 * rm must not be touched altogether if the write fails, including
1618 * not zero-extending it on 64-bit processors. So, precompute
1619 * the result of a successful writeback and perform the movcond
1620 * directly on cpu_regs. In case rm is part of RAX, note that this
1621 * movcond and the one below are mutually exclusive is executed.
1623 dest = gen_op_deposit_reg_v(s, ot, decode->op[0].n, newv, newv);
1624 tcg_gen_movcond_tl(TCG_COND_EQ, dest, oldv, cmpv, newv, dest);
1626 decode->op[0].unit = X86_OP_SKIP;
1629 /* Write RAX only if the cmpxchg fails. */
1630 dest = gen_op_deposit_reg_v(s, ot, R_EAX, s->T0, oldv);
1631 tcg_gen_movcond_tl(TCG_COND_NE, dest, oldv, cmpv, s->T0, dest);
1633 tcg_gen_mov_tl(s->cc_srcT, cmpv);
1634 tcg_gen_sub_tl(cmpv, cmpv, oldv);
1635 decode->cc_dst = cmpv;
1636 decode->cc_src = oldv;
1637 decode->cc_op = CC_OP_SUBB + ot;
1640 static void gen_CPUID(DisasContext *s, X86DecodedInsn *decode)
1642 gen_update_cc_op(s);
1643 gen_update_eip_cur(s);
1644 gen_helper_cpuid(tcg_env);
1647 static void gen_CRC32(DisasContext *s, X86DecodedInsn *decode)
1649 MemOp ot = decode->op[2].ot;
1651 tcg_gen_trunc_tl_i32(s->tmp2_i32, s->T0);
1652 gen_helper_crc32(s->T0, s->tmp2_i32, s->T1, tcg_constant_i32(8 << ot));
1655 static void gen_CVTPI2Px(DisasContext *s, X86DecodedInsn *decode)
1657 gen_helper_enter_mmx(tcg_env);
1658 if (s->prefix & PREFIX_DATA) {
1659 gen_helper_cvtpi2pd(tcg_env, OP_PTR0, OP_PTR2);
1661 gen_helper_cvtpi2ps(tcg_env, OP_PTR0, OP_PTR2);
1665 static void gen_CVTPx2PI(DisasContext *s, X86DecodedInsn *decode)
1667 gen_helper_enter_mmx(tcg_env);
1668 if (s->prefix & PREFIX_DATA) {
1669 gen_helper_cvtpd2pi(tcg_env, OP_PTR0, OP_PTR2);
1671 gen_helper_cvtps2pi(tcg_env, OP_PTR0, OP_PTR2);
1675 static void gen_CVTTPx2PI(DisasContext *s, X86DecodedInsn *decode)
1677 gen_helper_enter_mmx(tcg_env);
1678 if (s->prefix & PREFIX_DATA) {
1679 gen_helper_cvttpd2pi(tcg_env, OP_PTR0, OP_PTR2);
1681 gen_helper_cvttps2pi(tcg_env, OP_PTR0, OP_PTR2);
1685 static void gen_CWD(DisasContext *s, X86DecodedInsn *decode)
1687 int shift = 8 << decode->op[0].ot;
1689 tcg_gen_sextract_tl(s->T0, s->T0, shift - 1, 1);
1692 static void gen_DAA(DisasContext *s, X86DecodedInsn *decode)
1694 gen_update_cc_op(s);
1695 gen_helper_daa(tcg_env);
1696 assume_cc_op(s, CC_OP_EFLAGS);
1699 static void gen_DAS(DisasContext *s, X86DecodedInsn *decode)
1701 gen_update_cc_op(s);
1702 gen_helper_das(tcg_env);
1703 assume_cc_op(s, CC_OP_EFLAGS);
1706 static void gen_DEC(DisasContext *s, X86DecodedInsn *decode)
1708 MemOp ot = decode->op[1].ot;
1710 tcg_gen_movi_tl(s->T1, -1);
1711 if (s->prefix & PREFIX_LOCK) {
1712 tcg_gen_atomic_add_fetch_tl(s->T0, s->A0, s->T1,
1713 s->mem_index, ot | MO_LE);
1715 tcg_gen_add_tl(s->T0, s->T0, s->T1);
1717 prepare_update_cc_incdec(decode, s, CC_OP_DECB + ot);
1720 static void gen_DIV(DisasContext *s, X86DecodedInsn *decode)
1722 MemOp ot = decode->op[1].ot;
1726 gen_helper_divb_AL(tcg_env, s->T0);
1729 gen_helper_divw_AX(tcg_env, s->T0);
1733 gen_helper_divl_EAX(tcg_env, s->T0);
1735 #ifdef TARGET_X86_64
1737 gen_helper_divq_EAX(tcg_env, s->T0);
1743 static void gen_EMMS(DisasContext *s, X86DecodedInsn *decode)
1745 gen_helper_emms(tcg_env);
1748 static void gen_ENTER(DisasContext *s, X86DecodedInsn *decode)
1750 gen_enter(s, decode->op[1].imm, decode->op[2].imm);
1753 static void gen_EXTRQ_i(DisasContext *s, X86DecodedInsn *decode)
1755 TCGv_i32 length = tcg_constant_i32(decode->immediate & 63);
1756 TCGv_i32 index = tcg_constant_i32((decode->immediate >> 8) & 63);
1758 gen_helper_extrq_i(tcg_env, OP_PTR0, index, length);
1761 static void gen_EXTRQ_r(DisasContext *s, X86DecodedInsn *decode)
1763 gen_helper_extrq_r(tcg_env, OP_PTR0, OP_PTR2);
1766 static void gen_FXRSTOR(DisasContext *s, X86DecodedInsn *decode)
1768 if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
1769 gen_NM_exception(s);
1771 gen_helper_fxrstor(tcg_env, s->A0);
1775 static void gen_FXSAVE(DisasContext *s, X86DecodedInsn *decode)
1777 if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
1778 gen_NM_exception(s);
1780 gen_helper_fxsave(tcg_env, s->A0);
1784 static void gen_HLT(DisasContext *s, X86DecodedInsn *decode)
1786 #ifdef CONFIG_SYSTEM_ONLY
1787 gen_update_cc_op(s);
1788 gen_update_eip_next(s);
1789 gen_helper_hlt(tcg_env);
1790 s->base.is_jmp = DISAS_NORETURN;
1794 static void gen_IDIV(DisasContext *s, X86DecodedInsn *decode)
1796 MemOp ot = decode->op[1].ot;
1800 gen_helper_idivb_AL(tcg_env, s->T0);
1803 gen_helper_idivw_AX(tcg_env, s->T0);
1807 gen_helper_idivl_EAX(tcg_env, s->T0);
1809 #ifdef TARGET_X86_64
1811 gen_helper_idivq_EAX(tcg_env, s->T0);
1817 static void gen_IMUL3(DisasContext *s, X86DecodedInsn *decode)
1819 MemOp ot = decode->op[0].ot;
1824 /* s->T0 already sign-extended */
1825 tcg_gen_ext16s_tl(s->T1, s->T1);
1826 tcg_gen_mul_tl(s->T0, s->T0, s->T1);
1827 /* Compare the full result to the extension of the truncated result. */
1828 tcg_gen_ext16s_tl(s->T1, s->T0);
1833 #ifdef TARGET_X86_64
1834 if (TCG_TARGET_REG_BITS == 64) {
1836 * This produces fewer TCG ops, and better code if flags are needed,
1837 * but it requires a 64-bit multiply even if they are not. Use it
1838 * only if the target has 64-bits registers.
1840 * s->T0 is already sign-extended.
1842 tcg_gen_ext32s_tl(s->T1, s->T1);
1843 tcg_gen_mul_tl(s->T0, s->T0, s->T1);
1844 /* Compare the full result to the extension of the truncated result. */
1845 tcg_gen_ext32s_tl(s->T1, s->T0);
1848 /* Variant that only needs a 32-bit widening multiply. */
1849 TCGv_i32 hi = tcg_temp_new_i32();
1850 TCGv_i32 lo = tcg_temp_new_i32();
1851 tcg_gen_trunc_tl_i32(lo, s->T0);
1852 tcg_gen_trunc_tl_i32(hi, s->T1);
1853 tcg_gen_muls2_i32(lo, hi, lo, hi);
1854 tcg_gen_extu_i32_tl(s->T0, lo);
1856 cc_src_rhs = tcg_temp_new();
1857 tcg_gen_extu_i32_tl(cc_src_rhs, hi);
1858 /* Compare the high part to the sign bit of the truncated result */
1859 tcg_gen_sari_i32(lo, lo, 31);
1860 tcg_gen_extu_i32_tl(s->T1, lo);
1866 cc_src_rhs = tcg_temp_new();
1867 tcg_gen_muls2_tl(s->T0, cc_src_rhs, s->T0, s->T1);
1868 /* Compare the high part to the sign bit of the truncated result */
1869 tcg_gen_sari_tl(s->T1, s->T0, TARGET_LONG_BITS - 1);
1873 g_assert_not_reached();
1876 tcg_gen_sub_tl(s->T1, s->T1, cc_src_rhs);
1877 prepare_update2_cc(decode, s, CC_OP_MULB + ot);
1880 static void gen_IMUL(DisasContext *s, X86DecodedInsn *decode)
1882 MemOp ot = decode->op[1].ot;
1887 /* s->T0 already sign-extended */
1888 tcg_gen_ext8s_tl(s->T1, s->T1);
1889 tcg_gen_mul_tl(s->T0, s->T0, s->T1);
1890 gen_op_mov_reg_v(s, MO_16, R_EAX, s->T0);
1891 /* Compare the full result to the extension of the truncated result. */
1892 tcg_gen_ext8s_tl(s->T1, s->T0);
1897 /* s->T0 already sign-extended */
1898 tcg_gen_ext16s_tl(s->T1, s->T1);
1899 tcg_gen_mul_tl(s->T0, s->T0, s->T1);
1900 gen_op_mov_reg_v(s, MO_16, R_EAX, s->T0);
1901 tcg_gen_shri_tl(s->T1, s->T0, 16);
1902 gen_op_mov_reg_v(s, MO_16, R_EDX, s->T1);
1903 /* Compare the full result to the extension of the truncated result. */
1904 tcg_gen_ext16s_tl(s->T1, s->T0);
1909 #ifdef TARGET_X86_64
1910 /* s->T0 already sign-extended */
1911 tcg_gen_ext32s_tl(s->T1, s->T1);
1912 tcg_gen_mul_tl(s->T0, s->T0, s->T1);
1913 tcg_gen_ext32u_tl(cpu_regs[R_EAX], s->T0);
1914 tcg_gen_shri_tl(cpu_regs[R_EDX], s->T0, 32);
1915 /* Compare the full result to the extension of the truncated result. */
1916 tcg_gen_ext32s_tl(s->T1, s->T0);
1922 tcg_gen_muls2_tl(s->T0, cpu_regs[R_EDX], s->T0, s->T1);
1923 tcg_gen_mov_tl(cpu_regs[R_EAX], s->T0);
1925 /* Compare the high part to the sign bit of the truncated result */
1926 tcg_gen_negsetcondi_tl(TCG_COND_LT, s->T1, s->T0, 0);
1927 cc_src_rhs = cpu_regs[R_EDX];
1931 g_assert_not_reached();
1934 tcg_gen_sub_tl(s->T1, s->T1, cc_src_rhs);
1935 prepare_update2_cc(decode, s, CC_OP_MULB + ot);
1938 static void gen_IN(DisasContext *s, X86DecodedInsn *decode)
1940 MemOp ot = decode->op[0].ot;
1941 TCGv_i32 port = tcg_temp_new_i32();
1943 tcg_gen_trunc_tl_i32(port, s->T0);
1944 tcg_gen_ext16u_i32(port, port);
1945 if (!gen_check_io(s, ot, port, SVM_IOIO_TYPE_MASK)) {
1948 translator_io_start(&s->base);
1949 gen_helper_in_func(ot, s->T0, port);
1950 gen_writeback(s, decode, 0, s->T0);
1951 gen_bpt_io(s, port, ot);
1954 static void gen_INC(DisasContext *s, X86DecodedInsn *decode)
1956 MemOp ot = decode->op[1].ot;
1958 tcg_gen_movi_tl(s->T1, 1);
1959 if (s->prefix & PREFIX_LOCK) {
1960 tcg_gen_atomic_add_fetch_tl(s->T0, s->A0, s->T1,
1961 s->mem_index, ot | MO_LE);
1963 tcg_gen_add_tl(s->T0, s->T0, s->T1);
1965 prepare_update_cc_incdec(decode, s, CC_OP_INCB + ot);
1968 static void gen_INS(DisasContext *s, X86DecodedInsn *decode)
1970 MemOp ot = decode->op[1].ot;
1971 TCGv_i32 port = tcg_temp_new_i32();
1973 tcg_gen_trunc_tl_i32(port, s->T1);
1974 tcg_gen_ext16u_i32(port, port);
1975 if (!gen_check_io(s, ot, port,
1976 SVM_IOIO_TYPE_MASK | SVM_IOIO_STR_MASK)) {
1980 translator_io_start(&s->base);
1981 if (s->prefix & (PREFIX_REPZ | PREFIX_REPNZ)) {
1982 gen_repz(s, ot, gen_ins);
1988 static void gen_INSERTQ_i(DisasContext *s, X86DecodedInsn *decode)
1990 TCGv_i32 length = tcg_constant_i32(decode->immediate & 63);
1991 TCGv_i32 index = tcg_constant_i32((decode->immediate >> 8) & 63);
1993 gen_helper_insertq_i(tcg_env, OP_PTR0, OP_PTR1, index, length);
1996 static void gen_INSERTQ_r(DisasContext *s, X86DecodedInsn *decode)
1998 gen_helper_insertq_r(tcg_env, OP_PTR0, OP_PTR2);
2001 static void gen_INT(DisasContext *s, X86DecodedInsn *decode)
2003 gen_interrupt(s, decode->immediate);
2006 static void gen_INT1(DisasContext *s, X86DecodedInsn *decode)
2008 gen_update_cc_op(s);
2009 gen_update_eip_next(s);
2010 gen_helper_icebp(tcg_env);
2011 s->base.is_jmp = DISAS_NORETURN;
2014 static void gen_INT3(DisasContext *s, X86DecodedInsn *decode)
2016 gen_interrupt(s, EXCP03_INT3);
2019 static void gen_INTO(DisasContext *s, X86DecodedInsn *decode)
2021 gen_update_cc_op(s);
2022 gen_update_eip_cur(s);
2023 gen_helper_into(tcg_env, cur_insn_len_i32(s));
2026 static void gen_IRET(DisasContext *s, X86DecodedInsn *decode)
2028 if (!PE(s) || VM86(s)) {
2029 gen_helper_iret_real(tcg_env, tcg_constant_i32(s->dflag - 1));
2031 gen_helper_iret_protected(tcg_env, tcg_constant_i32(s->dflag - 1),
2034 assume_cc_op(s, CC_OP_EFLAGS);
2035 s->base.is_jmp = DISAS_EOB_ONLY;
2038 static void gen_Jcc(DisasContext *s, X86DecodedInsn *decode)
2041 gen_jcc(s, decode->b & 0xf, decode->immediate);
2044 static void gen_JCXZ(DisasContext *s, X86DecodedInsn *decode)
2046 TCGLabel *taken = gen_new_label();
2048 gen_update_cc_op(s);
2049 gen_op_jz_ecx(s, taken);
2050 gen_conditional_jump_labels(s, decode->immediate, NULL, taken);
2053 static void gen_JMP(DisasContext *s, X86DecodedInsn *decode)
2055 gen_update_cc_op(s);
2056 gen_jmp_rel(s, s->dflag, decode->immediate, 0);
2059 static void gen_JMP_m(DisasContext *s, X86DecodedInsn *decode)
2061 gen_op_jmp_v(s, s->T0);
2063 s->base.is_jmp = DISAS_JUMP;
2066 static void gen_JMPF(DisasContext *s, X86DecodedInsn *decode)
2071 static void gen_JMPF_m(DisasContext *s, X86DecodedInsn *decode)
2073 MemOp ot = decode->op[1].ot;
2075 gen_op_ld_v(s, ot, s->T0, s->A0);
2076 gen_add_A0_im(s, 1 << ot);
2077 gen_op_ld_v(s, MO_16, s->T1, s->A0);
2081 static void gen_LAHF(DisasContext *s, X86DecodedInsn *decode)
2083 if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM)) {
2084 return gen_illegal_opcode(s);
2086 gen_compute_eflags(s);
2087 /* Note: gen_compute_eflags() only gives the condition codes */
2088 tcg_gen_ori_tl(s->T0, cpu_cc_src, 0x02);
2089 tcg_gen_deposit_tl(cpu_regs[R_EAX], cpu_regs[R_EAX], s->T0, 8, 8);
2092 static void gen_LAR(DisasContext *s, X86DecodedInsn *decode)
2094 MemOp ot = decode->op[0].ot;
2095 TCGv result = tcg_temp_new();
2098 gen_compute_eflags(s);
2099 gen_update_cc_op(s);
2100 gen_helper_lar(result, tcg_env, s->T0);
2102 /* Perform writeback here to skip it if ZF=0. */
2103 decode->op[0].unit = X86_OP_SKIP;
2104 dest = gen_op_deposit_reg_v(s, ot, decode->op[0].n, result, result);
2105 tcg_gen_movcond_tl(TCG_COND_TSTNE, dest, cpu_cc_src, tcg_constant_tl(CC_Z),
2109 static void gen_LDMXCSR(DisasContext *s, X86DecodedInsn *decode)
2111 tcg_gen_trunc_tl_i32(s->tmp2_i32, s->T0);
2112 gen_helper_ldmxcsr(tcg_env, s->tmp2_i32);
2115 static void gen_lxx_seg(DisasContext *s, X86DecodedInsn *decode, int seg)
2117 MemOp ot = decode->op[0].ot;
2119 /* Offset already in s->T0. */
2120 gen_add_A0_im(s, 1 << ot);
2121 gen_op_ld_v(s, MO_16, s->T1, s->A0);
2123 /* load the segment here to handle exceptions properly */
2124 gen_movl_seg(s, seg, s->T1);
2127 static void gen_LDS(DisasContext *s, X86DecodedInsn *decode)
2129 gen_lxx_seg(s, decode, R_DS);
2132 static void gen_LEA(DisasContext *s, X86DecodedInsn *decode)
2134 TCGv ea = gen_lea_modrm_1(s, decode->mem, false);
2135 gen_lea_v_seg_dest(s, s->aflag, s->T0, ea, -1, -1);
2138 static void gen_LEAVE(DisasContext *s, X86DecodedInsn *decode)
2143 static void gen_LES(DisasContext *s, X86DecodedInsn *decode)
2145 gen_lxx_seg(s, decode, R_ES);
2148 static void gen_LFENCE(DisasContext *s, X86DecodedInsn *decode)
2150 tcg_gen_mb(TCG_MO_LD_LD | TCG_BAR_SC);
2153 static void gen_LFS(DisasContext *s, X86DecodedInsn *decode)
2155 gen_lxx_seg(s, decode, R_FS);
2158 static void gen_LGS(DisasContext *s, X86DecodedInsn *decode)
2160 gen_lxx_seg(s, decode, R_GS);
2163 static void gen_LODS(DisasContext *s, X86DecodedInsn *decode)
2165 MemOp ot = decode->op[1].ot;
2166 if (s->prefix & (PREFIX_REPZ | PREFIX_REPNZ)) {
2167 gen_repz(s, ot, gen_lods);
2173 static void gen_LOOP(DisasContext *s, X86DecodedInsn *decode)
2175 TCGLabel *taken = gen_new_label();
2177 gen_update_cc_op(s);
2178 gen_op_add_reg_im(s, s->aflag, R_ECX, -1);
2179 gen_op_jnz_ecx(s, taken);
2180 gen_conditional_jump_labels(s, decode->immediate, NULL, taken);
2183 static void gen_LOOPE(DisasContext *s, X86DecodedInsn *decode)
2185 TCGLabel *taken = gen_new_label();
2186 TCGLabel *not_taken = gen_new_label();
2188 gen_update_cc_op(s);
2189 gen_op_add_reg_im(s, s->aflag, R_ECX, -1);
2190 gen_op_jz_ecx(s, not_taken);
2191 gen_jcc1(s, (JCC_Z << 1), taken); /* jz taken */
2192 gen_conditional_jump_labels(s, decode->immediate, not_taken, taken);
2195 static void gen_LOOPNE(DisasContext *s, X86DecodedInsn *decode)
2197 TCGLabel *taken = gen_new_label();
2198 TCGLabel *not_taken = gen_new_label();
2200 gen_update_cc_op(s);
2201 gen_op_add_reg_im(s, s->aflag, R_ECX, -1);
2202 gen_op_jz_ecx(s, not_taken);
2203 gen_jcc1(s, (JCC_Z << 1) | 1, taken); /* jnz taken */
2204 gen_conditional_jump_labels(s, decode->immediate, not_taken, taken);
2207 static void gen_LSL(DisasContext *s, X86DecodedInsn *decode)
2209 MemOp ot = decode->op[0].ot;
2210 TCGv result = tcg_temp_new();
2213 gen_compute_eflags(s);
2214 gen_update_cc_op(s);
2215 gen_helper_lsl(result, tcg_env, s->T0);
2217 /* Perform writeback here to skip it if ZF=0. */
2218 decode->op[0].unit = X86_OP_SKIP;
2219 dest = gen_op_deposit_reg_v(s, ot, decode->op[0].n, result, result);
2220 tcg_gen_movcond_tl(TCG_COND_TSTNE, dest, cpu_cc_src, tcg_constant_tl(CC_Z),
2224 static void gen_LSS(DisasContext *s, X86DecodedInsn *decode)
2226 gen_lxx_seg(s, decode, R_SS);
2229 static void gen_LZCNT(DisasContext *s, X86DecodedInsn *decode)
2231 MemOp ot = decode->op[0].ot;
2233 /* C bit (cc_src) is defined related to the input. */
2234 decode->cc_src = tcg_temp_new();
2235 decode->cc_dst = s->T0;
2236 decode->cc_op = CC_OP_BMILGB + ot;
2237 tcg_gen_mov_tl(decode->cc_src, s->T0);
2240 * Reduce the target_ulong result by the number of zeros that
2241 * we expect to find at the top.
2243 tcg_gen_clzi_tl(s->T0, s->T0, TARGET_LONG_BITS);
2244 tcg_gen_subi_tl(s->T0, s->T0, TARGET_LONG_BITS - (8 << ot));
2247 static void gen_MFENCE(DisasContext *s, X86DecodedInsn *decode)
2249 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
2252 static void gen_MOV(DisasContext *s, X86DecodedInsn *decode)
2254 /* nothing to do! */
2256 #define gen_NOP gen_MOV
2258 static void gen_MASKMOV(DisasContext *s, X86DecodedInsn *decode)
2260 gen_lea_v_seg(s, cpu_regs[R_EDI], R_DS, s->override);
2262 if (s->prefix & PREFIX_DATA) {
2263 gen_helper_maskmov_xmm(tcg_env, OP_PTR1, OP_PTR2, s->A0);
2265 gen_helper_maskmov_mmx(tcg_env, OP_PTR1, OP_PTR2, s->A0);
2269 static void gen_MOVBE(DisasContext *s, X86DecodedInsn *decode)
2271 MemOp ot = decode->op[0].ot;
2273 /* M operand type does not load/store */
2274 if (decode->e.op0 == X86_TYPE_M) {
2275 tcg_gen_qemu_st_tl(s->T0, s->A0, s->mem_index, ot | MO_BE);
2277 tcg_gen_qemu_ld_tl(s->T0, s->A0, s->mem_index, ot | MO_BE);
2281 static void gen_MOVD_from(DisasContext *s, X86DecodedInsn *decode)
2283 MemOp ot = decode->op[2].ot;
2287 #ifdef TARGET_X86_64
2288 tcg_gen_ld32u_tl(s->T0, tcg_env, decode->op[2].offset);
2292 tcg_gen_ld_tl(s->T0, tcg_env, decode->op[2].offset);
2299 static void gen_MOVD_to(DisasContext *s, X86DecodedInsn *decode)
2301 MemOp ot = decode->op[2].ot;
2302 int vec_len = vector_len(s, decode);
2303 int lo_ofs = vector_elem_offset(&decode->op[0], ot, 0);
2305 tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
2309 #ifdef TARGET_X86_64
2310 tcg_gen_st32_tl(s->T1, tcg_env, lo_ofs);
2314 tcg_gen_st_tl(s->T1, tcg_env, lo_ofs);
2317 g_assert_not_reached();
2321 static void gen_MOVDQ(DisasContext *s, X86DecodedInsn *decode)
2323 gen_store_sse(s, decode, decode->op[2].offset);
2326 static void gen_MOVMSK(DisasContext *s, X86DecodedInsn *decode)
2328 typeof(gen_helper_movmskps_ymm) *ps, *pd, *fn;
2329 ps = s->vex_l ? gen_helper_movmskps_ymm : gen_helper_movmskps_xmm;
2330 pd = s->vex_l ? gen_helper_movmskpd_ymm : gen_helper_movmskpd_xmm;
2331 fn = s->prefix & PREFIX_DATA ? pd : ps;
2332 fn(s->tmp2_i32, tcg_env, OP_PTR2);
2333 tcg_gen_extu_i32_tl(s->T0, s->tmp2_i32);
2336 static void gen_MOVQ(DisasContext *s, X86DecodedInsn *decode)
2338 int vec_len = vector_len(s, decode);
2339 int lo_ofs = vector_elem_offset(&decode->op[0], MO_64, 0);
2341 tcg_gen_ld_i64(s->tmp1_i64, tcg_env, decode->op[2].offset);
2342 if (decode->op[0].has_ea) {
2343 tcg_gen_qemu_st_i64(s->tmp1_i64, s->A0, s->mem_index, MO_LEUQ);
2346 * tcg_gen_gvec_dup_i64(MO_64, op0.offset, 8, vec_len, s->tmp1_64) would
2347 * seem to work, but it does not on big-endian platforms; the cleared parts
2348 * are always at higher addresses, but cross-endian emulation inverts the
2349 * byte order so that the cleared parts need to be at *lower* addresses.
2350 * Because oprsz is 8, we see this here even for SSE; but more in general,
2351 * it disqualifies using oprsz < maxsz to emulate VEX128.
2353 tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
2354 tcg_gen_st_i64(s->tmp1_i64, tcg_env, lo_ofs);
2358 static void gen_MOVq_dq(DisasContext *s, X86DecodedInsn *decode)
2360 gen_helper_enter_mmx(tcg_env);
2361 /* Otherwise the same as any other movq. */
2362 return gen_MOVQ(s, decode);
2365 static void gen_MOVS(DisasContext *s, X86DecodedInsn *decode)
2367 MemOp ot = decode->op[2].ot;
2368 if (s->prefix & (PREFIX_REPZ | PREFIX_REPNZ)) {
2369 gen_repz(s, ot, gen_movs);
2375 static void gen_MUL(DisasContext *s, X86DecodedInsn *decode)
2377 MemOp ot = decode->op[1].ot;
2381 /* s->T0 already zero-extended */
2382 tcg_gen_ext8u_tl(s->T1, s->T1);
2383 tcg_gen_mul_tl(s->T0, s->T0, s->T1);
2384 gen_op_mov_reg_v(s, MO_16, R_EAX, s->T0);
2385 tcg_gen_andi_tl(s->T1, s->T0, 0xff00);
2386 decode->cc_dst = s->T0;
2387 decode->cc_src = s->T1;
2391 /* s->T0 already zero-extended */
2392 tcg_gen_ext16u_tl(s->T1, s->T1);
2393 tcg_gen_mul_tl(s->T0, s->T0, s->T1);
2394 gen_op_mov_reg_v(s, MO_16, R_EAX, s->T0);
2395 tcg_gen_shri_tl(s->T1, s->T0, 16);
2396 gen_op_mov_reg_v(s, MO_16, R_EDX, s->T1);
2397 decode->cc_dst = s->T0;
2398 decode->cc_src = s->T1;
2402 #ifdef TARGET_X86_64
2403 /* s->T0 already zero-extended */
2404 tcg_gen_ext32u_tl(s->T1, s->T1);
2405 tcg_gen_mul_tl(s->T0, s->T0, s->T1);
2406 tcg_gen_ext32u_tl(cpu_regs[R_EAX], s->T0);
2407 tcg_gen_shri_tl(cpu_regs[R_EDX], s->T0, 32);
2408 decode->cc_dst = cpu_regs[R_EAX];
2409 decode->cc_src = cpu_regs[R_EDX];
2414 tcg_gen_mulu2_tl(cpu_regs[R_EAX], cpu_regs[R_EDX], s->T0, s->T1);
2415 decode->cc_dst = cpu_regs[R_EAX];
2416 decode->cc_src = cpu_regs[R_EDX];
2420 g_assert_not_reached();
2423 decode->cc_op = CC_OP_MULB + ot;
2426 static void gen_MULX(DisasContext *s, X86DecodedInsn *decode)
2428 MemOp ot = decode->op[0].ot;
2430 /* low part of result in VEX.vvvv, high in MODRM */
2433 #ifdef TARGET_X86_64
2434 tcg_gen_trunc_tl_i32(s->tmp2_i32, s->T0);
2435 tcg_gen_trunc_tl_i32(s->tmp3_i32, s->T1);
2436 tcg_gen_mulu2_i32(s->tmp2_i32, s->tmp3_i32,
2437 s->tmp2_i32, s->tmp3_i32);
2438 tcg_gen_extu_i32_tl(cpu_regs[s->vex_v], s->tmp2_i32);
2439 tcg_gen_extu_i32_tl(s->T0, s->tmp3_i32);
2444 tcg_gen_mulu2_tl(cpu_regs[s->vex_v], s->T0, s->T0, s->T1);
2448 g_assert_not_reached();
2452 static void gen_NEG(DisasContext *s, X86DecodedInsn *decode)
2454 MemOp ot = decode->op[0].ot;
2455 TCGv oldv = tcg_temp_new();
2457 if (s->prefix & PREFIX_LOCK) {
2458 TCGv newv = tcg_temp_new();
2459 TCGv cmpv = tcg_temp_new();
2460 TCGLabel *label1 = gen_new_label();
2462 gen_set_label(label1);
2463 gen_op_ld_v(s, ot, oldv, s->A0);
2464 tcg_gen_neg_tl(newv, oldv);
2465 tcg_gen_atomic_cmpxchg_tl(cmpv, s->A0, oldv, newv,
2466 s->mem_index, ot | MO_LE);
2467 tcg_gen_brcond_tl(TCG_COND_NE, oldv, cmpv, label1);
2469 tcg_gen_mov_tl(oldv, s->T0);
2471 tcg_gen_neg_tl(s->T0, oldv);
2473 decode->cc_dst = s->T0;
2474 decode->cc_src = oldv;
2475 tcg_gen_movi_tl(s->cc_srcT, 0);
2476 decode->cc_op = CC_OP_SUBB + ot;
2479 static void gen_NOT(DisasContext *s, X86DecodedInsn *decode)
2481 MemOp ot = decode->op[0].ot;
2483 if (s->prefix & PREFIX_LOCK) {
2484 tcg_gen_movi_tl(s->T0, ~0);
2485 tcg_gen_atomic_xor_fetch_tl(s->T0, s->A0, s->T0,
2486 s->mem_index, ot | MO_LE);
2488 tcg_gen_not_tl(s->T0, s->T0);
2492 static void gen_OR(DisasContext *s, X86DecodedInsn *decode)
2494 MemOp ot = decode->op[1].ot;
2496 if (s->prefix & PREFIX_LOCK) {
2497 tcg_gen_atomic_or_fetch_tl(s->T0, s->A0, s->T1,
2498 s->mem_index, ot | MO_LE);
2500 tcg_gen_or_tl(s->T0, s->T0, s->T1);
2502 prepare_update1_cc(decode, s, CC_OP_LOGICB + ot);
2505 static void gen_OUT(DisasContext *s, X86DecodedInsn *decode)
2507 MemOp ot = decode->op[1].ot;
2508 TCGv_i32 port = tcg_temp_new_i32();
2509 TCGv_i32 value = tcg_temp_new_i32();
2511 tcg_gen_trunc_tl_i32(port, s->T1);
2512 tcg_gen_ext16u_i32(port, port);
2513 if (!gen_check_io(s, ot, port, 0)) {
2516 tcg_gen_trunc_tl_i32(value, s->T0);
2517 translator_io_start(&s->base);
2518 gen_helper_out_func(ot, port, value);
2519 gen_bpt_io(s, port, ot);
2522 static void gen_OUTS(DisasContext *s, X86DecodedInsn *decode)
2524 MemOp ot = decode->op[1].ot;
2525 TCGv_i32 port = tcg_temp_new_i32();
2527 tcg_gen_trunc_tl_i32(port, s->T1);
2528 tcg_gen_ext16u_i32(port, port);
2529 if (!gen_check_io(s, ot, port, SVM_IOIO_STR_MASK)) {
2533 translator_io_start(&s->base);
2534 if (s->prefix & (PREFIX_REPZ | PREFIX_REPNZ)) {
2535 gen_repz(s, ot, gen_outs);
2541 static void gen_PALIGNR(DisasContext *s, X86DecodedInsn *decode)
2543 TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
2544 if (!(s->prefix & PREFIX_DATA)) {
2545 gen_helper_palignr_mmx(tcg_env, OP_PTR0, OP_PTR1, OP_PTR2, imm);
2546 } else if (!s->vex_l) {
2547 gen_helper_palignr_xmm(tcg_env, OP_PTR0, OP_PTR1, OP_PTR2, imm);
2549 gen_helper_palignr_ymm(tcg_env, OP_PTR0, OP_PTR1, OP_PTR2, imm);
2553 static void gen_PANDN(DisasContext *s, X86DecodedInsn *decode)
2555 int vec_len = vector_len(s, decode);
2557 /* Careful, operand order is reversed! */
2558 tcg_gen_gvec_andc(MO_64,
2559 decode->op[0].offset, decode->op[2].offset,
2560 decode->op[1].offset, vec_len, vec_len);
2563 static void gen_PAUSE(DisasContext *s, X86DecodedInsn *decode)
2565 gen_update_cc_op(s);
2566 gen_update_eip_next(s);
2567 gen_helper_pause(tcg_env);
2568 s->base.is_jmp = DISAS_NORETURN;
2571 static void gen_PCMPESTRI(DisasContext *s, X86DecodedInsn *decode)
2573 TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
2574 gen_helper_pcmpestri_xmm(tcg_env, OP_PTR1, OP_PTR2, imm);
2575 assume_cc_op(s, CC_OP_EFLAGS);
2578 static void gen_PCMPESTRM(DisasContext *s, X86DecodedInsn *decode)
2580 TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
2581 gen_helper_pcmpestrm_xmm(tcg_env, OP_PTR1, OP_PTR2, imm);
2582 assume_cc_op(s, CC_OP_EFLAGS);
2583 if ((s->prefix & PREFIX_VEX) && !s->vex_l) {
2584 tcg_gen_gvec_dup_imm(MO_64, offsetof(CPUX86State, xmm_regs[0].ZMM_X(1)),
2589 static void gen_PCMPISTRI(DisasContext *s, X86DecodedInsn *decode)
2591 TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
2592 gen_helper_pcmpistri_xmm(tcg_env, OP_PTR1, OP_PTR2, imm);
2593 assume_cc_op(s, CC_OP_EFLAGS);
2596 static void gen_PCMPISTRM(DisasContext *s, X86DecodedInsn *decode)
2598 TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
2599 gen_helper_pcmpistrm_xmm(tcg_env, OP_PTR1, OP_PTR2, imm);
2600 assume_cc_op(s, CC_OP_EFLAGS);
2601 if ((s->prefix & PREFIX_VEX) && !s->vex_l) {
2602 tcg_gen_gvec_dup_imm(MO_64, offsetof(CPUX86State, xmm_regs[0].ZMM_X(1)),
2607 static void gen_PDEP(DisasContext *s, X86DecodedInsn *decode)
2609 gen_helper_pdep(s->T0, s->T0, s->T1);
2612 static void gen_PEXT(DisasContext *s, X86DecodedInsn *decode)
2614 gen_helper_pext(s->T0, s->T0, s->T1);
2617 static inline void gen_pextr(DisasContext *s, X86DecodedInsn *decode, MemOp ot)
2619 int vec_len = vector_len(s, decode);
2620 int mask = (vec_len >> ot) - 1;
2621 int val = decode->immediate & mask;
2625 tcg_gen_ld8u_tl(s->T0, tcg_env, vector_elem_offset(&decode->op[1], ot, val));
2628 tcg_gen_ld16u_tl(s->T0, tcg_env, vector_elem_offset(&decode->op[1], ot, val));
2631 #ifdef TARGET_X86_64
2632 tcg_gen_ld32u_tl(s->T0, tcg_env, vector_elem_offset(&decode->op[1], ot, val));
2636 tcg_gen_ld_tl(s->T0, tcg_env, vector_elem_offset(&decode->op[1], ot, val));
2643 static void gen_PEXTRB(DisasContext *s, X86DecodedInsn *decode)
2645 gen_pextr(s, decode, MO_8);
2648 static void gen_PEXTRW(DisasContext *s, X86DecodedInsn *decode)
2650 gen_pextr(s, decode, MO_16);
2653 static void gen_PEXTR(DisasContext *s, X86DecodedInsn *decode)
2655 MemOp ot = decode->op[0].ot;
2656 gen_pextr(s, decode, ot);
2659 static inline void gen_pinsr(DisasContext *s, X86DecodedInsn *decode, MemOp ot)
2661 int vec_len = vector_len(s, decode);
2662 int mask = (vec_len >> ot) - 1;
2663 int val = decode->immediate & mask;
2665 if (decode->op[1].offset != decode->op[0].offset) {
2666 assert(vec_len == 16);
2667 gen_store_sse(s, decode, decode->op[1].offset);
2672 tcg_gen_st8_tl(s->T1, tcg_env, vector_elem_offset(&decode->op[0], ot, val));
2675 tcg_gen_st16_tl(s->T1, tcg_env, vector_elem_offset(&decode->op[0], ot, val));
2678 #ifdef TARGET_X86_64
2679 tcg_gen_st32_tl(s->T1, tcg_env, vector_elem_offset(&decode->op[0], ot, val));
2683 tcg_gen_st_tl(s->T1, tcg_env, vector_elem_offset(&decode->op[0], ot, val));
2690 static void gen_PINSRB(DisasContext *s, X86DecodedInsn *decode)
2692 gen_pinsr(s, decode, MO_8);
2695 static void gen_PINSRW(DisasContext *s, X86DecodedInsn *decode)
2697 gen_pinsr(s, decode, MO_16);
2700 static void gen_PINSR(DisasContext *s, X86DecodedInsn *decode)
2702 gen_pinsr(s, decode, decode->op[2].ot);
2705 static void gen_pmovmskb_i64(TCGv_i64 d, TCGv_i64 s)
2707 TCGv_i64 t = tcg_temp_new_i64();
2709 tcg_gen_andi_i64(d, s, 0x8080808080808080ull);
2712 * After each shift+or pair:
2713 * 0: a.......b.......c.......d.......e.......f.......g.......h.......
2714 * 7: ab......bc......cd......de......ef......fg......gh......h.......
2715 * 14: abcd....bcde....cdef....defg....efgh....fgh.....gh......h.......
2716 * 28: abcdefghbcdefgh.cdefgh..defgh...efgh....fgh.....gh......h.......
2717 * The result is left in the high bits of the word.
2719 tcg_gen_shli_i64(t, d, 7);
2720 tcg_gen_or_i64(d, d, t);
2721 tcg_gen_shli_i64(t, d, 14);
2722 tcg_gen_or_i64(d, d, t);
2723 tcg_gen_shli_i64(t, d, 28);
2724 tcg_gen_or_i64(d, d, t);
2727 static void gen_pmovmskb_vec(unsigned vece, TCGv_vec d, TCGv_vec s)
2729 TCGv_vec t = tcg_temp_new_vec_matching(d);
2730 TCGv_vec m = tcg_constant_vec_matching(d, MO_8, 0x80);
2733 tcg_gen_and_vec(vece, d, s, m);
2734 tcg_gen_shli_vec(vece, t, d, 7);
2735 tcg_gen_or_vec(vece, d, d, t);
2736 tcg_gen_shli_vec(vece, t, d, 14);
2737 tcg_gen_or_vec(vece, d, d, t);
2738 tcg_gen_shli_vec(vece, t, d, 28);
2739 tcg_gen_or_vec(vece, d, d, t);
2742 static void gen_PMOVMSKB(DisasContext *s, X86DecodedInsn *decode)
2744 static const TCGOpcode vecop_list[] = { INDEX_op_shli_vec, 0 };
2745 static const GVecGen2 g = {
2746 .fni8 = gen_pmovmskb_i64,
2747 .fniv = gen_pmovmskb_vec,
2748 .opt_opc = vecop_list,
2750 .prefer_i64 = TCG_TARGET_REG_BITS == 64
2752 MemOp ot = decode->op[2].ot;
2753 int vec_len = vector_len(s, decode);
2754 TCGv t = tcg_temp_new();
2756 tcg_gen_gvec_2(offsetof(CPUX86State, xmm_t0) + xmm_offset(ot), decode->op[2].offset,
2757 vec_len, vec_len, &g);
2758 tcg_gen_ld8u_tl(s->T0, tcg_env, offsetof(CPUX86State, xmm_t0.ZMM_B(vec_len - 1)));
2759 while (vec_len > 8) {
2761 if (TCG_TARGET_HAS_extract2_tl) {
2763 * Load the next byte of the result into the high byte of T.
2764 * TCG does a similar expansion of deposit to shl+extract2; by
2765 * loading the whole word, the shift left is avoided.
2767 #ifdef TARGET_X86_64
2768 tcg_gen_ld_tl(t, tcg_env, offsetof(CPUX86State, xmm_t0.ZMM_Q((vec_len - 1) / 8)));
2770 tcg_gen_ld_tl(t, tcg_env, offsetof(CPUX86State, xmm_t0.ZMM_L((vec_len - 1) / 4)));
2773 tcg_gen_extract2_tl(s->T0, t, s->T0, TARGET_LONG_BITS - 8);
2776 * The _previous_ value is deposited into bits 8 and higher of t. Because
2777 * those bits are known to be zero after ld8u, this becomes a shift+or
2778 * if deposit is not available.
2780 tcg_gen_ld8u_tl(t, tcg_env, offsetof(CPUX86State, xmm_t0.ZMM_B(vec_len - 1)));
2781 tcg_gen_deposit_tl(s->T0, t, s->T0, 8, TARGET_LONG_BITS - 8);
2786 static void gen_POP(DisasContext *s, X86DecodedInsn *decode)
2788 X86DecodedOp *op = &decode->op[0];
2789 MemOp ot = gen_pop_T0(s);
2791 assert(ot >= op->ot);
2792 if (op->has_ea || op->unit == X86_OP_SEG) {
2793 /* NOTE: order is important for MMU exceptions */
2794 gen_writeback(s, decode, 0, s->T0);
2797 /* NOTE: writing back registers after update is important for pop %sp */
2798 gen_pop_update(s, ot);
2801 static void gen_POPA(DisasContext *s, X86DecodedInsn *decode)
2806 static void gen_POPCNT(DisasContext *s, X86DecodedInsn *decode)
2808 decode->cc_dst = tcg_temp_new();
2809 decode->cc_op = CC_OP_POPCNT;
2811 tcg_gen_mov_tl(decode->cc_dst, s->T0);
2812 tcg_gen_ctpop_tl(s->T0, s->T0);
2815 static void gen_POPF(DisasContext *s, X86DecodedInsn *decode)
2818 int mask = TF_MASK | AC_MASK | ID_MASK | NT_MASK;
2821 mask |= IF_MASK | IOPL_MASK;
2822 } else if (CPL(s) <= IOPL(s)) {
2825 if (s->dflag == MO_16) {
2830 gen_helper_write_eflags(tcg_env, s->T0, tcg_constant_i32(mask));
2831 gen_pop_update(s, ot);
2832 set_cc_op(s, CC_OP_EFLAGS);
2833 /* abort translation because TF/AC flag may change */
2834 s->base.is_jmp = DISAS_EOB_NEXT;
2837 static void gen_PSHUFW(DisasContext *s, X86DecodedInsn *decode)
2839 TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
2840 gen_helper_pshufw_mmx(OP_PTR0, OP_PTR1, imm);
2843 static void gen_PSRLW_i(DisasContext *s, X86DecodedInsn *decode)
2845 int vec_len = vector_len(s, decode);
2847 if (decode->immediate >= 16) {
2848 tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
2850 tcg_gen_gvec_shri(MO_16,
2851 decode->op[0].offset, decode->op[1].offset,
2852 decode->immediate, vec_len, vec_len);
2856 static void gen_PSLLW_i(DisasContext *s, X86DecodedInsn *decode)
2858 int vec_len = vector_len(s, decode);
2860 if (decode->immediate >= 16) {
2861 tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
2863 tcg_gen_gvec_shli(MO_16,
2864 decode->op[0].offset, decode->op[1].offset,
2865 decode->immediate, vec_len, vec_len);
2869 static void gen_PSRAW_i(DisasContext *s, X86DecodedInsn *decode)
2871 int vec_len = vector_len(s, decode);
2873 if (decode->immediate >= 16) {
2874 decode->immediate = 15;
2876 tcg_gen_gvec_sari(MO_16,
2877 decode->op[0].offset, decode->op[1].offset,
2878 decode->immediate, vec_len, vec_len);
2881 static void gen_PSRLD_i(DisasContext *s, X86DecodedInsn *decode)
2883 int vec_len = vector_len(s, decode);
2885 if (decode->immediate >= 32) {
2886 tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
2888 tcg_gen_gvec_shri(MO_32,
2889 decode->op[0].offset, decode->op[1].offset,
2890 decode->immediate, vec_len, vec_len);
2894 static void gen_PSLLD_i(DisasContext *s, X86DecodedInsn *decode)
2896 int vec_len = vector_len(s, decode);
2898 if (decode->immediate >= 32) {
2899 tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
2901 tcg_gen_gvec_shli(MO_32,
2902 decode->op[0].offset, decode->op[1].offset,
2903 decode->immediate, vec_len, vec_len);
2907 static void gen_PSRAD_i(DisasContext *s, X86DecodedInsn *decode)
2909 int vec_len = vector_len(s, decode);
2911 if (decode->immediate >= 32) {
2912 decode->immediate = 31;
2914 tcg_gen_gvec_sari(MO_32,
2915 decode->op[0].offset, decode->op[1].offset,
2916 decode->immediate, vec_len, vec_len);
2919 static void gen_PSRLQ_i(DisasContext *s, X86DecodedInsn *decode)
2921 int vec_len = vector_len(s, decode);
2923 if (decode->immediate >= 64) {
2924 tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
2926 tcg_gen_gvec_shri(MO_64,
2927 decode->op[0].offset, decode->op[1].offset,
2928 decode->immediate, vec_len, vec_len);
2932 static void gen_PSLLQ_i(DisasContext *s, X86DecodedInsn *decode)
2934 int vec_len = vector_len(s, decode);
2936 if (decode->immediate >= 64) {
2937 tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
2939 tcg_gen_gvec_shli(MO_64,
2940 decode->op[0].offset, decode->op[1].offset,
2941 decode->immediate, vec_len, vec_len);
2945 static TCGv_ptr make_imm8u_xmm_vec(uint8_t imm, int vec_len)
2947 MemOp ot = vec_len == 16 ? MO_128 : MO_256;
2948 TCGv_i32 imm_v = tcg_constant8u_i32(imm);
2949 TCGv_ptr ptr = tcg_temp_new_ptr();
2951 tcg_gen_gvec_dup_imm(MO_64, offsetof(CPUX86State, xmm_t0) + xmm_offset(ot),
2952 vec_len, vec_len, 0);
2954 tcg_gen_addi_ptr(ptr, tcg_env, offsetof(CPUX86State, xmm_t0));
2955 tcg_gen_st_i32(imm_v, tcg_env, offsetof(CPUX86State, xmm_t0.ZMM_L(0)));
2959 static void gen_PSRLDQ_i(DisasContext *s, X86DecodedInsn *decode)
2961 int vec_len = vector_len(s, decode);
2962 TCGv_ptr imm_vec = make_imm8u_xmm_vec(decode->immediate, vec_len);
2965 gen_helper_psrldq_ymm(tcg_env, OP_PTR0, OP_PTR1, imm_vec);
2967 gen_helper_psrldq_xmm(tcg_env, OP_PTR0, OP_PTR1, imm_vec);
2971 static void gen_PSLLDQ_i(DisasContext *s, X86DecodedInsn *decode)
2973 int vec_len = vector_len(s, decode);
2974 TCGv_ptr imm_vec = make_imm8u_xmm_vec(decode->immediate, vec_len);
2977 gen_helper_pslldq_ymm(tcg_env, OP_PTR0, OP_PTR1, imm_vec);
2979 gen_helper_pslldq_xmm(tcg_env, OP_PTR0, OP_PTR1, imm_vec);
2983 static void gen_PUSH(DisasContext *s, X86DecodedInsn *decode)
2985 gen_push_v(s, s->T0);
2988 static void gen_PUSHA(DisasContext *s, X86DecodedInsn *decode)
2993 static void gen_PUSHF(DisasContext *s, X86DecodedInsn *decode)
2995 gen_update_cc_op(s);
2996 gen_helper_read_eflags(s->T0, tcg_env);
2997 gen_push_v(s, s->T0);
3000 static MemOp gen_shift_count(DisasContext *s, X86DecodedInsn *decode,
3001 bool *can_be_zero, TCGv *count, int unit)
3003 MemOp ot = decode->op[0].ot;
3004 int mask = (ot <= MO_32 ? 0x1f : 0x3f);
3006 *can_be_zero = false;
3009 *count = tcg_temp_new();
3010 tcg_gen_andi_tl(*count, cpu_regs[R_ECX], mask);
3011 *can_be_zero = true;
3015 if ((decode->immediate & mask) == 0) {
3019 *count = tcg_temp_new();
3020 tcg_gen_movi_tl(*count, decode->immediate & mask);
3024 *count = tcg_temp_new();
3025 tcg_gen_movi_tl(*count, 1);
3029 g_assert_not_reached();
3036 * Compute existing flags in decode->cc_src, for gen_* functions that wants
3037 * to set the cc_op set to CC_OP_ADCOX. In particular, this allows rotate
3038 * operations to compute the carry in decode->cc_dst and the overflow in
3041 * If need_flags is true, decode->cc_dst and decode->cc_src2 are preloaded
3042 * with the value of CF and OF before the instruction, so that it is possible
3043 * to keep the flags unmodified.
3045 * Return true if carry could be made available cheaply as a 1-bit value in
3046 * decode->cc_dst (trying a bit harder if want_carry is true). If false is
3047 * returned, decode->cc_dst is uninitialized and the carry is only available
3048 * as bit 0 of decode->cc_src.
3050 static bool gen_eflags_adcox(DisasContext *s, X86DecodedInsn *decode, bool want_carry, bool need_flags)
3052 bool got_cf = false;
3053 bool got_of = false;
3055 decode->cc_dst = tcg_temp_new();
3056 decode->cc_src = tcg_temp_new();
3057 decode->cc_src2 = tcg_temp_new();
3058 decode->cc_op = CC_OP_ADCOX;
3060 /* A lot more cc_ops could be "optimized" to avoid the extracts at
3061 * the end (INC/DEC, BMILG, MUL), but they are all really unlikely
3062 * to be followed by rotations within the same basic block.
3066 /* No need to compute the full EFLAGS, CF/OF are already isolated. */
3067 tcg_gen_mov_tl(decode->cc_src, cpu_cc_src);
3069 tcg_gen_mov_tl(decode->cc_src2, cpu_cc_src2);
3072 if (want_carry || need_flags) {
3073 tcg_gen_mov_tl(decode->cc_dst, cpu_cc_dst);
3078 case CC_OP_LOGICB ... CC_OP_LOGICQ:
3079 /* CF and OF are zero, do it just because it's easy. */
3080 gen_mov_eflags(s, decode->cc_src);
3082 tcg_gen_movi_tl(decode->cc_src2, 0);
3085 if (want_carry || need_flags) {
3086 tcg_gen_movi_tl(decode->cc_dst, 0);
3091 case CC_OP_SARB ... CC_OP_SARQ:
3093 * SHR/RCR/SHR/RCR/... is a relatively common occurrence of RCR.
3094 * By computing CF without using eflags, the calls to cc_compute_all
3095 * can be eliminated as dead code (except for the last RCR).
3097 if (want_carry || need_flags) {
3098 tcg_gen_andi_tl(decode->cc_dst, cpu_cc_src, 1);
3101 gen_mov_eflags(s, decode->cc_src);
3104 case CC_OP_SHLB ... CC_OP_SHLQ:
3106 * Likewise for SHL/RCL/SHL/RCL/... but, if CF is not in the sign
3107 * bit, we might as well fish CF out of EFLAGS and save a shift.
3109 if (want_carry && (!need_flags || s->cc_op == CC_OP_SHLB + MO_TL)) {
3110 tcg_gen_shri_tl(decode->cc_dst, cpu_cc_src, (8 << (s->cc_op - CC_OP_SHLB)) - 1);
3113 gen_mov_eflags(s, decode->cc_src);
3117 gen_mov_eflags(s, decode->cc_src);
3122 /* If the flags could be left unmodified, always load them. */
3124 tcg_gen_extract_tl(decode->cc_src2, decode->cc_src, ctz32(CC_O), 1);
3128 tcg_gen_extract_tl(decode->cc_dst, decode->cc_src, ctz32(CC_C), 1);
3135 static void gen_rot_overflow(X86DecodedInsn *decode, TCGv result, TCGv old,
3136 bool can_be_zero, TCGv count)
3138 MemOp ot = decode->op[0].ot;
3139 TCGv temp = can_be_zero ? tcg_temp_new() : decode->cc_src2;
3141 tcg_gen_xor_tl(temp, old, result);
3142 tcg_gen_extract_tl(temp, temp, (8 << ot) - 1, 1);
3144 tcg_gen_movcond_tl(TCG_COND_EQ, decode->cc_src2, count, tcg_constant_tl(0),
3145 decode->cc_src2, temp);
3150 * RCx operations are invariant modulo 8*operand_size+1. For 8 and 16-bit operands,
3151 * this is less than 0x1f (the mask applied by gen_shift_count) so reduce further.
3153 static void gen_rotc_mod(MemOp ot, TCGv count)
3159 temp = tcg_temp_new();
3160 tcg_gen_subi_tl(temp, count, 18);
3161 tcg_gen_movcond_tl(TCG_COND_GE, count, temp, tcg_constant_tl(0), temp, count);
3162 tcg_gen_subi_tl(temp, count, 9);
3163 tcg_gen_movcond_tl(TCG_COND_GE, count, temp, tcg_constant_tl(0), temp, count);
3167 temp = tcg_temp_new();
3168 tcg_gen_subi_tl(temp, count, 17);
3169 tcg_gen_movcond_tl(TCG_COND_GE, count, temp, tcg_constant_tl(0), temp, count);
3178 * The idea here is that the bit to the right of the new bit 0 is the
3179 * new carry, and the bit to the right of the old bit 0 is the old carry.
3180 * Just like a regular rotation, the result of the rotation is composed
3181 * from a right shifted part and a left shifted part of s->T0. The new carry
3182 * is extracted from the right-shifted portion, and the old carry is
3183 * inserted at the end of the left-shifted portion.
3185 * Because of the separate shifts involving the carry, gen_RCL and gen_RCR
3186 * mostly operate on count-1. This also comes in handy when computing
3187 * length - count, because (length-1) - (count-1) can be computed with
3188 * a XOR, and that is commutative unlike subtraction.
3190 static void gen_RCL(DisasContext *s, X86DecodedInsn *decode)
3192 bool have_1bit_cin, can_be_zero;
3194 TCGLabel *zero_label = NULL;
3195 MemOp ot = gen_shift_count(s, decode, &can_be_zero, &count, decode->op[2].unit);
3196 TCGv low, high, low_count;
3202 low = tcg_temp_new();
3203 high = tcg_temp_new();
3204 low_count = tcg_temp_new();
3206 gen_rotc_mod(ot, count);
3207 have_1bit_cin = gen_eflags_adcox(s, decode, true, can_be_zero);
3209 zero_label = gen_new_label();
3210 tcg_gen_brcondi_tl(TCG_COND_EQ, count, 0, zero_label);
3213 /* Compute high part, including incoming carry. */
3214 if (!have_1bit_cin || TCG_TARGET_deposit_tl_valid(1, TARGET_LONG_BITS - 1)) {
3215 /* high = (T0 << 1) | cin */
3216 TCGv cin = have_1bit_cin ? decode->cc_dst : decode->cc_src;
3217 tcg_gen_deposit_tl(high, cin, s->T0, 1, TARGET_LONG_BITS - 1);
3219 /* Same as above but without deposit; cin in cc_dst. */
3220 tcg_gen_add_tl(high, s->T0, decode->cc_dst);
3221 tcg_gen_add_tl(high, high, s->T0);
3223 tcg_gen_subi_tl(count, count, 1);
3224 tcg_gen_shl_tl(high, high, count);
3226 /* Compute low part and outgoing carry, incoming s->T0 is zero extended */
3227 tcg_gen_xori_tl(low_count, count, (8 << ot) - 1); /* LENGTH - 1 - (count - 1) */
3228 tcg_gen_shr_tl(low, s->T0, low_count);
3229 tcg_gen_andi_tl(decode->cc_dst, low, 1);
3230 tcg_gen_shri_tl(low, low, 1);
3232 /* Compute result and outgoing overflow */
3233 tcg_gen_mov_tl(decode->cc_src2, s->T0);
3234 tcg_gen_or_tl(s->T0, low, high);
3235 gen_rot_overflow(decode, s->T0, decode->cc_src2, false, NULL);
3238 gen_set_label(zero_label);
3242 static void gen_RCR(DisasContext *s, X86DecodedInsn *decode)
3244 bool have_1bit_cin, can_be_zero;
3246 TCGLabel *zero_label = NULL;
3247 MemOp ot = gen_shift_count(s, decode, &can_be_zero, &count, decode->op[2].unit);
3248 TCGv low, high, high_count;
3254 low = tcg_temp_new();
3255 high = tcg_temp_new();
3256 high_count = tcg_temp_new();
3258 gen_rotc_mod(ot, count);
3259 have_1bit_cin = gen_eflags_adcox(s, decode, true, can_be_zero);
3261 zero_label = gen_new_label();
3262 tcg_gen_brcondi_tl(TCG_COND_EQ, count, 0, zero_label);
3265 /* Save incoming carry into high, it will be shifted later. */
3266 if (!have_1bit_cin || TCG_TARGET_deposit_tl_valid(1, TARGET_LONG_BITS - 1)) {
3267 TCGv cin = have_1bit_cin ? decode->cc_dst : decode->cc_src;
3268 tcg_gen_deposit_tl(high, cin, s->T0, 1, TARGET_LONG_BITS - 1);
3270 /* Same as above but without deposit; cin in cc_dst. */
3271 tcg_gen_add_tl(high, s->T0, decode->cc_dst);
3272 tcg_gen_add_tl(high, high, s->T0);
3275 /* Compute low part and outgoing carry, incoming s->T0 is zero extended */
3276 tcg_gen_subi_tl(count, count, 1);
3277 tcg_gen_shr_tl(low, s->T0, count);
3278 tcg_gen_andi_tl(decode->cc_dst, low, 1);
3279 tcg_gen_shri_tl(low, low, 1);
3281 /* Move high part to the right position */
3282 tcg_gen_xori_tl(high_count, count, (8 << ot) - 1); /* LENGTH - 1 - (count - 1) */
3283 tcg_gen_shl_tl(high, high, high_count);
3285 /* Compute result and outgoing overflow */
3286 tcg_gen_mov_tl(decode->cc_src2, s->T0);
3287 tcg_gen_or_tl(s->T0, low, high);
3288 gen_rot_overflow(decode, s->T0, decode->cc_src2, false, NULL);
3291 gen_set_label(zero_label);
3295 #ifdef CONFIG_USER_ONLY
3296 static void gen_unreachable(DisasContext *s, X86DecodedInsn *decode)
3298 g_assert_not_reached();
3302 #ifndef CONFIG_USER_ONLY
3303 static void gen_RDMSR(DisasContext *s, X86DecodedInsn *decode)
3305 gen_update_cc_op(s);
3306 gen_update_eip_cur(s);
3307 gen_helper_rdmsr(tcg_env);
3310 #define gen_RDMSR gen_unreachable
3313 static void gen_RDPMC(DisasContext *s, X86DecodedInsn *decode)
3315 gen_update_cc_op(s);
3316 gen_update_eip_cur(s);
3317 translator_io_start(&s->base);
3318 gen_helper_rdpmc(tcg_env);
3319 s->base.is_jmp = DISAS_NORETURN;
3322 static void gen_RDTSC(DisasContext *s, X86DecodedInsn *decode)
3324 gen_update_cc_op(s);
3325 gen_update_eip_cur(s);
3326 translator_io_start(&s->base);
3327 gen_helper_rdtsc(tcg_env);
3330 static void gen_RDxxBASE(DisasContext *s, X86DecodedInsn *decode)
3332 TCGv base = cpu_seg_base[s->modrm & 8 ? R_GS : R_FS];
3334 /* Preserve hflags bits by testing CR4 at runtime. */
3335 gen_helper_cr4_testbit(tcg_env, tcg_constant_i32(CR4_FSGSBASE_MASK));
3336 tcg_gen_mov_tl(s->T0, base);
3339 static void gen_RET(DisasContext *s, X86DecodedInsn *decode)
3341 int16_t adjust = decode->e.op1 == X86_TYPE_I ? decode->immediate : 0;
3343 MemOp ot = gen_pop_T0(s);
3344 gen_stack_update(s, adjust + (1 << ot));
3345 gen_op_jmp_v(s, s->T0);
3347 s->base.is_jmp = DISAS_JUMP;
3350 static void gen_RETF(DisasContext *s, X86DecodedInsn *decode)
3352 int16_t adjust = decode->e.op1 == X86_TYPE_I ? decode->immediate : 0;
3354 if (!PE(s) || VM86(s)) {
3355 gen_lea_ss_ofs(s, s->A0, cpu_regs[R_ESP], 0);
3357 gen_op_ld_v(s, s->dflag, s->T0, s->A0);
3358 /* NOTE: keeping EIP updated is not a problem in case of
3360 gen_op_jmp_v(s, s->T0);
3362 gen_add_A0_im(s, 1 << s->dflag);
3363 gen_op_ld_v(s, s->dflag, s->T0, s->A0);
3364 gen_op_movl_seg_real(s, R_CS, s->T0);
3365 /* add stack offset */
3366 gen_stack_update(s, adjust + (2 << s->dflag));
3368 gen_update_cc_op(s);
3369 gen_update_eip_cur(s);
3370 gen_helper_lret_protected(tcg_env, tcg_constant_i32(s->dflag - 1),
3371 tcg_constant_i32(adjust));
3373 s->base.is_jmp = DISAS_EOB_ONLY;
3377 * Return non-NULL if a 32-bit rotate works, after possibly replicating the input.
3378 * The input has already been zero-extended upon operand decode.
3380 static TCGv_i32 gen_rot_replicate(MemOp ot, TCGv in)
3385 temp = tcg_temp_new_i32();
3386 tcg_gen_trunc_tl_i32(temp, in);
3387 tcg_gen_muli_i32(temp, temp, 0x01010101);
3391 temp = tcg_temp_new_i32();
3392 tcg_gen_trunc_tl_i32(temp, in);
3393 tcg_gen_deposit_i32(temp, temp, temp, 16, 16);
3396 #ifdef TARGET_X86_64
3398 temp = tcg_temp_new_i32();
3399 tcg_gen_trunc_tl_i32(temp, in);
3408 static void gen_rot_carry(X86DecodedInsn *decode, TCGv result,
3409 bool can_be_zero, TCGv count, int bit)
3412 tcg_gen_extract_tl(decode->cc_dst, result, bit, 1);
3414 TCGv temp = tcg_temp_new();
3415 tcg_gen_extract_tl(temp, result, bit, 1);
3416 tcg_gen_movcond_tl(TCG_COND_EQ, decode->cc_dst, count, tcg_constant_tl(0),
3417 decode->cc_dst, temp);
3421 static void gen_ROL(DisasContext *s, X86DecodedInsn *decode)
3425 MemOp ot = gen_shift_count(s, decode, &can_be_zero, &count, decode->op[2].unit);
3426 TCGv_i32 temp32, count32;
3427 TCGv old = tcg_temp_new();
3433 gen_eflags_adcox(s, decode, false, can_be_zero);
3434 tcg_gen_mov_tl(old, s->T0);
3435 temp32 = gen_rot_replicate(ot, s->T0);
3437 count32 = tcg_temp_new_i32();
3438 tcg_gen_trunc_tl_i32(count32, count);
3439 tcg_gen_rotl_i32(temp32, temp32, count32);
3440 /* Zero extend to facilitate later optimization. */
3441 tcg_gen_extu_i32_tl(s->T0, temp32);
3443 tcg_gen_rotl_tl(s->T0, s->T0, count);
3445 gen_rot_carry(decode, s->T0, can_be_zero, count, 0);
3446 gen_rot_overflow(decode, s->T0, old, can_be_zero, count);
3449 static void gen_ROR(DisasContext *s, X86DecodedInsn *decode)
3453 MemOp ot = gen_shift_count(s, decode, &can_be_zero, &count, decode->op[2].unit);
3454 TCGv_i32 temp32, count32;
3455 TCGv old = tcg_temp_new();
3461 gen_eflags_adcox(s, decode, false, can_be_zero);
3462 tcg_gen_mov_tl(old, s->T0);
3463 temp32 = gen_rot_replicate(ot, s->T0);
3465 count32 = tcg_temp_new_i32();
3466 tcg_gen_trunc_tl_i32(count32, count);
3467 tcg_gen_rotr_i32(temp32, temp32, count32);
3468 /* Zero extend to facilitate later optimization. */
3469 tcg_gen_extu_i32_tl(s->T0, temp32);
3470 gen_rot_carry(decode, s->T0, can_be_zero, count, 31);
3472 tcg_gen_rotr_tl(s->T0, s->T0, count);
3473 gen_rot_carry(decode, s->T0, can_be_zero, count, TARGET_LONG_BITS - 1);
3475 gen_rot_overflow(decode, s->T0, old, can_be_zero, count);
3478 static void gen_RORX(DisasContext *s, X86DecodedInsn *decode)
3480 MemOp ot = decode->op[0].ot;
3481 int mask = ot == MO_64 ? 63 : 31;
3482 int b = decode->immediate & mask;
3486 #ifdef TARGET_X86_64
3487 tcg_gen_trunc_tl_i32(s->tmp2_i32, s->T0);
3488 tcg_gen_rotri_i32(s->tmp2_i32, s->tmp2_i32, b);
3489 tcg_gen_extu_i32_tl(s->T0, s->tmp2_i32);
3494 tcg_gen_rotri_tl(s->T0, s->T0, b);
3498 g_assert_not_reached();
3502 #ifndef CONFIG_USER_ONLY
3503 static void gen_RSM(DisasContext *s, X86DecodedInsn *decode)
3505 gen_helper_rsm(tcg_env);
3506 assume_cc_op(s, CC_OP_EFLAGS);
3507 s->base.is_jmp = DISAS_EOB_ONLY;
3510 #define gen_RSM gen_UD
3513 static void gen_SAHF(DisasContext *s, X86DecodedInsn *decode)
3515 if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM)) {
3516 return gen_illegal_opcode(s);
3518 tcg_gen_shri_tl(s->T0, cpu_regs[R_EAX], 8);
3519 gen_compute_eflags(s);
3520 tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, CC_O);
3521 tcg_gen_andi_tl(s->T0, s->T0, CC_S | CC_Z | CC_A | CC_P | CC_C);
3522 tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, s->T0);
3525 static void gen_SALC(DisasContext *s, X86DecodedInsn *decode)
3527 gen_compute_eflags_c(s, s->T0);
3528 tcg_gen_neg_tl(s->T0, s->T0);
3531 static void gen_shift_dynamic_flags(DisasContext *s, X86DecodedInsn *decode, TCGv count, CCOp cc_op)
3533 TCGv_i32 count32 = tcg_temp_new_i32();
3536 decode->cc_op = CC_OP_DYNAMIC;
3537 decode->cc_op_dynamic = tcg_temp_new_i32();
3539 assert(decode->cc_dst == s->T0);
3540 if (cc_op_live[s->cc_op] & USES_CC_DST) {
3541 decode->cc_dst = tcg_temp_new();
3542 tcg_gen_movcond_tl(TCG_COND_EQ, decode->cc_dst, count, tcg_constant_tl(0),
3546 if (cc_op_live[s->cc_op] & USES_CC_SRC) {
3547 tcg_gen_movcond_tl(TCG_COND_EQ, decode->cc_src, count, tcg_constant_tl(0),
3548 cpu_cc_src, decode->cc_src);
3551 tcg_gen_trunc_tl_i32(count32, count);
3552 if (s->cc_op == CC_OP_DYNAMIC) {
3553 old_cc_op = cpu_cc_op;
3555 old_cc_op = tcg_constant_i32(s->cc_op);
3557 tcg_gen_movcond_i32(TCG_COND_EQ, decode->cc_op_dynamic, count32, tcg_constant_i32(0),
3558 old_cc_op, tcg_constant_i32(cc_op));
3561 static void gen_SAR(DisasContext *s, X86DecodedInsn *decode)
3565 MemOp ot = gen_shift_count(s, decode, &can_be_zero, &count, decode->op[2].unit);
3571 decode->cc_dst = s->T0;
3572 decode->cc_src = tcg_temp_new();
3573 tcg_gen_subi_tl(decode->cc_src, count, 1);
3574 tcg_gen_sar_tl(decode->cc_src, s->T0, decode->cc_src);
3575 tcg_gen_sar_tl(s->T0, s->T0, count);
3577 gen_shift_dynamic_flags(s, decode, count, CC_OP_SARB + ot);
3579 decode->cc_op = CC_OP_SARB + ot;
3583 static void gen_SARX(DisasContext *s, X86DecodedInsn *decode)
3585 MemOp ot = decode->op[0].ot;
3588 mask = ot == MO_64 ? 63 : 31;
3589 tcg_gen_andi_tl(s->T1, s->T1, mask);
3590 tcg_gen_sar_tl(s->T0, s->T0, s->T1);
3593 static void gen_SBB(DisasContext *s, X86DecodedInsn *decode)
3595 MemOp ot = decode->op[0].ot;
3596 TCGv c_in = tcg_temp_new();
3598 gen_compute_eflags_c(s, c_in);
3599 if (s->prefix & PREFIX_LOCK) {
3600 tcg_gen_add_tl(s->T0, s->T1, c_in);
3601 tcg_gen_neg_tl(s->T0, s->T0);
3602 tcg_gen_atomic_add_fetch_tl(s->T0, s->A0, s->T0,
3603 s->mem_index, ot | MO_LE);
3606 * TODO: SBB reg, reg could use gen_prepare_eflags_c followed by
3607 * negsetcond, and CC_OP_SUBB as the cc_op.
3609 tcg_gen_sub_tl(s->T0, s->T0, s->T1);
3610 tcg_gen_sub_tl(s->T0, s->T0, c_in);
3612 prepare_update3_cc(decode, s, CC_OP_SBBB + ot, c_in);
3615 static void gen_SCAS(DisasContext *s, X86DecodedInsn *decode)
3617 MemOp ot = decode->op[2].ot;
3618 if (s->prefix & (PREFIX_REPZ | PREFIX_REPNZ)) {
3619 gen_repz_nz(s, ot, gen_scas);
3625 static void gen_SETcc(DisasContext *s, X86DecodedInsn *decode)
3627 gen_setcc1(s, decode->b & 0xf, s->T0);
3630 static void gen_SFENCE(DisasContext *s, X86DecodedInsn *decode)
3632 tcg_gen_mb(TCG_MO_ST_ST | TCG_BAR_SC);
3635 static void gen_SHA1NEXTE(DisasContext *s, X86DecodedInsn *decode)
3637 gen_helper_sha1nexte(OP_PTR0, OP_PTR1, OP_PTR2);
3640 static void gen_SHA1MSG1(DisasContext *s, X86DecodedInsn *decode)
3642 gen_helper_sha1msg1(OP_PTR0, OP_PTR1, OP_PTR2);
3645 static void gen_SHA1MSG2(DisasContext *s, X86DecodedInsn *decode)
3647 gen_helper_sha1msg2(OP_PTR0, OP_PTR1, OP_PTR2);
3650 static void gen_SHA1RNDS4(DisasContext *s, X86DecodedInsn *decode)
3652 switch(decode->immediate & 3) {
3654 gen_helper_sha1rnds4_f0(OP_PTR0, OP_PTR0, OP_PTR1);
3657 gen_helper_sha1rnds4_f1(OP_PTR0, OP_PTR0, OP_PTR1);
3660 gen_helper_sha1rnds4_f2(OP_PTR0, OP_PTR0, OP_PTR1);
3663 gen_helper_sha1rnds4_f3(OP_PTR0, OP_PTR0, OP_PTR1);
3668 static void gen_SHA256MSG1(DisasContext *s, X86DecodedInsn *decode)
3670 gen_helper_sha256msg1(OP_PTR0, OP_PTR1, OP_PTR2);
3673 static void gen_SHA256MSG2(DisasContext *s, X86DecodedInsn *decode)
3675 gen_helper_sha256msg2(OP_PTR0, OP_PTR1, OP_PTR2);
3678 static void gen_SHA256RNDS2(DisasContext *s, X86DecodedInsn *decode)
3680 TCGv_i32 wk0 = tcg_temp_new_i32();
3681 TCGv_i32 wk1 = tcg_temp_new_i32();
3683 tcg_gen_ld_i32(wk0, tcg_env, ZMM_OFFSET(0) + offsetof(ZMMReg, ZMM_L(0)));
3684 tcg_gen_ld_i32(wk1, tcg_env, ZMM_OFFSET(0) + offsetof(ZMMReg, ZMM_L(1)));
3686 gen_helper_sha256rnds2(OP_PTR0, OP_PTR1, OP_PTR2, wk0, wk1);
3689 static void gen_SHL(DisasContext *s, X86DecodedInsn *decode)
3693 MemOp ot = gen_shift_count(s, decode, &can_be_zero, &count, decode->op[2].unit);
3699 decode->cc_dst = s->T0;
3700 decode->cc_src = tcg_temp_new();
3701 tcg_gen_subi_tl(decode->cc_src, count, 1);
3702 tcg_gen_shl_tl(decode->cc_src, s->T0, decode->cc_src);
3703 tcg_gen_shl_tl(s->T0, s->T0, count);
3705 gen_shift_dynamic_flags(s, decode, count, CC_OP_SHLB + ot);
3707 decode->cc_op = CC_OP_SHLB + ot;
3711 static void gen_SHLD(DisasContext *s, X86DecodedInsn *decode)
3715 int unit = decode->e.op3 == X86_TYPE_I ? X86_OP_IMM : X86_OP_INT;
3716 MemOp ot = gen_shift_count(s, decode, &can_be_zero, &count, unit);
3722 decode->cc_dst = s->T0;
3723 decode->cc_src = s->tmp0;
3724 gen_shiftd_rm_T1(s, ot, false, count);
3726 gen_shift_dynamic_flags(s, decode, count, CC_OP_SHLB + ot);
3728 decode->cc_op = CC_OP_SHLB + ot;
3732 static void gen_SHLX(DisasContext *s, X86DecodedInsn *decode)
3734 MemOp ot = decode->op[0].ot;
3737 mask = ot == MO_64 ? 63 : 31;
3738 tcg_gen_andi_tl(s->T1, s->T1, mask);
3739 tcg_gen_shl_tl(s->T0, s->T0, s->T1);
3742 static void gen_SHR(DisasContext *s, X86DecodedInsn *decode)
3746 MemOp ot = gen_shift_count(s, decode, &can_be_zero, &count, decode->op[2].unit);
3752 decode->cc_dst = s->T0;
3753 decode->cc_src = tcg_temp_new();
3754 tcg_gen_subi_tl(decode->cc_src, count, 1);
3755 tcg_gen_shr_tl(decode->cc_src, s->T0, decode->cc_src);
3756 tcg_gen_shr_tl(s->T0, s->T0, count);
3758 gen_shift_dynamic_flags(s, decode, count, CC_OP_SARB + ot);
3760 decode->cc_op = CC_OP_SARB + ot;
3764 static void gen_SHRD(DisasContext *s, X86DecodedInsn *decode)
3768 int unit = decode->e.op3 == X86_TYPE_I ? X86_OP_IMM : X86_OP_INT;
3769 MemOp ot = gen_shift_count(s, decode, &can_be_zero, &count, unit);
3775 decode->cc_dst = s->T0;
3776 decode->cc_src = s->tmp0;
3777 gen_shiftd_rm_T1(s, ot, true, count);
3779 gen_shift_dynamic_flags(s, decode, count, CC_OP_SARB + ot);
3781 decode->cc_op = CC_OP_SARB + ot;
3785 static void gen_SHRX(DisasContext *s, X86DecodedInsn *decode)
3787 MemOp ot = decode->op[0].ot;
3790 mask = ot == MO_64 ? 63 : 31;
3791 tcg_gen_andi_tl(s->T1, s->T1, mask);
3792 tcg_gen_shr_tl(s->T0, s->T0, s->T1);
3795 static void gen_STC(DisasContext *s, X86DecodedInsn *decode)
3797 gen_compute_eflags(s);
3798 tcg_gen_ori_tl(cpu_cc_src, cpu_cc_src, CC_C);
3801 static void gen_STD(DisasContext *s, X86DecodedInsn *decode)
3803 tcg_gen_st_i32(tcg_constant_i32(-1), tcg_env, offsetof(CPUX86State, df));
3806 static void gen_STI(DisasContext *s, X86DecodedInsn *decode)
3808 gen_set_eflags(s, IF_MASK);
3809 s->base.is_jmp = DISAS_EOB_INHIBIT_IRQ;
3812 static void gen_VAESKEYGEN(DisasContext *s, X86DecodedInsn *decode)
3814 TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
3816 gen_helper_aeskeygenassist_xmm(tcg_env, OP_PTR0, OP_PTR1, imm);
3819 static void gen_STMXCSR(DisasContext *s, X86DecodedInsn *decode)
3821 gen_helper_update_mxcsr(tcg_env);
3822 tcg_gen_ld32u_tl(s->T0, tcg_env, offsetof(CPUX86State, mxcsr));
3825 static void gen_STOS(DisasContext *s, X86DecodedInsn *decode)
3827 MemOp ot = decode->op[1].ot;
3828 if (s->prefix & (PREFIX_REPZ | PREFIX_REPNZ)) {
3829 gen_repz(s, ot, gen_stos);
3835 static void gen_SUB(DisasContext *s, X86DecodedInsn *decode)
3837 MemOp ot = decode->op[1].ot;
3839 if (s->prefix & PREFIX_LOCK) {
3840 tcg_gen_neg_tl(s->T0, s->T1);
3841 tcg_gen_atomic_fetch_add_tl(s->cc_srcT, s->A0, s->T0,
3842 s->mem_index, ot | MO_LE);
3843 tcg_gen_sub_tl(s->T0, s->cc_srcT, s->T1);
3845 tcg_gen_mov_tl(s->cc_srcT, s->T0);
3846 tcg_gen_sub_tl(s->T0, s->T0, s->T1);
3848 prepare_update2_cc(decode, s, CC_OP_SUBB + ot);
3851 static void gen_SYSCALL(DisasContext *s, X86DecodedInsn *decode)
3853 gen_update_cc_op(s);
3854 gen_update_eip_cur(s);
3855 gen_helper_syscall(tcg_env, cur_insn_len_i32(s));
3857 assume_cc_op(s, CC_OP_EFLAGS);
3861 * TF handling for the syscall insn is different. The TF bit is checked
3862 * after the syscall insn completes. This allows #DB to not be
3863 * generated after one has entered CPL0 if TF is set in FMASK.
3865 s->base.is_jmp = DISAS_EOB_RECHECK_TF;
3868 static void gen_SYSENTER(DisasContext *s, X86DecodedInsn *decode)
3870 gen_helper_sysenter(tcg_env);
3871 s->base.is_jmp = DISAS_EOB_ONLY;
3874 static void gen_SYSEXIT(DisasContext *s, X86DecodedInsn *decode)
3876 gen_helper_sysexit(tcg_env, tcg_constant_i32(s->dflag - 1));
3877 s->base.is_jmp = DISAS_EOB_ONLY;
3880 static void gen_SYSRET(DisasContext *s, X86DecodedInsn *decode)
3882 gen_helper_sysret(tcg_env, tcg_constant_i32(s->dflag - 1));
3884 assume_cc_op(s, CC_OP_EFLAGS);
3888 * TF handling for the sysret insn is different. The TF bit is checked
3889 * after the sysret insn completes. This allows #DB to be
3890 * generated "as if" the syscall insn in userspace has just
3893 s->base.is_jmp = DISAS_EOB_RECHECK_TF;
3896 static void gen_TZCNT(DisasContext *s, X86DecodedInsn *decode)
3898 MemOp ot = decode->op[0].ot;
3900 /* C bit (cc_src) is defined related to the input. */
3901 decode->cc_src = tcg_temp_new();
3902 decode->cc_dst = s->T0;
3903 decode->cc_op = CC_OP_BMILGB + ot;
3904 tcg_gen_mov_tl(decode->cc_src, s->T0);
3906 /* A zero input returns the operand size. */
3907 tcg_gen_ctzi_tl(s->T0, s->T0, 8 << ot);
3910 static void gen_UD(DisasContext *s, X86DecodedInsn *decode)
3912 gen_illegal_opcode(s);
3915 static void gen_VAESIMC(DisasContext *s, X86DecodedInsn *decode)
3918 gen_helper_aesimc_xmm(tcg_env, OP_PTR0, OP_PTR2);
3922 * 00 = v*ps Vps, Hps, Wpd
3923 * 66 = v*pd Vpd, Hpd, Wps
3924 * f3 = v*ss Vss, Hss, Wps
3925 * f2 = v*sd Vsd, Hsd, Wps
3927 #define SSE_CMP(x) { \
3928 gen_helper_ ## x ## ps ## _xmm, gen_helper_ ## x ## pd ## _xmm, \
3929 gen_helper_ ## x ## ss, gen_helper_ ## x ## sd, \
3930 gen_helper_ ## x ## ps ## _ymm, gen_helper_ ## x ## pd ## _ymm}
3931 static const SSEFunc_0_eppp gen_helper_cmp_funcs[32][6] = {
3970 static void gen_VCMP(DisasContext *s, X86DecodedInsn *decode)
3972 int index = decode->immediate & (s->prefix & PREFIX_VEX ? 31 : 7);
3974 s->prefix & PREFIX_REPZ ? 2 /* ss */ :
3975 s->prefix & PREFIX_REPNZ ? 3 /* sd */ :
3976 !!(s->prefix & PREFIX_DATA) /* pd */ + (s->vex_l << 2);
3978 gen_helper_cmp_funcs[index][b](tcg_env, OP_PTR0, OP_PTR1, OP_PTR2);
3981 static void gen_VCOMI(DisasContext *s, X86DecodedInsn *decode)
3984 fn = s->prefix & PREFIX_DATA ? gen_helper_comisd : gen_helper_comiss;
3985 fn(tcg_env, OP_PTR1, OP_PTR2);
3986 assume_cc_op(s, CC_OP_EFLAGS);
3989 static void gen_VCVTPD2PS(DisasContext *s, X86DecodedInsn *decode)
3992 gen_helper_cvtpd2ps_ymm(tcg_env, OP_PTR0, OP_PTR2);
3994 gen_helper_cvtpd2ps_xmm(tcg_env, OP_PTR0, OP_PTR2);
3998 static void gen_VCVTPS2PD(DisasContext *s, X86DecodedInsn *decode)
4001 gen_helper_cvtps2pd_ymm(tcg_env, OP_PTR0, OP_PTR2);
4003 gen_helper_cvtps2pd_xmm(tcg_env, OP_PTR0, OP_PTR2);
4007 static void gen_VCVTPS2PH(DisasContext *s, X86DecodedInsn *decode)
4009 gen_unary_imm_fp_sse(s, decode,
4010 gen_helper_cvtps2ph_xmm,
4011 gen_helper_cvtps2ph_ymm);
4013 * VCVTPS2PH is the only instruction that performs an operation on a
4014 * register source and then *stores* into memory.
4016 if (decode->op[0].has_ea) {
4017 gen_store_sse(s, decode, decode->op[0].offset);
4021 static void gen_VCVTSD2SS(DisasContext *s, X86DecodedInsn *decode)
4023 gen_helper_cvtsd2ss(tcg_env, OP_PTR0, OP_PTR1, OP_PTR2);
4026 static void gen_VCVTSS2SD(DisasContext *s, X86DecodedInsn *decode)
4028 gen_helper_cvtss2sd(tcg_env, OP_PTR0, OP_PTR1, OP_PTR2);
4031 static void gen_VCVTSI2Sx(DisasContext *s, X86DecodedInsn *decode)
4033 int vec_len = vector_len(s, decode);
4036 tcg_gen_gvec_mov(MO_64, decode->op[0].offset, decode->op[1].offset, vec_len, vec_len);
4038 #ifdef TARGET_X86_64
4039 MemOp ot = decode->op[2].ot;
4041 if (s->prefix & PREFIX_REPNZ) {
4042 gen_helper_cvtsq2sd(tcg_env, OP_PTR0, s->T1);
4044 gen_helper_cvtsq2ss(tcg_env, OP_PTR0, s->T1);
4049 tcg_gen_trunc_tl_i32(in, s->T1);
4054 if (s->prefix & PREFIX_REPNZ) {
4055 gen_helper_cvtsi2sd(tcg_env, OP_PTR0, in);
4057 gen_helper_cvtsi2ss(tcg_env, OP_PTR0, in);
4061 static inline void gen_VCVTtSx2SI(DisasContext *s, X86DecodedInsn *decode,
4062 SSEFunc_i_ep ss2si, SSEFunc_l_ep ss2sq,
4063 SSEFunc_i_ep sd2si, SSEFunc_l_ep sd2sq)
4067 #ifdef TARGET_X86_64
4068 MemOp ot = decode->op[0].ot;
4070 if (s->prefix & PREFIX_REPNZ) {
4071 sd2sq(s->T0, tcg_env, OP_PTR2);
4073 ss2sq(s->T0, tcg_env, OP_PTR2);
4082 if (s->prefix & PREFIX_REPNZ) {
4083 sd2si(out, tcg_env, OP_PTR2);
4085 ss2si(out, tcg_env, OP_PTR2);
4087 #ifdef TARGET_X86_64
4088 tcg_gen_extu_i32_tl(s->T0, out);
4092 #ifndef TARGET_X86_64
4093 #define gen_helper_cvtss2sq NULL
4094 #define gen_helper_cvtsd2sq NULL
4095 #define gen_helper_cvttss2sq NULL
4096 #define gen_helper_cvttsd2sq NULL
4099 static void gen_VCVTSx2SI(DisasContext *s, X86DecodedInsn *decode)
4101 gen_VCVTtSx2SI(s, decode,
4102 gen_helper_cvtss2si, gen_helper_cvtss2sq,
4103 gen_helper_cvtsd2si, gen_helper_cvtsd2sq);
4106 static void gen_VCVTTSx2SI(DisasContext *s, X86DecodedInsn *decode)
4108 gen_VCVTtSx2SI(s, decode,
4109 gen_helper_cvttss2si, gen_helper_cvttss2sq,
4110 gen_helper_cvttsd2si, gen_helper_cvttsd2sq);
4113 static void gen_VEXTRACTx128(DisasContext *s, X86DecodedInsn *decode)
4115 int mask = decode->immediate & 1;
4116 int src_ofs = vector_elem_offset(&decode->op[1], MO_128, mask);
4117 if (decode->op[0].has_ea) {
4118 /* VEX-only instruction, no alignment requirements. */
4119 gen_sto_env_A0(s, src_ofs, false);
4121 tcg_gen_gvec_mov(MO_64, decode->op[0].offset, src_ofs, 16, 16);
4125 static void gen_VEXTRACTPS(DisasContext *s, X86DecodedInsn *decode)
4127 gen_pextr(s, decode, MO_32);
4130 static void gen_vinsertps(DisasContext *s, X86DecodedInsn *decode)
4132 int val = decode->immediate;
4133 int dest_word = (val >> 4) & 3;
4134 int new_mask = (val & 15) | (1 << dest_word);
4139 if (new_mask == 15) {
4140 /* All zeroes except possibly for the inserted element */
4141 tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
4142 } else if (decode->op[1].offset != decode->op[0].offset) {
4143 gen_store_sse(s, decode, decode->op[1].offset);
4146 if (new_mask != (val & 15)) {
4147 tcg_gen_st_i32(s->tmp2_i32, tcg_env,
4148 vector_elem_offset(&decode->op[0], MO_32, dest_word));
4151 if (new_mask != 15) {
4152 TCGv_i32 zero = tcg_constant_i32(0); /* float32_zero */
4154 for (i = 0; i < 4; i++) {
4155 if ((val >> i) & 1) {
4156 tcg_gen_st_i32(zero, tcg_env,
4157 vector_elem_offset(&decode->op[0], MO_32, i));
4163 static void gen_VINSERTPS_r(DisasContext *s, X86DecodedInsn *decode)
4165 int val = decode->immediate;
4166 tcg_gen_ld_i32(s->tmp2_i32, tcg_env,
4167 vector_elem_offset(&decode->op[2], MO_32, (val >> 6) & 3));
4168 gen_vinsertps(s, decode);
4171 static void gen_VINSERTPS_m(DisasContext *s, X86DecodedInsn *decode)
4173 tcg_gen_qemu_ld_i32(s->tmp2_i32, s->A0, s->mem_index, MO_LEUL);
4174 gen_vinsertps(s, decode);
4177 static void gen_VINSERTx128(DisasContext *s, X86DecodedInsn *decode)
4179 int mask = decode->immediate & 1;
4180 tcg_gen_gvec_mov(MO_64,
4181 decode->op[0].offset + offsetof(YMMReg, YMM_X(mask)),
4182 decode->op[2].offset + offsetof(YMMReg, YMM_X(0)), 16, 16);
4183 tcg_gen_gvec_mov(MO_64,
4184 decode->op[0].offset + offsetof(YMMReg, YMM_X(!mask)),
4185 decode->op[1].offset + offsetof(YMMReg, YMM_X(!mask)), 16, 16);
4188 static inline void gen_maskmov(DisasContext *s, X86DecodedInsn *decode,
4189 SSEFunc_0_eppt xmm, SSEFunc_0_eppt ymm)
4192 xmm(tcg_env, OP_PTR2, OP_PTR1, s->A0);
4194 ymm(tcg_env, OP_PTR2, OP_PTR1, s->A0);
4198 static void gen_VMASKMOVPD_st(DisasContext *s, X86DecodedInsn *decode)
4200 gen_maskmov(s, decode, gen_helper_vpmaskmovq_st_xmm, gen_helper_vpmaskmovq_st_ymm);
4203 static void gen_VMASKMOVPS_st(DisasContext *s, X86DecodedInsn *decode)
4205 gen_maskmov(s, decode, gen_helper_vpmaskmovd_st_xmm, gen_helper_vpmaskmovd_st_ymm);
4208 static void gen_VMOVHPx_ld(DisasContext *s, X86DecodedInsn *decode)
4210 gen_ldq_env_A0(s, decode->op[0].offset + offsetof(XMMReg, XMM_Q(1)));
4211 if (decode->op[0].offset != decode->op[1].offset) {
4212 tcg_gen_ld_i64(s->tmp1_i64, tcg_env, decode->op[1].offset + offsetof(XMMReg, XMM_Q(0)));
4213 tcg_gen_st_i64(s->tmp1_i64, tcg_env, decode->op[0].offset + offsetof(XMMReg, XMM_Q(0)));
4217 static void gen_VMOVHPx_st(DisasContext *s, X86DecodedInsn *decode)
4219 gen_stq_env_A0(s, decode->op[2].offset + offsetof(XMMReg, XMM_Q(1)));
4222 static void gen_VMOVHPx(DisasContext *s, X86DecodedInsn *decode)
4224 if (decode->op[0].offset != decode->op[2].offset) {
4225 tcg_gen_ld_i64(s->tmp1_i64, tcg_env, decode->op[2].offset + offsetof(XMMReg, XMM_Q(1)));
4226 tcg_gen_st_i64(s->tmp1_i64, tcg_env, decode->op[0].offset + offsetof(XMMReg, XMM_Q(1)));
4228 if (decode->op[0].offset != decode->op[1].offset) {
4229 tcg_gen_ld_i64(s->tmp1_i64, tcg_env, decode->op[1].offset + offsetof(XMMReg, XMM_Q(0)));
4230 tcg_gen_st_i64(s->tmp1_i64, tcg_env, decode->op[0].offset + offsetof(XMMReg, XMM_Q(0)));
4234 static void gen_VMOVHLPS(DisasContext *s, X86DecodedInsn *decode)
4236 tcg_gen_ld_i64(s->tmp1_i64, tcg_env, decode->op[2].offset + offsetof(XMMReg, XMM_Q(1)));
4237 tcg_gen_st_i64(s->tmp1_i64, tcg_env, decode->op[0].offset + offsetof(XMMReg, XMM_Q(0)));
4238 if (decode->op[0].offset != decode->op[1].offset) {
4239 tcg_gen_ld_i64(s->tmp1_i64, tcg_env, decode->op[1].offset + offsetof(XMMReg, XMM_Q(1)));
4240 tcg_gen_st_i64(s->tmp1_i64, tcg_env, decode->op[0].offset + offsetof(XMMReg, XMM_Q(1)));
4244 static void gen_VMOVLHPS(DisasContext *s, X86DecodedInsn *decode)
4246 tcg_gen_ld_i64(s->tmp1_i64, tcg_env, decode->op[2].offset);
4247 tcg_gen_st_i64(s->tmp1_i64, tcg_env, decode->op[0].offset + offsetof(XMMReg, XMM_Q(1)));
4248 if (decode->op[0].offset != decode->op[1].offset) {
4249 tcg_gen_ld_i64(s->tmp1_i64, tcg_env, decode->op[1].offset + offsetof(XMMReg, XMM_Q(0)));
4250 tcg_gen_st_i64(s->tmp1_i64, tcg_env, decode->op[0].offset + offsetof(XMMReg, XMM_Q(0)));
4255 * Note that MOVLPx supports 256-bit operation unlike MOVHLPx, MOVLHPx, MOXHPx.
4256 * Use a gvec move to move everything above the bottom 64 bits.
4259 static void gen_VMOVLPx(DisasContext *s, X86DecodedInsn *decode)
4261 int vec_len = vector_len(s, decode);
4263 tcg_gen_ld_i64(s->tmp1_i64, tcg_env, decode->op[2].offset + offsetof(XMMReg, XMM_Q(0)));
4264 tcg_gen_gvec_mov(MO_64, decode->op[0].offset, decode->op[1].offset, vec_len, vec_len);
4265 tcg_gen_st_i64(s->tmp1_i64, tcg_env, decode->op[0].offset + offsetof(XMMReg, XMM_Q(0)));
4268 static void gen_VMOVLPx_ld(DisasContext *s, X86DecodedInsn *decode)
4270 int vec_len = vector_len(s, decode);
4272 tcg_gen_qemu_ld_i64(s->tmp1_i64, s->A0, s->mem_index, MO_LEUQ);
4273 tcg_gen_gvec_mov(MO_64, decode->op[0].offset, decode->op[1].offset, vec_len, vec_len);
4274 tcg_gen_st_i64(s->tmp1_i64, OP_PTR0, offsetof(ZMMReg, ZMM_Q(0)));
4277 static void gen_VMOVLPx_st(DisasContext *s, X86DecodedInsn *decode)
4279 tcg_gen_ld_i64(s->tmp1_i64, OP_PTR2, offsetof(ZMMReg, ZMM_Q(0)));
4280 tcg_gen_qemu_st_i64(s->tmp1_i64, s->A0, s->mem_index, MO_LEUQ);
4283 static void gen_VMOVSD_ld(DisasContext *s, X86DecodedInsn *decode)
4285 TCGv_i64 zero = tcg_constant_i64(0);
4287 tcg_gen_qemu_ld_i64(s->tmp1_i64, s->A0, s->mem_index, MO_LEUQ);
4288 tcg_gen_st_i64(zero, OP_PTR0, offsetof(ZMMReg, ZMM_Q(1)));
4289 tcg_gen_st_i64(s->tmp1_i64, OP_PTR0, offsetof(ZMMReg, ZMM_Q(0)));
4292 static void gen_VMOVSS(DisasContext *s, X86DecodedInsn *decode)
4294 int vec_len = vector_len(s, decode);
4296 tcg_gen_ld_i32(s->tmp2_i32, OP_PTR2, offsetof(ZMMReg, ZMM_L(0)));
4297 tcg_gen_gvec_mov(MO_64, decode->op[0].offset, decode->op[1].offset, vec_len, vec_len);
4298 tcg_gen_st_i32(s->tmp2_i32, OP_PTR0, offsetof(ZMMReg, ZMM_L(0)));
4301 static void gen_VMOVSS_ld(DisasContext *s, X86DecodedInsn *decode)
4303 int vec_len = vector_len(s, decode);
4305 tcg_gen_qemu_ld_i32(s->tmp2_i32, s->A0, s->mem_index, MO_LEUL);
4306 tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
4307 tcg_gen_st_i32(s->tmp2_i32, OP_PTR0, offsetof(ZMMReg, ZMM_L(0)));
4310 static void gen_VMOVSS_st(DisasContext *s, X86DecodedInsn *decode)
4312 tcg_gen_ld_i32(s->tmp2_i32, OP_PTR2, offsetof(ZMMReg, ZMM_L(0)));
4313 tcg_gen_qemu_st_i32(s->tmp2_i32, s->A0, s->mem_index, MO_LEUL);
4316 static void gen_VPMASKMOV_st(DisasContext *s, X86DecodedInsn *decode)
4319 gen_VMASKMOVPD_st(s, decode);
4321 gen_VMASKMOVPS_st(s, decode);
4325 static void gen_VPERMD(DisasContext *s, X86DecodedInsn *decode)
4328 gen_helper_vpermd_ymm(OP_PTR0, OP_PTR1, OP_PTR2);
4331 static void gen_VPERM2x128(DisasContext *s, X86DecodedInsn *decode)
4333 TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
4335 gen_helper_vpermdq_ymm(OP_PTR0, OP_PTR1, OP_PTR2, imm);
4338 static void gen_VPHMINPOSUW(DisasContext *s, X86DecodedInsn *decode)
4341 gen_helper_phminposuw_xmm(tcg_env, OP_PTR0, OP_PTR2);
4344 static void gen_VROUNDSD(DisasContext *s, X86DecodedInsn *decode)
4346 TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
4348 gen_helper_roundsd_xmm(tcg_env, OP_PTR0, OP_PTR1, OP_PTR2, imm);
4351 static void gen_VROUNDSS(DisasContext *s, X86DecodedInsn *decode)
4353 TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
4355 gen_helper_roundss_xmm(tcg_env, OP_PTR0, OP_PTR1, OP_PTR2, imm);
4358 static void gen_VSHUF(DisasContext *s, X86DecodedInsn *decode)
4360 TCGv_i32 imm = tcg_constant_i32(decode->immediate);
4361 SSEFunc_0_pppi ps, pd, fn;
4362 ps = s->vex_l ? gen_helper_shufps_ymm : gen_helper_shufps_xmm;
4363 pd = s->vex_l ? gen_helper_shufpd_ymm : gen_helper_shufpd_xmm;
4364 fn = s->prefix & PREFIX_DATA ? pd : ps;
4365 fn(OP_PTR0, OP_PTR1, OP_PTR2, imm);
4368 static void gen_VUCOMI(DisasContext *s, X86DecodedInsn *decode)
4371 fn = s->prefix & PREFIX_DATA ? gen_helper_ucomisd : gen_helper_ucomiss;
4372 fn(tcg_env, OP_PTR1, OP_PTR2);
4373 assume_cc_op(s, CC_OP_EFLAGS);
4376 static void gen_VZEROALL(DisasContext *s, X86DecodedInsn *decode)
4378 TCGv_ptr ptr = tcg_temp_new_ptr();
4380 tcg_gen_addi_ptr(ptr, tcg_env, offsetof(CPUX86State, xmm_regs));
4381 gen_helper_memset(ptr, ptr, tcg_constant_i32(0),
4382 tcg_constant_ptr(CPU_NB_REGS * sizeof(ZMMReg)));
4385 static void gen_VZEROUPPER(DisasContext *s, X86DecodedInsn *decode)
4389 for (i = 0; i < CPU_NB_REGS; i++) {
4390 int offset = offsetof(CPUX86State, xmm_regs[i].ZMM_X(1));
4391 tcg_gen_gvec_dup_imm(MO_64, offset, 16, 16, 0);
4395 static void gen_WAIT(DisasContext *s, X86DecodedInsn *decode)
4397 if ((s->flags & (HF_MP_MASK | HF_TS_MASK)) == (HF_MP_MASK | HF_TS_MASK)) {
4398 gen_NM_exception(s);
4400 /* needs to be treated as I/O because of ferr_irq */
4401 translator_io_start(&s->base);
4402 gen_helper_fwait(tcg_env);
4406 #ifndef CONFIG_USER_ONLY
4407 static void gen_WRMSR(DisasContext *s, X86DecodedInsn *decode)
4409 gen_update_cc_op(s);
4410 gen_update_eip_cur(s);
4411 gen_helper_wrmsr(tcg_env);
4412 s->base.is_jmp = DISAS_EOB_NEXT;
4415 #define gen_WRMSR gen_unreachable
4418 static void gen_WRxxBASE(DisasContext *s, X86DecodedInsn *decode)
4420 TCGv base = cpu_seg_base[s->modrm & 8 ? R_GS : R_FS];
4422 /* Preserve hflags bits by testing CR4 at runtime. */
4423 gen_helper_cr4_testbit(tcg_env, tcg_constant_i32(CR4_FSGSBASE_MASK));
4424 tcg_gen_mov_tl(base, s->T0);
4427 static void gen_XADD(DisasContext *s, X86DecodedInsn *decode)
4429 MemOp ot = decode->op[1].ot;
4431 decode->cc_dst = tcg_temp_new();
4432 decode->cc_src = s->T1;
4433 decode->cc_op = CC_OP_ADDB + ot;
4435 if (s->prefix & PREFIX_LOCK) {
4436 tcg_gen_atomic_fetch_add_tl(s->T0, s->A0, s->T1, s->mem_index, ot | MO_LE);
4437 tcg_gen_add_tl(decode->cc_dst, s->T0, s->T1);
4439 tcg_gen_add_tl(decode->cc_dst, s->T0, s->T1);
4441 * NOTE: writing memory first is important for MMU exceptions,
4442 * but "new result" wins for XADD AX, AX.
4444 gen_writeback(s, decode, 0, decode->cc_dst);
4446 if (decode->op[0].has_ea || decode->op[2].n != decode->op[0].n) {
4447 gen_writeback(s, decode, 2, s->T0);
4451 static void gen_XCHG(DisasContext *s, X86DecodedInsn *decode)
4453 if (s->prefix & PREFIX_LOCK) {
4454 tcg_gen_atomic_xchg_tl(s->T0, s->A0, s->T1,
4455 s->mem_index, decode->op[0].ot | MO_LE);
4456 /* now store old value into register operand */
4457 gen_op_mov_reg_v(s, decode->op[2].ot, decode->op[2].n, s->T0);
4459 /* move destination value into source operand, source preserved in T1 */
4460 gen_op_mov_reg_v(s, decode->op[2].ot, decode->op[2].n, s->T0);
4461 tcg_gen_mov_tl(s->T0, s->T1);
4465 static void gen_XLAT(DisasContext *s, X86DecodedInsn *decode)
4467 /* AL is already zero-extended into s->T0. */
4468 tcg_gen_add_tl(s->A0, cpu_regs[R_EBX], s->T0);
4469 gen_lea_v_seg(s, s->A0, R_DS, s->override);
4470 gen_op_ld_v(s, MO_8, s->T0, s->A0);
4473 static void gen_XOR(DisasContext *s, X86DecodedInsn *decode)
4475 /* special case XOR reg, reg */
4476 if (decode->op[1].unit == X86_OP_INT &&
4477 decode->op[2].unit == X86_OP_INT &&
4478 decode->op[1].n == decode->op[2].n) {
4479 tcg_gen_movi_tl(s->T0, 0);
4480 decode->cc_op = CC_OP_CLR;
4482 MemOp ot = decode->op[1].ot;
4484 if (s->prefix & PREFIX_LOCK) {
4485 tcg_gen_atomic_xor_fetch_tl(s->T0, s->A0, s->T1,
4486 s->mem_index, ot | MO_LE);
4488 tcg_gen_xor_tl(s->T0, s->T0, s->T1);
4490 prepare_update1_cc(decode, s, CC_OP_LOGICB + ot);
4494 static void gen_XRSTOR(DisasContext *s, X86DecodedInsn *decode)
4496 TCGv_i64 features = tcg_temp_new_i64();
4498 tcg_gen_concat_tl_i64(features, cpu_regs[R_EAX], cpu_regs[R_EDX]);
4499 gen_helper_xrstor(tcg_env, s->A0, features);
4500 if (s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_MPX) {
4502 * XRSTOR is how MPX is enabled, which changes how
4503 * we translate. Thus we need to end the TB.
4505 s->base.is_jmp = DISAS_EOB_NEXT;
4509 static void gen_XSAVE(DisasContext *s, X86DecodedInsn *decode)
4511 TCGv_i64 features = tcg_temp_new_i64();
4513 tcg_gen_concat_tl_i64(features, cpu_regs[R_EAX], cpu_regs[R_EDX]);
4514 gen_helper_xsave(tcg_env, s->A0, features);
4517 static void gen_XSAVEOPT(DisasContext *s, X86DecodedInsn *decode)
4519 TCGv_i64 features = tcg_temp_new_i64();
4521 tcg_gen_concat_tl_i64(features, cpu_regs[R_EAX], cpu_regs[R_EDX]);
4522 gen_helper_xsave(tcg_env, s->A0, features);