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