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"
36 class ExecutionEngineState {
38 /// GlobalAddressMap - A mapping between LLVM global values and their
39 /// actualized version...
40 std::map<const GlobalValue*, void *> GlobalAddressMap;
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;
49 std::map<const GlobalValue*, void *> &
50 getGlobalAddressMap(const MutexGuard &locked) {
51 return GlobalAddressMap;
54 std::map<void*, const GlobalValue*> &
55 getGlobalAddressReverseMap(const MutexGuard& locked) {
56 return GlobalAddressReverseMap;
61 class ExecutionEngine {
65 ExecutionEngineState state;
70 void setTargetData(const TargetData &td) {
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;
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
86 ExecutionEngine(ModuleProvider *P);
87 ExecutionEngine(Module *M);
88 virtual ~ExecutionEngine();
90 Module &getModule() const { return CurMod; }
91 const TargetData &getTargetData() const { return *TD; }
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);
98 /// runFunction - Execute the specified function with the specified arguments,
99 /// and return the result.
101 virtual GenericValue runFunction(Function *F,
102 const std::vector<GenericValue> &ArgValues) = 0;
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);
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);
117 void addGlobalMapping(const GlobalValue *GV, void *Addr) {
118 MutexGuard locked(lock);
120 void *&CurVal = state.getGlobalAddressMap(locked)[GV];
121 assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
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!");
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);
137 state.getGlobalAddressMap(locked).clear();
138 state.getGlobalAddressReverseMap(locked).clear();
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);
146 void *&CurVal = state.getGlobalAddressMap(locked)[GV];
147 if (CurVal && !state.getGlobalAddressReverseMap(locked).empty())
148 state.getGlobalAddressReverseMap(locked).erase(CurVal);
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!");
159 /// getPointerToGlobalIfAvailable - This returns the address of the specified
160 /// global value if it is available, otherwise it returns null.
162 void *getPointerToGlobalIfAvailable(const GlobalValue *GV) {
163 MutexGuard locked(lock);
165 std::map<const GlobalValue*, void*>::iterator I =
166 state.getGlobalAddressMap(locked).find(GV);
167 return I != state.getGlobalAddressMap(locked).end() ? I->second : 0;
170 /// getPointerToGlobal - This returns the address of the specified global
171 /// value. This may involve code generation if it's a function.
173 void *getPointerToGlobal(const GlobalValue *GV);
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.
179 virtual void *getPointerToFunction(Function *F) = 0;
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.
185 virtual void *getPointerToFunctionOrStub(Function *F) {
186 // Default implementation, just codegen the function.
187 return getPointerToFunction(F);
190 /// getGlobalValueAtAddress - Return the LLVM global value object that starts
191 /// at the specified address.
193 const GlobalValue *getGlobalValueAtAddress(void *Addr);
196 void StoreValueToMemory(GenericValue Val, GenericValue *Ptr, const Type *Ty);
197 void InitializeMemory(const Constant *Init, void *Addr);
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().
205 virtual void *recompileAndRelinkFunction(Function *F) = 0;
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.
211 virtual void freeMachineCodeForFunction(Function *F) = 0;
213 /// getOrEmitGlobalVariable - Return the address of the specified global
214 /// variable, possibly emitting it to memory if needed. This is used by the
216 virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
217 return getPointerToGlobal((GlobalValue*)GV);
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);
228 GenericValue getConstantValue(const Constant *C);
229 GenericValue LoadValueFromMemory(GenericValue *Ptr, const Type *Ty);
232 } // End llvm namespace