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