blob: 4558d2d2159dfcc968da6168a0da407bf6fb0496 [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
Daniel Dunbar48224ee2009-07-26 02:12:58 +000010#include "llvm/Module.h"
Daniel Dunbar351304b2009-07-15 04:24:58 +000011#include "llvm/Target/TargetRegistry.h"
Daniel Dunbar123157a2009-07-25 10:09:50 +000012#include "llvm/System/Host.h"
Daniel Dunbar351304b2009-07-15 04:24:58 +000013#include <cassert>
14using namespace llvm;
15
Daniel Dunbar988af812009-07-15 07:09:29 +000016// Clients are responsible for avoid race conditions in registration.
Daniel Dunbar351304b2009-07-15 04:24:58 +000017static Target *FirstTarget = 0;
18
Daniel Dunbar9b3edb62009-07-16 02:06:09 +000019TargetRegistry::iterator TargetRegistry::begin() {
20 return iterator(FirstTarget);
21}
22
Daniel Dunbar48224ee2009-07-26 02:12:58 +000023const Target *TargetRegistry::lookupTarget(const std::string &TT,
24 bool FallbackToHost,
25 bool RequireJIT,
26 std::string &Error) {
Daniel Dunbar766e20f2009-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 Dunbar9b3edb62009-07-16 02:06:09 +000032 const Target *Best = 0, *EquallyBest = 0;
Daniel Dunbar351304b2009-07-15 04:24:58 +000033 unsigned BestQuality = 0;
Daniel Dunbar9b3edb62009-07-16 02:06:09 +000034 for (iterator it = begin(), ie = end(); it != ie; ++it) {
Daniel Dunbar48224ee2009-07-26 02:12:58 +000035 if (RequireJIT && !it->hasJIT())
36 continue;
37
Daniel Dunbar9b3edb62009-07-16 02:06:09 +000038 if (unsigned Qual = it->TripleMatchQualityFn(TT)) {
Daniel Dunbar351304b2009-07-15 04:24:58 +000039 if (!Best || Qual > BestQuality) {
Daniel Dunbar9b3edb62009-07-16 02:06:09 +000040 Best = &*it;
Daniel Dunbar351304b2009-07-15 04:24:58 +000041 EquallyBest = 0;
42 BestQuality = Qual;
43 } else if (Qual == BestQuality)
Daniel Dunbar9b3edb62009-07-16 02:06:09 +000044 EquallyBest = &*it;
Daniel Dunbar351304b2009-07-15 04:24:58 +000045 }
46 }
47
Daniel Dunbar48224ee2009-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 Dunbar351304b2009-07-15 04:24:58 +000057 if (!Best) {
Daniel Dunbar766e20f2009-07-17 15:50:49 +000058 Error = "No available targets are compatible with this triple";
Daniel Dunbar351304b2009-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 Dunbar351304b2009-07-15 04:24:58 +000073void TargetRegistry::RegisterTarget(Target &T,
74 const char *Name,
75 const char *ShortDesc,
76 Target::TripleMatchQualityFnTy TQualityFn,
77 Target::ModuleMatchQualityFnTy MQualityFn,
Daniel Dunbar123157a2009-07-25 10:09:50 +000078 bool HasJIT) {
79 assert(Name && ShortDesc && TQualityFn && MQualityFn &&
Daniel Dunbar351304b2009-07-15 04:24:58 +000080 "Missing required target information!");
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000081
82 // Check if this target has already been initialized, we allow this as a
83 // convenience to some clients.
84 if (T.Name)
85 return;
Daniel Dunbar351304b2009-07-15 04:24:58 +000086
87 // Add to the list of targets.
88 T.Next = FirstTarget;
Daniel Dunbar0a346f42009-07-15 07:37:49 +000089 FirstTarget = &T;
Daniel Dunbar351304b2009-07-15 04:24:58 +000090
91 T.Name = Name;
92 T.ShortDesc = ShortDesc;
93 T.TripleMatchQualityFn = TQualityFn;
94 T.ModuleMatchQualityFn = MQualityFn;
Daniel Dunbar123157a2009-07-25 10:09:50 +000095 T.HasJIT = HasJIT;
Daniel Dunbar351304b2009-07-15 04:24:58 +000096}
97