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/Support/TargetRegistry.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/StringRef.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 &ArchName,
29 // Allocate target machine. First, check whether the user has explicitly
30 // specified an architecture to compile for. If so we have to look it up by
31 // name, because it might be a backend that has no mapping to a target triple.
32 const Target *TheTarget = 0;
33 if (!ArchName.empty()) {
34 for (TargetRegistry::iterator it = TargetRegistry::begin(),
35 ie = TargetRegistry::end(); it != ie; ++it) {
36 if (ArchName == it->getName()) {
43 Error = "error: invalid target '" + ArchName + "'.\n";
47 // Adjust the triple to match (if known), otherwise stick with the
49 Triple::ArchType Type = Triple::getArchTypeForLLVMName(ArchName);
50 if (Type != Triple::UnknownArch)
51 TheTriple.setArch(Type);
53 // Get the target specific parser.
54 std::string TempError;
55 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), TempError);
57 Error = ": error: unable to get target for '"
58 + TheTriple.getTriple()
59 + "', see --version and --triple.\n";
67 const Target *TargetRegistry::lookupTarget(const std::string &TT,
69 // Provide special warning when no targets are initialized.
70 if (begin() == end()) {
71 Error = "Unable to find target for this triple (no targets are registered)";
74 const Target *Matching = 0;
75 Triple::ArchType Arch = Triple(TT).getArch();
76 for (iterator it = begin(), ie = end(); it != ie; ++it) {
77 if (it->ArchMatchFn(Arch)) {
79 Error = std::string("Cannot choose between targets \"") +
80 Matching->Name + "\" and \"" + it->Name + "\"";
88 Error = "No available targets are compatible with this triple, "
89 "see -version for the available targets.";
96 void TargetRegistry::RegisterTarget(Target &T,
98 const char *ShortDesc,
99 Target::ArchMatchFnTy ArchMatchFn,
101 assert(Name && ShortDesc && ArchMatchFn &&
102 "Missing required target information!");
104 // Check if this target has already been initialized, we allow this as a
105 // convenience to some clients.
109 // Add to the list of targets.
110 T.Next = FirstTarget;
114 T.ShortDesc = ShortDesc;
115 T.ArchMatchFn = ArchMatchFn;
119 const Target *TargetRegistry::getClosestTargetForJIT(std::string &Error) {
120 const Target *TheTarget = lookupTarget(sys::getDefaultTargetTriple(), Error);
122 if (TheTarget && !TheTarget->hasJIT()) {
123 Error = "No JIT compatible target available for this host";
130 void TargetRegistry::printRegisteredTargetsForVersion() {
131 std::vector<std::pair<StringRef, const Target*> > Targets;
133 for (TargetRegistry::iterator I = TargetRegistry::begin(),
134 E = TargetRegistry::end();
136 Targets.push_back(std::make_pair(I->getName(), &*I));
137 Width = std::max(Width, Targets.back().first.size());
139 array_pod_sort(Targets.begin(), Targets.end(),
140 [](const std::pair<StringRef, const Target *> *LHS,
141 const std::pair<StringRef, const Target *> *RHS) {
142 return LHS->first.compare(RHS->first);
145 raw_ostream &OS = outs();
146 OS << " Registered Targets:\n";
147 for (unsigned i = 0, e = Targets.size(); i != e; ++i) {
148 OS << " " << Targets[i].first;
149 OS.indent(Width - Targets[i].first.size()) << " - "
150 << Targets[i].second->getShortDescription() << '\n';