03fc6d6a9443b0dec30837d0013211ad651881b1
[oota-llvm.git] / include / llvm / ExecutionEngine / ExecutionEngine.h
1 //===- ExecutionEngine.h - Abstract Execution Engine Interface --*- C++ -*-===//
2 //
3 // This file defines the abstract interface that implements execution support
4 // for LLVM.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef EXECUTION_ENGINE_H
9 #define EXECUTION_ENGINE_H
10
11 #include "llvm/ModuleProvider.h"
12 #include <vector>
13 #include <string>
14 #include <map>
15 #include <cassert>
16 class Constant;
17 class Function;
18 union GenericValue;
19 class GlobalValue;
20 class Module;
21 class TargetData;
22 class Type;
23
24 class ExecutionEngine {
25   Module &CurMod;
26   const TargetData *TD;
27
28 protected:
29   ModuleProvider *MP;
30   // GlobalAddress - A mapping between LLVM global values and their actualized
31   // version...
32   std::map<const GlobalValue*, void *> GlobalAddress;
33
34   void setTargetData(const TargetData &td) {
35     TD = &td;
36   }
37
38 public:
39   ExecutionEngine(ModuleProvider *P) : CurMod(*(P->getModule())), MP(P) {
40     assert(P && "ModuleProvider is null?");
41   }
42   ExecutionEngine(Module *M) : CurMod(*M), MP(0) {
43      assert(M && "Module is null?");
44   }
45   virtual ~ExecutionEngine();
46   
47   Module &getModule() const { return CurMod; }
48   const TargetData &getTargetData() const { return *TD; }
49
50   /// run - Start execution with the specified function, arguments, and
51   ///       environment.
52   ///
53   virtual GenericValue run(Function *F,
54                            const std::vector<GenericValue> &ArgValues) = 0;
55
56   static ExecutionEngine *create(ModuleProvider *MP, bool ForceInterpreter,
57                                  bool TraceMode);
58
59   void addGlobalMapping(const Function *F, void *Addr) {
60     void *&CurVal = GlobalAddress[(const GlobalValue*)F];
61     assert(CurVal == 0 && "GlobalMapping already established!");
62     CurVal = Addr;
63   }
64
65   // getPointerToGlobalIfAvailable - This returns the address of the specified
66   // global value if it is available, otherwise it returns null.
67   //
68   void *getPointerToGlobalIfAvailable(const GlobalValue *GV) {
69     std::map<const GlobalValue*, void*>::iterator I = GlobalAddress.find(GV);
70     return I != GlobalAddress.end() ? I->second : 0;
71   }
72
73   // getPointerToGlobal - This returns the address of the specified global
74   // value.  This may involve code generation if it's a function.
75   //
76   void *getPointerToGlobal(const GlobalValue *GV);
77
78   // getPointerToFunction - The different EE's represent function bodies in
79   // different ways.  They should each implement this to say what a function
80   // pointer should look like.
81   //
82   virtual void *getPointerToFunction(Function *F) = 0;
83
84   void StoreValueToMemory(GenericValue Val, GenericValue *Ptr, const Type *Ty);
85   void InitializeMemory(const Constant *Init, void *Addr);
86
87 protected:
88   void emitGlobals();
89   GenericValue getConstantValue(const Constant *C);
90   GenericValue LoadValueFromMemory(GenericValue *Ptr, const Type *Ty);
91 };
92
93 #endif