Add a helper method for running static ctors/dtors in the module.
[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 *>& getGlobalAddressMap(const MutexGuard& locked) {
52     return GlobalAddressMap;
53   }
54
55   std::map<void *, const GlobalValue*>& getGlobalAddressReverseMap(const MutexGuard& locked) {
56     return GlobalAddressReverseMap;
57   }
58 };
59
60
61 class ExecutionEngine {
62   Module &CurMod;
63   const TargetData *TD;
64
65   ExecutionEngineState state;
66
67 protected:
68   ModuleProvider *MP;
69
70   void setTargetData(const TargetData &td) {
71     TD = &td;
72   }
73
74 public:
75   /// lock - This lock is protects the ExecutionEngine, JIT, JITResolver and JITEmitter classes.
76   /// It must be held while changing the internal state of any of those classes.
77   sys::Mutex lock; // Used to make this class and subclasses thread-safe
78
79   ExecutionEngine(ModuleProvider *P);
80   ExecutionEngine(Module *M);
81   virtual ~ExecutionEngine();
82
83   Module &getModule() const { return CurMod; }
84   const TargetData &getTargetData() const { return *TD; }
85
86   /// create - This is the factory method for creating an execution engine which
87   /// is appropriate for the current machine.  If specified, the
88   /// IntrinsicLowering implementation should be allocated on the heap.
89   static ExecutionEngine *create(ModuleProvider *MP, bool ForceInterpreter,
90                                  IntrinsicLowering *IL = 0);
91
92   /// runFunction - Execute the specified function with the specified arguments,
93   /// and return the result.
94   ///
95   virtual GenericValue runFunction(Function *F,
96                                 const std::vector<GenericValue> &ArgValues) = 0;
97
98   /// runStaticConstructorsDestructors - This method is used to execute all of
99   /// the static constructors or destructors for a module, depending on the
100   /// value of isDtors.
101   void runStaticConstructorsDestructors(bool isDtors);
102   
103   
104   /// runFunctionAsMain - This is a helper function which wraps runFunction to
105   /// handle the common task of starting up main with the specified argc, argv,
106   /// and envp parameters.
107   int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
108                         const char * const * envp);
109
110
111   void addGlobalMapping(const GlobalValue *GV, void *Addr) {
112     MutexGuard locked(lock);
113
114     void *&CurVal = state.getGlobalAddressMap(locked)[GV];
115     assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
116     CurVal = Addr;
117
118     // If we are using the reverse mapping, add it too
119     if (!state.getGlobalAddressReverseMap(locked).empty()) {
120       const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr];
121       assert((V == 0 || GV == 0) && "GlobalMapping already established!");
122       V = GV;
123     }
124   }
125
126   /// clearAllGlobalMappings - Clear all global mappings and start over again
127   /// use in dynamic compilation scenarios when you want to move globals
128   void clearAllGlobalMappings() {
129     MutexGuard locked(lock);
130
131     state.getGlobalAddressMap(locked).clear();
132     state.getGlobalAddressReverseMap(locked).clear();
133   }
134
135   /// updateGlobalMapping - Replace an existing mapping for GV with a new
136   /// address.  This updates both maps as required.
137   void updateGlobalMapping(const GlobalValue *GV, void *Addr) {
138     MutexGuard locked(lock);
139
140     void *&CurVal = state.getGlobalAddressMap(locked)[GV];
141     if (CurVal && !state.getGlobalAddressReverseMap(locked).empty())
142       state.getGlobalAddressReverseMap(locked).erase(CurVal);
143     CurVal = Addr;
144
145     // If we are using the reverse mapping, add it too
146     if (!state.getGlobalAddressReverseMap(locked).empty()) {
147       const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr];
148       assert((V == 0 || GV == 0) && "GlobalMapping already established!");
149       V = GV;
150     }
151   }
152
153   /// getPointerToGlobalIfAvailable - This returns the address of the specified
154   /// global value if it is available, otherwise it returns null.
155   ///
156   void *getPointerToGlobalIfAvailable(const GlobalValue *GV) {
157     MutexGuard locked(lock);
158
159     std::map<const GlobalValue*, void*>::iterator I = state.getGlobalAddressMap(locked).find(GV);
160     return I != state.getGlobalAddressMap(locked).end() ? I->second : 0;
161   }
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(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 protected:
214   void emitGlobals();
215
216   // EmitGlobalVariable - This method emits the specified global variable to the
217   // address specified in GlobalAddresses, or allocates new memory if it's not
218   // already in the map.
219   void EmitGlobalVariable(const GlobalVariable *GV);
220
221   GenericValue getConstantValue(const Constant *C);
222   GenericValue LoadValueFromMemory(GenericValue *Ptr, const Type *Ty);
223 };
224
225 } // End llvm namespace
226
227 #endif