1 //===- ExecutionEngine.h - Abstract Execution Engine Interface --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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 &) {
54 return GlobalAddressMap;
57 std::map<void*, const GlobalValue*> &
58 getGlobalAddressReverseMap(const MutexGuard &) {
59 return GlobalAddressReverseMap;
64 class ExecutionEngine {
66 ExecutionEngineState state;
67 bool LazyCompilationDisabled;
68 bool SymbolSearchingDisabled;
71 /// Modules - This is a list of ModuleProvider's that we are JIT'ing from. We
72 /// use a smallvector to optimize for the case where there is only one module.
73 SmallVector<ModuleProvider*, 1> Modules;
75 void setTargetData(const TargetData *td) {
79 // To avoid having libexecutionengine depend on the JIT and interpreter
80 // libraries, the JIT and Interpreter set these functions to ctor pointers
81 // at startup time if they are linked in.
82 typedef ExecutionEngine *(*EECtorFn)(ModuleProvider*, std::string*,
84 static EECtorFn JITCtor, InterpCtor;
86 /// LazyFunctionCreator - If an unknown function is needed, this function
87 /// pointer is invoked to create it. If this returns null, the JIT will abort.
88 void* (*LazyFunctionCreator)(const std::string &);
90 /// ExceptionTableRegister - If Exception Handling is set, the JIT will
91 /// register dwarf tables with this function
92 typedef void (*EERegisterFn)(void*);
93 static EERegisterFn ExceptionTableRegister;
96 /// lock - This lock is protects the ExecutionEngine, JIT, JITResolver and
97 /// JITEmitter classes. It must be held while changing the internal state of
98 /// any of those classes.
99 sys::Mutex lock; // Used to make this class and subclasses thread-safe
101 //===--------------------------------------------------------------------===//
102 // ExecutionEngine Startup
103 //===--------------------------------------------------------------------===//
105 virtual ~ExecutionEngine();
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(ModuleProvider *MP,
111 bool ForceInterpreter = false,
112 std::string *ErrorStr = 0,
115 /// create - This is the factory method for creating an execution engine which
116 /// is appropriate for the current machine. This takes ownership of the
118 static ExecutionEngine *create(Module *M);
120 /// createJIT - This is the factory method for creating a JIT for the current
121 /// machine, it does not fall back to the interpreter. This takes ownership
122 /// of the ModuleProvider and JITMemoryManager if successful.
123 static ExecutionEngine *createJIT(ModuleProvider *MP,
124 std::string *ErrorStr = 0,
125 JITMemoryManager *JMM = 0,
130 /// addModuleProvider - Add a ModuleProvider to the list of modules that we
131 /// can JIT from. Note that this takes ownership of the ModuleProvider: when
132 /// the ExecutionEngine is destroyed, it destroys the MP as well.
133 virtual void addModuleProvider(ModuleProvider *P) {
134 Modules.push_back(P);
137 //===----------------------------------------------------------------------===//
139 const TargetData *getTargetData() const { return TD; }
142 /// removeModuleProvider - Remove a ModuleProvider from the list of modules.
143 /// Release module from ModuleProvider.
144 virtual Module* removeModuleProvider(ModuleProvider *P,
145 std::string *ErrInfo = 0);
147 /// FindFunctionNamed - Search all of the active modules to find the one that
148 /// defines FnName. This is very slow operation and shouldn't be used for
150 Function *FindFunctionNamed(const char *FnName);
152 /// runFunction - Execute the specified function with the specified arguments,
153 /// and return the result.
155 virtual GenericValue runFunction(Function *F,
156 const std::vector<GenericValue> &ArgValues) = 0;
158 /// runStaticConstructorsDestructors - This method is used to execute all of
159 /// the static constructors or destructors for a module, depending on the
160 /// value of isDtors.
161 void runStaticConstructorsDestructors(bool isDtors);
164 /// runFunctionAsMain - This is a helper function which wraps runFunction to
165 /// handle the common task of starting up main with the specified argc, argv,
166 /// and envp parameters.
167 int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
168 const char * const * envp);
171 /// addGlobalMapping - Tell the execution engine that the specified global is
172 /// at the specified location. This is used internally as functions are JIT'd
173 /// and as global variables are laid out in memory. It can and should also be
174 /// used by clients of the EE that want to have an LLVM global overlay
175 /// existing data in memory.
176 void addGlobalMapping(const GlobalValue *GV, void *Addr);
178 /// clearAllGlobalMappings - Clear all global mappings and start over again
179 /// use in dynamic compilation scenarios when you want to move globals
180 void clearAllGlobalMappings();
182 /// clearGlobalMappingsFromModule - Clear all global mappings that came from a
183 /// particular module, because it has been removed from the JIT.
184 void clearGlobalMappingsFromModule(Module *M);
186 /// updateGlobalMapping - Replace an existing mapping for GV with a new
187 /// address. This updates both maps as required. If "Addr" is null, the
188 /// entry for the global is removed from the mappings. This returns the old
189 /// value of the pointer, or null if it was not in the map.
190 void *updateGlobalMapping(const GlobalValue *GV, void *Addr);
192 /// getPointerToGlobalIfAvailable - This returns the address of the specified
193 /// global value if it is has already been codegen'd, otherwise it returns
196 void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
198 /// getPointerToGlobal - This returns the address of the specified global
199 /// value. This may involve code generation if it's a function.
201 void *getPointerToGlobal(const GlobalValue *GV);
203 /// getPointerToFunction - The different EE's represent function bodies in
204 /// different ways. They should each implement this to say what a function
205 /// pointer should look like.
207 virtual void *getPointerToFunction(Function *F) = 0;
209 /// getPointerToFunctionOrStub - If the specified function has been
210 /// code-gen'd, return a pointer to the function. If not, compile it, or use
211 /// a stub to implement lazy compilation if available.
213 virtual void *getPointerToFunctionOrStub(Function *F) {
214 // Default implementation, just codegen the function.
215 return getPointerToFunction(F);
218 /// getGlobalValueAtAddress - Return the LLVM global value object that starts
219 /// at the specified address.
221 const GlobalValue *getGlobalValueAtAddress(void *Addr);
224 void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr,
226 void InitializeMemory(const Constant *Init, void *Addr);
228 /// recompileAndRelinkFunction - This method is used to force a function
229 /// which has already been compiled to be compiled again, possibly
230 /// after it has been modified. Then the entry to the old copy is overwritten
231 /// with a branch to the new copy. If there was no old copy, this acts
232 /// just like VM::getPointerToFunction().
234 virtual void *recompileAndRelinkFunction(Function *F) = 0;
236 /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
237 /// corresponding to the machine code emitted to execute this function, useful
238 /// for garbage-collecting generated code.
240 virtual void freeMachineCodeForFunction(Function *F) = 0;
242 /// getOrEmitGlobalVariable - Return the address of the specified global
243 /// variable, possibly emitting it to memory if needed. This is used by the
245 virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
246 return getPointerToGlobal((GlobalValue*)GV);
249 /// DisableLazyCompilation - If called, the JIT will abort if lazy compilation
250 /// is ever attempted.
251 void DisableLazyCompilation(bool Disabled = true) {
252 LazyCompilationDisabled = Disabled;
254 bool isLazyCompilationDisabled() const {
255 return LazyCompilationDisabled;
257 /// DisableSymbolSearching - If called, the JIT will not try to lookup unknown
258 /// symbols with dlsym. A client can still use InstallLazyFunctionCreator to
259 /// resolve symbols in a custom way.
260 void DisableSymbolSearching(bool Disabled = true) {
261 SymbolSearchingDisabled = Disabled;
263 bool isSymbolSearchingDisabled() const {
264 return SymbolSearchingDisabled;
268 /// InstallLazyFunctionCreator - If an unknown function is needed, the
269 /// specified function pointer is invoked to create it. If it returns null,
270 /// the JIT will abort.
271 void InstallLazyFunctionCreator(void* (*P)(const std::string &)) {
272 LazyFunctionCreator = P;
275 /// InstallExceptionTableRegister - The JIT will use the given function
276 /// to register the exception tables it generates.
277 static void InstallExceptionTableRegister(void (*F)(void*)) {
278 ExceptionTableRegister = F;
281 /// RegisterTable - Registers the given pointer as an exception table. It uses
282 /// the ExceptionTableRegister function.
283 static void RegisterTable(void* res) {
284 if (ExceptionTableRegister)
285 ExceptionTableRegister(res);
289 explicit ExecutionEngine(ModuleProvider *P);
293 // EmitGlobalVariable - This method emits the specified global variable to the
294 // address specified in GlobalAddresses, or allocates new memory if it's not
295 // already in the map.
296 void EmitGlobalVariable(const GlobalVariable *GV);
298 GenericValue getConstantValue(const Constant *C);
299 void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr,
303 } // End llvm namespace