1/* 2 * Tiny Code Generator for QEMU 3 * 4 * Copyright (c) 2009 Ulrich Hecht <uli@suse.de> 5 * Copyright (c) 2009 Alexander Graf <agraf@suse.de> 6 * Copyright (c) 2010 Richard Henderson <rth@twiddle.net> 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining a copy 9 * of this software and associated documentation files (the "Software"), to deal 10 * in the Software without restriction, including without limitation the rights 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 * copies of the Software, and to permit persons to whom the Software is 13 * furnished to do so, subject to the following conditions: 14 * 15 * The above copyright notice and this permission notice shall be included in 16 * all copies or substantial portions of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 * THE SOFTWARE. 25 */ 26 27/* We only support generating code for 64-bit mode. */ 28#if TCG_TARGET_REG_BITS != 64 29#error "unsupported code generation mode" 30#endif 31 32#include "../tcg-ldst.c.inc" 33#include "../tcg-pool.c.inc" 34#include "elf.h" 35 36#define TCG_CT_CONST_S16 (1 << 8) 37#define TCG_CT_CONST_S32 (1 << 9) 38#define TCG_CT_CONST_S33 (1 << 10) 39#define TCG_CT_CONST_ZERO (1 << 11) 40#define TCG_CT_CONST_P32 (1 << 12) 41#define TCG_CT_CONST_INV (1 << 13) 42#define TCG_CT_CONST_INVRISBG (1 << 14) 43 44#define ALL_GENERAL_REGS MAKE_64BIT_MASK(0, 16) 45#define ALL_VECTOR_REGS MAKE_64BIT_MASK(32, 32) 46 47/* Several places within the instruction set 0 means "no register" 48 rather than TCG_REG_R0. */ 49#define TCG_REG_NONE 0 50 51/* A scratch register that may be be used throughout the backend. */ 52#define TCG_TMP0 TCG_REG_R1 53 54#ifndef CONFIG_SOFTMMU 55#define TCG_GUEST_BASE_REG TCG_REG_R13 56#endif 57 58/* All of the following instructions are prefixed with their instruction 59 format, and are defined as 8- or 16-bit quantities, even when the two 60 halves of the 16-bit quantity may appear 32 bits apart in the insn. 61 This makes it easy to copy the values from the tables in Appendix B. */ 62typedef enum S390Opcode { 63 RIL_AFI = 0xc209, 64 RIL_AGFI = 0xc208, 65 RIL_ALFI = 0xc20b, 66 RIL_ALGFI = 0xc20a, 67 RIL_BRASL = 0xc005, 68 RIL_BRCL = 0xc004, 69 RIL_CFI = 0xc20d, 70 RIL_CGFI = 0xc20c, 71 RIL_CLFI = 0xc20f, 72 RIL_CLGFI = 0xc20e, 73 RIL_CLRL = 0xc60f, 74 RIL_CLGRL = 0xc60a, 75 RIL_CRL = 0xc60d, 76 RIL_CGRL = 0xc608, 77 RIL_IIHF = 0xc008, 78 RIL_IILF = 0xc009, 79 RIL_LARL = 0xc000, 80 RIL_LGFI = 0xc001, 81 RIL_LGRL = 0xc408, 82 RIL_LLIHF = 0xc00e, 83 RIL_LLILF = 0xc00f, 84 RIL_LRL = 0xc40d, 85 RIL_MSFI = 0xc201, 86 RIL_MSGFI = 0xc200, 87 RIL_NIHF = 0xc00a, 88 RIL_NILF = 0xc00b, 89 RIL_OIHF = 0xc00c, 90 RIL_OILF = 0xc00d, 91 RIL_SLFI = 0xc205, 92 RIL_SLGFI = 0xc204, 93 RIL_XIHF = 0xc006, 94 RIL_XILF = 0xc007, 95 96 RI_AGHI = 0xa70b, 97 RI_AHI = 0xa70a, 98 RI_BRC = 0xa704, 99 RI_CHI = 0xa70e, 100 RI_CGHI = 0xa70f, 101 RI_IIHH = 0xa500, 102 RI_IIHL = 0xa501, 103 RI_IILH = 0xa502, 104 RI_IILL = 0xa503, 105 RI_LGHI = 0xa709, 106 RI_LLIHH = 0xa50c, 107 RI_LLIHL = 0xa50d, 108 RI_LLILH = 0xa50e, 109 RI_LLILL = 0xa50f, 110 RI_MGHI = 0xa70d, 111 RI_MHI = 0xa70c, 112 RI_NIHH = 0xa504, 113 RI_NIHL = 0xa505, 114 RI_NILH = 0xa506, 115 RI_NILL = 0xa507, 116 RI_OIHH = 0xa508, 117 RI_OIHL = 0xa509, 118 RI_OILH = 0xa50a, 119 RI_OILL = 0xa50b, 120 RI_TMLL = 0xa701, 121 122 RIEb_CGRJ = 0xec64, 123 RIEb_CLGRJ = 0xec65, 124 RIEb_CLRJ = 0xec77, 125 RIEb_CRJ = 0xec76, 126 127 RIEc_CGIJ = 0xec7c, 128 RIEc_CIJ = 0xec7e, 129 RIEc_CLGIJ = 0xec7d, 130 RIEc_CLIJ = 0xec7f, 131 132 RIEf_RISBG = 0xec55, 133 134 RIEg_LOCGHI = 0xec46, 135 136 RRE_AGR = 0xb908, 137 RRE_ALGR = 0xb90a, 138 RRE_ALCR = 0xb998, 139 RRE_ALCGR = 0xb988, 140 RRE_ALGFR = 0xb91a, 141 RRE_CGR = 0xb920, 142 RRE_CLGR = 0xb921, 143 RRE_DLGR = 0xb987, 144 RRE_DLR = 0xb997, 145 RRE_DSGFR = 0xb91d, 146 RRE_DSGR = 0xb90d, 147 RRE_FLOGR = 0xb983, 148 RRE_LGBR = 0xb906, 149 RRE_LCGR = 0xb903, 150 RRE_LGFR = 0xb914, 151 RRE_LGHR = 0xb907, 152 RRE_LGR = 0xb904, 153 RRE_LLGCR = 0xb984, 154 RRE_LLGFR = 0xb916, 155 RRE_LLGHR = 0xb985, 156 RRE_LRVR = 0xb91f, 157 RRE_LRVGR = 0xb90f, 158 RRE_LTGR = 0xb902, 159 RRE_MLGR = 0xb986, 160 RRE_MSGR = 0xb90c, 161 RRE_MSR = 0xb252, 162 RRE_NGR = 0xb980, 163 RRE_OGR = 0xb981, 164 RRE_SGR = 0xb909, 165 RRE_SLGR = 0xb90b, 166 RRE_SLBR = 0xb999, 167 RRE_SLBGR = 0xb989, 168 RRE_XGR = 0xb982, 169 170 RRFa_MGRK = 0xb9ec, 171 RRFa_MSRKC = 0xb9fd, 172 RRFa_MSGRKC = 0xb9ed, 173 RRFa_NCRK = 0xb9f5, 174 RRFa_NCGRK = 0xb9e5, 175 RRFa_NNRK = 0xb974, 176 RRFa_NNGRK = 0xb964, 177 RRFa_NORK = 0xb976, 178 RRFa_NOGRK = 0xb966, 179 RRFa_NRK = 0xb9f4, 180 RRFa_NGRK = 0xb9e4, 181 RRFa_NXRK = 0xb977, 182 RRFa_NXGRK = 0xb967, 183 RRFa_OCRK = 0xb975, 184 RRFa_OCGRK = 0xb965, 185 RRFa_ORK = 0xb9f6, 186 RRFa_OGRK = 0xb9e6, 187 RRFa_SRK = 0xb9f9, 188 RRFa_SGRK = 0xb9e9, 189 RRFa_SLRK = 0xb9fb, 190 RRFa_SLGRK = 0xb9eb, 191 RRFa_XRK = 0xb9f7, 192 RRFa_XGRK = 0xb9e7, 193 194 RRFam_SELGR = 0xb9e3, 195 196 RRFc_LOCR = 0xb9f2, 197 RRFc_LOCGR = 0xb9e2, 198 RRFc_POPCNT = 0xb9e1, 199 200 RR_AR = 0x1a, 201 RR_ALR = 0x1e, 202 RR_BASR = 0x0d, 203 RR_BCR = 0x07, 204 RR_CLR = 0x15, 205 RR_CR = 0x19, 206 RR_DR = 0x1d, 207 RR_LCR = 0x13, 208 RR_LR = 0x18, 209 RR_LTR = 0x12, 210 RR_NR = 0x14, 211 RR_OR = 0x16, 212 RR_SR = 0x1b, 213 RR_SLR = 0x1f, 214 RR_XR = 0x17, 215 216 RSY_RLL = 0xeb1d, 217 RSY_RLLG = 0xeb1c, 218 RSY_SLLG = 0xeb0d, 219 RSY_SLLK = 0xebdf, 220 RSY_SRAG = 0xeb0a, 221 RSY_SRAK = 0xebdc, 222 RSY_SRLG = 0xeb0c, 223 RSY_SRLK = 0xebde, 224 225 RS_SLL = 0x89, 226 RS_SRA = 0x8a, 227 RS_SRL = 0x88, 228 229 RXY_AG = 0xe308, 230 RXY_AY = 0xe35a, 231 RXY_CG = 0xe320, 232 RXY_CLG = 0xe321, 233 RXY_CLY = 0xe355, 234 RXY_CY = 0xe359, 235 RXY_LAY = 0xe371, 236 RXY_LB = 0xe376, 237 RXY_LG = 0xe304, 238 RXY_LGB = 0xe377, 239 RXY_LGF = 0xe314, 240 RXY_LGH = 0xe315, 241 RXY_LHY = 0xe378, 242 RXY_LLGC = 0xe390, 243 RXY_LLGF = 0xe316, 244 RXY_LLGH = 0xe391, 245 RXY_LMG = 0xeb04, 246 RXY_LRV = 0xe31e, 247 RXY_LRVG = 0xe30f, 248 RXY_LRVH = 0xe31f, 249 RXY_LY = 0xe358, 250 RXY_NG = 0xe380, 251 RXY_OG = 0xe381, 252 RXY_STCY = 0xe372, 253 RXY_STG = 0xe324, 254 RXY_STHY = 0xe370, 255 RXY_STMG = 0xeb24, 256 RXY_STRV = 0xe33e, 257 RXY_STRVG = 0xe32f, 258 RXY_STRVH = 0xe33f, 259 RXY_STY = 0xe350, 260 RXY_XG = 0xe382, 261 262 RX_A = 0x5a, 263 RX_C = 0x59, 264 RX_L = 0x58, 265 RX_LA = 0x41, 266 RX_LH = 0x48, 267 RX_ST = 0x50, 268 RX_STC = 0x42, 269 RX_STH = 0x40, 270 271 VRIa_VGBM = 0xe744, 272 VRIa_VREPI = 0xe745, 273 VRIb_VGM = 0xe746, 274 VRIc_VREP = 0xe74d, 275 276 VRRa_VLC = 0xe7de, 277 VRRa_VLP = 0xe7df, 278 VRRa_VLR = 0xe756, 279 VRRc_VA = 0xe7f3, 280 VRRc_VCEQ = 0xe7f8, /* we leave the m5 cs field 0 */ 281 VRRc_VCH = 0xe7fb, /* " */ 282 VRRc_VCHL = 0xe7f9, /* " */ 283 VRRc_VERLLV = 0xe773, 284 VRRc_VESLV = 0xe770, 285 VRRc_VESRAV = 0xe77a, 286 VRRc_VESRLV = 0xe778, 287 VRRc_VML = 0xe7a2, 288 VRRc_VMN = 0xe7fe, 289 VRRc_VMNL = 0xe7fc, 290 VRRc_VMX = 0xe7ff, 291 VRRc_VMXL = 0xe7fd, 292 VRRc_VN = 0xe768, 293 VRRc_VNC = 0xe769, 294 VRRc_VNN = 0xe76e, 295 VRRc_VNO = 0xe76b, 296 VRRc_VNX = 0xe76c, 297 VRRc_VO = 0xe76a, 298 VRRc_VOC = 0xe76f, 299 VRRc_VPKS = 0xe797, /* we leave the m5 cs field 0 */ 300 VRRc_VS = 0xe7f7, 301 VRRa_VUPH = 0xe7d7, 302 VRRa_VUPL = 0xe7d6, 303 VRRc_VX = 0xe76d, 304 VRRe_VSEL = 0xe78d, 305 VRRf_VLVGP = 0xe762, 306 307 VRSa_VERLL = 0xe733, 308 VRSa_VESL = 0xe730, 309 VRSa_VESRA = 0xe73a, 310 VRSa_VESRL = 0xe738, 311 VRSb_VLVG = 0xe722, 312 VRSc_VLGV = 0xe721, 313 314 VRX_VL = 0xe706, 315 VRX_VLLEZ = 0xe704, 316 VRX_VLREP = 0xe705, 317 VRX_VST = 0xe70e, 318 VRX_VSTEF = 0xe70b, 319 VRX_VSTEG = 0xe70a, 320 321 NOP = 0x0707, 322} S390Opcode; 323 324#ifdef CONFIG_DEBUG_TCG 325static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = { 326 "%r0", "%r1", "%r2", "%r3", "%r4", "%r5", "%r6", "%r7", 327 "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15", 328 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 329 "%v0", "%v1", "%v2", "%v3", "%v4", "%v5", "%v6", "%v7", 330 "%v8", "%v9", "%v10", "%v11", "%v12", "%v13", "%v14", "%v15", 331 "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23", 332 "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31", 333}; 334#endif 335 336/* Since R6 is a potential argument register, choose it last of the 337 call-saved registers. Likewise prefer the call-clobbered registers 338 in reverse order to maximize the chance of avoiding the arguments. */ 339static const int tcg_target_reg_alloc_order[] = { 340 /* Call saved registers. */ 341 TCG_REG_R13, 342 TCG_REG_R12, 343 TCG_REG_R11, 344 TCG_REG_R10, 345 TCG_REG_R9, 346 TCG_REG_R8, 347 TCG_REG_R7, 348 TCG_REG_R6, 349 /* Call clobbered registers. */ 350 TCG_REG_R14, 351 TCG_REG_R0, 352 TCG_REG_R1, 353 /* Argument registers, in reverse order of allocation. */ 354 TCG_REG_R5, 355 TCG_REG_R4, 356 TCG_REG_R3, 357 TCG_REG_R2, 358 359 /* V8-V15 are call saved, and omitted. */ 360 TCG_REG_V0, 361 TCG_REG_V1, 362 TCG_REG_V2, 363 TCG_REG_V3, 364 TCG_REG_V4, 365 TCG_REG_V5, 366 TCG_REG_V6, 367 TCG_REG_V7, 368 TCG_REG_V16, 369 TCG_REG_V17, 370 TCG_REG_V18, 371 TCG_REG_V19, 372 TCG_REG_V20, 373 TCG_REG_V21, 374 TCG_REG_V22, 375 TCG_REG_V23, 376 TCG_REG_V24, 377 TCG_REG_V25, 378 TCG_REG_V26, 379 TCG_REG_V27, 380 TCG_REG_V28, 381 TCG_REG_V29, 382 TCG_REG_V30, 383 TCG_REG_V31, 384}; 385 386static const int tcg_target_call_iarg_regs[] = { 387 TCG_REG_R2, 388 TCG_REG_R3, 389 TCG_REG_R4, 390 TCG_REG_R5, 391 TCG_REG_R6, 392}; 393 394static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot) 395{ 396 tcg_debug_assert(kind == TCG_CALL_RET_NORMAL); 397 tcg_debug_assert(slot == 0); 398 return TCG_REG_R2; 399} 400 401#define S390_CC_EQ 8 402#define S390_CC_LT 4 403#define S390_CC_GT 2 404#define S390_CC_OV 1 405#define S390_CC_NE (S390_CC_LT | S390_CC_GT) 406#define S390_CC_LE (S390_CC_LT | S390_CC_EQ) 407#define S390_CC_GE (S390_CC_GT | S390_CC_EQ) 408#define S390_CC_NEVER 0 409#define S390_CC_ALWAYS 15 410 411/* Condition codes that result from a COMPARE and COMPARE LOGICAL. */ 412static const uint8_t tcg_cond_to_s390_cond[] = { 413 [TCG_COND_EQ] = S390_CC_EQ, 414 [TCG_COND_NE] = S390_CC_NE, 415 [TCG_COND_LT] = S390_CC_LT, 416 [TCG_COND_LE] = S390_CC_LE, 417 [TCG_COND_GT] = S390_CC_GT, 418 [TCG_COND_GE] = S390_CC_GE, 419 [TCG_COND_LTU] = S390_CC_LT, 420 [TCG_COND_LEU] = S390_CC_LE, 421 [TCG_COND_GTU] = S390_CC_GT, 422 [TCG_COND_GEU] = S390_CC_GE, 423}; 424 425/* Condition codes that result from a LOAD AND TEST. Here, we have no 426 unsigned instruction variation, however since the test is vs zero we 427 can re-map the outcomes appropriately. */ 428static const uint8_t tcg_cond_to_ltr_cond[] = { 429 [TCG_COND_EQ] = S390_CC_EQ, 430 [TCG_COND_NE] = S390_CC_NE, 431 [TCG_COND_LT] = S390_CC_LT, 432 [TCG_COND_LE] = S390_CC_LE, 433 [TCG_COND_GT] = S390_CC_GT, 434 [TCG_COND_GE] = S390_CC_GE, 435 [TCG_COND_LTU] = S390_CC_NEVER, 436 [TCG_COND_LEU] = S390_CC_EQ, 437 [TCG_COND_GTU] = S390_CC_NE, 438 [TCG_COND_GEU] = S390_CC_ALWAYS, 439}; 440 441static const tcg_insn_unit *tb_ret_addr; 442uint64_t s390_facilities[3]; 443 444static inline bool is_general_reg(TCGReg r) 445{ 446 return r <= TCG_REG_R15; 447} 448 449static inline bool is_vector_reg(TCGReg r) 450{ 451 return r >= TCG_REG_V0 && r <= TCG_REG_V31; 452} 453 454static bool patch_reloc(tcg_insn_unit *src_rw, int type, 455 intptr_t value, intptr_t addend) 456{ 457 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw); 458 intptr_t pcrel2; 459 uint32_t old; 460 461 value += addend; 462 pcrel2 = (tcg_insn_unit *)value - src_rx; 463 464 switch (type) { 465 case R_390_PC16DBL: 466 if (pcrel2 == (int16_t)pcrel2) { 467 tcg_patch16(src_rw, pcrel2); 468 return true; 469 } 470 break; 471 case R_390_PC32DBL: 472 if (pcrel2 == (int32_t)pcrel2) { 473 tcg_patch32(src_rw, pcrel2); 474 return true; 475 } 476 break; 477 case R_390_20: 478 if (value == sextract64(value, 0, 20)) { 479 old = *(uint32_t *)src_rw & 0xf00000ff; 480 old |= ((value & 0xfff) << 16) | ((value & 0xff000) >> 4); 481 tcg_patch32(src_rw, old); 482 return true; 483 } 484 break; 485 default: 486 g_assert_not_reached(); 487 } 488 return false; 489} 490 491static int is_const_p16(uint64_t val) 492{ 493 for (int i = 0; i < 4; ++i) { 494 uint64_t mask = 0xffffull << (i * 16); 495 if ((val & ~mask) == 0) { 496 return i; 497 } 498 } 499 return -1; 500} 501 502static int is_const_p32(uint64_t val) 503{ 504 if ((val & 0xffffffff00000000ull) == 0) { 505 return 0; 506 } 507 if ((val & 0x00000000ffffffffull) == 0) { 508 return 1; 509 } 510 return -1; 511} 512 513/* 514 * Accept bit patterns like these: 515 * 0....01....1 516 * 1....10....0 517 * 1..10..01..1 518 * 0..01..10..0 519 * Copied from gcc sources. 520 */ 521static bool risbg_mask(uint64_t c) 522{ 523 uint64_t lsb; 524 /* We don't change the number of transitions by inverting, 525 so make sure we start with the LSB zero. */ 526 if (c & 1) { 527 c = ~c; 528 } 529 /* Reject all zeros or all ones. */ 530 if (c == 0) { 531 return false; 532 } 533 /* Find the first transition. */ 534 lsb = c & -c; 535 /* Invert to look for a second transition. */ 536 c = ~c; 537 /* Erase the first transition. */ 538 c &= -lsb; 539 /* Find the second transition, if any. */ 540 lsb = c & -c; 541 /* Match if all the bits are 1's, or if c is zero. */ 542 return c == -lsb; 543} 544 545/* Test if a constant matches the constraint. */ 546static bool tcg_target_const_match(int64_t val, TCGType type, int ct) 547{ 548 if (ct & TCG_CT_CONST) { 549 return 1; 550 } 551 552 if (type == TCG_TYPE_I32) { 553 val = (int32_t)val; 554 } 555 556 /* The following are mutually exclusive. */ 557 if (ct & TCG_CT_CONST_S16) { 558 return val == (int16_t)val; 559 } else if (ct & TCG_CT_CONST_S32) { 560 return val == (int32_t)val; 561 } else if (ct & TCG_CT_CONST_S33) { 562 return val >= -0xffffffffll && val <= 0xffffffffll; 563 } else if (ct & TCG_CT_CONST_ZERO) { 564 return val == 0; 565 } 566 567 if (ct & TCG_CT_CONST_INV) { 568 val = ~val; 569 } 570 /* 571 * Note that is_const_p16 is a subset of is_const_p32, 572 * so we don't need both constraints. 573 */ 574 if ((ct & TCG_CT_CONST_P32) && is_const_p32(val) >= 0) { 575 return true; 576 } 577 if ((ct & TCG_CT_CONST_INVRISBG) && risbg_mask(~val)) { 578 return true; 579 } 580 581 return 0; 582} 583 584/* Emit instructions according to the given instruction format. */ 585 586static void tcg_out_insn_RR(TCGContext *s, S390Opcode op, TCGReg r1, TCGReg r2) 587{ 588 tcg_out16(s, (op << 8) | (r1 << 4) | r2); 589} 590 591static void tcg_out_insn_RRE(TCGContext *s, S390Opcode op, 592 TCGReg r1, TCGReg r2) 593{ 594 tcg_out32(s, (op << 16) | (r1 << 4) | r2); 595} 596 597/* RRF-a without the m4 field */ 598static void tcg_out_insn_RRFa(TCGContext *s, S390Opcode op, 599 TCGReg r1, TCGReg r2, TCGReg r3) 600{ 601 tcg_out32(s, (op << 16) | (r3 << 12) | (r1 << 4) | r2); 602} 603 604/* RRF-a with the m4 field */ 605static void tcg_out_insn_RRFam(TCGContext *s, S390Opcode op, 606 TCGReg r1, TCGReg r2, TCGReg r3, int m4) 607{ 608 tcg_out32(s, (op << 16) | (r3 << 12) | (m4 << 8) | (r1 << 4) | r2); 609} 610 611static void tcg_out_insn_RRFc(TCGContext *s, S390Opcode op, 612 TCGReg r1, TCGReg r2, int m3) 613{ 614 tcg_out32(s, (op << 16) | (m3 << 12) | (r1 << 4) | r2); 615} 616 617static void tcg_out_insn_RI(TCGContext *s, S390Opcode op, TCGReg r1, int i2) 618{ 619 tcg_out32(s, (op << 16) | (r1 << 20) | (i2 & 0xffff)); 620} 621 622static void tcg_out_insn_RIEg(TCGContext *s, S390Opcode op, TCGReg r1, 623 int i2, int m3) 624{ 625 tcg_out16(s, (op & 0xff00) | (r1 << 4) | m3); 626 tcg_out32(s, (i2 << 16) | (op & 0xff)); 627} 628 629static void tcg_out_insn_RIL(TCGContext *s, S390Opcode op, TCGReg r1, int i2) 630{ 631 tcg_out16(s, op | (r1 << 4)); 632 tcg_out32(s, i2); 633} 634 635static void tcg_out_insn_RS(TCGContext *s, S390Opcode op, TCGReg r1, 636 TCGReg b2, TCGReg r3, int disp) 637{ 638 tcg_out32(s, (op << 24) | (r1 << 20) | (r3 << 16) | (b2 << 12) 639 | (disp & 0xfff)); 640} 641 642static void tcg_out_insn_RSY(TCGContext *s, S390Opcode op, TCGReg r1, 643 TCGReg b2, TCGReg r3, int disp) 644{ 645 tcg_out16(s, (op & 0xff00) | (r1 << 4) | r3); 646 tcg_out32(s, (op & 0xff) | (b2 << 28) 647 | ((disp & 0xfff) << 16) | ((disp & 0xff000) >> 4)); 648} 649 650#define tcg_out_insn_RX tcg_out_insn_RS 651#define tcg_out_insn_RXY tcg_out_insn_RSY 652 653static int RXB(TCGReg v1, TCGReg v2, TCGReg v3, TCGReg v4) 654{ 655 /* 656 * Shift bit 4 of each regno to its corresponding bit of RXB. 657 * RXB itself begins at bit 8 of the instruction so 8 - 4 = 4 658 * is the left-shift of the 4th operand. 659 */ 660 return ((v1 & 0x10) << (4 + 3)) 661 | ((v2 & 0x10) << (4 + 2)) 662 | ((v3 & 0x10) << (4 + 1)) 663 | ((v4 & 0x10) << (4 + 0)); 664} 665 666static void tcg_out_insn_VRIa(TCGContext *s, S390Opcode op, 667 TCGReg v1, uint16_t i2, int m3) 668{ 669 tcg_debug_assert(is_vector_reg(v1)); 670 tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4)); 671 tcg_out16(s, i2); 672 tcg_out16(s, (op & 0x00ff) | RXB(v1, 0, 0, 0) | (m3 << 12)); 673} 674 675static void tcg_out_insn_VRIb(TCGContext *s, S390Opcode op, 676 TCGReg v1, uint8_t i2, uint8_t i3, int m4) 677{ 678 tcg_debug_assert(is_vector_reg(v1)); 679 tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4)); 680 tcg_out16(s, (i2 << 8) | (i3 & 0xff)); 681 tcg_out16(s, (op & 0x00ff) | RXB(v1, 0, 0, 0) | (m4 << 12)); 682} 683 684static void tcg_out_insn_VRIc(TCGContext *s, S390Opcode op, 685 TCGReg v1, uint16_t i2, TCGReg v3, int m4) 686{ 687 tcg_debug_assert(is_vector_reg(v1)); 688 tcg_debug_assert(is_vector_reg(v3)); 689 tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4) | (v3 & 0xf)); 690 tcg_out16(s, i2); 691 tcg_out16(s, (op & 0x00ff) | RXB(v1, 0, v3, 0) | (m4 << 12)); 692} 693 694static void tcg_out_insn_VRRa(TCGContext *s, S390Opcode op, 695 TCGReg v1, TCGReg v2, int m3) 696{ 697 tcg_debug_assert(is_vector_reg(v1)); 698 tcg_debug_assert(is_vector_reg(v2)); 699 tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4) | (v2 & 0xf)); 700 tcg_out32(s, (op & 0x00ff) | RXB(v1, v2, 0, 0) | (m3 << 12)); 701} 702 703static void tcg_out_insn_VRRc(TCGContext *s, S390Opcode op, 704 TCGReg v1, TCGReg v2, TCGReg v3, int m4) 705{ 706 tcg_debug_assert(is_vector_reg(v1)); 707 tcg_debug_assert(is_vector_reg(v2)); 708 tcg_debug_assert(is_vector_reg(v3)); 709 tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4) | (v2 & 0xf)); 710 tcg_out16(s, v3 << 12); 711 tcg_out16(s, (op & 0x00ff) | RXB(v1, v2, v3, 0) | (m4 << 12)); 712} 713 714static void tcg_out_insn_VRRe(TCGContext *s, S390Opcode op, 715 TCGReg v1, TCGReg v2, TCGReg v3, TCGReg v4) 716{ 717 tcg_debug_assert(is_vector_reg(v1)); 718 tcg_debug_assert(is_vector_reg(v2)); 719 tcg_debug_assert(is_vector_reg(v3)); 720 tcg_debug_assert(is_vector_reg(v4)); 721 tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4) | (v2 & 0xf)); 722 tcg_out16(s, v3 << 12); 723 tcg_out16(s, (op & 0x00ff) | RXB(v1, v2, v3, v4) | (v4 << 12)); 724} 725 726static void tcg_out_insn_VRRf(TCGContext *s, S390Opcode op, 727 TCGReg v1, TCGReg r2, TCGReg r3) 728{ 729 tcg_debug_assert(is_vector_reg(v1)); 730 tcg_debug_assert(is_general_reg(r2)); 731 tcg_debug_assert(is_general_reg(r3)); 732 tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4) | r2); 733 tcg_out16(s, r3 << 12); 734 tcg_out16(s, (op & 0x00ff) | RXB(v1, 0, 0, 0)); 735} 736 737static void tcg_out_insn_VRSa(TCGContext *s, S390Opcode op, TCGReg v1, 738 intptr_t d2, TCGReg b2, TCGReg v3, int m4) 739{ 740 tcg_debug_assert(is_vector_reg(v1)); 741 tcg_debug_assert(d2 >= 0 && d2 <= 0xfff); 742 tcg_debug_assert(is_general_reg(b2)); 743 tcg_debug_assert(is_vector_reg(v3)); 744 tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4) | (v3 & 0xf)); 745 tcg_out16(s, b2 << 12 | d2); 746 tcg_out16(s, (op & 0x00ff) | RXB(v1, 0, v3, 0) | (m4 << 12)); 747} 748 749static void tcg_out_insn_VRSb(TCGContext *s, S390Opcode op, TCGReg v1, 750 intptr_t d2, TCGReg b2, TCGReg r3, int m4) 751{ 752 tcg_debug_assert(is_vector_reg(v1)); 753 tcg_debug_assert(d2 >= 0 && d2 <= 0xfff); 754 tcg_debug_assert(is_general_reg(b2)); 755 tcg_debug_assert(is_general_reg(r3)); 756 tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4) | r3); 757 tcg_out16(s, b2 << 12 | d2); 758 tcg_out16(s, (op & 0x00ff) | RXB(v1, 0, 0, 0) | (m4 << 12)); 759} 760 761static void tcg_out_insn_VRSc(TCGContext *s, S390Opcode op, TCGReg r1, 762 intptr_t d2, TCGReg b2, TCGReg v3, int m4) 763{ 764 tcg_debug_assert(is_general_reg(r1)); 765 tcg_debug_assert(d2 >= 0 && d2 <= 0xfff); 766 tcg_debug_assert(is_general_reg(b2)); 767 tcg_debug_assert(is_vector_reg(v3)); 768 tcg_out16(s, (op & 0xff00) | (r1 << 4) | (v3 & 0xf)); 769 tcg_out16(s, b2 << 12 | d2); 770 tcg_out16(s, (op & 0x00ff) | RXB(0, 0, v3, 0) | (m4 << 12)); 771} 772 773static void tcg_out_insn_VRX(TCGContext *s, S390Opcode op, TCGReg v1, 774 TCGReg b2, TCGReg x2, intptr_t d2, int m3) 775{ 776 tcg_debug_assert(is_vector_reg(v1)); 777 tcg_debug_assert(d2 >= 0 && d2 <= 0xfff); 778 tcg_debug_assert(is_general_reg(x2)); 779 tcg_debug_assert(is_general_reg(b2)); 780 tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4) | x2); 781 tcg_out16(s, (b2 << 12) | d2); 782 tcg_out16(s, (op & 0x00ff) | RXB(v1, 0, 0, 0) | (m3 << 12)); 783} 784 785/* Emit an opcode with "type-checking" of the format. */ 786#define tcg_out_insn(S, FMT, OP, ...) \ 787 glue(tcg_out_insn_,FMT)(S, glue(glue(FMT,_),OP), ## __VA_ARGS__) 788 789 790/* emit 64-bit shifts */ 791static void tcg_out_sh64(TCGContext* s, S390Opcode op, TCGReg dest, 792 TCGReg src, TCGReg sh_reg, int sh_imm) 793{ 794 tcg_out_insn_RSY(s, op, dest, sh_reg, src, sh_imm); 795} 796 797/* emit 32-bit shifts */ 798static void tcg_out_sh32(TCGContext* s, S390Opcode op, TCGReg dest, 799 TCGReg sh_reg, int sh_imm) 800{ 801 tcg_out_insn_RS(s, op, dest, sh_reg, 0, sh_imm); 802} 803 804static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg dst, TCGReg src) 805{ 806 if (src == dst) { 807 return true; 808 } 809 switch (type) { 810 case TCG_TYPE_I32: 811 if (likely(is_general_reg(dst) && is_general_reg(src))) { 812 tcg_out_insn(s, RR, LR, dst, src); 813 break; 814 } 815 /* fallthru */ 816 817 case TCG_TYPE_I64: 818 if (likely(is_general_reg(dst))) { 819 if (likely(is_general_reg(src))) { 820 tcg_out_insn(s, RRE, LGR, dst, src); 821 } else { 822 tcg_out_insn(s, VRSc, VLGV, dst, 0, 0, src, 3); 823 } 824 break; 825 } else if (is_general_reg(src)) { 826 tcg_out_insn(s, VRSb, VLVG, dst, 0, 0, src, 3); 827 break; 828 } 829 /* fallthru */ 830 831 case TCG_TYPE_V64: 832 case TCG_TYPE_V128: 833 tcg_out_insn(s, VRRa, VLR, dst, src, 0); 834 break; 835 836 default: 837 g_assert_not_reached(); 838 } 839 return true; 840} 841 842static const S390Opcode li_insns[4] = { 843 RI_LLILL, RI_LLILH, RI_LLIHL, RI_LLIHH 844}; 845static const S390Opcode oi_insns[4] = { 846 RI_OILL, RI_OILH, RI_OIHL, RI_OIHH 847}; 848static const S390Opcode lif_insns[2] = { 849 RIL_LLILF, RIL_LLIHF, 850}; 851 852/* load a register with an immediate value */ 853static void tcg_out_movi(TCGContext *s, TCGType type, 854 TCGReg ret, tcg_target_long sval) 855{ 856 tcg_target_ulong uval = sval; 857 ptrdiff_t pc_off; 858 int i; 859 860 if (type == TCG_TYPE_I32) { 861 uval = (uint32_t)sval; 862 sval = (int32_t)sval; 863 } 864 865 /* Try all 32-bit insns that can load it in one go. */ 866 if (sval >= -0x8000 && sval < 0x8000) { 867 tcg_out_insn(s, RI, LGHI, ret, sval); 868 return; 869 } 870 871 i = is_const_p16(uval); 872 if (i >= 0) { 873 tcg_out_insn_RI(s, li_insns[i], ret, uval >> (i * 16)); 874 return; 875 } 876 877 /* Try all 48-bit insns that can load it in one go. */ 878 if (sval == (int32_t)sval) { 879 tcg_out_insn(s, RIL, LGFI, ret, sval); 880 return; 881 } 882 883 i = is_const_p32(uval); 884 if (i >= 0) { 885 tcg_out_insn_RIL(s, lif_insns[i], ret, uval >> (i * 32)); 886 return; 887 } 888 889 /* Try for PC-relative address load. For odd addresses, add one. */ 890 pc_off = tcg_pcrel_diff(s, (void *)sval) >> 1; 891 if (pc_off == (int32_t)pc_off) { 892 tcg_out_insn(s, RIL, LARL, ret, pc_off); 893 if (sval & 1) { 894 tcg_out_insn(s, RI, AGHI, ret, 1); 895 } 896 return; 897 } 898 899 /* Otherwise, load it by parts. */ 900 i = is_const_p16((uint32_t)uval); 901 if (i >= 0) { 902 tcg_out_insn_RI(s, li_insns[i], ret, uval >> (i * 16)); 903 } else { 904 tcg_out_insn(s, RIL, LLILF, ret, uval); 905 } 906 uval >>= 32; 907 i = is_const_p16(uval); 908 if (i >= 0) { 909 tcg_out_insn_RI(s, oi_insns[i + 2], ret, uval >> (i * 16)); 910 } else { 911 tcg_out_insn(s, RIL, OIHF, ret, uval); 912 } 913} 914 915/* Emit a load/store type instruction. Inputs are: 916 DATA: The register to be loaded or stored. 917 BASE+OFS: The effective address. 918 OPC_RX: If the operation has an RX format opcode (e.g. STC), otherwise 0. 919 OPC_RXY: The RXY format opcode for the operation (e.g. STCY). */ 920 921static void tcg_out_mem(TCGContext *s, S390Opcode opc_rx, S390Opcode opc_rxy, 922 TCGReg data, TCGReg base, TCGReg index, 923 tcg_target_long ofs) 924{ 925 if (ofs < -0x80000 || ofs >= 0x80000) { 926 /* Combine the low 20 bits of the offset with the actual load insn; 927 the high 44 bits must come from an immediate load. */ 928 tcg_target_long low = ((ofs & 0xfffff) ^ 0x80000) - 0x80000; 929 tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, ofs - low); 930 ofs = low; 931 932 /* If we were already given an index register, add it in. */ 933 if (index != TCG_REG_NONE) { 934 tcg_out_insn(s, RRE, AGR, TCG_TMP0, index); 935 } 936 index = TCG_TMP0; 937 } 938 939 if (opc_rx && ofs >= 0 && ofs < 0x1000) { 940 tcg_out_insn_RX(s, opc_rx, data, base, index, ofs); 941 } else { 942 tcg_out_insn_RXY(s, opc_rxy, data, base, index, ofs); 943 } 944} 945 946static void tcg_out_vrx_mem(TCGContext *s, S390Opcode opc_vrx, 947 TCGReg data, TCGReg base, TCGReg index, 948 tcg_target_long ofs, int m3) 949{ 950 if (ofs < 0 || ofs >= 0x1000) { 951 if (ofs >= -0x80000 && ofs < 0x80000) { 952 tcg_out_insn(s, RXY, LAY, TCG_TMP0, base, index, ofs); 953 base = TCG_TMP0; 954 index = TCG_REG_NONE; 955 ofs = 0; 956 } else { 957 tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, ofs); 958 if (index != TCG_REG_NONE) { 959 tcg_out_insn(s, RRE, AGR, TCG_TMP0, index); 960 } 961 index = TCG_TMP0; 962 ofs = 0; 963 } 964 } 965 tcg_out_insn_VRX(s, opc_vrx, data, base, index, ofs, m3); 966} 967 968/* load data without address translation or endianness conversion */ 969static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg data, 970 TCGReg base, intptr_t ofs) 971{ 972 switch (type) { 973 case TCG_TYPE_I32: 974 if (likely(is_general_reg(data))) { 975 tcg_out_mem(s, RX_L, RXY_LY, data, base, TCG_REG_NONE, ofs); 976 break; 977 } 978 tcg_out_vrx_mem(s, VRX_VLLEZ, data, base, TCG_REG_NONE, ofs, MO_32); 979 break; 980 981 case TCG_TYPE_I64: 982 if (likely(is_general_reg(data))) { 983 tcg_out_mem(s, 0, RXY_LG, data, base, TCG_REG_NONE, ofs); 984 break; 985 } 986 /* fallthru */ 987 988 case TCG_TYPE_V64: 989 tcg_out_vrx_mem(s, VRX_VLLEZ, data, base, TCG_REG_NONE, ofs, MO_64); 990 break; 991 992 case TCG_TYPE_V128: 993 /* Hint quadword aligned. */ 994 tcg_out_vrx_mem(s, VRX_VL, data, base, TCG_REG_NONE, ofs, 4); 995 break; 996 997 default: 998 g_assert_not_reached(); 999 } 1000} 1001 1002static void tcg_out_st(TCGContext *s, TCGType type, TCGReg data, 1003 TCGReg base, intptr_t ofs) 1004{ 1005 switch (type) { 1006 case TCG_TYPE_I32: 1007 if (likely(is_general_reg(data))) { 1008 tcg_out_mem(s, RX_ST, RXY_STY, data, base, TCG_REG_NONE, ofs); 1009 } else { 1010 tcg_out_vrx_mem(s, VRX_VSTEF, data, base, TCG_REG_NONE, ofs, 1); 1011 } 1012 break; 1013 1014 case TCG_TYPE_I64: 1015 if (likely(is_general_reg(data))) { 1016 tcg_out_mem(s, 0, RXY_STG, data, base, TCG_REG_NONE, ofs); 1017 break; 1018 } 1019 /* fallthru */ 1020 1021 case TCG_TYPE_V64: 1022 tcg_out_vrx_mem(s, VRX_VSTEG, data, base, TCG_REG_NONE, ofs, 0); 1023 break; 1024 1025 case TCG_TYPE_V128: 1026 /* Hint quadword aligned. */ 1027 tcg_out_vrx_mem(s, VRX_VST, data, base, TCG_REG_NONE, ofs, 4); 1028 break; 1029 1030 default: 1031 g_assert_not_reached(); 1032 } 1033} 1034 1035static inline bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val, 1036 TCGReg base, intptr_t ofs) 1037{ 1038 return false; 1039} 1040 1041static bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2) 1042{ 1043 return false; 1044} 1045 1046static void tcg_out_addi_ptr(TCGContext *s, TCGReg rd, TCGReg rs, 1047 tcg_target_long imm) 1048{ 1049 /* This function is only used for passing structs by reference. */ 1050 tcg_out_mem(s, RX_LA, RXY_LAY, rd, rs, TCG_REG_NONE, imm); 1051} 1052 1053static inline void tcg_out_risbg(TCGContext *s, TCGReg dest, TCGReg src, 1054 int msb, int lsb, int ofs, int z) 1055{ 1056 /* Format RIE-f */ 1057 tcg_out16(s, (RIEf_RISBG & 0xff00) | (dest << 4) | src); 1058 tcg_out16(s, (msb << 8) | (z << 7) | lsb); 1059 tcg_out16(s, (ofs << 8) | (RIEf_RISBG & 0xff)); 1060} 1061 1062static void tcg_out_ext8s(TCGContext *s, TCGType type, TCGReg dest, TCGReg src) 1063{ 1064 tcg_out_insn(s, RRE, LGBR, dest, src); 1065} 1066 1067static void tcg_out_ext8u(TCGContext *s, TCGReg dest, TCGReg src) 1068{ 1069 tcg_out_insn(s, RRE, LLGCR, dest, src); 1070} 1071 1072static void tcg_out_ext16s(TCGContext *s, TCGType type, TCGReg dest, TCGReg src) 1073{ 1074 tcg_out_insn(s, RRE, LGHR, dest, src); 1075} 1076 1077static void tcg_out_ext16u(TCGContext *s, TCGReg dest, TCGReg src) 1078{ 1079 tcg_out_insn(s, RRE, LLGHR, dest, src); 1080} 1081 1082static void tcg_out_ext32s(TCGContext *s, TCGReg dest, TCGReg src) 1083{ 1084 tcg_out_insn(s, RRE, LGFR, dest, src); 1085} 1086 1087static void tcg_out_ext32u(TCGContext *s, TCGReg dest, TCGReg src) 1088{ 1089 tcg_out_insn(s, RRE, LLGFR, dest, src); 1090} 1091 1092static void tcg_out_exts_i32_i64(TCGContext *s, TCGReg dest, TCGReg src) 1093{ 1094 tcg_out_ext32s(s, dest, src); 1095} 1096 1097static void tcg_out_extu_i32_i64(TCGContext *s, TCGReg dest, TCGReg src) 1098{ 1099 tcg_out_ext32u(s, dest, src); 1100} 1101 1102static void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg dest, TCGReg src) 1103{ 1104 tcg_out_mov(s, TCG_TYPE_I32, dest, src); 1105} 1106 1107static void tgen_andi_risbg(TCGContext *s, TCGReg out, TCGReg in, uint64_t val) 1108{ 1109 int msb, lsb; 1110 if ((val & 0x8000000000000001ull) == 0x8000000000000001ull) { 1111 /* Achieve wraparound by swapping msb and lsb. */ 1112 msb = 64 - ctz64(~val); 1113 lsb = clz64(~val) - 1; 1114 } else { 1115 msb = clz64(val); 1116 lsb = 63 - ctz64(val); 1117 } 1118 tcg_out_risbg(s, out, in, msb, lsb, 0, 1); 1119} 1120 1121static void tgen_andi(TCGContext *s, TCGType type, TCGReg dest, uint64_t val) 1122{ 1123 static const S390Opcode ni_insns[4] = { 1124 RI_NILL, RI_NILH, RI_NIHL, RI_NIHH 1125 }; 1126 static const S390Opcode nif_insns[2] = { 1127 RIL_NILF, RIL_NIHF 1128 }; 1129 uint64_t valid = (type == TCG_TYPE_I32 ? 0xffffffffull : -1ull); 1130 int i; 1131 1132 /* Look for the zero-extensions. */ 1133 if ((val & valid) == 0xffffffff) { 1134 tcg_out_ext32u(s, dest, dest); 1135 return; 1136 } 1137 if ((val & valid) == 0xff) { 1138 tcg_out_ext8u(s, dest, dest); 1139 return; 1140 } 1141 if ((val & valid) == 0xffff) { 1142 tcg_out_ext16u(s, dest, dest); 1143 return; 1144 } 1145 1146 i = is_const_p16(~val & valid); 1147 if (i >= 0) { 1148 tcg_out_insn_RI(s, ni_insns[i], dest, val >> (i * 16)); 1149 return; 1150 } 1151 1152 i = is_const_p32(~val & valid); 1153 tcg_debug_assert(i == 0 || type != TCG_TYPE_I32); 1154 if (i >= 0) { 1155 tcg_out_insn_RIL(s, nif_insns[i], dest, val >> (i * 32)); 1156 return; 1157 } 1158 1159 if (risbg_mask(val)) { 1160 tgen_andi_risbg(s, dest, dest, val); 1161 return; 1162 } 1163 1164 g_assert_not_reached(); 1165} 1166 1167static void tgen_ori(TCGContext *s, TCGReg dest, uint64_t val) 1168{ 1169 static const S390Opcode oif_insns[2] = { 1170 RIL_OILF, RIL_OIHF 1171 }; 1172 1173 int i; 1174 1175 i = is_const_p16(val); 1176 if (i >= 0) { 1177 tcg_out_insn_RI(s, oi_insns[i], dest, val >> (i * 16)); 1178 return; 1179 } 1180 1181 i = is_const_p32(val); 1182 if (i >= 0) { 1183 tcg_out_insn_RIL(s, oif_insns[i], dest, val >> (i * 32)); 1184 return; 1185 } 1186 1187 g_assert_not_reached(); 1188} 1189 1190static void tgen_xori(TCGContext *s, TCGReg dest, uint64_t val) 1191{ 1192 switch (is_const_p32(val)) { 1193 case 0: 1194 tcg_out_insn(s, RIL, XILF, dest, val); 1195 break; 1196 case 1: 1197 tcg_out_insn(s, RIL, XIHF, dest, val >> 32); 1198 break; 1199 default: 1200 g_assert_not_reached(); 1201 } 1202} 1203 1204static int tgen_cmp2(TCGContext *s, TCGType type, TCGCond c, TCGReg r1, 1205 TCGArg c2, bool c2const, bool need_carry, int *inv_cc) 1206{ 1207 bool is_unsigned = is_unsigned_cond(c); 1208 TCGCond inv_c = tcg_invert_cond(c); 1209 S390Opcode op; 1210 1211 if (c2const) { 1212 if (c2 == 0) { 1213 if (!(is_unsigned && need_carry)) { 1214 if (type == TCG_TYPE_I32) { 1215 tcg_out_insn(s, RR, LTR, r1, r1); 1216 } else { 1217 tcg_out_insn(s, RRE, LTGR, r1, r1); 1218 } 1219 *inv_cc = tcg_cond_to_ltr_cond[inv_c]; 1220 return tcg_cond_to_ltr_cond[c]; 1221 } 1222 } 1223 1224 if (!is_unsigned && c2 == (int16_t)c2) { 1225 op = (type == TCG_TYPE_I32 ? RI_CHI : RI_CGHI); 1226 tcg_out_insn_RI(s, op, r1, c2); 1227 goto exit; 1228 } 1229 1230 if (type == TCG_TYPE_I32) { 1231 op = (is_unsigned ? RIL_CLFI : RIL_CFI); 1232 tcg_out_insn_RIL(s, op, r1, c2); 1233 goto exit; 1234 } 1235 1236 /* 1237 * Constraints are for a signed 33-bit operand, which is a 1238 * convenient superset of this signed/unsigned test. 1239 */ 1240 if (c2 == (is_unsigned ? (TCGArg)(uint32_t)c2 : (TCGArg)(int32_t)c2)) { 1241 op = (is_unsigned ? RIL_CLGFI : RIL_CGFI); 1242 tcg_out_insn_RIL(s, op, r1, c2); 1243 goto exit; 1244 } 1245 1246 /* Load everything else into a register. */ 1247 tcg_out_movi(s, TCG_TYPE_I64, TCG_TMP0, c2); 1248 c2 = TCG_TMP0; 1249 } 1250 1251 if (type == TCG_TYPE_I32) { 1252 op = (is_unsigned ? RR_CLR : RR_CR); 1253 tcg_out_insn_RR(s, op, r1, c2); 1254 } else { 1255 op = (is_unsigned ? RRE_CLGR : RRE_CGR); 1256 tcg_out_insn_RRE(s, op, r1, c2); 1257 } 1258 1259 exit: 1260 *inv_cc = tcg_cond_to_s390_cond[inv_c]; 1261 return tcg_cond_to_s390_cond[c]; 1262} 1263 1264static int tgen_cmp(TCGContext *s, TCGType type, TCGCond c, TCGReg r1, 1265 TCGArg c2, bool c2const, bool need_carry) 1266{ 1267 int inv_cc; 1268 return tgen_cmp2(s, type, c, r1, c2, c2const, need_carry, &inv_cc); 1269} 1270 1271static void tgen_setcond(TCGContext *s, TCGType type, TCGCond cond, 1272 TCGReg dest, TCGReg c1, TCGArg c2, int c2const) 1273{ 1274 int cc; 1275 1276 /* With LOC2, we can always emit the minimum 3 insns. */ 1277 if (HAVE_FACILITY(LOAD_ON_COND2)) { 1278 /* Emit: d = 0, d = (cc ? 1 : d). */ 1279 cc = tgen_cmp(s, type, cond, c1, c2, c2const, false); 1280 tcg_out_movi(s, TCG_TYPE_I64, dest, 0); 1281 tcg_out_insn(s, RIEg, LOCGHI, dest, 1, cc); 1282 return; 1283 } 1284 1285 restart: 1286 switch (cond) { 1287 case TCG_COND_NE: 1288 /* X != 0 is X > 0. */ 1289 if (c2const && c2 == 0) { 1290 cond = TCG_COND_GTU; 1291 } else { 1292 break; 1293 } 1294 /* fallthru */ 1295 1296 case TCG_COND_GTU: 1297 case TCG_COND_GT: 1298 /* The result of a compare has CC=2 for GT and CC=3 unused. 1299 ADD LOGICAL WITH CARRY considers (CC & 2) the carry bit. */ 1300 tgen_cmp(s, type, cond, c1, c2, c2const, true); 1301 tcg_out_movi(s, type, dest, 0); 1302 tcg_out_insn(s, RRE, ALCGR, dest, dest); 1303 return; 1304 1305 case TCG_COND_EQ: 1306 /* X == 0 is X <= 0. */ 1307 if (c2const && c2 == 0) { 1308 cond = TCG_COND_LEU; 1309 } else { 1310 break; 1311 } 1312 /* fallthru */ 1313 1314 case TCG_COND_LEU: 1315 case TCG_COND_LE: 1316 /* As above, but we're looking for borrow, or !carry. 1317 The second insn computes d - d - borrow, or -1 for true 1318 and 0 for false. So we must mask to 1 bit afterward. */ 1319 tgen_cmp(s, type, cond, c1, c2, c2const, true); 1320 tcg_out_insn(s, RRE, SLBGR, dest, dest); 1321 tgen_andi(s, type, dest, 1); 1322 return; 1323 1324 case TCG_COND_GEU: 1325 case TCG_COND_LTU: 1326 case TCG_COND_LT: 1327 case TCG_COND_GE: 1328 /* Swap operands so that we can use LEU/GTU/GT/LE. */ 1329 if (!c2const) { 1330 TCGReg t = c1; 1331 c1 = c2; 1332 c2 = t; 1333 cond = tcg_swap_cond(cond); 1334 goto restart; 1335 } 1336 break; 1337 1338 default: 1339 g_assert_not_reached(); 1340 } 1341 1342 cc = tgen_cmp(s, type, cond, c1, c2, c2const, false); 1343 /* Emit: d = 0, t = 1, d = (cc ? t : d). */ 1344 tcg_out_movi(s, TCG_TYPE_I64, dest, 0); 1345 tcg_out_movi(s, TCG_TYPE_I64, TCG_TMP0, 1); 1346 tcg_out_insn(s, RRFc, LOCGR, dest, TCG_TMP0, cc); 1347} 1348 1349static void tgen_movcond_int(TCGContext *s, TCGType type, TCGReg dest, 1350 TCGArg v3, int v3const, TCGReg v4, 1351 int cc, int inv_cc) 1352{ 1353 TCGReg src; 1354 1355 if (v3const) { 1356 if (dest == v4) { 1357 if (HAVE_FACILITY(LOAD_ON_COND2)) { 1358 /* Emit: if (cc) dest = v3. */ 1359 tcg_out_insn(s, RIEg, LOCGHI, dest, v3, cc); 1360 return; 1361 } 1362 tcg_out_insn(s, RI, LGHI, TCG_TMP0, v3); 1363 src = TCG_TMP0; 1364 } else { 1365 /* LGR+LOCGHI is larger than LGHI+LOCGR. */ 1366 tcg_out_insn(s, RI, LGHI, dest, v3); 1367 cc = inv_cc; 1368 src = v4; 1369 } 1370 } else { 1371 if (HAVE_FACILITY(MISC_INSN_EXT3)) { 1372 /* Emit: dest = cc ? v3 : v4. */ 1373 tcg_out_insn(s, RRFam, SELGR, dest, v3, v4, cc); 1374 return; 1375 } 1376 if (dest == v4) { 1377 src = v3; 1378 } else { 1379 tcg_out_mov(s, type, dest, v3); 1380 cc = inv_cc; 1381 src = v4; 1382 } 1383 } 1384 1385 /* Emit: if (cc) dest = src. */ 1386 tcg_out_insn(s, RRFc, LOCGR, dest, src, cc); 1387} 1388 1389static void tgen_movcond(TCGContext *s, TCGType type, TCGCond c, TCGReg dest, 1390 TCGReg c1, TCGArg c2, int c2const, 1391 TCGArg v3, int v3const, TCGReg v4) 1392{ 1393 int cc, inv_cc; 1394 1395 cc = tgen_cmp2(s, type, c, c1, c2, c2const, false, &inv_cc); 1396 tgen_movcond_int(s, type, dest, v3, v3const, v4, cc, inv_cc); 1397} 1398 1399static void tgen_clz(TCGContext *s, TCGReg dest, TCGReg a1, 1400 TCGArg a2, int a2const) 1401{ 1402 /* Since this sets both R and R+1, we have no choice but to store the 1403 result into R0, allowing R1 == TCG_TMP0 to be clobbered as well. */ 1404 QEMU_BUILD_BUG_ON(TCG_TMP0 != TCG_REG_R1); 1405 tcg_out_insn(s, RRE, FLOGR, TCG_REG_R0, a1); 1406 1407 if (a2const && a2 == 64) { 1408 tcg_out_mov(s, TCG_TYPE_I64, dest, TCG_REG_R0); 1409 return; 1410 } 1411 1412 /* 1413 * Conditions from FLOGR are: 1414 * 2 -> one bit found 1415 * 8 -> no one bit found 1416 */ 1417 tgen_movcond_int(s, TCG_TYPE_I64, dest, a2, a2const, TCG_REG_R0, 8, 2); 1418} 1419 1420static void tgen_ctpop(TCGContext *s, TCGType type, TCGReg dest, TCGReg src) 1421{ 1422 /* With MIE3, and bit 0 of m4 set, we get the complete result. */ 1423 if (HAVE_FACILITY(MISC_INSN_EXT3)) { 1424 if (type == TCG_TYPE_I32) { 1425 tcg_out_ext32u(s, dest, src); 1426 src = dest; 1427 } 1428 tcg_out_insn(s, RRFc, POPCNT, dest, src, 8); 1429 return; 1430 } 1431 1432 /* Without MIE3, each byte gets the count of bits for the byte. */ 1433 tcg_out_insn(s, RRFc, POPCNT, dest, src, 0); 1434 1435 /* Multiply to sum each byte at the top of the word. */ 1436 if (type == TCG_TYPE_I32) { 1437 tcg_out_insn(s, RIL, MSFI, dest, 0x01010101); 1438 tcg_out_sh32(s, RS_SRL, dest, TCG_REG_NONE, 24); 1439 } else { 1440 tcg_out_movi(s, TCG_TYPE_I64, TCG_TMP0, 0x0101010101010101ull); 1441 tcg_out_insn(s, RRE, MSGR, dest, TCG_TMP0); 1442 tcg_out_sh64(s, RSY_SRLG, dest, dest, TCG_REG_NONE, 56); 1443 } 1444} 1445 1446static void tgen_deposit(TCGContext *s, TCGReg dest, TCGReg src, 1447 int ofs, int len, int z) 1448{ 1449 int lsb = (63 - ofs); 1450 int msb = lsb - (len - 1); 1451 tcg_out_risbg(s, dest, src, msb, lsb, ofs, z); 1452} 1453 1454static void tgen_extract(TCGContext *s, TCGReg dest, TCGReg src, 1455 int ofs, int len) 1456{ 1457 tcg_out_risbg(s, dest, src, 64 - len, 63, 64 - ofs, 1); 1458} 1459 1460static void tgen_gotoi(TCGContext *s, int cc, const tcg_insn_unit *dest) 1461{ 1462 ptrdiff_t off = tcg_pcrel_diff(s, dest) >> 1; 1463 if (off == (int16_t)off) { 1464 tcg_out_insn(s, RI, BRC, cc, off); 1465 } else if (off == (int32_t)off) { 1466 tcg_out_insn(s, RIL, BRCL, cc, off); 1467 } else { 1468 tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, (uintptr_t)dest); 1469 tcg_out_insn(s, RR, BCR, cc, TCG_TMP0); 1470 } 1471} 1472 1473static void tgen_branch(TCGContext *s, int cc, TCGLabel *l) 1474{ 1475 if (l->has_value) { 1476 tgen_gotoi(s, cc, l->u.value_ptr); 1477 } else { 1478 tcg_out16(s, RI_BRC | (cc << 4)); 1479 tcg_out_reloc(s, s->code_ptr, R_390_PC16DBL, l, 2); 1480 s->code_ptr += 1; 1481 } 1482} 1483 1484static void tgen_compare_branch(TCGContext *s, S390Opcode opc, int cc, 1485 TCGReg r1, TCGReg r2, TCGLabel *l) 1486{ 1487 tcg_out_reloc(s, s->code_ptr + 1, R_390_PC16DBL, l, 2); 1488 /* Format RIE-b */ 1489 tcg_out16(s, (opc & 0xff00) | (r1 << 4) | r2); 1490 tcg_out16(s, 0); 1491 tcg_out16(s, cc << 12 | (opc & 0xff)); 1492} 1493 1494static void tgen_compare_imm_branch(TCGContext *s, S390Opcode opc, int cc, 1495 TCGReg r1, int i2, TCGLabel *l) 1496{ 1497 tcg_out_reloc(s, s->code_ptr + 1, R_390_PC16DBL, l, 2); 1498 /* Format RIE-c */ 1499 tcg_out16(s, (opc & 0xff00) | (r1 << 4) | cc); 1500 tcg_out16(s, 0); 1501 tcg_out16(s, (i2 << 8) | (opc & 0xff)); 1502} 1503 1504static void tgen_brcond(TCGContext *s, TCGType type, TCGCond c, 1505 TCGReg r1, TCGArg c2, int c2const, TCGLabel *l) 1506{ 1507 int cc; 1508 bool is_unsigned = is_unsigned_cond(c); 1509 bool in_range; 1510 S390Opcode opc; 1511 1512 cc = tcg_cond_to_s390_cond[c]; 1513 1514 if (!c2const) { 1515 opc = (type == TCG_TYPE_I32 1516 ? (is_unsigned ? RIEb_CLRJ : RIEb_CRJ) 1517 : (is_unsigned ? RIEb_CLGRJ : RIEb_CGRJ)); 1518 tgen_compare_branch(s, opc, cc, r1, c2, l); 1519 return; 1520 } 1521 1522 /* 1523 * COMPARE IMMEDIATE AND BRANCH RELATIVE has an 8-bit immediate field. 1524 * If the immediate we've been given does not fit that range, we'll 1525 * fall back to separate compare and branch instructions using the 1526 * larger comparison range afforded by COMPARE IMMEDIATE. 1527 */ 1528 if (type == TCG_TYPE_I32) { 1529 if (is_unsigned) { 1530 opc = RIEc_CLIJ; 1531 in_range = (uint32_t)c2 == (uint8_t)c2; 1532 } else { 1533 opc = RIEc_CIJ; 1534 in_range = (int32_t)c2 == (int8_t)c2; 1535 } 1536 } else { 1537 if (is_unsigned) { 1538 opc = RIEc_CLGIJ; 1539 in_range = (uint64_t)c2 == (uint8_t)c2; 1540 } else { 1541 opc = RIEc_CGIJ; 1542 in_range = (int64_t)c2 == (int8_t)c2; 1543 } 1544 } 1545 if (in_range) { 1546 tgen_compare_imm_branch(s, opc, cc, r1, c2, l); 1547 return; 1548 } 1549 1550 cc = tgen_cmp(s, type, c, r1, c2, c2const, false); 1551 tgen_branch(s, cc, l); 1552} 1553 1554static void tcg_out_call_int(TCGContext *s, const tcg_insn_unit *dest) 1555{ 1556 ptrdiff_t off = tcg_pcrel_diff(s, dest) >> 1; 1557 if (off == (int32_t)off) { 1558 tcg_out_insn(s, RIL, BRASL, TCG_REG_R14, off); 1559 } else { 1560 tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, (uintptr_t)dest); 1561 tcg_out_insn(s, RR, BASR, TCG_REG_R14, TCG_TMP0); 1562 } 1563} 1564 1565static void tcg_out_call(TCGContext *s, const tcg_insn_unit *dest, 1566 const TCGHelperInfo *info) 1567{ 1568 tcg_out_call_int(s, dest); 1569} 1570 1571typedef struct { 1572 TCGReg base; 1573 TCGReg index; 1574 int disp; 1575} HostAddress; 1576 1577bool tcg_target_has_memory_bswap(MemOp memop) 1578{ 1579 return true; 1580} 1581 1582static void tcg_out_qemu_ld_direct(TCGContext *s, MemOp opc, TCGReg data, 1583 HostAddress h) 1584{ 1585 switch (opc & (MO_SSIZE | MO_BSWAP)) { 1586 case MO_UB: 1587 tcg_out_insn(s, RXY, LLGC, data, h.base, h.index, h.disp); 1588 break; 1589 case MO_SB: 1590 tcg_out_insn(s, RXY, LGB, data, h.base, h.index, h.disp); 1591 break; 1592 1593 case MO_UW | MO_BSWAP: 1594 /* swapped unsigned halfword load with upper bits zeroed */ 1595 tcg_out_insn(s, RXY, LRVH, data, h.base, h.index, h.disp); 1596 tcg_out_ext16u(s, data, data); 1597 break; 1598 case MO_UW: 1599 tcg_out_insn(s, RXY, LLGH, data, h.base, h.index, h.disp); 1600 break; 1601 1602 case MO_SW | MO_BSWAP: 1603 /* swapped sign-extended halfword load */ 1604 tcg_out_insn(s, RXY, LRVH, data, h.base, h.index, h.disp); 1605 tcg_out_ext16s(s, TCG_TYPE_REG, data, data); 1606 break; 1607 case MO_SW: 1608 tcg_out_insn(s, RXY, LGH, data, h.base, h.index, h.disp); 1609 break; 1610 1611 case MO_UL | MO_BSWAP: 1612 /* swapped unsigned int load with upper bits zeroed */ 1613 tcg_out_insn(s, RXY, LRV, data, h.base, h.index, h.disp); 1614 tcg_out_ext32u(s, data, data); 1615 break; 1616 case MO_UL: 1617 tcg_out_insn(s, RXY, LLGF, data, h.base, h.index, h.disp); 1618 break; 1619 1620 case MO_SL | MO_BSWAP: 1621 /* swapped sign-extended int load */ 1622 tcg_out_insn(s, RXY, LRV, data, h.base, h.index, h.disp); 1623 tcg_out_ext32s(s, data, data); 1624 break; 1625 case MO_SL: 1626 tcg_out_insn(s, RXY, LGF, data, h.base, h.index, h.disp); 1627 break; 1628 1629 case MO_UQ | MO_BSWAP: 1630 tcg_out_insn(s, RXY, LRVG, data, h.base, h.index, h.disp); 1631 break; 1632 case MO_UQ: 1633 tcg_out_insn(s, RXY, LG, data, h.base, h.index, h.disp); 1634 break; 1635 1636 default: 1637 g_assert_not_reached(); 1638 } 1639} 1640 1641static void tcg_out_qemu_st_direct(TCGContext *s, MemOp opc, TCGReg data, 1642 HostAddress h) 1643{ 1644 switch (opc & (MO_SIZE | MO_BSWAP)) { 1645 case MO_UB: 1646 if (h.disp >= 0 && h.disp < 0x1000) { 1647 tcg_out_insn(s, RX, STC, data, h.base, h.index, h.disp); 1648 } else { 1649 tcg_out_insn(s, RXY, STCY, data, h.base, h.index, h.disp); 1650 } 1651 break; 1652 1653 case MO_UW | MO_BSWAP: 1654 tcg_out_insn(s, RXY, STRVH, data, h.base, h.index, h.disp); 1655 break; 1656 case MO_UW: 1657 if (h.disp >= 0 && h.disp < 0x1000) { 1658 tcg_out_insn(s, RX, STH, data, h.base, h.index, h.disp); 1659 } else { 1660 tcg_out_insn(s, RXY, STHY, data, h.base, h.index, h.disp); 1661 } 1662 break; 1663 1664 case MO_UL | MO_BSWAP: 1665 tcg_out_insn(s, RXY, STRV, data, h.base, h.index, h.disp); 1666 break; 1667 case MO_UL: 1668 if (h.disp >= 0 && h.disp < 0x1000) { 1669 tcg_out_insn(s, RX, ST, data, h.base, h.index, h.disp); 1670 } else { 1671 tcg_out_insn(s, RXY, STY, data, h.base, h.index, h.disp); 1672 } 1673 break; 1674 1675 case MO_UQ | MO_BSWAP: 1676 tcg_out_insn(s, RXY, STRVG, data, h.base, h.index, h.disp); 1677 break; 1678 case MO_UQ: 1679 tcg_out_insn(s, RXY, STG, data, h.base, h.index, h.disp); 1680 break; 1681 1682 default: 1683 g_assert_not_reached(); 1684 } 1685} 1686 1687static const TCGLdstHelperParam ldst_helper_param = { 1688 .ntmp = 1, .tmp = { TCG_TMP0 } 1689}; 1690 1691static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *lb) 1692{ 1693 MemOp opc = get_memop(lb->oi); 1694 1695 if (!patch_reloc(lb->label_ptr[0], R_390_PC16DBL, 1696 (intptr_t)tcg_splitwx_to_rx(s->code_ptr), 2)) { 1697 return false; 1698 } 1699 1700 tcg_out_ld_helper_args(s, lb, &ldst_helper_param); 1701 tcg_out_call_int(s, qemu_ld_helpers[opc & MO_SIZE]); 1702 tcg_out_ld_helper_ret(s, lb, false, &ldst_helper_param); 1703 1704 tgen_gotoi(s, S390_CC_ALWAYS, lb->raddr); 1705 return true; 1706} 1707 1708static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *lb) 1709{ 1710 MemOp opc = get_memop(lb->oi); 1711 1712 if (!patch_reloc(lb->label_ptr[0], R_390_PC16DBL, 1713 (intptr_t)tcg_splitwx_to_rx(s->code_ptr), 2)) { 1714 return false; 1715 } 1716 1717 tcg_out_st_helper_args(s, lb, &ldst_helper_param); 1718 tcg_out_call_int(s, qemu_st_helpers[opc & MO_SIZE]); 1719 1720 tgen_gotoi(s, S390_CC_ALWAYS, lb->raddr); 1721 return true; 1722} 1723 1724/* 1725 * For softmmu, perform the TLB load and compare. 1726 * For useronly, perform any required alignment tests. 1727 * In both cases, return a TCGLabelQemuLdst structure if the slow path 1728 * is required and fill in @h with the host address for the fast path. 1729 */ 1730static TCGLabelQemuLdst *prepare_host_addr(TCGContext *s, HostAddress *h, 1731 TCGReg addr_reg, MemOpIdx oi, 1732 bool is_ld) 1733{ 1734 TCGLabelQemuLdst *ldst = NULL; 1735 MemOp opc = get_memop(oi); 1736 unsigned a_bits = get_alignment_bits(opc); 1737 unsigned a_mask = (1u << a_bits) - 1; 1738 1739#ifdef CONFIG_SOFTMMU 1740 unsigned s_bits = opc & MO_SIZE; 1741 unsigned s_mask = (1 << s_bits) - 1; 1742 int mem_index = get_mmuidx(oi); 1743 int fast_off = TLB_MASK_TABLE_OFS(mem_index); 1744 int mask_off = fast_off + offsetof(CPUTLBDescFast, mask); 1745 int table_off = fast_off + offsetof(CPUTLBDescFast, table); 1746 int ofs, a_off; 1747 uint64_t tlb_mask; 1748 1749 ldst = new_ldst_label(s); 1750 ldst->is_ld = is_ld; 1751 ldst->oi = oi; 1752 ldst->addrlo_reg = addr_reg; 1753 1754 tcg_out_sh64(s, RSY_SRLG, TCG_TMP0, addr_reg, TCG_REG_NONE, 1755 TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS); 1756 1757 QEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) > 0); 1758 QEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) < -(1 << 19)); 1759 tcg_out_insn(s, RXY, NG, TCG_TMP0, TCG_AREG0, TCG_REG_NONE, mask_off); 1760 tcg_out_insn(s, RXY, AG, TCG_TMP0, TCG_AREG0, TCG_REG_NONE, table_off); 1761 1762 /* 1763 * For aligned accesses, we check the first byte and include the alignment 1764 * bits within the address. For unaligned access, we check that we don't 1765 * cross pages using the address of the last byte of the access. 1766 */ 1767 a_off = (a_bits >= s_bits ? 0 : s_mask - a_mask); 1768 tlb_mask = (uint64_t)TARGET_PAGE_MASK | a_mask; 1769 if (a_off == 0) { 1770 tgen_andi_risbg(s, TCG_REG_R0, addr_reg, tlb_mask); 1771 } else { 1772 tcg_out_insn(s, RX, LA, TCG_REG_R0, addr_reg, TCG_REG_NONE, a_off); 1773 tgen_andi(s, TCG_TYPE_TL, TCG_REG_R0, tlb_mask); 1774 } 1775 1776 if (is_ld) { 1777 ofs = offsetof(CPUTLBEntry, addr_read); 1778 } else { 1779 ofs = offsetof(CPUTLBEntry, addr_write); 1780 } 1781 if (TARGET_LONG_BITS == 32) { 1782 tcg_out_insn(s, RX, C, TCG_REG_R0, TCG_TMP0, TCG_REG_NONE, ofs); 1783 } else { 1784 tcg_out_insn(s, RXY, CG, TCG_REG_R0, TCG_TMP0, TCG_REG_NONE, ofs); 1785 } 1786 1787 tcg_out16(s, RI_BRC | (S390_CC_NE << 4)); 1788 ldst->label_ptr[0] = s->code_ptr++; 1789 1790 h->index = TCG_TMP0; 1791 tcg_out_insn(s, RXY, LG, h->index, TCG_TMP0, TCG_REG_NONE, 1792 offsetof(CPUTLBEntry, addend)); 1793 1794 if (TARGET_LONG_BITS == 32) { 1795 tcg_out_insn(s, RRE, ALGFR, h->index, addr_reg); 1796 h->base = TCG_REG_NONE; 1797 } else { 1798 h->base = addr_reg; 1799 } 1800 h->disp = 0; 1801#else 1802 if (a_mask) { 1803 ldst = new_ldst_label(s); 1804 ldst->is_ld = is_ld; 1805 ldst->oi = oi; 1806 ldst->addrlo_reg = addr_reg; 1807 1808 /* We are expecting a_bits to max out at 7, much lower than TMLL. */ 1809 tcg_debug_assert(a_bits < 16); 1810 tcg_out_insn(s, RI, TMLL, addr_reg, a_mask); 1811 1812 tcg_out16(s, RI_BRC | (7 << 4)); /* CC in {1,2,3} */ 1813 ldst->label_ptr[0] = s->code_ptr++; 1814 } 1815 1816 h->base = addr_reg; 1817 if (TARGET_LONG_BITS == 32) { 1818 tcg_out_ext32u(s, TCG_TMP0, addr_reg); 1819 h->base = TCG_TMP0; 1820 } 1821 if (guest_base < 0x80000) { 1822 h->index = TCG_REG_NONE; 1823 h->disp = guest_base; 1824 } else { 1825 h->index = TCG_GUEST_BASE_REG; 1826 h->disp = 0; 1827 } 1828#endif 1829 1830 return ldst; 1831} 1832 1833static void tcg_out_qemu_ld(TCGContext* s, TCGReg data_reg, TCGReg addr_reg, 1834 MemOpIdx oi, TCGType data_type) 1835{ 1836 TCGLabelQemuLdst *ldst; 1837 HostAddress h; 1838 1839 ldst = prepare_host_addr(s, &h, addr_reg, oi, true); 1840 tcg_out_qemu_ld_direct(s, get_memop(oi), data_reg, h); 1841 1842 if (ldst) { 1843 ldst->type = data_type; 1844 ldst->datalo_reg = data_reg; 1845 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr); 1846 } 1847} 1848 1849static void tcg_out_qemu_st(TCGContext* s, TCGReg data_reg, TCGReg addr_reg, 1850 MemOpIdx oi, TCGType data_type) 1851{ 1852 TCGLabelQemuLdst *ldst; 1853 HostAddress h; 1854 1855 ldst = prepare_host_addr(s, &h, addr_reg, oi, false); 1856 tcg_out_qemu_st_direct(s, get_memop(oi), data_reg, h); 1857 1858 if (ldst) { 1859 ldst->type = data_type; 1860 ldst->datalo_reg = data_reg; 1861 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr); 1862 } 1863} 1864 1865static void tcg_out_exit_tb(TCGContext *s, uintptr_t a0) 1866{ 1867 /* Reuse the zeroing that exists for goto_ptr. */ 1868 if (a0 == 0) { 1869 tgen_gotoi(s, S390_CC_ALWAYS, tcg_code_gen_epilogue); 1870 } else { 1871 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R2, a0); 1872 tgen_gotoi(s, S390_CC_ALWAYS, tb_ret_addr); 1873 } 1874} 1875 1876static void tcg_out_goto_tb(TCGContext *s, int which) 1877{ 1878 /* 1879 * Branch displacement must be aligned for atomic patching; 1880 * see if we need to add extra nop before branch 1881 */ 1882 if (!QEMU_PTR_IS_ALIGNED(s->code_ptr + 1, 4)) { 1883 tcg_out16(s, NOP); 1884 } 1885 tcg_out16(s, RIL_BRCL | (S390_CC_ALWAYS << 4)); 1886 set_jmp_insn_offset(s, which); 1887 s->code_ptr += 2; 1888 set_jmp_reset_offset(s, which); 1889} 1890 1891void tb_target_set_jmp_target(const TranslationBlock *tb, int n, 1892 uintptr_t jmp_rx, uintptr_t jmp_rw) 1893{ 1894 if (!HAVE_FACILITY(GEN_INST_EXT)) { 1895 return; 1896 } 1897 /* patch the branch destination */ 1898 uintptr_t addr = tb->jmp_target_addr[n]; 1899 intptr_t disp = addr - (jmp_rx - 2); 1900 qatomic_set((int32_t *)jmp_rw, disp / 2); 1901 /* no need to flush icache explicitly */ 1902} 1903 1904# define OP_32_64(x) \ 1905 case glue(glue(INDEX_op_,x),_i32): \ 1906 case glue(glue(INDEX_op_,x),_i64) 1907 1908static inline void tcg_out_op(TCGContext *s, TCGOpcode opc, 1909 const TCGArg args[TCG_MAX_OP_ARGS], 1910 const int const_args[TCG_MAX_OP_ARGS]) 1911{ 1912 S390Opcode op, op2; 1913 TCGArg a0, a1, a2; 1914 1915 switch (opc) { 1916 case INDEX_op_goto_ptr: 1917 a0 = args[0]; 1918 tcg_out_insn(s, RR, BCR, S390_CC_ALWAYS, a0); 1919 break; 1920 1921 OP_32_64(ld8u): 1922 /* ??? LLC (RXY format) is only present with the extended-immediate 1923 facility, whereas LLGC is always present. */ 1924 tcg_out_mem(s, 0, RXY_LLGC, args[0], args[1], TCG_REG_NONE, args[2]); 1925 break; 1926 1927 OP_32_64(ld8s): 1928 /* ??? LB is no smaller than LGB, so no point to using it. */ 1929 tcg_out_mem(s, 0, RXY_LGB, args[0], args[1], TCG_REG_NONE, args[2]); 1930 break; 1931 1932 OP_32_64(ld16u): 1933 /* ??? LLH (RXY format) is only present with the extended-immediate 1934 facility, whereas LLGH is always present. */ 1935 tcg_out_mem(s, 0, RXY_LLGH, args[0], args[1], TCG_REG_NONE, args[2]); 1936 break; 1937 1938 case INDEX_op_ld16s_i32: 1939 tcg_out_mem(s, RX_LH, RXY_LHY, args[0], args[1], TCG_REG_NONE, args[2]); 1940 break; 1941 1942 case INDEX_op_ld_i32: 1943 tcg_out_ld(s, TCG_TYPE_I32, args[0], args[1], args[2]); 1944 break; 1945 1946 OP_32_64(st8): 1947 tcg_out_mem(s, RX_STC, RXY_STCY, args[0], args[1], 1948 TCG_REG_NONE, args[2]); 1949 break; 1950 1951 OP_32_64(st16): 1952 tcg_out_mem(s, RX_STH, RXY_STHY, args[0], args[1], 1953 TCG_REG_NONE, args[2]); 1954 break; 1955 1956 case INDEX_op_st_i32: 1957 tcg_out_st(s, TCG_TYPE_I32, args[0], args[1], args[2]); 1958 break; 1959 1960 case INDEX_op_add_i32: 1961 a0 = args[0], a1 = args[1], a2 = (int32_t)args[2]; 1962 if (const_args[2]) { 1963 do_addi_32: 1964 if (a0 == a1) { 1965 if (a2 == (int16_t)a2) { 1966 tcg_out_insn(s, RI, AHI, a0, a2); 1967 break; 1968 } 1969 tcg_out_insn(s, RIL, AFI, a0, a2); 1970 break; 1971 } 1972 tcg_out_mem(s, RX_LA, RXY_LAY, a0, a1, TCG_REG_NONE, a2); 1973 } else if (a0 == a1) { 1974 tcg_out_insn(s, RR, AR, a0, a2); 1975 } else { 1976 tcg_out_insn(s, RX, LA, a0, a1, a2, 0); 1977 } 1978 break; 1979 case INDEX_op_sub_i32: 1980 a0 = args[0], a1 = args[1], a2 = (int32_t)args[2]; 1981 if (const_args[2]) { 1982 a2 = -a2; 1983 goto do_addi_32; 1984 } else if (a0 == a1) { 1985 tcg_out_insn(s, RR, SR, a0, a2); 1986 } else { 1987 tcg_out_insn(s, RRFa, SRK, a0, a1, a2); 1988 } 1989 break; 1990 1991 case INDEX_op_and_i32: 1992 a0 = args[0], a1 = args[1], a2 = (uint32_t)args[2]; 1993 if (const_args[2]) { 1994 tcg_out_mov(s, TCG_TYPE_I32, a0, a1); 1995 tgen_andi(s, TCG_TYPE_I32, a0, a2); 1996 } else if (a0 == a1) { 1997 tcg_out_insn(s, RR, NR, a0, a2); 1998 } else { 1999 tcg_out_insn(s, RRFa, NRK, a0, a1, a2); 2000 } 2001 break; 2002 case INDEX_op_or_i32: 2003 a0 = args[0], a1 = args[1], a2 = (uint32_t)args[2]; 2004 if (const_args[2]) { 2005 tcg_out_mov(s, TCG_TYPE_I32, a0, a1); 2006 tgen_ori(s, a0, a2); 2007 } else if (a0 == a1) { 2008 tcg_out_insn(s, RR, OR, a0, a2); 2009 } else { 2010 tcg_out_insn(s, RRFa, ORK, a0, a1, a2); 2011 } 2012 break; 2013 case INDEX_op_xor_i32: 2014 a0 = args[0], a1 = args[1], a2 = (uint32_t)args[2]; 2015 if (const_args[2]) { 2016 tcg_out_mov(s, TCG_TYPE_I32, a0, a1); 2017 tcg_out_insn(s, RIL, XILF, a0, a2); 2018 } else if (a0 == a1) { 2019 tcg_out_insn(s, RR, XR, args[0], args[2]); 2020 } else { 2021 tcg_out_insn(s, RRFa, XRK, a0, a1, a2); 2022 } 2023 break; 2024 2025 case INDEX_op_andc_i32: 2026 a0 = args[0], a1 = args[1], a2 = (uint32_t)args[2]; 2027 if (const_args[2]) { 2028 tcg_out_mov(s, TCG_TYPE_I32, a0, a1); 2029 tgen_andi(s, TCG_TYPE_I32, a0, (uint32_t)~a2); 2030 } else { 2031 tcg_out_insn(s, RRFa, NCRK, a0, a1, a2); 2032 } 2033 break; 2034 case INDEX_op_orc_i32: 2035 a0 = args[0], a1 = args[1], a2 = (uint32_t)args[2]; 2036 if (const_args[2]) { 2037 tcg_out_mov(s, TCG_TYPE_I32, a0, a1); 2038 tgen_ori(s, a0, (uint32_t)~a2); 2039 } else { 2040 tcg_out_insn(s, RRFa, OCRK, a0, a1, a2); 2041 } 2042 break; 2043 case INDEX_op_eqv_i32: 2044 a0 = args[0], a1 = args[1], a2 = (uint32_t)args[2]; 2045 if (const_args[2]) { 2046 tcg_out_mov(s, TCG_TYPE_I32, a0, a1); 2047 tcg_out_insn(s, RIL, XILF, a0, ~a2); 2048 } else { 2049 tcg_out_insn(s, RRFa, NXRK, a0, a1, a2); 2050 } 2051 break; 2052 case INDEX_op_nand_i32: 2053 tcg_out_insn(s, RRFa, NNRK, args[0], args[1], args[2]); 2054 break; 2055 case INDEX_op_nor_i32: 2056 tcg_out_insn(s, RRFa, NORK, args[0], args[1], args[2]); 2057 break; 2058 2059 case INDEX_op_neg_i32: 2060 tcg_out_insn(s, RR, LCR, args[0], args[1]); 2061 break; 2062 case INDEX_op_not_i32: 2063 tcg_out_insn(s, RRFa, NORK, args[0], args[1], args[1]); 2064 break; 2065 2066 case INDEX_op_mul_i32: 2067 a0 = args[0], a1 = args[1], a2 = (int32_t)args[2]; 2068 if (const_args[2]) { 2069 tcg_out_mov(s, TCG_TYPE_I32, a0, a1); 2070 if (a2 == (int16_t)a2) { 2071 tcg_out_insn(s, RI, MHI, a0, a2); 2072 } else { 2073 tcg_out_insn(s, RIL, MSFI, a0, a2); 2074 } 2075 } else if (a0 == a1) { 2076 tcg_out_insn(s, RRE, MSR, a0, a2); 2077 } else { 2078 tcg_out_insn(s, RRFa, MSRKC, a0, a1, a2); 2079 } 2080 break; 2081 2082 case INDEX_op_div2_i32: 2083 tcg_debug_assert(args[0] == args[2]); 2084 tcg_debug_assert(args[1] == args[3]); 2085 tcg_debug_assert((args[1] & 1) == 0); 2086 tcg_debug_assert(args[0] == args[1] + 1); 2087 tcg_out_insn(s, RR, DR, args[1], args[4]); 2088 break; 2089 case INDEX_op_divu2_i32: 2090 tcg_debug_assert(args[0] == args[2]); 2091 tcg_debug_assert(args[1] == args[3]); 2092 tcg_debug_assert((args[1] & 1) == 0); 2093 tcg_debug_assert(args[0] == args[1] + 1); 2094 tcg_out_insn(s, RRE, DLR, args[1], args[4]); 2095 break; 2096 2097 case INDEX_op_shl_i32: 2098 op = RS_SLL; 2099 op2 = RSY_SLLK; 2100 do_shift32: 2101 a0 = args[0], a1 = args[1], a2 = (int32_t)args[2]; 2102 if (a0 == a1) { 2103 if (const_args[2]) { 2104 tcg_out_sh32(s, op, a0, TCG_REG_NONE, a2); 2105 } else { 2106 tcg_out_sh32(s, op, a0, a2, 0); 2107 } 2108 } else { 2109 /* Using tcg_out_sh64 here for the format; it is a 32-bit shift. */ 2110 if (const_args[2]) { 2111 tcg_out_sh64(s, op2, a0, a1, TCG_REG_NONE, a2); 2112 } else { 2113 tcg_out_sh64(s, op2, a0, a1, a2, 0); 2114 } 2115 } 2116 break; 2117 case INDEX_op_shr_i32: 2118 op = RS_SRL; 2119 op2 = RSY_SRLK; 2120 goto do_shift32; 2121 case INDEX_op_sar_i32: 2122 op = RS_SRA; 2123 op2 = RSY_SRAK; 2124 goto do_shift32; 2125 2126 case INDEX_op_rotl_i32: 2127 /* ??? Using tcg_out_sh64 here for the format; it is a 32-bit rol. */ 2128 if (const_args[2]) { 2129 tcg_out_sh64(s, RSY_RLL, args[0], args[1], TCG_REG_NONE, args[2]); 2130 } else { 2131 tcg_out_sh64(s, RSY_RLL, args[0], args[1], args[2], 0); 2132 } 2133 break; 2134 case INDEX_op_rotr_i32: 2135 if (const_args[2]) { 2136 tcg_out_sh64(s, RSY_RLL, args[0], args[1], 2137 TCG_REG_NONE, (32 - args[2]) & 31); 2138 } else { 2139 tcg_out_insn(s, RR, LCR, TCG_TMP0, args[2]); 2140 tcg_out_sh64(s, RSY_RLL, args[0], args[1], TCG_TMP0, 0); 2141 } 2142 break; 2143 2144 case INDEX_op_bswap16_i32: 2145 a0 = args[0], a1 = args[1], a2 = args[2]; 2146 tcg_out_insn(s, RRE, LRVR, a0, a1); 2147 if (a2 & TCG_BSWAP_OS) { 2148 tcg_out_sh32(s, RS_SRA, a0, TCG_REG_NONE, 16); 2149 } else { 2150 tcg_out_sh32(s, RS_SRL, a0, TCG_REG_NONE, 16); 2151 } 2152 break; 2153 case INDEX_op_bswap16_i64: 2154 a0 = args[0], a1 = args[1], a2 = args[2]; 2155 tcg_out_insn(s, RRE, LRVGR, a0, a1); 2156 if (a2 & TCG_BSWAP_OS) { 2157 tcg_out_sh64(s, RSY_SRAG, a0, a0, TCG_REG_NONE, 48); 2158 } else { 2159 tcg_out_sh64(s, RSY_SRLG, a0, a0, TCG_REG_NONE, 48); 2160 } 2161 break; 2162 2163 case INDEX_op_bswap32_i32: 2164 tcg_out_insn(s, RRE, LRVR, args[0], args[1]); 2165 break; 2166 case INDEX_op_bswap32_i64: 2167 a0 = args[0], a1 = args[1], a2 = args[2]; 2168 tcg_out_insn(s, RRE, LRVR, a0, a1); 2169 if (a2 & TCG_BSWAP_OS) { 2170 tcg_out_ext32s(s, a0, a0); 2171 } else if ((a2 & (TCG_BSWAP_IZ | TCG_BSWAP_OZ)) == TCG_BSWAP_OZ) { 2172 tcg_out_ext32u(s, a0, a0); 2173 } 2174 break; 2175 2176 case INDEX_op_add2_i32: 2177 if (const_args[4]) { 2178 tcg_out_insn(s, RIL, ALFI, args[0], args[4]); 2179 } else { 2180 tcg_out_insn(s, RR, ALR, args[0], args[4]); 2181 } 2182 tcg_out_insn(s, RRE, ALCR, args[1], args[5]); 2183 break; 2184 case INDEX_op_sub2_i32: 2185 if (const_args[4]) { 2186 tcg_out_insn(s, RIL, SLFI, args[0], args[4]); 2187 } else { 2188 tcg_out_insn(s, RR, SLR, args[0], args[4]); 2189 } 2190 tcg_out_insn(s, RRE, SLBR, args[1], args[5]); 2191 break; 2192 2193 case INDEX_op_br: 2194 tgen_branch(s, S390_CC_ALWAYS, arg_label(args[0])); 2195 break; 2196 2197 case INDEX_op_brcond_i32: 2198 tgen_brcond(s, TCG_TYPE_I32, args[2], args[0], 2199 args[1], const_args[1], arg_label(args[3])); 2200 break; 2201 case INDEX_op_setcond_i32: 2202 tgen_setcond(s, TCG_TYPE_I32, args[3], args[0], args[1], 2203 args[2], const_args[2]); 2204 break; 2205 case INDEX_op_movcond_i32: 2206 tgen_movcond(s, TCG_TYPE_I32, args[5], args[0], args[1], 2207 args[2], const_args[2], args[3], const_args[3], args[4]); 2208 break; 2209 2210 case INDEX_op_qemu_ld_i32: 2211 tcg_out_qemu_ld(s, args[0], args[1], args[2], TCG_TYPE_I32); 2212 break; 2213 case INDEX_op_qemu_ld_i64: 2214 tcg_out_qemu_ld(s, args[0], args[1], args[2], TCG_TYPE_I64); 2215 break; 2216 case INDEX_op_qemu_st_i32: 2217 tcg_out_qemu_st(s, args[0], args[1], args[2], TCG_TYPE_I32); 2218 break; 2219 case INDEX_op_qemu_st_i64: 2220 tcg_out_qemu_st(s, args[0], args[1], args[2], TCG_TYPE_I64); 2221 break; 2222 2223 case INDEX_op_ld16s_i64: 2224 tcg_out_mem(s, 0, RXY_LGH, args[0], args[1], TCG_REG_NONE, args[2]); 2225 break; 2226 case INDEX_op_ld32u_i64: 2227 tcg_out_mem(s, 0, RXY_LLGF, args[0], args[1], TCG_REG_NONE, args[2]); 2228 break; 2229 case INDEX_op_ld32s_i64: 2230 tcg_out_mem(s, 0, RXY_LGF, args[0], args[1], TCG_REG_NONE, args[2]); 2231 break; 2232 case INDEX_op_ld_i64: 2233 tcg_out_ld(s, TCG_TYPE_I64, args[0], args[1], args[2]); 2234 break; 2235 2236 case INDEX_op_st32_i64: 2237 tcg_out_st(s, TCG_TYPE_I32, args[0], args[1], args[2]); 2238 break; 2239 case INDEX_op_st_i64: 2240 tcg_out_st(s, TCG_TYPE_I64, args[0], args[1], args[2]); 2241 break; 2242 2243 case INDEX_op_add_i64: 2244 a0 = args[0], a1 = args[1], a2 = args[2]; 2245 if (const_args[2]) { 2246 do_addi_64: 2247 if (a0 == a1) { 2248 if (a2 == (int16_t)a2) { 2249 tcg_out_insn(s, RI, AGHI, a0, a2); 2250 break; 2251 } 2252 if (a2 == (int32_t)a2) { 2253 tcg_out_insn(s, RIL, AGFI, a0, a2); 2254 break; 2255 } 2256 if (a2 == (uint32_t)a2) { 2257 tcg_out_insn(s, RIL, ALGFI, a0, a2); 2258 break; 2259 } 2260 if (-a2 == (uint32_t)-a2) { 2261 tcg_out_insn(s, RIL, SLGFI, a0, -a2); 2262 break; 2263 } 2264 } 2265 tcg_out_mem(s, RX_LA, RXY_LAY, a0, a1, TCG_REG_NONE, a2); 2266 } else if (a0 == a1) { 2267 tcg_out_insn(s, RRE, AGR, a0, a2); 2268 } else { 2269 tcg_out_insn(s, RX, LA, a0, a1, a2, 0); 2270 } 2271 break; 2272 case INDEX_op_sub_i64: 2273 a0 = args[0], a1 = args[1], a2 = args[2]; 2274 if (const_args[2]) { 2275 a2 = -a2; 2276 goto do_addi_64; 2277 } else { 2278 tcg_out_insn(s, RRFa, SGRK, a0, a1, a2); 2279 } 2280 break; 2281 2282 case INDEX_op_and_i64: 2283 a0 = args[0], a1 = args[1], a2 = args[2]; 2284 if (const_args[2]) { 2285 tcg_out_mov(s, TCG_TYPE_I64, a0, a1); 2286 tgen_andi(s, TCG_TYPE_I64, args[0], args[2]); 2287 } else { 2288 tcg_out_insn(s, RRFa, NGRK, a0, a1, a2); 2289 } 2290 break; 2291 case INDEX_op_or_i64: 2292 a0 = args[0], a1 = args[1], a2 = args[2]; 2293 if (const_args[2]) { 2294 tcg_out_mov(s, TCG_TYPE_I64, a0, a1); 2295 tgen_ori(s, a0, a2); 2296 } else { 2297 tcg_out_insn(s, RRFa, OGRK, a0, a1, a2); 2298 } 2299 break; 2300 case INDEX_op_xor_i64: 2301 a0 = args[0], a1 = args[1], a2 = args[2]; 2302 if (const_args[2]) { 2303 tcg_out_mov(s, TCG_TYPE_I64, a0, a1); 2304 tgen_xori(s, a0, a2); 2305 } else { 2306 tcg_out_insn(s, RRFa, XGRK, a0, a1, a2); 2307 } 2308 break; 2309 2310 case INDEX_op_andc_i64: 2311 a0 = args[0], a1 = args[1], a2 = args[2]; 2312 if (const_args[2]) { 2313 tcg_out_mov(s, TCG_TYPE_I64, a0, a1); 2314 tgen_andi(s, TCG_TYPE_I64, a0, ~a2); 2315 } else { 2316 tcg_out_insn(s, RRFa, NCGRK, a0, a1, a2); 2317 } 2318 break; 2319 case INDEX_op_orc_i64: 2320 a0 = args[0], a1 = args[1], a2 = args[2]; 2321 if (const_args[2]) { 2322 tcg_out_mov(s, TCG_TYPE_I64, a0, a1); 2323 tgen_ori(s, a0, ~a2); 2324 } else { 2325 tcg_out_insn(s, RRFa, OCGRK, a0, a1, a2); 2326 } 2327 break; 2328 case INDEX_op_eqv_i64: 2329 a0 = args[0], a1 = args[1], a2 = args[2]; 2330 if (const_args[2]) { 2331 tcg_out_mov(s, TCG_TYPE_I64, a0, a1); 2332 tgen_xori(s, a0, ~a2); 2333 } else { 2334 tcg_out_insn(s, RRFa, NXGRK, a0, a1, a2); 2335 } 2336 break; 2337 case INDEX_op_nand_i64: 2338 tcg_out_insn(s, RRFa, NNGRK, args[0], args[1], args[2]); 2339 break; 2340 case INDEX_op_nor_i64: 2341 tcg_out_insn(s, RRFa, NOGRK, args[0], args[1], args[2]); 2342 break; 2343 2344 case INDEX_op_neg_i64: 2345 tcg_out_insn(s, RRE, LCGR, args[0], args[1]); 2346 break; 2347 case INDEX_op_not_i64: 2348 tcg_out_insn(s, RRFa, NOGRK, args[0], args[1], args[1]); 2349 break; 2350 case INDEX_op_bswap64_i64: 2351 tcg_out_insn(s, RRE, LRVGR, args[0], args[1]); 2352 break; 2353 2354 case INDEX_op_mul_i64: 2355 a0 = args[0], a1 = args[1], a2 = args[2]; 2356 if (const_args[2]) { 2357 tcg_out_mov(s, TCG_TYPE_I64, a0, a1); 2358 if (a2 == (int16_t)a2) { 2359 tcg_out_insn(s, RI, MGHI, a0, a2); 2360 } else { 2361 tcg_out_insn(s, RIL, MSGFI, a0, a2); 2362 } 2363 } else if (a0 == a1) { 2364 tcg_out_insn(s, RRE, MSGR, a0, a2); 2365 } else { 2366 tcg_out_insn(s, RRFa, MSGRKC, a0, a1, a2); 2367 } 2368 break; 2369 2370 case INDEX_op_div2_i64: 2371 /* 2372 * ??? We get an unnecessary sign-extension of the dividend 2373 * into op0 with this definition, but as we do in fact always 2374 * produce both quotient and remainder using INDEX_op_div_i64 2375 * instead requires jumping through even more hoops. 2376 */ 2377 tcg_debug_assert(args[0] == args[2]); 2378 tcg_debug_assert(args[1] == args[3]); 2379 tcg_debug_assert((args[1] & 1) == 0); 2380 tcg_debug_assert(args[0] == args[1] + 1); 2381 tcg_out_insn(s, RRE, DSGR, args[1], args[4]); 2382 break; 2383 case INDEX_op_divu2_i64: 2384 tcg_debug_assert(args[0] == args[2]); 2385 tcg_debug_assert(args[1] == args[3]); 2386 tcg_debug_assert((args[1] & 1) == 0); 2387 tcg_debug_assert(args[0] == args[1] + 1); 2388 tcg_out_insn(s, RRE, DLGR, args[1], args[4]); 2389 break; 2390 case INDEX_op_mulu2_i64: 2391 tcg_debug_assert(args[0] == args[2]); 2392 tcg_debug_assert((args[1] & 1) == 0); 2393 tcg_debug_assert(args[0] == args[1] + 1); 2394 tcg_out_insn(s, RRE, MLGR, args[1], args[3]); 2395 break; 2396 case INDEX_op_muls2_i64: 2397 tcg_debug_assert((args[1] & 1) == 0); 2398 tcg_debug_assert(args[0] == args[1] + 1); 2399 tcg_out_insn(s, RRFa, MGRK, args[1], args[2], args[3]); 2400 break; 2401 2402 case INDEX_op_shl_i64: 2403 op = RSY_SLLG; 2404 do_shift64: 2405 if (const_args[2]) { 2406 tcg_out_sh64(s, op, args[0], args[1], TCG_REG_NONE, args[2]); 2407 } else { 2408 tcg_out_sh64(s, op, args[0], args[1], args[2], 0); 2409 } 2410 break; 2411 case INDEX_op_shr_i64: 2412 op = RSY_SRLG; 2413 goto do_shift64; 2414 case INDEX_op_sar_i64: 2415 op = RSY_SRAG; 2416 goto do_shift64; 2417 2418 case INDEX_op_rotl_i64: 2419 if (const_args[2]) { 2420 tcg_out_sh64(s, RSY_RLLG, args[0], args[1], 2421 TCG_REG_NONE, args[2]); 2422 } else { 2423 tcg_out_sh64(s, RSY_RLLG, args[0], args[1], args[2], 0); 2424 } 2425 break; 2426 case INDEX_op_rotr_i64: 2427 if (const_args[2]) { 2428 tcg_out_sh64(s, RSY_RLLG, args[0], args[1], 2429 TCG_REG_NONE, (64 - args[2]) & 63); 2430 } else { 2431 /* We can use the smaller 32-bit negate because only the 2432 low 6 bits are examined for the rotate. */ 2433 tcg_out_insn(s, RR, LCR, TCG_TMP0, args[2]); 2434 tcg_out_sh64(s, RSY_RLLG, args[0], args[1], TCG_TMP0, 0); 2435 } 2436 break; 2437 2438 case INDEX_op_add2_i64: 2439 if (const_args[4]) { 2440 if ((int64_t)args[4] >= 0) { 2441 tcg_out_insn(s, RIL, ALGFI, args[0], args[4]); 2442 } else { 2443 tcg_out_insn(s, RIL, SLGFI, args[0], -args[4]); 2444 } 2445 } else { 2446 tcg_out_insn(s, RRE, ALGR, args[0], args[4]); 2447 } 2448 tcg_out_insn(s, RRE, ALCGR, args[1], args[5]); 2449 break; 2450 case INDEX_op_sub2_i64: 2451 if (const_args[4]) { 2452 if ((int64_t)args[4] >= 0) { 2453 tcg_out_insn(s, RIL, SLGFI, args[0], args[4]); 2454 } else { 2455 tcg_out_insn(s, RIL, ALGFI, args[0], -args[4]); 2456 } 2457 } else { 2458 tcg_out_insn(s, RRE, SLGR, args[0], args[4]); 2459 } 2460 tcg_out_insn(s, RRE, SLBGR, args[1], args[5]); 2461 break; 2462 2463 case INDEX_op_brcond_i64: 2464 tgen_brcond(s, TCG_TYPE_I64, args[2], args[0], 2465 args[1], const_args[1], arg_label(args[3])); 2466 break; 2467 case INDEX_op_setcond_i64: 2468 tgen_setcond(s, TCG_TYPE_I64, args[3], args[0], args[1], 2469 args[2], const_args[2]); 2470 break; 2471 case INDEX_op_movcond_i64: 2472 tgen_movcond(s, TCG_TYPE_I64, args[5], args[0], args[1], 2473 args[2], const_args[2], args[3], const_args[3], args[4]); 2474 break; 2475 2476 OP_32_64(deposit): 2477 a0 = args[0], a1 = args[1], a2 = args[2]; 2478 if (const_args[1]) { 2479 tgen_deposit(s, a0, a2, args[3], args[4], 1); 2480 } else { 2481 /* Since we can't support "0Z" as a constraint, we allow a1 in 2482 any register. Fix things up as if a matching constraint. */ 2483 if (a0 != a1) { 2484 TCGType type = (opc == INDEX_op_deposit_i64); 2485 if (a0 == a2) { 2486 tcg_out_mov(s, type, TCG_TMP0, a2); 2487 a2 = TCG_TMP0; 2488 } 2489 tcg_out_mov(s, type, a0, a1); 2490 } 2491 tgen_deposit(s, a0, a2, args[3], args[4], 0); 2492 } 2493 break; 2494 2495 OP_32_64(extract): 2496 tgen_extract(s, args[0], args[1], args[2], args[3]); 2497 break; 2498 2499 case INDEX_op_clz_i64: 2500 tgen_clz(s, args[0], args[1], args[2], const_args[2]); 2501 break; 2502 2503 case INDEX_op_ctpop_i32: 2504 tgen_ctpop(s, TCG_TYPE_I32, args[0], args[1]); 2505 break; 2506 case INDEX_op_ctpop_i64: 2507 tgen_ctpop(s, TCG_TYPE_I64, args[0], args[1]); 2508 break; 2509 2510 case INDEX_op_mb: 2511 /* The host memory model is quite strong, we simply need to 2512 serialize the instruction stream. */ 2513 if (args[0] & TCG_MO_ST_LD) { 2514 /* fast-bcr-serialization facility (45) is present */ 2515 tcg_out_insn(s, RR, BCR, 14, 0); 2516 } 2517 break; 2518 2519 case INDEX_op_mov_i32: /* Always emitted via tcg_out_mov. */ 2520 case INDEX_op_mov_i64: 2521 case INDEX_op_call: /* Always emitted via tcg_out_call. */ 2522 case INDEX_op_exit_tb: /* Always emitted via tcg_out_exit_tb. */ 2523 case INDEX_op_goto_tb: /* Always emitted via tcg_out_goto_tb. */ 2524 case INDEX_op_ext8s_i32: /* Always emitted via tcg_reg_alloc_op. */ 2525 case INDEX_op_ext8s_i64: 2526 case INDEX_op_ext8u_i32: 2527 case INDEX_op_ext8u_i64: 2528 case INDEX_op_ext16s_i32: 2529 case INDEX_op_ext16s_i64: 2530 case INDEX_op_ext16u_i32: 2531 case INDEX_op_ext16u_i64: 2532 case INDEX_op_ext32s_i64: 2533 case INDEX_op_ext32u_i64: 2534 case INDEX_op_ext_i32_i64: 2535 case INDEX_op_extu_i32_i64: 2536 case INDEX_op_extrl_i64_i32: 2537 default: 2538 g_assert_not_reached(); 2539 } 2540} 2541 2542static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece, 2543 TCGReg dst, TCGReg src) 2544{ 2545 if (is_general_reg(src)) { 2546 /* Replicate general register into two MO_64. */ 2547 tcg_out_insn(s, VRRf, VLVGP, dst, src, src); 2548 if (vece == MO_64) { 2549 return true; 2550 } 2551 src = dst; 2552 } 2553 2554 /* 2555 * Recall that the "standard" integer, within a vector, is the 2556 * rightmost element of the leftmost doubleword, a-la VLLEZ. 2557 */ 2558 tcg_out_insn(s, VRIc, VREP, dst, (8 >> vece) - 1, src, vece); 2559 return true; 2560} 2561 2562static bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece, 2563 TCGReg dst, TCGReg base, intptr_t offset) 2564{ 2565 tcg_out_vrx_mem(s, VRX_VLREP, dst, base, TCG_REG_NONE, offset, vece); 2566 return true; 2567} 2568 2569static void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece, 2570 TCGReg dst, int64_t val) 2571{ 2572 int i, mask, msb, lsb; 2573 2574 /* Look for int16_t elements. */ 2575 if (vece <= MO_16 || 2576 (vece == MO_32 ? (int32_t)val : val) == (int16_t)val) { 2577 tcg_out_insn(s, VRIa, VREPI, dst, val, vece); 2578 return; 2579 } 2580 2581 /* Look for bit masks. */ 2582 if (vece == MO_32) { 2583 if (risbg_mask((int32_t)val)) { 2584 /* Handle wraparound by swapping msb and lsb. */ 2585 if ((val & 0x80000001u) == 0x80000001u) { 2586 msb = 32 - ctz32(~val); 2587 lsb = clz32(~val) - 1; 2588 } else { 2589 msb = clz32(val); 2590 lsb = 31 - ctz32(val); 2591 } 2592 tcg_out_insn(s, VRIb, VGM, dst, msb, lsb, MO_32); 2593 return; 2594 } 2595 } else { 2596 if (risbg_mask(val)) { 2597 /* Handle wraparound by swapping msb and lsb. */ 2598 if ((val & 0x8000000000000001ull) == 0x8000000000000001ull) { 2599 /* Handle wraparound by swapping msb and lsb. */ 2600 msb = 64 - ctz64(~val); 2601 lsb = clz64(~val) - 1; 2602 } else { 2603 msb = clz64(val); 2604 lsb = 63 - ctz64(val); 2605 } 2606 tcg_out_insn(s, VRIb, VGM, dst, msb, lsb, MO_64); 2607 return; 2608 } 2609 } 2610 2611 /* Look for all bytes 0x00 or 0xff. */ 2612 for (i = mask = 0; i < 8; i++) { 2613 uint8_t byte = val >> (i * 8); 2614 if (byte == 0xff) { 2615 mask |= 1 << i; 2616 } else if (byte != 0) { 2617 break; 2618 } 2619 } 2620 if (i == 8) { 2621 tcg_out_insn(s, VRIa, VGBM, dst, mask * 0x0101, 0); 2622 return; 2623 } 2624 2625 /* Otherwise, stuff it in the constant pool. */ 2626 tcg_out_insn(s, RIL, LARL, TCG_TMP0, 0); 2627 new_pool_label(s, val, R_390_PC32DBL, s->code_ptr - 2, 2); 2628 tcg_out_insn(s, VRX, VLREP, dst, TCG_TMP0, TCG_REG_NONE, 0, MO_64); 2629} 2630 2631static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, 2632 unsigned vecl, unsigned vece, 2633 const TCGArg args[TCG_MAX_OP_ARGS], 2634 const int const_args[TCG_MAX_OP_ARGS]) 2635{ 2636 TCGType type = vecl + TCG_TYPE_V64; 2637 TCGArg a0 = args[0], a1 = args[1], a2 = args[2]; 2638 2639 switch (opc) { 2640 case INDEX_op_ld_vec: 2641 tcg_out_ld(s, type, a0, a1, a2); 2642 break; 2643 case INDEX_op_st_vec: 2644 tcg_out_st(s, type, a0, a1, a2); 2645 break; 2646 case INDEX_op_dupm_vec: 2647 tcg_out_dupm_vec(s, type, vece, a0, a1, a2); 2648 break; 2649 2650 case INDEX_op_abs_vec: 2651 tcg_out_insn(s, VRRa, VLP, a0, a1, vece); 2652 break; 2653 case INDEX_op_neg_vec: 2654 tcg_out_insn(s, VRRa, VLC, a0, a1, vece); 2655 break; 2656 case INDEX_op_not_vec: 2657 tcg_out_insn(s, VRRc, VNO, a0, a1, a1, 0); 2658 break; 2659 2660 case INDEX_op_add_vec: 2661 tcg_out_insn(s, VRRc, VA, a0, a1, a2, vece); 2662 break; 2663 case INDEX_op_sub_vec: 2664 tcg_out_insn(s, VRRc, VS, a0, a1, a2, vece); 2665 break; 2666 case INDEX_op_and_vec: 2667 tcg_out_insn(s, VRRc, VN, a0, a1, a2, 0); 2668 break; 2669 case INDEX_op_andc_vec: 2670 tcg_out_insn(s, VRRc, VNC, a0, a1, a2, 0); 2671 break; 2672 case INDEX_op_mul_vec: 2673 tcg_out_insn(s, VRRc, VML, a0, a1, a2, vece); 2674 break; 2675 case INDEX_op_or_vec: 2676 tcg_out_insn(s, VRRc, VO, a0, a1, a2, 0); 2677 break; 2678 case INDEX_op_orc_vec: 2679 tcg_out_insn(s, VRRc, VOC, a0, a1, a2, 0); 2680 break; 2681 case INDEX_op_xor_vec: 2682 tcg_out_insn(s, VRRc, VX, a0, a1, a2, 0); 2683 break; 2684 case INDEX_op_nand_vec: 2685 tcg_out_insn(s, VRRc, VNN, a0, a1, a2, 0); 2686 break; 2687 case INDEX_op_nor_vec: 2688 tcg_out_insn(s, VRRc, VNO, a0, a1, a2, 0); 2689 break; 2690 case INDEX_op_eqv_vec: 2691 tcg_out_insn(s, VRRc, VNX, a0, a1, a2, 0); 2692 break; 2693 2694 case INDEX_op_shli_vec: 2695 tcg_out_insn(s, VRSa, VESL, a0, a2, TCG_REG_NONE, a1, vece); 2696 break; 2697 case INDEX_op_shri_vec: 2698 tcg_out_insn(s, VRSa, VESRL, a0, a2, TCG_REG_NONE, a1, vece); 2699 break; 2700 case INDEX_op_sari_vec: 2701 tcg_out_insn(s, VRSa, VESRA, a0, a2, TCG_REG_NONE, a1, vece); 2702 break; 2703 case INDEX_op_rotli_vec: 2704 tcg_out_insn(s, VRSa, VERLL, a0, a2, TCG_REG_NONE, a1, vece); 2705 break; 2706 case INDEX_op_shls_vec: 2707 tcg_out_insn(s, VRSa, VESL, a0, 0, a2, a1, vece); 2708 break; 2709 case INDEX_op_shrs_vec: 2710 tcg_out_insn(s, VRSa, VESRL, a0, 0, a2, a1, vece); 2711 break; 2712 case INDEX_op_sars_vec: 2713 tcg_out_insn(s, VRSa, VESRA, a0, 0, a2, a1, vece); 2714 break; 2715 case INDEX_op_rotls_vec: 2716 tcg_out_insn(s, VRSa, VERLL, a0, 0, a2, a1, vece); 2717 break; 2718 case INDEX_op_shlv_vec: 2719 tcg_out_insn(s, VRRc, VESLV, a0, a1, a2, vece); 2720 break; 2721 case INDEX_op_shrv_vec: 2722 tcg_out_insn(s, VRRc, VESRLV, a0, a1, a2, vece); 2723 break; 2724 case INDEX_op_sarv_vec: 2725 tcg_out_insn(s, VRRc, VESRAV, a0, a1, a2, vece); 2726 break; 2727 case INDEX_op_rotlv_vec: 2728 tcg_out_insn(s, VRRc, VERLLV, a0, a1, a2, vece); 2729 break; 2730 2731 case INDEX_op_smin_vec: 2732 tcg_out_insn(s, VRRc, VMN, a0, a1, a2, vece); 2733 break; 2734 case INDEX_op_smax_vec: 2735 tcg_out_insn(s, VRRc, VMX, a0, a1, a2, vece); 2736 break; 2737 case INDEX_op_umin_vec: 2738 tcg_out_insn(s, VRRc, VMNL, a0, a1, a2, vece); 2739 break; 2740 case INDEX_op_umax_vec: 2741 tcg_out_insn(s, VRRc, VMXL, a0, a1, a2, vece); 2742 break; 2743 2744 case INDEX_op_bitsel_vec: 2745 tcg_out_insn(s, VRRe, VSEL, a0, a2, args[3], a1); 2746 break; 2747 2748 case INDEX_op_cmp_vec: 2749 switch ((TCGCond)args[3]) { 2750 case TCG_COND_EQ: 2751 tcg_out_insn(s, VRRc, VCEQ, a0, a1, a2, vece); 2752 break; 2753 case TCG_COND_GT: 2754 tcg_out_insn(s, VRRc, VCH, a0, a1, a2, vece); 2755 break; 2756 case TCG_COND_GTU: 2757 tcg_out_insn(s, VRRc, VCHL, a0, a1, a2, vece); 2758 break; 2759 default: 2760 g_assert_not_reached(); 2761 } 2762 break; 2763 2764 case INDEX_op_s390_vuph_vec: 2765 tcg_out_insn(s, VRRa, VUPH, a0, a1, vece); 2766 break; 2767 case INDEX_op_s390_vupl_vec: 2768 tcg_out_insn(s, VRRa, VUPL, a0, a1, vece); 2769 break; 2770 case INDEX_op_s390_vpks_vec: 2771 tcg_out_insn(s, VRRc, VPKS, a0, a1, a2, vece); 2772 break; 2773 2774 case INDEX_op_mov_vec: /* Always emitted via tcg_out_mov. */ 2775 case INDEX_op_dup_vec: /* Always emitted via tcg_out_dup_vec. */ 2776 default: 2777 g_assert_not_reached(); 2778 } 2779} 2780 2781int tcg_can_emit_vec_op(TCGOpcode opc, TCGType type, unsigned vece) 2782{ 2783 switch (opc) { 2784 case INDEX_op_abs_vec: 2785 case INDEX_op_add_vec: 2786 case INDEX_op_and_vec: 2787 case INDEX_op_andc_vec: 2788 case INDEX_op_bitsel_vec: 2789 case INDEX_op_eqv_vec: 2790 case INDEX_op_nand_vec: 2791 case INDEX_op_neg_vec: 2792 case INDEX_op_nor_vec: 2793 case INDEX_op_not_vec: 2794 case INDEX_op_or_vec: 2795 case INDEX_op_orc_vec: 2796 case INDEX_op_rotli_vec: 2797 case INDEX_op_rotls_vec: 2798 case INDEX_op_rotlv_vec: 2799 case INDEX_op_sari_vec: 2800 case INDEX_op_sars_vec: 2801 case INDEX_op_sarv_vec: 2802 case INDEX_op_shli_vec: 2803 case INDEX_op_shls_vec: 2804 case INDEX_op_shlv_vec: 2805 case INDEX_op_shri_vec: 2806 case INDEX_op_shrs_vec: 2807 case INDEX_op_shrv_vec: 2808 case INDEX_op_smax_vec: 2809 case INDEX_op_smin_vec: 2810 case INDEX_op_sub_vec: 2811 case INDEX_op_umax_vec: 2812 case INDEX_op_umin_vec: 2813 case INDEX_op_xor_vec: 2814 return 1; 2815 case INDEX_op_cmp_vec: 2816 case INDEX_op_cmpsel_vec: 2817 case INDEX_op_rotrv_vec: 2818 return -1; 2819 case INDEX_op_mul_vec: 2820 return vece < MO_64; 2821 case INDEX_op_ssadd_vec: 2822 case INDEX_op_sssub_vec: 2823 return vece < MO_64 ? -1 : 0; 2824 default: 2825 return 0; 2826 } 2827} 2828 2829static bool expand_vec_cmp_noinv(TCGType type, unsigned vece, TCGv_vec v0, 2830 TCGv_vec v1, TCGv_vec v2, TCGCond cond) 2831{ 2832 bool need_swap = false, need_inv = false; 2833 2834 switch (cond) { 2835 case TCG_COND_EQ: 2836 case TCG_COND_GT: 2837 case TCG_COND_GTU: 2838 break; 2839 case TCG_COND_NE: 2840 case TCG_COND_LE: 2841 case TCG_COND_LEU: 2842 need_inv = true; 2843 break; 2844 case TCG_COND_LT: 2845 case TCG_COND_LTU: 2846 need_swap = true; 2847 break; 2848 case TCG_COND_GE: 2849 case TCG_COND_GEU: 2850 need_swap = need_inv = true; 2851 break; 2852 default: 2853 g_assert_not_reached(); 2854 } 2855 2856 if (need_inv) { 2857 cond = tcg_invert_cond(cond); 2858 } 2859 if (need_swap) { 2860 TCGv_vec t1; 2861 t1 = v1, v1 = v2, v2 = t1; 2862 cond = tcg_swap_cond(cond); 2863 } 2864 2865 vec_gen_4(INDEX_op_cmp_vec, type, vece, tcgv_vec_arg(v0), 2866 tcgv_vec_arg(v1), tcgv_vec_arg(v2), cond); 2867 2868 return need_inv; 2869} 2870 2871static void expand_vec_cmp(TCGType type, unsigned vece, TCGv_vec v0, 2872 TCGv_vec v1, TCGv_vec v2, TCGCond cond) 2873{ 2874 if (expand_vec_cmp_noinv(type, vece, v0, v1, v2, cond)) { 2875 tcg_gen_not_vec(vece, v0, v0); 2876 } 2877} 2878 2879static void expand_vec_cmpsel(TCGType type, unsigned vece, TCGv_vec v0, 2880 TCGv_vec c1, TCGv_vec c2, 2881 TCGv_vec v3, TCGv_vec v4, TCGCond cond) 2882{ 2883 TCGv_vec t = tcg_temp_new_vec(type); 2884 2885 if (expand_vec_cmp_noinv(type, vece, t, c1, c2, cond)) { 2886 /* Invert the sense of the compare by swapping arguments. */ 2887 tcg_gen_bitsel_vec(vece, v0, t, v4, v3); 2888 } else { 2889 tcg_gen_bitsel_vec(vece, v0, t, v3, v4); 2890 } 2891 tcg_temp_free_vec(t); 2892} 2893 2894static void expand_vec_sat(TCGType type, unsigned vece, TCGv_vec v0, 2895 TCGv_vec v1, TCGv_vec v2, TCGOpcode add_sub_opc) 2896{ 2897 TCGv_vec h1 = tcg_temp_new_vec(type); 2898 TCGv_vec h2 = tcg_temp_new_vec(type); 2899 TCGv_vec l1 = tcg_temp_new_vec(type); 2900 TCGv_vec l2 = tcg_temp_new_vec(type); 2901 2902 tcg_debug_assert (vece < MO_64); 2903 2904 /* Unpack with sign-extension. */ 2905 vec_gen_2(INDEX_op_s390_vuph_vec, type, vece, 2906 tcgv_vec_arg(h1), tcgv_vec_arg(v1)); 2907 vec_gen_2(INDEX_op_s390_vuph_vec, type, vece, 2908 tcgv_vec_arg(h2), tcgv_vec_arg(v2)); 2909 2910 vec_gen_2(INDEX_op_s390_vupl_vec, type, vece, 2911 tcgv_vec_arg(l1), tcgv_vec_arg(v1)); 2912 vec_gen_2(INDEX_op_s390_vupl_vec, type, vece, 2913 tcgv_vec_arg(l2), tcgv_vec_arg(v2)); 2914 2915 /* Arithmetic on a wider element size. */ 2916 vec_gen_3(add_sub_opc, type, vece + 1, tcgv_vec_arg(h1), 2917 tcgv_vec_arg(h1), tcgv_vec_arg(h2)); 2918 vec_gen_3(add_sub_opc, type, vece + 1, tcgv_vec_arg(l1), 2919 tcgv_vec_arg(l1), tcgv_vec_arg(l2)); 2920 2921 /* Pack with saturation. */ 2922 vec_gen_3(INDEX_op_s390_vpks_vec, type, vece + 1, 2923 tcgv_vec_arg(v0), tcgv_vec_arg(h1), tcgv_vec_arg(l1)); 2924 2925 tcg_temp_free_vec(h1); 2926 tcg_temp_free_vec(h2); 2927 tcg_temp_free_vec(l1); 2928 tcg_temp_free_vec(l2); 2929} 2930 2931void tcg_expand_vec_op(TCGOpcode opc, TCGType type, unsigned vece, 2932 TCGArg a0, ...) 2933{ 2934 va_list va; 2935 TCGv_vec v0, v1, v2, v3, v4, t0; 2936 2937 va_start(va, a0); 2938 v0 = temp_tcgv_vec(arg_temp(a0)); 2939 v1 = temp_tcgv_vec(arg_temp(va_arg(va, TCGArg))); 2940 v2 = temp_tcgv_vec(arg_temp(va_arg(va, TCGArg))); 2941 2942 switch (opc) { 2943 case INDEX_op_cmp_vec: 2944 expand_vec_cmp(type, vece, v0, v1, v2, va_arg(va, TCGArg)); 2945 break; 2946 2947 case INDEX_op_cmpsel_vec: 2948 v3 = temp_tcgv_vec(arg_temp(va_arg(va, TCGArg))); 2949 v4 = temp_tcgv_vec(arg_temp(va_arg(va, TCGArg))); 2950 expand_vec_cmpsel(type, vece, v0, v1, v2, v3, v4, va_arg(va, TCGArg)); 2951 break; 2952 2953 case INDEX_op_rotrv_vec: 2954 t0 = tcg_temp_new_vec(type); 2955 tcg_gen_neg_vec(vece, t0, v2); 2956 tcg_gen_rotlv_vec(vece, v0, v1, t0); 2957 tcg_temp_free_vec(t0); 2958 break; 2959 2960 case INDEX_op_ssadd_vec: 2961 expand_vec_sat(type, vece, v0, v1, v2, INDEX_op_add_vec); 2962 break; 2963 case INDEX_op_sssub_vec: 2964 expand_vec_sat(type, vece, v0, v1, v2, INDEX_op_sub_vec); 2965 break; 2966 2967 default: 2968 g_assert_not_reached(); 2969 } 2970 va_end(va); 2971} 2972 2973static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op) 2974{ 2975 switch (op) { 2976 case INDEX_op_goto_ptr: 2977 return C_O0_I1(r); 2978 2979 case INDEX_op_ld8u_i32: 2980 case INDEX_op_ld8u_i64: 2981 case INDEX_op_ld8s_i32: 2982 case INDEX_op_ld8s_i64: 2983 case INDEX_op_ld16u_i32: 2984 case INDEX_op_ld16u_i64: 2985 case INDEX_op_ld16s_i32: 2986 case INDEX_op_ld16s_i64: 2987 case INDEX_op_ld_i32: 2988 case INDEX_op_ld32u_i64: 2989 case INDEX_op_ld32s_i64: 2990 case INDEX_op_ld_i64: 2991 return C_O1_I1(r, r); 2992 2993 case INDEX_op_st8_i32: 2994 case INDEX_op_st8_i64: 2995 case INDEX_op_st16_i32: 2996 case INDEX_op_st16_i64: 2997 case INDEX_op_st_i32: 2998 case INDEX_op_st32_i64: 2999 case INDEX_op_st_i64: 3000 return C_O0_I2(r, r); 3001 3002 case INDEX_op_add_i32: 3003 case INDEX_op_add_i64: 3004 case INDEX_op_shl_i64: 3005 case INDEX_op_shr_i64: 3006 case INDEX_op_sar_i64: 3007 case INDEX_op_rotl_i32: 3008 case INDEX_op_rotl_i64: 3009 case INDEX_op_rotr_i32: 3010 case INDEX_op_rotr_i64: 3011 case INDEX_op_setcond_i32: 3012 return C_O1_I2(r, r, ri); 3013 case INDEX_op_setcond_i64: 3014 return C_O1_I2(r, r, rA); 3015 3016 case INDEX_op_clz_i64: 3017 return C_O1_I2(r, r, rI); 3018 3019 case INDEX_op_sub_i32: 3020 case INDEX_op_sub_i64: 3021 case INDEX_op_and_i32: 3022 case INDEX_op_or_i32: 3023 case INDEX_op_xor_i32: 3024 return C_O1_I2(r, r, ri); 3025 case INDEX_op_and_i64: 3026 return C_O1_I2(r, r, rNKR); 3027 case INDEX_op_or_i64: 3028 case INDEX_op_xor_i64: 3029 return C_O1_I2(r, r, rK); 3030 3031 case INDEX_op_andc_i32: 3032 case INDEX_op_orc_i32: 3033 case INDEX_op_eqv_i32: 3034 return C_O1_I2(r, r, ri); 3035 case INDEX_op_andc_i64: 3036 return C_O1_I2(r, r, rKR); 3037 case INDEX_op_orc_i64: 3038 case INDEX_op_eqv_i64: 3039 return C_O1_I2(r, r, rNK); 3040 3041 case INDEX_op_nand_i32: 3042 case INDEX_op_nand_i64: 3043 case INDEX_op_nor_i32: 3044 case INDEX_op_nor_i64: 3045 return C_O1_I2(r, r, r); 3046 3047 case INDEX_op_mul_i32: 3048 return (HAVE_FACILITY(MISC_INSN_EXT2) 3049 ? C_O1_I2(r, r, ri) 3050 : C_O1_I2(r, 0, ri)); 3051 case INDEX_op_mul_i64: 3052 return (HAVE_FACILITY(MISC_INSN_EXT2) 3053 ? C_O1_I2(r, r, rJ) 3054 : C_O1_I2(r, 0, rJ)); 3055 3056 case INDEX_op_shl_i32: 3057 case INDEX_op_shr_i32: 3058 case INDEX_op_sar_i32: 3059 return C_O1_I2(r, r, ri); 3060 3061 case INDEX_op_brcond_i32: 3062 return C_O0_I2(r, ri); 3063 case INDEX_op_brcond_i64: 3064 return C_O0_I2(r, rA); 3065 3066 case INDEX_op_bswap16_i32: 3067 case INDEX_op_bswap16_i64: 3068 case INDEX_op_bswap32_i32: 3069 case INDEX_op_bswap32_i64: 3070 case INDEX_op_bswap64_i64: 3071 case INDEX_op_neg_i32: 3072 case INDEX_op_neg_i64: 3073 case INDEX_op_not_i32: 3074 case INDEX_op_not_i64: 3075 case INDEX_op_ext8s_i32: 3076 case INDEX_op_ext8s_i64: 3077 case INDEX_op_ext8u_i32: 3078 case INDEX_op_ext8u_i64: 3079 case INDEX_op_ext16s_i32: 3080 case INDEX_op_ext16s_i64: 3081 case INDEX_op_ext16u_i32: 3082 case INDEX_op_ext16u_i64: 3083 case INDEX_op_ext32s_i64: 3084 case INDEX_op_ext32u_i64: 3085 case INDEX_op_ext_i32_i64: 3086 case INDEX_op_extu_i32_i64: 3087 case INDEX_op_extract_i32: 3088 case INDEX_op_extract_i64: 3089 case INDEX_op_ctpop_i32: 3090 case INDEX_op_ctpop_i64: 3091 return C_O1_I1(r, r); 3092 3093 case INDEX_op_qemu_ld_i32: 3094 case INDEX_op_qemu_ld_i64: 3095 return C_O1_I1(r, r); 3096 case INDEX_op_qemu_st_i64: 3097 case INDEX_op_qemu_st_i32: 3098 return C_O0_I2(r, r); 3099 3100 case INDEX_op_deposit_i32: 3101 case INDEX_op_deposit_i64: 3102 return C_O1_I2(r, rZ, r); 3103 3104 case INDEX_op_movcond_i32: 3105 return C_O1_I4(r, r, ri, rI, r); 3106 case INDEX_op_movcond_i64: 3107 return C_O1_I4(r, r, rA, rI, r); 3108 3109 case INDEX_op_div2_i32: 3110 case INDEX_op_div2_i64: 3111 case INDEX_op_divu2_i32: 3112 case INDEX_op_divu2_i64: 3113 return C_O2_I3(o, m, 0, 1, r); 3114 3115 case INDEX_op_mulu2_i64: 3116 return C_O2_I2(o, m, 0, r); 3117 case INDEX_op_muls2_i64: 3118 return C_O2_I2(o, m, r, r); 3119 3120 case INDEX_op_add2_i32: 3121 case INDEX_op_sub2_i32: 3122 return C_O2_I4(r, r, 0, 1, ri, r); 3123 3124 case INDEX_op_add2_i64: 3125 case INDEX_op_sub2_i64: 3126 return C_O2_I4(r, r, 0, 1, rA, r); 3127 3128 case INDEX_op_st_vec: 3129 return C_O0_I2(v, r); 3130 case INDEX_op_ld_vec: 3131 case INDEX_op_dupm_vec: 3132 return C_O1_I1(v, r); 3133 case INDEX_op_dup_vec: 3134 return C_O1_I1(v, vr); 3135 case INDEX_op_abs_vec: 3136 case INDEX_op_neg_vec: 3137 case INDEX_op_not_vec: 3138 case INDEX_op_rotli_vec: 3139 case INDEX_op_sari_vec: 3140 case INDEX_op_shli_vec: 3141 case INDEX_op_shri_vec: 3142 case INDEX_op_s390_vuph_vec: 3143 case INDEX_op_s390_vupl_vec: 3144 return C_O1_I1(v, v); 3145 case INDEX_op_add_vec: 3146 case INDEX_op_sub_vec: 3147 case INDEX_op_and_vec: 3148 case INDEX_op_andc_vec: 3149 case INDEX_op_or_vec: 3150 case INDEX_op_orc_vec: 3151 case INDEX_op_xor_vec: 3152 case INDEX_op_nand_vec: 3153 case INDEX_op_nor_vec: 3154 case INDEX_op_eqv_vec: 3155 case INDEX_op_cmp_vec: 3156 case INDEX_op_mul_vec: 3157 case INDEX_op_rotlv_vec: 3158 case INDEX_op_rotrv_vec: 3159 case INDEX_op_shlv_vec: 3160 case INDEX_op_shrv_vec: 3161 case INDEX_op_sarv_vec: 3162 case INDEX_op_smax_vec: 3163 case INDEX_op_smin_vec: 3164 case INDEX_op_umax_vec: 3165 case INDEX_op_umin_vec: 3166 case INDEX_op_s390_vpks_vec: 3167 return C_O1_I2(v, v, v); 3168 case INDEX_op_rotls_vec: 3169 case INDEX_op_shls_vec: 3170 case INDEX_op_shrs_vec: 3171 case INDEX_op_sars_vec: 3172 return C_O1_I2(v, v, r); 3173 case INDEX_op_bitsel_vec: 3174 return C_O1_I3(v, v, v, v); 3175 3176 default: 3177 g_assert_not_reached(); 3178 } 3179} 3180 3181/* 3182 * Mainline glibc added HWCAP_S390_VX before it was kernel abi. 3183 * Some distros have fixed this up locally, others have not. 3184 */ 3185#ifndef HWCAP_S390_VXRS 3186#define HWCAP_S390_VXRS 2048 3187#endif 3188 3189static void query_s390_facilities(void) 3190{ 3191 unsigned long hwcap = qemu_getauxval(AT_HWCAP); 3192 const char *which; 3193 3194 /* Is STORE FACILITY LIST EXTENDED available? Honestly, I believe this 3195 is present on all 64-bit systems, but let's check for it anyway. */ 3196 if (hwcap & HWCAP_S390_STFLE) { 3197 register int r0 __asm__("0") = ARRAY_SIZE(s390_facilities) - 1; 3198 register void *r1 __asm__("1") = s390_facilities; 3199 3200 /* stfle 0(%r1) */ 3201 asm volatile(".word 0xb2b0,0x1000" 3202 : "=r"(r0) : "r"(r0), "r"(r1) : "memory", "cc"); 3203 } 3204 3205 /* 3206 * Use of vector registers requires os support beyond the facility bit. 3207 * If the kernel does not advertise support, disable the facility bits. 3208 * There is nothing else we currently care about in the 3rd word, so 3209 * disable VECTOR with one store. 3210 */ 3211 if (!(hwcap & HWCAP_S390_VXRS)) { 3212 s390_facilities[2] = 0; 3213 } 3214 3215 /* 3216 * Minimum supported cpu revision is z196. 3217 * Check for all required facilities. 3218 * ZARCH_ACTIVE is done via preprocessor check for 64-bit. 3219 */ 3220 if (!HAVE_FACILITY(LONG_DISP)) { 3221 which = "long-displacement"; 3222 goto fail; 3223 } 3224 if (!HAVE_FACILITY(EXT_IMM)) { 3225 which = "extended-immediate"; 3226 goto fail; 3227 } 3228 if (!HAVE_FACILITY(GEN_INST_EXT)) { 3229 which = "general-instructions-extension"; 3230 goto fail; 3231 } 3232 /* 3233 * Facility 45 is a big bin that contains: distinct-operands, 3234 * fast-BCR-serialization, high-word, population-count, 3235 * interlocked-access-1, and load/store-on-condition-1 3236 */ 3237 if (!HAVE_FACILITY(45)) { 3238 which = "45"; 3239 goto fail; 3240 } 3241 return; 3242 3243 fail: 3244 error_report("%s: missing required facility %s", __func__, which); 3245 exit(EXIT_FAILURE); 3246} 3247 3248static void tcg_target_init(TCGContext *s) 3249{ 3250 query_s390_facilities(); 3251 3252 tcg_target_available_regs[TCG_TYPE_I32] = 0xffff; 3253 tcg_target_available_regs[TCG_TYPE_I64] = 0xffff; 3254 if (HAVE_FACILITY(VECTOR)) { 3255 tcg_target_available_regs[TCG_TYPE_V64] = 0xffffffff00000000ull; 3256 tcg_target_available_regs[TCG_TYPE_V128] = 0xffffffff00000000ull; 3257 } 3258 3259 tcg_target_call_clobber_regs = 0; 3260 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R0); 3261 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R1); 3262 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R2); 3263 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R3); 3264 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R4); 3265 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R5); 3266 /* The r6 register is technically call-saved, but it's also a parameter 3267 register, so it can get killed by setup for the qemu_st helper. */ 3268 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R6); 3269 /* The return register can be considered call-clobbered. */ 3270 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R14); 3271 3272 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V0); 3273 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V1); 3274 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V2); 3275 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V3); 3276 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V4); 3277 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V5); 3278 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V6); 3279 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V7); 3280 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V16); 3281 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V17); 3282 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V18); 3283 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V19); 3284 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V20); 3285 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V21); 3286 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V22); 3287 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V23); 3288 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V24); 3289 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V25); 3290 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V26); 3291 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V27); 3292 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V28); 3293 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V29); 3294 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V30); 3295 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V31); 3296 3297 s->reserved_regs = 0; 3298 tcg_regset_set_reg(s->reserved_regs, TCG_TMP0); 3299 /* XXX many insns can't be used with R0, so we better avoid it for now */ 3300 tcg_regset_set_reg(s->reserved_regs, TCG_REG_R0); 3301 tcg_regset_set_reg(s->reserved_regs, TCG_REG_CALL_STACK); 3302} 3303 3304#define FRAME_SIZE ((int)(TCG_TARGET_CALL_STACK_OFFSET \ 3305 + TCG_STATIC_CALL_ARGS_SIZE \ 3306 + CPU_TEMP_BUF_NLONGS * sizeof(long))) 3307 3308static void tcg_target_qemu_prologue(TCGContext *s) 3309{ 3310 /* stmg %r6,%r15,48(%r15) (save registers) */ 3311 tcg_out_insn(s, RXY, STMG, TCG_REG_R6, TCG_REG_R15, TCG_REG_R15, 48); 3312 3313 /* aghi %r15,-frame_size */ 3314 tcg_out_insn(s, RI, AGHI, TCG_REG_R15, -FRAME_SIZE); 3315 3316 tcg_set_frame(s, TCG_REG_CALL_STACK, 3317 TCG_STATIC_CALL_ARGS_SIZE + TCG_TARGET_CALL_STACK_OFFSET, 3318 CPU_TEMP_BUF_NLONGS * sizeof(long)); 3319 3320#ifndef CONFIG_SOFTMMU 3321 if (guest_base >= 0x80000) { 3322 tcg_out_movi(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG, guest_base); 3323 tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG); 3324 } 3325#endif 3326 3327 tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]); 3328 3329 /* br %r3 (go to TB) */ 3330 tcg_out_insn(s, RR, BCR, S390_CC_ALWAYS, tcg_target_call_iarg_regs[1]); 3331 3332 /* 3333 * Return path for goto_ptr. Set return value to 0, a-la exit_tb, 3334 * and fall through to the rest of the epilogue. 3335 */ 3336 tcg_code_gen_epilogue = tcg_splitwx_to_rx(s->code_ptr); 3337 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R2, 0); 3338 3339 /* TB epilogue */ 3340 tb_ret_addr = tcg_splitwx_to_rx(s->code_ptr); 3341 3342 /* lmg %r6,%r15,fs+48(%r15) (restore registers) */ 3343 tcg_out_insn(s, RXY, LMG, TCG_REG_R6, TCG_REG_R15, TCG_REG_R15, 3344 FRAME_SIZE + 48); 3345 3346 /* br %r14 (return) */ 3347 tcg_out_insn(s, RR, BCR, S390_CC_ALWAYS, TCG_REG_R14); 3348} 3349 3350static void tcg_out_nop_fill(tcg_insn_unit *p, int count) 3351{ 3352 memset(p, 0x07, count * sizeof(tcg_insn_unit)); 3353} 3354 3355typedef struct { 3356 DebugFrameHeader h; 3357 uint8_t fde_def_cfa[4]; 3358 uint8_t fde_reg_ofs[18]; 3359} DebugFrame; 3360 3361/* We're expecting a 2 byte uleb128 encoded value. */ 3362QEMU_BUILD_BUG_ON(FRAME_SIZE >= (1 << 14)); 3363 3364#define ELF_HOST_MACHINE EM_S390 3365 3366static const DebugFrame debug_frame = { 3367 .h.cie.len = sizeof(DebugFrameCIE)-4, /* length after .len member */ 3368 .h.cie.id = -1, 3369 .h.cie.version = 1, 3370 .h.cie.code_align = 1, 3371 .h.cie.data_align = 8, /* sleb128 8 */ 3372 .h.cie.return_column = TCG_REG_R14, 3373 3374 /* Total FDE size does not include the "len" member. */ 3375 .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset), 3376 3377 .fde_def_cfa = { 3378 12, TCG_REG_CALL_STACK, /* DW_CFA_def_cfa %r15, ... */ 3379 (FRAME_SIZE & 0x7f) | 0x80, /* ... uleb128 FRAME_SIZE */ 3380 (FRAME_SIZE >> 7) 3381 }, 3382 .fde_reg_ofs = { 3383 0x86, 6, /* DW_CFA_offset, %r6, 48 */ 3384 0x87, 7, /* DW_CFA_offset, %r7, 56 */ 3385 0x88, 8, /* DW_CFA_offset, %r8, 64 */ 3386 0x89, 9, /* DW_CFA_offset, %r92, 72 */ 3387 0x8a, 10, /* DW_CFA_offset, %r10, 80 */ 3388 0x8b, 11, /* DW_CFA_offset, %r11, 88 */ 3389 0x8c, 12, /* DW_CFA_offset, %r12, 96 */ 3390 0x8d, 13, /* DW_CFA_offset, %r13, 104 */ 3391 0x8e, 14, /* DW_CFA_offset, %r14, 112 */ 3392 } 3393}; 3394 3395void tcg_register_jit(const void *buf, size_t buf_size) 3396{ 3397 tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame)); 3398} 3399