blob: a899d4f4080ceff2320c628c4232f2fce029d322 [file] [log] [blame]
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +00001//===-- MCJIT.h - Class definition for the MCJIT ----------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLVM_LIB_EXECUTIONENGINE_MCJIT_H
11#define LLVM_LIB_EXECUTIONENGINE_MCJIT_H
12
Andrew Kaylord8ffd9c2012-11-06 18:51:59 +000013#include "llvm/ADT/SmallVector.h"
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +000014#include "llvm/ExecutionEngine/ExecutionEngine.h"
Andrew Kaylorced4e8f2013-04-25 21:02:36 +000015#include "llvm/ExecutionEngine/ObjectCache.h"
Jim Grosbach348a5482011-03-22 01:06:42 +000016#include "llvm/ExecutionEngine/RuntimeDyld.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000017#include "llvm/PassManager.h"
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +000018
19namespace llvm {
20
Andrew Kayloradc70562012-10-02 21:18:39 +000021class ObjectImage;
22
Jim Grosbach7b162492011-03-18 22:48:41 +000023// FIXME: This makes all kinds of horrible assumptions for the time being,
24// like only having one module, not needing to worry about multi-threading,
25// blah blah. Purely in get-it-up-and-limping mode for now.
26
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +000027class MCJIT : public ExecutionEngine {
Jim Grosbachbea67532012-08-21 15:42:49 +000028 MCJIT(Module *M, TargetMachine *tm, RTDyldMemoryManager *MemMgr,
29 bool AllocateGVsWithCode);
Jim Grosbach7b162492011-03-18 22:48:41 +000030
31 TargetMachine *TM;
32 MCContext *Ctx;
Jim Grosbach2dcef0502011-04-04 23:04:39 +000033 RTDyldMemoryManager *MemMgr;
Andrew Kaylor1a568c32012-08-07 18:33:00 +000034 RuntimeDyld Dyld;
Andrew Kaylord8ffd9c2012-11-06 18:51:59 +000035 SmallVector<JITEventListener*, 2> EventListeners;
Jim Grosbach7b162492011-03-18 22:48:41 +000036
Andrew Kaylor1a568c32012-08-07 18:33:00 +000037 // FIXME: Add support for multiple modules
Andrew Kaylorced4e8f2013-04-25 21:02:36 +000038 bool IsLoaded;
Jim Grosbach7b162492011-03-18 22:48:41 +000039 Module *M;
Andrew Kayloradc70562012-10-02 21:18:39 +000040 OwningPtr<ObjectImage> LoadedObject;
Jim Grosbach7b162492011-03-18 22:48:41 +000041
Andrew Kaylorced4e8f2013-04-25 21:02:36 +000042 // An optional ObjectCache to be notified of compiled objects and used to
43 // perform lookup of pre-compiled code to avoid re-compilation.
44 ObjectCache *ObjCache;
45
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +000046public:
47 ~MCJIT();
48
49 /// @name ExecutionEngine interface implementation
50 /// @{
51
Andrew Kaylorced4e8f2013-04-25 21:02:36 +000052 /// Sets the object manager that MCJIT should use to avoid compilation.
53 virtual void setObjectCache(ObjectCache *manager);
54
David Tweed2e7efed2013-05-17 10:01:46 +000055 /// finalizeObject - ensure the module is fully processed and is usable.
56 ///
57 /// It is the user-level function for completing the process of making the
58 /// object usable for execution. It should be called after sections within an
59 /// object have been relocated using mapSectionAddress. When this method is
60 /// called the MCJIT execution engine will reapply relocations for a loaded
61 /// object.
Andrew Kaylora714efc2012-11-05 20:57:16 +000062 virtual void finalizeObject();
63
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +000064 virtual void *getPointerToBasicBlock(BasicBlock *BB);
65
66 virtual void *getPointerToFunction(Function *F);
67
68 virtual void *recompileAndRelinkFunction(Function *F);
69
70 virtual void freeMachineCodeForFunction(Function *F);
71
72 virtual GenericValue runFunction(Function *F,
73 const std::vector<GenericValue> &ArgValues);
74
Jim Grosbachd5274402011-03-22 18:05:27 +000075 /// getPointerToNamedFunction - This method returns the address of the
76 /// specified function by using the dlsym function call. As such it is only
77 /// useful for resolving library symbols, not code generated symbols.
78 ///
79 /// If AbortOnFailure is false and no function with the given name is
80 /// found, this function silently returns a null pointer. Otherwise,
81 /// it prints a message to stderr and aborts.
82 ///
Danil Malyshev7e325782012-01-05 21:16:14 +000083 virtual void *getPointerToNamedFunction(const std::string &Name,
84 bool AbortOnFailure = true);
Danil Malyshevbfee5422012-03-28 21:46:36 +000085
Jim Grosbach0ddb3a42012-01-16 23:50:55 +000086 /// mapSectionAddress - map a section to its target address space value.
87 /// Map the address of a JIT section as returned from the memory manager
88 /// to the address in the target process as the running code will see it.
89 /// This is the address which will be used for relocation resolution.
Jim Grosbach6d613972012-09-13 21:50:06 +000090 virtual void mapSectionAddress(const void *LocalAddress,
91 uint64_t TargetAddress) {
Jim Grosbach0ddb3a42012-01-16 23:50:55 +000092 Dyld.mapSectionAddress(LocalAddress, TargetAddress);
93 }
94
Andrew Kaylord8ffd9c2012-11-06 18:51:59 +000095 virtual void RegisterJITEventListener(JITEventListener *L);
96 virtual void UnregisterJITEventListener(JITEventListener *L);
97
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +000098 /// @}
99 /// @name (Private) Registration Interfaces
100 /// @{
101
102 static void Register() {
103 MCJITCtor = createJIT;
104 }
105
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000106 static ExecutionEngine *createJIT(Module *M,
107 std::string *ErrorStr,
Filip Pizlo9bc53e82013-05-14 19:29:00 +0000108 RTDyldMemoryManager *MemMgr,
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000109 bool GVsWithCode,
Dylan Noblesmith8418fdc2011-05-13 21:51:29 +0000110 TargetMachine *TM);
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000111
112 // @}
Andrew Kaylor1a568c32012-08-07 18:33:00 +0000113
114protected:
115 /// emitObject -- Generate a JITed object in memory from the specified module
116 /// Currently, MCJIT only supports a single module and the module passed to
117 /// this function call is expected to be the contained module. The module
118 /// is passed as a parameter here to prepare for multiple module support in
119 /// the future.
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000120 ObjectBufferStream* emitObject(Module *M);
121
122 void loadObject(Module *M);
Andrew Kaylord8ffd9c2012-11-06 18:51:59 +0000123
124 void NotifyObjectEmitted(const ObjectImage& Obj);
125 void NotifyFreeingObject(const ObjectImage& Obj);
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000126};
127
128} // End llvm namespace
129
130#endif