1/* 2 * Tiny Code Generator for QEMU 3 * 4 * Copyright (c) 2021 WANG Xuerui <git@xen0n.name> 5 * 6 * Based on tcg/riscv/tcg-target.c.inc 7 * 8 * Copyright (c) 2018 SiFive, Inc 9 * Copyright (c) 2008-2009 Arnaud Patard <arnaud.patard@rtp-net.org> 10 * Copyright (c) 2009 Aurelien Jarno <aurelien@aurel32.net> 11 * Copyright (c) 2008 Fabrice Bellard 12 * 13 * Permission is hereby granted, free of charge, to any person obtaining a copy 14 * of this software and associated documentation files (the "Software"), to deal 15 * in the Software without restriction, including without limitation the rights 16 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17 * copies of the Software, and to permit persons to whom the Software is 18 * furnished to do so, subject to the following conditions: 19 * 20 * The above copyright notice and this permission notice shall be included in 21 * all copies or substantial portions of the Software. 22 * 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 26 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 29 * THE SOFTWARE. 30 */ 31 32#include <asm/hwcap.h> 33 34/* used for function call generation */ 35#define TCG_REG_CALL_STACK TCG_REG_SP 36#define TCG_TARGET_STACK_ALIGN 16 37#define TCG_TARGET_CALL_STACK_OFFSET 0 38#define TCG_TARGET_CALL_ARG_I32 TCG_CALL_ARG_NORMAL 39#define TCG_TARGET_CALL_ARG_I64 TCG_CALL_ARG_NORMAL 40#define TCG_TARGET_CALL_ARG_I128 TCG_CALL_ARG_NORMAL 41#define TCG_TARGET_CALL_RET_I128 TCG_CALL_RET_NORMAL 42 43#ifdef CONFIG_DEBUG_TCG 44static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = { 45 "zero", 46 "ra", 47 "tp", 48 "sp", 49 "a0", 50 "a1", 51 "a2", 52 "a3", 53 "a4", 54 "a5", 55 "a6", 56 "a7", 57 "t0", 58 "t1", 59 "t2", 60 "t3", 61 "t4", 62 "t5", 63 "t6", 64 "t7", 65 "t8", 66 "r21", /* reserved in the LP64* ABI, hence no ABI name */ 67 "s9", 68 "s0", 69 "s1", 70 "s2", 71 "s3", 72 "s4", 73 "s5", 74 "s6", 75 "s7", 76 "s8", 77 "vr0", 78 "vr1", 79 "vr2", 80 "vr3", 81 "vr4", 82 "vr5", 83 "vr6", 84 "vr7", 85 "vr8", 86 "vr9", 87 "vr10", 88 "vr11", 89 "vr12", 90 "vr13", 91 "vr14", 92 "vr15", 93 "vr16", 94 "vr17", 95 "vr18", 96 "vr19", 97 "vr20", 98 "vr21", 99 "vr22", 100 "vr23", 101 "vr24", 102 "vr25", 103 "vr26", 104 "vr27", 105 "vr28", 106 "vr29", 107 "vr30", 108 "vr31", 109}; 110#endif 111 112static const int tcg_target_reg_alloc_order[] = { 113 /* Registers preserved across calls */ 114 /* TCG_REG_S0 reserved for TCG_AREG0 */ 115 TCG_REG_S1, 116 TCG_REG_S2, 117 TCG_REG_S3, 118 TCG_REG_S4, 119 TCG_REG_S5, 120 TCG_REG_S6, 121 TCG_REG_S7, 122 TCG_REG_S8, 123 TCG_REG_S9, 124 125 /* Registers (potentially) clobbered across calls */ 126 TCG_REG_T0, 127 TCG_REG_T1, 128 TCG_REG_T2, 129 TCG_REG_T3, 130 TCG_REG_T4, 131 TCG_REG_T5, 132 TCG_REG_T6, 133 TCG_REG_T7, 134 TCG_REG_T8, 135 136 /* Argument registers, opposite order of allocation. */ 137 TCG_REG_A7, 138 TCG_REG_A6, 139 TCG_REG_A5, 140 TCG_REG_A4, 141 TCG_REG_A3, 142 TCG_REG_A2, 143 TCG_REG_A1, 144 TCG_REG_A0, 145 146 /* Vector registers */ 147 TCG_REG_V0, TCG_REG_V1, TCG_REG_V2, TCG_REG_V3, 148 TCG_REG_V4, TCG_REG_V5, TCG_REG_V6, TCG_REG_V7, 149 TCG_REG_V8, TCG_REG_V9, TCG_REG_V10, TCG_REG_V11, 150 TCG_REG_V12, TCG_REG_V13, TCG_REG_V14, TCG_REG_V15, 151 TCG_REG_V16, TCG_REG_V17, TCG_REG_V18, TCG_REG_V19, 152 TCG_REG_V20, TCG_REG_V21, TCG_REG_V22, TCG_REG_V23, 153 /* V24 - V31 are caller-saved, and skipped. */ 154}; 155 156static const int tcg_target_call_iarg_regs[] = { 157 TCG_REG_A0, 158 TCG_REG_A1, 159 TCG_REG_A2, 160 TCG_REG_A3, 161 TCG_REG_A4, 162 TCG_REG_A5, 163 TCG_REG_A6, 164 TCG_REG_A7, 165}; 166 167static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot) 168{ 169 tcg_debug_assert(kind == TCG_CALL_RET_NORMAL); 170 tcg_debug_assert(slot >= 0 && slot <= 1); 171 return TCG_REG_A0 + slot; 172} 173 174#define TCG_GUEST_BASE_REG TCG_REG_S1 175 176#define TCG_CT_CONST_S12 0x100 177#define TCG_CT_CONST_S32 0x200 178#define TCG_CT_CONST_U12 0x400 179#define TCG_CT_CONST_C12 0x800 180#define TCG_CT_CONST_WSZ 0x1000 181#define TCG_CT_CONST_VCMP 0x2000 182#define TCG_CT_CONST_VADD 0x4000 183 184#define ALL_GENERAL_REGS MAKE_64BIT_MASK(0, 32) 185#define ALL_VECTOR_REGS MAKE_64BIT_MASK(32, 32) 186 187static inline tcg_target_long sextreg(tcg_target_long val, int pos, int len) 188{ 189 return sextract64(val, pos, len); 190} 191 192/* test if a constant matches the constraint */ 193static bool tcg_target_const_match(int64_t val, int ct, 194 TCGType type, TCGCond cond, int vece) 195{ 196 if (ct & TCG_CT_CONST) { 197 return true; 198 } 199 if ((ct & TCG_CT_CONST_S12) && val == sextreg(val, 0, 12)) { 200 return true; 201 } 202 if ((ct & TCG_CT_CONST_S32) && val == (int32_t)val) { 203 return true; 204 } 205 if ((ct & TCG_CT_CONST_U12) && val >= 0 && val <= 0xfff) { 206 return true; 207 } 208 if ((ct & TCG_CT_CONST_C12) && ~val >= 0 && ~val <= 0xfff) { 209 return true; 210 } 211 if ((ct & TCG_CT_CONST_WSZ) && val == (type == TCG_TYPE_I32 ? 32 : 64)) { 212 return true; 213 } 214 if (ct & (TCG_CT_CONST_VCMP | TCG_CT_CONST_VADD)) { 215 int64_t vec_val = sextract64(val, 0, 8 << vece); 216 if (ct & TCG_CT_CONST_VCMP) { 217 switch (cond) { 218 case TCG_COND_EQ: 219 case TCG_COND_LE: 220 case TCG_COND_LT: 221 return -0x10 <= vec_val && vec_val <= 0x0f; 222 case TCG_COND_LEU: 223 case TCG_COND_LTU: 224 return 0x00 <= vec_val && vec_val <= 0x1f; 225 default: 226 return false; 227 } 228 } 229 if ((ct & TCG_CT_CONST_VADD) && -0x1f <= vec_val && vec_val <= 0x1f) { 230 return true; 231 } 232 } 233 return false; 234} 235 236/* 237 * Relocations 238 */ 239 240/* 241 * Relocation records defined in LoongArch ELF psABI v1.00 is way too 242 * complicated; a whopping stack machine is needed to stuff the fields, at 243 * the very least one SOP_PUSH and one SOP_POP (of the correct format) are 244 * needed. 245 * 246 * Hence, define our own simpler relocation types. Numbers are chosen as to 247 * not collide with potential future additions to the true ELF relocation 248 * type enum. 249 */ 250 251/* Field Sk16, shifted right by 2; suitable for conditional jumps */ 252#define R_LOONGARCH_BR_SK16 256 253/* Field Sd10k16, shifted right by 2; suitable for B and BL */ 254#define R_LOONGARCH_BR_SD10K16 257 255 256static bool reloc_br_sk16(tcg_insn_unit *src_rw, const tcg_insn_unit *target) 257{ 258 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw); 259 intptr_t offset = (intptr_t)target - (intptr_t)src_rx; 260 261 tcg_debug_assert((offset & 3) == 0); 262 offset >>= 2; 263 if (offset == sextreg(offset, 0, 16)) { 264 *src_rw = deposit64(*src_rw, 10, 16, offset); 265 return true; 266 } 267 268 return false; 269} 270 271static bool reloc_br_sd10k16(tcg_insn_unit *src_rw, 272 const tcg_insn_unit *target) 273{ 274 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw); 275 intptr_t offset = (intptr_t)target - (intptr_t)src_rx; 276 277 tcg_debug_assert((offset & 3) == 0); 278 offset >>= 2; 279 if (offset == sextreg(offset, 0, 26)) { 280 *src_rw = deposit64(*src_rw, 0, 10, offset >> 16); /* slot d10 */ 281 *src_rw = deposit64(*src_rw, 10, 16, offset); /* slot k16 */ 282 return true; 283 } 284 285 return false; 286} 287 288static bool patch_reloc(tcg_insn_unit *code_ptr, int type, 289 intptr_t value, intptr_t addend) 290{ 291 tcg_debug_assert(addend == 0); 292 switch (type) { 293 case R_LOONGARCH_BR_SK16: 294 return reloc_br_sk16(code_ptr, (tcg_insn_unit *)value); 295 case R_LOONGARCH_BR_SD10K16: 296 return reloc_br_sd10k16(code_ptr, (tcg_insn_unit *)value); 297 default: 298 g_assert_not_reached(); 299 } 300} 301 302#include "tcg-insn-defs.c.inc" 303 304/* 305 * TCG intrinsics 306 */ 307 308static void tcg_out_mb(TCGContext *s, TCGArg a0) 309{ 310 /* Baseline LoongArch only has the full barrier, unfortunately. */ 311 tcg_out_opc_dbar(s, 0); 312} 313 314static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg) 315{ 316 if (ret == arg) { 317 return true; 318 } 319 switch (type) { 320 case TCG_TYPE_I32: 321 case TCG_TYPE_I64: 322 if (ret < TCG_REG_V0) { 323 if (arg < TCG_REG_V0) { 324 /* 325 * Conventional register-register move used in LoongArch is 326 * `or dst, src, zero`. 327 */ 328 tcg_out_opc_or(s, ret, arg, TCG_REG_ZERO); 329 } else { 330 tcg_out_opc_movfr2gr_d(s, ret, arg); 331 } 332 } else { 333 if (arg < TCG_REG_V0) { 334 tcg_out_opc_movgr2fr_d(s, ret, arg); 335 } else { 336 tcg_out_opc_fmov_d(s, ret, arg); 337 } 338 } 339 break; 340 case TCG_TYPE_V64: 341 case TCG_TYPE_V128: 342 tcg_out_opc_vori_b(s, ret, arg, 0); 343 break; 344 case TCG_TYPE_V256: 345 tcg_out_opc_xvori_b(s, ret, arg, 0); 346 break; 347 default: 348 g_assert_not_reached(); 349 } 350 return true; 351} 352 353/* Loads a 32-bit immediate into rd, sign-extended. */ 354static void tcg_out_movi_i32(TCGContext *s, TCGReg rd, int32_t val) 355{ 356 tcg_target_long lo = sextreg(val, 0, 12); 357 tcg_target_long hi12 = sextreg(val, 12, 20); 358 359 /* Single-instruction cases. */ 360 if (hi12 == 0) { 361 /* val fits in uimm12: ori rd, zero, val */ 362 tcg_out_opc_ori(s, rd, TCG_REG_ZERO, val); 363 return; 364 } 365 if (hi12 == sextreg(lo, 12, 20)) { 366 /* val fits in simm12: addi.w rd, zero, val */ 367 tcg_out_opc_addi_w(s, rd, TCG_REG_ZERO, val); 368 return; 369 } 370 371 /* High bits must be set; load with lu12i.w + optional ori. */ 372 tcg_out_opc_lu12i_w(s, rd, hi12); 373 if (lo != 0) { 374 tcg_out_opc_ori(s, rd, rd, lo & 0xfff); 375 } 376} 377 378static void tcg_out_movi(TCGContext *s, TCGType type, TCGReg rd, 379 tcg_target_long val) 380{ 381 /* 382 * LoongArch conventionally loads 64-bit immediates in at most 4 steps, 383 * with dedicated instructions for filling the respective bitfields 384 * below: 385 * 386 * 6 5 4 3 387 * 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 388 * +-----------------------+---------------------------------------+... 389 * | hi52 | hi32 | 390 * +-----------------------+---------------------------------------+... 391 * 3 2 1 392 * 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 393 * ...+-------------------------------------+-------------------------+ 394 * | hi12 | lo | 395 * ...+-------------------------------------+-------------------------+ 396 * 397 * Check if val belong to one of the several fast cases, before falling 398 * back to the slow path. 399 */ 400 401 intptr_t src_rx, pc_offset; 402 tcg_target_long hi12, hi32, hi52; 403 404 /* Value fits in signed i32. */ 405 if (type == TCG_TYPE_I32 || val == (int32_t)val) { 406 tcg_out_movi_i32(s, rd, val); 407 return; 408 } 409 410 /* PC-relative cases. */ 411 src_rx = (intptr_t)tcg_splitwx_to_rx(s->code_ptr); 412 if ((val & 3) == 0) { 413 pc_offset = val - src_rx; 414 if (pc_offset == sextreg(pc_offset, 0, 22)) { 415 /* Single pcaddu2i. */ 416 tcg_out_opc_pcaddu2i(s, rd, pc_offset >> 2); 417 return; 418 } 419 } 420 421 pc_offset = (val >> 12) - (src_rx >> 12); 422 if (pc_offset == sextreg(pc_offset, 0, 20)) { 423 /* Load with pcalau12i + ori. */ 424 tcg_target_long val_lo = val & 0xfff; 425 tcg_out_opc_pcalau12i(s, rd, pc_offset); 426 if (val_lo != 0) { 427 tcg_out_opc_ori(s, rd, rd, val_lo); 428 } 429 return; 430 } 431 432 hi12 = sextreg(val, 12, 20); 433 hi32 = sextreg(val, 32, 20); 434 hi52 = sextreg(val, 52, 12); 435 436 /* Single cu52i.d case. */ 437 if ((hi52 != 0) && (ctz64(val) >= 52)) { 438 tcg_out_opc_cu52i_d(s, rd, TCG_REG_ZERO, hi52); 439 return; 440 } 441 442 /* Slow path. Initialize the low 32 bits, then concat high bits. */ 443 tcg_out_movi_i32(s, rd, val); 444 445 /* Load hi32 and hi52 explicitly when they are unexpected values. */ 446 if (hi32 != sextreg(hi12, 20, 20)) { 447 tcg_out_opc_cu32i_d(s, rd, hi32); 448 } 449 450 if (hi52 != sextreg(hi32, 20, 12)) { 451 tcg_out_opc_cu52i_d(s, rd, rd, hi52); 452 } 453} 454 455static void tcg_out_addi(TCGContext *s, TCGType type, TCGReg rd, 456 TCGReg rs, tcg_target_long imm) 457{ 458 tcg_target_long lo12 = sextreg(imm, 0, 12); 459 tcg_target_long hi16 = sextreg(imm - lo12, 16, 16); 460 461 /* 462 * Note that there's a hole in between hi16 and lo12: 463 * 464 * 3 2 1 0 465 * 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 466 * ...+-------------------------------+-------+-----------------------+ 467 * | hi16 | | lo12 | 468 * ...+-------------------------------+-------+-----------------------+ 469 * 470 * For bits within that hole, it's more efficient to use LU12I and ADD. 471 */ 472 if (imm == (hi16 << 16) + lo12) { 473 if (hi16) { 474 tcg_out_opc_addu16i_d(s, rd, rs, hi16); 475 rs = rd; 476 } 477 if (type == TCG_TYPE_I32) { 478 tcg_out_opc_addi_w(s, rd, rs, lo12); 479 } else if (lo12) { 480 tcg_out_opc_addi_d(s, rd, rs, lo12); 481 } else { 482 tcg_out_mov(s, type, rd, rs); 483 } 484 } else { 485 tcg_out_movi(s, type, TCG_REG_TMP0, imm); 486 if (type == TCG_TYPE_I32) { 487 tcg_out_opc_add_w(s, rd, rs, TCG_REG_TMP0); 488 } else { 489 tcg_out_opc_add_d(s, rd, rs, TCG_REG_TMP0); 490 } 491 } 492} 493 494static bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2) 495{ 496 return false; 497} 498 499static void tcg_out_addi_ptr(TCGContext *s, TCGReg rd, TCGReg rs, 500 tcg_target_long imm) 501{ 502 /* This function is only used for passing structs by reference. */ 503 g_assert_not_reached(); 504} 505 506static void tcg_out_ext8u(TCGContext *s, TCGReg ret, TCGReg arg) 507{ 508 tcg_out_opc_andi(s, ret, arg, 0xff); 509} 510 511static void tcg_out_ext16u(TCGContext *s, TCGReg ret, TCGReg arg) 512{ 513 tcg_out_opc_bstrpick_w(s, ret, arg, 0, 15); 514} 515 516static void tcg_out_ext32u(TCGContext *s, TCGReg ret, TCGReg arg) 517{ 518 tcg_out_opc_bstrpick_d(s, ret, arg, 0, 31); 519} 520 521static void tcg_out_ext8s(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg) 522{ 523 tcg_out_opc_sext_b(s, ret, arg); 524} 525 526static void tcg_out_ext16s(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg) 527{ 528 tcg_out_opc_sext_h(s, ret, arg); 529} 530 531static void tcg_out_ext32s(TCGContext *s, TCGReg ret, TCGReg arg) 532{ 533 tcg_out_opc_addi_w(s, ret, arg, 0); 534} 535 536static void tcg_out_exts_i32_i64(TCGContext *s, TCGReg ret, TCGReg arg) 537{ 538 if (ret != arg) { 539 tcg_out_ext32s(s, ret, arg); 540 } 541} 542 543static void tcg_out_extu_i32_i64(TCGContext *s, TCGReg ret, TCGReg arg) 544{ 545 tcg_out_ext32u(s, ret, arg); 546} 547 548static void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg ret, TCGReg arg) 549{ 550 tcg_out_ext32s(s, ret, arg); 551} 552 553static void tcg_out_clzctz(TCGContext *s, LoongArchInsn opc, 554 TCGReg a0, TCGReg a1, TCGReg a2, 555 bool c2, bool is_32bit) 556{ 557 if (c2) { 558 /* 559 * Fast path: semantics already satisfied due to constraint and 560 * insn behavior, single instruction is enough. 561 */ 562 tcg_debug_assert(a2 == (is_32bit ? 32 : 64)); 563 /* all clz/ctz insns belong to DJ-format */ 564 tcg_out32(s, encode_dj_insn(opc, a0, a1)); 565 return; 566 } 567 568 tcg_out32(s, encode_dj_insn(opc, TCG_REG_TMP0, a1)); 569 /* a0 = a1 ? REG_TMP0 : a2 */ 570 tcg_out_opc_maskeqz(s, TCG_REG_TMP0, TCG_REG_TMP0, a1); 571 tcg_out_opc_masknez(s, a0, a2, a1); 572 tcg_out_opc_or(s, a0, TCG_REG_TMP0, a0); 573} 574 575#define SETCOND_INV TCG_TARGET_NB_REGS 576#define SETCOND_NEZ (SETCOND_INV << 1) 577#define SETCOND_FLAGS (SETCOND_INV | SETCOND_NEZ) 578 579static int tcg_out_setcond_int(TCGContext *s, TCGCond cond, TCGReg ret, 580 TCGReg arg1, tcg_target_long arg2, bool c2) 581{ 582 int flags = 0; 583 584 switch (cond) { 585 case TCG_COND_EQ: /* -> NE */ 586 case TCG_COND_GE: /* -> LT */ 587 case TCG_COND_GEU: /* -> LTU */ 588 case TCG_COND_GT: /* -> LE */ 589 case TCG_COND_GTU: /* -> LEU */ 590 cond = tcg_invert_cond(cond); 591 flags ^= SETCOND_INV; 592 break; 593 default: 594 break; 595 } 596 597 switch (cond) { 598 case TCG_COND_LE: 599 case TCG_COND_LEU: 600 /* 601 * If we have a constant input, the most efficient way to implement 602 * LE is by adding 1 and using LT. Watch out for wrap around for LEU. 603 * We don't need to care for this for LE because the constant input 604 * is still constrained to int32_t, and INT32_MAX+1 is representable 605 * in the 64-bit temporary register. 606 */ 607 if (c2) { 608 if (cond == TCG_COND_LEU) { 609 /* unsigned <= -1 is true */ 610 if (arg2 == -1) { 611 tcg_out_movi(s, TCG_TYPE_REG, ret, !(flags & SETCOND_INV)); 612 return ret; 613 } 614 cond = TCG_COND_LTU; 615 } else { 616 cond = TCG_COND_LT; 617 } 618 arg2 += 1; 619 } else { 620 TCGReg tmp = arg2; 621 arg2 = arg1; 622 arg1 = tmp; 623 cond = tcg_swap_cond(cond); /* LE -> GE */ 624 cond = tcg_invert_cond(cond); /* GE -> LT */ 625 flags ^= SETCOND_INV; 626 } 627 break; 628 default: 629 break; 630 } 631 632 switch (cond) { 633 case TCG_COND_NE: 634 flags |= SETCOND_NEZ; 635 if (!c2) { 636 tcg_out_opc_xor(s, ret, arg1, arg2); 637 } else if (arg2 == 0) { 638 ret = arg1; 639 } else if (arg2 >= 0 && arg2 <= 0xfff) { 640 tcg_out_opc_xori(s, ret, arg1, arg2); 641 } else { 642 tcg_out_addi(s, TCG_TYPE_REG, ret, arg1, -arg2); 643 } 644 break; 645 646 case TCG_COND_LT: 647 case TCG_COND_LTU: 648 if (c2) { 649 if (arg2 >= -0x800 && arg2 <= 0x7ff) { 650 if (cond == TCG_COND_LT) { 651 tcg_out_opc_slti(s, ret, arg1, arg2); 652 } else { 653 tcg_out_opc_sltui(s, ret, arg1, arg2); 654 } 655 break; 656 } 657 tcg_out_movi(s, TCG_TYPE_REG, TCG_REG_TMP0, arg2); 658 arg2 = TCG_REG_TMP0; 659 } 660 if (cond == TCG_COND_LT) { 661 tcg_out_opc_slt(s, ret, arg1, arg2); 662 } else { 663 tcg_out_opc_sltu(s, ret, arg1, arg2); 664 } 665 break; 666 667 default: 668 g_assert_not_reached(); 669 } 670 671 return ret | flags; 672} 673 674static void tcg_out_setcond(TCGContext *s, TCGCond cond, TCGReg ret, 675 TCGReg arg1, tcg_target_long arg2, bool c2) 676{ 677 int tmpflags = tcg_out_setcond_int(s, cond, ret, arg1, arg2, c2); 678 679 if (tmpflags != ret) { 680 TCGReg tmp = tmpflags & ~SETCOND_FLAGS; 681 682 switch (tmpflags & SETCOND_FLAGS) { 683 case SETCOND_INV: 684 /* Intermediate result is boolean: simply invert. */ 685 tcg_out_opc_xori(s, ret, tmp, 1); 686 break; 687 case SETCOND_NEZ: 688 /* Intermediate result is zero/non-zero: test != 0. */ 689 tcg_out_opc_sltu(s, ret, TCG_REG_ZERO, tmp); 690 break; 691 case SETCOND_NEZ | SETCOND_INV: 692 /* Intermediate result is zero/non-zero: test == 0. */ 693 tcg_out_opc_sltui(s, ret, tmp, 1); 694 break; 695 default: 696 g_assert_not_reached(); 697 } 698 } 699} 700 701static void tcg_out_movcond(TCGContext *s, TCGCond cond, TCGReg ret, 702 TCGReg c1, tcg_target_long c2, bool const2, 703 TCGReg v1, TCGReg v2) 704{ 705 int tmpflags = tcg_out_setcond_int(s, cond, TCG_REG_TMP0, c1, c2, const2); 706 TCGReg t; 707 708 /* Standardize the test below to t != 0. */ 709 if (tmpflags & SETCOND_INV) { 710 t = v1, v1 = v2, v2 = t; 711 } 712 713 t = tmpflags & ~SETCOND_FLAGS; 714 if (v1 == TCG_REG_ZERO) { 715 tcg_out_opc_masknez(s, ret, v2, t); 716 } else if (v2 == TCG_REG_ZERO) { 717 tcg_out_opc_maskeqz(s, ret, v1, t); 718 } else { 719 tcg_out_opc_masknez(s, TCG_REG_TMP2, v2, t); /* t ? 0 : v2 */ 720 tcg_out_opc_maskeqz(s, TCG_REG_TMP1, v1, t); /* t ? v1 : 0 */ 721 tcg_out_opc_or(s, ret, TCG_REG_TMP1, TCG_REG_TMP2); 722 } 723} 724 725/* 726 * Branch helpers 727 */ 728 729static const struct { 730 LoongArchInsn op; 731 bool swap; 732} tcg_brcond_to_loongarch[] = { 733 [TCG_COND_EQ] = { OPC_BEQ, false }, 734 [TCG_COND_NE] = { OPC_BNE, false }, 735 [TCG_COND_LT] = { OPC_BGT, true }, 736 [TCG_COND_GE] = { OPC_BLE, true }, 737 [TCG_COND_LE] = { OPC_BLE, false }, 738 [TCG_COND_GT] = { OPC_BGT, false }, 739 [TCG_COND_LTU] = { OPC_BGTU, true }, 740 [TCG_COND_GEU] = { OPC_BLEU, true }, 741 [TCG_COND_LEU] = { OPC_BLEU, false }, 742 [TCG_COND_GTU] = { OPC_BGTU, false } 743}; 744 745static void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1, 746 TCGReg arg2, TCGLabel *l) 747{ 748 LoongArchInsn op = tcg_brcond_to_loongarch[cond].op; 749 750 tcg_debug_assert(op != 0); 751 752 if (tcg_brcond_to_loongarch[cond].swap) { 753 TCGReg t = arg1; 754 arg1 = arg2; 755 arg2 = t; 756 } 757 758 /* all conditional branch insns belong to DJSk16-format */ 759 tcg_out_reloc(s, s->code_ptr, R_LOONGARCH_BR_SK16, l, 0); 760 tcg_out32(s, encode_djsk16_insn(op, arg1, arg2, 0)); 761} 762 763static void tcg_out_call_int(TCGContext *s, const tcg_insn_unit *arg, bool tail) 764{ 765 TCGReg link = tail ? TCG_REG_ZERO : TCG_REG_RA; 766 ptrdiff_t offset = tcg_pcrel_diff(s, arg); 767 768 tcg_debug_assert((offset & 3) == 0); 769 if (offset == sextreg(offset, 0, 28)) { 770 /* short jump: +/- 256MiB */ 771 if (tail) { 772 tcg_out_opc_b(s, offset >> 2); 773 } else { 774 tcg_out_opc_bl(s, offset >> 2); 775 } 776 } else if (offset == sextreg(offset, 0, 38)) { 777 /* long jump: +/- 256GiB */ 778 tcg_target_long lo = sextreg(offset, 0, 18); 779 tcg_target_long hi = offset - lo; 780 tcg_out_opc_pcaddu18i(s, TCG_REG_TMP0, hi >> 18); 781 tcg_out_opc_jirl(s, link, TCG_REG_TMP0, lo >> 2); 782 } else { 783 /* far jump: 64-bit */ 784 tcg_target_long lo = sextreg((tcg_target_long)arg, 0, 18); 785 tcg_target_long hi = (tcg_target_long)arg - lo; 786 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP0, hi); 787 tcg_out_opc_jirl(s, link, TCG_REG_TMP0, lo >> 2); 788 } 789} 790 791static void tcg_out_call(TCGContext *s, const tcg_insn_unit *arg, 792 const TCGHelperInfo *info) 793{ 794 tcg_out_call_int(s, arg, false); 795} 796 797/* 798 * Load/store helpers 799 */ 800 801static void tcg_out_ldst(TCGContext *s, LoongArchInsn opc, TCGReg data, 802 TCGReg addr, intptr_t offset) 803{ 804 intptr_t imm12 = sextreg(offset, 0, 12); 805 806 if (offset != imm12) { 807 intptr_t diff = tcg_pcrel_diff(s, (void *)offset); 808 809 if (addr == TCG_REG_ZERO && diff == (int32_t)diff) { 810 imm12 = sextreg(diff, 0, 12); 811 tcg_out_opc_pcaddu12i(s, TCG_REG_TMP2, (diff - imm12) >> 12); 812 } else { 813 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP2, offset - imm12); 814 if (addr != TCG_REG_ZERO) { 815 tcg_out_opc_add_d(s, TCG_REG_TMP2, TCG_REG_TMP2, addr); 816 } 817 } 818 addr = TCG_REG_TMP2; 819 } 820 821 switch (opc) { 822 case OPC_LD_B: 823 case OPC_LD_BU: 824 case OPC_LD_H: 825 case OPC_LD_HU: 826 case OPC_LD_W: 827 case OPC_LD_WU: 828 case OPC_LD_D: 829 case OPC_ST_B: 830 case OPC_ST_H: 831 case OPC_ST_W: 832 case OPC_ST_D: 833 tcg_out32(s, encode_djsk12_insn(opc, data, addr, imm12)); 834 break; 835 case OPC_FLD_S: 836 case OPC_FLD_D: 837 case OPC_FST_S: 838 case OPC_FST_D: 839 tcg_out32(s, encode_fdjsk12_insn(opc, data, addr, imm12)); 840 break; 841 default: 842 g_assert_not_reached(); 843 } 844} 845 846static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg dest, 847 TCGReg base, intptr_t offset) 848{ 849 switch (type) { 850 case TCG_TYPE_I32: 851 if (dest < TCG_REG_V0) { 852 tcg_out_ldst(s, OPC_LD_W, dest, base, offset); 853 } else { 854 tcg_out_ldst(s, OPC_FLD_S, dest, base, offset); 855 } 856 break; 857 case TCG_TYPE_I64: 858 case TCG_TYPE_V64: 859 if (dest < TCG_REG_V0) { 860 tcg_out_ldst(s, OPC_LD_D, dest, base, offset); 861 } else { 862 tcg_out_ldst(s, OPC_FLD_D, dest, base, offset); 863 } 864 break; 865 case TCG_TYPE_V128: 866 if (-0x800 <= offset && offset <= 0x7ff) { 867 tcg_out_opc_vld(s, dest, base, offset); 868 } else { 869 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP0, offset); 870 tcg_out_opc_vldx(s, dest, base, TCG_REG_TMP0); 871 } 872 break; 873 case TCG_TYPE_V256: 874 if (-0x800 <= offset && offset <= 0x7ff) { 875 tcg_out_opc_xvld(s, dest, base, offset); 876 } else { 877 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP0, offset); 878 tcg_out_opc_xvldx(s, dest, base, TCG_REG_TMP0); 879 } 880 break; 881 default: 882 g_assert_not_reached(); 883 } 884} 885 886static void tcg_out_st(TCGContext *s, TCGType type, TCGReg src, 887 TCGReg base, intptr_t offset) 888{ 889 switch (type) { 890 case TCG_TYPE_I32: 891 if (src < TCG_REG_V0) { 892 tcg_out_ldst(s, OPC_ST_W, src, base, offset); 893 } else { 894 tcg_out_ldst(s, OPC_FST_S, src, base, offset); 895 } 896 break; 897 case TCG_TYPE_I64: 898 case TCG_TYPE_V64: 899 if (src < TCG_REG_V0) { 900 tcg_out_ldst(s, OPC_ST_D, src, base, offset); 901 } else { 902 tcg_out_ldst(s, OPC_FST_D, src, base, offset); 903 } 904 break; 905 case TCG_TYPE_V128: 906 if (-0x800 <= offset && offset <= 0x7ff) { 907 tcg_out_opc_vst(s, src, base, offset); 908 } else { 909 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP0, offset); 910 tcg_out_opc_vstx(s, src, base, TCG_REG_TMP0); 911 } 912 break; 913 case TCG_TYPE_V256: 914 if (-0x800 <= offset && offset <= 0x7ff) { 915 tcg_out_opc_xvst(s, src, base, offset); 916 } else { 917 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP0, offset); 918 tcg_out_opc_xvstx(s, src, base, TCG_REG_TMP0); 919 } 920 break; 921 default: 922 g_assert_not_reached(); 923 } 924} 925 926static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val, 927 TCGReg base, intptr_t ofs) 928{ 929 if (val == 0) { 930 tcg_out_st(s, type, TCG_REG_ZERO, base, ofs); 931 return true; 932 } 933 return false; 934} 935 936/* 937 * Load/store helpers for SoftMMU, and qemu_ld/st implementations 938 */ 939 940static bool tcg_out_goto(TCGContext *s, const tcg_insn_unit *target) 941{ 942 tcg_out_opc_b(s, 0); 943 return reloc_br_sd10k16(s->code_ptr - 1, target); 944} 945 946static const TCGLdstHelperParam ldst_helper_param = { 947 .ntmp = 1, .tmp = { TCG_REG_TMP0 } 948}; 949 950static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l) 951{ 952 MemOp opc = get_memop(l->oi); 953 954 /* resolve label address */ 955 if (!reloc_br_sk16(l->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) { 956 return false; 957 } 958 959 tcg_out_ld_helper_args(s, l, &ldst_helper_param); 960 tcg_out_call_int(s, qemu_ld_helpers[opc & MO_SIZE], false); 961 tcg_out_ld_helper_ret(s, l, false, &ldst_helper_param); 962 return tcg_out_goto(s, l->raddr); 963} 964 965static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l) 966{ 967 MemOp opc = get_memop(l->oi); 968 969 /* resolve label address */ 970 if (!reloc_br_sk16(l->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) { 971 return false; 972 } 973 974 tcg_out_st_helper_args(s, l, &ldst_helper_param); 975 tcg_out_call_int(s, qemu_st_helpers[opc & MO_SIZE], false); 976 return tcg_out_goto(s, l->raddr); 977} 978 979typedef struct { 980 TCGReg base; 981 TCGReg index; 982 TCGAtomAlign aa; 983} HostAddress; 984 985bool tcg_target_has_memory_bswap(MemOp memop) 986{ 987 return false; 988} 989 990/* We expect to use a 12-bit negative offset from ENV. */ 991#define MIN_TLB_MASK_TABLE_OFS -(1 << 11) 992 993/* 994 * For system-mode, perform the TLB load and compare. 995 * For user-mode, perform any required alignment tests. 996 * In both cases, return a TCGLabelQemuLdst structure if the slow path 997 * is required and fill in @h with the host address for the fast path. 998 */ 999static TCGLabelQemuLdst *prepare_host_addr(TCGContext *s, HostAddress *h, 1000 TCGReg addr_reg, MemOpIdx oi, 1001 bool is_ld) 1002{ 1003 TCGType addr_type = s->addr_type; 1004 TCGLabelQemuLdst *ldst = NULL; 1005 MemOp opc = get_memop(oi); 1006 MemOp a_bits; 1007 1008 h->aa = atom_and_align_for_opc(s, opc, MO_ATOM_IFALIGN, false); 1009 a_bits = h->aa.align; 1010 1011 if (tcg_use_softmmu) { 1012 unsigned s_bits = opc & MO_SIZE; 1013 int mem_index = get_mmuidx(oi); 1014 int fast_ofs = tlb_mask_table_ofs(s, mem_index); 1015 int mask_ofs = fast_ofs + offsetof(CPUTLBDescFast, mask); 1016 int table_ofs = fast_ofs + offsetof(CPUTLBDescFast, table); 1017 1018 ldst = new_ldst_label(s); 1019 ldst->is_ld = is_ld; 1020 ldst->oi = oi; 1021 ldst->addr_reg = addr_reg; 1022 1023 tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP0, TCG_AREG0, mask_ofs); 1024 tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP1, TCG_AREG0, table_ofs); 1025 1026 tcg_out_opc_srli_d(s, TCG_REG_TMP2, addr_reg, 1027 s->page_bits - CPU_TLB_ENTRY_BITS); 1028 tcg_out_opc_and(s, TCG_REG_TMP2, TCG_REG_TMP2, TCG_REG_TMP0); 1029 tcg_out_opc_add_d(s, TCG_REG_TMP2, TCG_REG_TMP2, TCG_REG_TMP1); 1030 1031 /* Load the tlb comparator and the addend. */ 1032 QEMU_BUILD_BUG_ON(HOST_BIG_ENDIAN); 1033 tcg_out_ld(s, addr_type, TCG_REG_TMP0, TCG_REG_TMP2, 1034 is_ld ? offsetof(CPUTLBEntry, addr_read) 1035 : offsetof(CPUTLBEntry, addr_write)); 1036 tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP2, TCG_REG_TMP2, 1037 offsetof(CPUTLBEntry, addend)); 1038 1039 /* 1040 * For aligned accesses, we check the first byte and include the 1041 * alignment bits within the address. For unaligned access, we 1042 * check that we don't cross pages using the address of the last 1043 * byte of the access. 1044 */ 1045 if (a_bits < s_bits) { 1046 unsigned a_mask = (1u << a_bits) - 1; 1047 unsigned s_mask = (1u << s_bits) - 1; 1048 tcg_out_addi(s, addr_type, TCG_REG_TMP1, addr_reg, s_mask - a_mask); 1049 } else { 1050 tcg_out_mov(s, addr_type, TCG_REG_TMP1, addr_reg); 1051 } 1052 tcg_out_opc_bstrins_d(s, TCG_REG_TMP1, TCG_REG_ZERO, 1053 a_bits, s->page_bits - 1); 1054 1055 /* Compare masked address with the TLB entry. */ 1056 ldst->label_ptr[0] = s->code_ptr; 1057 tcg_out_opc_bne(s, TCG_REG_TMP0, TCG_REG_TMP1, 0); 1058 1059 h->index = TCG_REG_TMP2; 1060 } else { 1061 if (a_bits) { 1062 ldst = new_ldst_label(s); 1063 1064 ldst->is_ld = is_ld; 1065 ldst->oi = oi; 1066 ldst->addr_reg = addr_reg; 1067 1068 /* 1069 * Without micro-architecture details, we don't know which of 1070 * bstrpick or andi is faster, so use bstrpick as it's not 1071 * constrained by imm field width. Not to say alignments >= 2^12 1072 * are going to happen any time soon. 1073 */ 1074 tcg_out_opc_bstrpick_d(s, TCG_REG_TMP1, addr_reg, 0, a_bits - 1); 1075 1076 ldst->label_ptr[0] = s->code_ptr; 1077 tcg_out_opc_bne(s, TCG_REG_TMP1, TCG_REG_ZERO, 0); 1078 } 1079 1080 h->index = guest_base ? TCG_GUEST_BASE_REG : TCG_REG_ZERO; 1081 } 1082 1083 if (addr_type == TCG_TYPE_I32) { 1084 h->base = TCG_REG_TMP0; 1085 tcg_out_ext32u(s, h->base, addr_reg); 1086 } else { 1087 h->base = addr_reg; 1088 } 1089 1090 return ldst; 1091} 1092 1093static void tcg_out_qemu_ld_indexed(TCGContext *s, MemOp opc, TCGType type, 1094 TCGReg rd, HostAddress h) 1095{ 1096 /* Byte swapping is left to middle-end expansion. */ 1097 tcg_debug_assert((opc & MO_BSWAP) == 0); 1098 1099 switch (opc & MO_SSIZE) { 1100 case MO_UB: 1101 tcg_out_opc_ldx_bu(s, rd, h.base, h.index); 1102 break; 1103 case MO_SB: 1104 tcg_out_opc_ldx_b(s, rd, h.base, h.index); 1105 break; 1106 case MO_UW: 1107 tcg_out_opc_ldx_hu(s, rd, h.base, h.index); 1108 break; 1109 case MO_SW: 1110 tcg_out_opc_ldx_h(s, rd, h.base, h.index); 1111 break; 1112 case MO_UL: 1113 if (type == TCG_TYPE_I64) { 1114 tcg_out_opc_ldx_wu(s, rd, h.base, h.index); 1115 break; 1116 } 1117 /* fallthrough */ 1118 case MO_SL: 1119 tcg_out_opc_ldx_w(s, rd, h.base, h.index); 1120 break; 1121 case MO_UQ: 1122 tcg_out_opc_ldx_d(s, rd, h.base, h.index); 1123 break; 1124 default: 1125 g_assert_not_reached(); 1126 } 1127} 1128 1129static void tcg_out_qemu_ld(TCGContext *s, TCGReg data_reg, TCGReg addr_reg, 1130 MemOpIdx oi, TCGType data_type) 1131{ 1132 TCGLabelQemuLdst *ldst; 1133 HostAddress h; 1134 1135 ldst = prepare_host_addr(s, &h, addr_reg, oi, true); 1136 tcg_out_qemu_ld_indexed(s, get_memop(oi), data_type, data_reg, h); 1137 1138 if (ldst) { 1139 ldst->type = data_type; 1140 ldst->datalo_reg = data_reg; 1141 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr); 1142 } 1143} 1144 1145static void tcg_out_qemu_st_indexed(TCGContext *s, MemOp opc, 1146 TCGReg rd, HostAddress h) 1147{ 1148 /* Byte swapping is left to middle-end expansion. */ 1149 tcg_debug_assert((opc & MO_BSWAP) == 0); 1150 1151 switch (opc & MO_SIZE) { 1152 case MO_8: 1153 tcg_out_opc_stx_b(s, rd, h.base, h.index); 1154 break; 1155 case MO_16: 1156 tcg_out_opc_stx_h(s, rd, h.base, h.index); 1157 break; 1158 case MO_32: 1159 tcg_out_opc_stx_w(s, rd, h.base, h.index); 1160 break; 1161 case MO_64: 1162 tcg_out_opc_stx_d(s, rd, h.base, h.index); 1163 break; 1164 default: 1165 g_assert_not_reached(); 1166 } 1167} 1168 1169static void tcg_out_qemu_st(TCGContext *s, TCGReg data_reg, TCGReg addr_reg, 1170 MemOpIdx oi, TCGType data_type) 1171{ 1172 TCGLabelQemuLdst *ldst; 1173 HostAddress h; 1174 1175 ldst = prepare_host_addr(s, &h, addr_reg, oi, false); 1176 tcg_out_qemu_st_indexed(s, get_memop(oi), data_reg, h); 1177 1178 if (ldst) { 1179 ldst->type = data_type; 1180 ldst->datalo_reg = data_reg; 1181 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr); 1182 } 1183} 1184 1185static void tcg_out_qemu_ldst_i128(TCGContext *s, TCGReg data_lo, TCGReg data_hi, 1186 TCGReg addr_reg, MemOpIdx oi, bool is_ld) 1187{ 1188 TCGLabelQemuLdst *ldst; 1189 HostAddress h; 1190 1191 ldst = prepare_host_addr(s, &h, addr_reg, oi, is_ld); 1192 1193 if (h.aa.atom == MO_128) { 1194 /* 1195 * Use VLDX/VSTX when 128-bit atomicity is required. 1196 * If address is aligned to 16-bytes, the 128-bit load/store is atomic. 1197 */ 1198 if (is_ld) { 1199 tcg_out_opc_vldx(s, TCG_VEC_TMP0, h.base, h.index); 1200 tcg_out_opc_vpickve2gr_d(s, data_lo, TCG_VEC_TMP0, 0); 1201 tcg_out_opc_vpickve2gr_d(s, data_hi, TCG_VEC_TMP0, 1); 1202 } else { 1203 tcg_out_opc_vinsgr2vr_d(s, TCG_VEC_TMP0, data_lo, 0); 1204 tcg_out_opc_vinsgr2vr_d(s, TCG_VEC_TMP0, data_hi, 1); 1205 tcg_out_opc_vstx(s, TCG_VEC_TMP0, h.base, h.index); 1206 } 1207 } else { 1208 /* Otherwise use a pair of LD/ST. */ 1209 TCGReg base = h.base; 1210 if (h.index != TCG_REG_ZERO) { 1211 base = TCG_REG_TMP0; 1212 tcg_out_opc_add_d(s, base, h.base, h.index); 1213 } 1214 if (is_ld) { 1215 tcg_debug_assert(base != data_lo); 1216 tcg_out_opc_ld_d(s, data_lo, base, 0); 1217 tcg_out_opc_ld_d(s, data_hi, base, 8); 1218 } else { 1219 tcg_out_opc_st_d(s, data_lo, base, 0); 1220 tcg_out_opc_st_d(s, data_hi, base, 8); 1221 } 1222 } 1223 1224 if (ldst) { 1225 ldst->type = TCG_TYPE_I128; 1226 ldst->datalo_reg = data_lo; 1227 ldst->datahi_reg = data_hi; 1228 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr); 1229 } 1230} 1231 1232/* 1233 * Entry-points 1234 */ 1235 1236static const tcg_insn_unit *tb_ret_addr; 1237 1238static void tcg_out_exit_tb(TCGContext *s, uintptr_t a0) 1239{ 1240 /* Reuse the zeroing that exists for goto_ptr. */ 1241 if (a0 == 0) { 1242 tcg_out_call_int(s, tcg_code_gen_epilogue, true); 1243 } else { 1244 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_A0, a0); 1245 tcg_out_call_int(s, tb_ret_addr, true); 1246 } 1247} 1248 1249static void tcg_out_goto_tb(TCGContext *s, int which) 1250{ 1251 /* 1252 * Direct branch, or load indirect address, to be patched 1253 * by tb_target_set_jmp_target. Check indirect load offset 1254 * in range early, regardless of direct branch distance, 1255 * via assert within tcg_out_opc_pcaddu2i. 1256 */ 1257 uintptr_t i_addr = get_jmp_target_addr(s, which); 1258 intptr_t i_disp = tcg_pcrel_diff(s, (void *)i_addr); 1259 1260 set_jmp_insn_offset(s, which); 1261 tcg_out_opc_pcaddu2i(s, TCG_REG_TMP0, i_disp >> 2); 1262 1263 /* Finish the load and indirect branch. */ 1264 tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP0, TCG_REG_TMP0, 0); 1265 tcg_out_opc_jirl(s, TCG_REG_ZERO, TCG_REG_TMP0, 0); 1266 set_jmp_reset_offset(s, which); 1267} 1268 1269void tb_target_set_jmp_target(const TranslationBlock *tb, int n, 1270 uintptr_t jmp_rx, uintptr_t jmp_rw) 1271{ 1272 uintptr_t d_addr = tb->jmp_target_addr[n]; 1273 ptrdiff_t d_disp = (ptrdiff_t)(d_addr - jmp_rx) >> 2; 1274 tcg_insn_unit insn; 1275 1276 /* Either directly branch, or load slot address for indirect branch. */ 1277 if (d_disp == sextreg(d_disp, 0, 26)) { 1278 insn = encode_sd10k16_insn(OPC_B, d_disp); 1279 } else { 1280 uintptr_t i_addr = (uintptr_t)&tb->jmp_target_addr[n]; 1281 intptr_t i_disp = i_addr - jmp_rx; 1282 insn = encode_dsj20_insn(OPC_PCADDU2I, TCG_REG_TMP0, i_disp >> 2); 1283 } 1284 1285 qatomic_set((tcg_insn_unit *)jmp_rw, insn); 1286 flush_idcache_range(jmp_rx, jmp_rw, 4); 1287} 1288 1289 1290static void tgen_add(TCGContext *s, TCGType type, 1291 TCGReg a0, TCGReg a1, TCGReg a2) 1292{ 1293 if (type == TCG_TYPE_I32) { 1294 tcg_out_opc_add_w(s, a0, a1, a2); 1295 } else { 1296 tcg_out_opc_add_d(s, a0, a1, a2); 1297 } 1298} 1299 1300static const TCGOutOpBinary outop_add = { 1301 .base.static_constraint = C_O1_I2(r, r, rJ), 1302 .out_rrr = tgen_add, 1303 .out_rri = tcg_out_addi, 1304}; 1305 1306static void tgen_and(TCGContext *s, TCGType type, 1307 TCGReg a0, TCGReg a1, TCGReg a2) 1308{ 1309 tcg_out_opc_and(s, a0, a1, a2); 1310} 1311 1312static void tgen_andi(TCGContext *s, TCGType type, 1313 TCGReg a0, TCGReg a1, tcg_target_long a2) 1314{ 1315 tcg_out_opc_andi(s, a0, a1, a2); 1316} 1317 1318static const TCGOutOpBinary outop_and = { 1319 .base.static_constraint = C_O1_I2(r, r, rU), 1320 .out_rrr = tgen_and, 1321 .out_rri = tgen_andi, 1322}; 1323 1324static void tgen_andc(TCGContext *s, TCGType type, 1325 TCGReg a0, TCGReg a1, TCGReg a2) 1326{ 1327 tcg_out_opc_andn(s, a0, a1, a2); 1328} 1329 1330static const TCGOutOpBinary outop_andc = { 1331 .base.static_constraint = C_O1_I2(r, r, r), 1332 .out_rrr = tgen_andc, 1333}; 1334 1335 1336static void tcg_out_op(TCGContext *s, TCGOpcode opc, TCGType type, 1337 const TCGArg args[TCG_MAX_OP_ARGS], 1338 const int const_args[TCG_MAX_OP_ARGS]) 1339{ 1340 TCGArg a0 = args[0]; 1341 TCGArg a1 = args[1]; 1342 TCGArg a2 = args[2]; 1343 TCGArg a3 = args[3]; 1344 int c2 = const_args[2]; 1345 1346 switch (opc) { 1347 case INDEX_op_mb: 1348 tcg_out_mb(s, a0); 1349 break; 1350 1351 case INDEX_op_goto_ptr: 1352 tcg_out_opc_jirl(s, TCG_REG_ZERO, a0, 0); 1353 break; 1354 1355 case INDEX_op_br: 1356 tcg_out_reloc(s, s->code_ptr, R_LOONGARCH_BR_SD10K16, arg_label(a0), 1357 0); 1358 tcg_out_opc_b(s, 0); 1359 break; 1360 1361 case INDEX_op_brcond_i32: 1362 case INDEX_op_brcond_i64: 1363 tcg_out_brcond(s, a2, a0, a1, arg_label(args[3])); 1364 break; 1365 1366 case INDEX_op_extrh_i64_i32: 1367 tcg_out_opc_srai_d(s, a0, a1, 32); 1368 break; 1369 1370 case INDEX_op_not_i32: 1371 case INDEX_op_not_i64: 1372 tcg_out_opc_nor(s, a0, a1, TCG_REG_ZERO); 1373 break; 1374 1375 case INDEX_op_nor_i32: 1376 case INDEX_op_nor_i64: 1377 if (c2) { 1378 tcg_out_opc_ori(s, a0, a1, a2); 1379 tcg_out_opc_nor(s, a0, a0, TCG_REG_ZERO); 1380 } else { 1381 tcg_out_opc_nor(s, a0, a1, a2); 1382 } 1383 break; 1384 1385 case INDEX_op_orc_i32: 1386 case INDEX_op_orc_i64: 1387 if (c2) { 1388 /* guaranteed to fit due to constraint */ 1389 tcg_out_opc_ori(s, a0, a1, ~a2); 1390 } else { 1391 tcg_out_opc_orn(s, a0, a1, a2); 1392 } 1393 break; 1394 1395 case INDEX_op_or_i32: 1396 case INDEX_op_or_i64: 1397 if (c2) { 1398 tcg_out_opc_ori(s, a0, a1, a2); 1399 } else { 1400 tcg_out_opc_or(s, a0, a1, a2); 1401 } 1402 break; 1403 1404 case INDEX_op_xor_i32: 1405 case INDEX_op_xor_i64: 1406 if (c2) { 1407 tcg_out_opc_xori(s, a0, a1, a2); 1408 } else { 1409 tcg_out_opc_xor(s, a0, a1, a2); 1410 } 1411 break; 1412 1413 case INDEX_op_extract_i32: 1414 if (a2 == 0 && args[3] <= 12) { 1415 tcg_out_opc_andi(s, a0, a1, (1 << args[3]) - 1); 1416 } else { 1417 tcg_out_opc_bstrpick_w(s, a0, a1, a2, a2 + args[3] - 1); 1418 } 1419 break; 1420 case INDEX_op_extract_i64: 1421 if (a2 == 0 && args[3] <= 12) { 1422 tcg_out_opc_andi(s, a0, a1, (1 << args[3]) - 1); 1423 } else { 1424 tcg_out_opc_bstrpick_d(s, a0, a1, a2, a2 + args[3] - 1); 1425 } 1426 break; 1427 1428 case INDEX_op_sextract_i64: 1429 if (a2 + args[3] == 32) { 1430 if (a2 == 0) { 1431 tcg_out_ext32s(s, a0, a1); 1432 } else { 1433 tcg_out_opc_srai_w(s, a0, a1, a2); 1434 } 1435 break; 1436 } 1437 /* FALLTHRU */ 1438 case INDEX_op_sextract_i32: 1439 if (a2 == 0 && args[3] == 8) { 1440 tcg_out_ext8s(s, TCG_TYPE_REG, a0, a1); 1441 } else if (a2 == 0 && args[3] == 16) { 1442 tcg_out_ext16s(s, TCG_TYPE_REG, a0, a1); 1443 } else { 1444 g_assert_not_reached(); 1445 } 1446 break; 1447 1448 case INDEX_op_deposit_i32: 1449 tcg_out_opc_bstrins_w(s, a0, a2, args[3], args[3] + args[4] - 1); 1450 break; 1451 case INDEX_op_deposit_i64: 1452 tcg_out_opc_bstrins_d(s, a0, a2, args[3], args[3] + args[4] - 1); 1453 break; 1454 1455 case INDEX_op_bswap16_i32: 1456 case INDEX_op_bswap16_i64: 1457 tcg_out_opc_revb_2h(s, a0, a1); 1458 if (a2 & TCG_BSWAP_OS) { 1459 tcg_out_ext16s(s, TCG_TYPE_REG, a0, a0); 1460 } else if ((a2 & (TCG_BSWAP_IZ | TCG_BSWAP_OZ)) == TCG_BSWAP_OZ) { 1461 tcg_out_ext16u(s, a0, a0); 1462 } 1463 break; 1464 1465 case INDEX_op_bswap32_i32: 1466 /* All 32-bit values are computed sign-extended in the register. */ 1467 a2 = TCG_BSWAP_OS; 1468 /* fallthrough */ 1469 case INDEX_op_bswap32_i64: 1470 tcg_out_opc_revb_2w(s, a0, a1); 1471 if (a2 & TCG_BSWAP_OS) { 1472 tcg_out_ext32s(s, a0, a0); 1473 } else if ((a2 & (TCG_BSWAP_IZ | TCG_BSWAP_OZ)) == TCG_BSWAP_OZ) { 1474 tcg_out_ext32u(s, a0, a0); 1475 } 1476 break; 1477 1478 case INDEX_op_bswap64_i64: 1479 tcg_out_opc_revb_d(s, a0, a1); 1480 break; 1481 1482 case INDEX_op_clz_i32: 1483 tcg_out_clzctz(s, OPC_CLZ_W, a0, a1, a2, c2, true); 1484 break; 1485 case INDEX_op_clz_i64: 1486 tcg_out_clzctz(s, OPC_CLZ_D, a0, a1, a2, c2, false); 1487 break; 1488 1489 case INDEX_op_ctz_i32: 1490 tcg_out_clzctz(s, OPC_CTZ_W, a0, a1, a2, c2, true); 1491 break; 1492 case INDEX_op_ctz_i64: 1493 tcg_out_clzctz(s, OPC_CTZ_D, a0, a1, a2, c2, false); 1494 break; 1495 1496 case INDEX_op_shl_i32: 1497 if (c2) { 1498 tcg_out_opc_slli_w(s, a0, a1, a2 & 0x1f); 1499 } else { 1500 tcg_out_opc_sll_w(s, a0, a1, a2); 1501 } 1502 break; 1503 case INDEX_op_shl_i64: 1504 if (c2) { 1505 tcg_out_opc_slli_d(s, a0, a1, a2 & 0x3f); 1506 } else { 1507 tcg_out_opc_sll_d(s, a0, a1, a2); 1508 } 1509 break; 1510 1511 case INDEX_op_shr_i32: 1512 if (c2) { 1513 tcg_out_opc_srli_w(s, a0, a1, a2 & 0x1f); 1514 } else { 1515 tcg_out_opc_srl_w(s, a0, a1, a2); 1516 } 1517 break; 1518 case INDEX_op_shr_i64: 1519 if (c2) { 1520 tcg_out_opc_srli_d(s, a0, a1, a2 & 0x3f); 1521 } else { 1522 tcg_out_opc_srl_d(s, a0, a1, a2); 1523 } 1524 break; 1525 1526 case INDEX_op_sar_i32: 1527 if (c2) { 1528 tcg_out_opc_srai_w(s, a0, a1, a2 & 0x1f); 1529 } else { 1530 tcg_out_opc_sra_w(s, a0, a1, a2); 1531 } 1532 break; 1533 case INDEX_op_sar_i64: 1534 if (c2) { 1535 tcg_out_opc_srai_d(s, a0, a1, a2 & 0x3f); 1536 } else { 1537 tcg_out_opc_sra_d(s, a0, a1, a2); 1538 } 1539 break; 1540 1541 case INDEX_op_rotl_i32: 1542 /* transform into equivalent rotr/rotri */ 1543 if (c2) { 1544 tcg_out_opc_rotri_w(s, a0, a1, (32 - a2) & 0x1f); 1545 } else { 1546 tcg_out_opc_sub_w(s, TCG_REG_TMP0, TCG_REG_ZERO, a2); 1547 tcg_out_opc_rotr_w(s, a0, a1, TCG_REG_TMP0); 1548 } 1549 break; 1550 case INDEX_op_rotl_i64: 1551 /* transform into equivalent rotr/rotri */ 1552 if (c2) { 1553 tcg_out_opc_rotri_d(s, a0, a1, (64 - a2) & 0x3f); 1554 } else { 1555 tcg_out_opc_sub_w(s, TCG_REG_TMP0, TCG_REG_ZERO, a2); 1556 tcg_out_opc_rotr_d(s, a0, a1, TCG_REG_TMP0); 1557 } 1558 break; 1559 1560 case INDEX_op_rotr_i32: 1561 if (c2) { 1562 tcg_out_opc_rotri_w(s, a0, a1, a2 & 0x1f); 1563 } else { 1564 tcg_out_opc_rotr_w(s, a0, a1, a2); 1565 } 1566 break; 1567 case INDEX_op_rotr_i64: 1568 if (c2) { 1569 tcg_out_opc_rotri_d(s, a0, a1, a2 & 0x3f); 1570 } else { 1571 tcg_out_opc_rotr_d(s, a0, a1, a2); 1572 } 1573 break; 1574 1575 case INDEX_op_sub_i32: 1576 if (c2) { 1577 tcg_out_addi(s, TCG_TYPE_I32, a0, a1, -a2); 1578 } else { 1579 tcg_out_opc_sub_w(s, a0, a1, a2); 1580 } 1581 break; 1582 case INDEX_op_sub_i64: 1583 if (c2) { 1584 tcg_out_addi(s, TCG_TYPE_I64, a0, a1, -a2); 1585 } else { 1586 tcg_out_opc_sub_d(s, a0, a1, a2); 1587 } 1588 break; 1589 1590 case INDEX_op_neg_i32: 1591 tcg_out_opc_sub_w(s, a0, TCG_REG_ZERO, a1); 1592 break; 1593 case INDEX_op_neg_i64: 1594 tcg_out_opc_sub_d(s, a0, TCG_REG_ZERO, a1); 1595 break; 1596 1597 case INDEX_op_mul_i32: 1598 tcg_out_opc_mul_w(s, a0, a1, a2); 1599 break; 1600 case INDEX_op_mul_i64: 1601 tcg_out_opc_mul_d(s, a0, a1, a2); 1602 break; 1603 1604 case INDEX_op_mulsh_i32: 1605 tcg_out_opc_mulh_w(s, a0, a1, a2); 1606 break; 1607 case INDEX_op_mulsh_i64: 1608 tcg_out_opc_mulh_d(s, a0, a1, a2); 1609 break; 1610 1611 case INDEX_op_muluh_i32: 1612 tcg_out_opc_mulh_wu(s, a0, a1, a2); 1613 break; 1614 case INDEX_op_muluh_i64: 1615 tcg_out_opc_mulh_du(s, a0, a1, a2); 1616 break; 1617 1618 case INDEX_op_div_i32: 1619 tcg_out_opc_div_w(s, a0, a1, a2); 1620 break; 1621 case INDEX_op_div_i64: 1622 tcg_out_opc_div_d(s, a0, a1, a2); 1623 break; 1624 1625 case INDEX_op_divu_i32: 1626 tcg_out_opc_div_wu(s, a0, a1, a2); 1627 break; 1628 case INDEX_op_divu_i64: 1629 tcg_out_opc_div_du(s, a0, a1, a2); 1630 break; 1631 1632 case INDEX_op_rem_i32: 1633 tcg_out_opc_mod_w(s, a0, a1, a2); 1634 break; 1635 case INDEX_op_rem_i64: 1636 tcg_out_opc_mod_d(s, a0, a1, a2); 1637 break; 1638 1639 case INDEX_op_remu_i32: 1640 tcg_out_opc_mod_wu(s, a0, a1, a2); 1641 break; 1642 case INDEX_op_remu_i64: 1643 tcg_out_opc_mod_du(s, a0, a1, a2); 1644 break; 1645 1646 case INDEX_op_setcond_i32: 1647 case INDEX_op_setcond_i64: 1648 tcg_out_setcond(s, args[3], a0, a1, a2, c2); 1649 break; 1650 1651 case INDEX_op_movcond_i32: 1652 case INDEX_op_movcond_i64: 1653 tcg_out_movcond(s, args[5], a0, a1, a2, c2, args[3], args[4]); 1654 break; 1655 1656 case INDEX_op_ld8s_i32: 1657 case INDEX_op_ld8s_i64: 1658 tcg_out_ldst(s, OPC_LD_B, a0, a1, a2); 1659 break; 1660 case INDEX_op_ld8u_i32: 1661 case INDEX_op_ld8u_i64: 1662 tcg_out_ldst(s, OPC_LD_BU, a0, a1, a2); 1663 break; 1664 case INDEX_op_ld16s_i32: 1665 case INDEX_op_ld16s_i64: 1666 tcg_out_ldst(s, OPC_LD_H, a0, a1, a2); 1667 break; 1668 case INDEX_op_ld16u_i32: 1669 case INDEX_op_ld16u_i64: 1670 tcg_out_ldst(s, OPC_LD_HU, a0, a1, a2); 1671 break; 1672 case INDEX_op_ld_i32: 1673 case INDEX_op_ld32s_i64: 1674 tcg_out_ldst(s, OPC_LD_W, a0, a1, a2); 1675 break; 1676 case INDEX_op_ld32u_i64: 1677 tcg_out_ldst(s, OPC_LD_WU, a0, a1, a2); 1678 break; 1679 case INDEX_op_ld_i64: 1680 tcg_out_ldst(s, OPC_LD_D, a0, a1, a2); 1681 break; 1682 1683 case INDEX_op_st8_i32: 1684 case INDEX_op_st8_i64: 1685 tcg_out_ldst(s, OPC_ST_B, a0, a1, a2); 1686 break; 1687 case INDEX_op_st16_i32: 1688 case INDEX_op_st16_i64: 1689 tcg_out_ldst(s, OPC_ST_H, a0, a1, a2); 1690 break; 1691 case INDEX_op_st_i32: 1692 case INDEX_op_st32_i64: 1693 tcg_out_ldst(s, OPC_ST_W, a0, a1, a2); 1694 break; 1695 case INDEX_op_st_i64: 1696 tcg_out_ldst(s, OPC_ST_D, a0, a1, a2); 1697 break; 1698 1699 case INDEX_op_qemu_ld_i32: 1700 tcg_out_qemu_ld(s, a0, a1, a2, TCG_TYPE_I32); 1701 break; 1702 case INDEX_op_qemu_ld_i64: 1703 tcg_out_qemu_ld(s, a0, a1, a2, TCG_TYPE_I64); 1704 break; 1705 case INDEX_op_qemu_ld_i128: 1706 tcg_out_qemu_ldst_i128(s, a0, a1, a2, a3, true); 1707 break; 1708 case INDEX_op_qemu_st_i32: 1709 tcg_out_qemu_st(s, a0, a1, a2, TCG_TYPE_I32); 1710 break; 1711 case INDEX_op_qemu_st_i64: 1712 tcg_out_qemu_st(s, a0, a1, a2, TCG_TYPE_I64); 1713 break; 1714 case INDEX_op_qemu_st_i128: 1715 tcg_out_qemu_ldst_i128(s, a0, a1, a2, a3, false); 1716 break; 1717 1718 case INDEX_op_call: /* Always emitted via tcg_out_call. */ 1719 case INDEX_op_exit_tb: /* Always emitted via tcg_out_exit_tb. */ 1720 case INDEX_op_goto_tb: /* Always emitted via tcg_out_goto_tb. */ 1721 case INDEX_op_ext_i32_i64: /* Always emitted via tcg_reg_alloc_op. */ 1722 case INDEX_op_extu_i32_i64: 1723 case INDEX_op_extrl_i64_i32: 1724 default: 1725 g_assert_not_reached(); 1726 } 1727} 1728 1729static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece, 1730 TCGReg rd, TCGReg rs) 1731{ 1732 static const LoongArchInsn repl_insn[2][4] = { 1733 { OPC_VREPLGR2VR_B, OPC_VREPLGR2VR_H, 1734 OPC_VREPLGR2VR_W, OPC_VREPLGR2VR_D }, 1735 { OPC_XVREPLGR2VR_B, OPC_XVREPLGR2VR_H, 1736 OPC_XVREPLGR2VR_W, OPC_XVREPLGR2VR_D }, 1737 }; 1738 bool lasx = type == TCG_TYPE_V256; 1739 1740 tcg_debug_assert(vece <= MO_64); 1741 tcg_out32(s, encode_vdj_insn(repl_insn[lasx][vece], rd, rs)); 1742 return true; 1743} 1744 1745static bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece, 1746 TCGReg r, TCGReg base, intptr_t offset) 1747{ 1748 bool lasx = type == TCG_TYPE_V256; 1749 1750 /* Handle imm overflow and division (vldrepl.d imm is divided by 8). */ 1751 if (offset < -0x800 || offset > 0x7ff || 1752 (offset & ((1 << vece) - 1)) != 0) { 1753 tcg_out_addi(s, TCG_TYPE_I64, TCG_REG_TMP0, base, offset); 1754 base = TCG_REG_TMP0; 1755 offset = 0; 1756 } 1757 offset >>= vece; 1758 1759 switch (vece) { 1760 case MO_8: 1761 if (lasx) { 1762 tcg_out_opc_xvldrepl_b(s, r, base, offset); 1763 } else { 1764 tcg_out_opc_vldrepl_b(s, r, base, offset); 1765 } 1766 break; 1767 case MO_16: 1768 if (lasx) { 1769 tcg_out_opc_xvldrepl_h(s, r, base, offset); 1770 } else { 1771 tcg_out_opc_vldrepl_h(s, r, base, offset); 1772 } 1773 break; 1774 case MO_32: 1775 if (lasx) { 1776 tcg_out_opc_xvldrepl_w(s, r, base, offset); 1777 } else { 1778 tcg_out_opc_vldrepl_w(s, r, base, offset); 1779 } 1780 break; 1781 case MO_64: 1782 if (lasx) { 1783 tcg_out_opc_xvldrepl_d(s, r, base, offset); 1784 } else { 1785 tcg_out_opc_vldrepl_d(s, r, base, offset); 1786 } 1787 break; 1788 default: 1789 g_assert_not_reached(); 1790 } 1791 return true; 1792} 1793 1794static void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece, 1795 TCGReg rd, int64_t v64) 1796{ 1797 /* Try vldi if imm can fit */ 1798 int64_t value = sextract64(v64, 0, 8 << vece); 1799 if (-0x200 <= value && value <= 0x1FF) { 1800 uint32_t imm = (vece << 10) | ((uint32_t)v64 & 0x3FF); 1801 1802 if (type == TCG_TYPE_V256) { 1803 tcg_out_opc_xvldi(s, rd, imm); 1804 } else { 1805 tcg_out_opc_vldi(s, rd, imm); 1806 } 1807 return; 1808 } 1809 1810 /* TODO: vldi patterns when imm 12 is set */ 1811 1812 tcg_out_movi(s, TCG_TYPE_I64, TCG_REG_TMP0, value); 1813 tcg_out_dup_vec(s, type, vece, rd, TCG_REG_TMP0); 1814} 1815 1816static void tcg_out_addsub_vec(TCGContext *s, bool lasx, unsigned vece, 1817 TCGArg a0, TCGArg a1, TCGArg a2, 1818 bool a2_is_const, bool is_add) 1819{ 1820 static const LoongArchInsn add_vec_insn[2][4] = { 1821 { OPC_VADD_B, OPC_VADD_H, OPC_VADD_W, OPC_VADD_D }, 1822 { OPC_XVADD_B, OPC_XVADD_H, OPC_XVADD_W, OPC_XVADD_D }, 1823 }; 1824 static const LoongArchInsn add_vec_imm_insn[2][4] = { 1825 { OPC_VADDI_BU, OPC_VADDI_HU, OPC_VADDI_WU, OPC_VADDI_DU }, 1826 { OPC_XVADDI_BU, OPC_XVADDI_HU, OPC_XVADDI_WU, OPC_XVADDI_DU }, 1827 }; 1828 static const LoongArchInsn sub_vec_insn[2][4] = { 1829 { OPC_VSUB_B, OPC_VSUB_H, OPC_VSUB_W, OPC_VSUB_D }, 1830 { OPC_XVSUB_B, OPC_XVSUB_H, OPC_XVSUB_W, OPC_XVSUB_D }, 1831 }; 1832 static const LoongArchInsn sub_vec_imm_insn[2][4] = { 1833 { OPC_VSUBI_BU, OPC_VSUBI_HU, OPC_VSUBI_WU, OPC_VSUBI_DU }, 1834 { OPC_XVSUBI_BU, OPC_XVSUBI_HU, OPC_XVSUBI_WU, OPC_XVSUBI_DU }, 1835 }; 1836 LoongArchInsn insn; 1837 1838 if (a2_is_const) { 1839 int64_t value = sextract64(a2, 0, 8 << vece); 1840 1841 if (!is_add) { 1842 value = -value; 1843 } 1844 if (value < 0) { 1845 insn = sub_vec_imm_insn[lasx][vece]; 1846 value = -value; 1847 } else { 1848 insn = add_vec_imm_insn[lasx][vece]; 1849 } 1850 1851 /* Constraint TCG_CT_CONST_VADD ensures validity. */ 1852 tcg_debug_assert(0 <= value && value <= 0x1f); 1853 1854 tcg_out32(s, encode_vdvjuk5_insn(insn, a0, a1, value)); 1855 return; 1856 } 1857 1858 if (is_add) { 1859 insn = add_vec_insn[lasx][vece]; 1860 } else { 1861 insn = sub_vec_insn[lasx][vece]; 1862 } 1863 tcg_out32(s, encode_vdvjvk_insn(insn, a0, a1, a2)); 1864} 1865 1866static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, 1867 unsigned vecl, unsigned vece, 1868 const TCGArg args[TCG_MAX_OP_ARGS], 1869 const int const_args[TCG_MAX_OP_ARGS]) 1870{ 1871 TCGType type = vecl + TCG_TYPE_V64; 1872 bool lasx = type == TCG_TYPE_V256; 1873 TCGArg a0, a1, a2, a3; 1874 LoongArchInsn insn; 1875 1876 static const LoongArchInsn cmp_vec_insn[16][2][4] = { 1877 [TCG_COND_EQ] = { 1878 { OPC_VSEQ_B, OPC_VSEQ_H, OPC_VSEQ_W, OPC_VSEQ_D }, 1879 { OPC_XVSEQ_B, OPC_XVSEQ_H, OPC_XVSEQ_W, OPC_XVSEQ_D }, 1880 }, 1881 [TCG_COND_LE] = { 1882 { OPC_VSLE_B, OPC_VSLE_H, OPC_VSLE_W, OPC_VSLE_D }, 1883 { OPC_XVSLE_B, OPC_XVSLE_H, OPC_XVSLE_W, OPC_XVSLE_D }, 1884 }, 1885 [TCG_COND_LEU] = { 1886 { OPC_VSLE_BU, OPC_VSLE_HU, OPC_VSLE_WU, OPC_VSLE_DU }, 1887 { OPC_XVSLE_BU, OPC_XVSLE_HU, OPC_XVSLE_WU, OPC_XVSLE_DU }, 1888 }, 1889 [TCG_COND_LT] = { 1890 { OPC_VSLT_B, OPC_VSLT_H, OPC_VSLT_W, OPC_VSLT_D }, 1891 { OPC_XVSLT_B, OPC_XVSLT_H, OPC_XVSLT_W, OPC_XVSLT_D }, 1892 }, 1893 [TCG_COND_LTU] = { 1894 { OPC_VSLT_BU, OPC_VSLT_HU, OPC_VSLT_WU, OPC_VSLT_DU }, 1895 { OPC_XVSLT_BU, OPC_XVSLT_HU, OPC_XVSLT_WU, OPC_XVSLT_DU }, 1896 } 1897 }; 1898 static const LoongArchInsn cmp_vec_imm_insn[16][2][4] = { 1899 [TCG_COND_EQ] = { 1900 { OPC_VSEQI_B, OPC_VSEQI_H, OPC_VSEQI_W, OPC_VSEQI_D }, 1901 { OPC_XVSEQI_B, OPC_XVSEQI_H, OPC_XVSEQI_W, OPC_XVSEQI_D }, 1902 }, 1903 [TCG_COND_LE] = { 1904 { OPC_VSLEI_B, OPC_VSLEI_H, OPC_VSLEI_W, OPC_VSLEI_D }, 1905 { OPC_XVSLEI_B, OPC_XVSLEI_H, OPC_XVSLEI_W, OPC_XVSLEI_D }, 1906 }, 1907 [TCG_COND_LEU] = { 1908 { OPC_VSLEI_BU, OPC_VSLEI_HU, OPC_VSLEI_WU, OPC_VSLEI_DU }, 1909 { OPC_XVSLEI_BU, OPC_XVSLEI_HU, OPC_XVSLEI_WU, OPC_XVSLEI_DU }, 1910 }, 1911 [TCG_COND_LT] = { 1912 { OPC_VSLTI_B, OPC_VSLTI_H, OPC_VSLTI_W, OPC_VSLTI_D }, 1913 { OPC_XVSLTI_B, OPC_XVSLTI_H, OPC_XVSLTI_W, OPC_XVSLTI_D }, 1914 }, 1915 [TCG_COND_LTU] = { 1916 { OPC_VSLTI_BU, OPC_VSLTI_HU, OPC_VSLTI_WU, OPC_VSLTI_DU }, 1917 { OPC_XVSLTI_BU, OPC_XVSLTI_HU, OPC_XVSLTI_WU, OPC_XVSLTI_DU }, 1918 } 1919 }; 1920 static const LoongArchInsn neg_vec_insn[2][4] = { 1921 { OPC_VNEG_B, OPC_VNEG_H, OPC_VNEG_W, OPC_VNEG_D }, 1922 { OPC_XVNEG_B, OPC_XVNEG_H, OPC_XVNEG_W, OPC_XVNEG_D }, 1923 }; 1924 static const LoongArchInsn mul_vec_insn[2][4] = { 1925 { OPC_VMUL_B, OPC_VMUL_H, OPC_VMUL_W, OPC_VMUL_D }, 1926 { OPC_XVMUL_B, OPC_XVMUL_H, OPC_XVMUL_W, OPC_XVMUL_D }, 1927 }; 1928 static const LoongArchInsn smin_vec_insn[2][4] = { 1929 { OPC_VMIN_B, OPC_VMIN_H, OPC_VMIN_W, OPC_VMIN_D }, 1930 { OPC_XVMIN_B, OPC_XVMIN_H, OPC_XVMIN_W, OPC_XVMIN_D }, 1931 }; 1932 static const LoongArchInsn umin_vec_insn[2][4] = { 1933 { OPC_VMIN_BU, OPC_VMIN_HU, OPC_VMIN_WU, OPC_VMIN_DU }, 1934 { OPC_XVMIN_BU, OPC_XVMIN_HU, OPC_XVMIN_WU, OPC_XVMIN_DU }, 1935 }; 1936 static const LoongArchInsn smax_vec_insn[2][4] = { 1937 { OPC_VMAX_B, OPC_VMAX_H, OPC_VMAX_W, OPC_VMAX_D }, 1938 { OPC_XVMAX_B, OPC_XVMAX_H, OPC_XVMAX_W, OPC_XVMAX_D }, 1939 }; 1940 static const LoongArchInsn umax_vec_insn[2][4] = { 1941 { OPC_VMAX_BU, OPC_VMAX_HU, OPC_VMAX_WU, OPC_VMAX_DU }, 1942 { OPC_XVMAX_BU, OPC_XVMAX_HU, OPC_XVMAX_WU, OPC_XVMAX_DU }, 1943 }; 1944 static const LoongArchInsn ssadd_vec_insn[2][4] = { 1945 { OPC_VSADD_B, OPC_VSADD_H, OPC_VSADD_W, OPC_VSADD_D }, 1946 { OPC_XVSADD_B, OPC_XVSADD_H, OPC_XVSADD_W, OPC_XVSADD_D }, 1947 }; 1948 static const LoongArchInsn usadd_vec_insn[2][4] = { 1949 { OPC_VSADD_BU, OPC_VSADD_HU, OPC_VSADD_WU, OPC_VSADD_DU }, 1950 { OPC_XVSADD_BU, OPC_XVSADD_HU, OPC_XVSADD_WU, OPC_XVSADD_DU }, 1951 }; 1952 static const LoongArchInsn sssub_vec_insn[2][4] = { 1953 { OPC_VSSUB_B, OPC_VSSUB_H, OPC_VSSUB_W, OPC_VSSUB_D }, 1954 { OPC_XVSSUB_B, OPC_XVSSUB_H, OPC_XVSSUB_W, OPC_XVSSUB_D }, 1955 }; 1956 static const LoongArchInsn ussub_vec_insn[2][4] = { 1957 { OPC_VSSUB_BU, OPC_VSSUB_HU, OPC_VSSUB_WU, OPC_VSSUB_DU }, 1958 { OPC_XVSSUB_BU, OPC_XVSSUB_HU, OPC_XVSSUB_WU, OPC_XVSSUB_DU }, 1959 }; 1960 static const LoongArchInsn shlv_vec_insn[2][4] = { 1961 { OPC_VSLL_B, OPC_VSLL_H, OPC_VSLL_W, OPC_VSLL_D }, 1962 { OPC_XVSLL_B, OPC_XVSLL_H, OPC_XVSLL_W, OPC_XVSLL_D }, 1963 }; 1964 static const LoongArchInsn shrv_vec_insn[2][4] = { 1965 { OPC_VSRL_B, OPC_VSRL_H, OPC_VSRL_W, OPC_VSRL_D }, 1966 { OPC_XVSRL_B, OPC_XVSRL_H, OPC_XVSRL_W, OPC_XVSRL_D }, 1967 }; 1968 static const LoongArchInsn sarv_vec_insn[2][4] = { 1969 { OPC_VSRA_B, OPC_VSRA_H, OPC_VSRA_W, OPC_VSRA_D }, 1970 { OPC_XVSRA_B, OPC_XVSRA_H, OPC_XVSRA_W, OPC_XVSRA_D }, 1971 }; 1972 static const LoongArchInsn shli_vec_insn[2][4] = { 1973 { OPC_VSLLI_B, OPC_VSLLI_H, OPC_VSLLI_W, OPC_VSLLI_D }, 1974 { OPC_XVSLLI_B, OPC_XVSLLI_H, OPC_XVSLLI_W, OPC_XVSLLI_D }, 1975 }; 1976 static const LoongArchInsn shri_vec_insn[2][4] = { 1977 { OPC_VSRLI_B, OPC_VSRLI_H, OPC_VSRLI_W, OPC_VSRLI_D }, 1978 { OPC_XVSRLI_B, OPC_XVSRLI_H, OPC_XVSRLI_W, OPC_XVSRLI_D }, 1979 }; 1980 static const LoongArchInsn sari_vec_insn[2][4] = { 1981 { OPC_VSRAI_B, OPC_VSRAI_H, OPC_VSRAI_W, OPC_VSRAI_D }, 1982 { OPC_XVSRAI_B, OPC_XVSRAI_H, OPC_XVSRAI_W, OPC_XVSRAI_D }, 1983 }; 1984 static const LoongArchInsn rotrv_vec_insn[2][4] = { 1985 { OPC_VROTR_B, OPC_VROTR_H, OPC_VROTR_W, OPC_VROTR_D }, 1986 { OPC_XVROTR_B, OPC_XVROTR_H, OPC_XVROTR_W, OPC_XVROTR_D }, 1987 }; 1988 static const LoongArchInsn rotri_vec_insn[2][4] = { 1989 { OPC_VROTRI_B, OPC_VROTRI_H, OPC_VROTRI_W, OPC_VROTRI_D }, 1990 { OPC_XVROTRI_B, OPC_XVROTRI_H, OPC_XVROTRI_W, OPC_XVROTRI_D }, 1991 }; 1992 1993 a0 = args[0]; 1994 a1 = args[1]; 1995 a2 = args[2]; 1996 a3 = args[3]; 1997 1998 switch (opc) { 1999 case INDEX_op_st_vec: 2000 tcg_out_st(s, type, a0, a1, a2); 2001 break; 2002 case INDEX_op_ld_vec: 2003 tcg_out_ld(s, type, a0, a1, a2); 2004 break; 2005 case INDEX_op_and_vec: 2006 insn = lasx ? OPC_XVAND_V : OPC_VAND_V; 2007 goto vdvjvk; 2008 case INDEX_op_andc_vec: 2009 /* 2010 * vandn vd, vj, vk: vd = vk & ~vj 2011 * andc_vec vd, vj, vk: vd = vj & ~vk 2012 * vj and vk are swapped 2013 */ 2014 a1 = a2; 2015 a2 = args[1]; 2016 insn = lasx ? OPC_XVANDN_V : OPC_VANDN_V; 2017 goto vdvjvk; 2018 case INDEX_op_or_vec: 2019 insn = lasx ? OPC_XVOR_V : OPC_VOR_V; 2020 goto vdvjvk; 2021 case INDEX_op_orc_vec: 2022 insn = lasx ? OPC_XVORN_V : OPC_VORN_V; 2023 goto vdvjvk; 2024 case INDEX_op_xor_vec: 2025 insn = lasx ? OPC_XVXOR_V : OPC_VXOR_V; 2026 goto vdvjvk; 2027 case INDEX_op_not_vec: 2028 a2 = a1; 2029 /* fall through */ 2030 case INDEX_op_nor_vec: 2031 insn = lasx ? OPC_XVNOR_V : OPC_VNOR_V; 2032 goto vdvjvk; 2033 case INDEX_op_cmp_vec: 2034 { 2035 TCGCond cond = args[3]; 2036 2037 if (const_args[2]) { 2038 /* 2039 * cmp_vec dest, src, value 2040 * Try vseqi/vslei/vslti 2041 */ 2042 int64_t value = sextract64(a2, 0, 8 << vece); 2043 switch (cond) { 2044 case TCG_COND_EQ: 2045 case TCG_COND_LE: 2046 case TCG_COND_LT: 2047 insn = cmp_vec_imm_insn[cond][lasx][vece]; 2048 tcg_out32(s, encode_vdvjsk5_insn(insn, a0, a1, value)); 2049 break; 2050 case TCG_COND_LEU: 2051 case TCG_COND_LTU: 2052 insn = cmp_vec_imm_insn[cond][lasx][vece]; 2053 tcg_out32(s, encode_vdvjuk5_insn(insn, a0, a1, value)); 2054 break; 2055 default: 2056 g_assert_not_reached(); 2057 } 2058 break; 2059 } 2060 2061 insn = cmp_vec_insn[cond][lasx][vece]; 2062 if (insn == 0) { 2063 TCGArg t; 2064 t = a1, a1 = a2, a2 = t; 2065 cond = tcg_swap_cond(cond); 2066 insn = cmp_vec_insn[cond][lasx][vece]; 2067 tcg_debug_assert(insn != 0); 2068 } 2069 } 2070 goto vdvjvk; 2071 case INDEX_op_add_vec: 2072 tcg_out_addsub_vec(s, lasx, vece, a0, a1, a2, const_args[2], true); 2073 break; 2074 case INDEX_op_sub_vec: 2075 tcg_out_addsub_vec(s, lasx, vece, a0, a1, a2, const_args[2], false); 2076 break; 2077 case INDEX_op_neg_vec: 2078 tcg_out32(s, encode_vdvj_insn(neg_vec_insn[lasx][vece], a0, a1)); 2079 break; 2080 case INDEX_op_mul_vec: 2081 insn = mul_vec_insn[lasx][vece]; 2082 goto vdvjvk; 2083 case INDEX_op_smin_vec: 2084 insn = smin_vec_insn[lasx][vece]; 2085 goto vdvjvk; 2086 case INDEX_op_smax_vec: 2087 insn = smax_vec_insn[lasx][vece]; 2088 goto vdvjvk; 2089 case INDEX_op_umin_vec: 2090 insn = umin_vec_insn[lasx][vece]; 2091 goto vdvjvk; 2092 case INDEX_op_umax_vec: 2093 insn = umax_vec_insn[lasx][vece]; 2094 goto vdvjvk; 2095 case INDEX_op_ssadd_vec: 2096 insn = ssadd_vec_insn[lasx][vece]; 2097 goto vdvjvk; 2098 case INDEX_op_usadd_vec: 2099 insn = usadd_vec_insn[lasx][vece]; 2100 goto vdvjvk; 2101 case INDEX_op_sssub_vec: 2102 insn = sssub_vec_insn[lasx][vece]; 2103 goto vdvjvk; 2104 case INDEX_op_ussub_vec: 2105 insn = ussub_vec_insn[lasx][vece]; 2106 goto vdvjvk; 2107 case INDEX_op_shlv_vec: 2108 insn = shlv_vec_insn[lasx][vece]; 2109 goto vdvjvk; 2110 case INDEX_op_shrv_vec: 2111 insn = shrv_vec_insn[lasx][vece]; 2112 goto vdvjvk; 2113 case INDEX_op_sarv_vec: 2114 insn = sarv_vec_insn[lasx][vece]; 2115 goto vdvjvk; 2116 case INDEX_op_rotlv_vec: 2117 /* rotlv_vec a1, a2 = rotrv_vec a1, -a2 */ 2118 tcg_out32(s, encode_vdvj_insn(neg_vec_insn[lasx][vece], 2119 TCG_VEC_TMP0, a2)); 2120 a2 = TCG_VEC_TMP0; 2121 /* fall through */ 2122 case INDEX_op_rotrv_vec: 2123 insn = rotrv_vec_insn[lasx][vece]; 2124 goto vdvjvk; 2125 case INDEX_op_shli_vec: 2126 insn = shli_vec_insn[lasx][vece]; 2127 goto vdvjukN; 2128 case INDEX_op_shri_vec: 2129 insn = shri_vec_insn[lasx][vece]; 2130 goto vdvjukN; 2131 case INDEX_op_sari_vec: 2132 insn = sari_vec_insn[lasx][vece]; 2133 goto vdvjukN; 2134 case INDEX_op_rotli_vec: 2135 /* rotli_vec a1, a2 = rotri_vec a1, -a2 */ 2136 a2 = extract32(-a2, 0, 3 + vece); 2137 insn = rotri_vec_insn[lasx][vece]; 2138 goto vdvjukN; 2139 case INDEX_op_bitsel_vec: 2140 /* vbitsel vd, vj, vk, va = bitsel_vec vd, va, vk, vj */ 2141 if (lasx) { 2142 tcg_out_opc_xvbitsel_v(s, a0, a3, a2, a1); 2143 } else { 2144 tcg_out_opc_vbitsel_v(s, a0, a3, a2, a1); 2145 } 2146 break; 2147 case INDEX_op_dupm_vec: 2148 tcg_out_dupm_vec(s, type, vece, a0, a1, a2); 2149 break; 2150 default: 2151 g_assert_not_reached(); 2152 vdvjvk: 2153 tcg_out32(s, encode_vdvjvk_insn(insn, a0, a1, a2)); 2154 break; 2155 vdvjukN: 2156 switch (vece) { 2157 case MO_8: 2158 tcg_out32(s, encode_vdvjuk3_insn(insn, a0, a1, a2)); 2159 break; 2160 case MO_16: 2161 tcg_out32(s, encode_vdvjuk4_insn(insn, a0, a1, a2)); 2162 break; 2163 case MO_32: 2164 tcg_out32(s, encode_vdvjuk5_insn(insn, a0, a1, a2)); 2165 break; 2166 case MO_64: 2167 tcg_out32(s, encode_vdvjuk6_insn(insn, a0, a1, a2)); 2168 break; 2169 default: 2170 g_assert_not_reached(); 2171 } 2172 break; 2173 } 2174} 2175 2176int tcg_can_emit_vec_op(TCGOpcode opc, TCGType type, unsigned vece) 2177{ 2178 switch (opc) { 2179 case INDEX_op_ld_vec: 2180 case INDEX_op_st_vec: 2181 case INDEX_op_dup_vec: 2182 case INDEX_op_dupm_vec: 2183 case INDEX_op_cmp_vec: 2184 case INDEX_op_add_vec: 2185 case INDEX_op_sub_vec: 2186 case INDEX_op_and_vec: 2187 case INDEX_op_andc_vec: 2188 case INDEX_op_or_vec: 2189 case INDEX_op_orc_vec: 2190 case INDEX_op_xor_vec: 2191 case INDEX_op_nor_vec: 2192 case INDEX_op_not_vec: 2193 case INDEX_op_neg_vec: 2194 case INDEX_op_mul_vec: 2195 case INDEX_op_smin_vec: 2196 case INDEX_op_smax_vec: 2197 case INDEX_op_umin_vec: 2198 case INDEX_op_umax_vec: 2199 case INDEX_op_ssadd_vec: 2200 case INDEX_op_usadd_vec: 2201 case INDEX_op_sssub_vec: 2202 case INDEX_op_ussub_vec: 2203 case INDEX_op_shlv_vec: 2204 case INDEX_op_shrv_vec: 2205 case INDEX_op_sarv_vec: 2206 case INDEX_op_bitsel_vec: 2207 return 1; 2208 default: 2209 return 0; 2210 } 2211} 2212 2213void tcg_expand_vec_op(TCGOpcode opc, TCGType type, unsigned vece, 2214 TCGArg a0, ...) 2215{ 2216 g_assert_not_reached(); 2217} 2218 2219static TCGConstraintSetIndex 2220tcg_target_op_def(TCGOpcode op, TCGType type, unsigned flags) 2221{ 2222 switch (op) { 2223 case INDEX_op_goto_ptr: 2224 return C_O0_I1(r); 2225 2226 case INDEX_op_st8_i32: 2227 case INDEX_op_st8_i64: 2228 case INDEX_op_st16_i32: 2229 case INDEX_op_st16_i64: 2230 case INDEX_op_st32_i64: 2231 case INDEX_op_st_i32: 2232 case INDEX_op_st_i64: 2233 case INDEX_op_qemu_st_i32: 2234 case INDEX_op_qemu_st_i64: 2235 return C_O0_I2(rz, r); 2236 2237 case INDEX_op_qemu_ld_i128: 2238 return C_N2_I1(r, r, r); 2239 2240 case INDEX_op_qemu_st_i128: 2241 return C_O0_I3(r, r, r); 2242 2243 case INDEX_op_brcond_i32: 2244 case INDEX_op_brcond_i64: 2245 return C_O0_I2(rz, rz); 2246 2247 case INDEX_op_extu_i32_i64: 2248 case INDEX_op_extrl_i64_i32: 2249 case INDEX_op_extrh_i64_i32: 2250 case INDEX_op_ext_i32_i64: 2251 case INDEX_op_neg_i32: 2252 case INDEX_op_neg_i64: 2253 case INDEX_op_not_i32: 2254 case INDEX_op_not_i64: 2255 case INDEX_op_extract_i32: 2256 case INDEX_op_extract_i64: 2257 case INDEX_op_sextract_i32: 2258 case INDEX_op_sextract_i64: 2259 case INDEX_op_bswap16_i32: 2260 case INDEX_op_bswap16_i64: 2261 case INDEX_op_bswap32_i32: 2262 case INDEX_op_bswap32_i64: 2263 case INDEX_op_bswap64_i64: 2264 case INDEX_op_ld8s_i32: 2265 case INDEX_op_ld8s_i64: 2266 case INDEX_op_ld8u_i32: 2267 case INDEX_op_ld8u_i64: 2268 case INDEX_op_ld16s_i32: 2269 case INDEX_op_ld16s_i64: 2270 case INDEX_op_ld16u_i32: 2271 case INDEX_op_ld16u_i64: 2272 case INDEX_op_ld32s_i64: 2273 case INDEX_op_ld32u_i64: 2274 case INDEX_op_ld_i32: 2275 case INDEX_op_ld_i64: 2276 case INDEX_op_qemu_ld_i32: 2277 case INDEX_op_qemu_ld_i64: 2278 return C_O1_I1(r, r); 2279 2280 case INDEX_op_orc_i32: 2281 case INDEX_op_orc_i64: 2282 /* 2283 * LoongArch insns for these ops don't have reg-imm forms, but we 2284 * can express using andi/ori if ~constant satisfies 2285 * TCG_CT_CONST_U12. 2286 */ 2287 return C_O1_I2(r, r, rC); 2288 2289 case INDEX_op_shl_i32: 2290 case INDEX_op_shl_i64: 2291 case INDEX_op_shr_i32: 2292 case INDEX_op_shr_i64: 2293 case INDEX_op_sar_i32: 2294 case INDEX_op_sar_i64: 2295 case INDEX_op_rotl_i32: 2296 case INDEX_op_rotl_i64: 2297 case INDEX_op_rotr_i32: 2298 case INDEX_op_rotr_i64: 2299 return C_O1_I2(r, r, ri); 2300 2301 case INDEX_op_nor_i32: 2302 case INDEX_op_nor_i64: 2303 case INDEX_op_or_i32: 2304 case INDEX_op_or_i64: 2305 case INDEX_op_xor_i32: 2306 case INDEX_op_xor_i64: 2307 /* LoongArch reg-imm bitops have their imms ZERO-extended */ 2308 return C_O1_I2(r, r, rU); 2309 2310 case INDEX_op_clz_i32: 2311 case INDEX_op_clz_i64: 2312 case INDEX_op_ctz_i32: 2313 case INDEX_op_ctz_i64: 2314 return C_O1_I2(r, r, rW); 2315 2316 case INDEX_op_deposit_i32: 2317 case INDEX_op_deposit_i64: 2318 /* Must deposit into the same register as input */ 2319 return C_O1_I2(r, 0, rz); 2320 2321 case INDEX_op_sub_i32: 2322 case INDEX_op_setcond_i32: 2323 return C_O1_I2(r, rz, ri); 2324 case INDEX_op_sub_i64: 2325 case INDEX_op_setcond_i64: 2326 return C_O1_I2(r, rz, rJ); 2327 2328 case INDEX_op_mul_i32: 2329 case INDEX_op_mul_i64: 2330 case INDEX_op_mulsh_i32: 2331 case INDEX_op_mulsh_i64: 2332 case INDEX_op_muluh_i32: 2333 case INDEX_op_muluh_i64: 2334 case INDEX_op_div_i32: 2335 case INDEX_op_div_i64: 2336 case INDEX_op_divu_i32: 2337 case INDEX_op_divu_i64: 2338 case INDEX_op_rem_i32: 2339 case INDEX_op_rem_i64: 2340 case INDEX_op_remu_i32: 2341 case INDEX_op_remu_i64: 2342 return C_O1_I2(r, rz, rz); 2343 2344 case INDEX_op_movcond_i32: 2345 case INDEX_op_movcond_i64: 2346 return C_O1_I4(r, rz, rJ, rz, rz); 2347 2348 case INDEX_op_ld_vec: 2349 case INDEX_op_dupm_vec: 2350 case INDEX_op_dup_vec: 2351 return C_O1_I1(w, r); 2352 2353 case INDEX_op_st_vec: 2354 return C_O0_I2(w, r); 2355 2356 case INDEX_op_cmp_vec: 2357 return C_O1_I2(w, w, wM); 2358 2359 case INDEX_op_add_vec: 2360 case INDEX_op_sub_vec: 2361 return C_O1_I2(w, w, wA); 2362 2363 case INDEX_op_and_vec: 2364 case INDEX_op_andc_vec: 2365 case INDEX_op_or_vec: 2366 case INDEX_op_orc_vec: 2367 case INDEX_op_xor_vec: 2368 case INDEX_op_nor_vec: 2369 case INDEX_op_mul_vec: 2370 case INDEX_op_smin_vec: 2371 case INDEX_op_smax_vec: 2372 case INDEX_op_umin_vec: 2373 case INDEX_op_umax_vec: 2374 case INDEX_op_ssadd_vec: 2375 case INDEX_op_usadd_vec: 2376 case INDEX_op_sssub_vec: 2377 case INDEX_op_ussub_vec: 2378 case INDEX_op_shlv_vec: 2379 case INDEX_op_shrv_vec: 2380 case INDEX_op_sarv_vec: 2381 case INDEX_op_rotrv_vec: 2382 case INDEX_op_rotlv_vec: 2383 return C_O1_I2(w, w, w); 2384 2385 case INDEX_op_not_vec: 2386 case INDEX_op_neg_vec: 2387 case INDEX_op_shli_vec: 2388 case INDEX_op_shri_vec: 2389 case INDEX_op_sari_vec: 2390 case INDEX_op_rotli_vec: 2391 return C_O1_I1(w, w); 2392 2393 case INDEX_op_bitsel_vec: 2394 return C_O1_I3(w, w, w, w); 2395 2396 default: 2397 return C_NotImplemented; 2398 } 2399} 2400 2401static const int tcg_target_callee_save_regs[] = { 2402 TCG_REG_S0, /* used for the global env (TCG_AREG0) */ 2403 TCG_REG_S1, 2404 TCG_REG_S2, 2405 TCG_REG_S3, 2406 TCG_REG_S4, 2407 TCG_REG_S5, 2408 TCG_REG_S6, 2409 TCG_REG_S7, 2410 TCG_REG_S8, 2411 TCG_REG_S9, 2412 TCG_REG_RA, /* should be last for ABI compliance */ 2413}; 2414 2415/* Stack frame parameters. */ 2416#define REG_SIZE (TCG_TARGET_REG_BITS / 8) 2417#define SAVE_SIZE ((int)ARRAY_SIZE(tcg_target_callee_save_regs) * REG_SIZE) 2418#define TEMP_SIZE (CPU_TEMP_BUF_NLONGS * (int)sizeof(long)) 2419#define FRAME_SIZE ((TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE + SAVE_SIZE \ 2420 + TCG_TARGET_STACK_ALIGN - 1) \ 2421 & -TCG_TARGET_STACK_ALIGN) 2422#define SAVE_OFS (TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE) 2423 2424/* We're expecting to be able to use an immediate for frame allocation. */ 2425QEMU_BUILD_BUG_ON(FRAME_SIZE > 0x7ff); 2426 2427/* Generate global QEMU prologue and epilogue code */ 2428static void tcg_target_qemu_prologue(TCGContext *s) 2429{ 2430 int i; 2431 2432 tcg_set_frame(s, TCG_REG_SP, TCG_STATIC_CALL_ARGS_SIZE, TEMP_SIZE); 2433 2434 /* TB prologue */ 2435 tcg_out_opc_addi_d(s, TCG_REG_SP, TCG_REG_SP, -FRAME_SIZE); 2436 for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) { 2437 tcg_out_st(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i], 2438 TCG_REG_SP, SAVE_OFS + i * REG_SIZE); 2439 } 2440 2441 if (!tcg_use_softmmu && guest_base) { 2442 tcg_out_movi(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG, guest_base); 2443 tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG); 2444 } 2445 2446 /* Call generated code */ 2447 tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]); 2448 tcg_out_opc_jirl(s, TCG_REG_ZERO, tcg_target_call_iarg_regs[1], 0); 2449 2450 /* Return path for goto_ptr. Set return value to 0 */ 2451 tcg_code_gen_epilogue = tcg_splitwx_to_rx(s->code_ptr); 2452 tcg_out_mov(s, TCG_TYPE_REG, TCG_REG_A0, TCG_REG_ZERO); 2453 2454 /* TB epilogue */ 2455 tb_ret_addr = tcg_splitwx_to_rx(s->code_ptr); 2456 for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) { 2457 tcg_out_ld(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i], 2458 TCG_REG_SP, SAVE_OFS + i * REG_SIZE); 2459 } 2460 2461 tcg_out_opc_addi_d(s, TCG_REG_SP, TCG_REG_SP, FRAME_SIZE); 2462 tcg_out_opc_jirl(s, TCG_REG_ZERO, TCG_REG_RA, 0); 2463} 2464 2465static void tcg_out_tb_start(TCGContext *s) 2466{ 2467 /* nothing to do */ 2468} 2469 2470static void tcg_out_nop_fill(tcg_insn_unit *p, int count) 2471{ 2472 for (int i = 0; i < count; ++i) { 2473 /* Canonical nop is andi r0,r0,0 */ 2474 p[i] = OPC_ANDI; 2475 } 2476} 2477 2478static void tcg_target_init(TCGContext *s) 2479{ 2480 unsigned long hwcap = qemu_getauxval(AT_HWCAP); 2481 2482 /* Server and desktop class cpus have UAL; embedded cpus do not. */ 2483 if (!(hwcap & HWCAP_LOONGARCH_UAL)) { 2484 error_report("TCG: unaligned access support required; exiting"); 2485 exit(EXIT_FAILURE); 2486 } 2487 2488 tcg_target_available_regs[TCG_TYPE_I32] = ALL_GENERAL_REGS; 2489 tcg_target_available_regs[TCG_TYPE_I64] = ALL_GENERAL_REGS; 2490 2491 tcg_target_call_clobber_regs = ALL_GENERAL_REGS | ALL_VECTOR_REGS; 2492 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S0); 2493 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S1); 2494 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S2); 2495 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S3); 2496 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S4); 2497 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S5); 2498 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S6); 2499 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S7); 2500 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S8); 2501 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S9); 2502 2503 if (cpuinfo & CPUINFO_LSX) { 2504 tcg_target_available_regs[TCG_TYPE_V64] = ALL_VECTOR_REGS; 2505 tcg_target_available_regs[TCG_TYPE_V128] = ALL_VECTOR_REGS; 2506 if (cpuinfo & CPUINFO_LASX) { 2507 tcg_target_available_regs[TCG_TYPE_V256] = ALL_VECTOR_REGS; 2508 } 2509 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_V24); 2510 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_V25); 2511 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_V26); 2512 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_V27); 2513 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_V28); 2514 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_V29); 2515 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_V30); 2516 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_V31); 2517 } 2518 2519 s->reserved_regs = 0; 2520 tcg_regset_set_reg(s->reserved_regs, TCG_REG_ZERO); 2521 tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP0); 2522 tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP1); 2523 tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP2); 2524 tcg_regset_set_reg(s->reserved_regs, TCG_REG_SP); 2525 tcg_regset_set_reg(s->reserved_regs, TCG_REG_TP); 2526 tcg_regset_set_reg(s->reserved_regs, TCG_REG_RESERVED); 2527 tcg_regset_set_reg(s->reserved_regs, TCG_VEC_TMP0); 2528} 2529 2530typedef struct { 2531 DebugFrameHeader h; 2532 uint8_t fde_def_cfa[4]; 2533 uint8_t fde_reg_ofs[ARRAY_SIZE(tcg_target_callee_save_regs) * 2]; 2534} DebugFrame; 2535 2536#define ELF_HOST_MACHINE EM_LOONGARCH 2537 2538static const DebugFrame debug_frame = { 2539 .h.cie.len = sizeof(DebugFrameCIE) - 4, /* length after .len member */ 2540 .h.cie.id = -1, 2541 .h.cie.version = 1, 2542 .h.cie.code_align = 1, 2543 .h.cie.data_align = -(TCG_TARGET_REG_BITS / 8) & 0x7f, /* sleb128 */ 2544 .h.cie.return_column = TCG_REG_RA, 2545 2546 /* Total FDE size does not include the "len" member. */ 2547 .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset), 2548 2549 .fde_def_cfa = { 2550 12, TCG_REG_SP, /* DW_CFA_def_cfa sp, ... */ 2551 (FRAME_SIZE & 0x7f) | 0x80, /* ... uleb128 FRAME_SIZE */ 2552 (FRAME_SIZE >> 7) 2553 }, 2554 .fde_reg_ofs = { 2555 0x80 + 23, 11, /* DW_CFA_offset, s0, -88 */ 2556 0x80 + 24, 10, /* DW_CFA_offset, s1, -80 */ 2557 0x80 + 25, 9, /* DW_CFA_offset, s2, -72 */ 2558 0x80 + 26, 8, /* DW_CFA_offset, s3, -64 */ 2559 0x80 + 27, 7, /* DW_CFA_offset, s4, -56 */ 2560 0x80 + 28, 6, /* DW_CFA_offset, s5, -48 */ 2561 0x80 + 29, 5, /* DW_CFA_offset, s6, -40 */ 2562 0x80 + 30, 4, /* DW_CFA_offset, s7, -32 */ 2563 0x80 + 31, 3, /* DW_CFA_offset, s8, -24 */ 2564 0x80 + 22, 2, /* DW_CFA_offset, s9, -16 */ 2565 0x80 + 1 , 1, /* DW_CFA_offset, ra, -8 */ 2566 } 2567}; 2568 2569void tcg_register_jit(const void *buf, size_t buf_size) 2570{ 2571 tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame)); 2572} 2573