15460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//===- TargetRegistry.h ---------------------------------------------------===//
25460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//
35460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//                     The MCLinker Project
45460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//
55460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// This file is distributed under the University of Illinois Open Source
65460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// License. See LICENSE.TXT for details.
75460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//
85460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//===----------------------------------------------------------------------===//
9affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#ifndef MCLD_TARGET_REGISTRY_H
10affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#define MCLD_TARGET_REGISTRY_H
115460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <llvm/Support/TargetRegistry.h>
125460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <string>
135460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <list>
145460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
155460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaonamespace llvm {
165460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoclass TargetMachine;
175460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoclass MCCodeEmitter;
185460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoclass MCContext;
195460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoclass AsmPrinter;
205460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao} // namespace of llvm
215460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
225460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaonamespace mcld {
235460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoclass LLVMTargetMachine;
245460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoclass TargetRegistry;
255460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoclass SectLinker;
265460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoclass SectLinkerOption;
275460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoclass TargetLDBackend;
285460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoclass AttributeFactory;
295460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoclass InputFactory;
305460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoclass ContextFactory;
31affc150dc44fab1911775a49636d0ce85333b634Zonr Changclass DiagnosticLineInfo;
325460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
335460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//===----------------------------------------------------------------------===//
345460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/// Target - mcld::Target is an object adapter of llvm::Target
355460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao///
365460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoclass Target
375460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{
385460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  friend class mcld::LLVMTargetMachine;
395460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  friend class mcld::TargetRegistry;
405460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaopublic:
415460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  typedef mcld::LLVMTargetMachine *(*TargetMachineCtorTy)(const mcld::Target &,
425460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao                                                          llvm::TargetMachine &,
435460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao                                                          const std::string&);
445460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
455460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  typedef SectLinker *(*SectLinkerCtorTy)(const std::string& pTriple,
465460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao                                          SectLinkerOption &,
475460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao                                          TargetLDBackend&);
485460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
495460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  typedef TargetLDBackend  *(*TargetLDBackendCtorTy)(const llvm::Target&,
505460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao                                                     const std::string&);
515460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
5267e37f1be98c926645219cfb47fab9e90d8c725cShih-wei Liao  typedef DiagnosticLineInfo *(*DiagnosticLineInfoCtorTy)(const mcld::Target&,
53affc150dc44fab1911775a49636d0ce85333b634Zonr Chang                                                          const std::string&);
545460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
555460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaopublic:
565460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  Target();
575460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
58affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  void setTarget(const llvm::Target& pTarget)
59affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  { m_pT = &pTarget; }
605460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
615460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  mcld::LLVMTargetMachine *createTargetMachine(const std::string &pTriple,
625460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao                          const std::string &pCPU, const std::string &pFeatures,
635460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao                          const llvm::TargetOptions &Options,
645460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao                          llvm::Reloc::Model RM = llvm::Reloc::Default,
655460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao                          llvm::CodeModel::Model CM = llvm::CodeModel::Default,
66affc150dc44fab1911775a49636d0ce85333b634Zonr Chang                          llvm::CodeGenOpt::Level OL = llvm::CodeGenOpt::Default) const
67affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  {
685460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    if (TargetMachineCtorFn && m_pT) {
695460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      llvm::TargetMachine *tm = m_pT->createTargetMachine(pTriple, pCPU, pFeatures, Options, RM, CM, OL);
705460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      if (tm)
715460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao        return TargetMachineCtorFn(*this, *tm, pTriple);
725460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    }
73affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    return NULL;
745460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
755460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
765460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// createSectLinker - create target-specific SectLinker
775460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ///
785460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// @return created SectLinker
795460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  SectLinker *createSectLinker(const std::string &pTriple,
805460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao                               SectLinkerOption &pOption,
815460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao                               TargetLDBackend &pLDBackend) const {
825460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    if (!SectLinkerCtorFn)
83affc150dc44fab1911775a49636d0ce85333b634Zonr Chang      return NULL;
845460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    return SectLinkerCtorFn(pTriple,
855460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao                            pOption,
865460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao                            pLDBackend);
875460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
885460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
895460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// createLDBackend - create target-specific LDBackend
905460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ///
915460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// @return created TargetLDBackend
92affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  TargetLDBackend* createLDBackend(const std::string& Triple) const
93affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  {
945460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    if (!TargetLDBackendCtorFn)
95affc150dc44fab1911775a49636d0ce85333b634Zonr Chang      return NULL;
96affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    return TargetLDBackendCtorFn(*get(), Triple);
975460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
985460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
99affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  /// createDiagnosticLineInfo - create target-specific DiagnosticLineInfo
10067e37f1be98c926645219cfb47fab9e90d8c725cShih-wei Liao  DiagnosticLineInfo* createDiagnosticLineInfo(const mcld::Target& pTarget,
101affc150dc44fab1911775a49636d0ce85333b634Zonr Chang                                               const std::string& pTriple) const
102affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  {
103affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    if (!DiagnosticLineInfoCtorFn)
104affc150dc44fab1911775a49636d0ce85333b634Zonr Chang      return NULL;
105affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    return DiagnosticLineInfoCtorFn(pTarget, pTriple);
1065460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
1075460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
108affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  const llvm::Target* get() const
109affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  { return m_pT; }
110affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
1115460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoprivate:
112affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  // -----  function pointers  ----- //
113affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  TargetMachineCtorTy TargetMachineCtorFn;
114affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  SectLinkerCtorTy SectLinkerCtorFn;
115affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  TargetLDBackendCtorTy TargetLDBackendCtorFn;
116affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  DiagnosticLineInfoCtorTy DiagnosticLineInfoCtorFn;
117affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
118affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  // -----  adapted llvm::Target  ----- //
1195460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  const llvm::Target* m_pT;
1205460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao};
1215460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1225460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//===----------------------------------------------------------------------===//
1235460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/// TargetRegistry - mcld::TargetRegistry is an object adapter of
1245460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/// llvm::TargetRegistry
1255460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao///
1265460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoclass TargetRegistry
1275460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{
1285460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaopublic:
1295460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  typedef std::list<mcld::Target*> TargetListTy;
1305460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  typedef TargetListTy::iterator iterator;
1315460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1325460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoprivate:
1335460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  static TargetListTy s_TargetList;
1345460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1355460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaopublic:
1365460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  static iterator begin() { return s_TargetList.begin(); }
1375460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  static iterator end() { return s_TargetList.end(); }
1385460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1395460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  static size_t size() { return s_TargetList.size(); }
1405460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  static bool empty() { return s_TargetList.empty(); }
1415460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1425460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// RegisterTarget - Register the given target. Attempts to register a
1435460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// target which has already been registered will be ignored.
1445460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ///
1455460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// Clients are responsible for ensuring that registration doesn't occur
1465460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// while another thread is attempting to access the registry. Typically
1475460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// this is done by initializing all targets at program startup.
1485460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ///
1495460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// @param T - The target being registered.
1505460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  static void RegisterTarget(mcld::Target &T);
1515460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1525460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// RegisterTargetMachine - Register a TargetMachine implementation for the
1535460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// given target.
1545460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ///
1555460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// @param T - The target being registered.
1565460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// @param Fn - A function to construct a TargetMachine for the target.
157affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  static void RegisterTargetMachine(mcld::Target &T, mcld::Target::TargetMachineCtorTy Fn)
158affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  {
1595460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    // Ignore duplicate registration.
1605460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    if (!T.TargetMachineCtorFn)
1615460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      T.TargetMachineCtorFn = Fn;
1625460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
1635460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1645460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// RegisterSectLinker - Register a SectLinker implementation for the given
1655460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// target.
1665460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ///
1675460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// @param T - the target being registered
1685460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// @param Fn - A function to create SectLinker for the target
169affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  static void RegisterSectLinker(mcld::Target &T, mcld::Target::SectLinkerCtorTy Fn)
170affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  {
1715460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    if (!T.SectLinkerCtorFn)
1725460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      T.SectLinkerCtorFn = Fn;
1735460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
1745460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1755460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// RegisterTargetLDBackend - Register a TargetLDBackend implementation for
1765460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// the given target.
1775460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ///
1785460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// @param T - The target being registered
1795460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// @param Fn - A function to create TargetLDBackend for the target
180affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  static void RegisterTargetLDBackend(mcld::Target &T, mcld::Target::TargetLDBackendCtorTy Fn)
181affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  {
1825460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    if (!T.TargetLDBackendCtorFn)
1835460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      T.TargetLDBackendCtorFn = Fn;
1845460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
1855460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
186affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  /// RegisterTargetDiagnosticLineInfo - Register a DiagnosticLineInfo
187affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  /// implementation for the given target.
188affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  ///
189affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  /// @param T - The target being registered
190affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  /// @param Fn - A function to create DiagnosticLineInfo for the target
191affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  static void
192affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  RegisterDiagnosticLineInfo(mcld::Target &T,
193affc150dc44fab1911775a49636d0ce85333b634Zonr Chang                             mcld::Target::DiagnosticLineInfoCtorTy Fn)
194affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  {
195affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    if (!T.DiagnosticLineInfoCtorFn)
196affc150dc44fab1911775a49636d0ce85333b634Zonr Chang      T.DiagnosticLineInfoCtorFn = Fn;
197affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  }
198affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
1995460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// lookupTarget - Lookup a target based on a llvm::Target.
2005460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ///
2015460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// @param T - The llvm::Target to find
2025460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  static const mcld::Target *lookupTarget(const llvm::Target& T);
2035460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2045460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// lookupTarget - function wrapper of llvm::TargetRegistry::lookupTarget
2055460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ///
2065460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// @param Triple - The Triple string
2075460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// @param Error  - The returned error message
2085460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  static const mcld::Target *lookupTarget(const std::string &Triple,
2095460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao                                          std::string &Error);
2105460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao};
2115460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2125460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/// RegisterTarget - Helper function for registering a target, for use in the
2135460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/// target's initialization function. Usage:
2145460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao///
2155460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/// Target TheFooTarget; // The global target instance.
2165460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao///
2175460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/// extern "C" void LLVMInitializeFooTargetInfo() {
2185460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao///   RegisterTarget X(TheFooTarget, "foo", "Foo description");
2195460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/// }
2205460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaostruct RegisterTarget
2215460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{
2225460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  RegisterTarget(mcld::Target &T, const char *Name) {
2235460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    llvm::TargetRegistry::iterator TIter, TEnd = llvm::TargetRegistry::end();
2245460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    // lookup llvm::Target
2255460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    for( TIter=llvm::TargetRegistry::begin(); TIter!=TEnd; ++TIter ) {
2265460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      if( 0==strcmp(TIter->getName(), Name) )
2275460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao        break;
2285460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    }
2295460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    T.setTarget(*TIter);
2305460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2315460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    TargetRegistry::RegisterTarget(T);
2325460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
2335460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao};
2345460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2355460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/// RegisterTargetMachine - Helper template for registering a target machine
2365460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/// implementation, for use in the target machine initialization
2375460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/// function. Usage:
2385460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao///
2395460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/// extern "C" void LLVMInitializeFooTarget() {
2405460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao///   extern mcld::Target TheFooTarget;
2415460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao///   RegisterTargetMachine<mcld::FooTargetMachine> X(TheFooTarget);
2425460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/// }
2435460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaotemplate<class TargetMachineImpl>
2445460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaostruct RegisterTargetMachine
2455460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{
2465460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  RegisterTargetMachine(mcld::Target &T) {
2475460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    TargetRegistry::RegisterTargetMachine(T, &Allocator);
2485460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
2495460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2505460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoprivate:
2515460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  static mcld::LLVMTargetMachine *Allocator(const mcld::Target &T,
2525460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao                                            llvm::TargetMachine& TM,
2535460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao                                            const std::string &Triple) {
2545460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    return new TargetMachineImpl(TM, T, Triple);
2555460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
2565460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao};
2575460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2585460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao} //end namespace mcld
2595460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2605460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#endif
2615460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
262