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(); |
Fariborz Jahanian | 581c78f | 2010-06-21 23:31:29 +0000 | [diff] [blame^] | 176 | OrderGlobalInitsType Key(order, PrioritizedCXXGlobalInits.size()); |
Fariborz Jahanian | e0b691a | 2010-06-21 21:27:42 +0000 | [diff] [blame] | 177 | PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn)); |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 178 | } |
| 179 | else |
| 180 | CXXGlobalInits.push_back(Fn); |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Fariborz Jahanian | e0b691a | 2010-06-21 21:27:42 +0000 | [diff] [blame] | 183 | typedef std::pair<CodeGen::OrderGlobalInitsType, |
| 184 | llvm::Function *> global_init_pair; |
| 185 | static int PrioritizedCXXGlobalInitsCmp(const void* a, const void* b) { |
| 186 | const global_init_pair *LHS = static_cast<const global_init_pair*>(a); |
| 187 | const global_init_pair *RHS = static_cast<const global_init_pair*>(b); |
| 188 | if (LHS->first.priority < RHS->first.priority) |
| 189 | return -1; |
| 190 | if (LHS->first.priority == RHS->first.priority) { |
| 191 | if (LHS->first.lex_order < RHS->first.lex_order) |
| 192 | return -1; |
| 193 | if (LHS->first.lex_order == RHS->first.lex_order) |
| 194 | return 0; |
| 195 | } |
| 196 | return +1; |
| 197 | } |
| 198 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 199 | void |
| 200 | CodeGenModule::EmitCXXGlobalInitFunc() { |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 201 | if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty()) |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 202 | return; |
| 203 | |
| 204 | const llvm::FunctionType *FTy |
| 205 | = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
| 206 | false); |
| 207 | |
| 208 | // Create our global initialization function. |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 209 | llvm::Function *Fn = |
| 210 | CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__I_a"); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 211 | |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 212 | if (!PrioritizedCXXGlobalInits.empty()) { |
Fariborz Jahanian | 027d7ed | 2010-06-21 19:49:38 +0000 | [diff] [blame] | 213 | llvm::SmallVector<llvm::Constant*, 8> LocalCXXGlobalInits; |
| 214 | llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(), |
Fariborz Jahanian | e0b691a | 2010-06-21 21:27:42 +0000 | [diff] [blame] | 215 | PrioritizedCXXGlobalInits.end(), |
| 216 | PrioritizedCXXGlobalInitsCmp); |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 217 | for (unsigned i = 0; i < PrioritizedCXXGlobalInits.size(); i++) { |
| 218 | llvm::Function *Fn = PrioritizedCXXGlobalInits[i].second; |
| 219 | LocalCXXGlobalInits.push_back(Fn); |
| 220 | } |
| 221 | for (unsigned i = 0; i < CXXGlobalInits.size(); i++) |
| 222 | LocalCXXGlobalInits.push_back(CXXGlobalInits[i]); |
| 223 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, |
| 224 | &LocalCXXGlobalInits[0], |
| 225 | LocalCXXGlobalInits.size()); |
| 226 | } |
| 227 | else |
| 228 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, |
| 229 | &CXXGlobalInits[0], |
| 230 | CXXGlobalInits.size()); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 231 | AddGlobalCtor(Fn); |
| 232 | } |
| 233 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 234 | void CodeGenModule::EmitCXXGlobalDtorFunc() { |
| 235 | if (CXXGlobalDtors.empty()) |
| 236 | return; |
| 237 | |
| 238 | const llvm::FunctionType *FTy |
| 239 | = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
| 240 | false); |
| 241 | |
| 242 | // Create our global destructor function. |
| 243 | llvm::Function *Fn = |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 244 | CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__D_a"); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 245 | |
| 246 | CodeGenFunction(*this).GenerateCXXGlobalDtorFunc(Fn, CXXGlobalDtors); |
| 247 | AddGlobalDtor(Fn); |
| 248 | } |
| 249 | |
| 250 | void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn, |
| 251 | const VarDecl *D) { |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 252 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(), |
| 253 | SourceLocation()); |
| 254 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 255 | llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D); |
| 256 | EmitCXXGlobalVarDeclInit(*D, DeclPtr); |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 257 | |
| 258 | FinishFunction(); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 259 | } |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 260 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 261 | void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn, |
| 262 | llvm::Constant **Decls, |
| 263 | unsigned NumDecls) { |
| 264 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(), |
| 265 | SourceLocation()); |
| 266 | |
| 267 | for (unsigned i = 0; i != NumDecls; ++i) |
| 268 | Builder.CreateCall(Decls[i]); |
| 269 | |
| 270 | FinishFunction(); |
| 271 | } |
| 272 | |
| 273 | void CodeGenFunction::GenerateCXXGlobalDtorFunc(llvm::Function *Fn, |
Chris Lattner | 810112e | 2010-06-19 05:52:45 +0000 | [diff] [blame] | 274 | const std::vector<std::pair<llvm::WeakVH, llvm::Constant*> > |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 275 | &DtorsAndObjects) { |
| 276 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(), |
| 277 | SourceLocation()); |
| 278 | |
| 279 | // Emit the dtors, in reverse order from construction. |
Chris Lattner | c9a85f9 | 2010-04-26 20:35:54 +0000 | [diff] [blame] | 280 | for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; ++i) { |
Chris Lattner | 810112e | 2010-06-19 05:52:45 +0000 | [diff] [blame] | 281 | llvm::Value *Callee = DtorsAndObjects[e - i - 1].first; |
Chris Lattner | c9a85f9 | 2010-04-26 20:35:54 +0000 | [diff] [blame] | 282 | llvm::CallInst *CI = Builder.CreateCall(Callee, |
| 283 | DtorsAndObjects[e - i - 1].second); |
| 284 | // Make sure the call and the callee agree on calling convention. |
| 285 | if (llvm::Function *F = dyn_cast<llvm::Function>(Callee)) |
| 286 | CI->setCallingConv(F->getCallingConv()); |
| 287 | } |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 288 | |
| 289 | FinishFunction(); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 292 | static llvm::Constant *getGuardAcquireFn(CodeGenFunction &CGF) { |
| 293 | // int __cxa_guard_acquire(__int64_t *guard_object); |
| 294 | |
| 295 | const llvm::Type *Int64PtrTy = |
| 296 | llvm::Type::getInt64PtrTy(CGF.getLLVMContext()); |
| 297 | |
| 298 | std::vector<const llvm::Type*> Args(1, Int64PtrTy); |
| 299 | |
| 300 | const llvm::FunctionType *FTy = |
| 301 | llvm::FunctionType::get(CGF.ConvertType(CGF.getContext().IntTy), |
| 302 | Args, /*isVarArg=*/false); |
| 303 | |
| 304 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire"); |
| 305 | } |
| 306 | |
| 307 | static llvm::Constant *getGuardReleaseFn(CodeGenFunction &CGF) { |
| 308 | // void __cxa_guard_release(__int64_t *guard_object); |
| 309 | |
| 310 | const llvm::Type *Int64PtrTy = |
| 311 | llvm::Type::getInt64PtrTy(CGF.getLLVMContext()); |
| 312 | |
| 313 | std::vector<const llvm::Type*> Args(1, Int64PtrTy); |
| 314 | |
| 315 | const llvm::FunctionType *FTy = |
| 316 | llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), |
| 317 | Args, /*isVarArg=*/false); |
| 318 | |
| 319 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release"); |
| 320 | } |
| 321 | |
| 322 | static llvm::Constant *getGuardAbortFn(CodeGenFunction &CGF) { |
| 323 | // void __cxa_guard_abort(__int64_t *guard_object); |
| 324 | |
| 325 | const llvm::Type *Int64PtrTy = |
| 326 | llvm::Type::getInt64PtrTy(CGF.getLLVMContext()); |
| 327 | |
| 328 | std::vector<const llvm::Type*> Args(1, Int64PtrTy); |
| 329 | |
| 330 | const llvm::FunctionType *FTy = |
| 331 | llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), |
| 332 | Args, /*isVarArg=*/false); |
| 333 | |
| 334 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort"); |
| 335 | } |
| 336 | |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 337 | void |
| 338 | CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D, |
| 339 | llvm::GlobalVariable *GV) { |
John McCall | fe67f3b | 2010-05-04 20:45:42 +0000 | [diff] [blame] | 340 | // Bail out early if this initializer isn't reachable. |
| 341 | if (!Builder.GetInsertBlock()) return; |
| 342 | |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 343 | bool ThreadsafeStatics = getContext().getLangOptions().ThreadsafeStatics; |
| 344 | |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 345 | llvm::SmallString<256> GuardVName; |
| 346 | CGM.getMangleContext().mangleGuardVariable(&D, GuardVName); |
| 347 | |
| 348 | // Create the guard variable. |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 349 | const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(VMContext); |
| 350 | llvm::GlobalValue *GuardVariable = |
| 351 | new llvm::GlobalVariable(CGM.getModule(), Int64Ty, |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 352 | false, GV->getLinkage(), |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 353 | llvm::Constant::getNullValue(Int64Ty), |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 354 | GuardVName.str()); |
| 355 | |
| 356 | // Load the first byte of the guard variable. |
| 357 | const llvm::Type *PtrTy |
| 358 | = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0); |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 359 | llvm::Value *V = |
| 360 | Builder.CreateLoad(Builder.CreateBitCast(GuardVariable, PtrTy), "tmp"); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 361 | |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 362 | llvm::BasicBlock *InitCheckBlock = createBasicBlock("init.check"); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 363 | llvm::BasicBlock *EndBlock = createBasicBlock("init.end"); |
| 364 | |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 365 | // Check if the first byte of the guard variable is zero. |
| 366 | Builder.CreateCondBr(Builder.CreateIsNull(V, "tobool"), |
| 367 | InitCheckBlock, EndBlock); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 368 | |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 369 | EmitBlock(InitCheckBlock); |
| 370 | |
Douglas Gregor | 86a3a03 | 2010-05-16 01:24:12 +0000 | [diff] [blame] | 371 | // Variables used when coping with thread-safe statics and exceptions. |
| 372 | llvm::BasicBlock *SavedLandingPad = 0; |
| 373 | llvm::BasicBlock *LandingPad = 0; |
| 374 | if (ThreadsafeStatics) { |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 375 | // Call __cxa_guard_acquire. |
| 376 | V = Builder.CreateCall(getGuardAcquireFn(*this), GuardVariable); |
| 377 | |
| 378 | llvm::BasicBlock *InitBlock = createBasicBlock("init"); |
| 379 | |
| 380 | Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"), |
| 381 | InitBlock, EndBlock); |
| 382 | |
Douglas Gregor | 1eb2e59 | 2010-05-16 00:44:00 +0000 | [diff] [blame] | 383 | if (Exceptions) { |
Douglas Gregor | 86a3a03 | 2010-05-16 01:24:12 +0000 | [diff] [blame] | 384 | SavedLandingPad = getInvokeDest(); |
| 385 | LandingPad = createBasicBlock("guard.lpad"); |
| 386 | setInvokeDest(LandingPad); |
Douglas Gregor | 1eb2e59 | 2010-05-16 00:44:00 +0000 | [diff] [blame] | 387 | } |
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 | c7974ca | 2009-12-10 01:05:11 +0000 | [diff] [blame] | 394 | // We don't want to pass true for IsInitializer here, because a static |
| 395 | // reference to a temporary does not extend its lifetime. |
Anders Carlsson | a64a869 | 2010-02-03 16:38:03 +0000 | [diff] [blame] | 396 | RValue RV = EmitReferenceBindingToExpr(D.getInit(), |
Anders Carlsson | 864143f | 2009-12-10 01:58:33 +0000 | [diff] [blame] | 397 | /*IsInitializer=*/false); |
| 398 | EmitStoreOfScalar(RV.getScalarVal(), GV, /*Volatile=*/false, T); |
| 399 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 400 | } else |
| 401 | EmitDeclInit(*this, D, GV); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 402 | |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 403 | if (ThreadsafeStatics) { |
| 404 | // Call __cxa_guard_release. |
Douglas Gregor | 86a3a03 | 2010-05-16 01:24:12 +0000 | [diff] [blame] | 405 | Builder.CreateCall(getGuardReleaseFn(*this), GuardVariable); |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 406 | } else { |
| 407 | llvm::Value *One = |
| 408 | llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 1); |
| 409 | Builder.CreateStore(One, Builder.CreateBitCast(GuardVariable, PtrTy)); |
| 410 | } |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 411 | |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 412 | // Register the call to the destructor. |
| 413 | if (!D.getType()->isReferenceType()) |
| 414 | EmitDeclDestroy(*this, D, GV); |
| 415 | |
Douglas Gregor | 86a3a03 | 2010-05-16 01:24:12 +0000 | [diff] [blame] | 416 | if (ThreadsafeStatics && Exceptions) { |
| 417 | // If an exception is thrown during initialization, call __cxa_guard_abort |
| 418 | // along the exceptional edge. |
| 419 | EmitBranch(EndBlock); |
| 420 | |
| 421 | // Construct the landing pad. |
| 422 | EmitBlock(LandingPad); |
| 423 | |
| 424 | // Personality function and LLVM intrinsics. |
| 425 | llvm::Constant *Personality = |
| 426 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty |
| 427 | (VMContext), |
| 428 | true), |
| 429 | "__gxx_personality_v0"); |
| 430 | Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty); |
| 431 | llvm::Value *llvm_eh_exception = |
| 432 | CGM.getIntrinsic(llvm::Intrinsic::eh_exception); |
| 433 | llvm::Value *llvm_eh_selector = |
| 434 | CGM.getIntrinsic(llvm::Intrinsic::eh_selector); |
| 435 | |
| 436 | // Exception object |
| 437 | llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc"); |
| 438 | llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow"); |
| 439 | |
| 440 | // Call the selector function. |
| 441 | const llvm::PointerType *PtrToInt8Ty |
| 442 | = llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext)); |
| 443 | llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty); |
| 444 | llvm::Value* SelectorArgs[3] = { Exc, Personality, Null }; |
| 445 | Builder.CreateCall(llvm_eh_selector, SelectorArgs, SelectorArgs + 3, |
| 446 | "selector"); |
| 447 | Builder.CreateStore(Exc, RethrowPtr); |
| 448 | |
| 449 | // Call __cxa_guard_abort along the exceptional edge. |
| 450 | Builder.CreateCall(getGuardAbortFn(*this), GuardVariable); |
| 451 | |
| 452 | setInvokeDest(SavedLandingPad); |
| 453 | |
| 454 | // Rethrow the current exception. |
| 455 | if (getInvokeDest()) { |
| 456 | llvm::BasicBlock *Cont = createBasicBlock("invoke.cont"); |
| 457 | Builder.CreateInvoke(getUnwindResumeOrRethrowFn(), Cont, |
| 458 | getInvokeDest(), |
| 459 | Builder.CreateLoad(RethrowPtr)); |
| 460 | EmitBlock(Cont); |
| 461 | } else |
| 462 | Builder.CreateCall(getUnwindResumeOrRethrowFn(), |
| 463 | Builder.CreateLoad(RethrowPtr)); |
| 464 | |
| 465 | Builder.CreateUnreachable(); |
| 466 | } |
| 467 | |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 468 | EmitBlock(EndBlock); |
| 469 | } |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 470 | |
| 471 | /// GenerateCXXAggrDestructorHelper - Generates a helper function which when |
| 472 | /// invoked, calls the default destructor on array elements in reverse order of |
| 473 | /// construction. |
| 474 | llvm::Function * |
| 475 | CodeGenFunction::GenerateCXXAggrDestructorHelper(const CXXDestructorDecl *D, |
| 476 | const ArrayType *Array, |
| 477 | llvm::Value *This) { |
| 478 | FunctionArgList Args; |
| 479 | ImplicitParamDecl *Dst = |
| 480 | ImplicitParamDecl::Create(getContext(), 0, |
| 481 | SourceLocation(), 0, |
| 482 | getContext().getPointerType(getContext().VoidTy)); |
| 483 | Args.push_back(std::make_pair(Dst, Dst->getType())); |
| 484 | |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 485 | const CGFunctionInfo &FI = |
| 486 | CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args, |
| 487 | FunctionType::ExtInfo()); |
| 488 | const llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI, false); |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 489 | llvm::Function *Fn = |
| 490 | CreateGlobalInitOrDestructFunction(CGM, FTy, "__cxx_global_array_dtor"); |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 491 | |
| 492 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, Args, SourceLocation()); |
| 493 | |
| 494 | QualType BaseElementTy = getContext().getBaseElementType(Array); |
| 495 | const llvm::Type *BasePtr = ConvertType(BaseElementTy)->getPointerTo(); |
| 496 | llvm::Value *BaseAddrPtr = Builder.CreateBitCast(This, BasePtr); |
| 497 | |
| 498 | EmitCXXAggrDestructorCall(D, Array, BaseAddrPtr); |
| 499 | |
| 500 | FinishFunction(); |
| 501 | |
| 502 | return Fn; |
| 503 | } |