blob: 9faaed55386d1f777e9b6538328b06bde6e1a483 [file] [log] [blame]
Anders Carlsson5ec2e7c2009-12-10 00:16:00 +00001//===--- CGDeclCXX.cpp - Emit LLVM Code for C++ declarations --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code dealing with code generation of C++ declarations
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000015#include "clang/Frontend/CodeGenOptions.h"
Douglas Gregor86a3a032010-05-16 01:24:12 +000016#include "llvm/Intrinsics.h"
17
Anders Carlsson5ec2e7c2009-12-10 00:16:00 +000018using namespace clang;
19using namespace CodeGen;
20
Anders Carlssonfcbfdc12009-12-10 00:57:45 +000021static void EmitDeclInit(CodeGenFunction &CGF, const VarDecl &D,
22 llvm::Constant *DeclPtr) {
23 assert(D.hasGlobalStorage() && "VarDecl must have global storage!");
24 assert(!D.getType()->isReferenceType() &&
25 "Should not call EmitDeclInit on a reference!");
26
Anders Carlssonfcbfdc12009-12-10 00:57:45 +000027 ASTContext &Context = CGF.getContext();
28
Anders Carlsson5ec2e7c2009-12-10 00:16:00 +000029 const Expr *Init = D.getInit();
30 QualType T = D.getType();
Anders Carlssonfcbfdc12009-12-10 00:57:45 +000031 bool isVolatile = Context.getCanonicalType(T).isVolatileQualified();
Anders Carlsson5ec2e7c2009-12-10 00:16:00 +000032
Anders Carlssonfcbfdc12009-12-10 00:57:45 +000033 if (!CGF.hasAggregateLLVMType(T)) {
34 llvm::Value *V = CGF.EmitScalarExpr(Init);
35 CGF.EmitStoreOfScalar(V, DeclPtr, isVolatile, T);
Anders Carlsson5ec2e7c2009-12-10 00:16:00 +000036 } else if (T->isAnyComplexType()) {
Anders Carlssonfcbfdc12009-12-10 00:57:45 +000037 CGF.EmitComplexExprIntoAddr(Init, DeclPtr, isVolatile);
Anders Carlsson5ec2e7c2009-12-10 00:16:00 +000038 } else {
Anders Carlssonfcbfdc12009-12-10 00:57:45 +000039 CGF.EmitAggExpr(Init, DeclPtr, isVolatile);
Anders Carlsson5ec2e7c2009-12-10 00:16:00 +000040 }
41}
42
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000043static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D,
44 llvm::Constant *DeclPtr) {
45 CodeGenModule &CGM = CGF.CGM;
46 ASTContext &Context = CGF.getContext();
47
48 const Expr *Init = D.getInit();
49 QualType T = D.getType();
50 if (!CGF.hasAggregateLLVMType(T) || T->isAnyComplexType())
51 return;
52
53 // Avoid generating destructor(s) for initialized objects.
54 if (!isa<CXXConstructExpr>(Init))
55 return;
56
57 const ConstantArrayType *Array = Context.getAsConstantArrayType(T);
58 if (Array)
59 T = Context.getBaseElementType(Array);
60
61 const RecordType *RT = T->getAs<RecordType>();
62 if (!RT)
63 return;
64
65 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
66 if (RD->hasTrivialDestructor())
67 return;
68
69 CXXDestructorDecl *Dtor = RD->getDestructor(Context);
70
71 llvm::Constant *DtorFn;
72 if (Array) {
73 DtorFn =
Anders Carlsson02e370a2010-06-08 22:14:59 +000074 CodeGenFunction(CGM).GenerateCXXAggrDestructorHelper(Dtor, Array,
75 DeclPtr);
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000076 const llvm::Type *Int8PtrTy =
Anders Carlsson02e370a2010-06-08 22:14:59 +000077 llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000078 DeclPtr = llvm::Constant::getNullValue(Int8PtrTy);
79 } else
80 DtorFn = CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete);
81
82 CGF.EmitCXXGlobalDtorRegistration(DtorFn, DeclPtr);
83}
84
Anders Carlssonfcbfdc12009-12-10 00:57:45 +000085void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
86 llvm::Constant *DeclPtr) {
87
88 const Expr *Init = D.getInit();
89 QualType T = D.getType();
90
91 if (!T->isReferenceType()) {
92 EmitDeclInit(*this, D, DeclPtr);
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000093 EmitDeclDestroy(*this, D, DeclPtr);
Anders Carlssonfcbfdc12009-12-10 00:57:45 +000094 return;
95 }
Fariborz Jahanian616c1732010-01-25 21:40:39 +000096 if (Init->isLvalue(getContext()) == Expr::LV_Valid) {
Anders Carlssona64a8692010-02-03 16:38:03 +000097 RValue RV = EmitReferenceBindingToExpr(Init, /*IsInitializer=*/true);
Fariborz Jahanian616c1732010-01-25 21:40:39 +000098 EmitStoreOfScalar(RV.getScalarVal(), DeclPtr, false, T);
99 return;
100 }
101 ErrorUnsupported(Init,
102 "global variable that binds reference to a non-lvalue");
Anders Carlssonfcbfdc12009-12-10 00:57:45 +0000103}
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000104
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000105void
106CodeGenFunction::EmitCXXGlobalDtorRegistration(llvm::Constant *DtorFn,
107 llvm::Constant *DeclPtr) {
108 // Generate a global destructor entry if not using __cxa_atexit.
109 if (!CGM.getCodeGenOpts().CXAAtExit) {
110 CGM.AddCXXDtorEntry(DtorFn, DeclPtr);
111 return;
112 }
113
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000114 const llvm::Type *Int8PtrTy =
115 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
116
117 std::vector<const llvm::Type *> Params;
118 Params.push_back(Int8PtrTy);
119
120 // Get the destructor function type
121 const llvm::Type *DtorFnTy =
122 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false);
123 DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy);
124
125 Params.clear();
126 Params.push_back(DtorFnTy);
127 Params.push_back(Int8PtrTy);
128 Params.push_back(Int8PtrTy);
129
130 // Get the __cxa_atexit function type
131 // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d );
132 const llvm::FunctionType *AtExitFnTy =
133 llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false);
134
135 llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy,
136 "__cxa_atexit");
137
138 llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy,
139 "__dso_handle");
140 llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy),
141 llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy),
142 llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) };
143 Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args));
144}
145
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000146static llvm::Function *
147CreateGlobalInitOrDestructFunction(CodeGenModule &CGM,
148 const llvm::FunctionType *FTy,
149 llvm::StringRef Name) {
150 llvm::Function *Fn =
151 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
152 Name, &CGM.getModule());
153
Anders Carlsson18af3682010-06-08 22:47:50 +0000154 // Set the section if needed.
155 if (const char *Section =
156 CGM.getContext().Target.getStaticInitSectionSpecifier())
157 Fn->setSection(Section);
158
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000159 return Fn;
160}
161
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000162void
163CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D) {
Eli Friedman6c6bda32010-01-08 00:50:11 +0000164 const llvm::FunctionType *FTy
165 = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
166 false);
167
168 // Create a variable initialization function.
169 llvm::Function *Fn =
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000170 CreateGlobalInitOrDestructFunction(*this, FTy, "__cxx_global_var_init");
Eli Friedman6c6bda32010-01-08 00:50:11 +0000171
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000172 CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D);
Eli Friedman6c6bda32010-01-08 00:50:11 +0000173
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000174 if (D->hasAttr<InitPriorityAttr>()) {
Fariborz Jahaniane0b691a2010-06-21 21:27:42 +0000175 static unsigned lix = 0; // to keep the lexical order of equal priority
176 // objects intact;
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000177 unsigned int order = D->getAttr<InitPriorityAttr>()->getPriority();
Fariborz Jahaniane0b691a2010-06-21 21:27:42 +0000178 OrderGlobalInitsType Key(order, lix++);
179 PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn));
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000180 }
181 else
182 CXXGlobalInits.push_back(Fn);
Eli Friedman6c6bda32010-01-08 00:50:11 +0000183}
184
Fariborz Jahaniane0b691a2010-06-21 21:27:42 +0000185typedef std::pair<CodeGen::OrderGlobalInitsType,
186 llvm::Function *> global_init_pair;
187static int PrioritizedCXXGlobalInitsCmp(const void* a, const void* b) {
188 const global_init_pair *LHS = static_cast<const global_init_pair*>(a);
189 const global_init_pair *RHS = static_cast<const global_init_pair*>(b);
190 if (LHS->first.priority < RHS->first.priority)
191 return -1;
192 if (LHS->first.priority == RHS->first.priority) {
193 if (LHS->first.lex_order < RHS->first.lex_order)
194 return -1;
195 if (LHS->first.lex_order == RHS->first.lex_order)
196 return 0;
197 }
198 return +1;
199}
200
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000201void
202CodeGenModule::EmitCXXGlobalInitFunc() {
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000203 if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty())
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000204 return;
205
206 const llvm::FunctionType *FTy
207 = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
208 false);
209
210 // Create our global initialization function.
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000211 llvm::Function *Fn =
212 CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__I_a");
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000213
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000214 if (!PrioritizedCXXGlobalInits.empty()) {
Fariborz Jahanian027d7ed2010-06-21 19:49:38 +0000215 llvm::SmallVector<llvm::Constant*, 8> LocalCXXGlobalInits;
216 llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(),
Fariborz Jahaniane0b691a2010-06-21 21:27:42 +0000217 PrioritizedCXXGlobalInits.end(),
218 PrioritizedCXXGlobalInitsCmp);
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000219 for (unsigned i = 0; i < PrioritizedCXXGlobalInits.size(); i++) {
220 llvm::Function *Fn = PrioritizedCXXGlobalInits[i].second;
221 LocalCXXGlobalInits.push_back(Fn);
222 }
223 for (unsigned i = 0; i < CXXGlobalInits.size(); i++)
224 LocalCXXGlobalInits.push_back(CXXGlobalInits[i]);
225 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
226 &LocalCXXGlobalInits[0],
227 LocalCXXGlobalInits.size());
228 }
229 else
230 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
231 &CXXGlobalInits[0],
232 CXXGlobalInits.size());
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000233 AddGlobalCtor(Fn);
234}
235
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000236void CodeGenModule::EmitCXXGlobalDtorFunc() {
237 if (CXXGlobalDtors.empty())
238 return;
239
240 const llvm::FunctionType *FTy
241 = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
242 false);
243
244 // Create our global destructor function.
245 llvm::Function *Fn =
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000246 CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__D_a");
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000247
248 CodeGenFunction(*this).GenerateCXXGlobalDtorFunc(Fn, CXXGlobalDtors);
249 AddGlobalDtor(Fn);
250}
251
252void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
253 const VarDecl *D) {
Daniel Dunbar5c6846e2010-03-20 04:15:29 +0000254 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
255 SourceLocation());
256
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000257 llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D);
258 EmitCXXGlobalVarDeclInit(*D, DeclPtr);
Daniel Dunbar5c6846e2010-03-20 04:15:29 +0000259
260 FinishFunction();
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000261}
Daniel Dunbar5c6846e2010-03-20 04:15:29 +0000262
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000263void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
264 llvm::Constant **Decls,
265 unsigned NumDecls) {
266 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
267 SourceLocation());
268
269 for (unsigned i = 0; i != NumDecls; ++i)
270 Builder.CreateCall(Decls[i]);
271
272 FinishFunction();
273}
274
275void CodeGenFunction::GenerateCXXGlobalDtorFunc(llvm::Function *Fn,
Chris Lattner810112e2010-06-19 05:52:45 +0000276 const std::vector<std::pair<llvm::WeakVH, llvm::Constant*> >
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000277 &DtorsAndObjects) {
278 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
279 SourceLocation());
280
281 // Emit the dtors, in reverse order from construction.
Chris Lattnerc9a85f92010-04-26 20:35:54 +0000282 for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; ++i) {
Chris Lattner810112e2010-06-19 05:52:45 +0000283 llvm::Value *Callee = DtorsAndObjects[e - i - 1].first;
Chris Lattnerc9a85f92010-04-26 20:35:54 +0000284 llvm::CallInst *CI = Builder.CreateCall(Callee,
285 DtorsAndObjects[e - i - 1].second);
286 // Make sure the call and the callee agree on calling convention.
287 if (llvm::Function *F = dyn_cast<llvm::Function>(Callee))
288 CI->setCallingConv(F->getCallingConv());
289 }
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000290
291 FinishFunction();
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000292}
293
Anders Carlssona508b7d2010-02-06 23:23:06 +0000294static llvm::Constant *getGuardAcquireFn(CodeGenFunction &CGF) {
295 // int __cxa_guard_acquire(__int64_t *guard_object);
296
297 const llvm::Type *Int64PtrTy =
298 llvm::Type::getInt64PtrTy(CGF.getLLVMContext());
299
300 std::vector<const llvm::Type*> Args(1, Int64PtrTy);
301
302 const llvm::FunctionType *FTy =
303 llvm::FunctionType::get(CGF.ConvertType(CGF.getContext().IntTy),
304 Args, /*isVarArg=*/false);
305
306 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire");
307}
308
309static llvm::Constant *getGuardReleaseFn(CodeGenFunction &CGF) {
310 // void __cxa_guard_release(__int64_t *guard_object);
311
312 const llvm::Type *Int64PtrTy =
313 llvm::Type::getInt64PtrTy(CGF.getLLVMContext());
314
315 std::vector<const llvm::Type*> Args(1, Int64PtrTy);
316
317 const llvm::FunctionType *FTy =
318 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
319 Args, /*isVarArg=*/false);
320
321 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release");
322}
323
324static llvm::Constant *getGuardAbortFn(CodeGenFunction &CGF) {
325 // void __cxa_guard_abort(__int64_t *guard_object);
326
327 const llvm::Type *Int64PtrTy =
328 llvm::Type::getInt64PtrTy(CGF.getLLVMContext());
329
330 std::vector<const llvm::Type*> Args(1, Int64PtrTy);
331
332 const llvm::FunctionType *FTy =
333 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
334 Args, /*isVarArg=*/false);
335
336 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort");
337}
338
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000339void
340CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D,
341 llvm::GlobalVariable *GV) {
John McCallfe67f3b2010-05-04 20:45:42 +0000342 // Bail out early if this initializer isn't reachable.
343 if (!Builder.GetInsertBlock()) return;
344
Anders Carlssona508b7d2010-02-06 23:23:06 +0000345 bool ThreadsafeStatics = getContext().getLangOptions().ThreadsafeStatics;
346
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000347 llvm::SmallString<256> GuardVName;
348 CGM.getMangleContext().mangleGuardVariable(&D, GuardVName);
349
350 // Create the guard variable.
Anders Carlssona508b7d2010-02-06 23:23:06 +0000351 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(VMContext);
352 llvm::GlobalValue *GuardVariable =
353 new llvm::GlobalVariable(CGM.getModule(), Int64Ty,
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000354 false, GV->getLinkage(),
Anders Carlssona508b7d2010-02-06 23:23:06 +0000355 llvm::Constant::getNullValue(Int64Ty),
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000356 GuardVName.str());
357
358 // Load the first byte of the guard variable.
359 const llvm::Type *PtrTy
360 = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Anders Carlssona508b7d2010-02-06 23:23:06 +0000361 llvm::Value *V =
362 Builder.CreateLoad(Builder.CreateBitCast(GuardVariable, PtrTy), "tmp");
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000363
Anders Carlssona508b7d2010-02-06 23:23:06 +0000364 llvm::BasicBlock *InitCheckBlock = createBasicBlock("init.check");
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000365 llvm::BasicBlock *EndBlock = createBasicBlock("init.end");
366
Anders Carlssona508b7d2010-02-06 23:23:06 +0000367 // Check if the first byte of the guard variable is zero.
368 Builder.CreateCondBr(Builder.CreateIsNull(V, "tobool"),
369 InitCheckBlock, EndBlock);
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000370
Anders Carlssona508b7d2010-02-06 23:23:06 +0000371 EmitBlock(InitCheckBlock);
372
Douglas Gregor86a3a032010-05-16 01:24:12 +0000373 // Variables used when coping with thread-safe statics and exceptions.
374 llvm::BasicBlock *SavedLandingPad = 0;
375 llvm::BasicBlock *LandingPad = 0;
376 if (ThreadsafeStatics) {
Anders Carlssona508b7d2010-02-06 23:23:06 +0000377 // Call __cxa_guard_acquire.
378 V = Builder.CreateCall(getGuardAcquireFn(*this), GuardVariable);
379
380 llvm::BasicBlock *InitBlock = createBasicBlock("init");
381
382 Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"),
383 InitBlock, EndBlock);
384
Douglas Gregor1eb2e592010-05-16 00:44:00 +0000385 if (Exceptions) {
Douglas Gregor86a3a032010-05-16 01:24:12 +0000386 SavedLandingPad = getInvokeDest();
387 LandingPad = createBasicBlock("guard.lpad");
388 setInvokeDest(LandingPad);
Douglas Gregor1eb2e592010-05-16 00:44:00 +0000389 }
Douglas Gregor86a3a032010-05-16 01:24:12 +0000390
391 EmitBlock(InitBlock);
Anders Carlssona508b7d2010-02-06 23:23:06 +0000392 }
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000393
Anders Carlssonfcbfdc12009-12-10 00:57:45 +0000394 if (D.getType()->isReferenceType()) {
Anders Carlsson864143f2009-12-10 01:58:33 +0000395 QualType T = D.getType();
Anders Carlssonc7974ca2009-12-10 01:05:11 +0000396 // We don't want to pass true for IsInitializer here, because a static
397 // reference to a temporary does not extend its lifetime.
Anders Carlssona64a8692010-02-03 16:38:03 +0000398 RValue RV = EmitReferenceBindingToExpr(D.getInit(),
Anders Carlsson864143f2009-12-10 01:58:33 +0000399 /*IsInitializer=*/false);
400 EmitStoreOfScalar(RV.getScalarVal(), GV, /*Volatile=*/false, T);
401
Anders Carlssonfcbfdc12009-12-10 00:57:45 +0000402 } else
403 EmitDeclInit(*this, D, GV);
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000404
Anders Carlssona508b7d2010-02-06 23:23:06 +0000405 if (ThreadsafeStatics) {
406 // Call __cxa_guard_release.
Douglas Gregor86a3a032010-05-16 01:24:12 +0000407 Builder.CreateCall(getGuardReleaseFn(*this), GuardVariable);
Anders Carlssona508b7d2010-02-06 23:23:06 +0000408 } else {
409 llvm::Value *One =
410 llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 1);
411 Builder.CreateStore(One, Builder.CreateBitCast(GuardVariable, PtrTy));
412 }
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000413
Douglas Gregorcc6a44b2010-05-05 15:38:32 +0000414 // Register the call to the destructor.
415 if (!D.getType()->isReferenceType())
416 EmitDeclDestroy(*this, D, GV);
417
Douglas Gregor86a3a032010-05-16 01:24:12 +0000418 if (ThreadsafeStatics && Exceptions) {
419 // If an exception is thrown during initialization, call __cxa_guard_abort
420 // along the exceptional edge.
421 EmitBranch(EndBlock);
422
423 // Construct the landing pad.
424 EmitBlock(LandingPad);
425
426 // Personality function and LLVM intrinsics.
427 llvm::Constant *Personality =
428 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
429 (VMContext),
430 true),
431 "__gxx_personality_v0");
432 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
433 llvm::Value *llvm_eh_exception =
434 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
435 llvm::Value *llvm_eh_selector =
436 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
437
438 // Exception object
439 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
440 llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
441
442 // Call the selector function.
443 const llvm::PointerType *PtrToInt8Ty
444 = llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
445 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
446 llvm::Value* SelectorArgs[3] = { Exc, Personality, Null };
447 Builder.CreateCall(llvm_eh_selector, SelectorArgs, SelectorArgs + 3,
448 "selector");
449 Builder.CreateStore(Exc, RethrowPtr);
450
451 // Call __cxa_guard_abort along the exceptional edge.
452 Builder.CreateCall(getGuardAbortFn(*this), GuardVariable);
453
454 setInvokeDest(SavedLandingPad);
455
456 // Rethrow the current exception.
457 if (getInvokeDest()) {
458 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
459 Builder.CreateInvoke(getUnwindResumeOrRethrowFn(), Cont,
460 getInvokeDest(),
461 Builder.CreateLoad(RethrowPtr));
462 EmitBlock(Cont);
463 } else
464 Builder.CreateCall(getUnwindResumeOrRethrowFn(),
465 Builder.CreateLoad(RethrowPtr));
466
467 Builder.CreateUnreachable();
468 }
469
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000470 EmitBlock(EndBlock);
471}
Anders Carlsson77291362010-06-08 22:17:27 +0000472
473/// GenerateCXXAggrDestructorHelper - Generates a helper function which when
474/// invoked, calls the default destructor on array elements in reverse order of
475/// construction.
476llvm::Function *
477CodeGenFunction::GenerateCXXAggrDestructorHelper(const CXXDestructorDecl *D,
478 const ArrayType *Array,
479 llvm::Value *This) {
480 FunctionArgList Args;
481 ImplicitParamDecl *Dst =
482 ImplicitParamDecl::Create(getContext(), 0,
483 SourceLocation(), 0,
484 getContext().getPointerType(getContext().VoidTy));
485 Args.push_back(std::make_pair(Dst, Dst->getType()));
486
Anders Carlsson77291362010-06-08 22:17:27 +0000487 const CGFunctionInfo &FI =
488 CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args,
489 FunctionType::ExtInfo());
490 const llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI, false);
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000491 llvm::Function *Fn =
492 CreateGlobalInitOrDestructFunction(CGM, FTy, "__cxx_global_array_dtor");
Anders Carlsson77291362010-06-08 22:17:27 +0000493
494 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, Args, SourceLocation());
495
496 QualType BaseElementTy = getContext().getBaseElementType(Array);
497 const llvm::Type *BasePtr = ConvertType(BaseElementTy)->getPointerTo();
498 llvm::Value *BaseAddrPtr = Builder.CreateBitCast(This, BasePtr);
499
500 EmitCXXAggrDestructorCall(D, Array, BaseAddrPtr);
501
502 FinishFunction();
503
504 return Fn;
505}