blob: 13768006f457372c92b79a0a228e1e302524cb78 [file] [log] [blame]
Charles Davis3a811f12010-05-25 19:52:27 +00001//===------- ItaniumCXXABI.cpp - Emit LLVM Code from ASTs for a Module ----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides C++ code generation targetting the Itanium C++ ABI. The class
11// in this file generates structures that follow the Itanium C++ ABI, which is
12// documented at:
13// http://www.codesourcery.com/public/cxx-abi/abi.html
14// http://www.codesourcery.com/public/cxx-abi/abi-eh.html
John McCallee79a4c2010-08-21 22:46:04 +000015//
16// It also supports the closely-related ARM ABI, documented at:
17// http://infocenter.arm.com/help/topic/com.arm.doc.ihi0041c/IHI0041C_cppabi.pdf
18//
Charles Davis3a811f12010-05-25 19:52:27 +000019//===----------------------------------------------------------------------===//
20
21#include "CGCXXABI.h"
John McCall0bab0cd2010-08-23 01:21:21 +000022#include "CGRecordLayout.h"
John McCall93d557b2010-08-22 00:05:51 +000023#include "CodeGenFunction.h"
Charles Davis3a811f12010-05-25 19:52:27 +000024#include "CodeGenModule.h"
25#include "Mangle.h"
John McCall93d557b2010-08-22 00:05:51 +000026#include <clang/AST/Type.h>
John McCall0bab0cd2010-08-23 01:21:21 +000027#include <llvm/Target/TargetData.h>
John McCall93d557b2010-08-22 00:05:51 +000028#include <llvm/Value.h>
Charles Davis3a811f12010-05-25 19:52:27 +000029
30using namespace clang;
John McCall93d557b2010-08-22 00:05:51 +000031using namespace CodeGen;
Charles Davis3a811f12010-05-25 19:52:27 +000032
33namespace {
Charles Davis071cc7d2010-08-16 03:33:14 +000034class ItaniumCXXABI : public CodeGen::CGCXXABI {
John McCall0bab0cd2010-08-23 01:21:21 +000035private:
36 const llvm::IntegerType *PtrDiffTy;
John McCall93d557b2010-08-22 00:05:51 +000037protected:
Charles Davis3a811f12010-05-25 19:52:27 +000038 CodeGen::MangleContext MangleCtx;
John McCallbabc9a92010-08-22 00:59:17 +000039 bool IsARM;
John McCall0bab0cd2010-08-23 01:21:21 +000040
41 // It's a little silly for us to cache this.
42 const llvm::IntegerType *getPtrDiffTy() {
43 if (!PtrDiffTy) {
44 QualType T = CGM.getContext().getPointerDiffType();
45 const llvm::Type *Ty = CGM.getTypes().ConvertTypeRecursive(T);
46 PtrDiffTy = cast<llvm::IntegerType>(Ty);
47 }
48 return PtrDiffTy;
49 }
50
Charles Davis3a811f12010-05-25 19:52:27 +000051public:
John McCallbabc9a92010-08-22 00:59:17 +000052 ItaniumCXXABI(CodeGen::CodeGenModule &CGM, bool IsARM = false) :
John McCall0bab0cd2010-08-23 01:21:21 +000053 CGCXXABI(CGM), PtrDiffTy(0), MangleCtx(CGM.getContext(), CGM.getDiags()),
54 IsARM(IsARM) { }
Charles Davis3a811f12010-05-25 19:52:27 +000055
56 CodeGen::MangleContext &getMangleContext() {
57 return MangleCtx;
58 }
John McCall93d557b2010-08-22 00:05:51 +000059
John McCallf16aa102010-08-22 21:01:12 +000060 bool isZeroInitializable(const MemberPointerType *MPT);
John McCallcf2c85e2010-08-22 04:16:24 +000061
John McCall0bab0cd2010-08-23 01:21:21 +000062 const llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT);
63
John McCall93d557b2010-08-22 00:05:51 +000064 llvm::Value *EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
65 llvm::Value *&This,
66 llvm::Value *MemFnPtr,
67 const MemberPointerType *MPT);
John McCall3023def2010-08-22 03:04:22 +000068
John McCall6c2ab1d2010-08-31 21:07:20 +000069 llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
70 llvm::Value *Base,
71 llvm::Value *MemPtr,
72 const MemberPointerType *MPT);
73
John McCall0bab0cd2010-08-23 01:21:21 +000074 llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
75 const CastExpr *E,
76 llvm::Value *Src);
John McCallcf2c85e2010-08-22 04:16:24 +000077
John McCall0bab0cd2010-08-23 01:21:21 +000078 llvm::Constant *EmitMemberPointerConversion(llvm::Constant *C,
79 const CastExpr *E);
John McCallcf2c85e2010-08-22 04:16:24 +000080
John McCall0bab0cd2010-08-23 01:21:21 +000081 llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
John McCallcf2c85e2010-08-22 04:16:24 +000082
John McCall0bab0cd2010-08-23 01:21:21 +000083 llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
84 llvm::Constant *EmitMemberPointer(const FieldDecl *FD);
John McCall875ab102010-08-22 06:43:33 +000085
John McCall0bab0cd2010-08-23 01:21:21 +000086 llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF,
87 llvm::Value *L,
88 llvm::Value *R,
89 const MemberPointerType *MPT,
90 bool Inequality);
John McCalle9fd7eb2010-08-22 08:30:07 +000091
John McCall0bab0cd2010-08-23 01:21:21 +000092 llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
93 llvm::Value *Addr,
94 const MemberPointerType *MPT);
John McCall4c40d982010-08-31 07:33:07 +000095
96 void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
97 CXXCtorType T,
98 CanQualType &ResTy,
99 llvm::SmallVectorImpl<CanQualType> &ArgTys);
100
101 void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
102 CXXDtorType T,
103 CanQualType &ResTy,
104 llvm::SmallVectorImpl<CanQualType> &ArgTys);
105
106 void BuildInstanceFunctionParams(CodeGenFunction &CGF,
107 QualType &ResTy,
108 FunctionArgList &Params);
109
110 void EmitInstanceFunctionProlog(CodeGenFunction &CGF);
Charles Davis3a811f12010-05-25 19:52:27 +0000111};
John McCallee79a4c2010-08-21 22:46:04 +0000112
113class ARMCXXABI : public ItaniumCXXABI {
114public:
John McCallbabc9a92010-08-22 00:59:17 +0000115 ARMCXXABI(CodeGen::CodeGenModule &CGM) : ItaniumCXXABI(CGM, /*ARM*/ true) {}
John McCall4c40d982010-08-31 07:33:07 +0000116
117 void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
118 CXXCtorType T,
119 CanQualType &ResTy,
120 llvm::SmallVectorImpl<CanQualType> &ArgTys);
121
122 void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
123 CXXDtorType T,
124 CanQualType &ResTy,
125 llvm::SmallVectorImpl<CanQualType> &ArgTys);
126
127 void BuildInstanceFunctionParams(CodeGenFunction &CGF,
128 QualType &ResTy,
129 FunctionArgList &Params);
130
131 void EmitInstanceFunctionProlog(CodeGenFunction &CGF);
132
133 void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV, QualType ResTy);
134
135
136private:
137 /// \brief Returns true if the given instance method is one of the
138 /// kinds that the ARM ABI says returns 'this'.
139 static bool HasThisReturn(GlobalDecl GD) {
140 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
141 return ((isa<CXXDestructorDecl>(MD) && GD.getDtorType() != Dtor_Deleting) ||
142 (isa<CXXConstructorDecl>(MD)));
143 }
John McCallee79a4c2010-08-21 22:46:04 +0000144};
Charles Davis3a811f12010-05-25 19:52:27 +0000145}
146
Charles Davis071cc7d2010-08-16 03:33:14 +0000147CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) {
Charles Davis3a811f12010-05-25 19:52:27 +0000148 return new ItaniumCXXABI(CGM);
149}
150
John McCallee79a4c2010-08-21 22:46:04 +0000151CodeGen::CGCXXABI *CodeGen::CreateARMCXXABI(CodeGenModule &CGM) {
152 return new ARMCXXABI(CGM);
153}
154
John McCall0bab0cd2010-08-23 01:21:21 +0000155const llvm::Type *
156ItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
157 if (MPT->isMemberDataPointer())
158 return getPtrDiffTy();
159 else
160 return llvm::StructType::get(CGM.getLLVMContext(),
161 getPtrDiffTy(), getPtrDiffTy(), NULL);
John McCall875ab102010-08-22 06:43:33 +0000162}
163
John McCallbabc9a92010-08-22 00:59:17 +0000164/// In the Itanium and ARM ABIs, method pointers have the form:
165/// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr;
166///
167/// In the Itanium ABI:
168/// - method pointers are virtual if (memptr.ptr & 1) is nonzero
169/// - the this-adjustment is (memptr.adj)
170/// - the virtual offset is (memptr.ptr - 1)
171///
172/// In the ARM ABI:
173/// - method pointers are virtual if (memptr.adj & 1) is nonzero
174/// - the this-adjustment is (memptr.adj >> 1)
175/// - the virtual offset is (memptr.ptr)
176/// ARM uses 'adj' for the virtual flag because Thumb functions
177/// may be only single-byte aligned.
178///
179/// If the member is virtual, the adjusted 'this' pointer points
180/// to a vtable pointer from which the virtual offset is applied.
181///
182/// If the member is non-virtual, memptr.ptr is the address of
183/// the function to call.
John McCall93d557b2010-08-22 00:05:51 +0000184llvm::Value *
185ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
186 llvm::Value *&This,
187 llvm::Value *MemFnPtr,
188 const MemberPointerType *MPT) {
189 CGBuilderTy &Builder = CGF.Builder;
190
191 const FunctionProtoType *FPT =
192 MPT->getPointeeType()->getAs<FunctionProtoType>();
193 const CXXRecordDecl *RD =
194 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
195
196 const llvm::FunctionType *FTy =
197 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
198 FPT->isVariadic());
199
John McCall0bab0cd2010-08-23 01:21:21 +0000200 const llvm::IntegerType *ptrdiff = getPtrDiffTy();
John McCallbabc9a92010-08-22 00:59:17 +0000201 llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(ptrdiff, 1);
John McCall93d557b2010-08-22 00:05:51 +0000202
John McCallbabc9a92010-08-22 00:59:17 +0000203 llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual");
204 llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual");
205 llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end");
206
John McCalld608cdb2010-08-22 10:59:02 +0000207 // Extract memptr.adj, which is in the second field.
208 llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj");
John McCallbabc9a92010-08-22 00:59:17 +0000209
210 // Compute the true adjustment.
211 llvm::Value *Adj = RawAdj;
212 if (IsARM)
213 Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted");
John McCall93d557b2010-08-22 00:05:51 +0000214
215 // Apply the adjustment and cast back to the original struct type
216 // for consistency.
John McCallbabc9a92010-08-22 00:59:17 +0000217 llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy());
218 Ptr = Builder.CreateInBoundsGEP(Ptr, Adj);
219 This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted");
John McCall93d557b2010-08-22 00:05:51 +0000220
221 // Load the function pointer.
John McCalld608cdb2010-08-22 10:59:02 +0000222 llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr");
John McCall93d557b2010-08-22 00:05:51 +0000223
224 // If the LSB in the function pointer is 1, the function pointer points to
225 // a virtual function.
John McCallbabc9a92010-08-22 00:59:17 +0000226 llvm::Value *IsVirtual;
227 if (IsARM)
228 IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1);
229 else
230 IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1);
231 IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual");
John McCall93d557b2010-08-22 00:05:51 +0000232 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
233
234 // In the virtual path, the adjustment left 'This' pointing to the
235 // vtable of the correct base subobject. The "function pointer" is an
John McCallbabc9a92010-08-22 00:59:17 +0000236 // offset within the vtable (+1 for the virtual flag on non-ARM).
John McCall93d557b2010-08-22 00:05:51 +0000237 CGF.EmitBlock(FnVirtual);
238
239 // Cast the adjusted this to a pointer to vtable pointer and load.
240 const llvm::Type *VTableTy = Builder.getInt8PtrTy();
241 llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy->getPointerTo());
John McCallbabc9a92010-08-22 00:59:17 +0000242 VTable = Builder.CreateLoad(VTable, "memptr.vtable");
John McCall93d557b2010-08-22 00:05:51 +0000243
244 // Apply the offset.
John McCallbabc9a92010-08-22 00:59:17 +0000245 llvm::Value *VTableOffset = FnAsInt;
246 if (!IsARM) VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1);
247 VTable = Builder.CreateGEP(VTable, VTableOffset);
John McCall93d557b2010-08-22 00:05:51 +0000248
249 // Load the virtual function to call.
250 VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo());
John McCallbabc9a92010-08-22 00:59:17 +0000251 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn");
John McCall93d557b2010-08-22 00:05:51 +0000252 CGF.EmitBranch(FnEnd);
253
254 // In the non-virtual path, the function pointer is actually a
255 // function pointer.
256 CGF.EmitBlock(FnNonVirtual);
257 llvm::Value *NonVirtualFn =
John McCallbabc9a92010-08-22 00:59:17 +0000258 Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn");
John McCall93d557b2010-08-22 00:05:51 +0000259
260 // We're done.
261 CGF.EmitBlock(FnEnd);
262 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
263 Callee->reserveOperandSpace(2);
264 Callee->addIncoming(VirtualFn, FnVirtual);
265 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
266 return Callee;
267}
John McCall3023def2010-08-22 03:04:22 +0000268
John McCall6c2ab1d2010-08-31 21:07:20 +0000269/// Compute an l-value by applying the given pointer-to-member to a
270/// base object.
271llvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF,
272 llvm::Value *Base,
273 llvm::Value *MemPtr,
274 const MemberPointerType *MPT) {
275 assert(MemPtr->getType() == getPtrDiffTy());
276
277 CGBuilderTy &Builder = CGF.Builder;
278
279 unsigned AS = cast<llvm::PointerType>(Base->getType())->getAddressSpace();
280
281 // Cast to char*.
282 Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS));
283
284 // Apply the offset, which we assume is non-null.
285 llvm::Value *Addr = Builder.CreateInBoundsGEP(Base, MemPtr, "memptr.offset");
286
287 // Cast the address to the appropriate pointer type, adopting the
288 // address space of the base pointer.
289 const llvm::Type *PType
290 = CGF.ConvertType(MPT->getPointeeType())->getPointerTo(AS);
291 return Builder.CreateBitCast(Addr, PType);
292}
293
John McCall3023def2010-08-22 03:04:22 +0000294/// Perform a derived-to-base or base-to-derived member pointer conversion.
John McCall0bab0cd2010-08-23 01:21:21 +0000295///
296/// Obligatory offset/adjustment diagram:
297/// <-- offset --> <-- adjustment -->
298/// |--------------------------|----------------------|--------------------|
299/// ^Derived address point ^Base address point ^Member address point
300///
301/// So when converting a base member pointer to a derived member pointer,
302/// we add the offset to the adjustment because the address point has
303/// decreased; and conversely, when converting a derived MP to a base MP
304/// we subtract the offset from the adjustment because the address point
305/// has increased.
306///
307/// The standard forbids (at compile time) conversion to and from
308/// virtual bases, which is why we don't have to consider them here.
309///
310/// The standard forbids (at run time) casting a derived MP to a base
311/// MP when the derived MP does not point to a member of the base.
312/// This is why -1 is a reasonable choice for null data member
313/// pointers.
John McCalld608cdb2010-08-22 10:59:02 +0000314llvm::Value *
John McCall0bab0cd2010-08-23 01:21:21 +0000315ItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
316 const CastExpr *E,
317 llvm::Value *Src) {
John McCall2de56d12010-08-25 11:45:40 +0000318 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
319 E->getCastKind() == CK_BaseToDerivedMemberPointer);
John McCall3023def2010-08-22 03:04:22 +0000320
John McCalld608cdb2010-08-22 10:59:02 +0000321 if (isa<llvm::Constant>(Src))
John McCall0bab0cd2010-08-23 01:21:21 +0000322 return EmitMemberPointerConversion(cast<llvm::Constant>(Src), E);
John McCalld608cdb2010-08-22 10:59:02 +0000323
John McCall3023def2010-08-22 03:04:22 +0000324 CGBuilderTy &Builder = CGF.Builder;
325
326 const MemberPointerType *SrcTy =
327 E->getSubExpr()->getType()->getAs<MemberPointerType>();
328 const MemberPointerType *DestTy = E->getType()->getAs<MemberPointerType>();
329
330 const CXXRecordDecl *SrcDecl = SrcTy->getClass()->getAsCXXRecordDecl();
331 const CXXRecordDecl *DestDecl = DestTy->getClass()->getAsCXXRecordDecl();
332
John McCall3023def2010-08-22 03:04:22 +0000333 bool DerivedToBase =
John McCall2de56d12010-08-25 11:45:40 +0000334 E->getCastKind() == CK_DerivedToBaseMemberPointer;
John McCall3023def2010-08-22 03:04:22 +0000335
336 const CXXRecordDecl *BaseDecl, *DerivedDecl;
337 if (DerivedToBase)
338 DerivedDecl = SrcDecl, BaseDecl = DestDecl;
339 else
340 BaseDecl = SrcDecl, DerivedDecl = DestDecl;
341
John McCalld608cdb2010-08-22 10:59:02 +0000342 llvm::Constant *Adj =
343 CGF.CGM.GetNonVirtualBaseClassOffset(DerivedDecl,
344 E->path_begin(),
345 E->path_end());
346 if (!Adj) return Src;
John McCall875ab102010-08-22 06:43:33 +0000347
John McCall0bab0cd2010-08-23 01:21:21 +0000348 // For member data pointers, this is just a matter of adding the
349 // offset if the source is non-null.
350 if (SrcTy->isMemberDataPointer()) {
351 llvm::Value *Dst;
352 if (DerivedToBase)
353 Dst = Builder.CreateNSWSub(Src, Adj, "adj");
354 else
355 Dst = Builder.CreateNSWAdd(Src, Adj, "adj");
356
357 // Null check.
358 llvm::Value *Null = llvm::Constant::getAllOnesValue(Src->getType());
359 llvm::Value *IsNull = Builder.CreateICmpEQ(Src, Null, "memptr.isnull");
360 return Builder.CreateSelect(IsNull, Src, Dst);
361 }
362
John McCalld608cdb2010-08-22 10:59:02 +0000363 // The this-adjustment is left-shifted by 1 on ARM.
364 if (IsARM) {
365 uint64_t Offset = cast<llvm::ConstantInt>(Adj)->getZExtValue();
366 Offset <<= 1;
367 Adj = llvm::ConstantInt::get(Adj->getType(), Offset);
368 }
369
John McCalle14add42010-08-22 11:04:31 +0000370 llvm::Value *SrcAdj = Builder.CreateExtractValue(Src, 1, "src.adj");
John McCalld608cdb2010-08-22 10:59:02 +0000371 llvm::Value *DstAdj;
372 if (DerivedToBase)
John McCall0bab0cd2010-08-23 01:21:21 +0000373 DstAdj = Builder.CreateNSWSub(SrcAdj, Adj, "adj");
John McCalld608cdb2010-08-22 10:59:02 +0000374 else
John McCall0bab0cd2010-08-23 01:21:21 +0000375 DstAdj = Builder.CreateNSWAdd(SrcAdj, Adj, "adj");
John McCalld608cdb2010-08-22 10:59:02 +0000376
John McCalle14add42010-08-22 11:04:31 +0000377 return Builder.CreateInsertValue(Src, DstAdj, 1);
John McCall3023def2010-08-22 03:04:22 +0000378}
John McCallcf2c85e2010-08-22 04:16:24 +0000379
380llvm::Constant *
John McCall0bab0cd2010-08-23 01:21:21 +0000381ItaniumCXXABI::EmitMemberPointerConversion(llvm::Constant *C,
382 const CastExpr *E) {
John McCallcf2c85e2010-08-22 04:16:24 +0000383 const MemberPointerType *SrcTy =
384 E->getSubExpr()->getType()->getAs<MemberPointerType>();
385 const MemberPointerType *DestTy =
386 E->getType()->getAs<MemberPointerType>();
387
388 bool DerivedToBase =
John McCall2de56d12010-08-25 11:45:40 +0000389 E->getCastKind() == CK_DerivedToBaseMemberPointer;
John McCallcf2c85e2010-08-22 04:16:24 +0000390
391 const CXXRecordDecl *DerivedDecl;
392 if (DerivedToBase)
393 DerivedDecl = SrcTy->getClass()->getAsCXXRecordDecl();
394 else
395 DerivedDecl = DestTy->getClass()->getAsCXXRecordDecl();
396
397 // Calculate the offset to the base class.
398 llvm::Constant *Offset =
399 CGM.GetNonVirtualBaseClassOffset(DerivedDecl,
400 E->path_begin(),
401 E->path_end());
402 // If there's no offset, we're done.
403 if (!Offset) return C;
404
John McCall0bab0cd2010-08-23 01:21:21 +0000405 // If the source is a member data pointer, we have to do a null
406 // check and then add the offset. In the common case, we can fold
407 // away the offset.
408 if (SrcTy->isMemberDataPointer()) {
409 assert(C->getType() == getPtrDiffTy());
410
411 // If it's a constant int, just create a new constant int.
412 if (llvm::ConstantInt *CI = dyn_cast<llvm::ConstantInt>(C)) {
413 int64_t Src = CI->getSExtValue();
414
415 // Null converts to null.
416 if (Src == -1) return CI;
417
418 // Otherwise, just add the offset.
419 int64_t OffsetV = cast<llvm::ConstantInt>(Offset)->getSExtValue();
420 int64_t Dst = (DerivedToBase ? Src - OffsetV : Src + OffsetV);
421 return llvm::ConstantInt::get(CI->getType(), Dst, /*signed*/ true);
422 }
423
424 // Otherwise, we have to form a constant select expression.
425 llvm::Constant *Null = llvm::Constant::getAllOnesValue(C->getType());
426
427 llvm::Constant *IsNull =
428 llvm::ConstantExpr::getICmp(llvm::ICmpInst::ICMP_EQ, C, Null);
429
430 llvm::Constant *Dst;
431 if (DerivedToBase)
432 Dst = llvm::ConstantExpr::getNSWSub(C, Offset);
433 else
434 Dst = llvm::ConstantExpr::getNSWAdd(C, Offset);
435
436 return llvm::ConstantExpr::getSelect(IsNull, Null, Dst);
437 }
438
John McCall875ab102010-08-22 06:43:33 +0000439 // The this-adjustment is left-shifted by 1 on ARM.
440 if (IsARM) {
John McCall0bab0cd2010-08-23 01:21:21 +0000441 int64_t OffsetV = cast<llvm::ConstantInt>(Offset)->getSExtValue();
John McCall875ab102010-08-22 06:43:33 +0000442 OffsetV <<= 1;
443 Offset = llvm::ConstantInt::get(Offset->getType(), OffsetV);
444 }
445
John McCallcf2c85e2010-08-22 04:16:24 +0000446 llvm::ConstantStruct *CS = cast<llvm::ConstantStruct>(C);
447
John McCall0bab0cd2010-08-23 01:21:21 +0000448 llvm::Constant *Values[2] = { CS->getOperand(0), 0 };
449 if (DerivedToBase)
450 Values[1] = llvm::ConstantExpr::getSub(CS->getOperand(1), Offset);
451 else
452 Values[1] = llvm::ConstantExpr::getAdd(CS->getOperand(1), Offset);
453
John McCallcf2c85e2010-08-22 04:16:24 +0000454 return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2,
455 /*Packed=*/false);
456}
457
458
John McCallcf2c85e2010-08-22 04:16:24 +0000459llvm::Constant *
John McCall0bab0cd2010-08-23 01:21:21 +0000460ItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
461 const llvm::Type *ptrdiff_t = getPtrDiffTy();
462
463 // Itanium C++ ABI 2.3:
464 // A NULL pointer is represented as -1.
465 if (MPT->isMemberDataPointer())
466 return llvm::ConstantInt::get(ptrdiff_t, -1ULL, /*isSigned=*/true);
John McCalld608cdb2010-08-22 10:59:02 +0000467
468 llvm::Constant *Zero = llvm::ConstantInt::get(ptrdiff_t, 0);
469 llvm::Constant *Values[2] = { Zero, Zero };
470 return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2,
471 /*Packed=*/false);
John McCallcf2c85e2010-08-22 04:16:24 +0000472}
473
John McCall0bab0cd2010-08-23 01:21:21 +0000474llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const FieldDecl *FD) {
475 // Itanium C++ ABI 2.3:
476 // A pointer to data member is an offset from the base address of
477 // the class object containing it, represented as a ptrdiff_t
478
479 QualType ClassType = CGM.getContext().getTypeDeclType(FD->getParent());
480 const llvm::StructType *ClassLTy =
481 cast<llvm::StructType>(CGM.getTypes().ConvertType(ClassType));
482
483 const CGRecordLayout &RL = CGM.getTypes().getCGRecordLayout(FD->getParent());
484 unsigned FieldNo = RL.getLLVMFieldNo(FD);
485 uint64_t Offset =
486 CGM.getTargetData().getStructLayout(ClassLTy)->getElementOffset(FieldNo);
487
488 return llvm::ConstantInt::get(getPtrDiffTy(), Offset);
489}
490
491llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) {
John McCalld608cdb2010-08-22 10:59:02 +0000492 assert(MD->isInstance() && "Member function must not be static!");
493 MD = MD->getCanonicalDecl();
494
495 CodeGenTypes &Types = CGM.getTypes();
John McCall0bab0cd2010-08-23 01:21:21 +0000496 const llvm::Type *ptrdiff_t = getPtrDiffTy();
John McCalld608cdb2010-08-22 10:59:02 +0000497
498 // Get the function pointer (or index if this is a virtual function).
499 llvm::Constant *MemPtr[2];
500 if (MD->isVirtual()) {
501 uint64_t Index = CGM.getVTables().getMethodVTableIndex(MD);
502
503 // FIXME: We shouldn't use / 8 here.
504 uint64_t PointerWidthInBytes =
505 CGM.getContext().Target.getPointerWidth(0) / 8;
506 uint64_t VTableOffset = (Index * PointerWidthInBytes);
507
508 if (IsARM) {
509 // ARM C++ ABI 3.2.1:
510 // This ABI specifies that adj contains twice the this
511 // adjustment, plus 1 if the member function is virtual. The
512 // least significant bit of adj then makes exactly the same
513 // discrimination as the least significant bit of ptr does for
514 // Itanium.
515 MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset);
516 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 1);
517 } else {
518 // Itanium C++ ABI 2.3:
519 // For a virtual function, [the pointer field] is 1 plus the
520 // virtual table offset (in bytes) of the function,
521 // represented as a ptrdiff_t.
522 MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset + 1);
523 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0);
524 }
525 } else {
526 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
527 const llvm::Type *Ty;
528 // Check whether the function has a computable LLVM signature.
529 if (!CodeGenTypes::VerifyFuncTypeComplete(FPT)) {
530 // The function has a computable LLVM signature; use the correct type.
531 Ty = Types.GetFunctionType(Types.getFunctionInfo(MD), FPT->isVariadic());
532 } else {
533 // Use an arbitrary non-function type to tell GetAddrOfFunction that the
534 // function type is incomplete.
535 Ty = ptrdiff_t;
536 }
537
538 llvm::Constant *Addr = CGM.GetAddrOfFunction(MD, Ty);
539 MemPtr[0] = llvm::ConstantExpr::getPtrToInt(Addr, ptrdiff_t);
540 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0);
541 }
John McCall875ab102010-08-22 06:43:33 +0000542
543 return llvm::ConstantStruct::get(CGM.getLLVMContext(),
John McCalld608cdb2010-08-22 10:59:02 +0000544 MemPtr, 2, /*Packed=*/false);
John McCall875ab102010-08-22 06:43:33 +0000545}
546
John McCalle9fd7eb2010-08-22 08:30:07 +0000547/// The comparison algorithm is pretty easy: the member pointers are
548/// the same if they're either bitwise identical *or* both null.
549///
550/// ARM is different here only because null-ness is more complicated.
551llvm::Value *
John McCall0bab0cd2010-08-23 01:21:21 +0000552ItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
553 llvm::Value *L,
554 llvm::Value *R,
555 const MemberPointerType *MPT,
556 bool Inequality) {
John McCalle9fd7eb2010-08-22 08:30:07 +0000557 CGBuilderTy &Builder = CGF.Builder;
558
John McCalle9fd7eb2010-08-22 08:30:07 +0000559 llvm::ICmpInst::Predicate Eq;
560 llvm::Instruction::BinaryOps And, Or;
561 if (Inequality) {
562 Eq = llvm::ICmpInst::ICMP_NE;
563 And = llvm::Instruction::Or;
564 Or = llvm::Instruction::And;
565 } else {
566 Eq = llvm::ICmpInst::ICMP_EQ;
567 And = llvm::Instruction::And;
568 Or = llvm::Instruction::Or;
569 }
570
John McCall0bab0cd2010-08-23 01:21:21 +0000571 // Member data pointers are easy because there's a unique null
572 // value, so it just comes down to bitwise equality.
573 if (MPT->isMemberDataPointer())
574 return Builder.CreateICmp(Eq, L, R);
575
576 // For member function pointers, the tautologies are more complex.
577 // The Itanium tautology is:
John McCallde719f72010-08-23 06:56:36 +0000578 // (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj))
John McCall0bab0cd2010-08-23 01:21:21 +0000579 // The ARM tautology is:
John McCallde719f72010-08-23 06:56:36 +0000580 // (L == R) <==> (L.ptr == R.ptr &&
581 // (L.adj == R.adj ||
582 // (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0)))
John McCall0bab0cd2010-08-23 01:21:21 +0000583 // The inequality tautologies have exactly the same structure, except
584 // applying De Morgan's laws.
585
586 llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr");
587 llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr");
588
John McCalle9fd7eb2010-08-22 08:30:07 +0000589 // This condition tests whether L.ptr == R.ptr. This must always be
590 // true for equality to hold.
591 llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr");
592
593 // This condition, together with the assumption that L.ptr == R.ptr,
594 // tests whether the pointers are both null. ARM imposes an extra
595 // condition.
596 llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType());
597 llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null");
598
599 // This condition tests whether L.adj == R.adj. If this isn't
600 // true, the pointers are unequal unless they're both null.
John McCalld608cdb2010-08-22 10:59:02 +0000601 llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj");
602 llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj");
John McCalle9fd7eb2010-08-22 08:30:07 +0000603 llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj");
604
605 // Null member function pointers on ARM clear the low bit of Adj,
606 // so the zero condition has to check that neither low bit is set.
607 if (IsARM) {
608 llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1);
609
610 // Compute (l.adj | r.adj) & 1 and test it against zero.
611 llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj");
612 llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One);
613 llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero,
614 "cmp.or.adj");
615 EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero);
616 }
617
618 // Tie together all our conditions.
619 llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq);
620 Result = Builder.CreateBinOp(And, PtrEq, Result,
621 Inequality ? "memptr.ne" : "memptr.eq");
622 return Result;
623}
624
625llvm::Value *
John McCall0bab0cd2010-08-23 01:21:21 +0000626ItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
627 llvm::Value *MemPtr,
628 const MemberPointerType *MPT) {
John McCalle9fd7eb2010-08-22 08:30:07 +0000629 CGBuilderTy &Builder = CGF.Builder;
John McCall0bab0cd2010-08-23 01:21:21 +0000630
631 /// For member data pointers, this is just a check against -1.
632 if (MPT->isMemberDataPointer()) {
633 assert(MemPtr->getType() == getPtrDiffTy());
634 llvm::Value *NegativeOne =
635 llvm::Constant::getAllOnesValue(MemPtr->getType());
636 return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool");
637 }
John McCalle9fd7eb2010-08-22 08:30:07 +0000638
639 // In Itanium, a member function pointer is null if 'ptr' is null.
John McCalld608cdb2010-08-22 10:59:02 +0000640 llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr");
John McCalle9fd7eb2010-08-22 08:30:07 +0000641
642 llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0);
643 llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool");
644
645 // In ARM, it's that, plus the low bit of 'adj' must be zero.
646 if (IsARM) {
647 llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1);
John McCalld608cdb2010-08-22 10:59:02 +0000648 llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj");
John McCalle9fd7eb2010-08-22 08:30:07 +0000649 llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit");
650 llvm::Value *IsNotVirtual = Builder.CreateICmpEQ(VirtualBit, Zero,
651 "memptr.notvirtual");
652 Result = Builder.CreateAnd(Result, IsNotVirtual);
653 }
654
655 return Result;
656}
John McCall875ab102010-08-22 06:43:33 +0000657
John McCallf16aa102010-08-22 21:01:12 +0000658/// The Itanium ABI requires non-zero initialization only for data
659/// member pointers, for which '0' is a valid offset.
660bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
661 return MPT->getPointeeType()->isFunctionType();
John McCallcf2c85e2010-08-22 04:16:24 +0000662}
John McCall4c40d982010-08-31 07:33:07 +0000663
664/// The generic ABI passes 'this', plus a VTT if it's initializing a
665/// base subobject.
666void ItaniumCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor,
667 CXXCtorType Type,
668 CanQualType &ResTy,
669 llvm::SmallVectorImpl<CanQualType> &ArgTys) {
670 ASTContext &Context = CGM.getContext();
671
672 // 'this' is already there.
673
674 // Check if we need to add a VTT parameter (which has type void **).
675 if (Type == Ctor_Base && Ctor->getParent()->getNumVBases() != 0)
676 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
677}
678
679/// The ARM ABI does the same as the Itanium ABI, but returns 'this'.
680void ARMCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor,
681 CXXCtorType Type,
682 CanQualType &ResTy,
683 llvm::SmallVectorImpl<CanQualType> &ArgTys) {
684 ItaniumCXXABI::BuildConstructorSignature(Ctor, Type, ResTy, ArgTys);
685 ResTy = ArgTys[0];
686}
687
688/// The generic ABI passes 'this', plus a VTT if it's destroying a
689/// base subobject.
690void ItaniumCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor,
691 CXXDtorType Type,
692 CanQualType &ResTy,
693 llvm::SmallVectorImpl<CanQualType> &ArgTys) {
694 ASTContext &Context = CGM.getContext();
695
696 // 'this' is already there.
697
698 // Check if we need to add a VTT parameter (which has type void **).
699 if (Type == Dtor_Base && Dtor->getParent()->getNumVBases() != 0)
700 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
701}
702
703/// The ARM ABI does the same as the Itanium ABI, but returns 'this'
704/// for non-deleting destructors.
705void ARMCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor,
706 CXXDtorType Type,
707 CanQualType &ResTy,
708 llvm::SmallVectorImpl<CanQualType> &ArgTys) {
709 ItaniumCXXABI::BuildDestructorSignature(Dtor, Type, ResTy, ArgTys);
710
711 if (Type != Dtor_Deleting)
712 ResTy = ArgTys[0];
713}
714
715void ItaniumCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF,
716 QualType &ResTy,
717 FunctionArgList &Params) {
718 /// Create the 'this' variable.
719 BuildThisParam(CGF, Params);
720
721 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
722 assert(MD->isInstance());
723
724 // Check if we need a VTT parameter as well.
725 if (CodeGenVTables::needsVTTParameter(CGF.CurGD)) {
726 ASTContext &Context = CGF.getContext();
727
728 // FIXME: avoid the fake decl
729 QualType T = Context.getPointerType(Context.VoidPtrTy);
730 ImplicitParamDecl *VTTDecl
731 = ImplicitParamDecl::Create(Context, 0, MD->getLocation(),
732 &Context.Idents.get("vtt"), T);
733 Params.push_back(std::make_pair(VTTDecl, VTTDecl->getType()));
734 getVTTDecl(CGF) = VTTDecl;
735 }
736}
737
738void ARMCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF,
739 QualType &ResTy,
740 FunctionArgList &Params) {
741 ItaniumCXXABI::BuildInstanceFunctionParams(CGF, ResTy, Params);
742
743 // Return 'this' from certain constructors and destructors.
744 if (HasThisReturn(CGF.CurGD))
745 ResTy = Params[0].second;
746}
747
748void ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
749 /// Initialize the 'this' slot.
750 EmitThisParam(CGF);
751
752 /// Initialize the 'vtt' slot if needed.
753 if (getVTTDecl(CGF)) {
754 getVTTValue(CGF)
755 = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(getVTTDecl(CGF)),
756 "vtt");
757 }
758}
759
760void ARMCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
761 ItaniumCXXABI::EmitInstanceFunctionProlog(CGF);
762
763 /// Initialize the return slot to 'this' at the start of the
764 /// function.
765 if (HasThisReturn(CGF.CurGD))
766 CGF.Builder.CreateStore(CGF.LoadCXXThis(), CGF.ReturnValue);
767}
768
769void ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF,
770 RValue RV, QualType ResultType) {
771 if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl()))
772 return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType);
773
774 // Destructor thunks in the ARM ABI have indeterminate results.
775 const llvm::Type *T =
776 cast<llvm::PointerType>(CGF.ReturnValue->getType())->getElementType();
777 RValue Undef = RValue::get(llvm::UndefValue::get(T));
778 return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType);
779}