blob: 5a5bb62b0633275160cd07632c5be83fbf6fb885 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Decl nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
Sanjiv Guptab2a10452008-05-30 10:30:31 +000014#include "CGDebugInfo.h"
Chris Lattner4b009652007-07-25 00:24:17 +000015#include "CodeGenFunction.h"
Anders Carlsson855d78d2007-10-17 00:52:43 +000016#include "CodeGenModule.h"
Daniel Dunbareee5cd12008-08-11 05:00:27 +000017#include "clang/AST/ASTContext.h"
Daniel Dunbar64789f82008-08-11 05:35:13 +000018#include "clang/AST/Decl.h"
Anders Carlssonb10bac72008-08-25 01:38:19 +000019#include "clang/AST/DeclObjC.h"
Nate Begeman8a704172008-04-19 04:17:09 +000020#include "clang/Basic/SourceManager.h"
Chris Lattner85970f32008-05-08 05:58:21 +000021#include "clang/Basic/TargetInfo.h"
Anders Carlsson855d78d2007-10-17 00:52:43 +000022#include "llvm/GlobalVariable.h"
Anders Carlsson9be6aaf2008-12-12 07:38:43 +000023#include "llvm/Intrinsics.h"
Mike Stumpad9605d2009-03-04 03:23:46 +000024#include "llvm/Target/TargetData.h"
Chris Lattner4b009652007-07-25 00:24:17 +000025#include "llvm/Type.h"
26using namespace clang;
27using namespace CodeGen;
28
29
30void CodeGenFunction::EmitDecl(const Decl &D) {
31 switch (D.getKind()) {
32 default: assert(0 && "Unknown decl kind!");
Chris Lattner3289bca2007-10-08 21:37:32 +000033 case Decl::ParmVar:
Chris Lattner4b009652007-07-25 00:24:17 +000034 assert(0 && "Parmdecls should not be in declstmts!");
Chris Lattner4b009652007-07-25 00:24:17 +000035 case Decl::Function: // void X();
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +000036 case Decl::Record: // struct/union/class X;
Chris Lattner4b009652007-07-25 00:24:17 +000037 case Decl::Enum: // enum X;
Daniel Dunbar952f4732008-08-29 17:28:43 +000038 case Decl::EnumConstant: // enum ? { X = ? }
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +000039 case Decl::CXXRecord: // struct/union/class X; [C++]
Chris Lattner4b009652007-07-25 00:24:17 +000040 // None of these decls require codegen support.
41 return;
42
Daniel Dunbar952f4732008-08-29 17:28:43 +000043 case Decl::Var: {
44 const VarDecl &VD = cast<VarDecl>(D);
45 assert(VD.isBlockVarDecl() &&
46 "Should not see file-scope variables inside a function!");
47 return EmitBlockVarDecl(VD);
Chris Lattner4b009652007-07-25 00:24:17 +000048 }
Anders Carlssonef2f7df2008-12-20 21:51:53 +000049
50 case Decl::Typedef: { // typedef int X;
51 const TypedefDecl &TD = cast<TypedefDecl>(D);
52 QualType Ty = TD.getUnderlyingType();
53
54 if (Ty->isVariablyModifiedType())
55 EmitVLASize(Ty);
56 }
Daniel Dunbar952f4732008-08-29 17:28:43 +000057 }
Chris Lattner4b009652007-07-25 00:24:17 +000058}
59
60/// EmitBlockVarDecl - This method handles emission of any variable declaration
61/// inside a function, including static vars etc.
Steve Naroff72a6ebc2008-04-15 22:42:06 +000062void CodeGenFunction::EmitBlockVarDecl(const VarDecl &D) {
Fariborz Jahanian5b1fe332009-03-30 20:32:06 +000063 if (D.getAttr<AsmLabelAttr>())
64 CGM.ErrorUnsupported(&D, "__asm__");
65
Chris Lattner4b009652007-07-25 00:24:17 +000066 switch (D.getStorageClass()) {
67 case VarDecl::Static:
Anders Carlsson855d78d2007-10-17 00:52:43 +000068 return EmitStaticBlockVarDecl(D);
Chris Lattner4b009652007-07-25 00:24:17 +000069 case VarDecl::Extern:
Lauro Ramos Venancio2348d972008-02-16 22:30:38 +000070 // Don't emit it now, allow it to be emitted lazily on its first use.
71 return;
Chris Lattner4b009652007-07-25 00:24:17 +000072 default:
73 assert((D.getStorageClass() == VarDecl::None ||
74 D.getStorageClass() == VarDecl::Auto ||
75 D.getStorageClass() == VarDecl::Register) &&
76 "Unknown storage class");
77 return EmitLocalBlockVarDecl(D);
78 }
79}
80
Daniel Dunbardea59212009-02-25 19:24:29 +000081llvm::GlobalVariable *
82CodeGenFunction::CreateStaticBlockVarDecl(const VarDecl &D,
83 const char *Separator,
84 llvm::GlobalValue::LinkageTypes
85 Linkage) {
Chris Lattner42a21742008-04-06 23:10:54 +000086 QualType Ty = D.getType();
Eli Friedman62f67fd2008-02-15 12:20:59 +000087 assert(Ty->isConstantSizeType() && "VLAs can't be static");
Nate Begeman8a704172008-04-19 04:17:09 +000088
Chris Lattner9b026342008-02-06 04:54:32 +000089 std::string ContextName;
Anders Carlssonb10bac72008-08-25 01:38:19 +000090 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurFuncDecl))
Douglas Gregor3c3c4542009-02-18 23:53:56 +000091 ContextName = CGM.getMangledName(FD);
Anders Carlssonb10bac72008-08-25 01:38:19 +000092 else if (isa<ObjCMethodDecl>(CurFuncDecl))
93 ContextName = std::string(CurFn->getNameStart(),
94 CurFn->getNameStart() + CurFn->getNameLen());
Chris Lattner9b026342008-02-06 04:54:32 +000095 else
Anders Carlssonb10bac72008-08-25 01:38:19 +000096 assert(0 && "Unknown context for block var decl");
Nate Begeman8a704172008-04-19 04:17:09 +000097
Daniel Dunbardea59212009-02-25 19:24:29 +000098 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
99 return new llvm::GlobalVariable(LTy, Ty.isConstant(getContext()), Linkage,
100 llvm::Constant::getNullValue(LTy),
101 ContextName + Separator + D.getNameAsString(),
102 &CGM.getModule(), 0, Ty.getAddressSpace());
103}
104
Daniel Dunbare9b9bba2009-02-25 19:45:19 +0000105void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D) {
Daniel Dunbardea59212009-02-25 19:24:29 +0000106
Daniel Dunbare9b9bba2009-02-25 19:45:19 +0000107 llvm::Value *&DMEntry = LocalDeclMap[&D];
108 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
109
110 llvm::GlobalVariable *GV =
111 CreateStaticBlockVarDecl(D, ".", llvm::GlobalValue::InternalLinkage);
112
Daniel Dunbar06d71d82009-02-25 20:08:33 +0000113 // Store into LocalDeclMap before generating initializer to handle
114 // circular references.
115 DMEntry = GV;
116
Daniel Dunbare9b9bba2009-02-25 19:45:19 +0000117 if (D.getInit()) {
Daniel Dunbardea59212009-02-25 19:24:29 +0000118 llvm::Constant *Init = CGM.EmitConstantExpr(D.getInit(), this);
119
120 // If constant emission failed, then this should be a C++ static
121 // initializer.
122 if (!Init) {
123 if (!getContext().getLangOptions().CPlusPlus)
124 CGM.ErrorUnsupported(D.getInit(), "constant l-value expression");
125 else
126 GenerateStaticCXXBlockVarDeclInit(D, GV);
127 } else {
128 // The initializer may differ in type from the global. Rewrite
Eli Friedman9a8a4962009-03-04 04:22:58 +0000129 // the global to match the initializer. (We have to do this
130 // because some types, like unions, can't be completely represented
131 // in the LLVM type system.)
Daniel Dunbardea59212009-02-25 19:24:29 +0000132 if (GV->getType() != Init->getType()) {
133 llvm::GlobalVariable *OldGV = GV;
134
135 GV = new llvm::GlobalVariable(Init->getType(), OldGV->isConstant(),
136 OldGV->getLinkage(), Init, "",
137 &CGM.getModule(), 0,
138 D.getType().getAddressSpace());
139
140 // Steal the name of the old global
141 GV->takeName(OldGV);
142
143 // Replace all uses of the old global with the new global
144 llvm::Constant *NewPtrForOldDecl =
145 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
146 OldGV->replaceAllUsesWith(NewPtrForOldDecl);
147
148 // Erase the old global, since it is no longer used.
149 OldGV->eraseFromParent();
150 }
151
152 GV->setInitializer(Init);
153 }
154 }
Nate Begeman8a704172008-04-19 04:17:09 +0000155
Daniel Dunbard79bb392009-02-12 23:32:54 +0000156 // FIXME: Merge attribute handling.
Nate Begeman8a704172008-04-19 04:17:09 +0000157 if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) {
158 SourceManager &SM = CGM.getContext().getSourceManager();
159 llvm::Constant *Ann =
Chris Lattner18c8dc02009-01-16 07:36:28 +0000160 CGM.EmitAnnotateAttr(GV, AA,
161 SM.getInstantiationLineNumber(D.getLocation()));
Nate Begeman8a704172008-04-19 04:17:09 +0000162 CGM.AddAnnotation(Ann);
163 }
164
Daniel Dunbard79bb392009-02-12 23:32:54 +0000165 if (const SectionAttr *SA = D.getAttr<SectionAttr>())
166 GV->setSection(SA->getName());
167
Daniel Dunbar375da3f2009-02-13 22:08:43 +0000168 if (D.getAttr<UsedAttr>())
169 CGM.AddUsedGlobal(GV);
170
Daniel Dunbar06d71d82009-02-25 20:08:33 +0000171 // We may have to cast the constant because of the initializer
172 // mismatch above.
173 //
174 // FIXME: It is really dangerous to store this in the map; if anyone
175 // RAUW's the GV uses of this constant will be invalid.
Eli Friedman168cf2a2008-06-08 01:23:18 +0000176 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType());
177 const llvm::Type *LPtrTy =
178 llvm::PointerType::get(LTy, D.getType().getAddressSpace());
179 DMEntry = llvm::ConstantExpr::getBitCast(GV, LPtrTy);
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000180
181 // Emit global variable debug descriptor for static vars.
Anders Carlsson73007792009-02-13 08:11:52 +0000182 CGDebugInfo *DI = getDebugInfo();
Mike Stumpf3aafd62009-02-20 00:19:45 +0000183 if (DI) {
Daniel Dunbar6fc1f972008-10-17 16:15:48 +0000184 DI->setLocation(D.getLocation());
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000185 DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D);
186 }
Anders Carlsson855d78d2007-10-17 00:52:43 +0000187}
188
Mike Stumpad9605d2009-03-04 03:23:46 +0000189/// BuildByRefType - This routine changes a __block variable declared as T x
190/// into:
191///
192/// struct {
193/// void *__isa;
194/// void *__forwarding;
195/// int32_t __flags;
196/// int32_t __size;
197/// void *__copy_helper;
198/// void *__destroy_helper;
199/// T x;
200/// } x
201///
202/// Align is the alignment needed in bytes for x.
203const llvm::Type *CodeGenFunction::BuildByRefType(QualType Ty,
204 uint64_t Align) {
205 const llvm::Type *LTy = ConvertType(Ty);
206 bool needsCopyDispose = BlockRequiresCopying(Ty);
207 std::vector<const llvm::Type *> Types(needsCopyDispose*2+5);
208 const llvm::PointerType *PtrToInt8Ty
209 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
210 Types[0] = PtrToInt8Ty;
211 Types[1] = PtrToInt8Ty;
212 Types[2] = llvm::Type::Int32Ty;
213 Types[3] = llvm::Type::Int32Ty;
214 if (needsCopyDispose) {
215 Types[4] = PtrToInt8Ty;
216 Types[5] = PtrToInt8Ty;
217 }
218 // FIXME: Align this on at least an Align boundary.
219 Types[needsCopyDispose*2 + 4] = LTy;
220 return llvm::StructType::get(Types, false);
221}
222
Chris Lattner4b009652007-07-25 00:24:17 +0000223/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
224/// variable declaration with auto, register, or no storage class specifier.
Chris Lattner85970f32008-05-08 05:58:21 +0000225/// These turn into simple stack objects, or GlobalValues depending on target.
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000226void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
Chris Lattner42a21742008-04-06 23:10:54 +0000227 QualType Ty = D.getType();
Mike Stumpad9605d2009-03-04 03:23:46 +0000228 bool isByRef = D.getAttr<BlocksAttr>();
Mike Stump60294662009-03-05 01:23:13 +0000229 bool needsDispose = false;
Chris Lattner4b009652007-07-25 00:24:17 +0000230
231 llvm::Value *DeclPtr;
Eli Friedman62f67fd2008-02-15 12:20:59 +0000232 if (Ty->isConstantSizeType()) {
Chris Lattner85970f32008-05-08 05:58:21 +0000233 if (!Target.useGlobalsForAutomaticVariables()) {
234 // A normal fixed sized variable becomes an alloca in the entry block.
Eli Friedman86fc61a2009-03-04 04:25:14 +0000235 const llvm::Type *LTy = ConvertTypeForMem(Ty);
Mike Stumpad9605d2009-03-04 03:23:46 +0000236 if (isByRef)
237 LTy = BuildByRefType(Ty, getContext().getDeclAlignInBytes(&D));
Chris Lattner7fbb70d2009-03-22 00:24:14 +0000238 llvm::AllocaInst *Alloc = CreateTempAlloca(LTy);
239 Alloc->setName(D.getNameAsString().c_str());
240
Mike Stumpad9605d2009-03-04 03:23:46 +0000241 if (isByRef)
242 Alloc->setAlignment(std::max(getContext().getDeclAlignInBytes(&D),
Chris Lattner28466632009-03-22 00:32:22 +0000243 unsigned(Target.getPointerAlign(0) / 8)));
Mike Stumpad9605d2009-03-04 03:23:46 +0000244 else
245 Alloc->setAlignment(getContext().getDeclAlignInBytes(&D));
Eli Friedman696758f2008-05-31 21:20:41 +0000246 DeclPtr = Alloc;
Chris Lattner85970f32008-05-08 05:58:21 +0000247 } else {
248 // Targets that don't support recursion emit locals as globals.
249 const char *Class =
250 D.getStorageClass() == VarDecl::Register ? ".reg." : ".auto.";
Daniel Dunbare9b9bba2009-02-25 19:45:19 +0000251 DeclPtr = CreateStaticBlockVarDecl(D, Class,
252 llvm::GlobalValue
253 ::InternalLinkage);
Chris Lattner85970f32008-05-08 05:58:21 +0000254 }
Anders Carlssond9767612008-12-20 20:46:34 +0000255
256 if (Ty->isVariablyModifiedType())
257 EmitVLASize(Ty);
Chris Lattner4b009652007-07-25 00:24:17 +0000258 } else {
Anders Carlssonc5656e72009-02-09 20:41:50 +0000259 if (!DidCallStackSave) {
Anders Carlsson9be6aaf2008-12-12 07:38:43 +0000260 // Save the stack.
261 const llvm::Type *LTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
262 llvm::Value *Stack = CreateTempAlloca(LTy, "saved_stack");
263
264 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
265 llvm::Value *V = Builder.CreateCall(F);
266
267 Builder.CreateStore(V, Stack);
Anders Carlssonc5656e72009-02-09 20:41:50 +0000268
269 DidCallStackSave = true;
Anders Carlsson9be6aaf2008-12-12 07:38:43 +0000270
Anders Carlssonc5656e72009-02-09 20:41:50 +0000271 {
272 // Push a cleanup block and restore the stack there.
273 CleanupScope scope(*this);
274
275 V = Builder.CreateLoad(Stack, "tmp");
276 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
277 Builder.CreateCall(F, V);
278 }
Anders Carlsson9be6aaf2008-12-12 07:38:43 +0000279 }
Anders Carlssonc5656e72009-02-09 20:41:50 +0000280
Anders Carlsson9be6aaf2008-12-12 07:38:43 +0000281 // Get the element type.
Eli Friedman86fc61a2009-03-04 04:25:14 +0000282 const llvm::Type *LElemTy = ConvertTypeForMem(Ty);
Anders Carlsson9be6aaf2008-12-12 07:38:43 +0000283 const llvm::Type *LElemPtrTy =
284 llvm::PointerType::get(LElemTy, D.getType().getAddressSpace());
285
Anders Carlssond9767612008-12-20 20:46:34 +0000286 llvm::Value *VLASize = EmitVLASize(Ty);
Anders Carlsson9be6aaf2008-12-12 07:38:43 +0000287
Anders Carlsson8f30de92009-02-05 19:43:10 +0000288 // Downcast the VLA size expression
289 VLASize = Builder.CreateIntCast(VLASize, llvm::Type::Int32Ty, false, "tmp");
290
Anders Carlsson9be6aaf2008-12-12 07:38:43 +0000291 // Allocate memory for the array.
292 llvm::Value *VLA = Builder.CreateAlloca(llvm::Type::Int8Ty, VLASize, "vla");
Eli Friedman8fef47e2008-12-20 23:11:59 +0000293 DeclPtr = Builder.CreateBitCast(VLA, LElemPtrTy, "tmp");
Chris Lattner4b009652007-07-25 00:24:17 +0000294 }
Eli Friedman8fef47e2008-12-20 23:11:59 +0000295
Chris Lattner4b009652007-07-25 00:24:17 +0000296 llvm::Value *&DMEntry = LocalDeclMap[&D];
297 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
298 DMEntry = DeclPtr;
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000299
300 // Emit debug info for local var declaration.
Anders Carlsson73007792009-02-13 08:11:52 +0000301 if (CGDebugInfo *DI = getDebugInfo()) {
Daniel Dunbar6fc1f972008-10-17 16:15:48 +0000302 DI->setLocation(D.getLocation());
Mike Stumpad9605d2009-03-04 03:23:46 +0000303 if (isByRef) {
304 llvm::Value *Loc;
305 bool needsCopyDispose = BlockRequiresCopying(Ty);
306 // FIXME: I think we need to indirect through the forwarding pointer first
307 Loc = Builder.CreateStructGEP(DeclPtr, needsCopyDispose*2+4, "x");
308 DI->EmitDeclareOfAutoVariable(&D, Loc, Builder);
309 } else
310 DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder);
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000311 }
312
Chris Lattner4b009652007-07-25 00:24:17 +0000313 // If this local has an initializer, emit it now.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000314 if (const Expr *Init = D.getInit()) {
Mike Stumpad9605d2009-03-04 03:23:46 +0000315 llvm::Value *Loc = DeclPtr;
316 if (isByRef) {
317 bool needsCopyDispose = BlockRequiresCopying(Ty);
318 Loc = Builder.CreateStructGEP(DeclPtr, needsCopyDispose*2+4, "x");
319 }
Chris Lattner222d3222007-08-26 07:29:23 +0000320 if (!hasAggregateLLVMType(Init->getType())) {
321 llvm::Value *V = EmitScalarExpr(Init);
Eli Friedman86fc61a2009-03-04 04:25:14 +0000322 EmitStoreOfScalar(V, Loc, D.getType().isVolatileQualified());
Chris Lattnerde0908b2008-04-04 16:54:41 +0000323 } else if (Init->getType()->isAnyComplexType()) {
Mike Stumpad9605d2009-03-04 03:23:46 +0000324 EmitComplexExprIntoAddr(Init, Loc, D.getType().isVolatileQualified());
Chris Lattner64a113b2007-08-26 05:13:54 +0000325 } else {
Mike Stumpad9605d2009-03-04 03:23:46 +0000326 EmitAggExpr(Init, Loc, D.getType().isVolatileQualified());
327 }
328 }
329 if (isByRef) {
330 const llvm::PointerType *PtrToInt8Ty
331 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
332
333 llvm::Value *isa_field = Builder.CreateStructGEP(DeclPtr, 0);
334 llvm::Value *forwarding_field = Builder.CreateStructGEP(DeclPtr, 1);
335 llvm::Value *flags_field = Builder.CreateStructGEP(DeclPtr, 2);
336 llvm::Value *size_field = Builder.CreateStructGEP(DeclPtr, 3);
337 llvm::Value *V;
338 int flag = 0;
339 int flags = 0;
340
Mike Stump0a33ed32009-03-07 06:04:31 +0000341 needsDispose = true;
Mike Stump1bdbf632009-03-05 08:32:30 +0000342
Mike Stumpad9605d2009-03-04 03:23:46 +0000343 if (Ty->isBlockPointerType()) {
344 flag |= BLOCK_FIELD_IS_BLOCK;
345 flags |= BLOCK_HAS_COPY_DISPOSE;
346 } else if (BlockRequiresCopying(Ty)) {
Mike Stumpad9605d2009-03-04 03:23:46 +0000347 flag |= BLOCK_FIELD_IS_OBJECT;
Mike Stump4a0e5132009-03-06 04:53:30 +0000348 flags |= BLOCK_HAS_COPY_DISPOSE;
Mike Stumpad9605d2009-03-04 03:23:46 +0000349 }
Mike Stump0a33ed32009-03-07 06:04:31 +0000350
351 // FIXME: Someone double check this.
352 if (Ty.isObjCGCWeak())
353 flag |= BLOCK_FIELD_IS_WEAK;
Mike Stumpad9605d2009-03-04 03:23:46 +0000354
355 int isa = 0;
356 if (flag&BLOCK_FIELD_IS_WEAK)
357 isa = 1;
358 V = llvm::ConstantInt::get(llvm::Type::Int32Ty, isa);
Mike Stumpecd79422009-03-06 01:33:24 +0000359 V = Builder.CreateIntToPtr(V, PtrToInt8Ty, "isa");
Mike Stumpad9605d2009-03-04 03:23:46 +0000360 Builder.CreateStore(V, isa_field);
361
Mike Stumpecd79422009-03-06 01:33:24 +0000362 V = Builder.CreateBitCast(DeclPtr, PtrToInt8Ty, "forwarding");
Mike Stumpad9605d2009-03-04 03:23:46 +0000363 Builder.CreateStore(V, forwarding_field);
364
365 V = llvm::ConstantInt::get(llvm::Type::Int32Ty, flags);
366 Builder.CreateStore(V, flags_field);
367
Mike Stump1bdbf632009-03-05 08:32:30 +0000368 const llvm::Type *V1;
369 V1 = cast<llvm::PointerType>(DeclPtr->getType())->getElementType();
370 V = llvm::ConstantInt::get(llvm::Type::Int32Ty,
371 (CGM.getTargetData().getTypeStoreSizeInBits(V1)
372 / 8));
Mike Stumpad9605d2009-03-04 03:23:46 +0000373 Builder.CreateStore(V, size_field);
374
375 if (flags & BLOCK_HAS_COPY_DISPOSE) {
Mike Stump1bdbf632009-03-05 08:32:30 +0000376 BlockHasCopyDispose = true;
Mike Stumpad9605d2009-03-04 03:23:46 +0000377 llvm::Value *copy_helper = Builder.CreateStructGEP(DeclPtr, 4);
Mike Stump11973b62009-03-06 06:12:24 +0000378 Builder.CreateStore(BuildbyrefCopyHelper(DeclPtr->getType(), flag),
379 copy_helper);
Mike Stumpad9605d2009-03-04 03:23:46 +0000380
Mike Stump4a0e5132009-03-06 04:53:30 +0000381 llvm::Value *destroy_helper = Builder.CreateStructGEP(DeclPtr, 5);
382 Builder.CreateStore(BuildbyrefDestroyHelper(DeclPtr->getType(), flag),
383 destroy_helper);
Chris Lattner64a113b2007-08-26 05:13:54 +0000384 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000385 }
Anders Carlssonc40e10c2009-02-07 23:51:38 +0000386
387 // Handle the cleanup attribute
388 if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) {
389 const FunctionDecl *FD = CA->getFunctionDecl();
390
391 llvm::Constant* F = CGM.GetAddrOfFunction(FD);
392 assert(F && "Could not find function!");
393
394 CleanupScope scope(*this);
395
396 CallArgList Args;
397 Args.push_back(std::make_pair(RValue::get(DeclPtr),
398 getContext().getPointerType(D.getType())));
399
400 EmitCall(CGM.getTypes().getFunctionInfo(FD), F, Args);
401 }
Mike Stump60294662009-03-05 01:23:13 +0000402
Mike Stump6663f1b2009-03-05 02:34:38 +0000403 if (needsDispose && CGM.getLangOptions().getGCMode() != LangOptions::GCOnly) {
Mike Stump60294662009-03-05 01:23:13 +0000404 CleanupScope scope(*this);
Mike Stump4a0e5132009-03-06 04:53:30 +0000405 llvm::Value *V = Builder.CreateStructGEP(DeclPtr, 1, "forwarding");
406 V = Builder.CreateLoad(V, false);
407 BuildBlockRelease(V);
Mike Stump60294662009-03-05 01:23:13 +0000408 }
Chris Lattner4b009652007-07-25 00:24:17 +0000409}
410
Chris Lattner85970f32008-05-08 05:58:21 +0000411/// Emit an alloca (or GlobalValue depending on target)
412/// for the specified parameter and set up LocalDeclMap.
Daniel Dunbarace33292008-08-16 03:19:19 +0000413void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg) {
414 // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
Sanjiv Guptafa451432008-10-31 09:52:39 +0000415 assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
Daniel Dunbarace33292008-08-16 03:19:19 +0000416 "Invalid argument to EmitParmDecl");
Chris Lattner42a21742008-04-06 23:10:54 +0000417 QualType Ty = D.getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000418
419 llvm::Value *DeclPtr;
Eli Friedman62f67fd2008-02-15 12:20:59 +0000420 if (!Ty->isConstantSizeType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000421 // Variable sized values always are passed by-reference.
422 DeclPtr = Arg;
Chris Lattner85970f32008-05-08 05:58:21 +0000423 } else if (Target.useGlobalsForAutomaticVariables()) {
Sanjiv Guptaf7fd8e52009-02-03 18:07:49 +0000424 // Targets that don't have stack use global address space for parameters.
425 // Specify external linkage for such globals so that llvm optimizer do
426 // not assume there values initialized as zero.
Sanjiv Gupta737b9522009-03-07 18:09:52 +0000427 DeclPtr = CreateStaticBlockVarDecl(D, ".arg.",
Daniel Dunbare9b9bba2009-02-25 19:45:19 +0000428 llvm::GlobalValue::ExternalLinkage);
Chris Lattner4b009652007-07-25 00:24:17 +0000429 } else {
Dan Gohman377ba9f2008-05-22 22:12:56 +0000430 // A fixed sized single-value variable becomes an alloca in the entry block.
Eli Friedman86fc61a2009-03-04 04:25:14 +0000431 const llvm::Type *LTy = ConvertTypeForMem(Ty);
Dan Gohman377ba9f2008-05-22 22:12:56 +0000432 if (LTy->isSingleValueType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000433 // TODO: Alignment
Chris Lattner6c5ec622008-11-24 04:00:27 +0000434 std::string Name = D.getNameAsString();
Daniel Dunbar04d35782008-09-17 00:51:38 +0000435 Name += ".addr";
Chris Lattner7fbb70d2009-03-22 00:24:14 +0000436 DeclPtr = CreateTempAlloca(LTy);
437 DeclPtr->setName(Name.c_str());
Chris Lattner4b009652007-07-25 00:24:17 +0000438
439 // Store the initial value into the alloca.
Eli Friedman86fc61a2009-03-04 04:25:14 +0000440 EmitStoreOfScalar(Arg, DeclPtr, Ty.isVolatileQualified());
Chris Lattner4b009652007-07-25 00:24:17 +0000441 } else {
442 // Otherwise, if this is an aggregate, just use the input pointer.
443 DeclPtr = Arg;
444 }
Chris Lattner6c5ec622008-11-24 04:00:27 +0000445 Arg->setName(D.getNameAsString());
Chris Lattner4b009652007-07-25 00:24:17 +0000446 }
447
448 llvm::Value *&DMEntry = LocalDeclMap[&D];
449 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
450 DMEntry = DeclPtr;
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000451
452 // Emit debug info for param declaration.
Anders Carlsson73007792009-02-13 08:11:52 +0000453 if (CGDebugInfo *DI = getDebugInfo()) {
Daniel Dunbar6fc1f972008-10-17 16:15:48 +0000454 DI->setLocation(D.getLocation());
Chris Lattner562ce0a2008-11-10 06:08:34 +0000455 DI->EmitDeclareOfArgVariable(&D, DeclPtr, Builder);
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000456 }
Chris Lattner4b009652007-07-25 00:24:17 +0000457}
458