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