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
33 class IntrinsicLowering;
35 class ExecutionEngine {
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;
51 void setTargetData(const TargetData &td) {
56 ExecutionEngine(ModuleProvider *P);
57 ExecutionEngine(Module *M);
58 virtual ~ExecutionEngine();
60 Module &getModule() const { return CurMod; }
61 const TargetData &getTargetData() const { return *TD; }
63 /// create - This is the factory method for creating an execution engine which
64 /// is appropriate for the current machine. If specified, the
65 /// IntrinsicLowering implementation should be allocated on the heap.
66 static ExecutionEngine *create(ModuleProvider *MP, bool ForceInterpreter,
67 IntrinsicLowering *IL = 0);
69 /// runFunction - Execute the specified function with the specified arguments,
70 /// and return the result.
72 virtual GenericValue runFunction(Function *F,
73 const std::vector<GenericValue> &ArgValues) = 0;
75 /// runFunctionAsMain - This is a helper function which wraps runFunction to
76 /// handle the common task of starting up main with the specified argc, argv,
77 /// and envp parameters.
78 int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
79 const char * const * envp);
82 void addGlobalMapping(const GlobalValue *GV, void *Addr) {
83 void *&CurVal = GlobalAddressMap[GV];
84 assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
87 // If we are using the reverse mapping, add it too
88 if (!GlobalAddressReverseMap.empty()) {
89 const GlobalValue *&V = GlobalAddressReverseMap[Addr];
90 assert((V == 0 || GV == 0) && "GlobalMapping already established!");
95 /// getPointerToGlobalIfAvailable - This returns the address of the specified
96 /// global value if it is available, otherwise it returns null.
98 void *getPointerToGlobalIfAvailable(const GlobalValue *GV) {
99 std::map<const GlobalValue*, void*>::iterator I = GlobalAddressMap.find(GV);
100 return I != GlobalAddressMap.end() ? I->second : 0;
103 /// getPointerToGlobal - This returns the address of the specified global
104 /// value. This may involve code generation if it's a function.
106 void *getPointerToGlobal(const GlobalValue *GV);
108 /// getPointerToFunction - The different EE's represent function bodies in
109 /// different ways. They should each implement this to say what a function
110 /// pointer should look like.
112 virtual void *getPointerToFunction(Function *F) = 0;
114 /// getPointerToFunctionOrStub - If the specified function has been
115 /// code-gen'd, return a pointer to the function. If not, compile it, or use
116 /// a stub to implement lazy compilation if available.
118 virtual void *getPointerToFunctionOrStub(Function *F) {
119 // Default implementation, just codegen the function.
120 return getPointerToFunction(F);
123 /// getGlobalValueAtAddress - Return the LLVM global value object that starts
124 /// at the specified address.
126 const GlobalValue *getGlobalValueAtAddress(void *Addr);
129 void StoreValueToMemory(GenericValue Val, GenericValue *Ptr, const Type *Ty);
130 void InitializeMemory(const Constant *Init, void *Addr);
132 /// recompileAndRelinkFunction - This method is used to force a function
133 /// which has already been compiled to be compiled again, possibly
134 /// after it has been modified. Then the entry to the old copy is overwritten
135 /// with a branch to the new copy. If there was no old copy, this acts
136 /// just like VM::getPointerToFunction().
138 virtual void *recompileAndRelinkFunction(Function *F) = 0;
140 /// getOrEmitGlobalVariable - Return the address of the specified global
141 /// variable, possibly emitting it to memory if needed. This is used by the
143 virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
144 return getPointerToGlobal((GlobalValue*)GV);
150 // EmitGlobalVariable - This method emits the specified global variable to the
151 // address specified in GlobalAddresses, or allocates new memory if it's not
152 // already in the map.
153 void EmitGlobalVariable(const GlobalVariable *GV);
155 GenericValue getConstantValue(const Constant *C);
156 GenericValue LoadValueFromMemory(GenericValue *Ptr, const Type *Ty);
159 } // End llvm namespace