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