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