blob: 5e1bb255c91264afc6e2874e730577bb64b03630 [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 McCall0bab0cd2010-08-23 01:21:21 +000069 llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
70 const CastExpr *E,
71 llvm::Value *Src);
John McCallcf2c85e2010-08-22 04:16:24 +000072
John McCall0bab0cd2010-08-23 01:21:21 +000073 llvm::Constant *EmitMemberPointerConversion(llvm::Constant *C,
74 const CastExpr *E);
John McCallcf2c85e2010-08-22 04:16:24 +000075
John McCall0bab0cd2010-08-23 01:21:21 +000076 llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
John McCallcf2c85e2010-08-22 04:16:24 +000077
John McCall0bab0cd2010-08-23 01:21:21 +000078 llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
79 llvm::Constant *EmitMemberPointer(const FieldDecl *FD);
John McCall875ab102010-08-22 06:43:33 +000080
John McCall0bab0cd2010-08-23 01:21:21 +000081 llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF,
82 llvm::Value *L,
83 llvm::Value *R,
84 const MemberPointerType *MPT,
85 bool Inequality);
John McCalle9fd7eb2010-08-22 08:30:07 +000086
John McCall0bab0cd2010-08-23 01:21:21 +000087 llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
88 llvm::Value *Addr,
89 const MemberPointerType *MPT);
Charles Davis3a811f12010-05-25 19:52:27 +000090};
John McCallee79a4c2010-08-21 22:46:04 +000091
92class ARMCXXABI : public ItaniumCXXABI {
93public:
John McCallbabc9a92010-08-22 00:59:17 +000094 ARMCXXABI(CodeGen::CodeGenModule &CGM) : ItaniumCXXABI(CGM, /*ARM*/ true) {}
John McCallee79a4c2010-08-21 22:46:04 +000095};
Charles Davis3a811f12010-05-25 19:52:27 +000096}
97
Charles Davis071cc7d2010-08-16 03:33:14 +000098CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) {
Charles Davis3a811f12010-05-25 19:52:27 +000099 return new ItaniumCXXABI(CGM);
100}
101
John McCallee79a4c2010-08-21 22:46:04 +0000102CodeGen::CGCXXABI *CodeGen::CreateARMCXXABI(CodeGenModule &CGM) {
103 return new ARMCXXABI(CGM);
104}
105
John McCall0bab0cd2010-08-23 01:21:21 +0000106const llvm::Type *
107ItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
108 if (MPT->isMemberDataPointer())
109 return getPtrDiffTy();
110 else
111 return llvm::StructType::get(CGM.getLLVMContext(),
112 getPtrDiffTy(), getPtrDiffTy(), NULL);
John McCall875ab102010-08-22 06:43:33 +0000113}
114
John McCallbabc9a92010-08-22 00:59:17 +0000115/// In the Itanium and ARM ABIs, method pointers have the form:
116/// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr;
117///
118/// In the Itanium ABI:
119/// - method pointers are virtual if (memptr.ptr & 1) is nonzero
120/// - the this-adjustment is (memptr.adj)
121/// - the virtual offset is (memptr.ptr - 1)
122///
123/// In the ARM ABI:
124/// - method pointers are virtual if (memptr.adj & 1) is nonzero
125/// - the this-adjustment is (memptr.adj >> 1)
126/// - the virtual offset is (memptr.ptr)
127/// ARM uses 'adj' for the virtual flag because Thumb functions
128/// may be only single-byte aligned.
129///
130/// If the member is virtual, the adjusted 'this' pointer points
131/// to a vtable pointer from which the virtual offset is applied.
132///
133/// If the member is non-virtual, memptr.ptr is the address of
134/// the function to call.
John McCall93d557b2010-08-22 00:05:51 +0000135llvm::Value *
136ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
137 llvm::Value *&This,
138 llvm::Value *MemFnPtr,
139 const MemberPointerType *MPT) {
140 CGBuilderTy &Builder = CGF.Builder;
141
142 const FunctionProtoType *FPT =
143 MPT->getPointeeType()->getAs<FunctionProtoType>();
144 const CXXRecordDecl *RD =
145 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
146
147 const llvm::FunctionType *FTy =
148 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
149 FPT->isVariadic());
150
John McCall0bab0cd2010-08-23 01:21:21 +0000151 const llvm::IntegerType *ptrdiff = getPtrDiffTy();
John McCallbabc9a92010-08-22 00:59:17 +0000152 llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(ptrdiff, 1);
John McCall93d557b2010-08-22 00:05:51 +0000153
John McCallbabc9a92010-08-22 00:59:17 +0000154 llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual");
155 llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual");
156 llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end");
157
John McCalld608cdb2010-08-22 10:59:02 +0000158 // Extract memptr.adj, which is in the second field.
159 llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj");
John McCallbabc9a92010-08-22 00:59:17 +0000160
161 // Compute the true adjustment.
162 llvm::Value *Adj = RawAdj;
163 if (IsARM)
164 Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted");
John McCall93d557b2010-08-22 00:05:51 +0000165
166 // Apply the adjustment and cast back to the original struct type
167 // for consistency.
John McCallbabc9a92010-08-22 00:59:17 +0000168 llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy());
169 Ptr = Builder.CreateInBoundsGEP(Ptr, Adj);
170 This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted");
John McCall93d557b2010-08-22 00:05:51 +0000171
172 // Load the function pointer.
John McCalld608cdb2010-08-22 10:59:02 +0000173 llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr");
John McCall93d557b2010-08-22 00:05:51 +0000174
175 // If the LSB in the function pointer is 1, the function pointer points to
176 // a virtual function.
John McCallbabc9a92010-08-22 00:59:17 +0000177 llvm::Value *IsVirtual;
178 if (IsARM)
179 IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1);
180 else
181 IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1);
182 IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual");
John McCall93d557b2010-08-22 00:05:51 +0000183 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
184
185 // In the virtual path, the adjustment left 'This' pointing to the
186 // vtable of the correct base subobject. The "function pointer" is an
John McCallbabc9a92010-08-22 00:59:17 +0000187 // offset within the vtable (+1 for the virtual flag on non-ARM).
John McCall93d557b2010-08-22 00:05:51 +0000188 CGF.EmitBlock(FnVirtual);
189
190 // Cast the adjusted this to a pointer to vtable pointer and load.
191 const llvm::Type *VTableTy = Builder.getInt8PtrTy();
192 llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy->getPointerTo());
John McCallbabc9a92010-08-22 00:59:17 +0000193 VTable = Builder.CreateLoad(VTable, "memptr.vtable");
John McCall93d557b2010-08-22 00:05:51 +0000194
195 // Apply the offset.
John McCallbabc9a92010-08-22 00:59:17 +0000196 llvm::Value *VTableOffset = FnAsInt;
197 if (!IsARM) VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1);
198 VTable = Builder.CreateGEP(VTable, VTableOffset);
John McCall93d557b2010-08-22 00:05:51 +0000199
200 // Load the virtual function to call.
201 VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo());
John McCallbabc9a92010-08-22 00:59:17 +0000202 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn");
John McCall93d557b2010-08-22 00:05:51 +0000203 CGF.EmitBranch(FnEnd);
204
205 // In the non-virtual path, the function pointer is actually a
206 // function pointer.
207 CGF.EmitBlock(FnNonVirtual);
208 llvm::Value *NonVirtualFn =
John McCallbabc9a92010-08-22 00:59:17 +0000209 Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn");
John McCall93d557b2010-08-22 00:05:51 +0000210
211 // We're done.
212 CGF.EmitBlock(FnEnd);
213 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
214 Callee->reserveOperandSpace(2);
215 Callee->addIncoming(VirtualFn, FnVirtual);
216 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
217 return Callee;
218}
John McCall3023def2010-08-22 03:04:22 +0000219
220/// Perform a derived-to-base or base-to-derived member pointer conversion.
John McCall0bab0cd2010-08-23 01:21:21 +0000221///
222/// Obligatory offset/adjustment diagram:
223/// <-- offset --> <-- adjustment -->
224/// |--------------------------|----------------------|--------------------|
225/// ^Derived address point ^Base address point ^Member address point
226///
227/// So when converting a base member pointer to a derived member pointer,
228/// we add the offset to the adjustment because the address point has
229/// decreased; and conversely, when converting a derived MP to a base MP
230/// we subtract the offset from the adjustment because the address point
231/// has increased.
232///
233/// The standard forbids (at compile time) conversion to and from
234/// virtual bases, which is why we don't have to consider them here.
235///
236/// The standard forbids (at run time) casting a derived MP to a base
237/// MP when the derived MP does not point to a member of the base.
238/// This is why -1 is a reasonable choice for null data member
239/// pointers.
John McCalld608cdb2010-08-22 10:59:02 +0000240llvm::Value *
John McCall0bab0cd2010-08-23 01:21:21 +0000241ItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
242 const CastExpr *E,
243 llvm::Value *Src) {
John McCall3023def2010-08-22 03:04:22 +0000244 assert(E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer ||
245 E->getCastKind() == CastExpr::CK_BaseToDerivedMemberPointer);
246
John McCalld608cdb2010-08-22 10:59:02 +0000247 if (isa<llvm::Constant>(Src))
John McCall0bab0cd2010-08-23 01:21:21 +0000248 return EmitMemberPointerConversion(cast<llvm::Constant>(Src), E);
John McCalld608cdb2010-08-22 10:59:02 +0000249
John McCall3023def2010-08-22 03:04:22 +0000250 CGBuilderTy &Builder = CGF.Builder;
251
252 const MemberPointerType *SrcTy =
253 E->getSubExpr()->getType()->getAs<MemberPointerType>();
254 const MemberPointerType *DestTy = E->getType()->getAs<MemberPointerType>();
255
256 const CXXRecordDecl *SrcDecl = SrcTy->getClass()->getAsCXXRecordDecl();
257 const CXXRecordDecl *DestDecl = DestTy->getClass()->getAsCXXRecordDecl();
258
John McCall3023def2010-08-22 03:04:22 +0000259 bool DerivedToBase =
260 E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer;
261
262 const CXXRecordDecl *BaseDecl, *DerivedDecl;
263 if (DerivedToBase)
264 DerivedDecl = SrcDecl, BaseDecl = DestDecl;
265 else
266 BaseDecl = SrcDecl, DerivedDecl = DestDecl;
267
John McCalld608cdb2010-08-22 10:59:02 +0000268 llvm::Constant *Adj =
269 CGF.CGM.GetNonVirtualBaseClassOffset(DerivedDecl,
270 E->path_begin(),
271 E->path_end());
272 if (!Adj) return Src;
John McCall875ab102010-08-22 06:43:33 +0000273
John McCall0bab0cd2010-08-23 01:21:21 +0000274 // For member data pointers, this is just a matter of adding the
275 // offset if the source is non-null.
276 if (SrcTy->isMemberDataPointer()) {
277 llvm::Value *Dst;
278 if (DerivedToBase)
279 Dst = Builder.CreateNSWSub(Src, Adj, "adj");
280 else
281 Dst = Builder.CreateNSWAdd(Src, Adj, "adj");
282
283 // Null check.
284 llvm::Value *Null = llvm::Constant::getAllOnesValue(Src->getType());
285 llvm::Value *IsNull = Builder.CreateICmpEQ(Src, Null, "memptr.isnull");
286 return Builder.CreateSelect(IsNull, Src, Dst);
287 }
288
John McCalld608cdb2010-08-22 10:59:02 +0000289 // The this-adjustment is left-shifted by 1 on ARM.
290 if (IsARM) {
291 uint64_t Offset = cast<llvm::ConstantInt>(Adj)->getZExtValue();
292 Offset <<= 1;
293 Adj = llvm::ConstantInt::get(Adj->getType(), Offset);
294 }
295
John McCalle14add42010-08-22 11:04:31 +0000296 llvm::Value *SrcAdj = Builder.CreateExtractValue(Src, 1, "src.adj");
John McCalld608cdb2010-08-22 10:59:02 +0000297 llvm::Value *DstAdj;
298 if (DerivedToBase)
John McCall0bab0cd2010-08-23 01:21:21 +0000299 DstAdj = Builder.CreateNSWSub(SrcAdj, Adj, "adj");
John McCalld608cdb2010-08-22 10:59:02 +0000300 else
John McCall0bab0cd2010-08-23 01:21:21 +0000301 DstAdj = Builder.CreateNSWAdd(SrcAdj, Adj, "adj");
John McCalld608cdb2010-08-22 10:59:02 +0000302
John McCalle14add42010-08-22 11:04:31 +0000303 return Builder.CreateInsertValue(Src, DstAdj, 1);
John McCall3023def2010-08-22 03:04:22 +0000304}
John McCallcf2c85e2010-08-22 04:16:24 +0000305
306llvm::Constant *
John McCall0bab0cd2010-08-23 01:21:21 +0000307ItaniumCXXABI::EmitMemberPointerConversion(llvm::Constant *C,
308 const CastExpr *E) {
John McCallcf2c85e2010-08-22 04:16:24 +0000309 const MemberPointerType *SrcTy =
310 E->getSubExpr()->getType()->getAs<MemberPointerType>();
311 const MemberPointerType *DestTy =
312 E->getType()->getAs<MemberPointerType>();
313
314 bool DerivedToBase =
315 E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer;
316
317 const CXXRecordDecl *DerivedDecl;
318 if (DerivedToBase)
319 DerivedDecl = SrcTy->getClass()->getAsCXXRecordDecl();
320 else
321 DerivedDecl = DestTy->getClass()->getAsCXXRecordDecl();
322
323 // Calculate the offset to the base class.
324 llvm::Constant *Offset =
325 CGM.GetNonVirtualBaseClassOffset(DerivedDecl,
326 E->path_begin(),
327 E->path_end());
328 // If there's no offset, we're done.
329 if (!Offset) return C;
330
John McCall0bab0cd2010-08-23 01:21:21 +0000331 // If the source is a member data pointer, we have to do a null
332 // check and then add the offset. In the common case, we can fold
333 // away the offset.
334 if (SrcTy->isMemberDataPointer()) {
335 assert(C->getType() == getPtrDiffTy());
336
337 // If it's a constant int, just create a new constant int.
338 if (llvm::ConstantInt *CI = dyn_cast<llvm::ConstantInt>(C)) {
339 int64_t Src = CI->getSExtValue();
340
341 // Null converts to null.
342 if (Src == -1) return CI;
343
344 // Otherwise, just add the offset.
345 int64_t OffsetV = cast<llvm::ConstantInt>(Offset)->getSExtValue();
346 int64_t Dst = (DerivedToBase ? Src - OffsetV : Src + OffsetV);
347 return llvm::ConstantInt::get(CI->getType(), Dst, /*signed*/ true);
348 }
349
350 // Otherwise, we have to form a constant select expression.
351 llvm::Constant *Null = llvm::Constant::getAllOnesValue(C->getType());
352
353 llvm::Constant *IsNull =
354 llvm::ConstantExpr::getICmp(llvm::ICmpInst::ICMP_EQ, C, Null);
355
356 llvm::Constant *Dst;
357 if (DerivedToBase)
358 Dst = llvm::ConstantExpr::getNSWSub(C, Offset);
359 else
360 Dst = llvm::ConstantExpr::getNSWAdd(C, Offset);
361
362 return llvm::ConstantExpr::getSelect(IsNull, Null, Dst);
363 }
364
John McCall875ab102010-08-22 06:43:33 +0000365 // The this-adjustment is left-shifted by 1 on ARM.
366 if (IsARM) {
John McCall0bab0cd2010-08-23 01:21:21 +0000367 int64_t OffsetV = cast<llvm::ConstantInt>(Offset)->getSExtValue();
John McCall875ab102010-08-22 06:43:33 +0000368 OffsetV <<= 1;
369 Offset = llvm::ConstantInt::get(Offset->getType(), OffsetV);
370 }
371
John McCallcf2c85e2010-08-22 04:16:24 +0000372 llvm::ConstantStruct *CS = cast<llvm::ConstantStruct>(C);
373
John McCall0bab0cd2010-08-23 01:21:21 +0000374 llvm::Constant *Values[2] = { CS->getOperand(0), 0 };
375 if (DerivedToBase)
376 Values[1] = llvm::ConstantExpr::getSub(CS->getOperand(1), Offset);
377 else
378 Values[1] = llvm::ConstantExpr::getAdd(CS->getOperand(1), Offset);
379
John McCallcf2c85e2010-08-22 04:16:24 +0000380 return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2,
381 /*Packed=*/false);
382}
383
384
John McCallcf2c85e2010-08-22 04:16:24 +0000385llvm::Constant *
John McCall0bab0cd2010-08-23 01:21:21 +0000386ItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
387 const llvm::Type *ptrdiff_t = getPtrDiffTy();
388
389 // Itanium C++ ABI 2.3:
390 // A NULL pointer is represented as -1.
391 if (MPT->isMemberDataPointer())
392 return llvm::ConstantInt::get(ptrdiff_t, -1ULL, /*isSigned=*/true);
John McCalld608cdb2010-08-22 10:59:02 +0000393
394 llvm::Constant *Zero = llvm::ConstantInt::get(ptrdiff_t, 0);
395 llvm::Constant *Values[2] = { Zero, Zero };
396 return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2,
397 /*Packed=*/false);
John McCallcf2c85e2010-08-22 04:16:24 +0000398}
399
John McCall0bab0cd2010-08-23 01:21:21 +0000400llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const FieldDecl *FD) {
401 // Itanium C++ ABI 2.3:
402 // A pointer to data member is an offset from the base address of
403 // the class object containing it, represented as a ptrdiff_t
404
405 QualType ClassType = CGM.getContext().getTypeDeclType(FD->getParent());
406 const llvm::StructType *ClassLTy =
407 cast<llvm::StructType>(CGM.getTypes().ConvertType(ClassType));
408
409 const CGRecordLayout &RL = CGM.getTypes().getCGRecordLayout(FD->getParent());
410 unsigned FieldNo = RL.getLLVMFieldNo(FD);
411 uint64_t Offset =
412 CGM.getTargetData().getStructLayout(ClassLTy)->getElementOffset(FieldNo);
413
414 return llvm::ConstantInt::get(getPtrDiffTy(), Offset);
415}
416
417llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) {
John McCalld608cdb2010-08-22 10:59:02 +0000418 assert(MD->isInstance() && "Member function must not be static!");
419 MD = MD->getCanonicalDecl();
420
421 CodeGenTypes &Types = CGM.getTypes();
John McCall0bab0cd2010-08-23 01:21:21 +0000422 const llvm::Type *ptrdiff_t = getPtrDiffTy();
John McCalld608cdb2010-08-22 10:59:02 +0000423
424 // Get the function pointer (or index if this is a virtual function).
425 llvm::Constant *MemPtr[2];
426 if (MD->isVirtual()) {
427 uint64_t Index = CGM.getVTables().getMethodVTableIndex(MD);
428
429 // FIXME: We shouldn't use / 8 here.
430 uint64_t PointerWidthInBytes =
431 CGM.getContext().Target.getPointerWidth(0) / 8;
432 uint64_t VTableOffset = (Index * PointerWidthInBytes);
433
434 if (IsARM) {
435 // ARM C++ ABI 3.2.1:
436 // This ABI specifies that adj contains twice the this
437 // adjustment, plus 1 if the member function is virtual. The
438 // least significant bit of adj then makes exactly the same
439 // discrimination as the least significant bit of ptr does for
440 // Itanium.
441 MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset);
442 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 1);
443 } else {
444 // Itanium C++ ABI 2.3:
445 // For a virtual function, [the pointer field] is 1 plus the
446 // virtual table offset (in bytes) of the function,
447 // represented as a ptrdiff_t.
448 MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset + 1);
449 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0);
450 }
451 } else {
452 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
453 const llvm::Type *Ty;
454 // Check whether the function has a computable LLVM signature.
455 if (!CodeGenTypes::VerifyFuncTypeComplete(FPT)) {
456 // The function has a computable LLVM signature; use the correct type.
457 Ty = Types.GetFunctionType(Types.getFunctionInfo(MD), FPT->isVariadic());
458 } else {
459 // Use an arbitrary non-function type to tell GetAddrOfFunction that the
460 // function type is incomplete.
461 Ty = ptrdiff_t;
462 }
463
464 llvm::Constant *Addr = CGM.GetAddrOfFunction(MD, Ty);
465 MemPtr[0] = llvm::ConstantExpr::getPtrToInt(Addr, ptrdiff_t);
466 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0);
467 }
John McCall875ab102010-08-22 06:43:33 +0000468
469 return llvm::ConstantStruct::get(CGM.getLLVMContext(),
John McCalld608cdb2010-08-22 10:59:02 +0000470 MemPtr, 2, /*Packed=*/false);
John McCall875ab102010-08-22 06:43:33 +0000471}
472
John McCalle9fd7eb2010-08-22 08:30:07 +0000473/// The comparison algorithm is pretty easy: the member pointers are
474/// the same if they're either bitwise identical *or* both null.
475///
476/// ARM is different here only because null-ness is more complicated.
477llvm::Value *
John McCall0bab0cd2010-08-23 01:21:21 +0000478ItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
479 llvm::Value *L,
480 llvm::Value *R,
481 const MemberPointerType *MPT,
482 bool Inequality) {
John McCalle9fd7eb2010-08-22 08:30:07 +0000483 CGBuilderTy &Builder = CGF.Builder;
484
John McCalle9fd7eb2010-08-22 08:30:07 +0000485 llvm::ICmpInst::Predicate Eq;
486 llvm::Instruction::BinaryOps And, Or;
487 if (Inequality) {
488 Eq = llvm::ICmpInst::ICMP_NE;
489 And = llvm::Instruction::Or;
490 Or = llvm::Instruction::And;
491 } else {
492 Eq = llvm::ICmpInst::ICMP_EQ;
493 And = llvm::Instruction::And;
494 Or = llvm::Instruction::Or;
495 }
496
John McCall0bab0cd2010-08-23 01:21:21 +0000497 // Member data pointers are easy because there's a unique null
498 // value, so it just comes down to bitwise equality.
499 if (MPT->isMemberDataPointer())
500 return Builder.CreateICmp(Eq, L, R);
501
502 // For member function pointers, the tautologies are more complex.
503 // The Itanium tautology is:
John McCallde719f72010-08-23 06:56:36 +0000504 // (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj))
John McCall0bab0cd2010-08-23 01:21:21 +0000505 // The ARM tautology is:
John McCallde719f72010-08-23 06:56:36 +0000506 // (L == R) <==> (L.ptr == R.ptr &&
507 // (L.adj == R.adj ||
508 // (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0)))
John McCall0bab0cd2010-08-23 01:21:21 +0000509 // The inequality tautologies have exactly the same structure, except
510 // applying De Morgan's laws.
511
512 llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr");
513 llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr");
514
John McCalle9fd7eb2010-08-22 08:30:07 +0000515 // This condition tests whether L.ptr == R.ptr. This must always be
516 // true for equality to hold.
517 llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr");
518
519 // This condition, together with the assumption that L.ptr == R.ptr,
520 // tests whether the pointers are both null. ARM imposes an extra
521 // condition.
522 llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType());
523 llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null");
524
525 // This condition tests whether L.adj == R.adj. If this isn't
526 // true, the pointers are unequal unless they're both null.
John McCalld608cdb2010-08-22 10:59:02 +0000527 llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj");
528 llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj");
John McCalle9fd7eb2010-08-22 08:30:07 +0000529 llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj");
530
531 // Null member function pointers on ARM clear the low bit of Adj,
532 // so the zero condition has to check that neither low bit is set.
533 if (IsARM) {
534 llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1);
535
536 // Compute (l.adj | r.adj) & 1 and test it against zero.
537 llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj");
538 llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One);
539 llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero,
540 "cmp.or.adj");
541 EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero);
542 }
543
544 // Tie together all our conditions.
545 llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq);
546 Result = Builder.CreateBinOp(And, PtrEq, Result,
547 Inequality ? "memptr.ne" : "memptr.eq");
548 return Result;
549}
550
551llvm::Value *
John McCall0bab0cd2010-08-23 01:21:21 +0000552ItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
553 llvm::Value *MemPtr,
554 const MemberPointerType *MPT) {
John McCalle9fd7eb2010-08-22 08:30:07 +0000555 CGBuilderTy &Builder = CGF.Builder;
John McCall0bab0cd2010-08-23 01:21:21 +0000556
557 /// For member data pointers, this is just a check against -1.
558 if (MPT->isMemberDataPointer()) {
559 assert(MemPtr->getType() == getPtrDiffTy());
560 llvm::Value *NegativeOne =
561 llvm::Constant::getAllOnesValue(MemPtr->getType());
562 return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool");
563 }
John McCalle9fd7eb2010-08-22 08:30:07 +0000564
565 // In Itanium, a member function pointer is null if 'ptr' is null.
John McCalld608cdb2010-08-22 10:59:02 +0000566 llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr");
John McCalle9fd7eb2010-08-22 08:30:07 +0000567
568 llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0);
569 llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool");
570
571 // In ARM, it's that, plus the low bit of 'adj' must be zero.
572 if (IsARM) {
573 llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1);
John McCalld608cdb2010-08-22 10:59:02 +0000574 llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj");
John McCalle9fd7eb2010-08-22 08:30:07 +0000575 llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit");
576 llvm::Value *IsNotVirtual = Builder.CreateICmpEQ(VirtualBit, Zero,
577 "memptr.notvirtual");
578 Result = Builder.CreateAnd(Result, IsNotVirtual);
579 }
580
581 return Result;
582}
John McCall875ab102010-08-22 06:43:33 +0000583
John McCallf16aa102010-08-22 21:01:12 +0000584/// The Itanium ABI requires non-zero initialization only for data
585/// member pointers, for which '0' is a valid offset.
586bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
587 return MPT->getPointeeType()->isFunctionType();
John McCallcf2c85e2010-08-22 04:16:24 +0000588}