- Refactor the code that resolve basic block references to a TargetJITInfo
[oota-llvm.git] / include / llvm / Target / TargetJITInfo.h
1 //===- Target/TargetJITInfo.h - Target Information for 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 exposes an abstract interface used by the Just-In-Time code
11 // generator to perform target-specific activities, such as emitting stubs.  If
12 // a TargetMachine supports JIT code generation, it should provide one of these
13 // objects through the getJITInfo() method.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_TARGET_TARGETJITINFO_H
18 #define LLVM_TARGET_TARGETJITINFO_H
19
20 #include <cassert>
21 #include <vector>
22
23 namespace llvm {
24   class Function;
25   class FunctionPassManager;
26   class MachineBasicBlock;
27   class MachineCodeEmitter;
28   class MachineRelocation;
29
30   /// TargetJITInfo - Target specific information required by the Just-In-Time
31   /// code generator.
32   class TargetJITInfo {
33   public:
34     virtual ~TargetJITInfo() {}
35
36     /// addPassesToJITCompile - Add passes to the specified pass manager to
37     /// implement a fast code generator for this target.
38     ///
39     virtual void addPassesToJITCompile(FunctionPassManager &PM) = 0;
40
41     /// replaceMachineCodeForFunction - Make it so that calling the function
42     /// whose machine code is at OLD turns into a call to NEW, perhaps by
43     /// overwriting OLD with a branch to NEW.  This is used for self-modifying
44     /// code.
45     ///
46     virtual void replaceMachineCodeForFunction(void *Old, void *New) = 0;
47
48     /// emitFunctionStub - Use the specified MachineCodeEmitter object to emit a
49     /// small native function that simply calls the function at the specified
50     /// address.  Return the address of the resultant function.
51     virtual void *emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
52       assert(0 && "This target doesn't implement emitFunctionStub!");
53       return 0;
54     }
55
56     /// LazyResolverFn - This typedef is used to represent the function that
57     /// unresolved call points should invoke.  This is a target specific
58     /// function that knows how to walk the stack and find out which stub the
59     /// call is coming from.
60     typedef void (*LazyResolverFn)();
61
62     /// JITCompilerFn - This typedef is used to represent the JIT function that
63     /// lazily compiles the function corresponding to a stub.  The JIT keeps
64     /// track of the mapping between stubs and LLVM Functions, the target
65     /// provides the ability to figure out the address of a stub that is called
66     /// by the LazyResolverFn.
67     typedef void* (*JITCompilerFn)(void *);
68
69     /// getLazyResolverFunction - This method is used to initialize the JIT,
70     /// giving the target the function that should be used to compile a
71     /// function, and giving the JIT the target function used to do the lazy
72     /// resolving.
73     virtual LazyResolverFn getLazyResolverFunction(JITCompilerFn) {
74       assert(0 && "Not implemented for this target!");
75       return 0;
76     }
77
78     /// relocate - Before the JIT can run a block of code that has been emitted,
79     /// it must rewrite the code to contain the actual addresses of any
80     /// referenced global symbols.
81     virtual void relocate(void *Function, MachineRelocation *MR,
82                           unsigned NumRelocs, unsigned char* GOTBase) {
83       assert(NumRelocs == 0 && "This target does not have relocations!");
84     }
85
86     /// resolveBBRefs - Resolve branches to BasicBlocks for the JIT emitted
87     /// function.
88     virtual void resolveBBRefs(MachineCodeEmitter &MCE) {}
89
90     /// synchronizeICache - On some targets, the JIT emitted code must be
91     /// explicitly refetched to ensure correct execution.
92     virtual void synchronizeICache(const void *Addr, size_t len) {}
93
94     /// addBBRef - Add a BasicBlock reference to be resolved after the function
95     /// is emitted.
96     void addBBRef(MachineBasicBlock *BB, intptr_t PC) {
97       BBRefs.push_back(std::make_pair(BB, PC));
98     }
99
100     /// needsGOT - Allows a target to specify that it would like the
101     // JIT to manage a GOT for it.
102     bool needsGOT() const { return useGOT; }
103
104   protected:
105     bool useGOT;
106
107     // Tracks which instruction references which BasicBlock
108     std::vector<std::pair<MachineBasicBlock*, intptr_t> > BBRefs;
109     
110   };
111 } // End llvm namespace
112
113 #endif