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 1169e3e0bc6SRichard Henderson#ifndef have_zbb 1179e3e0bc6SRichard Hendersonbool have_zbb; 1189e3e0bc6SRichard Henderson#endif 1199e3e0bc6SRichard Henderson#if defined(__riscv_arch_test) && defined(__riscv_zba) 1209e3e0bc6SRichard Henderson# define have_zba true 1219e3e0bc6SRichard Henderson#else 1229e3e0bc6SRichard Hendersonstatic bool have_zba; 1239e3e0bc6SRichard Henderson#endif 1249e3e0bc6SRichard Henderson#if defined(__riscv_arch_test) && defined(__riscv_zicond) 1259e3e0bc6SRichard Henderson# define have_zicond true 1269e3e0bc6SRichard Henderson#else 1279e3e0bc6SRichard Hendersonstatic bool have_zicond; 1289e3e0bc6SRichard Henderson#endif 1299e3e0bc6SRichard Henderson 1305e3d0c19SRichard Hendersonstatic TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot) 1315e3d0c19SRichard Henderson{ 1325e3d0c19SRichard Henderson tcg_debug_assert(kind == TCG_CALL_RET_NORMAL); 1335e3d0c19SRichard Henderson tcg_debug_assert(slot >= 0 && slot <= 1); 1345e3d0c19SRichard Henderson return TCG_REG_A0 + slot; 1355e3d0c19SRichard Henderson} 136139c1837SPaolo Bonzini 137139c1837SPaolo Bonzini#define TCG_CT_CONST_ZERO 0x100 138139c1837SPaolo Bonzini#define TCG_CT_CONST_S12 0x200 139139c1837SPaolo Bonzini#define TCG_CT_CONST_N12 0x400 140139c1837SPaolo Bonzini#define TCG_CT_CONST_M12 0x800 14199f4ec6eSRichard Henderson#define TCG_CT_CONST_J12 0x1000 142139c1837SPaolo Bonzini 143fc63a4c5SRichard Henderson#define ALL_GENERAL_REGS MAKE_64BIT_MASK(0, 32) 144fc63a4c5SRichard Henderson 145aeb6326eSRichard Henderson#define sextreg sextract64 146139c1837SPaolo Bonzini 147139c1837SPaolo Bonzini/* test if a constant matches the constraint */ 148a4fbbd77SRichard Hendersonstatic bool tcg_target_const_match(int64_t val, TCGType type, int ct) 149139c1837SPaolo Bonzini{ 150139c1837SPaolo Bonzini if (ct & TCG_CT_CONST) { 151139c1837SPaolo Bonzini return 1; 152139c1837SPaolo Bonzini } 153139c1837SPaolo Bonzini if ((ct & TCG_CT_CONST_ZERO) && val == 0) { 154139c1837SPaolo Bonzini return 1; 155139c1837SPaolo Bonzini } 15662722763SRichard Henderson /* 15762722763SRichard Henderson * Sign extended from 12 bits: [-0x800, 0x7ff]. 15862722763SRichard Henderson * Used for most arithmetic, as this is the isa field. 15962722763SRichard Henderson */ 16062722763SRichard Henderson if ((ct & TCG_CT_CONST_S12) && val >= -0x800 && val <= 0x7ff) { 161139c1837SPaolo Bonzini return 1; 162139c1837SPaolo Bonzini } 16362722763SRichard Henderson /* 16462722763SRichard Henderson * Sign extended from 12 bits, negated: [-0x7ff, 0x800]. 16562722763SRichard Henderson * Used for subtraction, where a constant must be handled by ADDI. 16662722763SRichard Henderson */ 16762722763SRichard Henderson if ((ct & TCG_CT_CONST_N12) && val >= -0x7ff && val <= 0x800) { 168139c1837SPaolo Bonzini return 1; 169139c1837SPaolo Bonzini } 17062722763SRichard Henderson /* 17162722763SRichard Henderson * Sign extended from 12 bits, +/- matching: [-0x7ff, 0x7ff]. 17262722763SRichard Henderson * Used by addsub2, which may need the negative operation, 17362722763SRichard Henderson * and requires the modified constant to be representable. 17462722763SRichard Henderson */ 17562722763SRichard Henderson if ((ct & TCG_CT_CONST_M12) && val >= -0x7ff && val <= 0x7ff) { 176139c1837SPaolo Bonzini return 1; 177139c1837SPaolo Bonzini } 17899f4ec6eSRichard Henderson /* 17999f4ec6eSRichard Henderson * Inverse of sign extended from 12 bits: ~[-0x800, 0x7ff]. 18099f4ec6eSRichard Henderson * Used to map ANDN back to ANDI, etc. 18199f4ec6eSRichard Henderson */ 18299f4ec6eSRichard Henderson if ((ct & TCG_CT_CONST_J12) && ~val >= -0x800 && ~val <= 0x7ff) { 18399f4ec6eSRichard Henderson return 1; 18499f4ec6eSRichard Henderson } 185139c1837SPaolo Bonzini return 0; 186139c1837SPaolo Bonzini} 187139c1837SPaolo Bonzini 188139c1837SPaolo Bonzini/* 189139c1837SPaolo Bonzini * RISC-V Base ISA opcodes (IM) 190139c1837SPaolo Bonzini */ 191139c1837SPaolo Bonzini 192139c1837SPaolo Bonzinitypedef enum { 193139c1837SPaolo Bonzini OPC_ADD = 0x33, 194139c1837SPaolo Bonzini OPC_ADDI = 0x13, 195139c1837SPaolo Bonzini OPC_AND = 0x7033, 196139c1837SPaolo Bonzini OPC_ANDI = 0x7013, 197139c1837SPaolo Bonzini OPC_AUIPC = 0x17, 198139c1837SPaolo Bonzini OPC_BEQ = 0x63, 199139c1837SPaolo Bonzini OPC_BGE = 0x5063, 200139c1837SPaolo Bonzini OPC_BGEU = 0x7063, 201139c1837SPaolo Bonzini OPC_BLT = 0x4063, 202139c1837SPaolo Bonzini OPC_BLTU = 0x6063, 203139c1837SPaolo Bonzini OPC_BNE = 0x1063, 204139c1837SPaolo Bonzini OPC_DIV = 0x2004033, 205139c1837SPaolo Bonzini OPC_DIVU = 0x2005033, 206139c1837SPaolo Bonzini OPC_JAL = 0x6f, 207139c1837SPaolo Bonzini OPC_JALR = 0x67, 208139c1837SPaolo Bonzini OPC_LB = 0x3, 209139c1837SPaolo Bonzini OPC_LBU = 0x4003, 210139c1837SPaolo Bonzini OPC_LD = 0x3003, 211139c1837SPaolo Bonzini OPC_LH = 0x1003, 212139c1837SPaolo Bonzini OPC_LHU = 0x5003, 213139c1837SPaolo Bonzini OPC_LUI = 0x37, 214139c1837SPaolo Bonzini OPC_LW = 0x2003, 215139c1837SPaolo Bonzini OPC_LWU = 0x6003, 216139c1837SPaolo Bonzini OPC_MUL = 0x2000033, 217139c1837SPaolo Bonzini OPC_MULH = 0x2001033, 218139c1837SPaolo Bonzini OPC_MULHSU = 0x2002033, 219139c1837SPaolo Bonzini OPC_MULHU = 0x2003033, 220139c1837SPaolo Bonzini OPC_OR = 0x6033, 221139c1837SPaolo Bonzini OPC_ORI = 0x6013, 222139c1837SPaolo Bonzini OPC_REM = 0x2006033, 223139c1837SPaolo Bonzini OPC_REMU = 0x2007033, 224139c1837SPaolo Bonzini OPC_SB = 0x23, 225139c1837SPaolo Bonzini OPC_SD = 0x3023, 226139c1837SPaolo Bonzini OPC_SH = 0x1023, 227139c1837SPaolo Bonzini OPC_SLL = 0x1033, 228139c1837SPaolo Bonzini OPC_SLLI = 0x1013, 229139c1837SPaolo Bonzini OPC_SLT = 0x2033, 230139c1837SPaolo Bonzini OPC_SLTI = 0x2013, 231139c1837SPaolo Bonzini OPC_SLTIU = 0x3013, 232139c1837SPaolo Bonzini OPC_SLTU = 0x3033, 233139c1837SPaolo Bonzini OPC_SRA = 0x40005033, 234139c1837SPaolo Bonzini OPC_SRAI = 0x40005013, 235139c1837SPaolo Bonzini OPC_SRL = 0x5033, 236139c1837SPaolo Bonzini OPC_SRLI = 0x5013, 237139c1837SPaolo Bonzini OPC_SUB = 0x40000033, 238139c1837SPaolo Bonzini OPC_SW = 0x2023, 239139c1837SPaolo Bonzini OPC_XOR = 0x4033, 240139c1837SPaolo Bonzini OPC_XORI = 0x4013, 241139c1837SPaolo Bonzini 242139c1837SPaolo Bonzini OPC_ADDIW = 0x1b, 243139c1837SPaolo Bonzini OPC_ADDW = 0x3b, 244139c1837SPaolo Bonzini OPC_DIVUW = 0x200503b, 245139c1837SPaolo Bonzini OPC_DIVW = 0x200403b, 246139c1837SPaolo Bonzini OPC_MULW = 0x200003b, 247139c1837SPaolo Bonzini OPC_REMUW = 0x200703b, 248139c1837SPaolo Bonzini OPC_REMW = 0x200603b, 249139c1837SPaolo Bonzini OPC_SLLIW = 0x101b, 250139c1837SPaolo Bonzini OPC_SLLW = 0x103b, 251139c1837SPaolo Bonzini OPC_SRAIW = 0x4000501b, 252139c1837SPaolo Bonzini OPC_SRAW = 0x4000503b, 253139c1837SPaolo Bonzini OPC_SRLIW = 0x501b, 254139c1837SPaolo Bonzini OPC_SRLW = 0x503b, 255139c1837SPaolo Bonzini OPC_SUBW = 0x4000003b, 256139c1837SPaolo Bonzini 257139c1837SPaolo Bonzini OPC_FENCE = 0x0000000f, 2589ae958e4SRichard Henderson OPC_NOP = OPC_ADDI, /* nop = addi r0,r0,0 */ 2599e3e0bc6SRichard Henderson 2609e3e0bc6SRichard Henderson /* Zba: Bit manipulation extension, address generation */ 2619e3e0bc6SRichard Henderson OPC_ADD_UW = 0x0800003b, 2629e3e0bc6SRichard Henderson 2639e3e0bc6SRichard Henderson /* Zbb: Bit manipulation extension, basic bit manipulaton */ 2649e3e0bc6SRichard Henderson OPC_ANDN = 0x40007033, 2659e3e0bc6SRichard Henderson OPC_CLZ = 0x60001013, 2669e3e0bc6SRichard Henderson OPC_CLZW = 0x6000101b, 2679e3e0bc6SRichard Henderson OPC_CPOP = 0x60201013, 2689e3e0bc6SRichard Henderson OPC_CPOPW = 0x6020101b, 2699e3e0bc6SRichard Henderson OPC_CTZ = 0x60101013, 2709e3e0bc6SRichard Henderson OPC_CTZW = 0x6010101b, 2719e3e0bc6SRichard Henderson OPC_ORN = 0x40006033, 2729e3e0bc6SRichard Henderson OPC_REV8 = 0x6b805013, 2739e3e0bc6SRichard Henderson OPC_ROL = 0x60001033, 2749e3e0bc6SRichard Henderson OPC_ROLW = 0x6000103b, 2759e3e0bc6SRichard Henderson OPC_ROR = 0x60005033, 2769e3e0bc6SRichard Henderson OPC_RORW = 0x6000503b, 2779e3e0bc6SRichard Henderson OPC_RORI = 0x60005013, 2789e3e0bc6SRichard Henderson OPC_RORIW = 0x6000501b, 2799e3e0bc6SRichard Henderson OPC_SEXT_B = 0x60401013, 2809e3e0bc6SRichard Henderson OPC_SEXT_H = 0x60501013, 2819e3e0bc6SRichard Henderson OPC_XNOR = 0x40004033, 2829e3e0bc6SRichard Henderson OPC_ZEXT_H = 0x0800403b, 2839e3e0bc6SRichard Henderson 2849e3e0bc6SRichard Henderson /* Zicond: integer conditional operations */ 2859e3e0bc6SRichard Henderson OPC_CZERO_EQZ = 0x0e005033, 2869e3e0bc6SRichard Henderson OPC_CZERO_NEZ = 0x0e007033, 287139c1837SPaolo Bonzini} RISCVInsn; 288139c1837SPaolo Bonzini 289139c1837SPaolo Bonzini/* 290139c1837SPaolo Bonzini * RISC-V immediate and instruction encoders (excludes 16-bit RVC) 291139c1837SPaolo Bonzini */ 292139c1837SPaolo Bonzini 293139c1837SPaolo Bonzini/* Type-R */ 294139c1837SPaolo Bonzini 295139c1837SPaolo Bonzinistatic int32_t encode_r(RISCVInsn opc, TCGReg rd, TCGReg rs1, TCGReg rs2) 296139c1837SPaolo Bonzini{ 297139c1837SPaolo Bonzini return opc | (rd & 0x1f) << 7 | (rs1 & 0x1f) << 15 | (rs2 & 0x1f) << 20; 298139c1837SPaolo Bonzini} 299139c1837SPaolo Bonzini 300139c1837SPaolo Bonzini/* Type-I */ 301139c1837SPaolo Bonzini 302139c1837SPaolo Bonzinistatic int32_t encode_imm12(uint32_t imm) 303139c1837SPaolo Bonzini{ 304139c1837SPaolo Bonzini return (imm & 0xfff) << 20; 305139c1837SPaolo Bonzini} 306139c1837SPaolo Bonzini 307139c1837SPaolo Bonzinistatic int32_t encode_i(RISCVInsn opc, TCGReg rd, TCGReg rs1, uint32_t imm) 308139c1837SPaolo Bonzini{ 309139c1837SPaolo Bonzini return opc | (rd & 0x1f) << 7 | (rs1 & 0x1f) << 15 | encode_imm12(imm); 310139c1837SPaolo Bonzini} 311139c1837SPaolo Bonzini 312139c1837SPaolo Bonzini/* Type-S */ 313139c1837SPaolo Bonzini 314139c1837SPaolo Bonzinistatic int32_t encode_simm12(uint32_t imm) 315139c1837SPaolo Bonzini{ 316139c1837SPaolo Bonzini int32_t ret = 0; 317139c1837SPaolo Bonzini 318139c1837SPaolo Bonzini ret |= (imm & 0xFE0) << 20; 319139c1837SPaolo Bonzini ret |= (imm & 0x1F) << 7; 320139c1837SPaolo Bonzini 321139c1837SPaolo Bonzini return ret; 322139c1837SPaolo Bonzini} 323139c1837SPaolo Bonzini 324139c1837SPaolo Bonzinistatic int32_t encode_s(RISCVInsn opc, TCGReg rs1, TCGReg rs2, uint32_t imm) 325139c1837SPaolo Bonzini{ 326139c1837SPaolo Bonzini return opc | (rs1 & 0x1f) << 15 | (rs2 & 0x1f) << 20 | encode_simm12(imm); 327139c1837SPaolo Bonzini} 328139c1837SPaolo Bonzini 329139c1837SPaolo Bonzini/* Type-SB */ 330139c1837SPaolo Bonzini 331139c1837SPaolo Bonzinistatic int32_t encode_sbimm12(uint32_t imm) 332139c1837SPaolo Bonzini{ 333139c1837SPaolo Bonzini int32_t ret = 0; 334139c1837SPaolo Bonzini 335139c1837SPaolo Bonzini ret |= (imm & 0x1000) << 19; 336139c1837SPaolo Bonzini ret |= (imm & 0x7e0) << 20; 337139c1837SPaolo Bonzini ret |= (imm & 0x1e) << 7; 338139c1837SPaolo Bonzini ret |= (imm & 0x800) >> 4; 339139c1837SPaolo Bonzini 340139c1837SPaolo Bonzini return ret; 341139c1837SPaolo Bonzini} 342139c1837SPaolo Bonzini 343139c1837SPaolo Bonzinistatic int32_t encode_sb(RISCVInsn opc, TCGReg rs1, TCGReg rs2, uint32_t imm) 344139c1837SPaolo Bonzini{ 345139c1837SPaolo Bonzini return opc | (rs1 & 0x1f) << 15 | (rs2 & 0x1f) << 20 | encode_sbimm12(imm); 346139c1837SPaolo Bonzini} 347139c1837SPaolo Bonzini 348139c1837SPaolo Bonzini/* Type-U */ 349139c1837SPaolo Bonzini 350139c1837SPaolo Bonzinistatic int32_t encode_uimm20(uint32_t imm) 351139c1837SPaolo Bonzini{ 352139c1837SPaolo Bonzini return imm & 0xfffff000; 353139c1837SPaolo Bonzini} 354139c1837SPaolo Bonzini 355139c1837SPaolo Bonzinistatic int32_t encode_u(RISCVInsn opc, TCGReg rd, uint32_t imm) 356139c1837SPaolo Bonzini{ 357139c1837SPaolo Bonzini return opc | (rd & 0x1f) << 7 | encode_uimm20(imm); 358139c1837SPaolo Bonzini} 359139c1837SPaolo Bonzini 360139c1837SPaolo Bonzini/* Type-UJ */ 361139c1837SPaolo Bonzini 362139c1837SPaolo Bonzinistatic int32_t encode_ujimm20(uint32_t imm) 363139c1837SPaolo Bonzini{ 364139c1837SPaolo Bonzini int32_t ret = 0; 365139c1837SPaolo Bonzini 366139c1837SPaolo Bonzini ret |= (imm & 0x0007fe) << (21 - 1); 367139c1837SPaolo Bonzini ret |= (imm & 0x000800) << (20 - 11); 368139c1837SPaolo Bonzini ret |= (imm & 0x0ff000) << (12 - 12); 369139c1837SPaolo Bonzini ret |= (imm & 0x100000) << (31 - 20); 370139c1837SPaolo Bonzini 371139c1837SPaolo Bonzini return ret; 372139c1837SPaolo Bonzini} 373139c1837SPaolo Bonzini 374139c1837SPaolo Bonzinistatic int32_t encode_uj(RISCVInsn opc, TCGReg rd, uint32_t imm) 375139c1837SPaolo Bonzini{ 376139c1837SPaolo Bonzini return opc | (rd & 0x1f) << 7 | encode_ujimm20(imm); 377139c1837SPaolo Bonzini} 378139c1837SPaolo Bonzini 379139c1837SPaolo Bonzini/* 380139c1837SPaolo Bonzini * RISC-V instruction emitters 381139c1837SPaolo Bonzini */ 382139c1837SPaolo Bonzini 383139c1837SPaolo Bonzinistatic void tcg_out_opc_reg(TCGContext *s, RISCVInsn opc, 384139c1837SPaolo Bonzini TCGReg rd, TCGReg rs1, TCGReg rs2) 385139c1837SPaolo Bonzini{ 386139c1837SPaolo Bonzini tcg_out32(s, encode_r(opc, rd, rs1, rs2)); 387139c1837SPaolo Bonzini} 388139c1837SPaolo Bonzini 389139c1837SPaolo Bonzinistatic void tcg_out_opc_imm(TCGContext *s, RISCVInsn opc, 390139c1837SPaolo Bonzini TCGReg rd, TCGReg rs1, TCGArg imm) 391139c1837SPaolo Bonzini{ 392139c1837SPaolo Bonzini tcg_out32(s, encode_i(opc, rd, rs1, imm)); 393139c1837SPaolo Bonzini} 394139c1837SPaolo Bonzini 395139c1837SPaolo Bonzinistatic void tcg_out_opc_store(TCGContext *s, RISCVInsn opc, 396139c1837SPaolo Bonzini TCGReg rs1, TCGReg rs2, uint32_t imm) 397139c1837SPaolo Bonzini{ 398139c1837SPaolo Bonzini tcg_out32(s, encode_s(opc, rs1, rs2, imm)); 399139c1837SPaolo Bonzini} 400139c1837SPaolo Bonzini 401139c1837SPaolo Bonzinistatic void tcg_out_opc_branch(TCGContext *s, RISCVInsn opc, 402139c1837SPaolo Bonzini TCGReg rs1, TCGReg rs2, uint32_t imm) 403139c1837SPaolo Bonzini{ 404139c1837SPaolo Bonzini tcg_out32(s, encode_sb(opc, rs1, rs2, imm)); 405139c1837SPaolo Bonzini} 406139c1837SPaolo Bonzini 407139c1837SPaolo Bonzinistatic void tcg_out_opc_upper(TCGContext *s, RISCVInsn opc, 408139c1837SPaolo Bonzini TCGReg rd, uint32_t imm) 409139c1837SPaolo Bonzini{ 410139c1837SPaolo Bonzini tcg_out32(s, encode_u(opc, rd, imm)); 411139c1837SPaolo Bonzini} 412139c1837SPaolo Bonzini 413139c1837SPaolo Bonzinistatic void tcg_out_opc_jump(TCGContext *s, RISCVInsn opc, 414139c1837SPaolo Bonzini TCGReg rd, uint32_t imm) 415139c1837SPaolo Bonzini{ 416139c1837SPaolo Bonzini tcg_out32(s, encode_uj(opc, rd, imm)); 417139c1837SPaolo Bonzini} 418139c1837SPaolo Bonzini 419139c1837SPaolo Bonzinistatic void tcg_out_nop_fill(tcg_insn_unit *p, int count) 420139c1837SPaolo Bonzini{ 421139c1837SPaolo Bonzini int i; 422139c1837SPaolo Bonzini for (i = 0; i < count; ++i) { 4239ae958e4SRichard Henderson p[i] = OPC_NOP; 424139c1837SPaolo Bonzini } 425139c1837SPaolo Bonzini} 426139c1837SPaolo Bonzini 427139c1837SPaolo Bonzini/* 428139c1837SPaolo Bonzini * Relocations 429139c1837SPaolo Bonzini */ 430139c1837SPaolo Bonzini 431793f7381SRichard Hendersonstatic bool reloc_sbimm12(tcg_insn_unit *src_rw, const tcg_insn_unit *target) 432139c1837SPaolo Bonzini{ 433793f7381SRichard Henderson const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw); 434793f7381SRichard Henderson intptr_t offset = (intptr_t)target - (intptr_t)src_rx; 435139c1837SPaolo Bonzini 436844d0442SRichard Henderson tcg_debug_assert((offset & 1) == 0); 437844d0442SRichard Henderson if (offset == sextreg(offset, 0, 12)) { 438793f7381SRichard Henderson *src_rw |= encode_sbimm12(offset); 439139c1837SPaolo Bonzini return true; 440139c1837SPaolo Bonzini } 441139c1837SPaolo Bonzini 442139c1837SPaolo Bonzini return false; 443139c1837SPaolo Bonzini} 444139c1837SPaolo Bonzini 445793f7381SRichard Hendersonstatic bool reloc_jimm20(tcg_insn_unit *src_rw, const tcg_insn_unit *target) 446139c1837SPaolo Bonzini{ 447793f7381SRichard Henderson const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw); 448793f7381SRichard Henderson intptr_t offset = (intptr_t)target - (intptr_t)src_rx; 449139c1837SPaolo Bonzini 450844d0442SRichard Henderson tcg_debug_assert((offset & 1) == 0); 451844d0442SRichard Henderson if (offset == sextreg(offset, 0, 20)) { 452793f7381SRichard Henderson *src_rw |= encode_ujimm20(offset); 453139c1837SPaolo Bonzini return true; 454139c1837SPaolo Bonzini } 455139c1837SPaolo Bonzini 456139c1837SPaolo Bonzini return false; 457139c1837SPaolo Bonzini} 458139c1837SPaolo Bonzini 459793f7381SRichard Hendersonstatic bool reloc_call(tcg_insn_unit *src_rw, const tcg_insn_unit *target) 460139c1837SPaolo Bonzini{ 461793f7381SRichard Henderson const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw); 462793f7381SRichard Henderson intptr_t offset = (intptr_t)target - (intptr_t)src_rx; 463139c1837SPaolo Bonzini int32_t lo = sextreg(offset, 0, 12); 464139c1837SPaolo Bonzini int32_t hi = offset - lo; 465139c1837SPaolo Bonzini 466139c1837SPaolo Bonzini if (offset == hi + lo) { 467793f7381SRichard Henderson src_rw[0] |= encode_uimm20(hi); 468793f7381SRichard Henderson src_rw[1] |= encode_imm12(lo); 469139c1837SPaolo Bonzini return true; 470139c1837SPaolo Bonzini } 471139c1837SPaolo Bonzini 472139c1837SPaolo Bonzini return false; 473139c1837SPaolo Bonzini} 474139c1837SPaolo Bonzini 475139c1837SPaolo Bonzinistatic bool patch_reloc(tcg_insn_unit *code_ptr, int type, 476139c1837SPaolo Bonzini intptr_t value, intptr_t addend) 477139c1837SPaolo Bonzini{ 478139c1837SPaolo Bonzini tcg_debug_assert(addend == 0); 479139c1837SPaolo Bonzini switch (type) { 480139c1837SPaolo Bonzini case R_RISCV_BRANCH: 481139c1837SPaolo Bonzini return reloc_sbimm12(code_ptr, (tcg_insn_unit *)value); 482139c1837SPaolo Bonzini case R_RISCV_JAL: 483139c1837SPaolo Bonzini return reloc_jimm20(code_ptr, (tcg_insn_unit *)value); 484139c1837SPaolo Bonzini case R_RISCV_CALL: 485139c1837SPaolo Bonzini return reloc_call(code_ptr, (tcg_insn_unit *)value); 486139c1837SPaolo Bonzini default: 4874b6a52d0SRichard Henderson g_assert_not_reached(); 488139c1837SPaolo Bonzini } 489139c1837SPaolo Bonzini} 490139c1837SPaolo Bonzini 491139c1837SPaolo Bonzini/* 492139c1837SPaolo Bonzini * TCG intrinsics 493139c1837SPaolo Bonzini */ 494139c1837SPaolo Bonzini 495139c1837SPaolo Bonzinistatic bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg) 496139c1837SPaolo Bonzini{ 497139c1837SPaolo Bonzini if (ret == arg) { 498139c1837SPaolo Bonzini return true; 499139c1837SPaolo Bonzini } 500139c1837SPaolo Bonzini switch (type) { 501139c1837SPaolo Bonzini case TCG_TYPE_I32: 502139c1837SPaolo Bonzini case TCG_TYPE_I64: 503139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_ADDI, ret, arg, 0); 504139c1837SPaolo Bonzini break; 505139c1837SPaolo Bonzini default: 506139c1837SPaolo Bonzini g_assert_not_reached(); 507139c1837SPaolo Bonzini } 508139c1837SPaolo Bonzini return true; 509139c1837SPaolo Bonzini} 510139c1837SPaolo Bonzini 511139c1837SPaolo Bonzinistatic void tcg_out_movi(TCGContext *s, TCGType type, TCGReg rd, 512139c1837SPaolo Bonzini tcg_target_long val) 513139c1837SPaolo Bonzini{ 514139c1837SPaolo Bonzini tcg_target_long lo, hi, tmp; 515139c1837SPaolo Bonzini int shift, ret; 516139c1837SPaolo Bonzini 517aeb6326eSRichard Henderson if (type == TCG_TYPE_I32) { 518139c1837SPaolo Bonzini val = (int32_t)val; 519139c1837SPaolo Bonzini } 520139c1837SPaolo Bonzini 521139c1837SPaolo Bonzini lo = sextreg(val, 0, 12); 522139c1837SPaolo Bonzini if (val == lo) { 523139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_ADDI, rd, TCG_REG_ZERO, lo); 524139c1837SPaolo Bonzini return; 525139c1837SPaolo Bonzini } 526139c1837SPaolo Bonzini 527139c1837SPaolo Bonzini hi = val - lo; 528aeb6326eSRichard Henderson if (val == (int32_t)val) { 529139c1837SPaolo Bonzini tcg_out_opc_upper(s, OPC_LUI, rd, hi); 530139c1837SPaolo Bonzini if (lo != 0) { 531139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_ADDIW, rd, rd, lo); 532139c1837SPaolo Bonzini } 533139c1837SPaolo Bonzini return; 534139c1837SPaolo Bonzini } 535139c1837SPaolo Bonzini 536139c1837SPaolo Bonzini tmp = tcg_pcrel_diff(s, (void *)val); 537139c1837SPaolo Bonzini if (tmp == (int32_t)tmp) { 538139c1837SPaolo Bonzini tcg_out_opc_upper(s, OPC_AUIPC, rd, 0); 539139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_ADDI, rd, rd, 0); 540793f7381SRichard Henderson ret = reloc_call(s->code_ptr - 2, (const tcg_insn_unit *)val); 541139c1837SPaolo Bonzini tcg_debug_assert(ret == true); 542139c1837SPaolo Bonzini return; 543139c1837SPaolo Bonzini } 544139c1837SPaolo Bonzini 545139c1837SPaolo Bonzini /* Look for a single 20-bit section. */ 546139c1837SPaolo Bonzini shift = ctz64(val); 547139c1837SPaolo Bonzini tmp = val >> shift; 548139c1837SPaolo Bonzini if (tmp == sextreg(tmp, 0, 20)) { 549139c1837SPaolo Bonzini tcg_out_opc_upper(s, OPC_LUI, rd, tmp << 12); 550139c1837SPaolo Bonzini if (shift > 12) { 551139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_SLLI, rd, rd, shift - 12); 552139c1837SPaolo Bonzini } else { 553139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_SRAI, rd, rd, 12 - shift); 554139c1837SPaolo Bonzini } 555139c1837SPaolo Bonzini return; 556139c1837SPaolo Bonzini } 557139c1837SPaolo Bonzini 558139c1837SPaolo Bonzini /* Look for a few high zero bits, with lots of bits set in the middle. */ 559139c1837SPaolo Bonzini shift = clz64(val); 560139c1837SPaolo Bonzini tmp = val << shift; 561139c1837SPaolo Bonzini if (tmp == sextreg(tmp, 12, 20) << 12) { 562139c1837SPaolo Bonzini tcg_out_opc_upper(s, OPC_LUI, rd, tmp); 563139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_SRLI, rd, rd, shift); 564139c1837SPaolo Bonzini return; 565139c1837SPaolo Bonzini } else if (tmp == sextreg(tmp, 0, 12)) { 566139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_ADDI, rd, TCG_REG_ZERO, tmp); 567139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_SRLI, rd, rd, shift); 568139c1837SPaolo Bonzini return; 569139c1837SPaolo Bonzini } 570139c1837SPaolo Bonzini 571139c1837SPaolo Bonzini /* Drop into the constant pool. */ 572139c1837SPaolo Bonzini new_pool_label(s, val, R_RISCV_CALL, s->code_ptr, 0); 573139c1837SPaolo Bonzini tcg_out_opc_upper(s, OPC_AUIPC, rd, 0); 574139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_LD, rd, rd, 0); 575139c1837SPaolo Bonzini} 576139c1837SPaolo Bonzini 577767c2503SRichard Hendersonstatic bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2) 578767c2503SRichard Henderson{ 579767c2503SRichard Henderson return false; 580767c2503SRichard Henderson} 581767c2503SRichard Henderson 5826a6d772eSRichard Hendersonstatic void tcg_out_addi_ptr(TCGContext *s, TCGReg rd, TCGReg rs, 5836a6d772eSRichard Henderson tcg_target_long imm) 5846a6d772eSRichard Henderson{ 5856a6d772eSRichard Henderson /* This function is only used for passing structs by reference. */ 5866a6d772eSRichard Henderson g_assert_not_reached(); 5876a6d772eSRichard Henderson} 5886a6d772eSRichard Henderson 589139c1837SPaolo Bonzinistatic void tcg_out_ext8u(TCGContext *s, TCGReg ret, TCGReg arg) 590139c1837SPaolo Bonzini{ 591139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_ANDI, ret, arg, 0xff); 592139c1837SPaolo Bonzini} 593139c1837SPaolo Bonzini 594139c1837SPaolo Bonzinistatic void tcg_out_ext16u(TCGContext *s, TCGReg ret, TCGReg arg) 595139c1837SPaolo Bonzini{ 596d1c3f4e9SRichard Henderson if (have_zbb) { 597d1c3f4e9SRichard Henderson tcg_out_opc_reg(s, OPC_ZEXT_H, ret, arg, TCG_REG_ZERO); 598d1c3f4e9SRichard Henderson } else { 599139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_SLLIW, ret, arg, 16); 600139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_SRLIW, ret, ret, 16); 601139c1837SPaolo Bonzini } 602d1c3f4e9SRichard Henderson} 603139c1837SPaolo Bonzini 604139c1837SPaolo Bonzinistatic void tcg_out_ext32u(TCGContext *s, TCGReg ret, TCGReg arg) 605139c1837SPaolo Bonzini{ 606d1c3f4e9SRichard Henderson if (have_zba) { 607d1c3f4e9SRichard Henderson tcg_out_opc_reg(s, OPC_ADD_UW, ret, arg, TCG_REG_ZERO); 608d1c3f4e9SRichard Henderson } else { 609139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_SLLI, ret, arg, 32); 610139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_SRLI, ret, ret, 32); 611139c1837SPaolo Bonzini } 612d1c3f4e9SRichard Henderson} 613139c1837SPaolo Bonzini 614678155b2SRichard Hendersonstatic void tcg_out_ext8s(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg) 615139c1837SPaolo Bonzini{ 616d1c3f4e9SRichard Henderson if (have_zbb) { 617d1c3f4e9SRichard Henderson tcg_out_opc_imm(s, OPC_SEXT_B, ret, arg, 0); 618d1c3f4e9SRichard Henderson } else { 619139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_SLLIW, ret, arg, 24); 620139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_SRAIW, ret, ret, 24); 621139c1837SPaolo Bonzini } 622d1c3f4e9SRichard Henderson} 623139c1837SPaolo Bonzini 624753e42eaSRichard Hendersonstatic void tcg_out_ext16s(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg) 625139c1837SPaolo Bonzini{ 626d1c3f4e9SRichard Henderson if (have_zbb) { 627d1c3f4e9SRichard Henderson tcg_out_opc_imm(s, OPC_SEXT_H, ret, arg, 0); 628d1c3f4e9SRichard Henderson } else { 629139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_SLLIW, ret, arg, 16); 630139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_SRAIW, ret, ret, 16); 631139c1837SPaolo Bonzini } 632d1c3f4e9SRichard Henderson} 633139c1837SPaolo Bonzini 634139c1837SPaolo Bonzinistatic void tcg_out_ext32s(TCGContext *s, TCGReg ret, TCGReg arg) 635139c1837SPaolo Bonzini{ 636139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_ADDIW, ret, arg, 0); 637139c1837SPaolo Bonzini} 638139c1837SPaolo Bonzini 6399c6aa274SRichard Hendersonstatic void tcg_out_exts_i32_i64(TCGContext *s, TCGReg ret, TCGReg arg) 6409c6aa274SRichard Henderson{ 6413ea9be33SRichard Henderson if (ret != arg) { 6429c6aa274SRichard Henderson tcg_out_ext32s(s, ret, arg); 6439c6aa274SRichard Henderson } 6443ea9be33SRichard Henderson} 6459c6aa274SRichard Henderson 646b9bfe000SRichard Hendersonstatic void tcg_out_extu_i32_i64(TCGContext *s, TCGReg ret, TCGReg arg) 647b9bfe000SRichard Henderson{ 648b9bfe000SRichard Henderson tcg_out_ext32u(s, ret, arg); 649b9bfe000SRichard Henderson} 650b9bfe000SRichard Henderson 651b8b94ac6SRichard Hendersonstatic void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg ret, TCGReg arg) 652b8b94ac6SRichard Henderson{ 653b8b94ac6SRichard Henderson tcg_out_ext32s(s, ret, arg); 654b8b94ac6SRichard Henderson} 655b8b94ac6SRichard Henderson 656139c1837SPaolo Bonzinistatic void tcg_out_ldst(TCGContext *s, RISCVInsn opc, TCGReg data, 657139c1837SPaolo Bonzini TCGReg addr, intptr_t offset) 658139c1837SPaolo Bonzini{ 659139c1837SPaolo Bonzini intptr_t imm12 = sextreg(offset, 0, 12); 660139c1837SPaolo Bonzini 661139c1837SPaolo Bonzini if (offset != imm12) { 6629d9db413SRichard Henderson intptr_t diff = tcg_pcrel_diff(s, (void *)offset); 663139c1837SPaolo Bonzini 664139c1837SPaolo Bonzini if (addr == TCG_REG_ZERO && diff == (int32_t)diff) { 665139c1837SPaolo Bonzini imm12 = sextreg(diff, 0, 12); 666139c1837SPaolo Bonzini tcg_out_opc_upper(s, OPC_AUIPC, TCG_REG_TMP2, diff - imm12); 667139c1837SPaolo Bonzini } else { 668139c1837SPaolo Bonzini tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP2, offset - imm12); 669139c1837SPaolo Bonzini if (addr != TCG_REG_ZERO) { 670139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP2, TCG_REG_TMP2, addr); 671139c1837SPaolo Bonzini } 672139c1837SPaolo Bonzini } 673139c1837SPaolo Bonzini addr = TCG_REG_TMP2; 674139c1837SPaolo Bonzini } 675139c1837SPaolo Bonzini 676139c1837SPaolo Bonzini switch (opc) { 677139c1837SPaolo Bonzini case OPC_SB: 678139c1837SPaolo Bonzini case OPC_SH: 679139c1837SPaolo Bonzini case OPC_SW: 680139c1837SPaolo Bonzini case OPC_SD: 681139c1837SPaolo Bonzini tcg_out_opc_store(s, opc, addr, data, imm12); 682139c1837SPaolo Bonzini break; 683139c1837SPaolo Bonzini case OPC_LB: 684139c1837SPaolo Bonzini case OPC_LBU: 685139c1837SPaolo Bonzini case OPC_LH: 686139c1837SPaolo Bonzini case OPC_LHU: 687139c1837SPaolo Bonzini case OPC_LW: 688139c1837SPaolo Bonzini case OPC_LWU: 689139c1837SPaolo Bonzini case OPC_LD: 690139c1837SPaolo Bonzini tcg_out_opc_imm(s, opc, data, addr, imm12); 691139c1837SPaolo Bonzini break; 692139c1837SPaolo Bonzini default: 693139c1837SPaolo Bonzini g_assert_not_reached(); 694139c1837SPaolo Bonzini } 695139c1837SPaolo Bonzini} 696139c1837SPaolo Bonzini 697139c1837SPaolo Bonzinistatic void tcg_out_ld(TCGContext *s, TCGType type, TCGReg arg, 698139c1837SPaolo Bonzini TCGReg arg1, intptr_t arg2) 699139c1837SPaolo Bonzini{ 700aeb6326eSRichard Henderson RISCVInsn insn = type == TCG_TYPE_I32 ? OPC_LW : OPC_LD; 701aeb6326eSRichard Henderson tcg_out_ldst(s, insn, arg, arg1, arg2); 702139c1837SPaolo Bonzini} 703139c1837SPaolo Bonzini 704139c1837SPaolo Bonzinistatic void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg, 705139c1837SPaolo Bonzini TCGReg arg1, intptr_t arg2) 706139c1837SPaolo Bonzini{ 707aeb6326eSRichard Henderson RISCVInsn insn = type == TCG_TYPE_I32 ? OPC_SW : OPC_SD; 708aeb6326eSRichard Henderson tcg_out_ldst(s, insn, arg, arg1, arg2); 709139c1837SPaolo Bonzini} 710139c1837SPaolo Bonzini 711139c1837SPaolo Bonzinistatic bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val, 712139c1837SPaolo Bonzini TCGReg base, intptr_t ofs) 713139c1837SPaolo Bonzini{ 714139c1837SPaolo Bonzini if (val == 0) { 715139c1837SPaolo Bonzini tcg_out_st(s, type, TCG_REG_ZERO, base, ofs); 716139c1837SPaolo Bonzini return true; 717139c1837SPaolo Bonzini } 718139c1837SPaolo Bonzini return false; 719139c1837SPaolo Bonzini} 720139c1837SPaolo Bonzini 721139c1837SPaolo Bonzinistatic void tcg_out_addsub2(TCGContext *s, 722139c1837SPaolo Bonzini TCGReg rl, TCGReg rh, 723139c1837SPaolo Bonzini TCGReg al, TCGReg ah, 724139c1837SPaolo Bonzini TCGArg bl, TCGArg bh, 725139c1837SPaolo Bonzini bool cbl, bool cbh, bool is_sub, bool is32bit) 726139c1837SPaolo Bonzini{ 727139c1837SPaolo Bonzini const RISCVInsn opc_add = is32bit ? OPC_ADDW : OPC_ADD; 728139c1837SPaolo Bonzini const RISCVInsn opc_addi = is32bit ? OPC_ADDIW : OPC_ADDI; 729139c1837SPaolo Bonzini const RISCVInsn opc_sub = is32bit ? OPC_SUBW : OPC_SUB; 730139c1837SPaolo Bonzini TCGReg th = TCG_REG_TMP1; 731139c1837SPaolo Bonzini 732139c1837SPaolo Bonzini /* If we have a negative constant such that negating it would 733139c1837SPaolo Bonzini make the high part zero, we can (usually) eliminate one insn. */ 734139c1837SPaolo Bonzini if (cbl && cbh && bh == -1 && bl != 0) { 735139c1837SPaolo Bonzini bl = -bl; 736139c1837SPaolo Bonzini bh = 0; 737139c1837SPaolo Bonzini is_sub = !is_sub; 738139c1837SPaolo Bonzini } 739139c1837SPaolo Bonzini 740139c1837SPaolo Bonzini /* By operating on the high part first, we get to use the final 741139c1837SPaolo Bonzini carry operation to move back from the temporary. */ 742139c1837SPaolo Bonzini if (!cbh) { 743139c1837SPaolo Bonzini tcg_out_opc_reg(s, (is_sub ? opc_sub : opc_add), th, ah, bh); 744139c1837SPaolo Bonzini } else if (bh != 0 || ah == rl) { 745139c1837SPaolo Bonzini tcg_out_opc_imm(s, opc_addi, th, ah, (is_sub ? -bh : bh)); 746139c1837SPaolo Bonzini } else { 747139c1837SPaolo Bonzini th = ah; 748139c1837SPaolo Bonzini } 749139c1837SPaolo Bonzini 750139c1837SPaolo Bonzini /* Note that tcg optimization should eliminate the bl == 0 case. */ 751139c1837SPaolo Bonzini if (is_sub) { 752139c1837SPaolo Bonzini if (cbl) { 753139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_SLTIU, TCG_REG_TMP0, al, bl); 754139c1837SPaolo Bonzini tcg_out_opc_imm(s, opc_addi, rl, al, -bl); 755139c1837SPaolo Bonzini } else { 756139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_SLTU, TCG_REG_TMP0, al, bl); 757139c1837SPaolo Bonzini tcg_out_opc_reg(s, opc_sub, rl, al, bl); 758139c1837SPaolo Bonzini } 759139c1837SPaolo Bonzini tcg_out_opc_reg(s, opc_sub, rh, th, TCG_REG_TMP0); 760139c1837SPaolo Bonzini } else { 761139c1837SPaolo Bonzini if (cbl) { 762139c1837SPaolo Bonzini tcg_out_opc_imm(s, opc_addi, rl, al, bl); 763139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_SLTIU, TCG_REG_TMP0, rl, bl); 7649b246685SRichard Henderson } else if (al == bl) { 7659b246685SRichard Henderson /* 7669b246685SRichard Henderson * If the input regs overlap, this is a simple doubling 7679b246685SRichard Henderson * and carry-out is the input msb. This special case is 7689b246685SRichard Henderson * required when the output reg overlaps the input, 7699b246685SRichard Henderson * but we might as well use it always. 7709b246685SRichard Henderson */ 771139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_SLTI, TCG_REG_TMP0, al, 0); 7729b246685SRichard Henderson tcg_out_opc_reg(s, opc_add, rl, al, al); 773139c1837SPaolo Bonzini } else { 774139c1837SPaolo Bonzini tcg_out_opc_reg(s, opc_add, rl, al, bl); 775139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_SLTU, TCG_REG_TMP0, 776139c1837SPaolo Bonzini rl, (rl == bl ? al : bl)); 777139c1837SPaolo Bonzini } 778139c1837SPaolo Bonzini tcg_out_opc_reg(s, opc_add, rh, th, TCG_REG_TMP0); 779139c1837SPaolo Bonzini } 780139c1837SPaolo Bonzini} 781139c1837SPaolo Bonzini 782139c1837SPaolo Bonzinistatic const struct { 783139c1837SPaolo Bonzini RISCVInsn op; 784139c1837SPaolo Bonzini bool swap; 785139c1837SPaolo Bonzini} tcg_brcond_to_riscv[] = { 786139c1837SPaolo Bonzini [TCG_COND_EQ] = { OPC_BEQ, false }, 787139c1837SPaolo Bonzini [TCG_COND_NE] = { OPC_BNE, false }, 788139c1837SPaolo Bonzini [TCG_COND_LT] = { OPC_BLT, false }, 789139c1837SPaolo Bonzini [TCG_COND_GE] = { OPC_BGE, false }, 790139c1837SPaolo Bonzini [TCG_COND_LE] = { OPC_BGE, true }, 791139c1837SPaolo Bonzini [TCG_COND_GT] = { OPC_BLT, true }, 792139c1837SPaolo Bonzini [TCG_COND_LTU] = { OPC_BLTU, false }, 793139c1837SPaolo Bonzini [TCG_COND_GEU] = { OPC_BGEU, false }, 794139c1837SPaolo Bonzini [TCG_COND_LEU] = { OPC_BGEU, true }, 795139c1837SPaolo Bonzini [TCG_COND_GTU] = { OPC_BLTU, true } 796139c1837SPaolo Bonzini}; 797139c1837SPaolo Bonzini 798139c1837SPaolo Bonzinistatic void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1, 799139c1837SPaolo Bonzini TCGReg arg2, TCGLabel *l) 800139c1837SPaolo Bonzini{ 801139c1837SPaolo Bonzini RISCVInsn op = tcg_brcond_to_riscv[cond].op; 802139c1837SPaolo Bonzini 803139c1837SPaolo Bonzini tcg_debug_assert(op != 0); 804139c1837SPaolo Bonzini 805139c1837SPaolo Bonzini if (tcg_brcond_to_riscv[cond].swap) { 806139c1837SPaolo Bonzini TCGReg t = arg1; 807139c1837SPaolo Bonzini arg1 = arg2; 808139c1837SPaolo Bonzini arg2 = t; 809139c1837SPaolo Bonzini } 810139c1837SPaolo Bonzini 811139c1837SPaolo Bonzini tcg_out_reloc(s, s->code_ptr, R_RISCV_BRANCH, l, 0); 812139c1837SPaolo Bonzini tcg_out_opc_branch(s, op, arg1, arg2, 0); 813139c1837SPaolo Bonzini} 814139c1837SPaolo Bonzini 815139c1837SPaolo Bonzinistatic void tcg_out_setcond(TCGContext *s, TCGCond cond, TCGReg ret, 816139c1837SPaolo Bonzini TCGReg arg1, TCGReg arg2) 817139c1837SPaolo Bonzini{ 818139c1837SPaolo Bonzini switch (cond) { 819139c1837SPaolo Bonzini case TCG_COND_EQ: 820139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_SUB, ret, arg1, arg2); 821139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_SLTIU, ret, ret, 1); 822139c1837SPaolo Bonzini break; 823139c1837SPaolo Bonzini case TCG_COND_NE: 824139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_SUB, ret, arg1, arg2); 825139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_SLTU, ret, TCG_REG_ZERO, ret); 826139c1837SPaolo Bonzini break; 827139c1837SPaolo Bonzini case TCG_COND_LT: 828139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_SLT, ret, arg1, arg2); 829139c1837SPaolo Bonzini break; 830139c1837SPaolo Bonzini case TCG_COND_GE: 831139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_SLT, ret, arg1, arg2); 832139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1); 833139c1837SPaolo Bonzini break; 834139c1837SPaolo Bonzini case TCG_COND_LE: 835139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_SLT, ret, arg2, arg1); 836139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1); 837139c1837SPaolo Bonzini break; 838139c1837SPaolo Bonzini case TCG_COND_GT: 839139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_SLT, ret, arg2, arg1); 840139c1837SPaolo Bonzini break; 841139c1837SPaolo Bonzini case TCG_COND_LTU: 842139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_SLTU, ret, arg1, arg2); 843139c1837SPaolo Bonzini break; 844139c1837SPaolo Bonzini case TCG_COND_GEU: 845139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_SLTU, ret, arg1, arg2); 846139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1); 847139c1837SPaolo Bonzini break; 848139c1837SPaolo Bonzini case TCG_COND_LEU: 849139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_SLTU, ret, arg2, arg1); 850139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1); 851139c1837SPaolo Bonzini break; 852139c1837SPaolo Bonzini case TCG_COND_GTU: 853139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_SLTU, ret, arg2, arg1); 854139c1837SPaolo Bonzini break; 855139c1837SPaolo Bonzini default: 856139c1837SPaolo Bonzini g_assert_not_reached(); 857139c1837SPaolo Bonzini break; 858139c1837SPaolo Bonzini } 859139c1837SPaolo Bonzini} 860139c1837SPaolo Bonzini 8612be7d76bSRichard Hendersonstatic void tcg_out_call_int(TCGContext *s, const tcg_insn_unit *arg, bool tail) 862139c1837SPaolo Bonzini{ 863139c1837SPaolo Bonzini TCGReg link = tail ? TCG_REG_ZERO : TCG_REG_RA; 864139c1837SPaolo Bonzini ptrdiff_t offset = tcg_pcrel_diff(s, arg); 865139c1837SPaolo Bonzini int ret; 866139c1837SPaolo Bonzini 867844d0442SRichard Henderson tcg_debug_assert((offset & 1) == 0); 868844d0442SRichard Henderson if (offset == sextreg(offset, 0, 20)) { 869139c1837SPaolo Bonzini /* short jump: -2097150 to 2097152 */ 870139c1837SPaolo Bonzini tcg_out_opc_jump(s, OPC_JAL, link, offset); 871aeb6326eSRichard Henderson } else if (offset == (int32_t)offset) { 872139c1837SPaolo Bonzini /* long jump: -2147483646 to 2147483648 */ 873139c1837SPaolo Bonzini tcg_out_opc_upper(s, OPC_AUIPC, TCG_REG_TMP0, 0); 874139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_JALR, link, TCG_REG_TMP0, 0); 875844d0442SRichard Henderson ret = reloc_call(s->code_ptr - 2, arg); 876139c1837SPaolo Bonzini tcg_debug_assert(ret == true); 877aeb6326eSRichard Henderson } else { 878139c1837SPaolo Bonzini /* far jump: 64-bit */ 879139c1837SPaolo Bonzini tcg_target_long imm = sextreg((tcg_target_long)arg, 0, 12); 880139c1837SPaolo Bonzini tcg_target_long base = (tcg_target_long)arg - imm; 881139c1837SPaolo Bonzini tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP0, base); 882139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_JALR, link, TCG_REG_TMP0, imm); 883139c1837SPaolo Bonzini } 884139c1837SPaolo Bonzini} 885139c1837SPaolo Bonzini 886cee44b03SRichard Hendersonstatic void tcg_out_call(TCGContext *s, const tcg_insn_unit *arg, 887cee44b03SRichard Henderson const TCGHelperInfo *info) 888139c1837SPaolo Bonzini{ 889139c1837SPaolo Bonzini tcg_out_call_int(s, arg, false); 890139c1837SPaolo Bonzini} 891139c1837SPaolo Bonzini 892139c1837SPaolo Bonzinistatic void tcg_out_mb(TCGContext *s, TCGArg a0) 893139c1837SPaolo Bonzini{ 894139c1837SPaolo Bonzini tcg_insn_unit insn = OPC_FENCE; 895139c1837SPaolo Bonzini 896139c1837SPaolo Bonzini if (a0 & TCG_MO_LD_LD) { 897139c1837SPaolo Bonzini insn |= 0x02200000; 898139c1837SPaolo Bonzini } 899139c1837SPaolo Bonzini if (a0 & TCG_MO_ST_LD) { 900139c1837SPaolo Bonzini insn |= 0x01200000; 901139c1837SPaolo Bonzini } 902139c1837SPaolo Bonzini if (a0 & TCG_MO_LD_ST) { 903139c1837SPaolo Bonzini insn |= 0x02100000; 904139c1837SPaolo Bonzini } 905139c1837SPaolo Bonzini if (a0 & TCG_MO_ST_ST) { 906139c1837SPaolo Bonzini insn |= 0x02200000; 907139c1837SPaolo Bonzini } 908139c1837SPaolo Bonzini tcg_out32(s, insn); 909139c1837SPaolo Bonzini} 910139c1837SPaolo Bonzini 911139c1837SPaolo Bonzini/* 912139c1837SPaolo Bonzini * Load/store and TLB 913139c1837SPaolo Bonzini */ 914139c1837SPaolo Bonzini 915793f7381SRichard Hendersonstatic void tcg_out_goto(TCGContext *s, const tcg_insn_unit *target) 916844d0442SRichard Henderson{ 917844d0442SRichard Henderson tcg_out_opc_jump(s, OPC_JAL, TCG_REG_ZERO, 0); 918844d0442SRichard Henderson bool ok = reloc_jimm20(s->code_ptr - 1, target); 919844d0442SRichard Henderson tcg_debug_assert(ok); 920844d0442SRichard Henderson} 921844d0442SRichard Henderson 9227b880107SRichard Hendersonbool tcg_target_has_memory_bswap(MemOp memop) 9237b880107SRichard Henderson{ 9247b880107SRichard Henderson return false; 9257b880107SRichard Henderson} 9267b880107SRichard Henderson 92761b6daafSRichard Henderson/* We have three temps, we might as well expose them. */ 92861b6daafSRichard Hendersonstatic const TCGLdstHelperParam ldst_helper_param = { 92961b6daafSRichard Henderson .ntmp = 3, .tmp = { TCG_REG_TMP0, TCG_REG_TMP1, TCG_REG_TMP2 } 93061b6daafSRichard Henderson}; 93161b6daafSRichard Henderson 932139c1837SPaolo Bonzinistatic bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l) 933139c1837SPaolo Bonzini{ 93461b6daafSRichard Henderson MemOp opc = get_memop(l->oi); 935139c1837SPaolo Bonzini 936139c1837SPaolo Bonzini /* resolve label address */ 937793f7381SRichard Henderson if (!reloc_sbimm12(l->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) { 938139c1837SPaolo Bonzini return false; 939139c1837SPaolo Bonzini } 940139c1837SPaolo Bonzini 941139c1837SPaolo Bonzini /* call load helper */ 94261b6daafSRichard Henderson tcg_out_ld_helper_args(s, l, &ldst_helper_param); 943cee44b03SRichard Henderson tcg_out_call_int(s, qemu_ld_helpers[opc & MO_SSIZE], false); 94461b6daafSRichard Henderson tcg_out_ld_helper_ret(s, l, true, &ldst_helper_param); 945139c1837SPaolo Bonzini 946139c1837SPaolo Bonzini tcg_out_goto(s, l->raddr); 947139c1837SPaolo Bonzini return true; 948139c1837SPaolo Bonzini} 949139c1837SPaolo Bonzini 950139c1837SPaolo Bonzinistatic bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l) 951139c1837SPaolo Bonzini{ 95261b6daafSRichard Henderson MemOp opc = get_memop(l->oi); 953139c1837SPaolo Bonzini 954139c1837SPaolo Bonzini /* resolve label address */ 955793f7381SRichard Henderson if (!reloc_sbimm12(l->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) { 956139c1837SPaolo Bonzini return false; 957139c1837SPaolo Bonzini } 958139c1837SPaolo Bonzini 959139c1837SPaolo Bonzini /* call store helper */ 96061b6daafSRichard Henderson tcg_out_st_helper_args(s, l, &ldst_helper_param); 961cee44b03SRichard Henderson tcg_out_call_int(s, qemu_st_helpers[opc & MO_SIZE], false); 962139c1837SPaolo Bonzini 963139c1837SPaolo Bonzini tcg_out_goto(s, l->raddr); 964139c1837SPaolo Bonzini return true; 965139c1837SPaolo Bonzini} 966139c1837SPaolo Bonzini 967001dddfeSRichard Henderson/* 968001dddfeSRichard Henderson * For softmmu, perform the TLB load and compare. 969001dddfeSRichard Henderson * For useronly, perform any required alignment tests. 970001dddfeSRichard Henderson * In both cases, return a TCGLabelQemuLdst structure if the slow path 971001dddfeSRichard Henderson * is required and fill in @h with the host address for the fast path. 972001dddfeSRichard Henderson */ 973001dddfeSRichard Hendersonstatic TCGLabelQemuLdst *prepare_host_addr(TCGContext *s, TCGReg *pbase, 974001dddfeSRichard Henderson TCGReg addr_reg, MemOpIdx oi, 975001dddfeSRichard Henderson bool is_ld) 976001dddfeSRichard Henderson{ 977001dddfeSRichard Henderson TCGLabelQemuLdst *ldst = NULL; 978001dddfeSRichard Henderson MemOp opc = get_memop(oi); 97937e523f0SRichard Henderson TCGAtomAlign aa; 98037e523f0SRichard Henderson unsigned a_mask; 98137e523f0SRichard Henderson 98237e523f0SRichard Henderson aa = atom_and_align_for_opc(s, opc, MO_ATOM_IFALIGN, false); 98337e523f0SRichard Henderson a_mask = (1u << aa.align) - 1; 984001dddfeSRichard Henderson 985001dddfeSRichard Henderson#ifdef CONFIG_SOFTMMU 986001dddfeSRichard Henderson unsigned s_bits = opc & MO_SIZE; 987933b331bSRichard Henderson unsigned s_mask = (1u << s_bits) - 1; 988001dddfeSRichard Henderson int mem_index = get_mmuidx(oi); 989001dddfeSRichard Henderson int fast_ofs = TLB_MASK_TABLE_OFS(mem_index); 990001dddfeSRichard Henderson int mask_ofs = fast_ofs + offsetof(CPUTLBDescFast, mask); 991001dddfeSRichard Henderson int table_ofs = fast_ofs + offsetof(CPUTLBDescFast, table); 992933b331bSRichard Henderson int compare_mask; 993933b331bSRichard Henderson TCGReg addr_adj; 994001dddfeSRichard Henderson 995001dddfeSRichard Henderson ldst = new_ldst_label(s); 996001dddfeSRichard Henderson ldst->is_ld = is_ld; 997001dddfeSRichard Henderson ldst->oi = oi; 998001dddfeSRichard Henderson ldst->addrlo_reg = addr_reg; 999001dddfeSRichard Henderson 1000001dddfeSRichard Henderson QEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) > 0); 1001001dddfeSRichard Henderson QEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) < -(1 << 11)); 1002933b331bSRichard Henderson tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP0, TCG_AREG0, mask_ofs); 1003933b331bSRichard Henderson tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP1, TCG_AREG0, table_ofs); 1004001dddfeSRichard Henderson 1005001dddfeSRichard Henderson tcg_out_opc_imm(s, OPC_SRLI, TCG_REG_TMP2, addr_reg, 1006aece72b7SRichard Henderson s->page_bits - CPU_TLB_ENTRY_BITS); 1007001dddfeSRichard Henderson tcg_out_opc_reg(s, OPC_AND, TCG_REG_TMP2, TCG_REG_TMP2, TCG_REG_TMP0); 1008001dddfeSRichard Henderson tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP2, TCG_REG_TMP2, TCG_REG_TMP1); 1009001dddfeSRichard Henderson 1010933b331bSRichard Henderson /* 1011933b331bSRichard Henderson * For aligned accesses, we check the first byte and include the alignment 1012933b331bSRichard Henderson * bits within the address. For unaligned access, we check that we don't 1013933b331bSRichard Henderson * cross pages using the address of the last byte of the access. 1014933b331bSRichard Henderson */ 1015933b331bSRichard Henderson addr_adj = addr_reg; 101637e523f0SRichard Henderson if (a_mask < s_mask) { 1017933b331bSRichard Henderson addr_adj = TCG_REG_TMP0; 1018933b331bSRichard Henderson tcg_out_opc_imm(s, TARGET_LONG_BITS == 32 ? OPC_ADDIW : OPC_ADDI, 1019933b331bSRichard Henderson addr_adj, addr_reg, s_mask - a_mask); 1020933b331bSRichard Henderson } 1021aece72b7SRichard Henderson compare_mask = s->page_mask | a_mask; 1022933b331bSRichard Henderson if (compare_mask == sextreg(compare_mask, 0, 12)) { 1023933b331bSRichard Henderson tcg_out_opc_imm(s, OPC_ANDI, TCG_REG_TMP1, addr_adj, compare_mask); 1024933b331bSRichard Henderson } else { 1025933b331bSRichard Henderson tcg_out_movi(s, TCG_TYPE_TL, TCG_REG_TMP1, compare_mask); 1026933b331bSRichard Henderson tcg_out_opc_reg(s, OPC_AND, TCG_REG_TMP1, TCG_REG_TMP1, addr_adj); 1027933b331bSRichard Henderson } 1028933b331bSRichard Henderson 1029001dddfeSRichard Henderson /* Load the tlb comparator and the addend. */ 1030001dddfeSRichard Henderson tcg_out_ld(s, TCG_TYPE_TL, TCG_REG_TMP0, TCG_REG_TMP2, 1031001dddfeSRichard Henderson is_ld ? offsetof(CPUTLBEntry, addr_read) 1032001dddfeSRichard Henderson : offsetof(CPUTLBEntry, addr_write)); 1033001dddfeSRichard Henderson tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP2, TCG_REG_TMP2, 1034001dddfeSRichard Henderson offsetof(CPUTLBEntry, addend)); 1035001dddfeSRichard Henderson 1036001dddfeSRichard Henderson /* Compare masked address with the TLB entry. */ 1037001dddfeSRichard Henderson ldst->label_ptr[0] = s->code_ptr; 1038001dddfeSRichard Henderson tcg_out_opc_branch(s, OPC_BNE, TCG_REG_TMP0, TCG_REG_TMP1, 0); 1039001dddfeSRichard Henderson 1040001dddfeSRichard Henderson /* TLB Hit - translate address using addend. */ 1041*eda15159SRichard Henderson if (TARGET_LONG_BITS == 64) { 1042*eda15159SRichard Henderson tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP0, addr_reg, TCG_REG_TMP2); 1043*eda15159SRichard Henderson } else if (have_zba) { 1044*eda15159SRichard Henderson tcg_out_opc_reg(s, OPC_ADD_UW, TCG_REG_TMP0, addr_reg, TCG_REG_TMP2); 1045*eda15159SRichard Henderson } else { 1046*eda15159SRichard Henderson tcg_out_ext32u(s, TCG_REG_TMP0, addr_reg); 1047*eda15159SRichard Henderson tcg_out_opc_reg(s, OPC_ADD, TCG_REG_TMP0, TCG_REG_TMP0, TCG_REG_TMP2); 1048001dddfeSRichard Henderson } 1049001dddfeSRichard Henderson *pbase = TCG_REG_TMP0; 1050001dddfeSRichard Henderson#else 1051*eda15159SRichard Henderson TCGReg base; 1052*eda15159SRichard Henderson 1053001dddfeSRichard Henderson if (a_mask) { 1054001dddfeSRichard Henderson ldst = new_ldst_label(s); 1055001dddfeSRichard Henderson ldst->is_ld = is_ld; 1056001dddfeSRichard Henderson ldst->oi = oi; 1057001dddfeSRichard Henderson ldst->addrlo_reg = addr_reg; 1058001dddfeSRichard Henderson 105937e523f0SRichard Henderson /* We are expecting alignment max 7, so we can always use andi. */ 106037e523f0SRichard Henderson tcg_debug_assert(a_mask == sextreg(a_mask, 0, 12)); 1061001dddfeSRichard Henderson tcg_out_opc_imm(s, OPC_ANDI, TCG_REG_TMP1, addr_reg, a_mask); 1062001dddfeSRichard Henderson 1063001dddfeSRichard Henderson ldst->label_ptr[0] = s->code_ptr; 1064001dddfeSRichard Henderson tcg_out_opc_branch(s, OPC_BNE, TCG_REG_TMP1, TCG_REG_ZERO, 0); 1065001dddfeSRichard Henderson } 1066001dddfeSRichard Henderson 1067001dddfeSRichard Henderson if (guest_base != 0) { 1068001dddfeSRichard Henderson base = TCG_REG_TMP0; 1069*eda15159SRichard Henderson if (TARGET_LONG_BITS == 64) { 1070*eda15159SRichard Henderson tcg_out_opc_reg(s, OPC_ADD, base, addr_reg, TCG_GUEST_BASE_REG); 1071*eda15159SRichard Henderson } else if (have_zba) { 1072*eda15159SRichard Henderson tcg_out_opc_reg(s, OPC_ADD_UW, base, addr_reg, TCG_GUEST_BASE_REG); 1073*eda15159SRichard Henderson } else { 1074*eda15159SRichard Henderson tcg_out_ext32u(s, base, addr_reg); 1075*eda15159SRichard Henderson tcg_out_opc_reg(s, OPC_ADD, base, base, TCG_GUEST_BASE_REG); 1076*eda15159SRichard Henderson } 1077*eda15159SRichard Henderson } else if (TARGET_LONG_BITS == 64) { 1078*eda15159SRichard Henderson base = addr_reg; 1079*eda15159SRichard Henderson } else { 1080*eda15159SRichard Henderson base = TCG_REG_TMP0; 1081*eda15159SRichard Henderson tcg_out_ext32u(s, base, addr_reg); 1082001dddfeSRichard Henderson } 1083001dddfeSRichard Henderson *pbase = base; 1084001dddfeSRichard Henderson#endif 1085001dddfeSRichard Henderson 1086001dddfeSRichard Henderson return ldst; 1087001dddfeSRichard Henderson} 1088001dddfeSRichard Henderson 1089aeb6326eSRichard Hendersonstatic void tcg_out_qemu_ld_direct(TCGContext *s, TCGReg val, 1090f7041977SRichard Henderson TCGReg base, MemOp opc, TCGType type) 1091139c1837SPaolo Bonzini{ 1092c86bd2dcSRichard Henderson /* Byte swapping is left to middle-end expansion. */ 1093c86bd2dcSRichard Henderson tcg_debug_assert((opc & MO_BSWAP) == 0); 1094139c1837SPaolo Bonzini 1095139c1837SPaolo Bonzini switch (opc & (MO_SSIZE)) { 1096139c1837SPaolo Bonzini case MO_UB: 1097aeb6326eSRichard Henderson tcg_out_opc_imm(s, OPC_LBU, val, base, 0); 1098139c1837SPaolo Bonzini break; 1099139c1837SPaolo Bonzini case MO_SB: 1100aeb6326eSRichard Henderson tcg_out_opc_imm(s, OPC_LB, val, base, 0); 1101139c1837SPaolo Bonzini break; 1102139c1837SPaolo Bonzini case MO_UW: 1103aeb6326eSRichard Henderson tcg_out_opc_imm(s, OPC_LHU, val, base, 0); 1104139c1837SPaolo Bonzini break; 1105139c1837SPaolo Bonzini case MO_SW: 1106aeb6326eSRichard Henderson tcg_out_opc_imm(s, OPC_LH, val, base, 0); 1107139c1837SPaolo Bonzini break; 1108139c1837SPaolo Bonzini case MO_UL: 1109f7041977SRichard Henderson if (type == TCG_TYPE_I64) { 1110aeb6326eSRichard Henderson tcg_out_opc_imm(s, OPC_LWU, val, base, 0); 1111139c1837SPaolo Bonzini break; 1112139c1837SPaolo Bonzini } 1113139c1837SPaolo Bonzini /* FALLTHRU */ 1114139c1837SPaolo Bonzini case MO_SL: 1115aeb6326eSRichard Henderson tcg_out_opc_imm(s, OPC_LW, val, base, 0); 1116139c1837SPaolo Bonzini break; 1117fc313c64SFrédéric Pétrot case MO_UQ: 1118aeb6326eSRichard Henderson tcg_out_opc_imm(s, OPC_LD, val, base, 0); 1119139c1837SPaolo Bonzini break; 1120139c1837SPaolo Bonzini default: 1121139c1837SPaolo Bonzini g_assert_not_reached(); 1122139c1837SPaolo Bonzini } 1123139c1837SPaolo Bonzini} 1124139c1837SPaolo Bonzini 1125f7041977SRichard Hendersonstatic void tcg_out_qemu_ld(TCGContext *s, TCGReg data_reg, TCGReg addr_reg, 1126f7041977SRichard Henderson MemOpIdx oi, TCGType data_type) 1127139c1837SPaolo Bonzini{ 1128001dddfeSRichard Henderson TCGLabelQemuLdst *ldst; 11292e3a933aSRichard Henderson TCGReg base; 1130139c1837SPaolo Bonzini 1131001dddfeSRichard Henderson ldst = prepare_host_addr(s, &base, addr_reg, oi, true); 1132001dddfeSRichard Henderson tcg_out_qemu_ld_direct(s, data_reg, base, get_memop(oi), data_type); 1133f7041977SRichard Henderson 1134001dddfeSRichard Henderson if (ldst) { 1135001dddfeSRichard Henderson ldst->type = data_type; 1136001dddfeSRichard Henderson ldst->datalo_reg = data_reg; 1137001dddfeSRichard Henderson ldst->raddr = tcg_splitwx_to_rx(s->code_ptr); 1138a3fb7c99SRichard Henderson } 1139139c1837SPaolo Bonzini} 1140139c1837SPaolo Bonzini 1141aeb6326eSRichard Hendersonstatic void tcg_out_qemu_st_direct(TCGContext *s, TCGReg val, 1142139c1837SPaolo Bonzini TCGReg base, MemOp opc) 1143139c1837SPaolo Bonzini{ 1144c86bd2dcSRichard Henderson /* Byte swapping is left to middle-end expansion. */ 1145c86bd2dcSRichard Henderson tcg_debug_assert((opc & MO_BSWAP) == 0); 1146139c1837SPaolo Bonzini 1147139c1837SPaolo Bonzini switch (opc & (MO_SSIZE)) { 1148139c1837SPaolo Bonzini case MO_8: 1149aeb6326eSRichard Henderson tcg_out_opc_store(s, OPC_SB, base, val, 0); 1150139c1837SPaolo Bonzini break; 1151139c1837SPaolo Bonzini case MO_16: 1152aeb6326eSRichard Henderson tcg_out_opc_store(s, OPC_SH, base, val, 0); 1153139c1837SPaolo Bonzini break; 1154139c1837SPaolo Bonzini case MO_32: 1155aeb6326eSRichard Henderson tcg_out_opc_store(s, OPC_SW, base, val, 0); 1156139c1837SPaolo Bonzini break; 1157139c1837SPaolo Bonzini case MO_64: 1158aeb6326eSRichard Henderson tcg_out_opc_store(s, OPC_SD, base, val, 0); 1159139c1837SPaolo Bonzini break; 1160139c1837SPaolo Bonzini default: 1161139c1837SPaolo Bonzini g_assert_not_reached(); 1162139c1837SPaolo Bonzini } 1163139c1837SPaolo Bonzini} 1164139c1837SPaolo Bonzini 1165f7041977SRichard Hendersonstatic void tcg_out_qemu_st(TCGContext *s, TCGReg data_reg, TCGReg addr_reg, 1166f7041977SRichard Henderson MemOpIdx oi, TCGType data_type) 1167139c1837SPaolo Bonzini{ 1168001dddfeSRichard Henderson TCGLabelQemuLdst *ldst; 11692e3a933aSRichard Henderson TCGReg base; 1170139c1837SPaolo Bonzini 1171001dddfeSRichard Henderson ldst = prepare_host_addr(s, &base, addr_reg, oi, false); 1172001dddfeSRichard Henderson tcg_out_qemu_st_direct(s, data_reg, base, get_memop(oi)); 1173f7041977SRichard Henderson 1174001dddfeSRichard Henderson if (ldst) { 1175001dddfeSRichard Henderson ldst->type = data_type; 1176001dddfeSRichard Henderson ldst->datalo_reg = data_reg; 1177001dddfeSRichard Henderson ldst->raddr = tcg_splitwx_to_rx(s->code_ptr); 1178a3fb7c99SRichard Henderson } 1179139c1837SPaolo Bonzini} 1180139c1837SPaolo Bonzini 1181793f7381SRichard Hendersonstatic const tcg_insn_unit *tb_ret_addr; 1182139c1837SPaolo Bonzini 1183b55a8d9dSRichard Hendersonstatic void tcg_out_exit_tb(TCGContext *s, uintptr_t a0) 1184b55a8d9dSRichard Henderson{ 1185b55a8d9dSRichard Henderson /* Reuse the zeroing that exists for goto_ptr. */ 1186b55a8d9dSRichard Henderson if (a0 == 0) { 1187b55a8d9dSRichard Henderson tcg_out_call_int(s, tcg_code_gen_epilogue, true); 1188b55a8d9dSRichard Henderson } else { 1189b55a8d9dSRichard Henderson tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_A0, a0); 1190b55a8d9dSRichard Henderson tcg_out_call_int(s, tb_ret_addr, true); 1191b55a8d9dSRichard Henderson } 1192b55a8d9dSRichard Henderson} 1193b55a8d9dSRichard Henderson 1194cf7d6b8eSRichard Hendersonstatic void tcg_out_goto_tb(TCGContext *s, int which) 1195cf7d6b8eSRichard Henderson{ 1196493c9b19SRichard Henderson /* Direct branch will be patched by tb_target_set_jmp_target. */ 1197493c9b19SRichard Henderson set_jmp_insn_offset(s, which); 1198493c9b19SRichard Henderson tcg_out32(s, OPC_JAL); 1199493c9b19SRichard Henderson 1200493c9b19SRichard Henderson /* When branch is out of range, fall through to indirect. */ 1201cf7d6b8eSRichard Henderson tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP0, TCG_REG_ZERO, 1202cf7d6b8eSRichard Henderson get_jmp_target_addr(s, which)); 1203cf7d6b8eSRichard Henderson tcg_out_opc_imm(s, OPC_JALR, TCG_REG_ZERO, TCG_REG_TMP0, 0); 1204cf7d6b8eSRichard Henderson set_jmp_reset_offset(s, which); 1205cf7d6b8eSRichard Henderson} 1206cf7d6b8eSRichard Henderson 120790c0fee3SRichard Hendersonvoid tb_target_set_jmp_target(const TranslationBlock *tb, int n, 120890c0fee3SRichard Henderson uintptr_t jmp_rx, uintptr_t jmp_rw) 120990c0fee3SRichard Henderson{ 1210493c9b19SRichard Henderson uintptr_t addr = tb->jmp_target_addr[n]; 1211493c9b19SRichard Henderson ptrdiff_t offset = addr - jmp_rx; 1212493c9b19SRichard Henderson tcg_insn_unit insn; 1213493c9b19SRichard Henderson 1214493c9b19SRichard Henderson /* Either directly branch, or fall through to indirect branch. */ 1215493c9b19SRichard Henderson if (offset == sextreg(offset, 0, 20)) { 1216493c9b19SRichard Henderson insn = encode_uj(OPC_JAL, TCG_REG_ZERO, offset); 1217493c9b19SRichard Henderson } else { 1218493c9b19SRichard Henderson insn = OPC_NOP; 1219493c9b19SRichard Henderson } 1220493c9b19SRichard Henderson qatomic_set((uint32_t *)jmp_rw, insn); 1221493c9b19SRichard Henderson flush_idcache_range(jmp_rx, jmp_rw, 4); 122290c0fee3SRichard Henderson} 122390c0fee3SRichard Henderson 1224139c1837SPaolo Bonzinistatic void tcg_out_op(TCGContext *s, TCGOpcode opc, 12255e8892dbSMiroslav Rezanina const TCGArg args[TCG_MAX_OP_ARGS], 12265e8892dbSMiroslav Rezanina const int const_args[TCG_MAX_OP_ARGS]) 1227139c1837SPaolo Bonzini{ 1228139c1837SPaolo Bonzini TCGArg a0 = args[0]; 1229139c1837SPaolo Bonzini TCGArg a1 = args[1]; 1230139c1837SPaolo Bonzini TCGArg a2 = args[2]; 1231139c1837SPaolo Bonzini int c2 = const_args[2]; 1232139c1837SPaolo Bonzini 1233139c1837SPaolo Bonzini switch (opc) { 1234139c1837SPaolo Bonzini case INDEX_op_goto_ptr: 1235139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_JALR, TCG_REG_ZERO, a0, 0); 1236139c1837SPaolo Bonzini break; 1237139c1837SPaolo Bonzini 1238139c1837SPaolo Bonzini case INDEX_op_br: 1239139c1837SPaolo Bonzini tcg_out_reloc(s, s->code_ptr, R_RISCV_JAL, arg_label(a0), 0); 1240139c1837SPaolo Bonzini tcg_out_opc_jump(s, OPC_JAL, TCG_REG_ZERO, 0); 1241139c1837SPaolo Bonzini break; 1242139c1837SPaolo Bonzini 1243139c1837SPaolo Bonzini case INDEX_op_ld8u_i32: 1244139c1837SPaolo Bonzini case INDEX_op_ld8u_i64: 1245139c1837SPaolo Bonzini tcg_out_ldst(s, OPC_LBU, a0, a1, a2); 1246139c1837SPaolo Bonzini break; 1247139c1837SPaolo Bonzini case INDEX_op_ld8s_i32: 1248139c1837SPaolo Bonzini case INDEX_op_ld8s_i64: 1249139c1837SPaolo Bonzini tcg_out_ldst(s, OPC_LB, a0, a1, a2); 1250139c1837SPaolo Bonzini break; 1251139c1837SPaolo Bonzini case INDEX_op_ld16u_i32: 1252139c1837SPaolo Bonzini case INDEX_op_ld16u_i64: 1253139c1837SPaolo Bonzini tcg_out_ldst(s, OPC_LHU, a0, a1, a2); 1254139c1837SPaolo Bonzini break; 1255139c1837SPaolo Bonzini case INDEX_op_ld16s_i32: 1256139c1837SPaolo Bonzini case INDEX_op_ld16s_i64: 1257139c1837SPaolo Bonzini tcg_out_ldst(s, OPC_LH, a0, a1, a2); 1258139c1837SPaolo Bonzini break; 1259139c1837SPaolo Bonzini case INDEX_op_ld32u_i64: 1260139c1837SPaolo Bonzini tcg_out_ldst(s, OPC_LWU, a0, a1, a2); 1261139c1837SPaolo Bonzini break; 1262139c1837SPaolo Bonzini case INDEX_op_ld_i32: 1263139c1837SPaolo Bonzini case INDEX_op_ld32s_i64: 1264139c1837SPaolo Bonzini tcg_out_ldst(s, OPC_LW, a0, a1, a2); 1265139c1837SPaolo Bonzini break; 1266139c1837SPaolo Bonzini case INDEX_op_ld_i64: 1267139c1837SPaolo Bonzini tcg_out_ldst(s, OPC_LD, a0, a1, a2); 1268139c1837SPaolo Bonzini break; 1269139c1837SPaolo Bonzini 1270139c1837SPaolo Bonzini case INDEX_op_st8_i32: 1271139c1837SPaolo Bonzini case INDEX_op_st8_i64: 1272139c1837SPaolo Bonzini tcg_out_ldst(s, OPC_SB, a0, a1, a2); 1273139c1837SPaolo Bonzini break; 1274139c1837SPaolo Bonzini case INDEX_op_st16_i32: 1275139c1837SPaolo Bonzini case INDEX_op_st16_i64: 1276139c1837SPaolo Bonzini tcg_out_ldst(s, OPC_SH, a0, a1, a2); 1277139c1837SPaolo Bonzini break; 1278139c1837SPaolo Bonzini case INDEX_op_st_i32: 1279139c1837SPaolo Bonzini case INDEX_op_st32_i64: 1280139c1837SPaolo Bonzini tcg_out_ldst(s, OPC_SW, a0, a1, a2); 1281139c1837SPaolo Bonzini break; 1282139c1837SPaolo Bonzini case INDEX_op_st_i64: 1283139c1837SPaolo Bonzini tcg_out_ldst(s, OPC_SD, a0, a1, a2); 1284139c1837SPaolo Bonzini break; 1285139c1837SPaolo Bonzini 1286139c1837SPaolo Bonzini case INDEX_op_add_i32: 1287139c1837SPaolo Bonzini if (c2) { 1288139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_ADDIW, a0, a1, a2); 1289139c1837SPaolo Bonzini } else { 1290139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_ADDW, a0, a1, a2); 1291139c1837SPaolo Bonzini } 1292139c1837SPaolo Bonzini break; 1293139c1837SPaolo Bonzini case INDEX_op_add_i64: 1294139c1837SPaolo Bonzini if (c2) { 1295139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_ADDI, a0, a1, a2); 1296139c1837SPaolo Bonzini } else { 1297139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_ADD, a0, a1, a2); 1298139c1837SPaolo Bonzini } 1299139c1837SPaolo Bonzini break; 1300139c1837SPaolo Bonzini 1301139c1837SPaolo Bonzini case INDEX_op_sub_i32: 1302139c1837SPaolo Bonzini if (c2) { 1303139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_ADDIW, a0, a1, -a2); 1304139c1837SPaolo Bonzini } else { 1305139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_SUBW, a0, a1, a2); 1306139c1837SPaolo Bonzini } 1307139c1837SPaolo Bonzini break; 1308139c1837SPaolo Bonzini case INDEX_op_sub_i64: 1309139c1837SPaolo Bonzini if (c2) { 1310139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_ADDI, a0, a1, -a2); 1311139c1837SPaolo Bonzini } else { 1312139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_SUB, a0, a1, a2); 1313139c1837SPaolo Bonzini } 1314139c1837SPaolo Bonzini break; 1315139c1837SPaolo Bonzini 1316139c1837SPaolo Bonzini case INDEX_op_and_i32: 1317139c1837SPaolo Bonzini case INDEX_op_and_i64: 1318139c1837SPaolo Bonzini if (c2) { 1319139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_ANDI, a0, a1, a2); 1320139c1837SPaolo Bonzini } else { 1321139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_AND, a0, a1, a2); 1322139c1837SPaolo Bonzini } 1323139c1837SPaolo Bonzini break; 1324139c1837SPaolo Bonzini 1325139c1837SPaolo Bonzini case INDEX_op_or_i32: 1326139c1837SPaolo Bonzini case INDEX_op_or_i64: 1327139c1837SPaolo Bonzini if (c2) { 1328139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_ORI, a0, a1, a2); 1329139c1837SPaolo Bonzini } else { 1330139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_OR, a0, a1, a2); 1331139c1837SPaolo Bonzini } 1332139c1837SPaolo Bonzini break; 1333139c1837SPaolo Bonzini 1334139c1837SPaolo Bonzini case INDEX_op_xor_i32: 1335139c1837SPaolo Bonzini case INDEX_op_xor_i64: 1336139c1837SPaolo Bonzini if (c2) { 1337139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_XORI, a0, a1, a2); 1338139c1837SPaolo Bonzini } else { 1339139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_XOR, a0, a1, a2); 1340139c1837SPaolo Bonzini } 1341139c1837SPaolo Bonzini break; 1342139c1837SPaolo Bonzini 134399f4ec6eSRichard Henderson case INDEX_op_andc_i32: 134499f4ec6eSRichard Henderson case INDEX_op_andc_i64: 134599f4ec6eSRichard Henderson if (c2) { 134699f4ec6eSRichard Henderson tcg_out_opc_imm(s, OPC_ANDI, a0, a1, ~a2); 134799f4ec6eSRichard Henderson } else { 134899f4ec6eSRichard Henderson tcg_out_opc_reg(s, OPC_ANDN, a0, a1, a2); 134999f4ec6eSRichard Henderson } 135099f4ec6eSRichard Henderson break; 135199f4ec6eSRichard Henderson case INDEX_op_orc_i32: 135299f4ec6eSRichard Henderson case INDEX_op_orc_i64: 135399f4ec6eSRichard Henderson if (c2) { 135499f4ec6eSRichard Henderson tcg_out_opc_imm(s, OPC_ORI, a0, a1, ~a2); 135599f4ec6eSRichard Henderson } else { 135699f4ec6eSRichard Henderson tcg_out_opc_reg(s, OPC_ORN, a0, a1, a2); 135799f4ec6eSRichard Henderson } 135899f4ec6eSRichard Henderson break; 135999f4ec6eSRichard Henderson case INDEX_op_eqv_i32: 136099f4ec6eSRichard Henderson case INDEX_op_eqv_i64: 136199f4ec6eSRichard Henderson if (c2) { 136299f4ec6eSRichard Henderson tcg_out_opc_imm(s, OPC_XORI, a0, a1, ~a2); 136399f4ec6eSRichard Henderson } else { 136499f4ec6eSRichard Henderson tcg_out_opc_reg(s, OPC_XNOR, a0, a1, a2); 136599f4ec6eSRichard Henderson } 136699f4ec6eSRichard Henderson break; 136799f4ec6eSRichard Henderson 1368139c1837SPaolo Bonzini case INDEX_op_not_i32: 1369139c1837SPaolo Bonzini case INDEX_op_not_i64: 1370139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_XORI, a0, a1, -1); 1371139c1837SPaolo Bonzini break; 1372139c1837SPaolo Bonzini 1373139c1837SPaolo Bonzini case INDEX_op_neg_i32: 1374139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_SUBW, a0, TCG_REG_ZERO, a1); 1375139c1837SPaolo Bonzini break; 1376139c1837SPaolo Bonzini case INDEX_op_neg_i64: 1377139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_SUB, a0, TCG_REG_ZERO, a1); 1378139c1837SPaolo Bonzini break; 1379139c1837SPaolo Bonzini 1380139c1837SPaolo Bonzini case INDEX_op_mul_i32: 1381139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_MULW, a0, a1, a2); 1382139c1837SPaolo Bonzini break; 1383139c1837SPaolo Bonzini case INDEX_op_mul_i64: 1384139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_MUL, a0, a1, a2); 1385139c1837SPaolo Bonzini break; 1386139c1837SPaolo Bonzini 1387139c1837SPaolo Bonzini case INDEX_op_div_i32: 1388139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_DIVW, a0, a1, a2); 1389139c1837SPaolo Bonzini break; 1390139c1837SPaolo Bonzini case INDEX_op_div_i64: 1391139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_DIV, a0, a1, a2); 1392139c1837SPaolo Bonzini break; 1393139c1837SPaolo Bonzini 1394139c1837SPaolo Bonzini case INDEX_op_divu_i32: 1395139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_DIVUW, a0, a1, a2); 1396139c1837SPaolo Bonzini break; 1397139c1837SPaolo Bonzini case INDEX_op_divu_i64: 1398139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_DIVU, a0, a1, a2); 1399139c1837SPaolo Bonzini break; 1400139c1837SPaolo Bonzini 1401139c1837SPaolo Bonzini case INDEX_op_rem_i32: 1402139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_REMW, a0, a1, a2); 1403139c1837SPaolo Bonzini break; 1404139c1837SPaolo Bonzini case INDEX_op_rem_i64: 1405139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_REM, a0, a1, a2); 1406139c1837SPaolo Bonzini break; 1407139c1837SPaolo Bonzini 1408139c1837SPaolo Bonzini case INDEX_op_remu_i32: 1409139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_REMUW, a0, a1, a2); 1410139c1837SPaolo Bonzini break; 1411139c1837SPaolo Bonzini case INDEX_op_remu_i64: 1412139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_REMU, a0, a1, a2); 1413139c1837SPaolo Bonzini break; 1414139c1837SPaolo Bonzini 1415139c1837SPaolo Bonzini case INDEX_op_shl_i32: 1416139c1837SPaolo Bonzini if (c2) { 1417d2f3066eSZihao Yu tcg_out_opc_imm(s, OPC_SLLIW, a0, a1, a2 & 0x1f); 1418139c1837SPaolo Bonzini } else { 1419139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_SLLW, a0, a1, a2); 1420139c1837SPaolo Bonzini } 1421139c1837SPaolo Bonzini break; 1422139c1837SPaolo Bonzini case INDEX_op_shl_i64: 1423139c1837SPaolo Bonzini if (c2) { 1424d2f3066eSZihao Yu tcg_out_opc_imm(s, OPC_SLLI, a0, a1, a2 & 0x3f); 1425139c1837SPaolo Bonzini } else { 1426139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_SLL, a0, a1, a2); 1427139c1837SPaolo Bonzini } 1428139c1837SPaolo Bonzini break; 1429139c1837SPaolo Bonzini 1430139c1837SPaolo Bonzini case INDEX_op_shr_i32: 1431139c1837SPaolo Bonzini if (c2) { 1432d2f3066eSZihao Yu tcg_out_opc_imm(s, OPC_SRLIW, a0, a1, a2 & 0x1f); 1433139c1837SPaolo Bonzini } else { 1434139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_SRLW, a0, a1, a2); 1435139c1837SPaolo Bonzini } 1436139c1837SPaolo Bonzini break; 1437139c1837SPaolo Bonzini case INDEX_op_shr_i64: 1438139c1837SPaolo Bonzini if (c2) { 1439d2f3066eSZihao Yu tcg_out_opc_imm(s, OPC_SRLI, a0, a1, a2 & 0x3f); 1440139c1837SPaolo Bonzini } else { 1441139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_SRL, a0, a1, a2); 1442139c1837SPaolo Bonzini } 1443139c1837SPaolo Bonzini break; 1444139c1837SPaolo Bonzini 1445139c1837SPaolo Bonzini case INDEX_op_sar_i32: 1446139c1837SPaolo Bonzini if (c2) { 1447d2f3066eSZihao Yu tcg_out_opc_imm(s, OPC_SRAIW, a0, a1, a2 & 0x1f); 1448139c1837SPaolo Bonzini } else { 1449139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_SRAW, a0, a1, a2); 1450139c1837SPaolo Bonzini } 1451139c1837SPaolo Bonzini break; 1452139c1837SPaolo Bonzini case INDEX_op_sar_i64: 1453139c1837SPaolo Bonzini if (c2) { 1454d2f3066eSZihao Yu tcg_out_opc_imm(s, OPC_SRAI, a0, a1, a2 & 0x3f); 1455139c1837SPaolo Bonzini } else { 1456139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_SRA, a0, a1, a2); 1457139c1837SPaolo Bonzini } 1458139c1837SPaolo Bonzini break; 1459139c1837SPaolo Bonzini 1460139c1837SPaolo Bonzini case INDEX_op_add2_i32: 1461139c1837SPaolo Bonzini tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5], 1462139c1837SPaolo Bonzini const_args[4], const_args[5], false, true); 1463139c1837SPaolo Bonzini break; 1464139c1837SPaolo Bonzini case INDEX_op_add2_i64: 1465139c1837SPaolo Bonzini tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5], 1466139c1837SPaolo Bonzini const_args[4], const_args[5], false, false); 1467139c1837SPaolo Bonzini break; 1468139c1837SPaolo Bonzini case INDEX_op_sub2_i32: 1469139c1837SPaolo Bonzini tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5], 1470139c1837SPaolo Bonzini const_args[4], const_args[5], true, true); 1471139c1837SPaolo Bonzini break; 1472139c1837SPaolo Bonzini case INDEX_op_sub2_i64: 1473139c1837SPaolo Bonzini tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5], 1474139c1837SPaolo Bonzini const_args[4], const_args[5], true, false); 1475139c1837SPaolo Bonzini break; 1476139c1837SPaolo Bonzini 1477139c1837SPaolo Bonzini case INDEX_op_brcond_i32: 1478139c1837SPaolo Bonzini case INDEX_op_brcond_i64: 1479139c1837SPaolo Bonzini tcg_out_brcond(s, a2, a0, a1, arg_label(args[3])); 1480139c1837SPaolo Bonzini break; 1481139c1837SPaolo Bonzini 1482139c1837SPaolo Bonzini case INDEX_op_setcond_i32: 1483139c1837SPaolo Bonzini case INDEX_op_setcond_i64: 1484139c1837SPaolo Bonzini tcg_out_setcond(s, args[3], a0, a1, a2); 1485139c1837SPaolo Bonzini break; 1486139c1837SPaolo Bonzini 1487fecccfccSRichard Henderson case INDEX_op_qemu_ld_a32_i32: 1488fecccfccSRichard Henderson case INDEX_op_qemu_ld_a64_i32: 1489f7041977SRichard Henderson tcg_out_qemu_ld(s, a0, a1, a2, TCG_TYPE_I32); 1490139c1837SPaolo Bonzini break; 1491fecccfccSRichard Henderson case INDEX_op_qemu_ld_a32_i64: 1492fecccfccSRichard Henderson case INDEX_op_qemu_ld_a64_i64: 1493f7041977SRichard Henderson tcg_out_qemu_ld(s, a0, a1, a2, TCG_TYPE_I64); 1494139c1837SPaolo Bonzini break; 1495fecccfccSRichard Henderson case INDEX_op_qemu_st_a32_i32: 1496fecccfccSRichard Henderson case INDEX_op_qemu_st_a64_i32: 1497f7041977SRichard Henderson tcg_out_qemu_st(s, a0, a1, a2, TCG_TYPE_I32); 1498139c1837SPaolo Bonzini break; 1499fecccfccSRichard Henderson case INDEX_op_qemu_st_a32_i64: 1500fecccfccSRichard Henderson case INDEX_op_qemu_st_a64_i64: 1501f7041977SRichard Henderson tcg_out_qemu_st(s, a0, a1, a2, TCG_TYPE_I64); 1502139c1837SPaolo Bonzini break; 1503139c1837SPaolo Bonzini 1504139c1837SPaolo Bonzini case INDEX_op_extrh_i64_i32: 1505139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_SRAI, a0, a1, 32); 1506139c1837SPaolo Bonzini break; 1507139c1837SPaolo Bonzini 1508139c1837SPaolo Bonzini case INDEX_op_mulsh_i32: 1509139c1837SPaolo Bonzini case INDEX_op_mulsh_i64: 1510139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_MULH, a0, a1, a2); 1511139c1837SPaolo Bonzini break; 1512139c1837SPaolo Bonzini 1513139c1837SPaolo Bonzini case INDEX_op_muluh_i32: 1514139c1837SPaolo Bonzini case INDEX_op_muluh_i64: 1515139c1837SPaolo Bonzini tcg_out_opc_reg(s, OPC_MULHU, a0, a1, a2); 1516139c1837SPaolo Bonzini break; 1517139c1837SPaolo Bonzini 1518139c1837SPaolo Bonzini case INDEX_op_mb: 1519139c1837SPaolo Bonzini tcg_out_mb(s, a0); 1520139c1837SPaolo Bonzini break; 1521139c1837SPaolo Bonzini 1522139c1837SPaolo Bonzini case INDEX_op_mov_i32: /* Always emitted via tcg_out_mov. */ 1523139c1837SPaolo Bonzini case INDEX_op_mov_i64: 1524139c1837SPaolo Bonzini case INDEX_op_call: /* Always emitted via tcg_out_call. */ 1525b55a8d9dSRichard Henderson case INDEX_op_exit_tb: /* Always emitted via tcg_out_exit_tb. */ 1526cf7d6b8eSRichard Henderson case INDEX_op_goto_tb: /* Always emitted via tcg_out_goto_tb. */ 1527678155b2SRichard Henderson case INDEX_op_ext8s_i32: /* Always emitted via tcg_reg_alloc_op. */ 1528678155b2SRichard Henderson case INDEX_op_ext8s_i64: 1529d0e66c89SRichard Henderson case INDEX_op_ext8u_i32: 1530d0e66c89SRichard Henderson case INDEX_op_ext8u_i64: 1531753e42eaSRichard Henderson case INDEX_op_ext16s_i32: 1532753e42eaSRichard Henderson case INDEX_op_ext16s_i64: 1533379afdffSRichard Henderson case INDEX_op_ext16u_i32: 1534379afdffSRichard Henderson case INDEX_op_ext16u_i64: 153552bf3398SRichard Henderson case INDEX_op_ext32s_i64: 15369ecf5f61SRichard Henderson case INDEX_op_ext32u_i64: 15379c6aa274SRichard Henderson case INDEX_op_ext_i32_i64: 1538b9bfe000SRichard Henderson case INDEX_op_extu_i32_i64: 1539b8b94ac6SRichard Henderson case INDEX_op_extrl_i64_i32: 1540139c1837SPaolo Bonzini default: 1541139c1837SPaolo Bonzini g_assert_not_reached(); 1542139c1837SPaolo Bonzini } 1543139c1837SPaolo Bonzini} 1544139c1837SPaolo Bonzini 1545665be288SRichard Hendersonstatic TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op) 1546139c1837SPaolo Bonzini{ 1547139c1837SPaolo Bonzini switch (op) { 1548139c1837SPaolo Bonzini case INDEX_op_goto_ptr: 1549665be288SRichard Henderson return C_O0_I1(r); 1550139c1837SPaolo Bonzini 1551139c1837SPaolo Bonzini case INDEX_op_ld8u_i32: 1552139c1837SPaolo Bonzini case INDEX_op_ld8s_i32: 1553139c1837SPaolo Bonzini case INDEX_op_ld16u_i32: 1554139c1837SPaolo Bonzini case INDEX_op_ld16s_i32: 1555139c1837SPaolo Bonzini case INDEX_op_ld_i32: 1556139c1837SPaolo Bonzini case INDEX_op_not_i32: 1557139c1837SPaolo Bonzini case INDEX_op_neg_i32: 1558139c1837SPaolo Bonzini case INDEX_op_ld8u_i64: 1559139c1837SPaolo Bonzini case INDEX_op_ld8s_i64: 1560139c1837SPaolo Bonzini case INDEX_op_ld16u_i64: 1561139c1837SPaolo Bonzini case INDEX_op_ld16s_i64: 1562139c1837SPaolo Bonzini case INDEX_op_ld32s_i64: 1563139c1837SPaolo Bonzini case INDEX_op_ld32u_i64: 1564139c1837SPaolo Bonzini case INDEX_op_ld_i64: 1565139c1837SPaolo Bonzini case INDEX_op_not_i64: 1566139c1837SPaolo Bonzini case INDEX_op_neg_i64: 1567139c1837SPaolo Bonzini case INDEX_op_ext8u_i32: 1568139c1837SPaolo Bonzini case INDEX_op_ext8u_i64: 1569139c1837SPaolo Bonzini case INDEX_op_ext16u_i32: 1570139c1837SPaolo Bonzini case INDEX_op_ext16u_i64: 1571139c1837SPaolo Bonzini case INDEX_op_ext32u_i64: 1572139c1837SPaolo Bonzini case INDEX_op_extu_i32_i64: 1573139c1837SPaolo Bonzini case INDEX_op_ext8s_i32: 1574139c1837SPaolo Bonzini case INDEX_op_ext8s_i64: 1575139c1837SPaolo Bonzini case INDEX_op_ext16s_i32: 1576139c1837SPaolo Bonzini case INDEX_op_ext16s_i64: 1577139c1837SPaolo Bonzini case INDEX_op_ext32s_i64: 1578139c1837SPaolo Bonzini case INDEX_op_extrl_i64_i32: 1579139c1837SPaolo Bonzini case INDEX_op_extrh_i64_i32: 1580139c1837SPaolo Bonzini case INDEX_op_ext_i32_i64: 1581665be288SRichard Henderson return C_O1_I1(r, r); 1582139c1837SPaolo Bonzini 1583139c1837SPaolo Bonzini case INDEX_op_st8_i32: 1584139c1837SPaolo Bonzini case INDEX_op_st16_i32: 1585139c1837SPaolo Bonzini case INDEX_op_st_i32: 1586139c1837SPaolo Bonzini case INDEX_op_st8_i64: 1587139c1837SPaolo Bonzini case INDEX_op_st16_i64: 1588139c1837SPaolo Bonzini case INDEX_op_st32_i64: 1589139c1837SPaolo Bonzini case INDEX_op_st_i64: 1590665be288SRichard Henderson return C_O0_I2(rZ, r); 1591139c1837SPaolo Bonzini 1592139c1837SPaolo Bonzini case INDEX_op_add_i32: 1593139c1837SPaolo Bonzini case INDEX_op_and_i32: 1594139c1837SPaolo Bonzini case INDEX_op_or_i32: 1595139c1837SPaolo Bonzini case INDEX_op_xor_i32: 1596139c1837SPaolo Bonzini case INDEX_op_add_i64: 1597139c1837SPaolo Bonzini case INDEX_op_and_i64: 1598139c1837SPaolo Bonzini case INDEX_op_or_i64: 1599139c1837SPaolo Bonzini case INDEX_op_xor_i64: 1600665be288SRichard Henderson return C_O1_I2(r, r, rI); 1601139c1837SPaolo Bonzini 160299f4ec6eSRichard Henderson case INDEX_op_andc_i32: 160399f4ec6eSRichard Henderson case INDEX_op_andc_i64: 160499f4ec6eSRichard Henderson case INDEX_op_orc_i32: 160599f4ec6eSRichard Henderson case INDEX_op_orc_i64: 160699f4ec6eSRichard Henderson case INDEX_op_eqv_i32: 160799f4ec6eSRichard Henderson case INDEX_op_eqv_i64: 160899f4ec6eSRichard Henderson return C_O1_I2(r, r, rJ); 160999f4ec6eSRichard Henderson 1610139c1837SPaolo Bonzini case INDEX_op_sub_i32: 1611139c1837SPaolo Bonzini case INDEX_op_sub_i64: 1612665be288SRichard Henderson return C_O1_I2(r, rZ, rN); 1613139c1837SPaolo Bonzini 1614139c1837SPaolo Bonzini case INDEX_op_mul_i32: 1615139c1837SPaolo Bonzini case INDEX_op_mulsh_i32: 1616139c1837SPaolo Bonzini case INDEX_op_muluh_i32: 1617139c1837SPaolo Bonzini case INDEX_op_div_i32: 1618139c1837SPaolo Bonzini case INDEX_op_divu_i32: 1619139c1837SPaolo Bonzini case INDEX_op_rem_i32: 1620139c1837SPaolo Bonzini case INDEX_op_remu_i32: 1621139c1837SPaolo Bonzini case INDEX_op_setcond_i32: 1622139c1837SPaolo Bonzini case INDEX_op_mul_i64: 1623139c1837SPaolo Bonzini case INDEX_op_mulsh_i64: 1624139c1837SPaolo Bonzini case INDEX_op_muluh_i64: 1625139c1837SPaolo Bonzini case INDEX_op_div_i64: 1626139c1837SPaolo Bonzini case INDEX_op_divu_i64: 1627139c1837SPaolo Bonzini case INDEX_op_rem_i64: 1628139c1837SPaolo Bonzini case INDEX_op_remu_i64: 1629139c1837SPaolo Bonzini case INDEX_op_setcond_i64: 1630665be288SRichard Henderson return C_O1_I2(r, rZ, rZ); 1631139c1837SPaolo Bonzini 1632139c1837SPaolo Bonzini case INDEX_op_shl_i32: 1633139c1837SPaolo Bonzini case INDEX_op_shr_i32: 1634139c1837SPaolo Bonzini case INDEX_op_sar_i32: 1635139c1837SPaolo Bonzini case INDEX_op_shl_i64: 1636139c1837SPaolo Bonzini case INDEX_op_shr_i64: 1637139c1837SPaolo Bonzini case INDEX_op_sar_i64: 1638665be288SRichard Henderson return C_O1_I2(r, r, ri); 1639139c1837SPaolo Bonzini 1640139c1837SPaolo Bonzini case INDEX_op_brcond_i32: 1641139c1837SPaolo Bonzini case INDEX_op_brcond_i64: 1642665be288SRichard Henderson return C_O0_I2(rZ, rZ); 1643139c1837SPaolo Bonzini 1644139c1837SPaolo Bonzini case INDEX_op_add2_i32: 1645139c1837SPaolo Bonzini case INDEX_op_add2_i64: 1646139c1837SPaolo Bonzini case INDEX_op_sub2_i32: 1647139c1837SPaolo Bonzini case INDEX_op_sub2_i64: 1648665be288SRichard Henderson return C_O2_I4(r, r, rZ, rZ, rM, rM); 1649139c1837SPaolo Bonzini 1650fecccfccSRichard Henderson case INDEX_op_qemu_ld_a32_i32: 1651fecccfccSRichard Henderson case INDEX_op_qemu_ld_a64_i32: 1652fecccfccSRichard Henderson case INDEX_op_qemu_ld_a32_i64: 1653fecccfccSRichard Henderson case INDEX_op_qemu_ld_a64_i64: 1654f0f43534SRichard Henderson return C_O1_I1(r, r); 1655fecccfccSRichard Henderson case INDEX_op_qemu_st_a32_i32: 1656fecccfccSRichard Henderson case INDEX_op_qemu_st_a64_i32: 1657fecccfccSRichard Henderson case INDEX_op_qemu_st_a32_i64: 1658fecccfccSRichard Henderson case INDEX_op_qemu_st_a64_i64: 1659f0f43534SRichard Henderson return C_O0_I2(rZ, r); 1660139c1837SPaolo Bonzini 1661139c1837SPaolo Bonzini default: 1662665be288SRichard Henderson g_assert_not_reached(); 1663139c1837SPaolo Bonzini } 1664139c1837SPaolo Bonzini} 1665139c1837SPaolo Bonzini 1666139c1837SPaolo Bonzinistatic const int tcg_target_callee_save_regs[] = { 1667139c1837SPaolo Bonzini TCG_REG_S0, /* used for the global env (TCG_AREG0) */ 1668139c1837SPaolo Bonzini TCG_REG_S1, 1669139c1837SPaolo Bonzini TCG_REG_S2, 1670139c1837SPaolo Bonzini TCG_REG_S3, 1671139c1837SPaolo Bonzini TCG_REG_S4, 1672139c1837SPaolo Bonzini TCG_REG_S5, 1673139c1837SPaolo Bonzini TCG_REG_S6, 1674139c1837SPaolo Bonzini TCG_REG_S7, 1675139c1837SPaolo Bonzini TCG_REG_S8, 1676139c1837SPaolo Bonzini TCG_REG_S9, 1677139c1837SPaolo Bonzini TCG_REG_S10, 1678139c1837SPaolo Bonzini TCG_REG_S11, 1679139c1837SPaolo Bonzini TCG_REG_RA, /* should be last for ABI compliance */ 1680139c1837SPaolo Bonzini}; 1681139c1837SPaolo Bonzini 1682139c1837SPaolo Bonzini/* Stack frame parameters. */ 1683139c1837SPaolo Bonzini#define REG_SIZE (TCG_TARGET_REG_BITS / 8) 1684139c1837SPaolo Bonzini#define SAVE_SIZE ((int)ARRAY_SIZE(tcg_target_callee_save_regs) * REG_SIZE) 1685139c1837SPaolo Bonzini#define TEMP_SIZE (CPU_TEMP_BUF_NLONGS * (int)sizeof(long)) 1686139c1837SPaolo Bonzini#define FRAME_SIZE ((TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE + SAVE_SIZE \ 1687139c1837SPaolo Bonzini + TCG_TARGET_STACK_ALIGN - 1) \ 1688139c1837SPaolo Bonzini & -TCG_TARGET_STACK_ALIGN) 1689139c1837SPaolo Bonzini#define SAVE_OFS (TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE) 1690139c1837SPaolo Bonzini 1691139c1837SPaolo Bonzini/* We're expecting to be able to use an immediate for frame allocation. */ 1692139c1837SPaolo BonziniQEMU_BUILD_BUG_ON(FRAME_SIZE > 0x7ff); 1693139c1837SPaolo Bonzini 1694139c1837SPaolo Bonzini/* Generate global QEMU prologue and epilogue code */ 1695139c1837SPaolo Bonzinistatic void tcg_target_qemu_prologue(TCGContext *s) 1696139c1837SPaolo Bonzini{ 1697139c1837SPaolo Bonzini int i; 1698139c1837SPaolo Bonzini 1699139c1837SPaolo Bonzini tcg_set_frame(s, TCG_REG_SP, TCG_STATIC_CALL_ARGS_SIZE, TEMP_SIZE); 1700139c1837SPaolo Bonzini 1701139c1837SPaolo Bonzini /* TB prologue */ 1702139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_ADDI, TCG_REG_SP, TCG_REG_SP, -FRAME_SIZE); 1703139c1837SPaolo Bonzini for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) { 1704139c1837SPaolo Bonzini tcg_out_st(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i], 1705139c1837SPaolo Bonzini TCG_REG_SP, SAVE_OFS + i * REG_SIZE); 1706139c1837SPaolo Bonzini } 1707139c1837SPaolo Bonzini 1708139c1837SPaolo Bonzini#if !defined(CONFIG_SOFTMMU) 1709139c1837SPaolo Bonzini tcg_out_movi(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG, guest_base); 1710139c1837SPaolo Bonzini tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG); 1711139c1837SPaolo Bonzini#endif 1712139c1837SPaolo Bonzini 1713139c1837SPaolo Bonzini /* Call generated code */ 1714139c1837SPaolo Bonzini tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]); 1715139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_JALR, TCG_REG_ZERO, tcg_target_call_iarg_regs[1], 0); 1716139c1837SPaolo Bonzini 1717139c1837SPaolo Bonzini /* Return path for goto_ptr. Set return value to 0 */ 1718c8bc1168SRichard Henderson tcg_code_gen_epilogue = tcg_splitwx_to_rx(s->code_ptr); 1719139c1837SPaolo Bonzini tcg_out_mov(s, TCG_TYPE_REG, TCG_REG_A0, TCG_REG_ZERO); 1720139c1837SPaolo Bonzini 1721139c1837SPaolo Bonzini /* TB epilogue */ 1722793f7381SRichard Henderson tb_ret_addr = tcg_splitwx_to_rx(s->code_ptr); 1723139c1837SPaolo Bonzini for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) { 1724139c1837SPaolo Bonzini tcg_out_ld(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i], 1725139c1837SPaolo Bonzini TCG_REG_SP, SAVE_OFS + i * REG_SIZE); 1726139c1837SPaolo Bonzini } 1727139c1837SPaolo Bonzini 1728139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_ADDI, TCG_REG_SP, TCG_REG_SP, FRAME_SIZE); 1729139c1837SPaolo Bonzini tcg_out_opc_imm(s, OPC_JALR, TCG_REG_ZERO, TCG_REG_RA, 0); 1730139c1837SPaolo Bonzini} 1731139c1837SPaolo Bonzini 17329e3e0bc6SRichard Hendersonstatic volatile sig_atomic_t got_sigill; 17339e3e0bc6SRichard Henderson 17349e3e0bc6SRichard Hendersonstatic void sigill_handler(int signo, siginfo_t *si, void *data) 17359e3e0bc6SRichard Henderson{ 17369e3e0bc6SRichard Henderson /* Skip the faulty instruction */ 17379e3e0bc6SRichard Henderson ucontext_t *uc = (ucontext_t *)data; 17389e3e0bc6SRichard Henderson uc->uc_mcontext.__gregs[REG_PC] += 4; 17399e3e0bc6SRichard Henderson 17409e3e0bc6SRichard Henderson got_sigill = 1; 17419e3e0bc6SRichard Henderson} 17429e3e0bc6SRichard Henderson 17439e3e0bc6SRichard Hendersonstatic void tcg_target_detect_isa(void) 17449e3e0bc6SRichard Henderson{ 17459e3e0bc6SRichard Henderson#if !defined(have_zba) || !defined(have_zbb) || !defined(have_zicond) 17469e3e0bc6SRichard Henderson /* 17479e3e0bc6SRichard Henderson * TODO: It is expected that this will be determinable via 17489e3e0bc6SRichard Henderson * linux riscv_hwprobe syscall, not yet merged. 17499e3e0bc6SRichard Henderson * In the meantime, test via sigill. 17509e3e0bc6SRichard Henderson */ 17519e3e0bc6SRichard Henderson 17529e3e0bc6SRichard Henderson struct sigaction sa_old, sa_new; 17539e3e0bc6SRichard Henderson 17549e3e0bc6SRichard Henderson memset(&sa_new, 0, sizeof(sa_new)); 17559e3e0bc6SRichard Henderson sa_new.sa_flags = SA_SIGINFO; 17569e3e0bc6SRichard Henderson sa_new.sa_sigaction = sigill_handler; 17579e3e0bc6SRichard Henderson sigaction(SIGILL, &sa_new, &sa_old); 17589e3e0bc6SRichard Henderson 17599e3e0bc6SRichard Henderson#ifndef have_zba 17609e3e0bc6SRichard Henderson /* Probe for Zba: add.uw zero,zero,zero. */ 17619e3e0bc6SRichard Henderson got_sigill = 0; 17629e3e0bc6SRichard Henderson asm volatile(".insn r 0x3b, 0, 0x04, zero, zero, zero" : : : "memory"); 17639e3e0bc6SRichard Henderson have_zba = !got_sigill; 17649e3e0bc6SRichard Henderson#endif 17659e3e0bc6SRichard Henderson 17669e3e0bc6SRichard Henderson#ifndef have_zbb 17679e3e0bc6SRichard Henderson /* Probe for Zba: andn zero,zero,zero. */ 17689e3e0bc6SRichard Henderson got_sigill = 0; 17699e3e0bc6SRichard Henderson asm volatile(".insn r 0x33, 7, 0x20, zero, zero, zero" : : : "memory"); 17709e3e0bc6SRichard Henderson have_zbb = !got_sigill; 17719e3e0bc6SRichard Henderson#endif 17729e3e0bc6SRichard Henderson 17739e3e0bc6SRichard Henderson#ifndef have_zicond 17749e3e0bc6SRichard Henderson /* Probe for Zicond: czero.eqz zero,zero,zero. */ 17759e3e0bc6SRichard Henderson got_sigill = 0; 17769e3e0bc6SRichard Henderson asm volatile(".insn r 0x33, 5, 0x07, zero, zero, zero" : : : "memory"); 17779e3e0bc6SRichard Henderson have_zicond = !got_sigill; 17789e3e0bc6SRichard Henderson#endif 17799e3e0bc6SRichard Henderson 17809e3e0bc6SRichard Henderson sigaction(SIGILL, &sa_old, NULL); 17819e3e0bc6SRichard Henderson#endif 17829e3e0bc6SRichard Henderson} 17839e3e0bc6SRichard Henderson 1784139c1837SPaolo Bonzinistatic void tcg_target_init(TCGContext *s) 1785139c1837SPaolo Bonzini{ 17869e3e0bc6SRichard Henderson tcg_target_detect_isa(); 17879e3e0bc6SRichard Henderson 1788139c1837SPaolo Bonzini tcg_target_available_regs[TCG_TYPE_I32] = 0xffffffff; 1789139c1837SPaolo Bonzini tcg_target_available_regs[TCG_TYPE_I64] = 0xffffffff; 1790139c1837SPaolo Bonzini 1791139c1837SPaolo Bonzini tcg_target_call_clobber_regs = -1u; 1792139c1837SPaolo Bonzini tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S0); 1793139c1837SPaolo Bonzini tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S1); 1794139c1837SPaolo Bonzini tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S2); 1795139c1837SPaolo Bonzini tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S3); 1796139c1837SPaolo Bonzini tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S4); 1797139c1837SPaolo Bonzini tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S5); 1798139c1837SPaolo Bonzini tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S6); 1799139c1837SPaolo Bonzini tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S7); 1800139c1837SPaolo Bonzini tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S8); 1801139c1837SPaolo Bonzini tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S9); 1802139c1837SPaolo Bonzini tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S10); 1803139c1837SPaolo Bonzini tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S11); 1804139c1837SPaolo Bonzini 1805139c1837SPaolo Bonzini s->reserved_regs = 0; 1806139c1837SPaolo Bonzini tcg_regset_set_reg(s->reserved_regs, TCG_REG_ZERO); 1807139c1837SPaolo Bonzini tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP0); 1808139c1837SPaolo Bonzini tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP1); 1809139c1837SPaolo Bonzini tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP2); 1810139c1837SPaolo Bonzini tcg_regset_set_reg(s->reserved_regs, TCG_REG_SP); 1811139c1837SPaolo Bonzini tcg_regset_set_reg(s->reserved_regs, TCG_REG_GP); 1812139c1837SPaolo Bonzini tcg_regset_set_reg(s->reserved_regs, TCG_REG_TP); 1813139c1837SPaolo Bonzini} 1814139c1837SPaolo Bonzini 1815139c1837SPaolo Bonzinitypedef struct { 1816139c1837SPaolo Bonzini DebugFrameHeader h; 1817139c1837SPaolo Bonzini uint8_t fde_def_cfa[4]; 1818139c1837SPaolo Bonzini uint8_t fde_reg_ofs[ARRAY_SIZE(tcg_target_callee_save_regs) * 2]; 1819139c1837SPaolo Bonzini} DebugFrame; 1820139c1837SPaolo Bonzini 1821139c1837SPaolo Bonzini#define ELF_HOST_MACHINE EM_RISCV 1822139c1837SPaolo Bonzini 1823139c1837SPaolo Bonzinistatic const DebugFrame debug_frame = { 1824139c1837SPaolo Bonzini .h.cie.len = sizeof(DebugFrameCIE) - 4, /* length after .len member */ 1825139c1837SPaolo Bonzini .h.cie.id = -1, 1826139c1837SPaolo Bonzini .h.cie.version = 1, 1827139c1837SPaolo Bonzini .h.cie.code_align = 1, 1828139c1837SPaolo Bonzini .h.cie.data_align = -(TCG_TARGET_REG_BITS / 8) & 0x7f, /* sleb128 */ 1829139c1837SPaolo Bonzini .h.cie.return_column = TCG_REG_RA, 1830139c1837SPaolo Bonzini 1831139c1837SPaolo Bonzini /* Total FDE size does not include the "len" member. */ 1832139c1837SPaolo Bonzini .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset), 1833139c1837SPaolo Bonzini 1834139c1837SPaolo Bonzini .fde_def_cfa = { 1835139c1837SPaolo Bonzini 12, TCG_REG_SP, /* DW_CFA_def_cfa sp, ... */ 1836139c1837SPaolo Bonzini (FRAME_SIZE & 0x7f) | 0x80, /* ... uleb128 FRAME_SIZE */ 1837139c1837SPaolo Bonzini (FRAME_SIZE >> 7) 1838139c1837SPaolo Bonzini }, 1839139c1837SPaolo Bonzini .fde_reg_ofs = { 1840139c1837SPaolo Bonzini 0x80 + 9, 12, /* DW_CFA_offset, s1, -96 */ 1841139c1837SPaolo Bonzini 0x80 + 18, 11, /* DW_CFA_offset, s2, -88 */ 1842139c1837SPaolo Bonzini 0x80 + 19, 10, /* DW_CFA_offset, s3, -80 */ 1843139c1837SPaolo Bonzini 0x80 + 20, 9, /* DW_CFA_offset, s4, -72 */ 1844139c1837SPaolo Bonzini 0x80 + 21, 8, /* DW_CFA_offset, s5, -64 */ 1845139c1837SPaolo Bonzini 0x80 + 22, 7, /* DW_CFA_offset, s6, -56 */ 1846139c1837SPaolo Bonzini 0x80 + 23, 6, /* DW_CFA_offset, s7, -48 */ 1847139c1837SPaolo Bonzini 0x80 + 24, 5, /* DW_CFA_offset, s8, -40 */ 1848139c1837SPaolo Bonzini 0x80 + 25, 4, /* DW_CFA_offset, s9, -32 */ 1849139c1837SPaolo Bonzini 0x80 + 26, 3, /* DW_CFA_offset, s10, -24 */ 1850139c1837SPaolo Bonzini 0x80 + 27, 2, /* DW_CFA_offset, s11, -16 */ 1851139c1837SPaolo Bonzini 0x80 + 1 , 1, /* DW_CFA_offset, ra, -8 */ 1852139c1837SPaolo Bonzini } 1853139c1837SPaolo Bonzini}; 1854139c1837SPaolo Bonzini 1855755bf9e5SRichard Hendersonvoid tcg_register_jit(const void *buf, size_t buf_size) 1856139c1837SPaolo Bonzini{ 1857139c1837SPaolo Bonzini tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame)); 1858139c1837SPaolo Bonzini} 1859