This is the proper code for this method, thanks to Reid for getting CVS working
[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 #include <string>
22
23 namespace llvm {
24
25 union GenericValue;
26 class Constant;
27 class Function;
28 class GlobalVariable;
29 class GlobalValue;
30 class Module;
31 class ModuleProvider;
32 class TargetData;
33 class Type;
34 class IntrinsicLowering;
35
36 class ExecutionEngine {
37   Module &CurMod;
38   const TargetData *TD;
39
40   /// GlobalAddressMap - A mapping between LLVM global values and their
41   /// actualized version...
42   std::map<const GlobalValue*, void *> GlobalAddressMap;
43
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;
49 protected:
50   ModuleProvider *MP;
51
52   void setTargetData(const TargetData &td) {
53     TD = &td;
54   }
55
56 public:
57   ExecutionEngine(ModuleProvider *P);
58   ExecutionEngine(Module *M);
59   virtual ~ExecutionEngine();
60   
61   Module &getModule() const { return CurMod; }
62   const TargetData &getTargetData() const { return *TD; }
63
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);
69
70   /// runFunction - Execute the specified function with the specified arguments,
71   /// and return the result.
72   ///
73   virtual GenericValue runFunction(Function *F,
74                                 const std::vector<GenericValue> &ArgValues) = 0;
75
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);
81
82
83   void addGlobalMapping(const GlobalValue *GV, void *Addr) {
84     void *&CurVal = GlobalAddressMap[GV];
85     assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
86     CurVal = Addr;
87
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!");
92       V = GV;
93     }
94   }
95
96   /// updateGlobalMapping - Replace an existing mapping for GV with a new
97   /// address.  This updates both maps as required.
98   void updateGlobalMapping(const GlobalValue *GV, void *Addr) {
99     void *&CurVal = GlobalAddressMap[GV];
100     if (CurVal && !GlobalAddressReverseMap.empty())
101       GlobalAddressReverseMap.erase(CurVal);
102     CurVal = Addr;
103
104     // If we are using the reverse mapping, add it too
105     if (!GlobalAddressReverseMap.empty()) {
106       const GlobalValue *&V = GlobalAddressReverseMap[Addr];
107       assert((V == 0 || GV == 0) && "GlobalMapping already established!");
108       V = GV;
109     }
110   }
111
112   /// getPointerToGlobalIfAvailable - This returns the address of the specified
113   /// global value if it is available, otherwise it returns null.
114   ///
115   void *getPointerToGlobalIfAvailable(const GlobalValue *GV) {
116     std::map<const GlobalValue*, void*>::iterator I = GlobalAddressMap.find(GV);
117     return I != GlobalAddressMap.end() ? I->second : 0;
118   }
119
120   /// getPointerToGlobal - This returns the address of the specified global
121   /// value.  This may involve code generation if it's a function.
122   ///
123   void *getPointerToGlobal(const GlobalValue *GV);
124
125   /// getPointerToFunction - The different EE's represent function bodies in
126   /// different ways.  They should each implement this to say what a function
127   /// pointer should look like.
128   ///
129   virtual void *getPointerToFunction(Function *F) = 0;
130
131   /// getPointerToFunctionOrStub - If the specified function has been
132   /// code-gen'd, return a pointer to the function.  If not, compile it, or use
133   /// a stub to implement lazy compilation if available.
134   ///
135   virtual void *getPointerToFunctionOrStub(Function *F) {
136     // Default implementation, just codegen the function.
137     return getPointerToFunction(F);
138   }
139
140   /// getGlobalValueAtAddress - Return the LLVM global value object that starts
141   /// at the specified address.
142   ///
143   const GlobalValue *getGlobalValueAtAddress(void *Addr);
144
145
146   void StoreValueToMemory(GenericValue Val, GenericValue *Ptr, const Type *Ty);
147   void InitializeMemory(const Constant *Init, void *Addr);
148
149   /// recompileAndRelinkFunction - This method is used to force a function
150   /// which has already been compiled to be compiled again, possibly
151   /// after it has been modified. Then the entry to the old copy is overwritten
152   /// with a branch to the new copy. If there was no old copy, this acts
153   /// just like VM::getPointerToFunction().
154   ///
155   virtual void *recompileAndRelinkFunction(Function *F) = 0;
156
157   /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
158   /// corresponding to the machine code emitted to execute this function, useful
159   /// for garbage-collecting generated code.
160   ///
161   virtual void freeMachineCodeForFunction(Function *F) = 0;
162
163   /// getOrEmitGlobalVariable - Return the address of the specified global
164   /// variable, possibly emitting it to memory if needed.  This is used by the
165   /// Emitter.
166   virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
167     return getPointerToGlobal((GlobalValue*)GV);
168   }
169
170 protected:
171   void emitGlobals();
172
173   // EmitGlobalVariable - This method emits the specified global variable to the
174   // address specified in GlobalAddresses, or allocates new memory if it's not
175   // already in the map.
176   void EmitGlobalVariable(const GlobalVariable *GV);
177
178   GenericValue getConstantValue(const Constant *C);
179   GenericValue LoadValueFromMemory(GenericValue *Ptr, const Type *Ty);
180 };
181
182 } // End llvm namespace
183
184 #endif