First cut at the Code Generator using the TableGen methodology.
[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   }
34 public:
35   ExecutionEngine(Module *M) : CurMod(*M) {
36     assert(M && "Module is null?");
37   }
38   virtual ~ExecutionEngine();
39   
40   Module &getModule() const { return CurMod; }
41   const TargetData &getTargetData() const { return *TD; }
42
43   /// run - Start execution with the specified function and arguments.
44   ///
45   virtual int run(const std::string &FnName,
46                   const std::vector<std::string> &Args) = 0;
47
48   /// createJIT - Create an return a new JIT compiler if there is one available
49   /// for the current target.  Otherwise it returns null.
50   ///
51   static ExecutionEngine *createJIT(Module *M, unsigned Config);
52
53   /// createInterpreter - Create a new interpreter object.  This can never fail.
54   ///
55   static ExecutionEngine *createInterpreter(Module *M, unsigned Config,
56                                             bool DebugMode, bool TraceMode);
57
58   void addGlobalMapping(const Function *F, void *Addr) {
59     void *&CurVal = GlobalAddress[(const GlobalValue*)F];
60     assert(CurVal == 0 && "GlobalMapping already established!");
61     CurVal = Addr;
62   }
63
64   // getPointerToGlobalIfAvailable - This returns the address of the specified
65   // global value if it is available, otherwise it returns null.
66   //
67   void *getPointerToGlobalIfAvailable(const GlobalValue *GV) {
68     std::map<const GlobalValue*, void*>::iterator I = GlobalAddress.find(GV);
69     return I != GlobalAddress.end() ? I->second : 0;
70   }
71
72   // getPointerToGlobal - This returns the address of the specified global
73   // value.  This may involve code generation if it's a function.
74   //
75   void *getPointerToGlobal(const GlobalValue *GV);
76
77   // getPointerToFunction - The different EE's represent function bodies in
78   // different ways.  They should each implement this to say what a function
79   // pointer should look like.
80   //
81   virtual void *getPointerToFunction(const Function *F) = 0;
82
83 protected:
84   void emitGlobals();
85
86 public:   // FIXME: protected:   // API shared among subclasses
87   GenericValue getConstantValue(const Constant *C);
88   void StoreValueToMemory(GenericValue Val, GenericValue *Ptr, const Type *Ty);
89   GenericValue LoadValueFromMemory(GenericValue *Ptr, const Type *Ty);
90   void *CreateArgv(const std::vector<std::string> &InputArgv);
91   void InitializeMemory(const Constant *Init, void *Addr);
92 };
93
94 #endif