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 *>& getGlobalAddressMap(const MutexGuard& locked) {
52 return GlobalAddressMap;
55 std::map<void *, const GlobalValue*>& getGlobalAddressReverseMap(const MutexGuard& locked) {
56 return GlobalAddressReverseMap;
61 class ExecutionEngine {
65 ExecutionEngineState state;
70 void setTargetData(const TargetData &td) {
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
79 ExecutionEngine(ModuleProvider *P);
80 ExecutionEngine(Module *M);
81 virtual ~ExecutionEngine();
83 Module &getModule() const { return CurMod; }
84 const TargetData &getTargetData() const { return *TD; }
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);
92 /// runFunction - Execute the specified function with the specified arguments,
93 /// and return the result.
95 virtual GenericValue runFunction(Function *F,
96 const std::vector<GenericValue> &ArgValues) = 0;
98 /// runStaticConstructorsDestructors - This method is used to execute all of
99 /// the static constructors or destructors for a module, depending on the
100 /// value of isDtors.
101 void runStaticConstructorsDestructors(bool isDtors);
104 /// runFunctionAsMain - This is a helper function which wraps runFunction to
105 /// handle the common task of starting up main with the specified argc, argv,
106 /// and envp parameters.
107 int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
108 const char * const * envp);
111 void addGlobalMapping(const GlobalValue *GV, void *Addr) {
112 MutexGuard locked(lock);
114 void *&CurVal = state.getGlobalAddressMap(locked)[GV];
115 assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
118 // If we are using the reverse mapping, add it too
119 if (!state.getGlobalAddressReverseMap(locked).empty()) {
120 const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr];
121 assert((V == 0 || GV == 0) && "GlobalMapping already established!");
126 /// clearAllGlobalMappings - Clear all global mappings and start over again
127 /// use in dynamic compilation scenarios when you want to move globals
128 void clearAllGlobalMappings() {
129 MutexGuard locked(lock);
131 state.getGlobalAddressMap(locked).clear();
132 state.getGlobalAddressReverseMap(locked).clear();
135 /// updateGlobalMapping - Replace an existing mapping for GV with a new
136 /// address. This updates both maps as required.
137 void updateGlobalMapping(const GlobalValue *GV, void *Addr) {
138 MutexGuard locked(lock);
140 void *&CurVal = state.getGlobalAddressMap(locked)[GV];
141 if (CurVal && !state.getGlobalAddressReverseMap(locked).empty())
142 state.getGlobalAddressReverseMap(locked).erase(CurVal);
145 // If we are using the reverse mapping, add it too
146 if (!state.getGlobalAddressReverseMap(locked).empty()) {
147 const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr];
148 assert((V == 0 || GV == 0) && "GlobalMapping already established!");
153 /// getPointerToGlobalIfAvailable - This returns the address of the specified
154 /// global value if it is available, otherwise it returns null.
156 void *getPointerToGlobalIfAvailable(const GlobalValue *GV) {
157 MutexGuard locked(lock);
159 std::map<const GlobalValue*, void*>::iterator I = state.getGlobalAddressMap(locked).find(GV);
160 return I != state.getGlobalAddressMap(locked).end() ? I->second : 0;
163 /// getPointerToGlobal - This returns the address of the specified global
164 /// value. This may involve code generation if it's a function.
166 void *getPointerToGlobal(const GlobalValue *GV);
168 /// getPointerToFunction - The different EE's represent function bodies in
169 /// different ways. They should each implement this to say what a function
170 /// pointer should look like.
172 virtual void *getPointerToFunction(Function *F) = 0;
174 /// getPointerToFunctionOrStub - If the specified function has been
175 /// code-gen'd, return a pointer to the function. If not, compile it, or use
176 /// a stub to implement lazy compilation if available.
178 virtual void *getPointerToFunctionOrStub(Function *F) {
179 // Default implementation, just codegen the function.
180 return getPointerToFunction(F);
183 /// getGlobalValueAtAddress - Return the LLVM global value object that starts
184 /// at the specified address.
186 const GlobalValue *getGlobalValueAtAddress(void *Addr);
189 void StoreValueToMemory(GenericValue Val, GenericValue *Ptr, const Type *Ty);
190 void InitializeMemory(const Constant *Init, void *Addr);
192 /// recompileAndRelinkFunction - This method is used to force a function
193 /// which has already been compiled to be compiled again, possibly
194 /// after it has been modified. Then the entry to the old copy is overwritten
195 /// with a branch to the new copy. If there was no old copy, this acts
196 /// just like VM::getPointerToFunction().
198 virtual void *recompileAndRelinkFunction(Function *F) = 0;
200 /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
201 /// corresponding to the machine code emitted to execute this function, useful
202 /// for garbage-collecting generated code.
204 virtual void freeMachineCodeForFunction(Function *F) = 0;
206 /// getOrEmitGlobalVariable - Return the address of the specified global
207 /// variable, possibly emitting it to memory if needed. This is used by the
209 virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
210 return getPointerToGlobal((GlobalValue*)GV);
216 // EmitGlobalVariable - This method emits the specified global variable to the
217 // address specified in GlobalAddresses, or allocates new memory if it's not
218 // already in the map.
219 void EmitGlobalVariable(const GlobalVariable *GV);
221 GenericValue getConstantValue(const Constant *C);
222 GenericValue LoadValueFromMemory(GenericValue *Ptr, const Type *Ty);
225 } // End llvm namespace