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 "CGObjCRuntime.h" 16 #include "CGCXXABI.h" 17 #include "clang/Frontend/CodeGenOptions.h" 18 #include "llvm/Intrinsics.h" 19 20 using namespace clang; 21 using namespace CodeGen; 22 23 static void EmitDeclInit(CodeGenFunction &CGF, const VarDecl &D, 24 llvm::Constant *DeclPtr) { 25 assert(D.hasGlobalStorage() && "VarDecl must have global storage!"); 26 assert(!D.getType()->isReferenceType() && 27 "Should not call EmitDeclInit on a reference!"); 28 29 ASTContext &Context = CGF.getContext(); 30 31 CharUnits alignment = Context.getDeclAlign(&D); 32 QualType type = D.getType(); 33 LValue lv = CGF.MakeAddrLValue(DeclPtr, type, alignment); 34 35 const Expr *Init = D.getInit(); 36 if (!CGF.hasAggregateLLVMType(type)) { 37 CodeGenModule &CGM = CGF.CGM; 38 if (lv.isObjCStrong()) 39 CGM.getObjCRuntime().EmitObjCGlobalAssign(CGF, CGF.EmitScalarExpr(Init), 40 DeclPtr, D.isThreadSpecified()); 41 else if (lv.isObjCWeak()) 42 CGM.getObjCRuntime().EmitObjCWeakAssign(CGF, CGF.EmitScalarExpr(Init), 43 DeclPtr); 44 else 45 CGF.EmitScalarInit(Init, &D, lv, false); 46 } else if (type->isAnyComplexType()) { 47 CGF.EmitComplexExprIntoAddr(Init, DeclPtr, lv.isVolatile()); 48 } else { 49 CGF.EmitAggExpr(Init, AggValueSlot::forLValue(lv,AggValueSlot::IsDestructed, 50 AggValueSlot::DoesNotNeedGCBarriers, 51 AggValueSlot::IsNotAliased)); 52 } 53 } 54 55 /// Emit code to cause the destruction of the given variable with 56 /// static storage duration. 57 static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D, 58 llvm::Constant *addr) { 59 CodeGenModule &CGM = CGF.CGM; 60 61 // FIXME: __attribute__((cleanup)) ? 62 63 QualType type = D.getType(); 64 QualType::DestructionKind dtorKind = type.isDestructedType(); 65 66 switch (dtorKind) { 67 case QualType::DK_none: 68 return; 69 70 case QualType::DK_cxx_destructor: 71 break; 72 73 case QualType::DK_objc_strong_lifetime: 74 case QualType::DK_objc_weak_lifetime: 75 // We don't care about releasing objects during process teardown. 76 return; 77 } 78 79 llvm::Constant *function; 80 llvm::Constant *argument; 81 82 // Special-case non-array C++ destructors, where there's a function 83 // with the right signature that we can just call. 84 const CXXRecordDecl *record = 0; 85 if (dtorKind == QualType::DK_cxx_destructor && 86 (record = type->getAsCXXRecordDecl())) { 87 assert(!record->hasTrivialDestructor()); 88 CXXDestructorDecl *dtor = record->getDestructor(); 89 90 function = CGM.GetAddrOfCXXDestructor(dtor, Dtor_Complete); 91 argument = addr; 92 93 // Otherwise, the standard logic requires a helper function. 94 } else { 95 function = CodeGenFunction(CGM).generateDestroyHelper(addr, type, 96 CGF.getDestroyer(dtorKind), 97 CGF.needsEHCleanup(dtorKind)); 98 argument = llvm::Constant::getNullValue(CGF.Int8PtrTy); 99 } 100 101 CGF.EmitCXXGlobalDtorRegistration(function, argument); 102 } 103 104 /// Emit code to cause the variable at the given address to be considered as 105 /// constant from this point onwards. 106 static void EmitDeclInvariant(CodeGenFunction &CGF, const VarDecl &D, 107 llvm::Constant *Addr) { 108 // Don't emit the intrinsic if we're not optimizing. 109 if (!CGF.CGM.getCodeGenOpts().OptimizationLevel) 110 return; 111 112 // Grab the llvm.invariant.start intrinsic. 113 llvm::Intrinsic::ID InvStartID = llvm::Intrinsic::invariant_start; 114 llvm::Constant *InvariantStart = CGF.CGM.getIntrinsic(InvStartID); 115 116 // Emit a call with the size in bytes of the object. 117 CharUnits WidthChars = CGF.getContext().getTypeSizeInChars(D.getType()); 118 uint64_t Width = WidthChars.getQuantity(); 119 llvm::Value *Args[2] = { llvm::ConstantInt::getSigned(CGF.Int64Ty, Width), 120 llvm::ConstantExpr::getBitCast(Addr, CGF.Int8PtrTy)}; 121 CGF.Builder.CreateCall(InvariantStart, Args); 122 } 123 124 void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D, 125 llvm::Constant *DeclPtr, 126 bool PerformInit) { 127 128 const Expr *Init = D.getInit(); 129 QualType T = D.getType(); 130 131 if (!T->isReferenceType()) { 132 if (PerformInit) 133 EmitDeclInit(*this, D, DeclPtr); 134 if (CGM.isTypeConstant(D.getType(), true)) 135 EmitDeclInvariant(*this, D, DeclPtr); 136 else 137 EmitDeclDestroy(*this, D, DeclPtr); 138 return; 139 } 140 141 assert(PerformInit && "cannot have constant initializer which needs " 142 "destruction for reference"); 143 unsigned Alignment = getContext().getDeclAlign(&D).getQuantity(); 144 RValue RV = EmitReferenceBindingToExpr(Init, &D); 145 EmitStoreOfScalar(RV.getScalarVal(), DeclPtr, false, Alignment, T); 146 } 147 148 void 149 CodeGenFunction::EmitCXXGlobalDtorRegistration(llvm::Constant *DtorFn, 150 llvm::Constant *DeclPtr) { 151 // Generate a global destructor entry if not using __cxa_atexit. 152 if (!CGM.getCodeGenOpts().CXAAtExit) { 153 CGM.AddCXXDtorEntry(DtorFn, DeclPtr); 154 return; 155 } 156 157 // Get the destructor function type 158 llvm::Type *DtorFnTy = llvm::FunctionType::get(VoidTy, Int8PtrTy, false); 159 DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy); 160 161 llvm::Type *Params[] = { DtorFnTy, Int8PtrTy, Int8PtrTy }; 162 163 // Get the __cxa_atexit function type 164 // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d ); 165 llvm::FunctionType *AtExitFnTy = 166 llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false); 167 168 llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy, 169 "__cxa_atexit"); 170 if (llvm::Function *Fn = dyn_cast<llvm::Function>(AtExitFn)) 171 Fn->setDoesNotThrow(); 172 173 llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy, 174 "__dso_handle"); 175 llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy), 176 llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy), 177 llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) }; 178 Builder.CreateCall(AtExitFn, Args); 179 } 180 181 void CodeGenFunction::EmitCXXGuardedInit(const VarDecl &D, 182 llvm::GlobalVariable *DeclPtr, 183 bool PerformInit) { 184 // If we've been asked to forbid guard variables, emit an error now. 185 // This diagnostic is hard-coded for Darwin's use case; we can find 186 // better phrasing if someone else needs it. 187 if (CGM.getCodeGenOpts().ForbidGuardVariables) 188 CGM.Error(D.getLocation(), 189 "this initialization requires a guard variable, which " 190 "the kernel does not support"); 191 192 CGM.getCXXABI().EmitGuardedInit(*this, D, DeclPtr, PerformInit); 193 } 194 195 static llvm::Function * 196 CreateGlobalInitOrDestructFunction(CodeGenModule &CGM, 197 llvm::FunctionType *FTy, 198 StringRef Name) { 199 llvm::Function *Fn = 200 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage, 201 Name, &CGM.getModule()); 202 if (!CGM.getContext().getLangOptions().AppleKext) { 203 // Set the section if needed. 204 if (const char *Section = 205 CGM.getContext().getTargetInfo().getStaticInitSectionSpecifier()) 206 Fn->setSection(Section); 207 } 208 209 if (!CGM.getLangOptions().Exceptions) 210 Fn->setDoesNotThrow(); 211 212 return Fn; 213 } 214 215 void 216 CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D, 217 llvm::GlobalVariable *Addr, 218 bool PerformInit) { 219 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false); 220 221 // Create a variable initialization function. 222 llvm::Function *Fn = 223 CreateGlobalInitOrDestructFunction(*this, FTy, "__cxx_global_var_init"); 224 225 CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr, 226 PerformInit); 227 228 if (D->hasAttr<InitPriorityAttr>()) { 229 unsigned int order = D->getAttr<InitPriorityAttr>()->getPriority(); 230 OrderGlobalInits Key(order, PrioritizedCXXGlobalInits.size()); 231 PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn)); 232 DelayedCXXInitPosition.erase(D); 233 } 234 else { 235 llvm::DenseMap<const Decl *, unsigned>::iterator I = 236 DelayedCXXInitPosition.find(D); 237 if (I == DelayedCXXInitPosition.end()) { 238 CXXGlobalInits.push_back(Fn); 239 } else { 240 assert(CXXGlobalInits[I->second] == 0); 241 CXXGlobalInits[I->second] = Fn; 242 DelayedCXXInitPosition.erase(I); 243 } 244 } 245 } 246 247 void 248 CodeGenModule::EmitCXXGlobalInitFunc() { 249 while (!CXXGlobalInits.empty() && !CXXGlobalInits.back()) 250 CXXGlobalInits.pop_back(); 251 252 if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty()) 253 return; 254 255 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false); 256 257 // Create our global initialization function. 258 llvm::Function *Fn = 259 CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__I_a"); 260 261 if (!PrioritizedCXXGlobalInits.empty()) { 262 SmallVector<llvm::Constant*, 8> LocalCXXGlobalInits; 263 llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(), 264 PrioritizedCXXGlobalInits.end()); 265 for (unsigned i = 0; i < PrioritizedCXXGlobalInits.size(); i++) { 266 llvm::Function *Fn = PrioritizedCXXGlobalInits[i].second; 267 LocalCXXGlobalInits.push_back(Fn); 268 } 269 LocalCXXGlobalInits.append(CXXGlobalInits.begin(), CXXGlobalInits.end()); 270 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, 271 &LocalCXXGlobalInits[0], 272 LocalCXXGlobalInits.size()); 273 } 274 else 275 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, 276 &CXXGlobalInits[0], 277 CXXGlobalInits.size()); 278 AddGlobalCtor(Fn); 279 CXXGlobalInits.clear(); 280 PrioritizedCXXGlobalInits.clear(); 281 } 282 283 void CodeGenModule::EmitCXXGlobalDtorFunc() { 284 if (CXXGlobalDtors.empty()) 285 return; 286 287 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false); 288 289 // Create our global destructor function. 290 llvm::Function *Fn = 291 CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__D_a"); 292 293 CodeGenFunction(*this).GenerateCXXGlobalDtorFunc(Fn, CXXGlobalDtors); 294 AddGlobalDtor(Fn); 295 } 296 297 /// Emit the code necessary to initialize the given global variable. 298 void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn, 299 const VarDecl *D, 300 llvm::GlobalVariable *Addr, 301 bool PerformInit) { 302 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, 303 getTypes().arrangeNullaryFunction(), 304 FunctionArgList(), SourceLocation()); 305 306 // Use guarded initialization if the global variable is weak. This 307 // occurs for, e.g., instantiated static data members and 308 // definitions explicitly marked weak. 309 if (Addr->getLinkage() == llvm::GlobalValue::WeakODRLinkage || 310 Addr->getLinkage() == llvm::GlobalValue::WeakAnyLinkage) { 311 EmitCXXGuardedInit(*D, Addr, PerformInit); 312 } else { 313 EmitCXXGlobalVarDeclInit(*D, Addr, PerformInit); 314 } 315 316 FinishFunction(); 317 } 318 319 void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn, 320 llvm::Constant **Decls, 321 unsigned NumDecls) { 322 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, 323 getTypes().arrangeNullaryFunction(), 324 FunctionArgList(), SourceLocation()); 325 326 RunCleanupsScope Scope(*this); 327 328 // When building in Objective-C++ ARC mode, create an autorelease pool 329 // around the global initializers. 330 if (getLangOptions().ObjCAutoRefCount && getLangOptions().CPlusPlus) { 331 llvm::Value *token = EmitObjCAutoreleasePoolPush(); 332 EmitObjCAutoreleasePoolCleanup(token); 333 } 334 335 for (unsigned i = 0; i != NumDecls; ++i) 336 if (Decls[i]) 337 Builder.CreateCall(Decls[i]); 338 339 Scope.ForceCleanup(); 340 341 FinishFunction(); 342 } 343 344 void CodeGenFunction::GenerateCXXGlobalDtorFunc(llvm::Function *Fn, 345 const std::vector<std::pair<llvm::WeakVH, llvm::Constant*> > 346 &DtorsAndObjects) { 347 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, 348 getTypes().arrangeNullaryFunction(), 349 FunctionArgList(), SourceLocation()); 350 351 // Emit the dtors, in reverse order from construction. 352 for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; ++i) { 353 llvm::Value *Callee = DtorsAndObjects[e - i - 1].first; 354 llvm::CallInst *CI = Builder.CreateCall(Callee, 355 DtorsAndObjects[e - i - 1].second); 356 // Make sure the call and the callee agree on calling convention. 357 if (llvm::Function *F = dyn_cast<llvm::Function>(Callee)) 358 CI->setCallingConv(F->getCallingConv()); 359 } 360 361 FinishFunction(); 362 } 363 364 /// generateDestroyHelper - Generates a helper function which, when 365 /// invoked, destroys the given object. 366 llvm::Function * 367 CodeGenFunction::generateDestroyHelper(llvm::Constant *addr, 368 QualType type, 369 Destroyer *destroyer, 370 bool useEHCleanupForArray) { 371 FunctionArgList args; 372 ImplicitParamDecl dst(0, SourceLocation(), 0, getContext().VoidPtrTy); 373 args.push_back(&dst); 374 375 const CGFunctionInfo &FI = 376 CGM.getTypes().arrangeFunctionDeclaration(getContext().VoidTy, args, 377 FunctionType::ExtInfo(), 378 /*variadic*/ false); 379 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); 380 llvm::Function *fn = 381 CreateGlobalInitOrDestructFunction(CGM, FTy, "__cxx_global_array_dtor"); 382 383 StartFunction(GlobalDecl(), getContext().VoidTy, fn, FI, args, 384 SourceLocation()); 385 386 emitDestroy(addr, type, destroyer, useEHCleanupForArray); 387 388 FinishFunction(); 389 390 return fn; 391 } 392