blob: 43b098e021254fd16f04a80af444fe2dd67d3c83 [file] [log] [blame]
Anders Carlssonacfde802009-02-12 00:39:25 +00001//===--- CGBlocks.cpp - Emit LLVM Code for declarations -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit blocks.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
Mike Stump6cc88f72009-03-20 21:53:12 +000016#include "clang/AST/DeclObjC.h"
Anders Carlssonacfde802009-02-12 00:39:25 +000017#include "llvm/Module.h"
Anders Carlssond5cab542009-02-12 17:55:02 +000018#include "llvm/Target/TargetData.h"
Anders Carlssonacfde802009-02-12 00:39:25 +000019#include <algorithm>
Torok Edwinf42e4a62009-08-24 13:25:12 +000020#include <cstdio>
21
Anders Carlssonacfde802009-02-12 00:39:25 +000022using namespace clang;
23using namespace CodeGen;
24
Mike Stumpcf62d392009-03-06 18:42:23 +000025llvm::Constant *CodeGenFunction::
Mike Stumpa803b0e2009-03-25 17:58:24 +000026BuildDescriptorBlockDecl(bool BlockHasCopyDispose, uint64_t Size,
27 const llvm::StructType* Ty,
Mike Stump08920992009-03-07 02:35:30 +000028 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump56129b12009-02-13 16:55:51 +000029 const llvm::Type *UnsignedLongTy
30 = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpe5fee252009-02-13 16:19:19 +000031 llvm::Constant *C;
32 std::vector<llvm::Constant*> Elts;
33
34 // reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +000035 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpe5fee252009-02-13 16:19:19 +000036 Elts.push_back(C);
37
38 // Size
Mike Stumpd6840002009-02-21 20:07:44 +000039 // FIXME: What is the right way to say this doesn't fit? We should give
40 // a user diagnostic in that case. Better fix would be to change the
41 // API to size_t.
Owen Anderson4a28d5d2009-07-24 23:12:58 +000042 C = llvm::ConstantInt::get(UnsignedLongTy, Size);
Mike Stumpe5fee252009-02-13 16:19:19 +000043 Elts.push_back(C);
44
45 if (BlockHasCopyDispose) {
46 // copy_func_helper_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000047 Elts.push_back(BuildCopyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000048
49 // destroy_func_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000050 Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000051 }
52
Owen Anderson47a434f2009-08-05 23:18:46 +000053 C = llvm::ConstantStruct::get(VMContext, Elts);
Mike Stumpe5fee252009-02-13 16:19:19 +000054
Owen Anderson1c431b32009-07-08 19:05:04 +000055 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Mike Stumpe5fee252009-02-13 16:19:19 +000056 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +000057 C, "__block_descriptor_tmp");
Mike Stumpe5fee252009-02-13 16:19:19 +000058 return C;
59}
60
Mike Stump2a998142009-03-04 18:17:45 +000061llvm::Constant *BlockModule::getNSConcreteGlobalBlock() {
Chris Lattnere2f79b62009-05-13 02:50:56 +000062 if (NSConcreteGlobalBlock == 0)
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
Owen Anderson47a434f2009-08-05 23:18:46 +0000166 C = llvm::ConstantStruct::get(VMContext, Elts);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000167
168 char Name[32];
169 sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
Owen Anderson1c431b32009-07-08 19:05:04 +0000170 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Mike Stump8a2b4b12009-02-25 23:33:13 +0000171 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000172 C, Name);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000173 QualType BPT = BE->getType();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000174 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000175 return C;
176 }
Mike Stump00470a12009-03-05 08:32:30 +0000177
Mike Stump8a2b4b12009-02-25 23:33:13 +0000178 std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size());
Mike Stump08920992009-03-07 02:35:30 +0000179 for (int i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000180 Types[i] = Elts[i]->getType();
Mike Stump08920992009-03-07 02:35:30 +0000181 Types[4] = PtrToInt8Ty;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000182
Mike Stumpa99038c2009-02-28 09:07:16 +0000183 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
184 const Expr *E = subBlockDeclRefDecls[i];
185 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
186 QualType Ty = E->getType();
Mike Stumpdab514f2009-03-04 03:23:46 +0000187 if (BDRE && BDRE->isByRef()) {
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 const llvm::Type *Ty = Types[i+5];
231 llvm::Value *Loc = LocalDeclMap[VD];
232 Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
233 Loc = Builder.CreateLoad(Loc, false);
234 Loc = Builder.CreateBitCast(Loc, Ty);
235 Builder.CreateStore(Loc, Addr);
Mike Stump08920992009-03-07 02:35:30 +0000236 ++helpersize;
Mike Stumpdab514f2009-03-04 03:23:46 +0000237 continue;
238 } else
239 E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD),
240 VD->getType(), SourceLocation(),
241 false, false);
Mike Stumpa99038c2009-02-28 09:07:16 +0000242 }
Mike Stumpdab514f2009-03-04 03:23:46 +0000243 if (BDRE->isByRef()) {
Mike Stumpa8b60c92009-03-21 21:00:35 +0000244 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
245 // FIXME: Someone double check this.
246 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpa99038c2009-02-28 09:07:16 +0000247 E = new (getContext())
248 UnaryOperator(E, UnaryOperator::AddrOf,
249 getContext().getPointerType(E->getType()),
250 SourceLocation());
Mike Stumpdab514f2009-03-04 03:23:46 +0000251 }
Mike Stump08920992009-03-07 02:35:30 +0000252 ++helpersize;
Mike Stumpa99038c2009-02-28 09:07:16 +0000253
Mike Stumpa99038c2009-02-28 09:07:16 +0000254 RValue r = EmitAnyExpr(E, Addr, false);
Mike Stumpdab514f2009-03-04 03:23:46 +0000255 if (r.isScalar()) {
256 llvm::Value *Loc = r.getScalarVal();
257 const llvm::Type *Ty = Types[i+5];
258 if (BDRE->isByRef()) {
Mike Stump58a85142009-03-04 22:48:06 +0000259 // E is now the address of the value field, instead, we want the
260 // address of the actual ByRef struct. We optimize this slightly
261 // compared to gcc by not grabbing the forwarding slot as this must
262 // be done during Block_copy for us, and we can postpone the work
263 // until then.
264 uint64_t offset = BlockDecls[BDRE->getDecl()];
Mike Stump00470a12009-03-05 08:32:30 +0000265
Mike Stump58a85142009-03-04 22:48:06 +0000266 llvm::Value *BlockLiteral = LoadBlockStruct();
Mike Stump00470a12009-03-05 08:32:30 +0000267
Mike Stump58a85142009-03-04 22:48:06 +0000268 Loc = Builder.CreateGEP(BlockLiteral,
Owen Anderson0032b272009-08-13 21:57:51 +0000269 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Mike Stump58a85142009-03-04 22:48:06 +0000270 offset),
271 "block.literal");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000272 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000273 Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stump58a85142009-03-04 22:48:06 +0000274 Loc = Builder.CreateLoad(Loc, false);
275 // Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000276 }
277 Builder.CreateStore(Loc, Addr);
278 } else if (r.isComplex())
Mike Stump8a2b4b12009-02-25 23:33:13 +0000279 // FIXME: implement
280 ErrorUnsupported(BE, "complex in block literal");
281 else if (r.isAggregate())
282 ; // Already created into the destination
283 else
284 assert (0 && "bad block variable");
285 // FIXME: Ensure that the offset created by the backend for
286 // the struct matches the previously computed offset in BlockDecls.
287 }
Mike Stump08920992009-03-07 02:35:30 +0000288 NoteForHelper.resize(helpersize);
Mike Stumpcf62d392009-03-06 18:42:23 +0000289
290 // __descriptor
Mike Stumpa803b0e2009-03-25 17:58:24 +0000291 llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockHasCopyDispose,
292 subBlockSize, Ty,
Mike Stump08920992009-03-07 02:35:30 +0000293 &NoteForHelper);
294 Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
295 Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
Mike Stumpe5fee252009-02-13 16:19:19 +0000296 }
Mike Stump00470a12009-03-05 08:32:30 +0000297
Mike Stumpbd65cac2009-02-19 01:01:04 +0000298 QualType BPT = BE->getType();
Mike Stump8a2b4b12009-02-25 23:33:13 +0000299 return Builder.CreateBitCast(V, ConvertType(BPT));
Mike Stumpe5fee252009-02-13 16:19:19 +0000300}
301
302
Mike Stump2a998142009-03-04 18:17:45 +0000303const llvm::Type *BlockModule::getBlockDescriptorType() {
Mike Stumpab695142009-02-13 15:16:56 +0000304 if (BlockDescriptorType)
305 return BlockDescriptorType;
306
Mike Stumpa5448542009-02-13 15:32:32 +0000307 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000308 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000309
Mike Stumpab695142009-02-13 15:16:56 +0000310 // struct __block_descriptor {
311 // unsigned long reserved;
312 // unsigned long block_size;
313 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000314 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(),
315 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 Anderson96e0fc72009-07-29 22:16:19 +0000330 llvm::PointerType::getUnqual(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 Anderson47a434f2009-08-05 23:18:46 +0000342 GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
343 PtrToInt8Ty,
Mike Stump7cbb3602009-02-13 16:01:35 +0000344 IntTy,
345 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000346 PtrToInt8Ty,
Mike Stump9b8a7972009-02-13 15:25:34 +0000347 BlockDescPtrTy,
348 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000349
Mike Stump9b8a7972009-02-13 15:25:34 +0000350 getModule().addTypeName("struct.__block_literal_generic",
351 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000352
Mike Stump9b8a7972009-02-13 15:25:34 +0000353 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000354}
355
Mike Stump2a998142009-03-04 18:17:45 +0000356const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000357 if (GenericExtendedBlockLiteralType)
358 return GenericExtendedBlockLiteralType;
359
Mike Stumpbd65cac2009-02-19 01:01:04 +0000360 const llvm::Type *BlockDescPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000361 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpbd65cac2009-02-19 01:01:04 +0000362
363 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
364 getTypes().ConvertType(getContext().IntTy));
365
366 // struct __block_literal_generic {
367 // void *__isa;
368 // int __flags;
369 // int __reserved;
370 // void (*__invoke)(void *);
371 // struct __block_descriptor *__descriptor;
372 // void *__copy_func_helper_decl;
373 // void *__destroy_func_decl;
374 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000375 GenericExtendedBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
376 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000377 IntTy,
378 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000379 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000380 BlockDescPtrTy,
Mike Stump797b6322009-03-05 01:23:13 +0000381 PtrToInt8Ty,
382 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000383 NULL);
384
385 getModule().addTypeName("struct.__block_literal_extended_generic",
386 GenericExtendedBlockLiteralType);
387
388 return GenericExtendedBlockLiteralType;
389}
390
Anders Carlssond5cab542009-02-12 17:55:02 +0000391RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpa5448542009-02-13 15:32:32 +0000392 const BlockPointerType *BPT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000393 E->getCallee()->getType()->getAs<BlockPointerType>();
Mike Stumpa5448542009-02-13 15:32:32 +0000394
Anders Carlssonacfde802009-02-12 00:39:25 +0000395 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
396
397 // Get a pointer to the generic block literal.
398 const llvm::Type *BlockLiteralTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000399 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000400
401 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000402 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000403 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
404
405 // Get the function pointer from the literal.
406 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Anders Carlssonacfde802009-02-12 00:39:25 +0000407
Mike Stumpa5448542009-02-13 15:32:32 +0000408 BlockLiteral =
409 Builder.CreateBitCast(BlockLiteral,
Owen Anderson0032b272009-08-13 21:57:51 +0000410 llvm::PointerType::getUnqual(
411 llvm::Type::getInt8Ty(VMContext)),
Anders Carlssonacfde802009-02-12 00:39:25 +0000412 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000413
Anders Carlssonacfde802009-02-12 00:39:25 +0000414 // Add the block literal.
415 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
416 CallArgList Args;
417 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000418
Anders Carlsson782f3972009-04-08 23:13:16 +0000419 QualType FnType = BPT->getPointeeType();
420
Anders Carlssonacfde802009-02-12 00:39:25 +0000421 // And the rest of the arguments.
Anders Carlsson782f3972009-04-08 23:13:16 +0000422 EmitCallArgs(Args, FnType->getAsFunctionProtoType(),
423 E->arg_begin(), E->arg_end());
Mike Stumpa5448542009-02-13 15:32:32 +0000424
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000425 // Load the function.
426 llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp");
427
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000428 QualType ResultType = FnType->getAsFunctionType()->getResultType();
429
Mike Stump1eb44332009-09-09 15:08:12 +0000430 const CGFunctionInfo &FnInfo =
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000431 CGM.getTypes().getFunctionInfo(ResultType, Args);
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000433 // Cast the function pointer to the right type.
Mike Stump1eb44332009-09-09 15:08:12 +0000434 const llvm::Type *BlockFTy =
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000435 CGM.getTypes().GetFunctionType(FnInfo, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Owen Anderson96e0fc72009-07-29 22:16:19 +0000437 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000438 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Anders Carlssonacfde802009-02-12 00:39:25 +0000440 // And call the block.
Anders Carlssonf8544a42009-04-07 00:20:24 +0000441 return EmitCall(FnInfo, Func, Args);
Anders Carlssonacfde802009-02-12 00:39:25 +0000442}
Anders Carlssond5cab542009-02-12 17:55:02 +0000443
Mike Stumpdab514f2009-03-04 03:23:46 +0000444llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
445 uint64_t &offset = BlockDecls[E->getDecl()];
446
447 const llvm::Type *Ty;
448 Ty = CGM.getTypes().ConvertType(E->getDecl()->getType());
449
Mike Stumpdab514f2009-03-04 03:23:46 +0000450 // See if we have already allocated an offset for this variable.
451 if (offset == 0) {
Mike Stump00470a12009-03-05 08:32:30 +0000452 // Don't run the expensive check, unless we have to.
453 if (!BlockHasCopyDispose && BlockRequiresCopying(E->getType()))
454 BlockHasCopyDispose = true;
Mike Stumpdab514f2009-03-04 03:23:46 +0000455 // if not, allocate one now.
456 offset = getBlockOffset(E);
457 }
458
459 llvm::Value *BlockLiteral = LoadBlockStruct();
460 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
Owen Anderson0032b272009-08-13 21:57:51 +0000461 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000462 offset),
Mike Stump58a85142009-03-04 22:48:06 +0000463 "block.literal");
Mike Stumpdab514f2009-03-04 03:23:46 +0000464 if (E->isByRef()) {
465 bool needsCopyDispose = BlockRequiresCopying(E->getType());
Mike Stumpdab514f2009-03-04 03:23:46 +0000466 const llvm::Type *PtrStructTy
Anders Carlsson9ad55132009-09-09 02:51:03 +0000467 = llvm::PointerType::get(BuildByRefType(E->getDecl()), 0);
Mike Stumpa8b60c92009-03-21 21:00:35 +0000468 // The block literal will need a copy/destroy helper.
469 BlockHasCopyDispose = true;
Mike Stumpdab514f2009-03-04 03:23:46 +0000470 Ty = PtrStructTy;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000471 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000472 V = Builder.CreateBitCast(V, Ty);
473 V = Builder.CreateLoad(V, false);
474 V = Builder.CreateStructGEP(V, 1, "forwarding");
475 V = Builder.CreateLoad(V, false);
476 V = Builder.CreateBitCast(V, PtrStructTy);
477 V = Builder.CreateStructGEP(V, needsCopyDispose*2 + 4, "x");
478 } else {
Owen Anderson96e0fc72009-07-29 22:16:19 +0000479 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000480 V = Builder.CreateBitCast(V, Ty);
481 }
482 return V;
483}
484
Mike Stump6cc88f72009-03-20 21:53:12 +0000485void CodeGenFunction::BlockForwardSelf() {
486 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
487 ImplicitParamDecl *SelfDecl = OMD->getSelfDecl();
488 llvm::Value *&DMEntry = LocalDeclMap[SelfDecl];
489 if (DMEntry)
490 return;
491 // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care
492 BlockDeclRefExpr *BDRE = new (getContext())
493 BlockDeclRefExpr(SelfDecl,
494 SelfDecl->getType(), SourceLocation(), false);
495 DMEntry = GetAddrOfBlockDecl(BDRE);
496}
497
Mike Stump67a64482009-02-14 22:16:35 +0000498llvm::Constant *
Mike Stump90a90432009-03-04 18:47:42 +0000499BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000500 // Generate the block descriptor.
501 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000502 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
503 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000504
Anders Carlssond5cab542009-02-12 17:55:02 +0000505 llvm::Constant *DescriptorFields[2];
Mike Stumpa5448542009-02-13 15:32:32 +0000506
Anders Carlssond5cab542009-02-12 17:55:02 +0000507 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000508 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000509
Anders Carlssond5cab542009-02-12 17:55:02 +0000510 // Block literal size. For global blocks we just use the size of the generic
511 // block literal struct.
Mike Stumpa5448542009-02-13 15:32:32 +0000512 uint64_t BlockLiteralSize =
Mike Stump9b8a7972009-02-13 15:25:34 +0000513 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Owen Andersona1cf15f2009-07-14 23:10:40 +0000514 DescriptorFields[1] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000515 llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpa5448542009-02-13 15:32:32 +0000516
517 llvm::Constant *DescriptorStruct =
Owen Anderson47a434f2009-08-05 23:18:46 +0000518 llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 2);
Mike Stumpa5448542009-02-13 15:32:32 +0000519
Anders Carlssond5cab542009-02-12 17:55:02 +0000520 llvm::GlobalVariable *Descriptor =
Owen Anderson1c431b32009-07-08 19:05:04 +0000521 new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000522 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000523 DescriptorStruct, "__block_descriptor_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000524
Anders Carlssond5cab542009-02-12 17:55:02 +0000525 // Generate the constants for the block literal.
526 llvm::Constant *LiteralFields[5];
Mike Stumpa5448542009-02-13 15:32:32 +0000527
Mike Stump67a64482009-02-14 22:16:35 +0000528 CodeGenFunction::BlockInfo Info(0, n);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000529 uint64_t subBlockSize, subBlockAlign;
Mike Stumpa99038c2009-02-28 09:07:16 +0000530 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Daniel Dunbard67b09a2009-03-12 03:07:24 +0000531 bool subBlockHasCopyDispose = false;
Mike Stump7f28a9c2009-03-13 23:34:28 +0000532 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000533 llvm::Function *Fn
Mike Stump6cc88f72009-03-20 21:53:12 +0000534 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000535 subBlockSize,
Mike Stump90a90432009-03-04 18:47:42 +0000536 subBlockAlign,
Mike Stump00470a12009-03-05 08:32:30 +0000537 subBlockDeclRefDecls,
538 subBlockHasCopyDispose);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000539 assert(subBlockSize == BlockLiteralSize
540 && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000541
Anders Carlssond5cab542009-02-12 17:55:02 +0000542 // isa
Mike Stumpf99f1d02009-02-13 17:23:42 +0000543 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000544
Anders Carlssond5cab542009-02-12 17:55:02 +0000545 // Flags
Mike Stump00470a12009-03-05 08:32:30 +0000546 LiteralFields[1] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000547 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR);
Mike Stumpa5448542009-02-13 15:32:32 +0000548
Anders Carlssond5cab542009-02-12 17:55:02 +0000549 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000550 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000551
Anders Carlssond5cab542009-02-12 17:55:02 +0000552 // Function
553 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000554
Anders Carlssond5cab542009-02-12 17:55:02 +0000555 // Descriptor
556 LiteralFields[4] = Descriptor;
Mike Stumpa5448542009-02-13 15:32:32 +0000557
558 llvm::Constant *BlockLiteralStruct =
Owen Anderson47a434f2009-08-05 23:18:46 +0000559 llvm::ConstantStruct::get(VMContext, &LiteralFields[0], 5);
Mike Stumpa5448542009-02-13 15:32:32 +0000560
561 llvm::GlobalVariable *BlockLiteral =
Owen Anderson1c431b32009-07-08 19:05:04 +0000562 new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000563 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000564 BlockLiteralStruct, "__block_literal_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000565
Anders Carlssond5cab542009-02-12 17:55:02 +0000566 return BlockLiteral;
567}
568
Mike Stump4e7a1f72009-02-21 20:00:35 +0000569llvm::Value *CodeGenFunction::LoadBlockStruct() {
570 return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self");
571}
572
Mike Stump00470a12009-03-05 08:32:30 +0000573llvm::Function *
574CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
575 const BlockInfo& Info,
Mike Stump6cc88f72009-03-20 21:53:12 +0000576 const Decl *OuterFuncDecl,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000577 llvm::DenseMap<const Decl*, llvm::Value*> ldm,
Mike Stump00470a12009-03-05 08:32:30 +0000578 uint64_t &Size,
579 uint64_t &Align,
580 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls,
581 bool &subBlockHasCopyDispose) {
Devang Patel963dfbd2009-04-15 21:51:44 +0000582
583 // Check if we should generate debug info for this block.
584 if (CGM.getDebugInfo())
585 DebugInfo = CGM.getDebugInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Mike Stump7f28a9c2009-03-13 23:34:28 +0000587 // Arrange for local static and local extern declarations to appear
588 // to be local to this function as well, as they are directly referenced
589 // in a block.
590 for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
591 i != ldm.end();
592 ++i) {
593 const VarDecl *VD = dyn_cast<VarDecl>(i->first);
Mike Stump1eb44332009-09-09 15:08:12 +0000594
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000595 if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage())
Mike Stump7f28a9c2009-03-13 23:34:28 +0000596 LocalDeclMap[VD] = i->second;
597 }
598
Eli Friedman48f91222009-03-28 03:24:54 +0000599 // FIXME: We need to rearrange the code for copy/dispose so we have this
600 // sooner, so we can calculate offsets correctly.
601 if (!BlockHasCopyDispose)
602 BlockOffset = CGM.getTargetData()
603 .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8;
604 else
605 BlockOffset = CGM.getTargetData()
606 .getTypeStoreSizeInBits(CGM.getGenericExtendedBlockLiteralType()) / 8;
607 BlockAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
608
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000609 const FunctionType *BlockFunctionType = BExpr->getFunctionType();
610 QualType ResultType;
611 bool IsVariadic;
Mike Stump1eb44332009-09-09 15:08:12 +0000612 if (const FunctionProtoType *FTy =
Fariborz Jahanianda0895d2009-04-11 18:54:57 +0000613 dyn_cast<FunctionProtoType>(BlockFunctionType)) {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000614 ResultType = FTy->getResultType();
615 IsVariadic = FTy->isVariadic();
Mike Stumpb3589f42009-07-30 22:28:39 +0000616 } else {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000617 // K&R style block.
618 ResultType = BlockFunctionType->getResultType();
619 IsVariadic = false;
620 }
Mike Stumpa5448542009-02-13 15:32:32 +0000621
Anders Carlssond5cab542009-02-12 17:55:02 +0000622 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000623
Chris Lattner161d36d2009-02-28 19:01:03 +0000624 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlssond5cab542009-02-12 17:55:02 +0000625
626 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000627 ImplicitParamDecl *SelfDecl =
Anders Carlssond5cab542009-02-12 17:55:02 +0000628 ImplicitParamDecl::Create(getContext(), 0,
629 SourceLocation(), 0,
630 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000631
Anders Carlssond5cab542009-02-12 17:55:02 +0000632 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump4e7a1f72009-02-21 20:00:35 +0000633 BlockStructDecl = SelfDecl;
Mike Stumpa5448542009-02-13 15:32:32 +0000634
Steve Naroffe78b8092009-03-13 16:56:44 +0000635 for (BlockDecl::param_const_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000636 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000637 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000638
639 const CGFunctionInfo &FI =
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000640 CGM.getTypes().getFunctionInfo(ResultType, Args);
Anders Carlssond5cab542009-02-12 17:55:02 +0000641
Mike Stump67a64482009-02-14 22:16:35 +0000642 std::string Name = std::string("__") + Info.Name + "_block_invoke_";
Anders Carlssond5cab542009-02-12 17:55:02 +0000643 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000644 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic);
Mike Stumpa5448542009-02-13 15:32:32 +0000645
646 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000647 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
648 Name,
649 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000650
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000651 CGM.SetInternalFunctionAttributes(BD, Fn, FI);
652
Chris Lattner4863db42009-04-23 07:18:56 +0000653 StartFunction(BD, ResultType, Fn, Args,
Chris Lattner161d36d2009-02-28 19:01:03 +0000654 BExpr->getBody()->getLocEnd());
Chris Lattner4863db42009-04-23 07:18:56 +0000655 CurFuncDecl = OuterFuncDecl;
Chris Lattnerb5437d22009-04-23 05:30:27 +0000656 CurCodeDecl = BD;
Chris Lattner161d36d2009-02-28 19:01:03 +0000657 EmitStmt(BExpr->getBody());
658 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlssond5cab542009-02-12 17:55:02 +0000659
Mike Stump8a2b4b12009-02-25 23:33:13 +0000660 // The runtime needs a minimum alignment of a void *.
661 uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
662 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign);
663
Mike Stump4e7a1f72009-02-21 20:00:35 +0000664 Size = BlockOffset;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000665 Align = BlockAlign;
666 subBlockDeclRefDecls = BlockDeclRefDecls;
Mike Stump00470a12009-03-05 08:32:30 +0000667 subBlockHasCopyDispose |= BlockHasCopyDispose;
Anders Carlssond5cab542009-02-12 17:55:02 +0000668 return Fn;
669}
Mike Stumpa99038c2009-02-28 09:07:16 +0000670
Mike Stump08920992009-03-07 02:35:30 +0000671uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
Mike Stumpa99038c2009-02-28 09:07:16 +0000672 const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
673
674 uint64_t Size = getContext().getTypeSize(D->getType()) / 8;
675 uint64_t Align = getContext().getDeclAlignInBytes(D);
676
677 if (BDRE->isByRef()) {
678 Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8;
679 Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
680 }
681
682 assert ((Align > 0) && "alignment must be 1 byte or more");
683
684 uint64_t OldOffset = BlockOffset;
685
686 // Ensure proper alignment, even if it means we have to have a gap
687 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align);
688 BlockAlign = std::max(Align, BlockAlign);
Mike Stump00470a12009-03-05 08:32:30 +0000689
Mike Stumpa99038c2009-02-28 09:07:16 +0000690 uint64_t Pad = BlockOffset - OldOffset;
691 if (Pad) {
Owen Anderson0032b272009-08-13 21:57:51 +0000692 llvm::ArrayType::get(llvm::Type::getInt8Ty(VMContext), Pad);
Mike Stumpa99038c2009-02-28 09:07:16 +0000693 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
694 llvm::APInt(32, Pad),
695 ArrayType::Normal, 0);
696 ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000697 0, QualType(PadTy), 0, VarDecl::None);
Mike Stumpa99038c2009-02-28 09:07:16 +0000698 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()));
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Mike Stumpa4f668f2009-03-06 01:33:24 +0000725 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(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000744 SourceLocation(), II, R, 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000745 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 Anderson96e0fc72009-07-29 22:16:19 +0000755 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(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 Anderson96e0fc72009-07-29 22:16:19 +0000761 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(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 Anderson96e0fc72009-07-29 22:16:19 +0000774 llvm::PointerType::get(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 Anderson0032b272009-08-13 21:57:51 +0000780 llvm::Value *N = llvm::ConstantInt::get(
781 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000782 llvm::Value *F = getBlockObjectAssign();
783 Builder.CreateCall3(F, Dstv, Srcv, N);
784 }
Mike Stump08920992009-03-07 02:35:30 +0000785 }
786 }
787
Mike Stumpa4f668f2009-03-06 01:33:24 +0000788 CGF.FinishFunction();
789
Owen Anderson3c4972d2009-07-29 18:54:39 +0000790 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000791}
792
Mike Stumpcf62d392009-03-06 18:42:23 +0000793llvm::Constant *BlockFunction::
Mike Stump08920992009-03-07 02:35:30 +0000794GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
795 const llvm::StructType* T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000796 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000797 QualType R = getContext().VoidTy;
798
799 FunctionArgList Args;
800 // FIXME: This leaks
801 ImplicitParamDecl *Src =
802 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
803 getContext().getPointerType(getContext().VoidTy));
804
805 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Mike Stumpa4f668f2009-03-06 01:33:24 +0000807 const CGFunctionInfo &FI =
808 CGM.getTypes().getFunctionInfo(R, Args);
809
Mike Stump3899a7f2009-06-05 23:26:36 +0000810 // FIXME: We'd like to put these into a mergable by content, with
811 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000812 std::string Name = std::string("__destroy_helper_block_");
813 CodeGenTypes &Types = CGM.getTypes();
814 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
815
816 llvm::Function *Fn =
817 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
818 Name,
819 &CGM.getModule());
820
821 IdentifierInfo *II
822 = &CGM.getContext().Idents.get("__destroy_helper_block_");
823
824 FunctionDecl *FD = FunctionDecl::Create(getContext(),
825 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000826 SourceLocation(), II, R, 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000827 FunctionDecl::Static, false,
828 true);
829 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1edf6b62009-03-07 02:53:18 +0000830
Mike Stumpb7477cf2009-04-10 18:52:28 +0000831 if (NoteForHelperp) {
832 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump1edf6b62009-03-07 02:53:18 +0000833
Mike Stumpb7477cf2009-04-10 18:52:28 +0000834 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
835 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000836 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000837 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
838 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump1edf6b62009-03-07 02:53:18 +0000839
Mike Stumpb7477cf2009-04-10 18:52:28 +0000840 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
841 int flag = NoteForHelper[i].flag;
842 int index = NoteForHelper[i].index;
Mike Stump1edf6b62009-03-07 02:53:18 +0000843
Mike Stumpb7477cf2009-04-10 18:52:28 +0000844 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
845 || NoteForHelper[i].RequiresCopying) {
846 llvm::Value *Srcv = SrcObj;
847 Srcv = Builder.CreateStructGEP(Srcv, index);
848 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000849 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000850 Srcv = Builder.CreateLoad(Srcv);
851
852 BuildBlockRelease(Srcv, flag);
853 }
Mike Stump1edf6b62009-03-07 02:53:18 +0000854 }
855 }
856
Mike Stumpa4f668f2009-03-06 01:33:24 +0000857 CGF.FinishFunction();
858
Owen Anderson3c4972d2009-07-29 18:54:39 +0000859 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000860}
861
Mike Stump08920992009-03-07 02:35:30 +0000862llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000863 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump08920992009-03-07 02:35:30 +0000864 return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
865 T, NoteForHelper);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000866}
867
Mike Stump08920992009-03-07 02:35:30 +0000868llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000869 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stump08920992009-03-07 02:35:30 +0000870 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000871 T, NoteForHelperp);
Mike Stumpdab514f2009-03-04 03:23:46 +0000872}
Mike Stump797b6322009-03-05 01:23:13 +0000873
Mike Stumpee094222009-03-06 06:12:24 +0000874llvm::Constant *BlockFunction::
875GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
Mike Stump45031c02009-03-06 02:29:21 +0000876 QualType R = getContext().VoidTy;
877
878 FunctionArgList Args;
879 // FIXME: This leaks
Mike Stumpee094222009-03-06 06:12:24 +0000880 ImplicitParamDecl *Dst =
881 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
882 getContext().getPointerType(getContext().VoidTy));
883 Args.push_back(std::make_pair(Dst, Dst->getType()));
884
885 // FIXME: This leaks
Mike Stump45031c02009-03-06 02:29:21 +0000886 ImplicitParamDecl *Src =
887 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
888 getContext().getPointerType(getContext().VoidTy));
Mike Stump45031c02009-03-06 02:29:21 +0000889 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Mike Stump45031c02009-03-06 02:29:21 +0000891 const CGFunctionInfo &FI =
892 CGM.getTypes().getFunctionInfo(R, Args);
893
894 std::string Name = std::string("__Block_byref_id_object_copy_");
895 CodeGenTypes &Types = CGM.getTypes();
896 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
897
Mike Stump3899a7f2009-06-05 23:26:36 +0000898 // FIXME: We'd like to put these into a mergable by content, with
899 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +0000900 llvm::Function *Fn =
901 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
902 Name,
903 &CGM.getModule());
904
905 IdentifierInfo *II
906 = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
907
908 FunctionDecl *FD = FunctionDecl::Create(getContext(),
909 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000910 SourceLocation(), II, R, 0,
Mike Stump45031c02009-03-06 02:29:21 +0000911 FunctionDecl::Static, false,
912 true);
913 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpee094222009-03-06 06:12:24 +0000914
915 // dst->x
916 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000917 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +0000918 V = Builder.CreateLoad(V);
Mike Stumpee094222009-03-06 06:12:24 +0000919 V = Builder.CreateStructGEP(V, 6, "x");
920 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
921
922 // src->x
923 V = CGF.GetAddrOfLocalVar(Src);
924 V = Builder.CreateLoad(V);
925 V = Builder.CreateBitCast(V, T);
926 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000927 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpee094222009-03-06 06:12:24 +0000928 llvm::Value *SrcObj = Builder.CreateLoad(V);
Mike Stump1eb44332009-09-09 15:08:12 +0000929
Mike Stumpee094222009-03-06 06:12:24 +0000930 flag |= BLOCK_BYREF_CALLER;
931
Owen Anderson0032b272009-08-13 21:57:51 +0000932 llvm::Value *N = llvm::ConstantInt::get(
933 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpee094222009-03-06 06:12:24 +0000934 llvm::Value *F = getBlockObjectAssign();
935 Builder.CreateCall3(F, DstObj, SrcObj, N);
936
Mike Stump45031c02009-03-06 02:29:21 +0000937 CGF.FinishFunction();
938
Owen Anderson3c4972d2009-07-29 18:54:39 +0000939 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +0000940}
941
Mike Stump1851b682009-03-06 04:53:30 +0000942llvm::Constant *
943BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
944 int flag) {
Mike Stump45031c02009-03-06 02:29:21 +0000945 QualType R = getContext().VoidTy;
946
947 FunctionArgList Args;
948 // FIXME: This leaks
949 ImplicitParamDecl *Src =
950 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
951 getContext().getPointerType(getContext().VoidTy));
952
953 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000954
Mike Stump45031c02009-03-06 02:29:21 +0000955 const CGFunctionInfo &FI =
956 CGM.getTypes().getFunctionInfo(R, Args);
957
958 std::string Name = std::string("__Block_byref_id_object_dispose_");
959 CodeGenTypes &Types = CGM.getTypes();
960 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
961
Mike Stump3899a7f2009-06-05 23:26:36 +0000962 // FIXME: We'd like to put these into a mergable by content, with
963 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +0000964 llvm::Function *Fn =
965 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
966 Name,
967 &CGM.getModule());
968
969 IdentifierInfo *II
970 = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
971
972 FunctionDecl *FD = FunctionDecl::Create(getContext(),
973 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000974 SourceLocation(), II, R, 0,
Mike Stump45031c02009-03-06 02:29:21 +0000975 FunctionDecl::Static, false,
976 true);
977 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1851b682009-03-06 04:53:30 +0000978
979 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000980 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +0000981 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +0000982 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000983 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +0000984 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +0000985
Mike Stump1851b682009-03-06 04:53:30 +0000986 flag |= BLOCK_BYREF_CALLER;
987 BuildBlockRelease(V, flag);
Mike Stump45031c02009-03-06 02:29:21 +0000988 CGF.FinishFunction();
989
Owen Anderson3c4972d2009-07-29 18:54:39 +0000990 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +0000991}
992
Mike Stumpee094222009-03-06 06:12:24 +0000993llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
Mike Stump3899a7f2009-06-05 23:26:36 +0000994 int flag, unsigned Align) {
995 // All alignments below that of pointer alignment collpase down to just
996 // pointer alignment, as we always have at least that much alignment to begin
997 // with.
998 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
999 // As an optimization, we only generate a single function of each kind we
1000 // might need. We need a different one for each alignment and for each
1001 // setting of flags. We mix Align and flag to get the kind.
1002 uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag;
1003 llvm::Constant *& Entry = CGM.AssignCache[kind];
1004 if (Entry)
1005 return Entry;
1006 return Entry=CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001007}
1008
Mike Stump1851b682009-03-06 04:53:30 +00001009llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
Mike Stump3899a7f2009-06-05 23:26:36 +00001010 int flag,
1011 unsigned Align) {
1012 // All alignments below that of pointer alignment collpase down to just
1013 // pointer alignment, as we always have at least that much alignment to begin
1014 // with.
1015 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
1016 // As an optimization, we only generate a single function of each kind we
1017 // might need. We need a different one for each alignment and for each
1018 // setting of flags. We mix Align and flag to get the kind.
1019 uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag;
1020 llvm::Constant *& Entry = CGM.DestroyCache[kind];
1021 if (Entry)
1022 return Entry;
1023 return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001024}
1025
Mike Stump797b6322009-03-05 01:23:13 +00001026llvm::Value *BlockFunction::getBlockObjectDispose() {
1027 if (CGM.BlockObjectDispose == 0) {
1028 const llvm::FunctionType *FTy;
1029 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001030 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stump797b6322009-03-05 01:23:13 +00001031 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001032 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson96e0fc72009-07-29 22:16:19 +00001033 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stump00470a12009-03-05 08:32:30 +00001034 CGM.BlockObjectDispose
Mike Stump797b6322009-03-05 01:23:13 +00001035 = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
1036 }
1037 return CGM.BlockObjectDispose;
1038}
1039
Mike Stumpee094222009-03-06 06:12:24 +00001040llvm::Value *BlockFunction::getBlockObjectAssign() {
1041 if (CGM.BlockObjectAssign == 0) {
1042 const llvm::FunctionType *FTy;
1043 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001044 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stumpee094222009-03-06 06:12:24 +00001045 ArgTys.push_back(PtrToInt8Ty);
1046 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001047 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson96e0fc72009-07-29 22:16:19 +00001048 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stumpee094222009-03-06 06:12:24 +00001049 CGM.BlockObjectAssign
1050 = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
1051 }
1052 return CGM.BlockObjectAssign;
1053}
1054
Mike Stump1851b682009-03-06 04:53:30 +00001055void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Mike Stump797b6322009-03-05 01:23:13 +00001056 llvm::Value *F = getBlockObjectDispose();
Mike Stump1851b682009-03-06 04:53:30 +00001057 llvm::Value *N;
Mike Stump797b6322009-03-05 01:23:13 +00001058 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001059 N = llvm::ConstantInt::get(llvm::Type::getInt32Ty(V->getContext()), flag);
Mike Stump797b6322009-03-05 01:23:13 +00001060 Builder.CreateCall2(F, V, N);
1061}
Mike Stump00470a12009-03-05 08:32:30 +00001062
1063ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stump08920992009-03-07 02:35:30 +00001064
1065BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1066 CGBuilderTy &B)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001067 : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) {
Owen Anderson0032b272009-08-13 21:57:51 +00001068 PtrToInt8Ty = llvm::PointerType::getUnqual(
1069 llvm::Type::getInt8Ty(VMContext));
Mike Stump08920992009-03-07 02:35:30 +00001070
1071 BlockHasCopyDispose = false;
1072}