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