LLVM  4.0.0
TargetRegistry.cpp
Go to the documentation of this file.
1 //===--- 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 
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/StringRef.h"
14 #include <cassert>
15 #include <vector>
16 using namespace llvm;
17 
18 // Clients are responsible for avoid race conditions in registration.
19 static Target *FirstTarget = nullptr;
20 
23 }
24 
25 const 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.
31  const Target *TheTarget = nullptr;
32  if (!ArchName.empty()) {
33  auto I = find_if(targets(),
34  [&](const Target &T) { return ArchName == T.getName(); });
35 
36  if (I == targets().end()) {
37  Error = "error: invalid target '" + ArchName + "'.\n";
38  return nullptr;
39  }
40 
41  TheTarget = &*I;
42 
43  // Adjust the triple to match (if known), otherwise stick with the
44  // given triple.
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);
52  if (!TheTarget) {
53  Error = ": error: unable to get target for '"
54  + TheTriple.getTriple()
55  + "', see --version and --triple.\n";
56  return nullptr;
57  }
58  }
59 
60  return TheTarget;
61 }
62 
63 const Target *TargetRegistry::lookupTarget(const std::string &TT,
64  std::string &Error) {
65  // Provide special warning when no targets are initialized.
66  if (targets().begin() == targets().end()) {
67  Error = "Unable to find target for this triple (no targets are registered)";
68  return nullptr;
69  }
70  Triple::ArchType Arch = Triple(TT).getArch();
71  auto ArchMatch = [&](const Target &T) { return T.ArchMatchFn(Arch); };
72  auto I = find_if(targets(), ArchMatch);
73 
74  if (I == targets().end()) {
75  Error = "No available targets are compatible with this triple.";
76  return nullptr;
77  }
78 
79  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;
87 }
88 
90  const char *Name,
91  const char *ShortDesc,
92  Target::ArchMatchFnTy ArchMatchFn,
93  bool HasJIT) {
94  assert(Name && ShortDesc && ArchMatchFn &&
95  "Missing required target information!");
96 
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;
101 
102  // Add to the list of targets.
103  T.Next = FirstTarget;
104  FirstTarget = &T;
105 
106  T.Name = Name;
107  T.ShortDesc = ShortDesc;
108  T.ArchMatchFn = ArchMatchFn;
109  T.HasJIT = HasJIT;
110 }
111 
112 static 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 
118  std::vector<std::pair<StringRef, const Target*> > Targets;
119  size_t Width = 0;
120  for (const auto &T : TargetRegistry::targets()) {
121  Targets.push_back(std::make_pair(T.getName(), &T));
122  Width = std::max(Width, Targets.back().first.size());
123  }
124  array_pod_sort(Targets.begin(), Targets.end(), TargetArraySortFn);
125 
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 }
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:241
size_t i
const char * getName() const
getName - Get the target name.
static int TargetArraySortFn(const std::pair< StringRef, const Target * > *LHS, const std::pair< StringRef, const Target * > *RHS)
const_iterator begin(StringRef path)
Get begin iterator over path.
Definition: Path.cpp:233
static const Target * lookupTarget(const std::string &Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
#define T
static void RegisterTarget(Target &T, const char *Name, const char *ShortDesc, Target::ArchMatchFnTy ArchMatchFn, bool HasJIT=false)
RegisterTarget - Register the given target.
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:270
static iterator_range< iterator > targets()
raw_ostream & outs()
This returns a reference to a raw_ostream for standard output.
void array_pod_sort(IteratorTy Start, IteratorTy End)
array_pod_sort - This sorts an array with the specified start and end extent.
Definition: STLExtras.h:689
static void printRegisteredTargetsForVersion()
printRegisteredTargetsForVersion - Print the registered targets appropriately for inclusion in a tool...
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
const std::string & getTriple() const
Definition: Triple.h:341
bool(* ArchMatchFnTy)(Triple::ArchType Arch)
static Target * FirstTarget
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
static ArchType getArchTypeForLLVMName(StringRef Str)
getArchTypeForLLVMName - The canonical type for the given LLVM architecture name (e.g., "x86").
Definition: Triple.cpp:249
A range adaptor for a pair of iterators.
Target - Wrapper for Target specific information.
#define I(x, y, z)
Definition: MD5.cpp:54
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
auto find_if(R &&Range, UnaryPredicate P) -> decltype(std::begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:764
void setArch(ArchType Kind)
setArch - Set the architecture (first) component of the triple to a known type.
Definition: Triple.cpp:1080