blob: 77320c7c05cd79416af56c01cdf34b9acd13d6e6 [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//
Chris Lattnerfc8f0e12011-04-15 05:22:18 +000010// This provides C++ code generation targeting the Itanium C++ ABI. The class
Charles Davis3a811f12010-05-25 19:52:27 +000011// 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"
Peter Collingbourne14110472011-01-13 18:57:25 +000025#include <clang/AST/Mangle.h>
John McCall93d557b2010-08-22 00:05:51 +000026#include <clang/AST/Type.h>
John McCall0502a222011-06-17 07:33:57 +000027#include <llvm/Intrinsics.h>
John McCall0bab0cd2010-08-23 01:21:21 +000028#include <llvm/Target/TargetData.h>
John McCall93d557b2010-08-22 00:05:51 +000029#include <llvm/Value.h>
Charles Davis3a811f12010-05-25 19:52:27 +000030
31using namespace clang;
John McCall93d557b2010-08-22 00:05:51 +000032using namespace CodeGen;
Charles Davis3a811f12010-05-25 19:52:27 +000033
34namespace {
Charles Davis071cc7d2010-08-16 03:33:14 +000035class ItaniumCXXABI : public CodeGen::CGCXXABI {
John McCall0bab0cd2010-08-23 01:21:21 +000036private:
37 const llvm::IntegerType *PtrDiffTy;
John McCall93d557b2010-08-22 00:05:51 +000038protected:
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) {
John McCall9cb2cee2010-09-02 10:25:57 +000044 QualType T = getContext().getPointerDiffType();
John McCall0bab0cd2010-08-23 01:21:21 +000045 const llvm::Type *Ty = CGM.getTypes().ConvertTypeRecursive(T);
46 PtrDiffTy = cast<llvm::IntegerType>(Ty);
47 }
48 return PtrDiffTy;
49 }
50
John McCall6ec278d2011-01-27 09:37:56 +000051 bool NeedsArrayCookie(const CXXNewExpr *expr);
52 bool NeedsArrayCookie(const CXXDeleteExpr *expr,
53 QualType elementType);
John McCall1e7fe752010-09-02 09:58:18 +000054
Charles Davis3a811f12010-05-25 19:52:27 +000055public:
John McCallbabc9a92010-08-22 00:59:17 +000056 ItaniumCXXABI(CodeGen::CodeGenModule &CGM, bool IsARM = false) :
Peter Collingbourne14110472011-01-13 18:57:25 +000057 CGCXXABI(CGM), PtrDiffTy(0), IsARM(IsARM) { }
John McCall93d557b2010-08-22 00:05:51 +000058
John McCallf16aa102010-08-22 21:01:12 +000059 bool isZeroInitializable(const MemberPointerType *MPT);
John McCallcf2c85e2010-08-22 04:16:24 +000060
John McCall0bab0cd2010-08-23 01:21:21 +000061 const llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT);
62
John McCall93d557b2010-08-22 00:05:51 +000063 llvm::Value *EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
64 llvm::Value *&This,
65 llvm::Value *MemFnPtr,
66 const MemberPointerType *MPT);
John McCall3023def2010-08-22 03:04:22 +000067
John McCall6c2ab1d2010-08-31 21:07:20 +000068 llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
69 llvm::Value *Base,
70 llvm::Value *MemPtr,
71 const MemberPointerType *MPT);
72
John McCall0bab0cd2010-08-23 01:21:21 +000073 llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
74 const CastExpr *E,
75 llvm::Value *Src);
John McCallcf2c85e2010-08-22 04:16:24 +000076
John McCall0bab0cd2010-08-23 01:21:21 +000077 llvm::Constant *EmitMemberPointerConversion(llvm::Constant *C,
78 const CastExpr *E);
John McCallcf2c85e2010-08-22 04:16:24 +000079
John McCall0bab0cd2010-08-23 01:21:21 +000080 llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
John McCallcf2c85e2010-08-22 04:16:24 +000081
John McCall755d8492011-04-12 00:42:48 +000082 llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
John McCall5808ce42011-02-03 08:15:49 +000083 llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
84 CharUnits offset);
John McCall875ab102010-08-22 06:43:33 +000085
John McCall0bab0cd2010-08-23 01:21:21 +000086 llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF,
87 llvm::Value *L,
88 llvm::Value *R,
89 const MemberPointerType *MPT,
90 bool Inequality);
John McCalle9fd7eb2010-08-22 08:30:07 +000091
John McCall0bab0cd2010-08-23 01:21:21 +000092 llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
93 llvm::Value *Addr,
94 const MemberPointerType *MPT);
John McCall4c40d982010-08-31 07:33:07 +000095
96 void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
97 CXXCtorType T,
98 CanQualType &ResTy,
99 llvm::SmallVectorImpl<CanQualType> &ArgTys);
100
101 void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
102 CXXDtorType T,
103 CanQualType &ResTy,
104 llvm::SmallVectorImpl<CanQualType> &ArgTys);
105
106 void BuildInstanceFunctionParams(CodeGenFunction &CGF,
107 QualType &ResTy,
108 FunctionArgList &Params);
109
110 void EmitInstanceFunctionProlog(CodeGenFunction &CGF);
John McCall1e7fe752010-09-02 09:58:18 +0000111
John McCall6ec278d2011-01-27 09:37:56 +0000112 CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
John McCall1e7fe752010-09-02 09:58:18 +0000113 llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
114 llvm::Value *NewPtr,
115 llvm::Value *NumElements,
John McCall6ec278d2011-01-27 09:37:56 +0000116 const CXXNewExpr *expr,
John McCall1e7fe752010-09-02 09:58:18 +0000117 QualType ElementType);
118 void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
John McCall6ec278d2011-01-27 09:37:56 +0000119 const CXXDeleteExpr *expr,
John McCall1e7fe752010-09-02 09:58:18 +0000120 QualType ElementType, llvm::Value *&NumElements,
121 llvm::Value *&AllocPtr, CharUnits &CookieSize);
John McCall5cd91b52010-09-08 01:44:27 +0000122
John McCall3030eb82010-11-06 09:44:32 +0000123 void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
124 llvm::GlobalVariable *DeclPtr);
Charles Davis3a811f12010-05-25 19:52:27 +0000125};
John McCallee79a4c2010-08-21 22:46:04 +0000126
127class ARMCXXABI : public ItaniumCXXABI {
128public:
John McCallbabc9a92010-08-22 00:59:17 +0000129 ARMCXXABI(CodeGen::CodeGenModule &CGM) : ItaniumCXXABI(CGM, /*ARM*/ true) {}
John McCall4c40d982010-08-31 07:33:07 +0000130
131 void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
132 CXXCtorType T,
133 CanQualType &ResTy,
134 llvm::SmallVectorImpl<CanQualType> &ArgTys);
135
136 void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
137 CXXDtorType T,
138 CanQualType &ResTy,
139 llvm::SmallVectorImpl<CanQualType> &ArgTys);
140
141 void BuildInstanceFunctionParams(CodeGenFunction &CGF,
142 QualType &ResTy,
143 FunctionArgList &Params);
144
145 void EmitInstanceFunctionProlog(CodeGenFunction &CGF);
146
147 void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV, QualType ResTy);
148
John McCall6ec278d2011-01-27 09:37:56 +0000149 CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
John McCall1e7fe752010-09-02 09:58:18 +0000150 llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
151 llvm::Value *NewPtr,
152 llvm::Value *NumElements,
John McCall6ec278d2011-01-27 09:37:56 +0000153 const CXXNewExpr *expr,
John McCall1e7fe752010-09-02 09:58:18 +0000154 QualType ElementType);
155 void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
John McCall6ec278d2011-01-27 09:37:56 +0000156 const CXXDeleteExpr *expr,
John McCall1e7fe752010-09-02 09:58:18 +0000157 QualType ElementType, llvm::Value *&NumElements,
158 llvm::Value *&AllocPtr, CharUnits &CookieSize);
John McCall4c40d982010-08-31 07:33:07 +0000159
160private:
161 /// \brief Returns true if the given instance method is one of the
162 /// kinds that the ARM ABI says returns 'this'.
163 static bool HasThisReturn(GlobalDecl GD) {
164 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
165 return ((isa<CXXDestructorDecl>(MD) && GD.getDtorType() != Dtor_Deleting) ||
166 (isa<CXXConstructorDecl>(MD)));
167 }
John McCallee79a4c2010-08-21 22:46:04 +0000168};
Charles Davis3a811f12010-05-25 19:52:27 +0000169}
170
Charles Davis071cc7d2010-08-16 03:33:14 +0000171CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) {
Charles Davis3a811f12010-05-25 19:52:27 +0000172 return new ItaniumCXXABI(CGM);
173}
174
John McCallee79a4c2010-08-21 22:46:04 +0000175CodeGen::CGCXXABI *CodeGen::CreateARMCXXABI(CodeGenModule &CGM) {
176 return new ARMCXXABI(CGM);
177}
178
John McCall0bab0cd2010-08-23 01:21:21 +0000179const llvm::Type *
180ItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
181 if (MPT->isMemberDataPointer())
182 return getPtrDiffTy();
Chris Lattner7650d952011-06-18 22:49:11 +0000183 return llvm::StructType::get(getPtrDiffTy(), getPtrDiffTy(), NULL);
John McCall875ab102010-08-22 06:43:33 +0000184}
185
John McCallbabc9a92010-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 McCall93d557b2010-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 McCall0bab0cd2010-08-23 01:21:21 +0000222 const llvm::IntegerType *ptrdiff = getPtrDiffTy();
John McCallbabc9a92010-08-22 00:59:17 +0000223 llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(ptrdiff, 1);
John McCall93d557b2010-08-22 00:05:51 +0000224
John McCallbabc9a92010-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 McCalld608cdb2010-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 McCallbabc9a92010-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 McCall93d557b2010-08-22 00:05:51 +0000236
237 // Apply the adjustment and cast back to the original struct type
238 // for consistency.
John McCallbabc9a92010-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 McCall93d557b2010-08-22 00:05:51 +0000242
243 // Load the function pointer.
John McCalld608cdb2010-08-22 10:59:02 +0000244 llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr");
John McCall93d557b2010-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 McCallbabc9a92010-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 McCall93d557b2010-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 McCallbabc9a92010-08-22 00:59:17 +0000258 // offset within the vtable (+1 for the virtual flag on non-ARM).
John McCall93d557b2010-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 McCallbabc9a92010-08-22 00:59:17 +0000264 VTable = Builder.CreateLoad(VTable, "memptr.vtable");
John McCall93d557b2010-08-22 00:05:51 +0000265
266 // Apply the offset.
John McCallbabc9a92010-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 McCall93d557b2010-08-22 00:05:51 +0000270
271 // Load the virtual function to call.
272 VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo());
John McCallbabc9a92010-08-22 00:59:17 +0000273 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn");
John McCall93d557b2010-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 McCallbabc9a92010-08-22 00:59:17 +0000280 Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn");
John McCall93d557b2010-08-22 00:05:51 +0000281
282 // We're done.
283 CGF.EmitBlock(FnEnd);
Jay Foadbbf3bac2011-03-30 11:28:58 +0000284 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo(), 2);
John McCall93d557b2010-08-22 00:05:51 +0000285 Callee->addIncoming(VirtualFn, FnVirtual);
286 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
287 return Callee;
288}
John McCall3023def2010-08-22 03:04:22 +0000289
John McCall6c2ab1d2010-08-31 21:07:20 +0000290/// Compute an l-value by applying the given pointer-to-member to a
291/// base object.
292llvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF,
293 llvm::Value *Base,
294 llvm::Value *MemPtr,
295 const MemberPointerType *MPT) {
296 assert(MemPtr->getType() == getPtrDiffTy());
297
298 CGBuilderTy &Builder = CGF.Builder;
299
300 unsigned AS = cast<llvm::PointerType>(Base->getType())->getAddressSpace();
301
302 // Cast to char*.
303 Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS));
304
305 // Apply the offset, which we assume is non-null.
306 llvm::Value *Addr = Builder.CreateInBoundsGEP(Base, MemPtr, "memptr.offset");
307
308 // Cast the address to the appropriate pointer type, adopting the
309 // address space of the base pointer.
Douglas Gregoreede61a2010-09-02 17:38:50 +0000310 const llvm::Type *PType
311 = CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS);
John McCall6c2ab1d2010-08-31 21:07:20 +0000312 return Builder.CreateBitCast(Addr, PType);
313}
314
John McCall3023def2010-08-22 03:04:22 +0000315/// Perform a derived-to-base or base-to-derived member pointer conversion.
John McCall0bab0cd2010-08-23 01:21:21 +0000316///
317/// Obligatory offset/adjustment diagram:
318/// <-- offset --> <-- adjustment -->
319/// |--------------------------|----------------------|--------------------|
320/// ^Derived address point ^Base address point ^Member address point
321///
322/// So when converting a base member pointer to a derived member pointer,
323/// we add the offset to the adjustment because the address point has
324/// decreased; and conversely, when converting a derived MP to a base MP
325/// we subtract the offset from the adjustment because the address point
326/// has increased.
327///
328/// The standard forbids (at compile time) conversion to and from
329/// virtual bases, which is why we don't have to consider them here.
330///
331/// The standard forbids (at run time) casting a derived MP to a base
332/// MP when the derived MP does not point to a member of the base.
333/// This is why -1 is a reasonable choice for null data member
334/// pointers.
John McCalld608cdb2010-08-22 10:59:02 +0000335llvm::Value *
John McCall0bab0cd2010-08-23 01:21:21 +0000336ItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
337 const CastExpr *E,
338 llvm::Value *Src) {
John McCall2de56d12010-08-25 11:45:40 +0000339 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
340 E->getCastKind() == CK_BaseToDerivedMemberPointer);
John McCall3023def2010-08-22 03:04:22 +0000341
John McCalld608cdb2010-08-22 10:59:02 +0000342 if (isa<llvm::Constant>(Src))
John McCall0bab0cd2010-08-23 01:21:21 +0000343 return EmitMemberPointerConversion(cast<llvm::Constant>(Src), E);
John McCalld608cdb2010-08-22 10:59:02 +0000344
John McCall3023def2010-08-22 03:04:22 +0000345 CGBuilderTy &Builder = CGF.Builder;
346
347 const MemberPointerType *SrcTy =
348 E->getSubExpr()->getType()->getAs<MemberPointerType>();
349 const MemberPointerType *DestTy = E->getType()->getAs<MemberPointerType>();
350
351 const CXXRecordDecl *SrcDecl = SrcTy->getClass()->getAsCXXRecordDecl();
352 const CXXRecordDecl *DestDecl = DestTy->getClass()->getAsCXXRecordDecl();
353
John McCall3023def2010-08-22 03:04:22 +0000354 bool DerivedToBase =
John McCall2de56d12010-08-25 11:45:40 +0000355 E->getCastKind() == CK_DerivedToBaseMemberPointer;
John McCall3023def2010-08-22 03:04:22 +0000356
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000357 const CXXRecordDecl *DerivedDecl;
John McCall3023def2010-08-22 03:04:22 +0000358 if (DerivedToBase)
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000359 DerivedDecl = SrcDecl;
John McCall3023def2010-08-22 03:04:22 +0000360 else
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000361 DerivedDecl = DestDecl;
John McCall3023def2010-08-22 03:04:22 +0000362
John McCalld608cdb2010-08-22 10:59:02 +0000363 llvm::Constant *Adj =
364 CGF.CGM.GetNonVirtualBaseClassOffset(DerivedDecl,
365 E->path_begin(),
366 E->path_end());
367 if (!Adj) return Src;
John McCall875ab102010-08-22 06:43:33 +0000368
John McCall0bab0cd2010-08-23 01:21:21 +0000369 // For member data pointers, this is just a matter of adding the
370 // offset if the source is non-null.
371 if (SrcTy->isMemberDataPointer()) {
372 llvm::Value *Dst;
373 if (DerivedToBase)
374 Dst = Builder.CreateNSWSub(Src, Adj, "adj");
375 else
376 Dst = Builder.CreateNSWAdd(Src, Adj, "adj");
377
378 // Null check.
379 llvm::Value *Null = llvm::Constant::getAllOnesValue(Src->getType());
380 llvm::Value *IsNull = Builder.CreateICmpEQ(Src, Null, "memptr.isnull");
381 return Builder.CreateSelect(IsNull, Src, Dst);
382 }
383
John McCalld608cdb2010-08-22 10:59:02 +0000384 // The this-adjustment is left-shifted by 1 on ARM.
385 if (IsARM) {
386 uint64_t Offset = cast<llvm::ConstantInt>(Adj)->getZExtValue();
387 Offset <<= 1;
388 Adj = llvm::ConstantInt::get(Adj->getType(), Offset);
389 }
390
John McCalle14add42010-08-22 11:04:31 +0000391 llvm::Value *SrcAdj = Builder.CreateExtractValue(Src, 1, "src.adj");
John McCalld608cdb2010-08-22 10:59:02 +0000392 llvm::Value *DstAdj;
393 if (DerivedToBase)
John McCall0bab0cd2010-08-23 01:21:21 +0000394 DstAdj = Builder.CreateNSWSub(SrcAdj, Adj, "adj");
John McCalld608cdb2010-08-22 10:59:02 +0000395 else
John McCall0bab0cd2010-08-23 01:21:21 +0000396 DstAdj = Builder.CreateNSWAdd(SrcAdj, Adj, "adj");
John McCalld608cdb2010-08-22 10:59:02 +0000397
John McCalle14add42010-08-22 11:04:31 +0000398 return Builder.CreateInsertValue(Src, DstAdj, 1);
John McCall3023def2010-08-22 03:04:22 +0000399}
John McCallcf2c85e2010-08-22 04:16:24 +0000400
401llvm::Constant *
John McCall0bab0cd2010-08-23 01:21:21 +0000402ItaniumCXXABI::EmitMemberPointerConversion(llvm::Constant *C,
403 const CastExpr *E) {
John McCallcf2c85e2010-08-22 04:16:24 +0000404 const MemberPointerType *SrcTy =
405 E->getSubExpr()->getType()->getAs<MemberPointerType>();
406 const MemberPointerType *DestTy =
407 E->getType()->getAs<MemberPointerType>();
408
409 bool DerivedToBase =
John McCall2de56d12010-08-25 11:45:40 +0000410 E->getCastKind() == CK_DerivedToBaseMemberPointer;
John McCallcf2c85e2010-08-22 04:16:24 +0000411
412 const CXXRecordDecl *DerivedDecl;
413 if (DerivedToBase)
414 DerivedDecl = SrcTy->getClass()->getAsCXXRecordDecl();
415 else
416 DerivedDecl = DestTy->getClass()->getAsCXXRecordDecl();
417
418 // Calculate the offset to the base class.
419 llvm::Constant *Offset =
420 CGM.GetNonVirtualBaseClassOffset(DerivedDecl,
421 E->path_begin(),
422 E->path_end());
423 // If there's no offset, we're done.
424 if (!Offset) return C;
425
John McCall0bab0cd2010-08-23 01:21:21 +0000426 // If the source is a member data pointer, we have to do a null
427 // check and then add the offset. In the common case, we can fold
428 // away the offset.
429 if (SrcTy->isMemberDataPointer()) {
430 assert(C->getType() == getPtrDiffTy());
431
432 // If it's a constant int, just create a new constant int.
433 if (llvm::ConstantInt *CI = dyn_cast<llvm::ConstantInt>(C)) {
434 int64_t Src = CI->getSExtValue();
435
436 // Null converts to null.
437 if (Src == -1) return CI;
438
439 // Otherwise, just add the offset.
440 int64_t OffsetV = cast<llvm::ConstantInt>(Offset)->getSExtValue();
441 int64_t Dst = (DerivedToBase ? Src - OffsetV : Src + OffsetV);
442 return llvm::ConstantInt::get(CI->getType(), Dst, /*signed*/ true);
443 }
444
445 // Otherwise, we have to form a constant select expression.
446 llvm::Constant *Null = llvm::Constant::getAllOnesValue(C->getType());
447
448 llvm::Constant *IsNull =
449 llvm::ConstantExpr::getICmp(llvm::ICmpInst::ICMP_EQ, C, Null);
450
451 llvm::Constant *Dst;
452 if (DerivedToBase)
453 Dst = llvm::ConstantExpr::getNSWSub(C, Offset);
454 else
455 Dst = llvm::ConstantExpr::getNSWAdd(C, Offset);
456
457 return llvm::ConstantExpr::getSelect(IsNull, Null, Dst);
458 }
459
John McCall875ab102010-08-22 06:43:33 +0000460 // The this-adjustment is left-shifted by 1 on ARM.
461 if (IsARM) {
John McCall0bab0cd2010-08-23 01:21:21 +0000462 int64_t OffsetV = cast<llvm::ConstantInt>(Offset)->getSExtValue();
John McCall875ab102010-08-22 06:43:33 +0000463 OffsetV <<= 1;
464 Offset = llvm::ConstantInt::get(Offset->getType(), OffsetV);
465 }
466
John McCallcf2c85e2010-08-22 04:16:24 +0000467 llvm::ConstantStruct *CS = cast<llvm::ConstantStruct>(C);
468
John McCall0bab0cd2010-08-23 01:21:21 +0000469 llvm::Constant *Values[2] = { CS->getOperand(0), 0 };
470 if (DerivedToBase)
471 Values[1] = llvm::ConstantExpr::getSub(CS->getOperand(1), Offset);
472 else
473 Values[1] = llvm::ConstantExpr::getAdd(CS->getOperand(1), Offset);
474
John McCallcf2c85e2010-08-22 04:16:24 +0000475 return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2,
476 /*Packed=*/false);
477}
478
479
John McCallcf2c85e2010-08-22 04:16:24 +0000480llvm::Constant *
John McCall0bab0cd2010-08-23 01:21:21 +0000481ItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
482 const llvm::Type *ptrdiff_t = getPtrDiffTy();
483
484 // Itanium C++ ABI 2.3:
485 // A NULL pointer is represented as -1.
486 if (MPT->isMemberDataPointer())
487 return llvm::ConstantInt::get(ptrdiff_t, -1ULL, /*isSigned=*/true);
John McCalld608cdb2010-08-22 10:59:02 +0000488
489 llvm::Constant *Zero = llvm::ConstantInt::get(ptrdiff_t, 0);
490 llvm::Constant *Values[2] = { Zero, Zero };
491 return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2,
492 /*Packed=*/false);
John McCallcf2c85e2010-08-22 04:16:24 +0000493}
494
John McCall5808ce42011-02-03 08:15:49 +0000495llvm::Constant *
496ItaniumCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
497 CharUnits offset) {
John McCall0bab0cd2010-08-23 01:21:21 +0000498 // Itanium C++ ABI 2.3:
499 // A pointer to data member is an offset from the base address of
500 // the class object containing it, represented as a ptrdiff_t
John McCall5808ce42011-02-03 08:15:49 +0000501 return llvm::ConstantInt::get(getPtrDiffTy(), offset.getQuantity());
John McCall0bab0cd2010-08-23 01:21:21 +0000502}
503
John McCall755d8492011-04-12 00:42:48 +0000504llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) {
John McCalld608cdb2010-08-22 10:59:02 +0000505 assert(MD->isInstance() && "Member function must not be static!");
506 MD = MD->getCanonicalDecl();
507
508 CodeGenTypes &Types = CGM.getTypes();
John McCall0bab0cd2010-08-23 01:21:21 +0000509 const llvm::Type *ptrdiff_t = getPtrDiffTy();
John McCalld608cdb2010-08-22 10:59:02 +0000510
511 // Get the function pointer (or index if this is a virtual function).
512 llvm::Constant *MemPtr[2];
513 if (MD->isVirtual()) {
514 uint64_t Index = CGM.getVTables().getMethodVTableIndex(MD);
515
Ken Dyck1246ba62011-04-09 01:30:02 +0000516 const ASTContext &Context = getContext();
517 CharUnits PointerWidth =
518 Context.toCharUnitsFromBits(Context.Target.getPointerWidth(0));
519 uint64_t VTableOffset = (Index * PointerWidth.getQuantity());
John McCalld608cdb2010-08-22 10:59:02 +0000520
521 if (IsARM) {
522 // ARM C++ ABI 3.2.1:
523 // This ABI specifies that adj contains twice the this
524 // adjustment, plus 1 if the member function is virtual. The
525 // least significant bit of adj then makes exactly the same
526 // discrimination as the least significant bit of ptr does for
527 // Itanium.
528 MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset);
529 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 1);
530 } else {
531 // Itanium C++ ABI 2.3:
532 // For a virtual function, [the pointer field] is 1 plus the
533 // virtual table offset (in bytes) of the function,
534 // represented as a ptrdiff_t.
535 MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset + 1);
536 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0);
537 }
538 } else {
John McCall755d8492011-04-12 00:42:48 +0000539 const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
540 const llvm::Type *Ty;
541 // Check whether the function has a computable LLVM signature.
542 if (!CodeGenTypes::VerifyFuncTypeComplete(FPT)) {
543 // The function has a computable LLVM signature; use the correct type.
544 Ty = Types.GetFunctionType(Types.getFunctionInfo(MD),
545 FPT->isVariadic());
John McCalld608cdb2010-08-22 10:59:02 +0000546 } else {
John McCall755d8492011-04-12 00:42:48 +0000547 // Use an arbitrary non-function type to tell GetAddrOfFunction that the
548 // function type is incomplete.
549 Ty = ptrdiff_t;
John McCalld608cdb2010-08-22 10:59:02 +0000550 }
John McCall755d8492011-04-12 00:42:48 +0000551 llvm::Constant *addr = CGM.GetAddrOfFunction(MD, Ty);
John McCalld608cdb2010-08-22 10:59:02 +0000552
John McCall379b5152011-04-11 07:02:50 +0000553 MemPtr[0] = llvm::ConstantExpr::getPtrToInt(addr, ptrdiff_t);
John McCalld608cdb2010-08-22 10:59:02 +0000554 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0);
555 }
John McCall875ab102010-08-22 06:43:33 +0000556
557 return llvm::ConstantStruct::get(CGM.getLLVMContext(),
John McCalld608cdb2010-08-22 10:59:02 +0000558 MemPtr, 2, /*Packed=*/false);
John McCall875ab102010-08-22 06:43:33 +0000559}
560
John McCalle9fd7eb2010-08-22 08:30:07 +0000561/// The comparison algorithm is pretty easy: the member pointers are
562/// the same if they're either bitwise identical *or* both null.
563///
564/// ARM is different here only because null-ness is more complicated.
565llvm::Value *
John McCall0bab0cd2010-08-23 01:21:21 +0000566ItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
567 llvm::Value *L,
568 llvm::Value *R,
569 const MemberPointerType *MPT,
570 bool Inequality) {
John McCalle9fd7eb2010-08-22 08:30:07 +0000571 CGBuilderTy &Builder = CGF.Builder;
572
John McCalle9fd7eb2010-08-22 08:30:07 +0000573 llvm::ICmpInst::Predicate Eq;
574 llvm::Instruction::BinaryOps And, Or;
575 if (Inequality) {
576 Eq = llvm::ICmpInst::ICMP_NE;
577 And = llvm::Instruction::Or;
578 Or = llvm::Instruction::And;
579 } else {
580 Eq = llvm::ICmpInst::ICMP_EQ;
581 And = llvm::Instruction::And;
582 Or = llvm::Instruction::Or;
583 }
584
John McCall0bab0cd2010-08-23 01:21:21 +0000585 // Member data pointers are easy because there's a unique null
586 // value, so it just comes down to bitwise equality.
587 if (MPT->isMemberDataPointer())
588 return Builder.CreateICmp(Eq, L, R);
589
590 // For member function pointers, the tautologies are more complex.
591 // The Itanium tautology is:
John McCallde719f72010-08-23 06:56:36 +0000592 // (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj))
John McCall0bab0cd2010-08-23 01:21:21 +0000593 // The ARM tautology is:
John McCallde719f72010-08-23 06:56:36 +0000594 // (L == R) <==> (L.ptr == R.ptr &&
595 // (L.adj == R.adj ||
596 // (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0)))
John McCall0bab0cd2010-08-23 01:21:21 +0000597 // The inequality tautologies have exactly the same structure, except
598 // applying De Morgan's laws.
599
600 llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr");
601 llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr");
602
John McCalle9fd7eb2010-08-22 08:30:07 +0000603 // This condition tests whether L.ptr == R.ptr. This must always be
604 // true for equality to hold.
605 llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr");
606
607 // This condition, together with the assumption that L.ptr == R.ptr,
608 // tests whether the pointers are both null. ARM imposes an extra
609 // condition.
610 llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType());
611 llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null");
612
613 // This condition tests whether L.adj == R.adj. If this isn't
614 // true, the pointers are unequal unless they're both null.
John McCalld608cdb2010-08-22 10:59:02 +0000615 llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj");
616 llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj");
John McCalle9fd7eb2010-08-22 08:30:07 +0000617 llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj");
618
619 // Null member function pointers on ARM clear the low bit of Adj,
620 // so the zero condition has to check that neither low bit is set.
621 if (IsARM) {
622 llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1);
623
624 // Compute (l.adj | r.adj) & 1 and test it against zero.
625 llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj");
626 llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One);
627 llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero,
628 "cmp.or.adj");
629 EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero);
630 }
631
632 // Tie together all our conditions.
633 llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq);
634 Result = Builder.CreateBinOp(And, PtrEq, Result,
635 Inequality ? "memptr.ne" : "memptr.eq");
636 return Result;
637}
638
639llvm::Value *
John McCall0bab0cd2010-08-23 01:21:21 +0000640ItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
641 llvm::Value *MemPtr,
642 const MemberPointerType *MPT) {
John McCalle9fd7eb2010-08-22 08:30:07 +0000643 CGBuilderTy &Builder = CGF.Builder;
John McCall0bab0cd2010-08-23 01:21:21 +0000644
645 /// For member data pointers, this is just a check against -1.
646 if (MPT->isMemberDataPointer()) {
647 assert(MemPtr->getType() == getPtrDiffTy());
648 llvm::Value *NegativeOne =
649 llvm::Constant::getAllOnesValue(MemPtr->getType());
650 return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool");
651 }
John McCalle9fd7eb2010-08-22 08:30:07 +0000652
Daniel Dunbardb27b5f2011-04-19 23:10:47 +0000653 // In Itanium, a member function pointer is not null if 'ptr' is not null.
John McCalld608cdb2010-08-22 10:59:02 +0000654 llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr");
John McCalle9fd7eb2010-08-22 08:30:07 +0000655
656 llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0);
657 llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool");
658
Daniel Dunbardb27b5f2011-04-19 23:10:47 +0000659 // On ARM, a member function pointer is also non-null if the low bit of 'adj'
660 // (the virtual bit) is set.
John McCalle9fd7eb2010-08-22 08:30:07 +0000661 if (IsARM) {
662 llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1);
John McCalld608cdb2010-08-22 10:59:02 +0000663 llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj");
John McCalle9fd7eb2010-08-22 08:30:07 +0000664 llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit");
Daniel Dunbardb27b5f2011-04-19 23:10:47 +0000665 llvm::Value *IsVirtual = Builder.CreateICmpNE(VirtualBit, Zero,
666 "memptr.isvirtual");
667 Result = Builder.CreateOr(Result, IsVirtual);
John McCalle9fd7eb2010-08-22 08:30:07 +0000668 }
669
670 return Result;
671}
John McCall875ab102010-08-22 06:43:33 +0000672
John McCallf16aa102010-08-22 21:01:12 +0000673/// The Itanium ABI requires non-zero initialization only for data
674/// member pointers, for which '0' is a valid offset.
675bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
676 return MPT->getPointeeType()->isFunctionType();
John McCallcf2c85e2010-08-22 04:16:24 +0000677}
John McCall4c40d982010-08-31 07:33:07 +0000678
679/// The generic ABI passes 'this', plus a VTT if it's initializing a
680/// base subobject.
681void ItaniumCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor,
682 CXXCtorType Type,
683 CanQualType &ResTy,
684 llvm::SmallVectorImpl<CanQualType> &ArgTys) {
John McCall9cb2cee2010-09-02 10:25:57 +0000685 ASTContext &Context = getContext();
John McCall4c40d982010-08-31 07:33:07 +0000686
687 // 'this' is already there.
688
689 // Check if we need to add a VTT parameter (which has type void **).
690 if (Type == Ctor_Base && Ctor->getParent()->getNumVBases() != 0)
691 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
692}
693
694/// The ARM ABI does the same as the Itanium ABI, but returns 'this'.
695void ARMCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor,
696 CXXCtorType Type,
697 CanQualType &ResTy,
698 llvm::SmallVectorImpl<CanQualType> &ArgTys) {
699 ItaniumCXXABI::BuildConstructorSignature(Ctor, Type, ResTy, ArgTys);
700 ResTy = ArgTys[0];
701}
702
703/// The generic ABI passes 'this', plus a VTT if it's destroying a
704/// base subobject.
705void ItaniumCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor,
706 CXXDtorType Type,
707 CanQualType &ResTy,
708 llvm::SmallVectorImpl<CanQualType> &ArgTys) {
John McCall9cb2cee2010-09-02 10:25:57 +0000709 ASTContext &Context = getContext();
John McCall4c40d982010-08-31 07:33:07 +0000710
711 // 'this' is already there.
712
713 // Check if we need to add a VTT parameter (which has type void **).
714 if (Type == Dtor_Base && Dtor->getParent()->getNumVBases() != 0)
715 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
716}
717
718/// The ARM ABI does the same as the Itanium ABI, but returns 'this'
719/// for non-deleting destructors.
720void ARMCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor,
721 CXXDtorType Type,
722 CanQualType &ResTy,
723 llvm::SmallVectorImpl<CanQualType> &ArgTys) {
724 ItaniumCXXABI::BuildDestructorSignature(Dtor, Type, ResTy, ArgTys);
725
726 if (Type != Dtor_Deleting)
727 ResTy = ArgTys[0];
728}
729
730void ItaniumCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF,
731 QualType &ResTy,
732 FunctionArgList &Params) {
733 /// Create the 'this' variable.
734 BuildThisParam(CGF, Params);
735
736 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
737 assert(MD->isInstance());
738
739 // Check if we need a VTT parameter as well.
740 if (CodeGenVTables::needsVTTParameter(CGF.CurGD)) {
John McCall9cb2cee2010-09-02 10:25:57 +0000741 ASTContext &Context = getContext();
John McCall4c40d982010-08-31 07:33:07 +0000742
743 // FIXME: avoid the fake decl
744 QualType T = Context.getPointerType(Context.VoidPtrTy);
745 ImplicitParamDecl *VTTDecl
746 = ImplicitParamDecl::Create(Context, 0, MD->getLocation(),
747 &Context.Idents.get("vtt"), T);
John McCalld26bc762011-03-09 04:27:21 +0000748 Params.push_back(VTTDecl);
John McCall4c40d982010-08-31 07:33:07 +0000749 getVTTDecl(CGF) = VTTDecl;
750 }
751}
752
753void ARMCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF,
754 QualType &ResTy,
755 FunctionArgList &Params) {
756 ItaniumCXXABI::BuildInstanceFunctionParams(CGF, ResTy, Params);
757
758 // Return 'this' from certain constructors and destructors.
759 if (HasThisReturn(CGF.CurGD))
John McCalld26bc762011-03-09 04:27:21 +0000760 ResTy = Params[0]->getType();
John McCall4c40d982010-08-31 07:33:07 +0000761}
762
763void ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
764 /// Initialize the 'this' slot.
765 EmitThisParam(CGF);
766
767 /// Initialize the 'vtt' slot if needed.
768 if (getVTTDecl(CGF)) {
769 getVTTValue(CGF)
770 = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(getVTTDecl(CGF)),
771 "vtt");
772 }
773}
774
775void ARMCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
776 ItaniumCXXABI::EmitInstanceFunctionProlog(CGF);
777
778 /// Initialize the return slot to 'this' at the start of the
779 /// function.
780 if (HasThisReturn(CGF.CurGD))
781 CGF.Builder.CreateStore(CGF.LoadCXXThis(), CGF.ReturnValue);
782}
783
784void ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF,
785 RValue RV, QualType ResultType) {
786 if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl()))
787 return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType);
788
789 // Destructor thunks in the ARM ABI have indeterminate results.
790 const llvm::Type *T =
791 cast<llvm::PointerType>(CGF.ReturnValue->getType())->getElementType();
792 RValue Undef = RValue::get(llvm::UndefValue::get(T));
793 return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType);
794}
John McCall1e7fe752010-09-02 09:58:18 +0000795
796/************************** Array allocation cookies **************************/
797
John McCall6ec278d2011-01-27 09:37:56 +0000798bool ItaniumCXXABI::NeedsArrayCookie(const CXXNewExpr *expr) {
John McCall1e7fe752010-09-02 09:58:18 +0000799 // If the class's usual deallocation function takes two arguments,
John McCall6ec278d2011-01-27 09:37:56 +0000800 // it needs a cookie.
801 if (expr->doesUsualArrayDeleteWantSize())
802 return true;
John McCall1e7fe752010-09-02 09:58:18 +0000803
John McCallf85e1932011-06-15 23:02:42 +0000804 // Automatic Reference Counting:
805 // We need an array cookie for pointers with strong or weak lifetime.
806 QualType AllocatedType = expr->getAllocatedType();
807 if (getContext().getLangOptions().ObjCAutoRefCount &&
808 AllocatedType->isObjCLifetimeType()) {
809 switch (AllocatedType.getObjCLifetime()) {
810 case Qualifiers::OCL_None:
811 case Qualifiers::OCL_ExplicitNone:
812 case Qualifiers::OCL_Autoreleasing:
813 return false;
814
815 case Qualifiers::OCL_Strong:
816 case Qualifiers::OCL_Weak:
817 return true;
818 }
819 }
820
John McCall6ec278d2011-01-27 09:37:56 +0000821 // Otherwise, if the class has a non-trivial destructor, it always
822 // needs a cookie.
823 const CXXRecordDecl *record =
John McCallf85e1932011-06-15 23:02:42 +0000824 AllocatedType->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
John McCall6ec278d2011-01-27 09:37:56 +0000825 return (record && !record->hasTrivialDestructor());
John McCall1e7fe752010-09-02 09:58:18 +0000826}
827
John McCall6ec278d2011-01-27 09:37:56 +0000828bool ItaniumCXXABI::NeedsArrayCookie(const CXXDeleteExpr *expr,
829 QualType elementType) {
830 // If the class's usual deallocation function takes two arguments,
831 // it needs a cookie.
832 if (expr->doesUsualArrayDeleteWantSize())
833 return true;
834
John McCallf85e1932011-06-15 23:02:42 +0000835 // Automatic Reference Counting:
836 // We need an array cookie for pointers with strong or weak lifetime.
837 if (getContext().getLangOptions().ObjCAutoRefCount &&
838 elementType->isObjCLifetimeType()) {
839 switch (elementType.getObjCLifetime()) {
840 case Qualifiers::OCL_None:
841 case Qualifiers::OCL_ExplicitNone:
842 case Qualifiers::OCL_Autoreleasing:
843 return false;
844
845 case Qualifiers::OCL_Strong:
846 case Qualifiers::OCL_Weak:
847 return true;
848 }
849 }
850
John McCall6ec278d2011-01-27 09:37:56 +0000851 // Otherwise, if the class has a non-trivial destructor, it always
852 // needs a cookie.
853 const CXXRecordDecl *record =
854 elementType->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
855 return (record && !record->hasTrivialDestructor());
856}
857
858CharUnits ItaniumCXXABI::GetArrayCookieSize(const CXXNewExpr *expr) {
859 if (!NeedsArrayCookie(expr))
John McCall1e7fe752010-09-02 09:58:18 +0000860 return CharUnits::Zero();
861
John McCall6ec278d2011-01-27 09:37:56 +0000862 // Padding is the maximum of sizeof(size_t) and alignof(elementType)
John McCall9cb2cee2010-09-02 10:25:57 +0000863 ASTContext &Ctx = getContext();
John McCall1e7fe752010-09-02 09:58:18 +0000864 return std::max(Ctx.getTypeSizeInChars(Ctx.getSizeType()),
John McCall6ec278d2011-01-27 09:37:56 +0000865 Ctx.getTypeAlignInChars(expr->getAllocatedType()));
John McCall1e7fe752010-09-02 09:58:18 +0000866}
867
868llvm::Value *ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
869 llvm::Value *NewPtr,
870 llvm::Value *NumElements,
John McCall6ec278d2011-01-27 09:37:56 +0000871 const CXXNewExpr *expr,
John McCall1e7fe752010-09-02 09:58:18 +0000872 QualType ElementType) {
John McCall6ec278d2011-01-27 09:37:56 +0000873 assert(NeedsArrayCookie(expr));
John McCall1e7fe752010-09-02 09:58:18 +0000874
875 unsigned AS = cast<llvm::PointerType>(NewPtr->getType())->getAddressSpace();
876
John McCall9cb2cee2010-09-02 10:25:57 +0000877 ASTContext &Ctx = getContext();
John McCall1e7fe752010-09-02 09:58:18 +0000878 QualType SizeTy = Ctx.getSizeType();
879 CharUnits SizeSize = Ctx.getTypeSizeInChars(SizeTy);
880
881 // The size of the cookie.
882 CharUnits CookieSize =
883 std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType));
884
885 // Compute an offset to the cookie.
886 llvm::Value *CookiePtr = NewPtr;
887 CharUnits CookieOffset = CookieSize - SizeSize;
888 if (!CookieOffset.isZero())
889 CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_64(CookiePtr,
890 CookieOffset.getQuantity());
891
892 // Write the number of elements into the appropriate slot.
893 llvm::Value *NumElementsPtr
894 = CGF.Builder.CreateBitCast(CookiePtr,
895 CGF.ConvertType(SizeTy)->getPointerTo(AS));
896 CGF.Builder.CreateStore(NumElements, NumElementsPtr);
897
898 // Finally, compute a pointer to the actual data buffer by skipping
899 // over the cookie completely.
900 return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr,
901 CookieSize.getQuantity());
902}
903
904void ItaniumCXXABI::ReadArrayCookie(CodeGenFunction &CGF,
905 llvm::Value *Ptr,
John McCall6ec278d2011-01-27 09:37:56 +0000906 const CXXDeleteExpr *expr,
John McCall1e7fe752010-09-02 09:58:18 +0000907 QualType ElementType,
908 llvm::Value *&NumElements,
909 llvm::Value *&AllocPtr,
910 CharUnits &CookieSize) {
911 // Derive a char* in the same address space as the pointer.
912 unsigned AS = cast<llvm::PointerType>(Ptr->getType())->getAddressSpace();
913 const llvm::Type *CharPtrTy = CGF.Builder.getInt8Ty()->getPointerTo(AS);
914
915 // If we don't need an array cookie, bail out early.
John McCall6ec278d2011-01-27 09:37:56 +0000916 if (!NeedsArrayCookie(expr, ElementType)) {
John McCall1e7fe752010-09-02 09:58:18 +0000917 AllocPtr = CGF.Builder.CreateBitCast(Ptr, CharPtrTy);
918 NumElements = 0;
919 CookieSize = CharUnits::Zero();
920 return;
921 }
922
John McCall9cb2cee2010-09-02 10:25:57 +0000923 QualType SizeTy = getContext().getSizeType();
924 CharUnits SizeSize = getContext().getTypeSizeInChars(SizeTy);
John McCall1e7fe752010-09-02 09:58:18 +0000925 const llvm::Type *SizeLTy = CGF.ConvertType(SizeTy);
926
927 CookieSize
John McCall9cb2cee2010-09-02 10:25:57 +0000928 = std::max(SizeSize, getContext().getTypeAlignInChars(ElementType));
John McCall1e7fe752010-09-02 09:58:18 +0000929
930 CharUnits NumElementsOffset = CookieSize - SizeSize;
931
932 // Compute the allocated pointer.
933 AllocPtr = CGF.Builder.CreateBitCast(Ptr, CharPtrTy);
934 AllocPtr = CGF.Builder.CreateConstInBoundsGEP1_64(AllocPtr,
935 -CookieSize.getQuantity());
936
937 llvm::Value *NumElementsPtr = AllocPtr;
938 if (!NumElementsOffset.isZero())
939 NumElementsPtr =
940 CGF.Builder.CreateConstInBoundsGEP1_64(NumElementsPtr,
941 NumElementsOffset.getQuantity());
942 NumElementsPtr =
943 CGF.Builder.CreateBitCast(NumElementsPtr, SizeLTy->getPointerTo(AS));
944 NumElements = CGF.Builder.CreateLoad(NumElementsPtr);
945}
946
John McCall6ec278d2011-01-27 09:37:56 +0000947CharUnits ARMCXXABI::GetArrayCookieSize(const CXXNewExpr *expr) {
948 if (!NeedsArrayCookie(expr))
John McCall1e7fe752010-09-02 09:58:18 +0000949 return CharUnits::Zero();
950
951 // On ARM, the cookie is always:
952 // struct array_cookie {
953 // std::size_t element_size; // element_size != 0
954 // std::size_t element_count;
955 // };
956 // TODO: what should we do if the allocated type actually wants
957 // greater alignment?
958 return getContext().getTypeSizeInChars(getContext().getSizeType()) * 2;
959}
960
961llvm::Value *ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
962 llvm::Value *NewPtr,
963 llvm::Value *NumElements,
John McCall6ec278d2011-01-27 09:37:56 +0000964 const CXXNewExpr *expr,
John McCall1e7fe752010-09-02 09:58:18 +0000965 QualType ElementType) {
John McCall6ec278d2011-01-27 09:37:56 +0000966 assert(NeedsArrayCookie(expr));
John McCall1e7fe752010-09-02 09:58:18 +0000967
968 // NewPtr is a char*.
969
970 unsigned AS = cast<llvm::PointerType>(NewPtr->getType())->getAddressSpace();
971
John McCall9cb2cee2010-09-02 10:25:57 +0000972 ASTContext &Ctx = getContext();
John McCall1e7fe752010-09-02 09:58:18 +0000973 CharUnits SizeSize = Ctx.getTypeSizeInChars(Ctx.getSizeType());
974 const llvm::IntegerType *SizeTy =
975 cast<llvm::IntegerType>(CGF.ConvertType(Ctx.getSizeType()));
976
977 // The cookie is always at the start of the buffer.
978 llvm::Value *CookiePtr = NewPtr;
979
980 // The first element is the element size.
981 CookiePtr = CGF.Builder.CreateBitCast(CookiePtr, SizeTy->getPointerTo(AS));
982 llvm::Value *ElementSize = llvm::ConstantInt::get(SizeTy,
983 Ctx.getTypeSizeInChars(ElementType).getQuantity());
984 CGF.Builder.CreateStore(ElementSize, CookiePtr);
985
986 // The second element is the element count.
987 CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_32(CookiePtr, 1);
988 CGF.Builder.CreateStore(NumElements, CookiePtr);
989
990 // Finally, compute a pointer to the actual data buffer by skipping
991 // over the cookie completely.
992 CharUnits CookieSize = 2 * SizeSize;
993 return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr,
994 CookieSize.getQuantity());
995}
996
997void ARMCXXABI::ReadArrayCookie(CodeGenFunction &CGF,
998 llvm::Value *Ptr,
John McCall6ec278d2011-01-27 09:37:56 +0000999 const CXXDeleteExpr *expr,
John McCall1e7fe752010-09-02 09:58:18 +00001000 QualType ElementType,
1001 llvm::Value *&NumElements,
1002 llvm::Value *&AllocPtr,
1003 CharUnits &CookieSize) {
1004 // Derive a char* in the same address space as the pointer.
1005 unsigned AS = cast<llvm::PointerType>(Ptr->getType())->getAddressSpace();
1006 const llvm::Type *CharPtrTy = CGF.Builder.getInt8Ty()->getPointerTo(AS);
1007
1008 // If we don't need an array cookie, bail out early.
John McCall6ec278d2011-01-27 09:37:56 +00001009 if (!NeedsArrayCookie(expr, ElementType)) {
John McCall1e7fe752010-09-02 09:58:18 +00001010 AllocPtr = CGF.Builder.CreateBitCast(Ptr, CharPtrTy);
1011 NumElements = 0;
1012 CookieSize = CharUnits::Zero();
1013 return;
1014 }
1015
John McCall9cb2cee2010-09-02 10:25:57 +00001016 QualType SizeTy = getContext().getSizeType();
1017 CharUnits SizeSize = getContext().getTypeSizeInChars(SizeTy);
John McCall1e7fe752010-09-02 09:58:18 +00001018 const llvm::Type *SizeLTy = CGF.ConvertType(SizeTy);
1019
1020 // The cookie size is always 2 * sizeof(size_t).
1021 CookieSize = 2 * SizeSize;
John McCall1e7fe752010-09-02 09:58:18 +00001022
1023 // The allocated pointer is the input ptr, minus that amount.
1024 AllocPtr = CGF.Builder.CreateBitCast(Ptr, CharPtrTy);
1025 AllocPtr = CGF.Builder.CreateConstInBoundsGEP1_64(AllocPtr,
1026 -CookieSize.getQuantity());
1027
1028 // The number of elements is at offset sizeof(size_t) relative to that.
1029 llvm::Value *NumElementsPtr
1030 = CGF.Builder.CreateConstInBoundsGEP1_64(AllocPtr,
1031 SizeSize.getQuantity());
1032 NumElementsPtr =
1033 CGF.Builder.CreateBitCast(NumElementsPtr, SizeLTy->getPointerTo(AS));
1034 NumElements = CGF.Builder.CreateLoad(NumElementsPtr);
1035}
1036
John McCall5cd91b52010-09-08 01:44:27 +00001037/*********************** Static local initialization **************************/
1038
1039static llvm::Constant *getGuardAcquireFn(CodeGenModule &CGM,
1040 const llvm::PointerType *GuardPtrTy) {
1041 // int __cxa_guard_acquire(__guard *guard_object);
John McCall5cd91b52010-09-08 01:44:27 +00001042 const llvm::FunctionType *FTy =
1043 llvm::FunctionType::get(CGM.getTypes().ConvertType(CGM.getContext().IntTy),
Benjamin Kramer95d318c2011-05-28 14:26:31 +00001044 GuardPtrTy, /*isVarArg=*/false);
John McCall5cd91b52010-09-08 01:44:27 +00001045
1046 return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire");
1047}
1048
1049static llvm::Constant *getGuardReleaseFn(CodeGenModule &CGM,
1050 const llvm::PointerType *GuardPtrTy) {
1051 // void __cxa_guard_release(__guard *guard_object);
John McCall5cd91b52010-09-08 01:44:27 +00001052 const llvm::FunctionType *FTy =
1053 llvm::FunctionType::get(llvm::Type::getVoidTy(CGM.getLLVMContext()),
Benjamin Kramer95d318c2011-05-28 14:26:31 +00001054 GuardPtrTy, /*isVarArg=*/false);
John McCall5cd91b52010-09-08 01:44:27 +00001055
1056 return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release");
1057}
1058
1059static llvm::Constant *getGuardAbortFn(CodeGenModule &CGM,
1060 const llvm::PointerType *GuardPtrTy) {
1061 // void __cxa_guard_abort(__guard *guard_object);
John McCall5cd91b52010-09-08 01:44:27 +00001062 const llvm::FunctionType *FTy =
1063 llvm::FunctionType::get(llvm::Type::getVoidTy(CGM.getLLVMContext()),
Benjamin Kramer95d318c2011-05-28 14:26:31 +00001064 GuardPtrTy, /*isVarArg=*/false);
John McCall5cd91b52010-09-08 01:44:27 +00001065
1066 return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort");
1067}
1068
1069namespace {
1070 struct CallGuardAbort : EHScopeStack::Cleanup {
1071 llvm::GlobalVariable *Guard;
1072 CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {}
1073
1074 void Emit(CodeGenFunction &CGF, bool IsForEH) {
1075 CGF.Builder.CreateCall(getGuardAbortFn(CGF.CGM, Guard->getType()), Guard)
1076 ->setDoesNotThrow();
1077 }
1078 };
1079}
1080
1081/// The ARM code here follows the Itanium code closely enough that we
1082/// just special-case it at particular places.
John McCall3030eb82010-11-06 09:44:32 +00001083void ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF,
1084 const VarDecl &D,
1085 llvm::GlobalVariable *GV) {
John McCall5cd91b52010-09-08 01:44:27 +00001086 CGBuilderTy &Builder = CGF.Builder;
John McCall3030eb82010-11-06 09:44:32 +00001087
1088 // We only need to use thread-safe statics for local variables;
1089 // global initialization is always single-threaded.
John McCall0502a222011-06-17 07:33:57 +00001090 bool threadsafe =
1091 (getContext().getLangOptions().ThreadsafeStatics && D.isLocalVarDecl());
Anders Carlsson173d5122011-04-27 04:37:08 +00001092
1093 const llvm::IntegerType *GuardTy;
1094
1095 // If we have a global variable with internal linkage and thread-safe statics
1096 // are disabled, we can just let the guard variable be of type i8.
John McCall0502a222011-06-17 07:33:57 +00001097 bool useInt8GuardVariable = !threadsafe && GV->hasInternalLinkage();
1098 if (useInt8GuardVariable) {
1099 GuardTy = CGF.Int8Ty;
1100 } else {
Anders Carlsson173d5122011-04-27 04:37:08 +00001101 // Guard variables are 64 bits in the generic ABI and 32 bits on ARM.
John McCall0502a222011-06-17 07:33:57 +00001102 GuardTy = (IsARM ? CGF.Int32Ty : CGF.Int64Ty);
Anders Carlsson173d5122011-04-27 04:37:08 +00001103 }
John McCall5cd91b52010-09-08 01:44:27 +00001104 const llvm::PointerType *GuardPtrTy = GuardTy->getPointerTo();
1105
1106 // Create the guard variable.
1107 llvm::SmallString<256> GuardVName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +00001108 llvm::raw_svector_ostream Out(GuardVName);
1109 getMangleContext().mangleItaniumGuardVariable(&D, Out);
1110 Out.flush();
John McCall112c9672010-11-02 21:04:24 +00001111
John McCall3030eb82010-11-06 09:44:32 +00001112 // Just absorb linkage and visibility from the variable.
John McCall5cd91b52010-09-08 01:44:27 +00001113 llvm::GlobalVariable *GuardVariable =
1114 new llvm::GlobalVariable(CGM.getModule(), GuardTy,
John McCall3030eb82010-11-06 09:44:32 +00001115 false, GV->getLinkage(),
John McCall5cd91b52010-09-08 01:44:27 +00001116 llvm::ConstantInt::get(GuardTy, 0),
1117 GuardVName.str());
John McCall112c9672010-11-02 21:04:24 +00001118 GuardVariable->setVisibility(GV->getVisibility());
John McCall5cd91b52010-09-08 01:44:27 +00001119
1120 // Test whether the variable has completed initialization.
1121 llvm::Value *IsInitialized;
1122
1123 // ARM C++ ABI 3.2.3.1:
1124 // To support the potential use of initialization guard variables
1125 // as semaphores that are the target of ARM SWP and LDREX/STREX
1126 // synchronizing instructions we define a static initialization
1127 // guard variable to be a 4-byte aligned, 4- byte word with the
1128 // following inline access protocol.
1129 // #define INITIALIZED 1
1130 // if ((obj_guard & INITIALIZED) != INITIALIZED) {
1131 // if (__cxa_guard_acquire(&obj_guard))
1132 // ...
1133 // }
John McCall0502a222011-06-17 07:33:57 +00001134 if (IsARM && !useInt8GuardVariable) {
John McCall5cd91b52010-09-08 01:44:27 +00001135 llvm::Value *V = Builder.CreateLoad(GuardVariable);
1136 V = Builder.CreateAnd(V, Builder.getInt32(1));
1137 IsInitialized = Builder.CreateIsNull(V, "guard.uninitialized");
1138
1139 // Itanium C++ ABI 3.3.2:
1140 // The following is pseudo-code showing how these functions can be used:
1141 // if (obj_guard.first_byte == 0) {
1142 // if ( __cxa_guard_acquire (&obj_guard) ) {
1143 // try {
1144 // ... initialize the object ...;
1145 // } catch (...) {
1146 // __cxa_guard_abort (&obj_guard);
1147 // throw;
1148 // }
1149 // ... queue object destructor with __cxa_atexit() ...;
1150 // __cxa_guard_release (&obj_guard);
1151 // }
1152 // }
1153 } else {
1154 // Load the first byte of the guard variable.
1155 const llvm::Type *PtrTy = Builder.getInt8PtrTy();
1156 llvm::Value *V =
1157 Builder.CreateLoad(Builder.CreateBitCast(GuardVariable, PtrTy), "tmp");
1158
1159 IsInitialized = Builder.CreateIsNull(V, "guard.uninitialized");
1160 }
1161
1162 llvm::BasicBlock *InitCheckBlock = CGF.createBasicBlock("init.check");
1163 llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end");
1164
John McCall0502a222011-06-17 07:33:57 +00001165 llvm::BasicBlock *NoCheckBlock = EndBlock;
1166 if (threadsafe) NoCheckBlock = CGF.createBasicBlock("init.barrier");
1167
John McCall5cd91b52010-09-08 01:44:27 +00001168 // Check if the first byte of the guard variable is zero.
John McCall0502a222011-06-17 07:33:57 +00001169 Builder.CreateCondBr(IsInitialized, InitCheckBlock, NoCheckBlock);
John McCall5cd91b52010-09-08 01:44:27 +00001170
1171 CGF.EmitBlock(InitCheckBlock);
1172
1173 // Variables used when coping with thread-safe statics and exceptions.
John McCall0502a222011-06-17 07:33:57 +00001174 if (threadsafe) {
John McCall5cd91b52010-09-08 01:44:27 +00001175 // Call __cxa_guard_acquire.
1176 llvm::Value *V
1177 = Builder.CreateCall(getGuardAcquireFn(CGM, GuardPtrTy), GuardVariable);
1178
1179 llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init");
1180
1181 Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"),
1182 InitBlock, EndBlock);
1183
1184 // Call __cxa_guard_abort along the exceptional edge.
1185 CGF.EHStack.pushCleanup<CallGuardAbort>(EHCleanup, GuardVariable);
1186
1187 CGF.EmitBlock(InitBlock);
1188 }
1189
1190 // Emit the initializer and add a global destructor if appropriate.
1191 CGF.EmitCXXGlobalVarDeclInit(D, GV);
1192
John McCall0502a222011-06-17 07:33:57 +00001193 if (threadsafe) {
John McCall5cd91b52010-09-08 01:44:27 +00001194 // Pop the guard-abort cleanup if we pushed one.
1195 CGF.PopCleanupBlock();
1196
1197 // Call __cxa_guard_release. This cannot throw.
1198 Builder.CreateCall(getGuardReleaseFn(CGM, GuardPtrTy), GuardVariable);
1199 } else {
1200 Builder.CreateStore(llvm::ConstantInt::get(GuardTy, 1), GuardVariable);
1201 }
1202
John McCall0502a222011-06-17 07:33:57 +00001203 // Emit an acquire memory barrier if using thread-safe statics:
1204 // Itanium ABI:
1205 // An implementation supporting thread-safety on multiprocessor
1206 // systems must also guarantee that references to the initialized
1207 // object do not occur before the load of the initialization flag.
1208 if (threadsafe) {
1209 Builder.CreateBr(EndBlock);
1210 CGF.EmitBlock(NoCheckBlock);
1211
1212 llvm::Value *_false = Builder.getFalse();
1213 llvm::Value *_true = Builder.getTrue();
1214
1215 Builder.CreateCall5(CGM.getIntrinsic(llvm::Intrinsic::memory_barrier),
1216 /* load-load, load-store */ _true, _true,
1217 /* store-load, store-store */ _false, _false,
1218 /* device or I/O */ _false);
1219 }
1220
John McCall5cd91b52010-09-08 01:44:27 +00001221 CGF.EmitBlock(EndBlock);
1222}