Remove warnings about unused parameters and shadowed variables.
[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   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   Module* removeModuleProvider(ModuleProvider *P, std::string *ErrInfo = 0);
141
142   /// FindFunctionNamed - Search all of the active modules to find the one that
143   /// defines FnName.  This is very slow operation and shouldn't be used for
144   /// general code.
145   Function *FindFunctionNamed(const char *FnName);
146   
147   /// runFunction - Execute the specified function with the specified arguments,
148   /// and return the result.
149   ///
150   virtual GenericValue runFunction(Function *F,
151                                 const std::vector<GenericValue> &ArgValues) = 0;
152
153   /// runStaticConstructorsDestructors - This method is used to execute all of
154   /// the static constructors or destructors for a module, depending on the
155   /// value of isDtors.
156   void runStaticConstructorsDestructors(bool isDtors);
157   
158   
159   /// runFunctionAsMain - This is a helper function which wraps runFunction to
160   /// handle the common task of starting up main with the specified argc, argv,
161   /// and envp parameters.
162   int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
163                         const char * const * envp);
164
165
166   /// addGlobalMapping - Tell the execution engine that the specified global is
167   /// at the specified location.  This is used internally as functions are JIT'd
168   /// and as global variables are laid out in memory.  It can and should also be
169   /// used by clients of the EE that want to have an LLVM global overlay
170   /// existing data in memory.
171   void addGlobalMapping(const GlobalValue *GV, void *Addr);
172   
173   /// clearAllGlobalMappings - Clear all global mappings and start over again
174   /// use in dynamic compilation scenarios when you want to move globals
175   void clearAllGlobalMappings();
176   
177   /// updateGlobalMapping - Replace an existing mapping for GV with a new
178   /// address.  This updates both maps as required.  If "Addr" is null, the
179   /// entry for the global is removed from the mappings.  This returns the old
180   /// value of the pointer, or null if it was not in the map.
181   void *updateGlobalMapping(const GlobalValue *GV, void *Addr);
182   
183   /// getPointerToGlobalIfAvailable - This returns the address of the specified
184   /// global value if it is has already been codegen'd, otherwise it returns
185   /// null.
186   ///
187   void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
188
189   /// getPointerToGlobal - This returns the address of the specified global
190   /// value.  This may involve code generation if it's a function.
191   ///
192   void *getPointerToGlobal(const GlobalValue *GV);
193
194   /// getPointerToFunction - The different EE's represent function bodies in
195   /// different ways.  They should each implement this to say what a function
196   /// pointer should look like.
197   ///
198   virtual void *getPointerToFunction(Function *F) = 0;
199
200   /// getPointerToFunctionOrStub - If the specified function has been
201   /// code-gen'd, return a pointer to the function.  If not, compile it, or use
202   /// a stub to implement lazy compilation if available.
203   ///
204   virtual void *getPointerToFunctionOrStub(Function *F) {
205     // Default implementation, just codegen the function.
206     return getPointerToFunction(F);
207   }
208
209   /// getGlobalValueAtAddress - Return the LLVM global value object that starts
210   /// at the specified address.
211   ///
212   const GlobalValue *getGlobalValueAtAddress(void *Addr);
213
214
215   void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr,
216                           const Type *Ty);
217   void InitializeMemory(const Constant *Init, void *Addr);
218
219   /// recompileAndRelinkFunction - This method is used to force a function
220   /// which has already been compiled to be compiled again, possibly
221   /// after it has been modified. Then the entry to the old copy is overwritten
222   /// with a branch to the new copy. If there was no old copy, this acts
223   /// just like VM::getPointerToFunction().
224   ///
225   virtual void *recompileAndRelinkFunction(Function *F) = 0;
226
227   /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
228   /// corresponding to the machine code emitted to execute this function, useful
229   /// for garbage-collecting generated code.
230   ///
231   virtual void freeMachineCodeForFunction(Function *F) = 0;
232
233   /// getOrEmitGlobalVariable - Return the address of the specified global
234   /// variable, possibly emitting it to memory if needed.  This is used by the
235   /// Emitter.
236   virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
237     return getPointerToGlobal((GlobalValue*)GV);
238   }
239   
240   /// DisableLazyCompilation - If called, the JIT will abort if lazy compilation
241   // is ever attempted.
242   void DisableLazyCompilation() {
243     LazyCompilationDisabled = true;
244   }
245   bool isLazyCompilationDisabled() const {
246     return LazyCompilationDisabled;
247   }
248   
249   
250   /// InstallLazyFunctionCreator - If an unknown function is needed, the
251   /// specified function pointer is invoked to create it.  If it returns null,
252   /// the JIT will abort.
253   void InstallLazyFunctionCreator(void* (*P)(const std::string &)) {
254     LazyFunctionCreator = P;
255   }
256   
257   /// InstallExceptionTableRegister - The JIT will use the given function
258   /// to register the exception tables it generates.
259   static void InstallExceptionTableRegister(void (*F)(void*)) {
260     ExceptionTableRegister = F;
261   }
262   
263   /// RegisterTable - Registers the given pointer as an exception table. It uses
264   /// the ExceptionTableRegister function.
265   static void RegisterTable(void* res) {
266     if (ExceptionTableRegister)
267       ExceptionTableRegister(res);
268   }
269
270 protected:
271   explicit ExecutionEngine(ModuleProvider *P);
272
273   void emitGlobals();
274
275   // EmitGlobalVariable - This method emits the specified global variable to the
276   // address specified in GlobalAddresses, or allocates new memory if it's not
277   // already in the map.
278   void EmitGlobalVariable(const GlobalVariable *GV);
279
280   GenericValue getConstantValue(const Constant *C);
281   void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr, 
282                            const Type *Ty);
283 };
284
285 } // End llvm namespace
286
287 #endif