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" |
Anders Carlsson | 19567ee | 2008-08-25 01:38:19 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 20 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 21 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 22 | #include "llvm/GlobalVariable.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | #include "llvm/Type.h" |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Dwarf.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | using namespace CodeGen; |
| 27 | |
| 28 | |
| 29 | void CodeGenFunction::EmitDecl(const Decl &D) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | switch (D.getKind()) { |
| 31 | default: assert(0 && "Unknown decl kind!"); |
Chris Lattner | aa9fc46 | 2007-10-08 21:37:32 +0000 | [diff] [blame] | 32 | case Decl::ParmVar: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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; |
Daniel Dunbar | 662174c8 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 40 | case Decl::EnumConstant: // enum ? { X = ? } |
Argyrios Kyrtzidis | a7a6dc0 | 2008-06-09 23:42:47 +0000 | [diff] [blame] | 41 | case Decl::CXXStruct: // struct X; [C++] |
| 42 | case Decl::CXXUnion: // union X; [C++] |
| 43 | case Decl::CXXClass: // class X; [C++] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 44 | // None of these decls require codegen support. |
| 45 | return; |
| 46 | |
Daniel Dunbar | 662174c8 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 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); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 52 | } |
Daniel Dunbar | 662174c8 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 53 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /// EmitBlockVarDecl - This method handles emission of any variable declaration |
| 57 | /// inside a function, including static vars etc. |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 58 | void CodeGenFunction::EmitBlockVarDecl(const VarDecl &D) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 59 | switch (D.getStorageClass()) { |
| 60 | case VarDecl::Static: |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 61 | return EmitStaticBlockVarDecl(D); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 62 | case VarDecl::Extern: |
Lauro Ramos Venancio | fea90b8 | 2008-02-16 22:30:38 +0000 | [diff] [blame] | 63 | // Don't emit it now, allow it to be emitted lazily on its first use. |
| 64 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 74 | llvm::GlobalValue * |
| 75 | CodeGenFunction::GenerateStaticBlockVarDecl(const VarDecl &D, |
| 76 | bool NoInit, |
| 77 | const char *Separator) { |
Chris Lattner | 8bcfc5b | 2008-04-06 23:10:54 +0000 | [diff] [blame] | 78 | QualType Ty = D.getType(); |
Eli Friedman | 3c2b317 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 79 | assert(Ty->isConstantSizeType() && "VLAs can't be static"); |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 80 | |
Chris Lattner | 19009e6 | 2008-01-09 18:47:25 +0000 | [diff] [blame] | 81 | const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty); |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 82 | llvm::Constant *Init = 0; |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 83 | if ((D.getInit() == 0) || NoInit) { |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 84 | Init = llvm::Constant::getNullValue(LTy); |
Oliver Hunt | 2824723 | 2007-12-02 00:11:25 +0000 | [diff] [blame] | 85 | } else { |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 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 | } |
Devang Patel | e40daa4 | 2007-10-26 17:50:58 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Oliver Hunt | 2824723 | 2007-12-02 00:11:25 +0000 | [diff] [blame] | 95 | assert(Init && "Unable to create initialiser for static decl"); |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 96 | |
Chris Lattner | 352ffde2 | 2008-02-06 04:54:32 +0000 | [diff] [blame] | 97 | std::string ContextName; |
Anders Carlsson | 19567ee | 2008-08-25 01:38:19 +0000 | [diff] [blame] | 98 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurFuncDecl)) |
Chris Lattner | c8aa5f1 | 2008-04-04 04:07:35 +0000 | [diff] [blame] | 99 | ContextName = FD->getName(); |
Anders Carlsson | 19567ee | 2008-08-25 01:38:19 +0000 | [diff] [blame] | 100 | else if (isa<ObjCMethodDecl>(CurFuncDecl)) |
| 101 | ContextName = std::string(CurFn->getNameStart(), |
| 102 | CurFn->getNameStart() + CurFn->getNameLen()); |
Chris Lattner | 352ffde2 | 2008-02-06 04:54:32 +0000 | [diff] [blame] | 103 | else |
Anders Carlsson | 19567ee | 2008-08-25 01:38:19 +0000 | [diff] [blame] | 104 | assert(0 && "Unknown context for block var decl"); |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 105 | |
Eli Friedman | 2659052 | 2008-06-08 01:23:18 +0000 | [diff] [blame] | 106 | llvm::GlobalValue *GV = |
| 107 | new llvm::GlobalVariable(Init->getType(), false, |
| 108 | llvm::GlobalValue::InternalLinkage, |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 109 | Init, ContextName + Separator + D.getName(), |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 110 | &CGM.getModule(), 0, Ty.getAddressSpace()); |
| 111 | |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 112 | return GV; |
| 113 | } |
| 114 | |
| 115 | void 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 | |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 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 | |
Eli Friedman | 2659052 | 2008-06-08 01:23:18 +0000 | [diff] [blame] | 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); |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 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 | |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 144 | /// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a |
| 145 | /// variable declaration with auto, register, or no storage class specifier. |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 146 | /// These turn into simple stack objects, or GlobalValues depending on target. |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 147 | void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) { |
Chris Lattner | 8bcfc5b | 2008-04-06 23:10:54 +0000 | [diff] [blame] | 148 | QualType Ty = D.getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 149 | |
| 150 | llvm::Value *DeclPtr; |
Eli Friedman | 3c2b317 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 151 | if (Ty->isConstantSizeType()) { |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 152 | if (!Target.useGlobalsForAutomaticVariables()) { |
| 153 | // A normal fixed sized variable becomes an alloca in the entry block. |
| 154 | const llvm::Type *LTy = ConvertType(Ty); |
Eli Friedman | 77eedd6 | 2008-05-31 21:20:41 +0000 | [diff] [blame] | 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; |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 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 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 167 | } else { |
Daniel Dunbar | 662174c8 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 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; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | llvm::Value *&DMEntry = LocalDeclMap[&D]; |
| 178 | assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); |
| 179 | DMEntry = DeclPtr; |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 1978596 | 2007-07-12 00:39:48 +0000 | [diff] [blame] | 190 | // If this local has an initializer, emit it now. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 191 | if (const Expr *Init = D.getInit()) { |
Chris Lattner | d31beb1 | 2007-08-26 07:29:23 +0000 | [diff] [blame] | 192 | if (!hasAggregateLLVMType(Init->getType())) { |
| 193 | llvm::Value *V = EmitScalarExpr(Init); |
Chris Lattner | 8b2f3b7 | 2007-08-26 07:30:49 +0000 | [diff] [blame] | 194 | Builder.CreateStore(V, DeclPtr, D.getType().isVolatileQualified()); |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 195 | } else if (Init->getType()->isAnyComplexType()) { |
Chris Lattner | 190dbe2 | 2007-08-26 16:22:13 +0000 | [diff] [blame] | 196 | EmitComplexExprIntoAddr(Init, DeclPtr, D.getType().isVolatileQualified()); |
Chris Lattner | 9a19edf | 2007-08-26 05:13:54 +0000 | [diff] [blame] | 197 | } else { |
Chris Lattner | 8b2f3b7 | 2007-08-26 07:30:49 +0000 | [diff] [blame] | 198 | EmitAggExpr(Init, DeclPtr, D.getType().isVolatileQualified()); |
Chris Lattner | 9a19edf | 2007-08-26 05:13:54 +0000 | [diff] [blame] | 199 | } |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 200 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 203 | /// Emit an alloca (or GlobalValue depending on target) |
| 204 | /// for the specified parameter and set up LocalDeclMap. |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 205 | void 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"); |
Chris Lattner | 8bcfc5b | 2008-04-06 23:10:54 +0000 | [diff] [blame] | 209 | QualType Ty = D.getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 210 | |
| 211 | llvm::Value *DeclPtr; |
Eli Friedman | 3c2b317 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 212 | if (!Ty->isConstantSizeType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 213 | // Variable sized values always are passed by-reference. |
| 214 | DeclPtr = Arg; |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 215 | } else if (Target.useGlobalsForAutomaticVariables()) { |
| 216 | DeclPtr = GenerateStaticBlockVarDecl(D, true, ".arg."); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 217 | } else { |
Dan Gohman | d79a726 | 2008-05-22 22:12:56 +0000 | [diff] [blame] | 218 | // 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] | 219 | const llvm::Type *LTy = ConvertType(Ty); |
Dan Gohman | d79a726 | 2008-05-22 22:12:56 +0000 | [diff] [blame] | 220 | if (LTy->isSingleValueType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 221 | // TODO: Alignment |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 222 | std::string Name(D.getName()); |
| 223 | Name += ".addr"; |
| 224 | DeclPtr = CreateTempAlloca(LTy, Name.c_str()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 225 | |
| 226 | // Store the initial value into the alloca. |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 227 | Builder.CreateStore(Arg, DeclPtr,Ty.isVolatileQualified()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 228 | } else { |
| 229 | // Otherwise, if this is an aggregate, just use the input pointer. |
| 230 | DeclPtr = Arg; |
| 231 | } |
| 232 | Arg->setName(D.getName()); |
| 233 | } |
| 234 | |
| 235 | llvm::Value *&DMEntry = LocalDeclMap[&D]; |
| 236 | assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); |
| 237 | DMEntry = DeclPtr; |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 238 | |
| 239 | // Emit debug info for param declaration. |
| 240 | CGDebugInfo *DI = CGM.getDebugInfo(); |
| 241 | if(DI) { |
| 242 | if(D.getLocation().isValid()) |
| 243 | DI->setLocation(D.getLocation()); |
| 244 | DI->EmitDeclare(&D, llvm::dwarf::DW_TAG_arg_variable, |
| 245 | DeclPtr, Builder); |
| 246 | } |
| 247 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 248 | } |
| 249 | |