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 2406c3fb27SDimitry Andric const Target *TargetRegistry::lookupTarget(StringRef 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()) { 3606c3fb27SDimitry Andric Error = ("invalid target '" + ArchName + "'.\n").str(); 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) { 52*5f757f3fSDimitry Andric Error = "unable to get target for '" + TheTriple.getTriple() + 53*5f757f3fSDimitry Andric "', see --version and --triple."; 54349cc55cSDimitry Andric return nullptr; 55349cc55cSDimitry Andric } 56349cc55cSDimitry Andric } 57349cc55cSDimitry Andric 58349cc55cSDimitry Andric return TheTarget; 59349cc55cSDimitry Andric } 60349cc55cSDimitry Andric 6106c3fb27SDimitry Andric const Target *TargetRegistry::lookupTarget(StringRef TT, std::string &Error) { 62349cc55cSDimitry Andric // Provide special warning when no targets are initialized. 63349cc55cSDimitry Andric if (targets().begin() == targets().end()) { 64349cc55cSDimitry Andric Error = "Unable to find target for this triple (no targets are registered)"; 65349cc55cSDimitry Andric return nullptr; 66349cc55cSDimitry Andric } 67349cc55cSDimitry Andric Triple::ArchType Arch = Triple(TT).getArch(); 68349cc55cSDimitry Andric auto ArchMatch = [&](const Target &T) { return T.ArchMatchFn(Arch); }; 69349cc55cSDimitry Andric auto I = find_if(targets(), ArchMatch); 70349cc55cSDimitry Andric 71349cc55cSDimitry Andric if (I == targets().end()) { 7206c3fb27SDimitry Andric Error = ("No available targets are compatible with triple \"" + TT + "\"") 7306c3fb27SDimitry Andric .str(); 74349cc55cSDimitry Andric return nullptr; 75349cc55cSDimitry Andric } 76349cc55cSDimitry Andric 77349cc55cSDimitry Andric auto J = std::find_if(std::next(I), targets().end(), ArchMatch); 78349cc55cSDimitry Andric if (J != targets().end()) { 79349cc55cSDimitry Andric Error = std::string("Cannot choose between targets \"") + I->Name + 80349cc55cSDimitry Andric "\" and \"" + J->Name + "\""; 81349cc55cSDimitry Andric return nullptr; 82349cc55cSDimitry Andric } 83349cc55cSDimitry Andric 84349cc55cSDimitry Andric return &*I; 85349cc55cSDimitry Andric } 86349cc55cSDimitry Andric 87349cc55cSDimitry Andric void TargetRegistry::RegisterTarget(Target &T, const char *Name, 88349cc55cSDimitry Andric const char *ShortDesc, 89349cc55cSDimitry Andric const char *BackendName, 90349cc55cSDimitry Andric Target::ArchMatchFnTy ArchMatchFn, 91349cc55cSDimitry Andric bool HasJIT) { 92349cc55cSDimitry Andric assert(Name && ShortDesc && ArchMatchFn && 93349cc55cSDimitry Andric "Missing required target information!"); 94349cc55cSDimitry Andric 95349cc55cSDimitry Andric // Check if this target has already been initialized, we allow this as a 96349cc55cSDimitry Andric // convenience to some clients. 97349cc55cSDimitry Andric if (T.Name) 98349cc55cSDimitry Andric return; 99349cc55cSDimitry Andric 100349cc55cSDimitry Andric // Add to the list of targets. 101349cc55cSDimitry Andric T.Next = FirstTarget; 102349cc55cSDimitry Andric FirstTarget = &T; 103349cc55cSDimitry Andric 104349cc55cSDimitry Andric T.Name = Name; 105349cc55cSDimitry Andric T.ShortDesc = ShortDesc; 106349cc55cSDimitry Andric T.BackendName = BackendName; 107349cc55cSDimitry Andric T.ArchMatchFn = ArchMatchFn; 108349cc55cSDimitry Andric T.HasJIT = HasJIT; 109349cc55cSDimitry Andric } 110349cc55cSDimitry Andric 111349cc55cSDimitry Andric static int TargetArraySortFn(const std::pair<StringRef, const Target *> *LHS, 112349cc55cSDimitry Andric const std::pair<StringRef, const Target *> *RHS) { 113349cc55cSDimitry Andric return LHS->first.compare(RHS->first); 114349cc55cSDimitry Andric } 115349cc55cSDimitry Andric 116349cc55cSDimitry Andric void TargetRegistry::printRegisteredTargetsForVersion(raw_ostream &OS) { 117349cc55cSDimitry Andric std::vector<std::pair<StringRef, const Target*> > Targets; 118349cc55cSDimitry Andric size_t Width = 0; 119349cc55cSDimitry Andric for (const auto &T : TargetRegistry::targets()) { 120349cc55cSDimitry Andric Targets.push_back(std::make_pair(T.getName(), &T)); 121349cc55cSDimitry Andric Width = std::max(Width, Targets.back().first.size()); 122349cc55cSDimitry Andric } 123349cc55cSDimitry Andric array_pod_sort(Targets.begin(), Targets.end(), TargetArraySortFn); 124349cc55cSDimitry Andric 125bdd1243dSDimitry Andric OS << "\n"; 126349cc55cSDimitry Andric OS << " Registered Targets:\n"; 1270eae32dcSDimitry Andric for (const auto &Target : Targets) { 1280eae32dcSDimitry Andric OS << " " << Target.first; 1290eae32dcSDimitry Andric OS.indent(Width - Target.first.size()) 1300eae32dcSDimitry Andric << " - " << Target.second->getShortDescription() << '\n'; 131349cc55cSDimitry Andric } 132349cc55cSDimitry Andric if (Targets.empty()) 133349cc55cSDimitry Andric OS << " (none)\n"; 134349cc55cSDimitry Andric } 135