Fixed some bugs.
[llvm/zpu.git] / lib / Support / TargetRegistry.cpp
blob5896447f5ea5f2b6c8949337248971d880dd780b
1 //===--- TargetRegistry.cpp - Target registration -------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Target/TargetRegistry.h"
11 #include "llvm/System/Host.h"
12 #include <cassert>
13 using namespace llvm;
15 // Clients are responsible for avoid race conditions in registration.
16 static Target *FirstTarget = 0;
18 TargetRegistry::iterator TargetRegistry::begin() {
19 return iterator(FirstTarget);
22 const Target *TargetRegistry::lookupTarget(const std::string &TT,
23 std::string &Error) {
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)";
27 return 0;
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) {
34 Best = &*it;
35 EquallyBest = 0;
36 BestQuality = Qual;
37 } else if (Qual == BestQuality)
38 EquallyBest = &*it;
42 if (!Best) {
43 Error = "No available targets are compatible with this triple, "
44 "see -version for the available targets.";
45 return 0;
48 // Otherwise, take the best target, but make sure we don't have two equally
49 // good best targets.
50 if (EquallyBest) {
51 Error = std::string("Cannot choose between targets \"") +
52 Best->Name + "\" and \"" + EquallyBest->Name + "\"";
53 return 0;
56 return Best;
59 void TargetRegistry::RegisterTarget(Target &T,
60 const char *Name,
61 const char *ShortDesc,
62 Target::TripleMatchQualityFnTy TQualityFn,
63 bool HasJIT) {
64 assert(Name && ShortDesc && TQualityFn &&
65 "Missing required target information!");
67 // Check if this target has already been initialized, we allow this as a
68 // convenience to some clients.
69 if (T.Name)
70 return;
72 // Add to the list of targets.
73 T.Next = FirstTarget;
74 FirstTarget = &T;
76 T.Name = Name;
77 T.ShortDesc = ShortDesc;
78 T.TripleMatchQualityFn = TQualityFn;
79 T.HasJIT = HasJIT;
82 const Target *TargetRegistry::getClosestTargetForJIT(std::string &Error) {
83 const Target *TheTarget = lookupTarget(sys::getHostTriple(), Error);
85 if (TheTarget && !TheTarget->hasJIT()) {
86 Error = "No JIT compatible target available for this host";
87 return 0;
90 return TheTarget;