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
13776054dd938472828ac3ebf75b05e21171ef4ecfAndrew Kaylor#include "llvm/ADT/SmallVector.h"
146aec29848676494867e26307698155bc2c5a4033Daniel Dunbar#include "llvm/ExecutionEngine/ExecutionEngine.h"
151c489455ea5fac43a5f20911dfb5486630eb0160Andrew Kaylor#include "llvm/ExecutionEngine/ObjectCache.h"
16f922910494377909b4cf2a0b73f509b2b1925799Jim Grosbach#include "llvm/ExecutionEngine/RuntimeDyld.h"
17a1514e24cc24b050f53a12650e047799358833a1Chandler Carruth#include "llvm/PassManager.h"
186aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
196aec29848676494867e26307698155bc2c5a4033Daniel Dunbarnamespace llvm {
206aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
213f23cef24fc9200def464bd4bce820678b5715deAndrew Kaylorclass ObjectImage;
223f23cef24fc9200def464bd4bce820678b5715deAndrew Kaylor
2331649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach// FIXME: This makes all kinds of horrible assumptions for the time being,
2431649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach// like only having one module, not needing to worry about multi-threading,
2531649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach// blah blah. Purely in get-it-up-and-limping mode for now.
2631649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach
276aec29848676494867e26307698155bc2c5a4033Daniel Dunbarclass MCJIT : public ExecutionEngine {
288005bcd5e0c923881d82afcb813a7e537cd1b241Jim Grosbach  MCJIT(Module *M, TargetMachine *tm, RTDyldMemoryManager *MemMgr,
298005bcd5e0c923881d82afcb813a7e537cd1b241Jim Grosbach        bool AllocateGVsWithCode);
3031649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach
3131649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach  TargetMachine *TM;
3231649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach  MCContext *Ctx;
33fcbe5b71936b820647dffff0e4f9c60ece3988a5Jim Grosbach  RTDyldMemoryManager *MemMgr;
34ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylor  RuntimeDyld Dyld;
35776054dd938472828ac3ebf75b05e21171ef4ecfAndrew Kaylor  SmallVector<JITEventListener*, 2> EventListeners;
3631649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach
37ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylor  // FIXME: Add support for multiple modules
381c489455ea5fac43a5f20911dfb5486630eb0160Andrew Kaylor  bool IsLoaded;
3931649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach  Module *M;
403f23cef24fc9200def464bd4bce820678b5715deAndrew Kaylor  OwningPtr<ObjectImage> LoadedObject;
4131649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach
421c489455ea5fac43a5f20911dfb5486630eb0160Andrew Kaylor  // An optional ObjectCache to be notified of compiled objects and used to
431c489455ea5fac43a5f20911dfb5486630eb0160Andrew Kaylor  // perform lookup of pre-compiled code to avoid re-compilation.
441c489455ea5fac43a5f20911dfb5486630eb0160Andrew Kaylor  ObjectCache *ObjCache;
451c489455ea5fac43a5f20911dfb5486630eb0160Andrew Kaylor
466aec29848676494867e26307698155bc2c5a4033Daniel Dunbarpublic:
476aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  ~MCJIT();
486aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
496aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  /// @name ExecutionEngine interface implementation
506aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  /// @{
516aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
521c489455ea5fac43a5f20911dfb5486630eb0160Andrew Kaylor  /// Sets the object manager that MCJIT should use to avoid compilation.
531c489455ea5fac43a5f20911dfb5486630eb0160Andrew Kaylor  virtual void setObjectCache(ObjectCache *manager);
541c489455ea5fac43a5f20911dfb5486630eb0160Andrew Kaylor
55abb38fe8dec11b1ea7535f84fac8ad0f0af70addDavid Tweed  /// finalizeObject - ensure the module is fully processed and is usable.
56abb38fe8dec11b1ea7535f84fac8ad0f0af70addDavid Tweed  ///
57abb38fe8dec11b1ea7535f84fac8ad0f0af70addDavid Tweed  /// It is the user-level function for completing the process of making the
58abb38fe8dec11b1ea7535f84fac8ad0f0af70addDavid Tweed  /// object usable for execution. It should be called after sections within an
59abb38fe8dec11b1ea7535f84fac8ad0f0af70addDavid Tweed  /// object have been relocated using mapSectionAddress.  When this method is
60abb38fe8dec11b1ea7535f84fac8ad0f0af70addDavid Tweed  /// called the MCJIT execution engine will reapply relocations for a loaded
61abb38fe8dec11b1ea7535f84fac8ad0f0af70addDavid Tweed  /// object.
6228989889bf3aa3314562d438aece245b71176ec4Andrew Kaylor  virtual void finalizeObject();
6328989889bf3aa3314562d438aece245b71176ec4Andrew Kaylor
646aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  virtual void *getPointerToBasicBlock(BasicBlock *BB);
656aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
666aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  virtual void *getPointerToFunction(Function *F);
676aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
686aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  virtual void *recompileAndRelinkFunction(Function *F);
696aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
706aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  virtual void freeMachineCodeForFunction(Function *F);
716aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
726aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  virtual GenericValue runFunction(Function *F,
736aec29848676494867e26307698155bc2c5a4033Daniel Dunbar                                   const std::vector<GenericValue> &ArgValues);
746aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
7534714a06096f854c76371295d8c20b017fbba50bJim Grosbach  /// getPointerToNamedFunction - This method returns the address of the
7634714a06096f854c76371295d8c20b017fbba50bJim Grosbach  /// specified function by using the dlsym function call.  As such it is only
7734714a06096f854c76371295d8c20b017fbba50bJim Grosbach  /// useful for resolving library symbols, not code generated symbols.
7834714a06096f854c76371295d8c20b017fbba50bJim Grosbach  ///
7934714a06096f854c76371295d8c20b017fbba50bJim Grosbach  /// If AbortOnFailure is false and no function with the given name is
8034714a06096f854c76371295d8c20b017fbba50bJim Grosbach  /// found, this function silently returns a null pointer. Otherwise,
8134714a06096f854c76371295d8c20b017fbba50bJim Grosbach  /// it prints a message to stderr and aborts.
8234714a06096f854c76371295d8c20b017fbba50bJim Grosbach  ///
8345a93d6eb4c55876e94f91b471657f23fe2149aaDanil Malyshev  virtual void *getPointerToNamedFunction(const std::string &Name,
8445a93d6eb4c55876e94f91b471657f23fe2149aaDanil Malyshev                                          bool AbortOnFailure = true);
8530b9e322e159df8eaabb5b194cec6e11ba99c261Danil Malyshev
86020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  /// mapSectionAddress - map a section to its target address space value.
87020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  /// Map the address of a JIT section as returned from the memory manager
88020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  /// to the address in the target process as the running code will see it.
89020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  /// This is the address which will be used for relocation resolution.
90e940c1bb6c976539f07d6f440aeaacf7c25e1ddcJim Grosbach  virtual void mapSectionAddress(const void *LocalAddress,
91e940c1bb6c976539f07d6f440aeaacf7c25e1ddcJim Grosbach                                 uint64_t TargetAddress) {
92020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach    Dyld.mapSectionAddress(LocalAddress, TargetAddress);
93020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  }
94020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach
95776054dd938472828ac3ebf75b05e21171ef4ecfAndrew Kaylor  virtual void RegisterJITEventListener(JITEventListener *L);
96776054dd938472828ac3ebf75b05e21171ef4ecfAndrew Kaylor  virtual void UnregisterJITEventListener(JITEventListener *L);
97776054dd938472828ac3ebf75b05e21171ef4ecfAndrew Kaylor
986aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  /// @}
996aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  /// @name (Private) Registration Interfaces
1006aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  /// @{
1016aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
1026aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  static void Register() {
1036aec29848676494867e26307698155bc2c5a4033Daniel Dunbar    MCJITCtor = createJIT;
1046aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  }
1056aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
1066aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  static ExecutionEngine *createJIT(Module *M,
1076aec29848676494867e26307698155bc2c5a4033Daniel Dunbar                                    std::string *ErrorStr,
10813a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo                                    RTDyldMemoryManager *MemMgr,
1096aec29848676494867e26307698155bc2c5a4033Daniel Dunbar                                    bool GVsWithCode,
110c5b28580a94e247300e5d3ccf532e153f2ae6f12Dylan Noblesmith                                    TargetMachine *TM);
1116aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
1126aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  // @}
113ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylor
114ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylorprotected:
115ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylor  /// emitObject -- Generate a JITed object in memory from the specified module
116ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylor  /// Currently, MCJIT only supports a single module and the module passed to
117ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylor  /// this function call is expected to be the contained module.  The module
118ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylor  /// is passed as a parameter here to prepare for multiple module support in
119ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylor  /// the future.
1201c489455ea5fac43a5f20911dfb5486630eb0160Andrew Kaylor  ObjectBufferStream* emitObject(Module *M);
1211c489455ea5fac43a5f20911dfb5486630eb0160Andrew Kaylor
1221c489455ea5fac43a5f20911dfb5486630eb0160Andrew Kaylor  void loadObject(Module *M);
123776054dd938472828ac3ebf75b05e21171ef4ecfAndrew Kaylor
124776054dd938472828ac3ebf75b05e21171ef4ecfAndrew Kaylor  void NotifyObjectEmitted(const ObjectImage& Obj);
125776054dd938472828ac3ebf75b05e21171ef4ecfAndrew Kaylor  void NotifyFreeingObject(const ObjectImage& Obj);
1266aec29848676494867e26307698155bc2c5a4033Daniel Dunbar};
1276aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
1286aec29848676494867e26307698155bc2c5a4033Daniel Dunbar} // End llvm namespace
1296aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
1306aec29848676494867e26307698155bc2c5a4033Daniel Dunbar#endif
131