Registry.h should not depend on CommandLine.h.
[oota-llvm.git] / include / llvm / Target / TargetMachineRegistry.h
1 //===-- Target/TargetMachineRegistry.h - Target Registration ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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
13 // the system.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_TARGET_TARGETMACHINEREGISTRY_H
18 #define LLVM_TARGET_TARGETMACHINEREGISTRY_H
19
20 #include "llvm/Module.h"
21 #include "llvm/Support/Registry.h"
22
23 namespace llvm {
24   class Module;
25   class TargetMachine;
26
27   struct TargetMachineRegistryEntry {
28     const char *Name;
29     const char *ShortDesc;
30     TargetMachine *(*CtorFn)(const Module &, const std::string &);
31     unsigned (*ModuleMatchQualityFn)(const Module &M);
32     unsigned (*JITMatchQualityFn)();
33
34   public:
35     TargetMachineRegistryEntry(const char *N, const char *SD,
36                       TargetMachine *(*CF)(const Module &, const std::string &),
37                                unsigned (*MMF)(const Module &M),
38                                unsigned (*JMF)())
39       : Name(N), ShortDesc(SD), CtorFn(CF), ModuleMatchQualityFn(MMF),
40         JITMatchQualityFn(JMF) {}
41   };
42
43   template<>
44   class RegistryTraits<TargetMachine> {
45   public:
46     typedef TargetMachineRegistryEntry entry;
47
48     static const char *nameof(const entry &Entry) { return Entry.Name; }
49     static const char *descof(const entry &Entry) { return Entry.ShortDesc; }
50   };
51
52   struct TargetMachineRegistry : public Registry<TargetMachine> {
53     /// getClosestStaticTargetForModule - Given an LLVM module, pick the best
54     /// target that is compatible with the module.  If no close target can be
55     /// found, this returns null and sets the Error string to a reason.
56     static const entry *getClosestStaticTargetForModule(const Module &M,
57                                                         std::string &Error);
58
59     /// getClosestTargetForJIT - Pick the best target that is compatible with
60     /// the current host.  If no close target can be found, this returns null
61     /// and sets the Error string to a reason.
62     static const entry *getClosestTargetForJIT(std::string &Error);
63
64   };
65
66   //===--------------------------------------------------------------------===//
67   /// RegisterTarget - This class is used to make targets automatically register
68   /// themselves with the tool they are linked.  Targets should define an
69   /// instance of this and implement the static methods described in the
70   /// TargetMachine comments.
71   /// The type 'TargetMachineImpl' should provide a constructor with two
72   /// parameters:
73   /// - const Module& M: the module that is being compiled:
74   /// - const std::string& FS: target-specific string describing target
75   ///   flavour.
76
77   template<class TargetMachineImpl>
78   struct RegisterTarget {
79     RegisterTarget(const char *Name, const char *ShortDesc)
80       : Entry(Name, ShortDesc, &Allocator,
81               &TargetMachineImpl::getModuleMatchQuality,
82               &TargetMachineImpl::getJITMatchQuality),
83         Node(Entry)
84     {}
85
86   private:
87     TargetMachineRegistry::entry Entry;
88     TargetMachineRegistry::node Node;
89
90     static TargetMachine *Allocator(const Module &M, const std::string &FS) {
91       return new TargetMachineImpl(M, FS);
92     }
93   };
94
95 }
96
97 #endif