2007-06-02 12:16:21 +08:00
|
|
|
//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-06-02 12:16:21 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code to emit Decl nodes as LLVM code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-05-30 18:30:31 +08:00
|
|
|
#include "CGDebugInfo.h"
|
2007-06-02 12:16:21 +08:00
|
|
|
#include "CodeGenFunction.h"
|
2007-10-17 08:52:43 +08:00
|
|
|
#include "CodeGenModule.h"
|
2008-08-11 13:00:27 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-08-11 13:35:13 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2008-08-25 09:38:19 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2008-04-19 12:17:09 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2008-05-08 13:58:21 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2007-10-17 08:52:43 +08:00
|
|
|
#include "llvm/GlobalVariable.h"
|
2008-12-12 15:38:43 +08:00
|
|
|
#include "llvm/Intrinsics.h"
|
2007-06-14 04:44:40 +08:00
|
|
|
#include "llvm/Type.h"
|
2007-06-02 12:16:21 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
|
|
|
|
2007-06-09 09:20:56 +08:00
|
|
|
void CodeGenFunction::EmitDecl(const Decl &D) {
|
|
|
|
switch (D.getKind()) {
|
2007-06-02 12:16:21 +08:00
|
|
|
default: assert(0 && "Unknown decl kind!");
|
2007-10-09 05:37:32 +08:00
|
|
|
case Decl::ParmVar:
|
2007-06-02 12:16:21 +08:00
|
|
|
assert(0 && "Parmdecls should not be in declstmts!");
|
|
|
|
case Decl::Function: // void X();
|
2008-10-15 08:42:39 +08:00
|
|
|
case Decl::Record: // struct/union/class X;
|
2007-06-02 12:16:21 +08:00
|
|
|
case Decl::Enum: // enum X;
|
2008-08-30 01:28:43 +08:00
|
|
|
case Decl::EnumConstant: // enum ? { X = ? }
|
2008-10-15 08:42:39 +08:00
|
|
|
case Decl::CXXRecord: // struct/union/class X; [C++]
|
2007-06-02 12:16:21 +08:00
|
|
|
// None of these decls require codegen support.
|
|
|
|
return;
|
|
|
|
|
2008-08-30 01:28:43 +08:00
|
|
|
case Decl::Var: {
|
|
|
|
const VarDecl &VD = cast<VarDecl>(D);
|
|
|
|
assert(VD.isBlockVarDecl() &&
|
|
|
|
"Should not see file-scope variables inside a function!");
|
|
|
|
return EmitBlockVarDecl(VD);
|
|
|
|
}
|
2008-12-21 05:51:53 +08:00
|
|
|
|
|
|
|
case Decl::Typedef: { // typedef int X;
|
|
|
|
const TypedefDecl &TD = cast<TypedefDecl>(D);
|
|
|
|
QualType Ty = TD.getUnderlyingType();
|
|
|
|
|
|
|
|
if (Ty->isVariablyModifiedType())
|
|
|
|
EmitVLASize(Ty);
|
|
|
|
}
|
2007-06-02 12:16:21 +08:00
|
|
|
}
|
2007-06-02 12:53:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitBlockVarDecl - This method handles emission of any variable declaration
|
|
|
|
/// inside a function, including static vars etc.
|
2008-04-16 06:42:06 +08:00
|
|
|
void CodeGenFunction::EmitBlockVarDecl(const VarDecl &D) {
|
2007-06-02 12:53:11 +08:00
|
|
|
switch (D.getStorageClass()) {
|
|
|
|
case VarDecl::Static:
|
2007-10-17 08:52:43 +08:00
|
|
|
return EmitStaticBlockVarDecl(D);
|
2007-06-02 12:53:11 +08:00
|
|
|
case VarDecl::Extern:
|
2008-02-17 06:30:38 +08:00
|
|
|
// Don't emit it now, allow it to be emitted lazily on its first use.
|
|
|
|
return;
|
2007-06-02 12:53:11 +08:00
|
|
|
default:
|
|
|
|
assert((D.getStorageClass() == VarDecl::None ||
|
|
|
|
D.getStorageClass() == VarDecl::Auto ||
|
|
|
|
D.getStorageClass() == VarDecl::Register) &&
|
|
|
|
"Unknown storage class");
|
|
|
|
return EmitLocalBlockVarDecl(D);
|
|
|
|
}
|
2007-06-02 12:16:21 +08:00
|
|
|
}
|
|
|
|
|
2009-02-26 03:24:29 +08:00
|
|
|
llvm::GlobalVariable *
|
|
|
|
CodeGenFunction::CreateStaticBlockVarDecl(const VarDecl &D,
|
|
|
|
const char *Separator,
|
|
|
|
llvm::GlobalValue::LinkageTypes
|
|
|
|
Linkage) {
|
|
|
|
QualType Ty = D.getType();
|
|
|
|
assert(Ty->isConstantSizeType() && "VLAs can't be static");
|
|
|
|
|
|
|
|
std::string ContextName;
|
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurFuncDecl))
|
|
|
|
ContextName = CGM.getMangledName(FD);
|
|
|
|
else if (isa<ObjCMethodDecl>(CurFuncDecl))
|
|
|
|
ContextName = std::string(CurFn->getNameStart(),
|
|
|
|
CurFn->getNameStart() + CurFn->getNameLen());
|
|
|
|
else
|
|
|
|
assert(0 && "Unknown context for block var decl");
|
|
|
|
|
|
|
|
const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
|
|
|
|
return new llvm::GlobalVariable(LTy, Ty.isConstant(getContext()), Linkage,
|
|
|
|
llvm::Constant::getNullValue(LTy),
|
|
|
|
ContextName + Separator + D.getNameAsString(),
|
|
|
|
&CGM.getModule(), 0, Ty.getAddressSpace());
|
|
|
|
}
|
|
|
|
|
2009-02-26 03:45:19 +08:00
|
|
|
void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D) {
|
|
|
|
|
|
|
|
llvm::Value *&DMEntry = LocalDeclMap[&D];
|
|
|
|
assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
|
|
|
|
|
|
|
|
llvm::GlobalVariable *GV =
|
|
|
|
CreateStaticBlockVarDecl(D, ".", llvm::GlobalValue::InternalLinkage);
|
|
|
|
|
|
|
|
if (D.getInit()) {
|
2009-02-26 03:24:29 +08:00
|
|
|
llvm::Constant *Init = CGM.EmitConstantExpr(D.getInit(), this);
|
2009-02-18 02:43:32 +08:00
|
|
|
|
|
|
|
// If constant emission failed, then this should be a C++ static
|
|
|
|
// initializer.
|
|
|
|
if (!Init) {
|
2009-02-26 03:24:29 +08:00
|
|
|
if (!getContext().getLangOptions().CPlusPlus)
|
2009-02-18 02:43:32 +08:00
|
|
|
CGM.ErrorUnsupported(D.getInit(), "constant l-value expression");
|
2009-02-26 03:24:29 +08:00
|
|
|
else
|
|
|
|
GenerateStaticCXXBlockVarDeclInit(D, GV);
|
|
|
|
} else {
|
|
|
|
// The initializer may differ in type from the global. Rewrite
|
|
|
|
// the global to match the initializer!?
|
|
|
|
//
|
|
|
|
// FIXME: This matches what we have been doing historically, but
|
|
|
|
// it seems bad. Shouldn't the init expression have the right
|
|
|
|
// type?
|
|
|
|
if (GV->getType() != Init->getType()) {
|
|
|
|
llvm::GlobalVariable *OldGV = GV;
|
|
|
|
|
|
|
|
GV = new llvm::GlobalVariable(Init->getType(), OldGV->isConstant(),
|
|
|
|
OldGV->getLinkage(), Init, "",
|
|
|
|
&CGM.getModule(), 0,
|
|
|
|
D.getType().getAddressSpace());
|
2007-10-27 01:50:58 +08:00
|
|
|
|
2009-02-26 03:24:29 +08:00
|
|
|
// Steal the name of the old global
|
|
|
|
GV->takeName(OldGV);
|
2008-04-19 12:17:09 +08:00
|
|
|
|
2009-02-26 03:24:29 +08:00
|
|
|
// Replace all uses of the old global with the new global
|
|
|
|
llvm::Constant *NewPtrForOldDecl =
|
|
|
|
llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
|
|
|
|
OldGV->replaceAllUsesWith(NewPtrForOldDecl);
|
|
|
|
|
|
|
|
// Erase the old global, since it is no longer used.
|
|
|
|
OldGV->eraseFromParent();
|
|
|
|
}
|
2008-04-19 12:17:09 +08:00
|
|
|
|
2009-02-26 03:24:29 +08:00
|
|
|
GV->setInitializer(Init);
|
|
|
|
}
|
|
|
|
}
|
2008-04-19 12:17:09 +08:00
|
|
|
|
2009-02-13 07:32:54 +08:00
|
|
|
// FIXME: Merge attribute handling.
|
2008-04-19 12:17:09 +08:00
|
|
|
if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) {
|
|
|
|
SourceManager &SM = CGM.getContext().getSourceManager();
|
|
|
|
llvm::Constant *Ann =
|
2009-01-16 15:36:28 +08:00
|
|
|
CGM.EmitAnnotateAttr(GV, AA,
|
|
|
|
SM.getInstantiationLineNumber(D.getLocation()));
|
2008-04-19 12:17:09 +08:00
|
|
|
CGM.AddAnnotation(Ann);
|
|
|
|
}
|
|
|
|
|
2009-02-13 07:32:54 +08:00
|
|
|
if (const SectionAttr *SA = D.getAttr<SectionAttr>())
|
|
|
|
GV->setSection(SA->getName());
|
|
|
|
|
2009-02-14 06:08:43 +08:00
|
|
|
if (D.getAttr<UsedAttr>())
|
|
|
|
CGM.AddUsedGlobal(GV);
|
|
|
|
|
2008-06-08 09:23:18 +08:00
|
|
|
const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType());
|
|
|
|
const llvm::Type *LPtrTy =
|
|
|
|
llvm::PointerType::get(LTy, D.getType().getAddressSpace());
|
|
|
|
DMEntry = llvm::ConstantExpr::getBitCast(GV, LPtrTy);
|
2008-06-05 16:59:10 +08:00
|
|
|
|
|
|
|
// Emit global variable debug descriptor for static vars.
|
2009-02-13 16:11:52 +08:00
|
|
|
CGDebugInfo *DI = getDebugInfo();
|
2009-02-20 08:19:45 +08:00
|
|
|
if (DI) {
|
2008-10-18 00:15:48 +08:00
|
|
|
DI->setLocation(D.getLocation());
|
2008-06-05 16:59:10 +08:00
|
|
|
DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D);
|
|
|
|
}
|
2007-10-17 08:52:43 +08:00
|
|
|
}
|
|
|
|
|
2007-06-02 12:53:11 +08:00
|
|
|
/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
|
|
|
|
/// variable declaration with auto, register, or no storage class specifier.
|
2008-05-08 13:58:21 +08:00
|
|
|
/// These turn into simple stack objects, or GlobalValues depending on target.
|
2008-04-16 06:42:06 +08:00
|
|
|
void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
|
2008-04-07 07:10:54 +08:00
|
|
|
QualType Ty = D.getType();
|
2007-06-02 12:53:11 +08:00
|
|
|
|
|
|
|
llvm::Value *DeclPtr;
|
2008-02-15 20:20:59 +08:00
|
|
|
if (Ty->isConstantSizeType()) {
|
2008-05-08 13:58:21 +08:00
|
|
|
if (!Target.useGlobalsForAutomaticVariables()) {
|
|
|
|
// A normal fixed sized variable becomes an alloca in the entry block.
|
|
|
|
const llvm::Type *LTy = ConvertType(Ty);
|
2008-11-24 11:54:41 +08:00
|
|
|
llvm::AllocaInst *Alloc =
|
2009-02-19 07:53:56 +08:00
|
|
|
CreateTempAlloca(LTy, CGM.getMangledName(&D));
|
2009-02-22 11:23:43 +08:00
|
|
|
Alloc->setAlignment(getContext().getDeclAlignInBytes(&D));
|
2008-06-01 05:20:41 +08:00
|
|
|
DeclPtr = Alloc;
|
2008-05-08 13:58:21 +08:00
|
|
|
} else {
|
|
|
|
// Targets that don't support recursion emit locals as globals.
|
|
|
|
const char *Class =
|
|
|
|
D.getStorageClass() == VarDecl::Register ? ".reg." : ".auto.";
|
2009-02-26 03:45:19 +08:00
|
|
|
DeclPtr = CreateStaticBlockVarDecl(D, Class,
|
|
|
|
llvm::GlobalValue
|
|
|
|
::InternalLinkage);
|
2008-05-08 13:58:21 +08:00
|
|
|
}
|
2008-12-21 04:46:34 +08:00
|
|
|
|
|
|
|
if (Ty->isVariablyModifiedType())
|
|
|
|
EmitVLASize(Ty);
|
2007-06-02 12:53:11 +08:00
|
|
|
} else {
|
2009-02-10 04:41:50 +08:00
|
|
|
if (!DidCallStackSave) {
|
2008-12-12 15:38:43 +08:00
|
|
|
// Save the stack.
|
|
|
|
const llvm::Type *LTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
|
|
|
|
llvm::Value *Stack = CreateTempAlloca(LTy, "saved_stack");
|
|
|
|
|
|
|
|
llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
|
|
|
|
llvm::Value *V = Builder.CreateCall(F);
|
|
|
|
|
|
|
|
Builder.CreateStore(V, Stack);
|
2009-02-10 04:41:50 +08:00
|
|
|
|
|
|
|
DidCallStackSave = true;
|
|
|
|
|
|
|
|
{
|
|
|
|
// Push a cleanup block and restore the stack there.
|
|
|
|
CleanupScope scope(*this);
|
2008-12-12 15:38:43 +08:00
|
|
|
|
2009-02-10 04:41:50 +08:00
|
|
|
V = Builder.CreateLoad(Stack, "tmp");
|
|
|
|
llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
|
|
|
|
Builder.CreateCall(F, V);
|
|
|
|
}
|
2008-12-12 15:38:43 +08:00
|
|
|
}
|
2009-02-10 04:41:50 +08:00
|
|
|
|
2008-12-12 15:38:43 +08:00
|
|
|
// Get the element type.
|
|
|
|
const llvm::Type *LElemTy = ConvertType(Ty);
|
|
|
|
const llvm::Type *LElemPtrTy =
|
|
|
|
llvm::PointerType::get(LElemTy, D.getType().getAddressSpace());
|
|
|
|
|
2008-12-21 04:46:34 +08:00
|
|
|
llvm::Value *VLASize = EmitVLASize(Ty);
|
2008-12-12 15:38:43 +08:00
|
|
|
|
2009-02-06 03:43:10 +08:00
|
|
|
// Downcast the VLA size expression
|
|
|
|
VLASize = Builder.CreateIntCast(VLASize, llvm::Type::Int32Ty, false, "tmp");
|
|
|
|
|
2008-12-12 15:38:43 +08:00
|
|
|
// Allocate memory for the array.
|
|
|
|
llvm::Value *VLA = Builder.CreateAlloca(llvm::Type::Int8Ty, VLASize, "vla");
|
2008-12-21 07:11:59 +08:00
|
|
|
DeclPtr = Builder.CreateBitCast(VLA, LElemPtrTy, "tmp");
|
2007-06-02 12:53:11 +08:00
|
|
|
}
|
2008-12-21 07:11:59 +08:00
|
|
|
|
2007-06-02 12:53:11 +08:00
|
|
|
llvm::Value *&DMEntry = LocalDeclMap[&D];
|
|
|
|
assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
|
|
|
|
DMEntry = DeclPtr;
|
2008-05-30 18:30:31 +08:00
|
|
|
|
|
|
|
// Emit debug info for local var declaration.
|
2009-02-13 16:11:52 +08:00
|
|
|
if (CGDebugInfo *DI = getDebugInfo()) {
|
2008-10-18 00:15:48 +08:00
|
|
|
DI->setLocation(D.getLocation());
|
2008-11-10 14:08:34 +08:00
|
|
|
DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder);
|
2008-05-30 18:30:31 +08:00
|
|
|
}
|
|
|
|
|
2007-07-12 08:39:48 +08:00
|
|
|
// If this local has an initializer, emit it now.
|
2007-08-24 13:35:26 +08:00
|
|
|
if (const Expr *Init = D.getInit()) {
|
2007-08-26 15:29:23 +08:00
|
|
|
if (!hasAggregateLLVMType(Init->getType())) {
|
|
|
|
llvm::Value *V = EmitScalarExpr(Init);
|
2007-08-26 15:30:49 +08:00
|
|
|
Builder.CreateStore(V, DeclPtr, D.getType().isVolatileQualified());
|
2008-04-05 00:54:41 +08:00
|
|
|
} else if (Init->getType()->isAnyComplexType()) {
|
2007-08-27 00:22:13 +08:00
|
|
|
EmitComplexExprIntoAddr(Init, DeclPtr, D.getType().isVolatileQualified());
|
2007-08-26 13:13:54 +08:00
|
|
|
} else {
|
2007-08-26 15:30:49 +08:00
|
|
|
EmitAggExpr(Init, DeclPtr, D.getType().isVolatileQualified());
|
2007-08-26 13:13:54 +08:00
|
|
|
}
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
2009-02-08 07:51:38 +08:00
|
|
|
|
|
|
|
// Handle the cleanup attribute
|
|
|
|
if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) {
|
|
|
|
const FunctionDecl *FD = CA->getFunctionDecl();
|
|
|
|
|
|
|
|
llvm::Constant* F = CGM.GetAddrOfFunction(FD);
|
|
|
|
assert(F && "Could not find function!");
|
|
|
|
|
|
|
|
CleanupScope scope(*this);
|
|
|
|
|
|
|
|
CallArgList Args;
|
|
|
|
Args.push_back(std::make_pair(RValue::get(DeclPtr),
|
|
|
|
getContext().getPointerType(D.getType())));
|
|
|
|
|
|
|
|
EmitCall(CGM.getTypes().getFunctionInfo(FD), F, Args);
|
|
|
|
}
|
2007-06-02 12:16:21 +08:00
|
|
|
}
|
2007-06-14 04:44:40 +08:00
|
|
|
|
2008-05-08 13:58:21 +08:00
|
|
|
/// Emit an alloca (or GlobalValue depending on target)
|
|
|
|
/// for the specified parameter and set up LocalDeclMap.
|
2008-08-16 11:19:19 +08:00
|
|
|
void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg) {
|
|
|
|
// FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
|
2008-10-31 17:52:39 +08:00
|
|
|
assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
|
2008-08-16 11:19:19 +08:00
|
|
|
"Invalid argument to EmitParmDecl");
|
2008-04-07 07:10:54 +08:00
|
|
|
QualType Ty = D.getType();
|
2007-06-14 04:44:40 +08:00
|
|
|
|
|
|
|
llvm::Value *DeclPtr;
|
2008-02-15 20:20:59 +08:00
|
|
|
if (!Ty->isConstantSizeType()) {
|
2007-06-14 04:44:40 +08:00
|
|
|
// Variable sized values always are passed by-reference.
|
|
|
|
DeclPtr = Arg;
|
2008-05-08 13:58:21 +08:00
|
|
|
} else if (Target.useGlobalsForAutomaticVariables()) {
|
2009-02-04 02:07:49 +08:00
|
|
|
// Targets that don't have stack use global address space for parameters.
|
|
|
|
// Specify external linkage for such globals so that llvm optimizer do
|
|
|
|
// not assume there values initialized as zero.
|
2009-02-26 03:45:19 +08:00
|
|
|
DeclPtr = CreateStaticBlockVarDecl(D, ".auto.",
|
|
|
|
llvm::GlobalValue::ExternalLinkage);
|
2007-06-14 04:44:40 +08:00
|
|
|
} else {
|
2008-05-23 06:12:56 +08:00
|
|
|
// A fixed sized single-value variable becomes an alloca in the entry block.
|
2007-06-23 03:05:19 +08:00
|
|
|
const llvm::Type *LTy = ConvertType(Ty);
|
2008-05-23 06:12:56 +08:00
|
|
|
if (LTy->isSingleValueType()) {
|
2007-06-14 04:44:40 +08:00
|
|
|
// TODO: Alignment
|
2008-11-24 12:00:27 +08:00
|
|
|
std::string Name = D.getNameAsString();
|
2008-09-17 08:51:38 +08:00
|
|
|
Name += ".addr";
|
|
|
|
DeclPtr = CreateTempAlloca(LTy, Name.c_str());
|
2007-06-14 04:44:40 +08:00
|
|
|
|
|
|
|
// Store the initial value into the alloca.
|
2008-06-14 07:01:12 +08:00
|
|
|
Builder.CreateStore(Arg, DeclPtr,Ty.isVolatileQualified());
|
2007-06-14 04:44:40 +08:00
|
|
|
} else {
|
|
|
|
// Otherwise, if this is an aggregate, just use the input pointer.
|
2007-06-23 02:57:44 +08:00
|
|
|
DeclPtr = Arg;
|
2007-06-14 04:44:40 +08:00
|
|
|
}
|
2008-11-24 12:00:27 +08:00
|
|
|
Arg->setName(D.getNameAsString());
|
2007-06-14 04:44:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *&DMEntry = LocalDeclMap[&D];
|
|
|
|
assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
|
|
|
|
DMEntry = DeclPtr;
|
2008-05-30 18:30:31 +08:00
|
|
|
|
|
|
|
// Emit debug info for param declaration.
|
2009-02-13 16:11:52 +08:00
|
|
|
if (CGDebugInfo *DI = getDebugInfo()) {
|
2008-10-18 00:15:48 +08:00
|
|
|
DI->setLocation(D.getLocation());
|
2008-11-10 14:08:34 +08:00
|
|
|
DI->EmitDeclareOfArgVariable(&D, DeclPtr, Builder);
|
2008-05-30 18:30:31 +08:00
|
|
|
}
|
2007-06-14 04:44:40 +08:00
|
|
|
}
|
|
|
|
|