llvm.org GIT mirror llvm / 7b3aa03
Fix pr18235. The cpp backend is not a reasonable fallback for a missing target. It is a very special backend, so it is reasonable to use it only if explicitly requested. While at it, simplify the interface a bit. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@197241 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 6 years ago
4 changed file(s) with 31 addition(s) and 41 deletion(s). Raw diff Collapse all Expand all
7878 public:
7979 friend struct TargetRegistry;
8080
81 typedef unsigned (*TripleMatchQualityFnTy)(const std::string &TT);
81 typedef bool (*ArchMatchFnTy)(Triple::ArchType Arch);
8282
8383 typedef MCAsmInfo *(*MCAsmInfoCtorFnTy)(const MCRegisterInfo &MRI,
8484 StringRef TT);
153153 /// TargetRegistry.
154154 Target *Next;
155155
156 /// TripleMatchQualityFn - The target function for rating the match quality
157 /// of a triple.
158 TripleMatchQualityFnTy TripleMatchQualityFn;
156 /// The target function for checking if an architecture is supported.
157 ArchMatchFnTy ArchMatchFn;
159158
160159 /// Name - The target name.
161160 const char *Name;
577576 /// @param Name - The target name. This should be a static string.
578577 /// @param ShortDesc - A short target description. This should be a static
579578 /// string.
580 /// @param TQualityFn - The triple match quality computation function for
581 /// this target.
579 /// @param ArchMatchFn - The arch match checking function for this target.
582580 /// @param HasJIT - Whether the target supports JIT code
583581 /// generation.
584582 static void RegisterTarget(Target &T,
585583 const char *Name,
586584 const char *ShortDesc,
587 Target::TripleMatchQualityFnTy TQualityFn,
585 Target::ArchMatchFnTy ArchMatchFn,
588586 bool HasJIT = false);
589587
590588 /// RegisterMCAsmInfo - Register a MCAsmInfo implementation for the
830828 bool HasJIT = false>
831829 struct RegisterTarget {
832830 RegisterTarget(Target &T, const char *Name, const char *Desc) {
833 TargetRegistry::RegisterTarget(T, Name, Desc,
834 &getTripleMatchQuality,
835 HasJIT);
836 }
837
838 static unsigned getTripleMatchQuality(const std::string &TT) {
839 if (Triple(TT).getArch() == TargetArchType)
840 return 20;
841 return 0;
831 TargetRegistry::RegisterTarget(T, Name, Desc, &getArchMatch, HasJIT);
832 }
833
834 static bool getArchMatch(Triple::ArchType Arch) {
835 return Arch == TargetArchType;
842836 }
843837 };
844838
7070 Error = "Unable to find target for this triple (no targets are registered)";
7171 return 0;
7272 }
73 const Target *Best = 0, *EquallyBest = 0;
74 unsigned BestQuality = 0;
73 const Target *Matching = 0;
74 Triple::ArchType Arch = Triple(TT).getArch();
7575 for (iterator it = begin(), ie = end(); it != ie; ++it) {
76 if (unsigned Qual = it->TripleMatchQualityFn(TT)) {
77 if (!Best || Qual > BestQuality) {
78 Best = &*it;
79 EquallyBest = 0;
80 BestQuality = Qual;
81 } else if (Qual == BestQuality)
82 EquallyBest = &*it;
76 if (it->ArchMatchFn(Arch)) {
77 if (Matching) {
78 Error = std::string("Cannot choose between targets \"") +
79 Matching->Name + "\" and \"" + it->Name + "\"";
80 return 0;
81 }
82 Matching = &*it;
8383 }
8484 }
8585
86 if (!Best) {
86 if (!Matching) {
8787 Error = "No available targets are compatible with this triple, "
8888 "see -version for the available targets.";
8989 return 0;
9090 }
9191
92 // Otherwise, take the best target, but make sure we don't have two equally
93 // good best targets.
94 if (EquallyBest) {
95 Error = std::string("Cannot choose between targets \"") +
96 Best->Name + "\" and \"" + EquallyBest->Name + "\"";
97 return 0;
98 }
99
100 return Best;
92 return Matching;
10193 }
10294
10395 void TargetRegistry::RegisterTarget(Target &T,
10496 const char *Name,
10597 const char *ShortDesc,
106 Target::TripleMatchQualityFnTy TQualityFn,
98 Target::ArchMatchFnTy ArchMatchFn,
10799 bool HasJIT) {
108 assert(Name && ShortDesc && TQualityFn &&
100 assert(Name && ShortDesc && ArchMatchFn &&
109101 "Missing required target information!");
110102
111103 // Check if this target has already been initialized, we allow this as a
119111
120112 T.Name = Name;
121113 T.ShortDesc = ShortDesc;
122 T.TripleMatchQualityFn = TQualityFn;
114 T.ArchMatchFn = ArchMatchFn;
123115 T.HasJIT = HasJIT;
124116 }
125117
1313
1414 Target llvm::TheCppBackendTarget;
1515
16 static unsigned CppBackend_TripleMatchQuality(const std::string &TT) {
17 // This class always works, but shouldn't be the default in most cases.
18 return 1;
16 static bool CppBackend_TripleMatchQuality(Triple::ArchType Arch) {
17 // This backend doesn't correspond to any architecture. It must be explicitly
18 // selected with -march.
19 return false;
1920 }
2021
2122 extern "C" void LLVMInitializeCppBackendTargetInfo() {
0 ; RUN: not llc -mtriple le32-unknown-nacl %s -o - 2>&1 | FileCheck %s
1
2 ; CHECK: error: unable to get target for 'le32-unknown-nacl'