ItaniumCXXABI.cpp revision f3bbb155beb69cdad1c6b0472bc0ca20cece6c50
13a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis//===------- ItaniumCXXABI.cpp - Emit LLVM Code from ASTs for a Module ----===// 23a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// 33a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// The LLVM Compiler Infrastructure 43a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// 53a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// This file is distributed under the University of Illinois Open Source 63a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// License. See LICENSE.TXT for details. 73a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// 83a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis//===----------------------------------------------------------------------===// 93a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// 10fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner// This provides C++ code generation targeting the Itanium C++ ABI. The class 113a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// in this file generates structures that follow the Itanium C++ ABI, which is 123a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// documented at: 133a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// http://www.codesourcery.com/public/cxx-abi/abi.html 143a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// http://www.codesourcery.com/public/cxx-abi/abi-eh.html 15ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall// 16ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall// It also supports the closely-related ARM ABI, documented at: 17ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall// http://infocenter.arm.com/help/topic/com.arm.doc.ihi0041c/IHI0041C_cppabi.pdf 18ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall// 193a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis//===----------------------------------------------------------------------===// 203a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis 213a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis#include "CGCXXABI.h" 220bab0cdab751248ca389a5592bcb70eac5d39260John McCall#include "CGRecordLayout.h" 239ee494f98610dcd79441dce469d7bf609fcd7b92Charles Davis#include "CGVTables.h" 2493d557bc1867b7d7b102f87290194b4be7932c92John McCall#include "CodeGenFunction.h" 253a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis#include "CodeGenModule.h" 26ba77cb9c82cc9d498e4d6474d128007249f07871Craig Topper#include "clang/AST/Mangle.h" 27ba77cb9c82cc9d498e4d6474d128007249f07871Craig Topper#include "clang/AST/Type.h" 283b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/DataLayout.h" 293b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/Intrinsics.h" 303b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/Value.h" 313a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis 323a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davisusing namespace clang; 3393d557bc1867b7d7b102f87290194b4be7932c92John McCallusing namespace CodeGen; 343a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis 353a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davisnamespace { 36071cc7deffad608165b1ddd5263e8bf181861520Charles Davisclass ItaniumCXXABI : public CodeGen::CGCXXABI { 370bab0cdab751248ca389a5592bcb70eac5d39260John McCallprivate: 389cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::IntegerType *PtrDiffTy; 3993d557bc1867b7d7b102f87290194b4be7932c92John McCallprotected: 40babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall bool IsARM; 410bab0cdab751248ca389a5592bcb70eac5d39260John McCall 420bab0cdab751248ca389a5592bcb70eac5d39260John McCall // It's a little silly for us to cache this. 439cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::IntegerType *getPtrDiffTy() { 440bab0cdab751248ca389a5592bcb70eac5d39260John McCall if (!PtrDiffTy) { 459cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall QualType T = getContext().getPointerDiffType(); 469cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::Type *Ty = CGM.getTypes().ConvertType(T); 470bab0cdab751248ca389a5592bcb70eac5d39260John McCall PtrDiffTy = cast<llvm::IntegerType>(Ty); 480bab0cdab751248ca389a5592bcb70eac5d39260John McCall } 490bab0cdab751248ca389a5592bcb70eac5d39260John McCall return PtrDiffTy; 500bab0cdab751248ca389a5592bcb70eac5d39260John McCall } 510bab0cdab751248ca389a5592bcb70eac5d39260John McCall 523a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davispublic: 53babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall ItaniumCXXABI(CodeGen::CodeGenModule &CGM, bool IsARM = false) : 5414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne CGCXXABI(CGM), PtrDiffTy(0), IsARM(IsARM) { } 5593d557bc1867b7d7b102f87290194b4be7932c92John McCall 56f16aa103d3afd42fbca2ab346f191bf745cec092John McCall bool isZeroInitializable(const MemberPointerType *MPT); 57cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall 589cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT); 590bab0cdab751248ca389a5592bcb70eac5d39260John McCall 6093d557bc1867b7d7b102f87290194b4be7932c92John McCall llvm::Value *EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, 6193d557bc1867b7d7b102f87290194b4be7932c92John McCall llvm::Value *&This, 6293d557bc1867b7d7b102f87290194b4be7932c92John McCall llvm::Value *MemFnPtr, 6393d557bc1867b7d7b102f87290194b4be7932c92John McCall const MemberPointerType *MPT); 643023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall 656c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF, 666c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall llvm::Value *Base, 676c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall llvm::Value *MemPtr, 686c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall const MemberPointerType *MPT); 696c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall 700bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF, 710bab0cdab751248ca389a5592bcb70eac5d39260John McCall const CastExpr *E, 720bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Value *Src); 734d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall llvm::Constant *EmitMemberPointerConversion(const CastExpr *E, 744d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall llvm::Constant *Src); 75cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall 760bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT); 77cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall 78755d8497e39071aa24acc173ff07083e3256b8f8John McCall llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD); 795808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT, 805808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall CharUnits offset); 812d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT); 822d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith llvm::Constant *BuildMemberPointer(const CXXMethodDecl *MD, 832d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith CharUnits ThisAdjustment); 84875ab10245d3bf37252dd822aa1616bb0a391095John McCall 850bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF, 860bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Value *L, 870bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Value *R, 880bab0cdab751248ca389a5592bcb70eac5d39260John McCall const MemberPointerType *MPT, 890bab0cdab751248ca389a5592bcb70eac5d39260John McCall bool Inequality); 90e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 910bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF, 920bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Value *Addr, 930bab0cdab751248ca389a5592bcb70eac5d39260John McCall const MemberPointerType *MPT); 944c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 95ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF, 96ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall llvm::Value *ptr, 97ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall QualType type); 98ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall 994c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall void BuildConstructorSignature(const CXXConstructorDecl *Ctor, 1004c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CXXCtorType T, 1014c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CanQualType &ResTy, 1025f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVectorImpl<CanQualType> &ArgTys); 1034c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 1044c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall void BuildDestructorSignature(const CXXDestructorDecl *Dtor, 1054c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CXXDtorType T, 1064c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CanQualType &ResTy, 1075f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVectorImpl<CanQualType> &ArgTys); 1084c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 1094c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall void BuildInstanceFunctionParams(CodeGenFunction &CGF, 1104c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall QualType &ResTy, 1114c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall FunctionArgList &Params); 1124c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 1134c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall void EmitInstanceFunctionProlog(CodeGenFunction &CGF); 1141e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 115285baac67d722beb6854f5faa45ee1aa62a7ce92Joao Matos StringRef GetPureVirtualCallName() { return "__cxa_pure_virtual"; } 1162eb9a959d24ad757a82ecab61f343635ad67749aDavid Blaikie StringRef GetDeletedVirtualCallName() { return "__cxa_deleted_virtual"; } 117285baac67d722beb6854f5faa45ee1aa62a7ce92Joao Matos 118e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall CharUnits getArrayCookieSizeImpl(QualType elementType); 1191e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF, 1201e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall llvm::Value *NewPtr, 1211e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall llvm::Value *NumElements, 1226ec278d1a354517e20f13a877481453ee7940c78John McCall const CXXNewExpr *expr, 1231e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall QualType ElementType); 124e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, 125e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall llvm::Value *allocPtr, 126e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall CharUnits cookieSize); 1275cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 1283030eb82593097502469a8b3fc26112c79c75605John McCall void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, 1290f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth llvm::GlobalVariable *DeclPtr, bool PerformInit); 13020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall void registerGlobalDtor(CodeGenFunction &CGF, llvm::Constant *dtor, 13120bb175cb8ae5844034828db094fb948c0e3454aJohn McCall llvm::Constant *addr); 1323a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis}; 133ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall 134ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCallclass ARMCXXABI : public ItaniumCXXABI { 135ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCallpublic: 136babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall ARMCXXABI(CodeGen::CodeGenModule &CGM) : ItaniumCXXABI(CGM, /*ARM*/ true) {} 1374c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 1384c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall void BuildConstructorSignature(const CXXConstructorDecl *Ctor, 1394c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CXXCtorType T, 1404c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CanQualType &ResTy, 1415f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVectorImpl<CanQualType> &ArgTys); 1424c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 1434c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall void BuildDestructorSignature(const CXXDestructorDecl *Dtor, 1444c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CXXDtorType T, 1454c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CanQualType &ResTy, 1465f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVectorImpl<CanQualType> &ArgTys); 1474c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 1484c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall void BuildInstanceFunctionParams(CodeGenFunction &CGF, 1494c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall QualType &ResTy, 1504c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall FunctionArgList &Params); 1514c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 1524c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall void EmitInstanceFunctionProlog(CodeGenFunction &CGF); 1534c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 1544c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV, QualType ResTy); 1554c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 156e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall CharUnits getArrayCookieSizeImpl(QualType elementType); 1571e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF, 1581e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall llvm::Value *NewPtr, 1591e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall llvm::Value *NumElements, 1606ec278d1a354517e20f13a877481453ee7940c78John McCall const CXXNewExpr *expr, 1611e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall QualType ElementType); 162e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, llvm::Value *allocPtr, 163e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall CharUnits cookieSize); 1644c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 1654c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallprivate: 1664c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall /// \brief Returns true if the given instance method is one of the 1674c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall /// kinds that the ARM ABI says returns 'this'. 1684c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall static bool HasThisReturn(GlobalDecl GD) { 1694c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 1704c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return ((isa<CXXDestructorDecl>(MD) && GD.getDtorType() != Dtor_Deleting) || 1714c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall (isa<CXXConstructorDecl>(MD))); 1724c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall } 173ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall}; 1743a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis} 1753a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis 176071cc7deffad608165b1ddd5263e8bf181861520Charles DavisCodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) { 17796fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall switch (CGM.getContext().getTargetInfo().getCXXABI().getKind()) { 17896fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall // For IR-generation purposes, there's no significant difference 17996fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall // between the ARM and iOS ABIs. 18096fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall case TargetCXXABI::GenericARM: 18196fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall case TargetCXXABI::iOS: 18296fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall return new ARMCXXABI(CGM); 18396fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall 18496fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall case TargetCXXABI::GenericItanium: 18596fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall return new ItaniumCXXABI(CGM); 18696fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall 18796fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall case TargetCXXABI::Microsoft: 18896fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall llvm_unreachable("Microsoft ABI is not Itanium-based"); 18996fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall } 19096fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall llvm_unreachable("bad ABI kind"); 191ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall} 192ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall 1939cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type * 1940bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) { 1950bab0cdab751248ca389a5592bcb70eac5d39260John McCall if (MPT->isMemberDataPointer()) 1960bab0cdab751248ca389a5592bcb70eac5d39260John McCall return getPtrDiffTy(); 1977650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner return llvm::StructType::get(getPtrDiffTy(), getPtrDiffTy(), NULL); 198875ab10245d3bf37252dd822aa1616bb0a391095John McCall} 199875ab10245d3bf37252dd822aa1616bb0a391095John McCall 200babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// In the Itanium and ARM ABIs, method pointers have the form: 201babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr; 202babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// 203babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// In the Itanium ABI: 204babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// - method pointers are virtual if (memptr.ptr & 1) is nonzero 205babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// - the this-adjustment is (memptr.adj) 206babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// - the virtual offset is (memptr.ptr - 1) 207babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// 208babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// In the ARM ABI: 209babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// - method pointers are virtual if (memptr.adj & 1) is nonzero 210babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// - the this-adjustment is (memptr.adj >> 1) 211babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// - the virtual offset is (memptr.ptr) 212babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// ARM uses 'adj' for the virtual flag because Thumb functions 213babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// may be only single-byte aligned. 214babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// 215babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// If the member is virtual, the adjusted 'this' pointer points 216babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// to a vtable pointer from which the virtual offset is applied. 217babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// 218babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// If the member is non-virtual, memptr.ptr is the address of 219babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// the function to call. 22093d557bc1867b7d7b102f87290194b4be7932c92John McCallllvm::Value * 22193d557bc1867b7d7b102f87290194b4be7932c92John McCallItaniumCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, 22293d557bc1867b7d7b102f87290194b4be7932c92John McCall llvm::Value *&This, 22393d557bc1867b7d7b102f87290194b4be7932c92John McCall llvm::Value *MemFnPtr, 22493d557bc1867b7d7b102f87290194b4be7932c92John McCall const MemberPointerType *MPT) { 22593d557bc1867b7d7b102f87290194b4be7932c92John McCall CGBuilderTy &Builder = CGF.Builder; 22693d557bc1867b7d7b102f87290194b4be7932c92John McCall 22793d557bc1867b7d7b102f87290194b4be7932c92John McCall const FunctionProtoType *FPT = 22893d557bc1867b7d7b102f87290194b4be7932c92John McCall MPT->getPointeeType()->getAs<FunctionProtoType>(); 22993d557bc1867b7d7b102f87290194b4be7932c92John McCall const CXXRecordDecl *RD = 23093d557bc1867b7d7b102f87290194b4be7932c92John McCall cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl()); 23193d557bc1867b7d7b102f87290194b4be7932c92John McCall 2322acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *FTy = 233de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall CGM.getTypes().GetFunctionType( 234de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall CGM.getTypes().arrangeCXXMethodType(RD, FPT)); 23593d557bc1867b7d7b102f87290194b4be7932c92John McCall 2362acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::IntegerType *ptrdiff = getPtrDiffTy(); 237babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(ptrdiff, 1); 23893d557bc1867b7d7b102f87290194b4be7932c92John McCall 239babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual"); 240babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual"); 241babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end"); 242babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall 243d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // Extract memptr.adj, which is in the second field. 244d608cdb7c044365cf4e8764ade1e11e99c176078John McCall llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj"); 245babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall 246babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall // Compute the true adjustment. 247babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall llvm::Value *Adj = RawAdj; 248babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall if (IsARM) 249babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted"); 25093d557bc1867b7d7b102f87290194b4be7932c92John McCall 25193d557bc1867b7d7b102f87290194b4be7932c92John McCall // Apply the adjustment and cast back to the original struct type 25293d557bc1867b7d7b102f87290194b4be7932c92John McCall // for consistency. 253babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy()); 254babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall Ptr = Builder.CreateInBoundsGEP(Ptr, Adj); 255babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted"); 25693d557bc1867b7d7b102f87290194b4be7932c92John McCall 25793d557bc1867b7d7b102f87290194b4be7932c92John McCall // Load the function pointer. 258d608cdb7c044365cf4e8764ade1e11e99c176078John McCall llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr"); 25993d557bc1867b7d7b102f87290194b4be7932c92John McCall 26093d557bc1867b7d7b102f87290194b4be7932c92John McCall // If the LSB in the function pointer is 1, the function pointer points to 26193d557bc1867b7d7b102f87290194b4be7932c92John McCall // a virtual function. 262babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall llvm::Value *IsVirtual; 263babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall if (IsARM) 264babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1); 265babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall else 266babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1); 267babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual"); 26893d557bc1867b7d7b102f87290194b4be7932c92John McCall Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual); 26993d557bc1867b7d7b102f87290194b4be7932c92John McCall 27093d557bc1867b7d7b102f87290194b4be7932c92John McCall // In the virtual path, the adjustment left 'This' pointing to the 27193d557bc1867b7d7b102f87290194b4be7932c92John McCall // vtable of the correct base subobject. The "function pointer" is an 272babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall // offset within the vtable (+1 for the virtual flag on non-ARM). 27393d557bc1867b7d7b102f87290194b4be7932c92John McCall CGF.EmitBlock(FnVirtual); 27493d557bc1867b7d7b102f87290194b4be7932c92John McCall 27593d557bc1867b7d7b102f87290194b4be7932c92John McCall // Cast the adjusted this to a pointer to vtable pointer and load. 2762acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *VTableTy = Builder.getInt8PtrTy(); 27793d557bc1867b7d7b102f87290194b4be7932c92John McCall llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy->getPointerTo()); 278babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall VTable = Builder.CreateLoad(VTable, "memptr.vtable"); 27993d557bc1867b7d7b102f87290194b4be7932c92John McCall 28093d557bc1867b7d7b102f87290194b4be7932c92John McCall // Apply the offset. 281babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall llvm::Value *VTableOffset = FnAsInt; 282babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall if (!IsARM) VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1); 283babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall VTable = Builder.CreateGEP(VTable, VTableOffset); 28493d557bc1867b7d7b102f87290194b4be7932c92John McCall 28593d557bc1867b7d7b102f87290194b4be7932c92John McCall // Load the virtual function to call. 28693d557bc1867b7d7b102f87290194b4be7932c92John McCall VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo()); 287babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn"); 28893d557bc1867b7d7b102f87290194b4be7932c92John McCall CGF.EmitBranch(FnEnd); 28993d557bc1867b7d7b102f87290194b4be7932c92John McCall 29093d557bc1867b7d7b102f87290194b4be7932c92John McCall // In the non-virtual path, the function pointer is actually a 29193d557bc1867b7d7b102f87290194b4be7932c92John McCall // function pointer. 29293d557bc1867b7d7b102f87290194b4be7932c92John McCall CGF.EmitBlock(FnNonVirtual); 29393d557bc1867b7d7b102f87290194b4be7932c92John McCall llvm::Value *NonVirtualFn = 294babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn"); 29593d557bc1867b7d7b102f87290194b4be7932c92John McCall 29693d557bc1867b7d7b102f87290194b4be7932c92John McCall // We're done. 29793d557bc1867b7d7b102f87290194b4be7932c92John McCall CGF.EmitBlock(FnEnd); 298bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo(), 2); 29993d557bc1867b7d7b102f87290194b4be7932c92John McCall Callee->addIncoming(VirtualFn, FnVirtual); 30093d557bc1867b7d7b102f87290194b4be7932c92John McCall Callee->addIncoming(NonVirtualFn, FnNonVirtual); 30193d557bc1867b7d7b102f87290194b4be7932c92John McCall return Callee; 30293d557bc1867b7d7b102f87290194b4be7932c92John McCall} 3033023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall 3046c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall/// Compute an l-value by applying the given pointer-to-member to a 3056c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall/// base object. 3066c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCallllvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF, 3076c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall llvm::Value *Base, 3086c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall llvm::Value *MemPtr, 3096c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall const MemberPointerType *MPT) { 3106c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall assert(MemPtr->getType() == getPtrDiffTy()); 3116c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall 3126c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall CGBuilderTy &Builder = CGF.Builder; 3136c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall 314956a5a17713deb1b5b27893303c4f308a1bd2a62Micah Villmow unsigned AS = Base->getType()->getPointerAddressSpace(); 3156c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall 3166c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall // Cast to char*. 3176c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS)); 3186c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall 3196c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall // Apply the offset, which we assume is non-null. 3206c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall llvm::Value *Addr = Builder.CreateInBoundsGEP(Base, MemPtr, "memptr.offset"); 3216c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall 3226c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall // Cast the address to the appropriate pointer type, adopting the 3236c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall // address space of the base pointer. 3242acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *PType 325eede61a83e90f3cb03ef8665b67d648dccd6ce42Douglas Gregor = CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS); 3266c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall return Builder.CreateBitCast(Addr, PType); 3276c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall} 3286c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall 3294d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall/// Perform a bitcast, derived-to-base, or base-to-derived member pointer 3304d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall/// conversion. 3314d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall/// 3324d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall/// Bitcast conversions are always a no-op under Itanium. 3330bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// 3340bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// Obligatory offset/adjustment diagram: 3350bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// <-- offset --> <-- adjustment --> 3360bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// |--------------------------|----------------------|--------------------| 3370bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// ^Derived address point ^Base address point ^Member address point 3380bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// 3390bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// So when converting a base member pointer to a derived member pointer, 3400bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// we add the offset to the adjustment because the address point has 3410bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// decreased; and conversely, when converting a derived MP to a base MP 3420bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// we subtract the offset from the adjustment because the address point 3430bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// has increased. 3440bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// 3450bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// The standard forbids (at compile time) conversion to and from 3460bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// virtual bases, which is why we don't have to consider them here. 3470bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// 3480bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// The standard forbids (at run time) casting a derived MP to a base 3490bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// MP when the derived MP does not point to a member of the base. 3500bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// This is why -1 is a reasonable choice for null data member 3510bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// pointers. 352d608cdb7c044365cf4e8764ade1e11e99c176078John McCallllvm::Value * 3530bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF, 3540bab0cdab751248ca389a5592bcb70eac5d39260John McCall const CastExpr *E, 3554d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall llvm::Value *src) { 3562de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall assert(E->getCastKind() == CK_DerivedToBaseMemberPointer || 3574d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall E->getCastKind() == CK_BaseToDerivedMemberPointer || 3584d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall E->getCastKind() == CK_ReinterpretMemberPointer); 3594d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall 3604d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // Under Itanium, reinterprets don't require any additional processing. 3614d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (E->getCastKind() == CK_ReinterpretMemberPointer) return src; 3624d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall 3634d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // Use constant emission if we can. 3644d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (isa<llvm::Constant>(src)) 3654d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall return EmitMemberPointerConversion(E, cast<llvm::Constant>(src)); 3664d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall 3674d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall llvm::Constant *adj = getMemberPointerAdjustment(E); 3684d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (!adj) return src; 3693023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall 3703023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall CGBuilderTy &Builder = CGF.Builder; 3714d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer); 3724d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall 3734d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall const MemberPointerType *destTy = 3744d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall E->getType()->castAs<MemberPointerType>(); 3753023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall 3764d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // For member data pointers, this is just a matter of adding the 3774d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // offset if the source is non-null. 3784d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (destTy->isMemberDataPointer()) { 3794d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall llvm::Value *dst; 3804d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (isDerivedToBase) 3814d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall dst = Builder.CreateNSWSub(src, adj, "adj"); 3824d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall else 3834d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall dst = Builder.CreateNSWAdd(src, adj, "adj"); 3843023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall 3854d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // Null check. 3864d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall llvm::Value *null = llvm::Constant::getAllOnesValue(src->getType()); 3874d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall llvm::Value *isNull = Builder.CreateICmpEQ(src, null, "memptr.isnull"); 3884d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall return Builder.CreateSelect(isNull, src, dst); 3894d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall } 3903023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall 3914d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // The this-adjustment is left-shifted by 1 on ARM. 3924d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (IsARM) { 3934d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue(); 3944d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall offset <<= 1; 3954d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall adj = llvm::ConstantInt::get(adj->getType(), offset); 3964d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall } 3973023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall 3984d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall llvm::Value *srcAdj = Builder.CreateExtractValue(src, 1, "src.adj"); 3994d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall llvm::Value *dstAdj; 4004d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (isDerivedToBase) 4014d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall dstAdj = Builder.CreateNSWSub(srcAdj, adj, "adj"); 4023023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall else 4034d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall dstAdj = Builder.CreateNSWAdd(srcAdj, adj, "adj"); 4044d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall 4054d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall return Builder.CreateInsertValue(src, dstAdj, 1); 4064d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall} 4074d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall 4084d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCallllvm::Constant * 4094d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCallItaniumCXXABI::EmitMemberPointerConversion(const CastExpr *E, 4104d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall llvm::Constant *src) { 4114d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall assert(E->getCastKind() == CK_DerivedToBaseMemberPointer || 4124d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall E->getCastKind() == CK_BaseToDerivedMemberPointer || 4134d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall E->getCastKind() == CK_ReinterpretMemberPointer); 4143023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall 4154d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // Under Itanium, reinterprets don't require any additional processing. 4164d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (E->getCastKind() == CK_ReinterpretMemberPointer) return src; 4174d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall 4184d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // If the adjustment is trivial, we don't need to do anything. 4194d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall llvm::Constant *adj = getMemberPointerAdjustment(E); 4204d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (!adj) return src; 4214d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall 4224d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer); 4234d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall 4244d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall const MemberPointerType *destTy = 4254d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall E->getType()->castAs<MemberPointerType>(); 426875ab10245d3bf37252dd822aa1616bb0a391095John McCall 4270bab0cdab751248ca389a5592bcb70eac5d39260John McCall // For member data pointers, this is just a matter of adding the 4280bab0cdab751248ca389a5592bcb70eac5d39260John McCall // offset if the source is non-null. 4294d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (destTy->isMemberDataPointer()) { 4304d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // null maps to null. 4314d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (src->isAllOnesValue()) return src; 4320bab0cdab751248ca389a5592bcb70eac5d39260John McCall 4334d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (isDerivedToBase) 4344d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall return llvm::ConstantExpr::getNSWSub(src, adj); 4354d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall else 4364d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall return llvm::ConstantExpr::getNSWAdd(src, adj); 4370bab0cdab751248ca389a5592bcb70eac5d39260John McCall } 4380bab0cdab751248ca389a5592bcb70eac5d39260John McCall 439d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // The this-adjustment is left-shifted by 1 on ARM. 440d608cdb7c044365cf4e8764ade1e11e99c176078John McCall if (IsARM) { 4414d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue(); 4424d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall offset <<= 1; 4434d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall adj = llvm::ConstantInt::get(adj->getType(), offset); 444d608cdb7c044365cf4e8764ade1e11e99c176078John McCall } 445d608cdb7c044365cf4e8764ade1e11e99c176078John McCall 4464d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall llvm::Constant *srcAdj = llvm::ConstantExpr::getExtractValue(src, 1); 4474d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall llvm::Constant *dstAdj; 4484d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (isDerivedToBase) 4494d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall dstAdj = llvm::ConstantExpr::getNSWSub(srcAdj, adj); 450d608cdb7c044365cf4e8764ade1e11e99c176078John McCall else 4514d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall dstAdj = llvm::ConstantExpr::getNSWAdd(srcAdj, adj); 452d608cdb7c044365cf4e8764ade1e11e99c176078John McCall 4534d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall return llvm::ConstantExpr::getInsertValue(src, dstAdj, 1); 4543023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall} 455cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall 456cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCallllvm::Constant * 4570bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) { 4582acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *ptrdiff_t = getPtrDiffTy(); 4590bab0cdab751248ca389a5592bcb70eac5d39260John McCall 4600bab0cdab751248ca389a5592bcb70eac5d39260John McCall // Itanium C++ ABI 2.3: 4610bab0cdab751248ca389a5592bcb70eac5d39260John McCall // A NULL pointer is represented as -1. 4620bab0cdab751248ca389a5592bcb70eac5d39260John McCall if (MPT->isMemberDataPointer()) 4630bab0cdab751248ca389a5592bcb70eac5d39260John McCall return llvm::ConstantInt::get(ptrdiff_t, -1ULL, /*isSigned=*/true); 464d608cdb7c044365cf4e8764ade1e11e99c176078John McCall 465d608cdb7c044365cf4e8764ade1e11e99c176078John McCall llvm::Constant *Zero = llvm::ConstantInt::get(ptrdiff_t, 0); 466d608cdb7c044365cf4e8764ade1e11e99c176078John McCall llvm::Constant *Values[2] = { Zero, Zero }; 467c5cbb909e8a27deb8f1a2b6b7bf56a96051af81aChris Lattner return llvm::ConstantStruct::getAnon(Values); 468cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall} 469cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall 4705808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCallllvm::Constant * 4715808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCallItaniumCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT, 4725808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall CharUnits offset) { 4730bab0cdab751248ca389a5592bcb70eac5d39260John McCall // Itanium C++ ABI 2.3: 4740bab0cdab751248ca389a5592bcb70eac5d39260John McCall // A pointer to data member is an offset from the base address of 4750bab0cdab751248ca389a5592bcb70eac5d39260John McCall // the class object containing it, represented as a ptrdiff_t 4765808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall return llvm::ConstantInt::get(getPtrDiffTy(), offset.getQuantity()); 4770bab0cdab751248ca389a5592bcb70eac5d39260John McCall} 4780bab0cdab751248ca389a5592bcb70eac5d39260John McCall 479755d8497e39071aa24acc173ff07083e3256b8f8John McCallllvm::Constant *ItaniumCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) { 4802d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith return BuildMemberPointer(MD, CharUnits::Zero()); 4812d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith} 4822d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith 4832d6a5670465cb3f1d811695a9f23e372508240d2Richard Smithllvm::Constant *ItaniumCXXABI::BuildMemberPointer(const CXXMethodDecl *MD, 4842d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith CharUnits ThisAdjustment) { 485d608cdb7c044365cf4e8764ade1e11e99c176078John McCall assert(MD->isInstance() && "Member function must not be static!"); 486d608cdb7c044365cf4e8764ade1e11e99c176078John McCall MD = MD->getCanonicalDecl(); 487875ab10245d3bf37252dd822aa1616bb0a391095John McCall 488d608cdb7c044365cf4e8764ade1e11e99c176078John McCall CodeGenTypes &Types = CGM.getTypes(); 4892acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *ptrdiff_t = getPtrDiffTy(); 490875ab10245d3bf37252dd822aa1616bb0a391095John McCall 491d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // Get the function pointer (or index if this is a virtual function). 492d608cdb7c044365cf4e8764ade1e11e99c176078John McCall llvm::Constant *MemPtr[2]; 493d608cdb7c044365cf4e8764ade1e11e99c176078John McCall if (MD->isVirtual()) { 4941d2b31710539d705a3850c9fc3aa1804c2a5efeePeter Collingbourne uint64_t Index = CGM.getVTableContext().getMethodVTableIndex(MD); 495875ab10245d3bf37252dd822aa1616bb0a391095John McCall 4961246ba6f6801390ffc0e1d4b83a2b45e72283b8fKen Dyck const ASTContext &Context = getContext(); 4971246ba6f6801390ffc0e1d4b83a2b45e72283b8fKen Dyck CharUnits PointerWidth = 498bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); 4991246ba6f6801390ffc0e1d4b83a2b45e72283b8fKen Dyck uint64_t VTableOffset = (Index * PointerWidth.getQuantity()); 500d608cdb7c044365cf4e8764ade1e11e99c176078John McCall 501d608cdb7c044365cf4e8764ade1e11e99c176078John McCall if (IsARM) { 502d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // ARM C++ ABI 3.2.1: 503d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // This ABI specifies that adj contains twice the this 504d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // adjustment, plus 1 if the member function is virtual. The 505d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // least significant bit of adj then makes exactly the same 506d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // discrimination as the least significant bit of ptr does for 507d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // Itanium. 508d608cdb7c044365cf4e8764ade1e11e99c176078John McCall MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset); 5092d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 5102d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith 2 * ThisAdjustment.getQuantity() + 1); 511d608cdb7c044365cf4e8764ade1e11e99c176078John McCall } else { 512d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // Itanium C++ ABI 2.3: 513d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // For a virtual function, [the pointer field] is 1 plus the 514d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // virtual table offset (in bytes) of the function, 515d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // represented as a ptrdiff_t. 516d608cdb7c044365cf4e8764ade1e11e99c176078John McCall MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset + 1); 5172d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 5182d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith ThisAdjustment.getQuantity()); 519d608cdb7c044365cf4e8764ade1e11e99c176078John McCall } 520d608cdb7c044365cf4e8764ade1e11e99c176078John McCall } else { 521755d8497e39071aa24acc173ff07083e3256b8f8John McCall const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); 5222acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *Ty; 523755d8497e39071aa24acc173ff07083e3256b8f8John McCall // Check whether the function has a computable LLVM signature. 524f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner if (Types.isFuncTypeConvertible(FPT)) { 525755d8497e39071aa24acc173ff07083e3256b8f8John McCall // The function has a computable LLVM signature; use the correct type. 526de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD)); 527d608cdb7c044365cf4e8764ade1e11e99c176078John McCall } else { 528755d8497e39071aa24acc173ff07083e3256b8f8John McCall // Use an arbitrary non-function type to tell GetAddrOfFunction that the 529755d8497e39071aa24acc173ff07083e3256b8f8John McCall // function type is incomplete. 530755d8497e39071aa24acc173ff07083e3256b8f8John McCall Ty = ptrdiff_t; 531d608cdb7c044365cf4e8764ade1e11e99c176078John McCall } 532755d8497e39071aa24acc173ff07083e3256b8f8John McCall llvm::Constant *addr = CGM.GetAddrOfFunction(MD, Ty); 533d608cdb7c044365cf4e8764ade1e11e99c176078John McCall 534379b5155b4566f63679e1da6b0ceb5fdfa2aec6dJohn McCall MemPtr[0] = llvm::ConstantExpr::getPtrToInt(addr, ptrdiff_t); 5352d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, (IsARM ? 2 : 1) * 5362d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith ThisAdjustment.getQuantity()); 537d608cdb7c044365cf4e8764ade1e11e99c176078John McCall } 538d608cdb7c044365cf4e8764ade1e11e99c176078John McCall 539c5cbb909e8a27deb8f1a2b6b7bf56a96051af81aChris Lattner return llvm::ConstantStruct::getAnon(MemPtr); 540875ab10245d3bf37252dd822aa1616bb0a391095John McCall} 541875ab10245d3bf37252dd822aa1616bb0a391095John McCall 5422d6a5670465cb3f1d811695a9f23e372508240d2Richard Smithllvm::Constant *ItaniumCXXABI::EmitMemberPointer(const APValue &MP, 5432d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith QualType MPType) { 5442d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith const MemberPointerType *MPT = MPType->castAs<MemberPointerType>(); 5452d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith const ValueDecl *MPD = MP.getMemberPointerDecl(); 5462d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith if (!MPD) 5472d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith return EmitNullMemberPointer(MPT); 5482d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith 5492d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith // Compute the this-adjustment. 5502d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith CharUnits ThisAdjustment = CharUnits::Zero(); 5512d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith ArrayRef<const CXXRecordDecl*> Path = MP.getMemberPointerPath(); 5522d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith bool DerivedMember = MP.isMemberPointerToDerivedMember(); 5532d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith const CXXRecordDecl *RD = cast<CXXRecordDecl>(MPD->getDeclContext()); 5542d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith for (unsigned I = 0, N = Path.size(); I != N; ++I) { 5552d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith const CXXRecordDecl *Base = RD; 5562d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith const CXXRecordDecl *Derived = Path[I]; 5572d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith if (DerivedMember) 5582d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith std::swap(Base, Derived); 5592d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith ThisAdjustment += 5602d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith getContext().getASTRecordLayout(Derived).getBaseClassOffset(Base); 5612d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith RD = Path[I]; 5622d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith } 5632d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith if (DerivedMember) 5642d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith ThisAdjustment = -ThisAdjustment; 5652d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith 5662d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD)) 5672d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith return BuildMemberPointer(MD, ThisAdjustment); 5682d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith 5692d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith CharUnits FieldOffset = 5702d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD)); 5712d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset); 5722d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith} 5732d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith 574e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall/// The comparison algorithm is pretty easy: the member pointers are 575e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall/// the same if they're either bitwise identical *or* both null. 576e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall/// 577e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall/// ARM is different here only because null-ness is more complicated. 578e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCallllvm::Value * 5790bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF, 5800bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Value *L, 5810bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Value *R, 5820bab0cdab751248ca389a5592bcb70eac5d39260John McCall const MemberPointerType *MPT, 5830bab0cdab751248ca389a5592bcb70eac5d39260John McCall bool Inequality) { 584e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall CGBuilderTy &Builder = CGF.Builder; 585e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 586e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::ICmpInst::Predicate Eq; 587e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::Instruction::BinaryOps And, Or; 588e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall if (Inequality) { 589e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall Eq = llvm::ICmpInst::ICMP_NE; 590e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall And = llvm::Instruction::Or; 591e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall Or = llvm::Instruction::And; 592e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall } else { 593e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall Eq = llvm::ICmpInst::ICMP_EQ; 594e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall And = llvm::Instruction::And; 595e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall Or = llvm::Instruction::Or; 596e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall } 597e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 5980bab0cdab751248ca389a5592bcb70eac5d39260John McCall // Member data pointers are easy because there's a unique null 5990bab0cdab751248ca389a5592bcb70eac5d39260John McCall // value, so it just comes down to bitwise equality. 6000bab0cdab751248ca389a5592bcb70eac5d39260John McCall if (MPT->isMemberDataPointer()) 6010bab0cdab751248ca389a5592bcb70eac5d39260John McCall return Builder.CreateICmp(Eq, L, R); 6020bab0cdab751248ca389a5592bcb70eac5d39260John McCall 6030bab0cdab751248ca389a5592bcb70eac5d39260John McCall // For member function pointers, the tautologies are more complex. 6040bab0cdab751248ca389a5592bcb70eac5d39260John McCall // The Itanium tautology is: 605de719f7131e1ece5c9d6b5ffe21b83286d29f10fJohn McCall // (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj)) 6060bab0cdab751248ca389a5592bcb70eac5d39260John McCall // The ARM tautology is: 607de719f7131e1ece5c9d6b5ffe21b83286d29f10fJohn McCall // (L == R) <==> (L.ptr == R.ptr && 608de719f7131e1ece5c9d6b5ffe21b83286d29f10fJohn McCall // (L.adj == R.adj || 609de719f7131e1ece5c9d6b5ffe21b83286d29f10fJohn McCall // (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0))) 6100bab0cdab751248ca389a5592bcb70eac5d39260John McCall // The inequality tautologies have exactly the same structure, except 6110bab0cdab751248ca389a5592bcb70eac5d39260John McCall // applying De Morgan's laws. 6120bab0cdab751248ca389a5592bcb70eac5d39260John McCall 6130bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr"); 6140bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr"); 6150bab0cdab751248ca389a5592bcb70eac5d39260John McCall 616e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // This condition tests whether L.ptr == R.ptr. This must always be 617e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // true for equality to hold. 618e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr"); 619e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 620e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // This condition, together with the assumption that L.ptr == R.ptr, 621e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // tests whether the pointers are both null. ARM imposes an extra 622e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // condition. 623e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType()); 624e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null"); 625e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 626e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // This condition tests whether L.adj == R.adj. If this isn't 627e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // true, the pointers are unequal unless they're both null. 628d608cdb7c044365cf4e8764ade1e11e99c176078John McCall llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj"); 629d608cdb7c044365cf4e8764ade1e11e99c176078John McCall llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj"); 630e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj"); 631e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 632e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // Null member function pointers on ARM clear the low bit of Adj, 633e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // so the zero condition has to check that neither low bit is set. 634e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall if (IsARM) { 635e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1); 636e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 637e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // Compute (l.adj | r.adj) & 1 and test it against zero. 638e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj"); 639e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One); 640e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero, 641e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall "cmp.or.adj"); 642e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero); 643e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall } 644e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 645e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // Tie together all our conditions. 646e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq); 647e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall Result = Builder.CreateBinOp(And, PtrEq, Result, 648e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall Inequality ? "memptr.ne" : "memptr.eq"); 649e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall return Result; 650e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall} 651e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 652e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCallllvm::Value * 6530bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF, 6540bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Value *MemPtr, 6550bab0cdab751248ca389a5592bcb70eac5d39260John McCall const MemberPointerType *MPT) { 656e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall CGBuilderTy &Builder = CGF.Builder; 6570bab0cdab751248ca389a5592bcb70eac5d39260John McCall 6580bab0cdab751248ca389a5592bcb70eac5d39260John McCall /// For member data pointers, this is just a check against -1. 6590bab0cdab751248ca389a5592bcb70eac5d39260John McCall if (MPT->isMemberDataPointer()) { 6600bab0cdab751248ca389a5592bcb70eac5d39260John McCall assert(MemPtr->getType() == getPtrDiffTy()); 6610bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Value *NegativeOne = 6620bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Constant::getAllOnesValue(MemPtr->getType()); 6630bab0cdab751248ca389a5592bcb70eac5d39260John McCall return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool"); 6640bab0cdab751248ca389a5592bcb70eac5d39260John McCall } 665e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 666db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar // In Itanium, a member function pointer is not null if 'ptr' is not null. 667d608cdb7c044365cf4e8764ade1e11e99c176078John McCall llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr"); 668e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 669e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0); 670e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool"); 671e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 672db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar // On ARM, a member function pointer is also non-null if the low bit of 'adj' 673db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar // (the virtual bit) is set. 674e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall if (IsARM) { 675e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1); 676d608cdb7c044365cf4e8764ade1e11e99c176078John McCall llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj"); 677e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit"); 678db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar llvm::Value *IsVirtual = Builder.CreateICmpNE(VirtualBit, Zero, 679db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar "memptr.isvirtual"); 680db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar Result = Builder.CreateOr(Result, IsVirtual); 681e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall } 682e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 683e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall return Result; 684e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall} 685875ab10245d3bf37252dd822aa1616bb0a391095John McCall 686f16aa103d3afd42fbca2ab346f191bf745cec092John McCall/// The Itanium ABI requires non-zero initialization only for data 687f16aa103d3afd42fbca2ab346f191bf745cec092John McCall/// member pointers, for which '0' is a valid offset. 688f16aa103d3afd42fbca2ab346f191bf745cec092John McCallbool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) { 689f16aa103d3afd42fbca2ab346f191bf745cec092John McCall return MPT->getPointeeType()->isFunctionType(); 690cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall} 6914c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 692ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall/// The Itanium ABI always places an offset to the complete object 693ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall/// at entry -2 in the vtable. 694ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCallllvm::Value *ItaniumCXXABI::adjustToCompleteObject(CodeGenFunction &CGF, 695ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall llvm::Value *ptr, 696ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall QualType type) { 697ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall // Grab the vtable pointer as an intptr_t*. 698ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall llvm::Value *vtable = CGF.GetVTablePtr(ptr, CGF.IntPtrTy->getPointerTo()); 699ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall 700ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall // Track back to entry -2 and pull out the offset there. 701ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall llvm::Value *offsetPtr = 702ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall CGF.Builder.CreateConstInBoundsGEP1_64(vtable, -2, "complete-offset.ptr"); 703ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall llvm::LoadInst *offset = CGF.Builder.CreateLoad(offsetPtr); 704ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall offset->setAlignment(CGF.PointerAlignInBytes); 705ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall 706ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall // Apply the offset. 707ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall ptr = CGF.Builder.CreateBitCast(ptr, CGF.Int8PtrTy); 708ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall return CGF.Builder.CreateInBoundsGEP(ptr, offset); 709ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall} 710ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall 7114c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// The generic ABI passes 'this', plus a VTT if it's initializing a 7124c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// base subobject. 7134c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ItaniumCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor, 7144c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CXXCtorType Type, 7154c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CanQualType &ResTy, 7165f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVectorImpl<CanQualType> &ArgTys) { 7179cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall ASTContext &Context = getContext(); 7184c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7194c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // 'this' is already there. 7204c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7214c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // Check if we need to add a VTT parameter (which has type void **). 7224c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (Type == Ctor_Base && Ctor->getParent()->getNumVBases() != 0) 7234c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy)); 7244c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall} 7254c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7264c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// The ARM ABI does the same as the Itanium ABI, but returns 'this'. 7274c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ARMCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor, 7284c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CXXCtorType Type, 7294c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CanQualType &ResTy, 7305f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVectorImpl<CanQualType> &ArgTys) { 7314c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall ItaniumCXXABI::BuildConstructorSignature(Ctor, Type, ResTy, ArgTys); 7324c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall ResTy = ArgTys[0]; 7334c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall} 7344c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7354c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// The generic ABI passes 'this', plus a VTT if it's destroying a 7364c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// base subobject. 7374c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ItaniumCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor, 7384c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CXXDtorType Type, 7394c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CanQualType &ResTy, 7405f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVectorImpl<CanQualType> &ArgTys) { 7419cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall ASTContext &Context = getContext(); 7424c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7434c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // 'this' is already there. 7444c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7454c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // Check if we need to add a VTT parameter (which has type void **). 7464c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (Type == Dtor_Base && Dtor->getParent()->getNumVBases() != 0) 7474c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy)); 7484c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall} 7494c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7504c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// The ARM ABI does the same as the Itanium ABI, but returns 'this' 7514c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// for non-deleting destructors. 7524c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ARMCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor, 7534c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CXXDtorType Type, 7544c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CanQualType &ResTy, 7555f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVectorImpl<CanQualType> &ArgTys) { 7564c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall ItaniumCXXABI::BuildDestructorSignature(Dtor, Type, ResTy, ArgTys); 7574c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7584c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (Type != Dtor_Deleting) 7594c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall ResTy = ArgTys[0]; 7604c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall} 7614c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7624c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ItaniumCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF, 7634c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall QualType &ResTy, 7644c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall FunctionArgList &Params) { 7654c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall /// Create the 'this' variable. 7664c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall BuildThisParam(CGF, Params); 7674c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7684c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl()); 7694c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall assert(MD->isInstance()); 7704c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7714c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // Check if we need a VTT parameter as well. 7724c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (CodeGenVTables::needsVTTParameter(CGF.CurGD)) { 7739cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall ASTContext &Context = getContext(); 7744c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7754c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // FIXME: avoid the fake decl 7764c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall QualType T = Context.getPointerType(Context.VoidPtrTy); 7774c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall ImplicitParamDecl *VTTDecl 7784c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall = ImplicitParamDecl::Create(Context, 0, MD->getLocation(), 7794c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall &Context.Idents.get("vtt"), T); 780d26bc76c98006609002d9930f8840490e88ac5b5John McCall Params.push_back(VTTDecl); 7814c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall getVTTDecl(CGF) = VTTDecl; 7824c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall } 7834c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall} 7844c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7854c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ARMCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF, 7864c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall QualType &ResTy, 7874c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall FunctionArgList &Params) { 7884c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall ItaniumCXXABI::BuildInstanceFunctionParams(CGF, ResTy, Params); 7894c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7904c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // Return 'this' from certain constructors and destructors. 7914c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (HasThisReturn(CGF.CurGD)) 792d26bc76c98006609002d9930f8840490e88ac5b5John McCall ResTy = Params[0]->getType(); 7934c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall} 7944c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7954c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) { 7964c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall /// Initialize the 'this' slot. 7974c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall EmitThisParam(CGF); 7984c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7994c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall /// Initialize the 'vtt' slot if needed. 8004c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (getVTTDecl(CGF)) { 8014c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall getVTTValue(CGF) 8024c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(getVTTDecl(CGF)), 8034c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall "vtt"); 8044c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall } 8054c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall} 8064c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 8074c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ARMCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) { 8084c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall ItaniumCXXABI::EmitInstanceFunctionProlog(CGF); 8094c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 8104c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall /// Initialize the return slot to 'this' at the start of the 8114c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall /// function. 8124c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (HasThisReturn(CGF.CurGD)) 813cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue); 8144c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall} 8154c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 8164c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF, 8174c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall RValue RV, QualType ResultType) { 8184c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl())) 8194c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType); 8204c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 8214c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // Destructor thunks in the ARM ABI have indeterminate results. 8222acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *T = 8234c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall cast<llvm::PointerType>(CGF.ReturnValue->getType())->getElementType(); 8244c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall RValue Undef = RValue::get(llvm::UndefValue::get(T)); 8254c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType); 8264c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall} 8271e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 8281e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall/************************** Array allocation cookies **************************/ 8291e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 830e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCallCharUnits ItaniumCXXABI::getArrayCookieSizeImpl(QualType elementType) { 831e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall // The array cookie is a size_t; pad that up to the element alignment. 832e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall // The cookie is actually right-justified in that space. 833e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall return std::max(CharUnits::fromQuantity(CGM.SizeSizeInBytes), 834e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall CGM.getContext().getTypeAlignInChars(elementType)); 8351e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall} 8361e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 8371e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCallllvm::Value *ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, 8381e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall llvm::Value *NewPtr, 8391e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall llvm::Value *NumElements, 8406ec278d1a354517e20f13a877481453ee7940c78John McCall const CXXNewExpr *expr, 8411e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall QualType ElementType) { 842e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall assert(requiresArrayCookie(expr)); 8431e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 844956a5a17713deb1b5b27893303c4f308a1bd2a62Micah Villmow unsigned AS = NewPtr->getType()->getPointerAddressSpace(); 8451e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 8469cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall ASTContext &Ctx = getContext(); 8471e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall QualType SizeTy = Ctx.getSizeType(); 8481e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall CharUnits SizeSize = Ctx.getTypeSizeInChars(SizeTy); 8491e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 8501e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // The size of the cookie. 8511e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall CharUnits CookieSize = 8521e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType)); 853e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall assert(CookieSize == getArrayCookieSizeImpl(ElementType)); 8541e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 8551e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // Compute an offset to the cookie. 8561e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall llvm::Value *CookiePtr = NewPtr; 8571e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall CharUnits CookieOffset = CookieSize - SizeSize; 8581e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall if (!CookieOffset.isZero()) 8591e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_64(CookiePtr, 8601e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall CookieOffset.getQuantity()); 8611e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 8621e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // Write the number of elements into the appropriate slot. 8631e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall llvm::Value *NumElementsPtr 8641e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall = CGF.Builder.CreateBitCast(CookiePtr, 8651e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall CGF.ConvertType(SizeTy)->getPointerTo(AS)); 8661e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall CGF.Builder.CreateStore(NumElements, NumElementsPtr); 8671e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 8681e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // Finally, compute a pointer to the actual data buffer by skipping 8691e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // over the cookie completely. 8701e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr, 8711e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall CookieSize.getQuantity()); 8721e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall} 8731e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 874e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCallllvm::Value *ItaniumCXXABI::readArrayCookieImpl(CodeGenFunction &CGF, 875e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall llvm::Value *allocPtr, 876e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall CharUnits cookieSize) { 877e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall // The element size is right-justified in the cookie. 878e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall llvm::Value *numElementsPtr = allocPtr; 879e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall CharUnits numElementsOffset = 880e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall cookieSize - CharUnits::fromQuantity(CGF.SizeSizeInBytes); 881e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall if (!numElementsOffset.isZero()) 882e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall numElementsPtr = 883e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall CGF.Builder.CreateConstInBoundsGEP1_64(numElementsPtr, 884e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall numElementsOffset.getQuantity()); 8851e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 886956a5a17713deb1b5b27893303c4f308a1bd2a62Micah Villmow unsigned AS = allocPtr->getType()->getPointerAddressSpace(); 887e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall numElementsPtr = 888e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS)); 889e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall return CGF.Builder.CreateLoad(numElementsPtr); 8901e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall} 8911e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 892e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCallCharUnits ARMCXXABI::getArrayCookieSizeImpl(QualType elementType) { 893f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall // ARM says that the cookie is always: 8941e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // struct array_cookie { 8951e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // std::size_t element_size; // element_size != 0 8961e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // std::size_t element_count; 8971e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // }; 898f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall // But the base ABI doesn't give anything an alignment greater than 899f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall // 8, so we can dismiss this as typical ABI-author blindness to 900f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall // actual language complexity and round up to the element alignment. 901f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall return std::max(CharUnits::fromQuantity(2 * CGM.SizeSizeInBytes), 902f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall CGM.getContext().getTypeAlignInChars(elementType)); 9031e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall} 9041e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 9051e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCallllvm::Value *ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, 906f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall llvm::Value *newPtr, 907f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall llvm::Value *numElements, 9086ec278d1a354517e20f13a877481453ee7940c78John McCall const CXXNewExpr *expr, 909f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall QualType elementType) { 910e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall assert(requiresArrayCookie(expr)); 9111e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 912f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall // NewPtr is a char*, but we generalize to arbitrary addrspaces. 913f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall unsigned AS = newPtr->getType()->getPointerAddressSpace(); 9141e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 9151e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // The cookie is always at the start of the buffer. 916f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall llvm::Value *cookie = newPtr; 9171e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 9181e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // The first element is the element size. 919f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall cookie = CGF.Builder.CreateBitCast(cookie, CGF.SizeTy->getPointerTo(AS)); 920f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall llvm::Value *elementSize = llvm::ConstantInt::get(CGF.SizeTy, 921f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall getContext().getTypeSizeInChars(elementType).getQuantity()); 922f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall CGF.Builder.CreateStore(elementSize, cookie); 9231e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 9241e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // The second element is the element count. 925f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall cookie = CGF.Builder.CreateConstInBoundsGEP1_32(cookie, 1); 926f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall CGF.Builder.CreateStore(numElements, cookie); 9271e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 9281e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // Finally, compute a pointer to the actual data buffer by skipping 9291e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // over the cookie completely. 930f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall CharUnits cookieSize = ARMCXXABI::getArrayCookieSizeImpl(elementType); 931f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall return CGF.Builder.CreateConstInBoundsGEP1_64(newPtr, 932f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall cookieSize.getQuantity()); 9331e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall} 9341e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 935e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCallllvm::Value *ARMCXXABI::readArrayCookieImpl(CodeGenFunction &CGF, 936e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall llvm::Value *allocPtr, 937e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall CharUnits cookieSize) { 938e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall // The number of elements is at offset sizeof(size_t) relative to 939e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall // the allocated pointer. 940e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall llvm::Value *numElementsPtr 941e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall = CGF.Builder.CreateConstInBoundsGEP1_64(allocPtr, CGF.SizeSizeInBytes); 9421e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 943956a5a17713deb1b5b27893303c4f308a1bd2a62Micah Villmow unsigned AS = allocPtr->getType()->getPointerAddressSpace(); 944e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall numElementsPtr = 945e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS)); 946e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall return CGF.Builder.CreateLoad(numElementsPtr); 9471e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall} 9481e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 9495cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall/*********************** Static local initialization **************************/ 9505cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 9515cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCallstatic llvm::Constant *getGuardAcquireFn(CodeGenModule &CGM, 9529cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::PointerType *GuardPtrTy) { 9535cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // int __cxa_guard_acquire(__guard *guard_object); 9542acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *FTy = 9555cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall llvm::FunctionType::get(CGM.getTypes().ConvertType(CGM.getContext().IntTy), 956da549e8995c447542d5631b8b67fcc3a9582797aJay Foad GuardPtrTy, /*isVarArg=*/false); 957e76872e81ea32fbc863d8d7ef6eadc91a8f8673bNick Lewycky return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire", 95872390b39c545426023ec104afe8706395d732badBill Wendling llvm::Attribute::get(CGM.getLLVMContext(), 95972390b39c545426023ec104afe8706395d732badBill Wendling llvm::Attribute::NoUnwind)); 9605cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall} 9615cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 9625cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCallstatic llvm::Constant *getGuardReleaseFn(CodeGenModule &CGM, 9639cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::PointerType *GuardPtrTy) { 9645cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // void __cxa_guard_release(__guard *guard_object); 9652acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *FTy = 9668b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false); 967e76872e81ea32fbc863d8d7ef6eadc91a8f8673bNick Lewycky return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release", 96872390b39c545426023ec104afe8706395d732badBill Wendling llvm::Attribute::get(CGM.getLLVMContext(), 96972390b39c545426023ec104afe8706395d732badBill Wendling llvm::Attribute::NoUnwind)); 9705cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall} 9715cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 9725cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCallstatic llvm::Constant *getGuardAbortFn(CodeGenModule &CGM, 9739cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::PointerType *GuardPtrTy) { 9745cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // void __cxa_guard_abort(__guard *guard_object); 9752acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *FTy = 9768b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false); 977e76872e81ea32fbc863d8d7ef6eadc91a8f8673bNick Lewycky return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort", 97872390b39c545426023ec104afe8706395d732badBill Wendling llvm::Attribute::get(CGM.getLLVMContext(), 97972390b39c545426023ec104afe8706395d732badBill Wendling llvm::Attribute::NoUnwind)); 9805cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall} 9815cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 9825cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCallnamespace { 9835cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall struct CallGuardAbort : EHScopeStack::Cleanup { 9845cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall llvm::GlobalVariable *Guard; 9850f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {} 9865cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 987ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall void Emit(CodeGenFunction &CGF, Flags flags) { 9885cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall CGF.Builder.CreateCall(getGuardAbortFn(CGF.CGM, Guard->getType()), Guard) 9895cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall ->setDoesNotThrow(); 9905cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall } 9915cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall }; 9925cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall} 9935cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 9945cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall/// The ARM code here follows the Itanium code closely enough that we 9955cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall/// just special-case it at particular places. 9963030eb82593097502469a8b3fc26112c79c75605John McCallvoid ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF, 9973030eb82593097502469a8b3fc26112c79c75605John McCall const VarDecl &D, 998355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall llvm::GlobalVariable *var, 999355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall bool shouldPerformInit) { 10005cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall CGBuilderTy &Builder = CGF.Builder; 10013030eb82593097502469a8b3fc26112c79c75605John McCall 10023030eb82593097502469a8b3fc26112c79c75605John McCall // We only need to use thread-safe statics for local variables; 10033030eb82593097502469a8b3fc26112c79c75605John McCall // global initialization is always single-threaded. 10040502a224984a26087ea4d64e8e5d2dd4dca432f6John McCall bool threadsafe = 10054e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie (getContext().getLangOpts().ThreadsafeStatics && D.isLocalVarDecl()); 1006173d51286bcaff4b6b76eebf6542d3b1311142e2Anders Carlsson 1007173d51286bcaff4b6b76eebf6542d3b1311142e2Anders Carlsson // If we have a global variable with internal linkage and thread-safe statics 1008173d51286bcaff4b6b76eebf6542d3b1311142e2Anders Carlsson // are disabled, we can just let the guard variable be of type i8. 1009355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall bool useInt8GuardVariable = !threadsafe && var->hasInternalLinkage(); 1010355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall 1011355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall llvm::IntegerType *guardTy; 10120502a224984a26087ea4d64e8e5d2dd4dca432f6John McCall if (useInt8GuardVariable) { 1013355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall guardTy = CGF.Int8Ty; 10140502a224984a26087ea4d64e8e5d2dd4dca432f6John McCall } else { 1015173d51286bcaff4b6b76eebf6542d3b1311142e2Anders Carlsson // Guard variables are 64 bits in the generic ABI and 32 bits on ARM. 1016355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall guardTy = (IsARM ? CGF.Int32Ty : CGF.Int64Ty); 1017355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall } 1018355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall llvm::PointerType *guardPtrTy = guardTy->getPointerTo(); 1019355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall 1020355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall // Create the guard variable if we don't already have it (as we 1021355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall // might if we're double-emitting this function body). 1022355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall llvm::GlobalVariable *guard = CGM.getStaticLocalDeclGuardAddress(&D); 1023355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall if (!guard) { 1024355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall // Mangle the name for the guard. 1025355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall SmallString<256> guardName; 1026355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall { 1027355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall llvm::raw_svector_ostream out(guardName); 1028355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall getMangleContext().mangleItaniumGuardVariable(&D, out); 1029355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall out.flush(); 1030355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall } 1031355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall 1032355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall // Create the guard variable with a zero-initializer. 1033355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall // Just absorb linkage and visibility from the guarded variable. 1034355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall guard = new llvm::GlobalVariable(CGM.getModule(), guardTy, 1035355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall false, var->getLinkage(), 1036355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall llvm::ConstantInt::get(guardTy, 0), 1037355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall guardName.str()); 1038355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall guard->setVisibility(var->getVisibility()); 1039355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall 1040355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall CGM.setStaticLocalDeclGuardAddress(&D, guard); 1041173d51286bcaff4b6b76eebf6542d3b1311142e2Anders Carlsson } 10425cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 10435cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // Test whether the variable has completed initialization. 1044355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall llvm::Value *isInitialized; 10455cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 10465cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // ARM C++ ABI 3.2.3.1: 10475cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // To support the potential use of initialization guard variables 10485cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // as semaphores that are the target of ARM SWP and LDREX/STREX 10495cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // synchronizing instructions we define a static initialization 10505cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // guard variable to be a 4-byte aligned, 4- byte word with the 10515cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // following inline access protocol. 10525cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // #define INITIALIZED 1 10535cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // if ((obj_guard & INITIALIZED) != INITIALIZED) { 10545cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // if (__cxa_guard_acquire(&obj_guard)) 10555cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // ... 10565cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // } 10570502a224984a26087ea4d64e8e5d2dd4dca432f6John McCall if (IsARM && !useInt8GuardVariable) { 1058355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall llvm::Value *V = Builder.CreateLoad(guard); 10595cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall V = Builder.CreateAnd(V, Builder.getInt32(1)); 1060355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall isInitialized = Builder.CreateIsNull(V, "guard.uninitialized"); 10615cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 10625cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // Itanium C++ ABI 3.3.2: 10635cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // The following is pseudo-code showing how these functions can be used: 10645cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // if (obj_guard.first_byte == 0) { 10655cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // if ( __cxa_guard_acquire (&obj_guard) ) { 10665cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // try { 10675cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // ... initialize the object ...; 10685cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // } catch (...) { 10695cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // __cxa_guard_abort (&obj_guard); 10705cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // throw; 10715cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // } 10725cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // ... queue object destructor with __cxa_atexit() ...; 10735cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // __cxa_guard_release (&obj_guard); 10745cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // } 10755cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // } 10765cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall } else { 10775cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // Load the first byte of the guard variable. 10780f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth llvm::LoadInst *LI = 1079355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall Builder.CreateLoad(Builder.CreateBitCast(guard, CGM.Int8PtrTy)); 10800f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth LI->setAlignment(1); 1081eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman 1082eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman // Itanium ABI: 1083eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman // An implementation supporting thread-safety on multiprocessor 1084eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman // systems must also guarantee that references to the initialized 1085eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman // object do not occur before the load of the initialization flag. 1086eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman // 1087eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman // In LLVM, we do this by marking the load Acquire. 1088eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman if (threadsafe) 10890f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth LI->setAtomic(llvm::Acquire); 1090eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman 1091355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall isInitialized = Builder.CreateIsNull(LI, "guard.uninitialized"); 10925cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall } 10935cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 10945cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall llvm::BasicBlock *InitCheckBlock = CGF.createBasicBlock("init.check"); 10955cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end"); 10965cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 10975cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // Check if the first byte of the guard variable is zero. 1098355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall Builder.CreateCondBr(isInitialized, InitCheckBlock, EndBlock); 10995cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 11005cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall CGF.EmitBlock(InitCheckBlock); 11015cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 11025cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // Variables used when coping with thread-safe statics and exceptions. 11030502a224984a26087ea4d64e8e5d2dd4dca432f6John McCall if (threadsafe) { 11045cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // Call __cxa_guard_acquire. 11055cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall llvm::Value *V 1106355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall = Builder.CreateCall(getGuardAcquireFn(CGM, guardPtrTy), guard); 11075cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 11085cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init"); 11095cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 11105cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"), 11115cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall InitBlock, EndBlock); 11125cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 11135cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // Call __cxa_guard_abort along the exceptional edge. 1114355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall CGF.EHStack.pushCleanup<CallGuardAbort>(EHCleanup, guard); 11155cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 11165cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall CGF.EmitBlock(InitBlock); 11175cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall } 11185cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 11195cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // Emit the initializer and add a global destructor if appropriate. 1120355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall CGF.EmitCXXGlobalVarDeclInit(D, var, shouldPerformInit); 11215cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 11220502a224984a26087ea4d64e8e5d2dd4dca432f6John McCall if (threadsafe) { 11235cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // Pop the guard-abort cleanup if we pushed one. 11245cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall CGF.PopCleanupBlock(); 11255cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 11265cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // Call __cxa_guard_release. This cannot throw. 1127355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall Builder.CreateCall(getGuardReleaseFn(CGM, guardPtrTy), guard); 11285cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall } else { 1129355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall Builder.CreateStore(llvm::ConstantInt::get(guardTy, 1), guard); 11305cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall } 11315cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 11325cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall CGF.EmitBlock(EndBlock); 11335cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall} 113420bb175cb8ae5844034828db094fb948c0e3454aJohn McCall 113520bb175cb8ae5844034828db094fb948c0e3454aJohn McCall/// Register a global destructor using __cxa_atexit. 113620bb175cb8ae5844034828db094fb948c0e3454aJohn McCallstatic void emitGlobalDtorWithCXAAtExit(CodeGenFunction &CGF, 113720bb175cb8ae5844034828db094fb948c0e3454aJohn McCall llvm::Constant *dtor, 113820bb175cb8ae5844034828db094fb948c0e3454aJohn McCall llvm::Constant *addr) { 113920bb175cb8ae5844034828db094fb948c0e3454aJohn McCall // We're assuming that the destructor function is something we can 114020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall // reasonably call with the default CC. Go ahead and cast it to the 114120bb175cb8ae5844034828db094fb948c0e3454aJohn McCall // right prototype. 114220bb175cb8ae5844034828db094fb948c0e3454aJohn McCall llvm::Type *dtorTy = 114320bb175cb8ae5844034828db094fb948c0e3454aJohn McCall llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, false)->getPointerTo(); 114420bb175cb8ae5844034828db094fb948c0e3454aJohn McCall 114520bb175cb8ae5844034828db094fb948c0e3454aJohn McCall // extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d); 114620bb175cb8ae5844034828db094fb948c0e3454aJohn McCall llvm::Type *paramTys[] = { dtorTy, CGF.Int8PtrTy, CGF.Int8PtrTy }; 114720bb175cb8ae5844034828db094fb948c0e3454aJohn McCall llvm::FunctionType *atexitTy = 114820bb175cb8ae5844034828db094fb948c0e3454aJohn McCall llvm::FunctionType::get(CGF.IntTy, paramTys, false); 114920bb175cb8ae5844034828db094fb948c0e3454aJohn McCall 115020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall // Fetch the actual function. 115120bb175cb8ae5844034828db094fb948c0e3454aJohn McCall llvm::Constant *atexit = 115220bb175cb8ae5844034828db094fb948c0e3454aJohn McCall CGF.CGM.CreateRuntimeFunction(atexitTy, "__cxa_atexit"); 115320bb175cb8ae5844034828db094fb948c0e3454aJohn McCall if (llvm::Function *fn = dyn_cast<llvm::Function>(atexit)) 115420bb175cb8ae5844034828db094fb948c0e3454aJohn McCall fn->setDoesNotThrow(); 115520bb175cb8ae5844034828db094fb948c0e3454aJohn McCall 115620bb175cb8ae5844034828db094fb948c0e3454aJohn McCall // Create a variable that binds the atexit to this shared object. 115720bb175cb8ae5844034828db094fb948c0e3454aJohn McCall llvm::Constant *handle = 115820bb175cb8ae5844034828db094fb948c0e3454aJohn McCall CGF.CGM.CreateRuntimeVariable(CGF.Int8Ty, "__dso_handle"); 115920bb175cb8ae5844034828db094fb948c0e3454aJohn McCall 116020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall llvm::Value *args[] = { 116120bb175cb8ae5844034828db094fb948c0e3454aJohn McCall llvm::ConstantExpr::getBitCast(dtor, dtorTy), 116220bb175cb8ae5844034828db094fb948c0e3454aJohn McCall llvm::ConstantExpr::getBitCast(addr, CGF.Int8PtrTy), 116320bb175cb8ae5844034828db094fb948c0e3454aJohn McCall handle 116420bb175cb8ae5844034828db094fb948c0e3454aJohn McCall }; 116520bb175cb8ae5844034828db094fb948c0e3454aJohn McCall CGF.Builder.CreateCall(atexit, args)->setDoesNotThrow(); 116620bb175cb8ae5844034828db094fb948c0e3454aJohn McCall} 116720bb175cb8ae5844034828db094fb948c0e3454aJohn McCall 116820bb175cb8ae5844034828db094fb948c0e3454aJohn McCall/// Register a global destructor as best as we know how. 116920bb175cb8ae5844034828db094fb948c0e3454aJohn McCallvoid ItaniumCXXABI::registerGlobalDtor(CodeGenFunction &CGF, 117020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall llvm::Constant *dtor, 117120bb175cb8ae5844034828db094fb948c0e3454aJohn McCall llvm::Constant *addr) { 117220bb175cb8ae5844034828db094fb948c0e3454aJohn McCall // Use __cxa_atexit if available. 117320bb175cb8ae5844034828db094fb948c0e3454aJohn McCall if (CGM.getCodeGenOpts().CXAAtExit) { 117420bb175cb8ae5844034828db094fb948c0e3454aJohn McCall return emitGlobalDtorWithCXAAtExit(CGF, dtor, addr); 117520bb175cb8ae5844034828db094fb948c0e3454aJohn McCall } 117620bb175cb8ae5844034828db094fb948c0e3454aJohn McCall 117720bb175cb8ae5844034828db094fb948c0e3454aJohn McCall // In Apple kexts, we want to add a global destructor entry. 117820bb175cb8ae5844034828db094fb948c0e3454aJohn McCall // FIXME: shouldn't this be guarded by some variable? 11797edf9e38b91917b661277601c0e448eef0eb2b56Richard Smith if (CGM.getLangOpts().AppleKext) { 118020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall // Generate a global destructor entry. 118120bb175cb8ae5844034828db094fb948c0e3454aJohn McCall return CGM.AddCXXDtorEntry(dtor, addr); 118220bb175cb8ae5844034828db094fb948c0e3454aJohn McCall } 118320bb175cb8ae5844034828db094fb948c0e3454aJohn McCall 118420bb175cb8ae5844034828db094fb948c0e3454aJohn McCall CGF.registerGlobalDtorWithAtExit(dtor, addr); 118520bb175cb8ae5844034828db094fb948c0e3454aJohn McCall} 1186