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