Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 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" |
Chandler Carruth | 06057ce | 2010-06-15 23:19:56 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/CodeGenOptions.h" |
Douglas Gregor | 86a3a03 | 2010-05-16 01:24:12 +0000 | [diff] [blame] | 16 | #include "llvm/Intrinsics.h" |
| 17 | |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | using namespace CodeGen; |
| 20 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 21 | static void EmitDeclInit(CodeGenFunction &CGF, const VarDecl &D, |
| 22 | llvm::Constant *DeclPtr) { |
| 23 | assert(D.hasGlobalStorage() && "VarDecl must have global storage!"); |
| 24 | assert(!D.getType()->isReferenceType() && |
| 25 | "Should not call EmitDeclInit on a reference!"); |
| 26 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 27 | ASTContext &Context = CGF.getContext(); |
| 28 | |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 29 | const Expr *Init = D.getInit(); |
| 30 | QualType T = D.getType(); |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 31 | bool isVolatile = Context.getCanonicalType(T).isVolatileQualified(); |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 32 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 33 | if (!CGF.hasAggregateLLVMType(T)) { |
| 34 | llvm::Value *V = CGF.EmitScalarExpr(Init); |
| 35 | CGF.EmitStoreOfScalar(V, DeclPtr, isVolatile, T); |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 36 | } else if (T->isAnyComplexType()) { |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 37 | CGF.EmitComplexExprIntoAddr(Init, DeclPtr, isVolatile); |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 38 | } else { |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 39 | CGF.EmitAggExpr(Init, DeclPtr, isVolatile); |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 43 | static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D, |
| 44 | llvm::Constant *DeclPtr) { |
| 45 | CodeGenModule &CGM = CGF.CGM; |
| 46 | ASTContext &Context = CGF.getContext(); |
| 47 | |
| 48 | const Expr *Init = D.getInit(); |
| 49 | QualType T = D.getType(); |
| 50 | if (!CGF.hasAggregateLLVMType(T) || T->isAnyComplexType()) |
| 51 | return; |
| 52 | |
| 53 | // Avoid generating destructor(s) for initialized objects. |
| 54 | if (!isa<CXXConstructExpr>(Init)) |
| 55 | return; |
| 56 | |
| 57 | const ConstantArrayType *Array = Context.getAsConstantArrayType(T); |
| 58 | if (Array) |
| 59 | T = Context.getBaseElementType(Array); |
| 60 | |
| 61 | const RecordType *RT = T->getAs<RecordType>(); |
| 62 | if (!RT) |
| 63 | return; |
| 64 | |
| 65 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 66 | if (RD->hasTrivialDestructor()) |
| 67 | return; |
| 68 | |
| 69 | CXXDestructorDecl *Dtor = RD->getDestructor(Context); |
| 70 | |
| 71 | llvm::Constant *DtorFn; |
| 72 | if (Array) { |
| 73 | DtorFn = |
Anders Carlsson | 02e370a | 2010-06-08 22:14:59 +0000 | [diff] [blame] | 74 | CodeGenFunction(CGM).GenerateCXXAggrDestructorHelper(Dtor, Array, |
| 75 | DeclPtr); |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 76 | const llvm::Type *Int8PtrTy = |
Anders Carlsson | 02e370a | 2010-06-08 22:14:59 +0000 | [diff] [blame] | 77 | llvm::Type::getInt8PtrTy(CGM.getLLVMContext()); |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 78 | DeclPtr = llvm::Constant::getNullValue(Int8PtrTy); |
| 79 | } else |
| 80 | DtorFn = CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete); |
| 81 | |
| 82 | CGF.EmitCXXGlobalDtorRegistration(DtorFn, DeclPtr); |
| 83 | } |
| 84 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 85 | void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D, |
| 86 | llvm::Constant *DeclPtr) { |
| 87 | |
| 88 | const Expr *Init = D.getInit(); |
| 89 | QualType T = D.getType(); |
| 90 | |
| 91 | if (!T->isReferenceType()) { |
| 92 | EmitDeclInit(*this, D, DeclPtr); |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 93 | EmitDeclDestroy(*this, D, DeclPtr); |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 94 | return; |
| 95 | } |
Fariborz Jahanian | 616c173 | 2010-01-25 21:40:39 +0000 | [diff] [blame] | 96 | if (Init->isLvalue(getContext()) == Expr::LV_Valid) { |
Anders Carlsson | a64a869 | 2010-02-03 16:38:03 +0000 | [diff] [blame] | 97 | RValue RV = EmitReferenceBindingToExpr(Init, /*IsInitializer=*/true); |
Fariborz Jahanian | 616c173 | 2010-01-25 21:40:39 +0000 | [diff] [blame] | 98 | EmitStoreOfScalar(RV.getScalarVal(), DeclPtr, false, T); |
| 99 | return; |
| 100 | } |
| 101 | ErrorUnsupported(Init, |
| 102 | "global variable that binds reference to a non-lvalue"); |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 103 | } |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 104 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 105 | void |
| 106 | CodeGenFunction::EmitCXXGlobalDtorRegistration(llvm::Constant *DtorFn, |
| 107 | llvm::Constant *DeclPtr) { |
| 108 | // Generate a global destructor entry if not using __cxa_atexit. |
| 109 | if (!CGM.getCodeGenOpts().CXAAtExit) { |
| 110 | CGM.AddCXXDtorEntry(DtorFn, DeclPtr); |
| 111 | return; |
| 112 | } |
| 113 | |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 114 | const llvm::Type *Int8PtrTy = |
| 115 | llvm::Type::getInt8Ty(VMContext)->getPointerTo(); |
| 116 | |
| 117 | std::vector<const llvm::Type *> Params; |
| 118 | Params.push_back(Int8PtrTy); |
| 119 | |
| 120 | // Get the destructor function type |
| 121 | const llvm::Type *DtorFnTy = |
| 122 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false); |
| 123 | DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy); |
| 124 | |
| 125 | Params.clear(); |
| 126 | Params.push_back(DtorFnTy); |
| 127 | Params.push_back(Int8PtrTy); |
| 128 | Params.push_back(Int8PtrTy); |
| 129 | |
| 130 | // Get the __cxa_atexit function type |
| 131 | // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d ); |
| 132 | const llvm::FunctionType *AtExitFnTy = |
| 133 | llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false); |
| 134 | |
| 135 | llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy, |
| 136 | "__cxa_atexit"); |
| 137 | |
| 138 | llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy, |
| 139 | "__dso_handle"); |
| 140 | llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy), |
| 141 | llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy), |
| 142 | llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) }; |
| 143 | Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args)); |
| 144 | } |
| 145 | |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 146 | static llvm::Function * |
| 147 | CreateGlobalInitOrDestructFunction(CodeGenModule &CGM, |
| 148 | const llvm::FunctionType *FTy, |
| 149 | llvm::StringRef Name) { |
| 150 | llvm::Function *Fn = |
| 151 | llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage, |
| 152 | Name, &CGM.getModule()); |
| 153 | |
Anders Carlsson | 18af368 | 2010-06-08 22:47:50 +0000 | [diff] [blame] | 154 | // Set the section if needed. |
| 155 | if (const char *Section = |
| 156 | CGM.getContext().Target.getStaticInitSectionSpecifier()) |
| 157 | Fn->setSection(Section); |
| 158 | |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 159 | return Fn; |
| 160 | } |
| 161 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 162 | void |
| 163 | CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D) { |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 164 | const llvm::FunctionType *FTy |
| 165 | = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
| 166 | false); |
| 167 | |
| 168 | // Create a variable initialization function. |
| 169 | llvm::Function *Fn = |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 170 | CreateGlobalInitOrDestructFunction(*this, FTy, "__cxx_global_var_init"); |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 171 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 172 | CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D); |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 173 | |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 174 | if (D->hasAttr<InitPriorityAttr>()) { |
| 175 | unsigned int order = D->getAttr<InitPriorityAttr>()->getPriority(); |
| 176 | PrioritizedCXXGlobalInits.push_back(std::make_pair(order,Fn)); |
| 177 | } |
| 178 | else |
| 179 | CXXGlobalInits.push_back(Fn); |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 182 | void |
| 183 | CodeGenModule::EmitCXXGlobalInitFunc() { |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 184 | if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty()) |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 185 | return; |
| 186 | |
| 187 | const llvm::FunctionType *FTy |
| 188 | = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
| 189 | false); |
| 190 | |
| 191 | // Create our global initialization function. |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 192 | llvm::Function *Fn = |
| 193 | CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__I_a"); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 194 | |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 195 | if (!PrioritizedCXXGlobalInits.empty()) { |
| 196 | std::vector<llvm::Constant*> LocalCXXGlobalInits; |
| 197 | std::sort(PrioritizedCXXGlobalInits.begin(), |
| 198 | PrioritizedCXXGlobalInits.end()); |
| 199 | for (unsigned i = 0; i < PrioritizedCXXGlobalInits.size(); i++) { |
| 200 | llvm::Function *Fn = PrioritizedCXXGlobalInits[i].second; |
| 201 | LocalCXXGlobalInits.push_back(Fn); |
| 202 | } |
| 203 | for (unsigned i = 0; i < CXXGlobalInits.size(); i++) |
| 204 | LocalCXXGlobalInits.push_back(CXXGlobalInits[i]); |
| 205 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, |
| 206 | &LocalCXXGlobalInits[0], |
| 207 | LocalCXXGlobalInits.size()); |
| 208 | } |
| 209 | else |
| 210 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, |
| 211 | &CXXGlobalInits[0], |
| 212 | CXXGlobalInits.size()); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 213 | AddGlobalCtor(Fn); |
| 214 | } |
| 215 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 216 | void CodeGenModule::EmitCXXGlobalDtorFunc() { |
| 217 | if (CXXGlobalDtors.empty()) |
| 218 | return; |
| 219 | |
| 220 | const llvm::FunctionType *FTy |
| 221 | = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
| 222 | false); |
| 223 | |
| 224 | // Create our global destructor function. |
| 225 | llvm::Function *Fn = |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 226 | CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__D_a"); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 227 | |
| 228 | CodeGenFunction(*this).GenerateCXXGlobalDtorFunc(Fn, CXXGlobalDtors); |
| 229 | AddGlobalDtor(Fn); |
| 230 | } |
| 231 | |
| 232 | void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn, |
| 233 | const VarDecl *D) { |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 234 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(), |
| 235 | SourceLocation()); |
| 236 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 237 | llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D); |
| 238 | EmitCXXGlobalVarDeclInit(*D, DeclPtr); |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 239 | |
| 240 | FinishFunction(); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 241 | } |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 242 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 243 | void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn, |
| 244 | llvm::Constant **Decls, |
| 245 | unsigned NumDecls) { |
| 246 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(), |
| 247 | SourceLocation()); |
| 248 | |
| 249 | for (unsigned i = 0; i != NumDecls; ++i) |
| 250 | Builder.CreateCall(Decls[i]); |
| 251 | |
| 252 | FinishFunction(); |
| 253 | } |
| 254 | |
| 255 | void CodeGenFunction::GenerateCXXGlobalDtorFunc(llvm::Function *Fn, |
Chris Lattner | 810112e | 2010-06-19 05:52:45 +0000 | [diff] [blame] | 256 | const std::vector<std::pair<llvm::WeakVH, llvm::Constant*> > |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 257 | &DtorsAndObjects) { |
| 258 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(), |
| 259 | SourceLocation()); |
| 260 | |
| 261 | // Emit the dtors, in reverse order from construction. |
Chris Lattner | c9a85f9 | 2010-04-26 20:35:54 +0000 | [diff] [blame] | 262 | for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; ++i) { |
Chris Lattner | 810112e | 2010-06-19 05:52:45 +0000 | [diff] [blame] | 263 | llvm::Value *Callee = DtorsAndObjects[e - i - 1].first; |
Chris Lattner | c9a85f9 | 2010-04-26 20:35:54 +0000 | [diff] [blame] | 264 | llvm::CallInst *CI = Builder.CreateCall(Callee, |
| 265 | DtorsAndObjects[e - i - 1].second); |
| 266 | // Make sure the call and the callee agree on calling convention. |
| 267 | if (llvm::Function *F = dyn_cast<llvm::Function>(Callee)) |
| 268 | CI->setCallingConv(F->getCallingConv()); |
| 269 | } |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 270 | |
| 271 | FinishFunction(); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 274 | static llvm::Constant *getGuardAcquireFn(CodeGenFunction &CGF) { |
| 275 | // int __cxa_guard_acquire(__int64_t *guard_object); |
| 276 | |
| 277 | const llvm::Type *Int64PtrTy = |
| 278 | llvm::Type::getInt64PtrTy(CGF.getLLVMContext()); |
| 279 | |
| 280 | std::vector<const llvm::Type*> Args(1, Int64PtrTy); |
| 281 | |
| 282 | const llvm::FunctionType *FTy = |
| 283 | llvm::FunctionType::get(CGF.ConvertType(CGF.getContext().IntTy), |
| 284 | Args, /*isVarArg=*/false); |
| 285 | |
| 286 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire"); |
| 287 | } |
| 288 | |
| 289 | static llvm::Constant *getGuardReleaseFn(CodeGenFunction &CGF) { |
| 290 | // void __cxa_guard_release(__int64_t *guard_object); |
| 291 | |
| 292 | const llvm::Type *Int64PtrTy = |
| 293 | llvm::Type::getInt64PtrTy(CGF.getLLVMContext()); |
| 294 | |
| 295 | std::vector<const llvm::Type*> Args(1, Int64PtrTy); |
| 296 | |
| 297 | const llvm::FunctionType *FTy = |
| 298 | llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), |
| 299 | Args, /*isVarArg=*/false); |
| 300 | |
| 301 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release"); |
| 302 | } |
| 303 | |
| 304 | static llvm::Constant *getGuardAbortFn(CodeGenFunction &CGF) { |
| 305 | // void __cxa_guard_abort(__int64_t *guard_object); |
| 306 | |
| 307 | const llvm::Type *Int64PtrTy = |
| 308 | llvm::Type::getInt64PtrTy(CGF.getLLVMContext()); |
| 309 | |
| 310 | std::vector<const llvm::Type*> Args(1, Int64PtrTy); |
| 311 | |
| 312 | const llvm::FunctionType *FTy = |
| 313 | llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), |
| 314 | Args, /*isVarArg=*/false); |
| 315 | |
| 316 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort"); |
| 317 | } |
| 318 | |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 319 | void |
| 320 | CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D, |
| 321 | llvm::GlobalVariable *GV) { |
John McCall | fe67f3b | 2010-05-04 20:45:42 +0000 | [diff] [blame] | 322 | // Bail out early if this initializer isn't reachable. |
| 323 | if (!Builder.GetInsertBlock()) return; |
| 324 | |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 325 | bool ThreadsafeStatics = getContext().getLangOptions().ThreadsafeStatics; |
| 326 | |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 327 | llvm::SmallString<256> GuardVName; |
| 328 | CGM.getMangleContext().mangleGuardVariable(&D, GuardVName); |
| 329 | |
| 330 | // Create the guard variable. |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 331 | const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(VMContext); |
| 332 | llvm::GlobalValue *GuardVariable = |
| 333 | new llvm::GlobalVariable(CGM.getModule(), Int64Ty, |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 334 | false, GV->getLinkage(), |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 335 | llvm::Constant::getNullValue(Int64Ty), |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 336 | GuardVName.str()); |
| 337 | |
| 338 | // Load the first byte of the guard variable. |
| 339 | const llvm::Type *PtrTy |
| 340 | = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0); |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 341 | llvm::Value *V = |
| 342 | Builder.CreateLoad(Builder.CreateBitCast(GuardVariable, PtrTy), "tmp"); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 343 | |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 344 | llvm::BasicBlock *InitCheckBlock = createBasicBlock("init.check"); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 345 | llvm::BasicBlock *EndBlock = createBasicBlock("init.end"); |
| 346 | |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 347 | // Check if the first byte of the guard variable is zero. |
| 348 | Builder.CreateCondBr(Builder.CreateIsNull(V, "tobool"), |
| 349 | InitCheckBlock, EndBlock); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 350 | |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 351 | EmitBlock(InitCheckBlock); |
| 352 | |
Douglas Gregor | 86a3a03 | 2010-05-16 01:24:12 +0000 | [diff] [blame] | 353 | // Variables used when coping with thread-safe statics and exceptions. |
| 354 | llvm::BasicBlock *SavedLandingPad = 0; |
| 355 | llvm::BasicBlock *LandingPad = 0; |
| 356 | if (ThreadsafeStatics) { |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 357 | // Call __cxa_guard_acquire. |
| 358 | V = Builder.CreateCall(getGuardAcquireFn(*this), GuardVariable); |
| 359 | |
| 360 | llvm::BasicBlock *InitBlock = createBasicBlock("init"); |
| 361 | |
| 362 | Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"), |
| 363 | InitBlock, EndBlock); |
| 364 | |
Douglas Gregor | 1eb2e59 | 2010-05-16 00:44:00 +0000 | [diff] [blame] | 365 | if (Exceptions) { |
Douglas Gregor | 86a3a03 | 2010-05-16 01:24:12 +0000 | [diff] [blame] | 366 | SavedLandingPad = getInvokeDest(); |
| 367 | LandingPad = createBasicBlock("guard.lpad"); |
| 368 | setInvokeDest(LandingPad); |
Douglas Gregor | 1eb2e59 | 2010-05-16 00:44:00 +0000 | [diff] [blame] | 369 | } |
Douglas Gregor | 86a3a03 | 2010-05-16 01:24:12 +0000 | [diff] [blame] | 370 | |
| 371 | EmitBlock(InitBlock); |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 372 | } |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 373 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 374 | if (D.getType()->isReferenceType()) { |
Anders Carlsson | 864143f | 2009-12-10 01:58:33 +0000 | [diff] [blame] | 375 | QualType T = D.getType(); |
Anders Carlsson | c7974ca | 2009-12-10 01:05:11 +0000 | [diff] [blame] | 376 | // We don't want to pass true for IsInitializer here, because a static |
| 377 | // reference to a temporary does not extend its lifetime. |
Anders Carlsson | a64a869 | 2010-02-03 16:38:03 +0000 | [diff] [blame] | 378 | RValue RV = EmitReferenceBindingToExpr(D.getInit(), |
Anders Carlsson | 864143f | 2009-12-10 01:58:33 +0000 | [diff] [blame] | 379 | /*IsInitializer=*/false); |
| 380 | EmitStoreOfScalar(RV.getScalarVal(), GV, /*Volatile=*/false, T); |
| 381 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 382 | } else |
| 383 | EmitDeclInit(*this, D, GV); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 384 | |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 385 | if (ThreadsafeStatics) { |
| 386 | // Call __cxa_guard_release. |
Douglas Gregor | 86a3a03 | 2010-05-16 01:24:12 +0000 | [diff] [blame] | 387 | Builder.CreateCall(getGuardReleaseFn(*this), GuardVariable); |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 388 | } else { |
| 389 | llvm::Value *One = |
| 390 | llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 1); |
| 391 | Builder.CreateStore(One, Builder.CreateBitCast(GuardVariable, PtrTy)); |
| 392 | } |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 393 | |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 394 | // Register the call to the destructor. |
| 395 | if (!D.getType()->isReferenceType()) |
| 396 | EmitDeclDestroy(*this, D, GV); |
| 397 | |
Douglas Gregor | 86a3a03 | 2010-05-16 01:24:12 +0000 | [diff] [blame] | 398 | if (ThreadsafeStatics && Exceptions) { |
| 399 | // If an exception is thrown during initialization, call __cxa_guard_abort |
| 400 | // along the exceptional edge. |
| 401 | EmitBranch(EndBlock); |
| 402 | |
| 403 | // Construct the landing pad. |
| 404 | EmitBlock(LandingPad); |
| 405 | |
| 406 | // Personality function and LLVM intrinsics. |
| 407 | llvm::Constant *Personality = |
| 408 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty |
| 409 | (VMContext), |
| 410 | true), |
| 411 | "__gxx_personality_v0"); |
| 412 | Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty); |
| 413 | llvm::Value *llvm_eh_exception = |
| 414 | CGM.getIntrinsic(llvm::Intrinsic::eh_exception); |
| 415 | llvm::Value *llvm_eh_selector = |
| 416 | CGM.getIntrinsic(llvm::Intrinsic::eh_selector); |
| 417 | |
| 418 | // Exception object |
| 419 | llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc"); |
| 420 | llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow"); |
| 421 | |
| 422 | // Call the selector function. |
| 423 | const llvm::PointerType *PtrToInt8Ty |
| 424 | = llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext)); |
| 425 | llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty); |
| 426 | llvm::Value* SelectorArgs[3] = { Exc, Personality, Null }; |
| 427 | Builder.CreateCall(llvm_eh_selector, SelectorArgs, SelectorArgs + 3, |
| 428 | "selector"); |
| 429 | Builder.CreateStore(Exc, RethrowPtr); |
| 430 | |
| 431 | // Call __cxa_guard_abort along the exceptional edge. |
| 432 | Builder.CreateCall(getGuardAbortFn(*this), GuardVariable); |
| 433 | |
| 434 | setInvokeDest(SavedLandingPad); |
| 435 | |
| 436 | // Rethrow the current exception. |
| 437 | if (getInvokeDest()) { |
| 438 | llvm::BasicBlock *Cont = createBasicBlock("invoke.cont"); |
| 439 | Builder.CreateInvoke(getUnwindResumeOrRethrowFn(), Cont, |
| 440 | getInvokeDest(), |
| 441 | Builder.CreateLoad(RethrowPtr)); |
| 442 | EmitBlock(Cont); |
| 443 | } else |
| 444 | Builder.CreateCall(getUnwindResumeOrRethrowFn(), |
| 445 | Builder.CreateLoad(RethrowPtr)); |
| 446 | |
| 447 | Builder.CreateUnreachable(); |
| 448 | } |
| 449 | |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 450 | EmitBlock(EndBlock); |
| 451 | } |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 452 | |
| 453 | /// GenerateCXXAggrDestructorHelper - Generates a helper function which when |
| 454 | /// invoked, calls the default destructor on array elements in reverse order of |
| 455 | /// construction. |
| 456 | llvm::Function * |
| 457 | CodeGenFunction::GenerateCXXAggrDestructorHelper(const CXXDestructorDecl *D, |
| 458 | const ArrayType *Array, |
| 459 | llvm::Value *This) { |
| 460 | FunctionArgList Args; |
| 461 | ImplicitParamDecl *Dst = |
| 462 | ImplicitParamDecl::Create(getContext(), 0, |
| 463 | SourceLocation(), 0, |
| 464 | getContext().getPointerType(getContext().VoidTy)); |
| 465 | Args.push_back(std::make_pair(Dst, Dst->getType())); |
| 466 | |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 467 | const CGFunctionInfo &FI = |
| 468 | CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args, |
| 469 | FunctionType::ExtInfo()); |
| 470 | const llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI, false); |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 471 | llvm::Function *Fn = |
| 472 | CreateGlobalInitOrDestructFunction(CGM, FTy, "__cxx_global_array_dtor"); |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 473 | |
| 474 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, Args, SourceLocation()); |
| 475 | |
| 476 | QualType BaseElementTy = getContext().getBaseElementType(Array); |
| 477 | const llvm::Type *BasePtr = ConvertType(BaseElementTy)->getPointerTo(); |
| 478 | llvm::Value *BaseAddrPtr = Builder.CreateBitCast(This, BasePtr); |
| 479 | |
| 480 | EmitCXXAggrDestructorCall(D, Array, BaseAddrPtr); |
| 481 | |
| 482 | FinishFunction(); |
| 483 | |
| 484 | return Fn; |
| 485 | } |