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