Cleanups to implement PR135
[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
34 class ExecutionEngine {
35   Module &CurMod;
36   const TargetData *TD;
37
38   // GlobalAddress - A mapping between LLVM global values and their actualized
39   // version...
40   std::map<const GlobalValue*, void *> GlobalAddress;
41 protected:
42   ModuleProvider *MP;
43
44   void setTargetData(const TargetData &td) {
45     TD = &td;
46   }
47
48 public:
49   ExecutionEngine(ModuleProvider *P);
50   ExecutionEngine(Module *M);
51   virtual ~ExecutionEngine();
52   
53   Module &getModule() const { return CurMod; }
54   const TargetData &getTargetData() const { return *TD; }
55
56   /// run - Start execution with the specified function, arguments, and
57   ///       environment.
58   ///
59   virtual GenericValue run(Function *F,
60                            const std::vector<GenericValue> &ArgValues) = 0;
61
62   static ExecutionEngine *create(ModuleProvider *MP, bool ForceInterpreter);
63
64   void addGlobalMapping(const GlobalValue *GV, void *Addr) {
65     void *&CurVal = GlobalAddress[GV];
66     assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
67     CurVal = Addr;
68   }
69
70   /// getPointerToGlobalIfAvailable - This returns the address of the specified
71   /// global value if it is available, otherwise it returns null.
72   ///
73   void *getPointerToGlobalIfAvailable(const GlobalValue *GV) {
74     std::map<const GlobalValue*, void*>::iterator I = GlobalAddress.find(GV);
75     return I != GlobalAddress.end() ? I->second : 0;
76   }
77
78   /// getPointerToGlobal - This returns the address of the specified global
79   /// value.  This may involve code generation if it's a function.
80   ///
81   void *getPointerToGlobal(const GlobalValue *GV);
82
83   /// getPointerToFunction - The different EE's represent function bodies in
84   /// different ways.  They should each implement this to say what a function
85   /// pointer should look like.
86   ///
87   virtual void *getPointerToFunction(Function *F) = 0;
88
89   /// getPointerToFunctionOrStub - If the specified function has been
90   /// code-gen'd, return a pointer to the function.  If not, compile it, or use
91   /// a stub to implement lazy compilation if available.
92   ///
93   virtual void *getPointerToFunctionOrStub(Function *F) {
94     // Default implementation, just codegen the function.
95     return getPointerToFunction(F);
96   }
97
98   void StoreValueToMemory(GenericValue Val, GenericValue *Ptr, const Type *Ty);
99   void InitializeMemory(const Constant *Init, void *Addr);
100
101   /// recompileAndRelinkFunction - This method is used to force a function
102   /// which has already been compiled to be compiled again, possibly
103   /// after it has been modified. Then the entry to the old copy is overwritten
104   /// with a branch to the new copy. If there was no old copy, this acts
105   /// just like VM::getPointerToFunction().
106   ///
107   virtual void *recompileAndRelinkFunction(Function *F) = 0;
108
109   /// getOrEmitGlobalVariable - Return the address of the specified global
110   /// variable, possibly emitting it to memory if needed.  This is used by the
111   /// Emitter.
112   virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
113     return getPointerToGlobal((GlobalValue*)GV);
114   }
115
116 protected:
117   void emitGlobals();
118
119   // EmitGlobalVariable - This method emits the specified global variable to the
120   // address specified in GlobalAddresses, or allocates new memory if it's not
121   // already in the map.
122   void EmitGlobalVariable(const GlobalVariable *GV);
123
124   GenericValue getConstantValue(const Constant *C);
125   GenericValue LoadValueFromMemory(GenericValue *Ptr, const Type *Ty);
126 };
127
128 } // End llvm namespace
129
130 #endif