1 //===- ExecutionEngine.h - Abstract Execution Engine Interface --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the abstract interface that implements execution support
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_EXECUTION_ENGINE_H
16 #define LLVM_EXECUTION_ENGINE_H
22 #include "llvm/System/Mutex.h"
23 #include "llvm/ADT/SmallVector.h"
37 class JITMemoryManager;
39 class ExecutionEngineState {
41 /// GlobalAddressMap - A mapping between LLVM global values and their
42 /// actualized version...
43 std::map<const GlobalValue*, void *> GlobalAddressMap;
45 /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap,
46 /// used to convert raw addresses into the LLVM global value that is emitted
47 /// at the address. This map is not computed unless getGlobalValueAtAddress
48 /// is called at some point.
49 std::map<void *, const GlobalValue*> GlobalAddressReverseMap;
52 std::map<const GlobalValue*, void *> &
53 getGlobalAddressMap(const MutexGuard &) {
54 return GlobalAddressMap;
57 std::map<void*, const GlobalValue*> &
58 getGlobalAddressReverseMap(const MutexGuard &) {
59 return GlobalAddressReverseMap;
64 class ExecutionEngine {
66 ExecutionEngineState state;
67 bool LazyCompilationDisabled;
68 bool GVCompilationDisabled;
69 bool SymbolSearchingDisabled;
72 /// Modules - This is a list of ModuleProvider's that we are JIT'ing from. We
73 /// use a smallvector to optimize for the case where there is only one module.
74 SmallVector<ModuleProvider*, 1> Modules;
76 void setTargetData(const TargetData *td) {
80 // To avoid having libexecutionengine depend on the JIT and interpreter
81 // libraries, the JIT and Interpreter set these functions to ctor pointers
82 // at startup time if they are linked in.
83 typedef ExecutionEngine *(*EECtorFn)(ModuleProvider*, std::string*,
85 static EECtorFn JITCtor, InterpCtor;
87 /// LazyFunctionCreator - If an unknown function is needed, this function
88 /// pointer is invoked to create it. If this returns null, the JIT will abort.
89 void* (*LazyFunctionCreator)(const std::string &);
91 /// ExceptionTableRegister - If Exception Handling is set, the JIT will
92 /// register dwarf tables with this function
93 typedef void (*EERegisterFn)(void*);
94 static EERegisterFn ExceptionTableRegister;
97 /// lock - This lock is protects the ExecutionEngine, JIT, JITResolver and
98 /// JITEmitter classes. It must be held while changing the internal state of
99 /// any of those classes.
100 sys::Mutex lock; // Used to make this class and subclasses thread-safe
102 //===--------------------------------------------------------------------===//
103 // ExecutionEngine Startup
104 //===--------------------------------------------------------------------===//
106 virtual ~ExecutionEngine();
108 /// create - This is the factory method for creating an execution engine which
109 /// is appropriate for the current machine. This takes ownership of the
111 static ExecutionEngine *create(ModuleProvider *MP,
112 bool ForceInterpreter = false,
113 std::string *ErrorStr = 0,
116 /// create - This is the factory method for creating an execution engine which
117 /// is appropriate for the current machine. This takes ownership of the
119 static ExecutionEngine *create(Module *M);
121 /// createJIT - This is the factory method for creating a JIT for the current
122 /// machine, it does not fall back to the interpreter. This takes ownership
123 /// of the ModuleProvider and JITMemoryManager if successful.
124 static ExecutionEngine *createJIT(ModuleProvider *MP,
125 std::string *ErrorStr = 0,
126 JITMemoryManager *JMM = 0,
131 /// addModuleProvider - Add a ModuleProvider to the list of modules that we
132 /// can JIT from. Note that this takes ownership of the ModuleProvider: when
133 /// the ExecutionEngine is destroyed, it destroys the MP as well.
134 virtual void addModuleProvider(ModuleProvider *P) {
135 Modules.push_back(P);
138 //===----------------------------------------------------------------------===//
140 const TargetData *getTargetData() const { return TD; }
143 /// removeModuleProvider - Remove a ModuleProvider from the list of modules.
144 /// Release module from ModuleProvider.
145 virtual Module* removeModuleProvider(ModuleProvider *P,
146 std::string *ErrInfo = 0);
148 /// FindFunctionNamed - Search all of the active modules to find the one that
149 /// defines FnName. This is very slow operation and shouldn't be used for
151 Function *FindFunctionNamed(const char *FnName);
153 /// runFunction - Execute the specified function with the specified arguments,
154 /// and return the result.
156 virtual GenericValue runFunction(Function *F,
157 const std::vector<GenericValue> &ArgValues) = 0;
159 /// runStaticConstructorsDestructors - This method is used to execute all of
160 /// the static constructors or destructors for a program, depending on the
161 /// value of isDtors.
162 void runStaticConstructorsDestructors(bool isDtors);
163 /// runStaticConstructorsDestructors - This method is used to execute all of
164 /// the static constructors or destructors for a module, depending on the
165 /// value of isDtors.
166 void runStaticConstructorsDestructors(Module *module, bool isDtors);
169 /// runFunctionAsMain - This is a helper function which wraps runFunction to
170 /// handle the common task of starting up main with the specified argc, argv,
171 /// and envp parameters.
172 int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
173 const char * const * envp);
176 /// addGlobalMapping - Tell the execution engine that the specified global is
177 /// at the specified location. This is used internally as functions are JIT'd
178 /// and as global variables are laid out in memory. It can and should also be
179 /// used by clients of the EE that want to have an LLVM global overlay
180 /// existing data in memory.
181 void addGlobalMapping(const GlobalValue *GV, void *Addr);
183 /// clearAllGlobalMappings - Clear all global mappings and start over again
184 /// use in dynamic compilation scenarios when you want to move globals
185 void clearAllGlobalMappings();
187 /// clearGlobalMappingsFromModule - Clear all global mappings that came from a
188 /// particular module, because it has been removed from the JIT.
189 void clearGlobalMappingsFromModule(Module *M);
191 /// updateGlobalMapping - Replace an existing mapping for GV with a new
192 /// address. This updates both maps as required. If "Addr" is null, the
193 /// entry for the global is removed from the mappings. This returns the old
194 /// value of the pointer, or null if it was not in the map.
195 void *updateGlobalMapping(const GlobalValue *GV, void *Addr);
197 /// getPointerToGlobalIfAvailable - This returns the address of the specified
198 /// global value if it is has already been codegen'd, otherwise it returns
201 void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
203 /// getPointerToGlobal - This returns the address of the specified global
204 /// value. This may involve code generation if it's a function.
206 void *getPointerToGlobal(const GlobalValue *GV);
208 /// getPointerToFunction - The different EE's represent function bodies in
209 /// different ways. They should each implement this to say what a function
210 /// pointer should look like.
212 virtual void *getPointerToFunction(Function *F) = 0;
214 /// getPointerToFunctionOrStub - If the specified function has been
215 /// code-gen'd, return a pointer to the function. If not, compile it, or use
216 /// a stub to implement lazy compilation if available.
218 virtual void *getPointerToFunctionOrStub(Function *F) {
219 // Default implementation, just codegen the function.
220 return getPointerToFunction(F);
223 /// getGlobalValueAtAddress - Return the LLVM global value object that starts
224 /// at the specified address.
226 const GlobalValue *getGlobalValueAtAddress(void *Addr);
229 void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr,
231 void InitializeMemory(const Constant *Init, void *Addr);
233 /// recompileAndRelinkFunction - This method is used to force a function
234 /// which has already been compiled to be compiled again, possibly
235 /// after it has been modified. Then the entry to the old copy is overwritten
236 /// with a branch to the new copy. If there was no old copy, this acts
237 /// just like VM::getPointerToFunction().
239 virtual void *recompileAndRelinkFunction(Function *F) = 0;
241 /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
242 /// corresponding to the machine code emitted to execute this function, useful
243 /// for garbage-collecting generated code.
245 virtual void freeMachineCodeForFunction(Function *F) = 0;
247 /// getOrEmitGlobalVariable - Return the address of the specified global
248 /// variable, possibly emitting it to memory if needed. This is used by the
250 virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
251 return getPointerToGlobal((GlobalValue*)GV);
254 /// DisableLazyCompilation - If called, the JIT will abort if lazy compilation
255 /// is ever attempted.
256 void DisableLazyCompilation(bool Disabled = true) {
257 LazyCompilationDisabled = Disabled;
259 bool isLazyCompilationDisabled() const {
260 return LazyCompilationDisabled;
263 /// DisableGVCompilation - If called, the JIT will abort if it's asked to allocate
264 /// space and populate a GlobalVariable.
265 void DisableGVCompilation(bool Disabled = true) {
266 GVCompilationDisabled = Disabled;
268 bool isGVCompilationDisabled() const {
269 return GVCompilationDisabled;
272 /// DisableSymbolSearching - If called, the JIT will not try to lookup unknown
273 /// symbols with dlsym. A client can still use InstallLazyFunctionCreator to
274 /// resolve symbols in a custom way.
275 void DisableSymbolSearching(bool Disabled = true) {
276 SymbolSearchingDisabled = Disabled;
278 bool isSymbolSearchingDisabled() const {
279 return SymbolSearchingDisabled;
283 /// InstallLazyFunctionCreator - If an unknown function is needed, the
284 /// specified function pointer is invoked to create it. If it returns null,
285 /// the JIT will abort.
286 void InstallLazyFunctionCreator(void* (*P)(const std::string &)) {
287 LazyFunctionCreator = P;
290 /// InstallExceptionTableRegister - The JIT will use the given function
291 /// to register the exception tables it generates.
292 static void InstallExceptionTableRegister(void (*F)(void*)) {
293 ExceptionTableRegister = F;
296 /// RegisterTable - Registers the given pointer as an exception table. It uses
297 /// the ExceptionTableRegister function.
298 static void RegisterTable(void* res) {
299 if (ExceptionTableRegister)
300 ExceptionTableRegister(res);
304 explicit ExecutionEngine(ModuleProvider *P);
308 // EmitGlobalVariable - This method emits the specified global variable to the
309 // address specified in GlobalAddresses, or allocates new memory if it's not
310 // already in the map.
311 void EmitGlobalVariable(const GlobalVariable *GV);
313 GenericValue getConstantValue(const Constant *C);
314 void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr,
318 } // End llvm namespace