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;
70 /// Modules - This is a list of ModuleProvider's that we are JIT'ing from. We
71 /// use a smallvector to optimize for the case where there is only one module.
72 SmallVector<ModuleProvider*, 1> Modules;
74 void setTargetData(const TargetData *td) {
78 // To avoid having libexecutionengine depend on the JIT and interpreter
79 // libraries, the JIT and Interpreter set these functions to ctor pointers
80 // at startup time if they are linked in.
81 typedef ExecutionEngine *(*EECtorFn)(ModuleProvider*, std::string*);
82 static EECtorFn JITCtor, InterpCtor;
84 /// LazyFunctionCreator - If an unknown function is needed, this function
85 /// pointer is invoked to create it. If this returns null, the JIT will abort.
86 void* (*LazyFunctionCreator)(const std::string &);
88 /// ExceptionTableRegister - If Exception Handling is set, the JIT will
89 /// register dwarf tables with this function
90 typedef void (*EERegisterFn)(void*);
91 static EERegisterFn ExceptionTableRegister;
94 /// lock - This lock is protects the ExecutionEngine, JIT, JITResolver and
95 /// JITEmitter classes. It must be held while changing the internal state of
96 /// any of those classes.
97 sys::Mutex lock; // Used to make this class and subclasses thread-safe
99 //===--------------------------------------------------------------------===//
100 // ExecutionEngine Startup
101 //===--------------------------------------------------------------------===//
103 virtual ~ExecutionEngine();
105 /// create - This is the factory method for creating an execution engine which
106 /// is appropriate for the current machine. This takes ownership of the
108 static ExecutionEngine *create(ModuleProvider *MP,
109 bool ForceInterpreter = false,
110 std::string *ErrorStr = 0);
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(Module *M);
117 /// createJIT - This is the factory method for creating a JIT for the current
118 /// machine, it does not fall back to the interpreter. This takes ownership
119 /// of the ModuleProvider and JITMemoryManager if successful.
120 static ExecutionEngine *createJIT(ModuleProvider *MP,
121 std::string *ErrorStr = 0,
122 JITMemoryManager *JMM = 0);
126 /// addModuleProvider - Add a ModuleProvider to the list of modules that we
127 /// can JIT from. Note that this takes ownership of the ModuleProvider: when
128 /// the ExecutionEngine is destroyed, it destroys the MP as well.
129 void addModuleProvider(ModuleProvider *P) {
130 Modules.push_back(P);
133 //===----------------------------------------------------------------------===//
135 const TargetData *getTargetData() const { return TD; }
138 /// removeModuleProvider - Remove a ModuleProvider from the list of modules.
139 /// Release module from ModuleProvider.
140 Module* removeModuleProvider(ModuleProvider *P, std::string *ErrInfo = 0);
142 /// FindFunctionNamed - Search all of the active modules to find the one that
143 /// defines FnName. This is very slow operation and shouldn't be used for
145 Function *FindFunctionNamed(const char *FnName);
147 /// runFunction - Execute the specified function with the specified arguments,
148 /// and return the result.
150 virtual GenericValue runFunction(Function *F,
151 const std::vector<GenericValue> &ArgValues) = 0;
153 /// runStaticConstructorsDestructors - This method is used to execute all of
154 /// the static constructors or destructors for a module, depending on the
155 /// value of isDtors.
156 void runStaticConstructorsDestructors(bool isDtors);
159 /// runFunctionAsMain - This is a helper function which wraps runFunction to
160 /// handle the common task of starting up main with the specified argc, argv,
161 /// and envp parameters.
162 int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
163 const char * const * envp);
166 /// addGlobalMapping - Tell the execution engine that the specified global is
167 /// at the specified location. This is used internally as functions are JIT'd
168 /// and as global variables are laid out in memory. It can and should also be
169 /// used by clients of the EE that want to have an LLVM global overlay
170 /// existing data in memory.
171 void addGlobalMapping(const GlobalValue *GV, void *Addr);
173 /// clearAllGlobalMappings - Clear all global mappings and start over again
174 /// use in dynamic compilation scenarios when you want to move globals
175 void clearAllGlobalMappings();
177 /// updateGlobalMapping - Replace an existing mapping for GV with a new
178 /// address. This updates both maps as required. If "Addr" is null, the
179 /// entry for the global is removed from the mappings. This returns the old
180 /// value of the pointer, or null if it was not in the map.
181 void *updateGlobalMapping(const GlobalValue *GV, void *Addr);
183 /// getPointerToGlobalIfAvailable - This returns the address of the specified
184 /// global value if it is has already been codegen'd, otherwise it returns
187 void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
189 /// getPointerToGlobal - This returns the address of the specified global
190 /// value. This may involve code generation if it's a function.
192 void *getPointerToGlobal(const GlobalValue *GV);
194 /// getPointerToFunction - The different EE's represent function bodies in
195 /// different ways. They should each implement this to say what a function
196 /// pointer should look like.
198 virtual void *getPointerToFunction(Function *F) = 0;
200 /// getPointerToFunctionOrStub - If the specified function has been
201 /// code-gen'd, return a pointer to the function. If not, compile it, or use
202 /// a stub to implement lazy compilation if available.
204 virtual void *getPointerToFunctionOrStub(Function *F) {
205 // Default implementation, just codegen the function.
206 return getPointerToFunction(F);
209 /// getGlobalValueAtAddress - Return the LLVM global value object that starts
210 /// at the specified address.
212 const GlobalValue *getGlobalValueAtAddress(void *Addr);
215 void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr,
217 void InitializeMemory(const Constant *Init, void *Addr);
219 /// recompileAndRelinkFunction - This method is used to force a function
220 /// which has already been compiled to be compiled again, possibly
221 /// after it has been modified. Then the entry to the old copy is overwritten
222 /// with a branch to the new copy. If there was no old copy, this acts
223 /// just like VM::getPointerToFunction().
225 virtual void *recompileAndRelinkFunction(Function *F) = 0;
227 /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
228 /// corresponding to the machine code emitted to execute this function, useful
229 /// for garbage-collecting generated code.
231 virtual void freeMachineCodeForFunction(Function *F) = 0;
233 /// getOrEmitGlobalVariable - Return the address of the specified global
234 /// variable, possibly emitting it to memory if needed. This is used by the
236 virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
237 return getPointerToGlobal((GlobalValue*)GV);
240 /// DisableLazyCompilation - If called, the JIT will abort if lazy compilation
241 // is ever attempted.
242 void DisableLazyCompilation() {
243 LazyCompilationDisabled = true;
245 bool isLazyCompilationDisabled() const {
246 return LazyCompilationDisabled;
250 /// InstallLazyFunctionCreator - If an unknown function is needed, the
251 /// specified function pointer is invoked to create it. If it returns null,
252 /// the JIT will abort.
253 void InstallLazyFunctionCreator(void* (*P)(const std::string &)) {
254 LazyFunctionCreator = P;
257 /// InstallExceptionTableRegister - The JIT will use the given function
258 /// to register the exception tables it generates.
259 static void InstallExceptionTableRegister(void (*F)(void*)) {
260 ExceptionTableRegister = F;
263 /// RegisterTable - Registers the given pointer as an exception table. It uses
264 /// the ExceptionTableRegister function.
265 static void RegisterTable(void* res) {
266 if (ExceptionTableRegister)
267 ExceptionTableRegister(res);
271 explicit ExecutionEngine(ModuleProvider *P);
275 // EmitGlobalVariable - This method emits the specified global variable to the
276 // address specified in GlobalAddresses, or allocates new memory if it's not
277 // already in the map.
278 void EmitGlobalVariable(const GlobalVariable *GV);
280 GenericValue getConstantValue(const Constant *C);
281 void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr,
285 } // End llvm namespace