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 virtual 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 virtual Module* removeModuleProvider(ModuleProvider *P,
141 std::string *ErrInfo = 0);
143 /// FindFunctionNamed - Search all of the active modules to find the one that
144 /// defines FnName. This is very slow operation and shouldn't be used for
146 Function *FindFunctionNamed(const char *FnName);
148 /// runFunction - Execute the specified function with the specified arguments,
149 /// and return the result.
151 virtual GenericValue runFunction(Function *F,
152 const std::vector<GenericValue> &ArgValues) = 0;
154 /// runStaticConstructorsDestructors - This method is used to execute all of
155 /// the static constructors or destructors for a module, depending on the
156 /// value of isDtors.
157 void runStaticConstructorsDestructors(bool isDtors);
160 /// runFunctionAsMain - This is a helper function which wraps runFunction to
161 /// handle the common task of starting up main with the specified argc, argv,
162 /// and envp parameters.
163 int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
164 const char * const * envp);
167 /// addGlobalMapping - Tell the execution engine that the specified global is
168 /// at the specified location. This is used internally as functions are JIT'd
169 /// and as global variables are laid out in memory. It can and should also be
170 /// used by clients of the EE that want to have an LLVM global overlay
171 /// existing data in memory.
172 void addGlobalMapping(const GlobalValue *GV, void *Addr);
174 /// clearAllGlobalMappings - Clear all global mappings and start over again
175 /// use in dynamic compilation scenarios when you want to move globals
176 void clearAllGlobalMappings();
178 /// clearGlobalMappingsFromModule - Clear all global mappings that came from a
179 /// particular module, because it has been removed from the JIT.
180 void clearGlobalMappingsFromModule(Module *M);
182 /// updateGlobalMapping - Replace an existing mapping for GV with a new
183 /// address. This updates both maps as required. If "Addr" is null, the
184 /// entry for the global is removed from the mappings. This returns the old
185 /// value of the pointer, or null if it was not in the map.
186 void *updateGlobalMapping(const GlobalValue *GV, void *Addr);
188 /// getPointerToGlobalIfAvailable - This returns the address of the specified
189 /// global value if it is has already been codegen'd, otherwise it returns
192 void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
194 /// getPointerToGlobal - This returns the address of the specified global
195 /// value. This may involve code generation if it's a function.
197 void *getPointerToGlobal(const GlobalValue *GV);
199 /// getPointerToFunction - The different EE's represent function bodies in
200 /// different ways. They should each implement this to say what a function
201 /// pointer should look like.
203 virtual void *getPointerToFunction(Function *F) = 0;
205 /// getPointerToFunctionOrStub - If the specified function has been
206 /// code-gen'd, return a pointer to the function. If not, compile it, or use
207 /// a stub to implement lazy compilation if available.
209 virtual void *getPointerToFunctionOrStub(Function *F) {
210 // Default implementation, just codegen the function.
211 return getPointerToFunction(F);
214 /// getGlobalValueAtAddress - Return the LLVM global value object that starts
215 /// at the specified address.
217 const GlobalValue *getGlobalValueAtAddress(void *Addr);
220 void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr,
222 void InitializeMemory(const Constant *Init, void *Addr);
224 /// recompileAndRelinkFunction - This method is used to force a function
225 /// which has already been compiled to be compiled again, possibly
226 /// after it has been modified. Then the entry to the old copy is overwritten
227 /// with a branch to the new copy. If there was no old copy, this acts
228 /// just like VM::getPointerToFunction().
230 virtual void *recompileAndRelinkFunction(Function *F) = 0;
232 /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
233 /// corresponding to the machine code emitted to execute this function, useful
234 /// for garbage-collecting generated code.
236 virtual void freeMachineCodeForFunction(Function *F) = 0;
238 /// getOrEmitGlobalVariable - Return the address of the specified global
239 /// variable, possibly emitting it to memory if needed. This is used by the
241 virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
242 return getPointerToGlobal((GlobalValue*)GV);
245 /// DisableLazyCompilation - If called, the JIT will abort if lazy compilation
246 // is ever attempted.
247 void DisableLazyCompilation() {
248 LazyCompilationDisabled = true;
250 bool isLazyCompilationDisabled() const {
251 return LazyCompilationDisabled;
255 /// InstallLazyFunctionCreator - If an unknown function is needed, the
256 /// specified function pointer is invoked to create it. If it returns null,
257 /// the JIT will abort.
258 void InstallLazyFunctionCreator(void* (*P)(const std::string &)) {
259 LazyFunctionCreator = P;
262 /// InstallExceptionTableRegister - The JIT will use the given function
263 /// to register the exception tables it generates.
264 static void InstallExceptionTableRegister(void (*F)(void*)) {
265 ExceptionTableRegister = F;
268 /// RegisterTable - Registers the given pointer as an exception table. It uses
269 /// the ExceptionTableRegister function.
270 static void RegisterTable(void* res) {
271 if (ExceptionTableRegister)
272 ExceptionTableRegister(res);
276 explicit ExecutionEngine(ModuleProvider *P);
280 // EmitGlobalVariable - This method emits the specified global variable to the
281 // address specified in GlobalAddresses, or allocates new memory if it's not
282 // already in the map.
283 void EmitGlobalVariable(const GlobalVariable *GV);
285 GenericValue getConstantValue(const Constant *C);
286 void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr,
290 } // End llvm namespace