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*);
83 static EECtorFn JITCtor, InterpCtor;
85 /// LazyFunctionCreator - If an unknown function is needed, this function
86 /// pointer is invoked to create it. If this returns null, the JIT will abort.
87 void* (*LazyFunctionCreator)(const std::string &);
89 /// ExceptionTableRegister - If Exception Handling is set, the JIT will
90 /// register dwarf tables with this function
91 typedef void (*EERegisterFn)(void*);
92 static EERegisterFn ExceptionTableRegister;
95 /// lock - This lock is protects the ExecutionEngine, JIT, JITResolver and
96 /// JITEmitter classes. It must be held while changing the internal state of
97 /// any of those classes.
98 sys::Mutex lock; // Used to make this class and subclasses thread-safe
100 //===--------------------------------------------------------------------===//
101 // ExecutionEngine Startup
102 //===--------------------------------------------------------------------===//
104 virtual ~ExecutionEngine();
106 /// create - This is the factory method for creating an execution engine which
107 /// is appropriate for the current machine. This takes ownership of the
109 static ExecutionEngine *create(ModuleProvider *MP,
110 bool ForceInterpreter = false,
111 std::string *ErrorStr = 0);
113 /// create - This is the factory method for creating an execution engine which
114 /// is appropriate for the current machine. This takes ownership of the
116 static ExecutionEngine *create(Module *M);
118 /// createJIT - This is the factory method for creating a JIT for the current
119 /// machine, it does not fall back to the interpreter. This takes ownership
120 /// of the ModuleProvider and JITMemoryManager if successful.
121 static ExecutionEngine *createJIT(ModuleProvider *MP,
122 std::string *ErrorStr = 0,
123 JITMemoryManager *JMM = 0);
127 /// addModuleProvider - Add a ModuleProvider to the list of modules that we
128 /// can JIT from. Note that this takes ownership of the ModuleProvider: when
129 /// the ExecutionEngine is destroyed, it destroys the MP as well.
130 virtual void addModuleProvider(ModuleProvider *P) {
131 Modules.push_back(P);
134 //===----------------------------------------------------------------------===//
136 const TargetData *getTargetData() const { return TD; }
139 /// removeModuleProvider - Remove a ModuleProvider from the list of modules.
140 /// Release module from ModuleProvider.
141 virtual Module* removeModuleProvider(ModuleProvider *P,
142 std::string *ErrInfo = 0);
144 /// FindFunctionNamed - Search all of the active modules to find the one that
145 /// defines FnName. This is very slow operation and shouldn't be used for
147 Function *FindFunctionNamed(const char *FnName);
149 /// runFunction - Execute the specified function with the specified arguments,
150 /// and return the result.
152 virtual GenericValue runFunction(Function *F,
153 const std::vector<GenericValue> &ArgValues) = 0;
155 /// runStaticConstructorsDestructors - This method is used to execute all of
156 /// the static constructors or destructors for a module, depending on the
157 /// value of isDtors.
158 void runStaticConstructorsDestructors(bool isDtors);
161 /// runFunctionAsMain - This is a helper function which wraps runFunction to
162 /// handle the common task of starting up main with the specified argc, argv,
163 /// and envp parameters.
164 int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
165 const char * const * envp);
168 /// addGlobalMapping - Tell the execution engine that the specified global is
169 /// at the specified location. This is used internally as functions are JIT'd
170 /// and as global variables are laid out in memory. It can and should also be
171 /// used by clients of the EE that want to have an LLVM global overlay
172 /// existing data in memory.
173 void addGlobalMapping(const GlobalValue *GV, void *Addr);
175 /// clearAllGlobalMappings - Clear all global mappings and start over again
176 /// use in dynamic compilation scenarios when you want to move globals
177 void clearAllGlobalMappings();
179 /// clearGlobalMappingsFromModule - Clear all global mappings that came from a
180 /// particular module, because it has been removed from the JIT.
181 void clearGlobalMappingsFromModule(Module *M);
183 /// updateGlobalMapping - Replace an existing mapping for GV with a new
184 /// address. This updates both maps as required. If "Addr" is null, the
185 /// entry for the global is removed from the mappings. This returns the old
186 /// value of the pointer, or null if it was not in the map.
187 void *updateGlobalMapping(const GlobalValue *GV, void *Addr);
189 /// getPointerToGlobalIfAvailable - This returns the address of the specified
190 /// global value if it is has already been codegen'd, otherwise it returns
193 void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
195 /// getPointerToGlobal - This returns the address of the specified global
196 /// value. This may involve code generation if it's a function.
198 void *getPointerToGlobal(const GlobalValue *GV);
200 /// getPointerToFunction - The different EE's represent function bodies in
201 /// different ways. They should each implement this to say what a function
202 /// pointer should look like.
204 virtual void *getPointerToFunction(Function *F) = 0;
206 /// getPointerToFunctionOrStub - If the specified function has been
207 /// code-gen'd, return a pointer to the function. If not, compile it, or use
208 /// a stub to implement lazy compilation if available.
210 virtual void *getPointerToFunctionOrStub(Function *F) {
211 // Default implementation, just codegen the function.
212 return getPointerToFunction(F);
215 /// getGlobalValueAtAddress - Return the LLVM global value object that starts
216 /// at the specified address.
218 const GlobalValue *getGlobalValueAtAddress(void *Addr);
221 void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr,
223 void InitializeMemory(const Constant *Init, void *Addr);
225 /// recompileAndRelinkFunction - This method is used to force a function
226 /// which has already been compiled to be compiled again, possibly
227 /// after it has been modified. Then the entry to the old copy is overwritten
228 /// with a branch to the new copy. If there was no old copy, this acts
229 /// just like VM::getPointerToFunction().
231 virtual void *recompileAndRelinkFunction(Function *F) = 0;
233 /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
234 /// corresponding to the machine code emitted to execute this function, useful
235 /// for garbage-collecting generated code.
237 virtual void freeMachineCodeForFunction(Function *F) = 0;
239 /// getOrEmitGlobalVariable - Return the address of the specified global
240 /// variable, possibly emitting it to memory if needed. This is used by the
242 virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
243 return getPointerToGlobal((GlobalValue*)GV);
246 /// DisableLazyCompilation - If called, the JIT will abort if lazy compilation
247 /// is ever attempted.
248 void DisableLazyCompilation(bool Disabled = true) {
249 LazyCompilationDisabled = Disabled;
251 bool isLazyCompilationDisabled() const {
252 return LazyCompilationDisabled;
254 /// DisableSymbolSearching - If called, the JIT will not try to lookup unknown
255 /// symbols with dlsym. A client can still use InstallLazyFunctionCreator to
256 /// resolve symbols in a custom way.
257 void DisableSymbolSearching(bool Disabled = true) {
258 SymbolSearchingDisabled = Disabled;
260 bool isSymbolSearchingDisabled() const {
261 return SymbolSearchingDisabled;
265 /// InstallLazyFunctionCreator - If an unknown function is needed, the
266 /// specified function pointer is invoked to create it. If it returns null,
267 /// the JIT will abort.
268 void InstallLazyFunctionCreator(void* (*P)(const std::string &)) {
269 LazyFunctionCreator = P;
272 /// InstallExceptionTableRegister - The JIT will use the given function
273 /// to register the exception tables it generates.
274 static void InstallExceptionTableRegister(void (*F)(void*)) {
275 ExceptionTableRegister = F;
278 /// RegisterTable - Registers the given pointer as an exception table. It uses
279 /// the ExceptionTableRegister function.
280 static void RegisterTable(void* res) {
281 if (ExceptionTableRegister)
282 ExceptionTableRegister(res);
286 explicit ExecutionEngine(ModuleProvider *P);
290 // EmitGlobalVariable - This method emits the specified global variable to the
291 // address specified in GlobalAddresses, or allocates new memory if it's not
292 // already in the map.
293 void EmitGlobalVariable(const GlobalVariable *GV);
295 GenericValue getConstantValue(const Constant *C);
296 void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr,
300 } // End llvm namespace