blob: 41b49c7d7efc5aa80de102b3bfb09fc6b1bdb443 [file] [log] [blame]
Anders Carlssonacfde802009-02-12 00:39:25 +00001//===--- CGBlocks.cpp - Emit LLVM Code for declarations -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit blocks.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
Mike Stump6cc88f72009-03-20 21:53:12 +000016#include "clang/AST/DeclObjC.h"
Anders Carlssonacfde802009-02-12 00:39:25 +000017#include "llvm/Module.h"
Anders Carlssond5cab542009-02-12 17:55:02 +000018#include "llvm/Target/TargetData.h"
Anders Carlssonacfde802009-02-12 00:39:25 +000019#include <algorithm>
Torok Edwinf42e4a62009-08-24 13:25:12 +000020#include <cstdio>
21
Anders Carlssonacfde802009-02-12 00:39:25 +000022using namespace clang;
23using namespace CodeGen;
24
Mike Stumpcf62d392009-03-06 18:42:23 +000025llvm::Constant *CodeGenFunction::
Mike Stumpa803b0e2009-03-25 17:58:24 +000026BuildDescriptorBlockDecl(bool BlockHasCopyDispose, uint64_t Size,
27 const llvm::StructType* Ty,
Mike Stump08920992009-03-07 02:35:30 +000028 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump56129b12009-02-13 16:55:51 +000029 const llvm::Type *UnsignedLongTy
30 = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpe5fee252009-02-13 16:19:19 +000031 llvm::Constant *C;
32 std::vector<llvm::Constant*> Elts;
33
34 // reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +000035 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpe5fee252009-02-13 16:19:19 +000036 Elts.push_back(C);
37
38 // Size
Mike Stumpd6840002009-02-21 20:07:44 +000039 // FIXME: What is the right way to say this doesn't fit? We should give
40 // a user diagnostic in that case. Better fix would be to change the
41 // API to size_t.
Owen Anderson4a28d5d2009-07-24 23:12:58 +000042 C = llvm::ConstantInt::get(UnsignedLongTy, Size);
Mike Stumpe5fee252009-02-13 16:19:19 +000043 Elts.push_back(C);
44
45 if (BlockHasCopyDispose) {
46 // copy_func_helper_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000047 Elts.push_back(BuildCopyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000048
49 // destroy_func_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000050 Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000051 }
52
Nick Lewycky0d36dd22009-09-19 20:00:52 +000053 C = llvm::ConstantStruct::get(VMContext, Elts, false);
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)
Mike Stump1eb44332009-09-09 15:08:12 +000063 NSConcreteGlobalBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
Chris Lattnere2f79b62009-05-13 02:50:56 +000064 "_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)
Mike Stump1eb44332009-09-09 15:08:12 +000070 NSConcreteStackBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
Chris Lattnere2f79b62009-05-13 02:50:56 +000071 "_NSConcreteStackBlock");
Mike Stump59c5b112009-02-13 19:29:27 +000072 return NSConcreteStackBlock;
73}
74
Mike Stump00470a12009-03-05 08:32:30 +000075static void CollectBlockDeclRefInfo(const Stmt *S,
Anders Carlsson4de9fce2009-03-01 01:09:12 +000076 CodeGenFunction::BlockInfo &Info) {
77 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
78 I != E; ++I)
Daniel Dunbar82573ee2009-03-02 07:00:57 +000079 if (*I)
80 CollectBlockDeclRefInfo(*I, Info);
Mike Stump00470a12009-03-05 08:32:30 +000081
Anders Carlsson4de9fce2009-03-01 01:09:12 +000082 if (const BlockDeclRefExpr *DE = dyn_cast<BlockDeclRefExpr>(S)) {
83 // FIXME: Handle enums.
84 if (isa<FunctionDecl>(DE->getDecl()))
85 return;
Mike Stump00470a12009-03-05 08:32:30 +000086
Anders Carlsson4de9fce2009-03-01 01:09:12 +000087 if (DE->isByRef())
88 Info.ByRefDeclRefs.push_back(DE);
89 else
90 Info.ByCopyDeclRefs.push_back(DE);
91 }
92}
93
Mike Stump58a85142009-03-04 22:48:06 +000094/// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be
95/// declared as a global variable instead of on the stack.
Chris Lattnere2f79b62009-05-13 02:50:56 +000096static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info) {
Anders Carlsson4de9fce2009-03-01 01:09:12 +000097 return Info.ByRefDeclRefs.empty() && Info.ByCopyDeclRefs.empty();
98}
99
Mike Stump58a85142009-03-04 22:48:06 +0000100// FIXME: Push most into CGM, passing down a few bits, like current function
101// name.
Mike Stump8a2b4b12009-02-25 23:33:13 +0000102llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Mike Stumpe5fee252009-02-13 16:19:19 +0000103
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000104 std::string Name = CurFn->getName();
105 CodeGenFunction::BlockInfo Info(0, Name.c_str());
106 CollectBlockDeclRefInfo(BE->getBody(), Info);
107
108 // Check if the block can be global.
Mike Stump58a85142009-03-04 22:48:06 +0000109 // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like
110 // to just have one code path. We should move this function into CGM and pass
111 // CGF, then we can just check to see if CGF is 0.
Mike Stumpdab514f2009-03-04 03:23:46 +0000112 if (0 && CanBlockBeGlobal(Info))
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000113 return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
Mike Stump00470a12009-03-05 08:32:30 +0000114
115 std::vector<llvm::Constant*> Elts(5);
Mike Stumpe5fee252009-02-13 16:19:19 +0000116 llvm::Constant *C;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000117 llvm::Value *V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000118
Mike Stumpe5fee252009-02-13 16:19:19 +0000119 {
120 // C = BuildBlockStructInitlist();
121 unsigned int flags = BLOCK_HAS_DESCRIPTOR;
122
Mike Stump00470a12009-03-05 08:32:30 +0000123 // We run this first so that we set BlockHasCopyDispose from the entire
124 // block literal.
125 // __invoke
126 uint64_t subBlockSize, subBlockAlign;
127 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Mike Stumpa803b0e2009-03-25 17:58:24 +0000128 bool subBlockHasCopyDispose = false;
Mike Stump00470a12009-03-05 08:32:30 +0000129 llvm::Function *Fn
Mike Stump6cc88f72009-03-20 21:53:12 +0000130 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl, LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000131 subBlockSize,
Mike Stump00470a12009-03-05 08:32:30 +0000132 subBlockAlign,
133 subBlockDeclRefDecls,
Mike Stumpa803b0e2009-03-25 17:58:24 +0000134 subBlockHasCopyDispose);
135 BlockHasCopyDispose |= subBlockHasCopyDispose;
Mike Stump00470a12009-03-05 08:32:30 +0000136 Elts[3] = Fn;
137
Mike Stumpf5408fe2009-05-16 07:57:57 +0000138 // FIXME: Don't use BlockHasCopyDispose, it is set more often then
139 // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); }
Mike Stumpa803b0e2009-03-25 17:58:24 +0000140 if (subBlockHasCopyDispose)
Mike Stumpe5fee252009-02-13 16:19:19 +0000141 flags |= BLOCK_HAS_COPY_DISPOSE;
142
Mike Stump7d6dc4f2009-02-13 20:17:16 +0000143 // __isa
Mike Stump59c5b112009-02-13 19:29:27 +0000144 C = CGM.getNSConcreteStackBlock();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000145 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
Mike Stump00470a12009-03-05 08:32:30 +0000146 Elts[0] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000147
148 // __flags
149 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
150 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000151 C = llvm::ConstantInt::get(IntTy, flags);
Mike Stump00470a12009-03-05 08:32:30 +0000152 Elts[1] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000153
154 // __reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000155 C = llvm::ConstantInt::get(IntTy, 0);
Mike Stump00470a12009-03-05 08:32:30 +0000156 Elts[2] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000157
Mike Stump8a2b4b12009-02-25 23:33:13 +0000158 if (subBlockDeclRefDecls.size() == 0) {
Mike Stumpcf62d392009-03-06 18:42:23 +0000159 // __descriptor
Mike Stumpa803b0e2009-03-25 17:58:24 +0000160 Elts[4] = BuildDescriptorBlockDecl(subBlockHasCopyDispose, subBlockSize, 0, 0);
Mike Stumpcf62d392009-03-06 18:42:23 +0000161
Mike Stump5570cfe2009-03-01 20:07:53 +0000162 // Optimize to being a global block.
163 Elts[0] = CGM.getNSConcreteGlobalBlock();
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000164 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
Mike Stump5570cfe2009-03-01 20:07:53 +0000165
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000166 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000167
168 char Name[32];
169 sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
Owen Anderson1c431b32009-07-08 19:05:04 +0000170 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Mike Stump8a2b4b12009-02-25 23:33:13 +0000171 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000172 C, Name);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000173 QualType BPT = BE->getType();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000174 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000175 return C;
176 }
Mike Stump00470a12009-03-05 08:32:30 +0000177
Mike Stump8a2b4b12009-02-25 23:33:13 +0000178 std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size());
Mike Stump08920992009-03-07 02:35:30 +0000179 for (int i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000180 Types[i] = Elts[i]->getType();
Mike Stump08920992009-03-07 02:35:30 +0000181 Types[4] = PtrToInt8Ty;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000182
Mike Stumpa99038c2009-02-28 09:07:16 +0000183 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
184 const Expr *E = subBlockDeclRefDecls[i];
185 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
186 QualType Ty = E->getType();
Mike Stumpdab514f2009-03-04 03:23:46 +0000187 if (BDRE && BDRE->isByRef()) {
Anders Carlsson9ad55132009-09-09 02:51:03 +0000188 Types[i+5] = llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000189 } else
190 Types[i+5] = ConvertType(Ty);
Mike Stumpa99038c2009-02-28 09:07:16 +0000191 }
Mike Stump8a2b4b12009-02-25 23:33:13 +0000192
Owen Anderson47a434f2009-08-05 23:18:46 +0000193 llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000194
195 llvm::AllocaInst *A = CreateTempAlloca(Ty);
196 A->setAlignment(subBlockAlign);
197 V = A;
198
Mike Stump08920992009-03-07 02:35:30 +0000199 std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size());
200 int helpersize = 0;
201
202 for (unsigned i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000203 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
Mike Stump00470a12009-03-05 08:32:30 +0000204
Mike Stump8a2b4b12009-02-25 23:33:13 +0000205 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
206 {
Mike Stumpa99038c2009-02-28 09:07:16 +0000207 // FIXME: Push const down.
208 Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
209 DeclRefExpr *DR;
210 ValueDecl *VD;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000211
Mike Stumpa99038c2009-02-28 09:07:16 +0000212 DR = dyn_cast<DeclRefExpr>(E);
213 // Skip padding.
214 if (DR) continue;
215
216 BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
217 VD = BDRE->getDecl();
218
Mike Stumpdab514f2009-03-04 03:23:46 +0000219 llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp");
Mike Stump08920992009-03-07 02:35:30 +0000220 NoteForHelper[helpersize].index = i+5;
221 NoteForHelper[helpersize].RequiresCopying = BlockRequiresCopying(VD->getType());
222 NoteForHelper[helpersize].flag
223 = VD->getType()->isBlockPointerType() ? BLOCK_FIELD_IS_BLOCK : BLOCK_FIELD_IS_OBJECT;
224
Mike Stumpa99038c2009-02-28 09:07:16 +0000225 if (LocalDeclMap[VD]) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000226 if (BDRE->isByRef()) {
Mike Stump08920992009-03-07 02:35:30 +0000227 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
Mike Stumpf4bc3122009-03-07 06:04:31 +0000228 // FIXME: Someone double check this.
229 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000230 llvm::Value *Loc = LocalDeclMap[VD];
231 Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
232 Loc = Builder.CreateLoad(Loc, false);
Mike Stumpdab514f2009-03-04 03:23:46 +0000233 Builder.CreateStore(Loc, Addr);
Mike Stump08920992009-03-07 02:35:30 +0000234 ++helpersize;
Mike Stumpdab514f2009-03-04 03:23:46 +0000235 continue;
236 } else
237 E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD),
238 VD->getType(), SourceLocation(),
239 false, false);
Mike Stumpa99038c2009-02-28 09:07:16 +0000240 }
Mike Stumpdab514f2009-03-04 03:23:46 +0000241 if (BDRE->isByRef()) {
Mike Stumpa8b60c92009-03-21 21:00:35 +0000242 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
243 // FIXME: Someone double check this.
244 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpa99038c2009-02-28 09:07:16 +0000245 E = new (getContext())
246 UnaryOperator(E, UnaryOperator::AddrOf,
247 getContext().getPointerType(E->getType()),
248 SourceLocation());
Mike Stumpdab514f2009-03-04 03:23:46 +0000249 }
Mike Stump08920992009-03-07 02:35:30 +0000250 ++helpersize;
Mike Stumpa99038c2009-02-28 09:07:16 +0000251
Mike Stumpa99038c2009-02-28 09:07:16 +0000252 RValue r = EmitAnyExpr(E, Addr, false);
Mike Stumpdab514f2009-03-04 03:23:46 +0000253 if (r.isScalar()) {
254 llvm::Value *Loc = r.getScalarVal();
255 const llvm::Type *Ty = Types[i+5];
256 if (BDRE->isByRef()) {
Mike Stump58a85142009-03-04 22:48:06 +0000257 // E is now the address of the value field, instead, we want the
258 // address of the actual ByRef struct. We optimize this slightly
259 // compared to gcc by not grabbing the forwarding slot as this must
260 // be done during Block_copy for us, and we can postpone the work
261 // until then.
262 uint64_t offset = BlockDecls[BDRE->getDecl()];
Mike Stump00470a12009-03-05 08:32:30 +0000263
Mike Stump58a85142009-03-04 22:48:06 +0000264 llvm::Value *BlockLiteral = LoadBlockStruct();
Mike Stump00470a12009-03-05 08:32:30 +0000265
Mike Stump58a85142009-03-04 22:48:06 +0000266 Loc = Builder.CreateGEP(BlockLiteral,
Owen Anderson0032b272009-08-13 21:57:51 +0000267 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Mike Stump58a85142009-03-04 22:48:06 +0000268 offset),
269 "block.literal");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000270 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000271 Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stump58a85142009-03-04 22:48:06 +0000272 Loc = Builder.CreateLoad(Loc, false);
273 // Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000274 }
275 Builder.CreateStore(Loc, Addr);
276 } else if (r.isComplex())
Mike Stump8a2b4b12009-02-25 23:33:13 +0000277 // FIXME: implement
278 ErrorUnsupported(BE, "complex in block literal");
279 else if (r.isAggregate())
280 ; // Already created into the destination
281 else
282 assert (0 && "bad block variable");
283 // FIXME: Ensure that the offset created by the backend for
284 // the struct matches the previously computed offset in BlockDecls.
285 }
Mike Stump08920992009-03-07 02:35:30 +0000286 NoteForHelper.resize(helpersize);
Mike Stumpcf62d392009-03-06 18:42:23 +0000287
288 // __descriptor
Mike Stumpa803b0e2009-03-25 17:58:24 +0000289 llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockHasCopyDispose,
290 subBlockSize, Ty,
Mike Stump08920992009-03-07 02:35:30 +0000291 &NoteForHelper);
292 Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
293 Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
Mike Stumpe5fee252009-02-13 16:19:19 +0000294 }
Mike Stump00470a12009-03-05 08:32:30 +0000295
Mike Stumpbd65cac2009-02-19 01:01:04 +0000296 QualType BPT = BE->getType();
Mike Stump8a2b4b12009-02-25 23:33:13 +0000297 return Builder.CreateBitCast(V, ConvertType(BPT));
Mike Stumpe5fee252009-02-13 16:19:19 +0000298}
299
300
Mike Stump2a998142009-03-04 18:17:45 +0000301const llvm::Type *BlockModule::getBlockDescriptorType() {
Mike Stumpab695142009-02-13 15:16:56 +0000302 if (BlockDescriptorType)
303 return BlockDescriptorType;
304
Mike Stumpa5448542009-02-13 15:32:32 +0000305 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000306 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000307
Mike Stumpab695142009-02-13 15:16:56 +0000308 // struct __block_descriptor {
309 // unsigned long reserved;
310 // unsigned long block_size;
311 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000312 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(),
313 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 Anderson96e0fc72009-07-29 22:16:19 +0000328 llvm::PointerType::getUnqual(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 Anderson47a434f2009-08-05 23:18:46 +0000340 GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
341 PtrToInt8Ty,
Mike Stump7cbb3602009-02-13 16:01:35 +0000342 IntTy,
343 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000344 PtrToInt8Ty,
Mike Stump9b8a7972009-02-13 15:25:34 +0000345 BlockDescPtrTy,
346 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000347
Mike Stump9b8a7972009-02-13 15:25:34 +0000348 getModule().addTypeName("struct.__block_literal_generic",
349 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000350
Mike Stump9b8a7972009-02-13 15:25:34 +0000351 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000352}
353
Mike Stump2a998142009-03-04 18:17:45 +0000354const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000355 if (GenericExtendedBlockLiteralType)
356 return GenericExtendedBlockLiteralType;
357
Mike Stumpbd65cac2009-02-19 01:01:04 +0000358 const llvm::Type *BlockDescPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000359 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpbd65cac2009-02-19 01:01:04 +0000360
361 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
362 getTypes().ConvertType(getContext().IntTy));
363
364 // struct __block_literal_generic {
365 // void *__isa;
366 // int __flags;
367 // int __reserved;
368 // void (*__invoke)(void *);
369 // struct __block_descriptor *__descriptor;
370 // void *__copy_func_helper_decl;
371 // void *__destroy_func_decl;
372 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000373 GenericExtendedBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
374 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 =
Ted Kremenek6217b802009-07-29 21:53:49 +0000391 E->getCallee()->getType()->getAs<BlockPointerType>();
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 Anderson96e0fc72009-07-29 22:16:19 +0000397 llvm::PointerType::getUnqual(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 Anderson0032b272009-08-13 21:57:51 +0000408 llvm::PointerType::getUnqual(
409 llvm::Type::getInt8Ty(VMContext)),
Anders Carlssonacfde802009-02-12 00:39:25 +0000410 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000411
Anders Carlssonacfde802009-02-12 00:39:25 +0000412 // Add the block literal.
413 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
414 CallArgList Args;
415 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000416
Anders Carlsson782f3972009-04-08 23:13:16 +0000417 QualType FnType = BPT->getPointeeType();
418
Anders Carlssonacfde802009-02-12 00:39:25 +0000419 // And the rest of the arguments.
John McCall183700f2009-09-21 23:43:11 +0000420 EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(),
Anders Carlsson782f3972009-04-08 23:13:16 +0000421 E->arg_begin(), E->arg_end());
Mike Stumpa5448542009-02-13 15:32:32 +0000422
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000423 // Load the function.
424 llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp");
425
John McCall183700f2009-09-21 23:43:11 +0000426 QualType ResultType = FnType->getAs<FunctionType>()->getResultType();
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000427
Mike Stump1eb44332009-09-09 15:08:12 +0000428 const CGFunctionInfo &FnInfo =
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000429 CGM.getTypes().getFunctionInfo(ResultType, Args);
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000431 // Cast the function pointer to the right type.
Mike Stump1eb44332009-09-09 15:08:12 +0000432 const llvm::Type *BlockFTy =
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000433 CGM.getTypes().GetFunctionType(FnInfo, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Owen Anderson96e0fc72009-07-29 22:16:19 +0000435 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000436 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Anders Carlssonacfde802009-02-12 00:39:25 +0000438 // And call the block.
Anders Carlssonf8544a42009-04-07 00:20:24 +0000439 return EmitCall(FnInfo, Func, Args);
Anders Carlssonacfde802009-02-12 00:39:25 +0000440}
Anders Carlssond5cab542009-02-12 17:55:02 +0000441
Mike Stumpdab514f2009-03-04 03:23:46 +0000442llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000443 const ValueDecl *VD = E->getDecl();
444
445 uint64_t &offset = BlockDecls[VD];
Mike Stumpdab514f2009-03-04 03:23:46 +0000446
Mike Stumpdab514f2009-03-04 03:23:46 +0000447
Mike Stumpdab514f2009-03-04 03:23:46 +0000448 // See if we have already allocated an offset for this variable.
449 if (offset == 0) {
Mike Stump00470a12009-03-05 08:32:30 +0000450 // Don't run the expensive check, unless we have to.
451 if (!BlockHasCopyDispose && BlockRequiresCopying(E->getType()))
452 BlockHasCopyDispose = true;
Mike Stumpdab514f2009-03-04 03:23:46 +0000453 // if not, allocate one now.
454 offset = getBlockOffset(E);
455 }
456
457 llvm::Value *BlockLiteral = LoadBlockStruct();
458 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
Owen Anderson0032b272009-08-13 21:57:51 +0000459 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000460 offset),
Mike Stump58a85142009-03-04 22:48:06 +0000461 "block.literal");
Mike Stumpdab514f2009-03-04 03:23:46 +0000462 if (E->isByRef()) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000463 const llvm::Type *PtrStructTy
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000464 = llvm::PointerType::get(BuildByRefType(VD), 0);
Mike Stumpa8b60c92009-03-21 21:00:35 +0000465 // The block literal will need a copy/destroy helper.
466 BlockHasCopyDispose = true;
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000467
468 const llvm::Type *Ty = PtrStructTy;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000469 Ty = llvm::PointerType::get(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);
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000475 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
476 VD->getNameAsString());
Mike Stumpdab514f2009-03-04 03:23:46 +0000477 } else {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000478 const llvm::Type *Ty = CGM.getTypes().ConvertType(VD->getType());
479
Owen Anderson96e0fc72009-07-29 22:16:19 +0000480 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000481 V = Builder.CreateBitCast(V, Ty);
482 }
483 return V;
484}
485
Mike Stump6cc88f72009-03-20 21:53:12 +0000486void CodeGenFunction::BlockForwardSelf() {
487 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
488 ImplicitParamDecl *SelfDecl = OMD->getSelfDecl();
489 llvm::Value *&DMEntry = LocalDeclMap[SelfDecl];
490 if (DMEntry)
491 return;
492 // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care
493 BlockDeclRefExpr *BDRE = new (getContext())
494 BlockDeclRefExpr(SelfDecl,
495 SelfDecl->getType(), SourceLocation(), false);
496 DMEntry = GetAddrOfBlockDecl(BDRE);
497}
498
Mike Stump67a64482009-02-14 22:16:35 +0000499llvm::Constant *
Mike Stump90a90432009-03-04 18:47:42 +0000500BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000501 // Generate the block descriptor.
502 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000503 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
504 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000505
Anders Carlssond5cab542009-02-12 17:55:02 +0000506 llvm::Constant *DescriptorFields[2];
Mike Stumpa5448542009-02-13 15:32:32 +0000507
Anders Carlssond5cab542009-02-12 17:55:02 +0000508 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000509 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000510
Anders Carlssond5cab542009-02-12 17:55:02 +0000511 // Block literal size. For global blocks we just use the size of the generic
512 // block literal struct.
Mike Stumpa5448542009-02-13 15:32:32 +0000513 uint64_t BlockLiteralSize =
Mike Stump9b8a7972009-02-13 15:25:34 +0000514 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Owen Andersona1cf15f2009-07-14 23:10:40 +0000515 DescriptorFields[1] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000516 llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpa5448542009-02-13 15:32:32 +0000517
518 llvm::Constant *DescriptorStruct =
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000519 llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 2, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000520
Anders Carlssond5cab542009-02-12 17:55:02 +0000521 llvm::GlobalVariable *Descriptor =
Owen Anderson1c431b32009-07-08 19:05:04 +0000522 new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000523 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000524 DescriptorStruct, "__block_descriptor_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000525
Anders Carlssond5cab542009-02-12 17:55:02 +0000526 // Generate the constants for the block literal.
527 llvm::Constant *LiteralFields[5];
Mike Stumpa5448542009-02-13 15:32:32 +0000528
Mike Stump67a64482009-02-14 22:16:35 +0000529 CodeGenFunction::BlockInfo Info(0, n);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000530 uint64_t subBlockSize, subBlockAlign;
Mike Stumpa99038c2009-02-28 09:07:16 +0000531 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Daniel Dunbard67b09a2009-03-12 03:07:24 +0000532 bool subBlockHasCopyDispose = false;
Mike Stump7f28a9c2009-03-13 23:34:28 +0000533 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000534 llvm::Function *Fn
Mike Stump6cc88f72009-03-20 21:53:12 +0000535 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000536 subBlockSize,
Mike Stump90a90432009-03-04 18:47:42 +0000537 subBlockAlign,
Mike Stump00470a12009-03-05 08:32:30 +0000538 subBlockDeclRefDecls,
539 subBlockHasCopyDispose);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000540 assert(subBlockSize == BlockLiteralSize
541 && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000542
Anders Carlssond5cab542009-02-12 17:55:02 +0000543 // isa
Mike Stumpf99f1d02009-02-13 17:23:42 +0000544 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000545
Anders Carlssond5cab542009-02-12 17:55:02 +0000546 // Flags
Mike Stump00470a12009-03-05 08:32:30 +0000547 LiteralFields[1] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000548 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR);
Mike Stumpa5448542009-02-13 15:32:32 +0000549
Anders Carlssond5cab542009-02-12 17:55:02 +0000550 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000551 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000552
Anders Carlssond5cab542009-02-12 17:55:02 +0000553 // Function
554 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000555
Anders Carlssond5cab542009-02-12 17:55:02 +0000556 // Descriptor
557 LiteralFields[4] = Descriptor;
Mike Stumpa5448542009-02-13 15:32:32 +0000558
559 llvm::Constant *BlockLiteralStruct =
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000560 llvm::ConstantStruct::get(VMContext, &LiteralFields[0], 5, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000561
562 llvm::GlobalVariable *BlockLiteral =
Owen Anderson1c431b32009-07-08 19:05:04 +0000563 new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000564 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000565 BlockLiteralStruct, "__block_literal_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000566
Anders Carlssond5cab542009-02-12 17:55:02 +0000567 return BlockLiteral;
568}
569
Mike Stump4e7a1f72009-02-21 20:00:35 +0000570llvm::Value *CodeGenFunction::LoadBlockStruct() {
571 return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self");
572}
573
Mike Stump00470a12009-03-05 08:32:30 +0000574llvm::Function *
575CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
576 const BlockInfo& Info,
Mike Stump6cc88f72009-03-20 21:53:12 +0000577 const Decl *OuterFuncDecl,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000578 llvm::DenseMap<const Decl*, llvm::Value*> ldm,
Mike Stump00470a12009-03-05 08:32:30 +0000579 uint64_t &Size,
580 uint64_t &Align,
581 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls,
582 bool &subBlockHasCopyDispose) {
Devang Patel963dfbd2009-04-15 21:51:44 +0000583
584 // Check if we should generate debug info for this block.
585 if (CGM.getDebugInfo())
586 DebugInfo = CGM.getDebugInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Mike Stump7f28a9c2009-03-13 23:34:28 +0000588 // Arrange for local static and local extern declarations to appear
589 // to be local to this function as well, as they are directly referenced
590 // in a block.
591 for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
592 i != ldm.end();
593 ++i) {
594 const VarDecl *VD = dyn_cast<VarDecl>(i->first);
Mike Stump1eb44332009-09-09 15:08:12 +0000595
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000596 if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage())
Mike Stump7f28a9c2009-03-13 23:34:28 +0000597 LocalDeclMap[VD] = i->second;
598 }
599
Eli Friedman48f91222009-03-28 03:24:54 +0000600 // FIXME: We need to rearrange the code for copy/dispose so we have this
601 // sooner, so we can calculate offsets correctly.
602 if (!BlockHasCopyDispose)
603 BlockOffset = CGM.getTargetData()
604 .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8;
605 else
606 BlockOffset = CGM.getTargetData()
607 .getTypeStoreSizeInBits(CGM.getGenericExtendedBlockLiteralType()) / 8;
608 BlockAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
609
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000610 const FunctionType *BlockFunctionType = BExpr->getFunctionType();
611 QualType ResultType;
612 bool IsVariadic;
Mike Stump1eb44332009-09-09 15:08:12 +0000613 if (const FunctionProtoType *FTy =
Fariborz Jahanianda0895d2009-04-11 18:54:57 +0000614 dyn_cast<FunctionProtoType>(BlockFunctionType)) {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000615 ResultType = FTy->getResultType();
616 IsVariadic = FTy->isVariadic();
Mike Stumpb3589f42009-07-30 22:28:39 +0000617 } else {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000618 // K&R style block.
619 ResultType = BlockFunctionType->getResultType();
620 IsVariadic = false;
621 }
Mike Stumpa5448542009-02-13 15:32:32 +0000622
Anders Carlssond5cab542009-02-12 17:55:02 +0000623 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000624
Chris Lattner161d36d2009-02-28 19:01:03 +0000625 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlssond5cab542009-02-12 17:55:02 +0000626
627 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000628 ImplicitParamDecl *SelfDecl =
Anders Carlssond5cab542009-02-12 17:55:02 +0000629 ImplicitParamDecl::Create(getContext(), 0,
630 SourceLocation(), 0,
631 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000632
Anders Carlssond5cab542009-02-12 17:55:02 +0000633 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump4e7a1f72009-02-21 20:00:35 +0000634 BlockStructDecl = SelfDecl;
Mike Stumpa5448542009-02-13 15:32:32 +0000635
Steve Naroffe78b8092009-03-13 16:56:44 +0000636 for (BlockDecl::param_const_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000637 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000638 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000639
640 const CGFunctionInfo &FI =
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000641 CGM.getTypes().getFunctionInfo(ResultType, Args);
Anders Carlssond5cab542009-02-12 17:55:02 +0000642
Mike Stump67a64482009-02-14 22:16:35 +0000643 std::string Name = std::string("__") + Info.Name + "_block_invoke_";
Anders Carlssond5cab542009-02-12 17:55:02 +0000644 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000645 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic);
Mike Stumpa5448542009-02-13 15:32:32 +0000646
647 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000648 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
649 Name,
650 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000651
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000652 CGM.SetInternalFunctionAttributes(BD, Fn, FI);
653
Chris Lattner4863db42009-04-23 07:18:56 +0000654 StartFunction(BD, ResultType, Fn, Args,
Chris Lattner161d36d2009-02-28 19:01:03 +0000655 BExpr->getBody()->getLocEnd());
Chris Lattner4863db42009-04-23 07:18:56 +0000656 CurFuncDecl = OuterFuncDecl;
Chris Lattnerb5437d22009-04-23 05:30:27 +0000657 CurCodeDecl = BD;
Chris Lattner161d36d2009-02-28 19:01:03 +0000658 EmitStmt(BExpr->getBody());
659 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlssond5cab542009-02-12 17:55:02 +0000660
Mike Stump8a2b4b12009-02-25 23:33:13 +0000661 // The runtime needs a minimum alignment of a void *.
662 uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
663 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign);
664
Mike Stump4e7a1f72009-02-21 20:00:35 +0000665 Size = BlockOffset;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000666 Align = BlockAlign;
667 subBlockDeclRefDecls = BlockDeclRefDecls;
Mike Stump00470a12009-03-05 08:32:30 +0000668 subBlockHasCopyDispose |= BlockHasCopyDispose;
Anders Carlssond5cab542009-02-12 17:55:02 +0000669 return Fn;
670}
Mike Stumpa99038c2009-02-28 09:07:16 +0000671
Mike Stump08920992009-03-07 02:35:30 +0000672uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
Mike Stumpa99038c2009-02-28 09:07:16 +0000673 const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
674
675 uint64_t Size = getContext().getTypeSize(D->getType()) / 8;
676 uint64_t Align = getContext().getDeclAlignInBytes(D);
677
678 if (BDRE->isByRef()) {
679 Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8;
680 Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
681 }
682
683 assert ((Align > 0) && "alignment must be 1 byte or more");
684
685 uint64_t OldOffset = BlockOffset;
686
687 // Ensure proper alignment, even if it means we have to have a gap
688 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align);
689 BlockAlign = std::max(Align, BlockAlign);
Mike Stump00470a12009-03-05 08:32:30 +0000690
Mike Stumpa99038c2009-02-28 09:07:16 +0000691 uint64_t Pad = BlockOffset - OldOffset;
692 if (Pad) {
Owen Anderson0032b272009-08-13 21:57:51 +0000693 llvm::ArrayType::get(llvm::Type::getInt8Ty(VMContext), Pad);
Mike Stumpa99038c2009-02-28 09:07:16 +0000694 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
695 llvm::APInt(32, Pad),
696 ArrayType::Normal, 0);
697 ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000698 0, QualType(PadTy), 0, VarDecl::None);
Mike Stumpa99038c2009-02-28 09:07:16 +0000699 Expr *E;
700 E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
701 SourceLocation(), false, false);
702 BlockDeclRefDecls.push_back(E);
703 }
704 BlockDeclRefDecls.push_back(BDRE);
705
706 BlockOffset += Size;
707 return BlockOffset-Size;
708}
Mike Stumpdab514f2009-03-04 03:23:46 +0000709
Mike Stump08920992009-03-07 02:35:30 +0000710llvm::Constant *BlockFunction::
711GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000712 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000713 QualType R = getContext().VoidTy;
714
715 FunctionArgList Args;
716 // FIXME: This leaks
Mike Stump08920992009-03-07 02:35:30 +0000717 ImplicitParamDecl *Dst =
718 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
719 getContext().getPointerType(getContext().VoidTy));
720 Args.push_back(std::make_pair(Dst, Dst->getType()));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000721 ImplicitParamDecl *Src =
722 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
723 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000724 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Mike Stumpa4f668f2009-03-06 01:33:24 +0000726 const CGFunctionInfo &FI =
727 CGM.getTypes().getFunctionInfo(R, Args);
728
Mike Stump3899a7f2009-06-05 23:26:36 +0000729 // FIXME: We'd like to put these into a mergable by content, with
730 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000731 std::string Name = std::string("__copy_helper_block_");
732 CodeGenTypes &Types = CGM.getTypes();
733 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
734
735 llvm::Function *Fn =
736 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
737 Name,
738 &CGM.getModule());
739
740 IdentifierInfo *II
741 = &CGM.getContext().Idents.get("__copy_helper_block_");
742
743 FunctionDecl *FD = FunctionDecl::Create(getContext(),
744 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000745 SourceLocation(), II, R, 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000746 FunctionDecl::Static, false,
747 true);
748 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump08920992009-03-07 02:35:30 +0000749
750 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
751 llvm::Type *PtrPtrT;
Mike Stump08920992009-03-07 02:35:30 +0000752
Mike Stumpb7477cf2009-04-10 18:52:28 +0000753 if (NoteForHelperp) {
754 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump08920992009-03-07 02:35:30 +0000755
Owen Anderson96e0fc72009-07-29 22:16:19 +0000756 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000757 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
758 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump08920992009-03-07 02:35:30 +0000759
Mike Stumpb7477cf2009-04-10 18:52:28 +0000760 llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000761 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000762 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000763 DstObj = Builder.CreateBitCast(DstObj, PtrPtrT);
764 DstObj = Builder.CreateLoad(DstObj);
Mike Stump08920992009-03-07 02:35:30 +0000765
Mike Stumpb7477cf2009-04-10 18:52:28 +0000766 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
767 int flag = NoteForHelper[i].flag;
768 int index = NoteForHelper[i].index;
Mike Stump08920992009-03-07 02:35:30 +0000769
Mike Stumpb7477cf2009-04-10 18:52:28 +0000770 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
771 || NoteForHelper[i].RequiresCopying) {
772 llvm::Value *Srcv = SrcObj;
773 Srcv = Builder.CreateStructGEP(Srcv, index);
774 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000775 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000776 Srcv = Builder.CreateLoad(Srcv);
777
778 llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
779 Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
780
Owen Anderson0032b272009-08-13 21:57:51 +0000781 llvm::Value *N = llvm::ConstantInt::get(
782 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000783 llvm::Value *F = getBlockObjectAssign();
784 Builder.CreateCall3(F, Dstv, Srcv, N);
785 }
Mike Stump08920992009-03-07 02:35:30 +0000786 }
787 }
788
Mike Stumpa4f668f2009-03-06 01:33:24 +0000789 CGF.FinishFunction();
790
Owen Anderson3c4972d2009-07-29 18:54:39 +0000791 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000792}
793
Mike Stumpcf62d392009-03-06 18:42:23 +0000794llvm::Constant *BlockFunction::
Mike Stump08920992009-03-07 02:35:30 +0000795GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
796 const llvm::StructType* T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000797 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000798 QualType R = getContext().VoidTy;
799
800 FunctionArgList Args;
801 // FIXME: This leaks
802 ImplicitParamDecl *Src =
803 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
804 getContext().getPointerType(getContext().VoidTy));
805
806 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Mike Stumpa4f668f2009-03-06 01:33:24 +0000808 const CGFunctionInfo &FI =
809 CGM.getTypes().getFunctionInfo(R, Args);
810
Mike Stump3899a7f2009-06-05 23:26:36 +0000811 // FIXME: We'd like to put these into a mergable by content, with
812 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000813 std::string Name = std::string("__destroy_helper_block_");
814 CodeGenTypes &Types = CGM.getTypes();
815 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
816
817 llvm::Function *Fn =
818 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
819 Name,
820 &CGM.getModule());
821
822 IdentifierInfo *II
823 = &CGM.getContext().Idents.get("__destroy_helper_block_");
824
825 FunctionDecl *FD = FunctionDecl::Create(getContext(),
826 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000827 SourceLocation(), II, R, 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000828 FunctionDecl::Static, false,
829 true);
830 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1edf6b62009-03-07 02:53:18 +0000831
Mike Stumpb7477cf2009-04-10 18:52:28 +0000832 if (NoteForHelperp) {
833 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump1edf6b62009-03-07 02:53:18 +0000834
Mike Stumpb7477cf2009-04-10 18:52:28 +0000835 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
836 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000837 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000838 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
839 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump1edf6b62009-03-07 02:53:18 +0000840
Mike Stumpb7477cf2009-04-10 18:52:28 +0000841 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
842 int flag = NoteForHelper[i].flag;
843 int index = NoteForHelper[i].index;
Mike Stump1edf6b62009-03-07 02:53:18 +0000844
Mike Stumpb7477cf2009-04-10 18:52:28 +0000845 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
846 || NoteForHelper[i].RequiresCopying) {
847 llvm::Value *Srcv = SrcObj;
848 Srcv = Builder.CreateStructGEP(Srcv, index);
849 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000850 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000851 Srcv = Builder.CreateLoad(Srcv);
852
853 BuildBlockRelease(Srcv, flag);
854 }
Mike Stump1edf6b62009-03-07 02:53:18 +0000855 }
856 }
857
Mike Stumpa4f668f2009-03-06 01:33:24 +0000858 CGF.FinishFunction();
859
Owen Anderson3c4972d2009-07-29 18:54:39 +0000860 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000861}
862
Mike Stump08920992009-03-07 02:35:30 +0000863llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000864 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump08920992009-03-07 02:35:30 +0000865 return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
866 T, NoteForHelper);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000867}
868
Mike Stump08920992009-03-07 02:35:30 +0000869llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000870 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stump08920992009-03-07 02:35:30 +0000871 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000872 T, NoteForHelperp);
Mike Stumpdab514f2009-03-04 03:23:46 +0000873}
Mike Stump797b6322009-03-05 01:23:13 +0000874
Mike Stumpee094222009-03-06 06:12:24 +0000875llvm::Constant *BlockFunction::
876GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
Mike Stump45031c02009-03-06 02:29:21 +0000877 QualType R = getContext().VoidTy;
878
879 FunctionArgList Args;
880 // FIXME: This leaks
Mike Stumpee094222009-03-06 06:12:24 +0000881 ImplicitParamDecl *Dst =
882 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
883 getContext().getPointerType(getContext().VoidTy));
884 Args.push_back(std::make_pair(Dst, Dst->getType()));
885
886 // FIXME: This leaks
Mike Stump45031c02009-03-06 02:29:21 +0000887 ImplicitParamDecl *Src =
888 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
889 getContext().getPointerType(getContext().VoidTy));
Mike Stump45031c02009-03-06 02:29:21 +0000890 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Mike Stump45031c02009-03-06 02:29:21 +0000892 const CGFunctionInfo &FI =
893 CGM.getTypes().getFunctionInfo(R, Args);
894
895 std::string Name = std::string("__Block_byref_id_object_copy_");
896 CodeGenTypes &Types = CGM.getTypes();
897 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
898
Mike Stump3899a7f2009-06-05 23:26:36 +0000899 // FIXME: We'd like to put these into a mergable by content, with
900 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +0000901 llvm::Function *Fn =
902 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
903 Name,
904 &CGM.getModule());
905
906 IdentifierInfo *II
907 = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
908
909 FunctionDecl *FD = FunctionDecl::Create(getContext(),
910 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000911 SourceLocation(), II, R, 0,
Mike Stump45031c02009-03-06 02:29:21 +0000912 FunctionDecl::Static, false,
913 true);
914 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpee094222009-03-06 06:12:24 +0000915
916 // dst->x
917 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000918 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +0000919 V = Builder.CreateLoad(V);
Mike Stumpee094222009-03-06 06:12:24 +0000920 V = Builder.CreateStructGEP(V, 6, "x");
921 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
922
923 // src->x
924 V = CGF.GetAddrOfLocalVar(Src);
925 V = Builder.CreateLoad(V);
926 V = Builder.CreateBitCast(V, T);
927 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000928 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpee094222009-03-06 06:12:24 +0000929 llvm::Value *SrcObj = Builder.CreateLoad(V);
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Mike Stumpee094222009-03-06 06:12:24 +0000931 flag |= BLOCK_BYREF_CALLER;
932
Owen Anderson0032b272009-08-13 21:57:51 +0000933 llvm::Value *N = llvm::ConstantInt::get(
934 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpee094222009-03-06 06:12:24 +0000935 llvm::Value *F = getBlockObjectAssign();
936 Builder.CreateCall3(F, DstObj, SrcObj, N);
937
Mike Stump45031c02009-03-06 02:29:21 +0000938 CGF.FinishFunction();
939
Owen Anderson3c4972d2009-07-29 18:54:39 +0000940 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +0000941}
942
Mike Stump1851b682009-03-06 04:53:30 +0000943llvm::Constant *
944BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
945 int flag) {
Mike Stump45031c02009-03-06 02:29:21 +0000946 QualType R = getContext().VoidTy;
947
948 FunctionArgList Args;
949 // FIXME: This leaks
950 ImplicitParamDecl *Src =
951 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
952 getContext().getPointerType(getContext().VoidTy));
953
954 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Mike Stump45031c02009-03-06 02:29:21 +0000956 const CGFunctionInfo &FI =
957 CGM.getTypes().getFunctionInfo(R, Args);
958
959 std::string Name = std::string("__Block_byref_id_object_dispose_");
960 CodeGenTypes &Types = CGM.getTypes();
961 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
962
Mike Stump3899a7f2009-06-05 23:26:36 +0000963 // FIXME: We'd like to put these into a mergable by content, with
964 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +0000965 llvm::Function *Fn =
966 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
967 Name,
968 &CGM.getModule());
969
970 IdentifierInfo *II
971 = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
972
973 FunctionDecl *FD = FunctionDecl::Create(getContext(),
974 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000975 SourceLocation(), II, R, 0,
Mike Stump45031c02009-03-06 02:29:21 +0000976 FunctionDecl::Static, false,
977 true);
978 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1851b682009-03-06 04:53:30 +0000979
980 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000981 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +0000982 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +0000983 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000984 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +0000985 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +0000986
Mike Stump1851b682009-03-06 04:53:30 +0000987 flag |= BLOCK_BYREF_CALLER;
988 BuildBlockRelease(V, flag);
Mike Stump45031c02009-03-06 02:29:21 +0000989 CGF.FinishFunction();
990
Owen Anderson3c4972d2009-07-29 18:54:39 +0000991 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +0000992}
993
Mike Stumpee094222009-03-06 06:12:24 +0000994llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
Mike Stump3899a7f2009-06-05 23:26:36 +0000995 int flag, unsigned Align) {
996 // All alignments below that of pointer alignment collpase down to just
997 // pointer alignment, as we always have at least that much alignment to begin
998 // with.
999 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
1000 // As an optimization, we only generate a single function of each kind we
1001 // might need. We need a different one for each alignment and for each
1002 // setting of flags. We mix Align and flag to get the kind.
1003 uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag;
1004 llvm::Constant *& Entry = CGM.AssignCache[kind];
1005 if (Entry)
1006 return Entry;
1007 return Entry=CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001008}
1009
Mike Stump1851b682009-03-06 04:53:30 +00001010llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
Mike Stump3899a7f2009-06-05 23:26:36 +00001011 int flag,
1012 unsigned Align) {
1013 // All alignments below that of pointer alignment collpase down to just
1014 // pointer alignment, as we always have at least that much alignment to begin
1015 // with.
1016 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
1017 // As an optimization, we only generate a single function of each kind we
1018 // might need. We need a different one for each alignment and for each
1019 // setting of flags. We mix Align and flag to get the kind.
1020 uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag;
1021 llvm::Constant *& Entry = CGM.DestroyCache[kind];
1022 if (Entry)
1023 return Entry;
1024 return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001025}
1026
Mike Stump797b6322009-03-05 01:23:13 +00001027llvm::Value *BlockFunction::getBlockObjectDispose() {
1028 if (CGM.BlockObjectDispose == 0) {
1029 const llvm::FunctionType *FTy;
1030 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001031 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stump797b6322009-03-05 01:23:13 +00001032 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001033 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson96e0fc72009-07-29 22:16:19 +00001034 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stump00470a12009-03-05 08:32:30 +00001035 CGM.BlockObjectDispose
Mike Stump797b6322009-03-05 01:23:13 +00001036 = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
1037 }
1038 return CGM.BlockObjectDispose;
1039}
1040
Mike Stumpee094222009-03-06 06:12:24 +00001041llvm::Value *BlockFunction::getBlockObjectAssign() {
1042 if (CGM.BlockObjectAssign == 0) {
1043 const llvm::FunctionType *FTy;
1044 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001045 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stumpee094222009-03-06 06:12:24 +00001046 ArgTys.push_back(PtrToInt8Ty);
1047 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001048 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson96e0fc72009-07-29 22:16:19 +00001049 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stumpee094222009-03-06 06:12:24 +00001050 CGM.BlockObjectAssign
1051 = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
1052 }
1053 return CGM.BlockObjectAssign;
1054}
1055
Mike Stump1851b682009-03-06 04:53:30 +00001056void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Mike Stump797b6322009-03-05 01:23:13 +00001057 llvm::Value *F = getBlockObjectDispose();
Mike Stump1851b682009-03-06 04:53:30 +00001058 llvm::Value *N;
Mike Stump797b6322009-03-05 01:23:13 +00001059 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001060 N = llvm::ConstantInt::get(llvm::Type::getInt32Ty(V->getContext()), flag);
Mike Stump797b6322009-03-05 01:23:13 +00001061 Builder.CreateCall2(F, V, N);
1062}
Mike Stump00470a12009-03-05 08:32:30 +00001063
1064ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stump08920992009-03-07 02:35:30 +00001065
1066BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1067 CGBuilderTy &B)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001068 : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) {
Owen Anderson0032b272009-08-13 21:57:51 +00001069 PtrToInt8Ty = llvm::PointerType::getUnqual(
1070 llvm::Type::getInt8Ty(VMContext));
Mike Stump08920992009-03-07 02:35:30 +00001071
1072 BlockHasCopyDispose = false;
1073}