blob: 9c81327615c6ee62ec2dd173afe8cf5a9be56e07 [file] [log] [blame]
Daniel Dunbarbb061292009-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 Cheng3e74d6f2011-08-24 18:08:43 +000010#include "llvm/Support/TargetRegistry.h"
Chandler Carruth077c4082011-07-22 07:50:44 +000011#include "llvm/ADT/STLExtras.h"
12#include "llvm/ADT/StringRef.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000013#include "llvm/Support/Host.h"
Chandler Carruth077c4082011-07-22 07:50:44 +000014#include "llvm/Support/raw_ostream.h"
Daniel Dunbarbb061292009-07-15 04:24:58 +000015#include <cassert>
Chandler Carruth077c4082011-07-22 07:50:44 +000016#include <vector>
Daniel Dunbarbb061292009-07-15 04:24:58 +000017using namespace llvm;
18
Daniel Dunbar73b3ec42009-07-15 07:09:29 +000019// Clients are responsible for avoid race conditions in registration.
Daniel Dunbarbb061292009-07-15 04:24:58 +000020static Target *FirstTarget = 0;
21
Daniel Dunbar603bea32009-07-16 02:06:09 +000022TargetRegistry::iterator TargetRegistry::begin() {
23 return iterator(FirstTarget);
24}
25
Kevin Enderby9ed9e5d2012-05-08 23:38:45 +000026const Target *TargetRegistry::lookupTarget(const std::string &ArchName,
27 Triple &TheTriple,
28 std::string &Error) {
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()) {
37 TheTarget = &*it;
38 break;
39 }
40 }
41
42 if (!TheTarget) {
43 Error = "error: invalid target '" + ArchName + "'.\n";
44 return 0;
45 }
46
47 // Adjust the triple to match (if known), otherwise stick with the
48 // given triple.
49 Triple::ArchType Type = Triple::getArchTypeForLLVMName(ArchName);
50 if (Type != Triple::UnknownArch)
51 TheTriple.setArch(Type);
52 } else {
53 // Get the target specific parser.
54 std::string TempError;
55 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), TempError);
56 if (TheTarget == 0) {
57 Error = ": error: unable to get target for '"
58 + TheTriple.getTriple()
59 + "', see --version and --triple.\n";
60 return 0;
61 }
62 }
63
64 return TheTarget;
65}
66
Daniel Dunbara5881e32009-07-26 02:12:58 +000067const Target *TargetRegistry::lookupTarget(const std::string &TT,
Daniel Dunbara5881e32009-07-26 02:12:58 +000068 std::string &Error) {
Daniel Dunbar7df0c072009-07-17 15:50:49 +000069 // 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)";
72 return 0;
73 }
Daniel Dunbar603bea32009-07-16 02:06:09 +000074 const Target *Best = 0, *EquallyBest = 0;
Daniel Dunbarbb061292009-07-15 04:24:58 +000075 unsigned BestQuality = 0;
Daniel Dunbar603bea32009-07-16 02:06:09 +000076 for (iterator it = begin(), ie = end(); it != ie; ++it) {
77 if (unsigned Qual = it->TripleMatchQualityFn(TT)) {
Daniel Dunbarbb061292009-07-15 04:24:58 +000078 if (!Best || Qual > BestQuality) {
Daniel Dunbar603bea32009-07-16 02:06:09 +000079 Best = &*it;
Daniel Dunbarbb061292009-07-15 04:24:58 +000080 EquallyBest = 0;
81 BestQuality = Qual;
82 } else if (Qual == BestQuality)
Daniel Dunbar603bea32009-07-16 02:06:09 +000083 EquallyBest = &*it;
Daniel Dunbarbb061292009-07-15 04:24:58 +000084 }
85 }
86
87 if (!Best) {
Daniel Dunbar02476562009-09-08 23:32:35 +000088 Error = "No available targets are compatible with this triple, "
89 "see -version for the available targets.";
Daniel Dunbarbb061292009-07-15 04:24:58 +000090 return 0;
91 }
92
93 // Otherwise, take the best target, but make sure we don't have two equally
94 // good best targets.
95 if (EquallyBest) {
96 Error = std::string("Cannot choose between targets \"") +
97 Best->Name + "\" and \"" + EquallyBest->Name + "\"";
98 return 0;
99 }
100
101 return Best;
102}
103
Daniel Dunbarbb061292009-07-15 04:24:58 +0000104void TargetRegistry::RegisterTarget(Target &T,
105 const char *Name,
106 const char *ShortDesc,
107 Target::TripleMatchQualityFnTy TQualityFn,
Daniel Dunbard6fd3772009-07-25 10:09:50 +0000108 bool HasJIT) {
Daniel Dunbarfa27ff22009-07-26 02:22:58 +0000109 assert(Name && ShortDesc && TQualityFn &&
Daniel Dunbarbb061292009-07-15 04:24:58 +0000110 "Missing required target information!");
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000111
112 // Check if this target has already been initialized, we allow this as a
113 // convenience to some clients.
114 if (T.Name)
115 return;
Daniel Dunbarbb061292009-07-15 04:24:58 +0000116
117 // Add to the list of targets.
118 T.Next = FirstTarget;
Daniel Dunbarf23d4932009-07-15 07:37:49 +0000119 FirstTarget = &T;
Daniel Dunbarbb061292009-07-15 04:24:58 +0000120
121 T.Name = Name;
122 T.ShortDesc = ShortDesc;
123 T.TripleMatchQualityFn = TQualityFn;
Daniel Dunbard6fd3772009-07-25 10:09:50 +0000124 T.HasJIT = HasJIT;
Daniel Dunbarbb061292009-07-15 04:24:58 +0000125}
126
Daniel Dunbar4bd03ab2009-08-03 04:20:57 +0000127const Target *TargetRegistry::getClosestTargetForJIT(std::string &Error) {
Sebastian Pop6e9b5eb2012-01-05 18:28:46 +0000128 const Target *TheTarget = lookupTarget(sys::getDefaultTargetTriple(), Error);
Daniel Dunbar4bd03ab2009-08-03 04:20:57 +0000129
130 if (TheTarget && !TheTarget->hasJIT()) {
131 Error = "No JIT compatible target available for this host";
132 return 0;
133 }
134
135 return TheTarget;
136}
137
Chandler Carruth077c4082011-07-22 07:50:44 +0000138static int TargetArraySortFn(const void *LHS, const void *RHS) {
139 typedef std::pair<StringRef, const Target*> pair_ty;
140 return ((const pair_ty*)LHS)->first.compare(((const pair_ty*)RHS)->first);
141}
142
143void TargetRegistry::printRegisteredTargetsForVersion() {
144 std::vector<std::pair<StringRef, const Target*> > Targets;
145 size_t Width = 0;
146 for (TargetRegistry::iterator I = TargetRegistry::begin(),
147 E = TargetRegistry::end();
148 I != E; ++I) {
149 Targets.push_back(std::make_pair(I->getName(), &*I));
150 Width = std::max(Width, Targets.back().first.size());
151 }
152 array_pod_sort(Targets.begin(), Targets.end(), TargetArraySortFn);
153
154 raw_ostream &OS = outs();
155 OS << " Registered Targets:\n";
156 for (unsigned i = 0, e = Targets.size(); i != e; ++i) {
157 OS << " " << Targets[i].first;
158 OS.indent(Width - Targets[i].first.size()) << " - "
159 << Targets[i].second->getShortDescription() << '\n';
160 }
161 if (Targets.empty())
162 OS << " (none)\n";
163}