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 if (Sym->isVariable()) 205 copyLocalEntry(Sym, Sym->getVariableValue()); 206 } 207 208 private: 209 SmallPtrSet<MCSymbolELF *, 32> UpdateOther; 210 211 bool copyLocalEntry(MCSymbolELF *D, const MCExpr *S) { 212 auto *Ref = dyn_cast<const MCSymbolRefExpr>(S); 213 if (!Ref) 214 return false; 215 const auto &RhsSym = cast<MCSymbolELF>(Ref->getSymbol()); 216 unsigned Other = D->getOther(); 217 Other &= ~ELF::STO_PPC64_LOCAL_MASK; 218 Other |= RhsSym.getOther() & ELF::STO_PPC64_LOCAL_MASK; 219 D->setOther(Other); 220 return true; 221 } 222 }; 223 224 class PPCTargetMachOStreamer : public PPCTargetStreamer { 225 public: 226 PPCTargetMachOStreamer(MCStreamer &S) : PPCTargetStreamer(S) {} 227 228 void emitTCEntry(const MCSymbol &S) override { 229 llvm_unreachable("Unknown pseudo-op: .tc"); 230 } 231 232 void emitMachine(StringRef CPU) override { 233 // FIXME: We should update the CPUType, CPUSubType in the Object file if 234 // the new values are different from the defaults. 235 } 236 237 void emitAbiVersion(int AbiVersion) override { 238 llvm_unreachable("Unknown pseudo-op: .abiversion"); 239 } 240 241 void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override { 242 llvm_unreachable("Unknown pseudo-op: .localentry"); 243 } 244 }; 245 246 class PPCTargetXCOFFStreamer : public PPCTargetStreamer { 247 public: 248 PPCTargetXCOFFStreamer(MCStreamer &S) : PPCTargetStreamer(S) {} 249 250 void emitTCEntry(const MCSymbol &S) override { 251 const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo(); 252 const unsigned PointerSize = MAI->getCodePointerSize(); 253 Streamer.EmitValueToAlignment(PointerSize); 254 Streamer.EmitSymbolValue(&S, PointerSize); 255 } 256 257 void emitMachine(StringRef CPU) override { 258 llvm_unreachable("Machine pseudo-ops are invalid for XCOFF."); 259 } 260 261 void emitAbiVersion(int AbiVersion) override { 262 llvm_unreachable("ABI-version pseudo-ops are invalid for XCOFF."); 263 } 264 265 void emitLocalEntry(MCSymbolELF *S, const MCExpr *LocalOffset) override { 266 llvm_unreachable("Local-entry pseudo-ops are invalid for XCOFF."); 267 } 268 }; 269 270 } // end anonymous namespace 271 272 static MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S, 273 formatted_raw_ostream &OS, 274 MCInstPrinter *InstPrint, 275 bool isVerboseAsm) { 276 return new PPCTargetAsmStreamer(S, OS); 277 } 278 279 static MCTargetStreamer * 280 createObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) { 281 const Triple &TT = STI.getTargetTriple(); 282 if (TT.isOSBinFormatELF()) 283 return new PPCTargetELFStreamer(S); 284 if (TT.isOSBinFormatXCOFF()) 285 return new PPCTargetXCOFFStreamer(S); 286 return new PPCTargetMachOStreamer(S); 287 } 288 289 static MCInstPrinter *createPPCMCInstPrinter(const Triple &T, 290 unsigned SyntaxVariant, 291 const MCAsmInfo &MAI, 292 const MCInstrInfo &MII, 293 const MCRegisterInfo &MRI) { 294 return new PPCInstPrinter(MAI, MII, MRI, T); 295 } 296 297 extern "C" void LLVMInitializePowerPCTargetMC() { 298 for (Target *T : 299 {&getThePPC32Target(), &getThePPC64Target(), &getThePPC64LETarget()}) { 300 // Register the MC asm info. 301 RegisterMCAsmInfoFn C(*T, createPPCMCAsmInfo); 302 303 // Register the MC instruction info. 304 TargetRegistry::RegisterMCInstrInfo(*T, createPPCMCInstrInfo); 305 306 // Register the MC register info. 307 TargetRegistry::RegisterMCRegInfo(*T, createPPCMCRegisterInfo); 308 309 // Register the MC subtarget info. 310 TargetRegistry::RegisterMCSubtargetInfo(*T, createPPCMCSubtargetInfo); 311 312 // Register the MC Code Emitter 313 TargetRegistry::RegisterMCCodeEmitter(*T, createPPCMCCodeEmitter); 314 315 // Register the asm backend. 316 TargetRegistry::RegisterMCAsmBackend(*T, createPPCAsmBackend); 317 318 // Register the object target streamer. 319 TargetRegistry::RegisterObjectTargetStreamer(*T, 320 createObjectTargetStreamer); 321 322 // Register the asm target streamer. 323 TargetRegistry::RegisterAsmTargetStreamer(*T, createAsmTargetStreamer); 324 325 // Register the MCInstPrinter. 326 TargetRegistry::RegisterMCInstPrinter(*T, createPPCMCInstPrinter); 327 } 328 } 329