2 * Tiny Code Generator for QEMU
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
9 * Based on i386/tcg-target.c and mips/tcg-target.c
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:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
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
30 #include "../tcg-pool.c.inc"
32 #ifdef CONFIG_DEBUG_TCG
33 static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
69 static const int tcg_target_reg_alloc_order[] = {
70 /* Call saved registers */
71 /* TCG_REG_S0 reservered for TCG_AREG0 */
84 /* Call clobbered registers */
93 /* Argument registers */
104 static const int tcg_target_call_iarg_regs[] = {
115 static const int tcg_target_call_oarg_regs[] = {
120 #define TCG_CT_CONST_ZERO 0x100
121 #define TCG_CT_CONST_S12 0x200
122 #define TCG_CT_CONST_N12 0x400
123 #define TCG_CT_CONST_M12 0x800
125 #define ALL_GENERAL_REGS MAKE_64BIT_MASK(0, 32)
127 * For softmmu, we need to avoid conflicts with the first 5
128 * argument registers to call the helper. Some of these are
129 * also used for the tlb lookup.
131 #ifdef CONFIG_SOFTMMU
132 #define SOFTMMU_RESERVE_REGS MAKE_64BIT_MASK(TCG_REG_A0, 5)
134 #define SOFTMMU_RESERVE_REGS 0
138 static inline tcg_target_long sextreg(tcg_target_long val, int pos, int len)
140 if (TCG_TARGET_REG_BITS == 32) {
141 return sextract32(val, pos, len);
143 return sextract64(val, pos, len);
147 /* test if a constant matches the constraint */
148 static int tcg_target_const_match(tcg_target_long val, TCGType type,
149 const TCGArgConstraint *arg_ct)
152 if (ct & TCG_CT_CONST) {
155 if ((ct & TCG_CT_CONST_ZERO) && val == 0) {
158 if ((ct & TCG_CT_CONST_S12) && val == sextreg(val, 0, 12)) {
161 if ((ct & TCG_CT_CONST_N12) && -val == sextreg(-val, 0, 12)) {
164 if ((ct & TCG_CT_CONST_M12) && val >= -0xfff && val <= 0xfff) {
171 * RISC-V Base ISA opcodes (IM)
187 OPC_DIVU = 0x2005033,
199 OPC_MULH = 0x2001033,
200 OPC_MULHSU = 0x2002033,
201 OPC_MULHU = 0x2003033,
205 OPC_REMU = 0x2007033,
215 OPC_SRA = 0x40005033,
216 OPC_SRAI = 0x40005013,
219 OPC_SUB = 0x40000033,
224 #if TCG_TARGET_REG_BITS == 64
227 OPC_DIVUW = 0x200503b,
228 OPC_DIVW = 0x200403b,
229 OPC_MULW = 0x200003b,
230 OPC_REMUW = 0x200703b,
231 OPC_REMW = 0x200603b,
234 OPC_SRAIW = 0x4000501b,
235 OPC_SRAW = 0x4000503b,
238 OPC_SUBW = 0x4000003b,
240 /* Simplify code throughout by defining aliases for RV32. */
241 OPC_ADDIW = OPC_ADDI,
243 OPC_DIVUW = OPC_DIVU,
246 OPC_REMUW = OPC_REMU,
248 OPC_SLLIW = OPC_SLLI,
250 OPC_SRAIW = OPC_SRAI,
252 OPC_SRLIW = OPC_SRLI,
257 OPC_FENCE = 0x0000000f,
261 * RISC-V immediate and instruction encoders (excludes 16-bit RVC)
266 static int32_t encode_r(RISCVInsn opc, TCGReg rd, TCGReg rs1, TCGReg rs2)
268 return opc | (rd & 0x1f) << 7 | (rs1 & 0x1f) << 15 | (rs2 & 0x1f) << 20;
273 static int32_t encode_imm12(uint32_t imm)
275 return (imm & 0xfff) << 20;
278 static int32_t encode_i(RISCVInsn opc, TCGReg rd, TCGReg rs1, uint32_t imm)
280 return opc | (rd & 0x1f) << 7 | (rs1 & 0x1f) << 15 | encode_imm12(imm);
285 static int32_t encode_simm12(uint32_t imm)
289 ret |= (imm & 0xFE0) << 20;
290 ret |= (imm & 0x1F) << 7;
295 static int32_t encode_s(RISCVInsn opc, TCGReg rs1, TCGReg rs2, uint32_t imm)
297 return opc | (rs1 & 0x1f) << 15 | (rs2 & 0x1f) << 20 | encode_simm12(imm);
302 static int32_t encode_sbimm12(uint32_t imm)
306 ret |= (imm & 0x1000) << 19;
307 ret |= (imm & 0x7e0) << 20;
308 ret |= (imm & 0x1e) << 7;
309 ret |= (imm & 0x800) >> 4;
314 static int32_t encode_sb(RISCVInsn opc, TCGReg rs1, TCGReg rs2, uint32_t imm)
316 return opc | (rs1 & 0x1f) << 15 | (rs2 & 0x1f) << 20 | encode_sbimm12(imm);
321 static int32_t encode_uimm20(uint32_t imm)
323 return imm & 0xfffff000;
326 static int32_t encode_u(RISCVInsn opc, TCGReg rd, uint32_t imm)
328 return opc | (rd & 0x1f) << 7 | encode_uimm20(imm);
333 static int32_t encode_ujimm20(uint32_t imm)
337 ret |= (imm & 0x0007fe) << (21 - 1);
338 ret |= (imm & 0x000800) << (20 - 11);
339 ret |= (imm & 0x0ff000) << (12 - 12);
340 ret |= (imm & 0x100000) << (31 - 20);
345 static int32_t encode_uj(RISCVInsn opc, TCGReg rd, uint32_t imm)
347 return opc | (rd & 0x1f) << 7 | encode_ujimm20(imm);
351 * RISC-V instruction emitters
354 static void tcg_out_opc_reg(TCGContext *s, RISCVInsn opc,
355 TCGReg rd, TCGReg rs1, TCGReg rs2)
357 tcg_out32(s, encode_r(opc, rd, rs1, rs2));
360 static void tcg_out_opc_imm(TCGContext *s, RISCVInsn opc,
361 TCGReg rd, TCGReg rs1, TCGArg imm)
363 tcg_out32(s, encode_i(opc, rd, rs1, imm));
366 static void tcg_out_opc_store(TCGContext *s, RISCVInsn opc,
367 TCGReg rs1, TCGReg rs2, uint32_t imm)
369 tcg_out32(s, encode_s(opc, rs1, rs2, imm));
372 static void tcg_out_opc_branch(TCGContext *s, RISCVInsn opc,
373 TCGReg rs1, TCGReg rs2, uint32_t imm)
375 tcg_out32(s, encode_sb(opc, rs1, rs2, imm));
378 static void tcg_out_opc_upper(TCGContext *s, RISCVInsn opc,
379 TCGReg rd, uint32_t imm)
381 tcg_out32(s, encode_u(opc, rd, imm));
384 static void tcg_out_opc_jump(TCGContext *s, RISCVInsn opc,
385 TCGReg rd, uint32_t imm)
387 tcg_out32(s, encode_uj(opc, rd, imm));
390 static void tcg_out_nop_fill(tcg_insn_unit *p, int count)
393 for (i = 0; i < count; ++i) {
394 p[i] = encode_i(OPC_ADDI, TCG_REG_ZERO, TCG_REG_ZERO, 0);
402 static bool reloc_sbimm12(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
404 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
405 intptr_t offset = (intptr_t)target - (intptr_t)src_rx;
407 tcg_debug_assert((offset & 1) == 0);
408 if (offset == sextreg(offset, 0, 12)) {
409 *src_rw |= encode_sbimm12(offset);
416 static bool reloc_jimm20(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
418 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
419 intptr_t offset = (intptr_t)target - (intptr_t)src_rx;
421 tcg_debug_assert((offset & 1) == 0);
422 if (offset == sextreg(offset, 0, 20)) {
423 *src_rw |= encode_ujimm20(offset);
430 static bool reloc_call(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
432 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
433 intptr_t offset = (intptr_t)target - (intptr_t)src_rx;
434 int32_t lo = sextreg(offset, 0, 12);
435 int32_t hi = offset - lo;
437 if (offset == hi + lo) {
438 src_rw[0] |= encode_uimm20(hi);
439 src_rw[1] |= encode_imm12(lo);
446 static bool patch_reloc(tcg_insn_unit *code_ptr, int type,
447 intptr_t value, intptr_t addend)
449 tcg_debug_assert(addend == 0);
452 return reloc_sbimm12(code_ptr, (tcg_insn_unit *)value);
454 return reloc_jimm20(code_ptr, (tcg_insn_unit *)value);
456 return reloc_call(code_ptr, (tcg_insn_unit *)value);
458 g_assert_not_reached();
466 static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
474 tcg_out_opc_imm(s, OPC_ADDI, ret, arg, 0);
477 g_assert_not_reached();
482 static void tcg_out_movi(TCGContext *s, TCGType type, TCGReg rd,
485 tcg_target_long lo, hi, tmp;
488 if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I32) {
492 lo = sextreg(val, 0, 12);
494 tcg_out_opc_imm(s, OPC_ADDI, rd, TCG_REG_ZERO, lo);
499 if (TCG_TARGET_REG_BITS == 32 || val == (int32_t)val) {
500 tcg_out_opc_upper(s, OPC_LUI, rd, hi);
502 tcg_out_opc_imm(s, OPC_ADDIW, rd, rd, lo);
507 /* We can only be here if TCG_TARGET_REG_BITS != 32 */
508 tmp = tcg_pcrel_diff(s, (void *)val);
509 if (tmp == (int32_t)tmp) {
510 tcg_out_opc_upper(s, OPC_AUIPC, rd, 0);
511 tcg_out_opc_imm(s, OPC_ADDI, rd, rd, 0);
512 ret = reloc_call(s->code_ptr - 2, (const tcg_insn_unit *)val);
513 tcg_debug_assert(ret == true);
517 /* Look for a single 20-bit section. */
520 if (tmp == sextreg(tmp, 0, 20)) {
521 tcg_out_opc_upper(s, OPC_LUI, rd, tmp << 12);
523 tcg_out_opc_imm(s, OPC_SLLI, rd, rd, shift - 12);
525 tcg_out_opc_imm(s, OPC_SRAI, rd, rd, 12 - shift);
530 /* Look for a few high zero bits, with lots of bits set in the middle. */
533 if (tmp == sextreg(tmp, 12, 20) << 12) {
534 tcg_out_opc_upper(s, OPC_LUI, rd, tmp);
535 tcg_out_opc_imm(s, OPC_SRLI, rd, rd, shift);
537 } else if (tmp == sextreg(tmp, 0, 12)) {
538 tcg_out_opc_imm(s, OPC_ADDI, rd, TCG_REG_ZERO, tmp);
539 tcg_out_opc_imm(s, OPC_SRLI, rd, rd, shift);
543 /* Drop into the constant pool. */
544 new_pool_label(s, val, R_RISCV_CALL, s->code_ptr, 0);
545 tcg_out_opc_upper(s, OPC_AUIPC, rd, 0);
546 tcg_out_opc_imm(s, OPC_LD, rd, rd, 0);
549 static void tcg_out_ext8u(TCGContext *s, TCGReg ret, TCGReg arg)
551 tcg_out_opc_imm(s, OPC_ANDI, ret, arg, 0xff);
554 static void tcg_out_ext16u(TCGContext *s, TCGReg ret, TCGReg arg)
556 tcg_out_opc_imm(s, OPC_SLLIW, ret, arg, 16);
557 tcg_out_opc_imm(s, OPC_SRLIW, ret, ret, 16);
560 static void tcg_out_ext32u(TCGContext *s, TCGReg ret, TCGReg arg)
562 tcg_out_opc_imm(s, OPC_SLLI, ret, arg, 32);
563 tcg_out_opc_imm(s, OPC_SRLI, ret, ret, 32);
566 static void tcg_out_ext8s(TCGContext *s, TCGReg ret, TCGReg arg)
568 tcg_out_opc_imm(s, OPC_SLLIW, ret, arg, 24);
569 tcg_out_opc_imm(s, OPC_SRAIW, ret, ret, 24);
572 static void tcg_out_ext16s(TCGContext *s, TCGReg ret, TCGReg arg)
574 tcg_out_opc_imm(s, OPC_SLLIW, ret, arg, 16);
575 tcg_out_opc_imm(s, OPC_SRAIW, ret, ret, 16);
578 static void tcg_out_ext32s(TCGContext *s, TCGReg ret, TCGReg arg)
580 tcg_out_opc_imm(s, OPC_ADDIW, ret, arg, 0);
583 static void tcg_out_ldst(TCGContext *s, RISCVInsn opc, TCGReg data,
584 TCGReg addr, intptr_t offset)
586 intptr_t imm12 = sextreg(offset, 0, 12);
588 if (offset != imm12) {
589 intptr_t diff = offset - (uintptr_t)s->code_ptr;
591 if (addr == TCG_REG_ZERO && diff == (int32_t)diff) {
592 imm12 = sextreg(diff, 0, 12);
593 tcg_out_opc_upper(s, OPC_AUIPC, TCG_REG_TMP2, diff - imm12);
595 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP2, offset - imm12);
596 if (addr != TCG_REG_ZERO) {
597 tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP2, TCG_REG_TMP2, addr);
608 tcg_out_opc_store(s, opc, addr, data, imm12);
617 tcg_out_opc_imm(s, opc, data, addr, imm12);
620 g_assert_not_reached();
624 static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg arg,
625 TCGReg arg1, intptr_t arg2)
627 bool is32bit = (TCG_TARGET_REG_BITS == 32 || type == TCG_TYPE_I32);
628 tcg_out_ldst(s, is32bit ? OPC_LW : OPC_LD, arg, arg1, arg2);
631 static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
632 TCGReg arg1, intptr_t arg2)
634 bool is32bit = (TCG_TARGET_REG_BITS == 32 || type == TCG_TYPE_I32);
635 tcg_out_ldst(s, is32bit ? OPC_SW : OPC_SD, arg, arg1, arg2);
638 static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
639 TCGReg base, intptr_t ofs)
642 tcg_out_st(s, type, TCG_REG_ZERO, base, ofs);
648 static void tcg_out_addsub2(TCGContext *s,
649 TCGReg rl, TCGReg rh,
650 TCGReg al, TCGReg ah,
651 TCGArg bl, TCGArg bh,
652 bool cbl, bool cbh, bool is_sub, bool is32bit)
654 const RISCVInsn opc_add = is32bit ? OPC_ADDW : OPC_ADD;
655 const RISCVInsn opc_addi = is32bit ? OPC_ADDIW : OPC_ADDI;
656 const RISCVInsn opc_sub = is32bit ? OPC_SUBW : OPC_SUB;
657 TCGReg th = TCG_REG_TMP1;
659 /* If we have a negative constant such that negating it would
660 make the high part zero, we can (usually) eliminate one insn. */
661 if (cbl && cbh && bh == -1 && bl != 0) {
667 /* By operating on the high part first, we get to use the final
668 carry operation to move back from the temporary. */
670 tcg_out_opc_reg(s, (is_sub ? opc_sub : opc_add), th, ah, bh);
671 } else if (bh != 0 || ah == rl) {
672 tcg_out_opc_imm(s, opc_addi, th, ah, (is_sub ? -bh : bh));
677 /* Note that tcg optimization should eliminate the bl == 0 case. */
680 tcg_out_opc_imm(s, OPC_SLTIU, TCG_REG_TMP0, al, bl);
681 tcg_out_opc_imm(s, opc_addi, rl, al, -bl);
683 tcg_out_opc_reg(s, OPC_SLTU, TCG_REG_TMP0, al, bl);
684 tcg_out_opc_reg(s, opc_sub, rl, al, bl);
686 tcg_out_opc_reg(s, opc_sub, rh, th, TCG_REG_TMP0);
689 tcg_out_opc_imm(s, opc_addi, rl, al, bl);
690 tcg_out_opc_imm(s, OPC_SLTIU, TCG_REG_TMP0, rl, bl);
691 } else if (rl == al && rl == bl) {
692 tcg_out_opc_imm(s, OPC_SLTI, TCG_REG_TMP0, al, 0);
693 tcg_out_opc_reg(s, opc_addi, rl, al, bl);
695 tcg_out_opc_reg(s, opc_add, rl, al, bl);
696 tcg_out_opc_reg(s, OPC_SLTU, TCG_REG_TMP0,
697 rl, (rl == bl ? al : bl));
699 tcg_out_opc_reg(s, opc_add, rh, th, TCG_REG_TMP0);
703 static const struct {
706 } tcg_brcond_to_riscv[] = {
707 [TCG_COND_EQ] = { OPC_BEQ, false },
708 [TCG_COND_NE] = { OPC_BNE, false },
709 [TCG_COND_LT] = { OPC_BLT, false },
710 [TCG_COND_GE] = { OPC_BGE, false },
711 [TCG_COND_LE] = { OPC_BGE, true },
712 [TCG_COND_GT] = { OPC_BLT, true },
713 [TCG_COND_LTU] = { OPC_BLTU, false },
714 [TCG_COND_GEU] = { OPC_BGEU, false },
715 [TCG_COND_LEU] = { OPC_BGEU, true },
716 [TCG_COND_GTU] = { OPC_BLTU, true }
719 static void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1,
720 TCGReg arg2, TCGLabel *l)
722 RISCVInsn op = tcg_brcond_to_riscv[cond].op;
724 tcg_debug_assert(op != 0);
726 if (tcg_brcond_to_riscv[cond].swap) {
732 tcg_out_reloc(s, s->code_ptr, R_RISCV_BRANCH, l, 0);
733 tcg_out_opc_branch(s, op, arg1, arg2, 0);
736 static void tcg_out_setcond(TCGContext *s, TCGCond cond, TCGReg ret,
737 TCGReg arg1, TCGReg arg2)
741 tcg_out_opc_reg(s, OPC_SUB, ret, arg1, arg2);
742 tcg_out_opc_imm(s, OPC_SLTIU, ret, ret, 1);
745 tcg_out_opc_reg(s, OPC_SUB, ret, arg1, arg2);
746 tcg_out_opc_reg(s, OPC_SLTU, ret, TCG_REG_ZERO, ret);
749 tcg_out_opc_reg(s, OPC_SLT, ret, arg1, arg2);
752 tcg_out_opc_reg(s, OPC_SLT, ret, arg1, arg2);
753 tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1);
756 tcg_out_opc_reg(s, OPC_SLT, ret, arg2, arg1);
757 tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1);
760 tcg_out_opc_reg(s, OPC_SLT, ret, arg2, arg1);
763 tcg_out_opc_reg(s, OPC_SLTU, ret, arg1, arg2);
766 tcg_out_opc_reg(s, OPC_SLTU, ret, arg1, arg2);
767 tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1);
770 tcg_out_opc_reg(s, OPC_SLTU, ret, arg2, arg1);
771 tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1);
774 tcg_out_opc_reg(s, OPC_SLTU, ret, arg2, arg1);
777 g_assert_not_reached();
782 static void tcg_out_brcond2(TCGContext *s, TCGCond cond, TCGReg al, TCGReg ah,
783 TCGReg bl, TCGReg bh, TCGLabel *l)
786 g_assert_not_reached();
789 static void tcg_out_setcond2(TCGContext *s, TCGCond cond, TCGReg ret,
790 TCGReg al, TCGReg ah, TCGReg bl, TCGReg bh)
793 g_assert_not_reached();
796 static void tcg_out_call_int(TCGContext *s, const tcg_insn_unit *arg, bool tail)
798 TCGReg link = tail ? TCG_REG_ZERO : TCG_REG_RA;
799 ptrdiff_t offset = tcg_pcrel_diff(s, arg);
802 tcg_debug_assert((offset & 1) == 0);
803 if (offset == sextreg(offset, 0, 20)) {
804 /* short jump: -2097150 to 2097152 */
805 tcg_out_opc_jump(s, OPC_JAL, link, offset);
806 } else if (TCG_TARGET_REG_BITS == 32 || offset == (int32_t)offset) {
807 /* long jump: -2147483646 to 2147483648 */
808 tcg_out_opc_upper(s, OPC_AUIPC, TCG_REG_TMP0, 0);
809 tcg_out_opc_imm(s, OPC_JALR, link, TCG_REG_TMP0, 0);
810 ret = reloc_call(s->code_ptr - 2, arg);
811 tcg_debug_assert(ret == true);
812 } else if (TCG_TARGET_REG_BITS == 64) {
813 /* far jump: 64-bit */
814 tcg_target_long imm = sextreg((tcg_target_long)arg, 0, 12);
815 tcg_target_long base = (tcg_target_long)arg - imm;
816 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP0, base);
817 tcg_out_opc_imm(s, OPC_JALR, link, TCG_REG_TMP0, imm);
819 g_assert_not_reached();
823 static void tcg_out_call(TCGContext *s, const tcg_insn_unit *arg)
825 tcg_out_call_int(s, arg, false);
828 static void tcg_out_mb(TCGContext *s, TCGArg a0)
830 tcg_insn_unit insn = OPC_FENCE;
832 if (a0 & TCG_MO_LD_LD) {
835 if (a0 & TCG_MO_ST_LD) {
838 if (a0 & TCG_MO_LD_ST) {
841 if (a0 & TCG_MO_ST_ST) {
851 #if defined(CONFIG_SOFTMMU)
852 #include "../tcg-ldst.c.inc"
854 /* helper signature: helper_ret_ld_mmu(CPUState *env, target_ulong addr,
855 * TCGMemOpIdx oi, uintptr_t ra)
857 static void * const qemu_ld_helpers[16] = {
858 [MO_UB] = helper_ret_ldub_mmu,
859 [MO_SB] = helper_ret_ldsb_mmu,
860 [MO_LEUW] = helper_le_lduw_mmu,
861 [MO_LESW] = helper_le_ldsw_mmu,
862 [MO_LEUL] = helper_le_ldul_mmu,
863 #if TCG_TARGET_REG_BITS == 64
864 [MO_LESL] = helper_le_ldsl_mmu,
866 [MO_LEQ] = helper_le_ldq_mmu,
867 [MO_BEUW] = helper_be_lduw_mmu,
868 [MO_BESW] = helper_be_ldsw_mmu,
869 [MO_BEUL] = helper_be_ldul_mmu,
870 #if TCG_TARGET_REG_BITS == 64
871 [MO_BESL] = helper_be_ldsl_mmu,
873 [MO_BEQ] = helper_be_ldq_mmu,
876 /* helper signature: helper_ret_st_mmu(CPUState *env, target_ulong addr,
877 * uintxx_t val, TCGMemOpIdx oi,
880 static void * const qemu_st_helpers[16] = {
881 [MO_UB] = helper_ret_stb_mmu,
882 [MO_LEUW] = helper_le_stw_mmu,
883 [MO_LEUL] = helper_le_stl_mmu,
884 [MO_LEQ] = helper_le_stq_mmu,
885 [MO_BEUW] = helper_be_stw_mmu,
886 [MO_BEUL] = helper_be_stl_mmu,
887 [MO_BEQ] = helper_be_stq_mmu,
890 /* We don't support oversize guests */
891 QEMU_BUILD_BUG_ON(TCG_TARGET_REG_BITS < TARGET_LONG_BITS);
893 /* We expect to use a 12-bit negative offset from ENV. */
894 QEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) > 0);
895 QEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) < -(1 << 11));
897 static void tcg_out_goto(TCGContext *s, const tcg_insn_unit *target)
899 tcg_out_opc_jump(s, OPC_JAL, TCG_REG_ZERO, 0);
900 bool ok = reloc_jimm20(s->code_ptr - 1, target);
901 tcg_debug_assert(ok);
904 static void tcg_out_tlb_load(TCGContext *s, TCGReg addrl,
905 TCGReg addrh, TCGMemOpIdx oi,
906 tcg_insn_unit **label_ptr, bool is_load)
908 MemOp opc = get_memop(oi);
909 unsigned s_bits = opc & MO_SIZE;
910 unsigned a_bits = get_alignment_bits(opc);
911 tcg_target_long compare_mask;
912 int mem_index = get_mmuidx(oi);
913 int fast_ofs = TLB_MASK_TABLE_OFS(mem_index);
914 int mask_ofs = fast_ofs + offsetof(CPUTLBDescFast, mask);
915 int table_ofs = fast_ofs + offsetof(CPUTLBDescFast, table);
916 TCGReg mask_base = TCG_AREG0, table_base = TCG_AREG0;
918 tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP0, mask_base, mask_ofs);
919 tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP1, table_base, table_ofs);
921 tcg_out_opc_imm(s, OPC_SRLI, TCG_REG_TMP2, addrl,
922 TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS);
923 tcg_out_opc_reg(s, OPC_AND, TCG_REG_TMP2, TCG_REG_TMP2, TCG_REG_TMP0);
924 tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP2, TCG_REG_TMP2, TCG_REG_TMP1);
926 /* Load the tlb comparator and the addend. */
927 tcg_out_ld(s, TCG_TYPE_TL, TCG_REG_TMP0, TCG_REG_TMP2,
928 is_load ? offsetof(CPUTLBEntry, addr_read)
929 : offsetof(CPUTLBEntry, addr_write));
930 tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP2, TCG_REG_TMP2,
931 offsetof(CPUTLBEntry, addend));
933 /* We don't support unaligned accesses. */
934 if (a_bits < s_bits) {
937 /* Clear the non-page, non-alignment bits from the address. */
938 compare_mask = (tcg_target_long)TARGET_PAGE_MASK | ((1 << a_bits) - 1);
939 if (compare_mask == sextreg(compare_mask, 0, 12)) {
940 tcg_out_opc_imm(s, OPC_ANDI, TCG_REG_TMP1, addrl, compare_mask);
942 tcg_out_movi(s, TCG_TYPE_TL, TCG_REG_TMP1, compare_mask);
943 tcg_out_opc_reg(s, OPC_AND, TCG_REG_TMP1, TCG_REG_TMP1, addrl);
946 /* Compare masked address with the TLB entry. */
947 label_ptr[0] = s->code_ptr;
948 tcg_out_opc_branch(s, OPC_BNE, TCG_REG_TMP0, TCG_REG_TMP1, 0);
950 /* TLB Hit - translate address using addend. */
951 if (TCG_TARGET_REG_BITS > TARGET_LONG_BITS) {
952 tcg_out_ext32u(s, TCG_REG_TMP0, addrl);
953 addrl = TCG_REG_TMP0;
955 tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP0, TCG_REG_TMP2, addrl);
958 static void add_qemu_ldst_label(TCGContext *s, int is_ld, TCGMemOpIdx oi,
960 TCGReg datalo, TCGReg datahi,
961 TCGReg addrlo, TCGReg addrhi,
962 void *raddr, tcg_insn_unit **label_ptr)
964 TCGLabelQemuLdst *label = new_ldst_label(s);
966 label->is_ld = is_ld;
969 label->datalo_reg = datalo;
970 label->datahi_reg = datahi;
971 label->addrlo_reg = addrlo;
972 label->addrhi_reg = addrhi;
973 label->raddr = tcg_splitwx_to_rx(raddr);
974 label->label_ptr[0] = label_ptr[0];
977 static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
979 TCGMemOpIdx oi = l->oi;
980 MemOp opc = get_memop(oi);
981 TCGReg a0 = tcg_target_call_iarg_regs[0];
982 TCGReg a1 = tcg_target_call_iarg_regs[1];
983 TCGReg a2 = tcg_target_call_iarg_regs[2];
984 TCGReg a3 = tcg_target_call_iarg_regs[3];
986 /* We don't support oversize guests */
987 if (TCG_TARGET_REG_BITS < TARGET_LONG_BITS) {
988 g_assert_not_reached();
991 /* resolve label address */
992 if (!reloc_sbimm12(l->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
996 /* call load helper */
997 tcg_out_mov(s, TCG_TYPE_PTR, a0, TCG_AREG0);
998 tcg_out_mov(s, TCG_TYPE_PTR, a1, l->addrlo_reg);
999 tcg_out_movi(s, TCG_TYPE_PTR, a2, oi);
1000 tcg_out_movi(s, TCG_TYPE_PTR, a3, (tcg_target_long)l->raddr);
1002 tcg_out_call(s, qemu_ld_helpers[opc & (MO_BSWAP | MO_SSIZE)]);
1003 tcg_out_mov(s, (opc & MO_SIZE) == MO_64, l->datalo_reg, a0);
1005 tcg_out_goto(s, l->raddr);
1009 static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
1011 TCGMemOpIdx oi = l->oi;
1012 MemOp opc = get_memop(oi);
1013 MemOp s_bits = opc & MO_SIZE;
1014 TCGReg a0 = tcg_target_call_iarg_regs[0];
1015 TCGReg a1 = tcg_target_call_iarg_regs[1];
1016 TCGReg a2 = tcg_target_call_iarg_regs[2];
1017 TCGReg a3 = tcg_target_call_iarg_regs[3];
1018 TCGReg a4 = tcg_target_call_iarg_regs[4];
1020 /* We don't support oversize guests */
1021 if (TCG_TARGET_REG_BITS < TARGET_LONG_BITS) {
1022 g_assert_not_reached();
1025 /* resolve label address */
1026 if (!reloc_sbimm12(l->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
1030 /* call store helper */
1031 tcg_out_mov(s, TCG_TYPE_PTR, a0, TCG_AREG0);
1032 tcg_out_mov(s, TCG_TYPE_PTR, a1, l->addrlo_reg);
1033 tcg_out_mov(s, TCG_TYPE_PTR, a2, l->datalo_reg);
1036 tcg_out_ext8u(s, a2, a2);
1039 tcg_out_ext16u(s, a2, a2);
1044 tcg_out_movi(s, TCG_TYPE_PTR, a3, oi);
1045 tcg_out_movi(s, TCG_TYPE_PTR, a4, (tcg_target_long)l->raddr);
1047 tcg_out_call(s, qemu_st_helpers[opc & (MO_BSWAP | MO_SSIZE)]);
1049 tcg_out_goto(s, l->raddr);
1052 #endif /* CONFIG_SOFTMMU */
1054 static void tcg_out_qemu_ld_direct(TCGContext *s, TCGReg lo, TCGReg hi,
1055 TCGReg base, MemOp opc, bool is_64)
1057 const MemOp bswap = opc & MO_BSWAP;
1059 /* We don't yet handle byteswapping, assert */
1062 switch (opc & (MO_SSIZE)) {
1064 tcg_out_opc_imm(s, OPC_LBU, lo, base, 0);
1067 tcg_out_opc_imm(s, OPC_LB, lo, base, 0);
1070 tcg_out_opc_imm(s, OPC_LHU, lo, base, 0);
1073 tcg_out_opc_imm(s, OPC_LH, lo, base, 0);
1076 if (TCG_TARGET_REG_BITS == 64 && is_64) {
1077 tcg_out_opc_imm(s, OPC_LWU, lo, base, 0);
1082 tcg_out_opc_imm(s, OPC_LW, lo, base, 0);
1085 /* Prefer to load from offset 0 first, but allow for overlap. */
1086 if (TCG_TARGET_REG_BITS == 64) {
1087 tcg_out_opc_imm(s, OPC_LD, lo, base, 0);
1088 } else if (lo != base) {
1089 tcg_out_opc_imm(s, OPC_LW, lo, base, 0);
1090 tcg_out_opc_imm(s, OPC_LW, hi, base, 4);
1092 tcg_out_opc_imm(s, OPC_LW, hi, base, 4);
1093 tcg_out_opc_imm(s, OPC_LW, lo, base, 0);
1097 g_assert_not_reached();
1101 static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args, bool is_64)
1103 TCGReg addr_regl, addr_regh __attribute__((unused));
1104 TCGReg data_regl, data_regh;
1107 #if defined(CONFIG_SOFTMMU)
1108 tcg_insn_unit *label_ptr[1];
1110 TCGReg base = TCG_REG_TMP0;
1112 data_regl = *args++;
1113 data_regh = (TCG_TARGET_REG_BITS == 32 && is_64 ? *args++ : 0);
1114 addr_regl = *args++;
1115 addr_regh = (TCG_TARGET_REG_BITS < TARGET_LONG_BITS ? *args++ : 0);
1117 opc = get_memop(oi);
1119 #if defined(CONFIG_SOFTMMU)
1120 tcg_out_tlb_load(s, addr_regl, addr_regh, oi, label_ptr, 1);
1121 tcg_out_qemu_ld_direct(s, data_regl, data_regh, base, opc, is_64);
1122 add_qemu_ldst_label(s, 1, oi,
1123 (is_64 ? TCG_TYPE_I64 : TCG_TYPE_I32),
1124 data_regl, data_regh, addr_regl, addr_regh,
1125 s->code_ptr, label_ptr);
1127 if (TCG_TARGET_REG_BITS > TARGET_LONG_BITS) {
1128 tcg_out_ext32u(s, base, addr_regl);
1132 if (guest_base == 0) {
1133 tcg_out_opc_reg(s, OPC_ADD, base, addr_regl, TCG_REG_ZERO);
1135 tcg_out_opc_reg(s, OPC_ADD, base, TCG_GUEST_BASE_REG, addr_regl);
1137 tcg_out_qemu_ld_direct(s, data_regl, data_regh, base, opc, is_64);
1141 static void tcg_out_qemu_st_direct(TCGContext *s, TCGReg lo, TCGReg hi,
1142 TCGReg base, MemOp opc)
1144 const MemOp bswap = opc & MO_BSWAP;
1146 /* We don't yet handle byteswapping, assert */
1149 switch (opc & (MO_SSIZE)) {
1151 tcg_out_opc_store(s, OPC_SB, base, lo, 0);
1154 tcg_out_opc_store(s, OPC_SH, base, lo, 0);
1157 tcg_out_opc_store(s, OPC_SW, base, lo, 0);
1160 if (TCG_TARGET_REG_BITS == 64) {
1161 tcg_out_opc_store(s, OPC_SD, base, lo, 0);
1163 tcg_out_opc_store(s, OPC_SW, base, lo, 0);
1164 tcg_out_opc_store(s, OPC_SW, base, hi, 4);
1168 g_assert_not_reached();
1172 static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args, bool is_64)
1174 TCGReg addr_regl, addr_regh __attribute__((unused));
1175 TCGReg data_regl, data_regh;
1178 #if defined(CONFIG_SOFTMMU)
1179 tcg_insn_unit *label_ptr[1];
1181 TCGReg base = TCG_REG_TMP0;
1183 data_regl = *args++;
1184 data_regh = (TCG_TARGET_REG_BITS == 32 && is_64 ? *args++ : 0);
1185 addr_regl = *args++;
1186 addr_regh = (TCG_TARGET_REG_BITS < TARGET_LONG_BITS ? *args++ : 0);
1188 opc = get_memop(oi);
1190 #if defined(CONFIG_SOFTMMU)
1191 tcg_out_tlb_load(s, addr_regl, addr_regh, oi, label_ptr, 0);
1192 tcg_out_qemu_st_direct(s, data_regl, data_regh, base, opc);
1193 add_qemu_ldst_label(s, 0, oi,
1194 (is_64 ? TCG_TYPE_I64 : TCG_TYPE_I32),
1195 data_regl, data_regh, addr_regl, addr_regh,
1196 s->code_ptr, label_ptr);
1198 if (TCG_TARGET_REG_BITS > TARGET_LONG_BITS) {
1199 tcg_out_ext32u(s, base, addr_regl);
1203 if (guest_base == 0) {
1204 tcg_out_opc_reg(s, OPC_ADD, base, addr_regl, TCG_REG_ZERO);
1206 tcg_out_opc_reg(s, OPC_ADD, base, TCG_GUEST_BASE_REG, addr_regl);
1208 tcg_out_qemu_st_direct(s, data_regl, data_regh, base, opc);
1212 static const tcg_insn_unit *tb_ret_addr;
1214 static void tcg_out_op(TCGContext *s, TCGOpcode opc,
1215 const TCGArg args[TCG_MAX_OP_ARGS],
1216 const int const_args[TCG_MAX_OP_ARGS])
1218 TCGArg a0 = args[0];
1219 TCGArg a1 = args[1];
1220 TCGArg a2 = args[2];
1221 int c2 = const_args[2];
1224 case INDEX_op_exit_tb:
1225 /* Reuse the zeroing that exists for goto_ptr. */
1227 tcg_out_call_int(s, tcg_code_gen_epilogue, true);
1229 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_A0, a0);
1230 tcg_out_call_int(s, tb_ret_addr, true);
1234 case INDEX_op_goto_tb:
1235 assert(s->tb_jmp_insn_offset == 0);
1236 /* indirect jump method */
1237 tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP0, TCG_REG_ZERO,
1238 (uintptr_t)(s->tb_jmp_target_addr + a0));
1239 tcg_out_opc_imm(s, OPC_JALR, TCG_REG_ZERO, TCG_REG_TMP0, 0);
1240 set_jmp_reset_offset(s, a0);
1243 case INDEX_op_goto_ptr:
1244 tcg_out_opc_imm(s, OPC_JALR, TCG_REG_ZERO, a0, 0);
1248 tcg_out_reloc(s, s->code_ptr, R_RISCV_JAL, arg_label(a0), 0);
1249 tcg_out_opc_jump(s, OPC_JAL, TCG_REG_ZERO, 0);
1252 case INDEX_op_ld8u_i32:
1253 case INDEX_op_ld8u_i64:
1254 tcg_out_ldst(s, OPC_LBU, a0, a1, a2);
1256 case INDEX_op_ld8s_i32:
1257 case INDEX_op_ld8s_i64:
1258 tcg_out_ldst(s, OPC_LB, a0, a1, a2);
1260 case INDEX_op_ld16u_i32:
1261 case INDEX_op_ld16u_i64:
1262 tcg_out_ldst(s, OPC_LHU, a0, a1, a2);
1264 case INDEX_op_ld16s_i32:
1265 case INDEX_op_ld16s_i64:
1266 tcg_out_ldst(s, OPC_LH, a0, a1, a2);
1268 case INDEX_op_ld32u_i64:
1269 tcg_out_ldst(s, OPC_LWU, a0, a1, a2);
1271 case INDEX_op_ld_i32:
1272 case INDEX_op_ld32s_i64:
1273 tcg_out_ldst(s, OPC_LW, a0, a1, a2);
1275 case INDEX_op_ld_i64:
1276 tcg_out_ldst(s, OPC_LD, a0, a1, a2);
1279 case INDEX_op_st8_i32:
1280 case INDEX_op_st8_i64:
1281 tcg_out_ldst(s, OPC_SB, a0, a1, a2);
1283 case INDEX_op_st16_i32:
1284 case INDEX_op_st16_i64:
1285 tcg_out_ldst(s, OPC_SH, a0, a1, a2);
1287 case INDEX_op_st_i32:
1288 case INDEX_op_st32_i64:
1289 tcg_out_ldst(s, OPC_SW, a0, a1, a2);
1291 case INDEX_op_st_i64:
1292 tcg_out_ldst(s, OPC_SD, a0, a1, a2);
1295 case INDEX_op_add_i32:
1297 tcg_out_opc_imm(s, OPC_ADDIW, a0, a1, a2);
1299 tcg_out_opc_reg(s, OPC_ADDW, a0, a1, a2);
1302 case INDEX_op_add_i64:
1304 tcg_out_opc_imm(s, OPC_ADDI, a0, a1, a2);
1306 tcg_out_opc_reg(s, OPC_ADD, a0, a1, a2);
1310 case INDEX_op_sub_i32:
1312 tcg_out_opc_imm(s, OPC_ADDIW, a0, a1, -a2);
1314 tcg_out_opc_reg(s, OPC_SUBW, a0, a1, a2);
1317 case INDEX_op_sub_i64:
1319 tcg_out_opc_imm(s, OPC_ADDI, a0, a1, -a2);
1321 tcg_out_opc_reg(s, OPC_SUB, a0, a1, a2);
1325 case INDEX_op_and_i32:
1326 case INDEX_op_and_i64:
1328 tcg_out_opc_imm(s, OPC_ANDI, a0, a1, a2);
1330 tcg_out_opc_reg(s, OPC_AND, a0, a1, a2);
1334 case INDEX_op_or_i32:
1335 case INDEX_op_or_i64:
1337 tcg_out_opc_imm(s, OPC_ORI, a0, a1, a2);
1339 tcg_out_opc_reg(s, OPC_OR, a0, a1, a2);
1343 case INDEX_op_xor_i32:
1344 case INDEX_op_xor_i64:
1346 tcg_out_opc_imm(s, OPC_XORI, a0, a1, a2);
1348 tcg_out_opc_reg(s, OPC_XOR, a0, a1, a2);
1352 case INDEX_op_not_i32:
1353 case INDEX_op_not_i64:
1354 tcg_out_opc_imm(s, OPC_XORI, a0, a1, -1);
1357 case INDEX_op_neg_i32:
1358 tcg_out_opc_reg(s, OPC_SUBW, a0, TCG_REG_ZERO, a1);
1360 case INDEX_op_neg_i64:
1361 tcg_out_opc_reg(s, OPC_SUB, a0, TCG_REG_ZERO, a1);
1364 case INDEX_op_mul_i32:
1365 tcg_out_opc_reg(s, OPC_MULW, a0, a1, a2);
1367 case INDEX_op_mul_i64:
1368 tcg_out_opc_reg(s, OPC_MUL, a0, a1, a2);
1371 case INDEX_op_div_i32:
1372 tcg_out_opc_reg(s, OPC_DIVW, a0, a1, a2);
1374 case INDEX_op_div_i64:
1375 tcg_out_opc_reg(s, OPC_DIV, a0, a1, a2);
1378 case INDEX_op_divu_i32:
1379 tcg_out_opc_reg(s, OPC_DIVUW, a0, a1, a2);
1381 case INDEX_op_divu_i64:
1382 tcg_out_opc_reg(s, OPC_DIVU, a0, a1, a2);
1385 case INDEX_op_rem_i32:
1386 tcg_out_opc_reg(s, OPC_REMW, a0, a1, a2);
1388 case INDEX_op_rem_i64:
1389 tcg_out_opc_reg(s, OPC_REM, a0, a1, a2);
1392 case INDEX_op_remu_i32:
1393 tcg_out_opc_reg(s, OPC_REMUW, a0, a1, a2);
1395 case INDEX_op_remu_i64:
1396 tcg_out_opc_reg(s, OPC_REMU, a0, a1, a2);
1399 case INDEX_op_shl_i32:
1401 tcg_out_opc_imm(s, OPC_SLLIW, a0, a1, a2 & 0x1f);
1403 tcg_out_opc_reg(s, OPC_SLLW, a0, a1, a2);
1406 case INDEX_op_shl_i64:
1408 tcg_out_opc_imm(s, OPC_SLLI, a0, a1, a2 & 0x3f);
1410 tcg_out_opc_reg(s, OPC_SLL, a0, a1, a2);
1414 case INDEX_op_shr_i32:
1416 tcg_out_opc_imm(s, OPC_SRLIW, a0, a1, a2 & 0x1f);
1418 tcg_out_opc_reg(s, OPC_SRLW, a0, a1, a2);
1421 case INDEX_op_shr_i64:
1423 tcg_out_opc_imm(s, OPC_SRLI, a0, a1, a2 & 0x3f);
1425 tcg_out_opc_reg(s, OPC_SRL, a0, a1, a2);
1429 case INDEX_op_sar_i32:
1431 tcg_out_opc_imm(s, OPC_SRAIW, a0, a1, a2 & 0x1f);
1433 tcg_out_opc_reg(s, OPC_SRAW, a0, a1, a2);
1436 case INDEX_op_sar_i64:
1438 tcg_out_opc_imm(s, OPC_SRAI, a0, a1, a2 & 0x3f);
1440 tcg_out_opc_reg(s, OPC_SRA, a0, a1, a2);
1444 case INDEX_op_add2_i32:
1445 tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5],
1446 const_args[4], const_args[5], false, true);
1448 case INDEX_op_add2_i64:
1449 tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5],
1450 const_args[4], const_args[5], false, false);
1452 case INDEX_op_sub2_i32:
1453 tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5],
1454 const_args[4], const_args[5], true, true);
1456 case INDEX_op_sub2_i64:
1457 tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5],
1458 const_args[4], const_args[5], true, false);
1461 case INDEX_op_brcond_i32:
1462 case INDEX_op_brcond_i64:
1463 tcg_out_brcond(s, a2, a0, a1, arg_label(args[3]));
1465 case INDEX_op_brcond2_i32:
1466 tcg_out_brcond2(s, args[4], a0, a1, a2, args[3], arg_label(args[5]));
1469 case INDEX_op_setcond_i32:
1470 case INDEX_op_setcond_i64:
1471 tcg_out_setcond(s, args[3], a0, a1, a2);
1473 case INDEX_op_setcond2_i32:
1474 tcg_out_setcond2(s, args[5], a0, a1, a2, args[3], args[4]);
1477 case INDEX_op_qemu_ld_i32:
1478 tcg_out_qemu_ld(s, args, false);
1480 case INDEX_op_qemu_ld_i64:
1481 tcg_out_qemu_ld(s, args, true);
1483 case INDEX_op_qemu_st_i32:
1484 tcg_out_qemu_st(s, args, false);
1486 case INDEX_op_qemu_st_i64:
1487 tcg_out_qemu_st(s, args, true);
1490 case INDEX_op_ext8u_i32:
1491 case INDEX_op_ext8u_i64:
1492 tcg_out_ext8u(s, a0, a1);
1495 case INDEX_op_ext16u_i32:
1496 case INDEX_op_ext16u_i64:
1497 tcg_out_ext16u(s, a0, a1);
1500 case INDEX_op_ext32u_i64:
1501 case INDEX_op_extu_i32_i64:
1502 tcg_out_ext32u(s, a0, a1);
1505 case INDEX_op_ext8s_i32:
1506 case INDEX_op_ext8s_i64:
1507 tcg_out_ext8s(s, a0, a1);
1510 case INDEX_op_ext16s_i32:
1511 case INDEX_op_ext16s_i64:
1512 tcg_out_ext16s(s, a0, a1);
1515 case INDEX_op_ext32s_i64:
1516 case INDEX_op_extrl_i64_i32:
1517 case INDEX_op_ext_i32_i64:
1518 tcg_out_ext32s(s, a0, a1);
1521 case INDEX_op_extrh_i64_i32:
1522 tcg_out_opc_imm(s, OPC_SRAI, a0, a1, 32);
1525 case INDEX_op_mulsh_i32:
1526 case INDEX_op_mulsh_i64:
1527 tcg_out_opc_reg(s, OPC_MULH, a0, a1, a2);
1530 case INDEX_op_muluh_i32:
1531 case INDEX_op_muluh_i64:
1532 tcg_out_opc_reg(s, OPC_MULHU, a0, a1, a2);
1539 case INDEX_op_mov_i32: /* Always emitted via tcg_out_mov. */
1540 case INDEX_op_mov_i64:
1541 case INDEX_op_call: /* Always emitted via tcg_out_call. */
1543 g_assert_not_reached();
1547 static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
1550 case INDEX_op_goto_ptr:
1553 case INDEX_op_ld8u_i32:
1554 case INDEX_op_ld8s_i32:
1555 case INDEX_op_ld16u_i32:
1556 case INDEX_op_ld16s_i32:
1557 case INDEX_op_ld_i32:
1558 case INDEX_op_not_i32:
1559 case INDEX_op_neg_i32:
1560 case INDEX_op_ld8u_i64:
1561 case INDEX_op_ld8s_i64:
1562 case INDEX_op_ld16u_i64:
1563 case INDEX_op_ld16s_i64:
1564 case INDEX_op_ld32s_i64:
1565 case INDEX_op_ld32u_i64:
1566 case INDEX_op_ld_i64:
1567 case INDEX_op_not_i64:
1568 case INDEX_op_neg_i64:
1569 case INDEX_op_ext8u_i32:
1570 case INDEX_op_ext8u_i64:
1571 case INDEX_op_ext16u_i32:
1572 case INDEX_op_ext16u_i64:
1573 case INDEX_op_ext32u_i64:
1574 case INDEX_op_extu_i32_i64:
1575 case INDEX_op_ext8s_i32:
1576 case INDEX_op_ext8s_i64:
1577 case INDEX_op_ext16s_i32:
1578 case INDEX_op_ext16s_i64:
1579 case INDEX_op_ext32s_i64:
1580 case INDEX_op_extrl_i64_i32:
1581 case INDEX_op_extrh_i64_i32:
1582 case INDEX_op_ext_i32_i64:
1583 return C_O1_I1(r, r);
1585 case INDEX_op_st8_i32:
1586 case INDEX_op_st16_i32:
1587 case INDEX_op_st_i32:
1588 case INDEX_op_st8_i64:
1589 case INDEX_op_st16_i64:
1590 case INDEX_op_st32_i64:
1591 case INDEX_op_st_i64:
1592 return C_O0_I2(rZ, r);
1594 case INDEX_op_add_i32:
1595 case INDEX_op_and_i32:
1596 case INDEX_op_or_i32:
1597 case INDEX_op_xor_i32:
1598 case INDEX_op_add_i64:
1599 case INDEX_op_and_i64:
1600 case INDEX_op_or_i64:
1601 case INDEX_op_xor_i64:
1602 return C_O1_I2(r, r, rI);
1604 case INDEX_op_sub_i32:
1605 case INDEX_op_sub_i64:
1606 return C_O1_I2(r, rZ, rN);
1608 case INDEX_op_mul_i32:
1609 case INDEX_op_mulsh_i32:
1610 case INDEX_op_muluh_i32:
1611 case INDEX_op_div_i32:
1612 case INDEX_op_divu_i32:
1613 case INDEX_op_rem_i32:
1614 case INDEX_op_remu_i32:
1615 case INDEX_op_setcond_i32:
1616 case INDEX_op_mul_i64:
1617 case INDEX_op_mulsh_i64:
1618 case INDEX_op_muluh_i64:
1619 case INDEX_op_div_i64:
1620 case INDEX_op_divu_i64:
1621 case INDEX_op_rem_i64:
1622 case INDEX_op_remu_i64:
1623 case INDEX_op_setcond_i64:
1624 return C_O1_I2(r, rZ, rZ);
1626 case INDEX_op_shl_i32:
1627 case INDEX_op_shr_i32:
1628 case INDEX_op_sar_i32:
1629 case INDEX_op_shl_i64:
1630 case INDEX_op_shr_i64:
1631 case INDEX_op_sar_i64:
1632 return C_O1_I2(r, r, ri);
1634 case INDEX_op_brcond_i32:
1635 case INDEX_op_brcond_i64:
1636 return C_O0_I2(rZ, rZ);
1638 case INDEX_op_add2_i32:
1639 case INDEX_op_add2_i64:
1640 case INDEX_op_sub2_i32:
1641 case INDEX_op_sub2_i64:
1642 return C_O2_I4(r, r, rZ, rZ, rM, rM);
1644 case INDEX_op_brcond2_i32:
1645 return C_O0_I4(rZ, rZ, rZ, rZ);
1647 case INDEX_op_setcond2_i32:
1648 return C_O1_I4(r, rZ, rZ, rZ, rZ);
1650 case INDEX_op_qemu_ld_i32:
1651 return (TARGET_LONG_BITS <= TCG_TARGET_REG_BITS
1652 ? C_O1_I1(r, L) : C_O1_I2(r, L, L));
1653 case INDEX_op_qemu_st_i32:
1654 return (TARGET_LONG_BITS <= TCG_TARGET_REG_BITS
1655 ? C_O0_I2(LZ, L) : C_O0_I3(LZ, L, L));
1656 case INDEX_op_qemu_ld_i64:
1657 return (TCG_TARGET_REG_BITS == 64 ? C_O1_I1(r, L)
1658 : TARGET_LONG_BITS <= TCG_TARGET_REG_BITS ? C_O2_I1(r, r, L)
1659 : C_O2_I2(r, r, L, L));
1660 case INDEX_op_qemu_st_i64:
1661 return (TCG_TARGET_REG_BITS == 64 ? C_O0_I2(LZ, L)
1662 : TARGET_LONG_BITS <= TCG_TARGET_REG_BITS ? C_O0_I3(LZ, LZ, L)
1663 : C_O0_I4(LZ, LZ, L, L));
1666 g_assert_not_reached();
1670 static const int tcg_target_callee_save_regs[] = {
1671 TCG_REG_S0, /* used for the global env (TCG_AREG0) */
1683 TCG_REG_RA, /* should be last for ABI compliance */
1686 /* Stack frame parameters. */
1687 #define REG_SIZE (TCG_TARGET_REG_BITS / 8)
1688 #define SAVE_SIZE ((int)ARRAY_SIZE(tcg_target_callee_save_regs) * REG_SIZE)
1689 #define TEMP_SIZE (CPU_TEMP_BUF_NLONGS * (int)sizeof(long))
1690 #define FRAME_SIZE ((TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE + SAVE_SIZE \
1691 + TCG_TARGET_STACK_ALIGN - 1) \
1692 & -TCG_TARGET_STACK_ALIGN)
1693 #define SAVE_OFS (TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE)
1695 /* We're expecting to be able to use an immediate for frame allocation. */
1696 QEMU_BUILD_BUG_ON(FRAME_SIZE > 0x7ff);
1698 /* Generate global QEMU prologue and epilogue code */
1699 static void tcg_target_qemu_prologue(TCGContext *s)
1703 tcg_set_frame(s, TCG_REG_SP, TCG_STATIC_CALL_ARGS_SIZE, TEMP_SIZE);
1706 tcg_out_opc_imm(s, OPC_ADDI, TCG_REG_SP, TCG_REG_SP, -FRAME_SIZE);
1707 for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
1708 tcg_out_st(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
1709 TCG_REG_SP, SAVE_OFS + i * REG_SIZE);
1712 #if !defined(CONFIG_SOFTMMU)
1713 tcg_out_movi(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG, guest_base);
1714 tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
1717 /* Call generated code */
1718 tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
1719 tcg_out_opc_imm(s, OPC_JALR, TCG_REG_ZERO, tcg_target_call_iarg_regs[1], 0);
1721 /* Return path for goto_ptr. Set return value to 0 */
1722 tcg_code_gen_epilogue = tcg_splitwx_to_rx(s->code_ptr);
1723 tcg_out_mov(s, TCG_TYPE_REG, TCG_REG_A0, TCG_REG_ZERO);
1726 tb_ret_addr = tcg_splitwx_to_rx(s->code_ptr);
1727 for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
1728 tcg_out_ld(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
1729 TCG_REG_SP, SAVE_OFS + i * REG_SIZE);
1732 tcg_out_opc_imm(s, OPC_ADDI, TCG_REG_SP, TCG_REG_SP, FRAME_SIZE);
1733 tcg_out_opc_imm(s, OPC_JALR, TCG_REG_ZERO, TCG_REG_RA, 0);
1736 static void tcg_target_init(TCGContext *s)
1738 tcg_target_available_regs[TCG_TYPE_I32] = 0xffffffff;
1739 if (TCG_TARGET_REG_BITS == 64) {
1740 tcg_target_available_regs[TCG_TYPE_I64] = 0xffffffff;
1743 tcg_target_call_clobber_regs = -1u;
1744 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S0);
1745 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S1);
1746 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S2);
1747 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S3);
1748 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S4);
1749 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S5);
1750 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S6);
1751 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S7);
1752 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S8);
1753 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S9);
1754 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S10);
1755 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S11);
1757 s->reserved_regs = 0;
1758 tcg_regset_set_reg(s->reserved_regs, TCG_REG_ZERO);
1759 tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP0);
1760 tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP1);
1761 tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP2);
1762 tcg_regset_set_reg(s->reserved_regs, TCG_REG_SP);
1763 tcg_regset_set_reg(s->reserved_regs, TCG_REG_GP);
1764 tcg_regset_set_reg(s->reserved_regs, TCG_REG_TP);
1769 uint8_t fde_def_cfa[4];
1770 uint8_t fde_reg_ofs[ARRAY_SIZE(tcg_target_callee_save_regs) * 2];
1773 #define ELF_HOST_MACHINE EM_RISCV
1775 static const DebugFrame debug_frame = {
1776 .h.cie.len = sizeof(DebugFrameCIE) - 4, /* length after .len member */
1779 .h.cie.code_align = 1,
1780 .h.cie.data_align = -(TCG_TARGET_REG_BITS / 8) & 0x7f, /* sleb128 */
1781 .h.cie.return_column = TCG_REG_RA,
1783 /* Total FDE size does not include the "len" member. */
1784 .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset),
1787 12, TCG_REG_SP, /* DW_CFA_def_cfa sp, ... */
1788 (FRAME_SIZE & 0x7f) | 0x80, /* ... uleb128 FRAME_SIZE */
1792 0x80 + 9, 12, /* DW_CFA_offset, s1, -96 */
1793 0x80 + 18, 11, /* DW_CFA_offset, s2, -88 */
1794 0x80 + 19, 10, /* DW_CFA_offset, s3, -80 */
1795 0x80 + 20, 9, /* DW_CFA_offset, s4, -72 */
1796 0x80 + 21, 8, /* DW_CFA_offset, s5, -64 */
1797 0x80 + 22, 7, /* DW_CFA_offset, s6, -56 */
1798 0x80 + 23, 6, /* DW_CFA_offset, s7, -48 */
1799 0x80 + 24, 5, /* DW_CFA_offset, s8, -40 */
1800 0x80 + 25, 4, /* DW_CFA_offset, s9, -32 */
1801 0x80 + 26, 3, /* DW_CFA_offset, s10, -24 */
1802 0x80 + 27, 2, /* DW_CFA_offset, s11, -16 */
1803 0x80 + 1 , 1, /* DW_CFA_offset, ra, -8 */
1807 void tcg_register_jit(const void *buf, size_t buf_size)
1809 tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame));