Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/Target/TargetRegistry.h" |
Daniel Dunbar | d6fd377 | 2009-07-25 10:09:50 +0000 | [diff] [blame] | 11 | #include "llvm/System/Host.h" |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 12 | #include <cassert> |
| 13 | using namespace llvm; |
| 14 | |
Daniel Dunbar | 73b3ec4 | 2009-07-15 07:09:29 +0000 | [diff] [blame] | 15 | // Clients are responsible for avoid race conditions in registration. |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 16 | static Target *FirstTarget = 0; |
| 17 | |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 18 | TargetRegistry::iterator TargetRegistry::begin() { |
| 19 | return iterator(FirstTarget); |
| 20 | } |
| 21 | |
Daniel Dunbar | a5881e3 | 2009-07-26 02:12:58 +0000 | [diff] [blame] | 22 | const Target *TargetRegistry::lookupTarget(const std::string &TT, |
Daniel Dunbar | a5881e3 | 2009-07-26 02:12:58 +0000 | [diff] [blame] | 23 | std::string &Error) { |
Daniel Dunbar | 7df0c07 | 2009-07-17 15:50:49 +0000 | [diff] [blame] | 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; |
| 28 | } |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 29 | const Target *Best = 0, *EquallyBest = 0; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 30 | unsigned BestQuality = 0; |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 31 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 32 | if (unsigned Qual = it->TripleMatchQualityFn(TT)) { |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 33 | if (!Best || Qual > BestQuality) { |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 34 | Best = &*it; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 35 | EquallyBest = 0; |
| 36 | BestQuality = Qual; |
| 37 | } else if (Qual == BestQuality) |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 38 | EquallyBest = &*it; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
| 42 | if (!Best) { |
Daniel Dunbar | 0247656 | 2009-09-08 23:32:35 +0000 | [diff] [blame] | 43 | Error = "No available targets are compatible with this triple, " |
| 44 | "see -version for the available targets."; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 45 | return 0; |
| 46 | } |
| 47 | |
| 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; |
| 54 | } |
| 55 | |
| 56 | return Best; |
| 57 | } |
| 58 | |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 59 | void TargetRegistry::RegisterTarget(Target &T, |
| 60 | const char *Name, |
| 61 | const char *ShortDesc, |
| 62 | Target::TripleMatchQualityFnTy TQualityFn, |
Daniel Dunbar | d6fd377 | 2009-07-25 10:09:50 +0000 | [diff] [blame] | 63 | bool HasJIT) { |
Daniel Dunbar | fa27ff2 | 2009-07-26 02:22:58 +0000 | [diff] [blame] | 64 | assert(Name && ShortDesc && TQualityFn && |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 65 | "Missing required target information!"); |
Daniel Dunbar | 51b198a | 2009-07-15 20:24:03 +0000 | [diff] [blame] | 66 | |
| 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; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 71 | |
| 72 | // Add to the list of targets. |
| 73 | T.Next = FirstTarget; |
Daniel Dunbar | f23d493 | 2009-07-15 07:37:49 +0000 | [diff] [blame] | 74 | FirstTarget = &T; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 75 | |
| 76 | T.Name = Name; |
| 77 | T.ShortDesc = ShortDesc; |
| 78 | T.TripleMatchQualityFn = TQualityFn; |
Daniel Dunbar | d6fd377 | 2009-07-25 10:09:50 +0000 | [diff] [blame] | 79 | T.HasJIT = HasJIT; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Daniel Dunbar | 4bd03ab | 2009-08-03 04:20:57 +0000 | [diff] [blame] | 82 | const Target *TargetRegistry::getClosestTargetForJIT(std::string &Error) { |
| 83 | const Target *TheTarget = lookupTarget(sys::getHostTriple(), Error); |
| 84 | |
| 85 | if (TheTarget && !TheTarget->hasJIT()) { |
| 86 | Error = "No JIT compatible target available for this host"; |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | return TheTarget; |
| 91 | } |
| 92 | |