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