1/* 2 * Tiny Code Generator for QEMU 3 * 4 * Copyright (c) 2008-2009 Arnaud Patard <arnaud.patard@rtp-net.org> 5 * Copyright (c) 2009 Aurelien Jarno <aurelien@aurel32.net> 6 * Based on i386/tcg-target.c - Copyright (c) 2008 Fabrice Bellard 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/* used for function call generation */ 28#define TCG_TARGET_STACK_ALIGN 16 29#if _MIPS_SIM == _ABIO32 30# define TCG_TARGET_CALL_STACK_OFFSET 16 31# define TCG_TARGET_CALL_ARG_I64 TCG_CALL_ARG_EVEN 32# define TCG_TARGET_CALL_RET_I128 TCG_CALL_RET_BY_REF 33#else 34# define TCG_TARGET_CALL_STACK_OFFSET 0 35# define TCG_TARGET_CALL_ARG_I64 TCG_CALL_ARG_NORMAL 36# define TCG_TARGET_CALL_RET_I128 TCG_CALL_RET_NORMAL 37#endif 38#define TCG_TARGET_CALL_ARG_I32 TCG_CALL_ARG_NORMAL 39#define TCG_TARGET_CALL_ARG_I128 TCG_CALL_ARG_EVEN 40 41#if TCG_TARGET_REG_BITS == 32 42# define LO_OFF (HOST_BIG_ENDIAN * 4) 43# define HI_OFF (4 - LO_OFF) 44#else 45/* Assert at compile-time that these values are never used for 64-bit. */ 46# define LO_OFF ({ qemu_build_not_reached(); 0; }) 47# define HI_OFF ({ qemu_build_not_reached(); 0; }) 48#endif 49 50#ifdef CONFIG_DEBUG_TCG 51static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = { 52 "zero", 53 "at", 54 "v0", 55 "v1", 56 "a0", 57 "a1", 58 "a2", 59 "a3", 60 "t0", 61 "t1", 62 "t2", 63 "t3", 64 "t4", 65 "t5", 66 "t6", 67 "t7", 68 "s0", 69 "s1", 70 "s2", 71 "s3", 72 "s4", 73 "s5", 74 "s6", 75 "s7", 76 "t8", 77 "t9", 78 "k0", 79 "k1", 80 "gp", 81 "sp", 82 "s8", 83 "ra", 84}; 85#endif 86 87#define TCG_TMP0 TCG_REG_AT 88#define TCG_TMP1 TCG_REG_T9 89#define TCG_TMP2 TCG_REG_T8 90#define TCG_TMP3 TCG_REG_T7 91 92#define TCG_GUEST_BASE_REG TCG_REG_S7 93#if TCG_TARGET_REG_BITS == 64 94#define TCG_REG_TB TCG_REG_S6 95#else 96#define TCG_REG_TB ({ qemu_build_not_reached(); TCG_REG_ZERO; }) 97#endif 98 99/* check if we really need so many registers :P */ 100static const int tcg_target_reg_alloc_order[] = { 101 /* Call saved registers. */ 102 TCG_REG_S0, 103 TCG_REG_S1, 104 TCG_REG_S2, 105 TCG_REG_S3, 106 TCG_REG_S4, 107 TCG_REG_S5, 108 TCG_REG_S6, 109 TCG_REG_S7, 110 TCG_REG_S8, 111 112 /* Call clobbered registers. */ 113 TCG_REG_T4, 114 TCG_REG_T5, 115 TCG_REG_T6, 116 TCG_REG_T7, 117 TCG_REG_T8, 118 TCG_REG_T9, 119 TCG_REG_V1, 120 TCG_REG_V0, 121 122 /* Argument registers, opposite order of allocation. */ 123 TCG_REG_T3, 124 TCG_REG_T2, 125 TCG_REG_T1, 126 TCG_REG_T0, 127 TCG_REG_A3, 128 TCG_REG_A2, 129 TCG_REG_A1, 130 TCG_REG_A0, 131}; 132 133static const TCGReg tcg_target_call_iarg_regs[] = { 134 TCG_REG_A0, 135 TCG_REG_A1, 136 TCG_REG_A2, 137 TCG_REG_A3, 138#if _MIPS_SIM == _ABIN32 || _MIPS_SIM == _ABI64 139 TCG_REG_T0, 140 TCG_REG_T1, 141 TCG_REG_T2, 142 TCG_REG_T3, 143#endif 144}; 145 146static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot) 147{ 148 tcg_debug_assert(kind == TCG_CALL_RET_NORMAL); 149 tcg_debug_assert(slot >= 0 && slot <= 1); 150 return TCG_REG_V0 + slot; 151} 152 153static const tcg_insn_unit *tb_ret_addr; 154static const tcg_insn_unit *bswap32_addr; 155static const tcg_insn_unit *bswap32u_addr; 156static const tcg_insn_unit *bswap64_addr; 157 158static bool reloc_pc16(tcg_insn_unit *src_rw, const tcg_insn_unit *target) 159{ 160 /* Let the compiler perform the right-shift as part of the arithmetic. */ 161 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw); 162 ptrdiff_t disp = target - (src_rx + 1); 163 if (disp == (int16_t)disp) { 164 *src_rw = deposit32(*src_rw, 0, 16, disp); 165 return true; 166 } 167 return false; 168} 169 170static bool patch_reloc(tcg_insn_unit *code_ptr, int type, 171 intptr_t value, intptr_t addend) 172{ 173 value += addend; 174 switch (type) { 175 case R_MIPS_PC16: 176 return reloc_pc16(code_ptr, (const tcg_insn_unit *)value); 177 case R_MIPS_16: 178 if (value != (int16_t)value) { 179 return false; 180 } 181 *code_ptr = deposit32(*code_ptr, 0, 16, value); 182 return true; 183 } 184 g_assert_not_reached(); 185} 186 187#define TCG_CT_CONST_U16 0x100 /* Unsigned 16-bit: 0 - 0xffff. */ 188#define TCG_CT_CONST_S16 0x200 /* Signed 16-bit: -32768 - 32767 */ 189#define TCG_CT_CONST_P2M1 0x400 /* Power of 2 minus 1. */ 190#define TCG_CT_CONST_WSZ 0x800 /* word size */ 191 192#define ALL_GENERAL_REGS 0xffffffffu 193 194static bool is_p2m1(tcg_target_long val) 195{ 196 return val && ((val + 1) & val) == 0; 197} 198 199/* test if a constant matches the constraint */ 200static bool tcg_target_const_match(int64_t val, int ct, 201 TCGType type, TCGCond cond, int vece) 202{ 203 if (ct & TCG_CT_CONST) { 204 return 1; 205 } else if ((ct & TCG_CT_CONST_U16) && val == (uint16_t)val) { 206 return 1; 207 } else if ((ct & TCG_CT_CONST_S16) && val == (int16_t)val) { 208 return 1; 209 } else if ((ct & TCG_CT_CONST_P2M1) 210 && use_mips32r2_instructions && is_p2m1(val)) { 211 return 1; 212 } else if ((ct & TCG_CT_CONST_WSZ) 213 && val == (type == TCG_TYPE_I32 ? 32 : 64)) { 214 return 1; 215 } 216 return 0; 217} 218 219/* instruction opcodes */ 220typedef enum { 221 OPC_J = 002 << 26, 222 OPC_JAL = 003 << 26, 223 OPC_BEQ = 004 << 26, 224 OPC_BNE = 005 << 26, 225 OPC_BLEZ = 006 << 26, 226 OPC_BGTZ = 007 << 26, 227 OPC_ADDIU = 011 << 26, 228 OPC_SLTI = 012 << 26, 229 OPC_SLTIU = 013 << 26, 230 OPC_ANDI = 014 << 26, 231 OPC_ORI = 015 << 26, 232 OPC_XORI = 016 << 26, 233 OPC_LUI = 017 << 26, 234 OPC_BNEL = 025 << 26, 235 OPC_BNEZALC_R6 = 030 << 26, 236 OPC_DADDIU = 031 << 26, 237 OPC_LDL = 032 << 26, 238 OPC_LDR = 033 << 26, 239 OPC_LB = 040 << 26, 240 OPC_LH = 041 << 26, 241 OPC_LWL = 042 << 26, 242 OPC_LW = 043 << 26, 243 OPC_LBU = 044 << 26, 244 OPC_LHU = 045 << 26, 245 OPC_LWR = 046 << 26, 246 OPC_LWU = 047 << 26, 247 OPC_SB = 050 << 26, 248 OPC_SH = 051 << 26, 249 OPC_SWL = 052 << 26, 250 OPC_SW = 053 << 26, 251 OPC_SDL = 054 << 26, 252 OPC_SDR = 055 << 26, 253 OPC_SWR = 056 << 26, 254 OPC_LD = 067 << 26, 255 OPC_SD = 077 << 26, 256 257 OPC_SPECIAL = 000 << 26, 258 OPC_SLL = OPC_SPECIAL | 000, 259 OPC_SRL = OPC_SPECIAL | 002, 260 OPC_ROTR = OPC_SPECIAL | 002 | (1 << 21), 261 OPC_SRA = OPC_SPECIAL | 003, 262 OPC_SLLV = OPC_SPECIAL | 004, 263 OPC_SRLV = OPC_SPECIAL | 006, 264 OPC_ROTRV = OPC_SPECIAL | 006 | 0100, 265 OPC_SRAV = OPC_SPECIAL | 007, 266 OPC_JR_R5 = OPC_SPECIAL | 010, 267 OPC_JALR = OPC_SPECIAL | 011, 268 OPC_MOVZ = OPC_SPECIAL | 012, 269 OPC_MOVN = OPC_SPECIAL | 013, 270 OPC_SYNC = OPC_SPECIAL | 017, 271 OPC_MFHI = OPC_SPECIAL | 020, 272 OPC_MFLO = OPC_SPECIAL | 022, 273 OPC_DSLLV = OPC_SPECIAL | 024, 274 OPC_DSRLV = OPC_SPECIAL | 026, 275 OPC_DROTRV = OPC_SPECIAL | 026 | 0100, 276 OPC_DSRAV = OPC_SPECIAL | 027, 277 OPC_MULT = OPC_SPECIAL | 030, 278 OPC_MUL_R6 = OPC_SPECIAL | 030 | 0200, 279 OPC_MUH = OPC_SPECIAL | 030 | 0300, 280 OPC_MULTU = OPC_SPECIAL | 031, 281 OPC_MULU = OPC_SPECIAL | 031 | 0200, 282 OPC_MUHU = OPC_SPECIAL | 031 | 0300, 283 OPC_DIV = OPC_SPECIAL | 032, 284 OPC_DIV_R6 = OPC_SPECIAL | 032 | 0200, 285 OPC_MOD = OPC_SPECIAL | 032 | 0300, 286 OPC_DIVU = OPC_SPECIAL | 033, 287 OPC_DIVU_R6 = OPC_SPECIAL | 033 | 0200, 288 OPC_MODU = OPC_SPECIAL | 033 | 0300, 289 OPC_DMULT = OPC_SPECIAL | 034, 290 OPC_DMUL = OPC_SPECIAL | 034 | 0200, 291 OPC_DMUH = OPC_SPECIAL | 034 | 0300, 292 OPC_DMULTU = OPC_SPECIAL | 035, 293 OPC_DMULU = OPC_SPECIAL | 035 | 0200, 294 OPC_DMUHU = OPC_SPECIAL | 035 | 0300, 295 OPC_DDIV = OPC_SPECIAL | 036, 296 OPC_DDIV_R6 = OPC_SPECIAL | 036 | 0200, 297 OPC_DMOD = OPC_SPECIAL | 036 | 0300, 298 OPC_DDIVU = OPC_SPECIAL | 037, 299 OPC_DDIVU_R6 = OPC_SPECIAL | 037 | 0200, 300 OPC_DMODU = OPC_SPECIAL | 037 | 0300, 301 OPC_ADDU = OPC_SPECIAL | 041, 302 OPC_SUBU = OPC_SPECIAL | 043, 303 OPC_AND = OPC_SPECIAL | 044, 304 OPC_OR = OPC_SPECIAL | 045, 305 OPC_XOR = OPC_SPECIAL | 046, 306 OPC_NOR = OPC_SPECIAL | 047, 307 OPC_SLT = OPC_SPECIAL | 052, 308 OPC_SLTU = OPC_SPECIAL | 053, 309 OPC_DADDU = OPC_SPECIAL | 055, 310 OPC_DSUBU = OPC_SPECIAL | 057, 311 OPC_SELEQZ = OPC_SPECIAL | 065, 312 OPC_SELNEZ = OPC_SPECIAL | 067, 313 OPC_DSLL = OPC_SPECIAL | 070, 314 OPC_DSRL = OPC_SPECIAL | 072, 315 OPC_DROTR = OPC_SPECIAL | 072 | (1 << 21), 316 OPC_DSRA = OPC_SPECIAL | 073, 317 OPC_DSLL32 = OPC_SPECIAL | 074, 318 OPC_DSRL32 = OPC_SPECIAL | 076, 319 OPC_DROTR32 = OPC_SPECIAL | 076 | (1 << 21), 320 OPC_DSRA32 = OPC_SPECIAL | 077, 321 OPC_CLZ_R6 = OPC_SPECIAL | 0120, 322 OPC_DCLZ_R6 = OPC_SPECIAL | 0122, 323 324 OPC_REGIMM = 001 << 26, 325 OPC_BLTZ = OPC_REGIMM | (000 << 16), 326 OPC_BGEZ = OPC_REGIMM | (001 << 16), 327 328 OPC_SPECIAL2 = 034 << 26, 329 OPC_MUL_R5 = OPC_SPECIAL2 | 002, 330 OPC_CLZ = OPC_SPECIAL2 | 040, 331 OPC_DCLZ = OPC_SPECIAL2 | 044, 332 333 OPC_SPECIAL3 = 037 << 26, 334 OPC_EXT = OPC_SPECIAL3 | 000, 335 OPC_DEXTM = OPC_SPECIAL3 | 001, 336 OPC_DEXTU = OPC_SPECIAL3 | 002, 337 OPC_DEXT = OPC_SPECIAL3 | 003, 338 OPC_INS = OPC_SPECIAL3 | 004, 339 OPC_DINSM = OPC_SPECIAL3 | 005, 340 OPC_DINSU = OPC_SPECIAL3 | 006, 341 OPC_DINS = OPC_SPECIAL3 | 007, 342 OPC_WSBH = OPC_SPECIAL3 | 00240, 343 OPC_DSBH = OPC_SPECIAL3 | 00244, 344 OPC_DSHD = OPC_SPECIAL3 | 00544, 345 OPC_SEB = OPC_SPECIAL3 | 02040, 346 OPC_SEH = OPC_SPECIAL3 | 03040, 347 348 /* MIPS r6 doesn't have JR, JALR should be used instead */ 349 OPC_JR = use_mips32r6_instructions ? OPC_JALR : OPC_JR_R5, 350 351 /* 352 * MIPS r6 replaces MUL with an alternative encoding which is 353 * backwards-compatible at the assembly level. 354 */ 355 OPC_MUL = use_mips32r6_instructions ? OPC_MUL_R6 : OPC_MUL_R5, 356 357 /* MIPS r6 introduced names for weaker variants of SYNC. These are 358 backward compatible to previous architecture revisions. */ 359 OPC_SYNC_WMB = OPC_SYNC | 0x04 << 6, 360 OPC_SYNC_MB = OPC_SYNC | 0x10 << 6, 361 OPC_SYNC_ACQUIRE = OPC_SYNC | 0x11 << 6, 362 OPC_SYNC_RELEASE = OPC_SYNC | 0x12 << 6, 363 OPC_SYNC_RMB = OPC_SYNC | 0x13 << 6, 364 365 /* Aliases for convenience. */ 366 ALIAS_PADD = sizeof(void *) == 4 ? OPC_ADDU : OPC_DADDU, 367 ALIAS_PADDI = sizeof(void *) == 4 ? OPC_ADDIU : OPC_DADDIU, 368} MIPSInsn; 369 370/* 371 * Type reg 372 */ 373static void tcg_out_opc_reg(TCGContext *s, MIPSInsn opc, 374 TCGReg rd, TCGReg rs, TCGReg rt) 375{ 376 int32_t inst; 377 378 inst = opc; 379 inst |= (rs & 0x1F) << 21; 380 inst |= (rt & 0x1F) << 16; 381 inst |= (rd & 0x1F) << 11; 382 tcg_out32(s, inst); 383} 384 385/* 386 * Type immediate 387 */ 388static void tcg_out_opc_imm(TCGContext *s, MIPSInsn opc, 389 TCGReg rt, TCGReg rs, TCGArg imm) 390{ 391 int32_t inst; 392 393 inst = opc; 394 inst |= (rs & 0x1F) << 21; 395 inst |= (rt & 0x1F) << 16; 396 inst |= (imm & 0xffff); 397 tcg_out32(s, inst); 398} 399 400/* 401 * Type bitfield 402 */ 403static void tcg_out_opc_bf(TCGContext *s, MIPSInsn opc, TCGReg rt, 404 TCGReg rs, int msb, int lsb) 405{ 406 int32_t inst; 407 408 inst = opc; 409 inst |= (rs & 0x1F) << 21; 410 inst |= (rt & 0x1F) << 16; 411 inst |= (msb & 0x1F) << 11; 412 inst |= (lsb & 0x1F) << 6; 413 tcg_out32(s, inst); 414} 415 416static void tcg_out_opc_bf64(TCGContext *s, MIPSInsn opc, MIPSInsn opm, 417 MIPSInsn oph, TCGReg rt, TCGReg rs, 418 int msb, int lsb) 419{ 420 if (lsb >= 32) { 421 opc = oph; 422 msb -= 32; 423 lsb -= 32; 424 } else if (msb >= 32) { 425 opc = opm; 426 msb -= 32; 427 } 428 tcg_out_opc_bf(s, opc, rt, rs, msb, lsb); 429} 430 431/* 432 * Type branch 433 */ 434static void tcg_out_opc_br(TCGContext *s, MIPSInsn opc, TCGReg rt, TCGReg rs) 435{ 436 tcg_out_opc_imm(s, opc, rt, rs, 0); 437} 438 439/* 440 * Type sa 441 */ 442static void tcg_out_opc_sa(TCGContext *s, MIPSInsn opc, 443 TCGReg rd, TCGReg rt, TCGArg sa) 444{ 445 int32_t inst; 446 447 inst = opc; 448 inst |= (rt & 0x1F) << 16; 449 inst |= (rd & 0x1F) << 11; 450 inst |= (sa & 0x1F) << 6; 451 tcg_out32(s, inst); 452 453} 454 455static void tcg_out_opc_sa64(TCGContext *s, MIPSInsn opc1, MIPSInsn opc2, 456 TCGReg rd, TCGReg rt, TCGArg sa) 457{ 458 int32_t inst; 459 460 inst = (sa & 32 ? opc2 : opc1); 461 inst |= (rt & 0x1F) << 16; 462 inst |= (rd & 0x1F) << 11; 463 inst |= (sa & 0x1F) << 6; 464 tcg_out32(s, inst); 465} 466 467/* 468 * Type jump. 469 * Returns true if the branch was in range and the insn was emitted. 470 */ 471static bool tcg_out_opc_jmp(TCGContext *s, MIPSInsn opc, const void *target) 472{ 473 uintptr_t dest = (uintptr_t)target; 474 uintptr_t from = (uintptr_t)tcg_splitwx_to_rx(s->code_ptr) + 4; 475 int32_t inst; 476 477 /* The pc-region branch happens within the 256MB region of 478 the delay slot (thus the +4). */ 479 if ((from ^ dest) & -(1 << 28)) { 480 return false; 481 } 482 tcg_debug_assert((dest & 3) == 0); 483 484 inst = opc; 485 inst |= (dest >> 2) & 0x3ffffff; 486 tcg_out32(s, inst); 487 return true; 488} 489 490static void tcg_out_nop(TCGContext *s) 491{ 492 tcg_out32(s, 0); 493} 494 495static void tcg_out_nop_fill(tcg_insn_unit *p, int count) 496{ 497 memset(p, 0, count * sizeof(tcg_insn_unit)); 498} 499 500static void tcg_out_dsll(TCGContext *s, TCGReg rd, TCGReg rt, TCGArg sa) 501{ 502 tcg_out_opc_sa64(s, OPC_DSLL, OPC_DSLL32, rd, rt, sa); 503} 504 505static void tcg_out_dsrl(TCGContext *s, TCGReg rd, TCGReg rt, TCGArg sa) 506{ 507 tcg_out_opc_sa64(s, OPC_DSRL, OPC_DSRL32, rd, rt, sa); 508} 509 510static void tcg_out_dsra(TCGContext *s, TCGReg rd, TCGReg rt, TCGArg sa) 511{ 512 tcg_out_opc_sa64(s, OPC_DSRA, OPC_DSRA32, rd, rt, sa); 513} 514 515static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg) 516{ 517 /* Simple reg-reg move, optimising out the 'do nothing' case */ 518 if (ret != arg) { 519 tcg_out_opc_reg(s, OPC_OR, ret, arg, TCG_REG_ZERO); 520 } 521 return true; 522} 523 524static bool tcg_out_movi_one(TCGContext *s, TCGReg ret, tcg_target_long arg) 525{ 526 if (arg == (int16_t)arg) { 527 tcg_out_opc_imm(s, OPC_ADDIU, ret, TCG_REG_ZERO, arg); 528 return true; 529 } 530 if (arg == (uint16_t)arg) { 531 tcg_out_opc_imm(s, OPC_ORI, ret, TCG_REG_ZERO, arg); 532 return true; 533 } 534 if (arg == (int32_t)arg && (arg & 0xffff) == 0) { 535 tcg_out_opc_imm(s, OPC_LUI, ret, TCG_REG_ZERO, arg >> 16); 536 return true; 537 } 538 return false; 539} 540 541static bool tcg_out_movi_two(TCGContext *s, TCGReg ret, tcg_target_long arg) 542{ 543 /* 544 * All signed 32-bit constants are loadable with two immediates, 545 * and everything else requires more work. 546 */ 547 if (arg == (int32_t)arg) { 548 if (!tcg_out_movi_one(s, ret, arg)) { 549 tcg_out_opc_imm(s, OPC_LUI, ret, TCG_REG_ZERO, arg >> 16); 550 tcg_out_opc_imm(s, OPC_ORI, ret, ret, arg & 0xffff); 551 } 552 return true; 553 } 554 return false; 555} 556 557static void tcg_out_movi_pool(TCGContext *s, TCGReg ret, 558 tcg_target_long arg, TCGReg tbreg) 559{ 560 new_pool_label(s, arg, R_MIPS_16, s->code_ptr, tcg_tbrel_diff(s, NULL)); 561 tcg_out_opc_imm(s, OPC_LD, ret, tbreg, 0); 562} 563 564static void tcg_out_movi_int(TCGContext *s, TCGType type, TCGReg ret, 565 tcg_target_long arg, TCGReg tbreg) 566{ 567 tcg_target_long tmp; 568 int sh, lo; 569 570 if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I32) { 571 arg = (int32_t)arg; 572 } 573 574 /* Load all 32-bit constants. */ 575 if (tcg_out_movi_two(s, ret, arg)) { 576 return; 577 } 578 assert(TCG_TARGET_REG_BITS == 64); 579 580 /* Load addresses within 2GB of TB with 1 or 3 insns. */ 581 tmp = tcg_tbrel_diff(s, (void *)arg); 582 if (tmp == (int16_t)tmp) { 583 tcg_out_opc_imm(s, OPC_DADDIU, ret, tbreg, tmp); 584 return; 585 } 586 if (tcg_out_movi_two(s, ret, tmp)) { 587 tcg_out_opc_reg(s, OPC_DADDU, ret, ret, tbreg); 588 return; 589 } 590 591 /* 592 * Load bitmasks with a right-shift. This is good for things 593 * like 0x0fff_ffff_ffff_fff0: ADDUI r,0,0xff00 + DSRL r,r,4. 594 * or similarly using LUI. For this to work, bit 31 must be set. 595 */ 596 if (arg > 0 && (int32_t)arg < 0) { 597 sh = clz64(arg); 598 if (tcg_out_movi_one(s, ret, arg << sh)) { 599 tcg_out_dsrl(s, ret, ret, sh); 600 return; 601 } 602 } 603 604 /* 605 * Load slightly larger constants using left-shift. 606 * Limit this sequence to 3 insns to avoid too much expansion. 607 */ 608 sh = ctz64(arg); 609 if (sh && tcg_out_movi_two(s, ret, arg >> sh)) { 610 tcg_out_dsll(s, ret, ret, sh); 611 return; 612 } 613 614 /* 615 * Load slightly larger constants using left-shift and add/or. 616 * Prefer addi with a negative immediate when that would produce 617 * a larger shift. For this to work, bits 15 and 16 must be set. 618 */ 619 lo = arg & 0xffff; 620 if (lo) { 621 if ((arg & 0x18000) == 0x18000) { 622 lo = (int16_t)arg; 623 } 624 tmp = arg - lo; 625 sh = ctz64(tmp); 626 tmp >>= sh; 627 if (tcg_out_movi_one(s, ret, tmp)) { 628 tcg_out_dsll(s, ret, ret, sh); 629 tcg_out_opc_imm(s, lo < 0 ? OPC_DADDIU : OPC_ORI, ret, ret, lo); 630 return; 631 } 632 } 633 634 /* Otherwise, put 64-bit constants into the constant pool. */ 635 tcg_out_movi_pool(s, ret, arg, tbreg); 636} 637 638static void tcg_out_movi(TCGContext *s, TCGType type, 639 TCGReg ret, tcg_target_long arg) 640{ 641 TCGReg tbreg = TCG_TARGET_REG_BITS == 64 ? TCG_REG_TB : 0; 642 tcg_out_movi_int(s, type, ret, arg, tbreg); 643} 644 645static void tcg_out_ext8s(TCGContext *s, TCGType type, TCGReg rd, TCGReg rs) 646{ 647 tcg_debug_assert(use_mips32r2_instructions); 648 tcg_out_opc_reg(s, OPC_SEB, rd, TCG_REG_ZERO, rs); 649} 650 651static void tcg_out_ext8u(TCGContext *s, TCGReg rd, TCGReg rs) 652{ 653 tcg_out_opc_imm(s, OPC_ANDI, rd, rs, 0xff); 654} 655 656static void tcg_out_ext16s(TCGContext *s, TCGType type, TCGReg rd, TCGReg rs) 657{ 658 tcg_debug_assert(use_mips32r2_instructions); 659 tcg_out_opc_reg(s, OPC_SEH, rd, TCG_REG_ZERO, rs); 660} 661 662static void tcg_out_ext16u(TCGContext *s, TCGReg rd, TCGReg rs) 663{ 664 tcg_out_opc_imm(s, OPC_ANDI, rd, rs, 0xffff); 665} 666 667static void tcg_out_ext32s(TCGContext *s, TCGReg rd, TCGReg rs) 668{ 669 tcg_debug_assert(TCG_TARGET_REG_BITS == 64); 670 tcg_out_opc_sa(s, OPC_SLL, rd, rs, 0); 671} 672 673static void tcg_out_exts_i32_i64(TCGContext *s, TCGReg rd, TCGReg rs) 674{ 675 if (rd != rs) { 676 tcg_out_ext32s(s, rd, rs); 677 } 678} 679 680static void tcg_out_extu_i32_i64(TCGContext *s, TCGReg rd, TCGReg rs) 681{ 682 tcg_out_ext32u(s, rd, rs); 683} 684 685static void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg rd, TCGReg rs) 686{ 687 tcg_out_ext32s(s, rd, rs); 688} 689 690static bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2) 691{ 692 return false; 693} 694 695static void tcg_out_addi_ptr(TCGContext *s, TCGReg rd, TCGReg rs, 696 tcg_target_long imm) 697{ 698 /* This function is only used for passing structs by reference. */ 699 g_assert_not_reached(); 700} 701 702static void tcg_out_bswap_subr(TCGContext *s, const tcg_insn_unit *sub) 703{ 704 if (!tcg_out_opc_jmp(s, OPC_JAL, sub)) { 705 tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP1, (uintptr_t)sub); 706 tcg_out_opc_reg(s, OPC_JALR, TCG_REG_RA, TCG_TMP1, 0); 707 } 708} 709 710static void tcg_out_ext32u(TCGContext *s, TCGReg ret, TCGReg arg) 711{ 712 tcg_debug_assert(TCG_TARGET_REG_BITS == 64); 713 if (use_mips32r2_instructions) { 714 tcg_out_opc_bf(s, OPC_DEXT, ret, arg, 31, 0); 715 } else { 716 tcg_out_dsll(s, ret, arg, 32); 717 tcg_out_dsrl(s, ret, ret, 32); 718 } 719} 720 721static void tcg_out_ldst(TCGContext *s, MIPSInsn opc, TCGReg data, 722 TCGReg addr, intptr_t ofs) 723{ 724 int16_t lo = ofs; 725 if (ofs != lo) { 726 tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, ofs - lo); 727 if (addr != TCG_REG_ZERO) { 728 tcg_out_opc_reg(s, ALIAS_PADD, TCG_TMP0, TCG_TMP0, addr); 729 } 730 addr = TCG_TMP0; 731 } 732 tcg_out_opc_imm(s, opc, data, addr, lo); 733} 734 735static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg arg, 736 TCGReg arg1, intptr_t arg2) 737{ 738 MIPSInsn opc = OPC_LD; 739 if (TCG_TARGET_REG_BITS == 32 || type == TCG_TYPE_I32) { 740 opc = OPC_LW; 741 } 742 tcg_out_ldst(s, opc, arg, arg1, arg2); 743} 744 745static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg, 746 TCGReg arg1, intptr_t arg2) 747{ 748 MIPSInsn opc = OPC_SD; 749 if (TCG_TARGET_REG_BITS == 32 || type == TCG_TYPE_I32) { 750 opc = OPC_SW; 751 } 752 tcg_out_ldst(s, opc, arg, arg1, arg2); 753} 754 755static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val, 756 TCGReg base, intptr_t ofs) 757{ 758 if (val == 0) { 759 tcg_out_st(s, type, TCG_REG_ZERO, base, ofs); 760 return true; 761 } 762 return false; 763} 764 765#define SETCOND_INV TCG_TARGET_NB_REGS 766#define SETCOND_NEZ (SETCOND_INV << 1) 767#define SETCOND_FLAGS (SETCOND_INV | SETCOND_NEZ) 768 769static int tcg_out_setcond_int(TCGContext *s, TCGCond cond, TCGReg ret, 770 TCGReg arg1, TCGReg arg2) 771{ 772 int flags = 0; 773 774 switch (cond) { 775 case TCG_COND_EQ: /* -> NE */ 776 case TCG_COND_GE: /* -> LT */ 777 case TCG_COND_GEU: /* -> LTU */ 778 case TCG_COND_LE: /* -> GT */ 779 case TCG_COND_LEU: /* -> GTU */ 780 cond = tcg_invert_cond(cond); 781 flags ^= SETCOND_INV; 782 break; 783 default: 784 break; 785 } 786 787 switch (cond) { 788 case TCG_COND_NE: 789 flags |= SETCOND_NEZ; 790 if (arg2 == 0) { 791 return arg1 | flags; 792 } 793 tcg_out_opc_reg(s, OPC_XOR, ret, arg1, arg2); 794 break; 795 case TCG_COND_LT: 796 tcg_out_opc_reg(s, OPC_SLT, ret, arg1, arg2); 797 break; 798 case TCG_COND_LTU: 799 tcg_out_opc_reg(s, OPC_SLTU, ret, arg1, arg2); 800 break; 801 case TCG_COND_GT: 802 tcg_out_opc_reg(s, OPC_SLT, ret, arg2, arg1); 803 break; 804 case TCG_COND_GTU: 805 tcg_out_opc_reg(s, OPC_SLTU, ret, arg2, arg1); 806 break; 807 default: 808 g_assert_not_reached(); 809 } 810 return ret | flags; 811} 812 813static void tcg_out_setcond_end(TCGContext *s, TCGReg ret, int tmpflags) 814{ 815 if (tmpflags != ret) { 816 TCGReg tmp = tmpflags & ~SETCOND_FLAGS; 817 818 switch (tmpflags & SETCOND_FLAGS) { 819 case SETCOND_INV: 820 /* Intermediate result is boolean: simply invert. */ 821 tcg_out_opc_imm(s, OPC_XORI, ret, tmp, 1); 822 break; 823 case SETCOND_NEZ: 824 /* Intermediate result is zero/non-zero: test != 0. */ 825 tcg_out_opc_reg(s, OPC_SLTU, ret, TCG_REG_ZERO, tmp); 826 break; 827 case SETCOND_NEZ | SETCOND_INV: 828 /* Intermediate result is zero/non-zero: test == 0. */ 829 tcg_out_opc_imm(s, OPC_SLTIU, ret, tmp, 1); 830 break; 831 default: 832 g_assert_not_reached(); 833 } 834 } 835} 836 837static void tgen_setcond(TCGContext *s, TCGType type, TCGCond cond, 838 TCGReg ret, TCGReg arg1, TCGReg arg2) 839{ 840 int tmpflags = tcg_out_setcond_int(s, cond, ret, arg1, arg2); 841 tcg_out_setcond_end(s, ret, tmpflags); 842} 843 844static const TCGOutOpSetcond outop_setcond = { 845 .base.static_constraint = C_O1_I2(r, r, rz), 846 .out_rrr = tgen_setcond, 847}; 848 849static void tgen_negsetcond(TCGContext *s, TCGType type, TCGCond cond, 850 TCGReg ret, TCGReg arg1, TCGReg arg2) 851{ 852 int tmpflags = tcg_out_setcond_int(s, cond, ret, arg1, arg2); 853 TCGReg tmp = tmpflags & ~SETCOND_FLAGS; 854 855 /* If intermediate result is zero/non-zero: test != 0. */ 856 if (tmpflags & SETCOND_NEZ) { 857 tcg_out_opc_reg(s, OPC_SLTU, ret, TCG_REG_ZERO, tmp); 858 tmp = ret; 859 } 860 /* Produce the 0/-1 result. */ 861 if (tmpflags & SETCOND_INV) { 862 tcg_out_opc_imm(s, OPC_ADDIU, ret, tmp, -1); 863 } else { 864 tcg_out_opc_reg(s, OPC_SUBU, ret, TCG_REG_ZERO, tmp); 865 } 866} 867 868static const TCGOutOpSetcond outop_negsetcond = { 869 .base.static_constraint = C_O1_I2(r, r, rz), 870 .out_rrr = tgen_negsetcond, 871}; 872 873static void tgen_brcond(TCGContext *s, TCGType type, TCGCond cond, 874 TCGReg arg1, TCGReg arg2, TCGLabel *l) 875{ 876 static const MIPSInsn b_zero[16] = { 877 [TCG_COND_LT] = OPC_BLTZ, 878 [TCG_COND_GT] = OPC_BGTZ, 879 [TCG_COND_LE] = OPC_BLEZ, 880 [TCG_COND_GE] = OPC_BGEZ, 881 }; 882 883 MIPSInsn b_opc = 0; 884 885 switch (cond) { 886 case TCG_COND_EQ: 887 b_opc = OPC_BEQ; 888 break; 889 case TCG_COND_NE: 890 b_opc = OPC_BNE; 891 break; 892 case TCG_COND_LT: 893 case TCG_COND_GT: 894 case TCG_COND_LE: 895 case TCG_COND_GE: 896 if (arg2 == 0) { 897 b_opc = b_zero[cond]; 898 arg2 = arg1; 899 arg1 = 0; 900 } 901 break; 902 default: 903 break; 904 } 905 906 if (b_opc == 0) { 907 int tmpflags = tcg_out_setcond_int(s, cond, TCG_TMP0, arg1, arg2); 908 909 arg2 = TCG_REG_ZERO; 910 arg1 = tmpflags & ~SETCOND_FLAGS; 911 b_opc = tmpflags & SETCOND_INV ? OPC_BEQ : OPC_BNE; 912 } 913 914 tcg_out_reloc(s, s->code_ptr, R_MIPS_PC16, l, 0); 915 tcg_out_opc_br(s, b_opc, arg1, arg2); 916 tcg_out_nop(s); 917} 918 919static const TCGOutOpBrcond outop_brcond = { 920 .base.static_constraint = C_O0_I2(r, rz), 921 .out_rr = tgen_brcond, 922}; 923 924static int tcg_out_setcond2_int(TCGContext *s, TCGCond cond, TCGReg ret, 925 TCGReg al, TCGReg ah, TCGReg bl, TCGReg bh) 926{ 927 int flags = 0; 928 929 switch (cond) { 930 case TCG_COND_EQ: 931 flags |= SETCOND_INV; 932 /* fall through */ 933 case TCG_COND_NE: 934 flags |= SETCOND_NEZ; 935 tcg_out_opc_reg(s, OPC_XOR, TCG_TMP0, al, bl); 936 tcg_out_opc_reg(s, OPC_XOR, TCG_TMP1, ah, bh); 937 tcg_out_opc_reg(s, OPC_OR, ret, TCG_TMP0, TCG_TMP1); 938 break; 939 940 default: 941 tgen_setcond(s, TCG_TYPE_I32, TCG_COND_EQ, TCG_TMP0, ah, bh); 942 tgen_setcond(s, TCG_TYPE_I32, tcg_unsigned_cond(cond), 943 TCG_TMP1, al, bl); 944 tcg_out_opc_reg(s, OPC_AND, TCG_TMP1, TCG_TMP1, TCG_TMP0); 945 tgen_setcond(s, TCG_TYPE_I32, tcg_high_cond(cond), TCG_TMP0, ah, bh); 946 tcg_out_opc_reg(s, OPC_OR, ret, TCG_TMP0, TCG_TMP1); 947 break; 948 } 949 return ret | flags; 950} 951 952static void tgen_setcond2(TCGContext *s, TCGCond cond, TCGReg ret, 953 TCGReg al, TCGReg ah, 954 TCGArg bl, bool const_bl, 955 TCGArg bh, bool const_bh) 956{ 957 int tmpflags = tcg_out_setcond2_int(s, cond, ret, al, ah, bl, bh); 958 tcg_out_setcond_end(s, ret, tmpflags); 959} 960 961#if TCG_TARGET_REG_BITS != 32 962__attribute__((unused)) 963#endif 964static const TCGOutOpSetcond2 outop_setcond2 = { 965 .base.static_constraint = C_O1_I4(r, r, r, rz, rz), 966 .out = tgen_setcond2, 967}; 968 969static void tgen_brcond2(TCGContext *s, TCGCond cond, TCGReg al, TCGReg ah, 970 TCGArg bl, bool const_bl, 971 TCGArg bh, bool const_bh, TCGLabel *l) 972{ 973 int tmpflags = tcg_out_setcond2_int(s, cond, TCG_TMP0, al, ah, bl, bh); 974 TCGReg tmp = tmpflags & ~SETCOND_FLAGS; 975 MIPSInsn b_opc = tmpflags & SETCOND_INV ? OPC_BEQ : OPC_BNE; 976 977 tcg_out_reloc(s, s->code_ptr, R_MIPS_PC16, l, 0); 978 tcg_out_opc_br(s, b_opc, tmp, TCG_REG_ZERO); 979 tcg_out_nop(s); 980} 981 982#if TCG_TARGET_REG_BITS != 32 983__attribute__((unused)) 984#endif 985static const TCGOutOpBrcond2 outop_brcond2 = { 986 .base.static_constraint = C_O0_I4(r, r, rz, rz), 987 .out = tgen_brcond2, 988}; 989 990static void tgen_movcond(TCGContext *s, TCGType type, TCGCond cond, 991 TCGReg ret, TCGReg c1, TCGArg c2, bool const_c2, 992 TCGArg v1, bool const_v1, TCGArg v2, bool const_v2) 993{ 994 int tmpflags; 995 bool eqz; 996 997 /* If one of the values is zero, put it last to match SEL*Z instructions */ 998 if (use_mips32r6_instructions && v1 == 0) { 999 v1 = v2; 1000 v2 = 0; 1001 cond = tcg_invert_cond(cond); 1002 } 1003 1004 tmpflags = tcg_out_setcond_int(s, cond, TCG_TMP0, c1, c2); 1005 c1 = tmpflags & ~SETCOND_FLAGS; 1006 eqz = tmpflags & SETCOND_INV; 1007 1008 if (use_mips32r6_instructions) { 1009 MIPSInsn m_opc_t = eqz ? OPC_SELEQZ : OPC_SELNEZ; 1010 MIPSInsn m_opc_f = eqz ? OPC_SELNEZ : OPC_SELEQZ; 1011 1012 if (v2 != 0) { 1013 tcg_out_opc_reg(s, m_opc_f, TCG_TMP1, v2, c1); 1014 } 1015 tcg_out_opc_reg(s, m_opc_t, ret, v1, c1); 1016 if (v2 != 0) { 1017 tcg_out_opc_reg(s, OPC_OR, ret, ret, TCG_TMP1); 1018 } 1019 return; 1020 } 1021 1022 /* This should be guaranteed via constraints */ 1023 tcg_debug_assert(v2 == ret); 1024 1025 if (use_movnz_instructions) { 1026 MIPSInsn m_opc = eqz ? OPC_MOVZ : OPC_MOVN; 1027 tcg_out_opc_reg(s, m_opc, ret, v1, c1); 1028 } else { 1029 /* Invert the condition in order to branch over the move. */ 1030 MIPSInsn b_opc = eqz ? OPC_BNE : OPC_BEQ; 1031 tcg_out_opc_imm(s, b_opc, c1, TCG_REG_ZERO, 2); 1032 tcg_out_nop(s); 1033 /* Open-code tcg_out_mov, without the nop-move check. */ 1034 tcg_out_opc_reg(s, OPC_OR, ret, v1, TCG_REG_ZERO); 1035 } 1036} 1037 1038static const TCGOutOpMovcond outop_movcond = { 1039 .base.static_constraint = (use_mips32r6_instructions 1040 ? C_O1_I4(r, r, rz, rz, rz) 1041 : C_O1_I4(r, r, rz, rz, 0)), 1042 .out = tgen_movcond, 1043}; 1044 1045static void tcg_out_call_int(TCGContext *s, const tcg_insn_unit *arg, bool tail) 1046{ 1047 /* 1048 * Note that __mips_abicalls requires the called function's address 1049 * to be loaded into $25 (t9), even if a direct branch is in range. 1050 * 1051 * For n64, always drop the pointer into the constant pool. 1052 * We can re-use helper addresses often and do not want any 1053 * of the longer sequences tcg_out_movi may try. 1054 */ 1055 if (sizeof(uintptr_t) == 8) { 1056 tcg_out_movi_pool(s, TCG_REG_T9, (uintptr_t)arg, TCG_REG_TB); 1057 } else { 1058 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_T9, (uintptr_t)arg); 1059 } 1060 1061 /* But do try a direct branch, allowing the cpu better insn prefetch. */ 1062 if (tail) { 1063 if (!tcg_out_opc_jmp(s, OPC_J, arg)) { 1064 tcg_out_opc_reg(s, OPC_JR, 0, TCG_REG_T9, 0); 1065 } 1066 } else { 1067 if (!tcg_out_opc_jmp(s, OPC_JAL, arg)) { 1068 tcg_out_opc_reg(s, OPC_JALR, TCG_REG_RA, TCG_REG_T9, 0); 1069 } 1070 } 1071} 1072 1073static void tcg_out_call(TCGContext *s, const tcg_insn_unit *arg, 1074 const TCGHelperInfo *info) 1075{ 1076 tcg_out_call_int(s, arg, false); 1077 tcg_out_nop(s); 1078} 1079 1080/* We have four temps, we might as well expose three of them. */ 1081static const TCGLdstHelperParam ldst_helper_param = { 1082 .ntmp = 3, .tmp = { TCG_TMP0, TCG_TMP1, TCG_TMP2 } 1083}; 1084 1085static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l) 1086{ 1087 const tcg_insn_unit *tgt_rx = tcg_splitwx_to_rx(s->code_ptr); 1088 MemOp opc = get_memop(l->oi); 1089 1090 /* resolve label address */ 1091 if (!reloc_pc16(l->label_ptr[0], tgt_rx) 1092 || (l->label_ptr[1] && !reloc_pc16(l->label_ptr[1], tgt_rx))) { 1093 return false; 1094 } 1095 1096 tcg_out_ld_helper_args(s, l, &ldst_helper_param); 1097 1098 tcg_out_call_int(s, qemu_ld_helpers[opc & MO_SSIZE], false); 1099 /* delay slot */ 1100 tcg_out_nop(s); 1101 1102 tcg_out_ld_helper_ret(s, l, true, &ldst_helper_param); 1103 1104 tcg_out_opc_br(s, OPC_BEQ, TCG_REG_ZERO, TCG_REG_ZERO); 1105 if (!reloc_pc16(s->code_ptr - 1, l->raddr)) { 1106 return false; 1107 } 1108 1109 /* delay slot */ 1110 tcg_out_nop(s); 1111 return true; 1112} 1113 1114static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l) 1115{ 1116 const tcg_insn_unit *tgt_rx = tcg_splitwx_to_rx(s->code_ptr); 1117 MemOp opc = get_memop(l->oi); 1118 1119 /* resolve label address */ 1120 if (!reloc_pc16(l->label_ptr[0], tgt_rx) 1121 || (l->label_ptr[1] && !reloc_pc16(l->label_ptr[1], tgt_rx))) { 1122 return false; 1123 } 1124 1125 tcg_out_st_helper_args(s, l, &ldst_helper_param); 1126 1127 tcg_out_call_int(s, qemu_st_helpers[opc & MO_SIZE], false); 1128 /* delay slot */ 1129 tcg_out_nop(s); 1130 1131 tcg_out_opc_br(s, OPC_BEQ, TCG_REG_ZERO, TCG_REG_ZERO); 1132 if (!reloc_pc16(s->code_ptr - 1, l->raddr)) { 1133 return false; 1134 } 1135 1136 /* delay slot */ 1137 tcg_out_nop(s); 1138 return true; 1139} 1140 1141typedef struct { 1142 TCGReg base; 1143 TCGAtomAlign aa; 1144} HostAddress; 1145 1146bool tcg_target_has_memory_bswap(MemOp memop) 1147{ 1148 return false; 1149} 1150 1151/* We expect to use a 16-bit negative offset from ENV. */ 1152#define MIN_TLB_MASK_TABLE_OFS -32768 1153 1154/* 1155 * For system-mode, perform the TLB load and compare. 1156 * For user-mode, perform any required alignment tests. 1157 * In both cases, return a TCGLabelQemuLdst structure if the slow path 1158 * is required and fill in @h with the host address for the fast path. 1159 */ 1160static TCGLabelQemuLdst *prepare_host_addr(TCGContext *s, HostAddress *h, 1161 TCGReg addr, MemOpIdx oi, bool is_ld) 1162{ 1163 TCGType addr_type = s->addr_type; 1164 TCGLabelQemuLdst *ldst = NULL; 1165 MemOp opc = get_memop(oi); 1166 MemOp a_bits; 1167 unsigned s_bits = opc & MO_SIZE; 1168 unsigned a_mask; 1169 TCGReg base; 1170 1171 h->aa = atom_and_align_for_opc(s, opc, MO_ATOM_IFALIGN, false); 1172 a_bits = h->aa.align; 1173 a_mask = (1 << a_bits) - 1; 1174 1175 if (tcg_use_softmmu) { 1176 unsigned s_mask = (1 << s_bits) - 1; 1177 int mem_index = get_mmuidx(oi); 1178 int fast_off = tlb_mask_table_ofs(s, mem_index); 1179 int mask_off = fast_off + offsetof(CPUTLBDescFast, mask); 1180 int table_off = fast_off + offsetof(CPUTLBDescFast, table); 1181 int add_off = offsetof(CPUTLBEntry, addend); 1182 int cmp_off = is_ld ? offsetof(CPUTLBEntry, addr_read) 1183 : offsetof(CPUTLBEntry, addr_write); 1184 1185 ldst = new_ldst_label(s); 1186 ldst->is_ld = is_ld; 1187 ldst->oi = oi; 1188 ldst->addr_reg = addr; 1189 1190 /* Load tlb_mask[mmu_idx] and tlb_table[mmu_idx]. */ 1191 tcg_out_ld(s, TCG_TYPE_PTR, TCG_TMP0, TCG_AREG0, mask_off); 1192 tcg_out_ld(s, TCG_TYPE_PTR, TCG_TMP1, TCG_AREG0, table_off); 1193 1194 /* Extract the TLB index from the address into TMP3. */ 1195 if (TCG_TARGET_REG_BITS == 32 || addr_type == TCG_TYPE_I32) { 1196 tcg_out_opc_sa(s, OPC_SRL, TCG_TMP3, addr, 1197 s->page_bits - CPU_TLB_ENTRY_BITS); 1198 } else { 1199 tcg_out_dsrl(s, TCG_TMP3, addr, s->page_bits - CPU_TLB_ENTRY_BITS); 1200 } 1201 tcg_out_opc_reg(s, OPC_AND, TCG_TMP3, TCG_TMP3, TCG_TMP0); 1202 1203 /* Add the tlb_table pointer, creating the CPUTLBEntry address. */ 1204 tcg_out_opc_reg(s, ALIAS_PADD, TCG_TMP3, TCG_TMP3, TCG_TMP1); 1205 1206 /* Load the tlb comparator. */ 1207 if (TCG_TARGET_REG_BITS == 64 && addr_type == TCG_TYPE_I32) { 1208 tcg_out_ld(s, TCG_TYPE_I32, TCG_TMP0, TCG_TMP3, 1209 cmp_off + HOST_BIG_ENDIAN * 4); 1210 } else { 1211 tcg_out_ld(s, TCG_TYPE_REG, TCG_TMP0, TCG_TMP3, cmp_off); 1212 } 1213 1214 /* Load the tlb addend for the fast path. */ 1215 tcg_out_ld(s, TCG_TYPE_PTR, TCG_TMP3, TCG_TMP3, add_off); 1216 1217 /* 1218 * Mask the page bits, keeping the alignment bits to compare against. 1219 * For unaligned accesses, compare against the end of the access to 1220 * verify that it does not cross a page boundary. 1221 */ 1222 tcg_out_movi(s, addr_type, TCG_TMP1, s->page_mask | a_mask); 1223 if (a_mask < s_mask) { 1224 tcg_out_opc_imm(s, (TCG_TARGET_REG_BITS == 32 1225 || addr_type == TCG_TYPE_I32 1226 ? OPC_ADDIU : OPC_DADDIU), 1227 TCG_TMP2, addr, s_mask - a_mask); 1228 tcg_out_opc_reg(s, OPC_AND, TCG_TMP1, TCG_TMP1, TCG_TMP2); 1229 } else { 1230 tcg_out_opc_reg(s, OPC_AND, TCG_TMP1, TCG_TMP1, addr); 1231 } 1232 1233 /* Zero extend a 32-bit guest address for a 64-bit host. */ 1234 if (TCG_TARGET_REG_BITS == 64 && addr_type == TCG_TYPE_I32) { 1235 tcg_out_ext32u(s, TCG_TMP2, addr); 1236 addr = TCG_TMP2; 1237 } 1238 1239 ldst->label_ptr[0] = s->code_ptr; 1240 tcg_out_opc_br(s, OPC_BNE, TCG_TMP1, TCG_TMP0); 1241 1242 /* delay slot */ 1243 base = TCG_TMP3; 1244 tcg_out_opc_reg(s, ALIAS_PADD, base, TCG_TMP3, addr); 1245 } else { 1246 if (a_mask && (use_mips32r6_instructions || a_bits != s_bits)) { 1247 ldst = new_ldst_label(s); 1248 1249 ldst->is_ld = is_ld; 1250 ldst->oi = oi; 1251 ldst->addr_reg = addr; 1252 1253 /* We are expecting a_bits to max out at 7, much lower than ANDI. */ 1254 tcg_debug_assert(a_bits < 16); 1255 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP0, addr, a_mask); 1256 1257 ldst->label_ptr[0] = s->code_ptr; 1258 if (use_mips32r6_instructions) { 1259 tcg_out_opc_br(s, OPC_BNEZALC_R6, TCG_REG_ZERO, TCG_TMP0); 1260 } else { 1261 tcg_out_opc_br(s, OPC_BNEL, TCG_TMP0, TCG_REG_ZERO); 1262 tcg_out_nop(s); 1263 } 1264 } 1265 1266 base = addr; 1267 if (TCG_TARGET_REG_BITS == 64 && addr_type == TCG_TYPE_I32) { 1268 tcg_out_ext32u(s, TCG_REG_A0, base); 1269 base = TCG_REG_A0; 1270 } 1271 if (guest_base) { 1272 if (guest_base == (int16_t)guest_base) { 1273 tcg_out_opc_imm(s, ALIAS_PADDI, TCG_REG_A0, base, guest_base); 1274 } else { 1275 tcg_out_opc_reg(s, ALIAS_PADD, TCG_REG_A0, base, 1276 TCG_GUEST_BASE_REG); 1277 } 1278 base = TCG_REG_A0; 1279 } 1280 } 1281 1282 h->base = base; 1283 return ldst; 1284} 1285 1286static void tcg_out_qemu_ld_direct(TCGContext *s, TCGReg lo, TCGReg hi, 1287 TCGReg base, MemOp opc, TCGType type) 1288{ 1289 switch (opc & MO_SSIZE) { 1290 case MO_UB: 1291 tcg_out_opc_imm(s, OPC_LBU, lo, base, 0); 1292 break; 1293 case MO_SB: 1294 tcg_out_opc_imm(s, OPC_LB, lo, base, 0); 1295 break; 1296 case MO_UW: 1297 tcg_out_opc_imm(s, OPC_LHU, lo, base, 0); 1298 break; 1299 case MO_SW: 1300 tcg_out_opc_imm(s, OPC_LH, lo, base, 0); 1301 break; 1302 case MO_UL: 1303 if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I64) { 1304 tcg_out_opc_imm(s, OPC_LWU, lo, base, 0); 1305 break; 1306 } 1307 /* FALLTHRU */ 1308 case MO_SL: 1309 tcg_out_opc_imm(s, OPC_LW, lo, base, 0); 1310 break; 1311 case MO_UQ: 1312 /* Prefer to load from offset 0 first, but allow for overlap. */ 1313 if (TCG_TARGET_REG_BITS == 64) { 1314 tcg_out_opc_imm(s, OPC_LD, lo, base, 0); 1315 } else if (HOST_BIG_ENDIAN ? hi != base : lo == base) { 1316 tcg_out_opc_imm(s, OPC_LW, hi, base, HI_OFF); 1317 tcg_out_opc_imm(s, OPC_LW, lo, base, LO_OFF); 1318 } else { 1319 tcg_out_opc_imm(s, OPC_LW, lo, base, LO_OFF); 1320 tcg_out_opc_imm(s, OPC_LW, hi, base, HI_OFF); 1321 } 1322 break; 1323 default: 1324 g_assert_not_reached(); 1325 } 1326} 1327 1328static void tcg_out_qemu_ld_unalign(TCGContext *s, TCGReg lo, TCGReg hi, 1329 TCGReg base, MemOp opc, TCGType type) 1330{ 1331 const MIPSInsn lw1 = HOST_BIG_ENDIAN ? OPC_LWL : OPC_LWR; 1332 const MIPSInsn lw2 = HOST_BIG_ENDIAN ? OPC_LWR : OPC_LWL; 1333 const MIPSInsn ld1 = HOST_BIG_ENDIAN ? OPC_LDL : OPC_LDR; 1334 const MIPSInsn ld2 = HOST_BIG_ENDIAN ? OPC_LDR : OPC_LDL; 1335 bool sgn = opc & MO_SIGN; 1336 1337 switch (opc & MO_SIZE) { 1338 case MO_16: 1339 if (HOST_BIG_ENDIAN) { 1340 tcg_out_opc_imm(s, sgn ? OPC_LB : OPC_LBU, TCG_TMP0, base, 0); 1341 tcg_out_opc_imm(s, OPC_LBU, lo, base, 1); 1342 if (use_mips32r2_instructions) { 1343 tcg_out_opc_bf(s, OPC_INS, lo, TCG_TMP0, 31, 8); 1344 } else { 1345 tcg_out_opc_sa(s, OPC_SLL, TCG_TMP0, TCG_TMP0, 8); 1346 tcg_out_opc_reg(s, OPC_OR, lo, lo, TCG_TMP0); 1347 } 1348 } else if (use_mips32r2_instructions && lo != base) { 1349 tcg_out_opc_imm(s, OPC_LBU, lo, base, 0); 1350 tcg_out_opc_imm(s, sgn ? OPC_LB : OPC_LBU, TCG_TMP0, base, 1); 1351 tcg_out_opc_bf(s, OPC_INS, lo, TCG_TMP0, 31, 8); 1352 } else { 1353 tcg_out_opc_imm(s, OPC_LBU, TCG_TMP0, base, 0); 1354 tcg_out_opc_imm(s, sgn ? OPC_LB : OPC_LBU, TCG_TMP1, base, 1); 1355 tcg_out_opc_sa(s, OPC_SLL, TCG_TMP1, TCG_TMP1, 8); 1356 tcg_out_opc_reg(s, OPC_OR, lo, TCG_TMP0, TCG_TMP1); 1357 } 1358 break; 1359 1360 case MO_32: 1361 tcg_out_opc_imm(s, lw1, lo, base, 0); 1362 tcg_out_opc_imm(s, lw2, lo, base, 3); 1363 if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I64 && !sgn) { 1364 tcg_out_ext32u(s, lo, lo); 1365 } 1366 break; 1367 1368 case MO_64: 1369 if (TCG_TARGET_REG_BITS == 64) { 1370 tcg_out_opc_imm(s, ld1, lo, base, 0); 1371 tcg_out_opc_imm(s, ld2, lo, base, 7); 1372 } else { 1373 tcg_out_opc_imm(s, lw1, HOST_BIG_ENDIAN ? hi : lo, base, 0 + 0); 1374 tcg_out_opc_imm(s, lw2, HOST_BIG_ENDIAN ? hi : lo, base, 0 + 3); 1375 tcg_out_opc_imm(s, lw1, HOST_BIG_ENDIAN ? lo : hi, base, 4 + 0); 1376 tcg_out_opc_imm(s, lw2, HOST_BIG_ENDIAN ? lo : hi, base, 4 + 3); 1377 } 1378 break; 1379 1380 default: 1381 g_assert_not_reached(); 1382 } 1383} 1384 1385static void tcg_out_qemu_ld(TCGContext *s, TCGReg datalo, TCGReg datahi, 1386 TCGReg addr, MemOpIdx oi, TCGType data_type) 1387{ 1388 MemOp opc = get_memop(oi); 1389 TCGLabelQemuLdst *ldst; 1390 HostAddress h; 1391 1392 ldst = prepare_host_addr(s, &h, addr, oi, true); 1393 1394 if (use_mips32r6_instructions || h.aa.align >= (opc & MO_SIZE)) { 1395 tcg_out_qemu_ld_direct(s, datalo, datahi, h.base, opc, data_type); 1396 } else { 1397 tcg_out_qemu_ld_unalign(s, datalo, datahi, h.base, opc, data_type); 1398 } 1399 1400 if (ldst) { 1401 ldst->type = data_type; 1402 ldst->datalo_reg = datalo; 1403 ldst->datahi_reg = datahi; 1404 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr); 1405 } 1406} 1407 1408static void tcg_out_qemu_st_direct(TCGContext *s, TCGReg lo, TCGReg hi, 1409 TCGReg base, MemOp opc) 1410{ 1411 switch (opc & MO_SIZE) { 1412 case MO_8: 1413 tcg_out_opc_imm(s, OPC_SB, lo, base, 0); 1414 break; 1415 case MO_16: 1416 tcg_out_opc_imm(s, OPC_SH, lo, base, 0); 1417 break; 1418 case MO_32: 1419 tcg_out_opc_imm(s, OPC_SW, lo, base, 0); 1420 break; 1421 case MO_64: 1422 if (TCG_TARGET_REG_BITS == 64) { 1423 tcg_out_opc_imm(s, OPC_SD, lo, base, 0); 1424 } else { 1425 tcg_out_opc_imm(s, OPC_SW, HOST_BIG_ENDIAN ? hi : lo, base, 0); 1426 tcg_out_opc_imm(s, OPC_SW, HOST_BIG_ENDIAN ? lo : hi, base, 4); 1427 } 1428 break; 1429 default: 1430 g_assert_not_reached(); 1431 } 1432} 1433 1434static void tcg_out_qemu_st_unalign(TCGContext *s, TCGReg lo, TCGReg hi, 1435 TCGReg base, MemOp opc) 1436{ 1437 const MIPSInsn sw1 = HOST_BIG_ENDIAN ? OPC_SWL : OPC_SWR; 1438 const MIPSInsn sw2 = HOST_BIG_ENDIAN ? OPC_SWR : OPC_SWL; 1439 const MIPSInsn sd1 = HOST_BIG_ENDIAN ? OPC_SDL : OPC_SDR; 1440 const MIPSInsn sd2 = HOST_BIG_ENDIAN ? OPC_SDR : OPC_SDL; 1441 1442 switch (opc & MO_SIZE) { 1443 case MO_16: 1444 tcg_out_opc_sa(s, OPC_SRL, TCG_TMP0, lo, 8); 1445 tcg_out_opc_imm(s, OPC_SB, HOST_BIG_ENDIAN ? TCG_TMP0 : lo, base, 0); 1446 tcg_out_opc_imm(s, OPC_SB, HOST_BIG_ENDIAN ? lo : TCG_TMP0, base, 1); 1447 break; 1448 1449 case MO_32: 1450 tcg_out_opc_imm(s, sw1, lo, base, 0); 1451 tcg_out_opc_imm(s, sw2, lo, base, 3); 1452 break; 1453 1454 case MO_64: 1455 if (TCG_TARGET_REG_BITS == 64) { 1456 tcg_out_opc_imm(s, sd1, lo, base, 0); 1457 tcg_out_opc_imm(s, sd2, lo, base, 7); 1458 } else { 1459 tcg_out_opc_imm(s, sw1, HOST_BIG_ENDIAN ? hi : lo, base, 0 + 0); 1460 tcg_out_opc_imm(s, sw2, HOST_BIG_ENDIAN ? hi : lo, base, 0 + 3); 1461 tcg_out_opc_imm(s, sw1, HOST_BIG_ENDIAN ? lo : hi, base, 4 + 0); 1462 tcg_out_opc_imm(s, sw2, HOST_BIG_ENDIAN ? lo : hi, base, 4 + 3); 1463 } 1464 break; 1465 1466 default: 1467 g_assert_not_reached(); 1468 } 1469} 1470 1471static void tcg_out_qemu_st(TCGContext *s, TCGReg datalo, TCGReg datahi, 1472 TCGReg addr, MemOpIdx oi, TCGType data_type) 1473{ 1474 MemOp opc = get_memop(oi); 1475 TCGLabelQemuLdst *ldst; 1476 HostAddress h; 1477 1478 ldst = prepare_host_addr(s, &h, addr, oi, false); 1479 1480 if (use_mips32r6_instructions || h.aa.align >= (opc & MO_SIZE)) { 1481 tcg_out_qemu_st_direct(s, datalo, datahi, h.base, opc); 1482 } else { 1483 tcg_out_qemu_st_unalign(s, datalo, datahi, h.base, opc); 1484 } 1485 1486 if (ldst) { 1487 ldst->type = data_type; 1488 ldst->datalo_reg = datalo; 1489 ldst->datahi_reg = datahi; 1490 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr); 1491 } 1492} 1493 1494static void tcg_out_mb(TCGContext *s, TCGArg a0) 1495{ 1496 static const MIPSInsn sync[] = { 1497 /* Note that SYNC_MB is a slightly weaker than SYNC 0, 1498 as the former is an ordering barrier and the latter 1499 is a completion barrier. */ 1500 [0 ... TCG_MO_ALL] = OPC_SYNC_MB, 1501 [TCG_MO_LD_LD] = OPC_SYNC_RMB, 1502 [TCG_MO_ST_ST] = OPC_SYNC_WMB, 1503 [TCG_MO_LD_ST] = OPC_SYNC_RELEASE, 1504 [TCG_MO_LD_ST | TCG_MO_ST_ST] = OPC_SYNC_RELEASE, 1505 [TCG_MO_LD_ST | TCG_MO_LD_LD] = OPC_SYNC_ACQUIRE, 1506 }; 1507 tcg_out32(s, sync[a0 & TCG_MO_ALL]); 1508} 1509 1510static void tcg_out_exit_tb(TCGContext *s, uintptr_t a0) 1511{ 1512 TCGReg base = TCG_REG_ZERO; 1513 int16_t lo = 0; 1514 1515 if (a0) { 1516 intptr_t ofs; 1517 if (TCG_TARGET_REG_BITS == 64) { 1518 ofs = tcg_tbrel_diff(s, (void *)a0); 1519 lo = ofs; 1520 if (ofs == lo) { 1521 base = TCG_REG_TB; 1522 } else { 1523 base = TCG_REG_V0; 1524 tcg_out_movi(s, TCG_TYPE_PTR, base, ofs - lo); 1525 tcg_out_opc_reg(s, ALIAS_PADD, base, base, TCG_REG_TB); 1526 } 1527 } else { 1528 ofs = a0; 1529 lo = ofs; 1530 base = TCG_REG_V0; 1531 tcg_out_movi(s, TCG_TYPE_PTR, base, ofs - lo); 1532 } 1533 } 1534 if (!tcg_out_opc_jmp(s, OPC_J, tb_ret_addr)) { 1535 tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, (uintptr_t)tb_ret_addr); 1536 tcg_out_opc_reg(s, OPC_JR, 0, TCG_TMP0, 0); 1537 } 1538 /* delay slot */ 1539 tcg_out_opc_imm(s, ALIAS_PADDI, TCG_REG_V0, base, lo); 1540} 1541 1542static void tcg_out_goto_tb(TCGContext *s, int which) 1543{ 1544 intptr_t ofs = get_jmp_target_addr(s, which); 1545 TCGReg base, dest; 1546 1547 /* indirect jump method */ 1548 if (TCG_TARGET_REG_BITS == 64) { 1549 dest = TCG_REG_TB; 1550 base = TCG_REG_TB; 1551 ofs = tcg_tbrel_diff(s, (void *)ofs); 1552 } else { 1553 dest = TCG_TMP0; 1554 base = TCG_REG_ZERO; 1555 } 1556 tcg_out_ld(s, TCG_TYPE_PTR, dest, base, ofs); 1557 tcg_out_opc_reg(s, OPC_JR, 0, dest, 0); 1558 /* delay slot */ 1559 tcg_out_nop(s); 1560 1561 set_jmp_reset_offset(s, which); 1562 if (TCG_TARGET_REG_BITS == 64) { 1563 /* For the unlinked case, need to reset TCG_REG_TB. */ 1564 tcg_out_ldst(s, ALIAS_PADDI, TCG_REG_TB, TCG_REG_TB, 1565 -tcg_current_code_size(s)); 1566 } 1567} 1568 1569void tb_target_set_jmp_target(const TranslationBlock *tb, int n, 1570 uintptr_t jmp_rx, uintptr_t jmp_rw) 1571{ 1572 /* Always indirect, nothing to do */ 1573} 1574 1575 1576static void tgen_add(TCGContext *s, TCGType type, 1577 TCGReg a0, TCGReg a1, TCGReg a2) 1578{ 1579 MIPSInsn insn = type == TCG_TYPE_I32 ? OPC_ADDU : OPC_DADDU; 1580 tcg_out_opc_reg(s, insn, a0, a1, a2); 1581} 1582 1583static void tgen_addi(TCGContext *s, TCGType type, 1584 TCGReg a0, TCGReg a1, tcg_target_long a2) 1585{ 1586 MIPSInsn insn = type == TCG_TYPE_I32 ? OPC_ADDIU : OPC_DADDIU; 1587 tcg_out_opc_imm(s, insn, a0, a1, a2); 1588} 1589 1590static const TCGOutOpBinary outop_add = { 1591 .base.static_constraint = C_O1_I2(r, r, rJ), 1592 .out_rrr = tgen_add, 1593 .out_rri = tgen_addi, 1594}; 1595 1596static void tgen_and(TCGContext *s, TCGType type, 1597 TCGReg a0, TCGReg a1, TCGReg a2) 1598{ 1599 tcg_out_opc_reg(s, OPC_AND, a0, a1, a2); 1600} 1601 1602static void tgen_andi(TCGContext *s, TCGType type, 1603 TCGReg a0, TCGReg a1, tcg_target_long a2) 1604{ 1605 int msb; 1606 1607 if (a2 == (uint16_t)a2) { 1608 tcg_out_opc_imm(s, OPC_ANDI, a0, a1, a2); 1609 return; 1610 } 1611 1612 tcg_debug_assert(use_mips32r2_instructions); 1613 tcg_debug_assert(is_p2m1(a2)); 1614 msb = ctz64(~a2) - 1; 1615 if (type == TCG_TYPE_I32) { 1616 tcg_out_opc_bf(s, OPC_EXT, a0, a1, msb, 0); 1617 } else { 1618 tcg_out_opc_bf64(s, OPC_DEXT, OPC_DEXTM, OPC_DEXTU, a0, a1, msb, 0); 1619 } 1620} 1621 1622static const TCGOutOpBinary outop_and = { 1623 .base.static_constraint = C_O1_I2(r, r, rIK), 1624 .out_rrr = tgen_and, 1625 .out_rri = tgen_andi, 1626}; 1627 1628static const TCGOutOpBinary outop_andc = { 1629 .base.static_constraint = C_NotImplemented, 1630}; 1631 1632static void tgen_clz(TCGContext *s, TCGType type, 1633 TCGReg a0, TCGReg a1, TCGReg a2) 1634{ 1635 if (use_mips32r6_instructions) { 1636 MIPSInsn opcv6 = type == TCG_TYPE_I32 ? OPC_CLZ_R6 : OPC_DCLZ_R6; 1637 tcg_out_opc_reg(s, opcv6, TCG_TMP0, a1, 0); 1638 tgen_movcond(s, TCG_TYPE_REG, TCG_COND_EQ, a0, a1, a2, false, 1639 TCG_TMP0, false, TCG_REG_ZERO, false); 1640 } else { 1641 MIPSInsn opcv2 = type == TCG_TYPE_I32 ? OPC_CLZ : OPC_DCLZ; 1642 if (a0 == a2) { 1643 tcg_out_opc_reg(s, opcv2, TCG_TMP0, a1, a1); 1644 tcg_out_opc_reg(s, OPC_MOVN, a0, TCG_TMP0, a1); 1645 } else if (a0 != a1) { 1646 tcg_out_opc_reg(s, opcv2, a0, a1, a1); 1647 tcg_out_opc_reg(s, OPC_MOVZ, a0, a2, a1); 1648 } else { 1649 tcg_out_opc_reg(s, opcv2, TCG_TMP0, a1, a1); 1650 tcg_out_opc_reg(s, OPC_MOVZ, TCG_TMP0, a2, a1); 1651 tcg_out_mov(s, type, a0, TCG_TMP0); 1652 } 1653 } 1654} 1655 1656static void tgen_clzi(TCGContext *s, TCGType type, 1657 TCGReg a0, TCGReg a1, tcg_target_long a2) 1658{ 1659 if (a2 == 0) { 1660 tgen_clz(s, type, a0, a1, TCG_REG_ZERO); 1661 } else if (use_mips32r6_instructions) { 1662 MIPSInsn opcv6 = type == TCG_TYPE_I32 ? OPC_CLZ_R6 : OPC_DCLZ_R6; 1663 tcg_out_opc_reg(s, opcv6, a0, a1, 0); 1664 } else { 1665 MIPSInsn opcv2 = type == TCG_TYPE_I32 ? OPC_CLZ : OPC_DCLZ; 1666 tcg_out_opc_reg(s, opcv2, a0, a1, a1); 1667 } 1668} 1669 1670static TCGConstraintSetIndex cset_clz(TCGType type, unsigned flags) 1671{ 1672 return use_mips32r2_instructions ? C_O1_I2(r, r, rzW) : C_NotImplemented; 1673} 1674 1675static const TCGOutOpBinary outop_clz = { 1676 .base.static_constraint = C_Dynamic, 1677 .base.dynamic_constraint = cset_clz, 1678 .out_rrr = tgen_clz, 1679 .out_rri = tgen_clzi, 1680}; 1681 1682static const TCGOutOpUnary outop_ctpop = { 1683 .base.static_constraint = C_NotImplemented, 1684}; 1685 1686static const TCGOutOpBinary outop_ctz = { 1687 .base.static_constraint = C_NotImplemented, 1688}; 1689 1690static void tgen_divs(TCGContext *s, TCGType type, 1691 TCGReg a0, TCGReg a1, TCGReg a2) 1692{ 1693 if (use_mips32r6_instructions) { 1694 if (type == TCG_TYPE_I32) { 1695 tcg_out_opc_reg(s, OPC_DIV_R6, a0, a1, a2); 1696 } else { 1697 tcg_out_opc_reg(s, OPC_DDIV_R6, a0, a1, a2); 1698 } 1699 } else { 1700 MIPSInsn insn = type == TCG_TYPE_I32 ? OPC_DIV : OPC_DDIV; 1701 tcg_out_opc_reg(s, insn, 0, a1, a2); 1702 tcg_out_opc_reg(s, OPC_MFLO, a0, 0, 0); 1703 } 1704} 1705 1706static const TCGOutOpBinary outop_divs = { 1707 .base.static_constraint = C_O1_I2(r, r, r), 1708 .out_rrr = tgen_divs, 1709}; 1710 1711static const TCGOutOpDivRem outop_divs2 = { 1712 .base.static_constraint = C_NotImplemented, 1713}; 1714 1715static void tgen_divu(TCGContext *s, TCGType type, 1716 TCGReg a0, TCGReg a1, TCGReg a2) 1717{ 1718 if (use_mips32r6_instructions) { 1719 if (type == TCG_TYPE_I32) { 1720 tcg_out_opc_reg(s, OPC_DIVU_R6, a0, a1, a2); 1721 } else { 1722 tcg_out_opc_reg(s, OPC_DDIVU_R6, a0, a1, a2); 1723 } 1724 } else { 1725 MIPSInsn insn = type == TCG_TYPE_I32 ? OPC_DIVU : OPC_DDIVU; 1726 tcg_out_opc_reg(s, insn, 0, a1, a2); 1727 tcg_out_opc_reg(s, OPC_MFLO, a0, 0, 0); 1728 } 1729} 1730 1731static const TCGOutOpBinary outop_divu = { 1732 .base.static_constraint = C_O1_I2(r, r, r), 1733 .out_rrr = tgen_divu, 1734}; 1735 1736static const TCGOutOpDivRem outop_divu2 = { 1737 .base.static_constraint = C_NotImplemented, 1738}; 1739 1740static const TCGOutOpBinary outop_eqv = { 1741 .base.static_constraint = C_NotImplemented, 1742}; 1743 1744#if TCG_TARGET_REG_BITS == 64 1745static void tgen_extrh_i64_i32(TCGContext *s, TCGType t, TCGReg a0, TCGReg a1) 1746{ 1747 tcg_out_dsra(s, a0, a1, 32); 1748} 1749 1750static const TCGOutOpUnary outop_extrh_i64_i32 = { 1751 .base.static_constraint = C_O1_I1(r, r), 1752 .out_rr = tgen_extrh_i64_i32, 1753}; 1754#endif 1755 1756static void tgen_mul(TCGContext *s, TCGType type, 1757 TCGReg a0, TCGReg a1, TCGReg a2) 1758{ 1759 MIPSInsn insn; 1760 1761 if (type == TCG_TYPE_I32) { 1762 if (use_mips32_instructions) { 1763 tcg_out_opc_reg(s, OPC_MUL, a0, a1, a2); 1764 return; 1765 } 1766 insn = OPC_MULT; 1767 } else { 1768 if (use_mips32r6_instructions) { 1769 tcg_out_opc_reg(s, OPC_DMUL, a0, a1, a2); 1770 return; 1771 } 1772 insn = OPC_DMULT; 1773 } 1774 tcg_out_opc_reg(s, insn, 0, a1, a2); 1775 tcg_out_opc_reg(s, OPC_MFLO, a0, 0, 0); 1776} 1777 1778static const TCGOutOpBinary outop_mul = { 1779 .base.static_constraint = C_O1_I2(r, r, r), 1780 .out_rrr = tgen_mul, 1781}; 1782 1783static void tgen_muls2(TCGContext *s, TCGType type, 1784 TCGReg a0, TCGReg a1, TCGReg a2, TCGReg a3) 1785{ 1786 MIPSInsn insn = type == TCG_TYPE_I32 ? OPC_MULT : OPC_DMULT; 1787 tcg_out_opc_reg(s, insn, 0, a2, a3); 1788 tcg_out_opc_reg(s, OPC_MFLO, a0, 0, 0); 1789 tcg_out_opc_reg(s, OPC_MFHI, a1, 0, 0); 1790} 1791 1792static TCGConstraintSetIndex cset_mul2(TCGType type, unsigned flags) 1793{ 1794 return use_mips32r6_instructions ? C_NotImplemented : C_O2_I2(r, r, r, r); 1795} 1796 1797static const TCGOutOpMul2 outop_muls2 = { 1798 .base.static_constraint = C_Dynamic, 1799 .base.dynamic_constraint = cset_mul2, 1800 .out_rrrr = tgen_muls2, 1801}; 1802 1803static void tgen_mulsh(TCGContext *s, TCGType type, 1804 TCGReg a0, TCGReg a1, TCGReg a2) 1805{ 1806 if (use_mips32r6_instructions) { 1807 MIPSInsn insn = type == TCG_TYPE_I32 ? OPC_MUH : OPC_DMUH; 1808 tcg_out_opc_reg(s, insn, a0, a1, a2); 1809 } else { 1810 MIPSInsn insn = type == TCG_TYPE_I32 ? OPC_MULT : OPC_DMULT; 1811 tcg_out_opc_reg(s, insn, 0, a1, a2); 1812 tcg_out_opc_reg(s, OPC_MFHI, a0, 0, 0); 1813 } 1814} 1815 1816static const TCGOutOpBinary outop_mulsh = { 1817 .base.static_constraint = C_O1_I2(r, r, r), 1818 .out_rrr = tgen_mulsh, 1819}; 1820 1821static void tgen_mulu2(TCGContext *s, TCGType type, 1822 TCGReg a0, TCGReg a1, TCGReg a2, TCGReg a3) 1823{ 1824 MIPSInsn insn = type == TCG_TYPE_I32 ? OPC_MULTU : OPC_DMULTU; 1825 tcg_out_opc_reg(s, insn, 0, a2, a3); 1826 tcg_out_opc_reg(s, OPC_MFLO, a0, 0, 0); 1827 tcg_out_opc_reg(s, OPC_MFHI, a1, 0, 0); 1828} 1829 1830static const TCGOutOpMul2 outop_mulu2 = { 1831 .base.static_constraint = C_Dynamic, 1832 .base.dynamic_constraint = cset_mul2, 1833 .out_rrrr = tgen_mulu2, 1834}; 1835 1836static void tgen_muluh(TCGContext *s, TCGType type, 1837 TCGReg a0, TCGReg a1, TCGReg a2) 1838{ 1839 if (use_mips32r6_instructions) { 1840 MIPSInsn insn = type == TCG_TYPE_I32 ? OPC_MUHU : OPC_DMUHU; 1841 tcg_out_opc_reg(s, insn, a0, a1, a2); 1842 } else { 1843 MIPSInsn insn = type == TCG_TYPE_I32 ? OPC_MULTU : OPC_DMULTU; 1844 tcg_out_opc_reg(s, insn, 0, a1, a2); 1845 tcg_out_opc_reg(s, OPC_MFHI, a0, 0, 0); 1846 } 1847} 1848 1849static const TCGOutOpBinary outop_muluh = { 1850 .base.static_constraint = C_O1_I2(r, r, r), 1851 .out_rrr = tgen_muluh, 1852}; 1853 1854static const TCGOutOpBinary outop_nand = { 1855 .base.static_constraint = C_NotImplemented, 1856}; 1857 1858static void tgen_nor(TCGContext *s, TCGType type, 1859 TCGReg a0, TCGReg a1, TCGReg a2) 1860{ 1861 tcg_out_opc_reg(s, OPC_NOR, a0, a1, a2); 1862} 1863 1864static const TCGOutOpBinary outop_nor = { 1865 .base.static_constraint = C_O1_I2(r, r, r), 1866 .out_rrr = tgen_nor, 1867}; 1868 1869static void tgen_or(TCGContext *s, TCGType type, 1870 TCGReg a0, TCGReg a1, TCGReg a2) 1871{ 1872 tcg_out_opc_reg(s, OPC_OR, a0, a1, a2); 1873} 1874 1875static void tgen_ori(TCGContext *s, TCGType type, 1876 TCGReg a0, TCGReg a1, tcg_target_long a2) 1877{ 1878 tcg_out_opc_imm(s, OPC_ORI, a0, a1, a2); 1879} 1880 1881static const TCGOutOpBinary outop_or = { 1882 .base.static_constraint = C_O1_I2(r, r, rI), 1883 .out_rrr = tgen_or, 1884 .out_rri = tgen_ori, 1885}; 1886 1887static const TCGOutOpBinary outop_orc = { 1888 .base.static_constraint = C_NotImplemented, 1889}; 1890 1891static void tgen_rems(TCGContext *s, TCGType type, 1892 TCGReg a0, TCGReg a1, TCGReg a2) 1893{ 1894 if (use_mips32r6_instructions) { 1895 if (type == TCG_TYPE_I32) { 1896 tcg_out_opc_reg(s, OPC_MOD, a0, a1, a2); 1897 } else { 1898 tcg_out_opc_reg(s, OPC_DMOD, a0, a1, a2); 1899 } 1900 } else { 1901 MIPSInsn insn = type == TCG_TYPE_I32 ? OPC_DIV : OPC_DDIV; 1902 tcg_out_opc_reg(s, insn, 0, a1, a2); 1903 tcg_out_opc_reg(s, OPC_MFHI, a0, 0, 0); 1904 } 1905} 1906 1907static const TCGOutOpBinary outop_rems = { 1908 .base.static_constraint = C_O1_I2(r, r, r), 1909 .out_rrr = tgen_rems, 1910}; 1911 1912static void tgen_remu(TCGContext *s, TCGType type, 1913 TCGReg a0, TCGReg a1, TCGReg a2) 1914{ 1915 if (use_mips32r6_instructions) { 1916 if (type == TCG_TYPE_I32) { 1917 tcg_out_opc_reg(s, OPC_MODU, a0, a1, a2); 1918 } else { 1919 tcg_out_opc_reg(s, OPC_DMODU, a0, a1, a2); 1920 } 1921 } else { 1922 MIPSInsn insn = type == TCG_TYPE_I32 ? OPC_DIVU : OPC_DDIVU; 1923 tcg_out_opc_reg(s, insn, 0, a1, a2); 1924 tcg_out_opc_reg(s, OPC_MFHI, a0, 0, 0); 1925 } 1926} 1927 1928static const TCGOutOpBinary outop_remu = { 1929 .base.static_constraint = C_O1_I2(r, r, r), 1930 .out_rrr = tgen_remu, 1931}; 1932 1933static const TCGOutOpBinary outop_rotl = { 1934 .base.static_constraint = C_NotImplemented, 1935}; 1936 1937static TCGConstraintSetIndex cset_rotr(TCGType type, unsigned flags) 1938{ 1939 return use_mips32r2_instructions ? C_O1_I2(r, r, ri) : C_NotImplemented; 1940} 1941 1942static void tgen_rotr(TCGContext *s, TCGType type, 1943 TCGReg a0, TCGReg a1, TCGReg a2) 1944{ 1945 MIPSInsn insn = type == TCG_TYPE_I32 ? OPC_ROTRV : OPC_DROTRV; 1946 tcg_out_opc_reg(s, insn, a0, a1, a2); 1947} 1948 1949static void tgen_rotri(TCGContext *s, TCGType type, 1950 TCGReg a0, TCGReg a1, tcg_target_long a2) 1951{ 1952 if (type == TCG_TYPE_I32) { 1953 tcg_out_opc_sa(s, OPC_ROTR, a0, a1, a2); 1954 } else { 1955 tcg_out_opc_sa64(s, OPC_DROTR, OPC_DROTR32, a0, a1, a2); 1956 } 1957} 1958 1959static const TCGOutOpBinary outop_rotr = { 1960 .base.static_constraint = C_Dynamic, 1961 .base.dynamic_constraint = cset_rotr, 1962 .out_rrr = tgen_rotr, 1963 .out_rri = tgen_rotri, 1964}; 1965 1966static void tgen_sar(TCGContext *s, TCGType type, 1967 TCGReg a0, TCGReg a1, TCGReg a2) 1968{ 1969 MIPSInsn insn = type == TCG_TYPE_I32 ? OPC_SRAV : OPC_DSRAV; 1970 tcg_out_opc_reg(s, insn, a0, a1, a2); 1971} 1972 1973static void tgen_sari(TCGContext *s, TCGType type, 1974 TCGReg a0, TCGReg a1, tcg_target_long a2) 1975{ 1976 if (type == TCG_TYPE_I32) { 1977 tcg_out_opc_sa(s, OPC_SRA, a0, a1, a2); 1978 } else { 1979 tcg_out_dsra(s, a0, a1, a2); 1980 } 1981} 1982 1983static const TCGOutOpBinary outop_sar = { 1984 .base.static_constraint = C_O1_I2(r, r, ri), 1985 .out_rrr = tgen_sar, 1986 .out_rri = tgen_sari, 1987}; 1988 1989static void tgen_shl(TCGContext *s, TCGType type, 1990 TCGReg a0, TCGReg a1, TCGReg a2) 1991{ 1992 MIPSInsn insn = type == TCG_TYPE_I32 ? OPC_SLLV : OPC_DSLLV; 1993 tcg_out_opc_reg(s, insn, a0, a1, a2); 1994} 1995 1996static void tgen_shli(TCGContext *s, TCGType type, 1997 TCGReg a0, TCGReg a1, tcg_target_long a2) 1998{ 1999 if (type == TCG_TYPE_I32) { 2000 tcg_out_opc_sa(s, OPC_SLL, a0, a1, a2); 2001 } else { 2002 tcg_out_dsll(s, a0, a1, a2); 2003 } 2004} 2005 2006static const TCGOutOpBinary outop_shl = { 2007 .base.static_constraint = C_O1_I2(r, r, ri), 2008 .out_rrr = tgen_shl, 2009 .out_rri = tgen_shli, 2010}; 2011 2012static void tgen_shr(TCGContext *s, TCGType type, 2013 TCGReg a0, TCGReg a1, TCGReg a2) 2014{ 2015 MIPSInsn insn = type == TCG_TYPE_I32 ? OPC_SRLV : OPC_DSRLV; 2016 tcg_out_opc_reg(s, insn, a0, a1, a2); 2017} 2018 2019static void tgen_shri(TCGContext *s, TCGType type, 2020 TCGReg a0, TCGReg a1, tcg_target_long a2) 2021{ 2022 if (type == TCG_TYPE_I32) { 2023 tcg_out_opc_sa(s, OPC_SRL, a0, a1, a2); 2024 } else { 2025 tcg_out_dsrl(s, a0, a1, a2); 2026 } 2027} 2028 2029static const TCGOutOpBinary outop_shr = { 2030 .base.static_constraint = C_O1_I2(r, r, ri), 2031 .out_rrr = tgen_shr, 2032 .out_rri = tgen_shri, 2033}; 2034 2035static void tgen_sub(TCGContext *s, TCGType type, 2036 TCGReg a0, TCGReg a1, TCGReg a2) 2037{ 2038 MIPSInsn insn = type == TCG_TYPE_I32 ? OPC_SUBU : OPC_DSUBU; 2039 tcg_out_opc_reg(s, insn, a0, a1, a2); 2040} 2041 2042static const TCGOutOpSubtract outop_sub = { 2043 .base.static_constraint = C_O1_I2(r, r, r), 2044 .out_rrr = tgen_sub, 2045}; 2046 2047static void tgen_xor(TCGContext *s, TCGType type, 2048 TCGReg a0, TCGReg a1, TCGReg a2) 2049{ 2050 tcg_out_opc_reg(s, OPC_XOR, a0, a1, a2); 2051} 2052 2053static void tgen_xori(TCGContext *s, TCGType type, 2054 TCGReg a0, TCGReg a1, tcg_target_long a2) 2055{ 2056 tcg_out_opc_imm(s, OPC_XORI, a0, a1, a2); 2057} 2058 2059static const TCGOutOpBinary outop_xor = { 2060 .base.static_constraint = C_O1_I2(r, r, rI), 2061 .out_rrr = tgen_xor, 2062 .out_rri = tgen_xori, 2063}; 2064 2065static void tgen_bswap16(TCGContext *s, TCGType type, 2066 TCGReg ret, TCGReg arg, unsigned flags) 2067{ 2068 /* With arg = abcd: */ 2069 if (use_mips32r2_instructions) { 2070 tcg_out_opc_reg(s, OPC_WSBH, ret, 0, arg); /* badc */ 2071 if (flags & TCG_BSWAP_OS) { 2072 tcg_out_opc_reg(s, OPC_SEH, ret, 0, ret); /* ssdc */ 2073 } else if ((flags & (TCG_BSWAP_IZ | TCG_BSWAP_OZ)) == TCG_BSWAP_OZ) { 2074 tcg_out_opc_imm(s, OPC_ANDI, ret, ret, 0xffff); /* 00dc */ 2075 } 2076 return; 2077 } 2078 2079 tcg_out_opc_sa(s, OPC_SRL, TCG_TMP0, arg, 8); /* 0abc */ 2080 if (!(flags & TCG_BSWAP_IZ)) { 2081 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP0, TCG_TMP0, 0x00ff); /* 000c */ 2082 } 2083 if (flags & TCG_BSWAP_OS) { 2084 tcg_out_opc_sa(s, OPC_SLL, ret, arg, 24); /* d000 */ 2085 tcg_out_opc_sa(s, OPC_SRA, ret, ret, 16); /* ssd0 */ 2086 } else { 2087 tcg_out_opc_sa(s, OPC_SLL, ret, arg, 8); /* bcd0 */ 2088 if (flags & TCG_BSWAP_OZ) { 2089 tcg_out_opc_imm(s, OPC_ANDI, ret, ret, 0xff00); /* 00d0 */ 2090 } 2091 } 2092 tcg_out_opc_reg(s, OPC_OR, ret, ret, TCG_TMP0); /* ssdc */ 2093} 2094 2095static const TCGOutOpBswap outop_bswap16 = { 2096 .base.static_constraint = C_O1_I1(r, r), 2097 .out_rr = tgen_bswap16, 2098}; 2099 2100static void tgen_bswap32(TCGContext *s, TCGType type, 2101 TCGReg ret, TCGReg arg, unsigned flags) 2102{ 2103 if (use_mips32r2_instructions) { 2104 tcg_out_opc_reg(s, OPC_WSBH, ret, 0, arg); 2105 tcg_out_opc_sa(s, OPC_ROTR, ret, ret, 16); 2106 if (flags & TCG_BSWAP_OZ) { 2107 tcg_out_opc_bf(s, OPC_DEXT, ret, ret, 31, 0); 2108 } 2109 } else { 2110 if (flags & TCG_BSWAP_OZ) { 2111 tcg_out_bswap_subr(s, bswap32u_addr); 2112 } else { 2113 tcg_out_bswap_subr(s, bswap32_addr); 2114 } 2115 /* delay slot -- never omit the insn, like tcg_out_mov might. */ 2116 tcg_out_opc_reg(s, OPC_OR, TCG_TMP0, arg, TCG_REG_ZERO); 2117 tcg_out_mov(s, TCG_TYPE_I32, ret, TCG_TMP3); 2118 } 2119} 2120 2121static const TCGOutOpBswap outop_bswap32 = { 2122 .base.static_constraint = C_O1_I1(r, r), 2123 .out_rr = tgen_bswap32, 2124}; 2125 2126#if TCG_TARGET_REG_BITS == 64 2127static void tgen_bswap64(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg) 2128{ 2129 if (use_mips32r2_instructions) { 2130 tcg_out_opc_reg(s, OPC_DSBH, ret, 0, arg); 2131 tcg_out_opc_reg(s, OPC_DSHD, ret, 0, ret); 2132 } else { 2133 tcg_out_bswap_subr(s, bswap64_addr); 2134 /* delay slot -- never omit the insn, like tcg_out_mov might. */ 2135 tcg_out_opc_reg(s, OPC_OR, TCG_TMP0, arg, TCG_REG_ZERO); 2136 tcg_out_mov(s, TCG_TYPE_I32, ret, TCG_TMP3); 2137 } 2138} 2139 2140static const TCGOutOpUnary outop_bswap64 = { 2141 .base.static_constraint = C_O1_I1(r, r), 2142 .out_rr = tgen_bswap64, 2143}; 2144#endif /* TCG_TARGET_REG_BITS == 64 */ 2145 2146static void tgen_neg(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1) 2147{ 2148 tgen_sub(s, type, a0, TCG_REG_ZERO, a1); 2149} 2150 2151static const TCGOutOpUnary outop_neg = { 2152 .base.static_constraint = C_O1_I1(r, r), 2153 .out_rr = tgen_neg, 2154}; 2155 2156static void tgen_not(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1) 2157{ 2158 tgen_nor(s, type, a0, TCG_REG_ZERO, a1); 2159} 2160 2161static const TCGOutOpUnary outop_not = { 2162 .base.static_constraint = C_O1_I1(r, r), 2163 .out_rr = tgen_not, 2164}; 2165 2166static void tgen_deposit(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, 2167 TCGReg a2, unsigned ofs, unsigned len) 2168{ 2169 if (type == TCG_TYPE_I32) { 2170 tcg_out_opc_bf(s, OPC_INS, a0, a2, ofs + len - 1, ofs); 2171 } else { 2172 tcg_out_opc_bf64(s, OPC_DINS, OPC_DINSM, OPC_DINSU, a0, a2, 2173 ofs + len - 1, ofs); 2174 } 2175} 2176 2177static const TCGOutOpDeposit outop_deposit = { 2178 .base.static_constraint = C_O1_I2(r, 0, rz), 2179 .out_rrr = tgen_deposit, 2180}; 2181 2182static void tgen_extract(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, 2183 unsigned ofs, unsigned len) 2184{ 2185 if (ofs == 0 && len <= 16) { 2186 tcg_out_opc_imm(s, OPC_ANDI, a0, a1, (1 << len) - 1); 2187 } else if (type == TCG_TYPE_I32) { 2188 tcg_out_opc_bf(s, OPC_EXT, a0, a1, len - 1, ofs); 2189 } else { 2190 tcg_out_opc_bf64(s, OPC_DEXT, OPC_DEXTM, OPC_DEXTU, 2191 a0, a1, len - 1, ofs); 2192 } 2193} 2194 2195static const TCGOutOpExtract outop_extract = { 2196 .base.static_constraint = C_O1_I1(r, r), 2197 .out_rr = tgen_extract, 2198}; 2199 2200static void tgen_sextract(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, 2201 unsigned ofs, unsigned len) 2202{ 2203 if (ofs == 0) { 2204 switch (len) { 2205 case 8: 2206 tcg_out_ext8s(s, type, a0, a1); 2207 return; 2208 case 16: 2209 tcg_out_ext16s(s, type, a0, a1); 2210 return; 2211 case 32: 2212 tcg_out_ext32s(s, a0, a1); 2213 return; 2214 } 2215 } 2216 g_assert_not_reached(); 2217} 2218 2219static const TCGOutOpExtract outop_sextract = { 2220 .base.static_constraint = C_O1_I1(r, r), 2221 .out_rr = tgen_sextract, 2222}; 2223 2224static const TCGOutOpExtract2 outop_extract2 = { 2225 .base.static_constraint = C_NotImplemented, 2226}; 2227 2228 2229static void tcg_out_op(TCGContext *s, TCGOpcode opc, TCGType type, 2230 const TCGArg args[TCG_MAX_OP_ARGS], 2231 const int const_args[TCG_MAX_OP_ARGS]) 2232{ 2233 MIPSInsn i1; 2234 TCGArg a0, a1, a2; 2235 2236 a0 = args[0]; 2237 a1 = args[1]; 2238 a2 = args[2]; 2239 2240 switch (opc) { 2241 case INDEX_op_goto_ptr: 2242 /* jmp to the given host address (could be epilogue) */ 2243 tcg_out_opc_reg(s, OPC_JR, 0, a0, 0); 2244 if (TCG_TARGET_REG_BITS == 64) { 2245 tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_TB, a0); 2246 } else { 2247 tcg_out_nop(s); 2248 } 2249 break; 2250 case INDEX_op_br: 2251 tgen_brcond(s, TCG_TYPE_I32, TCG_COND_EQ, 2252 TCG_REG_ZERO, TCG_REG_ZERO, arg_label(a0)); 2253 break; 2254 2255 case INDEX_op_ld8u_i32: 2256 case INDEX_op_ld8u_i64: 2257 i1 = OPC_LBU; 2258 goto do_ldst; 2259 case INDEX_op_ld8s_i32: 2260 case INDEX_op_ld8s_i64: 2261 i1 = OPC_LB; 2262 goto do_ldst; 2263 case INDEX_op_ld16u_i32: 2264 case INDEX_op_ld16u_i64: 2265 i1 = OPC_LHU; 2266 goto do_ldst; 2267 case INDEX_op_ld16s_i32: 2268 case INDEX_op_ld16s_i64: 2269 i1 = OPC_LH; 2270 goto do_ldst; 2271 case INDEX_op_ld_i32: 2272 case INDEX_op_ld32s_i64: 2273 i1 = OPC_LW; 2274 goto do_ldst; 2275 case INDEX_op_ld32u_i64: 2276 i1 = OPC_LWU; 2277 goto do_ldst; 2278 case INDEX_op_ld_i64: 2279 i1 = OPC_LD; 2280 goto do_ldst; 2281 case INDEX_op_st8_i32: 2282 case INDEX_op_st8_i64: 2283 i1 = OPC_SB; 2284 goto do_ldst; 2285 case INDEX_op_st16_i32: 2286 case INDEX_op_st16_i64: 2287 i1 = OPC_SH; 2288 goto do_ldst; 2289 case INDEX_op_st_i32: 2290 case INDEX_op_st32_i64: 2291 i1 = OPC_SW; 2292 goto do_ldst; 2293 case INDEX_op_st_i64: 2294 i1 = OPC_SD; 2295 do_ldst: 2296 tcg_out_ldst(s, i1, a0, a1, a2); 2297 break; 2298 2299 case INDEX_op_qemu_ld_i32: 2300 tcg_out_qemu_ld(s, a0, 0, a1, a2, TCG_TYPE_I32); 2301 break; 2302 case INDEX_op_qemu_ld_i64: 2303 if (TCG_TARGET_REG_BITS == 64) { 2304 tcg_out_qemu_ld(s, a0, 0, a1, a2, TCG_TYPE_I64); 2305 } else { 2306 tcg_out_qemu_ld(s, a0, a1, a2, args[3], TCG_TYPE_I64); 2307 } 2308 break; 2309 2310 case INDEX_op_qemu_st_i32: 2311 tcg_out_qemu_st(s, a0, 0, a1, a2, TCG_TYPE_I32); 2312 break; 2313 case INDEX_op_qemu_st_i64: 2314 if (TCG_TARGET_REG_BITS == 64) { 2315 tcg_out_qemu_st(s, a0, 0, a1, a2, TCG_TYPE_I64); 2316 } else { 2317 tcg_out_qemu_st(s, a0, a1, a2, args[3], TCG_TYPE_I64); 2318 } 2319 break; 2320 2321 case INDEX_op_mb: 2322 tcg_out_mb(s, a0); 2323 break; 2324 case INDEX_op_call: /* Always emitted via tcg_out_call. */ 2325 case INDEX_op_exit_tb: /* Always emitted via tcg_out_exit_tb. */ 2326 case INDEX_op_goto_tb: /* Always emitted via tcg_out_goto_tb. */ 2327 default: 2328 g_assert_not_reached(); 2329 } 2330} 2331 2332static TCGConstraintSetIndex 2333tcg_target_op_def(TCGOpcode op, TCGType type, unsigned flags) 2334{ 2335 switch (op) { 2336 case INDEX_op_goto_ptr: 2337 return C_O0_I1(r); 2338 2339 case INDEX_op_ld8u_i32: 2340 case INDEX_op_ld8s_i32: 2341 case INDEX_op_ld16u_i32: 2342 case INDEX_op_ld16s_i32: 2343 case INDEX_op_ld_i32: 2344 case INDEX_op_ld8u_i64: 2345 case INDEX_op_ld8s_i64: 2346 case INDEX_op_ld16u_i64: 2347 case INDEX_op_ld16s_i64: 2348 case INDEX_op_ld32s_i64: 2349 case INDEX_op_ld32u_i64: 2350 case INDEX_op_ld_i64: 2351 return C_O1_I1(r, r); 2352 2353 case INDEX_op_st8_i32: 2354 case INDEX_op_st16_i32: 2355 case INDEX_op_st_i32: 2356 case INDEX_op_st8_i64: 2357 case INDEX_op_st16_i64: 2358 case INDEX_op_st32_i64: 2359 case INDEX_op_st_i64: 2360 return C_O0_I2(rz, r); 2361 2362 case INDEX_op_qemu_ld_i32: 2363 return C_O1_I1(r, r); 2364 case INDEX_op_qemu_st_i32: 2365 return C_O0_I2(rz, r); 2366 case INDEX_op_qemu_ld_i64: 2367 return TCG_TARGET_REG_BITS == 64 ? C_O1_I1(r, r) : C_O2_I1(r, r, r); 2368 case INDEX_op_qemu_st_i64: 2369 return TCG_TARGET_REG_BITS == 64 ? C_O0_I2(rz, r) : C_O0_I3(rz, rz, r); 2370 2371 default: 2372 return C_NotImplemented; 2373 } 2374} 2375 2376static const int tcg_target_callee_save_regs[] = { 2377 TCG_REG_S0, 2378 TCG_REG_S1, 2379 TCG_REG_S2, 2380 TCG_REG_S3, 2381 TCG_REG_S4, 2382 TCG_REG_S5, 2383 TCG_REG_S6, /* used for the tb base (TCG_REG_TB) */ 2384 TCG_REG_S7, /* used for guest_base */ 2385 TCG_REG_S8, /* used for the global env (TCG_AREG0) */ 2386 TCG_REG_RA, /* should be last for ABI compliance */ 2387}; 2388 2389/* The Linux kernel doesn't provide any information about the available 2390 instruction set. Probe it using a signal handler. */ 2391 2392 2393#ifndef use_movnz_instructions 2394bool use_movnz_instructions = false; 2395#endif 2396 2397#ifndef use_mips32_instructions 2398bool use_mips32_instructions = false; 2399#endif 2400 2401#ifndef use_mips32r2_instructions 2402bool use_mips32r2_instructions = false; 2403#endif 2404 2405static volatile sig_atomic_t got_sigill; 2406 2407static void sigill_handler(int signo, siginfo_t *si, void *data) 2408{ 2409 /* Skip the faulty instruction */ 2410 ucontext_t *uc = (ucontext_t *)data; 2411 uc->uc_mcontext.pc += 4; 2412 2413 got_sigill = 1; 2414} 2415 2416static void tcg_target_detect_isa(void) 2417{ 2418 struct sigaction sa_old, sa_new; 2419 2420 memset(&sa_new, 0, sizeof(sa_new)); 2421 sa_new.sa_flags = SA_SIGINFO; 2422 sa_new.sa_sigaction = sigill_handler; 2423 sigaction(SIGILL, &sa_new, &sa_old); 2424 2425 /* Probe for movn/movz, necessary to implement movcond. */ 2426#ifndef use_movnz_instructions 2427 got_sigill = 0; 2428 asm volatile(".set push\n" 2429 ".set mips32\n" 2430 "movn $zero, $zero, $zero\n" 2431 "movz $zero, $zero, $zero\n" 2432 ".set pop\n" 2433 : : : ); 2434 use_movnz_instructions = !got_sigill; 2435#endif 2436 2437 /* Probe for MIPS32 instructions. As no subsetting is allowed 2438 by the specification, it is only necessary to probe for one 2439 of the instructions. */ 2440#ifndef use_mips32_instructions 2441 got_sigill = 0; 2442 asm volatile(".set push\n" 2443 ".set mips32\n" 2444 "mul $zero, $zero\n" 2445 ".set pop\n" 2446 : : : ); 2447 use_mips32_instructions = !got_sigill; 2448#endif 2449 2450 /* Probe for MIPS32r2 instructions if MIPS32 instructions are 2451 available. As no subsetting is allowed by the specification, 2452 it is only necessary to probe for one of the instructions. */ 2453#ifndef use_mips32r2_instructions 2454 if (use_mips32_instructions) { 2455 got_sigill = 0; 2456 asm volatile(".set push\n" 2457 ".set mips32r2\n" 2458 "seb $zero, $zero\n" 2459 ".set pop\n" 2460 : : : ); 2461 use_mips32r2_instructions = !got_sigill; 2462 } 2463#endif 2464 2465 sigaction(SIGILL, &sa_old, NULL); 2466} 2467 2468static tcg_insn_unit *align_code_ptr(TCGContext *s) 2469{ 2470 uintptr_t p = (uintptr_t)s->code_ptr; 2471 if (p & 15) { 2472 p = (p + 15) & -16; 2473 s->code_ptr = (void *)p; 2474 } 2475 return s->code_ptr; 2476} 2477 2478/* Stack frame parameters. */ 2479#define REG_SIZE (TCG_TARGET_REG_BITS / 8) 2480#define SAVE_SIZE ((int)ARRAY_SIZE(tcg_target_callee_save_regs) * REG_SIZE) 2481#define TEMP_SIZE (CPU_TEMP_BUF_NLONGS * (int)sizeof(long)) 2482 2483#define FRAME_SIZE ((TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE + SAVE_SIZE \ 2484 + TCG_TARGET_STACK_ALIGN - 1) \ 2485 & -TCG_TARGET_STACK_ALIGN) 2486#define SAVE_OFS (TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE) 2487 2488/* We're expecting to be able to use an immediate for frame allocation. */ 2489QEMU_BUILD_BUG_ON(FRAME_SIZE > 0x7fff); 2490 2491/* Generate global QEMU prologue and epilogue code */ 2492static void tcg_target_qemu_prologue(TCGContext *s) 2493{ 2494 int i; 2495 2496 tcg_set_frame(s, TCG_REG_SP, TCG_STATIC_CALL_ARGS_SIZE, TEMP_SIZE); 2497 2498 /* TB prologue */ 2499 tcg_out_opc_imm(s, ALIAS_PADDI, TCG_REG_SP, TCG_REG_SP, -FRAME_SIZE); 2500 for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) { 2501 tcg_out_st(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i], 2502 TCG_REG_SP, SAVE_OFS + i * REG_SIZE); 2503 } 2504 2505 if (!tcg_use_softmmu && guest_base != (int16_t)guest_base) { 2506 /* 2507 * The function call abi for n32 and n64 will have loaded $25 (t9) 2508 * with the address of the prologue, so we can use that instead 2509 * of TCG_REG_TB. 2510 */ 2511#if TCG_TARGET_REG_BITS == 64 && !defined(__mips_abicalls) 2512# error "Unknown mips abi" 2513#endif 2514 tcg_out_movi_int(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG, guest_base, 2515 TCG_TARGET_REG_BITS == 64 ? TCG_REG_T9 : 0); 2516 tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG); 2517 } 2518 2519 if (TCG_TARGET_REG_BITS == 64) { 2520 tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_TB, tcg_target_call_iarg_regs[1]); 2521 } 2522 2523 /* Call generated code */ 2524 tcg_out_opc_reg(s, OPC_JR, 0, tcg_target_call_iarg_regs[1], 0); 2525 /* delay slot */ 2526 tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]); 2527 2528 /* 2529 * Return path for goto_ptr. Set return value to 0, a-la exit_tb, 2530 * and fall through to the rest of the epilogue. 2531 */ 2532 tcg_code_gen_epilogue = tcg_splitwx_to_rx(s->code_ptr); 2533 tcg_out_mov(s, TCG_TYPE_REG, TCG_REG_V0, TCG_REG_ZERO); 2534 2535 /* TB epilogue */ 2536 tb_ret_addr = tcg_splitwx_to_rx(s->code_ptr); 2537 for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) { 2538 tcg_out_ld(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i], 2539 TCG_REG_SP, SAVE_OFS + i * REG_SIZE); 2540 } 2541 2542 tcg_out_opc_reg(s, OPC_JR, 0, TCG_REG_RA, 0); 2543 /* delay slot */ 2544 tcg_out_opc_imm(s, ALIAS_PADDI, TCG_REG_SP, TCG_REG_SP, FRAME_SIZE); 2545 2546 if (use_mips32r2_instructions) { 2547 return; 2548 } 2549 2550 /* Bswap subroutines: Input in TCG_TMP0, output in TCG_TMP3; 2551 clobbers TCG_TMP1, TCG_TMP2. */ 2552 2553 /* 2554 * bswap32 -- 32-bit swap (signed result for mips64). a0 = abcd. 2555 */ 2556 bswap32_addr = tcg_splitwx_to_rx(align_code_ptr(s)); 2557 /* t3 = (ssss)d000 */ 2558 tcg_out_opc_sa(s, OPC_SLL, TCG_TMP3, TCG_TMP0, 24); 2559 /* t1 = 000a */ 2560 tcg_out_opc_sa(s, OPC_SRL, TCG_TMP1, TCG_TMP0, 24); 2561 /* t2 = 00c0 */ 2562 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP0, 0xff00); 2563 /* t3 = d00a */ 2564 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1); 2565 /* t1 = 0abc */ 2566 tcg_out_opc_sa(s, OPC_SRL, TCG_TMP1, TCG_TMP0, 8); 2567 /* t2 = 0c00 */ 2568 tcg_out_opc_sa(s, OPC_SLL, TCG_TMP2, TCG_TMP2, 8); 2569 /* t1 = 00b0 */ 2570 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP1, 0xff00); 2571 /* t3 = dc0a */ 2572 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP2); 2573 tcg_out_opc_reg(s, OPC_JR, 0, TCG_REG_RA, 0); 2574 /* t3 = dcba -- delay slot */ 2575 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1); 2576 2577 if (TCG_TARGET_REG_BITS == 32) { 2578 return; 2579 } 2580 2581 /* 2582 * bswap32u -- unsigned 32-bit swap. a0 = ....abcd. 2583 */ 2584 bswap32u_addr = tcg_splitwx_to_rx(align_code_ptr(s)); 2585 /* t1 = (0000)000d */ 2586 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP0, 0xff); 2587 /* t3 = 000a */ 2588 tcg_out_opc_sa(s, OPC_SRL, TCG_TMP3, TCG_TMP0, 24); 2589 /* t1 = (0000)d000 */ 2590 tcg_out_dsll(s, TCG_TMP1, TCG_TMP1, 24); 2591 /* t2 = 00c0 */ 2592 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP0, 0xff00); 2593 /* t3 = d00a */ 2594 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1); 2595 /* t1 = 0abc */ 2596 tcg_out_opc_sa(s, OPC_SRL, TCG_TMP1, TCG_TMP0, 8); 2597 /* t2 = 0c00 */ 2598 tcg_out_opc_sa(s, OPC_SLL, TCG_TMP2, TCG_TMP2, 8); 2599 /* t1 = 00b0 */ 2600 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP1, 0xff00); 2601 /* t3 = dc0a */ 2602 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP2); 2603 tcg_out_opc_reg(s, OPC_JR, 0, TCG_REG_RA, 0); 2604 /* t3 = dcba -- delay slot */ 2605 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1); 2606 2607 /* 2608 * bswap64 -- 64-bit swap. a0 = abcdefgh 2609 */ 2610 bswap64_addr = tcg_splitwx_to_rx(align_code_ptr(s)); 2611 /* t3 = h0000000 */ 2612 tcg_out_dsll(s, TCG_TMP3, TCG_TMP0, 56); 2613 /* t1 = 0000000a */ 2614 tcg_out_dsrl(s, TCG_TMP1, TCG_TMP0, 56); 2615 2616 /* t2 = 000000g0 */ 2617 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP0, 0xff00); 2618 /* t3 = h000000a */ 2619 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1); 2620 /* t1 = 00000abc */ 2621 tcg_out_dsrl(s, TCG_TMP1, TCG_TMP0, 40); 2622 /* t2 = 0g000000 */ 2623 tcg_out_dsll(s, TCG_TMP2, TCG_TMP2, 40); 2624 /* t1 = 000000b0 */ 2625 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP1, 0xff00); 2626 2627 /* t3 = hg00000a */ 2628 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP2); 2629 /* t2 = 0000abcd */ 2630 tcg_out_dsrl(s, TCG_TMP2, TCG_TMP0, 32); 2631 /* t3 = hg0000ba */ 2632 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1); 2633 2634 /* t1 = 000000c0 */ 2635 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP2, 0xff00); 2636 /* t2 = 0000000d */ 2637 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP2, 0x00ff); 2638 /* t1 = 00000c00 */ 2639 tcg_out_dsll(s, TCG_TMP1, TCG_TMP1, 8); 2640 /* t2 = 0000d000 */ 2641 tcg_out_dsll(s, TCG_TMP2, TCG_TMP2, 24); 2642 2643 /* t3 = hg000cba */ 2644 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1); 2645 /* t1 = 00abcdef */ 2646 tcg_out_dsrl(s, TCG_TMP1, TCG_TMP0, 16); 2647 /* t3 = hg00dcba */ 2648 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP2); 2649 2650 /* t2 = 0000000f */ 2651 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP1, 0x00ff); 2652 /* t1 = 000000e0 */ 2653 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP1, 0xff00); 2654 /* t2 = 00f00000 */ 2655 tcg_out_dsll(s, TCG_TMP2, TCG_TMP2, 40); 2656 /* t1 = 000e0000 */ 2657 tcg_out_dsll(s, TCG_TMP1, TCG_TMP1, 24); 2658 2659 /* t3 = hgf0dcba */ 2660 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP2); 2661 tcg_out_opc_reg(s, OPC_JR, 0, TCG_REG_RA, 0); 2662 /* t3 = hgfedcba -- delay slot */ 2663 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1); 2664} 2665 2666static void tcg_out_tb_start(TCGContext *s) 2667{ 2668 /* nothing to do */ 2669} 2670 2671static void tcg_target_init(TCGContext *s) 2672{ 2673 tcg_target_detect_isa(); 2674 tcg_target_available_regs[TCG_TYPE_I32] = 0xffffffff; 2675 if (TCG_TARGET_REG_BITS == 64) { 2676 tcg_target_available_regs[TCG_TYPE_I64] = 0xffffffff; 2677 } 2678 2679 tcg_target_call_clobber_regs = 0; 2680 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V0); 2681 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V1); 2682 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_A0); 2683 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_A1); 2684 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_A2); 2685 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_A3); 2686 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T0); 2687 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T1); 2688 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T2); 2689 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T3); 2690 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T4); 2691 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T5); 2692 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T6); 2693 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T7); 2694 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T8); 2695 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T9); 2696 2697 s->reserved_regs = 0; 2698 tcg_regset_set_reg(s->reserved_regs, TCG_REG_ZERO); /* zero register */ 2699 tcg_regset_set_reg(s->reserved_regs, TCG_REG_K0); /* kernel use only */ 2700 tcg_regset_set_reg(s->reserved_regs, TCG_REG_K1); /* kernel use only */ 2701 tcg_regset_set_reg(s->reserved_regs, TCG_TMP0); /* internal use */ 2702 tcg_regset_set_reg(s->reserved_regs, TCG_TMP1); /* internal use */ 2703 tcg_regset_set_reg(s->reserved_regs, TCG_TMP2); /* internal use */ 2704 tcg_regset_set_reg(s->reserved_regs, TCG_TMP3); /* internal use */ 2705 tcg_regset_set_reg(s->reserved_regs, TCG_REG_RA); /* return address */ 2706 tcg_regset_set_reg(s->reserved_regs, TCG_REG_SP); /* stack pointer */ 2707 tcg_regset_set_reg(s->reserved_regs, TCG_REG_GP); /* global pointer */ 2708 if (TCG_TARGET_REG_BITS == 64) { 2709 tcg_regset_set_reg(s->reserved_regs, TCG_REG_TB); /* tc->tc_ptr */ 2710 } 2711} 2712 2713typedef struct { 2714 DebugFrameHeader h; 2715 uint8_t fde_def_cfa[4]; 2716 uint8_t fde_reg_ofs[ARRAY_SIZE(tcg_target_callee_save_regs) * 2]; 2717} DebugFrame; 2718 2719#define ELF_HOST_MACHINE EM_MIPS 2720/* GDB doesn't appear to require proper setting of ELF_HOST_FLAGS, 2721 which is good because they're really quite complicated for MIPS. */ 2722 2723static const DebugFrame debug_frame = { 2724 .h.cie.len = sizeof(DebugFrameCIE) - 4, /* length after .len member */ 2725 .h.cie.id = -1, 2726 .h.cie.version = 1, 2727 .h.cie.code_align = 1, 2728 .h.cie.data_align = -(TCG_TARGET_REG_BITS / 8) & 0x7f, /* sleb128 */ 2729 .h.cie.return_column = TCG_REG_RA, 2730 2731 /* Total FDE size does not include the "len" member. */ 2732 .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset), 2733 2734 .fde_def_cfa = { 2735 12, TCG_REG_SP, /* DW_CFA_def_cfa sp, ... */ 2736 (FRAME_SIZE & 0x7f) | 0x80, /* ... uleb128 FRAME_SIZE */ 2737 (FRAME_SIZE >> 7) 2738 }, 2739 .fde_reg_ofs = { 2740 0x80 + 16, 9, /* DW_CFA_offset, s0, -72 */ 2741 0x80 + 17, 8, /* DW_CFA_offset, s2, -64 */ 2742 0x80 + 18, 7, /* DW_CFA_offset, s3, -56 */ 2743 0x80 + 19, 6, /* DW_CFA_offset, s4, -48 */ 2744 0x80 + 20, 5, /* DW_CFA_offset, s5, -40 */ 2745 0x80 + 21, 4, /* DW_CFA_offset, s6, -32 */ 2746 0x80 + 22, 3, /* DW_CFA_offset, s7, -24 */ 2747 0x80 + 30, 2, /* DW_CFA_offset, s8, -16 */ 2748 0x80 + 31, 1, /* DW_CFA_offset, ra, -8 */ 2749 } 2750}; 2751 2752void tcg_register_jit(const void *buf, size_t buf_size) 2753{ 2754 tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame)); 2755} 2756