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"
14 // Clients are responsible for avoid race conditions in registration.
15 static Target *FirstTarget = 0;
17 TargetRegistry::iterator TargetRegistry::begin() {
18 return iterator(FirstTarget);
22 TargetRegistry::getClosestStaticTargetForTriple(const std::string &TT,
24 const Target *Best = 0, *EquallyBest = 0;
25 unsigned BestQuality = 0;
26 for (iterator it = begin(), ie = end(); it != ie; ++it) {
27 if (unsigned Qual = it->TripleMatchQualityFn(TT)) {
28 if (!Best || Qual > BestQuality) {
32 } else if (Qual == BestQuality)
38 Error = "No available targets are compatible with this module";
42 // Otherwise, take the best target, but make sure we don't have two equally
45 Error = std::string("Cannot choose between targets \"") +
46 Best->Name + "\" and \"" + EquallyBest->Name + "\"";
54 TargetRegistry::getClosestStaticTargetForModule(const Module &M,
56 const Target *Best = 0, *EquallyBest = 0;
57 unsigned BestQuality = 0;
58 for (iterator it = begin(), ie = end(); it != ie; ++it) {
59 if (unsigned Qual = it->ModuleMatchQualityFn(M)) {
60 if (!Best || Qual > BestQuality) {
64 } else if (Qual == BestQuality)
70 Error = "No available targets are compatible with this module";
74 // Otherwise, take the best target, but make sure we don't have two equally
77 Error = std::string("Cannot choose between targets \"") +
78 Best->Name + "\" and \"" + EquallyBest->Name + "\"";
86 TargetRegistry::getClosestTargetForJIT(std::string &Error) {
87 const Target *Best = 0, *EquallyBest = 0;
88 unsigned BestQuality = 0;
89 for (iterator it = begin(), ie = end(); it != ie; ++it) {
90 if (unsigned Qual = it->JITMatchQualityFn()) {
91 if (!Best || Qual > BestQuality) {
95 } else if (Qual == BestQuality)
101 Error = "No JIT is available for this host";
105 // Return the best, ignoring ties.
109 void TargetRegistry::RegisterTarget(Target &T,
111 const char *ShortDesc,
112 Target::TripleMatchQualityFnTy TQualityFn,
113 Target::ModuleMatchQualityFnTy MQualityFn,
114 Target::JITMatchQualityFnTy JITQualityFn) {
115 assert(Name && ShortDesc && TQualityFn && MQualityFn && JITQualityFn &&
116 "Missing required target information!");
118 // Check if this target has already been initialized, we allow this as a
119 // convenience to some clients.
123 // Add to the list of targets.
124 T.Next = FirstTarget;
128 T.ShortDesc = ShortDesc;
129 T.TripleMatchQualityFn = TQualityFn;
130 T.ModuleMatchQualityFn = MQualityFn;
131 T.JITMatchQualityFn = JITQualityFn;