xref: /freebsd-src/contrib/llvm-project/llvm/lib/MC/TargetRegistry.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
1349cc55cSDimitry Andric //===--- TargetRegistry.cpp - Target registration -------------------------===//
2349cc55cSDimitry Andric //
3349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6349cc55cSDimitry Andric //
7349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
8349cc55cSDimitry Andric 
9349cc55cSDimitry Andric #include "llvm/MC/TargetRegistry.h"
10349cc55cSDimitry Andric #include "llvm/ADT/STLExtras.h"
11349cc55cSDimitry Andric #include "llvm/ADT/StringRef.h"
12349cc55cSDimitry Andric #include "llvm/Support/raw_ostream.h"
13349cc55cSDimitry Andric #include <cassert>
14349cc55cSDimitry Andric #include <vector>
15349cc55cSDimitry Andric using namespace llvm;
16349cc55cSDimitry Andric 
17349cc55cSDimitry Andric // Clients are responsible for avoid race conditions in registration.
18349cc55cSDimitry Andric static Target *FirstTarget = nullptr;
19349cc55cSDimitry Andric 
20349cc55cSDimitry Andric iterator_range<TargetRegistry::iterator> TargetRegistry::targets() {
21349cc55cSDimitry Andric   return make_range(iterator(FirstTarget), iterator());
22349cc55cSDimitry Andric }
23349cc55cSDimitry Andric 
24349cc55cSDimitry Andric const Target *TargetRegistry::lookupTarget(const std::string &ArchName,
25349cc55cSDimitry Andric                                            Triple &TheTriple,
26349cc55cSDimitry Andric                                            std::string &Error) {
27349cc55cSDimitry Andric   // Allocate target machine.  First, check whether the user has explicitly
28349cc55cSDimitry Andric   // specified an architecture to compile for. If so we have to look it up by
29349cc55cSDimitry Andric   // name, because it might be a backend that has no mapping to a target triple.
30349cc55cSDimitry Andric   const Target *TheTarget = nullptr;
31349cc55cSDimitry Andric   if (!ArchName.empty()) {
32349cc55cSDimitry Andric     auto I = find_if(targets(),
33349cc55cSDimitry Andric                      [&](const Target &T) { return ArchName == T.getName(); });
34349cc55cSDimitry Andric 
35349cc55cSDimitry Andric     if (I == targets().end()) {
3681ad6265SDimitry Andric       Error = "invalid target '" + ArchName + "'.\n";
37349cc55cSDimitry Andric       return nullptr;
38349cc55cSDimitry Andric     }
39349cc55cSDimitry Andric 
40349cc55cSDimitry Andric     TheTarget = &*I;
41349cc55cSDimitry Andric 
42349cc55cSDimitry Andric     // Adjust the triple to match (if known), otherwise stick with the
43349cc55cSDimitry Andric     // given triple.
44349cc55cSDimitry Andric     Triple::ArchType Type = Triple::getArchTypeForLLVMName(ArchName);
45349cc55cSDimitry Andric     if (Type != Triple::UnknownArch)
46349cc55cSDimitry Andric       TheTriple.setArch(Type);
47349cc55cSDimitry Andric   } else {
48349cc55cSDimitry Andric     // Get the target specific parser.
49349cc55cSDimitry Andric     std::string TempError;
50349cc55cSDimitry Andric     TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), TempError);
51349cc55cSDimitry Andric     if (!TheTarget) {
5281ad6265SDimitry Andric       Error = "unable to get target for '"
53349cc55cSDimitry Andric             + TheTriple.getTriple()
54349cc55cSDimitry Andric             + "', see --version and --triple.\n";
55349cc55cSDimitry Andric       return nullptr;
56349cc55cSDimitry Andric     }
57349cc55cSDimitry Andric   }
58349cc55cSDimitry Andric 
59349cc55cSDimitry Andric   return TheTarget;
60349cc55cSDimitry Andric }
61349cc55cSDimitry Andric 
62349cc55cSDimitry Andric const Target *TargetRegistry::lookupTarget(const std::string &TT,
63349cc55cSDimitry Andric                                            std::string &Error) {
64349cc55cSDimitry Andric   // Provide special warning when no targets are initialized.
65349cc55cSDimitry Andric   if (targets().begin() == targets().end()) {
66349cc55cSDimitry Andric     Error = "Unable to find target for this triple (no targets are registered)";
67349cc55cSDimitry Andric     return nullptr;
68349cc55cSDimitry Andric   }
69349cc55cSDimitry Andric   Triple::ArchType Arch = Triple(TT).getArch();
70349cc55cSDimitry Andric   auto ArchMatch = [&](const Target &T) { return T.ArchMatchFn(Arch); };
71349cc55cSDimitry Andric   auto I = find_if(targets(), ArchMatch);
72349cc55cSDimitry Andric 
73349cc55cSDimitry Andric   if (I == targets().end()) {
74349cc55cSDimitry Andric     Error = "No available targets are compatible with triple \"" + TT + "\"";
75349cc55cSDimitry Andric     return nullptr;
76349cc55cSDimitry Andric   }
77349cc55cSDimitry Andric 
78349cc55cSDimitry Andric   auto J = std::find_if(std::next(I), targets().end(), ArchMatch);
79349cc55cSDimitry Andric   if (J != targets().end()) {
80349cc55cSDimitry Andric     Error = std::string("Cannot choose between targets \"") + I->Name +
81349cc55cSDimitry Andric             "\" and \"" + J->Name + "\"";
82349cc55cSDimitry Andric     return nullptr;
83349cc55cSDimitry Andric   }
84349cc55cSDimitry Andric 
85349cc55cSDimitry Andric   return &*I;
86349cc55cSDimitry Andric }
87349cc55cSDimitry Andric 
88349cc55cSDimitry Andric void TargetRegistry::RegisterTarget(Target &T, const char *Name,
89349cc55cSDimitry Andric                                     const char *ShortDesc,
90349cc55cSDimitry Andric                                     const char *BackendName,
91349cc55cSDimitry Andric                                     Target::ArchMatchFnTy ArchMatchFn,
92349cc55cSDimitry Andric                                     bool HasJIT) {
93349cc55cSDimitry Andric   assert(Name && ShortDesc && ArchMatchFn &&
94349cc55cSDimitry Andric          "Missing required target information!");
95349cc55cSDimitry Andric 
96349cc55cSDimitry Andric   // Check if this target has already been initialized, we allow this as a
97349cc55cSDimitry Andric   // convenience to some clients.
98349cc55cSDimitry Andric   if (T.Name)
99349cc55cSDimitry Andric     return;
100349cc55cSDimitry Andric 
101349cc55cSDimitry Andric   // Add to the list of targets.
102349cc55cSDimitry Andric   T.Next = FirstTarget;
103349cc55cSDimitry Andric   FirstTarget = &T;
104349cc55cSDimitry Andric 
105349cc55cSDimitry Andric   T.Name = Name;
106349cc55cSDimitry Andric   T.ShortDesc = ShortDesc;
107349cc55cSDimitry Andric   T.BackendName = BackendName;
108349cc55cSDimitry Andric   T.ArchMatchFn = ArchMatchFn;
109349cc55cSDimitry Andric   T.HasJIT = HasJIT;
110349cc55cSDimitry Andric }
111349cc55cSDimitry Andric 
112349cc55cSDimitry Andric static int TargetArraySortFn(const std::pair<StringRef, const Target *> *LHS,
113349cc55cSDimitry Andric                              const std::pair<StringRef, const Target *> *RHS) {
114349cc55cSDimitry Andric   return LHS->first.compare(RHS->first);
115349cc55cSDimitry Andric }
116349cc55cSDimitry Andric 
117349cc55cSDimitry Andric void TargetRegistry::printRegisteredTargetsForVersion(raw_ostream &OS) {
118349cc55cSDimitry Andric   std::vector<std::pair<StringRef, const Target*> > Targets;
119349cc55cSDimitry Andric   size_t Width = 0;
120349cc55cSDimitry Andric   for (const auto &T : TargetRegistry::targets()) {
121349cc55cSDimitry Andric     Targets.push_back(std::make_pair(T.getName(), &T));
122349cc55cSDimitry Andric     Width = std::max(Width, Targets.back().first.size());
123349cc55cSDimitry Andric   }
124349cc55cSDimitry Andric   array_pod_sort(Targets.begin(), Targets.end(), TargetArraySortFn);
125349cc55cSDimitry Andric 
126*bdd1243dSDimitry Andric   OS << "\n";
127349cc55cSDimitry Andric   OS << "  Registered Targets:\n";
1280eae32dcSDimitry Andric   for (const auto &Target : Targets) {
1290eae32dcSDimitry Andric     OS << "    " << Target.first;
1300eae32dcSDimitry Andric     OS.indent(Width - Target.first.size())
1310eae32dcSDimitry Andric         << " - " << Target.second->getShortDescription() << '\n';
132349cc55cSDimitry Andric   }
133349cc55cSDimitry Andric   if (Targets.empty())
134349cc55cSDimitry Andric     OS << "    (none)\n";
135349cc55cSDimitry Andric }
136