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"
32 class JITEventListener;
33 class JITMemoryManager;
34 class MachineCodeInfo;
41 class ExecutionEngineState {
43 /// GlobalAddressMap - A mapping between LLVM global values and their
44 /// actualized version...
45 std::map<const GlobalValue*, void *> GlobalAddressMap;
47 /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap,
48 /// used to convert raw addresses into the LLVM global value that is emitted
49 /// at the address. This map is not computed unless getGlobalValueAtAddress
50 /// is called at some point.
51 std::map<void *, const GlobalValue*> GlobalAddressReverseMap;
54 std::map<const GlobalValue*, void *> &
55 getGlobalAddressMap(const MutexGuard &) {
56 return GlobalAddressMap;
59 std::map<void*, const GlobalValue*> &
60 getGlobalAddressReverseMap(const MutexGuard &) {
61 return GlobalAddressReverseMap;
66 class ExecutionEngine {
68 ExecutionEngineState state;
69 bool LazyCompilationDisabled;
70 bool GVCompilationDisabled;
71 bool SymbolSearchingDisabled;
72 bool DlsymStubsEnabled;
75 /// Modules - This is a list of ModuleProvider's that we are JIT'ing from. We
76 /// use a smallvector to optimize for the case where there is only one module.
77 SmallVector<ModuleProvider*, 1> Modules;
79 void setTargetData(const TargetData *td) {
83 /// getMemoryforGV - Allocate memory for a global variable.
84 virtual char* getMemoryForGV(const GlobalVariable* GV);
86 // To avoid having libexecutionengine depend on the JIT and interpreter
87 // libraries, the JIT and Interpreter set these functions to ctor pointers
88 // at startup time if they are linked in.
89 typedef ExecutionEngine *(*EECtorFn)(ModuleProvider*, std::string*,
90 CodeGenOpt::Level OptLevel);
91 static EECtorFn JITCtor, InterpCtor;
93 /// LazyFunctionCreator - If an unknown function is needed, this function
94 /// pointer is invoked to create it. If this returns null, the JIT will abort.
95 void* (*LazyFunctionCreator)(const std::string &);
97 /// ExceptionTableRegister - If Exception Handling is set, the JIT will
98 /// register dwarf tables with this function
99 typedef void (*EERegisterFn)(void*);
100 static EERegisterFn ExceptionTableRegister;
103 /// lock - This lock is protects the ExecutionEngine, JIT, JITResolver and
104 /// JITEmitter classes. It must be held while changing the internal state of
105 /// any of those classes.
106 sys::Mutex lock; // Used to make this class and subclasses thread-safe
108 //===--------------------------------------------------------------------===//
109 // ExecutionEngine Startup
110 //===--------------------------------------------------------------------===//
112 virtual ~ExecutionEngine();
114 /// create - This is the factory method for creating an execution engine which
115 /// is appropriate for the current machine. This takes ownership of the
117 static ExecutionEngine *create(ModuleProvider *MP,
118 bool ForceInterpreter = false,
119 std::string *ErrorStr = 0,
120 CodeGenOpt::Level OptLevel =
121 CodeGenOpt::Default);
123 /// create - This is the factory method for creating an execution engine which
124 /// is appropriate for the current machine. This takes ownership of the
126 static ExecutionEngine *create(Module *M);
128 /// createJIT - This is the factory method for creating a JIT for the current
129 /// machine, it does not fall back to the interpreter. This takes ownership
130 /// of the ModuleProvider and JITMemoryManager if successful.
131 static ExecutionEngine *createJIT(ModuleProvider *MP,
132 std::string *ErrorStr = 0,
133 JITMemoryManager *JMM = 0,
134 CodeGenOpt::Level OptLevel =
135 CodeGenOpt::Default);
137 /// addModuleProvider - Add a ModuleProvider to the list of modules that we
138 /// can JIT from. Note that this takes ownership of the ModuleProvider: when
139 /// the ExecutionEngine is destroyed, it destroys the MP as well.
140 virtual void addModuleProvider(ModuleProvider *P) {
141 Modules.push_back(P);
144 //===----------------------------------------------------------------------===//
146 const TargetData *getTargetData() const { return TD; }
149 /// removeModuleProvider - Remove a ModuleProvider from the list of modules.
150 /// Relases the Module from the ModuleProvider, materializing it in the
151 /// process, and returns the materialized Module.
152 virtual Module* removeModuleProvider(ModuleProvider *P,
153 std::string *ErrInfo = 0);
155 /// deleteModuleProvider - Remove a ModuleProvider from the list of modules,
156 /// and deletes the ModuleProvider and owned Module. Avoids materializing
157 /// the underlying module.
158 virtual void deleteModuleProvider(ModuleProvider *P,std::string *ErrInfo = 0);
160 /// FindFunctionNamed - Search all of the active modules to find the one that
161 /// defines FnName. This is very slow operation and shouldn't be used for
163 Function *FindFunctionNamed(const char *FnName);
165 /// runFunction - Execute the specified function with the specified arguments,
166 /// and return the result.
168 virtual GenericValue runFunction(Function *F,
169 const std::vector<GenericValue> &ArgValues) = 0;
171 /// runStaticConstructorsDestructors - This method is used to execute all of
172 /// the static constructors or destructors for a program, depending on the
173 /// value of isDtors.
174 void runStaticConstructorsDestructors(bool isDtors);
175 /// runStaticConstructorsDestructors - This method is used to execute all of
176 /// the static constructors or destructors for a module, depending on the
177 /// value of isDtors.
178 void runStaticConstructorsDestructors(Module *module, bool isDtors);
181 /// runFunctionAsMain - This is a helper function which wraps runFunction to
182 /// handle the common task of starting up main with the specified argc, argv,
183 /// and envp parameters.
184 int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
185 const char * const * envp);
188 /// addGlobalMapping - Tell the execution engine that the specified global is
189 /// at the specified location. This is used internally as functions are JIT'd
190 /// and as global variables are laid out in memory. It can and should also be
191 /// used by clients of the EE that want to have an LLVM global overlay
192 /// existing data in memory. After adding a mapping for GV, you must not
193 /// destroy it until you've removed the mapping.
194 void addGlobalMapping(const GlobalValue *GV, void *Addr);
196 /// clearAllGlobalMappings - Clear all global mappings and start over again
197 /// use in dynamic compilation scenarios when you want to move globals
198 void clearAllGlobalMappings();
200 /// clearGlobalMappingsFromModule - Clear all global mappings that came from a
201 /// particular module, because it has been removed from the JIT.
202 void clearGlobalMappingsFromModule(Module *M);
204 /// updateGlobalMapping - Replace an existing mapping for GV with a new
205 /// address. This updates both maps as required. If "Addr" is null, the
206 /// entry for the global is removed from the mappings. This returns the old
207 /// value of the pointer, or null if it was not in the map.
208 void *updateGlobalMapping(const GlobalValue *GV, void *Addr);
210 /// getPointerToGlobalIfAvailable - This returns the address of the specified
211 /// global value if it is has already been codegen'd, otherwise it returns
214 void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
216 /// getPointerToGlobal - This returns the address of the specified global
217 /// value. This may involve code generation if it's a function. After
218 /// getting a pointer to GV, it and all globals it transitively refers to have
219 /// been passed to addGlobalMapping. You must clear the mapping for each
220 /// referred-to global before destroying it. If a referred-to global RTG is a
221 /// function and this ExecutionEngine is a JIT compiler, calling
222 /// updateGlobalMapping(RTG, 0) will leak the function's machine code, so you
223 /// should call freeMachineCodeForFunction(RTG) instead. Note that
224 /// optimizations can move and delete non-external GlobalValues without
225 /// notifying the ExecutionEngine.
227 void *getPointerToGlobal(const GlobalValue *GV);
229 /// getPointerToFunction - The different EE's represent function bodies in
230 /// different ways. They should each implement this to say what a function
231 /// pointer should look like. See getPointerToGlobal for the requirements on
232 /// destroying F and any GlobalValues it refers to.
234 virtual void *getPointerToFunction(Function *F) = 0;
236 /// getPointerToFunctionOrStub - If the specified function has been
237 /// code-gen'd, return a pointer to the function. If not, compile it, or use
238 /// a stub to implement lazy compilation if available. See getPointerToGlobal
239 /// for the requirements on destroying F and any GlobalValues it refers to.
241 virtual void *getPointerToFunctionOrStub(Function *F) {
242 // Default implementation, just codegen the function.
243 return getPointerToFunction(F);
246 // The JIT overrides a version that actually does this.
247 virtual void runJITOnFunction(Function *, MachineCodeInfo * = 0) { }
249 /// getGlobalValueAtAddress - Return the LLVM global value object that starts
250 /// at the specified address.
252 const GlobalValue *getGlobalValueAtAddress(void *Addr);
255 void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr,
257 void InitializeMemory(const Constant *Init, void *Addr);
259 /// recompileAndRelinkFunction - This method is used to force a function
260 /// which has already been compiled to be compiled again, possibly
261 /// after it has been modified. Then the entry to the old copy is overwritten
262 /// with a branch to the new copy. If there was no old copy, this acts
263 /// just like VM::getPointerToFunction().
265 virtual void *recompileAndRelinkFunction(Function *F) = 0;
267 /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
268 /// corresponding to the machine code emitted to execute this function, useful
269 /// for garbage-collecting generated code.
271 virtual void freeMachineCodeForFunction(Function *F) = 0;
273 /// getOrEmitGlobalVariable - Return the address of the specified global
274 /// variable, possibly emitting it to memory if needed. This is used by the
275 /// Emitter. See getPointerToGlobal for the requirements on destroying GV and
276 /// any GlobalValues it refers to.
277 virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
278 return getPointerToGlobal((GlobalValue*)GV);
281 /// Registers a listener to be called back on various events within
282 /// the JIT. See JITEventListener.h for more details. Does not
283 /// take ownership of the argument. The argument may be NULL, in
284 /// which case these functions do nothing.
285 virtual void RegisterJITEventListener(JITEventListener *L) {}
286 virtual void UnregisterJITEventListener(JITEventListener *L) {}
288 /// DisableLazyCompilation - If called, the JIT will abort if lazy compilation
289 /// is ever attempted.
290 void DisableLazyCompilation(bool Disabled = true) {
291 LazyCompilationDisabled = Disabled;
293 bool isLazyCompilationDisabled() const {
294 return LazyCompilationDisabled;
297 /// DisableGVCompilation - If called, the JIT will abort if it's asked to
298 /// allocate space and populate a GlobalVariable that is not internal to
300 void DisableGVCompilation(bool Disabled = true) {
301 GVCompilationDisabled = Disabled;
303 bool isGVCompilationDisabled() const {
304 return GVCompilationDisabled;
307 /// DisableSymbolSearching - If called, the JIT will not try to lookup unknown
308 /// symbols with dlsym. A client can still use InstallLazyFunctionCreator to
309 /// resolve symbols in a custom way.
310 void DisableSymbolSearching(bool Disabled = true) {
311 SymbolSearchingDisabled = Disabled;
313 bool isSymbolSearchingDisabled() const {
314 return SymbolSearchingDisabled;
317 /// EnableDlsymStubs -
318 void EnableDlsymStubs(bool Enabled = true) {
319 DlsymStubsEnabled = Enabled;
321 bool areDlsymStubsEnabled() const {
322 return DlsymStubsEnabled;
325 /// InstallLazyFunctionCreator - If an unknown function is needed, the
326 /// specified function pointer is invoked to create it. If it returns null,
327 /// the JIT will abort.
328 void InstallLazyFunctionCreator(void* (*P)(const std::string &)) {
329 LazyFunctionCreator = P;
332 /// InstallExceptionTableRegister - The JIT will use the given function
333 /// to register the exception tables it generates.
334 static void InstallExceptionTableRegister(void (*F)(void*)) {
335 ExceptionTableRegister = F;
338 /// RegisterTable - Registers the given pointer as an exception table. It uses
339 /// the ExceptionTableRegister function.
340 static void RegisterTable(void* res) {
341 if (ExceptionTableRegister)
342 ExceptionTableRegister(res);
346 explicit ExecutionEngine(ModuleProvider *P);
350 // EmitGlobalVariable - This method emits the specified global variable to the
351 // address specified in GlobalAddresses, or allocates new memory if it's not
352 // already in the map.
353 void EmitGlobalVariable(const GlobalVariable *GV);
355 GenericValue getConstantValue(const Constant *C);
356 void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr,
360 } // End llvm namespace