MCJIT.h revision 776054dd938472828ac3ebf75b05e21171ef4ecf
16aec29848676494867e26307698155bc2c5a4033Daniel Dunbar//===-- MCJIT.h - Class definition for the MCJIT ----------------*- C++ -*-===//
26aec29848676494867e26307698155bc2c5a4033Daniel Dunbar//
36aec29848676494867e26307698155bc2c5a4033Daniel Dunbar//                     The LLVM Compiler Infrastructure
46aec29848676494867e26307698155bc2c5a4033Daniel Dunbar//
56aec29848676494867e26307698155bc2c5a4033Daniel Dunbar// This file is distributed under the University of Illinois Open Source
66aec29848676494867e26307698155bc2c5a4033Daniel Dunbar// License. See LICENSE.TXT for details.
76aec29848676494867e26307698155bc2c5a4033Daniel Dunbar//
86aec29848676494867e26307698155bc2c5a4033Daniel Dunbar//===----------------------------------------------------------------------===//
96aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
106aec29848676494867e26307698155bc2c5a4033Daniel Dunbar#ifndef LLVM_LIB_EXECUTIONENGINE_MCJIT_H
116aec29848676494867e26307698155bc2c5a4033Daniel Dunbar#define LLVM_LIB_EXECUTIONENGINE_MCJIT_H
126aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
1331649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach#include "llvm/PassManager.h"
14776054dd938472828ac3ebf75b05e21171ef4ecfAndrew Kaylor#include "llvm/ADT/SmallVector.h"
156aec29848676494867e26307698155bc2c5a4033Daniel Dunbar#include "llvm/ExecutionEngine/ExecutionEngine.h"
16f922910494377909b4cf2a0b73f509b2b1925799Jim Grosbach#include "llvm/ExecutionEngine/RuntimeDyld.h"
176aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
186aec29848676494867e26307698155bc2c5a4033Daniel Dunbarnamespace llvm {
196aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
203f23cef24fc9200def464bd4bce820678b5715deAndrew Kaylorclass ObjectImage;
213f23cef24fc9200def464bd4bce820678b5715deAndrew Kaylor
2231649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach// FIXME: This makes all kinds of horrible assumptions for the time being,
2331649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach// like only having one module, not needing to worry about multi-threading,
2431649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach// blah blah. Purely in get-it-up-and-limping mode for now.
2531649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach
266aec29848676494867e26307698155bc2c5a4033Daniel Dunbarclass MCJIT : public ExecutionEngine {
278005bcd5e0c923881d82afcb813a7e537cd1b241Jim Grosbach  MCJIT(Module *M, TargetMachine *tm, RTDyldMemoryManager *MemMgr,
288005bcd5e0c923881d82afcb813a7e537cd1b241Jim Grosbach        bool AllocateGVsWithCode);
2931649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach
3031649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach  TargetMachine *TM;
3131649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach  MCContext *Ctx;
32fcbe5b71936b820647dffff0e4f9c60ece3988a5Jim Grosbach  RTDyldMemoryManager *MemMgr;
33ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylor  RuntimeDyld Dyld;
34776054dd938472828ac3ebf75b05e21171ef4ecfAndrew Kaylor  SmallVector<JITEventListener*, 2> EventListeners;
3531649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach
36ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylor  // FIXME: Add support for multiple modules
37ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylor  bool isCompiled;
3831649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach  Module *M;
393f23cef24fc9200def464bd4bce820678b5715deAndrew Kaylor  OwningPtr<ObjectImage> LoadedObject;
4031649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach
416aec29848676494867e26307698155bc2c5a4033Daniel Dunbarpublic:
426aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  ~MCJIT();
436aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
446aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  /// @name ExecutionEngine interface implementation
456aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  /// @{
466aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
4728989889bf3aa3314562d438aece245b71176ec4Andrew Kaylor  virtual void finalizeObject();
4828989889bf3aa3314562d438aece245b71176ec4Andrew Kaylor
496aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  virtual void *getPointerToBasicBlock(BasicBlock *BB);
506aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
516aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  virtual void *getPointerToFunction(Function *F);
526aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
536aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  virtual void *recompileAndRelinkFunction(Function *F);
546aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
556aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  virtual void freeMachineCodeForFunction(Function *F);
566aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
576aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  virtual GenericValue runFunction(Function *F,
586aec29848676494867e26307698155bc2c5a4033Daniel Dunbar                                   const std::vector<GenericValue> &ArgValues);
596aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
6034714a06096f854c76371295d8c20b017fbba50bJim Grosbach  /// getPointerToNamedFunction - This method returns the address of the
6134714a06096f854c76371295d8c20b017fbba50bJim Grosbach  /// specified function by using the dlsym function call.  As such it is only
6234714a06096f854c76371295d8c20b017fbba50bJim Grosbach  /// useful for resolving library symbols, not code generated symbols.
6334714a06096f854c76371295d8c20b017fbba50bJim Grosbach  ///
6434714a06096f854c76371295d8c20b017fbba50bJim Grosbach  /// If AbortOnFailure is false and no function with the given name is
6534714a06096f854c76371295d8c20b017fbba50bJim Grosbach  /// found, this function silently returns a null pointer. Otherwise,
6634714a06096f854c76371295d8c20b017fbba50bJim Grosbach  /// it prints a message to stderr and aborts.
6734714a06096f854c76371295d8c20b017fbba50bJim Grosbach  ///
6845a93d6eb4c55876e94f91b471657f23fe2149aaDanil Malyshev  virtual void *getPointerToNamedFunction(const std::string &Name,
6945a93d6eb4c55876e94f91b471657f23fe2149aaDanil Malyshev                                          bool AbortOnFailure = true);
7030b9e322e159df8eaabb5b194cec6e11ba99c261Danil Malyshev
71020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  /// mapSectionAddress - map a section to its target address space value.
72020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  /// Map the address of a JIT section as returned from the memory manager
73020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  /// to the address in the target process as the running code will see it.
74020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  /// This is the address which will be used for relocation resolution.
75e940c1bb6c976539f07d6f440aeaacf7c25e1ddcJim Grosbach  virtual void mapSectionAddress(const void *LocalAddress,
76e940c1bb6c976539f07d6f440aeaacf7c25e1ddcJim Grosbach                                 uint64_t TargetAddress) {
77020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach    Dyld.mapSectionAddress(LocalAddress, TargetAddress);
78020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  }
79020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach
80776054dd938472828ac3ebf75b05e21171ef4ecfAndrew Kaylor  virtual void RegisterJITEventListener(JITEventListener *L);
81776054dd938472828ac3ebf75b05e21171ef4ecfAndrew Kaylor  virtual void UnregisterJITEventListener(JITEventListener *L);
82776054dd938472828ac3ebf75b05e21171ef4ecfAndrew Kaylor
836aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  /// @}
846aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  /// @name (Private) Registration Interfaces
856aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  /// @{
866aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
876aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  static void Register() {
886aec29848676494867e26307698155bc2c5a4033Daniel Dunbar    MCJITCtor = createJIT;
896aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  }
906aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
916aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  static ExecutionEngine *createJIT(Module *M,
926aec29848676494867e26307698155bc2c5a4033Daniel Dunbar                                    std::string *ErrorStr,
936aec29848676494867e26307698155bc2c5a4033Daniel Dunbar                                    JITMemoryManager *JMM,
946aec29848676494867e26307698155bc2c5a4033Daniel Dunbar                                    bool GVsWithCode,
95c5b28580a94e247300e5d3ccf532e153f2ae6f12Dylan Noblesmith                                    TargetMachine *TM);
966aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
976aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  // @}
98ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylor
99ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylorprotected:
100ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylor  /// emitObject -- Generate a JITed object in memory from the specified module
101ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylor  /// Currently, MCJIT only supports a single module and the module passed to
102ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylor  /// this function call is expected to be the contained module.  The module
103ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylor  /// is passed as a parameter here to prepare for multiple module support in
104ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylor  /// the future.
105ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylor  void emitObject(Module *M);
106776054dd938472828ac3ebf75b05e21171ef4ecfAndrew Kaylor
107776054dd938472828ac3ebf75b05e21171ef4ecfAndrew Kaylor  void NotifyObjectEmitted(const ObjectImage& Obj);
108776054dd938472828ac3ebf75b05e21171ef4ecfAndrew Kaylor  void NotifyFreeingObject(const ObjectImage& Obj);
1096aec29848676494867e26307698155bc2c5a4033Daniel Dunbar};
1106aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
1116aec29848676494867e26307698155bc2c5a4033Daniel Dunbar} // End llvm namespace
1126aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
1136aec29848676494867e26307698155bc2c5a4033Daniel Dunbar#endif
114