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