1 //===-- Target/TargetMachineRegistry.h - Target Registration ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file exposes two classes: the TargetMachineRegistry class, which allows
11 // tools to inspect all of registered targets, and the RegisterTarget class,
12 // which TargetMachine implementations should use to register themselves with
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_TARGET_TARGETMACHINEREGISTRY_H
18 #define LLVM_TARGET_TARGETMACHINEREGISTRY_H
20 #include "llvm/Support/CommandLine.h"
26 struct TargetMachineRegistry {
29 /// TargetMachineRegistry::getList - This static method returns the list of
30 /// target machines that are registered with the system.
31 static const Entry *getList() { return List; }
33 /// getClosestStaticTargetForModule - Given an LLVM module, pick the best
34 /// target that is compatible with the module. If no close target can be
35 /// found, this returns null and sets the Error string to a reason.
36 static const Entry *getClosestStaticTargetForModule(const Module &M,
39 /// getClosestTargetForJIT - Given an LLVM module, pick the best target that
40 /// is compatible with the current host and the specified module. If no
41 /// close target can be found, this returns null and sets the Error string
43 static const Entry *getClosestTargetForJIT(std::string &Error);
46 /// Entry - One instance of this struct is created for each target that is
50 const char *ShortDesc;
51 TargetMachine *(*CtorFn)(const Module &, const std::string &);
52 unsigned (*ModuleMatchQualityFn)(const Module &M);
53 unsigned (*JITMatchQualityFn)();
55 const Entry *getNext() const { return Next; }
58 Entry(const char *N, const char *SD,
59 TargetMachine *(*CF)(const Module &, const std::string &),
60 unsigned (*MMF)(const Module &M), unsigned (*JMF)());
62 const Entry *Next; // Next entry in the linked list.
66 static const Entry *List;
69 //===--------------------------------------------------------------------===//
70 /// RegisterTarget - This class is used to make targets automatically register
71 /// themselves with the tool they are linked. Targets should define an
72 /// instance of this and implement the static methods described in the
73 /// TargetMachine comments..
74 template<class TargetMachineImpl>
75 struct RegisterTarget : public TargetMachineRegistry::Entry {
76 RegisterTarget(const char *Name, const char *ShortDesc) :
77 TargetMachineRegistry::Entry(Name, ShortDesc, &Allocator,
78 &TargetMachineImpl::getModuleMatchQuality,
79 &TargetMachineImpl::getJITMatchQuality) {
82 static TargetMachine *Allocator(const Module &M, const std::string &FS) {
83 return new TargetMachineImpl(M, FS);
87 /// TargetRegistrationListener - This class allows code to listen for targets
88 /// that are dynamically registered, and be notified of it when they are.
89 class TargetRegistrationListener {
90 TargetRegistrationListener **Prev, *Next;
92 TargetRegistrationListener();
93 virtual ~TargetRegistrationListener();
95 TargetRegistrationListener *getNext() const { return Next; }
97 virtual void targetRegistered(const TargetMachineRegistry::Entry *E) = 0;
101 //===--------------------------------------------------------------------===//
102 /// TargetNameParser - This option can be used to provide a command line
103 /// option to choose among the various registered targets (commonly -march).
104 class TargetNameParser : public TargetRegistrationListener,
105 public cl::parser<const TargetMachineRegistry::Entry*> {
107 void initialize(cl::Option &O) {
108 for (const TargetMachineRegistry::Entry *E =
109 TargetMachineRegistry::getList(); E; E = E->getNext())
110 Values.push_back(std::make_pair(E->Name,
111 std::make_pair(E, E->ShortDesc)));
112 cl::parser<const TargetMachineRegistry::Entry*>::initialize(O);
115 virtual void targetRegistered(const TargetMachineRegistry::Entry *E) {
116 Values.push_back(std::make_pair(E->Name,
117 std::make_pair(E, E->ShortDesc)));