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 AsmPrinter *(*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 AsmPrinter *createAsmPrinter(formatted_raw_ostream &OS, TargetMachine &TM,
143 const TargetAsmInfo *TAI, bool Verbose) const {
144 if (!AsmPrinterCtorFn)
146 return AsmPrinterCtorFn(OS, TM, TAI, Verbose);
149 /// createAsmParser - Create a target specific assembly parser.
151 /// \arg Parser - The target independent parser implementation to use for
152 /// parsing and lexing.
153 TargetAsmParser *createAsmParser(MCAsmParser &Parser) const {
154 if (!AsmParserCtorFn)
156 return AsmParserCtorFn(*this, Parser);
160 /// TargetRegistry - Generic interface to target specific features.
161 struct TargetRegistry {
163 const Target *Current;
164 explicit iterator(Target *T) : Current(T) {}
165 friend struct TargetRegistry;
167 iterator(const iterator &I) : Current(I.Current) {}
168 iterator() : Current(0) {}
170 bool operator==(const iterator &x) const {
171 return Current == x.Current;
173 bool operator!=(const iterator &x) const {
174 return !operator==(x);
177 // Iterator traversal: forward iteration only
178 iterator &operator++() { // Preincrement
179 assert(Current && "Cannot increment end iterator!");
180 Current = Current->getNext();
183 iterator operator++(int) { // Postincrement
184 iterator tmp = *this;
189 const Target &operator*() const {
190 assert(Current && "Cannot dereference end iterator!");
194 const Target *operator->() const {
199 /// @name Registry Access
202 static iterator begin();
204 static iterator end() { return iterator(); }
206 /// lookupTarget - Lookup a target based on a target triple.
208 /// \param Triple - The triple to use for finding a target.
209 /// \param Error - On failure, an error string describing why no target was
211 static const Target *lookupTarget(const std::string &Triple,
214 /// getClosestTargetForJIT - Pick the best target that is compatible with
215 /// the current host. If no close target can be found, this returns null
216 /// and sets the Error string to a reason.
218 /// Maintained for compatibility through 2.6.
219 static const Target *getClosestTargetForJIT(std::string &Error);
222 /// @name Target Registration
225 /// RegisterTarget - Register the given target. Attempts to register a
226 /// target which has already been registered will be ignored.
228 /// Clients are responsible for ensuring that registration doesn't occur
229 /// while another thread is attempting to access the registry. Typically
230 /// this is done by initializing all targets at program startup.
232 /// @param T - The target being registered.
233 /// @param Name - The target name. This should be a static string.
234 /// @param ShortDesc - A short target description. This should be a static
236 /// @param TQualityFn - The triple match quality computation function for
238 /// @param HasJIT - Whether the target supports JIT code
240 static void RegisterTarget(Target &T,
242 const char *ShortDesc,
243 Target::TripleMatchQualityFnTy TQualityFn,
244 bool HasJIT = false);
246 /// RegisterAsmInfo - Register a TargetAsmInfo implementation for the
249 /// Clients are responsible for ensuring that registration doesn't occur
250 /// while another thread is attempting to access the registry. Typically
251 /// this is done by initializing all targets at program startup.
253 /// @param T - The target being registered.
254 /// @param Fn - A function to construct a TargetAsmInfo for the target.
255 static void RegisterAsmInfo(Target &T, Target::AsmInfoCtorFnTy Fn) {
256 // Ignore duplicate registration.
257 if (!T.AsmInfoCtorFn)
258 T.AsmInfoCtorFn = Fn;
261 /// RegisterTargetMachine - Register a TargetMachine implementation for the
264 /// Clients are responsible for ensuring that registration doesn't occur
265 /// while another thread is attempting to access the registry. Typically
266 /// this is done by initializing all targets at program startup.
268 /// @param T - The target being registered.
269 /// @param Fn - A function to construct a TargetMachine for the target.
270 static void RegisterTargetMachine(Target &T,
271 Target::TargetMachineCtorTy Fn) {
272 // Ignore duplicate registration.
273 if (!T.TargetMachineCtorFn)
274 T.TargetMachineCtorFn = Fn;
277 /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
280 /// Clients are responsible for ensuring that registration doesn't occur
281 /// while another thread is attempting to access the registry. Typically
282 /// this is done by initializing all targets at program startup.
284 /// @param T - The target being registered.
285 /// @param Fn - A function to construct an AsmPrinter for the target.
286 static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
287 // Ignore duplicate registration.
288 if (!T.AsmPrinterCtorFn)
289 T.AsmPrinterCtorFn = Fn;
292 /// RegisterAsmParser - Register a TargetAsmParser implementation for the
295 /// Clients are responsible for ensuring that registration doesn't occur
296 /// while another thread is attempting to access the registry. Typically
297 /// this is done by initializing all targets at program startup.
299 /// @param T - The target being registered.
300 /// @param Fn - A function to construct an AsmPrinter for the target.
301 static void RegisterAsmParser(Target &T, Target::AsmParserCtorTy Fn) {
302 if (!T.AsmParserCtorFn)
303 T.AsmParserCtorFn = Fn;
310 //===--------------------------------------------------------------------===//
312 /// RegisterTarget - Helper template for registering a target, for use in the
313 /// target's initialization function. Usage:
316 /// Target TheFooTarget; // The global target instance.
318 /// extern "C" void LLVMInitializeFooTargetInfo() {
319 /// RegisterTarget<Triple::foo> X(TheFooTarget, "foo", "Foo description");
321 template<Triple::ArchType TargetArchType = Triple::InvalidArch,
323 struct RegisterTarget {
324 RegisterTarget(Target &T, const char *Name, const char *Desc) {
325 TargetRegistry::RegisterTarget(T, Name, Desc,
326 &getTripleMatchQuality,
330 static unsigned getTripleMatchQuality(const std::string &TT) {
331 if (Triple(TT.c_str()).getArch() == TargetArchType)
337 /// RegisterAsmInfo - Helper template for registering a target assembly info
338 /// implementation. This invokes the static "Create" method on the class to
339 /// actually do the construction. Usage:
341 /// extern "C" void LLVMInitializeFooTarget() {
342 /// extern Target TheFooTarget;
343 /// RegisterAsmInfo<FooTargetAsmInfo> X(TheFooTarget);
345 template<class TargetAsmInfoImpl>
346 struct RegisterAsmInfo {
347 RegisterAsmInfo(Target &T) {
348 TargetRegistry::RegisterAsmInfo(T, &Allocator);
351 static const TargetAsmInfo *Allocator(const Target &T, const StringRef &TT){
352 return new TargetAsmInfoImpl(T, TT);
357 /// RegisterAsmInfoFn - Helper template for registering a target assembly info
358 /// implementation. This invokes the specified function to do the
359 /// construction. Usage:
361 /// extern "C" void LLVMInitializeFooTarget() {
362 /// extern Target TheFooTarget;
363 /// RegisterAsmInfoFn X(TheFooTarget, TheFunction);
365 struct RegisterAsmInfoFn {
366 RegisterAsmInfoFn(Target &T, Target::AsmInfoCtorFnTy Fn) {
367 TargetRegistry::RegisterAsmInfo(T, Fn);
372 /// RegisterTargetMachine - Helper template for registering a target machine
373 /// implementation, for use in the target machine initialization
376 /// extern "C" void LLVMInitializeFooTarget() {
377 /// extern Target TheFooTarget;
378 /// RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
380 template<class TargetMachineImpl>
381 struct RegisterTargetMachine {
382 RegisterTargetMachine(Target &T) {
383 TargetRegistry::RegisterTargetMachine(T, &Allocator);
387 static TargetMachine *Allocator(const Target &T, const std::string &TT,
388 const std::string &FS) {
389 return new TargetMachineImpl(T, TT, FS);
393 /// RegisterAsmPrinter - Helper template for registering a target specific
394 /// assembly printer, for use in the target machine initialization
397 /// extern "C" void LLVMInitializeFooAsmPrinter() {
398 /// extern Target TheFooTarget;
399 /// RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
401 template<class AsmPrinterImpl>
402 struct RegisterAsmPrinter {
403 RegisterAsmPrinter(Target &T) {
404 TargetRegistry::RegisterAsmPrinter(T, &Allocator);
408 static AsmPrinter *Allocator(formatted_raw_ostream &OS, TargetMachine &TM,
409 const TargetAsmInfo *TAI, bool Verbose) {
410 return new AsmPrinterImpl(OS, TM, TAI, Verbose);
414 /// RegisterAsmParser - Helper template for registering a target specific
415 /// assembly parser, for use in the target machine initialization
418 /// extern "C" void LLVMInitializeFooAsmParser() {
419 /// extern Target TheFooTarget;
420 /// RegisterAsmParser<FooAsmParser> X(TheFooTarget);
422 template<class AsmParserImpl>
423 struct RegisterAsmParser {
424 RegisterAsmParser(Target &T) {
425 TargetRegistry::RegisterAsmParser(T, &Allocator);
429 static TargetAsmParser *Allocator(const Target &T, MCAsmParser &P) {
430 return new AsmParserImpl(T, P);