1 //===-- Target/TargetMachineRegistry.h - Target Registration ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_TARGET_TARGETMACHINEREGISTRY_H
18 #define LLVM_TARGET_TARGETMACHINEREGISTRY_H
20 #include "llvm/Module.h"
21 #include "llvm/Support/Registry.h"
22 #include "llvm/Target/TargetRegistry.h"
29 struct TargetMachineRegistryEntry {
30 const Target &TheTarget;
32 const char *ShortDesc;
33 TargetMachine *(*CtorFn)(const Module &, const std::string &);
34 unsigned (*ModuleMatchQualityFn)(const Module &M);
35 unsigned (*JITMatchQualityFn)();
38 TargetMachineRegistryEntry(const Target &T, const char *N, const char *SD,
39 TargetMachine *(*CF)(const Module &, const std::string &),
40 unsigned (*MMF)(const Module &M),
42 : TheTarget(T), Name(N), ShortDesc(SD), CtorFn(CF),
43 ModuleMatchQualityFn(MMF), JITMatchQualityFn(JMF) {}
47 class RegistryTraits<TargetMachine> {
49 typedef TargetMachineRegistryEntry entry;
51 static const char *nameof(const entry &Entry) { return Entry.Name; }
52 static const char *descof(const entry &Entry) { return Entry.ShortDesc; }
55 struct TargetMachineRegistry : public Registry<TargetMachine> {
56 /// getClosestStaticTargetForModule - Given an LLVM module, pick the best
57 /// target that is compatible with the module. If no close target can be
58 /// found, this returns null and sets the Error string to a reason.
59 static const entry *getClosestStaticTargetForModule(const Module &M,
62 /// getClosestTargetForJIT - Pick the best target that is compatible with
63 /// the current host. If no close target can be found, this returns null
64 /// and sets the Error string to a reason.
65 static const entry *getClosestTargetForJIT(std::string &Error);
69 //===--------------------------------------------------------------------===//
70 /// RegisterTarget - This class is used to make targets automatically register
71 /// themselves with the tool they are linked. Targets should define an
72 /// instance of this and implement the static methods described in the
73 /// TargetMachine comments.
74 /// The type 'TargetMachineImpl' should provide a constructor with two
76 /// - const Module& M: the module that is being compiled:
77 /// - const std::string& FS: target-specific string describing target
80 template<class TargetMachineImpl>
81 struct RegisterTarget {
82 RegisterTarget(Target &T, const char *Name, const char *ShortDesc)
83 : Entry(T, Name, ShortDesc, &Allocator,
84 &TargetMachineImpl::getModuleMatchQuality,
85 &TargetMachineImpl::getJITMatchQuality),
87 TargetRegistry::RegisterTargetMachine(T, &Allocator);
91 TargetMachineRegistry::entry Entry;
92 TargetMachineRegistry::node Node;
94 static TargetMachine *Allocator(const Module &M, const std::string &FS) {
95 return new TargetMachineImpl(M, FS);