blob: 8a7ac0a83a4265679c6b34d1adc5b1c55136073c [file] [log] [blame]
Charles Davis4e786dd2010-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 McCall86353412010-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 Davis4e786dd2010-05-25 19:52:27 +000019//===----------------------------------------------------------------------===//
20
21#include "CGCXXABI.h"
John McCall7a9aac22010-08-23 01:21:21 +000022#include "CGRecordLayout.h"
John McCall475999d2010-08-22 00:05:51 +000023#include "CodeGenFunction.h"
Charles Davis4e786dd2010-05-25 19:52:27 +000024#include "CodeGenModule.h"
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000025#include <clang/AST/Mangle.h>
John McCall475999d2010-08-22 00:05:51 +000026#include <clang/AST/Type.h>
John McCall7a9aac22010-08-23 01:21:21 +000027#include <llvm/Target/TargetData.h>
John McCall475999d2010-08-22 00:05:51 +000028#include <llvm/Value.h>
Charles Davis4e786dd2010-05-25 19:52:27 +000029
30using namespace clang;
John McCall475999d2010-08-22 00:05:51 +000031using namespace CodeGen;
Charles Davis4e786dd2010-05-25 19:52:27 +000032
33namespace {
Charles Davis53c59df2010-08-16 03:33:14 +000034class ItaniumCXXABI : public CodeGen::CGCXXABI {
John McCall7a9aac22010-08-23 01:21:21 +000035private:
36 const llvm::IntegerType *PtrDiffTy;
John McCall475999d2010-08-22 00:05:51 +000037protected:
John McCalld9c6c0b2010-08-22 00:59:17 +000038 bool IsARM;
John McCall7a9aac22010-08-23 01:21:21 +000039
40 // It's a little silly for us to cache this.
41 const llvm::IntegerType *getPtrDiffTy() {
42 if (!PtrDiffTy) {
John McCall9bca9232010-09-02 10:25:57 +000043 QualType T = getContext().getPointerDiffType();
John McCall7a9aac22010-08-23 01:21:21 +000044 const llvm::Type *Ty = CGM.getTypes().ConvertTypeRecursive(T);
45 PtrDiffTy = cast<llvm::IntegerType>(Ty);
46 }
47 return PtrDiffTy;
48 }
49
John McCall284c48f2011-01-27 09:37:56 +000050 bool NeedsArrayCookie(const CXXNewExpr *expr);
51 bool NeedsArrayCookie(const CXXDeleteExpr *expr,
52 QualType elementType);
John McCall8ed55a52010-09-02 09:58:18 +000053
Charles Davis4e786dd2010-05-25 19:52:27 +000054public:
John McCalld9c6c0b2010-08-22 00:59:17 +000055 ItaniumCXXABI(CodeGen::CodeGenModule &CGM, bool IsARM = false) :
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000056 CGCXXABI(CGM), PtrDiffTy(0), IsARM(IsARM) { }
John McCall475999d2010-08-22 00:05:51 +000057
John McCall614dbdc2010-08-22 21:01:12 +000058 bool isZeroInitializable(const MemberPointerType *MPT);
John McCall84fa5102010-08-22 04:16:24 +000059
John McCall7a9aac22010-08-23 01:21:21 +000060 const llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT);
61
John McCall475999d2010-08-22 00:05:51 +000062 llvm::Value *EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
63 llvm::Value *&This,
64 llvm::Value *MemFnPtr,
65 const MemberPointerType *MPT);
John McCalla8bbb822010-08-22 03:04:22 +000066
John McCallc134eb52010-08-31 21:07:20 +000067 llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
68 llvm::Value *Base,
69 llvm::Value *MemPtr,
70 const MemberPointerType *MPT);
71
John McCall7a9aac22010-08-23 01:21:21 +000072 llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
73 const CastExpr *E,
74 llvm::Value *Src);
John McCall84fa5102010-08-22 04:16:24 +000075
John McCall7a9aac22010-08-23 01:21:21 +000076 llvm::Constant *EmitMemberPointerConversion(llvm::Constant *C,
77 const CastExpr *E);
John McCall84fa5102010-08-22 04:16:24 +000078
John McCall7a9aac22010-08-23 01:21:21 +000079 llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
John McCall84fa5102010-08-22 04:16:24 +000080
John McCall7a9aac22010-08-23 01:21:21 +000081 llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
82 llvm::Constant *EmitMemberPointer(const FieldDecl *FD);
John McCall1c456c82010-08-22 06:43:33 +000083
John McCall7a9aac22010-08-23 01:21:21 +000084 llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF,
85 llvm::Value *L,
86 llvm::Value *R,
87 const MemberPointerType *MPT,
88 bool Inequality);
John McCall131d97d2010-08-22 08:30:07 +000089
John McCall7a9aac22010-08-23 01:21:21 +000090 llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
91 llvm::Value *Addr,
92 const MemberPointerType *MPT);
John McCall5d865c322010-08-31 07:33:07 +000093
94 void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
95 CXXCtorType T,
96 CanQualType &ResTy,
97 llvm::SmallVectorImpl<CanQualType> &ArgTys);
98
99 void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
100 CXXDtorType T,
101 CanQualType &ResTy,
102 llvm::SmallVectorImpl<CanQualType> &ArgTys);
103
104 void BuildInstanceFunctionParams(CodeGenFunction &CGF,
105 QualType &ResTy,
106 FunctionArgList &Params);
107
108 void EmitInstanceFunctionProlog(CodeGenFunction &CGF);
John McCall8ed55a52010-09-02 09:58:18 +0000109
John McCall284c48f2011-01-27 09:37:56 +0000110 CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
John McCall8ed55a52010-09-02 09:58:18 +0000111 llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
112 llvm::Value *NewPtr,
113 llvm::Value *NumElements,
John McCall284c48f2011-01-27 09:37:56 +0000114 const CXXNewExpr *expr,
John McCall8ed55a52010-09-02 09:58:18 +0000115 QualType ElementType);
116 void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
John McCall284c48f2011-01-27 09:37:56 +0000117 const CXXDeleteExpr *expr,
John McCall8ed55a52010-09-02 09:58:18 +0000118 QualType ElementType, llvm::Value *&NumElements,
119 llvm::Value *&AllocPtr, CharUnits &CookieSize);
John McCall68ff0372010-09-08 01:44:27 +0000120
John McCallcdf7ef52010-11-06 09:44:32 +0000121 void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
122 llvm::GlobalVariable *DeclPtr);
Charles Davis4e786dd2010-05-25 19:52:27 +0000123};
John McCall86353412010-08-21 22:46:04 +0000124
125class ARMCXXABI : public ItaniumCXXABI {
126public:
John McCalld9c6c0b2010-08-22 00:59:17 +0000127 ARMCXXABI(CodeGen::CodeGenModule &CGM) : ItaniumCXXABI(CGM, /*ARM*/ true) {}
John McCall5d865c322010-08-31 07:33:07 +0000128
129 void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
130 CXXCtorType T,
131 CanQualType &ResTy,
132 llvm::SmallVectorImpl<CanQualType> &ArgTys);
133
134 void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
135 CXXDtorType T,
136 CanQualType &ResTy,
137 llvm::SmallVectorImpl<CanQualType> &ArgTys);
138
139 void BuildInstanceFunctionParams(CodeGenFunction &CGF,
140 QualType &ResTy,
141 FunctionArgList &Params);
142
143 void EmitInstanceFunctionProlog(CodeGenFunction &CGF);
144
145 void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV, QualType ResTy);
146
John McCall284c48f2011-01-27 09:37:56 +0000147 CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
John McCall8ed55a52010-09-02 09:58:18 +0000148 llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
149 llvm::Value *NewPtr,
150 llvm::Value *NumElements,
John McCall284c48f2011-01-27 09:37:56 +0000151 const CXXNewExpr *expr,
John McCall8ed55a52010-09-02 09:58:18 +0000152 QualType ElementType);
153 void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
John McCall284c48f2011-01-27 09:37:56 +0000154 const CXXDeleteExpr *expr,
John McCall8ed55a52010-09-02 09:58:18 +0000155 QualType ElementType, llvm::Value *&NumElements,
156 llvm::Value *&AllocPtr, CharUnits &CookieSize);
John McCall5d865c322010-08-31 07:33:07 +0000157
158private:
159 /// \brief Returns true if the given instance method is one of the
160 /// kinds that the ARM ABI says returns 'this'.
161 static bool HasThisReturn(GlobalDecl GD) {
162 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
163 return ((isa<CXXDestructorDecl>(MD) && GD.getDtorType() != Dtor_Deleting) ||
164 (isa<CXXConstructorDecl>(MD)));
165 }
John McCall86353412010-08-21 22:46:04 +0000166};
Charles Davis4e786dd2010-05-25 19:52:27 +0000167}
168
Charles Davis53c59df2010-08-16 03:33:14 +0000169CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) {
Charles Davis4e786dd2010-05-25 19:52:27 +0000170 return new ItaniumCXXABI(CGM);
171}
172
John McCall86353412010-08-21 22:46:04 +0000173CodeGen::CGCXXABI *CodeGen::CreateARMCXXABI(CodeGenModule &CGM) {
174 return new ARMCXXABI(CGM);
175}
176
John McCall7a9aac22010-08-23 01:21:21 +0000177const llvm::Type *
178ItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
179 if (MPT->isMemberDataPointer())
180 return getPtrDiffTy();
181 else
182 return llvm::StructType::get(CGM.getLLVMContext(),
183 getPtrDiffTy(), getPtrDiffTy(), NULL);
John McCall1c456c82010-08-22 06:43:33 +0000184}
185
John McCalld9c6c0b2010-08-22 00:59:17 +0000186/// In the Itanium and ARM ABIs, method pointers have the form:
187/// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr;
188///
189/// In the Itanium ABI:
190/// - method pointers are virtual if (memptr.ptr & 1) is nonzero
191/// - the this-adjustment is (memptr.adj)
192/// - the virtual offset is (memptr.ptr - 1)
193///
194/// In the ARM ABI:
195/// - method pointers are virtual if (memptr.adj & 1) is nonzero
196/// - the this-adjustment is (memptr.adj >> 1)
197/// - the virtual offset is (memptr.ptr)
198/// ARM uses 'adj' for the virtual flag because Thumb functions
199/// may be only single-byte aligned.
200///
201/// If the member is virtual, the adjusted 'this' pointer points
202/// to a vtable pointer from which the virtual offset is applied.
203///
204/// If the member is non-virtual, memptr.ptr is the address of
205/// the function to call.
John McCall475999d2010-08-22 00:05:51 +0000206llvm::Value *
207ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
208 llvm::Value *&This,
209 llvm::Value *MemFnPtr,
210 const MemberPointerType *MPT) {
211 CGBuilderTy &Builder = CGF.Builder;
212
213 const FunctionProtoType *FPT =
214 MPT->getPointeeType()->getAs<FunctionProtoType>();
215 const CXXRecordDecl *RD =
216 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
217
218 const llvm::FunctionType *FTy =
219 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
220 FPT->isVariadic());
221
John McCall7a9aac22010-08-23 01:21:21 +0000222 const llvm::IntegerType *ptrdiff = getPtrDiffTy();
John McCalld9c6c0b2010-08-22 00:59:17 +0000223 llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(ptrdiff, 1);
John McCall475999d2010-08-22 00:05:51 +0000224
John McCalld9c6c0b2010-08-22 00:59:17 +0000225 llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual");
226 llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual");
227 llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end");
228
John McCalla1dee5302010-08-22 10:59:02 +0000229 // Extract memptr.adj, which is in the second field.
230 llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj");
John McCalld9c6c0b2010-08-22 00:59:17 +0000231
232 // Compute the true adjustment.
233 llvm::Value *Adj = RawAdj;
234 if (IsARM)
235 Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted");
John McCall475999d2010-08-22 00:05:51 +0000236
237 // Apply the adjustment and cast back to the original struct type
238 // for consistency.
John McCalld9c6c0b2010-08-22 00:59:17 +0000239 llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy());
240 Ptr = Builder.CreateInBoundsGEP(Ptr, Adj);
241 This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted");
John McCall475999d2010-08-22 00:05:51 +0000242
243 // Load the function pointer.
John McCalla1dee5302010-08-22 10:59:02 +0000244 llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr");
John McCall475999d2010-08-22 00:05:51 +0000245
246 // If the LSB in the function pointer is 1, the function pointer points to
247 // a virtual function.
John McCalld9c6c0b2010-08-22 00:59:17 +0000248 llvm::Value *IsVirtual;
249 if (IsARM)
250 IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1);
251 else
252 IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1);
253 IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual");
John McCall475999d2010-08-22 00:05:51 +0000254 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
255
256 // In the virtual path, the adjustment left 'This' pointing to the
257 // vtable of the correct base subobject. The "function pointer" is an
John McCalld9c6c0b2010-08-22 00:59:17 +0000258 // offset within the vtable (+1 for the virtual flag on non-ARM).
John McCall475999d2010-08-22 00:05:51 +0000259 CGF.EmitBlock(FnVirtual);
260
261 // Cast the adjusted this to a pointer to vtable pointer and load.
262 const llvm::Type *VTableTy = Builder.getInt8PtrTy();
263 llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy->getPointerTo());
John McCalld9c6c0b2010-08-22 00:59:17 +0000264 VTable = Builder.CreateLoad(VTable, "memptr.vtable");
John McCall475999d2010-08-22 00:05:51 +0000265
266 // Apply the offset.
John McCalld9c6c0b2010-08-22 00:59:17 +0000267 llvm::Value *VTableOffset = FnAsInt;
268 if (!IsARM) VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1);
269 VTable = Builder.CreateGEP(VTable, VTableOffset);
John McCall475999d2010-08-22 00:05:51 +0000270
271 // Load the virtual function to call.
272 VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo());
John McCalld9c6c0b2010-08-22 00:59:17 +0000273 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn");
John McCall475999d2010-08-22 00:05:51 +0000274 CGF.EmitBranch(FnEnd);
275
276 // In the non-virtual path, the function pointer is actually a
277 // function pointer.
278 CGF.EmitBlock(FnNonVirtual);
279 llvm::Value *NonVirtualFn =
John McCalld9c6c0b2010-08-22 00:59:17 +0000280 Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn");
John McCall475999d2010-08-22 00:05:51 +0000281
282 // We're done.
283 CGF.EmitBlock(FnEnd);
284 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
285 Callee->reserveOperandSpace(2);
286 Callee->addIncoming(VirtualFn, FnVirtual);
287 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
288 return Callee;
289}
John McCalla8bbb822010-08-22 03:04:22 +0000290
John McCallc134eb52010-08-31 21:07:20 +0000291/// Compute an l-value by applying the given pointer-to-member to a
292/// base object.
293llvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF,
294 llvm::Value *Base,
295 llvm::Value *MemPtr,
296 const MemberPointerType *MPT) {
297 assert(MemPtr->getType() == getPtrDiffTy());
298
299 CGBuilderTy &Builder = CGF.Builder;
300
301 unsigned AS = cast<llvm::PointerType>(Base->getType())->getAddressSpace();
302
303 // Cast to char*.
304 Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS));
305
306 // Apply the offset, which we assume is non-null.
307 llvm::Value *Addr = Builder.CreateInBoundsGEP(Base, MemPtr, "memptr.offset");
308
309 // Cast the address to the appropriate pointer type, adopting the
310 // address space of the base pointer.
Douglas Gregor04f36212010-09-02 17:38:50 +0000311 const llvm::Type *PType
312 = CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS);
John McCallc134eb52010-08-31 21:07:20 +0000313 return Builder.CreateBitCast(Addr, PType);
314}
315
John McCalla8bbb822010-08-22 03:04:22 +0000316/// Perform a derived-to-base or base-to-derived member pointer conversion.
John McCall7a9aac22010-08-23 01:21:21 +0000317///
318/// Obligatory offset/adjustment diagram:
319/// <-- offset --> <-- adjustment -->
320/// |--------------------------|----------------------|--------------------|
321/// ^Derived address point ^Base address point ^Member address point
322///
323/// So when converting a base member pointer to a derived member pointer,
324/// we add the offset to the adjustment because the address point has
325/// decreased; and conversely, when converting a derived MP to a base MP
326/// we subtract the offset from the adjustment because the address point
327/// has increased.
328///
329/// The standard forbids (at compile time) conversion to and from
330/// virtual bases, which is why we don't have to consider them here.
331///
332/// The standard forbids (at run time) casting a derived MP to a base
333/// MP when the derived MP does not point to a member of the base.
334/// This is why -1 is a reasonable choice for null data member
335/// pointers.
John McCalla1dee5302010-08-22 10:59:02 +0000336llvm::Value *
John McCall7a9aac22010-08-23 01:21:21 +0000337ItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
338 const CastExpr *E,
339 llvm::Value *Src) {
John McCalle3027922010-08-25 11:45:40 +0000340 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
341 E->getCastKind() == CK_BaseToDerivedMemberPointer);
John McCalla8bbb822010-08-22 03:04:22 +0000342
John McCalla1dee5302010-08-22 10:59:02 +0000343 if (isa<llvm::Constant>(Src))
John McCall7a9aac22010-08-23 01:21:21 +0000344 return EmitMemberPointerConversion(cast<llvm::Constant>(Src), E);
John McCalla1dee5302010-08-22 10:59:02 +0000345
John McCalla8bbb822010-08-22 03:04:22 +0000346 CGBuilderTy &Builder = CGF.Builder;
347
348 const MemberPointerType *SrcTy =
349 E->getSubExpr()->getType()->getAs<MemberPointerType>();
350 const MemberPointerType *DestTy = E->getType()->getAs<MemberPointerType>();
351
352 const CXXRecordDecl *SrcDecl = SrcTy->getClass()->getAsCXXRecordDecl();
353 const CXXRecordDecl *DestDecl = DestTy->getClass()->getAsCXXRecordDecl();
354
John McCalla8bbb822010-08-22 03:04:22 +0000355 bool DerivedToBase =
John McCalle3027922010-08-25 11:45:40 +0000356 E->getCastKind() == CK_DerivedToBaseMemberPointer;
John McCalla8bbb822010-08-22 03:04:22 +0000357
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000358 const CXXRecordDecl *DerivedDecl;
John McCalla8bbb822010-08-22 03:04:22 +0000359 if (DerivedToBase)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000360 DerivedDecl = SrcDecl;
John McCalla8bbb822010-08-22 03:04:22 +0000361 else
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000362 DerivedDecl = DestDecl;
John McCalla8bbb822010-08-22 03:04:22 +0000363
John McCalla1dee5302010-08-22 10:59:02 +0000364 llvm::Constant *Adj =
365 CGF.CGM.GetNonVirtualBaseClassOffset(DerivedDecl,
366 E->path_begin(),
367 E->path_end());
368 if (!Adj) return Src;
John McCall1c456c82010-08-22 06:43:33 +0000369
John McCall7a9aac22010-08-23 01:21:21 +0000370 // For member data pointers, this is just a matter of adding the
371 // offset if the source is non-null.
372 if (SrcTy->isMemberDataPointer()) {
373 llvm::Value *Dst;
374 if (DerivedToBase)
375 Dst = Builder.CreateNSWSub(Src, Adj, "adj");
376 else
377 Dst = Builder.CreateNSWAdd(Src, Adj, "adj");
378
379 // Null check.
380 llvm::Value *Null = llvm::Constant::getAllOnesValue(Src->getType());
381 llvm::Value *IsNull = Builder.CreateICmpEQ(Src, Null, "memptr.isnull");
382 return Builder.CreateSelect(IsNull, Src, Dst);
383 }
384
John McCalla1dee5302010-08-22 10:59:02 +0000385 // The this-adjustment is left-shifted by 1 on ARM.
386 if (IsARM) {
387 uint64_t Offset = cast<llvm::ConstantInt>(Adj)->getZExtValue();
388 Offset <<= 1;
389 Adj = llvm::ConstantInt::get(Adj->getType(), Offset);
390 }
391
John McCallc29eb8a2010-08-22 11:04:31 +0000392 llvm::Value *SrcAdj = Builder.CreateExtractValue(Src, 1, "src.adj");
John McCalla1dee5302010-08-22 10:59:02 +0000393 llvm::Value *DstAdj;
394 if (DerivedToBase)
John McCall7a9aac22010-08-23 01:21:21 +0000395 DstAdj = Builder.CreateNSWSub(SrcAdj, Adj, "adj");
John McCalla1dee5302010-08-22 10:59:02 +0000396 else
John McCall7a9aac22010-08-23 01:21:21 +0000397 DstAdj = Builder.CreateNSWAdd(SrcAdj, Adj, "adj");
John McCalla1dee5302010-08-22 10:59:02 +0000398
John McCallc29eb8a2010-08-22 11:04:31 +0000399 return Builder.CreateInsertValue(Src, DstAdj, 1);
John McCalla8bbb822010-08-22 03:04:22 +0000400}
John McCall84fa5102010-08-22 04:16:24 +0000401
402llvm::Constant *
John McCall7a9aac22010-08-23 01:21:21 +0000403ItaniumCXXABI::EmitMemberPointerConversion(llvm::Constant *C,
404 const CastExpr *E) {
John McCall84fa5102010-08-22 04:16:24 +0000405 const MemberPointerType *SrcTy =
406 E->getSubExpr()->getType()->getAs<MemberPointerType>();
407 const MemberPointerType *DestTy =
408 E->getType()->getAs<MemberPointerType>();
409
410 bool DerivedToBase =
John McCalle3027922010-08-25 11:45:40 +0000411 E->getCastKind() == CK_DerivedToBaseMemberPointer;
John McCall84fa5102010-08-22 04:16:24 +0000412
413 const CXXRecordDecl *DerivedDecl;
414 if (DerivedToBase)
415 DerivedDecl = SrcTy->getClass()->getAsCXXRecordDecl();
416 else
417 DerivedDecl = DestTy->getClass()->getAsCXXRecordDecl();
418
419 // Calculate the offset to the base class.
420 llvm::Constant *Offset =
421 CGM.GetNonVirtualBaseClassOffset(DerivedDecl,
422 E->path_begin(),
423 E->path_end());
424 // If there's no offset, we're done.
425 if (!Offset) return C;
426
John McCall7a9aac22010-08-23 01:21:21 +0000427 // If the source is a member data pointer, we have to do a null
428 // check and then add the offset. In the common case, we can fold
429 // away the offset.
430 if (SrcTy->isMemberDataPointer()) {
431 assert(C->getType() == getPtrDiffTy());
432
433 // If it's a constant int, just create a new constant int.
434 if (llvm::ConstantInt *CI = dyn_cast<llvm::ConstantInt>(C)) {
435 int64_t Src = CI->getSExtValue();
436
437 // Null converts to null.
438 if (Src == -1) return CI;
439
440 // Otherwise, just add the offset.
441 int64_t OffsetV = cast<llvm::ConstantInt>(Offset)->getSExtValue();
442 int64_t Dst = (DerivedToBase ? Src - OffsetV : Src + OffsetV);
443 return llvm::ConstantInt::get(CI->getType(), Dst, /*signed*/ true);
444 }
445
446 // Otherwise, we have to form a constant select expression.
447 llvm::Constant *Null = llvm::Constant::getAllOnesValue(C->getType());
448
449 llvm::Constant *IsNull =
450 llvm::ConstantExpr::getICmp(llvm::ICmpInst::ICMP_EQ, C, Null);
451
452 llvm::Constant *Dst;
453 if (DerivedToBase)
454 Dst = llvm::ConstantExpr::getNSWSub(C, Offset);
455 else
456 Dst = llvm::ConstantExpr::getNSWAdd(C, Offset);
457
458 return llvm::ConstantExpr::getSelect(IsNull, Null, Dst);
459 }
460
John McCall1c456c82010-08-22 06:43:33 +0000461 // The this-adjustment is left-shifted by 1 on ARM.
462 if (IsARM) {
John McCall7a9aac22010-08-23 01:21:21 +0000463 int64_t OffsetV = cast<llvm::ConstantInt>(Offset)->getSExtValue();
John McCall1c456c82010-08-22 06:43:33 +0000464 OffsetV <<= 1;
465 Offset = llvm::ConstantInt::get(Offset->getType(), OffsetV);
466 }
467
John McCall84fa5102010-08-22 04:16:24 +0000468 llvm::ConstantStruct *CS = cast<llvm::ConstantStruct>(C);
469
John McCall7a9aac22010-08-23 01:21:21 +0000470 llvm::Constant *Values[2] = { CS->getOperand(0), 0 };
471 if (DerivedToBase)
472 Values[1] = llvm::ConstantExpr::getSub(CS->getOperand(1), Offset);
473 else
474 Values[1] = llvm::ConstantExpr::getAdd(CS->getOperand(1), Offset);
475
John McCall84fa5102010-08-22 04:16:24 +0000476 return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2,
477 /*Packed=*/false);
478}
479
480
John McCall84fa5102010-08-22 04:16:24 +0000481llvm::Constant *
John McCall7a9aac22010-08-23 01:21:21 +0000482ItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
483 const llvm::Type *ptrdiff_t = getPtrDiffTy();
484
485 // Itanium C++ ABI 2.3:
486 // A NULL pointer is represented as -1.
487 if (MPT->isMemberDataPointer())
488 return llvm::ConstantInt::get(ptrdiff_t, -1ULL, /*isSigned=*/true);
John McCalla1dee5302010-08-22 10:59:02 +0000489
490 llvm::Constant *Zero = llvm::ConstantInt::get(ptrdiff_t, 0);
491 llvm::Constant *Values[2] = { Zero, Zero };
492 return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2,
493 /*Packed=*/false);
John McCall84fa5102010-08-22 04:16:24 +0000494}
495
Argyrios Kyrtzidis8322b422011-01-31 07:04:29 +0000496static uint64_t getFieldOffset(const FieldDecl *FD, CodeGenModule &CGM) {
497 const CGRecordLayout &RL = CGM.getTypes().getCGRecordLayout(FD->getParent());
498 const llvm::StructType *ClassLTy = RL.getLLVMType();
499
500 unsigned FieldNo = RL.getLLVMFieldNo(FD);
501 return
502 CGM.getTargetData().getStructLayout(ClassLTy)->getElementOffset(FieldNo);
503}
504
John McCall7a9aac22010-08-23 01:21:21 +0000505llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const FieldDecl *FD) {
506 // Itanium C++ ABI 2.3:
507 // A pointer to data member is an offset from the base address of
508 // the class object containing it, represented as a ptrdiff_t
509
Argyrios Kyrtzidis8322b422011-01-31 07:04:29 +0000510 const RecordDecl *parent = FD->getParent();
511 if (!parent->isAnonymousStructOrUnion())
512 return llvm::ConstantInt::get(getPtrDiffTy(), getFieldOffset(FD, CGM));
Anders Carlsson72b19682010-11-24 21:53:50 +0000513
Argyrios Kyrtzidis8322b422011-01-31 07:04:29 +0000514 // Handle a field injected from an anonymous struct or union.
515
516 assert(FD->getDeclName() && "Requested pointer to member with no name!");
517
518 // Find the record which the field was injected into.
519 while (parent->isAnonymousStructOrUnion())
520 parent = cast<RecordDecl>(parent->getParent());
521
522 RecordDecl::lookup_const_result lookup = parent->lookup(FD->getDeclName());
523 assert(lookup.first != lookup.second && "Didn't find the field!");
524 const IndirectFieldDecl *indirectFD = cast<IndirectFieldDecl>(*lookup.first);
525
526 uint64_t Offset = 0;
527 for (IndirectFieldDecl::chain_iterator
528 I= indirectFD->chain_begin(), E= indirectFD->chain_end(); I!=E; ++I) {
529 Offset += getFieldOffset(cast<FieldDecl>(*I), CGM);
530 }
John McCall7a9aac22010-08-23 01:21:21 +0000531
532 return llvm::ConstantInt::get(getPtrDiffTy(), Offset);
533}
534
535llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) {
John McCalla1dee5302010-08-22 10:59:02 +0000536 assert(MD->isInstance() && "Member function must not be static!");
537 MD = MD->getCanonicalDecl();
538
539 CodeGenTypes &Types = CGM.getTypes();
John McCall7a9aac22010-08-23 01:21:21 +0000540 const llvm::Type *ptrdiff_t = getPtrDiffTy();
John McCalla1dee5302010-08-22 10:59:02 +0000541
542 // Get the function pointer (or index if this is a virtual function).
543 llvm::Constant *MemPtr[2];
544 if (MD->isVirtual()) {
545 uint64_t Index = CGM.getVTables().getMethodVTableIndex(MD);
546
547 // FIXME: We shouldn't use / 8 here.
548 uint64_t PointerWidthInBytes =
John McCall9bca9232010-09-02 10:25:57 +0000549 getContext().Target.getPointerWidth(0) / 8;
John McCalla1dee5302010-08-22 10:59:02 +0000550 uint64_t VTableOffset = (Index * PointerWidthInBytes);
551
552 if (IsARM) {
553 // ARM C++ ABI 3.2.1:
554 // This ABI specifies that adj contains twice the this
555 // adjustment, plus 1 if the member function is virtual. The
556 // least significant bit of adj then makes exactly the same
557 // discrimination as the least significant bit of ptr does for
558 // Itanium.
559 MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset);
560 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 1);
561 } else {
562 // Itanium C++ ABI 2.3:
563 // For a virtual function, [the pointer field] is 1 plus the
564 // virtual table offset (in bytes) of the function,
565 // represented as a ptrdiff_t.
566 MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset + 1);
567 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0);
568 }
569 } else {
570 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
571 const llvm::Type *Ty;
572 // Check whether the function has a computable LLVM signature.
573 if (!CodeGenTypes::VerifyFuncTypeComplete(FPT)) {
574 // The function has a computable LLVM signature; use the correct type.
575 Ty = Types.GetFunctionType(Types.getFunctionInfo(MD), FPT->isVariadic());
576 } else {
577 // Use an arbitrary non-function type to tell GetAddrOfFunction that the
578 // function type is incomplete.
579 Ty = ptrdiff_t;
580 }
581
582 llvm::Constant *Addr = CGM.GetAddrOfFunction(MD, Ty);
583 MemPtr[0] = llvm::ConstantExpr::getPtrToInt(Addr, ptrdiff_t);
584 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0);
585 }
John McCall1c456c82010-08-22 06:43:33 +0000586
587 return llvm::ConstantStruct::get(CGM.getLLVMContext(),
John McCalla1dee5302010-08-22 10:59:02 +0000588 MemPtr, 2, /*Packed=*/false);
John McCall1c456c82010-08-22 06:43:33 +0000589}
590
John McCall131d97d2010-08-22 08:30:07 +0000591/// The comparison algorithm is pretty easy: the member pointers are
592/// the same if they're either bitwise identical *or* both null.
593///
594/// ARM is different here only because null-ness is more complicated.
595llvm::Value *
John McCall7a9aac22010-08-23 01:21:21 +0000596ItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
597 llvm::Value *L,
598 llvm::Value *R,
599 const MemberPointerType *MPT,
600 bool Inequality) {
John McCall131d97d2010-08-22 08:30:07 +0000601 CGBuilderTy &Builder = CGF.Builder;
602
John McCall131d97d2010-08-22 08:30:07 +0000603 llvm::ICmpInst::Predicate Eq;
604 llvm::Instruction::BinaryOps And, Or;
605 if (Inequality) {
606 Eq = llvm::ICmpInst::ICMP_NE;
607 And = llvm::Instruction::Or;
608 Or = llvm::Instruction::And;
609 } else {
610 Eq = llvm::ICmpInst::ICMP_EQ;
611 And = llvm::Instruction::And;
612 Or = llvm::Instruction::Or;
613 }
614
John McCall7a9aac22010-08-23 01:21:21 +0000615 // Member data pointers are easy because there's a unique null
616 // value, so it just comes down to bitwise equality.
617 if (MPT->isMemberDataPointer())
618 return Builder.CreateICmp(Eq, L, R);
619
620 // For member function pointers, the tautologies are more complex.
621 // The Itanium tautology is:
John McCall61a14882010-08-23 06:56:36 +0000622 // (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj))
John McCall7a9aac22010-08-23 01:21:21 +0000623 // The ARM tautology is:
John McCall61a14882010-08-23 06:56:36 +0000624 // (L == R) <==> (L.ptr == R.ptr &&
625 // (L.adj == R.adj ||
626 // (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0)))
John McCall7a9aac22010-08-23 01:21:21 +0000627 // The inequality tautologies have exactly the same structure, except
628 // applying De Morgan's laws.
629
630 llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr");
631 llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr");
632
John McCall131d97d2010-08-22 08:30:07 +0000633 // This condition tests whether L.ptr == R.ptr. This must always be
634 // true for equality to hold.
635 llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr");
636
637 // This condition, together with the assumption that L.ptr == R.ptr,
638 // tests whether the pointers are both null. ARM imposes an extra
639 // condition.
640 llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType());
641 llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null");
642
643 // This condition tests whether L.adj == R.adj. If this isn't
644 // true, the pointers are unequal unless they're both null.
John McCalla1dee5302010-08-22 10:59:02 +0000645 llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj");
646 llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj");
John McCall131d97d2010-08-22 08:30:07 +0000647 llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj");
648
649 // Null member function pointers on ARM clear the low bit of Adj,
650 // so the zero condition has to check that neither low bit is set.
651 if (IsARM) {
652 llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1);
653
654 // Compute (l.adj | r.adj) & 1 and test it against zero.
655 llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj");
656 llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One);
657 llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero,
658 "cmp.or.adj");
659 EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero);
660 }
661
662 // Tie together all our conditions.
663 llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq);
664 Result = Builder.CreateBinOp(And, PtrEq, Result,
665 Inequality ? "memptr.ne" : "memptr.eq");
666 return Result;
667}
668
669llvm::Value *
John McCall7a9aac22010-08-23 01:21:21 +0000670ItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
671 llvm::Value *MemPtr,
672 const MemberPointerType *MPT) {
John McCall131d97d2010-08-22 08:30:07 +0000673 CGBuilderTy &Builder = CGF.Builder;
John McCall7a9aac22010-08-23 01:21:21 +0000674
675 /// For member data pointers, this is just a check against -1.
676 if (MPT->isMemberDataPointer()) {
677 assert(MemPtr->getType() == getPtrDiffTy());
678 llvm::Value *NegativeOne =
679 llvm::Constant::getAllOnesValue(MemPtr->getType());
680 return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool");
681 }
John McCall131d97d2010-08-22 08:30:07 +0000682
683 // In Itanium, a member function pointer is null if 'ptr' is null.
John McCalla1dee5302010-08-22 10:59:02 +0000684 llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr");
John McCall131d97d2010-08-22 08:30:07 +0000685
686 llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0);
687 llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool");
688
689 // In ARM, it's that, plus the low bit of 'adj' must be zero.
690 if (IsARM) {
691 llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1);
John McCalla1dee5302010-08-22 10:59:02 +0000692 llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj");
John McCall131d97d2010-08-22 08:30:07 +0000693 llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit");
694 llvm::Value *IsNotVirtual = Builder.CreateICmpEQ(VirtualBit, Zero,
695 "memptr.notvirtual");
696 Result = Builder.CreateAnd(Result, IsNotVirtual);
697 }
698
699 return Result;
700}
John McCall1c456c82010-08-22 06:43:33 +0000701
John McCall614dbdc2010-08-22 21:01:12 +0000702/// The Itanium ABI requires non-zero initialization only for data
703/// member pointers, for which '0' is a valid offset.
704bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
705 return MPT->getPointeeType()->isFunctionType();
John McCall84fa5102010-08-22 04:16:24 +0000706}
John McCall5d865c322010-08-31 07:33:07 +0000707
708/// The generic ABI passes 'this', plus a VTT if it's initializing a
709/// base subobject.
710void ItaniumCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor,
711 CXXCtorType Type,
712 CanQualType &ResTy,
713 llvm::SmallVectorImpl<CanQualType> &ArgTys) {
John McCall9bca9232010-09-02 10:25:57 +0000714 ASTContext &Context = getContext();
John McCall5d865c322010-08-31 07:33:07 +0000715
716 // 'this' is already there.
717
718 // Check if we need to add a VTT parameter (which has type void **).
719 if (Type == Ctor_Base && Ctor->getParent()->getNumVBases() != 0)
720 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
721}
722
723/// The ARM ABI does the same as the Itanium ABI, but returns 'this'.
724void ARMCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor,
725 CXXCtorType Type,
726 CanQualType &ResTy,
727 llvm::SmallVectorImpl<CanQualType> &ArgTys) {
728 ItaniumCXXABI::BuildConstructorSignature(Ctor, Type, ResTy, ArgTys);
729 ResTy = ArgTys[0];
730}
731
732/// The generic ABI passes 'this', plus a VTT if it's destroying a
733/// base subobject.
734void ItaniumCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor,
735 CXXDtorType Type,
736 CanQualType &ResTy,
737 llvm::SmallVectorImpl<CanQualType> &ArgTys) {
John McCall9bca9232010-09-02 10:25:57 +0000738 ASTContext &Context = getContext();
John McCall5d865c322010-08-31 07:33:07 +0000739
740 // 'this' is already there.
741
742 // Check if we need to add a VTT parameter (which has type void **).
743 if (Type == Dtor_Base && Dtor->getParent()->getNumVBases() != 0)
744 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
745}
746
747/// The ARM ABI does the same as the Itanium ABI, but returns 'this'
748/// for non-deleting destructors.
749void ARMCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor,
750 CXXDtorType Type,
751 CanQualType &ResTy,
752 llvm::SmallVectorImpl<CanQualType> &ArgTys) {
753 ItaniumCXXABI::BuildDestructorSignature(Dtor, Type, ResTy, ArgTys);
754
755 if (Type != Dtor_Deleting)
756 ResTy = ArgTys[0];
757}
758
759void ItaniumCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF,
760 QualType &ResTy,
761 FunctionArgList &Params) {
762 /// Create the 'this' variable.
763 BuildThisParam(CGF, Params);
764
765 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
766 assert(MD->isInstance());
767
768 // Check if we need a VTT parameter as well.
769 if (CodeGenVTables::needsVTTParameter(CGF.CurGD)) {
John McCall9bca9232010-09-02 10:25:57 +0000770 ASTContext &Context = getContext();
John McCall5d865c322010-08-31 07:33:07 +0000771
772 // FIXME: avoid the fake decl
773 QualType T = Context.getPointerType(Context.VoidPtrTy);
774 ImplicitParamDecl *VTTDecl
775 = ImplicitParamDecl::Create(Context, 0, MD->getLocation(),
776 &Context.Idents.get("vtt"), T);
777 Params.push_back(std::make_pair(VTTDecl, VTTDecl->getType()));
778 getVTTDecl(CGF) = VTTDecl;
779 }
780}
781
782void ARMCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF,
783 QualType &ResTy,
784 FunctionArgList &Params) {
785 ItaniumCXXABI::BuildInstanceFunctionParams(CGF, ResTy, Params);
786
787 // Return 'this' from certain constructors and destructors.
788 if (HasThisReturn(CGF.CurGD))
789 ResTy = Params[0].second;
790}
791
792void ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
793 /// Initialize the 'this' slot.
794 EmitThisParam(CGF);
795
796 /// Initialize the 'vtt' slot if needed.
797 if (getVTTDecl(CGF)) {
798 getVTTValue(CGF)
799 = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(getVTTDecl(CGF)),
800 "vtt");
801 }
802}
803
804void ARMCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
805 ItaniumCXXABI::EmitInstanceFunctionProlog(CGF);
806
807 /// Initialize the return slot to 'this' at the start of the
808 /// function.
809 if (HasThisReturn(CGF.CurGD))
810 CGF.Builder.CreateStore(CGF.LoadCXXThis(), CGF.ReturnValue);
811}
812
813void ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF,
814 RValue RV, QualType ResultType) {
815 if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl()))
816 return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType);
817
818 // Destructor thunks in the ARM ABI have indeterminate results.
819 const llvm::Type *T =
820 cast<llvm::PointerType>(CGF.ReturnValue->getType())->getElementType();
821 RValue Undef = RValue::get(llvm::UndefValue::get(T));
822 return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType);
823}
John McCall8ed55a52010-09-02 09:58:18 +0000824
825/************************** Array allocation cookies **************************/
826
John McCall284c48f2011-01-27 09:37:56 +0000827bool ItaniumCXXABI::NeedsArrayCookie(const CXXNewExpr *expr) {
John McCall8ed55a52010-09-02 09:58:18 +0000828 // If the class's usual deallocation function takes two arguments,
John McCall284c48f2011-01-27 09:37:56 +0000829 // it needs a cookie.
830 if (expr->doesUsualArrayDeleteWantSize())
831 return true;
John McCall8ed55a52010-09-02 09:58:18 +0000832
John McCall284c48f2011-01-27 09:37:56 +0000833 // Otherwise, if the class has a non-trivial destructor, it always
834 // needs a cookie.
835 const CXXRecordDecl *record =
836 expr->getAllocatedType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
837 return (record && !record->hasTrivialDestructor());
John McCall8ed55a52010-09-02 09:58:18 +0000838}
839
John McCall284c48f2011-01-27 09:37:56 +0000840bool ItaniumCXXABI::NeedsArrayCookie(const CXXDeleteExpr *expr,
841 QualType elementType) {
842 // If the class's usual deallocation function takes two arguments,
843 // it needs a cookie.
844 if (expr->doesUsualArrayDeleteWantSize())
845 return true;
846
847 // Otherwise, if the class has a non-trivial destructor, it always
848 // needs a cookie.
849 const CXXRecordDecl *record =
850 elementType->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
851 return (record && !record->hasTrivialDestructor());
852}
853
854CharUnits ItaniumCXXABI::GetArrayCookieSize(const CXXNewExpr *expr) {
855 if (!NeedsArrayCookie(expr))
John McCall8ed55a52010-09-02 09:58:18 +0000856 return CharUnits::Zero();
857
John McCall284c48f2011-01-27 09:37:56 +0000858 // Padding is the maximum of sizeof(size_t) and alignof(elementType)
John McCall9bca9232010-09-02 10:25:57 +0000859 ASTContext &Ctx = getContext();
John McCall8ed55a52010-09-02 09:58:18 +0000860 return std::max(Ctx.getTypeSizeInChars(Ctx.getSizeType()),
John McCall284c48f2011-01-27 09:37:56 +0000861 Ctx.getTypeAlignInChars(expr->getAllocatedType()));
John McCall8ed55a52010-09-02 09:58:18 +0000862}
863
864llvm::Value *ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
865 llvm::Value *NewPtr,
866 llvm::Value *NumElements,
John McCall284c48f2011-01-27 09:37:56 +0000867 const CXXNewExpr *expr,
John McCall8ed55a52010-09-02 09:58:18 +0000868 QualType ElementType) {
John McCall284c48f2011-01-27 09:37:56 +0000869 assert(NeedsArrayCookie(expr));
John McCall8ed55a52010-09-02 09:58:18 +0000870
871 unsigned AS = cast<llvm::PointerType>(NewPtr->getType())->getAddressSpace();
872
John McCall9bca9232010-09-02 10:25:57 +0000873 ASTContext &Ctx = getContext();
John McCall8ed55a52010-09-02 09:58:18 +0000874 QualType SizeTy = Ctx.getSizeType();
875 CharUnits SizeSize = Ctx.getTypeSizeInChars(SizeTy);
876
877 // The size of the cookie.
878 CharUnits CookieSize =
879 std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType));
880
881 // Compute an offset to the cookie.
882 llvm::Value *CookiePtr = NewPtr;
883 CharUnits CookieOffset = CookieSize - SizeSize;
884 if (!CookieOffset.isZero())
885 CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_64(CookiePtr,
886 CookieOffset.getQuantity());
887
888 // Write the number of elements into the appropriate slot.
889 llvm::Value *NumElementsPtr
890 = CGF.Builder.CreateBitCast(CookiePtr,
891 CGF.ConvertType(SizeTy)->getPointerTo(AS));
892 CGF.Builder.CreateStore(NumElements, NumElementsPtr);
893
894 // Finally, compute a pointer to the actual data buffer by skipping
895 // over the cookie completely.
896 return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr,
897 CookieSize.getQuantity());
898}
899
900void ItaniumCXXABI::ReadArrayCookie(CodeGenFunction &CGF,
901 llvm::Value *Ptr,
John McCall284c48f2011-01-27 09:37:56 +0000902 const CXXDeleteExpr *expr,
John McCall8ed55a52010-09-02 09:58:18 +0000903 QualType ElementType,
904 llvm::Value *&NumElements,
905 llvm::Value *&AllocPtr,
906 CharUnits &CookieSize) {
907 // Derive a char* in the same address space as the pointer.
908 unsigned AS = cast<llvm::PointerType>(Ptr->getType())->getAddressSpace();
909 const llvm::Type *CharPtrTy = CGF.Builder.getInt8Ty()->getPointerTo(AS);
910
911 // If we don't need an array cookie, bail out early.
John McCall284c48f2011-01-27 09:37:56 +0000912 if (!NeedsArrayCookie(expr, ElementType)) {
John McCall8ed55a52010-09-02 09:58:18 +0000913 AllocPtr = CGF.Builder.CreateBitCast(Ptr, CharPtrTy);
914 NumElements = 0;
915 CookieSize = CharUnits::Zero();
916 return;
917 }
918
John McCall9bca9232010-09-02 10:25:57 +0000919 QualType SizeTy = getContext().getSizeType();
920 CharUnits SizeSize = getContext().getTypeSizeInChars(SizeTy);
John McCall8ed55a52010-09-02 09:58:18 +0000921 const llvm::Type *SizeLTy = CGF.ConvertType(SizeTy);
922
923 CookieSize
John McCall9bca9232010-09-02 10:25:57 +0000924 = std::max(SizeSize, getContext().getTypeAlignInChars(ElementType));
John McCall8ed55a52010-09-02 09:58:18 +0000925
926 CharUnits NumElementsOffset = CookieSize - SizeSize;
927
928 // Compute the allocated pointer.
929 AllocPtr = CGF.Builder.CreateBitCast(Ptr, CharPtrTy);
930 AllocPtr = CGF.Builder.CreateConstInBoundsGEP1_64(AllocPtr,
931 -CookieSize.getQuantity());
932
933 llvm::Value *NumElementsPtr = AllocPtr;
934 if (!NumElementsOffset.isZero())
935 NumElementsPtr =
936 CGF.Builder.CreateConstInBoundsGEP1_64(NumElementsPtr,
937 NumElementsOffset.getQuantity());
938 NumElementsPtr =
939 CGF.Builder.CreateBitCast(NumElementsPtr, SizeLTy->getPointerTo(AS));
940 NumElements = CGF.Builder.CreateLoad(NumElementsPtr);
941}
942
John McCall284c48f2011-01-27 09:37:56 +0000943CharUnits ARMCXXABI::GetArrayCookieSize(const CXXNewExpr *expr) {
944 if (!NeedsArrayCookie(expr))
John McCall8ed55a52010-09-02 09:58:18 +0000945 return CharUnits::Zero();
946
947 // On ARM, the cookie is always:
948 // struct array_cookie {
949 // std::size_t element_size; // element_size != 0
950 // std::size_t element_count;
951 // };
952 // TODO: what should we do if the allocated type actually wants
953 // greater alignment?
954 return getContext().getTypeSizeInChars(getContext().getSizeType()) * 2;
955}
956
957llvm::Value *ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
958 llvm::Value *NewPtr,
959 llvm::Value *NumElements,
John McCall284c48f2011-01-27 09:37:56 +0000960 const CXXNewExpr *expr,
John McCall8ed55a52010-09-02 09:58:18 +0000961 QualType ElementType) {
John McCall284c48f2011-01-27 09:37:56 +0000962 assert(NeedsArrayCookie(expr));
John McCall8ed55a52010-09-02 09:58:18 +0000963
964 // NewPtr is a char*.
965
966 unsigned AS = cast<llvm::PointerType>(NewPtr->getType())->getAddressSpace();
967
John McCall9bca9232010-09-02 10:25:57 +0000968 ASTContext &Ctx = getContext();
John McCall8ed55a52010-09-02 09:58:18 +0000969 CharUnits SizeSize = Ctx.getTypeSizeInChars(Ctx.getSizeType());
970 const llvm::IntegerType *SizeTy =
971 cast<llvm::IntegerType>(CGF.ConvertType(Ctx.getSizeType()));
972
973 // The cookie is always at the start of the buffer.
974 llvm::Value *CookiePtr = NewPtr;
975
976 // The first element is the element size.
977 CookiePtr = CGF.Builder.CreateBitCast(CookiePtr, SizeTy->getPointerTo(AS));
978 llvm::Value *ElementSize = llvm::ConstantInt::get(SizeTy,
979 Ctx.getTypeSizeInChars(ElementType).getQuantity());
980 CGF.Builder.CreateStore(ElementSize, CookiePtr);
981
982 // The second element is the element count.
983 CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_32(CookiePtr, 1);
984 CGF.Builder.CreateStore(NumElements, CookiePtr);
985
986 // Finally, compute a pointer to the actual data buffer by skipping
987 // over the cookie completely.
988 CharUnits CookieSize = 2 * SizeSize;
989 return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr,
990 CookieSize.getQuantity());
991}
992
993void ARMCXXABI::ReadArrayCookie(CodeGenFunction &CGF,
994 llvm::Value *Ptr,
John McCall284c48f2011-01-27 09:37:56 +0000995 const CXXDeleteExpr *expr,
John McCall8ed55a52010-09-02 09:58:18 +0000996 QualType ElementType,
997 llvm::Value *&NumElements,
998 llvm::Value *&AllocPtr,
999 CharUnits &CookieSize) {
1000 // Derive a char* in the same address space as the pointer.
1001 unsigned AS = cast<llvm::PointerType>(Ptr->getType())->getAddressSpace();
1002 const llvm::Type *CharPtrTy = CGF.Builder.getInt8Ty()->getPointerTo(AS);
1003
1004 // If we don't need an array cookie, bail out early.
John McCall284c48f2011-01-27 09:37:56 +00001005 if (!NeedsArrayCookie(expr, ElementType)) {
John McCall8ed55a52010-09-02 09:58:18 +00001006 AllocPtr = CGF.Builder.CreateBitCast(Ptr, CharPtrTy);
1007 NumElements = 0;
1008 CookieSize = CharUnits::Zero();
1009 return;
1010 }
1011
John McCall9bca9232010-09-02 10:25:57 +00001012 QualType SizeTy = getContext().getSizeType();
1013 CharUnits SizeSize = getContext().getTypeSizeInChars(SizeTy);
John McCall8ed55a52010-09-02 09:58:18 +00001014 const llvm::Type *SizeLTy = CGF.ConvertType(SizeTy);
1015
1016 // The cookie size is always 2 * sizeof(size_t).
1017 CookieSize = 2 * SizeSize;
John McCall8ed55a52010-09-02 09:58:18 +00001018
1019 // The allocated pointer is the input ptr, minus that amount.
1020 AllocPtr = CGF.Builder.CreateBitCast(Ptr, CharPtrTy);
1021 AllocPtr = CGF.Builder.CreateConstInBoundsGEP1_64(AllocPtr,
1022 -CookieSize.getQuantity());
1023
1024 // The number of elements is at offset sizeof(size_t) relative to that.
1025 llvm::Value *NumElementsPtr
1026 = CGF.Builder.CreateConstInBoundsGEP1_64(AllocPtr,
1027 SizeSize.getQuantity());
1028 NumElementsPtr =
1029 CGF.Builder.CreateBitCast(NumElementsPtr, SizeLTy->getPointerTo(AS));
1030 NumElements = CGF.Builder.CreateLoad(NumElementsPtr);
1031}
1032
John McCall68ff0372010-09-08 01:44:27 +00001033/*********************** Static local initialization **************************/
1034
1035static llvm::Constant *getGuardAcquireFn(CodeGenModule &CGM,
1036 const llvm::PointerType *GuardPtrTy) {
1037 // int __cxa_guard_acquire(__guard *guard_object);
1038
1039 std::vector<const llvm::Type*> Args(1, GuardPtrTy);
1040 const llvm::FunctionType *FTy =
1041 llvm::FunctionType::get(CGM.getTypes().ConvertType(CGM.getContext().IntTy),
1042 Args, /*isVarArg=*/false);
1043
1044 return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire");
1045}
1046
1047static llvm::Constant *getGuardReleaseFn(CodeGenModule &CGM,
1048 const llvm::PointerType *GuardPtrTy) {
1049 // void __cxa_guard_release(__guard *guard_object);
1050
1051 std::vector<const llvm::Type*> Args(1, GuardPtrTy);
1052
1053 const llvm::FunctionType *FTy =
1054 llvm::FunctionType::get(llvm::Type::getVoidTy(CGM.getLLVMContext()),
1055 Args, /*isVarArg=*/false);
1056
1057 return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release");
1058}
1059
1060static llvm::Constant *getGuardAbortFn(CodeGenModule &CGM,
1061 const llvm::PointerType *GuardPtrTy) {
1062 // void __cxa_guard_abort(__guard *guard_object);
1063
1064 std::vector<const llvm::Type*> Args(1, GuardPtrTy);
1065
1066 const llvm::FunctionType *FTy =
1067 llvm::FunctionType::get(llvm::Type::getVoidTy(CGM.getLLVMContext()),
1068 Args, /*isVarArg=*/false);
1069
1070 return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort");
1071}
1072
1073namespace {
1074 struct CallGuardAbort : EHScopeStack::Cleanup {
1075 llvm::GlobalVariable *Guard;
1076 CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {}
1077
1078 void Emit(CodeGenFunction &CGF, bool IsForEH) {
1079 CGF.Builder.CreateCall(getGuardAbortFn(CGF.CGM, Guard->getType()), Guard)
1080 ->setDoesNotThrow();
1081 }
1082 };
1083}
1084
1085/// The ARM code here follows the Itanium code closely enough that we
1086/// just special-case it at particular places.
John McCallcdf7ef52010-11-06 09:44:32 +00001087void ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF,
1088 const VarDecl &D,
1089 llvm::GlobalVariable *GV) {
John McCall68ff0372010-09-08 01:44:27 +00001090 CGBuilderTy &Builder = CGF.Builder;
John McCallcdf7ef52010-11-06 09:44:32 +00001091
1092 // We only need to use thread-safe statics for local variables;
1093 // global initialization is always single-threaded.
1094 bool ThreadsafeStatics = (getContext().getLangOptions().ThreadsafeStatics &&
1095 D.isLocalVarDecl());
John McCall68ff0372010-09-08 01:44:27 +00001096
1097 // Guard variables are 64 bits in the generic ABI and 32 bits on ARM.
1098 const llvm::IntegerType *GuardTy
1099 = (IsARM ? Builder.getInt32Ty() : Builder.getInt64Ty());
1100 const llvm::PointerType *GuardPtrTy = GuardTy->getPointerTo();
1101
1102 // Create the guard variable.
1103 llvm::SmallString<256> GuardVName;
1104 getMangleContext().mangleItaniumGuardVariable(&D, GuardVName);
John McCall8e7cb6d2010-11-02 21:04:24 +00001105
John McCallcdf7ef52010-11-06 09:44:32 +00001106 // Just absorb linkage and visibility from the variable.
John McCall68ff0372010-09-08 01:44:27 +00001107 llvm::GlobalVariable *GuardVariable =
1108 new llvm::GlobalVariable(CGM.getModule(), GuardTy,
John McCallcdf7ef52010-11-06 09:44:32 +00001109 false, GV->getLinkage(),
John McCall68ff0372010-09-08 01:44:27 +00001110 llvm::ConstantInt::get(GuardTy, 0),
1111 GuardVName.str());
John McCall8e7cb6d2010-11-02 21:04:24 +00001112 GuardVariable->setVisibility(GV->getVisibility());
John McCall68ff0372010-09-08 01:44:27 +00001113
1114 // Test whether the variable has completed initialization.
1115 llvm::Value *IsInitialized;
1116
1117 // ARM C++ ABI 3.2.3.1:
1118 // To support the potential use of initialization guard variables
1119 // as semaphores that are the target of ARM SWP and LDREX/STREX
1120 // synchronizing instructions we define a static initialization
1121 // guard variable to be a 4-byte aligned, 4- byte word with the
1122 // following inline access protocol.
1123 // #define INITIALIZED 1
1124 // if ((obj_guard & INITIALIZED) != INITIALIZED) {
1125 // if (__cxa_guard_acquire(&obj_guard))
1126 // ...
1127 // }
1128 if (IsARM) {
1129 llvm::Value *V = Builder.CreateLoad(GuardVariable);
1130 V = Builder.CreateAnd(V, Builder.getInt32(1));
1131 IsInitialized = Builder.CreateIsNull(V, "guard.uninitialized");
1132
1133 // Itanium C++ ABI 3.3.2:
1134 // The following is pseudo-code showing how these functions can be used:
1135 // if (obj_guard.first_byte == 0) {
1136 // if ( __cxa_guard_acquire (&obj_guard) ) {
1137 // try {
1138 // ... initialize the object ...;
1139 // } catch (...) {
1140 // __cxa_guard_abort (&obj_guard);
1141 // throw;
1142 // }
1143 // ... queue object destructor with __cxa_atexit() ...;
1144 // __cxa_guard_release (&obj_guard);
1145 // }
1146 // }
1147 } else {
1148 // Load the first byte of the guard variable.
1149 const llvm::Type *PtrTy = Builder.getInt8PtrTy();
1150 llvm::Value *V =
1151 Builder.CreateLoad(Builder.CreateBitCast(GuardVariable, PtrTy), "tmp");
1152
1153 IsInitialized = Builder.CreateIsNull(V, "guard.uninitialized");
1154 }
1155
1156 llvm::BasicBlock *InitCheckBlock = CGF.createBasicBlock("init.check");
1157 llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end");
1158
1159 // Check if the first byte of the guard variable is zero.
1160 Builder.CreateCondBr(IsInitialized, InitCheckBlock, EndBlock);
1161
1162 CGF.EmitBlock(InitCheckBlock);
1163
1164 // Variables used when coping with thread-safe statics and exceptions.
1165 if (ThreadsafeStatics) {
1166 // Call __cxa_guard_acquire.
1167 llvm::Value *V
1168 = Builder.CreateCall(getGuardAcquireFn(CGM, GuardPtrTy), GuardVariable);
1169
1170 llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init");
1171
1172 Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"),
1173 InitBlock, EndBlock);
1174
1175 // Call __cxa_guard_abort along the exceptional edge.
1176 CGF.EHStack.pushCleanup<CallGuardAbort>(EHCleanup, GuardVariable);
1177
1178 CGF.EmitBlock(InitBlock);
1179 }
1180
1181 // Emit the initializer and add a global destructor if appropriate.
1182 CGF.EmitCXXGlobalVarDeclInit(D, GV);
1183
1184 if (ThreadsafeStatics) {
1185 // Pop the guard-abort cleanup if we pushed one.
1186 CGF.PopCleanupBlock();
1187
1188 // Call __cxa_guard_release. This cannot throw.
1189 Builder.CreateCall(getGuardReleaseFn(CGM, GuardPtrTy), GuardVariable);
1190 } else {
1191 Builder.CreateStore(llvm::ConstantInt::get(GuardTy, 1), GuardVariable);
1192 }
1193
1194 CGF.EmitBlock(EndBlock);
1195}