MCJIT.h revision 3f23cef24fc9200def464bd4bce820678b5715de
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"
146aec29848676494867e26307698155bc2c5a4033Daniel Dunbar#include "llvm/ExecutionEngine/ExecutionEngine.h"
15f922910494377909b4cf2a0b73f509b2b1925799Jim Grosbach#include "llvm/ExecutionEngine/RuntimeDyld.h"
166aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
176aec29848676494867e26307698155bc2c5a4033Daniel Dunbarnamespace llvm {
186aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
193f23cef24fc9200def464bd4bce820678b5715deAndrew Kaylorclass ObjectImage;
203f23cef24fc9200def464bd4bce820678b5715deAndrew Kaylor
2131649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach// FIXME: This makes all kinds of horrible assumptions for the time being,
2231649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach// like only having one module, not needing to worry about multi-threading,
2331649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach// blah blah. Purely in get-it-up-and-limping mode for now.
2431649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach
256aec29848676494867e26307698155bc2c5a4033Daniel Dunbarclass MCJIT : public ExecutionEngine {
268005bcd5e0c923881d82afcb813a7e537cd1b241Jim Grosbach  MCJIT(Module *M, TargetMachine *tm, RTDyldMemoryManager *MemMgr,
278005bcd5e0c923881d82afcb813a7e537cd1b241Jim Grosbach        bool AllocateGVsWithCode);
2831649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach
2931649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach  TargetMachine *TM;
3031649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach  MCContext *Ctx;
31fcbe5b71936b820647dffff0e4f9c60ece3988a5Jim Grosbach  RTDyldMemoryManager *MemMgr;
32ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylor  RuntimeDyld Dyld;
3331649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach
34ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylor  // FIXME: Add support for multiple modules
35ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylor  bool isCompiled;
3631649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach  Module *M;
373f23cef24fc9200def464bd4bce820678b5715deAndrew Kaylor  OwningPtr<ObjectImage> LoadedObject;
3831649e61bcead26a63c7cd452da90fff5e000b91Jim Grosbach
396aec29848676494867e26307698155bc2c5a4033Daniel Dunbarpublic:
406aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  ~MCJIT();
416aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
426aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  /// @name ExecutionEngine interface implementation
436aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  /// @{
446aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
456aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  virtual void *getPointerToBasicBlock(BasicBlock *BB);
466aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
476aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  virtual void *getPointerToFunction(Function *F);
486aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
496aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  virtual void *recompileAndRelinkFunction(Function *F);
506aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
516aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  virtual void freeMachineCodeForFunction(Function *F);
526aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
536aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  virtual GenericValue runFunction(Function *F,
546aec29848676494867e26307698155bc2c5a4033Daniel Dunbar                                   const std::vector<GenericValue> &ArgValues);
556aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
5634714a06096f854c76371295d8c20b017fbba50bJim Grosbach  /// getPointerToNamedFunction - This method returns the address of the
5734714a06096f854c76371295d8c20b017fbba50bJim Grosbach  /// specified function by using the dlsym function call.  As such it is only
5834714a06096f854c76371295d8c20b017fbba50bJim Grosbach  /// useful for resolving library symbols, not code generated symbols.
5934714a06096f854c76371295d8c20b017fbba50bJim Grosbach  ///
6034714a06096f854c76371295d8c20b017fbba50bJim Grosbach  /// If AbortOnFailure is false and no function with the given name is
6134714a06096f854c76371295d8c20b017fbba50bJim Grosbach  /// found, this function silently returns a null pointer. Otherwise,
6234714a06096f854c76371295d8c20b017fbba50bJim Grosbach  /// it prints a message to stderr and aborts.
6334714a06096f854c76371295d8c20b017fbba50bJim Grosbach  ///
6445a93d6eb4c55876e94f91b471657f23fe2149aaDanil Malyshev  virtual void *getPointerToNamedFunction(const std::string &Name,
6545a93d6eb4c55876e94f91b471657f23fe2149aaDanil Malyshev                                          bool AbortOnFailure = true);
6630b9e322e159df8eaabb5b194cec6e11ba99c261Danil Malyshev
67020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  /// mapSectionAddress - map a section to its target address space value.
68020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  /// Map the address of a JIT section as returned from the memory manager
69020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  /// to the address in the target process as the running code will see it.
70020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  /// This is the address which will be used for relocation resolution.
71e940c1bb6c976539f07d6f440aeaacf7c25e1ddcJim Grosbach  virtual void mapSectionAddress(const void *LocalAddress,
72e940c1bb6c976539f07d6f440aeaacf7c25e1ddcJim Grosbach                                 uint64_t TargetAddress) {
73020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach    Dyld.mapSectionAddress(LocalAddress, TargetAddress);
74020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  }
75020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach
766aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  /// @}
776aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  /// @name (Private) Registration Interfaces
786aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  /// @{
796aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
806aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  static void Register() {
816aec29848676494867e26307698155bc2c5a4033Daniel Dunbar    MCJITCtor = createJIT;
826aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  }
836aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
846aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  static ExecutionEngine *createJIT(Module *M,
856aec29848676494867e26307698155bc2c5a4033Daniel Dunbar                                    std::string *ErrorStr,
866aec29848676494867e26307698155bc2c5a4033Daniel Dunbar                                    JITMemoryManager *JMM,
876aec29848676494867e26307698155bc2c5a4033Daniel Dunbar                                    bool GVsWithCode,
88c5b28580a94e247300e5d3ccf532e153f2ae6f12Dylan Noblesmith                                    TargetMachine *TM);
896aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
906aec29848676494867e26307698155bc2c5a4033Daniel Dunbar  // @}
91ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylor
92ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylorprotected:
93ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylor  /// emitObject -- Generate a JITed object in memory from the specified module
94ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylor  /// Currently, MCJIT only supports a single module and the module passed to
95ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylor  /// this function call is expected to be the contained module.  The module
96ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylor  /// is passed as a parameter here to prepare for multiple module support in
97ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylor  /// the future.
98ea708d1071898bd8fda58f9d58d1d3fe38faf9f2Andrew Kaylor  void emitObject(Module *M);
996aec29848676494867e26307698155bc2c5a4033Daniel Dunbar};
1006aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
1016aec29848676494867e26307698155bc2c5a4033Daniel Dunbar} // End llvm namespace
1026aec29848676494867e26307698155bc2c5a4033Daniel Dunbar
1036aec29848676494867e26307698155bc2c5a4033Daniel Dunbar#endif
104