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