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