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/raw_ostream.h"
18 // Clients are responsible for avoid race conditions in registration.
19 static Target *FirstTarget = nullptr;
21 TargetRegistry::iterator TargetRegistry::begin() {
22 return iterator(FirstTarget);
25 const Target *TargetRegistry::lookupTarget(const std::string &ArchName,
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 for (TargetRegistry::iterator it = TargetRegistry::begin(),
34 ie = TargetRegistry::end(); it != ie; ++it) {
35 if (ArchName == it->getName()) {
42 Error = "error: invalid target '" + ArchName + "'.\n";
46 // Adjust the triple to match (if known), otherwise stick with the
48 Triple::ArchType Type = Triple::getArchTypeForLLVMName(ArchName);
49 if (Type != Triple::UnknownArch)
50 TheTriple.setArch(Type);
52 // Get the target specific parser.
53 std::string TempError;
54 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), TempError);
56 Error = ": error: unable to get target for '"
57 + TheTriple.getTriple()
58 + "', see --version and --triple.\n";
66 const Target *TargetRegistry::lookupTarget(const std::string &TT,
68 // Provide special warning when no targets are initialized.
69 if (begin() == end()) {
70 Error = "Unable to find target for this triple (no targets are registered)";
73 const Target *Matching = nullptr;
74 Triple::ArchType Arch = Triple(TT).getArch();
75 for (iterator it = begin(), ie = end(); it != ie; ++it) {
76 if (it->ArchMatchFn(Arch)) {
78 Error = std::string("Cannot choose between targets \"") +
79 Matching->Name + "\" and \"" + it->Name + "\"";
87 Error = "No available targets are compatible with this triple, "
88 "see -version for the available targets.";
95 void TargetRegistry::RegisterTarget(Target &T,
97 const char *ShortDesc,
98 Target::ArchMatchFnTy ArchMatchFn,
100 assert(Name && ShortDesc && ArchMatchFn &&
101 "Missing required target information!");
103 // Check if this target has already been initialized, we allow this as a
104 // convenience to some clients.
108 // Add to the list of targets.
109 T.Next = FirstTarget;
113 T.ShortDesc = ShortDesc;
114 T.ArchMatchFn = ArchMatchFn;
118 static int TargetArraySortFn(const std::pair<StringRef, const Target *> *LHS,
119 const std::pair<StringRef, const Target *> *RHS) {
120 return LHS->first.compare(RHS->first);
123 void TargetRegistry::printRegisteredTargetsForVersion() {
124 std::vector<std::pair<StringRef, const Target*> > Targets;
126 for (TargetRegistry::iterator I = TargetRegistry::begin(),
127 E = TargetRegistry::end();
129 Targets.push_back(std::make_pair(I->getName(), &*I));
130 Width = std::max(Width, Targets.back().first.size());
132 array_pod_sort(Targets.begin(), Targets.end(), TargetArraySortFn);
134 raw_ostream &OS = outs();
135 OS << " Registered Targets:\n";
136 for (unsigned i = 0, e = Targets.size(); i != e; ++i) {
137 OS << " " << Targets[i].first;
138 OS.indent(Width - Targets[i].first.size()) << " - "
139 << Targets[i].second->getShortDescription() << '\n';