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 // 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 + "\"";
59 TargetRegistry::getClosestStaticTargetForModule(const Module &M,
61 // Provide special warning when no targets are initialized.
62 if (begin() == end()) {
63 Error = "Unable to find target for this module (no targets are registered)";
67 const Target *Best = 0, *EquallyBest = 0;
68 unsigned BestQuality = 0;
69 for (iterator it = begin(), ie = end(); it != ie; ++it) {
70 if (unsigned Qual = it->ModuleMatchQualityFn(M)) {
71 if (!Best || Qual > BestQuality) {
75 } else if (Qual == BestQuality)
81 Error = "No available targets are compatible with this module";
85 // Otherwise, take the best target, but make sure we don't have two equally
88 Error = std::string("Cannot choose between targets \"") +
89 Best->Name + "\" and \"" + EquallyBest->Name + "\"";
97 TargetRegistry::getClosestTargetForJIT(std::string &Error) {
98 // Provide special warning when no targets are initialized.
99 if (begin() == end()) {
100 Error = "No JIT is available for this host (no targets are registered)";
104 const Target *Best = 0, *EquallyBest = 0;
105 unsigned BestQuality = 0;
106 for (iterator it = begin(), ie = end(); it != ie; ++it) {
107 if (unsigned Qual = it->JITMatchQualityFn()) {
108 if (!Best || Qual > BestQuality) {
112 } else if (Qual == BestQuality)
118 Error = "No JIT is available for this host";
122 // Return the best, ignoring ties.
126 void TargetRegistry::RegisterTarget(Target &T,
128 const char *ShortDesc,
129 Target::TripleMatchQualityFnTy TQualityFn,
130 Target::ModuleMatchQualityFnTy MQualityFn,
131 Target::JITMatchQualityFnTy JITQualityFn) {
132 assert(Name && ShortDesc && TQualityFn && MQualityFn && JITQualityFn &&
133 "Missing required target information!");
135 // Check if this target has already been initialized, we allow this as a
136 // convenience to some clients.
140 // Add to the list of targets.
141 T.Next = FirstTarget;
145 T.ShortDesc = ShortDesc;
146 T.TripleMatchQualityFn = TQualityFn;
147 T.ModuleMatchQualityFn = MQualityFn;
148 T.JITMatchQualityFn = JITQualityFn;