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" |
Fariborz Jahanian | ec80512 | 2011-01-13 20:00:54 +0000 | [diff] [blame] | 15 | #include "CGObjCRuntime.h" |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 16 | #include "CGCXXABI.h" |
Chandler Carruth | 06057ce | 2010-06-15 23:19:56 +0000 | [diff] [blame] | 17 | #include "clang/Frontend/CodeGenOptions.h" |
Douglas Gregor | 86a3a03 | 2010-05-16 01:24:12 +0000 | [diff] [blame] | 18 | #include "llvm/Intrinsics.h" |
| 19 | |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | using namespace CodeGen; |
| 22 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 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 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 29 | ASTContext &Context = CGF.getContext(); |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 30 | |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 31 | unsigned alignment = Context.getDeclAlign(&D).getQuantity(); |
| 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)) { |
Fariborz Jahanian | ec80512 | 2011-01-13 20:00:54 +0000 | [diff] [blame] | 37 | CodeGenModule &CGM = CGF.CGM; |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 38 | if (lv.isObjCStrong()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 39 | CGM.getObjCRuntime().EmitObjCGlobalAssign(CGF, CGF.EmitScalarExpr(Init), |
| 40 | DeclPtr, D.isThreadSpecified()); |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 41 | else if (lv.isObjCWeak()) |
| 42 | CGM.getObjCRuntime().EmitObjCWeakAssign(CGF, CGF.EmitScalarExpr(Init), |
| 43 | DeclPtr); |
Fariborz Jahanian | ec80512 | 2011-01-13 20:00:54 +0000 | [diff] [blame] | 44 | else |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 45 | CGF.EmitScalarInit(Init, &D, lv, false); |
| 46 | } else if (type->isAnyComplexType()) { |
| 47 | CGF.EmitComplexExprIntoAddr(Init, DeclPtr, lv.isVolatile()); |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 48 | } else { |
John McCall | 7c2349b | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 49 | CGF.EmitAggExpr(Init, AggValueSlot::forLValue(lv,AggValueSlot::IsDestructed, |
John McCall | 410ffb2 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 50 | AggValueSlot::DoesNotNeedGCBarriers, |
| 51 | AggValueSlot::IsNotAliased)); |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 55 | /// Emit code to cause the destruction of the given variable with |
| 56 | /// static storage duration. |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 57 | static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D, |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 58 | llvm::Constant *addr) { |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 59 | CodeGenModule &CGM = CGF.CGM; |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 60 | |
| 61 | // FIXME: __attribute__((cleanup)) ? |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 62 | |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 63 | QualType type = D.getType(); |
| 64 | QualType::DestructionKind dtorKind = type.isDestructedType(); |
| 65 | |
| 66 | switch (dtorKind) { |
| 67 | case QualType::DK_none: |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 68 | return; |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 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. |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 76 | return; |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 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); |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 104 | void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D, |
| 105 | llvm::Constant *DeclPtr) { |
| 106 | |
| 107 | const Expr *Init = D.getInit(); |
| 108 | QualType T = D.getType(); |
| 109 | |
| 110 | if (!T->isReferenceType()) { |
| 111 | EmitDeclInit(*this, D, DeclPtr); |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 112 | EmitDeclDestroy(*this, D, DeclPtr); |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 113 | return; |
| 114 | } |
Anders Carlsson | 045a6d8 | 2010-06-27 17:52:15 +0000 | [diff] [blame] | 115 | |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 116 | unsigned Alignment = getContext().getDeclAlign(&D).getQuantity(); |
Anders Carlsson | 045a6d8 | 2010-06-27 17:52:15 +0000 | [diff] [blame] | 117 | RValue RV = EmitReferenceBindingToExpr(Init, &D); |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 118 | EmitStoreOfScalar(RV.getScalarVal(), DeclPtr, false, Alignment, T); |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 119 | } |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 120 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 121 | void |
| 122 | CodeGenFunction::EmitCXXGlobalDtorRegistration(llvm::Constant *DtorFn, |
| 123 | llvm::Constant *DeclPtr) { |
| 124 | // Generate a global destructor entry if not using __cxa_atexit. |
| 125 | if (!CGM.getCodeGenOpts().CXAAtExit) { |
| 126 | CGM.AddCXXDtorEntry(DtorFn, DeclPtr); |
| 127 | return; |
| 128 | } |
| 129 | |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 130 | // Get the destructor function type |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 131 | llvm::Type *DtorFnTy = |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 132 | llvm::FunctionType::get(llvm::Type::getVoidTy(getLLVMContext()), |
Jay Foad | da549e8 | 2011-07-29 13:56:53 +0000 | [diff] [blame] | 133 | Int8PtrTy, false); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 134 | DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy); |
| 135 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 136 | llvm::Type *Params[] = { DtorFnTy, Int8PtrTy, Int8PtrTy }; |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 137 | |
| 138 | // Get the __cxa_atexit function type |
| 139 | // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d ); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 140 | llvm::FunctionType *AtExitFnTy = |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 141 | llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false); |
| 142 | |
| 143 | llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy, |
| 144 | "__cxa_atexit"); |
Anders Carlsson | bd4a073 | 2011-03-20 20:52:32 +0000 | [diff] [blame] | 145 | if (llvm::Function *Fn = dyn_cast<llvm::Function>(AtExitFn)) |
| 146 | Fn->setDoesNotThrow(); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 147 | |
| 148 | llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy, |
| 149 | "__dso_handle"); |
| 150 | llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy), |
| 151 | llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy), |
| 152 | llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) }; |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 153 | Builder.CreateCall(AtExitFn, Args); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 154 | } |
| 155 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 156 | void CodeGenFunction::EmitCXXGuardedInit(const VarDecl &D, |
| 157 | llvm::GlobalVariable *DeclPtr) { |
John McCall | 3209669 | 2011-03-18 02:56:14 +0000 | [diff] [blame] | 158 | // If we've been asked to forbid guard variables, emit an error now. |
| 159 | // This diagnostic is hard-coded for Darwin's use case; we can find |
| 160 | // better phrasing if someone else needs it. |
| 161 | if (CGM.getCodeGenOpts().ForbidGuardVariables) |
| 162 | CGM.Error(D.getLocation(), |
| 163 | "this initialization requires a guard variable, which " |
| 164 | "the kernel does not support"); |
| 165 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 166 | CGM.getCXXABI().EmitGuardedInit(*this, D, DeclPtr); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 169 | static llvm::Function * |
| 170 | CreateGlobalInitOrDestructFunction(CodeGenModule &CGM, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 171 | llvm::FunctionType *FTy, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 172 | StringRef Name) { |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 173 | llvm::Function *Fn = |
| 174 | llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage, |
| 175 | Name, &CGM.getModule()); |
Fariborz Jahanian | d6c9a0f | 2011-02-15 18:54:46 +0000 | [diff] [blame] | 176 | if (!CGM.getContext().getLangOptions().AppleKext) { |
| 177 | // Set the section if needed. |
| 178 | if (const char *Section = |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 179 | CGM.getContext().getTargetInfo().getStaticInitSectionSpecifier()) |
Fariborz Jahanian | d6c9a0f | 2011-02-15 18:54:46 +0000 | [diff] [blame] | 180 | Fn->setSection(Section); |
| 181 | } |
Anders Carlsson | 18af368 | 2010-06-08 22:47:50 +0000 | [diff] [blame] | 182 | |
Anders Carlsson | 7a17851 | 2011-02-28 00:33:03 +0000 | [diff] [blame] | 183 | if (!CGM.getLangOptions().Exceptions) |
John McCall | 044cc54 | 2010-07-06 04:38:10 +0000 | [diff] [blame] | 184 | Fn->setDoesNotThrow(); |
| 185 | |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 186 | return Fn; |
| 187 | } |
| 188 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 189 | void |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 190 | CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D, |
| 191 | llvm::GlobalVariable *Addr) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 192 | llvm::FunctionType *FTy |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 193 | = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
| 194 | false); |
| 195 | |
| 196 | // Create a variable initialization function. |
| 197 | llvm::Function *Fn = |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 198 | CreateGlobalInitOrDestructFunction(*this, FTy, "__cxx_global_var_init"); |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 199 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 200 | CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr); |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 201 | |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 202 | if (D->hasAttr<InitPriorityAttr>()) { |
| 203 | unsigned int order = D->getAttr<InitPriorityAttr>()->getPriority(); |
Chris Lattner | ec2830d | 2010-06-27 06:32:58 +0000 | [diff] [blame] | 204 | OrderGlobalInits Key(order, PrioritizedCXXGlobalInits.size()); |
Fariborz Jahanian | e0b691a | 2010-06-21 21:27:42 +0000 | [diff] [blame] | 205 | PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn)); |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 206 | DelayedCXXInitPosition.erase(D); |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 207 | } |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 208 | else { |
| 209 | llvm::DenseMap<const Decl *, unsigned>::iterator I = |
| 210 | DelayedCXXInitPosition.find(D); |
| 211 | if (I == DelayedCXXInitPosition.end()) { |
| 212 | CXXGlobalInits.push_back(Fn); |
| 213 | } else { |
| 214 | assert(CXXGlobalInits[I->second] == 0); |
| 215 | CXXGlobalInits[I->second] = Fn; |
| 216 | DelayedCXXInitPosition.erase(I); |
| 217 | } |
| 218 | } |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 221 | void |
| 222 | CodeGenModule::EmitCXXGlobalInitFunc() { |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 223 | while (!CXXGlobalInits.empty() && !CXXGlobalInits.back()) |
| 224 | CXXGlobalInits.pop_back(); |
| 225 | |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 226 | if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty()) |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 227 | return; |
| 228 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 229 | llvm::FunctionType *FTy |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 230 | = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
| 231 | false); |
| 232 | |
| 233 | // Create our global initialization function. |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 234 | llvm::Function *Fn = |
| 235 | CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__I_a"); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 236 | |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 237 | if (!PrioritizedCXXGlobalInits.empty()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 238 | SmallVector<llvm::Constant*, 8> LocalCXXGlobalInits; |
Fariborz Jahanian | 027d7ed | 2010-06-21 19:49:38 +0000 | [diff] [blame] | 239 | llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(), |
Fariborz Jahanian | f489688 | 2010-06-22 00:23:08 +0000 | [diff] [blame] | 240 | PrioritizedCXXGlobalInits.end()); |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 241 | for (unsigned i = 0; i < PrioritizedCXXGlobalInits.size(); i++) { |
| 242 | llvm::Function *Fn = PrioritizedCXXGlobalInits[i].second; |
| 243 | LocalCXXGlobalInits.push_back(Fn); |
| 244 | } |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 245 | LocalCXXGlobalInits.append(CXXGlobalInits.begin(), CXXGlobalInits.end()); |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 246 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, |
| 247 | &LocalCXXGlobalInits[0], |
| 248 | LocalCXXGlobalInits.size()); |
| 249 | } |
| 250 | else |
| 251 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, |
| 252 | &CXXGlobalInits[0], |
| 253 | CXXGlobalInits.size()); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 254 | AddGlobalCtor(Fn); |
Axel Naumann | 54ec6c5 | 2011-05-06 15:24:04 +0000 | [diff] [blame] | 255 | CXXGlobalInits.clear(); |
| 256 | PrioritizedCXXGlobalInits.clear(); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 259 | void CodeGenModule::EmitCXXGlobalDtorFunc() { |
| 260 | if (CXXGlobalDtors.empty()) |
| 261 | return; |
| 262 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 263 | llvm::FunctionType *FTy |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 264 | = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
| 265 | false); |
| 266 | |
| 267 | // Create our global destructor function. |
| 268 | llvm::Function *Fn = |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 269 | CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__D_a"); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 270 | |
| 271 | CodeGenFunction(*this).GenerateCXXGlobalDtorFunc(Fn, CXXGlobalDtors); |
| 272 | AddGlobalDtor(Fn); |
| 273 | } |
| 274 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 275 | /// Emit the code necessary to initialize the given global variable. |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 276 | void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn, |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 277 | const VarDecl *D, |
| 278 | llvm::GlobalVariable *Addr) { |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 279 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, |
| 280 | getTypes().getNullaryFunctionInfo(), |
| 281 | FunctionArgList(), SourceLocation()); |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 282 | |
Douglas Gregor | e67d151 | 2011-07-01 21:54:36 +0000 | [diff] [blame] | 283 | // Use guarded initialization if the global variable is weak. This |
| 284 | // occurs for, e.g., instantiated static data members and |
| 285 | // definitions explicitly marked weak. |
| 286 | if (Addr->getLinkage() == llvm::GlobalValue::WeakODRLinkage || |
| 287 | Addr->getLinkage() == llvm::GlobalValue::WeakAnyLinkage) { |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 288 | EmitCXXGuardedInit(*D, Addr); |
| 289 | } else { |
| 290 | EmitCXXGlobalVarDeclInit(*D, Addr); |
Fariborz Jahanian | 92d835a | 2010-10-26 22:47:47 +0000 | [diff] [blame] | 291 | } |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 292 | |
| 293 | FinishFunction(); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 294 | } |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 295 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 296 | void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn, |
| 297 | llvm::Constant **Decls, |
| 298 | unsigned NumDecls) { |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 299 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, |
| 300 | getTypes().getNullaryFunctionInfo(), |
| 301 | FunctionArgList(), SourceLocation()); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 302 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 303 | RunCleanupsScope Scope(*this); |
| 304 | |
| 305 | // When building in Objective-C++ ARC mode, create an autorelease pool |
| 306 | // around the global initializers. |
| 307 | if (getLangOptions().ObjCAutoRefCount && getLangOptions().CPlusPlus) { |
| 308 | llvm::Value *token = EmitObjCAutoreleasePoolPush(); |
| 309 | EmitObjCAutoreleasePoolCleanup(token); |
| 310 | } |
| 311 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 312 | for (unsigned i = 0; i != NumDecls; ++i) |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 313 | if (Decls[i]) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 314 | Builder.CreateCall(Decls[i]); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 315 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 316 | Scope.ForceCleanup(); |
| 317 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 318 | FinishFunction(); |
| 319 | } |
| 320 | |
| 321 | void CodeGenFunction::GenerateCXXGlobalDtorFunc(llvm::Function *Fn, |
Chris Lattner | 810112e | 2010-06-19 05:52:45 +0000 | [diff] [blame] | 322 | const std::vector<std::pair<llvm::WeakVH, llvm::Constant*> > |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 323 | &DtorsAndObjects) { |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 324 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, |
| 325 | getTypes().getNullaryFunctionInfo(), |
| 326 | FunctionArgList(), SourceLocation()); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 327 | |
| 328 | // Emit the dtors, in reverse order from construction. |
Chris Lattner | c9a85f9 | 2010-04-26 20:35:54 +0000 | [diff] [blame] | 329 | for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; ++i) { |
Chris Lattner | 810112e | 2010-06-19 05:52:45 +0000 | [diff] [blame] | 330 | llvm::Value *Callee = DtorsAndObjects[e - i - 1].first; |
Chris Lattner | c9a85f9 | 2010-04-26 20:35:54 +0000 | [diff] [blame] | 331 | llvm::CallInst *CI = Builder.CreateCall(Callee, |
| 332 | DtorsAndObjects[e - i - 1].second); |
| 333 | // Make sure the call and the callee agree on calling convention. |
| 334 | if (llvm::Function *F = dyn_cast<llvm::Function>(Callee)) |
| 335 | CI->setCallingConv(F->getCallingConv()); |
| 336 | } |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 337 | |
| 338 | FinishFunction(); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 339 | } |
| 340 | |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 341 | /// generateDestroyHelper - Generates a helper function which, when |
| 342 | /// invoked, destroys the given object. |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 343 | llvm::Function * |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 344 | CodeGenFunction::generateDestroyHelper(llvm::Constant *addr, |
| 345 | QualType type, |
| 346 | Destroyer &destroyer, |
| 347 | bool useEHCleanupForArray) { |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 348 | FunctionArgList args; |
| 349 | ImplicitParamDecl dst(0, SourceLocation(), 0, getContext().VoidPtrTy); |
| 350 | args.push_back(&dst); |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 351 | |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 352 | const CGFunctionInfo &FI = |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 353 | CGM.getTypes().getFunctionInfo(getContext().VoidTy, args, |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 354 | FunctionType::ExtInfo()); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 355 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI, false); |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 356 | llvm::Function *fn = |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 357 | CreateGlobalInitOrDestructFunction(CGM, FTy, "__cxx_global_array_dtor"); |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 358 | |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 359 | StartFunction(GlobalDecl(), getContext().VoidTy, fn, FI, args, |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 360 | SourceLocation()); |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 361 | |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 362 | emitDestroy(addr, type, destroyer, useEHCleanupForArray); |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 363 | |
| 364 | FinishFunction(); |
| 365 | |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 366 | return fn; |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 367 | } |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 368 | |