blob: c60322bdcd7ba93ce93534bb392e8518d0a9a5b6 [file] [log] [blame]
Daniel Dunbar351304b2009-07-15 04:24:58 +00001//===--- 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 Dunbar123157a2009-07-25 10:09:50 +000011#include "llvm/System/Host.h"
Daniel Dunbar351304b2009-07-15 04:24:58 +000012#include <cassert>
13using namespace llvm;
14
Daniel Dunbar988af812009-07-15 07:09:29 +000015// Clients are responsible for avoid race conditions in registration.
Daniel Dunbar351304b2009-07-15 04:24:58 +000016static Target *FirstTarget = 0;
17
Daniel Dunbar9b3edb62009-07-16 02:06:09 +000018TargetRegistry::iterator TargetRegistry::begin() {
19 return iterator(FirstTarget);
20}
21
Daniel Dunbar48224ee2009-07-26 02:12:58 +000022const Target *TargetRegistry::lookupTarget(const std::string &TT,
23 bool FallbackToHost,
24 bool RequireJIT,
25 std::string &Error) {
Daniel Dunbar766e20f2009-07-17 15:50:49 +000026 // Provide special warning when no targets are initialized.
27 if (begin() == end()) {
28 Error = "Unable to find target for this triple (no targets are registered)";
29 return 0;
30 }
Daniel Dunbar9b3edb62009-07-16 02:06:09 +000031 const Target *Best = 0, *EquallyBest = 0;
Daniel Dunbar351304b2009-07-15 04:24:58 +000032 unsigned BestQuality = 0;
Daniel Dunbar9b3edb62009-07-16 02:06:09 +000033 for (iterator it = begin(), ie = end(); it != ie; ++it) {
Daniel Dunbar48224ee2009-07-26 02:12:58 +000034 if (RequireJIT && !it->hasJIT())
35 continue;
36
Daniel Dunbar9b3edb62009-07-16 02:06:09 +000037 if (unsigned Qual = it->TripleMatchQualityFn(TT)) {
Daniel Dunbar351304b2009-07-15 04:24:58 +000038 if (!Best || Qual > BestQuality) {
Daniel Dunbar9b3edb62009-07-16 02:06:09 +000039 Best = &*it;
Daniel Dunbar351304b2009-07-15 04:24:58 +000040 EquallyBest = 0;
41 BestQuality = Qual;
42 } else if (Qual == BestQuality)
Daniel Dunbar9b3edb62009-07-16 02:06:09 +000043 EquallyBest = &*it;
Daniel Dunbar351304b2009-07-15 04:24:58 +000044 }
45 }
46
Daniel Dunbar48224ee2009-07-26 02:12:58 +000047 // FIXME: Hack. If we only have an extremely weak match and the client
48 // requested to fall back to the host, then ignore it and try again.
49 if (BestQuality == 1 && FallbackToHost)
50 Best = 0;
51
52 // Fallback to the host triple if we didn't find anything.
53 if (!Best && FallbackToHost)
54 return lookupTarget(sys::getHostTriple(), false, RequireJIT, Error);
55
Daniel Dunbar351304b2009-07-15 04:24:58 +000056 if (!Best) {
Daniel Dunbar766e20f2009-07-17 15:50:49 +000057 Error = "No available targets are compatible with this triple";
Daniel Dunbar351304b2009-07-15 04:24:58 +000058 return 0;
59 }
60
61 // Otherwise, take the best target, but make sure we don't have two equally
62 // good best targets.
63 if (EquallyBest) {
64 Error = std::string("Cannot choose between targets \"") +
65 Best->Name + "\" and \"" + EquallyBest->Name + "\"";
66 return 0;
67 }
68
69 return Best;
70}
71
Daniel Dunbar351304b2009-07-15 04:24:58 +000072void TargetRegistry::RegisterTarget(Target &T,
73 const char *Name,
74 const char *ShortDesc,
75 Target::TripleMatchQualityFnTy TQualityFn,
Daniel Dunbar123157a2009-07-25 10:09:50 +000076 bool HasJIT) {
Daniel Dunbarbfa8b2c2009-07-26 02:22:58 +000077 assert(Name && ShortDesc && TQualityFn &&
Daniel Dunbar351304b2009-07-15 04:24:58 +000078 "Missing required target information!");
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000079
80 // Check if this target has already been initialized, we allow this as a
81 // convenience to some clients.
82 if (T.Name)
83 return;
Daniel Dunbar351304b2009-07-15 04:24:58 +000084
85 // Add to the list of targets.
86 T.Next = FirstTarget;
Daniel Dunbar0a346f42009-07-15 07:37:49 +000087 FirstTarget = &T;
Daniel Dunbar351304b2009-07-15 04:24:58 +000088
89 T.Name = Name;
90 T.ShortDesc = ShortDesc;
91 T.TripleMatchQualityFn = TQualityFn;
Daniel Dunbar123157a2009-07-25 10:09:50 +000092 T.HasJIT = HasJIT;
Daniel Dunbar351304b2009-07-15 04:24:58 +000093}
94