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/>.
22 typedef void (*SSEFunc_0_epppti)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
23 TCGv_ptr reg_c, TCGv a0, TCGv_i32 scale);
25 static inline TCGv_i32 tcg_constant8u_i32(uint8_t val)
27 return tcg_constant_i32(val);
30 static void gen_NM_exception(DisasContext *s)
32 gen_exception(s, EXCP07_PREX);
35 static void gen_illegal(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
37 gen_illegal_opcode(s);
40 static void gen_load_ea(DisasContext *s, AddressParts *mem, bool is_vsib)
42 TCGv ea = gen_lea_modrm_1(s, *mem, is_vsib);
43 gen_lea_v_seg(s, s->aflag, ea, mem->def_seg, s->override);
46 static inline int mmx_offset(MemOp ot)
50 return offsetof(MMXReg, MMX_B(0));
52 return offsetof(MMXReg, MMX_W(0));
54 return offsetof(MMXReg, MMX_L(0));
56 return offsetof(MMXReg, MMX_Q(0));
58 g_assert_not_reached();
62 static inline int xmm_offset(MemOp ot)
66 return offsetof(ZMMReg, ZMM_B(0));
68 return offsetof(ZMMReg, ZMM_W(0));
70 return offsetof(ZMMReg, ZMM_L(0));
72 return offsetof(ZMMReg, ZMM_Q(0));
74 return offsetof(ZMMReg, ZMM_X(0));
76 return offsetof(ZMMReg, ZMM_Y(0));
78 g_assert_not_reached();
82 static int vector_reg_offset(X86DecodedOp *op)
84 assert(op->unit == X86_OP_MMX || op->unit == X86_OP_SSE);
86 if (op->unit == X86_OP_MMX) {
87 return op->offset - mmx_offset(op->ot);
89 return op->offset - xmm_offset(op->ot);
93 static int vector_elem_offset(X86DecodedOp *op, MemOp ot, int n)
95 int base_ofs = vector_reg_offset(op);
98 if (op->unit == X86_OP_MMX) {
99 return base_ofs + offsetof(MMXReg, MMX_B(n));
101 return base_ofs + offsetof(ZMMReg, ZMM_B(n));
104 if (op->unit == X86_OP_MMX) {
105 return base_ofs + offsetof(MMXReg, MMX_W(n));
107 return base_ofs + offsetof(ZMMReg, ZMM_W(n));
110 if (op->unit == X86_OP_MMX) {
111 return base_ofs + offsetof(MMXReg, MMX_L(n));
113 return base_ofs + offsetof(ZMMReg, ZMM_L(n));
116 if (op->unit == X86_OP_MMX) {
119 return base_ofs + offsetof(ZMMReg, ZMM_Q(n));
122 assert(op->unit == X86_OP_SSE);
123 return base_ofs + offsetof(ZMMReg, ZMM_X(n));
125 assert(op->unit == X86_OP_SSE);
126 return base_ofs + offsetof(ZMMReg, ZMM_Y(n));
128 g_assert_not_reached();
132 static void compute_mmx_offset(X86DecodedOp *op)
135 op->offset = offsetof(CPUX86State, fpregs[op->n].mmx) + mmx_offset(op->ot);
137 op->offset = offsetof(CPUX86State, mmx_t0) + mmx_offset(op->ot);
141 static void compute_xmm_offset(X86DecodedOp *op)
144 op->offset = ZMM_OFFSET(op->n) + xmm_offset(op->ot);
146 op->offset = offsetof(CPUX86State, xmm_t0) + xmm_offset(op->ot);
150 static void gen_load_sse(DisasContext *s, TCGv temp, MemOp ot, int dest_ofs, bool aligned)
154 gen_op_ld_v(s, MO_8, temp, s->A0);
155 tcg_gen_st8_tl(temp, cpu_env, dest_ofs);
158 gen_op_ld_v(s, MO_16, temp, s->A0);
159 tcg_gen_st16_tl(temp, cpu_env, dest_ofs);
162 gen_op_ld_v(s, MO_32, temp, s->A0);
163 tcg_gen_st32_tl(temp, cpu_env, dest_ofs);
166 gen_ldq_env_A0(s, dest_ofs);
169 gen_ldo_env_A0(s, dest_ofs, aligned);
172 gen_ldy_env_A0(s, dest_ofs, aligned);
175 g_assert_not_reached();
179 static bool sse_needs_alignment(DisasContext *s, X86DecodedInsn *decode, MemOp ot)
181 switch (decode->e.vex_class) {
184 if ((s->prefix & PREFIX_VEX) ||
185 decode->e.vex_special == X86_VEX_SSEUnaligned) {
186 /* MOST legacy SSE instructions require aligned memory operands, but not all. */
198 static void gen_load(DisasContext *s, X86DecodedInsn *decode, int opn, TCGv v)
200 X86DecodedOp *op = &decode->op[opn];
206 tcg_gen_ld32u_tl(v, cpu_env,
207 offsetof(CPUX86State,segs[op->n].selector));
210 tcg_gen_ld_tl(v, cpu_env, offsetof(CPUX86State, cr[op->n]));
213 tcg_gen_ld_tl(v, cpu_env, offsetof(CPUX86State, dr[op->n]));
217 gen_op_ld_v(s, op->ot, v, s->A0);
219 gen_op_mov_v_reg(s, op->ot, v, op->n);
223 tcg_gen_movi_tl(v, decode->immediate);
227 compute_mmx_offset(op);
231 compute_xmm_offset(op);
234 bool aligned = sse_needs_alignment(s, decode, op->ot);
235 gen_load_sse(s, v, op->ot, op->offset, aligned);
240 g_assert_not_reached();
244 static TCGv_ptr op_ptr(X86DecodedInsn *decode, int opn)
246 X86DecodedOp *op = &decode->op[opn];
250 op->v_ptr = tcg_temp_new_ptr();
252 /* The temporary points to the MMXReg or ZMMReg. */
253 tcg_gen_addi_ptr(op->v_ptr, cpu_env, vector_reg_offset(op));
257 #define OP_PTR0 op_ptr(decode, 0)
258 #define OP_PTR1 op_ptr(decode, 1)
259 #define OP_PTR2 op_ptr(decode, 2)
261 static void gen_writeback(DisasContext *s, X86DecodedInsn *decode, int opn, TCGv v)
263 X86DecodedOp *op = &decode->op[opn];
268 /* Note that gen_movl_seg_T0 takes care of interrupt shadow and TF. */
269 gen_movl_seg_T0(s, op->n);
273 gen_op_st_v(s, op->ot, v, s->A0);
275 gen_op_mov_reg_v(s, op->ot, op->n, v);
281 if ((s->prefix & PREFIX_VEX) && op->ot == MO_128) {
282 tcg_gen_gvec_dup_imm(MO_64,
283 offsetof(CPUX86State, xmm_regs[op->n].ZMM_X(1)),
290 g_assert_not_reached();
294 static inline int vector_len(DisasContext *s, X86DecodedInsn *decode)
296 if (decode->e.special == X86_SPECIAL_MMX &&
297 !(s->prefix & (PREFIX_DATA | PREFIX_REPZ | PREFIX_REPNZ))) {
300 return s->vex_l ? 32 : 16;
303 static void gen_store_sse(DisasContext *s, X86DecodedInsn *decode, int src_ofs)
305 MemOp ot = decode->op[0].ot;
306 int vec_len = vector_len(s, decode);
307 bool aligned = sse_needs_alignment(s, decode, ot);
309 if (!decode->op[0].has_ea) {
310 tcg_gen_gvec_mov(MO_64, decode->op[0].offset, src_ofs, vec_len, vec_len);
316 gen_stq_env_A0(s, src_ofs);
319 gen_sto_env_A0(s, src_ofs, aligned);
322 gen_sty_env_A0(s, src_ofs, aligned);
325 g_assert_not_reached();
330 * 00 = v*ps Vps, Hps, Wpd
331 * 66 = v*pd Vpd, Hpd, Wps
332 * f3 = v*ss Vss, Hss, Wps
333 * f2 = v*sd Vsd, Hsd, Wps
335 static inline void gen_unary_fp_sse(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode,
336 SSEFunc_0_epp pd_xmm, SSEFunc_0_epp ps_xmm,
337 SSEFunc_0_epp pd_ymm, SSEFunc_0_epp ps_ymm,
338 SSEFunc_0_eppp sd, SSEFunc_0_eppp ss)
340 if ((s->prefix & (PREFIX_REPZ | PREFIX_REPNZ)) != 0) {
341 SSEFunc_0_eppp fn = s->prefix & PREFIX_REPZ ? ss : sd;
343 gen_illegal_opcode(s);
346 fn(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2);
348 SSEFunc_0_epp ps, pd, fn;
349 ps = s->vex_l ? ps_ymm : ps_xmm;
350 pd = s->vex_l ? pd_ymm : pd_xmm;
351 fn = s->prefix & PREFIX_DATA ? pd : ps;
353 gen_illegal_opcode(s);
356 fn(cpu_env, OP_PTR0, OP_PTR2);
359 #define UNARY_FP_SSE(uname, lname) \
360 static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
362 gen_unary_fp_sse(s, env, decode, \
363 gen_helper_##lname##pd_xmm, \
364 gen_helper_##lname##ps_xmm, \
365 gen_helper_##lname##pd_ymm, \
366 gen_helper_##lname##ps_ymm, \
367 gen_helper_##lname##sd, \
368 gen_helper_##lname##ss); \
370 UNARY_FP_SSE(VSQRT, sqrt)
373 * 00 = v*ps Vps, Hps, Wpd
374 * 66 = v*pd Vpd, Hpd, Wps
375 * f3 = v*ss Vss, Hss, Wps
376 * f2 = v*sd Vsd, Hsd, Wps
378 static inline void gen_fp_sse(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode,
379 SSEFunc_0_eppp pd_xmm, SSEFunc_0_eppp ps_xmm,
380 SSEFunc_0_eppp pd_ymm, SSEFunc_0_eppp ps_ymm,
381 SSEFunc_0_eppp sd, SSEFunc_0_eppp ss)
383 SSEFunc_0_eppp ps, pd, fn;
384 if ((s->prefix & (PREFIX_REPZ | PREFIX_REPNZ)) != 0) {
385 fn = s->prefix & PREFIX_REPZ ? ss : sd;
387 ps = s->vex_l ? ps_ymm : ps_xmm;
388 pd = s->vex_l ? pd_ymm : pd_xmm;
389 fn = s->prefix & PREFIX_DATA ? pd : ps;
392 fn(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2);
394 gen_illegal_opcode(s);
398 #define FP_SSE(uname, lname) \
399 static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
401 gen_fp_sse(s, env, decode, \
402 gen_helper_##lname##pd_xmm, \
403 gen_helper_##lname##ps_xmm, \
404 gen_helper_##lname##pd_ymm, \
405 gen_helper_##lname##ps_ymm, \
406 gen_helper_##lname##sd, \
407 gen_helper_##lname##ss); \
416 #define FP_UNPACK_SSE(uname, lname) \
417 static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
419 /* PS maps to the DQ integer instruction, PD maps to QDQ. */ \
420 gen_fp_sse(s, env, decode, \
421 gen_helper_##lname##qdq_xmm, \
422 gen_helper_##lname##dq_xmm, \
423 gen_helper_##lname##qdq_ymm, \
424 gen_helper_##lname##dq_ymm, \
427 FP_UNPACK_SSE(VUNPCKLPx, punpckl)
428 FP_UNPACK_SSE(VUNPCKHPx, punpckh)
434 static inline void gen_unary_fp32_sse(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode,
435 SSEFunc_0_epp ps_xmm,
436 SSEFunc_0_epp ps_ymm,
439 if ((s->prefix & (PREFIX_DATA | PREFIX_REPNZ)) != 0) {
441 } else if (s->prefix & PREFIX_REPZ) {
445 ss(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2);
447 SSEFunc_0_epp fn = s->vex_l ? ps_ymm : ps_xmm;
451 fn(cpu_env, OP_PTR0, OP_PTR2);
456 gen_illegal_opcode(s);
458 #define UNARY_FP32_SSE(uname, lname) \
459 static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
461 gen_unary_fp32_sse(s, env, decode, \
462 gen_helper_##lname##ps_xmm, \
463 gen_helper_##lname##ps_ymm, \
464 gen_helper_##lname##ss); \
466 UNARY_FP32_SSE(VRSQRT, rsqrt)
467 UNARY_FP32_SSE(VRCP, rcp)
470 * 66 = v*pd Vpd, Hpd, Wpd
471 * f2 = v*ps Vps, Hps, Wps
473 static inline void gen_horizontal_fp_sse(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode,
474 SSEFunc_0_eppp pd_xmm, SSEFunc_0_eppp ps_xmm,
475 SSEFunc_0_eppp pd_ymm, SSEFunc_0_eppp ps_ymm)
477 SSEFunc_0_eppp ps, pd, fn;
478 ps = s->vex_l ? ps_ymm : ps_xmm;
479 pd = s->vex_l ? pd_ymm : pd_xmm;
480 fn = s->prefix & PREFIX_DATA ? pd : ps;
481 fn(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2);
483 #define HORIZONTAL_FP_SSE(uname, lname) \
484 static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
486 gen_horizontal_fp_sse(s, env, decode, \
487 gen_helper_##lname##pd_xmm, gen_helper_##lname##ps_xmm, \
488 gen_helper_##lname##pd_ymm, gen_helper_##lname##ps_ymm); \
490 HORIZONTAL_FP_SSE(VHADD, hadd)
491 HORIZONTAL_FP_SSE(VHSUB, hsub)
492 HORIZONTAL_FP_SSE(VADDSUB, addsub)
494 static inline void gen_ternary_sse(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode,
495 int op3, SSEFunc_0_epppp xmm, SSEFunc_0_epppp ymm)
497 SSEFunc_0_epppp fn = s->vex_l ? ymm : xmm;
498 TCGv_ptr ptr3 = tcg_temp_new_ptr();
500 /* The format of the fourth input is Lx */
501 tcg_gen_addi_ptr(ptr3, cpu_env, ZMM_OFFSET(op3));
502 fn(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2, ptr3);
503 tcg_temp_free_ptr(ptr3);
505 #define TERNARY_SSE(uname, uvname, lname) \
506 static void gen_##uvname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
508 gen_ternary_sse(s, env, decode, (uint8_t)decode->immediate >> 4, \
509 gen_helper_##lname##_xmm, gen_helper_##lname##_ymm); \
511 static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
513 gen_ternary_sse(s, env, decode, 0, \
514 gen_helper_##lname##_xmm, gen_helper_##lname##_ymm); \
516 TERNARY_SSE(BLENDVPS, VBLENDVPS, blendvps)
517 TERNARY_SSE(BLENDVPD, VBLENDVPD, blendvpd)
518 TERNARY_SSE(PBLENDVB, VPBLENDVB, pblendvb)
520 static inline void gen_binary_imm_sse(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode,
521 SSEFunc_0_epppi xmm, SSEFunc_0_epppi ymm)
523 TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
525 xmm(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2, imm);
527 ymm(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2, imm);
531 #define BINARY_IMM_SSE(uname, lname) \
532 static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
534 gen_binary_imm_sse(s, env, decode, \
535 gen_helper_##lname##_xmm, \
536 gen_helper_##lname##_ymm); \
539 BINARY_IMM_SSE(VBLENDPD, blendpd)
540 BINARY_IMM_SSE(VBLENDPS, blendps)
541 BINARY_IMM_SSE(VPBLENDW, pblendw)
542 BINARY_IMM_SSE(VDDPS, dpps)
543 #define gen_helper_dppd_ymm NULL
544 BINARY_IMM_SSE(VDDPD, dppd)
545 BINARY_IMM_SSE(VMPSADBW, mpsadbw)
546 BINARY_IMM_SSE(PCLMULQDQ, pclmulqdq)
549 #define UNARY_INT_GVEC(uname, func, ...) \
550 static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
552 int vec_len = vector_len(s, decode); \
554 func(__VA_ARGS__, decode->op[0].offset, \
555 decode->op[2].offset, vec_len, vec_len); \
557 UNARY_INT_GVEC(PABSB, tcg_gen_gvec_abs, MO_8)
558 UNARY_INT_GVEC(PABSW, tcg_gen_gvec_abs, MO_16)
559 UNARY_INT_GVEC(PABSD, tcg_gen_gvec_abs, MO_32)
560 UNARY_INT_GVEC(VBROADCASTx128, tcg_gen_gvec_dup_mem, MO_128)
561 UNARY_INT_GVEC(VPBROADCASTB, tcg_gen_gvec_dup_mem, MO_8)
562 UNARY_INT_GVEC(VPBROADCASTW, tcg_gen_gvec_dup_mem, MO_16)
563 UNARY_INT_GVEC(VPBROADCASTD, tcg_gen_gvec_dup_mem, MO_32)
564 UNARY_INT_GVEC(VPBROADCASTQ, tcg_gen_gvec_dup_mem, MO_64)
567 #define BINARY_INT_GVEC(uname, func, ...) \
568 static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
570 int vec_len = vector_len(s, decode); \
573 decode->op[0].offset, decode->op[1].offset, \
574 decode->op[2].offset, vec_len, vec_len); \
577 BINARY_INT_GVEC(PADDB, tcg_gen_gvec_add, MO_8)
578 BINARY_INT_GVEC(PADDW, tcg_gen_gvec_add, MO_16)
579 BINARY_INT_GVEC(PADDD, tcg_gen_gvec_add, MO_32)
580 BINARY_INT_GVEC(PADDQ, tcg_gen_gvec_add, MO_64)
581 BINARY_INT_GVEC(PADDSB, tcg_gen_gvec_ssadd, MO_8)
582 BINARY_INT_GVEC(PADDSW, tcg_gen_gvec_ssadd, MO_16)
583 BINARY_INT_GVEC(PADDUSB, tcg_gen_gvec_usadd, MO_8)
584 BINARY_INT_GVEC(PADDUSW, tcg_gen_gvec_usadd, MO_16)
585 BINARY_INT_GVEC(PAND, tcg_gen_gvec_and, MO_64)
586 BINARY_INT_GVEC(PCMPEQB, tcg_gen_gvec_cmp, TCG_COND_EQ, MO_8)
587 BINARY_INT_GVEC(PCMPEQD, tcg_gen_gvec_cmp, TCG_COND_EQ, MO_32)
588 BINARY_INT_GVEC(PCMPEQW, tcg_gen_gvec_cmp, TCG_COND_EQ, MO_16)
589 BINARY_INT_GVEC(PCMPEQQ, tcg_gen_gvec_cmp, TCG_COND_EQ, MO_64)
590 BINARY_INT_GVEC(PCMPGTB, tcg_gen_gvec_cmp, TCG_COND_GT, MO_8)
591 BINARY_INT_GVEC(PCMPGTW, tcg_gen_gvec_cmp, TCG_COND_GT, MO_16)
592 BINARY_INT_GVEC(PCMPGTD, tcg_gen_gvec_cmp, TCG_COND_GT, MO_32)
593 BINARY_INT_GVEC(PCMPGTQ, tcg_gen_gvec_cmp, TCG_COND_GT, MO_64)
594 BINARY_INT_GVEC(PMAXSB, tcg_gen_gvec_smax, MO_8)
595 BINARY_INT_GVEC(PMAXSW, tcg_gen_gvec_smax, MO_16)
596 BINARY_INT_GVEC(PMAXSD, tcg_gen_gvec_smax, MO_32)
597 BINARY_INT_GVEC(PMAXUB, tcg_gen_gvec_umax, MO_8)
598 BINARY_INT_GVEC(PMAXUW, tcg_gen_gvec_umax, MO_16)
599 BINARY_INT_GVEC(PMAXUD, tcg_gen_gvec_umax, MO_32)
600 BINARY_INT_GVEC(PMINSB, tcg_gen_gvec_smin, MO_8)
601 BINARY_INT_GVEC(PMINSW, tcg_gen_gvec_smin, MO_16)
602 BINARY_INT_GVEC(PMINSD, tcg_gen_gvec_smin, MO_32)
603 BINARY_INT_GVEC(PMINUB, tcg_gen_gvec_umin, MO_8)
604 BINARY_INT_GVEC(PMINUW, tcg_gen_gvec_umin, MO_16)
605 BINARY_INT_GVEC(PMINUD, tcg_gen_gvec_umin, MO_32)
606 BINARY_INT_GVEC(PMULLW, tcg_gen_gvec_mul, MO_16)
607 BINARY_INT_GVEC(PMULLD, tcg_gen_gvec_mul, MO_32)
608 BINARY_INT_GVEC(POR, tcg_gen_gvec_or, MO_64)
609 BINARY_INT_GVEC(PSUBB, tcg_gen_gvec_sub, MO_8)
610 BINARY_INT_GVEC(PSUBW, tcg_gen_gvec_sub, MO_16)
611 BINARY_INT_GVEC(PSUBD, tcg_gen_gvec_sub, MO_32)
612 BINARY_INT_GVEC(PSUBQ, tcg_gen_gvec_sub, MO_64)
613 BINARY_INT_GVEC(PSUBSB, tcg_gen_gvec_sssub, MO_8)
614 BINARY_INT_GVEC(PSUBSW, tcg_gen_gvec_sssub, MO_16)
615 BINARY_INT_GVEC(PSUBUSB, tcg_gen_gvec_ussub, MO_8)
616 BINARY_INT_GVEC(PSUBUSW, tcg_gen_gvec_ussub, MO_16)
617 BINARY_INT_GVEC(PXOR, tcg_gen_gvec_xor, MO_64)
621 * 00 = p* Pq, Qq (if mmx not NULL; no VEX)
622 * 66 = vp* Vx, Hx, Wx
624 * These are really the same encoding, because 1) V is the same as P when VEX.V
625 * is not present 2) P and Q are the same as H and W apart from MM/XMM
627 static inline void gen_binary_int_sse(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode,
628 SSEFunc_0_eppp mmx, SSEFunc_0_eppp xmm, SSEFunc_0_eppp ymm)
630 assert(!!mmx == !!(decode->e.special == X86_SPECIAL_MMX));
632 if (mmx && (s->prefix & PREFIX_VEX) && !(s->prefix & PREFIX_DATA)) {
633 /* VEX encoding is not applicable to MMX instructions. */
634 gen_illegal_opcode(s);
637 if (!(s->prefix & PREFIX_DATA)) {
638 mmx(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2);
639 } else if (!s->vex_l) {
640 xmm(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2);
642 ymm(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2);
647 #define BINARY_INT_MMX(uname, lname) \
648 static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
650 gen_binary_int_sse(s, env, decode, \
651 gen_helper_##lname##_mmx, \
652 gen_helper_##lname##_xmm, \
653 gen_helper_##lname##_ymm); \
655 BINARY_INT_MMX(PUNPCKLBW, punpcklbw)
656 BINARY_INT_MMX(PUNPCKLWD, punpcklwd)
657 BINARY_INT_MMX(PUNPCKLDQ, punpckldq)
658 BINARY_INT_MMX(PACKSSWB, packsswb)
659 BINARY_INT_MMX(PACKUSWB, packuswb)
660 BINARY_INT_MMX(PUNPCKHBW, punpckhbw)
661 BINARY_INT_MMX(PUNPCKHWD, punpckhwd)
662 BINARY_INT_MMX(PUNPCKHDQ, punpckhdq)
663 BINARY_INT_MMX(PACKSSDW, packssdw)
665 BINARY_INT_MMX(PAVGB, pavgb)
666 BINARY_INT_MMX(PAVGW, pavgw)
667 BINARY_INT_MMX(PMADDWD, pmaddwd)
668 BINARY_INT_MMX(PMULHUW, pmulhuw)
669 BINARY_INT_MMX(PMULHW, pmulhw)
670 BINARY_INT_MMX(PMULUDQ, pmuludq)
671 BINARY_INT_MMX(PSADBW, psadbw)
673 BINARY_INT_MMX(PSLLW_r, psllw)
674 BINARY_INT_MMX(PSLLD_r, pslld)
675 BINARY_INT_MMX(PSLLQ_r, psllq)
676 BINARY_INT_MMX(PSRLW_r, psrlw)
677 BINARY_INT_MMX(PSRLD_r, psrld)
678 BINARY_INT_MMX(PSRLQ_r, psrlq)
679 BINARY_INT_MMX(PSRAW_r, psraw)
680 BINARY_INT_MMX(PSRAD_r, psrad)
682 BINARY_INT_MMX(PHADDW, phaddw)
683 BINARY_INT_MMX(PHADDSW, phaddsw)
684 BINARY_INT_MMX(PHADDD, phaddd)
685 BINARY_INT_MMX(PHSUBW, phsubw)
686 BINARY_INT_MMX(PHSUBSW, phsubsw)
687 BINARY_INT_MMX(PHSUBD, phsubd)
688 BINARY_INT_MMX(PMADDUBSW, pmaddubsw)
689 BINARY_INT_MMX(PSHUFB, pshufb)
690 BINARY_INT_MMX(PSIGNB, psignb)
691 BINARY_INT_MMX(PSIGNW, psignw)
692 BINARY_INT_MMX(PSIGND, psignd)
693 BINARY_INT_MMX(PMULHRSW, pmulhrsw)
695 /* Instructions with no MMX equivalent. */
696 #define BINARY_INT_SSE(uname, lname) \
697 static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
699 gen_binary_int_sse(s, env, decode, \
701 gen_helper_##lname##_xmm, \
702 gen_helper_##lname##_ymm); \
705 /* Instructions with no MMX equivalent. */
706 BINARY_INT_SSE(PUNPCKLQDQ, punpcklqdq)
707 BINARY_INT_SSE(PUNPCKHQDQ, punpckhqdq)
708 BINARY_INT_SSE(VPACKUSDW, packusdw)
709 BINARY_INT_SSE(VPERMILPS, vpermilps)
710 BINARY_INT_SSE(VPERMILPD, vpermilpd)
711 BINARY_INT_SSE(VMASKMOVPS, vpmaskmovd)
712 BINARY_INT_SSE(VMASKMOVPD, vpmaskmovq)
714 BINARY_INT_SSE(PMULDQ, pmuldq)
716 BINARY_INT_SSE(VAESDEC, aesdec)
717 BINARY_INT_SSE(VAESDECLAST, aesdeclast)
718 BINARY_INT_SSE(VAESENC, aesenc)
719 BINARY_INT_SSE(VAESENCLAST, aesenclast)
721 #define UNARY_CMP_SSE(uname, lname) \
722 static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
725 gen_helper_##lname##_xmm(cpu_env, OP_PTR1, OP_PTR2); \
727 gen_helper_##lname##_ymm(cpu_env, OP_PTR1, OP_PTR2); \
729 set_cc_op(s, CC_OP_EFLAGS); \
731 UNARY_CMP_SSE(VPTEST, ptest)
732 UNARY_CMP_SSE(VTESTPS, vtestps)
733 UNARY_CMP_SSE(VTESTPD, vtestpd)
735 static inline void gen_unary_int_sse(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode,
736 SSEFunc_0_epp xmm, SSEFunc_0_epp ymm)
739 xmm(cpu_env, OP_PTR0, OP_PTR2);
741 ymm(cpu_env, OP_PTR0, OP_PTR2);
745 #define UNARY_INT_SSE(uname, lname) \
746 static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
748 gen_unary_int_sse(s, env, decode, \
749 gen_helper_##lname##_xmm, \
750 gen_helper_##lname##_ymm); \
753 UNARY_INT_SSE(VPMOVSXBW, pmovsxbw)
754 UNARY_INT_SSE(VPMOVSXBD, pmovsxbd)
755 UNARY_INT_SSE(VPMOVSXBQ, pmovsxbq)
756 UNARY_INT_SSE(VPMOVSXWD, pmovsxwd)
757 UNARY_INT_SSE(VPMOVSXWQ, pmovsxwq)
758 UNARY_INT_SSE(VPMOVSXDQ, pmovsxdq)
760 UNARY_INT_SSE(VPMOVZXBW, pmovzxbw)
761 UNARY_INT_SSE(VPMOVZXBD, pmovzxbd)
762 UNARY_INT_SSE(VPMOVZXBQ, pmovzxbq)
763 UNARY_INT_SSE(VPMOVZXWD, pmovzxwd)
764 UNARY_INT_SSE(VPMOVZXWQ, pmovzxwq)
765 UNARY_INT_SSE(VPMOVZXDQ, pmovzxdq)
767 UNARY_INT_SSE(VMOVSLDUP, pmovsldup)
768 UNARY_INT_SSE(VMOVSHDUP, pmovshdup)
769 UNARY_INT_SSE(VMOVDDUP, pmovdldup)
771 UNARY_INT_SSE(VCVTDQ2PD, cvtdq2pd)
772 UNARY_INT_SSE(VCVTPD2DQ, cvtpd2dq)
773 UNARY_INT_SSE(VCVTTPD2DQ, cvttpd2dq)
774 UNARY_INT_SSE(VCVTDQ2PS, cvtdq2ps)
775 UNARY_INT_SSE(VCVTPS2DQ, cvtps2dq)
776 UNARY_INT_SSE(VCVTTPS2DQ, cvttps2dq)
779 static inline void gen_unary_imm_sse(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode,
780 SSEFunc_0_ppi xmm, SSEFunc_0_ppi ymm)
782 TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
784 xmm(OP_PTR0, OP_PTR1, imm);
786 ymm(OP_PTR0, OP_PTR1, imm);
790 #define UNARY_IMM_SSE(uname, lname) \
791 static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
793 gen_unary_imm_sse(s, env, decode, \
794 gen_helper_##lname##_xmm, \
795 gen_helper_##lname##_ymm); \
798 UNARY_IMM_SSE(PSHUFD, pshufd)
799 UNARY_IMM_SSE(PSHUFHW, pshufhw)
800 UNARY_IMM_SSE(PSHUFLW, pshuflw)
801 #define gen_helper_vpermq_xmm NULL
802 UNARY_IMM_SSE(VPERMQ, vpermq)
803 UNARY_IMM_SSE(VPERMILPS_i, vpermilps_imm)
804 UNARY_IMM_SSE(VPERMILPD_i, vpermilpd_imm)
806 static inline void gen_unary_imm_fp_sse(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode,
807 SSEFunc_0_eppi xmm, SSEFunc_0_eppi ymm)
809 TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
811 xmm(cpu_env, OP_PTR0, OP_PTR1, imm);
813 ymm(cpu_env, OP_PTR0, OP_PTR1, imm);
817 #define UNARY_IMM_FP_SSE(uname, lname) \
818 static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
820 gen_unary_imm_fp_sse(s, env, decode, \
821 gen_helper_##lname##_xmm, \
822 gen_helper_##lname##_ymm); \
825 UNARY_IMM_FP_SSE(VROUNDPS, roundps)
826 UNARY_IMM_FP_SSE(VROUNDPD, roundpd)
828 static inline void gen_vexw_avx(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode,
829 SSEFunc_0_eppp d_xmm, SSEFunc_0_eppp q_xmm,
830 SSEFunc_0_eppp d_ymm, SSEFunc_0_eppp q_ymm)
832 SSEFunc_0_eppp d = s->vex_l ? d_ymm : d_xmm;
833 SSEFunc_0_eppp q = s->vex_l ? q_ymm : q_xmm;
834 SSEFunc_0_eppp fn = s->vex_w ? q : d;
835 fn(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2);
838 /* VEX.W affects whether to operate on 32- or 64-bit elements. */
839 #define VEXW_AVX(uname, lname) \
840 static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
842 gen_vexw_avx(s, env, decode, \
843 gen_helper_##lname##d_xmm, gen_helper_##lname##q_xmm, \
844 gen_helper_##lname##d_ymm, gen_helper_##lname##q_ymm); \
846 VEXW_AVX(VPSLLV, vpsllv)
847 VEXW_AVX(VPSRLV, vpsrlv)
848 VEXW_AVX(VPSRAV, vpsrav)
849 VEXW_AVX(VPMASKMOV, vpmaskmov)
851 /* Same as above, but with extra arguments to the helper. */
852 static inline void gen_vsib_avx(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode,
853 SSEFunc_0_epppti d_xmm, SSEFunc_0_epppti q_xmm,
854 SSEFunc_0_epppti d_ymm, SSEFunc_0_epppti q_ymm)
856 SSEFunc_0_epppti d = s->vex_l ? d_ymm : d_xmm;
857 SSEFunc_0_epppti q = s->vex_l ? q_ymm : q_xmm;
858 SSEFunc_0_epppti fn = s->vex_w ? q : d;
859 TCGv_i32 scale = tcg_constant_i32(decode->mem.scale);
860 TCGv_ptr index = tcg_temp_new_ptr();
862 /* Pass third input as (index, base, scale) */
863 tcg_gen_addi_ptr(index, cpu_env, ZMM_OFFSET(decode->mem.index));
864 fn(cpu_env, OP_PTR0, OP_PTR1, index, s->A0, scale);
867 * There are two output operands, so zero OP1's high 128 bits
868 * in the VEX.128 case.
871 int ymmh_ofs = vector_elem_offset(&decode->op[1], MO_128, 1);
872 tcg_gen_gvec_dup_imm(MO_64, ymmh_ofs, 16, 16, 0);
874 tcg_temp_free_ptr(index);
876 #define VSIB_AVX(uname, lname) \
877 static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
879 gen_vsib_avx(s, env, decode, \
880 gen_helper_##lname##d_xmm, gen_helper_##lname##q_xmm, \
881 gen_helper_##lname##d_ymm, gen_helper_##lname##q_ymm); \
883 VSIB_AVX(VPGATHERD, vpgatherd)
884 VSIB_AVX(VPGATHERQ, vpgatherq)
886 static void gen_ADCOX(DisasContext *s, CPUX86State *env, MemOp ot, int cc_op)
888 TCGv carry_in = NULL;
889 TCGv carry_out = (cc_op == CC_OP_ADCX ? cpu_cc_dst : cpu_cc_src2);
892 if (cc_op == s->cc_op || s->cc_op == CC_OP_ADCOX) {
893 /* Re-use the carry-out from a previous round. */
894 carry_in = carry_out;
896 } else if (s->cc_op == CC_OP_ADCX || s->cc_op == CC_OP_ADOX) {
897 /* Merge with the carry-out from the opposite instruction. */
901 /* If we don't have a carry-in, get it out of EFLAGS. */
903 if (s->cc_op != CC_OP_ADCX && s->cc_op != CC_OP_ADOX) {
904 gen_compute_eflags(s);
907 tcg_gen_extract_tl(carry_in, cpu_cc_src,
908 ctz32(cc_op == CC_OP_ADCX ? CC_C : CC_O), 1);
914 /* If TL is 64-bit just do everything in 64-bit arithmetic. */
915 tcg_gen_add_i64(s->T0, s->T0, s->T1);
916 tcg_gen_add_i64(s->T0, s->T0, carry_in);
917 tcg_gen_shri_i64(carry_out, s->T0, 32);
921 zero = tcg_constant_tl(0);
922 tcg_gen_add2_tl(s->T0, carry_out, s->T0, zero, carry_in, zero);
923 tcg_gen_add2_tl(s->T0, carry_out, s->T0, carry_out, s->T1, zero);
929 static void gen_ADCX(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
931 gen_ADCOX(s, env, decode->op[0].ot, CC_OP_ADCX);
934 static void gen_ADOX(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
936 gen_ADCOX(s, env, decode->op[0].ot, CC_OP_ADOX);
939 static void gen_ANDN(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
941 MemOp ot = decode->op[0].ot;
943 tcg_gen_andc_tl(s->T0, s->T1, s->T0);
944 gen_op_update1_cc(s);
945 set_cc_op(s, CC_OP_LOGICB + ot);
948 static void gen_BEXTR(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
950 MemOp ot = decode->op[0].ot;
954 * Extract START, and shift the operand.
955 * Shifts larger than operand size get zeros.
957 tcg_gen_ext8u_tl(s->A0, s->T1);
958 tcg_gen_shr_tl(s->T0, s->T0, s->A0);
960 bound = tcg_constant_tl(ot == MO_64 ? 63 : 31);
961 zero = tcg_constant_tl(0);
962 tcg_gen_movcond_tl(TCG_COND_LEU, s->T0, s->A0, bound, s->T0, zero);
965 * Extract the LEN into a mask. Lengths larger than
966 * operand size get all ones.
968 tcg_gen_extract_tl(s->A0, s->T1, 8, 8);
969 tcg_gen_movcond_tl(TCG_COND_LEU, s->A0, s->A0, bound, s->A0, bound);
971 tcg_gen_movi_tl(s->T1, 1);
972 tcg_gen_shl_tl(s->T1, s->T1, s->A0);
973 tcg_gen_subi_tl(s->T1, s->T1, 1);
974 tcg_gen_and_tl(s->T0, s->T0, s->T1);
976 gen_op_update1_cc(s);
977 set_cc_op(s, CC_OP_LOGICB + ot);
980 static void gen_BLSI(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
982 MemOp ot = decode->op[0].ot;
984 tcg_gen_neg_tl(s->T1, s->T0);
985 tcg_gen_and_tl(s->T0, s->T0, s->T1);
986 tcg_gen_mov_tl(cpu_cc_dst, s->T0);
987 set_cc_op(s, CC_OP_BMILGB + ot);
990 static void gen_BLSMSK(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
992 MemOp ot = decode->op[0].ot;
994 tcg_gen_subi_tl(s->T1, s->T0, 1);
995 tcg_gen_xor_tl(s->T0, s->T0, s->T1);
996 tcg_gen_mov_tl(cpu_cc_dst, s->T0);
997 set_cc_op(s, CC_OP_BMILGB + ot);
1000 static void gen_BLSR(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1002 MemOp ot = decode->op[0].ot;
1004 tcg_gen_subi_tl(s->T1, s->T0, 1);
1005 tcg_gen_and_tl(s->T0, s->T0, s->T1);
1006 tcg_gen_mov_tl(cpu_cc_dst, s->T0);
1007 set_cc_op(s, CC_OP_BMILGB + ot);
1010 static void gen_BZHI(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1012 MemOp ot = decode->op[0].ot;
1015 tcg_gen_ext8u_tl(s->T1, cpu_regs[s->vex_v]);
1016 bound = tcg_constant_tl(ot == MO_64 ? 63 : 31);
1019 * Note that since we're using BMILG (in order to get O
1020 * cleared) we need to store the inverse into C.
1022 tcg_gen_setcond_tl(TCG_COND_LT, cpu_cc_src, s->T1, bound);
1023 tcg_gen_movcond_tl(TCG_COND_GT, s->T1, s->T1, bound, bound, s->T1);
1025 tcg_gen_movi_tl(s->A0, -1);
1026 tcg_gen_shl_tl(s->A0, s->A0, s->T1);
1027 tcg_gen_andc_tl(s->T0, s->T0, s->A0);
1029 gen_op_update1_cc(s);
1030 set_cc_op(s, CC_OP_BMILGB + ot);
1033 static void gen_CRC32(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1035 MemOp ot = decode->op[2].ot;
1037 tcg_gen_trunc_tl_i32(s->tmp2_i32, s->T0);
1038 gen_helper_crc32(s->T0, s->tmp2_i32, s->T1, tcg_constant_i32(8 << ot));
1041 static void gen_CVTPI2Px(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1043 gen_helper_enter_mmx(cpu_env);
1044 if (s->prefix & PREFIX_DATA) {
1045 gen_helper_cvtpi2pd(cpu_env, OP_PTR0, OP_PTR2);
1047 gen_helper_cvtpi2ps(cpu_env, OP_PTR0, OP_PTR2);
1051 static void gen_CVTPx2PI(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1053 gen_helper_enter_mmx(cpu_env);
1054 if (s->prefix & PREFIX_DATA) {
1055 gen_helper_cvtpd2pi(cpu_env, OP_PTR0, OP_PTR2);
1057 gen_helper_cvtps2pi(cpu_env, OP_PTR0, OP_PTR2);
1061 static void gen_CVTTPx2PI(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1063 gen_helper_enter_mmx(cpu_env);
1064 if (s->prefix & PREFIX_DATA) {
1065 gen_helper_cvttpd2pi(cpu_env, OP_PTR0, OP_PTR2);
1067 gen_helper_cvttps2pi(cpu_env, OP_PTR0, OP_PTR2);
1071 static void gen_EMMS(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1073 gen_helper_emms(cpu_env);
1076 static void gen_EXTRQ_i(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1078 TCGv_i32 length = tcg_constant_i32(decode->immediate & 63);
1079 TCGv_i32 index = tcg_constant_i32((decode->immediate >> 8) & 63);
1081 gen_helper_extrq_i(cpu_env, OP_PTR0, index, length);
1084 static void gen_EXTRQ_r(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1086 gen_helper_extrq_r(cpu_env, OP_PTR0, OP_PTR2);
1089 static void gen_INSERTQ_i(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1091 TCGv_i32 length = tcg_constant_i32(decode->immediate & 63);
1092 TCGv_i32 index = tcg_constant_i32((decode->immediate >> 8) & 63);
1094 gen_helper_insertq_i(cpu_env, OP_PTR0, OP_PTR1, index, length);
1097 static void gen_INSERTQ_r(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1099 gen_helper_insertq_r(cpu_env, OP_PTR0, OP_PTR2);
1102 static void gen_MASKMOV(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1104 tcg_gen_mov_tl(s->A0, cpu_regs[R_EDI]);
1105 gen_extu(s->aflag, s->A0);
1106 gen_add_A0_ds_seg(s);
1108 if (s->prefix & PREFIX_DATA) {
1109 gen_helper_maskmov_xmm(cpu_env, OP_PTR1, OP_PTR2, s->A0);
1111 gen_helper_maskmov_mmx(cpu_env, OP_PTR1, OP_PTR2, s->A0);
1115 static void gen_MOVBE(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1117 MemOp ot = decode->op[0].ot;
1119 /* M operand type does not load/store */
1120 if (decode->e.op0 == X86_TYPE_M) {
1121 tcg_gen_qemu_st_tl(s->T0, s->A0, s->mem_index, ot | MO_BE);
1123 tcg_gen_qemu_ld_tl(s->T0, s->A0, s->mem_index, ot | MO_BE);
1127 static void gen_MOVD_from(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1129 MemOp ot = decode->op[2].ot;
1133 #ifdef TARGET_X86_64
1134 tcg_gen_ld32u_tl(s->T0, cpu_env, decode->op[2].offset);
1138 tcg_gen_ld_tl(s->T0, cpu_env, decode->op[2].offset);
1145 static void gen_MOVD_to(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1147 MemOp ot = decode->op[2].ot;
1148 int vec_len = vector_len(s, decode);
1149 int lo_ofs = vector_elem_offset(&decode->op[0], ot, 0);
1151 tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
1155 #ifdef TARGET_X86_64
1156 tcg_gen_st32_tl(s->T1, cpu_env, lo_ofs);
1160 tcg_gen_st_tl(s->T1, cpu_env, lo_ofs);
1163 g_assert_not_reached();
1167 static void gen_MOVDQ(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1169 gen_store_sse(s, decode, decode->op[2].offset);
1172 static void gen_MOVMSK(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1174 typeof(gen_helper_movmskps_ymm) *ps, *pd, *fn;
1175 ps = s->vex_l ? gen_helper_movmskps_ymm : gen_helper_movmskps_xmm;
1176 pd = s->vex_l ? gen_helper_movmskpd_ymm : gen_helper_movmskpd_xmm;
1177 fn = s->prefix & PREFIX_DATA ? pd : ps;
1178 fn(s->tmp2_i32, cpu_env, OP_PTR2);
1179 tcg_gen_extu_i32_tl(s->T0, s->tmp2_i32);
1182 static void gen_MOVQ(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1184 int vec_len = vector_len(s, decode);
1185 int lo_ofs = vector_elem_offset(&decode->op[0], MO_64, 0);
1187 tcg_gen_ld_i64(s->tmp1_i64, cpu_env, decode->op[2].offset);
1188 if (decode->op[0].has_ea) {
1189 tcg_gen_qemu_st_i64(s->tmp1_i64, s->A0, s->mem_index, MO_LEUQ);
1192 * tcg_gen_gvec_dup_i64(MO_64, op0.offset, 8, vec_len, s->tmp1_64) would
1193 * seem to work, but it does not on big-endian platforms; the cleared parts
1194 * are always at higher addresses, but cross-endian emulation inverts the
1195 * byte order so that the cleared parts need to be at *lower* addresses.
1196 * Because oprsz is 8, we see this here even for SSE; but more in general,
1197 * it disqualifies using oprsz < maxsz to emulate VEX128.
1199 tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
1200 tcg_gen_st_i64(s->tmp1_i64, cpu_env, lo_ofs);
1204 static void gen_MOVq_dq(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1206 gen_helper_enter_mmx(cpu_env);
1207 /* Otherwise the same as any other movq. */
1208 return gen_MOVQ(s, env, decode);
1211 static void gen_MULX(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1213 MemOp ot = decode->op[0].ot;
1215 /* low part of result in VEX.vvvv, high in MODRM */
1218 tcg_gen_trunc_tl_i32(s->tmp2_i32, s->T0);
1219 tcg_gen_trunc_tl_i32(s->tmp3_i32, s->T1);
1220 tcg_gen_mulu2_i32(s->tmp2_i32, s->tmp3_i32,
1221 s->tmp2_i32, s->tmp3_i32);
1222 tcg_gen_extu_i32_tl(cpu_regs[s->vex_v], s->tmp2_i32);
1223 tcg_gen_extu_i32_tl(s->T0, s->tmp3_i32);
1225 #ifdef TARGET_X86_64
1227 tcg_gen_mulu2_i64(cpu_regs[s->vex_v], s->T0, s->T0, s->T1);
1234 static void gen_PALIGNR(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1236 TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
1237 if (!(s->prefix & PREFIX_DATA)) {
1238 gen_helper_palignr_mmx(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2, imm);
1239 } else if (!s->vex_l) {
1240 gen_helper_palignr_xmm(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2, imm);
1242 gen_helper_palignr_ymm(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2, imm);
1246 static void gen_PANDN(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1248 int vec_len = vector_len(s, decode);
1250 /* Careful, operand order is reversed! */
1251 tcg_gen_gvec_andc(MO_64,
1252 decode->op[0].offset, decode->op[2].offset,
1253 decode->op[1].offset, vec_len, vec_len);
1256 static void gen_PCMPESTRI(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1258 TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
1259 gen_helper_pcmpestri_xmm(cpu_env, OP_PTR1, OP_PTR2, imm);
1260 set_cc_op(s, CC_OP_EFLAGS);
1263 static void gen_PCMPESTRM(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1265 TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
1266 gen_helper_pcmpestrm_xmm(cpu_env, OP_PTR1, OP_PTR2, imm);
1267 set_cc_op(s, CC_OP_EFLAGS);
1268 if ((s->prefix & PREFIX_VEX) && !s->vex_l) {
1269 tcg_gen_gvec_dup_imm(MO_64, offsetof(CPUX86State, xmm_regs[0].ZMM_X(1)),
1274 static void gen_PCMPISTRI(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1276 TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
1277 gen_helper_pcmpistri_xmm(cpu_env, OP_PTR1, OP_PTR2, imm);
1278 set_cc_op(s, CC_OP_EFLAGS);
1281 static void gen_PCMPISTRM(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1283 TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
1284 gen_helper_pcmpistrm_xmm(cpu_env, OP_PTR1, OP_PTR2, imm);
1285 set_cc_op(s, CC_OP_EFLAGS);
1286 if ((s->prefix & PREFIX_VEX) && !s->vex_l) {
1287 tcg_gen_gvec_dup_imm(MO_64, offsetof(CPUX86State, xmm_regs[0].ZMM_X(1)),
1292 static void gen_PDEP(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1294 MemOp ot = decode->op[1].ot;
1296 tcg_gen_ext32u_tl(s->T0, s->T0);
1298 gen_helper_pdep(s->T0, s->T0, s->T1);
1301 static void gen_PEXT(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1303 MemOp ot = decode->op[1].ot;
1305 tcg_gen_ext32u_tl(s->T0, s->T0);
1307 gen_helper_pext(s->T0, s->T0, s->T1);
1310 static inline void gen_pextr(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode, MemOp ot)
1312 int vec_len = vector_len(s, decode);
1313 int mask = (vec_len >> ot) - 1;
1314 int val = decode->immediate & mask;
1318 tcg_gen_ld8u_tl(s->T0, cpu_env, vector_elem_offset(&decode->op[1], ot, val));
1321 tcg_gen_ld16u_tl(s->T0, cpu_env, vector_elem_offset(&decode->op[1], ot, val));
1324 #ifdef TARGET_X86_64
1325 tcg_gen_ld32u_tl(s->T0, cpu_env, vector_elem_offset(&decode->op[1], ot, val));
1329 tcg_gen_ld_tl(s->T0, cpu_env, vector_elem_offset(&decode->op[1], ot, val));
1336 static void gen_PEXTRB(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1338 gen_pextr(s, env, decode, MO_8);
1341 static void gen_PEXTRW(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1343 gen_pextr(s, env, decode, MO_16);
1346 static void gen_PEXTR(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1348 MemOp ot = decode->op[0].ot;
1349 gen_pextr(s, env, decode, ot);
1352 static inline void gen_pinsr(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode, MemOp ot)
1354 int vec_len = vector_len(s, decode);
1355 int mask = (vec_len >> ot) - 1;
1356 int val = decode->immediate & mask;
1358 if (decode->op[1].offset != decode->op[0].offset) {
1359 assert(vec_len == 16);
1360 gen_store_sse(s, decode, decode->op[1].offset);
1365 tcg_gen_st8_tl(s->T1, cpu_env, vector_elem_offset(&decode->op[0], ot, val));
1368 tcg_gen_st16_tl(s->T1, cpu_env, vector_elem_offset(&decode->op[0], ot, val));
1371 #ifdef TARGET_X86_64
1372 tcg_gen_st32_tl(s->T1, cpu_env, vector_elem_offset(&decode->op[0], ot, val));
1376 tcg_gen_st_tl(s->T1, cpu_env, vector_elem_offset(&decode->op[0], ot, val));
1383 static void gen_PINSRB(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1385 gen_pinsr(s, env, decode, MO_8);
1388 static void gen_PINSRW(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1390 gen_pinsr(s, env, decode, MO_16);
1393 static void gen_PINSR(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1395 gen_pinsr(s, env, decode, decode->op[2].ot);
1398 static void gen_pmovmskb_i64(TCGv_i64 d, TCGv_i64 s)
1400 TCGv_i64 t = tcg_temp_new_i64();
1402 tcg_gen_andi_i64(d, s, 0x8080808080808080ull);
1405 * After each shift+or pair:
1406 * 0: a.......b.......c.......d.......e.......f.......g.......h.......
1407 * 7: ab......bc......cd......de......ef......fg......gh......h.......
1408 * 14: abcd....bcde....cdef....defg....efgh....fgh.....gh......h.......
1409 * 28: abcdefghbcdefgh.cdefgh..defgh...efgh....fgh.....gh......h.......
1410 * The result is left in the high bits of the word.
1412 tcg_gen_shli_i64(t, d, 7);
1413 tcg_gen_or_i64(d, d, t);
1414 tcg_gen_shli_i64(t, d, 14);
1415 tcg_gen_or_i64(d, d, t);
1416 tcg_gen_shli_i64(t, d, 28);
1417 tcg_gen_or_i64(d, d, t);
1420 static void gen_pmovmskb_vec(unsigned vece, TCGv_vec d, TCGv_vec s)
1422 TCGv_vec t = tcg_temp_new_vec_matching(d);
1423 TCGv_vec m = tcg_constant_vec_matching(d, MO_8, 0x80);
1426 tcg_gen_and_vec(vece, d, s, m);
1427 tcg_gen_shli_vec(vece, t, d, 7);
1428 tcg_gen_or_vec(vece, d, d, t);
1429 tcg_gen_shli_vec(vece, t, d, 14);
1430 tcg_gen_or_vec(vece, d, d, t);
1431 tcg_gen_shli_vec(vece, t, d, 28);
1432 tcg_gen_or_vec(vece, d, d, t);
1435 #ifdef TARGET_X86_64
1436 #define TCG_TARGET_HAS_extract2_tl TCG_TARGET_HAS_extract2_i64
1438 #define TCG_TARGET_HAS_extract2_tl TCG_TARGET_HAS_extract2_i32
1441 static void gen_PMOVMSKB(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1443 static const TCGOpcode vecop_list[] = { INDEX_op_shli_vec, 0 };
1444 static const GVecGen2 g = {
1445 .fni8 = gen_pmovmskb_i64,
1446 .fniv = gen_pmovmskb_vec,
1447 .opt_opc = vecop_list,
1449 .prefer_i64 = TCG_TARGET_REG_BITS == 64
1451 MemOp ot = decode->op[2].ot;
1452 int vec_len = vector_len(s, decode);
1453 TCGv t = tcg_temp_new();
1455 tcg_gen_gvec_2(offsetof(CPUX86State, xmm_t0) + xmm_offset(ot), decode->op[2].offset,
1456 vec_len, vec_len, &g);
1457 tcg_gen_ld8u_tl(s->T0, cpu_env, offsetof(CPUX86State, xmm_t0.ZMM_B(vec_len - 1)));
1458 while (vec_len > 8) {
1460 if (TCG_TARGET_HAS_extract2_tl) {
1462 * Load the next byte of the result into the high byte of T.
1463 * TCG does a similar expansion of deposit to shl+extract2; by
1464 * loading the whole word, the shift left is avoided.
1466 #ifdef TARGET_X86_64
1467 tcg_gen_ld_tl(t, cpu_env, offsetof(CPUX86State, xmm_t0.ZMM_Q((vec_len - 1) / 8)));
1469 tcg_gen_ld_tl(t, cpu_env, offsetof(CPUX86State, xmm_t0.ZMM_L((vec_len - 1) / 4)));
1472 tcg_gen_extract2_tl(s->T0, t, s->T0, TARGET_LONG_BITS - 8);
1475 * The _previous_ value is deposited into bits 8 and higher of t. Because
1476 * those bits are known to be zero after ld8u, this becomes a shift+or
1477 * if deposit is not available.
1479 tcg_gen_ld8u_tl(t, cpu_env, offsetof(CPUX86State, xmm_t0.ZMM_B(vec_len - 1)));
1480 tcg_gen_deposit_tl(s->T0, t, s->T0, 8, TARGET_LONG_BITS - 8);
1486 static void gen_PSHUFW(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1488 TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
1489 gen_helper_pshufw_mmx(OP_PTR0, OP_PTR1, imm);
1492 static void gen_PSRLW_i(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1494 int vec_len = vector_len(s, decode);
1496 if (decode->immediate >= 16) {
1497 tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
1499 tcg_gen_gvec_shri(MO_16,
1500 decode->op[0].offset, decode->op[1].offset,
1501 decode->immediate, vec_len, vec_len);
1505 static void gen_PSLLW_i(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1507 int vec_len = vector_len(s, decode);
1509 if (decode->immediate >= 16) {
1510 tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
1512 tcg_gen_gvec_shli(MO_16,
1513 decode->op[0].offset, decode->op[1].offset,
1514 decode->immediate, vec_len, vec_len);
1518 static void gen_PSRAW_i(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1520 int vec_len = vector_len(s, decode);
1522 if (decode->immediate >= 16) {
1523 decode->immediate = 15;
1525 tcg_gen_gvec_sari(MO_16,
1526 decode->op[0].offset, decode->op[1].offset,
1527 decode->immediate, vec_len, vec_len);
1530 static void gen_PSRLD_i(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1532 int vec_len = vector_len(s, decode);
1534 if (decode->immediate >= 32) {
1535 tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
1537 tcg_gen_gvec_shri(MO_32,
1538 decode->op[0].offset, decode->op[1].offset,
1539 decode->immediate, vec_len, vec_len);
1543 static void gen_PSLLD_i(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1545 int vec_len = vector_len(s, decode);
1547 if (decode->immediate >= 32) {
1548 tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
1550 tcg_gen_gvec_shli(MO_32,
1551 decode->op[0].offset, decode->op[1].offset,
1552 decode->immediate, vec_len, vec_len);
1556 static void gen_PSRAD_i(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1558 int vec_len = vector_len(s, decode);
1560 if (decode->immediate >= 32) {
1561 decode->immediate = 31;
1563 tcg_gen_gvec_sari(MO_32,
1564 decode->op[0].offset, decode->op[1].offset,
1565 decode->immediate, vec_len, vec_len);
1568 static void gen_PSRLQ_i(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1570 int vec_len = vector_len(s, decode);
1572 if (decode->immediate >= 64) {
1573 tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
1575 tcg_gen_gvec_shri(MO_64,
1576 decode->op[0].offset, decode->op[1].offset,
1577 decode->immediate, vec_len, vec_len);
1581 static void gen_PSLLQ_i(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1583 int vec_len = vector_len(s, decode);
1585 if (decode->immediate >= 64) {
1586 tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
1588 tcg_gen_gvec_shli(MO_64,
1589 decode->op[0].offset, decode->op[1].offset,
1590 decode->immediate, vec_len, vec_len);
1594 static TCGv_ptr make_imm8u_xmm_vec(uint8_t imm, int vec_len)
1596 MemOp ot = vec_len == 16 ? MO_128 : MO_256;
1597 TCGv_i32 imm_v = tcg_constant8u_i32(imm);
1598 TCGv_ptr ptr = tcg_temp_new_ptr();
1600 tcg_gen_gvec_dup_imm(MO_64, offsetof(CPUX86State, xmm_t0) + xmm_offset(ot),
1601 vec_len, vec_len, 0);
1603 tcg_gen_addi_ptr(ptr, cpu_env, offsetof(CPUX86State, xmm_t0));
1604 tcg_gen_st_i32(imm_v, cpu_env, offsetof(CPUX86State, xmm_t0.ZMM_L(0)));
1608 static void gen_PSRLDQ_i(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1610 int vec_len = vector_len(s, decode);
1611 TCGv_ptr imm_vec = make_imm8u_xmm_vec(decode->immediate, vec_len);
1614 gen_helper_psrldq_ymm(cpu_env, OP_PTR0, OP_PTR1, imm_vec);
1616 gen_helper_psrldq_xmm(cpu_env, OP_PTR0, OP_PTR1, imm_vec);
1618 tcg_temp_free_ptr(imm_vec);
1621 static void gen_PSLLDQ_i(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1623 int vec_len = vector_len(s, decode);
1624 TCGv_ptr imm_vec = make_imm8u_xmm_vec(decode->immediate, vec_len);
1627 gen_helper_pslldq_ymm(cpu_env, OP_PTR0, OP_PTR1, imm_vec);
1629 gen_helper_pslldq_xmm(cpu_env, OP_PTR0, OP_PTR1, imm_vec);
1631 tcg_temp_free_ptr(imm_vec);
1634 static void gen_RORX(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1636 MemOp ot = decode->op[0].ot;
1637 int b = decode->immediate;
1640 tcg_gen_rotri_tl(s->T0, s->T0, b & 63);
1642 tcg_gen_trunc_tl_i32(s->tmp2_i32, s->T0);
1643 tcg_gen_rotri_i32(s->tmp2_i32, s->tmp2_i32, b & 31);
1644 tcg_gen_extu_i32_tl(s->T0, s->tmp2_i32);
1648 static void gen_SARX(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1650 MemOp ot = decode->op[0].ot;
1653 mask = ot == MO_64 ? 63 : 31;
1654 tcg_gen_andi_tl(s->T1, s->T1, mask);
1656 tcg_gen_ext32s_tl(s->T0, s->T0);
1658 tcg_gen_sar_tl(s->T0, s->T0, s->T1);
1661 static void gen_SHLX(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1663 MemOp ot = decode->op[0].ot;
1666 mask = ot == MO_64 ? 63 : 31;
1667 tcg_gen_andi_tl(s->T1, s->T1, mask);
1668 tcg_gen_shl_tl(s->T0, s->T0, s->T1);
1671 static void gen_SHRX(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1673 MemOp ot = decode->op[0].ot;
1676 mask = ot == MO_64 ? 63 : 31;
1677 tcg_gen_andi_tl(s->T1, s->T1, mask);
1679 tcg_gen_ext32u_tl(s->T0, s->T0);
1681 tcg_gen_shr_tl(s->T0, s->T0, s->T1);
1684 static void gen_VAESKEYGEN(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1686 TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
1688 gen_helper_aeskeygenassist_xmm(cpu_env, OP_PTR0, OP_PTR1, imm);
1691 static void gen_VAESIMC(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1694 gen_helper_aesimc_xmm(cpu_env, OP_PTR0, OP_PTR2);
1698 * 00 = v*ps Vps, Hps, Wpd
1699 * 66 = v*pd Vpd, Hpd, Wps
1700 * f3 = v*ss Vss, Hss, Wps
1701 * f2 = v*sd Vsd, Hsd, Wps
1703 #define SSE_CMP(x) { \
1704 gen_helper_ ## x ## ps ## _xmm, gen_helper_ ## x ## pd ## _xmm, \
1705 gen_helper_ ## x ## ss, gen_helper_ ## x ## sd, \
1706 gen_helper_ ## x ## ps ## _ymm, gen_helper_ ## x ## pd ## _ymm}
1707 static const SSEFunc_0_eppp gen_helper_cmp_funcs[32][6] = {
1746 static void gen_VCMP(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1748 int index = decode->immediate & (s->prefix & PREFIX_VEX ? 31 : 7);
1750 s->prefix & PREFIX_REPZ ? 2 /* ss */ :
1751 s->prefix & PREFIX_REPNZ ? 3 /* sd */ :
1752 !!(s->prefix & PREFIX_DATA) /* pd */ + (s->vex_l << 2);
1754 gen_helper_cmp_funcs[index][b](cpu_env, OP_PTR0, OP_PTR1, OP_PTR2);
1757 static void gen_VCOMI(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1760 fn = s->prefix & PREFIX_DATA ? gen_helper_comisd : gen_helper_comiss;
1761 fn(cpu_env, OP_PTR1, OP_PTR2);
1762 set_cc_op(s, CC_OP_EFLAGS);
1765 static void gen_VCVTfp2fp(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1767 gen_unary_fp_sse(s, env, decode,
1768 gen_helper_cvtpd2ps_xmm, gen_helper_cvtps2pd_xmm,
1769 gen_helper_cvtpd2ps_ymm, gen_helper_cvtps2pd_ymm,
1770 gen_helper_cvtsd2ss, gen_helper_cvtss2sd);
1773 static void gen_VCVTSI2Sx(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1775 int vec_len = vector_len(s, decode);
1778 tcg_gen_gvec_mov(MO_64, decode->op[0].offset, decode->op[1].offset, vec_len, vec_len);
1780 #ifdef TARGET_X86_64
1781 MemOp ot = decode->op[2].ot;
1783 if (s->prefix & PREFIX_REPNZ) {
1784 gen_helper_cvtsq2sd(cpu_env, OP_PTR0, s->T1);
1786 gen_helper_cvtsq2ss(cpu_env, OP_PTR0, s->T1);
1791 tcg_gen_trunc_tl_i32(in, s->T1);
1796 if (s->prefix & PREFIX_REPNZ) {
1797 gen_helper_cvtsi2sd(cpu_env, OP_PTR0, in);
1799 gen_helper_cvtsi2ss(cpu_env, OP_PTR0, in);
1803 static inline void gen_VCVTtSx2SI(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode,
1804 SSEFunc_i_ep ss2si, SSEFunc_l_ep ss2sq,
1805 SSEFunc_i_ep sd2si, SSEFunc_l_ep sd2sq)
1809 #ifdef TARGET_X86_64
1810 MemOp ot = decode->op[0].ot;
1812 if (s->prefix & PREFIX_REPNZ) {
1813 sd2sq(s->T0, cpu_env, OP_PTR2);
1815 ss2sq(s->T0, cpu_env, OP_PTR2);
1824 if (s->prefix & PREFIX_REPNZ) {
1825 sd2si(out, cpu_env, OP_PTR2);
1827 ss2si(out, cpu_env, OP_PTR2);
1829 #ifdef TARGET_X86_64
1830 tcg_gen_extu_i32_tl(s->T0, out);
1834 #ifndef TARGET_X86_64
1835 #define gen_helper_cvtss2sq NULL
1836 #define gen_helper_cvtsd2sq NULL
1837 #define gen_helper_cvttss2sq NULL
1838 #define gen_helper_cvttsd2sq NULL
1841 static void gen_VCVTSx2SI(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1843 gen_VCVTtSx2SI(s, env, decode,
1844 gen_helper_cvtss2si, gen_helper_cvtss2sq,
1845 gen_helper_cvtsd2si, gen_helper_cvtsd2sq);
1848 static void gen_VCVTTSx2SI(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1850 gen_VCVTtSx2SI(s, env, decode,
1851 gen_helper_cvttss2si, gen_helper_cvttss2sq,
1852 gen_helper_cvttsd2si, gen_helper_cvttsd2sq);
1855 static void gen_VEXTRACTx128(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1857 int mask = decode->immediate & 1;
1858 int src_ofs = vector_elem_offset(&decode->op[1], MO_128, mask);
1859 if (decode->op[0].has_ea) {
1860 /* VEX-only instruction, no alignment requirements. */
1861 gen_sto_env_A0(s, src_ofs, false);
1863 tcg_gen_gvec_mov(MO_64, decode->op[0].offset, src_ofs, 16, 16);
1867 static void gen_VEXTRACTPS(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1869 gen_pextr(s, env, decode, MO_32);
1872 static void gen_vinsertps(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1874 int val = decode->immediate;
1875 int dest_word = (val >> 4) & 3;
1876 int new_mask = (val & 15) | (1 << dest_word);
1881 if (new_mask == 15) {
1882 /* All zeroes except possibly for the inserted element */
1883 tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
1884 } else if (decode->op[1].offset != decode->op[0].offset) {
1885 gen_store_sse(s, decode, decode->op[1].offset);
1888 if (new_mask != (val & 15)) {
1889 tcg_gen_st_i32(s->tmp2_i32, cpu_env,
1890 vector_elem_offset(&decode->op[0], MO_32, dest_word));
1893 if (new_mask != 15) {
1894 TCGv_i32 zero = tcg_constant_i32(0); /* float32_zero */
1896 for (i = 0; i < 4; i++) {
1897 if ((val >> i) & 1) {
1898 tcg_gen_st_i32(zero, cpu_env,
1899 vector_elem_offset(&decode->op[0], MO_32, i));
1905 static void gen_VINSERTPS_r(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1907 int val = decode->immediate;
1908 tcg_gen_ld_i32(s->tmp2_i32, cpu_env,
1909 vector_elem_offset(&decode->op[2], MO_32, (val >> 6) & 3));
1910 gen_vinsertps(s, env, decode);
1913 static void gen_VINSERTPS_m(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1915 tcg_gen_qemu_ld_i32(s->tmp2_i32, s->A0, s->mem_index, MO_LEUL);
1916 gen_vinsertps(s, env, decode);
1919 static void gen_VINSERTx128(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1921 int mask = decode->immediate & 1;
1922 tcg_gen_gvec_mov(MO_64,
1923 decode->op[0].offset + offsetof(YMMReg, YMM_X(mask)),
1924 decode->op[2].offset + offsetof(YMMReg, YMM_X(0)), 16, 16);
1925 tcg_gen_gvec_mov(MO_64,
1926 decode->op[0].offset + offsetof(YMMReg, YMM_X(!mask)),
1927 decode->op[1].offset + offsetof(YMMReg, YMM_X(!mask)), 16, 16);
1930 static inline void gen_maskmov(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode,
1931 SSEFunc_0_eppt xmm, SSEFunc_0_eppt ymm)
1934 xmm(cpu_env, OP_PTR2, OP_PTR1, s->A0);
1936 ymm(cpu_env, OP_PTR2, OP_PTR1, s->A0);
1940 static void gen_VMASKMOVPD_st(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1942 gen_maskmov(s, env, decode, gen_helper_vpmaskmovq_st_xmm, gen_helper_vpmaskmovq_st_ymm);
1945 static void gen_VMASKMOVPS_st(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1947 gen_maskmov(s, env, decode, gen_helper_vpmaskmovd_st_xmm, gen_helper_vpmaskmovd_st_ymm);
1950 static void gen_VMOVHPx_ld(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1952 gen_ldq_env_A0(s, decode->op[0].offset + offsetof(XMMReg, XMM_Q(1)));
1953 if (decode->op[0].offset != decode->op[1].offset) {
1954 tcg_gen_ld_i64(s->tmp1_i64, cpu_env, decode->op[1].offset + offsetof(XMMReg, XMM_Q(0)));
1955 tcg_gen_st_i64(s->tmp1_i64, cpu_env, decode->op[0].offset + offsetof(XMMReg, XMM_Q(0)));
1959 static void gen_VMOVHPx_st(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1961 gen_stq_env_A0(s, decode->op[2].offset + offsetof(XMMReg, XMM_Q(1)));
1964 static void gen_VMOVHPx(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1966 if (decode->op[0].offset != decode->op[2].offset) {
1967 tcg_gen_ld_i64(s->tmp1_i64, cpu_env, decode->op[2].offset + offsetof(XMMReg, XMM_Q(1)));
1968 tcg_gen_st_i64(s->tmp1_i64, cpu_env, decode->op[0].offset + offsetof(XMMReg, XMM_Q(1)));
1970 if (decode->op[0].offset != decode->op[1].offset) {
1971 tcg_gen_ld_i64(s->tmp1_i64, cpu_env, decode->op[1].offset + offsetof(XMMReg, XMM_Q(0)));
1972 tcg_gen_st_i64(s->tmp1_i64, cpu_env, decode->op[0].offset + offsetof(XMMReg, XMM_Q(0)));
1976 static void gen_VMOVHLPS(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1978 tcg_gen_ld_i64(s->tmp1_i64, cpu_env, decode->op[2].offset + offsetof(XMMReg, XMM_Q(1)));
1979 tcg_gen_st_i64(s->tmp1_i64, cpu_env, decode->op[0].offset + offsetof(XMMReg, XMM_Q(0)));
1980 if (decode->op[0].offset != decode->op[1].offset) {
1981 tcg_gen_ld_i64(s->tmp1_i64, cpu_env, decode->op[1].offset + offsetof(XMMReg, XMM_Q(1)));
1982 tcg_gen_st_i64(s->tmp1_i64, cpu_env, decode->op[0].offset + offsetof(XMMReg, XMM_Q(1)));
1986 static void gen_VMOVLHPS(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1988 tcg_gen_ld_i64(s->tmp1_i64, cpu_env, decode->op[2].offset);
1989 tcg_gen_st_i64(s->tmp1_i64, cpu_env, decode->op[0].offset + offsetof(XMMReg, XMM_Q(1)));
1990 if (decode->op[0].offset != decode->op[1].offset) {
1991 tcg_gen_ld_i64(s->tmp1_i64, cpu_env, decode->op[1].offset + offsetof(XMMReg, XMM_Q(0)));
1992 tcg_gen_st_i64(s->tmp1_i64, cpu_env, decode->op[0].offset + offsetof(XMMReg, XMM_Q(0)));
1997 * Note that MOVLPx supports 256-bit operation unlike MOVHLPx, MOVLHPx, MOXHPx.
1998 * Use a gvec move to move everything above the bottom 64 bits.
2001 static void gen_VMOVLPx(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2003 int vec_len = vector_len(s, decode);
2005 tcg_gen_ld_i64(s->tmp1_i64, cpu_env, decode->op[2].offset + offsetof(XMMReg, XMM_Q(0)));
2006 tcg_gen_gvec_mov(MO_64, decode->op[0].offset, decode->op[1].offset, vec_len, vec_len);
2007 tcg_gen_st_i64(s->tmp1_i64, cpu_env, decode->op[0].offset + offsetof(XMMReg, XMM_Q(0)));
2010 static void gen_VMOVLPx_ld(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2012 int vec_len = vector_len(s, decode);
2014 tcg_gen_qemu_ld_i64(s->tmp1_i64, s->A0, s->mem_index, MO_LEUQ);
2015 tcg_gen_gvec_mov(MO_64, decode->op[0].offset, decode->op[1].offset, vec_len, vec_len);
2016 tcg_gen_st_i64(s->tmp1_i64, OP_PTR0, offsetof(ZMMReg, ZMM_Q(0)));
2019 static void gen_VMOVLPx_st(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2021 tcg_gen_ld_i64(s->tmp1_i64, OP_PTR2, offsetof(ZMMReg, ZMM_Q(0)));
2022 tcg_gen_qemu_st_i64(s->tmp1_i64, s->A0, s->mem_index, MO_LEUQ);
2025 static void gen_VMOVSD_ld(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2027 TCGv_i64 zero = tcg_constant_i64(0);
2029 tcg_gen_qemu_ld_i64(s->tmp1_i64, s->A0, s->mem_index, MO_LEUQ);
2030 tcg_gen_st_i64(zero, OP_PTR0, offsetof(ZMMReg, ZMM_Q(1)));
2031 tcg_gen_st_i64(s->tmp1_i64, OP_PTR0, offsetof(ZMMReg, ZMM_Q(0)));
2034 static void gen_VMOVSS(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2036 int vec_len = vector_len(s, decode);
2038 tcg_gen_ld_i32(s->tmp2_i32, OP_PTR2, offsetof(ZMMReg, ZMM_L(0)));
2039 tcg_gen_gvec_mov(MO_64, decode->op[0].offset, decode->op[1].offset, vec_len, vec_len);
2040 tcg_gen_st_i32(s->tmp2_i32, OP_PTR0, offsetof(ZMMReg, ZMM_L(0)));
2043 static void gen_VMOVSS_ld(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2045 int vec_len = vector_len(s, decode);
2047 tcg_gen_qemu_ld_i32(s->tmp2_i32, s->A0, s->mem_index, MO_LEUL);
2048 tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
2049 tcg_gen_st_i32(s->tmp2_i32, OP_PTR0, offsetof(ZMMReg, ZMM_L(0)));
2052 static void gen_VMOVSS_st(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2054 tcg_gen_ld_i32(s->tmp2_i32, OP_PTR2, offsetof(ZMMReg, ZMM_L(0)));
2055 tcg_gen_qemu_st_i32(s->tmp2_i32, s->A0, s->mem_index, MO_LEUL);
2058 static void gen_VPMASKMOV_st(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2061 gen_VMASKMOVPD_st(s, env, decode);
2063 gen_VMASKMOVPS_st(s, env, decode);
2067 static void gen_VPERMD(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2070 gen_helper_vpermd_ymm(OP_PTR0, OP_PTR1, OP_PTR2);
2073 static void gen_VPERM2x128(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2075 TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
2077 gen_helper_vpermdq_ymm(OP_PTR0, OP_PTR1, OP_PTR2, imm);
2080 static void gen_VPHMINPOSUW(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2083 gen_helper_phminposuw_xmm(cpu_env, OP_PTR0, OP_PTR2);
2086 static void gen_VROUNDSD(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2088 TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
2090 gen_helper_roundsd_xmm(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2, imm);
2093 static void gen_VROUNDSS(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2095 TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
2097 gen_helper_roundss_xmm(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2, imm);
2100 static void gen_VSHUF(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2102 TCGv_i32 imm = tcg_constant_i32(decode->immediate);
2103 SSEFunc_0_pppi ps, pd, fn;
2104 ps = s->vex_l ? gen_helper_shufps_ymm : gen_helper_shufps_xmm;
2105 pd = s->vex_l ? gen_helper_shufpd_ymm : gen_helper_shufpd_xmm;
2106 fn = s->prefix & PREFIX_DATA ? pd : ps;
2107 fn(OP_PTR0, OP_PTR1, OP_PTR2, imm);
2110 static void gen_VUCOMI(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2113 fn = s->prefix & PREFIX_DATA ? gen_helper_ucomisd : gen_helper_ucomiss;
2114 fn(cpu_env, OP_PTR1, OP_PTR2);
2115 set_cc_op(s, CC_OP_EFLAGS);
2118 static void gen_VZEROALL(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2120 TCGv_ptr ptr = tcg_temp_new_ptr();
2122 tcg_gen_addi_ptr(ptr, cpu_env, offsetof(CPUX86State, xmm_t0));
2123 gen_helper_memset(ptr, ptr, tcg_constant_i32(0),
2124 tcg_constant_ptr(CPU_NB_REGS * sizeof(ZMMReg)));
2125 tcg_temp_free_ptr(ptr);
2128 static void gen_VZEROUPPER(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2132 for (i = 0; i < CPU_NB_REGS; i++) {
2133 int offset = offsetof(CPUX86State, xmm_regs[i].ZMM_X(1));
2134 tcg_gen_gvec_dup_imm(MO_64, offset, 16, 16, 0);