Interpreter cleanups:
[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 #include <cassert>
15 class Constant;
16 class Type;
17 class GlobalValue;
18 class Function;
19 class Module;
20 class TargetData;
21 union GenericValue;
22
23 class ExecutionEngine {
24   Module &CurMod;
25   const TargetData *TD;
26
27 protected:
28   // GlobalAddress - A mapping between LLVM global values and their actualized
29   // version...
30   std::map<const GlobalValue*, void *> GlobalAddress;
31
32   void setTargetData(const TargetData &td) {
33     TD = &td;
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, arguments, and
45   ///       environment.
46   ///
47   virtual int run(const std::string &FnName,
48                   const std::vector<std::string> &Args,
49                   const char ** envp) = 0;
50
51   static ExecutionEngine *create (Module *M, bool ForceInterpreter,
52                                   bool TraceMode);
53
54   /// createJIT - Create an return a new JIT compiler if there is one available
55   /// for the current target.  Otherwise it returns null.
56   ///
57   static ExecutionEngine *createJIT(Module *M);
58
59   /// createInterpreter - Create a new interpreter object.  This can never fail.
60   ///
61   static ExecutionEngine *createInterpreter(Module *M, bool TraceMode);
62
63   void addGlobalMapping(const Function *F, void *Addr) {
64     void *&CurVal = GlobalAddress[(const GlobalValue*)F];
65     assert(CurVal == 0 && "GlobalMapping already established!");
66     CurVal = Addr;
67   }
68
69   // getPointerToGlobalIfAvailable - This returns the address of the specified
70   // global value if it is available, otherwise it returns null.
71   //
72   void *getPointerToGlobalIfAvailable(const GlobalValue *GV) {
73     std::map<const GlobalValue*, void*>::iterator I = GlobalAddress.find(GV);
74     return I != GlobalAddress.end() ? I->second : 0;
75   }
76
77   // getPointerToGlobal - This returns the address of the specified global
78   // value.  This may involve code generation if it's a function.
79   //
80   void *getPointerToGlobal(const GlobalValue *GV);
81
82   // getPointerToFunction - The different EE's represent function bodies in
83   // different ways.  They should each implement this to say what a function
84   // pointer should look like.
85   //
86   virtual void *getPointerToFunction(Function *F) = 0;
87
88 protected:
89   void emitGlobals();
90
91 public:   // FIXME: protected:   // API shared among subclasses
92   GenericValue getConstantValue(const Constant *C);
93   void StoreValueToMemory(GenericValue Val, GenericValue *Ptr, const Type *Ty);
94   GenericValue LoadValueFromMemory(GenericValue *Ptr, const Type *Ty);
95   void *CreateArgv(const std::vector<std::string> &InputArgv);
96   void InitializeMemory(const Constant *Init, void *Addr);
97 };
98
99 #endif