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"
37 class ExecutionEngineState {
39 /// GlobalAddressMap - A mapping between LLVM global values and their
40 /// actualized version...
41 std::map<const GlobalValue*, void *> GlobalAddressMap;
43 /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap,
44 /// used to convert raw addresses into the LLVM global value that is emitted
45 /// at the address. This map is not computed unless getGlobalValueAtAddress
46 /// is called at some point.
47 std::map<void *, const GlobalValue*> GlobalAddressReverseMap;
50 std::map<const GlobalValue*, void *> &
51 getGlobalAddressMap(const MutexGuard &locked) {
52 return GlobalAddressMap;
55 std::map<void*, const GlobalValue*> &
56 getGlobalAddressReverseMap(const MutexGuard& locked) {
57 return GlobalAddressReverseMap;
62 class ExecutionEngine {
66 ExecutionEngineState state;
71 void setTargetData(const TargetData *td) {
75 // To avoid having libexecutionengine depend on the JIT and interpreter
76 // libraries, the JIT and Interpreter set these functions to ctor pointers
77 // at startup time if they are linked in.
78 typedef ExecutionEngine *(*EECtorFn)(ModuleProvider*);
79 static EECtorFn JITCtor, InterpCtor;
82 /// lock - This lock is protects the ExecutionEngine, JIT, JITResolver and
83 /// JITEmitter classes. It must be held while changing the internal state of
84 /// any of those classes.
85 sys::Mutex lock; // Used to make this class and subclasses thread-safe
87 ExecutionEngine(ModuleProvider *P);
88 ExecutionEngine(Module *M);
89 virtual ~ExecutionEngine();
91 Module &getModule() const { return CurMod; }
92 const TargetData *getTargetData() const { return TD; }
94 /// create - This is the factory method for creating an execution engine which
95 /// is appropriate for the current machine.
96 static ExecutionEngine *create(ModuleProvider *MP,
97 bool ForceInterpreter = false);
99 /// runFunction - Execute the specified function with the specified arguments,
100 /// and return the result.
102 virtual GenericValue runFunction(Function *F,
103 const std::vector<GenericValue> &ArgValues) = 0;
105 /// runStaticConstructorsDestructors - This method is used to execute all of
106 /// the static constructors or destructors for a module, depending on the
107 /// value of isDtors.
108 void runStaticConstructorsDestructors(bool isDtors);
111 /// runFunctionAsMain - This is a helper function which wraps runFunction to
112 /// handle the common task of starting up main with the specified argc, argv,
113 /// and envp parameters.
114 int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
115 const char * const * envp);
118 /// addGlobalMapping - Tell the execution engine that the specified global is
119 /// at the specified location. This is used internally as functions are JIT'd
120 /// and as global variables are laid out in memory. It can and should also be
121 /// used by clients of the EE that want to have an LLVM global overlay
122 /// existing data in memory.
123 void addGlobalMapping(const GlobalValue *GV, void *Addr);
125 /// clearAllGlobalMappings - Clear all global mappings and start over again
126 /// use in dynamic compilation scenarios when you want to move globals
127 void clearAllGlobalMappings();
129 /// updateGlobalMapping - Replace an existing mapping for GV with a new
130 /// address. This updates both maps as required. If "Addr" is null, the
131 /// entry for the global is removed from the mappings.
132 void updateGlobalMapping(const GlobalValue *GV, void *Addr);
134 /// getPointerToGlobalIfAvailable - This returns the address of the specified
135 /// global value if it is has already been codegen'd, otherwise it returns
138 void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
140 /// getPointerToGlobal - This returns the address of the specified global
141 /// value. This may involve code generation if it's a function.
143 void *getPointerToGlobal(const GlobalValue *GV);
145 /// getPointerToFunction - The different EE's represent function bodies in
146 /// different ways. They should each implement this to say what a function
147 /// pointer should look like.
149 virtual void *getPointerToFunction(Function *F) = 0;
151 /// getPointerToFunctionOrStub - If the specified function has been
152 /// code-gen'd, return a pointer to the function. If not, compile it, or use
153 /// a stub to implement lazy compilation if available.
155 virtual void *getPointerToFunctionOrStub(Function *F) {
156 // Default implementation, just codegen the function.
157 return getPointerToFunction(F);
160 /// getGlobalValueAtAddress - Return the LLVM global value object that starts
161 /// at the specified address.
163 const GlobalValue *getGlobalValueAtAddress(void *Addr);
166 void StoreValueToMemory(GenericValue Val, GenericValue *Ptr, const Type *Ty);
167 void InitializeMemory(const Constant *Init, void *Addr);
169 /// recompileAndRelinkFunction - This method is used to force a function
170 /// which has already been compiled to be compiled again, possibly
171 /// after it has been modified. Then the entry to the old copy is overwritten
172 /// with a branch to the new copy. If there was no old copy, this acts
173 /// just like VM::getPointerToFunction().
175 virtual void *recompileAndRelinkFunction(Function *F) = 0;
177 /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
178 /// corresponding to the machine code emitted to execute this function, useful
179 /// for garbage-collecting generated code.
181 virtual void freeMachineCodeForFunction(Function *F) = 0;
183 /// getOrEmitGlobalVariable - Return the address of the specified global
184 /// variable, possibly emitting it to memory if needed. This is used by the
186 virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
187 return getPointerToGlobal((GlobalValue*)GV);
193 // EmitGlobalVariable - This method emits the specified global variable to the
194 // address specified in GlobalAddresses, or allocates new memory if it's not
195 // already in the map.
196 void EmitGlobalVariable(const GlobalVariable *GV);
198 GenericValue getConstantValue(const Constant *C);
199 GenericValue LoadValueFromMemory(GenericValue *Ptr, const Type *Ty);
202 } // End llvm namespace