xref: /openbmc/qemu/tcg/riscv/tcg-target.c.inc (revision fc313c64345453c7a668d765610dfd7135e21a98)
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
125fc63a4c5SRichard Henderson#define ALL_GENERAL_REGS      MAKE_64BIT_MASK(0, 32)
126fc63a4c5SRichard Henderson/*
127fc63a4c5SRichard Henderson * For softmmu, we need to avoid conflicts with the first 5
128fc63a4c5SRichard Henderson * argument registers to call the helper.  Some of these are
129fc63a4c5SRichard Henderson * also used for the tlb lookup.
130fc63a4c5SRichard Henderson */
131fc63a4c5SRichard Henderson#ifdef CONFIG_SOFTMMU
132fc63a4c5SRichard Henderson#define SOFTMMU_RESERVE_REGS  MAKE_64BIT_MASK(TCG_REG_A0, 5)
133fc63a4c5SRichard Henderson#else
134fc63a4c5SRichard Henderson#define SOFTMMU_RESERVE_REGS  0
135fc63a4c5SRichard Henderson#endif
136fc63a4c5SRichard Henderson
137fc63a4c5SRichard Henderson
138139c1837SPaolo Bonzinistatic inline tcg_target_long sextreg(tcg_target_long val, int pos, int len)
139139c1837SPaolo Bonzini{
140139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS == 32) {
141139c1837SPaolo Bonzini        return sextract32(val, pos, len);
142139c1837SPaolo Bonzini    } else {
143139c1837SPaolo Bonzini        return sextract64(val, pos, len);
144139c1837SPaolo Bonzini    }
145139c1837SPaolo Bonzini}
146139c1837SPaolo Bonzini
147139c1837SPaolo Bonzini/* test if a constant matches the constraint */
148a4fbbd77SRichard Hendersonstatic bool tcg_target_const_match(int64_t val, TCGType type, int ct)
149139c1837SPaolo Bonzini{
150139c1837SPaolo Bonzini    if (ct & TCG_CT_CONST) {
151139c1837SPaolo Bonzini        return 1;
152139c1837SPaolo Bonzini    }
153139c1837SPaolo Bonzini    if ((ct & TCG_CT_CONST_ZERO) && val == 0) {
154139c1837SPaolo Bonzini        return 1;
155139c1837SPaolo Bonzini    }
156139c1837SPaolo Bonzini    if ((ct & TCG_CT_CONST_S12) && val == sextreg(val, 0, 12)) {
157139c1837SPaolo Bonzini        return 1;
158139c1837SPaolo Bonzini    }
159139c1837SPaolo Bonzini    if ((ct & TCG_CT_CONST_N12) && -val == sextreg(-val, 0, 12)) {
160139c1837SPaolo Bonzini        return 1;
161139c1837SPaolo Bonzini    }
162139c1837SPaolo Bonzini    if ((ct & TCG_CT_CONST_M12) && val >= -0xfff && val <= 0xfff) {
163139c1837SPaolo Bonzini        return 1;
164139c1837SPaolo Bonzini    }
165139c1837SPaolo Bonzini    return 0;
166139c1837SPaolo Bonzini}
167139c1837SPaolo Bonzini
168139c1837SPaolo Bonzini/*
169139c1837SPaolo Bonzini * RISC-V Base ISA opcodes (IM)
170139c1837SPaolo Bonzini */
171139c1837SPaolo Bonzini
172139c1837SPaolo Bonzinitypedef enum {
173139c1837SPaolo Bonzini    OPC_ADD = 0x33,
174139c1837SPaolo Bonzini    OPC_ADDI = 0x13,
175139c1837SPaolo Bonzini    OPC_AND = 0x7033,
176139c1837SPaolo Bonzini    OPC_ANDI = 0x7013,
177139c1837SPaolo Bonzini    OPC_AUIPC = 0x17,
178139c1837SPaolo Bonzini    OPC_BEQ = 0x63,
179139c1837SPaolo Bonzini    OPC_BGE = 0x5063,
180139c1837SPaolo Bonzini    OPC_BGEU = 0x7063,
181139c1837SPaolo Bonzini    OPC_BLT = 0x4063,
182139c1837SPaolo Bonzini    OPC_BLTU = 0x6063,
183139c1837SPaolo Bonzini    OPC_BNE = 0x1063,
184139c1837SPaolo Bonzini    OPC_DIV = 0x2004033,
185139c1837SPaolo Bonzini    OPC_DIVU = 0x2005033,
186139c1837SPaolo Bonzini    OPC_JAL = 0x6f,
187139c1837SPaolo Bonzini    OPC_JALR = 0x67,
188139c1837SPaolo Bonzini    OPC_LB = 0x3,
189139c1837SPaolo Bonzini    OPC_LBU = 0x4003,
190139c1837SPaolo Bonzini    OPC_LD = 0x3003,
191139c1837SPaolo Bonzini    OPC_LH = 0x1003,
192139c1837SPaolo Bonzini    OPC_LHU = 0x5003,
193139c1837SPaolo Bonzini    OPC_LUI = 0x37,
194139c1837SPaolo Bonzini    OPC_LW = 0x2003,
195139c1837SPaolo Bonzini    OPC_LWU = 0x6003,
196139c1837SPaolo Bonzini    OPC_MUL = 0x2000033,
197139c1837SPaolo Bonzini    OPC_MULH = 0x2001033,
198139c1837SPaolo Bonzini    OPC_MULHSU = 0x2002033,
199139c1837SPaolo Bonzini    OPC_MULHU = 0x2003033,
200139c1837SPaolo Bonzini    OPC_OR = 0x6033,
201139c1837SPaolo Bonzini    OPC_ORI = 0x6013,
202139c1837SPaolo Bonzini    OPC_REM = 0x2006033,
203139c1837SPaolo Bonzini    OPC_REMU = 0x2007033,
204139c1837SPaolo Bonzini    OPC_SB = 0x23,
205139c1837SPaolo Bonzini    OPC_SD = 0x3023,
206139c1837SPaolo Bonzini    OPC_SH = 0x1023,
207139c1837SPaolo Bonzini    OPC_SLL = 0x1033,
208139c1837SPaolo Bonzini    OPC_SLLI = 0x1013,
209139c1837SPaolo Bonzini    OPC_SLT = 0x2033,
210139c1837SPaolo Bonzini    OPC_SLTI = 0x2013,
211139c1837SPaolo Bonzini    OPC_SLTIU = 0x3013,
212139c1837SPaolo Bonzini    OPC_SLTU = 0x3033,
213139c1837SPaolo Bonzini    OPC_SRA = 0x40005033,
214139c1837SPaolo Bonzini    OPC_SRAI = 0x40005013,
215139c1837SPaolo Bonzini    OPC_SRL = 0x5033,
216139c1837SPaolo Bonzini    OPC_SRLI = 0x5013,
217139c1837SPaolo Bonzini    OPC_SUB = 0x40000033,
218139c1837SPaolo Bonzini    OPC_SW = 0x2023,
219139c1837SPaolo Bonzini    OPC_XOR = 0x4033,
220139c1837SPaolo Bonzini    OPC_XORI = 0x4013,
221139c1837SPaolo Bonzini
222139c1837SPaolo Bonzini#if TCG_TARGET_REG_BITS == 64
223139c1837SPaolo Bonzini    OPC_ADDIW = 0x1b,
224139c1837SPaolo Bonzini    OPC_ADDW = 0x3b,
225139c1837SPaolo Bonzini    OPC_DIVUW = 0x200503b,
226139c1837SPaolo Bonzini    OPC_DIVW = 0x200403b,
227139c1837SPaolo Bonzini    OPC_MULW = 0x200003b,
228139c1837SPaolo Bonzini    OPC_REMUW = 0x200703b,
229139c1837SPaolo Bonzini    OPC_REMW = 0x200603b,
230139c1837SPaolo Bonzini    OPC_SLLIW = 0x101b,
231139c1837SPaolo Bonzini    OPC_SLLW = 0x103b,
232139c1837SPaolo Bonzini    OPC_SRAIW = 0x4000501b,
233139c1837SPaolo Bonzini    OPC_SRAW = 0x4000503b,
234139c1837SPaolo Bonzini    OPC_SRLIW = 0x501b,
235139c1837SPaolo Bonzini    OPC_SRLW = 0x503b,
236139c1837SPaolo Bonzini    OPC_SUBW = 0x4000003b,
237139c1837SPaolo Bonzini#else
238139c1837SPaolo Bonzini    /* Simplify code throughout by defining aliases for RV32.  */
239139c1837SPaolo Bonzini    OPC_ADDIW = OPC_ADDI,
240139c1837SPaolo Bonzini    OPC_ADDW = OPC_ADD,
241139c1837SPaolo Bonzini    OPC_DIVUW = OPC_DIVU,
242139c1837SPaolo Bonzini    OPC_DIVW = OPC_DIV,
243139c1837SPaolo Bonzini    OPC_MULW = OPC_MUL,
244139c1837SPaolo Bonzini    OPC_REMUW = OPC_REMU,
245139c1837SPaolo Bonzini    OPC_REMW = OPC_REM,
246139c1837SPaolo Bonzini    OPC_SLLIW = OPC_SLLI,
247139c1837SPaolo Bonzini    OPC_SLLW = OPC_SLL,
248139c1837SPaolo Bonzini    OPC_SRAIW = OPC_SRAI,
249139c1837SPaolo Bonzini    OPC_SRAW = OPC_SRA,
250139c1837SPaolo Bonzini    OPC_SRLIW = OPC_SRLI,
251139c1837SPaolo Bonzini    OPC_SRLW = OPC_SRL,
252139c1837SPaolo Bonzini    OPC_SUBW = OPC_SUB,
253139c1837SPaolo Bonzini#endif
254139c1837SPaolo Bonzini
255139c1837SPaolo Bonzini    OPC_FENCE = 0x0000000f,
256139c1837SPaolo Bonzini} RISCVInsn;
257139c1837SPaolo Bonzini
258139c1837SPaolo Bonzini/*
259139c1837SPaolo Bonzini * RISC-V immediate and instruction encoders (excludes 16-bit RVC)
260139c1837SPaolo Bonzini */
261139c1837SPaolo Bonzini
262139c1837SPaolo Bonzini/* Type-R */
263139c1837SPaolo Bonzini
264139c1837SPaolo Bonzinistatic int32_t encode_r(RISCVInsn opc, TCGReg rd, TCGReg rs1, TCGReg rs2)
265139c1837SPaolo Bonzini{
266139c1837SPaolo Bonzini    return opc | (rd & 0x1f) << 7 | (rs1 & 0x1f) << 15 | (rs2 & 0x1f) << 20;
267139c1837SPaolo Bonzini}
268139c1837SPaolo Bonzini
269139c1837SPaolo Bonzini/* Type-I */
270139c1837SPaolo Bonzini
271139c1837SPaolo Bonzinistatic int32_t encode_imm12(uint32_t imm)
272139c1837SPaolo Bonzini{
273139c1837SPaolo Bonzini    return (imm & 0xfff) << 20;
274139c1837SPaolo Bonzini}
275139c1837SPaolo Bonzini
276139c1837SPaolo Bonzinistatic int32_t encode_i(RISCVInsn opc, TCGReg rd, TCGReg rs1, uint32_t imm)
277139c1837SPaolo Bonzini{
278139c1837SPaolo Bonzini    return opc | (rd & 0x1f) << 7 | (rs1 & 0x1f) << 15 | encode_imm12(imm);
279139c1837SPaolo Bonzini}
280139c1837SPaolo Bonzini
281139c1837SPaolo Bonzini/* Type-S */
282139c1837SPaolo Bonzini
283139c1837SPaolo Bonzinistatic int32_t encode_simm12(uint32_t imm)
284139c1837SPaolo Bonzini{
285139c1837SPaolo Bonzini    int32_t ret = 0;
286139c1837SPaolo Bonzini
287139c1837SPaolo Bonzini    ret |= (imm & 0xFE0) << 20;
288139c1837SPaolo Bonzini    ret |= (imm & 0x1F) << 7;
289139c1837SPaolo Bonzini
290139c1837SPaolo Bonzini    return ret;
291139c1837SPaolo Bonzini}
292139c1837SPaolo Bonzini
293139c1837SPaolo Bonzinistatic int32_t encode_s(RISCVInsn opc, TCGReg rs1, TCGReg rs2, uint32_t imm)
294139c1837SPaolo Bonzini{
295139c1837SPaolo Bonzini    return opc | (rs1 & 0x1f) << 15 | (rs2 & 0x1f) << 20 | encode_simm12(imm);
296139c1837SPaolo Bonzini}
297139c1837SPaolo Bonzini
298139c1837SPaolo Bonzini/* Type-SB */
299139c1837SPaolo Bonzini
300139c1837SPaolo Bonzinistatic int32_t encode_sbimm12(uint32_t imm)
301139c1837SPaolo Bonzini{
302139c1837SPaolo Bonzini    int32_t ret = 0;
303139c1837SPaolo Bonzini
304139c1837SPaolo Bonzini    ret |= (imm & 0x1000) << 19;
305139c1837SPaolo Bonzini    ret |= (imm & 0x7e0) << 20;
306139c1837SPaolo Bonzini    ret |= (imm & 0x1e) << 7;
307139c1837SPaolo Bonzini    ret |= (imm & 0x800) >> 4;
308139c1837SPaolo Bonzini
309139c1837SPaolo Bonzini    return ret;
310139c1837SPaolo Bonzini}
311139c1837SPaolo Bonzini
312139c1837SPaolo Bonzinistatic int32_t encode_sb(RISCVInsn opc, TCGReg rs1, TCGReg rs2, uint32_t imm)
313139c1837SPaolo Bonzini{
314139c1837SPaolo Bonzini    return opc | (rs1 & 0x1f) << 15 | (rs2 & 0x1f) << 20 | encode_sbimm12(imm);
315139c1837SPaolo Bonzini}
316139c1837SPaolo Bonzini
317139c1837SPaolo Bonzini/* Type-U */
318139c1837SPaolo Bonzini
319139c1837SPaolo Bonzinistatic int32_t encode_uimm20(uint32_t imm)
320139c1837SPaolo Bonzini{
321139c1837SPaolo Bonzini    return imm & 0xfffff000;
322139c1837SPaolo Bonzini}
323139c1837SPaolo Bonzini
324139c1837SPaolo Bonzinistatic int32_t encode_u(RISCVInsn opc, TCGReg rd, uint32_t imm)
325139c1837SPaolo Bonzini{
326139c1837SPaolo Bonzini    return opc | (rd & 0x1f) << 7 | encode_uimm20(imm);
327139c1837SPaolo Bonzini}
328139c1837SPaolo Bonzini
329139c1837SPaolo Bonzini/* Type-UJ */
330139c1837SPaolo Bonzini
331139c1837SPaolo Bonzinistatic int32_t encode_ujimm20(uint32_t imm)
332139c1837SPaolo Bonzini{
333139c1837SPaolo Bonzini    int32_t ret = 0;
334139c1837SPaolo Bonzini
335139c1837SPaolo Bonzini    ret |= (imm & 0x0007fe) << (21 - 1);
336139c1837SPaolo Bonzini    ret |= (imm & 0x000800) << (20 - 11);
337139c1837SPaolo Bonzini    ret |= (imm & 0x0ff000) << (12 - 12);
338139c1837SPaolo Bonzini    ret |= (imm & 0x100000) << (31 - 20);
339139c1837SPaolo Bonzini
340139c1837SPaolo Bonzini    return ret;
341139c1837SPaolo Bonzini}
342139c1837SPaolo Bonzini
343139c1837SPaolo Bonzinistatic int32_t encode_uj(RISCVInsn opc, TCGReg rd, uint32_t imm)
344139c1837SPaolo Bonzini{
345139c1837SPaolo Bonzini    return opc | (rd & 0x1f) << 7 | encode_ujimm20(imm);
346139c1837SPaolo Bonzini}
347139c1837SPaolo Bonzini
348139c1837SPaolo Bonzini/*
349139c1837SPaolo Bonzini * RISC-V instruction emitters
350139c1837SPaolo Bonzini */
351139c1837SPaolo Bonzini
352139c1837SPaolo Bonzinistatic void tcg_out_opc_reg(TCGContext *s, RISCVInsn opc,
353139c1837SPaolo Bonzini                            TCGReg rd, TCGReg rs1, TCGReg rs2)
354139c1837SPaolo Bonzini{
355139c1837SPaolo Bonzini    tcg_out32(s, encode_r(opc, rd, rs1, rs2));
356139c1837SPaolo Bonzini}
357139c1837SPaolo Bonzini
358139c1837SPaolo Bonzinistatic void tcg_out_opc_imm(TCGContext *s, RISCVInsn opc,
359139c1837SPaolo Bonzini                            TCGReg rd, TCGReg rs1, TCGArg imm)
360139c1837SPaolo Bonzini{
361139c1837SPaolo Bonzini    tcg_out32(s, encode_i(opc, rd, rs1, imm));
362139c1837SPaolo Bonzini}
363139c1837SPaolo Bonzini
364139c1837SPaolo Bonzinistatic void tcg_out_opc_store(TCGContext *s, RISCVInsn opc,
365139c1837SPaolo Bonzini                              TCGReg rs1, TCGReg rs2, uint32_t imm)
366139c1837SPaolo Bonzini{
367139c1837SPaolo Bonzini    tcg_out32(s, encode_s(opc, rs1, rs2, imm));
368139c1837SPaolo Bonzini}
369139c1837SPaolo Bonzini
370139c1837SPaolo Bonzinistatic void tcg_out_opc_branch(TCGContext *s, RISCVInsn opc,
371139c1837SPaolo Bonzini                               TCGReg rs1, TCGReg rs2, uint32_t imm)
372139c1837SPaolo Bonzini{
373139c1837SPaolo Bonzini    tcg_out32(s, encode_sb(opc, rs1, rs2, imm));
374139c1837SPaolo Bonzini}
375139c1837SPaolo Bonzini
376139c1837SPaolo Bonzinistatic void tcg_out_opc_upper(TCGContext *s, RISCVInsn opc,
377139c1837SPaolo Bonzini                              TCGReg rd, uint32_t imm)
378139c1837SPaolo Bonzini{
379139c1837SPaolo Bonzini    tcg_out32(s, encode_u(opc, rd, imm));
380139c1837SPaolo Bonzini}
381139c1837SPaolo Bonzini
382139c1837SPaolo Bonzinistatic void tcg_out_opc_jump(TCGContext *s, RISCVInsn opc,
383139c1837SPaolo Bonzini                             TCGReg rd, uint32_t imm)
384139c1837SPaolo Bonzini{
385139c1837SPaolo Bonzini    tcg_out32(s, encode_uj(opc, rd, imm));
386139c1837SPaolo Bonzini}
387139c1837SPaolo Bonzini
388139c1837SPaolo Bonzinistatic void tcg_out_nop_fill(tcg_insn_unit *p, int count)
389139c1837SPaolo Bonzini{
390139c1837SPaolo Bonzini    int i;
391139c1837SPaolo Bonzini    for (i = 0; i < count; ++i) {
392139c1837SPaolo Bonzini        p[i] = encode_i(OPC_ADDI, TCG_REG_ZERO, TCG_REG_ZERO, 0);
393139c1837SPaolo Bonzini    }
394139c1837SPaolo Bonzini}
395139c1837SPaolo Bonzini
396139c1837SPaolo Bonzini/*
397139c1837SPaolo Bonzini * Relocations
398139c1837SPaolo Bonzini */
399139c1837SPaolo Bonzini
400793f7381SRichard Hendersonstatic bool reloc_sbimm12(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
401139c1837SPaolo Bonzini{
402793f7381SRichard Henderson    const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
403793f7381SRichard Henderson    intptr_t offset = (intptr_t)target - (intptr_t)src_rx;
404139c1837SPaolo Bonzini
405844d0442SRichard Henderson    tcg_debug_assert((offset & 1) == 0);
406844d0442SRichard Henderson    if (offset == sextreg(offset, 0, 12)) {
407793f7381SRichard Henderson        *src_rw |= encode_sbimm12(offset);
408139c1837SPaolo Bonzini        return true;
409139c1837SPaolo Bonzini    }
410139c1837SPaolo Bonzini
411139c1837SPaolo Bonzini    return false;
412139c1837SPaolo Bonzini}
413139c1837SPaolo Bonzini
414793f7381SRichard Hendersonstatic bool reloc_jimm20(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
415139c1837SPaolo Bonzini{
416793f7381SRichard Henderson    const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
417793f7381SRichard Henderson    intptr_t offset = (intptr_t)target - (intptr_t)src_rx;
418139c1837SPaolo Bonzini
419844d0442SRichard Henderson    tcg_debug_assert((offset & 1) == 0);
420844d0442SRichard Henderson    if (offset == sextreg(offset, 0, 20)) {
421793f7381SRichard Henderson        *src_rw |= encode_ujimm20(offset);
422139c1837SPaolo Bonzini        return true;
423139c1837SPaolo Bonzini    }
424139c1837SPaolo Bonzini
425139c1837SPaolo Bonzini    return false;
426139c1837SPaolo Bonzini}
427139c1837SPaolo Bonzini
428793f7381SRichard Hendersonstatic bool reloc_call(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
429139c1837SPaolo Bonzini{
430793f7381SRichard Henderson    const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
431793f7381SRichard Henderson    intptr_t offset = (intptr_t)target - (intptr_t)src_rx;
432139c1837SPaolo Bonzini    int32_t lo = sextreg(offset, 0, 12);
433139c1837SPaolo Bonzini    int32_t hi = offset - lo;
434139c1837SPaolo Bonzini
435139c1837SPaolo Bonzini    if (offset == hi + lo) {
436793f7381SRichard Henderson        src_rw[0] |= encode_uimm20(hi);
437793f7381SRichard Henderson        src_rw[1] |= encode_imm12(lo);
438139c1837SPaolo Bonzini        return true;
439139c1837SPaolo Bonzini    }
440139c1837SPaolo Bonzini
441139c1837SPaolo Bonzini    return false;
442139c1837SPaolo Bonzini}
443139c1837SPaolo Bonzini
444139c1837SPaolo Bonzinistatic bool patch_reloc(tcg_insn_unit *code_ptr, int type,
445139c1837SPaolo Bonzini                        intptr_t value, intptr_t addend)
446139c1837SPaolo Bonzini{
447139c1837SPaolo Bonzini    tcg_debug_assert(addend == 0);
448139c1837SPaolo Bonzini    switch (type) {
449139c1837SPaolo Bonzini    case R_RISCV_BRANCH:
450139c1837SPaolo Bonzini        return reloc_sbimm12(code_ptr, (tcg_insn_unit *)value);
451139c1837SPaolo Bonzini    case R_RISCV_JAL:
452139c1837SPaolo Bonzini        return reloc_jimm20(code_ptr, (tcg_insn_unit *)value);
453139c1837SPaolo Bonzini    case R_RISCV_CALL:
454139c1837SPaolo Bonzini        return reloc_call(code_ptr, (tcg_insn_unit *)value);
455139c1837SPaolo Bonzini    default:
4564b6a52d0SRichard Henderson        g_assert_not_reached();
457139c1837SPaolo Bonzini    }
458139c1837SPaolo Bonzini}
459139c1837SPaolo Bonzini
460139c1837SPaolo Bonzini/*
461139c1837SPaolo Bonzini * TCG intrinsics
462139c1837SPaolo Bonzini */
463139c1837SPaolo Bonzini
464139c1837SPaolo Bonzinistatic bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
465139c1837SPaolo Bonzini{
466139c1837SPaolo Bonzini    if (ret == arg) {
467139c1837SPaolo Bonzini        return true;
468139c1837SPaolo Bonzini    }
469139c1837SPaolo Bonzini    switch (type) {
470139c1837SPaolo Bonzini    case TCG_TYPE_I32:
471139c1837SPaolo Bonzini    case TCG_TYPE_I64:
472139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_ADDI, ret, arg, 0);
473139c1837SPaolo Bonzini        break;
474139c1837SPaolo Bonzini    default:
475139c1837SPaolo Bonzini        g_assert_not_reached();
476139c1837SPaolo Bonzini    }
477139c1837SPaolo Bonzini    return true;
478139c1837SPaolo Bonzini}
479139c1837SPaolo Bonzini
480139c1837SPaolo Bonzinistatic void tcg_out_movi(TCGContext *s, TCGType type, TCGReg rd,
481139c1837SPaolo Bonzini                         tcg_target_long val)
482139c1837SPaolo Bonzini{
483139c1837SPaolo Bonzini    tcg_target_long lo, hi, tmp;
484139c1837SPaolo Bonzini    int shift, ret;
485139c1837SPaolo Bonzini
486139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I32) {
487139c1837SPaolo Bonzini        val = (int32_t)val;
488139c1837SPaolo Bonzini    }
489139c1837SPaolo Bonzini
490139c1837SPaolo Bonzini    lo = sextreg(val, 0, 12);
491139c1837SPaolo Bonzini    if (val == lo) {
492139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_ADDI, rd, TCG_REG_ZERO, lo);
493139c1837SPaolo Bonzini        return;
494139c1837SPaolo Bonzini    }
495139c1837SPaolo Bonzini
496139c1837SPaolo Bonzini    hi = val - lo;
497139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS == 32 || val == (int32_t)val) {
498139c1837SPaolo Bonzini        tcg_out_opc_upper(s, OPC_LUI, rd, hi);
499139c1837SPaolo Bonzini        if (lo != 0) {
500139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_ADDIW, rd, rd, lo);
501139c1837SPaolo Bonzini        }
502139c1837SPaolo Bonzini        return;
503139c1837SPaolo Bonzini    }
504139c1837SPaolo Bonzini
505139c1837SPaolo Bonzini    /* We can only be here if TCG_TARGET_REG_BITS != 32 */
506139c1837SPaolo Bonzini    tmp = tcg_pcrel_diff(s, (void *)val);
507139c1837SPaolo Bonzini    if (tmp == (int32_t)tmp) {
508139c1837SPaolo Bonzini        tcg_out_opc_upper(s, OPC_AUIPC, rd, 0);
509139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_ADDI, rd, rd, 0);
510793f7381SRichard Henderson        ret = reloc_call(s->code_ptr - 2, (const tcg_insn_unit *)val);
511139c1837SPaolo Bonzini        tcg_debug_assert(ret == true);
512139c1837SPaolo Bonzini        return;
513139c1837SPaolo Bonzini    }
514139c1837SPaolo Bonzini
515139c1837SPaolo Bonzini    /* Look for a single 20-bit section.  */
516139c1837SPaolo Bonzini    shift = ctz64(val);
517139c1837SPaolo Bonzini    tmp = val >> shift;
518139c1837SPaolo Bonzini    if (tmp == sextreg(tmp, 0, 20)) {
519139c1837SPaolo Bonzini        tcg_out_opc_upper(s, OPC_LUI, rd, tmp << 12);
520139c1837SPaolo Bonzini        if (shift > 12) {
521139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_SLLI, rd, rd, shift - 12);
522139c1837SPaolo Bonzini        } else {
523139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_SRAI, rd, rd, 12 - shift);
524139c1837SPaolo Bonzini        }
525139c1837SPaolo Bonzini        return;
526139c1837SPaolo Bonzini    }
527139c1837SPaolo Bonzini
528139c1837SPaolo Bonzini    /* Look for a few high zero bits, with lots of bits set in the middle.  */
529139c1837SPaolo Bonzini    shift = clz64(val);
530139c1837SPaolo Bonzini    tmp = val << shift;
531139c1837SPaolo Bonzini    if (tmp == sextreg(tmp, 12, 20) << 12) {
532139c1837SPaolo Bonzini        tcg_out_opc_upper(s, OPC_LUI, rd, tmp);
533139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_SRLI, rd, rd, shift);
534139c1837SPaolo Bonzini        return;
535139c1837SPaolo Bonzini    } else if (tmp == sextreg(tmp, 0, 12)) {
536139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_ADDI, rd, TCG_REG_ZERO, tmp);
537139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_SRLI, rd, rd, shift);
538139c1837SPaolo Bonzini        return;
539139c1837SPaolo Bonzini    }
540139c1837SPaolo Bonzini
541139c1837SPaolo Bonzini    /* Drop into the constant pool.  */
542139c1837SPaolo Bonzini    new_pool_label(s, val, R_RISCV_CALL, s->code_ptr, 0);
543139c1837SPaolo Bonzini    tcg_out_opc_upper(s, OPC_AUIPC, rd, 0);
544139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_LD, rd, rd, 0);
545139c1837SPaolo Bonzini}
546139c1837SPaolo Bonzini
547139c1837SPaolo Bonzinistatic void tcg_out_ext8u(TCGContext *s, TCGReg ret, TCGReg arg)
548139c1837SPaolo Bonzini{
549139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_ANDI, ret, arg, 0xff);
550139c1837SPaolo Bonzini}
551139c1837SPaolo Bonzini
552139c1837SPaolo Bonzinistatic void tcg_out_ext16u(TCGContext *s, TCGReg ret, TCGReg arg)
553139c1837SPaolo Bonzini{
554139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SLLIW, ret, arg, 16);
555139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SRLIW, ret, ret, 16);
556139c1837SPaolo Bonzini}
557139c1837SPaolo Bonzini
558139c1837SPaolo Bonzinistatic void tcg_out_ext32u(TCGContext *s, TCGReg ret, TCGReg arg)
559139c1837SPaolo Bonzini{
560139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SLLI, ret, arg, 32);
561139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SRLI, ret, ret, 32);
562139c1837SPaolo Bonzini}
563139c1837SPaolo Bonzini
564139c1837SPaolo Bonzinistatic void tcg_out_ext8s(TCGContext *s, TCGReg ret, TCGReg arg)
565139c1837SPaolo Bonzini{
566139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SLLIW, ret, arg, 24);
567139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SRAIW, ret, ret, 24);
568139c1837SPaolo Bonzini}
569139c1837SPaolo Bonzini
570139c1837SPaolo Bonzinistatic void tcg_out_ext16s(TCGContext *s, TCGReg ret, TCGReg arg)
571139c1837SPaolo Bonzini{
572139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SLLIW, ret, arg, 16);
573139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SRAIW, ret, ret, 16);
574139c1837SPaolo Bonzini}
575139c1837SPaolo Bonzini
576139c1837SPaolo Bonzinistatic void tcg_out_ext32s(TCGContext *s, TCGReg ret, TCGReg arg)
577139c1837SPaolo Bonzini{
578139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_ADDIW, ret, arg, 0);
579139c1837SPaolo Bonzini}
580139c1837SPaolo Bonzini
581139c1837SPaolo Bonzinistatic void tcg_out_ldst(TCGContext *s, RISCVInsn opc, TCGReg data,
582139c1837SPaolo Bonzini                         TCGReg addr, intptr_t offset)
583139c1837SPaolo Bonzini{
584139c1837SPaolo Bonzini    intptr_t imm12 = sextreg(offset, 0, 12);
585139c1837SPaolo Bonzini
586139c1837SPaolo Bonzini    if (offset != imm12) {
587139c1837SPaolo Bonzini        intptr_t diff = offset - (uintptr_t)s->code_ptr;
588139c1837SPaolo Bonzini
589139c1837SPaolo Bonzini        if (addr == TCG_REG_ZERO && diff == (int32_t)diff) {
590139c1837SPaolo Bonzini            imm12 = sextreg(diff, 0, 12);
591139c1837SPaolo Bonzini            tcg_out_opc_upper(s, OPC_AUIPC, TCG_REG_TMP2, diff - imm12);
592139c1837SPaolo Bonzini        } else {
593139c1837SPaolo Bonzini            tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP2, offset - imm12);
594139c1837SPaolo Bonzini            if (addr != TCG_REG_ZERO) {
595139c1837SPaolo Bonzini                tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP2, TCG_REG_TMP2, addr);
596139c1837SPaolo Bonzini            }
597139c1837SPaolo Bonzini        }
598139c1837SPaolo Bonzini        addr = TCG_REG_TMP2;
599139c1837SPaolo Bonzini    }
600139c1837SPaolo Bonzini
601139c1837SPaolo Bonzini    switch (opc) {
602139c1837SPaolo Bonzini    case OPC_SB:
603139c1837SPaolo Bonzini    case OPC_SH:
604139c1837SPaolo Bonzini    case OPC_SW:
605139c1837SPaolo Bonzini    case OPC_SD:
606139c1837SPaolo Bonzini        tcg_out_opc_store(s, opc, addr, data, imm12);
607139c1837SPaolo Bonzini        break;
608139c1837SPaolo Bonzini    case OPC_LB:
609139c1837SPaolo Bonzini    case OPC_LBU:
610139c1837SPaolo Bonzini    case OPC_LH:
611139c1837SPaolo Bonzini    case OPC_LHU:
612139c1837SPaolo Bonzini    case OPC_LW:
613139c1837SPaolo Bonzini    case OPC_LWU:
614139c1837SPaolo Bonzini    case OPC_LD:
615139c1837SPaolo Bonzini        tcg_out_opc_imm(s, opc, data, addr, imm12);
616139c1837SPaolo Bonzini        break;
617139c1837SPaolo Bonzini    default:
618139c1837SPaolo Bonzini        g_assert_not_reached();
619139c1837SPaolo Bonzini    }
620139c1837SPaolo Bonzini}
621139c1837SPaolo Bonzini
622139c1837SPaolo Bonzinistatic void tcg_out_ld(TCGContext *s, TCGType type, TCGReg arg,
623139c1837SPaolo Bonzini                       TCGReg arg1, intptr_t arg2)
624139c1837SPaolo Bonzini{
625139c1837SPaolo Bonzini    bool is32bit = (TCG_TARGET_REG_BITS == 32 || type == TCG_TYPE_I32);
626139c1837SPaolo Bonzini    tcg_out_ldst(s, is32bit ? OPC_LW : OPC_LD, arg, arg1, arg2);
627139c1837SPaolo Bonzini}
628139c1837SPaolo Bonzini
629139c1837SPaolo Bonzinistatic void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
630139c1837SPaolo Bonzini                       TCGReg arg1, intptr_t arg2)
631139c1837SPaolo Bonzini{
632139c1837SPaolo Bonzini    bool is32bit = (TCG_TARGET_REG_BITS == 32 || type == TCG_TYPE_I32);
633139c1837SPaolo Bonzini    tcg_out_ldst(s, is32bit ? OPC_SW : OPC_SD, arg, arg1, arg2);
634139c1837SPaolo Bonzini}
635139c1837SPaolo Bonzini
636139c1837SPaolo Bonzinistatic bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
637139c1837SPaolo Bonzini                        TCGReg base, intptr_t ofs)
638139c1837SPaolo Bonzini{
639139c1837SPaolo Bonzini    if (val == 0) {
640139c1837SPaolo Bonzini        tcg_out_st(s, type, TCG_REG_ZERO, base, ofs);
641139c1837SPaolo Bonzini        return true;
642139c1837SPaolo Bonzini    }
643139c1837SPaolo Bonzini    return false;
644139c1837SPaolo Bonzini}
645139c1837SPaolo Bonzini
646139c1837SPaolo Bonzinistatic void tcg_out_addsub2(TCGContext *s,
647139c1837SPaolo Bonzini                            TCGReg rl, TCGReg rh,
648139c1837SPaolo Bonzini                            TCGReg al, TCGReg ah,
649139c1837SPaolo Bonzini                            TCGArg bl, TCGArg bh,
650139c1837SPaolo Bonzini                            bool cbl, bool cbh, bool is_sub, bool is32bit)
651139c1837SPaolo Bonzini{
652139c1837SPaolo Bonzini    const RISCVInsn opc_add = is32bit ? OPC_ADDW : OPC_ADD;
653139c1837SPaolo Bonzini    const RISCVInsn opc_addi = is32bit ? OPC_ADDIW : OPC_ADDI;
654139c1837SPaolo Bonzini    const RISCVInsn opc_sub = is32bit ? OPC_SUBW : OPC_SUB;
655139c1837SPaolo Bonzini    TCGReg th = TCG_REG_TMP1;
656139c1837SPaolo Bonzini
657139c1837SPaolo Bonzini    /* If we have a negative constant such that negating it would
658139c1837SPaolo Bonzini       make the high part zero, we can (usually) eliminate one insn.  */
659139c1837SPaolo Bonzini    if (cbl && cbh && bh == -1 && bl != 0) {
660139c1837SPaolo Bonzini        bl = -bl;
661139c1837SPaolo Bonzini        bh = 0;
662139c1837SPaolo Bonzini        is_sub = !is_sub;
663139c1837SPaolo Bonzini    }
664139c1837SPaolo Bonzini
665139c1837SPaolo Bonzini    /* By operating on the high part first, we get to use the final
666139c1837SPaolo Bonzini       carry operation to move back from the temporary.  */
667139c1837SPaolo Bonzini    if (!cbh) {
668139c1837SPaolo Bonzini        tcg_out_opc_reg(s, (is_sub ? opc_sub : opc_add), th, ah, bh);
669139c1837SPaolo Bonzini    } else if (bh != 0 || ah == rl) {
670139c1837SPaolo Bonzini        tcg_out_opc_imm(s, opc_addi, th, ah, (is_sub ? -bh : bh));
671139c1837SPaolo Bonzini    } else {
672139c1837SPaolo Bonzini        th = ah;
673139c1837SPaolo Bonzini    }
674139c1837SPaolo Bonzini
675139c1837SPaolo Bonzini    /* Note that tcg optimization should eliminate the bl == 0 case.  */
676139c1837SPaolo Bonzini    if (is_sub) {
677139c1837SPaolo Bonzini        if (cbl) {
678139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_SLTIU, TCG_REG_TMP0, al, bl);
679139c1837SPaolo Bonzini            tcg_out_opc_imm(s, opc_addi, rl, al, -bl);
680139c1837SPaolo Bonzini        } else {
681139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SLTU, TCG_REG_TMP0, al, bl);
682139c1837SPaolo Bonzini            tcg_out_opc_reg(s, opc_sub, rl, al, bl);
683139c1837SPaolo Bonzini        }
684139c1837SPaolo Bonzini        tcg_out_opc_reg(s, opc_sub, rh, th, TCG_REG_TMP0);
685139c1837SPaolo Bonzini    } else {
686139c1837SPaolo Bonzini        if (cbl) {
687139c1837SPaolo Bonzini            tcg_out_opc_imm(s, opc_addi, rl, al, bl);
688139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_SLTIU, TCG_REG_TMP0, rl, bl);
689139c1837SPaolo Bonzini        } else if (rl == al && rl == bl) {
690139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_SLTI, TCG_REG_TMP0, al, 0);
691139c1837SPaolo Bonzini            tcg_out_opc_reg(s, opc_addi, rl, al, bl);
692139c1837SPaolo Bonzini        } else {
693139c1837SPaolo Bonzini            tcg_out_opc_reg(s, opc_add, rl, al, bl);
694139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SLTU, TCG_REG_TMP0,
695139c1837SPaolo Bonzini                            rl, (rl == bl ? al : bl));
696139c1837SPaolo Bonzini        }
697139c1837SPaolo Bonzini        tcg_out_opc_reg(s, opc_add, rh, th, TCG_REG_TMP0);
698139c1837SPaolo Bonzini    }
699139c1837SPaolo Bonzini}
700139c1837SPaolo Bonzini
701139c1837SPaolo Bonzinistatic const struct {
702139c1837SPaolo Bonzini    RISCVInsn op;
703139c1837SPaolo Bonzini    bool swap;
704139c1837SPaolo Bonzini} tcg_brcond_to_riscv[] = {
705139c1837SPaolo Bonzini    [TCG_COND_EQ] =  { OPC_BEQ,  false },
706139c1837SPaolo Bonzini    [TCG_COND_NE] =  { OPC_BNE,  false },
707139c1837SPaolo Bonzini    [TCG_COND_LT] =  { OPC_BLT,  false },
708139c1837SPaolo Bonzini    [TCG_COND_GE] =  { OPC_BGE,  false },
709139c1837SPaolo Bonzini    [TCG_COND_LE] =  { OPC_BGE,  true  },
710139c1837SPaolo Bonzini    [TCG_COND_GT] =  { OPC_BLT,  true  },
711139c1837SPaolo Bonzini    [TCG_COND_LTU] = { OPC_BLTU, false },
712139c1837SPaolo Bonzini    [TCG_COND_GEU] = { OPC_BGEU, false },
713139c1837SPaolo Bonzini    [TCG_COND_LEU] = { OPC_BGEU, true  },
714139c1837SPaolo Bonzini    [TCG_COND_GTU] = { OPC_BLTU, true  }
715139c1837SPaolo Bonzini};
716139c1837SPaolo Bonzini
717139c1837SPaolo Bonzinistatic void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1,
718139c1837SPaolo Bonzini                           TCGReg arg2, TCGLabel *l)
719139c1837SPaolo Bonzini{
720139c1837SPaolo Bonzini    RISCVInsn op = tcg_brcond_to_riscv[cond].op;
721139c1837SPaolo Bonzini
722139c1837SPaolo Bonzini    tcg_debug_assert(op != 0);
723139c1837SPaolo Bonzini
724139c1837SPaolo Bonzini    if (tcg_brcond_to_riscv[cond].swap) {
725139c1837SPaolo Bonzini        TCGReg t = arg1;
726139c1837SPaolo Bonzini        arg1 = arg2;
727139c1837SPaolo Bonzini        arg2 = t;
728139c1837SPaolo Bonzini    }
729139c1837SPaolo Bonzini
730139c1837SPaolo Bonzini    tcg_out_reloc(s, s->code_ptr, R_RISCV_BRANCH, l, 0);
731139c1837SPaolo Bonzini    tcg_out_opc_branch(s, op, arg1, arg2, 0);
732139c1837SPaolo Bonzini}
733139c1837SPaolo Bonzini
734139c1837SPaolo Bonzinistatic void tcg_out_setcond(TCGContext *s, TCGCond cond, TCGReg ret,
735139c1837SPaolo Bonzini                            TCGReg arg1, TCGReg arg2)
736139c1837SPaolo Bonzini{
737139c1837SPaolo Bonzini    switch (cond) {
738139c1837SPaolo Bonzini    case TCG_COND_EQ:
739139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SUB, ret, arg1, arg2);
740139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_SLTIU, ret, ret, 1);
741139c1837SPaolo Bonzini        break;
742139c1837SPaolo Bonzini    case TCG_COND_NE:
743139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SUB, ret, arg1, arg2);
744139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLTU, ret, TCG_REG_ZERO, ret);
745139c1837SPaolo Bonzini        break;
746139c1837SPaolo Bonzini    case TCG_COND_LT:
747139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLT, ret, arg1, arg2);
748139c1837SPaolo Bonzini        break;
749139c1837SPaolo Bonzini    case TCG_COND_GE:
750139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLT, ret, arg1, arg2);
751139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1);
752139c1837SPaolo Bonzini        break;
753139c1837SPaolo Bonzini    case TCG_COND_LE:
754139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLT, ret, arg2, arg1);
755139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1);
756139c1837SPaolo Bonzini        break;
757139c1837SPaolo Bonzini    case TCG_COND_GT:
758139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLT, ret, arg2, arg1);
759139c1837SPaolo Bonzini        break;
760139c1837SPaolo Bonzini    case TCG_COND_LTU:
761139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLTU, ret, arg1, arg2);
762139c1837SPaolo Bonzini        break;
763139c1837SPaolo Bonzini    case TCG_COND_GEU:
764139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLTU, ret, arg1, arg2);
765139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1);
766139c1837SPaolo Bonzini        break;
767139c1837SPaolo Bonzini    case TCG_COND_LEU:
768139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLTU, ret, arg2, arg1);
769139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1);
770139c1837SPaolo Bonzini        break;
771139c1837SPaolo Bonzini    case TCG_COND_GTU:
772139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLTU, ret, arg2, arg1);
773139c1837SPaolo Bonzini        break;
774139c1837SPaolo Bonzini    default:
775139c1837SPaolo Bonzini         g_assert_not_reached();
776139c1837SPaolo Bonzini         break;
777139c1837SPaolo Bonzini     }
778139c1837SPaolo Bonzini}
779139c1837SPaolo Bonzini
780139c1837SPaolo Bonzinistatic void tcg_out_brcond2(TCGContext *s, TCGCond cond, TCGReg al, TCGReg ah,
781139c1837SPaolo Bonzini                            TCGReg bl, TCGReg bh, TCGLabel *l)
782139c1837SPaolo Bonzini{
783139c1837SPaolo Bonzini    /* todo */
784139c1837SPaolo Bonzini    g_assert_not_reached();
785139c1837SPaolo Bonzini}
786139c1837SPaolo Bonzini
787139c1837SPaolo Bonzinistatic void tcg_out_setcond2(TCGContext *s, TCGCond cond, TCGReg ret,
788139c1837SPaolo Bonzini                             TCGReg al, TCGReg ah, TCGReg bl, TCGReg bh)
789139c1837SPaolo Bonzini{
790139c1837SPaolo Bonzini    /* todo */
791139c1837SPaolo Bonzini    g_assert_not_reached();
792139c1837SPaolo Bonzini}
793139c1837SPaolo Bonzini
7942be7d76bSRichard Hendersonstatic void tcg_out_call_int(TCGContext *s, const tcg_insn_unit *arg, bool tail)
795139c1837SPaolo Bonzini{
796139c1837SPaolo Bonzini    TCGReg link = tail ? TCG_REG_ZERO : TCG_REG_RA;
797139c1837SPaolo Bonzini    ptrdiff_t offset = tcg_pcrel_diff(s, arg);
798139c1837SPaolo Bonzini    int ret;
799139c1837SPaolo Bonzini
800844d0442SRichard Henderson    tcg_debug_assert((offset & 1) == 0);
801844d0442SRichard Henderson    if (offset == sextreg(offset, 0, 20)) {
802139c1837SPaolo Bonzini        /* short jump: -2097150 to 2097152 */
803139c1837SPaolo Bonzini        tcg_out_opc_jump(s, OPC_JAL, link, offset);
804844d0442SRichard Henderson    } else if (TCG_TARGET_REG_BITS == 32 || offset == (int32_t)offset) {
805139c1837SPaolo Bonzini        /* long jump: -2147483646 to 2147483648 */
806139c1837SPaolo Bonzini        tcg_out_opc_upper(s, OPC_AUIPC, TCG_REG_TMP0, 0);
807139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_JALR, link, TCG_REG_TMP0, 0);
808844d0442SRichard Henderson        ret = reloc_call(s->code_ptr - 2, arg);
809139c1837SPaolo Bonzini        tcg_debug_assert(ret == true);
810139c1837SPaolo Bonzini    } else if (TCG_TARGET_REG_BITS == 64) {
811139c1837SPaolo Bonzini        /* far jump: 64-bit */
812139c1837SPaolo Bonzini        tcg_target_long imm = sextreg((tcg_target_long)arg, 0, 12);
813139c1837SPaolo Bonzini        tcg_target_long base = (tcg_target_long)arg - imm;
814139c1837SPaolo Bonzini        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP0, base);
815139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_JALR, link, TCG_REG_TMP0, imm);
816139c1837SPaolo Bonzini    } else {
817139c1837SPaolo Bonzini        g_assert_not_reached();
818139c1837SPaolo Bonzini    }
819139c1837SPaolo Bonzini}
820139c1837SPaolo Bonzini
8212be7d76bSRichard Hendersonstatic void tcg_out_call(TCGContext *s, const tcg_insn_unit *arg)
822139c1837SPaolo Bonzini{
823139c1837SPaolo Bonzini    tcg_out_call_int(s, arg, false);
824139c1837SPaolo Bonzini}
825139c1837SPaolo Bonzini
826139c1837SPaolo Bonzinistatic void tcg_out_mb(TCGContext *s, TCGArg a0)
827139c1837SPaolo Bonzini{
828139c1837SPaolo Bonzini    tcg_insn_unit insn = OPC_FENCE;
829139c1837SPaolo Bonzini
830139c1837SPaolo Bonzini    if (a0 & TCG_MO_LD_LD) {
831139c1837SPaolo Bonzini        insn |= 0x02200000;
832139c1837SPaolo Bonzini    }
833139c1837SPaolo Bonzini    if (a0 & TCG_MO_ST_LD) {
834139c1837SPaolo Bonzini        insn |= 0x01200000;
835139c1837SPaolo Bonzini    }
836139c1837SPaolo Bonzini    if (a0 & TCG_MO_LD_ST) {
837139c1837SPaolo Bonzini        insn |= 0x02100000;
838139c1837SPaolo Bonzini    }
839139c1837SPaolo Bonzini    if (a0 & TCG_MO_ST_ST) {
840139c1837SPaolo Bonzini        insn |= 0x02200000;
841139c1837SPaolo Bonzini    }
842139c1837SPaolo Bonzini    tcg_out32(s, insn);
843139c1837SPaolo Bonzini}
844139c1837SPaolo Bonzini
845139c1837SPaolo Bonzini/*
846139c1837SPaolo Bonzini * Load/store and TLB
847139c1837SPaolo Bonzini */
848139c1837SPaolo Bonzini
849139c1837SPaolo Bonzini#if defined(CONFIG_SOFTMMU)
850139c1837SPaolo Bonzini#include "../tcg-ldst.c.inc"
851139c1837SPaolo Bonzini
852139c1837SPaolo Bonzini/* helper signature: helper_ret_ld_mmu(CPUState *env, target_ulong addr,
8539002ffcbSRichard Henderson *                                     MemOpIdx oi, uintptr_t ra)
854139c1837SPaolo Bonzini */
8554b473e0cSRichard Hendersonstatic void * const qemu_ld_helpers[MO_SSIZE + 1] = {
856139c1837SPaolo Bonzini    [MO_UB] = helper_ret_ldub_mmu,
857139c1837SPaolo Bonzini    [MO_SB] = helper_ret_ldsb_mmu,
858c86bd2dcSRichard Henderson#ifdef HOST_WORDS_BIGENDIAN
859c86bd2dcSRichard Henderson    [MO_UW] = helper_be_lduw_mmu,
860c86bd2dcSRichard Henderson    [MO_SW] = helper_be_ldsw_mmu,
861c86bd2dcSRichard Henderson    [MO_UL] = helper_be_ldul_mmu,
862139c1837SPaolo Bonzini#if TCG_TARGET_REG_BITS == 64
863c86bd2dcSRichard Henderson    [MO_SL] = helper_be_ldsl_mmu,
864139c1837SPaolo Bonzini#endif
865*fc313c64SFrédéric Pétrot    [MO_UQ] = helper_be_ldq_mmu,
866c86bd2dcSRichard Henderson#else
867c86bd2dcSRichard Henderson    [MO_UW] = helper_le_lduw_mmu,
868c86bd2dcSRichard Henderson    [MO_SW] = helper_le_ldsw_mmu,
869c86bd2dcSRichard Henderson    [MO_UL] = helper_le_ldul_mmu,
870139c1837SPaolo Bonzini#if TCG_TARGET_REG_BITS == 64
871c86bd2dcSRichard Henderson    [MO_SL] = helper_le_ldsl_mmu,
872139c1837SPaolo Bonzini#endif
873*fc313c64SFrédéric Pétrot    [MO_UQ] = helper_le_ldq_mmu,
874c86bd2dcSRichard Henderson#endif
875139c1837SPaolo Bonzini};
876139c1837SPaolo Bonzini
877139c1837SPaolo Bonzini/* helper signature: helper_ret_st_mmu(CPUState *env, target_ulong addr,
8789002ffcbSRichard Henderson *                                     uintxx_t val, MemOpIdx oi,
879139c1837SPaolo Bonzini *                                     uintptr_t ra)
880139c1837SPaolo Bonzini */
8814b473e0cSRichard Hendersonstatic void * const qemu_st_helpers[MO_SIZE + 1] = {
882c86bd2dcSRichard Henderson    [MO_8]   = helper_ret_stb_mmu,
883c86bd2dcSRichard Henderson#ifdef HOST_WORDS_BIGENDIAN
884c86bd2dcSRichard Henderson    [MO_16] = helper_be_stw_mmu,
885c86bd2dcSRichard Henderson    [MO_32] = helper_be_stl_mmu,
886c86bd2dcSRichard Henderson    [MO_64] = helper_be_stq_mmu,
887c86bd2dcSRichard Henderson#else
888c86bd2dcSRichard Henderson    [MO_16] = helper_le_stw_mmu,
889c86bd2dcSRichard Henderson    [MO_32] = helper_le_stl_mmu,
890c86bd2dcSRichard Henderson    [MO_64] = helper_le_stq_mmu,
891c86bd2dcSRichard Henderson#endif
892139c1837SPaolo Bonzini};
893139c1837SPaolo Bonzini
894139c1837SPaolo Bonzini/* We don't support oversize guests */
895139c1837SPaolo BonziniQEMU_BUILD_BUG_ON(TCG_TARGET_REG_BITS < TARGET_LONG_BITS);
896139c1837SPaolo Bonzini
897139c1837SPaolo Bonzini/* We expect to use a 12-bit negative offset from ENV.  */
898139c1837SPaolo BonziniQEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) > 0);
899139c1837SPaolo BonziniQEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) < -(1 << 11));
900139c1837SPaolo Bonzini
901793f7381SRichard Hendersonstatic void tcg_out_goto(TCGContext *s, const tcg_insn_unit *target)
902844d0442SRichard Henderson{
903844d0442SRichard Henderson    tcg_out_opc_jump(s, OPC_JAL, TCG_REG_ZERO, 0);
904844d0442SRichard Henderson    bool ok = reloc_jimm20(s->code_ptr - 1, target);
905844d0442SRichard Henderson    tcg_debug_assert(ok);
906844d0442SRichard Henderson}
907844d0442SRichard Henderson
908139c1837SPaolo Bonzinistatic void tcg_out_tlb_load(TCGContext *s, TCGReg addrl,
9099002ffcbSRichard Henderson                             TCGReg addrh, MemOpIdx oi,
910139c1837SPaolo Bonzini                             tcg_insn_unit **label_ptr, bool is_load)
911139c1837SPaolo Bonzini{
912139c1837SPaolo Bonzini    MemOp opc = get_memop(oi);
913139c1837SPaolo Bonzini    unsigned s_bits = opc & MO_SIZE;
914139c1837SPaolo Bonzini    unsigned a_bits = get_alignment_bits(opc);
915139c1837SPaolo Bonzini    tcg_target_long compare_mask;
916139c1837SPaolo Bonzini    int mem_index = get_mmuidx(oi);
917139c1837SPaolo Bonzini    int fast_ofs = TLB_MASK_TABLE_OFS(mem_index);
918139c1837SPaolo Bonzini    int mask_ofs = fast_ofs + offsetof(CPUTLBDescFast, mask);
919139c1837SPaolo Bonzini    int table_ofs = fast_ofs + offsetof(CPUTLBDescFast, table);
920139c1837SPaolo Bonzini    TCGReg mask_base = TCG_AREG0, table_base = TCG_AREG0;
921139c1837SPaolo Bonzini
922139c1837SPaolo Bonzini    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP0, mask_base, mask_ofs);
923139c1837SPaolo Bonzini    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP1, table_base, table_ofs);
924139c1837SPaolo Bonzini
925139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SRLI, TCG_REG_TMP2, addrl,
926139c1837SPaolo Bonzini                    TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS);
927139c1837SPaolo Bonzini    tcg_out_opc_reg(s, OPC_AND, TCG_REG_TMP2, TCG_REG_TMP2, TCG_REG_TMP0);
928139c1837SPaolo Bonzini    tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP2, TCG_REG_TMP2, TCG_REG_TMP1);
929139c1837SPaolo Bonzini
930139c1837SPaolo Bonzini    /* Load the tlb comparator and the addend.  */
931139c1837SPaolo Bonzini    tcg_out_ld(s, TCG_TYPE_TL, TCG_REG_TMP0, TCG_REG_TMP2,
932139c1837SPaolo Bonzini               is_load ? offsetof(CPUTLBEntry, addr_read)
933139c1837SPaolo Bonzini               : offsetof(CPUTLBEntry, addr_write));
934139c1837SPaolo Bonzini    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP2, TCG_REG_TMP2,
935139c1837SPaolo Bonzini               offsetof(CPUTLBEntry, addend));
936139c1837SPaolo Bonzini
937139c1837SPaolo Bonzini    /* We don't support unaligned accesses. */
938139c1837SPaolo Bonzini    if (a_bits < s_bits) {
939139c1837SPaolo Bonzini        a_bits = s_bits;
940139c1837SPaolo Bonzini    }
941139c1837SPaolo Bonzini    /* Clear the non-page, non-alignment bits from the address.  */
942139c1837SPaolo Bonzini    compare_mask = (tcg_target_long)TARGET_PAGE_MASK | ((1 << a_bits) - 1);
943139c1837SPaolo Bonzini    if (compare_mask == sextreg(compare_mask, 0, 12)) {
944139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_ANDI, TCG_REG_TMP1, addrl, compare_mask);
945139c1837SPaolo Bonzini    } else {
946139c1837SPaolo Bonzini        tcg_out_movi(s, TCG_TYPE_TL, TCG_REG_TMP1, compare_mask);
947139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_AND, TCG_REG_TMP1, TCG_REG_TMP1, addrl);
948139c1837SPaolo Bonzini    }
949139c1837SPaolo Bonzini
950139c1837SPaolo Bonzini    /* Compare masked address with the TLB entry. */
951139c1837SPaolo Bonzini    label_ptr[0] = s->code_ptr;
952139c1837SPaolo Bonzini    tcg_out_opc_branch(s, OPC_BNE, TCG_REG_TMP0, TCG_REG_TMP1, 0);
953139c1837SPaolo Bonzini
954139c1837SPaolo Bonzini    /* TLB Hit - translate address using addend.  */
955139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS > TARGET_LONG_BITS) {
956139c1837SPaolo Bonzini        tcg_out_ext32u(s, TCG_REG_TMP0, addrl);
957139c1837SPaolo Bonzini        addrl = TCG_REG_TMP0;
958139c1837SPaolo Bonzini    }
959139c1837SPaolo Bonzini    tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP0, TCG_REG_TMP2, addrl);
960139c1837SPaolo Bonzini}
961139c1837SPaolo Bonzini
9629002ffcbSRichard Hendersonstatic void add_qemu_ldst_label(TCGContext *s, int is_ld, MemOpIdx oi,
963139c1837SPaolo Bonzini                                TCGType ext,
964139c1837SPaolo Bonzini                                TCGReg datalo, TCGReg datahi,
965139c1837SPaolo Bonzini                                TCGReg addrlo, TCGReg addrhi,
966139c1837SPaolo Bonzini                                void *raddr, tcg_insn_unit **label_ptr)
967139c1837SPaolo Bonzini{
968139c1837SPaolo Bonzini    TCGLabelQemuLdst *label = new_ldst_label(s);
969139c1837SPaolo Bonzini
970139c1837SPaolo Bonzini    label->is_ld = is_ld;
971139c1837SPaolo Bonzini    label->oi = oi;
972139c1837SPaolo Bonzini    label->type = ext;
973139c1837SPaolo Bonzini    label->datalo_reg = datalo;
974139c1837SPaolo Bonzini    label->datahi_reg = datahi;
975139c1837SPaolo Bonzini    label->addrlo_reg = addrlo;
976139c1837SPaolo Bonzini    label->addrhi_reg = addrhi;
977e5e2e4c7SRichard Henderson    label->raddr = tcg_splitwx_to_rx(raddr);
978139c1837SPaolo Bonzini    label->label_ptr[0] = label_ptr[0];
979139c1837SPaolo Bonzini}
980139c1837SPaolo Bonzini
981139c1837SPaolo Bonzinistatic bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
982139c1837SPaolo Bonzini{
9839002ffcbSRichard Henderson    MemOpIdx oi = l->oi;
984139c1837SPaolo Bonzini    MemOp opc = get_memop(oi);
985139c1837SPaolo Bonzini    TCGReg a0 = tcg_target_call_iarg_regs[0];
986139c1837SPaolo Bonzini    TCGReg a1 = tcg_target_call_iarg_regs[1];
987139c1837SPaolo Bonzini    TCGReg a2 = tcg_target_call_iarg_regs[2];
988139c1837SPaolo Bonzini    TCGReg a3 = tcg_target_call_iarg_regs[3];
989139c1837SPaolo Bonzini
990139c1837SPaolo Bonzini    /* We don't support oversize guests */
991139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS < TARGET_LONG_BITS) {
992139c1837SPaolo Bonzini        g_assert_not_reached();
993139c1837SPaolo Bonzini    }
994139c1837SPaolo Bonzini
995139c1837SPaolo Bonzini    /* resolve label address */
996793f7381SRichard Henderson    if (!reloc_sbimm12(l->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
997139c1837SPaolo Bonzini        return false;
998139c1837SPaolo Bonzini    }
999139c1837SPaolo Bonzini
1000139c1837SPaolo Bonzini    /* call load helper */
1001139c1837SPaolo Bonzini    tcg_out_mov(s, TCG_TYPE_PTR, a0, TCG_AREG0);
1002139c1837SPaolo Bonzini    tcg_out_mov(s, TCG_TYPE_PTR, a1, l->addrlo_reg);
1003139c1837SPaolo Bonzini    tcg_out_movi(s, TCG_TYPE_PTR, a2, oi);
1004139c1837SPaolo Bonzini    tcg_out_movi(s, TCG_TYPE_PTR, a3, (tcg_target_long)l->raddr);
1005139c1837SPaolo Bonzini
1006c86bd2dcSRichard Henderson    tcg_out_call(s, qemu_ld_helpers[opc & MO_SSIZE]);
1007139c1837SPaolo Bonzini    tcg_out_mov(s, (opc & MO_SIZE) == MO_64, l->datalo_reg, a0);
1008139c1837SPaolo Bonzini
1009139c1837SPaolo Bonzini    tcg_out_goto(s, l->raddr);
1010139c1837SPaolo Bonzini    return true;
1011139c1837SPaolo Bonzini}
1012139c1837SPaolo Bonzini
1013139c1837SPaolo Bonzinistatic bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
1014139c1837SPaolo Bonzini{
10159002ffcbSRichard Henderson    MemOpIdx oi = l->oi;
1016139c1837SPaolo Bonzini    MemOp opc = get_memop(oi);
1017139c1837SPaolo Bonzini    MemOp s_bits = opc & MO_SIZE;
1018139c1837SPaolo Bonzini    TCGReg a0 = tcg_target_call_iarg_regs[0];
1019139c1837SPaolo Bonzini    TCGReg a1 = tcg_target_call_iarg_regs[1];
1020139c1837SPaolo Bonzini    TCGReg a2 = tcg_target_call_iarg_regs[2];
1021139c1837SPaolo Bonzini    TCGReg a3 = tcg_target_call_iarg_regs[3];
1022139c1837SPaolo Bonzini    TCGReg a4 = tcg_target_call_iarg_regs[4];
1023139c1837SPaolo Bonzini
1024139c1837SPaolo Bonzini    /* We don't support oversize guests */
1025139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS < TARGET_LONG_BITS) {
1026139c1837SPaolo Bonzini        g_assert_not_reached();
1027139c1837SPaolo Bonzini    }
1028139c1837SPaolo Bonzini
1029139c1837SPaolo Bonzini    /* resolve label address */
1030793f7381SRichard Henderson    if (!reloc_sbimm12(l->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
1031139c1837SPaolo Bonzini        return false;
1032139c1837SPaolo Bonzini    }
1033139c1837SPaolo Bonzini
1034139c1837SPaolo Bonzini    /* call store helper */
1035139c1837SPaolo Bonzini    tcg_out_mov(s, TCG_TYPE_PTR, a0, TCG_AREG0);
1036139c1837SPaolo Bonzini    tcg_out_mov(s, TCG_TYPE_PTR, a1, l->addrlo_reg);
1037139c1837SPaolo Bonzini    tcg_out_mov(s, TCG_TYPE_PTR, a2, l->datalo_reg);
1038139c1837SPaolo Bonzini    switch (s_bits) {
1039139c1837SPaolo Bonzini    case MO_8:
1040139c1837SPaolo Bonzini        tcg_out_ext8u(s, a2, a2);
1041139c1837SPaolo Bonzini        break;
1042139c1837SPaolo Bonzini    case MO_16:
1043139c1837SPaolo Bonzini        tcg_out_ext16u(s, a2, a2);
1044139c1837SPaolo Bonzini        break;
1045139c1837SPaolo Bonzini    default:
1046139c1837SPaolo Bonzini        break;
1047139c1837SPaolo Bonzini    }
1048139c1837SPaolo Bonzini    tcg_out_movi(s, TCG_TYPE_PTR, a3, oi);
1049139c1837SPaolo Bonzini    tcg_out_movi(s, TCG_TYPE_PTR, a4, (tcg_target_long)l->raddr);
1050139c1837SPaolo Bonzini
1051c86bd2dcSRichard Henderson    tcg_out_call(s, qemu_st_helpers[opc & MO_SIZE]);
1052139c1837SPaolo Bonzini
1053139c1837SPaolo Bonzini    tcg_out_goto(s, l->raddr);
1054139c1837SPaolo Bonzini    return true;
1055139c1837SPaolo Bonzini}
1056139c1837SPaolo Bonzini#endif /* CONFIG_SOFTMMU */
1057139c1837SPaolo Bonzini
1058139c1837SPaolo Bonzinistatic void tcg_out_qemu_ld_direct(TCGContext *s, TCGReg lo, TCGReg hi,
1059139c1837SPaolo Bonzini                                   TCGReg base, MemOp opc, bool is_64)
1060139c1837SPaolo Bonzini{
1061c86bd2dcSRichard Henderson    /* Byte swapping is left to middle-end expansion. */
1062c86bd2dcSRichard Henderson    tcg_debug_assert((opc & MO_BSWAP) == 0);
1063139c1837SPaolo Bonzini
1064139c1837SPaolo Bonzini    switch (opc & (MO_SSIZE)) {
1065139c1837SPaolo Bonzini    case MO_UB:
1066139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_LBU, lo, base, 0);
1067139c1837SPaolo Bonzini        break;
1068139c1837SPaolo Bonzini    case MO_SB:
1069139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_LB, lo, base, 0);
1070139c1837SPaolo Bonzini        break;
1071139c1837SPaolo Bonzini    case MO_UW:
1072139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_LHU, lo, base, 0);
1073139c1837SPaolo Bonzini        break;
1074139c1837SPaolo Bonzini    case MO_SW:
1075139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_LH, lo, base, 0);
1076139c1837SPaolo Bonzini        break;
1077139c1837SPaolo Bonzini    case MO_UL:
1078139c1837SPaolo Bonzini        if (TCG_TARGET_REG_BITS == 64 && is_64) {
1079139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_LWU, lo, base, 0);
1080139c1837SPaolo Bonzini            break;
1081139c1837SPaolo Bonzini        }
1082139c1837SPaolo Bonzini        /* FALLTHRU */
1083139c1837SPaolo Bonzini    case MO_SL:
1084139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_LW, lo, base, 0);
1085139c1837SPaolo Bonzini        break;
1086*fc313c64SFrédéric Pétrot    case MO_UQ:
1087139c1837SPaolo Bonzini        /* Prefer to load from offset 0 first, but allow for overlap.  */
1088139c1837SPaolo Bonzini        if (TCG_TARGET_REG_BITS == 64) {
1089139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_LD, lo, base, 0);
1090139c1837SPaolo Bonzini        } else if (lo != base) {
1091139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_LW, lo, base, 0);
1092139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_LW, hi, base, 4);
1093139c1837SPaolo Bonzini        } else {
1094139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_LW, hi, base, 4);
1095139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_LW, lo, base, 0);
1096139c1837SPaolo Bonzini        }
1097139c1837SPaolo Bonzini        break;
1098139c1837SPaolo Bonzini    default:
1099139c1837SPaolo Bonzini        g_assert_not_reached();
1100139c1837SPaolo Bonzini    }
1101139c1837SPaolo Bonzini}
1102139c1837SPaolo Bonzini
1103139c1837SPaolo Bonzinistatic void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args, bool is_64)
1104139c1837SPaolo Bonzini{
1105139c1837SPaolo Bonzini    TCGReg addr_regl, addr_regh __attribute__((unused));
1106139c1837SPaolo Bonzini    TCGReg data_regl, data_regh;
11079002ffcbSRichard Henderson    MemOpIdx oi;
1108139c1837SPaolo Bonzini    MemOp opc;
1109139c1837SPaolo Bonzini#if defined(CONFIG_SOFTMMU)
1110139c1837SPaolo Bonzini    tcg_insn_unit *label_ptr[1];
1111139c1837SPaolo Bonzini#endif
1112139c1837SPaolo Bonzini    TCGReg base = TCG_REG_TMP0;
1113139c1837SPaolo Bonzini
1114139c1837SPaolo Bonzini    data_regl = *args++;
1115139c1837SPaolo Bonzini    data_regh = (TCG_TARGET_REG_BITS == 32 && is_64 ? *args++ : 0);
1116139c1837SPaolo Bonzini    addr_regl = *args++;
1117139c1837SPaolo Bonzini    addr_regh = (TCG_TARGET_REG_BITS < TARGET_LONG_BITS ? *args++ : 0);
1118139c1837SPaolo Bonzini    oi = *args++;
1119139c1837SPaolo Bonzini    opc = get_memop(oi);
1120139c1837SPaolo Bonzini
1121139c1837SPaolo Bonzini#if defined(CONFIG_SOFTMMU)
1122139c1837SPaolo Bonzini    tcg_out_tlb_load(s, addr_regl, addr_regh, oi, label_ptr, 1);
1123139c1837SPaolo Bonzini    tcg_out_qemu_ld_direct(s, data_regl, data_regh, base, opc, is_64);
1124139c1837SPaolo Bonzini    add_qemu_ldst_label(s, 1, oi,
1125139c1837SPaolo Bonzini                        (is_64 ? TCG_TYPE_I64 : TCG_TYPE_I32),
1126139c1837SPaolo Bonzini                        data_regl, data_regh, addr_regl, addr_regh,
1127139c1837SPaolo Bonzini                        s->code_ptr, label_ptr);
1128139c1837SPaolo Bonzini#else
1129139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS > TARGET_LONG_BITS) {
1130139c1837SPaolo Bonzini        tcg_out_ext32u(s, base, addr_regl);
1131139c1837SPaolo Bonzini        addr_regl = base;
1132139c1837SPaolo Bonzini    }
113381c65ee2SRichard Henderson    if (guest_base != 0) {
1134139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_ADD, base, TCG_GUEST_BASE_REG, addr_regl);
1135139c1837SPaolo Bonzini    }
1136139c1837SPaolo Bonzini    tcg_out_qemu_ld_direct(s, data_regl, data_regh, base, opc, is_64);
1137139c1837SPaolo Bonzini#endif
1138139c1837SPaolo Bonzini}
1139139c1837SPaolo Bonzini
1140139c1837SPaolo Bonzinistatic void tcg_out_qemu_st_direct(TCGContext *s, TCGReg lo, TCGReg hi,
1141139c1837SPaolo Bonzini                                   TCGReg base, MemOp opc)
1142139c1837SPaolo Bonzini{
1143c86bd2dcSRichard Henderson    /* Byte swapping is left to middle-end expansion. */
1144c86bd2dcSRichard Henderson    tcg_debug_assert((opc & MO_BSWAP) == 0);
1145139c1837SPaolo Bonzini
1146139c1837SPaolo Bonzini    switch (opc & (MO_SSIZE)) {
1147139c1837SPaolo Bonzini    case MO_8:
1148139c1837SPaolo Bonzini        tcg_out_opc_store(s, OPC_SB, base, lo, 0);
1149139c1837SPaolo Bonzini        break;
1150139c1837SPaolo Bonzini    case MO_16:
1151139c1837SPaolo Bonzini        tcg_out_opc_store(s, OPC_SH, base, lo, 0);
1152139c1837SPaolo Bonzini        break;
1153139c1837SPaolo Bonzini    case MO_32:
1154139c1837SPaolo Bonzini        tcg_out_opc_store(s, OPC_SW, base, lo, 0);
1155139c1837SPaolo Bonzini        break;
1156139c1837SPaolo Bonzini    case MO_64:
1157139c1837SPaolo Bonzini        if (TCG_TARGET_REG_BITS == 64) {
1158139c1837SPaolo Bonzini            tcg_out_opc_store(s, OPC_SD, base, lo, 0);
1159139c1837SPaolo Bonzini        } else {
1160139c1837SPaolo Bonzini            tcg_out_opc_store(s, OPC_SW, base, lo, 0);
1161139c1837SPaolo Bonzini            tcg_out_opc_store(s, OPC_SW, base, hi, 4);
1162139c1837SPaolo Bonzini        }
1163139c1837SPaolo Bonzini        break;
1164139c1837SPaolo Bonzini    default:
1165139c1837SPaolo Bonzini        g_assert_not_reached();
1166139c1837SPaolo Bonzini    }
1167139c1837SPaolo Bonzini}
1168139c1837SPaolo Bonzini
1169139c1837SPaolo Bonzinistatic void tcg_out_qemu_st(TCGContext *s, const TCGArg *args, bool is_64)
1170139c1837SPaolo Bonzini{
1171139c1837SPaolo Bonzini    TCGReg addr_regl, addr_regh __attribute__((unused));
1172139c1837SPaolo Bonzini    TCGReg data_regl, data_regh;
11739002ffcbSRichard Henderson    MemOpIdx oi;
1174139c1837SPaolo Bonzini    MemOp opc;
1175139c1837SPaolo Bonzini#if defined(CONFIG_SOFTMMU)
1176139c1837SPaolo Bonzini    tcg_insn_unit *label_ptr[1];
1177139c1837SPaolo Bonzini#endif
1178139c1837SPaolo Bonzini    TCGReg base = TCG_REG_TMP0;
1179139c1837SPaolo Bonzini
1180139c1837SPaolo Bonzini    data_regl = *args++;
1181139c1837SPaolo Bonzini    data_regh = (TCG_TARGET_REG_BITS == 32 && is_64 ? *args++ : 0);
1182139c1837SPaolo Bonzini    addr_regl = *args++;
1183139c1837SPaolo Bonzini    addr_regh = (TCG_TARGET_REG_BITS < TARGET_LONG_BITS ? *args++ : 0);
1184139c1837SPaolo Bonzini    oi = *args++;
1185139c1837SPaolo Bonzini    opc = get_memop(oi);
1186139c1837SPaolo Bonzini
1187139c1837SPaolo Bonzini#if defined(CONFIG_SOFTMMU)
1188139c1837SPaolo Bonzini    tcg_out_tlb_load(s, addr_regl, addr_regh, oi, label_ptr, 0);
1189139c1837SPaolo Bonzini    tcg_out_qemu_st_direct(s, data_regl, data_regh, base, opc);
1190139c1837SPaolo Bonzini    add_qemu_ldst_label(s, 0, oi,
1191139c1837SPaolo Bonzini                        (is_64 ? TCG_TYPE_I64 : TCG_TYPE_I32),
1192139c1837SPaolo Bonzini                        data_regl, data_regh, addr_regl, addr_regh,
1193139c1837SPaolo Bonzini                        s->code_ptr, label_ptr);
1194139c1837SPaolo Bonzini#else
1195139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS > TARGET_LONG_BITS) {
1196139c1837SPaolo Bonzini        tcg_out_ext32u(s, base, addr_regl);
1197139c1837SPaolo Bonzini        addr_regl = base;
1198139c1837SPaolo Bonzini    }
119981c65ee2SRichard Henderson    if (guest_base != 0) {
1200139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_ADD, base, TCG_GUEST_BASE_REG, addr_regl);
1201139c1837SPaolo Bonzini    }
1202139c1837SPaolo Bonzini    tcg_out_qemu_st_direct(s, data_regl, data_regh, base, opc);
1203139c1837SPaolo Bonzini#endif
1204139c1837SPaolo Bonzini}
1205139c1837SPaolo Bonzini
1206793f7381SRichard Hendersonstatic const tcg_insn_unit *tb_ret_addr;
1207139c1837SPaolo Bonzini
1208139c1837SPaolo Bonzinistatic void tcg_out_op(TCGContext *s, TCGOpcode opc,
12095e8892dbSMiroslav Rezanina                       const TCGArg args[TCG_MAX_OP_ARGS],
12105e8892dbSMiroslav Rezanina                       const int const_args[TCG_MAX_OP_ARGS])
1211139c1837SPaolo Bonzini{
1212139c1837SPaolo Bonzini    TCGArg a0 = args[0];
1213139c1837SPaolo Bonzini    TCGArg a1 = args[1];
1214139c1837SPaolo Bonzini    TCGArg a2 = args[2];
1215139c1837SPaolo Bonzini    int c2 = const_args[2];
1216139c1837SPaolo Bonzini
1217139c1837SPaolo Bonzini    switch (opc) {
1218139c1837SPaolo Bonzini    case INDEX_op_exit_tb:
1219139c1837SPaolo Bonzini        /* Reuse the zeroing that exists for goto_ptr.  */
1220139c1837SPaolo Bonzini        if (a0 == 0) {
12218b5c2b62SRichard Henderson            tcg_out_call_int(s, tcg_code_gen_epilogue, true);
1222139c1837SPaolo Bonzini        } else {
1223139c1837SPaolo Bonzini            tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_A0, a0);
1224139c1837SPaolo Bonzini            tcg_out_call_int(s, tb_ret_addr, true);
1225139c1837SPaolo Bonzini        }
1226139c1837SPaolo Bonzini        break;
1227139c1837SPaolo Bonzini
1228139c1837SPaolo Bonzini    case INDEX_op_goto_tb:
1229139c1837SPaolo Bonzini        assert(s->tb_jmp_insn_offset == 0);
1230139c1837SPaolo Bonzini        /* indirect jump method */
1231139c1837SPaolo Bonzini        tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP0, TCG_REG_ZERO,
1232139c1837SPaolo Bonzini                   (uintptr_t)(s->tb_jmp_target_addr + a0));
1233139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_JALR, TCG_REG_ZERO, TCG_REG_TMP0, 0);
1234139c1837SPaolo Bonzini        set_jmp_reset_offset(s, a0);
1235139c1837SPaolo Bonzini        break;
1236139c1837SPaolo Bonzini
1237139c1837SPaolo Bonzini    case INDEX_op_goto_ptr:
1238139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_JALR, TCG_REG_ZERO, a0, 0);
1239139c1837SPaolo Bonzini        break;
1240139c1837SPaolo Bonzini
1241139c1837SPaolo Bonzini    case INDEX_op_br:
1242139c1837SPaolo Bonzini        tcg_out_reloc(s, s->code_ptr, R_RISCV_JAL, arg_label(a0), 0);
1243139c1837SPaolo Bonzini        tcg_out_opc_jump(s, OPC_JAL, TCG_REG_ZERO, 0);
1244139c1837SPaolo Bonzini        break;
1245139c1837SPaolo Bonzini
1246139c1837SPaolo Bonzini    case INDEX_op_ld8u_i32:
1247139c1837SPaolo Bonzini    case INDEX_op_ld8u_i64:
1248139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_LBU, a0, a1, a2);
1249139c1837SPaolo Bonzini        break;
1250139c1837SPaolo Bonzini    case INDEX_op_ld8s_i32:
1251139c1837SPaolo Bonzini    case INDEX_op_ld8s_i64:
1252139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_LB, a0, a1, a2);
1253139c1837SPaolo Bonzini        break;
1254139c1837SPaolo Bonzini    case INDEX_op_ld16u_i32:
1255139c1837SPaolo Bonzini    case INDEX_op_ld16u_i64:
1256139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_LHU, a0, a1, a2);
1257139c1837SPaolo Bonzini        break;
1258139c1837SPaolo Bonzini    case INDEX_op_ld16s_i32:
1259139c1837SPaolo Bonzini    case INDEX_op_ld16s_i64:
1260139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_LH, a0, a1, a2);
1261139c1837SPaolo Bonzini        break;
1262139c1837SPaolo Bonzini    case INDEX_op_ld32u_i64:
1263139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_LWU, a0, a1, a2);
1264139c1837SPaolo Bonzini        break;
1265139c1837SPaolo Bonzini    case INDEX_op_ld_i32:
1266139c1837SPaolo Bonzini    case INDEX_op_ld32s_i64:
1267139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_LW, a0, a1, a2);
1268139c1837SPaolo Bonzini        break;
1269139c1837SPaolo Bonzini    case INDEX_op_ld_i64:
1270139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_LD, a0, a1, a2);
1271139c1837SPaolo Bonzini        break;
1272139c1837SPaolo Bonzini
1273139c1837SPaolo Bonzini    case INDEX_op_st8_i32:
1274139c1837SPaolo Bonzini    case INDEX_op_st8_i64:
1275139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_SB, a0, a1, a2);
1276139c1837SPaolo Bonzini        break;
1277139c1837SPaolo Bonzini    case INDEX_op_st16_i32:
1278139c1837SPaolo Bonzini    case INDEX_op_st16_i64:
1279139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_SH, a0, a1, a2);
1280139c1837SPaolo Bonzini        break;
1281139c1837SPaolo Bonzini    case INDEX_op_st_i32:
1282139c1837SPaolo Bonzini    case INDEX_op_st32_i64:
1283139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_SW, a0, a1, a2);
1284139c1837SPaolo Bonzini        break;
1285139c1837SPaolo Bonzini    case INDEX_op_st_i64:
1286139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_SD, a0, a1, a2);
1287139c1837SPaolo Bonzini        break;
1288139c1837SPaolo Bonzini
1289139c1837SPaolo Bonzini    case INDEX_op_add_i32:
1290139c1837SPaolo Bonzini        if (c2) {
1291139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_ADDIW, a0, a1, a2);
1292139c1837SPaolo Bonzini        } else {
1293139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_ADDW, a0, a1, a2);
1294139c1837SPaolo Bonzini        }
1295139c1837SPaolo Bonzini        break;
1296139c1837SPaolo Bonzini    case INDEX_op_add_i64:
1297139c1837SPaolo Bonzini        if (c2) {
1298139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_ADDI, a0, a1, a2);
1299139c1837SPaolo Bonzini        } else {
1300139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_ADD, a0, a1, a2);
1301139c1837SPaolo Bonzini        }
1302139c1837SPaolo Bonzini        break;
1303139c1837SPaolo Bonzini
1304139c1837SPaolo Bonzini    case INDEX_op_sub_i32:
1305139c1837SPaolo Bonzini        if (c2) {
1306139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_ADDIW, a0, a1, -a2);
1307139c1837SPaolo Bonzini        } else {
1308139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SUBW, a0, a1, a2);
1309139c1837SPaolo Bonzini        }
1310139c1837SPaolo Bonzini        break;
1311139c1837SPaolo Bonzini    case INDEX_op_sub_i64:
1312139c1837SPaolo Bonzini        if (c2) {
1313139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_ADDI, a0, a1, -a2);
1314139c1837SPaolo Bonzini        } else {
1315139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SUB, a0, a1, a2);
1316139c1837SPaolo Bonzini        }
1317139c1837SPaolo Bonzini        break;
1318139c1837SPaolo Bonzini
1319139c1837SPaolo Bonzini    case INDEX_op_and_i32:
1320139c1837SPaolo Bonzini    case INDEX_op_and_i64:
1321139c1837SPaolo Bonzini        if (c2) {
1322139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_ANDI, a0, a1, a2);
1323139c1837SPaolo Bonzini        } else {
1324139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_AND, a0, a1, a2);
1325139c1837SPaolo Bonzini        }
1326139c1837SPaolo Bonzini        break;
1327139c1837SPaolo Bonzini
1328139c1837SPaolo Bonzini    case INDEX_op_or_i32:
1329139c1837SPaolo Bonzini    case INDEX_op_or_i64:
1330139c1837SPaolo Bonzini        if (c2) {
1331139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_ORI, a0, a1, a2);
1332139c1837SPaolo Bonzini        } else {
1333139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_OR, a0, a1, a2);
1334139c1837SPaolo Bonzini        }
1335139c1837SPaolo Bonzini        break;
1336139c1837SPaolo Bonzini
1337139c1837SPaolo Bonzini    case INDEX_op_xor_i32:
1338139c1837SPaolo Bonzini    case INDEX_op_xor_i64:
1339139c1837SPaolo Bonzini        if (c2) {
1340139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_XORI, a0, a1, a2);
1341139c1837SPaolo Bonzini        } else {
1342139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_XOR, a0, a1, a2);
1343139c1837SPaolo Bonzini        }
1344139c1837SPaolo Bonzini        break;
1345139c1837SPaolo Bonzini
1346139c1837SPaolo Bonzini    case INDEX_op_not_i32:
1347139c1837SPaolo Bonzini    case INDEX_op_not_i64:
1348139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_XORI, a0, a1, -1);
1349139c1837SPaolo Bonzini        break;
1350139c1837SPaolo Bonzini
1351139c1837SPaolo Bonzini    case INDEX_op_neg_i32:
1352139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SUBW, a0, TCG_REG_ZERO, a1);
1353139c1837SPaolo Bonzini        break;
1354139c1837SPaolo Bonzini    case INDEX_op_neg_i64:
1355139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SUB, a0, TCG_REG_ZERO, a1);
1356139c1837SPaolo Bonzini        break;
1357139c1837SPaolo Bonzini
1358139c1837SPaolo Bonzini    case INDEX_op_mul_i32:
1359139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_MULW, a0, a1, a2);
1360139c1837SPaolo Bonzini        break;
1361139c1837SPaolo Bonzini    case INDEX_op_mul_i64:
1362139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_MUL, a0, a1, a2);
1363139c1837SPaolo Bonzini        break;
1364139c1837SPaolo Bonzini
1365139c1837SPaolo Bonzini    case INDEX_op_div_i32:
1366139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_DIVW, a0, a1, a2);
1367139c1837SPaolo Bonzini        break;
1368139c1837SPaolo Bonzini    case INDEX_op_div_i64:
1369139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_DIV, a0, a1, a2);
1370139c1837SPaolo Bonzini        break;
1371139c1837SPaolo Bonzini
1372139c1837SPaolo Bonzini    case INDEX_op_divu_i32:
1373139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_DIVUW, a0, a1, a2);
1374139c1837SPaolo Bonzini        break;
1375139c1837SPaolo Bonzini    case INDEX_op_divu_i64:
1376139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_DIVU, a0, a1, a2);
1377139c1837SPaolo Bonzini        break;
1378139c1837SPaolo Bonzini
1379139c1837SPaolo Bonzini    case INDEX_op_rem_i32:
1380139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_REMW, a0, a1, a2);
1381139c1837SPaolo Bonzini        break;
1382139c1837SPaolo Bonzini    case INDEX_op_rem_i64:
1383139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_REM, a0, a1, a2);
1384139c1837SPaolo Bonzini        break;
1385139c1837SPaolo Bonzini
1386139c1837SPaolo Bonzini    case INDEX_op_remu_i32:
1387139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_REMUW, a0, a1, a2);
1388139c1837SPaolo Bonzini        break;
1389139c1837SPaolo Bonzini    case INDEX_op_remu_i64:
1390139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_REMU, a0, a1, a2);
1391139c1837SPaolo Bonzini        break;
1392139c1837SPaolo Bonzini
1393139c1837SPaolo Bonzini    case INDEX_op_shl_i32:
1394139c1837SPaolo Bonzini        if (c2) {
1395d2f3066eSZihao Yu            tcg_out_opc_imm(s, OPC_SLLIW, a0, a1, a2 & 0x1f);
1396139c1837SPaolo Bonzini        } else {
1397139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SLLW, a0, a1, a2);
1398139c1837SPaolo Bonzini        }
1399139c1837SPaolo Bonzini        break;
1400139c1837SPaolo Bonzini    case INDEX_op_shl_i64:
1401139c1837SPaolo Bonzini        if (c2) {
1402d2f3066eSZihao Yu            tcg_out_opc_imm(s, OPC_SLLI, a0, a1, a2 & 0x3f);
1403139c1837SPaolo Bonzini        } else {
1404139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SLL, a0, a1, a2);
1405139c1837SPaolo Bonzini        }
1406139c1837SPaolo Bonzini        break;
1407139c1837SPaolo Bonzini
1408139c1837SPaolo Bonzini    case INDEX_op_shr_i32:
1409139c1837SPaolo Bonzini        if (c2) {
1410d2f3066eSZihao Yu            tcg_out_opc_imm(s, OPC_SRLIW, a0, a1, a2 & 0x1f);
1411139c1837SPaolo Bonzini        } else {
1412139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SRLW, a0, a1, a2);
1413139c1837SPaolo Bonzini        }
1414139c1837SPaolo Bonzini        break;
1415139c1837SPaolo Bonzini    case INDEX_op_shr_i64:
1416139c1837SPaolo Bonzini        if (c2) {
1417d2f3066eSZihao Yu            tcg_out_opc_imm(s, OPC_SRLI, a0, a1, a2 & 0x3f);
1418139c1837SPaolo Bonzini        } else {
1419139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SRL, a0, a1, a2);
1420139c1837SPaolo Bonzini        }
1421139c1837SPaolo Bonzini        break;
1422139c1837SPaolo Bonzini
1423139c1837SPaolo Bonzini    case INDEX_op_sar_i32:
1424139c1837SPaolo Bonzini        if (c2) {
1425d2f3066eSZihao Yu            tcg_out_opc_imm(s, OPC_SRAIW, a0, a1, a2 & 0x1f);
1426139c1837SPaolo Bonzini        } else {
1427139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SRAW, a0, a1, a2);
1428139c1837SPaolo Bonzini        }
1429139c1837SPaolo Bonzini        break;
1430139c1837SPaolo Bonzini    case INDEX_op_sar_i64:
1431139c1837SPaolo Bonzini        if (c2) {
1432d2f3066eSZihao Yu            tcg_out_opc_imm(s, OPC_SRAI, a0, a1, a2 & 0x3f);
1433139c1837SPaolo Bonzini        } else {
1434139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SRA, a0, a1, a2);
1435139c1837SPaolo Bonzini        }
1436139c1837SPaolo Bonzini        break;
1437139c1837SPaolo Bonzini
1438139c1837SPaolo Bonzini    case INDEX_op_add2_i32:
1439139c1837SPaolo Bonzini        tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5],
1440139c1837SPaolo Bonzini                        const_args[4], const_args[5], false, true);
1441139c1837SPaolo Bonzini        break;
1442139c1837SPaolo Bonzini    case INDEX_op_add2_i64:
1443139c1837SPaolo Bonzini        tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5],
1444139c1837SPaolo Bonzini                        const_args[4], const_args[5], false, false);
1445139c1837SPaolo Bonzini        break;
1446139c1837SPaolo Bonzini    case INDEX_op_sub2_i32:
1447139c1837SPaolo Bonzini        tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5],
1448139c1837SPaolo Bonzini                        const_args[4], const_args[5], true, true);
1449139c1837SPaolo Bonzini        break;
1450139c1837SPaolo Bonzini    case INDEX_op_sub2_i64:
1451139c1837SPaolo Bonzini        tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5],
1452139c1837SPaolo Bonzini                        const_args[4], const_args[5], true, false);
1453139c1837SPaolo Bonzini        break;
1454139c1837SPaolo Bonzini
1455139c1837SPaolo Bonzini    case INDEX_op_brcond_i32:
1456139c1837SPaolo Bonzini    case INDEX_op_brcond_i64:
1457139c1837SPaolo Bonzini        tcg_out_brcond(s, a2, a0, a1, arg_label(args[3]));
1458139c1837SPaolo Bonzini        break;
1459139c1837SPaolo Bonzini    case INDEX_op_brcond2_i32:
1460139c1837SPaolo Bonzini        tcg_out_brcond2(s, args[4], a0, a1, a2, args[3], arg_label(args[5]));
1461139c1837SPaolo Bonzini        break;
1462139c1837SPaolo Bonzini
1463139c1837SPaolo Bonzini    case INDEX_op_setcond_i32:
1464139c1837SPaolo Bonzini    case INDEX_op_setcond_i64:
1465139c1837SPaolo Bonzini        tcg_out_setcond(s, args[3], a0, a1, a2);
1466139c1837SPaolo Bonzini        break;
1467139c1837SPaolo Bonzini    case INDEX_op_setcond2_i32:
1468139c1837SPaolo Bonzini        tcg_out_setcond2(s, args[5], a0, a1, a2, args[3], args[4]);
1469139c1837SPaolo Bonzini        break;
1470139c1837SPaolo Bonzini
1471139c1837SPaolo Bonzini    case INDEX_op_qemu_ld_i32:
1472139c1837SPaolo Bonzini        tcg_out_qemu_ld(s, args, false);
1473139c1837SPaolo Bonzini        break;
1474139c1837SPaolo Bonzini    case INDEX_op_qemu_ld_i64:
1475139c1837SPaolo Bonzini        tcg_out_qemu_ld(s, args, true);
1476139c1837SPaolo Bonzini        break;
1477139c1837SPaolo Bonzini    case INDEX_op_qemu_st_i32:
1478139c1837SPaolo Bonzini        tcg_out_qemu_st(s, args, false);
1479139c1837SPaolo Bonzini        break;
1480139c1837SPaolo Bonzini    case INDEX_op_qemu_st_i64:
1481139c1837SPaolo Bonzini        tcg_out_qemu_st(s, args, true);
1482139c1837SPaolo Bonzini        break;
1483139c1837SPaolo Bonzini
1484139c1837SPaolo Bonzini    case INDEX_op_ext8u_i32:
1485139c1837SPaolo Bonzini    case INDEX_op_ext8u_i64:
1486139c1837SPaolo Bonzini        tcg_out_ext8u(s, a0, a1);
1487139c1837SPaolo Bonzini        break;
1488139c1837SPaolo Bonzini
1489139c1837SPaolo Bonzini    case INDEX_op_ext16u_i32:
1490139c1837SPaolo Bonzini    case INDEX_op_ext16u_i64:
1491139c1837SPaolo Bonzini        tcg_out_ext16u(s, a0, a1);
1492139c1837SPaolo Bonzini        break;
1493139c1837SPaolo Bonzini
1494139c1837SPaolo Bonzini    case INDEX_op_ext32u_i64:
1495139c1837SPaolo Bonzini    case INDEX_op_extu_i32_i64:
1496139c1837SPaolo Bonzini        tcg_out_ext32u(s, a0, a1);
1497139c1837SPaolo Bonzini        break;
1498139c1837SPaolo Bonzini
1499139c1837SPaolo Bonzini    case INDEX_op_ext8s_i32:
1500139c1837SPaolo Bonzini    case INDEX_op_ext8s_i64:
1501139c1837SPaolo Bonzini        tcg_out_ext8s(s, a0, a1);
1502139c1837SPaolo Bonzini        break;
1503139c1837SPaolo Bonzini
1504139c1837SPaolo Bonzini    case INDEX_op_ext16s_i32:
1505139c1837SPaolo Bonzini    case INDEX_op_ext16s_i64:
1506139c1837SPaolo Bonzini        tcg_out_ext16s(s, a0, a1);
1507139c1837SPaolo Bonzini        break;
1508139c1837SPaolo Bonzini
1509139c1837SPaolo Bonzini    case INDEX_op_ext32s_i64:
1510139c1837SPaolo Bonzini    case INDEX_op_extrl_i64_i32:
1511139c1837SPaolo Bonzini    case INDEX_op_ext_i32_i64:
1512139c1837SPaolo Bonzini        tcg_out_ext32s(s, a0, a1);
1513139c1837SPaolo Bonzini        break;
1514139c1837SPaolo Bonzini
1515139c1837SPaolo Bonzini    case INDEX_op_extrh_i64_i32:
1516139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_SRAI, a0, a1, 32);
1517139c1837SPaolo Bonzini        break;
1518139c1837SPaolo Bonzini
1519139c1837SPaolo Bonzini    case INDEX_op_mulsh_i32:
1520139c1837SPaolo Bonzini    case INDEX_op_mulsh_i64:
1521139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_MULH, a0, a1, a2);
1522139c1837SPaolo Bonzini        break;
1523139c1837SPaolo Bonzini
1524139c1837SPaolo Bonzini    case INDEX_op_muluh_i32:
1525139c1837SPaolo Bonzini    case INDEX_op_muluh_i64:
1526139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_MULHU, a0, a1, a2);
1527139c1837SPaolo Bonzini        break;
1528139c1837SPaolo Bonzini
1529139c1837SPaolo Bonzini    case INDEX_op_mb:
1530139c1837SPaolo Bonzini        tcg_out_mb(s, a0);
1531139c1837SPaolo Bonzini        break;
1532139c1837SPaolo Bonzini
1533139c1837SPaolo Bonzini    case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
1534139c1837SPaolo Bonzini    case INDEX_op_mov_i64:
1535139c1837SPaolo Bonzini    case INDEX_op_call:     /* Always emitted via tcg_out_call.  */
1536139c1837SPaolo Bonzini    default:
1537139c1837SPaolo Bonzini        g_assert_not_reached();
1538139c1837SPaolo Bonzini    }
1539139c1837SPaolo Bonzini}
1540139c1837SPaolo Bonzini
1541665be288SRichard Hendersonstatic TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
1542139c1837SPaolo Bonzini{
1543139c1837SPaolo Bonzini    switch (op) {
1544139c1837SPaolo Bonzini    case INDEX_op_goto_ptr:
1545665be288SRichard Henderson        return C_O0_I1(r);
1546139c1837SPaolo Bonzini
1547139c1837SPaolo Bonzini    case INDEX_op_ld8u_i32:
1548139c1837SPaolo Bonzini    case INDEX_op_ld8s_i32:
1549139c1837SPaolo Bonzini    case INDEX_op_ld16u_i32:
1550139c1837SPaolo Bonzini    case INDEX_op_ld16s_i32:
1551139c1837SPaolo Bonzini    case INDEX_op_ld_i32:
1552139c1837SPaolo Bonzini    case INDEX_op_not_i32:
1553139c1837SPaolo Bonzini    case INDEX_op_neg_i32:
1554139c1837SPaolo Bonzini    case INDEX_op_ld8u_i64:
1555139c1837SPaolo Bonzini    case INDEX_op_ld8s_i64:
1556139c1837SPaolo Bonzini    case INDEX_op_ld16u_i64:
1557139c1837SPaolo Bonzini    case INDEX_op_ld16s_i64:
1558139c1837SPaolo Bonzini    case INDEX_op_ld32s_i64:
1559139c1837SPaolo Bonzini    case INDEX_op_ld32u_i64:
1560139c1837SPaolo Bonzini    case INDEX_op_ld_i64:
1561139c1837SPaolo Bonzini    case INDEX_op_not_i64:
1562139c1837SPaolo Bonzini    case INDEX_op_neg_i64:
1563139c1837SPaolo Bonzini    case INDEX_op_ext8u_i32:
1564139c1837SPaolo Bonzini    case INDEX_op_ext8u_i64:
1565139c1837SPaolo Bonzini    case INDEX_op_ext16u_i32:
1566139c1837SPaolo Bonzini    case INDEX_op_ext16u_i64:
1567139c1837SPaolo Bonzini    case INDEX_op_ext32u_i64:
1568139c1837SPaolo Bonzini    case INDEX_op_extu_i32_i64:
1569139c1837SPaolo Bonzini    case INDEX_op_ext8s_i32:
1570139c1837SPaolo Bonzini    case INDEX_op_ext8s_i64:
1571139c1837SPaolo Bonzini    case INDEX_op_ext16s_i32:
1572139c1837SPaolo Bonzini    case INDEX_op_ext16s_i64:
1573139c1837SPaolo Bonzini    case INDEX_op_ext32s_i64:
1574139c1837SPaolo Bonzini    case INDEX_op_extrl_i64_i32:
1575139c1837SPaolo Bonzini    case INDEX_op_extrh_i64_i32:
1576139c1837SPaolo Bonzini    case INDEX_op_ext_i32_i64:
1577665be288SRichard Henderson        return C_O1_I1(r, r);
1578139c1837SPaolo Bonzini
1579139c1837SPaolo Bonzini    case INDEX_op_st8_i32:
1580139c1837SPaolo Bonzini    case INDEX_op_st16_i32:
1581139c1837SPaolo Bonzini    case INDEX_op_st_i32:
1582139c1837SPaolo Bonzini    case INDEX_op_st8_i64:
1583139c1837SPaolo Bonzini    case INDEX_op_st16_i64:
1584139c1837SPaolo Bonzini    case INDEX_op_st32_i64:
1585139c1837SPaolo Bonzini    case INDEX_op_st_i64:
1586665be288SRichard Henderson        return C_O0_I2(rZ, r);
1587139c1837SPaolo Bonzini
1588139c1837SPaolo Bonzini    case INDEX_op_add_i32:
1589139c1837SPaolo Bonzini    case INDEX_op_and_i32:
1590139c1837SPaolo Bonzini    case INDEX_op_or_i32:
1591139c1837SPaolo Bonzini    case INDEX_op_xor_i32:
1592139c1837SPaolo Bonzini    case INDEX_op_add_i64:
1593139c1837SPaolo Bonzini    case INDEX_op_and_i64:
1594139c1837SPaolo Bonzini    case INDEX_op_or_i64:
1595139c1837SPaolo Bonzini    case INDEX_op_xor_i64:
1596665be288SRichard Henderson        return C_O1_I2(r, r, rI);
1597139c1837SPaolo Bonzini
1598139c1837SPaolo Bonzini    case INDEX_op_sub_i32:
1599139c1837SPaolo Bonzini    case INDEX_op_sub_i64:
1600665be288SRichard Henderson        return C_O1_I2(r, rZ, rN);
1601139c1837SPaolo Bonzini
1602139c1837SPaolo Bonzini    case INDEX_op_mul_i32:
1603139c1837SPaolo Bonzini    case INDEX_op_mulsh_i32:
1604139c1837SPaolo Bonzini    case INDEX_op_muluh_i32:
1605139c1837SPaolo Bonzini    case INDEX_op_div_i32:
1606139c1837SPaolo Bonzini    case INDEX_op_divu_i32:
1607139c1837SPaolo Bonzini    case INDEX_op_rem_i32:
1608139c1837SPaolo Bonzini    case INDEX_op_remu_i32:
1609139c1837SPaolo Bonzini    case INDEX_op_setcond_i32:
1610139c1837SPaolo Bonzini    case INDEX_op_mul_i64:
1611139c1837SPaolo Bonzini    case INDEX_op_mulsh_i64:
1612139c1837SPaolo Bonzini    case INDEX_op_muluh_i64:
1613139c1837SPaolo Bonzini    case INDEX_op_div_i64:
1614139c1837SPaolo Bonzini    case INDEX_op_divu_i64:
1615139c1837SPaolo Bonzini    case INDEX_op_rem_i64:
1616139c1837SPaolo Bonzini    case INDEX_op_remu_i64:
1617139c1837SPaolo Bonzini    case INDEX_op_setcond_i64:
1618665be288SRichard Henderson        return C_O1_I2(r, rZ, rZ);
1619139c1837SPaolo Bonzini
1620139c1837SPaolo Bonzini    case INDEX_op_shl_i32:
1621139c1837SPaolo Bonzini    case INDEX_op_shr_i32:
1622139c1837SPaolo Bonzini    case INDEX_op_sar_i32:
1623139c1837SPaolo Bonzini    case INDEX_op_shl_i64:
1624139c1837SPaolo Bonzini    case INDEX_op_shr_i64:
1625139c1837SPaolo Bonzini    case INDEX_op_sar_i64:
1626665be288SRichard Henderson        return C_O1_I2(r, r, ri);
1627139c1837SPaolo Bonzini
1628139c1837SPaolo Bonzini    case INDEX_op_brcond_i32:
1629139c1837SPaolo Bonzini    case INDEX_op_brcond_i64:
1630665be288SRichard Henderson        return C_O0_I2(rZ, rZ);
1631139c1837SPaolo Bonzini
1632139c1837SPaolo Bonzini    case INDEX_op_add2_i32:
1633139c1837SPaolo Bonzini    case INDEX_op_add2_i64:
1634139c1837SPaolo Bonzini    case INDEX_op_sub2_i32:
1635139c1837SPaolo Bonzini    case INDEX_op_sub2_i64:
1636665be288SRichard Henderson        return C_O2_I4(r, r, rZ, rZ, rM, rM);
1637139c1837SPaolo Bonzini
1638139c1837SPaolo Bonzini    case INDEX_op_brcond2_i32:
1639665be288SRichard Henderson        return C_O0_I4(rZ, rZ, rZ, rZ);
1640139c1837SPaolo Bonzini
1641139c1837SPaolo Bonzini    case INDEX_op_setcond2_i32:
1642665be288SRichard Henderson        return C_O1_I4(r, rZ, rZ, rZ, rZ);
1643139c1837SPaolo Bonzini
1644139c1837SPaolo Bonzini    case INDEX_op_qemu_ld_i32:
1645665be288SRichard Henderson        return (TARGET_LONG_BITS <= TCG_TARGET_REG_BITS
1646665be288SRichard Henderson                ? C_O1_I1(r, L) : C_O1_I2(r, L, L));
1647139c1837SPaolo Bonzini    case INDEX_op_qemu_st_i32:
1648665be288SRichard Henderson        return (TARGET_LONG_BITS <= TCG_TARGET_REG_BITS
1649665be288SRichard Henderson                ? C_O0_I2(LZ, L) : C_O0_I3(LZ, L, L));
1650139c1837SPaolo Bonzini    case INDEX_op_qemu_ld_i64:
1651665be288SRichard Henderson        return (TCG_TARGET_REG_BITS == 64 ? C_O1_I1(r, L)
1652665be288SRichard Henderson               : TARGET_LONG_BITS <= TCG_TARGET_REG_BITS ? C_O2_I1(r, r, L)
1653665be288SRichard Henderson               : C_O2_I2(r, r, L, L));
1654139c1837SPaolo Bonzini    case INDEX_op_qemu_st_i64:
1655665be288SRichard Henderson        return (TCG_TARGET_REG_BITS == 64 ? C_O0_I2(LZ, L)
1656665be288SRichard Henderson               : TARGET_LONG_BITS <= TCG_TARGET_REG_BITS ? C_O0_I3(LZ, LZ, L)
1657665be288SRichard Henderson               : C_O0_I4(LZ, LZ, L, L));
1658139c1837SPaolo Bonzini
1659139c1837SPaolo Bonzini    default:
1660665be288SRichard Henderson        g_assert_not_reached();
1661139c1837SPaolo Bonzini    }
1662139c1837SPaolo Bonzini}
1663139c1837SPaolo Bonzini
1664139c1837SPaolo Bonzinistatic const int tcg_target_callee_save_regs[] = {
1665139c1837SPaolo Bonzini    TCG_REG_S0,       /* used for the global env (TCG_AREG0) */
1666139c1837SPaolo Bonzini    TCG_REG_S1,
1667139c1837SPaolo Bonzini    TCG_REG_S2,
1668139c1837SPaolo Bonzini    TCG_REG_S3,
1669139c1837SPaolo Bonzini    TCG_REG_S4,
1670139c1837SPaolo Bonzini    TCG_REG_S5,
1671139c1837SPaolo Bonzini    TCG_REG_S6,
1672139c1837SPaolo Bonzini    TCG_REG_S7,
1673139c1837SPaolo Bonzini    TCG_REG_S8,
1674139c1837SPaolo Bonzini    TCG_REG_S9,
1675139c1837SPaolo Bonzini    TCG_REG_S10,
1676139c1837SPaolo Bonzini    TCG_REG_S11,
1677139c1837SPaolo Bonzini    TCG_REG_RA,       /* should be last for ABI compliance */
1678139c1837SPaolo Bonzini};
1679139c1837SPaolo Bonzini
1680139c1837SPaolo Bonzini/* Stack frame parameters.  */
1681139c1837SPaolo Bonzini#define REG_SIZE   (TCG_TARGET_REG_BITS / 8)
1682139c1837SPaolo Bonzini#define SAVE_SIZE  ((int)ARRAY_SIZE(tcg_target_callee_save_regs) * REG_SIZE)
1683139c1837SPaolo Bonzini#define TEMP_SIZE  (CPU_TEMP_BUF_NLONGS * (int)sizeof(long))
1684139c1837SPaolo Bonzini#define FRAME_SIZE ((TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE + SAVE_SIZE \
1685139c1837SPaolo Bonzini                     + TCG_TARGET_STACK_ALIGN - 1) \
1686139c1837SPaolo Bonzini                    & -TCG_TARGET_STACK_ALIGN)
1687139c1837SPaolo Bonzini#define SAVE_OFS   (TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE)
1688139c1837SPaolo Bonzini
1689139c1837SPaolo Bonzini/* We're expecting to be able to use an immediate for frame allocation.  */
1690139c1837SPaolo BonziniQEMU_BUILD_BUG_ON(FRAME_SIZE > 0x7ff);
1691139c1837SPaolo Bonzini
1692139c1837SPaolo Bonzini/* Generate global QEMU prologue and epilogue code */
1693139c1837SPaolo Bonzinistatic void tcg_target_qemu_prologue(TCGContext *s)
1694139c1837SPaolo Bonzini{
1695139c1837SPaolo Bonzini    int i;
1696139c1837SPaolo Bonzini
1697139c1837SPaolo Bonzini    tcg_set_frame(s, TCG_REG_SP, TCG_STATIC_CALL_ARGS_SIZE, TEMP_SIZE);
1698139c1837SPaolo Bonzini
1699139c1837SPaolo Bonzini    /* TB prologue */
1700139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_ADDI, TCG_REG_SP, TCG_REG_SP, -FRAME_SIZE);
1701139c1837SPaolo Bonzini    for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
1702139c1837SPaolo Bonzini        tcg_out_st(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
1703139c1837SPaolo Bonzini                   TCG_REG_SP, SAVE_OFS + i * REG_SIZE);
1704139c1837SPaolo Bonzini    }
1705139c1837SPaolo Bonzini
1706139c1837SPaolo Bonzini#if !defined(CONFIG_SOFTMMU)
1707139c1837SPaolo Bonzini    tcg_out_movi(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG, guest_base);
1708139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
1709139c1837SPaolo Bonzini#endif
1710139c1837SPaolo Bonzini
1711139c1837SPaolo Bonzini    /* Call generated code */
1712139c1837SPaolo Bonzini    tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
1713139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_JALR, TCG_REG_ZERO, tcg_target_call_iarg_regs[1], 0);
1714139c1837SPaolo Bonzini
1715139c1837SPaolo Bonzini    /* Return path for goto_ptr. Set return value to 0 */
1716c8bc1168SRichard Henderson    tcg_code_gen_epilogue = tcg_splitwx_to_rx(s->code_ptr);
1717139c1837SPaolo Bonzini    tcg_out_mov(s, TCG_TYPE_REG, TCG_REG_A0, TCG_REG_ZERO);
1718139c1837SPaolo Bonzini
1719139c1837SPaolo Bonzini    /* TB epilogue */
1720793f7381SRichard Henderson    tb_ret_addr = tcg_splitwx_to_rx(s->code_ptr);
1721139c1837SPaolo Bonzini    for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
1722139c1837SPaolo Bonzini        tcg_out_ld(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
1723139c1837SPaolo Bonzini                   TCG_REG_SP, SAVE_OFS + i * REG_SIZE);
1724139c1837SPaolo Bonzini    }
1725139c1837SPaolo Bonzini
1726139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_ADDI, TCG_REG_SP, TCG_REG_SP, FRAME_SIZE);
1727139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_JALR, TCG_REG_ZERO, TCG_REG_RA, 0);
1728139c1837SPaolo Bonzini}
1729139c1837SPaolo Bonzini
1730139c1837SPaolo Bonzinistatic void tcg_target_init(TCGContext *s)
1731139c1837SPaolo Bonzini{
1732139c1837SPaolo Bonzini    tcg_target_available_regs[TCG_TYPE_I32] = 0xffffffff;
1733139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS == 64) {
1734139c1837SPaolo Bonzini        tcg_target_available_regs[TCG_TYPE_I64] = 0xffffffff;
1735139c1837SPaolo Bonzini    }
1736139c1837SPaolo Bonzini
1737139c1837SPaolo Bonzini    tcg_target_call_clobber_regs = -1u;
1738139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S0);
1739139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S1);
1740139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S2);
1741139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S3);
1742139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S4);
1743139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S5);
1744139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S6);
1745139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S7);
1746139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S8);
1747139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S9);
1748139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S10);
1749139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S11);
1750139c1837SPaolo Bonzini
1751139c1837SPaolo Bonzini    s->reserved_regs = 0;
1752139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_REG_ZERO);
1753139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP0);
1754139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP1);
1755139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP2);
1756139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_REG_SP);
1757139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_REG_GP);
1758139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TP);
1759139c1837SPaolo Bonzini}
1760139c1837SPaolo Bonzini
1761139c1837SPaolo Bonzinitypedef struct {
1762139c1837SPaolo Bonzini    DebugFrameHeader h;
1763139c1837SPaolo Bonzini    uint8_t fde_def_cfa[4];
1764139c1837SPaolo Bonzini    uint8_t fde_reg_ofs[ARRAY_SIZE(tcg_target_callee_save_regs) * 2];
1765139c1837SPaolo Bonzini} DebugFrame;
1766139c1837SPaolo Bonzini
1767139c1837SPaolo Bonzini#define ELF_HOST_MACHINE EM_RISCV
1768139c1837SPaolo Bonzini
1769139c1837SPaolo Bonzinistatic const DebugFrame debug_frame = {
1770139c1837SPaolo Bonzini    .h.cie.len = sizeof(DebugFrameCIE) - 4, /* length after .len member */
1771139c1837SPaolo Bonzini    .h.cie.id = -1,
1772139c1837SPaolo Bonzini    .h.cie.version = 1,
1773139c1837SPaolo Bonzini    .h.cie.code_align = 1,
1774139c1837SPaolo Bonzini    .h.cie.data_align = -(TCG_TARGET_REG_BITS / 8) & 0x7f, /* sleb128 */
1775139c1837SPaolo Bonzini    .h.cie.return_column = TCG_REG_RA,
1776139c1837SPaolo Bonzini
1777139c1837SPaolo Bonzini    /* Total FDE size does not include the "len" member.  */
1778139c1837SPaolo Bonzini    .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset),
1779139c1837SPaolo Bonzini
1780139c1837SPaolo Bonzini    .fde_def_cfa = {
1781139c1837SPaolo Bonzini        12, TCG_REG_SP,                 /* DW_CFA_def_cfa sp, ... */
1782139c1837SPaolo Bonzini        (FRAME_SIZE & 0x7f) | 0x80,     /* ... uleb128 FRAME_SIZE */
1783139c1837SPaolo Bonzini        (FRAME_SIZE >> 7)
1784139c1837SPaolo Bonzini    },
1785139c1837SPaolo Bonzini    .fde_reg_ofs = {
1786139c1837SPaolo Bonzini        0x80 + 9,  12,                  /* DW_CFA_offset, s1,  -96 */
1787139c1837SPaolo Bonzini        0x80 + 18, 11,                  /* DW_CFA_offset, s2,  -88 */
1788139c1837SPaolo Bonzini        0x80 + 19, 10,                  /* DW_CFA_offset, s3,  -80 */
1789139c1837SPaolo Bonzini        0x80 + 20, 9,                   /* DW_CFA_offset, s4,  -72 */
1790139c1837SPaolo Bonzini        0x80 + 21, 8,                   /* DW_CFA_offset, s5,  -64 */
1791139c1837SPaolo Bonzini        0x80 + 22, 7,                   /* DW_CFA_offset, s6,  -56 */
1792139c1837SPaolo Bonzini        0x80 + 23, 6,                   /* DW_CFA_offset, s7,  -48 */
1793139c1837SPaolo Bonzini        0x80 + 24, 5,                   /* DW_CFA_offset, s8,  -40 */
1794139c1837SPaolo Bonzini        0x80 + 25, 4,                   /* DW_CFA_offset, s9,  -32 */
1795139c1837SPaolo Bonzini        0x80 + 26, 3,                   /* DW_CFA_offset, s10, -24 */
1796139c1837SPaolo Bonzini        0x80 + 27, 2,                   /* DW_CFA_offset, s11, -16 */
1797139c1837SPaolo Bonzini        0x80 + 1 , 1,                   /* DW_CFA_offset, ra,  -8 */
1798139c1837SPaolo Bonzini    }
1799139c1837SPaolo Bonzini};
1800139c1837SPaolo Bonzini
1801755bf9e5SRichard Hendersonvoid tcg_register_jit(const void *buf, size_t buf_size)
1802139c1837SPaolo Bonzini{
1803139c1837SPaolo Bonzini    tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame));
1804139c1837SPaolo Bonzini}
1805