blob: 8ab4a44f40b7e64887ff0ec26da7d4597cabeb5c [file] [log] [blame]
Anders Carlssonacfde802009-02-12 00:39:25 +00001//===--- CGBlocks.cpp - Emit LLVM Code for declarations -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit blocks.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
Mike Stump6cc88f72009-03-20 21:53:12 +000016#include "clang/AST/DeclObjC.h"
Anders Carlssonacfde802009-02-12 00:39:25 +000017#include "llvm/Module.h"
Anders Carlssond5cab542009-02-12 17:55:02 +000018#include "llvm/Target/TargetData.h"
Anders Carlssonacfde802009-02-12 00:39:25 +000019
20#include <algorithm>
21
22using namespace clang;
23using namespace CodeGen;
24
Mike Stump58919e12009-03-04 13:17:22 +000025// Temporary code to enable testing of __block variables
26// #include "clang/Frontend/CompileOptions.h"
27#include "llvm/Support/CommandLine.h"
Mike Stump58919e12009-03-04 13:17:22 +000028
Mike Stumpcf62d392009-03-06 18:42:23 +000029llvm::Constant *CodeGenFunction::
Mike Stumpa803b0e2009-03-25 17:58:24 +000030BuildDescriptorBlockDecl(bool BlockHasCopyDispose, uint64_t Size,
31 const llvm::StructType* Ty,
Mike Stump08920992009-03-07 02:35:30 +000032 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump56129b12009-02-13 16:55:51 +000033 const llvm::Type *UnsignedLongTy
34 = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpe5fee252009-02-13 16:19:19 +000035 llvm::Constant *C;
36 std::vector<llvm::Constant*> Elts;
37
38 // reserved
Mike Stump56129b12009-02-13 16:55:51 +000039 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpe5fee252009-02-13 16:19:19 +000040 Elts.push_back(C);
41
42 // Size
Mike Stumpd6840002009-02-21 20:07:44 +000043 // FIXME: What is the right way to say this doesn't fit? We should give
44 // a user diagnostic in that case. Better fix would be to change the
45 // API to size_t.
Mike Stump4e7a1f72009-02-21 20:00:35 +000046 C = llvm::ConstantInt::get(UnsignedLongTy, Size);
Mike Stumpe5fee252009-02-13 16:19:19 +000047 Elts.push_back(C);
48
49 if (BlockHasCopyDispose) {
50 // copy_func_helper_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000051 Elts.push_back(BuildCopyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000052
53 // destroy_func_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000054 Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000055 }
56
57 C = llvm::ConstantStruct::get(Elts);
58
Mike Stumpe5fee252009-02-13 16:19:19 +000059 C = new llvm::GlobalVariable(C->getType(), true,
60 llvm::GlobalValue::InternalLinkage,
Mike Stump7d6dc4f2009-02-13 20:17:16 +000061 C, "__block_descriptor_tmp", &CGM.getModule());
Mike Stumpe5fee252009-02-13 16:19:19 +000062 return C;
63}
64
Mike Stump2a998142009-03-04 18:17:45 +000065llvm::Constant *BlockModule::getNSConcreteGlobalBlock() {
Mike Stumpf99f1d02009-02-13 17:23:42 +000066 if (NSConcreteGlobalBlock)
67 return NSConcreteGlobalBlock;
68
Mike Stumpf7448952009-02-13 19:38:12 +000069 // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
Mike Stump58a85142009-03-04 22:48:06 +000070 // same thing as CreateRuntimeFunction if there's already a variable with the
71 // same name.
Mike Stumpf99f1d02009-02-13 17:23:42 +000072 NSConcreteGlobalBlock
Mike Stump00470a12009-03-05 08:32:30 +000073 = new llvm::GlobalVariable(PtrToInt8Ty, false,
Mike Stumpf99f1d02009-02-13 17:23:42 +000074 llvm::GlobalValue::ExternalLinkage,
75 0, "_NSConcreteGlobalBlock",
76 &getModule());
77
78 return NSConcreteGlobalBlock;
79}
80
Mike Stump2a998142009-03-04 18:17:45 +000081llvm::Constant *BlockModule::getNSConcreteStackBlock() {
Mike Stump59c5b112009-02-13 19:29:27 +000082 if (NSConcreteStackBlock)
83 return NSConcreteStackBlock;
84
Mike Stumpf7448952009-02-13 19:38:12 +000085 // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
Mike Stump58a85142009-03-04 22:48:06 +000086 // same thing as CreateRuntimeFunction if there's already a variable with the
87 // same name.
Mike Stump59c5b112009-02-13 19:29:27 +000088 NSConcreteStackBlock
Mike Stump00470a12009-03-05 08:32:30 +000089 = new llvm::GlobalVariable(PtrToInt8Ty, false,
Mike Stump59c5b112009-02-13 19:29:27 +000090 llvm::GlobalValue::ExternalLinkage,
91 0, "_NSConcreteStackBlock",
92 &getModule());
93
94 return NSConcreteStackBlock;
95}
96
Mike Stump00470a12009-03-05 08:32:30 +000097static void CollectBlockDeclRefInfo(const Stmt *S,
Anders Carlsson4de9fce2009-03-01 01:09:12 +000098 CodeGenFunction::BlockInfo &Info) {
99 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
100 I != E; ++I)
Daniel Dunbar82573ee2009-03-02 07:00:57 +0000101 if (*I)
102 CollectBlockDeclRefInfo(*I, Info);
Mike Stump00470a12009-03-05 08:32:30 +0000103
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000104 if (const BlockDeclRefExpr *DE = dyn_cast<BlockDeclRefExpr>(S)) {
105 // FIXME: Handle enums.
106 if (isa<FunctionDecl>(DE->getDecl()))
107 return;
Mike Stump00470a12009-03-05 08:32:30 +0000108
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000109 if (DE->isByRef())
110 Info.ByRefDeclRefs.push_back(DE);
111 else
112 Info.ByCopyDeclRefs.push_back(DE);
113 }
114}
115
Mike Stump58a85142009-03-04 22:48:06 +0000116/// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be
117/// declared as a global variable instead of on the stack.
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000118static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info)
119{
120 return Info.ByRefDeclRefs.empty() && Info.ByCopyDeclRefs.empty();
121}
122
Mike Stump58a85142009-03-04 22:48:06 +0000123// FIXME: Push most into CGM, passing down a few bits, like current function
124// name.
Mike Stump8a2b4b12009-02-25 23:33:13 +0000125llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Mike Stumpe5fee252009-02-13 16:19:19 +0000126
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000127 std::string Name = CurFn->getName();
128 CodeGenFunction::BlockInfo Info(0, Name.c_str());
129 CollectBlockDeclRefInfo(BE->getBody(), Info);
130
131 // Check if the block can be global.
Mike Stump58a85142009-03-04 22:48:06 +0000132 // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like
133 // to just have one code path. We should move this function into CGM and pass
134 // CGF, then we can just check to see if CGF is 0.
Mike Stumpdab514f2009-03-04 03:23:46 +0000135 if (0 && CanBlockBeGlobal(Info))
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000136 return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
Mike Stump00470a12009-03-05 08:32:30 +0000137
138 std::vector<llvm::Constant*> Elts(5);
Mike Stumpe5fee252009-02-13 16:19:19 +0000139 llvm::Constant *C;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000140 llvm::Value *V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000141
Mike Stumpe5fee252009-02-13 16:19:19 +0000142 {
143 // C = BuildBlockStructInitlist();
144 unsigned int flags = BLOCK_HAS_DESCRIPTOR;
145
Mike Stump00470a12009-03-05 08:32:30 +0000146 // We run this first so that we set BlockHasCopyDispose from the entire
147 // block literal.
148 // __invoke
149 uint64_t subBlockSize, subBlockAlign;
150 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Mike Stumpa803b0e2009-03-25 17:58:24 +0000151 bool subBlockHasCopyDispose = false;
Mike Stump00470a12009-03-05 08:32:30 +0000152 llvm::Function *Fn
Mike Stump6cc88f72009-03-20 21:53:12 +0000153 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl, LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000154 subBlockSize,
Mike Stump00470a12009-03-05 08:32:30 +0000155 subBlockAlign,
156 subBlockDeclRefDecls,
Mike Stumpa803b0e2009-03-25 17:58:24 +0000157 subBlockHasCopyDispose);
158 BlockHasCopyDispose |= subBlockHasCopyDispose;
Mike Stump00470a12009-03-05 08:32:30 +0000159 Elts[3] = Fn;
160
Mike Stumpa803b0e2009-03-25 17:58:24 +0000161 if (subBlockHasCopyDispose)
Mike Stumpe5fee252009-02-13 16:19:19 +0000162 flags |= BLOCK_HAS_COPY_DISPOSE;
163
Mike Stump7d6dc4f2009-02-13 20:17:16 +0000164 // __isa
Mike Stump59c5b112009-02-13 19:29:27 +0000165 C = CGM.getNSConcreteStackBlock();
Mike Stumpe5fee252009-02-13 16:19:19 +0000166 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
Mike Stump00470a12009-03-05 08:32:30 +0000167 Elts[0] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000168
169 // __flags
170 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
171 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
172 C = llvm::ConstantInt::get(IntTy, flags);
Mike Stump00470a12009-03-05 08:32:30 +0000173 Elts[1] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000174
175 // __reserved
176 C = llvm::ConstantInt::get(IntTy, 0);
Mike Stump00470a12009-03-05 08:32:30 +0000177 Elts[2] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000178
Mike Stump8a2b4b12009-02-25 23:33:13 +0000179 if (subBlockDeclRefDecls.size() == 0) {
Mike Stumpcf62d392009-03-06 18:42:23 +0000180 // __descriptor
Mike Stumpa803b0e2009-03-25 17:58:24 +0000181 Elts[4] = BuildDescriptorBlockDecl(subBlockHasCopyDispose, subBlockSize, 0, 0);
Mike Stumpcf62d392009-03-06 18:42:23 +0000182
Mike Stump5570cfe2009-03-01 20:07:53 +0000183 // Optimize to being a global block.
184 Elts[0] = CGM.getNSConcreteGlobalBlock();
185 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
186
Mike Stump8a2b4b12009-02-25 23:33:13 +0000187 C = llvm::ConstantStruct::get(Elts);
188
189 char Name[32];
190 sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
191 C = new llvm::GlobalVariable(C->getType(), true,
192 llvm::GlobalValue::InternalLinkage,
193 C, Name, &CGM.getModule());
194 QualType BPT = BE->getType();
195 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
196 return C;
197 }
Mike Stump00470a12009-03-05 08:32:30 +0000198
Mike Stump8a2b4b12009-02-25 23:33:13 +0000199 std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size());
Mike Stump08920992009-03-07 02:35:30 +0000200 for (int i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000201 Types[i] = Elts[i]->getType();
Mike Stump08920992009-03-07 02:35:30 +0000202 Types[4] = PtrToInt8Ty;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000203
Mike Stumpa99038c2009-02-28 09:07:16 +0000204 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
205 const Expr *E = subBlockDeclRefDecls[i];
206 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
207 QualType Ty = E->getType();
Mike Stumpdab514f2009-03-04 03:23:46 +0000208 if (BDRE && BDRE->isByRef()) {
209 uint64_t Align = getContext().getDeclAlignInBytes(BDRE->getDecl());
210 Types[i+5] = llvm::PointerType::get(BuildByRefType(Ty, Align), 0);
211 } else
212 Types[i+5] = ConvertType(Ty);
Mike Stumpa99038c2009-02-28 09:07:16 +0000213 }
Mike Stump8a2b4b12009-02-25 23:33:13 +0000214
Mike Stump08920992009-03-07 02:35:30 +0000215 llvm::StructType *Ty = llvm::StructType::get(Types, true);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000216
217 llvm::AllocaInst *A = CreateTempAlloca(Ty);
218 A->setAlignment(subBlockAlign);
219 V = A;
220
Mike Stump08920992009-03-07 02:35:30 +0000221 std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size());
222 int helpersize = 0;
223
224 for (unsigned i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000225 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
Mike Stump00470a12009-03-05 08:32:30 +0000226
Mike Stump8a2b4b12009-02-25 23:33:13 +0000227 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
228 {
Mike Stumpa99038c2009-02-28 09:07:16 +0000229 // FIXME: Push const down.
230 Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
231 DeclRefExpr *DR;
232 ValueDecl *VD;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000233
Mike Stumpa99038c2009-02-28 09:07:16 +0000234 DR = dyn_cast<DeclRefExpr>(E);
235 // Skip padding.
236 if (DR) continue;
237
238 BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
239 VD = BDRE->getDecl();
240
Mike Stumpdab514f2009-03-04 03:23:46 +0000241 llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp");
Mike Stump08920992009-03-07 02:35:30 +0000242 NoteForHelper[helpersize].index = i+5;
243 NoteForHelper[helpersize].RequiresCopying = BlockRequiresCopying(VD->getType());
244 NoteForHelper[helpersize].flag
245 = VD->getType()->isBlockPointerType() ? BLOCK_FIELD_IS_BLOCK : BLOCK_FIELD_IS_OBJECT;
246
Mike Stumpa99038c2009-02-28 09:07:16 +0000247 if (LocalDeclMap[VD]) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000248 if (BDRE->isByRef()) {
Mike Stump08920992009-03-07 02:35:30 +0000249 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
Mike Stumpf4bc3122009-03-07 06:04:31 +0000250 // FIXME: Someone double check this.
251 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000252 const llvm::Type *Ty = Types[i+5];
253 llvm::Value *Loc = LocalDeclMap[VD];
254 Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
255 Loc = Builder.CreateLoad(Loc, false);
256 Loc = Builder.CreateBitCast(Loc, Ty);
257 Builder.CreateStore(Loc, Addr);
Mike Stump08920992009-03-07 02:35:30 +0000258 ++helpersize;
Mike Stumpdab514f2009-03-04 03:23:46 +0000259 continue;
260 } else
261 E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD),
262 VD->getType(), SourceLocation(),
263 false, false);
Mike Stumpa99038c2009-02-28 09:07:16 +0000264 }
Mike Stumpdab514f2009-03-04 03:23:46 +0000265 if (BDRE->isByRef()) {
Mike Stumpa8b60c92009-03-21 21:00:35 +0000266 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
267 // FIXME: Someone double check this.
268 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpa99038c2009-02-28 09:07:16 +0000269 E = new (getContext())
270 UnaryOperator(E, UnaryOperator::AddrOf,
271 getContext().getPointerType(E->getType()),
272 SourceLocation());
Mike Stumpdab514f2009-03-04 03:23:46 +0000273 }
Mike Stump08920992009-03-07 02:35:30 +0000274 ++helpersize;
Mike Stumpa99038c2009-02-28 09:07:16 +0000275
Mike Stumpa99038c2009-02-28 09:07:16 +0000276 RValue r = EmitAnyExpr(E, Addr, false);
Mike Stumpdab514f2009-03-04 03:23:46 +0000277 if (r.isScalar()) {
278 llvm::Value *Loc = r.getScalarVal();
279 const llvm::Type *Ty = Types[i+5];
280 if (BDRE->isByRef()) {
Mike Stump58a85142009-03-04 22:48:06 +0000281 // E is now the address of the value field, instead, we want the
282 // address of the actual ByRef struct. We optimize this slightly
283 // compared to gcc by not grabbing the forwarding slot as this must
284 // be done during Block_copy for us, and we can postpone the work
285 // until then.
286 uint64_t offset = BlockDecls[BDRE->getDecl()];
Mike Stump00470a12009-03-05 08:32:30 +0000287
Mike Stump58a85142009-03-04 22:48:06 +0000288 llvm::Value *BlockLiteral = LoadBlockStruct();
Mike Stump00470a12009-03-05 08:32:30 +0000289
Mike Stump58a85142009-03-04 22:48:06 +0000290 Loc = Builder.CreateGEP(BlockLiteral,
291 llvm::ConstantInt::get(llvm::Type::Int64Ty,
292 offset),
293 "block.literal");
294 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000295 Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stump58a85142009-03-04 22:48:06 +0000296 Loc = Builder.CreateLoad(Loc, false);
297 // Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000298 }
299 Builder.CreateStore(Loc, Addr);
300 } else if (r.isComplex())
Mike Stump8a2b4b12009-02-25 23:33:13 +0000301 // FIXME: implement
302 ErrorUnsupported(BE, "complex in block literal");
303 else if (r.isAggregate())
304 ; // Already created into the destination
305 else
306 assert (0 && "bad block variable");
307 // FIXME: Ensure that the offset created by the backend for
308 // the struct matches the previously computed offset in BlockDecls.
309 }
Mike Stump08920992009-03-07 02:35:30 +0000310 NoteForHelper.resize(helpersize);
Mike Stumpcf62d392009-03-06 18:42:23 +0000311
312 // __descriptor
Mike Stumpa803b0e2009-03-25 17:58:24 +0000313 llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockHasCopyDispose,
314 subBlockSize, Ty,
Mike Stump08920992009-03-07 02:35:30 +0000315 &NoteForHelper);
316 Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
317 Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
Mike Stumpe5fee252009-02-13 16:19:19 +0000318 }
Mike Stump00470a12009-03-05 08:32:30 +0000319
Mike Stumpbd65cac2009-02-19 01:01:04 +0000320 QualType BPT = BE->getType();
Mike Stump8a2b4b12009-02-25 23:33:13 +0000321 return Builder.CreateBitCast(V, ConvertType(BPT));
Mike Stumpe5fee252009-02-13 16:19:19 +0000322}
323
324
Mike Stump2a998142009-03-04 18:17:45 +0000325const llvm::Type *BlockModule::getBlockDescriptorType() {
Mike Stumpab695142009-02-13 15:16:56 +0000326 if (BlockDescriptorType)
327 return BlockDescriptorType;
328
Mike Stumpa5448542009-02-13 15:32:32 +0000329 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000330 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000331
Mike Stumpab695142009-02-13 15:16:56 +0000332 // struct __block_descriptor {
333 // unsigned long reserved;
334 // unsigned long block_size;
335 // };
Mike Stumpa5448542009-02-13 15:32:32 +0000336 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy,
337 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000338 NULL);
339
340 getModule().addTypeName("struct.__block_descriptor",
341 BlockDescriptorType);
342
343 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000344}
345
Mike Stump2a998142009-03-04 18:17:45 +0000346const llvm::Type *BlockModule::getGenericBlockLiteralType() {
Mike Stump9b8a7972009-02-13 15:25:34 +0000347 if (GenericBlockLiteralType)
348 return GenericBlockLiteralType;
349
Mike Stumpa5448542009-02-13 15:32:32 +0000350 const llvm::Type *BlockDescPtrTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000351 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000352
Mike Stump7cbb3602009-02-13 16:01:35 +0000353 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
354 getTypes().ConvertType(getContext().IntTy));
355
Mike Stump9b8a7972009-02-13 15:25:34 +0000356 // struct __block_literal_generic {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000357 // void *__isa;
358 // int __flags;
359 // int __reserved;
360 // void (*__invoke)(void *);
361 // struct __block_descriptor *__descriptor;
Mike Stump9b8a7972009-02-13 15:25:34 +0000362 // };
Mike Stump797b6322009-03-05 01:23:13 +0000363 GenericBlockLiteralType = llvm::StructType::get(PtrToInt8Ty,
Mike Stump7cbb3602009-02-13 16:01:35 +0000364 IntTy,
365 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000366 PtrToInt8Ty,
Mike Stump9b8a7972009-02-13 15:25:34 +0000367 BlockDescPtrTy,
368 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000369
Mike Stump9b8a7972009-02-13 15:25:34 +0000370 getModule().addTypeName("struct.__block_literal_generic",
371 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000372
Mike Stump9b8a7972009-02-13 15:25:34 +0000373 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000374}
375
Mike Stump2a998142009-03-04 18:17:45 +0000376const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000377 if (GenericExtendedBlockLiteralType)
378 return GenericExtendedBlockLiteralType;
379
Mike Stumpbd65cac2009-02-19 01:01:04 +0000380 const llvm::Type *BlockDescPtrTy =
381 llvm::PointerType::getUnqual(getBlockDescriptorType());
382
383 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
384 getTypes().ConvertType(getContext().IntTy));
385
386 // struct __block_literal_generic {
387 // void *__isa;
388 // int __flags;
389 // int __reserved;
390 // void (*__invoke)(void *);
391 // struct __block_descriptor *__descriptor;
392 // void *__copy_func_helper_decl;
393 // void *__destroy_func_decl;
394 // };
Mike Stump797b6322009-03-05 01:23:13 +0000395 GenericExtendedBlockLiteralType = llvm::StructType::get(PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000396 IntTy,
397 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000398 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000399 BlockDescPtrTy,
Mike Stump797b6322009-03-05 01:23:13 +0000400 PtrToInt8Ty,
401 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000402 NULL);
403
404 getModule().addTypeName("struct.__block_literal_extended_generic",
405 GenericExtendedBlockLiteralType);
406
407 return GenericExtendedBlockLiteralType;
408}
409
Anders Carlssond5cab542009-02-12 17:55:02 +0000410RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpa5448542009-02-13 15:32:32 +0000411 const BlockPointerType *BPT =
Anders Carlssonacfde802009-02-12 00:39:25 +0000412 E->getCallee()->getType()->getAsBlockPointerType();
Mike Stumpa5448542009-02-13 15:32:32 +0000413
Anders Carlssonacfde802009-02-12 00:39:25 +0000414 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
415
416 // Get a pointer to the generic block literal.
417 const llvm::Type *BlockLiteralTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000418 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000419
420 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000421 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000422 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
423
424 // Get the function pointer from the literal.
425 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Anders Carlssonacfde802009-02-12 00:39:25 +0000426
Mike Stumpa5448542009-02-13 15:32:32 +0000427 BlockLiteral =
428 Builder.CreateBitCast(BlockLiteral,
Anders Carlssonacfde802009-02-12 00:39:25 +0000429 llvm::PointerType::getUnqual(llvm::Type::Int8Ty),
430 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000431
Anders Carlssonacfde802009-02-12 00:39:25 +0000432 // Add the block literal.
433 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
434 CallArgList Args;
435 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000436
Anders Carlsson782f3972009-04-08 23:13:16 +0000437 QualType FnType = BPT->getPointeeType();
438
Anders Carlssonacfde802009-02-12 00:39:25 +0000439 // And the rest of the arguments.
Anders Carlsson782f3972009-04-08 23:13:16 +0000440 EmitCallArgs(Args, FnType->getAsFunctionProtoType(),
441 E->arg_begin(), E->arg_end());
Mike Stumpa5448542009-02-13 15:32:32 +0000442
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000443 // Load the function.
444 llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp");
445
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000446 QualType ResultType = FnType->getAsFunctionType()->getResultType();
447
448 const CGFunctionInfo &FnInfo =
449 CGM.getTypes().getFunctionInfo(ResultType, Args);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000450
451 // Cast the function pointer to the right type.
452 const llvm::Type *BlockFTy =
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000453 CGM.getTypes().GetFunctionType(FnInfo, false);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000454
455 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
456 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
457
Anders Carlssonacfde802009-02-12 00:39:25 +0000458 // And call the block.
Anders Carlssonf8544a42009-04-07 00:20:24 +0000459 return EmitCall(FnInfo, Func, Args);
Anders Carlssonacfde802009-02-12 00:39:25 +0000460}
Anders Carlssond5cab542009-02-12 17:55:02 +0000461
Mike Stumpdab514f2009-03-04 03:23:46 +0000462llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
463 uint64_t &offset = BlockDecls[E->getDecl()];
464
465 const llvm::Type *Ty;
466 Ty = CGM.getTypes().ConvertType(E->getDecl()->getType());
467
Mike Stumpdab514f2009-03-04 03:23:46 +0000468 // See if we have already allocated an offset for this variable.
469 if (offset == 0) {
Mike Stump00470a12009-03-05 08:32:30 +0000470 // Don't run the expensive check, unless we have to.
471 if (!BlockHasCopyDispose && BlockRequiresCopying(E->getType()))
472 BlockHasCopyDispose = true;
Mike Stumpdab514f2009-03-04 03:23:46 +0000473 // if not, allocate one now.
474 offset = getBlockOffset(E);
475 }
476
477 llvm::Value *BlockLiteral = LoadBlockStruct();
478 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
479 llvm::ConstantInt::get(llvm::Type::Int64Ty,
480 offset),
Mike Stump58a85142009-03-04 22:48:06 +0000481 "block.literal");
Mike Stumpdab514f2009-03-04 03:23:46 +0000482 if (E->isByRef()) {
483 bool needsCopyDispose = BlockRequiresCopying(E->getType());
484 uint64_t Align = getContext().getDeclAlignInBytes(E->getDecl());
485 const llvm::Type *PtrStructTy
486 = llvm::PointerType::get(BuildByRefType(E->getType(), Align), 0);
Mike Stumpa8b60c92009-03-21 21:00:35 +0000487 // The block literal will need a copy/destroy helper.
488 BlockHasCopyDispose = true;
Mike Stumpdab514f2009-03-04 03:23:46 +0000489 Ty = PtrStructTy;
490 Ty = llvm::PointerType::get(Ty, 0);
491 V = Builder.CreateBitCast(V, Ty);
492 V = Builder.CreateLoad(V, false);
493 V = Builder.CreateStructGEP(V, 1, "forwarding");
494 V = Builder.CreateLoad(V, false);
495 V = Builder.CreateBitCast(V, PtrStructTy);
496 V = Builder.CreateStructGEP(V, needsCopyDispose*2 + 4, "x");
497 } else {
498 Ty = llvm::PointerType::get(Ty, 0);
499 V = Builder.CreateBitCast(V, Ty);
500 }
501 return V;
502}
503
Mike Stump6cc88f72009-03-20 21:53:12 +0000504void CodeGenFunction::BlockForwardSelf() {
505 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
506 ImplicitParamDecl *SelfDecl = OMD->getSelfDecl();
507 llvm::Value *&DMEntry = LocalDeclMap[SelfDecl];
508 if (DMEntry)
509 return;
510 // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care
511 BlockDeclRefExpr *BDRE = new (getContext())
512 BlockDeclRefExpr(SelfDecl,
513 SelfDecl->getType(), SourceLocation(), false);
514 DMEntry = GetAddrOfBlockDecl(BDRE);
515}
516
Mike Stump67a64482009-02-14 22:16:35 +0000517llvm::Constant *
Mike Stump90a90432009-03-04 18:47:42 +0000518BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000519 // Generate the block descriptor.
520 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000521 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
522 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000523
Anders Carlssond5cab542009-02-12 17:55:02 +0000524 llvm::Constant *DescriptorFields[2];
Mike Stumpa5448542009-02-13 15:32:32 +0000525
Anders Carlssond5cab542009-02-12 17:55:02 +0000526 // Reserved
527 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000528
Anders Carlssond5cab542009-02-12 17:55:02 +0000529 // Block literal size. For global blocks we just use the size of the generic
530 // block literal struct.
Mike Stumpa5448542009-02-13 15:32:32 +0000531 uint64_t BlockLiteralSize =
Mike Stump9b8a7972009-02-13 15:25:34 +0000532 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Anders Carlssond5cab542009-02-12 17:55:02 +0000533 DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpa5448542009-02-13 15:32:32 +0000534
535 llvm::Constant *DescriptorStruct =
Anders Carlssond5cab542009-02-12 17:55:02 +0000536 llvm::ConstantStruct::get(&DescriptorFields[0], 2);
Mike Stumpa5448542009-02-13 15:32:32 +0000537
Anders Carlssond5cab542009-02-12 17:55:02 +0000538 llvm::GlobalVariable *Descriptor =
539 new llvm::GlobalVariable(DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000540 llvm::GlobalVariable::InternalLinkage,
541 DescriptorStruct, "__block_descriptor_global",
Anders Carlssond5cab542009-02-12 17:55:02 +0000542 &getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000543
Anders Carlssond5cab542009-02-12 17:55:02 +0000544 // Generate the constants for the block literal.
545 llvm::Constant *LiteralFields[5];
Mike Stumpa5448542009-02-13 15:32:32 +0000546
Mike Stump67a64482009-02-14 22:16:35 +0000547 CodeGenFunction::BlockInfo Info(0, n);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000548 uint64_t subBlockSize, subBlockAlign;
Mike Stumpa99038c2009-02-28 09:07:16 +0000549 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Daniel Dunbard67b09a2009-03-12 03:07:24 +0000550 bool subBlockHasCopyDispose = false;
Mike Stump7f28a9c2009-03-13 23:34:28 +0000551 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000552 llvm::Function *Fn
Mike Stump6cc88f72009-03-20 21:53:12 +0000553 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000554 subBlockSize,
Mike Stump90a90432009-03-04 18:47:42 +0000555 subBlockAlign,
Mike Stump00470a12009-03-05 08:32:30 +0000556 subBlockDeclRefDecls,
557 subBlockHasCopyDispose);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000558 assert(subBlockSize == BlockLiteralSize
559 && "no imports allowed for global block");
Daniel Dunbard67b09a2009-03-12 03:07:24 +0000560 assert(!subBlockHasCopyDispose && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000561
Anders Carlssond5cab542009-02-12 17:55:02 +0000562 // isa
Mike Stumpf99f1d02009-02-13 17:23:42 +0000563 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000564
Anders Carlssond5cab542009-02-12 17:55:02 +0000565 // Flags
Mike Stump00470a12009-03-05 08:32:30 +0000566 LiteralFields[1] =
Anders Carlsson8045ee02009-03-01 21:09:29 +0000567 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR);
Mike Stumpa5448542009-02-13 15:32:32 +0000568
Anders Carlssond5cab542009-02-12 17:55:02 +0000569 // Reserved
Mike Stump7cbb3602009-02-13 16:01:35 +0000570 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000571
Anders Carlssond5cab542009-02-12 17:55:02 +0000572 // Function
573 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000574
Anders Carlssond5cab542009-02-12 17:55:02 +0000575 // Descriptor
576 LiteralFields[4] = Descriptor;
Mike Stumpa5448542009-02-13 15:32:32 +0000577
578 llvm::Constant *BlockLiteralStruct =
Anders Carlssond5cab542009-02-12 17:55:02 +0000579 llvm::ConstantStruct::get(&LiteralFields[0], 5);
Mike Stumpa5448542009-02-13 15:32:32 +0000580
581 llvm::GlobalVariable *BlockLiteral =
Anders Carlssond5cab542009-02-12 17:55:02 +0000582 new llvm::GlobalVariable(BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000583 llvm::GlobalVariable::InternalLinkage,
584 BlockLiteralStruct, "__block_literal_global",
Anders Carlssond5cab542009-02-12 17:55:02 +0000585 &getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000586
Anders Carlssond5cab542009-02-12 17:55:02 +0000587 return BlockLiteral;
588}
589
Mike Stump4e7a1f72009-02-21 20:00:35 +0000590llvm::Value *CodeGenFunction::LoadBlockStruct() {
591 return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self");
592}
593
Mike Stump00470a12009-03-05 08:32:30 +0000594llvm::Function *
595CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
596 const BlockInfo& Info,
Mike Stump6cc88f72009-03-20 21:53:12 +0000597 const Decl *OuterFuncDecl,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000598 llvm::DenseMap<const Decl*, llvm::Value*> ldm,
Mike Stump00470a12009-03-05 08:32:30 +0000599 uint64_t &Size,
600 uint64_t &Align,
601 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls,
602 bool &subBlockHasCopyDispose) {
Devang Patel963dfbd2009-04-15 21:51:44 +0000603
604 // Check if we should generate debug info for this block.
605 if (CGM.getDebugInfo())
606 DebugInfo = CGM.getDebugInfo();
607
Mike Stump7f28a9c2009-03-13 23:34:28 +0000608 // Arrange for local static and local extern declarations to appear
609 // to be local to this function as well, as they are directly referenced
610 // in a block.
611 for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
612 i != ldm.end();
613 ++i) {
614 const VarDecl *VD = dyn_cast<VarDecl>(i->first);
615
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000616 if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage())
Mike Stump7f28a9c2009-03-13 23:34:28 +0000617 LocalDeclMap[VD] = i->second;
618 }
619
Eli Friedman48f91222009-03-28 03:24:54 +0000620 // FIXME: We need to rearrange the code for copy/dispose so we have this
621 // sooner, so we can calculate offsets correctly.
622 if (!BlockHasCopyDispose)
623 BlockOffset = CGM.getTargetData()
624 .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8;
625 else
626 BlockOffset = CGM.getTargetData()
627 .getTypeStoreSizeInBits(CGM.getGenericExtendedBlockLiteralType()) / 8;
628 BlockAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
629
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000630 const FunctionType *BlockFunctionType = BExpr->getFunctionType();
631 QualType ResultType;
632 bool IsVariadic;
Fariborz Jahanianda0895d2009-04-11 18:54:57 +0000633 if (const FunctionProtoType *FTy =
634 dyn_cast<FunctionProtoType>(BlockFunctionType)) {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000635 ResultType = FTy->getResultType();
636 IsVariadic = FTy->isVariadic();
637 }
638 else {
639 // K&R style block.
640 ResultType = BlockFunctionType->getResultType();
641 IsVariadic = false;
642 }
Mike Stumpa5448542009-02-13 15:32:32 +0000643
Anders Carlssond5cab542009-02-12 17:55:02 +0000644 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000645
Chris Lattner161d36d2009-02-28 19:01:03 +0000646 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlssond5cab542009-02-12 17:55:02 +0000647
648 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000649 ImplicitParamDecl *SelfDecl =
Anders Carlssond5cab542009-02-12 17:55:02 +0000650 ImplicitParamDecl::Create(getContext(), 0,
651 SourceLocation(), 0,
652 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000653
Anders Carlssond5cab542009-02-12 17:55:02 +0000654 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump4e7a1f72009-02-21 20:00:35 +0000655 BlockStructDecl = SelfDecl;
Mike Stumpa5448542009-02-13 15:32:32 +0000656
Steve Naroffe78b8092009-03-13 16:56:44 +0000657 for (BlockDecl::param_const_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000658 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000659 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000660
661 const CGFunctionInfo &FI =
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000662 CGM.getTypes().getFunctionInfo(ResultType, Args);
Anders Carlssond5cab542009-02-12 17:55:02 +0000663
Mike Stump67a64482009-02-14 22:16:35 +0000664 std::string Name = std::string("__") + Info.Name + "_block_invoke_";
Anders Carlssond5cab542009-02-12 17:55:02 +0000665 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000666 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic);
Mike Stumpa5448542009-02-13 15:32:32 +0000667
668 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000669 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
670 Name,
671 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000672
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000673 CGM.SetInternalFunctionAttributes(BD, Fn, FI);
674
Chris Lattner4863db42009-04-23 07:18:56 +0000675 StartFunction(BD, ResultType, Fn, Args,
Chris Lattner161d36d2009-02-28 19:01:03 +0000676 BExpr->getBody()->getLocEnd());
Chris Lattner4863db42009-04-23 07:18:56 +0000677 CurFuncDecl = OuterFuncDecl;
Chris Lattnerb5437d22009-04-23 05:30:27 +0000678 CurCodeDecl = BD;
Chris Lattner161d36d2009-02-28 19:01:03 +0000679 EmitStmt(BExpr->getBody());
680 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlssond5cab542009-02-12 17:55:02 +0000681
Mike Stump8a2b4b12009-02-25 23:33:13 +0000682 // The runtime needs a minimum alignment of a void *.
683 uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
684 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign);
685
Mike Stump4e7a1f72009-02-21 20:00:35 +0000686 Size = BlockOffset;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000687 Align = BlockAlign;
688 subBlockDeclRefDecls = BlockDeclRefDecls;
Mike Stump00470a12009-03-05 08:32:30 +0000689 subBlockHasCopyDispose |= BlockHasCopyDispose;
Anders Carlssond5cab542009-02-12 17:55:02 +0000690 return Fn;
691}
Mike Stumpa99038c2009-02-28 09:07:16 +0000692
Mike Stump08920992009-03-07 02:35:30 +0000693uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
Mike Stumpa99038c2009-02-28 09:07:16 +0000694 const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
695
696 uint64_t Size = getContext().getTypeSize(D->getType()) / 8;
697 uint64_t Align = getContext().getDeclAlignInBytes(D);
698
699 if (BDRE->isByRef()) {
700 Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8;
701 Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
702 }
703
704 assert ((Align > 0) && "alignment must be 1 byte or more");
705
706 uint64_t OldOffset = BlockOffset;
707
708 // Ensure proper alignment, even if it means we have to have a gap
709 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align);
710 BlockAlign = std::max(Align, BlockAlign);
Mike Stump00470a12009-03-05 08:32:30 +0000711
Mike Stumpa99038c2009-02-28 09:07:16 +0000712 uint64_t Pad = BlockOffset - OldOffset;
713 if (Pad) {
714 llvm::ArrayType::get(llvm::Type::Int8Ty, Pad);
715 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
716 llvm::APInt(32, Pad),
717 ArrayType::Normal, 0);
718 ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
719 0, QualType(PadTy), VarDecl::None,
720 SourceLocation());
721 Expr *E;
722 E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
723 SourceLocation(), false, false);
724 BlockDeclRefDecls.push_back(E);
725 }
726 BlockDeclRefDecls.push_back(BDRE);
727
728 BlockOffset += Size;
729 return BlockOffset-Size;
730}
Mike Stumpdab514f2009-03-04 03:23:46 +0000731
Mike Stump08920992009-03-07 02:35:30 +0000732llvm::Constant *BlockFunction::
733GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000734 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000735 QualType R = getContext().VoidTy;
736
737 FunctionArgList Args;
738 // FIXME: This leaks
Mike Stump08920992009-03-07 02:35:30 +0000739 ImplicitParamDecl *Dst =
740 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
741 getContext().getPointerType(getContext().VoidTy));
742 Args.push_back(std::make_pair(Dst, Dst->getType()));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000743 ImplicitParamDecl *Src =
744 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
745 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000746 Args.push_back(std::make_pair(Src, Src->getType()));
747
748 const CGFunctionInfo &FI =
749 CGM.getTypes().getFunctionInfo(R, Args);
750
751 std::string Name = std::string("__copy_helper_block_");
752 CodeGenTypes &Types = CGM.getTypes();
753 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
754
755 llvm::Function *Fn =
756 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
757 Name,
758 &CGM.getModule());
759
760 IdentifierInfo *II
761 = &CGM.getContext().Idents.get("__copy_helper_block_");
762
763 FunctionDecl *FD = FunctionDecl::Create(getContext(),
764 getContext().getTranslationUnitDecl(),
765 SourceLocation(), II, R,
766 FunctionDecl::Static, false,
767 true);
768 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump08920992009-03-07 02:35:30 +0000769
770 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
771 llvm::Type *PtrPtrT;
Mike Stump08920992009-03-07 02:35:30 +0000772
Mike Stumpb7477cf2009-04-10 18:52:28 +0000773 if (NoteForHelperp) {
774 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump08920992009-03-07 02:35:30 +0000775
Mike Stumpb7477cf2009-04-10 18:52:28 +0000776 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
777 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
778 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump08920992009-03-07 02:35:30 +0000779
Mike Stumpb7477cf2009-04-10 18:52:28 +0000780 llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000781 llvm::Type *PtrPtrT;
782 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
783 DstObj = Builder.CreateBitCast(DstObj, PtrPtrT);
784 DstObj = Builder.CreateLoad(DstObj);
Mike Stump08920992009-03-07 02:35:30 +0000785
Mike Stumpb7477cf2009-04-10 18:52:28 +0000786 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
787 int flag = NoteForHelper[i].flag;
788 int index = NoteForHelper[i].index;
Mike Stump08920992009-03-07 02:35:30 +0000789
Mike Stumpb7477cf2009-04-10 18:52:28 +0000790 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
791 || NoteForHelper[i].RequiresCopying) {
792 llvm::Value *Srcv = SrcObj;
793 Srcv = Builder.CreateStructGEP(Srcv, index);
794 Srcv = Builder.CreateBitCast(Srcv,
795 llvm::PointerType::get(PtrToInt8Ty, 0));
796 Srcv = Builder.CreateLoad(Srcv);
797
798 llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
799 Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
800
801 llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag);
802 llvm::Value *F = getBlockObjectAssign();
803 Builder.CreateCall3(F, Dstv, Srcv, N);
804 }
Mike Stump08920992009-03-07 02:35:30 +0000805 }
806 }
807
Mike Stumpa4f668f2009-03-06 01:33:24 +0000808 CGF.FinishFunction();
809
810 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000811}
812
Mike Stumpcf62d392009-03-06 18:42:23 +0000813llvm::Constant *BlockFunction::
Mike Stump08920992009-03-07 02:35:30 +0000814GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
815 const llvm::StructType* T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000816 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000817 QualType R = getContext().VoidTy;
818
819 FunctionArgList Args;
820 // FIXME: This leaks
821 ImplicitParamDecl *Src =
822 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
823 getContext().getPointerType(getContext().VoidTy));
824
825 Args.push_back(std::make_pair(Src, Src->getType()));
826
827 const CGFunctionInfo &FI =
828 CGM.getTypes().getFunctionInfo(R, Args);
829
830 std::string Name = std::string("__destroy_helper_block_");
831 CodeGenTypes &Types = CGM.getTypes();
832 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
833
834 llvm::Function *Fn =
835 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
836 Name,
837 &CGM.getModule());
838
839 IdentifierInfo *II
840 = &CGM.getContext().Idents.get("__destroy_helper_block_");
841
842 FunctionDecl *FD = FunctionDecl::Create(getContext(),
843 getContext().getTranslationUnitDecl(),
844 SourceLocation(), II, R,
845 FunctionDecl::Static, false,
846 true);
847 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1edf6b62009-03-07 02:53:18 +0000848
Mike Stumpb7477cf2009-04-10 18:52:28 +0000849 if (NoteForHelperp) {
850 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump1edf6b62009-03-07 02:53:18 +0000851
Mike Stumpb7477cf2009-04-10 18:52:28 +0000852 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
853 llvm::Type *PtrPtrT;
854 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
855 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
856 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump1edf6b62009-03-07 02:53:18 +0000857
Mike Stumpb7477cf2009-04-10 18:52:28 +0000858 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
859 int flag = NoteForHelper[i].flag;
860 int index = NoteForHelper[i].index;
Mike Stump1edf6b62009-03-07 02:53:18 +0000861
Mike Stumpb7477cf2009-04-10 18:52:28 +0000862 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
863 || NoteForHelper[i].RequiresCopying) {
864 llvm::Value *Srcv = SrcObj;
865 Srcv = Builder.CreateStructGEP(Srcv, index);
866 Srcv = Builder.CreateBitCast(Srcv,
867 llvm::PointerType::get(PtrToInt8Ty, 0));
868 Srcv = Builder.CreateLoad(Srcv);
869
870 BuildBlockRelease(Srcv, flag);
871 }
Mike Stump1edf6b62009-03-07 02:53:18 +0000872 }
873 }
874
Mike Stumpa4f668f2009-03-06 01:33:24 +0000875 CGF.FinishFunction();
876
877 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
878}
879
Mike Stump08920992009-03-07 02:35:30 +0000880llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000881 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump08920992009-03-07 02:35:30 +0000882 return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
883 T, NoteForHelper);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000884}
885
Mike Stump08920992009-03-07 02:35:30 +0000886llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000887 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stump08920992009-03-07 02:35:30 +0000888 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000889 T, NoteForHelperp);
Mike Stumpdab514f2009-03-04 03:23:46 +0000890}
Mike Stump797b6322009-03-05 01:23:13 +0000891
Mike Stumpee094222009-03-06 06:12:24 +0000892llvm::Constant *BlockFunction::
893GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
Mike Stump45031c02009-03-06 02:29:21 +0000894 QualType R = getContext().VoidTy;
895
896 FunctionArgList Args;
897 // FIXME: This leaks
Mike Stumpee094222009-03-06 06:12:24 +0000898 ImplicitParamDecl *Dst =
899 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
900 getContext().getPointerType(getContext().VoidTy));
901 Args.push_back(std::make_pair(Dst, Dst->getType()));
902
903 // FIXME: This leaks
Mike Stump45031c02009-03-06 02:29:21 +0000904 ImplicitParamDecl *Src =
905 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
906 getContext().getPointerType(getContext().VoidTy));
Mike Stump45031c02009-03-06 02:29:21 +0000907 Args.push_back(std::make_pair(Src, Src->getType()));
908
909 const CGFunctionInfo &FI =
910 CGM.getTypes().getFunctionInfo(R, Args);
911
912 std::string Name = std::string("__Block_byref_id_object_copy_");
913 CodeGenTypes &Types = CGM.getTypes();
914 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
915
916 llvm::Function *Fn =
917 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
918 Name,
919 &CGM.getModule());
920
921 IdentifierInfo *II
922 = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
923
924 FunctionDecl *FD = FunctionDecl::Create(getContext(),
925 getContext().getTranslationUnitDecl(),
926 SourceLocation(), II, R,
927 FunctionDecl::Static, false,
928 true);
929 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpee094222009-03-06 06:12:24 +0000930
931 // dst->x
932 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000933 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
934 V = Builder.CreateLoad(V);
Mike Stumpee094222009-03-06 06:12:24 +0000935 V = Builder.CreateStructGEP(V, 6, "x");
936 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
937
938 // src->x
939 V = CGF.GetAddrOfLocalVar(Src);
940 V = Builder.CreateLoad(V);
941 V = Builder.CreateBitCast(V, T);
942 V = Builder.CreateStructGEP(V, 6, "x");
943 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
944 llvm::Value *SrcObj = Builder.CreateLoad(V);
945
946 flag |= BLOCK_BYREF_CALLER;
947
948 llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag);
949 llvm::Value *F = getBlockObjectAssign();
950 Builder.CreateCall3(F, DstObj, SrcObj, N);
951
Mike Stump45031c02009-03-06 02:29:21 +0000952 CGF.FinishFunction();
953
954 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
955}
956
Mike Stump1851b682009-03-06 04:53:30 +0000957llvm::Constant *
958BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
959 int flag) {
Mike Stump45031c02009-03-06 02:29:21 +0000960 QualType R = getContext().VoidTy;
961
962 FunctionArgList Args;
963 // FIXME: This leaks
964 ImplicitParamDecl *Src =
965 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
966 getContext().getPointerType(getContext().VoidTy));
967
968 Args.push_back(std::make_pair(Src, Src->getType()));
969
970 const CGFunctionInfo &FI =
971 CGM.getTypes().getFunctionInfo(R, Args);
972
973 std::string Name = std::string("__Block_byref_id_object_dispose_");
974 CodeGenTypes &Types = CGM.getTypes();
975 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
976
977 llvm::Function *Fn =
978 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
979 Name,
980 &CGM.getModule());
981
982 IdentifierInfo *II
983 = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
984
985 FunctionDecl *FD = FunctionDecl::Create(getContext(),
986 getContext().getTranslationUnitDecl(),
987 SourceLocation(), II, R,
988 FunctionDecl::Static, false,
989 true);
990 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1851b682009-03-06 04:53:30 +0000991
992 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000993 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
994 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +0000995 V = Builder.CreateStructGEP(V, 6, "x");
Mike Stumpc2f4c342009-04-15 22:11:36 +0000996 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
997 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +0000998
Mike Stump1851b682009-03-06 04:53:30 +0000999 flag |= BLOCK_BYREF_CALLER;
1000 BuildBlockRelease(V, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001001 CGF.FinishFunction();
1002
1003 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
1004}
1005
Mike Stumpee094222009-03-06 06:12:24 +00001006llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
1007 int flag) {
1008 return CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001009}
1010
Mike Stump1851b682009-03-06 04:53:30 +00001011llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
1012 int flag) {
1013 return CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001014}
1015
Mike Stump797b6322009-03-05 01:23:13 +00001016llvm::Value *BlockFunction::getBlockObjectDispose() {
1017 if (CGM.BlockObjectDispose == 0) {
1018 const llvm::FunctionType *FTy;
1019 std::vector<const llvm::Type*> ArgTys;
1020 const llvm::Type *ResultType = llvm::Type::VoidTy;
1021 ArgTys.push_back(PtrToInt8Ty);
1022 ArgTys.push_back(llvm::Type::Int32Ty);
1023 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stump00470a12009-03-05 08:32:30 +00001024 CGM.BlockObjectDispose
Mike Stump797b6322009-03-05 01:23:13 +00001025 = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
1026 }
1027 return CGM.BlockObjectDispose;
1028}
1029
Mike Stumpee094222009-03-06 06:12:24 +00001030llvm::Value *BlockFunction::getBlockObjectAssign() {
1031 if (CGM.BlockObjectAssign == 0) {
1032 const llvm::FunctionType *FTy;
1033 std::vector<const llvm::Type*> ArgTys;
1034 const llvm::Type *ResultType = llvm::Type::VoidTy;
1035 ArgTys.push_back(PtrToInt8Ty);
1036 ArgTys.push_back(PtrToInt8Ty);
1037 ArgTys.push_back(llvm::Type::Int32Ty);
1038 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
1039 CGM.BlockObjectAssign
1040 = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
1041 }
1042 return CGM.BlockObjectAssign;
1043}
1044
Mike Stump1851b682009-03-06 04:53:30 +00001045void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Mike Stump797b6322009-03-05 01:23:13 +00001046 llvm::Value *F = getBlockObjectDispose();
Mike Stump1851b682009-03-06 04:53:30 +00001047 llvm::Value *N;
Mike Stump797b6322009-03-05 01:23:13 +00001048 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Mike Stump1851b682009-03-06 04:53:30 +00001049 N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag);
Mike Stump797b6322009-03-05 01:23:13 +00001050 Builder.CreateCall2(F, V, N);
1051}
Mike Stump00470a12009-03-05 08:32:30 +00001052
1053ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stump08920992009-03-07 02:35:30 +00001054
1055BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1056 CGBuilderTy &B)
1057 : CGM(cgm), CGF(cgf), Builder(B) {
1058 PtrToInt8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1059
1060 BlockHasCopyDispose = false;
1061}