2009-12-10 08:16:00 +08:00
|
|
|
//===--- CGDeclCXX.cpp - Emit LLVM Code for C++ declarations --------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code dealing with code generation of C++ declarations
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenFunction.h"
|
2010-06-16 07:19:56 +08:00
|
|
|
#include "clang/Frontend/CodeGenOptions.h"
|
2010-05-16 09:24:12 +08:00
|
|
|
#include "llvm/Intrinsics.h"
|
|
|
|
|
2009-12-10 08:16:00 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2009-12-10 08:57:45 +08:00
|
|
|
static void EmitDeclInit(CodeGenFunction &CGF, const VarDecl &D,
|
|
|
|
llvm::Constant *DeclPtr) {
|
|
|
|
assert(D.hasGlobalStorage() && "VarDecl must have global storage!");
|
|
|
|
assert(!D.getType()->isReferenceType() &&
|
|
|
|
"Should not call EmitDeclInit on a reference!");
|
|
|
|
|
|
|
|
ASTContext &Context = CGF.getContext();
|
|
|
|
|
2009-12-10 08:16:00 +08:00
|
|
|
const Expr *Init = D.getInit();
|
|
|
|
QualType T = D.getType();
|
2009-12-10 08:57:45 +08:00
|
|
|
bool isVolatile = Context.getCanonicalType(T).isVolatileQualified();
|
2009-12-10 08:16:00 +08:00
|
|
|
|
2009-12-10 08:57:45 +08:00
|
|
|
if (!CGF.hasAggregateLLVMType(T)) {
|
|
|
|
llvm::Value *V = CGF.EmitScalarExpr(Init);
|
|
|
|
CGF.EmitStoreOfScalar(V, DeclPtr, isVolatile, T);
|
2009-12-10 08:16:00 +08:00
|
|
|
} else if (T->isAnyComplexType()) {
|
2009-12-10 08:57:45 +08:00
|
|
|
CGF.EmitComplexExprIntoAddr(Init, DeclPtr, isVolatile);
|
2009-12-10 08:16:00 +08:00
|
|
|
} else {
|
2009-12-10 08:57:45 +08:00
|
|
|
CGF.EmitAggExpr(Init, DeclPtr, isVolatile);
|
2009-12-10 08:16:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-05 23:38:32 +08:00
|
|
|
static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D,
|
|
|
|
llvm::Constant *DeclPtr) {
|
|
|
|
CodeGenModule &CGM = CGF.CGM;
|
|
|
|
ASTContext &Context = CGF.getContext();
|
|
|
|
|
|
|
|
const Expr *Init = D.getInit();
|
|
|
|
QualType T = D.getType();
|
|
|
|
if (!CGF.hasAggregateLLVMType(T) || T->isAnyComplexType())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Avoid generating destructor(s) for initialized objects.
|
|
|
|
if (!isa<CXXConstructExpr>(Init))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const ConstantArrayType *Array = Context.getAsConstantArrayType(T);
|
|
|
|
if (Array)
|
|
|
|
T = Context.getBaseElementType(Array);
|
|
|
|
|
|
|
|
const RecordType *RT = T->getAs<RecordType>();
|
|
|
|
if (!RT)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
|
|
|
|
if (RD->hasTrivialDestructor())
|
|
|
|
return;
|
|
|
|
|
|
|
|
CXXDestructorDecl *Dtor = RD->getDestructor(Context);
|
|
|
|
|
|
|
|
llvm::Constant *DtorFn;
|
|
|
|
if (Array) {
|
|
|
|
DtorFn =
|
2010-06-09 06:14:59 +08:00
|
|
|
CodeGenFunction(CGM).GenerateCXXAggrDestructorHelper(Dtor, Array,
|
|
|
|
DeclPtr);
|
2010-05-05 23:38:32 +08:00
|
|
|
const llvm::Type *Int8PtrTy =
|
2010-06-09 06:14:59 +08:00
|
|
|
llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
|
2010-05-05 23:38:32 +08:00
|
|
|
DeclPtr = llvm::Constant::getNullValue(Int8PtrTy);
|
|
|
|
} else
|
|
|
|
DtorFn = CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete);
|
|
|
|
|
|
|
|
CGF.EmitCXXGlobalDtorRegistration(DtorFn, DeclPtr);
|
|
|
|
}
|
|
|
|
|
2009-12-10 08:57:45 +08:00
|
|
|
void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
|
|
|
|
llvm::Constant *DeclPtr) {
|
|
|
|
|
|
|
|
const Expr *Init = D.getInit();
|
|
|
|
QualType T = D.getType();
|
|
|
|
|
|
|
|
if (!T->isReferenceType()) {
|
|
|
|
EmitDeclInit(*this, D, DeclPtr);
|
2010-05-05 23:38:32 +08:00
|
|
|
EmitDeclDestroy(*this, D, DeclPtr);
|
2009-12-10 08:57:45 +08:00
|
|
|
return;
|
|
|
|
}
|
2010-01-26 05:40:39 +08:00
|
|
|
if (Init->isLvalue(getContext()) == Expr::LV_Valid) {
|
2010-06-27 00:35:32 +08:00
|
|
|
RValue RV = EmitReferenceBindingToExpr(Init, &D);
|
2010-01-26 05:40:39 +08:00
|
|
|
EmitStoreOfScalar(RV.getScalarVal(), DeclPtr, false, T);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ErrorUnsupported(Init,
|
|
|
|
"global variable that binds reference to a non-lvalue");
|
2009-12-10 08:57:45 +08:00
|
|
|
}
|
2009-12-10 08:30:05 +08:00
|
|
|
|
2010-03-20 12:15:41 +08:00
|
|
|
void
|
|
|
|
CodeGenFunction::EmitCXXGlobalDtorRegistration(llvm::Constant *DtorFn,
|
|
|
|
llvm::Constant *DeclPtr) {
|
|
|
|
// Generate a global destructor entry if not using __cxa_atexit.
|
|
|
|
if (!CGM.getCodeGenOpts().CXAAtExit) {
|
|
|
|
CGM.AddCXXDtorEntry(DtorFn, DeclPtr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-12-10 08:30:05 +08:00
|
|
|
const llvm::Type *Int8PtrTy =
|
|
|
|
llvm::Type::getInt8Ty(VMContext)->getPointerTo();
|
|
|
|
|
|
|
|
std::vector<const llvm::Type *> Params;
|
|
|
|
Params.push_back(Int8PtrTy);
|
|
|
|
|
|
|
|
// Get the destructor function type
|
|
|
|
const llvm::Type *DtorFnTy =
|
|
|
|
llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false);
|
|
|
|
DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy);
|
|
|
|
|
|
|
|
Params.clear();
|
|
|
|
Params.push_back(DtorFnTy);
|
|
|
|
Params.push_back(Int8PtrTy);
|
|
|
|
Params.push_back(Int8PtrTy);
|
|
|
|
|
|
|
|
// Get the __cxa_atexit function type
|
|
|
|
// extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d );
|
|
|
|
const llvm::FunctionType *AtExitFnTy =
|
|
|
|
llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false);
|
|
|
|
|
|
|
|
llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy,
|
|
|
|
"__cxa_atexit");
|
|
|
|
|
|
|
|
llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy,
|
|
|
|
"__dso_handle");
|
|
|
|
llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy),
|
|
|
|
llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy),
|
|
|
|
llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) };
|
|
|
|
Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args));
|
|
|
|
}
|
|
|
|
|
2010-06-09 06:40:05 +08:00
|
|
|
static llvm::Function *
|
|
|
|
CreateGlobalInitOrDestructFunction(CodeGenModule &CGM,
|
|
|
|
const llvm::FunctionType *FTy,
|
|
|
|
llvm::StringRef Name) {
|
|
|
|
llvm::Function *Fn =
|
|
|
|
llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
|
|
|
|
Name, &CGM.getModule());
|
|
|
|
|
2010-06-09 06:47:50 +08:00
|
|
|
// Set the section if needed.
|
|
|
|
if (const char *Section =
|
|
|
|
CGM.getContext().Target.getStaticInitSectionSpecifier())
|
|
|
|
Fn->setSection(Section);
|
|
|
|
|
2010-06-09 06:40:05 +08:00
|
|
|
return Fn;
|
|
|
|
}
|
|
|
|
|
2010-03-20 12:15:41 +08:00
|
|
|
void
|
|
|
|
CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D) {
|
2010-01-08 08:50:11 +08:00
|
|
|
const llvm::FunctionType *FTy
|
|
|
|
= llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
|
|
|
|
false);
|
|
|
|
|
|
|
|
// Create a variable initialization function.
|
|
|
|
llvm::Function *Fn =
|
2010-06-09 06:40:05 +08:00
|
|
|
CreateGlobalInitOrDestructFunction(*this, FTy, "__cxx_global_var_init");
|
2010-01-08 08:50:11 +08:00
|
|
|
|
2010-03-20 12:15:41 +08:00
|
|
|
CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D);
|
2010-01-08 08:50:11 +08:00
|
|
|
|
2010-06-22 02:45:05 +08:00
|
|
|
if (D->hasAttr<InitPriorityAttr>()) {
|
|
|
|
unsigned int order = D->getAttr<InitPriorityAttr>()->getPriority();
|
2010-06-27 14:32:58 +08:00
|
|
|
OrderGlobalInits Key(order, PrioritizedCXXGlobalInits.size());
|
2010-06-22 05:27:42 +08:00
|
|
|
PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn));
|
2010-06-22 02:45:05 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
CXXGlobalInits.push_back(Fn);
|
2010-01-08 08:50:11 +08:00
|
|
|
}
|
|
|
|
|
2010-03-20 12:15:41 +08:00
|
|
|
void
|
|
|
|
CodeGenModule::EmitCXXGlobalInitFunc() {
|
2010-06-22 02:45:05 +08:00
|
|
|
if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty())
|
2009-12-10 08:30:05 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
const llvm::FunctionType *FTy
|
|
|
|
= llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
|
|
|
|
false);
|
|
|
|
|
|
|
|
// Create our global initialization function.
|
2010-06-09 06:40:05 +08:00
|
|
|
llvm::Function *Fn =
|
|
|
|
CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__I_a");
|
2010-03-20 12:15:41 +08:00
|
|
|
|
2010-06-22 02:45:05 +08:00
|
|
|
if (!PrioritizedCXXGlobalInits.empty()) {
|
2010-06-22 03:49:38 +08:00
|
|
|
llvm::SmallVector<llvm::Constant*, 8> LocalCXXGlobalInits;
|
|
|
|
llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(),
|
2010-06-22 08:23:08 +08:00
|
|
|
PrioritizedCXXGlobalInits.end());
|
2010-06-22 02:45:05 +08:00
|
|
|
for (unsigned i = 0; i < PrioritizedCXXGlobalInits.size(); i++) {
|
|
|
|
llvm::Function *Fn = PrioritizedCXXGlobalInits[i].second;
|
|
|
|
LocalCXXGlobalInits.push_back(Fn);
|
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < CXXGlobalInits.size(); i++)
|
|
|
|
LocalCXXGlobalInits.push_back(CXXGlobalInits[i]);
|
|
|
|
CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
|
|
|
|
&LocalCXXGlobalInits[0],
|
|
|
|
LocalCXXGlobalInits.size());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
|
|
|
|
&CXXGlobalInits[0],
|
|
|
|
CXXGlobalInits.size());
|
2010-03-20 12:15:41 +08:00
|
|
|
AddGlobalCtor(Fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenModule::EmitCXXGlobalDtorFunc() {
|
|
|
|
if (CXXGlobalDtors.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const llvm::FunctionType *FTy
|
|
|
|
= llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
|
|
|
|
false);
|
2009-12-10 08:30:05 +08:00
|
|
|
|
2010-03-20 12:15:41 +08:00
|
|
|
// Create our global destructor function.
|
|
|
|
llvm::Function *Fn =
|
2010-06-09 06:40:05 +08:00
|
|
|
CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__D_a");
|
2010-03-20 12:15:41 +08:00
|
|
|
|
|
|
|
CodeGenFunction(*this).GenerateCXXGlobalDtorFunc(Fn, CXXGlobalDtors);
|
|
|
|
AddGlobalDtor(Fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
|
|
|
|
const VarDecl *D) {
|
2010-01-08 08:50:11 +08:00
|
|
|
StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
|
|
|
|
SourceLocation());
|
|
|
|
|
2010-03-20 12:15:41 +08:00
|
|
|
llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D);
|
|
|
|
EmitCXXGlobalVarDeclInit(*D, DeclPtr);
|
2010-01-08 08:50:11 +08:00
|
|
|
|
|
|
|
FinishFunction();
|
2010-03-20 12:15:41 +08:00
|
|
|
}
|
2009-12-10 08:30:05 +08:00
|
|
|
|
2010-03-20 12:15:41 +08:00
|
|
|
void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
|
|
|
|
llvm::Constant **Decls,
|
|
|
|
unsigned NumDecls) {
|
|
|
|
StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
|
|
|
|
SourceLocation());
|
|
|
|
|
|
|
|
for (unsigned i = 0; i != NumDecls; ++i)
|
|
|
|
Builder.CreateCall(Decls[i]);
|
|
|
|
|
|
|
|
FinishFunction();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::GenerateCXXGlobalDtorFunc(llvm::Function *Fn,
|
2010-06-19 13:52:45 +08:00
|
|
|
const std::vector<std::pair<llvm::WeakVH, llvm::Constant*> >
|
2010-03-20 12:15:41 +08:00
|
|
|
&DtorsAndObjects) {
|
|
|
|
StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
|
|
|
|
SourceLocation());
|
|
|
|
|
|
|
|
// Emit the dtors, in reverse order from construction.
|
2010-04-27 04:35:54 +08:00
|
|
|
for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; ++i) {
|
2010-06-19 13:52:45 +08:00
|
|
|
llvm::Value *Callee = DtorsAndObjects[e - i - 1].first;
|
2010-04-27 04:35:54 +08:00
|
|
|
llvm::CallInst *CI = Builder.CreateCall(Callee,
|
|
|
|
DtorsAndObjects[e - i - 1].second);
|
|
|
|
// Make sure the call and the callee agree on calling convention.
|
|
|
|
if (llvm::Function *F = dyn_cast<llvm::Function>(Callee))
|
|
|
|
CI->setCallingConv(F->getCallingConv());
|
|
|
|
}
|
2010-03-20 12:15:41 +08:00
|
|
|
|
|
|
|
FinishFunction();
|
2009-12-10 08:30:05 +08:00
|
|
|
}
|
|
|
|
|
2010-02-07 07:23:06 +08:00
|
|
|
static llvm::Constant *getGuardAcquireFn(CodeGenFunction &CGF) {
|
|
|
|
// int __cxa_guard_acquire(__int64_t *guard_object);
|
|
|
|
|
|
|
|
const llvm::Type *Int64PtrTy =
|
|
|
|
llvm::Type::getInt64PtrTy(CGF.getLLVMContext());
|
|
|
|
|
|
|
|
std::vector<const llvm::Type*> Args(1, Int64PtrTy);
|
|
|
|
|
|
|
|
const llvm::FunctionType *FTy =
|
|
|
|
llvm::FunctionType::get(CGF.ConvertType(CGF.getContext().IntTy),
|
|
|
|
Args, /*isVarArg=*/false);
|
|
|
|
|
|
|
|
return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire");
|
|
|
|
}
|
|
|
|
|
|
|
|
static llvm::Constant *getGuardReleaseFn(CodeGenFunction &CGF) {
|
|
|
|
// void __cxa_guard_release(__int64_t *guard_object);
|
|
|
|
|
|
|
|
const llvm::Type *Int64PtrTy =
|
|
|
|
llvm::Type::getInt64PtrTy(CGF.getLLVMContext());
|
|
|
|
|
|
|
|
std::vector<const llvm::Type*> Args(1, Int64PtrTy);
|
|
|
|
|
|
|
|
const llvm::FunctionType *FTy =
|
|
|
|
llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
|
|
|
|
Args, /*isVarArg=*/false);
|
|
|
|
|
|
|
|
return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release");
|
|
|
|
}
|
|
|
|
|
|
|
|
static llvm::Constant *getGuardAbortFn(CodeGenFunction &CGF) {
|
|
|
|
// void __cxa_guard_abort(__int64_t *guard_object);
|
|
|
|
|
|
|
|
const llvm::Type *Int64PtrTy =
|
|
|
|
llvm::Type::getInt64PtrTy(CGF.getLLVMContext());
|
|
|
|
|
|
|
|
std::vector<const llvm::Type*> Args(1, Int64PtrTy);
|
|
|
|
|
|
|
|
const llvm::FunctionType *FTy =
|
|
|
|
llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
|
|
|
|
Args, /*isVarArg=*/false);
|
|
|
|
|
|
|
|
return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort");
|
|
|
|
}
|
|
|
|
|
2009-12-10 08:30:05 +08:00
|
|
|
void
|
|
|
|
CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D,
|
|
|
|
llvm::GlobalVariable *GV) {
|
2010-05-05 04:45:42 +08:00
|
|
|
// Bail out early if this initializer isn't reachable.
|
|
|
|
if (!Builder.GetInsertBlock()) return;
|
|
|
|
|
2010-02-07 07:23:06 +08:00
|
|
|
bool ThreadsafeStatics = getContext().getLangOptions().ThreadsafeStatics;
|
|
|
|
|
2009-12-10 08:30:05 +08:00
|
|
|
llvm::SmallString<256> GuardVName;
|
|
|
|
CGM.getMangleContext().mangleGuardVariable(&D, GuardVName);
|
|
|
|
|
|
|
|
// Create the guard variable.
|
2010-02-07 07:23:06 +08:00
|
|
|
llvm::GlobalValue *GuardVariable =
|
|
|
|
new llvm::GlobalVariable(CGM.getModule(), Int64Ty,
|
2009-12-10 08:30:05 +08:00
|
|
|
false, GV->getLinkage(),
|
2010-02-07 07:23:06 +08:00
|
|
|
llvm::Constant::getNullValue(Int64Ty),
|
2009-12-10 08:30:05 +08:00
|
|
|
GuardVName.str());
|
|
|
|
|
|
|
|
// Load the first byte of the guard variable.
|
|
|
|
const llvm::Type *PtrTy
|
|
|
|
= llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
|
2010-02-07 07:23:06 +08:00
|
|
|
llvm::Value *V =
|
|
|
|
Builder.CreateLoad(Builder.CreateBitCast(GuardVariable, PtrTy), "tmp");
|
2009-12-10 08:30:05 +08:00
|
|
|
|
2010-02-07 07:23:06 +08:00
|
|
|
llvm::BasicBlock *InitCheckBlock = createBasicBlock("init.check");
|
2009-12-10 08:30:05 +08:00
|
|
|
llvm::BasicBlock *EndBlock = createBasicBlock("init.end");
|
|
|
|
|
2010-02-07 07:23:06 +08:00
|
|
|
// Check if the first byte of the guard variable is zero.
|
|
|
|
Builder.CreateCondBr(Builder.CreateIsNull(V, "tobool"),
|
|
|
|
InitCheckBlock, EndBlock);
|
2009-12-10 08:30:05 +08:00
|
|
|
|
2010-02-07 07:23:06 +08:00
|
|
|
EmitBlock(InitCheckBlock);
|
|
|
|
|
2010-05-16 09:24:12 +08:00
|
|
|
// Variables used when coping with thread-safe statics and exceptions.
|
|
|
|
llvm::BasicBlock *SavedLandingPad = 0;
|
|
|
|
llvm::BasicBlock *LandingPad = 0;
|
|
|
|
if (ThreadsafeStatics) {
|
2010-02-07 07:23:06 +08:00
|
|
|
// Call __cxa_guard_acquire.
|
|
|
|
V = Builder.CreateCall(getGuardAcquireFn(*this), GuardVariable);
|
|
|
|
|
|
|
|
llvm::BasicBlock *InitBlock = createBasicBlock("init");
|
|
|
|
|
|
|
|
Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"),
|
|
|
|
InitBlock, EndBlock);
|
|
|
|
|
|
|
|
if (Exceptions) {
|
2010-05-16 09:24:12 +08:00
|
|
|
SavedLandingPad = getInvokeDest();
|
|
|
|
LandingPad = createBasicBlock("guard.lpad");
|
|
|
|
setInvokeDest(LandingPad);
|
2010-05-16 08:44:00 +08:00
|
|
|
}
|
2010-05-16 09:24:12 +08:00
|
|
|
|
|
|
|
EmitBlock(InitBlock);
|
2010-02-07 07:23:06 +08:00
|
|
|
}
|
2009-12-10 08:30:05 +08:00
|
|
|
|
2009-12-10 08:57:45 +08:00
|
|
|
if (D.getType()->isReferenceType()) {
|
2009-12-10 09:58:33 +08:00
|
|
|
QualType T = D.getType();
|
2009-12-10 09:05:11 +08:00
|
|
|
// We don't want to pass true for IsInitializer here, because a static
|
|
|
|
// reference to a temporary does not extend its lifetime.
|
2010-06-27 00:35:32 +08:00
|
|
|
// FIXME: This is incorrect.
|
2010-02-04 00:38:03 +08:00
|
|
|
RValue RV = EmitReferenceBindingToExpr(D.getInit(),
|
2010-06-27 00:35:32 +08:00
|
|
|
/*InitializedDecl=*/0);
|
2009-12-10 09:58:33 +08:00
|
|
|
EmitStoreOfScalar(RV.getScalarVal(), GV, /*Volatile=*/false, T);
|
|
|
|
|
2009-12-10 08:57:45 +08:00
|
|
|
} else
|
|
|
|
EmitDeclInit(*this, D, GV);
|
2009-12-10 08:30:05 +08:00
|
|
|
|
2010-02-07 07:23:06 +08:00
|
|
|
if (ThreadsafeStatics) {
|
|
|
|
// Call __cxa_guard_release.
|
2010-05-16 09:24:12 +08:00
|
|
|
Builder.CreateCall(getGuardReleaseFn(*this), GuardVariable);
|
2010-02-07 07:23:06 +08:00
|
|
|
} else {
|
|
|
|
llvm::Value *One =
|
|
|
|
llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 1);
|
|
|
|
Builder.CreateStore(One, Builder.CreateBitCast(GuardVariable, PtrTy));
|
|
|
|
}
|
2009-12-10 08:30:05 +08:00
|
|
|
|
2010-05-05 23:38:32 +08:00
|
|
|
// Register the call to the destructor.
|
|
|
|
if (!D.getType()->isReferenceType())
|
|
|
|
EmitDeclDestroy(*this, D, GV);
|
|
|
|
|
2010-05-16 09:24:12 +08:00
|
|
|
if (ThreadsafeStatics && Exceptions) {
|
|
|
|
// If an exception is thrown during initialization, call __cxa_guard_abort
|
|
|
|
// along the exceptional edge.
|
|
|
|
EmitBranch(EndBlock);
|
|
|
|
|
|
|
|
// Construct the landing pad.
|
|
|
|
EmitBlock(LandingPad);
|
|
|
|
|
|
|
|
// Personality function and LLVM intrinsics.
|
|
|
|
llvm::Constant *Personality =
|
|
|
|
CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
|
|
|
|
(VMContext),
|
|
|
|
true),
|
|
|
|
"__gxx_personality_v0");
|
|
|
|
Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
|
|
|
|
llvm::Value *llvm_eh_exception =
|
|
|
|
CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
|
|
|
|
llvm::Value *llvm_eh_selector =
|
|
|
|
CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
|
|
|
|
|
|
|
|
// Exception object
|
|
|
|
llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
|
|
|
|
llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
|
|
|
|
|
|
|
|
// Call the selector function.
|
|
|
|
const llvm::PointerType *PtrToInt8Ty
|
|
|
|
= llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
|
|
|
|
llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
|
|
|
|
llvm::Value* SelectorArgs[3] = { Exc, Personality, Null };
|
|
|
|
Builder.CreateCall(llvm_eh_selector, SelectorArgs, SelectorArgs + 3,
|
|
|
|
"selector");
|
|
|
|
Builder.CreateStore(Exc, RethrowPtr);
|
|
|
|
|
|
|
|
// Call __cxa_guard_abort along the exceptional edge.
|
|
|
|
Builder.CreateCall(getGuardAbortFn(*this), GuardVariable);
|
|
|
|
|
|
|
|
setInvokeDest(SavedLandingPad);
|
|
|
|
|
|
|
|
// Rethrow the current exception.
|
|
|
|
if (getInvokeDest()) {
|
|
|
|
llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
|
|
|
|
Builder.CreateInvoke(getUnwindResumeOrRethrowFn(), Cont,
|
|
|
|
getInvokeDest(),
|
|
|
|
Builder.CreateLoad(RethrowPtr));
|
|
|
|
EmitBlock(Cont);
|
|
|
|
} else
|
|
|
|
Builder.CreateCall(getUnwindResumeOrRethrowFn(),
|
|
|
|
Builder.CreateLoad(RethrowPtr));
|
|
|
|
|
|
|
|
Builder.CreateUnreachable();
|
|
|
|
}
|
|
|
|
|
2009-12-10 08:30:05 +08:00
|
|
|
EmitBlock(EndBlock);
|
|
|
|
}
|
2010-06-09 06:17:27 +08:00
|
|
|
|
|
|
|
/// GenerateCXXAggrDestructorHelper - Generates a helper function which when
|
|
|
|
/// invoked, calls the default destructor on array elements in reverse order of
|
|
|
|
/// construction.
|
|
|
|
llvm::Function *
|
|
|
|
CodeGenFunction::GenerateCXXAggrDestructorHelper(const CXXDestructorDecl *D,
|
|
|
|
const ArrayType *Array,
|
|
|
|
llvm::Value *This) {
|
|
|
|
FunctionArgList Args;
|
|
|
|
ImplicitParamDecl *Dst =
|
|
|
|
ImplicitParamDecl::Create(getContext(), 0,
|
|
|
|
SourceLocation(), 0,
|
|
|
|
getContext().getPointerType(getContext().VoidTy));
|
|
|
|
Args.push_back(std::make_pair(Dst, Dst->getType()));
|
|
|
|
|
|
|
|
const CGFunctionInfo &FI =
|
|
|
|
CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args,
|
|
|
|
FunctionType::ExtInfo());
|
|
|
|
const llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI, false);
|
2010-06-09 06:40:05 +08:00
|
|
|
llvm::Function *Fn =
|
|
|
|
CreateGlobalInitOrDestructFunction(CGM, FTy, "__cxx_global_array_dtor");
|
2010-06-09 06:17:27 +08:00
|
|
|
|
|
|
|
StartFunction(GlobalDecl(), getContext().VoidTy, Fn, Args, SourceLocation());
|
|
|
|
|
|
|
|
QualType BaseElementTy = getContext().getBaseElementType(Array);
|
|
|
|
const llvm::Type *BasePtr = ConvertType(BaseElementTy)->getPointerTo();
|
|
|
|
llvm::Value *BaseAddrPtr = Builder.CreateBitCast(This, BasePtr);
|
|
|
|
|
|
|
|
EmitCXXAggrDestructorCall(D, Array, BaseAddrPtr);
|
|
|
|
|
|
|
|
FinishFunction();
|
|
|
|
|
|
|
|
return Fn;
|
|
|
|
}
|