1 //===--- TargetRegistry.cpp - Target registration -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/TargetRegistry.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/Support/raw_ostream.h"
18 // Clients are responsible for avoid race conditions in registration.
19 static Target
*FirstTarget
= nullptr;
21 iterator_range
<TargetRegistry::iterator
> TargetRegistry::targets() {
22 return make_range(iterator(FirstTarget
), iterator());
25 const Target
*TargetRegistry::lookupTarget(const std::string
&ArchName
,
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(); });
36 if (I
== targets().end()) {
37 Error
= "error: invalid target '" + ArchName
+ "'.\n";
43 // Adjust the triple to match (if known), otherwise stick with the
45 Triple::ArchType Type
= Triple::getArchTypeForLLVMName(ArchName
);
46 if (Type
!= Triple::UnknownArch
)
47 TheTriple
.setArch(Type
);
49 // Get the target specific parser.
50 std::string TempError
;
51 TheTarget
= TargetRegistry::lookupTarget(TheTriple
.getTriple(), TempError
);
53 Error
= ": error: unable to get target for '"
54 + TheTriple
.getTriple()
55 + "', see --version and --triple.\n";
63 const Target
*TargetRegistry::lookupTarget(const std::string
&TT
,
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)";
70 Triple::ArchType Arch
= Triple(TT
).getArch();
71 auto ArchMatch
= [&](const Target
&T
) { return T
.ArchMatchFn(Arch
); };
72 auto I
= find_if(targets(), ArchMatch
);
74 if (I
== targets().end()) {
75 Error
= "No available targets are compatible with triple \"" + TT
+ "\"";
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
+ "\"";
89 void TargetRegistry::RegisterTarget(Target
&T
, const char *Name
,
90 const char *ShortDesc
,
91 const char *BackendName
,
92 Target::ArchMatchFnTy ArchMatchFn
,
94 assert(Name
&& ShortDesc
&& ArchMatchFn
&&
95 "Missing required target information!");
97 // Check if this target has already been initialized, we allow this as a
98 // convenience to some clients.
102 // Add to the list of targets.
103 T
.Next
= FirstTarget
;
107 T
.ShortDesc
= ShortDesc
;
108 T
.BackendName
= BackendName
;
109 T
.ArchMatchFn
= ArchMatchFn
;
113 static int TargetArraySortFn(const std::pair
<StringRef
, const Target
*> *LHS
,
114 const std::pair
<StringRef
, const Target
*> *RHS
) {
115 return LHS
->first
.compare(RHS
->first
);
118 void TargetRegistry::printRegisteredTargetsForVersion(raw_ostream
&OS
) {
119 std::vector
<std::pair
<StringRef
, const Target
*> > Targets
;
121 for (const auto &T
: TargetRegistry::targets()) {
122 Targets
.push_back(std::make_pair(T
.getName(), &T
));
123 Width
= std::max(Width
, Targets
.back().first
.size());
125 array_pod_sort(Targets
.begin(), Targets
.end(), TargetArraySortFn
);
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';