Add a new flag that disables symbol lookup with dlsym when set. This allows
[oota-llvm.git] / include / llvm / ExecutionEngine / ExecutionEngine.h
1 //===- ExecutionEngine.h - Abstract Execution Engine Interface --*- 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 // This file defines the abstract interface that implements execution support
11 // for LLVM.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_EXECUTION_ENGINE_H
16 #define LLVM_EXECUTION_ENGINE_H
17
18 #include <vector>
19 #include <map>
20 #include <cassert>
21 #include <string>
22 #include "llvm/System/Mutex.h"
23 #include "llvm/ADT/SmallVector.h"
24
25 namespace llvm {
26
27 struct GenericValue;
28 class Constant;
29 class Function;
30 class GlobalVariable;
31 class GlobalValue;
32 class Module;
33 class ModuleProvider;
34 class TargetData;
35 class Type;
36 class MutexGuard;
37 class JITMemoryManager;
38
39 class ExecutionEngineState {
40 private:
41   /// GlobalAddressMap - A mapping between LLVM global values and their
42   /// actualized version...
43   std::map<const GlobalValue*, void *> GlobalAddressMap;
44
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;
50
51 public:
52   std::map<const GlobalValue*, void *> &
53   getGlobalAddressMap(const MutexGuard &) {
54     return GlobalAddressMap;
55   }
56
57   std::map<void*, const GlobalValue*> & 
58   getGlobalAddressReverseMap(const MutexGuard &) {
59     return GlobalAddressReverseMap;
60   }
61 };
62
63
64 class ExecutionEngine {
65   const TargetData *TD;
66   ExecutionEngineState state;
67   bool LazyCompilationDisabled;
68   bool SymbolSearchingDisabled;
69
70 protected:
71   /// Modules - This is a list of ModuleProvider's that we are JIT'ing from.  We
72   /// use a smallvector to optimize for the case where there is only one module.
73   SmallVector<ModuleProvider*, 1> Modules;
74   
75   void setTargetData(const TargetData *td) {
76     TD = td;
77   }
78
79   // To avoid having libexecutionengine depend on the JIT and interpreter
80   // libraries, the JIT and Interpreter set these functions to ctor pointers
81   // at startup time if they are linked in.
82   typedef ExecutionEngine *(*EECtorFn)(ModuleProvider*, std::string*);
83   static EECtorFn JITCtor, InterpCtor;
84
85   /// LazyFunctionCreator - If an unknown function is needed, this function
86   /// pointer is invoked to create it. If this returns null, the JIT will abort.
87   void* (*LazyFunctionCreator)(const std::string &);
88   
89   /// ExceptionTableRegister - If Exception Handling is set, the JIT will 
90   /// register dwarf tables with this function
91   typedef void (*EERegisterFn)(void*);
92   static EERegisterFn ExceptionTableRegister;
93
94 public:
95   /// lock - This lock is protects the ExecutionEngine, JIT, JITResolver and
96   /// JITEmitter classes.  It must be held while changing the internal state of
97   /// any of those classes.
98   sys::Mutex lock; // Used to make this class and subclasses thread-safe
99
100   //===--------------------------------------------------------------------===//
101   //  ExecutionEngine Startup
102   //===--------------------------------------------------------------------===//
103
104   virtual ~ExecutionEngine();
105
106   /// create - This is the factory method for creating an execution engine which
107   /// is appropriate for the current machine.  This takes ownership of the
108   /// module provider.
109   static ExecutionEngine *create(ModuleProvider *MP,
110                                  bool ForceInterpreter = false,
111                                  std::string *ErrorStr = 0);
112   
113   /// create - This is the factory method for creating an execution engine which
114   /// is appropriate for the current machine.  This takes ownership of the
115   /// module.
116   static ExecutionEngine *create(Module *M);
117
118   /// createJIT - This is the factory method for creating a JIT for the current
119   /// machine, it does not fall back to the interpreter.  This takes ownership
120   /// of the ModuleProvider and JITMemoryManager if successful.
121   static ExecutionEngine *createJIT(ModuleProvider *MP,
122                                     std::string *ErrorStr = 0,
123                                     JITMemoryManager *JMM = 0);
124   
125   
126   
127   /// addModuleProvider - Add a ModuleProvider to the list of modules that we
128   /// can JIT from.  Note that this takes ownership of the ModuleProvider: when
129   /// the ExecutionEngine is destroyed, it destroys the MP as well.
130   virtual void addModuleProvider(ModuleProvider *P) {
131     Modules.push_back(P);
132   }
133   
134   //===----------------------------------------------------------------------===//
135
136   const TargetData *getTargetData() const { return TD; }
137
138
139   /// removeModuleProvider - Remove a ModuleProvider from the list of modules.
140   /// Release module from ModuleProvider.
141   virtual Module* removeModuleProvider(ModuleProvider *P,
142                                        std::string *ErrInfo = 0);
143
144   /// FindFunctionNamed - Search all of the active modules to find the one that
145   /// defines FnName.  This is very slow operation and shouldn't be used for
146   /// general code.
147   Function *FindFunctionNamed(const char *FnName);
148   
149   /// runFunction - Execute the specified function with the specified arguments,
150   /// and return the result.
151   ///
152   virtual GenericValue runFunction(Function *F,
153                                 const std::vector<GenericValue> &ArgValues) = 0;
154
155   /// runStaticConstructorsDestructors - This method is used to execute all of
156   /// the static constructors or destructors for a module, depending on the
157   /// value of isDtors.
158   void runStaticConstructorsDestructors(bool isDtors);
159   
160   
161   /// runFunctionAsMain - This is a helper function which wraps runFunction to
162   /// handle the common task of starting up main with the specified argc, argv,
163   /// and envp parameters.
164   int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
165                         const char * const * envp);
166
167
168   /// addGlobalMapping - Tell the execution engine that the specified global is
169   /// at the specified location.  This is used internally as functions are JIT'd
170   /// and as global variables are laid out in memory.  It can and should also be
171   /// used by clients of the EE that want to have an LLVM global overlay
172   /// existing data in memory.
173   void addGlobalMapping(const GlobalValue *GV, void *Addr);
174   
175   /// clearAllGlobalMappings - Clear all global mappings and start over again
176   /// use in dynamic compilation scenarios when you want to move globals
177   void clearAllGlobalMappings();
178   
179   /// clearGlobalMappingsFromModule - Clear all global mappings that came from a
180   /// particular module, because it has been removed from the JIT.
181   void clearGlobalMappingsFromModule(Module *M);
182   
183   /// updateGlobalMapping - Replace an existing mapping for GV with a new
184   /// address.  This updates both maps as required.  If "Addr" is null, the
185   /// entry for the global is removed from the mappings.  This returns the old
186   /// value of the pointer, or null if it was not in the map.
187   void *updateGlobalMapping(const GlobalValue *GV, void *Addr);
188   
189   /// getPointerToGlobalIfAvailable - This returns the address of the specified
190   /// global value if it is has already been codegen'd, otherwise it returns
191   /// null.
192   ///
193   void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
194
195   /// getPointerToGlobal - This returns the address of the specified global
196   /// value.  This may involve code generation if it's a function.
197   ///
198   void *getPointerToGlobal(const GlobalValue *GV);
199
200   /// getPointerToFunction - The different EE's represent function bodies in
201   /// different ways.  They should each implement this to say what a function
202   /// pointer should look like.
203   ///
204   virtual void *getPointerToFunction(Function *F) = 0;
205
206   /// getPointerToFunctionOrStub - If the specified function has been
207   /// code-gen'd, return a pointer to the function.  If not, compile it, or use
208   /// a stub to implement lazy compilation if available.
209   ///
210   virtual void *getPointerToFunctionOrStub(Function *F) {
211     // Default implementation, just codegen the function.
212     return getPointerToFunction(F);
213   }
214
215   /// getGlobalValueAtAddress - Return the LLVM global value object that starts
216   /// at the specified address.
217   ///
218   const GlobalValue *getGlobalValueAtAddress(void *Addr);
219
220
221   void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr,
222                           const Type *Ty);
223   void InitializeMemory(const Constant *Init, void *Addr);
224
225   /// recompileAndRelinkFunction - This method is used to force a function
226   /// which has already been compiled to be compiled again, possibly
227   /// after it has been modified. Then the entry to the old copy is overwritten
228   /// with a branch to the new copy. If there was no old copy, this acts
229   /// just like VM::getPointerToFunction().
230   ///
231   virtual void *recompileAndRelinkFunction(Function *F) = 0;
232
233   /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
234   /// corresponding to the machine code emitted to execute this function, useful
235   /// for garbage-collecting generated code.
236   ///
237   virtual void freeMachineCodeForFunction(Function *F) = 0;
238
239   /// getOrEmitGlobalVariable - Return the address of the specified global
240   /// variable, possibly emitting it to memory if needed.  This is used by the
241   /// Emitter.
242   virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
243     return getPointerToGlobal((GlobalValue*)GV);
244   }
245   
246   /// DisableLazyCompilation - If called, the JIT will abort if lazy compilation
247   /// is ever attempted.
248   void DisableLazyCompilation(bool Disabled = true) {
249     LazyCompilationDisabled = Disabled;
250   }
251   bool isLazyCompilationDisabled() const {
252     return LazyCompilationDisabled;
253   }
254   /// DisableSymbolSearching - If called, the JIT will not try to lookup unknown
255   /// symbols with dlsym.  A client can still use InstallLazyFunctionCreator to
256   /// resolve symbols in a custom way.
257   void DisableSymbolSearching(bool Disabled = true) {
258     SymbolSearchingDisabled = Disabled;
259   }
260   bool isSymbolSearchingDisabled() const {
261     return SymbolSearchingDisabled;
262   }
263   
264   
265   /// InstallLazyFunctionCreator - If an unknown function is needed, the
266   /// specified function pointer is invoked to create it.  If it returns null,
267   /// the JIT will abort.
268   void InstallLazyFunctionCreator(void* (*P)(const std::string &)) {
269     LazyFunctionCreator = P;
270   }
271   
272   /// InstallExceptionTableRegister - The JIT will use the given function
273   /// to register the exception tables it generates.
274   static void InstallExceptionTableRegister(void (*F)(void*)) {
275     ExceptionTableRegister = F;
276   }
277   
278   /// RegisterTable - Registers the given pointer as an exception table. It uses
279   /// the ExceptionTableRegister function.
280   static void RegisterTable(void* res) {
281     if (ExceptionTableRegister)
282       ExceptionTableRegister(res);
283   }
284
285 protected:
286   explicit ExecutionEngine(ModuleProvider *P);
287
288   void emitGlobals();
289
290   // EmitGlobalVariable - This method emits the specified global variable to the
291   // address specified in GlobalAddresses, or allocates new memory if it's not
292   // already in the map.
293   void EmitGlobalVariable(const GlobalVariable *GV);
294
295   GenericValue getConstantValue(const Constant *C);
296   void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr, 
297                            const Type *Ty);
298 };
299
300 } // End llvm namespace
301
302 #endif