remove the intrinsiclowering hook
[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
36 class ExecutionEngineState {
37 private:
38   /// GlobalAddressMap - A mapping between LLVM global values and their
39   /// actualized version...
40   std::map<const GlobalValue*, void *> GlobalAddressMap;
41
42   /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap,
43   /// used to convert raw addresses into the LLVM global value that is emitted
44   /// at the address.  This map is not computed unless getGlobalValueAtAddress
45   /// is called at some point.
46   std::map<void *, const GlobalValue*> GlobalAddressReverseMap;
47
48 public:
49   std::map<const GlobalValue*, void *> &
50   getGlobalAddressMap(const MutexGuard &locked) {
51     return GlobalAddressMap;
52   }
53
54   std::map<void*, const GlobalValue*> & 
55   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   // To avoid having libexecutionengine depend on the JIT and interpreter
75   // libraries, the JIT and Interpreter set these functions to ctor pointers
76   // at startup time if they are linked in.
77   typedef ExecutionEngine *(*EECtorFn)(ModuleProvider*);
78   static EECtorFn JITCtor, InterpCtor;
79     
80 public:
81   /// lock - This lock is protects the ExecutionEngine, JIT, JITResolver and
82   /// JITEmitter classes.  It must be held while changing the internal state of
83   /// any of those classes.
84   sys::Mutex lock; // Used to make this class and subclasses thread-safe
85
86   ExecutionEngine(ModuleProvider *P);
87   ExecutionEngine(Module *M);
88   virtual ~ExecutionEngine();
89
90   Module &getModule() const { return CurMod; }
91   const TargetData &getTargetData() const { return *TD; }
92
93   /// create - This is the factory method for creating an execution engine which
94   /// is appropriate for the current machine.
95   static ExecutionEngine *create(ModuleProvider *MP,
96                                  bool ForceInterpreter = false);
97
98   /// runFunction - Execute the specified function with the specified arguments,
99   /// and return the result.
100   ///
101   virtual GenericValue runFunction(Function *F,
102                                 const std::vector<GenericValue> &ArgValues) = 0;
103
104   /// runStaticConstructorsDestructors - This method is used to execute all of
105   /// the static constructors or destructors for a module, depending on the
106   /// value of isDtors.
107   void runStaticConstructorsDestructors(bool isDtors);
108   
109   
110   /// runFunctionAsMain - This is a helper function which wraps runFunction to
111   /// handle the common task of starting up main with the specified argc, argv,
112   /// and envp parameters.
113   int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
114                         const char * const * envp);
115
116
117   void addGlobalMapping(const GlobalValue *GV, void *Addr) {
118     MutexGuard locked(lock);
119
120     void *&CurVal = state.getGlobalAddressMap(locked)[GV];
121     assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
122     CurVal = Addr;
123
124     // If we are using the reverse mapping, add it too
125     if (!state.getGlobalAddressReverseMap(locked).empty()) {
126       const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr];
127       assert((V == 0 || GV == 0) && "GlobalMapping already established!");
128       V = GV;
129     }
130   }
131
132   /// clearAllGlobalMappings - Clear all global mappings and start over again
133   /// use in dynamic compilation scenarios when you want to move globals
134   void clearAllGlobalMappings() {
135     MutexGuard locked(lock);
136
137     state.getGlobalAddressMap(locked).clear();
138     state.getGlobalAddressReverseMap(locked).clear();
139   }
140
141   /// updateGlobalMapping - Replace an existing mapping for GV with a new
142   /// address.  This updates both maps as required.
143   void updateGlobalMapping(const GlobalValue *GV, void *Addr) {
144     MutexGuard locked(lock);
145
146     void *&CurVal = state.getGlobalAddressMap(locked)[GV];
147     if (CurVal && !state.getGlobalAddressReverseMap(locked).empty())
148       state.getGlobalAddressReverseMap(locked).erase(CurVal);
149     CurVal = Addr;
150
151     // If we are using the reverse mapping, add it too
152     if (!state.getGlobalAddressReverseMap(locked).empty()) {
153       const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr];
154       assert((V == 0 || GV == 0) && "GlobalMapping already established!");
155       V = GV;
156     }
157   }
158
159   /// getPointerToGlobalIfAvailable - This returns the address of the specified
160   /// global value if it is available, otherwise it returns null.
161   ///
162   void *getPointerToGlobalIfAvailable(const GlobalValue *GV) {
163     MutexGuard locked(lock);
164
165     std::map<const GlobalValue*, void*>::iterator I =
166       state.getGlobalAddressMap(locked).find(GV);
167     return I != state.getGlobalAddressMap(locked).end() ? I->second : 0;
168   }
169
170   /// getPointerToGlobal - This returns the address of the specified global
171   /// value.  This may involve code generation if it's a function.
172   ///
173   void *getPointerToGlobal(const GlobalValue *GV);
174
175   /// getPointerToFunction - The different EE's represent function bodies in
176   /// different ways.  They should each implement this to say what a function
177   /// pointer should look like.
178   ///
179   virtual void *getPointerToFunction(Function *F) = 0;
180
181   /// getPointerToFunctionOrStub - If the specified function has been
182   /// code-gen'd, return a pointer to the function.  If not, compile it, or use
183   /// a stub to implement lazy compilation if available.
184   ///
185   virtual void *getPointerToFunctionOrStub(Function *F) {
186     // Default implementation, just codegen the function.
187     return getPointerToFunction(F);
188   }
189
190   /// getGlobalValueAtAddress - Return the LLVM global value object that starts
191   /// at the specified address.
192   ///
193   const GlobalValue *getGlobalValueAtAddress(void *Addr);
194
195
196   void StoreValueToMemory(GenericValue Val, GenericValue *Ptr, const Type *Ty);
197   void InitializeMemory(const Constant *Init, void *Addr);
198
199   /// recompileAndRelinkFunction - This method is used to force a function
200   /// which has already been compiled to be compiled again, possibly
201   /// after it has been modified. Then the entry to the old copy is overwritten
202   /// with a branch to the new copy. If there was no old copy, this acts
203   /// just like VM::getPointerToFunction().
204   ///
205   virtual void *recompileAndRelinkFunction(Function *F) = 0;
206
207   /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
208   /// corresponding to the machine code emitted to execute this function, useful
209   /// for garbage-collecting generated code.
210   ///
211   virtual void freeMachineCodeForFunction(Function *F) = 0;
212
213   /// getOrEmitGlobalVariable - Return the address of the specified global
214   /// variable, possibly emitting it to memory if needed.  This is used by the
215   /// Emitter.
216   virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
217     return getPointerToGlobal((GlobalValue*)GV);
218   }
219
220 protected:
221   void emitGlobals();
222
223   // EmitGlobalVariable - This method emits the specified global variable to the
224   // address specified in GlobalAddresses, or allocates new memory if it's not
225   // already in the map.
226   void EmitGlobalVariable(const GlobalVariable *GV);
227
228   GenericValue getConstantValue(const Constant *C);
229   GenericValue LoadValueFromMemory(GenericValue *Ptr, const Type *Ty);
230 };
231
232 } // End llvm namespace
233
234 #endif