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