Add new ExecutionEngine::getGlobalValueAtAddress method, which can efficiently
[oota-llvm.git] / include / llvm / ExecutionEngine / ExecutionEngine.h
1 //===- ExecutionEngine.h - Abstract Execution Engine Interface --*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the abstract interface that implements execution support
11 // for LLVM.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef EXECUTION_ENGINE_H
16 #define EXECUTION_ENGINE_H
17
18 #include <vector>
19 #include <map>
20 #include <cassert>
21
22 namespace llvm {
23
24 union GenericValue;
25 class Constant;
26 class Function;
27 class GlobalVariable;
28 class GlobalValue;
29 class Module;
30 class ModuleProvider;
31 class TargetData;
32 class Type;
33 class IntrinsicLowering;
34
35 class ExecutionEngine {
36   Module &CurMod;
37   const TargetData *TD;
38
39   /// GlobalAddressMap - A mapping between LLVM global values and their
40   /// actualized version...
41   std::map<const GlobalValue*, void *> GlobalAddressMap;
42
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;
48 protected:
49   ModuleProvider *MP;
50
51   void setTargetData(const TargetData &td) {
52     TD = &td;
53   }
54
55 public:
56   ExecutionEngine(ModuleProvider *P);
57   ExecutionEngine(Module *M);
58   virtual ~ExecutionEngine();
59   
60   Module &getModule() const { return CurMod; }
61   const TargetData &getTargetData() const { return *TD; }
62
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);
68
69   /// runFunction - Execute the specified function with the specified arguments,
70   /// and return the result.
71   ///
72   virtual GenericValue runFunction(Function *F,
73                                 const std::vector<GenericValue> &ArgValues) = 0;
74
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);
80
81
82   void addGlobalMapping(const GlobalValue *GV, void *Addr) {
83     void *&CurVal = GlobalAddressMap[GV];
84     assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
85     CurVal = Addr;
86
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!");
91       V = GV;
92     }
93   }
94
95   /// getPointerToGlobalIfAvailable - This returns the address of the specified
96   /// global value if it is available, otherwise it returns null.
97   ///
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;
101   }
102
103   /// getPointerToGlobal - This returns the address of the specified global
104   /// value.  This may involve code generation if it's a function.
105   ///
106   void *getPointerToGlobal(const GlobalValue *GV);
107
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.
111   ///
112   virtual void *getPointerToFunction(Function *F) = 0;
113
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.
117   ///
118   virtual void *getPointerToFunctionOrStub(Function *F) {
119     // Default implementation, just codegen the function.
120     return getPointerToFunction(F);
121   }
122
123   /// getGlobalValueAtAddress - Return the LLVM global value object that starts
124   /// at the specified address.
125   ///
126   const GlobalValue *getGlobalValueAtAddress(void *Addr);
127
128
129   void StoreValueToMemory(GenericValue Val, GenericValue *Ptr, const Type *Ty);
130   void InitializeMemory(const Constant *Init, void *Addr);
131
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().
137   ///
138   virtual void *recompileAndRelinkFunction(Function *F) = 0;
139
140   /// getOrEmitGlobalVariable - Return the address of the specified global
141   /// variable, possibly emitting it to memory if needed.  This is used by the
142   /// Emitter.
143   virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
144     return getPointerToGlobal((GlobalValue*)GV);
145   }
146
147 protected:
148   void emitGlobals();
149
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);
154
155   GenericValue getConstantValue(const Constant *C);
156   GenericValue LoadValueFromMemory(GenericValue *Ptr, const Type *Ty);
157 };
158
159 } // End llvm namespace
160
161 #endif