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"
|
2007-06-02 12:16:21 +08:00
|
|
|
#include "clang/AST/AST.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"
|
2007-06-14 04:44:40 +08:00
|
|
|
#include "llvm/Type.h"
|
2008-05-30 18:30:31 +08:00
|
|
|
#include "llvm/Support/Dwarf.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::Typedef: // typedef int X;
|
|
|
|
case Decl::Function: // void X();
|
|
|
|
case Decl::Struct: // struct X;
|
|
|
|
case Decl::Union: // union X;
|
|
|
|
case Decl::Class: // class X;
|
|
|
|
case Decl::Enum: // enum X;
|
2008-06-10 07:42:47 +08:00
|
|
|
case Decl::CXXStruct: // struct X; [C++]
|
|
|
|
case Decl::CXXUnion: // union X; [C++]
|
|
|
|
case Decl::CXXClass: // class X; [C++]
|
2007-06-02 12:16:21 +08:00
|
|
|
// None of these decls require codegen support.
|
|
|
|
return;
|
|
|
|
|
2008-04-16 06:42:06 +08:00
|
|
|
case Decl::Var:
|
|
|
|
if (cast<VarDecl>(D).isBlockVarDecl())
|
|
|
|
return EmitBlockVarDecl(cast<VarDecl>(D));
|
|
|
|
assert(0 && "Should not see file-scope variables inside a function!");
|
|
|
|
|
2007-06-02 12:16:21 +08:00
|
|
|
case Decl::EnumConstant:
|
2007-06-09 09:20:56 +08:00
|
|
|
return EmitEnumConstantDecl(cast<EnumConstantDecl>(D));
|
2007-06-02 12:16:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-02 12:53:11 +08:00
|
|
|
void CodeGenFunction::EmitEnumConstantDecl(const EnumConstantDecl &D) {
|
|
|
|
assert(0 && "FIXME: Enum constant decls not implemented yet!");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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
|
|
|
}
|
|
|
|
|
2008-05-08 13:58:21 +08:00
|
|
|
llvm::GlobalValue *
|
|
|
|
CodeGenFunction::GenerateStaticBlockVarDecl(const VarDecl &D,
|
|
|
|
bool NoInit,
|
|
|
|
const char *Separator) {
|
2008-04-07 07:10:54 +08:00
|
|
|
QualType Ty = D.getType();
|
2008-02-15 20:20:59 +08:00
|
|
|
assert(Ty->isConstantSizeType() && "VLAs can't be static");
|
2007-10-17 08:52:43 +08:00
|
|
|
|
2008-01-10 02:47:25 +08:00
|
|
|
const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
|
2007-10-17 08:52:43 +08:00
|
|
|
llvm::Constant *Init = 0;
|
2008-05-08 13:58:21 +08:00
|
|
|
if ((D.getInit() == 0) || NoInit) {
|
2007-10-17 08:52:43 +08:00
|
|
|
Init = llvm::Constant::getNullValue(LTy);
|
2007-12-02 08:11:25 +08:00
|
|
|
} else {
|
2008-02-27 05:41:45 +08:00
|
|
|
Init = CGM.EmitConstantExpr(D.getInit(), this);
|
2007-10-27 01:50:58 +08:00
|
|
|
}
|
|
|
|
|
2007-12-02 08:11:25 +08:00
|
|
|
assert(Init && "Unable to create initialiser for static decl");
|
2008-04-19 12:17:09 +08:00
|
|
|
|
2008-02-06 12:54:32 +08:00
|
|
|
std::string ContextName;
|
2008-04-04 12:07:35 +08:00
|
|
|
if (const FunctionDecl * FD = dyn_cast<FunctionDecl>(CurFuncDecl))
|
|
|
|
ContextName = FD->getName();
|
2008-02-06 12:54:32 +08:00
|
|
|
else
|
|
|
|
assert(0 && "Unknown context for block var decl"); // FIXME Handle objc.
|
2008-04-19 12:17:09 +08:00
|
|
|
|
2008-06-08 09:23:18 +08:00
|
|
|
llvm::GlobalValue *GV =
|
|
|
|
new llvm::GlobalVariable(Init->getType(), false,
|
|
|
|
llvm::GlobalValue::InternalLinkage,
|
2008-05-08 13:58:21 +08:00
|
|
|
Init, ContextName + Separator + D.getName(),
|
2008-04-19 12:17:09 +08:00
|
|
|
&CGM.getModule(), 0, Ty.getAddressSpace());
|
|
|
|
|
2008-05-08 13:58:21 +08:00
|
|
|
return GV;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D) {
|
|
|
|
|
|
|
|
llvm::Value *&DMEntry = LocalDeclMap[&D];
|
|
|
|
assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
|
|
|
|
|
|
|
|
llvm::GlobalValue *GV = GenerateStaticBlockVarDecl(D, false, ".");
|
|
|
|
|
2008-04-19 12:17:09 +08:00
|
|
|
if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) {
|
|
|
|
SourceManager &SM = CGM.getContext().getSourceManager();
|
|
|
|
llvm::Constant *Ann =
|
|
|
|
CGM.EmitAnnotateAttr(GV, AA, SM.getLogicalLineNumber(D.getLocation()));
|
|
|
|
CGM.AddAnnotation(Ann);
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
CGDebugInfo *DI = CGM.getDebugInfo();
|
|
|
|
if(DI) {
|
|
|
|
if(D.getLocation().isValid())
|
|
|
|
DI->setLocation(D.getLocation());
|
|
|
|
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-06-01 05:20:41 +08:00
|
|
|
llvm::AllocaInst * Alloc = CreateTempAlloca(LTy, D.getName());
|
|
|
|
unsigned align = getContext().getTypeAlign(Ty);
|
|
|
|
if (const AlignedAttr* AA = D.getAttr<AlignedAttr>())
|
|
|
|
align = std::max(align, AA->getAlignment());
|
|
|
|
Alloc->setAlignment(align >> 3);
|
|
|
|
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.";
|
|
|
|
DeclPtr = GenerateStaticBlockVarDecl(D, true, Class);
|
|
|
|
}
|
2007-06-02 12:53:11 +08:00
|
|
|
} else {
|
|
|
|
// TODO: Create a dynamic alloca.
|
|
|
|
assert(0 && "FIXME: Local VLAs not implemented yet");
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
CGDebugInfo *DI = CGM.getDebugInfo();
|
|
|
|
if(DI) {
|
|
|
|
if(D.getLocation().isValid())
|
|
|
|
DI->setLocation(D.getLocation());
|
|
|
|
DI->EmitDeclare(&D, llvm::dwarf::DW_TAG_auto_variable,
|
|
|
|
DeclPtr, Builder);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
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.
|
2007-06-14 04:44:40 +08:00
|
|
|
void CodeGenFunction::EmitParmDecl(const ParmVarDecl &D, llvm::Value *Arg) {
|
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()) {
|
|
|
|
DeclPtr = GenerateStaticBlockVarDecl(D, true, ".arg.");
|
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
|
2007-06-16 07:05:46 +08:00
|
|
|
DeclPtr = new llvm::AllocaInst(LTy, 0, std::string(D.getName())+".addr",
|
|
|
|
AllocaInsertPt);
|
2007-06-14 04:44:40 +08:00
|
|
|
|
|
|
|
// Store the initial value into the alloca.
|
|
|
|
Builder.CreateStore(Arg, DeclPtr);
|
|
|
|
} 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
|
|
|
}
|
2007-06-23 02:15:16 +08:00
|
|
|
Arg->setName(D.getName());
|
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.
|
|
|
|
CGDebugInfo *DI = CGM.getDebugInfo();
|
|
|
|
if(DI) {
|
|
|
|
if(D.getLocation().isValid())
|
|
|
|
DI->setLocation(D.getLocation());
|
|
|
|
DI->EmitDeclare(&D, llvm::dwarf::DW_TAG_arg_variable,
|
|
|
|
DeclPtr, Builder);
|
|
|
|
}
|
|
|
|
|
2007-06-14 04:44:40 +08:00
|
|
|
}
|
|
|
|
|