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