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