xref: /openbmc/qemu/tcg/riscv/tcg-target.c.inc (revision 6a6d772e30d62e209587ef341df243e9789f5a9f)
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
30a3fb7c99SRichard Henderson#include "../tcg-ldst.c.inc"
31139c1837SPaolo Bonzini#include "../tcg-pool.c.inc"
32139c1837SPaolo Bonzini
33139c1837SPaolo Bonzini#ifdef CONFIG_DEBUG_TCG
34139c1837SPaolo Bonzinistatic const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
35139c1837SPaolo Bonzini    "zero",
36139c1837SPaolo Bonzini    "ra",
37139c1837SPaolo Bonzini    "sp",
38139c1837SPaolo Bonzini    "gp",
39139c1837SPaolo Bonzini    "tp",
40139c1837SPaolo Bonzini    "t0",
41139c1837SPaolo Bonzini    "t1",
42139c1837SPaolo Bonzini    "t2",
43139c1837SPaolo Bonzini    "s0",
44139c1837SPaolo Bonzini    "s1",
45139c1837SPaolo Bonzini    "a0",
46139c1837SPaolo Bonzini    "a1",
47139c1837SPaolo Bonzini    "a2",
48139c1837SPaolo Bonzini    "a3",
49139c1837SPaolo Bonzini    "a4",
50139c1837SPaolo Bonzini    "a5",
51139c1837SPaolo Bonzini    "a6",
52139c1837SPaolo Bonzini    "a7",
53139c1837SPaolo Bonzini    "s2",
54139c1837SPaolo Bonzini    "s3",
55139c1837SPaolo Bonzini    "s4",
56139c1837SPaolo Bonzini    "s5",
57139c1837SPaolo Bonzini    "s6",
58139c1837SPaolo Bonzini    "s7",
59139c1837SPaolo Bonzini    "s8",
60139c1837SPaolo Bonzini    "s9",
61139c1837SPaolo Bonzini    "s10",
62139c1837SPaolo Bonzini    "s11",
63139c1837SPaolo Bonzini    "t3",
64139c1837SPaolo Bonzini    "t4",
65139c1837SPaolo Bonzini    "t5",
66139c1837SPaolo Bonzini    "t6"
67139c1837SPaolo Bonzini};
68139c1837SPaolo Bonzini#endif
69139c1837SPaolo Bonzini
70139c1837SPaolo Bonzinistatic const int tcg_target_reg_alloc_order[] = {
71139c1837SPaolo Bonzini    /* Call saved registers */
72139c1837SPaolo Bonzini    /* TCG_REG_S0 reservered for TCG_AREG0 */
73139c1837SPaolo Bonzini    TCG_REG_S1,
74139c1837SPaolo Bonzini    TCG_REG_S2,
75139c1837SPaolo Bonzini    TCG_REG_S3,
76139c1837SPaolo Bonzini    TCG_REG_S4,
77139c1837SPaolo Bonzini    TCG_REG_S5,
78139c1837SPaolo Bonzini    TCG_REG_S6,
79139c1837SPaolo Bonzini    TCG_REG_S7,
80139c1837SPaolo Bonzini    TCG_REG_S8,
81139c1837SPaolo Bonzini    TCG_REG_S9,
82139c1837SPaolo Bonzini    TCG_REG_S10,
83139c1837SPaolo Bonzini    TCG_REG_S11,
84139c1837SPaolo Bonzini
85139c1837SPaolo Bonzini    /* Call clobbered registers */
86139c1837SPaolo Bonzini    TCG_REG_T0,
87139c1837SPaolo Bonzini    TCG_REG_T1,
88139c1837SPaolo Bonzini    TCG_REG_T2,
89139c1837SPaolo Bonzini    TCG_REG_T3,
90139c1837SPaolo Bonzini    TCG_REG_T4,
91139c1837SPaolo Bonzini    TCG_REG_T5,
92139c1837SPaolo Bonzini    TCG_REG_T6,
93139c1837SPaolo Bonzini
94139c1837SPaolo Bonzini    /* Argument registers */
95139c1837SPaolo Bonzini    TCG_REG_A0,
96139c1837SPaolo Bonzini    TCG_REG_A1,
97139c1837SPaolo Bonzini    TCG_REG_A2,
98139c1837SPaolo Bonzini    TCG_REG_A3,
99139c1837SPaolo Bonzini    TCG_REG_A4,
100139c1837SPaolo Bonzini    TCG_REG_A5,
101139c1837SPaolo Bonzini    TCG_REG_A6,
102139c1837SPaolo Bonzini    TCG_REG_A7,
103139c1837SPaolo Bonzini};
104139c1837SPaolo Bonzini
105139c1837SPaolo Bonzinistatic const int tcg_target_call_iarg_regs[] = {
106139c1837SPaolo Bonzini    TCG_REG_A0,
107139c1837SPaolo Bonzini    TCG_REG_A1,
108139c1837SPaolo Bonzini    TCG_REG_A2,
109139c1837SPaolo Bonzini    TCG_REG_A3,
110139c1837SPaolo Bonzini    TCG_REG_A4,
111139c1837SPaolo Bonzini    TCG_REG_A5,
112139c1837SPaolo Bonzini    TCG_REG_A6,
113139c1837SPaolo Bonzini    TCG_REG_A7,
114139c1837SPaolo Bonzini};
115139c1837SPaolo Bonzini
116139c1837SPaolo Bonzinistatic const int tcg_target_call_oarg_regs[] = {
117139c1837SPaolo Bonzini    TCG_REG_A0,
118139c1837SPaolo Bonzini    TCG_REG_A1,
119139c1837SPaolo Bonzini};
120139c1837SPaolo Bonzini
121139c1837SPaolo Bonzini#define TCG_CT_CONST_ZERO  0x100
122139c1837SPaolo Bonzini#define TCG_CT_CONST_S12   0x200
123139c1837SPaolo Bonzini#define TCG_CT_CONST_N12   0x400
124139c1837SPaolo Bonzini#define TCG_CT_CONST_M12   0x800
125139c1837SPaolo Bonzini
126fc63a4c5SRichard Henderson#define ALL_GENERAL_REGS      MAKE_64BIT_MASK(0, 32)
127fc63a4c5SRichard Henderson/*
128fc63a4c5SRichard Henderson * For softmmu, we need to avoid conflicts with the first 5
129fc63a4c5SRichard Henderson * argument registers to call the helper.  Some of these are
130fc63a4c5SRichard Henderson * also used for the tlb lookup.
131fc63a4c5SRichard Henderson */
132fc63a4c5SRichard Henderson#ifdef CONFIG_SOFTMMU
133fc63a4c5SRichard Henderson#define SOFTMMU_RESERVE_REGS  MAKE_64BIT_MASK(TCG_REG_A0, 5)
134fc63a4c5SRichard Henderson#else
135fc63a4c5SRichard Henderson#define SOFTMMU_RESERVE_REGS  0
136fc63a4c5SRichard Henderson#endif
137fc63a4c5SRichard Henderson
138fc63a4c5SRichard Henderson
139139c1837SPaolo Bonzinistatic inline tcg_target_long sextreg(tcg_target_long val, int pos, int len)
140139c1837SPaolo Bonzini{
141139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS == 32) {
142139c1837SPaolo Bonzini        return sextract32(val, pos, len);
143139c1837SPaolo Bonzini    } else {
144139c1837SPaolo Bonzini        return sextract64(val, pos, len);
145139c1837SPaolo Bonzini    }
146139c1837SPaolo Bonzini}
147139c1837SPaolo Bonzini
148139c1837SPaolo Bonzini/* test if a constant matches the constraint */
149a4fbbd77SRichard Hendersonstatic bool tcg_target_const_match(int64_t val, TCGType type, int ct)
150139c1837SPaolo Bonzini{
151139c1837SPaolo Bonzini    if (ct & TCG_CT_CONST) {
152139c1837SPaolo Bonzini        return 1;
153139c1837SPaolo Bonzini    }
154139c1837SPaolo Bonzini    if ((ct & TCG_CT_CONST_ZERO) && val == 0) {
155139c1837SPaolo Bonzini        return 1;
156139c1837SPaolo Bonzini    }
15762722763SRichard Henderson    /*
15862722763SRichard Henderson     * Sign extended from 12 bits: [-0x800, 0x7ff].
15962722763SRichard Henderson     * Used for most arithmetic, as this is the isa field.
16062722763SRichard Henderson     */
16162722763SRichard Henderson    if ((ct & TCG_CT_CONST_S12) && val >= -0x800 && val <= 0x7ff) {
162139c1837SPaolo Bonzini        return 1;
163139c1837SPaolo Bonzini    }
16462722763SRichard Henderson    /*
16562722763SRichard Henderson     * Sign extended from 12 bits, negated: [-0x7ff, 0x800].
16662722763SRichard Henderson     * Used for subtraction, where a constant must be handled by ADDI.
16762722763SRichard Henderson     */
16862722763SRichard Henderson    if ((ct & TCG_CT_CONST_N12) && val >= -0x7ff && val <= 0x800) {
169139c1837SPaolo Bonzini        return 1;
170139c1837SPaolo Bonzini    }
17162722763SRichard Henderson    /*
17262722763SRichard Henderson     * Sign extended from 12 bits, +/- matching: [-0x7ff, 0x7ff].
17362722763SRichard Henderson     * Used by addsub2, which may need the negative operation,
17462722763SRichard Henderson     * and requires the modified constant to be representable.
17562722763SRichard Henderson     */
17662722763SRichard Henderson    if ((ct & TCG_CT_CONST_M12) && val >= -0x7ff && val <= 0x7ff) {
177139c1837SPaolo Bonzini        return 1;
178139c1837SPaolo Bonzini    }
179139c1837SPaolo Bonzini    return 0;
180139c1837SPaolo Bonzini}
181139c1837SPaolo Bonzini
182139c1837SPaolo Bonzini/*
183139c1837SPaolo Bonzini * RISC-V Base ISA opcodes (IM)
184139c1837SPaolo Bonzini */
185139c1837SPaolo Bonzini
186139c1837SPaolo Bonzinitypedef enum {
187139c1837SPaolo Bonzini    OPC_ADD = 0x33,
188139c1837SPaolo Bonzini    OPC_ADDI = 0x13,
189139c1837SPaolo Bonzini    OPC_AND = 0x7033,
190139c1837SPaolo Bonzini    OPC_ANDI = 0x7013,
191139c1837SPaolo Bonzini    OPC_AUIPC = 0x17,
192139c1837SPaolo Bonzini    OPC_BEQ = 0x63,
193139c1837SPaolo Bonzini    OPC_BGE = 0x5063,
194139c1837SPaolo Bonzini    OPC_BGEU = 0x7063,
195139c1837SPaolo Bonzini    OPC_BLT = 0x4063,
196139c1837SPaolo Bonzini    OPC_BLTU = 0x6063,
197139c1837SPaolo Bonzini    OPC_BNE = 0x1063,
198139c1837SPaolo Bonzini    OPC_DIV = 0x2004033,
199139c1837SPaolo Bonzini    OPC_DIVU = 0x2005033,
200139c1837SPaolo Bonzini    OPC_JAL = 0x6f,
201139c1837SPaolo Bonzini    OPC_JALR = 0x67,
202139c1837SPaolo Bonzini    OPC_LB = 0x3,
203139c1837SPaolo Bonzini    OPC_LBU = 0x4003,
204139c1837SPaolo Bonzini    OPC_LD = 0x3003,
205139c1837SPaolo Bonzini    OPC_LH = 0x1003,
206139c1837SPaolo Bonzini    OPC_LHU = 0x5003,
207139c1837SPaolo Bonzini    OPC_LUI = 0x37,
208139c1837SPaolo Bonzini    OPC_LW = 0x2003,
209139c1837SPaolo Bonzini    OPC_LWU = 0x6003,
210139c1837SPaolo Bonzini    OPC_MUL = 0x2000033,
211139c1837SPaolo Bonzini    OPC_MULH = 0x2001033,
212139c1837SPaolo Bonzini    OPC_MULHSU = 0x2002033,
213139c1837SPaolo Bonzini    OPC_MULHU = 0x2003033,
214139c1837SPaolo Bonzini    OPC_OR = 0x6033,
215139c1837SPaolo Bonzini    OPC_ORI = 0x6013,
216139c1837SPaolo Bonzini    OPC_REM = 0x2006033,
217139c1837SPaolo Bonzini    OPC_REMU = 0x2007033,
218139c1837SPaolo Bonzini    OPC_SB = 0x23,
219139c1837SPaolo Bonzini    OPC_SD = 0x3023,
220139c1837SPaolo Bonzini    OPC_SH = 0x1023,
221139c1837SPaolo Bonzini    OPC_SLL = 0x1033,
222139c1837SPaolo Bonzini    OPC_SLLI = 0x1013,
223139c1837SPaolo Bonzini    OPC_SLT = 0x2033,
224139c1837SPaolo Bonzini    OPC_SLTI = 0x2013,
225139c1837SPaolo Bonzini    OPC_SLTIU = 0x3013,
226139c1837SPaolo Bonzini    OPC_SLTU = 0x3033,
227139c1837SPaolo Bonzini    OPC_SRA = 0x40005033,
228139c1837SPaolo Bonzini    OPC_SRAI = 0x40005013,
229139c1837SPaolo Bonzini    OPC_SRL = 0x5033,
230139c1837SPaolo Bonzini    OPC_SRLI = 0x5013,
231139c1837SPaolo Bonzini    OPC_SUB = 0x40000033,
232139c1837SPaolo Bonzini    OPC_SW = 0x2023,
233139c1837SPaolo Bonzini    OPC_XOR = 0x4033,
234139c1837SPaolo Bonzini    OPC_XORI = 0x4013,
235139c1837SPaolo Bonzini
236139c1837SPaolo Bonzini#if TCG_TARGET_REG_BITS == 64
237139c1837SPaolo Bonzini    OPC_ADDIW = 0x1b,
238139c1837SPaolo Bonzini    OPC_ADDW = 0x3b,
239139c1837SPaolo Bonzini    OPC_DIVUW = 0x200503b,
240139c1837SPaolo Bonzini    OPC_DIVW = 0x200403b,
241139c1837SPaolo Bonzini    OPC_MULW = 0x200003b,
242139c1837SPaolo Bonzini    OPC_REMUW = 0x200703b,
243139c1837SPaolo Bonzini    OPC_REMW = 0x200603b,
244139c1837SPaolo Bonzini    OPC_SLLIW = 0x101b,
245139c1837SPaolo Bonzini    OPC_SLLW = 0x103b,
246139c1837SPaolo Bonzini    OPC_SRAIW = 0x4000501b,
247139c1837SPaolo Bonzini    OPC_SRAW = 0x4000503b,
248139c1837SPaolo Bonzini    OPC_SRLIW = 0x501b,
249139c1837SPaolo Bonzini    OPC_SRLW = 0x503b,
250139c1837SPaolo Bonzini    OPC_SUBW = 0x4000003b,
251139c1837SPaolo Bonzini#else
252139c1837SPaolo Bonzini    /* Simplify code throughout by defining aliases for RV32.  */
253139c1837SPaolo Bonzini    OPC_ADDIW = OPC_ADDI,
254139c1837SPaolo Bonzini    OPC_ADDW = OPC_ADD,
255139c1837SPaolo Bonzini    OPC_DIVUW = OPC_DIVU,
256139c1837SPaolo Bonzini    OPC_DIVW = OPC_DIV,
257139c1837SPaolo Bonzini    OPC_MULW = OPC_MUL,
258139c1837SPaolo Bonzini    OPC_REMUW = OPC_REMU,
259139c1837SPaolo Bonzini    OPC_REMW = OPC_REM,
260139c1837SPaolo Bonzini    OPC_SLLIW = OPC_SLLI,
261139c1837SPaolo Bonzini    OPC_SLLW = OPC_SLL,
262139c1837SPaolo Bonzini    OPC_SRAIW = OPC_SRAI,
263139c1837SPaolo Bonzini    OPC_SRAW = OPC_SRA,
264139c1837SPaolo Bonzini    OPC_SRLIW = OPC_SRLI,
265139c1837SPaolo Bonzini    OPC_SRLW = OPC_SRL,
266139c1837SPaolo Bonzini    OPC_SUBW = OPC_SUB,
267139c1837SPaolo Bonzini#endif
268139c1837SPaolo Bonzini
269139c1837SPaolo Bonzini    OPC_FENCE = 0x0000000f,
2709ae958e4SRichard Henderson    OPC_NOP   = OPC_ADDI,   /* nop = addi r0,r0,0 */
271139c1837SPaolo Bonzini} RISCVInsn;
272139c1837SPaolo Bonzini
273139c1837SPaolo Bonzini/*
274139c1837SPaolo Bonzini * RISC-V immediate and instruction encoders (excludes 16-bit RVC)
275139c1837SPaolo Bonzini */
276139c1837SPaolo Bonzini
277139c1837SPaolo Bonzini/* Type-R */
278139c1837SPaolo Bonzini
279139c1837SPaolo Bonzinistatic int32_t encode_r(RISCVInsn opc, TCGReg rd, TCGReg rs1, TCGReg rs2)
280139c1837SPaolo Bonzini{
281139c1837SPaolo Bonzini    return opc | (rd & 0x1f) << 7 | (rs1 & 0x1f) << 15 | (rs2 & 0x1f) << 20;
282139c1837SPaolo Bonzini}
283139c1837SPaolo Bonzini
284139c1837SPaolo Bonzini/* Type-I */
285139c1837SPaolo Bonzini
286139c1837SPaolo Bonzinistatic int32_t encode_imm12(uint32_t imm)
287139c1837SPaolo Bonzini{
288139c1837SPaolo Bonzini    return (imm & 0xfff) << 20;
289139c1837SPaolo Bonzini}
290139c1837SPaolo Bonzini
291139c1837SPaolo Bonzinistatic int32_t encode_i(RISCVInsn opc, TCGReg rd, TCGReg rs1, uint32_t imm)
292139c1837SPaolo Bonzini{
293139c1837SPaolo Bonzini    return opc | (rd & 0x1f) << 7 | (rs1 & 0x1f) << 15 | encode_imm12(imm);
294139c1837SPaolo Bonzini}
295139c1837SPaolo Bonzini
296139c1837SPaolo Bonzini/* Type-S */
297139c1837SPaolo Bonzini
298139c1837SPaolo Bonzinistatic int32_t encode_simm12(uint32_t imm)
299139c1837SPaolo Bonzini{
300139c1837SPaolo Bonzini    int32_t ret = 0;
301139c1837SPaolo Bonzini
302139c1837SPaolo Bonzini    ret |= (imm & 0xFE0) << 20;
303139c1837SPaolo Bonzini    ret |= (imm & 0x1F) << 7;
304139c1837SPaolo Bonzini
305139c1837SPaolo Bonzini    return ret;
306139c1837SPaolo Bonzini}
307139c1837SPaolo Bonzini
308139c1837SPaolo Bonzinistatic int32_t encode_s(RISCVInsn opc, TCGReg rs1, TCGReg rs2, uint32_t imm)
309139c1837SPaolo Bonzini{
310139c1837SPaolo Bonzini    return opc | (rs1 & 0x1f) << 15 | (rs2 & 0x1f) << 20 | encode_simm12(imm);
311139c1837SPaolo Bonzini}
312139c1837SPaolo Bonzini
313139c1837SPaolo Bonzini/* Type-SB */
314139c1837SPaolo Bonzini
315139c1837SPaolo Bonzinistatic int32_t encode_sbimm12(uint32_t imm)
316139c1837SPaolo Bonzini{
317139c1837SPaolo Bonzini    int32_t ret = 0;
318139c1837SPaolo Bonzini
319139c1837SPaolo Bonzini    ret |= (imm & 0x1000) << 19;
320139c1837SPaolo Bonzini    ret |= (imm & 0x7e0) << 20;
321139c1837SPaolo Bonzini    ret |= (imm & 0x1e) << 7;
322139c1837SPaolo Bonzini    ret |= (imm & 0x800) >> 4;
323139c1837SPaolo Bonzini
324139c1837SPaolo Bonzini    return ret;
325139c1837SPaolo Bonzini}
326139c1837SPaolo Bonzini
327139c1837SPaolo Bonzinistatic int32_t encode_sb(RISCVInsn opc, TCGReg rs1, TCGReg rs2, uint32_t imm)
328139c1837SPaolo Bonzini{
329139c1837SPaolo Bonzini    return opc | (rs1 & 0x1f) << 15 | (rs2 & 0x1f) << 20 | encode_sbimm12(imm);
330139c1837SPaolo Bonzini}
331139c1837SPaolo Bonzini
332139c1837SPaolo Bonzini/* Type-U */
333139c1837SPaolo Bonzini
334139c1837SPaolo Bonzinistatic int32_t encode_uimm20(uint32_t imm)
335139c1837SPaolo Bonzini{
336139c1837SPaolo Bonzini    return imm & 0xfffff000;
337139c1837SPaolo Bonzini}
338139c1837SPaolo Bonzini
339139c1837SPaolo Bonzinistatic int32_t encode_u(RISCVInsn opc, TCGReg rd, uint32_t imm)
340139c1837SPaolo Bonzini{
341139c1837SPaolo Bonzini    return opc | (rd & 0x1f) << 7 | encode_uimm20(imm);
342139c1837SPaolo Bonzini}
343139c1837SPaolo Bonzini
344139c1837SPaolo Bonzini/* Type-UJ */
345139c1837SPaolo Bonzini
346139c1837SPaolo Bonzinistatic int32_t encode_ujimm20(uint32_t imm)
347139c1837SPaolo Bonzini{
348139c1837SPaolo Bonzini    int32_t ret = 0;
349139c1837SPaolo Bonzini
350139c1837SPaolo Bonzini    ret |= (imm & 0x0007fe) << (21 - 1);
351139c1837SPaolo Bonzini    ret |= (imm & 0x000800) << (20 - 11);
352139c1837SPaolo Bonzini    ret |= (imm & 0x0ff000) << (12 - 12);
353139c1837SPaolo Bonzini    ret |= (imm & 0x100000) << (31 - 20);
354139c1837SPaolo Bonzini
355139c1837SPaolo Bonzini    return ret;
356139c1837SPaolo Bonzini}
357139c1837SPaolo Bonzini
358139c1837SPaolo Bonzinistatic int32_t encode_uj(RISCVInsn opc, TCGReg rd, uint32_t imm)
359139c1837SPaolo Bonzini{
360139c1837SPaolo Bonzini    return opc | (rd & 0x1f) << 7 | encode_ujimm20(imm);
361139c1837SPaolo Bonzini}
362139c1837SPaolo Bonzini
363139c1837SPaolo Bonzini/*
364139c1837SPaolo Bonzini * RISC-V instruction emitters
365139c1837SPaolo Bonzini */
366139c1837SPaolo Bonzini
367139c1837SPaolo Bonzinistatic void tcg_out_opc_reg(TCGContext *s, RISCVInsn opc,
368139c1837SPaolo Bonzini                            TCGReg rd, TCGReg rs1, TCGReg rs2)
369139c1837SPaolo Bonzini{
370139c1837SPaolo Bonzini    tcg_out32(s, encode_r(opc, rd, rs1, rs2));
371139c1837SPaolo Bonzini}
372139c1837SPaolo Bonzini
373139c1837SPaolo Bonzinistatic void tcg_out_opc_imm(TCGContext *s, RISCVInsn opc,
374139c1837SPaolo Bonzini                            TCGReg rd, TCGReg rs1, TCGArg imm)
375139c1837SPaolo Bonzini{
376139c1837SPaolo Bonzini    tcg_out32(s, encode_i(opc, rd, rs1, imm));
377139c1837SPaolo Bonzini}
378139c1837SPaolo Bonzini
379139c1837SPaolo Bonzinistatic void tcg_out_opc_store(TCGContext *s, RISCVInsn opc,
380139c1837SPaolo Bonzini                              TCGReg rs1, TCGReg rs2, uint32_t imm)
381139c1837SPaolo Bonzini{
382139c1837SPaolo Bonzini    tcg_out32(s, encode_s(opc, rs1, rs2, imm));
383139c1837SPaolo Bonzini}
384139c1837SPaolo Bonzini
385139c1837SPaolo Bonzinistatic void tcg_out_opc_branch(TCGContext *s, RISCVInsn opc,
386139c1837SPaolo Bonzini                               TCGReg rs1, TCGReg rs2, uint32_t imm)
387139c1837SPaolo Bonzini{
388139c1837SPaolo Bonzini    tcg_out32(s, encode_sb(opc, rs1, rs2, imm));
389139c1837SPaolo Bonzini}
390139c1837SPaolo Bonzini
391139c1837SPaolo Bonzinistatic void tcg_out_opc_upper(TCGContext *s, RISCVInsn opc,
392139c1837SPaolo Bonzini                              TCGReg rd, uint32_t imm)
393139c1837SPaolo Bonzini{
394139c1837SPaolo Bonzini    tcg_out32(s, encode_u(opc, rd, imm));
395139c1837SPaolo Bonzini}
396139c1837SPaolo Bonzini
397139c1837SPaolo Bonzinistatic void tcg_out_opc_jump(TCGContext *s, RISCVInsn opc,
398139c1837SPaolo Bonzini                             TCGReg rd, uint32_t imm)
399139c1837SPaolo Bonzini{
400139c1837SPaolo Bonzini    tcg_out32(s, encode_uj(opc, rd, imm));
401139c1837SPaolo Bonzini}
402139c1837SPaolo Bonzini
403139c1837SPaolo Bonzinistatic void tcg_out_nop_fill(tcg_insn_unit *p, int count)
404139c1837SPaolo Bonzini{
405139c1837SPaolo Bonzini    int i;
406139c1837SPaolo Bonzini    for (i = 0; i < count; ++i) {
4079ae958e4SRichard Henderson        p[i] = OPC_NOP;
408139c1837SPaolo Bonzini    }
409139c1837SPaolo Bonzini}
410139c1837SPaolo Bonzini
411139c1837SPaolo Bonzini/*
412139c1837SPaolo Bonzini * Relocations
413139c1837SPaolo Bonzini */
414139c1837SPaolo Bonzini
415793f7381SRichard Hendersonstatic bool reloc_sbimm12(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
416139c1837SPaolo Bonzini{
417793f7381SRichard Henderson    const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
418793f7381SRichard Henderson    intptr_t offset = (intptr_t)target - (intptr_t)src_rx;
419139c1837SPaolo Bonzini
420844d0442SRichard Henderson    tcg_debug_assert((offset & 1) == 0);
421844d0442SRichard Henderson    if (offset == sextreg(offset, 0, 12)) {
422793f7381SRichard Henderson        *src_rw |= encode_sbimm12(offset);
423139c1837SPaolo Bonzini        return true;
424139c1837SPaolo Bonzini    }
425139c1837SPaolo Bonzini
426139c1837SPaolo Bonzini    return false;
427139c1837SPaolo Bonzini}
428139c1837SPaolo Bonzini
429793f7381SRichard Hendersonstatic bool reloc_jimm20(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
430139c1837SPaolo Bonzini{
431793f7381SRichard Henderson    const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
432793f7381SRichard Henderson    intptr_t offset = (intptr_t)target - (intptr_t)src_rx;
433139c1837SPaolo Bonzini
434844d0442SRichard Henderson    tcg_debug_assert((offset & 1) == 0);
435844d0442SRichard Henderson    if (offset == sextreg(offset, 0, 20)) {
436793f7381SRichard Henderson        *src_rw |= encode_ujimm20(offset);
437139c1837SPaolo Bonzini        return true;
438139c1837SPaolo Bonzini    }
439139c1837SPaolo Bonzini
440139c1837SPaolo Bonzini    return false;
441139c1837SPaolo Bonzini}
442139c1837SPaolo Bonzini
443793f7381SRichard Hendersonstatic bool reloc_call(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
444139c1837SPaolo Bonzini{
445793f7381SRichard Henderson    const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
446793f7381SRichard Henderson    intptr_t offset = (intptr_t)target - (intptr_t)src_rx;
447139c1837SPaolo Bonzini    int32_t lo = sextreg(offset, 0, 12);
448139c1837SPaolo Bonzini    int32_t hi = offset - lo;
449139c1837SPaolo Bonzini
450139c1837SPaolo Bonzini    if (offset == hi + lo) {
451793f7381SRichard Henderson        src_rw[0] |= encode_uimm20(hi);
452793f7381SRichard Henderson        src_rw[1] |= encode_imm12(lo);
453139c1837SPaolo Bonzini        return true;
454139c1837SPaolo Bonzini    }
455139c1837SPaolo Bonzini
456139c1837SPaolo Bonzini    return false;
457139c1837SPaolo Bonzini}
458139c1837SPaolo Bonzini
459139c1837SPaolo Bonzinistatic bool patch_reloc(tcg_insn_unit *code_ptr, int type,
460139c1837SPaolo Bonzini                        intptr_t value, intptr_t addend)
461139c1837SPaolo Bonzini{
462139c1837SPaolo Bonzini    tcg_debug_assert(addend == 0);
463139c1837SPaolo Bonzini    switch (type) {
464139c1837SPaolo Bonzini    case R_RISCV_BRANCH:
465139c1837SPaolo Bonzini        return reloc_sbimm12(code_ptr, (tcg_insn_unit *)value);
466139c1837SPaolo Bonzini    case R_RISCV_JAL:
467139c1837SPaolo Bonzini        return reloc_jimm20(code_ptr, (tcg_insn_unit *)value);
468139c1837SPaolo Bonzini    case R_RISCV_CALL:
469139c1837SPaolo Bonzini        return reloc_call(code_ptr, (tcg_insn_unit *)value);
470139c1837SPaolo Bonzini    default:
4714b6a52d0SRichard Henderson        g_assert_not_reached();
472139c1837SPaolo Bonzini    }
473139c1837SPaolo Bonzini}
474139c1837SPaolo Bonzini
475139c1837SPaolo Bonzini/*
476139c1837SPaolo Bonzini * TCG intrinsics
477139c1837SPaolo Bonzini */
478139c1837SPaolo Bonzini
479139c1837SPaolo Bonzinistatic bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
480139c1837SPaolo Bonzini{
481139c1837SPaolo Bonzini    if (ret == arg) {
482139c1837SPaolo Bonzini        return true;
483139c1837SPaolo Bonzini    }
484139c1837SPaolo Bonzini    switch (type) {
485139c1837SPaolo Bonzini    case TCG_TYPE_I32:
486139c1837SPaolo Bonzini    case TCG_TYPE_I64:
487139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_ADDI, ret, arg, 0);
488139c1837SPaolo Bonzini        break;
489139c1837SPaolo Bonzini    default:
490139c1837SPaolo Bonzini        g_assert_not_reached();
491139c1837SPaolo Bonzini    }
492139c1837SPaolo Bonzini    return true;
493139c1837SPaolo Bonzini}
494139c1837SPaolo Bonzini
495139c1837SPaolo Bonzinistatic void tcg_out_movi(TCGContext *s, TCGType type, TCGReg rd,
496139c1837SPaolo Bonzini                         tcg_target_long val)
497139c1837SPaolo Bonzini{
498139c1837SPaolo Bonzini    tcg_target_long lo, hi, tmp;
499139c1837SPaolo Bonzini    int shift, ret;
500139c1837SPaolo Bonzini
501139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I32) {
502139c1837SPaolo Bonzini        val = (int32_t)val;
503139c1837SPaolo Bonzini    }
504139c1837SPaolo Bonzini
505139c1837SPaolo Bonzini    lo = sextreg(val, 0, 12);
506139c1837SPaolo Bonzini    if (val == lo) {
507139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_ADDI, rd, TCG_REG_ZERO, lo);
508139c1837SPaolo Bonzini        return;
509139c1837SPaolo Bonzini    }
510139c1837SPaolo Bonzini
511139c1837SPaolo Bonzini    hi = val - lo;
512139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS == 32 || val == (int32_t)val) {
513139c1837SPaolo Bonzini        tcg_out_opc_upper(s, OPC_LUI, rd, hi);
514139c1837SPaolo Bonzini        if (lo != 0) {
515139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_ADDIW, rd, rd, lo);
516139c1837SPaolo Bonzini        }
517139c1837SPaolo Bonzini        return;
518139c1837SPaolo Bonzini    }
519139c1837SPaolo Bonzini
520139c1837SPaolo Bonzini    /* We can only be here if TCG_TARGET_REG_BITS != 32 */
521139c1837SPaolo Bonzini    tmp = tcg_pcrel_diff(s, (void *)val);
522139c1837SPaolo Bonzini    if (tmp == (int32_t)tmp) {
523139c1837SPaolo Bonzini        tcg_out_opc_upper(s, OPC_AUIPC, rd, 0);
524139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_ADDI, rd, rd, 0);
525793f7381SRichard Henderson        ret = reloc_call(s->code_ptr - 2, (const tcg_insn_unit *)val);
526139c1837SPaolo Bonzini        tcg_debug_assert(ret == true);
527139c1837SPaolo Bonzini        return;
528139c1837SPaolo Bonzini    }
529139c1837SPaolo Bonzini
530139c1837SPaolo Bonzini    /* Look for a single 20-bit section.  */
531139c1837SPaolo Bonzini    shift = ctz64(val);
532139c1837SPaolo Bonzini    tmp = val >> shift;
533139c1837SPaolo Bonzini    if (tmp == sextreg(tmp, 0, 20)) {
534139c1837SPaolo Bonzini        tcg_out_opc_upper(s, OPC_LUI, rd, tmp << 12);
535139c1837SPaolo Bonzini        if (shift > 12) {
536139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_SLLI, rd, rd, shift - 12);
537139c1837SPaolo Bonzini        } else {
538139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_SRAI, rd, rd, 12 - shift);
539139c1837SPaolo Bonzini        }
540139c1837SPaolo Bonzini        return;
541139c1837SPaolo Bonzini    }
542139c1837SPaolo Bonzini
543139c1837SPaolo Bonzini    /* Look for a few high zero bits, with lots of bits set in the middle.  */
544139c1837SPaolo Bonzini    shift = clz64(val);
545139c1837SPaolo Bonzini    tmp = val << shift;
546139c1837SPaolo Bonzini    if (tmp == sextreg(tmp, 12, 20) << 12) {
547139c1837SPaolo Bonzini        tcg_out_opc_upper(s, OPC_LUI, rd, tmp);
548139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_SRLI, rd, rd, shift);
549139c1837SPaolo Bonzini        return;
550139c1837SPaolo Bonzini    } else if (tmp == sextreg(tmp, 0, 12)) {
551139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_ADDI, rd, TCG_REG_ZERO, tmp);
552139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_SRLI, rd, rd, shift);
553139c1837SPaolo Bonzini        return;
554139c1837SPaolo Bonzini    }
555139c1837SPaolo Bonzini
556139c1837SPaolo Bonzini    /* Drop into the constant pool.  */
557139c1837SPaolo Bonzini    new_pool_label(s, val, R_RISCV_CALL, s->code_ptr, 0);
558139c1837SPaolo Bonzini    tcg_out_opc_upper(s, OPC_AUIPC, rd, 0);
559139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_LD, rd, rd, 0);
560139c1837SPaolo Bonzini}
561139c1837SPaolo Bonzini
562*6a6d772eSRichard Hendersonstatic void tcg_out_addi_ptr(TCGContext *s, TCGReg rd, TCGReg rs,
563*6a6d772eSRichard Henderson                             tcg_target_long imm)
564*6a6d772eSRichard Henderson{
565*6a6d772eSRichard Henderson    /* This function is only used for passing structs by reference. */
566*6a6d772eSRichard Henderson    g_assert_not_reached();
567*6a6d772eSRichard Henderson}
568*6a6d772eSRichard Henderson
569139c1837SPaolo Bonzinistatic void tcg_out_ext8u(TCGContext *s, TCGReg ret, TCGReg arg)
570139c1837SPaolo Bonzini{
571139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_ANDI, ret, arg, 0xff);
572139c1837SPaolo Bonzini}
573139c1837SPaolo Bonzini
574139c1837SPaolo Bonzinistatic void tcg_out_ext16u(TCGContext *s, TCGReg ret, TCGReg arg)
575139c1837SPaolo Bonzini{
576139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SLLIW, ret, arg, 16);
577139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SRLIW, ret, ret, 16);
578139c1837SPaolo Bonzini}
579139c1837SPaolo Bonzini
580139c1837SPaolo Bonzinistatic void tcg_out_ext32u(TCGContext *s, TCGReg ret, TCGReg arg)
581139c1837SPaolo Bonzini{
582139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SLLI, ret, arg, 32);
583139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SRLI, ret, ret, 32);
584139c1837SPaolo Bonzini}
585139c1837SPaolo Bonzini
586139c1837SPaolo Bonzinistatic void tcg_out_ext8s(TCGContext *s, TCGReg ret, TCGReg arg)
587139c1837SPaolo Bonzini{
588139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SLLIW, ret, arg, 24);
589139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SRAIW, ret, ret, 24);
590139c1837SPaolo Bonzini}
591139c1837SPaolo Bonzini
592139c1837SPaolo Bonzinistatic void tcg_out_ext16s(TCGContext *s, TCGReg ret, TCGReg arg)
593139c1837SPaolo Bonzini{
594139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SLLIW, ret, arg, 16);
595139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SRAIW, ret, ret, 16);
596139c1837SPaolo Bonzini}
597139c1837SPaolo Bonzini
598139c1837SPaolo Bonzinistatic void tcg_out_ext32s(TCGContext *s, TCGReg ret, TCGReg arg)
599139c1837SPaolo Bonzini{
600139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_ADDIW, ret, arg, 0);
601139c1837SPaolo Bonzini}
602139c1837SPaolo Bonzini
603139c1837SPaolo Bonzinistatic void tcg_out_ldst(TCGContext *s, RISCVInsn opc, TCGReg data,
604139c1837SPaolo Bonzini                         TCGReg addr, intptr_t offset)
605139c1837SPaolo Bonzini{
606139c1837SPaolo Bonzini    intptr_t imm12 = sextreg(offset, 0, 12);
607139c1837SPaolo Bonzini
608139c1837SPaolo Bonzini    if (offset != imm12) {
6099d9db413SRichard Henderson        intptr_t diff = tcg_pcrel_diff(s, (void *)offset);
610139c1837SPaolo Bonzini
611139c1837SPaolo Bonzini        if (addr == TCG_REG_ZERO && diff == (int32_t)diff) {
612139c1837SPaolo Bonzini            imm12 = sextreg(diff, 0, 12);
613139c1837SPaolo Bonzini            tcg_out_opc_upper(s, OPC_AUIPC, TCG_REG_TMP2, diff - imm12);
614139c1837SPaolo Bonzini        } else {
615139c1837SPaolo Bonzini            tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP2, offset - imm12);
616139c1837SPaolo Bonzini            if (addr != TCG_REG_ZERO) {
617139c1837SPaolo Bonzini                tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP2, TCG_REG_TMP2, addr);
618139c1837SPaolo Bonzini            }
619139c1837SPaolo Bonzini        }
620139c1837SPaolo Bonzini        addr = TCG_REG_TMP2;
621139c1837SPaolo Bonzini    }
622139c1837SPaolo Bonzini
623139c1837SPaolo Bonzini    switch (opc) {
624139c1837SPaolo Bonzini    case OPC_SB:
625139c1837SPaolo Bonzini    case OPC_SH:
626139c1837SPaolo Bonzini    case OPC_SW:
627139c1837SPaolo Bonzini    case OPC_SD:
628139c1837SPaolo Bonzini        tcg_out_opc_store(s, opc, addr, data, imm12);
629139c1837SPaolo Bonzini        break;
630139c1837SPaolo Bonzini    case OPC_LB:
631139c1837SPaolo Bonzini    case OPC_LBU:
632139c1837SPaolo Bonzini    case OPC_LH:
633139c1837SPaolo Bonzini    case OPC_LHU:
634139c1837SPaolo Bonzini    case OPC_LW:
635139c1837SPaolo Bonzini    case OPC_LWU:
636139c1837SPaolo Bonzini    case OPC_LD:
637139c1837SPaolo Bonzini        tcg_out_opc_imm(s, opc, data, addr, imm12);
638139c1837SPaolo Bonzini        break;
639139c1837SPaolo Bonzini    default:
640139c1837SPaolo Bonzini        g_assert_not_reached();
641139c1837SPaolo Bonzini    }
642139c1837SPaolo Bonzini}
643139c1837SPaolo Bonzini
644139c1837SPaolo Bonzinistatic void tcg_out_ld(TCGContext *s, TCGType type, TCGReg arg,
645139c1837SPaolo Bonzini                       TCGReg arg1, intptr_t arg2)
646139c1837SPaolo Bonzini{
647139c1837SPaolo Bonzini    bool is32bit = (TCG_TARGET_REG_BITS == 32 || type == TCG_TYPE_I32);
648139c1837SPaolo Bonzini    tcg_out_ldst(s, is32bit ? OPC_LW : OPC_LD, arg, arg1, arg2);
649139c1837SPaolo Bonzini}
650139c1837SPaolo Bonzini
651139c1837SPaolo Bonzinistatic void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
652139c1837SPaolo Bonzini                       TCGReg arg1, intptr_t arg2)
653139c1837SPaolo Bonzini{
654139c1837SPaolo Bonzini    bool is32bit = (TCG_TARGET_REG_BITS == 32 || type == TCG_TYPE_I32);
655139c1837SPaolo Bonzini    tcg_out_ldst(s, is32bit ? OPC_SW : OPC_SD, arg, arg1, arg2);
656139c1837SPaolo Bonzini}
657139c1837SPaolo Bonzini
658139c1837SPaolo Bonzinistatic bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
659139c1837SPaolo Bonzini                        TCGReg base, intptr_t ofs)
660139c1837SPaolo Bonzini{
661139c1837SPaolo Bonzini    if (val == 0) {
662139c1837SPaolo Bonzini        tcg_out_st(s, type, TCG_REG_ZERO, base, ofs);
663139c1837SPaolo Bonzini        return true;
664139c1837SPaolo Bonzini    }
665139c1837SPaolo Bonzini    return false;
666139c1837SPaolo Bonzini}
667139c1837SPaolo Bonzini
668139c1837SPaolo Bonzinistatic void tcg_out_addsub2(TCGContext *s,
669139c1837SPaolo Bonzini                            TCGReg rl, TCGReg rh,
670139c1837SPaolo Bonzini                            TCGReg al, TCGReg ah,
671139c1837SPaolo Bonzini                            TCGArg bl, TCGArg bh,
672139c1837SPaolo Bonzini                            bool cbl, bool cbh, bool is_sub, bool is32bit)
673139c1837SPaolo Bonzini{
674139c1837SPaolo Bonzini    const RISCVInsn opc_add = is32bit ? OPC_ADDW : OPC_ADD;
675139c1837SPaolo Bonzini    const RISCVInsn opc_addi = is32bit ? OPC_ADDIW : OPC_ADDI;
676139c1837SPaolo Bonzini    const RISCVInsn opc_sub = is32bit ? OPC_SUBW : OPC_SUB;
677139c1837SPaolo Bonzini    TCGReg th = TCG_REG_TMP1;
678139c1837SPaolo Bonzini
679139c1837SPaolo Bonzini    /* If we have a negative constant such that negating it would
680139c1837SPaolo Bonzini       make the high part zero, we can (usually) eliminate one insn.  */
681139c1837SPaolo Bonzini    if (cbl && cbh && bh == -1 && bl != 0) {
682139c1837SPaolo Bonzini        bl = -bl;
683139c1837SPaolo Bonzini        bh = 0;
684139c1837SPaolo Bonzini        is_sub = !is_sub;
685139c1837SPaolo Bonzini    }
686139c1837SPaolo Bonzini
687139c1837SPaolo Bonzini    /* By operating on the high part first, we get to use the final
688139c1837SPaolo Bonzini       carry operation to move back from the temporary.  */
689139c1837SPaolo Bonzini    if (!cbh) {
690139c1837SPaolo Bonzini        tcg_out_opc_reg(s, (is_sub ? opc_sub : opc_add), th, ah, bh);
691139c1837SPaolo Bonzini    } else if (bh != 0 || ah == rl) {
692139c1837SPaolo Bonzini        tcg_out_opc_imm(s, opc_addi, th, ah, (is_sub ? -bh : bh));
693139c1837SPaolo Bonzini    } else {
694139c1837SPaolo Bonzini        th = ah;
695139c1837SPaolo Bonzini    }
696139c1837SPaolo Bonzini
697139c1837SPaolo Bonzini    /* Note that tcg optimization should eliminate the bl == 0 case.  */
698139c1837SPaolo Bonzini    if (is_sub) {
699139c1837SPaolo Bonzini        if (cbl) {
700139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_SLTIU, TCG_REG_TMP0, al, bl);
701139c1837SPaolo Bonzini            tcg_out_opc_imm(s, opc_addi, rl, al, -bl);
702139c1837SPaolo Bonzini        } else {
703139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SLTU, TCG_REG_TMP0, al, bl);
704139c1837SPaolo Bonzini            tcg_out_opc_reg(s, opc_sub, rl, al, bl);
705139c1837SPaolo Bonzini        }
706139c1837SPaolo Bonzini        tcg_out_opc_reg(s, opc_sub, rh, th, TCG_REG_TMP0);
707139c1837SPaolo Bonzini    } else {
708139c1837SPaolo Bonzini        if (cbl) {
709139c1837SPaolo Bonzini            tcg_out_opc_imm(s, opc_addi, rl, al, bl);
710139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_SLTIU, TCG_REG_TMP0, rl, bl);
7119b246685SRichard Henderson        } else if (al == bl) {
7129b246685SRichard Henderson            /*
7139b246685SRichard Henderson             * If the input regs overlap, this is a simple doubling
7149b246685SRichard Henderson             * and carry-out is the input msb.  This special case is
7159b246685SRichard Henderson             * required when the output reg overlaps the input,
7169b246685SRichard Henderson             * but we might as well use it always.
7179b246685SRichard Henderson             */
718139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_SLTI, TCG_REG_TMP0, al, 0);
7199b246685SRichard Henderson            tcg_out_opc_reg(s, opc_add, rl, al, al);
720139c1837SPaolo Bonzini        } else {
721139c1837SPaolo Bonzini            tcg_out_opc_reg(s, opc_add, rl, al, bl);
722139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SLTU, TCG_REG_TMP0,
723139c1837SPaolo Bonzini                            rl, (rl == bl ? al : bl));
724139c1837SPaolo Bonzini        }
725139c1837SPaolo Bonzini        tcg_out_opc_reg(s, opc_add, rh, th, TCG_REG_TMP0);
726139c1837SPaolo Bonzini    }
727139c1837SPaolo Bonzini}
728139c1837SPaolo Bonzini
729139c1837SPaolo Bonzinistatic const struct {
730139c1837SPaolo Bonzini    RISCVInsn op;
731139c1837SPaolo Bonzini    bool swap;
732139c1837SPaolo Bonzini} tcg_brcond_to_riscv[] = {
733139c1837SPaolo Bonzini    [TCG_COND_EQ] =  { OPC_BEQ,  false },
734139c1837SPaolo Bonzini    [TCG_COND_NE] =  { OPC_BNE,  false },
735139c1837SPaolo Bonzini    [TCG_COND_LT] =  { OPC_BLT,  false },
736139c1837SPaolo Bonzini    [TCG_COND_GE] =  { OPC_BGE,  false },
737139c1837SPaolo Bonzini    [TCG_COND_LE] =  { OPC_BGE,  true  },
738139c1837SPaolo Bonzini    [TCG_COND_GT] =  { OPC_BLT,  true  },
739139c1837SPaolo Bonzini    [TCG_COND_LTU] = { OPC_BLTU, false },
740139c1837SPaolo Bonzini    [TCG_COND_GEU] = { OPC_BGEU, false },
741139c1837SPaolo Bonzini    [TCG_COND_LEU] = { OPC_BGEU, true  },
742139c1837SPaolo Bonzini    [TCG_COND_GTU] = { OPC_BLTU, true  }
743139c1837SPaolo Bonzini};
744139c1837SPaolo Bonzini
745139c1837SPaolo Bonzinistatic void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1,
746139c1837SPaolo Bonzini                           TCGReg arg2, TCGLabel *l)
747139c1837SPaolo Bonzini{
748139c1837SPaolo Bonzini    RISCVInsn op = tcg_brcond_to_riscv[cond].op;
749139c1837SPaolo Bonzini
750139c1837SPaolo Bonzini    tcg_debug_assert(op != 0);
751139c1837SPaolo Bonzini
752139c1837SPaolo Bonzini    if (tcg_brcond_to_riscv[cond].swap) {
753139c1837SPaolo Bonzini        TCGReg t = arg1;
754139c1837SPaolo Bonzini        arg1 = arg2;
755139c1837SPaolo Bonzini        arg2 = t;
756139c1837SPaolo Bonzini    }
757139c1837SPaolo Bonzini
758139c1837SPaolo Bonzini    tcg_out_reloc(s, s->code_ptr, R_RISCV_BRANCH, l, 0);
759139c1837SPaolo Bonzini    tcg_out_opc_branch(s, op, arg1, arg2, 0);
760139c1837SPaolo Bonzini}
761139c1837SPaolo Bonzini
762139c1837SPaolo Bonzinistatic void tcg_out_setcond(TCGContext *s, TCGCond cond, TCGReg ret,
763139c1837SPaolo Bonzini                            TCGReg arg1, TCGReg arg2)
764139c1837SPaolo Bonzini{
765139c1837SPaolo Bonzini    switch (cond) {
766139c1837SPaolo Bonzini    case TCG_COND_EQ:
767139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SUB, ret, arg1, arg2);
768139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_SLTIU, ret, ret, 1);
769139c1837SPaolo Bonzini        break;
770139c1837SPaolo Bonzini    case TCG_COND_NE:
771139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SUB, ret, arg1, arg2);
772139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLTU, ret, TCG_REG_ZERO, ret);
773139c1837SPaolo Bonzini        break;
774139c1837SPaolo Bonzini    case TCG_COND_LT:
775139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLT, ret, arg1, arg2);
776139c1837SPaolo Bonzini        break;
777139c1837SPaolo Bonzini    case TCG_COND_GE:
778139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLT, ret, arg1, arg2);
779139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1);
780139c1837SPaolo Bonzini        break;
781139c1837SPaolo Bonzini    case TCG_COND_LE:
782139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLT, ret, arg2, arg1);
783139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1);
784139c1837SPaolo Bonzini        break;
785139c1837SPaolo Bonzini    case TCG_COND_GT:
786139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLT, ret, arg2, arg1);
787139c1837SPaolo Bonzini        break;
788139c1837SPaolo Bonzini    case TCG_COND_LTU:
789139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLTU, ret, arg1, arg2);
790139c1837SPaolo Bonzini        break;
791139c1837SPaolo Bonzini    case TCG_COND_GEU:
792139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLTU, ret, arg1, arg2);
793139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1);
794139c1837SPaolo Bonzini        break;
795139c1837SPaolo Bonzini    case TCG_COND_LEU:
796139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLTU, ret, arg2, arg1);
797139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1);
798139c1837SPaolo Bonzini        break;
799139c1837SPaolo Bonzini    case TCG_COND_GTU:
800139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLTU, ret, arg2, arg1);
801139c1837SPaolo Bonzini        break;
802139c1837SPaolo Bonzini    default:
803139c1837SPaolo Bonzini         g_assert_not_reached();
804139c1837SPaolo Bonzini         break;
805139c1837SPaolo Bonzini     }
806139c1837SPaolo Bonzini}
807139c1837SPaolo Bonzini
808139c1837SPaolo Bonzinistatic void tcg_out_brcond2(TCGContext *s, TCGCond cond, TCGReg al, TCGReg ah,
809139c1837SPaolo Bonzini                            TCGReg bl, TCGReg bh, TCGLabel *l)
810139c1837SPaolo Bonzini{
811139c1837SPaolo Bonzini    /* todo */
812139c1837SPaolo Bonzini    g_assert_not_reached();
813139c1837SPaolo Bonzini}
814139c1837SPaolo Bonzini
815139c1837SPaolo Bonzinistatic void tcg_out_setcond2(TCGContext *s, TCGCond cond, TCGReg ret,
816139c1837SPaolo Bonzini                             TCGReg al, TCGReg ah, TCGReg bl, TCGReg bh)
817139c1837SPaolo Bonzini{
818139c1837SPaolo Bonzini    /* todo */
819139c1837SPaolo Bonzini    g_assert_not_reached();
820139c1837SPaolo Bonzini}
821139c1837SPaolo Bonzini
8222be7d76bSRichard Hendersonstatic void tcg_out_call_int(TCGContext *s, const tcg_insn_unit *arg, bool tail)
823139c1837SPaolo Bonzini{
824139c1837SPaolo Bonzini    TCGReg link = tail ? TCG_REG_ZERO : TCG_REG_RA;
825139c1837SPaolo Bonzini    ptrdiff_t offset = tcg_pcrel_diff(s, arg);
826139c1837SPaolo Bonzini    int ret;
827139c1837SPaolo Bonzini
828844d0442SRichard Henderson    tcg_debug_assert((offset & 1) == 0);
829844d0442SRichard Henderson    if (offset == sextreg(offset, 0, 20)) {
830139c1837SPaolo Bonzini        /* short jump: -2097150 to 2097152 */
831139c1837SPaolo Bonzini        tcg_out_opc_jump(s, OPC_JAL, link, offset);
832844d0442SRichard Henderson    } else if (TCG_TARGET_REG_BITS == 32 || offset == (int32_t)offset) {
833139c1837SPaolo Bonzini        /* long jump: -2147483646 to 2147483648 */
834139c1837SPaolo Bonzini        tcg_out_opc_upper(s, OPC_AUIPC, TCG_REG_TMP0, 0);
835139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_JALR, link, TCG_REG_TMP0, 0);
836844d0442SRichard Henderson        ret = reloc_call(s->code_ptr - 2, arg);
837139c1837SPaolo Bonzini        tcg_debug_assert(ret == true);
838139c1837SPaolo Bonzini    } else if (TCG_TARGET_REG_BITS == 64) {
839139c1837SPaolo Bonzini        /* far jump: 64-bit */
840139c1837SPaolo Bonzini        tcg_target_long imm = sextreg((tcg_target_long)arg, 0, 12);
841139c1837SPaolo Bonzini        tcg_target_long base = (tcg_target_long)arg - imm;
842139c1837SPaolo Bonzini        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP0, base);
843139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_JALR, link, TCG_REG_TMP0, imm);
844139c1837SPaolo Bonzini    } else {
845139c1837SPaolo Bonzini        g_assert_not_reached();
846139c1837SPaolo Bonzini    }
847139c1837SPaolo Bonzini}
848139c1837SPaolo Bonzini
849cee44b03SRichard Hendersonstatic void tcg_out_call(TCGContext *s, const tcg_insn_unit *arg,
850cee44b03SRichard Henderson                         const TCGHelperInfo *info)
851139c1837SPaolo Bonzini{
852139c1837SPaolo Bonzini    tcg_out_call_int(s, arg, false);
853139c1837SPaolo Bonzini}
854139c1837SPaolo Bonzini
855139c1837SPaolo Bonzinistatic void tcg_out_mb(TCGContext *s, TCGArg a0)
856139c1837SPaolo Bonzini{
857139c1837SPaolo Bonzini    tcg_insn_unit insn = OPC_FENCE;
858139c1837SPaolo Bonzini
859139c1837SPaolo Bonzini    if (a0 & TCG_MO_LD_LD) {
860139c1837SPaolo Bonzini        insn |= 0x02200000;
861139c1837SPaolo Bonzini    }
862139c1837SPaolo Bonzini    if (a0 & TCG_MO_ST_LD) {
863139c1837SPaolo Bonzini        insn |= 0x01200000;
864139c1837SPaolo Bonzini    }
865139c1837SPaolo Bonzini    if (a0 & TCG_MO_LD_ST) {
866139c1837SPaolo Bonzini        insn |= 0x02100000;
867139c1837SPaolo Bonzini    }
868139c1837SPaolo Bonzini    if (a0 & TCG_MO_ST_ST) {
869139c1837SPaolo Bonzini        insn |= 0x02200000;
870139c1837SPaolo Bonzini    }
871139c1837SPaolo Bonzini    tcg_out32(s, insn);
872139c1837SPaolo Bonzini}
873139c1837SPaolo Bonzini
874139c1837SPaolo Bonzini/*
875139c1837SPaolo Bonzini * Load/store and TLB
876139c1837SPaolo Bonzini */
877139c1837SPaolo Bonzini
878139c1837SPaolo Bonzini#if defined(CONFIG_SOFTMMU)
879139c1837SPaolo Bonzini/* helper signature: helper_ret_ld_mmu(CPUState *env, target_ulong addr,
8809002ffcbSRichard Henderson *                                     MemOpIdx oi, uintptr_t ra)
881139c1837SPaolo Bonzini */
8824b473e0cSRichard Hendersonstatic void * const qemu_ld_helpers[MO_SSIZE + 1] = {
883139c1837SPaolo Bonzini    [MO_UB] = helper_ret_ldub_mmu,
884139c1837SPaolo Bonzini    [MO_SB] = helper_ret_ldsb_mmu,
885e03b5686SMarc-André Lureau#if HOST_BIG_ENDIAN
886c86bd2dcSRichard Henderson    [MO_UW] = helper_be_lduw_mmu,
887c86bd2dcSRichard Henderson    [MO_SW] = helper_be_ldsw_mmu,
888c86bd2dcSRichard Henderson    [MO_UL] = helper_be_ldul_mmu,
889139c1837SPaolo Bonzini#if TCG_TARGET_REG_BITS == 64
890c86bd2dcSRichard Henderson    [MO_SL] = helper_be_ldsl_mmu,
891139c1837SPaolo Bonzini#endif
892fc313c64SFrédéric Pétrot    [MO_UQ] = helper_be_ldq_mmu,
893c86bd2dcSRichard Henderson#else
894c86bd2dcSRichard Henderson    [MO_UW] = helper_le_lduw_mmu,
895c86bd2dcSRichard Henderson    [MO_SW] = helper_le_ldsw_mmu,
896c86bd2dcSRichard Henderson    [MO_UL] = helper_le_ldul_mmu,
897139c1837SPaolo Bonzini#if TCG_TARGET_REG_BITS == 64
898c86bd2dcSRichard Henderson    [MO_SL] = helper_le_ldsl_mmu,
899139c1837SPaolo Bonzini#endif
900fc313c64SFrédéric Pétrot    [MO_UQ] = helper_le_ldq_mmu,
901c86bd2dcSRichard Henderson#endif
902139c1837SPaolo Bonzini};
903139c1837SPaolo Bonzini
904139c1837SPaolo Bonzini/* helper signature: helper_ret_st_mmu(CPUState *env, target_ulong addr,
9059002ffcbSRichard Henderson *                                     uintxx_t val, MemOpIdx oi,
906139c1837SPaolo Bonzini *                                     uintptr_t ra)
907139c1837SPaolo Bonzini */
9084b473e0cSRichard Hendersonstatic void * const qemu_st_helpers[MO_SIZE + 1] = {
909c86bd2dcSRichard Henderson    [MO_8]   = helper_ret_stb_mmu,
910e03b5686SMarc-André Lureau#if HOST_BIG_ENDIAN
911c86bd2dcSRichard Henderson    [MO_16] = helper_be_stw_mmu,
912c86bd2dcSRichard Henderson    [MO_32] = helper_be_stl_mmu,
913c86bd2dcSRichard Henderson    [MO_64] = helper_be_stq_mmu,
914c86bd2dcSRichard Henderson#else
915c86bd2dcSRichard Henderson    [MO_16] = helper_le_stw_mmu,
916c86bd2dcSRichard Henderson    [MO_32] = helper_le_stl_mmu,
917c86bd2dcSRichard Henderson    [MO_64] = helper_le_stq_mmu,
918c86bd2dcSRichard Henderson#endif
919139c1837SPaolo Bonzini};
920139c1837SPaolo Bonzini
921139c1837SPaolo Bonzini/* We don't support oversize guests */
922139c1837SPaolo BonziniQEMU_BUILD_BUG_ON(TCG_TARGET_REG_BITS < TARGET_LONG_BITS);
923139c1837SPaolo Bonzini
924139c1837SPaolo Bonzini/* We expect to use a 12-bit negative offset from ENV.  */
925139c1837SPaolo BonziniQEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) > 0);
926139c1837SPaolo BonziniQEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) < -(1 << 11));
927139c1837SPaolo Bonzini
928793f7381SRichard Hendersonstatic void tcg_out_goto(TCGContext *s, const tcg_insn_unit *target)
929844d0442SRichard Henderson{
930844d0442SRichard Henderson    tcg_out_opc_jump(s, OPC_JAL, TCG_REG_ZERO, 0);
931844d0442SRichard Henderson    bool ok = reloc_jimm20(s->code_ptr - 1, target);
932844d0442SRichard Henderson    tcg_debug_assert(ok);
933844d0442SRichard Henderson}
934844d0442SRichard Henderson
9352e3a933aSRichard Hendersonstatic TCGReg tcg_out_tlb_load(TCGContext *s, TCGReg addrl,
9369002ffcbSRichard Henderson                               TCGReg addrh, MemOpIdx oi,
937139c1837SPaolo Bonzini                               tcg_insn_unit **label_ptr, bool is_load)
938139c1837SPaolo Bonzini{
939139c1837SPaolo Bonzini    MemOp opc = get_memop(oi);
940139c1837SPaolo Bonzini    unsigned s_bits = opc & MO_SIZE;
941139c1837SPaolo Bonzini    unsigned a_bits = get_alignment_bits(opc);
942139c1837SPaolo Bonzini    tcg_target_long compare_mask;
943139c1837SPaolo Bonzini    int mem_index = get_mmuidx(oi);
944139c1837SPaolo Bonzini    int fast_ofs = TLB_MASK_TABLE_OFS(mem_index);
945139c1837SPaolo Bonzini    int mask_ofs = fast_ofs + offsetof(CPUTLBDescFast, mask);
946139c1837SPaolo Bonzini    int table_ofs = fast_ofs + offsetof(CPUTLBDescFast, table);
947139c1837SPaolo Bonzini    TCGReg mask_base = TCG_AREG0, table_base = TCG_AREG0;
948139c1837SPaolo Bonzini
949139c1837SPaolo Bonzini    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP0, mask_base, mask_ofs);
950139c1837SPaolo Bonzini    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP1, table_base, table_ofs);
951139c1837SPaolo Bonzini
952139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SRLI, TCG_REG_TMP2, addrl,
953139c1837SPaolo Bonzini                    TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS);
954139c1837SPaolo Bonzini    tcg_out_opc_reg(s, OPC_AND, TCG_REG_TMP2, TCG_REG_TMP2, TCG_REG_TMP0);
955139c1837SPaolo Bonzini    tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP2, TCG_REG_TMP2, TCG_REG_TMP1);
956139c1837SPaolo Bonzini
957139c1837SPaolo Bonzini    /* Load the tlb comparator and the addend.  */
958139c1837SPaolo Bonzini    tcg_out_ld(s, TCG_TYPE_TL, TCG_REG_TMP0, TCG_REG_TMP2,
959139c1837SPaolo Bonzini               is_load ? offsetof(CPUTLBEntry, addr_read)
960139c1837SPaolo Bonzini               : offsetof(CPUTLBEntry, addr_write));
961139c1837SPaolo Bonzini    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP2, TCG_REG_TMP2,
962139c1837SPaolo Bonzini               offsetof(CPUTLBEntry, addend));
963139c1837SPaolo Bonzini
964139c1837SPaolo Bonzini    /* We don't support unaligned accesses. */
965139c1837SPaolo Bonzini    if (a_bits < s_bits) {
966139c1837SPaolo Bonzini        a_bits = s_bits;
967139c1837SPaolo Bonzini    }
968139c1837SPaolo Bonzini    /* Clear the non-page, non-alignment bits from the address.  */
969139c1837SPaolo Bonzini    compare_mask = (tcg_target_long)TARGET_PAGE_MASK | ((1 << a_bits) - 1);
970139c1837SPaolo Bonzini    if (compare_mask == sextreg(compare_mask, 0, 12)) {
971139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_ANDI, TCG_REG_TMP1, addrl, compare_mask);
972139c1837SPaolo Bonzini    } else {
973139c1837SPaolo Bonzini        tcg_out_movi(s, TCG_TYPE_TL, TCG_REG_TMP1, compare_mask);
974139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_AND, TCG_REG_TMP1, TCG_REG_TMP1, addrl);
975139c1837SPaolo Bonzini    }
976139c1837SPaolo Bonzini
977139c1837SPaolo Bonzini    /* Compare masked address with the TLB entry. */
978139c1837SPaolo Bonzini    label_ptr[0] = s->code_ptr;
979139c1837SPaolo Bonzini    tcg_out_opc_branch(s, OPC_BNE, TCG_REG_TMP0, TCG_REG_TMP1, 0);
980139c1837SPaolo Bonzini
981139c1837SPaolo Bonzini    /* TLB Hit - translate address using addend.  */
982139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS > TARGET_LONG_BITS) {
983139c1837SPaolo Bonzini        tcg_out_ext32u(s, TCG_REG_TMP0, addrl);
984139c1837SPaolo Bonzini        addrl = TCG_REG_TMP0;
985139c1837SPaolo Bonzini    }
986139c1837SPaolo Bonzini    tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP0, TCG_REG_TMP2, addrl);
9872e3a933aSRichard Henderson    return TCG_REG_TMP0;
988139c1837SPaolo Bonzini}
989139c1837SPaolo Bonzini
9909002ffcbSRichard Hendersonstatic void add_qemu_ldst_label(TCGContext *s, int is_ld, MemOpIdx oi,
991139c1837SPaolo Bonzini                                TCGType ext,
992139c1837SPaolo Bonzini                                TCGReg datalo, TCGReg datahi,
993139c1837SPaolo Bonzini                                TCGReg addrlo, TCGReg addrhi,
994139c1837SPaolo Bonzini                                void *raddr, tcg_insn_unit **label_ptr)
995139c1837SPaolo Bonzini{
996139c1837SPaolo Bonzini    TCGLabelQemuLdst *label = new_ldst_label(s);
997139c1837SPaolo Bonzini
998139c1837SPaolo Bonzini    label->is_ld = is_ld;
999139c1837SPaolo Bonzini    label->oi = oi;
1000139c1837SPaolo Bonzini    label->type = ext;
1001139c1837SPaolo Bonzini    label->datalo_reg = datalo;
1002139c1837SPaolo Bonzini    label->datahi_reg = datahi;
1003139c1837SPaolo Bonzini    label->addrlo_reg = addrlo;
1004139c1837SPaolo Bonzini    label->addrhi_reg = addrhi;
1005e5e2e4c7SRichard Henderson    label->raddr = tcg_splitwx_to_rx(raddr);
1006139c1837SPaolo Bonzini    label->label_ptr[0] = label_ptr[0];
1007139c1837SPaolo Bonzini}
1008139c1837SPaolo Bonzini
1009139c1837SPaolo Bonzinistatic bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
1010139c1837SPaolo Bonzini{
10119002ffcbSRichard Henderson    MemOpIdx oi = l->oi;
1012139c1837SPaolo Bonzini    MemOp opc = get_memop(oi);
1013139c1837SPaolo Bonzini    TCGReg a0 = tcg_target_call_iarg_regs[0];
1014139c1837SPaolo Bonzini    TCGReg a1 = tcg_target_call_iarg_regs[1];
1015139c1837SPaolo Bonzini    TCGReg a2 = tcg_target_call_iarg_regs[2];
1016139c1837SPaolo Bonzini    TCGReg a3 = tcg_target_call_iarg_regs[3];
1017139c1837SPaolo Bonzini
1018139c1837SPaolo Bonzini    /* We don't support oversize guests */
1019139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS < TARGET_LONG_BITS) {
1020139c1837SPaolo Bonzini        g_assert_not_reached();
1021139c1837SPaolo Bonzini    }
1022139c1837SPaolo Bonzini
1023139c1837SPaolo Bonzini    /* resolve label address */
1024793f7381SRichard Henderson    if (!reloc_sbimm12(l->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
1025139c1837SPaolo Bonzini        return false;
1026139c1837SPaolo Bonzini    }
1027139c1837SPaolo Bonzini
1028139c1837SPaolo Bonzini    /* call load helper */
1029139c1837SPaolo Bonzini    tcg_out_mov(s, TCG_TYPE_PTR, a0, TCG_AREG0);
1030139c1837SPaolo Bonzini    tcg_out_mov(s, TCG_TYPE_PTR, a1, l->addrlo_reg);
1031139c1837SPaolo Bonzini    tcg_out_movi(s, TCG_TYPE_PTR, a2, oi);
1032139c1837SPaolo Bonzini    tcg_out_movi(s, TCG_TYPE_PTR, a3, (tcg_target_long)l->raddr);
1033139c1837SPaolo Bonzini
1034cee44b03SRichard Henderson    tcg_out_call_int(s, qemu_ld_helpers[opc & MO_SSIZE], false);
1035139c1837SPaolo Bonzini    tcg_out_mov(s, (opc & MO_SIZE) == MO_64, l->datalo_reg, a0);
1036139c1837SPaolo Bonzini
1037139c1837SPaolo Bonzini    tcg_out_goto(s, l->raddr);
1038139c1837SPaolo Bonzini    return true;
1039139c1837SPaolo Bonzini}
1040139c1837SPaolo Bonzini
1041139c1837SPaolo Bonzinistatic bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
1042139c1837SPaolo Bonzini{
10439002ffcbSRichard Henderson    MemOpIdx oi = l->oi;
1044139c1837SPaolo Bonzini    MemOp opc = get_memop(oi);
1045139c1837SPaolo Bonzini    MemOp s_bits = opc & MO_SIZE;
1046139c1837SPaolo Bonzini    TCGReg a0 = tcg_target_call_iarg_regs[0];
1047139c1837SPaolo Bonzini    TCGReg a1 = tcg_target_call_iarg_regs[1];
1048139c1837SPaolo Bonzini    TCGReg a2 = tcg_target_call_iarg_regs[2];
1049139c1837SPaolo Bonzini    TCGReg a3 = tcg_target_call_iarg_regs[3];
1050139c1837SPaolo Bonzini    TCGReg a4 = tcg_target_call_iarg_regs[4];
1051139c1837SPaolo Bonzini
1052139c1837SPaolo Bonzini    /* We don't support oversize guests */
1053139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS < TARGET_LONG_BITS) {
1054139c1837SPaolo Bonzini        g_assert_not_reached();
1055139c1837SPaolo Bonzini    }
1056139c1837SPaolo Bonzini
1057139c1837SPaolo Bonzini    /* resolve label address */
1058793f7381SRichard Henderson    if (!reloc_sbimm12(l->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
1059139c1837SPaolo Bonzini        return false;
1060139c1837SPaolo Bonzini    }
1061139c1837SPaolo Bonzini
1062139c1837SPaolo Bonzini    /* call store helper */
1063139c1837SPaolo Bonzini    tcg_out_mov(s, TCG_TYPE_PTR, a0, TCG_AREG0);
1064139c1837SPaolo Bonzini    tcg_out_mov(s, TCG_TYPE_PTR, a1, l->addrlo_reg);
1065139c1837SPaolo Bonzini    tcg_out_mov(s, TCG_TYPE_PTR, a2, l->datalo_reg);
1066139c1837SPaolo Bonzini    switch (s_bits) {
1067139c1837SPaolo Bonzini    case MO_8:
1068139c1837SPaolo Bonzini        tcg_out_ext8u(s, a2, a2);
1069139c1837SPaolo Bonzini        break;
1070139c1837SPaolo Bonzini    case MO_16:
1071139c1837SPaolo Bonzini        tcg_out_ext16u(s, a2, a2);
1072139c1837SPaolo Bonzini        break;
1073139c1837SPaolo Bonzini    default:
1074139c1837SPaolo Bonzini        break;
1075139c1837SPaolo Bonzini    }
1076139c1837SPaolo Bonzini    tcg_out_movi(s, TCG_TYPE_PTR, a3, oi);
1077139c1837SPaolo Bonzini    tcg_out_movi(s, TCG_TYPE_PTR, a4, (tcg_target_long)l->raddr);
1078139c1837SPaolo Bonzini
1079cee44b03SRichard Henderson    tcg_out_call_int(s, qemu_st_helpers[opc & MO_SIZE], false);
1080139c1837SPaolo Bonzini
1081139c1837SPaolo Bonzini    tcg_out_goto(s, l->raddr);
1082139c1837SPaolo Bonzini    return true;
1083139c1837SPaolo Bonzini}
1084a3fb7c99SRichard Henderson#else
1085a3fb7c99SRichard Henderson
1086a3fb7c99SRichard Hendersonstatic void tcg_out_test_alignment(TCGContext *s, bool is_ld, TCGReg addr_reg,
1087a3fb7c99SRichard Henderson                                   unsigned a_bits)
1088a3fb7c99SRichard Henderson{
1089a3fb7c99SRichard Henderson    unsigned a_mask = (1 << a_bits) - 1;
1090a3fb7c99SRichard Henderson    TCGLabelQemuLdst *l = new_ldst_label(s);
1091a3fb7c99SRichard Henderson
1092a3fb7c99SRichard Henderson    l->is_ld = is_ld;
1093a3fb7c99SRichard Henderson    l->addrlo_reg = addr_reg;
1094a3fb7c99SRichard Henderson
1095a3fb7c99SRichard Henderson    /* We are expecting a_bits to max out at 7, so we can always use andi. */
1096a3fb7c99SRichard Henderson    tcg_debug_assert(a_bits < 12);
1097a3fb7c99SRichard Henderson    tcg_out_opc_imm(s, OPC_ANDI, TCG_REG_TMP1, addr_reg, a_mask);
1098a3fb7c99SRichard Henderson
1099a3fb7c99SRichard Henderson    l->label_ptr[0] = s->code_ptr;
1100a3fb7c99SRichard Henderson    tcg_out_opc_branch(s, OPC_BNE, TCG_REG_TMP1, TCG_REG_ZERO, 0);
1101a3fb7c99SRichard Henderson
1102a3fb7c99SRichard Henderson    l->raddr = tcg_splitwx_to_rx(s->code_ptr);
1103a3fb7c99SRichard Henderson}
1104a3fb7c99SRichard Henderson
1105a3fb7c99SRichard Hendersonstatic bool tcg_out_fail_alignment(TCGContext *s, TCGLabelQemuLdst *l)
1106a3fb7c99SRichard Henderson{
1107a3fb7c99SRichard Henderson    /* resolve label address */
1108a3fb7c99SRichard Henderson    if (!reloc_sbimm12(l->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
1109a3fb7c99SRichard Henderson        return false;
1110a3fb7c99SRichard Henderson    }
1111a3fb7c99SRichard Henderson
1112a3fb7c99SRichard Henderson    tcg_out_mov(s, TCG_TYPE_TL, TCG_REG_A1, l->addrlo_reg);
1113a3fb7c99SRichard Henderson    tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_A0, TCG_AREG0);
1114a3fb7c99SRichard Henderson
1115a3fb7c99SRichard Henderson    /* tail call, with the return address back inline. */
1116a3fb7c99SRichard Henderson    tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_RA, (uintptr_t)l->raddr);
1117a3fb7c99SRichard Henderson    tcg_out_call_int(s, (const void *)(l->is_ld ? helper_unaligned_ld
1118a3fb7c99SRichard Henderson                                       : helper_unaligned_st), true);
1119a3fb7c99SRichard Henderson    return true;
1120a3fb7c99SRichard Henderson}
1121a3fb7c99SRichard Henderson
1122a3fb7c99SRichard Hendersonstatic bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
1123a3fb7c99SRichard Henderson{
1124a3fb7c99SRichard Henderson    return tcg_out_fail_alignment(s, l);
1125a3fb7c99SRichard Henderson}
1126a3fb7c99SRichard Henderson
1127a3fb7c99SRichard Hendersonstatic bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
1128a3fb7c99SRichard Henderson{
1129a3fb7c99SRichard Henderson    return tcg_out_fail_alignment(s, l);
1130a3fb7c99SRichard Henderson}
1131a3fb7c99SRichard Henderson
1132139c1837SPaolo Bonzini#endif /* CONFIG_SOFTMMU */
1133139c1837SPaolo Bonzini
1134139c1837SPaolo Bonzinistatic void tcg_out_qemu_ld_direct(TCGContext *s, TCGReg lo, TCGReg hi,
1135139c1837SPaolo Bonzini                                   TCGReg base, MemOp opc, bool is_64)
1136139c1837SPaolo Bonzini{
1137c86bd2dcSRichard Henderson    /* Byte swapping is left to middle-end expansion. */
1138c86bd2dcSRichard Henderson    tcg_debug_assert((opc & MO_BSWAP) == 0);
1139139c1837SPaolo Bonzini
1140139c1837SPaolo Bonzini    switch (opc & (MO_SSIZE)) {
1141139c1837SPaolo Bonzini    case MO_UB:
1142139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_LBU, lo, base, 0);
1143139c1837SPaolo Bonzini        break;
1144139c1837SPaolo Bonzini    case MO_SB:
1145139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_LB, lo, base, 0);
1146139c1837SPaolo Bonzini        break;
1147139c1837SPaolo Bonzini    case MO_UW:
1148139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_LHU, lo, base, 0);
1149139c1837SPaolo Bonzini        break;
1150139c1837SPaolo Bonzini    case MO_SW:
1151139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_LH, lo, base, 0);
1152139c1837SPaolo Bonzini        break;
1153139c1837SPaolo Bonzini    case MO_UL:
1154139c1837SPaolo Bonzini        if (TCG_TARGET_REG_BITS == 64 && is_64) {
1155139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_LWU, lo, base, 0);
1156139c1837SPaolo Bonzini            break;
1157139c1837SPaolo Bonzini        }
1158139c1837SPaolo Bonzini        /* FALLTHRU */
1159139c1837SPaolo Bonzini    case MO_SL:
1160139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_LW, lo, base, 0);
1161139c1837SPaolo Bonzini        break;
1162fc313c64SFrédéric Pétrot    case MO_UQ:
1163139c1837SPaolo Bonzini        /* Prefer to load from offset 0 first, but allow for overlap.  */
1164139c1837SPaolo Bonzini        if (TCG_TARGET_REG_BITS == 64) {
1165139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_LD, lo, base, 0);
1166139c1837SPaolo Bonzini        } else if (lo != base) {
1167139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_LW, lo, base, 0);
1168139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_LW, hi, base, 4);
1169139c1837SPaolo Bonzini        } else {
1170139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_LW, hi, base, 4);
1171139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_LW, lo, base, 0);
1172139c1837SPaolo Bonzini        }
1173139c1837SPaolo Bonzini        break;
1174139c1837SPaolo Bonzini    default:
1175139c1837SPaolo Bonzini        g_assert_not_reached();
1176139c1837SPaolo Bonzini    }
1177139c1837SPaolo Bonzini}
1178139c1837SPaolo Bonzini
1179139c1837SPaolo Bonzinistatic void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args, bool is_64)
1180139c1837SPaolo Bonzini{
1181139c1837SPaolo Bonzini    TCGReg addr_regl, addr_regh __attribute__((unused));
1182139c1837SPaolo Bonzini    TCGReg data_regl, data_regh;
11839002ffcbSRichard Henderson    MemOpIdx oi;
1184139c1837SPaolo Bonzini    MemOp opc;
1185139c1837SPaolo Bonzini#if defined(CONFIG_SOFTMMU)
1186139c1837SPaolo Bonzini    tcg_insn_unit *label_ptr[1];
1187a3fb7c99SRichard Henderson#else
1188a3fb7c99SRichard Henderson    unsigned a_bits;
1189139c1837SPaolo Bonzini#endif
11902e3a933aSRichard Henderson    TCGReg base;
1191139c1837SPaolo Bonzini
1192139c1837SPaolo Bonzini    data_regl = *args++;
1193139c1837SPaolo Bonzini    data_regh = (TCG_TARGET_REG_BITS == 32 && is_64 ? *args++ : 0);
1194139c1837SPaolo Bonzini    addr_regl = *args++;
1195139c1837SPaolo Bonzini    addr_regh = (TCG_TARGET_REG_BITS < TARGET_LONG_BITS ? *args++ : 0);
1196139c1837SPaolo Bonzini    oi = *args++;
1197139c1837SPaolo Bonzini    opc = get_memop(oi);
1198139c1837SPaolo Bonzini
1199139c1837SPaolo Bonzini#if defined(CONFIG_SOFTMMU)
12002e3a933aSRichard Henderson    base = tcg_out_tlb_load(s, addr_regl, addr_regh, oi, label_ptr, 1);
1201139c1837SPaolo Bonzini    tcg_out_qemu_ld_direct(s, data_regl, data_regh, base, opc, is_64);
1202139c1837SPaolo Bonzini    add_qemu_ldst_label(s, 1, oi,
1203139c1837SPaolo Bonzini                        (is_64 ? TCG_TYPE_I64 : TCG_TYPE_I32),
1204139c1837SPaolo Bonzini                        data_regl, data_regh, addr_regl, addr_regh,
1205139c1837SPaolo Bonzini                        s->code_ptr, label_ptr);
1206139c1837SPaolo Bonzini#else
1207a3fb7c99SRichard Henderson    a_bits = get_alignment_bits(opc);
1208a3fb7c99SRichard Henderson    if (a_bits) {
1209a3fb7c99SRichard Henderson        tcg_out_test_alignment(s, true, addr_regl, a_bits);
1210a3fb7c99SRichard Henderson    }
12112e3a933aSRichard Henderson    base = addr_regl;
12122e3a933aSRichard Henderson    if (TCG_TARGET_REG_BITS > TARGET_LONG_BITS) {
12132e3a933aSRichard Henderson        tcg_out_ext32u(s, TCG_REG_TMP0, base);
12142e3a933aSRichard Henderson        base = TCG_REG_TMP0;
12152e3a933aSRichard Henderson    }
121681c65ee2SRichard Henderson    if (guest_base != 0) {
12172e3a933aSRichard Henderson        tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP0, TCG_GUEST_BASE_REG, base);
12182e3a933aSRichard Henderson        base = TCG_REG_TMP0;
1219139c1837SPaolo Bonzini    }
1220139c1837SPaolo Bonzini    tcg_out_qemu_ld_direct(s, data_regl, data_regh, base, opc, is_64);
1221139c1837SPaolo Bonzini#endif
1222139c1837SPaolo Bonzini}
1223139c1837SPaolo Bonzini
1224139c1837SPaolo Bonzinistatic void tcg_out_qemu_st_direct(TCGContext *s, TCGReg lo, TCGReg hi,
1225139c1837SPaolo Bonzini                                   TCGReg base, MemOp opc)
1226139c1837SPaolo Bonzini{
1227c86bd2dcSRichard Henderson    /* Byte swapping is left to middle-end expansion. */
1228c86bd2dcSRichard Henderson    tcg_debug_assert((opc & MO_BSWAP) == 0);
1229139c1837SPaolo Bonzini
1230139c1837SPaolo Bonzini    switch (opc & (MO_SSIZE)) {
1231139c1837SPaolo Bonzini    case MO_8:
1232139c1837SPaolo Bonzini        tcg_out_opc_store(s, OPC_SB, base, lo, 0);
1233139c1837SPaolo Bonzini        break;
1234139c1837SPaolo Bonzini    case MO_16:
1235139c1837SPaolo Bonzini        tcg_out_opc_store(s, OPC_SH, base, lo, 0);
1236139c1837SPaolo Bonzini        break;
1237139c1837SPaolo Bonzini    case MO_32:
1238139c1837SPaolo Bonzini        tcg_out_opc_store(s, OPC_SW, base, lo, 0);
1239139c1837SPaolo Bonzini        break;
1240139c1837SPaolo Bonzini    case MO_64:
1241139c1837SPaolo Bonzini        if (TCG_TARGET_REG_BITS == 64) {
1242139c1837SPaolo Bonzini            tcg_out_opc_store(s, OPC_SD, base, lo, 0);
1243139c1837SPaolo Bonzini        } else {
1244139c1837SPaolo Bonzini            tcg_out_opc_store(s, OPC_SW, base, lo, 0);
1245139c1837SPaolo Bonzini            tcg_out_opc_store(s, OPC_SW, base, hi, 4);
1246139c1837SPaolo Bonzini        }
1247139c1837SPaolo Bonzini        break;
1248139c1837SPaolo Bonzini    default:
1249139c1837SPaolo Bonzini        g_assert_not_reached();
1250139c1837SPaolo Bonzini    }
1251139c1837SPaolo Bonzini}
1252139c1837SPaolo Bonzini
1253139c1837SPaolo Bonzinistatic void tcg_out_qemu_st(TCGContext *s, const TCGArg *args, bool is_64)
1254139c1837SPaolo Bonzini{
1255139c1837SPaolo Bonzini    TCGReg addr_regl, addr_regh __attribute__((unused));
1256139c1837SPaolo Bonzini    TCGReg data_regl, data_regh;
12579002ffcbSRichard Henderson    MemOpIdx oi;
1258139c1837SPaolo Bonzini    MemOp opc;
1259139c1837SPaolo Bonzini#if defined(CONFIG_SOFTMMU)
1260139c1837SPaolo Bonzini    tcg_insn_unit *label_ptr[1];
1261a3fb7c99SRichard Henderson#else
1262a3fb7c99SRichard Henderson    unsigned a_bits;
1263139c1837SPaolo Bonzini#endif
12642e3a933aSRichard Henderson    TCGReg base;
1265139c1837SPaolo Bonzini
1266139c1837SPaolo Bonzini    data_regl = *args++;
1267139c1837SPaolo Bonzini    data_regh = (TCG_TARGET_REG_BITS == 32 && is_64 ? *args++ : 0);
1268139c1837SPaolo Bonzini    addr_regl = *args++;
1269139c1837SPaolo Bonzini    addr_regh = (TCG_TARGET_REG_BITS < TARGET_LONG_BITS ? *args++ : 0);
1270139c1837SPaolo Bonzini    oi = *args++;
1271139c1837SPaolo Bonzini    opc = get_memop(oi);
1272139c1837SPaolo Bonzini
1273139c1837SPaolo Bonzini#if defined(CONFIG_SOFTMMU)
12742e3a933aSRichard Henderson    base = tcg_out_tlb_load(s, addr_regl, addr_regh, oi, label_ptr, 0);
1275139c1837SPaolo Bonzini    tcg_out_qemu_st_direct(s, data_regl, data_regh, base, opc);
1276139c1837SPaolo Bonzini    add_qemu_ldst_label(s, 0, oi,
1277139c1837SPaolo Bonzini                        (is_64 ? TCG_TYPE_I64 : TCG_TYPE_I32),
1278139c1837SPaolo Bonzini                        data_regl, data_regh, addr_regl, addr_regh,
1279139c1837SPaolo Bonzini                        s->code_ptr, label_ptr);
1280139c1837SPaolo Bonzini#else
1281a3fb7c99SRichard Henderson    a_bits = get_alignment_bits(opc);
1282a3fb7c99SRichard Henderson    if (a_bits) {
1283a3fb7c99SRichard Henderson        tcg_out_test_alignment(s, false, addr_regl, a_bits);
1284a3fb7c99SRichard Henderson    }
12852e3a933aSRichard Henderson    base = addr_regl;
12862e3a933aSRichard Henderson    if (TCG_TARGET_REG_BITS > TARGET_LONG_BITS) {
12872e3a933aSRichard Henderson        tcg_out_ext32u(s, TCG_REG_TMP0, base);
12882e3a933aSRichard Henderson        base = TCG_REG_TMP0;
12892e3a933aSRichard Henderson    }
129081c65ee2SRichard Henderson    if (guest_base != 0) {
12912e3a933aSRichard Henderson        tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP0, TCG_GUEST_BASE_REG, base);
12922e3a933aSRichard Henderson        base = TCG_REG_TMP0;
1293139c1837SPaolo Bonzini    }
1294139c1837SPaolo Bonzini    tcg_out_qemu_st_direct(s, data_regl, data_regh, base, opc);
1295139c1837SPaolo Bonzini#endif
1296139c1837SPaolo Bonzini}
1297139c1837SPaolo Bonzini
1298793f7381SRichard Hendersonstatic const tcg_insn_unit *tb_ret_addr;
1299139c1837SPaolo Bonzini
1300b55a8d9dSRichard Hendersonstatic void tcg_out_exit_tb(TCGContext *s, uintptr_t a0)
1301b55a8d9dSRichard Henderson{
1302b55a8d9dSRichard Henderson    /* Reuse the zeroing that exists for goto_ptr.  */
1303b55a8d9dSRichard Henderson    if (a0 == 0) {
1304b55a8d9dSRichard Henderson        tcg_out_call_int(s, tcg_code_gen_epilogue, true);
1305b55a8d9dSRichard Henderson    } else {
1306b55a8d9dSRichard Henderson        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_A0, a0);
1307b55a8d9dSRichard Henderson        tcg_out_call_int(s, tb_ret_addr, true);
1308b55a8d9dSRichard Henderson    }
1309b55a8d9dSRichard Henderson}
1310b55a8d9dSRichard Henderson
1311cf7d6b8eSRichard Hendersonstatic void tcg_out_goto_tb(TCGContext *s, int which)
1312cf7d6b8eSRichard Henderson{
1313493c9b19SRichard Henderson    /* Direct branch will be patched by tb_target_set_jmp_target. */
1314493c9b19SRichard Henderson    set_jmp_insn_offset(s, which);
1315493c9b19SRichard Henderson    tcg_out32(s, OPC_JAL);
1316493c9b19SRichard Henderson
1317493c9b19SRichard Henderson    /* When branch is out of range, fall through to indirect. */
1318cf7d6b8eSRichard Henderson    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP0, TCG_REG_ZERO,
1319cf7d6b8eSRichard Henderson               get_jmp_target_addr(s, which));
1320cf7d6b8eSRichard Henderson    tcg_out_opc_imm(s, OPC_JALR, TCG_REG_ZERO, TCG_REG_TMP0, 0);
1321cf7d6b8eSRichard Henderson    set_jmp_reset_offset(s, which);
1322cf7d6b8eSRichard Henderson}
1323cf7d6b8eSRichard Henderson
132490c0fee3SRichard Hendersonvoid tb_target_set_jmp_target(const TranslationBlock *tb, int n,
132590c0fee3SRichard Henderson                              uintptr_t jmp_rx, uintptr_t jmp_rw)
132690c0fee3SRichard Henderson{
1327493c9b19SRichard Henderson    uintptr_t addr = tb->jmp_target_addr[n];
1328493c9b19SRichard Henderson    ptrdiff_t offset = addr - jmp_rx;
1329493c9b19SRichard Henderson    tcg_insn_unit insn;
1330493c9b19SRichard Henderson
1331493c9b19SRichard Henderson    /* Either directly branch, or fall through to indirect branch. */
1332493c9b19SRichard Henderson    if (offset == sextreg(offset, 0, 20)) {
1333493c9b19SRichard Henderson        insn = encode_uj(OPC_JAL, TCG_REG_ZERO, offset);
1334493c9b19SRichard Henderson    } else {
1335493c9b19SRichard Henderson        insn = OPC_NOP;
1336493c9b19SRichard Henderson    }
1337493c9b19SRichard Henderson    qatomic_set((uint32_t *)jmp_rw, insn);
1338493c9b19SRichard Henderson    flush_idcache_range(jmp_rx, jmp_rw, 4);
133990c0fee3SRichard Henderson}
134090c0fee3SRichard Henderson
1341139c1837SPaolo Bonzinistatic void tcg_out_op(TCGContext *s, TCGOpcode opc,
13425e8892dbSMiroslav Rezanina                       const TCGArg args[TCG_MAX_OP_ARGS],
13435e8892dbSMiroslav Rezanina                       const int const_args[TCG_MAX_OP_ARGS])
1344139c1837SPaolo Bonzini{
1345139c1837SPaolo Bonzini    TCGArg a0 = args[0];
1346139c1837SPaolo Bonzini    TCGArg a1 = args[1];
1347139c1837SPaolo Bonzini    TCGArg a2 = args[2];
1348139c1837SPaolo Bonzini    int c2 = const_args[2];
1349139c1837SPaolo Bonzini
1350139c1837SPaolo Bonzini    switch (opc) {
1351139c1837SPaolo Bonzini    case INDEX_op_goto_ptr:
1352139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_JALR, TCG_REG_ZERO, a0, 0);
1353139c1837SPaolo Bonzini        break;
1354139c1837SPaolo Bonzini
1355139c1837SPaolo Bonzini    case INDEX_op_br:
1356139c1837SPaolo Bonzini        tcg_out_reloc(s, s->code_ptr, R_RISCV_JAL, arg_label(a0), 0);
1357139c1837SPaolo Bonzini        tcg_out_opc_jump(s, OPC_JAL, TCG_REG_ZERO, 0);
1358139c1837SPaolo Bonzini        break;
1359139c1837SPaolo Bonzini
1360139c1837SPaolo Bonzini    case INDEX_op_ld8u_i32:
1361139c1837SPaolo Bonzini    case INDEX_op_ld8u_i64:
1362139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_LBU, a0, a1, a2);
1363139c1837SPaolo Bonzini        break;
1364139c1837SPaolo Bonzini    case INDEX_op_ld8s_i32:
1365139c1837SPaolo Bonzini    case INDEX_op_ld8s_i64:
1366139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_LB, a0, a1, a2);
1367139c1837SPaolo Bonzini        break;
1368139c1837SPaolo Bonzini    case INDEX_op_ld16u_i32:
1369139c1837SPaolo Bonzini    case INDEX_op_ld16u_i64:
1370139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_LHU, a0, a1, a2);
1371139c1837SPaolo Bonzini        break;
1372139c1837SPaolo Bonzini    case INDEX_op_ld16s_i32:
1373139c1837SPaolo Bonzini    case INDEX_op_ld16s_i64:
1374139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_LH, a0, a1, a2);
1375139c1837SPaolo Bonzini        break;
1376139c1837SPaolo Bonzini    case INDEX_op_ld32u_i64:
1377139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_LWU, a0, a1, a2);
1378139c1837SPaolo Bonzini        break;
1379139c1837SPaolo Bonzini    case INDEX_op_ld_i32:
1380139c1837SPaolo Bonzini    case INDEX_op_ld32s_i64:
1381139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_LW, a0, a1, a2);
1382139c1837SPaolo Bonzini        break;
1383139c1837SPaolo Bonzini    case INDEX_op_ld_i64:
1384139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_LD, a0, a1, a2);
1385139c1837SPaolo Bonzini        break;
1386139c1837SPaolo Bonzini
1387139c1837SPaolo Bonzini    case INDEX_op_st8_i32:
1388139c1837SPaolo Bonzini    case INDEX_op_st8_i64:
1389139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_SB, a0, a1, a2);
1390139c1837SPaolo Bonzini        break;
1391139c1837SPaolo Bonzini    case INDEX_op_st16_i32:
1392139c1837SPaolo Bonzini    case INDEX_op_st16_i64:
1393139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_SH, a0, a1, a2);
1394139c1837SPaolo Bonzini        break;
1395139c1837SPaolo Bonzini    case INDEX_op_st_i32:
1396139c1837SPaolo Bonzini    case INDEX_op_st32_i64:
1397139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_SW, a0, a1, a2);
1398139c1837SPaolo Bonzini        break;
1399139c1837SPaolo Bonzini    case INDEX_op_st_i64:
1400139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_SD, a0, a1, a2);
1401139c1837SPaolo Bonzini        break;
1402139c1837SPaolo Bonzini
1403139c1837SPaolo Bonzini    case INDEX_op_add_i32:
1404139c1837SPaolo Bonzini        if (c2) {
1405139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_ADDIW, a0, a1, a2);
1406139c1837SPaolo Bonzini        } else {
1407139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_ADDW, a0, a1, a2);
1408139c1837SPaolo Bonzini        }
1409139c1837SPaolo Bonzini        break;
1410139c1837SPaolo Bonzini    case INDEX_op_add_i64:
1411139c1837SPaolo Bonzini        if (c2) {
1412139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_ADDI, a0, a1, a2);
1413139c1837SPaolo Bonzini        } else {
1414139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_ADD, a0, a1, a2);
1415139c1837SPaolo Bonzini        }
1416139c1837SPaolo Bonzini        break;
1417139c1837SPaolo Bonzini
1418139c1837SPaolo Bonzini    case INDEX_op_sub_i32:
1419139c1837SPaolo Bonzini        if (c2) {
1420139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_ADDIW, a0, a1, -a2);
1421139c1837SPaolo Bonzini        } else {
1422139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SUBW, a0, a1, a2);
1423139c1837SPaolo Bonzini        }
1424139c1837SPaolo Bonzini        break;
1425139c1837SPaolo Bonzini    case INDEX_op_sub_i64:
1426139c1837SPaolo Bonzini        if (c2) {
1427139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_ADDI, a0, a1, -a2);
1428139c1837SPaolo Bonzini        } else {
1429139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SUB, a0, a1, a2);
1430139c1837SPaolo Bonzini        }
1431139c1837SPaolo Bonzini        break;
1432139c1837SPaolo Bonzini
1433139c1837SPaolo Bonzini    case INDEX_op_and_i32:
1434139c1837SPaolo Bonzini    case INDEX_op_and_i64:
1435139c1837SPaolo Bonzini        if (c2) {
1436139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_ANDI, a0, a1, a2);
1437139c1837SPaolo Bonzini        } else {
1438139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_AND, a0, a1, a2);
1439139c1837SPaolo Bonzini        }
1440139c1837SPaolo Bonzini        break;
1441139c1837SPaolo Bonzini
1442139c1837SPaolo Bonzini    case INDEX_op_or_i32:
1443139c1837SPaolo Bonzini    case INDEX_op_or_i64:
1444139c1837SPaolo Bonzini        if (c2) {
1445139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_ORI, a0, a1, a2);
1446139c1837SPaolo Bonzini        } else {
1447139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_OR, a0, a1, a2);
1448139c1837SPaolo Bonzini        }
1449139c1837SPaolo Bonzini        break;
1450139c1837SPaolo Bonzini
1451139c1837SPaolo Bonzini    case INDEX_op_xor_i32:
1452139c1837SPaolo Bonzini    case INDEX_op_xor_i64:
1453139c1837SPaolo Bonzini        if (c2) {
1454139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_XORI, a0, a1, a2);
1455139c1837SPaolo Bonzini        } else {
1456139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_XOR, a0, a1, a2);
1457139c1837SPaolo Bonzini        }
1458139c1837SPaolo Bonzini        break;
1459139c1837SPaolo Bonzini
1460139c1837SPaolo Bonzini    case INDEX_op_not_i32:
1461139c1837SPaolo Bonzini    case INDEX_op_not_i64:
1462139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_XORI, a0, a1, -1);
1463139c1837SPaolo Bonzini        break;
1464139c1837SPaolo Bonzini
1465139c1837SPaolo Bonzini    case INDEX_op_neg_i32:
1466139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SUBW, a0, TCG_REG_ZERO, a1);
1467139c1837SPaolo Bonzini        break;
1468139c1837SPaolo Bonzini    case INDEX_op_neg_i64:
1469139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SUB, a0, TCG_REG_ZERO, a1);
1470139c1837SPaolo Bonzini        break;
1471139c1837SPaolo Bonzini
1472139c1837SPaolo Bonzini    case INDEX_op_mul_i32:
1473139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_MULW, a0, a1, a2);
1474139c1837SPaolo Bonzini        break;
1475139c1837SPaolo Bonzini    case INDEX_op_mul_i64:
1476139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_MUL, a0, a1, a2);
1477139c1837SPaolo Bonzini        break;
1478139c1837SPaolo Bonzini
1479139c1837SPaolo Bonzini    case INDEX_op_div_i32:
1480139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_DIVW, a0, a1, a2);
1481139c1837SPaolo Bonzini        break;
1482139c1837SPaolo Bonzini    case INDEX_op_div_i64:
1483139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_DIV, a0, a1, a2);
1484139c1837SPaolo Bonzini        break;
1485139c1837SPaolo Bonzini
1486139c1837SPaolo Bonzini    case INDEX_op_divu_i32:
1487139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_DIVUW, a0, a1, a2);
1488139c1837SPaolo Bonzini        break;
1489139c1837SPaolo Bonzini    case INDEX_op_divu_i64:
1490139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_DIVU, a0, a1, a2);
1491139c1837SPaolo Bonzini        break;
1492139c1837SPaolo Bonzini
1493139c1837SPaolo Bonzini    case INDEX_op_rem_i32:
1494139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_REMW, a0, a1, a2);
1495139c1837SPaolo Bonzini        break;
1496139c1837SPaolo Bonzini    case INDEX_op_rem_i64:
1497139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_REM, a0, a1, a2);
1498139c1837SPaolo Bonzini        break;
1499139c1837SPaolo Bonzini
1500139c1837SPaolo Bonzini    case INDEX_op_remu_i32:
1501139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_REMUW, a0, a1, a2);
1502139c1837SPaolo Bonzini        break;
1503139c1837SPaolo Bonzini    case INDEX_op_remu_i64:
1504139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_REMU, a0, a1, a2);
1505139c1837SPaolo Bonzini        break;
1506139c1837SPaolo Bonzini
1507139c1837SPaolo Bonzini    case INDEX_op_shl_i32:
1508139c1837SPaolo Bonzini        if (c2) {
1509d2f3066eSZihao Yu            tcg_out_opc_imm(s, OPC_SLLIW, a0, a1, a2 & 0x1f);
1510139c1837SPaolo Bonzini        } else {
1511139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SLLW, a0, a1, a2);
1512139c1837SPaolo Bonzini        }
1513139c1837SPaolo Bonzini        break;
1514139c1837SPaolo Bonzini    case INDEX_op_shl_i64:
1515139c1837SPaolo Bonzini        if (c2) {
1516d2f3066eSZihao Yu            tcg_out_opc_imm(s, OPC_SLLI, a0, a1, a2 & 0x3f);
1517139c1837SPaolo Bonzini        } else {
1518139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SLL, a0, a1, a2);
1519139c1837SPaolo Bonzini        }
1520139c1837SPaolo Bonzini        break;
1521139c1837SPaolo Bonzini
1522139c1837SPaolo Bonzini    case INDEX_op_shr_i32:
1523139c1837SPaolo Bonzini        if (c2) {
1524d2f3066eSZihao Yu            tcg_out_opc_imm(s, OPC_SRLIW, a0, a1, a2 & 0x1f);
1525139c1837SPaolo Bonzini        } else {
1526139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SRLW, a0, a1, a2);
1527139c1837SPaolo Bonzini        }
1528139c1837SPaolo Bonzini        break;
1529139c1837SPaolo Bonzini    case INDEX_op_shr_i64:
1530139c1837SPaolo Bonzini        if (c2) {
1531d2f3066eSZihao Yu            tcg_out_opc_imm(s, OPC_SRLI, a0, a1, a2 & 0x3f);
1532139c1837SPaolo Bonzini        } else {
1533139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SRL, a0, a1, a2);
1534139c1837SPaolo Bonzini        }
1535139c1837SPaolo Bonzini        break;
1536139c1837SPaolo Bonzini
1537139c1837SPaolo Bonzini    case INDEX_op_sar_i32:
1538139c1837SPaolo Bonzini        if (c2) {
1539d2f3066eSZihao Yu            tcg_out_opc_imm(s, OPC_SRAIW, a0, a1, a2 & 0x1f);
1540139c1837SPaolo Bonzini        } else {
1541139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SRAW, a0, a1, a2);
1542139c1837SPaolo Bonzini        }
1543139c1837SPaolo Bonzini        break;
1544139c1837SPaolo Bonzini    case INDEX_op_sar_i64:
1545139c1837SPaolo Bonzini        if (c2) {
1546d2f3066eSZihao Yu            tcg_out_opc_imm(s, OPC_SRAI, a0, a1, a2 & 0x3f);
1547139c1837SPaolo Bonzini        } else {
1548139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SRA, a0, a1, a2);
1549139c1837SPaolo Bonzini        }
1550139c1837SPaolo Bonzini        break;
1551139c1837SPaolo Bonzini
1552139c1837SPaolo Bonzini    case INDEX_op_add2_i32:
1553139c1837SPaolo Bonzini        tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5],
1554139c1837SPaolo Bonzini                        const_args[4], const_args[5], false, true);
1555139c1837SPaolo Bonzini        break;
1556139c1837SPaolo Bonzini    case INDEX_op_add2_i64:
1557139c1837SPaolo Bonzini        tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5],
1558139c1837SPaolo Bonzini                        const_args[4], const_args[5], false, false);
1559139c1837SPaolo Bonzini        break;
1560139c1837SPaolo Bonzini    case INDEX_op_sub2_i32:
1561139c1837SPaolo Bonzini        tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5],
1562139c1837SPaolo Bonzini                        const_args[4], const_args[5], true, true);
1563139c1837SPaolo Bonzini        break;
1564139c1837SPaolo Bonzini    case INDEX_op_sub2_i64:
1565139c1837SPaolo Bonzini        tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5],
1566139c1837SPaolo Bonzini                        const_args[4], const_args[5], true, false);
1567139c1837SPaolo Bonzini        break;
1568139c1837SPaolo Bonzini
1569139c1837SPaolo Bonzini    case INDEX_op_brcond_i32:
1570139c1837SPaolo Bonzini    case INDEX_op_brcond_i64:
1571139c1837SPaolo Bonzini        tcg_out_brcond(s, a2, a0, a1, arg_label(args[3]));
1572139c1837SPaolo Bonzini        break;
1573139c1837SPaolo Bonzini    case INDEX_op_brcond2_i32:
1574139c1837SPaolo Bonzini        tcg_out_brcond2(s, args[4], a0, a1, a2, args[3], arg_label(args[5]));
1575139c1837SPaolo Bonzini        break;
1576139c1837SPaolo Bonzini
1577139c1837SPaolo Bonzini    case INDEX_op_setcond_i32:
1578139c1837SPaolo Bonzini    case INDEX_op_setcond_i64:
1579139c1837SPaolo Bonzini        tcg_out_setcond(s, args[3], a0, a1, a2);
1580139c1837SPaolo Bonzini        break;
1581139c1837SPaolo Bonzini    case INDEX_op_setcond2_i32:
1582139c1837SPaolo Bonzini        tcg_out_setcond2(s, args[5], a0, a1, a2, args[3], args[4]);
1583139c1837SPaolo Bonzini        break;
1584139c1837SPaolo Bonzini
1585139c1837SPaolo Bonzini    case INDEX_op_qemu_ld_i32:
1586139c1837SPaolo Bonzini        tcg_out_qemu_ld(s, args, false);
1587139c1837SPaolo Bonzini        break;
1588139c1837SPaolo Bonzini    case INDEX_op_qemu_ld_i64:
1589139c1837SPaolo Bonzini        tcg_out_qemu_ld(s, args, true);
1590139c1837SPaolo Bonzini        break;
1591139c1837SPaolo Bonzini    case INDEX_op_qemu_st_i32:
1592139c1837SPaolo Bonzini        tcg_out_qemu_st(s, args, false);
1593139c1837SPaolo Bonzini        break;
1594139c1837SPaolo Bonzini    case INDEX_op_qemu_st_i64:
1595139c1837SPaolo Bonzini        tcg_out_qemu_st(s, args, true);
1596139c1837SPaolo Bonzini        break;
1597139c1837SPaolo Bonzini
1598139c1837SPaolo Bonzini    case INDEX_op_ext8u_i32:
1599139c1837SPaolo Bonzini    case INDEX_op_ext8u_i64:
1600139c1837SPaolo Bonzini        tcg_out_ext8u(s, a0, a1);
1601139c1837SPaolo Bonzini        break;
1602139c1837SPaolo Bonzini
1603139c1837SPaolo Bonzini    case INDEX_op_ext16u_i32:
1604139c1837SPaolo Bonzini    case INDEX_op_ext16u_i64:
1605139c1837SPaolo Bonzini        tcg_out_ext16u(s, a0, a1);
1606139c1837SPaolo Bonzini        break;
1607139c1837SPaolo Bonzini
1608139c1837SPaolo Bonzini    case INDEX_op_ext32u_i64:
1609139c1837SPaolo Bonzini    case INDEX_op_extu_i32_i64:
1610139c1837SPaolo Bonzini        tcg_out_ext32u(s, a0, a1);
1611139c1837SPaolo Bonzini        break;
1612139c1837SPaolo Bonzini
1613139c1837SPaolo Bonzini    case INDEX_op_ext8s_i32:
1614139c1837SPaolo Bonzini    case INDEX_op_ext8s_i64:
1615139c1837SPaolo Bonzini        tcg_out_ext8s(s, a0, a1);
1616139c1837SPaolo Bonzini        break;
1617139c1837SPaolo Bonzini
1618139c1837SPaolo Bonzini    case INDEX_op_ext16s_i32:
1619139c1837SPaolo Bonzini    case INDEX_op_ext16s_i64:
1620139c1837SPaolo Bonzini        tcg_out_ext16s(s, a0, a1);
1621139c1837SPaolo Bonzini        break;
1622139c1837SPaolo Bonzini
1623139c1837SPaolo Bonzini    case INDEX_op_ext32s_i64:
1624139c1837SPaolo Bonzini    case INDEX_op_extrl_i64_i32:
1625139c1837SPaolo Bonzini    case INDEX_op_ext_i32_i64:
1626139c1837SPaolo Bonzini        tcg_out_ext32s(s, a0, a1);
1627139c1837SPaolo Bonzini        break;
1628139c1837SPaolo Bonzini
1629139c1837SPaolo Bonzini    case INDEX_op_extrh_i64_i32:
1630139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_SRAI, a0, a1, 32);
1631139c1837SPaolo Bonzini        break;
1632139c1837SPaolo Bonzini
1633139c1837SPaolo Bonzini    case INDEX_op_mulsh_i32:
1634139c1837SPaolo Bonzini    case INDEX_op_mulsh_i64:
1635139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_MULH, a0, a1, a2);
1636139c1837SPaolo Bonzini        break;
1637139c1837SPaolo Bonzini
1638139c1837SPaolo Bonzini    case INDEX_op_muluh_i32:
1639139c1837SPaolo Bonzini    case INDEX_op_muluh_i64:
1640139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_MULHU, a0, a1, a2);
1641139c1837SPaolo Bonzini        break;
1642139c1837SPaolo Bonzini
1643139c1837SPaolo Bonzini    case INDEX_op_mb:
1644139c1837SPaolo Bonzini        tcg_out_mb(s, a0);
1645139c1837SPaolo Bonzini        break;
1646139c1837SPaolo Bonzini
1647139c1837SPaolo Bonzini    case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
1648139c1837SPaolo Bonzini    case INDEX_op_mov_i64:
1649139c1837SPaolo Bonzini    case INDEX_op_call:     /* Always emitted via tcg_out_call.  */
1650b55a8d9dSRichard Henderson    case INDEX_op_exit_tb:  /* Always emitted via tcg_out_exit_tb.  */
1651cf7d6b8eSRichard Henderson    case INDEX_op_goto_tb:  /* Always emitted via tcg_out_goto_tb.  */
1652139c1837SPaolo Bonzini    default:
1653139c1837SPaolo Bonzini        g_assert_not_reached();
1654139c1837SPaolo Bonzini    }
1655139c1837SPaolo Bonzini}
1656139c1837SPaolo Bonzini
1657665be288SRichard Hendersonstatic TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
1658139c1837SPaolo Bonzini{
1659139c1837SPaolo Bonzini    switch (op) {
1660139c1837SPaolo Bonzini    case INDEX_op_goto_ptr:
1661665be288SRichard Henderson        return C_O0_I1(r);
1662139c1837SPaolo Bonzini
1663139c1837SPaolo Bonzini    case INDEX_op_ld8u_i32:
1664139c1837SPaolo Bonzini    case INDEX_op_ld8s_i32:
1665139c1837SPaolo Bonzini    case INDEX_op_ld16u_i32:
1666139c1837SPaolo Bonzini    case INDEX_op_ld16s_i32:
1667139c1837SPaolo Bonzini    case INDEX_op_ld_i32:
1668139c1837SPaolo Bonzini    case INDEX_op_not_i32:
1669139c1837SPaolo Bonzini    case INDEX_op_neg_i32:
1670139c1837SPaolo Bonzini    case INDEX_op_ld8u_i64:
1671139c1837SPaolo Bonzini    case INDEX_op_ld8s_i64:
1672139c1837SPaolo Bonzini    case INDEX_op_ld16u_i64:
1673139c1837SPaolo Bonzini    case INDEX_op_ld16s_i64:
1674139c1837SPaolo Bonzini    case INDEX_op_ld32s_i64:
1675139c1837SPaolo Bonzini    case INDEX_op_ld32u_i64:
1676139c1837SPaolo Bonzini    case INDEX_op_ld_i64:
1677139c1837SPaolo Bonzini    case INDEX_op_not_i64:
1678139c1837SPaolo Bonzini    case INDEX_op_neg_i64:
1679139c1837SPaolo Bonzini    case INDEX_op_ext8u_i32:
1680139c1837SPaolo Bonzini    case INDEX_op_ext8u_i64:
1681139c1837SPaolo Bonzini    case INDEX_op_ext16u_i32:
1682139c1837SPaolo Bonzini    case INDEX_op_ext16u_i64:
1683139c1837SPaolo Bonzini    case INDEX_op_ext32u_i64:
1684139c1837SPaolo Bonzini    case INDEX_op_extu_i32_i64:
1685139c1837SPaolo Bonzini    case INDEX_op_ext8s_i32:
1686139c1837SPaolo Bonzini    case INDEX_op_ext8s_i64:
1687139c1837SPaolo Bonzini    case INDEX_op_ext16s_i32:
1688139c1837SPaolo Bonzini    case INDEX_op_ext16s_i64:
1689139c1837SPaolo Bonzini    case INDEX_op_ext32s_i64:
1690139c1837SPaolo Bonzini    case INDEX_op_extrl_i64_i32:
1691139c1837SPaolo Bonzini    case INDEX_op_extrh_i64_i32:
1692139c1837SPaolo Bonzini    case INDEX_op_ext_i32_i64:
1693665be288SRichard Henderson        return C_O1_I1(r, r);
1694139c1837SPaolo Bonzini
1695139c1837SPaolo Bonzini    case INDEX_op_st8_i32:
1696139c1837SPaolo Bonzini    case INDEX_op_st16_i32:
1697139c1837SPaolo Bonzini    case INDEX_op_st_i32:
1698139c1837SPaolo Bonzini    case INDEX_op_st8_i64:
1699139c1837SPaolo Bonzini    case INDEX_op_st16_i64:
1700139c1837SPaolo Bonzini    case INDEX_op_st32_i64:
1701139c1837SPaolo Bonzini    case INDEX_op_st_i64:
1702665be288SRichard Henderson        return C_O0_I2(rZ, r);
1703139c1837SPaolo Bonzini
1704139c1837SPaolo Bonzini    case INDEX_op_add_i32:
1705139c1837SPaolo Bonzini    case INDEX_op_and_i32:
1706139c1837SPaolo Bonzini    case INDEX_op_or_i32:
1707139c1837SPaolo Bonzini    case INDEX_op_xor_i32:
1708139c1837SPaolo Bonzini    case INDEX_op_add_i64:
1709139c1837SPaolo Bonzini    case INDEX_op_and_i64:
1710139c1837SPaolo Bonzini    case INDEX_op_or_i64:
1711139c1837SPaolo Bonzini    case INDEX_op_xor_i64:
1712665be288SRichard Henderson        return C_O1_I2(r, r, rI);
1713139c1837SPaolo Bonzini
1714139c1837SPaolo Bonzini    case INDEX_op_sub_i32:
1715139c1837SPaolo Bonzini    case INDEX_op_sub_i64:
1716665be288SRichard Henderson        return C_O1_I2(r, rZ, rN);
1717139c1837SPaolo Bonzini
1718139c1837SPaolo Bonzini    case INDEX_op_mul_i32:
1719139c1837SPaolo Bonzini    case INDEX_op_mulsh_i32:
1720139c1837SPaolo Bonzini    case INDEX_op_muluh_i32:
1721139c1837SPaolo Bonzini    case INDEX_op_div_i32:
1722139c1837SPaolo Bonzini    case INDEX_op_divu_i32:
1723139c1837SPaolo Bonzini    case INDEX_op_rem_i32:
1724139c1837SPaolo Bonzini    case INDEX_op_remu_i32:
1725139c1837SPaolo Bonzini    case INDEX_op_setcond_i32:
1726139c1837SPaolo Bonzini    case INDEX_op_mul_i64:
1727139c1837SPaolo Bonzini    case INDEX_op_mulsh_i64:
1728139c1837SPaolo Bonzini    case INDEX_op_muluh_i64:
1729139c1837SPaolo Bonzini    case INDEX_op_div_i64:
1730139c1837SPaolo Bonzini    case INDEX_op_divu_i64:
1731139c1837SPaolo Bonzini    case INDEX_op_rem_i64:
1732139c1837SPaolo Bonzini    case INDEX_op_remu_i64:
1733139c1837SPaolo Bonzini    case INDEX_op_setcond_i64:
1734665be288SRichard Henderson        return C_O1_I2(r, rZ, rZ);
1735139c1837SPaolo Bonzini
1736139c1837SPaolo Bonzini    case INDEX_op_shl_i32:
1737139c1837SPaolo Bonzini    case INDEX_op_shr_i32:
1738139c1837SPaolo Bonzini    case INDEX_op_sar_i32:
1739139c1837SPaolo Bonzini    case INDEX_op_shl_i64:
1740139c1837SPaolo Bonzini    case INDEX_op_shr_i64:
1741139c1837SPaolo Bonzini    case INDEX_op_sar_i64:
1742665be288SRichard Henderson        return C_O1_I2(r, r, ri);
1743139c1837SPaolo Bonzini
1744139c1837SPaolo Bonzini    case INDEX_op_brcond_i32:
1745139c1837SPaolo Bonzini    case INDEX_op_brcond_i64:
1746665be288SRichard Henderson        return C_O0_I2(rZ, rZ);
1747139c1837SPaolo Bonzini
1748139c1837SPaolo Bonzini    case INDEX_op_add2_i32:
1749139c1837SPaolo Bonzini    case INDEX_op_add2_i64:
1750139c1837SPaolo Bonzini    case INDEX_op_sub2_i32:
1751139c1837SPaolo Bonzini    case INDEX_op_sub2_i64:
1752665be288SRichard Henderson        return C_O2_I4(r, r, rZ, rZ, rM, rM);
1753139c1837SPaolo Bonzini
1754139c1837SPaolo Bonzini    case INDEX_op_brcond2_i32:
1755665be288SRichard Henderson        return C_O0_I4(rZ, rZ, rZ, rZ);
1756139c1837SPaolo Bonzini
1757139c1837SPaolo Bonzini    case INDEX_op_setcond2_i32:
1758665be288SRichard Henderson        return C_O1_I4(r, rZ, rZ, rZ, rZ);
1759139c1837SPaolo Bonzini
1760139c1837SPaolo Bonzini    case INDEX_op_qemu_ld_i32:
1761665be288SRichard Henderson        return (TARGET_LONG_BITS <= TCG_TARGET_REG_BITS
1762665be288SRichard Henderson                ? C_O1_I1(r, L) : C_O1_I2(r, L, L));
1763139c1837SPaolo Bonzini    case INDEX_op_qemu_st_i32:
1764665be288SRichard Henderson        return (TARGET_LONG_BITS <= TCG_TARGET_REG_BITS
1765665be288SRichard Henderson                ? C_O0_I2(LZ, L) : C_O0_I3(LZ, L, L));
1766139c1837SPaolo Bonzini    case INDEX_op_qemu_ld_i64:
1767665be288SRichard Henderson        return (TCG_TARGET_REG_BITS == 64 ? C_O1_I1(r, L)
1768665be288SRichard Henderson               : TARGET_LONG_BITS <= TCG_TARGET_REG_BITS ? C_O2_I1(r, r, L)
1769665be288SRichard Henderson               : C_O2_I2(r, r, L, L));
1770139c1837SPaolo Bonzini    case INDEX_op_qemu_st_i64:
1771665be288SRichard Henderson        return (TCG_TARGET_REG_BITS == 64 ? C_O0_I2(LZ, L)
1772665be288SRichard Henderson               : TARGET_LONG_BITS <= TCG_TARGET_REG_BITS ? C_O0_I3(LZ, LZ, L)
1773665be288SRichard Henderson               : C_O0_I4(LZ, LZ, L, L));
1774139c1837SPaolo Bonzini
1775139c1837SPaolo Bonzini    default:
1776665be288SRichard Henderson        g_assert_not_reached();
1777139c1837SPaolo Bonzini    }
1778139c1837SPaolo Bonzini}
1779139c1837SPaolo Bonzini
1780139c1837SPaolo Bonzinistatic const int tcg_target_callee_save_regs[] = {
1781139c1837SPaolo Bonzini    TCG_REG_S0,       /* used for the global env (TCG_AREG0) */
1782139c1837SPaolo Bonzini    TCG_REG_S1,
1783139c1837SPaolo Bonzini    TCG_REG_S2,
1784139c1837SPaolo Bonzini    TCG_REG_S3,
1785139c1837SPaolo Bonzini    TCG_REG_S4,
1786139c1837SPaolo Bonzini    TCG_REG_S5,
1787139c1837SPaolo Bonzini    TCG_REG_S6,
1788139c1837SPaolo Bonzini    TCG_REG_S7,
1789139c1837SPaolo Bonzini    TCG_REG_S8,
1790139c1837SPaolo Bonzini    TCG_REG_S9,
1791139c1837SPaolo Bonzini    TCG_REG_S10,
1792139c1837SPaolo Bonzini    TCG_REG_S11,
1793139c1837SPaolo Bonzini    TCG_REG_RA,       /* should be last for ABI compliance */
1794139c1837SPaolo Bonzini};
1795139c1837SPaolo Bonzini
1796139c1837SPaolo Bonzini/* Stack frame parameters.  */
1797139c1837SPaolo Bonzini#define REG_SIZE   (TCG_TARGET_REG_BITS / 8)
1798139c1837SPaolo Bonzini#define SAVE_SIZE  ((int)ARRAY_SIZE(tcg_target_callee_save_regs) * REG_SIZE)
1799139c1837SPaolo Bonzini#define TEMP_SIZE  (CPU_TEMP_BUF_NLONGS * (int)sizeof(long))
1800139c1837SPaolo Bonzini#define FRAME_SIZE ((TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE + SAVE_SIZE \
1801139c1837SPaolo Bonzini                     + TCG_TARGET_STACK_ALIGN - 1) \
1802139c1837SPaolo Bonzini                    & -TCG_TARGET_STACK_ALIGN)
1803139c1837SPaolo Bonzini#define SAVE_OFS   (TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE)
1804139c1837SPaolo Bonzini
1805139c1837SPaolo Bonzini/* We're expecting to be able to use an immediate for frame allocation.  */
1806139c1837SPaolo BonziniQEMU_BUILD_BUG_ON(FRAME_SIZE > 0x7ff);
1807139c1837SPaolo Bonzini
1808139c1837SPaolo Bonzini/* Generate global QEMU prologue and epilogue code */
1809139c1837SPaolo Bonzinistatic void tcg_target_qemu_prologue(TCGContext *s)
1810139c1837SPaolo Bonzini{
1811139c1837SPaolo Bonzini    int i;
1812139c1837SPaolo Bonzini
1813139c1837SPaolo Bonzini    tcg_set_frame(s, TCG_REG_SP, TCG_STATIC_CALL_ARGS_SIZE, TEMP_SIZE);
1814139c1837SPaolo Bonzini
1815139c1837SPaolo Bonzini    /* TB prologue */
1816139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_ADDI, TCG_REG_SP, TCG_REG_SP, -FRAME_SIZE);
1817139c1837SPaolo Bonzini    for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
1818139c1837SPaolo Bonzini        tcg_out_st(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
1819139c1837SPaolo Bonzini                   TCG_REG_SP, SAVE_OFS + i * REG_SIZE);
1820139c1837SPaolo Bonzini    }
1821139c1837SPaolo Bonzini
1822139c1837SPaolo Bonzini#if !defined(CONFIG_SOFTMMU)
1823139c1837SPaolo Bonzini    tcg_out_movi(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG, guest_base);
1824139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
1825139c1837SPaolo Bonzini#endif
1826139c1837SPaolo Bonzini
1827139c1837SPaolo Bonzini    /* Call generated code */
1828139c1837SPaolo Bonzini    tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
1829139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_JALR, TCG_REG_ZERO, tcg_target_call_iarg_regs[1], 0);
1830139c1837SPaolo Bonzini
1831139c1837SPaolo Bonzini    /* Return path for goto_ptr. Set return value to 0 */
1832c8bc1168SRichard Henderson    tcg_code_gen_epilogue = tcg_splitwx_to_rx(s->code_ptr);
1833139c1837SPaolo Bonzini    tcg_out_mov(s, TCG_TYPE_REG, TCG_REG_A0, TCG_REG_ZERO);
1834139c1837SPaolo Bonzini
1835139c1837SPaolo Bonzini    /* TB epilogue */
1836793f7381SRichard Henderson    tb_ret_addr = tcg_splitwx_to_rx(s->code_ptr);
1837139c1837SPaolo Bonzini    for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
1838139c1837SPaolo Bonzini        tcg_out_ld(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
1839139c1837SPaolo Bonzini                   TCG_REG_SP, SAVE_OFS + i * REG_SIZE);
1840139c1837SPaolo Bonzini    }
1841139c1837SPaolo Bonzini
1842139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_ADDI, TCG_REG_SP, TCG_REG_SP, FRAME_SIZE);
1843139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_JALR, TCG_REG_ZERO, TCG_REG_RA, 0);
1844139c1837SPaolo Bonzini}
1845139c1837SPaolo Bonzini
1846139c1837SPaolo Bonzinistatic void tcg_target_init(TCGContext *s)
1847139c1837SPaolo Bonzini{
1848139c1837SPaolo Bonzini    tcg_target_available_regs[TCG_TYPE_I32] = 0xffffffff;
1849139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS == 64) {
1850139c1837SPaolo Bonzini        tcg_target_available_regs[TCG_TYPE_I64] = 0xffffffff;
1851139c1837SPaolo Bonzini    }
1852139c1837SPaolo Bonzini
1853139c1837SPaolo Bonzini    tcg_target_call_clobber_regs = -1u;
1854139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S0);
1855139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S1);
1856139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S2);
1857139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S3);
1858139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S4);
1859139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S5);
1860139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S6);
1861139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S7);
1862139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S8);
1863139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S9);
1864139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S10);
1865139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S11);
1866139c1837SPaolo Bonzini
1867139c1837SPaolo Bonzini    s->reserved_regs = 0;
1868139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_REG_ZERO);
1869139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP0);
1870139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP1);
1871139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP2);
1872139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_REG_SP);
1873139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_REG_GP);
1874139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TP);
1875139c1837SPaolo Bonzini}
1876139c1837SPaolo Bonzini
1877139c1837SPaolo Bonzinitypedef struct {
1878139c1837SPaolo Bonzini    DebugFrameHeader h;
1879139c1837SPaolo Bonzini    uint8_t fde_def_cfa[4];
1880139c1837SPaolo Bonzini    uint8_t fde_reg_ofs[ARRAY_SIZE(tcg_target_callee_save_regs) * 2];
1881139c1837SPaolo Bonzini} DebugFrame;
1882139c1837SPaolo Bonzini
1883139c1837SPaolo Bonzini#define ELF_HOST_MACHINE EM_RISCV
1884139c1837SPaolo Bonzini
1885139c1837SPaolo Bonzinistatic const DebugFrame debug_frame = {
1886139c1837SPaolo Bonzini    .h.cie.len = sizeof(DebugFrameCIE) - 4, /* length after .len member */
1887139c1837SPaolo Bonzini    .h.cie.id = -1,
1888139c1837SPaolo Bonzini    .h.cie.version = 1,
1889139c1837SPaolo Bonzini    .h.cie.code_align = 1,
1890139c1837SPaolo Bonzini    .h.cie.data_align = -(TCG_TARGET_REG_BITS / 8) & 0x7f, /* sleb128 */
1891139c1837SPaolo Bonzini    .h.cie.return_column = TCG_REG_RA,
1892139c1837SPaolo Bonzini
1893139c1837SPaolo Bonzini    /* Total FDE size does not include the "len" member.  */
1894139c1837SPaolo Bonzini    .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset),
1895139c1837SPaolo Bonzini
1896139c1837SPaolo Bonzini    .fde_def_cfa = {
1897139c1837SPaolo Bonzini        12, TCG_REG_SP,                 /* DW_CFA_def_cfa sp, ... */
1898139c1837SPaolo Bonzini        (FRAME_SIZE & 0x7f) | 0x80,     /* ... uleb128 FRAME_SIZE */
1899139c1837SPaolo Bonzini        (FRAME_SIZE >> 7)
1900139c1837SPaolo Bonzini    },
1901139c1837SPaolo Bonzini    .fde_reg_ofs = {
1902139c1837SPaolo Bonzini        0x80 + 9,  12,                  /* DW_CFA_offset, s1,  -96 */
1903139c1837SPaolo Bonzini        0x80 + 18, 11,                  /* DW_CFA_offset, s2,  -88 */
1904139c1837SPaolo Bonzini        0x80 + 19, 10,                  /* DW_CFA_offset, s3,  -80 */
1905139c1837SPaolo Bonzini        0x80 + 20, 9,                   /* DW_CFA_offset, s4,  -72 */
1906139c1837SPaolo Bonzini        0x80 + 21, 8,                   /* DW_CFA_offset, s5,  -64 */
1907139c1837SPaolo Bonzini        0x80 + 22, 7,                   /* DW_CFA_offset, s6,  -56 */
1908139c1837SPaolo Bonzini        0x80 + 23, 6,                   /* DW_CFA_offset, s7,  -48 */
1909139c1837SPaolo Bonzini        0x80 + 24, 5,                   /* DW_CFA_offset, s8,  -40 */
1910139c1837SPaolo Bonzini        0x80 + 25, 4,                   /* DW_CFA_offset, s9,  -32 */
1911139c1837SPaolo Bonzini        0x80 + 26, 3,                   /* DW_CFA_offset, s10, -24 */
1912139c1837SPaolo Bonzini        0x80 + 27, 2,                   /* DW_CFA_offset, s11, -16 */
1913139c1837SPaolo Bonzini        0x80 + 1 , 1,                   /* DW_CFA_offset, ra,  -8 */
1914139c1837SPaolo Bonzini    }
1915139c1837SPaolo Bonzini};
1916139c1837SPaolo Bonzini
1917755bf9e5SRichard Hendersonvoid tcg_register_jit(const void *buf, size_t buf_size)
1918139c1837SPaolo Bonzini{
1919139c1837SPaolo Bonzini    tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame));
1920139c1837SPaolo Bonzini}
1921