TargetRegistry.cpp revision 4bd03abe593222b26e84066223feb321bf738625
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/Target/TargetRegistry.h" 11#include "llvm/System/Host.h" 12#include <cassert> 13using namespace llvm; 14 15// Clients are responsible for avoid race conditions in registration. 16static Target *FirstTarget = 0; 17 18TargetRegistry::iterator TargetRegistry::begin() { 19 return iterator(FirstTarget); 20} 21 22const Target *TargetRegistry::lookupTarget(const std::string &TT, 23 std::string &Error) { 24 // Provide special warning when no targets are initialized. 25 if (begin() == end()) { 26 Error = "Unable to find target for this triple (no targets are registered)"; 27 return 0; 28 } 29 const Target *Best = 0, *EquallyBest = 0; 30 unsigned BestQuality = 0; 31 for (iterator it = begin(), ie = end(); it != ie; ++it) { 32 if (unsigned Qual = it->TripleMatchQualityFn(TT)) { 33 if (!Best || Qual > BestQuality) { 34 Best = &*it; 35 EquallyBest = 0; 36 BestQuality = Qual; 37 } else if (Qual == BestQuality) 38 EquallyBest = &*it; 39 } 40 } 41 42 if (!Best) { 43 Error = "No available targets are compatible with this triple"; 44 return 0; 45 } 46 47 // Otherwise, take the best target, but make sure we don't have two equally 48 // good best targets. 49 if (EquallyBest) { 50 Error = std::string("Cannot choose between targets \"") + 51 Best->Name + "\" and \"" + EquallyBest->Name + "\""; 52 return 0; 53 } 54 55 return Best; 56} 57 58void TargetRegistry::RegisterTarget(Target &T, 59 const char *Name, 60 const char *ShortDesc, 61 Target::TripleMatchQualityFnTy TQualityFn, 62 bool HasJIT) { 63 assert(Name && ShortDesc && TQualityFn && 64 "Missing required target information!"); 65 66 // Check if this target has already been initialized, we allow this as a 67 // convenience to some clients. 68 if (T.Name) 69 return; 70 71 // Add to the list of targets. 72 T.Next = FirstTarget; 73 FirstTarget = &T; 74 75 T.Name = Name; 76 T.ShortDesc = ShortDesc; 77 T.TripleMatchQualityFn = TQualityFn; 78 T.HasJIT = HasJIT; 79} 80 81const Target *TargetRegistry::getClosestTargetForJIT(std::string &Error) { 82 const Target *TheTarget = lookupTarget(sys::getHostTriple(), Error); 83 84 if (TheTarget && !TheTarget->hasJIT()) { 85 Error = "No JIT compatible target available for this host"; 86 return 0; 87 } 88 89 return TheTarget; 90} 91 92