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
34 class IntrinsicLowering;
36 class ExecutionEngine {
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;
52 void setTargetData(const TargetData &td) {
57 ExecutionEngine(ModuleProvider *P);
58 ExecutionEngine(Module *M);
59 virtual ~ExecutionEngine();
61 Module &getModule() const { return CurMod; }
62 const TargetData &getTargetData() const { return *TD; }
64 /// create - This is the factory method for creating an execution engine which
65 /// is appropriate for the current machine. If specified, the
66 /// IntrinsicLowering implementation should be allocated on the heap.
67 static ExecutionEngine *create(ModuleProvider *MP, bool ForceInterpreter,
68 IntrinsicLowering *IL = 0);
70 /// runFunction - Execute the specified function with the specified arguments,
71 /// and return the result.
73 virtual GenericValue runFunction(Function *F,
74 const std::vector<GenericValue> &ArgValues) = 0;
76 /// runFunctionAsMain - This is a helper function which wraps runFunction to
77 /// handle the common task of starting up main with the specified argc, argv,
78 /// and envp parameters.
79 int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
80 const char * const * envp);
83 void addGlobalMapping(const GlobalValue *GV, void *Addr) {
84 void *&CurVal = GlobalAddressMap[GV];
85 assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
88 // If we are using the reverse mapping, add it too
89 if (!GlobalAddressReverseMap.empty()) {
90 const GlobalValue *&V = GlobalAddressReverseMap[Addr];
91 assert((V == 0 || GV == 0) && "GlobalMapping already established!");
96 /// clearAllGlobalMappings - Clear all global mappings and start over again
97 /// use in dynamic compilation scenarios when you want to move globals
98 void clearAllGlobalMappings() {
99 GlobalAddressMap.clear();
100 GlobalAddressReverseMap.clear();
103 /// updateGlobalMapping - Replace an existing mapping for GV with a new
104 /// address. This updates both maps as required.
105 void updateGlobalMapping(const GlobalValue *GV, void *Addr) {
106 void *&CurVal = GlobalAddressMap[GV];
107 if (CurVal && !GlobalAddressReverseMap.empty())
108 GlobalAddressReverseMap.erase(CurVal);
111 // If we are using the reverse mapping, add it too
112 if (!GlobalAddressReverseMap.empty()) {
113 const GlobalValue *&V = GlobalAddressReverseMap[Addr];
114 assert((V == 0 || GV == 0) && "GlobalMapping already established!");
119 /// getPointerToGlobalIfAvailable - This returns the address of the specified
120 /// global value if it is available, otherwise it returns null.
122 void *getPointerToGlobalIfAvailable(const GlobalValue *GV) {
123 std::map<const GlobalValue*, void*>::iterator I = GlobalAddressMap.find(GV);
124 return I != GlobalAddressMap.end() ? I->second : 0;
127 /// getPointerToGlobal - This returns the address of the specified global
128 /// value. This may involve code generation if it's a function.
130 void *getPointerToGlobal(const GlobalValue *GV);
132 /// getPointerToFunction - The different EE's represent function bodies in
133 /// different ways. They should each implement this to say what a function
134 /// pointer should look like.
136 virtual void *getPointerToFunction(Function *F) = 0;
138 /// getPointerToFunctionOrStub - If the specified function has been
139 /// code-gen'd, return a pointer to the function. If not, compile it, or use
140 /// a stub to implement lazy compilation if available.
142 virtual void *getPointerToFunctionOrStub(Function *F) {
143 // Default implementation, just codegen the function.
144 return getPointerToFunction(F);
147 /// getGlobalValueAtAddress - Return the LLVM global value object that starts
148 /// at the specified address.
150 const GlobalValue *getGlobalValueAtAddress(void *Addr);
153 void StoreValueToMemory(GenericValue Val, GenericValue *Ptr, const Type *Ty);
154 void InitializeMemory(const Constant *Init, void *Addr);
156 /// recompileAndRelinkFunction - This method is used to force a function
157 /// which has already been compiled to be compiled again, possibly
158 /// after it has been modified. Then the entry to the old copy is overwritten
159 /// with a branch to the new copy. If there was no old copy, this acts
160 /// just like VM::getPointerToFunction().
162 virtual void *recompileAndRelinkFunction(Function *F) = 0;
164 /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
165 /// corresponding to the machine code emitted to execute this function, useful
166 /// for garbage-collecting generated code.
168 virtual void freeMachineCodeForFunction(Function *F) = 0;
170 /// getOrEmitGlobalVariable - Return the address of the specified global
171 /// variable, possibly emitting it to memory if needed. This is used by the
173 virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
174 return getPointerToGlobal((GlobalValue*)GV);
180 // EmitGlobalVariable - This method emits the specified global variable to the
181 // address specified in GlobalAddresses, or allocates new memory if it's not
182 // already in the map.
183 void EmitGlobalVariable(const GlobalVariable *GV);
185 GenericValue getConstantValue(const Constant *C);
186 GenericValue LoadValueFromMemory(GenericValue *Ptr, const Type *Ty);
189 } // End llvm namespace