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