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" |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 15 | #include "CGCXXABI.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "CGObjCRuntime.h" |
Chandler Carruth | 06057ce | 2010-06-15 23:19:56 +0000 | [diff] [blame] | 17 | #include "clang/Frontend/CodeGenOptions.h" |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | 3b844ba | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Intrinsics.h" |
Douglas Gregor | 86a3a03 | 2010-05-16 01:24:12 +0000 | [diff] [blame] | 20 | |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | using namespace CodeGen; |
| 23 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 24 | static void EmitDeclInit(CodeGenFunction &CGF, const VarDecl &D, |
| 25 | llvm::Constant *DeclPtr) { |
| 26 | assert(D.hasGlobalStorage() && "VarDecl must have global storage!"); |
| 27 | assert(!D.getType()->isReferenceType() && |
| 28 | "Should not call EmitDeclInit on a reference!"); |
| 29 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 30 | ASTContext &Context = CGF.getContext(); |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 31 | |
Eli Friedman | 6da2c71 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 32 | CharUnits alignment = Context.getDeclAlign(&D); |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 33 | QualType type = D.getType(); |
| 34 | LValue lv = CGF.MakeAddrLValue(DeclPtr, type, alignment); |
| 35 | |
| 36 | const Expr *Init = D.getInit(); |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 37 | switch (CGF.getEvaluationKind(type)) { |
| 38 | case TEK_Scalar: { |
Fariborz Jahanian | ec80512 | 2011-01-13 20:00:54 +0000 | [diff] [blame] | 39 | CodeGenModule &CGM = CGF.CGM; |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 40 | if (lv.isObjCStrong()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 41 | CGM.getObjCRuntime().EmitObjCGlobalAssign(CGF, CGF.EmitScalarExpr(Init), |
| 42 | DeclPtr, D.isThreadSpecified()); |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 43 | else if (lv.isObjCWeak()) |
| 44 | CGM.getObjCRuntime().EmitObjCWeakAssign(CGF, CGF.EmitScalarExpr(Init), |
| 45 | DeclPtr); |
Fariborz Jahanian | ec80512 | 2011-01-13 20:00:54 +0000 | [diff] [blame] | 46 | else |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 47 | CGF.EmitScalarInit(Init, &D, lv, false); |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 48 | return; |
| 49 | } |
| 50 | case TEK_Complex: |
| 51 | CGF.EmitComplexExprIntoLValue(Init, lv, /*isInit*/ true); |
| 52 | return; |
| 53 | case TEK_Aggregate: |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 54 | CGF.EmitAggExpr(Init, AggValueSlot::forLValue(lv,AggValueSlot::IsDestructed, |
| 55 | AggValueSlot::DoesNotNeedGCBarriers, |
| 56 | AggValueSlot::IsNotAliased)); |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 57 | return; |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 58 | } |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 59 | llvm_unreachable("bad evaluation kind"); |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 60 | } |
| 61 | |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 62 | /// Emit code to cause the destruction of the given variable with |
| 63 | /// static storage duration. |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 64 | static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D, |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 65 | llvm::Constant *addr) { |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 66 | CodeGenModule &CGM = CGF.CGM; |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 67 | |
| 68 | // FIXME: __attribute__((cleanup)) ? |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 69 | |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 70 | QualType type = D.getType(); |
| 71 | QualType::DestructionKind dtorKind = type.isDestructedType(); |
| 72 | |
| 73 | switch (dtorKind) { |
| 74 | case QualType::DK_none: |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 75 | return; |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 76 | |
| 77 | case QualType::DK_cxx_destructor: |
| 78 | break; |
| 79 | |
| 80 | case QualType::DK_objc_strong_lifetime: |
| 81 | case QualType::DK_objc_weak_lifetime: |
| 82 | // We don't care about releasing objects during process teardown. |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 83 | return; |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | llvm::Constant *function; |
| 87 | llvm::Constant *argument; |
| 88 | |
| 89 | // Special-case non-array C++ destructors, where there's a function |
| 90 | // with the right signature that we can just call. |
| 91 | const CXXRecordDecl *record = 0; |
| 92 | if (dtorKind == QualType::DK_cxx_destructor && |
| 93 | (record = type->getAsCXXRecordDecl())) { |
| 94 | assert(!record->hasTrivialDestructor()); |
| 95 | CXXDestructorDecl *dtor = record->getDestructor(); |
| 96 | |
| 97 | function = CGM.GetAddrOfCXXDestructor(dtor, Dtor_Complete); |
| 98 | argument = addr; |
| 99 | |
| 100 | // Otherwise, the standard logic requires a helper function. |
| 101 | } else { |
| 102 | function = CodeGenFunction(CGM).generateDestroyHelper(addr, type, |
| 103 | CGF.getDestroyer(dtorKind), |
| 104 | CGF.needsEHCleanup(dtorKind)); |
| 105 | argument = llvm::Constant::getNullValue(CGF.Int8PtrTy); |
| 106 | } |
| 107 | |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 108 | CGM.getCXXABI().registerGlobalDtor(CGF, function, argument); |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Richard Smith | abb9432 | 2012-02-17 07:31:37 +0000 | [diff] [blame] | 111 | /// Emit code to cause the variable at the given address to be considered as |
| 112 | /// constant from this point onwards. |
Nick Lewycky | ef78446 | 2012-02-21 00:26:58 +0000 | [diff] [blame] | 113 | static void EmitDeclInvariant(CodeGenFunction &CGF, const VarDecl &D, |
| 114 | llvm::Constant *Addr) { |
Richard Smith | 00a8c3f | 2012-02-17 20:12:52 +0000 | [diff] [blame] | 115 | // Don't emit the intrinsic if we're not optimizing. |
| 116 | if (!CGF.CGM.getCodeGenOpts().OptimizationLevel) |
| 117 | return; |
| 118 | |
Richard Smith | abb9432 | 2012-02-17 07:31:37 +0000 | [diff] [blame] | 119 | // Grab the llvm.invariant.start intrinsic. |
| 120 | llvm::Intrinsic::ID InvStartID = llvm::Intrinsic::invariant_start; |
| 121 | llvm::Constant *InvariantStart = CGF.CGM.getIntrinsic(InvStartID); |
| 122 | |
Nick Lewycky | ef78446 | 2012-02-21 00:26:58 +0000 | [diff] [blame] | 123 | // Emit a call with the size in bytes of the object. |
| 124 | CharUnits WidthChars = CGF.getContext().getTypeSizeInChars(D.getType()); |
| 125 | uint64_t Width = WidthChars.getQuantity(); |
| 126 | llvm::Value *Args[2] = { llvm::ConstantInt::getSigned(CGF.Int64Ty, Width), |
Richard Smith | abb9432 | 2012-02-17 07:31:37 +0000 | [diff] [blame] | 127 | llvm::ConstantExpr::getBitCast(Addr, CGF.Int8PtrTy)}; |
| 128 | CGF.Builder.CreateCall(InvariantStart, Args); |
| 129 | } |
| 130 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 131 | void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D, |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 132 | llvm::Constant *DeclPtr, |
| 133 | bool PerformInit) { |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 134 | |
| 135 | const Expr *Init = D.getInit(); |
| 136 | QualType T = D.getType(); |
| 137 | |
| 138 | if (!T->isReferenceType()) { |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 139 | if (PerformInit) |
| 140 | EmitDeclInit(*this, D, DeclPtr); |
Richard Smith | abb9432 | 2012-02-17 07:31:37 +0000 | [diff] [blame] | 141 | if (CGM.isTypeConstant(D.getType(), true)) |
Nick Lewycky | ef78446 | 2012-02-21 00:26:58 +0000 | [diff] [blame] | 142 | EmitDeclInvariant(*this, D, DeclPtr); |
Richard Smith | abb9432 | 2012-02-17 07:31:37 +0000 | [diff] [blame] | 143 | else |
| 144 | EmitDeclDestroy(*this, D, DeclPtr); |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 145 | return; |
| 146 | } |
Anders Carlsson | 045a6d8 | 2010-06-27 17:52:15 +0000 | [diff] [blame] | 147 | |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 148 | assert(PerformInit && "cannot have constant initializer which needs " |
| 149 | "destruction for reference"); |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 150 | unsigned Alignment = getContext().getDeclAlign(&D).getQuantity(); |
Anders Carlsson | 045a6d8 | 2010-06-27 17:52:15 +0000 | [diff] [blame] | 151 | RValue RV = EmitReferenceBindingToExpr(Init, &D); |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 152 | EmitStoreOfScalar(RV.getScalarVal(), DeclPtr, false, Alignment, T); |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 153 | } |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 154 | |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 155 | static llvm::Function * |
| 156 | CreateGlobalInitOrDestructFunction(CodeGenModule &CGM, |
| 157 | llvm::FunctionType *ty, |
| 158 | const Twine &name); |
| 159 | |
| 160 | /// Create a stub function, suitable for being passed to atexit, |
| 161 | /// which passes the given address to the given destructor function. |
| 162 | static llvm::Constant *createAtExitStub(CodeGenModule &CGM, |
| 163 | llvm::Constant *dtor, |
| 164 | llvm::Constant *addr) { |
| 165 | // Get the destructor function type, void(*)(void). |
| 166 | llvm::FunctionType *ty = llvm::FunctionType::get(CGM.VoidTy, false); |
| 167 | llvm::Function *fn = |
| 168 | CreateGlobalInitOrDestructFunction(CGM, ty, |
| 169 | Twine("__dtor_", addr->getName())); |
| 170 | |
| 171 | CodeGenFunction CGF(CGM); |
| 172 | |
Alexey Samsonov | 34b41f8 | 2012-10-25 10:18:50 +0000 | [diff] [blame] | 173 | // Initialize debug info if needed. |
| 174 | CGF.maybeInitializeDebugInfo(); |
| 175 | |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 176 | CGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, fn, |
| 177 | CGM.getTypes().arrangeNullaryFunction(), |
| 178 | FunctionArgList(), SourceLocation()); |
| 179 | |
| 180 | llvm::CallInst *call = CGF.Builder.CreateCall(dtor, addr); |
| 181 | |
| 182 | // Make sure the call and the callee agree on calling convention. |
| 183 | if (llvm::Function *dtorFn = |
| 184 | dyn_cast<llvm::Function>(dtor->stripPointerCasts())) |
| 185 | call->setCallingConv(dtorFn->getCallingConv()); |
| 186 | |
| 187 | CGF.FinishFunction(); |
| 188 | |
| 189 | return fn; |
| 190 | } |
| 191 | |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 192 | /// Register a global destructor using the C atexit runtime function. |
| 193 | void CodeGenFunction::registerGlobalDtorWithAtExit(llvm::Constant *dtor, |
| 194 | llvm::Constant *addr) { |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 195 | // Create a function which calls the destructor. |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 196 | llvm::Constant *dtorStub = createAtExitStub(CGM, dtor, addr); |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 197 | |
| 198 | // extern "C" int atexit(void (*f)(void)); |
| 199 | llvm::FunctionType *atexitTy = |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 200 | llvm::FunctionType::get(IntTy, dtorStub->getType(), false); |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 201 | |
| 202 | llvm::Constant *atexit = |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 203 | CGM.CreateRuntimeFunction(atexitTy, "atexit"); |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 204 | if (llvm::Function *atexitFn = dyn_cast<llvm::Function>(atexit)) |
| 205 | atexitFn->setDoesNotThrow(); |
| 206 | |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 207 | EmitNounwindRuntimeCall(atexit, dtorStub); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 208 | } |
| 209 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 210 | void CodeGenFunction::EmitCXXGuardedInit(const VarDecl &D, |
Chandler Carruth | 0f30a12 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 211 | llvm::GlobalVariable *DeclPtr, |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 212 | bool PerformInit) { |
John McCall | 3209669 | 2011-03-18 02:56:14 +0000 | [diff] [blame] | 213 | // If we've been asked to forbid guard variables, emit an error now. |
| 214 | // This diagnostic is hard-coded for Darwin's use case; we can find |
| 215 | // better phrasing if someone else needs it. |
| 216 | if (CGM.getCodeGenOpts().ForbidGuardVariables) |
| 217 | CGM.Error(D.getLocation(), |
| 218 | "this initialization requires a guard variable, which " |
| 219 | "the kernel does not support"); |
| 220 | |
Chandler Carruth | 0f30a12 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 221 | CGM.getCXXABI().EmitGuardedInit(*this, D, DeclPtr, PerformInit); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 224 | static llvm::Function * |
| 225 | CreateGlobalInitOrDestructFunction(CodeGenModule &CGM, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 226 | llvm::FunctionType *FTy, |
NAKAMURA Takumi | a0786c9 | 2012-03-28 16:24:29 +0000 | [diff] [blame] | 227 | const Twine &Name) { |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 228 | llvm::Function *Fn = |
| 229 | llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage, |
| 230 | Name, &CGM.getModule()); |
Richard Smith | 7edf9e3 | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 231 | if (!CGM.getLangOpts().AppleKext) { |
Fariborz Jahanian | d6c9a0f | 2011-02-15 18:54:46 +0000 | [diff] [blame] | 232 | // Set the section if needed. |
| 233 | if (const char *Section = |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 234 | CGM.getContext().getTargetInfo().getStaticInitSectionSpecifier()) |
Fariborz Jahanian | d6c9a0f | 2011-02-15 18:54:46 +0000 | [diff] [blame] | 235 | Fn->setSection(Section); |
| 236 | } |
Anders Carlsson | 18af368 | 2010-06-08 22:47:50 +0000 | [diff] [blame] | 237 | |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 238 | Fn->setCallingConv(CGM.getRuntimeCC()); |
| 239 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 240 | if (!CGM.getLangOpts().Exceptions) |
John McCall | 044cc54 | 2010-07-06 04:38:10 +0000 | [diff] [blame] | 241 | Fn->setDoesNotThrow(); |
| 242 | |
Will Dietz | 4f45bc0 | 2013-01-18 11:30:38 +0000 | [diff] [blame] | 243 | if (CGM.getSanOpts().Address) |
Kostya Serebryany | 85aee96 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 244 | Fn->addFnAttr(llvm::Attribute::SanitizeAddress); |
| 245 | if (CGM.getSanOpts().Thread) |
| 246 | Fn->addFnAttr(llvm::Attribute::SanitizeThread); |
| 247 | if (CGM.getSanOpts().Memory) |
| 248 | Fn->addFnAttr(llvm::Attribute::SanitizeMemory); |
Kostya Serebryany | b9d2b3b | 2012-06-26 08:56:33 +0000 | [diff] [blame] | 249 | |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 250 | return Fn; |
| 251 | } |
| 252 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 253 | void |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 254 | CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D, |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 255 | llvm::GlobalVariable *Addr, |
| 256 | bool PerformInit) { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 257 | llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false); |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 258 | |
| 259 | // Create a variable initialization function. |
| 260 | llvm::Function *Fn = |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 261 | CreateGlobalInitOrDestructFunction(*this, FTy, "__cxx_global_var_init"); |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 262 | |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 263 | CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr, |
| 264 | PerformInit); |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 265 | |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 266 | if (D->hasAttr<InitPriorityAttr>()) { |
| 267 | unsigned int order = D->getAttr<InitPriorityAttr>()->getPriority(); |
Chris Lattner | ec2830d | 2010-06-27 06:32:58 +0000 | [diff] [blame] | 268 | OrderGlobalInits Key(order, PrioritizedCXXGlobalInits.size()); |
Fariborz Jahanian | e0b691a | 2010-06-21 21:27:42 +0000 | [diff] [blame] | 269 | PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn)); |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 270 | DelayedCXXInitPosition.erase(D); |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 271 | } else { |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 272 | llvm::DenseMap<const Decl *, unsigned>::iterator I = |
| 273 | DelayedCXXInitPosition.find(D); |
| 274 | if (I == DelayedCXXInitPosition.end()) { |
| 275 | CXXGlobalInits.push_back(Fn); |
| 276 | } else { |
| 277 | assert(CXXGlobalInits[I->second] == 0); |
| 278 | CXXGlobalInits[I->second] = Fn; |
| 279 | DelayedCXXInitPosition.erase(I); |
| 280 | } |
| 281 | } |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 284 | void |
| 285 | CodeGenModule::EmitCXXGlobalInitFunc() { |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 286 | while (!CXXGlobalInits.empty() && !CXXGlobalInits.back()) |
| 287 | CXXGlobalInits.pop_back(); |
| 288 | |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 289 | if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty()) |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 290 | return; |
| 291 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 292 | llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 293 | |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 294 | |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 295 | // Create our global initialization function. |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 296 | if (!PrioritizedCXXGlobalInits.empty()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 297 | SmallVector<llvm::Constant*, 8> LocalCXXGlobalInits; |
Fariborz Jahanian | 027d7ed | 2010-06-21 19:49:38 +0000 | [diff] [blame] | 298 | llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(), |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 299 | PrioritizedCXXGlobalInits.end()); |
| 300 | // Iterate over "chunks" of ctors with same priority and emit each chunk |
| 301 | // into separate function. Note - everything is sorted first by priority, |
| 302 | // second - by lex order, so we emit ctor functions in proper order. |
| 303 | for (SmallVectorImpl<GlobalInitData >::iterator |
| 304 | I = PrioritizedCXXGlobalInits.begin(), |
| 305 | E = PrioritizedCXXGlobalInits.end(); I != E; ) { |
| 306 | SmallVectorImpl<GlobalInitData >::iterator |
| 307 | PrioE = std::upper_bound(I + 1, E, *I, GlobalInitPriorityCmp()); |
| 308 | |
| 309 | LocalCXXGlobalInits.clear(); |
| 310 | unsigned Priority = I->first.priority; |
| 311 | // Compute the function suffix from priority. Prepend with zeroes to make |
| 312 | // sure the function names are also ordered as priorities. |
| 313 | std::string PrioritySuffix = llvm::utostr(Priority); |
| 314 | // Priority is always <= 65535 (enforced by sema).. |
| 315 | PrioritySuffix = std::string(6-PrioritySuffix.size(), '0')+PrioritySuffix; |
| 316 | llvm::Function *Fn = |
| 317 | CreateGlobalInitOrDestructFunction(*this, FTy, |
| 318 | "_GLOBAL__I_" + PrioritySuffix); |
| 319 | |
| 320 | for (; I < PrioE; ++I) |
| 321 | LocalCXXGlobalInits.push_back(I->second); |
| 322 | |
| 323 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 324 | &LocalCXXGlobalInits[0], |
| 325 | LocalCXXGlobalInits.size()); |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 326 | AddGlobalCtor(Fn, Priority); |
| 327 | } |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 328 | } |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 329 | |
| 330 | llvm::Function *Fn = |
| 331 | CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__I_a"); |
| 332 | |
| 333 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, |
| 334 | &CXXGlobalInits[0], |
| 335 | CXXGlobalInits.size()); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 336 | AddGlobalCtor(Fn); |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 337 | |
Axel Naumann | 54ec6c5 | 2011-05-06 15:24:04 +0000 | [diff] [blame] | 338 | CXXGlobalInits.clear(); |
| 339 | PrioritizedCXXGlobalInits.clear(); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 342 | void CodeGenModule::EmitCXXGlobalDtorFunc() { |
| 343 | if (CXXGlobalDtors.empty()) |
| 344 | return; |
| 345 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 346 | llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 347 | |
| 348 | // Create our global destructor function. |
| 349 | llvm::Function *Fn = |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 350 | CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__D_a"); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 351 | |
John McCall | 3f88f68 | 2012-04-06 18:21:03 +0000 | [diff] [blame] | 352 | CodeGenFunction(*this).GenerateCXXGlobalDtorsFunc(Fn, CXXGlobalDtors); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 353 | AddGlobalDtor(Fn); |
| 354 | } |
| 355 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 356 | /// Emit the code necessary to initialize the given global variable. |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 357 | void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn, |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 358 | const VarDecl *D, |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 359 | llvm::GlobalVariable *Addr, |
| 360 | bool PerformInit) { |
Alexey Samsonov | a240df2 | 2012-10-16 07:22:28 +0000 | [diff] [blame] | 361 | // Check if we need to emit debug info for variable initializer. |
| 362 | if (!D->hasAttr<NoDebugAttr>()) |
| 363 | maybeInitializeDebugInfo(); |
Nick Lewycky | 78d1a10 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 364 | |
| 365 | StartFunction(GlobalDecl(D), getContext().VoidTy, Fn, |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 366 | getTypes().arrangeNullaryFunction(), |
Nick Lewycky | 78d1a10 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 367 | FunctionArgList(), D->getInit()->getExprLoc()); |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 368 | |
Douglas Gregor | e67d151 | 2011-07-01 21:54:36 +0000 | [diff] [blame] | 369 | // Use guarded initialization if the global variable is weak. This |
| 370 | // occurs for, e.g., instantiated static data members and |
| 371 | // definitions explicitly marked weak. |
| 372 | if (Addr->getLinkage() == llvm::GlobalValue::WeakODRLinkage || |
| 373 | Addr->getLinkage() == llvm::GlobalValue::WeakAnyLinkage) { |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 374 | EmitCXXGuardedInit(*D, Addr, PerformInit); |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 375 | } else { |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 376 | EmitCXXGlobalVarDeclInit(*D, Addr, PerformInit); |
Fariborz Jahanian | 92d835a | 2010-10-26 22:47:47 +0000 | [diff] [blame] | 377 | } |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 378 | |
| 379 | FinishFunction(); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 380 | } |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 381 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 382 | void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn, |
| 383 | llvm::Constant **Decls, |
| 384 | unsigned NumDecls) { |
Alexey Samsonov | 34b41f8 | 2012-10-25 10:18:50 +0000 | [diff] [blame] | 385 | // Initialize debug info if needed. |
| 386 | maybeInitializeDebugInfo(); |
| 387 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 388 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 389 | getTypes().arrangeNullaryFunction(), |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 390 | FunctionArgList(), SourceLocation()); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 391 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 392 | RunCleanupsScope Scope(*this); |
| 393 | |
| 394 | // When building in Objective-C++ ARC mode, create an autorelease pool |
| 395 | // around the global initializers. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 396 | if (getLangOpts().ObjCAutoRefCount && getLangOpts().CPlusPlus) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 397 | llvm::Value *token = EmitObjCAutoreleasePoolPush(); |
| 398 | EmitObjCAutoreleasePoolCleanup(token); |
| 399 | } |
| 400 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 401 | for (unsigned i = 0; i != NumDecls; ++i) |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 402 | if (Decls[i]) |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 403 | EmitRuntimeCall(Decls[i]); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 404 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 405 | Scope.ForceCleanup(); |
| 406 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 407 | FinishFunction(); |
| 408 | } |
| 409 | |
John McCall | 3f88f68 | 2012-04-06 18:21:03 +0000 | [diff] [blame] | 410 | void CodeGenFunction::GenerateCXXGlobalDtorsFunc(llvm::Function *Fn, |
Chris Lattner | 810112e | 2010-06-19 05:52:45 +0000 | [diff] [blame] | 411 | const std::vector<std::pair<llvm::WeakVH, llvm::Constant*> > |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 412 | &DtorsAndObjects) { |
Alexey Samsonov | 34b41f8 | 2012-10-25 10:18:50 +0000 | [diff] [blame] | 413 | // Initialize debug info if needed. |
| 414 | maybeInitializeDebugInfo(); |
| 415 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 416 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 417 | getTypes().arrangeNullaryFunction(), |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 418 | FunctionArgList(), SourceLocation()); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 419 | |
| 420 | // Emit the dtors, in reverse order from construction. |
Chris Lattner | c9a85f9 | 2010-04-26 20:35:54 +0000 | [diff] [blame] | 421 | for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; ++i) { |
Chris Lattner | 810112e | 2010-06-19 05:52:45 +0000 | [diff] [blame] | 422 | llvm::Value *Callee = DtorsAndObjects[e - i - 1].first; |
Chris Lattner | c9a85f9 | 2010-04-26 20:35:54 +0000 | [diff] [blame] | 423 | llvm::CallInst *CI = Builder.CreateCall(Callee, |
| 424 | DtorsAndObjects[e - i - 1].second); |
| 425 | // Make sure the call and the callee agree on calling convention. |
| 426 | if (llvm::Function *F = dyn_cast<llvm::Function>(Callee)) |
| 427 | CI->setCallingConv(F->getCallingConv()); |
| 428 | } |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 429 | |
| 430 | FinishFunction(); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 431 | } |
| 432 | |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 433 | /// generateDestroyHelper - Generates a helper function which, when |
| 434 | /// invoked, destroys the given object. |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 435 | llvm::Function * |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 436 | CodeGenFunction::generateDestroyHelper(llvm::Constant *addr, |
| 437 | QualType type, |
Peter Collingbourne | 516bbd4 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 438 | Destroyer *destroyer, |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 439 | bool useEHCleanupForArray) { |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 440 | FunctionArgList args; |
| 441 | ImplicitParamDecl dst(0, SourceLocation(), 0, getContext().VoidPtrTy); |
| 442 | args.push_back(&dst); |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 443 | |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 444 | const CGFunctionInfo &FI = |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 445 | CGM.getTypes().arrangeFunctionDeclaration(getContext().VoidTy, args, |
| 446 | FunctionType::ExtInfo(), |
| 447 | /*variadic*/ false); |
| 448 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 449 | llvm::Function *fn = |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 450 | CreateGlobalInitOrDestructFunction(CGM, FTy, "__cxx_global_array_dtor"); |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 451 | |
Alexey Samsonov | 34b41f8 | 2012-10-25 10:18:50 +0000 | [diff] [blame] | 452 | // Initialize debug info if needed. |
| 453 | maybeInitializeDebugInfo(); |
| 454 | |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 455 | StartFunction(GlobalDecl(), getContext().VoidTy, fn, FI, args, |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 456 | SourceLocation()); |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 457 | |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 458 | emitDestroy(addr, type, destroyer, useEHCleanupForArray); |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 459 | |
| 460 | FinishFunction(); |
| 461 | |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 462 | return fn; |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 463 | } |