1 //===--- TargetRegistry.cpp - Target registration -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Target/TargetRegistry.h"
11 #include "llvm/System/Host.h"
15 // Clients are responsible for avoid race conditions in registration.
16 static Target *FirstTarget = 0;
18 TargetRegistry::iterator TargetRegistry::begin() {
19 return iterator(FirstTarget);
22 const Target *TargetRegistry::lookupTarget(const std::string &TT,
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)";
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) {
37 } else if (Qual == BestQuality)
43 Error = "No available targets are compatible with this triple";
47 // Otherwise, take the best target, but make sure we don't have two equally
50 Error = std::string("Cannot choose between targets \"") +
51 Best->Name + "\" and \"" + EquallyBest->Name + "\"";
58 void TargetRegistry::RegisterTarget(Target &T,
60 const char *ShortDesc,
61 Target::TripleMatchQualityFnTy TQualityFn,
63 assert(Name && ShortDesc && TQualityFn &&
64 "Missing required target information!");
66 // Check if this target has already been initialized, we allow this as a
67 // convenience to some clients.
71 // Add to the list of targets.
76 T.ShortDesc = ShortDesc;
77 T.TripleMatchQualityFn = TQualityFn;
81 const Target *TargetRegistry::getClosestTargetForJIT(std::string &Error) {
82 const Target *TheTarget = lookupTarget(sys::getHostTriple(), Error);
84 if (TheTarget && !TheTarget->hasJIT()) {
85 Error = "No JIT compatible target available for this host";