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