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