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