blob: ce901269d6635979f8a36da7f1b8a0a829be74ed [file] [log] [blame]
Logan28325bf2010-11-26 23:27:41 +08001/*
2 * Copyright 2010, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef BCC_CODE_EMITTER_H
18#define BCC_CODE_EMITTER_H
19
20#include <bcc/bcc.h>
21#include <bcc/bcc_cache.h>
22
Logan28325bf2010-11-26 23:27:41 +080023#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 defined(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;
Loganbce48b92010-11-27 16:44:24 +080063 class EmittedFuncEntry;
Logan28325bf2010-11-26 23:27:41 +080064
65 class CodeEmitter : public llvm::JITCodeEmitter {
66 public:
67 typedef llvm::DenseMap<const llvm::GlobalValue*, void*> GlobalAddressMapTy;
68 typedef GlobalAddressMapTy::const_iterator global_addresses_const_iterator;
69
70 GlobalAddressMapTy mGlobalAddressMap;
71
72 private:
73 CodeMemoryManager *mpMemMgr;
74
75 // The JITInfo for the target we are compiling to
76 const llvm::Target *mpTarget;
77
78 llvm::TargetJITInfo *mpTJI;
79
80 const llvm::TargetData *mpTD;
81
Logan1db37e32010-11-27 14:41:23 +080082 EmittedFuncEntry *mpCurEmitFunction;
Logan28325bf2010-11-26 23:27:41 +080083
84 typedef std::map<const std::string,
Logan1db37e32010-11-27 14:41:23 +080085 EmittedFuncEntry *> EmittedFunctionsMapTy;
Logan28325bf2010-11-26 23:27:41 +080086 EmittedFunctionsMapTy mEmittedFunctions;
87
88 // This vector is a mapping from MBB ID's to their address. It is filled in
89 // by the StartMachineBasicBlock callback and queried by the
90 // getMachineBasicBlockAddress callback.
91 std::vector<uintptr_t> mMBBLocations;
92
93 // The constant pool for the current function.
94 llvm::MachineConstantPool *mpConstantPool;
95
96 // A pointer to the first entry in the constant pool.
97 void *mpConstantPoolBase;
98
99 // Addresses of individual constant pool entries.
100 llvm::SmallVector<uintptr_t, 8> mConstPoolAddresses;
101
102 // The jump tables for the current function.
103 llvm::MachineJumpTableInfo *mpJumpTable;
104
105 // A pointer to the first entry in the jump table.
106 void *mpJumpTableBase;
107
108 // When outputting a function stub in the context of some other function, we
109 // save BufferBegin/BufferEnd/CurBufferPtr here.
110 uint8_t *mpSavedBufferBegin, *mpSavedBufferEnd, *mpSavedCurBufferPtr;
111
112 // These are the relocations that the function needs, as emitted.
113 std::vector<llvm::MachineRelocation> mRelocations;
114
115 std::vector<oBCCRelocEntry> mCachingRelocations;
116
117 // This vector is a mapping from Label ID's to their address.
118 llvm::DenseMap<llvm::MCSymbol*, uintptr_t> mLabelLocations;
119
120 // Machine module info for exception informations
121 llvm::MachineModuleInfo *mpMMI;
122
123 // Replace an existing mapping for GV with a new address. This updates both
124 // maps as required. If Addr is null, the entry for the global is removed
125 // from the mappings.
126 void *UpdateGlobalMapping(const llvm::GlobalValue *GV, void *Addr);
127
128 // Tell the execution engine that the specified global is at the specified
129 // location. This is used internally as functions are JIT'd and as global
130 // variables are laid out in memory.
Loganf2b79d02010-11-27 01:07:53 +0800131 void AddGlobalMapping(const llvm::GlobalValue *GV, void *Addr) {
132 void *&CurVal = mGlobalAddressMap[GV];
133 assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
134 CurVal = Addr;
135 }
Logan28325bf2010-11-26 23:27:41 +0800136
137 // This returns the address of the specified global value if it is has
138 // already been codegen'd, otherwise it returns null.
139 void *GetPointerToGlobalIfAvailable(const llvm::GlobalValue *GV) {
140 GlobalAddressMapTy::iterator I = mGlobalAddressMap.find(GV);
141 return ((I != mGlobalAddressMap.end()) ? I->second : NULL);
142 }
143
144 unsigned int GetConstantPoolSizeInBytes(llvm::MachineConstantPool *MCP);
145
146 // This function converts a Constant* into a GenericValue. The interesting
147 // part is if C is a ConstantExpr.
148 void GetConstantValue(const llvm::Constant *C, llvm::GenericValue &Result);
149
150 // Stores the data in @Val of type @Ty at address @Addr.
151 void StoreValueToMemory(const llvm::GenericValue &Val, void *Addr,
152 const llvm::Type *Ty);
153
154 // Recursive function to apply a @Constant value into the specified memory
155 // location @Addr.
156 void InitializeConstantToMemory(const llvm::Constant *C, void *Addr);
157
158 void emitConstantPool(llvm::MachineConstantPool *MCP);
159
160 void initJumpTableInfo(llvm::MachineJumpTableInfo *MJTI);
161
162 void emitJumpTableInfo(llvm::MachineJumpTableInfo *MJTI);
163
164 void *GetPointerToGlobal(llvm::GlobalValue *V,
165 void *Reference,
166 bool MayNeedFarStub);
167
168 // If the specified function has been code-gen'd, return a pointer to the
169 // function. If not, compile it, or use a stub to implement lazy compilation
170 // if available.
171 void *GetPointerToFunctionOrStub(llvm::Function *F);
172
173 typedef llvm::DenseMap<const llvm::Function*,
174 void*> FunctionToLazyStubMapTy;
175 FunctionToLazyStubMapTy mFunctionToLazyStubMap;
176
177 void *GetLazyFunctionStubIfAvailable(llvm::Function *F) {
178 return mFunctionToLazyStubMap.lookup(F);
179 }
180
181 std::set<const llvm::Function*> PendingFunctions;
182 void *GetLazyFunctionStub(llvm::Function *F);
183
184 void *GetPointerToFunction(const llvm::Function *F, bool AbortOnFailure);
185
186 void *GetPointerToNamedSymbol(const std::string &Name,
187 bool AbortOnFailure);
188
189 // Return the address of the specified global variable, possibly emitting it
190 // to memory if needed. This is used by the Emitter.
191 void *GetOrEmitGlobalVariable(const llvm::GlobalVariable *GV);
192
193 // This method abstracts memory allocation of global variable so that the
194 // JIT can allocate thread local variables depending on the target.
195 void *GetMemoryForGV(const llvm::GlobalVariable *GV);
196
197 void EmitGlobalVariable(const llvm::GlobalVariable *GV);
198
199 typedef std::map<llvm::AssertingVH<llvm::GlobalValue>,
200 void *> GlobalToIndirectSymMapTy;
201
202 GlobalToIndirectSymMapTy GlobalToIndirectSymMap;
203
204 void *GetPointerToGVIndirectSym(llvm::GlobalValue *V, void *Reference);
205
206 // This is the equivalent of FunctionToLazyStubMap for external functions.
207 //
208 // TODO(llvm.org): Of course, external functions don't need a lazy stub.
209 // It's actually here to make it more likely that far calls
210 // succeed, but no single stub can guarantee that. I'll
211 // remove this in a subsequent checkin when I actually fix
212 // far calls.
213 std::map<void*, void*> ExternalFnToStubMap;
214
215 // Return a stub for the function at the specified address.
216 void *GetExternalFunctionStub(void *FnAddr);
217
218#if defined(USE_DISASSEMBLER)
219 const llvm::MCAsmInfo *mpAsmInfo;
220 const llvm::MCDisassembler *mpDisassmbler;
221 llvm::MCInstPrinter *mpIP;
Logane57b3322010-11-27 18:21:00 +0800222#endif
Logan28325bf2010-11-26 23:27:41 +0800223
224 public:
225 void Disassemble(const llvm::StringRef &Name, uint8_t *Start,
226 size_t Length, bool IsStub);
Logan28325bf2010-11-26 23:27:41 +0800227
228 private:
229 // Resolver to undefined symbol in CodeEmitter
230 BCCSymbolLookupFn mpSymbolLookupFn;
231 void *mpSymbolLookupContext;
232
233 public:
234 // Will take the ownership of @MemMgr
235 explicit CodeEmitter(CodeMemoryManager *pMemMgr);
236
Logan5079c882010-11-27 01:57:21 +0800237 virtual ~CodeEmitter();
Logan28325bf2010-11-26 23:27:41 +0800238
Logan5079c882010-11-27 01:57:21 +0800239 global_addresses_const_iterator global_address_begin() const {
Logan28325bf2010-11-26 23:27:41 +0800240 return mGlobalAddressMap.begin();
241 }
242
Logan5079c882010-11-27 01:57:21 +0800243 global_addresses_const_iterator global_address_end() const {
Logan28325bf2010-11-26 23:27:41 +0800244 return mGlobalAddressMap.end();
245 }
246
247 std::vector<oBCCRelocEntry> const &getCachingRelocations() const {
248 return mCachingRelocations;
249 }
250
251 void registerSymbolCallback(BCCSymbolLookupFn pFn, BCCvoid *pContext) {
252 mpSymbolLookupFn = pFn;
253 mpSymbolLookupContext = pContext;
254 }
255
256 void setTargetMachine(llvm::TargetMachine &TM);
257
258 // This callback is invoked when the specified function is about to be code
259 // generated. This initializes the BufferBegin/End/Ptr fields.
Logan5079c882010-11-27 01:57:21 +0800260 virtual void startFunction(llvm::MachineFunction &F);
Logan28325bf2010-11-26 23:27:41 +0800261
262 // This callback is invoked when the specified function has finished code
263 // generation. If a buffer overflow has occurred, this method returns true
264 // (the callee is required to try again).
Logan5079c882010-11-27 01:57:21 +0800265 virtual bool finishFunction(llvm::MachineFunction &F);
Logan28325bf2010-11-26 23:27:41 +0800266
267 void startGVStub(const llvm::GlobalValue *GV, unsigned StubSize,
268 unsigned Alignment);
269
270 void startGVStub(void *Buffer, unsigned StubSize);
271
272 void finishGVStub();
273
274 // Allocates and fills storage for an indirect GlobalValue, and returns the
275 // address.
Logan5079c882010-11-27 01:57:21 +0800276 virtual void *allocIndirectGV(const llvm::GlobalValue *GV,
277 const uint8_t *Buffer, size_t Size,
278 unsigned Alignment);
Logan28325bf2010-11-26 23:27:41 +0800279
280 // Emits a label
281 void emitLabel(llvm::MCSymbol *Label) {
282 mLabelLocations[Label] = getCurrentPCValue();
283 }
284
285 // Allocate memory for a global. Unlike allocateSpace, this method does not
286 // allocate memory in the current output buffer, because a global may live
287 // longer than the current function.
Logan5079c882010-11-27 01:57:21 +0800288 virtual void *allocateGlobal(uintptr_t Size, unsigned Alignment);
Logan28325bf2010-11-26 23:27:41 +0800289
290 // This should be called by the target when a new basic block is about to be
291 // emitted. This way the MCE knows where the start of the block is, and can
292 // implement getMachineBasicBlockAddress.
Logan5079c882010-11-27 01:57:21 +0800293 virtual void StartMachineBasicBlock(llvm::MachineBasicBlock *MBB);
Logan28325bf2010-11-26 23:27:41 +0800294
295 // Whenever a relocatable address is needed, it should be noted with this
296 // interface.
Logan5079c882010-11-27 01:57:21 +0800297 virtual void addRelocation(const llvm::MachineRelocation &MR) {
Logan28325bf2010-11-26 23:27:41 +0800298 mRelocations.push_back(MR);
299 }
300
301 // Return the address of the @Index entry in the constant pool that was
302 // last emitted with the emitConstantPool method.
Logan5079c882010-11-27 01:57:21 +0800303 virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const {
Logan28325bf2010-11-26 23:27:41 +0800304 assert(Index < mpConstantPool->getConstants().size() &&
305 "Invalid constant pool index!");
306 return mConstPoolAddresses[Index];
307 }
308
309 // Return the address of the jump table with index @Index in the function
310 // that last called initJumpTableInfo.
Logan5079c882010-11-27 01:57:21 +0800311 virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const;
Logan28325bf2010-11-26 23:27:41 +0800312
313 // Return the address of the specified MachineBasicBlock, only usable after
314 // the label for the MBB has been emitted.
Logan5079c882010-11-27 01:57:21 +0800315 virtual uintptr_t getMachineBasicBlockAddress(
316 llvm::MachineBasicBlock *MBB) const;
Logan28325bf2010-11-26 23:27:41 +0800317
318 // Return the address of the specified LabelID, only usable after the
319 // LabelID has been emitted.
Logan5079c882010-11-27 01:57:21 +0800320 virtual uintptr_t getLabelAddress(llvm::MCSymbol *Label) const {
Logan28325bf2010-11-26 23:27:41 +0800321 assert(mLabelLocations.count(Label) && "Label not emitted!");
322 return mLabelLocations.find(Label)->second;
323 }
324
325 // Specifies the MachineModuleInfo object. This is used for exception
326 // handling purposes.
Logan5079c882010-11-27 01:57:21 +0800327 virtual void setModuleInfo(llvm::MachineModuleInfo *Info) {
Logan28325bf2010-11-26 23:27:41 +0800328 mpMMI = Info;
329 }
330
331 void updateFunctionStub(const llvm::Function *F);
332
333 void releaseUnnecessary();
334
335 void reset();
336
337 void *lookup(const char *Name) {
338 return lookup( llvm::StringRef(Name) );
339 }
340
Loganbce48b92010-11-27 16:44:24 +0800341 void *lookup(const llvm::StringRef &Name);
Logan28325bf2010-11-26 23:27:41 +0800342
343 void getFunctionNames(BCCsizei *actualFunctionCount,
344 BCCsizei maxFunctionCount,
345 BCCchar **functions);
346
347 void getFunctionBinary(BCCchar *label,
348 BCCvoid **base,
349 BCCsizei *length);
350 };
351
352} // namespace bcc
353
354#endif // BCC_CODE_EMITTER_H