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