blob: 91071afeb800a0557723fd316c5c804135bef768 [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) {
Chris Lattner4b009652007-07-25 00:24:17 +000063 switch (D.getStorageClass()) {
64 case VarDecl::Static:
Anders Carlsson855d78d2007-10-17 00:52:43 +000065 return EmitStaticBlockVarDecl(D);
Chris Lattner4b009652007-07-25 00:24:17 +000066 case VarDecl::Extern:
Lauro Ramos Venancio2348d972008-02-16 22:30:38 +000067 // Don't emit it now, allow it to be emitted lazily on its first use.
68 return;
Chris Lattner4b009652007-07-25 00:24:17 +000069 default:
70 assert((D.getStorageClass() == VarDecl::None ||
71 D.getStorageClass() == VarDecl::Auto ||
72 D.getStorageClass() == VarDecl::Register) &&
73 "Unknown storage class");
74 return EmitLocalBlockVarDecl(D);
75 }
76}
77
Daniel Dunbardea59212009-02-25 19:24:29 +000078llvm::GlobalVariable *
79CodeGenFunction::CreateStaticBlockVarDecl(const VarDecl &D,
80 const char *Separator,
81 llvm::GlobalValue::LinkageTypes
82 Linkage) {
Chris Lattner42a21742008-04-06 23:10:54 +000083 QualType Ty = D.getType();
Eli Friedman62f67fd2008-02-15 12:20:59 +000084 assert(Ty->isConstantSizeType() && "VLAs can't be static");
Nate Begeman8a704172008-04-19 04:17:09 +000085
Chris Lattner9b026342008-02-06 04:54:32 +000086 std::string ContextName;
Anders Carlssonb10bac72008-08-25 01:38:19 +000087 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurFuncDecl))
Douglas Gregor3c3c4542009-02-18 23:53:56 +000088 ContextName = CGM.getMangledName(FD);
Anders Carlssonb10bac72008-08-25 01:38:19 +000089 else if (isa<ObjCMethodDecl>(CurFuncDecl))
90 ContextName = std::string(CurFn->getNameStart(),
91 CurFn->getNameStart() + CurFn->getNameLen());
Mike Stump2b6933f2009-02-28 09:07:16 +000092 else if (isa<BlockDecl>(CurFuncDecl))
93 // FIXME: We want to traverse up and pick a name based upon where we came
94 // from.
95 ContextName = "block";
Chris Lattner9b026342008-02-06 04:54:32 +000096 else
Anders Carlssonb10bac72008-08-25 01:38:19 +000097 assert(0 && "Unknown context for block var decl");
Nate Begeman8a704172008-04-19 04:17:09 +000098
Daniel Dunbardea59212009-02-25 19:24:29 +000099 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
100 return new llvm::GlobalVariable(LTy, Ty.isConstant(getContext()), Linkage,
101 llvm::Constant::getNullValue(LTy),
102 ContextName + Separator + D.getNameAsString(),
103 &CGM.getModule(), 0, Ty.getAddressSpace());
104}
105
Daniel Dunbare9b9bba2009-02-25 19:45:19 +0000106void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D) {
Daniel Dunbardea59212009-02-25 19:24:29 +0000107
Daniel Dunbare9b9bba2009-02-25 19:45:19 +0000108 llvm::Value *&DMEntry = LocalDeclMap[&D];
109 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
110
111 llvm::GlobalVariable *GV =
112 CreateStaticBlockVarDecl(D, ".", llvm::GlobalValue::InternalLinkage);
113
Daniel Dunbar06d71d82009-02-25 20:08:33 +0000114 // Store into LocalDeclMap before generating initializer to handle
115 // circular references.
116 DMEntry = GV;
117
Daniel Dunbare9b9bba2009-02-25 19:45:19 +0000118 if (D.getInit()) {
Daniel Dunbardea59212009-02-25 19:24:29 +0000119 llvm::Constant *Init = CGM.EmitConstantExpr(D.getInit(), this);
120
121 // If constant emission failed, then this should be a C++ static
122 // initializer.
123 if (!Init) {
124 if (!getContext().getLangOptions().CPlusPlus)
125 CGM.ErrorUnsupported(D.getInit(), "constant l-value expression");
126 else
127 GenerateStaticCXXBlockVarDeclInit(D, GV);
128 } else {
129 // The initializer may differ in type from the global. Rewrite
130 // the global to match the initializer!?
131 //
132 // FIXME: This matches what we have been doing historically, but
133 // it seems bad. Shouldn't the init expression have the right
134 // type?
135 if (GV->getType() != Init->getType()) {
136 llvm::GlobalVariable *OldGV = GV;
137
138 GV = new llvm::GlobalVariable(Init->getType(), OldGV->isConstant(),
139 OldGV->getLinkage(), Init, "",
140 &CGM.getModule(), 0,
141 D.getType().getAddressSpace());
142
143 // Steal the name of the old global
144 GV->takeName(OldGV);
145
146 // Replace all uses of the old global with the new global
147 llvm::Constant *NewPtrForOldDecl =
148 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
149 OldGV->replaceAllUsesWith(NewPtrForOldDecl);
150
151 // Erase the old global, since it is no longer used.
152 OldGV->eraseFromParent();
153 }
154
155 GV->setInitializer(Init);
156 }
157 }
Nate Begeman8a704172008-04-19 04:17:09 +0000158
Daniel Dunbard79bb392009-02-12 23:32:54 +0000159 // FIXME: Merge attribute handling.
Nate Begeman8a704172008-04-19 04:17:09 +0000160 if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) {
161 SourceManager &SM = CGM.getContext().getSourceManager();
162 llvm::Constant *Ann =
Chris Lattner18c8dc02009-01-16 07:36:28 +0000163 CGM.EmitAnnotateAttr(GV, AA,
164 SM.getInstantiationLineNumber(D.getLocation()));
Nate Begeman8a704172008-04-19 04:17:09 +0000165 CGM.AddAnnotation(Ann);
166 }
167
Daniel Dunbard79bb392009-02-12 23:32:54 +0000168 if (const SectionAttr *SA = D.getAttr<SectionAttr>())
169 GV->setSection(SA->getName());
170
Daniel Dunbar375da3f2009-02-13 22:08:43 +0000171 if (D.getAttr<UsedAttr>())
172 CGM.AddUsedGlobal(GV);
173
Daniel Dunbar06d71d82009-02-25 20:08:33 +0000174 // We may have to cast the constant because of the initializer
175 // mismatch above.
176 //
177 // FIXME: It is really dangerous to store this in the map; if anyone
178 // RAUW's the GV uses of this constant will be invalid.
Eli Friedman168cf2a2008-06-08 01:23:18 +0000179 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType());
180 const llvm::Type *LPtrTy =
181 llvm::PointerType::get(LTy, D.getType().getAddressSpace());
182 DMEntry = llvm::ConstantExpr::getBitCast(GV, LPtrTy);
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000183
184 // Emit global variable debug descriptor for static vars.
Anders Carlsson73007792009-02-13 08:11:52 +0000185 CGDebugInfo *DI = getDebugInfo();
Mike Stumpf3aafd62009-02-20 00:19:45 +0000186 if (DI) {
Daniel Dunbar6fc1f972008-10-17 16:15:48 +0000187 DI->setLocation(D.getLocation());
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000188 DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D);
189 }
Anders Carlsson855d78d2007-10-17 00:52:43 +0000190}
191
Mike Stumpad9605d2009-03-04 03:23:46 +0000192/// BuildByRefType - This routine changes a __block variable declared as T x
193/// into:
194///
195/// struct {
196/// void *__isa;
197/// void *__forwarding;
198/// int32_t __flags;
199/// int32_t __size;
200/// void *__copy_helper;
201/// void *__destroy_helper;
202/// T x;
203/// } x
204///
205/// Align is the alignment needed in bytes for x.
206const llvm::Type *CodeGenFunction::BuildByRefType(QualType Ty,
207 uint64_t Align) {
208 const llvm::Type *LTy = ConvertType(Ty);
209 bool needsCopyDispose = BlockRequiresCopying(Ty);
210 std::vector<const llvm::Type *> Types(needsCopyDispose*2+5);
211 const llvm::PointerType *PtrToInt8Ty
212 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
213 Types[0] = PtrToInt8Ty;
214 Types[1] = PtrToInt8Ty;
215 Types[2] = llvm::Type::Int32Ty;
216 Types[3] = llvm::Type::Int32Ty;
217 if (needsCopyDispose) {
218 Types[4] = PtrToInt8Ty;
219 Types[5] = PtrToInt8Ty;
220 }
221 // FIXME: Align this on at least an Align boundary.
222 Types[needsCopyDispose*2 + 4] = LTy;
223 return llvm::StructType::get(Types, false);
224}
225
Chris Lattner4b009652007-07-25 00:24:17 +0000226/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
227/// variable declaration with auto, register, or no storage class specifier.
Chris Lattner85970f32008-05-08 05:58:21 +0000228/// These turn into simple stack objects, or GlobalValues depending on target.
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000229void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
Chris Lattner42a21742008-04-06 23:10:54 +0000230 QualType Ty = D.getType();
Mike Stumpad9605d2009-03-04 03:23:46 +0000231 bool isByRef = D.getAttr<BlocksAttr>();
Chris Lattner4b009652007-07-25 00:24:17 +0000232
233 llvm::Value *DeclPtr;
Eli Friedman62f67fd2008-02-15 12:20:59 +0000234 if (Ty->isConstantSizeType()) {
Chris Lattner85970f32008-05-08 05:58:21 +0000235 if (!Target.useGlobalsForAutomaticVariables()) {
236 // A normal fixed sized variable becomes an alloca in the entry block.
237 const llvm::Type *LTy = ConvertType(Ty);
Mike Stumpad9605d2009-03-04 03:23:46 +0000238 if (isByRef)
239 LTy = BuildByRefType(Ty, getContext().getDeclAlignInBytes(&D));
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000240 llvm::AllocaInst *Alloc =
Douglas Gregor3c3c4542009-02-18 23:53:56 +0000241 CreateTempAlloca(LTy, CGM.getMangledName(&D));
Mike Stumpad9605d2009-03-04 03:23:46 +0000242 if (isByRef)
243 Alloc->setAlignment(std::max(getContext().getDeclAlignInBytes(&D),
244 getContext().getTypeAlign(getContext().VoidPtrTy) / 8));
245 else
246 Alloc->setAlignment(getContext().getDeclAlignInBytes(&D));
Eli Friedman696758f2008-05-31 21:20:41 +0000247 DeclPtr = Alloc;
Chris Lattner85970f32008-05-08 05:58:21 +0000248 } else {
249 // Targets that don't support recursion emit locals as globals.
250 const char *Class =
251 D.getStorageClass() == VarDecl::Register ? ".reg." : ".auto.";
Daniel Dunbare9b9bba2009-02-25 19:45:19 +0000252 DeclPtr = CreateStaticBlockVarDecl(D, Class,
253 llvm::GlobalValue
254 ::InternalLinkage);
Chris Lattner85970f32008-05-08 05:58:21 +0000255 }
Anders Carlssond9767612008-12-20 20:46:34 +0000256
257 if (Ty->isVariablyModifiedType())
258 EmitVLASize(Ty);
Chris Lattner4b009652007-07-25 00:24:17 +0000259 } else {
Anders Carlssonc5656e72009-02-09 20:41:50 +0000260 if (!DidCallStackSave) {
Anders Carlsson9be6aaf2008-12-12 07:38:43 +0000261 // Save the stack.
262 const llvm::Type *LTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
263 llvm::Value *Stack = CreateTempAlloca(LTy, "saved_stack");
264
265 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
266 llvm::Value *V = Builder.CreateCall(F);
267
268 Builder.CreateStore(V, Stack);
Anders Carlssonc5656e72009-02-09 20:41:50 +0000269
270 DidCallStackSave = true;
Anders Carlsson9be6aaf2008-12-12 07:38:43 +0000271
Anders Carlssonc5656e72009-02-09 20:41:50 +0000272 {
273 // Push a cleanup block and restore the stack there.
274 CleanupScope scope(*this);
275
276 V = Builder.CreateLoad(Stack, "tmp");
277 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
278 Builder.CreateCall(F, V);
279 }
Anders Carlsson9be6aaf2008-12-12 07:38:43 +0000280 }
Anders Carlssonc5656e72009-02-09 20:41:50 +0000281
Anders Carlsson9be6aaf2008-12-12 07:38:43 +0000282 // Get the element type.
283 const llvm::Type *LElemTy = ConvertType(Ty);
284 const llvm::Type *LElemPtrTy =
285 llvm::PointerType::get(LElemTy, D.getType().getAddressSpace());
286
Anders Carlssond9767612008-12-20 20:46:34 +0000287 llvm::Value *VLASize = EmitVLASize(Ty);
Anders Carlsson9be6aaf2008-12-12 07:38:43 +0000288
Anders Carlsson8f30de92009-02-05 19:43:10 +0000289 // Downcast the VLA size expression
290 VLASize = Builder.CreateIntCast(VLASize, llvm::Type::Int32Ty, false, "tmp");
291
Anders Carlsson9be6aaf2008-12-12 07:38:43 +0000292 // Allocate memory for the array.
293 llvm::Value *VLA = Builder.CreateAlloca(llvm::Type::Int8Ty, VLASize, "vla");
Eli Friedman8fef47e2008-12-20 23:11:59 +0000294 DeclPtr = Builder.CreateBitCast(VLA, LElemPtrTy, "tmp");
Chris Lattner4b009652007-07-25 00:24:17 +0000295 }
Eli Friedman8fef47e2008-12-20 23:11:59 +0000296
Chris Lattner4b009652007-07-25 00:24:17 +0000297 llvm::Value *&DMEntry = LocalDeclMap[&D];
298 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
299 DMEntry = DeclPtr;
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000300
301 // Emit debug info for local var declaration.
Anders Carlsson73007792009-02-13 08:11:52 +0000302 if (CGDebugInfo *DI = getDebugInfo()) {
Daniel Dunbar6fc1f972008-10-17 16:15:48 +0000303 DI->setLocation(D.getLocation());
Mike Stumpad9605d2009-03-04 03:23:46 +0000304 if (isByRef) {
305 llvm::Value *Loc;
306 bool needsCopyDispose = BlockRequiresCopying(Ty);
307 // FIXME: I think we need to indirect through the forwarding pointer first
308 Loc = Builder.CreateStructGEP(DeclPtr, needsCopyDispose*2+4, "x");
309 DI->EmitDeclareOfAutoVariable(&D, Loc, Builder);
310 } else
311 DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder);
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000312 }
313
Chris Lattner4b009652007-07-25 00:24:17 +0000314 // If this local has an initializer, emit it now.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000315 if (const Expr *Init = D.getInit()) {
Mike Stumpad9605d2009-03-04 03:23:46 +0000316 llvm::Value *Loc = DeclPtr;
317 if (isByRef) {
318 bool needsCopyDispose = BlockRequiresCopying(Ty);
319 Loc = Builder.CreateStructGEP(DeclPtr, needsCopyDispose*2+4, "x");
320 }
Chris Lattner222d3222007-08-26 07:29:23 +0000321 if (!hasAggregateLLVMType(Init->getType())) {
322 llvm::Value *V = EmitScalarExpr(Init);
Mike Stumpad9605d2009-03-04 03:23:46 +0000323 Builder.CreateStore(V, Loc, D.getType().isVolatileQualified());
Chris Lattnerde0908b2008-04-04 16:54:41 +0000324 } else if (Init->getType()->isAnyComplexType()) {
Mike Stumpad9605d2009-03-04 03:23:46 +0000325 EmitComplexExprIntoAddr(Init, Loc, D.getType().isVolatileQualified());
Chris Lattner64a113b2007-08-26 05:13:54 +0000326 } else {
Mike Stumpad9605d2009-03-04 03:23:46 +0000327 EmitAggExpr(Init, Loc, D.getType().isVolatileQualified());
328 }
329 }
330 if (isByRef) {
331 const llvm::PointerType *PtrToInt8Ty
332 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
333
334 llvm::Value *isa_field = Builder.CreateStructGEP(DeclPtr, 0);
335 llvm::Value *forwarding_field = Builder.CreateStructGEP(DeclPtr, 1);
336 llvm::Value *flags_field = Builder.CreateStructGEP(DeclPtr, 2);
337 llvm::Value *size_field = Builder.CreateStructGEP(DeclPtr, 3);
338 llvm::Value *V;
339 int flag = 0;
340 int flags = 0;
341
342 if (Ty->isBlockPointerType()) {
343 flag |= BLOCK_FIELD_IS_BLOCK;
344 flags |= BLOCK_HAS_COPY_DISPOSE;
345 } else if (BlockRequiresCopying(Ty)) {
346 flags |= BLOCK_HAS_COPY_DISPOSE;
347 flag |= BLOCK_FIELD_IS_OBJECT;
348 }
349 // FIXME: Need to set BLOCK_FIELD_IS_WEAK as appropriate.
350
351 int isa = 0;
352 if (flag&BLOCK_FIELD_IS_WEAK)
353 isa = 1;
354 V = llvm::ConstantInt::get(llvm::Type::Int32Ty, isa);
355 V = Builder.CreateIntToPtr(V, PtrToInt8Ty, "tmp");
356 Builder.CreateStore(V, isa_field);
357
358 V = Builder.CreateBitCast(DeclPtr, PtrToInt8Ty, "tmp");
359 Builder.CreateStore(V, forwarding_field);
360
361 V = llvm::ConstantInt::get(llvm::Type::Int32Ty, flags);
362 Builder.CreateStore(V, flags_field);
363
364 const llvm::Type *V1 = cast<llvm::PointerType>(DeclPtr->getType())->getElementType();
365 V = llvm::ConstantInt::get(llvm::Type::Int32Ty, CGM.getTargetData().getTypeStoreSizeInBits(V1) / 8);
366 Builder.CreateStore(V, size_field);
367
368 if (flags & BLOCK_HAS_COPY_DISPOSE) {
369 llvm::Value *copy_helper = Builder.CreateStructGEP(DeclPtr, 4);
370 llvm::Value *destroy_helper = Builder.CreateStructGEP(DeclPtr, 5);
371
372 Builder.CreateStore(BuildCopyHelper(flag), copy_helper);
373
374 Builder.CreateStore(BuildDestroyHelper(flag), destroy_helper);
Chris Lattner64a113b2007-08-26 05:13:54 +0000375 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000376 }
Anders Carlssonc40e10c2009-02-07 23:51:38 +0000377
378 // Handle the cleanup attribute
379 if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) {
380 const FunctionDecl *FD = CA->getFunctionDecl();
381
382 llvm::Constant* F = CGM.GetAddrOfFunction(FD);
383 assert(F && "Could not find function!");
384
385 CleanupScope scope(*this);
386
387 CallArgList Args;
388 Args.push_back(std::make_pair(RValue::get(DeclPtr),
389 getContext().getPointerType(D.getType())));
390
391 EmitCall(CGM.getTypes().getFunctionInfo(FD), F, Args);
392 }
Chris Lattner4b009652007-07-25 00:24:17 +0000393}
394
Chris Lattner85970f32008-05-08 05:58:21 +0000395/// Emit an alloca (or GlobalValue depending on target)
396/// for the specified parameter and set up LocalDeclMap.
Daniel Dunbarace33292008-08-16 03:19:19 +0000397void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg) {
398 // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
Sanjiv Guptafa451432008-10-31 09:52:39 +0000399 assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
Daniel Dunbarace33292008-08-16 03:19:19 +0000400 "Invalid argument to EmitParmDecl");
Chris Lattner42a21742008-04-06 23:10:54 +0000401 QualType Ty = D.getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000402
403 llvm::Value *DeclPtr;
Eli Friedman62f67fd2008-02-15 12:20:59 +0000404 if (!Ty->isConstantSizeType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000405 // Variable sized values always are passed by-reference.
406 DeclPtr = Arg;
Chris Lattner85970f32008-05-08 05:58:21 +0000407 } else if (Target.useGlobalsForAutomaticVariables()) {
Sanjiv Guptaf7fd8e52009-02-03 18:07:49 +0000408 // Targets that don't have stack use global address space for parameters.
409 // Specify external linkage for such globals so that llvm optimizer do
410 // not assume there values initialized as zero.
Daniel Dunbare9b9bba2009-02-25 19:45:19 +0000411 DeclPtr = CreateStaticBlockVarDecl(D, ".auto.",
412 llvm::GlobalValue::ExternalLinkage);
Chris Lattner4b009652007-07-25 00:24:17 +0000413 } else {
Dan Gohman377ba9f2008-05-22 22:12:56 +0000414 // A fixed sized single-value variable becomes an alloca in the entry block.
Chris Lattner4b009652007-07-25 00:24:17 +0000415 const llvm::Type *LTy = ConvertType(Ty);
Dan Gohman377ba9f2008-05-22 22:12:56 +0000416 if (LTy->isSingleValueType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000417 // TODO: Alignment
Chris Lattner6c5ec622008-11-24 04:00:27 +0000418 std::string Name = D.getNameAsString();
Daniel Dunbar04d35782008-09-17 00:51:38 +0000419 Name += ".addr";
420 DeclPtr = CreateTempAlloca(LTy, Name.c_str());
Chris Lattner4b009652007-07-25 00:24:17 +0000421
422 // Store the initial value into the alloca.
Eli Friedman2e630542008-06-13 23:01:12 +0000423 Builder.CreateStore(Arg, DeclPtr,Ty.isVolatileQualified());
Chris Lattner4b009652007-07-25 00:24:17 +0000424 } else {
425 // Otherwise, if this is an aggregate, just use the input pointer.
426 DeclPtr = Arg;
427 }
Chris Lattner6c5ec622008-11-24 04:00:27 +0000428 Arg->setName(D.getNameAsString());
Chris Lattner4b009652007-07-25 00:24:17 +0000429 }
430
431 llvm::Value *&DMEntry = LocalDeclMap[&D];
432 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
433 DMEntry = DeclPtr;
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000434
435 // Emit debug info for param declaration.
Anders Carlsson73007792009-02-13 08:11:52 +0000436 if (CGDebugInfo *DI = getDebugInfo()) {
Daniel Dunbar6fc1f972008-10-17 16:15:48 +0000437 DI->setLocation(D.getLocation());
Chris Lattner562ce0a2008-11-10 06:08:34 +0000438 DI->EmitDeclareOfArgVariable(&D, DeclPtr, Builder);
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000439 }
Chris Lattner4b009652007-07-25 00:24:17 +0000440}
441