1*f4a2713aSLionel Sambuc //===--- CGDeclCXX.cpp - Emit LLVM Code for C++ declarations --------------===// 2*f4a2713aSLionel Sambuc // 3*f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure 4*f4a2713aSLionel Sambuc // 5*f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source 6*f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details. 7*f4a2713aSLionel Sambuc // 8*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 9*f4a2713aSLionel Sambuc // 10*f4a2713aSLionel Sambuc // This contains code dealing with code generation of C++ declarations 11*f4a2713aSLionel Sambuc // 12*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 13*f4a2713aSLionel Sambuc 14*f4a2713aSLionel Sambuc #include "CodeGenFunction.h" 15*f4a2713aSLionel Sambuc #include "CGCXXABI.h" 16*f4a2713aSLionel Sambuc #include "CGObjCRuntime.h" 17*f4a2713aSLionel Sambuc #include "clang/Frontend/CodeGenOptions.h" 18*f4a2713aSLionel Sambuc #include "llvm/ADT/StringExtras.h" 19*f4a2713aSLionel Sambuc #include "llvm/IR/Intrinsics.h" 20*f4a2713aSLionel Sambuc 21*f4a2713aSLionel Sambuc using namespace clang; 22*f4a2713aSLionel Sambuc using namespace CodeGen; 23*f4a2713aSLionel Sambuc 24*f4a2713aSLionel Sambuc static void EmitDeclInit(CodeGenFunction &CGF, const VarDecl &D, 25*f4a2713aSLionel Sambuc llvm::Constant *DeclPtr) { 26*f4a2713aSLionel Sambuc assert(D.hasGlobalStorage() && "VarDecl must have global storage!"); 27*f4a2713aSLionel Sambuc assert(!D.getType()->isReferenceType() && 28*f4a2713aSLionel Sambuc "Should not call EmitDeclInit on a reference!"); 29*f4a2713aSLionel Sambuc 30*f4a2713aSLionel Sambuc ASTContext &Context = CGF.getContext(); 31*f4a2713aSLionel Sambuc 32*f4a2713aSLionel Sambuc CharUnits alignment = Context.getDeclAlign(&D); 33*f4a2713aSLionel Sambuc QualType type = D.getType(); 34*f4a2713aSLionel Sambuc LValue lv = CGF.MakeAddrLValue(DeclPtr, type, alignment); 35*f4a2713aSLionel Sambuc 36*f4a2713aSLionel Sambuc const Expr *Init = D.getInit(); 37*f4a2713aSLionel Sambuc switch (CGF.getEvaluationKind(type)) { 38*f4a2713aSLionel Sambuc case TEK_Scalar: { 39*f4a2713aSLionel Sambuc CodeGenModule &CGM = CGF.CGM; 40*f4a2713aSLionel Sambuc if (lv.isObjCStrong()) 41*f4a2713aSLionel Sambuc CGM.getObjCRuntime().EmitObjCGlobalAssign(CGF, CGF.EmitScalarExpr(Init), 42*f4a2713aSLionel Sambuc DeclPtr, D.getTLSKind()); 43*f4a2713aSLionel Sambuc else if (lv.isObjCWeak()) 44*f4a2713aSLionel Sambuc CGM.getObjCRuntime().EmitObjCWeakAssign(CGF, CGF.EmitScalarExpr(Init), 45*f4a2713aSLionel Sambuc DeclPtr); 46*f4a2713aSLionel Sambuc else 47*f4a2713aSLionel Sambuc CGF.EmitScalarInit(Init, &D, lv, false); 48*f4a2713aSLionel Sambuc return; 49*f4a2713aSLionel Sambuc } 50*f4a2713aSLionel Sambuc case TEK_Complex: 51*f4a2713aSLionel Sambuc CGF.EmitComplexExprIntoLValue(Init, lv, /*isInit*/ true); 52*f4a2713aSLionel Sambuc return; 53*f4a2713aSLionel Sambuc case TEK_Aggregate: 54*f4a2713aSLionel Sambuc CGF.EmitAggExpr(Init, AggValueSlot::forLValue(lv,AggValueSlot::IsDestructed, 55*f4a2713aSLionel Sambuc AggValueSlot::DoesNotNeedGCBarriers, 56*f4a2713aSLionel Sambuc AggValueSlot::IsNotAliased)); 57*f4a2713aSLionel Sambuc return; 58*f4a2713aSLionel Sambuc } 59*f4a2713aSLionel Sambuc llvm_unreachable("bad evaluation kind"); 60*f4a2713aSLionel Sambuc } 61*f4a2713aSLionel Sambuc 62*f4a2713aSLionel Sambuc /// Emit code to cause the destruction of the given variable with 63*f4a2713aSLionel Sambuc /// static storage duration. 64*f4a2713aSLionel Sambuc static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D, 65*f4a2713aSLionel Sambuc llvm::Constant *addr) { 66*f4a2713aSLionel Sambuc CodeGenModule &CGM = CGF.CGM; 67*f4a2713aSLionel Sambuc 68*f4a2713aSLionel Sambuc // FIXME: __attribute__((cleanup)) ? 69*f4a2713aSLionel Sambuc 70*f4a2713aSLionel Sambuc QualType type = D.getType(); 71*f4a2713aSLionel Sambuc QualType::DestructionKind dtorKind = type.isDestructedType(); 72*f4a2713aSLionel Sambuc 73*f4a2713aSLionel Sambuc switch (dtorKind) { 74*f4a2713aSLionel Sambuc case QualType::DK_none: 75*f4a2713aSLionel Sambuc return; 76*f4a2713aSLionel Sambuc 77*f4a2713aSLionel Sambuc case QualType::DK_cxx_destructor: 78*f4a2713aSLionel Sambuc break; 79*f4a2713aSLionel Sambuc 80*f4a2713aSLionel Sambuc case QualType::DK_objc_strong_lifetime: 81*f4a2713aSLionel Sambuc case QualType::DK_objc_weak_lifetime: 82*f4a2713aSLionel Sambuc // We don't care about releasing objects during process teardown. 83*f4a2713aSLionel Sambuc assert(!D.getTLSKind() && "should have rejected this"); 84*f4a2713aSLionel Sambuc return; 85*f4a2713aSLionel Sambuc } 86*f4a2713aSLionel Sambuc 87*f4a2713aSLionel Sambuc llvm::Constant *function; 88*f4a2713aSLionel Sambuc llvm::Constant *argument; 89*f4a2713aSLionel Sambuc 90*f4a2713aSLionel Sambuc // Special-case non-array C++ destructors, where there's a function 91*f4a2713aSLionel Sambuc // with the right signature that we can just call. 92*f4a2713aSLionel Sambuc const CXXRecordDecl *record = 0; 93*f4a2713aSLionel Sambuc if (dtorKind == QualType::DK_cxx_destructor && 94*f4a2713aSLionel Sambuc (record = type->getAsCXXRecordDecl())) { 95*f4a2713aSLionel Sambuc assert(!record->hasTrivialDestructor()); 96*f4a2713aSLionel Sambuc CXXDestructorDecl *dtor = record->getDestructor(); 97*f4a2713aSLionel Sambuc 98*f4a2713aSLionel Sambuc function = CGM.GetAddrOfCXXDestructor(dtor, Dtor_Complete); 99*f4a2713aSLionel Sambuc argument = llvm::ConstantExpr::getBitCast( 100*f4a2713aSLionel Sambuc addr, CGF.getTypes().ConvertType(type)->getPointerTo()); 101*f4a2713aSLionel Sambuc 102*f4a2713aSLionel Sambuc // Otherwise, the standard logic requires a helper function. 103*f4a2713aSLionel Sambuc } else { 104*f4a2713aSLionel Sambuc function = CodeGenFunction(CGM) 105*f4a2713aSLionel Sambuc .generateDestroyHelper(addr, type, CGF.getDestroyer(dtorKind), 106*f4a2713aSLionel Sambuc CGF.needsEHCleanup(dtorKind), &D); 107*f4a2713aSLionel Sambuc argument = llvm::Constant::getNullValue(CGF.Int8PtrTy); 108*f4a2713aSLionel Sambuc } 109*f4a2713aSLionel Sambuc 110*f4a2713aSLionel Sambuc CGM.getCXXABI().registerGlobalDtor(CGF, D, function, argument); 111*f4a2713aSLionel Sambuc } 112*f4a2713aSLionel Sambuc 113*f4a2713aSLionel Sambuc /// Emit code to cause the variable at the given address to be considered as 114*f4a2713aSLionel Sambuc /// constant from this point onwards. 115*f4a2713aSLionel Sambuc static void EmitDeclInvariant(CodeGenFunction &CGF, const VarDecl &D, 116*f4a2713aSLionel Sambuc llvm::Constant *Addr) { 117*f4a2713aSLionel Sambuc // Don't emit the intrinsic if we're not optimizing. 118*f4a2713aSLionel Sambuc if (!CGF.CGM.getCodeGenOpts().OptimizationLevel) 119*f4a2713aSLionel Sambuc return; 120*f4a2713aSLionel Sambuc 121*f4a2713aSLionel Sambuc // Grab the llvm.invariant.start intrinsic. 122*f4a2713aSLionel Sambuc llvm::Intrinsic::ID InvStartID = llvm::Intrinsic::invariant_start; 123*f4a2713aSLionel Sambuc llvm::Constant *InvariantStart = CGF.CGM.getIntrinsic(InvStartID); 124*f4a2713aSLionel Sambuc 125*f4a2713aSLionel Sambuc // Emit a call with the size in bytes of the object. 126*f4a2713aSLionel Sambuc CharUnits WidthChars = CGF.getContext().getTypeSizeInChars(D.getType()); 127*f4a2713aSLionel Sambuc uint64_t Width = WidthChars.getQuantity(); 128*f4a2713aSLionel Sambuc llvm::Value *Args[2] = { llvm::ConstantInt::getSigned(CGF.Int64Ty, Width), 129*f4a2713aSLionel Sambuc llvm::ConstantExpr::getBitCast(Addr, CGF.Int8PtrTy)}; 130*f4a2713aSLionel Sambuc CGF.Builder.CreateCall(InvariantStart, Args); 131*f4a2713aSLionel Sambuc } 132*f4a2713aSLionel Sambuc 133*f4a2713aSLionel Sambuc void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D, 134*f4a2713aSLionel Sambuc llvm::Constant *DeclPtr, 135*f4a2713aSLionel Sambuc bool PerformInit) { 136*f4a2713aSLionel Sambuc 137*f4a2713aSLionel Sambuc const Expr *Init = D.getInit(); 138*f4a2713aSLionel Sambuc QualType T = D.getType(); 139*f4a2713aSLionel Sambuc 140*f4a2713aSLionel Sambuc if (!T->isReferenceType()) { 141*f4a2713aSLionel Sambuc if (PerformInit) 142*f4a2713aSLionel Sambuc EmitDeclInit(*this, D, DeclPtr); 143*f4a2713aSLionel Sambuc if (CGM.isTypeConstant(D.getType(), true)) 144*f4a2713aSLionel Sambuc EmitDeclInvariant(*this, D, DeclPtr); 145*f4a2713aSLionel Sambuc else 146*f4a2713aSLionel Sambuc EmitDeclDestroy(*this, D, DeclPtr); 147*f4a2713aSLionel Sambuc return; 148*f4a2713aSLionel Sambuc } 149*f4a2713aSLionel Sambuc 150*f4a2713aSLionel Sambuc assert(PerformInit && "cannot have constant initializer which needs " 151*f4a2713aSLionel Sambuc "destruction for reference"); 152*f4a2713aSLionel Sambuc unsigned Alignment = getContext().getDeclAlign(&D).getQuantity(); 153*f4a2713aSLionel Sambuc RValue RV = EmitReferenceBindingToExpr(Init); 154*f4a2713aSLionel Sambuc EmitStoreOfScalar(RV.getScalarVal(), DeclPtr, false, Alignment, T); 155*f4a2713aSLionel Sambuc } 156*f4a2713aSLionel Sambuc 157*f4a2713aSLionel Sambuc static llvm::Function * 158*f4a2713aSLionel Sambuc CreateGlobalInitOrDestructFunction(CodeGenModule &CGM, 159*f4a2713aSLionel Sambuc llvm::FunctionType *ty, 160*f4a2713aSLionel Sambuc const Twine &name, 161*f4a2713aSLionel Sambuc bool TLS = false); 162*f4a2713aSLionel Sambuc 163*f4a2713aSLionel Sambuc /// Create a stub function, suitable for being passed to atexit, 164*f4a2713aSLionel Sambuc /// which passes the given address to the given destructor function. 165*f4a2713aSLionel Sambuc static llvm::Constant *createAtExitStub(CodeGenModule &CGM, const VarDecl &VD, 166*f4a2713aSLionel Sambuc llvm::Constant *dtor, 167*f4a2713aSLionel Sambuc llvm::Constant *addr) { 168*f4a2713aSLionel Sambuc // Get the destructor function type, void(*)(void). 169*f4a2713aSLionel Sambuc llvm::FunctionType *ty = llvm::FunctionType::get(CGM.VoidTy, false); 170*f4a2713aSLionel Sambuc SmallString<256> FnName; 171*f4a2713aSLionel Sambuc { 172*f4a2713aSLionel Sambuc llvm::raw_svector_ostream Out(FnName); 173*f4a2713aSLionel Sambuc CGM.getCXXABI().getMangleContext().mangleDynamicAtExitDestructor(&VD, Out); 174*f4a2713aSLionel Sambuc } 175*f4a2713aSLionel Sambuc llvm::Function *fn = 176*f4a2713aSLionel Sambuc CreateGlobalInitOrDestructFunction(CGM, ty, FnName.str()); 177*f4a2713aSLionel Sambuc 178*f4a2713aSLionel Sambuc CodeGenFunction CGF(CGM); 179*f4a2713aSLionel Sambuc 180*f4a2713aSLionel Sambuc CGF.StartFunction(&VD, CGM.getContext().VoidTy, fn, 181*f4a2713aSLionel Sambuc CGM.getTypes().arrangeNullaryFunction(), FunctionArgList(), 182*f4a2713aSLionel Sambuc SourceLocation()); 183*f4a2713aSLionel Sambuc 184*f4a2713aSLionel Sambuc llvm::CallInst *call = CGF.Builder.CreateCall(dtor, addr); 185*f4a2713aSLionel Sambuc 186*f4a2713aSLionel Sambuc // Make sure the call and the callee agree on calling convention. 187*f4a2713aSLionel Sambuc if (llvm::Function *dtorFn = 188*f4a2713aSLionel Sambuc dyn_cast<llvm::Function>(dtor->stripPointerCasts())) 189*f4a2713aSLionel Sambuc call->setCallingConv(dtorFn->getCallingConv()); 190*f4a2713aSLionel Sambuc 191*f4a2713aSLionel Sambuc CGF.FinishFunction(); 192*f4a2713aSLionel Sambuc 193*f4a2713aSLionel Sambuc return fn; 194*f4a2713aSLionel Sambuc } 195*f4a2713aSLionel Sambuc 196*f4a2713aSLionel Sambuc /// Register a global destructor using the C atexit runtime function. 197*f4a2713aSLionel Sambuc void CodeGenFunction::registerGlobalDtorWithAtExit(const VarDecl &VD, 198*f4a2713aSLionel Sambuc llvm::Constant *dtor, 199*f4a2713aSLionel Sambuc llvm::Constant *addr) { 200*f4a2713aSLionel Sambuc // Create a function which calls the destructor. 201*f4a2713aSLionel Sambuc llvm::Constant *dtorStub = createAtExitStub(CGM, VD, dtor, addr); 202*f4a2713aSLionel Sambuc 203*f4a2713aSLionel Sambuc // extern "C" int atexit(void (*f)(void)); 204*f4a2713aSLionel Sambuc llvm::FunctionType *atexitTy = 205*f4a2713aSLionel Sambuc llvm::FunctionType::get(IntTy, dtorStub->getType(), false); 206*f4a2713aSLionel Sambuc 207*f4a2713aSLionel Sambuc llvm::Constant *atexit = 208*f4a2713aSLionel Sambuc CGM.CreateRuntimeFunction(atexitTy, "atexit"); 209*f4a2713aSLionel Sambuc if (llvm::Function *atexitFn = dyn_cast<llvm::Function>(atexit)) 210*f4a2713aSLionel Sambuc atexitFn->setDoesNotThrow(); 211*f4a2713aSLionel Sambuc 212*f4a2713aSLionel Sambuc EmitNounwindRuntimeCall(atexit, dtorStub); 213*f4a2713aSLionel Sambuc } 214*f4a2713aSLionel Sambuc 215*f4a2713aSLionel Sambuc void CodeGenFunction::EmitCXXGuardedInit(const VarDecl &D, 216*f4a2713aSLionel Sambuc llvm::GlobalVariable *DeclPtr, 217*f4a2713aSLionel Sambuc bool PerformInit) { 218*f4a2713aSLionel Sambuc // If we've been asked to forbid guard variables, emit an error now. 219*f4a2713aSLionel Sambuc // This diagnostic is hard-coded for Darwin's use case; we can find 220*f4a2713aSLionel Sambuc // better phrasing if someone else needs it. 221*f4a2713aSLionel Sambuc if (CGM.getCodeGenOpts().ForbidGuardVariables) 222*f4a2713aSLionel Sambuc CGM.Error(D.getLocation(), 223*f4a2713aSLionel Sambuc "this initialization requires a guard variable, which " 224*f4a2713aSLionel Sambuc "the kernel does not support"); 225*f4a2713aSLionel Sambuc 226*f4a2713aSLionel Sambuc CGM.getCXXABI().EmitGuardedInit(*this, D, DeclPtr, PerformInit); 227*f4a2713aSLionel Sambuc } 228*f4a2713aSLionel Sambuc 229*f4a2713aSLionel Sambuc static llvm::Function * 230*f4a2713aSLionel Sambuc CreateGlobalInitOrDestructFunction(CodeGenModule &CGM, 231*f4a2713aSLionel Sambuc llvm::FunctionType *FTy, 232*f4a2713aSLionel Sambuc const Twine &Name, bool TLS) { 233*f4a2713aSLionel Sambuc llvm::Function *Fn = 234*f4a2713aSLionel Sambuc llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage, 235*f4a2713aSLionel Sambuc Name, &CGM.getModule()); 236*f4a2713aSLionel Sambuc if (!CGM.getLangOpts().AppleKext && !TLS) { 237*f4a2713aSLionel Sambuc // Set the section if needed. 238*f4a2713aSLionel Sambuc if (const char *Section = 239*f4a2713aSLionel Sambuc CGM.getTarget().getStaticInitSectionSpecifier()) 240*f4a2713aSLionel Sambuc Fn->setSection(Section); 241*f4a2713aSLionel Sambuc } 242*f4a2713aSLionel Sambuc 243*f4a2713aSLionel Sambuc Fn->setCallingConv(CGM.getRuntimeCC()); 244*f4a2713aSLionel Sambuc 245*f4a2713aSLionel Sambuc if (!CGM.getLangOpts().Exceptions) 246*f4a2713aSLionel Sambuc Fn->setDoesNotThrow(); 247*f4a2713aSLionel Sambuc 248*f4a2713aSLionel Sambuc if (CGM.getSanOpts().Address) 249*f4a2713aSLionel Sambuc Fn->addFnAttr(llvm::Attribute::SanitizeAddress); 250*f4a2713aSLionel Sambuc if (CGM.getSanOpts().Thread) 251*f4a2713aSLionel Sambuc Fn->addFnAttr(llvm::Attribute::SanitizeThread); 252*f4a2713aSLionel Sambuc if (CGM.getSanOpts().Memory) 253*f4a2713aSLionel Sambuc Fn->addFnAttr(llvm::Attribute::SanitizeMemory); 254*f4a2713aSLionel Sambuc 255*f4a2713aSLionel Sambuc return Fn; 256*f4a2713aSLionel Sambuc } 257*f4a2713aSLionel Sambuc 258*f4a2713aSLionel Sambuc void 259*f4a2713aSLionel Sambuc CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D, 260*f4a2713aSLionel Sambuc llvm::GlobalVariable *Addr, 261*f4a2713aSLionel Sambuc bool PerformInit) { 262*f4a2713aSLionel Sambuc llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false); 263*f4a2713aSLionel Sambuc SmallString<256> FnName; 264*f4a2713aSLionel Sambuc { 265*f4a2713aSLionel Sambuc llvm::raw_svector_ostream Out(FnName); 266*f4a2713aSLionel Sambuc getCXXABI().getMangleContext().mangleDynamicInitializer(D, Out); 267*f4a2713aSLionel Sambuc } 268*f4a2713aSLionel Sambuc 269*f4a2713aSLionel Sambuc // Create a variable initialization function. 270*f4a2713aSLionel Sambuc llvm::Function *Fn = 271*f4a2713aSLionel Sambuc CreateGlobalInitOrDestructFunction(*this, FTy, FnName.str()); 272*f4a2713aSLionel Sambuc 273*f4a2713aSLionel Sambuc CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr, 274*f4a2713aSLionel Sambuc PerformInit); 275*f4a2713aSLionel Sambuc 276*f4a2713aSLionel Sambuc if (D->getTLSKind()) { 277*f4a2713aSLionel Sambuc // FIXME: Should we support init_priority for thread_local? 278*f4a2713aSLionel Sambuc // FIXME: Ideally, initialization of instantiated thread_local static data 279*f4a2713aSLionel Sambuc // members of class templates should not trigger initialization of other 280*f4a2713aSLionel Sambuc // entities in the TU. 281*f4a2713aSLionel Sambuc // FIXME: We only need to register one __cxa_thread_atexit function for the 282*f4a2713aSLionel Sambuc // entire TU. 283*f4a2713aSLionel Sambuc CXXThreadLocalInits.push_back(Fn); 284*f4a2713aSLionel Sambuc } else if (D->hasAttr<InitPriorityAttr>()) { 285*f4a2713aSLionel Sambuc unsigned int order = D->getAttr<InitPriorityAttr>()->getPriority(); 286*f4a2713aSLionel Sambuc OrderGlobalInits Key(order, PrioritizedCXXGlobalInits.size()); 287*f4a2713aSLionel Sambuc PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn)); 288*f4a2713aSLionel Sambuc DelayedCXXInitPosition.erase(D); 289*f4a2713aSLionel Sambuc } else if (D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization && 290*f4a2713aSLionel Sambuc D->getTemplateSpecializationKind() != TSK_Undeclared) { 291*f4a2713aSLionel Sambuc // C++ [basic.start.init]p2: 292*f4a2713aSLionel Sambuc // Definitions of explicitly specialized class template static data 293*f4a2713aSLionel Sambuc // members have ordered initialization. Other class template static data 294*f4a2713aSLionel Sambuc // members (i.e., implicitly or explicitly instantiated specializations) 295*f4a2713aSLionel Sambuc // have unordered initialization. 296*f4a2713aSLionel Sambuc // 297*f4a2713aSLionel Sambuc // As a consequence, we can put them into their own llvm.global_ctors entry. 298*f4a2713aSLionel Sambuc // This should allow GlobalOpt to fire more often, and allow us to implement 299*f4a2713aSLionel Sambuc // the Microsoft C++ ABI, which uses COMDAT elimination to avoid double 300*f4a2713aSLionel Sambuc // initializaiton. 301*f4a2713aSLionel Sambuc AddGlobalCtor(Fn); 302*f4a2713aSLionel Sambuc DelayedCXXInitPosition.erase(D); 303*f4a2713aSLionel Sambuc } else { 304*f4a2713aSLionel Sambuc llvm::DenseMap<const Decl *, unsigned>::iterator I = 305*f4a2713aSLionel Sambuc DelayedCXXInitPosition.find(D); 306*f4a2713aSLionel Sambuc if (I == DelayedCXXInitPosition.end()) { 307*f4a2713aSLionel Sambuc CXXGlobalInits.push_back(Fn); 308*f4a2713aSLionel Sambuc } else { 309*f4a2713aSLionel Sambuc assert(CXXGlobalInits[I->second] == 0); 310*f4a2713aSLionel Sambuc CXXGlobalInits[I->second] = Fn; 311*f4a2713aSLionel Sambuc DelayedCXXInitPosition.erase(I); 312*f4a2713aSLionel Sambuc } 313*f4a2713aSLionel Sambuc } 314*f4a2713aSLionel Sambuc } 315*f4a2713aSLionel Sambuc 316*f4a2713aSLionel Sambuc void CodeGenModule::EmitCXXThreadLocalInitFunc() { 317*f4a2713aSLionel Sambuc llvm::Function *InitFn = 0; 318*f4a2713aSLionel Sambuc if (!CXXThreadLocalInits.empty()) { 319*f4a2713aSLionel Sambuc // Generate a guarded initialization function. 320*f4a2713aSLionel Sambuc llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false); 321*f4a2713aSLionel Sambuc InitFn = CreateGlobalInitOrDestructFunction(*this, FTy, "__tls_init", 322*f4a2713aSLionel Sambuc /*TLS*/ true); 323*f4a2713aSLionel Sambuc llvm::GlobalVariable *Guard = new llvm::GlobalVariable( 324*f4a2713aSLionel Sambuc getModule(), Int8Ty, false, llvm::GlobalVariable::InternalLinkage, 325*f4a2713aSLionel Sambuc llvm::ConstantInt::get(Int8Ty, 0), "__tls_guard"); 326*f4a2713aSLionel Sambuc Guard->setThreadLocal(true); 327*f4a2713aSLionel Sambuc CodeGenFunction(*this) 328*f4a2713aSLionel Sambuc .GenerateCXXGlobalInitFunc(InitFn, CXXThreadLocalInits, Guard); 329*f4a2713aSLionel Sambuc } 330*f4a2713aSLionel Sambuc 331*f4a2713aSLionel Sambuc getCXXABI().EmitThreadLocalInitFuncs(CXXThreadLocals, InitFn); 332*f4a2713aSLionel Sambuc 333*f4a2713aSLionel Sambuc CXXThreadLocalInits.clear(); 334*f4a2713aSLionel Sambuc CXXThreadLocals.clear(); 335*f4a2713aSLionel Sambuc } 336*f4a2713aSLionel Sambuc 337*f4a2713aSLionel Sambuc void 338*f4a2713aSLionel Sambuc CodeGenModule::EmitCXXGlobalInitFunc() { 339*f4a2713aSLionel Sambuc while (!CXXGlobalInits.empty() && !CXXGlobalInits.back()) 340*f4a2713aSLionel Sambuc CXXGlobalInits.pop_back(); 341*f4a2713aSLionel Sambuc 342*f4a2713aSLionel Sambuc if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty()) 343*f4a2713aSLionel Sambuc return; 344*f4a2713aSLionel Sambuc 345*f4a2713aSLionel Sambuc llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false); 346*f4a2713aSLionel Sambuc 347*f4a2713aSLionel Sambuc 348*f4a2713aSLionel Sambuc // Create our global initialization function. 349*f4a2713aSLionel Sambuc if (!PrioritizedCXXGlobalInits.empty()) { 350*f4a2713aSLionel Sambuc SmallVector<llvm::Constant*, 8> LocalCXXGlobalInits; 351*f4a2713aSLionel Sambuc llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(), 352*f4a2713aSLionel Sambuc PrioritizedCXXGlobalInits.end()); 353*f4a2713aSLionel Sambuc // Iterate over "chunks" of ctors with same priority and emit each chunk 354*f4a2713aSLionel Sambuc // into separate function. Note - everything is sorted first by priority, 355*f4a2713aSLionel Sambuc // second - by lex order, so we emit ctor functions in proper order. 356*f4a2713aSLionel Sambuc for (SmallVectorImpl<GlobalInitData >::iterator 357*f4a2713aSLionel Sambuc I = PrioritizedCXXGlobalInits.begin(), 358*f4a2713aSLionel Sambuc E = PrioritizedCXXGlobalInits.end(); I != E; ) { 359*f4a2713aSLionel Sambuc SmallVectorImpl<GlobalInitData >::iterator 360*f4a2713aSLionel Sambuc PrioE = std::upper_bound(I + 1, E, *I, GlobalInitPriorityCmp()); 361*f4a2713aSLionel Sambuc 362*f4a2713aSLionel Sambuc LocalCXXGlobalInits.clear(); 363*f4a2713aSLionel Sambuc unsigned Priority = I->first.priority; 364*f4a2713aSLionel Sambuc // Compute the function suffix from priority. Prepend with zeroes to make 365*f4a2713aSLionel Sambuc // sure the function names are also ordered as priorities. 366*f4a2713aSLionel Sambuc std::string PrioritySuffix = llvm::utostr(Priority); 367*f4a2713aSLionel Sambuc // Priority is always <= 65535 (enforced by sema).. 368*f4a2713aSLionel Sambuc PrioritySuffix = std::string(6-PrioritySuffix.size(), '0')+PrioritySuffix; 369*f4a2713aSLionel Sambuc llvm::Function *Fn = 370*f4a2713aSLionel Sambuc CreateGlobalInitOrDestructFunction(*this, FTy, 371*f4a2713aSLionel Sambuc "_GLOBAL__I_" + PrioritySuffix); 372*f4a2713aSLionel Sambuc 373*f4a2713aSLionel Sambuc for (; I < PrioE; ++I) 374*f4a2713aSLionel Sambuc LocalCXXGlobalInits.push_back(I->second); 375*f4a2713aSLionel Sambuc 376*f4a2713aSLionel Sambuc CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, LocalCXXGlobalInits); 377*f4a2713aSLionel Sambuc AddGlobalCtor(Fn, Priority); 378*f4a2713aSLionel Sambuc } 379*f4a2713aSLionel Sambuc } 380*f4a2713aSLionel Sambuc 381*f4a2713aSLionel Sambuc llvm::Function *Fn = 382*f4a2713aSLionel Sambuc CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__I_a"); 383*f4a2713aSLionel Sambuc 384*f4a2713aSLionel Sambuc CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, CXXGlobalInits); 385*f4a2713aSLionel Sambuc AddGlobalCtor(Fn); 386*f4a2713aSLionel Sambuc 387*f4a2713aSLionel Sambuc CXXGlobalInits.clear(); 388*f4a2713aSLionel Sambuc PrioritizedCXXGlobalInits.clear(); 389*f4a2713aSLionel Sambuc } 390*f4a2713aSLionel Sambuc 391*f4a2713aSLionel Sambuc void CodeGenModule::EmitCXXGlobalDtorFunc() { 392*f4a2713aSLionel Sambuc if (CXXGlobalDtors.empty()) 393*f4a2713aSLionel Sambuc return; 394*f4a2713aSLionel Sambuc 395*f4a2713aSLionel Sambuc llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false); 396*f4a2713aSLionel Sambuc 397*f4a2713aSLionel Sambuc // Create our global destructor function. 398*f4a2713aSLionel Sambuc llvm::Function *Fn = 399*f4a2713aSLionel Sambuc CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__D_a"); 400*f4a2713aSLionel Sambuc 401*f4a2713aSLionel Sambuc CodeGenFunction(*this).GenerateCXXGlobalDtorsFunc(Fn, CXXGlobalDtors); 402*f4a2713aSLionel Sambuc AddGlobalDtor(Fn); 403*f4a2713aSLionel Sambuc } 404*f4a2713aSLionel Sambuc 405*f4a2713aSLionel Sambuc /// Emit the code necessary to initialize the given global variable. 406*f4a2713aSLionel Sambuc void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn, 407*f4a2713aSLionel Sambuc const VarDecl *D, 408*f4a2713aSLionel Sambuc llvm::GlobalVariable *Addr, 409*f4a2713aSLionel Sambuc bool PerformInit) { 410*f4a2713aSLionel Sambuc // Check if we need to emit debug info for variable initializer. 411*f4a2713aSLionel Sambuc if (D->hasAttr<NoDebugAttr>()) 412*f4a2713aSLionel Sambuc DebugInfo = NULL; // disable debug info indefinitely for this function 413*f4a2713aSLionel Sambuc 414*f4a2713aSLionel Sambuc StartFunction(GlobalDecl(D), getContext().VoidTy, Fn, 415*f4a2713aSLionel Sambuc getTypes().arrangeNullaryFunction(), 416*f4a2713aSLionel Sambuc FunctionArgList(), D->getInit()->getExprLoc()); 417*f4a2713aSLionel Sambuc 418*f4a2713aSLionel Sambuc // Use guarded initialization if the global variable is weak. This 419*f4a2713aSLionel Sambuc // occurs for, e.g., instantiated static data members and 420*f4a2713aSLionel Sambuc // definitions explicitly marked weak. 421*f4a2713aSLionel Sambuc if (Addr->getLinkage() == llvm::GlobalValue::WeakODRLinkage || 422*f4a2713aSLionel Sambuc Addr->getLinkage() == llvm::GlobalValue::WeakAnyLinkage) { 423*f4a2713aSLionel Sambuc EmitCXXGuardedInit(*D, Addr, PerformInit); 424*f4a2713aSLionel Sambuc } else { 425*f4a2713aSLionel Sambuc EmitCXXGlobalVarDeclInit(*D, Addr, PerformInit); 426*f4a2713aSLionel Sambuc } 427*f4a2713aSLionel Sambuc 428*f4a2713aSLionel Sambuc FinishFunction(); 429*f4a2713aSLionel Sambuc } 430*f4a2713aSLionel Sambuc 431*f4a2713aSLionel Sambuc void 432*f4a2713aSLionel Sambuc CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn, 433*f4a2713aSLionel Sambuc ArrayRef<llvm::Constant *> Decls, 434*f4a2713aSLionel Sambuc llvm::GlobalVariable *Guard) { 435*f4a2713aSLionel Sambuc StartFunction(GlobalDecl(), getContext().VoidTy, Fn, 436*f4a2713aSLionel Sambuc getTypes().arrangeNullaryFunction(), 437*f4a2713aSLionel Sambuc FunctionArgList(), SourceLocation()); 438*f4a2713aSLionel Sambuc 439*f4a2713aSLionel Sambuc llvm::BasicBlock *ExitBlock = 0; 440*f4a2713aSLionel Sambuc if (Guard) { 441*f4a2713aSLionel Sambuc // If we have a guard variable, check whether we've already performed these 442*f4a2713aSLionel Sambuc // initializations. This happens for TLS initialization functions. 443*f4a2713aSLionel Sambuc llvm::Value *GuardVal = Builder.CreateLoad(Guard); 444*f4a2713aSLionel Sambuc llvm::Value *Uninit = Builder.CreateIsNull(GuardVal, "guard.uninitialized"); 445*f4a2713aSLionel Sambuc // Mark as initialized before initializing anything else. If the 446*f4a2713aSLionel Sambuc // initializers use previously-initialized thread_local vars, that's 447*f4a2713aSLionel Sambuc // probably supposed to be OK, but the standard doesn't say. 448*f4a2713aSLionel Sambuc Builder.CreateStore(llvm::ConstantInt::get(GuardVal->getType(), 1), Guard); 449*f4a2713aSLionel Sambuc llvm::BasicBlock *InitBlock = createBasicBlock("init"); 450*f4a2713aSLionel Sambuc ExitBlock = createBasicBlock("exit"); 451*f4a2713aSLionel Sambuc Builder.CreateCondBr(Uninit, InitBlock, ExitBlock); 452*f4a2713aSLionel Sambuc EmitBlock(InitBlock); 453*f4a2713aSLionel Sambuc } 454*f4a2713aSLionel Sambuc 455*f4a2713aSLionel Sambuc RunCleanupsScope Scope(*this); 456*f4a2713aSLionel Sambuc 457*f4a2713aSLionel Sambuc // When building in Objective-C++ ARC mode, create an autorelease pool 458*f4a2713aSLionel Sambuc // around the global initializers. 459*f4a2713aSLionel Sambuc if (getLangOpts().ObjCAutoRefCount && getLangOpts().CPlusPlus) { 460*f4a2713aSLionel Sambuc llvm::Value *token = EmitObjCAutoreleasePoolPush(); 461*f4a2713aSLionel Sambuc EmitObjCAutoreleasePoolCleanup(token); 462*f4a2713aSLionel Sambuc } 463*f4a2713aSLionel Sambuc 464*f4a2713aSLionel Sambuc for (unsigned i = 0, e = Decls.size(); i != e; ++i) 465*f4a2713aSLionel Sambuc if (Decls[i]) 466*f4a2713aSLionel Sambuc EmitRuntimeCall(Decls[i]); 467*f4a2713aSLionel Sambuc 468*f4a2713aSLionel Sambuc Scope.ForceCleanup(); 469*f4a2713aSLionel Sambuc 470*f4a2713aSLionel Sambuc if (ExitBlock) { 471*f4a2713aSLionel Sambuc Builder.CreateBr(ExitBlock); 472*f4a2713aSLionel Sambuc EmitBlock(ExitBlock); 473*f4a2713aSLionel Sambuc } 474*f4a2713aSLionel Sambuc 475*f4a2713aSLionel Sambuc FinishFunction(); 476*f4a2713aSLionel Sambuc } 477*f4a2713aSLionel Sambuc 478*f4a2713aSLionel Sambuc void CodeGenFunction::GenerateCXXGlobalDtorsFunc(llvm::Function *Fn, 479*f4a2713aSLionel Sambuc const std::vector<std::pair<llvm::WeakVH, llvm::Constant*> > 480*f4a2713aSLionel Sambuc &DtorsAndObjects) { 481*f4a2713aSLionel Sambuc StartFunction(GlobalDecl(), getContext().VoidTy, Fn, 482*f4a2713aSLionel Sambuc getTypes().arrangeNullaryFunction(), 483*f4a2713aSLionel Sambuc FunctionArgList(), SourceLocation()); 484*f4a2713aSLionel Sambuc 485*f4a2713aSLionel Sambuc // Emit the dtors, in reverse order from construction. 486*f4a2713aSLionel Sambuc for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; ++i) { 487*f4a2713aSLionel Sambuc llvm::Value *Callee = DtorsAndObjects[e - i - 1].first; 488*f4a2713aSLionel Sambuc llvm::CallInst *CI = Builder.CreateCall(Callee, 489*f4a2713aSLionel Sambuc DtorsAndObjects[e - i - 1].second); 490*f4a2713aSLionel Sambuc // Make sure the call and the callee agree on calling convention. 491*f4a2713aSLionel Sambuc if (llvm::Function *F = dyn_cast<llvm::Function>(Callee)) 492*f4a2713aSLionel Sambuc CI->setCallingConv(F->getCallingConv()); 493*f4a2713aSLionel Sambuc } 494*f4a2713aSLionel Sambuc 495*f4a2713aSLionel Sambuc FinishFunction(); 496*f4a2713aSLionel Sambuc } 497*f4a2713aSLionel Sambuc 498*f4a2713aSLionel Sambuc /// generateDestroyHelper - Generates a helper function which, when 499*f4a2713aSLionel Sambuc /// invoked, destroys the given object. 500*f4a2713aSLionel Sambuc llvm::Function *CodeGenFunction::generateDestroyHelper( 501*f4a2713aSLionel Sambuc llvm::Constant *addr, QualType type, Destroyer *destroyer, 502*f4a2713aSLionel Sambuc bool useEHCleanupForArray, const VarDecl *VD) { 503*f4a2713aSLionel Sambuc FunctionArgList args; 504*f4a2713aSLionel Sambuc ImplicitParamDecl dst(0, SourceLocation(), 0, getContext().VoidPtrTy); 505*f4a2713aSLionel Sambuc args.push_back(&dst); 506*f4a2713aSLionel Sambuc 507*f4a2713aSLionel Sambuc const CGFunctionInfo &FI = 508*f4a2713aSLionel Sambuc CGM.getTypes().arrangeFunctionDeclaration(getContext().VoidTy, args, 509*f4a2713aSLionel Sambuc FunctionType::ExtInfo(), 510*f4a2713aSLionel Sambuc /*variadic*/ false); 511*f4a2713aSLionel Sambuc llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); 512*f4a2713aSLionel Sambuc llvm::Function *fn = 513*f4a2713aSLionel Sambuc CreateGlobalInitOrDestructFunction(CGM, FTy, "__cxx_global_array_dtor"); 514*f4a2713aSLionel Sambuc 515*f4a2713aSLionel Sambuc StartFunction(VD, getContext().VoidTy, fn, FI, args, SourceLocation()); 516*f4a2713aSLionel Sambuc 517*f4a2713aSLionel Sambuc emitDestroy(addr, type, destroyer, useEHCleanupForArray); 518*f4a2713aSLionel Sambuc 519*f4a2713aSLionel Sambuc FinishFunction(); 520*f4a2713aSLionel Sambuc 521*f4a2713aSLionel Sambuc return fn; 522*f4a2713aSLionel Sambuc } 523