blob: 46b62441d6e452b7d7158005f140a32864f776f5 [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"
Fariborz Jahanian263c4de2010-02-10 23:34:57 +000016#include "CGObjCRuntime.h"
Anders Carlssonacfde802009-02-12 00:39:25 +000017#include "CodeGenModule.h"
Mike Stump6cc88f72009-03-20 21:53:12 +000018#include "clang/AST/DeclObjC.h"
Anders Carlssonacfde802009-02-12 00:39:25 +000019#include "llvm/Module.h"
Anders Carlssond5cab542009-02-12 17:55:02 +000020#include "llvm/Target/TargetData.h"
Anders Carlssonacfde802009-02-12 00:39:25 +000021#include <algorithm>
Torok Edwinf42e4a62009-08-24 13:25:12 +000022
Anders Carlssonacfde802009-02-12 00:39:25 +000023using namespace clang;
24using namespace CodeGen;
25
Mike Stumpcf62d392009-03-06 18:42:23 +000026llvm::Constant *CodeGenFunction::
Ken Dyck199c3d62010-01-11 17:06:35 +000027BuildDescriptorBlockDecl(bool BlockHasCopyDispose, CharUnits Size,
Mike Stumpa803b0e2009-03-25 17:58:24 +000028 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.
Ken Dyck199c3d62010-01-11 17:06:35 +000043 C = llvm::ConstantInt::get(UnsignedLongTy, Size.getQuantity());
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 Stump38e16272009-10-21 22:01:24 +000076static void CollectBlockDeclRefInfo(
77 const Stmt *S, CodeGenFunction::BlockInfo &Info,
78 llvm::SmallSet<const DeclContext *, 16> &InnerContexts) {
Anders Carlsson4de9fce2009-03-01 01:09:12 +000079 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
80 I != E; ++I)
Daniel Dunbar82573ee2009-03-02 07:00:57 +000081 if (*I)
Mike Stump38e16272009-10-21 22:01:24 +000082 CollectBlockDeclRefInfo(*I, Info, InnerContexts);
Mike Stump00470a12009-03-05 08:32:30 +000083
Mike Stumpea26cb52009-10-21 03:49:08 +000084 // We want to ensure we walk down into block literals so we can find
85 // all nested BlockDeclRefExprs.
Mike Stump38e16272009-10-21 22:01:24 +000086 if (const BlockExpr *BE = dyn_cast<BlockExpr>(S)) {
87 InnerContexts.insert(cast<DeclContext>(BE->getBlockDecl()));
88 CollectBlockDeclRefInfo(BE->getBody(), Info, InnerContexts);
89 }
Mike Stumpea26cb52009-10-21 03:49:08 +000090
Mike Stump38e16272009-10-21 22:01:24 +000091 if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) {
Anders Carlsson4de9fce2009-03-01 01:09:12 +000092 // FIXME: Handle enums.
Mike Stump38e16272009-10-21 22:01:24 +000093 if (isa<FunctionDecl>(BDRE->getDecl()))
Anders Carlsson4de9fce2009-03-01 01:09:12 +000094 return;
Mike Stump00470a12009-03-05 08:32:30 +000095
Mike Stump38e16272009-10-21 22:01:24 +000096 // Only Decls that escape are added.
97 if (!InnerContexts.count(BDRE->getDecl()->getDeclContext()))
98 Info.DeclRefs.push_back(BDRE);
Anders Carlsson4de9fce2009-03-01 01:09:12 +000099 }
100}
101
Mike Stump58a85142009-03-04 22:48:06 +0000102/// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be
103/// declared as a global variable instead of on the stack.
Chris Lattnere2f79b62009-05-13 02:50:56 +0000104static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info) {
Mike Stumpea26cb52009-10-21 03:49:08 +0000105 return Info.DeclRefs.empty();
106}
107
108/// AllocateAllBlockDeclRefs - Preallocate all nested BlockDeclRefExprs to
109/// ensure we can generate the debug information for the parameter for the block
110/// invoke function.
111static void AllocateAllBlockDeclRefs(const CodeGenFunction::BlockInfo &Info,
112 CodeGenFunction *CGF) {
113 // Always allocate self, as it is often handy in the debugger, even if there
114 // is no codegen in the block that uses it. This is also useful to always do
115 // this as if we didn't, we'd have to figure out all code that uses a self
116 // pointer, including implicit uses.
117 if (const ObjCMethodDecl *OMD
118 = dyn_cast_or_null<ObjCMethodDecl>(CGF->CurFuncDecl)) {
119 ImplicitParamDecl *SelfDecl = OMD->getSelfDecl();
120 BlockDeclRefExpr *BDRE = new (CGF->getContext())
121 BlockDeclRefExpr(SelfDecl,
122 SelfDecl->getType(), SourceLocation(), false);
123 CGF->AllocateBlockDecl(BDRE);
124 }
125
126 // FIXME: Also always forward the this pointer in C++ as well.
127
128 for (size_t i = 0; i < Info.DeclRefs.size(); ++i)
129 CGF->AllocateBlockDecl(Info.DeclRefs[i]);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000130}
131
Mike Stump58a85142009-03-04 22:48:06 +0000132// FIXME: Push most into CGM, passing down a few bits, like current function
133// name.
Mike Stump8a2b4b12009-02-25 23:33:13 +0000134llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Mike Stumpe5fee252009-02-13 16:19:19 +0000135
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000136 std::string Name = CurFn->getName();
137 CodeGenFunction::BlockInfo Info(0, Name.c_str());
Mike Stump38e16272009-10-21 22:01:24 +0000138 llvm::SmallSet<const DeclContext *, 16> InnerContexts;
139 InnerContexts.insert(BE->getBlockDecl());
140 CollectBlockDeclRefInfo(BE->getBody(), Info, InnerContexts);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000141
142 // Check if the block can be global.
Mike Stump58a85142009-03-04 22:48:06 +0000143 // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like
144 // to just have one code path. We should move this function into CGM and pass
145 // CGF, then we can just check to see if CGF is 0.
Mike Stumpdab514f2009-03-04 03:23:46 +0000146 if (0 && CanBlockBeGlobal(Info))
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000147 return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
Mike Stump00470a12009-03-05 08:32:30 +0000148
David Chisnall5e530af2009-11-17 19:33:30 +0000149 size_t BlockFields = 5;
150
151 bool hasIntrospection = CGM.getContext().getLangOptions().BlockIntrospection;
152
153 if (hasIntrospection) {
154 BlockFields++;
155 }
156 std::vector<llvm::Constant*> Elts(BlockFields);
157
158 if (hasIntrospection) {
159 std::string BlockTypeEncoding;
160 CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
161
162 Elts[5] = llvm::ConstantExpr::getBitCast(
163 CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty);
164 }
165
Mike Stumpe5fee252009-02-13 16:19:19 +0000166 llvm::Constant *C;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000167 llvm::Value *V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000168
Mike Stumpe5fee252009-02-13 16:19:19 +0000169 {
170 // C = BuildBlockStructInitlist();
171 unsigned int flags = BLOCK_HAS_DESCRIPTOR;
172
David Chisnall5e530af2009-11-17 19:33:30 +0000173 if (hasIntrospection)
174 flags |= BLOCK_HAS_OBJC_TYPE;
175
Mike Stump00470a12009-03-05 08:32:30 +0000176 // We run this first so that we set BlockHasCopyDispose from the entire
177 // block literal.
178 // __invoke
Ken Dyck199c3d62010-01-11 17:06:35 +0000179 CharUnits subBlockSize;
Ken Dyck30a8a272010-01-26 19:13:33 +0000180 CharUnits subBlockAlign;
Mike Stump00470a12009-03-05 08:32:30 +0000181 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Mike Stumpa803b0e2009-03-25 17:58:24 +0000182 bool subBlockHasCopyDispose = false;
Mike Stump00470a12009-03-05 08:32:30 +0000183 llvm::Function *Fn
Mike Stumpbb304192009-09-24 22:31:14 +0000184 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl,
185 LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000186 subBlockSize,
Mike Stump00470a12009-03-05 08:32:30 +0000187 subBlockAlign,
188 subBlockDeclRefDecls,
Mike Stumpa803b0e2009-03-25 17:58:24 +0000189 subBlockHasCopyDispose);
190 BlockHasCopyDispose |= subBlockHasCopyDispose;
Mike Stump00470a12009-03-05 08:32:30 +0000191 Elts[3] = Fn;
192
Mike Stumpf5408fe2009-05-16 07:57:57 +0000193 // FIXME: Don't use BlockHasCopyDispose, it is set more often then
194 // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); }
Mike Stumpa803b0e2009-03-25 17:58:24 +0000195 if (subBlockHasCopyDispose)
Mike Stumpe5fee252009-02-13 16:19:19 +0000196 flags |= BLOCK_HAS_COPY_DISPOSE;
197
Mike Stump7d6dc4f2009-02-13 20:17:16 +0000198 // __isa
Mike Stump59c5b112009-02-13 19:29:27 +0000199 C = CGM.getNSConcreteStackBlock();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000200 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
Mike Stump00470a12009-03-05 08:32:30 +0000201 Elts[0] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000202
203 // __flags
204 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
205 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000206 C = llvm::ConstantInt::get(IntTy, flags);
Mike Stump00470a12009-03-05 08:32:30 +0000207 Elts[1] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000208
209 // __reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000210 C = llvm::ConstantInt::get(IntTy, 0);
Mike Stump00470a12009-03-05 08:32:30 +0000211 Elts[2] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000212
Mike Stump8a2b4b12009-02-25 23:33:13 +0000213 if (subBlockDeclRefDecls.size() == 0) {
Mike Stumpcf62d392009-03-06 18:42:23 +0000214 // __descriptor
Mike Stumpea26cb52009-10-21 03:49:08 +0000215 Elts[4] = BuildDescriptorBlockDecl(subBlockHasCopyDispose, subBlockSize,
216 0, 0);
Mike Stumpcf62d392009-03-06 18:42:23 +0000217
Mike Stump5570cfe2009-03-01 20:07:53 +0000218 // Optimize to being a global block.
219 Elts[0] = CGM.getNSConcreteGlobalBlock();
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000220 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
Mike Stump5570cfe2009-03-01 20:07:53 +0000221
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000222 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000223
Owen Anderson1c431b32009-07-08 19:05:04 +0000224 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000225 llvm::GlobalValue::InternalLinkage, C,
226 "__block_holder_tmp_" +
227 llvm::Twine(CGM.getGlobalUniqueCount()));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000228 QualType BPT = BE->getType();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000229 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000230 return C;
231 }
Mike Stump00470a12009-03-05 08:32:30 +0000232
David Chisnall5e530af2009-11-17 19:33:30 +0000233 std::vector<const llvm::Type *> Types(BlockFields+subBlockDeclRefDecls.size());
Mike Stump08920992009-03-07 02:35:30 +0000234 for (int i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000235 Types[i] = Elts[i]->getType();
Mike Stump08920992009-03-07 02:35:30 +0000236 Types[4] = PtrToInt8Ty;
David Chisnall5e530af2009-11-17 19:33:30 +0000237 if (hasIntrospection)
238 Types[5] = PtrToInt8Ty;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000239
Mike Stumpa99038c2009-02-28 09:07:16 +0000240 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
241 const Expr *E = subBlockDeclRefDecls[i];
242 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
243 QualType Ty = E->getType();
Mike Stumpdab514f2009-03-04 03:23:46 +0000244 if (BDRE && BDRE->isByRef()) {
David Chisnall5e530af2009-11-17 19:33:30 +0000245 Types[i+BlockFields] = llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000246 } else
David Chisnall5e530af2009-11-17 19:33:30 +0000247 Types[i+BlockFields] = ConvertType(Ty);
Mike Stumpa99038c2009-02-28 09:07:16 +0000248 }
Mike Stump8a2b4b12009-02-25 23:33:13 +0000249
Owen Anderson47a434f2009-08-05 23:18:46 +0000250 llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000251
252 llvm::AllocaInst *A = CreateTempAlloca(Ty);
Ken Dyck30a8a272010-01-26 19:13:33 +0000253 A->setAlignment(subBlockAlign.getQuantity());
Mike Stump8a2b4b12009-02-25 23:33:13 +0000254 V = A;
255
Mike Stump08920992009-03-07 02:35:30 +0000256 std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size());
257 int helpersize = 0;
258
259 for (unsigned i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000260 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
David Chisnall5e530af2009-11-17 19:33:30 +0000261 if (hasIntrospection)
262 Builder.CreateStore(Elts[5], Builder.CreateStructGEP(V, 5, "block.tmp"));
Mike Stump00470a12009-03-05 08:32:30 +0000263
Mike Stump8a2b4b12009-02-25 23:33:13 +0000264 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
265 {
Mike Stumpa99038c2009-02-28 09:07:16 +0000266 // FIXME: Push const down.
267 Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
268 DeclRefExpr *DR;
269 ValueDecl *VD;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000270
Mike Stumpa99038c2009-02-28 09:07:16 +0000271 DR = dyn_cast<DeclRefExpr>(E);
272 // Skip padding.
273 if (DR) continue;
274
275 BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
276 VD = BDRE->getDecl();
277
David Chisnall5e530af2009-11-17 19:33:30 +0000278 llvm::Value* Addr = Builder.CreateStructGEP(V, i+BlockFields, "tmp");
Mike Stump08920992009-03-07 02:35:30 +0000279 NoteForHelper[helpersize].index = i+5;
Mike Stump39605b42009-09-22 02:12:52 +0000280 NoteForHelper[helpersize].RequiresCopying
281 = BlockRequiresCopying(VD->getType());
Mike Stump08920992009-03-07 02:35:30 +0000282 NoteForHelper[helpersize].flag
Mike Stump39605b42009-09-22 02:12:52 +0000283 = (VD->getType()->isBlockPointerType()
284 ? BLOCK_FIELD_IS_BLOCK
285 : BLOCK_FIELD_IS_OBJECT);
Mike Stump08920992009-03-07 02:35:30 +0000286
Mike Stumpa99038c2009-02-28 09:07:16 +0000287 if (LocalDeclMap[VD]) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000288 if (BDRE->isByRef()) {
Mike Stump08920992009-03-07 02:35:30 +0000289 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
Mike Stumpf4bc3122009-03-07 06:04:31 +0000290 // FIXME: Someone double check this.
291 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000292 llvm::Value *Loc = LocalDeclMap[VD];
293 Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000294 Loc = Builder.CreateLoad(Loc);
Mike Stumpdab514f2009-03-04 03:23:46 +0000295 Builder.CreateStore(Loc, Addr);
Mike Stump08920992009-03-07 02:35:30 +0000296 ++helpersize;
Mike Stumpdab514f2009-03-04 03:23:46 +0000297 continue;
298 } else
John McCalldbd872f2009-12-08 09:08:17 +0000299 E = new (getContext()) DeclRefExpr (VD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000300 VD->getType(),
301 SourceLocation());
Mike Stumpa99038c2009-02-28 09:07:16 +0000302 }
Mike Stumpdab514f2009-03-04 03:23:46 +0000303 if (BDRE->isByRef()) {
Mike Stumpa8b60c92009-03-21 21:00:35 +0000304 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
305 // FIXME: Someone double check this.
306 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpa99038c2009-02-28 09:07:16 +0000307 E = new (getContext())
308 UnaryOperator(E, UnaryOperator::AddrOf,
309 getContext().getPointerType(E->getType()),
310 SourceLocation());
Mike Stumpdab514f2009-03-04 03:23:46 +0000311 }
Mike Stump08920992009-03-07 02:35:30 +0000312 ++helpersize;
Mike Stumpa99038c2009-02-28 09:07:16 +0000313
Mike Stumpa99038c2009-02-28 09:07:16 +0000314 RValue r = EmitAnyExpr(E, Addr, false);
Mike Stumpdab514f2009-03-04 03:23:46 +0000315 if (r.isScalar()) {
316 llvm::Value *Loc = r.getScalarVal();
David Chisnall5e530af2009-11-17 19:33:30 +0000317 const llvm::Type *Ty = Types[i+BlockFields];
Mike Stumpdab514f2009-03-04 03:23:46 +0000318 if (BDRE->isByRef()) {
Mike Stump58a85142009-03-04 22:48:06 +0000319 // E is now the address of the value field, instead, we want the
320 // address of the actual ByRef struct. We optimize this slightly
321 // compared to gcc by not grabbing the forwarding slot as this must
322 // be done during Block_copy for us, and we can postpone the work
323 // until then.
Ken Dyck199c3d62010-01-11 17:06:35 +0000324 CharUnits offset = BlockDecls[BDRE->getDecl()];
Mike Stump00470a12009-03-05 08:32:30 +0000325
Mike Stump58a85142009-03-04 22:48:06 +0000326 llvm::Value *BlockLiteral = LoadBlockStruct();
Mike Stump00470a12009-03-05 08:32:30 +0000327
Mike Stump58a85142009-03-04 22:48:06 +0000328 Loc = Builder.CreateGEP(BlockLiteral,
Mike Stumpea26cb52009-10-21 03:49:08 +0000329 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Ken Dyck199c3d62010-01-11 17:06:35 +0000330 offset.getQuantity()),
Mike Stump58a85142009-03-04 22:48:06 +0000331 "block.literal");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000332 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000333 Loc = Builder.CreateBitCast(Loc, Ty);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000334 Loc = Builder.CreateLoad(Loc);
Mike Stump58a85142009-03-04 22:48:06 +0000335 // Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000336 }
337 Builder.CreateStore(Loc, Addr);
338 } else if (r.isComplex())
Mike Stump8a2b4b12009-02-25 23:33:13 +0000339 // FIXME: implement
340 ErrorUnsupported(BE, "complex in block literal");
341 else if (r.isAggregate())
342 ; // Already created into the destination
343 else
344 assert (0 && "bad block variable");
345 // FIXME: Ensure that the offset created by the backend for
346 // the struct matches the previously computed offset in BlockDecls.
347 }
Mike Stump08920992009-03-07 02:35:30 +0000348 NoteForHelper.resize(helpersize);
Mike Stumpcf62d392009-03-06 18:42:23 +0000349
350 // __descriptor
Mike Stumpa803b0e2009-03-25 17:58:24 +0000351 llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockHasCopyDispose,
352 subBlockSize, Ty,
Mike Stump08920992009-03-07 02:35:30 +0000353 &NoteForHelper);
354 Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
355 Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
Mike Stumpe5fee252009-02-13 16:19:19 +0000356 }
Mike Stump00470a12009-03-05 08:32:30 +0000357
Mike Stumpbd65cac2009-02-19 01:01:04 +0000358 QualType BPT = BE->getType();
Fariborz Jahanian263c4de2010-02-10 23:34:57 +0000359 V = Builder.CreateBitCast(V, ConvertType(BPT));
360 // See if this is a __weak block variable and the must call objc_read_weak
361 // on it.
362 const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>();
363 QualType RES = ftype->getResultType();
364 if (RES.isObjCGCWeak()) {
365 // Must cast argument to id*
366 const llvm::Type *ObjectPtrTy =
367 ConvertType(CGM.getContext().getObjCIdType());
368 const llvm::Type *PtrObjectPtrTy =
369 llvm::PointerType::getUnqual(ObjectPtrTy);
370 V = Builder.CreateBitCast(V, PtrObjectPtrTy);
371 V = CGM.getObjCRuntime().EmitObjCWeakRead(*this, V);
372 }
373 return V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000374}
375
376
Mike Stump2a998142009-03-04 18:17:45 +0000377const llvm::Type *BlockModule::getBlockDescriptorType() {
Mike Stumpab695142009-02-13 15:16:56 +0000378 if (BlockDescriptorType)
379 return BlockDescriptorType;
380
Mike Stumpa5448542009-02-13 15:32:32 +0000381 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000382 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000383
Mike Stumpab695142009-02-13 15:16:56 +0000384 // struct __block_descriptor {
385 // unsigned long reserved;
386 // unsigned long block_size;
387 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000388 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(),
389 UnsignedLongTy,
Mike Stumpa5448542009-02-13 15:32:32 +0000390 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000391 NULL);
392
393 getModule().addTypeName("struct.__block_descriptor",
394 BlockDescriptorType);
395
396 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000397}
398
Mike Stump2a998142009-03-04 18:17:45 +0000399const llvm::Type *BlockModule::getGenericBlockLiteralType() {
Mike Stump9b8a7972009-02-13 15:25:34 +0000400 if (GenericBlockLiteralType)
401 return GenericBlockLiteralType;
402
Mike Stumpa5448542009-02-13 15:32:32 +0000403 const llvm::Type *BlockDescPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000404 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000405
Mike Stump7cbb3602009-02-13 16:01:35 +0000406 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
407 getTypes().ConvertType(getContext().IntTy));
408
Mike Stump9b8a7972009-02-13 15:25:34 +0000409 // struct __block_literal_generic {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000410 // void *__isa;
411 // int __flags;
412 // int __reserved;
413 // void (*__invoke)(void *);
414 // struct __block_descriptor *__descriptor;
David Chisnall5e530af2009-11-17 19:33:30 +0000415 // // GNU runtime only:
416 // const char *types;
Mike Stump9b8a7972009-02-13 15:25:34 +0000417 // };
David Chisnall5e530af2009-11-17 19:33:30 +0000418 if (CGM.getContext().getLangOptions().BlockIntrospection)
419 GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
420 PtrToInt8Ty,
421 IntTy,
422 IntTy,
423 PtrToInt8Ty,
424 BlockDescPtrTy,
425 PtrToInt8Ty,
426 NULL);
427 else
428 GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
Owen Anderson47a434f2009-08-05 23:18:46 +0000429 PtrToInt8Ty,
Mike Stump7cbb3602009-02-13 16:01:35 +0000430 IntTy,
431 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000432 PtrToInt8Ty,
Mike Stump9b8a7972009-02-13 15:25:34 +0000433 BlockDescPtrTy,
434 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000435
Mike Stump9b8a7972009-02-13 15:25:34 +0000436 getModule().addTypeName("struct.__block_literal_generic",
437 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000438
Mike Stump9b8a7972009-02-13 15:25:34 +0000439 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000440}
441
Mike Stump2a998142009-03-04 18:17:45 +0000442const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000443 if (GenericExtendedBlockLiteralType)
444 return GenericExtendedBlockLiteralType;
445
Mike Stumpbd65cac2009-02-19 01:01:04 +0000446 const llvm::Type *BlockDescPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000447 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpbd65cac2009-02-19 01:01:04 +0000448
449 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
450 getTypes().ConvertType(getContext().IntTy));
451
452 // struct __block_literal_generic {
453 // void *__isa;
454 // int __flags;
455 // int __reserved;
456 // void (*__invoke)(void *);
457 // struct __block_descriptor *__descriptor;
458 // void *__copy_func_helper_decl;
459 // void *__destroy_func_decl;
460 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000461 GenericExtendedBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
462 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000463 IntTy,
464 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000465 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000466 BlockDescPtrTy,
Mike Stump797b6322009-03-05 01:23:13 +0000467 PtrToInt8Ty,
468 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000469 NULL);
470
471 getModule().addTypeName("struct.__block_literal_extended_generic",
472 GenericExtendedBlockLiteralType);
473
474 return GenericExtendedBlockLiteralType;
475}
476
Anders Carlssona1736c02009-12-24 21:13:40 +0000477RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E,
478 ReturnValueSlot ReturnValue) {
Mike Stumpa5448542009-02-13 15:32:32 +0000479 const BlockPointerType *BPT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000480 E->getCallee()->getType()->getAs<BlockPointerType>();
Mike Stumpa5448542009-02-13 15:32:32 +0000481
Anders Carlssonacfde802009-02-12 00:39:25 +0000482 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
483
484 // Get a pointer to the generic block literal.
485 const llvm::Type *BlockLiteralTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000486 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000487
488 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000489 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000490 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
491
492 // Get the function pointer from the literal.
493 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Anders Carlssonacfde802009-02-12 00:39:25 +0000494
Mike Stumpa5448542009-02-13 15:32:32 +0000495 BlockLiteral =
496 Builder.CreateBitCast(BlockLiteral,
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000497 llvm::Type::getInt8PtrTy(VMContext),
Anders Carlssonacfde802009-02-12 00:39:25 +0000498 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000499
Anders Carlssonacfde802009-02-12 00:39:25 +0000500 // Add the block literal.
501 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
502 CallArgList Args;
503 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000504
Anders Carlsson782f3972009-04-08 23:13:16 +0000505 QualType FnType = BPT->getPointeeType();
506
Anders Carlssonacfde802009-02-12 00:39:25 +0000507 // And the rest of the arguments.
John McCall183700f2009-09-21 23:43:11 +0000508 EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(),
Anders Carlsson782f3972009-04-08 23:13:16 +0000509 E->arg_begin(), E->arg_end());
Mike Stumpa5448542009-02-13 15:32:32 +0000510
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000511 // Load the function.
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000512 llvm::Value *Func = Builder.CreateLoad(FuncPtr, "tmp");
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000513
John McCall04a67a62010-02-05 21:31:56 +0000514 const FunctionType *FuncTy = FnType->getAs<FunctionType>();
515 QualType ResultType = FuncTy->getResultType();
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000516
Mike Stump1eb44332009-09-09 15:08:12 +0000517 const CGFunctionInfo &FnInfo =
John McCall04a67a62010-02-05 21:31:56 +0000518 CGM.getTypes().getFunctionInfo(ResultType, Args, FuncTy->getCallConv(),
519 FuncTy->getNoReturnAttr());
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000521 // Cast the function pointer to the right type.
Mike Stump1eb44332009-09-09 15:08:12 +0000522 const llvm::Type *BlockFTy =
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000523 CGM.getTypes().GetFunctionType(FnInfo, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Owen Anderson96e0fc72009-07-29 22:16:19 +0000525 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000526 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Anders Carlssonacfde802009-02-12 00:39:25 +0000528 // And call the block.
Anders Carlssona1736c02009-12-24 21:13:40 +0000529 return EmitCall(FnInfo, Func, ReturnValue, Args);
Anders Carlssonacfde802009-02-12 00:39:25 +0000530}
Anders Carlssond5cab542009-02-12 17:55:02 +0000531
Ken Dyck199c3d62010-01-11 17:06:35 +0000532CharUnits CodeGenFunction::AllocateBlockDecl(const BlockDeclRefExpr *E) {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000533 const ValueDecl *VD = E->getDecl();
Ken Dyck199c3d62010-01-11 17:06:35 +0000534 CharUnits &offset = BlockDecls[VD];
Mike Stumpdab514f2009-03-04 03:23:46 +0000535
Mike Stumpdab514f2009-03-04 03:23:46 +0000536 // See if we have already allocated an offset for this variable.
Ken Dyck199c3d62010-01-11 17:06:35 +0000537 if (offset.isPositive())
Mike Stumpea26cb52009-10-21 03:49:08 +0000538 return offset;
539
540 // Don't run the expensive check, unless we have to.
Mike Stump083c25e2009-10-22 00:49:09 +0000541 if (!BlockHasCopyDispose)
542 if (E->isByRef()
543 || BlockRequiresCopying(E->getType()))
544 BlockHasCopyDispose = true;
Mike Stumpea26cb52009-10-21 03:49:08 +0000545
546 // if not, allocate one now.
547 offset = getBlockOffset(E);
548
549 return offset;
550}
551
552llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
553 const ValueDecl *VD = E->getDecl();
Ken Dyck199c3d62010-01-11 17:06:35 +0000554 CharUnits offset = AllocateBlockDecl(E);
Mike Stumpea26cb52009-10-21 03:49:08 +0000555
Mike Stumpdab514f2009-03-04 03:23:46 +0000556
557 llvm::Value *BlockLiteral = LoadBlockStruct();
558 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
Mike Stumpea26cb52009-10-21 03:49:08 +0000559 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Ken Dyck199c3d62010-01-11 17:06:35 +0000560 offset.getQuantity()),
Mike Stump58a85142009-03-04 22:48:06 +0000561 "block.literal");
Mike Stumpdab514f2009-03-04 03:23:46 +0000562 if (E->isByRef()) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000563 const llvm::Type *PtrStructTy
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000564 = llvm::PointerType::get(BuildByRefType(VD), 0);
Mike Stumpa8b60c92009-03-21 21:00:35 +0000565 // The block literal will need a copy/destroy helper.
566 BlockHasCopyDispose = true;
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000567
568 const llvm::Type *Ty = PtrStructTy;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000569 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000570 V = Builder.CreateBitCast(V, Ty);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000571 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000572 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000573 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000574 V = Builder.CreateBitCast(V, PtrStructTy);
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000575 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
576 VD->getNameAsString());
Mike Stumpdab514f2009-03-04 03:23:46 +0000577 } else {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000578 const llvm::Type *Ty = CGM.getTypes().ConvertType(VD->getType());
579
Owen Anderson96e0fc72009-07-29 22:16:19 +0000580 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000581 V = Builder.CreateBitCast(V, Ty);
582 }
583 return V;
584}
585
Mike Stump6cc88f72009-03-20 21:53:12 +0000586void CodeGenFunction::BlockForwardSelf() {
587 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
588 ImplicitParamDecl *SelfDecl = OMD->getSelfDecl();
589 llvm::Value *&DMEntry = LocalDeclMap[SelfDecl];
590 if (DMEntry)
591 return;
592 // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care
593 BlockDeclRefExpr *BDRE = new (getContext())
594 BlockDeclRefExpr(SelfDecl,
595 SelfDecl->getType(), SourceLocation(), false);
596 DMEntry = GetAddrOfBlockDecl(BDRE);
597}
598
Mike Stump67a64482009-02-14 22:16:35 +0000599llvm::Constant *
Mike Stump90a90432009-03-04 18:47:42 +0000600BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000601 // Generate the block descriptor.
602 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000603 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
604 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000605
Anders Carlssond5cab542009-02-12 17:55:02 +0000606 llvm::Constant *DescriptorFields[2];
Mike Stumpa5448542009-02-13 15:32:32 +0000607
Anders Carlssond5cab542009-02-12 17:55:02 +0000608 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000609 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000610
Anders Carlssond5cab542009-02-12 17:55:02 +0000611 // Block literal size. For global blocks we just use the size of the generic
612 // block literal struct.
Ken Dyck687cc4a2010-01-26 13:48:07 +0000613 CharUnits BlockLiteralSize =
614 CGM.GetTargetTypeStoreSize(getGenericBlockLiteralType());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000615 DescriptorFields[1] =
Ken Dyck199c3d62010-01-11 17:06:35 +0000616 llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize.getQuantity());
Mike Stumpa5448542009-02-13 15:32:32 +0000617
618 llvm::Constant *DescriptorStruct =
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000619 llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 2, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000620
Anders Carlssond5cab542009-02-12 17:55:02 +0000621 llvm::GlobalVariable *Descriptor =
Owen Anderson1c431b32009-07-08 19:05:04 +0000622 new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000623 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000624 DescriptorStruct, "__block_descriptor_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000625
David Chisnall5e530af2009-11-17 19:33:30 +0000626 int FieldCount = 5;
Anders Carlssond5cab542009-02-12 17:55:02 +0000627 // Generate the constants for the block literal.
David Chisnall5e530af2009-11-17 19:33:30 +0000628 if (CGM.getContext().getLangOptions().BlockIntrospection)
629 FieldCount = 6;
630
631 std::vector<llvm::Constant*> LiteralFields(FieldCount);
Mike Stumpa5448542009-02-13 15:32:32 +0000632
Mike Stump67a64482009-02-14 22:16:35 +0000633 CodeGenFunction::BlockInfo Info(0, n);
Ken Dyck199c3d62010-01-11 17:06:35 +0000634 CharUnits subBlockSize;
Ken Dyck30a8a272010-01-26 19:13:33 +0000635 CharUnits subBlockAlign;
Mike Stumpa99038c2009-02-28 09:07:16 +0000636 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Daniel Dunbard67b09a2009-03-12 03:07:24 +0000637 bool subBlockHasCopyDispose = false;
Mike Stump7f28a9c2009-03-13 23:34:28 +0000638 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000639 llvm::Function *Fn
Mike Stump6cc88f72009-03-20 21:53:12 +0000640 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000641 subBlockSize,
Mike Stump90a90432009-03-04 18:47:42 +0000642 subBlockAlign,
Mike Stump00470a12009-03-05 08:32:30 +0000643 subBlockDeclRefDecls,
644 subBlockHasCopyDispose);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000645 assert(subBlockSize == BlockLiteralSize
646 && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000647
Anders Carlssond5cab542009-02-12 17:55:02 +0000648 // isa
Mike Stumpf99f1d02009-02-13 17:23:42 +0000649 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000650
Anders Carlssond5cab542009-02-12 17:55:02 +0000651 // Flags
David Chisnall5e530af2009-11-17 19:33:30 +0000652 LiteralFields[1] = CGM.getContext().getLangOptions().BlockIntrospection ?
653 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR |
654 BLOCK_HAS_OBJC_TYPE) :
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000655 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR);
Mike Stumpa5448542009-02-13 15:32:32 +0000656
Anders Carlssond5cab542009-02-12 17:55:02 +0000657 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000658 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000659
Anders Carlssond5cab542009-02-12 17:55:02 +0000660 // Function
661 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000662
Anders Carlssond5cab542009-02-12 17:55:02 +0000663 // Descriptor
664 LiteralFields[4] = Descriptor;
David Chisnall5e530af2009-11-17 19:33:30 +0000665
666 // Type encoding
667 if (CGM.getContext().getLangOptions().BlockIntrospection) {
668 std::string BlockTypeEncoding;
669 CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
670
671 LiteralFields[5] = CGM.GetAddrOfConstantCString(BlockTypeEncoding);
672 }
Mike Stumpa5448542009-02-13 15:32:32 +0000673
674 llvm::Constant *BlockLiteralStruct =
David Chisnall5e530af2009-11-17 19:33:30 +0000675 llvm::ConstantStruct::get(VMContext, LiteralFields, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000676
677 llvm::GlobalVariable *BlockLiteral =
Owen Anderson1c431b32009-07-08 19:05:04 +0000678 new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000679 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000680 BlockLiteralStruct, "__block_literal_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000681
Anders Carlssond5cab542009-02-12 17:55:02 +0000682 return BlockLiteral;
683}
684
Mike Stump4e7a1f72009-02-21 20:00:35 +0000685llvm::Value *CodeGenFunction::LoadBlockStruct() {
Mike Stumpbf1914b2009-10-20 20:30:01 +0000686 llvm::Value *V = Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()],
687 "self");
688 // For now, we codegen based upon byte offsets.
689 return Builder.CreateBitCast(V, PtrToInt8Ty);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000690}
691
Mike Stump00470a12009-03-05 08:32:30 +0000692llvm::Function *
693CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
694 const BlockInfo& Info,
Mike Stump6cc88f72009-03-20 21:53:12 +0000695 const Decl *OuterFuncDecl,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000696 llvm::DenseMap<const Decl*, llvm::Value*> ldm,
Ken Dyck199c3d62010-01-11 17:06:35 +0000697 CharUnits &Size,
Ken Dyck30a8a272010-01-26 19:13:33 +0000698 CharUnits &Align,
Mike Stump00470a12009-03-05 08:32:30 +0000699 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls,
700 bool &subBlockHasCopyDispose) {
Devang Patel963dfbd2009-04-15 21:51:44 +0000701
702 // Check if we should generate debug info for this block.
703 if (CGM.getDebugInfo())
704 DebugInfo = CGM.getDebugInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000705
Mike Stump7f28a9c2009-03-13 23:34:28 +0000706 // Arrange for local static and local extern declarations to appear
707 // to be local to this function as well, as they are directly referenced
708 // in a block.
709 for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
710 i != ldm.end();
711 ++i) {
712 const VarDecl *VD = dyn_cast<VarDecl>(i->first);
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000714 if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage())
Mike Stump7f28a9c2009-03-13 23:34:28 +0000715 LocalDeclMap[VD] = i->second;
716 }
717
Ken Dyck687cc4a2010-01-26 13:48:07 +0000718 BlockOffset =
719 CGM.GetTargetTypeStoreSize(CGM.getGenericBlockLiteralType());
Ken Dyck30a8a272010-01-26 19:13:33 +0000720 BlockAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Eli Friedman48f91222009-03-28 03:24:54 +0000721
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000722 const FunctionType *BlockFunctionType = BExpr->getFunctionType();
723 QualType ResultType;
John McCall04a67a62010-02-05 21:31:56 +0000724 CallingConv CC = BlockFunctionType->getCallConv();
725 bool NoReturn = BlockFunctionType->getNoReturnAttr();
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000726 bool IsVariadic;
Mike Stump1eb44332009-09-09 15:08:12 +0000727 if (const FunctionProtoType *FTy =
Fariborz Jahanianda0895d2009-04-11 18:54:57 +0000728 dyn_cast<FunctionProtoType>(BlockFunctionType)) {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000729 ResultType = FTy->getResultType();
730 IsVariadic = FTy->isVariadic();
Mike Stumpb3589f42009-07-30 22:28:39 +0000731 } else {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000732 // K&R style block.
733 ResultType = BlockFunctionType->getResultType();
734 IsVariadic = false;
735 }
Mike Stumpa5448542009-02-13 15:32:32 +0000736
Anders Carlssond5cab542009-02-12 17:55:02 +0000737 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000738
Mike Stumpea26cb52009-10-21 03:49:08 +0000739 CurFuncDecl = OuterFuncDecl;
740
Chris Lattner161d36d2009-02-28 19:01:03 +0000741 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlssond5cab542009-02-12 17:55:02 +0000742
Mike Stumpea26cb52009-10-21 03:49:08 +0000743 IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor");
Mike Stumpadaaad32009-10-20 02:12:22 +0000744
Mike Stumpbfbd5df2009-10-21 18:24:18 +0000745 // Allocate all BlockDeclRefDecls, so we can calculate the right ParmTy below.
Mike Stump0298d382009-10-21 22:02:08 +0000746 AllocateAllBlockDeclRefs(Info, this);
Mike Stumpea26cb52009-10-21 03:49:08 +0000747
Mike Stump083c25e2009-10-22 00:49:09 +0000748 QualType ParmTy = getContext().getBlockParmType(BlockHasCopyDispose,
749 BlockDeclRefDecls);
Anders Carlssond5cab542009-02-12 17:55:02 +0000750 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000751 ImplicitParamDecl *SelfDecl =
Anders Carlssond5cab542009-02-12 17:55:02 +0000752 ImplicitParamDecl::Create(getContext(), 0,
Mike Stumpadaaad32009-10-20 02:12:22 +0000753 SourceLocation(), II,
754 ParmTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000755
Anders Carlssond5cab542009-02-12 17:55:02 +0000756 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump4e7a1f72009-02-21 20:00:35 +0000757 BlockStructDecl = SelfDecl;
Mike Stumpa5448542009-02-13 15:32:32 +0000758
Steve Naroffe78b8092009-03-13 16:56:44 +0000759 for (BlockDecl::param_const_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000760 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000761 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000762
763 const CGFunctionInfo &FI =
John McCall04a67a62010-02-05 21:31:56 +0000764 CGM.getTypes().getFunctionInfo(ResultType, Args, CC, NoReturn);
Anders Carlssond5cab542009-02-12 17:55:02 +0000765
Anders Carlssond5cab542009-02-12 17:55:02 +0000766 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000767 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic);
Mike Stumpa5448542009-02-13 15:32:32 +0000768
769 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000770 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000771 llvm::Twine("__") + Info.Name + "_block_invoke_",
Anders Carlssond5cab542009-02-12 17:55:02 +0000772 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000773
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000774 CGM.SetInternalFunctionAttributes(BD, Fn, FI);
775
Chris Lattner4863db42009-04-23 07:18:56 +0000776 StartFunction(BD, ResultType, Fn, Args,
Chris Lattner161d36d2009-02-28 19:01:03 +0000777 BExpr->getBody()->getLocEnd());
Mike Stumpb1a6e682009-09-30 02:43:10 +0000778
Chris Lattner4863db42009-04-23 07:18:56 +0000779 CurFuncDecl = OuterFuncDecl;
Chris Lattnerb5437d22009-04-23 05:30:27 +0000780 CurCodeDecl = BD;
Mike Stumpb289b3f2009-10-01 22:29:41 +0000781
782 // Save a spot to insert the debug information for all the BlockDeclRefDecls.
783 llvm::BasicBlock *entry = Builder.GetInsertBlock();
784 llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint();
785 --entry_ptr;
786
Chris Lattner161d36d2009-02-28 19:01:03 +0000787 EmitStmt(BExpr->getBody());
Mike Stumpb289b3f2009-10-01 22:29:41 +0000788
Mike Stumpde8c5c72009-10-01 00:27:30 +0000789 // Remember where we were...
790 llvm::BasicBlock *resume = Builder.GetInsertBlock();
Mike Stumpb289b3f2009-10-01 22:29:41 +0000791
Mike Stumpde8c5c72009-10-01 00:27:30 +0000792 // Go back to the entry.
Mike Stumpb289b3f2009-10-01 22:29:41 +0000793 ++entry_ptr;
794 Builder.SetInsertPoint(entry, entry_ptr);
795
Mike Stumpb1a6e682009-09-30 02:43:10 +0000796 if (CGDebugInfo *DI = getDebugInfo()) {
Mike Stumpb1a6e682009-09-30 02:43:10 +0000797 // Emit debug information for all the BlockDeclRefDecls.
Chris Lattner14b1a362010-01-25 03:29:35 +0000798 for (unsigned i = 0, e = BlockDeclRefDecls.size(); i != e; ++i) {
799 if (const BlockDeclRefExpr *BDRE =
800 dyn_cast<BlockDeclRefExpr>(BlockDeclRefDecls[i])) {
Mike Stumpb1a6e682009-09-30 02:43:10 +0000801 const ValueDecl *D = BDRE->getDecl();
802 DI->setLocation(D->getLocation());
803 DI->EmitDeclareOfBlockDeclRefVariable(BDRE,
804 LocalDeclMap[getBlockStructDecl()],
805 Builder, this);
806 }
807 }
Mike Stumpb1a6e682009-09-30 02:43:10 +0000808 }
Mike Stumpde8c5c72009-10-01 00:27:30 +0000809 // And resume where we left off.
810 if (resume == 0)
811 Builder.ClearInsertionPoint();
812 else
813 Builder.SetInsertPoint(resume);
Mike Stumpb1a6e682009-09-30 02:43:10 +0000814
Chris Lattner161d36d2009-02-28 19:01:03 +0000815 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlssond5cab542009-02-12 17:55:02 +0000816
Mike Stump8a2b4b12009-02-25 23:33:13 +0000817 // The runtime needs a minimum alignment of a void *.
Ken Dyck30a8a272010-01-26 19:13:33 +0000818 CharUnits MinAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Ken Dyck199c3d62010-01-11 17:06:35 +0000819 BlockOffset = CharUnits::fromQuantity(
Ken Dyck30a8a272010-01-26 19:13:33 +0000820 llvm::RoundUpToAlignment(BlockOffset.getQuantity(),
821 MinAlign.getQuantity()));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000822
Mike Stump4e7a1f72009-02-21 20:00:35 +0000823 Size = BlockOffset;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000824 Align = BlockAlign;
825 subBlockDeclRefDecls = BlockDeclRefDecls;
Mike Stump00470a12009-03-05 08:32:30 +0000826 subBlockHasCopyDispose |= BlockHasCopyDispose;
Anders Carlssond5cab542009-02-12 17:55:02 +0000827 return Fn;
828}
Mike Stumpa99038c2009-02-28 09:07:16 +0000829
Ken Dyck199c3d62010-01-11 17:06:35 +0000830CharUnits BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
Mike Stumpa99038c2009-02-28 09:07:16 +0000831 const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
832
Ken Dyck199c3d62010-01-11 17:06:35 +0000833 CharUnits Size = getContext().getTypeSizeInChars(D->getType());
Ken Dyck8b752f12010-01-27 17:10:57 +0000834 CharUnits Align = getContext().getDeclAlign(D);
Mike Stumpa99038c2009-02-28 09:07:16 +0000835
836 if (BDRE->isByRef()) {
Ken Dyck199c3d62010-01-11 17:06:35 +0000837 Size = getContext().getTypeSizeInChars(getContext().VoidPtrTy);
Ken Dyck30a8a272010-01-26 19:13:33 +0000838 Align = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Mike Stumpa99038c2009-02-28 09:07:16 +0000839 }
840
Ken Dyck30a8a272010-01-26 19:13:33 +0000841 assert ((Align.isPositive()) && "alignment must be 1 byte or more");
Mike Stumpa99038c2009-02-28 09:07:16 +0000842
Ken Dyck199c3d62010-01-11 17:06:35 +0000843 CharUnits OldOffset = BlockOffset;
Mike Stumpa99038c2009-02-28 09:07:16 +0000844
845 // Ensure proper alignment, even if it means we have to have a gap
Ken Dyck199c3d62010-01-11 17:06:35 +0000846 BlockOffset = CharUnits::fromQuantity(
Ken Dyck30a8a272010-01-26 19:13:33 +0000847 llvm::RoundUpToAlignment(BlockOffset.getQuantity(), Align.getQuantity()));
Mike Stumpa99038c2009-02-28 09:07:16 +0000848 BlockAlign = std::max(Align, BlockAlign);
Mike Stump00470a12009-03-05 08:32:30 +0000849
Ken Dyck199c3d62010-01-11 17:06:35 +0000850 CharUnits Pad = BlockOffset - OldOffset;
851 if (Pad.isPositive()) {
852 llvm::ArrayType::get(llvm::Type::getInt8Ty(VMContext), Pad.getQuantity());
Mike Stumpa99038c2009-02-28 09:07:16 +0000853 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
Ken Dyck199c3d62010-01-11 17:06:35 +0000854 llvm::APInt(32,
855 Pad.getQuantity()),
Mike Stumpa99038c2009-02-28 09:07:16 +0000856 ArrayType::Normal, 0);
857 ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000858 0, QualType(PadTy), 0, VarDecl::None);
Mike Stumpa99038c2009-02-28 09:07:16 +0000859 Expr *E;
860 E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
Douglas Gregor0da76df2009-11-23 11:41:28 +0000861 SourceLocation());
Mike Stumpa99038c2009-02-28 09:07:16 +0000862 BlockDeclRefDecls.push_back(E);
863 }
864 BlockDeclRefDecls.push_back(BDRE);
865
866 BlockOffset += Size;
867 return BlockOffset-Size;
868}
Mike Stumpdab514f2009-03-04 03:23:46 +0000869
Mike Stump08920992009-03-07 02:35:30 +0000870llvm::Constant *BlockFunction::
871GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000872 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000873 QualType R = getContext().VoidTy;
874
875 FunctionArgList Args;
876 // FIXME: This leaks
Mike Stump08920992009-03-07 02:35:30 +0000877 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +0000878 ImplicitParamDecl::Create(getContext(), 0,
879 SourceLocation(), 0,
Mike Stump08920992009-03-07 02:35:30 +0000880 getContext().getPointerType(getContext().VoidTy));
881 Args.push_back(std::make_pair(Dst, Dst->getType()));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000882 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +0000883 ImplicitParamDecl::Create(getContext(), 0,
884 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000885 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000886 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Mike Stumpa4f668f2009-03-06 01:33:24 +0000888 const CGFunctionInfo &FI =
John McCall04a67a62010-02-05 21:31:56 +0000889 CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000890
Mike Stump3899a7f2009-06-05 23:26:36 +0000891 // FIXME: We'd like to put these into a mergable by content, with
892 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000893 CodeGenTypes &Types = CGM.getTypes();
894 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
895
896 llvm::Function *Fn =
897 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000898 "__copy_helper_block_", &CGM.getModule());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000899
900 IdentifierInfo *II
901 = &CGM.getContext().Idents.get("__copy_helper_block_");
902
903 FunctionDecl *FD = FunctionDecl::Create(getContext(),
904 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000905 SourceLocation(), II, R, 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000906 FunctionDecl::Static, false,
907 true);
908 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump08920992009-03-07 02:35:30 +0000909
910 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
911 llvm::Type *PtrPtrT;
Mike Stump08920992009-03-07 02:35:30 +0000912
Mike Stumpb7477cf2009-04-10 18:52:28 +0000913 if (NoteForHelperp) {
914 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump08920992009-03-07 02:35:30 +0000915
Owen Anderson96e0fc72009-07-29 22:16:19 +0000916 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000917 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
918 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump08920992009-03-07 02:35:30 +0000919
Mike Stumpb7477cf2009-04-10 18:52:28 +0000920 llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000921 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000922 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000923 DstObj = Builder.CreateBitCast(DstObj, PtrPtrT);
924 DstObj = Builder.CreateLoad(DstObj);
Mike Stump08920992009-03-07 02:35:30 +0000925
Mike Stumpb7477cf2009-04-10 18:52:28 +0000926 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
927 int flag = NoteForHelper[i].flag;
928 int index = NoteForHelper[i].index;
Mike Stump08920992009-03-07 02:35:30 +0000929
Mike Stumpb7477cf2009-04-10 18:52:28 +0000930 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
931 || NoteForHelper[i].RequiresCopying) {
932 llvm::Value *Srcv = SrcObj;
933 Srcv = Builder.CreateStructGEP(Srcv, index);
934 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000935 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000936 Srcv = Builder.CreateLoad(Srcv);
937
938 llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
939 Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
940
Owen Anderson0032b272009-08-13 21:57:51 +0000941 llvm::Value *N = llvm::ConstantInt::get(
942 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000943 llvm::Value *F = getBlockObjectAssign();
944 Builder.CreateCall3(F, Dstv, Srcv, N);
945 }
Mike Stump08920992009-03-07 02:35:30 +0000946 }
947 }
948
Mike Stumpa4f668f2009-03-06 01:33:24 +0000949 CGF.FinishFunction();
950
Owen Anderson3c4972d2009-07-29 18:54:39 +0000951 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000952}
953
Mike Stumpcf62d392009-03-06 18:42:23 +0000954llvm::Constant *BlockFunction::
Mike Stump08920992009-03-07 02:35:30 +0000955GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
956 const llvm::StructType* T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000957 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000958 QualType R = getContext().VoidTy;
959
960 FunctionArgList Args;
961 // FIXME: This leaks
962 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +0000963 ImplicitParamDecl::Create(getContext(), 0,
964 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000965 getContext().getPointerType(getContext().VoidTy));
966
967 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000968
Mike Stumpa4f668f2009-03-06 01:33:24 +0000969 const CGFunctionInfo &FI =
John McCall04a67a62010-02-05 21:31:56 +0000970 CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000971
Mike Stump3899a7f2009-06-05 23:26:36 +0000972 // FIXME: We'd like to put these into a mergable by content, with
973 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000974 CodeGenTypes &Types = CGM.getTypes();
975 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
976
977 llvm::Function *Fn =
978 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000979 "__destroy_helper_block_", &CGM.getModule());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000980
981 IdentifierInfo *II
982 = &CGM.getContext().Idents.get("__destroy_helper_block_");
983
984 FunctionDecl *FD = FunctionDecl::Create(getContext(),
985 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000986 SourceLocation(), II, R, 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000987 FunctionDecl::Static, false,
988 true);
989 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1edf6b62009-03-07 02:53:18 +0000990
Mike Stumpb7477cf2009-04-10 18:52:28 +0000991 if (NoteForHelperp) {
992 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump1edf6b62009-03-07 02:53:18 +0000993
Mike Stumpb7477cf2009-04-10 18:52:28 +0000994 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
995 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000996 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000997 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
998 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump1edf6b62009-03-07 02:53:18 +0000999
Mike Stumpb7477cf2009-04-10 18:52:28 +00001000 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
1001 int flag = NoteForHelper[i].flag;
1002 int index = NoteForHelper[i].index;
Mike Stump1edf6b62009-03-07 02:53:18 +00001003
Mike Stumpb7477cf2009-04-10 18:52:28 +00001004 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
1005 || NoteForHelper[i].RequiresCopying) {
1006 llvm::Value *Srcv = SrcObj;
1007 Srcv = Builder.CreateStructGEP(Srcv, index);
1008 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001009 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +00001010 Srcv = Builder.CreateLoad(Srcv);
1011
1012 BuildBlockRelease(Srcv, flag);
1013 }
Mike Stump1edf6b62009-03-07 02:53:18 +00001014 }
1015 }
1016
Mike Stumpa4f668f2009-03-06 01:33:24 +00001017 CGF.FinishFunction();
1018
Owen Anderson3c4972d2009-07-29 18:54:39 +00001019 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpa4f668f2009-03-06 01:33:24 +00001020}
1021
Mike Stump08920992009-03-07 02:35:30 +00001022llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001023 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump08920992009-03-07 02:35:30 +00001024 return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
1025 T, NoteForHelper);
Mike Stumpa4f668f2009-03-06 01:33:24 +00001026}
1027
Mike Stump08920992009-03-07 02:35:30 +00001028llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001029 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stump08920992009-03-07 02:35:30 +00001030 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001031 T, NoteForHelperp);
Mike Stumpdab514f2009-03-04 03:23:46 +00001032}
Mike Stump797b6322009-03-05 01:23:13 +00001033
Mike Stumpee094222009-03-06 06:12:24 +00001034llvm::Constant *BlockFunction::
1035GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
Mike Stump45031c02009-03-06 02:29:21 +00001036 QualType R = getContext().VoidTy;
1037
1038 FunctionArgList Args;
1039 // FIXME: This leaks
Mike Stumpee094222009-03-06 06:12:24 +00001040 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +00001041 ImplicitParamDecl::Create(getContext(), 0,
1042 SourceLocation(), 0,
Mike Stumpee094222009-03-06 06:12:24 +00001043 getContext().getPointerType(getContext().VoidTy));
1044 Args.push_back(std::make_pair(Dst, Dst->getType()));
1045
1046 // FIXME: This leaks
Mike Stump45031c02009-03-06 02:29:21 +00001047 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001048 ImplicitParamDecl::Create(getContext(), 0,
1049 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001050 getContext().getPointerType(getContext().VoidTy));
Mike Stump45031c02009-03-06 02:29:21 +00001051 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Mike Stump45031c02009-03-06 02:29:21 +00001053 const CGFunctionInfo &FI =
John McCall04a67a62010-02-05 21:31:56 +00001054 CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false);
Mike Stump45031c02009-03-06 02:29:21 +00001055
Mike Stump45031c02009-03-06 02:29:21 +00001056 CodeGenTypes &Types = CGM.getTypes();
1057 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1058
Mike Stump3899a7f2009-06-05 23:26:36 +00001059 // FIXME: We'd like to put these into a mergable by content, with
1060 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001061 llvm::Function *Fn =
1062 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001063 "__Block_byref_id_object_copy_", &CGM.getModule());
Mike Stump45031c02009-03-06 02:29:21 +00001064
1065 IdentifierInfo *II
1066 = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
1067
1068 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1069 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001070 SourceLocation(), II, R, 0,
Mike Stump45031c02009-03-06 02:29:21 +00001071 FunctionDecl::Static, false,
1072 true);
1073 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpee094222009-03-06 06:12:24 +00001074
1075 // dst->x
1076 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001077 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001078 V = Builder.CreateLoad(V);
Mike Stumpee094222009-03-06 06:12:24 +00001079 V = Builder.CreateStructGEP(V, 6, "x");
1080 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
1081
1082 // src->x
1083 V = CGF.GetAddrOfLocalVar(Src);
1084 V = Builder.CreateLoad(V);
1085 V = Builder.CreateBitCast(V, T);
1086 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001087 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpee094222009-03-06 06:12:24 +00001088 llvm::Value *SrcObj = Builder.CreateLoad(V);
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Mike Stumpee094222009-03-06 06:12:24 +00001090 flag |= BLOCK_BYREF_CALLER;
1091
Owen Anderson0032b272009-08-13 21:57:51 +00001092 llvm::Value *N = llvm::ConstantInt::get(
1093 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpee094222009-03-06 06:12:24 +00001094 llvm::Value *F = getBlockObjectAssign();
1095 Builder.CreateCall3(F, DstObj, SrcObj, N);
1096
Mike Stump45031c02009-03-06 02:29:21 +00001097 CGF.FinishFunction();
1098
Owen Anderson3c4972d2009-07-29 18:54:39 +00001099 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001100}
1101
Mike Stump1851b682009-03-06 04:53:30 +00001102llvm::Constant *
1103BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
1104 int flag) {
Mike Stump45031c02009-03-06 02:29:21 +00001105 QualType R = getContext().VoidTy;
1106
1107 FunctionArgList Args;
1108 // FIXME: This leaks
1109 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001110 ImplicitParamDecl::Create(getContext(), 0,
1111 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001112 getContext().getPointerType(getContext().VoidTy));
1113
1114 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001115
Mike Stump45031c02009-03-06 02:29:21 +00001116 const CGFunctionInfo &FI =
John McCall04a67a62010-02-05 21:31:56 +00001117 CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false);
Mike Stump45031c02009-03-06 02:29:21 +00001118
Mike Stump45031c02009-03-06 02:29:21 +00001119 CodeGenTypes &Types = CGM.getTypes();
1120 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1121
Mike Stump3899a7f2009-06-05 23:26:36 +00001122 // FIXME: We'd like to put these into a mergable by content, with
1123 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001124 llvm::Function *Fn =
1125 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001126 "__Block_byref_id_object_dispose_",
Mike Stump45031c02009-03-06 02:29:21 +00001127 &CGM.getModule());
1128
1129 IdentifierInfo *II
1130 = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
1131
1132 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1133 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001134 SourceLocation(), II, R, 0,
Mike Stump45031c02009-03-06 02:29:21 +00001135 FunctionDecl::Static, false,
1136 true);
1137 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1851b682009-03-06 04:53:30 +00001138
1139 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001140 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001141 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001142 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001143 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001144 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001145
Mike Stump1851b682009-03-06 04:53:30 +00001146 flag |= BLOCK_BYREF_CALLER;
1147 BuildBlockRelease(V, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001148 CGF.FinishFunction();
1149
Owen Anderson3c4972d2009-07-29 18:54:39 +00001150 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001151}
1152
Mike Stumpee094222009-03-06 06:12:24 +00001153llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001154 int Flag, unsigned Align) {
1155 // All alignments below that of pointer alignment collapse down to just
Mike Stump3899a7f2009-06-05 23:26:36 +00001156 // pointer alignment, as we always have at least that much alignment to begin
1157 // with.
1158 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001159
Mike Stump3899a7f2009-06-05 23:26:36 +00001160 // As an optimization, we only generate a single function of each kind we
1161 // might need. We need a different one for each alignment and for each
1162 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001163 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1164 llvm::Constant *&Entry = CGM.AssignCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001165 if (Entry)
1166 return Entry;
Chris Lattner10976d92009-12-05 08:21:30 +00001167 return Entry = CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, Flag);
Mike Stump45031c02009-03-06 02:29:21 +00001168}
1169
Mike Stump1851b682009-03-06 04:53:30 +00001170llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001171 int Flag,
Mike Stump3899a7f2009-06-05 23:26:36 +00001172 unsigned Align) {
1173 // All alignments below that of pointer alignment collpase down to just
1174 // pointer alignment, as we always have at least that much alignment to begin
1175 // with.
1176 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001177
Mike Stump3899a7f2009-06-05 23:26:36 +00001178 // As an optimization, we only generate a single function of each kind we
1179 // might need. We need a different one for each alignment and for each
1180 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001181 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1182 llvm::Constant *&Entry = CGM.DestroyCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001183 if (Entry)
1184 return Entry;
Chris Lattner10976d92009-12-05 08:21:30 +00001185 return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, Flag);
Mike Stump45031c02009-03-06 02:29:21 +00001186}
1187
Mike Stump797b6322009-03-05 01:23:13 +00001188llvm::Value *BlockFunction::getBlockObjectDispose() {
1189 if (CGM.BlockObjectDispose == 0) {
1190 const llvm::FunctionType *FTy;
1191 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001192 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stump797b6322009-03-05 01:23:13 +00001193 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 Stump00470a12009-03-05 08:32:30 +00001196 CGM.BlockObjectDispose
Mike Stump797b6322009-03-05 01:23:13 +00001197 = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
1198 }
1199 return CGM.BlockObjectDispose;
1200}
1201
Mike Stumpee094222009-03-06 06:12:24 +00001202llvm::Value *BlockFunction::getBlockObjectAssign() {
1203 if (CGM.BlockObjectAssign == 0) {
1204 const llvm::FunctionType *FTy;
1205 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001206 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stumpee094222009-03-06 06:12:24 +00001207 ArgTys.push_back(PtrToInt8Ty);
1208 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001209 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson96e0fc72009-07-29 22:16:19 +00001210 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stumpee094222009-03-06 06:12:24 +00001211 CGM.BlockObjectAssign
1212 = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
1213 }
1214 return CGM.BlockObjectAssign;
1215}
1216
Mike Stump1851b682009-03-06 04:53:30 +00001217void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Mike Stump797b6322009-03-05 01:23:13 +00001218 llvm::Value *F = getBlockObjectDispose();
Mike Stump1851b682009-03-06 04:53:30 +00001219 llvm::Value *N;
Mike Stump797b6322009-03-05 01:23:13 +00001220 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001221 N = llvm::ConstantInt::get(llvm::Type::getInt32Ty(V->getContext()), flag);
Mike Stump797b6322009-03-05 01:23:13 +00001222 Builder.CreateCall2(F, V, N);
1223}
Mike Stump00470a12009-03-05 08:32:30 +00001224
1225ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stump08920992009-03-07 02:35:30 +00001226
1227BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1228 CGBuilderTy &B)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001229 : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) {
Owen Anderson0032b272009-08-13 21:57:51 +00001230 PtrToInt8Ty = llvm::PointerType::getUnqual(
1231 llvm::Type::getInt8Ty(VMContext));
Mike Stump08920992009-03-07 02:35:30 +00001232
1233 BlockHasCopyDispose = false;
1234}