add a way to have multiple modules in a JIT :)
[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/System/Mutex.h"
23 #include "llvm/ADT/SmallVector.h"
24
25 namespace llvm {
26
27 union 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
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   const TargetData *TD;
65   ExecutionEngineState state;
66 protected:
67   /// Modules - This is a list of ModuleProvider's that we are JIT'ing from.  We
68   /// use a smallvector to optimize for the case where there is only one module.
69   SmallVector<ModuleProvider*, 1> Modules;
70   
71   void setTargetData(const TargetData *td) {
72     TD = td;
73   }
74
75   // To avoid having libexecutionengine depend on the JIT and interpreter
76   // libraries, the JIT and Interpreter set these functions to ctor pointers
77   // at startup time if they are linked in.
78   typedef ExecutionEngine *(*EECtorFn)(ModuleProvider*);
79   static EECtorFn JITCtor, InterpCtor;
80     
81 public:
82   /// lock - This lock is protects the ExecutionEngine, JIT, JITResolver and
83   /// JITEmitter classes.  It must be held while changing the internal state of
84   /// any of those classes.
85   sys::Mutex lock; // Used to make this class and subclasses thread-safe
86
87   ExecutionEngine(ModuleProvider *P);
88   ExecutionEngine(Module *M);
89   virtual ~ExecutionEngine();
90
91   const TargetData *getTargetData() const { return TD; }
92
93   /// addModuleProvider - Add a ModuleProvider to the list of modules that we
94   /// can JIT from.  Note that this takes ownership of the ModuleProvider: when
95   /// the ExecutionEngine is destroyed, it destroys the MP as well.
96   void addModuleProvider(ModuleProvider *P) {
97     Modules.push_back(P);
98   }
99   
100   /// FindFunctionNamed - Search all of the active modules to find the one that
101   /// defines FnName.  This is very slow operation and shouldn't be used for
102   /// general code.
103   Function *FindFunctionNamed(const char *FnName);
104   
105   /// create - This is the factory method for creating an execution engine which
106   /// is appropriate for the current machine.
107   static ExecutionEngine *create(ModuleProvider *MP,
108                                  bool ForceInterpreter = false);
109
110   /// runFunction - Execute the specified function with the specified arguments,
111   /// and return the result.
112   ///
113   virtual GenericValue runFunction(Function *F,
114                                 const std::vector<GenericValue> &ArgValues) = 0;
115
116   /// runStaticConstructorsDestructors - This method is used to execute all of
117   /// the static constructors or destructors for a module, depending on the
118   /// value of isDtors.
119   void runStaticConstructorsDestructors(bool isDtors);
120   
121   
122   /// runFunctionAsMain - This is a helper function which wraps runFunction to
123   /// handle the common task of starting up main with the specified argc, argv,
124   /// and envp parameters.
125   int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
126                         const char * const * envp);
127
128
129   /// addGlobalMapping - Tell the execution engine that the specified global is
130   /// at the specified location.  This is used internally as functions are JIT'd
131   /// and as global variables are laid out in memory.  It can and should also be
132   /// used by clients of the EE that want to have an LLVM global overlay
133   /// existing data in memory.
134   void addGlobalMapping(const GlobalValue *GV, void *Addr);
135   
136   /// clearAllGlobalMappings - Clear all global mappings and start over again
137   /// use in dynamic compilation scenarios when you want to move globals
138   void clearAllGlobalMappings();
139   
140   /// updateGlobalMapping - Replace an existing mapping for GV with a new
141   /// address.  This updates both maps as required.  If "Addr" is null, the
142   /// entry for the global is removed from the mappings.
143   void updateGlobalMapping(const GlobalValue *GV, void *Addr);
144   
145   /// getPointerToGlobalIfAvailable - This returns the address of the specified
146   /// global value if it is has already been codegen'd, otherwise it returns
147   /// null.
148   ///
149   void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
150
151   /// getPointerToGlobal - This returns the address of the specified global
152   /// value.  This may involve code generation if it's a function.
153   ///
154   void *getPointerToGlobal(const GlobalValue *GV);
155
156   /// getPointerToFunction - The different EE's represent function bodies in
157   /// different ways.  They should each implement this to say what a function
158   /// pointer should look like.
159   ///
160   virtual void *getPointerToFunction(Function *F) = 0;
161
162   /// getPointerToFunctionOrStub - If the specified function has been
163   /// code-gen'd, return a pointer to the function.  If not, compile it, or use
164   /// a stub to implement lazy compilation if available.
165   ///
166   virtual void *getPointerToFunctionOrStub(Function *F) {
167     // Default implementation, just codegen the function.
168     return getPointerToFunction(F);
169   }
170
171   /// getGlobalValueAtAddress - Return the LLVM global value object that starts
172   /// at the specified address.
173   ///
174   const GlobalValue *getGlobalValueAtAddress(void *Addr);
175
176
177   void StoreValueToMemory(GenericValue Val, GenericValue *Ptr, const Type *Ty);
178   void InitializeMemory(const Constant *Init, void *Addr);
179
180   /// recompileAndRelinkFunction - This method is used to force a function
181   /// which has already been compiled to be compiled again, possibly
182   /// after it has been modified. Then the entry to the old copy is overwritten
183   /// with a branch to the new copy. If there was no old copy, this acts
184   /// just like VM::getPointerToFunction().
185   ///
186   virtual void *recompileAndRelinkFunction(Function *F) = 0;
187
188   /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
189   /// corresponding to the machine code emitted to execute this function, useful
190   /// for garbage-collecting generated code.
191   ///
192   virtual void freeMachineCodeForFunction(Function *F) = 0;
193
194   /// getOrEmitGlobalVariable - Return the address of the specified global
195   /// variable, possibly emitting it to memory if needed.  This is used by the
196   /// Emitter.
197   virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
198     return getPointerToGlobal((GlobalValue*)GV);
199   }
200
201 protected:
202   void emitGlobals();
203
204   // EmitGlobalVariable - This method emits the specified global variable to the
205   // address specified in GlobalAddresses, or allocates new memory if it's not
206   // already in the map.
207   void EmitGlobalVariable(const GlobalVariable *GV);
208
209   GenericValue getConstantValue(const Constant *C);
210   GenericValue LoadValueFromMemory(GenericValue *Ptr, const Type *Ty);
211 };
212
213 } // End llvm namespace
214
215 #endif