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