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