1 //===-- Support/TargetRegistry.h - Target Registration ----------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file exposes the TargetRegistry interface, which tools can use to access 11 // the appropriate target specific classes (TargetMachine, AsmPrinter, etc.) 12 // which have been registered. 13 // 14 // Target specific class implementations should register themselves using the 15 // appropriate TargetRegistry interfaces. 16 // 17 //===----------------------------------------------------------------------===// 18 19 #ifndef LLVM_SUPPORT_TARGETREGISTRY_H 20 #define LLVM_SUPPORT_TARGETREGISTRY_H 21 22 #include "llvm-c/Disassembler.h" 23 #include "llvm/ADT/Triple.h" 24 #include "llvm/Support/CodeGen.h" 25 #include "llvm/Support/FormattedStream.h" 26 #include <cassert> 27 #include <memory> 28 #include <string> 29 30 namespace llvm { 31 class AsmPrinter; 32 class MCAsmBackend; 33 class MCAsmInfo; 34 class MCAsmParser; 35 class MCCodeEmitter; 36 class MCCodeGenInfo; 37 class MCContext; 38 class MCDisassembler; 39 class MCInstrAnalysis; 40 class MCInstPrinter; 41 class MCInstrInfo; 42 class MCRegisterInfo; 43 class MCStreamer; 44 class MCSubtargetInfo; 45 class MCSymbolizer; 46 class MCRelocationInfo; 47 class MCTargetAsmParser; 48 class MCTargetOptions; 49 class MCTargetStreamer; 50 class TargetMachine; 51 class TargetOptions; 52 class raw_ostream; 53 class raw_pwrite_stream; 54 class formatted_raw_ostream; 55 56 MCStreamer *createNullStreamer(MCContext &Ctx); 57 MCStreamer *createAsmStreamer(MCContext &Ctx, 58 std::unique_ptr<formatted_raw_ostream> OS, 59 bool isVerboseAsm, bool useDwarfDirectory, 60 MCInstPrinter *InstPrint, MCCodeEmitter *CE, 61 MCAsmBackend *TAB, bool ShowInst); 62 63 /// Takes ownership of \p TAB and \p CE. 64 MCStreamer *createELFStreamer(MCContext &Ctx, MCAsmBackend &TAB, 65 raw_pwrite_stream &OS, MCCodeEmitter *CE, 66 bool RelaxAll); 67 MCStreamer *createMachOStreamer(MCContext &Ctx, MCAsmBackend &TAB, 68 raw_pwrite_stream &OS, MCCodeEmitter *CE, 69 bool RelaxAll, bool DWARFMustBeAtTheEnd, 70 bool LabelSections = false); 71 72 MCRelocationInfo *createMCRelocationInfo(const Triple &TT, MCContext &Ctx); 73 74 MCSymbolizer *createMCSymbolizer(const Triple &TT, LLVMOpInfoCallback GetOpInfo, 75 LLVMSymbolLookupCallback SymbolLookUp, 76 void *DisInfo, MCContext *Ctx, 77 std::unique_ptr<MCRelocationInfo> &&RelInfo); 78 79 /// Target - Wrapper for Target specific information. 80 /// 81 /// For registration purposes, this is a POD type so that targets can be 82 /// registered without the use of static constructors. 83 /// 84 /// Targets should implement a single global instance of this class (which 85 /// will be zero initialized), and pass that instance to the TargetRegistry as 86 /// part of their initialization. 87 class Target { 88 public: 89 friend struct TargetRegistry; 90 91 typedef bool (*ArchMatchFnTy)(Triple::ArchType Arch); 92 93 typedef MCAsmInfo *(*MCAsmInfoCtorFnTy)(const MCRegisterInfo &MRI, 94 const Triple &TT); 95 typedef MCCodeGenInfo *(*MCCodeGenInfoCtorFnTy)(const Triple &TT, 96 Reloc::Model RM, 97 CodeModel::Model CM, 98 CodeGenOpt::Level OL); 99 typedef MCInstrInfo *(*MCInstrInfoCtorFnTy)(void); 100 typedef MCInstrAnalysis *(*MCInstrAnalysisCtorFnTy)(const MCInstrInfo *Info); 101 typedef MCRegisterInfo *(*MCRegInfoCtorFnTy)(const Triple &TT); 102 typedef MCSubtargetInfo *(*MCSubtargetInfoCtorFnTy)(const Triple &TT, 103 StringRef CPU, 104 StringRef Features); 105 typedef TargetMachine *(*TargetMachineCtorTy)( 106 const Target &T, const Triple &TT, StringRef CPU, StringRef Features, 107 const TargetOptions &Options, Reloc::Model RM, CodeModel::Model CM, 108 CodeGenOpt::Level OL); 109 // If it weren't for layering issues (this header is in llvm/Support, but 110 // depends on MC?) this should take the Streamer by value rather than rvalue 111 // reference. 112 typedef AsmPrinter *(*AsmPrinterCtorTy)( 113 TargetMachine &TM, std::unique_ptr<MCStreamer> &&Streamer); 114 typedef MCAsmBackend *(*MCAsmBackendCtorTy)(const Target &T, 115 const MCRegisterInfo &MRI, 116 const Triple &TT, StringRef CPU); 117 typedef MCTargetAsmParser *(*MCAsmParserCtorTy)( 118 MCSubtargetInfo &STI, MCAsmParser &P, const MCInstrInfo &MII, 119 const MCTargetOptions &Options); 120 typedef MCDisassembler *(*MCDisassemblerCtorTy)(const Target &T, 121 const MCSubtargetInfo &STI, 122 MCContext &Ctx); 123 typedef MCInstPrinter *(*MCInstPrinterCtorTy)(const Triple &T, 124 unsigned SyntaxVariant, 125 const MCAsmInfo &MAI, 126 const MCInstrInfo &MII, 127 const MCRegisterInfo &MRI); 128 typedef MCCodeEmitter *(*MCCodeEmitterCtorTy)(const MCInstrInfo &II, 129 const MCRegisterInfo &MRI, 130 MCContext &Ctx); 131 typedef MCStreamer *(*ELFStreamerCtorTy)(const Triple &T, MCContext &Ctx, 132 MCAsmBackend &TAB, 133 raw_pwrite_stream &OS, 134 MCCodeEmitter *Emitter, 135 bool RelaxAll); 136 typedef MCStreamer *(*MachOStreamerCtorTy)(MCContext &Ctx, MCAsmBackend &TAB, 137 raw_pwrite_stream &OS, 138 MCCodeEmitter *Emitter, 139 bool RelaxAll, 140 bool DWARFMustBeAtTheEnd); 141 typedef MCStreamer *(*COFFStreamerCtorTy)(MCContext &Ctx, MCAsmBackend &TAB, 142 raw_pwrite_stream &OS, 143 MCCodeEmitter *Emitter, 144 bool RelaxAll); 145 typedef MCTargetStreamer *(*NullTargetStreamerCtorTy)(MCStreamer &S); 146 typedef MCTargetStreamer *(*AsmTargetStreamerCtorTy)( 147 MCStreamer &S, formatted_raw_ostream &OS, MCInstPrinter *InstPrint, 148 bool IsVerboseAsm); 149 typedef MCTargetStreamer *(*ObjectTargetStreamerCtorTy)( 150 MCStreamer &S, const MCSubtargetInfo &STI); 151 typedef MCRelocationInfo *(*MCRelocationInfoCtorTy)(const Triple &TT, 152 MCContext &Ctx); 153 typedef MCSymbolizer *(*MCSymbolizerCtorTy)( 154 const Triple &TT, LLVMOpInfoCallback GetOpInfo, 155 LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo, MCContext *Ctx, 156 std::unique_ptr<MCRelocationInfo> &&RelInfo); 157 158 private: 159 /// Next - The next registered target in the linked list, maintained by the 160 /// TargetRegistry. 161 Target *Next; 162 163 /// The target function for checking if an architecture is supported. 164 ArchMatchFnTy ArchMatchFn; 165 166 /// Name - The target name. 167 const char *Name; 168 169 /// ShortDesc - A short description of the target. 170 const char *ShortDesc; 171 172 /// HasJIT - Whether this target supports the JIT. 173 bool HasJIT; 174 175 /// MCAsmInfoCtorFn - Constructor function for this target's MCAsmInfo, if 176 /// registered. 177 MCAsmInfoCtorFnTy MCAsmInfoCtorFn; 178 179 /// MCCodeGenInfoCtorFn - Constructor function for this target's 180 /// MCCodeGenInfo, if registered. 181 MCCodeGenInfoCtorFnTy MCCodeGenInfoCtorFn; 182 183 /// MCInstrInfoCtorFn - Constructor function for this target's MCInstrInfo, 184 /// if registered. 185 MCInstrInfoCtorFnTy MCInstrInfoCtorFn; 186 187 /// MCInstrAnalysisCtorFn - Constructor function for this target's 188 /// MCInstrAnalysis, if registered. 189 MCInstrAnalysisCtorFnTy MCInstrAnalysisCtorFn; 190 191 /// MCRegInfoCtorFn - Constructor function for this target's MCRegisterInfo, 192 /// if registered. 193 MCRegInfoCtorFnTy MCRegInfoCtorFn; 194 195 /// MCSubtargetInfoCtorFn - Constructor function for this target's 196 /// MCSubtargetInfo, if registered. 197 MCSubtargetInfoCtorFnTy MCSubtargetInfoCtorFn; 198 199 /// TargetMachineCtorFn - Construction function for this target's 200 /// TargetMachine, if registered. 201 TargetMachineCtorTy TargetMachineCtorFn; 202 203 /// MCAsmBackendCtorFn - Construction function for this target's 204 /// MCAsmBackend, if registered. 205 MCAsmBackendCtorTy MCAsmBackendCtorFn; 206 207 /// MCAsmParserCtorFn - Construction function for this target's 208 /// MCTargetAsmParser, if registered. 209 MCAsmParserCtorTy MCAsmParserCtorFn; 210 211 /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter, 212 /// if registered. 213 AsmPrinterCtorTy AsmPrinterCtorFn; 214 215 /// MCDisassemblerCtorFn - Construction function for this target's 216 /// MCDisassembler, if registered. 217 MCDisassemblerCtorTy MCDisassemblerCtorFn; 218 219 /// MCInstPrinterCtorFn - Construction function for this target's 220 /// MCInstPrinter, if registered. 221 MCInstPrinterCtorTy MCInstPrinterCtorFn; 222 223 /// MCCodeEmitterCtorFn - Construction function for this target's 224 /// CodeEmitter, if registered. 225 MCCodeEmitterCtorTy MCCodeEmitterCtorFn; 226 227 // Construction functions for the various object formats, if registered. 228 COFFStreamerCtorTy COFFStreamerCtorFn; 229 MachOStreamerCtorTy MachOStreamerCtorFn; 230 ELFStreamerCtorTy ELFStreamerCtorFn; 231 232 /// Construction function for this target's null TargetStreamer, if 233 /// registered (default = nullptr). 234 NullTargetStreamerCtorTy NullTargetStreamerCtorFn; 235 236 /// Construction function for this target's asm TargetStreamer, if 237 /// registered (default = nullptr). 238 AsmTargetStreamerCtorTy AsmTargetStreamerCtorFn; 239 240 /// Construction function for this target's obj TargetStreamer, if 241 /// registered (default = nullptr). 242 ObjectTargetStreamerCtorTy ObjectTargetStreamerCtorFn; 243 244 /// MCRelocationInfoCtorFn - Construction function for this target's 245 /// MCRelocationInfo, if registered (default = llvm::createMCRelocationInfo) 246 MCRelocationInfoCtorTy MCRelocationInfoCtorFn; 247 248 /// MCSymbolizerCtorFn - Construction function for this target's 249 /// MCSymbolizer, if registered (default = llvm::createMCSymbolizer) 250 MCSymbolizerCtorTy MCSymbolizerCtorFn; 251 252 public: Target()253 Target() 254 : COFFStreamerCtorFn(nullptr), MachOStreamerCtorFn(nullptr), 255 ELFStreamerCtorFn(nullptr), NullTargetStreamerCtorFn(nullptr), 256 AsmTargetStreamerCtorFn(nullptr), ObjectTargetStreamerCtorFn(nullptr), 257 MCRelocationInfoCtorFn(nullptr), MCSymbolizerCtorFn(nullptr) {} 258 259 /// @name Target Information 260 /// @{ 261 262 // getNext - Return the next registered target. getNext()263 const Target *getNext() const { return Next; } 264 265 /// getName - Get the target name. getName()266 const char *getName() const { return Name; } 267 268 /// getShortDescription - Get a short description of the target. getShortDescription()269 const char *getShortDescription() const { return ShortDesc; } 270 271 /// @} 272 /// @name Feature Predicates 273 /// @{ 274 275 /// hasJIT - Check if this targets supports the just-in-time compilation. hasJIT()276 bool hasJIT() const { return HasJIT; } 277 278 /// hasTargetMachine - Check if this target supports code generation. hasTargetMachine()279 bool hasTargetMachine() const { return TargetMachineCtorFn != nullptr; } 280 281 /// hasMCAsmBackend - Check if this target supports .o generation. hasMCAsmBackend()282 bool hasMCAsmBackend() const { return MCAsmBackendCtorFn != nullptr; } 283 284 /// @} 285 /// @name Feature Constructors 286 /// @{ 287 288 /// createMCAsmInfo - Create a MCAsmInfo implementation for the specified 289 /// target triple. 290 /// 291 /// \param TheTriple This argument is used to determine the target machine 292 /// feature set; it should always be provided. Generally this should be 293 /// either the target triple from the module, or the target triple of the 294 /// host if that does not exist. createMCAsmInfo(const MCRegisterInfo & MRI,StringRef TheTriple)295 MCAsmInfo *createMCAsmInfo(const MCRegisterInfo &MRI, 296 StringRef TheTriple) const { 297 if (!MCAsmInfoCtorFn) 298 return nullptr; 299 return MCAsmInfoCtorFn(MRI, Triple(TheTriple)); 300 } 301 302 /// createMCCodeGenInfo - Create a MCCodeGenInfo implementation. 303 /// createMCCodeGenInfo(StringRef TT,Reloc::Model RM,CodeModel::Model CM,CodeGenOpt::Level OL)304 MCCodeGenInfo *createMCCodeGenInfo(StringRef TT, Reloc::Model RM, 305 CodeModel::Model CM, 306 CodeGenOpt::Level OL) const { 307 if (!MCCodeGenInfoCtorFn) 308 return nullptr; 309 return MCCodeGenInfoCtorFn(Triple(TT), RM, CM, OL); 310 } 311 312 /// createMCInstrInfo - Create a MCInstrInfo implementation. 313 /// createMCInstrInfo()314 MCInstrInfo *createMCInstrInfo() const { 315 if (!MCInstrInfoCtorFn) 316 return nullptr; 317 return MCInstrInfoCtorFn(); 318 } 319 320 /// createMCInstrAnalysis - Create a MCInstrAnalysis implementation. 321 /// createMCInstrAnalysis(const MCInstrInfo * Info)322 MCInstrAnalysis *createMCInstrAnalysis(const MCInstrInfo *Info) const { 323 if (!MCInstrAnalysisCtorFn) 324 return nullptr; 325 return MCInstrAnalysisCtorFn(Info); 326 } 327 328 /// createMCRegInfo - Create a MCRegisterInfo implementation. 329 /// createMCRegInfo(StringRef TT)330 MCRegisterInfo *createMCRegInfo(StringRef TT) const { 331 if (!MCRegInfoCtorFn) 332 return nullptr; 333 return MCRegInfoCtorFn(Triple(TT)); 334 } 335 336 /// createMCSubtargetInfo - Create a MCSubtargetInfo implementation. 337 /// 338 /// \param TheTriple This argument is used to determine the target machine 339 /// feature set; it should always be provided. Generally this should be 340 /// either the target triple from the module, or the target triple of the 341 /// host if that does not exist. 342 /// \param CPU This specifies the name of the target CPU. 343 /// \param Features This specifies the string representation of the 344 /// additional target features. createMCSubtargetInfo(StringRef TheTriple,StringRef CPU,StringRef Features)345 MCSubtargetInfo *createMCSubtargetInfo(StringRef TheTriple, StringRef CPU, 346 StringRef Features) const { 347 if (!MCSubtargetInfoCtorFn) 348 return nullptr; 349 return MCSubtargetInfoCtorFn(Triple(TheTriple), CPU, Features); 350 } 351 352 /// createTargetMachine - Create a target specific machine implementation 353 /// for the specified \p Triple. 354 /// 355 /// \param TT This argument is used to determine the target machine 356 /// feature set; it should always be provided. Generally this should be 357 /// either the target triple from the module, or the target triple of the 358 /// host if that does not exist. 359 TargetMachine * 360 createTargetMachine(StringRef TT, StringRef CPU, StringRef Features, 361 const TargetOptions &Options, 362 Reloc::Model RM = Reloc::Default, 363 CodeModel::Model CM = CodeModel::Default, 364 CodeGenOpt::Level OL = CodeGenOpt::Default) const { 365 if (!TargetMachineCtorFn) 366 return nullptr; 367 return TargetMachineCtorFn(*this, Triple(TT), CPU, Features, Options, RM, 368 CM, OL); 369 } 370 371 /// createMCAsmBackend - Create a target specific assembly parser. 372 /// 373 /// \param TheTriple The target triple string. createMCAsmBackend(const MCRegisterInfo & MRI,StringRef TheTriple,StringRef CPU)374 MCAsmBackend *createMCAsmBackend(const MCRegisterInfo &MRI, 375 StringRef TheTriple, StringRef CPU) const { 376 if (!MCAsmBackendCtorFn) 377 return nullptr; 378 return MCAsmBackendCtorFn(*this, MRI, Triple(TheTriple), CPU); 379 } 380 381 /// createMCAsmParser - Create a target specific assembly parser. 382 /// 383 /// \param Parser The target independent parser implementation to use for 384 /// parsing and lexing. createMCAsmParser(MCSubtargetInfo & STI,MCAsmParser & Parser,const MCInstrInfo & MII,const MCTargetOptions & Options)385 MCTargetAsmParser *createMCAsmParser(MCSubtargetInfo &STI, 386 MCAsmParser &Parser, 387 const MCInstrInfo &MII, 388 const MCTargetOptions &Options) const { 389 if (!MCAsmParserCtorFn) 390 return nullptr; 391 return MCAsmParserCtorFn(STI, Parser, MII, Options); 392 } 393 394 /// createAsmPrinter - Create a target specific assembly printer pass. This 395 /// takes ownership of the MCStreamer object. createAsmPrinter(TargetMachine & TM,std::unique_ptr<MCStreamer> && Streamer)396 AsmPrinter *createAsmPrinter(TargetMachine &TM, 397 std::unique_ptr<MCStreamer> &&Streamer) const { 398 if (!AsmPrinterCtorFn) 399 return nullptr; 400 return AsmPrinterCtorFn(TM, std::move(Streamer)); 401 } 402 createMCDisassembler(const MCSubtargetInfo & STI,MCContext & Ctx)403 MCDisassembler *createMCDisassembler(const MCSubtargetInfo &STI, 404 MCContext &Ctx) const { 405 if (!MCDisassemblerCtorFn) 406 return nullptr; 407 return MCDisassemblerCtorFn(*this, STI, Ctx); 408 } 409 createMCInstPrinter(const Triple & T,unsigned SyntaxVariant,const MCAsmInfo & MAI,const MCInstrInfo & MII,const MCRegisterInfo & MRI)410 MCInstPrinter *createMCInstPrinter(const Triple &T, unsigned SyntaxVariant, 411 const MCAsmInfo &MAI, 412 const MCInstrInfo &MII, 413 const MCRegisterInfo &MRI) const { 414 if (!MCInstPrinterCtorFn) 415 return nullptr; 416 return MCInstPrinterCtorFn(T, SyntaxVariant, MAI, MII, MRI); 417 } 418 419 /// createMCCodeEmitter - Create a target specific code emitter. createMCCodeEmitter(const MCInstrInfo & II,const MCRegisterInfo & MRI,MCContext & Ctx)420 MCCodeEmitter *createMCCodeEmitter(const MCInstrInfo &II, 421 const MCRegisterInfo &MRI, 422 MCContext &Ctx) const { 423 if (!MCCodeEmitterCtorFn) 424 return nullptr; 425 return MCCodeEmitterCtorFn(II, MRI, Ctx); 426 } 427 428 /// Create a target specific MCStreamer. 429 /// 430 /// \param T The target triple. 431 /// \param Ctx The target context. 432 /// \param TAB The target assembler backend object. Takes ownership. 433 /// \param OS The stream object. 434 /// \param Emitter The target independent assembler object.Takes ownership. 435 /// \param RelaxAll Relax all fixups? createMCObjectStreamer(const Triple & T,MCContext & Ctx,MCAsmBackend & TAB,raw_pwrite_stream & OS,MCCodeEmitter * Emitter,const MCSubtargetInfo & STI,bool RelaxAll,bool DWARFMustBeAtTheEnd)436 MCStreamer *createMCObjectStreamer(const Triple &T, MCContext &Ctx, 437 MCAsmBackend &TAB, raw_pwrite_stream &OS, 438 MCCodeEmitter *Emitter, 439 const MCSubtargetInfo &STI, bool RelaxAll, 440 bool DWARFMustBeAtTheEnd) const { 441 MCStreamer *S; 442 switch (T.getObjectFormat()) { 443 default: 444 llvm_unreachable("Unknown object format"); 445 case Triple::COFF: 446 assert(T.isOSWindows() && "only Windows COFF is supported"); 447 S = COFFStreamerCtorFn(Ctx, TAB, OS, Emitter, RelaxAll); 448 break; 449 case Triple::MachO: 450 if (MachOStreamerCtorFn) 451 S = MachOStreamerCtorFn(Ctx, TAB, OS, Emitter, RelaxAll, 452 DWARFMustBeAtTheEnd); 453 else 454 S = createMachOStreamer(Ctx, TAB, OS, Emitter, RelaxAll, 455 DWARFMustBeAtTheEnd); 456 break; 457 case Triple::ELF: 458 if (ELFStreamerCtorFn) 459 S = ELFStreamerCtorFn(T, Ctx, TAB, OS, Emitter, RelaxAll); 460 else 461 S = createELFStreamer(Ctx, TAB, OS, Emitter, RelaxAll); 462 break; 463 } 464 if (ObjectTargetStreamerCtorFn) 465 ObjectTargetStreamerCtorFn(*S, STI); 466 return S; 467 } 468 createAsmStreamer(MCContext & Ctx,std::unique_ptr<formatted_raw_ostream> OS,bool IsVerboseAsm,bool UseDwarfDirectory,MCInstPrinter * InstPrint,MCCodeEmitter * CE,MCAsmBackend * TAB,bool ShowInst)469 MCStreamer *createAsmStreamer(MCContext &Ctx, 470 std::unique_ptr<formatted_raw_ostream> OS, 471 bool IsVerboseAsm, bool UseDwarfDirectory, 472 MCInstPrinter *InstPrint, MCCodeEmitter *CE, 473 MCAsmBackend *TAB, bool ShowInst) const { 474 formatted_raw_ostream &OSRef = *OS; 475 MCStreamer *S = llvm::createAsmStreamer(Ctx, std::move(OS), IsVerboseAsm, 476 UseDwarfDirectory, InstPrint, CE, 477 TAB, ShowInst); 478 createAsmTargetStreamer(*S, OSRef, InstPrint, IsVerboseAsm); 479 return S; 480 } 481 createAsmTargetStreamer(MCStreamer & S,formatted_raw_ostream & OS,MCInstPrinter * InstPrint,bool IsVerboseAsm)482 MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S, 483 formatted_raw_ostream &OS, 484 MCInstPrinter *InstPrint, 485 bool IsVerboseAsm) const { 486 if (AsmTargetStreamerCtorFn) 487 return AsmTargetStreamerCtorFn(S, OS, InstPrint, IsVerboseAsm); 488 return nullptr; 489 } 490 createNullStreamer(MCContext & Ctx)491 MCStreamer *createNullStreamer(MCContext &Ctx) const { 492 MCStreamer *S = llvm::createNullStreamer(Ctx); 493 createNullTargetStreamer(*S); 494 return S; 495 } 496 createNullTargetStreamer(MCStreamer & S)497 MCTargetStreamer *createNullTargetStreamer(MCStreamer &S) const { 498 if (NullTargetStreamerCtorFn) 499 return NullTargetStreamerCtorFn(S); 500 return nullptr; 501 } 502 503 /// createMCRelocationInfo - Create a target specific MCRelocationInfo. 504 /// 505 /// \param TT The target triple. 506 /// \param Ctx The target context. createMCRelocationInfo(StringRef TT,MCContext & Ctx)507 MCRelocationInfo *createMCRelocationInfo(StringRef TT, MCContext &Ctx) const { 508 MCRelocationInfoCtorTy Fn = MCRelocationInfoCtorFn 509 ? MCRelocationInfoCtorFn 510 : llvm::createMCRelocationInfo; 511 return Fn(Triple(TT), Ctx); 512 } 513 514 /// createMCSymbolizer - Create a target specific MCSymbolizer. 515 /// 516 /// \param TT The target triple. 517 /// \param GetOpInfo The function to get the symbolic information for 518 /// operands. 519 /// \param SymbolLookUp The function to lookup a symbol name. 520 /// \param DisInfo The pointer to the block of symbolic information for above 521 /// call 522 /// back. 523 /// \param Ctx The target context. 524 /// \param RelInfo The relocation information for this target. Takes 525 /// ownership. 526 MCSymbolizer * createMCSymbolizer(StringRef TT,LLVMOpInfoCallback GetOpInfo,LLVMSymbolLookupCallback SymbolLookUp,void * DisInfo,MCContext * Ctx,std::unique_ptr<MCRelocationInfo> && RelInfo)527 createMCSymbolizer(StringRef TT, LLVMOpInfoCallback GetOpInfo, 528 LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo, 529 MCContext *Ctx, 530 std::unique_ptr<MCRelocationInfo> &&RelInfo) const { 531 MCSymbolizerCtorTy Fn = 532 MCSymbolizerCtorFn ? MCSymbolizerCtorFn : llvm::createMCSymbolizer; 533 return Fn(Triple(TT), GetOpInfo, SymbolLookUp, DisInfo, Ctx, 534 std::move(RelInfo)); 535 } 536 537 /// @} 538 }; 539 540 /// TargetRegistry - Generic interface to target specific features. 541 struct TargetRegistry { 542 // FIXME: Make this a namespace, probably just move all the Register* 543 // functions into Target (currently they all just set members on the Target 544 // anyway, and Target friends this class so those functions can... 545 // function). 546 TargetRegistry() = delete; 547 548 class iterator 549 : public std::iterator<std::forward_iterator_tag, Target, ptrdiff_t> { 550 const Target *Current; iteratorTargetRegistry551 explicit iterator(Target *T) : Current(T) {} 552 friend struct TargetRegistry; 553 554 public: iteratorTargetRegistry555 iterator() : Current(nullptr) {} 556 557 bool operator==(const iterator &x) const { return Current == x.Current; } 558 bool operator!=(const iterator &x) const { return !operator==(x); } 559 560 // Iterator traversal: forward iteration only 561 iterator &operator++() { // Preincrement 562 assert(Current && "Cannot increment end iterator!"); 563 Current = Current->getNext(); 564 return *this; 565 } 566 iterator operator++(int) { // Postincrement 567 iterator tmp = *this; 568 ++*this; 569 return tmp; 570 } 571 572 const Target &operator*() const { 573 assert(Current && "Cannot dereference end iterator!"); 574 return *Current; 575 } 576 577 const Target *operator->() const { return &operator*(); } 578 }; 579 580 /// printRegisteredTargetsForVersion - Print the registered targets 581 /// appropriately for inclusion in a tool's version output. 582 static void printRegisteredTargetsForVersion(); 583 584 /// @name Registry Access 585 /// @{ 586 587 static iterator_range<iterator> targets(); 588 589 /// lookupTarget - Lookup a target based on a target triple. 590 /// 591 /// \param Triple - The triple to use for finding a target. 592 /// \param Error - On failure, an error string describing why no target was 593 /// found. 594 static const Target *lookupTarget(const std::string &Triple, 595 std::string &Error); 596 597 /// lookupTarget - Lookup a target based on an architecture name 598 /// and a target triple. If the architecture name is non-empty, 599 /// then the lookup is done by architecture. Otherwise, the target 600 /// triple is used. 601 /// 602 /// \param ArchName - The architecture to use for finding a target. 603 /// \param TheTriple - The triple to use for finding a target. The 604 /// triple is updated with canonical architecture name if a lookup 605 /// by architecture is done. 606 /// \param Error - On failure, an error string describing why no target was 607 /// found. 608 static const Target *lookupTarget(const std::string &ArchName, 609 Triple &TheTriple, std::string &Error); 610 611 /// @} 612 /// @name Target Registration 613 /// @{ 614 615 /// RegisterTarget - Register the given target. Attempts to register a 616 /// target which has already been registered will be ignored. 617 /// 618 /// Clients are responsible for ensuring that registration doesn't occur 619 /// while another thread is attempting to access the registry. Typically 620 /// this is done by initializing all targets at program startup. 621 /// 622 /// @param T - The target being registered. 623 /// @param Name - The target name. This should be a static string. 624 /// @param ShortDesc - A short target description. This should be a static 625 /// string. 626 /// @param ArchMatchFn - The arch match checking function for this target. 627 /// @param HasJIT - Whether the target supports JIT code 628 /// generation. 629 static void RegisterTarget(Target &T, const char *Name, const char *ShortDesc, 630 Target::ArchMatchFnTy ArchMatchFn, 631 bool HasJIT = false); 632 633 /// RegisterMCAsmInfo - Register a MCAsmInfo implementation for the 634 /// given target. 635 /// 636 /// Clients are responsible for ensuring that registration doesn't occur 637 /// while another thread is attempting to access the registry. Typically 638 /// this is done by initializing all targets at program startup. 639 /// 640 /// @param T - The target being registered. 641 /// @param Fn - A function to construct a MCAsmInfo for the target. RegisterMCAsmInfoTargetRegistry642 static void RegisterMCAsmInfo(Target &T, Target::MCAsmInfoCtorFnTy Fn) { 643 T.MCAsmInfoCtorFn = Fn; 644 } 645 646 /// RegisterMCCodeGenInfo - Register a MCCodeGenInfo implementation for the 647 /// given target. 648 /// 649 /// Clients are responsible for ensuring that registration doesn't occur 650 /// while another thread is attempting to access the registry. Typically 651 /// this is done by initializing all targets at program startup. 652 /// 653 /// @param T - The target being registered. 654 /// @param Fn - A function to construct a MCCodeGenInfo for the target. RegisterMCCodeGenInfoTargetRegistry655 static void RegisterMCCodeGenInfo(Target &T, 656 Target::MCCodeGenInfoCtorFnTy Fn) { 657 T.MCCodeGenInfoCtorFn = Fn; 658 } 659 660 /// RegisterMCInstrInfo - Register a MCInstrInfo implementation for the 661 /// given target. 662 /// 663 /// Clients are responsible for ensuring that registration doesn't occur 664 /// while another thread is attempting to access the registry. Typically 665 /// this is done by initializing all targets at program startup. 666 /// 667 /// @param T - The target being registered. 668 /// @param Fn - A function to construct a MCInstrInfo for the target. RegisterMCInstrInfoTargetRegistry669 static void RegisterMCInstrInfo(Target &T, Target::MCInstrInfoCtorFnTy Fn) { 670 T.MCInstrInfoCtorFn = Fn; 671 } 672 673 /// RegisterMCInstrAnalysis - Register a MCInstrAnalysis implementation for 674 /// the given target. RegisterMCInstrAnalysisTargetRegistry675 static void RegisterMCInstrAnalysis(Target &T, 676 Target::MCInstrAnalysisCtorFnTy Fn) { 677 T.MCInstrAnalysisCtorFn = Fn; 678 } 679 680 /// RegisterMCRegInfo - Register a MCRegisterInfo implementation for the 681 /// given target. 682 /// 683 /// Clients are responsible for ensuring that registration doesn't occur 684 /// while another thread is attempting to access the registry. Typically 685 /// this is done by initializing all targets at program startup. 686 /// 687 /// @param T - The target being registered. 688 /// @param Fn - A function to construct a MCRegisterInfo for the target. RegisterMCRegInfoTargetRegistry689 static void RegisterMCRegInfo(Target &T, Target::MCRegInfoCtorFnTy Fn) { 690 T.MCRegInfoCtorFn = Fn; 691 } 692 693 /// RegisterMCSubtargetInfo - Register a MCSubtargetInfo implementation for 694 /// the given target. 695 /// 696 /// Clients are responsible for ensuring that registration doesn't occur 697 /// while another thread is attempting to access the registry. Typically 698 /// this is done by initializing all targets at program startup. 699 /// 700 /// @param T - The target being registered. 701 /// @param Fn - A function to construct a MCSubtargetInfo for the target. RegisterMCSubtargetInfoTargetRegistry702 static void RegisterMCSubtargetInfo(Target &T, 703 Target::MCSubtargetInfoCtorFnTy Fn) { 704 T.MCSubtargetInfoCtorFn = Fn; 705 } 706 707 /// RegisterTargetMachine - Register a TargetMachine implementation for the 708 /// given target. 709 /// 710 /// Clients are responsible for ensuring that registration doesn't occur 711 /// while another thread is attempting to access the registry. Typically 712 /// this is done by initializing all targets at program startup. 713 /// 714 /// @param T - The target being registered. 715 /// @param Fn - A function to construct a TargetMachine for the target. RegisterTargetMachineTargetRegistry716 static void RegisterTargetMachine(Target &T, Target::TargetMachineCtorTy Fn) { 717 T.TargetMachineCtorFn = Fn; 718 } 719 720 /// RegisterMCAsmBackend - Register a MCAsmBackend implementation for the 721 /// given target. 722 /// 723 /// Clients are responsible for ensuring that registration doesn't occur 724 /// while another thread is attempting to access the registry. Typically 725 /// this is done by initializing all targets at program startup. 726 /// 727 /// @param T - The target being registered. 728 /// @param Fn - A function to construct an AsmBackend for the target. RegisterMCAsmBackendTargetRegistry729 static void RegisterMCAsmBackend(Target &T, Target::MCAsmBackendCtorTy Fn) { 730 T.MCAsmBackendCtorFn = Fn; 731 } 732 733 /// RegisterMCAsmParser - Register a MCTargetAsmParser implementation for 734 /// the given target. 735 /// 736 /// Clients are responsible for ensuring that registration doesn't occur 737 /// while another thread is attempting to access the registry. Typically 738 /// this is done by initializing all targets at program startup. 739 /// 740 /// @param T - The target being registered. 741 /// @param Fn - A function to construct an MCTargetAsmParser for the target. RegisterMCAsmParserTargetRegistry742 static void RegisterMCAsmParser(Target &T, Target::MCAsmParserCtorTy Fn) { 743 T.MCAsmParserCtorFn = Fn; 744 } 745 746 /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given 747 /// target. 748 /// 749 /// Clients are responsible for ensuring that registration doesn't occur 750 /// while another thread is attempting to access the registry. Typically 751 /// this is done by initializing all targets at program startup. 752 /// 753 /// @param T - The target being registered. 754 /// @param Fn - A function to construct an AsmPrinter for the target. RegisterAsmPrinterTargetRegistry755 static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) { 756 T.AsmPrinterCtorFn = Fn; 757 } 758 759 /// RegisterMCDisassembler - Register a MCDisassembler implementation for 760 /// the given target. 761 /// 762 /// Clients are responsible for ensuring that registration doesn't occur 763 /// while another thread is attempting to access the registry. Typically 764 /// this is done by initializing all targets at program startup. 765 /// 766 /// @param T - The target being registered. 767 /// @param Fn - A function to construct an MCDisassembler for the target. RegisterMCDisassemblerTargetRegistry768 static void RegisterMCDisassembler(Target &T, 769 Target::MCDisassemblerCtorTy Fn) { 770 T.MCDisassemblerCtorFn = Fn; 771 } 772 773 /// RegisterMCInstPrinter - Register a MCInstPrinter implementation for the 774 /// given target. 775 /// 776 /// Clients are responsible for ensuring that registration doesn't occur 777 /// while another thread is attempting to access the registry. Typically 778 /// this is done by initializing all targets at program startup. 779 /// 780 /// @param T - The target being registered. 781 /// @param Fn - A function to construct an MCInstPrinter for the target. RegisterMCInstPrinterTargetRegistry782 static void RegisterMCInstPrinter(Target &T, Target::MCInstPrinterCtorTy Fn) { 783 T.MCInstPrinterCtorFn = Fn; 784 } 785 786 /// RegisterMCCodeEmitter - Register a MCCodeEmitter implementation for the 787 /// given target. 788 /// 789 /// Clients are responsible for ensuring that registration doesn't occur 790 /// while another thread is attempting to access the registry. Typically 791 /// this is done by initializing all targets at program startup. 792 /// 793 /// @param T - The target being registered. 794 /// @param Fn - A function to construct an MCCodeEmitter for the target. RegisterMCCodeEmitterTargetRegistry795 static void RegisterMCCodeEmitter(Target &T, Target::MCCodeEmitterCtorTy Fn) { 796 T.MCCodeEmitterCtorFn = Fn; 797 } 798 RegisterCOFFStreamerTargetRegistry799 static void RegisterCOFFStreamer(Target &T, Target::COFFStreamerCtorTy Fn) { 800 T.COFFStreamerCtorFn = Fn; 801 } 802 RegisterMachOStreamerTargetRegistry803 static void RegisterMachOStreamer(Target &T, Target::MachOStreamerCtorTy Fn) { 804 T.MachOStreamerCtorFn = Fn; 805 } 806 RegisterELFStreamerTargetRegistry807 static void RegisterELFStreamer(Target &T, Target::ELFStreamerCtorTy Fn) { 808 T.ELFStreamerCtorFn = Fn; 809 } 810 RegisterNullTargetStreamerTargetRegistry811 static void RegisterNullTargetStreamer(Target &T, 812 Target::NullTargetStreamerCtorTy Fn) { 813 T.NullTargetStreamerCtorFn = Fn; 814 } 815 RegisterAsmTargetStreamerTargetRegistry816 static void RegisterAsmTargetStreamer(Target &T, 817 Target::AsmTargetStreamerCtorTy Fn) { 818 T.AsmTargetStreamerCtorFn = Fn; 819 } 820 821 static void RegisterObjectTargetStreamerTargetRegistry822 RegisterObjectTargetStreamer(Target &T, 823 Target::ObjectTargetStreamerCtorTy Fn) { 824 T.ObjectTargetStreamerCtorFn = Fn; 825 } 826 827 /// RegisterMCRelocationInfo - Register an MCRelocationInfo 828 /// implementation for the given target. 829 /// 830 /// Clients are responsible for ensuring that registration doesn't occur 831 /// while another thread is attempting to access the registry. Typically 832 /// this is done by initializing all targets at program startup. 833 /// 834 /// @param T - The target being registered. 835 /// @param Fn - A function to construct an MCRelocationInfo for the target. RegisterMCRelocationInfoTargetRegistry836 static void RegisterMCRelocationInfo(Target &T, 837 Target::MCRelocationInfoCtorTy Fn) { 838 T.MCRelocationInfoCtorFn = Fn; 839 } 840 841 /// RegisterMCSymbolizer - Register an MCSymbolizer 842 /// implementation for the given target. 843 /// 844 /// Clients are responsible for ensuring that registration doesn't occur 845 /// while another thread is attempting to access the registry. Typically 846 /// this is done by initializing all targets at program startup. 847 /// 848 /// @param T - The target being registered. 849 /// @param Fn - A function to construct an MCSymbolizer for the target. RegisterMCSymbolizerTargetRegistry850 static void RegisterMCSymbolizer(Target &T, Target::MCSymbolizerCtorTy Fn) { 851 T.MCSymbolizerCtorFn = Fn; 852 } 853 854 /// @} 855 }; 856 857 //===--------------------------------------------------------------------===// 858 859 /// RegisterTarget - Helper template for registering a target, for use in the 860 /// target's initialization function. Usage: 861 /// 862 /// 863 /// Target TheFooTarget; // The global target instance. 864 /// 865 /// extern "C" void LLVMInitializeFooTargetInfo() { 866 /// RegisterTarget<Triple::foo> X(TheFooTarget, "foo", "Foo description"); 867 /// } 868 template <Triple::ArchType TargetArchType = Triple::UnknownArch, 869 bool HasJIT = false> 870 struct RegisterTarget { RegisterTargetRegisterTarget871 RegisterTarget(Target &T, const char *Name, const char *Desc) { 872 TargetRegistry::RegisterTarget(T, Name, Desc, &getArchMatch, HasJIT); 873 } 874 getArchMatchRegisterTarget875 static bool getArchMatch(Triple::ArchType Arch) { 876 return Arch == TargetArchType; 877 } 878 }; 879 880 /// RegisterMCAsmInfo - Helper template for registering a target assembly info 881 /// implementation. This invokes the static "Create" method on the class to 882 /// actually do the construction. Usage: 883 /// 884 /// extern "C" void LLVMInitializeFooTarget() { 885 /// extern Target TheFooTarget; 886 /// RegisterMCAsmInfo<FooMCAsmInfo> X(TheFooTarget); 887 /// } 888 template <class MCAsmInfoImpl> struct RegisterMCAsmInfo { RegisterMCAsmInfoRegisterMCAsmInfo889 RegisterMCAsmInfo(Target &T) { 890 TargetRegistry::RegisterMCAsmInfo(T, &Allocator); 891 } 892 893 private: AllocatorRegisterMCAsmInfo894 static MCAsmInfo *Allocator(const MCRegisterInfo & /*MRI*/, 895 const Triple &TT) { 896 return new MCAsmInfoImpl(TT); 897 } 898 }; 899 900 /// RegisterMCAsmInfoFn - Helper template for registering a target assembly info 901 /// implementation. This invokes the specified function to do the 902 /// construction. Usage: 903 /// 904 /// extern "C" void LLVMInitializeFooTarget() { 905 /// extern Target TheFooTarget; 906 /// RegisterMCAsmInfoFn X(TheFooTarget, TheFunction); 907 /// } 908 struct RegisterMCAsmInfoFn { RegisterMCAsmInfoFnRegisterMCAsmInfoFn909 RegisterMCAsmInfoFn(Target &T, Target::MCAsmInfoCtorFnTy Fn) { 910 TargetRegistry::RegisterMCAsmInfo(T, Fn); 911 } 912 }; 913 914 /// RegisterMCCodeGenInfo - Helper template for registering a target codegen 915 /// info 916 /// implementation. This invokes the static "Create" method on the class 917 /// to actually do the construction. Usage: 918 /// 919 /// extern "C" void LLVMInitializeFooTarget() { 920 /// extern Target TheFooTarget; 921 /// RegisterMCCodeGenInfo<FooMCCodeGenInfo> X(TheFooTarget); 922 /// } 923 template <class MCCodeGenInfoImpl> struct RegisterMCCodeGenInfo { RegisterMCCodeGenInfoRegisterMCCodeGenInfo924 RegisterMCCodeGenInfo(Target &T) { 925 TargetRegistry::RegisterMCCodeGenInfo(T, &Allocator); 926 } 927 928 private: AllocatorRegisterMCCodeGenInfo929 static MCCodeGenInfo *Allocator(const Triple & /*TT*/, Reloc::Model /*RM*/, 930 CodeModel::Model /*CM*/, 931 CodeGenOpt::Level /*OL*/) { 932 return new MCCodeGenInfoImpl(); 933 } 934 }; 935 936 /// RegisterMCCodeGenInfoFn - Helper template for registering a target codegen 937 /// info implementation. This invokes the specified function to do the 938 /// construction. Usage: 939 /// 940 /// extern "C" void LLVMInitializeFooTarget() { 941 /// extern Target TheFooTarget; 942 /// RegisterMCCodeGenInfoFn X(TheFooTarget, TheFunction); 943 /// } 944 struct RegisterMCCodeGenInfoFn { RegisterMCCodeGenInfoFnRegisterMCCodeGenInfoFn945 RegisterMCCodeGenInfoFn(Target &T, Target::MCCodeGenInfoCtorFnTy Fn) { 946 TargetRegistry::RegisterMCCodeGenInfo(T, Fn); 947 } 948 }; 949 950 /// RegisterMCInstrInfo - Helper template for registering a target instruction 951 /// info implementation. This invokes the static "Create" method on the class 952 /// to actually do the construction. Usage: 953 /// 954 /// extern "C" void LLVMInitializeFooTarget() { 955 /// extern Target TheFooTarget; 956 /// RegisterMCInstrInfo<FooMCInstrInfo> X(TheFooTarget); 957 /// } 958 template <class MCInstrInfoImpl> struct RegisterMCInstrInfo { RegisterMCInstrInfoRegisterMCInstrInfo959 RegisterMCInstrInfo(Target &T) { 960 TargetRegistry::RegisterMCInstrInfo(T, &Allocator); 961 } 962 963 private: AllocatorRegisterMCInstrInfo964 static MCInstrInfo *Allocator() { return new MCInstrInfoImpl(); } 965 }; 966 967 /// RegisterMCInstrInfoFn - Helper template for registering a target 968 /// instruction info implementation. This invokes the specified function to 969 /// do the construction. Usage: 970 /// 971 /// extern "C" void LLVMInitializeFooTarget() { 972 /// extern Target TheFooTarget; 973 /// RegisterMCInstrInfoFn X(TheFooTarget, TheFunction); 974 /// } 975 struct RegisterMCInstrInfoFn { RegisterMCInstrInfoFnRegisterMCInstrInfoFn976 RegisterMCInstrInfoFn(Target &T, Target::MCInstrInfoCtorFnTy Fn) { 977 TargetRegistry::RegisterMCInstrInfo(T, Fn); 978 } 979 }; 980 981 /// RegisterMCInstrAnalysis - Helper template for registering a target 982 /// instruction analyzer implementation. This invokes the static "Create" 983 /// method on the class to actually do the construction. Usage: 984 /// 985 /// extern "C" void LLVMInitializeFooTarget() { 986 /// extern Target TheFooTarget; 987 /// RegisterMCInstrAnalysis<FooMCInstrAnalysis> X(TheFooTarget); 988 /// } 989 template <class MCInstrAnalysisImpl> struct RegisterMCInstrAnalysis { RegisterMCInstrAnalysisRegisterMCInstrAnalysis990 RegisterMCInstrAnalysis(Target &T) { 991 TargetRegistry::RegisterMCInstrAnalysis(T, &Allocator); 992 } 993 994 private: AllocatorRegisterMCInstrAnalysis995 static MCInstrAnalysis *Allocator(const MCInstrInfo *Info) { 996 return new MCInstrAnalysisImpl(Info); 997 } 998 }; 999 1000 /// RegisterMCInstrAnalysisFn - Helper template for registering a target 1001 /// instruction analyzer implementation. This invokes the specified function 1002 /// to do the construction. Usage: 1003 /// 1004 /// extern "C" void LLVMInitializeFooTarget() { 1005 /// extern Target TheFooTarget; 1006 /// RegisterMCInstrAnalysisFn X(TheFooTarget, TheFunction); 1007 /// } 1008 struct RegisterMCInstrAnalysisFn { RegisterMCInstrAnalysisFnRegisterMCInstrAnalysisFn1009 RegisterMCInstrAnalysisFn(Target &T, Target::MCInstrAnalysisCtorFnTy Fn) { 1010 TargetRegistry::RegisterMCInstrAnalysis(T, Fn); 1011 } 1012 }; 1013 1014 /// RegisterMCRegInfo - Helper template for registering a target register info 1015 /// implementation. This invokes the static "Create" method on the class to 1016 /// actually do the construction. Usage: 1017 /// 1018 /// extern "C" void LLVMInitializeFooTarget() { 1019 /// extern Target TheFooTarget; 1020 /// RegisterMCRegInfo<FooMCRegInfo> X(TheFooTarget); 1021 /// } 1022 template <class MCRegisterInfoImpl> struct RegisterMCRegInfo { RegisterMCRegInfoRegisterMCRegInfo1023 RegisterMCRegInfo(Target &T) { 1024 TargetRegistry::RegisterMCRegInfo(T, &Allocator); 1025 } 1026 1027 private: AllocatorRegisterMCRegInfo1028 static MCRegisterInfo *Allocator(const Triple & /*TT*/) { 1029 return new MCRegisterInfoImpl(); 1030 } 1031 }; 1032 1033 /// RegisterMCRegInfoFn - Helper template for registering a target register 1034 /// info implementation. This invokes the specified function to do the 1035 /// construction. Usage: 1036 /// 1037 /// extern "C" void LLVMInitializeFooTarget() { 1038 /// extern Target TheFooTarget; 1039 /// RegisterMCRegInfoFn X(TheFooTarget, TheFunction); 1040 /// } 1041 struct RegisterMCRegInfoFn { RegisterMCRegInfoFnRegisterMCRegInfoFn1042 RegisterMCRegInfoFn(Target &T, Target::MCRegInfoCtorFnTy Fn) { 1043 TargetRegistry::RegisterMCRegInfo(T, Fn); 1044 } 1045 }; 1046 1047 /// RegisterMCSubtargetInfo - Helper template for registering a target 1048 /// subtarget info implementation. This invokes the static "Create" method 1049 /// on the class to actually do the construction. Usage: 1050 /// 1051 /// extern "C" void LLVMInitializeFooTarget() { 1052 /// extern Target TheFooTarget; 1053 /// RegisterMCSubtargetInfo<FooMCSubtargetInfo> X(TheFooTarget); 1054 /// } 1055 template <class MCSubtargetInfoImpl> struct RegisterMCSubtargetInfo { RegisterMCSubtargetInfoRegisterMCSubtargetInfo1056 RegisterMCSubtargetInfo(Target &T) { 1057 TargetRegistry::RegisterMCSubtargetInfo(T, &Allocator); 1058 } 1059 1060 private: AllocatorRegisterMCSubtargetInfo1061 static MCSubtargetInfo *Allocator(const Triple & /*TT*/, StringRef /*CPU*/, 1062 StringRef /*FS*/) { 1063 return new MCSubtargetInfoImpl(); 1064 } 1065 }; 1066 1067 /// RegisterMCSubtargetInfoFn - Helper template for registering a target 1068 /// subtarget info implementation. This invokes the specified function to 1069 /// do the construction. Usage: 1070 /// 1071 /// extern "C" void LLVMInitializeFooTarget() { 1072 /// extern Target TheFooTarget; 1073 /// RegisterMCSubtargetInfoFn X(TheFooTarget, TheFunction); 1074 /// } 1075 struct RegisterMCSubtargetInfoFn { RegisterMCSubtargetInfoFnRegisterMCSubtargetInfoFn1076 RegisterMCSubtargetInfoFn(Target &T, Target::MCSubtargetInfoCtorFnTy Fn) { 1077 TargetRegistry::RegisterMCSubtargetInfo(T, Fn); 1078 } 1079 }; 1080 1081 /// RegisterTargetMachine - Helper template for registering a target machine 1082 /// implementation, for use in the target machine initialization 1083 /// function. Usage: 1084 /// 1085 /// extern "C" void LLVMInitializeFooTarget() { 1086 /// extern Target TheFooTarget; 1087 /// RegisterTargetMachine<FooTargetMachine> X(TheFooTarget); 1088 /// } 1089 template <class TargetMachineImpl> struct RegisterTargetMachine { RegisterTargetMachineRegisterTargetMachine1090 RegisterTargetMachine(Target &T) { 1091 TargetRegistry::RegisterTargetMachine(T, &Allocator); 1092 } 1093 1094 private: AllocatorRegisterTargetMachine1095 static TargetMachine *Allocator(const Target &T, const Triple &TT, 1096 StringRef CPU, StringRef FS, 1097 const TargetOptions &Options, Reloc::Model RM, 1098 CodeModel::Model CM, CodeGenOpt::Level OL) { 1099 return new TargetMachineImpl(T, TT, CPU, FS, Options, RM, CM, OL); 1100 } 1101 }; 1102 1103 /// RegisterMCAsmBackend - Helper template for registering a target specific 1104 /// assembler backend. Usage: 1105 /// 1106 /// extern "C" void LLVMInitializeFooMCAsmBackend() { 1107 /// extern Target TheFooTarget; 1108 /// RegisterMCAsmBackend<FooAsmLexer> X(TheFooTarget); 1109 /// } 1110 template <class MCAsmBackendImpl> struct RegisterMCAsmBackend { RegisterMCAsmBackendRegisterMCAsmBackend1111 RegisterMCAsmBackend(Target &T) { 1112 TargetRegistry::RegisterMCAsmBackend(T, &Allocator); 1113 } 1114 1115 private: AllocatorRegisterMCAsmBackend1116 static MCAsmBackend *Allocator(const Target &T, const MCRegisterInfo &MRI, 1117 const Triple &TheTriple, StringRef CPU) { 1118 return new MCAsmBackendImpl(T, MRI, TheTriple, CPU); 1119 } 1120 }; 1121 1122 /// RegisterMCAsmParser - Helper template for registering a target specific 1123 /// assembly parser, for use in the target machine initialization 1124 /// function. Usage: 1125 /// 1126 /// extern "C" void LLVMInitializeFooMCAsmParser() { 1127 /// extern Target TheFooTarget; 1128 /// RegisterMCAsmParser<FooAsmParser> X(TheFooTarget); 1129 /// } 1130 template <class MCAsmParserImpl> struct RegisterMCAsmParser { RegisterMCAsmParserRegisterMCAsmParser1131 RegisterMCAsmParser(Target &T) { 1132 TargetRegistry::RegisterMCAsmParser(T, &Allocator); 1133 } 1134 1135 private: AllocatorRegisterMCAsmParser1136 static MCTargetAsmParser *Allocator(MCSubtargetInfo &STI, MCAsmParser &P, 1137 const MCInstrInfo &MII, 1138 const MCTargetOptions &Options) { 1139 return new MCAsmParserImpl(STI, P, MII, Options); 1140 } 1141 }; 1142 1143 /// RegisterAsmPrinter - Helper template for registering a target specific 1144 /// assembly printer, for use in the target machine initialization 1145 /// function. Usage: 1146 /// 1147 /// extern "C" void LLVMInitializeFooAsmPrinter() { 1148 /// extern Target TheFooTarget; 1149 /// RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget); 1150 /// } 1151 template <class AsmPrinterImpl> struct RegisterAsmPrinter { RegisterAsmPrinterRegisterAsmPrinter1152 RegisterAsmPrinter(Target &T) { 1153 TargetRegistry::RegisterAsmPrinter(T, &Allocator); 1154 } 1155 1156 private: AllocatorRegisterAsmPrinter1157 static AsmPrinter *Allocator(TargetMachine &TM, 1158 std::unique_ptr<MCStreamer> &&Streamer) { 1159 return new AsmPrinterImpl(TM, std::move(Streamer)); 1160 } 1161 }; 1162 1163 /// RegisterMCCodeEmitter - Helper template for registering a target specific 1164 /// machine code emitter, for use in the target initialization 1165 /// function. Usage: 1166 /// 1167 /// extern "C" void LLVMInitializeFooMCCodeEmitter() { 1168 /// extern Target TheFooTarget; 1169 /// RegisterMCCodeEmitter<FooCodeEmitter> X(TheFooTarget); 1170 /// } 1171 template <class MCCodeEmitterImpl> struct RegisterMCCodeEmitter { RegisterMCCodeEmitterRegisterMCCodeEmitter1172 RegisterMCCodeEmitter(Target &T) { 1173 TargetRegistry::RegisterMCCodeEmitter(T, &Allocator); 1174 } 1175 1176 private: AllocatorRegisterMCCodeEmitter1177 static MCCodeEmitter *Allocator(const MCInstrInfo & /*II*/, 1178 const MCRegisterInfo & /*MRI*/, 1179 MCContext & /*Ctx*/) { 1180 return new MCCodeEmitterImpl(); 1181 } 1182 }; 1183 } 1184 1185 #endif 1186