1 //===- ExecutionEngine.h - Abstract Execution Engine Interface --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the abstract interface that implements execution support
13 //===----------------------------------------------------------------------===//
15 #ifndef EXECUTION_ENGINE_H
16 #define EXECUTION_ENGINE_H
22 #include "llvm/System/Mutex.h"
23 #include "llvm/ADT/SmallVector.h"
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 &locked) {
53 return GlobalAddressMap;
56 std::map<void*, const GlobalValue*> &
57 getGlobalAddressReverseMap(const MutexGuard& locked) {
58 return GlobalAddressReverseMap;
63 class ExecutionEngine {
65 ExecutionEngineState state;
66 bool LazyCompilationDisabled;
68 /// Modules - This is a list of ModuleProvider's that we are JIT'ing from. We
69 /// use a smallvector to optimize for the case where there is only one module.
70 SmallVector<ModuleProvider*, 1> Modules;
72 void setTargetData(const TargetData *td) {
76 // To avoid having libexecutionengine depend on the JIT and interpreter
77 // libraries, the JIT and Interpreter set these functions to ctor pointers
78 // at startup time if they are linked in.
79 typedef ExecutionEngine *(*EECtorFn)(ModuleProvider*, std::string*);
80 static EECtorFn JITCtor, InterpCtor;
83 /// lock - This lock is protects the ExecutionEngine, JIT, JITResolver and
84 /// JITEmitter classes. It must be held while changing the internal state of
85 /// any of those classes.
86 sys::Mutex lock; // Used to make this class and subclasses thread-safe
88 ExecutionEngine(ModuleProvider *P);
89 ExecutionEngine(Module *M);
90 virtual ~ExecutionEngine();
92 const TargetData *getTargetData() const { return TD; }
94 /// addModuleProvider - Add a ModuleProvider to the list of modules that we
95 /// can JIT from. Note that this takes ownership of the ModuleProvider: when
96 /// the ExecutionEngine is destroyed, it destroys the MP as well.
97 void addModuleProvider(ModuleProvider *P) {
101 /// FindFunctionNamed - Search all of the active modules to find the one that
102 /// defines FnName. This is very slow operation and shouldn't be used for
104 Function *FindFunctionNamed(const char *FnName);
106 /// create - This is the factory method for creating an execution engine which
107 /// is appropriate for the current machine.
108 static ExecutionEngine *create(ModuleProvider *MP,
109 bool ForceInterpreter = false,
110 std::string *ErrorStr = 0);
112 /// runFunction - Execute the specified function with the specified arguments,
113 /// and return the result.
115 virtual GenericValue runFunction(Function *F,
116 const std::vector<GenericValue> &ArgValues) = 0;
118 /// runStaticConstructorsDestructors - This method is used to execute all of
119 /// the static constructors or destructors for a module, depending on the
120 /// value of isDtors.
121 void runStaticConstructorsDestructors(bool isDtors);
124 /// runFunctionAsMain - This is a helper function which wraps runFunction to
125 /// handle the common task of starting up main with the specified argc, argv,
126 /// and envp parameters.
127 int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
128 const char * const * envp);
131 /// addGlobalMapping - Tell the execution engine that the specified global is
132 /// at the specified location. This is used internally as functions are JIT'd
133 /// and as global variables are laid out in memory. It can and should also be
134 /// used by clients of the EE that want to have an LLVM global overlay
135 /// existing data in memory.
136 void addGlobalMapping(const GlobalValue *GV, void *Addr);
138 /// clearAllGlobalMappings - Clear all global mappings and start over again
139 /// use in dynamic compilation scenarios when you want to move globals
140 void clearAllGlobalMappings();
142 /// updateGlobalMapping - Replace an existing mapping for GV with a new
143 /// address. This updates both maps as required. If "Addr" is null, the
144 /// entry for the global is removed from the mappings.
145 void updateGlobalMapping(const GlobalValue *GV, void *Addr);
147 /// getPointerToGlobalIfAvailable - This returns the address of the specified
148 /// global value if it is has already been codegen'd, otherwise it returns
151 void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
153 /// getPointerToGlobal - This returns the address of the specified global
154 /// value. This may involve code generation if it's a function.
156 void *getPointerToGlobal(const GlobalValue *GV);
158 /// getPointerToFunction - The different EE's represent function bodies in
159 /// different ways. They should each implement this to say what a function
160 /// pointer should look like.
162 virtual void *getPointerToFunction(Function *F) = 0;
164 /// getPointerToFunctionOrStub - If the specified function has been
165 /// code-gen'd, return a pointer to the function. If not, compile it, or use
166 /// a stub to implement lazy compilation if available.
168 virtual void *getPointerToFunctionOrStub(Function *F) {
169 // Default implementation, just codegen the function.
170 return getPointerToFunction(F);
173 /// getGlobalValueAtAddress - Return the LLVM global value object that starts
174 /// at the specified address.
176 const GlobalValue *getGlobalValueAtAddress(void *Addr);
179 void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr, const Type *Ty);
180 void InitializeMemory(const Constant *Init, void *Addr);
182 /// recompileAndRelinkFunction - This method is used to force a function
183 /// which has already been compiled to be compiled again, possibly
184 /// after it has been modified. Then the entry to the old copy is overwritten
185 /// with a branch to the new copy. If there was no old copy, this acts
186 /// just like VM::getPointerToFunction().
188 virtual void *recompileAndRelinkFunction(Function *F) = 0;
190 /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
191 /// corresponding to the machine code emitted to execute this function, useful
192 /// for garbage-collecting generated code.
194 virtual void freeMachineCodeForFunction(Function *F) = 0;
196 /// getOrEmitGlobalVariable - Return the address of the specified global
197 /// variable, possibly emitting it to memory if needed. This is used by the
199 virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
200 return getPointerToGlobal((GlobalValue*)GV);
203 /// DisableLazyCompilation - If called, the JIT will abort if lazy compilation
204 // is ever attempted.
205 void DisableLazyCompilation() {
206 LazyCompilationDisabled = true;
208 bool isLazyCompilationDisabled() const {
209 return LazyCompilationDisabled;
215 // EmitGlobalVariable - This method emits the specified global variable to the
216 // address specified in GlobalAddresses, or allocates new memory if it's not
217 // already in the map.
218 void EmitGlobalVariable(const GlobalVariable *GV);
220 GenericValue getConstantValue(const Constant *C);
221 void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr,
225 } // End llvm namespace