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