blob: ac394956c8d3978282f598afd5861261c8317f87 [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 McCall93d557b2010-08-22 00:05:51 +000022#include "CodeGenFunction.h"
Charles Davis3a811f12010-05-25 19:52:27 +000023#include "CodeGenModule.h"
24#include "Mangle.h"
John McCall93d557b2010-08-22 00:05:51 +000025#include <clang/AST/Type.h>
26#include <llvm/Value.h>
Charles Davis3a811f12010-05-25 19:52:27 +000027
28using namespace clang;
John McCall93d557b2010-08-22 00:05:51 +000029using namespace CodeGen;
Charles Davis3a811f12010-05-25 19:52:27 +000030
31namespace {
Charles Davis071cc7d2010-08-16 03:33:14 +000032class ItaniumCXXABI : public CodeGen::CGCXXABI {
John McCall93d557b2010-08-22 00:05:51 +000033protected:
34 CodeGenModule &CGM;
Charles Davis3a811f12010-05-25 19:52:27 +000035 CodeGen::MangleContext MangleCtx;
John McCallbabc9a92010-08-22 00:59:17 +000036 bool IsARM;
Charles Davis3a811f12010-05-25 19:52:27 +000037public:
John McCallbabc9a92010-08-22 00:59:17 +000038 ItaniumCXXABI(CodeGen::CodeGenModule &CGM, bool IsARM = false) :
39 CGM(CGM), MangleCtx(CGM.getContext(), CGM.getDiags()), IsARM(IsARM) { }
Charles Davis3a811f12010-05-25 19:52:27 +000040
41 CodeGen::MangleContext &getMangleContext() {
42 return MangleCtx;
43 }
John McCall93d557b2010-08-22 00:05:51 +000044
John McCallcf2c85e2010-08-22 04:16:24 +000045 bool RequiresNonZeroInitializer(QualType T);
46 bool RequiresNonZeroInitializer(const CXXRecordDecl *D);
47
John McCall93d557b2010-08-22 00:05:51 +000048 llvm::Value *EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
49 llvm::Value *&This,
50 llvm::Value *MemFnPtr,
51 const MemberPointerType *MPT);
John McCall3023def2010-08-22 03:04:22 +000052
John McCallcf2c85e2010-08-22 04:16:24 +000053 void EmitMemberFunctionPointerConversion(CodeGenFunction &CGF,
54 const CastExpr *E,
55 llvm::Value *Src,
56 llvm::Value *Dest,
57 bool VolatileDest);
58
59 llvm::Constant *EmitMemberFunctionPointerConversion(llvm::Constant *C,
60 const CastExpr *E);
61
62 void EmitNullMemberFunctionPointer(CodeGenFunction &CGF,
63 const MemberPointerType *MPT,
64 llvm::Value *Dest,
65 bool VolatileDest);
66
67 llvm::Constant *EmitNullMemberFunctionPointer(const MemberPointerType *MPT);
68
John McCall875ab102010-08-22 06:43:33 +000069 void EmitMemberFunctionPointer(CodeGenFunction &CGF,
70 const CXXMethodDecl *MD,
71 llvm::Value *Dest,
72 bool VolatileDest);
73
74 llvm::Constant *EmitMemberFunctionPointer(const CXXMethodDecl *MD);
75
76private:
77 void GetMemberFunctionPointer(const CXXMethodDecl *MD,
78 llvm::Constant *(&Array)[2]);
Charles Davis3a811f12010-05-25 19:52:27 +000079};
John McCallee79a4c2010-08-21 22:46:04 +000080
81class ARMCXXABI : public ItaniumCXXABI {
82public:
John McCallbabc9a92010-08-22 00:59:17 +000083 ARMCXXABI(CodeGen::CodeGenModule &CGM) : ItaniumCXXABI(CGM, /*ARM*/ true) {}
John McCallee79a4c2010-08-21 22:46:04 +000084};
Charles Davis3a811f12010-05-25 19:52:27 +000085}
86
Charles Davis071cc7d2010-08-16 03:33:14 +000087CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) {
Charles Davis3a811f12010-05-25 19:52:27 +000088 return new ItaniumCXXABI(CGM);
89}
90
John McCallee79a4c2010-08-21 22:46:04 +000091CodeGen::CGCXXABI *CodeGen::CreateARMCXXABI(CodeGenModule &CGM) {
92 return new ARMCXXABI(CGM);
93}
94
John McCall875ab102010-08-22 06:43:33 +000095void ItaniumCXXABI::GetMemberFunctionPointer(const CXXMethodDecl *MD,
96 llvm::Constant *(&MemPtr)[2]) {
97 assert(MD->isInstance() && "Member function must not be static!");
98
99 MD = MD->getCanonicalDecl();
100
101 CodeGenTypes &Types = CGM.getTypes();
102 const llvm::Type *ptrdiff_t =
103 Types.ConvertType(CGM.getContext().getPointerDiffType());
104
105 // Get the function pointer (or index if this is a virtual function).
106 if (MD->isVirtual()) {
107 uint64_t Index = CGM.getVTables().getMethodVTableIndex(MD);
108
109 // FIXME: We shouldn't use / 8 here.
110 uint64_t PointerWidthInBytes =
111 CGM.getContext().Target.getPointerWidth(0) / 8;
112 uint64_t VTableOffset = (Index * PointerWidthInBytes);
113
114 if (IsARM) {
115 // ARM C++ ABI 3.2.1:
116 // This ABI specifies that adj contains twice the this
117 // adjustment, plus 1 if the member function is virtual. The
118 // least significant bit of adj then makes exactly the same
119 // discrimination as the least significant bit of ptr does for
120 // Itanium.
121 MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset);
122 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 1);
123 } else {
124 // Itanium C++ ABI 2.3:
125 // For a virtual function, [the pointer field] is 1 plus the
126 // virtual table offset (in bytes) of the function,
127 // represented as a ptrdiff_t.
128 MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset + 1);
129 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0);
130 }
131 } else {
132 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
133 const llvm::Type *Ty;
134 // Check whether the function has a computable LLVM signature.
135 if (!CodeGenTypes::VerifyFuncTypeComplete(FPT)) {
136 // The function has a computable LLVM signature; use the correct type.
137 Ty = Types.GetFunctionType(Types.getFunctionInfo(MD), FPT->isVariadic());
138 } else {
139 // Use an arbitrary non-function type to tell GetAddrOfFunction that the
140 // function type is incomplete.
141 Ty = ptrdiff_t;
142 }
143
144 llvm::Constant *Addr = CGM.GetAddrOfFunction(MD, Ty);
145 MemPtr[0] = llvm::ConstantExpr::getPtrToInt(Addr, ptrdiff_t);
146 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0);
147 }
148}
149
150
John McCallbabc9a92010-08-22 00:59:17 +0000151/// In the Itanium and ARM ABIs, method pointers have the form:
152/// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr;
153///
154/// In the Itanium ABI:
155/// - method pointers are virtual if (memptr.ptr & 1) is nonzero
156/// - the this-adjustment is (memptr.adj)
157/// - the virtual offset is (memptr.ptr - 1)
158///
159/// In the ARM ABI:
160/// - method pointers are virtual if (memptr.adj & 1) is nonzero
161/// - the this-adjustment is (memptr.adj >> 1)
162/// - the virtual offset is (memptr.ptr)
163/// ARM uses 'adj' for the virtual flag because Thumb functions
164/// may be only single-byte aligned.
165///
166/// If the member is virtual, the adjusted 'this' pointer points
167/// to a vtable pointer from which the virtual offset is applied.
168///
169/// If the member is non-virtual, memptr.ptr is the address of
170/// the function to call.
John McCall93d557b2010-08-22 00:05:51 +0000171llvm::Value *
172ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
173 llvm::Value *&This,
174 llvm::Value *MemFnPtr,
175 const MemberPointerType *MPT) {
176 CGBuilderTy &Builder = CGF.Builder;
177
178 const FunctionProtoType *FPT =
179 MPT->getPointeeType()->getAs<FunctionProtoType>();
180 const CXXRecordDecl *RD =
181 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
182
183 const llvm::FunctionType *FTy =
184 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
185 FPT->isVariadic());
186
John McCallbabc9a92010-08-22 00:59:17 +0000187 const llvm::IntegerType *ptrdiff = CGF.IntPtrTy;
188 llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(ptrdiff, 1);
John McCall93d557b2010-08-22 00:05:51 +0000189
John McCallbabc9a92010-08-22 00:59:17 +0000190 llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual");
191 llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual");
192 llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end");
193
194 // Load memptr.adj, which is in the second field.
195 llvm::Value *RawAdj = Builder.CreateStructGEP(MemFnPtr, 1);
196 RawAdj = Builder.CreateLoad(RawAdj, "memptr.adj");
197
198 // Compute the true adjustment.
199 llvm::Value *Adj = RawAdj;
200 if (IsARM)
201 Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted");
John McCall93d557b2010-08-22 00:05:51 +0000202
203 // Apply the adjustment and cast back to the original struct type
204 // for consistency.
John McCallbabc9a92010-08-22 00:59:17 +0000205 llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy());
206 Ptr = Builder.CreateInBoundsGEP(Ptr, Adj);
207 This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted");
John McCall93d557b2010-08-22 00:05:51 +0000208
209 // Load the function pointer.
John McCallbabc9a92010-08-22 00:59:17 +0000210 llvm::Value *FnPtr = Builder.CreateStructGEP(MemFnPtr, 0);
211 llvm::Value *FnAsInt = Builder.CreateLoad(FnPtr, "memptr.ptr");
John McCall93d557b2010-08-22 00:05:51 +0000212
213 // If the LSB in the function pointer is 1, the function pointer points to
214 // a virtual function.
John McCallbabc9a92010-08-22 00:59:17 +0000215 llvm::Value *IsVirtual;
216 if (IsARM)
217 IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1);
218 else
219 IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1);
220 IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual");
John McCall93d557b2010-08-22 00:05:51 +0000221 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
222
223 // In the virtual path, the adjustment left 'This' pointing to the
224 // vtable of the correct base subobject. The "function pointer" is an
John McCallbabc9a92010-08-22 00:59:17 +0000225 // offset within the vtable (+1 for the virtual flag on non-ARM).
John McCall93d557b2010-08-22 00:05:51 +0000226 CGF.EmitBlock(FnVirtual);
227
228 // Cast the adjusted this to a pointer to vtable pointer and load.
229 const llvm::Type *VTableTy = Builder.getInt8PtrTy();
230 llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy->getPointerTo());
John McCallbabc9a92010-08-22 00:59:17 +0000231 VTable = Builder.CreateLoad(VTable, "memptr.vtable");
John McCall93d557b2010-08-22 00:05:51 +0000232
233 // Apply the offset.
John McCallbabc9a92010-08-22 00:59:17 +0000234 llvm::Value *VTableOffset = FnAsInt;
235 if (!IsARM) VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1);
236 VTable = Builder.CreateGEP(VTable, VTableOffset);
John McCall93d557b2010-08-22 00:05:51 +0000237
238 // Load the virtual function to call.
239 VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo());
John McCallbabc9a92010-08-22 00:59:17 +0000240 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn");
John McCall93d557b2010-08-22 00:05:51 +0000241 CGF.EmitBranch(FnEnd);
242
243 // In the non-virtual path, the function pointer is actually a
244 // function pointer.
245 CGF.EmitBlock(FnNonVirtual);
246 llvm::Value *NonVirtualFn =
John McCallbabc9a92010-08-22 00:59:17 +0000247 Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn");
John McCall93d557b2010-08-22 00:05:51 +0000248
249 // We're done.
250 CGF.EmitBlock(FnEnd);
251 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
252 Callee->reserveOperandSpace(2);
253 Callee->addIncoming(VirtualFn, FnVirtual);
254 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
255 return Callee;
256}
John McCall3023def2010-08-22 03:04:22 +0000257
258/// Perform a derived-to-base or base-to-derived member pointer conversion.
John McCallcf2c85e2010-08-22 04:16:24 +0000259void ItaniumCXXABI::EmitMemberFunctionPointerConversion(CodeGenFunction &CGF,
260 const CastExpr *E,
261 llvm::Value *Src,
262 llvm::Value *Dest,
263 bool VolatileDest) {
John McCall3023def2010-08-22 03:04:22 +0000264 assert(E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer ||
265 E->getCastKind() == CastExpr::CK_BaseToDerivedMemberPointer);
266
267 CGBuilderTy &Builder = CGF.Builder;
268
269 const MemberPointerType *SrcTy =
270 E->getSubExpr()->getType()->getAs<MemberPointerType>();
271 const MemberPointerType *DestTy = E->getType()->getAs<MemberPointerType>();
272
273 const CXXRecordDecl *SrcDecl = SrcTy->getClass()->getAsCXXRecordDecl();
274 const CXXRecordDecl *DestDecl = DestTy->getClass()->getAsCXXRecordDecl();
275
276 llvm::Value *SrcPtr = Builder.CreateStructGEP(Src, 0, "src.ptr");
277 SrcPtr = Builder.CreateLoad(SrcPtr);
278
279 llvm::Value *SrcAdj = Builder.CreateStructGEP(Src, 1, "src.adj");
280 SrcAdj = Builder.CreateLoad(SrcAdj);
281
282 llvm::Value *DstPtr = Builder.CreateStructGEP(Dest, 0, "dst.ptr");
283 Builder.CreateStore(SrcPtr, DstPtr, VolatileDest);
284
285 llvm::Value *DstAdj = Builder.CreateStructGEP(Dest, 1, "dst.adj");
286
287 bool DerivedToBase =
288 E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer;
289
290 const CXXRecordDecl *BaseDecl, *DerivedDecl;
291 if (DerivedToBase)
292 DerivedDecl = SrcDecl, BaseDecl = DestDecl;
293 else
294 BaseDecl = SrcDecl, DerivedDecl = DestDecl;
295
296 if (llvm::Constant *Adj =
297 CGF.CGM.GetNonVirtualBaseClassOffset(DerivedDecl,
298 E->path_begin(),
299 E->path_end())) {
John McCall875ab102010-08-22 06:43:33 +0000300 // The this-adjustment is left-shifted by 1 on ARM.
301 if (IsARM) {
302 uint64_t Offset = cast<llvm::ConstantInt>(Adj)->getZExtValue();
303 Offset <<= 1;
304 Adj = llvm::ConstantInt::get(Adj->getType(), Offset);
305 }
306
John McCall3023def2010-08-22 03:04:22 +0000307 if (DerivedToBase)
308 SrcAdj = Builder.CreateSub(SrcAdj, Adj, "adj");
309 else
310 SrcAdj = Builder.CreateAdd(SrcAdj, Adj, "adj");
311 }
312
313 Builder.CreateStore(SrcAdj, DstAdj, VolatileDest);
314}
John McCallcf2c85e2010-08-22 04:16:24 +0000315
316llvm::Constant *
317ItaniumCXXABI::EmitMemberFunctionPointerConversion(llvm::Constant *C,
318 const CastExpr *E) {
319 const MemberPointerType *SrcTy =
320 E->getSubExpr()->getType()->getAs<MemberPointerType>();
321 const MemberPointerType *DestTy =
322 E->getType()->getAs<MemberPointerType>();
323
324 bool DerivedToBase =
325 E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer;
326
327 const CXXRecordDecl *DerivedDecl;
328 if (DerivedToBase)
329 DerivedDecl = SrcTy->getClass()->getAsCXXRecordDecl();
330 else
331 DerivedDecl = DestTy->getClass()->getAsCXXRecordDecl();
332
333 // Calculate the offset to the base class.
334 llvm::Constant *Offset =
335 CGM.GetNonVirtualBaseClassOffset(DerivedDecl,
336 E->path_begin(),
337 E->path_end());
338 // If there's no offset, we're done.
339 if (!Offset) return C;
340
John McCall875ab102010-08-22 06:43:33 +0000341 // The this-adjustment is left-shifted by 1 on ARM.
342 if (IsARM) {
343 uint64_t OffsetV = cast<llvm::ConstantInt>(Offset)->getZExtValue();
344 OffsetV <<= 1;
345 Offset = llvm::ConstantInt::get(Offset->getType(), OffsetV);
346 }
347
John McCallcf2c85e2010-08-22 04:16:24 +0000348 llvm::ConstantStruct *CS = cast<llvm::ConstantStruct>(C);
349
350 llvm::Constant *Values[2] = {
351 CS->getOperand(0),
352 llvm::ConstantExpr::getAdd(CS->getOperand(1), Offset)
353 };
354 return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2,
355 /*Packed=*/false);
356}
357
358
359void ItaniumCXXABI::EmitNullMemberFunctionPointer(CodeGenFunction &CGF,
360 const MemberPointerType *MPT,
361 llvm::Value *Dest,
362 bool VolatileDest) {
363 // Should this be "unabstracted" and implemented in terms of the
364 // Constant version?
365
366 CGBuilderTy &Builder = CGF.Builder;
367
368 const llvm::IntegerType *PtrDiffTy = CGF.IntPtrTy;
369 llvm::Value *Zero = llvm::ConstantInt::get(PtrDiffTy, 0);
370
371 llvm::Value *Ptr = Builder.CreateStructGEP(Dest, 0, "ptr");
372 Builder.CreateStore(Zero, Ptr, VolatileDest);
373
374 llvm::Value *Adj = Builder.CreateStructGEP(Dest, 1, "adj");
375 Builder.CreateStore(Zero, Adj, VolatileDest);
376}
377
378llvm::Constant *
379ItaniumCXXABI::EmitNullMemberFunctionPointer(const MemberPointerType *MPT) {
380 return CGM.EmitNullConstant(QualType(MPT, 0));
381}
382
John McCall875ab102010-08-22 06:43:33 +0000383llvm::Constant *
384ItaniumCXXABI::EmitMemberFunctionPointer(const CXXMethodDecl *MD) {
385 llvm::Constant *Values[2];
386 GetMemberFunctionPointer(MD, Values);
387
388 return llvm::ConstantStruct::get(CGM.getLLVMContext(),
389 Values, 2, /*Packed=*/false);
390}
391
392void ItaniumCXXABI::EmitMemberFunctionPointer(CodeGenFunction &CGF,
393 const CXXMethodDecl *MD,
394 llvm::Value *DestPtr,
395 bool VolatileDest) {
396 llvm::Constant *Values[2];
397 GetMemberFunctionPointer(MD, Values);
398
399 CGBuilderTy &Builder = CGF.Builder;
400
401 llvm::Value *DstPtr = Builder.CreateStructGEP(DestPtr, 0, "memptr.ptr");
402 Builder.CreateStore(Values[0], DstPtr, VolatileDest);
403
404 llvm::Value *AdjPtr = Builder.CreateStructGEP(DestPtr, 1, "memptr.adj");
405 Builder.CreateStore(Values[1], AdjPtr, VolatileDest);
406}
407
408
John McCallcf2c85e2010-08-22 04:16:24 +0000409bool ItaniumCXXABI::RequiresNonZeroInitializer(QualType T) {
410 return CGM.getTypes().ContainsPointerToDataMember(T);
411}
412
413bool ItaniumCXXABI::RequiresNonZeroInitializer(const CXXRecordDecl *D) {
414 return CGM.getTypes().ContainsPointerToDataMember(D);
415}