Rename 'run' to 'runFunction' to emphasize that it is usable to run any
[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   /// create - This is the factory method for creating an execution engine which
57   /// is appropriate for the current machine.
58   static ExecutionEngine *create(ModuleProvider *MP, bool ForceInterpreter);
59
60   /// runFunction - Execute the specified function with the specified arguments,
61   /// and return the result.
62   ///
63   virtual GenericValue runFunction(Function *F,
64                                 const std::vector<GenericValue> &ArgValues) = 0;
65
66   void addGlobalMapping(const GlobalValue *GV, void *Addr) {
67     void *&CurVal = GlobalAddress[GV];
68     assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
69     CurVal = Addr;
70   }
71
72   /// getPointerToGlobalIfAvailable - This returns the address of the specified
73   /// global value if it is available, otherwise it returns null.
74   ///
75   void *getPointerToGlobalIfAvailable(const GlobalValue *GV) {
76     std::map<const GlobalValue*, void*>::iterator I = GlobalAddress.find(GV);
77     return I != GlobalAddress.end() ? I->second : 0;
78   }
79
80   /// getPointerToGlobal - This returns the address of the specified global
81   /// value.  This may involve code generation if it's a function.
82   ///
83   void *getPointerToGlobal(const GlobalValue *GV);
84
85   /// getPointerToFunction - The different EE's represent function bodies in
86   /// different ways.  They should each implement this to say what a function
87   /// pointer should look like.
88   ///
89   virtual void *getPointerToFunction(Function *F) = 0;
90
91   /// getPointerToFunctionOrStub - If the specified function has been
92   /// code-gen'd, return a pointer to the function.  If not, compile it, or use
93   /// a stub to implement lazy compilation if available.
94   ///
95   virtual void *getPointerToFunctionOrStub(Function *F) {
96     // Default implementation, just codegen the function.
97     return getPointerToFunction(F);
98   }
99
100   void StoreValueToMemory(GenericValue Val, GenericValue *Ptr, const Type *Ty);
101   void InitializeMemory(const Constant *Init, void *Addr);
102
103   /// recompileAndRelinkFunction - This method is used to force a function
104   /// which has already been compiled to be compiled again, possibly
105   /// after it has been modified. Then the entry to the old copy is overwritten
106   /// with a branch to the new copy. If there was no old copy, this acts
107   /// just like VM::getPointerToFunction().
108   ///
109   virtual void *recompileAndRelinkFunction(Function *F) = 0;
110
111   /// getOrEmitGlobalVariable - Return the address of the specified global
112   /// variable, possibly emitting it to memory if needed.  This is used by the
113   /// Emitter.
114   virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
115     return getPointerToGlobal((GlobalValue*)GV);
116   }
117
118 protected:
119   void emitGlobals();
120
121   // EmitGlobalVariable - This method emits the specified global variable to the
122   // address specified in GlobalAddresses, or allocates new memory if it's not
123   // already in the map.
124   void EmitGlobalVariable(const GlobalVariable *GV);
125
126   GenericValue getConstantValue(const Constant *C);
127   GenericValue LoadValueFromMemory(GenericValue *Ptr, const Type *Ty);
128 };
129
130 } // End llvm namespace
131
132 #endif