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"
25 class IntrinsicLowering;
27 struct TargetMachineRegistry {
30 /// TargetMachineRegistry::getList - This static method returns the list of
31 /// target machines that are registered with the system.
32 static const Entry *getList() { return List; }
34 /// getClosestStaticTargetForModule - Given an LLVM module, pick the best
35 /// target that is compatible with the module. If no close target can be
36 /// found, this returns null and sets the Error string to a reason.
37 static const Entry *getClosestStaticTargetForModule(const Module &M,
40 /// getClosestTargetForJIT - Given an LLVM module, pick the best target that
41 /// is compatible with the current host and the specified module. If no
42 /// close target can be found, this returns null and sets the Error string
44 static const Entry *getClosestTargetForJIT(std::string &Error);
47 /// Entry - One instance of this struct is created for each target that is
51 const char *ShortDesc;
52 TargetMachine *(*CtorFn)(const Module &, IntrinsicLowering*);
53 unsigned (*ModuleMatchQualityFn)(const Module &M);
54 unsigned (*JITMatchQualityFn)();
56 const Entry *getNext() const { return Next; }
59 Entry(const char *N, const char *SD,
60 TargetMachine *(*CF)(const Module &, IntrinsicLowering*),
61 unsigned (*MMF)(const Module &M), unsigned (*JMF)());
63 const Entry *Next; // Next entry in the linked list.
67 static const Entry *List;
70 //===--------------------------------------------------------------------===//
71 /// RegisterTarget - This class is used to make targets automatically register
72 /// themselves with the tool they are linked. Targets should define an
73 /// instance of this and implement the static methods described in the
74 /// TargetMachine comments..
75 template<class TargetMachineImpl>
76 struct RegisterTarget : public TargetMachineRegistry::Entry {
77 RegisterTarget(const char *Name, const char *ShortDesc) :
78 TargetMachineRegistry::Entry(Name, ShortDesc, &Allocator,
79 &TargetMachineImpl::getModuleMatchQuality,
80 &TargetMachineImpl::getJITMatchQuality) {
83 static TargetMachine *Allocator(const Module &M, IntrinsicLowering *IL) {
84 return new TargetMachineImpl(M, IL);
88 /// TargetRegistrationListener - This class allows code to listen for targets
89 /// that are dynamically registered, and be notified of it when they are.
90 class TargetRegistrationListener {
91 TargetRegistrationListener **Prev, *Next;
93 TargetRegistrationListener();
94 virtual ~TargetRegistrationListener();
96 TargetRegistrationListener *getNext() const { return Next; }
98 virtual void targetRegistered(const TargetMachineRegistry::Entry *E) = 0;
102 //===--------------------------------------------------------------------===//
103 /// TargetNameParser - This option can be used to provide a command line
104 /// option to choose among the various registered targets (commonly -march).
105 class TargetNameParser : public TargetRegistrationListener,
106 public cl::parser<const TargetMachineRegistry::Entry*> {
108 void initialize(cl::Option &O) {
109 for (const TargetMachineRegistry::Entry *E =
110 TargetMachineRegistry::getList(); E; E = E->getNext())
111 Values.push_back(std::make_pair(E->Name,
112 std::make_pair(E, E->ShortDesc)));
113 cl::parser<const TargetMachineRegistry::Entry*>::initialize(O);
116 virtual void targetRegistered(const TargetMachineRegistry::Entry *E) {
117 Values.push_back(std::make_pair(E->Name,
118 std::make_pair(E, E->ShortDesc)));