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