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/Module.h"
11 #include "llvm/Target/TargetRegistry.h"
12 #include "llvm/System/Host.h"
16 // Clients are responsible for avoid race conditions in registration.
17 static Target *FirstTarget = 0;
19 TargetRegistry::iterator TargetRegistry::begin() {
20 return iterator(FirstTarget);
23 const Target *TargetRegistry::lookupTarget(const std::string &TT,
27 // Provide special warning when no targets are initialized.
28 if (begin() == end()) {
29 Error = "Unable to find target for this triple (no targets are registered)";
32 const Target *Best = 0, *EquallyBest = 0;
33 unsigned BestQuality = 0;
34 for (iterator it = begin(), ie = end(); it != ie; ++it) {
35 if (RequireJIT && !it->hasJIT())
38 if (unsigned Qual = it->TripleMatchQualityFn(TT)) {
39 if (!Best || Qual > BestQuality) {
43 } else if (Qual == BestQuality)
48 // FIXME: Hack. If we only have an extremely weak match and the client
49 // requested to fall back to the host, then ignore it and try again.
50 if (BestQuality == 1 && FallbackToHost)
53 // Fallback to the host triple if we didn't find anything.
54 if (!Best && FallbackToHost)
55 return lookupTarget(sys::getHostTriple(), false, RequireJIT, Error);
58 Error = "No available targets are compatible with this triple";
62 // Otherwise, take the best target, but make sure we don't have two equally
65 Error = std::string("Cannot choose between targets \"") +
66 Best->Name + "\" and \"" + EquallyBest->Name + "\"";
73 void TargetRegistry::RegisterTarget(Target &T,
75 const char *ShortDesc,
76 Target::TripleMatchQualityFnTy TQualityFn,
78 assert(Name && ShortDesc && TQualityFn &&
79 "Missing required target information!");
81 // Check if this target has already been initialized, we allow this as a
82 // convenience to some clients.
86 // Add to the list of targets.
91 T.ShortDesc = ShortDesc;
92 T.TripleMatchQualityFn = TQualityFn;