Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- JIT.h - Class definition for the JIT --------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame^] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the top-level JIT data structure. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef JIT_H |
| 15 | #define JIT_H |
| 16 | |
| 17 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
| 18 | #include "llvm/PassManager.h" |
| 19 | #include <map> |
| 20 | |
| 21 | namespace llvm { |
| 22 | |
| 23 | class Function; |
| 24 | class GlobalValue; |
| 25 | class Constant; |
| 26 | class TargetMachine; |
| 27 | class TargetJITInfo; |
| 28 | class MachineCodeEmitter; |
| 29 | |
| 30 | class JITState { |
| 31 | private: |
| 32 | FunctionPassManager PM; // Passes to compile a function |
| 33 | |
| 34 | /// PendingGlobals - Global variables which have had memory allocated for them |
| 35 | /// while a function was code generated, but which have not been initialized |
| 36 | /// yet. |
| 37 | std::vector<const GlobalVariable*> PendingGlobals; |
| 38 | |
| 39 | public: |
| 40 | JITState(ModuleProvider *MP) : PM(MP) {} |
| 41 | |
| 42 | FunctionPassManager &getPM(const MutexGuard &L) { |
| 43 | return PM; |
| 44 | } |
| 45 | |
| 46 | std::vector<const GlobalVariable*> &getPendingGlobals(const MutexGuard &L) { |
| 47 | return PendingGlobals; |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | |
| 52 | class JIT : public ExecutionEngine { |
| 53 | TargetMachine &TM; // The current target we are compiling to |
| 54 | TargetJITInfo &TJI; // The JITInfo for the target we are compiling to |
| 55 | MachineCodeEmitter *MCE; // MCE object |
| 56 | |
| 57 | JITState jitstate; |
| 58 | |
Chris Lattner | 4db98aa | 2007-12-06 01:34:04 +0000 | [diff] [blame] | 59 | JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji, |
| 60 | JITMemoryManager *JMM); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 61 | public: |
| 62 | ~JIT(); |
| 63 | |
| 64 | static void Register() { |
| 65 | JITCtor = create; |
| 66 | } |
| 67 | |
| 68 | /// getJITInfo - Return the target JIT information structure. |
| 69 | /// |
| 70 | TargetJITInfo &getJITInfo() const { return TJI; } |
| 71 | |
| 72 | /// create - Create an return a new JIT compiler if there is one available |
| 73 | /// for the current target. Otherwise, return null. |
| 74 | /// |
Chris Lattner | 4db98aa | 2007-12-06 01:34:04 +0000 | [diff] [blame] | 75 | static ExecutionEngine *create(ModuleProvider *MP, std::string *Err) { |
| 76 | return createJIT(MP, Err, 0); |
| 77 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 78 | |
| 79 | /// run - Start execution with the specified function and arguments. |
| 80 | /// |
| 81 | virtual GenericValue runFunction(Function *F, |
| 82 | const std::vector<GenericValue> &ArgValues); |
| 83 | |
| 84 | /// getPointerToNamedFunction - This method returns the address of the |
| 85 | /// specified function by using the dlsym function call. As such it is only |
| 86 | /// useful for resolving library symbols, not code generated symbols. |
| 87 | /// |
| 88 | void *getPointerToNamedFunction(const std::string &Name); |
| 89 | |
| 90 | // CompilationCallback - Invoked the first time that a call site is found, |
| 91 | // which causes lazy compilation of the target function. |
| 92 | // |
| 93 | static void CompilationCallback(); |
| 94 | |
| 95 | /// getPointerToFunction - This returns the address of the specified function, |
| 96 | /// compiling it if necessary. |
| 97 | /// |
| 98 | void *getPointerToFunction(Function *F); |
| 99 | |
| 100 | /// getOrEmitGlobalVariable - Return the address of the specified global |
| 101 | /// variable, possibly emitting it to memory if needed. This is used by the |
| 102 | /// Emitter. |
| 103 | void *getOrEmitGlobalVariable(const GlobalVariable *GV); |
| 104 | |
| 105 | /// getPointerToFunctionOrStub - If the specified function has been |
| 106 | /// code-gen'd, return a pointer to the function. If not, compile it, or use |
| 107 | /// a stub to implement lazy compilation if available. |
| 108 | /// |
| 109 | void *getPointerToFunctionOrStub(Function *F); |
| 110 | |
| 111 | /// recompileAndRelinkFunction - This method is used to force a function |
| 112 | /// which has already been compiled, to be compiled again, possibly |
| 113 | /// after it has been modified. Then the entry to the old copy is overwritten |
| 114 | /// with a branch to the new copy. If there was no old copy, this acts |
| 115 | /// just like JIT::getPointerToFunction(). |
| 116 | /// |
| 117 | void *recompileAndRelinkFunction(Function *F); |
| 118 | |
| 119 | /// freeMachineCodeForFunction - deallocate memory used to code-generate this |
| 120 | /// Function. |
| 121 | /// |
| 122 | void freeMachineCodeForFunction(Function *F); |
| 123 | |
| 124 | /// getCodeEmitter - Return the code emitter this JIT is emitting into. |
| 125 | MachineCodeEmitter *getCodeEmitter() const { return MCE; } |
Chris Lattner | 4db98aa | 2007-12-06 01:34:04 +0000 | [diff] [blame] | 126 | |
| 127 | static ExecutionEngine *createJIT(ModuleProvider *MP, std::string *Err, |
| 128 | JITMemoryManager *JMM); |
| 129 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 130 | private: |
Chris Lattner | e44be00 | 2007-12-06 01:08:09 +0000 | [diff] [blame] | 131 | static MachineCodeEmitter *createEmitter(JIT &J, JITMemoryManager *JMM); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 132 | void runJITOnFunction (Function *F); |
| 133 | }; |
| 134 | |
| 135 | } // End llvm namespace |
| 136 | |
| 137 | #endif |