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