JIT.h revision c07ed1387503d25c0b93fcf617f69329d73fc589
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 JIT : public ExecutionEngine {
31  TargetMachine &TM;       // The current target we are compiling to
32  TargetJITInfo &TJI;      // The JITInfo for the target we are compiling to
33
34  FunctionPassManager PM;  // Passes to compile a function
35  MachineCodeEmitter *MCE; // MCE object
36
37  /// PendingGlobals - Global variables which have had memory allocated for them
38  /// while a function was code generated, but which have not been initialized
39  /// yet.
40  std::vector<const GlobalVariable*> PendingGlobals;
41
42  JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji);
43public:
44  ~JIT();
45
46  /// create - Create an return a new JIT compiler if there is one available
47  /// for the current target.  Otherwise, return null.
48  ///
49  static ExecutionEngine *create(ModuleProvider *MP);
50
51  /// run - Start execution with the specified function and arguments.
52  ///
53  virtual GenericValue run(Function *F,
54			   const std::vector<GenericValue> &ArgValues);
55
56  /// getPointerToNamedFunction - This method returns the address of the
57  /// specified function by using the dlsym function call.  As such it is only
58  /// useful for resolving library symbols, not code generated symbols.
59  ///
60  void *getPointerToNamedFunction(const std::string &Name);
61
62  // CompilationCallback - Invoked the first time that a call site is found,
63  // which causes lazy compilation of the target function.
64  //
65  static void CompilationCallback();
66
67  /// runAtExitHandlers - Before exiting the program, at_exit functions must be
68  /// called.  This method calls them.
69  ///
70  static void runAtExitHandlers();
71
72  /// getPointerToFunction - This returns the address of the specified function,
73  /// compiling it if necessary.
74  ///
75  void *getPointerToFunction(Function *F);
76
77  /// getOrEmitGlobalVariable - Return the address of the specified global
78  /// variable, possibly emitting it to memory if needed.  This is used by the
79  /// Emitter.
80  void *getOrEmitGlobalVariable(const GlobalVariable *GV);
81
82  /// getPointerToFunctionOrStub - If the specified function has been
83  /// code-gen'd, return a pointer to the function.  If not, compile it, or use
84  /// a stub to implement lazy compilation if available.
85  ///
86  void *getPointerToFunctionOrStub(Function *F);
87
88  /// recompileAndRelinkFunction - This method is used to force a function
89  /// which has already been compiled, to be compiled again, possibly
90  /// after it has been modified. Then the entry to the old copy is overwritten
91  /// with a branch to the new copy. If there was no old copy, this acts
92  /// just like JIT::getPointerToFunction().
93  ///
94  void *recompileAndRelinkFunction(Function *F);
95
96private:
97  static MachineCodeEmitter *createEmitter(JIT &J);
98  void runJITOnFunction (Function *F);
99};
100
101} // End llvm namespace
102
103#endif
104