blob: 5abe31e2527235f153900a06d529776d9668754f [file] [log] [blame]
Chris Lattner84915fa2007-06-02 04:16:21 +00001//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattner84915fa2007-06-02 04:16:21 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Decl nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
Sanjiv Gupta18de6242008-05-30 10:30:31 +000014#include "CGDebugInfo.h"
Chris Lattner84915fa2007-06-02 04:16:21 +000015#include "CodeGenFunction.h"
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000016#include "CodeGenModule.h"
Daniel Dunbarad319a72008-08-11 05:00:27 +000017#include "clang/AST/ASTContext.h"
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000018#include "clang/AST/Decl.h"
Anders Carlsson4c03d982008-08-25 01:38:19 +000019#include "clang/AST/DeclObjC.h"
Nate Begemanfaae0812008-04-19 04:17:09 +000020#include "clang/Basic/SourceManager.h"
Chris Lattnerb781dc792008-05-08 05:58:21 +000021#include "clang/Basic/TargetInfo.h"
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000022#include "llvm/GlobalVariable.h"
Anders Carlsson30032882008-12-12 07:38:43 +000023#include "llvm/Intrinsics.h"
Mike Stump97d01d52009-03-04 03:23:46 +000024#include "llvm/Target/TargetData.h"
Chris Lattner53621a52007-06-13 20:44:40 +000025#include "llvm/Type.h"
Chris Lattner84915fa2007-06-02 04:16:21 +000026using namespace clang;
27using namespace CodeGen;
28
29
Chris Lattner1ad38f82007-06-09 01:20:56 +000030void CodeGenFunction::EmitDecl(const Decl &D) {
Chris Lattner1ad38f82007-06-09 01:20:56 +000031 switch (D.getKind()) {
Chris Lattner84915fa2007-06-02 04:16:21 +000032 default: assert(0 && "Unknown decl kind!");
Chris Lattnerba9dddb2007-10-08 21:37:32 +000033 case Decl::ParmVar:
Chris Lattner84915fa2007-06-02 04:16:21 +000034 assert(0 && "Parmdecls should not be in declstmts!");
Chris Lattner84915fa2007-06-02 04:16:21 +000035 case Decl::Function: // void X();
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +000036 case Decl::Record: // struct/union/class X;
Chris Lattner84915fa2007-06-02 04:16:21 +000037 case Decl::Enum: // enum X;
Mike Stump11289f42009-09-09 15:08:12 +000038 case Decl::EnumConstant: // enum ? { X = ? }
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +000039 case Decl::CXXRecord: // struct/union/class X; [C++]
Chris Lattner84915fa2007-06-02 04:16:21 +000040 // None of these decls require codegen support.
41 return;
Mike Stump11289f42009-09-09 15:08:12 +000042
Daniel Dunbara7998072008-08-29 17:28:43 +000043 case Decl::Var: {
44 const VarDecl &VD = cast<VarDecl>(D);
Mike Stump11289f42009-09-09 15:08:12 +000045 assert(VD.isBlockVarDecl() &&
Daniel Dunbara7998072008-08-29 17:28:43 +000046 "Should not see file-scope variables inside a function!");
47 return EmitBlockVarDecl(VD);
Chris Lattner84915fa2007-06-02 04:16:21 +000048 }
Mike Stump11289f42009-09-09 15:08:12 +000049
Anders Carlsson5d985f52008-12-20 21:51:53 +000050 case Decl::Typedef: { // typedef int X;
51 const TypedefDecl &TD = cast<TypedefDecl>(D);
52 QualType Ty = TD.getUnderlyingType();
Mike Stump11289f42009-09-09 15:08:12 +000053
Anders Carlsson5d985f52008-12-20 21:51:53 +000054 if (Ty->isVariablyModifiedType())
55 EmitVLASize(Ty);
56 }
Daniel Dunbara7998072008-08-29 17:28:43 +000057 }
Chris Lattner84915fa2007-06-02 04:16:21 +000058}
Chris Lattner03df1222007-06-02 04:53:11 +000059
60/// EmitBlockVarDecl - This method handles emission of any variable declaration
61/// inside a function, including static vars etc.
Steve Naroff08899ff2008-04-15 22:42:06 +000062void CodeGenFunction::EmitBlockVarDecl(const VarDecl &D) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +000063 if (D.hasAttr<AsmLabelAttr>())
Fariborz Jahanianed308a92009-03-30 20:32:06 +000064 CGM.ErrorUnsupported(&D, "__asm__");
Mike Stump11289f42009-09-09 15:08:12 +000065
Chris Lattner03df1222007-06-02 04:53:11 +000066 switch (D.getStorageClass()) {
Daniel Dunbar0ca16602009-04-14 02:25:56 +000067 case VarDecl::None:
68 case VarDecl::Auto:
69 case VarDecl::Register:
70 return EmitLocalBlockVarDecl(D);
Chris Lattner03df1222007-06-02 04:53:11 +000071 case VarDecl::Static:
Anders Carlssonf94cd1f2007-10-17 00:52:43 +000072 return EmitStaticBlockVarDecl(D);
Chris Lattner03df1222007-06-02 04:53:11 +000073 case VarDecl::Extern:
Daniel Dunbar0ca16602009-04-14 02:25:56 +000074 case VarDecl::PrivateExtern:
Lauro Ramos Venanciobada8d42008-02-16 22:30:38 +000075 // Don't emit it now, allow it to be emitted lazily on its first use.
76 return;
Chris Lattner03df1222007-06-02 04:53:11 +000077 }
Daniel Dunbar0ca16602009-04-14 02:25:56 +000078
79 assert(0 && "Unknown storage class");
Chris Lattner03df1222007-06-02 04:53:11 +000080}
81
Daniel Dunbar22a87f92009-02-25 19:24:29 +000082llvm::GlobalVariable *
83CodeGenFunction::CreateStaticBlockVarDecl(const VarDecl &D,
84 const char *Separator,
85 llvm::GlobalValue::LinkageTypes
86 Linkage) {
Chris Lattnerfc4379f2008-04-06 23:10:54 +000087 QualType Ty = D.getType();
Eli Friedmana682d392008-02-15 12:20:59 +000088 assert(Ty->isConstantSizeType() && "VLAs can't be static");
Nate Begemanfaae0812008-04-19 04:17:09 +000089
Anders Carlssonf7e01ff2009-04-02 03:29:47 +000090 std::string Name;
91 if (getContext().getLangOptions().CPlusPlus) {
92 Name = CGM.getMangledName(&D);
93 } else {
94 std::string ContextName;
95 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurFuncDecl))
96 ContextName = CGM.getMangledName(FD);
97 else if (isa<ObjCMethodDecl>(CurFuncDecl))
Daniel Dunbar600dfac2009-07-26 08:32:30 +000098 ContextName = CurFn->getName();
Anders Carlssonf7e01ff2009-04-02 03:29:47 +000099 else
100 assert(0 && "Unknown context for block var decl");
Mike Stump11289f42009-09-09 15:08:12 +0000101
Anders Carlssonf7e01ff2009-04-02 03:29:47 +0000102 Name = ContextName + Separator + D.getNameAsString();
103 }
Nate Begemanfaae0812008-04-19 04:17:09 +0000104
Daniel Dunbar22a87f92009-02-25 19:24:29 +0000105 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
Owen Andersonc10c8d32009-07-08 19:05:04 +0000106 return new llvm::GlobalVariable(CGM.getModule(), LTy,
107 Ty.isConstant(getContext()), Linkage,
Mike Stump11289f42009-09-09 15:08:12 +0000108 CGM.EmitNullConstant(D.getType()), Name, 0,
Anders Carlssonf18318c2009-08-02 21:18:22 +0000109 D.isThreadSpecified(), Ty.getAddressSpace());
Daniel Dunbar22a87f92009-02-25 19:24:29 +0000110}
111
Mike Stump11289f42009-09-09 15:08:12 +0000112void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D) {
Daniel Dunbara374e602009-02-25 19:45:19 +0000113 llvm::Value *&DMEntry = LocalDeclMap[&D];
114 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
Mike Stump11289f42009-09-09 15:08:12 +0000115
116 llvm::GlobalVariable *GV =
Daniel Dunbara374e602009-02-25 19:45:19 +0000117 CreateStaticBlockVarDecl(D, ".", llvm::GlobalValue::InternalLinkage);
118
Daniel Dunbar1cdbc542009-02-25 20:08:33 +0000119 // Store into LocalDeclMap before generating initializer to handle
120 // circular references.
121 DMEntry = GV;
122
Eli Friedmanbc633be2009-04-20 03:54:15 +0000123 // Make sure to evaluate VLA bounds now so that we have them for later.
Daniel Dunbarb6adc432009-07-19 06:58:07 +0000124 //
125 // FIXME: Can this happen?
Eli Friedmanbc633be2009-04-20 03:54:15 +0000126 if (D.getType()->isVariablyModifiedType())
127 EmitVLASize(D.getType());
128
Daniel Dunbara374e602009-02-25 19:45:19 +0000129 if (D.getInit()) {
Anders Carlsson80f97ab2009-04-08 04:48:15 +0000130 llvm::Constant *Init = CGM.EmitConstantExpr(D.getInit(), D.getType(), this);
Daniel Dunbar22a87f92009-02-25 19:24:29 +0000131
132 // If constant emission failed, then this should be a C++ static
133 // initializer.
134 if (!Init) {
135 if (!getContext().getLangOptions().CPlusPlus)
136 CGM.ErrorUnsupported(D.getInit(), "constant l-value expression");
137 else
Anders Carlssonf40886a2009-08-08 21:45:14 +0000138 EmitStaticCXXBlockVarDeclInit(D, GV);
Daniel Dunbar22a87f92009-02-25 19:24:29 +0000139 } else {
140 // The initializer may differ in type from the global. Rewrite
Eli Friedmana1493b32009-03-04 04:22:58 +0000141 // the global to match the initializer. (We have to do this
142 // because some types, like unions, can't be completely represented
143 // in the LLVM type system.)
Daniel Dunbar22a87f92009-02-25 19:24:29 +0000144 if (GV->getType() != Init->getType()) {
145 llvm::GlobalVariable *OldGV = GV;
Mike Stump11289f42009-09-09 15:08:12 +0000146
Owen Andersonc10c8d32009-07-08 19:05:04 +0000147 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
148 OldGV->isConstant(),
Daniel Dunbar22a87f92009-02-25 19:24:29 +0000149 OldGV->getLinkage(), Init, "",
Owen Andersonc10c8d32009-07-08 19:05:04 +0000150 0, D.isThreadSpecified(),
Daniel Dunbar22a87f92009-02-25 19:24:29 +0000151 D.getType().getAddressSpace());
152
153 // Steal the name of the old global
154 GV->takeName(OldGV);
155
156 // Replace all uses of the old global with the new global
Mike Stump11289f42009-09-09 15:08:12 +0000157 llvm::Constant *NewPtrForOldDecl =
Owen Andersonade90fd2009-07-29 18:54:39 +0000158 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
Daniel Dunbar22a87f92009-02-25 19:24:29 +0000159 OldGV->replaceAllUsesWith(NewPtrForOldDecl);
160
161 // Erase the old global, since it is no longer used.
162 OldGV->eraseFromParent();
Mike Stump11289f42009-09-09 15:08:12 +0000163 }
Daniel Dunbar22a87f92009-02-25 19:24:29 +0000164
165 GV->setInitializer(Init);
166 }
167 }
Nate Begemanfaae0812008-04-19 04:17:09 +0000168
Daniel Dunbar53bf7412009-02-12 23:32:54 +0000169 // FIXME: Merge attribute handling.
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000170 if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) {
Nate Begemanfaae0812008-04-19 04:17:09 +0000171 SourceManager &SM = CGM.getContext().getSourceManager();
172 llvm::Constant *Ann =
Mike Stump11289f42009-09-09 15:08:12 +0000173 CGM.EmitAnnotateAttr(GV, AA,
Chris Lattner8a425862009-01-16 07:36:28 +0000174 SM.getInstantiationLineNumber(D.getLocation()));
Nate Begemanfaae0812008-04-19 04:17:09 +0000175 CGM.AddAnnotation(Ann);
176 }
177
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000178 if (const SectionAttr *SA = D.getAttr<SectionAttr>())
Daniel Dunbar53bf7412009-02-12 23:32:54 +0000179 GV->setSection(SA->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000180
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000181 if (D.hasAttr<UsedAttr>())
Daniel Dunbar128a1382009-02-13 22:08:43 +0000182 CGM.AddUsedGlobal(GV);
183
Daniel Dunbar1cdbc542009-02-25 20:08:33 +0000184 // We may have to cast the constant because of the initializer
185 // mismatch above.
186 //
187 // FIXME: It is really dangerous to store this in the map; if anyone
188 // RAUW's the GV uses of this constant will be invalid.
Eli Friedmanc98a7ad2008-06-08 01:23:18 +0000189 const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType());
190 const llvm::Type *LPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000191 llvm::PointerType::get(LTy, D.getType().getAddressSpace());
Owen Andersonade90fd2009-07-29 18:54:39 +0000192 DMEntry = llvm::ConstantExpr::getBitCast(GV, LPtrTy);
Sanjiv Gupta158143a2008-06-05 08:59:10 +0000193
194 // Emit global variable debug descriptor for static vars.
Anders Carlsson63784f42009-02-13 08:11:52 +0000195 CGDebugInfo *DI = getDebugInfo();
Mike Stumpf5a5c4f2009-02-20 00:19:45 +0000196 if (DI) {
Daniel Dunbarb9fd9022008-10-17 16:15:48 +0000197 DI->setLocation(D.getLocation());
Sanjiv Gupta158143a2008-06-05 08:59:10 +0000198 DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D);
199 }
Anders Carlssonf94cd1f2007-10-17 00:52:43 +0000200}
Mike Stump11289f42009-09-09 15:08:12 +0000201
Mike Stump97d01d52009-03-04 03:23:46 +0000202/// BuildByRefType - This routine changes a __block variable declared as T x
203/// into:
204///
205/// struct {
206/// void *__isa;
207/// void *__forwarding;
208/// int32_t __flags;
209/// int32_t __size;
210/// void *__copy_helper;
211/// void *__destroy_helper;
212/// T x;
213/// } x
214///
215/// Align is the alignment needed in bytes for x.
Anders Carlsson71d1d922009-09-09 02:51:03 +0000216const llvm::Type *CodeGenFunction::BuildByRefType(const ValueDecl *D) {
217 QualType Ty = D->getType();
218 uint64_t Align = getContext().getDeclAlignInBytes(D);
Daniel Dunbar277aa362009-09-09 22:32:15 +0000219 (void) Align;
Mike Stump11289f42009-09-09 15:08:12 +0000220
Mike Stump97d01d52009-03-04 03:23:46 +0000221 const llvm::Type *LTy = ConvertType(Ty);
222 bool needsCopyDispose = BlockRequiresCopying(Ty);
223 std::vector<const llvm::Type *> Types(needsCopyDispose*2+5);
224 const llvm::PointerType *PtrToInt8Ty
Owen Anderson41a75022009-08-13 21:57:51 +0000225 = llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Anders Carlsson10f2c102009-09-10 01:32:12 +0000226
227 llvm::PATypeHolder ByRefTypeHolder = llvm::OpaqueType::get(VMContext);
228
Mike Stump97d01d52009-03-04 03:23:46 +0000229 Types[0] = PtrToInt8Ty;
Anders Carlsson10f2c102009-09-10 01:32:12 +0000230 Types[1] = llvm::PointerType::getUnqual(ByRefTypeHolder);
Owen Anderson41a75022009-08-13 21:57:51 +0000231 Types[2] = llvm::Type::getInt32Ty(VMContext);
232 Types[3] = llvm::Type::getInt32Ty(VMContext);
Mike Stump97d01d52009-03-04 03:23:46 +0000233 if (needsCopyDispose) {
234 Types[4] = PtrToInt8Ty;
235 Types[5] = PtrToInt8Ty;
236 }
Mike Stumpcbc2bca2009-06-05 23:26:36 +0000237 // FIXME: Align this on at least an Align boundary, assert if we can't.
238 assert((Align <= unsigned(Target.getPointerAlign(0))/8)
Devang Patel2872df82009-06-26 18:33:42 +0000239 && "Can't align more than pointer yet");
Mike Stump97d01d52009-03-04 03:23:46 +0000240 Types[needsCopyDispose*2 + 4] = LTy;
Anders Carlsson10f2c102009-09-10 01:32:12 +0000241
242 const llvm::Type *T = llvm::StructType::get(VMContext, Types, false);
243
244 cast<llvm::OpaqueType>(ByRefTypeHolder.get())->refineAbstractTypeTo(T);
245 CGM.getModule().addTypeName("struct.__block_byref_" + D->getNameAsString(),
246 ByRefTypeHolder.get());
247
248 return ByRefTypeHolder.get();
Mike Stump97d01d52009-03-04 03:23:46 +0000249}
250
Chris Lattner03df1222007-06-02 04:53:11 +0000251/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
252/// variable declaration with auto, register, or no storage class specifier.
Chris Lattnerb781dc792008-05-08 05:58:21 +0000253/// These turn into simple stack objects, or GlobalValues depending on target.
Steve Naroff08899ff2008-04-15 22:42:06 +0000254void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
Chris Lattnerfc4379f2008-04-06 23:10:54 +0000255 QualType Ty = D.getType();
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000256 bool isByRef = D.hasAttr<BlocksAttr>();
Mike Stump626aecc2009-03-05 01:23:13 +0000257 bool needsDispose = false;
Mike Stumpcbc2bca2009-06-05 23:26:36 +0000258 unsigned Align = 0;
Chris Lattner03df1222007-06-02 04:53:11 +0000259
260 llvm::Value *DeclPtr;
Eli Friedmana682d392008-02-15 12:20:59 +0000261 if (Ty->isConstantSizeType()) {
Chris Lattnerb781dc792008-05-08 05:58:21 +0000262 if (!Target.useGlobalsForAutomaticVariables()) {
263 // A normal fixed sized variable becomes an alloca in the entry block.
Eli Friedman3efa41a2009-03-04 04:25:14 +0000264 const llvm::Type *LTy = ConvertTypeForMem(Ty);
Mike Stumpcbc2bca2009-06-05 23:26:36 +0000265 Align = getContext().getDeclAlignInBytes(&D);
Mike Stump97d01d52009-03-04 03:23:46 +0000266 if (isByRef)
Anders Carlsson71d1d922009-09-09 02:51:03 +0000267 LTy = BuildByRefType(&D);
Chris Lattner47640222009-03-22 00:24:14 +0000268 llvm::AllocaInst *Alloc = CreateTempAlloca(LTy);
269 Alloc->setName(D.getNameAsString().c_str());
Mike Stump11289f42009-09-09 15:08:12 +0000270
Mike Stump97d01d52009-03-04 03:23:46 +0000271 if (isByRef)
Mike Stumpcbc2bca2009-06-05 23:26:36 +0000272 Align = std::max(Align, unsigned(Target.getPointerAlign(0) / 8));
273 Alloc->setAlignment(Align);
Eli Friedman252e5f12008-05-31 21:20:41 +0000274 DeclPtr = Alloc;
Chris Lattnerb781dc792008-05-08 05:58:21 +0000275 } else {
276 // Targets that don't support recursion emit locals as globals.
277 const char *Class =
278 D.getStorageClass() == VarDecl::Register ? ".reg." : ".auto.";
Mike Stump11289f42009-09-09 15:08:12 +0000279 DeclPtr = CreateStaticBlockVarDecl(D, Class,
Daniel Dunbara374e602009-02-25 19:45:19 +0000280 llvm::GlobalValue
281 ::InternalLinkage);
Chris Lattnerb781dc792008-05-08 05:58:21 +0000282 }
Mike Stump11289f42009-09-09 15:08:12 +0000283
Daniel Dunbarb6adc432009-07-19 06:58:07 +0000284 // FIXME: Can this happen?
Anders Carlsson8a01b792008-12-20 20:46:34 +0000285 if (Ty->isVariablyModifiedType())
286 EmitVLASize(Ty);
Chris Lattner03df1222007-06-02 04:53:11 +0000287 } else {
Daniel Dunbarb6adc432009-07-19 06:58:07 +0000288 EnsureInsertPoint();
289
Anders Carlsson15949b32009-02-09 20:41:50 +0000290 if (!DidCallStackSave) {
Anders Carlsson30032882008-12-12 07:38:43 +0000291 // Save the stack.
Owen Anderson170229f2009-07-14 23:10:40 +0000292 const llvm::Type *LTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000293 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Anders Carlsson30032882008-12-12 07:38:43 +0000294 llvm::Value *Stack = CreateTempAlloca(LTy, "saved_stack");
Mike Stump11289f42009-09-09 15:08:12 +0000295
Anders Carlsson30032882008-12-12 07:38:43 +0000296 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
297 llvm::Value *V = Builder.CreateCall(F);
Mike Stump11289f42009-09-09 15:08:12 +0000298
Anders Carlsson30032882008-12-12 07:38:43 +0000299 Builder.CreateStore(V, Stack);
Anders Carlsson15949b32009-02-09 20:41:50 +0000300
301 DidCallStackSave = true;
Mike Stump11289f42009-09-09 15:08:12 +0000302
Anders Carlsson15949b32009-02-09 20:41:50 +0000303 {
304 // Push a cleanup block and restore the stack there.
305 CleanupScope scope(*this);
Mike Stump11289f42009-09-09 15:08:12 +0000306
Anders Carlsson15949b32009-02-09 20:41:50 +0000307 V = Builder.CreateLoad(Stack, "tmp");
308 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
309 Builder.CreateCall(F, V);
310 }
Anders Carlsson30032882008-12-12 07:38:43 +0000311 }
Mike Stump11289f42009-09-09 15:08:12 +0000312
Anders Carlsson30032882008-12-12 07:38:43 +0000313 // Get the element type.
Mike Stump11289f42009-09-09 15:08:12 +0000314 const llvm::Type *LElemTy = ConvertTypeForMem(Ty);
Anders Carlsson30032882008-12-12 07:38:43 +0000315 const llvm::Type *LElemPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000316 llvm::PointerType::get(LElemTy, D.getType().getAddressSpace());
Anders Carlsson30032882008-12-12 07:38:43 +0000317
Anders Carlsson8a01b792008-12-20 20:46:34 +0000318 llvm::Value *VLASize = EmitVLASize(Ty);
Anders Carlsson30032882008-12-12 07:38:43 +0000319
Anders Carlsson31f86492009-02-05 19:43:10 +0000320 // Downcast the VLA size expression
Owen Anderson41a75022009-08-13 21:57:51 +0000321 VLASize = Builder.CreateIntCast(VLASize, llvm::Type::getInt32Ty(VMContext),
322 false, "tmp");
Mike Stump11289f42009-09-09 15:08:12 +0000323
Anders Carlsson30032882008-12-12 07:38:43 +0000324 // Allocate memory for the array.
Owen Anderson41a75022009-08-13 21:57:51 +0000325 llvm::Value *VLA = Builder.CreateAlloca(llvm::Type::getInt8Ty(VMContext),
326 VLASize, "vla");
Eli Friedmanf4084702008-12-20 23:11:59 +0000327 DeclPtr = Builder.CreateBitCast(VLA, LElemPtrTy, "tmp");
Chris Lattner03df1222007-06-02 04:53:11 +0000328 }
Eli Friedmanf4084702008-12-20 23:11:59 +0000329
Chris Lattner03df1222007-06-02 04:53:11 +0000330 llvm::Value *&DMEntry = LocalDeclMap[&D];
331 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
332 DMEntry = DeclPtr;
Sanjiv Gupta18de6242008-05-30 10:30:31 +0000333
334 // Emit debug info for local var declaration.
Anders Carlsson63784f42009-02-13 08:11:52 +0000335 if (CGDebugInfo *DI = getDebugInfo()) {
Daniel Dunbar669521c2009-07-19 07:03:11 +0000336 assert(HaveInsertPoint() && "Unexpected unreachable point!");
Mike Stump11289f42009-09-09 15:08:12 +0000337
Daniel Dunbarb9fd9022008-10-17 16:15:48 +0000338 DI->setLocation(D.getLocation());
Sanjiv Gupta47e29612009-05-22 13:54:25 +0000339 if (Target.useGlobalsForAutomaticVariables()) {
340 DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(DeclPtr), &D);
Mike Stump658fe022009-07-30 22:28:39 +0000341 } else if (isByRef) {
Mike Stump11289f42009-09-09 15:08:12 +0000342 // FIXME: This code is broken and will not emit debug info for the
Anders Carlsson71d1d922009-09-09 02:51:03 +0000343 // variable. The right way to do this would be to tell LLVM that this is a
344 // byref pointer, and what the offset is. Unfortunately, right now it's
345 // not possible unless we create a DIType that corresponds to the byref
Mike Stump11289f42009-09-09 15:08:12 +0000346 // struct.
Anders Carlsson71d1d922009-09-09 02:51:03 +0000347 /*
Mike Stump97d01d52009-03-04 03:23:46 +0000348 llvm::Value *Loc;
349 bool needsCopyDispose = BlockRequiresCopying(Ty);
Mike Stump2c002922009-05-15 00:29:54 +0000350 Loc = Builder.CreateStructGEP(DeclPtr, 1, "forwarding");
351 Loc = Builder.CreateLoad(Loc, false);
352 Loc = Builder.CreateBitCast(Loc, DeclPtr->getType());
353 Loc = Builder.CreateStructGEP(Loc, needsCopyDispose*2+4, "x");
Mike Stump97d01d52009-03-04 03:23:46 +0000354 DI->EmitDeclareOfAutoVariable(&D, Loc, Builder);
Anders Carlsson71d1d922009-09-09 02:51:03 +0000355 */
Mike Stump97d01d52009-03-04 03:23:46 +0000356 } else
357 DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder);
Sanjiv Gupta18de6242008-05-30 10:30:31 +0000358 }
359
Chris Lattner07eb7332007-07-12 00:39:48 +0000360 // If this local has an initializer, emit it now.
Daniel Dunbarb6adc432009-07-19 06:58:07 +0000361 const Expr *Init = D.getInit();
362
363 // If we are at an unreachable point, we don't need to emit the initializer
364 // unless it contains a label.
365 if (!HaveInsertPoint()) {
366 if (!ContainsLabel(Init))
367 Init = 0;
368 else
369 EnsureInsertPoint();
370 }
371
372 if (Init) {
Mike Stump97d01d52009-03-04 03:23:46 +0000373 llvm::Value *Loc = DeclPtr;
374 if (isByRef) {
375 bool needsCopyDispose = BlockRequiresCopying(Ty);
376 Loc = Builder.CreateStructGEP(DeclPtr, needsCopyDispose*2+4, "x");
377 }
Eli Friedman55422ad2009-05-27 05:39:06 +0000378 if (Ty->isReferenceType()) {
Anders Carlsson5b106a72009-08-16 07:36:22 +0000379 RValue RV = EmitReferenceBindingToExpr(Init, Ty, /*IsInitializer=*/true);
380 EmitStoreOfScalar(RV.getScalarVal(), Loc, false, Ty);
Eli Friedman55422ad2009-05-27 05:39:06 +0000381 } else if (!hasAggregateLLVMType(Init->getType())) {
Chris Lattnerb753f662007-08-26 07:29:23 +0000382 llvm::Value *V = EmitScalarExpr(Init);
Mike Stump11289f42009-09-09 15:08:12 +0000383 EmitStoreOfScalar(V, Loc, D.getType().isVolatileQualified(),
Anders Carlsson83709642009-05-19 18:50:41 +0000384 D.getType());
Chris Lattnerf3bc75a2008-04-04 16:54:41 +0000385 } else if (Init->getType()->isAnyComplexType()) {
Mike Stump97d01d52009-03-04 03:23:46 +0000386 EmitComplexExprIntoAddr(Init, Loc, D.getType().isVolatileQualified());
Chris Lattner9de95272007-08-26 05:13:54 +0000387 } else {
Mike Stump97d01d52009-03-04 03:23:46 +0000388 EmitAggExpr(Init, Loc, D.getType().isVolatileQualified());
389 }
390 }
Mike Stump11289f42009-09-09 15:08:12 +0000391
Mike Stump97d01d52009-03-04 03:23:46 +0000392 if (isByRef) {
393 const llvm::PointerType *PtrToInt8Ty
Owen Anderson41a75022009-08-13 21:57:51 +0000394 = llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Mike Stump97d01d52009-03-04 03:23:46 +0000395
Daniel Dunbarb6adc432009-07-19 06:58:07 +0000396 EnsureInsertPoint();
Mike Stump97d01d52009-03-04 03:23:46 +0000397 llvm::Value *isa_field = Builder.CreateStructGEP(DeclPtr, 0);
398 llvm::Value *forwarding_field = Builder.CreateStructGEP(DeclPtr, 1);
399 llvm::Value *flags_field = Builder.CreateStructGEP(DeclPtr, 2);
400 llvm::Value *size_field = Builder.CreateStructGEP(DeclPtr, 3);
401 llvm::Value *V;
402 int flag = 0;
403 int flags = 0;
404
Mike Stump90d8daf2009-03-07 06:04:31 +0000405 needsDispose = true;
Mike Stump4446dcf2009-03-05 08:32:30 +0000406
Mike Stump97d01d52009-03-04 03:23:46 +0000407 if (Ty->isBlockPointerType()) {
408 flag |= BLOCK_FIELD_IS_BLOCK;
409 flags |= BLOCK_HAS_COPY_DISPOSE;
410 } else if (BlockRequiresCopying(Ty)) {
Mike Stump97d01d52009-03-04 03:23:46 +0000411 flag |= BLOCK_FIELD_IS_OBJECT;
Mike Stumpfbe25dd2009-03-06 04:53:30 +0000412 flags |= BLOCK_HAS_COPY_DISPOSE;
Mike Stump97d01d52009-03-04 03:23:46 +0000413 }
Mike Stump90d8daf2009-03-07 06:04:31 +0000414
415 // FIXME: Someone double check this.
416 if (Ty.isObjCGCWeak())
417 flag |= BLOCK_FIELD_IS_WEAK;
Mike Stump97d01d52009-03-04 03:23:46 +0000418
419 int isa = 0;
420 if (flag&BLOCK_FIELD_IS_WEAK)
421 isa = 1;
Owen Anderson41a75022009-08-13 21:57:51 +0000422 V = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), isa);
Mike Stump0c743272009-03-06 01:33:24 +0000423 V = Builder.CreateIntToPtr(V, PtrToInt8Ty, "isa");
Mike Stump97d01d52009-03-04 03:23:46 +0000424 Builder.CreateStore(V, isa_field);
425
Anders Carlsson10f2c102009-09-10 01:32:12 +0000426 Builder.CreateStore(DeclPtr, forwarding_field);
Mike Stump97d01d52009-03-04 03:23:46 +0000427
Owen Anderson41a75022009-08-13 21:57:51 +0000428 V = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), flags);
Mike Stump97d01d52009-03-04 03:23:46 +0000429 Builder.CreateStore(V, flags_field);
430
Mike Stump4446dcf2009-03-05 08:32:30 +0000431 const llvm::Type *V1;
432 V1 = cast<llvm::PointerType>(DeclPtr->getType())->getElementType();
Owen Anderson41a75022009-08-13 21:57:51 +0000433 V = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Mike Stump4446dcf2009-03-05 08:32:30 +0000434 (CGM.getTargetData().getTypeStoreSizeInBits(V1)
435 / 8));
Mike Stump97d01d52009-03-04 03:23:46 +0000436 Builder.CreateStore(V, size_field);
437
438 if (flags & BLOCK_HAS_COPY_DISPOSE) {
Mike Stump4446dcf2009-03-05 08:32:30 +0000439 BlockHasCopyDispose = true;
Mike Stump97d01d52009-03-04 03:23:46 +0000440 llvm::Value *copy_helper = Builder.CreateStructGEP(DeclPtr, 4);
Mike Stumpcbc2bca2009-06-05 23:26:36 +0000441 Builder.CreateStore(BuildbyrefCopyHelper(DeclPtr->getType(), flag, Align),
Mike Stumpf89230d2009-03-06 06:12:24 +0000442 copy_helper);
Mike Stump97d01d52009-03-04 03:23:46 +0000443
Mike Stumpfbe25dd2009-03-06 04:53:30 +0000444 llvm::Value *destroy_helper = Builder.CreateStructGEP(DeclPtr, 5);
Mike Stumpcbc2bca2009-06-05 23:26:36 +0000445 Builder.CreateStore(BuildbyrefDestroyHelper(DeclPtr->getType(), flag,
446 Align),
Mike Stumpfbe25dd2009-03-06 04:53:30 +0000447 destroy_helper);
Chris Lattner9de95272007-08-26 05:13:54 +0000448 }
Chris Lattner2da04b32007-08-24 05:35:26 +0000449 }
Anders Carlssoncadb9a62009-02-07 23:51:38 +0000450
Fariborz Jahanian18c06232009-08-03 20:20:07 +0000451 // Handle CXX destruction of variables.
Fariborz Jahaniand44bdb22009-08-03 20:51:29 +0000452 QualType DtorTy(Ty);
453 if (const ArrayType *Array = DtorTy->getAs<ArrayType>())
454 DtorTy = Array->getElementType();
455 if (const RecordType *RT = DtorTy->getAs<RecordType>())
Fariborz Jahanian18c06232009-08-03 20:20:07 +0000456 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
457 if (!ClassDecl->hasTrivialDestructor()) {
458 const CXXDestructorDecl *D = ClassDecl->getDestructor(getContext());
459 assert(D && "EmitLocalBlockVarDecl - destructor is nul");
Fariborz Jahaniand44bdb22009-08-03 20:51:29 +0000460 assert(!Ty->getAs<ArrayType>() && "FIXME - destruction of arrays NYI");
Mike Stump11289f42009-09-09 15:08:12 +0000461
Fariborz Jahanian18c06232009-08-03 20:20:07 +0000462 CleanupScope scope(*this);
463 EmitCXXDestructorCall(D, Dtor_Complete, DeclPtr);
464 }
465 }
Mike Stump11289f42009-09-09 15:08:12 +0000466
Anders Carlssoncadb9a62009-02-07 23:51:38 +0000467 // Handle the cleanup attribute
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000468 if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) {
Anders Carlssoncadb9a62009-02-07 23:51:38 +0000469 const FunctionDecl *FD = CA->getFunctionDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000470
Chris Lattnere0be0df2009-05-12 21:21:08 +0000471 llvm::Constant* F = CGM.GetAddrOfFunction(GlobalDecl(FD));
Anders Carlssoncadb9a62009-02-07 23:51:38 +0000472 assert(F && "Could not find function!");
Mike Stump11289f42009-09-09 15:08:12 +0000473
Anders Carlssoncadb9a62009-02-07 23:51:38 +0000474 CleanupScope scope(*this);
475
Anders Carlsson6f5c1562009-04-26 00:34:20 +0000476 const CGFunctionInfo &Info = CGM.getTypes().getFunctionInfo(FD);
477
478 // In some cases, the type of the function argument will be different from
479 // the type of the pointer. An example of this is
480 // void f(void* arg);
481 // __attribute__((cleanup(f))) void *g;
Mike Stump11289f42009-09-09 15:08:12 +0000482 //
Anders Carlsson6f5c1562009-04-26 00:34:20 +0000483 // To fix this we insert a bitcast here.
484 QualType ArgTy = Info.arg_begin()->type;
485 DeclPtr = Builder.CreateBitCast(DeclPtr, ConvertType(ArgTy));
Mike Stump11289f42009-09-09 15:08:12 +0000486
Anders Carlssoncadb9a62009-02-07 23:51:38 +0000487 CallArgList Args;
Mike Stump11289f42009-09-09 15:08:12 +0000488 Args.push_back(std::make_pair(RValue::get(DeclPtr),
Anders Carlssoncadb9a62009-02-07 23:51:38 +0000489 getContext().getPointerType(D.getType())));
Mike Stump11289f42009-09-09 15:08:12 +0000490
Anders Carlsson6f5c1562009-04-26 00:34:20 +0000491 EmitCall(Info, F, Args);
Anders Carlssoncadb9a62009-02-07 23:51:38 +0000492 }
Mike Stump626aecc2009-03-05 01:23:13 +0000493
Mike Stumpcd1280b2009-03-05 02:34:38 +0000494 if (needsDispose && CGM.getLangOptions().getGCMode() != LangOptions::GCOnly) {
Mike Stump626aecc2009-03-05 01:23:13 +0000495 CleanupScope scope(*this);
Mike Stumpfbe25dd2009-03-06 04:53:30 +0000496 llvm::Value *V = Builder.CreateStructGEP(DeclPtr, 1, "forwarding");
497 V = Builder.CreateLoad(V, false);
498 BuildBlockRelease(V);
Mike Stump626aecc2009-03-05 01:23:13 +0000499 }
Chris Lattner03df1222007-06-02 04:53:11 +0000500}
Chris Lattner53621a52007-06-13 20:44:40 +0000501
Mike Stump11289f42009-09-09 15:08:12 +0000502/// Emit an alloca (or GlobalValue depending on target)
Chris Lattnerb781dc792008-05-08 05:58:21 +0000503/// for the specified parameter and set up LocalDeclMap.
Daniel Dunbara94ecd22008-08-16 03:19:19 +0000504void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg) {
505 // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
Sanjiv Guptad7959242008-10-31 09:52:39 +0000506 assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
Daniel Dunbara94ecd22008-08-16 03:19:19 +0000507 "Invalid argument to EmitParmDecl");
Chris Lattnerfc4379f2008-04-06 23:10:54 +0000508 QualType Ty = D.getType();
Mike Stump11289f42009-09-09 15:08:12 +0000509
Chris Lattner53621a52007-06-13 20:44:40 +0000510 llvm::Value *DeclPtr;
Eli Friedmana682d392008-02-15 12:20:59 +0000511 if (!Ty->isConstantSizeType()) {
Chris Lattner53621a52007-06-13 20:44:40 +0000512 // Variable sized values always are passed by-reference.
513 DeclPtr = Arg;
Chris Lattner53621a52007-06-13 20:44:40 +0000514 } else {
Dan Gohman5d309752008-05-22 22:12:56 +0000515 // A fixed sized single-value variable becomes an alloca in the entry block.
Eli Friedman3efa41a2009-03-04 04:25:14 +0000516 const llvm::Type *LTy = ConvertTypeForMem(Ty);
Dan Gohman5d309752008-05-22 22:12:56 +0000517 if (LTy->isSingleValueType()) {
Chris Lattner53621a52007-06-13 20:44:40 +0000518 // TODO: Alignment
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000519 std::string Name = D.getNameAsString();
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000520 Name += ".addr";
Chris Lattner47640222009-03-22 00:24:14 +0000521 DeclPtr = CreateTempAlloca(LTy);
522 DeclPtr->setName(Name.c_str());
Mike Stump11289f42009-09-09 15:08:12 +0000523
Chris Lattner53621a52007-06-13 20:44:40 +0000524 // Store the initial value into the alloca.
Anders Carlsson83709642009-05-19 18:50:41 +0000525 EmitStoreOfScalar(Arg, DeclPtr, Ty.isVolatileQualified(), Ty);
Chris Lattner53621a52007-06-13 20:44:40 +0000526 } else {
527 // Otherwise, if this is an aggregate, just use the input pointer.
Chris Lattner0fb84652007-06-22 18:57:44 +0000528 DeclPtr = Arg;
Chris Lattner53621a52007-06-13 20:44:40 +0000529 }
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000530 Arg->setName(D.getNameAsString());
Chris Lattner53621a52007-06-13 20:44:40 +0000531 }
532
533 llvm::Value *&DMEntry = LocalDeclMap[&D];
534 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
535 DMEntry = DeclPtr;
Sanjiv Gupta18de6242008-05-30 10:30:31 +0000536
537 // Emit debug info for param declaration.
Anders Carlsson63784f42009-02-13 08:11:52 +0000538 if (CGDebugInfo *DI = getDebugInfo()) {
Daniel Dunbarb9fd9022008-10-17 16:15:48 +0000539 DI->setLocation(D.getLocation());
Chris Lattneraffb3732008-11-10 06:08:34 +0000540 DI->EmitDeclareOfArgVariable(&D, DeclPtr, Builder);
Sanjiv Gupta18de6242008-05-30 10:30:31 +0000541 }
Chris Lattner53621a52007-06-13 20:44:40 +0000542}
543