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