xref: /openbmc/qemu/tcg/riscv/tcg-target.c.inc (revision 2be7d76b1557d3ee72cde3b2cf5d4abf25220fb2)
1139c1837SPaolo Bonzini/*
2139c1837SPaolo Bonzini * Tiny Code Generator for QEMU
3139c1837SPaolo Bonzini *
4139c1837SPaolo Bonzini * Copyright (c) 2018 SiFive, Inc
5139c1837SPaolo Bonzini * Copyright (c) 2008-2009 Arnaud Patard <arnaud.patard@rtp-net.org>
6139c1837SPaolo Bonzini * Copyright (c) 2009 Aurelien Jarno <aurelien@aurel32.net>
7139c1837SPaolo Bonzini * Copyright (c) 2008 Fabrice Bellard
8139c1837SPaolo Bonzini *
9139c1837SPaolo Bonzini * Based on i386/tcg-target.c and mips/tcg-target.c
10139c1837SPaolo Bonzini *
11139c1837SPaolo Bonzini * Permission is hereby granted, free of charge, to any person obtaining a copy
12139c1837SPaolo Bonzini * of this software and associated documentation files (the "Software"), to deal
13139c1837SPaolo Bonzini * in the Software without restriction, including without limitation the rights
14139c1837SPaolo Bonzini * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15139c1837SPaolo Bonzini * copies of the Software, and to permit persons to whom the Software is
16139c1837SPaolo Bonzini * furnished to do so, subject to the following conditions:
17139c1837SPaolo Bonzini *
18139c1837SPaolo Bonzini * The above copyright notice and this permission notice shall be included in
19139c1837SPaolo Bonzini * all copies or substantial portions of the Software.
20139c1837SPaolo Bonzini *
21139c1837SPaolo Bonzini * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22139c1837SPaolo Bonzini * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23139c1837SPaolo Bonzini * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24139c1837SPaolo Bonzini * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25139c1837SPaolo Bonzini * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26139c1837SPaolo Bonzini * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27139c1837SPaolo Bonzini * THE SOFTWARE.
28139c1837SPaolo Bonzini */
29139c1837SPaolo Bonzini
30139c1837SPaolo Bonzini#include "../tcg-pool.c.inc"
31139c1837SPaolo Bonzini
32139c1837SPaolo Bonzini#ifdef CONFIG_DEBUG_TCG
33139c1837SPaolo Bonzinistatic const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
34139c1837SPaolo Bonzini    "zero",
35139c1837SPaolo Bonzini    "ra",
36139c1837SPaolo Bonzini    "sp",
37139c1837SPaolo Bonzini    "gp",
38139c1837SPaolo Bonzini    "tp",
39139c1837SPaolo Bonzini    "t0",
40139c1837SPaolo Bonzini    "t1",
41139c1837SPaolo Bonzini    "t2",
42139c1837SPaolo Bonzini    "s0",
43139c1837SPaolo Bonzini    "s1",
44139c1837SPaolo Bonzini    "a0",
45139c1837SPaolo Bonzini    "a1",
46139c1837SPaolo Bonzini    "a2",
47139c1837SPaolo Bonzini    "a3",
48139c1837SPaolo Bonzini    "a4",
49139c1837SPaolo Bonzini    "a5",
50139c1837SPaolo Bonzini    "a6",
51139c1837SPaolo Bonzini    "a7",
52139c1837SPaolo Bonzini    "s2",
53139c1837SPaolo Bonzini    "s3",
54139c1837SPaolo Bonzini    "s4",
55139c1837SPaolo Bonzini    "s5",
56139c1837SPaolo Bonzini    "s6",
57139c1837SPaolo Bonzini    "s7",
58139c1837SPaolo Bonzini    "s8",
59139c1837SPaolo Bonzini    "s9",
60139c1837SPaolo Bonzini    "s10",
61139c1837SPaolo Bonzini    "s11",
62139c1837SPaolo Bonzini    "t3",
63139c1837SPaolo Bonzini    "t4",
64139c1837SPaolo Bonzini    "t5",
65139c1837SPaolo Bonzini    "t6"
66139c1837SPaolo Bonzini};
67139c1837SPaolo Bonzini#endif
68139c1837SPaolo Bonzini
69139c1837SPaolo Bonzinistatic const int tcg_target_reg_alloc_order[] = {
70139c1837SPaolo Bonzini    /* Call saved registers */
71139c1837SPaolo Bonzini    /* TCG_REG_S0 reservered for TCG_AREG0 */
72139c1837SPaolo Bonzini    TCG_REG_S1,
73139c1837SPaolo Bonzini    TCG_REG_S2,
74139c1837SPaolo Bonzini    TCG_REG_S3,
75139c1837SPaolo Bonzini    TCG_REG_S4,
76139c1837SPaolo Bonzini    TCG_REG_S5,
77139c1837SPaolo Bonzini    TCG_REG_S6,
78139c1837SPaolo Bonzini    TCG_REG_S7,
79139c1837SPaolo Bonzini    TCG_REG_S8,
80139c1837SPaolo Bonzini    TCG_REG_S9,
81139c1837SPaolo Bonzini    TCG_REG_S10,
82139c1837SPaolo Bonzini    TCG_REG_S11,
83139c1837SPaolo Bonzini
84139c1837SPaolo Bonzini    /* Call clobbered registers */
85139c1837SPaolo Bonzini    TCG_REG_T0,
86139c1837SPaolo Bonzini    TCG_REG_T1,
87139c1837SPaolo Bonzini    TCG_REG_T2,
88139c1837SPaolo Bonzini    TCG_REG_T3,
89139c1837SPaolo Bonzini    TCG_REG_T4,
90139c1837SPaolo Bonzini    TCG_REG_T5,
91139c1837SPaolo Bonzini    TCG_REG_T6,
92139c1837SPaolo Bonzini
93139c1837SPaolo Bonzini    /* Argument registers */
94139c1837SPaolo Bonzini    TCG_REG_A0,
95139c1837SPaolo Bonzini    TCG_REG_A1,
96139c1837SPaolo Bonzini    TCG_REG_A2,
97139c1837SPaolo Bonzini    TCG_REG_A3,
98139c1837SPaolo Bonzini    TCG_REG_A4,
99139c1837SPaolo Bonzini    TCG_REG_A5,
100139c1837SPaolo Bonzini    TCG_REG_A6,
101139c1837SPaolo Bonzini    TCG_REG_A7,
102139c1837SPaolo Bonzini};
103139c1837SPaolo Bonzini
104139c1837SPaolo Bonzinistatic const int tcg_target_call_iarg_regs[] = {
105139c1837SPaolo Bonzini    TCG_REG_A0,
106139c1837SPaolo Bonzini    TCG_REG_A1,
107139c1837SPaolo Bonzini    TCG_REG_A2,
108139c1837SPaolo Bonzini    TCG_REG_A3,
109139c1837SPaolo Bonzini    TCG_REG_A4,
110139c1837SPaolo Bonzini    TCG_REG_A5,
111139c1837SPaolo Bonzini    TCG_REG_A6,
112139c1837SPaolo Bonzini    TCG_REG_A7,
113139c1837SPaolo Bonzini};
114139c1837SPaolo Bonzini
115139c1837SPaolo Bonzinistatic const int tcg_target_call_oarg_regs[] = {
116139c1837SPaolo Bonzini    TCG_REG_A0,
117139c1837SPaolo Bonzini    TCG_REG_A1,
118139c1837SPaolo Bonzini};
119139c1837SPaolo Bonzini
120139c1837SPaolo Bonzini#define TCG_CT_CONST_ZERO  0x100
121139c1837SPaolo Bonzini#define TCG_CT_CONST_S12   0x200
122139c1837SPaolo Bonzini#define TCG_CT_CONST_N12   0x400
123139c1837SPaolo Bonzini#define TCG_CT_CONST_M12   0x800
124139c1837SPaolo Bonzini
125139c1837SPaolo Bonzinistatic inline tcg_target_long sextreg(tcg_target_long val, int pos, int len)
126139c1837SPaolo Bonzini{
127139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS == 32) {
128139c1837SPaolo Bonzini        return sextract32(val, pos, len);
129139c1837SPaolo Bonzini    } else {
130139c1837SPaolo Bonzini        return sextract64(val, pos, len);
131139c1837SPaolo Bonzini    }
132139c1837SPaolo Bonzini}
133139c1837SPaolo Bonzini
134139c1837SPaolo Bonzini/* parse target specific constraints */
135139c1837SPaolo Bonzinistatic const char *target_parse_constraint(TCGArgConstraint *ct,
136139c1837SPaolo Bonzini                                           const char *ct_str, TCGType type)
137139c1837SPaolo Bonzini{
138139c1837SPaolo Bonzini    switch (*ct_str++) {
139139c1837SPaolo Bonzini    case 'r':
1409be0d080SRichard Henderson        ct->regs = 0xffffffff;
141139c1837SPaolo Bonzini        break;
142139c1837SPaolo Bonzini    case 'L':
143139c1837SPaolo Bonzini        /* qemu_ld/qemu_st constraint */
1449be0d080SRichard Henderson        ct->regs = 0xffffffff;
145139c1837SPaolo Bonzini        /* qemu_ld/qemu_st uses TCG_REG_TMP0 */
146139c1837SPaolo Bonzini#if defined(CONFIG_SOFTMMU)
1479be0d080SRichard Henderson        tcg_regset_reset_reg(ct->regs, tcg_target_call_iarg_regs[0]);
1489be0d080SRichard Henderson        tcg_regset_reset_reg(ct->regs, tcg_target_call_iarg_regs[1]);
1499be0d080SRichard Henderson        tcg_regset_reset_reg(ct->regs, tcg_target_call_iarg_regs[2]);
1509be0d080SRichard Henderson        tcg_regset_reset_reg(ct->regs, tcg_target_call_iarg_regs[3]);
1519be0d080SRichard Henderson        tcg_regset_reset_reg(ct->regs, tcg_target_call_iarg_regs[4]);
152139c1837SPaolo Bonzini#endif
153139c1837SPaolo Bonzini        break;
154139c1837SPaolo Bonzini    case 'I':
155139c1837SPaolo Bonzini        ct->ct |= TCG_CT_CONST_S12;
156139c1837SPaolo Bonzini        break;
157139c1837SPaolo Bonzini    case 'N':
158139c1837SPaolo Bonzini        ct->ct |= TCG_CT_CONST_N12;
159139c1837SPaolo Bonzini        break;
160139c1837SPaolo Bonzini    case 'M':
161139c1837SPaolo Bonzini        ct->ct |= TCG_CT_CONST_M12;
162139c1837SPaolo Bonzini        break;
163139c1837SPaolo Bonzini    case 'Z':
164139c1837SPaolo Bonzini        /* we can use a zero immediate as a zero register argument. */
165139c1837SPaolo Bonzini        ct->ct |= TCG_CT_CONST_ZERO;
166139c1837SPaolo Bonzini        break;
167139c1837SPaolo Bonzini    default:
168139c1837SPaolo Bonzini        return NULL;
169139c1837SPaolo Bonzini    }
170139c1837SPaolo Bonzini    return ct_str;
171139c1837SPaolo Bonzini}
172139c1837SPaolo Bonzini
173139c1837SPaolo Bonzini/* test if a constant matches the constraint */
174139c1837SPaolo Bonzinistatic int tcg_target_const_match(tcg_target_long val, TCGType type,
175139c1837SPaolo Bonzini                                  const TCGArgConstraint *arg_ct)
176139c1837SPaolo Bonzini{
177139c1837SPaolo Bonzini    int ct = arg_ct->ct;
178139c1837SPaolo Bonzini    if (ct & TCG_CT_CONST) {
179139c1837SPaolo Bonzini        return 1;
180139c1837SPaolo Bonzini    }
181139c1837SPaolo Bonzini    if ((ct & TCG_CT_CONST_ZERO) && val == 0) {
182139c1837SPaolo Bonzini        return 1;
183139c1837SPaolo Bonzini    }
184139c1837SPaolo Bonzini    if ((ct & TCG_CT_CONST_S12) && val == sextreg(val, 0, 12)) {
185139c1837SPaolo Bonzini        return 1;
186139c1837SPaolo Bonzini    }
187139c1837SPaolo Bonzini    if ((ct & TCG_CT_CONST_N12) && -val == sextreg(-val, 0, 12)) {
188139c1837SPaolo Bonzini        return 1;
189139c1837SPaolo Bonzini    }
190139c1837SPaolo Bonzini    if ((ct & TCG_CT_CONST_M12) && val >= -0xfff && val <= 0xfff) {
191139c1837SPaolo Bonzini        return 1;
192139c1837SPaolo Bonzini    }
193139c1837SPaolo Bonzini    return 0;
194139c1837SPaolo Bonzini}
195139c1837SPaolo Bonzini
196139c1837SPaolo Bonzini/*
197139c1837SPaolo Bonzini * RISC-V Base ISA opcodes (IM)
198139c1837SPaolo Bonzini */
199139c1837SPaolo Bonzini
200139c1837SPaolo Bonzinitypedef enum {
201139c1837SPaolo Bonzini    OPC_ADD = 0x33,
202139c1837SPaolo Bonzini    OPC_ADDI = 0x13,
203139c1837SPaolo Bonzini    OPC_AND = 0x7033,
204139c1837SPaolo Bonzini    OPC_ANDI = 0x7013,
205139c1837SPaolo Bonzini    OPC_AUIPC = 0x17,
206139c1837SPaolo Bonzini    OPC_BEQ = 0x63,
207139c1837SPaolo Bonzini    OPC_BGE = 0x5063,
208139c1837SPaolo Bonzini    OPC_BGEU = 0x7063,
209139c1837SPaolo Bonzini    OPC_BLT = 0x4063,
210139c1837SPaolo Bonzini    OPC_BLTU = 0x6063,
211139c1837SPaolo Bonzini    OPC_BNE = 0x1063,
212139c1837SPaolo Bonzini    OPC_DIV = 0x2004033,
213139c1837SPaolo Bonzini    OPC_DIVU = 0x2005033,
214139c1837SPaolo Bonzini    OPC_JAL = 0x6f,
215139c1837SPaolo Bonzini    OPC_JALR = 0x67,
216139c1837SPaolo Bonzini    OPC_LB = 0x3,
217139c1837SPaolo Bonzini    OPC_LBU = 0x4003,
218139c1837SPaolo Bonzini    OPC_LD = 0x3003,
219139c1837SPaolo Bonzini    OPC_LH = 0x1003,
220139c1837SPaolo Bonzini    OPC_LHU = 0x5003,
221139c1837SPaolo Bonzini    OPC_LUI = 0x37,
222139c1837SPaolo Bonzini    OPC_LW = 0x2003,
223139c1837SPaolo Bonzini    OPC_LWU = 0x6003,
224139c1837SPaolo Bonzini    OPC_MUL = 0x2000033,
225139c1837SPaolo Bonzini    OPC_MULH = 0x2001033,
226139c1837SPaolo Bonzini    OPC_MULHSU = 0x2002033,
227139c1837SPaolo Bonzini    OPC_MULHU = 0x2003033,
228139c1837SPaolo Bonzini    OPC_OR = 0x6033,
229139c1837SPaolo Bonzini    OPC_ORI = 0x6013,
230139c1837SPaolo Bonzini    OPC_REM = 0x2006033,
231139c1837SPaolo Bonzini    OPC_REMU = 0x2007033,
232139c1837SPaolo Bonzini    OPC_SB = 0x23,
233139c1837SPaolo Bonzini    OPC_SD = 0x3023,
234139c1837SPaolo Bonzini    OPC_SH = 0x1023,
235139c1837SPaolo Bonzini    OPC_SLL = 0x1033,
236139c1837SPaolo Bonzini    OPC_SLLI = 0x1013,
237139c1837SPaolo Bonzini    OPC_SLT = 0x2033,
238139c1837SPaolo Bonzini    OPC_SLTI = 0x2013,
239139c1837SPaolo Bonzini    OPC_SLTIU = 0x3013,
240139c1837SPaolo Bonzini    OPC_SLTU = 0x3033,
241139c1837SPaolo Bonzini    OPC_SRA = 0x40005033,
242139c1837SPaolo Bonzini    OPC_SRAI = 0x40005013,
243139c1837SPaolo Bonzini    OPC_SRL = 0x5033,
244139c1837SPaolo Bonzini    OPC_SRLI = 0x5013,
245139c1837SPaolo Bonzini    OPC_SUB = 0x40000033,
246139c1837SPaolo Bonzini    OPC_SW = 0x2023,
247139c1837SPaolo Bonzini    OPC_XOR = 0x4033,
248139c1837SPaolo Bonzini    OPC_XORI = 0x4013,
249139c1837SPaolo Bonzini
250139c1837SPaolo Bonzini#if TCG_TARGET_REG_BITS == 64
251139c1837SPaolo Bonzini    OPC_ADDIW = 0x1b,
252139c1837SPaolo Bonzini    OPC_ADDW = 0x3b,
253139c1837SPaolo Bonzini    OPC_DIVUW = 0x200503b,
254139c1837SPaolo Bonzini    OPC_DIVW = 0x200403b,
255139c1837SPaolo Bonzini    OPC_MULW = 0x200003b,
256139c1837SPaolo Bonzini    OPC_REMUW = 0x200703b,
257139c1837SPaolo Bonzini    OPC_REMW = 0x200603b,
258139c1837SPaolo Bonzini    OPC_SLLIW = 0x101b,
259139c1837SPaolo Bonzini    OPC_SLLW = 0x103b,
260139c1837SPaolo Bonzini    OPC_SRAIW = 0x4000501b,
261139c1837SPaolo Bonzini    OPC_SRAW = 0x4000503b,
262139c1837SPaolo Bonzini    OPC_SRLIW = 0x501b,
263139c1837SPaolo Bonzini    OPC_SRLW = 0x503b,
264139c1837SPaolo Bonzini    OPC_SUBW = 0x4000003b,
265139c1837SPaolo Bonzini#else
266139c1837SPaolo Bonzini    /* Simplify code throughout by defining aliases for RV32.  */
267139c1837SPaolo Bonzini    OPC_ADDIW = OPC_ADDI,
268139c1837SPaolo Bonzini    OPC_ADDW = OPC_ADD,
269139c1837SPaolo Bonzini    OPC_DIVUW = OPC_DIVU,
270139c1837SPaolo Bonzini    OPC_DIVW = OPC_DIV,
271139c1837SPaolo Bonzini    OPC_MULW = OPC_MUL,
272139c1837SPaolo Bonzini    OPC_REMUW = OPC_REMU,
273139c1837SPaolo Bonzini    OPC_REMW = OPC_REM,
274139c1837SPaolo Bonzini    OPC_SLLIW = OPC_SLLI,
275139c1837SPaolo Bonzini    OPC_SLLW = OPC_SLL,
276139c1837SPaolo Bonzini    OPC_SRAIW = OPC_SRAI,
277139c1837SPaolo Bonzini    OPC_SRAW = OPC_SRA,
278139c1837SPaolo Bonzini    OPC_SRLIW = OPC_SRLI,
279139c1837SPaolo Bonzini    OPC_SRLW = OPC_SRL,
280139c1837SPaolo Bonzini    OPC_SUBW = OPC_SUB,
281139c1837SPaolo Bonzini#endif
282139c1837SPaolo Bonzini
283139c1837SPaolo Bonzini    OPC_FENCE = 0x0000000f,
284139c1837SPaolo Bonzini} RISCVInsn;
285139c1837SPaolo Bonzini
286139c1837SPaolo Bonzini/*
287139c1837SPaolo Bonzini * RISC-V immediate and instruction encoders (excludes 16-bit RVC)
288139c1837SPaolo Bonzini */
289139c1837SPaolo Bonzini
290139c1837SPaolo Bonzini/* Type-R */
291139c1837SPaolo Bonzini
292139c1837SPaolo Bonzinistatic int32_t encode_r(RISCVInsn opc, TCGReg rd, TCGReg rs1, TCGReg rs2)
293139c1837SPaolo Bonzini{
294139c1837SPaolo Bonzini    return opc | (rd & 0x1f) << 7 | (rs1 & 0x1f) << 15 | (rs2 & 0x1f) << 20;
295139c1837SPaolo Bonzini}
296139c1837SPaolo Bonzini
297139c1837SPaolo Bonzini/* Type-I */
298139c1837SPaolo Bonzini
299139c1837SPaolo Bonzinistatic int32_t encode_imm12(uint32_t imm)
300139c1837SPaolo Bonzini{
301139c1837SPaolo Bonzini    return (imm & 0xfff) << 20;
302139c1837SPaolo Bonzini}
303139c1837SPaolo Bonzini
304139c1837SPaolo Bonzinistatic int32_t encode_i(RISCVInsn opc, TCGReg rd, TCGReg rs1, uint32_t imm)
305139c1837SPaolo Bonzini{
306139c1837SPaolo Bonzini    return opc | (rd & 0x1f) << 7 | (rs1 & 0x1f) << 15 | encode_imm12(imm);
307139c1837SPaolo Bonzini}
308139c1837SPaolo Bonzini
309139c1837SPaolo Bonzini/* Type-S */
310139c1837SPaolo Bonzini
311139c1837SPaolo Bonzinistatic int32_t encode_simm12(uint32_t imm)
312139c1837SPaolo Bonzini{
313139c1837SPaolo Bonzini    int32_t ret = 0;
314139c1837SPaolo Bonzini
315139c1837SPaolo Bonzini    ret |= (imm & 0xFE0) << 20;
316139c1837SPaolo Bonzini    ret |= (imm & 0x1F) << 7;
317139c1837SPaolo Bonzini
318139c1837SPaolo Bonzini    return ret;
319139c1837SPaolo Bonzini}
320139c1837SPaolo Bonzini
321139c1837SPaolo Bonzinistatic int32_t encode_s(RISCVInsn opc, TCGReg rs1, TCGReg rs2, uint32_t imm)
322139c1837SPaolo Bonzini{
323139c1837SPaolo Bonzini    return opc | (rs1 & 0x1f) << 15 | (rs2 & 0x1f) << 20 | encode_simm12(imm);
324139c1837SPaolo Bonzini}
325139c1837SPaolo Bonzini
326139c1837SPaolo Bonzini/* Type-SB */
327139c1837SPaolo Bonzini
328139c1837SPaolo Bonzinistatic int32_t encode_sbimm12(uint32_t imm)
329139c1837SPaolo Bonzini{
330139c1837SPaolo Bonzini    int32_t ret = 0;
331139c1837SPaolo Bonzini
332139c1837SPaolo Bonzini    ret |= (imm & 0x1000) << 19;
333139c1837SPaolo Bonzini    ret |= (imm & 0x7e0) << 20;
334139c1837SPaolo Bonzini    ret |= (imm & 0x1e) << 7;
335139c1837SPaolo Bonzini    ret |= (imm & 0x800) >> 4;
336139c1837SPaolo Bonzini
337139c1837SPaolo Bonzini    return ret;
338139c1837SPaolo Bonzini}
339139c1837SPaolo Bonzini
340139c1837SPaolo Bonzinistatic int32_t encode_sb(RISCVInsn opc, TCGReg rs1, TCGReg rs2, uint32_t imm)
341139c1837SPaolo Bonzini{
342139c1837SPaolo Bonzini    return opc | (rs1 & 0x1f) << 15 | (rs2 & 0x1f) << 20 | encode_sbimm12(imm);
343139c1837SPaolo Bonzini}
344139c1837SPaolo Bonzini
345139c1837SPaolo Bonzini/* Type-U */
346139c1837SPaolo Bonzini
347139c1837SPaolo Bonzinistatic int32_t encode_uimm20(uint32_t imm)
348139c1837SPaolo Bonzini{
349139c1837SPaolo Bonzini    return imm & 0xfffff000;
350139c1837SPaolo Bonzini}
351139c1837SPaolo Bonzini
352139c1837SPaolo Bonzinistatic int32_t encode_u(RISCVInsn opc, TCGReg rd, uint32_t imm)
353139c1837SPaolo Bonzini{
354139c1837SPaolo Bonzini    return opc | (rd & 0x1f) << 7 | encode_uimm20(imm);
355139c1837SPaolo Bonzini}
356139c1837SPaolo Bonzini
357139c1837SPaolo Bonzini/* Type-UJ */
358139c1837SPaolo Bonzini
359139c1837SPaolo Bonzinistatic int32_t encode_ujimm20(uint32_t imm)
360139c1837SPaolo Bonzini{
361139c1837SPaolo Bonzini    int32_t ret = 0;
362139c1837SPaolo Bonzini
363139c1837SPaolo Bonzini    ret |= (imm & 0x0007fe) << (21 - 1);
364139c1837SPaolo Bonzini    ret |= (imm & 0x000800) << (20 - 11);
365139c1837SPaolo Bonzini    ret |= (imm & 0x0ff000) << (12 - 12);
366139c1837SPaolo Bonzini    ret |= (imm & 0x100000) << (31 - 20);
367139c1837SPaolo Bonzini
368139c1837SPaolo Bonzini    return ret;
369139c1837SPaolo Bonzini}
370139c1837SPaolo Bonzini
371139c1837SPaolo Bonzinistatic int32_t encode_uj(RISCVInsn opc, TCGReg rd, uint32_t imm)
372139c1837SPaolo Bonzini{
373139c1837SPaolo Bonzini    return opc | (rd & 0x1f) << 7 | encode_ujimm20(imm);
374139c1837SPaolo Bonzini}
375139c1837SPaolo Bonzini
376139c1837SPaolo Bonzini/*
377139c1837SPaolo Bonzini * RISC-V instruction emitters
378139c1837SPaolo Bonzini */
379139c1837SPaolo Bonzini
380139c1837SPaolo Bonzinistatic void tcg_out_opc_reg(TCGContext *s, RISCVInsn opc,
381139c1837SPaolo Bonzini                            TCGReg rd, TCGReg rs1, TCGReg rs2)
382139c1837SPaolo Bonzini{
383139c1837SPaolo Bonzini    tcg_out32(s, encode_r(opc, rd, rs1, rs2));
384139c1837SPaolo Bonzini}
385139c1837SPaolo Bonzini
386139c1837SPaolo Bonzinistatic void tcg_out_opc_imm(TCGContext *s, RISCVInsn opc,
387139c1837SPaolo Bonzini                            TCGReg rd, TCGReg rs1, TCGArg imm)
388139c1837SPaolo Bonzini{
389139c1837SPaolo Bonzini    tcg_out32(s, encode_i(opc, rd, rs1, imm));
390139c1837SPaolo Bonzini}
391139c1837SPaolo Bonzini
392139c1837SPaolo Bonzinistatic void tcg_out_opc_store(TCGContext *s, RISCVInsn opc,
393139c1837SPaolo Bonzini                              TCGReg rs1, TCGReg rs2, uint32_t imm)
394139c1837SPaolo Bonzini{
395139c1837SPaolo Bonzini    tcg_out32(s, encode_s(opc, rs1, rs2, imm));
396139c1837SPaolo Bonzini}
397139c1837SPaolo Bonzini
398139c1837SPaolo Bonzinistatic void tcg_out_opc_branch(TCGContext *s, RISCVInsn opc,
399139c1837SPaolo Bonzini                               TCGReg rs1, TCGReg rs2, uint32_t imm)
400139c1837SPaolo Bonzini{
401139c1837SPaolo Bonzini    tcg_out32(s, encode_sb(opc, rs1, rs2, imm));
402139c1837SPaolo Bonzini}
403139c1837SPaolo Bonzini
404139c1837SPaolo Bonzinistatic void tcg_out_opc_upper(TCGContext *s, RISCVInsn opc,
405139c1837SPaolo Bonzini                              TCGReg rd, uint32_t imm)
406139c1837SPaolo Bonzini{
407139c1837SPaolo Bonzini    tcg_out32(s, encode_u(opc, rd, imm));
408139c1837SPaolo Bonzini}
409139c1837SPaolo Bonzini
410139c1837SPaolo Bonzinistatic void tcg_out_opc_jump(TCGContext *s, RISCVInsn opc,
411139c1837SPaolo Bonzini                             TCGReg rd, uint32_t imm)
412139c1837SPaolo Bonzini{
413139c1837SPaolo Bonzini    tcg_out32(s, encode_uj(opc, rd, imm));
414139c1837SPaolo Bonzini}
415139c1837SPaolo Bonzini
416139c1837SPaolo Bonzinistatic void tcg_out_nop_fill(tcg_insn_unit *p, int count)
417139c1837SPaolo Bonzini{
418139c1837SPaolo Bonzini    int i;
419139c1837SPaolo Bonzini    for (i = 0; i < count; ++i) {
420139c1837SPaolo Bonzini        p[i] = encode_i(OPC_ADDI, TCG_REG_ZERO, TCG_REG_ZERO, 0);
421139c1837SPaolo Bonzini    }
422139c1837SPaolo Bonzini}
423139c1837SPaolo Bonzini
424139c1837SPaolo Bonzini/*
425139c1837SPaolo Bonzini * Relocations
426139c1837SPaolo Bonzini */
427139c1837SPaolo Bonzini
428139c1837SPaolo Bonzinistatic bool reloc_sbimm12(tcg_insn_unit *code_ptr, tcg_insn_unit *target)
429139c1837SPaolo Bonzini{
430139c1837SPaolo Bonzini    intptr_t offset = (intptr_t)target - (intptr_t)code_ptr;
431139c1837SPaolo Bonzini
432139c1837SPaolo Bonzini    if (offset == sextreg(offset, 1, 12) << 1) {
433139c1837SPaolo Bonzini        code_ptr[0] |= encode_sbimm12(offset);
434139c1837SPaolo Bonzini        return true;
435139c1837SPaolo Bonzini    }
436139c1837SPaolo Bonzini
437139c1837SPaolo Bonzini    return false;
438139c1837SPaolo Bonzini}
439139c1837SPaolo Bonzini
440139c1837SPaolo Bonzinistatic bool reloc_jimm20(tcg_insn_unit *code_ptr, tcg_insn_unit *target)
441139c1837SPaolo Bonzini{
442139c1837SPaolo Bonzini    intptr_t offset = (intptr_t)target - (intptr_t)code_ptr;
443139c1837SPaolo Bonzini
444139c1837SPaolo Bonzini    if (offset == sextreg(offset, 1, 20) << 1) {
445139c1837SPaolo Bonzini        code_ptr[0] |= encode_ujimm20(offset);
446139c1837SPaolo Bonzini        return true;
447139c1837SPaolo Bonzini    }
448139c1837SPaolo Bonzini
449139c1837SPaolo Bonzini    return false;
450139c1837SPaolo Bonzini}
451139c1837SPaolo Bonzini
452*2be7d76bSRichard Hendersonstatic bool reloc_call(tcg_insn_unit *code_ptr, const tcg_insn_unit *target)
453139c1837SPaolo Bonzini{
454139c1837SPaolo Bonzini    intptr_t offset = (intptr_t)target - (intptr_t)code_ptr;
455139c1837SPaolo Bonzini    int32_t lo = sextreg(offset, 0, 12);
456139c1837SPaolo Bonzini    int32_t hi = offset - lo;
457139c1837SPaolo Bonzini
458139c1837SPaolo Bonzini    if (offset == hi + lo) {
459139c1837SPaolo Bonzini        code_ptr[0] |= encode_uimm20(hi);
460139c1837SPaolo Bonzini        code_ptr[1] |= encode_imm12(lo);
461139c1837SPaolo Bonzini        return true;
462139c1837SPaolo Bonzini    }
463139c1837SPaolo Bonzini
464139c1837SPaolo Bonzini    return false;
465139c1837SPaolo Bonzini}
466139c1837SPaolo Bonzini
467139c1837SPaolo Bonzinistatic bool patch_reloc(tcg_insn_unit *code_ptr, int type,
468139c1837SPaolo Bonzini                        intptr_t value, intptr_t addend)
469139c1837SPaolo Bonzini{
470139c1837SPaolo Bonzini    uint32_t insn = *code_ptr;
471139c1837SPaolo Bonzini    intptr_t diff;
472139c1837SPaolo Bonzini    bool short_jmp;
473139c1837SPaolo Bonzini
474139c1837SPaolo Bonzini    tcg_debug_assert(addend == 0);
475139c1837SPaolo Bonzini
476139c1837SPaolo Bonzini    switch (type) {
477139c1837SPaolo Bonzini    case R_RISCV_BRANCH:
478139c1837SPaolo Bonzini        diff = value - (uintptr_t)code_ptr;
479139c1837SPaolo Bonzini        short_jmp = diff == sextreg(diff, 0, 12);
480139c1837SPaolo Bonzini        if (short_jmp) {
481139c1837SPaolo Bonzini            return reloc_sbimm12(code_ptr, (tcg_insn_unit *)value);
482139c1837SPaolo Bonzini        } else {
483139c1837SPaolo Bonzini            /* Invert the condition */
484139c1837SPaolo Bonzini            insn = insn ^ (1 << 12);
485139c1837SPaolo Bonzini            /* Clear the offset */
486139c1837SPaolo Bonzini            insn &= 0x01fff07f;
487139c1837SPaolo Bonzini            /* Set the offset to the PC + 8 */
488139c1837SPaolo Bonzini            insn |= encode_sbimm12(8);
489139c1837SPaolo Bonzini
490139c1837SPaolo Bonzini            /* Move forward */
491139c1837SPaolo Bonzini            code_ptr[0] = insn;
492139c1837SPaolo Bonzini
493139c1837SPaolo Bonzini            /* Overwrite the NOP with jal x0,value */
494139c1837SPaolo Bonzini            diff = value - (uintptr_t)(code_ptr + 1);
495139c1837SPaolo Bonzini            insn = encode_uj(OPC_JAL, TCG_REG_ZERO, diff);
496139c1837SPaolo Bonzini            code_ptr[1] = insn;
497139c1837SPaolo Bonzini
498139c1837SPaolo Bonzini            return true;
499139c1837SPaolo Bonzini        }
500139c1837SPaolo Bonzini        break;
501139c1837SPaolo Bonzini    case R_RISCV_JAL:
502139c1837SPaolo Bonzini        return reloc_jimm20(code_ptr, (tcg_insn_unit *)value);
503139c1837SPaolo Bonzini    case R_RISCV_CALL:
504139c1837SPaolo Bonzini        return reloc_call(code_ptr, (tcg_insn_unit *)value);
505139c1837SPaolo Bonzini    default:
506139c1837SPaolo Bonzini        tcg_abort();
507139c1837SPaolo Bonzini    }
508139c1837SPaolo Bonzini}
509139c1837SPaolo Bonzini
510139c1837SPaolo Bonzini/*
511139c1837SPaolo Bonzini * TCG intrinsics
512139c1837SPaolo Bonzini */
513139c1837SPaolo Bonzini
514139c1837SPaolo Bonzinistatic bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
515139c1837SPaolo Bonzini{
516139c1837SPaolo Bonzini    if (ret == arg) {
517139c1837SPaolo Bonzini        return true;
518139c1837SPaolo Bonzini    }
519139c1837SPaolo Bonzini    switch (type) {
520139c1837SPaolo Bonzini    case TCG_TYPE_I32:
521139c1837SPaolo Bonzini    case TCG_TYPE_I64:
522139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_ADDI, ret, arg, 0);
523139c1837SPaolo Bonzini        break;
524139c1837SPaolo Bonzini    default:
525139c1837SPaolo Bonzini        g_assert_not_reached();
526139c1837SPaolo Bonzini    }
527139c1837SPaolo Bonzini    return true;
528139c1837SPaolo Bonzini}
529139c1837SPaolo Bonzini
530139c1837SPaolo Bonzinistatic void tcg_out_movi(TCGContext *s, TCGType type, TCGReg rd,
531139c1837SPaolo Bonzini                         tcg_target_long val)
532139c1837SPaolo Bonzini{
533139c1837SPaolo Bonzini    tcg_target_long lo, hi, tmp;
534139c1837SPaolo Bonzini    int shift, ret;
535139c1837SPaolo Bonzini
536139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I32) {
537139c1837SPaolo Bonzini        val = (int32_t)val;
538139c1837SPaolo Bonzini    }
539139c1837SPaolo Bonzini
540139c1837SPaolo Bonzini    lo = sextreg(val, 0, 12);
541139c1837SPaolo Bonzini    if (val == lo) {
542139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_ADDI, rd, TCG_REG_ZERO, lo);
543139c1837SPaolo Bonzini        return;
544139c1837SPaolo Bonzini    }
545139c1837SPaolo Bonzini
546139c1837SPaolo Bonzini    hi = val - lo;
547139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS == 32 || val == (int32_t)val) {
548139c1837SPaolo Bonzini        tcg_out_opc_upper(s, OPC_LUI, rd, hi);
549139c1837SPaolo Bonzini        if (lo != 0) {
550139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_ADDIW, rd, rd, lo);
551139c1837SPaolo Bonzini        }
552139c1837SPaolo Bonzini        return;
553139c1837SPaolo Bonzini    }
554139c1837SPaolo Bonzini
555139c1837SPaolo Bonzini    /* We can only be here if TCG_TARGET_REG_BITS != 32 */
556139c1837SPaolo Bonzini    tmp = tcg_pcrel_diff(s, (void *)val);
557139c1837SPaolo Bonzini    if (tmp == (int32_t)tmp) {
558139c1837SPaolo Bonzini        tcg_out_opc_upper(s, OPC_AUIPC, rd, 0);
559139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_ADDI, rd, rd, 0);
560139c1837SPaolo Bonzini        ret = reloc_call(s->code_ptr - 2, (tcg_insn_unit *)val);
561139c1837SPaolo Bonzini        tcg_debug_assert(ret == true);
562139c1837SPaolo Bonzini        return;
563139c1837SPaolo Bonzini    }
564139c1837SPaolo Bonzini
565139c1837SPaolo Bonzini    /* Look for a single 20-bit section.  */
566139c1837SPaolo Bonzini    shift = ctz64(val);
567139c1837SPaolo Bonzini    tmp = val >> shift;
568139c1837SPaolo Bonzini    if (tmp == sextreg(tmp, 0, 20)) {
569139c1837SPaolo Bonzini        tcg_out_opc_upper(s, OPC_LUI, rd, tmp << 12);
570139c1837SPaolo Bonzini        if (shift > 12) {
571139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_SLLI, rd, rd, shift - 12);
572139c1837SPaolo Bonzini        } else {
573139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_SRAI, rd, rd, 12 - shift);
574139c1837SPaolo Bonzini        }
575139c1837SPaolo Bonzini        return;
576139c1837SPaolo Bonzini    }
577139c1837SPaolo Bonzini
578139c1837SPaolo Bonzini    /* Look for a few high zero bits, with lots of bits set in the middle.  */
579139c1837SPaolo Bonzini    shift = clz64(val);
580139c1837SPaolo Bonzini    tmp = val << shift;
581139c1837SPaolo Bonzini    if (tmp == sextreg(tmp, 12, 20) << 12) {
582139c1837SPaolo Bonzini        tcg_out_opc_upper(s, OPC_LUI, rd, tmp);
583139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_SRLI, rd, rd, shift);
584139c1837SPaolo Bonzini        return;
585139c1837SPaolo Bonzini    } else if (tmp == sextreg(tmp, 0, 12)) {
586139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_ADDI, rd, TCG_REG_ZERO, tmp);
587139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_SRLI, rd, rd, shift);
588139c1837SPaolo Bonzini        return;
589139c1837SPaolo Bonzini    }
590139c1837SPaolo Bonzini
591139c1837SPaolo Bonzini    /* Drop into the constant pool.  */
592139c1837SPaolo Bonzini    new_pool_label(s, val, R_RISCV_CALL, s->code_ptr, 0);
593139c1837SPaolo Bonzini    tcg_out_opc_upper(s, OPC_AUIPC, rd, 0);
594139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_LD, rd, rd, 0);
595139c1837SPaolo Bonzini}
596139c1837SPaolo Bonzini
597139c1837SPaolo Bonzinistatic void tcg_out_ext8u(TCGContext *s, TCGReg ret, TCGReg arg)
598139c1837SPaolo Bonzini{
599139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_ANDI, ret, arg, 0xff);
600139c1837SPaolo Bonzini}
601139c1837SPaolo Bonzini
602139c1837SPaolo Bonzinistatic void tcg_out_ext16u(TCGContext *s, TCGReg ret, TCGReg arg)
603139c1837SPaolo Bonzini{
604139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SLLIW, ret, arg, 16);
605139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SRLIW, ret, ret, 16);
606139c1837SPaolo Bonzini}
607139c1837SPaolo Bonzini
608139c1837SPaolo Bonzinistatic void tcg_out_ext32u(TCGContext *s, TCGReg ret, TCGReg arg)
609139c1837SPaolo Bonzini{
610139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SLLI, ret, arg, 32);
611139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SRLI, ret, ret, 32);
612139c1837SPaolo Bonzini}
613139c1837SPaolo Bonzini
614139c1837SPaolo Bonzinistatic void tcg_out_ext8s(TCGContext *s, TCGReg ret, TCGReg arg)
615139c1837SPaolo Bonzini{
616139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SLLIW, ret, arg, 24);
617139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SRAIW, ret, ret, 24);
618139c1837SPaolo Bonzini}
619139c1837SPaolo Bonzini
620139c1837SPaolo Bonzinistatic void tcg_out_ext16s(TCGContext *s, TCGReg ret, TCGReg arg)
621139c1837SPaolo Bonzini{
622139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SLLIW, ret, arg, 16);
623139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SRAIW, ret, ret, 16);
624139c1837SPaolo Bonzini}
625139c1837SPaolo Bonzini
626139c1837SPaolo Bonzinistatic void tcg_out_ext32s(TCGContext *s, TCGReg ret, TCGReg arg)
627139c1837SPaolo Bonzini{
628139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_ADDIW, ret, arg, 0);
629139c1837SPaolo Bonzini}
630139c1837SPaolo Bonzini
631139c1837SPaolo Bonzinistatic void tcg_out_ldst(TCGContext *s, RISCVInsn opc, TCGReg data,
632139c1837SPaolo Bonzini                         TCGReg addr, intptr_t offset)
633139c1837SPaolo Bonzini{
634139c1837SPaolo Bonzini    intptr_t imm12 = sextreg(offset, 0, 12);
635139c1837SPaolo Bonzini
636139c1837SPaolo Bonzini    if (offset != imm12) {
637139c1837SPaolo Bonzini        intptr_t diff = offset - (uintptr_t)s->code_ptr;
638139c1837SPaolo Bonzini
639139c1837SPaolo Bonzini        if (addr == TCG_REG_ZERO && diff == (int32_t)diff) {
640139c1837SPaolo Bonzini            imm12 = sextreg(diff, 0, 12);
641139c1837SPaolo Bonzini            tcg_out_opc_upper(s, OPC_AUIPC, TCG_REG_TMP2, diff - imm12);
642139c1837SPaolo Bonzini        } else {
643139c1837SPaolo Bonzini            tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP2, offset - imm12);
644139c1837SPaolo Bonzini            if (addr != TCG_REG_ZERO) {
645139c1837SPaolo Bonzini                tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP2, TCG_REG_TMP2, addr);
646139c1837SPaolo Bonzini            }
647139c1837SPaolo Bonzini        }
648139c1837SPaolo Bonzini        addr = TCG_REG_TMP2;
649139c1837SPaolo Bonzini    }
650139c1837SPaolo Bonzini
651139c1837SPaolo Bonzini    switch (opc) {
652139c1837SPaolo Bonzini    case OPC_SB:
653139c1837SPaolo Bonzini    case OPC_SH:
654139c1837SPaolo Bonzini    case OPC_SW:
655139c1837SPaolo Bonzini    case OPC_SD:
656139c1837SPaolo Bonzini        tcg_out_opc_store(s, opc, addr, data, imm12);
657139c1837SPaolo Bonzini        break;
658139c1837SPaolo Bonzini    case OPC_LB:
659139c1837SPaolo Bonzini    case OPC_LBU:
660139c1837SPaolo Bonzini    case OPC_LH:
661139c1837SPaolo Bonzini    case OPC_LHU:
662139c1837SPaolo Bonzini    case OPC_LW:
663139c1837SPaolo Bonzini    case OPC_LWU:
664139c1837SPaolo Bonzini    case OPC_LD:
665139c1837SPaolo Bonzini        tcg_out_opc_imm(s, opc, data, addr, imm12);
666139c1837SPaolo Bonzini        break;
667139c1837SPaolo Bonzini    default:
668139c1837SPaolo Bonzini        g_assert_not_reached();
669139c1837SPaolo Bonzini    }
670139c1837SPaolo Bonzini}
671139c1837SPaolo Bonzini
672139c1837SPaolo Bonzinistatic void tcg_out_ld(TCGContext *s, TCGType type, TCGReg arg,
673139c1837SPaolo Bonzini                       TCGReg arg1, intptr_t arg2)
674139c1837SPaolo Bonzini{
675139c1837SPaolo Bonzini    bool is32bit = (TCG_TARGET_REG_BITS == 32 || type == TCG_TYPE_I32);
676139c1837SPaolo Bonzini    tcg_out_ldst(s, is32bit ? OPC_LW : OPC_LD, arg, arg1, arg2);
677139c1837SPaolo Bonzini}
678139c1837SPaolo Bonzini
679139c1837SPaolo Bonzinistatic void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
680139c1837SPaolo Bonzini                       TCGReg arg1, intptr_t arg2)
681139c1837SPaolo Bonzini{
682139c1837SPaolo Bonzini    bool is32bit = (TCG_TARGET_REG_BITS == 32 || type == TCG_TYPE_I32);
683139c1837SPaolo Bonzini    tcg_out_ldst(s, is32bit ? OPC_SW : OPC_SD, arg, arg1, arg2);
684139c1837SPaolo Bonzini}
685139c1837SPaolo Bonzini
686139c1837SPaolo Bonzinistatic bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
687139c1837SPaolo Bonzini                        TCGReg base, intptr_t ofs)
688139c1837SPaolo Bonzini{
689139c1837SPaolo Bonzini    if (val == 0) {
690139c1837SPaolo Bonzini        tcg_out_st(s, type, TCG_REG_ZERO, base, ofs);
691139c1837SPaolo Bonzini        return true;
692139c1837SPaolo Bonzini    }
693139c1837SPaolo Bonzini    return false;
694139c1837SPaolo Bonzini}
695139c1837SPaolo Bonzini
696139c1837SPaolo Bonzinistatic void tcg_out_addsub2(TCGContext *s,
697139c1837SPaolo Bonzini                            TCGReg rl, TCGReg rh,
698139c1837SPaolo Bonzini                            TCGReg al, TCGReg ah,
699139c1837SPaolo Bonzini                            TCGArg bl, TCGArg bh,
700139c1837SPaolo Bonzini                            bool cbl, bool cbh, bool is_sub, bool is32bit)
701139c1837SPaolo Bonzini{
702139c1837SPaolo Bonzini    const RISCVInsn opc_add = is32bit ? OPC_ADDW : OPC_ADD;
703139c1837SPaolo Bonzini    const RISCVInsn opc_addi = is32bit ? OPC_ADDIW : OPC_ADDI;
704139c1837SPaolo Bonzini    const RISCVInsn opc_sub = is32bit ? OPC_SUBW : OPC_SUB;
705139c1837SPaolo Bonzini    TCGReg th = TCG_REG_TMP1;
706139c1837SPaolo Bonzini
707139c1837SPaolo Bonzini    /* If we have a negative constant such that negating it would
708139c1837SPaolo Bonzini       make the high part zero, we can (usually) eliminate one insn.  */
709139c1837SPaolo Bonzini    if (cbl && cbh && bh == -1 && bl != 0) {
710139c1837SPaolo Bonzini        bl = -bl;
711139c1837SPaolo Bonzini        bh = 0;
712139c1837SPaolo Bonzini        is_sub = !is_sub;
713139c1837SPaolo Bonzini    }
714139c1837SPaolo Bonzini
715139c1837SPaolo Bonzini    /* By operating on the high part first, we get to use the final
716139c1837SPaolo Bonzini       carry operation to move back from the temporary.  */
717139c1837SPaolo Bonzini    if (!cbh) {
718139c1837SPaolo Bonzini        tcg_out_opc_reg(s, (is_sub ? opc_sub : opc_add), th, ah, bh);
719139c1837SPaolo Bonzini    } else if (bh != 0 || ah == rl) {
720139c1837SPaolo Bonzini        tcg_out_opc_imm(s, opc_addi, th, ah, (is_sub ? -bh : bh));
721139c1837SPaolo Bonzini    } else {
722139c1837SPaolo Bonzini        th = ah;
723139c1837SPaolo Bonzini    }
724139c1837SPaolo Bonzini
725139c1837SPaolo Bonzini    /* Note that tcg optimization should eliminate the bl == 0 case.  */
726139c1837SPaolo Bonzini    if (is_sub) {
727139c1837SPaolo Bonzini        if (cbl) {
728139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_SLTIU, TCG_REG_TMP0, al, bl);
729139c1837SPaolo Bonzini            tcg_out_opc_imm(s, opc_addi, rl, al, -bl);
730139c1837SPaolo Bonzini        } else {
731139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SLTU, TCG_REG_TMP0, al, bl);
732139c1837SPaolo Bonzini            tcg_out_opc_reg(s, opc_sub, rl, al, bl);
733139c1837SPaolo Bonzini        }
734139c1837SPaolo Bonzini        tcg_out_opc_reg(s, opc_sub, rh, th, TCG_REG_TMP0);
735139c1837SPaolo Bonzini    } else {
736139c1837SPaolo Bonzini        if (cbl) {
737139c1837SPaolo Bonzini            tcg_out_opc_imm(s, opc_addi, rl, al, bl);
738139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_SLTIU, TCG_REG_TMP0, rl, bl);
739139c1837SPaolo Bonzini        } else if (rl == al && rl == bl) {
740139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_SLTI, TCG_REG_TMP0, al, 0);
741139c1837SPaolo Bonzini            tcg_out_opc_reg(s, opc_addi, rl, al, bl);
742139c1837SPaolo Bonzini        } else {
743139c1837SPaolo Bonzini            tcg_out_opc_reg(s, opc_add, rl, al, bl);
744139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SLTU, TCG_REG_TMP0,
745139c1837SPaolo Bonzini                            rl, (rl == bl ? al : bl));
746139c1837SPaolo Bonzini        }
747139c1837SPaolo Bonzini        tcg_out_opc_reg(s, opc_add, rh, th, TCG_REG_TMP0);
748139c1837SPaolo Bonzini    }
749139c1837SPaolo Bonzini}
750139c1837SPaolo Bonzini
751139c1837SPaolo Bonzinistatic const struct {
752139c1837SPaolo Bonzini    RISCVInsn op;
753139c1837SPaolo Bonzini    bool swap;
754139c1837SPaolo Bonzini} tcg_brcond_to_riscv[] = {
755139c1837SPaolo Bonzini    [TCG_COND_EQ] =  { OPC_BEQ,  false },
756139c1837SPaolo Bonzini    [TCG_COND_NE] =  { OPC_BNE,  false },
757139c1837SPaolo Bonzini    [TCG_COND_LT] =  { OPC_BLT,  false },
758139c1837SPaolo Bonzini    [TCG_COND_GE] =  { OPC_BGE,  false },
759139c1837SPaolo Bonzini    [TCG_COND_LE] =  { OPC_BGE,  true  },
760139c1837SPaolo Bonzini    [TCG_COND_GT] =  { OPC_BLT,  true  },
761139c1837SPaolo Bonzini    [TCG_COND_LTU] = { OPC_BLTU, false },
762139c1837SPaolo Bonzini    [TCG_COND_GEU] = { OPC_BGEU, false },
763139c1837SPaolo Bonzini    [TCG_COND_LEU] = { OPC_BGEU, true  },
764139c1837SPaolo Bonzini    [TCG_COND_GTU] = { OPC_BLTU, true  }
765139c1837SPaolo Bonzini};
766139c1837SPaolo Bonzini
767139c1837SPaolo Bonzinistatic void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1,
768139c1837SPaolo Bonzini                           TCGReg arg2, TCGLabel *l)
769139c1837SPaolo Bonzini{
770139c1837SPaolo Bonzini    RISCVInsn op = tcg_brcond_to_riscv[cond].op;
771139c1837SPaolo Bonzini
772139c1837SPaolo Bonzini    tcg_debug_assert(op != 0);
773139c1837SPaolo Bonzini
774139c1837SPaolo Bonzini    if (tcg_brcond_to_riscv[cond].swap) {
775139c1837SPaolo Bonzini        TCGReg t = arg1;
776139c1837SPaolo Bonzini        arg1 = arg2;
777139c1837SPaolo Bonzini        arg2 = t;
778139c1837SPaolo Bonzini    }
779139c1837SPaolo Bonzini
780139c1837SPaolo Bonzini    if (l->has_value) {
781139c1837SPaolo Bonzini        intptr_t diff = tcg_pcrel_diff(s, l->u.value_ptr);
782139c1837SPaolo Bonzini        if (diff == sextreg(diff, 0, 12)) {
783139c1837SPaolo Bonzini            tcg_out_opc_branch(s, op, arg1, arg2, diff);
784139c1837SPaolo Bonzini        } else {
785139c1837SPaolo Bonzini            /* Invert the conditional branch.  */
786139c1837SPaolo Bonzini            tcg_out_opc_branch(s, op ^ (1 << 12), arg1, arg2, 8);
787139c1837SPaolo Bonzini            tcg_out_opc_jump(s, OPC_JAL, TCG_REG_ZERO, diff - 4);
788139c1837SPaolo Bonzini        }
789139c1837SPaolo Bonzini    } else {
790139c1837SPaolo Bonzini        tcg_out_reloc(s, s->code_ptr, R_RISCV_BRANCH, l, 0);
791139c1837SPaolo Bonzini        tcg_out_opc_branch(s, op, arg1, arg2, 0);
792139c1837SPaolo Bonzini        /* NOP to allow patching later */
793139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_ADDI, TCG_REG_ZERO, TCG_REG_ZERO, 0);
794139c1837SPaolo Bonzini    }
795139c1837SPaolo Bonzini}
796139c1837SPaolo Bonzini
797139c1837SPaolo Bonzinistatic void tcg_out_setcond(TCGContext *s, TCGCond cond, TCGReg ret,
798139c1837SPaolo Bonzini                            TCGReg arg1, TCGReg arg2)
799139c1837SPaolo Bonzini{
800139c1837SPaolo Bonzini    switch (cond) {
801139c1837SPaolo Bonzini    case TCG_COND_EQ:
802139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SUB, ret, arg1, arg2);
803139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_SLTIU, ret, ret, 1);
804139c1837SPaolo Bonzini        break;
805139c1837SPaolo Bonzini    case TCG_COND_NE:
806139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SUB, ret, arg1, arg2);
807139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLTU, ret, TCG_REG_ZERO, ret);
808139c1837SPaolo Bonzini        break;
809139c1837SPaolo Bonzini    case TCG_COND_LT:
810139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLT, ret, arg1, arg2);
811139c1837SPaolo Bonzini        break;
812139c1837SPaolo Bonzini    case TCG_COND_GE:
813139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLT, ret, arg1, arg2);
814139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1);
815139c1837SPaolo Bonzini        break;
816139c1837SPaolo Bonzini    case TCG_COND_LE:
817139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLT, ret, arg2, arg1);
818139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1);
819139c1837SPaolo Bonzini        break;
820139c1837SPaolo Bonzini    case TCG_COND_GT:
821139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLT, ret, arg2, arg1);
822139c1837SPaolo Bonzini        break;
823139c1837SPaolo Bonzini    case TCG_COND_LTU:
824139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLTU, ret, arg1, arg2);
825139c1837SPaolo Bonzini        break;
826139c1837SPaolo Bonzini    case TCG_COND_GEU:
827139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLTU, ret, arg1, arg2);
828139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1);
829139c1837SPaolo Bonzini        break;
830139c1837SPaolo Bonzini    case TCG_COND_LEU:
831139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLTU, ret, arg2, arg1);
832139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1);
833139c1837SPaolo Bonzini        break;
834139c1837SPaolo Bonzini    case TCG_COND_GTU:
835139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLTU, ret, arg2, arg1);
836139c1837SPaolo Bonzini        break;
837139c1837SPaolo Bonzini    default:
838139c1837SPaolo Bonzini         g_assert_not_reached();
839139c1837SPaolo Bonzini         break;
840139c1837SPaolo Bonzini     }
841139c1837SPaolo Bonzini}
842139c1837SPaolo Bonzini
843139c1837SPaolo Bonzinistatic void tcg_out_brcond2(TCGContext *s, TCGCond cond, TCGReg al, TCGReg ah,
844139c1837SPaolo Bonzini                            TCGReg bl, TCGReg bh, TCGLabel *l)
845139c1837SPaolo Bonzini{
846139c1837SPaolo Bonzini    /* todo */
847139c1837SPaolo Bonzini    g_assert_not_reached();
848139c1837SPaolo Bonzini}
849139c1837SPaolo Bonzini
850139c1837SPaolo Bonzinistatic void tcg_out_setcond2(TCGContext *s, TCGCond cond, TCGReg ret,
851139c1837SPaolo Bonzini                             TCGReg al, TCGReg ah, TCGReg bl, TCGReg bh)
852139c1837SPaolo Bonzini{
853139c1837SPaolo Bonzini    /* todo */
854139c1837SPaolo Bonzini    g_assert_not_reached();
855139c1837SPaolo Bonzini}
856139c1837SPaolo Bonzini
857139c1837SPaolo Bonzinistatic inline void tcg_out_goto(TCGContext *s, tcg_insn_unit *target)
858139c1837SPaolo Bonzini{
859139c1837SPaolo Bonzini    ptrdiff_t offset = tcg_pcrel_diff(s, target);
860139c1837SPaolo Bonzini    tcg_debug_assert(offset == sextreg(offset, 1, 20) << 1);
861139c1837SPaolo Bonzini    tcg_out_opc_jump(s, OPC_JAL, TCG_REG_ZERO, offset);
862139c1837SPaolo Bonzini}
863139c1837SPaolo Bonzini
864*2be7d76bSRichard Hendersonstatic void tcg_out_call_int(TCGContext *s, const tcg_insn_unit *arg, bool tail)
865139c1837SPaolo Bonzini{
866139c1837SPaolo Bonzini    TCGReg link = tail ? TCG_REG_ZERO : TCG_REG_RA;
867139c1837SPaolo Bonzini    ptrdiff_t offset = tcg_pcrel_diff(s, arg);
868139c1837SPaolo Bonzini    int ret;
869139c1837SPaolo Bonzini
870139c1837SPaolo Bonzini    if (offset == sextreg(offset, 1, 20) << 1) {
871139c1837SPaolo Bonzini        /* short jump: -2097150 to 2097152 */
872139c1837SPaolo Bonzini        tcg_out_opc_jump(s, OPC_JAL, link, offset);
873139c1837SPaolo Bonzini    } else if (TCG_TARGET_REG_BITS == 32 ||
874139c1837SPaolo Bonzini        offset == sextreg(offset, 1, 31) << 1) {
875139c1837SPaolo Bonzini        /* long jump: -2147483646 to 2147483648 */
876139c1837SPaolo Bonzini        tcg_out_opc_upper(s, OPC_AUIPC, TCG_REG_TMP0, 0);
877139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_JALR, link, TCG_REG_TMP0, 0);
878139c1837SPaolo Bonzini        ret = reloc_call(s->code_ptr - 2, arg);\
879139c1837SPaolo Bonzini        tcg_debug_assert(ret == true);
880139c1837SPaolo Bonzini    } else if (TCG_TARGET_REG_BITS == 64) {
881139c1837SPaolo Bonzini        /* far jump: 64-bit */
882139c1837SPaolo Bonzini        tcg_target_long imm = sextreg((tcg_target_long)arg, 0, 12);
883139c1837SPaolo Bonzini        tcg_target_long base = (tcg_target_long)arg - imm;
884139c1837SPaolo Bonzini        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP0, base);
885139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_JALR, link, TCG_REG_TMP0, imm);
886139c1837SPaolo Bonzini    } else {
887139c1837SPaolo Bonzini        g_assert_not_reached();
888139c1837SPaolo Bonzini    }
889139c1837SPaolo Bonzini}
890139c1837SPaolo Bonzini
891*2be7d76bSRichard Hendersonstatic void tcg_out_call(TCGContext *s, const tcg_insn_unit *arg)
892139c1837SPaolo Bonzini{
893139c1837SPaolo Bonzini    tcg_out_call_int(s, arg, false);
894139c1837SPaolo Bonzini}
895139c1837SPaolo Bonzini
896139c1837SPaolo Bonzinistatic void tcg_out_mb(TCGContext *s, TCGArg a0)
897139c1837SPaolo Bonzini{
898139c1837SPaolo Bonzini    tcg_insn_unit insn = OPC_FENCE;
899139c1837SPaolo Bonzini
900139c1837SPaolo Bonzini    if (a0 & TCG_MO_LD_LD) {
901139c1837SPaolo Bonzini        insn |= 0x02200000;
902139c1837SPaolo Bonzini    }
903139c1837SPaolo Bonzini    if (a0 & TCG_MO_ST_LD) {
904139c1837SPaolo Bonzini        insn |= 0x01200000;
905139c1837SPaolo Bonzini    }
906139c1837SPaolo Bonzini    if (a0 & TCG_MO_LD_ST) {
907139c1837SPaolo Bonzini        insn |= 0x02100000;
908139c1837SPaolo Bonzini    }
909139c1837SPaolo Bonzini    if (a0 & TCG_MO_ST_ST) {
910139c1837SPaolo Bonzini        insn |= 0x02200000;
911139c1837SPaolo Bonzini    }
912139c1837SPaolo Bonzini    tcg_out32(s, insn);
913139c1837SPaolo Bonzini}
914139c1837SPaolo Bonzini
915139c1837SPaolo Bonzini/*
916139c1837SPaolo Bonzini * Load/store and TLB
917139c1837SPaolo Bonzini */
918139c1837SPaolo Bonzini
919139c1837SPaolo Bonzini#if defined(CONFIG_SOFTMMU)
920139c1837SPaolo Bonzini#include "../tcg-ldst.c.inc"
921139c1837SPaolo Bonzini
922139c1837SPaolo Bonzini/* helper signature: helper_ret_ld_mmu(CPUState *env, target_ulong addr,
923139c1837SPaolo Bonzini *                                     TCGMemOpIdx oi, uintptr_t ra)
924139c1837SPaolo Bonzini */
925139c1837SPaolo Bonzinistatic void * const qemu_ld_helpers[16] = {
926139c1837SPaolo Bonzini    [MO_UB]   = helper_ret_ldub_mmu,
927139c1837SPaolo Bonzini    [MO_SB]   = helper_ret_ldsb_mmu,
928139c1837SPaolo Bonzini    [MO_LEUW] = helper_le_lduw_mmu,
929139c1837SPaolo Bonzini    [MO_LESW] = helper_le_ldsw_mmu,
930139c1837SPaolo Bonzini    [MO_LEUL] = helper_le_ldul_mmu,
931139c1837SPaolo Bonzini#if TCG_TARGET_REG_BITS == 64
932139c1837SPaolo Bonzini    [MO_LESL] = helper_le_ldsl_mmu,
933139c1837SPaolo Bonzini#endif
934139c1837SPaolo Bonzini    [MO_LEQ]  = helper_le_ldq_mmu,
935139c1837SPaolo Bonzini    [MO_BEUW] = helper_be_lduw_mmu,
936139c1837SPaolo Bonzini    [MO_BESW] = helper_be_ldsw_mmu,
937139c1837SPaolo Bonzini    [MO_BEUL] = helper_be_ldul_mmu,
938139c1837SPaolo Bonzini#if TCG_TARGET_REG_BITS == 64
939139c1837SPaolo Bonzini    [MO_BESL] = helper_be_ldsl_mmu,
940139c1837SPaolo Bonzini#endif
941139c1837SPaolo Bonzini    [MO_BEQ]  = helper_be_ldq_mmu,
942139c1837SPaolo Bonzini};
943139c1837SPaolo Bonzini
944139c1837SPaolo Bonzini/* helper signature: helper_ret_st_mmu(CPUState *env, target_ulong addr,
945139c1837SPaolo Bonzini *                                     uintxx_t val, TCGMemOpIdx oi,
946139c1837SPaolo Bonzini *                                     uintptr_t ra)
947139c1837SPaolo Bonzini */
948139c1837SPaolo Bonzinistatic void * const qemu_st_helpers[16] = {
949139c1837SPaolo Bonzini    [MO_UB]   = helper_ret_stb_mmu,
950139c1837SPaolo Bonzini    [MO_LEUW] = helper_le_stw_mmu,
951139c1837SPaolo Bonzini    [MO_LEUL] = helper_le_stl_mmu,
952139c1837SPaolo Bonzini    [MO_LEQ]  = helper_le_stq_mmu,
953139c1837SPaolo Bonzini    [MO_BEUW] = helper_be_stw_mmu,
954139c1837SPaolo Bonzini    [MO_BEUL] = helper_be_stl_mmu,
955139c1837SPaolo Bonzini    [MO_BEQ]  = helper_be_stq_mmu,
956139c1837SPaolo Bonzini};
957139c1837SPaolo Bonzini
958139c1837SPaolo Bonzini/* We don't support oversize guests */
959139c1837SPaolo BonziniQEMU_BUILD_BUG_ON(TCG_TARGET_REG_BITS < TARGET_LONG_BITS);
960139c1837SPaolo Bonzini
961139c1837SPaolo Bonzini/* We expect to use a 12-bit negative offset from ENV.  */
962139c1837SPaolo BonziniQEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) > 0);
963139c1837SPaolo BonziniQEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) < -(1 << 11));
964139c1837SPaolo Bonzini
965139c1837SPaolo Bonzinistatic void tcg_out_tlb_load(TCGContext *s, TCGReg addrl,
966139c1837SPaolo Bonzini                             TCGReg addrh, TCGMemOpIdx oi,
967139c1837SPaolo Bonzini                             tcg_insn_unit **label_ptr, bool is_load)
968139c1837SPaolo Bonzini{
969139c1837SPaolo Bonzini    MemOp opc = get_memop(oi);
970139c1837SPaolo Bonzini    unsigned s_bits = opc & MO_SIZE;
971139c1837SPaolo Bonzini    unsigned a_bits = get_alignment_bits(opc);
972139c1837SPaolo Bonzini    tcg_target_long compare_mask;
973139c1837SPaolo Bonzini    int mem_index = get_mmuidx(oi);
974139c1837SPaolo Bonzini    int fast_ofs = TLB_MASK_TABLE_OFS(mem_index);
975139c1837SPaolo Bonzini    int mask_ofs = fast_ofs + offsetof(CPUTLBDescFast, mask);
976139c1837SPaolo Bonzini    int table_ofs = fast_ofs + offsetof(CPUTLBDescFast, table);
977139c1837SPaolo Bonzini    TCGReg mask_base = TCG_AREG0, table_base = TCG_AREG0;
978139c1837SPaolo Bonzini
979139c1837SPaolo Bonzini    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP0, mask_base, mask_ofs);
980139c1837SPaolo Bonzini    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP1, table_base, table_ofs);
981139c1837SPaolo Bonzini
982139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SRLI, TCG_REG_TMP2, addrl,
983139c1837SPaolo Bonzini                    TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS);
984139c1837SPaolo Bonzini    tcg_out_opc_reg(s, OPC_AND, TCG_REG_TMP2, TCG_REG_TMP2, TCG_REG_TMP0);
985139c1837SPaolo Bonzini    tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP2, TCG_REG_TMP2, TCG_REG_TMP1);
986139c1837SPaolo Bonzini
987139c1837SPaolo Bonzini    /* Load the tlb comparator and the addend.  */
988139c1837SPaolo Bonzini    tcg_out_ld(s, TCG_TYPE_TL, TCG_REG_TMP0, TCG_REG_TMP2,
989139c1837SPaolo Bonzini               is_load ? offsetof(CPUTLBEntry, addr_read)
990139c1837SPaolo Bonzini               : offsetof(CPUTLBEntry, addr_write));
991139c1837SPaolo Bonzini    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP2, TCG_REG_TMP2,
992139c1837SPaolo Bonzini               offsetof(CPUTLBEntry, addend));
993139c1837SPaolo Bonzini
994139c1837SPaolo Bonzini    /* We don't support unaligned accesses. */
995139c1837SPaolo Bonzini    if (a_bits < s_bits) {
996139c1837SPaolo Bonzini        a_bits = s_bits;
997139c1837SPaolo Bonzini    }
998139c1837SPaolo Bonzini    /* Clear the non-page, non-alignment bits from the address.  */
999139c1837SPaolo Bonzini    compare_mask = (tcg_target_long)TARGET_PAGE_MASK | ((1 << a_bits) - 1);
1000139c1837SPaolo Bonzini    if (compare_mask == sextreg(compare_mask, 0, 12)) {
1001139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_ANDI, TCG_REG_TMP1, addrl, compare_mask);
1002139c1837SPaolo Bonzini    } else {
1003139c1837SPaolo Bonzini        tcg_out_movi(s, TCG_TYPE_TL, TCG_REG_TMP1, compare_mask);
1004139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_AND, TCG_REG_TMP1, TCG_REG_TMP1, addrl);
1005139c1837SPaolo Bonzini    }
1006139c1837SPaolo Bonzini
1007139c1837SPaolo Bonzini    /* Compare masked address with the TLB entry. */
1008139c1837SPaolo Bonzini    label_ptr[0] = s->code_ptr;
1009139c1837SPaolo Bonzini    tcg_out_opc_branch(s, OPC_BNE, TCG_REG_TMP0, TCG_REG_TMP1, 0);
1010139c1837SPaolo Bonzini    /* NOP to allow patching later */
1011139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_ADDI, TCG_REG_ZERO, TCG_REG_ZERO, 0);
1012139c1837SPaolo Bonzini
1013139c1837SPaolo Bonzini    /* TLB Hit - translate address using addend.  */
1014139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS > TARGET_LONG_BITS) {
1015139c1837SPaolo Bonzini        tcg_out_ext32u(s, TCG_REG_TMP0, addrl);
1016139c1837SPaolo Bonzini        addrl = TCG_REG_TMP0;
1017139c1837SPaolo Bonzini    }
1018139c1837SPaolo Bonzini    tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP0, TCG_REG_TMP2, addrl);
1019139c1837SPaolo Bonzini}
1020139c1837SPaolo Bonzini
1021139c1837SPaolo Bonzinistatic void add_qemu_ldst_label(TCGContext *s, int is_ld, TCGMemOpIdx oi,
1022139c1837SPaolo Bonzini                                TCGType ext,
1023139c1837SPaolo Bonzini                                TCGReg datalo, TCGReg datahi,
1024139c1837SPaolo Bonzini                                TCGReg addrlo, TCGReg addrhi,
1025139c1837SPaolo Bonzini                                void *raddr, tcg_insn_unit **label_ptr)
1026139c1837SPaolo Bonzini{
1027139c1837SPaolo Bonzini    TCGLabelQemuLdst *label = new_ldst_label(s);
1028139c1837SPaolo Bonzini
1029139c1837SPaolo Bonzini    label->is_ld = is_ld;
1030139c1837SPaolo Bonzini    label->oi = oi;
1031139c1837SPaolo Bonzini    label->type = ext;
1032139c1837SPaolo Bonzini    label->datalo_reg = datalo;
1033139c1837SPaolo Bonzini    label->datahi_reg = datahi;
1034139c1837SPaolo Bonzini    label->addrlo_reg = addrlo;
1035139c1837SPaolo Bonzini    label->addrhi_reg = addrhi;
1036139c1837SPaolo Bonzini    label->raddr = raddr;
1037139c1837SPaolo Bonzini    label->label_ptr[0] = label_ptr[0];
1038139c1837SPaolo Bonzini}
1039139c1837SPaolo Bonzini
1040139c1837SPaolo Bonzinistatic bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
1041139c1837SPaolo Bonzini{
1042139c1837SPaolo Bonzini    TCGMemOpIdx oi = l->oi;
1043139c1837SPaolo Bonzini    MemOp opc = get_memop(oi);
1044139c1837SPaolo Bonzini    TCGReg a0 = tcg_target_call_iarg_regs[0];
1045139c1837SPaolo Bonzini    TCGReg a1 = tcg_target_call_iarg_regs[1];
1046139c1837SPaolo Bonzini    TCGReg a2 = tcg_target_call_iarg_regs[2];
1047139c1837SPaolo Bonzini    TCGReg a3 = tcg_target_call_iarg_regs[3];
1048139c1837SPaolo Bonzini
1049139c1837SPaolo Bonzini    /* We don't support oversize guests */
1050139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS < TARGET_LONG_BITS) {
1051139c1837SPaolo Bonzini        g_assert_not_reached();
1052139c1837SPaolo Bonzini    }
1053139c1837SPaolo Bonzini
1054139c1837SPaolo Bonzini    /* resolve label address */
1055139c1837SPaolo Bonzini    if (!patch_reloc(l->label_ptr[0], R_RISCV_BRANCH,
1056139c1837SPaolo Bonzini                     (intptr_t) s->code_ptr, 0)) {
1057139c1837SPaolo Bonzini        return false;
1058139c1837SPaolo Bonzini    }
1059139c1837SPaolo Bonzini
1060139c1837SPaolo Bonzini    /* call load helper */
1061139c1837SPaolo Bonzini    tcg_out_mov(s, TCG_TYPE_PTR, a0, TCG_AREG0);
1062139c1837SPaolo Bonzini    tcg_out_mov(s, TCG_TYPE_PTR, a1, l->addrlo_reg);
1063139c1837SPaolo Bonzini    tcg_out_movi(s, TCG_TYPE_PTR, a2, oi);
1064139c1837SPaolo Bonzini    tcg_out_movi(s, TCG_TYPE_PTR, a3, (tcg_target_long)l->raddr);
1065139c1837SPaolo Bonzini
1066139c1837SPaolo Bonzini    tcg_out_call(s, qemu_ld_helpers[opc & (MO_BSWAP | MO_SSIZE)]);
1067139c1837SPaolo Bonzini    tcg_out_mov(s, (opc & MO_SIZE) == MO_64, l->datalo_reg, a0);
1068139c1837SPaolo Bonzini
1069139c1837SPaolo Bonzini    tcg_out_goto(s, l->raddr);
1070139c1837SPaolo Bonzini    return true;
1071139c1837SPaolo Bonzini}
1072139c1837SPaolo Bonzini
1073139c1837SPaolo Bonzinistatic bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
1074139c1837SPaolo Bonzini{
1075139c1837SPaolo Bonzini    TCGMemOpIdx oi = l->oi;
1076139c1837SPaolo Bonzini    MemOp opc = get_memop(oi);
1077139c1837SPaolo Bonzini    MemOp s_bits = opc & MO_SIZE;
1078139c1837SPaolo Bonzini    TCGReg a0 = tcg_target_call_iarg_regs[0];
1079139c1837SPaolo Bonzini    TCGReg a1 = tcg_target_call_iarg_regs[1];
1080139c1837SPaolo Bonzini    TCGReg a2 = tcg_target_call_iarg_regs[2];
1081139c1837SPaolo Bonzini    TCGReg a3 = tcg_target_call_iarg_regs[3];
1082139c1837SPaolo Bonzini    TCGReg a4 = tcg_target_call_iarg_regs[4];
1083139c1837SPaolo Bonzini
1084139c1837SPaolo Bonzini    /* We don't support oversize guests */
1085139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS < TARGET_LONG_BITS) {
1086139c1837SPaolo Bonzini        g_assert_not_reached();
1087139c1837SPaolo Bonzini    }
1088139c1837SPaolo Bonzini
1089139c1837SPaolo Bonzini    /* resolve label address */
1090139c1837SPaolo Bonzini    if (!patch_reloc(l->label_ptr[0], R_RISCV_BRANCH,
1091139c1837SPaolo Bonzini                     (intptr_t) s->code_ptr, 0)) {
1092139c1837SPaolo Bonzini        return false;
1093139c1837SPaolo Bonzini    }
1094139c1837SPaolo Bonzini
1095139c1837SPaolo Bonzini    /* call store helper */
1096139c1837SPaolo Bonzini    tcg_out_mov(s, TCG_TYPE_PTR, a0, TCG_AREG0);
1097139c1837SPaolo Bonzini    tcg_out_mov(s, TCG_TYPE_PTR, a1, l->addrlo_reg);
1098139c1837SPaolo Bonzini    tcg_out_mov(s, TCG_TYPE_PTR, a2, l->datalo_reg);
1099139c1837SPaolo Bonzini    switch (s_bits) {
1100139c1837SPaolo Bonzini    case MO_8:
1101139c1837SPaolo Bonzini        tcg_out_ext8u(s, a2, a2);
1102139c1837SPaolo Bonzini        break;
1103139c1837SPaolo Bonzini    case MO_16:
1104139c1837SPaolo Bonzini        tcg_out_ext16u(s, a2, a2);
1105139c1837SPaolo Bonzini        break;
1106139c1837SPaolo Bonzini    default:
1107139c1837SPaolo Bonzini        break;
1108139c1837SPaolo Bonzini    }
1109139c1837SPaolo Bonzini    tcg_out_movi(s, TCG_TYPE_PTR, a3, oi);
1110139c1837SPaolo Bonzini    tcg_out_movi(s, TCG_TYPE_PTR, a4, (tcg_target_long)l->raddr);
1111139c1837SPaolo Bonzini
1112139c1837SPaolo Bonzini    tcg_out_call(s, qemu_st_helpers[opc & (MO_BSWAP | MO_SSIZE)]);
1113139c1837SPaolo Bonzini
1114139c1837SPaolo Bonzini    tcg_out_goto(s, l->raddr);
1115139c1837SPaolo Bonzini    return true;
1116139c1837SPaolo Bonzini}
1117139c1837SPaolo Bonzini#endif /* CONFIG_SOFTMMU */
1118139c1837SPaolo Bonzini
1119139c1837SPaolo Bonzinistatic void tcg_out_qemu_ld_direct(TCGContext *s, TCGReg lo, TCGReg hi,
1120139c1837SPaolo Bonzini                                   TCGReg base, MemOp opc, bool is_64)
1121139c1837SPaolo Bonzini{
1122139c1837SPaolo Bonzini    const MemOp bswap = opc & MO_BSWAP;
1123139c1837SPaolo Bonzini
1124139c1837SPaolo Bonzini    /* We don't yet handle byteswapping, assert */
1125139c1837SPaolo Bonzini    g_assert(!bswap);
1126139c1837SPaolo Bonzini
1127139c1837SPaolo Bonzini    switch (opc & (MO_SSIZE)) {
1128139c1837SPaolo Bonzini    case MO_UB:
1129139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_LBU, lo, base, 0);
1130139c1837SPaolo Bonzini        break;
1131139c1837SPaolo Bonzini    case MO_SB:
1132139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_LB, lo, base, 0);
1133139c1837SPaolo Bonzini        break;
1134139c1837SPaolo Bonzini    case MO_UW:
1135139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_LHU, lo, base, 0);
1136139c1837SPaolo Bonzini        break;
1137139c1837SPaolo Bonzini    case MO_SW:
1138139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_LH, lo, base, 0);
1139139c1837SPaolo Bonzini        break;
1140139c1837SPaolo Bonzini    case MO_UL:
1141139c1837SPaolo Bonzini        if (TCG_TARGET_REG_BITS == 64 && is_64) {
1142139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_LWU, lo, base, 0);
1143139c1837SPaolo Bonzini            break;
1144139c1837SPaolo Bonzini        }
1145139c1837SPaolo Bonzini        /* FALLTHRU */
1146139c1837SPaolo Bonzini    case MO_SL:
1147139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_LW, lo, base, 0);
1148139c1837SPaolo Bonzini        break;
1149139c1837SPaolo Bonzini    case MO_Q:
1150139c1837SPaolo Bonzini        /* Prefer to load from offset 0 first, but allow for overlap.  */
1151139c1837SPaolo Bonzini        if (TCG_TARGET_REG_BITS == 64) {
1152139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_LD, lo, base, 0);
1153139c1837SPaolo Bonzini        } else if (lo != base) {
1154139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_LW, lo, base, 0);
1155139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_LW, hi, base, 4);
1156139c1837SPaolo Bonzini        } else {
1157139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_LW, hi, base, 4);
1158139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_LW, lo, base, 0);
1159139c1837SPaolo Bonzini        }
1160139c1837SPaolo Bonzini        break;
1161139c1837SPaolo Bonzini    default:
1162139c1837SPaolo Bonzini        g_assert_not_reached();
1163139c1837SPaolo Bonzini    }
1164139c1837SPaolo Bonzini}
1165139c1837SPaolo Bonzini
1166139c1837SPaolo Bonzinistatic void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args, bool is_64)
1167139c1837SPaolo Bonzini{
1168139c1837SPaolo Bonzini    TCGReg addr_regl, addr_regh __attribute__((unused));
1169139c1837SPaolo Bonzini    TCGReg data_regl, data_regh;
1170139c1837SPaolo Bonzini    TCGMemOpIdx oi;
1171139c1837SPaolo Bonzini    MemOp opc;
1172139c1837SPaolo Bonzini#if defined(CONFIG_SOFTMMU)
1173139c1837SPaolo Bonzini    tcg_insn_unit *label_ptr[1];
1174139c1837SPaolo Bonzini#endif
1175139c1837SPaolo Bonzini    TCGReg base = TCG_REG_TMP0;
1176139c1837SPaolo Bonzini
1177139c1837SPaolo Bonzini    data_regl = *args++;
1178139c1837SPaolo Bonzini    data_regh = (TCG_TARGET_REG_BITS == 32 && is_64 ? *args++ : 0);
1179139c1837SPaolo Bonzini    addr_regl = *args++;
1180139c1837SPaolo Bonzini    addr_regh = (TCG_TARGET_REG_BITS < TARGET_LONG_BITS ? *args++ : 0);
1181139c1837SPaolo Bonzini    oi = *args++;
1182139c1837SPaolo Bonzini    opc = get_memop(oi);
1183139c1837SPaolo Bonzini
1184139c1837SPaolo Bonzini#if defined(CONFIG_SOFTMMU)
1185139c1837SPaolo Bonzini    tcg_out_tlb_load(s, addr_regl, addr_regh, oi, label_ptr, 1);
1186139c1837SPaolo Bonzini    tcg_out_qemu_ld_direct(s, data_regl, data_regh, base, opc, is_64);
1187139c1837SPaolo Bonzini    add_qemu_ldst_label(s, 1, oi,
1188139c1837SPaolo Bonzini                        (is_64 ? TCG_TYPE_I64 : TCG_TYPE_I32),
1189139c1837SPaolo Bonzini                        data_regl, data_regh, addr_regl, addr_regh,
1190139c1837SPaolo Bonzini                        s->code_ptr, label_ptr);
1191139c1837SPaolo Bonzini#else
1192139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS > TARGET_LONG_BITS) {
1193139c1837SPaolo Bonzini        tcg_out_ext32u(s, base, addr_regl);
1194139c1837SPaolo Bonzini        addr_regl = base;
1195139c1837SPaolo Bonzini    }
1196139c1837SPaolo Bonzini
1197139c1837SPaolo Bonzini    if (guest_base == 0) {
1198139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_ADD, base, addr_regl, TCG_REG_ZERO);
1199139c1837SPaolo Bonzini    } else {
1200139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_ADD, base, TCG_GUEST_BASE_REG, addr_regl);
1201139c1837SPaolo Bonzini    }
1202139c1837SPaolo Bonzini    tcg_out_qemu_ld_direct(s, data_regl, data_regh, base, opc, is_64);
1203139c1837SPaolo Bonzini#endif
1204139c1837SPaolo Bonzini}
1205139c1837SPaolo Bonzini
1206139c1837SPaolo Bonzinistatic void tcg_out_qemu_st_direct(TCGContext *s, TCGReg lo, TCGReg hi,
1207139c1837SPaolo Bonzini                                   TCGReg base, MemOp opc)
1208139c1837SPaolo Bonzini{
1209139c1837SPaolo Bonzini    const MemOp bswap = opc & MO_BSWAP;
1210139c1837SPaolo Bonzini
1211139c1837SPaolo Bonzini    /* We don't yet handle byteswapping, assert */
1212139c1837SPaolo Bonzini    g_assert(!bswap);
1213139c1837SPaolo Bonzini
1214139c1837SPaolo Bonzini    switch (opc & (MO_SSIZE)) {
1215139c1837SPaolo Bonzini    case MO_8:
1216139c1837SPaolo Bonzini        tcg_out_opc_store(s, OPC_SB, base, lo, 0);
1217139c1837SPaolo Bonzini        break;
1218139c1837SPaolo Bonzini    case MO_16:
1219139c1837SPaolo Bonzini        tcg_out_opc_store(s, OPC_SH, base, lo, 0);
1220139c1837SPaolo Bonzini        break;
1221139c1837SPaolo Bonzini    case MO_32:
1222139c1837SPaolo Bonzini        tcg_out_opc_store(s, OPC_SW, base, lo, 0);
1223139c1837SPaolo Bonzini        break;
1224139c1837SPaolo Bonzini    case MO_64:
1225139c1837SPaolo Bonzini        if (TCG_TARGET_REG_BITS == 64) {
1226139c1837SPaolo Bonzini            tcg_out_opc_store(s, OPC_SD, base, lo, 0);
1227139c1837SPaolo Bonzini        } else {
1228139c1837SPaolo Bonzini            tcg_out_opc_store(s, OPC_SW, base, lo, 0);
1229139c1837SPaolo Bonzini            tcg_out_opc_store(s, OPC_SW, base, hi, 4);
1230139c1837SPaolo Bonzini        }
1231139c1837SPaolo Bonzini        break;
1232139c1837SPaolo Bonzini    default:
1233139c1837SPaolo Bonzini        g_assert_not_reached();
1234139c1837SPaolo Bonzini    }
1235139c1837SPaolo Bonzini}
1236139c1837SPaolo Bonzini
1237139c1837SPaolo Bonzinistatic void tcg_out_qemu_st(TCGContext *s, const TCGArg *args, bool is_64)
1238139c1837SPaolo Bonzini{
1239139c1837SPaolo Bonzini    TCGReg addr_regl, addr_regh __attribute__((unused));
1240139c1837SPaolo Bonzini    TCGReg data_regl, data_regh;
1241139c1837SPaolo Bonzini    TCGMemOpIdx oi;
1242139c1837SPaolo Bonzini    MemOp opc;
1243139c1837SPaolo Bonzini#if defined(CONFIG_SOFTMMU)
1244139c1837SPaolo Bonzini    tcg_insn_unit *label_ptr[1];
1245139c1837SPaolo Bonzini#endif
1246139c1837SPaolo Bonzini    TCGReg base = TCG_REG_TMP0;
1247139c1837SPaolo Bonzini
1248139c1837SPaolo Bonzini    data_regl = *args++;
1249139c1837SPaolo Bonzini    data_regh = (TCG_TARGET_REG_BITS == 32 && is_64 ? *args++ : 0);
1250139c1837SPaolo Bonzini    addr_regl = *args++;
1251139c1837SPaolo Bonzini    addr_regh = (TCG_TARGET_REG_BITS < TARGET_LONG_BITS ? *args++ : 0);
1252139c1837SPaolo Bonzini    oi = *args++;
1253139c1837SPaolo Bonzini    opc = get_memop(oi);
1254139c1837SPaolo Bonzini
1255139c1837SPaolo Bonzini#if defined(CONFIG_SOFTMMU)
1256139c1837SPaolo Bonzini    tcg_out_tlb_load(s, addr_regl, addr_regh, oi, label_ptr, 0);
1257139c1837SPaolo Bonzini    tcg_out_qemu_st_direct(s, data_regl, data_regh, base, opc);
1258139c1837SPaolo Bonzini    add_qemu_ldst_label(s, 0, oi,
1259139c1837SPaolo Bonzini                        (is_64 ? TCG_TYPE_I64 : TCG_TYPE_I32),
1260139c1837SPaolo Bonzini                        data_regl, data_regh, addr_regl, addr_regh,
1261139c1837SPaolo Bonzini                        s->code_ptr, label_ptr);
1262139c1837SPaolo Bonzini#else
1263139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS > TARGET_LONG_BITS) {
1264139c1837SPaolo Bonzini        tcg_out_ext32u(s, base, addr_regl);
1265139c1837SPaolo Bonzini        addr_regl = base;
1266139c1837SPaolo Bonzini    }
1267139c1837SPaolo Bonzini
1268139c1837SPaolo Bonzini    if (guest_base == 0) {
1269139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_ADD, base, addr_regl, TCG_REG_ZERO);
1270139c1837SPaolo Bonzini    } else {
1271139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_ADD, base, TCG_GUEST_BASE_REG, addr_regl);
1272139c1837SPaolo Bonzini    }
1273139c1837SPaolo Bonzini    tcg_out_qemu_st_direct(s, data_regl, data_regh, base, opc);
1274139c1837SPaolo Bonzini#endif
1275139c1837SPaolo Bonzini}
1276139c1837SPaolo Bonzini
1277139c1837SPaolo Bonzinistatic tcg_insn_unit *tb_ret_addr;
1278139c1837SPaolo Bonzini
1279139c1837SPaolo Bonzinistatic void tcg_out_op(TCGContext *s, TCGOpcode opc,
1280139c1837SPaolo Bonzini                       const TCGArg *args, const int *const_args)
1281139c1837SPaolo Bonzini{
1282139c1837SPaolo Bonzini    TCGArg a0 = args[0];
1283139c1837SPaolo Bonzini    TCGArg a1 = args[1];
1284139c1837SPaolo Bonzini    TCGArg a2 = args[2];
1285139c1837SPaolo Bonzini    int c2 = const_args[2];
1286139c1837SPaolo Bonzini
1287139c1837SPaolo Bonzini    switch (opc) {
1288139c1837SPaolo Bonzini    case INDEX_op_exit_tb:
1289139c1837SPaolo Bonzini        /* Reuse the zeroing that exists for goto_ptr.  */
1290139c1837SPaolo Bonzini        if (a0 == 0) {
12918b5c2b62SRichard Henderson            tcg_out_call_int(s, tcg_code_gen_epilogue, true);
1292139c1837SPaolo Bonzini        } else {
1293139c1837SPaolo Bonzini            tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_A0, a0);
1294139c1837SPaolo Bonzini            tcg_out_call_int(s, tb_ret_addr, true);
1295139c1837SPaolo Bonzini        }
1296139c1837SPaolo Bonzini        break;
1297139c1837SPaolo Bonzini
1298139c1837SPaolo Bonzini    case INDEX_op_goto_tb:
1299139c1837SPaolo Bonzini        assert(s->tb_jmp_insn_offset == 0);
1300139c1837SPaolo Bonzini        /* indirect jump method */
1301139c1837SPaolo Bonzini        tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP0, TCG_REG_ZERO,
1302139c1837SPaolo Bonzini                   (uintptr_t)(s->tb_jmp_target_addr + a0));
1303139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_JALR, TCG_REG_ZERO, TCG_REG_TMP0, 0);
1304139c1837SPaolo Bonzini        set_jmp_reset_offset(s, a0);
1305139c1837SPaolo Bonzini        break;
1306139c1837SPaolo Bonzini
1307139c1837SPaolo Bonzini    case INDEX_op_goto_ptr:
1308139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_JALR, TCG_REG_ZERO, a0, 0);
1309139c1837SPaolo Bonzini        break;
1310139c1837SPaolo Bonzini
1311139c1837SPaolo Bonzini    case INDEX_op_br:
1312139c1837SPaolo Bonzini        tcg_out_reloc(s, s->code_ptr, R_RISCV_JAL, arg_label(a0), 0);
1313139c1837SPaolo Bonzini        tcg_out_opc_jump(s, OPC_JAL, TCG_REG_ZERO, 0);
1314139c1837SPaolo Bonzini        break;
1315139c1837SPaolo Bonzini
1316139c1837SPaolo Bonzini    case INDEX_op_ld8u_i32:
1317139c1837SPaolo Bonzini    case INDEX_op_ld8u_i64:
1318139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_LBU, a0, a1, a2);
1319139c1837SPaolo Bonzini        break;
1320139c1837SPaolo Bonzini    case INDEX_op_ld8s_i32:
1321139c1837SPaolo Bonzini    case INDEX_op_ld8s_i64:
1322139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_LB, a0, a1, a2);
1323139c1837SPaolo Bonzini        break;
1324139c1837SPaolo Bonzini    case INDEX_op_ld16u_i32:
1325139c1837SPaolo Bonzini    case INDEX_op_ld16u_i64:
1326139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_LHU, a0, a1, a2);
1327139c1837SPaolo Bonzini        break;
1328139c1837SPaolo Bonzini    case INDEX_op_ld16s_i32:
1329139c1837SPaolo Bonzini    case INDEX_op_ld16s_i64:
1330139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_LH, a0, a1, a2);
1331139c1837SPaolo Bonzini        break;
1332139c1837SPaolo Bonzini    case INDEX_op_ld32u_i64:
1333139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_LWU, a0, a1, a2);
1334139c1837SPaolo Bonzini        break;
1335139c1837SPaolo Bonzini    case INDEX_op_ld_i32:
1336139c1837SPaolo Bonzini    case INDEX_op_ld32s_i64:
1337139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_LW, a0, a1, a2);
1338139c1837SPaolo Bonzini        break;
1339139c1837SPaolo Bonzini    case INDEX_op_ld_i64:
1340139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_LD, a0, a1, a2);
1341139c1837SPaolo Bonzini        break;
1342139c1837SPaolo Bonzini
1343139c1837SPaolo Bonzini    case INDEX_op_st8_i32:
1344139c1837SPaolo Bonzini    case INDEX_op_st8_i64:
1345139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_SB, a0, a1, a2);
1346139c1837SPaolo Bonzini        break;
1347139c1837SPaolo Bonzini    case INDEX_op_st16_i32:
1348139c1837SPaolo Bonzini    case INDEX_op_st16_i64:
1349139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_SH, a0, a1, a2);
1350139c1837SPaolo Bonzini        break;
1351139c1837SPaolo Bonzini    case INDEX_op_st_i32:
1352139c1837SPaolo Bonzini    case INDEX_op_st32_i64:
1353139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_SW, a0, a1, a2);
1354139c1837SPaolo Bonzini        break;
1355139c1837SPaolo Bonzini    case INDEX_op_st_i64:
1356139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_SD, a0, a1, a2);
1357139c1837SPaolo Bonzini        break;
1358139c1837SPaolo Bonzini
1359139c1837SPaolo Bonzini    case INDEX_op_add_i32:
1360139c1837SPaolo Bonzini        if (c2) {
1361139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_ADDIW, a0, a1, a2);
1362139c1837SPaolo Bonzini        } else {
1363139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_ADDW, a0, a1, a2);
1364139c1837SPaolo Bonzini        }
1365139c1837SPaolo Bonzini        break;
1366139c1837SPaolo Bonzini    case INDEX_op_add_i64:
1367139c1837SPaolo Bonzini        if (c2) {
1368139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_ADDI, a0, a1, a2);
1369139c1837SPaolo Bonzini        } else {
1370139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_ADD, a0, a1, a2);
1371139c1837SPaolo Bonzini        }
1372139c1837SPaolo Bonzini        break;
1373139c1837SPaolo Bonzini
1374139c1837SPaolo Bonzini    case INDEX_op_sub_i32:
1375139c1837SPaolo Bonzini        if (c2) {
1376139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_ADDIW, a0, a1, -a2);
1377139c1837SPaolo Bonzini        } else {
1378139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SUBW, a0, a1, a2);
1379139c1837SPaolo Bonzini        }
1380139c1837SPaolo Bonzini        break;
1381139c1837SPaolo Bonzini    case INDEX_op_sub_i64:
1382139c1837SPaolo Bonzini        if (c2) {
1383139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_ADDI, a0, a1, -a2);
1384139c1837SPaolo Bonzini        } else {
1385139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SUB, a0, a1, a2);
1386139c1837SPaolo Bonzini        }
1387139c1837SPaolo Bonzini        break;
1388139c1837SPaolo Bonzini
1389139c1837SPaolo Bonzini    case INDEX_op_and_i32:
1390139c1837SPaolo Bonzini    case INDEX_op_and_i64:
1391139c1837SPaolo Bonzini        if (c2) {
1392139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_ANDI, a0, a1, a2);
1393139c1837SPaolo Bonzini        } else {
1394139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_AND, a0, a1, a2);
1395139c1837SPaolo Bonzini        }
1396139c1837SPaolo Bonzini        break;
1397139c1837SPaolo Bonzini
1398139c1837SPaolo Bonzini    case INDEX_op_or_i32:
1399139c1837SPaolo Bonzini    case INDEX_op_or_i64:
1400139c1837SPaolo Bonzini        if (c2) {
1401139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_ORI, a0, a1, a2);
1402139c1837SPaolo Bonzini        } else {
1403139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_OR, a0, a1, a2);
1404139c1837SPaolo Bonzini        }
1405139c1837SPaolo Bonzini        break;
1406139c1837SPaolo Bonzini
1407139c1837SPaolo Bonzini    case INDEX_op_xor_i32:
1408139c1837SPaolo Bonzini    case INDEX_op_xor_i64:
1409139c1837SPaolo Bonzini        if (c2) {
1410139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_XORI, a0, a1, a2);
1411139c1837SPaolo Bonzini        } else {
1412139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_XOR, a0, a1, a2);
1413139c1837SPaolo Bonzini        }
1414139c1837SPaolo Bonzini        break;
1415139c1837SPaolo Bonzini
1416139c1837SPaolo Bonzini    case INDEX_op_not_i32:
1417139c1837SPaolo Bonzini    case INDEX_op_not_i64:
1418139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_XORI, a0, a1, -1);
1419139c1837SPaolo Bonzini        break;
1420139c1837SPaolo Bonzini
1421139c1837SPaolo Bonzini    case INDEX_op_neg_i32:
1422139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SUBW, a0, TCG_REG_ZERO, a1);
1423139c1837SPaolo Bonzini        break;
1424139c1837SPaolo Bonzini    case INDEX_op_neg_i64:
1425139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SUB, a0, TCG_REG_ZERO, a1);
1426139c1837SPaolo Bonzini        break;
1427139c1837SPaolo Bonzini
1428139c1837SPaolo Bonzini    case INDEX_op_mul_i32:
1429139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_MULW, a0, a1, a2);
1430139c1837SPaolo Bonzini        break;
1431139c1837SPaolo Bonzini    case INDEX_op_mul_i64:
1432139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_MUL, a0, a1, a2);
1433139c1837SPaolo Bonzini        break;
1434139c1837SPaolo Bonzini
1435139c1837SPaolo Bonzini    case INDEX_op_div_i32:
1436139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_DIVW, a0, a1, a2);
1437139c1837SPaolo Bonzini        break;
1438139c1837SPaolo Bonzini    case INDEX_op_div_i64:
1439139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_DIV, a0, a1, a2);
1440139c1837SPaolo Bonzini        break;
1441139c1837SPaolo Bonzini
1442139c1837SPaolo Bonzini    case INDEX_op_divu_i32:
1443139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_DIVUW, a0, a1, a2);
1444139c1837SPaolo Bonzini        break;
1445139c1837SPaolo Bonzini    case INDEX_op_divu_i64:
1446139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_DIVU, a0, a1, a2);
1447139c1837SPaolo Bonzini        break;
1448139c1837SPaolo Bonzini
1449139c1837SPaolo Bonzini    case INDEX_op_rem_i32:
1450139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_REMW, a0, a1, a2);
1451139c1837SPaolo Bonzini        break;
1452139c1837SPaolo Bonzini    case INDEX_op_rem_i64:
1453139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_REM, a0, a1, a2);
1454139c1837SPaolo Bonzini        break;
1455139c1837SPaolo Bonzini
1456139c1837SPaolo Bonzini    case INDEX_op_remu_i32:
1457139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_REMUW, a0, a1, a2);
1458139c1837SPaolo Bonzini        break;
1459139c1837SPaolo Bonzini    case INDEX_op_remu_i64:
1460139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_REMU, a0, a1, a2);
1461139c1837SPaolo Bonzini        break;
1462139c1837SPaolo Bonzini
1463139c1837SPaolo Bonzini    case INDEX_op_shl_i32:
1464139c1837SPaolo Bonzini        if (c2) {
1465d2f3066eSZihao Yu            tcg_out_opc_imm(s, OPC_SLLIW, a0, a1, a2 & 0x1f);
1466139c1837SPaolo Bonzini        } else {
1467139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SLLW, a0, a1, a2);
1468139c1837SPaolo Bonzini        }
1469139c1837SPaolo Bonzini        break;
1470139c1837SPaolo Bonzini    case INDEX_op_shl_i64:
1471139c1837SPaolo Bonzini        if (c2) {
1472d2f3066eSZihao Yu            tcg_out_opc_imm(s, OPC_SLLI, a0, a1, a2 & 0x3f);
1473139c1837SPaolo Bonzini        } else {
1474139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SLL, a0, a1, a2);
1475139c1837SPaolo Bonzini        }
1476139c1837SPaolo Bonzini        break;
1477139c1837SPaolo Bonzini
1478139c1837SPaolo Bonzini    case INDEX_op_shr_i32:
1479139c1837SPaolo Bonzini        if (c2) {
1480d2f3066eSZihao Yu            tcg_out_opc_imm(s, OPC_SRLIW, a0, a1, a2 & 0x1f);
1481139c1837SPaolo Bonzini        } else {
1482139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SRLW, a0, a1, a2);
1483139c1837SPaolo Bonzini        }
1484139c1837SPaolo Bonzini        break;
1485139c1837SPaolo Bonzini    case INDEX_op_shr_i64:
1486139c1837SPaolo Bonzini        if (c2) {
1487d2f3066eSZihao Yu            tcg_out_opc_imm(s, OPC_SRLI, a0, a1, a2 & 0x3f);
1488139c1837SPaolo Bonzini        } else {
1489139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SRL, a0, a1, a2);
1490139c1837SPaolo Bonzini        }
1491139c1837SPaolo Bonzini        break;
1492139c1837SPaolo Bonzini
1493139c1837SPaolo Bonzini    case INDEX_op_sar_i32:
1494139c1837SPaolo Bonzini        if (c2) {
1495d2f3066eSZihao Yu            tcg_out_opc_imm(s, OPC_SRAIW, a0, a1, a2 & 0x1f);
1496139c1837SPaolo Bonzini        } else {
1497139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SRAW, a0, a1, a2);
1498139c1837SPaolo Bonzini        }
1499139c1837SPaolo Bonzini        break;
1500139c1837SPaolo Bonzini    case INDEX_op_sar_i64:
1501139c1837SPaolo Bonzini        if (c2) {
1502d2f3066eSZihao Yu            tcg_out_opc_imm(s, OPC_SRAI, a0, a1, a2 & 0x3f);
1503139c1837SPaolo Bonzini        } else {
1504139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SRA, a0, a1, a2);
1505139c1837SPaolo Bonzini        }
1506139c1837SPaolo Bonzini        break;
1507139c1837SPaolo Bonzini
1508139c1837SPaolo Bonzini    case INDEX_op_add2_i32:
1509139c1837SPaolo Bonzini        tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5],
1510139c1837SPaolo Bonzini                        const_args[4], const_args[5], false, true);
1511139c1837SPaolo Bonzini        break;
1512139c1837SPaolo Bonzini    case INDEX_op_add2_i64:
1513139c1837SPaolo Bonzini        tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5],
1514139c1837SPaolo Bonzini                        const_args[4], const_args[5], false, false);
1515139c1837SPaolo Bonzini        break;
1516139c1837SPaolo Bonzini    case INDEX_op_sub2_i32:
1517139c1837SPaolo Bonzini        tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5],
1518139c1837SPaolo Bonzini                        const_args[4], const_args[5], true, true);
1519139c1837SPaolo Bonzini        break;
1520139c1837SPaolo Bonzini    case INDEX_op_sub2_i64:
1521139c1837SPaolo Bonzini        tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5],
1522139c1837SPaolo Bonzini                        const_args[4], const_args[5], true, false);
1523139c1837SPaolo Bonzini        break;
1524139c1837SPaolo Bonzini
1525139c1837SPaolo Bonzini    case INDEX_op_brcond_i32:
1526139c1837SPaolo Bonzini    case INDEX_op_brcond_i64:
1527139c1837SPaolo Bonzini        tcg_out_brcond(s, a2, a0, a1, arg_label(args[3]));
1528139c1837SPaolo Bonzini        break;
1529139c1837SPaolo Bonzini    case INDEX_op_brcond2_i32:
1530139c1837SPaolo Bonzini        tcg_out_brcond2(s, args[4], a0, a1, a2, args[3], arg_label(args[5]));
1531139c1837SPaolo Bonzini        break;
1532139c1837SPaolo Bonzini
1533139c1837SPaolo Bonzini    case INDEX_op_setcond_i32:
1534139c1837SPaolo Bonzini    case INDEX_op_setcond_i64:
1535139c1837SPaolo Bonzini        tcg_out_setcond(s, args[3], a0, a1, a2);
1536139c1837SPaolo Bonzini        break;
1537139c1837SPaolo Bonzini    case INDEX_op_setcond2_i32:
1538139c1837SPaolo Bonzini        tcg_out_setcond2(s, args[5], a0, a1, a2, args[3], args[4]);
1539139c1837SPaolo Bonzini        break;
1540139c1837SPaolo Bonzini
1541139c1837SPaolo Bonzini    case INDEX_op_qemu_ld_i32:
1542139c1837SPaolo Bonzini        tcg_out_qemu_ld(s, args, false);
1543139c1837SPaolo Bonzini        break;
1544139c1837SPaolo Bonzini    case INDEX_op_qemu_ld_i64:
1545139c1837SPaolo Bonzini        tcg_out_qemu_ld(s, args, true);
1546139c1837SPaolo Bonzini        break;
1547139c1837SPaolo Bonzini    case INDEX_op_qemu_st_i32:
1548139c1837SPaolo Bonzini        tcg_out_qemu_st(s, args, false);
1549139c1837SPaolo Bonzini        break;
1550139c1837SPaolo Bonzini    case INDEX_op_qemu_st_i64:
1551139c1837SPaolo Bonzini        tcg_out_qemu_st(s, args, true);
1552139c1837SPaolo Bonzini        break;
1553139c1837SPaolo Bonzini
1554139c1837SPaolo Bonzini    case INDEX_op_ext8u_i32:
1555139c1837SPaolo Bonzini    case INDEX_op_ext8u_i64:
1556139c1837SPaolo Bonzini        tcg_out_ext8u(s, a0, a1);
1557139c1837SPaolo Bonzini        break;
1558139c1837SPaolo Bonzini
1559139c1837SPaolo Bonzini    case INDEX_op_ext16u_i32:
1560139c1837SPaolo Bonzini    case INDEX_op_ext16u_i64:
1561139c1837SPaolo Bonzini        tcg_out_ext16u(s, a0, a1);
1562139c1837SPaolo Bonzini        break;
1563139c1837SPaolo Bonzini
1564139c1837SPaolo Bonzini    case INDEX_op_ext32u_i64:
1565139c1837SPaolo Bonzini    case INDEX_op_extu_i32_i64:
1566139c1837SPaolo Bonzini        tcg_out_ext32u(s, a0, a1);
1567139c1837SPaolo Bonzini        break;
1568139c1837SPaolo Bonzini
1569139c1837SPaolo Bonzini    case INDEX_op_ext8s_i32:
1570139c1837SPaolo Bonzini    case INDEX_op_ext8s_i64:
1571139c1837SPaolo Bonzini        tcg_out_ext8s(s, a0, a1);
1572139c1837SPaolo Bonzini        break;
1573139c1837SPaolo Bonzini
1574139c1837SPaolo Bonzini    case INDEX_op_ext16s_i32:
1575139c1837SPaolo Bonzini    case INDEX_op_ext16s_i64:
1576139c1837SPaolo Bonzini        tcg_out_ext16s(s, a0, a1);
1577139c1837SPaolo Bonzini        break;
1578139c1837SPaolo Bonzini
1579139c1837SPaolo Bonzini    case INDEX_op_ext32s_i64:
1580139c1837SPaolo Bonzini    case INDEX_op_extrl_i64_i32:
1581139c1837SPaolo Bonzini    case INDEX_op_ext_i32_i64:
1582139c1837SPaolo Bonzini        tcg_out_ext32s(s, a0, a1);
1583139c1837SPaolo Bonzini        break;
1584139c1837SPaolo Bonzini
1585139c1837SPaolo Bonzini    case INDEX_op_extrh_i64_i32:
1586139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_SRAI, a0, a1, 32);
1587139c1837SPaolo Bonzini        break;
1588139c1837SPaolo Bonzini
1589139c1837SPaolo Bonzini    case INDEX_op_mulsh_i32:
1590139c1837SPaolo Bonzini    case INDEX_op_mulsh_i64:
1591139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_MULH, a0, a1, a2);
1592139c1837SPaolo Bonzini        break;
1593139c1837SPaolo Bonzini
1594139c1837SPaolo Bonzini    case INDEX_op_muluh_i32:
1595139c1837SPaolo Bonzini    case INDEX_op_muluh_i64:
1596139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_MULHU, a0, a1, a2);
1597139c1837SPaolo Bonzini        break;
1598139c1837SPaolo Bonzini
1599139c1837SPaolo Bonzini    case INDEX_op_mb:
1600139c1837SPaolo Bonzini        tcg_out_mb(s, a0);
1601139c1837SPaolo Bonzini        break;
1602139c1837SPaolo Bonzini
1603139c1837SPaolo Bonzini    case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
1604139c1837SPaolo Bonzini    case INDEX_op_mov_i64:
1605139c1837SPaolo Bonzini    case INDEX_op_movi_i32: /* Always emitted via tcg_out_movi.  */
1606139c1837SPaolo Bonzini    case INDEX_op_movi_i64:
1607139c1837SPaolo Bonzini    case INDEX_op_call:     /* Always emitted via tcg_out_call.  */
1608139c1837SPaolo Bonzini    default:
1609139c1837SPaolo Bonzini        g_assert_not_reached();
1610139c1837SPaolo Bonzini    }
1611139c1837SPaolo Bonzini}
1612139c1837SPaolo Bonzini
1613139c1837SPaolo Bonzinistatic const TCGTargetOpDef *tcg_target_op_def(TCGOpcode op)
1614139c1837SPaolo Bonzini{
1615139c1837SPaolo Bonzini    static const TCGTargetOpDef r
1616139c1837SPaolo Bonzini        = { .args_ct_str = { "r" } };
1617139c1837SPaolo Bonzini    static const TCGTargetOpDef r_r
1618139c1837SPaolo Bonzini        = { .args_ct_str = { "r", "r" } };
1619139c1837SPaolo Bonzini    static const TCGTargetOpDef rZ_r
1620139c1837SPaolo Bonzini        = { .args_ct_str = { "rZ", "r" } };
1621139c1837SPaolo Bonzini    static const TCGTargetOpDef rZ_rZ
1622139c1837SPaolo Bonzini        = { .args_ct_str = { "rZ", "rZ" } };
1623139c1837SPaolo Bonzini    static const TCGTargetOpDef rZ_rZ_rZ_rZ
1624139c1837SPaolo Bonzini        = { .args_ct_str = { "rZ", "rZ", "rZ", "rZ" } };
1625139c1837SPaolo Bonzini    static const TCGTargetOpDef r_r_ri
1626139c1837SPaolo Bonzini        = { .args_ct_str = { "r", "r", "ri" } };
1627139c1837SPaolo Bonzini    static const TCGTargetOpDef r_r_rI
1628139c1837SPaolo Bonzini        = { .args_ct_str = { "r", "r", "rI" } };
1629139c1837SPaolo Bonzini    static const TCGTargetOpDef r_rZ_rN
1630139c1837SPaolo Bonzini        = { .args_ct_str = { "r", "rZ", "rN" } };
1631139c1837SPaolo Bonzini    static const TCGTargetOpDef r_rZ_rZ
1632139c1837SPaolo Bonzini        = { .args_ct_str = { "r", "rZ", "rZ" } };
1633139c1837SPaolo Bonzini    static const TCGTargetOpDef r_rZ_rZ_rZ_rZ
1634139c1837SPaolo Bonzini        = { .args_ct_str = { "r", "rZ", "rZ", "rZ", "rZ" } };
1635139c1837SPaolo Bonzini    static const TCGTargetOpDef r_L
1636139c1837SPaolo Bonzini        = { .args_ct_str = { "r", "L" } };
1637139c1837SPaolo Bonzini    static const TCGTargetOpDef r_r_L
1638139c1837SPaolo Bonzini        = { .args_ct_str = { "r", "r", "L" } };
1639139c1837SPaolo Bonzini    static const TCGTargetOpDef r_L_L
1640139c1837SPaolo Bonzini        = { .args_ct_str = { "r", "L", "L" } };
1641139c1837SPaolo Bonzini    static const TCGTargetOpDef r_r_L_L
1642139c1837SPaolo Bonzini        = { .args_ct_str = { "r", "r", "L", "L" } };
1643139c1837SPaolo Bonzini    static const TCGTargetOpDef LZ_L
1644139c1837SPaolo Bonzini        = { .args_ct_str = { "LZ", "L" } };
1645139c1837SPaolo Bonzini    static const TCGTargetOpDef LZ_L_L
1646139c1837SPaolo Bonzini        = { .args_ct_str = { "LZ", "L", "L" } };
1647139c1837SPaolo Bonzini    static const TCGTargetOpDef LZ_LZ_L
1648139c1837SPaolo Bonzini        = { .args_ct_str = { "LZ", "LZ", "L" } };
1649139c1837SPaolo Bonzini    static const TCGTargetOpDef LZ_LZ_L_L
1650139c1837SPaolo Bonzini        = { .args_ct_str = { "LZ", "LZ", "L", "L" } };
1651139c1837SPaolo Bonzini    static const TCGTargetOpDef r_r_rZ_rZ_rM_rM
1652139c1837SPaolo Bonzini        = { .args_ct_str = { "r", "r", "rZ", "rZ", "rM", "rM" } };
1653139c1837SPaolo Bonzini
1654139c1837SPaolo Bonzini    switch (op) {
1655139c1837SPaolo Bonzini    case INDEX_op_goto_ptr:
1656139c1837SPaolo Bonzini        return &r;
1657139c1837SPaolo Bonzini
1658139c1837SPaolo Bonzini    case INDEX_op_ld8u_i32:
1659139c1837SPaolo Bonzini    case INDEX_op_ld8s_i32:
1660139c1837SPaolo Bonzini    case INDEX_op_ld16u_i32:
1661139c1837SPaolo Bonzini    case INDEX_op_ld16s_i32:
1662139c1837SPaolo Bonzini    case INDEX_op_ld_i32:
1663139c1837SPaolo Bonzini    case INDEX_op_not_i32:
1664139c1837SPaolo Bonzini    case INDEX_op_neg_i32:
1665139c1837SPaolo Bonzini    case INDEX_op_ld8u_i64:
1666139c1837SPaolo Bonzini    case INDEX_op_ld8s_i64:
1667139c1837SPaolo Bonzini    case INDEX_op_ld16u_i64:
1668139c1837SPaolo Bonzini    case INDEX_op_ld16s_i64:
1669139c1837SPaolo Bonzini    case INDEX_op_ld32s_i64:
1670139c1837SPaolo Bonzini    case INDEX_op_ld32u_i64:
1671139c1837SPaolo Bonzini    case INDEX_op_ld_i64:
1672139c1837SPaolo Bonzini    case INDEX_op_not_i64:
1673139c1837SPaolo Bonzini    case INDEX_op_neg_i64:
1674139c1837SPaolo Bonzini    case INDEX_op_ext8u_i32:
1675139c1837SPaolo Bonzini    case INDEX_op_ext8u_i64:
1676139c1837SPaolo Bonzini    case INDEX_op_ext16u_i32:
1677139c1837SPaolo Bonzini    case INDEX_op_ext16u_i64:
1678139c1837SPaolo Bonzini    case INDEX_op_ext32u_i64:
1679139c1837SPaolo Bonzini    case INDEX_op_extu_i32_i64:
1680139c1837SPaolo Bonzini    case INDEX_op_ext8s_i32:
1681139c1837SPaolo Bonzini    case INDEX_op_ext8s_i64:
1682139c1837SPaolo Bonzini    case INDEX_op_ext16s_i32:
1683139c1837SPaolo Bonzini    case INDEX_op_ext16s_i64:
1684139c1837SPaolo Bonzini    case INDEX_op_ext32s_i64:
1685139c1837SPaolo Bonzini    case INDEX_op_extrl_i64_i32:
1686139c1837SPaolo Bonzini    case INDEX_op_extrh_i64_i32:
1687139c1837SPaolo Bonzini    case INDEX_op_ext_i32_i64:
1688139c1837SPaolo Bonzini        return &r_r;
1689139c1837SPaolo Bonzini
1690139c1837SPaolo Bonzini    case INDEX_op_st8_i32:
1691139c1837SPaolo Bonzini    case INDEX_op_st16_i32:
1692139c1837SPaolo Bonzini    case INDEX_op_st_i32:
1693139c1837SPaolo Bonzini    case INDEX_op_st8_i64:
1694139c1837SPaolo Bonzini    case INDEX_op_st16_i64:
1695139c1837SPaolo Bonzini    case INDEX_op_st32_i64:
1696139c1837SPaolo Bonzini    case INDEX_op_st_i64:
1697139c1837SPaolo Bonzini        return &rZ_r;
1698139c1837SPaolo Bonzini
1699139c1837SPaolo Bonzini    case INDEX_op_add_i32:
1700139c1837SPaolo Bonzini    case INDEX_op_and_i32:
1701139c1837SPaolo Bonzini    case INDEX_op_or_i32:
1702139c1837SPaolo Bonzini    case INDEX_op_xor_i32:
1703139c1837SPaolo Bonzini    case INDEX_op_add_i64:
1704139c1837SPaolo Bonzini    case INDEX_op_and_i64:
1705139c1837SPaolo Bonzini    case INDEX_op_or_i64:
1706139c1837SPaolo Bonzini    case INDEX_op_xor_i64:
1707139c1837SPaolo Bonzini        return &r_r_rI;
1708139c1837SPaolo Bonzini
1709139c1837SPaolo Bonzini    case INDEX_op_sub_i32:
1710139c1837SPaolo Bonzini    case INDEX_op_sub_i64:
1711139c1837SPaolo Bonzini        return &r_rZ_rN;
1712139c1837SPaolo Bonzini
1713139c1837SPaolo Bonzini    case INDEX_op_mul_i32:
1714139c1837SPaolo Bonzini    case INDEX_op_mulsh_i32:
1715139c1837SPaolo Bonzini    case INDEX_op_muluh_i32:
1716139c1837SPaolo Bonzini    case INDEX_op_div_i32:
1717139c1837SPaolo Bonzini    case INDEX_op_divu_i32:
1718139c1837SPaolo Bonzini    case INDEX_op_rem_i32:
1719139c1837SPaolo Bonzini    case INDEX_op_remu_i32:
1720139c1837SPaolo Bonzini    case INDEX_op_setcond_i32:
1721139c1837SPaolo Bonzini    case INDEX_op_mul_i64:
1722139c1837SPaolo Bonzini    case INDEX_op_mulsh_i64:
1723139c1837SPaolo Bonzini    case INDEX_op_muluh_i64:
1724139c1837SPaolo Bonzini    case INDEX_op_div_i64:
1725139c1837SPaolo Bonzini    case INDEX_op_divu_i64:
1726139c1837SPaolo Bonzini    case INDEX_op_rem_i64:
1727139c1837SPaolo Bonzini    case INDEX_op_remu_i64:
1728139c1837SPaolo Bonzini    case INDEX_op_setcond_i64:
1729139c1837SPaolo Bonzini        return &r_rZ_rZ;
1730139c1837SPaolo Bonzini
1731139c1837SPaolo Bonzini    case INDEX_op_shl_i32:
1732139c1837SPaolo Bonzini    case INDEX_op_shr_i32:
1733139c1837SPaolo Bonzini    case INDEX_op_sar_i32:
1734139c1837SPaolo Bonzini    case INDEX_op_shl_i64:
1735139c1837SPaolo Bonzini    case INDEX_op_shr_i64:
1736139c1837SPaolo Bonzini    case INDEX_op_sar_i64:
1737139c1837SPaolo Bonzini        return &r_r_ri;
1738139c1837SPaolo Bonzini
1739139c1837SPaolo Bonzini    case INDEX_op_brcond_i32:
1740139c1837SPaolo Bonzini    case INDEX_op_brcond_i64:
1741139c1837SPaolo Bonzini        return &rZ_rZ;
1742139c1837SPaolo Bonzini
1743139c1837SPaolo Bonzini    case INDEX_op_add2_i32:
1744139c1837SPaolo Bonzini    case INDEX_op_add2_i64:
1745139c1837SPaolo Bonzini    case INDEX_op_sub2_i32:
1746139c1837SPaolo Bonzini    case INDEX_op_sub2_i64:
1747139c1837SPaolo Bonzini        return &r_r_rZ_rZ_rM_rM;
1748139c1837SPaolo Bonzini
1749139c1837SPaolo Bonzini    case INDEX_op_brcond2_i32:
1750139c1837SPaolo Bonzini        return &rZ_rZ_rZ_rZ;
1751139c1837SPaolo Bonzini
1752139c1837SPaolo Bonzini    case INDEX_op_setcond2_i32:
1753139c1837SPaolo Bonzini        return &r_rZ_rZ_rZ_rZ;
1754139c1837SPaolo Bonzini
1755139c1837SPaolo Bonzini    case INDEX_op_qemu_ld_i32:
1756139c1837SPaolo Bonzini        return TARGET_LONG_BITS <= TCG_TARGET_REG_BITS ? &r_L : &r_L_L;
1757139c1837SPaolo Bonzini    case INDEX_op_qemu_st_i32:
1758139c1837SPaolo Bonzini        return TARGET_LONG_BITS <= TCG_TARGET_REG_BITS ? &LZ_L : &LZ_L_L;
1759139c1837SPaolo Bonzini    case INDEX_op_qemu_ld_i64:
1760139c1837SPaolo Bonzini        return TCG_TARGET_REG_BITS == 64 ? &r_L
1761139c1837SPaolo Bonzini               : TARGET_LONG_BITS <= TCG_TARGET_REG_BITS ? &r_r_L
1762139c1837SPaolo Bonzini               : &r_r_L_L;
1763139c1837SPaolo Bonzini    case INDEX_op_qemu_st_i64:
1764139c1837SPaolo Bonzini        return TCG_TARGET_REG_BITS == 64 ? &LZ_L
1765139c1837SPaolo Bonzini               : TARGET_LONG_BITS <= TCG_TARGET_REG_BITS ? &LZ_LZ_L
1766139c1837SPaolo Bonzini               : &LZ_LZ_L_L;
1767139c1837SPaolo Bonzini
1768139c1837SPaolo Bonzini    default:
1769139c1837SPaolo Bonzini        return NULL;
1770139c1837SPaolo Bonzini    }
1771139c1837SPaolo Bonzini}
1772139c1837SPaolo Bonzini
1773139c1837SPaolo Bonzinistatic const int tcg_target_callee_save_regs[] = {
1774139c1837SPaolo Bonzini    TCG_REG_S0,       /* used for the global env (TCG_AREG0) */
1775139c1837SPaolo Bonzini    TCG_REG_S1,
1776139c1837SPaolo Bonzini    TCG_REG_S2,
1777139c1837SPaolo Bonzini    TCG_REG_S3,
1778139c1837SPaolo Bonzini    TCG_REG_S4,
1779139c1837SPaolo Bonzini    TCG_REG_S5,
1780139c1837SPaolo Bonzini    TCG_REG_S6,
1781139c1837SPaolo Bonzini    TCG_REG_S7,
1782139c1837SPaolo Bonzini    TCG_REG_S8,
1783139c1837SPaolo Bonzini    TCG_REG_S9,
1784139c1837SPaolo Bonzini    TCG_REG_S10,
1785139c1837SPaolo Bonzini    TCG_REG_S11,
1786139c1837SPaolo Bonzini    TCG_REG_RA,       /* should be last for ABI compliance */
1787139c1837SPaolo Bonzini};
1788139c1837SPaolo Bonzini
1789139c1837SPaolo Bonzini/* Stack frame parameters.  */
1790139c1837SPaolo Bonzini#define REG_SIZE   (TCG_TARGET_REG_BITS / 8)
1791139c1837SPaolo Bonzini#define SAVE_SIZE  ((int)ARRAY_SIZE(tcg_target_callee_save_regs) * REG_SIZE)
1792139c1837SPaolo Bonzini#define TEMP_SIZE  (CPU_TEMP_BUF_NLONGS * (int)sizeof(long))
1793139c1837SPaolo Bonzini#define FRAME_SIZE ((TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE + SAVE_SIZE \
1794139c1837SPaolo Bonzini                     + TCG_TARGET_STACK_ALIGN - 1) \
1795139c1837SPaolo Bonzini                    & -TCG_TARGET_STACK_ALIGN)
1796139c1837SPaolo Bonzini#define SAVE_OFS   (TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE)
1797139c1837SPaolo Bonzini
1798139c1837SPaolo Bonzini/* We're expecting to be able to use an immediate for frame allocation.  */
1799139c1837SPaolo BonziniQEMU_BUILD_BUG_ON(FRAME_SIZE > 0x7ff);
1800139c1837SPaolo Bonzini
1801139c1837SPaolo Bonzini/* Generate global QEMU prologue and epilogue code */
1802139c1837SPaolo Bonzinistatic void tcg_target_qemu_prologue(TCGContext *s)
1803139c1837SPaolo Bonzini{
1804139c1837SPaolo Bonzini    int i;
1805139c1837SPaolo Bonzini
1806139c1837SPaolo Bonzini    tcg_set_frame(s, TCG_REG_SP, TCG_STATIC_CALL_ARGS_SIZE, TEMP_SIZE);
1807139c1837SPaolo Bonzini
1808139c1837SPaolo Bonzini    /* TB prologue */
1809139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_ADDI, TCG_REG_SP, TCG_REG_SP, -FRAME_SIZE);
1810139c1837SPaolo Bonzini    for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
1811139c1837SPaolo Bonzini        tcg_out_st(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
1812139c1837SPaolo Bonzini                   TCG_REG_SP, SAVE_OFS + i * REG_SIZE);
1813139c1837SPaolo Bonzini    }
1814139c1837SPaolo Bonzini
1815139c1837SPaolo Bonzini#if !defined(CONFIG_SOFTMMU)
1816139c1837SPaolo Bonzini    tcg_out_movi(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG, guest_base);
1817139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
1818139c1837SPaolo Bonzini#endif
1819139c1837SPaolo Bonzini
1820139c1837SPaolo Bonzini    /* Call generated code */
1821139c1837SPaolo Bonzini    tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
1822139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_JALR, TCG_REG_ZERO, tcg_target_call_iarg_regs[1], 0);
1823139c1837SPaolo Bonzini
1824139c1837SPaolo Bonzini    /* Return path for goto_ptr. Set return value to 0 */
18258b5c2b62SRichard Henderson    tcg_code_gen_epilogue = s->code_ptr;
1826139c1837SPaolo Bonzini    tcg_out_mov(s, TCG_TYPE_REG, TCG_REG_A0, TCG_REG_ZERO);
1827139c1837SPaolo Bonzini
1828139c1837SPaolo Bonzini    /* TB epilogue */
1829139c1837SPaolo Bonzini    tb_ret_addr = s->code_ptr;
1830139c1837SPaolo Bonzini    for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
1831139c1837SPaolo Bonzini        tcg_out_ld(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
1832139c1837SPaolo Bonzini                   TCG_REG_SP, SAVE_OFS + i * REG_SIZE);
1833139c1837SPaolo Bonzini    }
1834139c1837SPaolo Bonzini
1835139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_ADDI, TCG_REG_SP, TCG_REG_SP, FRAME_SIZE);
1836139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_JALR, TCG_REG_ZERO, TCG_REG_RA, 0);
1837139c1837SPaolo Bonzini}
1838139c1837SPaolo Bonzini
1839139c1837SPaolo Bonzinistatic void tcg_target_init(TCGContext *s)
1840139c1837SPaolo Bonzini{
1841139c1837SPaolo Bonzini    tcg_target_available_regs[TCG_TYPE_I32] = 0xffffffff;
1842139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS == 64) {
1843139c1837SPaolo Bonzini        tcg_target_available_regs[TCG_TYPE_I64] = 0xffffffff;
1844139c1837SPaolo Bonzini    }
1845139c1837SPaolo Bonzini
1846139c1837SPaolo Bonzini    tcg_target_call_clobber_regs = -1u;
1847139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S0);
1848139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S1);
1849139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S2);
1850139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S3);
1851139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S4);
1852139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S5);
1853139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S6);
1854139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S7);
1855139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S8);
1856139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S9);
1857139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S10);
1858139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S11);
1859139c1837SPaolo Bonzini
1860139c1837SPaolo Bonzini    s->reserved_regs = 0;
1861139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_REG_ZERO);
1862139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP0);
1863139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP1);
1864139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP2);
1865139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_REG_SP);
1866139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_REG_GP);
1867139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TP);
1868139c1837SPaolo Bonzini}
1869139c1837SPaolo Bonzini
1870139c1837SPaolo Bonzinitypedef struct {
1871139c1837SPaolo Bonzini    DebugFrameHeader h;
1872139c1837SPaolo Bonzini    uint8_t fde_def_cfa[4];
1873139c1837SPaolo Bonzini    uint8_t fde_reg_ofs[ARRAY_SIZE(tcg_target_callee_save_regs) * 2];
1874139c1837SPaolo Bonzini} DebugFrame;
1875139c1837SPaolo Bonzini
1876139c1837SPaolo Bonzini#define ELF_HOST_MACHINE EM_RISCV
1877139c1837SPaolo Bonzini
1878139c1837SPaolo Bonzinistatic const DebugFrame debug_frame = {
1879139c1837SPaolo Bonzini    .h.cie.len = sizeof(DebugFrameCIE) - 4, /* length after .len member */
1880139c1837SPaolo Bonzini    .h.cie.id = -1,
1881139c1837SPaolo Bonzini    .h.cie.version = 1,
1882139c1837SPaolo Bonzini    .h.cie.code_align = 1,
1883139c1837SPaolo Bonzini    .h.cie.data_align = -(TCG_TARGET_REG_BITS / 8) & 0x7f, /* sleb128 */
1884139c1837SPaolo Bonzini    .h.cie.return_column = TCG_REG_RA,
1885139c1837SPaolo Bonzini
1886139c1837SPaolo Bonzini    /* Total FDE size does not include the "len" member.  */
1887139c1837SPaolo Bonzini    .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset),
1888139c1837SPaolo Bonzini
1889139c1837SPaolo Bonzini    .fde_def_cfa = {
1890139c1837SPaolo Bonzini        12, TCG_REG_SP,                 /* DW_CFA_def_cfa sp, ... */
1891139c1837SPaolo Bonzini        (FRAME_SIZE & 0x7f) | 0x80,     /* ... uleb128 FRAME_SIZE */
1892139c1837SPaolo Bonzini        (FRAME_SIZE >> 7)
1893139c1837SPaolo Bonzini    },
1894139c1837SPaolo Bonzini    .fde_reg_ofs = {
1895139c1837SPaolo Bonzini        0x80 + 9,  12,                  /* DW_CFA_offset, s1,  -96 */
1896139c1837SPaolo Bonzini        0x80 + 18, 11,                  /* DW_CFA_offset, s2,  -88 */
1897139c1837SPaolo Bonzini        0x80 + 19, 10,                  /* DW_CFA_offset, s3,  -80 */
1898139c1837SPaolo Bonzini        0x80 + 20, 9,                   /* DW_CFA_offset, s4,  -72 */
1899139c1837SPaolo Bonzini        0x80 + 21, 8,                   /* DW_CFA_offset, s5,  -64 */
1900139c1837SPaolo Bonzini        0x80 + 22, 7,                   /* DW_CFA_offset, s6,  -56 */
1901139c1837SPaolo Bonzini        0x80 + 23, 6,                   /* DW_CFA_offset, s7,  -48 */
1902139c1837SPaolo Bonzini        0x80 + 24, 5,                   /* DW_CFA_offset, s8,  -40 */
1903139c1837SPaolo Bonzini        0x80 + 25, 4,                   /* DW_CFA_offset, s9,  -32 */
1904139c1837SPaolo Bonzini        0x80 + 26, 3,                   /* DW_CFA_offset, s10, -24 */
1905139c1837SPaolo Bonzini        0x80 + 27, 2,                   /* DW_CFA_offset, s11, -16 */
1906139c1837SPaolo Bonzini        0x80 + 1 , 1,                   /* DW_CFA_offset, ra,  -8 */
1907139c1837SPaolo Bonzini    }
1908139c1837SPaolo Bonzini};
1909139c1837SPaolo Bonzini
1910139c1837SPaolo Bonzinivoid tcg_register_jit(void *buf, size_t buf_size)
1911139c1837SPaolo Bonzini{
1912139c1837SPaolo Bonzini    tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame));
1913139c1837SPaolo Bonzini}
1914