2009-07-15 12:24:58 +08:00
|
|
|
//===--- TargetRegistry.cpp - Target registration -------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-08-25 02:08:43 +08:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2011-07-22 15:50:44 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-07-15 12:24:58 +08:00
|
|
|
#include <cassert>
|
2011-07-22 15:50:44 +08:00
|
|
|
#include <vector>
|
2009-07-15 12:24:58 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2009-07-15 15:09:29 +08:00
|
|
|
// Clients are responsible for avoid race conditions in registration.
|
2014-04-07 12:17:22 +08:00
|
|
|
static Target *FirstTarget = nullptr;
|
2009-07-15 12:24:58 +08:00
|
|
|
|
2015-05-12 06:20:48 +08:00
|
|
|
iterator_range<TargetRegistry::iterator> TargetRegistry::targets() {
|
|
|
|
return make_range(iterator(FirstTarget), iterator());
|
2009-07-16 10:06:09 +08:00
|
|
|
}
|
|
|
|
|
2012-05-09 07:38:45 +08:00
|
|
|
const Target *TargetRegistry::lookupTarget(const std::string &ArchName,
|
|
|
|
Triple &TheTriple,
|
|
|
|
std::string &Error) {
|
|
|
|
// Allocate target machine. First, check whether the user has explicitly
|
|
|
|
// specified an architecture to compile for. If so we have to look it up by
|
|
|
|
// name, because it might be a backend that has no mapping to a target triple.
|
2014-04-07 12:17:22 +08:00
|
|
|
const Target *TheTarget = nullptr;
|
2012-05-09 07:38:45 +08:00
|
|
|
if (!ArchName.empty()) {
|
2015-05-12 06:20:48 +08:00
|
|
|
auto I =
|
|
|
|
std::find_if(targets().begin(), targets().end(),
|
|
|
|
[&](const Target &T) { return ArchName == T.getName(); });
|
2012-05-09 07:38:45 +08:00
|
|
|
|
2015-05-12 06:20:48 +08:00
|
|
|
if (I == targets().end()) {
|
2012-05-09 07:38:45 +08:00
|
|
|
Error = "error: invalid target '" + ArchName + "'.\n";
|
2014-04-07 12:17:22 +08:00
|
|
|
return nullptr;
|
2012-05-09 07:38:45 +08:00
|
|
|
}
|
|
|
|
|
2015-05-12 06:20:48 +08:00
|
|
|
TheTarget = &*I;
|
|
|
|
|
2012-05-09 07:38:45 +08:00
|
|
|
// Adjust the triple to match (if known), otherwise stick with the
|
|
|
|
// given triple.
|
|
|
|
Triple::ArchType Type = Triple::getArchTypeForLLVMName(ArchName);
|
|
|
|
if (Type != Triple::UnknownArch)
|
|
|
|
TheTriple.setArch(Type);
|
|
|
|
} else {
|
|
|
|
// Get the target specific parser.
|
|
|
|
std::string TempError;
|
|
|
|
TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), TempError);
|
2014-04-09 12:20:00 +08:00
|
|
|
if (!TheTarget) {
|
2012-05-09 07:38:45 +08:00
|
|
|
Error = ": error: unable to get target for '"
|
|
|
|
+ TheTriple.getTriple()
|
|
|
|
+ "', see --version and --triple.\n";
|
2014-04-07 12:17:22 +08:00
|
|
|
return nullptr;
|
2012-05-09 07:38:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TheTarget;
|
|
|
|
}
|
|
|
|
|
2009-07-26 10:12:58 +08:00
|
|
|
const Target *TargetRegistry::lookupTarget(const std::string &TT,
|
|
|
|
std::string &Error) {
|
2009-07-17 23:50:49 +08:00
|
|
|
// Provide special warning when no targets are initialized.
|
2015-05-12 06:20:48 +08:00
|
|
|
if (targets().begin() == targets().end()) {
|
2009-07-17 23:50:49 +08:00
|
|
|
Error = "Unable to find target for this triple (no targets are registered)";
|
2014-04-07 12:17:22 +08:00
|
|
|
return nullptr;
|
2009-07-17 23:50:49 +08:00
|
|
|
}
|
2015-05-12 06:20:48 +08:00
|
|
|
Triple::ArchType Arch = Triple(TT).getArch();
|
|
|
|
auto ArchMatch = [&](const Target &T) { return T.ArchMatchFn(Arch); };
|
|
|
|
auto I = std::find_if(targets().begin(), targets().end(), ArchMatch);
|
2009-07-15 12:24:58 +08:00
|
|
|
|
2015-05-12 06:20:48 +08:00
|
|
|
if (I == targets().end()) {
|
2016-01-29 06:55:45 +08:00
|
|
|
Error = "No available targets are compatible with this triple.";
|
2014-04-07 12:17:22 +08:00
|
|
|
return nullptr;
|
2009-07-15 12:24:58 +08:00
|
|
|
}
|
|
|
|
|
2015-05-12 06:20:48 +08:00
|
|
|
auto J = std::find_if(std::next(I), targets().end(), ArchMatch);
|
|
|
|
if (J != targets().end()) {
|
|
|
|
Error = std::string("Cannot choose between targets \"") + I->Name +
|
|
|
|
"\" and \"" + J->Name + "\"";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &*I;
|
2009-07-15 12:24:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TargetRegistry::RegisterTarget(Target &T,
|
|
|
|
const char *Name,
|
|
|
|
const char *ShortDesc,
|
2013-12-14 00:05:32 +08:00
|
|
|
Target::ArchMatchFnTy ArchMatchFn,
|
2009-07-25 18:09:50 +08:00
|
|
|
bool HasJIT) {
|
2013-12-14 00:05:32 +08:00
|
|
|
assert(Name && ShortDesc && ArchMatchFn &&
|
2009-07-15 12:24:58 +08:00
|
|
|
"Missing required target information!");
|
2009-07-16 04:24:03 +08:00
|
|
|
|
|
|
|
// Check if this target has already been initialized, we allow this as a
|
|
|
|
// convenience to some clients.
|
|
|
|
if (T.Name)
|
|
|
|
return;
|
2009-07-15 12:24:58 +08:00
|
|
|
|
|
|
|
// Add to the list of targets.
|
|
|
|
T.Next = FirstTarget;
|
2009-07-15 15:37:49 +08:00
|
|
|
FirstTarget = &T;
|
2009-07-15 12:24:58 +08:00
|
|
|
|
|
|
|
T.Name = Name;
|
|
|
|
T.ShortDesc = ShortDesc;
|
2013-12-14 00:05:32 +08:00
|
|
|
T.ArchMatchFn = ArchMatchFn;
|
2009-07-25 18:09:50 +08:00
|
|
|
T.HasJIT = HasJIT;
|
2009-07-15 12:24:58 +08:00
|
|
|
}
|
|
|
|
|
2014-03-08 05:52:38 +08:00
|
|
|
static int TargetArraySortFn(const std::pair<StringRef, const Target *> *LHS,
|
|
|
|
const std::pair<StringRef, const Target *> *RHS) {
|
|
|
|
return LHS->first.compare(RHS->first);
|
|
|
|
}
|
|
|
|
|
2011-07-22 15:50:44 +08:00
|
|
|
void TargetRegistry::printRegisteredTargetsForVersion() {
|
|
|
|
std::vector<std::pair<StringRef, const Target*> > Targets;
|
|
|
|
size_t Width = 0;
|
2015-05-12 06:20:48 +08:00
|
|
|
for (const auto &T : TargetRegistry::targets()) {
|
|
|
|
Targets.push_back(std::make_pair(T.getName(), &T));
|
2011-07-22 15:50:44 +08:00
|
|
|
Width = std::max(Width, Targets.back().first.size());
|
|
|
|
}
|
2014-03-08 05:52:38 +08:00
|
|
|
array_pod_sort(Targets.begin(), Targets.end(), TargetArraySortFn);
|
2011-07-22 15:50:44 +08:00
|
|
|
|
|
|
|
raw_ostream &OS = outs();
|
|
|
|
OS << " Registered Targets:\n";
|
|
|
|
for (unsigned i = 0, e = Targets.size(); i != e; ++i) {
|
|
|
|
OS << " " << Targets[i].first;
|
|
|
|
OS.indent(Width - Targets[i].first.size()) << " - "
|
|
|
|
<< Targets[i].second->getShortDescription() << '\n';
|
|
|
|
}
|
|
|
|
if (Targets.empty())
|
|
|
|
OS << " (none)\n";
|
|
|
|
}
|