blob: c4e0d7878663f773e390113992519027a2751b82 [file] [log] [blame]
Shih-wei Liao67d8f372011-01-16 22:48:35 -08001//===-- CodeEmitter.h - CodeEmitter Class -----------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See external/llvm/LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the CodeEmitter class.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef BCC_CODEEMITTER_H
15#define BCC_CODEEMITTER_H
16
17#include <bcc/bcc.h>
18#include <bcc/bcc_cache.h>
19#include "bcc_internal.h"
20
21#include "Config.h"
22
23#include "llvm/ADT/DenseMap.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/ADT/StringRef.h"
26#include "llvm/CodeGen/MachineRelocation.h"
27#include "llvm/CodeGen/JITCodeEmitter.h"
28#include "llvm/Support/ValueHandle.h"
29
30#include <map>
31#include <vector>
32#include <set>
33
34#include <assert.h>
35#include <stdint.h>
36
37namespace llvm {
38 class Constant;
39 class GenericValue;
40 class GlobalVariable;
41 class GlobalValue;
42 class Function;
43 class MachineBasicBlock;
44 class MachineConstantPool;
45 class MachineFunction;
46 class MachineJumpTableInfo;
47 class MachineModuleInfo;
48#if USE_DISASSEMBLER
49 class MCAsmInfo;
50 class MCDisassembler;
51 class MCInstPrinter;
52#endif
53 class MCSymbol;
54 class Target;
55 class TargetData;
56 class TargetJITInfo;
57 class TargetMachine;
58 class Type;
59}
60
61namespace bcc {
62 class CodeMemoryManager;
63 class ScriptCompiled;
64
65 class CodeEmitter : public llvm::JITCodeEmitter {
66 private:
67 typedef llvm::DenseMap<const llvm::GlobalValue *, void *>
68 GlobalAddressMapTy;
69
70 typedef llvm::DenseMap<const llvm::Function *, void*>
71 FunctionToLazyStubMapTy;
72
73 typedef std::map<llvm::AssertingVH<llvm::GlobalValue>, void *>
74 GlobalToIndirectSymMapTy;
75
76 public:
77 typedef GlobalAddressMapTy::const_iterator global_addresses_const_iterator;
78
79
80 private:
81 ScriptCompiled *mpResult;
82
83 CodeMemoryManager *mpMemMgr;
84
Shih-wei Liaocfd23fd2011-04-19 18:02:28 -070085 llvm::TargetMachine *mpTargetMachine;
86
Shih-wei Liao67d8f372011-01-16 22:48:35 -080087 // The JITInfo for the target we are compiling to
88 const llvm::Target *mpTarget;
89
90 llvm::TargetJITInfo *mpTJI;
91
92 const llvm::TargetData *mpTD;
93
94
95 FuncInfo *mpCurEmitFunction;
96
97 GlobalAddressMapTy mGlobalAddressMap;
98
99 // This vector is a mapping from MBB ID's to their address. It is filled in
100 // by the StartMachineBasicBlock callback and queried by the
101 // getMachineBasicBlockAddress callback.
102 std::vector<uintptr_t> mMBBLocations;
103
104 // The constant pool for the current function.
105 llvm::MachineConstantPool *mpConstantPool;
106
107 // A pointer to the first entry in the constant pool.
108 void *mpConstantPoolBase;
109
110 // Addresses of individual constant pool entries.
111 llvm::SmallVector<uintptr_t, 8> mConstPoolAddresses;
112
113 // The jump tables for the current function.
114 llvm::MachineJumpTableInfo *mpJumpTable;
115
116 // A pointer to the first entry in the jump table.
117 void *mpJumpTableBase;
118
119 // When outputting a function stub in the context of some other function, we
120 // save BufferBegin/BufferEnd/CurBufferPtr here.
121 uint8_t *mpSavedBufferBegin, *mpSavedBufferEnd, *mpSavedCurBufferPtr;
122
123 // These are the relocations that the function needs, as emitted.
124 std::vector<llvm::MachineRelocation> mRelocations;
125
126#if 0
127 std::vector<oBCCRelocEntry> mCachingRelocations;
128#endif
129
130 // This vector is a mapping from Label ID's to their address.
131 llvm::DenseMap<llvm::MCSymbol*, uintptr_t> mLabelLocations;
132
133 // Machine module info for exception informations
134 llvm::MachineModuleInfo *mpMMI;
135
136
137 FunctionToLazyStubMapTy mFunctionToLazyStubMap;
138
139 std::set<const llvm::Function*> PendingFunctions;
140
141 GlobalToIndirectSymMapTy GlobalToIndirectSymMap;
142
143 std::map<void*, void*> ExternalFnToStubMap;
144
145#if USE_DISASSEMBLER
146 const llvm::MCAsmInfo *mpAsmInfo;
147 const llvm::MCDisassembler *mpDisassmbler;
148 llvm::MCInstPrinter *mpIP;
149#endif
150
151 public:
152 // Resolver to undefined symbol in CodeEmitter
153 BCCSymbolLookupFn mpSymbolLookupFn;
154 void *mpSymbolLookupContext;
155
156 // Will take the ownership of @MemMgr
157 explicit CodeEmitter(ScriptCompiled *result, CodeMemoryManager *pMemMgr);
158
159 virtual ~CodeEmitter();
160
161 void Disassemble(const llvm::StringRef &Name, uint8_t *Start,
162 size_t Length, bool IsStub);
163
164 global_addresses_const_iterator global_address_begin() const {
165 return mGlobalAddressMap.begin();
166 }
167
168 global_addresses_const_iterator global_address_end() const {
169 return mGlobalAddressMap.end();
170 }
171
172#if 0
173 std::vector<oBCCRelocEntry> const &getCachingRelocations() const {
174 return mCachingRelocations;
175 }
176#endif
177
178 void registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) {
179 mpSymbolLookupFn = pFn;
180 mpSymbolLookupContext = pContext;
181 }
182
183 void setTargetMachine(llvm::TargetMachine &TM);
184
185 // This callback is invoked when the specified function is about to be code
186 // generated. This initializes the BufferBegin/End/Ptr fields.
187 virtual void startFunction(llvm::MachineFunction &F);
188
189 // This callback is invoked when the specified function has finished code
190 // generation. If a buffer overflow has occurred, this method returns true
191 // (the callee is required to try again).
192 virtual bool finishFunction(llvm::MachineFunction &F);
193
194 // Allocates and fills storage for an indirect GlobalValue, and returns the
195 // address.
196 virtual void *allocIndirectGV(const llvm::GlobalValue *GV,
197 const uint8_t *Buffer, size_t Size,
198 unsigned Alignment);
199
200 // Emits a label
201 virtual void emitLabel(llvm::MCSymbol *Label) {
202 mLabelLocations[Label] = getCurrentPCValue();
203 }
204
205 // Allocate memory for a global. Unlike allocateSpace, this method does not
206 // allocate memory in the current output buffer, because a global may live
207 // longer than the current function.
208 virtual void *allocateGlobal(uintptr_t Size, unsigned Alignment);
209
210 // This should be called by the target when a new basic block is about to be
211 // emitted. This way the MCE knows where the start of the block is, and can
212 // implement getMachineBasicBlockAddress.
213 virtual void StartMachineBasicBlock(llvm::MachineBasicBlock *MBB);
214
215 // Whenever a relocatable address is needed, it should be noted with this
216 // interface.
217 virtual void addRelocation(const llvm::MachineRelocation &MR) {
218 mRelocations.push_back(MR);
219 }
220
221 // Return the address of the @Index entry in the constant pool that was
222 // last emitted with the emitConstantPool method.
223 virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const {
224 assert(Index < mpConstantPool->getConstants().size() &&
225 "Invalid constant pool index!");
226 return mConstPoolAddresses[Index];
227 }
228
229 // Return the address of the jump table with index @Index in the function
230 // that last called initJumpTableInfo.
231 virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const;
232
233 // Return the address of the specified MachineBasicBlock, only usable after
234 // the label for the MBB has been emitted.
235 virtual uintptr_t getMachineBasicBlockAddress(
236 llvm::MachineBasicBlock *MBB) const;
237
238 // Return the address of the specified LabelID, only usable after the
239 // LabelID has been emitted.
240 virtual uintptr_t getLabelAddress(llvm::MCSymbol *Label) const {
241 assert(mLabelLocations.count(Label) && "Label not emitted!");
242 return mLabelLocations.find(Label)->second;
243 }
244
245 // Specifies the MachineModuleInfo object. This is used for exception
246 // handling purposes.
247 virtual void setModuleInfo(llvm::MachineModuleInfo *Info) {
248 mpMMI = Info;
249 }
250
251 void releaseUnnecessary();
252
253 void reset();
254
255 private:
256 void startGVStub(const llvm::GlobalValue *GV, unsigned StubSize,
257 unsigned Alignment);
258
259 void startGVStub(void *Buffer, unsigned StubSize);
260
261 void finishGVStub();
262
263 // Replace an existing mapping for GV with a new address. This updates both
264 // maps as required. If Addr is null, the entry for the global is removed
265 // from the mappings.
266 void *UpdateGlobalMapping(const llvm::GlobalValue *GV, void *Addr);
267
268 // Tell the execution engine that the specified global is at the specified
269 // location. This is used internally as functions are JIT'd and as global
270 // variables are laid out in memory.
271 void AddGlobalMapping(const llvm::GlobalValue *GV, void *Addr) {
272 void *&CurVal = mGlobalAddressMap[GV];
273 assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
274 CurVal = Addr;
275 }
276
277 // This returns the address of the specified global value if it is has
278 // already been codegen'd, otherwise it returns null.
279 void *GetPointerToGlobalIfAvailable(const llvm::GlobalValue *GV) {
280 GlobalAddressMapTy::iterator I = mGlobalAddressMap.find(GV);
281 return ((I != mGlobalAddressMap.end()) ? I->second : NULL);
282 }
283
284 unsigned int GetConstantPoolSizeInBytes(llvm::MachineConstantPool *MCP);
285
286 // This function converts a Constant* into a GenericValue. The interesting
287 // part is if C is a ConstantExpr.
288 void GetConstantValue(const llvm::Constant *C, llvm::GenericValue &Result);
289
290 // Stores the data in @Val of type @Ty at address @Addr.
291 void StoreValueToMemory(const llvm::GenericValue &Val, void *Addr,
292 const llvm::Type *Ty);
293
294 // Recursive function to apply a @Constant value into the specified memory
295 // location @Addr.
296 void InitializeConstantToMemory(const llvm::Constant *C, void *Addr);
297
298 void emitConstantPool(llvm::MachineConstantPool *MCP);
299
300 void initJumpTableInfo(llvm::MachineJumpTableInfo *MJTI);
301
302 void emitJumpTableInfo(llvm::MachineJumpTableInfo *MJTI);
303
304 void *GetPointerToGlobal(llvm::GlobalValue *V,
305 void *Reference,
306 bool MayNeedFarStub);
307
308 // If the specified function has been code-gen'd, return a pointer to the
309 // function. If not, compile it, or use a stub to implement lazy compilation
310 // if available.
311 void *GetPointerToFunctionOrStub(llvm::Function *F);
312
313 void *GetLazyFunctionStubIfAvailable(llvm::Function *F) {
314 return mFunctionToLazyStubMap.lookup(F);
315 }
316
317 void *GetLazyFunctionStub(llvm::Function *F);
318
319 void updateFunctionStub(const llvm::Function *F);
320
321 void *GetPointerToFunction(const llvm::Function *F, bool AbortOnFailure);
322
323 void *GetPointerToNamedSymbol(const std::string &Name,
324 bool AbortOnFailure);
325
326 // Return the address of the specified global variable, possibly emitting it
327 // to memory if needed. This is used by the Emitter.
328 void *GetOrEmitGlobalVariable(const llvm::GlobalVariable *GV);
329
330 // This method abstracts memory allocation of global variable so that the
331 // JIT can allocate thread local variables depending on the target.
332 void *GetMemoryForGV(const llvm::GlobalVariable *GV);
333
334 void EmitGlobalVariable(const llvm::GlobalVariable *GV);
335
336 void *GetPointerToGVIndirectSym(llvm::GlobalValue *V, void *Reference);
337
338 // This is the equivalent of FunctionToLazyStubMap for external functions.
339 //
340 // TODO(llvm.org): Of course, external functions don't need a lazy stub.
341 // It's actually here to make it more likely that far calls
342 // succeed, but no single stub can guarantee that. I'll
343 // remove this in a subsequent checkin when I actually fix
344 // far calls.
345
346 // Return a stub for the function at the specified address.
347 void *GetExternalFunctionStub(void *FnAddr);
348
349 };
350
351} // namespace bcc
352
353#endif // BCC_CODEEMITTER_H