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
10 #include "llvm/Support/TargetRegistry.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/Support/raw_ostream.h"
14 #include <cassert>
15 #include <vector>
16 using namespace llvm;
17
18 // Clients are responsible for avoid race conditions in registration.
19 static Target *FirstTarget = nullptr;
20
targets()21 iterator_range<TargetRegistry::iterator> TargetRegistry::targets() {
22 return make_range(iterator(FirstTarget), iterator());
23 }
24
lookupTarget(const std::string & ArchName,Triple & TheTriple,std::string & Error)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.
31 const Target *TheTarget = nullptr;
32 if (!ArchName.empty()) {
33 auto I =
34 std::find_if(targets().begin(), targets().end(),
35 [&](const Target &T) { return ArchName == T.getName(); });
36
37 if (I == targets().end()) {
38 Error = "error: invalid target '" + ArchName + "'.\n";
39 return nullptr;
40 }
41
42 TheTarget = &*I;
43
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);
53 if (!TheTarget) {
54 Error = ": error: unable to get target for '"
55 + TheTriple.getTriple()
56 + "', see --version and --triple.\n";
57 return nullptr;
58 }
59 }
60
61 return TheTarget;
62 }
63
lookupTarget(const std::string & TT,std::string & Error)64 const Target *TargetRegistry::lookupTarget(const std::string &TT,
65 std::string &Error) {
66 // Provide special warning when no targets are initialized.
67 if (targets().begin() == targets().end()) {
68 Error = "Unable to find target for this triple (no targets are registered)";
69 return nullptr;
70 }
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);
74
75 if (I == targets().end()) {
76 Error = "No available targets are compatible with this triple, "
77 "see -version for the available targets.";
78 return nullptr;
79 }
80
81 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;
89 }
90
RegisterTarget(Target & T,const char * Name,const char * ShortDesc,Target::ArchMatchFnTy ArchMatchFn,bool HasJIT)91 void TargetRegistry::RegisterTarget(Target &T,
92 const char *Name,
93 const char *ShortDesc,
94 Target::ArchMatchFnTy ArchMatchFn,
95 bool HasJIT) {
96 assert(Name && ShortDesc && ArchMatchFn &&
97 "Missing required target information!");
98
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;
103
104 // Add to the list of targets.
105 T.Next = FirstTarget;
106 FirstTarget = &T;
107
108 T.Name = Name;
109 T.ShortDesc = ShortDesc;
110 T.ArchMatchFn = ArchMatchFn;
111 T.HasJIT = HasJIT;
112 }
113
TargetArraySortFn(const std::pair<StringRef,const Target * > * LHS,const std::pair<StringRef,const Target * > * RHS)114 static 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
printRegisteredTargetsForVersion()119 void TargetRegistry::printRegisteredTargetsForVersion() {
120 std::vector<std::pair<StringRef, const Target*> > Targets;
121 size_t Width = 0;
122 for (const auto &T : TargetRegistry::targets()) {
123 Targets.push_back(std::make_pair(T.getName(), &T));
124 Width = std::max(Width, Targets.back().first.size());
125 }
126 array_pod_sort(Targets.begin(), Targets.end(), TargetArraySortFn);
127
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 }
138