1 //===-- PPCMCTargetDesc.cpp - PowerPC Target Descriptions -----------------===// 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 provides PowerPC specific target descriptions. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "MCTargetDesc/PPCMCTargetDesc.h" 14 #include "MCTargetDesc/PPCInstPrinter.h" 15 #include "MCTargetDesc/PPCMCAsmInfo.h" 16 #include "PPCELFStreamer.h" 17 #include "PPCTargetStreamer.h" 18 #include "PPCXCOFFStreamer.h" 19 #include "TargetInfo/PowerPCTargetInfo.h" 20 #include "llvm/ADT/SmallPtrSet.h" 21 #include "llvm/ADT/StringRef.h" 22 #include "llvm/ADT/Triple.h" 23 #include "llvm/BinaryFormat/ELF.h" 24 #include "llvm/MC/MCAsmBackend.h" 25 #include "llvm/MC/MCAssembler.h" 26 #include "llvm/MC/MCCodeEmitter.h" 27 #include "llvm/MC/MCContext.h" 28 #include "llvm/MC/MCDwarf.h" 29 #include "llvm/MC/MCELFStreamer.h" 30 #include "llvm/MC/MCExpr.h" 31 #include "llvm/MC/MCInstrAnalysis.h" 32 #include "llvm/MC/MCInstrInfo.h" 33 #include "llvm/MC/MCObjectWriter.h" 34 #include "llvm/MC/MCRegisterInfo.h" 35 #include "llvm/MC/MCSectionXCOFF.h" 36 #include "llvm/MC/MCStreamer.h" 37 #include "llvm/MC/MCSubtargetInfo.h" 38 #include "llvm/MC/MCSymbol.h" 39 #include "llvm/MC/MCSymbolELF.h" 40 #include "llvm/MC/MCSymbolXCOFF.h" 41 #include "llvm/MC/TargetRegistry.h" 42 #include "llvm/Support/Casting.h" 43 #include "llvm/Support/CodeGen.h" 44 #include "llvm/Support/ErrorHandling.h" 45 #include "llvm/Support/FormattedStream.h" 46 #include "llvm/Support/raw_ostream.h" 47 48 using namespace llvm; 49 50 #define GET_INSTRINFO_MC_DESC 51 #include "PPCGenInstrInfo.inc" 52 53 #define GET_SUBTARGETINFO_MC_DESC 54 #include "PPCGenSubtargetInfo.inc" 55 56 #define GET_REGINFO_MC_DESC 57 #include "PPCGenRegisterInfo.inc" 58 59 PPCTargetStreamer::PPCTargetStreamer(MCStreamer &S) : MCTargetStreamer(S) {} 60 61 // Pin the vtable to this file. 62 PPCTargetStreamer::~PPCTargetStreamer() = default; 63 64 static MCInstrInfo *createPPCMCInstrInfo() { 65 MCInstrInfo *X = new MCInstrInfo(); 66 InitPPCMCInstrInfo(X); 67 return X; 68 } 69 70 static MCRegisterInfo *createPPCMCRegisterInfo(const Triple &TT) { 71 bool isPPC64 = 72 (TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le); 73 unsigned Flavour = isPPC64 ? 0 : 1; 74 unsigned RA = isPPC64 ? PPC::LR8 : PPC::LR; 75 76 MCRegisterInfo *X = new MCRegisterInfo(); 77 InitPPCMCRegisterInfo(X, RA, Flavour, Flavour); 78 return X; 79 } 80 81 static MCSubtargetInfo *createPPCMCSubtargetInfo(const Triple &TT, 82 StringRef CPU, StringRef FS) { 83 // Set some default feature to MC layer. 84 std::string FullFS = std::string(FS); 85 86 if (TT.isOSAIX()) { 87 if (!FullFS.empty()) 88 FullFS = "+aix," + FullFS; 89 else 90 FullFS = "+aix"; 91 } 92 93 return createPPCMCSubtargetInfoImpl(TT, CPU, /*TuneCPU*/ CPU, FullFS); 94 } 95 96 static MCAsmInfo *createPPCMCAsmInfo(const MCRegisterInfo &MRI, 97 const Triple &TheTriple, 98 const MCTargetOptions &Options) { 99 bool isPPC64 = (TheTriple.getArch() == Triple::ppc64 || 100 TheTriple.getArch() == Triple::ppc64le); 101 102 MCAsmInfo *MAI; 103 if (TheTriple.isOSBinFormatXCOFF()) 104 MAI = new PPCXCOFFMCAsmInfo(isPPC64, TheTriple); 105 else 106 MAI = new PPCELFMCAsmInfo(isPPC64, TheTriple); 107 108 // Initial state of the frame pointer is R1. 109 unsigned Reg = isPPC64 ? PPC::X1 : PPC::R1; 110 MCCFIInstruction Inst = 111 MCCFIInstruction::cfiDefCfa(nullptr, MRI.getDwarfRegNum(Reg, true), 0); 112 MAI->addInitialFrameState(Inst); 113 114 return MAI; 115 } 116 117 static MCStreamer * 118 createPPCELFStreamer(const Triple &T, MCContext &Context, 119 std::unique_ptr<MCAsmBackend> &&MAB, 120 std::unique_ptr<MCObjectWriter> &&OW, 121 std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll) { 122 return createPPCELFStreamer(Context, std::move(MAB), std::move(OW), 123 std::move(Emitter)); 124 } 125 126 static MCStreamer *createPPCXCOFFStreamer( 127 const Triple &T, MCContext &Context, std::unique_ptr<MCAsmBackend> &&MAB, 128 std::unique_ptr<MCObjectWriter> &&OW, 129 std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll) { 130 return createPPCXCOFFStreamer(Context, std::move(MAB), std::move(OW), 131 std::move(Emitter)); 132 } 133 134 namespace { 135 136 class PPCTargetAsmStreamer : public PPCTargetStreamer { 137 formatted_raw_ostream &OS; 138 139 public: 140 PPCTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS) 141 : PPCTargetStreamer(S), OS(OS) {} 142 143 void emitTCEntry(const MCSymbol &S, 144 MCSymbolRefExpr::VariantKind Kind) override { 145 if (const MCSymbolXCOFF *XSym = dyn_cast<MCSymbolXCOFF>(&S)) { 146 MCSymbolXCOFF *TCSym = 147 cast<MCSectionXCOFF>(Streamer.getCurrentSectionOnly()) 148 ->getQualNameSymbol(); 149 // If the variant kind is VK_PPC_AIX_TLSGDM the entry represents the 150 // region handle for the symbol, we add the relocation specifier @m. 151 // If the variant kind is VK_PPC_AIX_TLSGD the entry represents the 152 // variable offset for the symbol, we add the relocation specifier @gd. 153 if (Kind == MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSGD || 154 Kind == MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSGDM) 155 OS << "\t.tc " << TCSym->getName() << "," << XSym->getName() << "@" 156 << MCSymbolRefExpr::getVariantKindName(Kind) << '\n'; 157 else 158 OS << "\t.tc " << TCSym->getName() << "," << XSym->getName() << '\n'; 159 160 if (TCSym->hasRename()) 161 Streamer.emitXCOFFRenameDirective(TCSym, TCSym->getSymbolTableName()); 162 return; 163 } 164 165 OS << "\t.tc " << S.getName() << "[TC]," << S.getName() << '\n'; 166 } 167 168 void emitMachine(StringRef CPU) override { 169 OS << "\t.machine " << CPU << '\n'; 170 } 171 172 void emitAbiVersion(int AbiVersion) override { 173 OS << "\t.abiversion " << AbiVersion << '\n'; 174 } 175 176 void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override { 177 const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo(); 178 179 OS << "\t.localentry\t"; 180 S->print(OS, MAI); 181 OS << ", "; 182 LocalOffset->print(OS, MAI); 183 OS << '\n'; 184 } 185 }; 186 187 class PPCTargetELFStreamer : public PPCTargetStreamer { 188 public: 189 PPCTargetELFStreamer(MCStreamer &S) : PPCTargetStreamer(S) {} 190 191 MCELFStreamer &getStreamer() { 192 return static_cast<MCELFStreamer &>(Streamer); 193 } 194 195 void emitTCEntry(const MCSymbol &S, 196 MCSymbolRefExpr::VariantKind Kind) override { 197 // Creates a R_PPC64_TOC relocation 198 Streamer.emitValueToAlignment(8); 199 Streamer.emitSymbolValue(&S, 8); 200 } 201 202 void emitMachine(StringRef CPU) override { 203 // FIXME: Is there anything to do in here or does this directive only 204 // limit the parser? 205 } 206 207 void emitAbiVersion(int AbiVersion) override { 208 MCAssembler &MCA = getStreamer().getAssembler(); 209 unsigned Flags = MCA.getELFHeaderEFlags(); 210 Flags &= ~ELF::EF_PPC64_ABI; 211 Flags |= (AbiVersion & ELF::EF_PPC64_ABI); 212 MCA.setELFHeaderEFlags(Flags); 213 } 214 215 void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override { 216 MCAssembler &MCA = getStreamer().getAssembler(); 217 218 // encodePPC64LocalEntryOffset will report an error if it cannot 219 // encode LocalOffset. 220 unsigned Encoded = encodePPC64LocalEntryOffset(LocalOffset); 221 222 unsigned Other = S->getOther(); 223 Other &= ~ELF::STO_PPC64_LOCAL_MASK; 224 Other |= Encoded; 225 S->setOther(Other); 226 227 // For GAS compatibility, unless we already saw a .abiversion directive, 228 // set e_flags to indicate ELFv2 ABI. 229 unsigned Flags = MCA.getELFHeaderEFlags(); 230 if ((Flags & ELF::EF_PPC64_ABI) == 0) 231 MCA.setELFHeaderEFlags(Flags | 2); 232 } 233 234 void emitAssignment(MCSymbol *S, const MCExpr *Value) override { 235 auto *Symbol = cast<MCSymbolELF>(S); 236 237 // When encoding an assignment to set symbol A to symbol B, also copy 238 // the st_other bits encoding the local entry point offset. 239 if (copyLocalEntry(Symbol, Value)) 240 UpdateOther.insert(Symbol); 241 else 242 UpdateOther.erase(Symbol); 243 } 244 245 void finish() override { 246 for (auto *Sym : UpdateOther) 247 if (Sym->isVariable()) 248 copyLocalEntry(Sym, Sym->getVariableValue()); 249 250 // Clear the set of symbols that needs to be updated so the streamer can 251 // be reused without issues. 252 UpdateOther.clear(); 253 } 254 255 private: 256 SmallPtrSet<MCSymbolELF *, 32> UpdateOther; 257 258 bool copyLocalEntry(MCSymbolELF *D, const MCExpr *S) { 259 auto *Ref = dyn_cast<const MCSymbolRefExpr>(S); 260 if (!Ref) 261 return false; 262 const auto &RhsSym = cast<MCSymbolELF>(Ref->getSymbol()); 263 unsigned Other = D->getOther(); 264 Other &= ~ELF::STO_PPC64_LOCAL_MASK; 265 Other |= RhsSym.getOther() & ELF::STO_PPC64_LOCAL_MASK; 266 D->setOther(Other); 267 return true; 268 } 269 270 unsigned encodePPC64LocalEntryOffset(const MCExpr *LocalOffset) { 271 MCAssembler &MCA = getStreamer().getAssembler(); 272 int64_t Offset; 273 if (!LocalOffset->evaluateAsAbsolute(Offset, MCA)) 274 MCA.getContext().reportError(LocalOffset->getLoc(), 275 ".localentry expression must be absolute"); 276 277 switch (Offset) { 278 default: 279 MCA.getContext().reportError( 280 LocalOffset->getLoc(), ".localentry expression must be a power of 2"); 281 return 0; 282 case 0: 283 return 0; 284 case 1: 285 return 1 << ELF::STO_PPC64_LOCAL_BIT; 286 case 4: 287 case 8: 288 case 16: 289 case 32: 290 case 64: 291 return (int)Log2(Offset) << (int)ELF::STO_PPC64_LOCAL_BIT; 292 } 293 } 294 }; 295 296 class PPCTargetMachOStreamer : public PPCTargetStreamer { 297 public: 298 PPCTargetMachOStreamer(MCStreamer &S) : PPCTargetStreamer(S) {} 299 300 void emitTCEntry(const MCSymbol &S, 301 MCSymbolRefExpr::VariantKind Kind) override { 302 llvm_unreachable("Unknown pseudo-op: .tc"); 303 } 304 305 void emitMachine(StringRef CPU) override { 306 // FIXME: We should update the CPUType, CPUSubType in the Object file if 307 // the new values are different from the defaults. 308 } 309 310 void emitAbiVersion(int AbiVersion) override { 311 llvm_unreachable("Unknown pseudo-op: .abiversion"); 312 } 313 314 void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override { 315 llvm_unreachable("Unknown pseudo-op: .localentry"); 316 } 317 }; 318 319 class PPCTargetXCOFFStreamer : public PPCTargetStreamer { 320 public: 321 PPCTargetXCOFFStreamer(MCStreamer &S) : PPCTargetStreamer(S) {} 322 323 void emitTCEntry(const MCSymbol &S, 324 MCSymbolRefExpr::VariantKind Kind) override { 325 const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo(); 326 const unsigned PointerSize = MAI->getCodePointerSize(); 327 Streamer.emitValueToAlignment(PointerSize); 328 Streamer.emitValue(MCSymbolRefExpr::create(&S, Kind, Streamer.getContext()), 329 PointerSize); 330 } 331 332 void emitMachine(StringRef CPU) override { 333 llvm_unreachable("Machine pseudo-ops are invalid for XCOFF."); 334 } 335 336 void emitAbiVersion(int AbiVersion) override { 337 llvm_unreachable("ABI-version pseudo-ops are invalid for XCOFF."); 338 } 339 340 void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override { 341 llvm_unreachable("Local-entry pseudo-ops are invalid for XCOFF."); 342 } 343 }; 344 345 } // end anonymous namespace 346 347 static MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S, 348 formatted_raw_ostream &OS, 349 MCInstPrinter *InstPrint, 350 bool isVerboseAsm) { 351 return new PPCTargetAsmStreamer(S, OS); 352 } 353 354 static MCTargetStreamer * 355 createObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) { 356 const Triple &TT = STI.getTargetTriple(); 357 if (TT.isOSBinFormatELF()) 358 return new PPCTargetELFStreamer(S); 359 if (TT.isOSBinFormatXCOFF()) 360 return new PPCTargetXCOFFStreamer(S); 361 return new PPCTargetMachOStreamer(S); 362 } 363 364 static MCInstPrinter *createPPCMCInstPrinter(const Triple &T, 365 unsigned SyntaxVariant, 366 const MCAsmInfo &MAI, 367 const MCInstrInfo &MII, 368 const MCRegisterInfo &MRI) { 369 return new PPCInstPrinter(MAI, MII, MRI, T); 370 } 371 372 namespace { 373 374 class PPCMCInstrAnalysis : public MCInstrAnalysis { 375 public: 376 explicit PPCMCInstrAnalysis(const MCInstrInfo *Info) 377 : MCInstrAnalysis(Info) {} 378 379 bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size, 380 uint64_t &Target) const override { 381 unsigned NumOps = Inst.getNumOperands(); 382 if (NumOps == 0 || 383 Info->get(Inst.getOpcode()).OpInfo[NumOps - 1].OperandType != 384 MCOI::OPERAND_PCREL) 385 return false; 386 Target = Addr + Inst.getOperand(NumOps - 1).getImm() * Size; 387 return true; 388 } 389 }; 390 391 } // end anonymous namespace 392 393 static MCInstrAnalysis *createPPCMCInstrAnalysis(const MCInstrInfo *Info) { 394 return new PPCMCInstrAnalysis(Info); 395 } 396 397 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializePowerPCTargetMC() { 398 for (Target *T : {&getThePPC32Target(), &getThePPC32LETarget(), 399 &getThePPC64Target(), &getThePPC64LETarget()}) { 400 // Register the MC asm info. 401 RegisterMCAsmInfoFn C(*T, createPPCMCAsmInfo); 402 403 // Register the MC instruction info. 404 TargetRegistry::RegisterMCInstrInfo(*T, createPPCMCInstrInfo); 405 406 // Register the MC register info. 407 TargetRegistry::RegisterMCRegInfo(*T, createPPCMCRegisterInfo); 408 409 // Register the MC subtarget info. 410 TargetRegistry::RegisterMCSubtargetInfo(*T, createPPCMCSubtargetInfo); 411 412 // Register the MC instruction analyzer. 413 TargetRegistry::RegisterMCInstrAnalysis(*T, createPPCMCInstrAnalysis); 414 415 // Register the MC Code Emitter 416 TargetRegistry::RegisterMCCodeEmitter(*T, createPPCMCCodeEmitter); 417 418 // Register the asm backend. 419 TargetRegistry::RegisterMCAsmBackend(*T, createPPCAsmBackend); 420 421 // Register the elf streamer. 422 TargetRegistry::RegisterELFStreamer(*T, createPPCELFStreamer); 423 424 // Register the XCOFF streamer. 425 TargetRegistry::RegisterXCOFFStreamer(*T, createPPCXCOFFStreamer); 426 427 // Register the object target streamer. 428 TargetRegistry::RegisterObjectTargetStreamer(*T, 429 createObjectTargetStreamer); 430 431 // Register the asm target streamer. 432 TargetRegistry::RegisterAsmTargetStreamer(*T, createAsmTargetStreamer); 433 434 // Register the MCInstPrinter. 435 TargetRegistry::RegisterMCInstPrinter(*T, createPPCMCInstPrinter); 436 } 437 } 438