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
21 #include "llvm/System/Mutex.h"
22 #include "llvm/ADT/SmallVector.h"
36 class JITMemoryManager;
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 *> &
52 getGlobalAddressMap(const MutexGuard &) {
53 return GlobalAddressMap;
56 std::map<void*, const GlobalValue*> &
57 getGlobalAddressReverseMap(const MutexGuard &) {
58 return GlobalAddressReverseMap;
63 class ExecutionEngine {
65 ExecutionEngineState state;
66 bool LazyCompilationDisabled;
67 bool GVCompilationDisabled;
68 bool SymbolSearchingDisabled;
69 bool DlsymStubsEnabled;
72 /// Modules - This is a list of ModuleProvider's that we are JIT'ing from. We
73 /// use a smallvector to optimize for the case where there is only one module.
74 SmallVector<ModuleProvider*, 1> Modules;
76 void setTargetData(const TargetData *td) {
80 /// getMemoryforGV - Allocate memory for a global variable.
81 virtual char* getMemoryForGV(const GlobalVariable* GV);
83 // To avoid having libexecutionengine depend on the JIT and interpreter
84 // libraries, the JIT and Interpreter set these functions to ctor pointers
85 // at startup time if they are linked in.
86 typedef ExecutionEngine *(*EECtorFn)(ModuleProvider*, std::string*,
88 static EECtorFn JITCtor, InterpCtor;
90 /// LazyFunctionCreator - If an unknown function is needed, this function
91 /// pointer is invoked to create it. If this returns null, the JIT will abort.
92 void* (*LazyFunctionCreator)(const std::string &);
94 /// ExceptionTableRegister - If Exception Handling is set, the JIT will
95 /// register dwarf tables with this function
96 typedef void (*EERegisterFn)(void*);
97 static EERegisterFn ExceptionTableRegister;
100 /// lock - This lock is protects the ExecutionEngine, JIT, JITResolver and
101 /// JITEmitter classes. It must be held while changing the internal state of
102 /// any of those classes.
103 sys::Mutex lock; // Used to make this class and subclasses thread-safe
105 //===--------------------------------------------------------------------===//
106 // ExecutionEngine Startup
107 //===--------------------------------------------------------------------===//
109 virtual ~ExecutionEngine();
111 /// create - This is the factory method for creating an execution engine which
112 /// is appropriate for the current machine. This takes ownership of the
114 static ExecutionEngine *create(ModuleProvider *MP,
115 bool ForceInterpreter = false,
116 std::string *ErrorStr = 0,
117 unsigned OptLevel = 3);
119 /// create - This is the factory method for creating an execution engine which
120 /// is appropriate for the current machine. This takes ownership of the
122 static ExecutionEngine *create(Module *M);
124 /// createJIT - This is the factory method for creating a JIT for the current
125 /// machine, it does not fall back to the interpreter. This takes ownership
126 /// of the ModuleProvider and JITMemoryManager if successful.
127 static ExecutionEngine *createJIT(ModuleProvider *MP,
128 std::string *ErrorStr = 0,
129 JITMemoryManager *JMM = 0,
130 unsigned OptLevel = 3);
134 /// addModuleProvider - Add a ModuleProvider to the list of modules that we
135 /// can JIT from. Note that this takes ownership of the ModuleProvider: when
136 /// the ExecutionEngine is destroyed, it destroys the MP as well.
137 virtual void addModuleProvider(ModuleProvider *P) {
138 Modules.push_back(P);
141 //===----------------------------------------------------------------------===//
143 const TargetData *getTargetData() const { return TD; }
146 /// removeModuleProvider - Remove a ModuleProvider from the list of modules.
147 /// Relases the Module from the ModuleProvider, materializing it in the
148 /// process, and returns the materialized Module.
149 virtual Module* removeModuleProvider(ModuleProvider *P,
150 std::string *ErrInfo = 0);
152 /// deleteModuleProvider - Remove a ModuleProvider from the list of modules,
153 /// and deletes the ModuleProvider and owned Module. Avoids materializing
154 /// the underlying module.
155 virtual void deleteModuleProvider(ModuleProvider *P,std::string *ErrInfo = 0);
157 /// FindFunctionNamed - Search all of the active modules to find the one that
158 /// defines FnName. This is very slow operation and shouldn't be used for
160 Function *FindFunctionNamed(const char *FnName);
162 /// runFunction - Execute the specified function with the specified arguments,
163 /// and return the result.
165 virtual GenericValue runFunction(Function *F,
166 const std::vector<GenericValue> &ArgValues) = 0;
168 /// runStaticConstructorsDestructors - This method is used to execute all of
169 /// the static constructors or destructors for a program, depending on the
170 /// value of isDtors.
171 void runStaticConstructorsDestructors(bool isDtors);
172 /// runStaticConstructorsDestructors - This method is used to execute all of
173 /// the static constructors or destructors for a module, depending on the
174 /// value of isDtors.
175 void runStaticConstructorsDestructors(Module *module, bool isDtors);
178 /// runFunctionAsMain - This is a helper function which wraps runFunction to
179 /// handle the common task of starting up main with the specified argc, argv,
180 /// and envp parameters.
181 int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
182 const char * const * envp);
185 /// addGlobalMapping - Tell the execution engine that the specified global is
186 /// at the specified location. This is used internally as functions are JIT'd
187 /// and as global variables are laid out in memory. It can and should also be
188 /// used by clients of the EE that want to have an LLVM global overlay
189 /// existing data in memory. After adding a mapping for GV, you must not
190 /// destroy it until you've removed the mapping.
191 void addGlobalMapping(const GlobalValue *GV, void *Addr);
193 /// clearAllGlobalMappings - Clear all global mappings and start over again
194 /// use in dynamic compilation scenarios when you want to move globals
195 void clearAllGlobalMappings();
197 /// clearGlobalMappingsFromModule - Clear all global mappings that came from a
198 /// particular module, because it has been removed from the JIT.
199 void clearGlobalMappingsFromModule(Module *M);
201 /// updateGlobalMapping - Replace an existing mapping for GV with a new
202 /// address. This updates both maps as required. If "Addr" is null, the
203 /// entry for the global is removed from the mappings. This returns the old
204 /// value of the pointer, or null if it was not in the map.
205 void *updateGlobalMapping(const GlobalValue *GV, void *Addr);
207 /// getPointerToGlobalIfAvailable - This returns the address of the specified
208 /// global value if it is has already been codegen'd, otherwise it returns
211 void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
213 /// getPointerToGlobal - This returns the address of the specified global
214 /// value. This may involve code generation if it's a function. After
215 /// getting a pointer to GV, it and all globals it transitively refers to have
216 /// been passed to addGlobalMapping. You must clear the mapping for each
217 /// referred-to global before destroying it. If a referred-to global RTG is a
218 /// function and this ExecutionEngine is a JIT compiler, calling
219 /// updateGlobalMapping(RTG, 0) will leak the function's machine code, so you
220 /// should call freeMachineCodeForFunction(RTG) instead. Note that
221 /// optimizations can move and delete non-external GlobalValues without
222 /// notifying the ExecutionEngine.
224 void *getPointerToGlobal(const GlobalValue *GV);
226 /// getPointerToFunction - The different EE's represent function bodies in
227 /// different ways. They should each implement this to say what a function
228 /// pointer should look like. See getPointerToGlobal for the requirements on
229 /// destroying F and any GlobalValues it refers to.
231 virtual void *getPointerToFunction(Function *F) = 0;
233 /// getPointerToFunctionOrStub - If the specified function has been
234 /// code-gen'd, return a pointer to the function. If not, compile it, or use
235 /// a stub to implement lazy compilation if available. See getPointerToGlobal
236 /// for the requirements on destroying F and any GlobalValues it refers to.
238 virtual void *getPointerToFunctionOrStub(Function *F) {
239 // Default implementation, just codegen the function.
240 return getPointerToFunction(F);
243 /// getGlobalValueAtAddress - Return the LLVM global value object that starts
244 /// at the specified address.
246 const GlobalValue *getGlobalValueAtAddress(void *Addr);
249 void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr,
251 void InitializeMemory(const Constant *Init, void *Addr);
253 /// recompileAndRelinkFunction - This method is used to force a function
254 /// which has already been compiled to be compiled again, possibly
255 /// after it has been modified. Then the entry to the old copy is overwritten
256 /// with a branch to the new copy. If there was no old copy, this acts
257 /// just like VM::getPointerToFunction().
259 virtual void *recompileAndRelinkFunction(Function *F) = 0;
261 /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
262 /// corresponding to the machine code emitted to execute this function, useful
263 /// for garbage-collecting generated code.
265 virtual void freeMachineCodeForFunction(Function *F) = 0;
267 /// getOrEmitGlobalVariable - Return the address of the specified global
268 /// variable, possibly emitting it to memory if needed. This is used by the
269 /// Emitter. See getPointerToGlobal for the requirements on destroying GV and
270 /// any GlobalValues it refers to.
271 virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
272 return getPointerToGlobal((GlobalValue*)GV);
275 /// DisableLazyCompilation - If called, the JIT will abort if lazy compilation
276 /// is ever attempted.
277 void DisableLazyCompilation(bool Disabled = true) {
278 LazyCompilationDisabled = Disabled;
280 bool isLazyCompilationDisabled() const {
281 return LazyCompilationDisabled;
284 /// DisableGVCompilation - If called, the JIT will abort if it's asked to
285 /// allocate space and populate a GlobalVariable that is not internal to
287 void DisableGVCompilation(bool Disabled = true) {
288 GVCompilationDisabled = Disabled;
290 bool isGVCompilationDisabled() const {
291 return GVCompilationDisabled;
294 /// DisableSymbolSearching - If called, the JIT will not try to lookup unknown
295 /// symbols with dlsym. A client can still use InstallLazyFunctionCreator to
296 /// resolve symbols in a custom way.
297 void DisableSymbolSearching(bool Disabled = true) {
298 SymbolSearchingDisabled = Disabled;
300 bool isSymbolSearchingDisabled() const {
301 return SymbolSearchingDisabled;
304 /// EnableDlsymStubs -
305 void EnableDlsymStubs(bool Enabled = true) {
306 DlsymStubsEnabled = Enabled;
308 bool areDlsymStubsEnabled() const {
309 return DlsymStubsEnabled;
312 /// InstallLazyFunctionCreator - If an unknown function is needed, the
313 /// specified function pointer is invoked to create it. If it returns null,
314 /// the JIT will abort.
315 void InstallLazyFunctionCreator(void* (*P)(const std::string &)) {
316 LazyFunctionCreator = P;
319 /// InstallExceptionTableRegister - The JIT will use the given function
320 /// to register the exception tables it generates.
321 static void InstallExceptionTableRegister(void (*F)(void*)) {
322 ExceptionTableRegister = F;
325 /// RegisterTable - Registers the given pointer as an exception table. It uses
326 /// the ExceptionTableRegister function.
327 static void RegisterTable(void* res) {
328 if (ExceptionTableRegister)
329 ExceptionTableRegister(res);
333 explicit ExecutionEngine(ModuleProvider *P);
337 // EmitGlobalVariable - This method emits the specified global variable to the
338 // address specified in GlobalAddresses, or allocates new memory if it's not
339 // already in the map.
340 void EmitGlobalVariable(const GlobalVariable *GV);
342 GenericValue getConstantValue(const Constant *C);
343 void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr,
347 } // End llvm namespace