blob: 73200fe2ca379718565cd21c8bed714315030304 [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
Owen Andersona1cf15f2009-07-14 23:10:40 +000032 llvm::LLVMContext &VMContext = CGM.getLLVMContext();
33
Mike Stumpe5fee252009-02-13 16:19:19 +000034 // reserved
Owen Andersona1cf15f2009-07-14 23:10:40 +000035 C = VMContext.getConstantInt(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 Andersona1cf15f2009-07-14 23:10:40 +000042 C = VMContext.getConstantInt(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 Andersona1cf15f2009-07-14 23:10:40 +000053 C = VMContext.getConstantStruct(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 Andersona1cf15f2009-07-14 23:10:40 +0000145 C = VMContext.getConstantExprBitCast(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 Andersona1cf15f2009-07-14 23:10:40 +0000151 C = VMContext.getConstantInt(IntTy, flags);
Mike Stump00470a12009-03-05 08:32:30 +0000152 Elts[1] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000153
154 // __reserved
Owen Andersona1cf15f2009-07-14 23:10:40 +0000155 C = VMContext.getConstantInt(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 Andersona1cf15f2009-07-14 23:10:40 +0000164 Elts[1] = VMContext.getConstantInt(IntTy, flags|BLOCK_IS_GLOBAL);
Mike Stump5570cfe2009-03-01 20:07:53 +0000165
Owen Andersona1cf15f2009-07-14 23:10:40 +0000166 C = VMContext.getConstantStruct(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 Andersona1cf15f2009-07-14 23:10:40 +0000174 C = VMContext.getConstantExprBitCast(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 Andersona1cf15f2009-07-14 23:10:40 +0000189 Types[i+5] = VMContext.getPointerType(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 Andersona1cf15f2009-07-14 23:10:40 +0000194 llvm::StructType *Ty = VMContext.getStructType(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 Andersona1cf15f2009-07-14 23:10:40 +0000270 VMContext.getConstantInt(llvm::Type::Int64Ty,
Mike Stump58a85142009-03-04 22:48:06 +0000271 offset),
272 "block.literal");
Owen Andersona1cf15f2009-07-14 23:10:40 +0000273 Ty = VMContext.getPointerType(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 Andersona1cf15f2009-07-14 23:10:40 +0000315 BlockDescriptorType = VMContext.getStructType(UnsignedLongTy,
Mike Stumpa5448542009-02-13 15:32:32 +0000316 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000317 NULL);
318
319 getModule().addTypeName("struct.__block_descriptor",
320 BlockDescriptorType);
321
322 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000323}
324
Mike Stump2a998142009-03-04 18:17:45 +0000325const llvm::Type *BlockModule::getGenericBlockLiteralType() {
Mike Stump9b8a7972009-02-13 15:25:34 +0000326 if (GenericBlockLiteralType)
327 return GenericBlockLiteralType;
328
Mike Stumpa5448542009-02-13 15:32:32 +0000329 const llvm::Type *BlockDescPtrTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +0000330 VMContext.getPointerTypeUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000331
Mike Stump7cbb3602009-02-13 16:01:35 +0000332 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
333 getTypes().ConvertType(getContext().IntTy));
334
Mike Stump9b8a7972009-02-13 15:25:34 +0000335 // struct __block_literal_generic {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000336 // void *__isa;
337 // int __flags;
338 // int __reserved;
339 // void (*__invoke)(void *);
340 // struct __block_descriptor *__descriptor;
Mike Stump9b8a7972009-02-13 15:25:34 +0000341 // };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000342 GenericBlockLiteralType = VMContext.getStructType(PtrToInt8Ty,
Mike Stump7cbb3602009-02-13 16:01:35 +0000343 IntTy,
344 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000345 PtrToInt8Ty,
Mike Stump9b8a7972009-02-13 15:25:34 +0000346 BlockDescPtrTy,
347 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000348
Mike Stump9b8a7972009-02-13 15:25:34 +0000349 getModule().addTypeName("struct.__block_literal_generic",
350 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000351
Mike Stump9b8a7972009-02-13 15:25:34 +0000352 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000353}
354
Mike Stump2a998142009-03-04 18:17:45 +0000355const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000356 if (GenericExtendedBlockLiteralType)
357 return GenericExtendedBlockLiteralType;
358
Mike Stumpbd65cac2009-02-19 01:01:04 +0000359 const llvm::Type *BlockDescPtrTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +0000360 VMContext.getPointerTypeUnqual(getBlockDescriptorType());
Mike Stumpbd65cac2009-02-19 01:01:04 +0000361
362 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
363 getTypes().ConvertType(getContext().IntTy));
364
365 // struct __block_literal_generic {
366 // void *__isa;
367 // int __flags;
368 // int __reserved;
369 // void (*__invoke)(void *);
370 // struct __block_descriptor *__descriptor;
371 // void *__copy_func_helper_decl;
372 // void *__destroy_func_decl;
373 // };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000374 GenericExtendedBlockLiteralType = VMContext.getStructType(PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000375 IntTy,
376 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000377 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000378 BlockDescPtrTy,
Mike Stump797b6322009-03-05 01:23:13 +0000379 PtrToInt8Ty,
380 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000381 NULL);
382
383 getModule().addTypeName("struct.__block_literal_extended_generic",
384 GenericExtendedBlockLiteralType);
385
386 return GenericExtendedBlockLiteralType;
387}
388
Anders Carlssond5cab542009-02-12 17:55:02 +0000389RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpa5448542009-02-13 15:32:32 +0000390 const BlockPointerType *BPT =
Anders Carlssonacfde802009-02-12 00:39:25 +0000391 E->getCallee()->getType()->getAsBlockPointerType();
Mike Stumpa5448542009-02-13 15:32:32 +0000392
Anders Carlssonacfde802009-02-12 00:39:25 +0000393 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
394
395 // Get a pointer to the generic block literal.
396 const llvm::Type *BlockLiteralTy =
Owen Andersona1cf15f2009-07-14 23:10:40 +0000397 VMContext.getPointerTypeUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000398
399 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000400 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000401 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
402
403 // Get the function pointer from the literal.
404 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Anders Carlssonacfde802009-02-12 00:39:25 +0000405
Mike Stumpa5448542009-02-13 15:32:32 +0000406 BlockLiteral =
407 Builder.CreateBitCast(BlockLiteral,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000408 VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty),
Anders Carlssonacfde802009-02-12 00:39:25 +0000409 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000410
Anders Carlssonacfde802009-02-12 00:39:25 +0000411 // Add the block literal.
412 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
413 CallArgList Args;
414 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000415
Anders Carlsson782f3972009-04-08 23:13:16 +0000416 QualType FnType = BPT->getPointeeType();
417
Anders Carlssonacfde802009-02-12 00:39:25 +0000418 // And the rest of the arguments.
Anders Carlsson782f3972009-04-08 23:13:16 +0000419 EmitCallArgs(Args, FnType->getAsFunctionProtoType(),
420 E->arg_begin(), E->arg_end());
Mike Stumpa5448542009-02-13 15:32:32 +0000421
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000422 // Load the function.
423 llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp");
424
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000425 QualType ResultType = FnType->getAsFunctionType()->getResultType();
426
427 const CGFunctionInfo &FnInfo =
428 CGM.getTypes().getFunctionInfo(ResultType, Args);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000429
430 // Cast the function pointer to the right type.
431 const llvm::Type *BlockFTy =
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000432 CGM.getTypes().GetFunctionType(FnInfo, false);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000433
Owen Andersona1cf15f2009-07-14 23:10:40 +0000434 const llvm::Type *BlockFTyPtr = VMContext.getPointerTypeUnqual(BlockFTy);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000435 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
436
Anders Carlssonacfde802009-02-12 00:39:25 +0000437 // And call the block.
Anders Carlssonf8544a42009-04-07 00:20:24 +0000438 return EmitCall(FnInfo, Func, Args);
Anders Carlssonacfde802009-02-12 00:39:25 +0000439}
Anders Carlssond5cab542009-02-12 17:55:02 +0000440
Mike Stumpdab514f2009-03-04 03:23:46 +0000441llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
442 uint64_t &offset = BlockDecls[E->getDecl()];
443
444 const llvm::Type *Ty;
445 Ty = CGM.getTypes().ConvertType(E->getDecl()->getType());
446
Mike Stumpdab514f2009-03-04 03:23:46 +0000447 // See if we have already allocated an offset for this variable.
448 if (offset == 0) {
Mike Stump00470a12009-03-05 08:32:30 +0000449 // Don't run the expensive check, unless we have to.
450 if (!BlockHasCopyDispose && BlockRequiresCopying(E->getType()))
451 BlockHasCopyDispose = true;
Mike Stumpdab514f2009-03-04 03:23:46 +0000452 // if not, allocate one now.
453 offset = getBlockOffset(E);
454 }
455
456 llvm::Value *BlockLiteral = LoadBlockStruct();
457 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000458 VMContext.getConstantInt(llvm::Type::Int64Ty,
Mike Stumpdab514f2009-03-04 03:23:46 +0000459 offset),
Mike Stump58a85142009-03-04 22:48:06 +0000460 "block.literal");
Mike Stumpdab514f2009-03-04 03:23:46 +0000461 if (E->isByRef()) {
462 bool needsCopyDispose = BlockRequiresCopying(E->getType());
463 uint64_t Align = getContext().getDeclAlignInBytes(E->getDecl());
464 const llvm::Type *PtrStructTy
Owen Andersona1cf15f2009-07-14 23:10:40 +0000465 = VMContext.getPointerType(BuildByRefType(E->getType(), Align), 0);
Mike Stumpa8b60c92009-03-21 21:00:35 +0000466 // The block literal will need a copy/destroy helper.
467 BlockHasCopyDispose = true;
Mike Stumpdab514f2009-03-04 03:23:46 +0000468 Ty = PtrStructTy;
Owen Andersona1cf15f2009-07-14 23:10:40 +0000469 Ty = VMContext.getPointerType(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000470 V = Builder.CreateBitCast(V, Ty);
471 V = Builder.CreateLoad(V, false);
472 V = Builder.CreateStructGEP(V, 1, "forwarding");
473 V = Builder.CreateLoad(V, false);
474 V = Builder.CreateBitCast(V, PtrStructTy);
475 V = Builder.CreateStructGEP(V, needsCopyDispose*2 + 4, "x");
476 } else {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000477 Ty = VMContext.getPointerType(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000478 V = Builder.CreateBitCast(V, Ty);
479 }
480 return V;
481}
482
Mike Stump6cc88f72009-03-20 21:53:12 +0000483void CodeGenFunction::BlockForwardSelf() {
484 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
485 ImplicitParamDecl *SelfDecl = OMD->getSelfDecl();
486 llvm::Value *&DMEntry = LocalDeclMap[SelfDecl];
487 if (DMEntry)
488 return;
489 // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care
490 BlockDeclRefExpr *BDRE = new (getContext())
491 BlockDeclRefExpr(SelfDecl,
492 SelfDecl->getType(), SourceLocation(), false);
493 DMEntry = GetAddrOfBlockDecl(BDRE);
494}
495
Mike Stump67a64482009-02-14 22:16:35 +0000496llvm::Constant *
Mike Stump90a90432009-03-04 18:47:42 +0000497BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000498 // Generate the block descriptor.
499 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000500 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
501 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000502
Anders Carlssond5cab542009-02-12 17:55:02 +0000503 llvm::Constant *DescriptorFields[2];
Mike Stumpa5448542009-02-13 15:32:32 +0000504
Anders Carlssond5cab542009-02-12 17:55:02 +0000505 // Reserved
Owen Anderson69243822009-07-13 04:10:07 +0000506 DescriptorFields[0] = getModule().getContext().getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000507
Anders Carlssond5cab542009-02-12 17:55:02 +0000508 // Block literal size. For global blocks we just use the size of the generic
509 // block literal struct.
Mike Stumpa5448542009-02-13 15:32:32 +0000510 uint64_t BlockLiteralSize =
Mike Stump9b8a7972009-02-13 15:25:34 +0000511 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Owen Andersona1cf15f2009-07-14 23:10:40 +0000512 DescriptorFields[1] =
513 VMContext.getConstantInt(UnsignedLongTy,BlockLiteralSize);
Mike Stumpa5448542009-02-13 15:32:32 +0000514
515 llvm::Constant *DescriptorStruct =
Owen Andersona1cf15f2009-07-14 23:10:40 +0000516 VMContext.getConstantStruct(&DescriptorFields[0], 2);
Mike Stumpa5448542009-02-13 15:32:32 +0000517
Anders Carlssond5cab542009-02-12 17:55:02 +0000518 llvm::GlobalVariable *Descriptor =
Owen Anderson1c431b32009-07-08 19:05:04 +0000519 new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000520 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000521 DescriptorStruct, "__block_descriptor_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000522
Anders Carlssond5cab542009-02-12 17:55:02 +0000523 // Generate the constants for the block literal.
524 llvm::Constant *LiteralFields[5];
Mike Stumpa5448542009-02-13 15:32:32 +0000525
Mike Stump67a64482009-02-14 22:16:35 +0000526 CodeGenFunction::BlockInfo Info(0, n);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000527 uint64_t subBlockSize, subBlockAlign;
Mike Stumpa99038c2009-02-28 09:07:16 +0000528 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Daniel Dunbard67b09a2009-03-12 03:07:24 +0000529 bool subBlockHasCopyDispose = false;
Mike Stump7f28a9c2009-03-13 23:34:28 +0000530 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000531 llvm::Function *Fn
Mike Stump6cc88f72009-03-20 21:53:12 +0000532 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000533 subBlockSize,
Mike Stump90a90432009-03-04 18:47:42 +0000534 subBlockAlign,
Mike Stump00470a12009-03-05 08:32:30 +0000535 subBlockDeclRefDecls,
536 subBlockHasCopyDispose);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000537 assert(subBlockSize == BlockLiteralSize
538 && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000539
Anders Carlssond5cab542009-02-12 17:55:02 +0000540 // isa
Mike Stumpf99f1d02009-02-13 17:23:42 +0000541 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000542
Anders Carlssond5cab542009-02-12 17:55:02 +0000543 // Flags
Mike Stump00470a12009-03-05 08:32:30 +0000544 LiteralFields[1] =
Owen Andersona1cf15f2009-07-14 23:10:40 +0000545 VMContext.getConstantInt(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR);
Mike Stumpa5448542009-02-13 15:32:32 +0000546
Anders Carlssond5cab542009-02-12 17:55:02 +0000547 // Reserved
Owen Anderson69243822009-07-13 04:10:07 +0000548 LiteralFields[2] = getModule().getContext().getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000549
Anders Carlssond5cab542009-02-12 17:55:02 +0000550 // Function
551 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000552
Anders Carlssond5cab542009-02-12 17:55:02 +0000553 // Descriptor
554 LiteralFields[4] = Descriptor;
Mike Stumpa5448542009-02-13 15:32:32 +0000555
556 llvm::Constant *BlockLiteralStruct =
Owen Andersona1cf15f2009-07-14 23:10:40 +0000557 VMContext.getConstantStruct(&LiteralFields[0], 5);
Mike Stumpa5448542009-02-13 15:32:32 +0000558
559 llvm::GlobalVariable *BlockLiteral =
Owen Anderson1c431b32009-07-08 19:05:04 +0000560 new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000561 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000562 BlockLiteralStruct, "__block_literal_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000563
Anders Carlssond5cab542009-02-12 17:55:02 +0000564 return BlockLiteral;
565}
566
Mike Stump4e7a1f72009-02-21 20:00:35 +0000567llvm::Value *CodeGenFunction::LoadBlockStruct() {
568 return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self");
569}
570
Mike Stump00470a12009-03-05 08:32:30 +0000571llvm::Function *
572CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
573 const BlockInfo& Info,
Mike Stump6cc88f72009-03-20 21:53:12 +0000574 const Decl *OuterFuncDecl,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000575 llvm::DenseMap<const Decl*, llvm::Value*> ldm,
Mike Stump00470a12009-03-05 08:32:30 +0000576 uint64_t &Size,
577 uint64_t &Align,
578 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls,
579 bool &subBlockHasCopyDispose) {
Devang Patel963dfbd2009-04-15 21:51:44 +0000580
581 // Check if we should generate debug info for this block.
582 if (CGM.getDebugInfo())
583 DebugInfo = CGM.getDebugInfo();
584
Mike Stump7f28a9c2009-03-13 23:34:28 +0000585 // Arrange for local static and local extern declarations to appear
586 // to be local to this function as well, as they are directly referenced
587 // in a block.
588 for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
589 i != ldm.end();
590 ++i) {
591 const VarDecl *VD = dyn_cast<VarDecl>(i->first);
592
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000593 if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage())
Mike Stump7f28a9c2009-03-13 23:34:28 +0000594 LocalDeclMap[VD] = i->second;
595 }
596
Eli Friedman48f91222009-03-28 03:24:54 +0000597 // FIXME: We need to rearrange the code for copy/dispose so we have this
598 // sooner, so we can calculate offsets correctly.
599 if (!BlockHasCopyDispose)
600 BlockOffset = CGM.getTargetData()
601 .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8;
602 else
603 BlockOffset = CGM.getTargetData()
604 .getTypeStoreSizeInBits(CGM.getGenericExtendedBlockLiteralType()) / 8;
605 BlockAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
606
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000607 const FunctionType *BlockFunctionType = BExpr->getFunctionType();
608 QualType ResultType;
609 bool IsVariadic;
Fariborz Jahanianda0895d2009-04-11 18:54:57 +0000610 if (const FunctionProtoType *FTy =
611 dyn_cast<FunctionProtoType>(BlockFunctionType)) {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000612 ResultType = FTy->getResultType();
613 IsVariadic = FTy->isVariadic();
614 }
615 else {
616 // K&R style block.
617 ResultType = BlockFunctionType->getResultType();
618 IsVariadic = false;
619 }
Mike Stumpa5448542009-02-13 15:32:32 +0000620
Anders Carlssond5cab542009-02-12 17:55:02 +0000621 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000622
Chris Lattner161d36d2009-02-28 19:01:03 +0000623 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlssond5cab542009-02-12 17:55:02 +0000624
625 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000626 ImplicitParamDecl *SelfDecl =
Anders Carlssond5cab542009-02-12 17:55:02 +0000627 ImplicitParamDecl::Create(getContext(), 0,
628 SourceLocation(), 0,
629 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000630
Anders Carlssond5cab542009-02-12 17:55:02 +0000631 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump4e7a1f72009-02-21 20:00:35 +0000632 BlockStructDecl = SelfDecl;
Mike Stumpa5448542009-02-13 15:32:32 +0000633
Steve Naroffe78b8092009-03-13 16:56:44 +0000634 for (BlockDecl::param_const_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000635 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000636 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000637
638 const CGFunctionInfo &FI =
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000639 CGM.getTypes().getFunctionInfo(ResultType, Args);
Anders Carlssond5cab542009-02-12 17:55:02 +0000640
Mike Stump67a64482009-02-14 22:16:35 +0000641 std::string Name = std::string("__") + Info.Name + "_block_invoke_";
Anders Carlssond5cab542009-02-12 17:55:02 +0000642 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000643 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic);
Mike Stumpa5448542009-02-13 15:32:32 +0000644
645 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000646 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
647 Name,
648 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000649
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000650 CGM.SetInternalFunctionAttributes(BD, Fn, FI);
651
Chris Lattner4863db42009-04-23 07:18:56 +0000652 StartFunction(BD, ResultType, Fn, Args,
Chris Lattner161d36d2009-02-28 19:01:03 +0000653 BExpr->getBody()->getLocEnd());
Chris Lattner4863db42009-04-23 07:18:56 +0000654 CurFuncDecl = OuterFuncDecl;
Chris Lattnerb5437d22009-04-23 05:30:27 +0000655 CurCodeDecl = BD;
Chris Lattner161d36d2009-02-28 19:01:03 +0000656 EmitStmt(BExpr->getBody());
657 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlssond5cab542009-02-12 17:55:02 +0000658
Mike Stump8a2b4b12009-02-25 23:33:13 +0000659 // The runtime needs a minimum alignment of a void *.
660 uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
661 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign);
662
Mike Stump4e7a1f72009-02-21 20:00:35 +0000663 Size = BlockOffset;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000664 Align = BlockAlign;
665 subBlockDeclRefDecls = BlockDeclRefDecls;
Mike Stump00470a12009-03-05 08:32:30 +0000666 subBlockHasCopyDispose |= BlockHasCopyDispose;
Anders Carlssond5cab542009-02-12 17:55:02 +0000667 return Fn;
668}
Mike Stumpa99038c2009-02-28 09:07:16 +0000669
Mike Stump08920992009-03-07 02:35:30 +0000670uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
Mike Stumpa99038c2009-02-28 09:07:16 +0000671 const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
672
673 uint64_t Size = getContext().getTypeSize(D->getType()) / 8;
674 uint64_t Align = getContext().getDeclAlignInBytes(D);
675
676 if (BDRE->isByRef()) {
677 Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8;
678 Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
679 }
680
681 assert ((Align > 0) && "alignment must be 1 byte or more");
682
683 uint64_t OldOffset = BlockOffset;
684
685 // Ensure proper alignment, even if it means we have to have a gap
686 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align);
687 BlockAlign = std::max(Align, BlockAlign);
Mike Stump00470a12009-03-05 08:32:30 +0000688
Mike Stumpa99038c2009-02-28 09:07:16 +0000689 uint64_t Pad = BlockOffset - OldOffset;
690 if (Pad) {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000691 VMContext.getArrayType(llvm::Type::Int8Ty, Pad);
Mike Stumpa99038c2009-02-28 09:07:16 +0000692 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
693 llvm::APInt(32, Pad),
694 ArrayType::Normal, 0);
695 ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
696 0, QualType(PadTy), VarDecl::None,
697 SourceLocation());
698 Expr *E;
699 E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
700 SourceLocation(), false, false);
701 BlockDeclRefDecls.push_back(E);
702 }
703 BlockDeclRefDecls.push_back(BDRE);
704
705 BlockOffset += Size;
706 return BlockOffset-Size;
707}
Mike Stumpdab514f2009-03-04 03:23:46 +0000708
Mike Stump08920992009-03-07 02:35:30 +0000709llvm::Constant *BlockFunction::
710GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000711 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000712 QualType R = getContext().VoidTy;
713
714 FunctionArgList Args;
715 // FIXME: This leaks
Mike Stump08920992009-03-07 02:35:30 +0000716 ImplicitParamDecl *Dst =
717 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
718 getContext().getPointerType(getContext().VoidTy));
719 Args.push_back(std::make_pair(Dst, Dst->getType()));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000720 ImplicitParamDecl *Src =
721 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
722 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000723 Args.push_back(std::make_pair(Src, Src->getType()));
724
725 const CGFunctionInfo &FI =
726 CGM.getTypes().getFunctionInfo(R, Args);
727
Mike Stump3899a7f2009-06-05 23:26:36 +0000728 // FIXME: We'd like to put these into a mergable by content, with
729 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000730 std::string Name = std::string("__copy_helper_block_");
731 CodeGenTypes &Types = CGM.getTypes();
732 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
733
734 llvm::Function *Fn =
735 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
736 Name,
737 &CGM.getModule());
738
739 IdentifierInfo *II
740 = &CGM.getContext().Idents.get("__copy_helper_block_");
741
742 FunctionDecl *FD = FunctionDecl::Create(getContext(),
743 getContext().getTranslationUnitDecl(),
744 SourceLocation(), II, R,
745 FunctionDecl::Static, false,
746 true);
747 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump08920992009-03-07 02:35:30 +0000748
749 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
750 llvm::Type *PtrPtrT;
Mike Stump08920992009-03-07 02:35:30 +0000751
Mike Stumpb7477cf2009-04-10 18:52:28 +0000752 if (NoteForHelperp) {
753 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump08920992009-03-07 02:35:30 +0000754
Owen Andersona1cf15f2009-07-14 23:10:40 +0000755 PtrPtrT = VMContext.getPointerType(VMContext.getPointerType(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000756 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
757 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump08920992009-03-07 02:35:30 +0000758
Mike Stumpb7477cf2009-04-10 18:52:28 +0000759 llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000760 llvm::Type *PtrPtrT;
Owen Andersona1cf15f2009-07-14 23:10:40 +0000761 PtrPtrT = VMContext.getPointerType(VMContext.getPointerType(T, 0), 0);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000762 DstObj = Builder.CreateBitCast(DstObj, PtrPtrT);
763 DstObj = Builder.CreateLoad(DstObj);
Mike Stump08920992009-03-07 02:35:30 +0000764
Mike Stumpb7477cf2009-04-10 18:52:28 +0000765 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
766 int flag = NoteForHelper[i].flag;
767 int index = NoteForHelper[i].index;
Mike Stump08920992009-03-07 02:35:30 +0000768
Mike Stumpb7477cf2009-04-10 18:52:28 +0000769 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
770 || NoteForHelper[i].RequiresCopying) {
771 llvm::Value *Srcv = SrcObj;
772 Srcv = Builder.CreateStructGEP(Srcv, index);
773 Srcv = Builder.CreateBitCast(Srcv,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000774 VMContext.getPointerType(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000775 Srcv = Builder.CreateLoad(Srcv);
776
777 llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
778 Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
779
Owen Andersona1cf15f2009-07-14 23:10:40 +0000780 llvm::Value *N = VMContext.getConstantInt(llvm::Type::Int32Ty, flag);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000781 llvm::Value *F = getBlockObjectAssign();
782 Builder.CreateCall3(F, Dstv, Srcv, N);
783 }
Mike Stump08920992009-03-07 02:35:30 +0000784 }
785 }
786
Mike Stumpa4f668f2009-03-06 01:33:24 +0000787 CGF.FinishFunction();
788
Owen Andersona1cf15f2009-07-14 23:10:40 +0000789 return VMContext.getConstantExprBitCast(Fn, PtrToInt8Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000790}
791
Mike Stumpcf62d392009-03-06 18:42:23 +0000792llvm::Constant *BlockFunction::
Mike Stump08920992009-03-07 02:35:30 +0000793GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
794 const llvm::StructType* T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000795 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000796 QualType R = getContext().VoidTy;
797
798 FunctionArgList Args;
799 // FIXME: This leaks
800 ImplicitParamDecl *Src =
801 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
802 getContext().getPointerType(getContext().VoidTy));
803
804 Args.push_back(std::make_pair(Src, Src->getType()));
805
806 const CGFunctionInfo &FI =
807 CGM.getTypes().getFunctionInfo(R, Args);
808
Mike Stump3899a7f2009-06-05 23:26:36 +0000809 // FIXME: We'd like to put these into a mergable by content, with
810 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000811 std::string Name = std::string("__destroy_helper_block_");
812 CodeGenTypes &Types = CGM.getTypes();
813 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
814
815 llvm::Function *Fn =
816 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
817 Name,
818 &CGM.getModule());
819
820 IdentifierInfo *II
821 = &CGM.getContext().Idents.get("__destroy_helper_block_");
822
823 FunctionDecl *FD = FunctionDecl::Create(getContext(),
824 getContext().getTranslationUnitDecl(),
825 SourceLocation(), II, R,
826 FunctionDecl::Static, false,
827 true);
828 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1edf6b62009-03-07 02:53:18 +0000829
Mike Stumpb7477cf2009-04-10 18:52:28 +0000830 if (NoteForHelperp) {
831 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump1edf6b62009-03-07 02:53:18 +0000832
Mike Stumpb7477cf2009-04-10 18:52:28 +0000833 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
834 llvm::Type *PtrPtrT;
Owen Andersona1cf15f2009-07-14 23:10:40 +0000835 PtrPtrT = VMContext.getPointerType(VMContext.getPointerType(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000836 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
837 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump1edf6b62009-03-07 02:53:18 +0000838
Mike Stumpb7477cf2009-04-10 18:52:28 +0000839 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
840 int flag = NoteForHelper[i].flag;
841 int index = NoteForHelper[i].index;
Mike Stump1edf6b62009-03-07 02:53:18 +0000842
Mike Stumpb7477cf2009-04-10 18:52:28 +0000843 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
844 || NoteForHelper[i].RequiresCopying) {
845 llvm::Value *Srcv = SrcObj;
846 Srcv = Builder.CreateStructGEP(Srcv, index);
847 Srcv = Builder.CreateBitCast(Srcv,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000848 VMContext.getPointerType(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000849 Srcv = Builder.CreateLoad(Srcv);
850
851 BuildBlockRelease(Srcv, flag);
852 }
Mike Stump1edf6b62009-03-07 02:53:18 +0000853 }
854 }
855
Mike Stumpa4f668f2009-03-06 01:33:24 +0000856 CGF.FinishFunction();
857
Owen Andersona1cf15f2009-07-14 23:10:40 +0000858 return VMContext.getConstantExprBitCast(Fn, PtrToInt8Ty);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000859}
860
Mike Stump08920992009-03-07 02:35:30 +0000861llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000862 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump08920992009-03-07 02:35:30 +0000863 return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
864 T, NoteForHelper);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000865}
866
Mike Stump08920992009-03-07 02:35:30 +0000867llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000868 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stump08920992009-03-07 02:35:30 +0000869 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000870 T, NoteForHelperp);
Mike Stumpdab514f2009-03-04 03:23:46 +0000871}
Mike Stump797b6322009-03-05 01:23:13 +0000872
Mike Stumpee094222009-03-06 06:12:24 +0000873llvm::Constant *BlockFunction::
874GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
Mike Stump45031c02009-03-06 02:29:21 +0000875 QualType R = getContext().VoidTy;
876
877 FunctionArgList Args;
878 // FIXME: This leaks
Mike Stumpee094222009-03-06 06:12:24 +0000879 ImplicitParamDecl *Dst =
880 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
881 getContext().getPointerType(getContext().VoidTy));
882 Args.push_back(std::make_pair(Dst, Dst->getType()));
883
884 // FIXME: This leaks
Mike Stump45031c02009-03-06 02:29:21 +0000885 ImplicitParamDecl *Src =
886 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
887 getContext().getPointerType(getContext().VoidTy));
Mike Stump45031c02009-03-06 02:29:21 +0000888 Args.push_back(std::make_pair(Src, Src->getType()));
889
890 const CGFunctionInfo &FI =
891 CGM.getTypes().getFunctionInfo(R, Args);
892
893 std::string Name = std::string("__Block_byref_id_object_copy_");
894 CodeGenTypes &Types = CGM.getTypes();
895 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
896
Mike Stump3899a7f2009-06-05 23:26:36 +0000897 // FIXME: We'd like to put these into a mergable by content, with
898 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +0000899 llvm::Function *Fn =
900 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
901 Name,
902 &CGM.getModule());
903
904 IdentifierInfo *II
905 = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
906
907 FunctionDecl *FD = FunctionDecl::Create(getContext(),
908 getContext().getTranslationUnitDecl(),
909 SourceLocation(), II, R,
910 FunctionDecl::Static, false,
911 true);
912 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpee094222009-03-06 06:12:24 +0000913
914 // dst->x
915 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000916 V = Builder.CreateBitCast(V, VMContext.getPointerType(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +0000917 V = Builder.CreateLoad(V);
Mike Stumpee094222009-03-06 06:12:24 +0000918 V = Builder.CreateStructGEP(V, 6, "x");
919 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
920
921 // src->x
922 V = CGF.GetAddrOfLocalVar(Src);
923 V = Builder.CreateLoad(V);
924 V = Builder.CreateBitCast(V, T);
925 V = Builder.CreateStructGEP(V, 6, "x");
Owen Andersona1cf15f2009-07-14 23:10:40 +0000926 V = Builder.CreateBitCast(V, VMContext.getPointerType(PtrToInt8Ty, 0));
Mike Stumpee094222009-03-06 06:12:24 +0000927 llvm::Value *SrcObj = Builder.CreateLoad(V);
928
929 flag |= BLOCK_BYREF_CALLER;
930
Owen Andersona1cf15f2009-07-14 23:10:40 +0000931 llvm::Value *N = VMContext.getConstantInt(llvm::Type::Int32Ty, flag);
Mike Stumpee094222009-03-06 06:12:24 +0000932 llvm::Value *F = getBlockObjectAssign();
933 Builder.CreateCall3(F, DstObj, SrcObj, N);
934
Mike Stump45031c02009-03-06 02:29:21 +0000935 CGF.FinishFunction();
936
Owen Andersona1cf15f2009-07-14 23:10:40 +0000937 return VMContext.getConstantExprBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +0000938}
939
Mike Stump1851b682009-03-06 04:53:30 +0000940llvm::Constant *
941BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
942 int flag) {
Mike Stump45031c02009-03-06 02:29:21 +0000943 QualType R = getContext().VoidTy;
944
945 FunctionArgList Args;
946 // FIXME: This leaks
947 ImplicitParamDecl *Src =
948 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
949 getContext().getPointerType(getContext().VoidTy));
950
951 Args.push_back(std::make_pair(Src, Src->getType()));
952
953 const CGFunctionInfo &FI =
954 CGM.getTypes().getFunctionInfo(R, Args);
955
956 std::string Name = std::string("__Block_byref_id_object_dispose_");
957 CodeGenTypes &Types = CGM.getTypes();
958 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
959
Mike Stump3899a7f2009-06-05 23:26:36 +0000960 // FIXME: We'd like to put these into a mergable by content, with
961 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +0000962 llvm::Function *Fn =
963 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
964 Name,
965 &CGM.getModule());
966
967 IdentifierInfo *II
968 = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
969
970 FunctionDecl *FD = FunctionDecl::Create(getContext(),
971 getContext().getTranslationUnitDecl(),
972 SourceLocation(), II, R,
973 FunctionDecl::Static, false,
974 true);
975 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1851b682009-03-06 04:53:30 +0000976
977 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000978 V = Builder.CreateBitCast(V, VMContext.getPointerType(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +0000979 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +0000980 V = Builder.CreateStructGEP(V, 6, "x");
Owen Andersona1cf15f2009-07-14 23:10:40 +0000981 V = Builder.CreateBitCast(V, VMContext.getPointerType(PtrToInt8Ty, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +0000982 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +0000983
Mike Stump1851b682009-03-06 04:53:30 +0000984 flag |= BLOCK_BYREF_CALLER;
985 BuildBlockRelease(V, flag);
Mike Stump45031c02009-03-06 02:29:21 +0000986 CGF.FinishFunction();
987
Owen Andersona1cf15f2009-07-14 23:10:40 +0000988 return VMContext.getConstantExprBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +0000989}
990
Mike Stumpee094222009-03-06 06:12:24 +0000991llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
Mike Stump3899a7f2009-06-05 23:26:36 +0000992 int flag, unsigned Align) {
993 // All alignments below that of pointer alignment collpase down to just
994 // pointer alignment, as we always have at least that much alignment to begin
995 // with.
996 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
997 // As an optimization, we only generate a single function of each kind we
998 // might need. We need a different one for each alignment and for each
999 // setting of flags. We mix Align and flag to get the kind.
1000 uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag;
1001 llvm::Constant *& Entry = CGM.AssignCache[kind];
1002 if (Entry)
1003 return Entry;
1004 return Entry=CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001005}
1006
Mike Stump1851b682009-03-06 04:53:30 +00001007llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
Mike Stump3899a7f2009-06-05 23:26:36 +00001008 int flag,
1009 unsigned Align) {
1010 // All alignments below that of pointer alignment collpase down to just
1011 // pointer alignment, as we always have at least that much alignment to begin
1012 // with.
1013 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
1014 // As an optimization, we only generate a single function of each kind we
1015 // might need. We need a different one for each alignment and for each
1016 // setting of flags. We mix Align and flag to get the kind.
1017 uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag;
1018 llvm::Constant *& Entry = CGM.DestroyCache[kind];
1019 if (Entry)
1020 return Entry;
1021 return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001022}
1023
Mike Stump797b6322009-03-05 01:23:13 +00001024llvm::Value *BlockFunction::getBlockObjectDispose() {
1025 if (CGM.BlockObjectDispose == 0) {
1026 const llvm::FunctionType *FTy;
1027 std::vector<const llvm::Type*> ArgTys;
1028 const llvm::Type *ResultType = llvm::Type::VoidTy;
1029 ArgTys.push_back(PtrToInt8Ty);
1030 ArgTys.push_back(llvm::Type::Int32Ty);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001031 FTy = VMContext.getFunctionType(ResultType, ArgTys, false);
Mike Stump00470a12009-03-05 08:32:30 +00001032 CGM.BlockObjectDispose
Mike Stump797b6322009-03-05 01:23:13 +00001033 = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
1034 }
1035 return CGM.BlockObjectDispose;
1036}
1037
Mike Stumpee094222009-03-06 06:12:24 +00001038llvm::Value *BlockFunction::getBlockObjectAssign() {
1039 if (CGM.BlockObjectAssign == 0) {
1040 const llvm::FunctionType *FTy;
1041 std::vector<const llvm::Type*> ArgTys;
1042 const llvm::Type *ResultType = llvm::Type::VoidTy;
1043 ArgTys.push_back(PtrToInt8Ty);
1044 ArgTys.push_back(PtrToInt8Ty);
1045 ArgTys.push_back(llvm::Type::Int32Ty);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001046 FTy = VMContext.getFunctionType(ResultType, ArgTys, false);
Mike Stumpee094222009-03-06 06:12:24 +00001047 CGM.BlockObjectAssign
1048 = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
1049 }
1050 return CGM.BlockObjectAssign;
1051}
1052
Mike Stump1851b682009-03-06 04:53:30 +00001053void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Mike Stump797b6322009-03-05 01:23:13 +00001054 llvm::Value *F = getBlockObjectDispose();
Mike Stump1851b682009-03-06 04:53:30 +00001055 llvm::Value *N;
Mike Stump797b6322009-03-05 01:23:13 +00001056 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001057 N = VMContext.getConstantInt(llvm::Type::Int32Ty, flag);
Mike Stump797b6322009-03-05 01:23:13 +00001058 Builder.CreateCall2(F, V, N);
1059}
Mike Stump00470a12009-03-05 08:32:30 +00001060
1061ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stump08920992009-03-07 02:35:30 +00001062
1063BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1064 CGBuilderTy &B)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001065 : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) {
1066 PtrToInt8Ty = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty);
Mike Stump08920992009-03-07 02:35:30 +00001067
1068 BlockHasCopyDispose = false;
1069}