1 //===- ExecutionEngine.h - Abstract Execution Engine Interface --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file defines the abstract interface that implements execution support
13 //===----------------------------------------------------------------------===//
15 #ifndef EXECUTION_ENGINE_H
16 #define EXECUTION_ENGINE_H
22 #include "llvm/Support/MutexGuard.h"
35 class IntrinsicLowering;
38 class ExecutionEngineState {
40 /// GlobalAddressMap - A mapping between LLVM global values and their
41 /// actualized version...
42 std::map<const GlobalValue*, void *> GlobalAddressMap;
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;
51 std::map<const GlobalValue*, void *> &
52 getGlobalAddressMap(const MutexGuard &locked) {
53 return GlobalAddressMap;
56 std::map<void*, const GlobalValue*> &
57 getGlobalAddressReverseMap(const MutexGuard& locked) {
58 return GlobalAddressReverseMap;
63 class ExecutionEngine {
67 ExecutionEngineState state;
72 void setTargetData(const TargetData &td) {
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;
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
88 ExecutionEngine(ModuleProvider *P);
89 ExecutionEngine(Module *M);
90 virtual ~ExecutionEngine();
92 Module &getModule() const { return CurMod; }
93 const TargetData &getTargetData() const { return *TD; }
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);
101 /// runFunction - Execute the specified function with the specified arguments,
102 /// and return the result.
104 virtual GenericValue runFunction(Function *F,
105 const std::vector<GenericValue> &ArgValues) = 0;
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);
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);
120 void addGlobalMapping(const GlobalValue *GV, void *Addr) {
121 MutexGuard locked(lock);
123 void *&CurVal = state.getGlobalAddressMap(locked)[GV];
124 assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
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!");
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);
140 state.getGlobalAddressMap(locked).clear();
141 state.getGlobalAddressReverseMap(locked).clear();
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);
149 void *&CurVal = state.getGlobalAddressMap(locked)[GV];
150 if (CurVal && !state.getGlobalAddressReverseMap(locked).empty())
151 state.getGlobalAddressReverseMap(locked).erase(CurVal);
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!");
162 /// getPointerToGlobalIfAvailable - This returns the address of the specified
163 /// global value if it is available, otherwise it returns null.
165 void *getPointerToGlobalIfAvailable(const GlobalValue *GV) {
166 MutexGuard locked(lock);
168 std::map<const GlobalValue*, void*>::iterator I =
169 state.getGlobalAddressMap(locked).find(GV);
170 return I != state.getGlobalAddressMap(locked).end() ? I->second : 0;
173 /// getPointerToGlobal - This returns the address of the specified global
174 /// value. This may involve code generation if it's a function.
176 void *getPointerToGlobal(const GlobalValue *GV);
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.
182 virtual void *getPointerToFunction(Function *F) = 0;
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.
188 virtual void *getPointerToFunctionOrStub(Function *F) {
189 // Default implementation, just codegen the function.
190 return getPointerToFunction(F);
193 /// getGlobalValueAtAddress - Return the LLVM global value object that starts
194 /// at the specified address.
196 const GlobalValue *getGlobalValueAtAddress(void *Addr);
199 void StoreValueToMemory(GenericValue Val, GenericValue *Ptr, const Type *Ty);
200 void InitializeMemory(const Constant *Init, void *Addr);
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().
208 virtual void *recompileAndRelinkFunction(Function *F) = 0;
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.
214 virtual void freeMachineCodeForFunction(Function *F) = 0;
216 /// getOrEmitGlobalVariable - Return the address of the specified global
217 /// variable, possibly emitting it to memory if needed. This is used by the
219 virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
220 return getPointerToGlobal((GlobalValue*)GV);
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);
231 GenericValue getConstantValue(const Constant *C);
232 GenericValue LoadValueFromMemory(GenericValue *Ptr, const Type *Ty);
235 } // End llvm namespace