blob: 470deac24020ad1593248b88f13afa572498ae36 [file] [log] [blame]
Daniel Dunbarbb061292009-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
Daniel Dunbara5881e32009-07-26 02:12:58 +000010#include "llvm/Module.h"
Daniel Dunbarbb061292009-07-15 04:24:58 +000011#include "llvm/Target/TargetRegistry.h"
Daniel Dunbard6fd3772009-07-25 10:09:50 +000012#include "llvm/System/Host.h"
Daniel Dunbarbb061292009-07-15 04:24:58 +000013#include <cassert>
14using namespace llvm;
15
Daniel Dunbar73b3ec42009-07-15 07:09:29 +000016// Clients are responsible for avoid race conditions in registration.
Daniel Dunbarbb061292009-07-15 04:24:58 +000017static Target *FirstTarget = 0;
18
Daniel Dunbar603bea32009-07-16 02:06:09 +000019TargetRegistry::iterator TargetRegistry::begin() {
20 return iterator(FirstTarget);
21}
22
Daniel Dunbara5881e32009-07-26 02:12:58 +000023const Target *TargetRegistry::lookupTarget(const std::string &TT,
24 bool FallbackToHost,
25 bool RequireJIT,
26 std::string &Error) {
Daniel Dunbar7df0c072009-07-17 15:50:49 +000027 // 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 Dunbar603bea32009-07-16 02:06:09 +000032 const Target *Best = 0, *EquallyBest = 0;
Daniel Dunbarbb061292009-07-15 04:24:58 +000033 unsigned BestQuality = 0;
Daniel Dunbar603bea32009-07-16 02:06:09 +000034 for (iterator it = begin(), ie = end(); it != ie; ++it) {
Daniel Dunbara5881e32009-07-26 02:12:58 +000035 if (RequireJIT && !it->hasJIT())
36 continue;
37
Daniel Dunbar603bea32009-07-16 02:06:09 +000038 if (unsigned Qual = it->TripleMatchQualityFn(TT)) {
Daniel Dunbarbb061292009-07-15 04:24:58 +000039 if (!Best || Qual > BestQuality) {
Daniel Dunbar603bea32009-07-16 02:06:09 +000040 Best = &*it;
Daniel Dunbarbb061292009-07-15 04:24:58 +000041 EquallyBest = 0;
42 BestQuality = Qual;
43 } else if (Qual == BestQuality)
Daniel Dunbar603bea32009-07-16 02:06:09 +000044 EquallyBest = &*it;
Daniel Dunbarbb061292009-07-15 04:24:58 +000045 }
46 }
47
Daniel Dunbara5881e32009-07-26 02:12:58 +000048 // 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 Dunbarbb061292009-07-15 04:24:58 +000057 if (!Best) {
Daniel Dunbar7df0c072009-07-17 15:50:49 +000058 Error = "No available targets are compatible with this triple";
Daniel Dunbarbb061292009-07-15 04:24:58 +000059 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 Dunbarbb061292009-07-15 04:24:58 +000073void TargetRegistry::RegisterTarget(Target &T,
74 const char *Name,
75 const char *ShortDesc,
76 Target::TripleMatchQualityFnTy TQualityFn,
Daniel Dunbard6fd3772009-07-25 10:09:50 +000077 bool HasJIT) {
Daniel Dunbarfa27ff22009-07-26 02:22:58 +000078 assert(Name && ShortDesc && TQualityFn &&
Daniel Dunbarbb061292009-07-15 04:24:58 +000079 "Missing required target information!");
Daniel Dunbar51b198a2009-07-15 20:24:03 +000080
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 Dunbarbb061292009-07-15 04:24:58 +000085
86 // Add to the list of targets.
87 T.Next = FirstTarget;
Daniel Dunbarf23d4932009-07-15 07:37:49 +000088 FirstTarget = &T;
Daniel Dunbarbb061292009-07-15 04:24:58 +000089
90 T.Name = Name;
91 T.ShortDesc = ShortDesc;
92 T.TripleMatchQualityFn = TQualityFn;
Daniel Dunbard6fd3772009-07-25 10:09:50 +000093 T.HasJIT = HasJIT;
Daniel Dunbarbb061292009-07-15 04:24:58 +000094}
95