Add new method to check to see if a global is available
[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 <vector>
12 #include <string>
13 #include <map>
14 class Constant;
15 class Type;
16 class GlobalValue;
17 class Function;
18 class Module;
19 class TargetData;
20 union GenericValue;
21
22 class ExecutionEngine {
23   Module &CurMod;
24   const TargetData *TD;
25
26 protected:
27   // GlobalAddress - A mapping between LLVM global values and their actualized
28   // version...
29   std::map<const GlobalValue*, void *> GlobalAddress;
30
31   void setTargetData(const TargetData &td) {
32     TD = &td;
33     emitGlobals();
34   }
35 public:
36   ExecutionEngine(Module *M) : CurMod(*M) {
37     assert(M && "Module is null?");
38   }
39   virtual ~ExecutionEngine();
40   
41   Module &getModule() const { return CurMod; }
42   const TargetData &getTargetData() const { return *TD; }
43
44   /// run - Start execution with the specified function and arguments.
45   ///
46   virtual int run(const std::string &FnName,
47                   const std::vector<std::string> &Args) = 0;
48
49   /// createJIT - Create an return a new JIT compiler if there is one available
50   /// for the current target.  Otherwise it returns null.
51   ///
52   static ExecutionEngine *createJIT(Module *M, unsigned Config);
53
54   /// createInterpreter - Create a new interpreter object.  This can never fail.
55   ///
56   static ExecutionEngine *createInterpreter(Module *M, unsigned Config,
57                                             bool DebugMode, 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(const Function *F) = 0;
83
84 private:
85   void emitGlobals();
86
87 public:   // FIXME: protected:   // API shared among subclasses
88   GenericValue getConstantValue(const Constant *C);
89   void StoreValueToMemory(GenericValue Val, GenericValue *Ptr, const Type *Ty);
90   GenericValue LoadValueFromMemory(GenericValue *Ptr, const Type *Ty);
91   void *CreateArgv(const std::vector<std::string> &InputArgv);
92   void InitializeMemory(const Constant *Init, void *Addr);
93 };
94
95 #endif