blob: 05d138b2a2b24d2c09b5070c3613d68a60889dbd [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
Anders Carlssonacfde802009-02-12 00:39:25 +000022using namespace clang;
23using namespace CodeGen;
24
Mike Stumpcf62d392009-03-06 18:42:23 +000025llvm::Constant *CodeGenFunction::
Ken Dyck199c3d62010-01-11 17:06:35 +000026BuildDescriptorBlockDecl(bool BlockHasCopyDispose, CharUnits Size,
Mike Stumpa803b0e2009-03-25 17:58:24 +000027 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.
Ken Dyck199c3d62010-01-11 17:06:35 +000042 C = llvm::ConstantInt::get(UnsignedLongTy, Size.getQuantity());
Mike Stumpe5fee252009-02-13 16:19:19 +000043 Elts.push_back(C);
44
45 if (BlockHasCopyDispose) {
46 // copy_func_helper_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000047 Elts.push_back(BuildCopyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000048
49 // destroy_func_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000050 Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000051 }
52
Nick Lewycky0d36dd22009-09-19 20:00:52 +000053 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stumpe5fee252009-02-13 16:19:19 +000054
Owen Anderson1c431b32009-07-08 19:05:04 +000055 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Mike Stumpe5fee252009-02-13 16:19:19 +000056 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +000057 C, "__block_descriptor_tmp");
Mike Stumpe5fee252009-02-13 16:19:19 +000058 return C;
59}
60
Mike Stump2a998142009-03-04 18:17:45 +000061llvm::Constant *BlockModule::getNSConcreteGlobalBlock() {
Chris Lattnere2f79b62009-05-13 02:50:56 +000062 if (NSConcreteGlobalBlock == 0)
Mike Stump1eb44332009-09-09 15:08:12 +000063 NSConcreteGlobalBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
Chris Lattnere2f79b62009-05-13 02:50:56 +000064 "_NSConcreteGlobalBlock");
Mike Stumpf99f1d02009-02-13 17:23:42 +000065 return NSConcreteGlobalBlock;
66}
67
Mike Stump2a998142009-03-04 18:17:45 +000068llvm::Constant *BlockModule::getNSConcreteStackBlock() {
Chris Lattnere2f79b62009-05-13 02:50:56 +000069 if (NSConcreteStackBlock == 0)
Mike Stump1eb44332009-09-09 15:08:12 +000070 NSConcreteStackBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
Chris Lattnere2f79b62009-05-13 02:50:56 +000071 "_NSConcreteStackBlock");
Mike Stump59c5b112009-02-13 19:29:27 +000072 return NSConcreteStackBlock;
73}
74
Mike Stump38e16272009-10-21 22:01:24 +000075static void CollectBlockDeclRefInfo(
76 const Stmt *S, CodeGenFunction::BlockInfo &Info,
77 llvm::SmallSet<const DeclContext *, 16> &InnerContexts) {
Anders Carlsson4de9fce2009-03-01 01:09:12 +000078 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)
Mike Stump38e16272009-10-21 22:01:24 +000081 CollectBlockDeclRefInfo(*I, Info, InnerContexts);
Mike Stump00470a12009-03-05 08:32:30 +000082
Mike Stumpea26cb52009-10-21 03:49:08 +000083 // We want to ensure we walk down into block literals so we can find
84 // all nested BlockDeclRefExprs.
Mike Stump38e16272009-10-21 22:01:24 +000085 if (const BlockExpr *BE = dyn_cast<BlockExpr>(S)) {
86 InnerContexts.insert(cast<DeclContext>(BE->getBlockDecl()));
87 CollectBlockDeclRefInfo(BE->getBody(), Info, InnerContexts);
88 }
Mike Stumpea26cb52009-10-21 03:49:08 +000089
Mike Stump38e16272009-10-21 22:01:24 +000090 if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) {
Anders Carlsson4de9fce2009-03-01 01:09:12 +000091 // FIXME: Handle enums.
Mike Stump38e16272009-10-21 22:01:24 +000092 if (isa<FunctionDecl>(BDRE->getDecl()))
Anders Carlsson4de9fce2009-03-01 01:09:12 +000093 return;
Mike Stump00470a12009-03-05 08:32:30 +000094
Mike Stump38e16272009-10-21 22:01:24 +000095 // Only Decls that escape are added.
96 if (!InnerContexts.count(BDRE->getDecl()->getDeclContext()))
97 Info.DeclRefs.push_back(BDRE);
Anders Carlsson4de9fce2009-03-01 01:09:12 +000098 }
99}
100
Mike Stump58a85142009-03-04 22:48:06 +0000101/// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be
102/// declared as a global variable instead of on the stack.
Chris Lattnere2f79b62009-05-13 02:50:56 +0000103static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info) {
Mike Stumpea26cb52009-10-21 03:49:08 +0000104 return Info.DeclRefs.empty();
105}
106
107/// AllocateAllBlockDeclRefs - Preallocate all nested BlockDeclRefExprs to
108/// ensure we can generate the debug information for the parameter for the block
109/// invoke function.
110static void AllocateAllBlockDeclRefs(const CodeGenFunction::BlockInfo &Info,
111 CodeGenFunction *CGF) {
112 // Always allocate self, as it is often handy in the debugger, even if there
113 // is no codegen in the block that uses it. This is also useful to always do
114 // this as if we didn't, we'd have to figure out all code that uses a self
115 // pointer, including implicit uses.
116 if (const ObjCMethodDecl *OMD
117 = dyn_cast_or_null<ObjCMethodDecl>(CGF->CurFuncDecl)) {
118 ImplicitParamDecl *SelfDecl = OMD->getSelfDecl();
119 BlockDeclRefExpr *BDRE = new (CGF->getContext())
120 BlockDeclRefExpr(SelfDecl,
121 SelfDecl->getType(), SourceLocation(), false);
122 CGF->AllocateBlockDecl(BDRE);
123 }
124
125 // FIXME: Also always forward the this pointer in C++ as well.
126
127 for (size_t i = 0; i < Info.DeclRefs.size(); ++i)
128 CGF->AllocateBlockDecl(Info.DeclRefs[i]);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000129}
130
Mike Stump58a85142009-03-04 22:48:06 +0000131// FIXME: Push most into CGM, passing down a few bits, like current function
132// name.
Mike Stump8a2b4b12009-02-25 23:33:13 +0000133llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Mike Stumpe5fee252009-02-13 16:19:19 +0000134
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000135 std::string Name = CurFn->getName();
136 CodeGenFunction::BlockInfo Info(0, Name.c_str());
Mike Stump38e16272009-10-21 22:01:24 +0000137 llvm::SmallSet<const DeclContext *, 16> InnerContexts;
138 InnerContexts.insert(BE->getBlockDecl());
139 CollectBlockDeclRefInfo(BE->getBody(), Info, InnerContexts);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000140
141 // Check if the block can be global.
Mike Stump58a85142009-03-04 22:48:06 +0000142 // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like
143 // to just have one code path. We should move this function into CGM and pass
144 // CGF, then we can just check to see if CGF is 0.
Mike Stumpdab514f2009-03-04 03:23:46 +0000145 if (0 && CanBlockBeGlobal(Info))
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000146 return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
Mike Stump00470a12009-03-05 08:32:30 +0000147
David Chisnall5e530af2009-11-17 19:33:30 +0000148 size_t BlockFields = 5;
149
150 bool hasIntrospection = CGM.getContext().getLangOptions().BlockIntrospection;
151
152 if (hasIntrospection) {
153 BlockFields++;
154 }
155 std::vector<llvm::Constant*> Elts(BlockFields);
156
157 if (hasIntrospection) {
158 std::string BlockTypeEncoding;
159 CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
160
161 Elts[5] = llvm::ConstantExpr::getBitCast(
162 CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty);
163 }
164
Mike Stumpe5fee252009-02-13 16:19:19 +0000165 llvm::Constant *C;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000166 llvm::Value *V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000167
Mike Stumpe5fee252009-02-13 16:19:19 +0000168 {
169 // C = BuildBlockStructInitlist();
170 unsigned int flags = BLOCK_HAS_DESCRIPTOR;
171
David Chisnall5e530af2009-11-17 19:33:30 +0000172 if (hasIntrospection)
173 flags |= BLOCK_HAS_OBJC_TYPE;
174
Mike Stump00470a12009-03-05 08:32:30 +0000175 // We run this first so that we set BlockHasCopyDispose from the entire
176 // block literal.
177 // __invoke
Ken Dyck199c3d62010-01-11 17:06:35 +0000178 CharUnits subBlockSize;
Ken Dyck30a8a272010-01-26 19:13:33 +0000179 CharUnits subBlockAlign;
Mike Stump00470a12009-03-05 08:32:30 +0000180 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Mike Stumpa803b0e2009-03-25 17:58:24 +0000181 bool subBlockHasCopyDispose = false;
Mike Stump00470a12009-03-05 08:32:30 +0000182 llvm::Function *Fn
Mike Stumpbb304192009-09-24 22:31:14 +0000183 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl,
184 LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000185 subBlockSize,
Mike Stump00470a12009-03-05 08:32:30 +0000186 subBlockAlign,
187 subBlockDeclRefDecls,
Mike Stumpa803b0e2009-03-25 17:58:24 +0000188 subBlockHasCopyDispose);
189 BlockHasCopyDispose |= subBlockHasCopyDispose;
Mike Stump00470a12009-03-05 08:32:30 +0000190 Elts[3] = Fn;
191
Mike Stumpf5408fe2009-05-16 07:57:57 +0000192 // FIXME: Don't use BlockHasCopyDispose, it is set more often then
193 // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); }
Mike Stumpa803b0e2009-03-25 17:58:24 +0000194 if (subBlockHasCopyDispose)
Mike Stumpe5fee252009-02-13 16:19:19 +0000195 flags |= BLOCK_HAS_COPY_DISPOSE;
196
Mike Stump7d6dc4f2009-02-13 20:17:16 +0000197 // __isa
Mike Stump59c5b112009-02-13 19:29:27 +0000198 C = CGM.getNSConcreteStackBlock();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000199 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
Mike Stump00470a12009-03-05 08:32:30 +0000200 Elts[0] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000201
202 // __flags
203 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
204 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000205 C = llvm::ConstantInt::get(IntTy, flags);
Mike Stump00470a12009-03-05 08:32:30 +0000206 Elts[1] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000207
208 // __reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000209 C = llvm::ConstantInt::get(IntTy, 0);
Mike Stump00470a12009-03-05 08:32:30 +0000210 Elts[2] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000211
Mike Stump8a2b4b12009-02-25 23:33:13 +0000212 if (subBlockDeclRefDecls.size() == 0) {
Mike Stumpcf62d392009-03-06 18:42:23 +0000213 // __descriptor
Mike Stumpea26cb52009-10-21 03:49:08 +0000214 Elts[4] = BuildDescriptorBlockDecl(subBlockHasCopyDispose, subBlockSize,
215 0, 0);
Mike Stumpcf62d392009-03-06 18:42:23 +0000216
Mike Stump5570cfe2009-03-01 20:07:53 +0000217 // Optimize to being a global block.
218 Elts[0] = CGM.getNSConcreteGlobalBlock();
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000219 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
Mike Stump5570cfe2009-03-01 20:07:53 +0000220
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000221 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000222
Owen Anderson1c431b32009-07-08 19:05:04 +0000223 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000224 llvm::GlobalValue::InternalLinkage, C,
225 "__block_holder_tmp_" +
226 llvm::Twine(CGM.getGlobalUniqueCount()));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000227 QualType BPT = BE->getType();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000228 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000229 return C;
230 }
Mike Stump00470a12009-03-05 08:32:30 +0000231
David Chisnall5e530af2009-11-17 19:33:30 +0000232 std::vector<const llvm::Type *> Types(BlockFields+subBlockDeclRefDecls.size());
Mike Stump08920992009-03-07 02:35:30 +0000233 for (int i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000234 Types[i] = Elts[i]->getType();
Mike Stump08920992009-03-07 02:35:30 +0000235 Types[4] = PtrToInt8Ty;
David Chisnall5e530af2009-11-17 19:33:30 +0000236 if (hasIntrospection)
237 Types[5] = PtrToInt8Ty;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000238
Mike Stumpa99038c2009-02-28 09:07:16 +0000239 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
240 const Expr *E = subBlockDeclRefDecls[i];
241 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
242 QualType Ty = E->getType();
Mike Stumpdab514f2009-03-04 03:23:46 +0000243 if (BDRE && BDRE->isByRef()) {
David Chisnall5e530af2009-11-17 19:33:30 +0000244 Types[i+BlockFields] = llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000245 } else
David Chisnall5e530af2009-11-17 19:33:30 +0000246 Types[i+BlockFields] = ConvertType(Ty);
Mike Stumpa99038c2009-02-28 09:07:16 +0000247 }
Mike Stump8a2b4b12009-02-25 23:33:13 +0000248
Owen Anderson47a434f2009-08-05 23:18:46 +0000249 llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000250
251 llvm::AllocaInst *A = CreateTempAlloca(Ty);
Ken Dyck30a8a272010-01-26 19:13:33 +0000252 A->setAlignment(subBlockAlign.getQuantity());
Mike Stump8a2b4b12009-02-25 23:33:13 +0000253 V = A;
254
Mike Stump08920992009-03-07 02:35:30 +0000255 std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size());
256 int helpersize = 0;
257
258 for (unsigned i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000259 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
David Chisnall5e530af2009-11-17 19:33:30 +0000260 if (hasIntrospection)
261 Builder.CreateStore(Elts[5], Builder.CreateStructGEP(V, 5, "block.tmp"));
Mike Stump00470a12009-03-05 08:32:30 +0000262
Mike Stump8a2b4b12009-02-25 23:33:13 +0000263 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
264 {
Mike Stumpa99038c2009-02-28 09:07:16 +0000265 // FIXME: Push const down.
266 Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
267 DeclRefExpr *DR;
268 ValueDecl *VD;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000269
Mike Stumpa99038c2009-02-28 09:07:16 +0000270 DR = dyn_cast<DeclRefExpr>(E);
271 // Skip padding.
272 if (DR) continue;
273
274 BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
275 VD = BDRE->getDecl();
276
David Chisnall5e530af2009-11-17 19:33:30 +0000277 llvm::Value* Addr = Builder.CreateStructGEP(V, i+BlockFields, "tmp");
Mike Stump08920992009-03-07 02:35:30 +0000278 NoteForHelper[helpersize].index = i+5;
Mike Stump39605b42009-09-22 02:12:52 +0000279 NoteForHelper[helpersize].RequiresCopying
280 = BlockRequiresCopying(VD->getType());
Mike Stump08920992009-03-07 02:35:30 +0000281 NoteForHelper[helpersize].flag
Mike Stump39605b42009-09-22 02:12:52 +0000282 = (VD->getType()->isBlockPointerType()
283 ? BLOCK_FIELD_IS_BLOCK
284 : BLOCK_FIELD_IS_OBJECT);
Mike Stump08920992009-03-07 02:35:30 +0000285
Mike Stumpa99038c2009-02-28 09:07:16 +0000286 if (LocalDeclMap[VD]) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000287 if (BDRE->isByRef()) {
Mike Stump08920992009-03-07 02:35:30 +0000288 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
Mike Stumpf4bc3122009-03-07 06:04:31 +0000289 // FIXME: Someone double check this.
290 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000291 llvm::Value *Loc = LocalDeclMap[VD];
292 Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000293 Loc = Builder.CreateLoad(Loc);
Mike Stumpdab514f2009-03-04 03:23:46 +0000294 Builder.CreateStore(Loc, Addr);
Mike Stump08920992009-03-07 02:35:30 +0000295 ++helpersize;
Mike Stumpdab514f2009-03-04 03:23:46 +0000296 continue;
297 } else
John McCalldbd872f2009-12-08 09:08:17 +0000298 E = new (getContext()) DeclRefExpr (VD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000299 VD->getType(),
300 SourceLocation());
Mike Stumpa99038c2009-02-28 09:07:16 +0000301 }
Mike Stumpdab514f2009-03-04 03:23:46 +0000302 if (BDRE->isByRef()) {
Mike Stumpa8b60c92009-03-21 21:00:35 +0000303 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
304 // FIXME: Someone double check this.
305 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpa99038c2009-02-28 09:07:16 +0000306 E = new (getContext())
307 UnaryOperator(E, UnaryOperator::AddrOf,
308 getContext().getPointerType(E->getType()),
309 SourceLocation());
Mike Stumpdab514f2009-03-04 03:23:46 +0000310 }
Mike Stump08920992009-03-07 02:35:30 +0000311 ++helpersize;
Mike Stumpa99038c2009-02-28 09:07:16 +0000312
Mike Stumpa99038c2009-02-28 09:07:16 +0000313 RValue r = EmitAnyExpr(E, Addr, false);
Mike Stumpdab514f2009-03-04 03:23:46 +0000314 if (r.isScalar()) {
315 llvm::Value *Loc = r.getScalarVal();
David Chisnall5e530af2009-11-17 19:33:30 +0000316 const llvm::Type *Ty = Types[i+BlockFields];
Mike Stumpdab514f2009-03-04 03:23:46 +0000317 if (BDRE->isByRef()) {
Mike Stump58a85142009-03-04 22:48:06 +0000318 // E is now the address of the value field, instead, we want the
319 // address of the actual ByRef struct. We optimize this slightly
320 // compared to gcc by not grabbing the forwarding slot as this must
321 // be done during Block_copy for us, and we can postpone the work
322 // until then.
Ken Dyck199c3d62010-01-11 17:06:35 +0000323 CharUnits offset = BlockDecls[BDRE->getDecl()];
Mike Stump00470a12009-03-05 08:32:30 +0000324
Mike Stump58a85142009-03-04 22:48:06 +0000325 llvm::Value *BlockLiteral = LoadBlockStruct();
Mike Stump00470a12009-03-05 08:32:30 +0000326
Mike Stump58a85142009-03-04 22:48:06 +0000327 Loc = Builder.CreateGEP(BlockLiteral,
Mike Stumpea26cb52009-10-21 03:49:08 +0000328 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Ken Dyck199c3d62010-01-11 17:06:35 +0000329 offset.getQuantity()),
Mike Stump58a85142009-03-04 22:48:06 +0000330 "block.literal");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000331 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000332 Loc = Builder.CreateBitCast(Loc, Ty);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000333 Loc = Builder.CreateLoad(Loc);
Mike Stump58a85142009-03-04 22:48:06 +0000334 // Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000335 }
336 Builder.CreateStore(Loc, Addr);
337 } else if (r.isComplex())
Mike Stump8a2b4b12009-02-25 23:33:13 +0000338 // FIXME: implement
339 ErrorUnsupported(BE, "complex in block literal");
340 else if (r.isAggregate())
341 ; // Already created into the destination
342 else
343 assert (0 && "bad block variable");
344 // FIXME: Ensure that the offset created by the backend for
345 // the struct matches the previously computed offset in BlockDecls.
346 }
Mike Stump08920992009-03-07 02:35:30 +0000347 NoteForHelper.resize(helpersize);
Mike Stumpcf62d392009-03-06 18:42:23 +0000348
349 // __descriptor
Mike Stumpa803b0e2009-03-25 17:58:24 +0000350 llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockHasCopyDispose,
351 subBlockSize, Ty,
Mike Stump08920992009-03-07 02:35:30 +0000352 &NoteForHelper);
353 Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
354 Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
Mike Stumpe5fee252009-02-13 16:19:19 +0000355 }
Mike Stump00470a12009-03-05 08:32:30 +0000356
Mike Stumpbd65cac2009-02-19 01:01:04 +0000357 QualType BPT = BE->getType();
Mike Stump8a2b4b12009-02-25 23:33:13 +0000358 return Builder.CreateBitCast(V, ConvertType(BPT));
Mike Stumpe5fee252009-02-13 16:19:19 +0000359}
360
361
Mike Stump2a998142009-03-04 18:17:45 +0000362const llvm::Type *BlockModule::getBlockDescriptorType() {
Mike Stumpab695142009-02-13 15:16:56 +0000363 if (BlockDescriptorType)
364 return BlockDescriptorType;
365
Mike Stumpa5448542009-02-13 15:32:32 +0000366 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000367 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000368
Mike Stumpab695142009-02-13 15:16:56 +0000369 // struct __block_descriptor {
370 // unsigned long reserved;
371 // unsigned long block_size;
372 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000373 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(),
374 UnsignedLongTy,
Mike Stumpa5448542009-02-13 15:32:32 +0000375 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000376 NULL);
377
378 getModule().addTypeName("struct.__block_descriptor",
379 BlockDescriptorType);
380
381 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000382}
383
Mike Stump2a998142009-03-04 18:17:45 +0000384const llvm::Type *BlockModule::getGenericBlockLiteralType() {
Mike Stump9b8a7972009-02-13 15:25:34 +0000385 if (GenericBlockLiteralType)
386 return GenericBlockLiteralType;
387
Mike Stumpa5448542009-02-13 15:32:32 +0000388 const llvm::Type *BlockDescPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000389 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000390
Mike Stump7cbb3602009-02-13 16:01:35 +0000391 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
392 getTypes().ConvertType(getContext().IntTy));
393
Mike Stump9b8a7972009-02-13 15:25:34 +0000394 // struct __block_literal_generic {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000395 // void *__isa;
396 // int __flags;
397 // int __reserved;
398 // void (*__invoke)(void *);
399 // struct __block_descriptor *__descriptor;
David Chisnall5e530af2009-11-17 19:33:30 +0000400 // // GNU runtime only:
401 // const char *types;
Mike Stump9b8a7972009-02-13 15:25:34 +0000402 // };
David Chisnall5e530af2009-11-17 19:33:30 +0000403 if (CGM.getContext().getLangOptions().BlockIntrospection)
404 GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
405 PtrToInt8Ty,
406 IntTy,
407 IntTy,
408 PtrToInt8Ty,
409 BlockDescPtrTy,
410 PtrToInt8Ty,
411 NULL);
412 else
413 GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
Owen Anderson47a434f2009-08-05 23:18:46 +0000414 PtrToInt8Ty,
Mike Stump7cbb3602009-02-13 16:01:35 +0000415 IntTy,
416 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000417 PtrToInt8Ty,
Mike Stump9b8a7972009-02-13 15:25:34 +0000418 BlockDescPtrTy,
419 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000420
Mike Stump9b8a7972009-02-13 15:25:34 +0000421 getModule().addTypeName("struct.__block_literal_generic",
422 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000423
Mike Stump9b8a7972009-02-13 15:25:34 +0000424 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000425}
426
Mike Stump2a998142009-03-04 18:17:45 +0000427const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000428 if (GenericExtendedBlockLiteralType)
429 return GenericExtendedBlockLiteralType;
430
Mike Stumpbd65cac2009-02-19 01:01:04 +0000431 const llvm::Type *BlockDescPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000432 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpbd65cac2009-02-19 01:01:04 +0000433
434 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
435 getTypes().ConvertType(getContext().IntTy));
436
437 // struct __block_literal_generic {
438 // void *__isa;
439 // int __flags;
440 // int __reserved;
441 // void (*__invoke)(void *);
442 // struct __block_descriptor *__descriptor;
443 // void *__copy_func_helper_decl;
444 // void *__destroy_func_decl;
445 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000446 GenericExtendedBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
447 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000448 IntTy,
449 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000450 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000451 BlockDescPtrTy,
Mike Stump797b6322009-03-05 01:23:13 +0000452 PtrToInt8Ty,
453 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000454 NULL);
455
456 getModule().addTypeName("struct.__block_literal_extended_generic",
457 GenericExtendedBlockLiteralType);
458
459 return GenericExtendedBlockLiteralType;
460}
461
Anders Carlssona1736c02009-12-24 21:13:40 +0000462RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E,
463 ReturnValueSlot ReturnValue) {
Mike Stumpa5448542009-02-13 15:32:32 +0000464 const BlockPointerType *BPT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000465 E->getCallee()->getType()->getAs<BlockPointerType>();
Mike Stumpa5448542009-02-13 15:32:32 +0000466
Anders Carlssonacfde802009-02-12 00:39:25 +0000467 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
468
469 // Get a pointer to the generic block literal.
470 const llvm::Type *BlockLiteralTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000471 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000472
473 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000474 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000475 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
476
477 // Get the function pointer from the literal.
478 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Anders Carlssonacfde802009-02-12 00:39:25 +0000479
Mike Stumpa5448542009-02-13 15:32:32 +0000480 BlockLiteral =
481 Builder.CreateBitCast(BlockLiteral,
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000482 llvm::Type::getInt8PtrTy(VMContext),
Anders Carlssonacfde802009-02-12 00:39:25 +0000483 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000484
Anders Carlssonacfde802009-02-12 00:39:25 +0000485 // Add the block literal.
486 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
487 CallArgList Args;
488 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000489
Anders Carlsson782f3972009-04-08 23:13:16 +0000490 QualType FnType = BPT->getPointeeType();
491
Anders Carlssonacfde802009-02-12 00:39:25 +0000492 // And the rest of the arguments.
John McCall183700f2009-09-21 23:43:11 +0000493 EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(),
Anders Carlsson782f3972009-04-08 23:13:16 +0000494 E->arg_begin(), E->arg_end());
Mike Stumpa5448542009-02-13 15:32:32 +0000495
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000496 // Load the function.
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000497 llvm::Value *Func = Builder.CreateLoad(FuncPtr, "tmp");
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000498
John McCall04a67a62010-02-05 21:31:56 +0000499 const FunctionType *FuncTy = FnType->getAs<FunctionType>();
500 QualType ResultType = FuncTy->getResultType();
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000501
Mike Stump1eb44332009-09-09 15:08:12 +0000502 const CGFunctionInfo &FnInfo =
John McCall04a67a62010-02-05 21:31:56 +0000503 CGM.getTypes().getFunctionInfo(ResultType, Args, FuncTy->getCallConv(),
504 FuncTy->getNoReturnAttr());
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000506 // Cast the function pointer to the right type.
Mike Stump1eb44332009-09-09 15:08:12 +0000507 const llvm::Type *BlockFTy =
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000508 CGM.getTypes().GetFunctionType(FnInfo, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Owen Anderson96e0fc72009-07-29 22:16:19 +0000510 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000511 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Anders Carlssonacfde802009-02-12 00:39:25 +0000513 // And call the block.
Anders Carlssona1736c02009-12-24 21:13:40 +0000514 return EmitCall(FnInfo, Func, ReturnValue, Args);
Anders Carlssonacfde802009-02-12 00:39:25 +0000515}
Anders Carlssond5cab542009-02-12 17:55:02 +0000516
Ken Dyck199c3d62010-01-11 17:06:35 +0000517CharUnits CodeGenFunction::AllocateBlockDecl(const BlockDeclRefExpr *E) {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000518 const ValueDecl *VD = E->getDecl();
Ken Dyck199c3d62010-01-11 17:06:35 +0000519 CharUnits &offset = BlockDecls[VD];
Mike Stumpdab514f2009-03-04 03:23:46 +0000520
Mike Stumpdab514f2009-03-04 03:23:46 +0000521 // See if we have already allocated an offset for this variable.
Ken Dyck199c3d62010-01-11 17:06:35 +0000522 if (offset.isPositive())
Mike Stumpea26cb52009-10-21 03:49:08 +0000523 return offset;
524
525 // Don't run the expensive check, unless we have to.
Mike Stump083c25e2009-10-22 00:49:09 +0000526 if (!BlockHasCopyDispose)
527 if (E->isByRef()
528 || BlockRequiresCopying(E->getType()))
529 BlockHasCopyDispose = true;
Mike Stumpea26cb52009-10-21 03:49:08 +0000530
531 // if not, allocate one now.
532 offset = getBlockOffset(E);
533
534 return offset;
535}
536
537llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
538 const ValueDecl *VD = E->getDecl();
Ken Dyck199c3d62010-01-11 17:06:35 +0000539 CharUnits offset = AllocateBlockDecl(E);
Mike Stumpea26cb52009-10-21 03:49:08 +0000540
Mike Stumpdab514f2009-03-04 03:23:46 +0000541
542 llvm::Value *BlockLiteral = LoadBlockStruct();
543 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
Mike Stumpea26cb52009-10-21 03:49:08 +0000544 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Ken Dyck199c3d62010-01-11 17:06:35 +0000545 offset.getQuantity()),
Mike Stump58a85142009-03-04 22:48:06 +0000546 "block.literal");
Mike Stumpdab514f2009-03-04 03:23:46 +0000547 if (E->isByRef()) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000548 const llvm::Type *PtrStructTy
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000549 = llvm::PointerType::get(BuildByRefType(VD), 0);
Mike Stumpa8b60c92009-03-21 21:00:35 +0000550 // The block literal will need a copy/destroy helper.
551 BlockHasCopyDispose = true;
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000552
553 const llvm::Type *Ty = PtrStructTy;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000554 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000555 V = Builder.CreateBitCast(V, Ty);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000556 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000557 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000558 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000559 V = Builder.CreateBitCast(V, PtrStructTy);
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000560 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
561 VD->getNameAsString());
Mike Stumpdab514f2009-03-04 03:23:46 +0000562 } else {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000563 const llvm::Type *Ty = CGM.getTypes().ConvertType(VD->getType());
564
Owen Anderson96e0fc72009-07-29 22:16:19 +0000565 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000566 V = Builder.CreateBitCast(V, Ty);
567 }
568 return V;
569}
570
Mike Stump6cc88f72009-03-20 21:53:12 +0000571void CodeGenFunction::BlockForwardSelf() {
572 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
573 ImplicitParamDecl *SelfDecl = OMD->getSelfDecl();
574 llvm::Value *&DMEntry = LocalDeclMap[SelfDecl];
575 if (DMEntry)
576 return;
577 // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care
578 BlockDeclRefExpr *BDRE = new (getContext())
579 BlockDeclRefExpr(SelfDecl,
580 SelfDecl->getType(), SourceLocation(), false);
581 DMEntry = GetAddrOfBlockDecl(BDRE);
582}
583
Mike Stump67a64482009-02-14 22:16:35 +0000584llvm::Constant *
Mike Stump90a90432009-03-04 18:47:42 +0000585BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000586 // Generate the block descriptor.
587 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000588 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
589 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000590
Anders Carlssond5cab542009-02-12 17:55:02 +0000591 llvm::Constant *DescriptorFields[2];
Mike Stumpa5448542009-02-13 15:32:32 +0000592
Anders Carlssond5cab542009-02-12 17:55:02 +0000593 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000594 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000595
Anders Carlssond5cab542009-02-12 17:55:02 +0000596 // Block literal size. For global blocks we just use the size of the generic
597 // block literal struct.
Ken Dyck687cc4a2010-01-26 13:48:07 +0000598 CharUnits BlockLiteralSize =
599 CGM.GetTargetTypeStoreSize(getGenericBlockLiteralType());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000600 DescriptorFields[1] =
Ken Dyck199c3d62010-01-11 17:06:35 +0000601 llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize.getQuantity());
Mike Stumpa5448542009-02-13 15:32:32 +0000602
603 llvm::Constant *DescriptorStruct =
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000604 llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 2, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000605
Anders Carlssond5cab542009-02-12 17:55:02 +0000606 llvm::GlobalVariable *Descriptor =
Owen Anderson1c431b32009-07-08 19:05:04 +0000607 new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000608 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000609 DescriptorStruct, "__block_descriptor_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000610
David Chisnall5e530af2009-11-17 19:33:30 +0000611 int FieldCount = 5;
Anders Carlssond5cab542009-02-12 17:55:02 +0000612 // Generate the constants for the block literal.
David Chisnall5e530af2009-11-17 19:33:30 +0000613 if (CGM.getContext().getLangOptions().BlockIntrospection)
614 FieldCount = 6;
615
616 std::vector<llvm::Constant*> LiteralFields(FieldCount);
Mike Stumpa5448542009-02-13 15:32:32 +0000617
Mike Stump67a64482009-02-14 22:16:35 +0000618 CodeGenFunction::BlockInfo Info(0, n);
Ken Dyck199c3d62010-01-11 17:06:35 +0000619 CharUnits subBlockSize;
Ken Dyck30a8a272010-01-26 19:13:33 +0000620 CharUnits subBlockAlign;
Mike Stumpa99038c2009-02-28 09:07:16 +0000621 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Daniel Dunbard67b09a2009-03-12 03:07:24 +0000622 bool subBlockHasCopyDispose = false;
Mike Stump7f28a9c2009-03-13 23:34:28 +0000623 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000624 llvm::Function *Fn
Mike Stump6cc88f72009-03-20 21:53:12 +0000625 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000626 subBlockSize,
Mike Stump90a90432009-03-04 18:47:42 +0000627 subBlockAlign,
Mike Stump00470a12009-03-05 08:32:30 +0000628 subBlockDeclRefDecls,
629 subBlockHasCopyDispose);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000630 assert(subBlockSize == BlockLiteralSize
631 && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000632
Anders Carlssond5cab542009-02-12 17:55:02 +0000633 // isa
Mike Stumpf99f1d02009-02-13 17:23:42 +0000634 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000635
Anders Carlssond5cab542009-02-12 17:55:02 +0000636 // Flags
David Chisnall5e530af2009-11-17 19:33:30 +0000637 LiteralFields[1] = CGM.getContext().getLangOptions().BlockIntrospection ?
638 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR |
639 BLOCK_HAS_OBJC_TYPE) :
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000640 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR);
Mike Stumpa5448542009-02-13 15:32:32 +0000641
Anders Carlssond5cab542009-02-12 17:55:02 +0000642 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000643 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000644
Anders Carlssond5cab542009-02-12 17:55:02 +0000645 // Function
646 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000647
Anders Carlssond5cab542009-02-12 17:55:02 +0000648 // Descriptor
649 LiteralFields[4] = Descriptor;
David Chisnall5e530af2009-11-17 19:33:30 +0000650
651 // Type encoding
652 if (CGM.getContext().getLangOptions().BlockIntrospection) {
653 std::string BlockTypeEncoding;
654 CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
655
656 LiteralFields[5] = CGM.GetAddrOfConstantCString(BlockTypeEncoding);
657 }
Mike Stumpa5448542009-02-13 15:32:32 +0000658
659 llvm::Constant *BlockLiteralStruct =
David Chisnall5e530af2009-11-17 19:33:30 +0000660 llvm::ConstantStruct::get(VMContext, LiteralFields, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000661
662 llvm::GlobalVariable *BlockLiteral =
Owen Anderson1c431b32009-07-08 19:05:04 +0000663 new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000664 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000665 BlockLiteralStruct, "__block_literal_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000666
Anders Carlssond5cab542009-02-12 17:55:02 +0000667 return BlockLiteral;
668}
669
Mike Stump4e7a1f72009-02-21 20:00:35 +0000670llvm::Value *CodeGenFunction::LoadBlockStruct() {
Mike Stumpbf1914b2009-10-20 20:30:01 +0000671 llvm::Value *V = Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()],
672 "self");
673 // For now, we codegen based upon byte offsets.
674 return Builder.CreateBitCast(V, PtrToInt8Ty);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000675}
676
Mike Stump00470a12009-03-05 08:32:30 +0000677llvm::Function *
678CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
679 const BlockInfo& Info,
Mike Stump6cc88f72009-03-20 21:53:12 +0000680 const Decl *OuterFuncDecl,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000681 llvm::DenseMap<const Decl*, llvm::Value*> ldm,
Ken Dyck199c3d62010-01-11 17:06:35 +0000682 CharUnits &Size,
Ken Dyck30a8a272010-01-26 19:13:33 +0000683 CharUnits &Align,
Mike Stump00470a12009-03-05 08:32:30 +0000684 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls,
685 bool &subBlockHasCopyDispose) {
Devang Patel963dfbd2009-04-15 21:51:44 +0000686
687 // Check if we should generate debug info for this block.
688 if (CGM.getDebugInfo())
689 DebugInfo = CGM.getDebugInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Mike Stump7f28a9c2009-03-13 23:34:28 +0000691 // Arrange for local static and local extern declarations to appear
692 // to be local to this function as well, as they are directly referenced
693 // in a block.
694 for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
695 i != ldm.end();
696 ++i) {
697 const VarDecl *VD = dyn_cast<VarDecl>(i->first);
Mike Stump1eb44332009-09-09 15:08:12 +0000698
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000699 if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage())
Mike Stump7f28a9c2009-03-13 23:34:28 +0000700 LocalDeclMap[VD] = i->second;
701 }
702
Ken Dyck687cc4a2010-01-26 13:48:07 +0000703 BlockOffset =
704 CGM.GetTargetTypeStoreSize(CGM.getGenericBlockLiteralType());
Ken Dyck30a8a272010-01-26 19:13:33 +0000705 BlockAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Eli Friedman48f91222009-03-28 03:24:54 +0000706
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000707 const FunctionType *BlockFunctionType = BExpr->getFunctionType();
708 QualType ResultType;
John McCall04a67a62010-02-05 21:31:56 +0000709 CallingConv CC = BlockFunctionType->getCallConv();
710 bool NoReturn = BlockFunctionType->getNoReturnAttr();
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000711 bool IsVariadic;
Mike Stump1eb44332009-09-09 15:08:12 +0000712 if (const FunctionProtoType *FTy =
Fariborz Jahanianda0895d2009-04-11 18:54:57 +0000713 dyn_cast<FunctionProtoType>(BlockFunctionType)) {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000714 ResultType = FTy->getResultType();
715 IsVariadic = FTy->isVariadic();
Mike Stumpb3589f42009-07-30 22:28:39 +0000716 } else {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000717 // K&R style block.
718 ResultType = BlockFunctionType->getResultType();
719 IsVariadic = false;
720 }
Mike Stumpa5448542009-02-13 15:32:32 +0000721
Anders Carlssond5cab542009-02-12 17:55:02 +0000722 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000723
Mike Stumpea26cb52009-10-21 03:49:08 +0000724 CurFuncDecl = OuterFuncDecl;
725
Chris Lattner161d36d2009-02-28 19:01:03 +0000726 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlssond5cab542009-02-12 17:55:02 +0000727
Mike Stumpea26cb52009-10-21 03:49:08 +0000728 IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor");
Mike Stumpadaaad32009-10-20 02:12:22 +0000729
Mike Stumpbfbd5df2009-10-21 18:24:18 +0000730 // Allocate all BlockDeclRefDecls, so we can calculate the right ParmTy below.
Mike Stump0298d382009-10-21 22:02:08 +0000731 AllocateAllBlockDeclRefs(Info, this);
Mike Stumpea26cb52009-10-21 03:49:08 +0000732
Mike Stump083c25e2009-10-22 00:49:09 +0000733 QualType ParmTy = getContext().getBlockParmType(BlockHasCopyDispose,
734 BlockDeclRefDecls);
Anders Carlssond5cab542009-02-12 17:55:02 +0000735 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000736 ImplicitParamDecl *SelfDecl =
Anders Carlssond5cab542009-02-12 17:55:02 +0000737 ImplicitParamDecl::Create(getContext(), 0,
Mike Stumpadaaad32009-10-20 02:12:22 +0000738 SourceLocation(), II,
739 ParmTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000740
Anders Carlssond5cab542009-02-12 17:55:02 +0000741 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump4e7a1f72009-02-21 20:00:35 +0000742 BlockStructDecl = SelfDecl;
Mike Stumpa5448542009-02-13 15:32:32 +0000743
Steve Naroffe78b8092009-03-13 16:56:44 +0000744 for (BlockDecl::param_const_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000745 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000746 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000747
748 const CGFunctionInfo &FI =
John McCall04a67a62010-02-05 21:31:56 +0000749 CGM.getTypes().getFunctionInfo(ResultType, Args, CC, NoReturn);
Anders Carlssond5cab542009-02-12 17:55:02 +0000750
Anders Carlssond5cab542009-02-12 17:55:02 +0000751 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000752 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic);
Mike Stumpa5448542009-02-13 15:32:32 +0000753
754 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000755 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000756 llvm::Twine("__") + Info.Name + "_block_invoke_",
Anders Carlssond5cab542009-02-12 17:55:02 +0000757 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000758
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000759 CGM.SetInternalFunctionAttributes(BD, Fn, FI);
760
Chris Lattner4863db42009-04-23 07:18:56 +0000761 StartFunction(BD, ResultType, Fn, Args,
Chris Lattner161d36d2009-02-28 19:01:03 +0000762 BExpr->getBody()->getLocEnd());
Mike Stumpb1a6e682009-09-30 02:43:10 +0000763
Chris Lattner4863db42009-04-23 07:18:56 +0000764 CurFuncDecl = OuterFuncDecl;
Chris Lattnerb5437d22009-04-23 05:30:27 +0000765 CurCodeDecl = BD;
Mike Stumpb289b3f2009-10-01 22:29:41 +0000766
767 // Save a spot to insert the debug information for all the BlockDeclRefDecls.
768 llvm::BasicBlock *entry = Builder.GetInsertBlock();
769 llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint();
770 --entry_ptr;
771
Chris Lattner161d36d2009-02-28 19:01:03 +0000772 EmitStmt(BExpr->getBody());
Mike Stumpb289b3f2009-10-01 22:29:41 +0000773
Mike Stumpde8c5c72009-10-01 00:27:30 +0000774 // Remember where we were...
775 llvm::BasicBlock *resume = Builder.GetInsertBlock();
Mike Stumpb289b3f2009-10-01 22:29:41 +0000776
Mike Stumpde8c5c72009-10-01 00:27:30 +0000777 // Go back to the entry.
Mike Stumpb289b3f2009-10-01 22:29:41 +0000778 ++entry_ptr;
779 Builder.SetInsertPoint(entry, entry_ptr);
780
Mike Stumpb1a6e682009-09-30 02:43:10 +0000781 if (CGDebugInfo *DI = getDebugInfo()) {
Mike Stumpb1a6e682009-09-30 02:43:10 +0000782 // Emit debug information for all the BlockDeclRefDecls.
Chris Lattner14b1a362010-01-25 03:29:35 +0000783 for (unsigned i = 0, e = BlockDeclRefDecls.size(); i != e; ++i) {
784 if (const BlockDeclRefExpr *BDRE =
785 dyn_cast<BlockDeclRefExpr>(BlockDeclRefDecls[i])) {
Mike Stumpb1a6e682009-09-30 02:43:10 +0000786 const ValueDecl *D = BDRE->getDecl();
787 DI->setLocation(D->getLocation());
788 DI->EmitDeclareOfBlockDeclRefVariable(BDRE,
789 LocalDeclMap[getBlockStructDecl()],
790 Builder, this);
791 }
792 }
Mike Stumpb1a6e682009-09-30 02:43:10 +0000793 }
Mike Stumpde8c5c72009-10-01 00:27:30 +0000794 // And resume where we left off.
795 if (resume == 0)
796 Builder.ClearInsertionPoint();
797 else
798 Builder.SetInsertPoint(resume);
Mike Stumpb1a6e682009-09-30 02:43:10 +0000799
Chris Lattner161d36d2009-02-28 19:01:03 +0000800 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlssond5cab542009-02-12 17:55:02 +0000801
Mike Stump8a2b4b12009-02-25 23:33:13 +0000802 // The runtime needs a minimum alignment of a void *.
Ken Dyck30a8a272010-01-26 19:13:33 +0000803 CharUnits MinAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Ken Dyck199c3d62010-01-11 17:06:35 +0000804 BlockOffset = CharUnits::fromQuantity(
Ken Dyck30a8a272010-01-26 19:13:33 +0000805 llvm::RoundUpToAlignment(BlockOffset.getQuantity(),
806 MinAlign.getQuantity()));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000807
Mike Stump4e7a1f72009-02-21 20:00:35 +0000808 Size = BlockOffset;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000809 Align = BlockAlign;
810 subBlockDeclRefDecls = BlockDeclRefDecls;
Mike Stump00470a12009-03-05 08:32:30 +0000811 subBlockHasCopyDispose |= BlockHasCopyDispose;
Anders Carlssond5cab542009-02-12 17:55:02 +0000812 return Fn;
813}
Mike Stumpa99038c2009-02-28 09:07:16 +0000814
Ken Dyck199c3d62010-01-11 17:06:35 +0000815CharUnits BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
Mike Stumpa99038c2009-02-28 09:07:16 +0000816 const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
817
Ken Dyck199c3d62010-01-11 17:06:35 +0000818 CharUnits Size = getContext().getTypeSizeInChars(D->getType());
Ken Dyck8b752f12010-01-27 17:10:57 +0000819 CharUnits Align = getContext().getDeclAlign(D);
Mike Stumpa99038c2009-02-28 09:07:16 +0000820
821 if (BDRE->isByRef()) {
Ken Dyck199c3d62010-01-11 17:06:35 +0000822 Size = getContext().getTypeSizeInChars(getContext().VoidPtrTy);
Ken Dyck30a8a272010-01-26 19:13:33 +0000823 Align = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Mike Stumpa99038c2009-02-28 09:07:16 +0000824 }
825
Ken Dyck30a8a272010-01-26 19:13:33 +0000826 assert ((Align.isPositive()) && "alignment must be 1 byte or more");
Mike Stumpa99038c2009-02-28 09:07:16 +0000827
Ken Dyck199c3d62010-01-11 17:06:35 +0000828 CharUnits OldOffset = BlockOffset;
Mike Stumpa99038c2009-02-28 09:07:16 +0000829
830 // Ensure proper alignment, even if it means we have to have a gap
Ken Dyck199c3d62010-01-11 17:06:35 +0000831 BlockOffset = CharUnits::fromQuantity(
Ken Dyck30a8a272010-01-26 19:13:33 +0000832 llvm::RoundUpToAlignment(BlockOffset.getQuantity(), Align.getQuantity()));
Mike Stumpa99038c2009-02-28 09:07:16 +0000833 BlockAlign = std::max(Align, BlockAlign);
Mike Stump00470a12009-03-05 08:32:30 +0000834
Ken Dyck199c3d62010-01-11 17:06:35 +0000835 CharUnits Pad = BlockOffset - OldOffset;
836 if (Pad.isPositive()) {
837 llvm::ArrayType::get(llvm::Type::getInt8Ty(VMContext), Pad.getQuantity());
Mike Stumpa99038c2009-02-28 09:07:16 +0000838 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
Ken Dyck199c3d62010-01-11 17:06:35 +0000839 llvm::APInt(32,
840 Pad.getQuantity()),
Mike Stumpa99038c2009-02-28 09:07:16 +0000841 ArrayType::Normal, 0);
842 ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000843 0, QualType(PadTy), 0, VarDecl::None);
Mike Stumpa99038c2009-02-28 09:07:16 +0000844 Expr *E;
845 E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
Douglas Gregor0da76df2009-11-23 11:41:28 +0000846 SourceLocation());
Mike Stumpa99038c2009-02-28 09:07:16 +0000847 BlockDeclRefDecls.push_back(E);
848 }
849 BlockDeclRefDecls.push_back(BDRE);
850
851 BlockOffset += Size;
852 return BlockOffset-Size;
853}
Mike Stumpdab514f2009-03-04 03:23:46 +0000854
Mike Stump08920992009-03-07 02:35:30 +0000855llvm::Constant *BlockFunction::
856GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000857 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000858 QualType R = getContext().VoidTy;
859
860 FunctionArgList Args;
861 // FIXME: This leaks
Mike Stump08920992009-03-07 02:35:30 +0000862 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +0000863 ImplicitParamDecl::Create(getContext(), 0,
864 SourceLocation(), 0,
Mike Stump08920992009-03-07 02:35:30 +0000865 getContext().getPointerType(getContext().VoidTy));
866 Args.push_back(std::make_pair(Dst, Dst->getType()));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000867 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +0000868 ImplicitParamDecl::Create(getContext(), 0,
869 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000870 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000871 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Mike Stumpa4f668f2009-03-06 01:33:24 +0000873 const CGFunctionInfo &FI =
John McCall04a67a62010-02-05 21:31:56 +0000874 CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000875
Mike Stump3899a7f2009-06-05 23:26:36 +0000876 // FIXME: We'd like to put these into a mergable by content, with
877 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000878 CodeGenTypes &Types = CGM.getTypes();
879 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
880
881 llvm::Function *Fn =
882 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000883 "__copy_helper_block_", &CGM.getModule());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000884
885 IdentifierInfo *II
886 = &CGM.getContext().Idents.get("__copy_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 Stump08920992009-03-07 02:35:30 +0000894
895 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
896 llvm::Type *PtrPtrT;
Mike Stump08920992009-03-07 02:35:30 +0000897
Mike Stumpb7477cf2009-04-10 18:52:28 +0000898 if (NoteForHelperp) {
899 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump08920992009-03-07 02:35:30 +0000900
Owen Anderson96e0fc72009-07-29 22:16:19 +0000901 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000902 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
903 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump08920992009-03-07 02:35:30 +0000904
Mike Stumpb7477cf2009-04-10 18:52:28 +0000905 llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000906 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000907 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000908 DstObj = Builder.CreateBitCast(DstObj, PtrPtrT);
909 DstObj = Builder.CreateLoad(DstObj);
Mike Stump08920992009-03-07 02:35:30 +0000910
Mike Stumpb7477cf2009-04-10 18:52:28 +0000911 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
912 int flag = NoteForHelper[i].flag;
913 int index = NoteForHelper[i].index;
Mike Stump08920992009-03-07 02:35:30 +0000914
Mike Stumpb7477cf2009-04-10 18:52:28 +0000915 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
916 || NoteForHelper[i].RequiresCopying) {
917 llvm::Value *Srcv = SrcObj;
918 Srcv = Builder.CreateStructGEP(Srcv, index);
919 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000920 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000921 Srcv = Builder.CreateLoad(Srcv);
922
923 llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
924 Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
925
Owen Anderson0032b272009-08-13 21:57:51 +0000926 llvm::Value *N = llvm::ConstantInt::get(
927 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000928 llvm::Value *F = getBlockObjectAssign();
929 Builder.CreateCall3(F, Dstv, Srcv, N);
930 }
Mike Stump08920992009-03-07 02:35:30 +0000931 }
932 }
933
Mike Stumpa4f668f2009-03-06 01:33:24 +0000934 CGF.FinishFunction();
935
Owen Anderson3c4972d2009-07-29 18:54:39 +0000936 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000937}
938
Mike Stumpcf62d392009-03-06 18:42:23 +0000939llvm::Constant *BlockFunction::
Mike Stump08920992009-03-07 02:35:30 +0000940GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
941 const llvm::StructType* T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000942 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000943 QualType R = getContext().VoidTy;
944
945 FunctionArgList Args;
946 // FIXME: This leaks
947 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +0000948 ImplicitParamDecl::Create(getContext(), 0,
949 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000950 getContext().getPointerType(getContext().VoidTy));
951
952 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000953
Mike Stumpa4f668f2009-03-06 01:33:24 +0000954 const CGFunctionInfo &FI =
John McCall04a67a62010-02-05 21:31:56 +0000955 CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000956
Mike Stump3899a7f2009-06-05 23:26:36 +0000957 // FIXME: We'd like to put these into a mergable by content, with
958 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000959 CodeGenTypes &Types = CGM.getTypes();
960 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
961
962 llvm::Function *Fn =
963 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000964 "__destroy_helper_block_", &CGM.getModule());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000965
966 IdentifierInfo *II
967 = &CGM.getContext().Idents.get("__destroy_helper_block_");
968
969 FunctionDecl *FD = FunctionDecl::Create(getContext(),
970 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000971 SourceLocation(), II, R, 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000972 FunctionDecl::Static, false,
973 true);
974 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1edf6b62009-03-07 02:53:18 +0000975
Mike Stumpb7477cf2009-04-10 18:52:28 +0000976 if (NoteForHelperp) {
977 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump1edf6b62009-03-07 02:53:18 +0000978
Mike Stumpb7477cf2009-04-10 18:52:28 +0000979 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
980 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000981 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000982 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
983 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump1edf6b62009-03-07 02:53:18 +0000984
Mike Stumpb7477cf2009-04-10 18:52:28 +0000985 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
986 int flag = NoteForHelper[i].flag;
987 int index = NoteForHelper[i].index;
Mike Stump1edf6b62009-03-07 02:53:18 +0000988
Mike Stumpb7477cf2009-04-10 18:52:28 +0000989 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
990 || NoteForHelper[i].RequiresCopying) {
991 llvm::Value *Srcv = SrcObj;
992 Srcv = Builder.CreateStructGEP(Srcv, index);
993 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000994 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000995 Srcv = Builder.CreateLoad(Srcv);
996
997 BuildBlockRelease(Srcv, flag);
998 }
Mike Stump1edf6b62009-03-07 02:53:18 +0000999 }
1000 }
1001
Mike Stumpa4f668f2009-03-06 01:33:24 +00001002 CGF.FinishFunction();
1003
Owen Anderson3c4972d2009-07-29 18:54:39 +00001004 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpa4f668f2009-03-06 01:33:24 +00001005}
1006
Mike Stump08920992009-03-07 02:35:30 +00001007llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001008 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump08920992009-03-07 02:35:30 +00001009 return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
1010 T, NoteForHelper);
Mike Stumpa4f668f2009-03-06 01:33:24 +00001011}
1012
Mike Stump08920992009-03-07 02:35:30 +00001013llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001014 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stump08920992009-03-07 02:35:30 +00001015 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001016 T, NoteForHelperp);
Mike Stumpdab514f2009-03-04 03:23:46 +00001017}
Mike Stump797b6322009-03-05 01:23:13 +00001018
Mike Stumpee094222009-03-06 06:12:24 +00001019llvm::Constant *BlockFunction::
1020GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
Mike Stump45031c02009-03-06 02:29:21 +00001021 QualType R = getContext().VoidTy;
1022
1023 FunctionArgList Args;
1024 // FIXME: This leaks
Mike Stumpee094222009-03-06 06:12:24 +00001025 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +00001026 ImplicitParamDecl::Create(getContext(), 0,
1027 SourceLocation(), 0,
Mike Stumpee094222009-03-06 06:12:24 +00001028 getContext().getPointerType(getContext().VoidTy));
1029 Args.push_back(std::make_pair(Dst, Dst->getType()));
1030
1031 // FIXME: This leaks
Mike Stump45031c02009-03-06 02:29:21 +00001032 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001033 ImplicitParamDecl::Create(getContext(), 0,
1034 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001035 getContext().getPointerType(getContext().VoidTy));
Mike Stump45031c02009-03-06 02:29:21 +00001036 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Mike Stump45031c02009-03-06 02:29:21 +00001038 const CGFunctionInfo &FI =
John McCall04a67a62010-02-05 21:31:56 +00001039 CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false);
Mike Stump45031c02009-03-06 02:29:21 +00001040
Mike Stump45031c02009-03-06 02:29:21 +00001041 CodeGenTypes &Types = CGM.getTypes();
1042 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1043
Mike Stump3899a7f2009-06-05 23:26:36 +00001044 // FIXME: We'd like to put these into a mergable by content, with
1045 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001046 llvm::Function *Fn =
1047 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001048 "__Block_byref_id_object_copy_", &CGM.getModule());
Mike Stump45031c02009-03-06 02:29:21 +00001049
1050 IdentifierInfo *II
1051 = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
1052
1053 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1054 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001055 SourceLocation(), II, R, 0,
Mike Stump45031c02009-03-06 02:29:21 +00001056 FunctionDecl::Static, false,
1057 true);
1058 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpee094222009-03-06 06:12:24 +00001059
1060 // dst->x
1061 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001062 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001063 V = Builder.CreateLoad(V);
Mike Stumpee094222009-03-06 06:12:24 +00001064 V = Builder.CreateStructGEP(V, 6, "x");
1065 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
1066
1067 // src->x
1068 V = CGF.GetAddrOfLocalVar(Src);
1069 V = Builder.CreateLoad(V);
1070 V = Builder.CreateBitCast(V, T);
1071 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001072 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpee094222009-03-06 06:12:24 +00001073 llvm::Value *SrcObj = Builder.CreateLoad(V);
Mike Stump1eb44332009-09-09 15:08:12 +00001074
Mike Stumpee094222009-03-06 06:12:24 +00001075 flag |= BLOCK_BYREF_CALLER;
1076
Owen Anderson0032b272009-08-13 21:57:51 +00001077 llvm::Value *N = llvm::ConstantInt::get(
1078 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpee094222009-03-06 06:12:24 +00001079 llvm::Value *F = getBlockObjectAssign();
1080 Builder.CreateCall3(F, DstObj, SrcObj, N);
1081
Mike Stump45031c02009-03-06 02:29:21 +00001082 CGF.FinishFunction();
1083
Owen Anderson3c4972d2009-07-29 18:54:39 +00001084 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001085}
1086
Mike Stump1851b682009-03-06 04:53:30 +00001087llvm::Constant *
1088BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
1089 int flag) {
Mike Stump45031c02009-03-06 02:29:21 +00001090 QualType R = getContext().VoidTy;
1091
1092 FunctionArgList Args;
1093 // FIXME: This leaks
1094 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001095 ImplicitParamDecl::Create(getContext(), 0,
1096 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001097 getContext().getPointerType(getContext().VoidTy));
1098
1099 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Mike Stump45031c02009-03-06 02:29:21 +00001101 const CGFunctionInfo &FI =
John McCall04a67a62010-02-05 21:31:56 +00001102 CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false);
Mike Stump45031c02009-03-06 02:29:21 +00001103
Mike Stump45031c02009-03-06 02:29:21 +00001104 CodeGenTypes &Types = CGM.getTypes();
1105 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1106
Mike Stump3899a7f2009-06-05 23:26:36 +00001107 // FIXME: We'd like to put these into a mergable by content, with
1108 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001109 llvm::Function *Fn =
1110 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001111 "__Block_byref_id_object_dispose_",
Mike Stump45031c02009-03-06 02:29:21 +00001112 &CGM.getModule());
1113
1114 IdentifierInfo *II
1115 = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
1116
1117 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1118 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001119 SourceLocation(), II, R, 0,
Mike Stump45031c02009-03-06 02:29:21 +00001120 FunctionDecl::Static, false,
1121 true);
1122 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1851b682009-03-06 04:53:30 +00001123
1124 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001125 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001126 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001127 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001128 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001129 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001130
Mike Stump1851b682009-03-06 04:53:30 +00001131 flag |= BLOCK_BYREF_CALLER;
1132 BuildBlockRelease(V, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001133 CGF.FinishFunction();
1134
Owen Anderson3c4972d2009-07-29 18:54:39 +00001135 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001136}
1137
Mike Stumpee094222009-03-06 06:12:24 +00001138llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001139 int Flag, unsigned Align) {
1140 // All alignments below that of pointer alignment collapse down to just
Mike Stump3899a7f2009-06-05 23:26:36 +00001141 // pointer alignment, as we always have at least that much alignment to begin
1142 // with.
1143 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001144
Mike Stump3899a7f2009-06-05 23:26:36 +00001145 // As an optimization, we only generate a single function of each kind we
1146 // might need. We need a different one for each alignment and for each
1147 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001148 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1149 llvm::Constant *&Entry = CGM.AssignCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001150 if (Entry)
1151 return Entry;
Chris Lattner10976d92009-12-05 08:21:30 +00001152 return Entry = CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, Flag);
Mike Stump45031c02009-03-06 02:29:21 +00001153}
1154
Mike Stump1851b682009-03-06 04:53:30 +00001155llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001156 int Flag,
Mike Stump3899a7f2009-06-05 23:26:36 +00001157 unsigned Align) {
1158 // All alignments below that of pointer alignment collpase down to just
1159 // pointer alignment, as we always have at least that much alignment to begin
1160 // with.
1161 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001162
Mike Stump3899a7f2009-06-05 23:26:36 +00001163 // As an optimization, we only generate a single function of each kind we
1164 // might need. We need a different one for each alignment and for each
1165 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001166 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1167 llvm::Constant *&Entry = CGM.DestroyCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001168 if (Entry)
1169 return Entry;
Chris Lattner10976d92009-12-05 08:21:30 +00001170 return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, Flag);
Mike Stump45031c02009-03-06 02:29:21 +00001171}
1172
Mike Stump797b6322009-03-05 01:23:13 +00001173llvm::Value *BlockFunction::getBlockObjectDispose() {
1174 if (CGM.BlockObjectDispose == 0) {
1175 const llvm::FunctionType *FTy;
1176 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001177 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stump797b6322009-03-05 01:23:13 +00001178 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001179 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson96e0fc72009-07-29 22:16:19 +00001180 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stump00470a12009-03-05 08:32:30 +00001181 CGM.BlockObjectDispose
Mike Stump797b6322009-03-05 01:23:13 +00001182 = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
1183 }
1184 return CGM.BlockObjectDispose;
1185}
1186
Mike Stumpee094222009-03-06 06:12:24 +00001187llvm::Value *BlockFunction::getBlockObjectAssign() {
1188 if (CGM.BlockObjectAssign == 0) {
1189 const llvm::FunctionType *FTy;
1190 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001191 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stumpee094222009-03-06 06:12:24 +00001192 ArgTys.push_back(PtrToInt8Ty);
1193 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001194 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson96e0fc72009-07-29 22:16:19 +00001195 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stumpee094222009-03-06 06:12:24 +00001196 CGM.BlockObjectAssign
1197 = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
1198 }
1199 return CGM.BlockObjectAssign;
1200}
1201
Mike Stump1851b682009-03-06 04:53:30 +00001202void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Mike Stump797b6322009-03-05 01:23:13 +00001203 llvm::Value *F = getBlockObjectDispose();
Mike Stump1851b682009-03-06 04:53:30 +00001204 llvm::Value *N;
Mike Stump797b6322009-03-05 01:23:13 +00001205 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001206 N = llvm::ConstantInt::get(llvm::Type::getInt32Ty(V->getContext()), flag);
Mike Stump797b6322009-03-05 01:23:13 +00001207 Builder.CreateCall2(F, V, N);
1208}
Mike Stump00470a12009-03-05 08:32:30 +00001209
1210ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stump08920992009-03-07 02:35:30 +00001211
1212BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1213 CGBuilderTy &B)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001214 : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) {
Owen Anderson0032b272009-08-13 21:57:51 +00001215 PtrToInt8Ty = llvm::PointerType::getUnqual(
1216 llvm::Type::getInt8Ty(VMContext));
Mike Stump08920992009-03-07 02:35:30 +00001217
1218 BlockHasCopyDispose = false;
1219}