blob: 02a6d334b911e5316b3b00f449de1eaa93bc138c [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"
Chandler Carruth2baac022011-07-22 07:50:44 +000013#include "llvm/Support/raw_ostream.h"
Daniel Dunbar078a71e2009-07-15 04:24:58 +000014#include <cassert>
Chandler Carruth2baac022011-07-22 07:50:44 +000015#include <vector>
Daniel Dunbar078a71e2009-07-15 04:24:58 +000016using namespace llvm;
17
Daniel Dunbar1c663322009-07-15 07:09:29 +000018// Clients are responsible for avoid race conditions in registration.
Craig Topperc10719f2014-04-07 04:17:22 +000019static Target *FirstTarget = nullptr;
Daniel Dunbar078a71e2009-07-15 04:24:58 +000020
David Blaikie46c561c2015-05-11 22:20:48 +000021iterator_range<TargetRegistry::iterator> TargetRegistry::targets() {
22 return make_range(iterator(FirstTarget), iterator());
Daniel Dunbar78c8fb12009-07-16 02:06:09 +000023}
24
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000025const Target *TargetRegistry::lookupTarget(const std::string &ArchName,
26 Triple &TheTriple,
27 std::string &Error) {
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.
Craig Topperc10719f2014-04-07 04:17:22 +000031 const Target *TheTarget = nullptr;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000032 if (!ArchName.empty()) {
David Blaikie46c561c2015-05-11 22:20:48 +000033 auto I =
34 std::find_if(targets().begin(), targets().end(),
35 [&](const Target &T) { return ArchName == T.getName(); });
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000036
David Blaikie46c561c2015-05-11 22:20:48 +000037 if (I == targets().end()) {
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000038 Error = "error: invalid target '" + ArchName + "'.\n";
Craig Topperc10719f2014-04-07 04:17:22 +000039 return nullptr;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000040 }
41
David Blaikie46c561c2015-05-11 22:20:48 +000042 TheTarget = &*I;
43
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000044 // Adjust the triple to match (if known), otherwise stick with the
45 // given triple.
46 Triple::ArchType Type = Triple::getArchTypeForLLVMName(ArchName);
47 if (Type != Triple::UnknownArch)
48 TheTriple.setArch(Type);
49 } else {
50 // Get the target specific parser.
51 std::string TempError;
52 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), TempError);
Craig Topper8d399f82014-04-09 04:20:00 +000053 if (!TheTarget) {
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000054 Error = ": error: unable to get target for '"
55 + TheTriple.getTriple()
56 + "', see --version and --triple.\n";
Craig Topperc10719f2014-04-07 04:17:22 +000057 return nullptr;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000058 }
59 }
60
61 return TheTarget;
62}
63
Daniel Dunbar47d67912009-07-26 02:12:58 +000064const Target *TargetRegistry::lookupTarget(const std::string &TT,
Daniel Dunbar47d67912009-07-26 02:12:58 +000065 std::string &Error) {
Daniel Dunbar50745bf2009-07-17 15:50:49 +000066 // Provide special warning when no targets are initialized.
David Blaikie46c561c2015-05-11 22:20:48 +000067 if (targets().begin() == targets().end()) {
Daniel Dunbar50745bf2009-07-17 15:50:49 +000068 Error = "Unable to find target for this triple (no targets are registered)";
Craig Topperc10719f2014-04-07 04:17:22 +000069 return nullptr;
Daniel Dunbar50745bf2009-07-17 15:50:49 +000070 }
David Blaikie46c561c2015-05-11 22:20:48 +000071 Triple::ArchType Arch = Triple(TT).getArch();
72 auto ArchMatch = [&](const Target &T) { return T.ArchMatchFn(Arch); };
73 auto I = std::find_if(targets().begin(), targets().end(), ArchMatch);
Daniel Dunbar078a71e2009-07-15 04:24:58 +000074
David Blaikie46c561c2015-05-11 22:20:48 +000075 if (I == targets().end()) {
Rafael Espindola26e65812016-01-28 22:55:45 +000076 Error = "No available targets are compatible with this triple.";
Craig Topperc10719f2014-04-07 04:17:22 +000077 return nullptr;
Daniel Dunbar078a71e2009-07-15 04:24:58 +000078 }
79
David Blaikie46c561c2015-05-11 22:20:48 +000080 auto J = std::find_if(std::next(I), targets().end(), ArchMatch);
81 if (J != targets().end()) {
82 Error = std::string("Cannot choose between targets \"") + I->Name +
83 "\" and \"" + J->Name + "\"";
84 return nullptr;
85 }
86
87 return &*I;
Daniel Dunbar078a71e2009-07-15 04:24:58 +000088}
89
Daniel Dunbar078a71e2009-07-15 04:24:58 +000090void TargetRegistry::RegisterTarget(Target &T,
91 const char *Name,
92 const char *ShortDesc,
Rafael Espindoladfc14702013-12-13 16:05:32 +000093 Target::ArchMatchFnTy ArchMatchFn,
Daniel Dunbar691a4782009-07-25 10:09:50 +000094 bool HasJIT) {
Rafael Espindoladfc14702013-12-13 16:05:32 +000095 assert(Name && ShortDesc && ArchMatchFn &&
Daniel Dunbar078a71e2009-07-15 04:24:58 +000096 "Missing required target information!");
Daniel Dunbare8338102009-07-15 20:24:03 +000097
98 // Check if this target has already been initialized, we allow this as a
99 // convenience to some clients.
100 if (T.Name)
101 return;
Daniel Dunbar078a71e2009-07-15 04:24:58 +0000102
103 // Add to the list of targets.
104 T.Next = FirstTarget;
Daniel Dunbar54995de2009-07-15 07:37:49 +0000105 FirstTarget = &T;
Daniel Dunbar078a71e2009-07-15 04:24:58 +0000106
107 T.Name = Name;
108 T.ShortDesc = ShortDesc;
Rafael Espindoladfc14702013-12-13 16:05:32 +0000109 T.ArchMatchFn = ArchMatchFn;
Daniel Dunbar691a4782009-07-25 10:09:50 +0000110 T.HasJIT = HasJIT;
Daniel Dunbar078a71e2009-07-15 04:24:58 +0000111}
112
Benjamin Krameradf1ea82014-03-07 21:52:38 +0000113static int TargetArraySortFn(const std::pair<StringRef, const Target *> *LHS,
114 const std::pair<StringRef, const Target *> *RHS) {
115 return LHS->first.compare(RHS->first);
116}
117
Chandler Carruth2baac022011-07-22 07:50:44 +0000118void TargetRegistry::printRegisteredTargetsForVersion() {
119 std::vector<std::pair<StringRef, const Target*> > Targets;
120 size_t Width = 0;
David Blaikie46c561c2015-05-11 22:20:48 +0000121 for (const auto &T : TargetRegistry::targets()) {
122 Targets.push_back(std::make_pair(T.getName(), &T));
Chandler Carruth2baac022011-07-22 07:50:44 +0000123 Width = std::max(Width, Targets.back().first.size());
124 }
Benjamin Krameradf1ea82014-03-07 21:52:38 +0000125 array_pod_sort(Targets.begin(), Targets.end(), TargetArraySortFn);
Chandler Carruth2baac022011-07-22 07:50:44 +0000126
127 raw_ostream &OS = outs();
128 OS << " Registered Targets:\n";
129 for (unsigned i = 0, e = Targets.size(); i != e; ++i) {
130 OS << " " << Targets[i].first;
131 OS.indent(Width - Targets[i].first.size()) << " - "
132 << Targets[i].second->getShortDescription() << '\n';
133 }
134 if (Targets.empty())
135 OS << " (none)\n";
136}