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