Chris Lattner | 836f675 | 2002-12-24 00:01:22 +0000 | [diff] [blame] | 1 | //===-- VM.h - Definitions for Virtual Machine ------------------*- C++ -*-===// |
| 2 | // |
| 3 | // This file defines the top-level Virtual Machine data structure. |
| 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
| 7 | #ifndef VM_H |
| 8 | #define VM_H |
| 9 | |
| 10 | #include "../ExecutionEngine.h" |
| 11 | #include "llvm/PassManager.h" |
| 12 | #include <map> |
| 13 | |
| 14 | class Function; |
| 15 | class GlobalValue; |
| 16 | class Constant; |
| 17 | class TargetMachine; |
| 18 | class MachineCodeEmitter; |
| 19 | |
| 20 | class VM : public ExecutionEngine { |
| 21 | TargetMachine &TM; // The current target we are compiling to |
| 22 | PassManager PM; // Passes to compile a function |
| 23 | MachineCodeEmitter *MCE; // MCE object |
| 24 | |
| 25 | // FunctionRefs - A mapping between addresses that refer to unresolved |
| 26 | // functions and the LLVM function object itself. This is used by the fault |
| 27 | // handler to lazily patch up references... |
| 28 | // |
| 29 | std::map<void*, Function*> FunctionRefs; |
Chris Lattner | 22080f9 | 2003-05-14 13:53:40 +0000 | [diff] [blame^] | 30 | |
Chris Lattner | 836f675 | 2002-12-24 00:01:22 +0000 | [diff] [blame] | 31 | public: |
| 32 | VM(Module *M, TargetMachine *tm); |
| 33 | ~VM(); |
| 34 | |
| 35 | /// run - Start execution with the specified function and arguments. |
| 36 | /// |
| 37 | virtual int run(const std::string &FnName, |
| 38 | const std::vector<std::string> &Args); |
| 39 | |
| 40 | void addFunctionRef(void *Ref, Function *F) { |
| 41 | FunctionRefs[Ref] = F; |
| 42 | } |
| 43 | |
| 44 | const std::string &getFunctionReferencedName(void *RefAddr); |
| 45 | |
| 46 | void *resolveFunctionReference(void *RefAddr); |
| 47 | |
Chris Lattner | 0d448c0 | 2003-01-13 01:00:48 +0000 | [diff] [blame] | 48 | /// getPointerToNamedFunction - This method returns the address of the |
| 49 | /// specified function by using the dlsym function call. As such it is only |
| 50 | /// useful for resolving library symbols, not code generated symbols. |
| 51 | /// |
| 52 | void *getPointerToNamedFunction(const std::string &Name); |
| 53 | |
Chris Lattner | c309a76 | 2003-05-08 21:34:11 +0000 | [diff] [blame] | 54 | // CompilationCallback - Invoked the first time that a call site is found, |
| 55 | // which causes lazy compilation of the target function. |
| 56 | // |
| 57 | static void CompilationCallback(); |
Chris Lattner | 22080f9 | 2003-05-14 13:53:40 +0000 | [diff] [blame^] | 58 | |
| 59 | /// runAtExitHandlers - Before exiting the program, at_exit functions must be |
| 60 | /// called. This method calls them. |
| 61 | /// |
| 62 | static void runAtExitHandlers(); |
| 63 | |
Chris Lattner | 836f675 | 2002-12-24 00:01:22 +0000 | [diff] [blame] | 64 | private: |
| 65 | static MachineCodeEmitter *createEmitter(VM &V); |
| 66 | void setupPassManager(); |
| 67 | void *getPointerToFunction(const Function *F); |
Chris Lattner | c309a76 | 2003-05-08 21:34:11 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 836f675 | 2002-12-24 00:01:22 +0000 | [diff] [blame] | 69 | void registerCallback(); |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 70 | |
| 71 | /// emitStubForFunction - This method is used by the JIT when it needs to emit |
| 72 | /// the address of a function for a function whose code has not yet been |
| 73 | /// generated. In order to do this, it generates a stub which jumps to the |
| 74 | /// lazy function compiler, which will eventually get fixed to call the |
| 75 | /// function directly. |
| 76 | /// |
| 77 | void *emitStubForFunction(const Function &F); |
Chris Lattner | 836f675 | 2002-12-24 00:01:22 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | #endif |