1 //===-- PPCMCCodeEmitter.cpp - Convert PPC code to machine code -----------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the PPCMCCodeEmitter class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "MCTargetDesc/PPCFixupKinds.h" 14 #include "PPCInstrInfo.h" 15 #include "PPCMCCodeEmitter.h" 16 #include "llvm/ADT/SmallVector.h" 17 #include "llvm/ADT/Statistic.h" 18 #include "llvm/ADT/Triple.h" 19 #include "llvm/MC/MCFixup.h" 20 #include "llvm/MC/MCInstrDesc.h" 21 #include "llvm/MC/MCRegisterInfo.h" 22 #include "llvm/Support/Endian.h" 23 #include "llvm/Support/EndianStream.h" 24 #include "llvm/Support/ErrorHandling.h" 25 #include "llvm/Support/MathExtras.h" 26 #include "llvm/Support/raw_ostream.h" 27 #include <cassert> 28 #include <cstdint> 29 30 using namespace llvm; 31 32 #define DEBUG_TYPE "mccodeemitter" 33 34 STATISTIC(MCNumEmitted, "Number of MC instructions emitted"); 35 36 MCCodeEmitter *llvm::createPPCMCCodeEmitter(const MCInstrInfo &MCII, 37 const MCRegisterInfo &MRI, 38 MCContext &Ctx) { 39 return new PPCMCCodeEmitter(MCII, Ctx); 40 } 41 42 unsigned PPCMCCodeEmitter:: 43 getDirectBrEncoding(const MCInst &MI, unsigned OpNo, 44 SmallVectorImpl<MCFixup> &Fixups, 45 const MCSubtargetInfo &STI) const { 46 const MCOperand &MO = MI.getOperand(OpNo); 47 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI); 48 49 // Add a fixup for the branch target. 50 Fixups.push_back(MCFixup::create(0, MO.getExpr(), 51 ((MI.getOpcode() == PPC::BL8_NOTOC) 52 ? (MCFixupKind)PPC::fixup_ppc_br24_notoc 53 : (MCFixupKind)PPC::fixup_ppc_br24))); 54 return 0; 55 } 56 57 unsigned PPCMCCodeEmitter::getCondBrEncoding(const MCInst &MI, unsigned OpNo, 58 SmallVectorImpl<MCFixup> &Fixups, 59 const MCSubtargetInfo &STI) const { 60 const MCOperand &MO = MI.getOperand(OpNo); 61 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI); 62 63 // Add a fixup for the branch target. 64 Fixups.push_back(MCFixup::create(0, MO.getExpr(), 65 (MCFixupKind)PPC::fixup_ppc_brcond14)); 66 return 0; 67 } 68 69 unsigned PPCMCCodeEmitter:: 70 getAbsDirectBrEncoding(const MCInst &MI, unsigned OpNo, 71 SmallVectorImpl<MCFixup> &Fixups, 72 const MCSubtargetInfo &STI) const { 73 const MCOperand &MO = MI.getOperand(OpNo); 74 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI); 75 76 // Add a fixup for the branch target. 77 Fixups.push_back(MCFixup::create(0, MO.getExpr(), 78 (MCFixupKind)PPC::fixup_ppc_br24abs)); 79 return 0; 80 } 81 82 unsigned PPCMCCodeEmitter:: 83 getAbsCondBrEncoding(const MCInst &MI, unsigned OpNo, 84 SmallVectorImpl<MCFixup> &Fixups, 85 const MCSubtargetInfo &STI) const { 86 const MCOperand &MO = MI.getOperand(OpNo); 87 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI); 88 89 // Add a fixup for the branch target. 90 Fixups.push_back(MCFixup::create(0, MO.getExpr(), 91 (MCFixupKind)PPC::fixup_ppc_brcond14abs)); 92 return 0; 93 } 94 95 unsigned PPCMCCodeEmitter::getImm16Encoding(const MCInst &MI, unsigned OpNo, 96 SmallVectorImpl<MCFixup> &Fixups, 97 const MCSubtargetInfo &STI) const { 98 const MCOperand &MO = MI.getOperand(OpNo); 99 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI); 100 101 // Add a fixup for the immediate field. 102 Fixups.push_back(MCFixup::create(IsLittleEndian? 0 : 2, MO.getExpr(), 103 (MCFixupKind)PPC::fixup_ppc_half16)); 104 return 0; 105 } 106 107 uint64_t 108 PPCMCCodeEmitter::getImm34Encoding(const MCInst &MI, unsigned OpNo, 109 SmallVectorImpl<MCFixup> &Fixups, 110 const MCSubtargetInfo &STI) const { 111 const MCOperand &MO = MI.getOperand(OpNo); 112 if (MO.isReg() || MO.isImm()) 113 return getMachineOpValue(MI, MO, Fixups, STI); 114 115 // Add a fixup for the immediate field. 116 Fixups.push_back(MCFixup::create(IsLittleEndian? 0 : 1, MO.getExpr(), 117 (MCFixupKind)PPC::fixup_ppc_pcrel34)); 118 return 0; 119 } 120 121 unsigned PPCMCCodeEmitter::getMemRIEncoding(const MCInst &MI, unsigned OpNo, 122 SmallVectorImpl<MCFixup> &Fixups, 123 const MCSubtargetInfo &STI) const { 124 // Encode (imm, reg) as a memri, which has the low 16-bits as the 125 // displacement and the next 5 bits as the register #. 126 assert(MI.getOperand(OpNo+1).isReg()); 127 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 16; 128 129 const MCOperand &MO = MI.getOperand(OpNo); 130 if (MO.isImm()) 131 return (getMachineOpValue(MI, MO, Fixups, STI) & 0xFFFF) | RegBits; 132 133 // Add a fixup for the displacement field. 134 Fixups.push_back(MCFixup::create(IsLittleEndian? 0 : 2, MO.getExpr(), 135 (MCFixupKind)PPC::fixup_ppc_half16)); 136 return RegBits; 137 } 138 139 unsigned PPCMCCodeEmitter::getMemRIXEncoding(const MCInst &MI, unsigned OpNo, 140 SmallVectorImpl<MCFixup> &Fixups, 141 const MCSubtargetInfo &STI) const { 142 // Encode (imm, reg) as a memrix, which has the low 14-bits as the 143 // displacement and the next 5 bits as the register #. 144 assert(MI.getOperand(OpNo+1).isReg()); 145 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 14; 146 147 const MCOperand &MO = MI.getOperand(OpNo); 148 if (MO.isImm()) 149 return ((getMachineOpValue(MI, MO, Fixups, STI) >> 2) & 0x3FFF) | RegBits; 150 151 // Add a fixup for the displacement field. 152 Fixups.push_back(MCFixup::create(IsLittleEndian? 0 : 2, MO.getExpr(), 153 (MCFixupKind)PPC::fixup_ppc_half16ds)); 154 return RegBits; 155 } 156 157 unsigned PPCMCCodeEmitter::getMemRIX16Encoding(const MCInst &MI, unsigned OpNo, 158 SmallVectorImpl<MCFixup> &Fixups, 159 const MCSubtargetInfo &STI) const { 160 // Encode (imm, reg) as a memrix16, which has the low 12-bits as the 161 // displacement and the next 5 bits as the register #. 162 assert(MI.getOperand(OpNo+1).isReg()); 163 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 12; 164 165 const MCOperand &MO = MI.getOperand(OpNo); 166 if (MO.isImm()) { 167 assert(!(MO.getImm() % 16) && 168 "Expecting an immediate that is a multiple of 16"); 169 return ((getMachineOpValue(MI, MO, Fixups, STI) >> 4) & 0xFFF) | RegBits; 170 } 171 172 // Otherwise add a fixup for the displacement field. 173 Fixups.push_back(MCFixup::create(IsLittleEndian? 0 : 2, MO.getExpr(), 174 (MCFixupKind)PPC::fixup_ppc_half16ds)); 175 return RegBits; 176 } 177 178 uint64_t 179 PPCMCCodeEmitter::getMemRI34PCRelEncoding(const MCInst &MI, unsigned OpNo, 180 SmallVectorImpl<MCFixup> &Fixups, 181 const MCSubtargetInfo &STI) const { 182 // Encode (imm, reg) as a memri34, which has the low 34-bits as the 183 // displacement and the next 5 bits as an immediate 0. 184 assert(MI.getOperand(OpNo + 1).isImm() && "Expecting an immediate."); 185 uint64_t RegBits = 186 getMachineOpValue(MI, MI.getOperand(OpNo + 1), Fixups, STI) << 34; 187 188 if (RegBits != 0) 189 report_fatal_error("Operand must be 0"); 190 191 const MCOperand &MO = MI.getOperand(OpNo); 192 if (MO.isExpr()) { 193 const MCExpr *Expr = MO.getExpr(); 194 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(Expr); 195 (void)SRE; 196 assert(SRE->getKind() == MCSymbolRefExpr::VK_PCREL && 197 "VariantKind must be VK_PCREL"); 198 Fixups.push_back( 199 MCFixup::create(IsLittleEndian ? 0 : 1, Expr, 200 static_cast<MCFixupKind>(PPC::fixup_ppc_pcrel34))); 201 return 0; 202 } 203 return ((getMachineOpValue(MI, MO, Fixups, STI)) & 0x3FFFFFFFFUL) | RegBits; 204 } 205 206 uint64_t 207 PPCMCCodeEmitter::getMemRI34Encoding(const MCInst &MI, unsigned OpNo, 208 SmallVectorImpl<MCFixup> &Fixups, 209 const MCSubtargetInfo &STI) const { 210 // Encode (imm, reg) as a memri34, which has the low 34-bits as the 211 // displacement and the next 5 bits as the register #. 212 assert(MI.getOperand(OpNo + 1).isReg() && "Expecting a register."); 213 uint64_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo + 1), Fixups, STI) 214 << 34; 215 const MCOperand &MO = MI.getOperand(OpNo); 216 return ((getMachineOpValue(MI, MO, Fixups, STI)) & 0x3FFFFFFFFUL) | RegBits; 217 } 218 219 unsigned PPCMCCodeEmitter::getSPE8DisEncoding(const MCInst &MI, unsigned OpNo, 220 SmallVectorImpl<MCFixup> &Fixups, 221 const MCSubtargetInfo &STI) 222 const { 223 // Encode (imm, reg) as a spe8dis, which has the low 5-bits of (imm / 8) 224 // as the displacement and the next 5 bits as the register #. 225 assert(MI.getOperand(OpNo+1).isReg()); 226 uint32_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 5; 227 228 const MCOperand &MO = MI.getOperand(OpNo); 229 assert(MO.isImm()); 230 uint32_t Imm = getMachineOpValue(MI, MO, Fixups, STI) >> 3; 231 return reverseBits(Imm | RegBits) >> 22; 232 } 233 234 unsigned PPCMCCodeEmitter::getSPE4DisEncoding(const MCInst &MI, unsigned OpNo, 235 SmallVectorImpl<MCFixup> &Fixups, 236 const MCSubtargetInfo &STI) 237 const { 238 // Encode (imm, reg) as a spe4dis, which has the low 5-bits of (imm / 4) 239 // as the displacement and the next 5 bits as the register #. 240 assert(MI.getOperand(OpNo+1).isReg()); 241 uint32_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 5; 242 243 const MCOperand &MO = MI.getOperand(OpNo); 244 assert(MO.isImm()); 245 uint32_t Imm = getMachineOpValue(MI, MO, Fixups, STI) >> 2; 246 return reverseBits(Imm | RegBits) >> 22; 247 } 248 249 unsigned PPCMCCodeEmitter::getSPE2DisEncoding(const MCInst &MI, unsigned OpNo, 250 SmallVectorImpl<MCFixup> &Fixups, 251 const MCSubtargetInfo &STI) 252 const { 253 // Encode (imm, reg) as a spe2dis, which has the low 5-bits of (imm / 2) 254 // as the displacement and the next 5 bits as the register #. 255 assert(MI.getOperand(OpNo+1).isReg()); 256 uint32_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 5; 257 258 const MCOperand &MO = MI.getOperand(OpNo); 259 assert(MO.isImm()); 260 uint32_t Imm = getMachineOpValue(MI, MO, Fixups, STI) >> 1; 261 return reverseBits(Imm | RegBits) >> 22; 262 } 263 264 unsigned PPCMCCodeEmitter::getTLSRegEncoding(const MCInst &MI, unsigned OpNo, 265 SmallVectorImpl<MCFixup> &Fixups, 266 const MCSubtargetInfo &STI) const { 267 const MCOperand &MO = MI.getOperand(OpNo); 268 if (MO.isReg()) return getMachineOpValue(MI, MO, Fixups, STI); 269 270 // Add a fixup for the TLS register, which simply provides a relocation 271 // hint to the linker that this statement is part of a relocation sequence. 272 // Return the thread-pointer register's encoding. 273 Fixups.push_back(MCFixup::create(0, MO.getExpr(), 274 (MCFixupKind)PPC::fixup_ppc_nofixup)); 275 const Triple &TT = STI.getTargetTriple(); 276 bool isPPC64 = TT.isPPC64(); 277 return CTX.getRegisterInfo()->getEncodingValue(isPPC64 ? PPC::X13 : PPC::R2); 278 } 279 280 unsigned PPCMCCodeEmitter::getTLSCallEncoding(const MCInst &MI, unsigned OpNo, 281 SmallVectorImpl<MCFixup> &Fixups, 282 const MCSubtargetInfo &STI) const { 283 // For special TLS calls, we need two fixups; one for the branch target 284 // (__tls_get_addr), which we create via getDirectBrEncoding as usual, 285 // and one for the TLSGD or TLSLD symbol, which is emitted here. 286 const MCOperand &MO = MI.getOperand(OpNo+1); 287 Fixups.push_back(MCFixup::create(0, MO.getExpr(), 288 (MCFixupKind)PPC::fixup_ppc_nofixup)); 289 return getDirectBrEncoding(MI, OpNo, Fixups, STI); 290 } 291 292 unsigned PPCMCCodeEmitter:: 293 get_crbitm_encoding(const MCInst &MI, unsigned OpNo, 294 SmallVectorImpl<MCFixup> &Fixups, 295 const MCSubtargetInfo &STI) const { 296 const MCOperand &MO = MI.getOperand(OpNo); 297 assert((MI.getOpcode() == PPC::MTOCRF || MI.getOpcode() == PPC::MTOCRF8 || 298 MI.getOpcode() == PPC::MFOCRF || MI.getOpcode() == PPC::MFOCRF8) && 299 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)); 300 return 0x80 >> CTX.getRegisterInfo()->getEncodingValue(MO.getReg()); 301 } 302 303 // Get the index for this operand in this instruction. This is needed for 304 // computing the register number in PPCInstrInfo::getRegNumForOperand() for 305 // any instructions that use a different numbering scheme for registers in 306 // different operands. 307 static unsigned getOpIdxForMO(const MCInst &MI, const MCOperand &MO) { 308 for (unsigned i = 0; i < MI.getNumOperands(); i++) { 309 const MCOperand &Op = MI.getOperand(i); 310 if (&Op == &MO) 311 return i; 312 } 313 llvm_unreachable("This operand is not part of this instruction"); 314 return ~0U; // Silence any warnings about no return. 315 } 316 317 uint64_t PPCMCCodeEmitter:: 318 getMachineOpValue(const MCInst &MI, const MCOperand &MO, 319 SmallVectorImpl<MCFixup> &Fixups, 320 const MCSubtargetInfo &STI) const { 321 if (MO.isReg()) { 322 // MTOCRF/MFOCRF should go through get_crbitm_encoding for the CR operand. 323 // The GPR operand should come through here though. 324 assert((MI.getOpcode() != PPC::MTOCRF && MI.getOpcode() != PPC::MTOCRF8 && 325 MI.getOpcode() != PPC::MFOCRF && MI.getOpcode() != PPC::MFOCRF8) || 326 MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7); 327 unsigned OpNo = getOpIdxForMO(MI, MO); 328 unsigned Reg = 329 PPCInstrInfo::getRegNumForOperand(MCII.get(MI.getOpcode()), 330 MO.getReg(), OpNo); 331 return CTX.getRegisterInfo()->getEncodingValue(Reg); 332 } 333 334 assert(MO.isImm() && 335 "Relocation required in an instruction that we cannot encode!"); 336 return MO.getImm(); 337 } 338 339 void PPCMCCodeEmitter::encodeInstruction( 340 const MCInst &MI, raw_ostream &OS, SmallVectorImpl<MCFixup> &Fixups, 341 const MCSubtargetInfo &STI) const { 342 verifyInstructionPredicates(MI, 343 computeAvailableFeatures(STI.getFeatureBits())); 344 345 uint64_t Bits = getBinaryCodeForInstr(MI, Fixups, STI); 346 347 // Output the constant in big/little endian byte order. 348 unsigned Size = getInstSizeInBytes(MI); 349 support::endianness E = IsLittleEndian ? support::little : support::big; 350 switch (Size) { 351 case 0: 352 break; 353 case 4: 354 support::endian::write<uint32_t>(OS, Bits, E); 355 break; 356 case 8: 357 // If we emit a pair of instructions, the first one is 358 // always in the top 32 bits, even on little-endian. 359 support::endian::write<uint32_t>(OS, Bits >> 32, E); 360 support::endian::write<uint32_t>(OS, Bits, E); 361 break; 362 default: 363 llvm_unreachable("Invalid instruction size"); 364 } 365 366 ++MCNumEmitted; // Keep track of the # of mi's emitted. 367 } 368 369 // Get the number of bytes used to encode the given MCInst. 370 unsigned PPCMCCodeEmitter::getInstSizeInBytes(const MCInst &MI) const { 371 unsigned Opcode = MI.getOpcode(); 372 const MCInstrDesc &Desc = MCII.get(Opcode); 373 return Desc.getSize(); 374 } 375 376 bool PPCMCCodeEmitter::isPrefixedInstruction(const MCInst &MI) const { 377 unsigned Opcode = MI.getOpcode(); 378 const PPCInstrInfo *InstrInfo = static_cast<const PPCInstrInfo*>(&MCII); 379 return InstrInfo->isPrefixed(Opcode); 380 } 381 382 #define ENABLE_INSTR_PREDICATE_VERIFIER 383 #include "PPCGenMCCodeEmitter.inc" 384