Support jump tables when in PIC relocation model
[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 #include <string>
22 #include "llvm/System/Mutex.h"
23
24 namespace llvm {
25
26 union GenericValue;
27 class Constant;
28 class Function;
29 class GlobalVariable;
30 class GlobalValue;
31 class Module;
32 class ModuleProvider;
33 class TargetData;
34 class Type;
35 class MutexGuard;
36
37 class ExecutionEngineState {
38 private:
39   /// GlobalAddressMap - A mapping between LLVM global values and their
40   /// actualized version...
41   std::map<const GlobalValue*, void *> GlobalAddressMap;
42
43   /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap,
44   /// used to convert raw addresses into the LLVM global value that is emitted
45   /// at the address.  This map is not computed unless getGlobalValueAtAddress
46   /// is called at some point.
47   std::map<void *, const GlobalValue*> GlobalAddressReverseMap;
48
49 public:
50   std::map<const GlobalValue*, void *> &
51   getGlobalAddressMap(const MutexGuard &locked) {
52     return GlobalAddressMap;
53   }
54
55   std::map<void*, const GlobalValue*> & 
56   getGlobalAddressReverseMap(const MutexGuard& locked) {
57     return GlobalAddressReverseMap;
58   }
59 };
60
61
62 class ExecutionEngine {
63   Module &CurMod;
64   const TargetData *TD;
65
66   ExecutionEngineState state;
67
68 protected:
69   ModuleProvider *MP;
70
71   void setTargetData(const TargetData *td) {
72     TD = td;
73   }
74
75   // To avoid having libexecutionengine depend on the JIT and interpreter
76   // libraries, the JIT and Interpreter set these functions to ctor pointers
77   // at startup time if they are linked in.
78   typedef ExecutionEngine *(*EECtorFn)(ModuleProvider*);
79   static EECtorFn JITCtor, InterpCtor;
80     
81 public:
82   /// lock - This lock is protects the ExecutionEngine, JIT, JITResolver and
83   /// JITEmitter classes.  It must be held while changing the internal state of
84   /// any of those classes.
85   sys::Mutex lock; // Used to make this class and subclasses thread-safe
86
87   ExecutionEngine(ModuleProvider *P);
88   ExecutionEngine(Module *M);
89   virtual ~ExecutionEngine();
90
91   Module &getModule() const { return CurMod; }
92   const TargetData *getTargetData() const { return TD; }
93
94   /// create - This is the factory method for creating an execution engine which
95   /// is appropriate for the current machine.
96   static ExecutionEngine *create(ModuleProvider *MP,
97                                  bool ForceInterpreter = false);
98
99   /// runFunction - Execute the specified function with the specified arguments,
100   /// and return the result.
101   ///
102   virtual GenericValue runFunction(Function *F,
103                                 const std::vector<GenericValue> &ArgValues) = 0;
104
105   /// runStaticConstructorsDestructors - This method is used to execute all of
106   /// the static constructors or destructors for a module, depending on the
107   /// value of isDtors.
108   void runStaticConstructorsDestructors(bool isDtors);
109   
110   
111   /// runFunctionAsMain - This is a helper function which wraps runFunction to
112   /// handle the common task of starting up main with the specified argc, argv,
113   /// and envp parameters.
114   int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
115                         const char * const * envp);
116
117
118   /// addGlobalMapping - Tell the execution engine that the specified global is
119   /// at the specified location.  This is used internally as functions are JIT'd
120   /// and as global variables are laid out in memory.  It can and should also be
121   /// used by clients of the EE that want to have an LLVM global overlay
122   /// existing data in memory.
123   void addGlobalMapping(const GlobalValue *GV, void *Addr);
124   
125   /// clearAllGlobalMappings - Clear all global mappings and start over again
126   /// use in dynamic compilation scenarios when you want to move globals
127   void clearAllGlobalMappings();
128   
129   /// updateGlobalMapping - Replace an existing mapping for GV with a new
130   /// address.  This updates both maps as required.  If "Addr" is null, the
131   /// entry for the global is removed from the mappings.
132   void updateGlobalMapping(const GlobalValue *GV, void *Addr);
133   
134   /// getPointerToGlobalIfAvailable - This returns the address of the specified
135   /// global value if it is has already been codegen'd, otherwise it returns
136   /// null.
137   ///
138   void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
139
140   /// getPointerToGlobal - This returns the address of the specified global
141   /// value.  This may involve code generation if it's a function.
142   ///
143   void *getPointerToGlobal(const GlobalValue *GV);
144
145   /// getPointerToFunction - The different EE's represent function bodies in
146   /// different ways.  They should each implement this to say what a function
147   /// pointer should look like.
148   ///
149   virtual void *getPointerToFunction(Function *F) = 0;
150
151   /// getPointerToFunctionOrStub - If the specified function has been
152   /// code-gen'd, return a pointer to the function.  If not, compile it, or use
153   /// a stub to implement lazy compilation if available.
154   ///
155   virtual void *getPointerToFunctionOrStub(Function *F) {
156     // Default implementation, just codegen the function.
157     return getPointerToFunction(F);
158   }
159
160   /// getGlobalValueAtAddress - Return the LLVM global value object that starts
161   /// at the specified address.
162   ///
163   const GlobalValue *getGlobalValueAtAddress(void *Addr);
164
165
166   void StoreValueToMemory(GenericValue Val, GenericValue *Ptr, const Type *Ty);
167   void InitializeMemory(const Constant *Init, void *Addr);
168
169   /// recompileAndRelinkFunction - This method is used to force a function
170   /// which has already been compiled to be compiled again, possibly
171   /// after it has been modified. Then the entry to the old copy is overwritten
172   /// with a branch to the new copy. If there was no old copy, this acts
173   /// just like VM::getPointerToFunction().
174   ///
175   virtual void *recompileAndRelinkFunction(Function *F) = 0;
176
177   /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
178   /// corresponding to the machine code emitted to execute this function, useful
179   /// for garbage-collecting generated code.
180   ///
181   virtual void freeMachineCodeForFunction(Function *F) = 0;
182
183   /// getOrEmitGlobalVariable - Return the address of the specified global
184   /// variable, possibly emitting it to memory if needed.  This is used by the
185   /// Emitter.
186   virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
187     return getPointerToGlobal((GlobalValue*)GV);
188   }
189
190 protected:
191   void emitGlobals();
192
193   // EmitGlobalVariable - This method emits the specified global variable to the
194   // address specified in GlobalAddresses, or allocates new memory if it's not
195   // already in the map.
196   void EmitGlobalVariable(const GlobalVariable *GV);
197
198   GenericValue getConstantValue(const Constant *C);
199   GenericValue LoadValueFromMemory(GenericValue *Ptr, const Type *Ty);
200 };
201
202 } // End llvm namespace
203
204 #endif