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/BinaryFormat/ELF.h" 23 #include "llvm/MC/MCAsmBackend.h" 24 #include "llvm/MC/MCAssembler.h" 25 #include "llvm/MC/MCCodeEmitter.h" 26 #include "llvm/MC/MCContext.h" 27 #include "llvm/MC/MCDwarf.h" 28 #include "llvm/MC/MCELFStreamer.h" 29 #include "llvm/MC/MCExpr.h" 30 #include "llvm/MC/MCInstrAnalysis.h" 31 #include "llvm/MC/MCInstrInfo.h" 32 #include "llvm/MC/MCObjectWriter.h" 33 #include "llvm/MC/MCRegisterInfo.h" 34 #include "llvm/MC/MCSectionXCOFF.h" 35 #include "llvm/MC/MCStreamer.h" 36 #include "llvm/MC/MCSubtargetInfo.h" 37 #include "llvm/MC/MCSymbol.h" 38 #include "llvm/MC/MCSymbolELF.h" 39 #include "llvm/MC/MCSymbolXCOFF.h" 40 #include "llvm/MC/TargetRegistry.h" 41 #include "llvm/Support/Casting.h" 42 #include "llvm/Support/CodeGen.h" 43 #include "llvm/Support/ErrorHandling.h" 44 #include "llvm/Support/FormattedStream.h" 45 #include "llvm/Support/raw_ostream.h" 46 #include "llvm/TargetParser/Triple.h" 47 48 using namespace llvm; 49 50 #define GET_INSTRINFO_MC_DESC 51 #define ENABLE_INSTR_PREDICATE_VERIFIER 52 #include "PPCGenInstrInfo.inc" 53 54 #define GET_SUBTARGETINFO_MC_DESC 55 #include "PPCGenSubtargetInfo.inc" 56 57 #define GET_REGINFO_MC_DESC 58 #include "PPCGenRegisterInfo.inc" 59 60 PPCTargetStreamer::PPCTargetStreamer(MCStreamer &S) : MCTargetStreamer(S) {} 61 62 // Pin the vtable to this file. 63 PPCTargetStreamer::~PPCTargetStreamer() = default; 64 65 static MCInstrInfo *createPPCMCInstrInfo() { 66 MCInstrInfo *X = new MCInstrInfo(); 67 InitPPCMCInstrInfo(X); 68 return X; 69 } 70 71 static MCRegisterInfo *createPPCMCRegisterInfo(const Triple &TT) { 72 bool isPPC64 = 73 (TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le); 74 unsigned Flavour = isPPC64 ? 0 : 1; 75 unsigned RA = isPPC64 ? PPC::LR8 : PPC::LR; 76 77 MCRegisterInfo *X = new MCRegisterInfo(); 78 InitPPCMCRegisterInfo(X, RA, Flavour, Flavour); 79 return X; 80 } 81 82 static MCSubtargetInfo *createPPCMCSubtargetInfo(const Triple &TT, 83 StringRef CPU, StringRef FS) { 84 // Set some default feature to MC layer. 85 std::string FullFS = std::string(FS); 86 87 if (TT.isOSAIX()) { 88 if (!FullFS.empty()) 89 FullFS = "+aix," + FullFS; 90 else 91 FullFS = "+aix"; 92 } 93 94 return createPPCMCSubtargetInfoImpl(TT, CPU, /*TuneCPU*/ CPU, FullFS); 95 } 96 97 static MCAsmInfo *createPPCMCAsmInfo(const MCRegisterInfo &MRI, 98 const Triple &TheTriple, 99 const MCTargetOptions &Options) { 100 bool isPPC64 = (TheTriple.getArch() == Triple::ppc64 || 101 TheTriple.getArch() == Triple::ppc64le); 102 103 MCAsmInfo *MAI; 104 if (TheTriple.isOSBinFormatXCOFF()) 105 MAI = new PPCXCOFFMCAsmInfo(isPPC64, TheTriple); 106 else 107 MAI = new PPCELFMCAsmInfo(isPPC64, TheTriple); 108 109 // Initial state of the frame pointer is R1. 110 unsigned Reg = isPPC64 ? PPC::X1 : PPC::R1; 111 MCCFIInstruction Inst = 112 MCCFIInstruction::cfiDefCfa(nullptr, MRI.getDwarfRegNum(Reg, true), 0); 113 MAI->addInitialFrameState(Inst); 114 115 return MAI; 116 } 117 118 static MCStreamer * 119 createPPCELFStreamer(const Triple &T, MCContext &Context, 120 std::unique_ptr<MCAsmBackend> &&MAB, 121 std::unique_ptr<MCObjectWriter> &&OW, 122 std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll) { 123 return createPPCELFStreamer(Context, std::move(MAB), std::move(OW), 124 std::move(Emitter)); 125 } 126 127 static MCStreamer *createPPCXCOFFStreamer( 128 const Triple &T, MCContext &Context, std::unique_ptr<MCAsmBackend> &&MAB, 129 std::unique_ptr<MCObjectWriter> &&OW, 130 std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll) { 131 return createPPCXCOFFStreamer(Context, std::move(MAB), std::move(OW), 132 std::move(Emitter)); 133 } 134 135 namespace { 136 137 class PPCTargetAsmStreamer : public PPCTargetStreamer { 138 formatted_raw_ostream &OS; 139 140 public: 141 PPCTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS) 142 : PPCTargetStreamer(S), OS(OS) {} 143 144 void emitTCEntry(const MCSymbol &S, 145 MCSymbolRefExpr::VariantKind Kind) override { 146 if (const MCSymbolXCOFF *XSym = dyn_cast<MCSymbolXCOFF>(&S)) { 147 MCSymbolXCOFF *TCSym = 148 cast<MCSectionXCOFF>(Streamer.getCurrentSectionOnly()) 149 ->getQualNameSymbol(); 150 // On AIX, we have a region handle (symbol@m) and the variable offset 151 // (symbol@{gd|le}) for TLS variables, depending on the TLS model. 152 if (Kind == MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSGD || 153 Kind == MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSGDM || 154 Kind == MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSLE) 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(Align(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 Log2_32(Offset) << 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(Align(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 *createNullTargetStreamer(MCStreamer &S) { 355 return new PPCTargetStreamer(S); 356 } 357 358 static MCTargetStreamer * 359 createObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) { 360 const Triple &TT = STI.getTargetTriple(); 361 if (TT.isOSBinFormatELF()) 362 return new PPCTargetELFStreamer(S); 363 if (TT.isOSBinFormatXCOFF()) 364 return new PPCTargetXCOFFStreamer(S); 365 return new PPCTargetMachOStreamer(S); 366 } 367 368 static MCInstPrinter *createPPCMCInstPrinter(const Triple &T, 369 unsigned SyntaxVariant, 370 const MCAsmInfo &MAI, 371 const MCInstrInfo &MII, 372 const MCRegisterInfo &MRI) { 373 return new PPCInstPrinter(MAI, MII, MRI, T); 374 } 375 376 namespace { 377 378 class PPCMCInstrAnalysis : public MCInstrAnalysis { 379 public: 380 explicit PPCMCInstrAnalysis(const MCInstrInfo *Info) 381 : MCInstrAnalysis(Info) {} 382 383 bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size, 384 uint64_t &Target) const override { 385 unsigned NumOps = Inst.getNumOperands(); 386 if (NumOps == 0 || 387 Info->get(Inst.getOpcode()).operands()[NumOps - 1].OperandType != 388 MCOI::OPERAND_PCREL) 389 return false; 390 Target = Addr + Inst.getOperand(NumOps - 1).getImm() * Size; 391 return true; 392 } 393 }; 394 395 } // end anonymous namespace 396 397 static MCInstrAnalysis *createPPCMCInstrAnalysis(const MCInstrInfo *Info) { 398 return new PPCMCInstrAnalysis(Info); 399 } 400 401 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializePowerPCTargetMC() { 402 for (Target *T : {&getThePPC32Target(), &getThePPC32LETarget(), 403 &getThePPC64Target(), &getThePPC64LETarget()}) { 404 // Register the MC asm info. 405 RegisterMCAsmInfoFn C(*T, createPPCMCAsmInfo); 406 407 // Register the MC instruction info. 408 TargetRegistry::RegisterMCInstrInfo(*T, createPPCMCInstrInfo); 409 410 // Register the MC register info. 411 TargetRegistry::RegisterMCRegInfo(*T, createPPCMCRegisterInfo); 412 413 // Register the MC subtarget info. 414 TargetRegistry::RegisterMCSubtargetInfo(*T, createPPCMCSubtargetInfo); 415 416 // Register the MC instruction analyzer. 417 TargetRegistry::RegisterMCInstrAnalysis(*T, createPPCMCInstrAnalysis); 418 419 // Register the MC Code Emitter 420 TargetRegistry::RegisterMCCodeEmitter(*T, createPPCMCCodeEmitter); 421 422 // Register the asm backend. 423 TargetRegistry::RegisterMCAsmBackend(*T, createPPCAsmBackend); 424 425 // Register the elf streamer. 426 TargetRegistry::RegisterELFStreamer(*T, createPPCELFStreamer); 427 428 // Register the XCOFF streamer. 429 TargetRegistry::RegisterXCOFFStreamer(*T, createPPCXCOFFStreamer); 430 431 // Register the object target streamer. 432 TargetRegistry::RegisterObjectTargetStreamer(*T, 433 createObjectTargetStreamer); 434 435 // Register the asm target streamer. 436 TargetRegistry::RegisterAsmTargetStreamer(*T, createAsmTargetStreamer); 437 438 // Register the null target streamer. 439 TargetRegistry::RegisterNullTargetStreamer(*T, createNullTargetStreamer); 440 441 // Register the MCInstPrinter. 442 TargetRegistry::RegisterMCInstPrinter(*T, createPPCMCInstPrinter); 443 } 444 } 445