blob: f6918835f74bed5833cfb1edfc3a09c9bd696069 [file] [log] [blame]
Daniel Dunbar078a71e2009-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 Cheng2bb40352011-08-24 18:08:43 +000010#include "llvm/Support/TargetRegistry.h"
Chandler Carruth2baac022011-07-22 07:50:44 +000011#include "llvm/ADT/STLExtras.h"
12#include "llvm/ADT/StringRef.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000013#include "llvm/Support/Host.h"
Chandler Carruth2baac022011-07-22 07:50:44 +000014#include "llvm/Support/raw_ostream.h"
Daniel Dunbar078a71e2009-07-15 04:24:58 +000015#include <cassert>
Chandler Carruth2baac022011-07-22 07:50:44 +000016#include <vector>
Daniel Dunbar078a71e2009-07-15 04:24:58 +000017using namespace llvm;
18
Daniel Dunbar1c663322009-07-15 07:09:29 +000019// Clients are responsible for avoid race conditions in registration.
Craig Topperc10719f2014-04-07 04:17:22 +000020static Target *FirstTarget = nullptr;
Daniel Dunbar078a71e2009-07-15 04:24:58 +000021
Daniel Dunbar78c8fb12009-07-16 02:06:09 +000022TargetRegistry::iterator TargetRegistry::begin() {
23 return iterator(FirstTarget);
24}
25
Kevin Enderbyfe3d0052012-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.
Craig Topperc10719f2014-04-07 04:17:22 +000032 const Target *TheTarget = nullptr;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000033 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";
Craig Topperc10719f2014-04-07 04:17:22 +000044 return nullptr;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000045 }
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);
Craig Topper8d399f82014-04-09 04:20:00 +000056 if (!TheTarget) {
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000057 Error = ": error: unable to get target for '"
58 + TheTriple.getTriple()
59 + "', see --version and --triple.\n";
Craig Topperc10719f2014-04-07 04:17:22 +000060 return nullptr;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000061 }
62 }
63
64 return TheTarget;
65}
66
Daniel Dunbar47d67912009-07-26 02:12:58 +000067const Target *TargetRegistry::lookupTarget(const std::string &TT,
Daniel Dunbar47d67912009-07-26 02:12:58 +000068 std::string &Error) {
Daniel Dunbar50745bf2009-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)";
Craig Topperc10719f2014-04-07 04:17:22 +000072 return nullptr;
Daniel Dunbar50745bf2009-07-17 15:50:49 +000073 }
Craig Topperc10719f2014-04-07 04:17:22 +000074 const Target *Matching = nullptr;
Rafael Espindoladfc14702013-12-13 16:05:32 +000075 Triple::ArchType Arch = Triple(TT).getArch();
Daniel Dunbar78c8fb12009-07-16 02:06:09 +000076 for (iterator it = begin(), ie = end(); it != ie; ++it) {
Rafael Espindoladfc14702013-12-13 16:05:32 +000077 if (it->ArchMatchFn(Arch)) {
78 if (Matching) {
79 Error = std::string("Cannot choose between targets \"") +
80 Matching->Name + "\" and \"" + it->Name + "\"";
Craig Topperc10719f2014-04-07 04:17:22 +000081 return nullptr;
Rafael Espindoladfc14702013-12-13 16:05:32 +000082 }
83 Matching = &*it;
Daniel Dunbar078a71e2009-07-15 04:24:58 +000084 }
85 }
86
Rafael Espindoladfc14702013-12-13 16:05:32 +000087 if (!Matching) {
Daniel Dunbar6119d872009-09-08 23:32:35 +000088 Error = "No available targets are compatible with this triple, "
89 "see -version for the available targets.";
Craig Topperc10719f2014-04-07 04:17:22 +000090 return nullptr;
Daniel Dunbar078a71e2009-07-15 04:24:58 +000091 }
92
Rafael Espindoladfc14702013-12-13 16:05:32 +000093 return Matching;
Daniel Dunbar078a71e2009-07-15 04:24:58 +000094}
95
Daniel Dunbar078a71e2009-07-15 04:24:58 +000096void TargetRegistry::RegisterTarget(Target &T,
97 const char *Name,
98 const char *ShortDesc,
Rafael Espindoladfc14702013-12-13 16:05:32 +000099 Target::ArchMatchFnTy ArchMatchFn,
Daniel Dunbar691a4782009-07-25 10:09:50 +0000100 bool HasJIT) {
Rafael Espindoladfc14702013-12-13 16:05:32 +0000101 assert(Name && ShortDesc && ArchMatchFn &&
Daniel Dunbar078a71e2009-07-15 04:24:58 +0000102 "Missing required target information!");
Daniel Dunbare8338102009-07-15 20:24:03 +0000103
104 // Check if this target has already been initialized, we allow this as a
105 // convenience to some clients.
106 if (T.Name)
107 return;
Daniel Dunbar078a71e2009-07-15 04:24:58 +0000108
109 // Add to the list of targets.
110 T.Next = FirstTarget;
Daniel Dunbar54995de2009-07-15 07:37:49 +0000111 FirstTarget = &T;
Daniel Dunbar078a71e2009-07-15 04:24:58 +0000112
113 T.Name = Name;
114 T.ShortDesc = ShortDesc;
Rafael Espindoladfc14702013-12-13 16:05:32 +0000115 T.ArchMatchFn = ArchMatchFn;
Daniel Dunbar691a4782009-07-25 10:09:50 +0000116 T.HasJIT = HasJIT;
Daniel Dunbar078a71e2009-07-15 04:24:58 +0000117}
118
Benjamin Krameradf1ea82014-03-07 21:52:38 +0000119static int TargetArraySortFn(const std::pair<StringRef, const Target *> *LHS,
120 const std::pair<StringRef, const Target *> *RHS) {
121 return LHS->first.compare(RHS->first);
122}
123
Chandler Carruth2baac022011-07-22 07:50:44 +0000124void TargetRegistry::printRegisteredTargetsForVersion() {
125 std::vector<std::pair<StringRef, const Target*> > Targets;
126 size_t Width = 0;
127 for (TargetRegistry::iterator I = TargetRegistry::begin(),
128 E = TargetRegistry::end();
129 I != E; ++I) {
130 Targets.push_back(std::make_pair(I->getName(), &*I));
131 Width = std::max(Width, Targets.back().first.size());
132 }
Benjamin Krameradf1ea82014-03-07 21:52:38 +0000133 array_pod_sort(Targets.begin(), Targets.end(), TargetArraySortFn);
Chandler Carruth2baac022011-07-22 07:50:44 +0000134
135 raw_ostream &OS = outs();
136 OS << " Registered Targets:\n";
137 for (unsigned i = 0, e = Targets.size(); i != e; ++i) {
138 OS << " " << Targets[i].first;
139 OS.indent(Width - Targets[i].first.size()) << " - "
140 << Targets[i].second->getShortDescription() << '\n';
141 }
142 if (Targets.empty())
143 OS << " (none)\n";
144}