Add a convenience method for creating EE's.
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 EXECUTION_ENGINE_H
16 #define 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
38 class ExecutionEngineState {
39 private:
40   /// GlobalAddressMap - A mapping between LLVM global values and their
41   /// actualized version...
42   std::map<const GlobalValue*, void *> GlobalAddressMap;
43
44   /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap,
45   /// used to convert raw addresses into the LLVM global value that is emitted
46   /// at the address.  This map is not computed unless getGlobalValueAtAddress
47   /// is called at some point.
48   std::map<void *, const GlobalValue*> GlobalAddressReverseMap;
49
50 public:
51   std::map<const GlobalValue*, void *> &
52   getGlobalAddressMap(const MutexGuard &locked) {
53     return GlobalAddressMap;
54   }
55
56   std::map<void*, const GlobalValue*> & 
57   getGlobalAddressReverseMap(const MutexGuard& locked) {
58     return GlobalAddressReverseMap;
59   }
60 };
61
62
63 class ExecutionEngine {
64   const TargetData *TD;
65   ExecutionEngineState state;
66   bool LazyCompilationDisabled;
67 protected:
68   /// Modules - This is a list of ModuleProvider's that we are JIT'ing from.  We
69   /// use a smallvector to optimize for the case where there is only one module.
70   SmallVector<ModuleProvider*, 1> Modules;
71   
72   void setTargetData(const TargetData *td) {
73     TD = td;
74   }
75
76   // To avoid having libexecutionengine depend on the JIT and interpreter
77   // libraries, the JIT and Interpreter set these functions to ctor pointers
78   // at startup time if they are linked in.
79   typedef ExecutionEngine *(*EECtorFn)(ModuleProvider*, std::string*);
80   static EECtorFn JITCtor, InterpCtor;
81     
82 public:
83   /// lock - This lock is protects the ExecutionEngine, JIT, JITResolver and
84   /// JITEmitter classes.  It must be held while changing the internal state of
85   /// any of those classes.
86   sys::Mutex lock; // Used to make this class and subclasses thread-safe
87
88   ExecutionEngine(ModuleProvider *P);
89   ExecutionEngine(Module *M);
90   virtual ~ExecutionEngine();
91
92   const TargetData *getTargetData() const { return TD; }
93
94   /// addModuleProvider - Add a ModuleProvider to the list of modules that we
95   /// can JIT from.  Note that this takes ownership of the ModuleProvider: when
96   /// the ExecutionEngine is destroyed, it destroys the MP as well.
97   void addModuleProvider(ModuleProvider *P) {
98     Modules.push_back(P);
99   }
100
101   /// removeModuleProvider - Remove a ModuleProvider from the list of modules.
102   /// Release module from ModuleProvider.
103   Module* removeModuleProvider(ModuleProvider *P, std::string *ErrInfo = 0);
104
105   /// FindFunctionNamed - Search all of the active modules to find the one that
106   /// defines FnName.  This is very slow operation and shouldn't be used for
107   /// general code.
108   Function *FindFunctionNamed(const char *FnName);
109   
110   /// create - This is the factory method for creating an execution engine which
111   /// is appropriate for the current machine.  This takes ownership of the
112   /// module provider.
113   static ExecutionEngine *create(ModuleProvider *MP,
114                                  bool ForceInterpreter = false,
115                                  std::string *ErrorStr = 0);
116
117   /// create - This is the factory method for creating an execution engine which
118   /// is appropriate for the current machine.  This takes ownership of the
119   /// module.
120   static ExecutionEngine *create(Module *M);
121   
122   /// runFunction - Execute the specified function with the specified arguments,
123   /// and return the result.
124   ///
125   virtual GenericValue runFunction(Function *F,
126                                 const std::vector<GenericValue> &ArgValues) = 0;
127
128   /// runStaticConstructorsDestructors - This method is used to execute all of
129   /// the static constructors or destructors for a module, depending on the
130   /// value of isDtors.
131   void runStaticConstructorsDestructors(bool isDtors);
132   
133   
134   /// runFunctionAsMain - This is a helper function which wraps runFunction to
135   /// handle the common task of starting up main with the specified argc, argv,
136   /// and envp parameters.
137   int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
138                         const char * const * envp);
139
140
141   /// addGlobalMapping - Tell the execution engine that the specified global is
142   /// at the specified location.  This is used internally as functions are JIT'd
143   /// and as global variables are laid out in memory.  It can and should also be
144   /// used by clients of the EE that want to have an LLVM global overlay
145   /// existing data in memory.
146   void addGlobalMapping(const GlobalValue *GV, void *Addr);
147   
148   /// clearAllGlobalMappings - Clear all global mappings and start over again
149   /// use in dynamic compilation scenarios when you want to move globals
150   void clearAllGlobalMappings();
151   
152   /// updateGlobalMapping - Replace an existing mapping for GV with a new
153   /// address.  This updates both maps as required.  If "Addr" is null, the
154   /// entry for the global is removed from the mappings.
155   void updateGlobalMapping(const GlobalValue *GV, void *Addr);
156   
157   /// getPointerToGlobalIfAvailable - This returns the address of the specified
158   /// global value if it is has already been codegen'd, otherwise it returns
159   /// null.
160   ///
161   void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
162
163   /// getPointerToGlobal - This returns the address of the specified global
164   /// value.  This may involve code generation if it's a function.
165   ///
166   void *getPointerToGlobal(const GlobalValue *GV);
167
168   /// getPointerToFunction - The different EE's represent function bodies in
169   /// different ways.  They should each implement this to say what a function
170   /// pointer should look like.
171   ///
172   virtual void *getPointerToFunction(Function *F) = 0;
173
174   /// getPointerToFunctionOrStub - If the specified function has been
175   /// code-gen'd, return a pointer to the function.  If not, compile it, or use
176   /// a stub to implement lazy compilation if available.
177   ///
178   virtual void *getPointerToFunctionOrStub(Function *F) {
179     // Default implementation, just codegen the function.
180     return getPointerToFunction(F);
181   }
182
183   /// getGlobalValueAtAddress - Return the LLVM global value object that starts
184   /// at the specified address.
185   ///
186   const GlobalValue *getGlobalValueAtAddress(void *Addr);
187
188
189   void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr, const Type *Ty);
190   void InitializeMemory(const Constant *Init, void *Addr);
191
192   /// recompileAndRelinkFunction - This method is used to force a function
193   /// which has already been compiled to be compiled again, possibly
194   /// after it has been modified. Then the entry to the old copy is overwritten
195   /// with a branch to the new copy. If there was no old copy, this acts
196   /// just like VM::getPointerToFunction().
197   ///
198   virtual void *recompileAndRelinkFunction(Function *F) = 0;
199
200   /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
201   /// corresponding to the machine code emitted to execute this function, useful
202   /// for garbage-collecting generated code.
203   ///
204   virtual void freeMachineCodeForFunction(Function *F) = 0;
205
206   /// getOrEmitGlobalVariable - Return the address of the specified global
207   /// variable, possibly emitting it to memory if needed.  This is used by the
208   /// Emitter.
209   virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
210     return getPointerToGlobal((GlobalValue*)GV);
211   }
212   
213   /// DisableLazyCompilation - If called, the JIT will abort if lazy compilation
214   // is ever attempted.
215   void DisableLazyCompilation() {
216     LazyCompilationDisabled = true;
217   }
218   bool isLazyCompilationDisabled() const {
219     return LazyCompilationDisabled;
220   }
221
222 protected:
223   void emitGlobals();
224
225   // EmitGlobalVariable - This method emits the specified global variable to the
226   // address specified in GlobalAddresses, or allocates new memory if it's not
227   // already in the map.
228   void EmitGlobalVariable(const GlobalVariable *GV);
229
230   GenericValue getConstantValue(const Constant *C);
231   void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr, 
232                            const Type *Ty);
233 };
234
235 } // End llvm namespace
236
237 #endif