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/ADT/STLExtras.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/Target/TargetRegistry.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 &TT,
28 // Provide special warning when no targets are initialized.
29 if (begin() == end()) {
30 Error = "Unable to find target for this triple (no targets are registered)";
33 const Target *Best = 0, *EquallyBest = 0;
34 unsigned BestQuality = 0;
35 for (iterator it = begin(), ie = end(); it != ie; ++it) {
36 if (unsigned Qual = it->TripleMatchQualityFn(TT)) {
37 if (!Best || Qual > BestQuality) {
41 } else if (Qual == BestQuality)
47 Error = "No available targets are compatible with this triple, "
48 "see -version for the available targets.";
52 // Otherwise, take the best target, but make sure we don't have two equally
55 Error = std::string("Cannot choose between targets \"") +
56 Best->Name + "\" and \"" + EquallyBest->Name + "\"";
63 void TargetRegistry::RegisterTarget(Target &T,
65 const char *ShortDesc,
66 Target::TripleMatchQualityFnTy TQualityFn,
68 assert(Name && ShortDesc && TQualityFn &&
69 "Missing required target information!");
71 // Check if this target has already been initialized, we allow this as a
72 // convenience to some clients.
76 // Add to the list of targets.
81 T.ShortDesc = ShortDesc;
82 T.TripleMatchQualityFn = TQualityFn;
86 const Target *TargetRegistry::getClosestTargetForJIT(std::string &Error) {
87 const Target *TheTarget = lookupTarget(sys::getHostTriple(), Error);
89 if (TheTarget && !TheTarget->hasJIT()) {
90 Error = "No JIT compatible target available for this host";
97 static int TargetArraySortFn(const void *LHS, const void *RHS) {
98 typedef std::pair<StringRef, const Target*> pair_ty;
99 return ((const pair_ty*)LHS)->first.compare(((const pair_ty*)RHS)->first);
102 void TargetRegistry::printRegisteredTargetsForVersion() {
103 std::vector<std::pair<StringRef, const Target*> > Targets;
105 for (TargetRegistry::iterator I = TargetRegistry::begin(),
106 E = TargetRegistry::end();
108 Targets.push_back(std::make_pair(I->getName(), &*I));
109 Width = std::max(Width, Targets.back().first.size());
111 array_pod_sort(Targets.begin(), Targets.end(), TargetArraySortFn);
113 raw_ostream &OS = outs();
114 OS << " Registered Targets:\n";
115 for (unsigned i = 0, e = Targets.size(); i != e; ++i) {
116 OS << " " << Targets[i].first;
117 OS.indent(Width - Targets[i].first.size()) << " - "
118 << Targets[i].second->getShortDescription() << '\n';