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" |
Anders Carlsson | 5d46315 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 23 | #include "llvm/Intrinsics.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | #include "llvm/Type.h" |
| 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!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 34 | case Decl::Function: // void X(); |
Argyrios Kyrtzidis | 35bc082 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 35 | case Decl::Record: // struct/union/class X; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 36 | case Decl::Enum: // enum X; |
Daniel Dunbar | 662174c8 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 37 | case Decl::EnumConstant: // enum ? { X = ? } |
Argyrios Kyrtzidis | 35bc082 | 2008-10-15 00:42:39 +0000 | [diff] [blame] | 38 | case Decl::CXXRecord: // struct/union/class X; [C++] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 39 | // None of these decls require codegen support. |
| 40 | return; |
| 41 | |
Daniel Dunbar | 662174c8 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 42 | case Decl::Var: { |
| 43 | const VarDecl &VD = cast<VarDecl>(D); |
| 44 | assert(VD.isBlockVarDecl() && |
| 45 | "Should not see file-scope variables inside a function!"); |
| 46 | return EmitBlockVarDecl(VD); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 47 | } |
Anders Carlsson | fcdbb93 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 48 | |
| 49 | case Decl::Typedef: { // typedef int X; |
| 50 | const TypedefDecl &TD = cast<TypedefDecl>(D); |
| 51 | QualType Ty = TD.getUnderlyingType(); |
| 52 | |
| 53 | if (Ty->isVariablyModifiedType()) |
| 54 | EmitVLASize(Ty); |
| 55 | } |
Daniel Dunbar | 662174c8 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 56 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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, |
Sanjiv Gupta | 45206ec | 2009-02-03 18:07:49 +0000 | [diff] [blame] | 80 | const char *Separator, |
| 81 | llvm::GlobalValue |
| 82 | ::LinkageTypes Linkage) { |
Chris Lattner | 8bcfc5b | 2008-04-06 23:10:54 +0000 | [diff] [blame] | 83 | QualType Ty = D.getType(); |
Eli Friedman | 3c2b317 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 84 | assert(Ty->isConstantSizeType() && "VLAs can't be static"); |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 19009e6 | 2008-01-09 18:47:25 +0000 | [diff] [blame] | 86 | const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty); |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 87 | llvm::Constant *Init = 0; |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 88 | if ((D.getInit() == 0) || NoInit) { |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 89 | Init = llvm::Constant::getNullValue(LTy); |
Oliver Hunt | 2824723 | 2007-12-02 00:11:25 +0000 | [diff] [blame] | 90 | } else { |
Daniel Dunbar | d60f2fb | 2009-02-17 18:43:32 +0000 | [diff] [blame] | 91 | Init = CGM.EmitConstantExpr(D.getInit(), this); |
| 92 | |
| 93 | // If constant emission failed, then this should be a C++ static |
| 94 | // initializer. |
| 95 | if (!Init) { |
| 96 | if (!getContext().getLangOptions().CPlusPlus) { |
| 97 | CGM.ErrorUnsupported(D.getInit(), "constant l-value expression"); |
| 98 | Init = llvm::Constant::getNullValue(LTy); |
| 99 | } else { |
| 100 | return GenerateStaticCXXBlockVarDecl(D); |
| 101 | } |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 102 | } |
Devang Patel | e40daa4 | 2007-10-26 17:50:58 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Oliver Hunt | 2824723 | 2007-12-02 00:11:25 +0000 | [diff] [blame] | 105 | assert(Init && "Unable to create initialiser for static decl"); |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 352ffde2 | 2008-02-06 04:54:32 +0000 | [diff] [blame] | 107 | std::string ContextName; |
Anders Carlsson | 19567ee | 2008-08-25 01:38:19 +0000 | [diff] [blame] | 108 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurFuncDecl)) |
Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 109 | ContextName = CGM.getMangledName(FD); |
Anders Carlsson | 19567ee | 2008-08-25 01:38:19 +0000 | [diff] [blame] | 110 | else if (isa<ObjCMethodDecl>(CurFuncDecl)) |
| 111 | ContextName = std::string(CurFn->getNameStart(), |
| 112 | CurFn->getNameStart() + CurFn->getNameLen()); |
Chris Lattner | 352ffde2 | 2008-02-06 04:54:32 +0000 | [diff] [blame] | 113 | else |
Anders Carlsson | 19567ee | 2008-08-25 01:38:19 +0000 | [diff] [blame] | 114 | assert(0 && "Unknown context for block var decl"); |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 115 | |
Eli Friedman | 2659052 | 2008-06-08 01:23:18 +0000 | [diff] [blame] | 116 | llvm::GlobalValue *GV = |
Daniel Dunbar | e4c9238 | 2009-02-13 22:58:39 +0000 | [diff] [blame] | 117 | new llvm::GlobalVariable(Init->getType(), Ty.isConstant(getContext()), |
Sanjiv Gupta | 45206ec | 2009-02-03 18:07:49 +0000 | [diff] [blame] | 118 | Linkage, |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 119 | Init, ContextName + Separator +D.getNameAsString(), |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 120 | &CGM.getModule(), 0, Ty.getAddressSpace()); |
| 121 | |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 122 | return GV; |
| 123 | } |
| 124 | |
| 125 | void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D) { |
| 126 | |
| 127 | llvm::Value *&DMEntry = LocalDeclMap[&D]; |
| 128 | assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); |
| 129 | |
Sanjiv Gupta | 45206ec | 2009-02-03 18:07:49 +0000 | [diff] [blame] | 130 | llvm::GlobalValue *GV; |
| 131 | GV = GenerateStaticBlockVarDecl(D, false, ".", |
| 132 | llvm::GlobalValue::InternalLinkage); |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 133 | |
Daniel Dunbar | 30510ab | 2009-02-12 23:32:54 +0000 | [diff] [blame] | 134 | // FIXME: Merge attribute handling. |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 135 | if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) { |
| 136 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 137 | llvm::Constant *Ann = |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 138 | CGM.EmitAnnotateAttr(GV, AA, |
| 139 | SM.getInstantiationLineNumber(D.getLocation())); |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 140 | CGM.AddAnnotation(Ann); |
| 141 | } |
| 142 | |
Daniel Dunbar | 30510ab | 2009-02-12 23:32:54 +0000 | [diff] [blame] | 143 | if (const SectionAttr *SA = D.getAttr<SectionAttr>()) |
| 144 | GV->setSection(SA->getName()); |
| 145 | |
Daniel Dunbar | 5c61d97 | 2009-02-13 22:08:43 +0000 | [diff] [blame] | 146 | if (D.getAttr<UsedAttr>()) |
| 147 | CGM.AddUsedGlobal(GV); |
| 148 | |
Eli Friedman | 2659052 | 2008-06-08 01:23:18 +0000 | [diff] [blame] | 149 | const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType()); |
| 150 | const llvm::Type *LPtrTy = |
| 151 | llvm::PointerType::get(LTy, D.getType().getAddressSpace()); |
| 152 | DMEntry = llvm::ConstantExpr::getBitCast(GV, LPtrTy); |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 153 | |
| 154 | // Emit global variable debug descriptor for static vars. |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 155 | CGDebugInfo *DI = getDebugInfo(); |
Mike Stump | 4451bd9 | 2009-02-20 00:19:45 +0000 | [diff] [blame] | 156 | if (DI) { |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 157 | DI->setLocation(D.getLocation()); |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 158 | DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D); |
| 159 | } |
Anders Carlsson | 1a86b33 | 2007-10-17 00:52:43 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 162 | /// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a |
| 163 | /// variable declaration with auto, register, or no storage class specifier. |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 164 | /// These turn into simple stack objects, or GlobalValues depending on target. |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 165 | void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) { |
Chris Lattner | 8bcfc5b | 2008-04-06 23:10:54 +0000 | [diff] [blame] | 166 | QualType Ty = D.getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 167 | |
| 168 | llvm::Value *DeclPtr; |
Eli Friedman | 3c2b317 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 169 | if (Ty->isConstantSizeType()) { |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 170 | if (!Target.useGlobalsForAutomaticVariables()) { |
| 171 | // A normal fixed sized variable becomes an alloca in the entry block. |
| 172 | const llvm::Type *LTy = ConvertType(Ty); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 173 | llvm::AllocaInst *Alloc = |
Douglas Gregor | 6ec3668 | 2009-02-18 23:53:56 +0000 | [diff] [blame] | 174 | CreateTempAlloca(LTy, CGM.getMangledName(&D)); |
Eli Friedman | 9c2f06b | 2009-02-22 03:23:43 +0000 | [diff] [blame^] | 175 | Alloc->setAlignment(getContext().getDeclAlignInBytes(&D)); |
Eli Friedman | 77eedd6 | 2008-05-31 21:20:41 +0000 | [diff] [blame] | 176 | DeclPtr = Alloc; |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 177 | } else { |
| 178 | // Targets that don't support recursion emit locals as globals. |
| 179 | const char *Class = |
| 180 | D.getStorageClass() == VarDecl::Register ? ".reg." : ".auto."; |
Sanjiv Gupta | 45206ec | 2009-02-03 18:07:49 +0000 | [diff] [blame] | 181 | DeclPtr = GenerateStaticBlockVarDecl(D, true, Class, |
| 182 | llvm::GlobalValue |
| 183 | ::InternalLinkage); |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 184 | } |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 185 | |
| 186 | if (Ty->isVariablyModifiedType()) |
| 187 | EmitVLASize(Ty); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 188 | } else { |
Anders Carlsson | 5ecb1b9 | 2009-02-09 20:41:50 +0000 | [diff] [blame] | 189 | if (!DidCallStackSave) { |
Anders Carlsson | 5d46315 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 190 | // Save the stack. |
| 191 | const llvm::Type *LTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 192 | llvm::Value *Stack = CreateTempAlloca(LTy, "saved_stack"); |
| 193 | |
| 194 | llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave); |
| 195 | llvm::Value *V = Builder.CreateCall(F); |
| 196 | |
| 197 | Builder.CreateStore(V, Stack); |
Anders Carlsson | 5ecb1b9 | 2009-02-09 20:41:50 +0000 | [diff] [blame] | 198 | |
| 199 | DidCallStackSave = true; |
Anders Carlsson | 5d46315 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 200 | |
Anders Carlsson | 5ecb1b9 | 2009-02-09 20:41:50 +0000 | [diff] [blame] | 201 | { |
| 202 | // Push a cleanup block and restore the stack there. |
| 203 | CleanupScope scope(*this); |
| 204 | |
| 205 | V = Builder.CreateLoad(Stack, "tmp"); |
| 206 | llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stackrestore); |
| 207 | Builder.CreateCall(F, V); |
| 208 | } |
Anders Carlsson | 5d46315 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 209 | } |
Anders Carlsson | 5ecb1b9 | 2009-02-09 20:41:50 +0000 | [diff] [blame] | 210 | |
Anders Carlsson | 5d46315 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 211 | // Get the element type. |
| 212 | const llvm::Type *LElemTy = ConvertType(Ty); |
| 213 | const llvm::Type *LElemPtrTy = |
| 214 | llvm::PointerType::get(LElemTy, D.getType().getAddressSpace()); |
| 215 | |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 216 | llvm::Value *VLASize = EmitVLASize(Ty); |
Anders Carlsson | 5d46315 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 217 | |
Anders Carlsson | 96f2147 | 2009-02-05 19:43:10 +0000 | [diff] [blame] | 218 | // Downcast the VLA size expression |
| 219 | VLASize = Builder.CreateIntCast(VLASize, llvm::Type::Int32Ty, false, "tmp"); |
| 220 | |
Anders Carlsson | 5d46315 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 221 | // Allocate memory for the array. |
| 222 | llvm::Value *VLA = Builder.CreateAlloca(llvm::Type::Int8Ty, VLASize, "vla"); |
Eli Friedman | 8f39f5e | 2008-12-20 23:11:59 +0000 | [diff] [blame] | 223 | DeclPtr = Builder.CreateBitCast(VLA, LElemPtrTy, "tmp"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 224 | } |
Eli Friedman | 8f39f5e | 2008-12-20 23:11:59 +0000 | [diff] [blame] | 225 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 226 | llvm::Value *&DMEntry = LocalDeclMap[&D]; |
| 227 | assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); |
| 228 | DMEntry = DeclPtr; |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 229 | |
| 230 | // Emit debug info for local var declaration. |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 231 | if (CGDebugInfo *DI = getDebugInfo()) { |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 232 | DI->setLocation(D.getLocation()); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 233 | DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder); |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Chris Lattner | 1978596 | 2007-07-12 00:39:48 +0000 | [diff] [blame] | 236 | // If this local has an initializer, emit it now. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 237 | if (const Expr *Init = D.getInit()) { |
Chris Lattner | d31beb1 | 2007-08-26 07:29:23 +0000 | [diff] [blame] | 238 | if (!hasAggregateLLVMType(Init->getType())) { |
| 239 | llvm::Value *V = EmitScalarExpr(Init); |
Chris Lattner | 8b2f3b7 | 2007-08-26 07:30:49 +0000 | [diff] [blame] | 240 | Builder.CreateStore(V, DeclPtr, D.getType().isVolatileQualified()); |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 241 | } else if (Init->getType()->isAnyComplexType()) { |
Chris Lattner | 190dbe2 | 2007-08-26 16:22:13 +0000 | [diff] [blame] | 242 | EmitComplexExprIntoAddr(Init, DeclPtr, D.getType().isVolatileQualified()); |
Chris Lattner | 9a19edf | 2007-08-26 05:13:54 +0000 | [diff] [blame] | 243 | } else { |
Chris Lattner | 8b2f3b7 | 2007-08-26 07:30:49 +0000 | [diff] [blame] | 244 | EmitAggExpr(Init, DeclPtr, D.getType().isVolatileQualified()); |
Chris Lattner | 9a19edf | 2007-08-26 05:13:54 +0000 | [diff] [blame] | 245 | } |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 246 | } |
Anders Carlsson | 69c68b7 | 2009-02-07 23:51:38 +0000 | [diff] [blame] | 247 | |
| 248 | // Handle the cleanup attribute |
| 249 | if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) { |
| 250 | const FunctionDecl *FD = CA->getFunctionDecl(); |
| 251 | |
| 252 | llvm::Constant* F = CGM.GetAddrOfFunction(FD); |
| 253 | assert(F && "Could not find function!"); |
| 254 | |
| 255 | CleanupScope scope(*this); |
| 256 | |
| 257 | CallArgList Args; |
| 258 | Args.push_back(std::make_pair(RValue::get(DeclPtr), |
| 259 | getContext().getPointerType(D.getType()))); |
| 260 | |
| 261 | EmitCall(CGM.getTypes().getFunctionInfo(FD), F, Args); |
| 262 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 265 | /// Emit an alloca (or GlobalValue depending on target) |
| 266 | /// for the specified parameter and set up LocalDeclMap. |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 267 | void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg) { |
| 268 | // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl? |
Sanjiv Gupta | 31fc07d | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 269 | assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) && |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 270 | "Invalid argument to EmitParmDecl"); |
Chris Lattner | 8bcfc5b | 2008-04-06 23:10:54 +0000 | [diff] [blame] | 271 | QualType Ty = D.getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 272 | |
| 273 | llvm::Value *DeclPtr; |
Eli Friedman | 3c2b317 | 2008-02-15 12:20:59 +0000 | [diff] [blame] | 274 | if (!Ty->isConstantSizeType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 275 | // Variable sized values always are passed by-reference. |
| 276 | DeclPtr = Arg; |
Chris Lattner | 2621fd1 | 2008-05-08 05:58:21 +0000 | [diff] [blame] | 277 | } else if (Target.useGlobalsForAutomaticVariables()) { |
Sanjiv Gupta | 45206ec | 2009-02-03 18:07:49 +0000 | [diff] [blame] | 278 | // Targets that don't have stack use global address space for parameters. |
| 279 | // Specify external linkage for such globals so that llvm optimizer do |
| 280 | // not assume there values initialized as zero. |
Sanjiv Gupta | 193cdbe | 2009-02-10 04:17:25 +0000 | [diff] [blame] | 281 | DeclPtr = GenerateStaticBlockVarDecl(D, true, ".auto.", |
Sanjiv Gupta | 45206ec | 2009-02-03 18:07:49 +0000 | [diff] [blame] | 282 | llvm::GlobalValue::ExternalLinkage); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 283 | } else { |
Dan Gohman | d79a726 | 2008-05-22 22:12:56 +0000 | [diff] [blame] | 284 | // 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] | 285 | const llvm::Type *LTy = ConvertType(Ty); |
Dan Gohman | d79a726 | 2008-05-22 22:12:56 +0000 | [diff] [blame] | 286 | if (LTy->isSingleValueType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 287 | // TODO: Alignment |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 288 | std::string Name = D.getNameAsString(); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 289 | Name += ".addr"; |
| 290 | DeclPtr = CreateTempAlloca(LTy, Name.c_str()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 291 | |
| 292 | // Store the initial value into the alloca. |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 293 | Builder.CreateStore(Arg, DeclPtr,Ty.isVolatileQualified()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 294 | } else { |
| 295 | // Otherwise, if this is an aggregate, just use the input pointer. |
| 296 | DeclPtr = Arg; |
| 297 | } |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 298 | Arg->setName(D.getNameAsString()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | llvm::Value *&DMEntry = LocalDeclMap[&D]; |
| 302 | assert(DMEntry == 0 && "Decl already exists in localdeclmap!"); |
| 303 | DMEntry = DeclPtr; |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 304 | |
| 305 | // Emit debug info for param declaration. |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 306 | if (CGDebugInfo *DI = getDebugInfo()) { |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 307 | DI->setLocation(D.getLocation()); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 308 | DI->EmitDeclareOfArgVariable(&D, DeclPtr, Builder); |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 309 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 310 | } |
| 311 | |