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