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 /// runFunctionAsMain - This is a helper function which wraps runFunction to
99 /// handle the common task of starting up main with the specified argc, argv,
100 /// and envp parameters.
101 int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
102 const char * const * envp);
105 void addGlobalMapping(const GlobalValue *GV, void *Addr) {
106 MutexGuard locked(lock);
108 void *&CurVal = state.getGlobalAddressMap(locked)[GV];
109 assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
112 // If we are using the reverse mapping, add it too
113 if (!state.getGlobalAddressReverseMap(locked).empty()) {
114 const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr];
115 assert((V == 0 || GV == 0) && "GlobalMapping already established!");
120 /// clearAllGlobalMappings - Clear all global mappings and start over again
121 /// use in dynamic compilation scenarios when you want to move globals
122 void clearAllGlobalMappings() {
123 MutexGuard locked(lock);
125 state.getGlobalAddressMap(locked).clear();
126 state.getGlobalAddressReverseMap(locked).clear();
129 /// updateGlobalMapping - Replace an existing mapping for GV with a new
130 /// address. This updates both maps as required.
131 void updateGlobalMapping(const GlobalValue *GV, void *Addr) {
132 MutexGuard locked(lock);
134 void *&CurVal = state.getGlobalAddressMap(locked)[GV];
135 if (CurVal && !state.getGlobalAddressReverseMap(locked).empty())
136 state.getGlobalAddressReverseMap(locked).erase(CurVal);
139 // If we are using the reverse mapping, add it too
140 if (!state.getGlobalAddressReverseMap(locked).empty()) {
141 const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr];
142 assert((V == 0 || GV == 0) && "GlobalMapping already established!");
147 /// getPointerToGlobalIfAvailable - This returns the address of the specified
148 /// global value if it is available, otherwise it returns null.
150 void *getPointerToGlobalIfAvailable(const GlobalValue *GV) {
151 MutexGuard locked(lock);
153 std::map<const GlobalValue*, void*>::iterator I = state.getGlobalAddressMap(locked).find(GV);
154 return I != state.getGlobalAddressMap(locked).end() ? I->second : 0;
157 /// getPointerToGlobal - This returns the address of the specified global
158 /// value. This may involve code generation if it's a function.
160 void *getPointerToGlobal(const GlobalValue *GV);
162 /// getPointerToFunction - The different EE's represent function bodies in
163 /// different ways. They should each implement this to say what a function
164 /// pointer should look like.
166 virtual void *getPointerToFunction(Function *F) = 0;
168 /// getPointerToFunctionOrStub - If the specified function has been
169 /// code-gen'd, return a pointer to the function. If not, compile it, or use
170 /// a stub to implement lazy compilation if available.
172 virtual void *getPointerToFunctionOrStub(Function *F) {
173 // Default implementation, just codegen the function.
174 return getPointerToFunction(F);
177 /// getGlobalValueAtAddress - Return the LLVM global value object that starts
178 /// at the specified address.
180 const GlobalValue *getGlobalValueAtAddress(void *Addr);
183 void StoreValueToMemory(GenericValue Val, GenericValue *Ptr, const Type *Ty);
184 void InitializeMemory(const Constant *Init, void *Addr);
186 /// recompileAndRelinkFunction - This method is used to force a function
187 /// which has already been compiled to be compiled again, possibly
188 /// after it has been modified. Then the entry to the old copy is overwritten
189 /// with a branch to the new copy. If there was no old copy, this acts
190 /// just like VM::getPointerToFunction().
192 virtual void *recompileAndRelinkFunction(Function *F) = 0;
194 /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
195 /// corresponding to the machine code emitted to execute this function, useful
196 /// for garbage-collecting generated code.
198 virtual void freeMachineCodeForFunction(Function *F) = 0;
200 /// getOrEmitGlobalVariable - Return the address of the specified global
201 /// variable, possibly emitting it to memory if needed. This is used by the
203 virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
204 return getPointerToGlobal((GlobalValue*)GV);
210 // EmitGlobalVariable - This method emits the specified global variable to the
211 // address specified in GlobalAddresses, or allocates new memory if it's not
212 // already in the map.
213 void EmitGlobalVariable(const GlobalVariable *GV);
215 GenericValue getConstantValue(const Constant *C);
216 GenericValue LoadValueFromMemory(GenericValue *Ptr, const Type *Ty);
219 } // End llvm namespace