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 | |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 48 | QualType T = D.getType(); |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 49 | |
John McCall | 85aca0f | 2010-07-30 04:56:58 +0000 | [diff] [blame] | 50 | // Drill down past array types. |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 51 | const ConstantArrayType *Array = Context.getAsConstantArrayType(T); |
| 52 | if (Array) |
| 53 | T = Context.getBaseElementType(Array); |
| 54 | |
John McCall | 85aca0f | 2010-07-30 04:56:58 +0000 | [diff] [blame] | 55 | /// If that's not a record, we're done. |
| 56 | /// FIXME: __attribute__((cleanup)) ? |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 57 | const RecordType *RT = T->getAs<RecordType>(); |
| 58 | if (!RT) |
| 59 | return; |
| 60 | |
| 61 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 62 | if (RD->hasTrivialDestructor()) |
| 63 | return; |
| 64 | |
Douglas Gregor | 1d110e0 | 2010-07-01 14:13:13 +0000 | [diff] [blame] | 65 | CXXDestructorDecl *Dtor = RD->getDestructor(); |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 66 | |
| 67 | llvm::Constant *DtorFn; |
| 68 | if (Array) { |
| 69 | DtorFn = |
Anders Carlsson | 02e370a | 2010-06-08 22:14:59 +0000 | [diff] [blame] | 70 | CodeGenFunction(CGM).GenerateCXXAggrDestructorHelper(Dtor, Array, |
| 71 | DeclPtr); |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 72 | const llvm::Type *Int8PtrTy = |
Anders Carlsson | 02e370a | 2010-06-08 22:14:59 +0000 | [diff] [blame] | 73 | llvm::Type::getInt8PtrTy(CGM.getLLVMContext()); |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 74 | DeclPtr = llvm::Constant::getNullValue(Int8PtrTy); |
| 75 | } else |
| 76 | DtorFn = CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete); |
| 77 | |
| 78 | CGF.EmitCXXGlobalDtorRegistration(DtorFn, DeclPtr); |
| 79 | } |
| 80 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 81 | void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D, |
| 82 | llvm::Constant *DeclPtr) { |
| 83 | |
| 84 | const Expr *Init = D.getInit(); |
| 85 | QualType T = D.getType(); |
| 86 | |
| 87 | if (!T->isReferenceType()) { |
| 88 | EmitDeclInit(*this, D, DeclPtr); |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 89 | EmitDeclDestroy(*this, D, DeclPtr); |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 90 | return; |
| 91 | } |
Anders Carlsson | 045a6d8 | 2010-06-27 17:52:15 +0000 | [diff] [blame] | 92 | |
| 93 | RValue RV = EmitReferenceBindingToExpr(Init, &D); |
| 94 | EmitStoreOfScalar(RV.getScalarVal(), DeclPtr, false, T); |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 95 | } |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 96 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 97 | void |
| 98 | CodeGenFunction::EmitCXXGlobalDtorRegistration(llvm::Constant *DtorFn, |
| 99 | llvm::Constant *DeclPtr) { |
| 100 | // Generate a global destructor entry if not using __cxa_atexit. |
| 101 | if (!CGM.getCodeGenOpts().CXAAtExit) { |
| 102 | CGM.AddCXXDtorEntry(DtorFn, DeclPtr); |
| 103 | return; |
| 104 | } |
| 105 | |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 106 | const llvm::Type *Int8PtrTy = |
| 107 | llvm::Type::getInt8Ty(VMContext)->getPointerTo(); |
| 108 | |
| 109 | std::vector<const llvm::Type *> Params; |
| 110 | Params.push_back(Int8PtrTy); |
| 111 | |
| 112 | // Get the destructor function type |
| 113 | const llvm::Type *DtorFnTy = |
| 114 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false); |
| 115 | DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy); |
| 116 | |
| 117 | Params.clear(); |
| 118 | Params.push_back(DtorFnTy); |
| 119 | Params.push_back(Int8PtrTy); |
| 120 | Params.push_back(Int8PtrTy); |
| 121 | |
| 122 | // Get the __cxa_atexit function type |
| 123 | // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d ); |
| 124 | const llvm::FunctionType *AtExitFnTy = |
| 125 | llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false); |
| 126 | |
| 127 | llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy, |
| 128 | "__cxa_atexit"); |
| 129 | |
| 130 | llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy, |
| 131 | "__dso_handle"); |
| 132 | llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy), |
| 133 | llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy), |
| 134 | llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) }; |
| 135 | Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args)); |
| 136 | } |
| 137 | |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 138 | static llvm::Function * |
| 139 | CreateGlobalInitOrDestructFunction(CodeGenModule &CGM, |
| 140 | const llvm::FunctionType *FTy, |
| 141 | llvm::StringRef Name) { |
| 142 | llvm::Function *Fn = |
| 143 | llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage, |
| 144 | Name, &CGM.getModule()); |
| 145 | |
Anders Carlsson | 18af368 | 2010-06-08 22:47:50 +0000 | [diff] [blame] | 146 | // Set the section if needed. |
| 147 | if (const char *Section = |
| 148 | CGM.getContext().Target.getStaticInitSectionSpecifier()) |
| 149 | Fn->setSection(Section); |
| 150 | |
John McCall | 044cc54 | 2010-07-06 04:38:10 +0000 | [diff] [blame] | 151 | if (!CGM.getLangOptions().Exceptions) |
| 152 | Fn->setDoesNotThrow(); |
| 153 | |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 154 | return Fn; |
| 155 | } |
| 156 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 157 | void |
| 158 | CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D) { |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 159 | const llvm::FunctionType *FTy |
| 160 | = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
| 161 | false); |
| 162 | |
| 163 | // Create a variable initialization function. |
| 164 | llvm::Function *Fn = |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 165 | CreateGlobalInitOrDestructFunction(*this, FTy, "__cxx_global_var_init"); |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 166 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 167 | CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D); |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 168 | |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 169 | if (D->hasAttr<InitPriorityAttr>()) { |
| 170 | unsigned int order = D->getAttr<InitPriorityAttr>()->getPriority(); |
Chris Lattner | ec2830d | 2010-06-27 06:32:58 +0000 | [diff] [blame] | 171 | OrderGlobalInits Key(order, PrioritizedCXXGlobalInits.size()); |
Fariborz Jahanian | e0b691a | 2010-06-21 21:27:42 +0000 | [diff] [blame] | 172 | PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn)); |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 173 | DelayedCXXInitPosition.erase(D); |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 174 | } |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 175 | else { |
| 176 | llvm::DenseMap<const Decl *, unsigned>::iterator I = |
| 177 | DelayedCXXInitPosition.find(D); |
| 178 | if (I == DelayedCXXInitPosition.end()) { |
| 179 | CXXGlobalInits.push_back(Fn); |
| 180 | } else { |
| 181 | assert(CXXGlobalInits[I->second] == 0); |
| 182 | CXXGlobalInits[I->second] = Fn; |
| 183 | DelayedCXXInitPosition.erase(I); |
| 184 | } |
| 185 | } |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 188 | void |
| 189 | CodeGenModule::EmitCXXGlobalInitFunc() { |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 190 | while (!CXXGlobalInits.empty() && !CXXGlobalInits.back()) |
| 191 | CXXGlobalInits.pop_back(); |
| 192 | |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 193 | if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty()) |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 194 | return; |
| 195 | |
| 196 | const llvm::FunctionType *FTy |
| 197 | = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
| 198 | false); |
| 199 | |
| 200 | // Create our global initialization function. |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 201 | llvm::Function *Fn = |
| 202 | CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__I_a"); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 203 | |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 204 | if (!PrioritizedCXXGlobalInits.empty()) { |
Fariborz Jahanian | 027d7ed | 2010-06-21 19:49:38 +0000 | [diff] [blame] | 205 | llvm::SmallVector<llvm::Constant*, 8> LocalCXXGlobalInits; |
| 206 | llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(), |
Fariborz Jahanian | f489688 | 2010-06-22 00:23:08 +0000 | [diff] [blame] | 207 | PrioritizedCXXGlobalInits.end()); |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 208 | for (unsigned i = 0; i < PrioritizedCXXGlobalInits.size(); i++) { |
| 209 | llvm::Function *Fn = PrioritizedCXXGlobalInits[i].second; |
| 210 | LocalCXXGlobalInits.push_back(Fn); |
| 211 | } |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 212 | LocalCXXGlobalInits.append(CXXGlobalInits.begin(), CXXGlobalInits.end()); |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 213 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, |
| 214 | &LocalCXXGlobalInits[0], |
| 215 | LocalCXXGlobalInits.size()); |
| 216 | } |
| 217 | else |
| 218 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, |
| 219 | &CXXGlobalInits[0], |
| 220 | CXXGlobalInits.size()); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 221 | AddGlobalCtor(Fn); |
| 222 | } |
| 223 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 224 | void CodeGenModule::EmitCXXGlobalDtorFunc() { |
| 225 | if (CXXGlobalDtors.empty()) |
| 226 | return; |
| 227 | |
| 228 | const llvm::FunctionType *FTy |
| 229 | = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
| 230 | false); |
| 231 | |
| 232 | // Create our global destructor function. |
| 233 | llvm::Function *Fn = |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 234 | CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__D_a"); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 235 | |
| 236 | CodeGenFunction(*this).GenerateCXXGlobalDtorFunc(Fn, CXXGlobalDtors); |
| 237 | AddGlobalDtor(Fn); |
| 238 | } |
| 239 | |
| 240 | void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn, |
| 241 | const VarDecl *D) { |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 242 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(), |
| 243 | SourceLocation()); |
| 244 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 245 | llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D); |
| 246 | EmitCXXGlobalVarDeclInit(*D, DeclPtr); |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 247 | |
| 248 | FinishFunction(); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 249 | } |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 250 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 251 | void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn, |
| 252 | llvm::Constant **Decls, |
| 253 | unsigned NumDecls) { |
| 254 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(), |
| 255 | SourceLocation()); |
| 256 | |
| 257 | for (unsigned i = 0; i != NumDecls; ++i) |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 258 | if (Decls[i]) |
| 259 | Builder.CreateCall(Decls[i]); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 260 | |
| 261 | FinishFunction(); |
| 262 | } |
| 263 | |
| 264 | void CodeGenFunction::GenerateCXXGlobalDtorFunc(llvm::Function *Fn, |
Chris Lattner | 810112e | 2010-06-19 05:52:45 +0000 | [diff] [blame] | 265 | const std::vector<std::pair<llvm::WeakVH, llvm::Constant*> > |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 266 | &DtorsAndObjects) { |
| 267 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(), |
| 268 | SourceLocation()); |
| 269 | |
| 270 | // Emit the dtors, in reverse order from construction. |
Chris Lattner | c9a85f9 | 2010-04-26 20:35:54 +0000 | [diff] [blame] | 271 | for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; ++i) { |
Chris Lattner | 810112e | 2010-06-19 05:52:45 +0000 | [diff] [blame] | 272 | llvm::Value *Callee = DtorsAndObjects[e - i - 1].first; |
Chris Lattner | c9a85f9 | 2010-04-26 20:35:54 +0000 | [diff] [blame] | 273 | llvm::CallInst *CI = Builder.CreateCall(Callee, |
| 274 | DtorsAndObjects[e - i - 1].second); |
| 275 | // Make sure the call and the callee agree on calling convention. |
| 276 | if (llvm::Function *F = dyn_cast<llvm::Function>(Callee)) |
| 277 | CI->setCallingConv(F->getCallingConv()); |
| 278 | } |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 279 | |
| 280 | FinishFunction(); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 283 | static llvm::Constant *getGuardAcquireFn(CodeGenFunction &CGF) { |
| 284 | // int __cxa_guard_acquire(__int64_t *guard_object); |
| 285 | |
| 286 | const llvm::Type *Int64PtrTy = |
| 287 | llvm::Type::getInt64PtrTy(CGF.getLLVMContext()); |
| 288 | |
| 289 | std::vector<const llvm::Type*> Args(1, Int64PtrTy); |
| 290 | |
| 291 | const llvm::FunctionType *FTy = |
| 292 | llvm::FunctionType::get(CGF.ConvertType(CGF.getContext().IntTy), |
| 293 | Args, /*isVarArg=*/false); |
| 294 | |
| 295 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire"); |
| 296 | } |
| 297 | |
| 298 | static llvm::Constant *getGuardReleaseFn(CodeGenFunction &CGF) { |
| 299 | // void __cxa_guard_release(__int64_t *guard_object); |
| 300 | |
| 301 | const llvm::Type *Int64PtrTy = |
| 302 | llvm::Type::getInt64PtrTy(CGF.getLLVMContext()); |
| 303 | |
| 304 | std::vector<const llvm::Type*> Args(1, Int64PtrTy); |
| 305 | |
| 306 | const llvm::FunctionType *FTy = |
| 307 | llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), |
| 308 | Args, /*isVarArg=*/false); |
| 309 | |
| 310 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release"); |
| 311 | } |
| 312 | |
| 313 | static llvm::Constant *getGuardAbortFn(CodeGenFunction &CGF) { |
| 314 | // void __cxa_guard_abort(__int64_t *guard_object); |
| 315 | |
| 316 | const llvm::Type *Int64PtrTy = |
| 317 | llvm::Type::getInt64PtrTy(CGF.getLLVMContext()); |
| 318 | |
| 319 | std::vector<const llvm::Type*> Args(1, Int64PtrTy); |
| 320 | |
| 321 | const llvm::FunctionType *FTy = |
| 322 | llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), |
| 323 | Args, /*isVarArg=*/false); |
| 324 | |
| 325 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort"); |
| 326 | } |
| 327 | |
John McCall | e540e63 | 2010-07-21 06:20:50 +0000 | [diff] [blame] | 328 | namespace { |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 329 | struct CallGuardAbort : EHScopeStack::Cleanup { |
John McCall | e540e63 | 2010-07-21 06:20:50 +0000 | [diff] [blame] | 330 | llvm::GlobalVariable *Guard; |
| 331 | CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {} |
| 332 | |
| 333 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 334 | // It shouldn't be possible for this to throw, but if it can, |
| 335 | // this should allow for the possibility of an invoke. |
| 336 | CGF.Builder.CreateCall(getGuardAbortFn(CGF), Guard) |
| 337 | ->setDoesNotThrow(); |
| 338 | } |
| 339 | }; |
| 340 | } |
| 341 | |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 342 | void |
| 343 | CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D, |
| 344 | llvm::GlobalVariable *GV) { |
John McCall | fe67f3b | 2010-05-04 20:45:42 +0000 | [diff] [blame] | 345 | // Bail out early if this initializer isn't reachable. |
| 346 | if (!Builder.GetInsertBlock()) return; |
| 347 | |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 348 | bool ThreadsafeStatics = getContext().getLangOptions().ThreadsafeStatics; |
| 349 | |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 350 | llvm::SmallString<256> GuardVName; |
| 351 | CGM.getMangleContext().mangleGuardVariable(&D, GuardVName); |
| 352 | |
| 353 | // Create the guard variable. |
John McCall | e540e63 | 2010-07-21 06:20:50 +0000 | [diff] [blame] | 354 | llvm::GlobalVariable *GuardVariable = |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 355 | new llvm::GlobalVariable(CGM.getModule(), Int64Ty, |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 356 | false, GV->getLinkage(), |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 357 | llvm::Constant::getNullValue(Int64Ty), |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 358 | GuardVName.str()); |
| 359 | |
| 360 | // Load the first byte of the guard variable. |
| 361 | const llvm::Type *PtrTy |
| 362 | = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0); |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 363 | llvm::Value *V = |
| 364 | Builder.CreateLoad(Builder.CreateBitCast(GuardVariable, PtrTy), "tmp"); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 365 | |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 366 | llvm::BasicBlock *InitCheckBlock = createBasicBlock("init.check"); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 367 | llvm::BasicBlock *EndBlock = createBasicBlock("init.end"); |
| 368 | |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 369 | // Check if the first byte of the guard variable is zero. |
| 370 | Builder.CreateCondBr(Builder.CreateIsNull(V, "tobool"), |
| 371 | InitCheckBlock, EndBlock); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 372 | |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 373 | EmitBlock(InitCheckBlock); |
| 374 | |
Douglas Gregor | 86a3a03 | 2010-05-16 01:24:12 +0000 | [diff] [blame] | 375 | // Variables used when coping with thread-safe statics and exceptions. |
Douglas Gregor | 86a3a03 | 2010-05-16 01:24:12 +0000 | [diff] [blame] | 376 | if (ThreadsafeStatics) { |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 377 | // Call __cxa_guard_acquire. |
| 378 | V = Builder.CreateCall(getGuardAcquireFn(*this), GuardVariable); |
| 379 | |
| 380 | llvm::BasicBlock *InitBlock = createBasicBlock("init"); |
| 381 | |
| 382 | Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"), |
| 383 | InitBlock, EndBlock); |
| 384 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 385 | // Call __cxa_guard_abort along the exceptional edge. |
John McCall | e540e63 | 2010-07-21 06:20:50 +0000 | [diff] [blame] | 386 | if (Exceptions) |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 387 | EHStack.pushCleanup<CallGuardAbort>(EHCleanup, GuardVariable); |
Douglas Gregor | 86a3a03 | 2010-05-16 01:24:12 +0000 | [diff] [blame] | 388 | |
| 389 | EmitBlock(InitBlock); |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 390 | } |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 391 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 392 | if (D.getType()->isReferenceType()) { |
Anders Carlsson | 864143f | 2009-12-10 01:58:33 +0000 | [diff] [blame] | 393 | QualType T = D.getType(); |
Anders Carlsson | 045a6d8 | 2010-06-27 17:52:15 +0000 | [diff] [blame] | 394 | RValue RV = EmitReferenceBindingToExpr(D.getInit(), &D); |
Anders Carlsson | 864143f | 2009-12-10 01:58:33 +0000 | [diff] [blame] | 395 | EmitStoreOfScalar(RV.getScalarVal(), GV, /*Volatile=*/false, T); |
| 396 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 397 | } else |
| 398 | EmitDeclInit(*this, D, GV); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 399 | |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 400 | if (ThreadsafeStatics) { |
John McCall | 224124c | 2010-08-10 18:51:44 +0000 | [diff] [blame^] | 401 | // Pop the guard-abort cleanup if we pushed one. |
| 402 | if (Exceptions) |
| 403 | PopCleanupBlock(); |
| 404 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 405 | // Call __cxa_guard_release. This cannot throw. |
Douglas Gregor | 86a3a03 | 2010-05-16 01:24:12 +0000 | [diff] [blame] | 406 | Builder.CreateCall(getGuardReleaseFn(*this), GuardVariable); |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 407 | } else { |
| 408 | llvm::Value *One = |
| 409 | llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 1); |
| 410 | Builder.CreateStore(One, Builder.CreateBitCast(GuardVariable, PtrTy)); |
| 411 | } |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 412 | |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 413 | // Register the call to the destructor. |
| 414 | if (!D.getType()->isReferenceType()) |
| 415 | EmitDeclDestroy(*this, D, GV); |
| 416 | |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 417 | EmitBlock(EndBlock); |
| 418 | } |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 419 | |
| 420 | /// GenerateCXXAggrDestructorHelper - Generates a helper function which when |
| 421 | /// invoked, calls the default destructor on array elements in reverse order of |
| 422 | /// construction. |
| 423 | llvm::Function * |
| 424 | CodeGenFunction::GenerateCXXAggrDestructorHelper(const CXXDestructorDecl *D, |
| 425 | const ArrayType *Array, |
| 426 | llvm::Value *This) { |
| 427 | FunctionArgList Args; |
| 428 | ImplicitParamDecl *Dst = |
| 429 | ImplicitParamDecl::Create(getContext(), 0, |
| 430 | SourceLocation(), 0, |
| 431 | getContext().getPointerType(getContext().VoidTy)); |
| 432 | Args.push_back(std::make_pair(Dst, Dst->getType())); |
| 433 | |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 434 | const CGFunctionInfo &FI = |
| 435 | CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args, |
| 436 | FunctionType::ExtInfo()); |
| 437 | const llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI, false); |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 438 | llvm::Function *Fn = |
| 439 | CreateGlobalInitOrDestructFunction(CGM, FTy, "__cxx_global_array_dtor"); |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 440 | |
| 441 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, Args, SourceLocation()); |
| 442 | |
| 443 | QualType BaseElementTy = getContext().getBaseElementType(Array); |
| 444 | const llvm::Type *BasePtr = ConvertType(BaseElementTy)->getPointerTo(); |
| 445 | llvm::Value *BaseAddrPtr = Builder.CreateBitCast(This, BasePtr); |
| 446 | |
| 447 | EmitCXXAggrDestructorCall(D, Array, BaseAddrPtr); |
| 448 | |
| 449 | FinishFunction(); |
| 450 | |
| 451 | return Fn; |
| 452 | } |