blob: 05c2533d6f39126cdb7880b0769e43c3dcc6f748 [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
Anders Carlsson0168f4b2009-09-12 02:14:24 +0000202unsigned CodeGenFunction::getByRefValueLLVMField(const ValueDecl *VD) const {
203 assert(ByRefValueInfo.count(VD) && "Did not find value!");
204
205 return ByRefValueInfo.find(VD)->second.second;
206}
207
Mike Stump97d01d52009-03-04 03:23:46 +0000208/// BuildByRefType - This routine changes a __block variable declared as T x
209/// into:
210///
211/// struct {
212/// void *__isa;
213/// void *__forwarding;
214/// int32_t __flags;
215/// int32_t __size;
216/// void *__copy_helper;
217/// void *__destroy_helper;
218/// T x;
219/// } x
220///
Anders Carlsson71d1d922009-09-09 02:51:03 +0000221const llvm::Type *CodeGenFunction::BuildByRefType(const ValueDecl *D) {
Anders Carlsson0168f4b2009-09-12 02:14:24 +0000222 std::pair<const llvm::Type *, unsigned> &Info = ByRefValueInfo[D];
223 if (Info.first)
224 return Info.first;
225
Anders Carlsson71d1d922009-09-09 02:51:03 +0000226 QualType Ty = D->getType();
Mike Stump11289f42009-09-09 15:08:12 +0000227
Anders Carlssonf8e94f22009-09-12 02:44:18 +0000228 std::vector<const llvm::Type *> Types;
Anders Carlsson10f2c102009-09-10 01:32:12 +0000229
Anders Carlssonf8e94f22009-09-12 02:44:18 +0000230 const llvm::PointerType *Int8PtrTy
231 = llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
232
Anders Carlsson10f2c102009-09-10 01:32:12 +0000233 llvm::PATypeHolder ByRefTypeHolder = llvm::OpaqueType::get(VMContext);
234
Anders Carlssonf8e94f22009-09-12 02:44:18 +0000235 // void *__isa;
236 Types.push_back(Int8PtrTy);
237
238 // void *__forwarding;
239 Types.push_back(llvm::PointerType::getUnqual(ByRefTypeHolder));
240
241 // int32_t __flags;
242 Types.push_back(llvm::Type::getInt32Ty(VMContext));
243
244 // int32_t __size;
245 Types.push_back(llvm::Type::getInt32Ty(VMContext));
246
247 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
248 if (HasCopyAndDispose) {
249 /// void *__copy_helper;
250 Types.push_back(Int8PtrTy);
251
252 /// void *__destroy_helper;
253 Types.push_back(Int8PtrTy);
Mike Stump97d01d52009-03-04 03:23:46 +0000254 }
Anders Carlssonf8e94f22009-09-12 02:44:18 +0000255
256 bool Packed = false;
257 unsigned Align = getContext().getDeclAlignInBytes(D);
258 if (Align > Target.getPointerAlign(0) / 8) {
259 // We have to insert padding.
260
261 // The struct above has 2 32-bit integers.
262 unsigned CurrentOffsetInBytes = 4 * 2;
263
264 // And either 2 or 4 pointers.
265 CurrentOffsetInBytes += (HasCopyAndDispose ? 4 : 2) *
266 CGM.getTargetData().getTypeAllocSize(Int8PtrTy);
267
268 // Align the offset.
269 unsigned AlignedOffsetInBytes =
270 llvm::RoundUpToAlignment(CurrentOffsetInBytes, Align);
271
272 unsigned NumPaddingBytes = AlignedOffsetInBytes - CurrentOffsetInBytes;
273 assert(NumPaddingBytes > 0 && "Can't append any padding!");
274
275 const llvm::Type *Ty = llvm::Type::getInt8Ty(VMContext);
276 if (NumPaddingBytes > 1)
277 Ty = llvm::ArrayType::get(Ty, NumPaddingBytes);
278
279 Types.push_back(Ty);
280
281 // We want a packed struct.
282 Packed = true;
283 }
284
285 // T x;
286 Types.push_back(ConvertType(Ty));
Anders Carlsson0168f4b2009-09-12 02:14:24 +0000287
Anders Carlssonf8e94f22009-09-12 02:44:18 +0000288 const llvm::Type *T = llvm::StructType::get(VMContext, Types, Packed);
Anders Carlsson10f2c102009-09-10 01:32:12 +0000289
290 cast<llvm::OpaqueType>(ByRefTypeHolder.get())->refineAbstractTypeTo(T);
291 CGM.getModule().addTypeName("struct.__block_byref_" + D->getNameAsString(),
292 ByRefTypeHolder.get());
293
Anders Carlsson0168f4b2009-09-12 02:14:24 +0000294 Info.first = ByRefTypeHolder.get();
Anders Carlssonf8e94f22009-09-12 02:44:18 +0000295
296 Info.second = Types.size() - 1;
Anders Carlsson0168f4b2009-09-12 02:14:24 +0000297
298 return Info.first;
Mike Stump97d01d52009-03-04 03:23:46 +0000299}
300
Chris Lattner03df1222007-06-02 04:53:11 +0000301/// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
302/// variable declaration with auto, register, or no storage class specifier.
Chris Lattnerb781dc792008-05-08 05:58:21 +0000303/// These turn into simple stack objects, or GlobalValues depending on target.
Steve Naroff08899ff2008-04-15 22:42:06 +0000304void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
Chris Lattnerfc4379f2008-04-06 23:10:54 +0000305 QualType Ty = D.getType();
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000306 bool isByRef = D.hasAttr<BlocksAttr>();
Mike Stump626aecc2009-03-05 01:23:13 +0000307 bool needsDispose = false;
Mike Stumpcbc2bca2009-06-05 23:26:36 +0000308 unsigned Align = 0;
Chris Lattner03df1222007-06-02 04:53:11 +0000309
310 llvm::Value *DeclPtr;
Eli Friedmana682d392008-02-15 12:20:59 +0000311 if (Ty->isConstantSizeType()) {
Chris Lattnerb781dc792008-05-08 05:58:21 +0000312 if (!Target.useGlobalsForAutomaticVariables()) {
313 // A normal fixed sized variable becomes an alloca in the entry block.
Eli Friedman3efa41a2009-03-04 04:25:14 +0000314 const llvm::Type *LTy = ConvertTypeForMem(Ty);
Mike Stumpcbc2bca2009-06-05 23:26:36 +0000315 Align = getContext().getDeclAlignInBytes(&D);
Mike Stump97d01d52009-03-04 03:23:46 +0000316 if (isByRef)
Anders Carlsson71d1d922009-09-09 02:51:03 +0000317 LTy = BuildByRefType(&D);
Chris Lattner47640222009-03-22 00:24:14 +0000318 llvm::AllocaInst *Alloc = CreateTempAlloca(LTy);
319 Alloc->setName(D.getNameAsString().c_str());
Mike Stump11289f42009-09-09 15:08:12 +0000320
Mike Stump97d01d52009-03-04 03:23:46 +0000321 if (isByRef)
Mike Stumpcbc2bca2009-06-05 23:26:36 +0000322 Align = std::max(Align, unsigned(Target.getPointerAlign(0) / 8));
323 Alloc->setAlignment(Align);
Eli Friedman252e5f12008-05-31 21:20:41 +0000324 DeclPtr = Alloc;
Chris Lattnerb781dc792008-05-08 05:58:21 +0000325 } else {
326 // Targets that don't support recursion emit locals as globals.
327 const char *Class =
328 D.getStorageClass() == VarDecl::Register ? ".reg." : ".auto.";
Mike Stump11289f42009-09-09 15:08:12 +0000329 DeclPtr = CreateStaticBlockVarDecl(D, Class,
Daniel Dunbara374e602009-02-25 19:45:19 +0000330 llvm::GlobalValue
331 ::InternalLinkage);
Chris Lattnerb781dc792008-05-08 05:58:21 +0000332 }
Mike Stump11289f42009-09-09 15:08:12 +0000333
Daniel Dunbarb6adc432009-07-19 06:58:07 +0000334 // FIXME: Can this happen?
Anders Carlsson8a01b792008-12-20 20:46:34 +0000335 if (Ty->isVariablyModifiedType())
336 EmitVLASize(Ty);
Chris Lattner03df1222007-06-02 04:53:11 +0000337 } else {
Daniel Dunbarb6adc432009-07-19 06:58:07 +0000338 EnsureInsertPoint();
339
Anders Carlsson15949b32009-02-09 20:41:50 +0000340 if (!DidCallStackSave) {
Anders Carlsson30032882008-12-12 07:38:43 +0000341 // Save the stack.
Owen Anderson170229f2009-07-14 23:10:40 +0000342 const llvm::Type *LTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000343 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Anders Carlsson30032882008-12-12 07:38:43 +0000344 llvm::Value *Stack = CreateTempAlloca(LTy, "saved_stack");
Mike Stump11289f42009-09-09 15:08:12 +0000345
Anders Carlsson30032882008-12-12 07:38:43 +0000346 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
347 llvm::Value *V = Builder.CreateCall(F);
Mike Stump11289f42009-09-09 15:08:12 +0000348
Anders Carlsson30032882008-12-12 07:38:43 +0000349 Builder.CreateStore(V, Stack);
Anders Carlsson15949b32009-02-09 20:41:50 +0000350
351 DidCallStackSave = true;
Mike Stump11289f42009-09-09 15:08:12 +0000352
Anders Carlsson15949b32009-02-09 20:41:50 +0000353 {
354 // Push a cleanup block and restore the stack there.
355 CleanupScope scope(*this);
Mike Stump11289f42009-09-09 15:08:12 +0000356
Anders Carlsson15949b32009-02-09 20:41:50 +0000357 V = Builder.CreateLoad(Stack, "tmp");
358 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
359 Builder.CreateCall(F, V);
360 }
Anders Carlsson30032882008-12-12 07:38:43 +0000361 }
Mike Stump11289f42009-09-09 15:08:12 +0000362
Anders Carlsson30032882008-12-12 07:38:43 +0000363 // Get the element type.
Mike Stump11289f42009-09-09 15:08:12 +0000364 const llvm::Type *LElemTy = ConvertTypeForMem(Ty);
Anders Carlsson30032882008-12-12 07:38:43 +0000365 const llvm::Type *LElemPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000366 llvm::PointerType::get(LElemTy, D.getType().getAddressSpace());
Anders Carlsson30032882008-12-12 07:38:43 +0000367
Anders Carlsson8a01b792008-12-20 20:46:34 +0000368 llvm::Value *VLASize = EmitVLASize(Ty);
Anders Carlsson30032882008-12-12 07:38:43 +0000369
Anders Carlsson31f86492009-02-05 19:43:10 +0000370 // Downcast the VLA size expression
Owen Anderson41a75022009-08-13 21:57:51 +0000371 VLASize = Builder.CreateIntCast(VLASize, llvm::Type::getInt32Ty(VMContext),
372 false, "tmp");
Mike Stump11289f42009-09-09 15:08:12 +0000373
Anders Carlsson30032882008-12-12 07:38:43 +0000374 // Allocate memory for the array.
Owen Anderson41a75022009-08-13 21:57:51 +0000375 llvm::Value *VLA = Builder.CreateAlloca(llvm::Type::getInt8Ty(VMContext),
376 VLASize, "vla");
Eli Friedmanf4084702008-12-20 23:11:59 +0000377 DeclPtr = Builder.CreateBitCast(VLA, LElemPtrTy, "tmp");
Chris Lattner03df1222007-06-02 04:53:11 +0000378 }
Eli Friedmanf4084702008-12-20 23:11:59 +0000379
Chris Lattner03df1222007-06-02 04:53:11 +0000380 llvm::Value *&DMEntry = LocalDeclMap[&D];
381 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
382 DMEntry = DeclPtr;
Sanjiv Gupta18de6242008-05-30 10:30:31 +0000383
384 // Emit debug info for local var declaration.
Anders Carlsson63784f42009-02-13 08:11:52 +0000385 if (CGDebugInfo *DI = getDebugInfo()) {
Daniel Dunbar669521c2009-07-19 07:03:11 +0000386 assert(HaveInsertPoint() && "Unexpected unreachable point!");
Mike Stump11289f42009-09-09 15:08:12 +0000387
Daniel Dunbarb9fd9022008-10-17 16:15:48 +0000388 DI->setLocation(D.getLocation());
Sanjiv Gupta47e29612009-05-22 13:54:25 +0000389 if (Target.useGlobalsForAutomaticVariables()) {
390 DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(DeclPtr), &D);
Mike Stump658fe022009-07-30 22:28:39 +0000391 } else if (isByRef) {
Mike Stump11289f42009-09-09 15:08:12 +0000392 // FIXME: This code is broken and will not emit debug info for the
Anders Carlsson71d1d922009-09-09 02:51:03 +0000393 // variable. The right way to do this would be to tell LLVM that this is a
394 // byref pointer, and what the offset is. Unfortunately, right now it's
395 // not possible unless we create a DIType that corresponds to the byref
Mike Stump11289f42009-09-09 15:08:12 +0000396 // struct.
Anders Carlsson71d1d922009-09-09 02:51:03 +0000397 /*
Mike Stump97d01d52009-03-04 03:23:46 +0000398 llvm::Value *Loc;
399 bool needsCopyDispose = BlockRequiresCopying(Ty);
Mike Stump2c002922009-05-15 00:29:54 +0000400 Loc = Builder.CreateStructGEP(DeclPtr, 1, "forwarding");
401 Loc = Builder.CreateLoad(Loc, false);
402 Loc = Builder.CreateBitCast(Loc, DeclPtr->getType());
403 Loc = Builder.CreateStructGEP(Loc, needsCopyDispose*2+4, "x");
Mike Stump97d01d52009-03-04 03:23:46 +0000404 DI->EmitDeclareOfAutoVariable(&D, Loc, Builder);
Anders Carlsson71d1d922009-09-09 02:51:03 +0000405 */
Mike Stump97d01d52009-03-04 03:23:46 +0000406 } else
407 DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder);
Sanjiv Gupta18de6242008-05-30 10:30:31 +0000408 }
409
Chris Lattner07eb7332007-07-12 00:39:48 +0000410 // If this local has an initializer, emit it now.
Daniel Dunbarb6adc432009-07-19 06:58:07 +0000411 const Expr *Init = D.getInit();
412
413 // If we are at an unreachable point, we don't need to emit the initializer
414 // unless it contains a label.
415 if (!HaveInsertPoint()) {
416 if (!ContainsLabel(Init))
417 Init = 0;
418 else
419 EnsureInsertPoint();
420 }
421
422 if (Init) {
Mike Stump97d01d52009-03-04 03:23:46 +0000423 llvm::Value *Loc = DeclPtr;
Anders Carlsson0168f4b2009-09-12 02:14:24 +0000424 if (isByRef)
425 Loc = Builder.CreateStructGEP(DeclPtr, getByRefValueLLVMField(&D),
426 D.getNameAsString());
427
Eli Friedman55422ad2009-05-27 05:39:06 +0000428 if (Ty->isReferenceType()) {
Anders Carlsson5b106a72009-08-16 07:36:22 +0000429 RValue RV = EmitReferenceBindingToExpr(Init, Ty, /*IsInitializer=*/true);
430 EmitStoreOfScalar(RV.getScalarVal(), Loc, false, Ty);
Eli Friedman55422ad2009-05-27 05:39:06 +0000431 } else if (!hasAggregateLLVMType(Init->getType())) {
Chris Lattnerb753f662007-08-26 07:29:23 +0000432 llvm::Value *V = EmitScalarExpr(Init);
Mike Stump11289f42009-09-09 15:08:12 +0000433 EmitStoreOfScalar(V, Loc, D.getType().isVolatileQualified(),
Anders Carlsson83709642009-05-19 18:50:41 +0000434 D.getType());
Chris Lattnerf3bc75a2008-04-04 16:54:41 +0000435 } else if (Init->getType()->isAnyComplexType()) {
Mike Stump97d01d52009-03-04 03:23:46 +0000436 EmitComplexExprIntoAddr(Init, Loc, D.getType().isVolatileQualified());
Chris Lattner9de95272007-08-26 05:13:54 +0000437 } else {
Mike Stump97d01d52009-03-04 03:23:46 +0000438 EmitAggExpr(Init, Loc, D.getType().isVolatileQualified());
439 }
440 }
Mike Stump11289f42009-09-09 15:08:12 +0000441
Mike Stump97d01d52009-03-04 03:23:46 +0000442 if (isByRef) {
443 const llvm::PointerType *PtrToInt8Ty
Owen Anderson41a75022009-08-13 21:57:51 +0000444 = llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Mike Stump97d01d52009-03-04 03:23:46 +0000445
Daniel Dunbarb6adc432009-07-19 06:58:07 +0000446 EnsureInsertPoint();
Mike Stump97d01d52009-03-04 03:23:46 +0000447 llvm::Value *isa_field = Builder.CreateStructGEP(DeclPtr, 0);
448 llvm::Value *forwarding_field = Builder.CreateStructGEP(DeclPtr, 1);
449 llvm::Value *flags_field = Builder.CreateStructGEP(DeclPtr, 2);
450 llvm::Value *size_field = Builder.CreateStructGEP(DeclPtr, 3);
451 llvm::Value *V;
452 int flag = 0;
453 int flags = 0;
454
Mike Stump90d8daf2009-03-07 06:04:31 +0000455 needsDispose = true;
Mike Stump4446dcf2009-03-05 08:32:30 +0000456
Mike Stump97d01d52009-03-04 03:23:46 +0000457 if (Ty->isBlockPointerType()) {
458 flag |= BLOCK_FIELD_IS_BLOCK;
459 flags |= BLOCK_HAS_COPY_DISPOSE;
460 } else if (BlockRequiresCopying(Ty)) {
Mike Stump97d01d52009-03-04 03:23:46 +0000461 flag |= BLOCK_FIELD_IS_OBJECT;
Mike Stumpfbe25dd2009-03-06 04:53:30 +0000462 flags |= BLOCK_HAS_COPY_DISPOSE;
Mike Stump97d01d52009-03-04 03:23:46 +0000463 }
Mike Stump90d8daf2009-03-07 06:04:31 +0000464
465 // FIXME: Someone double check this.
466 if (Ty.isObjCGCWeak())
467 flag |= BLOCK_FIELD_IS_WEAK;
Mike Stump97d01d52009-03-04 03:23:46 +0000468
469 int isa = 0;
470 if (flag&BLOCK_FIELD_IS_WEAK)
471 isa = 1;
Owen Anderson41a75022009-08-13 21:57:51 +0000472 V = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), isa);
Mike Stump0c743272009-03-06 01:33:24 +0000473 V = Builder.CreateIntToPtr(V, PtrToInt8Ty, "isa");
Mike Stump97d01d52009-03-04 03:23:46 +0000474 Builder.CreateStore(V, isa_field);
475
Anders Carlsson10f2c102009-09-10 01:32:12 +0000476 Builder.CreateStore(DeclPtr, forwarding_field);
Mike Stump97d01d52009-03-04 03:23:46 +0000477
Owen Anderson41a75022009-08-13 21:57:51 +0000478 V = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), flags);
Mike Stump97d01d52009-03-04 03:23:46 +0000479 Builder.CreateStore(V, flags_field);
480
Mike Stump4446dcf2009-03-05 08:32:30 +0000481 const llvm::Type *V1;
482 V1 = cast<llvm::PointerType>(DeclPtr->getType())->getElementType();
Owen Anderson41a75022009-08-13 21:57:51 +0000483 V = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Mike Stump4446dcf2009-03-05 08:32:30 +0000484 (CGM.getTargetData().getTypeStoreSizeInBits(V1)
485 / 8));
Mike Stump97d01d52009-03-04 03:23:46 +0000486 Builder.CreateStore(V, size_field);
487
488 if (flags & BLOCK_HAS_COPY_DISPOSE) {
Mike Stump4446dcf2009-03-05 08:32:30 +0000489 BlockHasCopyDispose = true;
Mike Stump97d01d52009-03-04 03:23:46 +0000490 llvm::Value *copy_helper = Builder.CreateStructGEP(DeclPtr, 4);
Mike Stumpcbc2bca2009-06-05 23:26:36 +0000491 Builder.CreateStore(BuildbyrefCopyHelper(DeclPtr->getType(), flag, Align),
Mike Stumpf89230d2009-03-06 06:12:24 +0000492 copy_helper);
Mike Stump97d01d52009-03-04 03:23:46 +0000493
Mike Stumpfbe25dd2009-03-06 04:53:30 +0000494 llvm::Value *destroy_helper = Builder.CreateStructGEP(DeclPtr, 5);
Mike Stumpcbc2bca2009-06-05 23:26:36 +0000495 Builder.CreateStore(BuildbyrefDestroyHelper(DeclPtr->getType(), flag,
496 Align),
Mike Stumpfbe25dd2009-03-06 04:53:30 +0000497 destroy_helper);
Chris Lattner9de95272007-08-26 05:13:54 +0000498 }
Chris Lattner2da04b32007-08-24 05:35:26 +0000499 }
Anders Carlssoncadb9a62009-02-07 23:51:38 +0000500
Fariborz Jahanian18c06232009-08-03 20:20:07 +0000501 // Handle CXX destruction of variables.
Fariborz Jahaniand44bdb22009-08-03 20:51:29 +0000502 QualType DtorTy(Ty);
503 if (const ArrayType *Array = DtorTy->getAs<ArrayType>())
504 DtorTy = Array->getElementType();
505 if (const RecordType *RT = DtorTy->getAs<RecordType>())
Fariborz Jahanian18c06232009-08-03 20:20:07 +0000506 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
507 if (!ClassDecl->hasTrivialDestructor()) {
508 const CXXDestructorDecl *D = ClassDecl->getDestructor(getContext());
509 assert(D && "EmitLocalBlockVarDecl - destructor is nul");
Fariborz Jahaniand44bdb22009-08-03 20:51:29 +0000510 assert(!Ty->getAs<ArrayType>() && "FIXME - destruction of arrays NYI");
Mike Stump11289f42009-09-09 15:08:12 +0000511
Fariborz Jahanian18c06232009-08-03 20:20:07 +0000512 CleanupScope scope(*this);
513 EmitCXXDestructorCall(D, Dtor_Complete, DeclPtr);
514 }
515 }
Mike Stump11289f42009-09-09 15:08:12 +0000516
Anders Carlssoncadb9a62009-02-07 23:51:38 +0000517 // Handle the cleanup attribute
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000518 if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) {
Anders Carlssoncadb9a62009-02-07 23:51:38 +0000519 const FunctionDecl *FD = CA->getFunctionDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000520
Anders Carlssonecf9bf02009-09-10 23:43:36 +0000521 llvm::Constant* F = CGM.GetAddrOfFunction(FD);
Anders Carlssoncadb9a62009-02-07 23:51:38 +0000522 assert(F && "Could not find function!");
Mike Stump11289f42009-09-09 15:08:12 +0000523
Anders Carlssoncadb9a62009-02-07 23:51:38 +0000524 CleanupScope scope(*this);
525
Anders Carlsson6f5c1562009-04-26 00:34:20 +0000526 const CGFunctionInfo &Info = CGM.getTypes().getFunctionInfo(FD);
527
528 // In some cases, the type of the function argument will be different from
529 // the type of the pointer. An example of this is
530 // void f(void* arg);
531 // __attribute__((cleanup(f))) void *g;
Mike Stump11289f42009-09-09 15:08:12 +0000532 //
Anders Carlsson6f5c1562009-04-26 00:34:20 +0000533 // To fix this we insert a bitcast here.
534 QualType ArgTy = Info.arg_begin()->type;
535 DeclPtr = Builder.CreateBitCast(DeclPtr, ConvertType(ArgTy));
Mike Stump11289f42009-09-09 15:08:12 +0000536
Anders Carlssoncadb9a62009-02-07 23:51:38 +0000537 CallArgList Args;
Mike Stump11289f42009-09-09 15:08:12 +0000538 Args.push_back(std::make_pair(RValue::get(DeclPtr),
Anders Carlssoncadb9a62009-02-07 23:51:38 +0000539 getContext().getPointerType(D.getType())));
Mike Stump11289f42009-09-09 15:08:12 +0000540
Anders Carlsson6f5c1562009-04-26 00:34:20 +0000541 EmitCall(Info, F, Args);
Anders Carlssoncadb9a62009-02-07 23:51:38 +0000542 }
Mike Stump626aecc2009-03-05 01:23:13 +0000543
Mike Stumpcd1280b2009-03-05 02:34:38 +0000544 if (needsDispose && CGM.getLangOptions().getGCMode() != LangOptions::GCOnly) {
Mike Stump626aecc2009-03-05 01:23:13 +0000545 CleanupScope scope(*this);
Mike Stumpfbe25dd2009-03-06 04:53:30 +0000546 llvm::Value *V = Builder.CreateStructGEP(DeclPtr, 1, "forwarding");
547 V = Builder.CreateLoad(V, false);
548 BuildBlockRelease(V);
Mike Stump626aecc2009-03-05 01:23:13 +0000549 }
Chris Lattner03df1222007-06-02 04:53:11 +0000550}
Chris Lattner53621a52007-06-13 20:44:40 +0000551
Mike Stump11289f42009-09-09 15:08:12 +0000552/// Emit an alloca (or GlobalValue depending on target)
Chris Lattnerb781dc792008-05-08 05:58:21 +0000553/// for the specified parameter and set up LocalDeclMap.
Daniel Dunbara94ecd22008-08-16 03:19:19 +0000554void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg) {
555 // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
Sanjiv Guptad7959242008-10-31 09:52:39 +0000556 assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
Daniel Dunbara94ecd22008-08-16 03:19:19 +0000557 "Invalid argument to EmitParmDecl");
Chris Lattnerfc4379f2008-04-06 23:10:54 +0000558 QualType Ty = D.getType();
Mike Stump11289f42009-09-09 15:08:12 +0000559
Chris Lattner53621a52007-06-13 20:44:40 +0000560 llvm::Value *DeclPtr;
Eli Friedmana682d392008-02-15 12:20:59 +0000561 if (!Ty->isConstantSizeType()) {
Chris Lattner53621a52007-06-13 20:44:40 +0000562 // Variable sized values always are passed by-reference.
563 DeclPtr = Arg;
Chris Lattner53621a52007-06-13 20:44:40 +0000564 } else {
Dan Gohman5d309752008-05-22 22:12:56 +0000565 // A fixed sized single-value variable becomes an alloca in the entry block.
Eli Friedman3efa41a2009-03-04 04:25:14 +0000566 const llvm::Type *LTy = ConvertTypeForMem(Ty);
Dan Gohman5d309752008-05-22 22:12:56 +0000567 if (LTy->isSingleValueType()) {
Chris Lattner53621a52007-06-13 20:44:40 +0000568 // TODO: Alignment
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000569 std::string Name = D.getNameAsString();
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000570 Name += ".addr";
Chris Lattner47640222009-03-22 00:24:14 +0000571 DeclPtr = CreateTempAlloca(LTy);
572 DeclPtr->setName(Name.c_str());
Mike Stump11289f42009-09-09 15:08:12 +0000573
Chris Lattner53621a52007-06-13 20:44:40 +0000574 // Store the initial value into the alloca.
Anders Carlsson83709642009-05-19 18:50:41 +0000575 EmitStoreOfScalar(Arg, DeclPtr, Ty.isVolatileQualified(), Ty);
Chris Lattner53621a52007-06-13 20:44:40 +0000576 } else {
577 // Otherwise, if this is an aggregate, just use the input pointer.
Chris Lattner0fb84652007-06-22 18:57:44 +0000578 DeclPtr = Arg;
Chris Lattner53621a52007-06-13 20:44:40 +0000579 }
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000580 Arg->setName(D.getNameAsString());
Chris Lattner53621a52007-06-13 20:44:40 +0000581 }
582
583 llvm::Value *&DMEntry = LocalDeclMap[&D];
584 assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
585 DMEntry = DeclPtr;
Sanjiv Gupta18de6242008-05-30 10:30:31 +0000586
587 // Emit debug info for param declaration.
Anders Carlsson63784f42009-02-13 08:11:52 +0000588 if (CGDebugInfo *DI = getDebugInfo()) {
Daniel Dunbarb9fd9022008-10-17 16:15:48 +0000589 DI->setLocation(D.getLocation());
Chris Lattneraffb3732008-11-10 06:08:34 +0000590 DI->EmitDeclareOfArgVariable(&D, DeclPtr, Builder);
Sanjiv Gupta18de6242008-05-30 10:30:31 +0000591 }
Chris Lattner53621a52007-06-13 20:44:40 +0000592}
593