blob: 5998493986177f6cf59cd274bdaa846f7151ae19 [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
Mike Stumpb1a6e682009-09-30 02:43:10 +000014#include "CGDebugInfo.h"
Anders Carlssonacfde802009-02-12 00:39:25 +000015#include "CodeGenFunction.h"
16#include "CodeGenModule.h"
Mike Stump6cc88f72009-03-20 21:53:12 +000017#include "clang/AST/DeclObjC.h"
Anders Carlssonacfde802009-02-12 00:39:25 +000018#include "llvm/Module.h"
Anders Carlssond5cab542009-02-12 17:55:02 +000019#include "llvm/Target/TargetData.h"
Anders Carlssonacfde802009-02-12 00:39:25 +000020#include <algorithm>
Torok Edwinf42e4a62009-08-24 13:25:12 +000021#include <cstdio>
22
Anders Carlssonacfde802009-02-12 00:39:25 +000023using namespace clang;
24using namespace CodeGen;
25
Mike Stumpcf62d392009-03-06 18:42:23 +000026llvm::Constant *CodeGenFunction::
Mike Stumpa803b0e2009-03-25 17:58:24 +000027BuildDescriptorBlockDecl(bool BlockHasCopyDispose, uint64_t Size,
28 const llvm::StructType* Ty,
Mike Stump08920992009-03-07 02:35:30 +000029 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump56129b12009-02-13 16:55:51 +000030 const llvm::Type *UnsignedLongTy
31 = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpe5fee252009-02-13 16:19:19 +000032 llvm::Constant *C;
33 std::vector<llvm::Constant*> Elts;
34
35 // reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +000036 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpe5fee252009-02-13 16:19:19 +000037 Elts.push_back(C);
38
39 // Size
Mike Stumpd6840002009-02-21 20:07:44 +000040 // FIXME: What is the right way to say this doesn't fit? We should give
41 // a user diagnostic in that case. Better fix would be to change the
42 // API to size_t.
Owen Anderson4a28d5d2009-07-24 23:12:58 +000043 C = llvm::ConstantInt::get(UnsignedLongTy, Size);
Mike Stumpe5fee252009-02-13 16:19:19 +000044 Elts.push_back(C);
45
46 if (BlockHasCopyDispose) {
47 // copy_func_helper_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000048 Elts.push_back(BuildCopyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000049
50 // destroy_func_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000051 Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000052 }
53
Nick Lewycky0d36dd22009-09-19 20:00:52 +000054 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stumpe5fee252009-02-13 16:19:19 +000055
Owen Anderson1c431b32009-07-08 19:05:04 +000056 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Mike Stumpe5fee252009-02-13 16:19:19 +000057 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +000058 C, "__block_descriptor_tmp");
Mike Stumpe5fee252009-02-13 16:19:19 +000059 return C;
60}
61
Mike Stump2a998142009-03-04 18:17:45 +000062llvm::Constant *BlockModule::getNSConcreteGlobalBlock() {
Chris Lattnere2f79b62009-05-13 02:50:56 +000063 if (NSConcreteGlobalBlock == 0)
Mike Stump1eb44332009-09-09 15:08:12 +000064 NSConcreteGlobalBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
Chris Lattnere2f79b62009-05-13 02:50:56 +000065 "_NSConcreteGlobalBlock");
Mike Stumpf99f1d02009-02-13 17:23:42 +000066 return NSConcreteGlobalBlock;
67}
68
Mike Stump2a998142009-03-04 18:17:45 +000069llvm::Constant *BlockModule::getNSConcreteStackBlock() {
Chris Lattnere2f79b62009-05-13 02:50:56 +000070 if (NSConcreteStackBlock == 0)
Mike Stump1eb44332009-09-09 15:08:12 +000071 NSConcreteStackBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
Chris Lattnere2f79b62009-05-13 02:50:56 +000072 "_NSConcreteStackBlock");
Mike Stump59c5b112009-02-13 19:29:27 +000073 return NSConcreteStackBlock;
74}
75
Mike Stump00470a12009-03-05 08:32:30 +000076static void CollectBlockDeclRefInfo(const Stmt *S,
Anders Carlsson4de9fce2009-03-01 01:09:12 +000077 CodeGenFunction::BlockInfo &Info) {
78 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
79 I != E; ++I)
Daniel Dunbar82573ee2009-03-02 07:00:57 +000080 if (*I)
81 CollectBlockDeclRefInfo(*I, Info);
Mike Stump00470a12009-03-05 08:32:30 +000082
Anders Carlsson4de9fce2009-03-01 01:09:12 +000083 if (const BlockDeclRefExpr *DE = dyn_cast<BlockDeclRefExpr>(S)) {
84 // FIXME: Handle enums.
85 if (isa<FunctionDecl>(DE->getDecl()))
86 return;
Mike Stump00470a12009-03-05 08:32:30 +000087
Anders Carlsson4de9fce2009-03-01 01:09:12 +000088 if (DE->isByRef())
89 Info.ByRefDeclRefs.push_back(DE);
90 else
91 Info.ByCopyDeclRefs.push_back(DE);
92 }
93}
94
Mike Stump58a85142009-03-04 22:48:06 +000095/// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be
96/// declared as a global variable instead of on the stack.
Chris Lattnere2f79b62009-05-13 02:50:56 +000097static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info) {
Anders Carlsson4de9fce2009-03-01 01:09:12 +000098 return Info.ByRefDeclRefs.empty() && Info.ByCopyDeclRefs.empty();
99}
100
Mike Stump58a85142009-03-04 22:48:06 +0000101// FIXME: Push most into CGM, passing down a few bits, like current function
102// name.
Mike Stump8a2b4b12009-02-25 23:33:13 +0000103llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Mike Stumpe5fee252009-02-13 16:19:19 +0000104
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000105 std::string Name = CurFn->getName();
106 CodeGenFunction::BlockInfo Info(0, Name.c_str());
107 CollectBlockDeclRefInfo(BE->getBody(), Info);
108
109 // Check if the block can be global.
Mike Stump58a85142009-03-04 22:48:06 +0000110 // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like
111 // to just have one code path. We should move this function into CGM and pass
112 // CGF, then we can just check to see if CGF is 0.
Mike Stumpdab514f2009-03-04 03:23:46 +0000113 if (0 && CanBlockBeGlobal(Info))
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000114 return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
Mike Stump00470a12009-03-05 08:32:30 +0000115
116 std::vector<llvm::Constant*> Elts(5);
Mike Stumpe5fee252009-02-13 16:19:19 +0000117 llvm::Constant *C;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000118 llvm::Value *V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000119
Mike Stumpe5fee252009-02-13 16:19:19 +0000120 {
121 // C = BuildBlockStructInitlist();
122 unsigned int flags = BLOCK_HAS_DESCRIPTOR;
123
Mike Stump00470a12009-03-05 08:32:30 +0000124 // We run this first so that we set BlockHasCopyDispose from the entire
125 // block literal.
126 // __invoke
127 uint64_t subBlockSize, subBlockAlign;
128 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Mike Stumpa803b0e2009-03-25 17:58:24 +0000129 bool subBlockHasCopyDispose = false;
Mike Stump00470a12009-03-05 08:32:30 +0000130 llvm::Function *Fn
Mike Stumpbb304192009-09-24 22:31:14 +0000131 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl,
132 LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000133 subBlockSize,
Mike Stump00470a12009-03-05 08:32:30 +0000134 subBlockAlign,
135 subBlockDeclRefDecls,
Mike Stumpa803b0e2009-03-25 17:58:24 +0000136 subBlockHasCopyDispose);
137 BlockHasCopyDispose |= subBlockHasCopyDispose;
Mike Stump00470a12009-03-05 08:32:30 +0000138 Elts[3] = Fn;
139
Mike Stumpf5408fe2009-05-16 07:57:57 +0000140 // FIXME: Don't use BlockHasCopyDispose, it is set more often then
141 // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); }
Mike Stumpa803b0e2009-03-25 17:58:24 +0000142 if (subBlockHasCopyDispose)
Mike Stumpe5fee252009-02-13 16:19:19 +0000143 flags |= BLOCK_HAS_COPY_DISPOSE;
144
Mike Stump7d6dc4f2009-02-13 20:17:16 +0000145 // __isa
Mike Stump59c5b112009-02-13 19:29:27 +0000146 C = CGM.getNSConcreteStackBlock();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000147 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
Mike Stump00470a12009-03-05 08:32:30 +0000148 Elts[0] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000149
150 // __flags
151 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
152 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000153 C = llvm::ConstantInt::get(IntTy, flags);
Mike Stump00470a12009-03-05 08:32:30 +0000154 Elts[1] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000155
156 // __reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000157 C = llvm::ConstantInt::get(IntTy, 0);
Mike Stump00470a12009-03-05 08:32:30 +0000158 Elts[2] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000159
Mike Stump8a2b4b12009-02-25 23:33:13 +0000160 if (subBlockDeclRefDecls.size() == 0) {
Mike Stumpcf62d392009-03-06 18:42:23 +0000161 // __descriptor
Mike Stumpa803b0e2009-03-25 17:58:24 +0000162 Elts[4] = BuildDescriptorBlockDecl(subBlockHasCopyDispose, subBlockSize, 0, 0);
Mike Stumpcf62d392009-03-06 18:42:23 +0000163
Mike Stump5570cfe2009-03-01 20:07:53 +0000164 // Optimize to being a global block.
165 Elts[0] = CGM.getNSConcreteGlobalBlock();
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000166 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
Mike Stump5570cfe2009-03-01 20:07:53 +0000167
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000168 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000169
170 char Name[32];
171 sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
Owen Anderson1c431b32009-07-08 19:05:04 +0000172 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Mike Stump8a2b4b12009-02-25 23:33:13 +0000173 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000174 C, Name);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000175 QualType BPT = BE->getType();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000176 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000177 return C;
178 }
Mike Stump00470a12009-03-05 08:32:30 +0000179
Mike Stump8a2b4b12009-02-25 23:33:13 +0000180 std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size());
Mike Stump08920992009-03-07 02:35:30 +0000181 for (int i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000182 Types[i] = Elts[i]->getType();
Mike Stump08920992009-03-07 02:35:30 +0000183 Types[4] = PtrToInt8Ty;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000184
Mike Stumpa99038c2009-02-28 09:07:16 +0000185 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
186 const Expr *E = subBlockDeclRefDecls[i];
187 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
188 QualType Ty = E->getType();
Mike Stumpdab514f2009-03-04 03:23:46 +0000189 if (BDRE && BDRE->isByRef()) {
Anders Carlsson9ad55132009-09-09 02:51:03 +0000190 Types[i+5] = llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000191 } else
192 Types[i+5] = ConvertType(Ty);
Mike Stumpa99038c2009-02-28 09:07:16 +0000193 }
Mike Stump8a2b4b12009-02-25 23:33:13 +0000194
Owen Anderson47a434f2009-08-05 23:18:46 +0000195 llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000196
197 llvm::AllocaInst *A = CreateTempAlloca(Ty);
198 A->setAlignment(subBlockAlign);
199 V = A;
200
Mike Stump08920992009-03-07 02:35:30 +0000201 std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size());
202 int helpersize = 0;
203
204 for (unsigned i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000205 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
Mike Stump00470a12009-03-05 08:32:30 +0000206
Mike Stump8a2b4b12009-02-25 23:33:13 +0000207 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
208 {
Mike Stumpa99038c2009-02-28 09:07:16 +0000209 // FIXME: Push const down.
210 Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
211 DeclRefExpr *DR;
212 ValueDecl *VD;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000213
Mike Stumpa99038c2009-02-28 09:07:16 +0000214 DR = dyn_cast<DeclRefExpr>(E);
215 // Skip padding.
216 if (DR) continue;
217
218 BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
219 VD = BDRE->getDecl();
220
Mike Stumpdab514f2009-03-04 03:23:46 +0000221 llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp");
Mike Stump08920992009-03-07 02:35:30 +0000222 NoteForHelper[helpersize].index = i+5;
Mike Stump39605b42009-09-22 02:12:52 +0000223 NoteForHelper[helpersize].RequiresCopying
224 = BlockRequiresCopying(VD->getType());
Mike Stump08920992009-03-07 02:35:30 +0000225 NoteForHelper[helpersize].flag
Mike Stump39605b42009-09-22 02:12:52 +0000226 = (VD->getType()->isBlockPointerType()
227 ? BLOCK_FIELD_IS_BLOCK
228 : BLOCK_FIELD_IS_OBJECT);
Mike Stump08920992009-03-07 02:35:30 +0000229
Mike Stumpa99038c2009-02-28 09:07:16 +0000230 if (LocalDeclMap[VD]) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000231 if (BDRE->isByRef()) {
Mike Stump08920992009-03-07 02:35:30 +0000232 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
Mike Stumpf4bc3122009-03-07 06:04:31 +0000233 // FIXME: Someone double check this.
234 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000235 llvm::Value *Loc = LocalDeclMap[VD];
236 Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
237 Loc = Builder.CreateLoad(Loc, false);
Mike Stumpdab514f2009-03-04 03:23:46 +0000238 Builder.CreateStore(Loc, Addr);
Mike Stump08920992009-03-07 02:35:30 +0000239 ++helpersize;
Mike Stumpdab514f2009-03-04 03:23:46 +0000240 continue;
241 } else
242 E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD),
243 VD->getType(), SourceLocation(),
244 false, false);
Mike Stumpa99038c2009-02-28 09:07:16 +0000245 }
Mike Stumpdab514f2009-03-04 03:23:46 +0000246 if (BDRE->isByRef()) {
Mike Stumpa8b60c92009-03-21 21:00:35 +0000247 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
248 // FIXME: Someone double check this.
249 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpa99038c2009-02-28 09:07:16 +0000250 E = new (getContext())
251 UnaryOperator(E, UnaryOperator::AddrOf,
252 getContext().getPointerType(E->getType()),
253 SourceLocation());
Mike Stumpdab514f2009-03-04 03:23:46 +0000254 }
Mike Stump08920992009-03-07 02:35:30 +0000255 ++helpersize;
Mike Stumpa99038c2009-02-28 09:07:16 +0000256
Mike Stumpa99038c2009-02-28 09:07:16 +0000257 RValue r = EmitAnyExpr(E, Addr, false);
Mike Stumpdab514f2009-03-04 03:23:46 +0000258 if (r.isScalar()) {
259 llvm::Value *Loc = r.getScalarVal();
260 const llvm::Type *Ty = Types[i+5];
261 if (BDRE->isByRef()) {
Mike Stump58a85142009-03-04 22:48:06 +0000262 // E is now the address of the value field, instead, we want the
263 // address of the actual ByRef struct. We optimize this slightly
264 // compared to gcc by not grabbing the forwarding slot as this must
265 // be done during Block_copy for us, and we can postpone the work
266 // until then.
267 uint64_t offset = BlockDecls[BDRE->getDecl()];
Mike Stump00470a12009-03-05 08:32:30 +0000268
Mike Stump58a85142009-03-04 22:48:06 +0000269 llvm::Value *BlockLiteral = LoadBlockStruct();
Mike Stump00470a12009-03-05 08:32:30 +0000270
Mike Stump58a85142009-03-04 22:48:06 +0000271 Loc = Builder.CreateGEP(BlockLiteral,
Owen Anderson0032b272009-08-13 21:57:51 +0000272 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Mike Stump58a85142009-03-04 22:48:06 +0000273 offset),
274 "block.literal");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000275 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000276 Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stump58a85142009-03-04 22:48:06 +0000277 Loc = Builder.CreateLoad(Loc, false);
278 // Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000279 }
280 Builder.CreateStore(Loc, Addr);
281 } else if (r.isComplex())
Mike Stump8a2b4b12009-02-25 23:33:13 +0000282 // FIXME: implement
283 ErrorUnsupported(BE, "complex in block literal");
284 else if (r.isAggregate())
285 ; // Already created into the destination
286 else
287 assert (0 && "bad block variable");
288 // FIXME: Ensure that the offset created by the backend for
289 // the struct matches the previously computed offset in BlockDecls.
290 }
Mike Stump08920992009-03-07 02:35:30 +0000291 NoteForHelper.resize(helpersize);
Mike Stumpcf62d392009-03-06 18:42:23 +0000292
293 // __descriptor
Mike Stumpa803b0e2009-03-25 17:58:24 +0000294 llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockHasCopyDispose,
295 subBlockSize, Ty,
Mike Stump08920992009-03-07 02:35:30 +0000296 &NoteForHelper);
297 Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
298 Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
Mike Stumpe5fee252009-02-13 16:19:19 +0000299 }
Mike Stump00470a12009-03-05 08:32:30 +0000300
Mike Stumpbd65cac2009-02-19 01:01:04 +0000301 QualType BPT = BE->getType();
Mike Stump8a2b4b12009-02-25 23:33:13 +0000302 return Builder.CreateBitCast(V, ConvertType(BPT));
Mike Stumpe5fee252009-02-13 16:19:19 +0000303}
304
305
Mike Stump2a998142009-03-04 18:17:45 +0000306const llvm::Type *BlockModule::getBlockDescriptorType() {
Mike Stumpab695142009-02-13 15:16:56 +0000307 if (BlockDescriptorType)
308 return BlockDescriptorType;
309
Mike Stumpa5448542009-02-13 15:32:32 +0000310 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000311 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000312
Mike Stumpab695142009-02-13 15:16:56 +0000313 // struct __block_descriptor {
314 // unsigned long reserved;
315 // unsigned long block_size;
316 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000317 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(),
318 UnsignedLongTy,
Mike Stumpa5448542009-02-13 15:32:32 +0000319 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000320 NULL);
321
322 getModule().addTypeName("struct.__block_descriptor",
323 BlockDescriptorType);
324
325 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000326}
327
Mike Stump2a998142009-03-04 18:17:45 +0000328const llvm::Type *BlockModule::getGenericBlockLiteralType() {
Mike Stump9b8a7972009-02-13 15:25:34 +0000329 if (GenericBlockLiteralType)
330 return GenericBlockLiteralType;
331
Mike Stumpa5448542009-02-13 15:32:32 +0000332 const llvm::Type *BlockDescPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000333 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000334
Mike Stump7cbb3602009-02-13 16:01:35 +0000335 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
336 getTypes().ConvertType(getContext().IntTy));
337
Mike Stump9b8a7972009-02-13 15:25:34 +0000338 // struct __block_literal_generic {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000339 // void *__isa;
340 // int __flags;
341 // int __reserved;
342 // void (*__invoke)(void *);
343 // struct __block_descriptor *__descriptor;
Mike Stump9b8a7972009-02-13 15:25:34 +0000344 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000345 GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
346 PtrToInt8Ty,
Mike Stump7cbb3602009-02-13 16:01:35 +0000347 IntTy,
348 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000349 PtrToInt8Ty,
Mike Stump9b8a7972009-02-13 15:25:34 +0000350 BlockDescPtrTy,
351 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000352
Mike Stump9b8a7972009-02-13 15:25:34 +0000353 getModule().addTypeName("struct.__block_literal_generic",
354 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000355
Mike Stump9b8a7972009-02-13 15:25:34 +0000356 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000357}
358
Mike Stump2a998142009-03-04 18:17:45 +0000359const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000360 if (GenericExtendedBlockLiteralType)
361 return GenericExtendedBlockLiteralType;
362
Mike Stumpbd65cac2009-02-19 01:01:04 +0000363 const llvm::Type *BlockDescPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000364 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpbd65cac2009-02-19 01:01:04 +0000365
366 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
367 getTypes().ConvertType(getContext().IntTy));
368
369 // struct __block_literal_generic {
370 // void *__isa;
371 // int __flags;
372 // int __reserved;
373 // void (*__invoke)(void *);
374 // struct __block_descriptor *__descriptor;
375 // void *__copy_func_helper_decl;
376 // void *__destroy_func_decl;
377 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000378 GenericExtendedBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
379 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000380 IntTy,
381 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000382 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000383 BlockDescPtrTy,
Mike Stump797b6322009-03-05 01:23:13 +0000384 PtrToInt8Ty,
385 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000386 NULL);
387
388 getModule().addTypeName("struct.__block_literal_extended_generic",
389 GenericExtendedBlockLiteralType);
390
391 return GenericExtendedBlockLiteralType;
392}
393
Mike Stump39605b42009-09-22 02:12:52 +0000394bool BlockFunction::BlockRequiresCopying(QualType Ty) {
395 return CGM.BlockRequiresCopying(Ty);
396}
397
Anders Carlssond5cab542009-02-12 17:55:02 +0000398RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpa5448542009-02-13 15:32:32 +0000399 const BlockPointerType *BPT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000400 E->getCallee()->getType()->getAs<BlockPointerType>();
Mike Stumpa5448542009-02-13 15:32:32 +0000401
Anders Carlssonacfde802009-02-12 00:39:25 +0000402 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
403
404 // Get a pointer to the generic block literal.
405 const llvm::Type *BlockLiteralTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000406 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000407
408 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000409 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000410 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
411
412 // Get the function pointer from the literal.
413 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Anders Carlssonacfde802009-02-12 00:39:25 +0000414
Mike Stumpa5448542009-02-13 15:32:32 +0000415 BlockLiteral =
416 Builder.CreateBitCast(BlockLiteral,
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000417 llvm::Type::getInt8PtrTy(VMContext),
Anders Carlssonacfde802009-02-12 00:39:25 +0000418 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000419
Anders Carlssonacfde802009-02-12 00:39:25 +0000420 // Add the block literal.
421 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
422 CallArgList Args;
423 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000424
Anders Carlsson782f3972009-04-08 23:13:16 +0000425 QualType FnType = BPT->getPointeeType();
426
Anders Carlssonacfde802009-02-12 00:39:25 +0000427 // And the rest of the arguments.
John McCall183700f2009-09-21 23:43:11 +0000428 EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(),
Anders Carlsson782f3972009-04-08 23:13:16 +0000429 E->arg_begin(), E->arg_end());
Mike Stumpa5448542009-02-13 15:32:32 +0000430
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000431 // Load the function.
432 llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp");
433
John McCall183700f2009-09-21 23:43:11 +0000434 QualType ResultType = FnType->getAs<FunctionType>()->getResultType();
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000435
Mike Stump1eb44332009-09-09 15:08:12 +0000436 const CGFunctionInfo &FnInfo =
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000437 CGM.getTypes().getFunctionInfo(ResultType, Args);
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000439 // Cast the function pointer to the right type.
Mike Stump1eb44332009-09-09 15:08:12 +0000440 const llvm::Type *BlockFTy =
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000441 CGM.getTypes().GetFunctionType(FnInfo, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Owen Anderson96e0fc72009-07-29 22:16:19 +0000443 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000444 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Anders Carlssonacfde802009-02-12 00:39:25 +0000446 // And call the block.
Anders Carlssonf8544a42009-04-07 00:20:24 +0000447 return EmitCall(FnInfo, Func, Args);
Anders Carlssonacfde802009-02-12 00:39:25 +0000448}
Anders Carlssond5cab542009-02-12 17:55:02 +0000449
Mike Stumpdab514f2009-03-04 03:23:46 +0000450llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000451 const ValueDecl *VD = E->getDecl();
452
453 uint64_t &offset = BlockDecls[VD];
Mike Stumpdab514f2009-03-04 03:23:46 +0000454
Mike Stumpdab514f2009-03-04 03:23:46 +0000455
Mike Stumpdab514f2009-03-04 03:23:46 +0000456 // See if we have already allocated an offset for this variable.
457 if (offset == 0) {
Mike Stump00470a12009-03-05 08:32:30 +0000458 // Don't run the expensive check, unless we have to.
459 if (!BlockHasCopyDispose && BlockRequiresCopying(E->getType()))
460 BlockHasCopyDispose = true;
Mike Stumpdab514f2009-03-04 03:23:46 +0000461 // if not, allocate one now.
462 offset = getBlockOffset(E);
463 }
464
465 llvm::Value *BlockLiteral = LoadBlockStruct();
466 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
Owen Anderson0032b272009-08-13 21:57:51 +0000467 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000468 offset),
Mike Stump58a85142009-03-04 22:48:06 +0000469 "block.literal");
Mike Stumpdab514f2009-03-04 03:23:46 +0000470 if (E->isByRef()) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000471 const llvm::Type *PtrStructTy
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000472 = llvm::PointerType::get(BuildByRefType(VD), 0);
Mike Stumpa8b60c92009-03-21 21:00:35 +0000473 // The block literal will need a copy/destroy helper.
474 BlockHasCopyDispose = true;
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000475
476 const llvm::Type *Ty = PtrStructTy;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000477 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000478 V = Builder.CreateBitCast(V, Ty);
479 V = Builder.CreateLoad(V, false);
480 V = Builder.CreateStructGEP(V, 1, "forwarding");
481 V = Builder.CreateLoad(V, false);
482 V = Builder.CreateBitCast(V, PtrStructTy);
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000483 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
484 VD->getNameAsString());
Mike Stumpdab514f2009-03-04 03:23:46 +0000485 } else {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000486 const llvm::Type *Ty = CGM.getTypes().ConvertType(VD->getType());
487
Owen Anderson96e0fc72009-07-29 22:16:19 +0000488 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000489 V = Builder.CreateBitCast(V, Ty);
490 }
491 return V;
492}
493
Mike Stump6cc88f72009-03-20 21:53:12 +0000494void CodeGenFunction::BlockForwardSelf() {
495 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
496 ImplicitParamDecl *SelfDecl = OMD->getSelfDecl();
497 llvm::Value *&DMEntry = LocalDeclMap[SelfDecl];
498 if (DMEntry)
499 return;
500 // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care
501 BlockDeclRefExpr *BDRE = new (getContext())
502 BlockDeclRefExpr(SelfDecl,
503 SelfDecl->getType(), SourceLocation(), false);
504 DMEntry = GetAddrOfBlockDecl(BDRE);
505}
506
Mike Stump67a64482009-02-14 22:16:35 +0000507llvm::Constant *
Mike Stump90a90432009-03-04 18:47:42 +0000508BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000509 // Generate the block descriptor.
510 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000511 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
512 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000513
Anders Carlssond5cab542009-02-12 17:55:02 +0000514 llvm::Constant *DescriptorFields[2];
Mike Stumpa5448542009-02-13 15:32:32 +0000515
Anders Carlssond5cab542009-02-12 17:55:02 +0000516 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000517 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000518
Anders Carlssond5cab542009-02-12 17:55:02 +0000519 // Block literal size. For global blocks we just use the size of the generic
520 // block literal struct.
Mike Stumpa5448542009-02-13 15:32:32 +0000521 uint64_t BlockLiteralSize =
Mike Stump9b8a7972009-02-13 15:25:34 +0000522 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Owen Andersona1cf15f2009-07-14 23:10:40 +0000523 DescriptorFields[1] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000524 llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpa5448542009-02-13 15:32:32 +0000525
526 llvm::Constant *DescriptorStruct =
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000527 llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 2, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000528
Anders Carlssond5cab542009-02-12 17:55:02 +0000529 llvm::GlobalVariable *Descriptor =
Owen Anderson1c431b32009-07-08 19:05:04 +0000530 new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000531 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000532 DescriptorStruct, "__block_descriptor_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000533
Anders Carlssond5cab542009-02-12 17:55:02 +0000534 // Generate the constants for the block literal.
535 llvm::Constant *LiteralFields[5];
Mike Stumpa5448542009-02-13 15:32:32 +0000536
Mike Stump67a64482009-02-14 22:16:35 +0000537 CodeGenFunction::BlockInfo Info(0, n);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000538 uint64_t subBlockSize, subBlockAlign;
Mike Stumpa99038c2009-02-28 09:07:16 +0000539 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Daniel Dunbard67b09a2009-03-12 03:07:24 +0000540 bool subBlockHasCopyDispose = false;
Mike Stump7f28a9c2009-03-13 23:34:28 +0000541 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000542 llvm::Function *Fn
Mike Stump6cc88f72009-03-20 21:53:12 +0000543 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000544 subBlockSize,
Mike Stump90a90432009-03-04 18:47:42 +0000545 subBlockAlign,
Mike Stump00470a12009-03-05 08:32:30 +0000546 subBlockDeclRefDecls,
547 subBlockHasCopyDispose);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000548 assert(subBlockSize == BlockLiteralSize
549 && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000550
Anders Carlssond5cab542009-02-12 17:55:02 +0000551 // isa
Mike Stumpf99f1d02009-02-13 17:23:42 +0000552 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000553
Anders Carlssond5cab542009-02-12 17:55:02 +0000554 // Flags
Mike Stump00470a12009-03-05 08:32:30 +0000555 LiteralFields[1] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000556 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR);
Mike Stumpa5448542009-02-13 15:32:32 +0000557
Anders Carlssond5cab542009-02-12 17:55:02 +0000558 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000559 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000560
Anders Carlssond5cab542009-02-12 17:55:02 +0000561 // Function
562 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000563
Anders Carlssond5cab542009-02-12 17:55:02 +0000564 // Descriptor
565 LiteralFields[4] = Descriptor;
Mike Stumpa5448542009-02-13 15:32:32 +0000566
567 llvm::Constant *BlockLiteralStruct =
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000568 llvm::ConstantStruct::get(VMContext, &LiteralFields[0], 5, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000569
570 llvm::GlobalVariable *BlockLiteral =
Owen Anderson1c431b32009-07-08 19:05:04 +0000571 new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000572 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000573 BlockLiteralStruct, "__block_literal_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000574
Anders Carlssond5cab542009-02-12 17:55:02 +0000575 return BlockLiteral;
576}
577
Mike Stump4e7a1f72009-02-21 20:00:35 +0000578llvm::Value *CodeGenFunction::LoadBlockStruct() {
579 return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self");
580}
581
Mike Stump00470a12009-03-05 08:32:30 +0000582llvm::Function *
583CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
584 const BlockInfo& Info,
Mike Stump6cc88f72009-03-20 21:53:12 +0000585 const Decl *OuterFuncDecl,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000586 llvm::DenseMap<const Decl*, llvm::Value*> ldm,
Mike Stump00470a12009-03-05 08:32:30 +0000587 uint64_t &Size,
588 uint64_t &Align,
589 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls,
590 bool &subBlockHasCopyDispose) {
Devang Patel963dfbd2009-04-15 21:51:44 +0000591
592 // Check if we should generate debug info for this block.
593 if (CGM.getDebugInfo())
594 DebugInfo = CGM.getDebugInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000595
Mike Stump7f28a9c2009-03-13 23:34:28 +0000596 // Arrange for local static and local extern declarations to appear
597 // to be local to this function as well, as they are directly referenced
598 // in a block.
599 for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
600 i != ldm.end();
601 ++i) {
602 const VarDecl *VD = dyn_cast<VarDecl>(i->first);
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000604 if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage())
Mike Stump7f28a9c2009-03-13 23:34:28 +0000605 LocalDeclMap[VD] = i->second;
606 }
607
Eli Friedman48f91222009-03-28 03:24:54 +0000608 // FIXME: We need to rearrange the code for copy/dispose so we have this
609 // sooner, so we can calculate offsets correctly.
610 if (!BlockHasCopyDispose)
611 BlockOffset = CGM.getTargetData()
612 .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8;
613 else
614 BlockOffset = CGM.getTargetData()
615 .getTypeStoreSizeInBits(CGM.getGenericExtendedBlockLiteralType()) / 8;
616 BlockAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
617
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000618 const FunctionType *BlockFunctionType = BExpr->getFunctionType();
619 QualType ResultType;
620 bool IsVariadic;
Mike Stump1eb44332009-09-09 15:08:12 +0000621 if (const FunctionProtoType *FTy =
Fariborz Jahanianda0895d2009-04-11 18:54:57 +0000622 dyn_cast<FunctionProtoType>(BlockFunctionType)) {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000623 ResultType = FTy->getResultType();
624 IsVariadic = FTy->isVariadic();
Mike Stumpb3589f42009-07-30 22:28:39 +0000625 } else {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000626 // K&R style block.
627 ResultType = BlockFunctionType->getResultType();
628 IsVariadic = false;
629 }
Mike Stumpa5448542009-02-13 15:32:32 +0000630
Anders Carlssond5cab542009-02-12 17:55:02 +0000631 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000632
Chris Lattner161d36d2009-02-28 19:01:03 +0000633 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlssond5cab542009-02-12 17:55:02 +0000634
Mike Stumpadaaad32009-10-20 02:12:22 +0000635 IdentifierInfo *II
636 = &CGM.getContext().Idents.get(".block_descriptor");
637
638 QualType ParmTy = getContext().getBlockParmType();
Anders Carlssond5cab542009-02-12 17:55:02 +0000639 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000640 ImplicitParamDecl *SelfDecl =
Anders Carlssond5cab542009-02-12 17:55:02 +0000641 ImplicitParamDecl::Create(getContext(), 0,
Mike Stumpadaaad32009-10-20 02:12:22 +0000642 SourceLocation(), II,
643 ParmTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000644
Anders Carlssond5cab542009-02-12 17:55:02 +0000645 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump4e7a1f72009-02-21 20:00:35 +0000646 BlockStructDecl = SelfDecl;
Mike Stumpa5448542009-02-13 15:32:32 +0000647
Steve Naroffe78b8092009-03-13 16:56:44 +0000648 for (BlockDecl::param_const_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000649 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000650 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000651
652 const CGFunctionInfo &FI =
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000653 CGM.getTypes().getFunctionInfo(ResultType, Args);
Anders Carlssond5cab542009-02-12 17:55:02 +0000654
Mike Stump67a64482009-02-14 22:16:35 +0000655 std::string Name = std::string("__") + Info.Name + "_block_invoke_";
Anders Carlssond5cab542009-02-12 17:55:02 +0000656 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000657 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic);
Mike Stumpa5448542009-02-13 15:32:32 +0000658
659 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000660 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
661 Name,
662 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000663
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000664 CGM.SetInternalFunctionAttributes(BD, Fn, FI);
665
Chris Lattner4863db42009-04-23 07:18:56 +0000666 StartFunction(BD, ResultType, Fn, Args,
Chris Lattner161d36d2009-02-28 19:01:03 +0000667 BExpr->getBody()->getLocEnd());
Mike Stumpb1a6e682009-09-30 02:43:10 +0000668
Chris Lattner4863db42009-04-23 07:18:56 +0000669 CurFuncDecl = OuterFuncDecl;
Chris Lattnerb5437d22009-04-23 05:30:27 +0000670 CurCodeDecl = BD;
Mike Stumpb289b3f2009-10-01 22:29:41 +0000671
672 // Save a spot to insert the debug information for all the BlockDeclRefDecls.
673 llvm::BasicBlock *entry = Builder.GetInsertBlock();
674 llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint();
675 --entry_ptr;
676
Chris Lattner161d36d2009-02-28 19:01:03 +0000677 EmitStmt(BExpr->getBody());
Mike Stumpb289b3f2009-10-01 22:29:41 +0000678
Mike Stumpde8c5c72009-10-01 00:27:30 +0000679 // Remember where we were...
680 llvm::BasicBlock *resume = Builder.GetInsertBlock();
Mike Stumpb289b3f2009-10-01 22:29:41 +0000681
Mike Stumpde8c5c72009-10-01 00:27:30 +0000682 // Go back to the entry.
Mike Stumpb289b3f2009-10-01 22:29:41 +0000683 ++entry_ptr;
684 Builder.SetInsertPoint(entry, entry_ptr);
685
Mike Stumpb1a6e682009-09-30 02:43:10 +0000686 if (CGDebugInfo *DI = getDebugInfo()) {
Mike Stumpb1a6e682009-09-30 02:43:10 +0000687 // Emit debug information for all the BlockDeclRefDecls.
Mike Stumpb1a6e682009-09-30 02:43:10 +0000688 for (unsigned i=0; i < BlockDeclRefDecls.size(); ++i) {
689 const Expr *E = BlockDeclRefDecls[i];
690 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
691 if (BDRE) {
692 const ValueDecl *D = BDRE->getDecl();
693 DI->setLocation(D->getLocation());
694 DI->EmitDeclareOfBlockDeclRefVariable(BDRE,
695 LocalDeclMap[getBlockStructDecl()],
696 Builder, this);
697 }
698 }
Mike Stumpb1a6e682009-09-30 02:43:10 +0000699 }
Mike Stumpde8c5c72009-10-01 00:27:30 +0000700 // And resume where we left off.
701 if (resume == 0)
702 Builder.ClearInsertionPoint();
703 else
704 Builder.SetInsertPoint(resume);
Mike Stumpb1a6e682009-09-30 02:43:10 +0000705
Chris Lattner161d36d2009-02-28 19:01:03 +0000706 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlssond5cab542009-02-12 17:55:02 +0000707
Mike Stumpadaaad32009-10-20 02:12:22 +0000708 // And now finish off the type for the parameter, since now we know
709 // BlockDeclRefDecls is complete.
710 getContext().completeBlockParmType(ParmTy, BlockDeclRefDecls);
711
712#define REV2
713#ifdef REV2
714 TagDecl *TD = ParmTy->getPointeeType()->getAs<RecordType>()->getDecl();
715 CGM.UpdateCompletedType(TD);
716#else
717 TagDecl *TD = ParmTy->getPointeeType()->getAs<RecordType>()->getDecl();
718 TagDecl::redecl_iterator rdi = TD->redecls_begin();
719 ++rdi;
720 TD = *rdi;
721 CGM.UpdateCompletedType(TD);
722#endif
723
Mike Stump8a2b4b12009-02-25 23:33:13 +0000724 // The runtime needs a minimum alignment of a void *.
725 uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
726 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign);
727
Mike Stump4e7a1f72009-02-21 20:00:35 +0000728 Size = BlockOffset;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000729 Align = BlockAlign;
730 subBlockDeclRefDecls = BlockDeclRefDecls;
Mike Stump00470a12009-03-05 08:32:30 +0000731 subBlockHasCopyDispose |= BlockHasCopyDispose;
Anders Carlssond5cab542009-02-12 17:55:02 +0000732 return Fn;
733}
Mike Stumpa99038c2009-02-28 09:07:16 +0000734
Mike Stump08920992009-03-07 02:35:30 +0000735uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
Mike Stumpa99038c2009-02-28 09:07:16 +0000736 const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
737
738 uint64_t Size = getContext().getTypeSize(D->getType()) / 8;
739 uint64_t Align = getContext().getDeclAlignInBytes(D);
740
741 if (BDRE->isByRef()) {
742 Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8;
743 Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
744 }
745
746 assert ((Align > 0) && "alignment must be 1 byte or more");
747
748 uint64_t OldOffset = BlockOffset;
749
750 // Ensure proper alignment, even if it means we have to have a gap
751 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align);
752 BlockAlign = std::max(Align, BlockAlign);
Mike Stump00470a12009-03-05 08:32:30 +0000753
Mike Stumpa99038c2009-02-28 09:07:16 +0000754 uint64_t Pad = BlockOffset - OldOffset;
755 if (Pad) {
Owen Anderson0032b272009-08-13 21:57:51 +0000756 llvm::ArrayType::get(llvm::Type::getInt8Ty(VMContext), Pad);
Mike Stumpa99038c2009-02-28 09:07:16 +0000757 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
758 llvm::APInt(32, Pad),
759 ArrayType::Normal, 0);
760 ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000761 0, QualType(PadTy), 0, VarDecl::None);
Mike Stumpa99038c2009-02-28 09:07:16 +0000762 Expr *E;
763 E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
764 SourceLocation(), false, false);
765 BlockDeclRefDecls.push_back(E);
766 }
767 BlockDeclRefDecls.push_back(BDRE);
768
769 BlockOffset += Size;
770 return BlockOffset-Size;
771}
Mike Stumpdab514f2009-03-04 03:23:46 +0000772
Mike Stump08920992009-03-07 02:35:30 +0000773llvm::Constant *BlockFunction::
774GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000775 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000776 QualType R = getContext().VoidTy;
777
778 FunctionArgList Args;
779 // FIXME: This leaks
Mike Stump08920992009-03-07 02:35:30 +0000780 ImplicitParamDecl *Dst =
781 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
782 getContext().getPointerType(getContext().VoidTy));
783 Args.push_back(std::make_pair(Dst, Dst->getType()));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000784 ImplicitParamDecl *Src =
785 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
786 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000787 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000788
Mike Stumpa4f668f2009-03-06 01:33:24 +0000789 const CGFunctionInfo &FI =
790 CGM.getTypes().getFunctionInfo(R, Args);
791
Mike Stump3899a7f2009-06-05 23:26:36 +0000792 // FIXME: We'd like to put these into a mergable by content, with
793 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000794 std::string Name = std::string("__copy_helper_block_");
795 CodeGenTypes &Types = CGM.getTypes();
796 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
797
798 llvm::Function *Fn =
799 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
800 Name,
801 &CGM.getModule());
802
803 IdentifierInfo *II
804 = &CGM.getContext().Idents.get("__copy_helper_block_");
805
806 FunctionDecl *FD = FunctionDecl::Create(getContext(),
807 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000808 SourceLocation(), II, R, 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000809 FunctionDecl::Static, false,
810 true);
811 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump08920992009-03-07 02:35:30 +0000812
813 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
814 llvm::Type *PtrPtrT;
Mike Stump08920992009-03-07 02:35:30 +0000815
Mike Stumpb7477cf2009-04-10 18:52:28 +0000816 if (NoteForHelperp) {
817 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump08920992009-03-07 02:35:30 +0000818
Owen Anderson96e0fc72009-07-29 22:16:19 +0000819 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000820 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
821 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump08920992009-03-07 02:35:30 +0000822
Mike Stumpb7477cf2009-04-10 18:52:28 +0000823 llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000824 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000825 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000826 DstObj = Builder.CreateBitCast(DstObj, PtrPtrT);
827 DstObj = Builder.CreateLoad(DstObj);
Mike Stump08920992009-03-07 02:35:30 +0000828
Mike Stumpb7477cf2009-04-10 18:52:28 +0000829 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
830 int flag = NoteForHelper[i].flag;
831 int index = NoteForHelper[i].index;
Mike Stump08920992009-03-07 02:35:30 +0000832
Mike Stumpb7477cf2009-04-10 18:52:28 +0000833 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
834 || NoteForHelper[i].RequiresCopying) {
835 llvm::Value *Srcv = SrcObj;
836 Srcv = Builder.CreateStructGEP(Srcv, index);
837 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000838 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000839 Srcv = Builder.CreateLoad(Srcv);
840
841 llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
842 Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
843
Owen Anderson0032b272009-08-13 21:57:51 +0000844 llvm::Value *N = llvm::ConstantInt::get(
845 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000846 llvm::Value *F = getBlockObjectAssign();
847 Builder.CreateCall3(F, Dstv, Srcv, N);
848 }
Mike Stump08920992009-03-07 02:35:30 +0000849 }
850 }
851
Mike Stumpa4f668f2009-03-06 01:33:24 +0000852 CGF.FinishFunction();
853
Owen Anderson3c4972d2009-07-29 18:54:39 +0000854 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000855}
856
Mike Stumpcf62d392009-03-06 18:42:23 +0000857llvm::Constant *BlockFunction::
Mike Stump08920992009-03-07 02:35:30 +0000858GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
859 const llvm::StructType* T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000860 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000861 QualType R = getContext().VoidTy;
862
863 FunctionArgList Args;
864 // FIXME: This leaks
865 ImplicitParamDecl *Src =
866 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
867 getContext().getPointerType(getContext().VoidTy));
868
869 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Mike Stumpa4f668f2009-03-06 01:33:24 +0000871 const CGFunctionInfo &FI =
872 CGM.getTypes().getFunctionInfo(R, Args);
873
Mike Stump3899a7f2009-06-05 23:26:36 +0000874 // FIXME: We'd like to put these into a mergable by content, with
875 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000876 std::string Name = std::string("__destroy_helper_block_");
877 CodeGenTypes &Types = CGM.getTypes();
878 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
879
880 llvm::Function *Fn =
881 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
882 Name,
883 &CGM.getModule());
884
885 IdentifierInfo *II
886 = &CGM.getContext().Idents.get("__destroy_helper_block_");
887
888 FunctionDecl *FD = FunctionDecl::Create(getContext(),
889 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000890 SourceLocation(), II, R, 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000891 FunctionDecl::Static, false,
892 true);
893 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1edf6b62009-03-07 02:53:18 +0000894
Mike Stumpb7477cf2009-04-10 18:52:28 +0000895 if (NoteForHelperp) {
896 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump1edf6b62009-03-07 02:53:18 +0000897
Mike Stumpb7477cf2009-04-10 18:52:28 +0000898 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
899 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000900 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000901 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
902 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump1edf6b62009-03-07 02:53:18 +0000903
Mike Stumpb7477cf2009-04-10 18:52:28 +0000904 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
905 int flag = NoteForHelper[i].flag;
906 int index = NoteForHelper[i].index;
Mike Stump1edf6b62009-03-07 02:53:18 +0000907
Mike Stumpb7477cf2009-04-10 18:52:28 +0000908 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
909 || NoteForHelper[i].RequiresCopying) {
910 llvm::Value *Srcv = SrcObj;
911 Srcv = Builder.CreateStructGEP(Srcv, index);
912 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000913 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000914 Srcv = Builder.CreateLoad(Srcv);
915
916 BuildBlockRelease(Srcv, flag);
917 }
Mike Stump1edf6b62009-03-07 02:53:18 +0000918 }
919 }
920
Mike Stumpa4f668f2009-03-06 01:33:24 +0000921 CGF.FinishFunction();
922
Owen Anderson3c4972d2009-07-29 18:54:39 +0000923 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000924}
925
Mike Stump08920992009-03-07 02:35:30 +0000926llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000927 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump08920992009-03-07 02:35:30 +0000928 return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
929 T, NoteForHelper);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000930}
931
Mike Stump08920992009-03-07 02:35:30 +0000932llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000933 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stump08920992009-03-07 02:35:30 +0000934 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000935 T, NoteForHelperp);
Mike Stumpdab514f2009-03-04 03:23:46 +0000936}
Mike Stump797b6322009-03-05 01:23:13 +0000937
Mike Stumpee094222009-03-06 06:12:24 +0000938llvm::Constant *BlockFunction::
939GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
Mike Stump45031c02009-03-06 02:29:21 +0000940 QualType R = getContext().VoidTy;
941
942 FunctionArgList Args;
943 // FIXME: This leaks
Mike Stumpee094222009-03-06 06:12:24 +0000944 ImplicitParamDecl *Dst =
945 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
946 getContext().getPointerType(getContext().VoidTy));
947 Args.push_back(std::make_pair(Dst, Dst->getType()));
948
949 // FIXME: This leaks
Mike Stump45031c02009-03-06 02:29:21 +0000950 ImplicitParamDecl *Src =
951 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
952 getContext().getPointerType(getContext().VoidTy));
Mike Stump45031c02009-03-06 02:29:21 +0000953 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_copy_");
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_copy_");
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 Stumpee094222009-03-06 06:12:24 +0000978
979 // dst->x
980 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
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 Stumpee094222009-03-06 06:12:24 +0000983 V = Builder.CreateStructGEP(V, 6, "x");
984 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
985
986 // src->x
987 V = CGF.GetAddrOfLocalVar(Src);
988 V = Builder.CreateLoad(V);
989 V = Builder.CreateBitCast(V, T);
990 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000991 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpee094222009-03-06 06:12:24 +0000992 llvm::Value *SrcObj = Builder.CreateLoad(V);
Mike Stump1eb44332009-09-09 15:08:12 +0000993
Mike Stumpee094222009-03-06 06:12:24 +0000994 flag |= BLOCK_BYREF_CALLER;
995
Owen Anderson0032b272009-08-13 21:57:51 +0000996 llvm::Value *N = llvm::ConstantInt::get(
997 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpee094222009-03-06 06:12:24 +0000998 llvm::Value *F = getBlockObjectAssign();
999 Builder.CreateCall3(F, DstObj, SrcObj, N);
1000
Mike Stump45031c02009-03-06 02:29:21 +00001001 CGF.FinishFunction();
1002
Owen Anderson3c4972d2009-07-29 18:54:39 +00001003 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001004}
1005
Mike Stump1851b682009-03-06 04:53:30 +00001006llvm::Constant *
1007BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
1008 int flag) {
Mike Stump45031c02009-03-06 02:29:21 +00001009 QualType R = getContext().VoidTy;
1010
1011 FunctionArgList Args;
1012 // FIXME: This leaks
1013 ImplicitParamDecl *Src =
1014 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
1015 getContext().getPointerType(getContext().VoidTy));
1016
1017 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001018
Mike Stump45031c02009-03-06 02:29:21 +00001019 const CGFunctionInfo &FI =
1020 CGM.getTypes().getFunctionInfo(R, Args);
1021
1022 std::string Name = std::string("__Block_byref_id_object_dispose_");
1023 CodeGenTypes &Types = CGM.getTypes();
1024 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1025
Mike Stump3899a7f2009-06-05 23:26:36 +00001026 // FIXME: We'd like to put these into a mergable by content, with
1027 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001028 llvm::Function *Fn =
1029 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
1030 Name,
1031 &CGM.getModule());
1032
1033 IdentifierInfo *II
1034 = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
1035
1036 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1037 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001038 SourceLocation(), II, R, 0,
Mike Stump45031c02009-03-06 02:29:21 +00001039 FunctionDecl::Static, false,
1040 true);
1041 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1851b682009-03-06 04:53:30 +00001042
1043 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001044 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001045 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001046 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001047 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001048 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001049
Mike Stump1851b682009-03-06 04:53:30 +00001050 flag |= BLOCK_BYREF_CALLER;
1051 BuildBlockRelease(V, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001052 CGF.FinishFunction();
1053
Owen Anderson3c4972d2009-07-29 18:54:39 +00001054 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001055}
1056
Mike Stumpee094222009-03-06 06:12:24 +00001057llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
Mike Stump3899a7f2009-06-05 23:26:36 +00001058 int flag, unsigned Align) {
1059 // All alignments below that of pointer alignment collpase down to just
1060 // pointer alignment, as we always have at least that much alignment to begin
1061 // with.
1062 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
1063 // As an optimization, we only generate a single function of each kind we
1064 // might need. We need a different one for each alignment and for each
1065 // setting of flags. We mix Align and flag to get the kind.
1066 uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag;
1067 llvm::Constant *& Entry = CGM.AssignCache[kind];
1068 if (Entry)
1069 return Entry;
1070 return Entry=CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001071}
1072
Mike Stump1851b682009-03-06 04:53:30 +00001073llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
Mike Stump3899a7f2009-06-05 23:26:36 +00001074 int flag,
1075 unsigned Align) {
1076 // All alignments below that of pointer alignment collpase down to just
1077 // pointer alignment, as we always have at least that much alignment to begin
1078 // with.
1079 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
1080 // As an optimization, we only generate a single function of each kind we
1081 // might need. We need a different one for each alignment and for each
1082 // setting of flags. We mix Align and flag to get the kind.
1083 uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag;
1084 llvm::Constant *& Entry = CGM.DestroyCache[kind];
1085 if (Entry)
1086 return Entry;
1087 return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001088}
1089
Mike Stump797b6322009-03-05 01:23:13 +00001090llvm::Value *BlockFunction::getBlockObjectDispose() {
1091 if (CGM.BlockObjectDispose == 0) {
1092 const llvm::FunctionType *FTy;
1093 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001094 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stump797b6322009-03-05 01:23:13 +00001095 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001096 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson96e0fc72009-07-29 22:16:19 +00001097 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stump00470a12009-03-05 08:32:30 +00001098 CGM.BlockObjectDispose
Mike Stump797b6322009-03-05 01:23:13 +00001099 = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
1100 }
1101 return CGM.BlockObjectDispose;
1102}
1103
Mike Stumpee094222009-03-06 06:12:24 +00001104llvm::Value *BlockFunction::getBlockObjectAssign() {
1105 if (CGM.BlockObjectAssign == 0) {
1106 const llvm::FunctionType *FTy;
1107 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001108 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stumpee094222009-03-06 06:12:24 +00001109 ArgTys.push_back(PtrToInt8Ty);
1110 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001111 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson96e0fc72009-07-29 22:16:19 +00001112 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stumpee094222009-03-06 06:12:24 +00001113 CGM.BlockObjectAssign
1114 = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
1115 }
1116 return CGM.BlockObjectAssign;
1117}
1118
Mike Stump1851b682009-03-06 04:53:30 +00001119void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Mike Stump797b6322009-03-05 01:23:13 +00001120 llvm::Value *F = getBlockObjectDispose();
Mike Stump1851b682009-03-06 04:53:30 +00001121 llvm::Value *N;
Mike Stump797b6322009-03-05 01:23:13 +00001122 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001123 N = llvm::ConstantInt::get(llvm::Type::getInt32Ty(V->getContext()), flag);
Mike Stump797b6322009-03-05 01:23:13 +00001124 Builder.CreateCall2(F, V, N);
1125}
Mike Stump00470a12009-03-05 08:32:30 +00001126
1127ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stump08920992009-03-07 02:35:30 +00001128
1129BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1130 CGBuilderTy &B)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001131 : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) {
Owen Anderson0032b272009-08-13 21:57:51 +00001132 PtrToInt8Ty = llvm::PointerType::getUnqual(
1133 llvm::Type::getInt8Ty(VMContext));
Mike Stump08920992009-03-07 02:35:30 +00001134
1135 BlockHasCopyDispose = false;
1136}