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