1/* 2 * Tiny Code Generator for QEMU 3 * 4 * Copyright (c) 2018 SiFive, Inc 5 * Copyright (c) 2008-2009 Arnaud Patard <arnaud.patard@rtp-net.org> 6 * Copyright (c) 2009 Aurelien Jarno <aurelien@aurel32.net> 7 * Copyright (c) 2008 Fabrice Bellard 8 * 9 * Based on i386/tcg-target.c and mips/tcg-target.c 10 * 11 * Permission is hereby granted, free of charge, to any person obtaining a copy 12 * of this software and associated documentation files (the "Software"), to deal 13 * in the Software without restriction, including without limitation the rights 14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 15 * copies of the Software, and to permit persons to whom the Software is 16 * furnished to do so, subject to the following conditions: 17 * 18 * The above copyright notice and this permission notice shall be included in 19 * all copies or substantial portions of the Software. 20 * 21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 27 * THE SOFTWARE. 28 */ 29 30#include "../tcg-ldst.c.inc" 31#include "../tcg-pool.c.inc" 32 33#ifdef CONFIG_DEBUG_TCG 34static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = { 35 "zero", 36 "ra", 37 "sp", 38 "gp", 39 "tp", 40 "t0", 41 "t1", 42 "t2", 43 "s0", 44 "s1", 45 "a0", 46 "a1", 47 "a2", 48 "a3", 49 "a4", 50 "a5", 51 "a6", 52 "a7", 53 "s2", 54 "s3", 55 "s4", 56 "s5", 57 "s6", 58 "s7", 59 "s8", 60 "s9", 61 "s10", 62 "s11", 63 "t3", 64 "t4", 65 "t5", 66 "t6" 67}; 68#endif 69 70static const int tcg_target_reg_alloc_order[] = { 71 /* Call saved registers */ 72 /* TCG_REG_S0 reservered for TCG_AREG0 */ 73 TCG_REG_S1, 74 TCG_REG_S2, 75 TCG_REG_S3, 76 TCG_REG_S4, 77 TCG_REG_S5, 78 TCG_REG_S6, 79 TCG_REG_S7, 80 TCG_REG_S8, 81 TCG_REG_S9, 82 TCG_REG_S10, 83 TCG_REG_S11, 84 85 /* Call clobbered registers */ 86 TCG_REG_T0, 87 TCG_REG_T1, 88 TCG_REG_T2, 89 TCG_REG_T3, 90 TCG_REG_T4, 91 TCG_REG_T5, 92 TCG_REG_T6, 93 94 /* Argument registers */ 95 TCG_REG_A0, 96 TCG_REG_A1, 97 TCG_REG_A2, 98 TCG_REG_A3, 99 TCG_REG_A4, 100 TCG_REG_A5, 101 TCG_REG_A6, 102 TCG_REG_A7, 103}; 104 105static const int tcg_target_call_iarg_regs[] = { 106 TCG_REG_A0, 107 TCG_REG_A1, 108 TCG_REG_A2, 109 TCG_REG_A3, 110 TCG_REG_A4, 111 TCG_REG_A5, 112 TCG_REG_A6, 113 TCG_REG_A7, 114}; 115 116static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot) 117{ 118 tcg_debug_assert(kind == TCG_CALL_RET_NORMAL); 119 tcg_debug_assert(slot >= 0 && slot <= 1); 120 return TCG_REG_A0 + slot; 121} 122 123#define TCG_CT_CONST_ZERO 0x100 124#define TCG_CT_CONST_S12 0x200 125#define TCG_CT_CONST_N12 0x400 126#define TCG_CT_CONST_M12 0x800 127 128#define ALL_GENERAL_REGS MAKE_64BIT_MASK(0, 32) 129 130#define sextreg sextract64 131 132/* test if a constant matches the constraint */ 133static bool tcg_target_const_match(int64_t val, TCGType type, int ct) 134{ 135 if (ct & TCG_CT_CONST) { 136 return 1; 137 } 138 if ((ct & TCG_CT_CONST_ZERO) && val == 0) { 139 return 1; 140 } 141 /* 142 * Sign extended from 12 bits: [-0x800, 0x7ff]. 143 * Used for most arithmetic, as this is the isa field. 144 */ 145 if ((ct & TCG_CT_CONST_S12) && val >= -0x800 && val <= 0x7ff) { 146 return 1; 147 } 148 /* 149 * Sign extended from 12 bits, negated: [-0x7ff, 0x800]. 150 * Used for subtraction, where a constant must be handled by ADDI. 151 */ 152 if ((ct & TCG_CT_CONST_N12) && val >= -0x7ff && val <= 0x800) { 153 return 1; 154 } 155 /* 156 * Sign extended from 12 bits, +/- matching: [-0x7ff, 0x7ff]. 157 * Used by addsub2, which may need the negative operation, 158 * and requires the modified constant to be representable. 159 */ 160 if ((ct & TCG_CT_CONST_M12) && val >= -0x7ff && val <= 0x7ff) { 161 return 1; 162 } 163 return 0; 164} 165 166/* 167 * RISC-V Base ISA opcodes (IM) 168 */ 169 170typedef enum { 171 OPC_ADD = 0x33, 172 OPC_ADDI = 0x13, 173 OPC_AND = 0x7033, 174 OPC_ANDI = 0x7013, 175 OPC_AUIPC = 0x17, 176 OPC_BEQ = 0x63, 177 OPC_BGE = 0x5063, 178 OPC_BGEU = 0x7063, 179 OPC_BLT = 0x4063, 180 OPC_BLTU = 0x6063, 181 OPC_BNE = 0x1063, 182 OPC_DIV = 0x2004033, 183 OPC_DIVU = 0x2005033, 184 OPC_JAL = 0x6f, 185 OPC_JALR = 0x67, 186 OPC_LB = 0x3, 187 OPC_LBU = 0x4003, 188 OPC_LD = 0x3003, 189 OPC_LH = 0x1003, 190 OPC_LHU = 0x5003, 191 OPC_LUI = 0x37, 192 OPC_LW = 0x2003, 193 OPC_LWU = 0x6003, 194 OPC_MUL = 0x2000033, 195 OPC_MULH = 0x2001033, 196 OPC_MULHSU = 0x2002033, 197 OPC_MULHU = 0x2003033, 198 OPC_OR = 0x6033, 199 OPC_ORI = 0x6013, 200 OPC_REM = 0x2006033, 201 OPC_REMU = 0x2007033, 202 OPC_SB = 0x23, 203 OPC_SD = 0x3023, 204 OPC_SH = 0x1023, 205 OPC_SLL = 0x1033, 206 OPC_SLLI = 0x1013, 207 OPC_SLT = 0x2033, 208 OPC_SLTI = 0x2013, 209 OPC_SLTIU = 0x3013, 210 OPC_SLTU = 0x3033, 211 OPC_SRA = 0x40005033, 212 OPC_SRAI = 0x40005013, 213 OPC_SRL = 0x5033, 214 OPC_SRLI = 0x5013, 215 OPC_SUB = 0x40000033, 216 OPC_SW = 0x2023, 217 OPC_XOR = 0x4033, 218 OPC_XORI = 0x4013, 219 220 OPC_ADDIW = 0x1b, 221 OPC_ADDW = 0x3b, 222 OPC_DIVUW = 0x200503b, 223 OPC_DIVW = 0x200403b, 224 OPC_MULW = 0x200003b, 225 OPC_REMUW = 0x200703b, 226 OPC_REMW = 0x200603b, 227 OPC_SLLIW = 0x101b, 228 OPC_SLLW = 0x103b, 229 OPC_SRAIW = 0x4000501b, 230 OPC_SRAW = 0x4000503b, 231 OPC_SRLIW = 0x501b, 232 OPC_SRLW = 0x503b, 233 OPC_SUBW = 0x4000003b, 234 235 OPC_FENCE = 0x0000000f, 236 OPC_NOP = OPC_ADDI, /* nop = addi r0,r0,0 */ 237} RISCVInsn; 238 239/* 240 * RISC-V immediate and instruction encoders (excludes 16-bit RVC) 241 */ 242 243/* Type-R */ 244 245static int32_t encode_r(RISCVInsn opc, TCGReg rd, TCGReg rs1, TCGReg rs2) 246{ 247 return opc | (rd & 0x1f) << 7 | (rs1 & 0x1f) << 15 | (rs2 & 0x1f) << 20; 248} 249 250/* Type-I */ 251 252static int32_t encode_imm12(uint32_t imm) 253{ 254 return (imm & 0xfff) << 20; 255} 256 257static int32_t encode_i(RISCVInsn opc, TCGReg rd, TCGReg rs1, uint32_t imm) 258{ 259 return opc | (rd & 0x1f) << 7 | (rs1 & 0x1f) << 15 | encode_imm12(imm); 260} 261 262/* Type-S */ 263 264static int32_t encode_simm12(uint32_t imm) 265{ 266 int32_t ret = 0; 267 268 ret |= (imm & 0xFE0) << 20; 269 ret |= (imm & 0x1F) << 7; 270 271 return ret; 272} 273 274static int32_t encode_s(RISCVInsn opc, TCGReg rs1, TCGReg rs2, uint32_t imm) 275{ 276 return opc | (rs1 & 0x1f) << 15 | (rs2 & 0x1f) << 20 | encode_simm12(imm); 277} 278 279/* Type-SB */ 280 281static int32_t encode_sbimm12(uint32_t imm) 282{ 283 int32_t ret = 0; 284 285 ret |= (imm & 0x1000) << 19; 286 ret |= (imm & 0x7e0) << 20; 287 ret |= (imm & 0x1e) << 7; 288 ret |= (imm & 0x800) >> 4; 289 290 return ret; 291} 292 293static int32_t encode_sb(RISCVInsn opc, TCGReg rs1, TCGReg rs2, uint32_t imm) 294{ 295 return opc | (rs1 & 0x1f) << 15 | (rs2 & 0x1f) << 20 | encode_sbimm12(imm); 296} 297 298/* Type-U */ 299 300static int32_t encode_uimm20(uint32_t imm) 301{ 302 return imm & 0xfffff000; 303} 304 305static int32_t encode_u(RISCVInsn opc, TCGReg rd, uint32_t imm) 306{ 307 return opc | (rd & 0x1f) << 7 | encode_uimm20(imm); 308} 309 310/* Type-UJ */ 311 312static int32_t encode_ujimm20(uint32_t imm) 313{ 314 int32_t ret = 0; 315 316 ret |= (imm & 0x0007fe) << (21 - 1); 317 ret |= (imm & 0x000800) << (20 - 11); 318 ret |= (imm & 0x0ff000) << (12 - 12); 319 ret |= (imm & 0x100000) << (31 - 20); 320 321 return ret; 322} 323 324static int32_t encode_uj(RISCVInsn opc, TCGReg rd, uint32_t imm) 325{ 326 return opc | (rd & 0x1f) << 7 | encode_ujimm20(imm); 327} 328 329/* 330 * RISC-V instruction emitters 331 */ 332 333static void tcg_out_opc_reg(TCGContext *s, RISCVInsn opc, 334 TCGReg rd, TCGReg rs1, TCGReg rs2) 335{ 336 tcg_out32(s, encode_r(opc, rd, rs1, rs2)); 337} 338 339static void tcg_out_opc_imm(TCGContext *s, RISCVInsn opc, 340 TCGReg rd, TCGReg rs1, TCGArg imm) 341{ 342 tcg_out32(s, encode_i(opc, rd, rs1, imm)); 343} 344 345static void tcg_out_opc_store(TCGContext *s, RISCVInsn opc, 346 TCGReg rs1, TCGReg rs2, uint32_t imm) 347{ 348 tcg_out32(s, encode_s(opc, rs1, rs2, imm)); 349} 350 351static void tcg_out_opc_branch(TCGContext *s, RISCVInsn opc, 352 TCGReg rs1, TCGReg rs2, uint32_t imm) 353{ 354 tcg_out32(s, encode_sb(opc, rs1, rs2, imm)); 355} 356 357static void tcg_out_opc_upper(TCGContext *s, RISCVInsn opc, 358 TCGReg rd, uint32_t imm) 359{ 360 tcg_out32(s, encode_u(opc, rd, imm)); 361} 362 363static void tcg_out_opc_jump(TCGContext *s, RISCVInsn opc, 364 TCGReg rd, uint32_t imm) 365{ 366 tcg_out32(s, encode_uj(opc, rd, imm)); 367} 368 369static void tcg_out_nop_fill(tcg_insn_unit *p, int count) 370{ 371 int i; 372 for (i = 0; i < count; ++i) { 373 p[i] = OPC_NOP; 374 } 375} 376 377/* 378 * Relocations 379 */ 380 381static bool reloc_sbimm12(tcg_insn_unit *src_rw, const tcg_insn_unit *target) 382{ 383 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw); 384 intptr_t offset = (intptr_t)target - (intptr_t)src_rx; 385 386 tcg_debug_assert((offset & 1) == 0); 387 if (offset == sextreg(offset, 0, 12)) { 388 *src_rw |= encode_sbimm12(offset); 389 return true; 390 } 391 392 return false; 393} 394 395static bool reloc_jimm20(tcg_insn_unit *src_rw, const tcg_insn_unit *target) 396{ 397 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw); 398 intptr_t offset = (intptr_t)target - (intptr_t)src_rx; 399 400 tcg_debug_assert((offset & 1) == 0); 401 if (offset == sextreg(offset, 0, 20)) { 402 *src_rw |= encode_ujimm20(offset); 403 return true; 404 } 405 406 return false; 407} 408 409static bool reloc_call(tcg_insn_unit *src_rw, const tcg_insn_unit *target) 410{ 411 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw); 412 intptr_t offset = (intptr_t)target - (intptr_t)src_rx; 413 int32_t lo = sextreg(offset, 0, 12); 414 int32_t hi = offset - lo; 415 416 if (offset == hi + lo) { 417 src_rw[0] |= encode_uimm20(hi); 418 src_rw[1] |= encode_imm12(lo); 419 return true; 420 } 421 422 return false; 423} 424 425static bool patch_reloc(tcg_insn_unit *code_ptr, int type, 426 intptr_t value, intptr_t addend) 427{ 428 tcg_debug_assert(addend == 0); 429 switch (type) { 430 case R_RISCV_BRANCH: 431 return reloc_sbimm12(code_ptr, (tcg_insn_unit *)value); 432 case R_RISCV_JAL: 433 return reloc_jimm20(code_ptr, (tcg_insn_unit *)value); 434 case R_RISCV_CALL: 435 return reloc_call(code_ptr, (tcg_insn_unit *)value); 436 default: 437 g_assert_not_reached(); 438 } 439} 440 441/* 442 * TCG intrinsics 443 */ 444 445static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg) 446{ 447 if (ret == arg) { 448 return true; 449 } 450 switch (type) { 451 case TCG_TYPE_I32: 452 case TCG_TYPE_I64: 453 tcg_out_opc_imm(s, OPC_ADDI, ret, arg, 0); 454 break; 455 default: 456 g_assert_not_reached(); 457 } 458 return true; 459} 460 461static void tcg_out_movi(TCGContext *s, TCGType type, TCGReg rd, 462 tcg_target_long val) 463{ 464 tcg_target_long lo, hi, tmp; 465 int shift, ret; 466 467 if (type == TCG_TYPE_I32) { 468 val = (int32_t)val; 469 } 470 471 lo = sextreg(val, 0, 12); 472 if (val == lo) { 473 tcg_out_opc_imm(s, OPC_ADDI, rd, TCG_REG_ZERO, lo); 474 return; 475 } 476 477 hi = val - lo; 478 if (val == (int32_t)val) { 479 tcg_out_opc_upper(s, OPC_LUI, rd, hi); 480 if (lo != 0) { 481 tcg_out_opc_imm(s, OPC_ADDIW, rd, rd, lo); 482 } 483 return; 484 } 485 486 tmp = tcg_pcrel_diff(s, (void *)val); 487 if (tmp == (int32_t)tmp) { 488 tcg_out_opc_upper(s, OPC_AUIPC, rd, 0); 489 tcg_out_opc_imm(s, OPC_ADDI, rd, rd, 0); 490 ret = reloc_call(s->code_ptr - 2, (const tcg_insn_unit *)val); 491 tcg_debug_assert(ret == true); 492 return; 493 } 494 495 /* Look for a single 20-bit section. */ 496 shift = ctz64(val); 497 tmp = val >> shift; 498 if (tmp == sextreg(tmp, 0, 20)) { 499 tcg_out_opc_upper(s, OPC_LUI, rd, tmp << 12); 500 if (shift > 12) { 501 tcg_out_opc_imm(s, OPC_SLLI, rd, rd, shift - 12); 502 } else { 503 tcg_out_opc_imm(s, OPC_SRAI, rd, rd, 12 - shift); 504 } 505 return; 506 } 507 508 /* Look for a few high zero bits, with lots of bits set in the middle. */ 509 shift = clz64(val); 510 tmp = val << shift; 511 if (tmp == sextreg(tmp, 12, 20) << 12) { 512 tcg_out_opc_upper(s, OPC_LUI, rd, tmp); 513 tcg_out_opc_imm(s, OPC_SRLI, rd, rd, shift); 514 return; 515 } else if (tmp == sextreg(tmp, 0, 12)) { 516 tcg_out_opc_imm(s, OPC_ADDI, rd, TCG_REG_ZERO, tmp); 517 tcg_out_opc_imm(s, OPC_SRLI, rd, rd, shift); 518 return; 519 } 520 521 /* Drop into the constant pool. */ 522 new_pool_label(s, val, R_RISCV_CALL, s->code_ptr, 0); 523 tcg_out_opc_upper(s, OPC_AUIPC, rd, 0); 524 tcg_out_opc_imm(s, OPC_LD, rd, rd, 0); 525} 526 527static bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2) 528{ 529 return false; 530} 531 532static void tcg_out_addi_ptr(TCGContext *s, TCGReg rd, TCGReg rs, 533 tcg_target_long imm) 534{ 535 /* This function is only used for passing structs by reference. */ 536 g_assert_not_reached(); 537} 538 539static void tcg_out_ext8u(TCGContext *s, TCGReg ret, TCGReg arg) 540{ 541 tcg_out_opc_imm(s, OPC_ANDI, ret, arg, 0xff); 542} 543 544static void tcg_out_ext16u(TCGContext *s, TCGReg ret, TCGReg arg) 545{ 546 tcg_out_opc_imm(s, OPC_SLLIW, ret, arg, 16); 547 tcg_out_opc_imm(s, OPC_SRLIW, ret, ret, 16); 548} 549 550static void tcg_out_ext32u(TCGContext *s, TCGReg ret, TCGReg arg) 551{ 552 tcg_out_opc_imm(s, OPC_SLLI, ret, arg, 32); 553 tcg_out_opc_imm(s, OPC_SRLI, ret, ret, 32); 554} 555 556static void tcg_out_ext8s(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg) 557{ 558 tcg_out_opc_imm(s, OPC_SLLIW, ret, arg, 24); 559 tcg_out_opc_imm(s, OPC_SRAIW, ret, ret, 24); 560} 561 562static void tcg_out_ext16s(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg) 563{ 564 tcg_out_opc_imm(s, OPC_SLLIW, ret, arg, 16); 565 tcg_out_opc_imm(s, OPC_SRAIW, ret, ret, 16); 566} 567 568static void tcg_out_ext32s(TCGContext *s, TCGReg ret, TCGReg arg) 569{ 570 tcg_out_opc_imm(s, OPC_ADDIW, ret, arg, 0); 571} 572 573static void tcg_out_exts_i32_i64(TCGContext *s, TCGReg ret, TCGReg arg) 574{ 575 if (ret != arg) { 576 tcg_out_ext32s(s, ret, arg); 577 } 578} 579 580static void tcg_out_extu_i32_i64(TCGContext *s, TCGReg ret, TCGReg arg) 581{ 582 tcg_out_ext32u(s, ret, arg); 583} 584 585static void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg ret, TCGReg arg) 586{ 587 tcg_out_ext32s(s, ret, arg); 588} 589 590static void tcg_out_ldst(TCGContext *s, RISCVInsn opc, TCGReg data, 591 TCGReg addr, intptr_t offset) 592{ 593 intptr_t imm12 = sextreg(offset, 0, 12); 594 595 if (offset != imm12) { 596 intptr_t diff = tcg_pcrel_diff(s, (void *)offset); 597 598 if (addr == TCG_REG_ZERO && diff == (int32_t)diff) { 599 imm12 = sextreg(diff, 0, 12); 600 tcg_out_opc_upper(s, OPC_AUIPC, TCG_REG_TMP2, diff - imm12); 601 } else { 602 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP2, offset - imm12); 603 if (addr != TCG_REG_ZERO) { 604 tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP2, TCG_REG_TMP2, addr); 605 } 606 } 607 addr = TCG_REG_TMP2; 608 } 609 610 switch (opc) { 611 case OPC_SB: 612 case OPC_SH: 613 case OPC_SW: 614 case OPC_SD: 615 tcg_out_opc_store(s, opc, addr, data, imm12); 616 break; 617 case OPC_LB: 618 case OPC_LBU: 619 case OPC_LH: 620 case OPC_LHU: 621 case OPC_LW: 622 case OPC_LWU: 623 case OPC_LD: 624 tcg_out_opc_imm(s, opc, data, addr, imm12); 625 break; 626 default: 627 g_assert_not_reached(); 628 } 629} 630 631static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg arg, 632 TCGReg arg1, intptr_t arg2) 633{ 634 RISCVInsn insn = type == TCG_TYPE_I32 ? OPC_LW : OPC_LD; 635 tcg_out_ldst(s, insn, arg, arg1, arg2); 636} 637 638static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg, 639 TCGReg arg1, intptr_t arg2) 640{ 641 RISCVInsn insn = type == TCG_TYPE_I32 ? OPC_SW : OPC_SD; 642 tcg_out_ldst(s, insn, arg, arg1, arg2); 643} 644 645static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val, 646 TCGReg base, intptr_t ofs) 647{ 648 if (val == 0) { 649 tcg_out_st(s, type, TCG_REG_ZERO, base, ofs); 650 return true; 651 } 652 return false; 653} 654 655static void tcg_out_addsub2(TCGContext *s, 656 TCGReg rl, TCGReg rh, 657 TCGReg al, TCGReg ah, 658 TCGArg bl, TCGArg bh, 659 bool cbl, bool cbh, bool is_sub, bool is32bit) 660{ 661 const RISCVInsn opc_add = is32bit ? OPC_ADDW : OPC_ADD; 662 const RISCVInsn opc_addi = is32bit ? OPC_ADDIW : OPC_ADDI; 663 const RISCVInsn opc_sub = is32bit ? OPC_SUBW : OPC_SUB; 664 TCGReg th = TCG_REG_TMP1; 665 666 /* If we have a negative constant such that negating it would 667 make the high part zero, we can (usually) eliminate one insn. */ 668 if (cbl && cbh && bh == -1 && bl != 0) { 669 bl = -bl; 670 bh = 0; 671 is_sub = !is_sub; 672 } 673 674 /* By operating on the high part first, we get to use the final 675 carry operation to move back from the temporary. */ 676 if (!cbh) { 677 tcg_out_opc_reg(s, (is_sub ? opc_sub : opc_add), th, ah, bh); 678 } else if (bh != 0 || ah == rl) { 679 tcg_out_opc_imm(s, opc_addi, th, ah, (is_sub ? -bh : bh)); 680 } else { 681 th = ah; 682 } 683 684 /* Note that tcg optimization should eliminate the bl == 0 case. */ 685 if (is_sub) { 686 if (cbl) { 687 tcg_out_opc_imm(s, OPC_SLTIU, TCG_REG_TMP0, al, bl); 688 tcg_out_opc_imm(s, opc_addi, rl, al, -bl); 689 } else { 690 tcg_out_opc_reg(s, OPC_SLTU, TCG_REG_TMP0, al, bl); 691 tcg_out_opc_reg(s, opc_sub, rl, al, bl); 692 } 693 tcg_out_opc_reg(s, opc_sub, rh, th, TCG_REG_TMP0); 694 } else { 695 if (cbl) { 696 tcg_out_opc_imm(s, opc_addi, rl, al, bl); 697 tcg_out_opc_imm(s, OPC_SLTIU, TCG_REG_TMP0, rl, bl); 698 } else if (al == bl) { 699 /* 700 * If the input regs overlap, this is a simple doubling 701 * and carry-out is the input msb. This special case is 702 * required when the output reg overlaps the input, 703 * but we might as well use it always. 704 */ 705 tcg_out_opc_imm(s, OPC_SLTI, TCG_REG_TMP0, al, 0); 706 tcg_out_opc_reg(s, opc_add, rl, al, al); 707 } else { 708 tcg_out_opc_reg(s, opc_add, rl, al, bl); 709 tcg_out_opc_reg(s, OPC_SLTU, TCG_REG_TMP0, 710 rl, (rl == bl ? al : bl)); 711 } 712 tcg_out_opc_reg(s, opc_add, rh, th, TCG_REG_TMP0); 713 } 714} 715 716static const struct { 717 RISCVInsn op; 718 bool swap; 719} tcg_brcond_to_riscv[] = { 720 [TCG_COND_EQ] = { OPC_BEQ, false }, 721 [TCG_COND_NE] = { OPC_BNE, false }, 722 [TCG_COND_LT] = { OPC_BLT, false }, 723 [TCG_COND_GE] = { OPC_BGE, false }, 724 [TCG_COND_LE] = { OPC_BGE, true }, 725 [TCG_COND_GT] = { OPC_BLT, true }, 726 [TCG_COND_LTU] = { OPC_BLTU, false }, 727 [TCG_COND_GEU] = { OPC_BGEU, false }, 728 [TCG_COND_LEU] = { OPC_BGEU, true }, 729 [TCG_COND_GTU] = { OPC_BLTU, true } 730}; 731 732static void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1, 733 TCGReg arg2, TCGLabel *l) 734{ 735 RISCVInsn op = tcg_brcond_to_riscv[cond].op; 736 737 tcg_debug_assert(op != 0); 738 739 if (tcg_brcond_to_riscv[cond].swap) { 740 TCGReg t = arg1; 741 arg1 = arg2; 742 arg2 = t; 743 } 744 745 tcg_out_reloc(s, s->code_ptr, R_RISCV_BRANCH, l, 0); 746 tcg_out_opc_branch(s, op, arg1, arg2, 0); 747} 748 749static void tcg_out_setcond(TCGContext *s, TCGCond cond, TCGReg ret, 750 TCGReg arg1, TCGReg arg2) 751{ 752 switch (cond) { 753 case TCG_COND_EQ: 754 tcg_out_opc_reg(s, OPC_SUB, ret, arg1, arg2); 755 tcg_out_opc_imm(s, OPC_SLTIU, ret, ret, 1); 756 break; 757 case TCG_COND_NE: 758 tcg_out_opc_reg(s, OPC_SUB, ret, arg1, arg2); 759 tcg_out_opc_reg(s, OPC_SLTU, ret, TCG_REG_ZERO, ret); 760 break; 761 case TCG_COND_LT: 762 tcg_out_opc_reg(s, OPC_SLT, ret, arg1, arg2); 763 break; 764 case TCG_COND_GE: 765 tcg_out_opc_reg(s, OPC_SLT, ret, arg1, arg2); 766 tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1); 767 break; 768 case TCG_COND_LE: 769 tcg_out_opc_reg(s, OPC_SLT, ret, arg2, arg1); 770 tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1); 771 break; 772 case TCG_COND_GT: 773 tcg_out_opc_reg(s, OPC_SLT, ret, arg2, arg1); 774 break; 775 case TCG_COND_LTU: 776 tcg_out_opc_reg(s, OPC_SLTU, ret, arg1, arg2); 777 break; 778 case TCG_COND_GEU: 779 tcg_out_opc_reg(s, OPC_SLTU, ret, arg1, arg2); 780 tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1); 781 break; 782 case TCG_COND_LEU: 783 tcg_out_opc_reg(s, OPC_SLTU, ret, arg2, arg1); 784 tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1); 785 break; 786 case TCG_COND_GTU: 787 tcg_out_opc_reg(s, OPC_SLTU, ret, arg2, arg1); 788 break; 789 default: 790 g_assert_not_reached(); 791 break; 792 } 793} 794 795static void tcg_out_call_int(TCGContext *s, const tcg_insn_unit *arg, bool tail) 796{ 797 TCGReg link = tail ? TCG_REG_ZERO : TCG_REG_RA; 798 ptrdiff_t offset = tcg_pcrel_diff(s, arg); 799 int ret; 800 801 tcg_debug_assert((offset & 1) == 0); 802 if (offset == sextreg(offset, 0, 20)) { 803 /* short jump: -2097150 to 2097152 */ 804 tcg_out_opc_jump(s, OPC_JAL, link, offset); 805 } else if (offset == (int32_t)offset) { 806 /* long jump: -2147483646 to 2147483648 */ 807 tcg_out_opc_upper(s, OPC_AUIPC, TCG_REG_TMP0, 0); 808 tcg_out_opc_imm(s, OPC_JALR, link, TCG_REG_TMP0, 0); 809 ret = reloc_call(s->code_ptr - 2, arg); 810 tcg_debug_assert(ret == true); 811 } else { 812 /* far jump: 64-bit */ 813 tcg_target_long imm = sextreg((tcg_target_long)arg, 0, 12); 814 tcg_target_long base = (tcg_target_long)arg - imm; 815 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP0, base); 816 tcg_out_opc_imm(s, OPC_JALR, link, TCG_REG_TMP0, imm); 817 } 818} 819 820static void tcg_out_call(TCGContext *s, const tcg_insn_unit *arg, 821 const TCGHelperInfo *info) 822{ 823 tcg_out_call_int(s, arg, false); 824} 825 826static void tcg_out_mb(TCGContext *s, TCGArg a0) 827{ 828 tcg_insn_unit insn = OPC_FENCE; 829 830 if (a0 & TCG_MO_LD_LD) { 831 insn |= 0x02200000; 832 } 833 if (a0 & TCG_MO_ST_LD) { 834 insn |= 0x01200000; 835 } 836 if (a0 & TCG_MO_LD_ST) { 837 insn |= 0x02100000; 838 } 839 if (a0 & TCG_MO_ST_ST) { 840 insn |= 0x02200000; 841 } 842 tcg_out32(s, insn); 843} 844 845/* 846 * Load/store and TLB 847 */ 848 849static void tcg_out_goto(TCGContext *s, const tcg_insn_unit *target) 850{ 851 tcg_out_opc_jump(s, OPC_JAL, TCG_REG_ZERO, 0); 852 bool ok = reloc_jimm20(s->code_ptr - 1, target); 853 tcg_debug_assert(ok); 854} 855 856bool tcg_target_has_memory_bswap(MemOp memop) 857{ 858 return false; 859} 860 861/* We have three temps, we might as well expose them. */ 862static const TCGLdstHelperParam ldst_helper_param = { 863 .ntmp = 3, .tmp = { TCG_REG_TMP0, TCG_REG_TMP1, TCG_REG_TMP2 } 864}; 865 866static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l) 867{ 868 MemOp opc = get_memop(l->oi); 869 870 /* resolve label address */ 871 if (!reloc_sbimm12(l->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) { 872 return false; 873 } 874 875 /* call load helper */ 876 tcg_out_ld_helper_args(s, l, &ldst_helper_param); 877 tcg_out_call_int(s, qemu_ld_helpers[opc & MO_SSIZE], false); 878 tcg_out_ld_helper_ret(s, l, true, &ldst_helper_param); 879 880 tcg_out_goto(s, l->raddr); 881 return true; 882} 883 884static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l) 885{ 886 MemOp opc = get_memop(l->oi); 887 888 /* resolve label address */ 889 if (!reloc_sbimm12(l->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) { 890 return false; 891 } 892 893 /* call store helper */ 894 tcg_out_st_helper_args(s, l, &ldst_helper_param); 895 tcg_out_call_int(s, qemu_st_helpers[opc & MO_SIZE], false); 896 897 tcg_out_goto(s, l->raddr); 898 return true; 899} 900 901/* 902 * For softmmu, perform the TLB load and compare. 903 * For useronly, perform any required alignment tests. 904 * In both cases, return a TCGLabelQemuLdst structure if the slow path 905 * is required and fill in @h with the host address for the fast path. 906 */ 907static TCGLabelQemuLdst *prepare_host_addr(TCGContext *s, TCGReg *pbase, 908 TCGReg addr_reg, MemOpIdx oi, 909 bool is_ld) 910{ 911 TCGLabelQemuLdst *ldst = NULL; 912 MemOp opc = get_memop(oi); 913 unsigned a_bits = get_alignment_bits(opc); 914 unsigned a_mask = (1u << a_bits) - 1; 915 916#ifdef CONFIG_SOFTMMU 917 unsigned s_bits = opc & MO_SIZE; 918 unsigned s_mask = (1u << s_bits) - 1; 919 int mem_index = get_mmuidx(oi); 920 int fast_ofs = TLB_MASK_TABLE_OFS(mem_index); 921 int mask_ofs = fast_ofs + offsetof(CPUTLBDescFast, mask); 922 int table_ofs = fast_ofs + offsetof(CPUTLBDescFast, table); 923 int compare_mask; 924 TCGReg addr_adj; 925 926 ldst = new_ldst_label(s); 927 ldst->is_ld = is_ld; 928 ldst->oi = oi; 929 ldst->addrlo_reg = addr_reg; 930 931 QEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) > 0); 932 QEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) < -(1 << 11)); 933 tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP0, TCG_AREG0, mask_ofs); 934 tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP1, TCG_AREG0, table_ofs); 935 936 tcg_out_opc_imm(s, OPC_SRLI, TCG_REG_TMP2, addr_reg, 937 TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS); 938 tcg_out_opc_reg(s, OPC_AND, TCG_REG_TMP2, TCG_REG_TMP2, TCG_REG_TMP0); 939 tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP2, TCG_REG_TMP2, TCG_REG_TMP1); 940 941 /* 942 * For aligned accesses, we check the first byte and include the alignment 943 * bits within the address. For unaligned access, we check that we don't 944 * cross pages using the address of the last byte of the access. 945 */ 946 addr_adj = addr_reg; 947 if (a_bits < s_bits) { 948 addr_adj = TCG_REG_TMP0; 949 tcg_out_opc_imm(s, TARGET_LONG_BITS == 32 ? OPC_ADDIW : OPC_ADDI, 950 addr_adj, addr_reg, s_mask - a_mask); 951 } 952 compare_mask = TARGET_PAGE_MASK | a_mask; 953 if (compare_mask == sextreg(compare_mask, 0, 12)) { 954 tcg_out_opc_imm(s, OPC_ANDI, TCG_REG_TMP1, addr_adj, compare_mask); 955 } else { 956 tcg_out_movi(s, TCG_TYPE_TL, TCG_REG_TMP1, compare_mask); 957 tcg_out_opc_reg(s, OPC_AND, TCG_REG_TMP1, TCG_REG_TMP1, addr_adj); 958 } 959 960 /* Load the tlb comparator and the addend. */ 961 tcg_out_ld(s, TCG_TYPE_TL, TCG_REG_TMP0, TCG_REG_TMP2, 962 is_ld ? offsetof(CPUTLBEntry, addr_read) 963 : offsetof(CPUTLBEntry, addr_write)); 964 tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP2, TCG_REG_TMP2, 965 offsetof(CPUTLBEntry, addend)); 966 967 /* Compare masked address with the TLB entry. */ 968 ldst->label_ptr[0] = s->code_ptr; 969 tcg_out_opc_branch(s, OPC_BNE, TCG_REG_TMP0, TCG_REG_TMP1, 0); 970 971 /* TLB Hit - translate address using addend. */ 972 addr_adj = addr_reg; 973 if (TARGET_LONG_BITS == 32) { 974 addr_adj = TCG_REG_TMP0; 975 tcg_out_ext32u(s, addr_adj, addr_reg); 976 } 977 tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP0, TCG_REG_TMP2, addr_adj); 978 *pbase = TCG_REG_TMP0; 979#else 980 if (a_mask) { 981 ldst = new_ldst_label(s); 982 ldst->is_ld = is_ld; 983 ldst->oi = oi; 984 ldst->addrlo_reg = addr_reg; 985 986 /* We are expecting a_bits max 7, so we can always use andi. */ 987 tcg_debug_assert(a_bits < 12); 988 tcg_out_opc_imm(s, OPC_ANDI, TCG_REG_TMP1, addr_reg, a_mask); 989 990 ldst->label_ptr[0] = s->code_ptr; 991 tcg_out_opc_branch(s, OPC_BNE, TCG_REG_TMP1, TCG_REG_ZERO, 0); 992 } 993 994 TCGReg base = addr_reg; 995 if (TARGET_LONG_BITS == 32) { 996 tcg_out_ext32u(s, TCG_REG_TMP0, base); 997 base = TCG_REG_TMP0; 998 } 999 if (guest_base != 0) { 1000 tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP0, TCG_GUEST_BASE_REG, base); 1001 base = TCG_REG_TMP0; 1002 } 1003 *pbase = base; 1004#endif 1005 1006 return ldst; 1007} 1008 1009static void tcg_out_qemu_ld_direct(TCGContext *s, TCGReg val, 1010 TCGReg base, MemOp opc, TCGType type) 1011{ 1012 /* Byte swapping is left to middle-end expansion. */ 1013 tcg_debug_assert((opc & MO_BSWAP) == 0); 1014 1015 switch (opc & (MO_SSIZE)) { 1016 case MO_UB: 1017 tcg_out_opc_imm(s, OPC_LBU, val, base, 0); 1018 break; 1019 case MO_SB: 1020 tcg_out_opc_imm(s, OPC_LB, val, base, 0); 1021 break; 1022 case MO_UW: 1023 tcg_out_opc_imm(s, OPC_LHU, val, base, 0); 1024 break; 1025 case MO_SW: 1026 tcg_out_opc_imm(s, OPC_LH, val, base, 0); 1027 break; 1028 case MO_UL: 1029 if (type == TCG_TYPE_I64) { 1030 tcg_out_opc_imm(s, OPC_LWU, val, base, 0); 1031 break; 1032 } 1033 /* FALLTHRU */ 1034 case MO_SL: 1035 tcg_out_opc_imm(s, OPC_LW, val, base, 0); 1036 break; 1037 case MO_UQ: 1038 tcg_out_opc_imm(s, OPC_LD, val, base, 0); 1039 break; 1040 default: 1041 g_assert_not_reached(); 1042 } 1043} 1044 1045static void tcg_out_qemu_ld(TCGContext *s, TCGReg data_reg, TCGReg addr_reg, 1046 MemOpIdx oi, TCGType data_type) 1047{ 1048 TCGLabelQemuLdst *ldst; 1049 TCGReg base; 1050 1051 ldst = prepare_host_addr(s, &base, addr_reg, oi, true); 1052 tcg_out_qemu_ld_direct(s, data_reg, base, get_memop(oi), data_type); 1053 1054 if (ldst) { 1055 ldst->type = data_type; 1056 ldst->datalo_reg = data_reg; 1057 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr); 1058 } 1059} 1060 1061static void tcg_out_qemu_st_direct(TCGContext *s, TCGReg val, 1062 TCGReg base, MemOp opc) 1063{ 1064 /* Byte swapping is left to middle-end expansion. */ 1065 tcg_debug_assert((opc & MO_BSWAP) == 0); 1066 1067 switch (opc & (MO_SSIZE)) { 1068 case MO_8: 1069 tcg_out_opc_store(s, OPC_SB, base, val, 0); 1070 break; 1071 case MO_16: 1072 tcg_out_opc_store(s, OPC_SH, base, val, 0); 1073 break; 1074 case MO_32: 1075 tcg_out_opc_store(s, OPC_SW, base, val, 0); 1076 break; 1077 case MO_64: 1078 tcg_out_opc_store(s, OPC_SD, base, val, 0); 1079 break; 1080 default: 1081 g_assert_not_reached(); 1082 } 1083} 1084 1085static void tcg_out_qemu_st(TCGContext *s, TCGReg data_reg, TCGReg addr_reg, 1086 MemOpIdx oi, TCGType data_type) 1087{ 1088 TCGLabelQemuLdst *ldst; 1089 TCGReg base; 1090 1091 ldst = prepare_host_addr(s, &base, addr_reg, oi, false); 1092 tcg_out_qemu_st_direct(s, data_reg, base, get_memop(oi)); 1093 1094 if (ldst) { 1095 ldst->type = data_type; 1096 ldst->datalo_reg = data_reg; 1097 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr); 1098 } 1099} 1100 1101static const tcg_insn_unit *tb_ret_addr; 1102 1103static void tcg_out_exit_tb(TCGContext *s, uintptr_t a0) 1104{ 1105 /* Reuse the zeroing that exists for goto_ptr. */ 1106 if (a0 == 0) { 1107 tcg_out_call_int(s, tcg_code_gen_epilogue, true); 1108 } else { 1109 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_A0, a0); 1110 tcg_out_call_int(s, tb_ret_addr, true); 1111 } 1112} 1113 1114static void tcg_out_goto_tb(TCGContext *s, int which) 1115{ 1116 /* Direct branch will be patched by tb_target_set_jmp_target. */ 1117 set_jmp_insn_offset(s, which); 1118 tcg_out32(s, OPC_JAL); 1119 1120 /* When branch is out of range, fall through to indirect. */ 1121 tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP0, TCG_REG_ZERO, 1122 get_jmp_target_addr(s, which)); 1123 tcg_out_opc_imm(s, OPC_JALR, TCG_REG_ZERO, TCG_REG_TMP0, 0); 1124 set_jmp_reset_offset(s, which); 1125} 1126 1127void tb_target_set_jmp_target(const TranslationBlock *tb, int n, 1128 uintptr_t jmp_rx, uintptr_t jmp_rw) 1129{ 1130 uintptr_t addr = tb->jmp_target_addr[n]; 1131 ptrdiff_t offset = addr - jmp_rx; 1132 tcg_insn_unit insn; 1133 1134 /* Either directly branch, or fall through to indirect branch. */ 1135 if (offset == sextreg(offset, 0, 20)) { 1136 insn = encode_uj(OPC_JAL, TCG_REG_ZERO, offset); 1137 } else { 1138 insn = OPC_NOP; 1139 } 1140 qatomic_set((uint32_t *)jmp_rw, insn); 1141 flush_idcache_range(jmp_rx, jmp_rw, 4); 1142} 1143 1144static void tcg_out_op(TCGContext *s, TCGOpcode opc, 1145 const TCGArg args[TCG_MAX_OP_ARGS], 1146 const int const_args[TCG_MAX_OP_ARGS]) 1147{ 1148 TCGArg a0 = args[0]; 1149 TCGArg a1 = args[1]; 1150 TCGArg a2 = args[2]; 1151 int c2 = const_args[2]; 1152 1153 switch (opc) { 1154 case INDEX_op_goto_ptr: 1155 tcg_out_opc_imm(s, OPC_JALR, TCG_REG_ZERO, a0, 0); 1156 break; 1157 1158 case INDEX_op_br: 1159 tcg_out_reloc(s, s->code_ptr, R_RISCV_JAL, arg_label(a0), 0); 1160 tcg_out_opc_jump(s, OPC_JAL, TCG_REG_ZERO, 0); 1161 break; 1162 1163 case INDEX_op_ld8u_i32: 1164 case INDEX_op_ld8u_i64: 1165 tcg_out_ldst(s, OPC_LBU, a0, a1, a2); 1166 break; 1167 case INDEX_op_ld8s_i32: 1168 case INDEX_op_ld8s_i64: 1169 tcg_out_ldst(s, OPC_LB, a0, a1, a2); 1170 break; 1171 case INDEX_op_ld16u_i32: 1172 case INDEX_op_ld16u_i64: 1173 tcg_out_ldst(s, OPC_LHU, a0, a1, a2); 1174 break; 1175 case INDEX_op_ld16s_i32: 1176 case INDEX_op_ld16s_i64: 1177 tcg_out_ldst(s, OPC_LH, a0, a1, a2); 1178 break; 1179 case INDEX_op_ld32u_i64: 1180 tcg_out_ldst(s, OPC_LWU, a0, a1, a2); 1181 break; 1182 case INDEX_op_ld_i32: 1183 case INDEX_op_ld32s_i64: 1184 tcg_out_ldst(s, OPC_LW, a0, a1, a2); 1185 break; 1186 case INDEX_op_ld_i64: 1187 tcg_out_ldst(s, OPC_LD, a0, a1, a2); 1188 break; 1189 1190 case INDEX_op_st8_i32: 1191 case INDEX_op_st8_i64: 1192 tcg_out_ldst(s, OPC_SB, a0, a1, a2); 1193 break; 1194 case INDEX_op_st16_i32: 1195 case INDEX_op_st16_i64: 1196 tcg_out_ldst(s, OPC_SH, a0, a1, a2); 1197 break; 1198 case INDEX_op_st_i32: 1199 case INDEX_op_st32_i64: 1200 tcg_out_ldst(s, OPC_SW, a0, a1, a2); 1201 break; 1202 case INDEX_op_st_i64: 1203 tcg_out_ldst(s, OPC_SD, a0, a1, a2); 1204 break; 1205 1206 case INDEX_op_add_i32: 1207 if (c2) { 1208 tcg_out_opc_imm(s, OPC_ADDIW, a0, a1, a2); 1209 } else { 1210 tcg_out_opc_reg(s, OPC_ADDW, a0, a1, a2); 1211 } 1212 break; 1213 case INDEX_op_add_i64: 1214 if (c2) { 1215 tcg_out_opc_imm(s, OPC_ADDI, a0, a1, a2); 1216 } else { 1217 tcg_out_opc_reg(s, OPC_ADD, a0, a1, a2); 1218 } 1219 break; 1220 1221 case INDEX_op_sub_i32: 1222 if (c2) { 1223 tcg_out_opc_imm(s, OPC_ADDIW, a0, a1, -a2); 1224 } else { 1225 tcg_out_opc_reg(s, OPC_SUBW, a0, a1, a2); 1226 } 1227 break; 1228 case INDEX_op_sub_i64: 1229 if (c2) { 1230 tcg_out_opc_imm(s, OPC_ADDI, a0, a1, -a2); 1231 } else { 1232 tcg_out_opc_reg(s, OPC_SUB, a0, a1, a2); 1233 } 1234 break; 1235 1236 case INDEX_op_and_i32: 1237 case INDEX_op_and_i64: 1238 if (c2) { 1239 tcg_out_opc_imm(s, OPC_ANDI, a0, a1, a2); 1240 } else { 1241 tcg_out_opc_reg(s, OPC_AND, a0, a1, a2); 1242 } 1243 break; 1244 1245 case INDEX_op_or_i32: 1246 case INDEX_op_or_i64: 1247 if (c2) { 1248 tcg_out_opc_imm(s, OPC_ORI, a0, a1, a2); 1249 } else { 1250 tcg_out_opc_reg(s, OPC_OR, a0, a1, a2); 1251 } 1252 break; 1253 1254 case INDEX_op_xor_i32: 1255 case INDEX_op_xor_i64: 1256 if (c2) { 1257 tcg_out_opc_imm(s, OPC_XORI, a0, a1, a2); 1258 } else { 1259 tcg_out_opc_reg(s, OPC_XOR, a0, a1, a2); 1260 } 1261 break; 1262 1263 case INDEX_op_not_i32: 1264 case INDEX_op_not_i64: 1265 tcg_out_opc_imm(s, OPC_XORI, a0, a1, -1); 1266 break; 1267 1268 case INDEX_op_neg_i32: 1269 tcg_out_opc_reg(s, OPC_SUBW, a0, TCG_REG_ZERO, a1); 1270 break; 1271 case INDEX_op_neg_i64: 1272 tcg_out_opc_reg(s, OPC_SUB, a0, TCG_REG_ZERO, a1); 1273 break; 1274 1275 case INDEX_op_mul_i32: 1276 tcg_out_opc_reg(s, OPC_MULW, a0, a1, a2); 1277 break; 1278 case INDEX_op_mul_i64: 1279 tcg_out_opc_reg(s, OPC_MUL, a0, a1, a2); 1280 break; 1281 1282 case INDEX_op_div_i32: 1283 tcg_out_opc_reg(s, OPC_DIVW, a0, a1, a2); 1284 break; 1285 case INDEX_op_div_i64: 1286 tcg_out_opc_reg(s, OPC_DIV, a0, a1, a2); 1287 break; 1288 1289 case INDEX_op_divu_i32: 1290 tcg_out_opc_reg(s, OPC_DIVUW, a0, a1, a2); 1291 break; 1292 case INDEX_op_divu_i64: 1293 tcg_out_opc_reg(s, OPC_DIVU, a0, a1, a2); 1294 break; 1295 1296 case INDEX_op_rem_i32: 1297 tcg_out_opc_reg(s, OPC_REMW, a0, a1, a2); 1298 break; 1299 case INDEX_op_rem_i64: 1300 tcg_out_opc_reg(s, OPC_REM, a0, a1, a2); 1301 break; 1302 1303 case INDEX_op_remu_i32: 1304 tcg_out_opc_reg(s, OPC_REMUW, a0, a1, a2); 1305 break; 1306 case INDEX_op_remu_i64: 1307 tcg_out_opc_reg(s, OPC_REMU, a0, a1, a2); 1308 break; 1309 1310 case INDEX_op_shl_i32: 1311 if (c2) { 1312 tcg_out_opc_imm(s, OPC_SLLIW, a0, a1, a2 & 0x1f); 1313 } else { 1314 tcg_out_opc_reg(s, OPC_SLLW, a0, a1, a2); 1315 } 1316 break; 1317 case INDEX_op_shl_i64: 1318 if (c2) { 1319 tcg_out_opc_imm(s, OPC_SLLI, a0, a1, a2 & 0x3f); 1320 } else { 1321 tcg_out_opc_reg(s, OPC_SLL, a0, a1, a2); 1322 } 1323 break; 1324 1325 case INDEX_op_shr_i32: 1326 if (c2) { 1327 tcg_out_opc_imm(s, OPC_SRLIW, a0, a1, a2 & 0x1f); 1328 } else { 1329 tcg_out_opc_reg(s, OPC_SRLW, a0, a1, a2); 1330 } 1331 break; 1332 case INDEX_op_shr_i64: 1333 if (c2) { 1334 tcg_out_opc_imm(s, OPC_SRLI, a0, a1, a2 & 0x3f); 1335 } else { 1336 tcg_out_opc_reg(s, OPC_SRL, a0, a1, a2); 1337 } 1338 break; 1339 1340 case INDEX_op_sar_i32: 1341 if (c2) { 1342 tcg_out_opc_imm(s, OPC_SRAIW, a0, a1, a2 & 0x1f); 1343 } else { 1344 tcg_out_opc_reg(s, OPC_SRAW, a0, a1, a2); 1345 } 1346 break; 1347 case INDEX_op_sar_i64: 1348 if (c2) { 1349 tcg_out_opc_imm(s, OPC_SRAI, a0, a1, a2 & 0x3f); 1350 } else { 1351 tcg_out_opc_reg(s, OPC_SRA, a0, a1, a2); 1352 } 1353 break; 1354 1355 case INDEX_op_add2_i32: 1356 tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5], 1357 const_args[4], const_args[5], false, true); 1358 break; 1359 case INDEX_op_add2_i64: 1360 tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5], 1361 const_args[4], const_args[5], false, false); 1362 break; 1363 case INDEX_op_sub2_i32: 1364 tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5], 1365 const_args[4], const_args[5], true, true); 1366 break; 1367 case INDEX_op_sub2_i64: 1368 tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5], 1369 const_args[4], const_args[5], true, false); 1370 break; 1371 1372 case INDEX_op_brcond_i32: 1373 case INDEX_op_brcond_i64: 1374 tcg_out_brcond(s, a2, a0, a1, arg_label(args[3])); 1375 break; 1376 1377 case INDEX_op_setcond_i32: 1378 case INDEX_op_setcond_i64: 1379 tcg_out_setcond(s, args[3], a0, a1, a2); 1380 break; 1381 1382 case INDEX_op_qemu_ld_i32: 1383 tcg_out_qemu_ld(s, a0, a1, a2, TCG_TYPE_I32); 1384 break; 1385 case INDEX_op_qemu_ld_i64: 1386 tcg_out_qemu_ld(s, a0, a1, a2, TCG_TYPE_I64); 1387 break; 1388 case INDEX_op_qemu_st_i32: 1389 tcg_out_qemu_st(s, a0, a1, a2, TCG_TYPE_I32); 1390 break; 1391 case INDEX_op_qemu_st_i64: 1392 tcg_out_qemu_st(s, a0, a1, a2, TCG_TYPE_I64); 1393 break; 1394 1395 case INDEX_op_extrh_i64_i32: 1396 tcg_out_opc_imm(s, OPC_SRAI, a0, a1, 32); 1397 break; 1398 1399 case INDEX_op_mulsh_i32: 1400 case INDEX_op_mulsh_i64: 1401 tcg_out_opc_reg(s, OPC_MULH, a0, a1, a2); 1402 break; 1403 1404 case INDEX_op_muluh_i32: 1405 case INDEX_op_muluh_i64: 1406 tcg_out_opc_reg(s, OPC_MULHU, a0, a1, a2); 1407 break; 1408 1409 case INDEX_op_mb: 1410 tcg_out_mb(s, a0); 1411 break; 1412 1413 case INDEX_op_mov_i32: /* Always emitted via tcg_out_mov. */ 1414 case INDEX_op_mov_i64: 1415 case INDEX_op_call: /* Always emitted via tcg_out_call. */ 1416 case INDEX_op_exit_tb: /* Always emitted via tcg_out_exit_tb. */ 1417 case INDEX_op_goto_tb: /* Always emitted via tcg_out_goto_tb. */ 1418 case INDEX_op_ext8s_i32: /* Always emitted via tcg_reg_alloc_op. */ 1419 case INDEX_op_ext8s_i64: 1420 case INDEX_op_ext8u_i32: 1421 case INDEX_op_ext8u_i64: 1422 case INDEX_op_ext16s_i32: 1423 case INDEX_op_ext16s_i64: 1424 case INDEX_op_ext16u_i32: 1425 case INDEX_op_ext16u_i64: 1426 case INDEX_op_ext32s_i64: 1427 case INDEX_op_ext32u_i64: 1428 case INDEX_op_ext_i32_i64: 1429 case INDEX_op_extu_i32_i64: 1430 case INDEX_op_extrl_i64_i32: 1431 default: 1432 g_assert_not_reached(); 1433 } 1434} 1435 1436static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op) 1437{ 1438 switch (op) { 1439 case INDEX_op_goto_ptr: 1440 return C_O0_I1(r); 1441 1442 case INDEX_op_ld8u_i32: 1443 case INDEX_op_ld8s_i32: 1444 case INDEX_op_ld16u_i32: 1445 case INDEX_op_ld16s_i32: 1446 case INDEX_op_ld_i32: 1447 case INDEX_op_not_i32: 1448 case INDEX_op_neg_i32: 1449 case INDEX_op_ld8u_i64: 1450 case INDEX_op_ld8s_i64: 1451 case INDEX_op_ld16u_i64: 1452 case INDEX_op_ld16s_i64: 1453 case INDEX_op_ld32s_i64: 1454 case INDEX_op_ld32u_i64: 1455 case INDEX_op_ld_i64: 1456 case INDEX_op_not_i64: 1457 case INDEX_op_neg_i64: 1458 case INDEX_op_ext8u_i32: 1459 case INDEX_op_ext8u_i64: 1460 case INDEX_op_ext16u_i32: 1461 case INDEX_op_ext16u_i64: 1462 case INDEX_op_ext32u_i64: 1463 case INDEX_op_extu_i32_i64: 1464 case INDEX_op_ext8s_i32: 1465 case INDEX_op_ext8s_i64: 1466 case INDEX_op_ext16s_i32: 1467 case INDEX_op_ext16s_i64: 1468 case INDEX_op_ext32s_i64: 1469 case INDEX_op_extrl_i64_i32: 1470 case INDEX_op_extrh_i64_i32: 1471 case INDEX_op_ext_i32_i64: 1472 return C_O1_I1(r, r); 1473 1474 case INDEX_op_st8_i32: 1475 case INDEX_op_st16_i32: 1476 case INDEX_op_st_i32: 1477 case INDEX_op_st8_i64: 1478 case INDEX_op_st16_i64: 1479 case INDEX_op_st32_i64: 1480 case INDEX_op_st_i64: 1481 return C_O0_I2(rZ, r); 1482 1483 case INDEX_op_add_i32: 1484 case INDEX_op_and_i32: 1485 case INDEX_op_or_i32: 1486 case INDEX_op_xor_i32: 1487 case INDEX_op_add_i64: 1488 case INDEX_op_and_i64: 1489 case INDEX_op_or_i64: 1490 case INDEX_op_xor_i64: 1491 return C_O1_I2(r, r, rI); 1492 1493 case INDEX_op_sub_i32: 1494 case INDEX_op_sub_i64: 1495 return C_O1_I2(r, rZ, rN); 1496 1497 case INDEX_op_mul_i32: 1498 case INDEX_op_mulsh_i32: 1499 case INDEX_op_muluh_i32: 1500 case INDEX_op_div_i32: 1501 case INDEX_op_divu_i32: 1502 case INDEX_op_rem_i32: 1503 case INDEX_op_remu_i32: 1504 case INDEX_op_setcond_i32: 1505 case INDEX_op_mul_i64: 1506 case INDEX_op_mulsh_i64: 1507 case INDEX_op_muluh_i64: 1508 case INDEX_op_div_i64: 1509 case INDEX_op_divu_i64: 1510 case INDEX_op_rem_i64: 1511 case INDEX_op_remu_i64: 1512 case INDEX_op_setcond_i64: 1513 return C_O1_I2(r, rZ, rZ); 1514 1515 case INDEX_op_shl_i32: 1516 case INDEX_op_shr_i32: 1517 case INDEX_op_sar_i32: 1518 case INDEX_op_shl_i64: 1519 case INDEX_op_shr_i64: 1520 case INDEX_op_sar_i64: 1521 return C_O1_I2(r, r, ri); 1522 1523 case INDEX_op_brcond_i32: 1524 case INDEX_op_brcond_i64: 1525 return C_O0_I2(rZ, rZ); 1526 1527 case INDEX_op_add2_i32: 1528 case INDEX_op_add2_i64: 1529 case INDEX_op_sub2_i32: 1530 case INDEX_op_sub2_i64: 1531 return C_O2_I4(r, r, rZ, rZ, rM, rM); 1532 1533 case INDEX_op_qemu_ld_i32: 1534 case INDEX_op_qemu_ld_i64: 1535 return C_O1_I1(r, r); 1536 case INDEX_op_qemu_st_i32: 1537 case INDEX_op_qemu_st_i64: 1538 return C_O0_I2(rZ, r); 1539 1540 default: 1541 g_assert_not_reached(); 1542 } 1543} 1544 1545static const int tcg_target_callee_save_regs[] = { 1546 TCG_REG_S0, /* used for the global env (TCG_AREG0) */ 1547 TCG_REG_S1, 1548 TCG_REG_S2, 1549 TCG_REG_S3, 1550 TCG_REG_S4, 1551 TCG_REG_S5, 1552 TCG_REG_S6, 1553 TCG_REG_S7, 1554 TCG_REG_S8, 1555 TCG_REG_S9, 1556 TCG_REG_S10, 1557 TCG_REG_S11, 1558 TCG_REG_RA, /* should be last for ABI compliance */ 1559}; 1560 1561/* Stack frame parameters. */ 1562#define REG_SIZE (TCG_TARGET_REG_BITS / 8) 1563#define SAVE_SIZE ((int)ARRAY_SIZE(tcg_target_callee_save_regs) * REG_SIZE) 1564#define TEMP_SIZE (CPU_TEMP_BUF_NLONGS * (int)sizeof(long)) 1565#define FRAME_SIZE ((TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE + SAVE_SIZE \ 1566 + TCG_TARGET_STACK_ALIGN - 1) \ 1567 & -TCG_TARGET_STACK_ALIGN) 1568#define SAVE_OFS (TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE) 1569 1570/* We're expecting to be able to use an immediate for frame allocation. */ 1571QEMU_BUILD_BUG_ON(FRAME_SIZE > 0x7ff); 1572 1573/* Generate global QEMU prologue and epilogue code */ 1574static void tcg_target_qemu_prologue(TCGContext *s) 1575{ 1576 int i; 1577 1578 tcg_set_frame(s, TCG_REG_SP, TCG_STATIC_CALL_ARGS_SIZE, TEMP_SIZE); 1579 1580 /* TB prologue */ 1581 tcg_out_opc_imm(s, OPC_ADDI, TCG_REG_SP, TCG_REG_SP, -FRAME_SIZE); 1582 for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) { 1583 tcg_out_st(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i], 1584 TCG_REG_SP, SAVE_OFS + i * REG_SIZE); 1585 } 1586 1587#if !defined(CONFIG_SOFTMMU) 1588 tcg_out_movi(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG, guest_base); 1589 tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG); 1590#endif 1591 1592 /* Call generated code */ 1593 tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]); 1594 tcg_out_opc_imm(s, OPC_JALR, TCG_REG_ZERO, tcg_target_call_iarg_regs[1], 0); 1595 1596 /* Return path for goto_ptr. Set return value to 0 */ 1597 tcg_code_gen_epilogue = tcg_splitwx_to_rx(s->code_ptr); 1598 tcg_out_mov(s, TCG_TYPE_REG, TCG_REG_A0, TCG_REG_ZERO); 1599 1600 /* TB epilogue */ 1601 tb_ret_addr = tcg_splitwx_to_rx(s->code_ptr); 1602 for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) { 1603 tcg_out_ld(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i], 1604 TCG_REG_SP, SAVE_OFS + i * REG_SIZE); 1605 } 1606 1607 tcg_out_opc_imm(s, OPC_ADDI, TCG_REG_SP, TCG_REG_SP, FRAME_SIZE); 1608 tcg_out_opc_imm(s, OPC_JALR, TCG_REG_ZERO, TCG_REG_RA, 0); 1609} 1610 1611static void tcg_target_init(TCGContext *s) 1612{ 1613 tcg_target_available_regs[TCG_TYPE_I32] = 0xffffffff; 1614 tcg_target_available_regs[TCG_TYPE_I64] = 0xffffffff; 1615 1616 tcg_target_call_clobber_regs = -1u; 1617 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S0); 1618 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S1); 1619 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S2); 1620 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S3); 1621 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S4); 1622 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S5); 1623 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S6); 1624 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S7); 1625 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S8); 1626 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S9); 1627 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S10); 1628 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S11); 1629 1630 s->reserved_regs = 0; 1631 tcg_regset_set_reg(s->reserved_regs, TCG_REG_ZERO); 1632 tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP0); 1633 tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP1); 1634 tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP2); 1635 tcg_regset_set_reg(s->reserved_regs, TCG_REG_SP); 1636 tcg_regset_set_reg(s->reserved_regs, TCG_REG_GP); 1637 tcg_regset_set_reg(s->reserved_regs, TCG_REG_TP); 1638} 1639 1640typedef struct { 1641 DebugFrameHeader h; 1642 uint8_t fde_def_cfa[4]; 1643 uint8_t fde_reg_ofs[ARRAY_SIZE(tcg_target_callee_save_regs) * 2]; 1644} DebugFrame; 1645 1646#define ELF_HOST_MACHINE EM_RISCV 1647 1648static const DebugFrame debug_frame = { 1649 .h.cie.len = sizeof(DebugFrameCIE) - 4, /* length after .len member */ 1650 .h.cie.id = -1, 1651 .h.cie.version = 1, 1652 .h.cie.code_align = 1, 1653 .h.cie.data_align = -(TCG_TARGET_REG_BITS / 8) & 0x7f, /* sleb128 */ 1654 .h.cie.return_column = TCG_REG_RA, 1655 1656 /* Total FDE size does not include the "len" member. */ 1657 .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset), 1658 1659 .fde_def_cfa = { 1660 12, TCG_REG_SP, /* DW_CFA_def_cfa sp, ... */ 1661 (FRAME_SIZE & 0x7f) | 0x80, /* ... uleb128 FRAME_SIZE */ 1662 (FRAME_SIZE >> 7) 1663 }, 1664 .fde_reg_ofs = { 1665 0x80 + 9, 12, /* DW_CFA_offset, s1, -96 */ 1666 0x80 + 18, 11, /* DW_CFA_offset, s2, -88 */ 1667 0x80 + 19, 10, /* DW_CFA_offset, s3, -80 */ 1668 0x80 + 20, 9, /* DW_CFA_offset, s4, -72 */ 1669 0x80 + 21, 8, /* DW_CFA_offset, s5, -64 */ 1670 0x80 + 22, 7, /* DW_CFA_offset, s6, -56 */ 1671 0x80 + 23, 6, /* DW_CFA_offset, s7, -48 */ 1672 0x80 + 24, 5, /* DW_CFA_offset, s8, -40 */ 1673 0x80 + 25, 4, /* DW_CFA_offset, s9, -32 */ 1674 0x80 + 26, 3, /* DW_CFA_offset, s10, -24 */ 1675 0x80 + 27, 2, /* DW_CFA_offset, s11, -16 */ 1676 0x80 + 1 , 1, /* DW_CFA_offset, ra, -8 */ 1677 } 1678}; 1679 1680void tcg_register_jit(const void *buf, size_t buf_size) 1681{ 1682 tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame)); 1683} 1684