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 // FIXME: We shouldn't need this header, but we need it until there is a
23 // different interface to get the TargetAsmInfo.
24 #include "llvm/Target/TargetMachine.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 typedef unsigned (*TripleMatchQualityFnTy)(const std::string &TT);
46 typedef unsigned (*ModuleMatchQualityFnTy)(const Module &M);
47 typedef unsigned (*JITMatchQualityFnTy)();
49 typedef TargetMachine *(*TargetMachineCtorTy)(const Target &,
52 typedef FunctionPass *(*AsmPrinterCtorTy)(formatted_raw_ostream &,
55 typedef TargetAsmParser *(*AsmParserCtorTy)(const Target &);
57 friend struct TargetRegistry;
59 /// Next - The next registered target in the linked list, maintained by the
63 /// TripleMatchQualityFn - The target function for rating the match quality
65 TripleMatchQualityFnTy TripleMatchQualityFn;
67 /// ModuleMatchQualityFn - The target function for rating the match quality
69 ModuleMatchQualityFnTy ModuleMatchQualityFn;
71 /// JITMatchQualityFn - The target function for rating the match quality
73 JITMatchQualityFnTy JITMatchQualityFn;
75 /// Name - The target name.
78 /// ShortDesc - A short description of the target.
79 const char *ShortDesc;
81 /// TargetMachineCtorFn - Construction function for this target's
82 /// TargetMachine, if registered.
83 TargetMachineCtorTy TargetMachineCtorFn;
85 /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
87 AsmPrinterCtorTy AsmPrinterCtorFn;
89 /// AsmParserCtorFn - Construction function for this target's AsmParser,
91 AsmParserCtorTy AsmParserCtorFn;
94 // getNext - Return the next registered target.
95 const Target *getNext() const { return Next; }
97 /// getName - Get the target name.
98 const char *getName() const { return Name; }
100 /// getShortDescription - Get a short description of the target.
101 const char *getShortDescription() const { return ShortDesc; }
103 /// getJITMatchQuality - Get the quality of this targets match for use as a
105 unsigned getJITMatchQuality() const { return JITMatchQualityFn(); }
107 /// hasTargetMachine - Check if this target supports code generation.
108 bool hasTargetMachine() const { return TargetMachineCtorFn != 0; }
110 /// hasAsmPrinter - Check if this target supports .s printing.
111 bool hasAsmPrinter() const { return AsmPrinterCtorFn != 0; }
113 /// hasAsmParser - Check if this target supports .s parsing.
114 bool hasAsmParser() const { return AsmParserCtorFn != 0; }
116 /// createTargetMachine - Create a target specific machine implementation.
117 TargetMachine *createTargetMachine(const Module &M,
118 const std::string &Features) const {
119 if (!TargetMachineCtorFn)
121 return TargetMachineCtorFn(*this, M, Features);
124 /// createAsmPrinter - Create a target specific assembly printer pass.
125 FunctionPass *createAsmPrinter(formatted_raw_ostream &OS,
127 bool Verbose) const {
128 if (!AsmPrinterCtorFn)
130 return AsmPrinterCtorFn(OS, M, Verbose);
133 /// createAsmParser - Create a target specific assembly parser.
134 TargetAsmParser *createAsmParser() const {
135 if (!AsmParserCtorFn)
137 return AsmParserCtorFn(*this);
141 /// TargetRegistry - Generic interface to target specific features.
143 // FIXME: Provide Target* iterator.
144 struct TargetRegistry {
146 const Target *Current;
147 explicit iterator(Target *T) : Current(T) {}
148 friend struct TargetRegistry;
150 iterator(const iterator &I) : Current(I.Current) {}
151 iterator() : Current(0) {}
153 bool operator==(const iterator &x) const {
154 return Current == x.Current;
156 bool operator!=(const iterator &x) const {
157 return !operator==(x);
160 // Iterator traversal: forward iteration only
161 iterator &operator++() { // Preincrement
162 assert(Current && "Cannot increment end iterator!");
163 Current = Current->getNext();
166 iterator operator++(int) { // Postincrement
167 iterator tmp = *this;
172 const Target &operator*() const {
173 assert(Current && "Cannot dereference end iterator!");
177 const Target *operator->() const {
182 /// @name Registry Access
185 static iterator begin();
187 static iterator end() { return iterator(); }
189 /// getClosestStaticTargetForTriple - Given a target triple, pick the most
190 /// capable target for that triple.
191 static const Target *getClosestStaticTargetForTriple(const std::string &TT,
194 /// getClosestStaticTargetForModule - Given an LLVM module, pick the best
195 /// target that is compatible with the module. If no close target can be
196 /// found, this returns null and sets the Error string to a reason.
197 static const Target *getClosestStaticTargetForModule(const Module &M,
200 /// getClosestTargetForJIT - Pick the best target that is compatible with
201 /// the current host. If no close target can be found, this returns null
202 /// and sets the Error string to a reason.
204 // FIXME: Do we still need this interface, clients can always look for the
205 // match for the host triple.
206 static const Target *getClosestTargetForJIT(std::string &Error);
209 /// @name Target Registration
212 /// RegisterTarget - Register the given target. Attempts to register a
213 /// target which has already been registered will be ignored.
215 /// Clients are responsible for ensuring that registration doesn't occur
216 /// while another thread is attempting to access the registry. Typically
217 /// this is done by initializing all targets at program startup.
219 /// @param T - The target being registered.
220 /// @param Name - The target name. This should be a static string.
221 /// @param ShortDesc - A short target description. This should be a static
223 /// @param TQualityFn - The triple match quality computation function for
225 /// @param MQualityFn - The module match quality computation function for
227 /// @param JITMatchQualityFn - The JIT match quality computation function
229 static void RegisterTarget(Target &T,
231 const char *ShortDesc,
232 Target::TripleMatchQualityFnTy TQualityFn,
233 Target::ModuleMatchQualityFnTy MQualityFn,
234 Target::JITMatchQualityFnTy JITQualityFn);
236 /// RegisterTargetMachine - Register a TargetMachine implementation for the
239 /// Clients are responsible for ensuring that registration doesn't occur
240 /// while another thread is attempting to access the registry. Typically
241 /// this is done by initializing all targets at program startup.
243 /// @param T - The target being registered.
244 /// @param Fn - A function to construct a TargetMachine for the target.
245 static void RegisterTargetMachine(Target &T,
246 Target::TargetMachineCtorTy Fn) {
247 // Ignore duplicate registration.
248 if (!T.TargetMachineCtorFn)
249 T.TargetMachineCtorFn = Fn;
252 /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
255 /// Clients are responsible for ensuring that registration doesn't occur
256 /// while another thread is attempting to access the registry. Typically
257 /// this is done by initializing all targets at program startup.
259 /// @param T - The target being registered.
260 /// @param Fn - A function to construct an AsmPrinter for the target.
261 static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
262 // Ignore duplicate registration.
263 if (!T.AsmPrinterCtorFn)
264 T.AsmPrinterCtorFn = Fn;
267 /// RegisterAsmParser - Register a TargetAsmParser implementation for the
270 /// Clients are responsible for ensuring that registration doesn't occur
271 /// while another thread is attempting to access the registry. Typically
272 /// this is done by initializing all targets at program startup.
274 /// @param T - The target being registered.
275 /// @param Fn - A function to construct an AsmPrinter for the target.
276 static void RegisterAsmParser(Target &T, Target::AsmParserCtorTy Fn) {
277 if (!T.AsmParserCtorFn)
278 T.AsmParserCtorFn = Fn;
285 //===--------------------------------------------------------------------===//
287 /// RegisterTarget - Helper template for registering a target, for use in the
288 /// target's initialization function. Usage:
291 /// Target TheFooTarget; // The global target instance.
295 /// static unsigned getJITMatchQuality() { ... }
296 /// static unsigned getTripleMatchQuality(const std::string &) { ... }
297 /// static unsigned getModuleMatchQuality(const Module &) { ... }
301 /// extern "C" void LLVMInitializeFooTargetInfo() {
302 /// RegisterTarget<FooAsmPrinter> X(TheFooTarget, "foo", "Foo description");
304 template<class TargetInfoImpl>
305 struct RegisterTarget {
306 RegisterTarget(Target &T, const char *Name, const char *Desc) {
307 TargetRegistry::RegisterTarget(T, Name, Desc,
308 &TargetInfoImpl::getTripleMatchQuality,
309 &TargetInfoImpl::getModuleMatchQuality,
310 &TargetInfoImpl::getJITMatchQuality);
314 /// RegisterTargetMachine - Helper template for registering a target machine
315 /// implementation, for use in the target machine initialization
318 /// extern "C" void LLVMInitializeFooTarget() {
319 /// extern Target TheFooTarget;
320 /// RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
322 template<class TargetMachineImpl>
323 struct RegisterTargetMachine {
324 RegisterTargetMachine(Target &T) {
325 TargetRegistry::RegisterTargetMachine(T, &Allocator);
329 static TargetMachine *Allocator(const Target &T, const Module &M,
330 const std::string &FS) {
331 return new TargetMachineImpl(T, M, FS);
335 /// RegisterAsmPrinter - Helper template for registering a target specific
336 /// assembly printer, for use in the target machine initialization
339 /// extern "C" void LLVMInitializeFooAsmPrinter() {
340 /// extern Target TheFooTarget;
341 /// RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
343 template<class AsmPrinterImpl>
344 struct RegisterAsmPrinter {
345 RegisterAsmPrinter(Target &T) {
346 TargetRegistry::RegisterAsmPrinter(T, &Allocator);
350 static FunctionPass *Allocator(formatted_raw_ostream &OS,
353 return new AsmPrinterImpl(OS, TM, TM.getTargetAsmInfo(), Verbose);
357 /// RegisterAsmParser - Helper template for registering a target specific asm
358 /// parser, for use in the target machine initialization function. Usage:
360 /// extern "C" void LLVMInitializeFooAsmPrinter() {
361 /// extern Target TheFooTarget;
362 /// RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
364 template<class AsmParserImpl>
365 struct RegisterAsmParser {
366 RegisterAsmParser(Target &T) {
367 TargetRegistry::RegisterAsmParser(T, &Allocator);
371 static TargetAsmParser *Allocator(const Target &T) {
372 return new AsmParserImpl(T);