e3c1219d2984d44baef879251bdbe26354ef3329
[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/Support/MutexGuard.h"
23
24 namespace llvm {
25
26 union GenericValue;
27 class Constant;
28 class Function;
29 class GlobalVariable;
30 class GlobalValue;
31 class Module;
32 class ModuleProvider;
33 class TargetData;
34 class Type;
35 class IntrinsicLowering;
36
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   Module &CurMod;
65   const TargetData *TD;
66
67   ExecutionEngineState state;
68
69 protected:
70   ModuleProvider *MP;
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*, IntrinsicLowering*);
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   Module &getModule() const { return CurMod; }
93   const TargetData &getTargetData() const { return *TD; }
94
95   /// create - This is the factory method for creating an execution engine which
96   /// is appropriate for the current machine.  If specified, the
97   /// IntrinsicLowering implementation should be allocated on the heap.
98   static ExecutionEngine *create(ModuleProvider *MP, bool ForceInterpreter,
99                                  IntrinsicLowering *IL = 0);
100
101   /// runFunction - Execute the specified function with the specified arguments,
102   /// and return the result.
103   ///
104   virtual GenericValue runFunction(Function *F,
105                                 const std::vector<GenericValue> &ArgValues) = 0;
106
107   /// runStaticConstructorsDestructors - This method is used to execute all of
108   /// the static constructors or destructors for a module, depending on the
109   /// value of isDtors.
110   void runStaticConstructorsDestructors(bool isDtors);
111   
112   
113   /// runFunctionAsMain - This is a helper function which wraps runFunction to
114   /// handle the common task of starting up main with the specified argc, argv,
115   /// and envp parameters.
116   int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
117                         const char * const * envp);
118
119
120   void addGlobalMapping(const GlobalValue *GV, void *Addr) {
121     MutexGuard locked(lock);
122
123     void *&CurVal = state.getGlobalAddressMap(locked)[GV];
124     assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
125     CurVal = Addr;
126
127     // If we are using the reverse mapping, add it too
128     if (!state.getGlobalAddressReverseMap(locked).empty()) {
129       const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr];
130       assert((V == 0 || GV == 0) && "GlobalMapping already established!");
131       V = GV;
132     }
133   }
134
135   /// clearAllGlobalMappings - Clear all global mappings and start over again
136   /// use in dynamic compilation scenarios when you want to move globals
137   void clearAllGlobalMappings() {
138     MutexGuard locked(lock);
139
140     state.getGlobalAddressMap(locked).clear();
141     state.getGlobalAddressReverseMap(locked).clear();
142   }
143
144   /// updateGlobalMapping - Replace an existing mapping for GV with a new
145   /// address.  This updates both maps as required.
146   void updateGlobalMapping(const GlobalValue *GV, void *Addr) {
147     MutexGuard locked(lock);
148
149     void *&CurVal = state.getGlobalAddressMap(locked)[GV];
150     if (CurVal && !state.getGlobalAddressReverseMap(locked).empty())
151       state.getGlobalAddressReverseMap(locked).erase(CurVal);
152     CurVal = Addr;
153
154     // If we are using the reverse mapping, add it too
155     if (!state.getGlobalAddressReverseMap(locked).empty()) {
156       const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr];
157       assert((V == 0 || GV == 0) && "GlobalMapping already established!");
158       V = GV;
159     }
160   }
161
162   /// getPointerToGlobalIfAvailable - This returns the address of the specified
163   /// global value if it is available, otherwise it returns null.
164   ///
165   void *getPointerToGlobalIfAvailable(const GlobalValue *GV) {
166     MutexGuard locked(lock);
167
168     std::map<const GlobalValue*, void*>::iterator I =
169       state.getGlobalAddressMap(locked).find(GV);
170     return I != state.getGlobalAddressMap(locked).end() ? I->second : 0;
171   }
172
173   /// getPointerToGlobal - This returns the address of the specified global
174   /// value.  This may involve code generation if it's a function.
175   ///
176   void *getPointerToGlobal(const GlobalValue *GV);
177
178   /// getPointerToFunction - The different EE's represent function bodies in
179   /// different ways.  They should each implement this to say what a function
180   /// pointer should look like.
181   ///
182   virtual void *getPointerToFunction(Function *F) = 0;
183
184   /// getPointerToFunctionOrStub - If the specified function has been
185   /// code-gen'd, return a pointer to the function.  If not, compile it, or use
186   /// a stub to implement lazy compilation if available.
187   ///
188   virtual void *getPointerToFunctionOrStub(Function *F) {
189     // Default implementation, just codegen the function.
190     return getPointerToFunction(F);
191   }
192
193   /// getGlobalValueAtAddress - Return the LLVM global value object that starts
194   /// at the specified address.
195   ///
196   const GlobalValue *getGlobalValueAtAddress(void *Addr);
197
198
199   void StoreValueToMemory(GenericValue Val, GenericValue *Ptr, const Type *Ty);
200   void InitializeMemory(const Constant *Init, void *Addr);
201
202   /// recompileAndRelinkFunction - This method is used to force a function
203   /// which has already been compiled to be compiled again, possibly
204   /// after it has been modified. Then the entry to the old copy is overwritten
205   /// with a branch to the new copy. If there was no old copy, this acts
206   /// just like VM::getPointerToFunction().
207   ///
208   virtual void *recompileAndRelinkFunction(Function *F) = 0;
209
210   /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
211   /// corresponding to the machine code emitted to execute this function, useful
212   /// for garbage-collecting generated code.
213   ///
214   virtual void freeMachineCodeForFunction(Function *F) = 0;
215
216   /// getOrEmitGlobalVariable - Return the address of the specified global
217   /// variable, possibly emitting it to memory if needed.  This is used by the
218   /// Emitter.
219   virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
220     return getPointerToGlobal((GlobalValue*)GV);
221   }
222
223 protected:
224   void emitGlobals();
225
226   // EmitGlobalVariable - This method emits the specified global variable to the
227   // address specified in GlobalAddresses, or allocates new memory if it's not
228   // already in the map.
229   void EmitGlobalVariable(const GlobalVariable *GV);
230
231   GenericValue getConstantValue(const Constant *C);
232   GenericValue LoadValueFromMemory(GenericValue *Ptr, const Type *Ty);
233 };
234
235 } // End llvm namespace
236
237 #endif