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