JIT.h revision ee448630bdf7eb6037fe2c50518d32010c433ca3
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& locked) {
43    return PM;
44  }
45
46  std::vector<const GlobalVariable*>& getPendingGlobals(const MutexGuard& locked) {
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 state;
58
59  JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji);
60public:
61  ~JIT();
62
63  /// getJITInfo - Return the target JIT information structure.
64  ///
65  TargetJITInfo &getJITInfo() const { return TJI; }
66
67  /// create - Create an return a new JIT compiler if there is one available
68  /// for the current target.  Otherwise, return null.  If the JIT is created
69  /// successfully, it takes responsibility for deleting the specified
70  /// IntrinsicLowering implementation.
71  ///
72  static ExecutionEngine *create(ModuleProvider *MP, IntrinsicLowering *IL = 0);
73
74  /// run - Start execution with the specified function and arguments.
75  ///
76  virtual GenericValue runFunction(Function *F,
77                                   const std::vector<GenericValue> &ArgValues);
78
79  /// getPointerToNamedFunction - This method returns the address of the
80  /// specified function by using the dlsym function call.  As such it is only
81  /// useful for resolving library symbols, not code generated symbols.
82  ///
83  void *getPointerToNamedFunction(const std::string &Name);
84
85  // CompilationCallback - Invoked the first time that a call site is found,
86  // which causes lazy compilation of the target function.
87  //
88  static void CompilationCallback();
89
90  /// getPointerToFunction - This returns the address of the specified function,
91  /// compiling it if necessary.
92  ///
93  void *getPointerToFunction(Function *F);
94
95  /// getOrEmitGlobalVariable - Return the address of the specified global
96  /// variable, possibly emitting it to memory if needed.  This is used by the
97  /// Emitter.
98  void *getOrEmitGlobalVariable(const GlobalVariable *GV);
99
100  /// getPointerToFunctionOrStub - If the specified function has been
101  /// code-gen'd, return a pointer to the function.  If not, compile it, or use
102  /// a stub to implement lazy compilation if available.
103  ///
104  void *getPointerToFunctionOrStub(Function *F);
105
106  /// recompileAndRelinkFunction - This method is used to force a function
107  /// which has already been compiled, to be compiled again, possibly
108  /// after it has been modified. Then the entry to the old copy is overwritten
109  /// with a branch to the new copy. If there was no old copy, this acts
110  /// just like JIT::getPointerToFunction().
111  ///
112  void *recompileAndRelinkFunction(Function *F);
113
114  /// freeMachineCodeForFunction - deallocate memory used to code-generate this
115  /// Function.
116  ///
117  void freeMachineCodeForFunction(Function *F);
118
119private:
120  static MachineCodeEmitter *createEmitter(JIT &J);
121  void runJITOnFunction (Function *F);
122};
123
124} // End llvm namespace
125
126#endif
127