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