blob: 53c8d84e7d45882c548863becfe17ffef61d8c7a [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
Daniel Dunbara5881e32009-07-26 02:12:58 +000026const Target *TargetRegistry::lookupTarget(const std::string &TT,
Daniel Dunbara5881e32009-07-26 02:12:58 +000027 std::string &Error) {
Daniel Dunbar7df0c072009-07-17 15:50:49 +000028 // Provide special warning when no targets are initialized.
29 if (begin() == end()) {
30 Error = "Unable to find target for this triple (no targets are registered)";
31 return 0;
32 }
Daniel Dunbar603bea32009-07-16 02:06:09 +000033 const Target *Best = 0, *EquallyBest = 0;
Daniel Dunbarbb061292009-07-15 04:24:58 +000034 unsigned BestQuality = 0;
Daniel Dunbar603bea32009-07-16 02:06:09 +000035 for (iterator it = begin(), ie = end(); it != ie; ++it) {
36 if (unsigned Qual = it->TripleMatchQualityFn(TT)) {
Daniel Dunbarbb061292009-07-15 04:24:58 +000037 if (!Best || Qual > BestQuality) {
Daniel Dunbar603bea32009-07-16 02:06:09 +000038 Best = &*it;
Daniel Dunbarbb061292009-07-15 04:24:58 +000039 EquallyBest = 0;
40 BestQuality = Qual;
41 } else if (Qual == BestQuality)
Daniel Dunbar603bea32009-07-16 02:06:09 +000042 EquallyBest = &*it;
Daniel Dunbarbb061292009-07-15 04:24:58 +000043 }
44 }
45
46 if (!Best) {
Daniel Dunbar02476562009-09-08 23:32:35 +000047 Error = "No available targets are compatible with this triple, "
48 "see -version for the available targets.";
Daniel Dunbarbb061292009-07-15 04:24:58 +000049 return 0;
50 }
51
52 // Otherwise, take the best target, but make sure we don't have two equally
53 // good best targets.
54 if (EquallyBest) {
55 Error = std::string("Cannot choose between targets \"") +
56 Best->Name + "\" and \"" + EquallyBest->Name + "\"";
57 return 0;
58 }
59
60 return Best;
61}
62
Daniel Dunbarbb061292009-07-15 04:24:58 +000063void TargetRegistry::RegisterTarget(Target &T,
64 const char *Name,
65 const char *ShortDesc,
66 Target::TripleMatchQualityFnTy TQualityFn,
Daniel Dunbard6fd3772009-07-25 10:09:50 +000067 bool HasJIT) {
Daniel Dunbarfa27ff22009-07-26 02:22:58 +000068 assert(Name && ShortDesc && TQualityFn &&
Daniel Dunbarbb061292009-07-15 04:24:58 +000069 "Missing required target information!");
Daniel Dunbar51b198a2009-07-15 20:24:03 +000070
71 // Check if this target has already been initialized, we allow this as a
72 // convenience to some clients.
73 if (T.Name)
74 return;
Daniel Dunbarbb061292009-07-15 04:24:58 +000075
76 // Add to the list of targets.
77 T.Next = FirstTarget;
Daniel Dunbarf23d4932009-07-15 07:37:49 +000078 FirstTarget = &T;
Daniel Dunbarbb061292009-07-15 04:24:58 +000079
80 T.Name = Name;
81 T.ShortDesc = ShortDesc;
82 T.TripleMatchQualityFn = TQualityFn;
Daniel Dunbard6fd3772009-07-25 10:09:50 +000083 T.HasJIT = HasJIT;
Daniel Dunbarbb061292009-07-15 04:24:58 +000084}
85
Daniel Dunbar4bd03ab2009-08-03 04:20:57 +000086const Target *TargetRegistry::getClosestTargetForJIT(std::string &Error) {
Sebastian Pop6e9b5eb2012-01-05 18:28:46 +000087 const Target *TheTarget = lookupTarget(sys::getDefaultTargetTriple(), Error);
Daniel Dunbar4bd03ab2009-08-03 04:20:57 +000088
89 if (TheTarget && !TheTarget->hasJIT()) {
90 Error = "No JIT compatible target available for this host";
91 return 0;
92 }
93
94 return TheTarget;
95}
96
Chandler Carruth077c4082011-07-22 07:50:44 +000097static int TargetArraySortFn(const void *LHS, const void *RHS) {
98 typedef std::pair<StringRef, const Target*> pair_ty;
99 return ((const pair_ty*)LHS)->first.compare(((const pair_ty*)RHS)->first);
100}
101
102void TargetRegistry::printRegisteredTargetsForVersion() {
103 std::vector<std::pair<StringRef, const Target*> > Targets;
104 size_t Width = 0;
105 for (TargetRegistry::iterator I = TargetRegistry::begin(),
106 E = TargetRegistry::end();
107 I != E; ++I) {
108 Targets.push_back(std::make_pair(I->getName(), &*I));
109 Width = std::max(Width, Targets.back().first.size());
110 }
111 array_pod_sort(Targets.begin(), Targets.end(), TargetArraySortFn);
112
113 raw_ostream &OS = outs();
114 OS << " Registered Targets:\n";
115 for (unsigned i = 0, e = Targets.size(); i != e; ++i) {
116 OS << " " << Targets[i].first;
117 OS.indent(Width - Targets[i].first.size()) << " - "
118 << Targets[i].second->getShortDescription() << '\n';
119 }
120 if (Targets.empty())
121 OS << " (none)\n";
122}