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;
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 /// getMemoryforGV - Allocate memory for a global variable.
80 virtual char* getMemoryForGV(const GlobalVariable* GV);
82 // To avoid having libexecutionengine depend on the JIT and interpreter
83 // libraries, the JIT and Interpreter set these functions to ctor pointers
84 // at startup time if they are linked in.
85 typedef ExecutionEngine *(*EECtorFn)(ModuleProvider*, std::string*,
87 static EECtorFn JITCtor, InterpCtor;
89 /// LazyFunctionCreator - If an unknown function is needed, this function
90 /// pointer is invoked to create it. If this returns null, the JIT will abort.
91 void* (*LazyFunctionCreator)(const std::string &);
93 /// ExceptionTableRegister - If Exception Handling is set, the JIT will
94 /// register dwarf tables with this function
95 typedef void (*EERegisterFn)(void*);
96 static EERegisterFn ExceptionTableRegister;
99 /// lock - This lock is protects the ExecutionEngine, JIT, JITResolver and
100 /// JITEmitter classes. It must be held while changing the internal state of
101 /// any of those classes.
102 sys::Mutex lock; // Used to make this class and subclasses thread-safe
104 //===--------------------------------------------------------------------===//
105 // ExecutionEngine Startup
106 //===--------------------------------------------------------------------===//
108 virtual ~ExecutionEngine();
110 /// create - This is the factory method for creating an execution engine which
111 /// is appropriate for the current machine. This takes ownership of the
113 static ExecutionEngine *create(ModuleProvider *MP,
114 bool ForceInterpreter = false,
115 std::string *ErrorStr = 0,
118 /// create - This is the factory method for creating an execution engine which
119 /// is appropriate for the current machine. This takes ownership of the
121 static ExecutionEngine *create(Module *M);
123 /// createJIT - This is the factory method for creating a JIT for the current
124 /// machine, it does not fall back to the interpreter. This takes ownership
125 /// of the ModuleProvider and JITMemoryManager if successful.
126 static ExecutionEngine *createJIT(ModuleProvider *MP,
127 std::string *ErrorStr = 0,
128 JITMemoryManager *JMM = 0,
133 /// addModuleProvider - Add a ModuleProvider to the list of modules that we
134 /// can JIT from. Note that this takes ownership of the ModuleProvider: when
135 /// the ExecutionEngine is destroyed, it destroys the MP as well.
136 virtual void addModuleProvider(ModuleProvider *P) {
137 Modules.push_back(P);
140 //===----------------------------------------------------------------------===//
142 const TargetData *getTargetData() const { return TD; }
145 /// removeModuleProvider - Remove a ModuleProvider from the list of modules.
146 /// Release module from ModuleProvider.
147 virtual Module* removeModuleProvider(ModuleProvider *P,
148 std::string *ErrInfo = 0);
150 /// FindFunctionNamed - Search all of the active modules to find the one that
151 /// defines FnName. This is very slow operation and shouldn't be used for
153 Function *FindFunctionNamed(const char *FnName);
155 /// runFunction - Execute the specified function with the specified arguments,
156 /// and return the result.
158 virtual GenericValue runFunction(Function *F,
159 const std::vector<GenericValue> &ArgValues) = 0;
161 /// runStaticConstructorsDestructors - This method is used to execute all of
162 /// the static constructors or destructors for a program, depending on the
163 /// value of isDtors.
164 void runStaticConstructorsDestructors(bool isDtors);
165 /// runStaticConstructorsDestructors - This method is used to execute all of
166 /// the static constructors or destructors for a module, depending on the
167 /// value of isDtors.
168 void runStaticConstructorsDestructors(Module *module, bool isDtors);
171 /// runFunctionAsMain - This is a helper function which wraps runFunction to
172 /// handle the common task of starting up main with the specified argc, argv,
173 /// and envp parameters.
174 int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
175 const char * const * envp);
178 /// addGlobalMapping - Tell the execution engine that the specified global is
179 /// at the specified location. This is used internally as functions are JIT'd
180 /// and as global variables are laid out in memory. It can and should also be
181 /// used by clients of the EE that want to have an LLVM global overlay
182 /// existing data in memory.
183 void addGlobalMapping(const GlobalValue *GV, void *Addr);
185 /// clearAllGlobalMappings - Clear all global mappings and start over again
186 /// use in dynamic compilation scenarios when you want to move globals
187 void clearAllGlobalMappings();
189 /// clearGlobalMappingsFromModule - Clear all global mappings that came from a
190 /// particular module, because it has been removed from the JIT.
191 void clearGlobalMappingsFromModule(Module *M);
193 /// updateGlobalMapping - Replace an existing mapping for GV with a new
194 /// address. This updates both maps as required. If "Addr" is null, the
195 /// entry for the global is removed from the mappings. This returns the old
196 /// value of the pointer, or null if it was not in the map.
197 void *updateGlobalMapping(const GlobalValue *GV, void *Addr);
199 /// getPointerToGlobalIfAvailable - This returns the address of the specified
200 /// global value if it is has already been codegen'd, otherwise it returns
203 void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
205 /// getPointerToGlobal - This returns the address of the specified global
206 /// value. This may involve code generation if it's a function.
208 void *getPointerToGlobal(const GlobalValue *GV);
210 /// getPointerToFunction - The different EE's represent function bodies in
211 /// different ways. They should each implement this to say what a function
212 /// pointer should look like.
214 virtual void *getPointerToFunction(Function *F) = 0;
216 /// getPointerToFunctionOrStub - If the specified function has been
217 /// code-gen'd, return a pointer to the function. If not, compile it, or use
218 /// a stub to implement lazy compilation if available.
220 virtual void *getPointerToFunctionOrStub(Function *F) {
221 // Default implementation, just codegen the function.
222 return getPointerToFunction(F);
225 /// getGlobalValueAtAddress - Return the LLVM global value object that starts
226 /// at the specified address.
228 const GlobalValue *getGlobalValueAtAddress(void *Addr);
231 void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr,
233 void InitializeMemory(const Constant *Init, void *Addr);
235 /// recompileAndRelinkFunction - This method is used to force a function
236 /// which has already been compiled to be compiled again, possibly
237 /// after it has been modified. Then the entry to the old copy is overwritten
238 /// with a branch to the new copy. If there was no old copy, this acts
239 /// just like VM::getPointerToFunction().
241 virtual void *recompileAndRelinkFunction(Function *F) = 0;
243 /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
244 /// corresponding to the machine code emitted to execute this function, useful
245 /// for garbage-collecting generated code.
247 virtual void freeMachineCodeForFunction(Function *F) = 0;
249 /// getOrEmitGlobalVariable - Return the address of the specified global
250 /// variable, possibly emitting it to memory if needed. This is used by the
252 virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
253 return getPointerToGlobal((GlobalValue*)GV);
256 /// DisableLazyCompilation - If called, the JIT will abort if lazy compilation
257 /// is ever attempted.
258 void DisableLazyCompilation(bool Disabled = true) {
259 LazyCompilationDisabled = Disabled;
261 bool isLazyCompilationDisabled() const {
262 return LazyCompilationDisabled;
265 /// DisableGVCompilation - If called, the JIT will abort if it's asked to
266 /// allocate space and populate a GlobalVariable that is not internal to
268 void DisableGVCompilation(bool Disabled = true) {
269 GVCompilationDisabled = Disabled;
271 bool isGVCompilationDisabled() const {
272 return GVCompilationDisabled;
275 /// DisableSymbolSearching - If called, the JIT will not try to lookup unknown
276 /// symbols with dlsym. A client can still use InstallLazyFunctionCreator to
277 /// resolve symbols in a custom way.
278 void DisableSymbolSearching(bool Disabled = true) {
279 SymbolSearchingDisabled = Disabled;
281 bool isSymbolSearchingDisabled() const {
282 return SymbolSearchingDisabled;
286 /// InstallLazyFunctionCreator - If an unknown function is needed, the
287 /// specified function pointer is invoked to create it. If it returns null,
288 /// the JIT will abort.
289 void InstallLazyFunctionCreator(void* (*P)(const std::string &)) {
290 LazyFunctionCreator = P;
293 /// InstallExceptionTableRegister - The JIT will use the given function
294 /// to register the exception tables it generates.
295 static void InstallExceptionTableRegister(void (*F)(void*)) {
296 ExceptionTableRegister = F;
299 /// RegisterTable - Registers the given pointer as an exception table. It uses
300 /// the ExceptionTableRegister function.
301 static void RegisterTable(void* res) {
302 if (ExceptionTableRegister)
303 ExceptionTableRegister(res);
307 explicit ExecutionEngine(ModuleProvider *P);
311 // EmitGlobalVariable - This method emits the specified global variable to the
312 // address specified in GlobalAddresses, or allocates new memory if it's not
313 // already in the map.
314 void EmitGlobalVariable(const GlobalVariable *GV);
316 GenericValue getConstantValue(const Constant *C);
317 void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr,
321 } // End llvm namespace