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