blob: bed9ed64f802b8467da156d82e6b698c56674174 [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 Majnemer562e8292016-08-12 00:18:03 +000033 auto I = find_if(targets(),
David Blaikie46c561c2015-05-11 22:20:48 +000034 [&](const Target &T) { return ArchName == T.getName(); });
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000035
David Blaikie46c561c2015-05-11 22:20:48 +000036 if (I == targets().end()) {
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000037 Error = "error: invalid target '" + ArchName + "'.\n";
Craig Topperc10719f2014-04-07 04:17:22 +000038 return nullptr;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000039 }
40
David Blaikie46c561c2015-05-11 22:20:48 +000041 TheTarget = &*I;
42
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000043 // Adjust the triple to match (if known), otherwise stick with the
44 // given triple.
45 Triple::ArchType Type = Triple::getArchTypeForLLVMName(ArchName);
46 if (Type != Triple::UnknownArch)
47 TheTriple.setArch(Type);
48 } else {
49 // Get the target specific parser.
50 std::string TempError;
51 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), TempError);
Craig Topper8d399f82014-04-09 04:20:00 +000052 if (!TheTarget) {
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000053 Error = ": error: unable to get target for '"
54 + TheTriple.getTriple()
55 + "', see --version and --triple.\n";
Craig Topperc10719f2014-04-07 04:17:22 +000056 return nullptr;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000057 }
58 }
59
60 return TheTarget;
61}
62
Daniel Dunbar47d67912009-07-26 02:12:58 +000063const Target *TargetRegistry::lookupTarget(const std::string &TT,
Daniel Dunbar47d67912009-07-26 02:12:58 +000064 std::string &Error) {
Daniel Dunbar50745bf2009-07-17 15:50:49 +000065 // Provide special warning when no targets are initialized.
David Blaikie46c561c2015-05-11 22:20:48 +000066 if (targets().begin() == targets().end()) {
Daniel Dunbar50745bf2009-07-17 15:50:49 +000067 Error = "Unable to find target for this triple (no targets are registered)";
Craig Topperc10719f2014-04-07 04:17:22 +000068 return nullptr;
Daniel Dunbar50745bf2009-07-17 15:50:49 +000069 }
David Blaikie46c561c2015-05-11 22:20:48 +000070 Triple::ArchType Arch = Triple(TT).getArch();
71 auto ArchMatch = [&](const Target &T) { return T.ArchMatchFn(Arch); };
David Majnemer562e8292016-08-12 00:18:03 +000072 auto I = find_if(targets(), ArchMatch);
Daniel Dunbar078a71e2009-07-15 04:24:58 +000073
David Blaikie46c561c2015-05-11 22:20:48 +000074 if (I == targets().end()) {
Rafael Espindola26e65812016-01-28 22:55:45 +000075 Error = "No available targets are compatible with this triple.";
Craig Topperc10719f2014-04-07 04:17:22 +000076 return nullptr;
Daniel Dunbar078a71e2009-07-15 04:24:58 +000077 }
78
David Blaikie46c561c2015-05-11 22:20:48 +000079 auto J = std::find_if(std::next(I), targets().end(), ArchMatch);
80 if (J != targets().end()) {
81 Error = std::string("Cannot choose between targets \"") + I->Name +
82 "\" and \"" + J->Name + "\"";
83 return nullptr;
84 }
85
86 return &*I;
Daniel Dunbar078a71e2009-07-15 04:24:58 +000087}
88
Daniel Dunbar078a71e2009-07-15 04:24:58 +000089void TargetRegistry::RegisterTarget(Target &T,
90 const char *Name,
91 const char *ShortDesc,
Rafael Espindoladfc14702013-12-13 16:05:32 +000092 Target::ArchMatchFnTy ArchMatchFn,
Daniel Dunbar691a4782009-07-25 10:09:50 +000093 bool HasJIT) {
Rafael Espindoladfc14702013-12-13 16:05:32 +000094 assert(Name && ShortDesc && ArchMatchFn &&
Daniel Dunbar078a71e2009-07-15 04:24:58 +000095 "Missing required target information!");
Daniel Dunbare8338102009-07-15 20:24:03 +000096
97 // Check if this target has already been initialized, we allow this as a
98 // convenience to some clients.
99 if (T.Name)
100 return;
Daniel Dunbar078a71e2009-07-15 04:24:58 +0000101
102 // Add to the list of targets.
103 T.Next = FirstTarget;
Daniel Dunbar54995de2009-07-15 07:37:49 +0000104 FirstTarget = &T;
Daniel Dunbar078a71e2009-07-15 04:24:58 +0000105
106 T.Name = Name;
107 T.ShortDesc = ShortDesc;
Rafael Espindoladfc14702013-12-13 16:05:32 +0000108 T.ArchMatchFn = ArchMatchFn;
Daniel Dunbar691a4782009-07-25 10:09:50 +0000109 T.HasJIT = HasJIT;
Daniel Dunbar078a71e2009-07-15 04:24:58 +0000110}
111
Benjamin Krameradf1ea82014-03-07 21:52:38 +0000112static int TargetArraySortFn(const std::pair<StringRef, const Target *> *LHS,
113 const std::pair<StringRef, const Target *> *RHS) {
114 return LHS->first.compare(RHS->first);
115}
116
Chandler Carruth2baac022011-07-22 07:50:44 +0000117void TargetRegistry::printRegisteredTargetsForVersion() {
118 std::vector<std::pair<StringRef, const Target*> > Targets;
119 size_t Width = 0;
David Blaikie46c561c2015-05-11 22:20:48 +0000120 for (const auto &T : TargetRegistry::targets()) {
121 Targets.push_back(std::make_pair(T.getName(), &T));
Chandler Carruth2baac022011-07-22 07:50:44 +0000122 Width = std::max(Width, Targets.back().first.size());
123 }
Benjamin Krameradf1ea82014-03-07 21:52:38 +0000124 array_pod_sort(Targets.begin(), Targets.end(), TargetArraySortFn);
Chandler Carruth2baac022011-07-22 07:50:44 +0000125
126 raw_ostream &OS = outs();
127 OS << " Registered Targets:\n";
128 for (unsigned i = 0, e = Targets.size(); i != e; ++i) {
129 OS << " " << Targets[i].first;
130 OS.indent(Width - Targets[i].first.size()) << " - "
131 << Targets[i].second->getShortDescription() << '\n';
132 }
133 if (Targets.empty())
134 OS << " (none)\n";
135}