1*349cc55cSDimitry Andric //===--- TargetRegistry.cpp - Target registration -------------------------===// 2*349cc55cSDimitry Andric // 3*349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*349cc55cSDimitry Andric // 7*349cc55cSDimitry Andric //===----------------------------------------------------------------------===// 8*349cc55cSDimitry Andric 9*349cc55cSDimitry Andric #include "llvm/MC/TargetRegistry.h" 10*349cc55cSDimitry Andric #include "llvm/ADT/STLExtras.h" 11*349cc55cSDimitry Andric #include "llvm/ADT/StringRef.h" 12*349cc55cSDimitry Andric #include "llvm/Support/raw_ostream.h" 13*349cc55cSDimitry Andric #include <cassert> 14*349cc55cSDimitry Andric #include <vector> 15*349cc55cSDimitry Andric using namespace llvm; 16*349cc55cSDimitry Andric 17*349cc55cSDimitry Andric // Clients are responsible for avoid race conditions in registration. 18*349cc55cSDimitry Andric static Target *FirstTarget = nullptr; 19*349cc55cSDimitry Andric 20*349cc55cSDimitry Andric iterator_range<TargetRegistry::iterator> TargetRegistry::targets() { 21*349cc55cSDimitry Andric return make_range(iterator(FirstTarget), iterator()); 22*349cc55cSDimitry Andric } 23*349cc55cSDimitry Andric 24*349cc55cSDimitry Andric const Target *TargetRegistry::lookupTarget(const std::string &ArchName, 25*349cc55cSDimitry Andric Triple &TheTriple, 26*349cc55cSDimitry Andric std::string &Error) { 27*349cc55cSDimitry Andric // Allocate target machine. First, check whether the user has explicitly 28*349cc55cSDimitry Andric // specified an architecture to compile for. If so we have to look it up by 29*349cc55cSDimitry Andric // name, because it might be a backend that has no mapping to a target triple. 30*349cc55cSDimitry Andric const Target *TheTarget = nullptr; 31*349cc55cSDimitry Andric if (!ArchName.empty()) { 32*349cc55cSDimitry Andric auto I = find_if(targets(), 33*349cc55cSDimitry Andric [&](const Target &T) { return ArchName == T.getName(); }); 34*349cc55cSDimitry Andric 35*349cc55cSDimitry Andric if (I == targets().end()) { 36*349cc55cSDimitry Andric Error = "error: invalid target '" + ArchName + "'.\n"; 37*349cc55cSDimitry Andric return nullptr; 38*349cc55cSDimitry Andric } 39*349cc55cSDimitry Andric 40*349cc55cSDimitry Andric TheTarget = &*I; 41*349cc55cSDimitry Andric 42*349cc55cSDimitry Andric // Adjust the triple to match (if known), otherwise stick with the 43*349cc55cSDimitry Andric // given triple. 44*349cc55cSDimitry Andric Triple::ArchType Type = Triple::getArchTypeForLLVMName(ArchName); 45*349cc55cSDimitry Andric if (Type != Triple::UnknownArch) 46*349cc55cSDimitry Andric TheTriple.setArch(Type); 47*349cc55cSDimitry Andric } else { 48*349cc55cSDimitry Andric // Get the target specific parser. 49*349cc55cSDimitry Andric std::string TempError; 50*349cc55cSDimitry Andric TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), TempError); 51*349cc55cSDimitry Andric if (!TheTarget) { 52*349cc55cSDimitry Andric Error = ": error: unable to get target for '" 53*349cc55cSDimitry Andric + TheTriple.getTriple() 54*349cc55cSDimitry Andric + "', see --version and --triple.\n"; 55*349cc55cSDimitry Andric return nullptr; 56*349cc55cSDimitry Andric } 57*349cc55cSDimitry Andric } 58*349cc55cSDimitry Andric 59*349cc55cSDimitry Andric return TheTarget; 60*349cc55cSDimitry Andric } 61*349cc55cSDimitry Andric 62*349cc55cSDimitry Andric const Target *TargetRegistry::lookupTarget(const std::string &TT, 63*349cc55cSDimitry Andric std::string &Error) { 64*349cc55cSDimitry Andric // Provide special warning when no targets are initialized. 65*349cc55cSDimitry Andric if (targets().begin() == targets().end()) { 66*349cc55cSDimitry Andric Error = "Unable to find target for this triple (no targets are registered)"; 67*349cc55cSDimitry Andric return nullptr; 68*349cc55cSDimitry Andric } 69*349cc55cSDimitry Andric Triple::ArchType Arch = Triple(TT).getArch(); 70*349cc55cSDimitry Andric auto ArchMatch = [&](const Target &T) { return T.ArchMatchFn(Arch); }; 71*349cc55cSDimitry Andric auto I = find_if(targets(), ArchMatch); 72*349cc55cSDimitry Andric 73*349cc55cSDimitry Andric if (I == targets().end()) { 74*349cc55cSDimitry Andric Error = "No available targets are compatible with triple \"" + TT + "\""; 75*349cc55cSDimitry Andric return nullptr; 76*349cc55cSDimitry Andric } 77*349cc55cSDimitry Andric 78*349cc55cSDimitry Andric auto J = std::find_if(std::next(I), targets().end(), ArchMatch); 79*349cc55cSDimitry Andric if (J != targets().end()) { 80*349cc55cSDimitry Andric Error = std::string("Cannot choose between targets \"") + I->Name + 81*349cc55cSDimitry Andric "\" and \"" + J->Name + "\""; 82*349cc55cSDimitry Andric return nullptr; 83*349cc55cSDimitry Andric } 84*349cc55cSDimitry Andric 85*349cc55cSDimitry Andric return &*I; 86*349cc55cSDimitry Andric } 87*349cc55cSDimitry Andric 88*349cc55cSDimitry Andric void TargetRegistry::RegisterTarget(Target &T, const char *Name, 89*349cc55cSDimitry Andric const char *ShortDesc, 90*349cc55cSDimitry Andric const char *BackendName, 91*349cc55cSDimitry Andric Target::ArchMatchFnTy ArchMatchFn, 92*349cc55cSDimitry Andric bool HasJIT) { 93*349cc55cSDimitry Andric assert(Name && ShortDesc && ArchMatchFn && 94*349cc55cSDimitry Andric "Missing required target information!"); 95*349cc55cSDimitry Andric 96*349cc55cSDimitry Andric // Check if this target has already been initialized, we allow this as a 97*349cc55cSDimitry Andric // convenience to some clients. 98*349cc55cSDimitry Andric if (T.Name) 99*349cc55cSDimitry Andric return; 100*349cc55cSDimitry Andric 101*349cc55cSDimitry Andric // Add to the list of targets. 102*349cc55cSDimitry Andric T.Next = FirstTarget; 103*349cc55cSDimitry Andric FirstTarget = &T; 104*349cc55cSDimitry Andric 105*349cc55cSDimitry Andric T.Name = Name; 106*349cc55cSDimitry Andric T.ShortDesc = ShortDesc; 107*349cc55cSDimitry Andric T.BackendName = BackendName; 108*349cc55cSDimitry Andric T.ArchMatchFn = ArchMatchFn; 109*349cc55cSDimitry Andric T.HasJIT = HasJIT; 110*349cc55cSDimitry Andric } 111*349cc55cSDimitry Andric 112*349cc55cSDimitry Andric static int TargetArraySortFn(const std::pair<StringRef, const Target *> *LHS, 113*349cc55cSDimitry Andric const std::pair<StringRef, const Target *> *RHS) { 114*349cc55cSDimitry Andric return LHS->first.compare(RHS->first); 115*349cc55cSDimitry Andric } 116*349cc55cSDimitry Andric 117*349cc55cSDimitry Andric void TargetRegistry::printRegisteredTargetsForVersion(raw_ostream &OS) { 118*349cc55cSDimitry Andric std::vector<std::pair<StringRef, const Target*> > Targets; 119*349cc55cSDimitry Andric size_t Width = 0; 120*349cc55cSDimitry Andric for (const auto &T : TargetRegistry::targets()) { 121*349cc55cSDimitry Andric Targets.push_back(std::make_pair(T.getName(), &T)); 122*349cc55cSDimitry Andric Width = std::max(Width, Targets.back().first.size()); 123*349cc55cSDimitry Andric } 124*349cc55cSDimitry Andric array_pod_sort(Targets.begin(), Targets.end(), TargetArraySortFn); 125*349cc55cSDimitry Andric 126*349cc55cSDimitry Andric OS << " Registered Targets:\n"; 127*349cc55cSDimitry Andric for (unsigned i = 0, e = Targets.size(); i != e; ++i) { 128*349cc55cSDimitry Andric OS << " " << Targets[i].first; 129*349cc55cSDimitry Andric OS.indent(Width - Targets[i].first.size()) << " - " 130*349cc55cSDimitry Andric << Targets[i].second->getShortDescription() << '\n'; 131*349cc55cSDimitry Andric } 132*349cc55cSDimitry Andric if (Targets.empty()) 133*349cc55cSDimitry Andric OS << " (none)\n"; 134*349cc55cSDimitry Andric } 135