Remove unused parameter.
[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 <string>
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/System/Mutex.h"
23 #include "llvm/Target/TargetMachine.h"
24
25 namespace llvm {
26
27 struct GenericValue;
28 class Constant;
29 class Function;
30 class GlobalVariable;
31 class GlobalValue;
32 class JITEventListener;
33 class JITMemoryManager;
34 class MachineCodeInfo;
35 class Module;
36 class ModuleProvider;
37 class MutexGuard;
38 class TargetData;
39 class Type;
40
41 class ExecutionEngineState {
42 private:
43   /// GlobalAddressMap - A mapping between LLVM global values and their
44   /// actualized version...
45   std::map<const GlobalValue*, void *> GlobalAddressMap;
46
47   /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap,
48   /// used to convert raw addresses into the LLVM global value that is emitted
49   /// at the address.  This map is not computed unless getGlobalValueAtAddress
50   /// is called at some point.
51   std::map<void *, const GlobalValue*> GlobalAddressReverseMap;
52
53 public:
54   std::map<const GlobalValue*, void *> &
55   getGlobalAddressMap(const MutexGuard &) {
56     return GlobalAddressMap;
57   }
58
59   std::map<void*, const GlobalValue*> & 
60   getGlobalAddressReverseMap(const MutexGuard &) {
61     return GlobalAddressReverseMap;
62   }
63 };
64
65
66 class ExecutionEngine {
67   const TargetData *TD;
68   ExecutionEngineState state;
69   bool LazyCompilationDisabled;
70   bool GVCompilationDisabled;
71   bool SymbolSearchingDisabled;
72   bool DlsymStubsEnabled;
73
74 protected:
75   /// Modules - This is a list of ModuleProvider's that we are JIT'ing from.  We
76   /// use a smallvector to optimize for the case where there is only one module.
77   SmallVector<ModuleProvider*, 1> Modules;
78   
79   void setTargetData(const TargetData *td) {
80     TD = td;
81   }
82   
83   /// getMemoryforGV - Allocate memory for a global variable.
84   virtual char* getMemoryForGV(const GlobalVariable* GV);
85
86   // To avoid having libexecutionengine depend on the JIT and interpreter
87   // libraries, the JIT and Interpreter set these functions to ctor pointers
88   // at startup time if they are linked in.
89   typedef ExecutionEngine *(*EECtorFn)(ModuleProvider*, std::string*,
90                                        CodeGenOpt::Level OptLevel);
91   static EECtorFn JITCtor, InterpCtor;
92
93   /// LazyFunctionCreator - If an unknown function is needed, this function
94   /// pointer is invoked to create it. If this returns null, the JIT will abort.
95   void* (*LazyFunctionCreator)(const std::string &);
96   
97   /// ExceptionTableRegister - If Exception Handling is set, the JIT will 
98   /// register dwarf tables with this function
99   typedef void (*EERegisterFn)(void*);
100   static EERegisterFn ExceptionTableRegister;
101
102 public:
103   /// lock - This lock is protects the ExecutionEngine, JIT, JITResolver and
104   /// JITEmitter classes.  It must be held while changing the internal state of
105   /// any of those classes.
106   sys::Mutex lock; // Used to make this class and subclasses thread-safe
107
108   //===--------------------------------------------------------------------===//
109   //  ExecutionEngine Startup
110   //===--------------------------------------------------------------------===//
111
112   virtual ~ExecutionEngine();
113
114   /// create - This is the factory method for creating an execution engine which
115   /// is appropriate for the current machine.  This takes ownership of the
116   /// module provider.
117   static ExecutionEngine *create(ModuleProvider *MP,
118                                  bool ForceInterpreter = false,
119                                  std::string *ErrorStr = 0,
120                                  CodeGenOpt::Level OptLevel =
121                                    CodeGenOpt::Default);
122   
123   /// create - This is the factory method for creating an execution engine which
124   /// is appropriate for the current machine.  This takes ownership of the
125   /// module.
126   static ExecutionEngine *create(Module *M);
127
128   /// createJIT - This is the factory method for creating a JIT for the current
129   /// machine, it does not fall back to the interpreter.  This takes ownership
130   /// of the ModuleProvider and JITMemoryManager if successful.
131   static ExecutionEngine *createJIT(ModuleProvider *MP,
132                                     std::string *ErrorStr = 0,
133                                     JITMemoryManager *JMM = 0,
134                                     CodeGenOpt::Level OptLevel =
135                                       CodeGenOpt::Default);
136
137   /// addModuleProvider - Add a ModuleProvider to the list of modules that we
138   /// can JIT from.  Note that this takes ownership of the ModuleProvider: when
139   /// the ExecutionEngine is destroyed, it destroys the MP as well.
140   virtual void addModuleProvider(ModuleProvider *P) {
141     Modules.push_back(P);
142   }
143   
144   //===----------------------------------------------------------------------===//
145
146   const TargetData *getTargetData() const { return TD; }
147
148
149   /// removeModuleProvider - Remove a ModuleProvider from the list of modules.
150   /// Relases the Module from the ModuleProvider, materializing it in the
151   /// process, and returns the materialized Module.
152   virtual Module* removeModuleProvider(ModuleProvider *P,
153                                        std::string *ErrInfo = 0);
154
155   /// deleteModuleProvider - Remove a ModuleProvider from the list of modules,
156   /// and deletes the ModuleProvider and owned Module.  Avoids materializing 
157   /// the underlying module.
158   virtual void deleteModuleProvider(ModuleProvider *P,std::string *ErrInfo = 0);
159
160   /// FindFunctionNamed - Search all of the active modules to find the one that
161   /// defines FnName.  This is very slow operation and shouldn't be used for
162   /// general code.
163   Function *FindFunctionNamed(const char *FnName);
164   
165   /// runFunction - Execute the specified function with the specified arguments,
166   /// and return the result.
167   ///
168   virtual GenericValue runFunction(Function *F,
169                                 const std::vector<GenericValue> &ArgValues) = 0;
170
171   /// runStaticConstructorsDestructors - This method is used to execute all of
172   /// the static constructors or destructors for a program, depending on the
173   /// value of isDtors.
174   void runStaticConstructorsDestructors(bool isDtors);
175   /// runStaticConstructorsDestructors - This method is used to execute all of
176   /// the static constructors or destructors for a module, depending on the
177   /// value of isDtors.
178   void runStaticConstructorsDestructors(Module *module, bool isDtors);
179   
180   
181   /// runFunctionAsMain - This is a helper function which wraps runFunction to
182   /// handle the common task of starting up main with the specified argc, argv,
183   /// and envp parameters.
184   int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
185                         const char * const * envp);
186
187
188   /// addGlobalMapping - Tell the execution engine that the specified global is
189   /// at the specified location.  This is used internally as functions are JIT'd
190   /// and as global variables are laid out in memory.  It can and should also be
191   /// used by clients of the EE that want to have an LLVM global overlay
192   /// existing data in memory.  After adding a mapping for GV, you must not
193   /// destroy it until you've removed the mapping.
194   void addGlobalMapping(const GlobalValue *GV, void *Addr);
195   
196   /// clearAllGlobalMappings - Clear all global mappings and start over again
197   /// use in dynamic compilation scenarios when you want to move globals
198   void clearAllGlobalMappings();
199   
200   /// clearGlobalMappingsFromModule - Clear all global mappings that came from a
201   /// particular module, because it has been removed from the JIT.
202   void clearGlobalMappingsFromModule(Module *M);
203   
204   /// updateGlobalMapping - Replace an existing mapping for GV with a new
205   /// address.  This updates both maps as required.  If "Addr" is null, the
206   /// entry for the global is removed from the mappings.  This returns the old
207   /// value of the pointer, or null if it was not in the map.
208   void *updateGlobalMapping(const GlobalValue *GV, void *Addr);
209   
210   /// getPointerToGlobalIfAvailable - This returns the address of the specified
211   /// global value if it is has already been codegen'd, otherwise it returns
212   /// null.
213   ///
214   void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
215
216   /// getPointerToGlobal - This returns the address of the specified global
217   /// value.  This may involve code generation if it's a function.  After
218   /// getting a pointer to GV, it and all globals it transitively refers to have
219   /// been passed to addGlobalMapping.  You must clear the mapping for each
220   /// referred-to global before destroying it.  If a referred-to global RTG is a
221   /// function and this ExecutionEngine is a JIT compiler, calling
222   /// updateGlobalMapping(RTG, 0) will leak the function's machine code, so you
223   /// should call freeMachineCodeForFunction(RTG) instead.  Note that
224   /// optimizations can move and delete non-external GlobalValues without
225   /// notifying the ExecutionEngine.
226   ///
227   void *getPointerToGlobal(const GlobalValue *GV);
228
229   /// getPointerToFunction - The different EE's represent function bodies in
230   /// different ways.  They should each implement this to say what a function
231   /// pointer should look like.  See getPointerToGlobal for the requirements on
232   /// destroying F and any GlobalValues it refers to.
233   ///
234   virtual void *getPointerToFunction(Function *F) = 0;
235
236   /// getPointerToFunctionOrStub - If the specified function has been
237   /// code-gen'd, return a pointer to the function.  If not, compile it, or use
238   /// a stub to implement lazy compilation if available.  See getPointerToGlobal
239   /// for the requirements on destroying F and any GlobalValues it refers to.
240   ///
241   virtual void *getPointerToFunctionOrStub(Function *F) {
242     // Default implementation, just codegen the function.
243     return getPointerToFunction(F);
244   }
245
246   // The JIT overrides a version that actually does this.
247   virtual void runJITOnFunction(Function *, MachineCodeInfo * = 0) { }
248
249   /// getGlobalValueAtAddress - Return the LLVM global value object that starts
250   /// at the specified address.
251   ///
252   const GlobalValue *getGlobalValueAtAddress(void *Addr);
253
254
255   void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr,
256                           const Type *Ty);
257   void InitializeMemory(const Constant *Init, void *Addr);
258
259   /// recompileAndRelinkFunction - This method is used to force a function
260   /// which has already been compiled to be compiled again, possibly
261   /// after it has been modified. Then the entry to the old copy is overwritten
262   /// with a branch to the new copy. If there was no old copy, this acts
263   /// just like VM::getPointerToFunction().
264   ///
265   virtual void *recompileAndRelinkFunction(Function *F) = 0;
266
267   /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
268   /// corresponding to the machine code emitted to execute this function, useful
269   /// for garbage-collecting generated code.
270   ///
271   virtual void freeMachineCodeForFunction(Function *F) = 0;
272
273   /// getOrEmitGlobalVariable - Return the address of the specified global
274   /// variable, possibly emitting it to memory if needed.  This is used by the
275   /// Emitter.  See getPointerToGlobal for the requirements on destroying GV and
276   /// any GlobalValues it refers to.
277   virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
278     return getPointerToGlobal((GlobalValue*)GV);
279   }
280
281   /// Registers a listener to be called back on various events within
282   /// the JIT.  See JITEventListener.h for more details.  Does not
283   /// take ownership of the argument.  The argument may be NULL, in
284   /// which case these functions do nothing.
285   virtual void RegisterJITEventListener(JITEventListener *) {}
286   virtual void UnregisterJITEventListener(JITEventListener *) {}
287
288   /// DisableLazyCompilation - If called, the JIT will abort if lazy compilation
289   /// is ever attempted.
290   void DisableLazyCompilation(bool Disabled = true) {
291     LazyCompilationDisabled = Disabled;
292   }
293   bool isLazyCompilationDisabled() const {
294     return LazyCompilationDisabled;
295   }
296
297   /// DisableGVCompilation - If called, the JIT will abort if it's asked to
298   /// allocate space and populate a GlobalVariable that is not internal to
299   /// the module.
300   void DisableGVCompilation(bool Disabled = true) {
301     GVCompilationDisabled = Disabled;
302   }
303   bool isGVCompilationDisabled() const {
304     return GVCompilationDisabled;
305   }
306
307   /// DisableSymbolSearching - If called, the JIT will not try to lookup unknown
308   /// symbols with dlsym.  A client can still use InstallLazyFunctionCreator to
309   /// resolve symbols in a custom way.
310   void DisableSymbolSearching(bool Disabled = true) {
311     SymbolSearchingDisabled = Disabled;
312   }
313   bool isSymbolSearchingDisabled() const {
314     return SymbolSearchingDisabled;
315   }
316   
317   /// EnableDlsymStubs - 
318   void EnableDlsymStubs(bool Enabled = true) {
319     DlsymStubsEnabled = Enabled;
320   }
321   bool areDlsymStubsEnabled() const {
322     return DlsymStubsEnabled;
323   }
324   
325   /// InstallLazyFunctionCreator - If an unknown function is needed, the
326   /// specified function pointer is invoked to create it.  If it returns null,
327   /// the JIT will abort.
328   void InstallLazyFunctionCreator(void* (*P)(const std::string &)) {
329     LazyFunctionCreator = P;
330   }
331   
332   /// InstallExceptionTableRegister - The JIT will use the given function
333   /// to register the exception tables it generates.
334   static void InstallExceptionTableRegister(void (*F)(void*)) {
335     ExceptionTableRegister = F;
336   }
337   
338   /// RegisterTable - Registers the given pointer as an exception table. It uses
339   /// the ExceptionTableRegister function.
340   static void RegisterTable(void* res) {
341     if (ExceptionTableRegister)
342       ExceptionTableRegister(res);
343   }
344
345 protected:
346   explicit ExecutionEngine(ModuleProvider *P);
347
348   void emitGlobals();
349
350   // EmitGlobalVariable - This method emits the specified global variable to the
351   // address specified in GlobalAddresses, or allocates new memory if it's not
352   // already in the map.
353   void EmitGlobalVariable(const GlobalVariable *GV);
354
355   GenericValue getConstantValue(const Constant *C);
356   void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr, 
357                            const Type *Ty);
358 };
359
360 } // End llvm namespace
361
362 #endif