Provide TargetMachine implementations with reference to Target they were created
[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 Target;
27   class TargetMachine;
28
29   struct TargetMachineRegistryEntry {
30     const Target &TheTarget;
31     const char *Name;
32     const char *ShortDesc;
33
34   public:
35     TargetMachineRegistryEntry(const Target &T, const char *N, const char *SD)
36       : TheTarget(T), Name(N), ShortDesc(SD) {}
37   };
38
39   template<>
40   class RegistryTraits<TargetMachine> {
41   public:
42     typedef TargetMachineRegistryEntry entry;
43
44     static const char *nameof(const entry &Entry) { return Entry.Name; }
45     static const char *descof(const entry &Entry) { return Entry.ShortDesc; }
46   };
47
48   struct TargetMachineRegistry : public Registry<TargetMachine> {
49
50   };
51
52   //===--------------------------------------------------------------------===//
53   /// RegisterTarget - This class is used to make targets automatically register
54   /// themselves with the tools they are linked with.  Targets should define an
55   /// single global Target instance and register it using the TargetRegistry
56   /// interfaces. Targets must also include a static instance of this class.
57   ///
58   /// The type 'TargetMachineImpl' should provide a constructor with two
59   /// parameters:
60   /// - const Module& M: the module that is being compiled:
61   /// - const std::string& FS: target-specific string describing target
62   ///   flavour.
63
64   template<class TargetMachineImpl>
65   struct RegisterTarget {
66     RegisterTarget(Target &T, const char *Name, const char *ShortDesc)
67       : Entry(T, Name, ShortDesc),
68         Node(Entry) {
69       TargetRegistry::RegisterTargetMachine(T, &Allocator);
70     }
71
72   private:
73     TargetMachineRegistry::entry Entry;
74     TargetMachineRegistry::node Node;
75
76     static TargetMachine *Allocator(const Target &T, const Module &M, 
77                                     const std::string &FS) {
78       return new TargetMachineImpl(T, M, FS);
79     }
80   };
81
82 }
83
84 #endif