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