Daniel Dunbar | 078a71e | 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 | 2bb4035 | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 10 | #include "llvm/Support/TargetRegistry.h" |
Chandler Carruth | 2baac02 | 2011-07-22 07:50:44 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/STLExtras.h" |
| 12 | #include "llvm/ADT/StringRef.h" |
Chandler Carruth | 2baac02 | 2011-07-22 07:50:44 +0000 | [diff] [blame] | 13 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 078a71e | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 14 | #include <cassert> |
Chandler Carruth | 2baac02 | 2011-07-22 07:50:44 +0000 | [diff] [blame] | 15 | #include <vector> |
Daniel Dunbar | 078a71e | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 16 | using namespace llvm; |
| 17 | |
Daniel Dunbar | 1c66332 | 2009-07-15 07:09:29 +0000 | [diff] [blame] | 18 | // Clients are responsible for avoid race conditions in registration. |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 19 | static Target *FirstTarget = nullptr; |
Daniel Dunbar | 078a71e | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 20 | |
David Blaikie | 46c561c | 2015-05-11 22:20:48 +0000 | [diff] [blame] | 21 | iterator_range<TargetRegistry::iterator> TargetRegistry::targets() { |
| 22 | return make_range(iterator(FirstTarget), iterator()); |
Daniel Dunbar | 78c8fb1 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 23 | } |
| 24 | |
Kevin Enderby | fe3d005 | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 25 | const Target *TargetRegistry::lookupTarget(const std::string &ArchName, |
| 26 | Triple &TheTriple, |
| 27 | std::string &Error) { |
| 28 | // Allocate target machine. First, check whether the user has explicitly |
| 29 | // specified an architecture to compile for. If so we have to look it up by |
| 30 | // name, because it might be a backend that has no mapping to a target triple. |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 31 | const Target *TheTarget = nullptr; |
Kevin Enderby | fe3d005 | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 32 | if (!ArchName.empty()) { |
David Blaikie | 46c561c | 2015-05-11 22:20:48 +0000 | [diff] [blame] | 33 | auto I = |
| 34 | std::find_if(targets().begin(), targets().end(), |
| 35 | [&](const Target &T) { return ArchName == T.getName(); }); |
Kevin Enderby | fe3d005 | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 36 | |
David Blaikie | 46c561c | 2015-05-11 22:20:48 +0000 | [diff] [blame] | 37 | if (I == targets().end()) { |
Kevin Enderby | fe3d005 | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 38 | Error = "error: invalid target '" + ArchName + "'.\n"; |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 39 | return nullptr; |
Kevin Enderby | fe3d005 | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 40 | } |
| 41 | |
David Blaikie | 46c561c | 2015-05-11 22:20:48 +0000 | [diff] [blame] | 42 | TheTarget = &*I; |
| 43 | |
Kevin Enderby | fe3d005 | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 44 | // Adjust the triple to match (if known), otherwise stick with the |
| 45 | // given triple. |
| 46 | Triple::ArchType Type = Triple::getArchTypeForLLVMName(ArchName); |
| 47 | if (Type != Triple::UnknownArch) |
| 48 | TheTriple.setArch(Type); |
| 49 | } else { |
| 50 | // Get the target specific parser. |
| 51 | std::string TempError; |
| 52 | TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), TempError); |
Craig Topper | 8d399f8 | 2014-04-09 04:20:00 +0000 | [diff] [blame] | 53 | if (!TheTarget) { |
Kevin Enderby | fe3d005 | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 54 | Error = ": error: unable to get target for '" |
| 55 | + TheTriple.getTriple() |
| 56 | + "', see --version and --triple.\n"; |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 57 | return nullptr; |
Kevin Enderby | fe3d005 | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
| 61 | return TheTarget; |
| 62 | } |
| 63 | |
Daniel Dunbar | 47d6791 | 2009-07-26 02:12:58 +0000 | [diff] [blame] | 64 | const Target *TargetRegistry::lookupTarget(const std::string &TT, |
Daniel Dunbar | 47d6791 | 2009-07-26 02:12:58 +0000 | [diff] [blame] | 65 | std::string &Error) { |
Daniel Dunbar | 50745bf | 2009-07-17 15:50:49 +0000 | [diff] [blame] | 66 | // Provide special warning when no targets are initialized. |
David Blaikie | 46c561c | 2015-05-11 22:20:48 +0000 | [diff] [blame] | 67 | if (targets().begin() == targets().end()) { |
Daniel Dunbar | 50745bf | 2009-07-17 15:50:49 +0000 | [diff] [blame] | 68 | Error = "Unable to find target for this triple (no targets are registered)"; |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 69 | return nullptr; |
Daniel Dunbar | 50745bf | 2009-07-17 15:50:49 +0000 | [diff] [blame] | 70 | } |
David Blaikie | 46c561c | 2015-05-11 22:20:48 +0000 | [diff] [blame] | 71 | Triple::ArchType Arch = Triple(TT).getArch(); |
| 72 | auto ArchMatch = [&](const Target &T) { return T.ArchMatchFn(Arch); }; |
| 73 | auto I = std::find_if(targets().begin(), targets().end(), ArchMatch); |
Daniel Dunbar | 078a71e | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 74 | |
David Blaikie | 46c561c | 2015-05-11 22:20:48 +0000 | [diff] [blame] | 75 | if (I == targets().end()) { |
Rafael Espindola | 26e6581 | 2016-01-28 22:55:45 +0000 | [diff] [blame] | 76 | Error = "No available targets are compatible with this triple."; |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 77 | return nullptr; |
Daniel Dunbar | 078a71e | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 78 | } |
| 79 | |
David Blaikie | 46c561c | 2015-05-11 22:20:48 +0000 | [diff] [blame] | 80 | auto J = std::find_if(std::next(I), targets().end(), ArchMatch); |
| 81 | if (J != targets().end()) { |
| 82 | Error = std::string("Cannot choose between targets \"") + I->Name + |
| 83 | "\" and \"" + J->Name + "\""; |
| 84 | return nullptr; |
| 85 | } |
| 86 | |
| 87 | return &*I; |
Daniel Dunbar | 078a71e | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Daniel Dunbar | 078a71e | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 90 | void TargetRegistry::RegisterTarget(Target &T, |
| 91 | const char *Name, |
| 92 | const char *ShortDesc, |
Rafael Espindola | dfc1470 | 2013-12-13 16:05:32 +0000 | [diff] [blame] | 93 | Target::ArchMatchFnTy ArchMatchFn, |
Daniel Dunbar | 691a478 | 2009-07-25 10:09:50 +0000 | [diff] [blame] | 94 | bool HasJIT) { |
Rafael Espindola | dfc1470 | 2013-12-13 16:05:32 +0000 | [diff] [blame] | 95 | assert(Name && ShortDesc && ArchMatchFn && |
Daniel Dunbar | 078a71e | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 96 | "Missing required target information!"); |
Daniel Dunbar | e833810 | 2009-07-15 20:24:03 +0000 | [diff] [blame] | 97 | |
| 98 | // Check if this target has already been initialized, we allow this as a |
| 99 | // convenience to some clients. |
| 100 | if (T.Name) |
| 101 | return; |
Daniel Dunbar | 078a71e | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 102 | |
| 103 | // Add to the list of targets. |
| 104 | T.Next = FirstTarget; |
Daniel Dunbar | 54995de | 2009-07-15 07:37:49 +0000 | [diff] [blame] | 105 | FirstTarget = &T; |
Daniel Dunbar | 078a71e | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 106 | |
| 107 | T.Name = Name; |
| 108 | T.ShortDesc = ShortDesc; |
Rafael Espindola | dfc1470 | 2013-12-13 16:05:32 +0000 | [diff] [blame] | 109 | T.ArchMatchFn = ArchMatchFn; |
Daniel Dunbar | 691a478 | 2009-07-25 10:09:50 +0000 | [diff] [blame] | 110 | T.HasJIT = HasJIT; |
Daniel Dunbar | 078a71e | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Benjamin Kramer | adf1ea8 | 2014-03-07 21:52:38 +0000 | [diff] [blame] | 113 | static int TargetArraySortFn(const std::pair<StringRef, const Target *> *LHS, |
| 114 | const std::pair<StringRef, const Target *> *RHS) { |
| 115 | return LHS->first.compare(RHS->first); |
| 116 | } |
| 117 | |
Chandler Carruth | 2baac02 | 2011-07-22 07:50:44 +0000 | [diff] [blame] | 118 | void TargetRegistry::printRegisteredTargetsForVersion() { |
| 119 | std::vector<std::pair<StringRef, const Target*> > Targets; |
| 120 | size_t Width = 0; |
David Blaikie | 46c561c | 2015-05-11 22:20:48 +0000 | [diff] [blame] | 121 | for (const auto &T : TargetRegistry::targets()) { |
| 122 | Targets.push_back(std::make_pair(T.getName(), &T)); |
Chandler Carruth | 2baac02 | 2011-07-22 07:50:44 +0000 | [diff] [blame] | 123 | Width = std::max(Width, Targets.back().first.size()); |
| 124 | } |
Benjamin Kramer | adf1ea8 | 2014-03-07 21:52:38 +0000 | [diff] [blame] | 125 | array_pod_sort(Targets.begin(), Targets.end(), TargetArraySortFn); |
Chandler Carruth | 2baac02 | 2011-07-22 07:50:44 +0000 | [diff] [blame] | 126 | |
| 127 | raw_ostream &OS = outs(); |
| 128 | OS << " Registered Targets:\n"; |
| 129 | for (unsigned i = 0, e = Targets.size(); i != e; ++i) { |
| 130 | OS << " " << Targets[i].first; |
| 131 | OS.indent(Width - Targets[i].first.size()) << " - " |
| 132 | << Targets[i].second->getShortDescription() << '\n'; |
| 133 | } |
| 134 | if (Targets.empty()) |
| 135 | OS << " (none)\n"; |
| 136 | } |