First cut at TargetMachineRegistry and RegisterTarget classes
[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 namespace llvm {
21   class Module;
22   class TargetMachine;
23   class IntrinsicLowering;
24
25   struct TargetMachineRegistry {
26     /// Entry - One instance of this struct is created for each target that is
27     /// registered.
28     struct Entry {
29       const char *Name;
30       const char *ShortDesc;
31       TargetMachine *(*CtorFn)(const Module &, IntrinsicLowering*);
32       unsigned (*ModuleMatchQualityFn)(const Module &M);
33       unsigned (*JITMatchQualityFn)();
34
35       const Entry *getNext() const { return Next; }
36
37     protected:
38       Entry(const char *N, const char *SD,
39             TargetMachine *(*CF)(const Module &, IntrinsicLowering*),
40             unsigned (*MMF)(const Module &M), unsigned (*JMF)())
41       : Name(N), ShortDesc(SD), CtorFn(CF), ModuleMatchQualityFn(MMF),
42       JITMatchQualityFn(JMF), Next(List) {
43         List = this;
44       }
45     private:
46       const Entry *Next;  // Next entry in the linked list.
47     };
48
49     /// TargetMachineRegistry::getList - This static method returns the list of
50     /// target machines that are registered with the system.
51     static const Entry *getList() { return List; }
52
53   private:
54     static const Entry *List;
55   };
56
57   //===--------------------------------------------------------------------===//
58   /// RegisterTarget - This class is used to make targets automatically register
59   /// themselves with the tool they are linked.  Targets should define an
60   /// instance of this and implement the static methods described in the
61   /// TargetMachine comments..
62   template<class TargetMachineImpl>
63   struct RegisterTarget : public TargetMachineRegistry::Entry {
64     RegisterTarget(const char *Name, const char *ShortDesc) : 
65       TargetMachineRegistry::Entry(Name, ShortDesc, &Allocator,
66                                    &TargetMachineImpl::getModuleMatchQuality,
67                                    &TargetMachineImpl::getJITMatchQuality) {
68     }
69   private:
70     static TargetMachine *Allocator(const Module &M, IntrinsicLowering *IL) {
71       return new TargetMachineImpl(M, IL);
72     }
73   };
74 }
75
76 #endif