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
29 class formatted_raw_ostream;
31 /// Target - Wrapper for Target specific information.
33 /// For registration purposes, this is a POD type so that targets can be
34 /// registered without the use of static constructors.
36 /// Targets should implement a single global instance of this class (which
37 /// will be zero initialized), and pass that instance to the TargetRegistry as
38 /// part of their initialization.
41 typedef unsigned (*TripleMatchQualityFnTy)(const std::string &TT);
42 typedef unsigned (*ModuleMatchQualityFnTy)(const Module &M);
43 typedef unsigned (*JITMatchQualityFnTy)();
45 typedef TargetMachine *(*TargetMachineCtorTy)(const Module &,
47 typedef FunctionPass *(*AsmPrinterCtorTy)(formatted_raw_ostream &,
51 friend struct TargetRegistry;
52 // FIXME: Temporary hack, please remove.
53 friend struct TargetMachineRegistry;
55 /// Next - The next registered target in the linked list, maintained by the
59 /// TripleMatchQualityFn - The target function for rating the match quality
61 TripleMatchQualityFnTy TripleMatchQualityFn;
63 /// ModuleMatchQualityFn - The target function for rating the match quality
65 ModuleMatchQualityFnTy ModuleMatchQualityFn;
67 /// JITMatchQualityFn - The target function for rating the match quality
69 JITMatchQualityFnTy JITMatchQualityFn;
71 /// Name - The target name.
74 /// ShortDesc - A short description of the target.
75 const char *ShortDesc;
77 /// TargetMachineCtorFn - Construction function for this target's
78 /// TargetMachine, if registered.
79 TargetMachineCtorTy TargetMachineCtorFn;
81 /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
83 AsmPrinterCtorTy AsmPrinterCtorFn;
86 /// getName - Get the target name.
87 const char *getName() const { return Name; }
89 /// getShortDescription - Get a short description of the target.
90 const char *getShortDescription() const { return ShortDesc; }
92 /// createTargetMachine - Create a target specific machine implementation.
93 TargetMachine *createTargetMachine(const Module &M,
94 const std::string &Features) const {
95 if (!TargetMachineCtorFn)
97 return TargetMachineCtorFn(M, Features);
100 /// createAsmPrinter - Create a target specific assembly printer pass.
101 FunctionPass *createAsmPrinter(formatted_raw_ostream &OS,
103 bool Verbose) const {
104 if (!AsmPrinterCtorFn)
106 return AsmPrinterCtorFn(OS, M, Verbose);
110 /// TargetRegistry - Generic interface to target specific features.
112 // FIXME: Provide Target* iterator.
113 struct TargetRegistry {
114 /// @name Registry Access
117 /// getClosestStaticTargetForTriple - Given a target triple, pick the most
118 /// capable target for that triple.
119 static const Target *getClosestStaticTargetForTriple(const std::string &TT,
122 /// getClosestStaticTargetForModule - Given an LLVM module, pick the best
123 /// target that is compatible with the module. If no close target can be
124 /// found, this returns null and sets the Error string to a reason.
125 static const Target *getClosestStaticTargetForModule(const Module &M,
128 /// getClosestTargetForJIT - Pick the best target that is compatible with
129 /// the current host. If no close target can be found, this returns null
130 /// and sets the Error string to a reason.
132 // FIXME: Do we still need this interface, clients can always look for the
133 // match for the host triple.
134 static const Target *getClosestTargetForJIT(std::string &Error);
137 /// @name Target Registration
140 /// RegisterTarget - Register the given target. Attempts to register a
141 /// target which has already been registered will be ignored.
143 /// Clients are responsible for ensuring that registration doesn't occur
144 /// while another thread is attempting to access the registry. Typically
145 /// this is done by initializing all targets at program startup.
147 /// @param T - The target being registered.
148 /// @param Name - The target name. This should be a static string.
149 /// @param ShortDesc - A short target description. This should be a static
151 /// @param TQualityFn - The triple match quality computation function for
153 /// @param MQualityFn - The module match quality computation function for
155 /// @param JITMatchQualityFn - The JIT match quality computation function
157 static void RegisterTarget(Target &T,
159 const char *ShortDesc,
160 Target::TripleMatchQualityFnTy TQualityFn,
161 Target::ModuleMatchQualityFnTy MQualityFn,
162 Target::JITMatchQualityFnTy JITQualityFn);
164 /// RegisterTargetMachine - Register a TargetMachine implementation for the
167 /// Clients are responsible for ensuring that registration doesn't occur
168 /// while another thread is attempting to access the registry. Typically
169 /// this is done by initializing all targets at program startup.
171 /// @param T - The target being registered.
172 /// @param Fn - A function to construct a TargetMachine for the target.
173 static void RegisterTargetMachine(Target &T,
174 Target::TargetMachineCtorTy Fn) {
175 assert(!T.TargetMachineCtorFn && "Constructor already registered!");
176 T.TargetMachineCtorFn = Fn;
179 /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
182 /// Clients are responsible for ensuring that registration doesn't occur
183 /// while another thread is attempting to access the registry. Typically
184 /// this is done by initializing all targets at program startup.
186 /// @param T - The target being registered.
187 /// @param Fn - A function to construct an AsmPrinter for the target.
188 static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
189 assert(!T.AsmPrinterCtorFn && "Constructor already registered!");
190 T.AsmPrinterCtorFn = Fn;