LLVM API Documentation
00001 //===--- TargetRegistry.cpp - Target registration -------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #include "llvm/Target/TargetRegistry.h" 00011 #include "llvm/System/Host.h" 00012 #include <cassert> 00013 using namespace llvm; 00014 00015 // Clients are responsible for avoid race conditions in registration. 00016 static Target *FirstTarget = 0; 00017 00018 TargetRegistry::iterator TargetRegistry::begin() { 00019 return iterator(FirstTarget); 00020 } 00021 00022 const Target *TargetRegistry::lookupTarget(const std::string &TT, 00023 std::string &Error) { 00024 // Provide special warning when no targets are initialized. 00025 if (begin() == end()) { 00026 Error = "Unable to find target for this triple (no targets are registered)"; 00027 return 0; 00028 } 00029 const Target *Best = 0, *EquallyBest = 0; 00030 unsigned BestQuality = 0; 00031 for (iterator it = begin(), ie = end(); it != ie; ++it) { 00032 if (unsigned Qual = it->TripleMatchQualityFn(TT)) { 00033 if (!Best || Qual > BestQuality) { 00034 Best = &*it; 00035 EquallyBest = 0; 00036 BestQuality = Qual; 00037 } else if (Qual == BestQuality) 00038 EquallyBest = &*it; 00039 } 00040 } 00041 00042 if (!Best) { 00043 Error = "No available targets are compatible with this triple"; 00044 return 0; 00045 } 00046 00047 // Otherwise, take the best target, but make sure we don't have two equally 00048 // good best targets. 00049 if (EquallyBest) { 00050 Error = std::string("Cannot choose between targets \"") + 00051 Best->Name + "\" and \"" + EquallyBest->Name + "\""; 00052 return 0; 00053 } 00054 00055 return Best; 00056 } 00057 00058 void TargetRegistry::RegisterTarget(Target &T, 00059 const char *Name, 00060 const char *ShortDesc, 00061 Target::TripleMatchQualityFnTy TQualityFn, 00062 bool HasJIT) { 00063 assert(Name && ShortDesc && TQualityFn && 00064 "Missing required target information!"); 00065 00066 // Check if this target has already been initialized, we allow this as a 00067 // convenience to some clients. 00068 if (T.Name) 00069 return; 00070 00071 // Add to the list of targets. 00072 T.Next = FirstTarget; 00073 FirstTarget = &T; 00074 00075 T.Name = Name; 00076 T.ShortDesc = ShortDesc; 00077 T.TripleMatchQualityFn = TQualityFn; 00078 T.HasJIT = HasJIT; 00079 } 00080 00081 const Target *TargetRegistry::getClosestTargetForJIT(std::string &Error) { 00082 const Target *TheTarget = lookupTarget(sys::getHostTriple(), Error); 00083 00084 if (TheTarget && !TheTarget->hasJIT()) { 00085 Error = "No JIT compatible target available for this host"; 00086 return 0; 00087 } 00088 00089 return TheTarget; 00090 } 00091