1 //===-- Target/TargetRegistry.h - Target Registration -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 // Target specific class implementations should register themselves using the
15 // appropriate TargetRegistry interfaces.
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_TARGET_TARGETREGISTRY_H
20 #define LLVM_TARGET_TARGETREGISTRY_H
22 #include "llvm/ADT/Triple.h"
31 class TargetAsmParser;
33 class formatted_raw_ostream;
35 /// Target - Wrapper for Target specific information.
37 /// For registration purposes, this is a POD type so that targets can be
38 /// registered without the use of static constructors.
40 /// Targets should implement a single global instance of this class (which
41 /// will be zero initialized), and pass that instance to the TargetRegistry as
42 /// part of their initialization.
45 friend struct TargetRegistry;
47 typedef unsigned (*TripleMatchQualityFnTy)(const std::string &TT);
49 typedef const TargetAsmInfo *(*AsmInfoCtorFnTy)(const Target &T,
51 typedef TargetMachine *(*TargetMachineCtorTy)(const Target &T,
52 const std::string &TT,
53 const std::string &Features);
54 typedef FunctionPass *(*AsmPrinterCtorTy)(formatted_raw_ostream &OS,
56 const TargetAsmInfo *TAI,
58 typedef TargetAsmParser *(*AsmParserCtorTy)(const Target &T,
61 /// Next - The next registered target in the linked list, maintained by the
65 /// TripleMatchQualityFn - The target function for rating the match quality
67 TripleMatchQualityFnTy TripleMatchQualityFn;
69 /// Name - The target name.
72 /// ShortDesc - A short description of the target.
73 const char *ShortDesc;
75 /// HasJIT - Whether this target supports the JIT.
78 AsmInfoCtorFnTy AsmInfoCtorFn;
80 /// TargetMachineCtorFn - Construction function for this target's
81 /// TargetMachine, if registered.
82 TargetMachineCtorTy TargetMachineCtorFn;
84 /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
86 AsmPrinterCtorTy AsmPrinterCtorFn;
88 /// AsmParserCtorFn - Construction function for this target's AsmParser,
90 AsmParserCtorTy AsmParserCtorFn;
93 // getNext - Return the next registered target.
94 const Target *getNext() const { return Next; }
96 /// getName - Get the target name.
97 const char *getName() const { return Name; }
99 /// getShortDescription - Get a short description of the target.
100 const char *getShortDescription() const { return ShortDesc; }
102 bool hasJIT() const { return HasJIT; }
104 /// hasTargetMachine - Check if this target supports code generation.
105 bool hasTargetMachine() const { return TargetMachineCtorFn != 0; }
107 /// hasAsmPrinter - Check if this target supports .s printing.
108 bool hasAsmPrinter() const { return AsmPrinterCtorFn != 0; }
110 /// hasAsmParser - Check if this target supports .s parsing.
111 bool hasAsmParser() const { return AsmParserCtorFn != 0; }
114 /// createAsmInfo - Create a TargetAsmInfo implementation for the specified
117 /// \arg Triple - This argument is used to determine the target machine
118 /// feature set; it should always be provided. Generally this should be
119 /// either the target triple from the module, or the target triple of the
120 /// host if that does not exist.
121 const TargetAsmInfo *createAsmInfo(const StringRef &Triple) const {
124 return AsmInfoCtorFn(*this, Triple);
127 /// createTargetMachine - Create a target specific machine implementation
128 /// for the specified \arg Triple.
130 /// \arg Triple - This argument is used to determine the target machine
131 /// feature set; it should always be provided. Generally this should be
132 /// either the target triple from the module, or the target triple of the
133 /// host if that does not exist.
134 TargetMachine *createTargetMachine(const std::string &Triple,
135 const std::string &Features) const {
136 if (!TargetMachineCtorFn)
138 return TargetMachineCtorFn(*this, Triple, Features);
141 /// createAsmPrinter - Create a target specific assembly printer pass.
142 FunctionPass *createAsmPrinter(formatted_raw_ostream &OS,
144 const TargetAsmInfo *TAI,
145 bool Verbose) const {
146 if (!AsmPrinterCtorFn)
148 return AsmPrinterCtorFn(OS, TM, TAI, Verbose);
151 /// createAsmParser - Create a target specific assembly parser.
153 /// \arg Parser - The target independent parser implementation to use for
154 /// parsing and lexing.
155 TargetAsmParser *createAsmParser(MCAsmParser &Parser) const {
156 if (!AsmParserCtorFn)
158 return AsmParserCtorFn(*this, Parser);
162 /// TargetRegistry - Generic interface to target specific features.
163 struct TargetRegistry {
165 const Target *Current;
166 explicit iterator(Target *T) : Current(T) {}
167 friend struct TargetRegistry;
169 iterator(const iterator &I) : Current(I.Current) {}
170 iterator() : Current(0) {}
172 bool operator==(const iterator &x) const {
173 return Current == x.Current;
175 bool operator!=(const iterator &x) const {
176 return !operator==(x);
179 // Iterator traversal: forward iteration only
180 iterator &operator++() { // Preincrement
181 assert(Current && "Cannot increment end iterator!");
182 Current = Current->getNext();
185 iterator operator++(int) { // Postincrement
186 iterator tmp = *this;
191 const Target &operator*() const {
192 assert(Current && "Cannot dereference end iterator!");
196 const Target *operator->() const {
201 /// @name Registry Access
204 static iterator begin();
206 static iterator end() { return iterator(); }
208 /// lookupTarget - Lookup a target based on a target triple.
210 /// \param Triple - The triple to use for finding a target.
211 /// \param Error - On failure, an error string describing why no target was
213 static const Target *lookupTarget(const std::string &Triple,
216 /// getClosestTargetForJIT - Pick the best target that is compatible with
217 /// the current host. If no close target can be found, this returns null
218 /// and sets the Error string to a reason.
220 /// Maintained for compatibility through 2.6.
221 static const Target *getClosestTargetForJIT(std::string &Error);
224 /// @name Target Registration
227 /// RegisterTarget - Register the given target. Attempts to register a
228 /// target which has already been registered will be ignored.
230 /// Clients are responsible for ensuring that registration doesn't occur
231 /// while another thread is attempting to access the registry. Typically
232 /// this is done by initializing all targets at program startup.
234 /// @param T - The target being registered.
235 /// @param Name - The target name. This should be a static string.
236 /// @param ShortDesc - A short target description. This should be a static
238 /// @param TQualityFn - The triple match quality computation function for
240 /// @param HasJIT - Whether the target supports JIT code
242 static void RegisterTarget(Target &T,
244 const char *ShortDesc,
245 Target::TripleMatchQualityFnTy TQualityFn,
246 bool HasJIT = false);
248 /// RegisterAsmInfo - Register a TargetAsmInfo implementation for the
251 /// Clients are responsible for ensuring that registration doesn't occur
252 /// while another thread is attempting to access the registry. Typically
253 /// this is done by initializing all targets at program startup.
255 /// @param T - The target being registered.
256 /// @param Fn - A function to construct a TargetAsmInfo for the target.
257 static void RegisterAsmInfo(Target &T, Target::AsmInfoCtorFnTy Fn) {
258 // Ignore duplicate registration.
259 if (!T.AsmInfoCtorFn)
260 T.AsmInfoCtorFn = Fn;
263 /// RegisterTargetMachine - Register a TargetMachine implementation for the
266 /// Clients are responsible for ensuring that registration doesn't occur
267 /// while another thread is attempting to access the registry. Typically
268 /// this is done by initializing all targets at program startup.
270 /// @param T - The target being registered.
271 /// @param Fn - A function to construct a TargetMachine for the target.
272 static void RegisterTargetMachine(Target &T,
273 Target::TargetMachineCtorTy Fn) {
274 // Ignore duplicate registration.
275 if (!T.TargetMachineCtorFn)
276 T.TargetMachineCtorFn = Fn;
279 /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
282 /// Clients are responsible for ensuring that registration doesn't occur
283 /// while another thread is attempting to access the registry. Typically
284 /// this is done by initializing all targets at program startup.
286 /// @param T - The target being registered.
287 /// @param Fn - A function to construct an AsmPrinter for the target.
288 static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
289 // Ignore duplicate registration.
290 if (!T.AsmPrinterCtorFn)
291 T.AsmPrinterCtorFn = Fn;
294 /// RegisterAsmParser - Register a TargetAsmParser implementation for the
297 /// Clients are responsible for ensuring that registration doesn't occur
298 /// while another thread is attempting to access the registry. Typically
299 /// this is done by initializing all targets at program startup.
301 /// @param T - The target being registered.
302 /// @param Fn - A function to construct an AsmPrinter for the target.
303 static void RegisterAsmParser(Target &T, Target::AsmParserCtorTy Fn) {
304 if (!T.AsmParserCtorFn)
305 T.AsmParserCtorFn = Fn;
312 //===--------------------------------------------------------------------===//
314 /// RegisterTarget - Helper template for registering a target, for use in the
315 /// target's initialization function. Usage:
318 /// Target TheFooTarget; // The global target instance.
320 /// extern "C" void LLVMInitializeFooTargetInfo() {
321 /// RegisterTarget<Triple::foo> X(TheFooTarget, "foo", "Foo description");
323 template<Triple::ArchType TargetArchType = Triple::InvalidArch,
325 struct RegisterTarget {
326 RegisterTarget(Target &T, const char *Name, const char *Desc) {
327 TargetRegistry::RegisterTarget(T, Name, Desc,
328 &getTripleMatchQuality,
332 static unsigned getTripleMatchQuality(const std::string &TT) {
333 if (Triple(TT.c_str()).getArch() == TargetArchType)
339 /// RegisterAsmInfo - Helper template for registering a target assembly info
340 /// implementation. This invokes the static "Create" method on the class to
341 /// actually do the construction. Usage:
343 /// extern "C" void LLVMInitializeFooTarget() {
344 /// extern Target TheFooTarget;
345 /// RegisterAsmInfo<FooTargetAsmInfo> X(TheFooTarget);
347 template<class TargetAsmInfoImpl>
348 struct RegisterAsmInfo {
349 RegisterAsmInfo(Target &T) {
350 TargetRegistry::RegisterAsmInfo(T, &Allocator);
353 static const TargetAsmInfo *Allocator(const Target &T, const StringRef &TT){
354 return new TargetAsmInfoImpl(T, TT);
359 /// RegisterAsmInfoFn - Helper template for registering a target assembly info
360 /// implementation. This invokes the specified function to do the
361 /// construction. Usage:
363 /// extern "C" void LLVMInitializeFooTarget() {
364 /// extern Target TheFooTarget;
365 /// RegisterAsmInfoFn X(TheFooTarget, TheFunction);
367 struct RegisterAsmInfoFn {
368 RegisterAsmInfoFn(Target &T, Target::AsmInfoCtorFnTy Fn) {
369 TargetRegistry::RegisterAsmInfo(T, Fn);
374 /// RegisterTargetMachine - Helper template for registering a target machine
375 /// implementation, for use in the target machine initialization
378 /// extern "C" void LLVMInitializeFooTarget() {
379 /// extern Target TheFooTarget;
380 /// RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
382 template<class TargetMachineImpl>
383 struct RegisterTargetMachine {
384 RegisterTargetMachine(Target &T) {
385 TargetRegistry::RegisterTargetMachine(T, &Allocator);
389 static TargetMachine *Allocator(const Target &T, const std::string &TT,
390 const std::string &FS) {
391 return new TargetMachineImpl(T, TT, FS);
395 /// RegisterAsmPrinter - Helper template for registering a target specific
396 /// assembly printer, for use in the target machine initialization
399 /// extern "C" void LLVMInitializeFooAsmPrinter() {
400 /// extern Target TheFooTarget;
401 /// RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
403 template<class AsmPrinterImpl>
404 struct RegisterAsmPrinter {
405 RegisterAsmPrinter(Target &T) {
406 TargetRegistry::RegisterAsmPrinter(T, &Allocator);
410 static FunctionPass *Allocator(formatted_raw_ostream &OS,
412 const TargetAsmInfo *TAI,
414 return new AsmPrinterImpl(OS, TM, TAI, Verbose);
418 /// RegisterAsmParser - Helper template for registering a target specific
419 /// assembly parser, for use in the target machine initialization
422 /// extern "C" void LLVMInitializeFooAsmParser() {
423 /// extern Target TheFooTarget;
424 /// RegisterAsmParser<FooAsmParser> X(TheFooTarget);
426 template<class AsmParserImpl>
427 struct RegisterAsmParser {
428 RegisterAsmParser(Target &T) {
429 TargetRegistry::RegisterAsmParser(T, &Allocator);
433 static TargetAsmParser *Allocator(const Target &T, MCAsmParser &P) {
434 return new AsmParserImpl(T, P);