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