1 //===- MC/TargetRegistry.h - Target Registration ----------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file exposes the TargetRegistry interface, which tools can use to access 10 // the appropriate target specific classes (TargetMachine, AsmPrinter, etc.) 11 // which have been registered. 12 // 13 // Target specific class implementations should register themselves using the 14 // appropriate TargetRegistry interfaces. 15 // 16 //===----------------------------------------------------------------------===// 17 18 #ifndef LLVM_MC_TARGETREGISTRY_H 19 #define LLVM_MC_TARGETREGISTRY_H 20 21 #include "llvm-c/DisassemblerTypes.h" 22 #include "llvm/ADT/StringRef.h" 23 #include "llvm/ADT/iterator_range.h" 24 #include "llvm/MC/MCObjectFileInfo.h" 25 #include "llvm/Support/CodeGen.h" 26 #include "llvm/Support/Compiler.h" 27 #include "llvm/Support/ErrorHandling.h" 28 #include "llvm/Support/FormattedStream.h" 29 #include "llvm/TargetParser/Triple.h" 30 #include <cassert> 31 #include <cstddef> 32 #include <iterator> 33 #include <memory> 34 #include <optional> 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 MCObjectWriter; 50 class MCRegisterInfo; 51 class MCRelocationInfo; 52 class MCStreamer; 53 class MCSubtargetInfo; 54 class MCSymbolizer; 55 class MCTargetAsmParser; 56 class MCTargetOptions; 57 class MCTargetStreamer; 58 class raw_ostream; 59 class TargetMachine; 60 class TargetOptions; 61 namespace mca { 62 class CustomBehaviour; 63 class InstrPostProcess; 64 class InstrumentManager; 65 struct SourceMgr; 66 } // namespace mca 67 68 LLVM_ABI MCStreamer *createNullStreamer(MCContext &Ctx); 69 // Takes ownership of \p TAB and \p CE. 70 71 /// Create a machine code streamer which will print out assembly for the native 72 /// target, suitable for compiling with a native assembler. 73 /// 74 /// \param InstPrint - If given, the instruction printer to use. If not given 75 /// the MCInst representation will be printed. This method takes ownership of 76 /// InstPrint. 77 /// 78 /// \param CE - If given, a code emitter to use to show the instruction 79 /// encoding inline with the assembly. This method takes ownership of \p CE. 80 /// 81 /// \param TAB - If given, a target asm backend to use to show the fixup 82 /// information in conjunction with encoding information. This method takes 83 /// ownership of \p TAB. 84 /// 85 /// \param ShowInst - Whether to show the MCInst representation inline with 86 /// the assembly. 87 LLVM_ABI MCStreamer * 88 createAsmStreamer(MCContext &Ctx, std::unique_ptr<formatted_raw_ostream> OS, 89 std::unique_ptr<MCInstPrinter> InstPrint, 90 std::unique_ptr<MCCodeEmitter> CE, 91 std::unique_ptr<MCAsmBackend> TAB); 92 93 LLVM_ABI MCStreamer *createELFStreamer(MCContext &Ctx, 94 std::unique_ptr<MCAsmBackend> &&TAB, 95 std::unique_ptr<MCObjectWriter> &&OW, 96 std::unique_ptr<MCCodeEmitter> &&CE); 97 LLVM_ABI MCStreamer *createGOFFStreamer(MCContext &Ctx, 98 std::unique_ptr<MCAsmBackend> &&TAB, 99 std::unique_ptr<MCObjectWriter> &&OW, 100 std::unique_ptr<MCCodeEmitter> &&CE); 101 LLVM_ABI MCStreamer *createMachOStreamer(MCContext &Ctx, 102 std::unique_ptr<MCAsmBackend> &&TAB, 103 std::unique_ptr<MCObjectWriter> &&OW, 104 std::unique_ptr<MCCodeEmitter> &&CE, 105 bool DWARFMustBeAtTheEnd, 106 bool LabelSections = false); 107 LLVM_ABI MCStreamer *createWasmStreamer(MCContext &Ctx, 108 std::unique_ptr<MCAsmBackend> &&TAB, 109 std::unique_ptr<MCObjectWriter> &&OW, 110 std::unique_ptr<MCCodeEmitter> &&CE); 111 LLVM_ABI MCStreamer *createSPIRVStreamer(MCContext &Ctx, 112 std::unique_ptr<MCAsmBackend> &&TAB, 113 std::unique_ptr<MCObjectWriter> &&OW, 114 std::unique_ptr<MCCodeEmitter> &&CE); 115 LLVM_ABI MCStreamer * 116 createDXContainerStreamer(MCContext &Ctx, std::unique_ptr<MCAsmBackend> &&TAB, 117 std::unique_ptr<MCObjectWriter> &&OW, 118 std::unique_ptr<MCCodeEmitter> &&CE); 119 120 LLVM_ABI MCRelocationInfo *createMCRelocationInfo(const Triple &TT, 121 MCContext &Ctx); 122 123 LLVM_ABI MCSymbolizer * 124 createMCSymbolizer(const Triple &TT, LLVMOpInfoCallback GetOpInfo, 125 LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo, 126 MCContext *Ctx, std::unique_ptr<MCRelocationInfo> &&RelInfo); 127 128 LLVM_ABI mca::CustomBehaviour * 129 createCustomBehaviour(const MCSubtargetInfo &STI, const mca::SourceMgr &SrcMgr, 130 const MCInstrInfo &MCII); 131 132 LLVM_ABI mca::InstrPostProcess * 133 createInstrPostProcess(const MCSubtargetInfo &STI, const MCInstrInfo &MCII); 134 135 LLVM_ABI mca::InstrumentManager * 136 createInstrumentManager(const MCSubtargetInfo &STI, const MCInstrInfo &MCII); 137 138 /// Target - Wrapper for Target specific information. 139 /// 140 /// For registration purposes, this is a POD type so that targets can be 141 /// registered without the use of static constructors. 142 /// 143 /// Targets should implement a single global instance of this class (which 144 /// will be zero initialized), and pass that instance to the TargetRegistry as 145 /// part of their initialization. 146 class Target { 147 public: 148 friend struct TargetRegistry; 149 150 using ArchMatchFnTy = bool (*)(Triple::ArchType Arch); 151 152 using MCAsmInfoCtorFnTy = MCAsmInfo *(*)(const MCRegisterInfo &MRI, 153 const Triple &TT, 154 const MCTargetOptions &Options); 155 using MCObjectFileInfoCtorFnTy = MCObjectFileInfo *(*)(MCContext &Ctx, 156 bool PIC, 157 bool LargeCodeModel); 158 using MCInstrInfoCtorFnTy = MCInstrInfo *(*)(); 159 using MCInstrAnalysisCtorFnTy = MCInstrAnalysis *(*)(const MCInstrInfo *Info); 160 using MCRegInfoCtorFnTy = MCRegisterInfo *(*)(const Triple &TT); 161 using MCSubtargetInfoCtorFnTy = MCSubtargetInfo *(*)(const Triple &TT, 162 StringRef CPU, 163 StringRef Features); 164 using TargetMachineCtorTy = TargetMachine 165 *(*)(const Target &T, const Triple &TT, StringRef CPU, StringRef Features, 166 const TargetOptions &Options, std::optional<Reloc::Model> RM, 167 std::optional<CodeModel::Model> CM, CodeGenOptLevel OL, bool JIT); 168 // If it weren't for layering issues (this header is in llvm/Support, but 169 // depends on MC?) this should take the Streamer by value rather than rvalue 170 // reference. 171 using AsmPrinterCtorTy = AsmPrinter *(*)( 172 TargetMachine &TM, std::unique_ptr<MCStreamer> &&Streamer); 173 using MCAsmBackendCtorTy = MCAsmBackend *(*)(const Target &T, 174 const MCSubtargetInfo &STI, 175 const MCRegisterInfo &MRI, 176 const MCTargetOptions &Options); 177 using MCAsmParserCtorTy = MCTargetAsmParser *(*)( 178 const MCSubtargetInfo &STI, MCAsmParser &P, const MCInstrInfo &MII, 179 const MCTargetOptions &Options); 180 using MCDisassemblerCtorTy = MCDisassembler *(*)(const Target &T, 181 const MCSubtargetInfo &STI, 182 MCContext &Ctx); 183 using MCInstPrinterCtorTy = MCInstPrinter *(*)(const Triple &T, 184 unsigned SyntaxVariant, 185 const MCAsmInfo &MAI, 186 const MCInstrInfo &MII, 187 const MCRegisterInfo &MRI); 188 using MCCodeEmitterCtorTy = MCCodeEmitter *(*)(const MCInstrInfo &II, 189 MCContext &Ctx); 190 using ELFStreamerCtorTy = 191 MCStreamer *(*)(const Triple &T, MCContext &Ctx, 192 std::unique_ptr<MCAsmBackend> &&TAB, 193 std::unique_ptr<MCObjectWriter> &&OW, 194 std::unique_ptr<MCCodeEmitter> &&Emitter); 195 using MachOStreamerCtorTy = 196 MCStreamer *(*)(MCContext &Ctx, std::unique_ptr<MCAsmBackend> &&TAB, 197 std::unique_ptr<MCObjectWriter> &&OW, 198 std::unique_ptr<MCCodeEmitter> &&Emitter); 199 using COFFStreamerCtorTy = 200 MCStreamer *(*)(MCContext &Ctx, std::unique_ptr<MCAsmBackend> &&TAB, 201 std::unique_ptr<MCObjectWriter> &&OW, 202 std::unique_ptr<MCCodeEmitter> &&Emitter); 203 using XCOFFStreamerCtorTy = 204 MCStreamer *(*)(const Triple &T, MCContext &Ctx, 205 std::unique_ptr<MCAsmBackend> &&TAB, 206 std::unique_ptr<MCObjectWriter> &&OW, 207 std::unique_ptr<MCCodeEmitter> &&Emitter); 208 209 using NullTargetStreamerCtorTy = MCTargetStreamer *(*)(MCStreamer &S); 210 using AsmTargetStreamerCtorTy = 211 MCTargetStreamer *(*)(MCStreamer &S, formatted_raw_ostream &OS, 212 MCInstPrinter *InstPrint); 213 using AsmStreamerCtorTy = MCStreamer 214 *(*)(MCContext & Ctx, std::unique_ptr<formatted_raw_ostream> OS, 215 std::unique_ptr<MCInstPrinter> IP, std::unique_ptr<MCCodeEmitter> CE, 216 std::unique_ptr<MCAsmBackend> TAB); 217 using ObjectTargetStreamerCtorTy = 218 MCTargetStreamer *(*)(MCStreamer &S, const MCSubtargetInfo &STI); 219 using MCRelocationInfoCtorTy = MCRelocationInfo *(*)(const Triple &TT, 220 MCContext &Ctx); 221 using MCSymbolizerCtorTy = 222 MCSymbolizer *(*)(const Triple &TT, LLVMOpInfoCallback GetOpInfo, 223 LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo, 224 MCContext *Ctx, 225 std::unique_ptr<MCRelocationInfo> &&RelInfo); 226 227 using CustomBehaviourCtorTy = 228 mca::CustomBehaviour *(*)(const MCSubtargetInfo &STI, 229 const mca::SourceMgr &SrcMgr, 230 const MCInstrInfo &MCII); 231 232 using InstrPostProcessCtorTy = 233 mca::InstrPostProcess *(*)(const MCSubtargetInfo &STI, 234 const MCInstrInfo &MCII); 235 236 using InstrumentManagerCtorTy = 237 mca::InstrumentManager *(*)(const MCSubtargetInfo &STI, 238 const MCInstrInfo &MCII); 239 240 private: 241 /// Next - The next registered target in the linked list, maintained by the 242 /// TargetRegistry. 243 Target *Next; 244 245 /// The target function for checking if an architecture is supported. 246 ArchMatchFnTy ArchMatchFn; 247 248 /// Name - The target name. 249 const char *Name; 250 251 /// ShortDesc - A short description of the target. 252 const char *ShortDesc; 253 254 /// BackendName - The name of the backend implementation. This must match the 255 /// name of the 'def X : Target ...' in TableGen. 256 const char *BackendName; 257 258 /// HasJIT - Whether this target supports the JIT. 259 bool HasJIT; 260 261 /// MCAsmInfoCtorFn - Constructor function for this target's MCAsmInfo, if 262 /// registered. 263 MCAsmInfoCtorFnTy MCAsmInfoCtorFn; 264 265 /// Constructor function for this target's MCObjectFileInfo, if registered. 266 MCObjectFileInfoCtorFnTy MCObjectFileInfoCtorFn; 267 268 /// MCInstrInfoCtorFn - Constructor function for this target's MCInstrInfo, 269 /// if registered. 270 MCInstrInfoCtorFnTy MCInstrInfoCtorFn; 271 272 /// MCInstrAnalysisCtorFn - Constructor function for this target's 273 /// MCInstrAnalysis, if registered. 274 MCInstrAnalysisCtorFnTy MCInstrAnalysisCtorFn; 275 276 /// MCRegInfoCtorFn - Constructor function for this target's MCRegisterInfo, 277 /// if registered. 278 MCRegInfoCtorFnTy MCRegInfoCtorFn; 279 280 /// MCSubtargetInfoCtorFn - Constructor function for this target's 281 /// MCSubtargetInfo, if registered. 282 MCSubtargetInfoCtorFnTy MCSubtargetInfoCtorFn; 283 284 /// TargetMachineCtorFn - Construction function for this target's 285 /// TargetMachine, if registered. 286 TargetMachineCtorTy TargetMachineCtorFn; 287 288 /// MCAsmBackendCtorFn - Construction function for this target's 289 /// MCAsmBackend, if registered. 290 MCAsmBackendCtorTy MCAsmBackendCtorFn; 291 292 /// MCAsmParserCtorFn - Construction function for this target's 293 /// MCTargetAsmParser, if registered. 294 MCAsmParserCtorTy MCAsmParserCtorFn; 295 296 /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter, 297 /// if registered. 298 AsmPrinterCtorTy AsmPrinterCtorFn; 299 300 /// MCDisassemblerCtorFn - Construction function for this target's 301 /// MCDisassembler, if registered. 302 MCDisassemblerCtorTy MCDisassemblerCtorFn; 303 304 /// MCInstPrinterCtorFn - Construction function for this target's 305 /// MCInstPrinter, if registered. 306 MCInstPrinterCtorTy MCInstPrinterCtorFn; 307 308 /// MCCodeEmitterCtorFn - Construction function for this target's 309 /// CodeEmitter, if registered. 310 MCCodeEmitterCtorTy MCCodeEmitterCtorFn; 311 312 // Construction functions for the various object formats, if registered. 313 COFFStreamerCtorTy COFFStreamerCtorFn = nullptr; 314 MachOStreamerCtorTy MachOStreamerCtorFn = nullptr; 315 ELFStreamerCtorTy ELFStreamerCtorFn = nullptr; 316 XCOFFStreamerCtorTy XCOFFStreamerCtorFn = nullptr; 317 318 /// Construction function for this target's null TargetStreamer, if 319 /// registered (default = nullptr). 320 NullTargetStreamerCtorTy NullTargetStreamerCtorFn = nullptr; 321 322 /// Construction function for this target's asm TargetStreamer, if 323 /// registered (default = nullptr). 324 AsmTargetStreamerCtorTy AsmTargetStreamerCtorFn = nullptr; 325 326 /// Construction function for this target's AsmStreamer, if 327 /// registered (default = nullptr). 328 AsmStreamerCtorTy AsmStreamerCtorFn = nullptr; 329 330 /// Construction function for this target's obj TargetStreamer, if 331 /// registered (default = nullptr). 332 ObjectTargetStreamerCtorTy ObjectTargetStreamerCtorFn = nullptr; 333 334 /// MCRelocationInfoCtorFn - Construction function for this target's 335 /// MCRelocationInfo, if registered (default = llvm::createMCRelocationInfo) 336 MCRelocationInfoCtorTy MCRelocationInfoCtorFn = nullptr; 337 338 /// MCSymbolizerCtorFn - Construction function for this target's 339 /// MCSymbolizer, if registered (default = llvm::createMCSymbolizer) 340 MCSymbolizerCtorTy MCSymbolizerCtorFn = nullptr; 341 342 /// CustomBehaviourCtorFn - Construction function for this target's 343 /// CustomBehaviour, if registered (default = nullptr). 344 CustomBehaviourCtorTy CustomBehaviourCtorFn = nullptr; 345 346 /// InstrPostProcessCtorFn - Construction function for this target's 347 /// InstrPostProcess, if registered (default = nullptr). 348 InstrPostProcessCtorTy InstrPostProcessCtorFn = nullptr; 349 350 /// InstrumentManagerCtorFn - Construction function for this target's 351 /// InstrumentManager, if registered (default = nullptr). 352 InstrumentManagerCtorTy InstrumentManagerCtorFn = nullptr; 353 354 public: 355 Target() = default; 356 357 /// @name Target Information 358 /// @{ 359 360 // getNext - Return the next registered target. getNext()361 const Target *getNext() const { return Next; } 362 363 /// getName - Get the target name. getName()364 const char *getName() const { return Name; } 365 366 /// getShortDescription - Get a short description of the target. getShortDescription()367 const char *getShortDescription() const { return ShortDesc; } 368 369 /// getBackendName - Get the backend name. getBackendName()370 const char *getBackendName() const { return BackendName; } 371 372 /// @} 373 /// @name Feature Predicates 374 /// @{ 375 376 /// hasJIT - Check if this targets supports the just-in-time compilation. hasJIT()377 bool hasJIT() const { return HasJIT; } 378 379 /// hasTargetMachine - Check if this target supports code generation. hasTargetMachine()380 bool hasTargetMachine() const { return TargetMachineCtorFn != nullptr; } 381 382 /// hasMCAsmBackend - Check if this target supports .o generation. hasMCAsmBackend()383 bool hasMCAsmBackend() const { return MCAsmBackendCtorFn != nullptr; } 384 385 /// hasMCAsmParser - Check if this target supports assembly parsing. hasMCAsmParser()386 bool hasMCAsmParser() const { return MCAsmParserCtorFn != nullptr; } 387 388 /// @} 389 /// @name Feature Constructors 390 /// @{ 391 392 /// createMCAsmInfo - Create a MCAsmInfo implementation for the specified 393 /// target triple. 394 /// 395 /// \param TheTriple This argument is used to determine the target machine 396 /// feature set; it should always be provided. Generally this should be 397 /// either the target triple from the module, or the target triple of the 398 /// host if that does not exist. createMCAsmInfo(const MCRegisterInfo & MRI,StringRef TheTriple,const MCTargetOptions & Options)399 MCAsmInfo *createMCAsmInfo(const MCRegisterInfo &MRI, StringRef TheTriple, 400 const MCTargetOptions &Options) const { 401 if (!MCAsmInfoCtorFn) 402 return nullptr; 403 return MCAsmInfoCtorFn(MRI, Triple(TheTriple), Options); 404 } 405 406 /// Create a MCObjectFileInfo implementation for the specified target 407 /// triple. 408 /// 409 MCObjectFileInfo *createMCObjectFileInfo(MCContext &Ctx, bool PIC, 410 bool LargeCodeModel = false) const { 411 if (!MCObjectFileInfoCtorFn) { 412 MCObjectFileInfo *MOFI = new MCObjectFileInfo(); 413 MOFI->initMCObjectFileInfo(Ctx, PIC, LargeCodeModel); 414 return MOFI; 415 } 416 return MCObjectFileInfoCtorFn(Ctx, PIC, LargeCodeModel); 417 } 418 419 /// createMCInstrInfo - Create a MCInstrInfo implementation. 420 /// createMCInstrInfo()421 MCInstrInfo *createMCInstrInfo() const { 422 if (!MCInstrInfoCtorFn) 423 return nullptr; 424 return MCInstrInfoCtorFn(); 425 } 426 427 /// createMCInstrAnalysis - Create a MCInstrAnalysis implementation. 428 /// createMCInstrAnalysis(const MCInstrInfo * Info)429 MCInstrAnalysis *createMCInstrAnalysis(const MCInstrInfo *Info) const { 430 if (!MCInstrAnalysisCtorFn) 431 return nullptr; 432 return MCInstrAnalysisCtorFn(Info); 433 } 434 435 /// createMCRegInfo - Create a MCRegisterInfo implementation. 436 /// createMCRegInfo(StringRef TT)437 MCRegisterInfo *createMCRegInfo(StringRef TT) const { 438 if (!MCRegInfoCtorFn) 439 return nullptr; 440 return MCRegInfoCtorFn(Triple(TT)); 441 } 442 443 /// createMCSubtargetInfo - Create a MCSubtargetInfo implementation. 444 /// 445 /// \param TheTriple This argument is used to determine the target machine 446 /// feature set; it should always be provided. Generally this should be 447 /// either the target triple from the module, or the target triple of the 448 /// host if that does not exist. 449 /// \param CPU This specifies the name of the target CPU. 450 /// \param Features This specifies the string representation of the 451 /// additional target features. createMCSubtargetInfo(StringRef TheTriple,StringRef CPU,StringRef Features)452 MCSubtargetInfo *createMCSubtargetInfo(StringRef TheTriple, StringRef CPU, 453 StringRef Features) const { 454 if (!MCSubtargetInfoCtorFn) 455 return nullptr; 456 return MCSubtargetInfoCtorFn(Triple(TheTriple), CPU, Features); 457 } 458 459 /// createTargetMachine - Create a target specific machine implementation 460 /// for the specified \p Triple. 461 /// 462 /// \param TT This argument is used to determine the target machine 463 /// feature set; it should always be provided. Generally this should be 464 /// either the target triple from the module, or the target triple of the 465 /// host if that does not exist. 466 TargetMachine *createTargetMachine( 467 const Triple &TT, StringRef CPU, StringRef Features, 468 const TargetOptions &Options, std::optional<Reloc::Model> RM, 469 std::optional<CodeModel::Model> CM = std::nullopt, 470 CodeGenOptLevel OL = CodeGenOptLevel::Default, bool JIT = false) const { 471 if (!TargetMachineCtorFn) 472 return nullptr; 473 return TargetMachineCtorFn(*this, TT, CPU, Features, Options, RM, CM, OL, 474 JIT); 475 } 476 477 [[deprecated("Use overload accepting Triple instead")]] 478 TargetMachine *createTargetMachine( 479 StringRef TT, StringRef CPU, StringRef Features, 480 const TargetOptions &Options, std::optional<Reloc::Model> RM, 481 std::optional<CodeModel::Model> CM = std::nullopt, 482 CodeGenOptLevel OL = CodeGenOptLevel::Default, bool JIT = false) const { 483 return createTargetMachine(Triple(TT), CPU, Features, Options, RM, CM, OL, 484 JIT); 485 } 486 487 /// createMCAsmBackend - Create a target specific assembly parser. createMCAsmBackend(const MCSubtargetInfo & STI,const MCRegisterInfo & MRI,const MCTargetOptions & Options)488 MCAsmBackend *createMCAsmBackend(const MCSubtargetInfo &STI, 489 const MCRegisterInfo &MRI, 490 const MCTargetOptions &Options) const { 491 if (!MCAsmBackendCtorFn) 492 return nullptr; 493 return MCAsmBackendCtorFn(*this, STI, MRI, Options); 494 } 495 496 /// createMCAsmParser - Create a target specific assembly parser. 497 /// 498 /// \param Parser The target independent parser implementation to use for 499 /// parsing and lexing. createMCAsmParser(const MCSubtargetInfo & STI,MCAsmParser & Parser,const MCInstrInfo & MII,const MCTargetOptions & Options)500 MCTargetAsmParser *createMCAsmParser(const MCSubtargetInfo &STI, 501 MCAsmParser &Parser, 502 const MCInstrInfo &MII, 503 const MCTargetOptions &Options) const { 504 if (!MCAsmParserCtorFn) 505 return nullptr; 506 return MCAsmParserCtorFn(STI, Parser, MII, Options); 507 } 508 509 /// createAsmPrinter - Create a target specific assembly printer pass. This 510 /// takes ownership of the MCStreamer object. createAsmPrinter(TargetMachine & TM,std::unique_ptr<MCStreamer> && Streamer)511 AsmPrinter *createAsmPrinter(TargetMachine &TM, 512 std::unique_ptr<MCStreamer> &&Streamer) const { 513 if (!AsmPrinterCtorFn) 514 return nullptr; 515 return AsmPrinterCtorFn(TM, std::move(Streamer)); 516 } 517 createMCDisassembler(const MCSubtargetInfo & STI,MCContext & Ctx)518 MCDisassembler *createMCDisassembler(const MCSubtargetInfo &STI, 519 MCContext &Ctx) const { 520 if (!MCDisassemblerCtorFn) 521 return nullptr; 522 return MCDisassemblerCtorFn(*this, STI, Ctx); 523 } 524 createMCInstPrinter(const Triple & T,unsigned SyntaxVariant,const MCAsmInfo & MAI,const MCInstrInfo & MII,const MCRegisterInfo & MRI)525 MCInstPrinter *createMCInstPrinter(const Triple &T, unsigned SyntaxVariant, 526 const MCAsmInfo &MAI, 527 const MCInstrInfo &MII, 528 const MCRegisterInfo &MRI) const { 529 if (!MCInstPrinterCtorFn) 530 return nullptr; 531 return MCInstPrinterCtorFn(T, SyntaxVariant, MAI, MII, MRI); 532 } 533 534 /// createMCCodeEmitter - Create a target specific code emitter. createMCCodeEmitter(const MCInstrInfo & II,MCContext & Ctx)535 MCCodeEmitter *createMCCodeEmitter(const MCInstrInfo &II, 536 MCContext &Ctx) const { 537 if (!MCCodeEmitterCtorFn) 538 return nullptr; 539 return MCCodeEmitterCtorFn(II, Ctx); 540 } 541 542 /// Create a target specific MCStreamer. 543 /// 544 /// \param T The target triple. 545 /// \param Ctx The target context. 546 /// \param TAB The target assembler backend object. Takes ownership. 547 /// \param OW The stream object. 548 /// \param Emitter The target independent assembler object.Takes ownership. 549 LLVM_ABI MCStreamer *createMCObjectStreamer( 550 const Triple &T, MCContext &Ctx, std::unique_ptr<MCAsmBackend> TAB, 551 std::unique_ptr<MCObjectWriter> OW, 552 std::unique_ptr<MCCodeEmitter> Emitter, const MCSubtargetInfo &STI) const; 553 554 LLVM_ABI MCStreamer * 555 createAsmStreamer(MCContext &Ctx, std::unique_ptr<formatted_raw_ostream> OS, 556 std::unique_ptr<MCInstPrinter> IP, 557 std::unique_ptr<MCCodeEmitter> CE, 558 std::unique_ptr<MCAsmBackend> TAB) const; 559 createAsmTargetStreamer(MCStreamer & S,formatted_raw_ostream & OS,MCInstPrinter * InstPrint)560 MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S, 561 formatted_raw_ostream &OS, 562 MCInstPrinter *InstPrint) const { 563 if (AsmTargetStreamerCtorFn) 564 return AsmTargetStreamerCtorFn(S, OS, InstPrint); 565 return nullptr; 566 } 567 createNullStreamer(MCContext & Ctx)568 MCStreamer *createNullStreamer(MCContext &Ctx) const { 569 MCStreamer *S = llvm::createNullStreamer(Ctx); 570 createNullTargetStreamer(*S); 571 return S; 572 } 573 createNullTargetStreamer(MCStreamer & S)574 MCTargetStreamer *createNullTargetStreamer(MCStreamer &S) const { 575 if (NullTargetStreamerCtorFn) 576 return NullTargetStreamerCtorFn(S); 577 return nullptr; 578 } 579 580 /// createMCRelocationInfo - Create a target specific MCRelocationInfo. 581 /// 582 /// \param TT The target triple. 583 /// \param Ctx The target context. createMCRelocationInfo(StringRef TT,MCContext & Ctx)584 MCRelocationInfo *createMCRelocationInfo(StringRef TT, MCContext &Ctx) const { 585 MCRelocationInfoCtorTy Fn = MCRelocationInfoCtorFn 586 ? MCRelocationInfoCtorFn 587 : llvm::createMCRelocationInfo; 588 return Fn(Triple(TT), Ctx); 589 } 590 591 /// createMCSymbolizer - Create a target specific MCSymbolizer. 592 /// 593 /// \param TT The target triple. 594 /// \param GetOpInfo The function to get the symbolic information for 595 /// operands. 596 /// \param SymbolLookUp The function to lookup a symbol name. 597 /// \param DisInfo The pointer to the block of symbolic information for above 598 /// call 599 /// back. 600 /// \param Ctx The target context. 601 /// \param RelInfo The relocation information for this target. Takes 602 /// ownership. 603 MCSymbolizer * createMCSymbolizer(StringRef TT,LLVMOpInfoCallback GetOpInfo,LLVMSymbolLookupCallback SymbolLookUp,void * DisInfo,MCContext * Ctx,std::unique_ptr<MCRelocationInfo> && RelInfo)604 createMCSymbolizer(StringRef TT, LLVMOpInfoCallback GetOpInfo, 605 LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo, 606 MCContext *Ctx, 607 std::unique_ptr<MCRelocationInfo> &&RelInfo) const { 608 MCSymbolizerCtorTy Fn = 609 MCSymbolizerCtorFn ? MCSymbolizerCtorFn : llvm::createMCSymbolizer; 610 return Fn(Triple(TT), GetOpInfo, SymbolLookUp, DisInfo, Ctx, 611 std::move(RelInfo)); 612 } 613 614 /// createCustomBehaviour - Create a target specific CustomBehaviour. 615 /// This class is used by llvm-mca and requires backend functionality. createCustomBehaviour(const MCSubtargetInfo & STI,const mca::SourceMgr & SrcMgr,const MCInstrInfo & MCII)616 mca::CustomBehaviour *createCustomBehaviour(const MCSubtargetInfo &STI, 617 const mca::SourceMgr &SrcMgr, 618 const MCInstrInfo &MCII) const { 619 if (CustomBehaviourCtorFn) 620 return CustomBehaviourCtorFn(STI, SrcMgr, MCII); 621 return nullptr; 622 } 623 624 /// createInstrPostProcess - Create a target specific InstrPostProcess. 625 /// This class is used by llvm-mca and requires backend functionality. createInstrPostProcess(const MCSubtargetInfo & STI,const MCInstrInfo & MCII)626 mca::InstrPostProcess *createInstrPostProcess(const MCSubtargetInfo &STI, 627 const MCInstrInfo &MCII) const { 628 if (InstrPostProcessCtorFn) 629 return InstrPostProcessCtorFn(STI, MCII); 630 return nullptr; 631 } 632 633 /// createInstrumentManager - Create a target specific 634 /// InstrumentManager. This class is used by llvm-mca and requires 635 /// backend functionality. 636 mca::InstrumentManager * createInstrumentManager(const MCSubtargetInfo & STI,const MCInstrInfo & MCII)637 createInstrumentManager(const MCSubtargetInfo &STI, 638 const MCInstrInfo &MCII) const { 639 if (InstrumentManagerCtorFn) 640 return InstrumentManagerCtorFn(STI, MCII); 641 return nullptr; 642 } 643 644 /// @} 645 }; 646 647 /// TargetRegistry - Generic interface to target specific features. 648 struct TargetRegistry { 649 // FIXME: Make this a namespace, probably just move all the Register* 650 // functions into Target (currently they all just set members on the Target 651 // anyway, and Target friends this class so those functions can... 652 // function). 653 TargetRegistry() = delete; 654 655 class iterator { 656 friend struct TargetRegistry; 657 658 const Target *Current = nullptr; 659 iteratorTargetRegistry660 explicit iterator(Target *T) : Current(T) {} 661 662 public: 663 using iterator_category = std::forward_iterator_tag; 664 using value_type = Target; 665 using difference_type = std::ptrdiff_t; 666 using pointer = value_type *; 667 using reference = value_type &; 668 669 iterator() = default; 670 671 bool operator==(const iterator &x) const { return Current == x.Current; } 672 bool operator!=(const iterator &x) const { return !operator==(x); } 673 674 // Iterator traversal: forward iteration only 675 iterator &operator++() { // Preincrement 676 assert(Current && "Cannot increment end iterator!"); 677 Current = Current->getNext(); 678 return *this; 679 } 680 iterator operator++(int) { // Postincrement 681 iterator tmp = *this; 682 ++*this; 683 return tmp; 684 } 685 686 const Target &operator*() const { 687 assert(Current && "Cannot dereference end iterator!"); 688 return *Current; 689 } 690 691 const Target *operator->() const { return &operator*(); } 692 }; 693 694 /// printRegisteredTargetsForVersion - Print the registered targets 695 /// appropriately for inclusion in a tool's version output. 696 LLVM_ABI static void printRegisteredTargetsForVersion(raw_ostream &OS); 697 698 /// @name Registry Access 699 /// @{ 700 701 LLVM_ABI static iterator_range<iterator> targets(); 702 703 /// lookupTarget - Lookup a target based on a target triple. 704 /// 705 /// \param TripleStr - The triple to use for finding a target. 706 /// \param Error - On failure, an error string describing why no target was 707 /// found. 708 // TODO: Drop this in favor of the method accepting Triple. lookupTargetTargetRegistry709 static const Target *lookupTarget(StringRef TripleStr, std::string &Error) { 710 return lookupTarget(Triple(TripleStr), Error); 711 } 712 713 /// lookupTarget - Lookup a target based on a target triple. 714 /// 715 /// \param Triple - The triple to use for finding a target. 716 /// \param Error - On failure, an error string describing why no target was 717 /// found. 718 LLVM_ABI static const Target *lookupTarget(const Triple &TheTriple, 719 std::string &Error); 720 721 /// lookupTarget - Lookup a target based on an architecture name 722 /// and a target triple. If the architecture name is non-empty, 723 /// then the lookup is done by architecture. Otherwise, the target 724 /// triple is used. 725 /// 726 /// \param ArchName - The architecture to use for finding a target. 727 /// \param TheTriple - The triple to use for finding a target. The 728 /// triple is updated with canonical architecture name if a lookup 729 /// by architecture is done. 730 /// \param Error - On failure, an error string describing why no target was 731 /// found. 732 LLVM_ABI static const Target * 733 lookupTarget(StringRef ArchName, Triple &TheTriple, std::string &Error); 734 735 /// @} 736 /// @name Target Registration 737 /// @{ 738 739 /// RegisterTarget - Register the given target. Attempts to register a 740 /// target which has already been registered will be ignored. 741 /// 742 /// Clients are responsible for ensuring that registration doesn't occur 743 /// while another thread is attempting to access the registry. Typically 744 /// this is done by initializing all targets at program startup. 745 /// 746 /// @param T - The target being registered. 747 /// @param Name - The target name. This should be a static string. 748 /// @param ShortDesc - A short target description. This should be a static 749 /// string. 750 /// @param BackendName - The name of the backend. This should be a static 751 /// string that is the same for all targets that share a backend 752 /// implementation and must match the name used in the 'def X : Target ...' in 753 /// TableGen. 754 /// @param ArchMatchFn - The arch match checking function for this target. 755 /// @param HasJIT - Whether the target supports JIT code 756 /// generation. 757 LLVM_ABI static void RegisterTarget(Target &T, const char *Name, 758 const char *ShortDesc, 759 const char *BackendName, 760 Target::ArchMatchFnTy ArchMatchFn, 761 bool HasJIT = false); 762 763 /// RegisterMCAsmInfo - Register a MCAsmInfo implementation for the 764 /// given target. 765 /// 766 /// Clients are responsible for ensuring that registration doesn't occur 767 /// while another thread is attempting to access the registry. Typically 768 /// this is done by initializing all targets at program startup. 769 /// 770 /// @param T - The target being registered. 771 /// @param Fn - A function to construct a MCAsmInfo for the target. RegisterMCAsmInfoTargetRegistry772 static void RegisterMCAsmInfo(Target &T, Target::MCAsmInfoCtorFnTy Fn) { 773 T.MCAsmInfoCtorFn = Fn; 774 } 775 776 /// Register a MCObjectFileInfo implementation for the given target. 777 /// 778 /// Clients are responsible for ensuring that registration doesn't occur 779 /// while another thread is attempting to access the registry. Typically 780 /// this is done by initializing all targets at program startup. 781 /// 782 /// @param T - The target being registered. 783 /// @param Fn - A function to construct a MCObjectFileInfo for the target. RegisterMCObjectFileInfoTargetRegistry784 static void RegisterMCObjectFileInfo(Target &T, 785 Target::MCObjectFileInfoCtorFnTy Fn) { 786 T.MCObjectFileInfoCtorFn = Fn; 787 } 788 789 /// RegisterMCInstrInfo - Register a MCInstrInfo implementation for the 790 /// given target. 791 /// 792 /// Clients are responsible for ensuring that registration doesn't occur 793 /// while another thread is attempting to access the registry. Typically 794 /// this is done by initializing all targets at program startup. 795 /// 796 /// @param T - The target being registered. 797 /// @param Fn - A function to construct a MCInstrInfo for the target. RegisterMCInstrInfoTargetRegistry798 static void RegisterMCInstrInfo(Target &T, Target::MCInstrInfoCtorFnTy Fn) { 799 T.MCInstrInfoCtorFn = Fn; 800 } 801 802 /// RegisterMCInstrAnalysis - Register a MCInstrAnalysis implementation for 803 /// the given target. RegisterMCInstrAnalysisTargetRegistry804 static void RegisterMCInstrAnalysis(Target &T, 805 Target::MCInstrAnalysisCtorFnTy Fn) { 806 T.MCInstrAnalysisCtorFn = Fn; 807 } 808 809 /// RegisterMCRegInfo - Register a MCRegisterInfo implementation for the 810 /// given target. 811 /// 812 /// Clients are responsible for ensuring that registration doesn't occur 813 /// while another thread is attempting to access the registry. Typically 814 /// this is done by initializing all targets at program startup. 815 /// 816 /// @param T - The target being registered. 817 /// @param Fn - A function to construct a MCRegisterInfo for the target. RegisterMCRegInfoTargetRegistry818 static void RegisterMCRegInfo(Target &T, Target::MCRegInfoCtorFnTy Fn) { 819 T.MCRegInfoCtorFn = Fn; 820 } 821 822 /// RegisterMCSubtargetInfo - Register a MCSubtargetInfo implementation for 823 /// the given target. 824 /// 825 /// Clients are responsible for ensuring that registration doesn't occur 826 /// while another thread is attempting to access the registry. Typically 827 /// this is done by initializing all targets at program startup. 828 /// 829 /// @param T - The target being registered. 830 /// @param Fn - A function to construct a MCSubtargetInfo for the target. RegisterMCSubtargetInfoTargetRegistry831 static void RegisterMCSubtargetInfo(Target &T, 832 Target::MCSubtargetInfoCtorFnTy Fn) { 833 T.MCSubtargetInfoCtorFn = Fn; 834 } 835 836 /// RegisterTargetMachine - Register a TargetMachine implementation for the 837 /// given target. 838 /// 839 /// Clients are responsible for ensuring that registration doesn't occur 840 /// while another thread is attempting to access the registry. Typically 841 /// this is done by initializing all targets at program startup. 842 /// 843 /// @param T - The target being registered. 844 /// @param Fn - A function to construct a TargetMachine for the target. RegisterTargetMachineTargetRegistry845 static void RegisterTargetMachine(Target &T, Target::TargetMachineCtorTy Fn) { 846 T.TargetMachineCtorFn = Fn; 847 } 848 849 /// RegisterMCAsmBackend - Register a MCAsmBackend implementation for the 850 /// given target. 851 /// 852 /// Clients are responsible for ensuring that registration doesn't occur 853 /// while another thread is attempting to access the registry. Typically 854 /// this is done by initializing all targets at program startup. 855 /// 856 /// @param T - The target being registered. 857 /// @param Fn - A function to construct an AsmBackend for the target. RegisterMCAsmBackendTargetRegistry858 static void RegisterMCAsmBackend(Target &T, Target::MCAsmBackendCtorTy Fn) { 859 T.MCAsmBackendCtorFn = Fn; 860 } 861 862 /// RegisterMCAsmParser - Register a MCTargetAsmParser implementation for 863 /// the given target. 864 /// 865 /// Clients are responsible for ensuring that registration doesn't occur 866 /// while another thread is attempting to access the registry. Typically 867 /// this is done by initializing all targets at program startup. 868 /// 869 /// @param T - The target being registered. 870 /// @param Fn - A function to construct an MCTargetAsmParser for the target. RegisterMCAsmParserTargetRegistry871 static void RegisterMCAsmParser(Target &T, Target::MCAsmParserCtorTy Fn) { 872 T.MCAsmParserCtorFn = Fn; 873 } 874 875 /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given 876 /// target. 877 /// 878 /// Clients are responsible for ensuring that registration doesn't occur 879 /// while another thread is attempting to access the registry. Typically 880 /// this is done by initializing all targets at program startup. 881 /// 882 /// @param T - The target being registered. 883 /// @param Fn - A function to construct an AsmPrinter for the target. RegisterAsmPrinterTargetRegistry884 static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) { 885 T.AsmPrinterCtorFn = Fn; 886 } 887 888 /// RegisterMCDisassembler - Register a MCDisassembler implementation for 889 /// the given target. 890 /// 891 /// Clients are responsible for ensuring that registration doesn't occur 892 /// while another thread is attempting to access the registry. Typically 893 /// this is done by initializing all targets at program startup. 894 /// 895 /// @param T - The target being registered. 896 /// @param Fn - A function to construct an MCDisassembler for the target. RegisterMCDisassemblerTargetRegistry897 static void RegisterMCDisassembler(Target &T, 898 Target::MCDisassemblerCtorTy Fn) { 899 T.MCDisassemblerCtorFn = Fn; 900 } 901 902 /// RegisterMCInstPrinter - Register a MCInstPrinter implementation for the 903 /// given target. 904 /// 905 /// Clients are responsible for ensuring that registration doesn't occur 906 /// while another thread is attempting to access the registry. Typically 907 /// this is done by initializing all targets at program startup. 908 /// 909 /// @param T - The target being registered. 910 /// @param Fn - A function to construct an MCInstPrinter for the target. RegisterMCInstPrinterTargetRegistry911 static void RegisterMCInstPrinter(Target &T, Target::MCInstPrinterCtorTy Fn) { 912 T.MCInstPrinterCtorFn = Fn; 913 } 914 915 /// RegisterMCCodeEmitter - Register a MCCodeEmitter implementation for the 916 /// given target. 917 /// 918 /// Clients are responsible for ensuring that registration doesn't occur 919 /// while another thread is attempting to access the registry. Typically 920 /// this is done by initializing all targets at program startup. 921 /// 922 /// @param T - The target being registered. 923 /// @param Fn - A function to construct an MCCodeEmitter for the target. RegisterMCCodeEmitterTargetRegistry924 static void RegisterMCCodeEmitter(Target &T, Target::MCCodeEmitterCtorTy Fn) { 925 T.MCCodeEmitterCtorFn = Fn; 926 } 927 RegisterCOFFStreamerTargetRegistry928 static void RegisterCOFFStreamer(Target &T, Target::COFFStreamerCtorTy Fn) { 929 T.COFFStreamerCtorFn = Fn; 930 } 931 RegisterMachOStreamerTargetRegistry932 static void RegisterMachOStreamer(Target &T, Target::MachOStreamerCtorTy Fn) { 933 T.MachOStreamerCtorFn = Fn; 934 } 935 RegisterELFStreamerTargetRegistry936 static void RegisterELFStreamer(Target &T, Target::ELFStreamerCtorTy Fn) { 937 T.ELFStreamerCtorFn = Fn; 938 } 939 RegisterXCOFFStreamerTargetRegistry940 static void RegisterXCOFFStreamer(Target &T, Target::XCOFFStreamerCtorTy Fn) { 941 T.XCOFFStreamerCtorFn = Fn; 942 } 943 RegisterNullTargetStreamerTargetRegistry944 static void RegisterNullTargetStreamer(Target &T, 945 Target::NullTargetStreamerCtorTy Fn) { 946 T.NullTargetStreamerCtorFn = Fn; 947 } 948 RegisterAsmStreamerTargetRegistry949 static void RegisterAsmStreamer(Target &T, Target::AsmStreamerCtorTy Fn) { 950 T.AsmStreamerCtorFn = Fn; 951 } 952 RegisterAsmTargetStreamerTargetRegistry953 static void RegisterAsmTargetStreamer(Target &T, 954 Target::AsmTargetStreamerCtorTy Fn) { 955 T.AsmTargetStreamerCtorFn = Fn; 956 } 957 958 static void RegisterObjectTargetStreamerTargetRegistry959 RegisterObjectTargetStreamer(Target &T, 960 Target::ObjectTargetStreamerCtorTy Fn) { 961 T.ObjectTargetStreamerCtorFn = Fn; 962 } 963 964 /// RegisterMCRelocationInfo - Register an MCRelocationInfo 965 /// implementation for the given target. 966 /// 967 /// Clients are responsible for ensuring that registration doesn't occur 968 /// while another thread is attempting to access the registry. Typically 969 /// this is done by initializing all targets at program startup. 970 /// 971 /// @param T - The target being registered. 972 /// @param Fn - A function to construct an MCRelocationInfo for the target. RegisterMCRelocationInfoTargetRegistry973 static void RegisterMCRelocationInfo(Target &T, 974 Target::MCRelocationInfoCtorTy Fn) { 975 T.MCRelocationInfoCtorFn = Fn; 976 } 977 978 /// RegisterMCSymbolizer - Register an MCSymbolizer 979 /// implementation for the given target. 980 /// 981 /// Clients are responsible for ensuring that registration doesn't occur 982 /// while another thread is attempting to access the registry. Typically 983 /// this is done by initializing all targets at program startup. 984 /// 985 /// @param T - The target being registered. 986 /// @param Fn - A function to construct an MCSymbolizer for the target. RegisterMCSymbolizerTargetRegistry987 static void RegisterMCSymbolizer(Target &T, Target::MCSymbolizerCtorTy Fn) { 988 T.MCSymbolizerCtorFn = Fn; 989 } 990 991 /// RegisterCustomBehaviour - Register a CustomBehaviour 992 /// implementation for the given target. 993 /// 994 /// Clients are responsible for ensuring that registration doesn't occur 995 /// while another thread is attempting to access the registry. Typically 996 /// this is done by initializing all targets at program startup. 997 /// 998 /// @param T - The target being registered. 999 /// @param Fn - A function to construct a CustomBehaviour for the target. RegisterCustomBehaviourTargetRegistry1000 static void RegisterCustomBehaviour(Target &T, 1001 Target::CustomBehaviourCtorTy Fn) { 1002 T.CustomBehaviourCtorFn = Fn; 1003 } 1004 1005 /// RegisterInstrPostProcess - Register an InstrPostProcess 1006 /// implementation for the given target. 1007 /// 1008 /// Clients are responsible for ensuring that registration doesn't occur 1009 /// while another thread is attempting to access the registry. Typically 1010 /// this is done by initializing all targets at program startup. 1011 /// 1012 /// @param T - The target being registered. 1013 /// @param Fn - A function to construct an InstrPostProcess for the target. RegisterInstrPostProcessTargetRegistry1014 static void RegisterInstrPostProcess(Target &T, 1015 Target::InstrPostProcessCtorTy Fn) { 1016 T.InstrPostProcessCtorFn = Fn; 1017 } 1018 1019 /// RegisterInstrumentManager - Register an InstrumentManager 1020 /// implementation for the given target. 1021 /// 1022 /// Clients are responsible for ensuring that registration doesn't occur 1023 /// while another thread is attempting to access the registry. Typically 1024 /// this is done by initializing all targets at program startup. 1025 /// 1026 /// @param T - The target being registered. 1027 /// @param Fn - A function to construct an InstrumentManager for the 1028 /// target. RegisterInstrumentManagerTargetRegistry1029 static void RegisterInstrumentManager(Target &T, 1030 Target::InstrumentManagerCtorTy Fn) { 1031 T.InstrumentManagerCtorFn = Fn; 1032 } 1033 1034 /// @} 1035 }; 1036 1037 //===--------------------------------------------------------------------===// 1038 1039 /// RegisterTarget - Helper template for registering a target, for use in the 1040 /// target's initialization function. Usage: 1041 /// 1042 /// 1043 /// Target &getTheFooTarget() { // The global target instance. 1044 /// static Target TheFooTarget; 1045 /// return TheFooTarget; 1046 /// } 1047 /// extern "C" void LLVMInitializeFooTargetInfo() { 1048 /// RegisterTarget<Triple::foo> X(getTheFooTarget(), "foo", "Foo 1049 /// description", "Foo" /* Backend Name */); 1050 /// } 1051 template <Triple::ArchType TargetArchType = Triple::UnknownArch, 1052 bool HasJIT = false> 1053 struct RegisterTarget { RegisterTargetRegisterTarget1054 RegisterTarget(Target &T, const char *Name, const char *Desc, 1055 const char *BackendName) { 1056 TargetRegistry::RegisterTarget(T, Name, Desc, BackendName, &getArchMatch, 1057 HasJIT); 1058 } 1059 getArchMatchRegisterTarget1060 static bool getArchMatch(Triple::ArchType Arch) { 1061 return Arch == TargetArchType; 1062 } 1063 }; 1064 1065 /// RegisterMCAsmInfo - Helper template for registering a target assembly info 1066 /// implementation. This invokes the static "Create" method on the class to 1067 /// actually do the construction. Usage: 1068 /// 1069 /// extern "C" void LLVMInitializeFooTarget() { 1070 /// extern Target TheFooTarget; 1071 /// RegisterMCAsmInfo<FooMCAsmInfo> X(TheFooTarget); 1072 /// } 1073 template <class MCAsmInfoImpl> struct RegisterMCAsmInfo { RegisterMCAsmInfoRegisterMCAsmInfo1074 RegisterMCAsmInfo(Target &T) { 1075 TargetRegistry::RegisterMCAsmInfo(T, &Allocator); 1076 } 1077 1078 private: AllocatorRegisterMCAsmInfo1079 static MCAsmInfo *Allocator(const MCRegisterInfo & /*MRI*/, const Triple &TT, 1080 const MCTargetOptions &Options) { 1081 return new MCAsmInfoImpl(TT, Options); 1082 } 1083 }; 1084 1085 /// RegisterMCAsmInfoFn - Helper template for registering a target assembly info 1086 /// implementation. This invokes the specified function to do the 1087 /// construction. Usage: 1088 /// 1089 /// extern "C" void LLVMInitializeFooTarget() { 1090 /// extern Target TheFooTarget; 1091 /// RegisterMCAsmInfoFn X(TheFooTarget, TheFunction); 1092 /// } 1093 struct RegisterMCAsmInfoFn { RegisterMCAsmInfoFnRegisterMCAsmInfoFn1094 RegisterMCAsmInfoFn(Target &T, Target::MCAsmInfoCtorFnTy Fn) { 1095 TargetRegistry::RegisterMCAsmInfo(T, Fn); 1096 } 1097 }; 1098 1099 /// Helper template for registering a target object file info implementation. 1100 /// This invokes the static "Create" method on the class to actually do the 1101 /// construction. Usage: 1102 /// 1103 /// extern "C" void LLVMInitializeFooTarget() { 1104 /// extern Target TheFooTarget; 1105 /// RegisterMCObjectFileInfo<FooMCObjectFileInfo> X(TheFooTarget); 1106 /// } 1107 template <class MCObjectFileInfoImpl> struct RegisterMCObjectFileInfo { RegisterMCObjectFileInfoRegisterMCObjectFileInfo1108 RegisterMCObjectFileInfo(Target &T) { 1109 TargetRegistry::RegisterMCObjectFileInfo(T, &Allocator); 1110 } 1111 1112 private: 1113 static MCObjectFileInfo *Allocator(MCContext &Ctx, bool PIC, 1114 bool LargeCodeModel = false) { 1115 return new MCObjectFileInfoImpl(Ctx, PIC, LargeCodeModel); 1116 } 1117 }; 1118 1119 /// Helper template for registering a target object file info implementation. 1120 /// This invokes the specified function to do the construction. Usage: 1121 /// 1122 /// extern "C" void LLVMInitializeFooTarget() { 1123 /// extern Target TheFooTarget; 1124 /// RegisterMCObjectFileInfoFn X(TheFooTarget, TheFunction); 1125 /// } 1126 struct RegisterMCObjectFileInfoFn { RegisterMCObjectFileInfoFnRegisterMCObjectFileInfoFn1127 RegisterMCObjectFileInfoFn(Target &T, Target::MCObjectFileInfoCtorFnTy Fn) { 1128 TargetRegistry::RegisterMCObjectFileInfo(T, Fn); 1129 } 1130 }; 1131 1132 /// RegisterMCInstrInfo - Helper template for registering a target instruction 1133 /// info implementation. This invokes the static "Create" method on the class 1134 /// to actually do the construction. Usage: 1135 /// 1136 /// extern "C" void LLVMInitializeFooTarget() { 1137 /// extern Target TheFooTarget; 1138 /// RegisterMCInstrInfo<FooMCInstrInfo> X(TheFooTarget); 1139 /// } 1140 template <class MCInstrInfoImpl> struct RegisterMCInstrInfo { RegisterMCInstrInfoRegisterMCInstrInfo1141 RegisterMCInstrInfo(Target &T) { 1142 TargetRegistry::RegisterMCInstrInfo(T, &Allocator); 1143 } 1144 1145 private: AllocatorRegisterMCInstrInfo1146 static MCInstrInfo *Allocator() { return new MCInstrInfoImpl(); } 1147 }; 1148 1149 /// RegisterMCInstrInfoFn - Helper template for registering a target 1150 /// instruction info implementation. This invokes the specified function to 1151 /// do the construction. Usage: 1152 /// 1153 /// extern "C" void LLVMInitializeFooTarget() { 1154 /// extern Target TheFooTarget; 1155 /// RegisterMCInstrInfoFn X(TheFooTarget, TheFunction); 1156 /// } 1157 struct RegisterMCInstrInfoFn { RegisterMCInstrInfoFnRegisterMCInstrInfoFn1158 RegisterMCInstrInfoFn(Target &T, Target::MCInstrInfoCtorFnTy Fn) { 1159 TargetRegistry::RegisterMCInstrInfo(T, Fn); 1160 } 1161 }; 1162 1163 /// RegisterMCInstrAnalysis - Helper template for registering a target 1164 /// instruction analyzer implementation. This invokes the static "Create" 1165 /// method on the class to actually do the construction. Usage: 1166 /// 1167 /// extern "C" void LLVMInitializeFooTarget() { 1168 /// extern Target TheFooTarget; 1169 /// RegisterMCInstrAnalysis<FooMCInstrAnalysis> X(TheFooTarget); 1170 /// } 1171 template <class MCInstrAnalysisImpl> struct RegisterMCInstrAnalysis { RegisterMCInstrAnalysisRegisterMCInstrAnalysis1172 RegisterMCInstrAnalysis(Target &T) { 1173 TargetRegistry::RegisterMCInstrAnalysis(T, &Allocator); 1174 } 1175 1176 private: AllocatorRegisterMCInstrAnalysis1177 static MCInstrAnalysis *Allocator(const MCInstrInfo *Info) { 1178 return new MCInstrAnalysisImpl(Info); 1179 } 1180 }; 1181 1182 /// RegisterMCInstrAnalysisFn - Helper template for registering a target 1183 /// instruction analyzer implementation. This invokes the specified function 1184 /// to do the construction. Usage: 1185 /// 1186 /// extern "C" void LLVMInitializeFooTarget() { 1187 /// extern Target TheFooTarget; 1188 /// RegisterMCInstrAnalysisFn X(TheFooTarget, TheFunction); 1189 /// } 1190 struct RegisterMCInstrAnalysisFn { RegisterMCInstrAnalysisFnRegisterMCInstrAnalysisFn1191 RegisterMCInstrAnalysisFn(Target &T, Target::MCInstrAnalysisCtorFnTy Fn) { 1192 TargetRegistry::RegisterMCInstrAnalysis(T, Fn); 1193 } 1194 }; 1195 1196 /// RegisterMCRegInfo - Helper template for registering a target register info 1197 /// implementation. This invokes the static "Create" method on the class to 1198 /// actually do the construction. Usage: 1199 /// 1200 /// extern "C" void LLVMInitializeFooTarget() { 1201 /// extern Target TheFooTarget; 1202 /// RegisterMCRegInfo<FooMCRegInfo> X(TheFooTarget); 1203 /// } 1204 template <class MCRegisterInfoImpl> struct RegisterMCRegInfo { RegisterMCRegInfoRegisterMCRegInfo1205 RegisterMCRegInfo(Target &T) { 1206 TargetRegistry::RegisterMCRegInfo(T, &Allocator); 1207 } 1208 1209 private: AllocatorRegisterMCRegInfo1210 static MCRegisterInfo *Allocator(const Triple & /*TT*/) { 1211 return new MCRegisterInfoImpl(); 1212 } 1213 }; 1214 1215 /// RegisterMCRegInfoFn - Helper template for registering a target register 1216 /// info implementation. This invokes the specified function to do the 1217 /// construction. Usage: 1218 /// 1219 /// extern "C" void LLVMInitializeFooTarget() { 1220 /// extern Target TheFooTarget; 1221 /// RegisterMCRegInfoFn X(TheFooTarget, TheFunction); 1222 /// } 1223 struct RegisterMCRegInfoFn { RegisterMCRegInfoFnRegisterMCRegInfoFn1224 RegisterMCRegInfoFn(Target &T, Target::MCRegInfoCtorFnTy Fn) { 1225 TargetRegistry::RegisterMCRegInfo(T, Fn); 1226 } 1227 }; 1228 1229 /// RegisterMCSubtargetInfo - Helper template for registering a target 1230 /// subtarget info implementation. This invokes the static "Create" method 1231 /// on the class to actually do the construction. Usage: 1232 /// 1233 /// extern "C" void LLVMInitializeFooTarget() { 1234 /// extern Target TheFooTarget; 1235 /// RegisterMCSubtargetInfo<FooMCSubtargetInfo> X(TheFooTarget); 1236 /// } 1237 template <class MCSubtargetInfoImpl> struct RegisterMCSubtargetInfo { RegisterMCSubtargetInfoRegisterMCSubtargetInfo1238 RegisterMCSubtargetInfo(Target &T) { 1239 TargetRegistry::RegisterMCSubtargetInfo(T, &Allocator); 1240 } 1241 1242 private: AllocatorRegisterMCSubtargetInfo1243 static MCSubtargetInfo *Allocator(const Triple & /*TT*/, StringRef /*CPU*/, 1244 StringRef /*FS*/) { 1245 return new MCSubtargetInfoImpl(); 1246 } 1247 }; 1248 1249 /// RegisterMCSubtargetInfoFn - Helper template for registering a target 1250 /// subtarget info implementation. This invokes the specified function to 1251 /// do the construction. Usage: 1252 /// 1253 /// extern "C" void LLVMInitializeFooTarget() { 1254 /// extern Target TheFooTarget; 1255 /// RegisterMCSubtargetInfoFn X(TheFooTarget, TheFunction); 1256 /// } 1257 struct RegisterMCSubtargetInfoFn { RegisterMCSubtargetInfoFnRegisterMCSubtargetInfoFn1258 RegisterMCSubtargetInfoFn(Target &T, Target::MCSubtargetInfoCtorFnTy Fn) { 1259 TargetRegistry::RegisterMCSubtargetInfo(T, Fn); 1260 } 1261 }; 1262 1263 /// RegisterTargetMachine - Helper template for registering a target machine 1264 /// implementation, for use in the target machine initialization 1265 /// function. Usage: 1266 /// 1267 /// extern "C" void LLVMInitializeFooTarget() { 1268 /// extern Target TheFooTarget; 1269 /// RegisterTargetMachine<FooTargetMachine> X(TheFooTarget); 1270 /// } 1271 template <class TargetMachineImpl> struct RegisterTargetMachine { RegisterTargetMachineRegisterTargetMachine1272 RegisterTargetMachine(Target &T) { 1273 TargetRegistry::RegisterTargetMachine(T, &Allocator); 1274 } 1275 1276 private: 1277 static TargetMachine * AllocatorRegisterTargetMachine1278 Allocator(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, 1279 const TargetOptions &Options, std::optional<Reloc::Model> RM, 1280 std::optional<CodeModel::Model> CM, CodeGenOptLevel OL, bool JIT) { 1281 return new TargetMachineImpl(T, TT, CPU, FS, Options, RM, CM, OL, JIT); 1282 } 1283 }; 1284 1285 /// RegisterMCAsmBackend - Helper template for registering a target specific 1286 /// assembler backend. Usage: 1287 /// 1288 /// extern "C" void LLVMInitializeFooMCAsmBackend() { 1289 /// extern Target TheFooTarget; 1290 /// RegisterMCAsmBackend<FooAsmLexer> X(TheFooTarget); 1291 /// } 1292 template <class MCAsmBackendImpl> struct RegisterMCAsmBackend { RegisterMCAsmBackendRegisterMCAsmBackend1293 RegisterMCAsmBackend(Target &T) { 1294 TargetRegistry::RegisterMCAsmBackend(T, &Allocator); 1295 } 1296 1297 private: AllocatorRegisterMCAsmBackend1298 static MCAsmBackend *Allocator(const Target &T, const MCSubtargetInfo &STI, 1299 const MCRegisterInfo &MRI, 1300 const MCTargetOptions &Options) { 1301 return new MCAsmBackendImpl(T, STI, MRI); 1302 } 1303 }; 1304 1305 /// RegisterMCAsmParser - Helper template for registering a target specific 1306 /// assembly parser, for use in the target machine initialization 1307 /// function. Usage: 1308 /// 1309 /// extern "C" void LLVMInitializeFooMCAsmParser() { 1310 /// extern Target TheFooTarget; 1311 /// RegisterMCAsmParser<FooAsmParser> X(TheFooTarget); 1312 /// } 1313 template <class MCAsmParserImpl> struct RegisterMCAsmParser { RegisterMCAsmParserRegisterMCAsmParser1314 RegisterMCAsmParser(Target &T) { 1315 TargetRegistry::RegisterMCAsmParser(T, &Allocator); 1316 } 1317 1318 private: AllocatorRegisterMCAsmParser1319 static MCTargetAsmParser *Allocator(const MCSubtargetInfo &STI, 1320 MCAsmParser &P, const MCInstrInfo &MII, 1321 const MCTargetOptions &Options) { 1322 return new MCAsmParserImpl(STI, P, MII, Options); 1323 } 1324 }; 1325 1326 /// RegisterAsmPrinter - Helper template for registering a target specific 1327 /// assembly printer, for use in the target machine initialization 1328 /// function. Usage: 1329 /// 1330 /// extern "C" void LLVMInitializeFooAsmPrinter() { 1331 /// extern Target TheFooTarget; 1332 /// RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget); 1333 /// } 1334 template <class AsmPrinterImpl> struct RegisterAsmPrinter { RegisterAsmPrinterRegisterAsmPrinter1335 RegisterAsmPrinter(Target &T) { 1336 TargetRegistry::RegisterAsmPrinter(T, &Allocator); 1337 } 1338 1339 private: AllocatorRegisterAsmPrinter1340 static AsmPrinter *Allocator(TargetMachine &TM, 1341 std::unique_ptr<MCStreamer> &&Streamer) { 1342 return new AsmPrinterImpl(TM, std::move(Streamer)); 1343 } 1344 }; 1345 1346 /// RegisterMCCodeEmitter - Helper template for registering a target specific 1347 /// machine code emitter, for use in the target initialization 1348 /// function. Usage: 1349 /// 1350 /// extern "C" void LLVMInitializeFooMCCodeEmitter() { 1351 /// extern Target TheFooTarget; 1352 /// RegisterMCCodeEmitter<FooCodeEmitter> X(TheFooTarget); 1353 /// } 1354 template <class MCCodeEmitterImpl> struct RegisterMCCodeEmitter { RegisterMCCodeEmitterRegisterMCCodeEmitter1355 RegisterMCCodeEmitter(Target &T) { 1356 TargetRegistry::RegisterMCCodeEmitter(T, &Allocator); 1357 } 1358 1359 private: AllocatorRegisterMCCodeEmitter1360 static MCCodeEmitter *Allocator(const MCInstrInfo & /*II*/, 1361 MCContext & /*Ctx*/) { 1362 return new MCCodeEmitterImpl(); 1363 } 1364 }; 1365 1366 } // end namespace llvm 1367 1368 #endif // LLVM_MC_TARGETREGISTRY_H 1369