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 LLVM_EXECUTION_ENGINE_H
16 #define LLVM_EXECUTION_ENGINE_H
22 #include "llvm/System/Mutex.h"
23 #include "llvm/ADT/SmallVector.h"
37 class JITMemoryManager;
39 class ExecutionEngineState {
41 /// GlobalAddressMap - A mapping between LLVM global values and their
42 /// actualized version...
43 std::map<const GlobalValue*, void *> GlobalAddressMap;
45 /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap,
46 /// used to convert raw addresses into the LLVM global value that is emitted
47 /// at the address. This map is not computed unless getGlobalValueAtAddress
48 /// is called at some point.
49 std::map<void *, const GlobalValue*> GlobalAddressReverseMap;
52 std::map<const GlobalValue*, void *> &
53 getGlobalAddressMap(const MutexGuard &locked) {
54 return GlobalAddressMap;
57 std::map<void*, const GlobalValue*> &
58 getGlobalAddressReverseMap(const MutexGuard& locked) {
59 return GlobalAddressReverseMap;
64 class ExecutionEngine {
66 ExecutionEngineState state;
67 bool LazyCompilationDisabled;
70 /// Modules - This is a list of ModuleProvider's that we are JIT'ing from. We
71 /// use a smallvector to optimize for the case where there is only one module.
72 SmallVector<ModuleProvider*, 1> Modules;
74 void setTargetData(const TargetData *td) {
78 // To avoid having libexecutionengine depend on the JIT and interpreter
79 // libraries, the JIT and Interpreter set these functions to ctor pointers
80 // at startup time if they are linked in.
81 typedef ExecutionEngine *(*EECtorFn)(ModuleProvider*, std::string*);
82 static EECtorFn JITCtor, InterpCtor;
84 /// LazyFunctionCreator - If an unknown function is needed, this function
85 /// pointer is invoked to create it. If this returns null, the JIT will abort.
86 void* (*LazyFunctionCreator)(const std::string &);
89 /// lock - This lock is protects the ExecutionEngine, JIT, JITResolver and
90 /// JITEmitter classes. It must be held while changing the internal state of
91 /// any of those classes.
92 sys::Mutex lock; // Used to make this class and subclasses thread-safe
94 //===----------------------------------------------------------------------===//
95 // ExecutionEngine Startup
96 //===----------------------------------------------------------------------===//
98 virtual ~ExecutionEngine();
100 /// create - This is the factory method for creating an execution engine which
101 /// is appropriate for the current machine. This takes ownership of the
103 static ExecutionEngine *create(ModuleProvider *MP,
104 bool ForceInterpreter = false,
105 std::string *ErrorStr = 0);
107 /// create - This is the factory method for creating an execution engine which
108 /// is appropriate for the current machine. This takes ownership of the
110 static ExecutionEngine *create(Module *M);
112 /// createJIT - This is the factory method for creating a JIT for the current
113 /// machine, it does not fall back to the interpreter. This takes ownership
114 /// of the ModuleProvider and JITMemoryManager if successful.
115 static ExecutionEngine *createJIT(ModuleProvider *MP,
116 std::string *ErrorStr = 0,
117 JITMemoryManager *JMM = 0);
121 /// addModuleProvider - Add a ModuleProvider to the list of modules that we
122 /// can JIT from. Note that this takes ownership of the ModuleProvider: when
123 /// the ExecutionEngine is destroyed, it destroys the MP as well.
124 void addModuleProvider(ModuleProvider *P) {
125 Modules.push_back(P);
128 //===----------------------------------------------------------------------===//
130 const TargetData *getTargetData() const { return TD; }
133 /// removeModuleProvider - Remove a ModuleProvider from the list of modules.
134 /// Release module from ModuleProvider.
135 Module* removeModuleProvider(ModuleProvider *P, std::string *ErrInfo = 0);
137 /// FindFunctionNamed - Search all of the active modules to find the one that
138 /// defines FnName. This is very slow operation and shouldn't be used for
140 Function *FindFunctionNamed(const char *FnName);
142 /// runFunction - Execute the specified function with the specified arguments,
143 /// and return the result.
145 virtual GenericValue runFunction(Function *F,
146 const std::vector<GenericValue> &ArgValues) = 0;
148 /// runStaticConstructorsDestructors - This method is used to execute all of
149 /// the static constructors or destructors for a module, depending on the
150 /// value of isDtors.
151 void runStaticConstructorsDestructors(bool isDtors);
154 /// runFunctionAsMain - This is a helper function which wraps runFunction to
155 /// handle the common task of starting up main with the specified argc, argv,
156 /// and envp parameters.
157 int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
158 const char * const * envp);
161 /// addGlobalMapping - Tell the execution engine that the specified global is
162 /// at the specified location. This is used internally as functions are JIT'd
163 /// and as global variables are laid out in memory. It can and should also be
164 /// used by clients of the EE that want to have an LLVM global overlay
165 /// existing data in memory.
166 void addGlobalMapping(const GlobalValue *GV, void *Addr);
168 /// clearAllGlobalMappings - Clear all global mappings and start over again
169 /// use in dynamic compilation scenarios when you want to move globals
170 void clearAllGlobalMappings();
172 /// updateGlobalMapping - Replace an existing mapping for GV with a new
173 /// address. This updates both maps as required. If "Addr" is null, the
174 /// entry for the global is removed from the mappings.
175 void updateGlobalMapping(const GlobalValue *GV, void *Addr);
177 /// getPointerToGlobalIfAvailable - This returns the address of the specified
178 /// global value if it is has already been codegen'd, otherwise it returns
181 void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
183 /// getPointerToGlobal - This returns the address of the specified global
184 /// value. This may involve code generation if it's a function.
186 void *getPointerToGlobal(const GlobalValue *GV);
188 /// getPointerToFunction - The different EE's represent function bodies in
189 /// different ways. They should each implement this to say what a function
190 /// pointer should look like.
192 virtual void *getPointerToFunction(Function *F) = 0;
194 /// getPointerToFunctionOrStub - If the specified function has been
195 /// code-gen'd, return a pointer to the function. If not, compile it, or use
196 /// a stub to implement lazy compilation if available.
198 virtual void *getPointerToFunctionOrStub(Function *F) {
199 // Default implementation, just codegen the function.
200 return getPointerToFunction(F);
203 /// getGlobalValueAtAddress - Return the LLVM global value object that starts
204 /// at the specified address.
206 const GlobalValue *getGlobalValueAtAddress(void *Addr);
209 void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr, const Type *Ty);
210 void InitializeMemory(const Constant *Init, void *Addr);
212 /// recompileAndRelinkFunction - This method is used to force a function
213 /// which has already been compiled to be compiled again, possibly
214 /// after it has been modified. Then the entry to the old copy is overwritten
215 /// with a branch to the new copy. If there was no old copy, this acts
216 /// just like VM::getPointerToFunction().
218 virtual void *recompileAndRelinkFunction(Function *F) = 0;
220 /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
221 /// corresponding to the machine code emitted to execute this function, useful
222 /// for garbage-collecting generated code.
224 virtual void freeMachineCodeForFunction(Function *F) = 0;
226 /// getOrEmitGlobalVariable - Return the address of the specified global
227 /// variable, possibly emitting it to memory if needed. This is used by the
229 virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
230 return getPointerToGlobal((GlobalValue*)GV);
233 /// DisableLazyCompilation - If called, the JIT will abort if lazy compilation
234 // is ever attempted.
235 void DisableLazyCompilation() {
236 LazyCompilationDisabled = true;
238 bool isLazyCompilationDisabled() const {
239 return LazyCompilationDisabled;
243 /// InstallLazyFunctionCreator - If an unknown function is needed, the
244 /// specified function pointer is invoked to create it. If it returns null,
245 /// the JIT will abort.
246 void InstallLazyFunctionCreator(void* (*P)(const std::string &)) {
247 LazyFunctionCreator = P;
251 explicit ExecutionEngine(ModuleProvider *P);
255 // EmitGlobalVariable - This method emits the specified global variable to the
256 // address specified in GlobalAddresses, or allocates new memory if it's not
257 // already in the map.
258 void EmitGlobalVariable(const GlobalVariable *GV);
260 GenericValue getConstantValue(const Constant *C);
261 void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr,
265 } // End llvm namespace