opnsense-src/lib/CodeGen/CGDeclCXX.cpp

368 lines
13 KiB
C++
Raw Normal View History

2009-12-15 13:49:47 -05: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"
#include "CGObjCRuntime.h"
#include "CGCXXABI.h"
2010-07-13 13:21:42 -04:00
#include "clang/Frontend/CodeGenOptions.h"
2010-05-27 11:17:06 -04:00
#include "llvm/Intrinsics.h"
2009-12-15 13:49:47 -05:00
using namespace clang;
using namespace CodeGen;
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();
unsigned alignment = Context.getDeclAlign(&D).getQuantity();
QualType type = D.getType();
LValue lv = CGF.MakeAddrLValue(DeclPtr, type, alignment);
const Expr *Init = D.getInit();
if (!CGF.hasAggregateLLVMType(type)) {
CodeGenModule &CGM = CGF.CGM;
if (lv.isObjCStrong())
CGM.getObjCRuntime().EmitObjCGlobalAssign(CGF, CGF.EmitScalarExpr(Init),
DeclPtr, D.isThreadSpecified());
else if (lv.isObjCWeak())
CGM.getObjCRuntime().EmitObjCWeakAssign(CGF, CGF.EmitScalarExpr(Init),
DeclPtr);
else
CGF.EmitScalarInit(Init, &D, lv, false);
} else if (type->isAnyComplexType()) {
CGF.EmitComplexExprIntoAddr(Init, DeclPtr, lv.isVolatile());
2009-12-15 13:49:47 -05:00
} else {
CGF.EmitAggExpr(Init, AggValueSlot::forLValue(lv, true));
2009-12-15 13:49:47 -05:00
}
}
/// Emit code to cause the destruction of the given variable with
/// static storage duration.
2010-05-27 11:17:06 -04:00
static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D,
llvm::Constant *addr) {
2010-05-27 11:17:06 -04:00
CodeGenModule &CGM = CGF.CGM;
// FIXME: __attribute__((cleanup)) ?
2010-05-27 11:17:06 -04:00
QualType type = D.getType();
QualType::DestructionKind dtorKind = type.isDestructedType();
switch (dtorKind) {
case QualType::DK_none:
2010-05-27 11:17:06 -04:00
return;
case QualType::DK_cxx_destructor:
break;
case QualType::DK_objc_strong_lifetime:
case QualType::DK_objc_weak_lifetime:
// We don't care about releasing objects during process teardown.
2010-05-27 11:17:06 -04:00
return;
}
llvm::Constant *function;
llvm::Constant *argument;
// Special-case non-array C++ destructors, where there's a function
// with the right signature that we can just call.
const CXXRecordDecl *record = 0;
if (dtorKind == QualType::DK_cxx_destructor &&
(record = type->getAsCXXRecordDecl())) {
assert(!record->hasTrivialDestructor());
CXXDestructorDecl *dtor = record->getDestructor();
function = CGM.GetAddrOfCXXDestructor(dtor, Dtor_Complete);
argument = addr;
// Otherwise, the standard logic requires a helper function.
} else {
function = CodeGenFunction(CGM).generateDestroyHelper(addr, type,
CGF.getDestroyer(dtorKind),
CGF.needsEHCleanup(dtorKind));
argument = llvm::Constant::getNullValue(CGF.Int8PtrTy);
}
CGF.EmitCXXGlobalDtorRegistration(function, argument);
2010-05-27 11:17:06 -04:00
}
2009-12-15 13:49:47 -05: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-27 11:17:06 -04:00
EmitDeclDestroy(*this, D, DeclPtr);
2009-12-15 13:49:47 -05:00
return;
}
2010-07-13 13:21:42 -04:00
unsigned Alignment = getContext().getDeclAlign(&D).getQuantity();
2010-07-13 13:21:42 -04:00
RValue RV = EmitReferenceBindingToExpr(Init, &D);
EmitStoreOfScalar(RV.getScalarVal(), DeclPtr, false, Alignment, T);
2009-12-15 13:49:47 -05:00
}
void
CodeGenFunction::EmitCXXGlobalDtorRegistration(llvm::Constant *DtorFn,
llvm::Constant *DeclPtr) {
2010-03-21 06:50:08 -04:00
// Generate a global destructor entry if not using __cxa_atexit.
if (!CGM.getCodeGenOpts().CXAAtExit) {
CGM.AddCXXDtorEntry(DtorFn, DeclPtr);
return;
}
2009-12-15 13:49:47 -05:00
// Get the destructor function type
llvm::Type *ArgTys[] = { Int8PtrTy };
llvm::Type *DtorFnTy =
llvm::FunctionType::get(llvm::Type::getVoidTy(getLLVMContext()),
ArgTys, false);
2009-12-15 13:49:47 -05:00
DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy);
llvm::Type *Params[] = { DtorFnTy, Int8PtrTy, Int8PtrTy };
2009-12-15 13:49:47 -05:00
// 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");
if (llvm::Function *Fn = dyn_cast<llvm::Function>(AtExitFn))
Fn->setDoesNotThrow();
2009-12-15 13:49:47 -05:00
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);
2009-12-15 13:49:47 -05:00
}
void CodeGenFunction::EmitCXXGuardedInit(const VarDecl &D,
llvm::GlobalVariable *DeclPtr) {
// If we've been asked to forbid guard variables, emit an error now.
// This diagnostic is hard-coded for Darwin's use case; we can find
// better phrasing if someone else needs it.
if (CGM.getCodeGenOpts().ForbidGuardVariables)
CGM.Error(D.getLocation(),
"this initialization requires a guard variable, which "
"the kernel does not support");
CGM.getCXXABI().EmitGuardedInit(*this, D, DeclPtr);
}
2010-07-13 13:21:42 -04: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());
if (!CGM.getContext().getLangOptions().AppleKext) {
// Set the section if needed.
if (const char *Section =
CGM.getContext().Target.getStaticInitSectionSpecifier())
Fn->setSection(Section);
}
2010-07-13 13:21:42 -04:00
if (!CGM.getLangOptions().Exceptions)
2010-07-13 13:21:42 -04:00
Fn->setDoesNotThrow();
return Fn;
}
2010-01-15 10:39:40 -05:00
void
CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
llvm::GlobalVariable *Addr) {
2010-01-15 10:39:40 -05:00
const llvm::FunctionType *FTy
= llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
false);
// Create a variable initialization function.
llvm::Function *Fn =
2010-07-13 13:21:42 -04:00
CreateGlobalInitOrDestructFunction(*this, FTy, "__cxx_global_var_init");
2010-01-15 10:39:40 -05:00
CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr);
2010-01-15 10:39:40 -05:00
2010-07-13 13:21:42 -04:00
if (D->hasAttr<InitPriorityAttr>()) {
unsigned int order = D->getAttr<InitPriorityAttr>()->getPriority();
OrderGlobalInits Key(order, PrioritizedCXXGlobalInits.size());
PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn));
DelayedCXXInitPosition.erase(D);
}
else {
llvm::DenseMap<const Decl *, unsigned>::iterator I =
DelayedCXXInitPosition.find(D);
if (I == DelayedCXXInitPosition.end()) {
CXXGlobalInits.push_back(Fn);
} else {
assert(CXXGlobalInits[I->second] == 0);
CXXGlobalInits[I->second] = Fn;
DelayedCXXInitPosition.erase(I);
}
2010-07-13 13:21:42 -04:00
}
2010-01-15 10:39:40 -05:00
}
2009-12-15 13:49:47 -05:00
void
CodeGenModule::EmitCXXGlobalInitFunc() {
while (!CXXGlobalInits.empty() && !CXXGlobalInits.back())
CXXGlobalInits.pop_back();
2010-07-13 13:21:42 -04:00
if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty())
2009-12-15 13:49:47 -05:00
return;
const llvm::FunctionType *FTy
= llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
false);
// Create our global initialization function.
2010-07-13 13:21:42 -04:00
llvm::Function *Fn =
CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__I_a");
if (!PrioritizedCXXGlobalInits.empty()) {
llvm::SmallVector<llvm::Constant*, 8> LocalCXXGlobalInits;
llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(),
PrioritizedCXXGlobalInits.end());
for (unsigned i = 0; i < PrioritizedCXXGlobalInits.size(); i++) {
llvm::Function *Fn = PrioritizedCXXGlobalInits[i].second;
LocalCXXGlobalInits.push_back(Fn);
}
LocalCXXGlobalInits.append(CXXGlobalInits.begin(), CXXGlobalInits.end());
2010-07-13 13:21:42 -04:00
CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
&LocalCXXGlobalInits[0],
LocalCXXGlobalInits.size());
}
else
CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
&CXXGlobalInits[0],
CXXGlobalInits.size());
2009-12-15 13:49:47 -05:00
AddGlobalCtor(Fn);
CXXGlobalInits.clear();
PrioritizedCXXGlobalInits.clear();
2009-12-15 13:49:47 -05:00
}
2010-03-21 06:50:08 -04:00
void CodeGenModule::EmitCXXGlobalDtorFunc() {
if (CXXGlobalDtors.empty())
return;
const llvm::FunctionType *FTy
= llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
false);
// Create our global destructor function.
llvm::Function *Fn =
2010-07-13 13:21:42 -04:00
CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__D_a");
2010-03-21 06:50:08 -04:00
CodeGenFunction(*this).GenerateCXXGlobalDtorFunc(Fn, CXXGlobalDtors);
AddGlobalDtor(Fn);
}
/// Emit the code necessary to initialize the given global variable.
2010-01-15 10:39:40 -05:00
void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
const VarDecl *D,
llvm::GlobalVariable *Addr) {
StartFunction(GlobalDecl(), getContext().VoidTy, Fn,
getTypes().getNullaryFunctionInfo(),
FunctionArgList(), SourceLocation());
2010-01-15 10:39:40 -05:00
// Use guarded initialization if the global variable is weak. This
// occurs for, e.g., instantiated static data members and
// definitions explicitly marked weak.
if (Addr->getLinkage() == llvm::GlobalValue::WeakODRLinkage ||
Addr->getLinkage() == llvm::GlobalValue::WeakAnyLinkage) {
EmitCXXGuardedInit(*D, Addr);
} else {
EmitCXXGlobalVarDeclInit(*D, Addr);
}
2010-01-15 10:39:40 -05:00
FinishFunction();
}
2009-12-15 13:49:47 -05:00
void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
2010-01-15 10:39:40 -05:00
llvm::Constant **Decls,
2009-12-15 13:49:47 -05:00
unsigned NumDecls) {
StartFunction(GlobalDecl(), getContext().VoidTy, Fn,
getTypes().getNullaryFunctionInfo(),
FunctionArgList(), SourceLocation());
2009-12-15 13:49:47 -05:00
RunCleanupsScope Scope(*this);
// When building in Objective-C++ ARC mode, create an autorelease pool
// around the global initializers.
if (getLangOptions().ObjCAutoRefCount && getLangOptions().CPlusPlus) {
llvm::Value *token = EmitObjCAutoreleasePoolPush();
EmitObjCAutoreleasePoolCleanup(token);
}
2010-01-15 10:39:40 -05:00
for (unsigned i = 0; i != NumDecls; ++i)
if (Decls[i])
Builder.CreateCall(Decls[i]);
2009-12-15 13:49:47 -05:00
Scope.ForceCleanup();
2009-12-15 13:49:47 -05:00
FinishFunction();
}
2010-03-21 06:50:08 -04:00
void CodeGenFunction::GenerateCXXGlobalDtorFunc(llvm::Function *Fn,
2010-07-13 13:21:42 -04:00
const std::vector<std::pair<llvm::WeakVH, llvm::Constant*> >
2010-03-21 06:50:08 -04:00
&DtorsAndObjects) {
StartFunction(GlobalDecl(), getContext().VoidTy, Fn,
getTypes().getNullaryFunctionInfo(),
FunctionArgList(), SourceLocation());
2010-03-21 06:50:08 -04:00
// Emit the dtors, in reverse order from construction.
2010-05-04 12:12:48 -04:00
for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; ++i) {
2010-07-13 13:21:42 -04:00
llvm::Value *Callee = DtorsAndObjects[e - i - 1].first;
2010-05-04 12:12:48 -04: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-21 06:50:08 -04:00
FinishFunction();
}
/// generateDestroyHelper - Generates a helper function which, when
/// invoked, destroys the given object.
2010-07-13 13:21:42 -04:00
llvm::Function *
CodeGenFunction::generateDestroyHelper(llvm::Constant *addr,
QualType type,
Destroyer &destroyer,
bool useEHCleanupForArray) {
FunctionArgList args;
ImplicitParamDecl dst(0, SourceLocation(), 0, getContext().VoidPtrTy);
args.push_back(&dst);
2010-07-13 13:21:42 -04:00
const CGFunctionInfo &FI =
CGM.getTypes().getFunctionInfo(getContext().VoidTy, args,
2010-07-13 13:21:42 -04:00
FunctionType::ExtInfo());
const llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI, false);
llvm::Function *fn =
2010-07-13 13:21:42 -04:00
CreateGlobalInitOrDestructFunction(CGM, FTy, "__cxx_global_array_dtor");
StartFunction(GlobalDecl(), getContext().VoidTy, fn, FI, args,
SourceLocation());
2010-07-13 13:21:42 -04:00
emitDestroy(addr, type, destroyer, useEHCleanupForArray);
2010-07-13 13:21:42 -04:00
FinishFunction();
return fn;
2010-07-13 13:21:42 -04:00
}