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"
|
2009-03-04 11:23:46 +08:00
|
|
|
#include "llvm/Target/TargetData.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) {
|
2009-04-14 05:08:27 +08:00
|
|
|
if (D.hasAttr<AsmLabelAttr>())
|
2009-03-31 04:32:06 +08:00
|
|
|
CGM.ErrorUnsupported(&D, "__asm__");
|
|
|
|
|
2007-06-02 12:53:11 +08:00
|
|
|
switch (D.getStorageClass()) {
|
2009-04-14 10:25:56 +08:00
|
|
|
case VarDecl::None:
|
|
|
|
case VarDecl::Auto:
|
|
|
|
case VarDecl::Register:
|
|
|
|
return EmitLocalBlockVarDecl(D);
|
2007-06-02 12:53:11 +08:00
|
|
|
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:
|
2009-04-14 10:25:56 +08:00
|
|
|
case VarDecl::PrivateExtern:
|
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
|
|
|
}
|
2009-04-14 10:25:56 +08:00
|
|
|
|
|
|
|
assert(0 && "Unknown storage class");
|
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");
|
|
|
|
|
2009-04-02 11:29:47 +08:00
|
|
|
std::string Name;
|
|
|
|
if (getContext().getLangOptions().CPlusPlus) {
|
|
|
|
Name = CGM.getMangledName(&D);
|
|
|
|
} else {
|
|
|
|
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");
|
|
|
|
|
|
|
|
Name = ContextName + Separator + D.getNameAsString();
|
|
|
|
}
|
2009-02-26 03:24:29 +08:00
|
|
|
|
|
|
|
const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
|
|
|
|
return new llvm::GlobalVariable(LTy, Ty.isConstant(getContext()), Linkage,
|
2009-04-02 11:29:47 +08:00
|
|
|
llvm::Constant::getNullValue(LTy), Name,
|
2009-04-20 05:05:03 +08:00
|
|
|
&CGM.getModule(), D.isThreadSpecified(),
|
|
|
|
Ty.getAddressSpace());
|
2009-02-26 03:24:29 +08:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2009-02-26 04:08:33 +08:00
|
|
|
// Store into LocalDeclMap before generating initializer to handle
|
|
|
|
// circular references.
|
|
|
|
DMEntry = GV;
|
|
|
|
|
2009-02-26 03:45:19 +08:00
|
|
|
if (D.getInit()) {
|
2009-04-08 12:48:15 +08:00
|
|
|
llvm::Constant *Init = CGM.EmitConstantExpr(D.getInit(), D.getType(), 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
|
2009-03-04 12:22:58 +08:00
|
|
|
// the global to match the initializer. (We have to do this
|
|
|
|
// because some types, like unions, can't be completely represented
|
|
|
|
// in the LLVM type system.)
|
2009-02-26 03:24:29 +08:00
|
|
|
if (GV->getType() != Init->getType()) {
|
|
|
|
llvm::GlobalVariable *OldGV = GV;
|
|
|
|
|
|
|
|
GV = new llvm::GlobalVariable(Init->getType(), OldGV->isConstant(),
|
|
|
|
OldGV->getLinkage(), Init, "",
|
2009-04-20 05:05:03 +08:00
|
|
|
&CGM.getModule(), D.isThreadSpecified(),
|
2009-02-26 03:24:29 +08:00
|
|
|
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-04-14 05:08:27 +08:00
|
|
|
if (D.hasAttr<UsedAttr>())
|
2009-02-14 06:08:43 +08:00
|
|
|
CGM.AddUsedGlobal(GV);
|
|
|
|
|
2009-02-26 04:08:33 +08:00
|
|
|
// We may have to cast the constant because of the initializer
|
|
|
|
// mismatch above.
|
|
|
|
//
|
|
|
|
// FIXME: It is really dangerous to store this in the map; if anyone
|
|
|
|
// RAUW's the GV uses of this constant will be invalid.
|
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
|
|
|
}
|
|
|
|
|
2009-03-04 11:23:46 +08:00
|
|
|
/// BuildByRefType - This routine changes a __block variable declared as T x
|
|
|
|
/// into:
|
|
|
|
///
|
|
|
|
/// struct {
|
|
|
|
/// void *__isa;
|
|
|
|
/// void *__forwarding;
|
|
|
|
/// int32_t __flags;
|
|
|
|
/// int32_t __size;
|
|
|
|
/// void *__copy_helper;
|
|
|
|
/// void *__destroy_helper;
|
|
|
|
/// T x;
|
|
|
|
/// } x
|
|
|
|
///
|
|
|
|
/// Align is the alignment needed in bytes for x.
|
|
|
|
const llvm::Type *CodeGenFunction::BuildByRefType(QualType Ty,
|
|
|
|
uint64_t Align) {
|
|
|
|
const llvm::Type *LTy = ConvertType(Ty);
|
|
|
|
bool needsCopyDispose = BlockRequiresCopying(Ty);
|
|
|
|
std::vector<const llvm::Type *> Types(needsCopyDispose*2+5);
|
|
|
|
const llvm::PointerType *PtrToInt8Ty
|
|
|
|
= llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
|
|
|
|
Types[0] = PtrToInt8Ty;
|
|
|
|
Types[1] = PtrToInt8Ty;
|
|
|
|
Types[2] = llvm::Type::Int32Ty;
|
|
|
|
Types[3] = llvm::Type::Int32Ty;
|
|
|
|
if (needsCopyDispose) {
|
|
|
|
Types[4] = PtrToInt8Ty;
|
|
|
|
Types[5] = PtrToInt8Ty;
|
|
|
|
}
|
|
|
|
// FIXME: Align this on at least an Align boundary.
|
|
|
|
Types[needsCopyDispose*2 + 4] = LTy;
|
|
|
|
return llvm::StructType::get(Types, false);
|
|
|
|
}
|
|
|
|
|
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();
|
2009-04-14 05:08:27 +08:00
|
|
|
bool isByRef = D.hasAttr<BlocksAttr>();
|
2009-03-05 09:23:13 +08:00
|
|
|
bool needsDispose = false;
|
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.
|
2009-03-04 12:25:14 +08:00
|
|
|
const llvm::Type *LTy = ConvertTypeForMem(Ty);
|
2009-03-04 11:23:46 +08:00
|
|
|
if (isByRef)
|
|
|
|
LTy = BuildByRefType(Ty, getContext().getDeclAlignInBytes(&D));
|
2009-03-22 08:24:14 +08:00
|
|
|
llvm::AllocaInst *Alloc = CreateTempAlloca(LTy);
|
|
|
|
Alloc->setName(D.getNameAsString().c_str());
|
|
|
|
|
2009-03-04 11:23:46 +08:00
|
|
|
if (isByRef)
|
|
|
|
Alloc->setAlignment(std::max(getContext().getDeclAlignInBytes(&D),
|
2009-03-22 08:32:22 +08:00
|
|
|
unsigned(Target.getPointerAlign(0) / 8)));
|
2009-03-04 11:23:46 +08:00
|
|
|
else
|
|
|
|
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.
|
2009-03-04 12:25:14 +08:00
|
|
|
const llvm::Type *LElemTy = ConvertTypeForMem(Ty);
|
2008-12-12 15:38:43 +08:00
|
|
|
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());
|
2009-03-04 11:23:46 +08:00
|
|
|
if (isByRef) {
|
|
|
|
llvm::Value *Loc;
|
|
|
|
bool needsCopyDispose = BlockRequiresCopying(Ty);
|
|
|
|
// FIXME: I think we need to indirect through the forwarding pointer first
|
|
|
|
Loc = Builder.CreateStructGEP(DeclPtr, needsCopyDispose*2+4, "x");
|
|
|
|
DI->EmitDeclareOfAutoVariable(&D, Loc, Builder);
|
|
|
|
} else
|
|
|
|
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()) {
|
2009-03-04 11:23:46 +08:00
|
|
|
llvm::Value *Loc = DeclPtr;
|
|
|
|
if (isByRef) {
|
|
|
|
bool needsCopyDispose = BlockRequiresCopying(Ty);
|
|
|
|
Loc = Builder.CreateStructGEP(DeclPtr, needsCopyDispose*2+4, "x");
|
|
|
|
}
|
2007-08-26 15:29:23 +08:00
|
|
|
if (!hasAggregateLLVMType(Init->getType())) {
|
|
|
|
llvm::Value *V = EmitScalarExpr(Init);
|
2009-03-04 12:25:14 +08:00
|
|
|
EmitStoreOfScalar(V, Loc, D.getType().isVolatileQualified());
|
2008-04-05 00:54:41 +08:00
|
|
|
} else if (Init->getType()->isAnyComplexType()) {
|
2009-03-04 11:23:46 +08:00
|
|
|
EmitComplexExprIntoAddr(Init, Loc, D.getType().isVolatileQualified());
|
2007-08-26 13:13:54 +08:00
|
|
|
} else {
|
2009-03-04 11:23:46 +08:00
|
|
|
EmitAggExpr(Init, Loc, D.getType().isVolatileQualified());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isByRef) {
|
|
|
|
const llvm::PointerType *PtrToInt8Ty
|
|
|
|
= llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
|
|
|
|
|
|
|
|
llvm::Value *isa_field = Builder.CreateStructGEP(DeclPtr, 0);
|
|
|
|
llvm::Value *forwarding_field = Builder.CreateStructGEP(DeclPtr, 1);
|
|
|
|
llvm::Value *flags_field = Builder.CreateStructGEP(DeclPtr, 2);
|
|
|
|
llvm::Value *size_field = Builder.CreateStructGEP(DeclPtr, 3);
|
|
|
|
llvm::Value *V;
|
|
|
|
int flag = 0;
|
|
|
|
int flags = 0;
|
|
|
|
|
2009-03-07 14:04:31 +08:00
|
|
|
needsDispose = true;
|
2009-03-05 16:32:30 +08:00
|
|
|
|
2009-03-04 11:23:46 +08:00
|
|
|
if (Ty->isBlockPointerType()) {
|
|
|
|
flag |= BLOCK_FIELD_IS_BLOCK;
|
|
|
|
flags |= BLOCK_HAS_COPY_DISPOSE;
|
|
|
|
} else if (BlockRequiresCopying(Ty)) {
|
|
|
|
flag |= BLOCK_FIELD_IS_OBJECT;
|
2009-03-06 12:53:30 +08:00
|
|
|
flags |= BLOCK_HAS_COPY_DISPOSE;
|
2009-03-04 11:23:46 +08:00
|
|
|
}
|
2009-03-07 14:04:31 +08:00
|
|
|
|
|
|
|
// FIXME: Someone double check this.
|
|
|
|
if (Ty.isObjCGCWeak())
|
|
|
|
flag |= BLOCK_FIELD_IS_WEAK;
|
2009-03-04 11:23:46 +08:00
|
|
|
|
|
|
|
int isa = 0;
|
|
|
|
if (flag&BLOCK_FIELD_IS_WEAK)
|
|
|
|
isa = 1;
|
|
|
|
V = llvm::ConstantInt::get(llvm::Type::Int32Ty, isa);
|
2009-03-06 09:33:24 +08:00
|
|
|
V = Builder.CreateIntToPtr(V, PtrToInt8Ty, "isa");
|
2009-03-04 11:23:46 +08:00
|
|
|
Builder.CreateStore(V, isa_field);
|
|
|
|
|
2009-03-06 09:33:24 +08:00
|
|
|
V = Builder.CreateBitCast(DeclPtr, PtrToInt8Ty, "forwarding");
|
2009-03-04 11:23:46 +08:00
|
|
|
Builder.CreateStore(V, forwarding_field);
|
|
|
|
|
|
|
|
V = llvm::ConstantInt::get(llvm::Type::Int32Ty, flags);
|
|
|
|
Builder.CreateStore(V, flags_field);
|
|
|
|
|
2009-03-05 16:32:30 +08:00
|
|
|
const llvm::Type *V1;
|
|
|
|
V1 = cast<llvm::PointerType>(DeclPtr->getType())->getElementType();
|
|
|
|
V = llvm::ConstantInt::get(llvm::Type::Int32Ty,
|
|
|
|
(CGM.getTargetData().getTypeStoreSizeInBits(V1)
|
|
|
|
/ 8));
|
2009-03-04 11:23:46 +08:00
|
|
|
Builder.CreateStore(V, size_field);
|
|
|
|
|
|
|
|
if (flags & BLOCK_HAS_COPY_DISPOSE) {
|
2009-03-05 16:32:30 +08:00
|
|
|
BlockHasCopyDispose = true;
|
2009-03-04 11:23:46 +08:00
|
|
|
llvm::Value *copy_helper = Builder.CreateStructGEP(DeclPtr, 4);
|
2009-03-06 14:12:24 +08:00
|
|
|
Builder.CreateStore(BuildbyrefCopyHelper(DeclPtr->getType(), flag),
|
|
|
|
copy_helper);
|
2009-03-04 11:23:46 +08:00
|
|
|
|
2009-03-06 12:53:30 +08:00
|
|
|
llvm::Value *destroy_helper = Builder.CreateStructGEP(DeclPtr, 5);
|
|
|
|
Builder.CreateStore(BuildbyrefDestroyHelper(DeclPtr->getType(), flag),
|
|
|
|
destroy_helper);
|
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);
|
|
|
|
}
|
2009-03-05 09:23:13 +08:00
|
|
|
|
2009-03-05 10:34:38 +08:00
|
|
|
if (needsDispose && CGM.getLangOptions().getGCMode() != LangOptions::GCOnly) {
|
2009-03-05 09:23:13 +08:00
|
|
|
CleanupScope scope(*this);
|
2009-03-06 12:53:30 +08:00
|
|
|
llvm::Value *V = Builder.CreateStructGEP(DeclPtr, 1, "forwarding");
|
|
|
|
V = Builder.CreateLoad(V, false);
|
|
|
|
BuildBlockRelease(V);
|
2009-03-05 09:23:13 +08:00
|
|
|
}
|
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;
|
|
|
|
} else {
|
2008-05-23 06:12:56 +08:00
|
|
|
// A fixed sized single-value variable becomes an alloca in the entry block.
|
2009-03-04 12:25:14 +08:00
|
|
|
const llvm::Type *LTy = ConvertTypeForMem(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";
|
2009-03-22 08:24:14 +08:00
|
|
|
DeclPtr = CreateTempAlloca(LTy);
|
|
|
|
DeclPtr->setName(Name.c_str());
|
2007-06-14 04:44:40 +08:00
|
|
|
|
|
|
|
// Store the initial value into the alloca.
|
2009-03-04 12:25:14 +08:00
|
|
|
EmitStoreOfScalar(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
|
|
|
}
|
|
|
|
|