LLVM API Documentation

TargetMachineRegistry.h

Go to the documentation of this file.
00001 //===-- Target/TargetMachineRegistry.h - Target Registration ----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file exposes two classes: the TargetMachineRegistry class, which allows
00011 // tools to inspect all of registered targets, and the RegisterTarget class,
00012 // which TargetMachine implementations should use to register themselves with
00013 // the system.
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #ifndef LLVM_TARGET_TARGETMACHINEREGISTRY_H
00018 #define LLVM_TARGET_TARGETMACHINEREGISTRY_H
00019 
00020 #include "llvm/Module.h"
00021 #include "llvm/Support/Registry.h"
00022 
00023 namespace llvm {
00024   class Module;
00025   class TargetMachine;
00026 
00027   struct TargetMachineRegistryEntry {
00028     const char *Name;
00029     const char *ShortDesc;
00030     TargetMachine *(*CtorFn)(const Module &, const std::string &);
00031     unsigned (*ModuleMatchQualityFn)(const Module &M);
00032     unsigned (*JITMatchQualityFn)();
00033 
00034   public:
00035     TargetMachineRegistryEntry(const char *N, const char *SD,
00036                       TargetMachine *(*CF)(const Module &, const std::string &),
00037                                unsigned (*MMF)(const Module &M),
00038                                unsigned (*JMF)())
00039       : Name(N), ShortDesc(SD), CtorFn(CF), ModuleMatchQualityFn(MMF),
00040         JITMatchQualityFn(JMF) {}
00041   };
00042 
00043   template<>
00044   class RegistryTraits<TargetMachine> {
00045   public:
00046     typedef TargetMachineRegistryEntry entry;
00047 
00048     static const char *nameof(const entry &Entry) { return Entry.Name; }
00049     static const char *descof(const entry &Entry) { return Entry.ShortDesc; }
00050   };
00051 
00052   struct TargetMachineRegistry : public Registry<TargetMachine> {
00053     /// getClosestStaticTargetForModule - Given an LLVM module, pick the best
00054     /// target that is compatible with the module.  If no close target can be
00055     /// found, this returns null and sets the Error string to a reason.
00056     static const entry *getClosestStaticTargetForModule(const Module &M,
00057                                                         std::string &Error);
00058 
00059     /// getClosestTargetForJIT - Pick the best target that is compatible with
00060     /// the current host.  If no close target can be found, this returns null
00061     /// and sets the Error string to a reason.
00062     static const entry *getClosestTargetForJIT(std::string &Error);
00063 
00064   };
00065 
00066   //===--------------------------------------------------------------------===//
00067   /// RegisterTarget - This class is used to make targets automatically register
00068   /// themselves with the tool they are linked.  Targets should define an
00069   /// instance of this and implement the static methods described in the
00070   /// TargetMachine comments.
00071   /// The type 'TargetMachineImpl' should provide a constructor with two
00072   /// parameters:
00073   /// - const Module& M: the module that is being compiled:
00074   /// - const std::string& FS: target-specific string describing target
00075   ///   flavour.
00076 
00077   template<class TargetMachineImpl>
00078   struct RegisterTarget {
00079     RegisterTarget(const char *Name, const char *ShortDesc)
00080       : Entry(Name, ShortDesc, &Allocator,
00081               &TargetMachineImpl::getModuleMatchQuality,
00082               &TargetMachineImpl::getJITMatchQuality),
00083         Node(Entry)
00084     {}
00085 
00086   private:
00087     TargetMachineRegistry::entry Entry;
00088     TargetMachineRegistry::node Node;
00089 
00090     static TargetMachine *Allocator(const Module &M, const std::string &FS) {
00091       return new TargetMachineImpl(M, FS);
00092     }
00093   };
00094 
00095 }
00096 
00097 #endif