blob: 8e7a7922adbc0d97b677bc54f8a17eac9c296dcc [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>
Anders Carlssonacfde802009-02-12 00:39:25 +000020using namespace clang;
21using namespace CodeGen;
22
Mike Stumpcf62d392009-03-06 18:42:23 +000023llvm::Constant *CodeGenFunction::
Mike Stumpa803b0e2009-03-25 17:58:24 +000024BuildDescriptorBlockDecl(bool BlockHasCopyDispose, uint64_t Size,
25 const llvm::StructType* Ty,
Mike Stump08920992009-03-07 02:35:30 +000026 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump56129b12009-02-13 16:55:51 +000027 const llvm::Type *UnsignedLongTy
28 = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpe5fee252009-02-13 16:19:19 +000029 llvm::Constant *C;
30 std::vector<llvm::Constant*> Elts;
31
32 // reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +000033 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpe5fee252009-02-13 16:19:19 +000034 Elts.push_back(C);
35
36 // Size
Mike Stumpd6840002009-02-21 20:07:44 +000037 // FIXME: What is the right way to say this doesn't fit? We should give
38 // a user diagnostic in that case. Better fix would be to change the
39 // API to size_t.
Owen Anderson4a28d5d2009-07-24 23:12:58 +000040 C = llvm::ConstantInt::get(UnsignedLongTy, Size);
Mike Stumpe5fee252009-02-13 16:19:19 +000041 Elts.push_back(C);
42
43 if (BlockHasCopyDispose) {
44 // copy_func_helper_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000045 Elts.push_back(BuildCopyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000046
47 // destroy_func_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000048 Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000049 }
50
Owen Anderson08e25242009-07-27 22:29:56 +000051 C = llvm::ConstantStruct::get(Elts);
Mike Stumpe5fee252009-02-13 16:19:19 +000052
Owen Anderson1c431b32009-07-08 19:05:04 +000053 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Mike Stumpe5fee252009-02-13 16:19:19 +000054 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +000055 C, "__block_descriptor_tmp");
Mike Stumpe5fee252009-02-13 16:19:19 +000056 return C;
57}
58
Mike Stump2a998142009-03-04 18:17:45 +000059llvm::Constant *BlockModule::getNSConcreteGlobalBlock() {
Chris Lattnere2f79b62009-05-13 02:50:56 +000060 if (NSConcreteGlobalBlock == 0)
61 NSConcreteGlobalBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
62 "_NSConcreteGlobalBlock");
Mike Stumpf99f1d02009-02-13 17:23:42 +000063 return NSConcreteGlobalBlock;
64}
65
Mike Stump2a998142009-03-04 18:17:45 +000066llvm::Constant *BlockModule::getNSConcreteStackBlock() {
Chris Lattnere2f79b62009-05-13 02:50:56 +000067 if (NSConcreteStackBlock == 0)
68 NSConcreteStackBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
69 "_NSConcreteStackBlock");
Mike Stump59c5b112009-02-13 19:29:27 +000070 return NSConcreteStackBlock;
71}
72
Mike Stump00470a12009-03-05 08:32:30 +000073static void CollectBlockDeclRefInfo(const Stmt *S,
Anders Carlsson4de9fce2009-03-01 01:09:12 +000074 CodeGenFunction::BlockInfo &Info) {
75 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
76 I != E; ++I)
Daniel Dunbar82573ee2009-03-02 07:00:57 +000077 if (*I)
78 CollectBlockDeclRefInfo(*I, Info);
Mike Stump00470a12009-03-05 08:32:30 +000079
Anders Carlsson4de9fce2009-03-01 01:09:12 +000080 if (const BlockDeclRefExpr *DE = dyn_cast<BlockDeclRefExpr>(S)) {
81 // FIXME: Handle enums.
82 if (isa<FunctionDecl>(DE->getDecl()))
83 return;
Mike Stump00470a12009-03-05 08:32:30 +000084
Anders Carlsson4de9fce2009-03-01 01:09:12 +000085 if (DE->isByRef())
86 Info.ByRefDeclRefs.push_back(DE);
87 else
88 Info.ByCopyDeclRefs.push_back(DE);
89 }
90}
91
Mike Stump58a85142009-03-04 22:48:06 +000092/// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be
93/// declared as a global variable instead of on the stack.
Chris Lattnere2f79b62009-05-13 02:50:56 +000094static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info) {
Anders Carlsson4de9fce2009-03-01 01:09:12 +000095 return Info.ByRefDeclRefs.empty() && Info.ByCopyDeclRefs.empty();
96}
97
Mike Stump58a85142009-03-04 22:48:06 +000098// FIXME: Push most into CGM, passing down a few bits, like current function
99// name.
Mike Stump8a2b4b12009-02-25 23:33:13 +0000100llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Mike Stumpe5fee252009-02-13 16:19:19 +0000101
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000102 std::string Name = CurFn->getName();
103 CodeGenFunction::BlockInfo Info(0, Name.c_str());
104 CollectBlockDeclRefInfo(BE->getBody(), Info);
105
106 // Check if the block can be global.
Mike Stump58a85142009-03-04 22:48:06 +0000107 // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like
108 // to just have one code path. We should move this function into CGM and pass
109 // CGF, then we can just check to see if CGF is 0.
Mike Stumpdab514f2009-03-04 03:23:46 +0000110 if (0 && CanBlockBeGlobal(Info))
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000111 return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
Mike Stump00470a12009-03-05 08:32:30 +0000112
113 std::vector<llvm::Constant*> Elts(5);
Mike Stumpe5fee252009-02-13 16:19:19 +0000114 llvm::Constant *C;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000115 llvm::Value *V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000116
Mike Stumpe5fee252009-02-13 16:19:19 +0000117 {
118 // C = BuildBlockStructInitlist();
119 unsigned int flags = BLOCK_HAS_DESCRIPTOR;
120
Mike Stump00470a12009-03-05 08:32:30 +0000121 // We run this first so that we set BlockHasCopyDispose from the entire
122 // block literal.
123 // __invoke
124 uint64_t subBlockSize, subBlockAlign;
125 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Mike Stumpa803b0e2009-03-25 17:58:24 +0000126 bool subBlockHasCopyDispose = false;
Mike Stump00470a12009-03-05 08:32:30 +0000127 llvm::Function *Fn
Mike Stump6cc88f72009-03-20 21:53:12 +0000128 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl, LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000129 subBlockSize,
Mike Stump00470a12009-03-05 08:32:30 +0000130 subBlockAlign,
131 subBlockDeclRefDecls,
Mike Stumpa803b0e2009-03-25 17:58:24 +0000132 subBlockHasCopyDispose);
133 BlockHasCopyDispose |= subBlockHasCopyDispose;
Mike Stump00470a12009-03-05 08:32:30 +0000134 Elts[3] = Fn;
135
Mike Stumpf5408fe2009-05-16 07:57:57 +0000136 // FIXME: Don't use BlockHasCopyDispose, it is set more often then
137 // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); }
Mike Stumpa803b0e2009-03-25 17:58:24 +0000138 if (subBlockHasCopyDispose)
Mike Stumpe5fee252009-02-13 16:19:19 +0000139 flags |= BLOCK_HAS_COPY_DISPOSE;
140
Mike Stump7d6dc4f2009-02-13 20:17:16 +0000141 // __isa
Mike Stump59c5b112009-02-13 19:29:27 +0000142 C = CGM.getNSConcreteStackBlock();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000143 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
Mike Stump00470a12009-03-05 08:32:30 +0000144 Elts[0] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000145
146 // __flags
147 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
148 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000149 C = llvm::ConstantInt::get(IntTy, flags);
Mike Stump00470a12009-03-05 08:32:30 +0000150 Elts[1] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000151
152 // __reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000153 C = llvm::ConstantInt::get(IntTy, 0);
Mike Stump00470a12009-03-05 08:32:30 +0000154 Elts[2] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000155
Mike Stump8a2b4b12009-02-25 23:33:13 +0000156 if (subBlockDeclRefDecls.size() == 0) {
Mike Stumpcf62d392009-03-06 18:42:23 +0000157 // __descriptor
Mike Stumpa803b0e2009-03-25 17:58:24 +0000158 Elts[4] = BuildDescriptorBlockDecl(subBlockHasCopyDispose, subBlockSize, 0, 0);
Mike Stumpcf62d392009-03-06 18:42:23 +0000159
Mike Stump5570cfe2009-03-01 20:07:53 +0000160 // Optimize to being a global block.
161 Elts[0] = CGM.getNSConcreteGlobalBlock();
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000162 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
Mike Stump5570cfe2009-03-01 20:07:53 +0000163
Owen Anderson08e25242009-07-27 22:29:56 +0000164 C = llvm::ConstantStruct::get(Elts);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000165
166 char Name[32];
167 sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
Owen Anderson1c431b32009-07-08 19:05:04 +0000168 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Mike Stump8a2b4b12009-02-25 23:33:13 +0000169 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000170 C, Name);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000171 QualType BPT = BE->getType();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000172 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000173 return C;
174 }
Mike Stump00470a12009-03-05 08:32:30 +0000175
Mike Stump8a2b4b12009-02-25 23:33:13 +0000176 std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size());
Mike Stump08920992009-03-07 02:35:30 +0000177 for (int i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000178 Types[i] = Elts[i]->getType();
Mike Stump08920992009-03-07 02:35:30 +0000179 Types[4] = PtrToInt8Ty;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000180
Mike Stumpa99038c2009-02-28 09:07:16 +0000181 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
182 const Expr *E = subBlockDeclRefDecls[i];
183 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
184 QualType Ty = E->getType();
Mike Stumpdab514f2009-03-04 03:23:46 +0000185 if (BDRE && BDRE->isByRef()) {
186 uint64_t Align = getContext().getDeclAlignInBytes(BDRE->getDecl());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000187 Types[i+5] = VMContext.getPointerType(BuildByRefType(Ty, Align), 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000188 } else
189 Types[i+5] = ConvertType(Ty);
Mike Stumpa99038c2009-02-28 09:07:16 +0000190 }
Mike Stump8a2b4b12009-02-25 23:33:13 +0000191
Owen Andersona1cf15f2009-07-14 23:10:40 +0000192 llvm::StructType *Ty = VMContext.getStructType(Types, true);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000193
194 llvm::AllocaInst *A = CreateTempAlloca(Ty);
195 A->setAlignment(subBlockAlign);
196 V = A;
197
Mike Stump08920992009-03-07 02:35:30 +0000198 std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size());
199 int helpersize = 0;
200
201 for (unsigned i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000202 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
Mike Stump00470a12009-03-05 08:32:30 +0000203
Mike Stump8a2b4b12009-02-25 23:33:13 +0000204 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
205 {
Mike Stumpa99038c2009-02-28 09:07:16 +0000206 // FIXME: Push const down.
207 Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
208 DeclRefExpr *DR;
209 ValueDecl *VD;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000210
Mike Stumpa99038c2009-02-28 09:07:16 +0000211 DR = dyn_cast<DeclRefExpr>(E);
212 // Skip padding.
213 if (DR) continue;
214
215 BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
216 VD = BDRE->getDecl();
217
Mike Stumpdab514f2009-03-04 03:23:46 +0000218 llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp");
Mike Stump08920992009-03-07 02:35:30 +0000219 NoteForHelper[helpersize].index = i+5;
220 NoteForHelper[helpersize].RequiresCopying = BlockRequiresCopying(VD->getType());
221 NoteForHelper[helpersize].flag
222 = VD->getType()->isBlockPointerType() ? BLOCK_FIELD_IS_BLOCK : BLOCK_FIELD_IS_OBJECT;
223
Mike Stumpa99038c2009-02-28 09:07:16 +0000224 if (LocalDeclMap[VD]) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000225 if (BDRE->isByRef()) {
Mike Stump08920992009-03-07 02:35:30 +0000226 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
Mike Stumpf4bc3122009-03-07 06:04:31 +0000227 // FIXME: Someone double check this.
228 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000229 const llvm::Type *Ty = Types[i+5];
230 llvm::Value *Loc = LocalDeclMap[VD];
231 Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
232 Loc = Builder.CreateLoad(Loc, false);
233 Loc = Builder.CreateBitCast(Loc, Ty);
234 Builder.CreateStore(Loc, Addr);
Mike Stump08920992009-03-07 02:35:30 +0000235 ++helpersize;
Mike Stumpdab514f2009-03-04 03:23:46 +0000236 continue;
237 } else
238 E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD),
239 VD->getType(), SourceLocation(),
240 false, false);
Mike Stumpa99038c2009-02-28 09:07:16 +0000241 }
Mike Stumpdab514f2009-03-04 03:23:46 +0000242 if (BDRE->isByRef()) {
Mike Stumpa8b60c92009-03-21 21:00:35 +0000243 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
244 // FIXME: Someone double check this.
245 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpa99038c2009-02-28 09:07:16 +0000246 E = new (getContext())
247 UnaryOperator(E, UnaryOperator::AddrOf,
248 getContext().getPointerType(E->getType()),
249 SourceLocation());
Mike Stumpdab514f2009-03-04 03:23:46 +0000250 }
Mike Stump08920992009-03-07 02:35:30 +0000251 ++helpersize;
Mike Stumpa99038c2009-02-28 09:07:16 +0000252
Mike Stumpa99038c2009-02-28 09:07:16 +0000253 RValue r = EmitAnyExpr(E, Addr, false);
Mike Stumpdab514f2009-03-04 03:23:46 +0000254 if (r.isScalar()) {
255 llvm::Value *Loc = r.getScalarVal();
256 const llvm::Type *Ty = Types[i+5];
257 if (BDRE->isByRef()) {
Mike Stump58a85142009-03-04 22:48:06 +0000258 // E is now the address of the value field, instead, we want the
259 // address of the actual ByRef struct. We optimize this slightly
260 // compared to gcc by not grabbing the forwarding slot as this must
261 // be done during Block_copy for us, and we can postpone the work
262 // until then.
263 uint64_t offset = BlockDecls[BDRE->getDecl()];
Mike Stump00470a12009-03-05 08:32:30 +0000264
Mike Stump58a85142009-03-04 22:48:06 +0000265 llvm::Value *BlockLiteral = LoadBlockStruct();
Mike Stump00470a12009-03-05 08:32:30 +0000266
Mike Stump58a85142009-03-04 22:48:06 +0000267 Loc = Builder.CreateGEP(BlockLiteral,
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000268 llvm::ConstantInt::get(llvm::Type::Int64Ty,
Mike Stump58a85142009-03-04 22:48:06 +0000269 offset),
270 "block.literal");
Owen Andersona1cf15f2009-07-14 23:10:40 +0000271 Ty = VMContext.getPointerType(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000272 Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stump58a85142009-03-04 22:48:06 +0000273 Loc = Builder.CreateLoad(Loc, false);
274 // Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000275 }
276 Builder.CreateStore(Loc, Addr);
277 } else if (r.isComplex())
Mike Stump8a2b4b12009-02-25 23:33:13 +0000278 // FIXME: implement
279 ErrorUnsupported(BE, "complex in block literal");
280 else if (r.isAggregate())
281 ; // Already created into the destination
282 else
283 assert (0 && "bad block variable");
284 // FIXME: Ensure that the offset created by the backend for
285 // the struct matches the previously computed offset in BlockDecls.
286 }
Mike Stump08920992009-03-07 02:35:30 +0000287 NoteForHelper.resize(helpersize);
Mike Stumpcf62d392009-03-06 18:42:23 +0000288
289 // __descriptor
Mike Stumpa803b0e2009-03-25 17:58:24 +0000290 llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockHasCopyDispose,
291 subBlockSize, Ty,
Mike Stump08920992009-03-07 02:35:30 +0000292 &NoteForHelper);
293 Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
294 Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
Mike Stumpe5fee252009-02-13 16:19:19 +0000295 }
Mike Stump00470a12009-03-05 08:32:30 +0000296
Mike Stumpbd65cac2009-02-19 01:01:04 +0000297 QualType BPT = BE->getType();
Mike Stump8a2b4b12009-02-25 23:33:13 +0000298 return Builder.CreateBitCast(V, ConvertType(BPT));
Mike Stumpe5fee252009-02-13 16:19:19 +0000299}
300
301
Mike Stump2a998142009-03-04 18:17:45 +0000302const llvm::Type *BlockModule::getBlockDescriptorType() {
Mike Stumpab695142009-02-13 15:16:56 +0000303 if (BlockDescriptorType)
304 return BlockDescriptorType;
305
Mike Stumpa5448542009-02-13 15:32:32 +0000306 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000307 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000308
Mike Stumpab695142009-02-13 15:16:56 +0000309 // struct __block_descriptor {
310 // unsigned long reserved;
311 // unsigned long block_size;
312 // };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000313 BlockDescriptorType = VMContext.getStructType(UnsignedLongTy,
Mike Stumpa5448542009-02-13 15:32:32 +0000314 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000315 NULL);
316
317 getModule().addTypeName("struct.__block_descriptor",
318 BlockDescriptorType);
319
320 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000321}
322
Mike Stump2a998142009-03-04 18:17:45 +0000323const llvm::Type *BlockModule::getGenericBlockLiteralType() {
Mike Stump9b8a7972009-02-13 15:25:34 +0000324 if (GenericBlockLiteralType)
325 return GenericBlockLiteralType;
326
Mike Stumpa5448542009-02-13 15:32:32 +0000327 const llvm::Type *BlockDescPtrTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +0000328 VMContext.getPointerTypeUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000329
Mike Stump7cbb3602009-02-13 16:01:35 +0000330 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
331 getTypes().ConvertType(getContext().IntTy));
332
Mike Stump9b8a7972009-02-13 15:25:34 +0000333 // struct __block_literal_generic {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000334 // void *__isa;
335 // int __flags;
336 // int __reserved;
337 // void (*__invoke)(void *);
338 // struct __block_descriptor *__descriptor;
Mike Stump9b8a7972009-02-13 15:25:34 +0000339 // };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000340 GenericBlockLiteralType = VMContext.getStructType(PtrToInt8Ty,
Mike Stump7cbb3602009-02-13 16:01:35 +0000341 IntTy,
342 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000343 PtrToInt8Ty,
Mike Stump9b8a7972009-02-13 15:25:34 +0000344 BlockDescPtrTy,
345 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000346
Mike Stump9b8a7972009-02-13 15:25:34 +0000347 getModule().addTypeName("struct.__block_literal_generic",
348 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000349
Mike Stump9b8a7972009-02-13 15:25:34 +0000350 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000351}
352
Mike Stump2a998142009-03-04 18:17:45 +0000353const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000354 if (GenericExtendedBlockLiteralType)
355 return GenericExtendedBlockLiteralType;
356
Mike Stumpbd65cac2009-02-19 01:01:04 +0000357 const llvm::Type *BlockDescPtrTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +0000358 VMContext.getPointerTypeUnqual(getBlockDescriptorType());
Mike Stumpbd65cac2009-02-19 01:01:04 +0000359
360 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
361 getTypes().ConvertType(getContext().IntTy));
362
363 // struct __block_literal_generic {
364 // void *__isa;
365 // int __flags;
366 // int __reserved;
367 // void (*__invoke)(void *);
368 // struct __block_descriptor *__descriptor;
369 // void *__copy_func_helper_decl;
370 // void *__destroy_func_decl;
371 // };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000372 GenericExtendedBlockLiteralType = VMContext.getStructType(PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000373 IntTy,
374 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000375 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000376 BlockDescPtrTy,
Mike Stump797b6322009-03-05 01:23:13 +0000377 PtrToInt8Ty,
378 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000379 NULL);
380
381 getModule().addTypeName("struct.__block_literal_extended_generic",
382 GenericExtendedBlockLiteralType);
383
384 return GenericExtendedBlockLiteralType;
385}
386
Anders Carlssond5cab542009-02-12 17:55:02 +0000387RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpa5448542009-02-13 15:32:32 +0000388 const BlockPointerType *BPT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000389 E->getCallee()->getType()->getAs<BlockPointerType>();
Mike Stumpa5448542009-02-13 15:32:32 +0000390
Anders Carlssonacfde802009-02-12 00:39:25 +0000391 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
392
393 // Get a pointer to the generic block literal.
394 const llvm::Type *BlockLiteralTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +0000395 VMContext.getPointerTypeUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000396
397 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000398 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000399 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
400
401 // Get the function pointer from the literal.
402 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Anders Carlssonacfde802009-02-12 00:39:25 +0000403
Mike Stumpa5448542009-02-13 15:32:32 +0000404 BlockLiteral =
405 Builder.CreateBitCast(BlockLiteral,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000406 VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty),
Anders Carlssonacfde802009-02-12 00:39:25 +0000407 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000408
Anders Carlssonacfde802009-02-12 00:39:25 +0000409 // Add the block literal.
410 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
411 CallArgList Args;
412 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000413
Anders Carlsson782f3972009-04-08 23:13:16 +0000414 QualType FnType = BPT->getPointeeType();
415
Anders Carlssonacfde802009-02-12 00:39:25 +0000416 // And the rest of the arguments.
Anders Carlsson782f3972009-04-08 23:13:16 +0000417 EmitCallArgs(Args, FnType->getAsFunctionProtoType(),
418 E->arg_begin(), E->arg_end());
Mike Stumpa5448542009-02-13 15:32:32 +0000419
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000420 // Load the function.
421 llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp");
422
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000423 QualType ResultType = FnType->getAsFunctionType()->getResultType();
424
425 const CGFunctionInfo &FnInfo =
426 CGM.getTypes().getFunctionInfo(ResultType, Args);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000427
428 // Cast the function pointer to the right type.
429 const llvm::Type *BlockFTy =
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000430 CGM.getTypes().GetFunctionType(FnInfo, false);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000431
Owen Andersona1cf15f2009-07-14 23:10:40 +0000432 const llvm::Type *BlockFTyPtr = VMContext.getPointerTypeUnqual(BlockFTy);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000433 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
434
Anders Carlssonacfde802009-02-12 00:39:25 +0000435 // And call the block.
Anders Carlssonf8544a42009-04-07 00:20:24 +0000436 return EmitCall(FnInfo, Func, Args);
Anders Carlssonacfde802009-02-12 00:39:25 +0000437}
Anders Carlssond5cab542009-02-12 17:55:02 +0000438
Mike Stumpdab514f2009-03-04 03:23:46 +0000439llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
440 uint64_t &offset = BlockDecls[E->getDecl()];
441
442 const llvm::Type *Ty;
443 Ty = CGM.getTypes().ConvertType(E->getDecl()->getType());
444
Mike Stumpdab514f2009-03-04 03:23:46 +0000445 // See if we have already allocated an offset for this variable.
446 if (offset == 0) {
Mike Stump00470a12009-03-05 08:32:30 +0000447 // Don't run the expensive check, unless we have to.
448 if (!BlockHasCopyDispose && BlockRequiresCopying(E->getType()))
449 BlockHasCopyDispose = true;
Mike Stumpdab514f2009-03-04 03:23:46 +0000450 // if not, allocate one now.
451 offset = getBlockOffset(E);
452 }
453
454 llvm::Value *BlockLiteral = LoadBlockStruct();
455 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000456 llvm::ConstantInt::get(llvm::Type::Int64Ty,
Mike Stumpdab514f2009-03-04 03:23:46 +0000457 offset),
Mike Stump58a85142009-03-04 22:48:06 +0000458 "block.literal");
Mike Stumpdab514f2009-03-04 03:23:46 +0000459 if (E->isByRef()) {
460 bool needsCopyDispose = BlockRequiresCopying(E->getType());
461 uint64_t Align = getContext().getDeclAlignInBytes(E->getDecl());
462 const llvm::Type *PtrStructTy
Owen Andersona1cf15f2009-07-14 23:10:40 +0000463 = VMContext.getPointerType(BuildByRefType(E->getType(), Align), 0);
Mike Stumpa8b60c92009-03-21 21:00:35 +0000464 // The block literal will need a copy/destroy helper.
465 BlockHasCopyDispose = true;
Mike Stumpdab514f2009-03-04 03:23:46 +0000466 Ty = PtrStructTy;
Owen Andersona1cf15f2009-07-14 23:10:40 +0000467 Ty = VMContext.getPointerType(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000468 V = Builder.CreateBitCast(V, Ty);
469 V = Builder.CreateLoad(V, false);
470 V = Builder.CreateStructGEP(V, 1, "forwarding");
471 V = Builder.CreateLoad(V, false);
472 V = Builder.CreateBitCast(V, PtrStructTy);
473 V = Builder.CreateStructGEP(V, needsCopyDispose*2 + 4, "x");
474 } else {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000475 Ty = VMContext.getPointerType(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000476 V = Builder.CreateBitCast(V, Ty);
477 }
478 return V;
479}
480
Mike Stump6cc88f72009-03-20 21:53:12 +0000481void CodeGenFunction::BlockForwardSelf() {
482 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
483 ImplicitParamDecl *SelfDecl = OMD->getSelfDecl();
484 llvm::Value *&DMEntry = LocalDeclMap[SelfDecl];
485 if (DMEntry)
486 return;
487 // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care
488 BlockDeclRefExpr *BDRE = new (getContext())
489 BlockDeclRefExpr(SelfDecl,
490 SelfDecl->getType(), SourceLocation(), false);
491 DMEntry = GetAddrOfBlockDecl(BDRE);
492}
493
Mike Stump67a64482009-02-14 22:16:35 +0000494llvm::Constant *
Mike Stump90a90432009-03-04 18:47:42 +0000495BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000496 // Generate the block descriptor.
497 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000498 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
499 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000500
Anders Carlssond5cab542009-02-12 17:55:02 +0000501 llvm::Constant *DescriptorFields[2];
Mike Stumpa5448542009-02-13 15:32:32 +0000502
Anders Carlssond5cab542009-02-12 17:55:02 +0000503 // Reserved
Owen Anderson69243822009-07-13 04:10:07 +0000504 DescriptorFields[0] = getModule().getContext().getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000505
Anders Carlssond5cab542009-02-12 17:55:02 +0000506 // Block literal size. For global blocks we just use the size of the generic
507 // block literal struct.
Mike Stumpa5448542009-02-13 15:32:32 +0000508 uint64_t BlockLiteralSize =
Mike Stump9b8a7972009-02-13 15:25:34 +0000509 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Owen Andersona1cf15f2009-07-14 23:10:40 +0000510 DescriptorFields[1] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000511 llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpa5448542009-02-13 15:32:32 +0000512
513 llvm::Constant *DescriptorStruct =
Owen Anderson08e25242009-07-27 22:29:56 +0000514 llvm::ConstantStruct::get(&DescriptorFields[0], 2);
Mike Stumpa5448542009-02-13 15:32:32 +0000515
Anders Carlssond5cab542009-02-12 17:55:02 +0000516 llvm::GlobalVariable *Descriptor =
Owen Anderson1c431b32009-07-08 19:05:04 +0000517 new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000518 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000519 DescriptorStruct, "__block_descriptor_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000520
Anders Carlssond5cab542009-02-12 17:55:02 +0000521 // Generate the constants for the block literal.
522 llvm::Constant *LiteralFields[5];
Mike Stumpa5448542009-02-13 15:32:32 +0000523
Mike Stump67a64482009-02-14 22:16:35 +0000524 CodeGenFunction::BlockInfo Info(0, n);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000525 uint64_t subBlockSize, subBlockAlign;
Mike Stumpa99038c2009-02-28 09:07:16 +0000526 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Daniel Dunbard67b09a2009-03-12 03:07:24 +0000527 bool subBlockHasCopyDispose = false;
Mike Stump7f28a9c2009-03-13 23:34:28 +0000528 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000529 llvm::Function *Fn
Mike Stump6cc88f72009-03-20 21:53:12 +0000530 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000531 subBlockSize,
Mike Stump90a90432009-03-04 18:47:42 +0000532 subBlockAlign,
Mike Stump00470a12009-03-05 08:32:30 +0000533 subBlockDeclRefDecls,
534 subBlockHasCopyDispose);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000535 assert(subBlockSize == BlockLiteralSize
536 && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000537
Anders Carlssond5cab542009-02-12 17:55:02 +0000538 // isa
Mike Stumpf99f1d02009-02-13 17:23:42 +0000539 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000540
Anders Carlssond5cab542009-02-12 17:55:02 +0000541 // Flags
Mike Stump00470a12009-03-05 08:32:30 +0000542 LiteralFields[1] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000543 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR);
Mike Stumpa5448542009-02-13 15:32:32 +0000544
Anders Carlssond5cab542009-02-12 17:55:02 +0000545 // Reserved
Owen Anderson69243822009-07-13 04:10:07 +0000546 LiteralFields[2] = getModule().getContext().getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000547
Anders Carlssond5cab542009-02-12 17:55:02 +0000548 // Function
549 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000550
Anders Carlssond5cab542009-02-12 17:55:02 +0000551 // Descriptor
552 LiteralFields[4] = Descriptor;
Mike Stumpa5448542009-02-13 15:32:32 +0000553
554 llvm::Constant *BlockLiteralStruct =
Owen Anderson08e25242009-07-27 22:29:56 +0000555 llvm::ConstantStruct::get(&LiteralFields[0], 5);
Mike Stumpa5448542009-02-13 15:32:32 +0000556
557 llvm::GlobalVariable *BlockLiteral =
Owen Anderson1c431b32009-07-08 19:05:04 +0000558 new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000559 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000560 BlockLiteralStruct, "__block_literal_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000561
Anders Carlssond5cab542009-02-12 17:55:02 +0000562 return BlockLiteral;
563}
564
Mike Stump4e7a1f72009-02-21 20:00:35 +0000565llvm::Value *CodeGenFunction::LoadBlockStruct() {
566 return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self");
567}
568
Mike Stump00470a12009-03-05 08:32:30 +0000569llvm::Function *
570CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
571 const BlockInfo& Info,
Mike Stump6cc88f72009-03-20 21:53:12 +0000572 const Decl *OuterFuncDecl,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000573 llvm::DenseMap<const Decl*, llvm::Value*> ldm,
Mike Stump00470a12009-03-05 08:32:30 +0000574 uint64_t &Size,
575 uint64_t &Align,
576 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls,
577 bool &subBlockHasCopyDispose) {
Devang Patel963dfbd2009-04-15 21:51:44 +0000578
579 // Check if we should generate debug info for this block.
580 if (CGM.getDebugInfo())
581 DebugInfo = CGM.getDebugInfo();
582
Mike Stump7f28a9c2009-03-13 23:34:28 +0000583 // Arrange for local static and local extern declarations to appear
584 // to be local to this function as well, as they are directly referenced
585 // in a block.
586 for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
587 i != ldm.end();
588 ++i) {
589 const VarDecl *VD = dyn_cast<VarDecl>(i->first);
590
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000591 if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage())
Mike Stump7f28a9c2009-03-13 23:34:28 +0000592 LocalDeclMap[VD] = i->second;
593 }
594
Eli Friedman48f91222009-03-28 03:24:54 +0000595 // FIXME: We need to rearrange the code for copy/dispose so we have this
596 // sooner, so we can calculate offsets correctly.
597 if (!BlockHasCopyDispose)
598 BlockOffset = CGM.getTargetData()
599 .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8;
600 else
601 BlockOffset = CGM.getTargetData()
602 .getTypeStoreSizeInBits(CGM.getGenericExtendedBlockLiteralType()) / 8;
603 BlockAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
604
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000605 const FunctionType *BlockFunctionType = BExpr->getFunctionType();
606 QualType ResultType;
607 bool IsVariadic;
Fariborz Jahanianda0895d2009-04-11 18:54:57 +0000608 if (const FunctionProtoType *FTy =
609 dyn_cast<FunctionProtoType>(BlockFunctionType)) {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000610 ResultType = FTy->getResultType();
611 IsVariadic = FTy->isVariadic();
612 }
613 else {
614 // K&R style block.
615 ResultType = BlockFunctionType->getResultType();
616 IsVariadic = false;
617 }
Mike Stumpa5448542009-02-13 15:32:32 +0000618
Anders Carlssond5cab542009-02-12 17:55:02 +0000619 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000620
Chris Lattner161d36d2009-02-28 19:01:03 +0000621 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlssond5cab542009-02-12 17:55:02 +0000622
623 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000624 ImplicitParamDecl *SelfDecl =
Anders Carlssond5cab542009-02-12 17:55:02 +0000625 ImplicitParamDecl::Create(getContext(), 0,
626 SourceLocation(), 0,
627 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000628
Anders Carlssond5cab542009-02-12 17:55:02 +0000629 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump4e7a1f72009-02-21 20:00:35 +0000630 BlockStructDecl = SelfDecl;
Mike Stumpa5448542009-02-13 15:32:32 +0000631
Steve Naroffe78b8092009-03-13 16:56:44 +0000632 for (BlockDecl::param_const_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000633 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000634 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000635
636 const CGFunctionInfo &FI =
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000637 CGM.getTypes().getFunctionInfo(ResultType, Args);
Anders Carlssond5cab542009-02-12 17:55:02 +0000638
Mike Stump67a64482009-02-14 22:16:35 +0000639 std::string Name = std::string("__") + Info.Name + "_block_invoke_";
Anders Carlssond5cab542009-02-12 17:55:02 +0000640 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000641 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic);
Mike Stumpa5448542009-02-13 15:32:32 +0000642
643 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000644 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
645 Name,
646 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000647
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000648 CGM.SetInternalFunctionAttributes(BD, Fn, FI);
649
Chris Lattner4863db42009-04-23 07:18:56 +0000650 StartFunction(BD, ResultType, Fn, Args,
Chris Lattner161d36d2009-02-28 19:01:03 +0000651 BExpr->getBody()->getLocEnd());
Chris Lattner4863db42009-04-23 07:18:56 +0000652 CurFuncDecl = OuterFuncDecl;
Chris Lattnerb5437d22009-04-23 05:30:27 +0000653 CurCodeDecl = BD;
Chris Lattner161d36d2009-02-28 19:01:03 +0000654 EmitStmt(BExpr->getBody());
655 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlssond5cab542009-02-12 17:55:02 +0000656
Mike Stump8a2b4b12009-02-25 23:33:13 +0000657 // The runtime needs a minimum alignment of a void *.
658 uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
659 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign);
660
Mike Stump4e7a1f72009-02-21 20:00:35 +0000661 Size = BlockOffset;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000662 Align = BlockAlign;
663 subBlockDeclRefDecls = BlockDeclRefDecls;
Mike Stump00470a12009-03-05 08:32:30 +0000664 subBlockHasCopyDispose |= BlockHasCopyDispose;
Anders Carlssond5cab542009-02-12 17:55:02 +0000665 return Fn;
666}
Mike Stumpa99038c2009-02-28 09:07:16 +0000667
Mike Stump08920992009-03-07 02:35:30 +0000668uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
Mike Stumpa99038c2009-02-28 09:07:16 +0000669 const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
670
671 uint64_t Size = getContext().getTypeSize(D->getType()) / 8;
672 uint64_t Align = getContext().getDeclAlignInBytes(D);
673
674 if (BDRE->isByRef()) {
675 Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8;
676 Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
677 }
678
679 assert ((Align > 0) && "alignment must be 1 byte or more");
680
681 uint64_t OldOffset = BlockOffset;
682
683 // Ensure proper alignment, even if it means we have to have a gap
684 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align);
685 BlockAlign = std::max(Align, BlockAlign);
Mike Stump00470a12009-03-05 08:32:30 +0000686
Mike Stumpa99038c2009-02-28 09:07:16 +0000687 uint64_t Pad = BlockOffset - OldOffset;
688 if (Pad) {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000689 VMContext.getArrayType(llvm::Type::Int8Ty, Pad);
Mike Stumpa99038c2009-02-28 09:07:16 +0000690 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
691 llvm::APInt(32, Pad),
692 ArrayType::Normal, 0);
693 ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
694 0, QualType(PadTy), VarDecl::None,
695 SourceLocation());
696 Expr *E;
697 E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
698 SourceLocation(), false, false);
699 BlockDeclRefDecls.push_back(E);
700 }
701 BlockDeclRefDecls.push_back(BDRE);
702
703 BlockOffset += Size;
704 return BlockOffset-Size;
705}
Mike Stumpdab514f2009-03-04 03:23:46 +0000706
Mike Stump08920992009-03-07 02:35:30 +0000707llvm::Constant *BlockFunction::
708GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000709 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000710 QualType R = getContext().VoidTy;
711
712 FunctionArgList Args;
713 // FIXME: This leaks
Mike Stump08920992009-03-07 02:35:30 +0000714 ImplicitParamDecl *Dst =
715 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
716 getContext().getPointerType(getContext().VoidTy));
717 Args.push_back(std::make_pair(Dst, Dst->getType()));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000718 ImplicitParamDecl *Src =
719 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
720 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000721 Args.push_back(std::make_pair(Src, Src->getType()));
722
723 const CGFunctionInfo &FI =
724 CGM.getTypes().getFunctionInfo(R, Args);
725
Mike Stump3899a7f2009-06-05 23:26:36 +0000726 // FIXME: We'd like to put these into a mergable by content, with
727 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000728 std::string Name = std::string("__copy_helper_block_");
729 CodeGenTypes &Types = CGM.getTypes();
730 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
731
732 llvm::Function *Fn =
733 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
734 Name,
735 &CGM.getModule());
736
737 IdentifierInfo *II
738 = &CGM.getContext().Idents.get("__copy_helper_block_");
739
740 FunctionDecl *FD = FunctionDecl::Create(getContext(),
741 getContext().getTranslationUnitDecl(),
742 SourceLocation(), II, R,
743 FunctionDecl::Static, false,
744 true);
745 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump08920992009-03-07 02:35:30 +0000746
747 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
748 llvm::Type *PtrPtrT;
Mike Stump08920992009-03-07 02:35:30 +0000749
Mike Stumpb7477cf2009-04-10 18:52:28 +0000750 if (NoteForHelperp) {
751 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump08920992009-03-07 02:35:30 +0000752
Owen Andersona1cf15f2009-07-14 23:10:40 +0000753 PtrPtrT = VMContext.getPointerType(VMContext.getPointerType(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000754 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
755 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump08920992009-03-07 02:35:30 +0000756
Mike Stumpb7477cf2009-04-10 18:52:28 +0000757 llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000758 llvm::Type *PtrPtrT;
Owen Andersona1cf15f2009-07-14 23:10:40 +0000759 PtrPtrT = VMContext.getPointerType(VMContext.getPointerType(T, 0), 0);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000760 DstObj = Builder.CreateBitCast(DstObj, PtrPtrT);
761 DstObj = Builder.CreateLoad(DstObj);
Mike Stump08920992009-03-07 02:35:30 +0000762
Mike Stumpb7477cf2009-04-10 18:52:28 +0000763 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
764 int flag = NoteForHelper[i].flag;
765 int index = NoteForHelper[i].index;
Mike Stump08920992009-03-07 02:35:30 +0000766
Mike Stumpb7477cf2009-04-10 18:52:28 +0000767 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
768 || NoteForHelper[i].RequiresCopying) {
769 llvm::Value *Srcv = SrcObj;
770 Srcv = Builder.CreateStructGEP(Srcv, index);
771 Srcv = Builder.CreateBitCast(Srcv,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000772 VMContext.getPointerType(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000773 Srcv = Builder.CreateLoad(Srcv);
774
775 llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
776 Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
777
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000778 llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000779 llvm::Value *F = getBlockObjectAssign();
780 Builder.CreateCall3(F, Dstv, Srcv, N);
781 }
Mike Stump08920992009-03-07 02:35:30 +0000782 }
783 }
784
Mike Stumpa4f668f2009-03-06 01:33:24 +0000785 CGF.FinishFunction();
786
Owen Anderson3c4972d2009-07-29 18:54:39 +0000787 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000788}
789
Mike Stumpcf62d392009-03-06 18:42:23 +0000790llvm::Constant *BlockFunction::
Mike Stump08920992009-03-07 02:35:30 +0000791GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
792 const llvm::StructType* T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000793 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000794 QualType R = getContext().VoidTy;
795
796 FunctionArgList Args;
797 // FIXME: This leaks
798 ImplicitParamDecl *Src =
799 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
800 getContext().getPointerType(getContext().VoidTy));
801
802 Args.push_back(std::make_pair(Src, Src->getType()));
803
804 const CGFunctionInfo &FI =
805 CGM.getTypes().getFunctionInfo(R, Args);
806
Mike Stump3899a7f2009-06-05 23:26:36 +0000807 // FIXME: We'd like to put these into a mergable by content, with
808 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000809 std::string Name = std::string("__destroy_helper_block_");
810 CodeGenTypes &Types = CGM.getTypes();
811 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
812
813 llvm::Function *Fn =
814 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
815 Name,
816 &CGM.getModule());
817
818 IdentifierInfo *II
819 = &CGM.getContext().Idents.get("__destroy_helper_block_");
820
821 FunctionDecl *FD = FunctionDecl::Create(getContext(),
822 getContext().getTranslationUnitDecl(),
823 SourceLocation(), II, R,
824 FunctionDecl::Static, false,
825 true);
826 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1edf6b62009-03-07 02:53:18 +0000827
Mike Stumpb7477cf2009-04-10 18:52:28 +0000828 if (NoteForHelperp) {
829 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump1edf6b62009-03-07 02:53:18 +0000830
Mike Stumpb7477cf2009-04-10 18:52:28 +0000831 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
832 llvm::Type *PtrPtrT;
Owen Andersona1cf15f2009-07-14 23:10:40 +0000833 PtrPtrT = VMContext.getPointerType(VMContext.getPointerType(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000834 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
835 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump1edf6b62009-03-07 02:53:18 +0000836
Mike Stumpb7477cf2009-04-10 18:52:28 +0000837 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
838 int flag = NoteForHelper[i].flag;
839 int index = NoteForHelper[i].index;
Mike Stump1edf6b62009-03-07 02:53:18 +0000840
Mike Stumpb7477cf2009-04-10 18:52:28 +0000841 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
842 || NoteForHelper[i].RequiresCopying) {
843 llvm::Value *Srcv = SrcObj;
844 Srcv = Builder.CreateStructGEP(Srcv, index);
845 Srcv = Builder.CreateBitCast(Srcv,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000846 VMContext.getPointerType(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000847 Srcv = Builder.CreateLoad(Srcv);
848
849 BuildBlockRelease(Srcv, flag);
850 }
Mike Stump1edf6b62009-03-07 02:53:18 +0000851 }
852 }
853
Mike Stumpa4f668f2009-03-06 01:33:24 +0000854 CGF.FinishFunction();
855
Owen Anderson3c4972d2009-07-29 18:54:39 +0000856 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000857}
858
Mike Stump08920992009-03-07 02:35:30 +0000859llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000860 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump08920992009-03-07 02:35:30 +0000861 return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
862 T, NoteForHelper);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000863}
864
Mike Stump08920992009-03-07 02:35:30 +0000865llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000866 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stump08920992009-03-07 02:35:30 +0000867 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000868 T, NoteForHelperp);
Mike Stumpdab514f2009-03-04 03:23:46 +0000869}
Mike Stump797b6322009-03-05 01:23:13 +0000870
Mike Stumpee094222009-03-06 06:12:24 +0000871llvm::Constant *BlockFunction::
872GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
Mike Stump45031c02009-03-06 02:29:21 +0000873 QualType R = getContext().VoidTy;
874
875 FunctionArgList Args;
876 // FIXME: This leaks
Mike Stumpee094222009-03-06 06:12:24 +0000877 ImplicitParamDecl *Dst =
878 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
879 getContext().getPointerType(getContext().VoidTy));
880 Args.push_back(std::make_pair(Dst, Dst->getType()));
881
882 // FIXME: This leaks
Mike Stump45031c02009-03-06 02:29:21 +0000883 ImplicitParamDecl *Src =
884 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
885 getContext().getPointerType(getContext().VoidTy));
Mike Stump45031c02009-03-06 02:29:21 +0000886 Args.push_back(std::make_pair(Src, Src->getType()));
887
888 const CGFunctionInfo &FI =
889 CGM.getTypes().getFunctionInfo(R, Args);
890
891 std::string Name = std::string("__Block_byref_id_object_copy_");
892 CodeGenTypes &Types = CGM.getTypes();
893 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
894
Mike Stump3899a7f2009-06-05 23:26:36 +0000895 // FIXME: We'd like to put these into a mergable by content, with
896 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +0000897 llvm::Function *Fn =
898 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
899 Name,
900 &CGM.getModule());
901
902 IdentifierInfo *II
903 = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
904
905 FunctionDecl *FD = FunctionDecl::Create(getContext(),
906 getContext().getTranslationUnitDecl(),
907 SourceLocation(), II, R,
908 FunctionDecl::Static, false,
909 true);
910 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpee094222009-03-06 06:12:24 +0000911
912 // dst->x
913 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000914 V = Builder.CreateBitCast(V, VMContext.getPointerType(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +0000915 V = Builder.CreateLoad(V);
Mike Stumpee094222009-03-06 06:12:24 +0000916 V = Builder.CreateStructGEP(V, 6, "x");
917 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
918
919 // src->x
920 V = CGF.GetAddrOfLocalVar(Src);
921 V = Builder.CreateLoad(V);
922 V = Builder.CreateBitCast(V, T);
923 V = Builder.CreateStructGEP(V, 6, "x");
Owen Andersona1cf15f2009-07-14 23:10:40 +0000924 V = Builder.CreateBitCast(V, VMContext.getPointerType(PtrToInt8Ty, 0));
Mike Stumpee094222009-03-06 06:12:24 +0000925 llvm::Value *SrcObj = Builder.CreateLoad(V);
926
927 flag |= BLOCK_BYREF_CALLER;
928
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000929 llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag);
Mike Stumpee094222009-03-06 06:12:24 +0000930 llvm::Value *F = getBlockObjectAssign();
931 Builder.CreateCall3(F, DstObj, SrcObj, N);
932
Mike Stump45031c02009-03-06 02:29:21 +0000933 CGF.FinishFunction();
934
Owen Anderson3c4972d2009-07-29 18:54:39 +0000935 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +0000936}
937
Mike Stump1851b682009-03-06 04:53:30 +0000938llvm::Constant *
939BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
940 int flag) {
Mike Stump45031c02009-03-06 02:29:21 +0000941 QualType R = getContext().VoidTy;
942
943 FunctionArgList Args;
944 // FIXME: This leaks
945 ImplicitParamDecl *Src =
946 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
947 getContext().getPointerType(getContext().VoidTy));
948
949 Args.push_back(std::make_pair(Src, Src->getType()));
950
951 const CGFunctionInfo &FI =
952 CGM.getTypes().getFunctionInfo(R, Args);
953
954 std::string Name = std::string("__Block_byref_id_object_dispose_");
955 CodeGenTypes &Types = CGM.getTypes();
956 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
957
Mike Stump3899a7f2009-06-05 23:26:36 +0000958 // FIXME: We'd like to put these into a mergable by content, with
959 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +0000960 llvm::Function *Fn =
961 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
962 Name,
963 &CGM.getModule());
964
965 IdentifierInfo *II
966 = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
967
968 FunctionDecl *FD = FunctionDecl::Create(getContext(),
969 getContext().getTranslationUnitDecl(),
970 SourceLocation(), II, R,
971 FunctionDecl::Static, false,
972 true);
973 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1851b682009-03-06 04:53:30 +0000974
975 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000976 V = Builder.CreateBitCast(V, VMContext.getPointerType(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +0000977 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +0000978 V = Builder.CreateStructGEP(V, 6, "x");
Owen Andersona1cf15f2009-07-14 23:10:40 +0000979 V = Builder.CreateBitCast(V, VMContext.getPointerType(PtrToInt8Ty, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +0000980 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +0000981
Mike Stump1851b682009-03-06 04:53:30 +0000982 flag |= BLOCK_BYREF_CALLER;
983 BuildBlockRelease(V, flag);
Mike Stump45031c02009-03-06 02:29:21 +0000984 CGF.FinishFunction();
985
Owen Anderson3c4972d2009-07-29 18:54:39 +0000986 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +0000987}
988
Mike Stumpee094222009-03-06 06:12:24 +0000989llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
Mike Stump3899a7f2009-06-05 23:26:36 +0000990 int flag, unsigned Align) {
991 // All alignments below that of pointer alignment collpase down to just
992 // pointer alignment, as we always have at least that much alignment to begin
993 // with.
994 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
995 // As an optimization, we only generate a single function of each kind we
996 // might need. We need a different one for each alignment and for each
997 // setting of flags. We mix Align and flag to get the kind.
998 uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag;
999 llvm::Constant *& Entry = CGM.AssignCache[kind];
1000 if (Entry)
1001 return Entry;
1002 return Entry=CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001003}
1004
Mike Stump1851b682009-03-06 04:53:30 +00001005llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
Mike Stump3899a7f2009-06-05 23:26:36 +00001006 int flag,
1007 unsigned Align) {
1008 // All alignments below that of pointer alignment collpase down to just
1009 // pointer alignment, as we always have at least that much alignment to begin
1010 // with.
1011 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
1012 // As an optimization, we only generate a single function of each kind we
1013 // might need. We need a different one for each alignment and for each
1014 // setting of flags. We mix Align and flag to get the kind.
1015 uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag;
1016 llvm::Constant *& Entry = CGM.DestroyCache[kind];
1017 if (Entry)
1018 return Entry;
1019 return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001020}
1021
Mike Stump797b6322009-03-05 01:23:13 +00001022llvm::Value *BlockFunction::getBlockObjectDispose() {
1023 if (CGM.BlockObjectDispose == 0) {
1024 const llvm::FunctionType *FTy;
1025 std::vector<const llvm::Type*> ArgTys;
1026 const llvm::Type *ResultType = llvm::Type::VoidTy;
1027 ArgTys.push_back(PtrToInt8Ty);
1028 ArgTys.push_back(llvm::Type::Int32Ty);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001029 FTy = VMContext.getFunctionType(ResultType, ArgTys, false);
Mike Stump00470a12009-03-05 08:32:30 +00001030 CGM.BlockObjectDispose
Mike Stump797b6322009-03-05 01:23:13 +00001031 = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
1032 }
1033 return CGM.BlockObjectDispose;
1034}
1035
Mike Stumpee094222009-03-06 06:12:24 +00001036llvm::Value *BlockFunction::getBlockObjectAssign() {
1037 if (CGM.BlockObjectAssign == 0) {
1038 const llvm::FunctionType *FTy;
1039 std::vector<const llvm::Type*> ArgTys;
1040 const llvm::Type *ResultType = llvm::Type::VoidTy;
1041 ArgTys.push_back(PtrToInt8Ty);
1042 ArgTys.push_back(PtrToInt8Ty);
1043 ArgTys.push_back(llvm::Type::Int32Ty);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001044 FTy = VMContext.getFunctionType(ResultType, ArgTys, false);
Mike Stumpee094222009-03-06 06:12:24 +00001045 CGM.BlockObjectAssign
1046 = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
1047 }
1048 return CGM.BlockObjectAssign;
1049}
1050
Mike Stump1851b682009-03-06 04:53:30 +00001051void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Mike Stump797b6322009-03-05 01:23:13 +00001052 llvm::Value *F = getBlockObjectDispose();
Mike Stump1851b682009-03-06 04:53:30 +00001053 llvm::Value *N;
Mike Stump797b6322009-03-05 01:23:13 +00001054 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001055 N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag);
Mike Stump797b6322009-03-05 01:23:13 +00001056 Builder.CreateCall2(F, V, N);
1057}
Mike Stump00470a12009-03-05 08:32:30 +00001058
1059ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stump08920992009-03-07 02:35:30 +00001060
1061BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1062 CGBuilderTy &B)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001063 : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) {
1064 PtrToInt8Ty = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty);
Mike Stump08920992009-03-07 02:35:30 +00001065
1066 BlockHasCopyDispose = false;
1067}