1/* 2 * Tiny Code Generator for QEMU 3 * 4 * Copyright (c) 2008 Fabrice Bellard 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 * THE SOFTWARE. 23 */ 24 25/* Used for function call generation. */ 26#define TCG_TARGET_STACK_ALIGN 16 27#if defined(_WIN64) 28#define TCG_TARGET_CALL_STACK_OFFSET 32 29#else 30#define TCG_TARGET_CALL_STACK_OFFSET 0 31#endif 32#define TCG_TARGET_CALL_ARG_I32 TCG_CALL_ARG_NORMAL 33#define TCG_TARGET_CALL_ARG_I64 TCG_CALL_ARG_NORMAL 34#if defined(_WIN64) 35# define TCG_TARGET_CALL_ARG_I128 TCG_CALL_ARG_BY_REF 36# define TCG_TARGET_CALL_RET_I128 TCG_CALL_RET_BY_VEC 37#elif TCG_TARGET_REG_BITS == 64 38# define TCG_TARGET_CALL_ARG_I128 TCG_CALL_ARG_NORMAL 39# define TCG_TARGET_CALL_RET_I128 TCG_CALL_RET_NORMAL 40#else 41# define TCG_TARGET_CALL_ARG_I128 TCG_CALL_ARG_NORMAL 42# define TCG_TARGET_CALL_RET_I128 TCG_CALL_RET_BY_REF 43#endif 44 45#ifdef CONFIG_DEBUG_TCG 46static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = { 47#if TCG_TARGET_REG_BITS == 64 48 "%rax", "%rcx", "%rdx", "%rbx", "%rsp", "%rbp", "%rsi", "%rdi", 49#else 50 "%eax", "%ecx", "%edx", "%ebx", "%esp", "%ebp", "%esi", "%edi", 51#endif 52 "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15", 53 "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7", 54#if TCG_TARGET_REG_BITS == 64 55 "%xmm8", "%xmm9", "%xmm10", "%xmm11", 56 "%xmm12", "%xmm13", "%xmm14", "%xmm15", 57#endif 58}; 59#endif 60 61static const int tcg_target_reg_alloc_order[] = { 62#if TCG_TARGET_REG_BITS == 64 63 TCG_REG_RBP, 64 TCG_REG_RBX, 65 TCG_REG_R12, 66 TCG_REG_R13, 67 TCG_REG_R14, 68 TCG_REG_R15, 69 TCG_REG_R10, 70 TCG_REG_R11, 71 TCG_REG_R9, 72 TCG_REG_R8, 73 TCG_REG_RCX, 74 TCG_REG_RDX, 75 TCG_REG_RSI, 76 TCG_REG_RDI, 77 TCG_REG_RAX, 78#else 79 TCG_REG_EBX, 80 TCG_REG_ESI, 81 TCG_REG_EDI, 82 TCG_REG_EBP, 83 TCG_REG_ECX, 84 TCG_REG_EDX, 85 TCG_REG_EAX, 86#endif 87 TCG_REG_XMM0, 88 TCG_REG_XMM1, 89 TCG_REG_XMM2, 90 TCG_REG_XMM3, 91 TCG_REG_XMM4, 92 TCG_REG_XMM5, 93#ifndef _WIN64 94 /* The Win64 ABI has xmm6-xmm15 as caller-saves, and we do not save 95 any of them. Therefore only allow xmm0-xmm5 to be allocated. */ 96 TCG_REG_XMM6, 97 TCG_REG_XMM7, 98#if TCG_TARGET_REG_BITS == 64 99 TCG_REG_XMM8, 100 TCG_REG_XMM9, 101 TCG_REG_XMM10, 102 TCG_REG_XMM11, 103 TCG_REG_XMM12, 104 TCG_REG_XMM13, 105 TCG_REG_XMM14, 106 TCG_REG_XMM15, 107#endif 108#endif 109}; 110 111#define TCG_TMP_VEC TCG_REG_XMM5 112 113static const int tcg_target_call_iarg_regs[] = { 114#if TCG_TARGET_REG_BITS == 64 115#if defined(_WIN64) 116 TCG_REG_RCX, 117 TCG_REG_RDX, 118#else 119 TCG_REG_RDI, 120 TCG_REG_RSI, 121 TCG_REG_RDX, 122 TCG_REG_RCX, 123#endif 124 TCG_REG_R8, 125 TCG_REG_R9, 126#else 127 /* 32 bit mode uses stack based calling convention (GCC default). */ 128#endif 129}; 130 131static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot) 132{ 133 switch (kind) { 134 case TCG_CALL_RET_NORMAL: 135 tcg_debug_assert(slot >= 0 && slot <= 1); 136 return slot ? TCG_REG_EDX : TCG_REG_EAX; 137#ifdef _WIN64 138 case TCG_CALL_RET_BY_VEC: 139 tcg_debug_assert(slot == 0); 140 return TCG_REG_XMM0; 141#endif 142 default: 143 g_assert_not_reached(); 144 } 145} 146 147/* Constants we accept. */ 148#define TCG_CT_CONST_S32 0x100 149#define TCG_CT_CONST_U32 0x200 150#define TCG_CT_CONST_I32 0x400 151#define TCG_CT_CONST_WSZ 0x800 152#define TCG_CT_CONST_TST 0x1000 153#define TCG_CT_CONST_ZERO 0x2000 154 155/* Registers used with L constraint, which are the first argument 156 registers on x86_64, and two random call clobbered registers on 157 i386. */ 158#if TCG_TARGET_REG_BITS == 64 159# define TCG_REG_L0 tcg_target_call_iarg_regs[0] 160# define TCG_REG_L1 tcg_target_call_iarg_regs[1] 161#else 162# define TCG_REG_L0 TCG_REG_EAX 163# define TCG_REG_L1 TCG_REG_EDX 164#endif 165 166#if TCG_TARGET_REG_BITS == 64 167# define ALL_GENERAL_REGS 0x0000ffffu 168# define ALL_VECTOR_REGS 0xffff0000u 169# define ALL_BYTEL_REGS ALL_GENERAL_REGS 170#else 171# define ALL_GENERAL_REGS 0x000000ffu 172# define ALL_VECTOR_REGS 0x00ff0000u 173# define ALL_BYTEL_REGS 0x0000000fu 174#endif 175#define SOFTMMU_RESERVE_REGS \ 176 (tcg_use_softmmu ? (1 << TCG_REG_L0) | (1 << TCG_REG_L1) : 0) 177 178#define have_bmi2 (cpuinfo & CPUINFO_BMI2) 179#define have_lzcnt (cpuinfo & CPUINFO_LZCNT) 180 181static const tcg_insn_unit *tb_ret_addr; 182 183static bool patch_reloc(tcg_insn_unit *code_ptr, int type, 184 intptr_t value, intptr_t addend) 185{ 186 value += addend; 187 switch(type) { 188 case R_386_PC32: 189 value -= (uintptr_t)tcg_splitwx_to_rx(code_ptr); 190 if (value != (int32_t)value) { 191 return false; 192 } 193 /* FALLTHRU */ 194 case R_386_32: 195 tcg_patch32(code_ptr, value); 196 break; 197 case R_386_PC8: 198 value -= (uintptr_t)tcg_splitwx_to_rx(code_ptr); 199 if (value != (int8_t)value) { 200 return false; 201 } 202 tcg_patch8(code_ptr, value); 203 break; 204 default: 205 g_assert_not_reached(); 206 } 207 return true; 208} 209 210/* test if a constant matches the constraint */ 211static bool tcg_target_const_match(int64_t val, int ct, 212 TCGType type, TCGCond cond, int vece) 213{ 214 if (ct & TCG_CT_CONST) { 215 return 1; 216 } 217 if (type == TCG_TYPE_I32) { 218 if (ct & (TCG_CT_CONST_S32 | TCG_CT_CONST_U32 | 219 TCG_CT_CONST_I32 | TCG_CT_CONST_TST)) { 220 return 1; 221 } 222 } else { 223 if ((ct & TCG_CT_CONST_S32) && val == (int32_t)val) { 224 return 1; 225 } 226 if ((ct & TCG_CT_CONST_U32) && val == (uint32_t)val) { 227 return 1; 228 } 229 if ((ct & TCG_CT_CONST_I32) && ~val == (int32_t)~val) { 230 return 1; 231 } 232 /* 233 * This will be used in combination with TCG_CT_CONST_S32, 234 * so "normal" TESTQ is already matched. Also accept: 235 * TESTQ -> TESTL (uint32_t) 236 * TESTQ -> BT (is_power_of_2) 237 */ 238 if ((ct & TCG_CT_CONST_TST) 239 && is_tst_cond(cond) 240 && (val == (uint32_t)val || is_power_of_2(val))) { 241 return 1; 242 } 243 } 244 if ((ct & TCG_CT_CONST_WSZ) && val == (type == TCG_TYPE_I32 ? 32 : 64)) { 245 return 1; 246 } 247 if ((ct & TCG_CT_CONST_ZERO) && val == 0) { 248 return 1; 249 } 250 return 0; 251} 252 253# define LOWREGMASK(x) ((x) & 7) 254 255#define P_EXT 0x100 /* 0x0f opcode prefix */ 256#define P_EXT38 0x200 /* 0x0f 0x38 opcode prefix */ 257#define P_DATA16 0x400 /* 0x66 opcode prefix */ 258#define P_VEXW 0x1000 /* Set VEX.W = 1 */ 259#if TCG_TARGET_REG_BITS == 64 260# define P_REXW P_VEXW /* Set REX.W = 1; match VEXW */ 261# define P_REXB_R 0x2000 /* REG field as byte register */ 262# define P_REXB_RM 0x4000 /* R/M field as byte register */ 263# define P_GS 0x8000 /* gs segment override */ 264#else 265# define P_REXW 0 266# define P_REXB_R 0 267# define P_REXB_RM 0 268# define P_GS 0 269#endif 270#define P_EXT3A 0x10000 /* 0x0f 0x3a opcode prefix */ 271#define P_SIMDF3 0x20000 /* 0xf3 opcode prefix */ 272#define P_SIMDF2 0x40000 /* 0xf2 opcode prefix */ 273#define P_VEXL 0x80000 /* Set VEX.L = 1 */ 274#define P_EVEX 0x100000 /* Requires EVEX encoding */ 275 276#define OPC_ARITH_EbIb (0x80) 277#define OPC_ARITH_EvIz (0x81) 278#define OPC_ARITH_EvIb (0x83) 279#define OPC_ARITH_GvEv (0x03) /* ... plus (ARITH_FOO << 3) */ 280#define OPC_ANDN (0xf2 | P_EXT38) 281#define OPC_ADD_GvEv (OPC_ARITH_GvEv | (ARITH_ADD << 3)) 282#define OPC_AND_GvEv (OPC_ARITH_GvEv | (ARITH_AND << 3)) 283#define OPC_BLENDPS (0x0c | P_EXT3A | P_DATA16) 284#define OPC_BSF (0xbc | P_EXT) 285#define OPC_BSR (0xbd | P_EXT) 286#define OPC_BSWAP (0xc8 | P_EXT) 287#define OPC_CALL_Jz (0xe8) 288#define OPC_CMOVCC (0x40 | P_EXT) /* ... plus condition code */ 289#define OPC_CMP_GvEv (OPC_ARITH_GvEv | (ARITH_CMP << 3)) 290#define OPC_DEC_r32 (0x48) 291#define OPC_IMUL_GvEv (0xaf | P_EXT) 292#define OPC_IMUL_GvEvIb (0x6b) 293#define OPC_IMUL_GvEvIz (0x69) 294#define OPC_INC_r32 (0x40) 295#define OPC_JCC_long (0x80 | P_EXT) /* ... plus condition code */ 296#define OPC_JCC_short (0x70) /* ... plus condition code */ 297#define OPC_JMP_long (0xe9) 298#define OPC_JMP_short (0xeb) 299#define OPC_LEA (0x8d) 300#define OPC_LZCNT (0xbd | P_EXT | P_SIMDF3) 301#define OPC_MOVB_EvGv (0x88) /* stores, more or less */ 302#define OPC_MOVL_EvGv (0x89) /* stores, more or less */ 303#define OPC_MOVL_GvEv (0x8b) /* loads, more or less */ 304#define OPC_MOVB_EvIz (0xc6) 305#define OPC_MOVL_EvIz (0xc7) 306#define OPC_MOVB_Ib (0xb0) 307#define OPC_MOVL_Iv (0xb8) 308#define OPC_MOVBE_GyMy (0xf0 | P_EXT38) 309#define OPC_MOVBE_MyGy (0xf1 | P_EXT38) 310#define OPC_MOVD_VyEy (0x6e | P_EXT | P_DATA16) 311#define OPC_MOVD_EyVy (0x7e | P_EXT | P_DATA16) 312#define OPC_MOVDDUP (0x12 | P_EXT | P_SIMDF2) 313#define OPC_MOVDQA_VxWx (0x6f | P_EXT | P_DATA16) 314#define OPC_MOVDQA_WxVx (0x7f | P_EXT | P_DATA16) 315#define OPC_MOVDQU_VxWx (0x6f | P_EXT | P_SIMDF3) 316#define OPC_MOVDQU_WxVx (0x7f | P_EXT | P_SIMDF3) 317#define OPC_MOVQ_VqWq (0x7e | P_EXT | P_SIMDF3) 318#define OPC_MOVQ_WqVq (0xd6 | P_EXT | P_DATA16) 319#define OPC_MOVSBL (0xbe | P_EXT) 320#define OPC_MOVSWL (0xbf | P_EXT) 321#define OPC_MOVSLQ (0x63 | P_REXW) 322#define OPC_MOVZBL (0xb6 | P_EXT) 323#define OPC_MOVZWL (0xb7 | P_EXT) 324#define OPC_PABSB (0x1c | P_EXT38 | P_DATA16) 325#define OPC_PABSW (0x1d | P_EXT38 | P_DATA16) 326#define OPC_PABSD (0x1e | P_EXT38 | P_DATA16) 327#define OPC_VPABSQ (0x1f | P_EXT38 | P_DATA16 | P_VEXW | P_EVEX) 328#define OPC_PACKSSDW (0x6b | P_EXT | P_DATA16) 329#define OPC_PACKSSWB (0x63 | P_EXT | P_DATA16) 330#define OPC_PACKUSDW (0x2b | P_EXT38 | P_DATA16) 331#define OPC_PACKUSWB (0x67 | P_EXT | P_DATA16) 332#define OPC_PADDB (0xfc | P_EXT | P_DATA16) 333#define OPC_PADDW (0xfd | P_EXT | P_DATA16) 334#define OPC_PADDD (0xfe | P_EXT | P_DATA16) 335#define OPC_PADDQ (0xd4 | P_EXT | P_DATA16) 336#define OPC_PADDSB (0xec | P_EXT | P_DATA16) 337#define OPC_PADDSW (0xed | P_EXT | P_DATA16) 338#define OPC_PADDUB (0xdc | P_EXT | P_DATA16) 339#define OPC_PADDUW (0xdd | P_EXT | P_DATA16) 340#define OPC_PAND (0xdb | P_EXT | P_DATA16) 341#define OPC_PANDN (0xdf | P_EXT | P_DATA16) 342#define OPC_PBLENDW (0x0e | P_EXT3A | P_DATA16) 343#define OPC_PCMPEQB (0x74 | P_EXT | P_DATA16) 344#define OPC_PCMPEQW (0x75 | P_EXT | P_DATA16) 345#define OPC_PCMPEQD (0x76 | P_EXT | P_DATA16) 346#define OPC_PCMPEQQ (0x29 | P_EXT38 | P_DATA16) 347#define OPC_PCMPGTB (0x64 | P_EXT | P_DATA16) 348#define OPC_PCMPGTW (0x65 | P_EXT | P_DATA16) 349#define OPC_PCMPGTD (0x66 | P_EXT | P_DATA16) 350#define OPC_PCMPGTQ (0x37 | P_EXT38 | P_DATA16) 351#define OPC_PEXTRD (0x16 | P_EXT3A | P_DATA16) 352#define OPC_PINSRD (0x22 | P_EXT3A | P_DATA16) 353#define OPC_PMAXSB (0x3c | P_EXT38 | P_DATA16) 354#define OPC_PMAXSW (0xee | P_EXT | P_DATA16) 355#define OPC_PMAXSD (0x3d | P_EXT38 | P_DATA16) 356#define OPC_VPMAXSQ (0x3d | P_EXT38 | P_DATA16 | P_VEXW | P_EVEX) 357#define OPC_PMAXUB (0xde | P_EXT | P_DATA16) 358#define OPC_PMAXUW (0x3e | P_EXT38 | P_DATA16) 359#define OPC_PMAXUD (0x3f | P_EXT38 | P_DATA16) 360#define OPC_VPMAXUQ (0x3f | P_EXT38 | P_DATA16 | P_VEXW | P_EVEX) 361#define OPC_PMINSB (0x38 | P_EXT38 | P_DATA16) 362#define OPC_PMINSW (0xea | P_EXT | P_DATA16) 363#define OPC_PMINSD (0x39 | P_EXT38 | P_DATA16) 364#define OPC_VPMINSQ (0x39 | P_EXT38 | P_DATA16 | P_VEXW | P_EVEX) 365#define OPC_PMINUB (0xda | P_EXT | P_DATA16) 366#define OPC_PMINUW (0x3a | P_EXT38 | P_DATA16) 367#define OPC_PMINUD (0x3b | P_EXT38 | P_DATA16) 368#define OPC_VPMINUQ (0x3b | P_EXT38 | P_DATA16 | P_VEXW | P_EVEX) 369#define OPC_PMOVSXBW (0x20 | P_EXT38 | P_DATA16) 370#define OPC_PMOVSXWD (0x23 | P_EXT38 | P_DATA16) 371#define OPC_PMOVSXDQ (0x25 | P_EXT38 | P_DATA16) 372#define OPC_PMOVZXBW (0x30 | P_EXT38 | P_DATA16) 373#define OPC_PMOVZXWD (0x33 | P_EXT38 | P_DATA16) 374#define OPC_PMOVZXDQ (0x35 | P_EXT38 | P_DATA16) 375#define OPC_PMULLW (0xd5 | P_EXT | P_DATA16) 376#define OPC_PMULLD (0x40 | P_EXT38 | P_DATA16) 377#define OPC_VPMULLQ (0x40 | P_EXT38 | P_DATA16 | P_VEXW | P_EVEX) 378#define OPC_POR (0xeb | P_EXT | P_DATA16) 379#define OPC_PSHUFB (0x00 | P_EXT38 | P_DATA16) 380#define OPC_PSHUFD (0x70 | P_EXT | P_DATA16) 381#define OPC_PSHUFLW (0x70 | P_EXT | P_SIMDF2) 382#define OPC_PSHUFHW (0x70 | P_EXT | P_SIMDF3) 383#define OPC_PSHIFTW_Ib (0x71 | P_EXT | P_DATA16) /* /2 /6 /4 */ 384#define OPC_PSHIFTD_Ib (0x72 | P_EXT | P_DATA16) /* /1 /2 /6 /4 */ 385#define OPC_PSHIFTQ_Ib (0x73 | P_EXT | P_DATA16) /* /2 /6 /4 */ 386#define OPC_PSLLW (0xf1 | P_EXT | P_DATA16) 387#define OPC_PSLLD (0xf2 | P_EXT | P_DATA16) 388#define OPC_PSLLQ (0xf3 | P_EXT | P_DATA16) 389#define OPC_PSRAW (0xe1 | P_EXT | P_DATA16) 390#define OPC_PSRAD (0xe2 | P_EXT | P_DATA16) 391#define OPC_VPSRAQ (0xe2 | P_EXT | P_DATA16 | P_VEXW | P_EVEX) 392#define OPC_PSRLW (0xd1 | P_EXT | P_DATA16) 393#define OPC_PSRLD (0xd2 | P_EXT | P_DATA16) 394#define OPC_PSRLQ (0xd3 | P_EXT | P_DATA16) 395#define OPC_PSUBB (0xf8 | P_EXT | P_DATA16) 396#define OPC_PSUBW (0xf9 | P_EXT | P_DATA16) 397#define OPC_PSUBD (0xfa | P_EXT | P_DATA16) 398#define OPC_PSUBQ (0xfb | P_EXT | P_DATA16) 399#define OPC_PSUBSB (0xe8 | P_EXT | P_DATA16) 400#define OPC_PSUBSW (0xe9 | P_EXT | P_DATA16) 401#define OPC_PSUBUB (0xd8 | P_EXT | P_DATA16) 402#define OPC_PSUBUW (0xd9 | P_EXT | P_DATA16) 403#define OPC_PUNPCKLBW (0x60 | P_EXT | P_DATA16) 404#define OPC_PUNPCKLWD (0x61 | P_EXT | P_DATA16) 405#define OPC_PUNPCKLDQ (0x62 | P_EXT | P_DATA16) 406#define OPC_PUNPCKLQDQ (0x6c | P_EXT | P_DATA16) 407#define OPC_PUNPCKHBW (0x68 | P_EXT | P_DATA16) 408#define OPC_PUNPCKHWD (0x69 | P_EXT | P_DATA16) 409#define OPC_PUNPCKHDQ (0x6a | P_EXT | P_DATA16) 410#define OPC_PUNPCKHQDQ (0x6d | P_EXT | P_DATA16) 411#define OPC_PXOR (0xef | P_EXT | P_DATA16) 412#define OPC_POP_r32 (0x58) 413#define OPC_POPCNT (0xb8 | P_EXT | P_SIMDF3) 414#define OPC_PUSH_r32 (0x50) 415#define OPC_PUSH_Iv (0x68) 416#define OPC_PUSH_Ib (0x6a) 417#define OPC_RET (0xc3) 418#define OPC_SETCC (0x90 | P_EXT | P_REXB_RM) /* ... plus cc */ 419#define OPC_SHIFT_1 (0xd1) 420#define OPC_SHIFT_Ib (0xc1) 421#define OPC_SHIFT_cl (0xd3) 422#define OPC_SARX (0xf7 | P_EXT38 | P_SIMDF3) 423#define OPC_SHUFPS (0xc6 | P_EXT) 424#define OPC_SHLX (0xf7 | P_EXT38 | P_DATA16) 425#define OPC_SHRX (0xf7 | P_EXT38 | P_SIMDF2) 426#define OPC_SHRD_Ib (0xac | P_EXT) 427#define OPC_STC (0xf9) 428#define OPC_TESTB (0x84) 429#define OPC_TESTL (0x85) 430#define OPC_TZCNT (0xbc | P_EXT | P_SIMDF3) 431#define OPC_UD2 (0x0b | P_EXT) 432#define OPC_VPBLENDD (0x02 | P_EXT3A | P_DATA16) 433#define OPC_VPBLENDVB (0x4c | P_EXT3A | P_DATA16) 434#define OPC_VPBLENDMB (0x66 | P_EXT38 | P_DATA16 | P_EVEX) 435#define OPC_VPBLENDMW (0x66 | P_EXT38 | P_DATA16 | P_VEXW | P_EVEX) 436#define OPC_VPBLENDMD (0x64 | P_EXT38 | P_DATA16 | P_EVEX) 437#define OPC_VPBLENDMQ (0x64 | P_EXT38 | P_DATA16 | P_VEXW | P_EVEX) 438#define OPC_VPCMPB (0x3f | P_EXT3A | P_DATA16 | P_EVEX) 439#define OPC_VPCMPUB (0x3e | P_EXT3A | P_DATA16 | P_EVEX) 440#define OPC_VPCMPW (0x3f | P_EXT3A | P_DATA16 | P_VEXW | P_EVEX) 441#define OPC_VPCMPUW (0x3e | P_EXT3A | P_DATA16 | P_VEXW | P_EVEX) 442#define OPC_VPCMPD (0x1f | P_EXT3A | P_DATA16 | P_EVEX) 443#define OPC_VPCMPUD (0x1e | P_EXT3A | P_DATA16 | P_EVEX) 444#define OPC_VPCMPQ (0x1f | P_EXT3A | P_DATA16 | P_VEXW | P_EVEX) 445#define OPC_VPCMPUQ (0x1e | P_EXT3A | P_DATA16 | P_VEXW | P_EVEX) 446#define OPC_VPINSRB (0x20 | P_EXT3A | P_DATA16) 447#define OPC_VPINSRW (0xc4 | P_EXT | P_DATA16) 448#define OPC_VBROADCASTSS (0x18 | P_EXT38 | P_DATA16) 449#define OPC_VBROADCASTSD (0x19 | P_EXT38 | P_DATA16) 450#define OPC_VPBROADCASTB (0x78 | P_EXT38 | P_DATA16) 451#define OPC_VPBROADCASTW (0x79 | P_EXT38 | P_DATA16) 452#define OPC_VPBROADCASTD (0x58 | P_EXT38 | P_DATA16) 453#define OPC_VPBROADCASTQ (0x59 | P_EXT38 | P_DATA16) 454#define OPC_VPMOVM2B (0x28 | P_EXT38 | P_SIMDF3 | P_EVEX) 455#define OPC_VPMOVM2W (0x28 | P_EXT38 | P_SIMDF3 | P_VEXW | P_EVEX) 456#define OPC_VPMOVM2D (0x38 | P_EXT38 | P_SIMDF3 | P_EVEX) 457#define OPC_VPMOVM2Q (0x38 | P_EXT38 | P_SIMDF3 | P_VEXW | P_EVEX) 458#define OPC_VPERMQ (0x00 | P_EXT3A | P_DATA16 | P_VEXW) 459#define OPC_VPERM2I128 (0x46 | P_EXT3A | P_DATA16 | P_VEXL) 460#define OPC_VPROLVD (0x15 | P_EXT38 | P_DATA16 | P_EVEX) 461#define OPC_VPROLVQ (0x15 | P_EXT38 | P_DATA16 | P_VEXW | P_EVEX) 462#define OPC_VPRORVD (0x14 | P_EXT38 | P_DATA16 | P_EVEX) 463#define OPC_VPRORVQ (0x14 | P_EXT38 | P_DATA16 | P_VEXW | P_EVEX) 464#define OPC_VPSHLDW (0x70 | P_EXT3A | P_DATA16 | P_VEXW | P_EVEX) 465#define OPC_VPSHLDD (0x71 | P_EXT3A | P_DATA16 | P_EVEX) 466#define OPC_VPSHLDQ (0x71 | P_EXT3A | P_DATA16 | P_VEXW | P_EVEX) 467#define OPC_VPSHLDVW (0x70 | P_EXT38 | P_DATA16 | P_VEXW | P_EVEX) 468#define OPC_VPSHLDVD (0x71 | P_EXT38 | P_DATA16 | P_EVEX) 469#define OPC_VPSHLDVQ (0x71 | P_EXT38 | P_DATA16 | P_VEXW | P_EVEX) 470#define OPC_VPSHRDVW (0x72 | P_EXT38 | P_DATA16 | P_VEXW | P_EVEX) 471#define OPC_VPSHRDVD (0x73 | P_EXT38 | P_DATA16 | P_EVEX) 472#define OPC_VPSHRDVQ (0x73 | P_EXT38 | P_DATA16 | P_VEXW | P_EVEX) 473#define OPC_VPSLLVW (0x12 | P_EXT38 | P_DATA16 | P_VEXW | P_EVEX) 474#define OPC_VPSLLVD (0x47 | P_EXT38 | P_DATA16) 475#define OPC_VPSLLVQ (0x47 | P_EXT38 | P_DATA16 | P_VEXW) 476#define OPC_VPSRAVW (0x11 | P_EXT38 | P_DATA16 | P_VEXW | P_EVEX) 477#define OPC_VPSRAVD (0x46 | P_EXT38 | P_DATA16) 478#define OPC_VPSRAVQ (0x46 | P_EXT38 | P_DATA16 | P_VEXW | P_EVEX) 479#define OPC_VPSRLVW (0x10 | P_EXT38 | P_DATA16 | P_VEXW | P_EVEX) 480#define OPC_VPSRLVD (0x45 | P_EXT38 | P_DATA16) 481#define OPC_VPSRLVQ (0x45 | P_EXT38 | P_DATA16 | P_VEXW) 482#define OPC_VPTERNLOGQ (0x25 | P_EXT3A | P_DATA16 | P_VEXW | P_EVEX) 483#define OPC_VPTESTMB (0x26 | P_EXT38 | P_DATA16 | P_EVEX) 484#define OPC_VPTESTMW (0x26 | P_EXT38 | P_DATA16 | P_VEXW | P_EVEX) 485#define OPC_VPTESTMD (0x27 | P_EXT38 | P_DATA16 | P_EVEX) 486#define OPC_VPTESTMQ (0x27 | P_EXT38 | P_DATA16 | P_VEXW | P_EVEX) 487#define OPC_VPTESTNMB (0x26 | P_EXT38 | P_SIMDF3 | P_EVEX) 488#define OPC_VPTESTNMW (0x26 | P_EXT38 | P_SIMDF3 | P_VEXW | P_EVEX) 489#define OPC_VPTESTNMD (0x27 | P_EXT38 | P_SIMDF3 | P_EVEX) 490#define OPC_VPTESTNMQ (0x27 | P_EXT38 | P_SIMDF3 | P_VEXW | P_EVEX) 491#define OPC_VZEROUPPER (0x77 | P_EXT) 492#define OPC_XCHG_ax_r32 (0x90) 493#define OPC_XCHG_EvGv (0x87) 494 495#define OPC_GRP3_Eb (0xf6) 496#define OPC_GRP3_Ev (0xf7) 497#define OPC_GRP5 (0xff) 498#define OPC_GRP14 (0x73 | P_EXT | P_DATA16) 499#define OPC_GRPBT (0xba | P_EXT) 500 501#define OPC_GRPBT_BT 4 502#define OPC_GRPBT_BTS 5 503#define OPC_GRPBT_BTR 6 504#define OPC_GRPBT_BTC 7 505 506/* Group 1 opcode extensions for 0x80-0x83. 507 These are also used as modifiers for OPC_ARITH. */ 508#define ARITH_ADD 0 509#define ARITH_OR 1 510#define ARITH_ADC 2 511#define ARITH_SBB 3 512#define ARITH_AND 4 513#define ARITH_SUB 5 514#define ARITH_XOR 6 515#define ARITH_CMP 7 516 517/* Group 2 opcode extensions for 0xc0, 0xc1, 0xd0-0xd3. */ 518#define SHIFT_ROL 0 519#define SHIFT_ROR 1 520#define SHIFT_SHL 4 521#define SHIFT_SHR 5 522#define SHIFT_SAR 7 523 524/* Group 3 opcode extensions for 0xf6, 0xf7. To be used with OPC_GRP3. */ 525#define EXT3_TESTi 0 526#define EXT3_NOT 2 527#define EXT3_NEG 3 528#define EXT3_MUL 4 529#define EXT3_IMUL 5 530#define EXT3_DIV 6 531#define EXT3_IDIV 7 532 533/* Group 5 opcode extensions for 0xff. To be used with OPC_GRP5. */ 534#define EXT5_INC_Ev 0 535#define EXT5_DEC_Ev 1 536#define EXT5_CALLN_Ev 2 537#define EXT5_JMPN_Ev 4 538 539/* Condition codes to be added to OPC_JCC_{long,short}. */ 540#define JCC_JMP (-1) 541#define JCC_JO 0x0 542#define JCC_JNO 0x1 543#define JCC_JB 0x2 544#define JCC_JAE 0x3 545#define JCC_JE 0x4 546#define JCC_JNE 0x5 547#define JCC_JBE 0x6 548#define JCC_JA 0x7 549#define JCC_JS 0x8 550#define JCC_JNS 0x9 551#define JCC_JP 0xa 552#define JCC_JNP 0xb 553#define JCC_JL 0xc 554#define JCC_JGE 0xd 555#define JCC_JLE 0xe 556#define JCC_JG 0xf 557 558static const uint8_t tcg_cond_to_jcc[] = { 559 [TCG_COND_EQ] = JCC_JE, 560 [TCG_COND_NE] = JCC_JNE, 561 [TCG_COND_LT] = JCC_JL, 562 [TCG_COND_GE] = JCC_JGE, 563 [TCG_COND_LE] = JCC_JLE, 564 [TCG_COND_GT] = JCC_JG, 565 [TCG_COND_LTU] = JCC_JB, 566 [TCG_COND_GEU] = JCC_JAE, 567 [TCG_COND_LEU] = JCC_JBE, 568 [TCG_COND_GTU] = JCC_JA, 569 [TCG_COND_TSTEQ] = JCC_JE, 570 [TCG_COND_TSTNE] = JCC_JNE, 571}; 572 573#if TCG_TARGET_REG_BITS == 64 574static void tcg_out_opc(TCGContext *s, int opc, int r, int rm, int x) 575{ 576 int rex; 577 578 if (opc & P_GS) { 579 tcg_out8(s, 0x65); 580 } 581 if (opc & P_DATA16) { 582 /* We should never be asking for both 16 and 64-bit operation. */ 583 tcg_debug_assert((opc & P_REXW) == 0); 584 tcg_out8(s, 0x66); 585 } 586 if (opc & P_SIMDF3) { 587 tcg_out8(s, 0xf3); 588 } else if (opc & P_SIMDF2) { 589 tcg_out8(s, 0xf2); 590 } 591 592 rex = 0; 593 rex |= (opc & P_REXW) ? 0x8 : 0x0; /* REX.W */ 594 rex |= (r & 8) >> 1; /* REX.R */ 595 rex |= (x & 8) >> 2; /* REX.X */ 596 rex |= (rm & 8) >> 3; /* REX.B */ 597 598 /* P_REXB_{R,RM} indicates that the given register is the low byte. 599 For %[abcd]l we need no REX prefix, but for %{si,di,bp,sp}l we do, 600 as otherwise the encoding indicates %[abcd]h. Note that the values 601 that are ORed in merely indicate that the REX byte must be present; 602 those bits get discarded in output. */ 603 rex |= opc & (r >= 4 ? P_REXB_R : 0); 604 rex |= opc & (rm >= 4 ? P_REXB_RM : 0); 605 606 if (rex) { 607 tcg_out8(s, (uint8_t)(rex | 0x40)); 608 } 609 610 if (opc & (P_EXT | P_EXT38 | P_EXT3A)) { 611 tcg_out8(s, 0x0f); 612 if (opc & P_EXT38) { 613 tcg_out8(s, 0x38); 614 } else if (opc & P_EXT3A) { 615 tcg_out8(s, 0x3a); 616 } 617 } 618 619 tcg_out8(s, opc); 620} 621#else 622static void tcg_out_opc(TCGContext *s, int opc) 623{ 624 if (opc & P_DATA16) { 625 tcg_out8(s, 0x66); 626 } 627 if (opc & P_SIMDF3) { 628 tcg_out8(s, 0xf3); 629 } else if (opc & P_SIMDF2) { 630 tcg_out8(s, 0xf2); 631 } 632 if (opc & (P_EXT | P_EXT38 | P_EXT3A)) { 633 tcg_out8(s, 0x0f); 634 if (opc & P_EXT38) { 635 tcg_out8(s, 0x38); 636 } else if (opc & P_EXT3A) { 637 tcg_out8(s, 0x3a); 638 } 639 } 640 tcg_out8(s, opc); 641} 642/* Discard the register arguments to tcg_out_opc early, so as not to penalize 643 the 32-bit compilation paths. This method works with all versions of gcc, 644 whereas relying on optimization may not be able to exclude them. */ 645#define tcg_out_opc(s, opc, r, rm, x) (tcg_out_opc)(s, opc) 646#endif 647 648static void tcg_out_modrm(TCGContext *s, int opc, int r, int rm) 649{ 650 tcg_out_opc(s, opc, r, rm, 0); 651 tcg_out8(s, 0xc0 | (LOWREGMASK(r) << 3) | LOWREGMASK(rm)); 652} 653 654static void tcg_out_vex_opc(TCGContext *s, int opc, int r, int v, 655 int rm, int index) 656{ 657 int tmp; 658 659 if (opc & P_GS) { 660 tcg_out8(s, 0x65); 661 } 662 /* Use the two byte form if possible, which cannot encode 663 VEX.W, VEX.B, VEX.X, or an m-mmmm field other than P_EXT. */ 664 if ((opc & (P_EXT | P_EXT38 | P_EXT3A | P_VEXW)) == P_EXT 665 && ((rm | index) & 8) == 0) { 666 /* Two byte VEX prefix. */ 667 tcg_out8(s, 0xc5); 668 669 tmp = (r & 8 ? 0 : 0x80); /* VEX.R */ 670 } else { 671 /* Three byte VEX prefix. */ 672 tcg_out8(s, 0xc4); 673 674 /* VEX.m-mmmm */ 675 if (opc & P_EXT3A) { 676 tmp = 3; 677 } else if (opc & P_EXT38) { 678 tmp = 2; 679 } else if (opc & P_EXT) { 680 tmp = 1; 681 } else { 682 g_assert_not_reached(); 683 } 684 tmp |= (r & 8 ? 0 : 0x80); /* VEX.R */ 685 tmp |= (index & 8 ? 0 : 0x40); /* VEX.X */ 686 tmp |= (rm & 8 ? 0 : 0x20); /* VEX.B */ 687 tcg_out8(s, tmp); 688 689 tmp = (opc & P_VEXW ? 0x80 : 0); /* VEX.W */ 690 } 691 692 tmp |= (opc & P_VEXL ? 0x04 : 0); /* VEX.L */ 693 /* VEX.pp */ 694 if (opc & P_DATA16) { 695 tmp |= 1; /* 0x66 */ 696 } else if (opc & P_SIMDF3) { 697 tmp |= 2; /* 0xf3 */ 698 } else if (opc & P_SIMDF2) { 699 tmp |= 3; /* 0xf2 */ 700 } 701 tmp |= (~v & 15) << 3; /* VEX.vvvv */ 702 tcg_out8(s, tmp); 703 tcg_out8(s, opc); 704} 705 706static void tcg_out_evex_opc(TCGContext *s, int opc, int r, int v, 707 int rm, int index, int aaa, bool z) 708{ 709 /* The entire 4-byte evex prefix; with R' and V' set. */ 710 uint32_t p = 0x08041062; 711 int mm, pp; 712 713 tcg_debug_assert(have_avx512vl); 714 715 /* EVEX.mm */ 716 if (opc & P_EXT3A) { 717 mm = 3; 718 } else if (opc & P_EXT38) { 719 mm = 2; 720 } else if (opc & P_EXT) { 721 mm = 1; 722 } else { 723 g_assert_not_reached(); 724 } 725 726 /* EVEX.pp */ 727 if (opc & P_DATA16) { 728 pp = 1; /* 0x66 */ 729 } else if (opc & P_SIMDF3) { 730 pp = 2; /* 0xf3 */ 731 } else if (opc & P_SIMDF2) { 732 pp = 3; /* 0xf2 */ 733 } else { 734 pp = 0; 735 } 736 737 p = deposit32(p, 8, 2, mm); 738 p = deposit32(p, 13, 1, (rm & 8) == 0); /* EVEX.RXB.B */ 739 p = deposit32(p, 14, 1, (index & 8) == 0); /* EVEX.RXB.X */ 740 p = deposit32(p, 15, 1, (r & 8) == 0); /* EVEX.RXB.R */ 741 p = deposit32(p, 16, 2, pp); 742 p = deposit32(p, 19, 4, ~v); 743 p = deposit32(p, 23, 1, (opc & P_VEXW) != 0); 744 p = deposit32(p, 24, 3, aaa); 745 p = deposit32(p, 29, 2, (opc & P_VEXL) != 0); 746 p = deposit32(p, 31, 1, z); 747 748 tcg_out32(s, p); 749 tcg_out8(s, opc); 750} 751 752static void tcg_out_vex_modrm(TCGContext *s, int opc, int r, int v, int rm) 753{ 754 if (opc & P_EVEX) { 755 tcg_out_evex_opc(s, opc, r, v, rm, 0, 0, false); 756 } else { 757 tcg_out_vex_opc(s, opc, r, v, rm, 0); 758 } 759 tcg_out8(s, 0xc0 | (LOWREGMASK(r) << 3) | LOWREGMASK(rm)); 760} 761 762static void tcg_out_vex_modrm_type(TCGContext *s, int opc, 763 int r, int v, int rm, TCGType type) 764{ 765 if (type == TCG_TYPE_V256) { 766 opc |= P_VEXL; 767 } 768 tcg_out_vex_modrm(s, opc, r, v, rm); 769} 770 771static void tcg_out_evex_modrm_type(TCGContext *s, int opc, int r, int v, 772 int rm, int aaa, bool z, TCGType type) 773{ 774 if (type == TCG_TYPE_V256) { 775 opc |= P_VEXL; 776 } 777 tcg_out_evex_opc(s, opc, r, v, rm, 0, aaa, z); 778 tcg_out8(s, 0xc0 | (LOWREGMASK(r) << 3) | LOWREGMASK(rm)); 779} 780 781/* Output an opcode with a full "rm + (index<<shift) + offset" address mode. 782 We handle either RM and INDEX missing with a negative value. In 64-bit 783 mode for absolute addresses, ~RM is the size of the immediate operand 784 that will follow the instruction. */ 785 786static void tcg_out_sib_offset(TCGContext *s, int r, int rm, int index, 787 int shift, intptr_t offset) 788{ 789 int mod, len; 790 791 if (index < 0 && rm < 0) { 792 if (TCG_TARGET_REG_BITS == 64) { 793 /* Try for a rip-relative addressing mode. This has replaced 794 the 32-bit-mode absolute addressing encoding. */ 795 intptr_t pc = (intptr_t)s->code_ptr + 5 + ~rm; 796 intptr_t disp = offset - pc; 797 if (disp == (int32_t)disp) { 798 tcg_out8(s, (LOWREGMASK(r) << 3) | 5); 799 tcg_out32(s, disp); 800 return; 801 } 802 803 /* Try for an absolute address encoding. This requires the 804 use of the MODRM+SIB encoding and is therefore larger than 805 rip-relative addressing. */ 806 if (offset == (int32_t)offset) { 807 tcg_out8(s, (LOWREGMASK(r) << 3) | 4); 808 tcg_out8(s, (4 << 3) | 5); 809 tcg_out32(s, offset); 810 return; 811 } 812 813 /* ??? The memory isn't directly addressable. */ 814 g_assert_not_reached(); 815 } else { 816 /* Absolute address. */ 817 tcg_out8(s, (r << 3) | 5); 818 tcg_out32(s, offset); 819 return; 820 } 821 } 822 823 /* Find the length of the immediate addend. Note that the encoding 824 that would be used for (%ebp) indicates absolute addressing. */ 825 if (rm < 0) { 826 mod = 0, len = 4, rm = 5; 827 } else if (offset == 0 && LOWREGMASK(rm) != TCG_REG_EBP) { 828 mod = 0, len = 0; 829 } else if (offset == (int8_t)offset) { 830 mod = 0x40, len = 1; 831 } else { 832 mod = 0x80, len = 4; 833 } 834 835 /* Use a single byte MODRM format if possible. Note that the encoding 836 that would be used for %esp is the escape to the two byte form. */ 837 if (index < 0 && LOWREGMASK(rm) != TCG_REG_ESP) { 838 /* Single byte MODRM format. */ 839 tcg_out8(s, mod | (LOWREGMASK(r) << 3) | LOWREGMASK(rm)); 840 } else { 841 /* Two byte MODRM+SIB format. */ 842 843 /* Note that the encoding that would place %esp into the index 844 field indicates no index register. In 64-bit mode, the REX.X 845 bit counts, so %r12 can be used as the index. */ 846 if (index < 0) { 847 index = 4; 848 } else { 849 tcg_debug_assert(index != TCG_REG_ESP); 850 } 851 852 tcg_out8(s, mod | (LOWREGMASK(r) << 3) | 4); 853 tcg_out8(s, (shift << 6) | (LOWREGMASK(index) << 3) | LOWREGMASK(rm)); 854 } 855 856 if (len == 1) { 857 tcg_out8(s, offset); 858 } else if (len == 4) { 859 tcg_out32(s, offset); 860 } 861} 862 863static void tcg_out_modrm_sib_offset(TCGContext *s, int opc, int r, int rm, 864 int index, int shift, intptr_t offset) 865{ 866 tcg_out_opc(s, opc, r, rm < 0 ? 0 : rm, index < 0 ? 0 : index); 867 tcg_out_sib_offset(s, r, rm, index, shift, offset); 868} 869 870static void tcg_out_vex_modrm_sib_offset(TCGContext *s, int opc, int r, int v, 871 int rm, int index, int shift, 872 intptr_t offset) 873{ 874 tcg_out_vex_opc(s, opc, r, v, rm < 0 ? 0 : rm, index < 0 ? 0 : index); 875 tcg_out_sib_offset(s, r, rm, index, shift, offset); 876} 877 878/* A simplification of the above with no index or shift. */ 879static inline void tcg_out_modrm_offset(TCGContext *s, int opc, int r, 880 int rm, intptr_t offset) 881{ 882 tcg_out_modrm_sib_offset(s, opc, r, rm, -1, 0, offset); 883} 884 885static inline void tcg_out_vex_modrm_offset(TCGContext *s, int opc, int r, 886 int v, int rm, intptr_t offset) 887{ 888 tcg_out_vex_modrm_sib_offset(s, opc, r, v, rm, -1, 0, offset); 889} 890 891/* Output an opcode with an expected reference to the constant pool. */ 892static inline void tcg_out_modrm_pool(TCGContext *s, int opc, int r) 893{ 894 tcg_out_opc(s, opc, r, 0, 0); 895 /* Absolute for 32-bit, pc-relative for 64-bit. */ 896 tcg_out8(s, LOWREGMASK(r) << 3 | 5); 897 tcg_out32(s, 0); 898} 899 900/* Output an opcode with an expected reference to the constant pool. */ 901static inline void tcg_out_vex_modrm_pool(TCGContext *s, int opc, int r) 902{ 903 tcg_out_vex_opc(s, opc, r, 0, 0, 0); 904 /* Absolute for 32-bit, pc-relative for 64-bit. */ 905 tcg_out8(s, LOWREGMASK(r) << 3 | 5); 906 tcg_out32(s, 0); 907} 908 909/* Generate dest op= src. Uses the same ARITH_* codes as tgen_arithi. */ 910static inline void tgen_arithr(TCGContext *s, int subop, int dest, int src) 911{ 912 /* Propagate an opcode prefix, such as P_REXW. */ 913 int ext = subop & ~0x7; 914 subop &= 0x7; 915 916 tcg_out_modrm(s, OPC_ARITH_GvEv + (subop << 3) + ext, dest, src); 917} 918 919static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg) 920{ 921 int rexw = 0; 922 923 if (arg == ret) { 924 return true; 925 } 926 switch (type) { 927 case TCG_TYPE_I64: 928 rexw = P_REXW; 929 /* fallthru */ 930 case TCG_TYPE_I32: 931 if (ret < 16) { 932 if (arg < 16) { 933 tcg_out_modrm(s, OPC_MOVL_GvEv + rexw, ret, arg); 934 } else { 935 tcg_out_vex_modrm(s, OPC_MOVD_EyVy + rexw, arg, 0, ret); 936 } 937 } else { 938 if (arg < 16) { 939 tcg_out_vex_modrm(s, OPC_MOVD_VyEy + rexw, ret, 0, arg); 940 } else { 941 tcg_out_vex_modrm(s, OPC_MOVQ_VqWq, ret, 0, arg); 942 } 943 } 944 break; 945 946 case TCG_TYPE_V64: 947 tcg_debug_assert(ret >= 16 && arg >= 16); 948 tcg_out_vex_modrm(s, OPC_MOVQ_VqWq, ret, 0, arg); 949 break; 950 case TCG_TYPE_V128: 951 tcg_debug_assert(ret >= 16 && arg >= 16); 952 tcg_out_vex_modrm(s, OPC_MOVDQA_VxWx, ret, 0, arg); 953 break; 954 case TCG_TYPE_V256: 955 tcg_debug_assert(ret >= 16 && arg >= 16); 956 tcg_out_vex_modrm(s, OPC_MOVDQA_VxWx | P_VEXL, ret, 0, arg); 957 break; 958 959 default: 960 g_assert_not_reached(); 961 } 962 return true; 963} 964 965static const int avx2_dup_insn[4] = { 966 OPC_VPBROADCASTB, OPC_VPBROADCASTW, 967 OPC_VPBROADCASTD, OPC_VPBROADCASTQ, 968}; 969 970static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece, 971 TCGReg r, TCGReg a) 972{ 973 if (have_avx2) { 974 tcg_out_vex_modrm_type(s, avx2_dup_insn[vece], r, 0, a, type); 975 } else { 976 switch (vece) { 977 case MO_8: 978 /* ??? With zero in a register, use PSHUFB. */ 979 tcg_out_vex_modrm(s, OPC_PUNPCKLBW, r, a, a); 980 a = r; 981 /* FALLTHRU */ 982 case MO_16: 983 tcg_out_vex_modrm(s, OPC_PUNPCKLWD, r, a, a); 984 a = r; 985 /* FALLTHRU */ 986 case MO_32: 987 tcg_out_vex_modrm(s, OPC_PSHUFD, r, 0, a); 988 /* imm8 operand: all output lanes selected from input lane 0. */ 989 tcg_out8(s, 0); 990 break; 991 case MO_64: 992 tcg_out_vex_modrm(s, OPC_PUNPCKLQDQ, r, a, a); 993 break; 994 default: 995 g_assert_not_reached(); 996 } 997 } 998 return true; 999} 1000 1001static bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece, 1002 TCGReg r, TCGReg base, intptr_t offset) 1003{ 1004 if (have_avx2) { 1005 int vex_l = (type == TCG_TYPE_V256 ? P_VEXL : 0); 1006 tcg_out_vex_modrm_offset(s, avx2_dup_insn[vece] + vex_l, 1007 r, 0, base, offset); 1008 } else { 1009 switch (vece) { 1010 case MO_64: 1011 tcg_out_vex_modrm_offset(s, OPC_MOVDDUP, r, 0, base, offset); 1012 break; 1013 case MO_32: 1014 tcg_out_vex_modrm_offset(s, OPC_VBROADCASTSS, r, 0, base, offset); 1015 break; 1016 case MO_16: 1017 tcg_out_vex_modrm_offset(s, OPC_VPINSRW, r, r, base, offset); 1018 tcg_out8(s, 0); /* imm8 */ 1019 tcg_out_dup_vec(s, type, vece, r, r); 1020 break; 1021 case MO_8: 1022 tcg_out_vex_modrm_offset(s, OPC_VPINSRB, r, r, base, offset); 1023 tcg_out8(s, 0); /* imm8 */ 1024 tcg_out_dup_vec(s, type, vece, r, r); 1025 break; 1026 default: 1027 g_assert_not_reached(); 1028 } 1029 } 1030 return true; 1031} 1032 1033static void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece, 1034 TCGReg ret, int64_t arg) 1035{ 1036 int vex_l = (type == TCG_TYPE_V256 ? P_VEXL : 0); 1037 1038 if (arg == 0) { 1039 tcg_out_vex_modrm(s, OPC_PXOR, ret, ret, ret); 1040 return; 1041 } 1042 if (arg == -1) { 1043 tcg_out_vex_modrm(s, OPC_PCMPEQB + vex_l, ret, ret, ret); 1044 return; 1045 } 1046 1047 if (TCG_TARGET_REG_BITS == 32 && vece < MO_64) { 1048 if (have_avx2) { 1049 tcg_out_vex_modrm_pool(s, OPC_VPBROADCASTD + vex_l, ret); 1050 } else { 1051 tcg_out_vex_modrm_pool(s, OPC_VBROADCASTSS, ret); 1052 } 1053 new_pool_label(s, arg, R_386_32, s->code_ptr - 4, 0); 1054 } else { 1055 if (type == TCG_TYPE_V64) { 1056 tcg_out_vex_modrm_pool(s, OPC_MOVQ_VqWq, ret); 1057 } else if (have_avx2) { 1058 tcg_out_vex_modrm_pool(s, OPC_VPBROADCASTQ + vex_l, ret); 1059 } else { 1060 tcg_out_vex_modrm_pool(s, OPC_MOVDDUP, ret); 1061 } 1062 if (TCG_TARGET_REG_BITS == 64) { 1063 new_pool_label(s, arg, R_386_PC32, s->code_ptr - 4, -4); 1064 } else { 1065 new_pool_l2(s, R_386_32, s->code_ptr - 4, 0, arg, arg >> 32); 1066 } 1067 } 1068} 1069 1070static void tcg_out_movi_vec(TCGContext *s, TCGType type, 1071 TCGReg ret, tcg_target_long arg) 1072{ 1073 if (arg == 0) { 1074 tcg_out_vex_modrm(s, OPC_PXOR, ret, ret, ret); 1075 return; 1076 } 1077 if (arg == -1) { 1078 tcg_out_vex_modrm(s, OPC_PCMPEQB, ret, ret, ret); 1079 return; 1080 } 1081 1082 int rexw = (type == TCG_TYPE_I32 ? 0 : P_REXW); 1083 tcg_out_vex_modrm_pool(s, OPC_MOVD_VyEy + rexw, ret); 1084 if (TCG_TARGET_REG_BITS == 64) { 1085 new_pool_label(s, arg, R_386_PC32, s->code_ptr - 4, -4); 1086 } else { 1087 new_pool_label(s, arg, R_386_32, s->code_ptr - 4, 0); 1088 } 1089} 1090 1091static void tcg_out_movi_int(TCGContext *s, TCGType type, 1092 TCGReg ret, tcg_target_long arg) 1093{ 1094 tcg_target_long diff; 1095 1096 if (arg == 0 && !s->carry_live) { 1097 tgen_arithr(s, ARITH_XOR, ret, ret); 1098 return; 1099 } 1100 if (arg == (uint32_t)arg || type == TCG_TYPE_I32) { 1101 tcg_out_opc(s, OPC_MOVL_Iv + LOWREGMASK(ret), 0, ret, 0); 1102 tcg_out32(s, arg); 1103 return; 1104 } 1105 if (arg == (int32_t)arg) { 1106 tcg_out_modrm(s, OPC_MOVL_EvIz + P_REXW, 0, ret); 1107 tcg_out32(s, arg); 1108 return; 1109 } 1110 1111 /* Try a 7 byte pc-relative lea before the 10 byte movq. */ 1112 diff = tcg_pcrel_diff(s, (const void *)arg) - 7; 1113 if (diff == (int32_t)diff) { 1114 tcg_out_opc(s, OPC_LEA | P_REXW, ret, 0, 0); 1115 tcg_out8(s, (LOWREGMASK(ret) << 3) | 5); 1116 tcg_out32(s, diff); 1117 return; 1118 } 1119 1120 tcg_out_opc(s, OPC_MOVL_Iv + P_REXW + LOWREGMASK(ret), 0, ret, 0); 1121 tcg_out64(s, arg); 1122} 1123 1124static void tcg_out_movi(TCGContext *s, TCGType type, 1125 TCGReg ret, tcg_target_long arg) 1126{ 1127 switch (type) { 1128 case TCG_TYPE_I32: 1129#if TCG_TARGET_REG_BITS == 64 1130 case TCG_TYPE_I64: 1131#endif 1132 if (ret < 16) { 1133 tcg_out_movi_int(s, type, ret, arg); 1134 } else { 1135 tcg_out_movi_vec(s, type, ret, arg); 1136 } 1137 break; 1138 default: 1139 g_assert_not_reached(); 1140 } 1141} 1142 1143static bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2) 1144{ 1145 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 1146 tcg_out_modrm(s, OPC_XCHG_EvGv + rexw, r1, r2); 1147 return true; 1148} 1149 1150static void tcg_out_addi_ptr(TCGContext *s, TCGReg rd, TCGReg rs, 1151 tcg_target_long imm) 1152{ 1153 /* This function is only used for passing structs by reference. */ 1154 tcg_debug_assert(imm == (int32_t)imm); 1155 tcg_out_modrm_offset(s, OPC_LEA | P_REXW, rd, rs, imm); 1156} 1157 1158static inline void tcg_out_pushi(TCGContext *s, tcg_target_long val) 1159{ 1160 if (val == (int8_t)val) { 1161 tcg_out_opc(s, OPC_PUSH_Ib, 0, 0, 0); 1162 tcg_out8(s, val); 1163 } else if (val == (int32_t)val) { 1164 tcg_out_opc(s, OPC_PUSH_Iv, 0, 0, 0); 1165 tcg_out32(s, val); 1166 } else { 1167 g_assert_not_reached(); 1168 } 1169} 1170 1171static void tcg_out_mb(TCGContext *s, unsigned a0) 1172{ 1173 /* Given the strength of x86 memory ordering, we only need care for 1174 store-load ordering. Experimentally, "lock orl $0,0(%esp)" is 1175 faster than "mfence", so don't bother with the sse insn. */ 1176 if (a0 & TCG_MO_ST_LD) { 1177 tcg_out8(s, 0xf0); 1178 tcg_out_modrm_offset(s, OPC_ARITH_EvIb, ARITH_OR, TCG_REG_ESP, 0); 1179 tcg_out8(s, 0); 1180 } 1181} 1182 1183static inline void tcg_out_push(TCGContext *s, int reg) 1184{ 1185 tcg_out_opc(s, OPC_PUSH_r32 + LOWREGMASK(reg), 0, reg, 0); 1186} 1187 1188static inline void tcg_out_pop(TCGContext *s, int reg) 1189{ 1190 tcg_out_opc(s, OPC_POP_r32 + LOWREGMASK(reg), 0, reg, 0); 1191} 1192 1193static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg ret, 1194 TCGReg arg1, intptr_t arg2) 1195{ 1196 switch (type) { 1197 case TCG_TYPE_I32: 1198 if (ret < 16) { 1199 tcg_out_modrm_offset(s, OPC_MOVL_GvEv, ret, arg1, arg2); 1200 } else { 1201 tcg_out_vex_modrm_offset(s, OPC_MOVD_VyEy, ret, 0, arg1, arg2); 1202 } 1203 break; 1204 case TCG_TYPE_I64: 1205 if (ret < 16) { 1206 tcg_out_modrm_offset(s, OPC_MOVL_GvEv | P_REXW, ret, arg1, arg2); 1207 break; 1208 } 1209 /* FALLTHRU */ 1210 case TCG_TYPE_V64: 1211 /* There is no instruction that can validate 8-byte alignment. */ 1212 tcg_debug_assert(ret >= 16); 1213 tcg_out_vex_modrm_offset(s, OPC_MOVQ_VqWq, ret, 0, arg1, arg2); 1214 break; 1215 case TCG_TYPE_V128: 1216 /* 1217 * The gvec infrastructure is asserts that v128 vector loads 1218 * and stores use a 16-byte aligned offset. Validate that the 1219 * final pointer is aligned by using an insn that will SIGSEGV. 1220 */ 1221 tcg_debug_assert(ret >= 16); 1222 tcg_out_vex_modrm_offset(s, OPC_MOVDQA_VxWx, ret, 0, arg1, arg2); 1223 break; 1224 case TCG_TYPE_V256: 1225 /* 1226 * The gvec infrastructure only requires 16-byte alignment, 1227 * so here we must use an unaligned load. 1228 */ 1229 tcg_debug_assert(ret >= 16); 1230 tcg_out_vex_modrm_offset(s, OPC_MOVDQU_VxWx | P_VEXL, 1231 ret, 0, arg1, arg2); 1232 break; 1233 default: 1234 g_assert_not_reached(); 1235 } 1236} 1237 1238static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg, 1239 TCGReg arg1, intptr_t arg2) 1240{ 1241 switch (type) { 1242 case TCG_TYPE_I32: 1243 if (arg < 16) { 1244 tcg_out_modrm_offset(s, OPC_MOVL_EvGv, arg, arg1, arg2); 1245 } else { 1246 tcg_out_vex_modrm_offset(s, OPC_MOVD_EyVy, arg, 0, arg1, arg2); 1247 } 1248 break; 1249 case TCG_TYPE_I64: 1250 if (arg < 16) { 1251 tcg_out_modrm_offset(s, OPC_MOVL_EvGv | P_REXW, arg, arg1, arg2); 1252 break; 1253 } 1254 /* FALLTHRU */ 1255 case TCG_TYPE_V64: 1256 /* There is no instruction that can validate 8-byte alignment. */ 1257 tcg_debug_assert(arg >= 16); 1258 tcg_out_vex_modrm_offset(s, OPC_MOVQ_WqVq, arg, 0, arg1, arg2); 1259 break; 1260 case TCG_TYPE_V128: 1261 /* 1262 * The gvec infrastructure is asserts that v128 vector loads 1263 * and stores use a 16-byte aligned offset. Validate that the 1264 * final pointer is aligned by using an insn that will SIGSEGV. 1265 * 1266 * This specific instance is also used by TCG_CALL_RET_BY_VEC, 1267 * for _WIN64, which must have SSE2 but may not have AVX. 1268 */ 1269 tcg_debug_assert(arg >= 16); 1270 if (have_avx1) { 1271 tcg_out_vex_modrm_offset(s, OPC_MOVDQA_WxVx, arg, 0, arg1, arg2); 1272 } else { 1273 tcg_out_modrm_offset(s, OPC_MOVDQA_WxVx, arg, arg1, arg2); 1274 } 1275 break; 1276 case TCG_TYPE_V256: 1277 /* 1278 * The gvec infrastructure only requires 16-byte alignment, 1279 * so here we must use an unaligned store. 1280 */ 1281 tcg_debug_assert(arg >= 16); 1282 tcg_out_vex_modrm_offset(s, OPC_MOVDQU_WxVx | P_VEXL, 1283 arg, 0, arg1, arg2); 1284 break; 1285 default: 1286 g_assert_not_reached(); 1287 } 1288} 1289 1290static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val, 1291 TCGReg base, intptr_t ofs) 1292{ 1293 int rexw = 0; 1294 if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I64) { 1295 if (val != (int32_t)val) { 1296 return false; 1297 } 1298 rexw = P_REXW; 1299 } else if (type != TCG_TYPE_I32) { 1300 return false; 1301 } 1302 tcg_out_modrm_offset(s, OPC_MOVL_EvIz | rexw, 0, base, ofs); 1303 tcg_out32(s, val); 1304 return true; 1305} 1306 1307static void tcg_out_shifti(TCGContext *s, int subopc, int reg, int count) 1308{ 1309 /* Propagate an opcode prefix, such as P_DATA16. */ 1310 int ext = subopc & ~0x7; 1311 subopc &= 0x7; 1312 1313 if (count == 1) { 1314 tcg_out_modrm(s, OPC_SHIFT_1 + ext, subopc, reg); 1315 } else { 1316 tcg_out_modrm(s, OPC_SHIFT_Ib + ext, subopc, reg); 1317 tcg_out8(s, count); 1318 } 1319} 1320 1321static inline void tcg_out_bswap32(TCGContext *s, int reg) 1322{ 1323 tcg_out_opc(s, OPC_BSWAP + LOWREGMASK(reg), 0, reg, 0); 1324} 1325 1326static inline void tcg_out_rolw_8(TCGContext *s, int reg) 1327{ 1328 tcg_out_shifti(s, SHIFT_ROL + P_DATA16, reg, 8); 1329} 1330 1331static void tcg_out_ext8u(TCGContext *s, TCGReg dest, TCGReg src) 1332{ 1333 if (TCG_TARGET_REG_BITS == 32 && src >= 4) { 1334 tcg_out_mov(s, TCG_TYPE_I32, dest, src); 1335 if (dest >= 4) { 1336 tcg_out_modrm(s, OPC_ARITH_EvIz, ARITH_AND, dest); 1337 tcg_out32(s, 0xff); 1338 return; 1339 } 1340 src = dest; 1341 } 1342 tcg_out_modrm(s, OPC_MOVZBL + P_REXB_RM, dest, src); 1343} 1344 1345static void tcg_out_ext8s(TCGContext *s, TCGType type, TCGReg dest, TCGReg src) 1346{ 1347 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 1348 1349 if (TCG_TARGET_REG_BITS == 32 && src >= 4) { 1350 tcg_out_mov(s, TCG_TYPE_I32, dest, src); 1351 if (dest >= 4) { 1352 tcg_out_shifti(s, SHIFT_SHL, dest, 24); 1353 tcg_out_shifti(s, SHIFT_SAR, dest, 24); 1354 return; 1355 } 1356 src = dest; 1357 } 1358 tcg_out_modrm(s, OPC_MOVSBL + P_REXB_RM + rexw, dest, src); 1359} 1360 1361static void tcg_out_ext16u(TCGContext *s, TCGReg dest, TCGReg src) 1362{ 1363 /* movzwl */ 1364 tcg_out_modrm(s, OPC_MOVZWL, dest, src); 1365} 1366 1367static void tcg_out_ext16s(TCGContext *s, TCGType type, TCGReg dest, TCGReg src) 1368{ 1369 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 1370 /* movsw[lq] */ 1371 tcg_out_modrm(s, OPC_MOVSWL + rexw, dest, src); 1372} 1373 1374static void tcg_out_ext32u(TCGContext *s, TCGReg dest, TCGReg src) 1375{ 1376 /* 32-bit mov zero extends. */ 1377 tcg_out_modrm(s, OPC_MOVL_GvEv, dest, src); 1378} 1379 1380static void tcg_out_ext32s(TCGContext *s, TCGReg dest, TCGReg src) 1381{ 1382 tcg_debug_assert(TCG_TARGET_REG_BITS == 64); 1383 tcg_out_modrm(s, OPC_MOVSLQ, dest, src); 1384} 1385 1386static void tcg_out_exts_i32_i64(TCGContext *s, TCGReg dest, TCGReg src) 1387{ 1388 tcg_out_ext32s(s, dest, src); 1389} 1390 1391static void tcg_out_extu_i32_i64(TCGContext *s, TCGReg dest, TCGReg src) 1392{ 1393 if (dest != src) { 1394 tcg_out_ext32u(s, dest, src); 1395 } 1396} 1397 1398static void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg dest, TCGReg src) 1399{ 1400 tcg_out_ext32u(s, dest, src); 1401} 1402 1403static inline void tcg_out_bswap64(TCGContext *s, int reg) 1404{ 1405 tcg_out_opc(s, OPC_BSWAP + P_REXW + LOWREGMASK(reg), 0, reg, 0); 1406} 1407 1408static void tgen_arithi(TCGContext *s, int c, int r0, 1409 tcg_target_long val, int cf) 1410{ 1411 int rexw = 0; 1412 1413 if (TCG_TARGET_REG_BITS == 64) { 1414 rexw = c & -8; 1415 c &= 7; 1416 } 1417 1418 switch (c) { 1419 case ARITH_ADD: 1420 case ARITH_SUB: 1421 if (!cf) { 1422 /* 1423 * ??? While INC is 2 bytes shorter than ADDL $1, they also induce 1424 * partial flags update stalls on Pentium4 and are not recommended 1425 * by current Intel optimization manuals. 1426 */ 1427 if (val == 1 || val == -1) { 1428 int is_inc = (c == ARITH_ADD) ^ (val < 0); 1429 if (TCG_TARGET_REG_BITS == 64) { 1430 /* 1431 * The single-byte increment encodings are re-tasked 1432 * as the REX prefixes. Use the MODRM encoding. 1433 */ 1434 tcg_out_modrm(s, OPC_GRP5 + rexw, 1435 (is_inc ? EXT5_INC_Ev : EXT5_DEC_Ev), r0); 1436 } else { 1437 tcg_out8(s, (is_inc ? OPC_INC_r32 : OPC_DEC_r32) + r0); 1438 } 1439 return; 1440 } 1441 if (val == 128) { 1442 /* 1443 * Facilitate using an 8-bit immediate. Carry is inverted 1444 * by this transformation, so do it only if cf == 0. 1445 */ 1446 c ^= ARITH_ADD ^ ARITH_SUB; 1447 val = -128; 1448 } 1449 } 1450 break; 1451 1452 case ARITH_AND: 1453 if (TCG_TARGET_REG_BITS == 64) { 1454 if (val == 0xffffffffu) { 1455 tcg_out_ext32u(s, r0, r0); 1456 return; 1457 } 1458 if (val == (uint32_t)val) { 1459 /* AND with no high bits set can use a 32-bit operation. */ 1460 rexw = 0; 1461 } 1462 } 1463 if (val == 0xffu && (r0 < 4 || TCG_TARGET_REG_BITS == 64)) { 1464 tcg_out_ext8u(s, r0, r0); 1465 return; 1466 } 1467 if (val == 0xffffu) { 1468 tcg_out_ext16u(s, r0, r0); 1469 return; 1470 } 1471 break; 1472 1473 case ARITH_OR: 1474 case ARITH_XOR: 1475 if (val >= 0x80 && val <= 0xff 1476 && (r0 < 4 || TCG_TARGET_REG_BITS == 64)) { 1477 tcg_out_modrm(s, OPC_ARITH_EbIb + P_REXB_RM, c, r0); 1478 tcg_out8(s, val); 1479 return; 1480 } 1481 break; 1482 } 1483 1484 if (val == (int8_t)val) { 1485 tcg_out_modrm(s, OPC_ARITH_EvIb + rexw, c, r0); 1486 tcg_out8(s, val); 1487 return; 1488 } 1489 if (rexw == 0 || val == (int32_t)val) { 1490 tcg_out_modrm(s, OPC_ARITH_EvIz + rexw, c, r0); 1491 tcg_out32(s, val); 1492 return; 1493 } 1494 1495 g_assert_not_reached(); 1496} 1497 1498static void tcg_out_addi(TCGContext *s, int reg, tcg_target_long val) 1499{ 1500 if (val != 0) { 1501 tgen_arithi(s, ARITH_ADD + P_REXW, reg, val, 0); 1502 } 1503} 1504 1505/* Set SMALL to force a short forward branch. */ 1506static void tcg_out_jxx(TCGContext *s, int opc, TCGLabel *l, bool small) 1507{ 1508 int32_t val, val1; 1509 1510 if (l->has_value) { 1511 val = tcg_pcrel_diff(s, l->u.value_ptr); 1512 val1 = val - 2; 1513 if ((int8_t)val1 == val1) { 1514 if (opc == -1) { 1515 tcg_out8(s, OPC_JMP_short); 1516 } else { 1517 tcg_out8(s, OPC_JCC_short + opc); 1518 } 1519 tcg_out8(s, val1); 1520 } else { 1521 tcg_debug_assert(!small); 1522 if (opc == -1) { 1523 tcg_out8(s, OPC_JMP_long); 1524 tcg_out32(s, val - 5); 1525 } else { 1526 tcg_out_opc(s, OPC_JCC_long + opc, 0, 0, 0); 1527 tcg_out32(s, val - 6); 1528 } 1529 } 1530 } else if (small) { 1531 if (opc == -1) { 1532 tcg_out8(s, OPC_JMP_short); 1533 } else { 1534 tcg_out8(s, OPC_JCC_short + opc); 1535 } 1536 tcg_out_reloc(s, s->code_ptr, R_386_PC8, l, -1); 1537 s->code_ptr += 1; 1538 } else { 1539 if (opc == -1) { 1540 tcg_out8(s, OPC_JMP_long); 1541 } else { 1542 tcg_out_opc(s, OPC_JCC_long + opc, 0, 0, 0); 1543 } 1544 tcg_out_reloc(s, s->code_ptr, R_386_PC32, l, -4); 1545 s->code_ptr += 4; 1546 } 1547} 1548 1549static void tcg_out_br(TCGContext *s, TCGLabel *l) 1550{ 1551 tcg_out_jxx(s, JCC_JMP, l, 0); 1552} 1553 1554static int tcg_out_cmp(TCGContext *s, TCGCond cond, TCGArg arg1, 1555 TCGArg arg2, int const_arg2, int rexw) 1556{ 1557 int jz, js; 1558 1559 if (!is_tst_cond(cond)) { 1560 if (!const_arg2) { 1561 tgen_arithr(s, ARITH_CMP + rexw, arg1, arg2); 1562 } else if (arg2 == 0) { 1563 tcg_out_modrm(s, OPC_TESTL + rexw, arg1, arg1); 1564 } else { 1565 tcg_debug_assert(!rexw || arg2 == (int32_t)arg2); 1566 tgen_arithi(s, ARITH_CMP + rexw, arg1, arg2, 0); 1567 } 1568 return tcg_cond_to_jcc[cond]; 1569 } 1570 1571 jz = tcg_cond_to_jcc[cond]; 1572 js = (cond == TCG_COND_TSTNE ? JCC_JS : JCC_JNS); 1573 1574 if (!const_arg2) { 1575 tcg_out_modrm(s, OPC_TESTL + rexw, arg1, arg2); 1576 return jz; 1577 } 1578 1579 if (arg2 <= 0xff && (TCG_TARGET_REG_BITS == 64 || arg1 < 4)) { 1580 if (arg2 == 0x80) { 1581 tcg_out_modrm(s, OPC_TESTB | P_REXB_R, arg1, arg1); 1582 return js; 1583 } 1584 if (arg2 == 0xff) { 1585 tcg_out_modrm(s, OPC_TESTB | P_REXB_R, arg1, arg1); 1586 return jz; 1587 } 1588 tcg_out_modrm(s, OPC_GRP3_Eb | P_REXB_RM, EXT3_TESTi, arg1); 1589 tcg_out8(s, arg2); 1590 return jz; 1591 } 1592 1593 if ((arg2 & ~0xff00) == 0 && arg1 < 4) { 1594 if (arg2 == 0x8000) { 1595 tcg_out_modrm(s, OPC_TESTB, arg1 + 4, arg1 + 4); 1596 return js; 1597 } 1598 if (arg2 == 0xff00) { 1599 tcg_out_modrm(s, OPC_TESTB, arg1 + 4, arg1 + 4); 1600 return jz; 1601 } 1602 tcg_out_modrm(s, OPC_GRP3_Eb, EXT3_TESTi, arg1 + 4); 1603 tcg_out8(s, arg2 >> 8); 1604 return jz; 1605 } 1606 1607 if (arg2 == 0xffff) { 1608 tcg_out_modrm(s, OPC_TESTL | P_DATA16, arg1, arg1); 1609 return jz; 1610 } 1611 if (arg2 == 0xffffffffu) { 1612 tcg_out_modrm(s, OPC_TESTL, arg1, arg1); 1613 return jz; 1614 } 1615 1616 if (is_power_of_2(rexw ? arg2 : (uint32_t)arg2)) { 1617 int jc = (cond == TCG_COND_TSTNE ? JCC_JB : JCC_JAE); 1618 int sh = ctz64(arg2); 1619 1620 rexw = (sh & 32 ? P_REXW : 0); 1621 if ((sh & 31) == 31) { 1622 tcg_out_modrm(s, OPC_TESTL | rexw, arg1, arg1); 1623 return js; 1624 } else { 1625 tcg_out_modrm(s, OPC_GRPBT | rexw, OPC_GRPBT_BT, arg1); 1626 tcg_out8(s, sh); 1627 return jc; 1628 } 1629 } 1630 1631 if (rexw) { 1632 if (arg2 == (uint32_t)arg2) { 1633 rexw = 0; 1634 } else { 1635 tcg_debug_assert(arg2 == (int32_t)arg2); 1636 } 1637 } 1638 tcg_out_modrm(s, OPC_GRP3_Ev + rexw, EXT3_TESTi, arg1); 1639 tcg_out32(s, arg2); 1640 return jz; 1641} 1642 1643static void tcg_out_brcond(TCGContext *s, int rexw, TCGCond cond, 1644 TCGArg arg1, TCGArg arg2, int const_arg2, 1645 TCGLabel *label, bool small) 1646{ 1647 int jcc = tcg_out_cmp(s, cond, arg1, arg2, const_arg2, rexw); 1648 tcg_out_jxx(s, jcc, label, small); 1649} 1650 1651static void tgen_brcond(TCGContext *s, TCGType type, TCGCond cond, 1652 TCGReg arg1, TCGReg arg2, TCGLabel *label) 1653{ 1654 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 1655 tcg_out_brcond(s, rexw, cond, arg1, arg2, false, label, false); 1656} 1657 1658static void tgen_brcondi(TCGContext *s, TCGType type, TCGCond cond, 1659 TCGReg arg1, tcg_target_long arg2, TCGLabel *label) 1660{ 1661 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 1662 tcg_out_brcond(s, rexw, cond, arg1, arg2, true, label, false); 1663} 1664 1665static const TCGOutOpBrcond outop_brcond = { 1666 .base.static_constraint = C_O0_I2(r, reT), 1667 .out_rr = tgen_brcond, 1668 .out_ri = tgen_brcondi, 1669}; 1670 1671static void tcg_out_brcond2(TCGContext *s, TCGCond cond, TCGReg al, 1672 TCGReg ah, TCGArg bl, bool blconst, 1673 TCGArg bh, bool bhconst, 1674 TCGLabel *label_this, bool small) 1675{ 1676 TCGLabel *label_next = gen_new_label(); 1677 1678 switch (cond) { 1679 case TCG_COND_EQ: 1680 case TCG_COND_TSTEQ: 1681 tcg_out_brcond(s, 0, tcg_invert_cond(cond), 1682 al, bl, blconst, label_next, true); 1683 tcg_out_brcond(s, 0, cond, ah, bh, bhconst, label_this, small); 1684 break; 1685 1686 case TCG_COND_NE: 1687 case TCG_COND_TSTNE: 1688 tcg_out_brcond(s, 0, cond, al, bl, blconst, label_this, small); 1689 tcg_out_brcond(s, 0, cond, ah, bh, bhconst, label_this, small); 1690 break; 1691 1692 default: 1693 tcg_out_brcond(s, 0, tcg_high_cond(cond), 1694 ah, bh, bhconst, label_this, small); 1695 tcg_out_jxx(s, JCC_JNE, label_next, 1); 1696 tcg_out_brcond(s, 0, tcg_unsigned_cond(cond), 1697 al, bl, blconst, label_this, small); 1698 break; 1699 } 1700 tcg_out_label(s, label_next); 1701} 1702 1703static void tgen_brcond2(TCGContext *s, TCGCond cond, TCGReg al, 1704 TCGReg ah, TCGArg bl, bool blconst, 1705 TCGArg bh, bool bhconst, TCGLabel *l) 1706{ 1707 tcg_out_brcond2(s, cond, al, ah, bl, blconst, bh, bhconst, l, false); 1708} 1709 1710#if TCG_TARGET_REG_BITS != 32 1711__attribute__((unused)) 1712#endif 1713static const TCGOutOpBrcond2 outop_brcond2 = { 1714 .base.static_constraint = C_O0_I4(r, r, ri, ri), 1715 .out = tgen_brcond2, 1716}; 1717 1718static void tcg_out_setcond(TCGContext *s, TCGType type, TCGCond cond, 1719 TCGReg dest, TCGReg arg1, TCGArg arg2, 1720 bool const_arg2, bool neg) 1721{ 1722 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 1723 int cmp_rexw = rexw; 1724 bool inv = false; 1725 bool cleared; 1726 int jcc; 1727 1728 switch (cond) { 1729 case TCG_COND_NE: 1730 inv = true; 1731 /* fall through */ 1732 case TCG_COND_EQ: 1733 /* If arg2 is 0, convert to LTU/GEU vs 1. */ 1734 if (const_arg2 && arg2 == 0) { 1735 arg2 = 1; 1736 goto do_ltu; 1737 } 1738 break; 1739 1740 case TCG_COND_TSTNE: 1741 inv = true; 1742 /* fall through */ 1743 case TCG_COND_TSTEQ: 1744 /* If arg2 is -1, convert to LTU/GEU vs 1. */ 1745 if (const_arg2 && arg2 == 0xffffffffu) { 1746 arg2 = 1; 1747 cmp_rexw = 0; 1748 goto do_ltu; 1749 } 1750 break; 1751 1752 case TCG_COND_LEU: 1753 inv = true; 1754 /* fall through */ 1755 case TCG_COND_GTU: 1756 /* If arg2 is a register, swap for LTU/GEU. */ 1757 if (!const_arg2) { 1758 TCGReg t = arg1; 1759 arg1 = arg2; 1760 arg2 = t; 1761 goto do_ltu; 1762 } 1763 break; 1764 1765 case TCG_COND_GEU: 1766 inv = true; 1767 /* fall through */ 1768 case TCG_COND_LTU: 1769 do_ltu: 1770 /* 1771 * Relying on the carry bit, use SBB to produce -1 if LTU, 0 if GEU. 1772 * We can then use NEG or INC to produce the desired result. 1773 * This is always smaller than the SETCC expansion. 1774 */ 1775 tcg_out_cmp(s, TCG_COND_LTU, arg1, arg2, const_arg2, cmp_rexw); 1776 1777 /* X - X - C = -C = (C ? -1 : 0) */ 1778 tgen_arithr(s, ARITH_SBB + (neg ? rexw : 0), dest, dest); 1779 if (inv && neg) { 1780 /* ~(C ? -1 : 0) = (C ? 0 : -1) */ 1781 tcg_out_modrm(s, OPC_GRP3_Ev + rexw, EXT3_NOT, dest); 1782 } else if (inv) { 1783 /* (C ? -1 : 0) + 1 = (C ? 0 : 1) */ 1784 tgen_arithi(s, ARITH_ADD, dest, 1, 0); 1785 } else if (!neg) { 1786 /* -(C ? -1 : 0) = (C ? 1 : 0) */ 1787 tcg_out_modrm(s, OPC_GRP3_Ev, EXT3_NEG, dest); 1788 } 1789 return; 1790 1791 case TCG_COND_GE: 1792 inv = true; 1793 /* fall through */ 1794 case TCG_COND_LT: 1795 /* If arg2 is 0, extract the sign bit. */ 1796 if (const_arg2 && arg2 == 0) { 1797 tcg_out_mov(s, type, dest, arg1); 1798 if (inv) { 1799 tcg_out_modrm(s, OPC_GRP3_Ev + rexw, EXT3_NOT, dest); 1800 } 1801 tcg_out_shifti(s, (neg ? SHIFT_SAR : SHIFT_SHR) + rexw, 1802 dest, rexw ? 63 : 31); 1803 return; 1804 } 1805 break; 1806 1807 default: 1808 break; 1809 } 1810 1811 /* 1812 * If dest does not overlap the inputs, clearing it first is preferred. 1813 * The XOR breaks any false dependency for the low-byte write to dest, 1814 * and is also one byte smaller than MOVZBL. 1815 */ 1816 cleared = false; 1817 if (dest != arg1 && (const_arg2 || dest != arg2)) { 1818 tgen_arithr(s, ARITH_XOR, dest, dest); 1819 cleared = true; 1820 } 1821 1822 jcc = tcg_out_cmp(s, cond, arg1, arg2, const_arg2, cmp_rexw); 1823 tcg_out_modrm(s, OPC_SETCC | jcc, 0, dest); 1824 1825 if (!cleared) { 1826 tcg_out_ext8u(s, dest, dest); 1827 } 1828 if (neg) { 1829 tcg_out_modrm(s, OPC_GRP3_Ev + rexw, EXT3_NEG, dest); 1830 } 1831} 1832 1833static void tgen_setcond(TCGContext *s, TCGType type, TCGCond cond, 1834 TCGReg dest, TCGReg arg1, TCGReg arg2) 1835{ 1836 tcg_out_setcond(s, type, cond, dest, arg1, arg2, false, false); 1837} 1838 1839static void tgen_setcondi(TCGContext *s, TCGType type, TCGCond cond, 1840 TCGReg dest, TCGReg arg1, tcg_target_long arg2) 1841{ 1842 tcg_out_setcond(s, type, cond, dest, arg1, arg2, true, false); 1843} 1844 1845static const TCGOutOpSetcond outop_setcond = { 1846 .base.static_constraint = C_O1_I2(q, r, reT), 1847 .out_rrr = tgen_setcond, 1848 .out_rri = tgen_setcondi, 1849}; 1850 1851static void tgen_negsetcond(TCGContext *s, TCGType type, TCGCond cond, 1852 TCGReg dest, TCGReg arg1, TCGReg arg2) 1853{ 1854 tcg_out_setcond(s, type, cond, dest, arg1, arg2, false, true); 1855} 1856 1857static void tgen_negsetcondi(TCGContext *s, TCGType type, TCGCond cond, 1858 TCGReg dest, TCGReg arg1, tcg_target_long arg2) 1859{ 1860 tcg_out_setcond(s, type, cond, dest, arg1, arg2, true, true); 1861} 1862 1863static const TCGOutOpSetcond outop_negsetcond = { 1864 .base.static_constraint = C_O1_I2(q, r, reT), 1865 .out_rrr = tgen_negsetcond, 1866 .out_rri = tgen_negsetcondi, 1867}; 1868 1869static void tgen_setcond2(TCGContext *s, TCGCond cond, TCGReg ret, 1870 TCGReg al, TCGReg ah, 1871 TCGArg bl, bool const_bl, 1872 TCGArg bh, bool const_bh) 1873{ 1874 TCGLabel *label_over = gen_new_label(); 1875 1876 if (ret == al || ret == ah 1877 || (!const_bl && ret == bl) 1878 || (!const_bh && ret == bh)) { 1879 /* 1880 * When the destination overlaps with one of the argument 1881 * registers, don't do anything tricky. 1882 */ 1883 TCGLabel *label_true = gen_new_label(); 1884 1885 tcg_out_brcond2(s, cond, al, ah, bl, const_bl, 1886 bh, const_bh, label_true, true); 1887 1888 tcg_out_movi(s, TCG_TYPE_I32, ret, 0); 1889 tcg_out_jxx(s, JCC_JMP, label_over, 1); 1890 tcg_out_label(s, label_true); 1891 1892 tcg_out_movi(s, TCG_TYPE_I32, ret, 1); 1893 } else { 1894 /* 1895 * When the destination does not overlap one of the arguments, 1896 * clear the destination first, jump if cond false, and emit an 1897 * increment in the true case. This results in smaller code. 1898 */ 1899 tcg_out_movi(s, TCG_TYPE_I32, ret, 0); 1900 1901 tcg_out_brcond2(s, tcg_invert_cond(cond), al, ah, bl, const_bl, 1902 bh, const_bh, label_over, true); 1903 1904 tgen_arithi(s, ARITH_ADD, ret, 1, 0); 1905 } 1906 tcg_out_label(s, label_over); 1907} 1908 1909#if TCG_TARGET_REG_BITS != 32 1910__attribute__((unused)) 1911#endif 1912static const TCGOutOpSetcond2 outop_setcond2 = { 1913 .base.static_constraint = C_O1_I4(r, r, r, ri, ri), 1914 .out = tgen_setcond2, 1915}; 1916 1917static void tcg_out_cmov(TCGContext *s, int jcc, int rexw, 1918 TCGReg dest, TCGReg v1) 1919{ 1920 tcg_out_modrm(s, OPC_CMOVCC | jcc | rexw, dest, v1); 1921} 1922 1923static void tgen_movcond(TCGContext *s, TCGType type, TCGCond cond, 1924 TCGReg dest, TCGReg c1, TCGArg c2, bool const_c2, 1925 TCGArg vt, bool const_vt, 1926 TCGArg vf, bool consf_vf) 1927{ 1928 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 1929 int jcc = tcg_out_cmp(s, cond, c1, c2, const_c2, rexw); 1930 tcg_out_cmov(s, jcc, rexw, dest, vt); 1931} 1932 1933static const TCGOutOpMovcond outop_movcond = { 1934 .base.static_constraint = C_O1_I4(r, r, reT, r, 0), 1935 .out = tgen_movcond, 1936}; 1937 1938static void tcg_out_branch(TCGContext *s, int call, const tcg_insn_unit *dest) 1939{ 1940 intptr_t disp = tcg_pcrel_diff(s, dest) - 5; 1941 1942 if (disp == (int32_t)disp) { 1943 tcg_out_opc(s, call ? OPC_CALL_Jz : OPC_JMP_long, 0, 0, 0); 1944 tcg_out32(s, disp); 1945 } else { 1946 /* rip-relative addressing into the constant pool. 1947 This is 6 + 8 = 14 bytes, as compared to using an 1948 immediate load 10 + 6 = 16 bytes, plus we may 1949 be able to re-use the pool constant for more calls. */ 1950 tcg_out_opc(s, OPC_GRP5, 0, 0, 0); 1951 tcg_out8(s, (call ? EXT5_CALLN_Ev : EXT5_JMPN_Ev) << 3 | 5); 1952 new_pool_label(s, (uintptr_t)dest, R_386_PC32, s->code_ptr, -4); 1953 tcg_out32(s, 0); 1954 } 1955} 1956 1957static void tcg_out_call(TCGContext *s, const tcg_insn_unit *dest, 1958 const TCGHelperInfo *info) 1959{ 1960 tcg_out_branch(s, 1, dest); 1961 1962#ifndef _WIN32 1963 if (TCG_TARGET_REG_BITS == 32 && info->out_kind == TCG_CALL_RET_BY_REF) { 1964 /* 1965 * The sysv i386 abi for struct return places a reference as the 1966 * first argument of the stack, and pops that argument with the 1967 * return statement. Since we want to retain the aligned stack 1968 * pointer for the callee, we do not want to actually push that 1969 * argument before the call but rely on the normal store to the 1970 * stack slot. But we do need to compensate for the pop in order 1971 * to reset our correct stack pointer value. 1972 * Pushing a garbage value back onto the stack is quickest. 1973 */ 1974 tcg_out_push(s, TCG_REG_EAX); 1975 } 1976#endif 1977} 1978 1979static void tcg_out_jmp(TCGContext *s, const tcg_insn_unit *dest) 1980{ 1981 tcg_out_branch(s, 0, dest); 1982} 1983 1984static void tcg_out_nopn(TCGContext *s, int n) 1985{ 1986 int i; 1987 /* Emit 1 or 2 operand size prefixes for the standard one byte nop, 1988 * "xchg %eax,%eax", forming "xchg %ax,%ax". All cores accept the 1989 * duplicate prefix, and all of the interesting recent cores can 1990 * decode and discard the duplicates in a single cycle. 1991 */ 1992 tcg_debug_assert(n >= 1); 1993 for (i = 1; i < n; ++i) { 1994 tcg_out8(s, 0x66); 1995 } 1996 tcg_out8(s, 0x90); 1997} 1998 1999typedef struct { 2000 TCGReg base; 2001 int index; 2002 int ofs; 2003 int seg; 2004 TCGAtomAlign aa; 2005} HostAddress; 2006 2007bool tcg_target_has_memory_bswap(MemOp memop) 2008{ 2009 TCGAtomAlign aa; 2010 2011 if (!have_movbe) { 2012 return false; 2013 } 2014 if ((memop & MO_SIZE) < MO_128) { 2015 return true; 2016 } 2017 2018 /* 2019 * Reject 16-byte memop with 16-byte atomicity, i.e. VMOVDQA, 2020 * but do allow a pair of 64-bit operations, i.e. MOVBEQ. 2021 */ 2022 aa = atom_and_align_for_opc(tcg_ctx, memop, MO_ATOM_IFALIGN, true); 2023 return aa.atom < MO_128; 2024} 2025 2026/* 2027 * Because i686 has no register parameters and because x86_64 has xchg 2028 * to handle addr/data register overlap, we have placed all input arguments 2029 * before we need might need a scratch reg. 2030 * 2031 * Even then, a scratch is only needed for l->raddr. Rather than expose 2032 * a general-purpose scratch when we don't actually know it's available, 2033 * use the ra_gen hook to load into RAX if needed. 2034 */ 2035#if TCG_TARGET_REG_BITS == 64 2036static TCGReg ldst_ra_gen(TCGContext *s, const TCGLabelQemuLdst *l, int arg) 2037{ 2038 if (arg < 0) { 2039 arg = TCG_REG_RAX; 2040 } 2041 tcg_out_movi(s, TCG_TYPE_PTR, arg, (uintptr_t)l->raddr); 2042 return arg; 2043} 2044static const TCGLdstHelperParam ldst_helper_param = { 2045 .ra_gen = ldst_ra_gen 2046}; 2047#else 2048static const TCGLdstHelperParam ldst_helper_param = { }; 2049#endif 2050 2051static void tcg_out_vec_to_pair(TCGContext *s, TCGType type, 2052 TCGReg l, TCGReg h, TCGReg v) 2053{ 2054 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 2055 2056 /* vpmov{d,q} %v, %l */ 2057 tcg_out_vex_modrm(s, OPC_MOVD_EyVy + rexw, v, 0, l); 2058 /* vpextr{d,q} $1, %v, %h */ 2059 tcg_out_vex_modrm(s, OPC_PEXTRD + rexw, v, 0, h); 2060 tcg_out8(s, 1); 2061} 2062 2063static void tcg_out_pair_to_vec(TCGContext *s, TCGType type, 2064 TCGReg v, TCGReg l, TCGReg h) 2065{ 2066 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 2067 2068 /* vmov{d,q} %l, %v */ 2069 tcg_out_vex_modrm(s, OPC_MOVD_VyEy + rexw, v, 0, l); 2070 /* vpinsr{d,q} $1, %h, %v, %v */ 2071 tcg_out_vex_modrm(s, OPC_PINSRD + rexw, v, v, h); 2072 tcg_out8(s, 1); 2073} 2074 2075/* 2076 * Generate code for the slow path for a load at the end of block 2077 */ 2078static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l) 2079{ 2080 MemOp opc = get_memop(l->oi); 2081 tcg_insn_unit **label_ptr = &l->label_ptr[0]; 2082 2083 /* resolve label address */ 2084 tcg_patch32(label_ptr[0], s->code_ptr - label_ptr[0] - 4); 2085 if (label_ptr[1]) { 2086 tcg_patch32(label_ptr[1], s->code_ptr - label_ptr[1] - 4); 2087 } 2088 2089 tcg_out_ld_helper_args(s, l, &ldst_helper_param); 2090 tcg_out_branch(s, 1, qemu_ld_helpers[opc & MO_SIZE]); 2091 tcg_out_ld_helper_ret(s, l, false, &ldst_helper_param); 2092 2093 tcg_out_jmp(s, l->raddr); 2094 return true; 2095} 2096 2097/* 2098 * Generate code for the slow path for a store at the end of block 2099 */ 2100static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l) 2101{ 2102 MemOp opc = get_memop(l->oi); 2103 tcg_insn_unit **label_ptr = &l->label_ptr[0]; 2104 2105 /* resolve label address */ 2106 tcg_patch32(label_ptr[0], s->code_ptr - label_ptr[0] - 4); 2107 if (label_ptr[1]) { 2108 tcg_patch32(label_ptr[1], s->code_ptr - label_ptr[1] - 4); 2109 } 2110 2111 tcg_out_st_helper_args(s, l, &ldst_helper_param); 2112 tcg_out_branch(s, 1, qemu_st_helpers[opc & MO_SIZE]); 2113 2114 tcg_out_jmp(s, l->raddr); 2115 return true; 2116} 2117 2118#ifdef CONFIG_USER_ONLY 2119static HostAddress x86_guest_base = { 2120 .index = -1 2121}; 2122 2123#if defined(__x86_64__) && defined(__linux__) 2124# include <asm/prctl.h> 2125# include <sys/prctl.h> 2126int arch_prctl(int code, unsigned long addr); 2127static inline int setup_guest_base_seg(void) 2128{ 2129 if (arch_prctl(ARCH_SET_GS, guest_base) == 0) { 2130 return P_GS; 2131 } 2132 return 0; 2133} 2134#define setup_guest_base_seg setup_guest_base_seg 2135#elif defined(__x86_64__) && \ 2136 (defined (__FreeBSD__) || defined (__FreeBSD_kernel__)) 2137# include <machine/sysarch.h> 2138static inline int setup_guest_base_seg(void) 2139{ 2140 if (sysarch(AMD64_SET_GSBASE, &guest_base) == 0) { 2141 return P_GS; 2142 } 2143 return 0; 2144} 2145#define setup_guest_base_seg setup_guest_base_seg 2146#endif 2147#else 2148# define x86_guest_base (*(HostAddress *)({ qemu_build_not_reached(); NULL; })) 2149#endif /* CONFIG_USER_ONLY */ 2150#ifndef setup_guest_base_seg 2151# define setup_guest_base_seg() 0 2152#endif 2153 2154#define MIN_TLB_MASK_TABLE_OFS INT_MIN 2155 2156/* 2157 * For softmmu, perform the TLB load and compare. 2158 * For useronly, perform any required alignment tests. 2159 * In both cases, return a TCGLabelQemuLdst structure if the slow path 2160 * is required and fill in @h with the host address for the fast path. 2161 */ 2162static TCGLabelQemuLdst *prepare_host_addr(TCGContext *s, HostAddress *h, 2163 TCGReg addr, MemOpIdx oi, bool is_ld) 2164{ 2165 TCGLabelQemuLdst *ldst = NULL; 2166 MemOp opc = get_memop(oi); 2167 MemOp s_bits = opc & MO_SIZE; 2168 unsigned a_mask; 2169 2170 if (tcg_use_softmmu) { 2171 h->index = TCG_REG_L0; 2172 h->ofs = 0; 2173 h->seg = 0; 2174 } else { 2175 *h = x86_guest_base; 2176 } 2177 h->base = addr; 2178 h->aa = atom_and_align_for_opc(s, opc, MO_ATOM_IFALIGN, s_bits == MO_128); 2179 a_mask = (1 << h->aa.align) - 1; 2180 2181 if (tcg_use_softmmu) { 2182 int cmp_ofs = is_ld ? offsetof(CPUTLBEntry, addr_read) 2183 : offsetof(CPUTLBEntry, addr_write); 2184 TCGType ttype = TCG_TYPE_I32; 2185 TCGType tlbtype = TCG_TYPE_I32; 2186 int trexw = 0, hrexw = 0, tlbrexw = 0; 2187 unsigned mem_index = get_mmuidx(oi); 2188 unsigned s_mask = (1 << s_bits) - 1; 2189 int fast_ofs = tlb_mask_table_ofs(s, mem_index); 2190 int tlb_mask; 2191 2192 ldst = new_ldst_label(s); 2193 ldst->is_ld = is_ld; 2194 ldst->oi = oi; 2195 ldst->addr_reg = addr; 2196 2197 if (TCG_TARGET_REG_BITS == 64) { 2198 ttype = s->addr_type; 2199 trexw = (ttype == TCG_TYPE_I32 ? 0 : P_REXW); 2200 if (TCG_TYPE_PTR == TCG_TYPE_I64) { 2201 hrexw = P_REXW; 2202 if (s->page_bits + s->tlb_dyn_max_bits > 32) { 2203 tlbtype = TCG_TYPE_I64; 2204 tlbrexw = P_REXW; 2205 } 2206 } 2207 } 2208 2209 tcg_out_mov(s, tlbtype, TCG_REG_L0, addr); 2210 tcg_out_shifti(s, SHIFT_SHR + tlbrexw, TCG_REG_L0, 2211 s->page_bits - CPU_TLB_ENTRY_BITS); 2212 2213 tcg_out_modrm_offset(s, OPC_AND_GvEv + trexw, TCG_REG_L0, TCG_AREG0, 2214 fast_ofs + offsetof(CPUTLBDescFast, mask)); 2215 2216 tcg_out_modrm_offset(s, OPC_ADD_GvEv + hrexw, TCG_REG_L0, TCG_AREG0, 2217 fast_ofs + offsetof(CPUTLBDescFast, table)); 2218 2219 /* 2220 * If the required alignment is at least as large as the access, 2221 * simply copy the address and mask. For lesser alignments, 2222 * check that we don't cross pages for the complete access. 2223 */ 2224 if (a_mask >= s_mask) { 2225 tcg_out_mov(s, ttype, TCG_REG_L1, addr); 2226 } else { 2227 tcg_out_modrm_offset(s, OPC_LEA + trexw, TCG_REG_L1, 2228 addr, s_mask - a_mask); 2229 } 2230 tlb_mask = s->page_mask | a_mask; 2231 tgen_arithi(s, ARITH_AND + trexw, TCG_REG_L1, tlb_mask, 0); 2232 2233 /* cmp 0(TCG_REG_L0), TCG_REG_L1 */ 2234 tcg_out_modrm_offset(s, OPC_CMP_GvEv + trexw, 2235 TCG_REG_L1, TCG_REG_L0, cmp_ofs); 2236 2237 /* jne slow_path */ 2238 tcg_out_opc(s, OPC_JCC_long + JCC_JNE, 0, 0, 0); 2239 ldst->label_ptr[0] = s->code_ptr; 2240 s->code_ptr += 4; 2241 2242 /* TLB Hit. */ 2243 tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_L0, TCG_REG_L0, 2244 offsetof(CPUTLBEntry, addend)); 2245 } else if (a_mask) { 2246 int jcc; 2247 2248 ldst = new_ldst_label(s); 2249 ldst->is_ld = is_ld; 2250 ldst->oi = oi; 2251 ldst->addr_reg = addr; 2252 2253 /* jne slow_path */ 2254 jcc = tcg_out_cmp(s, TCG_COND_TSTNE, addr, a_mask, true, false); 2255 tcg_out_opc(s, OPC_JCC_long + jcc, 0, 0, 0); 2256 ldst->label_ptr[0] = s->code_ptr; 2257 s->code_ptr += 4; 2258 } 2259 2260 return ldst; 2261} 2262 2263static void tcg_out_qemu_ld_direct(TCGContext *s, TCGReg datalo, TCGReg datahi, 2264 HostAddress h, TCGType type, MemOp memop) 2265{ 2266 bool use_movbe = false; 2267 int rexw = (type == TCG_TYPE_I32 ? 0 : P_REXW); 2268 int movop = OPC_MOVL_GvEv; 2269 2270 /* Do big-endian loads with movbe. */ 2271 if (memop & MO_BSWAP) { 2272 tcg_debug_assert(have_movbe); 2273 use_movbe = true; 2274 movop = OPC_MOVBE_GyMy; 2275 } 2276 2277 switch (memop & MO_SSIZE) { 2278 case MO_UB: 2279 tcg_out_modrm_sib_offset(s, OPC_MOVZBL + h.seg, datalo, 2280 h.base, h.index, 0, h.ofs); 2281 break; 2282 case MO_SB: 2283 tcg_out_modrm_sib_offset(s, OPC_MOVSBL + rexw + h.seg, datalo, 2284 h.base, h.index, 0, h.ofs); 2285 break; 2286 case MO_UW: 2287 if (use_movbe) { 2288 /* There is no extending movbe; only low 16-bits are modified. */ 2289 if (datalo != h.base && datalo != h.index) { 2290 /* XOR breaks dependency chains. */ 2291 tgen_arithr(s, ARITH_XOR, datalo, datalo); 2292 tcg_out_modrm_sib_offset(s, OPC_MOVBE_GyMy + P_DATA16 + h.seg, 2293 datalo, h.base, h.index, 0, h.ofs); 2294 } else { 2295 tcg_out_modrm_sib_offset(s, OPC_MOVBE_GyMy + P_DATA16 + h.seg, 2296 datalo, h.base, h.index, 0, h.ofs); 2297 tcg_out_ext16u(s, datalo, datalo); 2298 } 2299 } else { 2300 tcg_out_modrm_sib_offset(s, OPC_MOVZWL + h.seg, datalo, 2301 h.base, h.index, 0, h.ofs); 2302 } 2303 break; 2304 case MO_SW: 2305 if (use_movbe) { 2306 tcg_out_modrm_sib_offset(s, OPC_MOVBE_GyMy + P_DATA16 + h.seg, 2307 datalo, h.base, h.index, 0, h.ofs); 2308 tcg_out_ext16s(s, type, datalo, datalo); 2309 } else { 2310 tcg_out_modrm_sib_offset(s, OPC_MOVSWL + rexw + h.seg, 2311 datalo, h.base, h.index, 0, h.ofs); 2312 } 2313 break; 2314 case MO_UL: 2315 tcg_out_modrm_sib_offset(s, movop + h.seg, datalo, 2316 h.base, h.index, 0, h.ofs); 2317 break; 2318#if TCG_TARGET_REG_BITS == 64 2319 case MO_SL: 2320 if (use_movbe) { 2321 tcg_out_modrm_sib_offset(s, OPC_MOVBE_GyMy + h.seg, datalo, 2322 h.base, h.index, 0, h.ofs); 2323 tcg_out_ext32s(s, datalo, datalo); 2324 } else { 2325 tcg_out_modrm_sib_offset(s, OPC_MOVSLQ + h.seg, datalo, 2326 h.base, h.index, 0, h.ofs); 2327 } 2328 break; 2329#endif 2330 case MO_UQ: 2331 if (TCG_TARGET_REG_BITS == 64) { 2332 tcg_out_modrm_sib_offset(s, movop + P_REXW + h.seg, datalo, 2333 h.base, h.index, 0, h.ofs); 2334 break; 2335 } 2336 if (use_movbe) { 2337 TCGReg t = datalo; 2338 datalo = datahi; 2339 datahi = t; 2340 } 2341 if (h.base == datalo || h.index == datalo) { 2342 tcg_out_modrm_sib_offset(s, OPC_LEA, datahi, 2343 h.base, h.index, 0, h.ofs); 2344 tcg_out_modrm_offset(s, movop + h.seg, datalo, datahi, 0); 2345 tcg_out_modrm_offset(s, movop + h.seg, datahi, datahi, 4); 2346 } else { 2347 tcg_out_modrm_sib_offset(s, movop + h.seg, datalo, 2348 h.base, h.index, 0, h.ofs); 2349 tcg_out_modrm_sib_offset(s, movop + h.seg, datahi, 2350 h.base, h.index, 0, h.ofs + 4); 2351 } 2352 break; 2353 2354 case MO_128: 2355 tcg_debug_assert(TCG_TARGET_REG_BITS == 64); 2356 2357 /* 2358 * Without 16-byte atomicity, use integer regs. 2359 * That is where we want the data, and it allows bswaps. 2360 */ 2361 if (h.aa.atom < MO_128) { 2362 if (use_movbe) { 2363 TCGReg t = datalo; 2364 datalo = datahi; 2365 datahi = t; 2366 } 2367 if (h.base == datalo || h.index == datalo) { 2368 tcg_out_modrm_sib_offset(s, OPC_LEA + P_REXW, datahi, 2369 h.base, h.index, 0, h.ofs); 2370 tcg_out_modrm_offset(s, movop + P_REXW + h.seg, 2371 datalo, datahi, 0); 2372 tcg_out_modrm_offset(s, movop + P_REXW + h.seg, 2373 datahi, datahi, 8); 2374 } else { 2375 tcg_out_modrm_sib_offset(s, movop + P_REXW + h.seg, datalo, 2376 h.base, h.index, 0, h.ofs); 2377 tcg_out_modrm_sib_offset(s, movop + P_REXW + h.seg, datahi, 2378 h.base, h.index, 0, h.ofs + 8); 2379 } 2380 break; 2381 } 2382 2383 /* 2384 * With 16-byte atomicity, a vector load is required. 2385 * If we already have 16-byte alignment, then VMOVDQA always works. 2386 * Else if VMOVDQU has atomicity with dynamic alignment, use that. 2387 * Else use we require a runtime test for alignment for VMOVDQA; 2388 * use VMOVDQU on the unaligned nonatomic path for simplicity. 2389 */ 2390 if (h.aa.align >= MO_128) { 2391 tcg_out_vex_modrm_sib_offset(s, OPC_MOVDQA_VxWx + h.seg, 2392 TCG_TMP_VEC, 0, 2393 h.base, h.index, 0, h.ofs); 2394 } else if (cpuinfo & CPUINFO_ATOMIC_VMOVDQU) { 2395 tcg_out_vex_modrm_sib_offset(s, OPC_MOVDQU_VxWx + h.seg, 2396 TCG_TMP_VEC, 0, 2397 h.base, h.index, 0, h.ofs); 2398 } else { 2399 TCGLabel *l1 = gen_new_label(); 2400 TCGLabel *l2 = gen_new_label(); 2401 int jcc; 2402 2403 jcc = tcg_out_cmp(s, TCG_COND_TSTNE, h.base, 15, true, false); 2404 tcg_out_jxx(s, jcc, l1, true); 2405 2406 tcg_out_vex_modrm_sib_offset(s, OPC_MOVDQA_VxWx + h.seg, 2407 TCG_TMP_VEC, 0, 2408 h.base, h.index, 0, h.ofs); 2409 tcg_out_jxx(s, JCC_JMP, l2, true); 2410 2411 tcg_out_label(s, l1); 2412 tcg_out_vex_modrm_sib_offset(s, OPC_MOVDQU_VxWx + h.seg, 2413 TCG_TMP_VEC, 0, 2414 h.base, h.index, 0, h.ofs); 2415 tcg_out_label(s, l2); 2416 } 2417 tcg_out_vec_to_pair(s, TCG_TYPE_I64, datalo, datahi, TCG_TMP_VEC); 2418 break; 2419 2420 default: 2421 g_assert_not_reached(); 2422 } 2423} 2424 2425static void tgen_qemu_ld(TCGContext *s, TCGType type, TCGReg data, 2426 TCGReg addr, MemOpIdx oi) 2427{ 2428 TCGLabelQemuLdst *ldst; 2429 HostAddress h; 2430 2431 ldst = prepare_host_addr(s, &h, addr, oi, true); 2432 tcg_out_qemu_ld_direct(s, data, -1, h, type, get_memop(oi)); 2433 2434 if (ldst) { 2435 ldst->type = type; 2436 ldst->datalo_reg = data; 2437 ldst->datahi_reg = -1; 2438 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr); 2439 } 2440} 2441 2442static const TCGOutOpQemuLdSt outop_qemu_ld = { 2443 .base.static_constraint = C_O1_I1(r, L), 2444 .out = tgen_qemu_ld, 2445}; 2446 2447static void tgen_qemu_ld2(TCGContext *s, TCGType type, TCGReg datalo, 2448 TCGReg datahi, TCGReg addr, MemOpIdx oi) 2449{ 2450 TCGLabelQemuLdst *ldst; 2451 HostAddress h; 2452 2453 ldst = prepare_host_addr(s, &h, addr, oi, true); 2454 tcg_out_qemu_ld_direct(s, datalo, datahi, h, type, get_memop(oi)); 2455 2456 if (ldst) { 2457 ldst->type = type; 2458 ldst->datalo_reg = datalo; 2459 ldst->datahi_reg = datahi; 2460 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr); 2461 } 2462} 2463 2464static const TCGOutOpQemuLdSt2 outop_qemu_ld2 = { 2465 .base.static_constraint = C_O2_I1(r, r, L), 2466 .out = tgen_qemu_ld2, 2467}; 2468 2469static void tcg_out_qemu_st_direct(TCGContext *s, TCGReg datalo, TCGReg datahi, 2470 HostAddress h, MemOp memop) 2471{ 2472 bool use_movbe = false; 2473 int movop = OPC_MOVL_EvGv; 2474 2475 /* 2476 * Do big-endian stores with movbe or system-mode. 2477 * User-only without movbe will have its swapping done generically. 2478 */ 2479 if (memop & MO_BSWAP) { 2480 tcg_debug_assert(have_movbe); 2481 use_movbe = true; 2482 movop = OPC_MOVBE_MyGy; 2483 } 2484 2485 switch (memop & MO_SIZE) { 2486 case MO_8: 2487 /* This is handled with constraints in cset_qemu_st(). */ 2488 tcg_debug_assert(TCG_TARGET_REG_BITS == 64 || datalo < 4); 2489 tcg_out_modrm_sib_offset(s, OPC_MOVB_EvGv + P_REXB_R + h.seg, 2490 datalo, h.base, h.index, 0, h.ofs); 2491 break; 2492 case MO_16: 2493 tcg_out_modrm_sib_offset(s, movop + P_DATA16 + h.seg, datalo, 2494 h.base, h.index, 0, h.ofs); 2495 break; 2496 case MO_32: 2497 tcg_out_modrm_sib_offset(s, movop + h.seg, datalo, 2498 h.base, h.index, 0, h.ofs); 2499 break; 2500 case MO_64: 2501 if (TCG_TARGET_REG_BITS == 64) { 2502 tcg_out_modrm_sib_offset(s, movop + P_REXW + h.seg, datalo, 2503 h.base, h.index, 0, h.ofs); 2504 } else { 2505 if (use_movbe) { 2506 TCGReg t = datalo; 2507 datalo = datahi; 2508 datahi = t; 2509 } 2510 tcg_out_modrm_sib_offset(s, movop + h.seg, datalo, 2511 h.base, h.index, 0, h.ofs); 2512 tcg_out_modrm_sib_offset(s, movop + h.seg, datahi, 2513 h.base, h.index, 0, h.ofs + 4); 2514 } 2515 break; 2516 2517 case MO_128: 2518 tcg_debug_assert(TCG_TARGET_REG_BITS == 64); 2519 2520 /* 2521 * Without 16-byte atomicity, use integer regs. 2522 * That is where we have the data, and it allows bswaps. 2523 */ 2524 if (h.aa.atom < MO_128) { 2525 if (use_movbe) { 2526 TCGReg t = datalo; 2527 datalo = datahi; 2528 datahi = t; 2529 } 2530 tcg_out_modrm_sib_offset(s, movop + P_REXW + h.seg, datalo, 2531 h.base, h.index, 0, h.ofs); 2532 tcg_out_modrm_sib_offset(s, movop + P_REXW + h.seg, datahi, 2533 h.base, h.index, 0, h.ofs + 8); 2534 break; 2535 } 2536 2537 /* 2538 * With 16-byte atomicity, a vector store is required. 2539 * If we already have 16-byte alignment, then VMOVDQA always works. 2540 * Else if VMOVDQU has atomicity with dynamic alignment, use that. 2541 * Else use we require a runtime test for alignment for VMOVDQA; 2542 * use VMOVDQU on the unaligned nonatomic path for simplicity. 2543 */ 2544 tcg_out_pair_to_vec(s, TCG_TYPE_I64, TCG_TMP_VEC, datalo, datahi); 2545 if (h.aa.align >= MO_128) { 2546 tcg_out_vex_modrm_sib_offset(s, OPC_MOVDQA_WxVx + h.seg, 2547 TCG_TMP_VEC, 0, 2548 h.base, h.index, 0, h.ofs); 2549 } else if (cpuinfo & CPUINFO_ATOMIC_VMOVDQU) { 2550 tcg_out_vex_modrm_sib_offset(s, OPC_MOVDQU_WxVx + h.seg, 2551 TCG_TMP_VEC, 0, 2552 h.base, h.index, 0, h.ofs); 2553 } else { 2554 TCGLabel *l1 = gen_new_label(); 2555 TCGLabel *l2 = gen_new_label(); 2556 int jcc; 2557 2558 jcc = tcg_out_cmp(s, TCG_COND_TSTNE, h.base, 15, true, false); 2559 tcg_out_jxx(s, jcc, l1, true); 2560 2561 tcg_out_vex_modrm_sib_offset(s, OPC_MOVDQA_WxVx + h.seg, 2562 TCG_TMP_VEC, 0, 2563 h.base, h.index, 0, h.ofs); 2564 tcg_out_jxx(s, JCC_JMP, l2, true); 2565 2566 tcg_out_label(s, l1); 2567 tcg_out_vex_modrm_sib_offset(s, OPC_MOVDQU_WxVx + h.seg, 2568 TCG_TMP_VEC, 0, 2569 h.base, h.index, 0, h.ofs); 2570 tcg_out_label(s, l2); 2571 } 2572 break; 2573 2574 default: 2575 g_assert_not_reached(); 2576 } 2577} 2578 2579static void tgen_qemu_st(TCGContext *s, TCGType type, TCGReg data, 2580 TCGReg addr, MemOpIdx oi) 2581{ 2582 TCGLabelQemuLdst *ldst; 2583 HostAddress h; 2584 2585 ldst = prepare_host_addr(s, &h, addr, oi, false); 2586 tcg_out_qemu_st_direct(s, data, -1, h, get_memop(oi)); 2587 2588 if (ldst) { 2589 ldst->type = type; 2590 ldst->datalo_reg = data; 2591 ldst->datahi_reg = -1; 2592 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr); 2593 } 2594} 2595 2596static TCGConstraintSetIndex cset_qemu_st(TCGType type, unsigned flags) 2597{ 2598 return flags == MO_8 ? C_O0_I2(s, L) : C_O0_I2(L, L); 2599} 2600 2601static const TCGOutOpQemuLdSt outop_qemu_st = { 2602 .base.static_constraint = 2603 TCG_TARGET_REG_BITS == 32 ? C_Dynamic : C_O0_I2(L, L), 2604 .base.dynamic_constraint = 2605 TCG_TARGET_REG_BITS == 32 ? cset_qemu_st : NULL, 2606 .out = tgen_qemu_st, 2607}; 2608 2609static void tgen_qemu_st2(TCGContext *s, TCGType type, TCGReg datalo, 2610 TCGReg datahi, TCGReg addr, MemOpIdx oi) 2611{ 2612 TCGLabelQemuLdst *ldst; 2613 HostAddress h; 2614 2615 ldst = prepare_host_addr(s, &h, addr, oi, false); 2616 tcg_out_qemu_st_direct(s, datalo, datahi, h, get_memop(oi)); 2617 2618 if (ldst) { 2619 ldst->type = type; 2620 ldst->datalo_reg = datalo; 2621 ldst->datahi_reg = datahi; 2622 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr); 2623 } 2624} 2625 2626static const TCGOutOpQemuLdSt2 outop_qemu_st2 = { 2627 .base.static_constraint = C_O0_I3(L, L, L), 2628 .out = tgen_qemu_st2, 2629}; 2630 2631static void tcg_out_exit_tb(TCGContext *s, uintptr_t a0) 2632{ 2633 /* Reuse the zeroing that exists for goto_ptr. */ 2634 if (a0 == 0) { 2635 tcg_out_jmp(s, tcg_code_gen_epilogue); 2636 } else { 2637 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_EAX, a0); 2638 tcg_out_jmp(s, tb_ret_addr); 2639 } 2640} 2641 2642static void tcg_out_goto_tb(TCGContext *s, int which) 2643{ 2644 /* 2645 * Jump displacement must be aligned for atomic patching; 2646 * see if we need to add extra nops before jump 2647 */ 2648 int gap = QEMU_ALIGN_PTR_UP(s->code_ptr + 1, 4) - s->code_ptr; 2649 if (gap != 1) { 2650 tcg_out_nopn(s, gap - 1); 2651 } 2652 tcg_out8(s, OPC_JMP_long); /* jmp im */ 2653 set_jmp_insn_offset(s, which); 2654 tcg_out32(s, 0); 2655 set_jmp_reset_offset(s, which); 2656} 2657 2658static void tcg_out_goto_ptr(TCGContext *s, TCGReg a0) 2659{ 2660 /* Jump to the given host address (could be epilogue) */ 2661 tcg_out_modrm(s, OPC_GRP5, EXT5_JMPN_Ev, a0); 2662} 2663 2664void tb_target_set_jmp_target(const TranslationBlock *tb, int n, 2665 uintptr_t jmp_rx, uintptr_t jmp_rw) 2666{ 2667 /* patch the branch destination */ 2668 uintptr_t addr = tb->jmp_target_addr[n]; 2669 qatomic_set((int32_t *)jmp_rw, addr - (jmp_rx + 4)); 2670 /* no need to flush icache explicitly */ 2671} 2672 2673 2674static void tgen_add(TCGContext *s, TCGType type, 2675 TCGReg a0, TCGReg a1, TCGReg a2) 2676{ 2677 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 2678 2679 if (a0 == a1) { 2680 tgen_arithr(s, ARITH_ADD + rexw, a0, a2); 2681 } else if (a0 == a2) { 2682 tgen_arithr(s, ARITH_ADD + rexw, a0, a1); 2683 } else { 2684 tcg_out_modrm_sib_offset(s, OPC_LEA + rexw, a0, a1, a2, 0, 0); 2685 } 2686} 2687 2688static void tgen_addi(TCGContext *s, TCGType type, 2689 TCGReg a0, TCGReg a1, tcg_target_long a2) 2690{ 2691 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 2692 2693 if (a0 == a1) { 2694 tgen_arithi(s, ARITH_ADD + rexw, a0, a2, false); 2695 } else { 2696 tcg_out_modrm_sib_offset(s, OPC_LEA + rexw, a0, a1, -1, 0, a2); 2697 } 2698} 2699 2700static const TCGOutOpBinary outop_add = { 2701 .base.static_constraint = C_O1_I2(r, r, re), 2702 .out_rrr = tgen_add, 2703 .out_rri = tgen_addi, 2704}; 2705 2706static void tgen_addco(TCGContext *s, TCGType type, 2707 TCGReg a0, TCGReg a1, TCGReg a2) 2708{ 2709 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 2710 tgen_arithr(s, ARITH_ADD + rexw, a0, a2); 2711} 2712 2713static void tgen_addco_imm(TCGContext *s, TCGType type, 2714 TCGReg a0, TCGReg a1, tcg_target_long a2) 2715{ 2716 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 2717 tgen_arithi(s, ARITH_ADD + rexw, a0, a2, true); 2718} 2719 2720static const TCGOutOpBinary outop_addco = { 2721 .base.static_constraint = C_O1_I2(r, 0, re), 2722 .out_rrr = tgen_addco, 2723 .out_rri = tgen_addco_imm, 2724}; 2725 2726static void tgen_addcio(TCGContext *s, TCGType type, 2727 TCGReg a0, TCGReg a1, TCGReg a2) 2728{ 2729 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 2730 tgen_arithr(s, ARITH_ADC + rexw, a0, a2); 2731} 2732 2733static void tgen_addcio_imm(TCGContext *s, TCGType type, 2734 TCGReg a0, TCGReg a1, tcg_target_long a2) 2735{ 2736 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 2737 tgen_arithi(s, ARITH_ADC + rexw, a0, a2, true); 2738} 2739 2740static const TCGOutOpBinary outop_addcio = { 2741 .base.static_constraint = C_O1_I2(r, 0, re), 2742 .out_rrr = tgen_addcio, 2743 .out_rri = tgen_addcio_imm, 2744}; 2745 2746static void tgen_addci_rrr(TCGContext *s, TCGType type, 2747 TCGReg a0, TCGReg a1, TCGReg a2) 2748{ 2749 /* Because "0O" is not a valid constraint, we must match ourselves. */ 2750 if (a0 == a2) { 2751 tgen_addcio(s, type, a0, a0, a1); 2752 } else { 2753 tcg_out_mov(s, type, a0, a1); 2754 tgen_addcio(s, type, a0, a0, a2); 2755 } 2756} 2757 2758static void tgen_addci_rri(TCGContext *s, TCGType type, 2759 TCGReg a0, TCGReg a1, tcg_target_long a2) 2760{ 2761 tcg_out_mov(s, type, a0, a1); 2762 tgen_addcio_imm(s, type, a0, a0, a2); 2763} 2764 2765static void tgen_addci_rir(TCGContext *s, TCGType type, 2766 TCGReg a0, tcg_target_long a1, TCGReg a2) 2767{ 2768 tgen_addci_rri(s, type, a0, a2, a1); 2769} 2770 2771static void tgen_addci_rii(TCGContext *s, TCGType type, TCGReg a0, 2772 tcg_target_long a1, tcg_target_long a2) 2773{ 2774 if (a2 == 0) { 2775 /* Implement 0 + 0 + C with -(x - x - c). */ 2776 tgen_arithr(s, ARITH_SBB, a0, a0); 2777 tcg_out_modrm(s, OPC_GRP3_Ev, EXT3_NEG, a0); 2778 } else { 2779 tcg_out_movi(s, type, a0, a2); 2780 tgen_addcio_imm(s, type, a0, a0, a1); 2781 } 2782} 2783 2784static const TCGOutOpAddSubCarry outop_addci = { 2785 .base.static_constraint = C_O1_I2(r, rO, re), 2786 .out_rrr = tgen_addci_rrr, 2787 .out_rri = tgen_addci_rri, 2788 .out_rir = tgen_addci_rir, 2789 .out_rii = tgen_addci_rii, 2790}; 2791 2792static void tcg_out_set_carry(TCGContext *s) 2793{ 2794 tcg_out8(s, OPC_STC); 2795} 2796 2797static void tgen_and(TCGContext *s, TCGType type, 2798 TCGReg a0, TCGReg a1, TCGReg a2) 2799{ 2800 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 2801 tgen_arithr(s, ARITH_AND + rexw, a0, a2); 2802} 2803 2804static void tgen_andi(TCGContext *s, TCGType type, 2805 TCGReg a0, TCGReg a1, tcg_target_long a2) 2806{ 2807 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 2808 tgen_arithi(s, ARITH_AND + rexw, a0, a2, false); 2809} 2810 2811static const TCGOutOpBinary outop_and = { 2812 .base.static_constraint = C_O1_I2(r, 0, reZ), 2813 .out_rrr = tgen_and, 2814 .out_rri = tgen_andi, 2815}; 2816 2817static void tgen_andc(TCGContext *s, TCGType type, 2818 TCGReg a0, TCGReg a1, TCGReg a2) 2819{ 2820 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 2821 tcg_out_vex_modrm(s, OPC_ANDN + rexw, a0, a2, a1); 2822} 2823 2824static TCGConstraintSetIndex cset_andc(TCGType type, unsigned flags) 2825{ 2826 return have_bmi1 ? C_O1_I2(r, r, r) : C_NotImplemented; 2827} 2828 2829static const TCGOutOpBinary outop_andc = { 2830 .base.static_constraint = C_Dynamic, 2831 .base.dynamic_constraint = cset_andc, 2832 .out_rrr = tgen_andc, 2833}; 2834 2835static void tgen_clz(TCGContext *s, TCGType type, 2836 TCGReg a0, TCGReg a1, TCGReg a2) 2837{ 2838 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 2839 int jcc; 2840 2841 if (have_lzcnt) { 2842 tcg_out_modrm(s, OPC_LZCNT + rexw, a0, a1); 2843 jcc = JCC_JB; 2844 } else { 2845 /* Recall that the output of BSR is the index not the count. */ 2846 tcg_out_modrm(s, OPC_BSR + rexw, a0, a1); 2847 tgen_arithi(s, ARITH_XOR + rexw, a0, rexw ? 63 : 31, 0); 2848 2849 /* Since we have destroyed the flags from BSR, we have to re-test. */ 2850 jcc = tcg_out_cmp(s, TCG_COND_EQ, a1, 0, 1, rexw); 2851 } 2852 tcg_out_cmov(s, jcc, rexw, a0, a2); 2853} 2854 2855static void tgen_clzi(TCGContext *s, TCGType type, 2856 TCGReg a0, TCGReg a1, tcg_target_long a2) 2857{ 2858 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 2859 tcg_out_modrm(s, OPC_LZCNT + rexw, a0, a1); 2860} 2861 2862static TCGConstraintSetIndex cset_clz(TCGType type, unsigned flags) 2863{ 2864 return have_lzcnt ? C_N1_I2(r, r, rW) : C_N1_I2(r, r, r); 2865} 2866 2867static const TCGOutOpBinary outop_clz = { 2868 .base.static_constraint = C_Dynamic, 2869 .base.dynamic_constraint = cset_clz, 2870 .out_rrr = tgen_clz, 2871 .out_rri = tgen_clzi, 2872}; 2873 2874static void tgen_ctpop(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1) 2875{ 2876 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 2877 tcg_out_modrm(s, OPC_POPCNT + rexw, a0, a1); 2878} 2879 2880static TCGConstraintSetIndex cset_ctpop(TCGType type, unsigned flags) 2881{ 2882 return have_popcnt ? C_O1_I1(r, r) : C_NotImplemented; 2883} 2884 2885static const TCGOutOpUnary outop_ctpop = { 2886 .base.static_constraint = C_Dynamic, 2887 .base.dynamic_constraint = cset_ctpop, 2888 .out_rr = tgen_ctpop, 2889}; 2890 2891static void tgen_ctz(TCGContext *s, TCGType type, 2892 TCGReg a0, TCGReg a1, TCGReg a2) 2893{ 2894 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 2895 int jcc; 2896 2897 if (have_bmi1) { 2898 tcg_out_modrm(s, OPC_TZCNT + rexw, a0, a1); 2899 jcc = JCC_JB; 2900 } else { 2901 tcg_out_modrm(s, OPC_BSF + rexw, a0, a1); 2902 jcc = JCC_JE; 2903 } 2904 tcg_out_cmov(s, jcc, rexw, a0, a2); 2905} 2906 2907static void tgen_ctzi(TCGContext *s, TCGType type, 2908 TCGReg a0, TCGReg a1, tcg_target_long a2) 2909{ 2910 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 2911 tcg_out_modrm(s, OPC_TZCNT + rexw, a0, a1); 2912} 2913 2914static TCGConstraintSetIndex cset_ctz(TCGType type, unsigned flags) 2915{ 2916 return have_bmi1 ? C_N1_I2(r, r, rW) : C_N1_I2(r, r, r); 2917} 2918 2919static const TCGOutOpBinary outop_ctz = { 2920 .base.static_constraint = C_Dynamic, 2921 .base.dynamic_constraint = cset_ctz, 2922 .out_rrr = tgen_ctz, 2923 .out_rri = tgen_ctzi, 2924}; 2925 2926static const TCGOutOpBinary outop_divs = { 2927 .base.static_constraint = C_NotImplemented, 2928}; 2929 2930static void tgen_divs2(TCGContext *s, TCGType type, 2931 TCGReg a0, TCGReg a1, TCGReg a4) 2932{ 2933 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 2934 tcg_out_modrm(s, OPC_GRP3_Ev + rexw, EXT3_IDIV, a4); 2935} 2936 2937static const TCGOutOpDivRem outop_divs2 = { 2938 .base.static_constraint = C_O2_I3(a, d, 0, 1, r), 2939 .out_rr01r = tgen_divs2, 2940}; 2941 2942static const TCGOutOpBinary outop_divu = { 2943 .base.static_constraint = C_NotImplemented, 2944}; 2945 2946static void tgen_divu2(TCGContext *s, TCGType type, 2947 TCGReg a0, TCGReg a1, TCGReg a4) 2948{ 2949 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 2950 tcg_out_modrm(s, OPC_GRP3_Ev + rexw, EXT3_DIV, a4); 2951} 2952 2953static const TCGOutOpDivRem outop_divu2 = { 2954 .base.static_constraint = C_O2_I3(a, d, 0, 1, r), 2955 .out_rr01r = tgen_divu2, 2956}; 2957 2958static const TCGOutOpBinary outop_eqv = { 2959 .base.static_constraint = C_NotImplemented, 2960}; 2961 2962#if TCG_TARGET_REG_BITS == 64 2963static void tgen_extrh_i64_i32(TCGContext *s, TCGType t, TCGReg a0, TCGReg a1) 2964{ 2965 tcg_out_shifti(s, SHIFT_SHR + P_REXW, a0, 32); 2966} 2967 2968static const TCGOutOpUnary outop_extrh_i64_i32 = { 2969 .base.static_constraint = C_O1_I1(r, 0), 2970 .out_rr = tgen_extrh_i64_i32, 2971}; 2972#endif /* TCG_TARGET_REG_BITS == 64 */ 2973 2974static void tgen_mul(TCGContext *s, TCGType type, 2975 TCGReg a0, TCGReg a1, TCGReg a2) 2976{ 2977 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 2978 tcg_out_modrm(s, OPC_IMUL_GvEv + rexw, a0, a2); 2979} 2980 2981static void tgen_muli(TCGContext *s, TCGType type, 2982 TCGReg a0, TCGReg a1, tcg_target_long a2) 2983{ 2984 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 2985 2986 if (a2 == (int8_t)a2) { 2987 tcg_out_modrm(s, OPC_IMUL_GvEvIb + rexw, a0, a0); 2988 tcg_out8(s, a2); 2989 } else { 2990 tcg_out_modrm(s, OPC_IMUL_GvEvIz + rexw, a0, a0); 2991 tcg_out32(s, a2); 2992 } 2993} 2994 2995static const TCGOutOpBinary outop_mul = { 2996 .base.static_constraint = C_O1_I2(r, 0, re), 2997 .out_rrr = tgen_mul, 2998 .out_rri = tgen_muli, 2999}; 3000 3001static void tgen_muls2(TCGContext *s, TCGType type, 3002 TCGReg a0, TCGReg a1, TCGReg a2, TCGReg a3) 3003{ 3004 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 3005 tcg_out_modrm(s, OPC_GRP3_Ev + rexw, EXT3_IMUL, a3); 3006} 3007 3008static const TCGOutOpMul2 outop_muls2 = { 3009 .base.static_constraint = C_O2_I2(a, d, a, r), 3010 .out_rrrr = tgen_muls2, 3011}; 3012 3013static const TCGOutOpBinary outop_mulsh = { 3014 .base.static_constraint = C_NotImplemented, 3015}; 3016 3017static const TCGOutOpBinary outop_muluh = { 3018 .base.static_constraint = C_NotImplemented, 3019}; 3020 3021static void tgen_mulu2(TCGContext *s, TCGType type, 3022 TCGReg a0, TCGReg a1, TCGReg a2, TCGReg a3) 3023{ 3024 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 3025 tcg_out_modrm(s, OPC_GRP3_Ev + rexw, EXT3_MUL, a3); 3026} 3027 3028static const TCGOutOpMul2 outop_mulu2 = { 3029 .base.static_constraint = C_O2_I2(a, d, a, r), 3030 .out_rrrr = tgen_mulu2, 3031}; 3032 3033static const TCGOutOpBinary outop_nand = { 3034 .base.static_constraint = C_NotImplemented, 3035}; 3036 3037static const TCGOutOpBinary outop_nor = { 3038 .base.static_constraint = C_NotImplemented, 3039}; 3040 3041static void tgen_or(TCGContext *s, TCGType type, 3042 TCGReg a0, TCGReg a1, TCGReg a2) 3043{ 3044 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 3045 tgen_arithr(s, ARITH_OR + rexw, a0, a2); 3046} 3047 3048static void tgen_ori(TCGContext *s, TCGType type, 3049 TCGReg a0, TCGReg a1, tcg_target_long a2) 3050{ 3051 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 3052 tgen_arithi(s, ARITH_OR + rexw, a0, a2, false); 3053} 3054 3055static const TCGOutOpBinary outop_or = { 3056 .base.static_constraint = C_O1_I2(r, 0, re), 3057 .out_rrr = tgen_or, 3058 .out_rri = tgen_ori, 3059}; 3060 3061static const TCGOutOpBinary outop_orc = { 3062 .base.static_constraint = C_NotImplemented, 3063}; 3064 3065static const TCGOutOpBinary outop_rems = { 3066 .base.static_constraint = C_NotImplemented, 3067}; 3068 3069static const TCGOutOpBinary outop_remu = { 3070 .base.static_constraint = C_NotImplemented, 3071}; 3072 3073static void tgen_rotl(TCGContext *s, TCGType type, 3074 TCGReg a0, TCGReg a1, TCGReg a2) 3075{ 3076 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 3077 tcg_out_modrm(s, OPC_SHIFT_cl + rexw, SHIFT_ROL, a0); 3078} 3079 3080static void tgen_rotli(TCGContext *s, TCGType type, 3081 TCGReg a0, TCGReg a1, tcg_target_long a2) 3082{ 3083 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 3084 tcg_out_shifti(s, SHIFT_ROL + rexw, a0, a2); 3085} 3086 3087static const TCGOutOpBinary outop_rotl = { 3088 .base.static_constraint = C_O1_I2(r, 0, ci), 3089 .out_rrr = tgen_rotl, 3090 .out_rri = tgen_rotli, 3091}; 3092 3093static void tgen_rotr(TCGContext *s, TCGType type, 3094 TCGReg a0, TCGReg a1, TCGReg a2) 3095{ 3096 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 3097 tcg_out_modrm(s, OPC_SHIFT_cl + rexw, SHIFT_ROR, a0); 3098} 3099 3100static void tgen_rotri(TCGContext *s, TCGType type, 3101 TCGReg a0, TCGReg a1, tcg_target_long a2) 3102{ 3103 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 3104 tcg_out_shifti(s, SHIFT_ROR + rexw, a0, a2); 3105} 3106 3107static const TCGOutOpBinary outop_rotr = { 3108 .base.static_constraint = C_O1_I2(r, 0, ci), 3109 .out_rrr = tgen_rotr, 3110 .out_rri = tgen_rotri, 3111}; 3112 3113static TCGConstraintSetIndex cset_shift(TCGType type, unsigned flags) 3114{ 3115 return have_bmi2 ? C_O1_I2(r, r, ri) : C_O1_I2(r, 0, ci); 3116} 3117 3118static void tgen_sar(TCGContext *s, TCGType type, 3119 TCGReg a0, TCGReg a1, TCGReg a2) 3120{ 3121 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 3122 if (have_bmi2) { 3123 tcg_out_vex_modrm(s, OPC_SARX + rexw, a0, a2, a1); 3124 } else { 3125 tcg_out_modrm(s, OPC_SHIFT_cl + rexw, SHIFT_SAR, a0); 3126 } 3127} 3128 3129static void tgen_sari(TCGContext *s, TCGType type, 3130 TCGReg a0, TCGReg a1, tcg_target_long a2) 3131{ 3132 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 3133 3134 tcg_out_mov(s, type, a0, a1); 3135 tcg_out_shifti(s, SHIFT_SAR + rexw, a0, a2); 3136} 3137 3138static const TCGOutOpBinary outop_sar = { 3139 .base.static_constraint = C_Dynamic, 3140 .base.dynamic_constraint = cset_shift, 3141 .out_rrr = tgen_sar, 3142 .out_rri = tgen_sari, 3143}; 3144 3145static void tgen_shl(TCGContext *s, TCGType type, 3146 TCGReg a0, TCGReg a1, TCGReg a2) 3147{ 3148 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 3149 if (have_bmi2) { 3150 tcg_out_vex_modrm(s, OPC_SHLX + rexw, a0, a2, a1); 3151 } else { 3152 tcg_out_modrm(s, OPC_SHIFT_cl + rexw, SHIFT_SHL, a0); 3153 } 3154} 3155 3156static void tgen_shli(TCGContext *s, TCGType type, 3157 TCGReg a0, TCGReg a1, tcg_target_long a2) 3158{ 3159 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 3160 3161 /* For small constant 3-operand shift, use LEA. */ 3162 if (a0 != a1 && a2 >= 1 && a2 <= 3) { 3163 if (a2 == 1) { 3164 /* shl $1,a1,a0 -> lea (a1,a1),a0 */ 3165 tcg_out_modrm_sib_offset(s, OPC_LEA + rexw, a0, a1, a1, 0, 0); 3166 } else { 3167 /* shl $n,a1,a0 -> lea 0(,a1,n),a0 */ 3168 tcg_out_modrm_sib_offset(s, OPC_LEA + rexw, a0, -1, a1, a2, 0); 3169 } 3170 return; 3171 } 3172 tcg_out_mov(s, type, a0, a1); 3173 tcg_out_shifti(s, SHIFT_SHL + rexw, a0, a2); 3174} 3175 3176static const TCGOutOpBinary outop_shl = { 3177 .base.static_constraint = C_Dynamic, 3178 .base.dynamic_constraint = cset_shift, 3179 .out_rrr = tgen_shl, 3180 .out_rri = tgen_shli, 3181}; 3182 3183static void tgen_shr(TCGContext *s, TCGType type, 3184 TCGReg a0, TCGReg a1, TCGReg a2) 3185{ 3186 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 3187 if (have_bmi2) { 3188 tcg_out_vex_modrm(s, OPC_SHRX + rexw, a0, a2, a1); 3189 } else { 3190 tcg_out_modrm(s, OPC_SHIFT_cl + rexw, SHIFT_SHR, a0); 3191 } 3192} 3193 3194static void tgen_shri(TCGContext *s, TCGType type, 3195 TCGReg a0, TCGReg a1, tcg_target_long a2) 3196{ 3197 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 3198 3199 tcg_out_mov(s, type, a0, a1); 3200 tcg_out_shifti(s, SHIFT_SHR + rexw, a0, a2); 3201} 3202 3203static const TCGOutOpBinary outop_shr = { 3204 .base.static_constraint = C_Dynamic, 3205 .base.dynamic_constraint = cset_shift, 3206 .out_rrr = tgen_shr, 3207 .out_rri = tgen_shri, 3208}; 3209 3210static void tgen_sub(TCGContext *s, TCGType type, 3211 TCGReg a0, TCGReg a1, TCGReg a2) 3212{ 3213 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 3214 tgen_arithr(s, ARITH_SUB + rexw, a0, a2); 3215} 3216 3217static const TCGOutOpSubtract outop_sub = { 3218 .base.static_constraint = C_O1_I2(r, 0, r), 3219 .out_rrr = tgen_sub, 3220}; 3221 3222static void tgen_subbo_rri(TCGContext *s, TCGType type, 3223 TCGReg a0, TCGReg a1, tcg_target_long a2) 3224{ 3225 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 3226 tgen_arithi(s, ARITH_SUB + rexw, a0, a2, 1); 3227} 3228 3229static const TCGOutOpAddSubCarry outop_subbo = { 3230 .base.static_constraint = C_O1_I2(r, 0, re), 3231 .out_rrr = tgen_sub, 3232 .out_rri = tgen_subbo_rri, 3233}; 3234 3235static void tgen_subbio_rrr(TCGContext *s, TCGType type, 3236 TCGReg a0, TCGReg a1, TCGReg a2) 3237{ 3238 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 3239 tgen_arithr(s, ARITH_SBB + rexw, a0, a2); 3240} 3241 3242static void tgen_subbio_rri(TCGContext *s, TCGType type, 3243 TCGReg a0, TCGReg a1, tcg_target_long a2) 3244{ 3245 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 3246 tgen_arithi(s, ARITH_SBB + rexw, a0, a2, 1); 3247} 3248 3249static const TCGOutOpAddSubCarry outop_subbio = { 3250 .base.static_constraint = C_O1_I2(r, 0, re), 3251 .out_rrr = tgen_subbio_rrr, 3252 .out_rri = tgen_subbio_rri, 3253}; 3254 3255#define outop_subbi outop_subbio 3256 3257static void tcg_out_set_borrow(TCGContext *s) 3258{ 3259 tcg_out8(s, OPC_STC); 3260} 3261 3262static void tgen_xor(TCGContext *s, TCGType type, 3263 TCGReg a0, TCGReg a1, TCGReg a2) 3264{ 3265 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 3266 tgen_arithr(s, ARITH_XOR + rexw, a0, a2); 3267} 3268 3269static void tgen_xori(TCGContext *s, TCGType type, 3270 TCGReg a0, TCGReg a1, tcg_target_long a2) 3271{ 3272 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 3273 tgen_arithi(s, ARITH_XOR + rexw, a0, a2, false); 3274} 3275 3276static const TCGOutOpBinary outop_xor = { 3277 .base.static_constraint = C_O1_I2(r, 0, re), 3278 .out_rrr = tgen_xor, 3279 .out_rri = tgen_xori, 3280}; 3281 3282static void tgen_bswap16(TCGContext *s, TCGType type, 3283 TCGReg a0, TCGReg a1, unsigned flags) 3284{ 3285 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 3286 3287 if (flags & TCG_BSWAP_OS) { 3288 /* Output must be sign-extended. */ 3289 if (rexw) { 3290 tcg_out_bswap64(s, a0); 3291 tcg_out_shifti(s, SHIFT_SAR + rexw, a0, 48); 3292 } else { 3293 tcg_out_bswap32(s, a0); 3294 tcg_out_shifti(s, SHIFT_SAR, a0, 16); 3295 } 3296 } else if ((flags & (TCG_BSWAP_IZ | TCG_BSWAP_OZ)) == TCG_BSWAP_OZ) { 3297 /* Output must be zero-extended, but input isn't. */ 3298 tcg_out_bswap32(s, a0); 3299 tcg_out_shifti(s, SHIFT_SHR, a0, 16); 3300 } else { 3301 tcg_out_rolw_8(s, a0); 3302 } 3303} 3304 3305static const TCGOutOpBswap outop_bswap16 = { 3306 .base.static_constraint = C_O1_I1(r, 0), 3307 .out_rr = tgen_bswap16, 3308}; 3309 3310static void tgen_bswap32(TCGContext *s, TCGType type, 3311 TCGReg a0, TCGReg a1, unsigned flags) 3312{ 3313 tcg_out_bswap32(s, a0); 3314 if (flags & TCG_BSWAP_OS) { 3315 tcg_out_ext32s(s, a0, a0); 3316 } 3317} 3318 3319static const TCGOutOpBswap outop_bswap32 = { 3320 .base.static_constraint = C_O1_I1(r, 0), 3321 .out_rr = tgen_bswap32, 3322}; 3323 3324#if TCG_TARGET_REG_BITS == 64 3325static void tgen_bswap64(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1) 3326{ 3327 tcg_out_bswap64(s, a0); 3328} 3329 3330static const TCGOutOpUnary outop_bswap64 = { 3331 .base.static_constraint = C_O1_I1(r, 0), 3332 .out_rr = tgen_bswap64, 3333}; 3334#endif 3335 3336static void tgen_neg(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1) 3337{ 3338 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 3339 tcg_out_modrm(s, OPC_GRP3_Ev + rexw, EXT3_NEG, a0); 3340} 3341 3342static const TCGOutOpUnary outop_neg = { 3343 .base.static_constraint = C_O1_I1(r, 0), 3344 .out_rr = tgen_neg, 3345}; 3346 3347static void tgen_not(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1) 3348{ 3349 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 3350 tcg_out_modrm(s, OPC_GRP3_Ev + rexw, EXT3_NOT, a0); 3351} 3352 3353static const TCGOutOpUnary outop_not = { 3354 .base.static_constraint = C_O1_I1(r, 0), 3355 .out_rr = tgen_not, 3356}; 3357 3358static void tgen_deposit(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, 3359 TCGReg a2, unsigned ofs, unsigned len) 3360{ 3361 if (ofs == 0 && len == 8) { 3362 tcg_out_modrm(s, OPC_MOVB_EvGv | P_REXB_R | P_REXB_RM, a2, a0); 3363 } else if (ofs == 0 && len == 16) { 3364 tcg_out_modrm(s, OPC_MOVL_EvGv | P_DATA16, a2, a0); 3365 } else if (TCG_TARGET_REG_BITS == 32 && ofs == 8 && len == 8) { 3366 tcg_out_modrm(s, OPC_MOVB_EvGv, a2, a0 + 4); 3367 } else { 3368 g_assert_not_reached(); 3369 } 3370} 3371 3372static void tgen_depositi(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, 3373 tcg_target_long a2, unsigned ofs, unsigned len) 3374{ 3375 if (ofs == 0 && len == 8) { 3376 tcg_out_opc(s, OPC_MOVB_Ib | P_REXB_RM | LOWREGMASK(a0), 0, a0, 0); 3377 tcg_out8(s, a2); 3378 } else if (ofs == 0 && len == 16) { 3379 tcg_out_opc(s, OPC_MOVL_Iv | P_DATA16 | LOWREGMASK(a0), 0, a0, 0); 3380 tcg_out16(s, a2); 3381 } else if (TCG_TARGET_REG_BITS == 32 && ofs == 8 && len == 8) { 3382 tcg_out8(s, OPC_MOVB_Ib + a0 + 4); 3383 tcg_out8(s, a2); 3384 } else { 3385 g_assert_not_reached(); 3386 } 3387} 3388 3389static const TCGOutOpDeposit outop_deposit = { 3390 .base.static_constraint = C_O1_I2(q, 0, qi), 3391 .out_rrr = tgen_deposit, 3392 .out_rri = tgen_depositi, 3393}; 3394 3395static void tgen_extract(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, 3396 unsigned ofs, unsigned len) 3397{ 3398 if (ofs == 0) { 3399 switch (len) { 3400 case 8: 3401 tcg_out_ext8u(s, a0, a1); 3402 return; 3403 case 16: 3404 tcg_out_ext16u(s, a0, a1); 3405 return; 3406 case 32: 3407 tcg_out_ext32u(s, a0, a1); 3408 return; 3409 } 3410 } else if (TCG_TARGET_REG_BITS == 64 && ofs + len == 32) { 3411 /* This is a 32-bit zero-extending right shift. */ 3412 tcg_out_mov(s, TCG_TYPE_I32, a0, a1); 3413 tcg_out_shifti(s, SHIFT_SHR, a0, ofs); 3414 return; 3415 } else if (ofs == 8 && len == 8) { 3416 /* 3417 * On the off-chance that we can use the high-byte registers. 3418 * Otherwise we emit the same ext16 + shift pattern that we 3419 * would have gotten from the normal tcg-op.c expansion. 3420 */ 3421 if (a1 < 4 && (TCG_TARGET_REG_BITS == 32 || a0 < 8)) { 3422 tcg_out_modrm(s, OPC_MOVZBL, a0, a1 + 4); 3423 } else { 3424 tcg_out_ext16u(s, a0, a1); 3425 tcg_out_shifti(s, SHIFT_SHR, a0, 8); 3426 } 3427 return; 3428 } 3429 g_assert_not_reached(); 3430} 3431 3432static const TCGOutOpExtract outop_extract = { 3433 .base.static_constraint = C_O1_I1(r, r), 3434 .out_rr = tgen_extract, 3435}; 3436 3437static void tgen_sextract(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, 3438 unsigned ofs, unsigned len) 3439{ 3440 if (ofs == 0) { 3441 switch (len) { 3442 case 8: 3443 tcg_out_ext8s(s, type, a0, a1); 3444 return; 3445 case 16: 3446 tcg_out_ext16s(s, type, a0, a1); 3447 return; 3448 case 32: 3449 tcg_out_ext32s(s, a0, a1); 3450 return; 3451 } 3452 } else if (ofs == 8 && len == 8) { 3453 if (type == TCG_TYPE_I32 && a1 < 4 && a0 < 8) { 3454 tcg_out_modrm(s, OPC_MOVSBL, a0, a1 + 4); 3455 } else { 3456 tcg_out_ext16s(s, type, a0, a1); 3457 tgen_sari(s, type, a0, a0, 8); 3458 } 3459 return; 3460 } 3461 g_assert_not_reached(); 3462} 3463 3464static const TCGOutOpExtract outop_sextract = { 3465 .base.static_constraint = C_O1_I1(r, r), 3466 .out_rr = tgen_sextract, 3467}; 3468 3469static void tgen_extract2(TCGContext *s, TCGType type, TCGReg a0, 3470 TCGReg a1, TCGReg a2, unsigned shr) 3471{ 3472 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 3473 3474 /* Note that SHRD outputs to the r/m operand. */ 3475 tcg_out_modrm(s, OPC_SHRD_Ib + rexw, a2, a0); 3476 tcg_out8(s, shr); 3477} 3478 3479static const TCGOutOpExtract2 outop_extract2 = { 3480 .base.static_constraint = C_O1_I2(r, 0, r), 3481 .out_rrr = tgen_extract2, 3482}; 3483 3484static void tgen_ld8u(TCGContext *s, TCGType type, TCGReg dest, 3485 TCGReg base, ptrdiff_t offset) 3486{ 3487 tcg_out_modrm_offset(s, OPC_MOVZBL, dest, base, offset); 3488} 3489 3490static const TCGOutOpLoad outop_ld8u = { 3491 .base.static_constraint = C_O1_I1(r, r), 3492 .out = tgen_ld8u, 3493}; 3494 3495static void tgen_ld8s(TCGContext *s, TCGType type, TCGReg dest, 3496 TCGReg base, ptrdiff_t offset) 3497{ 3498 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 3499 tcg_out_modrm_offset(s, OPC_MOVSBL + rexw, dest, base, offset); 3500} 3501 3502static const TCGOutOpLoad outop_ld8s = { 3503 .base.static_constraint = C_O1_I1(r, r), 3504 .out = tgen_ld8s, 3505}; 3506 3507static void tgen_ld16u(TCGContext *s, TCGType type, TCGReg dest, 3508 TCGReg base, ptrdiff_t offset) 3509{ 3510 tcg_out_modrm_offset(s, OPC_MOVZWL, dest, base, offset); 3511} 3512 3513static const TCGOutOpLoad outop_ld16u = { 3514 .base.static_constraint = C_O1_I1(r, r), 3515 .out = tgen_ld16u, 3516}; 3517 3518static void tgen_ld16s(TCGContext *s, TCGType type, TCGReg dest, 3519 TCGReg base, ptrdiff_t offset) 3520{ 3521 int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW; 3522 tcg_out_modrm_offset(s, OPC_MOVSWL + rexw, dest, base, offset); 3523} 3524 3525static const TCGOutOpLoad outop_ld16s = { 3526 .base.static_constraint = C_O1_I1(r, r), 3527 .out = tgen_ld16s, 3528}; 3529 3530#if TCG_TARGET_REG_BITS == 64 3531static void tgen_ld32u(TCGContext *s, TCGType type, TCGReg dest, 3532 TCGReg base, ptrdiff_t offset) 3533{ 3534 tcg_out_modrm_offset(s, OPC_MOVL_GvEv, dest, base, offset); 3535} 3536 3537static const TCGOutOpLoad outop_ld32u = { 3538 .base.static_constraint = C_O1_I1(r, r), 3539 .out = tgen_ld32u, 3540}; 3541 3542static void tgen_ld32s(TCGContext *s, TCGType type, TCGReg dest, 3543 TCGReg base, ptrdiff_t offset) 3544{ 3545 tcg_out_modrm_offset(s, OPC_MOVSLQ, dest, base, offset); 3546} 3547 3548static const TCGOutOpLoad outop_ld32s = { 3549 .base.static_constraint = C_O1_I1(r, r), 3550 .out = tgen_ld32s, 3551}; 3552#endif 3553 3554static void tgen_st8_r(TCGContext *s, TCGType type, TCGReg data, 3555 TCGReg base, ptrdiff_t offset) 3556{ 3557 tcg_out_modrm_offset(s, OPC_MOVB_EvGv | P_REXB_R, data, base, offset); 3558} 3559 3560static void tgen_st8_i(TCGContext *s, TCGType type, tcg_target_long data, 3561 TCGReg base, ptrdiff_t offset) 3562{ 3563 tcg_out_modrm_offset(s, OPC_MOVB_EvIz, 0, base, offset); 3564 tcg_out8(s, data); 3565} 3566 3567static const TCGOutOpStore outop_st8 = { 3568 .base.static_constraint = C_O0_I2(qi, r), 3569 .out_r = tgen_st8_r, 3570 .out_i = tgen_st8_i, 3571}; 3572 3573static void tgen_st16_r(TCGContext *s, TCGType type, TCGReg data, 3574 TCGReg base, ptrdiff_t offset) 3575{ 3576 tcg_out_modrm_offset(s, OPC_MOVL_EvGv | P_DATA16, data, base, offset); 3577} 3578 3579static void tgen_st16_i(TCGContext *s, TCGType type, tcg_target_long data, 3580 TCGReg base, ptrdiff_t offset) 3581{ 3582 tcg_out_modrm_offset(s, OPC_MOVL_EvIz | P_DATA16, 0, base, offset); 3583 tcg_out16(s, data); 3584} 3585 3586static const TCGOutOpStore outop_st16 = { 3587 .base.static_constraint = C_O0_I2(ri, r), 3588 .out_r = tgen_st16_r, 3589 .out_i = tgen_st16_i, 3590}; 3591 3592static void tgen_st_i(TCGContext *s, TCGType type, tcg_target_long data, 3593 TCGReg base, ptrdiff_t offset) 3594{ 3595 bool ok = tcg_out_sti(s, type, data, base, offset); 3596 tcg_debug_assert(ok); 3597} 3598 3599static const TCGOutOpStore outop_st = { 3600 .base.static_constraint = C_O0_I2(re, r), 3601 .out_r = tcg_out_st, 3602 .out_i = tgen_st_i, 3603}; 3604 3605static int const umin_insn[4] = { 3606 OPC_PMINUB, OPC_PMINUW, OPC_PMINUD, OPC_VPMINUQ 3607}; 3608 3609static int const umax_insn[4] = { 3610 OPC_PMAXUB, OPC_PMAXUW, OPC_PMAXUD, OPC_VPMAXUQ 3611}; 3612 3613static bool tcg_out_cmp_vec_noinv(TCGContext *s, TCGType type, unsigned vece, 3614 TCGReg v0, TCGReg v1, TCGReg v2, TCGCond cond) 3615{ 3616 static int const cmpeq_insn[4] = { 3617 OPC_PCMPEQB, OPC_PCMPEQW, OPC_PCMPEQD, OPC_PCMPEQQ 3618 }; 3619 static int const cmpgt_insn[4] = { 3620 OPC_PCMPGTB, OPC_PCMPGTW, OPC_PCMPGTD, OPC_PCMPGTQ 3621 }; 3622 3623 enum { 3624 NEED_INV = 1, 3625 NEED_SWAP = 2, 3626 NEED_UMIN = 4, 3627 NEED_UMAX = 8, 3628 INVALID = 16, 3629 }; 3630 static const uint8_t cond_fixup[16] = { 3631 [0 ... 15] = INVALID, 3632 [TCG_COND_EQ] = 0, 3633 [TCG_COND_GT] = 0, 3634 [TCG_COND_NE] = NEED_INV, 3635 [TCG_COND_LE] = NEED_INV, 3636 [TCG_COND_LT] = NEED_SWAP, 3637 [TCG_COND_GE] = NEED_SWAP | NEED_INV, 3638 [TCG_COND_LEU] = NEED_UMIN, 3639 [TCG_COND_GTU] = NEED_UMIN | NEED_INV, 3640 [TCG_COND_GEU] = NEED_UMAX, 3641 [TCG_COND_LTU] = NEED_UMAX | NEED_INV, 3642 }; 3643 int fixup = cond_fixup[cond]; 3644 3645 assert(!(fixup & INVALID)); 3646 3647 if (fixup & NEED_INV) { 3648 cond = tcg_invert_cond(cond); 3649 } 3650 3651 if (fixup & NEED_SWAP) { 3652 TCGReg swap = v1; 3653 v1 = v2; 3654 v2 = swap; 3655 cond = tcg_swap_cond(cond); 3656 } 3657 3658 if (fixup & (NEED_UMIN | NEED_UMAX)) { 3659 int op = (fixup & NEED_UMIN ? umin_insn[vece] : umax_insn[vece]); 3660 3661 /* avx2 does not have 64-bit min/max; adjusted during expand. */ 3662 assert(vece <= MO_32); 3663 3664 tcg_out_vex_modrm_type(s, op, TCG_TMP_VEC, v1, v2, type); 3665 v2 = TCG_TMP_VEC; 3666 cond = TCG_COND_EQ; 3667 } 3668 3669 switch (cond) { 3670 case TCG_COND_EQ: 3671 tcg_out_vex_modrm_type(s, cmpeq_insn[vece], v0, v1, v2, type); 3672 break; 3673 case TCG_COND_GT: 3674 tcg_out_vex_modrm_type(s, cmpgt_insn[vece], v0, v1, v2, type); 3675 break; 3676 default: 3677 g_assert_not_reached(); 3678 } 3679 return fixup & NEED_INV; 3680} 3681 3682static void tcg_out_cmp_vec_k1(TCGContext *s, TCGType type, unsigned vece, 3683 TCGReg v1, TCGReg v2, TCGCond cond) 3684{ 3685 static const int cmpm_insn[2][4] = { 3686 { OPC_VPCMPB, OPC_VPCMPW, OPC_VPCMPD, OPC_VPCMPQ }, 3687 { OPC_VPCMPUB, OPC_VPCMPUW, OPC_VPCMPUD, OPC_VPCMPUQ } 3688 }; 3689 static const int testm_insn[4] = { 3690 OPC_VPTESTMB, OPC_VPTESTMW, OPC_VPTESTMD, OPC_VPTESTMQ 3691 }; 3692 static const int testnm_insn[4] = { 3693 OPC_VPTESTNMB, OPC_VPTESTNMW, OPC_VPTESTNMD, OPC_VPTESTNMQ 3694 }; 3695 3696 static const int cond_ext[16] = { 3697 [TCG_COND_EQ] = 0, 3698 [TCG_COND_NE] = 4, 3699 [TCG_COND_LT] = 1, 3700 [TCG_COND_LTU] = 1, 3701 [TCG_COND_LE] = 2, 3702 [TCG_COND_LEU] = 2, 3703 [TCG_COND_NEVER] = 3, 3704 [TCG_COND_GE] = 5, 3705 [TCG_COND_GEU] = 5, 3706 [TCG_COND_GT] = 6, 3707 [TCG_COND_GTU] = 6, 3708 [TCG_COND_ALWAYS] = 7, 3709 }; 3710 3711 switch (cond) { 3712 case TCG_COND_TSTNE: 3713 tcg_out_vex_modrm_type(s, testm_insn[vece], /* k1 */ 1, v1, v2, type); 3714 break; 3715 case TCG_COND_TSTEQ: 3716 tcg_out_vex_modrm_type(s, testnm_insn[vece], /* k1 */ 1, v1, v2, type); 3717 break; 3718 default: 3719 tcg_out_vex_modrm_type(s, cmpm_insn[is_unsigned_cond(cond)][vece], 3720 /* k1 */ 1, v1, v2, type); 3721 tcg_out8(s, cond_ext[cond]); 3722 break; 3723 } 3724} 3725 3726static void tcg_out_k1_to_vec(TCGContext *s, TCGType type, 3727 unsigned vece, TCGReg dest) 3728{ 3729 static const int movm_insn[] = { 3730 OPC_VPMOVM2B, OPC_VPMOVM2W, OPC_VPMOVM2D, OPC_VPMOVM2Q 3731 }; 3732 tcg_out_vex_modrm_type(s, movm_insn[vece], dest, 0, /* k1 */ 1, type); 3733} 3734 3735static void tcg_out_cmp_vec(TCGContext *s, TCGType type, unsigned vece, 3736 TCGReg v0, TCGReg v1, TCGReg v2, TCGCond cond) 3737{ 3738 /* 3739 * With avx512, we have a complete set of comparisons into mask. 3740 * Unless there's a single insn expansion for the comparision, 3741 * expand via a mask in k1. 3742 */ 3743 if ((vece <= MO_16 ? have_avx512bw : have_avx512dq) 3744 && cond != TCG_COND_EQ 3745 && cond != TCG_COND_LT 3746 && cond != TCG_COND_GT) { 3747 tcg_out_cmp_vec_k1(s, type, vece, v1, v2, cond); 3748 tcg_out_k1_to_vec(s, type, vece, v0); 3749 return; 3750 } 3751 3752 if (tcg_out_cmp_vec_noinv(s, type, vece, v0, v1, v2, cond)) { 3753 tcg_out_dupi_vec(s, type, vece, TCG_TMP_VEC, -1); 3754 tcg_out_vex_modrm_type(s, OPC_PXOR, v0, v0, TCG_TMP_VEC, type); 3755 } 3756} 3757 3758static void tcg_out_cmpsel_vec_k1(TCGContext *s, TCGType type, unsigned vece, 3759 TCGReg v0, TCGReg c1, TCGReg c2, 3760 TCGReg v3, TCGReg v4, TCGCond cond) 3761{ 3762 static const int vpblendm_insn[] = { 3763 OPC_VPBLENDMB, OPC_VPBLENDMW, OPC_VPBLENDMD, OPC_VPBLENDMQ 3764 }; 3765 bool z = false; 3766 3767 /* Swap to place constant in V4 to take advantage of zero-masking. */ 3768 if (!v3) { 3769 z = true; 3770 v3 = v4; 3771 cond = tcg_invert_cond(cond); 3772 } 3773 3774 tcg_out_cmp_vec_k1(s, type, vece, c1, c2, cond); 3775 tcg_out_evex_modrm_type(s, vpblendm_insn[vece], v0, v4, v3, 3776 /* k1 */1, z, type); 3777} 3778 3779static void tcg_out_cmpsel_vec(TCGContext *s, TCGType type, unsigned vece, 3780 TCGReg v0, TCGReg c1, TCGReg c2, 3781 TCGReg v3, TCGReg v4, TCGCond cond) 3782{ 3783 bool inv; 3784 3785 if (vece <= MO_16 ? have_avx512bw : have_avx512vl) { 3786 tcg_out_cmpsel_vec_k1(s, type, vece, v0, c1, c2, v3, v4, cond); 3787 return; 3788 } 3789 3790 inv = tcg_out_cmp_vec_noinv(s, type, vece, TCG_TMP_VEC, c1, c2, cond); 3791 3792 /* 3793 * Since XMM0 is 16, the only way we get 0 into V3 3794 * is via the constant zero constraint. 3795 */ 3796 if (!v3) { 3797 if (inv) { 3798 tcg_out_vex_modrm_type(s, OPC_PAND, v0, TCG_TMP_VEC, v4, type); 3799 } else { 3800 tcg_out_vex_modrm_type(s, OPC_PANDN, v0, TCG_TMP_VEC, v4, type); 3801 } 3802 } else { 3803 if (inv) { 3804 TCGReg swap = v3; 3805 v3 = v4; 3806 v4 = swap; 3807 } 3808 tcg_out_vex_modrm_type(s, OPC_VPBLENDVB, v0, v4, v3, type); 3809 tcg_out8(s, (TCG_TMP_VEC - TCG_REG_XMM0) << 4); 3810 } 3811} 3812 3813static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, 3814 unsigned vecl, unsigned vece, 3815 const TCGArg args[TCG_MAX_OP_ARGS], 3816 const int const_args[TCG_MAX_OP_ARGS]) 3817{ 3818 static int const add_insn[4] = { 3819 OPC_PADDB, OPC_PADDW, OPC_PADDD, OPC_PADDQ 3820 }; 3821 static int const ssadd_insn[4] = { 3822 OPC_PADDSB, OPC_PADDSW, OPC_UD2, OPC_UD2 3823 }; 3824 static int const usadd_insn[4] = { 3825 OPC_PADDUB, OPC_PADDUW, OPC_UD2, OPC_UD2 3826 }; 3827 static int const sub_insn[4] = { 3828 OPC_PSUBB, OPC_PSUBW, OPC_PSUBD, OPC_PSUBQ 3829 }; 3830 static int const sssub_insn[4] = { 3831 OPC_PSUBSB, OPC_PSUBSW, OPC_UD2, OPC_UD2 3832 }; 3833 static int const ussub_insn[4] = { 3834 OPC_PSUBUB, OPC_PSUBUW, OPC_UD2, OPC_UD2 3835 }; 3836 static int const mul_insn[4] = { 3837 OPC_UD2, OPC_PMULLW, OPC_PMULLD, OPC_VPMULLQ 3838 }; 3839 static int const shift_imm_insn[4] = { 3840 OPC_UD2, OPC_PSHIFTW_Ib, OPC_PSHIFTD_Ib, OPC_PSHIFTQ_Ib 3841 }; 3842 static int const punpckl_insn[4] = { 3843 OPC_PUNPCKLBW, OPC_PUNPCKLWD, OPC_PUNPCKLDQ, OPC_PUNPCKLQDQ 3844 }; 3845 static int const punpckh_insn[4] = { 3846 OPC_PUNPCKHBW, OPC_PUNPCKHWD, OPC_PUNPCKHDQ, OPC_PUNPCKHQDQ 3847 }; 3848 static int const packss_insn[4] = { 3849 OPC_PACKSSWB, OPC_PACKSSDW, OPC_UD2, OPC_UD2 3850 }; 3851 static int const packus_insn[4] = { 3852 OPC_PACKUSWB, OPC_PACKUSDW, OPC_UD2, OPC_UD2 3853 }; 3854 static int const smin_insn[4] = { 3855 OPC_PMINSB, OPC_PMINSW, OPC_PMINSD, OPC_VPMINSQ 3856 }; 3857 static int const smax_insn[4] = { 3858 OPC_PMAXSB, OPC_PMAXSW, OPC_PMAXSD, OPC_VPMAXSQ 3859 }; 3860 static int const rotlv_insn[4] = { 3861 OPC_UD2, OPC_UD2, OPC_VPROLVD, OPC_VPROLVQ 3862 }; 3863 static int const rotrv_insn[4] = { 3864 OPC_UD2, OPC_UD2, OPC_VPRORVD, OPC_VPRORVQ 3865 }; 3866 static int const shlv_insn[4] = { 3867 OPC_UD2, OPC_VPSLLVW, OPC_VPSLLVD, OPC_VPSLLVQ 3868 }; 3869 static int const shrv_insn[4] = { 3870 OPC_UD2, OPC_VPSRLVW, OPC_VPSRLVD, OPC_VPSRLVQ 3871 }; 3872 static int const sarv_insn[4] = { 3873 OPC_UD2, OPC_VPSRAVW, OPC_VPSRAVD, OPC_VPSRAVQ 3874 }; 3875 static int const shls_insn[4] = { 3876 OPC_UD2, OPC_PSLLW, OPC_PSLLD, OPC_PSLLQ 3877 }; 3878 static int const shrs_insn[4] = { 3879 OPC_UD2, OPC_PSRLW, OPC_PSRLD, OPC_PSRLQ 3880 }; 3881 static int const sars_insn[4] = { 3882 OPC_UD2, OPC_PSRAW, OPC_PSRAD, OPC_VPSRAQ 3883 }; 3884 static int const vpshldi_insn[4] = { 3885 OPC_UD2, OPC_VPSHLDW, OPC_VPSHLDD, OPC_VPSHLDQ 3886 }; 3887 static int const vpshldv_insn[4] = { 3888 OPC_UD2, OPC_VPSHLDVW, OPC_VPSHLDVD, OPC_VPSHLDVQ 3889 }; 3890 static int const vpshrdv_insn[4] = { 3891 OPC_UD2, OPC_VPSHRDVW, OPC_VPSHRDVD, OPC_VPSHRDVQ 3892 }; 3893 static int const abs_insn[4] = { 3894 OPC_PABSB, OPC_PABSW, OPC_PABSD, OPC_VPABSQ 3895 }; 3896 3897 TCGType type = vecl + TCG_TYPE_V64; 3898 int insn, sub; 3899 TCGArg a0, a1, a2, a3; 3900 3901 a0 = args[0]; 3902 a1 = args[1]; 3903 a2 = args[2]; 3904 3905 switch (opc) { 3906 case INDEX_op_add_vec: 3907 insn = add_insn[vece]; 3908 goto gen_simd; 3909 case INDEX_op_ssadd_vec: 3910 insn = ssadd_insn[vece]; 3911 goto gen_simd; 3912 case INDEX_op_usadd_vec: 3913 insn = usadd_insn[vece]; 3914 goto gen_simd; 3915 case INDEX_op_sub_vec: 3916 insn = sub_insn[vece]; 3917 goto gen_simd; 3918 case INDEX_op_sssub_vec: 3919 insn = sssub_insn[vece]; 3920 goto gen_simd; 3921 case INDEX_op_ussub_vec: 3922 insn = ussub_insn[vece]; 3923 goto gen_simd; 3924 case INDEX_op_mul_vec: 3925 insn = mul_insn[vece]; 3926 goto gen_simd; 3927 case INDEX_op_and_vec: 3928 insn = OPC_PAND; 3929 goto gen_simd; 3930 case INDEX_op_or_vec: 3931 insn = OPC_POR; 3932 goto gen_simd; 3933 case INDEX_op_xor_vec: 3934 insn = OPC_PXOR; 3935 goto gen_simd; 3936 case INDEX_op_smin_vec: 3937 insn = smin_insn[vece]; 3938 goto gen_simd; 3939 case INDEX_op_umin_vec: 3940 insn = umin_insn[vece]; 3941 goto gen_simd; 3942 case INDEX_op_smax_vec: 3943 insn = smax_insn[vece]; 3944 goto gen_simd; 3945 case INDEX_op_umax_vec: 3946 insn = umax_insn[vece]; 3947 goto gen_simd; 3948 case INDEX_op_shlv_vec: 3949 insn = shlv_insn[vece]; 3950 goto gen_simd; 3951 case INDEX_op_shrv_vec: 3952 insn = shrv_insn[vece]; 3953 goto gen_simd; 3954 case INDEX_op_sarv_vec: 3955 insn = sarv_insn[vece]; 3956 goto gen_simd; 3957 case INDEX_op_rotlv_vec: 3958 insn = rotlv_insn[vece]; 3959 goto gen_simd; 3960 case INDEX_op_rotrv_vec: 3961 insn = rotrv_insn[vece]; 3962 goto gen_simd; 3963 case INDEX_op_shls_vec: 3964 insn = shls_insn[vece]; 3965 goto gen_simd; 3966 case INDEX_op_shrs_vec: 3967 insn = shrs_insn[vece]; 3968 goto gen_simd; 3969 case INDEX_op_sars_vec: 3970 insn = sars_insn[vece]; 3971 goto gen_simd; 3972 case INDEX_op_x86_punpckl_vec: 3973 insn = punpckl_insn[vece]; 3974 goto gen_simd; 3975 case INDEX_op_x86_punpckh_vec: 3976 insn = punpckh_insn[vece]; 3977 goto gen_simd; 3978 case INDEX_op_x86_packss_vec: 3979 insn = packss_insn[vece]; 3980 goto gen_simd; 3981 case INDEX_op_x86_packus_vec: 3982 insn = packus_insn[vece]; 3983 goto gen_simd; 3984 case INDEX_op_x86_vpshldv_vec: 3985 insn = vpshldv_insn[vece]; 3986 a1 = a2; 3987 a2 = args[3]; 3988 goto gen_simd; 3989 case INDEX_op_x86_vpshrdv_vec: 3990 insn = vpshrdv_insn[vece]; 3991 a1 = a2; 3992 a2 = args[3]; 3993 goto gen_simd; 3994#if TCG_TARGET_REG_BITS == 32 3995 case INDEX_op_dup2_vec: 3996 /* First merge the two 32-bit inputs to a single 64-bit element. */ 3997 tcg_out_vex_modrm(s, OPC_PUNPCKLDQ, a0, a1, a2); 3998 /* Then replicate the 64-bit elements across the rest of the vector. */ 3999 if (type != TCG_TYPE_V64) { 4000 tcg_out_dup_vec(s, type, MO_64, a0, a0); 4001 } 4002 break; 4003#endif 4004 case INDEX_op_abs_vec: 4005 insn = abs_insn[vece]; 4006 a2 = a1; 4007 a1 = 0; 4008 goto gen_simd; 4009 gen_simd: 4010 tcg_debug_assert(insn != OPC_UD2); 4011 tcg_out_vex_modrm_type(s, insn, a0, a1, a2, type); 4012 break; 4013 4014 case INDEX_op_cmp_vec: 4015 tcg_out_cmp_vec(s, type, vece, a0, a1, a2, args[3]); 4016 break; 4017 4018 case INDEX_op_cmpsel_vec: 4019 tcg_out_cmpsel_vec(s, type, vece, a0, a1, a2, 4020 args[3], args[4], args[5]); 4021 break; 4022 4023 case INDEX_op_andc_vec: 4024 insn = OPC_PANDN; 4025 tcg_out_vex_modrm_type(s, insn, a0, a2, a1, type); 4026 break; 4027 4028 case INDEX_op_shli_vec: 4029 insn = shift_imm_insn[vece]; 4030 sub = 6; 4031 goto gen_shift; 4032 case INDEX_op_shri_vec: 4033 insn = shift_imm_insn[vece]; 4034 sub = 2; 4035 goto gen_shift; 4036 case INDEX_op_sari_vec: 4037 if (vece == MO_64) { 4038 insn = OPC_PSHIFTD_Ib | P_VEXW | P_EVEX; 4039 } else { 4040 insn = shift_imm_insn[vece]; 4041 } 4042 sub = 4; 4043 goto gen_shift; 4044 case INDEX_op_rotli_vec: 4045 insn = OPC_PSHIFTD_Ib | P_EVEX; /* VPROL[DQ] */ 4046 if (vece == MO_64) { 4047 insn |= P_VEXW; 4048 } 4049 sub = 1; 4050 goto gen_shift; 4051 gen_shift: 4052 tcg_debug_assert(vece != MO_8); 4053 tcg_out_vex_modrm_type(s, insn, sub, a0, a1, type); 4054 tcg_out8(s, a2); 4055 break; 4056 4057 case INDEX_op_ld_vec: 4058 tcg_out_ld(s, type, a0, a1, a2); 4059 break; 4060 case INDEX_op_st_vec: 4061 tcg_out_st(s, type, a0, a1, a2); 4062 break; 4063 case INDEX_op_dupm_vec: 4064 tcg_out_dupm_vec(s, type, vece, a0, a1, a2); 4065 break; 4066 4067 case INDEX_op_x86_shufps_vec: 4068 insn = OPC_SHUFPS; 4069 sub = args[3]; 4070 goto gen_simd_imm8; 4071 case INDEX_op_x86_blend_vec: 4072 if (vece == MO_16) { 4073 insn = OPC_PBLENDW; 4074 } else if (vece == MO_32) { 4075 insn = (have_avx2 ? OPC_VPBLENDD : OPC_BLENDPS); 4076 } else { 4077 g_assert_not_reached(); 4078 } 4079 sub = args[3]; 4080 goto gen_simd_imm8; 4081 case INDEX_op_x86_vperm2i128_vec: 4082 insn = OPC_VPERM2I128; 4083 sub = args[3]; 4084 goto gen_simd_imm8; 4085 case INDEX_op_x86_vpshldi_vec: 4086 insn = vpshldi_insn[vece]; 4087 sub = args[3]; 4088 goto gen_simd_imm8; 4089 4090 case INDEX_op_not_vec: 4091 insn = OPC_VPTERNLOGQ; 4092 a2 = a1; 4093 sub = 0x33; /* !B */ 4094 goto gen_simd_imm8; 4095 case INDEX_op_nor_vec: 4096 insn = OPC_VPTERNLOGQ; 4097 sub = 0x11; /* norCB */ 4098 goto gen_simd_imm8; 4099 case INDEX_op_nand_vec: 4100 insn = OPC_VPTERNLOGQ; 4101 sub = 0x77; /* nandCB */ 4102 goto gen_simd_imm8; 4103 case INDEX_op_eqv_vec: 4104 insn = OPC_VPTERNLOGQ; 4105 sub = 0x99; /* xnorCB */ 4106 goto gen_simd_imm8; 4107 case INDEX_op_orc_vec: 4108 insn = OPC_VPTERNLOGQ; 4109 sub = 0xdd; /* orB!C */ 4110 goto gen_simd_imm8; 4111 4112 case INDEX_op_bitsel_vec: 4113 insn = OPC_VPTERNLOGQ; 4114 a3 = args[3]; 4115 if (a0 == a1) { 4116 a1 = a2; 4117 a2 = a3; 4118 sub = 0xca; /* A?B:C */ 4119 } else if (a0 == a2) { 4120 a2 = a3; 4121 sub = 0xe2; /* B?A:C */ 4122 } else { 4123 tcg_out_mov(s, type, a0, a3); 4124 sub = 0xb8; /* B?C:A */ 4125 } 4126 goto gen_simd_imm8; 4127 4128 gen_simd_imm8: 4129 tcg_debug_assert(insn != OPC_UD2); 4130 tcg_out_vex_modrm_type(s, insn, a0, a1, a2, type); 4131 tcg_out8(s, sub); 4132 break; 4133 4134 case INDEX_op_x86_psrldq_vec: 4135 tcg_out_vex_modrm(s, OPC_GRP14, 3, a0, a1); 4136 tcg_out8(s, a2); 4137 break; 4138 4139 case INDEX_op_mov_vec: /* Always emitted via tcg_out_mov. */ 4140 case INDEX_op_dup_vec: /* Always emitted via tcg_out_dup_vec. */ 4141 default: 4142 g_assert_not_reached(); 4143 } 4144} 4145 4146static TCGConstraintSetIndex 4147tcg_target_op_def(TCGOpcode op, TCGType type, unsigned flags) 4148{ 4149 switch (op) { 4150 case INDEX_op_ld_vec: 4151 case INDEX_op_dupm_vec: 4152 return C_O1_I1(x, r); 4153 4154 case INDEX_op_st_vec: 4155 return C_O0_I2(x, r); 4156 4157 case INDEX_op_add_vec: 4158 case INDEX_op_sub_vec: 4159 case INDEX_op_mul_vec: 4160 case INDEX_op_and_vec: 4161 case INDEX_op_or_vec: 4162 case INDEX_op_xor_vec: 4163 case INDEX_op_andc_vec: 4164 case INDEX_op_orc_vec: 4165 case INDEX_op_nand_vec: 4166 case INDEX_op_nor_vec: 4167 case INDEX_op_eqv_vec: 4168 case INDEX_op_ssadd_vec: 4169 case INDEX_op_usadd_vec: 4170 case INDEX_op_sssub_vec: 4171 case INDEX_op_ussub_vec: 4172 case INDEX_op_smin_vec: 4173 case INDEX_op_umin_vec: 4174 case INDEX_op_smax_vec: 4175 case INDEX_op_umax_vec: 4176 case INDEX_op_shlv_vec: 4177 case INDEX_op_shrv_vec: 4178 case INDEX_op_sarv_vec: 4179 case INDEX_op_rotlv_vec: 4180 case INDEX_op_rotrv_vec: 4181 case INDEX_op_shls_vec: 4182 case INDEX_op_shrs_vec: 4183 case INDEX_op_sars_vec: 4184 case INDEX_op_cmp_vec: 4185 case INDEX_op_x86_shufps_vec: 4186 case INDEX_op_x86_blend_vec: 4187 case INDEX_op_x86_packss_vec: 4188 case INDEX_op_x86_packus_vec: 4189 case INDEX_op_x86_vperm2i128_vec: 4190 case INDEX_op_x86_punpckl_vec: 4191 case INDEX_op_x86_punpckh_vec: 4192 case INDEX_op_x86_vpshldi_vec: 4193#if TCG_TARGET_REG_BITS == 32 4194 case INDEX_op_dup2_vec: 4195#endif 4196 return C_O1_I2(x, x, x); 4197 4198 case INDEX_op_abs_vec: 4199 case INDEX_op_dup_vec: 4200 case INDEX_op_not_vec: 4201 case INDEX_op_shli_vec: 4202 case INDEX_op_shri_vec: 4203 case INDEX_op_sari_vec: 4204 case INDEX_op_rotli_vec: 4205 case INDEX_op_x86_psrldq_vec: 4206 return C_O1_I1(x, x); 4207 4208 case INDEX_op_x86_vpshldv_vec: 4209 case INDEX_op_x86_vpshrdv_vec: 4210 return C_O1_I3(x, 0, x, x); 4211 4212 case INDEX_op_bitsel_vec: 4213 return C_O1_I3(x, x, x, x); 4214 case INDEX_op_cmpsel_vec: 4215 return C_O1_I4(x, x, x, xO, x); 4216 4217 default: 4218 return C_NotImplemented; 4219 } 4220} 4221 4222int tcg_can_emit_vec_op(TCGOpcode opc, TCGType type, unsigned vece) 4223{ 4224 switch (opc) { 4225 case INDEX_op_add_vec: 4226 case INDEX_op_sub_vec: 4227 case INDEX_op_and_vec: 4228 case INDEX_op_or_vec: 4229 case INDEX_op_xor_vec: 4230 case INDEX_op_andc_vec: 4231 case INDEX_op_orc_vec: 4232 case INDEX_op_nand_vec: 4233 case INDEX_op_nor_vec: 4234 case INDEX_op_eqv_vec: 4235 case INDEX_op_not_vec: 4236 case INDEX_op_bitsel_vec: 4237 return 1; 4238 case INDEX_op_cmp_vec: 4239 case INDEX_op_cmpsel_vec: 4240 return -1; 4241 4242 case INDEX_op_rotli_vec: 4243 return have_avx512vl && vece >= MO_32 ? 1 : -1; 4244 4245 case INDEX_op_shli_vec: 4246 case INDEX_op_shri_vec: 4247 /* We must expand the operation for MO_8. */ 4248 return vece == MO_8 ? -1 : 1; 4249 4250 case INDEX_op_sari_vec: 4251 switch (vece) { 4252 case MO_8: 4253 return -1; 4254 case MO_16: 4255 case MO_32: 4256 return 1; 4257 case MO_64: 4258 if (have_avx512vl) { 4259 return 1; 4260 } 4261 /* 4262 * We can emulate this for MO_64, but it does not pay off 4263 * unless we're producing at least 4 values. 4264 */ 4265 return type >= TCG_TYPE_V256 ? -1 : 0; 4266 } 4267 return 0; 4268 4269 case INDEX_op_shls_vec: 4270 case INDEX_op_shrs_vec: 4271 return vece >= MO_16; 4272 case INDEX_op_sars_vec: 4273 switch (vece) { 4274 case MO_16: 4275 case MO_32: 4276 return 1; 4277 case MO_64: 4278 return have_avx512vl; 4279 } 4280 return 0; 4281 case INDEX_op_rotls_vec: 4282 return vece >= MO_16 ? -1 : 0; 4283 4284 case INDEX_op_shlv_vec: 4285 case INDEX_op_shrv_vec: 4286 switch (vece) { 4287 case MO_16: 4288 return have_avx512bw; 4289 case MO_32: 4290 case MO_64: 4291 return have_avx2; 4292 } 4293 return 0; 4294 case INDEX_op_sarv_vec: 4295 switch (vece) { 4296 case MO_16: 4297 return have_avx512bw; 4298 case MO_32: 4299 return have_avx2; 4300 case MO_64: 4301 return have_avx512vl; 4302 } 4303 return 0; 4304 case INDEX_op_rotlv_vec: 4305 case INDEX_op_rotrv_vec: 4306 switch (vece) { 4307 case MO_16: 4308 return have_avx512vbmi2 ? -1 : 0; 4309 case MO_32: 4310 case MO_64: 4311 return have_avx512vl ? 1 : have_avx2 ? -1 : 0; 4312 } 4313 return 0; 4314 4315 case INDEX_op_mul_vec: 4316 switch (vece) { 4317 case MO_8: 4318 return -1; 4319 case MO_64: 4320 return have_avx512dq; 4321 } 4322 return 1; 4323 4324 case INDEX_op_ssadd_vec: 4325 case INDEX_op_usadd_vec: 4326 case INDEX_op_sssub_vec: 4327 case INDEX_op_ussub_vec: 4328 return vece <= MO_16; 4329 case INDEX_op_smin_vec: 4330 case INDEX_op_smax_vec: 4331 case INDEX_op_umin_vec: 4332 case INDEX_op_umax_vec: 4333 case INDEX_op_abs_vec: 4334 return vece <= MO_32 || have_avx512vl; 4335 4336 default: 4337 return 0; 4338 } 4339} 4340 4341static void expand_vec_shi(TCGType type, unsigned vece, bool right, 4342 TCGv_vec v0, TCGv_vec v1, TCGArg imm) 4343{ 4344 uint8_t mask; 4345 4346 tcg_debug_assert(vece == MO_8); 4347 if (right) { 4348 mask = 0xff >> imm; 4349 tcg_gen_shri_vec(MO_16, v0, v1, imm); 4350 } else { 4351 mask = 0xff << imm; 4352 tcg_gen_shli_vec(MO_16, v0, v1, imm); 4353 } 4354 tcg_gen_and_vec(MO_8, v0, v0, tcg_constant_vec(type, MO_8, mask)); 4355} 4356 4357static void expand_vec_sari(TCGType type, unsigned vece, 4358 TCGv_vec v0, TCGv_vec v1, TCGArg imm) 4359{ 4360 TCGv_vec t1, t2; 4361 4362 switch (vece) { 4363 case MO_8: 4364 /* Unpack to 16-bit, shift, and repack. */ 4365 t1 = tcg_temp_new_vec(type); 4366 t2 = tcg_temp_new_vec(type); 4367 vec_gen_3(INDEX_op_x86_punpckl_vec, type, MO_8, 4368 tcgv_vec_arg(t1), tcgv_vec_arg(v1), tcgv_vec_arg(v1)); 4369 vec_gen_3(INDEX_op_x86_punpckh_vec, type, MO_8, 4370 tcgv_vec_arg(t2), tcgv_vec_arg(v1), tcgv_vec_arg(v1)); 4371 tcg_gen_sari_vec(MO_16, t1, t1, imm + 8); 4372 tcg_gen_sari_vec(MO_16, t2, t2, imm + 8); 4373 vec_gen_3(INDEX_op_x86_packss_vec, type, MO_8, 4374 tcgv_vec_arg(v0), tcgv_vec_arg(t1), tcgv_vec_arg(t2)); 4375 tcg_temp_free_vec(t1); 4376 tcg_temp_free_vec(t2); 4377 break; 4378 4379 case MO_64: 4380 t1 = tcg_temp_new_vec(type); 4381 if (imm <= 32) { 4382 /* 4383 * We can emulate a small sign extend by performing an arithmetic 4384 * 32-bit shift and overwriting the high half of a 64-bit logical 4385 * shift. Note that the ISA says shift of 32 is valid, but TCG 4386 * does not, so we have to bound the smaller shift -- we get the 4387 * same result in the high half either way. 4388 */ 4389 tcg_gen_sari_vec(MO_32, t1, v1, MIN(imm, 31)); 4390 tcg_gen_shri_vec(MO_64, v0, v1, imm); 4391 vec_gen_4(INDEX_op_x86_blend_vec, type, MO_32, 4392 tcgv_vec_arg(v0), tcgv_vec_arg(v0), 4393 tcgv_vec_arg(t1), 0xaa); 4394 } else { 4395 /* Otherwise we will need to use a compare vs 0 to produce 4396 * the sign-extend, shift and merge. 4397 */ 4398 tcg_gen_cmp_vec(TCG_COND_GT, MO_64, t1, 4399 tcg_constant_vec(type, MO_64, 0), v1); 4400 tcg_gen_shri_vec(MO_64, v0, v1, imm); 4401 tcg_gen_shli_vec(MO_64, t1, t1, 64 - imm); 4402 tcg_gen_or_vec(MO_64, v0, v0, t1); 4403 } 4404 tcg_temp_free_vec(t1); 4405 break; 4406 4407 default: 4408 g_assert_not_reached(); 4409 } 4410} 4411 4412static void expand_vec_rotli(TCGType type, unsigned vece, 4413 TCGv_vec v0, TCGv_vec v1, TCGArg imm) 4414{ 4415 TCGv_vec t; 4416 4417 if (vece != MO_8 && have_avx512vbmi2) { 4418 vec_gen_4(INDEX_op_x86_vpshldi_vec, type, vece, 4419 tcgv_vec_arg(v0), tcgv_vec_arg(v1), tcgv_vec_arg(v1), imm); 4420 return; 4421 } 4422 4423 t = tcg_temp_new_vec(type); 4424 tcg_gen_shli_vec(vece, t, v1, imm); 4425 tcg_gen_shri_vec(vece, v0, v1, (8 << vece) - imm); 4426 tcg_gen_or_vec(vece, v0, v0, t); 4427 tcg_temp_free_vec(t); 4428} 4429 4430static void expand_vec_rotv(TCGType type, unsigned vece, TCGv_vec v0, 4431 TCGv_vec v1, TCGv_vec sh, bool right) 4432{ 4433 TCGv_vec t; 4434 4435 if (have_avx512vbmi2) { 4436 vec_gen_4(right ? INDEX_op_x86_vpshrdv_vec : INDEX_op_x86_vpshldv_vec, 4437 type, vece, tcgv_vec_arg(v0), tcgv_vec_arg(v1), 4438 tcgv_vec_arg(v1), tcgv_vec_arg(sh)); 4439 return; 4440 } 4441 4442 t = tcg_temp_new_vec(type); 4443 tcg_gen_dupi_vec(vece, t, 8 << vece); 4444 tcg_gen_sub_vec(vece, t, t, sh); 4445 if (right) { 4446 tcg_gen_shlv_vec(vece, t, v1, t); 4447 tcg_gen_shrv_vec(vece, v0, v1, sh); 4448 } else { 4449 tcg_gen_shrv_vec(vece, t, v1, t); 4450 tcg_gen_shlv_vec(vece, v0, v1, sh); 4451 } 4452 tcg_gen_or_vec(vece, v0, v0, t); 4453 tcg_temp_free_vec(t); 4454} 4455 4456static void expand_vec_rotls(TCGType type, unsigned vece, 4457 TCGv_vec v0, TCGv_vec v1, TCGv_i32 lsh) 4458{ 4459 TCGv_vec t = tcg_temp_new_vec(type); 4460 4461 tcg_debug_assert(vece != MO_8); 4462 4463 if (vece >= MO_32 ? have_avx512vl : have_avx512vbmi2) { 4464 tcg_gen_dup_i32_vec(vece, t, lsh); 4465 if (vece >= MO_32) { 4466 tcg_gen_rotlv_vec(vece, v0, v1, t); 4467 } else { 4468 expand_vec_rotv(type, vece, v0, v1, t, false); 4469 } 4470 } else { 4471 TCGv_i32 rsh = tcg_temp_new_i32(); 4472 4473 tcg_gen_neg_i32(rsh, lsh); 4474 tcg_gen_andi_i32(rsh, rsh, (8 << vece) - 1); 4475 tcg_gen_shls_vec(vece, t, v1, lsh); 4476 tcg_gen_shrs_vec(vece, v0, v1, rsh); 4477 tcg_gen_or_vec(vece, v0, v0, t); 4478 4479 tcg_temp_free_i32(rsh); 4480 } 4481 4482 tcg_temp_free_vec(t); 4483} 4484 4485static void expand_vec_mul(TCGType type, unsigned vece, 4486 TCGv_vec v0, TCGv_vec v1, TCGv_vec v2) 4487{ 4488 TCGv_vec t1, t2, t3, t4, zero; 4489 4490 tcg_debug_assert(vece == MO_8); 4491 4492 /* 4493 * Unpack v1 bytes to words, 0 | x. 4494 * Unpack v2 bytes to words, y | 0. 4495 * This leaves the 8-bit result, x * y, with 8 bits of right padding. 4496 * Shift logical right by 8 bits to clear the high 8 bytes before 4497 * using an unsigned saturated pack. 4498 * 4499 * The difference between the V64, V128 and V256 cases is merely how 4500 * we distribute the expansion between temporaries. 4501 */ 4502 switch (type) { 4503 case TCG_TYPE_V64: 4504 t1 = tcg_temp_new_vec(TCG_TYPE_V128); 4505 t2 = tcg_temp_new_vec(TCG_TYPE_V128); 4506 zero = tcg_constant_vec(TCG_TYPE_V128, MO_8, 0); 4507 vec_gen_3(INDEX_op_x86_punpckl_vec, TCG_TYPE_V128, MO_8, 4508 tcgv_vec_arg(t1), tcgv_vec_arg(v1), tcgv_vec_arg(zero)); 4509 vec_gen_3(INDEX_op_x86_punpckl_vec, TCG_TYPE_V128, MO_8, 4510 tcgv_vec_arg(t2), tcgv_vec_arg(zero), tcgv_vec_arg(v2)); 4511 tcg_gen_mul_vec(MO_16, t1, t1, t2); 4512 tcg_gen_shri_vec(MO_16, t1, t1, 8); 4513 vec_gen_3(INDEX_op_x86_packus_vec, TCG_TYPE_V128, MO_8, 4514 tcgv_vec_arg(v0), tcgv_vec_arg(t1), tcgv_vec_arg(t1)); 4515 tcg_temp_free_vec(t1); 4516 tcg_temp_free_vec(t2); 4517 break; 4518 4519 case TCG_TYPE_V128: 4520 case TCG_TYPE_V256: 4521 t1 = tcg_temp_new_vec(type); 4522 t2 = tcg_temp_new_vec(type); 4523 t3 = tcg_temp_new_vec(type); 4524 t4 = tcg_temp_new_vec(type); 4525 zero = tcg_constant_vec(TCG_TYPE_V128, MO_8, 0); 4526 vec_gen_3(INDEX_op_x86_punpckl_vec, type, MO_8, 4527 tcgv_vec_arg(t1), tcgv_vec_arg(v1), tcgv_vec_arg(zero)); 4528 vec_gen_3(INDEX_op_x86_punpckl_vec, type, MO_8, 4529 tcgv_vec_arg(t2), tcgv_vec_arg(zero), tcgv_vec_arg(v2)); 4530 vec_gen_3(INDEX_op_x86_punpckh_vec, type, MO_8, 4531 tcgv_vec_arg(t3), tcgv_vec_arg(v1), tcgv_vec_arg(zero)); 4532 vec_gen_3(INDEX_op_x86_punpckh_vec, type, MO_8, 4533 tcgv_vec_arg(t4), tcgv_vec_arg(zero), tcgv_vec_arg(v2)); 4534 tcg_gen_mul_vec(MO_16, t1, t1, t2); 4535 tcg_gen_mul_vec(MO_16, t3, t3, t4); 4536 tcg_gen_shri_vec(MO_16, t1, t1, 8); 4537 tcg_gen_shri_vec(MO_16, t3, t3, 8); 4538 vec_gen_3(INDEX_op_x86_packus_vec, type, MO_8, 4539 tcgv_vec_arg(v0), tcgv_vec_arg(t1), tcgv_vec_arg(t3)); 4540 tcg_temp_free_vec(t1); 4541 tcg_temp_free_vec(t2); 4542 tcg_temp_free_vec(t3); 4543 tcg_temp_free_vec(t4); 4544 break; 4545 4546 default: 4547 g_assert_not_reached(); 4548 } 4549} 4550 4551static TCGCond expand_vec_cond(TCGType type, unsigned vece, 4552 TCGArg *a1, TCGArg *a2, TCGCond cond) 4553{ 4554 /* 4555 * Without AVX512, there are no 64-bit unsigned comparisons. 4556 * We must bias the inputs so that they become signed. 4557 * All other swapping and inversion are handled during code generation. 4558 */ 4559 if (vece == MO_64 && !have_avx512dq && is_unsigned_cond(cond)) { 4560 TCGv_vec v1 = temp_tcgv_vec(arg_temp(*a1)); 4561 TCGv_vec v2 = temp_tcgv_vec(arg_temp(*a2)); 4562 TCGv_vec t1 = tcg_temp_new_vec(type); 4563 TCGv_vec t2 = tcg_temp_new_vec(type); 4564 TCGv_vec t3 = tcg_constant_vec(type, vece, 1ull << ((8 << vece) - 1)); 4565 4566 tcg_gen_sub_vec(vece, t1, v1, t3); 4567 tcg_gen_sub_vec(vece, t2, v2, t3); 4568 *a1 = tcgv_vec_arg(t1); 4569 *a2 = tcgv_vec_arg(t2); 4570 cond = tcg_signed_cond(cond); 4571 } 4572 return cond; 4573} 4574 4575static void expand_vec_cmp(TCGType type, unsigned vece, TCGArg a0, 4576 TCGArg a1, TCGArg a2, TCGCond cond) 4577{ 4578 cond = expand_vec_cond(type, vece, &a1, &a2, cond); 4579 /* Expand directly; do not recurse. */ 4580 vec_gen_4(INDEX_op_cmp_vec, type, vece, a0, a1, a2, cond); 4581} 4582 4583static void expand_vec_cmpsel(TCGType type, unsigned vece, TCGArg a0, 4584 TCGArg a1, TCGArg a2, 4585 TCGArg a3, TCGArg a4, TCGCond cond) 4586{ 4587 cond = expand_vec_cond(type, vece, &a1, &a2, cond); 4588 /* Expand directly; do not recurse. */ 4589 vec_gen_6(INDEX_op_cmpsel_vec, type, vece, a0, a1, a2, a3, a4, cond); 4590} 4591 4592void tcg_expand_vec_op(TCGOpcode opc, TCGType type, unsigned vece, 4593 TCGArg a0, ...) 4594{ 4595 va_list va; 4596 TCGArg a1, a2, a3, a4, a5; 4597 TCGv_vec v0, v1, v2; 4598 4599 va_start(va, a0); 4600 a1 = va_arg(va, TCGArg); 4601 a2 = va_arg(va, TCGArg); 4602 v0 = temp_tcgv_vec(arg_temp(a0)); 4603 v1 = temp_tcgv_vec(arg_temp(a1)); 4604 4605 switch (opc) { 4606 case INDEX_op_shli_vec: 4607 expand_vec_shi(type, vece, false, v0, v1, a2); 4608 break; 4609 case INDEX_op_shri_vec: 4610 expand_vec_shi(type, vece, true, v0, v1, a2); 4611 break; 4612 case INDEX_op_sari_vec: 4613 expand_vec_sari(type, vece, v0, v1, a2); 4614 break; 4615 4616 case INDEX_op_rotli_vec: 4617 expand_vec_rotli(type, vece, v0, v1, a2); 4618 break; 4619 4620 case INDEX_op_rotls_vec: 4621 expand_vec_rotls(type, vece, v0, v1, temp_tcgv_i32(arg_temp(a2))); 4622 break; 4623 4624 case INDEX_op_rotlv_vec: 4625 v2 = temp_tcgv_vec(arg_temp(a2)); 4626 expand_vec_rotv(type, vece, v0, v1, v2, false); 4627 break; 4628 case INDEX_op_rotrv_vec: 4629 v2 = temp_tcgv_vec(arg_temp(a2)); 4630 expand_vec_rotv(type, vece, v0, v1, v2, true); 4631 break; 4632 4633 case INDEX_op_mul_vec: 4634 v2 = temp_tcgv_vec(arg_temp(a2)); 4635 expand_vec_mul(type, vece, v0, v1, v2); 4636 break; 4637 4638 case INDEX_op_cmp_vec: 4639 a3 = va_arg(va, TCGArg); 4640 expand_vec_cmp(type, vece, a0, a1, a2, a3); 4641 break; 4642 4643 case INDEX_op_cmpsel_vec: 4644 a3 = va_arg(va, TCGArg); 4645 a4 = va_arg(va, TCGArg); 4646 a5 = va_arg(va, TCGArg); 4647 expand_vec_cmpsel(type, vece, a0, a1, a2, a3, a4, a5); 4648 break; 4649 4650 default: 4651 break; 4652 } 4653 4654 va_end(va); 4655} 4656 4657static const int tcg_target_callee_save_regs[] = { 4658#if TCG_TARGET_REG_BITS == 64 4659 TCG_REG_RBP, 4660 TCG_REG_RBX, 4661#if defined(_WIN64) 4662 TCG_REG_RDI, 4663 TCG_REG_RSI, 4664#endif 4665 TCG_REG_R12, 4666 TCG_REG_R13, 4667 TCG_REG_R14, /* Currently used for the global env. */ 4668 TCG_REG_R15, 4669#else 4670 TCG_REG_EBP, /* Currently used for the global env. */ 4671 TCG_REG_EBX, 4672 TCG_REG_ESI, 4673 TCG_REG_EDI, 4674#endif 4675}; 4676 4677/* Compute frame size via macros, to share between tcg_target_qemu_prologue 4678 and tcg_register_jit. */ 4679 4680#define PUSH_SIZE \ 4681 ((1 + ARRAY_SIZE(tcg_target_callee_save_regs)) \ 4682 * (TCG_TARGET_REG_BITS / 8)) 4683 4684#define FRAME_SIZE \ 4685 ((PUSH_SIZE \ 4686 + TCG_STATIC_CALL_ARGS_SIZE \ 4687 + CPU_TEMP_BUF_NLONGS * sizeof(long) \ 4688 + TCG_TARGET_STACK_ALIGN - 1) \ 4689 & ~(TCG_TARGET_STACK_ALIGN - 1)) 4690 4691/* Generate global QEMU prologue and epilogue code */ 4692static void tcg_target_qemu_prologue(TCGContext *s) 4693{ 4694 int i, stack_addend; 4695 4696 /* TB prologue */ 4697 4698 /* Reserve some stack space, also for TCG temps. */ 4699 stack_addend = FRAME_SIZE - PUSH_SIZE; 4700 tcg_set_frame(s, TCG_REG_CALL_STACK, TCG_STATIC_CALL_ARGS_SIZE, 4701 CPU_TEMP_BUF_NLONGS * sizeof(long)); 4702 4703 /* Save all callee saved registers. */ 4704 for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) { 4705 tcg_out_push(s, tcg_target_callee_save_regs[i]); 4706 } 4707 4708 if (!tcg_use_softmmu && guest_base) { 4709 int seg = setup_guest_base_seg(); 4710 if (seg != 0) { 4711 x86_guest_base.seg = seg; 4712 } else if (guest_base == (int32_t)guest_base) { 4713 x86_guest_base.ofs = guest_base; 4714 } else { 4715 assert(TCG_TARGET_REG_BITS == 64); 4716 /* Choose R12 because, as a base, it requires a SIB byte. */ 4717 x86_guest_base.index = TCG_REG_R12; 4718 tcg_out_movi(s, TCG_TYPE_PTR, x86_guest_base.index, guest_base); 4719 tcg_regset_set_reg(s->reserved_regs, x86_guest_base.index); 4720 } 4721 } 4722 4723 if (TCG_TARGET_REG_BITS == 32) { 4724 tcg_out_ld(s, TCG_TYPE_PTR, TCG_AREG0, TCG_REG_ESP, 4725 (ARRAY_SIZE(tcg_target_callee_save_regs) + 1) * 4); 4726 tcg_out_addi(s, TCG_REG_ESP, -stack_addend); 4727 /* jmp *tb. */ 4728 tcg_out_modrm_offset(s, OPC_GRP5, EXT5_JMPN_Ev, TCG_REG_ESP, 4729 (ARRAY_SIZE(tcg_target_callee_save_regs) + 2) * 4 4730 + stack_addend); 4731 } else { 4732 tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]); 4733 tcg_out_addi(s, TCG_REG_ESP, -stack_addend); 4734 /* jmp *tb. */ 4735 tcg_out_modrm(s, OPC_GRP5, EXT5_JMPN_Ev, tcg_target_call_iarg_regs[1]); 4736 } 4737 4738 /* 4739 * Return path for goto_ptr. Set return value to 0, a-la exit_tb, 4740 * and fall through to the rest of the epilogue. 4741 */ 4742 tcg_code_gen_epilogue = tcg_splitwx_to_rx(s->code_ptr); 4743 tcg_out_movi(s, TCG_TYPE_REG, TCG_REG_EAX, 0); 4744 4745 /* TB epilogue */ 4746 tb_ret_addr = tcg_splitwx_to_rx(s->code_ptr); 4747 4748 tcg_out_addi(s, TCG_REG_CALL_STACK, stack_addend); 4749 4750 if (have_avx2) { 4751 tcg_out_vex_opc(s, OPC_VZEROUPPER, 0, 0, 0, 0); 4752 } 4753 for (i = ARRAY_SIZE(tcg_target_callee_save_regs) - 1; i >= 0; i--) { 4754 tcg_out_pop(s, tcg_target_callee_save_regs[i]); 4755 } 4756 tcg_out_opc(s, OPC_RET, 0, 0, 0); 4757} 4758 4759static void tcg_out_tb_start(TCGContext *s) 4760{ 4761 /* nothing to do */ 4762} 4763 4764static void tcg_out_nop_fill(tcg_insn_unit *p, int count) 4765{ 4766 memset(p, 0x90, count); 4767} 4768 4769static void tcg_target_init(TCGContext *s) 4770{ 4771 tcg_target_available_regs[TCG_TYPE_I32] = ALL_GENERAL_REGS; 4772 if (TCG_TARGET_REG_BITS == 64) { 4773 tcg_target_available_regs[TCG_TYPE_I64] = ALL_GENERAL_REGS; 4774 } 4775 if (have_avx1) { 4776 tcg_target_available_regs[TCG_TYPE_V64] = ALL_VECTOR_REGS; 4777 tcg_target_available_regs[TCG_TYPE_V128] = ALL_VECTOR_REGS; 4778 } 4779 if (have_avx2) { 4780 tcg_target_available_regs[TCG_TYPE_V256] = ALL_VECTOR_REGS; 4781 } 4782 4783 tcg_target_call_clobber_regs = ALL_VECTOR_REGS; 4784 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_EAX); 4785 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_EDX); 4786 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_ECX); 4787 if (TCG_TARGET_REG_BITS == 64) { 4788#if !defined(_WIN64) 4789 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_RDI); 4790 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_RSI); 4791#endif 4792 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R8); 4793 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R9); 4794 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R10); 4795 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R11); 4796 } 4797 4798 s->reserved_regs = 0; 4799 tcg_regset_set_reg(s->reserved_regs, TCG_REG_CALL_STACK); 4800 tcg_regset_set_reg(s->reserved_regs, TCG_TMP_VEC); 4801#ifdef _WIN64 4802 /* These are call saved, and we don't save them, so don't use them. */ 4803 tcg_regset_set_reg(s->reserved_regs, TCG_REG_XMM6); 4804 tcg_regset_set_reg(s->reserved_regs, TCG_REG_XMM7); 4805 tcg_regset_set_reg(s->reserved_regs, TCG_REG_XMM8); 4806 tcg_regset_set_reg(s->reserved_regs, TCG_REG_XMM9); 4807 tcg_regset_set_reg(s->reserved_regs, TCG_REG_XMM10); 4808 tcg_regset_set_reg(s->reserved_regs, TCG_REG_XMM11); 4809 tcg_regset_set_reg(s->reserved_regs, TCG_REG_XMM12); 4810 tcg_regset_set_reg(s->reserved_regs, TCG_REG_XMM13); 4811 tcg_regset_set_reg(s->reserved_regs, TCG_REG_XMM14); 4812 tcg_regset_set_reg(s->reserved_regs, TCG_REG_XMM15); 4813#endif 4814} 4815 4816typedef struct { 4817 DebugFrameHeader h; 4818 uint8_t fde_def_cfa[4]; 4819 uint8_t fde_reg_ofs[14]; 4820} DebugFrame; 4821 4822/* We're expecting a 2 byte uleb128 encoded value. */ 4823QEMU_BUILD_BUG_ON(FRAME_SIZE >= (1 << 14)); 4824 4825#if !defined(__ELF__) 4826 /* Host machine without ELF. */ 4827#elif TCG_TARGET_REG_BITS == 64 4828#define ELF_HOST_MACHINE EM_X86_64 4829static const DebugFrame debug_frame = { 4830 .h.cie.len = sizeof(DebugFrameCIE)-4, /* length after .len member */ 4831 .h.cie.id = -1, 4832 .h.cie.version = 1, 4833 .h.cie.code_align = 1, 4834 .h.cie.data_align = 0x78, /* sleb128 -8 */ 4835 .h.cie.return_column = 16, 4836 4837 /* Total FDE size does not include the "len" member. */ 4838 .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset), 4839 4840 .fde_def_cfa = { 4841 12, 7, /* DW_CFA_def_cfa %rsp, ... */ 4842 (FRAME_SIZE & 0x7f) | 0x80, /* ... uleb128 FRAME_SIZE */ 4843 (FRAME_SIZE >> 7) 4844 }, 4845 .fde_reg_ofs = { 4846 0x90, 1, /* DW_CFA_offset, %rip, -8 */ 4847 /* The following ordering must match tcg_target_callee_save_regs. */ 4848 0x86, 2, /* DW_CFA_offset, %rbp, -16 */ 4849 0x83, 3, /* DW_CFA_offset, %rbx, -24 */ 4850 0x8c, 4, /* DW_CFA_offset, %r12, -32 */ 4851 0x8d, 5, /* DW_CFA_offset, %r13, -40 */ 4852 0x8e, 6, /* DW_CFA_offset, %r14, -48 */ 4853 0x8f, 7, /* DW_CFA_offset, %r15, -56 */ 4854 } 4855}; 4856#else 4857#define ELF_HOST_MACHINE EM_386 4858static const DebugFrame debug_frame = { 4859 .h.cie.len = sizeof(DebugFrameCIE)-4, /* length after .len member */ 4860 .h.cie.id = -1, 4861 .h.cie.version = 1, 4862 .h.cie.code_align = 1, 4863 .h.cie.data_align = 0x7c, /* sleb128 -4 */ 4864 .h.cie.return_column = 8, 4865 4866 /* Total FDE size does not include the "len" member. */ 4867 .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset), 4868 4869 .fde_def_cfa = { 4870 12, 4, /* DW_CFA_def_cfa %esp, ... */ 4871 (FRAME_SIZE & 0x7f) | 0x80, /* ... uleb128 FRAME_SIZE */ 4872 (FRAME_SIZE >> 7) 4873 }, 4874 .fde_reg_ofs = { 4875 0x88, 1, /* DW_CFA_offset, %eip, -4 */ 4876 /* The following ordering must match tcg_target_callee_save_regs. */ 4877 0x85, 2, /* DW_CFA_offset, %ebp, -8 */ 4878 0x83, 3, /* DW_CFA_offset, %ebx, -12 */ 4879 0x86, 4, /* DW_CFA_offset, %esi, -16 */ 4880 0x87, 5, /* DW_CFA_offset, %edi, -20 */ 4881 } 4882}; 4883#endif 4884 4885#if defined(ELF_HOST_MACHINE) 4886void tcg_register_jit(const void *buf, size_t buf_size) 4887{ 4888 tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame)); 4889} 4890#endif 4891