xref: /openbmc/qemu/tcg/riscv/tcg-target.c.inc (revision 2e3a933abb8bd965db99375bfc341d2d46480995)
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,
270139c1837SPaolo Bonzini} RISCVInsn;
271139c1837SPaolo Bonzini
272139c1837SPaolo Bonzini/*
273139c1837SPaolo Bonzini * RISC-V immediate and instruction encoders (excludes 16-bit RVC)
274139c1837SPaolo Bonzini */
275139c1837SPaolo Bonzini
276139c1837SPaolo Bonzini/* Type-R */
277139c1837SPaolo Bonzini
278139c1837SPaolo Bonzinistatic int32_t encode_r(RISCVInsn opc, TCGReg rd, TCGReg rs1, TCGReg rs2)
279139c1837SPaolo Bonzini{
280139c1837SPaolo Bonzini    return opc | (rd & 0x1f) << 7 | (rs1 & 0x1f) << 15 | (rs2 & 0x1f) << 20;
281139c1837SPaolo Bonzini}
282139c1837SPaolo Bonzini
283139c1837SPaolo Bonzini/* Type-I */
284139c1837SPaolo Bonzini
285139c1837SPaolo Bonzinistatic int32_t encode_imm12(uint32_t imm)
286139c1837SPaolo Bonzini{
287139c1837SPaolo Bonzini    return (imm & 0xfff) << 20;
288139c1837SPaolo Bonzini}
289139c1837SPaolo Bonzini
290139c1837SPaolo Bonzinistatic int32_t encode_i(RISCVInsn opc, TCGReg rd, TCGReg rs1, uint32_t imm)
291139c1837SPaolo Bonzini{
292139c1837SPaolo Bonzini    return opc | (rd & 0x1f) << 7 | (rs1 & 0x1f) << 15 | encode_imm12(imm);
293139c1837SPaolo Bonzini}
294139c1837SPaolo Bonzini
295139c1837SPaolo Bonzini/* Type-S */
296139c1837SPaolo Bonzini
297139c1837SPaolo Bonzinistatic int32_t encode_simm12(uint32_t imm)
298139c1837SPaolo Bonzini{
299139c1837SPaolo Bonzini    int32_t ret = 0;
300139c1837SPaolo Bonzini
301139c1837SPaolo Bonzini    ret |= (imm & 0xFE0) << 20;
302139c1837SPaolo Bonzini    ret |= (imm & 0x1F) << 7;
303139c1837SPaolo Bonzini
304139c1837SPaolo Bonzini    return ret;
305139c1837SPaolo Bonzini}
306139c1837SPaolo Bonzini
307139c1837SPaolo Bonzinistatic int32_t encode_s(RISCVInsn opc, TCGReg rs1, TCGReg rs2, uint32_t imm)
308139c1837SPaolo Bonzini{
309139c1837SPaolo Bonzini    return opc | (rs1 & 0x1f) << 15 | (rs2 & 0x1f) << 20 | encode_simm12(imm);
310139c1837SPaolo Bonzini}
311139c1837SPaolo Bonzini
312139c1837SPaolo Bonzini/* Type-SB */
313139c1837SPaolo Bonzini
314139c1837SPaolo Bonzinistatic int32_t encode_sbimm12(uint32_t imm)
315139c1837SPaolo Bonzini{
316139c1837SPaolo Bonzini    int32_t ret = 0;
317139c1837SPaolo Bonzini
318139c1837SPaolo Bonzini    ret |= (imm & 0x1000) << 19;
319139c1837SPaolo Bonzini    ret |= (imm & 0x7e0) << 20;
320139c1837SPaolo Bonzini    ret |= (imm & 0x1e) << 7;
321139c1837SPaolo Bonzini    ret |= (imm & 0x800) >> 4;
322139c1837SPaolo Bonzini
323139c1837SPaolo Bonzini    return ret;
324139c1837SPaolo Bonzini}
325139c1837SPaolo Bonzini
326139c1837SPaolo Bonzinistatic int32_t encode_sb(RISCVInsn opc, TCGReg rs1, TCGReg rs2, uint32_t imm)
327139c1837SPaolo Bonzini{
328139c1837SPaolo Bonzini    return opc | (rs1 & 0x1f) << 15 | (rs2 & 0x1f) << 20 | encode_sbimm12(imm);
329139c1837SPaolo Bonzini}
330139c1837SPaolo Bonzini
331139c1837SPaolo Bonzini/* Type-U */
332139c1837SPaolo Bonzini
333139c1837SPaolo Bonzinistatic int32_t encode_uimm20(uint32_t imm)
334139c1837SPaolo Bonzini{
335139c1837SPaolo Bonzini    return imm & 0xfffff000;
336139c1837SPaolo Bonzini}
337139c1837SPaolo Bonzini
338139c1837SPaolo Bonzinistatic int32_t encode_u(RISCVInsn opc, TCGReg rd, uint32_t imm)
339139c1837SPaolo Bonzini{
340139c1837SPaolo Bonzini    return opc | (rd & 0x1f) << 7 | encode_uimm20(imm);
341139c1837SPaolo Bonzini}
342139c1837SPaolo Bonzini
343139c1837SPaolo Bonzini/* Type-UJ */
344139c1837SPaolo Bonzini
345139c1837SPaolo Bonzinistatic int32_t encode_ujimm20(uint32_t imm)
346139c1837SPaolo Bonzini{
347139c1837SPaolo Bonzini    int32_t ret = 0;
348139c1837SPaolo Bonzini
349139c1837SPaolo Bonzini    ret |= (imm & 0x0007fe) << (21 - 1);
350139c1837SPaolo Bonzini    ret |= (imm & 0x000800) << (20 - 11);
351139c1837SPaolo Bonzini    ret |= (imm & 0x0ff000) << (12 - 12);
352139c1837SPaolo Bonzini    ret |= (imm & 0x100000) << (31 - 20);
353139c1837SPaolo Bonzini
354139c1837SPaolo Bonzini    return ret;
355139c1837SPaolo Bonzini}
356139c1837SPaolo Bonzini
357139c1837SPaolo Bonzinistatic int32_t encode_uj(RISCVInsn opc, TCGReg rd, uint32_t imm)
358139c1837SPaolo Bonzini{
359139c1837SPaolo Bonzini    return opc | (rd & 0x1f) << 7 | encode_ujimm20(imm);
360139c1837SPaolo Bonzini}
361139c1837SPaolo Bonzini
362139c1837SPaolo Bonzini/*
363139c1837SPaolo Bonzini * RISC-V instruction emitters
364139c1837SPaolo Bonzini */
365139c1837SPaolo Bonzini
366139c1837SPaolo Bonzinistatic void tcg_out_opc_reg(TCGContext *s, RISCVInsn opc,
367139c1837SPaolo Bonzini                            TCGReg rd, TCGReg rs1, TCGReg rs2)
368139c1837SPaolo Bonzini{
369139c1837SPaolo Bonzini    tcg_out32(s, encode_r(opc, rd, rs1, rs2));
370139c1837SPaolo Bonzini}
371139c1837SPaolo Bonzini
372139c1837SPaolo Bonzinistatic void tcg_out_opc_imm(TCGContext *s, RISCVInsn opc,
373139c1837SPaolo Bonzini                            TCGReg rd, TCGReg rs1, TCGArg imm)
374139c1837SPaolo Bonzini{
375139c1837SPaolo Bonzini    tcg_out32(s, encode_i(opc, rd, rs1, imm));
376139c1837SPaolo Bonzini}
377139c1837SPaolo Bonzini
378139c1837SPaolo Bonzinistatic void tcg_out_opc_store(TCGContext *s, RISCVInsn opc,
379139c1837SPaolo Bonzini                              TCGReg rs1, TCGReg rs2, uint32_t imm)
380139c1837SPaolo Bonzini{
381139c1837SPaolo Bonzini    tcg_out32(s, encode_s(opc, rs1, rs2, imm));
382139c1837SPaolo Bonzini}
383139c1837SPaolo Bonzini
384139c1837SPaolo Bonzinistatic void tcg_out_opc_branch(TCGContext *s, RISCVInsn opc,
385139c1837SPaolo Bonzini                               TCGReg rs1, TCGReg rs2, uint32_t imm)
386139c1837SPaolo Bonzini{
387139c1837SPaolo Bonzini    tcg_out32(s, encode_sb(opc, rs1, rs2, imm));
388139c1837SPaolo Bonzini}
389139c1837SPaolo Bonzini
390139c1837SPaolo Bonzinistatic void tcg_out_opc_upper(TCGContext *s, RISCVInsn opc,
391139c1837SPaolo Bonzini                              TCGReg rd, uint32_t imm)
392139c1837SPaolo Bonzini{
393139c1837SPaolo Bonzini    tcg_out32(s, encode_u(opc, rd, imm));
394139c1837SPaolo Bonzini}
395139c1837SPaolo Bonzini
396139c1837SPaolo Bonzinistatic void tcg_out_opc_jump(TCGContext *s, RISCVInsn opc,
397139c1837SPaolo Bonzini                             TCGReg rd, uint32_t imm)
398139c1837SPaolo Bonzini{
399139c1837SPaolo Bonzini    tcg_out32(s, encode_uj(opc, rd, imm));
400139c1837SPaolo Bonzini}
401139c1837SPaolo Bonzini
402139c1837SPaolo Bonzinistatic void tcg_out_nop_fill(tcg_insn_unit *p, int count)
403139c1837SPaolo Bonzini{
404139c1837SPaolo Bonzini    int i;
405139c1837SPaolo Bonzini    for (i = 0; i < count; ++i) {
406139c1837SPaolo Bonzini        p[i] = encode_i(OPC_ADDI, TCG_REG_ZERO, TCG_REG_ZERO, 0);
407139c1837SPaolo Bonzini    }
408139c1837SPaolo Bonzini}
409139c1837SPaolo Bonzini
410139c1837SPaolo Bonzini/*
411139c1837SPaolo Bonzini * Relocations
412139c1837SPaolo Bonzini */
413139c1837SPaolo Bonzini
414793f7381SRichard Hendersonstatic bool reloc_sbimm12(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
415139c1837SPaolo Bonzini{
416793f7381SRichard Henderson    const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
417793f7381SRichard Henderson    intptr_t offset = (intptr_t)target - (intptr_t)src_rx;
418139c1837SPaolo Bonzini
419844d0442SRichard Henderson    tcg_debug_assert((offset & 1) == 0);
420844d0442SRichard Henderson    if (offset == sextreg(offset, 0, 12)) {
421793f7381SRichard Henderson        *src_rw |= encode_sbimm12(offset);
422139c1837SPaolo Bonzini        return true;
423139c1837SPaolo Bonzini    }
424139c1837SPaolo Bonzini
425139c1837SPaolo Bonzini    return false;
426139c1837SPaolo Bonzini}
427139c1837SPaolo Bonzini
428793f7381SRichard Hendersonstatic bool reloc_jimm20(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
429139c1837SPaolo Bonzini{
430793f7381SRichard Henderson    const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
431793f7381SRichard Henderson    intptr_t offset = (intptr_t)target - (intptr_t)src_rx;
432139c1837SPaolo Bonzini
433844d0442SRichard Henderson    tcg_debug_assert((offset & 1) == 0);
434844d0442SRichard Henderson    if (offset == sextreg(offset, 0, 20)) {
435793f7381SRichard Henderson        *src_rw |= encode_ujimm20(offset);
436139c1837SPaolo Bonzini        return true;
437139c1837SPaolo Bonzini    }
438139c1837SPaolo Bonzini
439139c1837SPaolo Bonzini    return false;
440139c1837SPaolo Bonzini}
441139c1837SPaolo Bonzini
442793f7381SRichard Hendersonstatic bool reloc_call(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
443139c1837SPaolo Bonzini{
444793f7381SRichard Henderson    const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
445793f7381SRichard Henderson    intptr_t offset = (intptr_t)target - (intptr_t)src_rx;
446139c1837SPaolo Bonzini    int32_t lo = sextreg(offset, 0, 12);
447139c1837SPaolo Bonzini    int32_t hi = offset - lo;
448139c1837SPaolo Bonzini
449139c1837SPaolo Bonzini    if (offset == hi + lo) {
450793f7381SRichard Henderson        src_rw[0] |= encode_uimm20(hi);
451793f7381SRichard Henderson        src_rw[1] |= encode_imm12(lo);
452139c1837SPaolo Bonzini        return true;
453139c1837SPaolo Bonzini    }
454139c1837SPaolo Bonzini
455139c1837SPaolo Bonzini    return false;
456139c1837SPaolo Bonzini}
457139c1837SPaolo Bonzini
458139c1837SPaolo Bonzinistatic bool patch_reloc(tcg_insn_unit *code_ptr, int type,
459139c1837SPaolo Bonzini                        intptr_t value, intptr_t addend)
460139c1837SPaolo Bonzini{
461139c1837SPaolo Bonzini    tcg_debug_assert(addend == 0);
462139c1837SPaolo Bonzini    switch (type) {
463139c1837SPaolo Bonzini    case R_RISCV_BRANCH:
464139c1837SPaolo Bonzini        return reloc_sbimm12(code_ptr, (tcg_insn_unit *)value);
465139c1837SPaolo Bonzini    case R_RISCV_JAL:
466139c1837SPaolo Bonzini        return reloc_jimm20(code_ptr, (tcg_insn_unit *)value);
467139c1837SPaolo Bonzini    case R_RISCV_CALL:
468139c1837SPaolo Bonzini        return reloc_call(code_ptr, (tcg_insn_unit *)value);
469139c1837SPaolo Bonzini    default:
4704b6a52d0SRichard Henderson        g_assert_not_reached();
471139c1837SPaolo Bonzini    }
472139c1837SPaolo Bonzini}
473139c1837SPaolo Bonzini
474139c1837SPaolo Bonzini/*
475139c1837SPaolo Bonzini * TCG intrinsics
476139c1837SPaolo Bonzini */
477139c1837SPaolo Bonzini
478139c1837SPaolo Bonzinistatic bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
479139c1837SPaolo Bonzini{
480139c1837SPaolo Bonzini    if (ret == arg) {
481139c1837SPaolo Bonzini        return true;
482139c1837SPaolo Bonzini    }
483139c1837SPaolo Bonzini    switch (type) {
484139c1837SPaolo Bonzini    case TCG_TYPE_I32:
485139c1837SPaolo Bonzini    case TCG_TYPE_I64:
486139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_ADDI, ret, arg, 0);
487139c1837SPaolo Bonzini        break;
488139c1837SPaolo Bonzini    default:
489139c1837SPaolo Bonzini        g_assert_not_reached();
490139c1837SPaolo Bonzini    }
491139c1837SPaolo Bonzini    return true;
492139c1837SPaolo Bonzini}
493139c1837SPaolo Bonzini
494139c1837SPaolo Bonzinistatic void tcg_out_movi(TCGContext *s, TCGType type, TCGReg rd,
495139c1837SPaolo Bonzini                         tcg_target_long val)
496139c1837SPaolo Bonzini{
497139c1837SPaolo Bonzini    tcg_target_long lo, hi, tmp;
498139c1837SPaolo Bonzini    int shift, ret;
499139c1837SPaolo Bonzini
500139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I32) {
501139c1837SPaolo Bonzini        val = (int32_t)val;
502139c1837SPaolo Bonzini    }
503139c1837SPaolo Bonzini
504139c1837SPaolo Bonzini    lo = sextreg(val, 0, 12);
505139c1837SPaolo Bonzini    if (val == lo) {
506139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_ADDI, rd, TCG_REG_ZERO, lo);
507139c1837SPaolo Bonzini        return;
508139c1837SPaolo Bonzini    }
509139c1837SPaolo Bonzini
510139c1837SPaolo Bonzini    hi = val - lo;
511139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS == 32 || val == (int32_t)val) {
512139c1837SPaolo Bonzini        tcg_out_opc_upper(s, OPC_LUI, rd, hi);
513139c1837SPaolo Bonzini        if (lo != 0) {
514139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_ADDIW, rd, rd, lo);
515139c1837SPaolo Bonzini        }
516139c1837SPaolo Bonzini        return;
517139c1837SPaolo Bonzini    }
518139c1837SPaolo Bonzini
519139c1837SPaolo Bonzini    /* We can only be here if TCG_TARGET_REG_BITS != 32 */
520139c1837SPaolo Bonzini    tmp = tcg_pcrel_diff(s, (void *)val);
521139c1837SPaolo Bonzini    if (tmp == (int32_t)tmp) {
522139c1837SPaolo Bonzini        tcg_out_opc_upper(s, OPC_AUIPC, rd, 0);
523139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_ADDI, rd, rd, 0);
524793f7381SRichard Henderson        ret = reloc_call(s->code_ptr - 2, (const tcg_insn_unit *)val);
525139c1837SPaolo Bonzini        tcg_debug_assert(ret == true);
526139c1837SPaolo Bonzini        return;
527139c1837SPaolo Bonzini    }
528139c1837SPaolo Bonzini
529139c1837SPaolo Bonzini    /* Look for a single 20-bit section.  */
530139c1837SPaolo Bonzini    shift = ctz64(val);
531139c1837SPaolo Bonzini    tmp = val >> shift;
532139c1837SPaolo Bonzini    if (tmp == sextreg(tmp, 0, 20)) {
533139c1837SPaolo Bonzini        tcg_out_opc_upper(s, OPC_LUI, rd, tmp << 12);
534139c1837SPaolo Bonzini        if (shift > 12) {
535139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_SLLI, rd, rd, shift - 12);
536139c1837SPaolo Bonzini        } else {
537139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_SRAI, rd, rd, 12 - shift);
538139c1837SPaolo Bonzini        }
539139c1837SPaolo Bonzini        return;
540139c1837SPaolo Bonzini    }
541139c1837SPaolo Bonzini
542139c1837SPaolo Bonzini    /* Look for a few high zero bits, with lots of bits set in the middle.  */
543139c1837SPaolo Bonzini    shift = clz64(val);
544139c1837SPaolo Bonzini    tmp = val << shift;
545139c1837SPaolo Bonzini    if (tmp == sextreg(tmp, 12, 20) << 12) {
546139c1837SPaolo Bonzini        tcg_out_opc_upper(s, OPC_LUI, rd, tmp);
547139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_SRLI, rd, rd, shift);
548139c1837SPaolo Bonzini        return;
549139c1837SPaolo Bonzini    } else if (tmp == sextreg(tmp, 0, 12)) {
550139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_ADDI, rd, TCG_REG_ZERO, tmp);
551139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_SRLI, rd, rd, shift);
552139c1837SPaolo Bonzini        return;
553139c1837SPaolo Bonzini    }
554139c1837SPaolo Bonzini
555139c1837SPaolo Bonzini    /* Drop into the constant pool.  */
556139c1837SPaolo Bonzini    new_pool_label(s, val, R_RISCV_CALL, s->code_ptr, 0);
557139c1837SPaolo Bonzini    tcg_out_opc_upper(s, OPC_AUIPC, rd, 0);
558139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_LD, rd, rd, 0);
559139c1837SPaolo Bonzini}
560139c1837SPaolo Bonzini
561139c1837SPaolo Bonzinistatic void tcg_out_ext8u(TCGContext *s, TCGReg ret, TCGReg arg)
562139c1837SPaolo Bonzini{
563139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_ANDI, ret, arg, 0xff);
564139c1837SPaolo Bonzini}
565139c1837SPaolo Bonzini
566139c1837SPaolo Bonzinistatic void tcg_out_ext16u(TCGContext *s, TCGReg ret, TCGReg arg)
567139c1837SPaolo Bonzini{
568139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SLLIW, ret, arg, 16);
569139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SRLIW, ret, ret, 16);
570139c1837SPaolo Bonzini}
571139c1837SPaolo Bonzini
572139c1837SPaolo Bonzinistatic void tcg_out_ext32u(TCGContext *s, TCGReg ret, TCGReg arg)
573139c1837SPaolo Bonzini{
574139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SLLI, ret, arg, 32);
575139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SRLI, ret, ret, 32);
576139c1837SPaolo Bonzini}
577139c1837SPaolo Bonzini
578139c1837SPaolo Bonzinistatic void tcg_out_ext8s(TCGContext *s, TCGReg ret, TCGReg arg)
579139c1837SPaolo Bonzini{
580139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SLLIW, ret, arg, 24);
581139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SRAIW, ret, ret, 24);
582139c1837SPaolo Bonzini}
583139c1837SPaolo Bonzini
584139c1837SPaolo Bonzinistatic void tcg_out_ext16s(TCGContext *s, TCGReg ret, TCGReg arg)
585139c1837SPaolo Bonzini{
586139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SLLIW, ret, arg, 16);
587139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SRAIW, ret, ret, 16);
588139c1837SPaolo Bonzini}
589139c1837SPaolo Bonzini
590139c1837SPaolo Bonzinistatic void tcg_out_ext32s(TCGContext *s, TCGReg ret, TCGReg arg)
591139c1837SPaolo Bonzini{
592139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_ADDIW, ret, arg, 0);
593139c1837SPaolo Bonzini}
594139c1837SPaolo Bonzini
595139c1837SPaolo Bonzinistatic void tcg_out_ldst(TCGContext *s, RISCVInsn opc, TCGReg data,
596139c1837SPaolo Bonzini                         TCGReg addr, intptr_t offset)
597139c1837SPaolo Bonzini{
598139c1837SPaolo Bonzini    intptr_t imm12 = sextreg(offset, 0, 12);
599139c1837SPaolo Bonzini
600139c1837SPaolo Bonzini    if (offset != imm12) {
601139c1837SPaolo Bonzini        intptr_t diff = offset - (uintptr_t)s->code_ptr;
602139c1837SPaolo Bonzini
603139c1837SPaolo Bonzini        if (addr == TCG_REG_ZERO && diff == (int32_t)diff) {
604139c1837SPaolo Bonzini            imm12 = sextreg(diff, 0, 12);
605139c1837SPaolo Bonzini            tcg_out_opc_upper(s, OPC_AUIPC, TCG_REG_TMP2, diff - imm12);
606139c1837SPaolo Bonzini        } else {
607139c1837SPaolo Bonzini            tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP2, offset - imm12);
608139c1837SPaolo Bonzini            if (addr != TCG_REG_ZERO) {
609139c1837SPaolo Bonzini                tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP2, TCG_REG_TMP2, addr);
610139c1837SPaolo Bonzini            }
611139c1837SPaolo Bonzini        }
612139c1837SPaolo Bonzini        addr = TCG_REG_TMP2;
613139c1837SPaolo Bonzini    }
614139c1837SPaolo Bonzini
615139c1837SPaolo Bonzini    switch (opc) {
616139c1837SPaolo Bonzini    case OPC_SB:
617139c1837SPaolo Bonzini    case OPC_SH:
618139c1837SPaolo Bonzini    case OPC_SW:
619139c1837SPaolo Bonzini    case OPC_SD:
620139c1837SPaolo Bonzini        tcg_out_opc_store(s, opc, addr, data, imm12);
621139c1837SPaolo Bonzini        break;
622139c1837SPaolo Bonzini    case OPC_LB:
623139c1837SPaolo Bonzini    case OPC_LBU:
624139c1837SPaolo Bonzini    case OPC_LH:
625139c1837SPaolo Bonzini    case OPC_LHU:
626139c1837SPaolo Bonzini    case OPC_LW:
627139c1837SPaolo Bonzini    case OPC_LWU:
628139c1837SPaolo Bonzini    case OPC_LD:
629139c1837SPaolo Bonzini        tcg_out_opc_imm(s, opc, data, addr, imm12);
630139c1837SPaolo Bonzini        break;
631139c1837SPaolo Bonzini    default:
632139c1837SPaolo Bonzini        g_assert_not_reached();
633139c1837SPaolo Bonzini    }
634139c1837SPaolo Bonzini}
635139c1837SPaolo Bonzini
636139c1837SPaolo Bonzinistatic void tcg_out_ld(TCGContext *s, TCGType type, TCGReg arg,
637139c1837SPaolo Bonzini                       TCGReg arg1, intptr_t arg2)
638139c1837SPaolo Bonzini{
639139c1837SPaolo Bonzini    bool is32bit = (TCG_TARGET_REG_BITS == 32 || type == TCG_TYPE_I32);
640139c1837SPaolo Bonzini    tcg_out_ldst(s, is32bit ? OPC_LW : OPC_LD, arg, arg1, arg2);
641139c1837SPaolo Bonzini}
642139c1837SPaolo Bonzini
643139c1837SPaolo Bonzinistatic void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
644139c1837SPaolo Bonzini                       TCGReg arg1, intptr_t arg2)
645139c1837SPaolo Bonzini{
646139c1837SPaolo Bonzini    bool is32bit = (TCG_TARGET_REG_BITS == 32 || type == TCG_TYPE_I32);
647139c1837SPaolo Bonzini    tcg_out_ldst(s, is32bit ? OPC_SW : OPC_SD, arg, arg1, arg2);
648139c1837SPaolo Bonzini}
649139c1837SPaolo Bonzini
650139c1837SPaolo Bonzinistatic bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
651139c1837SPaolo Bonzini                        TCGReg base, intptr_t ofs)
652139c1837SPaolo Bonzini{
653139c1837SPaolo Bonzini    if (val == 0) {
654139c1837SPaolo Bonzini        tcg_out_st(s, type, TCG_REG_ZERO, base, ofs);
655139c1837SPaolo Bonzini        return true;
656139c1837SPaolo Bonzini    }
657139c1837SPaolo Bonzini    return false;
658139c1837SPaolo Bonzini}
659139c1837SPaolo Bonzini
660139c1837SPaolo Bonzinistatic void tcg_out_addsub2(TCGContext *s,
661139c1837SPaolo Bonzini                            TCGReg rl, TCGReg rh,
662139c1837SPaolo Bonzini                            TCGReg al, TCGReg ah,
663139c1837SPaolo Bonzini                            TCGArg bl, TCGArg bh,
664139c1837SPaolo Bonzini                            bool cbl, bool cbh, bool is_sub, bool is32bit)
665139c1837SPaolo Bonzini{
666139c1837SPaolo Bonzini    const RISCVInsn opc_add = is32bit ? OPC_ADDW : OPC_ADD;
667139c1837SPaolo Bonzini    const RISCVInsn opc_addi = is32bit ? OPC_ADDIW : OPC_ADDI;
668139c1837SPaolo Bonzini    const RISCVInsn opc_sub = is32bit ? OPC_SUBW : OPC_SUB;
669139c1837SPaolo Bonzini    TCGReg th = TCG_REG_TMP1;
670139c1837SPaolo Bonzini
671139c1837SPaolo Bonzini    /* If we have a negative constant such that negating it would
672139c1837SPaolo Bonzini       make the high part zero, we can (usually) eliminate one insn.  */
673139c1837SPaolo Bonzini    if (cbl && cbh && bh == -1 && bl != 0) {
674139c1837SPaolo Bonzini        bl = -bl;
675139c1837SPaolo Bonzini        bh = 0;
676139c1837SPaolo Bonzini        is_sub = !is_sub;
677139c1837SPaolo Bonzini    }
678139c1837SPaolo Bonzini
679139c1837SPaolo Bonzini    /* By operating on the high part first, we get to use the final
680139c1837SPaolo Bonzini       carry operation to move back from the temporary.  */
681139c1837SPaolo Bonzini    if (!cbh) {
682139c1837SPaolo Bonzini        tcg_out_opc_reg(s, (is_sub ? opc_sub : opc_add), th, ah, bh);
683139c1837SPaolo Bonzini    } else if (bh != 0 || ah == rl) {
684139c1837SPaolo Bonzini        tcg_out_opc_imm(s, opc_addi, th, ah, (is_sub ? -bh : bh));
685139c1837SPaolo Bonzini    } else {
686139c1837SPaolo Bonzini        th = ah;
687139c1837SPaolo Bonzini    }
688139c1837SPaolo Bonzini
689139c1837SPaolo Bonzini    /* Note that tcg optimization should eliminate the bl == 0 case.  */
690139c1837SPaolo Bonzini    if (is_sub) {
691139c1837SPaolo Bonzini        if (cbl) {
692139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_SLTIU, TCG_REG_TMP0, al, bl);
693139c1837SPaolo Bonzini            tcg_out_opc_imm(s, opc_addi, rl, al, -bl);
694139c1837SPaolo Bonzini        } else {
695139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SLTU, TCG_REG_TMP0, al, bl);
696139c1837SPaolo Bonzini            tcg_out_opc_reg(s, opc_sub, rl, al, bl);
697139c1837SPaolo Bonzini        }
698139c1837SPaolo Bonzini        tcg_out_opc_reg(s, opc_sub, rh, th, TCG_REG_TMP0);
699139c1837SPaolo Bonzini    } else {
700139c1837SPaolo Bonzini        if (cbl) {
701139c1837SPaolo Bonzini            tcg_out_opc_imm(s, opc_addi, rl, al, bl);
702139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_SLTIU, TCG_REG_TMP0, rl, bl);
7039b246685SRichard Henderson        } else if (al == bl) {
7049b246685SRichard Henderson            /*
7059b246685SRichard Henderson             * If the input regs overlap, this is a simple doubling
7069b246685SRichard Henderson             * and carry-out is the input msb.  This special case is
7079b246685SRichard Henderson             * required when the output reg overlaps the input,
7089b246685SRichard Henderson             * but we might as well use it always.
7099b246685SRichard Henderson             */
710139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_SLTI, TCG_REG_TMP0, al, 0);
7119b246685SRichard Henderson            tcg_out_opc_reg(s, opc_add, rl, al, al);
712139c1837SPaolo Bonzini        } else {
713139c1837SPaolo Bonzini            tcg_out_opc_reg(s, opc_add, rl, al, bl);
714139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SLTU, TCG_REG_TMP0,
715139c1837SPaolo Bonzini                            rl, (rl == bl ? al : bl));
716139c1837SPaolo Bonzini        }
717139c1837SPaolo Bonzini        tcg_out_opc_reg(s, opc_add, rh, th, TCG_REG_TMP0);
718139c1837SPaolo Bonzini    }
719139c1837SPaolo Bonzini}
720139c1837SPaolo Bonzini
721139c1837SPaolo Bonzinistatic const struct {
722139c1837SPaolo Bonzini    RISCVInsn op;
723139c1837SPaolo Bonzini    bool swap;
724139c1837SPaolo Bonzini} tcg_brcond_to_riscv[] = {
725139c1837SPaolo Bonzini    [TCG_COND_EQ] =  { OPC_BEQ,  false },
726139c1837SPaolo Bonzini    [TCG_COND_NE] =  { OPC_BNE,  false },
727139c1837SPaolo Bonzini    [TCG_COND_LT] =  { OPC_BLT,  false },
728139c1837SPaolo Bonzini    [TCG_COND_GE] =  { OPC_BGE,  false },
729139c1837SPaolo Bonzini    [TCG_COND_LE] =  { OPC_BGE,  true  },
730139c1837SPaolo Bonzini    [TCG_COND_GT] =  { OPC_BLT,  true  },
731139c1837SPaolo Bonzini    [TCG_COND_LTU] = { OPC_BLTU, false },
732139c1837SPaolo Bonzini    [TCG_COND_GEU] = { OPC_BGEU, false },
733139c1837SPaolo Bonzini    [TCG_COND_LEU] = { OPC_BGEU, true  },
734139c1837SPaolo Bonzini    [TCG_COND_GTU] = { OPC_BLTU, true  }
735139c1837SPaolo Bonzini};
736139c1837SPaolo Bonzini
737139c1837SPaolo Bonzinistatic void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1,
738139c1837SPaolo Bonzini                           TCGReg arg2, TCGLabel *l)
739139c1837SPaolo Bonzini{
740139c1837SPaolo Bonzini    RISCVInsn op = tcg_brcond_to_riscv[cond].op;
741139c1837SPaolo Bonzini
742139c1837SPaolo Bonzini    tcg_debug_assert(op != 0);
743139c1837SPaolo Bonzini
744139c1837SPaolo Bonzini    if (tcg_brcond_to_riscv[cond].swap) {
745139c1837SPaolo Bonzini        TCGReg t = arg1;
746139c1837SPaolo Bonzini        arg1 = arg2;
747139c1837SPaolo Bonzini        arg2 = t;
748139c1837SPaolo Bonzini    }
749139c1837SPaolo Bonzini
750139c1837SPaolo Bonzini    tcg_out_reloc(s, s->code_ptr, R_RISCV_BRANCH, l, 0);
751139c1837SPaolo Bonzini    tcg_out_opc_branch(s, op, arg1, arg2, 0);
752139c1837SPaolo Bonzini}
753139c1837SPaolo Bonzini
754139c1837SPaolo Bonzinistatic void tcg_out_setcond(TCGContext *s, TCGCond cond, TCGReg ret,
755139c1837SPaolo Bonzini                            TCGReg arg1, TCGReg arg2)
756139c1837SPaolo Bonzini{
757139c1837SPaolo Bonzini    switch (cond) {
758139c1837SPaolo Bonzini    case TCG_COND_EQ:
759139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SUB, ret, arg1, arg2);
760139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_SLTIU, ret, ret, 1);
761139c1837SPaolo Bonzini        break;
762139c1837SPaolo Bonzini    case TCG_COND_NE:
763139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SUB, ret, arg1, arg2);
764139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLTU, ret, TCG_REG_ZERO, ret);
765139c1837SPaolo Bonzini        break;
766139c1837SPaolo Bonzini    case TCG_COND_LT:
767139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLT, ret, arg1, arg2);
768139c1837SPaolo Bonzini        break;
769139c1837SPaolo Bonzini    case TCG_COND_GE:
770139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLT, ret, arg1, arg2);
771139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1);
772139c1837SPaolo Bonzini        break;
773139c1837SPaolo Bonzini    case TCG_COND_LE:
774139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLT, ret, arg2, arg1);
775139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1);
776139c1837SPaolo Bonzini        break;
777139c1837SPaolo Bonzini    case TCG_COND_GT:
778139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLT, ret, arg2, arg1);
779139c1837SPaolo Bonzini        break;
780139c1837SPaolo Bonzini    case TCG_COND_LTU:
781139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLTU, ret, arg1, arg2);
782139c1837SPaolo Bonzini        break;
783139c1837SPaolo Bonzini    case TCG_COND_GEU:
784139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLTU, ret, arg1, arg2);
785139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1);
786139c1837SPaolo Bonzini        break;
787139c1837SPaolo Bonzini    case TCG_COND_LEU:
788139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLTU, ret, arg2, arg1);
789139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1);
790139c1837SPaolo Bonzini        break;
791139c1837SPaolo Bonzini    case TCG_COND_GTU:
792139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SLTU, ret, arg2, arg1);
793139c1837SPaolo Bonzini        break;
794139c1837SPaolo Bonzini    default:
795139c1837SPaolo Bonzini         g_assert_not_reached();
796139c1837SPaolo Bonzini         break;
797139c1837SPaolo Bonzini     }
798139c1837SPaolo Bonzini}
799139c1837SPaolo Bonzini
800139c1837SPaolo Bonzinistatic void tcg_out_brcond2(TCGContext *s, TCGCond cond, TCGReg al, TCGReg ah,
801139c1837SPaolo Bonzini                            TCGReg bl, TCGReg bh, TCGLabel *l)
802139c1837SPaolo Bonzini{
803139c1837SPaolo Bonzini    /* todo */
804139c1837SPaolo Bonzini    g_assert_not_reached();
805139c1837SPaolo Bonzini}
806139c1837SPaolo Bonzini
807139c1837SPaolo Bonzinistatic void tcg_out_setcond2(TCGContext *s, TCGCond cond, TCGReg ret,
808139c1837SPaolo Bonzini                             TCGReg al, TCGReg ah, TCGReg bl, TCGReg bh)
809139c1837SPaolo Bonzini{
810139c1837SPaolo Bonzini    /* todo */
811139c1837SPaolo Bonzini    g_assert_not_reached();
812139c1837SPaolo Bonzini}
813139c1837SPaolo Bonzini
8142be7d76bSRichard Hendersonstatic void tcg_out_call_int(TCGContext *s, const tcg_insn_unit *arg, bool tail)
815139c1837SPaolo Bonzini{
816139c1837SPaolo Bonzini    TCGReg link = tail ? TCG_REG_ZERO : TCG_REG_RA;
817139c1837SPaolo Bonzini    ptrdiff_t offset = tcg_pcrel_diff(s, arg);
818139c1837SPaolo Bonzini    int ret;
819139c1837SPaolo Bonzini
820844d0442SRichard Henderson    tcg_debug_assert((offset & 1) == 0);
821844d0442SRichard Henderson    if (offset == sextreg(offset, 0, 20)) {
822139c1837SPaolo Bonzini        /* short jump: -2097150 to 2097152 */
823139c1837SPaolo Bonzini        tcg_out_opc_jump(s, OPC_JAL, link, offset);
824844d0442SRichard Henderson    } else if (TCG_TARGET_REG_BITS == 32 || offset == (int32_t)offset) {
825139c1837SPaolo Bonzini        /* long jump: -2147483646 to 2147483648 */
826139c1837SPaolo Bonzini        tcg_out_opc_upper(s, OPC_AUIPC, TCG_REG_TMP0, 0);
827139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_JALR, link, TCG_REG_TMP0, 0);
828844d0442SRichard Henderson        ret = reloc_call(s->code_ptr - 2, arg);
829139c1837SPaolo Bonzini        tcg_debug_assert(ret == true);
830139c1837SPaolo Bonzini    } else if (TCG_TARGET_REG_BITS == 64) {
831139c1837SPaolo Bonzini        /* far jump: 64-bit */
832139c1837SPaolo Bonzini        tcg_target_long imm = sextreg((tcg_target_long)arg, 0, 12);
833139c1837SPaolo Bonzini        tcg_target_long base = (tcg_target_long)arg - imm;
834139c1837SPaolo Bonzini        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP0, base);
835139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_JALR, link, TCG_REG_TMP0, imm);
836139c1837SPaolo Bonzini    } else {
837139c1837SPaolo Bonzini        g_assert_not_reached();
838139c1837SPaolo Bonzini    }
839139c1837SPaolo Bonzini}
840139c1837SPaolo Bonzini
8412be7d76bSRichard Hendersonstatic void tcg_out_call(TCGContext *s, const tcg_insn_unit *arg)
842139c1837SPaolo Bonzini{
843139c1837SPaolo Bonzini    tcg_out_call_int(s, arg, false);
844139c1837SPaolo Bonzini}
845139c1837SPaolo Bonzini
846139c1837SPaolo Bonzinistatic void tcg_out_mb(TCGContext *s, TCGArg a0)
847139c1837SPaolo Bonzini{
848139c1837SPaolo Bonzini    tcg_insn_unit insn = OPC_FENCE;
849139c1837SPaolo Bonzini
850139c1837SPaolo Bonzini    if (a0 & TCG_MO_LD_LD) {
851139c1837SPaolo Bonzini        insn |= 0x02200000;
852139c1837SPaolo Bonzini    }
853139c1837SPaolo Bonzini    if (a0 & TCG_MO_ST_LD) {
854139c1837SPaolo Bonzini        insn |= 0x01200000;
855139c1837SPaolo Bonzini    }
856139c1837SPaolo Bonzini    if (a0 & TCG_MO_LD_ST) {
857139c1837SPaolo Bonzini        insn |= 0x02100000;
858139c1837SPaolo Bonzini    }
859139c1837SPaolo Bonzini    if (a0 & TCG_MO_ST_ST) {
860139c1837SPaolo Bonzini        insn |= 0x02200000;
861139c1837SPaolo Bonzini    }
862139c1837SPaolo Bonzini    tcg_out32(s, insn);
863139c1837SPaolo Bonzini}
864139c1837SPaolo Bonzini
865139c1837SPaolo Bonzini/*
866139c1837SPaolo Bonzini * Load/store and TLB
867139c1837SPaolo Bonzini */
868139c1837SPaolo Bonzini
869139c1837SPaolo Bonzini#if defined(CONFIG_SOFTMMU)
870139c1837SPaolo Bonzini/* helper signature: helper_ret_ld_mmu(CPUState *env, target_ulong addr,
8719002ffcbSRichard Henderson *                                     MemOpIdx oi, uintptr_t ra)
872139c1837SPaolo Bonzini */
8734b473e0cSRichard Hendersonstatic void * const qemu_ld_helpers[MO_SSIZE + 1] = {
874139c1837SPaolo Bonzini    [MO_UB] = helper_ret_ldub_mmu,
875139c1837SPaolo Bonzini    [MO_SB] = helper_ret_ldsb_mmu,
876e03b5686SMarc-André Lureau#if HOST_BIG_ENDIAN
877c86bd2dcSRichard Henderson    [MO_UW] = helper_be_lduw_mmu,
878c86bd2dcSRichard Henderson    [MO_SW] = helper_be_ldsw_mmu,
879c86bd2dcSRichard Henderson    [MO_UL] = helper_be_ldul_mmu,
880139c1837SPaolo Bonzini#if TCG_TARGET_REG_BITS == 64
881c86bd2dcSRichard Henderson    [MO_SL] = helper_be_ldsl_mmu,
882139c1837SPaolo Bonzini#endif
883fc313c64SFrédéric Pétrot    [MO_UQ] = helper_be_ldq_mmu,
884c86bd2dcSRichard Henderson#else
885c86bd2dcSRichard Henderson    [MO_UW] = helper_le_lduw_mmu,
886c86bd2dcSRichard Henderson    [MO_SW] = helper_le_ldsw_mmu,
887c86bd2dcSRichard Henderson    [MO_UL] = helper_le_ldul_mmu,
888139c1837SPaolo Bonzini#if TCG_TARGET_REG_BITS == 64
889c86bd2dcSRichard Henderson    [MO_SL] = helper_le_ldsl_mmu,
890139c1837SPaolo Bonzini#endif
891fc313c64SFrédéric Pétrot    [MO_UQ] = helper_le_ldq_mmu,
892c86bd2dcSRichard Henderson#endif
893139c1837SPaolo Bonzini};
894139c1837SPaolo Bonzini
895139c1837SPaolo Bonzini/* helper signature: helper_ret_st_mmu(CPUState *env, target_ulong addr,
8969002ffcbSRichard Henderson *                                     uintxx_t val, MemOpIdx oi,
897139c1837SPaolo Bonzini *                                     uintptr_t ra)
898139c1837SPaolo Bonzini */
8994b473e0cSRichard Hendersonstatic void * const qemu_st_helpers[MO_SIZE + 1] = {
900c86bd2dcSRichard Henderson    [MO_8]   = helper_ret_stb_mmu,
901e03b5686SMarc-André Lureau#if HOST_BIG_ENDIAN
902c86bd2dcSRichard Henderson    [MO_16] = helper_be_stw_mmu,
903c86bd2dcSRichard Henderson    [MO_32] = helper_be_stl_mmu,
904c86bd2dcSRichard Henderson    [MO_64] = helper_be_stq_mmu,
905c86bd2dcSRichard Henderson#else
906c86bd2dcSRichard Henderson    [MO_16] = helper_le_stw_mmu,
907c86bd2dcSRichard Henderson    [MO_32] = helper_le_stl_mmu,
908c86bd2dcSRichard Henderson    [MO_64] = helper_le_stq_mmu,
909c86bd2dcSRichard Henderson#endif
910139c1837SPaolo Bonzini};
911139c1837SPaolo Bonzini
912139c1837SPaolo Bonzini/* We don't support oversize guests */
913139c1837SPaolo BonziniQEMU_BUILD_BUG_ON(TCG_TARGET_REG_BITS < TARGET_LONG_BITS);
914139c1837SPaolo Bonzini
915139c1837SPaolo Bonzini/* We expect to use a 12-bit negative offset from ENV.  */
916139c1837SPaolo BonziniQEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) > 0);
917139c1837SPaolo BonziniQEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) < -(1 << 11));
918139c1837SPaolo Bonzini
919793f7381SRichard Hendersonstatic void tcg_out_goto(TCGContext *s, const tcg_insn_unit *target)
920844d0442SRichard Henderson{
921844d0442SRichard Henderson    tcg_out_opc_jump(s, OPC_JAL, TCG_REG_ZERO, 0);
922844d0442SRichard Henderson    bool ok = reloc_jimm20(s->code_ptr - 1, target);
923844d0442SRichard Henderson    tcg_debug_assert(ok);
924844d0442SRichard Henderson}
925844d0442SRichard Henderson
926*2e3a933aSRichard Hendersonstatic TCGReg tcg_out_tlb_load(TCGContext *s, TCGReg addrl,
9279002ffcbSRichard Henderson                               TCGReg addrh, MemOpIdx oi,
928139c1837SPaolo Bonzini                               tcg_insn_unit **label_ptr, bool is_load)
929139c1837SPaolo Bonzini{
930139c1837SPaolo Bonzini    MemOp opc = get_memop(oi);
931139c1837SPaolo Bonzini    unsigned s_bits = opc & MO_SIZE;
932139c1837SPaolo Bonzini    unsigned a_bits = get_alignment_bits(opc);
933139c1837SPaolo Bonzini    tcg_target_long compare_mask;
934139c1837SPaolo Bonzini    int mem_index = get_mmuidx(oi);
935139c1837SPaolo Bonzini    int fast_ofs = TLB_MASK_TABLE_OFS(mem_index);
936139c1837SPaolo Bonzini    int mask_ofs = fast_ofs + offsetof(CPUTLBDescFast, mask);
937139c1837SPaolo Bonzini    int table_ofs = fast_ofs + offsetof(CPUTLBDescFast, table);
938139c1837SPaolo Bonzini    TCGReg mask_base = TCG_AREG0, table_base = TCG_AREG0;
939139c1837SPaolo Bonzini
940139c1837SPaolo Bonzini    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP0, mask_base, mask_ofs);
941139c1837SPaolo Bonzini    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP1, table_base, table_ofs);
942139c1837SPaolo Bonzini
943139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_SRLI, TCG_REG_TMP2, addrl,
944139c1837SPaolo Bonzini                    TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS);
945139c1837SPaolo Bonzini    tcg_out_opc_reg(s, OPC_AND, TCG_REG_TMP2, TCG_REG_TMP2, TCG_REG_TMP0);
946139c1837SPaolo Bonzini    tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP2, TCG_REG_TMP2, TCG_REG_TMP1);
947139c1837SPaolo Bonzini
948139c1837SPaolo Bonzini    /* Load the tlb comparator and the addend.  */
949139c1837SPaolo Bonzini    tcg_out_ld(s, TCG_TYPE_TL, TCG_REG_TMP0, TCG_REG_TMP2,
950139c1837SPaolo Bonzini               is_load ? offsetof(CPUTLBEntry, addr_read)
951139c1837SPaolo Bonzini               : offsetof(CPUTLBEntry, addr_write));
952139c1837SPaolo Bonzini    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP2, TCG_REG_TMP2,
953139c1837SPaolo Bonzini               offsetof(CPUTLBEntry, addend));
954139c1837SPaolo Bonzini
955139c1837SPaolo Bonzini    /* We don't support unaligned accesses. */
956139c1837SPaolo Bonzini    if (a_bits < s_bits) {
957139c1837SPaolo Bonzini        a_bits = s_bits;
958139c1837SPaolo Bonzini    }
959139c1837SPaolo Bonzini    /* Clear the non-page, non-alignment bits from the address.  */
960139c1837SPaolo Bonzini    compare_mask = (tcg_target_long)TARGET_PAGE_MASK | ((1 << a_bits) - 1);
961139c1837SPaolo Bonzini    if (compare_mask == sextreg(compare_mask, 0, 12)) {
962139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_ANDI, TCG_REG_TMP1, addrl, compare_mask);
963139c1837SPaolo Bonzini    } else {
964139c1837SPaolo Bonzini        tcg_out_movi(s, TCG_TYPE_TL, TCG_REG_TMP1, compare_mask);
965139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_AND, TCG_REG_TMP1, TCG_REG_TMP1, addrl);
966139c1837SPaolo Bonzini    }
967139c1837SPaolo Bonzini
968139c1837SPaolo Bonzini    /* Compare masked address with the TLB entry. */
969139c1837SPaolo Bonzini    label_ptr[0] = s->code_ptr;
970139c1837SPaolo Bonzini    tcg_out_opc_branch(s, OPC_BNE, TCG_REG_TMP0, TCG_REG_TMP1, 0);
971139c1837SPaolo Bonzini
972139c1837SPaolo Bonzini    /* TLB Hit - translate address using addend.  */
973139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS > TARGET_LONG_BITS) {
974139c1837SPaolo Bonzini        tcg_out_ext32u(s, TCG_REG_TMP0, addrl);
975139c1837SPaolo Bonzini        addrl = TCG_REG_TMP0;
976139c1837SPaolo Bonzini    }
977139c1837SPaolo Bonzini    tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP0, TCG_REG_TMP2, addrl);
978*2e3a933aSRichard Henderson    return TCG_REG_TMP0;
979139c1837SPaolo Bonzini}
980139c1837SPaolo Bonzini
9819002ffcbSRichard Hendersonstatic void add_qemu_ldst_label(TCGContext *s, int is_ld, MemOpIdx oi,
982139c1837SPaolo Bonzini                                TCGType ext,
983139c1837SPaolo Bonzini                                TCGReg datalo, TCGReg datahi,
984139c1837SPaolo Bonzini                                TCGReg addrlo, TCGReg addrhi,
985139c1837SPaolo Bonzini                                void *raddr, tcg_insn_unit **label_ptr)
986139c1837SPaolo Bonzini{
987139c1837SPaolo Bonzini    TCGLabelQemuLdst *label = new_ldst_label(s);
988139c1837SPaolo Bonzini
989139c1837SPaolo Bonzini    label->is_ld = is_ld;
990139c1837SPaolo Bonzini    label->oi = oi;
991139c1837SPaolo Bonzini    label->type = ext;
992139c1837SPaolo Bonzini    label->datalo_reg = datalo;
993139c1837SPaolo Bonzini    label->datahi_reg = datahi;
994139c1837SPaolo Bonzini    label->addrlo_reg = addrlo;
995139c1837SPaolo Bonzini    label->addrhi_reg = addrhi;
996e5e2e4c7SRichard Henderson    label->raddr = tcg_splitwx_to_rx(raddr);
997139c1837SPaolo Bonzini    label->label_ptr[0] = label_ptr[0];
998139c1837SPaolo Bonzini}
999139c1837SPaolo Bonzini
1000139c1837SPaolo Bonzinistatic bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
1001139c1837SPaolo Bonzini{
10029002ffcbSRichard Henderson    MemOpIdx oi = l->oi;
1003139c1837SPaolo Bonzini    MemOp opc = get_memop(oi);
1004139c1837SPaolo Bonzini    TCGReg a0 = tcg_target_call_iarg_regs[0];
1005139c1837SPaolo Bonzini    TCGReg a1 = tcg_target_call_iarg_regs[1];
1006139c1837SPaolo Bonzini    TCGReg a2 = tcg_target_call_iarg_regs[2];
1007139c1837SPaolo Bonzini    TCGReg a3 = tcg_target_call_iarg_regs[3];
1008139c1837SPaolo Bonzini
1009139c1837SPaolo Bonzini    /* We don't support oversize guests */
1010139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS < TARGET_LONG_BITS) {
1011139c1837SPaolo Bonzini        g_assert_not_reached();
1012139c1837SPaolo Bonzini    }
1013139c1837SPaolo Bonzini
1014139c1837SPaolo Bonzini    /* resolve label address */
1015793f7381SRichard Henderson    if (!reloc_sbimm12(l->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
1016139c1837SPaolo Bonzini        return false;
1017139c1837SPaolo Bonzini    }
1018139c1837SPaolo Bonzini
1019139c1837SPaolo Bonzini    /* call load helper */
1020139c1837SPaolo Bonzini    tcg_out_mov(s, TCG_TYPE_PTR, a0, TCG_AREG0);
1021139c1837SPaolo Bonzini    tcg_out_mov(s, TCG_TYPE_PTR, a1, l->addrlo_reg);
1022139c1837SPaolo Bonzini    tcg_out_movi(s, TCG_TYPE_PTR, a2, oi);
1023139c1837SPaolo Bonzini    tcg_out_movi(s, TCG_TYPE_PTR, a3, (tcg_target_long)l->raddr);
1024139c1837SPaolo Bonzini
1025c86bd2dcSRichard Henderson    tcg_out_call(s, qemu_ld_helpers[opc & MO_SSIZE]);
1026139c1837SPaolo Bonzini    tcg_out_mov(s, (opc & MO_SIZE) == MO_64, l->datalo_reg, a0);
1027139c1837SPaolo Bonzini
1028139c1837SPaolo Bonzini    tcg_out_goto(s, l->raddr);
1029139c1837SPaolo Bonzini    return true;
1030139c1837SPaolo Bonzini}
1031139c1837SPaolo Bonzini
1032139c1837SPaolo Bonzinistatic bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
1033139c1837SPaolo Bonzini{
10349002ffcbSRichard Henderson    MemOpIdx oi = l->oi;
1035139c1837SPaolo Bonzini    MemOp opc = get_memop(oi);
1036139c1837SPaolo Bonzini    MemOp s_bits = opc & MO_SIZE;
1037139c1837SPaolo Bonzini    TCGReg a0 = tcg_target_call_iarg_regs[0];
1038139c1837SPaolo Bonzini    TCGReg a1 = tcg_target_call_iarg_regs[1];
1039139c1837SPaolo Bonzini    TCGReg a2 = tcg_target_call_iarg_regs[2];
1040139c1837SPaolo Bonzini    TCGReg a3 = tcg_target_call_iarg_regs[3];
1041139c1837SPaolo Bonzini    TCGReg a4 = tcg_target_call_iarg_regs[4];
1042139c1837SPaolo Bonzini
1043139c1837SPaolo Bonzini    /* We don't support oversize guests */
1044139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS < TARGET_LONG_BITS) {
1045139c1837SPaolo Bonzini        g_assert_not_reached();
1046139c1837SPaolo Bonzini    }
1047139c1837SPaolo Bonzini
1048139c1837SPaolo Bonzini    /* resolve label address */
1049793f7381SRichard Henderson    if (!reloc_sbimm12(l->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
1050139c1837SPaolo Bonzini        return false;
1051139c1837SPaolo Bonzini    }
1052139c1837SPaolo Bonzini
1053139c1837SPaolo Bonzini    /* call store helper */
1054139c1837SPaolo Bonzini    tcg_out_mov(s, TCG_TYPE_PTR, a0, TCG_AREG0);
1055139c1837SPaolo Bonzini    tcg_out_mov(s, TCG_TYPE_PTR, a1, l->addrlo_reg);
1056139c1837SPaolo Bonzini    tcg_out_mov(s, TCG_TYPE_PTR, a2, l->datalo_reg);
1057139c1837SPaolo Bonzini    switch (s_bits) {
1058139c1837SPaolo Bonzini    case MO_8:
1059139c1837SPaolo Bonzini        tcg_out_ext8u(s, a2, a2);
1060139c1837SPaolo Bonzini        break;
1061139c1837SPaolo Bonzini    case MO_16:
1062139c1837SPaolo Bonzini        tcg_out_ext16u(s, a2, a2);
1063139c1837SPaolo Bonzini        break;
1064139c1837SPaolo Bonzini    default:
1065139c1837SPaolo Bonzini        break;
1066139c1837SPaolo Bonzini    }
1067139c1837SPaolo Bonzini    tcg_out_movi(s, TCG_TYPE_PTR, a3, oi);
1068139c1837SPaolo Bonzini    tcg_out_movi(s, TCG_TYPE_PTR, a4, (tcg_target_long)l->raddr);
1069139c1837SPaolo Bonzini
1070c86bd2dcSRichard Henderson    tcg_out_call(s, qemu_st_helpers[opc & MO_SIZE]);
1071139c1837SPaolo Bonzini
1072139c1837SPaolo Bonzini    tcg_out_goto(s, l->raddr);
1073139c1837SPaolo Bonzini    return true;
1074139c1837SPaolo Bonzini}
1075a3fb7c99SRichard Henderson#else
1076a3fb7c99SRichard Henderson
1077a3fb7c99SRichard Hendersonstatic void tcg_out_test_alignment(TCGContext *s, bool is_ld, TCGReg addr_reg,
1078a3fb7c99SRichard Henderson                                   unsigned a_bits)
1079a3fb7c99SRichard Henderson{
1080a3fb7c99SRichard Henderson    unsigned a_mask = (1 << a_bits) - 1;
1081a3fb7c99SRichard Henderson    TCGLabelQemuLdst *l = new_ldst_label(s);
1082a3fb7c99SRichard Henderson
1083a3fb7c99SRichard Henderson    l->is_ld = is_ld;
1084a3fb7c99SRichard Henderson    l->addrlo_reg = addr_reg;
1085a3fb7c99SRichard Henderson
1086a3fb7c99SRichard Henderson    /* We are expecting a_bits to max out at 7, so we can always use andi. */
1087a3fb7c99SRichard Henderson    tcg_debug_assert(a_bits < 12);
1088a3fb7c99SRichard Henderson    tcg_out_opc_imm(s, OPC_ANDI, TCG_REG_TMP1, addr_reg, a_mask);
1089a3fb7c99SRichard Henderson
1090a3fb7c99SRichard Henderson    l->label_ptr[0] = s->code_ptr;
1091a3fb7c99SRichard Henderson    tcg_out_opc_branch(s, OPC_BNE, TCG_REG_TMP1, TCG_REG_ZERO, 0);
1092a3fb7c99SRichard Henderson
1093a3fb7c99SRichard Henderson    l->raddr = tcg_splitwx_to_rx(s->code_ptr);
1094a3fb7c99SRichard Henderson}
1095a3fb7c99SRichard Henderson
1096a3fb7c99SRichard Hendersonstatic bool tcg_out_fail_alignment(TCGContext *s, TCGLabelQemuLdst *l)
1097a3fb7c99SRichard Henderson{
1098a3fb7c99SRichard Henderson    /* resolve label address */
1099a3fb7c99SRichard Henderson    if (!reloc_sbimm12(l->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
1100a3fb7c99SRichard Henderson        return false;
1101a3fb7c99SRichard Henderson    }
1102a3fb7c99SRichard Henderson
1103a3fb7c99SRichard Henderson    tcg_out_mov(s, TCG_TYPE_TL, TCG_REG_A1, l->addrlo_reg);
1104a3fb7c99SRichard Henderson    tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_A0, TCG_AREG0);
1105a3fb7c99SRichard Henderson
1106a3fb7c99SRichard Henderson    /* tail call, with the return address back inline. */
1107a3fb7c99SRichard Henderson    tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_RA, (uintptr_t)l->raddr);
1108a3fb7c99SRichard Henderson    tcg_out_call_int(s, (const void *)(l->is_ld ? helper_unaligned_ld
1109a3fb7c99SRichard Henderson                                       : helper_unaligned_st), true);
1110a3fb7c99SRichard Henderson    return true;
1111a3fb7c99SRichard Henderson}
1112a3fb7c99SRichard Henderson
1113a3fb7c99SRichard Hendersonstatic bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
1114a3fb7c99SRichard Henderson{
1115a3fb7c99SRichard Henderson    return tcg_out_fail_alignment(s, l);
1116a3fb7c99SRichard Henderson}
1117a3fb7c99SRichard Henderson
1118a3fb7c99SRichard Hendersonstatic bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
1119a3fb7c99SRichard Henderson{
1120a3fb7c99SRichard Henderson    return tcg_out_fail_alignment(s, l);
1121a3fb7c99SRichard Henderson}
1122a3fb7c99SRichard Henderson
1123139c1837SPaolo Bonzini#endif /* CONFIG_SOFTMMU */
1124139c1837SPaolo Bonzini
1125139c1837SPaolo Bonzinistatic void tcg_out_qemu_ld_direct(TCGContext *s, TCGReg lo, TCGReg hi,
1126139c1837SPaolo Bonzini                                   TCGReg base, MemOp opc, bool is_64)
1127139c1837SPaolo Bonzini{
1128c86bd2dcSRichard Henderson    /* Byte swapping is left to middle-end expansion. */
1129c86bd2dcSRichard Henderson    tcg_debug_assert((opc & MO_BSWAP) == 0);
1130139c1837SPaolo Bonzini
1131139c1837SPaolo Bonzini    switch (opc & (MO_SSIZE)) {
1132139c1837SPaolo Bonzini    case MO_UB:
1133139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_LBU, lo, base, 0);
1134139c1837SPaolo Bonzini        break;
1135139c1837SPaolo Bonzini    case MO_SB:
1136139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_LB, lo, base, 0);
1137139c1837SPaolo Bonzini        break;
1138139c1837SPaolo Bonzini    case MO_UW:
1139139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_LHU, lo, base, 0);
1140139c1837SPaolo Bonzini        break;
1141139c1837SPaolo Bonzini    case MO_SW:
1142139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_LH, lo, base, 0);
1143139c1837SPaolo Bonzini        break;
1144139c1837SPaolo Bonzini    case MO_UL:
1145139c1837SPaolo Bonzini        if (TCG_TARGET_REG_BITS == 64 && is_64) {
1146139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_LWU, lo, base, 0);
1147139c1837SPaolo Bonzini            break;
1148139c1837SPaolo Bonzini        }
1149139c1837SPaolo Bonzini        /* FALLTHRU */
1150139c1837SPaolo Bonzini    case MO_SL:
1151139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_LW, lo, base, 0);
1152139c1837SPaolo Bonzini        break;
1153fc313c64SFrédéric Pétrot    case MO_UQ:
1154139c1837SPaolo Bonzini        /* Prefer to load from offset 0 first, but allow for overlap.  */
1155139c1837SPaolo Bonzini        if (TCG_TARGET_REG_BITS == 64) {
1156139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_LD, lo, base, 0);
1157139c1837SPaolo Bonzini        } else if (lo != base) {
1158139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_LW, lo, base, 0);
1159139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_LW, hi, base, 4);
1160139c1837SPaolo Bonzini        } else {
1161139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_LW, hi, base, 4);
1162139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_LW, lo, base, 0);
1163139c1837SPaolo Bonzini        }
1164139c1837SPaolo Bonzini        break;
1165139c1837SPaolo Bonzini    default:
1166139c1837SPaolo Bonzini        g_assert_not_reached();
1167139c1837SPaolo Bonzini    }
1168139c1837SPaolo Bonzini}
1169139c1837SPaolo Bonzini
1170139c1837SPaolo Bonzinistatic void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args, bool is_64)
1171139c1837SPaolo Bonzini{
1172139c1837SPaolo Bonzini    TCGReg addr_regl, addr_regh __attribute__((unused));
1173139c1837SPaolo Bonzini    TCGReg data_regl, data_regh;
11749002ffcbSRichard Henderson    MemOpIdx oi;
1175139c1837SPaolo Bonzini    MemOp opc;
1176139c1837SPaolo Bonzini#if defined(CONFIG_SOFTMMU)
1177139c1837SPaolo Bonzini    tcg_insn_unit *label_ptr[1];
1178a3fb7c99SRichard Henderson#else
1179a3fb7c99SRichard Henderson    unsigned a_bits;
1180139c1837SPaolo Bonzini#endif
1181*2e3a933aSRichard Henderson    TCGReg base;
1182139c1837SPaolo Bonzini
1183139c1837SPaolo Bonzini    data_regl = *args++;
1184139c1837SPaolo Bonzini    data_regh = (TCG_TARGET_REG_BITS == 32 && is_64 ? *args++ : 0);
1185139c1837SPaolo Bonzini    addr_regl = *args++;
1186139c1837SPaolo Bonzini    addr_regh = (TCG_TARGET_REG_BITS < TARGET_LONG_BITS ? *args++ : 0);
1187139c1837SPaolo Bonzini    oi = *args++;
1188139c1837SPaolo Bonzini    opc = get_memop(oi);
1189139c1837SPaolo Bonzini
1190139c1837SPaolo Bonzini#if defined(CONFIG_SOFTMMU)
1191*2e3a933aSRichard Henderson    base = tcg_out_tlb_load(s, addr_regl, addr_regh, oi, label_ptr, 1);
1192139c1837SPaolo Bonzini    tcg_out_qemu_ld_direct(s, data_regl, data_regh, base, opc, is_64);
1193139c1837SPaolo Bonzini    add_qemu_ldst_label(s, 1, oi,
1194139c1837SPaolo Bonzini                        (is_64 ? TCG_TYPE_I64 : TCG_TYPE_I32),
1195139c1837SPaolo Bonzini                        data_regl, data_regh, addr_regl, addr_regh,
1196139c1837SPaolo Bonzini                        s->code_ptr, label_ptr);
1197139c1837SPaolo Bonzini#else
1198a3fb7c99SRichard Henderson    a_bits = get_alignment_bits(opc);
1199a3fb7c99SRichard Henderson    if (a_bits) {
1200a3fb7c99SRichard Henderson        tcg_out_test_alignment(s, true, addr_regl, a_bits);
1201a3fb7c99SRichard Henderson    }
1202*2e3a933aSRichard Henderson    base = addr_regl;
1203*2e3a933aSRichard Henderson    if (TCG_TARGET_REG_BITS > TARGET_LONG_BITS) {
1204*2e3a933aSRichard Henderson        tcg_out_ext32u(s, TCG_REG_TMP0, base);
1205*2e3a933aSRichard Henderson        base = TCG_REG_TMP0;
1206*2e3a933aSRichard Henderson    }
120781c65ee2SRichard Henderson    if (guest_base != 0) {
1208*2e3a933aSRichard Henderson        tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP0, TCG_GUEST_BASE_REG, base);
1209*2e3a933aSRichard Henderson        base = TCG_REG_TMP0;
1210139c1837SPaolo Bonzini    }
1211139c1837SPaolo Bonzini    tcg_out_qemu_ld_direct(s, data_regl, data_regh, base, opc, is_64);
1212139c1837SPaolo Bonzini#endif
1213139c1837SPaolo Bonzini}
1214139c1837SPaolo Bonzini
1215139c1837SPaolo Bonzinistatic void tcg_out_qemu_st_direct(TCGContext *s, TCGReg lo, TCGReg hi,
1216139c1837SPaolo Bonzini                                   TCGReg base, MemOp opc)
1217139c1837SPaolo Bonzini{
1218c86bd2dcSRichard Henderson    /* Byte swapping is left to middle-end expansion. */
1219c86bd2dcSRichard Henderson    tcg_debug_assert((opc & MO_BSWAP) == 0);
1220139c1837SPaolo Bonzini
1221139c1837SPaolo Bonzini    switch (opc & (MO_SSIZE)) {
1222139c1837SPaolo Bonzini    case MO_8:
1223139c1837SPaolo Bonzini        tcg_out_opc_store(s, OPC_SB, base, lo, 0);
1224139c1837SPaolo Bonzini        break;
1225139c1837SPaolo Bonzini    case MO_16:
1226139c1837SPaolo Bonzini        tcg_out_opc_store(s, OPC_SH, base, lo, 0);
1227139c1837SPaolo Bonzini        break;
1228139c1837SPaolo Bonzini    case MO_32:
1229139c1837SPaolo Bonzini        tcg_out_opc_store(s, OPC_SW, base, lo, 0);
1230139c1837SPaolo Bonzini        break;
1231139c1837SPaolo Bonzini    case MO_64:
1232139c1837SPaolo Bonzini        if (TCG_TARGET_REG_BITS == 64) {
1233139c1837SPaolo Bonzini            tcg_out_opc_store(s, OPC_SD, base, lo, 0);
1234139c1837SPaolo Bonzini        } else {
1235139c1837SPaolo Bonzini            tcg_out_opc_store(s, OPC_SW, base, lo, 0);
1236139c1837SPaolo Bonzini            tcg_out_opc_store(s, OPC_SW, base, hi, 4);
1237139c1837SPaolo Bonzini        }
1238139c1837SPaolo Bonzini        break;
1239139c1837SPaolo Bonzini    default:
1240139c1837SPaolo Bonzini        g_assert_not_reached();
1241139c1837SPaolo Bonzini    }
1242139c1837SPaolo Bonzini}
1243139c1837SPaolo Bonzini
1244139c1837SPaolo Bonzinistatic void tcg_out_qemu_st(TCGContext *s, const TCGArg *args, bool is_64)
1245139c1837SPaolo Bonzini{
1246139c1837SPaolo Bonzini    TCGReg addr_regl, addr_regh __attribute__((unused));
1247139c1837SPaolo Bonzini    TCGReg data_regl, data_regh;
12489002ffcbSRichard Henderson    MemOpIdx oi;
1249139c1837SPaolo Bonzini    MemOp opc;
1250139c1837SPaolo Bonzini#if defined(CONFIG_SOFTMMU)
1251139c1837SPaolo Bonzini    tcg_insn_unit *label_ptr[1];
1252a3fb7c99SRichard Henderson#else
1253a3fb7c99SRichard Henderson    unsigned a_bits;
1254139c1837SPaolo Bonzini#endif
1255*2e3a933aSRichard Henderson    TCGReg base;
1256139c1837SPaolo Bonzini
1257139c1837SPaolo Bonzini    data_regl = *args++;
1258139c1837SPaolo Bonzini    data_regh = (TCG_TARGET_REG_BITS == 32 && is_64 ? *args++ : 0);
1259139c1837SPaolo Bonzini    addr_regl = *args++;
1260139c1837SPaolo Bonzini    addr_regh = (TCG_TARGET_REG_BITS < TARGET_LONG_BITS ? *args++ : 0);
1261139c1837SPaolo Bonzini    oi = *args++;
1262139c1837SPaolo Bonzini    opc = get_memop(oi);
1263139c1837SPaolo Bonzini
1264139c1837SPaolo Bonzini#if defined(CONFIG_SOFTMMU)
1265*2e3a933aSRichard Henderson    base = tcg_out_tlb_load(s, addr_regl, addr_regh, oi, label_ptr, 0);
1266139c1837SPaolo Bonzini    tcg_out_qemu_st_direct(s, data_regl, data_regh, base, opc);
1267139c1837SPaolo Bonzini    add_qemu_ldst_label(s, 0, oi,
1268139c1837SPaolo Bonzini                        (is_64 ? TCG_TYPE_I64 : TCG_TYPE_I32),
1269139c1837SPaolo Bonzini                        data_regl, data_regh, addr_regl, addr_regh,
1270139c1837SPaolo Bonzini                        s->code_ptr, label_ptr);
1271139c1837SPaolo Bonzini#else
1272a3fb7c99SRichard Henderson    a_bits = get_alignment_bits(opc);
1273a3fb7c99SRichard Henderson    if (a_bits) {
1274a3fb7c99SRichard Henderson        tcg_out_test_alignment(s, false, addr_regl, a_bits);
1275a3fb7c99SRichard Henderson    }
1276*2e3a933aSRichard Henderson    base = addr_regl;
1277*2e3a933aSRichard Henderson    if (TCG_TARGET_REG_BITS > TARGET_LONG_BITS) {
1278*2e3a933aSRichard Henderson        tcg_out_ext32u(s, TCG_REG_TMP0, base);
1279*2e3a933aSRichard Henderson        base = TCG_REG_TMP0;
1280*2e3a933aSRichard Henderson    }
128181c65ee2SRichard Henderson    if (guest_base != 0) {
1282*2e3a933aSRichard Henderson        tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP0, TCG_GUEST_BASE_REG, base);
1283*2e3a933aSRichard Henderson        base = TCG_REG_TMP0;
1284139c1837SPaolo Bonzini    }
1285139c1837SPaolo Bonzini    tcg_out_qemu_st_direct(s, data_regl, data_regh, base, opc);
1286139c1837SPaolo Bonzini#endif
1287139c1837SPaolo Bonzini}
1288139c1837SPaolo Bonzini
1289793f7381SRichard Hendersonstatic const tcg_insn_unit *tb_ret_addr;
1290139c1837SPaolo Bonzini
1291139c1837SPaolo Bonzinistatic void tcg_out_op(TCGContext *s, TCGOpcode opc,
12925e8892dbSMiroslav Rezanina                       const TCGArg args[TCG_MAX_OP_ARGS],
12935e8892dbSMiroslav Rezanina                       const int const_args[TCG_MAX_OP_ARGS])
1294139c1837SPaolo Bonzini{
1295139c1837SPaolo Bonzini    TCGArg a0 = args[0];
1296139c1837SPaolo Bonzini    TCGArg a1 = args[1];
1297139c1837SPaolo Bonzini    TCGArg a2 = args[2];
1298139c1837SPaolo Bonzini    int c2 = const_args[2];
1299139c1837SPaolo Bonzini
1300139c1837SPaolo Bonzini    switch (opc) {
1301139c1837SPaolo Bonzini    case INDEX_op_exit_tb:
1302139c1837SPaolo Bonzini        /* Reuse the zeroing that exists for goto_ptr.  */
1303139c1837SPaolo Bonzini        if (a0 == 0) {
13048b5c2b62SRichard Henderson            tcg_out_call_int(s, tcg_code_gen_epilogue, true);
1305139c1837SPaolo Bonzini        } else {
1306139c1837SPaolo Bonzini            tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_A0, a0);
1307139c1837SPaolo Bonzini            tcg_out_call_int(s, tb_ret_addr, true);
1308139c1837SPaolo Bonzini        }
1309139c1837SPaolo Bonzini        break;
1310139c1837SPaolo Bonzini
1311139c1837SPaolo Bonzini    case INDEX_op_goto_tb:
1312139c1837SPaolo Bonzini        assert(s->tb_jmp_insn_offset == 0);
1313139c1837SPaolo Bonzini        /* indirect jump method */
1314139c1837SPaolo Bonzini        tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP0, TCG_REG_ZERO,
1315139c1837SPaolo Bonzini                   (uintptr_t)(s->tb_jmp_target_addr + a0));
1316139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_JALR, TCG_REG_ZERO, TCG_REG_TMP0, 0);
1317139c1837SPaolo Bonzini        set_jmp_reset_offset(s, a0);
1318139c1837SPaolo Bonzini        break;
1319139c1837SPaolo Bonzini
1320139c1837SPaolo Bonzini    case INDEX_op_goto_ptr:
1321139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_JALR, TCG_REG_ZERO, a0, 0);
1322139c1837SPaolo Bonzini        break;
1323139c1837SPaolo Bonzini
1324139c1837SPaolo Bonzini    case INDEX_op_br:
1325139c1837SPaolo Bonzini        tcg_out_reloc(s, s->code_ptr, R_RISCV_JAL, arg_label(a0), 0);
1326139c1837SPaolo Bonzini        tcg_out_opc_jump(s, OPC_JAL, TCG_REG_ZERO, 0);
1327139c1837SPaolo Bonzini        break;
1328139c1837SPaolo Bonzini
1329139c1837SPaolo Bonzini    case INDEX_op_ld8u_i32:
1330139c1837SPaolo Bonzini    case INDEX_op_ld8u_i64:
1331139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_LBU, a0, a1, a2);
1332139c1837SPaolo Bonzini        break;
1333139c1837SPaolo Bonzini    case INDEX_op_ld8s_i32:
1334139c1837SPaolo Bonzini    case INDEX_op_ld8s_i64:
1335139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_LB, a0, a1, a2);
1336139c1837SPaolo Bonzini        break;
1337139c1837SPaolo Bonzini    case INDEX_op_ld16u_i32:
1338139c1837SPaolo Bonzini    case INDEX_op_ld16u_i64:
1339139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_LHU, a0, a1, a2);
1340139c1837SPaolo Bonzini        break;
1341139c1837SPaolo Bonzini    case INDEX_op_ld16s_i32:
1342139c1837SPaolo Bonzini    case INDEX_op_ld16s_i64:
1343139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_LH, a0, a1, a2);
1344139c1837SPaolo Bonzini        break;
1345139c1837SPaolo Bonzini    case INDEX_op_ld32u_i64:
1346139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_LWU, a0, a1, a2);
1347139c1837SPaolo Bonzini        break;
1348139c1837SPaolo Bonzini    case INDEX_op_ld_i32:
1349139c1837SPaolo Bonzini    case INDEX_op_ld32s_i64:
1350139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_LW, a0, a1, a2);
1351139c1837SPaolo Bonzini        break;
1352139c1837SPaolo Bonzini    case INDEX_op_ld_i64:
1353139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_LD, a0, a1, a2);
1354139c1837SPaolo Bonzini        break;
1355139c1837SPaolo Bonzini
1356139c1837SPaolo Bonzini    case INDEX_op_st8_i32:
1357139c1837SPaolo Bonzini    case INDEX_op_st8_i64:
1358139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_SB, a0, a1, a2);
1359139c1837SPaolo Bonzini        break;
1360139c1837SPaolo Bonzini    case INDEX_op_st16_i32:
1361139c1837SPaolo Bonzini    case INDEX_op_st16_i64:
1362139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_SH, a0, a1, a2);
1363139c1837SPaolo Bonzini        break;
1364139c1837SPaolo Bonzini    case INDEX_op_st_i32:
1365139c1837SPaolo Bonzini    case INDEX_op_st32_i64:
1366139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_SW, a0, a1, a2);
1367139c1837SPaolo Bonzini        break;
1368139c1837SPaolo Bonzini    case INDEX_op_st_i64:
1369139c1837SPaolo Bonzini        tcg_out_ldst(s, OPC_SD, a0, a1, a2);
1370139c1837SPaolo Bonzini        break;
1371139c1837SPaolo Bonzini
1372139c1837SPaolo Bonzini    case INDEX_op_add_i32:
1373139c1837SPaolo Bonzini        if (c2) {
1374139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_ADDIW, a0, a1, a2);
1375139c1837SPaolo Bonzini        } else {
1376139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_ADDW, a0, a1, a2);
1377139c1837SPaolo Bonzini        }
1378139c1837SPaolo Bonzini        break;
1379139c1837SPaolo Bonzini    case INDEX_op_add_i64:
1380139c1837SPaolo Bonzini        if (c2) {
1381139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_ADDI, a0, a1, a2);
1382139c1837SPaolo Bonzini        } else {
1383139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_ADD, a0, a1, a2);
1384139c1837SPaolo Bonzini        }
1385139c1837SPaolo Bonzini        break;
1386139c1837SPaolo Bonzini
1387139c1837SPaolo Bonzini    case INDEX_op_sub_i32:
1388139c1837SPaolo Bonzini        if (c2) {
1389139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_ADDIW, a0, a1, -a2);
1390139c1837SPaolo Bonzini        } else {
1391139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SUBW, a0, a1, a2);
1392139c1837SPaolo Bonzini        }
1393139c1837SPaolo Bonzini        break;
1394139c1837SPaolo Bonzini    case INDEX_op_sub_i64:
1395139c1837SPaolo Bonzini        if (c2) {
1396139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_ADDI, a0, a1, -a2);
1397139c1837SPaolo Bonzini        } else {
1398139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SUB, a0, a1, a2);
1399139c1837SPaolo Bonzini        }
1400139c1837SPaolo Bonzini        break;
1401139c1837SPaolo Bonzini
1402139c1837SPaolo Bonzini    case INDEX_op_and_i32:
1403139c1837SPaolo Bonzini    case INDEX_op_and_i64:
1404139c1837SPaolo Bonzini        if (c2) {
1405139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_ANDI, a0, a1, a2);
1406139c1837SPaolo Bonzini        } else {
1407139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_AND, a0, a1, a2);
1408139c1837SPaolo Bonzini        }
1409139c1837SPaolo Bonzini        break;
1410139c1837SPaolo Bonzini
1411139c1837SPaolo Bonzini    case INDEX_op_or_i32:
1412139c1837SPaolo Bonzini    case INDEX_op_or_i64:
1413139c1837SPaolo Bonzini        if (c2) {
1414139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_ORI, a0, a1, a2);
1415139c1837SPaolo Bonzini        } else {
1416139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_OR, a0, a1, a2);
1417139c1837SPaolo Bonzini        }
1418139c1837SPaolo Bonzini        break;
1419139c1837SPaolo Bonzini
1420139c1837SPaolo Bonzini    case INDEX_op_xor_i32:
1421139c1837SPaolo Bonzini    case INDEX_op_xor_i64:
1422139c1837SPaolo Bonzini        if (c2) {
1423139c1837SPaolo Bonzini            tcg_out_opc_imm(s, OPC_XORI, a0, a1, a2);
1424139c1837SPaolo Bonzini        } else {
1425139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_XOR, a0, a1, a2);
1426139c1837SPaolo Bonzini        }
1427139c1837SPaolo Bonzini        break;
1428139c1837SPaolo Bonzini
1429139c1837SPaolo Bonzini    case INDEX_op_not_i32:
1430139c1837SPaolo Bonzini    case INDEX_op_not_i64:
1431139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_XORI, a0, a1, -1);
1432139c1837SPaolo Bonzini        break;
1433139c1837SPaolo Bonzini
1434139c1837SPaolo Bonzini    case INDEX_op_neg_i32:
1435139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SUBW, a0, TCG_REG_ZERO, a1);
1436139c1837SPaolo Bonzini        break;
1437139c1837SPaolo Bonzini    case INDEX_op_neg_i64:
1438139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_SUB, a0, TCG_REG_ZERO, a1);
1439139c1837SPaolo Bonzini        break;
1440139c1837SPaolo Bonzini
1441139c1837SPaolo Bonzini    case INDEX_op_mul_i32:
1442139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_MULW, a0, a1, a2);
1443139c1837SPaolo Bonzini        break;
1444139c1837SPaolo Bonzini    case INDEX_op_mul_i64:
1445139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_MUL, a0, a1, a2);
1446139c1837SPaolo Bonzini        break;
1447139c1837SPaolo Bonzini
1448139c1837SPaolo Bonzini    case INDEX_op_div_i32:
1449139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_DIVW, a0, a1, a2);
1450139c1837SPaolo Bonzini        break;
1451139c1837SPaolo Bonzini    case INDEX_op_div_i64:
1452139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_DIV, a0, a1, a2);
1453139c1837SPaolo Bonzini        break;
1454139c1837SPaolo Bonzini
1455139c1837SPaolo Bonzini    case INDEX_op_divu_i32:
1456139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_DIVUW, a0, a1, a2);
1457139c1837SPaolo Bonzini        break;
1458139c1837SPaolo Bonzini    case INDEX_op_divu_i64:
1459139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_DIVU, a0, a1, a2);
1460139c1837SPaolo Bonzini        break;
1461139c1837SPaolo Bonzini
1462139c1837SPaolo Bonzini    case INDEX_op_rem_i32:
1463139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_REMW, a0, a1, a2);
1464139c1837SPaolo Bonzini        break;
1465139c1837SPaolo Bonzini    case INDEX_op_rem_i64:
1466139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_REM, a0, a1, a2);
1467139c1837SPaolo Bonzini        break;
1468139c1837SPaolo Bonzini
1469139c1837SPaolo Bonzini    case INDEX_op_remu_i32:
1470139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_REMUW, a0, a1, a2);
1471139c1837SPaolo Bonzini        break;
1472139c1837SPaolo Bonzini    case INDEX_op_remu_i64:
1473139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_REMU, a0, a1, a2);
1474139c1837SPaolo Bonzini        break;
1475139c1837SPaolo Bonzini
1476139c1837SPaolo Bonzini    case INDEX_op_shl_i32:
1477139c1837SPaolo Bonzini        if (c2) {
1478d2f3066eSZihao Yu            tcg_out_opc_imm(s, OPC_SLLIW, a0, a1, a2 & 0x1f);
1479139c1837SPaolo Bonzini        } else {
1480139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SLLW, a0, a1, a2);
1481139c1837SPaolo Bonzini        }
1482139c1837SPaolo Bonzini        break;
1483139c1837SPaolo Bonzini    case INDEX_op_shl_i64:
1484139c1837SPaolo Bonzini        if (c2) {
1485d2f3066eSZihao Yu            tcg_out_opc_imm(s, OPC_SLLI, a0, a1, a2 & 0x3f);
1486139c1837SPaolo Bonzini        } else {
1487139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SLL, a0, a1, a2);
1488139c1837SPaolo Bonzini        }
1489139c1837SPaolo Bonzini        break;
1490139c1837SPaolo Bonzini
1491139c1837SPaolo Bonzini    case INDEX_op_shr_i32:
1492139c1837SPaolo Bonzini        if (c2) {
1493d2f3066eSZihao Yu            tcg_out_opc_imm(s, OPC_SRLIW, a0, a1, a2 & 0x1f);
1494139c1837SPaolo Bonzini        } else {
1495139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SRLW, a0, a1, a2);
1496139c1837SPaolo Bonzini        }
1497139c1837SPaolo Bonzini        break;
1498139c1837SPaolo Bonzini    case INDEX_op_shr_i64:
1499139c1837SPaolo Bonzini        if (c2) {
1500d2f3066eSZihao Yu            tcg_out_opc_imm(s, OPC_SRLI, a0, a1, a2 & 0x3f);
1501139c1837SPaolo Bonzini        } else {
1502139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SRL, a0, a1, a2);
1503139c1837SPaolo Bonzini        }
1504139c1837SPaolo Bonzini        break;
1505139c1837SPaolo Bonzini
1506139c1837SPaolo Bonzini    case INDEX_op_sar_i32:
1507139c1837SPaolo Bonzini        if (c2) {
1508d2f3066eSZihao Yu            tcg_out_opc_imm(s, OPC_SRAIW, a0, a1, a2 & 0x1f);
1509139c1837SPaolo Bonzini        } else {
1510139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SRAW, a0, a1, a2);
1511139c1837SPaolo Bonzini        }
1512139c1837SPaolo Bonzini        break;
1513139c1837SPaolo Bonzini    case INDEX_op_sar_i64:
1514139c1837SPaolo Bonzini        if (c2) {
1515d2f3066eSZihao Yu            tcg_out_opc_imm(s, OPC_SRAI, a0, a1, a2 & 0x3f);
1516139c1837SPaolo Bonzini        } else {
1517139c1837SPaolo Bonzini            tcg_out_opc_reg(s, OPC_SRA, a0, a1, a2);
1518139c1837SPaolo Bonzini        }
1519139c1837SPaolo Bonzini        break;
1520139c1837SPaolo Bonzini
1521139c1837SPaolo Bonzini    case INDEX_op_add2_i32:
1522139c1837SPaolo Bonzini        tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5],
1523139c1837SPaolo Bonzini                        const_args[4], const_args[5], false, true);
1524139c1837SPaolo Bonzini        break;
1525139c1837SPaolo Bonzini    case INDEX_op_add2_i64:
1526139c1837SPaolo Bonzini        tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5],
1527139c1837SPaolo Bonzini                        const_args[4], const_args[5], false, false);
1528139c1837SPaolo Bonzini        break;
1529139c1837SPaolo Bonzini    case INDEX_op_sub2_i32:
1530139c1837SPaolo Bonzini        tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5],
1531139c1837SPaolo Bonzini                        const_args[4], const_args[5], true, true);
1532139c1837SPaolo Bonzini        break;
1533139c1837SPaolo Bonzini    case INDEX_op_sub2_i64:
1534139c1837SPaolo Bonzini        tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5],
1535139c1837SPaolo Bonzini                        const_args[4], const_args[5], true, false);
1536139c1837SPaolo Bonzini        break;
1537139c1837SPaolo Bonzini
1538139c1837SPaolo Bonzini    case INDEX_op_brcond_i32:
1539139c1837SPaolo Bonzini    case INDEX_op_brcond_i64:
1540139c1837SPaolo Bonzini        tcg_out_brcond(s, a2, a0, a1, arg_label(args[3]));
1541139c1837SPaolo Bonzini        break;
1542139c1837SPaolo Bonzini    case INDEX_op_brcond2_i32:
1543139c1837SPaolo Bonzini        tcg_out_brcond2(s, args[4], a0, a1, a2, args[3], arg_label(args[5]));
1544139c1837SPaolo Bonzini        break;
1545139c1837SPaolo Bonzini
1546139c1837SPaolo Bonzini    case INDEX_op_setcond_i32:
1547139c1837SPaolo Bonzini    case INDEX_op_setcond_i64:
1548139c1837SPaolo Bonzini        tcg_out_setcond(s, args[3], a0, a1, a2);
1549139c1837SPaolo Bonzini        break;
1550139c1837SPaolo Bonzini    case INDEX_op_setcond2_i32:
1551139c1837SPaolo Bonzini        tcg_out_setcond2(s, args[5], a0, a1, a2, args[3], args[4]);
1552139c1837SPaolo Bonzini        break;
1553139c1837SPaolo Bonzini
1554139c1837SPaolo Bonzini    case INDEX_op_qemu_ld_i32:
1555139c1837SPaolo Bonzini        tcg_out_qemu_ld(s, args, false);
1556139c1837SPaolo Bonzini        break;
1557139c1837SPaolo Bonzini    case INDEX_op_qemu_ld_i64:
1558139c1837SPaolo Bonzini        tcg_out_qemu_ld(s, args, true);
1559139c1837SPaolo Bonzini        break;
1560139c1837SPaolo Bonzini    case INDEX_op_qemu_st_i32:
1561139c1837SPaolo Bonzini        tcg_out_qemu_st(s, args, false);
1562139c1837SPaolo Bonzini        break;
1563139c1837SPaolo Bonzini    case INDEX_op_qemu_st_i64:
1564139c1837SPaolo Bonzini        tcg_out_qemu_st(s, args, true);
1565139c1837SPaolo Bonzini        break;
1566139c1837SPaolo Bonzini
1567139c1837SPaolo Bonzini    case INDEX_op_ext8u_i32:
1568139c1837SPaolo Bonzini    case INDEX_op_ext8u_i64:
1569139c1837SPaolo Bonzini        tcg_out_ext8u(s, a0, a1);
1570139c1837SPaolo Bonzini        break;
1571139c1837SPaolo Bonzini
1572139c1837SPaolo Bonzini    case INDEX_op_ext16u_i32:
1573139c1837SPaolo Bonzini    case INDEX_op_ext16u_i64:
1574139c1837SPaolo Bonzini        tcg_out_ext16u(s, a0, a1);
1575139c1837SPaolo Bonzini        break;
1576139c1837SPaolo Bonzini
1577139c1837SPaolo Bonzini    case INDEX_op_ext32u_i64:
1578139c1837SPaolo Bonzini    case INDEX_op_extu_i32_i64:
1579139c1837SPaolo Bonzini        tcg_out_ext32u(s, a0, a1);
1580139c1837SPaolo Bonzini        break;
1581139c1837SPaolo Bonzini
1582139c1837SPaolo Bonzini    case INDEX_op_ext8s_i32:
1583139c1837SPaolo Bonzini    case INDEX_op_ext8s_i64:
1584139c1837SPaolo Bonzini        tcg_out_ext8s(s, a0, a1);
1585139c1837SPaolo Bonzini        break;
1586139c1837SPaolo Bonzini
1587139c1837SPaolo Bonzini    case INDEX_op_ext16s_i32:
1588139c1837SPaolo Bonzini    case INDEX_op_ext16s_i64:
1589139c1837SPaolo Bonzini        tcg_out_ext16s(s, a0, a1);
1590139c1837SPaolo Bonzini        break;
1591139c1837SPaolo Bonzini
1592139c1837SPaolo Bonzini    case INDEX_op_ext32s_i64:
1593139c1837SPaolo Bonzini    case INDEX_op_extrl_i64_i32:
1594139c1837SPaolo Bonzini    case INDEX_op_ext_i32_i64:
1595139c1837SPaolo Bonzini        tcg_out_ext32s(s, a0, a1);
1596139c1837SPaolo Bonzini        break;
1597139c1837SPaolo Bonzini
1598139c1837SPaolo Bonzini    case INDEX_op_extrh_i64_i32:
1599139c1837SPaolo Bonzini        tcg_out_opc_imm(s, OPC_SRAI, a0, a1, 32);
1600139c1837SPaolo Bonzini        break;
1601139c1837SPaolo Bonzini
1602139c1837SPaolo Bonzini    case INDEX_op_mulsh_i32:
1603139c1837SPaolo Bonzini    case INDEX_op_mulsh_i64:
1604139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_MULH, a0, a1, a2);
1605139c1837SPaolo Bonzini        break;
1606139c1837SPaolo Bonzini
1607139c1837SPaolo Bonzini    case INDEX_op_muluh_i32:
1608139c1837SPaolo Bonzini    case INDEX_op_muluh_i64:
1609139c1837SPaolo Bonzini        tcg_out_opc_reg(s, OPC_MULHU, a0, a1, a2);
1610139c1837SPaolo Bonzini        break;
1611139c1837SPaolo Bonzini
1612139c1837SPaolo Bonzini    case INDEX_op_mb:
1613139c1837SPaolo Bonzini        tcg_out_mb(s, a0);
1614139c1837SPaolo Bonzini        break;
1615139c1837SPaolo Bonzini
1616139c1837SPaolo Bonzini    case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
1617139c1837SPaolo Bonzini    case INDEX_op_mov_i64:
1618139c1837SPaolo Bonzini    case INDEX_op_call:     /* Always emitted via tcg_out_call.  */
1619139c1837SPaolo Bonzini    default:
1620139c1837SPaolo Bonzini        g_assert_not_reached();
1621139c1837SPaolo Bonzini    }
1622139c1837SPaolo Bonzini}
1623139c1837SPaolo Bonzini
1624665be288SRichard Hendersonstatic TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
1625139c1837SPaolo Bonzini{
1626139c1837SPaolo Bonzini    switch (op) {
1627139c1837SPaolo Bonzini    case INDEX_op_goto_ptr:
1628665be288SRichard Henderson        return C_O0_I1(r);
1629139c1837SPaolo Bonzini
1630139c1837SPaolo Bonzini    case INDEX_op_ld8u_i32:
1631139c1837SPaolo Bonzini    case INDEX_op_ld8s_i32:
1632139c1837SPaolo Bonzini    case INDEX_op_ld16u_i32:
1633139c1837SPaolo Bonzini    case INDEX_op_ld16s_i32:
1634139c1837SPaolo Bonzini    case INDEX_op_ld_i32:
1635139c1837SPaolo Bonzini    case INDEX_op_not_i32:
1636139c1837SPaolo Bonzini    case INDEX_op_neg_i32:
1637139c1837SPaolo Bonzini    case INDEX_op_ld8u_i64:
1638139c1837SPaolo Bonzini    case INDEX_op_ld8s_i64:
1639139c1837SPaolo Bonzini    case INDEX_op_ld16u_i64:
1640139c1837SPaolo Bonzini    case INDEX_op_ld16s_i64:
1641139c1837SPaolo Bonzini    case INDEX_op_ld32s_i64:
1642139c1837SPaolo Bonzini    case INDEX_op_ld32u_i64:
1643139c1837SPaolo Bonzini    case INDEX_op_ld_i64:
1644139c1837SPaolo Bonzini    case INDEX_op_not_i64:
1645139c1837SPaolo Bonzini    case INDEX_op_neg_i64:
1646139c1837SPaolo Bonzini    case INDEX_op_ext8u_i32:
1647139c1837SPaolo Bonzini    case INDEX_op_ext8u_i64:
1648139c1837SPaolo Bonzini    case INDEX_op_ext16u_i32:
1649139c1837SPaolo Bonzini    case INDEX_op_ext16u_i64:
1650139c1837SPaolo Bonzini    case INDEX_op_ext32u_i64:
1651139c1837SPaolo Bonzini    case INDEX_op_extu_i32_i64:
1652139c1837SPaolo Bonzini    case INDEX_op_ext8s_i32:
1653139c1837SPaolo Bonzini    case INDEX_op_ext8s_i64:
1654139c1837SPaolo Bonzini    case INDEX_op_ext16s_i32:
1655139c1837SPaolo Bonzini    case INDEX_op_ext16s_i64:
1656139c1837SPaolo Bonzini    case INDEX_op_ext32s_i64:
1657139c1837SPaolo Bonzini    case INDEX_op_extrl_i64_i32:
1658139c1837SPaolo Bonzini    case INDEX_op_extrh_i64_i32:
1659139c1837SPaolo Bonzini    case INDEX_op_ext_i32_i64:
1660665be288SRichard Henderson        return C_O1_I1(r, r);
1661139c1837SPaolo Bonzini
1662139c1837SPaolo Bonzini    case INDEX_op_st8_i32:
1663139c1837SPaolo Bonzini    case INDEX_op_st16_i32:
1664139c1837SPaolo Bonzini    case INDEX_op_st_i32:
1665139c1837SPaolo Bonzini    case INDEX_op_st8_i64:
1666139c1837SPaolo Bonzini    case INDEX_op_st16_i64:
1667139c1837SPaolo Bonzini    case INDEX_op_st32_i64:
1668139c1837SPaolo Bonzini    case INDEX_op_st_i64:
1669665be288SRichard Henderson        return C_O0_I2(rZ, r);
1670139c1837SPaolo Bonzini
1671139c1837SPaolo Bonzini    case INDEX_op_add_i32:
1672139c1837SPaolo Bonzini    case INDEX_op_and_i32:
1673139c1837SPaolo Bonzini    case INDEX_op_or_i32:
1674139c1837SPaolo Bonzini    case INDEX_op_xor_i32:
1675139c1837SPaolo Bonzini    case INDEX_op_add_i64:
1676139c1837SPaolo Bonzini    case INDEX_op_and_i64:
1677139c1837SPaolo Bonzini    case INDEX_op_or_i64:
1678139c1837SPaolo Bonzini    case INDEX_op_xor_i64:
1679665be288SRichard Henderson        return C_O1_I2(r, r, rI);
1680139c1837SPaolo Bonzini
1681139c1837SPaolo Bonzini    case INDEX_op_sub_i32:
1682139c1837SPaolo Bonzini    case INDEX_op_sub_i64:
1683665be288SRichard Henderson        return C_O1_I2(r, rZ, rN);
1684139c1837SPaolo Bonzini
1685139c1837SPaolo Bonzini    case INDEX_op_mul_i32:
1686139c1837SPaolo Bonzini    case INDEX_op_mulsh_i32:
1687139c1837SPaolo Bonzini    case INDEX_op_muluh_i32:
1688139c1837SPaolo Bonzini    case INDEX_op_div_i32:
1689139c1837SPaolo Bonzini    case INDEX_op_divu_i32:
1690139c1837SPaolo Bonzini    case INDEX_op_rem_i32:
1691139c1837SPaolo Bonzini    case INDEX_op_remu_i32:
1692139c1837SPaolo Bonzini    case INDEX_op_setcond_i32:
1693139c1837SPaolo Bonzini    case INDEX_op_mul_i64:
1694139c1837SPaolo Bonzini    case INDEX_op_mulsh_i64:
1695139c1837SPaolo Bonzini    case INDEX_op_muluh_i64:
1696139c1837SPaolo Bonzini    case INDEX_op_div_i64:
1697139c1837SPaolo Bonzini    case INDEX_op_divu_i64:
1698139c1837SPaolo Bonzini    case INDEX_op_rem_i64:
1699139c1837SPaolo Bonzini    case INDEX_op_remu_i64:
1700139c1837SPaolo Bonzini    case INDEX_op_setcond_i64:
1701665be288SRichard Henderson        return C_O1_I2(r, rZ, rZ);
1702139c1837SPaolo Bonzini
1703139c1837SPaolo Bonzini    case INDEX_op_shl_i32:
1704139c1837SPaolo Bonzini    case INDEX_op_shr_i32:
1705139c1837SPaolo Bonzini    case INDEX_op_sar_i32:
1706139c1837SPaolo Bonzini    case INDEX_op_shl_i64:
1707139c1837SPaolo Bonzini    case INDEX_op_shr_i64:
1708139c1837SPaolo Bonzini    case INDEX_op_sar_i64:
1709665be288SRichard Henderson        return C_O1_I2(r, r, ri);
1710139c1837SPaolo Bonzini
1711139c1837SPaolo Bonzini    case INDEX_op_brcond_i32:
1712139c1837SPaolo Bonzini    case INDEX_op_brcond_i64:
1713665be288SRichard Henderson        return C_O0_I2(rZ, rZ);
1714139c1837SPaolo Bonzini
1715139c1837SPaolo Bonzini    case INDEX_op_add2_i32:
1716139c1837SPaolo Bonzini    case INDEX_op_add2_i64:
1717139c1837SPaolo Bonzini    case INDEX_op_sub2_i32:
1718139c1837SPaolo Bonzini    case INDEX_op_sub2_i64:
1719665be288SRichard Henderson        return C_O2_I4(r, r, rZ, rZ, rM, rM);
1720139c1837SPaolo Bonzini
1721139c1837SPaolo Bonzini    case INDEX_op_brcond2_i32:
1722665be288SRichard Henderson        return C_O0_I4(rZ, rZ, rZ, rZ);
1723139c1837SPaolo Bonzini
1724139c1837SPaolo Bonzini    case INDEX_op_setcond2_i32:
1725665be288SRichard Henderson        return C_O1_I4(r, rZ, rZ, rZ, rZ);
1726139c1837SPaolo Bonzini
1727139c1837SPaolo Bonzini    case INDEX_op_qemu_ld_i32:
1728665be288SRichard Henderson        return (TARGET_LONG_BITS <= TCG_TARGET_REG_BITS
1729665be288SRichard Henderson                ? C_O1_I1(r, L) : C_O1_I2(r, L, L));
1730139c1837SPaolo Bonzini    case INDEX_op_qemu_st_i32:
1731665be288SRichard Henderson        return (TARGET_LONG_BITS <= TCG_TARGET_REG_BITS
1732665be288SRichard Henderson                ? C_O0_I2(LZ, L) : C_O0_I3(LZ, L, L));
1733139c1837SPaolo Bonzini    case INDEX_op_qemu_ld_i64:
1734665be288SRichard Henderson        return (TCG_TARGET_REG_BITS == 64 ? C_O1_I1(r, L)
1735665be288SRichard Henderson               : TARGET_LONG_BITS <= TCG_TARGET_REG_BITS ? C_O2_I1(r, r, L)
1736665be288SRichard Henderson               : C_O2_I2(r, r, L, L));
1737139c1837SPaolo Bonzini    case INDEX_op_qemu_st_i64:
1738665be288SRichard Henderson        return (TCG_TARGET_REG_BITS == 64 ? C_O0_I2(LZ, L)
1739665be288SRichard Henderson               : TARGET_LONG_BITS <= TCG_TARGET_REG_BITS ? C_O0_I3(LZ, LZ, L)
1740665be288SRichard Henderson               : C_O0_I4(LZ, LZ, L, L));
1741139c1837SPaolo Bonzini
1742139c1837SPaolo Bonzini    default:
1743665be288SRichard Henderson        g_assert_not_reached();
1744139c1837SPaolo Bonzini    }
1745139c1837SPaolo Bonzini}
1746139c1837SPaolo Bonzini
1747139c1837SPaolo Bonzinistatic const int tcg_target_callee_save_regs[] = {
1748139c1837SPaolo Bonzini    TCG_REG_S0,       /* used for the global env (TCG_AREG0) */
1749139c1837SPaolo Bonzini    TCG_REG_S1,
1750139c1837SPaolo Bonzini    TCG_REG_S2,
1751139c1837SPaolo Bonzini    TCG_REG_S3,
1752139c1837SPaolo Bonzini    TCG_REG_S4,
1753139c1837SPaolo Bonzini    TCG_REG_S5,
1754139c1837SPaolo Bonzini    TCG_REG_S6,
1755139c1837SPaolo Bonzini    TCG_REG_S7,
1756139c1837SPaolo Bonzini    TCG_REG_S8,
1757139c1837SPaolo Bonzini    TCG_REG_S9,
1758139c1837SPaolo Bonzini    TCG_REG_S10,
1759139c1837SPaolo Bonzini    TCG_REG_S11,
1760139c1837SPaolo Bonzini    TCG_REG_RA,       /* should be last for ABI compliance */
1761139c1837SPaolo Bonzini};
1762139c1837SPaolo Bonzini
1763139c1837SPaolo Bonzini/* Stack frame parameters.  */
1764139c1837SPaolo Bonzini#define REG_SIZE   (TCG_TARGET_REG_BITS / 8)
1765139c1837SPaolo Bonzini#define SAVE_SIZE  ((int)ARRAY_SIZE(tcg_target_callee_save_regs) * REG_SIZE)
1766139c1837SPaolo Bonzini#define TEMP_SIZE  (CPU_TEMP_BUF_NLONGS * (int)sizeof(long))
1767139c1837SPaolo Bonzini#define FRAME_SIZE ((TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE + SAVE_SIZE \
1768139c1837SPaolo Bonzini                     + TCG_TARGET_STACK_ALIGN - 1) \
1769139c1837SPaolo Bonzini                    & -TCG_TARGET_STACK_ALIGN)
1770139c1837SPaolo Bonzini#define SAVE_OFS   (TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE)
1771139c1837SPaolo Bonzini
1772139c1837SPaolo Bonzini/* We're expecting to be able to use an immediate for frame allocation.  */
1773139c1837SPaolo BonziniQEMU_BUILD_BUG_ON(FRAME_SIZE > 0x7ff);
1774139c1837SPaolo Bonzini
1775139c1837SPaolo Bonzini/* Generate global QEMU prologue and epilogue code */
1776139c1837SPaolo Bonzinistatic void tcg_target_qemu_prologue(TCGContext *s)
1777139c1837SPaolo Bonzini{
1778139c1837SPaolo Bonzini    int i;
1779139c1837SPaolo Bonzini
1780139c1837SPaolo Bonzini    tcg_set_frame(s, TCG_REG_SP, TCG_STATIC_CALL_ARGS_SIZE, TEMP_SIZE);
1781139c1837SPaolo Bonzini
1782139c1837SPaolo Bonzini    /* TB prologue */
1783139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_ADDI, TCG_REG_SP, TCG_REG_SP, -FRAME_SIZE);
1784139c1837SPaolo Bonzini    for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
1785139c1837SPaolo Bonzini        tcg_out_st(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
1786139c1837SPaolo Bonzini                   TCG_REG_SP, SAVE_OFS + i * REG_SIZE);
1787139c1837SPaolo Bonzini    }
1788139c1837SPaolo Bonzini
1789139c1837SPaolo Bonzini#if !defined(CONFIG_SOFTMMU)
1790139c1837SPaolo Bonzini    tcg_out_movi(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG, guest_base);
1791139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
1792139c1837SPaolo Bonzini#endif
1793139c1837SPaolo Bonzini
1794139c1837SPaolo Bonzini    /* Call generated code */
1795139c1837SPaolo Bonzini    tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
1796139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_JALR, TCG_REG_ZERO, tcg_target_call_iarg_regs[1], 0);
1797139c1837SPaolo Bonzini
1798139c1837SPaolo Bonzini    /* Return path for goto_ptr. Set return value to 0 */
1799c8bc1168SRichard Henderson    tcg_code_gen_epilogue = tcg_splitwx_to_rx(s->code_ptr);
1800139c1837SPaolo Bonzini    tcg_out_mov(s, TCG_TYPE_REG, TCG_REG_A0, TCG_REG_ZERO);
1801139c1837SPaolo Bonzini
1802139c1837SPaolo Bonzini    /* TB epilogue */
1803793f7381SRichard Henderson    tb_ret_addr = tcg_splitwx_to_rx(s->code_ptr);
1804139c1837SPaolo Bonzini    for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
1805139c1837SPaolo Bonzini        tcg_out_ld(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
1806139c1837SPaolo Bonzini                   TCG_REG_SP, SAVE_OFS + i * REG_SIZE);
1807139c1837SPaolo Bonzini    }
1808139c1837SPaolo Bonzini
1809139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_ADDI, TCG_REG_SP, TCG_REG_SP, FRAME_SIZE);
1810139c1837SPaolo Bonzini    tcg_out_opc_imm(s, OPC_JALR, TCG_REG_ZERO, TCG_REG_RA, 0);
1811139c1837SPaolo Bonzini}
1812139c1837SPaolo Bonzini
1813139c1837SPaolo Bonzinistatic void tcg_target_init(TCGContext *s)
1814139c1837SPaolo Bonzini{
1815139c1837SPaolo Bonzini    tcg_target_available_regs[TCG_TYPE_I32] = 0xffffffff;
1816139c1837SPaolo Bonzini    if (TCG_TARGET_REG_BITS == 64) {
1817139c1837SPaolo Bonzini        tcg_target_available_regs[TCG_TYPE_I64] = 0xffffffff;
1818139c1837SPaolo Bonzini    }
1819139c1837SPaolo Bonzini
1820139c1837SPaolo Bonzini    tcg_target_call_clobber_regs = -1u;
1821139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S0);
1822139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S1);
1823139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S2);
1824139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S3);
1825139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S4);
1826139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S5);
1827139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S6);
1828139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S7);
1829139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S8);
1830139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S9);
1831139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S10);
1832139c1837SPaolo Bonzini    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S11);
1833139c1837SPaolo Bonzini
1834139c1837SPaolo Bonzini    s->reserved_regs = 0;
1835139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_REG_ZERO);
1836139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP0);
1837139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP1);
1838139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP2);
1839139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_REG_SP);
1840139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_REG_GP);
1841139c1837SPaolo Bonzini    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TP);
1842139c1837SPaolo Bonzini}
1843139c1837SPaolo Bonzini
1844139c1837SPaolo Bonzinitypedef struct {
1845139c1837SPaolo Bonzini    DebugFrameHeader h;
1846139c1837SPaolo Bonzini    uint8_t fde_def_cfa[4];
1847139c1837SPaolo Bonzini    uint8_t fde_reg_ofs[ARRAY_SIZE(tcg_target_callee_save_regs) * 2];
1848139c1837SPaolo Bonzini} DebugFrame;
1849139c1837SPaolo Bonzini
1850139c1837SPaolo Bonzini#define ELF_HOST_MACHINE EM_RISCV
1851139c1837SPaolo Bonzini
1852139c1837SPaolo Bonzinistatic const DebugFrame debug_frame = {
1853139c1837SPaolo Bonzini    .h.cie.len = sizeof(DebugFrameCIE) - 4, /* length after .len member */
1854139c1837SPaolo Bonzini    .h.cie.id = -1,
1855139c1837SPaolo Bonzini    .h.cie.version = 1,
1856139c1837SPaolo Bonzini    .h.cie.code_align = 1,
1857139c1837SPaolo Bonzini    .h.cie.data_align = -(TCG_TARGET_REG_BITS / 8) & 0x7f, /* sleb128 */
1858139c1837SPaolo Bonzini    .h.cie.return_column = TCG_REG_RA,
1859139c1837SPaolo Bonzini
1860139c1837SPaolo Bonzini    /* Total FDE size does not include the "len" member.  */
1861139c1837SPaolo Bonzini    .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset),
1862139c1837SPaolo Bonzini
1863139c1837SPaolo Bonzini    .fde_def_cfa = {
1864139c1837SPaolo Bonzini        12, TCG_REG_SP,                 /* DW_CFA_def_cfa sp, ... */
1865139c1837SPaolo Bonzini        (FRAME_SIZE & 0x7f) | 0x80,     /* ... uleb128 FRAME_SIZE */
1866139c1837SPaolo Bonzini        (FRAME_SIZE >> 7)
1867139c1837SPaolo Bonzini    },
1868139c1837SPaolo Bonzini    .fde_reg_ofs = {
1869139c1837SPaolo Bonzini        0x80 + 9,  12,                  /* DW_CFA_offset, s1,  -96 */
1870139c1837SPaolo Bonzini        0x80 + 18, 11,                  /* DW_CFA_offset, s2,  -88 */
1871139c1837SPaolo Bonzini        0x80 + 19, 10,                  /* DW_CFA_offset, s3,  -80 */
1872139c1837SPaolo Bonzini        0x80 + 20, 9,                   /* DW_CFA_offset, s4,  -72 */
1873139c1837SPaolo Bonzini        0x80 + 21, 8,                   /* DW_CFA_offset, s5,  -64 */
1874139c1837SPaolo Bonzini        0x80 + 22, 7,                   /* DW_CFA_offset, s6,  -56 */
1875139c1837SPaolo Bonzini        0x80 + 23, 6,                   /* DW_CFA_offset, s7,  -48 */
1876139c1837SPaolo Bonzini        0x80 + 24, 5,                   /* DW_CFA_offset, s8,  -40 */
1877139c1837SPaolo Bonzini        0x80 + 25, 4,                   /* DW_CFA_offset, s9,  -32 */
1878139c1837SPaolo Bonzini        0x80 + 26, 3,                   /* DW_CFA_offset, s10, -24 */
1879139c1837SPaolo Bonzini        0x80 + 27, 2,                   /* DW_CFA_offset, s11, -16 */
1880139c1837SPaolo Bonzini        0x80 + 1 , 1,                   /* DW_CFA_offset, ra,  -8 */
1881139c1837SPaolo Bonzini    }
1882139c1837SPaolo Bonzini};
1883139c1837SPaolo Bonzini
1884755bf9e5SRichard Hendersonvoid tcg_register_jit(const void *buf, size_t buf_size)
1885139c1837SPaolo Bonzini{
1886139c1837SPaolo Bonzini    tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame));
1887139c1837SPaolo Bonzini}
1888