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 // FIXME: Worry about locking? In general everything should be registered at
17 static Target *FirstTarget = 0;
20 TargetRegistry::getClosestStaticTargetForTriple(const std::string &TT,
22 Target *Best = 0, *EquallyBest = 0;
23 unsigned BestQuality = 0;
24 // FIXME: Use iterator.
25 for (Target *i = FirstTarget; i; i = i->Next) {
26 if (unsigned Qual = i->TripleMatchQualityFn(TT)) {
27 if (!Best || Qual > BestQuality) {
31 } else if (Qual == BestQuality)
37 Error = "No available targets are compatible with this module";
41 // Otherwise, take the best target, but make sure we don't have two equally
44 Error = std::string("Cannot choose between targets \"") +
45 Best->Name + "\" and \"" + EquallyBest->Name + "\"";
53 TargetRegistry::getClosestStaticTargetForModule(const Module &M,
55 Target *Best = 0, *EquallyBest = 0;
56 unsigned BestQuality = 0;
57 // FIXME: Use iterator.
58 for (Target *i = FirstTarget; i; i = i->Next) {
59 if (unsigned Qual = i->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 Target *Best = 0, *EquallyBest = 0;
88 unsigned BestQuality = 0;
89 // FIXME: Use iterator.
90 for (Target *i = FirstTarget; i; i = i->Next) {
91 if (unsigned Qual = i->JITMatchQualityFn()) {
92 if (!Best || Qual > BestQuality) {
96 } else if (Qual == BestQuality)
102 Error = "No JIT is available for this host";
106 // Return the best, ignoring ties.
110 void TargetRegistry::RegisterTarget(Target &T,
112 const char *ShortDesc,
113 Target::TripleMatchQualityFnTy TQualityFn,
114 Target::ModuleMatchQualityFnTy MQualityFn,
115 Target::JITMatchQualityFnTy JITQualityFn) {
116 // Note that we don't require the constructor functions already be defined, in
117 // case a module happens to initialize the optional functionality before the
119 assert(!T.Next && !T.Name && !T.ShortDesc && !T.TripleMatchQualityFn &&
120 !T.ModuleMatchQualityFn && !T.JITMatchQualityFn &&
121 "This Target already registered!");
123 assert(Name && ShortDesc && TQualityFn && MQualityFn && JITQualityFn &&
124 "Missing required target information!");
126 // Add to the list of targets.
127 T.Next = FirstTarget;
128 FirstTarget = T.Next;
131 T.ShortDesc = ShortDesc;
132 T.TripleMatchQualityFn = TQualityFn;
133 T.ModuleMatchQualityFn = MQualityFn;
134 T.JITMatchQualityFn = JITQualityFn;