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/ADT/SmallVector.h"
22 #include "llvm/System/Mutex.h"
23 #include "llvm/Target/TargetMachine.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 GVCompilationDisabled;
69 bool SymbolSearchingDisabled;
70 bool DlsymStubsEnabled;
73 /// Modules - This is a list of ModuleProvider's that we are JIT'ing from. We
74 /// use a smallvector to optimize for the case where there is only one module.
75 SmallVector<ModuleProvider*, 1> Modules;
77 void setTargetData(const TargetData *td) {
81 /// getMemoryforGV - Allocate memory for a global variable.
82 virtual char* getMemoryForGV(const GlobalVariable* GV);
84 // To avoid having libexecutionengine depend on the JIT and interpreter
85 // libraries, the JIT and Interpreter set these functions to ctor pointers
86 // at startup time if they are linked in.
87 typedef ExecutionEngine *(*EECtorFn)(ModuleProvider*, std::string*,
88 CodeGenOpt::Level OptLevel);
89 static EECtorFn JITCtor, InterpCtor;
91 /// LazyFunctionCreator - If an unknown function is needed, this function
92 /// pointer is invoked to create it. If this returns null, the JIT will abort.
93 void* (*LazyFunctionCreator)(const std::string &);
95 /// ExceptionTableRegister - If Exception Handling is set, the JIT will
96 /// register dwarf tables with this function
97 typedef void (*EERegisterFn)(void*);
98 static EERegisterFn ExceptionTableRegister;
101 /// lock - This lock is protects the ExecutionEngine, JIT, JITResolver and
102 /// JITEmitter classes. It must be held while changing the internal state of
103 /// any of those classes.
104 sys::Mutex lock; // Used to make this class and subclasses thread-safe
106 //===--------------------------------------------------------------------===//
107 // ExecutionEngine Startup
108 //===--------------------------------------------------------------------===//
110 virtual ~ExecutionEngine();
112 /// create - This is the factory method for creating an execution engine which
113 /// is appropriate for the current machine. This takes ownership of the
115 static ExecutionEngine *create(ModuleProvider *MP,
116 bool ForceInterpreter = false,
117 std::string *ErrorStr = 0,
118 CodeGenOpt::Level OptLevel =
119 CodeGenOpt::Default);
121 /// create - This is the factory method for creating an execution engine which
122 /// is appropriate for the current machine. This takes ownership of the
124 static ExecutionEngine *create(Module *M);
126 /// createJIT - This is the factory method for creating a JIT for the current
127 /// machine, it does not fall back to the interpreter. This takes ownership
128 /// of the ModuleProvider and JITMemoryManager if successful.
129 static ExecutionEngine *createJIT(ModuleProvider *MP,
130 std::string *ErrorStr = 0,
131 JITMemoryManager *JMM = 0,
132 CodeGenOpt::Level OptLevel =
133 CodeGenOpt::Default);
135 /// addModuleProvider - Add a ModuleProvider to the list of modules that we
136 /// can JIT from. Note that this takes ownership of the ModuleProvider: when
137 /// the ExecutionEngine is destroyed, it destroys the MP as well.
138 virtual void addModuleProvider(ModuleProvider *P) {
139 Modules.push_back(P);
142 //===----------------------------------------------------------------------===//
144 const TargetData *getTargetData() const { return TD; }
147 /// removeModuleProvider - Remove a ModuleProvider from the list of modules.
148 /// Relases the Module from the ModuleProvider, materializing it in the
149 /// process, and returns the materialized Module.
150 virtual Module* removeModuleProvider(ModuleProvider *P,
151 std::string *ErrInfo = 0);
153 /// deleteModuleProvider - Remove a ModuleProvider from the list of modules,
154 /// and deletes the ModuleProvider and owned Module. Avoids materializing
155 /// the underlying module.
156 virtual void deleteModuleProvider(ModuleProvider *P,std::string *ErrInfo = 0);
158 /// FindFunctionNamed - Search all of the active modules to find the one that
159 /// defines FnName. This is very slow operation and shouldn't be used for
161 Function *FindFunctionNamed(const char *FnName);
163 /// runFunction - Execute the specified function with the specified arguments,
164 /// and return the result.
166 virtual GenericValue runFunction(Function *F,
167 const std::vector<GenericValue> &ArgValues) = 0;
169 /// runStaticConstructorsDestructors - This method is used to execute all of
170 /// the static constructors or destructors for a program, depending on the
171 /// value of isDtors.
172 void runStaticConstructorsDestructors(bool isDtors);
173 /// runStaticConstructorsDestructors - This method is used to execute all of
174 /// the static constructors or destructors for a module, depending on the
175 /// value of isDtors.
176 void runStaticConstructorsDestructors(Module *module, bool isDtors);
179 /// runFunctionAsMain - This is a helper function which wraps runFunction to
180 /// handle the common task of starting up main with the specified argc, argv,
181 /// and envp parameters.
182 int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
183 const char * const * envp);
186 /// addGlobalMapping - Tell the execution engine that the specified global is
187 /// at the specified location. This is used internally as functions are JIT'd
188 /// and as global variables are laid out in memory. It can and should also be
189 /// used by clients of the EE that want to have an LLVM global overlay
190 /// existing data in memory. After adding a mapping for GV, you must not
191 /// destroy it until you've removed the mapping.
192 void addGlobalMapping(const GlobalValue *GV, void *Addr);
194 /// clearAllGlobalMappings - Clear all global mappings and start over again
195 /// use in dynamic compilation scenarios when you want to move globals
196 void clearAllGlobalMappings();
198 /// clearGlobalMappingsFromModule - Clear all global mappings that came from a
199 /// particular module, because it has been removed from the JIT.
200 void clearGlobalMappingsFromModule(Module *M);
202 /// updateGlobalMapping - Replace an existing mapping for GV with a new
203 /// address. This updates both maps as required. If "Addr" is null, the
204 /// entry for the global is removed from the mappings. This returns the old
205 /// value of the pointer, or null if it was not in the map.
206 void *updateGlobalMapping(const GlobalValue *GV, void *Addr);
208 /// getPointerToGlobalIfAvailable - This returns the address of the specified
209 /// global value if it is has already been codegen'd, otherwise it returns
212 void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
214 /// getPointerToGlobal - This returns the address of the specified global
215 /// value. This may involve code generation if it's a function. After
216 /// getting a pointer to GV, it and all globals it transitively refers to have
217 /// been passed to addGlobalMapping. You must clear the mapping for each
218 /// referred-to global before destroying it. If a referred-to global RTG is a
219 /// function and this ExecutionEngine is a JIT compiler, calling
220 /// updateGlobalMapping(RTG, 0) will leak the function's machine code, so you
221 /// should call freeMachineCodeForFunction(RTG) instead. Note that
222 /// optimizations can move and delete non-external GlobalValues without
223 /// notifying the ExecutionEngine.
225 void *getPointerToGlobal(const GlobalValue *GV);
227 /// getPointerToFunction - The different EE's represent function bodies in
228 /// different ways. They should each implement this to say what a function
229 /// pointer should look like. See getPointerToGlobal for the requirements on
230 /// destroying F and any GlobalValues it refers to.
232 virtual void *getPointerToFunction(Function *F) = 0;
234 /// getPointerToFunctionOrStub - If the specified function has been
235 /// code-gen'd, return a pointer to the function. If not, compile it, or use
236 /// a stub to implement lazy compilation if available. See getPointerToGlobal
237 /// for the requirements on destroying F and any GlobalValues it refers to.
239 virtual void *getPointerToFunctionOrStub(Function *F) {
240 // Default implementation, just codegen the function.
241 return getPointerToFunction(F);
244 /// getGlobalValueAtAddress - Return the LLVM global value object that starts
245 /// at the specified address.
247 const GlobalValue *getGlobalValueAtAddress(void *Addr);
250 void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr,
252 void InitializeMemory(const Constant *Init, void *Addr);
254 /// recompileAndRelinkFunction - This method is used to force a function
255 /// which has already been compiled to be compiled again, possibly
256 /// after it has been modified. Then the entry to the old copy is overwritten
257 /// with a branch to the new copy. If there was no old copy, this acts
258 /// just like VM::getPointerToFunction().
260 virtual void *recompileAndRelinkFunction(Function *F) = 0;
262 /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
263 /// corresponding to the machine code emitted to execute this function, useful
264 /// for garbage-collecting generated code.
266 virtual void freeMachineCodeForFunction(Function *F) = 0;
268 /// getOrEmitGlobalVariable - Return the address of the specified global
269 /// variable, possibly emitting it to memory if needed. This is used by the
270 /// Emitter. See getPointerToGlobal for the requirements on destroying GV and
271 /// any GlobalValues it refers to.
272 virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
273 return getPointerToGlobal((GlobalValue*)GV);
276 /// DisableLazyCompilation - If called, the JIT will abort if lazy compilation
277 /// is ever attempted.
278 void DisableLazyCompilation(bool Disabled = true) {
279 LazyCompilationDisabled = Disabled;
281 bool isLazyCompilationDisabled() const {
282 return LazyCompilationDisabled;
285 /// DisableGVCompilation - If called, the JIT will abort if it's asked to
286 /// allocate space and populate a GlobalVariable that is not internal to
288 void DisableGVCompilation(bool Disabled = true) {
289 GVCompilationDisabled = Disabled;
291 bool isGVCompilationDisabled() const {
292 return GVCompilationDisabled;
295 /// DisableSymbolSearching - If called, the JIT will not try to lookup unknown
296 /// symbols with dlsym. A client can still use InstallLazyFunctionCreator to
297 /// resolve symbols in a custom way.
298 void DisableSymbolSearching(bool Disabled = true) {
299 SymbolSearchingDisabled = Disabled;
301 bool isSymbolSearchingDisabled() const {
302 return SymbolSearchingDisabled;
305 /// EnableDlsymStubs -
306 void EnableDlsymStubs(bool Enabled = true) {
307 DlsymStubsEnabled = Enabled;
309 bool areDlsymStubsEnabled() const {
310 return DlsymStubsEnabled;
313 /// InstallLazyFunctionCreator - If an unknown function is needed, the
314 /// specified function pointer is invoked to create it. If it returns null,
315 /// the JIT will abort.
316 void InstallLazyFunctionCreator(void* (*P)(const std::string &)) {
317 LazyFunctionCreator = P;
320 /// InstallExceptionTableRegister - The JIT will use the given function
321 /// to register the exception tables it generates.
322 static void InstallExceptionTableRegister(void (*F)(void*)) {
323 ExceptionTableRegister = F;
326 /// RegisterTable - Registers the given pointer as an exception table. It uses
327 /// the ExceptionTableRegister function.
328 static void RegisterTable(void* res) {
329 if (ExceptionTableRegister)
330 ExceptionTableRegister(res);
334 explicit ExecutionEngine(ModuleProvider *P);
338 // EmitGlobalVariable - This method emits the specified global variable to the
339 // address specified in GlobalAddresses, or allocates new memory if it's not
340 // already in the map.
341 void EmitGlobalVariable(const GlobalVariable *GV);
343 GenericValue getConstantValue(const Constant *C);
344 void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr,
348 } // End llvm namespace