blob: d62379e4ef63c0a7bede70fa074c330a791a6992 [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) {
Mike Stump7f28a9c2009-03-13 23:34:28 +0000603 // Arrange for local static and local extern declarations to appear
604 // to be local to this function as well, as they are directly referenced
605 // in a block.
606 for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
607 i != ldm.end();
608 ++i) {
609 const VarDecl *VD = dyn_cast<VarDecl>(i->first);
610
611 if (VD->getStorageClass() == VarDecl::Static
612 || VD->getStorageClass() == VarDecl::Extern)
613 LocalDeclMap[VD] = i->second;
614 }
615
Eli Friedman48f91222009-03-28 03:24:54 +0000616 // FIXME: We need to rearrange the code for copy/dispose so we have this
617 // sooner, so we can calculate offsets correctly.
618 if (!BlockHasCopyDispose)
619 BlockOffset = CGM.getTargetData()
620 .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8;
621 else
622 BlockOffset = CGM.getTargetData()
623 .getTypeStoreSizeInBits(CGM.getGenericExtendedBlockLiteralType()) / 8;
624 BlockAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
625
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000626 const FunctionType *BlockFunctionType = BExpr->getFunctionType();
627 QualType ResultType;
628 bool IsVariadic;
Fariborz Jahanianda0895d2009-04-11 18:54:57 +0000629 if (const FunctionProtoType *FTy =
630 dyn_cast<FunctionProtoType>(BlockFunctionType)) {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000631 ResultType = FTy->getResultType();
632 IsVariadic = FTy->isVariadic();
633 }
634 else {
635 // K&R style block.
636 ResultType = BlockFunctionType->getResultType();
637 IsVariadic = false;
638 }
Mike Stumpa5448542009-02-13 15:32:32 +0000639
Anders Carlssond5cab542009-02-12 17:55:02 +0000640 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000641
Chris Lattner161d36d2009-02-28 19:01:03 +0000642 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlssond5cab542009-02-12 17:55:02 +0000643
644 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000645 ImplicitParamDecl *SelfDecl =
Anders Carlssond5cab542009-02-12 17:55:02 +0000646 ImplicitParamDecl::Create(getContext(), 0,
647 SourceLocation(), 0,
648 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000649
Anders Carlssond5cab542009-02-12 17:55:02 +0000650 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump4e7a1f72009-02-21 20:00:35 +0000651 BlockStructDecl = SelfDecl;
Mike Stumpa5448542009-02-13 15:32:32 +0000652
Steve Naroffe78b8092009-03-13 16:56:44 +0000653 for (BlockDecl::param_const_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000654 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000655 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000656
657 const CGFunctionInfo &FI =
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000658 CGM.getTypes().getFunctionInfo(ResultType, Args);
Anders Carlssond5cab542009-02-12 17:55:02 +0000659
Mike Stump67a64482009-02-14 22:16:35 +0000660 std::string Name = std::string("__") + Info.Name + "_block_invoke_";
Anders Carlssond5cab542009-02-12 17:55:02 +0000661 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000662 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic);
Mike Stumpa5448542009-02-13 15:32:32 +0000663
664 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000665 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
666 Name,
667 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000668
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000669 StartFunction(BD, ResultType, Fn, Args,
Chris Lattner161d36d2009-02-28 19:01:03 +0000670 BExpr->getBody()->getLocEnd());
Mike Stump6cc88f72009-03-20 21:53:12 +0000671 CurFuncDecl = OuterFuncDecl;
Chris Lattner161d36d2009-02-28 19:01:03 +0000672 EmitStmt(BExpr->getBody());
673 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlssond5cab542009-02-12 17:55:02 +0000674
Mike Stump8a2b4b12009-02-25 23:33:13 +0000675 // The runtime needs a minimum alignment of a void *.
676 uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
677 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign);
678
Mike Stump4e7a1f72009-02-21 20:00:35 +0000679 Size = BlockOffset;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000680 Align = BlockAlign;
681 subBlockDeclRefDecls = BlockDeclRefDecls;
Mike Stump00470a12009-03-05 08:32:30 +0000682 subBlockHasCopyDispose |= BlockHasCopyDispose;
Anders Carlssond5cab542009-02-12 17:55:02 +0000683 return Fn;
684}
Mike Stumpa99038c2009-02-28 09:07:16 +0000685
Mike Stump08920992009-03-07 02:35:30 +0000686uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
Mike Stumpa99038c2009-02-28 09:07:16 +0000687 const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
688
689 uint64_t Size = getContext().getTypeSize(D->getType()) / 8;
690 uint64_t Align = getContext().getDeclAlignInBytes(D);
691
692 if (BDRE->isByRef()) {
693 Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8;
694 Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
695 }
696
697 assert ((Align > 0) && "alignment must be 1 byte or more");
698
699 uint64_t OldOffset = BlockOffset;
700
701 // Ensure proper alignment, even if it means we have to have a gap
702 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align);
703 BlockAlign = std::max(Align, BlockAlign);
Mike Stump00470a12009-03-05 08:32:30 +0000704
Mike Stumpa99038c2009-02-28 09:07:16 +0000705 uint64_t Pad = BlockOffset - OldOffset;
706 if (Pad) {
707 llvm::ArrayType::get(llvm::Type::Int8Ty, Pad);
708 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
709 llvm::APInt(32, Pad),
710 ArrayType::Normal, 0);
711 ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
712 0, QualType(PadTy), VarDecl::None,
713 SourceLocation());
714 Expr *E;
715 E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
716 SourceLocation(), false, false);
717 BlockDeclRefDecls.push_back(E);
718 }
719 BlockDeclRefDecls.push_back(BDRE);
720
721 BlockOffset += Size;
722 return BlockOffset-Size;
723}
Mike Stumpdab514f2009-03-04 03:23:46 +0000724
Mike Stump08920992009-03-07 02:35:30 +0000725llvm::Constant *BlockFunction::
726GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000727 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000728 QualType R = getContext().VoidTy;
729
730 FunctionArgList Args;
731 // FIXME: This leaks
Mike Stump08920992009-03-07 02:35:30 +0000732 ImplicitParamDecl *Dst =
733 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
734 getContext().getPointerType(getContext().VoidTy));
735 Args.push_back(std::make_pair(Dst, Dst->getType()));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000736 ImplicitParamDecl *Src =
737 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
738 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000739 Args.push_back(std::make_pair(Src, Src->getType()));
740
741 const CGFunctionInfo &FI =
742 CGM.getTypes().getFunctionInfo(R, Args);
743
744 std::string Name = std::string("__copy_helper_block_");
745 CodeGenTypes &Types = CGM.getTypes();
746 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
747
748 llvm::Function *Fn =
749 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
750 Name,
751 &CGM.getModule());
752
753 IdentifierInfo *II
754 = &CGM.getContext().Idents.get("__copy_helper_block_");
755
756 FunctionDecl *FD = FunctionDecl::Create(getContext(),
757 getContext().getTranslationUnitDecl(),
758 SourceLocation(), II, R,
759 FunctionDecl::Static, false,
760 true);
761 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump08920992009-03-07 02:35:30 +0000762
763 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
764 llvm::Type *PtrPtrT;
Mike Stump08920992009-03-07 02:35:30 +0000765
Mike Stumpb7477cf2009-04-10 18:52:28 +0000766 if (NoteForHelperp) {
767 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump08920992009-03-07 02:35:30 +0000768
Mike Stumpb7477cf2009-04-10 18:52:28 +0000769 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
770 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
771 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump08920992009-03-07 02:35:30 +0000772
Mike Stumpb7477cf2009-04-10 18:52:28 +0000773 llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
774 DstObj = Builder.CreateBitCast(DstObj, llvm::PointerType::get(T, 0));
Mike Stump08920992009-03-07 02:35:30 +0000775
Mike Stumpb7477cf2009-04-10 18:52:28 +0000776 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
777 int flag = NoteForHelper[i].flag;
778 int index = NoteForHelper[i].index;
Mike Stump08920992009-03-07 02:35:30 +0000779
Mike Stumpb7477cf2009-04-10 18:52:28 +0000780 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
781 || NoteForHelper[i].RequiresCopying) {
782 llvm::Value *Srcv = SrcObj;
783 Srcv = Builder.CreateStructGEP(Srcv, index);
784 Srcv = Builder.CreateBitCast(Srcv,
785 llvm::PointerType::get(PtrToInt8Ty, 0));
786 Srcv = Builder.CreateLoad(Srcv);
787
788 llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
789 Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
790
791 llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag);
792 llvm::Value *F = getBlockObjectAssign();
793 Builder.CreateCall3(F, Dstv, Srcv, N);
794 }
Mike Stump08920992009-03-07 02:35:30 +0000795 }
796 }
797
Mike Stumpa4f668f2009-03-06 01:33:24 +0000798 CGF.FinishFunction();
799
800 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000801}
802
Mike Stumpcf62d392009-03-06 18:42:23 +0000803llvm::Constant *BlockFunction::
Mike Stump08920992009-03-07 02:35:30 +0000804GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
805 const llvm::StructType* T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000806 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000807 QualType R = getContext().VoidTy;
808
809 FunctionArgList Args;
810 // FIXME: This leaks
811 ImplicitParamDecl *Src =
812 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
813 getContext().getPointerType(getContext().VoidTy));
814
815 Args.push_back(std::make_pair(Src, Src->getType()));
816
817 const CGFunctionInfo &FI =
818 CGM.getTypes().getFunctionInfo(R, Args);
819
820 std::string Name = std::string("__destroy_helper_block_");
821 CodeGenTypes &Types = CGM.getTypes();
822 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
823
824 llvm::Function *Fn =
825 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
826 Name,
827 &CGM.getModule());
828
829 IdentifierInfo *II
830 = &CGM.getContext().Idents.get("__destroy_helper_block_");
831
832 FunctionDecl *FD = FunctionDecl::Create(getContext(),
833 getContext().getTranslationUnitDecl(),
834 SourceLocation(), II, R,
835 FunctionDecl::Static, false,
836 true);
837 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1edf6b62009-03-07 02:53:18 +0000838
Mike Stumpb7477cf2009-04-10 18:52:28 +0000839 if (NoteForHelperp) {
840 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump1edf6b62009-03-07 02:53:18 +0000841
Mike Stumpb7477cf2009-04-10 18:52:28 +0000842 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
843 llvm::Type *PtrPtrT;
844 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
845 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
846 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump1edf6b62009-03-07 02:53:18 +0000847
Mike Stumpb7477cf2009-04-10 18:52:28 +0000848 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
849 int flag = NoteForHelper[i].flag;
850 int index = NoteForHelper[i].index;
Mike Stump1edf6b62009-03-07 02:53:18 +0000851
Mike Stumpb7477cf2009-04-10 18:52:28 +0000852 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
853 || NoteForHelper[i].RequiresCopying) {
854 llvm::Value *Srcv = SrcObj;
855 Srcv = Builder.CreateStructGEP(Srcv, index);
856 Srcv = Builder.CreateBitCast(Srcv,
857 llvm::PointerType::get(PtrToInt8Ty, 0));
858 Srcv = Builder.CreateLoad(Srcv);
859
860 BuildBlockRelease(Srcv, flag);
861 }
Mike Stump1edf6b62009-03-07 02:53:18 +0000862 }
863 }
864
Mike Stumpa4f668f2009-03-06 01:33:24 +0000865 CGF.FinishFunction();
866
867 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
868}
869
Mike Stump08920992009-03-07 02:35:30 +0000870llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000871 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump08920992009-03-07 02:35:30 +0000872 return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
873 T, NoteForHelper);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000874}
875
Mike Stump08920992009-03-07 02:35:30 +0000876llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000877 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stump08920992009-03-07 02:35:30 +0000878 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000879 T, NoteForHelperp);
Mike Stumpdab514f2009-03-04 03:23:46 +0000880}
Mike Stump797b6322009-03-05 01:23:13 +0000881
Mike Stumpee094222009-03-06 06:12:24 +0000882llvm::Constant *BlockFunction::
883GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
Mike Stump45031c02009-03-06 02:29:21 +0000884 QualType R = getContext().VoidTy;
885
886 FunctionArgList Args;
887 // FIXME: This leaks
Mike Stumpee094222009-03-06 06:12:24 +0000888 ImplicitParamDecl *Dst =
889 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
890 getContext().getPointerType(getContext().VoidTy));
891 Args.push_back(std::make_pair(Dst, Dst->getType()));
892
893 // FIXME: This leaks
Mike Stump45031c02009-03-06 02:29:21 +0000894 ImplicitParamDecl *Src =
895 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
896 getContext().getPointerType(getContext().VoidTy));
Mike Stump45031c02009-03-06 02:29:21 +0000897 Args.push_back(std::make_pair(Src, Src->getType()));
898
899 const CGFunctionInfo &FI =
900 CGM.getTypes().getFunctionInfo(R, Args);
901
902 std::string Name = std::string("__Block_byref_id_object_copy_");
903 CodeGenTypes &Types = CGM.getTypes();
904 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
905
906 llvm::Function *Fn =
907 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
908 Name,
909 &CGM.getModule());
910
911 IdentifierInfo *II
912 = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
913
914 FunctionDecl *FD = FunctionDecl::Create(getContext(),
915 getContext().getTranslationUnitDecl(),
916 SourceLocation(), II, R,
917 FunctionDecl::Static, false,
918 true);
919 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpee094222009-03-06 06:12:24 +0000920
921 // dst->x
922 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
923 V = Builder.CreateBitCast(V, T);
924 V = Builder.CreateStructGEP(V, 6, "x");
925 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
926
927 // src->x
928 V = CGF.GetAddrOfLocalVar(Src);
929 V = Builder.CreateLoad(V);
930 V = Builder.CreateBitCast(V, T);
931 V = Builder.CreateStructGEP(V, 6, "x");
932 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
933 llvm::Value *SrcObj = Builder.CreateLoad(V);
934
935 flag |= BLOCK_BYREF_CALLER;
936
937 llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag);
938 llvm::Value *F = getBlockObjectAssign();
939 Builder.CreateCall3(F, DstObj, SrcObj, N);
940
Mike Stump45031c02009-03-06 02:29:21 +0000941 CGF.FinishFunction();
942
943 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
944}
945
Mike Stump1851b682009-03-06 04:53:30 +0000946llvm::Constant *
947BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
948 int flag) {
Mike Stump45031c02009-03-06 02:29:21 +0000949 QualType R = getContext().VoidTy;
950
951 FunctionArgList Args;
952 // FIXME: This leaks
953 ImplicitParamDecl *Src =
954 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
955 getContext().getPointerType(getContext().VoidTy));
956
957 Args.push_back(std::make_pair(Src, Src->getType()));
958
959 const CGFunctionInfo &FI =
960 CGM.getTypes().getFunctionInfo(R, Args);
961
962 std::string Name = std::string("__Block_byref_id_object_dispose_");
963 CodeGenTypes &Types = CGM.getTypes();
964 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
965
966 llvm::Function *Fn =
967 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
968 Name,
969 &CGM.getModule());
970
971 IdentifierInfo *II
972 = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
973
974 FunctionDecl *FD = FunctionDecl::Create(getContext(),
975 getContext().getTranslationUnitDecl(),
976 SourceLocation(), II, R,
977 FunctionDecl::Static, false,
978 true);
979 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1851b682009-03-06 04:53:30 +0000980
981 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
982 V = Builder.CreateBitCast(V, T);
983 V = Builder.CreateStructGEP(V, 6, "x");
984 V = Builder.CreateBitCast(V, PtrToInt8Ty);
985
Mike Stump1851b682009-03-06 04:53:30 +0000986 flag |= BLOCK_BYREF_CALLER;
987 BuildBlockRelease(V, flag);
Mike Stump45031c02009-03-06 02:29:21 +0000988 CGF.FinishFunction();
989
990 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
991}
992
Mike Stumpee094222009-03-06 06:12:24 +0000993llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
994 int flag) {
995 return CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag);
Mike Stump45031c02009-03-06 02:29:21 +0000996}
997
Mike Stump1851b682009-03-06 04:53:30 +0000998llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
999 int flag) {
1000 return CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001001}
1002
Mike Stump797b6322009-03-05 01:23:13 +00001003llvm::Value *BlockFunction::getBlockObjectDispose() {
1004 if (CGM.BlockObjectDispose == 0) {
1005 const llvm::FunctionType *FTy;
1006 std::vector<const llvm::Type*> ArgTys;
1007 const llvm::Type *ResultType = llvm::Type::VoidTy;
1008 ArgTys.push_back(PtrToInt8Ty);
1009 ArgTys.push_back(llvm::Type::Int32Ty);
1010 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stump00470a12009-03-05 08:32:30 +00001011 CGM.BlockObjectDispose
Mike Stump797b6322009-03-05 01:23:13 +00001012 = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
1013 }
1014 return CGM.BlockObjectDispose;
1015}
1016
Mike Stumpee094222009-03-06 06:12:24 +00001017llvm::Value *BlockFunction::getBlockObjectAssign() {
1018 if (CGM.BlockObjectAssign == 0) {
1019 const llvm::FunctionType *FTy;
1020 std::vector<const llvm::Type*> ArgTys;
1021 const llvm::Type *ResultType = llvm::Type::VoidTy;
1022 ArgTys.push_back(PtrToInt8Ty);
1023 ArgTys.push_back(PtrToInt8Ty);
1024 ArgTys.push_back(llvm::Type::Int32Ty);
1025 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
1026 CGM.BlockObjectAssign
1027 = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
1028 }
1029 return CGM.BlockObjectAssign;
1030}
1031
Mike Stump1851b682009-03-06 04:53:30 +00001032void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Mike Stump797b6322009-03-05 01:23:13 +00001033 llvm::Value *F = getBlockObjectDispose();
Mike Stump1851b682009-03-06 04:53:30 +00001034 llvm::Value *N;
Mike Stump797b6322009-03-05 01:23:13 +00001035 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Mike Stump1851b682009-03-06 04:53:30 +00001036 N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag);
Mike Stump797b6322009-03-05 01:23:13 +00001037 Builder.CreateCall2(F, V, N);
1038}
Mike Stump00470a12009-03-05 08:32:30 +00001039
1040ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stump08920992009-03-07 02:35:30 +00001041
1042BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1043 CGBuilderTy &B)
1044 : CGM(cgm), CGF(cgf), Builder(B) {
1045 PtrToInt8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1046
1047 BlockHasCopyDispose = false;
1048}