blob: eefef8ad8eaa595631c2ee73e23849b1c186f13e [file] [log] [blame]
Daniel Dunbar078a71e2009-07-15 04:24:58 +00001//===--- 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 Cheng2bb40352011-08-24 18:08:43 +000010#include "llvm/Support/TargetRegistry.h"
Chandler Carruth2baac022011-07-22 07:50:44 +000011#include "llvm/ADT/STLExtras.h"
12#include "llvm/ADT/StringRef.h"
Chandler Carruth2baac022011-07-22 07:50:44 +000013#include "llvm/Support/raw_ostream.h"
Daniel Dunbar078a71e2009-07-15 04:24:58 +000014#include <cassert>
Chandler Carruth2baac022011-07-22 07:50:44 +000015#include <vector>
Daniel Dunbar078a71e2009-07-15 04:24:58 +000016using namespace llvm;
17
Daniel Dunbar1c663322009-07-15 07:09:29 +000018// Clients are responsible for avoid race conditions in registration.
Craig Topperc10719f2014-04-07 04:17:22 +000019static Target *FirstTarget = nullptr;
Daniel Dunbar078a71e2009-07-15 04:24:58 +000020
David Blaikie46c561c2015-05-11 22:20:48 +000021iterator_range<TargetRegistry::iterator> TargetRegistry::targets() {
22 return make_range(iterator(FirstTarget), iterator());
Daniel Dunbar78c8fb12009-07-16 02:06:09 +000023}
24
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000025const 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 Topperc10719f2014-04-07 04:17:22 +000031 const Target *TheTarget = nullptr;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000032 if (!ArchName.empty()) {
David Blaikie46c561c2015-05-11 22:20:48 +000033 auto I =
34 std::find_if(targets().begin(), targets().end(),
35 [&](const Target &T) { return ArchName == T.getName(); });
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000036
David Blaikie46c561c2015-05-11 22:20:48 +000037 if (I == targets().end()) {
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000038 Error = "error: invalid target '" + ArchName + "'.\n";
Craig Topperc10719f2014-04-07 04:17:22 +000039 return nullptr;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000040 }
41
David Blaikie46c561c2015-05-11 22:20:48 +000042 TheTarget = &*I;
43
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000044 // 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 Topper8d399f82014-04-09 04:20:00 +000053 if (!TheTarget) {
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000054 Error = ": error: unable to get target for '"
55 + TheTriple.getTriple()
56 + "', see --version and --triple.\n";
Craig Topperc10719f2014-04-07 04:17:22 +000057 return nullptr;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000058 }
59 }
60
61 return TheTarget;
62}
63
Daniel Dunbar47d67912009-07-26 02:12:58 +000064const Target *TargetRegistry::lookupTarget(const std::string &TT,
Daniel Dunbar47d67912009-07-26 02:12:58 +000065 std::string &Error) {
Daniel Dunbar50745bf2009-07-17 15:50:49 +000066 // Provide special warning when no targets are initialized.
David Blaikie46c561c2015-05-11 22:20:48 +000067 if (targets().begin() == targets().end()) {
Daniel Dunbar50745bf2009-07-17 15:50:49 +000068 Error = "Unable to find target for this triple (no targets are registered)";
Craig Topperc10719f2014-04-07 04:17:22 +000069 return nullptr;
Daniel Dunbar50745bf2009-07-17 15:50:49 +000070 }
David Blaikie46c561c2015-05-11 22:20:48 +000071 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 Dunbar078a71e2009-07-15 04:24:58 +000074
David Blaikie46c561c2015-05-11 22:20:48 +000075 if (I == targets().end()) {
Daniel Dunbar6119d872009-09-08 23:32:35 +000076 Error = "No available targets are compatible with this triple, "
77 "see -version for the available targets.";
Craig Topperc10719f2014-04-07 04:17:22 +000078 return nullptr;
Daniel Dunbar078a71e2009-07-15 04:24:58 +000079 }
80
David Blaikie46c561c2015-05-11 22:20:48 +000081 auto J = std::find_if(std::next(I), targets().end(), ArchMatch);
82 if (J != targets().end()) {
83 Error = std::string("Cannot choose between targets \"") + I->Name +
84 "\" and \"" + J->Name + "\"";
85 return nullptr;
86 }
87
88 return &*I;
Daniel Dunbar078a71e2009-07-15 04:24:58 +000089}
90
Daniel Dunbar078a71e2009-07-15 04:24:58 +000091void TargetRegistry::RegisterTarget(Target &T,
92 const char *Name,
93 const char *ShortDesc,
Rafael Espindoladfc14702013-12-13 16:05:32 +000094 Target::ArchMatchFnTy ArchMatchFn,
Daniel Dunbar691a4782009-07-25 10:09:50 +000095 bool HasJIT) {
Rafael Espindoladfc14702013-12-13 16:05:32 +000096 assert(Name && ShortDesc && ArchMatchFn &&
Daniel Dunbar078a71e2009-07-15 04:24:58 +000097 "Missing required target information!");
Daniel Dunbare8338102009-07-15 20:24:03 +000098
99 // Check if this target has already been initialized, we allow this as a
100 // convenience to some clients.
101 if (T.Name)
102 return;
Daniel Dunbar078a71e2009-07-15 04:24:58 +0000103
104 // Add to the list of targets.
105 T.Next = FirstTarget;
Daniel Dunbar54995de2009-07-15 07:37:49 +0000106 FirstTarget = &T;
Daniel Dunbar078a71e2009-07-15 04:24:58 +0000107
108 T.Name = Name;
109 T.ShortDesc = ShortDesc;
Rafael Espindoladfc14702013-12-13 16:05:32 +0000110 T.ArchMatchFn = ArchMatchFn;
Daniel Dunbar691a4782009-07-25 10:09:50 +0000111 T.HasJIT = HasJIT;
Daniel Dunbar078a71e2009-07-15 04:24:58 +0000112}
113
Benjamin Krameradf1ea82014-03-07 21:52:38 +0000114static int TargetArraySortFn(const std::pair<StringRef, const Target *> *LHS,
115 const std::pair<StringRef, const Target *> *RHS) {
116 return LHS->first.compare(RHS->first);
117}
118
Chandler Carruth2baac022011-07-22 07:50:44 +0000119void TargetRegistry::printRegisteredTargetsForVersion() {
120 std::vector<std::pair<StringRef, const Target*> > Targets;
121 size_t Width = 0;
David Blaikie46c561c2015-05-11 22:20:48 +0000122 for (const auto &T : TargetRegistry::targets()) {
123 Targets.push_back(std::make_pair(T.getName(), &T));
Chandler Carruth2baac022011-07-22 07:50:44 +0000124 Width = std::max(Width, Targets.back().first.size());
125 }
Benjamin Krameradf1ea82014-03-07 21:52:38 +0000126 array_pod_sort(Targets.begin(), Targets.end(), TargetArraySortFn);
Chandler Carruth2baac022011-07-22 07:50:44 +0000127
128 raw_ostream &OS = outs();
129 OS << " Registered Targets:\n";
130 for (unsigned i = 0, e = Targets.size(); i != e; ++i) {
131 OS << " " << Targets[i].first;
132 OS.indent(Width - Targets[i].first.size()) << " - "
133 << Targets[i].second->getShortDescription() << '\n';
134 }
135 if (Targets.empty())
136 OS << " (none)\n";
137}