blob: 5156d488e2b9444ae87310f8ea361bb8a5dd4a87 [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#include <algorithm>
Torok Edwinf42e4a62009-08-24 13:25:12 +000020#include <cstdio>
21
Anders Carlssonacfde802009-02-12 00:39:25 +000022using namespace clang;
23using namespace CodeGen;
24
Mike Stumpcf62d392009-03-06 18:42:23 +000025llvm::Constant *CodeGenFunction::
Mike Stumpa803b0e2009-03-25 17:58:24 +000026BuildDescriptorBlockDecl(bool BlockHasCopyDispose, uint64_t Size,
27 const llvm::StructType* Ty,
Mike Stump08920992009-03-07 02:35:30 +000028 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump56129b12009-02-13 16:55:51 +000029 const llvm::Type *UnsignedLongTy
30 = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpe5fee252009-02-13 16:19:19 +000031 llvm::Constant *C;
32 std::vector<llvm::Constant*> Elts;
33
34 // reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +000035 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpe5fee252009-02-13 16:19:19 +000036 Elts.push_back(C);
37
38 // Size
Mike Stumpd6840002009-02-21 20:07:44 +000039 // FIXME: What is the right way to say this doesn't fit? We should give
40 // a user diagnostic in that case. Better fix would be to change the
41 // API to size_t.
Owen Anderson4a28d5d2009-07-24 23:12:58 +000042 C = llvm::ConstantInt::get(UnsignedLongTy, Size);
Mike Stumpe5fee252009-02-13 16:19:19 +000043 Elts.push_back(C);
44
45 if (BlockHasCopyDispose) {
46 // copy_func_helper_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000047 Elts.push_back(BuildCopyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000048
49 // destroy_func_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000050 Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000051 }
52
Owen Anderson47a434f2009-08-05 23:18:46 +000053 C = llvm::ConstantStruct::get(VMContext, Elts);
Mike Stumpe5fee252009-02-13 16:19:19 +000054
Owen Anderson1c431b32009-07-08 19:05:04 +000055 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Mike Stumpe5fee252009-02-13 16:19:19 +000056 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +000057 C, "__block_descriptor_tmp");
Mike Stumpe5fee252009-02-13 16:19:19 +000058 return C;
59}
60
Mike Stump2a998142009-03-04 18:17:45 +000061llvm::Constant *BlockModule::getNSConcreteGlobalBlock() {
Chris Lattnere2f79b62009-05-13 02:50:56 +000062 if (NSConcreteGlobalBlock == 0)
63 NSConcreteGlobalBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
64 "_NSConcreteGlobalBlock");
Mike Stumpf99f1d02009-02-13 17:23:42 +000065 return NSConcreteGlobalBlock;
66}
67
Mike Stump2a998142009-03-04 18:17:45 +000068llvm::Constant *BlockModule::getNSConcreteStackBlock() {
Chris Lattnere2f79b62009-05-13 02:50:56 +000069 if (NSConcreteStackBlock == 0)
70 NSConcreteStackBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
71 "_NSConcreteStackBlock");
Mike Stump59c5b112009-02-13 19:29:27 +000072 return NSConcreteStackBlock;
73}
74
Mike Stump00470a12009-03-05 08:32:30 +000075static void CollectBlockDeclRefInfo(const Stmt *S,
Anders Carlsson4de9fce2009-03-01 01:09:12 +000076 CodeGenFunction::BlockInfo &Info) {
77 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
78 I != E; ++I)
Daniel Dunbar82573ee2009-03-02 07:00:57 +000079 if (*I)
80 CollectBlockDeclRefInfo(*I, Info);
Mike Stump00470a12009-03-05 08:32:30 +000081
Anders Carlsson4de9fce2009-03-01 01:09:12 +000082 if (const BlockDeclRefExpr *DE = dyn_cast<BlockDeclRefExpr>(S)) {
83 // FIXME: Handle enums.
84 if (isa<FunctionDecl>(DE->getDecl()))
85 return;
Mike Stump00470a12009-03-05 08:32:30 +000086
Anders Carlsson4de9fce2009-03-01 01:09:12 +000087 if (DE->isByRef())
88 Info.ByRefDeclRefs.push_back(DE);
89 else
90 Info.ByCopyDeclRefs.push_back(DE);
91 }
92}
93
Mike Stump58a85142009-03-04 22:48:06 +000094/// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be
95/// declared as a global variable instead of on the stack.
Chris Lattnere2f79b62009-05-13 02:50:56 +000096static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info) {
Anders Carlsson4de9fce2009-03-01 01:09:12 +000097 return Info.ByRefDeclRefs.empty() && Info.ByCopyDeclRefs.empty();
98}
99
Mike Stump58a85142009-03-04 22:48:06 +0000100// FIXME: Push most into CGM, passing down a few bits, like current function
101// name.
Mike Stump8a2b4b12009-02-25 23:33:13 +0000102llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Mike Stumpe5fee252009-02-13 16:19:19 +0000103
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000104 std::string Name = CurFn->getName();
105 CodeGenFunction::BlockInfo Info(0, Name.c_str());
106 CollectBlockDeclRefInfo(BE->getBody(), Info);
107
108 // Check if the block can be global.
Mike Stump58a85142009-03-04 22:48:06 +0000109 // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like
110 // to just have one code path. We should move this function into CGM and pass
111 // CGF, then we can just check to see if CGF is 0.
Mike Stumpdab514f2009-03-04 03:23:46 +0000112 if (0 && CanBlockBeGlobal(Info))
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000113 return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
Mike Stump00470a12009-03-05 08:32:30 +0000114
115 std::vector<llvm::Constant*> Elts(5);
Mike Stumpe5fee252009-02-13 16:19:19 +0000116 llvm::Constant *C;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000117 llvm::Value *V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000118
Mike Stumpe5fee252009-02-13 16:19:19 +0000119 {
120 // C = BuildBlockStructInitlist();
121 unsigned int flags = BLOCK_HAS_DESCRIPTOR;
122
Mike Stump00470a12009-03-05 08:32:30 +0000123 // We run this first so that we set BlockHasCopyDispose from the entire
124 // block literal.
125 // __invoke
126 uint64_t subBlockSize, subBlockAlign;
127 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Mike Stumpa803b0e2009-03-25 17:58:24 +0000128 bool subBlockHasCopyDispose = false;
Mike Stump00470a12009-03-05 08:32:30 +0000129 llvm::Function *Fn
Mike Stump6cc88f72009-03-20 21:53:12 +0000130 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl, LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000131 subBlockSize,
Mike Stump00470a12009-03-05 08:32:30 +0000132 subBlockAlign,
133 subBlockDeclRefDecls,
Mike Stumpa803b0e2009-03-25 17:58:24 +0000134 subBlockHasCopyDispose);
135 BlockHasCopyDispose |= subBlockHasCopyDispose;
Mike Stump00470a12009-03-05 08:32:30 +0000136 Elts[3] = Fn;
137
Mike Stumpf5408fe2009-05-16 07:57:57 +0000138 // FIXME: Don't use BlockHasCopyDispose, it is set more often then
139 // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); }
Mike Stumpa803b0e2009-03-25 17:58:24 +0000140 if (subBlockHasCopyDispose)
Mike Stumpe5fee252009-02-13 16:19:19 +0000141 flags |= BLOCK_HAS_COPY_DISPOSE;
142
Mike Stump7d6dc4f2009-02-13 20:17:16 +0000143 // __isa
Mike Stump59c5b112009-02-13 19:29:27 +0000144 C = CGM.getNSConcreteStackBlock();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000145 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
Mike Stump00470a12009-03-05 08:32:30 +0000146 Elts[0] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000147
148 // __flags
149 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
150 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000151 C = llvm::ConstantInt::get(IntTy, flags);
Mike Stump00470a12009-03-05 08:32:30 +0000152 Elts[1] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000153
154 // __reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000155 C = llvm::ConstantInt::get(IntTy, 0);
Mike Stump00470a12009-03-05 08:32:30 +0000156 Elts[2] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000157
Mike Stump8a2b4b12009-02-25 23:33:13 +0000158 if (subBlockDeclRefDecls.size() == 0) {
Mike Stumpcf62d392009-03-06 18:42:23 +0000159 // __descriptor
Mike Stumpa803b0e2009-03-25 17:58:24 +0000160 Elts[4] = BuildDescriptorBlockDecl(subBlockHasCopyDispose, subBlockSize, 0, 0);
Mike Stumpcf62d392009-03-06 18:42:23 +0000161
Mike Stump5570cfe2009-03-01 20:07:53 +0000162 // Optimize to being a global block.
163 Elts[0] = CGM.getNSConcreteGlobalBlock();
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000164 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
Mike Stump5570cfe2009-03-01 20:07:53 +0000165
Owen Anderson47a434f2009-08-05 23:18:46 +0000166 C = llvm::ConstantStruct::get(VMContext, Elts);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000167
168 char Name[32];
169 sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
Owen Anderson1c431b32009-07-08 19:05:04 +0000170 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Mike Stump8a2b4b12009-02-25 23:33:13 +0000171 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000172 C, Name);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000173 QualType BPT = BE->getType();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000174 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000175 return C;
176 }
Mike Stump00470a12009-03-05 08:32:30 +0000177
Mike Stump8a2b4b12009-02-25 23:33:13 +0000178 std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size());
Mike Stump08920992009-03-07 02:35:30 +0000179 for (int i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000180 Types[i] = Elts[i]->getType();
Mike Stump08920992009-03-07 02:35:30 +0000181 Types[4] = PtrToInt8Ty;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000182
Mike Stumpa99038c2009-02-28 09:07:16 +0000183 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
184 const Expr *E = subBlockDeclRefDecls[i];
185 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
186 QualType Ty = E->getType();
Mike Stumpdab514f2009-03-04 03:23:46 +0000187 if (BDRE && BDRE->isByRef()) {
188 uint64_t Align = getContext().getDeclAlignInBytes(BDRE->getDecl());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000189 Types[i+5] = llvm::PointerType::get(BuildByRefType(Ty, Align), 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000190 } else
191 Types[i+5] = ConvertType(Ty);
Mike Stumpa99038c2009-02-28 09:07:16 +0000192 }
Mike Stump8a2b4b12009-02-25 23:33:13 +0000193
Owen Anderson47a434f2009-08-05 23:18:46 +0000194 llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000195
196 llvm::AllocaInst *A = CreateTempAlloca(Ty);
197 A->setAlignment(subBlockAlign);
198 V = A;
199
Mike Stump08920992009-03-07 02:35:30 +0000200 std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size());
201 int helpersize = 0;
202
203 for (unsigned i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000204 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
Mike Stump00470a12009-03-05 08:32:30 +0000205
Mike Stump8a2b4b12009-02-25 23:33:13 +0000206 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
207 {
Mike Stumpa99038c2009-02-28 09:07:16 +0000208 // FIXME: Push const down.
209 Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
210 DeclRefExpr *DR;
211 ValueDecl *VD;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000212
Mike Stumpa99038c2009-02-28 09:07:16 +0000213 DR = dyn_cast<DeclRefExpr>(E);
214 // Skip padding.
215 if (DR) continue;
216
217 BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
218 VD = BDRE->getDecl();
219
Mike Stumpdab514f2009-03-04 03:23:46 +0000220 llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp");
Mike Stump08920992009-03-07 02:35:30 +0000221 NoteForHelper[helpersize].index = i+5;
222 NoteForHelper[helpersize].RequiresCopying = BlockRequiresCopying(VD->getType());
223 NoteForHelper[helpersize].flag
224 = VD->getType()->isBlockPointerType() ? BLOCK_FIELD_IS_BLOCK : BLOCK_FIELD_IS_OBJECT;
225
Mike Stumpa99038c2009-02-28 09:07:16 +0000226 if (LocalDeclMap[VD]) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000227 if (BDRE->isByRef()) {
Mike Stump08920992009-03-07 02:35:30 +0000228 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
Mike Stumpf4bc3122009-03-07 06:04:31 +0000229 // FIXME: Someone double check this.
230 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000231 const llvm::Type *Ty = Types[i+5];
232 llvm::Value *Loc = LocalDeclMap[VD];
233 Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
234 Loc = Builder.CreateLoad(Loc, false);
235 Loc = Builder.CreateBitCast(Loc, Ty);
236 Builder.CreateStore(Loc, Addr);
Mike Stump08920992009-03-07 02:35:30 +0000237 ++helpersize;
Mike Stumpdab514f2009-03-04 03:23:46 +0000238 continue;
239 } else
240 E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD),
241 VD->getType(), SourceLocation(),
242 false, false);
Mike Stumpa99038c2009-02-28 09:07:16 +0000243 }
Mike Stumpdab514f2009-03-04 03:23:46 +0000244 if (BDRE->isByRef()) {
Mike Stumpa8b60c92009-03-21 21:00:35 +0000245 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
246 // FIXME: Someone double check this.
247 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpa99038c2009-02-28 09:07:16 +0000248 E = new (getContext())
249 UnaryOperator(E, UnaryOperator::AddrOf,
250 getContext().getPointerType(E->getType()),
251 SourceLocation());
Mike Stumpdab514f2009-03-04 03:23:46 +0000252 }
Mike Stump08920992009-03-07 02:35:30 +0000253 ++helpersize;
Mike Stumpa99038c2009-02-28 09:07:16 +0000254
Mike Stumpa99038c2009-02-28 09:07:16 +0000255 RValue r = EmitAnyExpr(E, Addr, false);
Mike Stumpdab514f2009-03-04 03:23:46 +0000256 if (r.isScalar()) {
257 llvm::Value *Loc = r.getScalarVal();
258 const llvm::Type *Ty = Types[i+5];
259 if (BDRE->isByRef()) {
Mike Stump58a85142009-03-04 22:48:06 +0000260 // E is now the address of the value field, instead, we want the
261 // address of the actual ByRef struct. We optimize this slightly
262 // compared to gcc by not grabbing the forwarding slot as this must
263 // be done during Block_copy for us, and we can postpone the work
264 // until then.
265 uint64_t offset = BlockDecls[BDRE->getDecl()];
Mike Stump00470a12009-03-05 08:32:30 +0000266
Mike Stump58a85142009-03-04 22:48:06 +0000267 llvm::Value *BlockLiteral = LoadBlockStruct();
Mike Stump00470a12009-03-05 08:32:30 +0000268
Mike Stump58a85142009-03-04 22:48:06 +0000269 Loc = Builder.CreateGEP(BlockLiteral,
Owen Anderson0032b272009-08-13 21:57:51 +0000270 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Mike Stump58a85142009-03-04 22:48:06 +0000271 offset),
272 "block.literal");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000273 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000274 Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stump58a85142009-03-04 22:48:06 +0000275 Loc = Builder.CreateLoad(Loc, false);
276 // Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000277 }
278 Builder.CreateStore(Loc, Addr);
279 } else if (r.isComplex())
Mike Stump8a2b4b12009-02-25 23:33:13 +0000280 // FIXME: implement
281 ErrorUnsupported(BE, "complex in block literal");
282 else if (r.isAggregate())
283 ; // Already created into the destination
284 else
285 assert (0 && "bad block variable");
286 // FIXME: Ensure that the offset created by the backend for
287 // the struct matches the previously computed offset in BlockDecls.
288 }
Mike Stump08920992009-03-07 02:35:30 +0000289 NoteForHelper.resize(helpersize);
Mike Stumpcf62d392009-03-06 18:42:23 +0000290
291 // __descriptor
Mike Stumpa803b0e2009-03-25 17:58:24 +0000292 llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockHasCopyDispose,
293 subBlockSize, Ty,
Mike Stump08920992009-03-07 02:35:30 +0000294 &NoteForHelper);
295 Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
296 Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
Mike Stumpe5fee252009-02-13 16:19:19 +0000297 }
Mike Stump00470a12009-03-05 08:32:30 +0000298
Mike Stumpbd65cac2009-02-19 01:01:04 +0000299 QualType BPT = BE->getType();
Mike Stump8a2b4b12009-02-25 23:33:13 +0000300 return Builder.CreateBitCast(V, ConvertType(BPT));
Mike Stumpe5fee252009-02-13 16:19:19 +0000301}
302
303
Mike Stump2a998142009-03-04 18:17:45 +0000304const llvm::Type *BlockModule::getBlockDescriptorType() {
Mike Stumpab695142009-02-13 15:16:56 +0000305 if (BlockDescriptorType)
306 return BlockDescriptorType;
307
Mike Stumpa5448542009-02-13 15:32:32 +0000308 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000309 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000310
Mike Stumpab695142009-02-13 15:16:56 +0000311 // struct __block_descriptor {
312 // unsigned long reserved;
313 // unsigned long block_size;
314 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000315 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(),
316 UnsignedLongTy,
Mike Stumpa5448542009-02-13 15:32:32 +0000317 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000318 NULL);
319
320 getModule().addTypeName("struct.__block_descriptor",
321 BlockDescriptorType);
322
323 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000324}
325
Mike Stump2a998142009-03-04 18:17:45 +0000326const llvm::Type *BlockModule::getGenericBlockLiteralType() {
Mike Stump9b8a7972009-02-13 15:25:34 +0000327 if (GenericBlockLiteralType)
328 return GenericBlockLiteralType;
329
Mike Stumpa5448542009-02-13 15:32:32 +0000330 const llvm::Type *BlockDescPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000331 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000332
Mike Stump7cbb3602009-02-13 16:01:35 +0000333 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
334 getTypes().ConvertType(getContext().IntTy));
335
Mike Stump9b8a7972009-02-13 15:25:34 +0000336 // struct __block_literal_generic {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000337 // void *__isa;
338 // int __flags;
339 // int __reserved;
340 // void (*__invoke)(void *);
341 // struct __block_descriptor *__descriptor;
Mike Stump9b8a7972009-02-13 15:25:34 +0000342 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000343 GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
344 PtrToInt8Ty,
Mike Stump7cbb3602009-02-13 16:01:35 +0000345 IntTy,
346 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000347 PtrToInt8Ty,
Mike Stump9b8a7972009-02-13 15:25:34 +0000348 BlockDescPtrTy,
349 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000350
Mike Stump9b8a7972009-02-13 15:25:34 +0000351 getModule().addTypeName("struct.__block_literal_generic",
352 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000353
Mike Stump9b8a7972009-02-13 15:25:34 +0000354 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000355}
356
Mike Stump2a998142009-03-04 18:17:45 +0000357const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000358 if (GenericExtendedBlockLiteralType)
359 return GenericExtendedBlockLiteralType;
360
Mike Stumpbd65cac2009-02-19 01:01:04 +0000361 const llvm::Type *BlockDescPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000362 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpbd65cac2009-02-19 01:01:04 +0000363
364 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
365 getTypes().ConvertType(getContext().IntTy));
366
367 // struct __block_literal_generic {
368 // void *__isa;
369 // int __flags;
370 // int __reserved;
371 // void (*__invoke)(void *);
372 // struct __block_descriptor *__descriptor;
373 // void *__copy_func_helper_decl;
374 // void *__destroy_func_decl;
375 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000376 GenericExtendedBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
377 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000378 IntTy,
379 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000380 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000381 BlockDescPtrTy,
Mike Stump797b6322009-03-05 01:23:13 +0000382 PtrToInt8Ty,
383 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000384 NULL);
385
386 getModule().addTypeName("struct.__block_literal_extended_generic",
387 GenericExtendedBlockLiteralType);
388
389 return GenericExtendedBlockLiteralType;
390}
391
Anders Carlssond5cab542009-02-12 17:55:02 +0000392RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpa5448542009-02-13 15:32:32 +0000393 const BlockPointerType *BPT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000394 E->getCallee()->getType()->getAs<BlockPointerType>();
Mike Stumpa5448542009-02-13 15:32:32 +0000395
Anders Carlssonacfde802009-02-12 00:39:25 +0000396 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
397
398 // Get a pointer to the generic block literal.
399 const llvm::Type *BlockLiteralTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000400 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000401
402 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000403 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000404 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
405
406 // Get the function pointer from the literal.
407 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Anders Carlssonacfde802009-02-12 00:39:25 +0000408
Mike Stumpa5448542009-02-13 15:32:32 +0000409 BlockLiteral =
410 Builder.CreateBitCast(BlockLiteral,
Owen Anderson0032b272009-08-13 21:57:51 +0000411 llvm::PointerType::getUnqual(
412 llvm::Type::getInt8Ty(VMContext)),
Anders Carlssonacfde802009-02-12 00:39:25 +0000413 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000414
Anders Carlssonacfde802009-02-12 00:39:25 +0000415 // Add the block literal.
416 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
417 CallArgList Args;
418 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000419
Anders Carlsson782f3972009-04-08 23:13:16 +0000420 QualType FnType = BPT->getPointeeType();
421
Anders Carlssonacfde802009-02-12 00:39:25 +0000422 // And the rest of the arguments.
Anders Carlsson782f3972009-04-08 23:13:16 +0000423 EmitCallArgs(Args, FnType->getAsFunctionProtoType(),
424 E->arg_begin(), E->arg_end());
Mike Stumpa5448542009-02-13 15:32:32 +0000425
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000426 // Load the function.
427 llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp");
428
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000429 QualType ResultType = FnType->getAsFunctionType()->getResultType();
430
431 const CGFunctionInfo &FnInfo =
432 CGM.getTypes().getFunctionInfo(ResultType, Args);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000433
434 // Cast the function pointer to the right type.
435 const llvm::Type *BlockFTy =
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000436 CGM.getTypes().GetFunctionType(FnInfo, false);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000437
Owen Anderson96e0fc72009-07-29 22:16:19 +0000438 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000439 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
440
Anders Carlssonacfde802009-02-12 00:39:25 +0000441 // And call the block.
Anders Carlssonf8544a42009-04-07 00:20:24 +0000442 return EmitCall(FnInfo, Func, Args);
Anders Carlssonacfde802009-02-12 00:39:25 +0000443}
Anders Carlssond5cab542009-02-12 17:55:02 +0000444
Mike Stumpdab514f2009-03-04 03:23:46 +0000445llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
446 uint64_t &offset = BlockDecls[E->getDecl()];
447
448 const llvm::Type *Ty;
449 Ty = CGM.getTypes().ConvertType(E->getDecl()->getType());
450
Mike Stumpdab514f2009-03-04 03:23:46 +0000451 // See if we have already allocated an offset for this variable.
452 if (offset == 0) {
Mike Stump00470a12009-03-05 08:32:30 +0000453 // Don't run the expensive check, unless we have to.
454 if (!BlockHasCopyDispose && BlockRequiresCopying(E->getType()))
455 BlockHasCopyDispose = true;
Mike Stumpdab514f2009-03-04 03:23:46 +0000456 // if not, allocate one now.
457 offset = getBlockOffset(E);
458 }
459
460 llvm::Value *BlockLiteral = LoadBlockStruct();
461 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
Owen Anderson0032b272009-08-13 21:57:51 +0000462 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Mike Stumpdab514f2009-03-04 03:23:46 +0000463 offset),
Mike Stump58a85142009-03-04 22:48:06 +0000464 "block.literal");
Mike Stumpdab514f2009-03-04 03:23:46 +0000465 if (E->isByRef()) {
466 bool needsCopyDispose = BlockRequiresCopying(E->getType());
467 uint64_t Align = getContext().getDeclAlignInBytes(E->getDecl());
468 const llvm::Type *PtrStructTy
Owen Anderson96e0fc72009-07-29 22:16:19 +0000469 = llvm::PointerType::get(BuildByRefType(E->getType(), Align), 0);
Mike Stumpa8b60c92009-03-21 21:00:35 +0000470 // The block literal will need a copy/destroy helper.
471 BlockHasCopyDispose = true;
Mike Stumpdab514f2009-03-04 03:23:46 +0000472 Ty = PtrStructTy;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000473 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000474 V = Builder.CreateBitCast(V, Ty);
475 V = Builder.CreateLoad(V, false);
476 V = Builder.CreateStructGEP(V, 1, "forwarding");
477 V = Builder.CreateLoad(V, false);
478 V = Builder.CreateBitCast(V, PtrStructTy);
479 V = Builder.CreateStructGEP(V, needsCopyDispose*2 + 4, "x");
480 } else {
Owen Anderson96e0fc72009-07-29 22:16:19 +0000481 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000482 V = Builder.CreateBitCast(V, Ty);
483 }
484 return V;
485}
486
Mike Stump6cc88f72009-03-20 21:53:12 +0000487void CodeGenFunction::BlockForwardSelf() {
488 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
489 ImplicitParamDecl *SelfDecl = OMD->getSelfDecl();
490 llvm::Value *&DMEntry = LocalDeclMap[SelfDecl];
491 if (DMEntry)
492 return;
493 // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care
494 BlockDeclRefExpr *BDRE = new (getContext())
495 BlockDeclRefExpr(SelfDecl,
496 SelfDecl->getType(), SourceLocation(), false);
497 DMEntry = GetAddrOfBlockDecl(BDRE);
498}
499
Mike Stump67a64482009-02-14 22:16:35 +0000500llvm::Constant *
Mike Stump90a90432009-03-04 18:47:42 +0000501BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000502 // Generate the block descriptor.
503 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000504 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
505 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000506
Anders Carlssond5cab542009-02-12 17:55:02 +0000507 llvm::Constant *DescriptorFields[2];
Mike Stumpa5448542009-02-13 15:32:32 +0000508
Anders Carlssond5cab542009-02-12 17:55:02 +0000509 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000510 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000511
Anders Carlssond5cab542009-02-12 17:55:02 +0000512 // Block literal size. For global blocks we just use the size of the generic
513 // block literal struct.
Mike Stumpa5448542009-02-13 15:32:32 +0000514 uint64_t BlockLiteralSize =
Mike Stump9b8a7972009-02-13 15:25:34 +0000515 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Owen Andersona1cf15f2009-07-14 23:10:40 +0000516 DescriptorFields[1] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000517 llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpa5448542009-02-13 15:32:32 +0000518
519 llvm::Constant *DescriptorStruct =
Owen Anderson47a434f2009-08-05 23:18:46 +0000520 llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 2);
Mike Stumpa5448542009-02-13 15:32:32 +0000521
Anders Carlssond5cab542009-02-12 17:55:02 +0000522 llvm::GlobalVariable *Descriptor =
Owen Anderson1c431b32009-07-08 19:05:04 +0000523 new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000524 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000525 DescriptorStruct, "__block_descriptor_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000526
Anders Carlssond5cab542009-02-12 17:55:02 +0000527 // Generate the constants for the block literal.
528 llvm::Constant *LiteralFields[5];
Mike Stumpa5448542009-02-13 15:32:32 +0000529
Mike Stump67a64482009-02-14 22:16:35 +0000530 CodeGenFunction::BlockInfo Info(0, n);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000531 uint64_t subBlockSize, subBlockAlign;
Mike Stumpa99038c2009-02-28 09:07:16 +0000532 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Daniel Dunbard67b09a2009-03-12 03:07:24 +0000533 bool subBlockHasCopyDispose = false;
Mike Stump7f28a9c2009-03-13 23:34:28 +0000534 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000535 llvm::Function *Fn
Mike Stump6cc88f72009-03-20 21:53:12 +0000536 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000537 subBlockSize,
Mike Stump90a90432009-03-04 18:47:42 +0000538 subBlockAlign,
Mike Stump00470a12009-03-05 08:32:30 +0000539 subBlockDeclRefDecls,
540 subBlockHasCopyDispose);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000541 assert(subBlockSize == BlockLiteralSize
542 && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000543
Anders Carlssond5cab542009-02-12 17:55:02 +0000544 // isa
Mike Stumpf99f1d02009-02-13 17:23:42 +0000545 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000546
Anders Carlssond5cab542009-02-12 17:55:02 +0000547 // Flags
Mike Stump00470a12009-03-05 08:32:30 +0000548 LiteralFields[1] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000549 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR);
Mike Stumpa5448542009-02-13 15:32:32 +0000550
Anders Carlssond5cab542009-02-12 17:55:02 +0000551 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000552 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000553
Anders Carlssond5cab542009-02-12 17:55:02 +0000554 // Function
555 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000556
Anders Carlssond5cab542009-02-12 17:55:02 +0000557 // Descriptor
558 LiteralFields[4] = Descriptor;
Mike Stumpa5448542009-02-13 15:32:32 +0000559
560 llvm::Constant *BlockLiteralStruct =
Owen Anderson47a434f2009-08-05 23:18:46 +0000561 llvm::ConstantStruct::get(VMContext, &LiteralFields[0], 5);
Mike Stumpa5448542009-02-13 15:32:32 +0000562
563 llvm::GlobalVariable *BlockLiteral =
Owen Anderson1c431b32009-07-08 19:05:04 +0000564 new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000565 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000566 BlockLiteralStruct, "__block_literal_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000567
Anders Carlssond5cab542009-02-12 17:55:02 +0000568 return BlockLiteral;
569}
570
Mike Stump4e7a1f72009-02-21 20:00:35 +0000571llvm::Value *CodeGenFunction::LoadBlockStruct() {
572 return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self");
573}
574
Mike Stump00470a12009-03-05 08:32:30 +0000575llvm::Function *
576CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
577 const BlockInfo& Info,
Mike Stump6cc88f72009-03-20 21:53:12 +0000578 const Decl *OuterFuncDecl,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000579 llvm::DenseMap<const Decl*, llvm::Value*> ldm,
Mike Stump00470a12009-03-05 08:32:30 +0000580 uint64_t &Size,
581 uint64_t &Align,
582 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls,
583 bool &subBlockHasCopyDispose) {
Devang Patel963dfbd2009-04-15 21:51:44 +0000584
585 // Check if we should generate debug info for this block.
586 if (CGM.getDebugInfo())
587 DebugInfo = CGM.getDebugInfo();
588
Mike Stump7f28a9c2009-03-13 23:34:28 +0000589 // Arrange for local static and local extern declarations to appear
590 // to be local to this function as well, as they are directly referenced
591 // in a block.
592 for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
593 i != ldm.end();
594 ++i) {
595 const VarDecl *VD = dyn_cast<VarDecl>(i->first);
596
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000597 if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage())
Mike Stump7f28a9c2009-03-13 23:34:28 +0000598 LocalDeclMap[VD] = i->second;
599 }
600
Eli Friedman48f91222009-03-28 03:24:54 +0000601 // FIXME: We need to rearrange the code for copy/dispose so we have this
602 // sooner, so we can calculate offsets correctly.
603 if (!BlockHasCopyDispose)
604 BlockOffset = CGM.getTargetData()
605 .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8;
606 else
607 BlockOffset = CGM.getTargetData()
608 .getTypeStoreSizeInBits(CGM.getGenericExtendedBlockLiteralType()) / 8;
609 BlockAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
610
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000611 const FunctionType *BlockFunctionType = BExpr->getFunctionType();
612 QualType ResultType;
613 bool IsVariadic;
Fariborz Jahanianda0895d2009-04-11 18:54:57 +0000614 if (const FunctionProtoType *FTy =
615 dyn_cast<FunctionProtoType>(BlockFunctionType)) {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000616 ResultType = FTy->getResultType();
617 IsVariadic = FTy->isVariadic();
Mike Stumpb3589f42009-07-30 22:28:39 +0000618 } else {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000619 // K&R style block.
620 ResultType = BlockFunctionType->getResultType();
621 IsVariadic = false;
622 }
Mike Stumpa5448542009-02-13 15:32:32 +0000623
Anders Carlssond5cab542009-02-12 17:55:02 +0000624 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000625
Chris Lattner161d36d2009-02-28 19:01:03 +0000626 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlssond5cab542009-02-12 17:55:02 +0000627
628 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000629 ImplicitParamDecl *SelfDecl =
Anders Carlssond5cab542009-02-12 17:55:02 +0000630 ImplicitParamDecl::Create(getContext(), 0,
631 SourceLocation(), 0,
632 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000633
Anders Carlssond5cab542009-02-12 17:55:02 +0000634 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump4e7a1f72009-02-21 20:00:35 +0000635 BlockStructDecl = SelfDecl;
Mike Stumpa5448542009-02-13 15:32:32 +0000636
Steve Naroffe78b8092009-03-13 16:56:44 +0000637 for (BlockDecl::param_const_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000638 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000639 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000640
641 const CGFunctionInfo &FI =
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000642 CGM.getTypes().getFunctionInfo(ResultType, Args);
Anders Carlssond5cab542009-02-12 17:55:02 +0000643
Mike Stump67a64482009-02-14 22:16:35 +0000644 std::string Name = std::string("__") + Info.Name + "_block_invoke_";
Anders Carlssond5cab542009-02-12 17:55:02 +0000645 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000646 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic);
Mike Stumpa5448542009-02-13 15:32:32 +0000647
648 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000649 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
650 Name,
651 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000652
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000653 CGM.SetInternalFunctionAttributes(BD, Fn, FI);
654
Chris Lattner4863db42009-04-23 07:18:56 +0000655 StartFunction(BD, ResultType, Fn, Args,
Chris Lattner161d36d2009-02-28 19:01:03 +0000656 BExpr->getBody()->getLocEnd());
Chris Lattner4863db42009-04-23 07:18:56 +0000657 CurFuncDecl = OuterFuncDecl;
Chris Lattnerb5437d22009-04-23 05:30:27 +0000658 CurCodeDecl = BD;
Chris Lattner161d36d2009-02-28 19:01:03 +0000659 EmitStmt(BExpr->getBody());
660 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlssond5cab542009-02-12 17:55:02 +0000661
Mike Stump8a2b4b12009-02-25 23:33:13 +0000662 // The runtime needs a minimum alignment of a void *.
663 uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
664 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign);
665
Mike Stump4e7a1f72009-02-21 20:00:35 +0000666 Size = BlockOffset;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000667 Align = BlockAlign;
668 subBlockDeclRefDecls = BlockDeclRefDecls;
Mike Stump00470a12009-03-05 08:32:30 +0000669 subBlockHasCopyDispose |= BlockHasCopyDispose;
Anders Carlssond5cab542009-02-12 17:55:02 +0000670 return Fn;
671}
Mike Stumpa99038c2009-02-28 09:07:16 +0000672
Mike Stump08920992009-03-07 02:35:30 +0000673uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
Mike Stumpa99038c2009-02-28 09:07:16 +0000674 const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
675
676 uint64_t Size = getContext().getTypeSize(D->getType()) / 8;
677 uint64_t Align = getContext().getDeclAlignInBytes(D);
678
679 if (BDRE->isByRef()) {
680 Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8;
681 Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
682 }
683
684 assert ((Align > 0) && "alignment must be 1 byte or more");
685
686 uint64_t OldOffset = BlockOffset;
687
688 // Ensure proper alignment, even if it means we have to have a gap
689 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align);
690 BlockAlign = std::max(Align, BlockAlign);
Mike Stump00470a12009-03-05 08:32:30 +0000691
Mike Stumpa99038c2009-02-28 09:07:16 +0000692 uint64_t Pad = BlockOffset - OldOffset;
693 if (Pad) {
Owen Anderson0032b272009-08-13 21:57:51 +0000694 llvm::ArrayType::get(llvm::Type::getInt8Ty(VMContext), Pad);
Mike Stumpa99038c2009-02-28 09:07:16 +0000695 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
696 llvm::APInt(32, Pad),
697 ArrayType::Normal, 0);
698 ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000699 0, QualType(PadTy), 0, VarDecl::None);
Mike Stumpa99038c2009-02-28 09:07:16 +0000700 Expr *E;
701 E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
702 SourceLocation(), false, false);
703 BlockDeclRefDecls.push_back(E);
704 }
705 BlockDeclRefDecls.push_back(BDRE);
706
707 BlockOffset += Size;
708 return BlockOffset-Size;
709}
Mike Stumpdab514f2009-03-04 03:23:46 +0000710
Mike Stump08920992009-03-07 02:35:30 +0000711llvm::Constant *BlockFunction::
712GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000713 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000714 QualType R = getContext().VoidTy;
715
716 FunctionArgList Args;
717 // FIXME: This leaks
Mike Stump08920992009-03-07 02:35:30 +0000718 ImplicitParamDecl *Dst =
719 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
720 getContext().getPointerType(getContext().VoidTy));
721 Args.push_back(std::make_pair(Dst, Dst->getType()));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000722 ImplicitParamDecl *Src =
723 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
724 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000725 Args.push_back(std::make_pair(Src, Src->getType()));
726
727 const CGFunctionInfo &FI =
728 CGM.getTypes().getFunctionInfo(R, Args);
729
Mike Stump3899a7f2009-06-05 23:26:36 +0000730 // FIXME: We'd like to put these into a mergable by content, with
731 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000732 std::string Name = std::string("__copy_helper_block_");
733 CodeGenTypes &Types = CGM.getTypes();
734 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
735
736 llvm::Function *Fn =
737 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
738 Name,
739 &CGM.getModule());
740
741 IdentifierInfo *II
742 = &CGM.getContext().Idents.get("__copy_helper_block_");
743
744 FunctionDecl *FD = FunctionDecl::Create(getContext(),
745 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000746 SourceLocation(), II, R, 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000747 FunctionDecl::Static, false,
748 true);
749 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump08920992009-03-07 02:35:30 +0000750
751 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
752 llvm::Type *PtrPtrT;
Mike Stump08920992009-03-07 02:35:30 +0000753
Mike Stumpb7477cf2009-04-10 18:52:28 +0000754 if (NoteForHelperp) {
755 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump08920992009-03-07 02:35:30 +0000756
Owen Anderson96e0fc72009-07-29 22:16:19 +0000757 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000758 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
759 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump08920992009-03-07 02:35:30 +0000760
Mike Stumpb7477cf2009-04-10 18:52:28 +0000761 llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000762 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000763 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000764 DstObj = Builder.CreateBitCast(DstObj, PtrPtrT);
765 DstObj = Builder.CreateLoad(DstObj);
Mike Stump08920992009-03-07 02:35:30 +0000766
Mike Stumpb7477cf2009-04-10 18:52:28 +0000767 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
768 int flag = NoteForHelper[i].flag;
769 int index = NoteForHelper[i].index;
Mike Stump08920992009-03-07 02:35:30 +0000770
Mike Stumpb7477cf2009-04-10 18:52:28 +0000771 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
772 || NoteForHelper[i].RequiresCopying) {
773 llvm::Value *Srcv = SrcObj;
774 Srcv = Builder.CreateStructGEP(Srcv, index);
775 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000776 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000777 Srcv = Builder.CreateLoad(Srcv);
778
779 llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
780 Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
781
Owen Anderson0032b272009-08-13 21:57:51 +0000782 llvm::Value *N = llvm::ConstantInt::get(
783 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000784 llvm::Value *F = getBlockObjectAssign();
785 Builder.CreateCall3(F, Dstv, Srcv, N);
786 }
Mike Stump08920992009-03-07 02:35:30 +0000787 }
788 }
789
Mike Stumpa4f668f2009-03-06 01:33:24 +0000790 CGF.FinishFunction();
791
Owen Anderson3c4972d2009-07-29 18:54:39 +0000792 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000793}
794
Mike Stumpcf62d392009-03-06 18:42:23 +0000795llvm::Constant *BlockFunction::
Mike Stump08920992009-03-07 02:35:30 +0000796GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
797 const llvm::StructType* T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000798 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000799 QualType R = getContext().VoidTy;
800
801 FunctionArgList Args;
802 // FIXME: This leaks
803 ImplicitParamDecl *Src =
804 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
805 getContext().getPointerType(getContext().VoidTy));
806
807 Args.push_back(std::make_pair(Src, Src->getType()));
808
809 const CGFunctionInfo &FI =
810 CGM.getTypes().getFunctionInfo(R, Args);
811
Mike Stump3899a7f2009-06-05 23:26:36 +0000812 // FIXME: We'd like to put these into a mergable by content, with
813 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000814 std::string Name = std::string("__destroy_helper_block_");
815 CodeGenTypes &Types = CGM.getTypes();
816 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
817
818 llvm::Function *Fn =
819 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
820 Name,
821 &CGM.getModule());
822
823 IdentifierInfo *II
824 = &CGM.getContext().Idents.get("__destroy_helper_block_");
825
826 FunctionDecl *FD = FunctionDecl::Create(getContext(),
827 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000828 SourceLocation(), II, R, 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000829 FunctionDecl::Static, false,
830 true);
831 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1edf6b62009-03-07 02:53:18 +0000832
Mike Stumpb7477cf2009-04-10 18:52:28 +0000833 if (NoteForHelperp) {
834 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump1edf6b62009-03-07 02:53:18 +0000835
Mike Stumpb7477cf2009-04-10 18:52:28 +0000836 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
837 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000838 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000839 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
840 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump1edf6b62009-03-07 02:53:18 +0000841
Mike Stumpb7477cf2009-04-10 18:52:28 +0000842 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
843 int flag = NoteForHelper[i].flag;
844 int index = NoteForHelper[i].index;
Mike Stump1edf6b62009-03-07 02:53:18 +0000845
Mike Stumpb7477cf2009-04-10 18:52:28 +0000846 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
847 || NoteForHelper[i].RequiresCopying) {
848 llvm::Value *Srcv = SrcObj;
849 Srcv = Builder.CreateStructGEP(Srcv, index);
850 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000851 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000852 Srcv = Builder.CreateLoad(Srcv);
853
854 BuildBlockRelease(Srcv, flag);
855 }
Mike Stump1edf6b62009-03-07 02:53:18 +0000856 }
857 }
858
Mike Stumpa4f668f2009-03-06 01:33:24 +0000859 CGF.FinishFunction();
860
Owen Anderson3c4972d2009-07-29 18:54:39 +0000861 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000862}
863
Mike Stump08920992009-03-07 02:35:30 +0000864llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000865 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump08920992009-03-07 02:35:30 +0000866 return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
867 T, NoteForHelper);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000868}
869
Mike Stump08920992009-03-07 02:35:30 +0000870llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000871 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stump08920992009-03-07 02:35:30 +0000872 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000873 T, NoteForHelperp);
Mike Stumpdab514f2009-03-04 03:23:46 +0000874}
Mike Stump797b6322009-03-05 01:23:13 +0000875
Mike Stumpee094222009-03-06 06:12:24 +0000876llvm::Constant *BlockFunction::
877GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
Mike Stump45031c02009-03-06 02:29:21 +0000878 QualType R = getContext().VoidTy;
879
880 FunctionArgList Args;
881 // FIXME: This leaks
Mike Stumpee094222009-03-06 06:12:24 +0000882 ImplicitParamDecl *Dst =
883 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
884 getContext().getPointerType(getContext().VoidTy));
885 Args.push_back(std::make_pair(Dst, Dst->getType()));
886
887 // FIXME: This leaks
Mike Stump45031c02009-03-06 02:29:21 +0000888 ImplicitParamDecl *Src =
889 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
890 getContext().getPointerType(getContext().VoidTy));
Mike Stump45031c02009-03-06 02:29:21 +0000891 Args.push_back(std::make_pair(Src, Src->getType()));
892
893 const CGFunctionInfo &FI =
894 CGM.getTypes().getFunctionInfo(R, Args);
895
896 std::string Name = std::string("__Block_byref_id_object_copy_");
897 CodeGenTypes &Types = CGM.getTypes();
898 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
899
Mike Stump3899a7f2009-06-05 23:26:36 +0000900 // FIXME: We'd like to put these into a mergable by content, with
901 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +0000902 llvm::Function *Fn =
903 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
904 Name,
905 &CGM.getModule());
906
907 IdentifierInfo *II
908 = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
909
910 FunctionDecl *FD = FunctionDecl::Create(getContext(),
911 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000912 SourceLocation(), II, R, 0,
Mike Stump45031c02009-03-06 02:29:21 +0000913 FunctionDecl::Static, false,
914 true);
915 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpee094222009-03-06 06:12:24 +0000916
917 // dst->x
918 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000919 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +0000920 V = Builder.CreateLoad(V);
Mike Stumpee094222009-03-06 06:12:24 +0000921 V = Builder.CreateStructGEP(V, 6, "x");
922 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
923
924 // src->x
925 V = CGF.GetAddrOfLocalVar(Src);
926 V = Builder.CreateLoad(V);
927 V = Builder.CreateBitCast(V, T);
928 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000929 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpee094222009-03-06 06:12:24 +0000930 llvm::Value *SrcObj = Builder.CreateLoad(V);
931
932 flag |= BLOCK_BYREF_CALLER;
933
Owen Anderson0032b272009-08-13 21:57:51 +0000934 llvm::Value *N = llvm::ConstantInt::get(
935 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpee094222009-03-06 06:12:24 +0000936 llvm::Value *F = getBlockObjectAssign();
937 Builder.CreateCall3(F, DstObj, SrcObj, N);
938
Mike Stump45031c02009-03-06 02:29:21 +0000939 CGF.FinishFunction();
940
Owen Anderson3c4972d2009-07-29 18:54:39 +0000941 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +0000942}
943
Mike Stump1851b682009-03-06 04:53:30 +0000944llvm::Constant *
945BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
946 int flag) {
Mike Stump45031c02009-03-06 02:29:21 +0000947 QualType R = getContext().VoidTy;
948
949 FunctionArgList Args;
950 // FIXME: This leaks
951 ImplicitParamDecl *Src =
952 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
953 getContext().getPointerType(getContext().VoidTy));
954
955 Args.push_back(std::make_pair(Src, Src->getType()));
956
957 const CGFunctionInfo &FI =
958 CGM.getTypes().getFunctionInfo(R, Args);
959
960 std::string Name = std::string("__Block_byref_id_object_dispose_");
961 CodeGenTypes &Types = CGM.getTypes();
962 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
963
Mike Stump3899a7f2009-06-05 23:26:36 +0000964 // FIXME: We'd like to put these into a mergable by content, with
965 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +0000966 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(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000976 SourceLocation(), II, R, 0,
Mike Stump45031c02009-03-06 02:29:21 +0000977 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);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000982 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +0000983 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +0000984 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000985 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +0000986 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +0000987
Mike Stump1851b682009-03-06 04:53:30 +0000988 flag |= BLOCK_BYREF_CALLER;
989 BuildBlockRelease(V, flag);
Mike Stump45031c02009-03-06 02:29:21 +0000990 CGF.FinishFunction();
991
Owen Anderson3c4972d2009-07-29 18:54:39 +0000992 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +0000993}
994
Mike Stumpee094222009-03-06 06:12:24 +0000995llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
Mike Stump3899a7f2009-06-05 23:26:36 +0000996 int flag, unsigned Align) {
997 // All alignments below that of pointer alignment collpase down to just
998 // pointer alignment, as we always have at least that much alignment to begin
999 // with.
1000 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
1001 // As an optimization, we only generate a single function of each kind we
1002 // might need. We need a different one for each alignment and for each
1003 // setting of flags. We mix Align and flag to get the kind.
1004 uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag;
1005 llvm::Constant *& Entry = CGM.AssignCache[kind];
1006 if (Entry)
1007 return Entry;
1008 return Entry=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,
Mike Stump3899a7f2009-06-05 23:26:36 +00001012 int flag,
1013 unsigned Align) {
1014 // All alignments below that of pointer alignment collpase down to just
1015 // pointer alignment, as we always have at least that much alignment to begin
1016 // with.
1017 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
1018 // As an optimization, we only generate a single function of each kind we
1019 // might need. We need a different one for each alignment and for each
1020 // setting of flags. We mix Align and flag to get the kind.
1021 uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag;
1022 llvm::Constant *& Entry = CGM.DestroyCache[kind];
1023 if (Entry)
1024 return Entry;
1025 return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001026}
1027
Mike Stump797b6322009-03-05 01:23:13 +00001028llvm::Value *BlockFunction::getBlockObjectDispose() {
1029 if (CGM.BlockObjectDispose == 0) {
1030 const llvm::FunctionType *FTy;
1031 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001032 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stump797b6322009-03-05 01:23:13 +00001033 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001034 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson96e0fc72009-07-29 22:16:19 +00001035 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stump00470a12009-03-05 08:32:30 +00001036 CGM.BlockObjectDispose
Mike Stump797b6322009-03-05 01:23:13 +00001037 = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
1038 }
1039 return CGM.BlockObjectDispose;
1040}
1041
Mike Stumpee094222009-03-06 06:12:24 +00001042llvm::Value *BlockFunction::getBlockObjectAssign() {
1043 if (CGM.BlockObjectAssign == 0) {
1044 const llvm::FunctionType *FTy;
1045 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001046 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stumpee094222009-03-06 06:12:24 +00001047 ArgTys.push_back(PtrToInt8Ty);
1048 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001049 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson96e0fc72009-07-29 22:16:19 +00001050 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stumpee094222009-03-06 06:12:24 +00001051 CGM.BlockObjectAssign
1052 = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
1053 }
1054 return CGM.BlockObjectAssign;
1055}
1056
Mike Stump1851b682009-03-06 04:53:30 +00001057void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Mike Stump797b6322009-03-05 01:23:13 +00001058 llvm::Value *F = getBlockObjectDispose();
Mike Stump1851b682009-03-06 04:53:30 +00001059 llvm::Value *N;
Mike Stump797b6322009-03-05 01:23:13 +00001060 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001061 N = llvm::ConstantInt::get(llvm::Type::getInt32Ty(V->getContext()), flag);
Mike Stump797b6322009-03-05 01:23:13 +00001062 Builder.CreateCall2(F, V, N);
1063}
Mike Stump00470a12009-03-05 08:32:30 +00001064
1065ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stump08920992009-03-07 02:35:30 +00001066
1067BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1068 CGBuilderTy &B)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001069 : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) {
Owen Anderson0032b272009-08-13 21:57:51 +00001070 PtrToInt8Ty = llvm::PointerType::getUnqual(
1071 llvm::Type::getInt8Ty(VMContext));
Mike Stump08920992009-03-07 02:35:30 +00001072
1073 BlockHasCopyDispose = false;
1074}