blob: a2ab883432e0d91437adfa1a4dfa67243fda90b9 [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"
Benjamin Kramer6876fe62010-03-31 15:04:05 +000020#include "llvm/ADT/SmallSet.h"
Anders Carlssond5cab542009-02-12 17:55:02 +000021#include "llvm/Target/TargetData.h"
Anders Carlssonacfde802009-02-12 00:39:25 +000022#include <algorithm>
Torok Edwinf42e4a62009-08-24 13:25:12 +000023
Anders Carlssonacfde802009-02-12 00:39:25 +000024using namespace clang;
25using namespace CodeGen;
26
John McCallee504292010-05-21 04:11:14 +000027CGBlockInfo::CGBlockInfo(const char *N)
28 : Name(N), CXXThisRef(0), NeedsObjCSelf(false) {
29
30 // Skip asm prefix, if any.
31 if (Name && Name[0] == '\01')
32 ++Name;
33}
34
35
Mike Stumpcf62d392009-03-06 18:42:23 +000036llvm::Constant *CodeGenFunction::
Fariborz Jahanian89ecd412010-08-04 16:57:49 +000037BuildDescriptorBlockDecl(const BlockExpr *BE, const CGBlockInfo &Info,
Mike Stumpa803b0e2009-03-25 17:58:24 +000038 const llvm::StructType* Ty,
Mike Stump08920992009-03-07 02:35:30 +000039 std::vector<HelperInfo> *NoteForHelper) {
Fariborz Jahanian89ecd412010-08-04 16:57:49 +000040 bool BlockHasCopyDispose = Info.BlockHasCopyDispose;
41 CharUnits Size = Info.BlockSize;
Mike Stump56129b12009-02-13 16:55:51 +000042 const llvm::Type *UnsignedLongTy
43 = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpe5fee252009-02-13 16:19:19 +000044 llvm::Constant *C;
45 std::vector<llvm::Constant*> Elts;
46
47 // reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +000048 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpe5fee252009-02-13 16:19:19 +000049 Elts.push_back(C);
50
51 // Size
Mike Stumpd6840002009-02-21 20:07:44 +000052 // FIXME: What is the right way to say this doesn't fit? We should give
53 // a user diagnostic in that case. Better fix would be to change the
54 // API to size_t.
Ken Dyck199c3d62010-01-11 17:06:35 +000055 C = llvm::ConstantInt::get(UnsignedLongTy, Size.getQuantity());
Mike Stumpe5fee252009-02-13 16:19:19 +000056 Elts.push_back(C);
57
Blaine Garst2a7eb282010-02-23 21:51:17 +000058 // optional copy/dispose helpers
Mike Stumpe5fee252009-02-13 16:19:19 +000059 if (BlockHasCopyDispose) {
60 // copy_func_helper_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000061 Elts.push_back(BuildCopyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000062
63 // destroy_func_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000064 Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000065 }
66
Blaine Garst2a7eb282010-02-23 21:51:17 +000067 // Signature. non-optional ObjC-style method descriptor @encode sequence
68 std::string BlockTypeEncoding;
69 CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
70
71 Elts.push_back(llvm::ConstantExpr::getBitCast(
72 CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty));
73
74 // Layout.
Fariborz Jahanian89ecd412010-08-04 16:57:49 +000075 if (CGM.getContext().getLangOptions().ObjC1)
76 C = CGM.getObjCRuntime().GCBlockLayout(*this, Info.DeclRefs);
77 else
78 C = llvm::Constant::getNullValue(PtrToInt8Ty);
79
Blaine Garst2a7eb282010-02-23 21:51:17 +000080 Elts.push_back(C);
81
Nick Lewycky0d36dd22009-09-19 20:00:52 +000082 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stumpe5fee252009-02-13 16:19:19 +000083
Owen Anderson1c431b32009-07-08 19:05:04 +000084 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Mike Stumpe5fee252009-02-13 16:19:19 +000085 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +000086 C, "__block_descriptor_tmp");
Mike Stumpe5fee252009-02-13 16:19:19 +000087 return C;
88}
89
John McCallee504292010-05-21 04:11:14 +000090static void CollectBlockDeclRefInfo(const Stmt *S, CGBlockInfo &Info) {
Anders Carlsson4de9fce2009-03-01 01:09:12 +000091 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
92 I != E; ++I)
Daniel Dunbar82573ee2009-03-02 07:00:57 +000093 if (*I)
John McCallee504292010-05-21 04:11:14 +000094 CollectBlockDeclRefInfo(*I, Info);
Mike Stump00470a12009-03-05 08:32:30 +000095
Mike Stumpea26cb52009-10-21 03:49:08 +000096 // We want to ensure we walk down into block literals so we can find
97 // all nested BlockDeclRefExprs.
Mike Stump38e16272009-10-21 22:01:24 +000098 if (const BlockExpr *BE = dyn_cast<BlockExpr>(S)) {
John McCallee504292010-05-21 04:11:14 +000099 Info.InnerBlocks.insert(BE->getBlockDecl());
100 CollectBlockDeclRefInfo(BE->getBody(), Info);
Mike Stump38e16272009-10-21 22:01:24 +0000101 }
Mike Stumpea26cb52009-10-21 03:49:08 +0000102
John McCallee504292010-05-21 04:11:14 +0000103 else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) {
104 const ValueDecl *D = BDRE->getDecl();
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000105 // FIXME: Handle enums.
John McCallee504292010-05-21 04:11:14 +0000106 if (isa<FunctionDecl>(D))
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000107 return;
Mike Stump00470a12009-03-05 08:32:30 +0000108
John McCallee504292010-05-21 04:11:14 +0000109 if (isa<ImplicitParamDecl>(D) &&
110 isa<ObjCMethodDecl>(D->getDeclContext()) &&
111 cast<ObjCMethodDecl>(D->getDeclContext())->getSelfDecl() == D) {
112 Info.NeedsObjCSelf = true;
113 return;
114 }
115
Mike Stump38e16272009-10-21 22:01:24 +0000116 // Only Decls that escape are added.
John McCallee504292010-05-21 04:11:14 +0000117 if (!Info.InnerBlocks.count(D->getDeclContext()))
Mike Stump38e16272009-10-21 22:01:24 +0000118 Info.DeclRefs.push_back(BDRE);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000119 }
John McCallea1471e2010-05-20 01:18:31 +0000120
John McCallee504292010-05-21 04:11:14 +0000121 // Make sure to capture implicit 'self' references due to super calls.
Chandler Carruthf54b80f2010-05-21 10:29:28 +0000122 else if (const ObjCMessageExpr *E = dyn_cast<ObjCMessageExpr>(S)) {
John McCallee504292010-05-21 04:11:14 +0000123 if (E->getReceiverKind() == ObjCMessageExpr::SuperClass ||
124 E->getReceiverKind() == ObjCMessageExpr::SuperInstance)
125 Info.NeedsObjCSelf = true;
Chandler Carruthf54b80f2010-05-21 10:29:28 +0000126 }
John McCallee504292010-05-21 04:11:14 +0000127
128 // Getter/setter uses may also cause implicit super references,
129 // which we can check for with:
130 else if (isa<ObjCSuperExpr>(S))
131 Info.NeedsObjCSelf = true;
132
133 else if (isa<CXXThisExpr>(S))
John McCallea1471e2010-05-20 01:18:31 +0000134 Info.CXXThisRef = cast<CXXThisExpr>(S);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000135}
136
John McCallee504292010-05-21 04:11:14 +0000137/// CanBlockBeGlobal - Given a CGBlockInfo struct, determines if a block can be
Mike Stump58a85142009-03-04 22:48:06 +0000138/// declared as a global variable instead of on the stack.
John McCallee504292010-05-21 04:11:14 +0000139static bool CanBlockBeGlobal(const CGBlockInfo &Info) {
Mike Stumpea26cb52009-10-21 03:49:08 +0000140 return Info.DeclRefs.empty();
141}
142
143/// AllocateAllBlockDeclRefs - Preallocate all nested BlockDeclRefExprs to
144/// ensure we can generate the debug information for the parameter for the block
145/// invoke function.
John McCallee504292010-05-21 04:11:14 +0000146static void AllocateAllBlockDeclRefs(CodeGenFunction &CGF, CGBlockInfo &Info) {
John McCallea1471e2010-05-20 01:18:31 +0000147 if (Info.CXXThisRef)
John McCallee504292010-05-21 04:11:14 +0000148 CGF.AllocateBlockCXXThisPointer(Info.CXXThisRef);
Mike Stumpea26cb52009-10-21 03:49:08 +0000149
150 for (size_t i = 0; i < Info.DeclRefs.size(); ++i)
John McCallee504292010-05-21 04:11:14 +0000151 CGF.AllocateBlockDecl(Info.DeclRefs[i]);
152
153 if (Info.NeedsObjCSelf) {
154 ValueDecl *Self = cast<ObjCMethodDecl>(CGF.CurFuncDecl)->getSelfDecl();
155 BlockDeclRefExpr *BDRE =
156 new (CGF.getContext()) BlockDeclRefExpr(Self, Self->getType(),
157 SourceLocation(), false);
158 Info.DeclRefs.push_back(BDRE);
159 CGF.AllocateBlockDecl(BDRE);
160 }
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000161}
162
Fariborz Jahanian7edddb82010-07-28 19:07:18 +0000163static unsigned computeBlockFlag(CodeGenModule &CGM,
164 const BlockExpr *BE, unsigned flags) {
165 QualType BPT = BE->getType();
166 const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>();
167 QualType ResultType = ftype->getResultType();
168
169 CallArgList Args;
170 CodeGenTypes &Types = CGM.getTypes();
171 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, Args,
172 FunctionType::ExtInfo());
173 if (CGM.ReturnTypeUsesSRet(FnInfo))
174 flags |= CodeGenFunction::BLOCK_USE_STRET;
175 return flags;
176}
177
Mike Stump58a85142009-03-04 22:48:06 +0000178// FIXME: Push most into CGM, passing down a few bits, like current function
179// name.
Mike Stump8a2b4b12009-02-25 23:33:13 +0000180llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000181 std::string Name = CurFn->getName();
John McCallee504292010-05-21 04:11:14 +0000182 CGBlockInfo Info(Name.c_str());
183 Info.InnerBlocks.insert(BE->getBlockDecl());
184 CollectBlockDeclRefInfo(BE->getBody(), Info);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000185
186 // Check if the block can be global.
Mike Stump58a85142009-03-04 22:48:06 +0000187 // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like
188 // to just have one code path. We should move this function into CGM and pass
189 // CGF, then we can just check to see if CGF is 0.
Mike Stumpdab514f2009-03-04 03:23:46 +0000190 if (0 && CanBlockBeGlobal(Info))
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000191 return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
Mike Stump00470a12009-03-05 08:32:30 +0000192
David Chisnall5e530af2009-11-17 19:33:30 +0000193 size_t BlockFields = 5;
194
David Chisnall5e530af2009-11-17 19:33:30 +0000195 std::vector<llvm::Constant*> Elts(BlockFields);
196
Mike Stumpe5fee252009-02-13 16:19:19 +0000197 llvm::Constant *C;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000198 llvm::Value *V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000199
Mike Stumpe5fee252009-02-13 16:19:19 +0000200 {
201 // C = BuildBlockStructInitlist();
Blaine Garsta36e2232010-03-05 01:29:59 +0000202 unsigned int flags = BLOCK_HAS_SIGNATURE;
David Chisnall5e530af2009-11-17 19:33:30 +0000203
Mike Stump00470a12009-03-05 08:32:30 +0000204 // We run this first so that we set BlockHasCopyDispose from the entire
205 // block literal.
206 // __invoke
Mike Stump00470a12009-03-05 08:32:30 +0000207 llvm::Function *Fn
Fariborz Jahanian564360b2010-06-24 00:08:06 +0000208 = CodeGenFunction(CGM).GenerateBlockFunction(CurGD, BE, Info, CurFuncDecl,
John McCallee504292010-05-21 04:11:14 +0000209 LocalDeclMap);
210 BlockHasCopyDispose |= Info.BlockHasCopyDispose;
Mike Stump00470a12009-03-05 08:32:30 +0000211 Elts[3] = Fn;
212
Mike Stumpf5408fe2009-05-16 07:57:57 +0000213 // FIXME: Don't use BlockHasCopyDispose, it is set more often then
214 // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); }
John McCallee504292010-05-21 04:11:14 +0000215 if (Info.BlockHasCopyDispose)
Mike Stumpe5fee252009-02-13 16:19:19 +0000216 flags |= BLOCK_HAS_COPY_DISPOSE;
217
Mike Stump7d6dc4f2009-02-13 20:17:16 +0000218 // __isa
Mike Stump59c5b112009-02-13 19:29:27 +0000219 C = CGM.getNSConcreteStackBlock();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000220 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
Mike Stump00470a12009-03-05 08:32:30 +0000221 Elts[0] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000222
223 // __flags
Fariborz Jahanian7edddb82010-07-28 19:07:18 +0000224 flags = computeBlockFlag(CGM, BE, flags);
Mike Stumpe5fee252009-02-13 16:19:19 +0000225 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
226 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000227 C = llvm::ConstantInt::get(IntTy, flags);
Mike Stump00470a12009-03-05 08:32:30 +0000228 Elts[1] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000229
230 // __reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000231 C = llvm::ConstantInt::get(IntTy, 0);
Mike Stump00470a12009-03-05 08:32:30 +0000232 Elts[2] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000233
John McCallee504292010-05-21 04:11:14 +0000234 if (Info.BlockLayout.empty()) {
Mike Stumpcf62d392009-03-06 18:42:23 +0000235 // __descriptor
Fariborz Jahanian89ecd412010-08-04 16:57:49 +0000236 Elts[4] = BuildDescriptorBlockDecl(BE, Info, 0, 0);
Mike Stumpcf62d392009-03-06 18:42:23 +0000237
Mike Stump5570cfe2009-03-01 20:07:53 +0000238 // Optimize to being a global block.
239 Elts[0] = CGM.getNSConcreteGlobalBlock();
Blaine Garsta36e2232010-03-05 01:29:59 +0000240
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000241 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
Mike Stump5570cfe2009-03-01 20:07:53 +0000242
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000243 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000244
Owen Anderson1c431b32009-07-08 19:05:04 +0000245 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000246 llvm::GlobalValue::InternalLinkage, C,
247 "__block_holder_tmp_" +
248 llvm::Twine(CGM.getGlobalUniqueCount()));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000249 QualType BPT = BE->getType();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000250 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000251 return C;
252 }
Mike Stump00470a12009-03-05 08:32:30 +0000253
John McCallee504292010-05-21 04:11:14 +0000254 std::vector<const llvm::Type *> Types(BlockFields+Info.BlockLayout.size());
Mike Stump08920992009-03-07 02:35:30 +0000255 for (int i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000256 Types[i] = Elts[i]->getType();
Mike Stump08920992009-03-07 02:35:30 +0000257 Types[4] = PtrToInt8Ty;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000258
John McCallee504292010-05-21 04:11:14 +0000259 for (unsigned i = 0, n = Info.BlockLayout.size(); i != n; ++i) {
260 const Expr *E = Info.BlockLayout[i];
Mike Stumpa99038c2009-02-28 09:07:16 +0000261 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
262 QualType Ty = E->getType();
Mike Stumpdab514f2009-03-04 03:23:46 +0000263 if (BDRE && BDRE->isByRef()) {
Fariborz Jahanian79651722010-06-02 21:35:17 +0000264 Types[i+BlockFields] =
265 llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0);
266 } else if (BDRE && BDRE->getDecl()->getType()->isReferenceType()) {
267 Types[i+BlockFields] = llvm::PointerType::get(ConvertType(Ty), 0);
268 } else
David Chisnall5e530af2009-11-17 19:33:30 +0000269 Types[i+BlockFields] = ConvertType(Ty);
Mike Stumpa99038c2009-02-28 09:07:16 +0000270 }
Mike Stump8a2b4b12009-02-25 23:33:13 +0000271
Owen Anderson47a434f2009-08-05 23:18:46 +0000272 llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000273
274 llvm::AllocaInst *A = CreateTempAlloca(Ty);
John McCallee504292010-05-21 04:11:14 +0000275 A->setAlignment(Info.BlockAlign.getQuantity());
Mike Stump8a2b4b12009-02-25 23:33:13 +0000276 V = A;
277
John McCallea1471e2010-05-20 01:18:31 +0000278 // Build layout / cleanup information for all the data entries in the
279 // layout, and write the enclosing fields into the type.
John McCallee504292010-05-21 04:11:14 +0000280 std::vector<HelperInfo> NoteForHelper(Info.BlockLayout.size());
John McCallea1471e2010-05-20 01:18:31 +0000281 unsigned NumHelpers = 0;
Mike Stump08920992009-03-07 02:35:30 +0000282
283 for (unsigned i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000284 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
Mike Stump00470a12009-03-05 08:32:30 +0000285
John McCallee504292010-05-21 04:11:14 +0000286 for (unsigned i=0; i < Info.BlockLayout.size(); ++i) {
287 const Expr *E = Info.BlockLayout[i];
Mike Stump8a2b4b12009-02-25 23:33:13 +0000288
John McCallea1471e2010-05-20 01:18:31 +0000289 // Skip padding.
290 if (isa<DeclRefExpr>(E)) continue;
Mike Stumpa99038c2009-02-28 09:07:16 +0000291
John McCallea1471e2010-05-20 01:18:31 +0000292 llvm::Value* Addr = Builder.CreateStructGEP(V, i+BlockFields, "tmp");
293 HelperInfo &Note = NoteForHelper[NumHelpers++];
Mike Stumpa99038c2009-02-28 09:07:16 +0000294
John McCallea1471e2010-05-20 01:18:31 +0000295 Note.index = i+5;
Mike Stump08920992009-03-07 02:35:30 +0000296
John McCallea1471e2010-05-20 01:18:31 +0000297 if (isa<CXXThisExpr>(E)) {
298 Note.RequiresCopying = false;
299 Note.flag = BLOCK_FIELD_IS_OBJECT;
Mike Stumpa99038c2009-02-28 09:07:16 +0000300
John McCallea1471e2010-05-20 01:18:31 +0000301 Builder.CreateStore(LoadCXXThis(), Addr);
302 continue;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000303 }
John McCallea1471e2010-05-20 01:18:31 +0000304
305 const BlockDeclRefExpr *BDRE = cast<BlockDeclRefExpr>(E);
306 const ValueDecl *VD = BDRE->getDecl();
307 QualType T = VD->getType();
308
309 Note.RequiresCopying = BlockRequiresCopying(T);
310
311 if (BDRE->isByRef()) {
312 Note.flag = BLOCK_FIELD_IS_BYREF;
313 if (T.isObjCGCWeak())
314 Note.flag |= BLOCK_FIELD_IS_WEAK;
315 } else if (T->isBlockPointerType()) {
316 Note.flag = BLOCK_FIELD_IS_BLOCK;
317 } else {
318 Note.flag = BLOCK_FIELD_IS_OBJECT;
319 }
320
321 if (LocalDeclMap[VD]) {
322 if (BDRE->isByRef()) {
323 llvm::Value *Loc = LocalDeclMap[VD];
324 Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
325 Loc = Builder.CreateLoad(Loc);
326 Builder.CreateStore(Loc, Addr);
327 continue;
328 } else {
Fariborz Jahanianac7362d2010-06-08 20:57:22 +0000329 if (BDRE->getCopyConstructorExpr()) {
330 E = BDRE->getCopyConstructorExpr();
John McCallf1549f62010-07-06 01:34:17 +0000331 PushDestructorCleanup(E->getType(), Addr);
Fariborz Jahanianac7362d2010-06-08 20:57:22 +0000332 }
Fariborz Jahanian59da45a2010-06-04 21:35:44 +0000333 else {
334 E = new (getContext()) DeclRefExpr(const_cast<ValueDecl*>(VD),
Fariborz Jahanian79651722010-06-02 21:35:17 +0000335 VD->getType().getNonReferenceType(),
336 SourceLocation());
Fariborz Jahanian59da45a2010-06-04 21:35:44 +0000337 if (VD->getType()->isReferenceType()) {
338 E = new (getContext())
339 UnaryOperator(const_cast<Expr*>(E), UnaryOperator::AddrOf,
340 getContext().getPointerType(E->getType()),
341 SourceLocation());
342 }
Fariborz Jahaniandf8b8ea2010-06-04 16:10:00 +0000343 }
344 }
John McCallea1471e2010-05-20 01:18:31 +0000345 }
John McCallea1471e2010-05-20 01:18:31 +0000346
347 if (BDRE->isByRef()) {
348 E = new (getContext())
349 UnaryOperator(const_cast<Expr*>(E), UnaryOperator::AddrOf,
350 getContext().getPointerType(E->getType()),
351 SourceLocation());
352 }
353
354 RValue r = EmitAnyExpr(E, Addr, false);
355 if (r.isScalar()) {
356 llvm::Value *Loc = r.getScalarVal();
357 const llvm::Type *Ty = Types[i+BlockFields];
358 if (BDRE->isByRef()) {
359 // E is now the address of the value field, instead, we want the
360 // address of the actual ByRef struct. We optimize this slightly
361 // compared to gcc by not grabbing the forwarding slot as this must
362 // be done during Block_copy for us, and we can postpone the work
363 // until then.
364 CharUnits offset = BlockDecls[BDRE->getDecl()];
365
366 llvm::Value *BlockLiteral = LoadBlockStruct();
367
368 Loc = Builder.CreateGEP(BlockLiteral,
Chris Lattner77b89b82010-06-27 07:15:29 +0000369 llvm::ConstantInt::get(Int64Ty, offset.getQuantity()),
John McCallea1471e2010-05-20 01:18:31 +0000370 "block.literal");
371 Ty = llvm::PointerType::get(Ty, 0);
372 Loc = Builder.CreateBitCast(Loc, Ty);
373 Loc = Builder.CreateLoad(Loc);
374 // Loc = Builder.CreateBitCast(Loc, Ty);
375 }
376 Builder.CreateStore(Loc, Addr);
377 } else if (r.isComplex())
378 // FIXME: implement
379 ErrorUnsupported(BE, "complex in block literal");
380 else if (r.isAggregate())
381 ; // Already created into the destination
382 else
383 assert (0 && "bad block variable");
384 // FIXME: Ensure that the offset created by the backend for
385 // the struct matches the previously computed offset in BlockDecls.
386 }
387 NoteForHelper.resize(NumHelpers);
Mike Stumpcf62d392009-03-06 18:42:23 +0000388
389 // __descriptor
Fariborz Jahanian89ecd412010-08-04 16:57:49 +0000390 llvm::Value *Descriptor = BuildDescriptorBlockDecl(BE, Info, Ty,
Mike Stump08920992009-03-07 02:35:30 +0000391 &NoteForHelper);
392 Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
393 Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
Mike Stumpe5fee252009-02-13 16:19:19 +0000394 }
Mike Stump00470a12009-03-05 08:32:30 +0000395
Mike Stumpbd65cac2009-02-19 01:01:04 +0000396 QualType BPT = BE->getType();
Fariborz Jahanian263c4de2010-02-10 23:34:57 +0000397 V = Builder.CreateBitCast(V, ConvertType(BPT));
398 // See if this is a __weak block variable and the must call objc_read_weak
399 // on it.
400 const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>();
401 QualType RES = ftype->getResultType();
402 if (RES.isObjCGCWeak()) {
403 // Must cast argument to id*
404 const llvm::Type *ObjectPtrTy =
405 ConvertType(CGM.getContext().getObjCIdType());
406 const llvm::Type *PtrObjectPtrTy =
407 llvm::PointerType::getUnqual(ObjectPtrTy);
408 V = Builder.CreateBitCast(V, PtrObjectPtrTy);
409 V = CGM.getObjCRuntime().EmitObjCWeakRead(*this, V);
410 }
411 return V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000412}
413
414
Mike Stump2a998142009-03-04 18:17:45 +0000415const llvm::Type *BlockModule::getBlockDescriptorType() {
Mike Stumpab695142009-02-13 15:16:56 +0000416 if (BlockDescriptorType)
417 return BlockDescriptorType;
418
Mike Stumpa5448542009-02-13 15:32:32 +0000419 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000420 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000421
Mike Stumpab695142009-02-13 15:16:56 +0000422 // struct __block_descriptor {
423 // unsigned long reserved;
424 // unsigned long block_size;
Blaine Garst2a7eb282010-02-23 21:51:17 +0000425 //
426 // // later, the following will be added
427 //
428 // struct {
429 // void (*copyHelper)();
430 // void (*copyHelper)();
431 // } helpers; // !!! optional
432 //
433 // const char *signature; // the block signature
434 // const char *layout; // reserved
Mike Stumpab695142009-02-13 15:16:56 +0000435 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000436 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(),
437 UnsignedLongTy,
Mike Stumpa5448542009-02-13 15:32:32 +0000438 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000439 NULL);
440
441 getModule().addTypeName("struct.__block_descriptor",
442 BlockDescriptorType);
443
444 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000445}
446
Mike Stump2a998142009-03-04 18:17:45 +0000447const llvm::Type *BlockModule::getGenericBlockLiteralType() {
Mike Stump9b8a7972009-02-13 15:25:34 +0000448 if (GenericBlockLiteralType)
449 return GenericBlockLiteralType;
450
Mike Stumpa5448542009-02-13 15:32:32 +0000451 const llvm::Type *BlockDescPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000452 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000453
Mike Stump7cbb3602009-02-13 16:01:35 +0000454 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
455 getTypes().ConvertType(getContext().IntTy));
456
Mike Stump9b8a7972009-02-13 15:25:34 +0000457 // struct __block_literal_generic {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000458 // void *__isa;
459 // int __flags;
460 // int __reserved;
461 // void (*__invoke)(void *);
462 // struct __block_descriptor *__descriptor;
Mike Stump9b8a7972009-02-13 15:25:34 +0000463 // };
Blaine Garst2a7eb282010-02-23 21:51:17 +0000464 GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
Owen Anderson47a434f2009-08-05 23:18:46 +0000465 PtrToInt8Ty,
Mike Stump7cbb3602009-02-13 16:01:35 +0000466 IntTy,
467 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000468 PtrToInt8Ty,
Mike Stump9b8a7972009-02-13 15:25:34 +0000469 BlockDescPtrTy,
470 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000471
Mike Stump9b8a7972009-02-13 15:25:34 +0000472 getModule().addTypeName("struct.__block_literal_generic",
473 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000474
Mike Stump9b8a7972009-02-13 15:25:34 +0000475 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000476}
477
Mike Stumpbd65cac2009-02-19 01:01:04 +0000478
Anders Carlssona1736c02009-12-24 21:13:40 +0000479RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E,
480 ReturnValueSlot ReturnValue) {
Mike Stumpa5448542009-02-13 15:32:32 +0000481 const BlockPointerType *BPT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000482 E->getCallee()->getType()->getAs<BlockPointerType>();
Mike Stumpa5448542009-02-13 15:32:32 +0000483
Anders Carlssonacfde802009-02-12 00:39:25 +0000484 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
485
486 // Get a pointer to the generic block literal.
487 const llvm::Type *BlockLiteralTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000488 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000489
490 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000491 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000492 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
493
494 // Get the function pointer from the literal.
495 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Anders Carlssonacfde802009-02-12 00:39:25 +0000496
Mike Stumpa5448542009-02-13 15:32:32 +0000497 BlockLiteral =
498 Builder.CreateBitCast(BlockLiteral,
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000499 llvm::Type::getInt8PtrTy(VMContext),
Anders Carlssonacfde802009-02-12 00:39:25 +0000500 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000501
Anders Carlssonacfde802009-02-12 00:39:25 +0000502 // Add the block literal.
503 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
504 CallArgList Args;
505 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000506
Anders Carlsson782f3972009-04-08 23:13:16 +0000507 QualType FnType = BPT->getPointeeType();
508
Anders Carlssonacfde802009-02-12 00:39:25 +0000509 // And the rest of the arguments.
John McCall183700f2009-09-21 23:43:11 +0000510 EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(),
Anders Carlsson782f3972009-04-08 23:13:16 +0000511 E->arg_begin(), E->arg_end());
Mike Stumpa5448542009-02-13 15:32:32 +0000512
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000513 // Load the function.
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000514 llvm::Value *Func = Builder.CreateLoad(FuncPtr, "tmp");
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000515
John McCall04a67a62010-02-05 21:31:56 +0000516 const FunctionType *FuncTy = FnType->getAs<FunctionType>();
517 QualType ResultType = FuncTy->getResultType();
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000518
Mike Stump1eb44332009-09-09 15:08:12 +0000519 const CGFunctionInfo &FnInfo =
Rafael Espindola264ba482010-03-30 20:24:48 +0000520 CGM.getTypes().getFunctionInfo(ResultType, Args,
521 FuncTy->getExtInfo());
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000523 // Cast the function pointer to the right type.
Mike Stump1eb44332009-09-09 15:08:12 +0000524 const llvm::Type *BlockFTy =
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000525 CGM.getTypes().GetFunctionType(FnInfo, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Owen Anderson96e0fc72009-07-29 22:16:19 +0000527 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000528 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Anders Carlssonacfde802009-02-12 00:39:25 +0000530 // And call the block.
Anders Carlssona1736c02009-12-24 21:13:40 +0000531 return EmitCall(FnInfo, Func, ReturnValue, Args);
Anders Carlssonacfde802009-02-12 00:39:25 +0000532}
Anders Carlssond5cab542009-02-12 17:55:02 +0000533
John McCallea1471e2010-05-20 01:18:31 +0000534void CodeGenFunction::AllocateBlockCXXThisPointer(const CXXThisExpr *E) {
535 assert(BlockCXXThisOffset.isZero() && "already computed 'this' pointer");
536
537 // Figure out what the offset is.
538 QualType T = E->getType();
539 std::pair<CharUnits,CharUnits> TypeInfo = getContext().getTypeInfoInChars(T);
540 CharUnits Offset = getBlockOffset(TypeInfo.first, TypeInfo.second);
541
542 BlockCXXThisOffset = Offset;
543 BlockLayout.push_back(E);
544}
545
546void CodeGenFunction::AllocateBlockDecl(const BlockDeclRefExpr *E) {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000547 const ValueDecl *VD = E->getDecl();
John McCallea1471e2010-05-20 01:18:31 +0000548 CharUnits &Offset = BlockDecls[VD];
Mike Stumpdab514f2009-03-04 03:23:46 +0000549
Mike Stumpdab514f2009-03-04 03:23:46 +0000550 // See if we have already allocated an offset for this variable.
John McCallea1471e2010-05-20 01:18:31 +0000551 if (!Offset.isZero())
552 return;
Mike Stumpea26cb52009-10-21 03:49:08 +0000553
554 // Don't run the expensive check, unless we have to.
Mike Stump083c25e2009-10-22 00:49:09 +0000555 if (!BlockHasCopyDispose)
556 if (E->isByRef()
557 || BlockRequiresCopying(E->getType()))
558 BlockHasCopyDispose = true;
Mike Stumpea26cb52009-10-21 03:49:08 +0000559
John McCallea1471e2010-05-20 01:18:31 +0000560 const ValueDecl *D = cast<ValueDecl>(E->getDecl());
Mike Stumpea26cb52009-10-21 03:49:08 +0000561
John McCallea1471e2010-05-20 01:18:31 +0000562 CharUnits Size;
563 CharUnits Align;
564
565 if (E->isByRef()) {
566 llvm::tie(Size,Align) =
567 getContext().getTypeInfoInChars(getContext().VoidPtrTy);
568 } else {
569 Size = getContext().getTypeSizeInChars(D->getType());
570 Align = getContext().getDeclAlign(D);
571 }
572
573 Offset = getBlockOffset(Size, Align);
574 BlockLayout.push_back(E);
Mike Stumpea26cb52009-10-21 03:49:08 +0000575}
576
John McCallee504292010-05-21 04:11:14 +0000577llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const ValueDecl *VD,
578 bool IsByRef) {
Fariborz Jahaniandf8b8ea2010-06-04 16:10:00 +0000579
John McCallea1471e2010-05-20 01:18:31 +0000580 CharUnits offset = BlockDecls[VD];
581 assert(!offset.isZero() && "getting address of unallocated decl");
Mike Stumpdab514f2009-03-04 03:23:46 +0000582
583 llvm::Value *BlockLiteral = LoadBlockStruct();
584 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
Chris Lattner77b89b82010-06-27 07:15:29 +0000585 llvm::ConstantInt::get(Int64Ty, offset.getQuantity()),
Mike Stump58a85142009-03-04 22:48:06 +0000586 "block.literal");
John McCallee504292010-05-21 04:11:14 +0000587 if (IsByRef) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000588 const llvm::Type *PtrStructTy
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000589 = llvm::PointerType::get(BuildByRefType(VD), 0);
Mike Stumpa8b60c92009-03-21 21:00:35 +0000590 // The block literal will need a copy/destroy helper.
591 BlockHasCopyDispose = true;
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000592
593 const llvm::Type *Ty = PtrStructTy;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000594 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000595 V = Builder.CreateBitCast(V, Ty);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000596 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000597 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000598 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000599 V = Builder.CreateBitCast(V, PtrStructTy);
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000600 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
601 VD->getNameAsString());
Fariborz Jahaniand33ded52010-05-04 17:59:32 +0000602 if (VD->getType()->isReferenceType())
603 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000604 } else {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000605 const llvm::Type *Ty = CGM.getTypes().ConvertType(VD->getType());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000606 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000607 V = Builder.CreateBitCast(V, Ty);
Fariborz Jahanian79651722010-06-02 21:35:17 +0000608 if (VD->getType()->isReferenceType())
Fariborz Jahaniandf8b8ea2010-06-04 16:10:00 +0000609 V = Builder.CreateLoad(V, "ref.tmp");
Mike Stumpdab514f2009-03-04 03:23:46 +0000610 }
611 return V;
612}
613
Mike Stump67a64482009-02-14 22:16:35 +0000614llvm::Constant *
Mike Stump90a90432009-03-04 18:47:42 +0000615BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000616 // Generate the block descriptor.
617 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000618 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
619 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000620
Blaine Garst2a7eb282010-02-23 21:51:17 +0000621 llvm::Constant *DescriptorFields[4];
Mike Stumpa5448542009-02-13 15:32:32 +0000622
Anders Carlssond5cab542009-02-12 17:55:02 +0000623 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000624 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000625
Anders Carlssond5cab542009-02-12 17:55:02 +0000626 // Block literal size. For global blocks we just use the size of the generic
627 // block literal struct.
Ken Dyck687cc4a2010-01-26 13:48:07 +0000628 CharUnits BlockLiteralSize =
629 CGM.GetTargetTypeStoreSize(getGenericBlockLiteralType());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000630 DescriptorFields[1] =
Ken Dyck199c3d62010-01-11 17:06:35 +0000631 llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize.getQuantity());
Blaine Garst2a7eb282010-02-23 21:51:17 +0000632
633 // signature. non-optional ObjC-style method descriptor @encode sequence
634 std::string BlockTypeEncoding;
635 CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
Mike Stumpa5448542009-02-13 15:32:32 +0000636
Blaine Garst2a7eb282010-02-23 21:51:17 +0000637 DescriptorFields[2] = llvm::ConstantExpr::getBitCast(
638 CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty);
639
640 // layout
641 DescriptorFields[3] =
642 llvm::ConstantInt::get(UnsignedLongTy,0);
643
644 // build the structure from the 4 elements
Mike Stumpa5448542009-02-13 15:32:32 +0000645 llvm::Constant *DescriptorStruct =
Blaine Garst2a7eb282010-02-23 21:51:17 +0000646 llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 4, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000647
Anders Carlssond5cab542009-02-12 17:55:02 +0000648 llvm::GlobalVariable *Descriptor =
Owen Anderson1c431b32009-07-08 19:05:04 +0000649 new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000650 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000651 DescriptorStruct, "__block_descriptor_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000652
David Chisnall5e530af2009-11-17 19:33:30 +0000653 int FieldCount = 5;
Anders Carlssond5cab542009-02-12 17:55:02 +0000654 // Generate the constants for the block literal.
David Chisnall5e530af2009-11-17 19:33:30 +0000655
656 std::vector<llvm::Constant*> LiteralFields(FieldCount);
Mike Stumpa5448542009-02-13 15:32:32 +0000657
John McCallee504292010-05-21 04:11:14 +0000658 CGBlockInfo Info(n);
Mike Stump7f28a9c2009-03-13 23:34:28 +0000659 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000660 llvm::Function *Fn
Fariborz Jahanian7edddb82010-07-28 19:07:18 +0000661 = CodeGenFunction(CGM).GenerateBlockFunction(GlobalDecl(), BE,
662 Info, 0, LocalDeclMap);
John McCallee504292010-05-21 04:11:14 +0000663 assert(Info.BlockSize == BlockLiteralSize
Mike Stump4e7a1f72009-02-21 20:00:35 +0000664 && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000665
Anders Carlssond5cab542009-02-12 17:55:02 +0000666 // isa
Daniel Dunbar673431a2010-07-16 00:00:15 +0000667 LiteralFields[0] = CGM.getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000668
Fariborz Jahanian7edddb82010-07-28 19:07:18 +0000669 // __flags
670 unsigned flags = computeBlockFlag(CGM, BE,
671 (BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE));
Blaine Garst2a7eb282010-02-23 21:51:17 +0000672 LiteralFields[1] =
Fariborz Jahanian7edddb82010-07-28 19:07:18 +0000673 llvm::ConstantInt::get(IntTy, flags);
Mike Stumpa5448542009-02-13 15:32:32 +0000674
Anders Carlssond5cab542009-02-12 17:55:02 +0000675 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000676 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000677
Anders Carlssond5cab542009-02-12 17:55:02 +0000678 // Function
679 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000680
Anders Carlssond5cab542009-02-12 17:55:02 +0000681 // Descriptor
682 LiteralFields[4] = Descriptor;
David Chisnall5e530af2009-11-17 19:33:30 +0000683
Mike Stumpa5448542009-02-13 15:32:32 +0000684 llvm::Constant *BlockLiteralStruct =
David Chisnall5e530af2009-11-17 19:33:30 +0000685 llvm::ConstantStruct::get(VMContext, LiteralFields, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000686
687 llvm::GlobalVariable *BlockLiteral =
Owen Anderson1c431b32009-07-08 19:05:04 +0000688 new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000689 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000690 BlockLiteralStruct, "__block_literal_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000691
Anders Carlssond5cab542009-02-12 17:55:02 +0000692 return BlockLiteral;
693}
694
Mike Stump4e7a1f72009-02-21 20:00:35 +0000695llvm::Value *CodeGenFunction::LoadBlockStruct() {
Mike Stumpbf1914b2009-10-20 20:30:01 +0000696 llvm::Value *V = Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()],
697 "self");
698 // For now, we codegen based upon byte offsets.
699 return Builder.CreateBitCast(V, PtrToInt8Ty);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000700}
701
Mike Stump00470a12009-03-05 08:32:30 +0000702llvm::Function *
Fariborz Jahanian564360b2010-06-24 00:08:06 +0000703CodeGenFunction::GenerateBlockFunction(GlobalDecl GD, const BlockExpr *BExpr,
John McCallee504292010-05-21 04:11:14 +0000704 CGBlockInfo &Info,
Mike Stump6cc88f72009-03-20 21:53:12 +0000705 const Decl *OuterFuncDecl,
John McCallee504292010-05-21 04:11:14 +0000706 llvm::DenseMap<const Decl*, llvm::Value*> ldm) {
Devang Patel963dfbd2009-04-15 21:51:44 +0000707
708 // Check if we should generate debug info for this block.
709 if (CGM.getDebugInfo())
710 DebugInfo = CGM.getDebugInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Mike Stump7f28a9c2009-03-13 23:34:28 +0000712 // Arrange for local static and local extern declarations to appear
713 // to be local to this function as well, as they are directly referenced
714 // in a block.
715 for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
716 i != ldm.end();
717 ++i) {
718 const VarDecl *VD = dyn_cast<VarDecl>(i->first);
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000720 if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage())
Mike Stump7f28a9c2009-03-13 23:34:28 +0000721 LocalDeclMap[VD] = i->second;
722 }
723
Ken Dyck687cc4a2010-01-26 13:48:07 +0000724 BlockOffset =
725 CGM.GetTargetTypeStoreSize(CGM.getGenericBlockLiteralType());
Ken Dyck30a8a272010-01-26 19:13:33 +0000726 BlockAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Eli Friedman48f91222009-03-28 03:24:54 +0000727
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000728 const FunctionType *BlockFunctionType = BExpr->getFunctionType();
729 QualType ResultType;
Rafael Espindola264ba482010-03-30 20:24:48 +0000730 FunctionType::ExtInfo EInfo = getFunctionExtInfo(*BlockFunctionType);
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000731 bool IsVariadic;
Mike Stump1eb44332009-09-09 15:08:12 +0000732 if (const FunctionProtoType *FTy =
Fariborz Jahanianda0895d2009-04-11 18:54:57 +0000733 dyn_cast<FunctionProtoType>(BlockFunctionType)) {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000734 ResultType = FTy->getResultType();
735 IsVariadic = FTy->isVariadic();
Mike Stumpb3589f42009-07-30 22:28:39 +0000736 } else {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000737 // K&R style block.
738 ResultType = BlockFunctionType->getResultType();
739 IsVariadic = false;
740 }
Mike Stumpa5448542009-02-13 15:32:32 +0000741
Anders Carlssond5cab542009-02-12 17:55:02 +0000742 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000743
Mike Stumpea26cb52009-10-21 03:49:08 +0000744 CurFuncDecl = OuterFuncDecl;
745
Chris Lattner161d36d2009-02-28 19:01:03 +0000746 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlssond5cab542009-02-12 17:55:02 +0000747
Mike Stumpea26cb52009-10-21 03:49:08 +0000748 IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor");
Mike Stumpadaaad32009-10-20 02:12:22 +0000749
John McCallea1471e2010-05-20 01:18:31 +0000750 // Build the block struct now.
John McCallee504292010-05-21 04:11:14 +0000751 AllocateAllBlockDeclRefs(*this, Info);
Mike Stumpea26cb52009-10-21 03:49:08 +0000752
Mike Stump083c25e2009-10-22 00:49:09 +0000753 QualType ParmTy = getContext().getBlockParmType(BlockHasCopyDispose,
John McCallea1471e2010-05-20 01:18:31 +0000754 BlockLayout);
755
Anders Carlssond5cab542009-02-12 17:55:02 +0000756 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000757 ImplicitParamDecl *SelfDecl =
John McCall2888b652010-04-30 21:35:41 +0000758 ImplicitParamDecl::Create(getContext(), const_cast<BlockDecl*>(BD),
Mike Stumpadaaad32009-10-20 02:12:22 +0000759 SourceLocation(), II,
760 ParmTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000761
Anders Carlssond5cab542009-02-12 17:55:02 +0000762 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump4e7a1f72009-02-21 20:00:35 +0000763 BlockStructDecl = SelfDecl;
Mike Stumpa5448542009-02-13 15:32:32 +0000764
Steve Naroffe78b8092009-03-13 16:56:44 +0000765 for (BlockDecl::param_const_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000766 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000767 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000768
769 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +0000770 CGM.getTypes().getFunctionInfo(ResultType, Args, EInfo);
Anders Carlssond5cab542009-02-12 17:55:02 +0000771
Anders Carlssond5cab542009-02-12 17:55:02 +0000772 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000773 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic);
Mike Stumpa5448542009-02-13 15:32:32 +0000774
Douglas Gregor35415f52010-05-25 17:04:15 +0000775 MangleBuffer Name;
Fariborz Jahanian564360b2010-06-24 00:08:06 +0000776 CGM.getMangledName(GD, Name, BD);
Mike Stumpa5448542009-02-13 15:32:32 +0000777 llvm::Function *Fn =
Douglas Gregor6e5d9b02010-05-25 17:12:30 +0000778 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
779 Name.getString(), &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000780
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000781 CGM.SetInternalFunctionAttributes(BD, Fn, FI);
782
Fariborz Jahanian16ac5ce2010-06-28 18:58:34 +0000783 QualType FnType(BlockFunctionType, 0);
Fariborz Jahanianef160b42010-06-28 19:42:10 +0000784 bool HasPrototype = isa<FunctionProtoType>(BlockFunctionType);
Fariborz Jahanian16ac5ce2010-06-28 18:58:34 +0000785
786 IdentifierInfo *ID = &getContext().Idents.get(Name.getString());
787 CurCodeDecl = FunctionDecl::Create(getContext(),
788 getContext().getTranslationUnitDecl(),
789 SourceLocation(), ID, FnType,
790 0,
791 FunctionDecl::Static,
792 FunctionDecl::None,
793 false, HasPrototype);
794
Chris Lattner4863db42009-04-23 07:18:56 +0000795 StartFunction(BD, ResultType, Fn, Args,
Chris Lattner161d36d2009-02-28 19:01:03 +0000796 BExpr->getBody()->getLocEnd());
Mike Stumpb1a6e682009-09-30 02:43:10 +0000797
Chris Lattner4863db42009-04-23 07:18:56 +0000798 CurFuncDecl = OuterFuncDecl;
Mike Stumpb289b3f2009-10-01 22:29:41 +0000799
John McCallea1471e2010-05-20 01:18:31 +0000800 // If we have a C++ 'this' reference, go ahead and force it into
801 // existence now.
802 if (Info.CXXThisRef) {
803 assert(!BlockCXXThisOffset.isZero() &&
804 "haven't yet allocated 'this' reference");
805
806 // TODO: I have a dream that one day this will be typed.
807 llvm::Value *BlockLiteral = LoadBlockStruct();
808 llvm::Value *ThisPtrRaw =
809 Builder.CreateConstInBoundsGEP1_64(BlockLiteral,
810 BlockCXXThisOffset.getQuantity(),
811 "this.ptr.raw");
812
813 const llvm::Type *Ty =
814 CGM.getTypes().ConvertType(Info.CXXThisRef->getType());
815 Ty = llvm::PointerType::get(Ty, 0);
816 llvm::Value *ThisPtr = Builder.CreateBitCast(ThisPtrRaw, Ty, "this.ptr");
817
818 CXXThisValue = Builder.CreateLoad(ThisPtr, "this");
819 }
820
John McCallee504292010-05-21 04:11:14 +0000821 // If we have an Objective C 'self' reference, go ahead and force it
822 // into existence now.
823 if (Info.NeedsObjCSelf) {
824 ValueDecl *Self = cast<ObjCMethodDecl>(CurFuncDecl)->getSelfDecl();
825 LocalDeclMap[Self] = GetAddrOfBlockDecl(Self, false);
826 }
827
Mike Stumpb289b3f2009-10-01 22:29:41 +0000828 // Save a spot to insert the debug information for all the BlockDeclRefDecls.
829 llvm::BasicBlock *entry = Builder.GetInsertBlock();
830 llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint();
831 --entry_ptr;
832
Chris Lattner161d36d2009-02-28 19:01:03 +0000833 EmitStmt(BExpr->getBody());
Mike Stumpb289b3f2009-10-01 22:29:41 +0000834
Mike Stumpde8c5c72009-10-01 00:27:30 +0000835 // Remember where we were...
836 llvm::BasicBlock *resume = Builder.GetInsertBlock();
Mike Stumpb289b3f2009-10-01 22:29:41 +0000837
Mike Stumpde8c5c72009-10-01 00:27:30 +0000838 // Go back to the entry.
Mike Stumpb289b3f2009-10-01 22:29:41 +0000839 ++entry_ptr;
840 Builder.SetInsertPoint(entry, entry_ptr);
841
Mike Stumpb1a6e682009-09-30 02:43:10 +0000842 if (CGDebugInfo *DI = getDebugInfo()) {
Mike Stumpb1a6e682009-09-30 02:43:10 +0000843 // Emit debug information for all the BlockDeclRefDecls.
John McCallea1471e2010-05-20 01:18:31 +0000844 // FIXME: also for 'this'
845 for (unsigned i = 0, e = BlockLayout.size(); i != e; ++i) {
846 if (const BlockDeclRefExpr *BDRE =
847 dyn_cast<BlockDeclRefExpr>(BlockLayout[i])) {
Mike Stumpb1a6e682009-09-30 02:43:10 +0000848 const ValueDecl *D = BDRE->getDecl();
849 DI->setLocation(D->getLocation());
850 DI->EmitDeclareOfBlockDeclRefVariable(BDRE,
851 LocalDeclMap[getBlockStructDecl()],
852 Builder, this);
853 }
854 }
Mike Stumpb1a6e682009-09-30 02:43:10 +0000855 }
Mike Stumpde8c5c72009-10-01 00:27:30 +0000856 // And resume where we left off.
857 if (resume == 0)
858 Builder.ClearInsertionPoint();
859 else
860 Builder.SetInsertPoint(resume);
Mike Stumpb1a6e682009-09-30 02:43:10 +0000861
Chris Lattner161d36d2009-02-28 19:01:03 +0000862 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlssond5cab542009-02-12 17:55:02 +0000863
Mike Stump8a2b4b12009-02-25 23:33:13 +0000864 // The runtime needs a minimum alignment of a void *.
Ken Dyck30a8a272010-01-26 19:13:33 +0000865 CharUnits MinAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Ken Dyck199c3d62010-01-11 17:06:35 +0000866 BlockOffset = CharUnits::fromQuantity(
Ken Dyck30a8a272010-01-26 19:13:33 +0000867 llvm::RoundUpToAlignment(BlockOffset.getQuantity(),
868 MinAlign.getQuantity()));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000869
John McCallee504292010-05-21 04:11:14 +0000870 Info.BlockSize = BlockOffset;
871 Info.BlockAlign = BlockAlign;
872 Info.BlockLayout = BlockLayout;
873 Info.BlockHasCopyDispose = BlockHasCopyDispose;
Anders Carlssond5cab542009-02-12 17:55:02 +0000874 return Fn;
875}
Mike Stumpa99038c2009-02-28 09:07:16 +0000876
John McCallea1471e2010-05-20 01:18:31 +0000877CharUnits BlockFunction::getBlockOffset(CharUnits Size, CharUnits Align) {
878 assert((Align.isPositive()) && "alignment must be 1 byte or more");
Mike Stumpa99038c2009-02-28 09:07:16 +0000879
Ken Dyck199c3d62010-01-11 17:06:35 +0000880 CharUnits OldOffset = BlockOffset;
Mike Stumpa99038c2009-02-28 09:07:16 +0000881
882 // Ensure proper alignment, even if it means we have to have a gap
Ken Dyck199c3d62010-01-11 17:06:35 +0000883 BlockOffset = CharUnits::fromQuantity(
Ken Dyck30a8a272010-01-26 19:13:33 +0000884 llvm::RoundUpToAlignment(BlockOffset.getQuantity(), Align.getQuantity()));
Mike Stumpa99038c2009-02-28 09:07:16 +0000885 BlockAlign = std::max(Align, BlockAlign);
Mike Stump00470a12009-03-05 08:32:30 +0000886
Ken Dyck199c3d62010-01-11 17:06:35 +0000887 CharUnits Pad = BlockOffset - OldOffset;
888 if (Pad.isPositive()) {
Mike Stumpa99038c2009-02-28 09:07:16 +0000889 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
Ken Dyck199c3d62010-01-11 17:06:35 +0000890 llvm::APInt(32,
891 Pad.getQuantity()),
Mike Stumpa99038c2009-02-28 09:07:16 +0000892 ArrayType::Normal, 0);
Douglas Gregorba55ec22010-05-11 18:17:16 +0000893 ValueDecl *PadDecl = VarDecl::Create(getContext(),
894 getContext().getTranslationUnitDecl(),
895 SourceLocation(),
Douglas Gregor16573fa2010-04-19 22:54:31 +0000896 0, QualType(PadTy), 0,
897 VarDecl::None, VarDecl::None);
John McCallea1471e2010-05-20 01:18:31 +0000898 Expr *E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
899 SourceLocation());
900 BlockLayout.push_back(E);
Mike Stumpa99038c2009-02-28 09:07:16 +0000901 }
Mike Stumpa99038c2009-02-28 09:07:16 +0000902
903 BlockOffset += Size;
John McCallea1471e2010-05-20 01:18:31 +0000904 return BlockOffset - Size;
Mike Stumpa99038c2009-02-28 09:07:16 +0000905}
Mike Stumpdab514f2009-03-04 03:23:46 +0000906
Mike Stump08920992009-03-07 02:35:30 +0000907llvm::Constant *BlockFunction::
908GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000909 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000910 QualType R = getContext().VoidTy;
911
912 FunctionArgList Args;
913 // FIXME: This leaks
Mike Stump08920992009-03-07 02:35:30 +0000914 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +0000915 ImplicitParamDecl::Create(getContext(), 0,
916 SourceLocation(), 0,
Mike Stump08920992009-03-07 02:35:30 +0000917 getContext().getPointerType(getContext().VoidTy));
918 Args.push_back(std::make_pair(Dst, Dst->getType()));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000919 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +0000920 ImplicitParamDecl::Create(getContext(), 0,
921 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000922 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000923 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000924
Mike Stumpa4f668f2009-03-06 01:33:24 +0000925 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +0000926 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000927
Mike Stump3899a7f2009-06-05 23:26:36 +0000928 // FIXME: We'd like to put these into a mergable by content, with
929 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000930 CodeGenTypes &Types = CGM.getTypes();
931 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
932
933 llvm::Function *Fn =
934 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000935 "__copy_helper_block_", &CGM.getModule());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000936
937 IdentifierInfo *II
938 = &CGM.getContext().Idents.get("__copy_helper_block_");
939
940 FunctionDecl *FD = FunctionDecl::Create(getContext(),
941 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000942 SourceLocation(), II, R, 0,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000943 FunctionDecl::Static,
944 FunctionDecl::None,
945 false,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000946 true);
947 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump08920992009-03-07 02:35:30 +0000948
949 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
950 llvm::Type *PtrPtrT;
Mike Stump08920992009-03-07 02:35:30 +0000951
Mike Stumpb7477cf2009-04-10 18:52:28 +0000952 if (NoteForHelperp) {
953 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump08920992009-03-07 02:35:30 +0000954
Owen Anderson96e0fc72009-07-29 22:16:19 +0000955 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000956 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
957 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump08920992009-03-07 02:35:30 +0000958
Mike Stumpb7477cf2009-04-10 18:52:28 +0000959 llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000960 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000961 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000962 DstObj = Builder.CreateBitCast(DstObj, PtrPtrT);
963 DstObj = Builder.CreateLoad(DstObj);
Mike Stump08920992009-03-07 02:35:30 +0000964
Mike Stumpb7477cf2009-04-10 18:52:28 +0000965 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
966 int flag = NoteForHelper[i].flag;
967 int index = NoteForHelper[i].index;
Mike Stump08920992009-03-07 02:35:30 +0000968
Mike Stumpb7477cf2009-04-10 18:52:28 +0000969 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
970 || NoteForHelper[i].RequiresCopying) {
971 llvm::Value *Srcv = SrcObj;
972 Srcv = Builder.CreateStructGEP(Srcv, index);
973 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000974 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000975 Srcv = Builder.CreateLoad(Srcv);
976
977 llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
978 Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
979
Chris Lattner77b89b82010-06-27 07:15:29 +0000980 llvm::Value *N = llvm::ConstantInt::get(CGF.Int32Ty, flag);
Daniel Dunbar673431a2010-07-16 00:00:15 +0000981 llvm::Value *F = CGM.getBlockObjectAssign();
Mike Stumpb7477cf2009-04-10 18:52:28 +0000982 Builder.CreateCall3(F, Dstv, Srcv, N);
983 }
Mike Stump08920992009-03-07 02:35:30 +0000984 }
985 }
986
Mike Stumpa4f668f2009-03-06 01:33:24 +0000987 CGF.FinishFunction();
988
Owen Anderson3c4972d2009-07-29 18:54:39 +0000989 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000990}
991
Mike Stumpcf62d392009-03-06 18:42:23 +0000992llvm::Constant *BlockFunction::
Mike Stump08920992009-03-07 02:35:30 +0000993GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
994 const llvm::StructType* T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000995 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000996 QualType R = getContext().VoidTy;
997
998 FunctionArgList Args;
999 // FIXME: This leaks
1000 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001001 ImplicitParamDecl::Create(getContext(), 0,
1002 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +00001003 getContext().getPointerType(getContext().VoidTy));
1004
1005 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Mike Stumpa4f668f2009-03-06 01:33:24 +00001007 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +00001008 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stumpa4f668f2009-03-06 01:33:24 +00001009
Mike Stump3899a7f2009-06-05 23:26:36 +00001010 // FIXME: We'd like to put these into a mergable by content, with
1011 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +00001012 CodeGenTypes &Types = CGM.getTypes();
1013 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1014
1015 llvm::Function *Fn =
1016 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001017 "__destroy_helper_block_", &CGM.getModule());
Mike Stumpa4f668f2009-03-06 01:33:24 +00001018
1019 IdentifierInfo *II
1020 = &CGM.getContext().Idents.get("__destroy_helper_block_");
1021
1022 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1023 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001024 SourceLocation(), II, R, 0,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001025 FunctionDecl::Static,
1026 FunctionDecl::None,
1027 false, true);
Mike Stumpa4f668f2009-03-06 01:33:24 +00001028 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1edf6b62009-03-07 02:53:18 +00001029
Mike Stumpb7477cf2009-04-10 18:52:28 +00001030 if (NoteForHelperp) {
1031 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump1edf6b62009-03-07 02:53:18 +00001032
Mike Stumpb7477cf2009-04-10 18:52:28 +00001033 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
1034 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +00001035 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +00001036 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
1037 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump1edf6b62009-03-07 02:53:18 +00001038
Mike Stumpb7477cf2009-04-10 18:52:28 +00001039 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
1040 int flag = NoteForHelper[i].flag;
1041 int index = NoteForHelper[i].index;
Mike Stump1edf6b62009-03-07 02:53:18 +00001042
Mike Stumpb7477cf2009-04-10 18:52:28 +00001043 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
1044 || NoteForHelper[i].RequiresCopying) {
1045 llvm::Value *Srcv = SrcObj;
1046 Srcv = Builder.CreateStructGEP(Srcv, index);
1047 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001048 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +00001049 Srcv = Builder.CreateLoad(Srcv);
1050
1051 BuildBlockRelease(Srcv, flag);
1052 }
Mike Stump1edf6b62009-03-07 02:53:18 +00001053 }
1054 }
1055
Mike Stumpa4f668f2009-03-06 01:33:24 +00001056 CGF.FinishFunction();
1057
Owen Anderson3c4972d2009-07-29 18:54:39 +00001058 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpa4f668f2009-03-06 01:33:24 +00001059}
1060
Mike Stump08920992009-03-07 02:35:30 +00001061llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001062 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump08920992009-03-07 02:35:30 +00001063 return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
1064 T, NoteForHelper);
Mike Stumpa4f668f2009-03-06 01:33:24 +00001065}
1066
Mike Stump08920992009-03-07 02:35:30 +00001067llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001068 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stump08920992009-03-07 02:35:30 +00001069 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001070 T, NoteForHelperp);
Mike Stumpdab514f2009-03-04 03:23:46 +00001071}
Mike Stump797b6322009-03-05 01:23:13 +00001072
Mike Stumpee094222009-03-06 06:12:24 +00001073llvm::Constant *BlockFunction::
1074GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
Mike Stump45031c02009-03-06 02:29:21 +00001075 QualType R = getContext().VoidTy;
1076
1077 FunctionArgList Args;
1078 // FIXME: This leaks
Mike Stumpee094222009-03-06 06:12:24 +00001079 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +00001080 ImplicitParamDecl::Create(getContext(), 0,
1081 SourceLocation(), 0,
Mike Stumpee094222009-03-06 06:12:24 +00001082 getContext().getPointerType(getContext().VoidTy));
1083 Args.push_back(std::make_pair(Dst, Dst->getType()));
1084
1085 // FIXME: This leaks
Mike Stump45031c02009-03-06 02:29:21 +00001086 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001087 ImplicitParamDecl::Create(getContext(), 0,
1088 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001089 getContext().getPointerType(getContext().VoidTy));
Mike Stump45031c02009-03-06 02:29:21 +00001090 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Mike Stump45031c02009-03-06 02:29:21 +00001092 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +00001093 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stump45031c02009-03-06 02:29:21 +00001094
Mike Stump45031c02009-03-06 02:29:21 +00001095 CodeGenTypes &Types = CGM.getTypes();
1096 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1097
Mike Stump3899a7f2009-06-05 23:26:36 +00001098 // FIXME: We'd like to put these into a mergable by content, with
1099 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001100 llvm::Function *Fn =
1101 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001102 "__Block_byref_id_object_copy_", &CGM.getModule());
Mike Stump45031c02009-03-06 02:29:21 +00001103
1104 IdentifierInfo *II
1105 = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
1106
1107 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1108 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001109 SourceLocation(), II, R, 0,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001110 FunctionDecl::Static,
1111 FunctionDecl::None,
1112 false, true);
Mike Stump45031c02009-03-06 02:29:21 +00001113 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpee094222009-03-06 06:12:24 +00001114
1115 // dst->x
1116 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001117 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001118 V = Builder.CreateLoad(V);
Mike Stumpee094222009-03-06 06:12:24 +00001119 V = Builder.CreateStructGEP(V, 6, "x");
1120 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
1121
1122 // src->x
1123 V = CGF.GetAddrOfLocalVar(Src);
1124 V = Builder.CreateLoad(V);
1125 V = Builder.CreateBitCast(V, T);
1126 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001127 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpee094222009-03-06 06:12:24 +00001128 llvm::Value *SrcObj = Builder.CreateLoad(V);
Mike Stump1eb44332009-09-09 15:08:12 +00001129
Mike Stumpee094222009-03-06 06:12:24 +00001130 flag |= BLOCK_BYREF_CALLER;
1131
Chris Lattner77b89b82010-06-27 07:15:29 +00001132 llvm::Value *N = llvm::ConstantInt::get(CGF.Int32Ty, flag);
Daniel Dunbar673431a2010-07-16 00:00:15 +00001133 llvm::Value *F = CGM.getBlockObjectAssign();
Mike Stumpee094222009-03-06 06:12:24 +00001134 Builder.CreateCall3(F, DstObj, SrcObj, N);
1135
Mike Stump45031c02009-03-06 02:29:21 +00001136 CGF.FinishFunction();
1137
Owen Anderson3c4972d2009-07-29 18:54:39 +00001138 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001139}
1140
Mike Stump1851b682009-03-06 04:53:30 +00001141llvm::Constant *
1142BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
1143 int flag) {
Mike Stump45031c02009-03-06 02:29:21 +00001144 QualType R = getContext().VoidTy;
1145
1146 FunctionArgList Args;
1147 // FIXME: This leaks
1148 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001149 ImplicitParamDecl::Create(getContext(), 0,
1150 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001151 getContext().getPointerType(getContext().VoidTy));
1152
1153 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Mike Stump45031c02009-03-06 02:29:21 +00001155 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +00001156 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stump45031c02009-03-06 02:29:21 +00001157
Mike Stump45031c02009-03-06 02:29:21 +00001158 CodeGenTypes &Types = CGM.getTypes();
1159 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1160
Mike Stump3899a7f2009-06-05 23:26:36 +00001161 // FIXME: We'd like to put these into a mergable by content, with
1162 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001163 llvm::Function *Fn =
1164 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001165 "__Block_byref_id_object_dispose_",
Mike Stump45031c02009-03-06 02:29:21 +00001166 &CGM.getModule());
1167
1168 IdentifierInfo *II
1169 = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
1170
1171 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1172 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001173 SourceLocation(), II, R, 0,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001174 FunctionDecl::Static,
1175 FunctionDecl::None,
1176 false, true);
Mike Stump45031c02009-03-06 02:29:21 +00001177 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1851b682009-03-06 04:53:30 +00001178
1179 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001180 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001181 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001182 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001183 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001184 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001185
Mike Stump1851b682009-03-06 04:53:30 +00001186 flag |= BLOCK_BYREF_CALLER;
1187 BuildBlockRelease(V, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001188 CGF.FinishFunction();
1189
Owen Anderson3c4972d2009-07-29 18:54:39 +00001190 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001191}
1192
Mike Stumpee094222009-03-06 06:12:24 +00001193llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001194 int Flag, unsigned Align) {
1195 // All alignments below that of pointer alignment collapse down to just
Mike Stump3899a7f2009-06-05 23:26:36 +00001196 // pointer alignment, as we always have at least that much alignment to begin
1197 // with.
1198 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001199
Mike Stump3899a7f2009-06-05 23:26:36 +00001200 // As an optimization, we only generate a single function of each kind we
1201 // might need. We need a different one for each alignment and for each
1202 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001203 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1204 llvm::Constant *&Entry = CGM.AssignCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001205 if (Entry)
1206 return Entry;
Chris Lattner10976d92009-12-05 08:21:30 +00001207 return Entry = CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, Flag);
Mike Stump45031c02009-03-06 02:29:21 +00001208}
1209
Mike Stump1851b682009-03-06 04:53:30 +00001210llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001211 int Flag,
Mike Stump3899a7f2009-06-05 23:26:36 +00001212 unsigned Align) {
1213 // All alignments below that of pointer alignment collpase down to just
1214 // pointer alignment, as we always have at least that much alignment to begin
1215 // with.
1216 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001217
Mike Stump3899a7f2009-06-05 23:26:36 +00001218 // As an optimization, we only generate a single function of each kind we
1219 // might need. We need a different one for each alignment and for each
1220 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001221 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1222 llvm::Constant *&Entry = CGM.DestroyCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001223 if (Entry)
1224 return Entry;
Chris Lattner10976d92009-12-05 08:21:30 +00001225 return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, Flag);
Mike Stump45031c02009-03-06 02:29:21 +00001226}
1227
Mike Stump1851b682009-03-06 04:53:30 +00001228void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Daniel Dunbar673431a2010-07-16 00:00:15 +00001229 llvm::Value *F = CGM.getBlockObjectDispose();
Mike Stump1851b682009-03-06 04:53:30 +00001230 llvm::Value *N;
Mike Stump797b6322009-03-05 01:23:13 +00001231 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Chris Lattner77b89b82010-06-27 07:15:29 +00001232 N = llvm::ConstantInt::get(CGF.Int32Ty, flag);
Mike Stump797b6322009-03-05 01:23:13 +00001233 Builder.CreateCall2(F, V, N);
1234}
Mike Stump00470a12009-03-05 08:32:30 +00001235
1236ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stump08920992009-03-07 02:35:30 +00001237
1238BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1239 CGBuilderTy &B)
Chris Lattner77b89b82010-06-27 07:15:29 +00001240 : CGM(cgm), VMContext(cgm.getLLVMContext()), CGF(cgf), Builder(B) {
Owen Anderson0032b272009-08-13 21:57:51 +00001241 PtrToInt8Ty = llvm::PointerType::getUnqual(
1242 llvm::Type::getInt8Ty(VMContext));
Mike Stump08920992009-03-07 02:35:30 +00001243
1244 BlockHasCopyDispose = false;
1245}