Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 1 | //===--- TargetRegistry.cpp - Target registration -------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Evan Cheng | 3e74d6f | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 10 | #include "llvm/Support/TargetRegistry.h" |
Chandler Carruth | 077c408 | 2011-07-22 07:50:44 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/STLExtras.h" |
| 12 | #include "llvm/ADT/StringRef.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 13 | #include "llvm/Support/Host.h" |
Chandler Carruth | 077c408 | 2011-07-22 07:50:44 +0000 | [diff] [blame] | 14 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 15 | #include <cassert> |
Chandler Carruth | 077c408 | 2011-07-22 07:50:44 +0000 | [diff] [blame] | 16 | #include <vector> |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 17 | using namespace llvm; |
| 18 | |
Daniel Dunbar | 73b3ec4 | 2009-07-15 07:09:29 +0000 | [diff] [blame] | 19 | // Clients are responsible for avoid race conditions in registration. |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 20 | static Target *FirstTarget = 0; |
| 21 | |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 22 | TargetRegistry::iterator TargetRegistry::begin() { |
| 23 | return iterator(FirstTarget); |
| 24 | } |
| 25 | |
Daniel Dunbar | a5881e3 | 2009-07-26 02:12:58 +0000 | [diff] [blame] | 26 | const Target *TargetRegistry::lookupTarget(const std::string &TT, |
Daniel Dunbar | a5881e3 | 2009-07-26 02:12:58 +0000 | [diff] [blame] | 27 | std::string &Error) { |
Daniel Dunbar | 7df0c07 | 2009-07-17 15:50:49 +0000 | [diff] [blame] | 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)"; |
| 31 | return 0; |
| 32 | } |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 33 | const Target *Best = 0, *EquallyBest = 0; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 34 | unsigned BestQuality = 0; |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 35 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 36 | if (unsigned Qual = it->TripleMatchQualityFn(TT)) { |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 37 | if (!Best || Qual > BestQuality) { |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 38 | Best = &*it; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 39 | EquallyBest = 0; |
| 40 | BestQuality = Qual; |
| 41 | } else if (Qual == BestQuality) |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 42 | EquallyBest = &*it; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
| 46 | if (!Best) { |
Daniel Dunbar | 0247656 | 2009-09-08 23:32:35 +0000 | [diff] [blame] | 47 | Error = "No available targets are compatible with this triple, " |
| 48 | "see -version for the available targets."; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | // Otherwise, take the best target, but make sure we don't have two equally |
| 53 | // good best targets. |
| 54 | if (EquallyBest) { |
| 55 | Error = std::string("Cannot choose between targets \"") + |
| 56 | Best->Name + "\" and \"" + EquallyBest->Name + "\""; |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | return Best; |
| 61 | } |
| 62 | |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 63 | void TargetRegistry::RegisterTarget(Target &T, |
| 64 | const char *Name, |
| 65 | const char *ShortDesc, |
| 66 | Target::TripleMatchQualityFnTy TQualityFn, |
Daniel Dunbar | d6fd377 | 2009-07-25 10:09:50 +0000 | [diff] [blame] | 67 | bool HasJIT) { |
Daniel Dunbar | fa27ff2 | 2009-07-26 02:22:58 +0000 | [diff] [blame] | 68 | assert(Name && ShortDesc && TQualityFn && |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 69 | "Missing required target information!"); |
Daniel Dunbar | 51b198a | 2009-07-15 20:24:03 +0000 | [diff] [blame] | 70 | |
| 71 | // Check if this target has already been initialized, we allow this as a |
| 72 | // convenience to some clients. |
| 73 | if (T.Name) |
| 74 | return; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 75 | |
| 76 | // Add to the list of targets. |
| 77 | T.Next = FirstTarget; |
Daniel Dunbar | f23d493 | 2009-07-15 07:37:49 +0000 | [diff] [blame] | 78 | FirstTarget = &T; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 79 | |
| 80 | T.Name = Name; |
| 81 | T.ShortDesc = ShortDesc; |
| 82 | T.TripleMatchQualityFn = TQualityFn; |
Daniel Dunbar | d6fd377 | 2009-07-25 10:09:50 +0000 | [diff] [blame] | 83 | T.HasJIT = HasJIT; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Daniel Dunbar | 4bd03ab | 2009-08-03 04:20:57 +0000 | [diff] [blame] | 86 | const Target *TargetRegistry::getClosestTargetForJIT(std::string &Error) { |
Sebastian Pop | 6e9b5eb | 2012-01-05 18:28:46 +0000 | [diff] [blame] | 87 | const Target *TheTarget = lookupTarget(sys::getDefaultTargetTriple(), Error); |
Daniel Dunbar | 4bd03ab | 2009-08-03 04:20:57 +0000 | [diff] [blame] | 88 | |
| 89 | if (TheTarget && !TheTarget->hasJIT()) { |
| 90 | Error = "No JIT compatible target available for this host"; |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | return TheTarget; |
| 95 | } |
| 96 | |
Chandler Carruth | 077c408 | 2011-07-22 07:50:44 +0000 | [diff] [blame] | 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); |
| 100 | } |
| 101 | |
| 102 | void TargetRegistry::printRegisteredTargetsForVersion() { |
| 103 | std::vector<std::pair<StringRef, const Target*> > Targets; |
| 104 | size_t Width = 0; |
| 105 | for (TargetRegistry::iterator I = TargetRegistry::begin(), |
| 106 | E = TargetRegistry::end(); |
| 107 | I != E; ++I) { |
| 108 | Targets.push_back(std::make_pair(I->getName(), &*I)); |
| 109 | Width = std::max(Width, Targets.back().first.size()); |
| 110 | } |
| 111 | array_pod_sort(Targets.begin(), Targets.end(), TargetArraySortFn); |
| 112 | |
| 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'; |
| 119 | } |
| 120 | if (Targets.empty()) |
| 121 | OS << " (none)\n"; |
| 122 | } |