blob: 3ca857289856881f09ec4b24d2623f70e843eac5 [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
Daniel Dunbar78c8fb12009-07-16 02:06:09 +000021TargetRegistry::iterator TargetRegistry::begin() {
22 return iterator(FirstTarget);
23}
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()) {
33 for (TargetRegistry::iterator it = TargetRegistry::begin(),
34 ie = TargetRegistry::end(); it != ie; ++it) {
35 if (ArchName == it->getName()) {
36 TheTarget = &*it;
37 break;
38 }
39 }
40
41 if (!TheTarget) {
42 Error = "error: invalid target '" + ArchName + "'.\n";
Craig Topperc10719f2014-04-07 04:17:22 +000043 return nullptr;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000044 }
45
46 // Adjust the triple to match (if known), otherwise stick with the
47 // given triple.
48 Triple::ArchType Type = Triple::getArchTypeForLLVMName(ArchName);
49 if (Type != Triple::UnknownArch)
50 TheTriple.setArch(Type);
51 } else {
52 // Get the target specific parser.
53 std::string TempError;
54 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), TempError);
Craig Topper8d399f82014-04-09 04:20:00 +000055 if (!TheTarget) {
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000056 Error = ": error: unable to get target for '"
57 + TheTriple.getTriple()
58 + "', see --version and --triple.\n";
Craig Topperc10719f2014-04-07 04:17:22 +000059 return nullptr;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +000060 }
61 }
62
63 return TheTarget;
64}
65
Daniel Dunbar47d67912009-07-26 02:12:58 +000066const Target *TargetRegistry::lookupTarget(const std::string &TT,
Daniel Dunbar47d67912009-07-26 02:12:58 +000067 std::string &Error) {
Daniel Dunbar50745bf2009-07-17 15:50:49 +000068 // Provide special warning when no targets are initialized.
69 if (begin() == end()) {
70 Error = "Unable to find target for this triple (no targets are registered)";
Craig Topperc10719f2014-04-07 04:17:22 +000071 return nullptr;
Daniel Dunbar50745bf2009-07-17 15:50:49 +000072 }
Craig Topperc10719f2014-04-07 04:17:22 +000073 const Target *Matching = nullptr;
Rafael Espindoladfc14702013-12-13 16:05:32 +000074 Triple::ArchType Arch = Triple(TT).getArch();
Daniel Dunbar78c8fb12009-07-16 02:06:09 +000075 for (iterator it = begin(), ie = end(); it != ie; ++it) {
Rafael Espindoladfc14702013-12-13 16:05:32 +000076 if (it->ArchMatchFn(Arch)) {
77 if (Matching) {
78 Error = std::string("Cannot choose between targets \"") +
79 Matching->Name + "\" and \"" + it->Name + "\"";
Craig Topperc10719f2014-04-07 04:17:22 +000080 return nullptr;
Rafael Espindoladfc14702013-12-13 16:05:32 +000081 }
82 Matching = &*it;
Daniel Dunbar078a71e2009-07-15 04:24:58 +000083 }
84 }
85
Rafael Espindoladfc14702013-12-13 16:05:32 +000086 if (!Matching) {
Daniel Dunbar6119d872009-09-08 23:32:35 +000087 Error = "No available targets are compatible with this triple, "
88 "see -version for the available targets.";
Craig Topperc10719f2014-04-07 04:17:22 +000089 return nullptr;
Daniel Dunbar078a71e2009-07-15 04:24:58 +000090 }
91
Rafael Espindoladfc14702013-12-13 16:05:32 +000092 return Matching;
Daniel Dunbar078a71e2009-07-15 04:24:58 +000093}
94
Daniel Dunbar078a71e2009-07-15 04:24:58 +000095void TargetRegistry::RegisterTarget(Target &T,
96 const char *Name,
97 const char *ShortDesc,
Rafael Espindoladfc14702013-12-13 16:05:32 +000098 Target::ArchMatchFnTy ArchMatchFn,
Daniel Dunbar691a4782009-07-25 10:09:50 +000099 bool HasJIT) {
Rafael Espindoladfc14702013-12-13 16:05:32 +0000100 assert(Name && ShortDesc && ArchMatchFn &&
Daniel Dunbar078a71e2009-07-15 04:24:58 +0000101 "Missing required target information!");
Daniel Dunbare8338102009-07-15 20:24:03 +0000102
103 // Check if this target has already been initialized, we allow this as a
104 // convenience to some clients.
105 if (T.Name)
106 return;
Daniel Dunbar078a71e2009-07-15 04:24:58 +0000107
108 // Add to the list of targets.
109 T.Next = FirstTarget;
Daniel Dunbar54995de2009-07-15 07:37:49 +0000110 FirstTarget = &T;
Daniel Dunbar078a71e2009-07-15 04:24:58 +0000111
112 T.Name = Name;
113 T.ShortDesc = ShortDesc;
Rafael Espindoladfc14702013-12-13 16:05:32 +0000114 T.ArchMatchFn = ArchMatchFn;
Daniel Dunbar691a4782009-07-25 10:09:50 +0000115 T.HasJIT = HasJIT;
Daniel Dunbar078a71e2009-07-15 04:24:58 +0000116}
117
Benjamin Krameradf1ea82014-03-07 21:52:38 +0000118static int TargetArraySortFn(const std::pair<StringRef, const Target *> *LHS,
119 const std::pair<StringRef, const Target *> *RHS) {
120 return LHS->first.compare(RHS->first);
121}
122
Chandler Carruth2baac022011-07-22 07:50:44 +0000123void TargetRegistry::printRegisteredTargetsForVersion() {
124 std::vector<std::pair<StringRef, const Target*> > Targets;
125 size_t Width = 0;
126 for (TargetRegistry::iterator I = TargetRegistry::begin(),
127 E = TargetRegistry::end();
128 I != E; ++I) {
129 Targets.push_back(std::make_pair(I->getName(), &*I));
130 Width = std::max(Width, Targets.back().first.size());
131 }
Benjamin Krameradf1ea82014-03-07 21:52:38 +0000132 array_pod_sort(Targets.begin(), Targets.end(), TargetArraySortFn);
Chandler Carruth2baac022011-07-22 07:50:44 +0000133
134 raw_ostream &OS = outs();
135 OS << " Registered Targets:\n";
136 for (unsigned i = 0, e = Targets.size(); i != e; ++i) {
137 OS << " " << Targets[i].first;
138 OS.indent(Width - Targets[i].first.size()) << " - "
139 << Targets[i].second->getShortDescription() << '\n';
140 }
141 if (Targets.empty())
142 OS << " (none)\n";
143}