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;
18 TargetRegistry::getClosestStaticTargetForTriple(const std::string &TT,
20 Target *Best = 0, *EquallyBest = 0;
21 unsigned BestQuality = 0;
22 // FIXME: Use iterator.
23 for (Target *i = FirstTarget; i; i = i->Next) {
24 if (unsigned Qual = i->TripleMatchQualityFn(TT)) {
25 if (!Best || Qual > BestQuality) {
29 } else if (Qual == BestQuality)
35 Error = "No available targets are compatible with this module";
39 // Otherwise, take the best target, but make sure we don't have two equally
42 Error = std::string("Cannot choose between targets \"") +
43 Best->Name + "\" and \"" + EquallyBest->Name + "\"";
51 TargetRegistry::getClosestStaticTargetForModule(const Module &M,
53 Target *Best = 0, *EquallyBest = 0;
54 unsigned BestQuality = 0;
55 // FIXME: Use iterator.
56 for (Target *i = FirstTarget; i; i = i->Next) {
57 if (unsigned Qual = i->ModuleMatchQualityFn(M)) {
58 if (!Best || Qual > BestQuality) {
62 } else if (Qual == BestQuality)
68 Error = "No available targets are compatible with this module";
72 // Otherwise, take the best target, but make sure we don't have two equally
75 Error = std::string("Cannot choose between targets \"") +
76 Best->Name + "\" and \"" + EquallyBest->Name + "\"";
84 TargetRegistry::getClosestTargetForJIT(std::string &Error) {
85 Target *Best = 0, *EquallyBest = 0;
86 unsigned BestQuality = 0;
87 // FIXME: Use iterator.
88 for (Target *i = FirstTarget; i; i = i->Next) {
89 if (unsigned Qual = i->JITMatchQualityFn()) {
90 if (!Best || Qual > BestQuality) {
94 } else if (Qual == BestQuality)
100 Error = "No JIT is available for this host";
104 // Return the best, ignoring ties.
108 void TargetRegistry::RegisterTarget(Target &T,
110 const char *ShortDesc,
111 Target::TripleMatchQualityFnTy TQualityFn,
112 Target::ModuleMatchQualityFnTy MQualityFn,
113 Target::JITMatchQualityFnTy JITQualityFn) {
114 assert(Name && ShortDesc && TQualityFn && MQualityFn && JITQualityFn &&
115 "Missing required target information!");
117 // Check if this target has already been initialized, we allow this as a
118 // convenience to some clients.
122 // Add to the list of targets.
123 T.Next = FirstTarget;
127 T.ShortDesc = ShortDesc;
128 T.TripleMatchQualityFn = TQualityFn;
129 T.ModuleMatchQualityFn = MQualityFn;
130 T.JITMatchQualityFn = JITQualityFn;