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