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 - Pick the best target that is compatible with
40 /// the current host. If no close target can be found, this returns null
41 /// and sets the Error string to a reason.
42 static const Entry *getClosestTargetForJIT(std::string &Error);
45 /// Entry - One instance of this struct is created for each target that is
49 const char *ShortDesc;
50 TargetMachine *(*CtorFn)(const Module &, const std::string &);
51 unsigned (*ModuleMatchQualityFn)(const Module &M);
52 unsigned (*JITMatchQualityFn)();
54 const Entry *getNext() const { return Next; }
57 Entry(const char *N, const char *SD,
58 TargetMachine *(*CF)(const Module &, const std::string &),
59 unsigned (*MMF)(const Module &M), unsigned (*JMF)());
61 const Entry *Next; // Next entry in the linked list.
65 static const Entry *List;
68 //===--------------------------------------------------------------------===//
69 /// RegisterTarget - This class is used to make targets automatically register
70 /// themselves with the tool they are linked. Targets should define an
71 /// instance of this and implement the static methods described in the
72 /// TargetMachine comments.
73 /// The type 'TargetMachineImpl' should provide a constructor with two
75 /// - const Module& M: the module that is being compiled:
76 /// - const std::string& FS: target-specific string describing target
79 template<class TargetMachineImpl>
80 struct RegisterTarget : public TargetMachineRegistry::Entry {
81 RegisterTarget(const char *Name, const char *ShortDesc) :
82 TargetMachineRegistry::Entry(Name, ShortDesc, &Allocator,
83 &TargetMachineImpl::getModuleMatchQuality,
84 &TargetMachineImpl::getJITMatchQuality) {
87 static TargetMachine *Allocator(const Module &M, const std::string &FS) {
88 return new TargetMachineImpl(M, FS);
92 /// TargetRegistrationListener - This class allows code to listen for targets
93 /// that are dynamically registered, and be notified of it when they are.
94 class TargetRegistrationListener {
95 TargetRegistrationListener **Prev, *Next;
97 TargetRegistrationListener();
98 virtual ~TargetRegistrationListener();
100 TargetRegistrationListener *getNext() const { return Next; }
102 virtual void targetRegistered(const TargetMachineRegistry::Entry *E) = 0;
106 //===--------------------------------------------------------------------===//
107 /// TargetNameParser - This option can be used to provide a command line
108 /// option to choose among the various registered targets (commonly -march).
109 class TargetNameParser : public TargetRegistrationListener,
110 public cl::parser<const TargetMachineRegistry::Entry*> {
112 void initialize(cl::Option &O) {
113 for (const TargetMachineRegistry::Entry *E =
114 TargetMachineRegistry::getList(); E; E = E->getNext())
115 Values.push_back(std::make_pair(E->Name,
116 std::make_pair(E, E->ShortDesc)));
117 cl::parser<const TargetMachineRegistry::Entry*>::initialize(O);
120 virtual void targetRegistered(const TargetMachineRegistry::Entry *E) {
121 Values.push_back(std::make_pair(E->Name,
122 std::make_pair(E, E->ShortDesc)));