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 | |
Daniel Dunbar | a5881e3 | 2009-07-26 02:12:58 +0000 | [diff] [blame] | 10 | #include "llvm/Module.h" |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 11 | #include "llvm/Target/TargetRegistry.h" |
Daniel Dunbar | d6fd377 | 2009-07-25 10:09:50 +0000 | [diff] [blame] | 12 | #include "llvm/System/Host.h" |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 13 | #include <cassert> |
| 14 | using namespace llvm; |
| 15 | |
Daniel Dunbar | 73b3ec4 | 2009-07-15 07:09:29 +0000 | [diff] [blame] | 16 | // Clients are responsible for avoid race conditions in registration. |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 17 | static Target *FirstTarget = 0; |
| 18 | |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 19 | TargetRegistry::iterator TargetRegistry::begin() { |
| 20 | return iterator(FirstTarget); |
| 21 | } |
| 22 | |
Daniel Dunbar | a5881e3 | 2009-07-26 02:12:58 +0000 | [diff] [blame] | 23 | const Target *TargetRegistry::lookupTarget(const std::string &TT, |
| 24 | bool FallbackToHost, |
| 25 | bool RequireJIT, |
| 26 | std::string &Error) { |
Daniel Dunbar | 7df0c07 | 2009-07-17 15:50:49 +0000 | [diff] [blame] | 27 | // Provide special warning when no targets are initialized. |
| 28 | if (begin() == end()) { |
| 29 | Error = "Unable to find target for this triple (no targets are registered)"; |
| 30 | return 0; |
| 31 | } |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 32 | const Target *Best = 0, *EquallyBest = 0; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 33 | unsigned BestQuality = 0; |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 34 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
Daniel Dunbar | a5881e3 | 2009-07-26 02:12:58 +0000 | [diff] [blame] | 35 | if (RequireJIT && !it->hasJIT()) |
| 36 | continue; |
| 37 | |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 38 | if (unsigned Qual = it->TripleMatchQualityFn(TT)) { |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 39 | if (!Best || Qual > BestQuality) { |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 40 | Best = &*it; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 41 | EquallyBest = 0; |
| 42 | BestQuality = Qual; |
| 43 | } else if (Qual == BestQuality) |
Daniel Dunbar | 603bea3 | 2009-07-16 02:06:09 +0000 | [diff] [blame] | 44 | EquallyBest = &*it; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
Daniel Dunbar | a5881e3 | 2009-07-26 02:12:58 +0000 | [diff] [blame] | 48 | // FIXME: Hack. If we only have an extremely weak match and the client |
| 49 | // requested to fall back to the host, then ignore it and try again. |
| 50 | if (BestQuality == 1 && FallbackToHost) |
| 51 | Best = 0; |
| 52 | |
| 53 | // Fallback to the host triple if we didn't find anything. |
| 54 | if (!Best && FallbackToHost) |
| 55 | return lookupTarget(sys::getHostTriple(), false, RequireJIT, Error); |
| 56 | |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 57 | if (!Best) { |
Daniel Dunbar | 7df0c07 | 2009-07-17 15:50:49 +0000 | [diff] [blame] | 58 | Error = "No available targets are compatible with this triple"; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | // Otherwise, take the best target, but make sure we don't have two equally |
| 63 | // good best targets. |
| 64 | if (EquallyBest) { |
| 65 | Error = std::string("Cannot choose between targets \"") + |
| 66 | Best->Name + "\" and \"" + EquallyBest->Name + "\""; |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | return Best; |
| 71 | } |
| 72 | |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 73 | void TargetRegistry::RegisterTarget(Target &T, |
| 74 | const char *Name, |
| 75 | const char *ShortDesc, |
| 76 | Target::TripleMatchQualityFnTy TQualityFn, |
Daniel Dunbar | d6fd377 | 2009-07-25 10:09:50 +0000 | [diff] [blame] | 77 | bool HasJIT) { |
Daniel Dunbar | fa27ff2 | 2009-07-26 02:22:58 +0000 | [diff] [blame^] | 78 | assert(Name && ShortDesc && TQualityFn && |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 79 | "Missing required target information!"); |
Daniel Dunbar | 51b198a | 2009-07-15 20:24:03 +0000 | [diff] [blame] | 80 | |
| 81 | // Check if this target has already been initialized, we allow this as a |
| 82 | // convenience to some clients. |
| 83 | if (T.Name) |
| 84 | return; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 85 | |
| 86 | // Add to the list of targets. |
| 87 | T.Next = FirstTarget; |
Daniel Dunbar | f23d493 | 2009-07-15 07:37:49 +0000 | [diff] [blame] | 88 | FirstTarget = &T; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 89 | |
| 90 | T.Name = Name; |
| 91 | T.ShortDesc = ShortDesc; |
| 92 | T.TripleMatchQualityFn = TQualityFn; |
Daniel Dunbar | d6fd377 | 2009-07-25 10:09:50 +0000 | [diff] [blame] | 93 | T.HasJIT = HasJIT; |
Daniel Dunbar | bb06129 | 2009-07-15 04:24:58 +0000 | [diff] [blame] | 94 | } |
| 95 | |