JIT.h revision 34c9433004cabd4760987dce4804a91c84908219
1//===-- JIT.h - Class definition for the JIT --------------------*- 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 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
21namespace llvm {
22
23class Function;
24class GlobalValue;
25class Constant;
26class TargetMachine;
27class TargetJITInfo;
28class MachineCodeEmitter;
29
30class JITState {
31private:
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
39public:
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
52class 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
59  JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji,
60      JITMemoryManager *JMM);
61public:
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  ///
75  static ExecutionEngine *create(ModuleProvider *MP, std::string *Err) {
76    return createJIT(MP, Err, 0);
77  }
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; }
126
127  static ExecutionEngine *createJIT(ModuleProvider *MP, std::string *Err,
128                                    JITMemoryManager *JMM);
129
130private:
131  static MachineCodeEmitter *createEmitter(JIT &J, JITMemoryManager *JMM);
132  void runJITOnFunction (Function *F);
133};
134
135} // End llvm namespace
136
137#endif
138