CGDecl.cpp revision 31fc07df7f0fc89ebf83ca05a20b29de45a7598d
1//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This contains code to emit Decl nodes as LLVM code. 11// 12//===----------------------------------------------------------------------===// 13 14#include "CGDebugInfo.h" 15#include "CodeGenFunction.h" 16#include "CodeGenModule.h" 17#include "clang/AST/ASTContext.h" 18#include "clang/AST/Decl.h" 19#include "clang/AST/DeclObjC.h" 20#include "clang/Basic/SourceManager.h" 21#include "clang/Basic/TargetInfo.h" 22#include "llvm/GlobalVariable.h" 23#include "llvm/Type.h" 24#include "llvm/Support/Dwarf.h" 25using namespace clang; 26using namespace CodeGen; 27 28 29void CodeGenFunction::EmitDecl(const Decl &D) { 30 switch (D.getKind()) { 31 default: assert(0 && "Unknown decl kind!"); 32 case Decl::ParmVar: 33 assert(0 && "Parmdecls should not be in declstmts!"); 34 case Decl::Typedef: // typedef int X; 35 case Decl::Function: // void X(); 36 case Decl::Record: // struct/union/class X; 37 case Decl::Enum: // enum X; 38 case Decl::EnumConstant: // enum ? { X = ? } 39 case Decl::CXXRecord: // struct/union/class X; [C++] 40 // None of these decls require codegen support. 41 return; 42 43 case Decl::Var: { 44 const VarDecl &VD = cast<VarDecl>(D); 45 assert(VD.isBlockVarDecl() && 46 "Should not see file-scope variables inside a function!"); 47 return EmitBlockVarDecl(VD); 48 } 49 } 50} 51 52/// EmitBlockVarDecl - This method handles emission of any variable declaration 53/// inside a function, including static vars etc. 54void CodeGenFunction::EmitBlockVarDecl(const VarDecl &D) { 55 switch (D.getStorageClass()) { 56 case VarDecl::Static: 57 return EmitStaticBlockVarDecl(D); 58 case VarDecl::Extern: 59 // Don't emit it now, allow it to be emitted lazily on its first use. 60 return; 61 default: 62 assert((D.getStorageClass() == VarDecl::None || 63 D.getStorageClass() == VarDecl::Auto || 64 D.getStorageClass() == VarDecl::Register) && 65 "Unknown storage class"); 66 return EmitLocalBlockVarDecl(D); 67 } 68} 69 70llvm::GlobalValue * 71CodeGenFunction::GenerateStaticBlockVarDecl(const VarDecl &D, 72 bool NoInit, 73 const char *Separator) { 74 QualType Ty = D.getType(); 75 assert(Ty->isConstantSizeType() && "VLAs can't be static"); 76 77 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty); 78 llvm::Constant *Init = 0; 79 if ((D.getInit() == 0) || NoInit) { 80 Init = llvm::Constant::getNullValue(LTy); 81 } else { 82 if (D.getInit()->isConstantExpr(getContext(), 0)) 83 Init = CGM.EmitConstantExpr(D.getInit(), this); 84 else { 85 assert(getContext().getLangOptions().CPlusPlus && 86 "only C++ supports non-constant static initializers!"); 87 return GenerateStaticCXXBlockVarDecl(D); 88 } 89 } 90 91 assert(Init && "Unable to create initialiser for static decl"); 92 93 std::string ContextName; 94 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurFuncDecl)) 95 ContextName = FD->getName(); 96 else if (isa<ObjCMethodDecl>(CurFuncDecl)) 97 ContextName = std::string(CurFn->getNameStart(), 98 CurFn->getNameStart() + CurFn->getNameLen()); 99 else 100 assert(0 && "Unknown context for block var decl"); 101 102 llvm::GlobalValue *GV = 103 new llvm::GlobalVariable(Init->getType(), false, 104 llvm::GlobalValue::InternalLinkage, 105 Init, ContextName + Separator + D.getName(), 106 &CGM.getModule(), 0, Ty.getAddressSpace()); 107 108 return GV; 109} 110 111void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D) { 112 113 llvm::Value *&DMEntry = LocalDeclMap[&D]; 114 assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); 115 116 llvm::GlobalValue *GV = GenerateStaticBlockVarDecl(D, false, "."); 117 118 if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) { 119 SourceManager &SM = CGM.getContext().getSourceManager(); 120 llvm::Constant *Ann = 121 CGM.EmitAnnotateAttr(GV, AA, SM.getLogicalLineNumber(D.getLocation())); 122 CGM.AddAnnotation(Ann); 123 } 124 125 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType()); 126 const llvm::Type *LPtrTy = 127 llvm::PointerType::get(LTy, D.getType().getAddressSpace()); 128 DMEntry = llvm::ConstantExpr::getBitCast(GV, LPtrTy); 129 130 // Emit global variable debug descriptor for static vars. 131 CGDebugInfo *DI = CGM.getDebugInfo(); 132 if(DI) { 133 DI->setLocation(D.getLocation()); 134 DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D); 135 } 136 137} 138 139/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a 140/// variable declaration with auto, register, or no storage class specifier. 141/// These turn into simple stack objects, or GlobalValues depending on target. 142void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) { 143 QualType Ty = D.getType(); 144 145 llvm::Value *DeclPtr; 146 if (Ty->isConstantSizeType()) { 147 if (!Target.useGlobalsForAutomaticVariables()) { 148 // A normal fixed sized variable becomes an alloca in the entry block. 149 const llvm::Type *LTy = ConvertType(Ty); 150 llvm::AllocaInst * Alloc = CreateTempAlloca(LTy, D.getName()); 151 unsigned align = getContext().getTypeAlign(Ty); 152 if (const AlignedAttr* AA = D.getAttr<AlignedAttr>()) 153 align = std::max(align, AA->getAlignment()); 154 Alloc->setAlignment(align >> 3); 155 DeclPtr = Alloc; 156 } else { 157 // Targets that don't support recursion emit locals as globals. 158 const char *Class = 159 D.getStorageClass() == VarDecl::Register ? ".reg." : ".auto."; 160 DeclPtr = GenerateStaticBlockVarDecl(D, true, Class); 161 } 162 } else { 163 CGM.ErrorUnsupported(&D, "variable-length array"); 164 165 // FIXME: VLA: Add VLA support. For now just make up enough to let 166 // the compile go through. 167 const llvm::Type *LTy = ConvertType(Ty); 168 llvm::AllocaInst * Alloc = CreateTempAlloca(LTy, D.getName()); 169 DeclPtr = Alloc; 170 } 171 172 llvm::Value *&DMEntry = LocalDeclMap[&D]; 173 assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); 174 DMEntry = DeclPtr; 175 176 // Emit debug info for local var declaration. 177 CGDebugInfo *DI = CGM.getDebugInfo(); 178 if(DI) { 179 DI->setLocation(D.getLocation()); 180 DI->EmitDeclare(&D, llvm::dwarf::DW_TAG_auto_variable, 181 DeclPtr, Builder); 182 } 183 184 // If this local has an initializer, emit it now. 185 if (const Expr *Init = D.getInit()) { 186 if (!hasAggregateLLVMType(Init->getType())) { 187 llvm::Value *V = EmitScalarExpr(Init); 188 Builder.CreateStore(V, DeclPtr, D.getType().isVolatileQualified()); 189 } else if (Init->getType()->isAnyComplexType()) { 190 EmitComplexExprIntoAddr(Init, DeclPtr, D.getType().isVolatileQualified()); 191 } else { 192 EmitAggExpr(Init, DeclPtr, D.getType().isVolatileQualified()); 193 } 194 } 195} 196 197/// Emit an alloca (or GlobalValue depending on target) 198/// for the specified parameter and set up LocalDeclMap. 199void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg) { 200 // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl? 201 assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) && 202 "Invalid argument to EmitParmDecl"); 203 QualType Ty = D.getType(); 204 205 llvm::Value *DeclPtr; 206 if (!Ty->isConstantSizeType()) { 207 // Variable sized values always are passed by-reference. 208 DeclPtr = Arg; 209 } else if (Target.useGlobalsForAutomaticVariables()) { 210 DeclPtr = GenerateStaticBlockVarDecl(D, true, ".arg."); 211 } else { 212 // A fixed sized single-value variable becomes an alloca in the entry block. 213 const llvm::Type *LTy = ConvertType(Ty); 214 if (LTy->isSingleValueType()) { 215 // TODO: Alignment 216 std::string Name(D.getName()); 217 Name += ".addr"; 218 DeclPtr = CreateTempAlloca(LTy, Name.c_str()); 219 220 // Store the initial value into the alloca. 221 Builder.CreateStore(Arg, DeclPtr,Ty.isVolatileQualified()); 222 } else { 223 // Otherwise, if this is an aggregate, just use the input pointer. 224 DeclPtr = Arg; 225 } 226 Arg->setName(D.getName()); 227 } 228 229 llvm::Value *&DMEntry = LocalDeclMap[&D]; 230 assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); 231 DMEntry = DeclPtr; 232 233 // Emit debug info for param declaration. 234 CGDebugInfo *DI = CGM.getDebugInfo(); 235 if(DI) { 236 DI->setLocation(D.getLocation()); 237 DI->EmitDeclare(&D, llvm::dwarf::DW_TAG_arg_variable, 238 DeclPtr, Builder); 239 } 240 241} 242 243