1 //===--- TargetRegistry.cpp - Target registration -------------------------===//
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 #include "llvm/Support/TargetRegistry.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/Support/Host.h"
14 #include "llvm/Support/raw_ostream.h"
19 // Clients are responsible for avoid race conditions in registration.
20 static Target *FirstTarget = 0;
22 TargetRegistry::iterator TargetRegistry::begin() {
23 return iterator(FirstTarget);
26 const Target *TargetRegistry::lookupTarget(const std::string &ArchName,
29 // Allocate target machine. First, check whether the user has explicitly
30 // specified an architecture to compile for. If so we have to look it up by
31 // name, because it might be a backend that has no mapping to a target triple.
32 const Target *TheTarget = 0;
33 if (!ArchName.empty()) {
34 for (TargetRegistry::iterator it = TargetRegistry::begin(),
35 ie = TargetRegistry::end(); it != ie; ++it) {
36 if (ArchName == it->getName()) {
43 Error = "error: invalid target '" + ArchName + "'.\n";
47 // Adjust the triple to match (if known), otherwise stick with the
49 Triple::ArchType Type = Triple::getArchTypeForLLVMName(ArchName);
50 if (Type != Triple::UnknownArch)
51 TheTriple.setArch(Type);
53 // Get the target specific parser.
54 std::string TempError;
55 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), TempError);
57 Error = ": error: unable to get target for '"
58 + TheTriple.getTriple()
59 + "', see --version and --triple.\n";
67 const Target *TargetRegistry::lookupTarget(const std::string &TT,
69 // Provide special warning when no targets are initialized.
70 if (begin() == end()) {
71 Error = "Unable to find target for this triple (no targets are registered)";
74 const Target *Best = 0, *EquallyBest = 0;
75 unsigned BestQuality = 0;
76 for (iterator it = begin(), ie = end(); it != ie; ++it) {
77 if (unsigned Qual = it->TripleMatchQualityFn(TT)) {
78 if (!Best || Qual > BestQuality) {
82 } else if (Qual == BestQuality)
88 Error = "No available targets are compatible with this triple, "
89 "see -version for the available targets.";
93 // Otherwise, take the best target, but make sure we don't have two equally
96 Error = std::string("Cannot choose between targets \"") +
97 Best->Name + "\" and \"" + EquallyBest->Name + "\"";
104 void TargetRegistry::RegisterTarget(Target &T,
106 const char *ShortDesc,
107 Target::TripleMatchQualityFnTy TQualityFn,
109 assert(Name && ShortDesc && TQualityFn &&
110 "Missing required target information!");
112 // Check if this target has already been initialized, we allow this as a
113 // convenience to some clients.
117 // Add to the list of targets.
118 T.Next = FirstTarget;
122 T.ShortDesc = ShortDesc;
123 T.TripleMatchQualityFn = TQualityFn;
127 const Target *TargetRegistry::getClosestTargetForJIT(std::string &Error) {
128 const Target *TheTarget = lookupTarget(sys::getDefaultTargetTriple(), Error);
130 if (TheTarget && !TheTarget->hasJIT()) {
131 Error = "No JIT compatible target available for this host";
138 static int TargetArraySortFn(const void *LHS, const void *RHS) {
139 typedef std::pair<StringRef, const Target*> pair_ty;
140 return ((const pair_ty*)LHS)->first.compare(((const pair_ty*)RHS)->first);
143 void TargetRegistry::printRegisteredTargetsForVersion() {
144 std::vector<std::pair<StringRef, const Target*> > Targets;
146 for (TargetRegistry::iterator I = TargetRegistry::begin(),
147 E = TargetRegistry::end();
149 Targets.push_back(std::make_pair(I->getName(), &*I));
150 Width = std::max(Width, Targets.back().first.size());
152 array_pod_sort(Targets.begin(), Targets.end(), TargetArraySortFn);
154 raw_ostream &OS = outs();
155 OS << " Registered Targets:\n";
156 for (unsigned i = 0, e = Targets.size(); i != e; ++i) {
157 OS << " " << Targets[i].first;
158 OS.indent(Width - Targets[i].first.size()) << " - "
159 << Targets[i].second->getShortDescription() << '\n';