blob: 33646871e087f0ace363210a414b0e01f1c689cc [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,
Fariborz Jahanian44034db2010-08-04 18:44:59 +000039 llvm::Constant *BlockVarLayout,
Mike Stump08920992009-03-07 02:35:30 +000040 std::vector<HelperInfo> *NoteForHelper) {
Fariborz Jahanian89ecd412010-08-04 16:57:49 +000041 bool BlockHasCopyDispose = Info.BlockHasCopyDispose;
42 CharUnits Size = Info.BlockSize;
Mike Stump56129b12009-02-13 16:55:51 +000043 const llvm::Type *UnsignedLongTy
44 = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpe5fee252009-02-13 16:19:19 +000045 llvm::Constant *C;
46 std::vector<llvm::Constant*> Elts;
47
48 // reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +000049 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpe5fee252009-02-13 16:19:19 +000050 Elts.push_back(C);
51
52 // Size
Mike Stumpd6840002009-02-21 20:07:44 +000053 // FIXME: What is the right way to say this doesn't fit? We should give
54 // a user diagnostic in that case. Better fix would be to change the
55 // API to size_t.
Ken Dyck199c3d62010-01-11 17:06:35 +000056 C = llvm::ConstantInt::get(UnsignedLongTy, Size.getQuantity());
Mike Stumpe5fee252009-02-13 16:19:19 +000057 Elts.push_back(C);
58
Blaine Garst2a7eb282010-02-23 21:51:17 +000059 // optional copy/dispose helpers
Mike Stumpe5fee252009-02-13 16:19:19 +000060 if (BlockHasCopyDispose) {
61 // copy_func_helper_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000062 Elts.push_back(BuildCopyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000063
64 // destroy_func_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000065 Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000066 }
67
Blaine Garst2a7eb282010-02-23 21:51:17 +000068 // Signature. non-optional ObjC-style method descriptor @encode sequence
69 std::string BlockTypeEncoding;
70 CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
71
72 Elts.push_back(llvm::ConstantExpr::getBitCast(
73 CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty));
74
75 // Layout.
Fariborz Jahanian44034db2010-08-04 18:44:59 +000076 C = BlockVarLayout;
77
Blaine Garst2a7eb282010-02-23 21:51:17 +000078 Elts.push_back(C);
79
Nick Lewycky0d36dd22009-09-19 20:00:52 +000080 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stumpe5fee252009-02-13 16:19:19 +000081
Owen Anderson1c431b32009-07-08 19:05:04 +000082 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Mike Stumpe5fee252009-02-13 16:19:19 +000083 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +000084 C, "__block_descriptor_tmp");
Mike Stumpe5fee252009-02-13 16:19:19 +000085 return C;
86}
87
John McCallee504292010-05-21 04:11:14 +000088static void CollectBlockDeclRefInfo(const Stmt *S, CGBlockInfo &Info) {
Anders Carlsson4de9fce2009-03-01 01:09:12 +000089 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
90 I != E; ++I)
Daniel Dunbar82573ee2009-03-02 07:00:57 +000091 if (*I)
John McCallee504292010-05-21 04:11:14 +000092 CollectBlockDeclRefInfo(*I, Info);
Mike Stump00470a12009-03-05 08:32:30 +000093
Mike Stumpea26cb52009-10-21 03:49:08 +000094 // We want to ensure we walk down into block literals so we can find
95 // all nested BlockDeclRefExprs.
Mike Stump38e16272009-10-21 22:01:24 +000096 if (const BlockExpr *BE = dyn_cast<BlockExpr>(S)) {
John McCallee504292010-05-21 04:11:14 +000097 Info.InnerBlocks.insert(BE->getBlockDecl());
98 CollectBlockDeclRefInfo(BE->getBody(), Info);
Mike Stump38e16272009-10-21 22:01:24 +000099 }
Mike Stumpea26cb52009-10-21 03:49:08 +0000100
John McCallee504292010-05-21 04:11:14 +0000101 else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) {
102 const ValueDecl *D = BDRE->getDecl();
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000103 // FIXME: Handle enums.
John McCallee504292010-05-21 04:11:14 +0000104 if (isa<FunctionDecl>(D))
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000105 return;
Mike Stump00470a12009-03-05 08:32:30 +0000106
John McCallee504292010-05-21 04:11:14 +0000107 if (isa<ImplicitParamDecl>(D) &&
108 isa<ObjCMethodDecl>(D->getDeclContext()) &&
109 cast<ObjCMethodDecl>(D->getDeclContext())->getSelfDecl() == D) {
110 Info.NeedsObjCSelf = true;
111 return;
112 }
113
Mike Stump38e16272009-10-21 22:01:24 +0000114 // Only Decls that escape are added.
John McCallee504292010-05-21 04:11:14 +0000115 if (!Info.InnerBlocks.count(D->getDeclContext()))
Mike Stump38e16272009-10-21 22:01:24 +0000116 Info.DeclRefs.push_back(BDRE);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000117 }
John McCallea1471e2010-05-20 01:18:31 +0000118
John McCallee504292010-05-21 04:11:14 +0000119 // Make sure to capture implicit 'self' references due to super calls.
Chandler Carruthf54b80f2010-05-21 10:29:28 +0000120 else if (const ObjCMessageExpr *E = dyn_cast<ObjCMessageExpr>(S)) {
John McCallee504292010-05-21 04:11:14 +0000121 if (E->getReceiverKind() == ObjCMessageExpr::SuperClass ||
122 E->getReceiverKind() == ObjCMessageExpr::SuperInstance)
123 Info.NeedsObjCSelf = true;
Chandler Carruthf54b80f2010-05-21 10:29:28 +0000124 }
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000125 else if (const ObjCPropertyRefExpr *PE = dyn_cast<ObjCPropertyRefExpr>(S)) {
126 // Getter/setter uses may also cause implicit super references,
127 // which we can check for with:
128 if (PE->isSuperReceiver())
129 Info.NeedsObjCSelf = true;
130 }
131 else if (const ObjCImplicitSetterGetterRefExpr *IE =
132 dyn_cast<ObjCImplicitSetterGetterRefExpr>(S)) {
133 // Getter/setter uses may also cause implicit super references,
134 // which we can check for with:
135 if (IE->isSuperReceiver())
136 Info.NeedsObjCSelf = true;
137 }
John McCallee504292010-05-21 04:11:14 +0000138 else if (isa<CXXThisExpr>(S))
John McCallea1471e2010-05-20 01:18:31 +0000139 Info.CXXThisRef = cast<CXXThisExpr>(S);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000140}
141
John McCallee504292010-05-21 04:11:14 +0000142/// CanBlockBeGlobal - Given a CGBlockInfo struct, determines if a block can be
Mike Stump58a85142009-03-04 22:48:06 +0000143/// declared as a global variable instead of on the stack.
John McCallee504292010-05-21 04:11:14 +0000144static bool CanBlockBeGlobal(const CGBlockInfo &Info) {
Mike Stumpea26cb52009-10-21 03:49:08 +0000145 return Info.DeclRefs.empty();
146}
147
148/// AllocateAllBlockDeclRefs - Preallocate all nested BlockDeclRefExprs to
149/// ensure we can generate the debug information for the parameter for the block
150/// invoke function.
John McCallee504292010-05-21 04:11:14 +0000151static void AllocateAllBlockDeclRefs(CodeGenFunction &CGF, CGBlockInfo &Info) {
John McCallea1471e2010-05-20 01:18:31 +0000152 if (Info.CXXThisRef)
John McCallee504292010-05-21 04:11:14 +0000153 CGF.AllocateBlockCXXThisPointer(Info.CXXThisRef);
Mike Stumpea26cb52009-10-21 03:49:08 +0000154
155 for (size_t i = 0; i < Info.DeclRefs.size(); ++i)
John McCallee504292010-05-21 04:11:14 +0000156 CGF.AllocateBlockDecl(Info.DeclRefs[i]);
157
158 if (Info.NeedsObjCSelf) {
159 ValueDecl *Self = cast<ObjCMethodDecl>(CGF.CurFuncDecl)->getSelfDecl();
160 BlockDeclRefExpr *BDRE =
161 new (CGF.getContext()) BlockDeclRefExpr(Self, Self->getType(),
162 SourceLocation(), false);
163 Info.DeclRefs.push_back(BDRE);
164 CGF.AllocateBlockDecl(BDRE);
165 }
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000166}
167
Fariborz Jahanian7edddb82010-07-28 19:07:18 +0000168static unsigned computeBlockFlag(CodeGenModule &CGM,
169 const BlockExpr *BE, unsigned flags) {
170 QualType BPT = BE->getType();
171 const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>();
172 QualType ResultType = ftype->getResultType();
173
174 CallArgList Args;
175 CodeGenTypes &Types = CGM.getTypes();
176 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, Args,
177 FunctionType::ExtInfo());
178 if (CGM.ReturnTypeUsesSRet(FnInfo))
179 flags |= CodeGenFunction::BLOCK_USE_STRET;
180 return flags;
181}
182
Mike Stump58a85142009-03-04 22:48:06 +0000183// FIXME: Push most into CGM, passing down a few bits, like current function
184// name.
Mike Stump8a2b4b12009-02-25 23:33:13 +0000185llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000186 std::string Name = CurFn->getName();
John McCallee504292010-05-21 04:11:14 +0000187 CGBlockInfo Info(Name.c_str());
188 Info.InnerBlocks.insert(BE->getBlockDecl());
189 CollectBlockDeclRefInfo(BE->getBody(), Info);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000190
191 // Check if the block can be global.
Mike Stump58a85142009-03-04 22:48:06 +0000192 // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like
193 // to just have one code path. We should move this function into CGM and pass
194 // CGF, then we can just check to see if CGF is 0.
Mike Stumpdab514f2009-03-04 03:23:46 +0000195 if (0 && CanBlockBeGlobal(Info))
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000196 return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
Mike Stump00470a12009-03-05 08:32:30 +0000197
David Chisnall5e530af2009-11-17 19:33:30 +0000198 size_t BlockFields = 5;
199
David Chisnall5e530af2009-11-17 19:33:30 +0000200 std::vector<llvm::Constant*> Elts(BlockFields);
201
Mike Stumpe5fee252009-02-13 16:19:19 +0000202 llvm::Constant *C;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000203 llvm::Value *V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000204
Mike Stumpe5fee252009-02-13 16:19:19 +0000205 {
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000206 llvm::Constant *BlockVarLayout;
Mike Stumpe5fee252009-02-13 16:19:19 +0000207 // C = BuildBlockStructInitlist();
Blaine Garsta36e2232010-03-05 01:29:59 +0000208 unsigned int flags = BLOCK_HAS_SIGNATURE;
David Chisnall5e530af2009-11-17 19:33:30 +0000209
Mike Stump00470a12009-03-05 08:32:30 +0000210 // We run this first so that we set BlockHasCopyDispose from the entire
211 // block literal.
212 // __invoke
Mike Stump00470a12009-03-05 08:32:30 +0000213 llvm::Function *Fn
Fariborz Jahanian564360b2010-06-24 00:08:06 +0000214 = CodeGenFunction(CGM).GenerateBlockFunction(CurGD, BE, Info, CurFuncDecl,
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000215 BlockVarLayout,
John McCallee504292010-05-21 04:11:14 +0000216 LocalDeclMap);
217 BlockHasCopyDispose |= Info.BlockHasCopyDispose;
Mike Stump00470a12009-03-05 08:32:30 +0000218 Elts[3] = Fn;
219
Mike Stumpf5408fe2009-05-16 07:57:57 +0000220 // FIXME: Don't use BlockHasCopyDispose, it is set more often then
221 // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); }
John McCallee504292010-05-21 04:11:14 +0000222 if (Info.BlockHasCopyDispose)
Mike Stumpe5fee252009-02-13 16:19:19 +0000223 flags |= BLOCK_HAS_COPY_DISPOSE;
224
Mike Stump7d6dc4f2009-02-13 20:17:16 +0000225 // __isa
Mike Stump59c5b112009-02-13 19:29:27 +0000226 C = CGM.getNSConcreteStackBlock();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000227 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
Mike Stump00470a12009-03-05 08:32:30 +0000228 Elts[0] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000229
230 // __flags
Fariborz Jahanian7edddb82010-07-28 19:07:18 +0000231 flags = computeBlockFlag(CGM, BE, flags);
Mike Stumpe5fee252009-02-13 16:19:19 +0000232 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
233 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000234 C = llvm::ConstantInt::get(IntTy, flags);
Mike Stump00470a12009-03-05 08:32:30 +0000235 Elts[1] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000236
237 // __reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000238 C = llvm::ConstantInt::get(IntTy, 0);
Mike Stump00470a12009-03-05 08:32:30 +0000239 Elts[2] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000240
John McCallee504292010-05-21 04:11:14 +0000241 if (Info.BlockLayout.empty()) {
Mike Stumpcf62d392009-03-06 18:42:23 +0000242 // __descriptor
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000243 C = llvm::Constant::getNullValue(PtrToInt8Ty);
244 Elts[4] = BuildDescriptorBlockDecl(BE, Info, 0, C, 0);
Mike Stumpcf62d392009-03-06 18:42:23 +0000245
Mike Stump5570cfe2009-03-01 20:07:53 +0000246 // Optimize to being a global block.
247 Elts[0] = CGM.getNSConcreteGlobalBlock();
Blaine Garsta36e2232010-03-05 01:29:59 +0000248
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000249 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
Mike Stump5570cfe2009-03-01 20:07:53 +0000250
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000251 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000252
Owen Anderson1c431b32009-07-08 19:05:04 +0000253 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000254 llvm::GlobalValue::InternalLinkage, C,
255 "__block_holder_tmp_" +
256 llvm::Twine(CGM.getGlobalUniqueCount()));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000257 QualType BPT = BE->getType();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000258 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000259 return C;
260 }
Mike Stump00470a12009-03-05 08:32:30 +0000261
John McCallee504292010-05-21 04:11:14 +0000262 std::vector<const llvm::Type *> Types(BlockFields+Info.BlockLayout.size());
Mike Stump08920992009-03-07 02:35:30 +0000263 for (int i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000264 Types[i] = Elts[i]->getType();
Mike Stump08920992009-03-07 02:35:30 +0000265 Types[4] = PtrToInt8Ty;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000266
John McCallee504292010-05-21 04:11:14 +0000267 for (unsigned i = 0, n = Info.BlockLayout.size(); i != n; ++i) {
268 const Expr *E = Info.BlockLayout[i];
Mike Stumpa99038c2009-02-28 09:07:16 +0000269 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
270 QualType Ty = E->getType();
Mike Stumpdab514f2009-03-04 03:23:46 +0000271 if (BDRE && BDRE->isByRef()) {
Fariborz Jahanian79651722010-06-02 21:35:17 +0000272 Types[i+BlockFields] =
273 llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0);
274 } else if (BDRE && BDRE->getDecl()->getType()->isReferenceType()) {
275 Types[i+BlockFields] = llvm::PointerType::get(ConvertType(Ty), 0);
276 } else
David Chisnall5e530af2009-11-17 19:33:30 +0000277 Types[i+BlockFields] = ConvertType(Ty);
Mike Stumpa99038c2009-02-28 09:07:16 +0000278 }
Mike Stump8a2b4b12009-02-25 23:33:13 +0000279
Owen Anderson47a434f2009-08-05 23:18:46 +0000280 llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000281
282 llvm::AllocaInst *A = CreateTempAlloca(Ty);
John McCallee504292010-05-21 04:11:14 +0000283 A->setAlignment(Info.BlockAlign.getQuantity());
Mike Stump8a2b4b12009-02-25 23:33:13 +0000284 V = A;
285
John McCallea1471e2010-05-20 01:18:31 +0000286 // Build layout / cleanup information for all the data entries in the
287 // layout, and write the enclosing fields into the type.
John McCallee504292010-05-21 04:11:14 +0000288 std::vector<HelperInfo> NoteForHelper(Info.BlockLayout.size());
John McCallea1471e2010-05-20 01:18:31 +0000289 unsigned NumHelpers = 0;
Mike Stump08920992009-03-07 02:35:30 +0000290
291 for (unsigned i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000292 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
Mike Stump00470a12009-03-05 08:32:30 +0000293
John McCallee504292010-05-21 04:11:14 +0000294 for (unsigned i=0; i < Info.BlockLayout.size(); ++i) {
295 const Expr *E = Info.BlockLayout[i];
Mike Stump8a2b4b12009-02-25 23:33:13 +0000296
John McCallea1471e2010-05-20 01:18:31 +0000297 // Skip padding.
298 if (isa<DeclRefExpr>(E)) continue;
Mike Stumpa99038c2009-02-28 09:07:16 +0000299
John McCallea1471e2010-05-20 01:18:31 +0000300 llvm::Value* Addr = Builder.CreateStructGEP(V, i+BlockFields, "tmp");
301 HelperInfo &Note = NoteForHelper[NumHelpers++];
Mike Stumpa99038c2009-02-28 09:07:16 +0000302
John McCallea1471e2010-05-20 01:18:31 +0000303 Note.index = i+5;
Mike Stump08920992009-03-07 02:35:30 +0000304
John McCallea1471e2010-05-20 01:18:31 +0000305 if (isa<CXXThisExpr>(E)) {
306 Note.RequiresCopying = false;
307 Note.flag = BLOCK_FIELD_IS_OBJECT;
Mike Stumpa99038c2009-02-28 09:07:16 +0000308
John McCallea1471e2010-05-20 01:18:31 +0000309 Builder.CreateStore(LoadCXXThis(), Addr);
310 continue;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000311 }
John McCallea1471e2010-05-20 01:18:31 +0000312
313 const BlockDeclRefExpr *BDRE = cast<BlockDeclRefExpr>(E);
Fariborz Jahaniane27e9d62010-11-11 00:11:38 +0000314 Note.RequiresCopying = BlockRequiresCopying(BDRE);
315
John McCallea1471e2010-05-20 01:18:31 +0000316 const ValueDecl *VD = BDRE->getDecl();
317 QualType T = VD->getType();
John McCallea1471e2010-05-20 01:18:31 +0000318 if (BDRE->isByRef()) {
319 Note.flag = BLOCK_FIELD_IS_BYREF;
320 if (T.isObjCGCWeak())
321 Note.flag |= BLOCK_FIELD_IS_WEAK;
322 } else if (T->isBlockPointerType()) {
323 Note.flag = BLOCK_FIELD_IS_BLOCK;
324 } else {
325 Note.flag = BLOCK_FIELD_IS_OBJECT;
326 }
327
328 if (LocalDeclMap[VD]) {
329 if (BDRE->isByRef()) {
330 llvm::Value *Loc = LocalDeclMap[VD];
331 Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
332 Loc = Builder.CreateLoad(Loc);
333 Builder.CreateStore(Loc, Addr);
334 continue;
335 } else {
Fariborz Jahanianac7362d2010-06-08 20:57:22 +0000336 if (BDRE->getCopyConstructorExpr()) {
337 E = BDRE->getCopyConstructorExpr();
John McCallf1549f62010-07-06 01:34:17 +0000338 PushDestructorCleanup(E->getType(), Addr);
Fariborz Jahanianac7362d2010-06-08 20:57:22 +0000339 }
Fariborz Jahanian59da45a2010-06-04 21:35:44 +0000340 else {
341 E = new (getContext()) DeclRefExpr(const_cast<ValueDecl*>(VD),
Fariborz Jahanian79651722010-06-02 21:35:17 +0000342 VD->getType().getNonReferenceType(),
343 SourceLocation());
Fariborz Jahanian59da45a2010-06-04 21:35:44 +0000344 if (VD->getType()->isReferenceType()) {
345 E = new (getContext())
John McCall2de56d12010-08-25 11:45:40 +0000346 UnaryOperator(const_cast<Expr*>(E), UO_AddrOf,
Fariborz Jahanian59da45a2010-06-04 21:35:44 +0000347 getContext().getPointerType(E->getType()),
348 SourceLocation());
349 }
Fariborz Jahaniandf8b8ea2010-06-04 16:10:00 +0000350 }
351 }
John McCallea1471e2010-05-20 01:18:31 +0000352 }
John McCallea1471e2010-05-20 01:18:31 +0000353
354 if (BDRE->isByRef()) {
355 E = new (getContext())
John McCall2de56d12010-08-25 11:45:40 +0000356 UnaryOperator(const_cast<Expr*>(E), UO_AddrOf,
John McCallea1471e2010-05-20 01:18:31 +0000357 getContext().getPointerType(E->getType()),
358 SourceLocation());
359 }
360
John McCall558d2ab2010-09-15 10:14:12 +0000361 RValue r = EmitAnyExpr(E, AggValueSlot::forAddr(Addr, false, true));
John McCallea1471e2010-05-20 01:18:31 +0000362 if (r.isScalar()) {
363 llvm::Value *Loc = r.getScalarVal();
364 const llvm::Type *Ty = Types[i+BlockFields];
365 if (BDRE->isByRef()) {
366 // E is now the address of the value field, instead, we want the
367 // address of the actual ByRef struct. We optimize this slightly
368 // compared to gcc by not grabbing the forwarding slot as this must
369 // be done during Block_copy for us, and we can postpone the work
370 // until then.
371 CharUnits offset = BlockDecls[BDRE->getDecl()];
372
373 llvm::Value *BlockLiteral = LoadBlockStruct();
374
375 Loc = Builder.CreateGEP(BlockLiteral,
Chris Lattner77b89b82010-06-27 07:15:29 +0000376 llvm::ConstantInt::get(Int64Ty, offset.getQuantity()),
John McCallea1471e2010-05-20 01:18:31 +0000377 "block.literal");
378 Ty = llvm::PointerType::get(Ty, 0);
379 Loc = Builder.CreateBitCast(Loc, Ty);
380 Loc = Builder.CreateLoad(Loc);
381 // Loc = Builder.CreateBitCast(Loc, Ty);
382 }
383 Builder.CreateStore(Loc, Addr);
384 } else if (r.isComplex())
385 // FIXME: implement
386 ErrorUnsupported(BE, "complex in block literal");
387 else if (r.isAggregate())
388 ; // Already created into the destination
389 else
390 assert (0 && "bad block variable");
391 // FIXME: Ensure that the offset created by the backend for
392 // the struct matches the previously computed offset in BlockDecls.
393 }
394 NoteForHelper.resize(NumHelpers);
Mike Stumpcf62d392009-03-06 18:42:23 +0000395
396 // __descriptor
Fariborz Jahanian89ecd412010-08-04 16:57:49 +0000397 llvm::Value *Descriptor = BuildDescriptorBlockDecl(BE, Info, Ty,
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000398 BlockVarLayout,
Mike Stump08920992009-03-07 02:35:30 +0000399 &NoteForHelper);
400 Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
401 Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
Mike Stumpe5fee252009-02-13 16:19:19 +0000402 }
Mike Stump00470a12009-03-05 08:32:30 +0000403
Mike Stumpbd65cac2009-02-19 01:01:04 +0000404 QualType BPT = BE->getType();
Fariborz Jahanian263c4de2010-02-10 23:34:57 +0000405 V = Builder.CreateBitCast(V, ConvertType(BPT));
406 // See if this is a __weak block variable and the must call objc_read_weak
407 // on it.
408 const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>();
409 QualType RES = ftype->getResultType();
410 if (RES.isObjCGCWeak()) {
411 // Must cast argument to id*
412 const llvm::Type *ObjectPtrTy =
413 ConvertType(CGM.getContext().getObjCIdType());
414 const llvm::Type *PtrObjectPtrTy =
415 llvm::PointerType::getUnqual(ObjectPtrTy);
416 V = Builder.CreateBitCast(V, PtrObjectPtrTy);
417 V = CGM.getObjCRuntime().EmitObjCWeakRead(*this, V);
418 }
419 return V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000420}
421
422
Mike Stump2a998142009-03-04 18:17:45 +0000423const llvm::Type *BlockModule::getBlockDescriptorType() {
Mike Stumpab695142009-02-13 15:16:56 +0000424 if (BlockDescriptorType)
425 return BlockDescriptorType;
426
Mike Stumpa5448542009-02-13 15:32:32 +0000427 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000428 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000429
Mike Stumpab695142009-02-13 15:16:56 +0000430 // struct __block_descriptor {
431 // unsigned long reserved;
432 // unsigned long block_size;
Blaine Garst2a7eb282010-02-23 21:51:17 +0000433 //
434 // // later, the following will be added
435 //
436 // struct {
437 // void (*copyHelper)();
438 // void (*copyHelper)();
439 // } helpers; // !!! optional
440 //
441 // const char *signature; // the block signature
442 // const char *layout; // reserved
Mike Stumpab695142009-02-13 15:16:56 +0000443 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000444 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(),
445 UnsignedLongTy,
Mike Stumpa5448542009-02-13 15:32:32 +0000446 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000447 NULL);
448
449 getModule().addTypeName("struct.__block_descriptor",
450 BlockDescriptorType);
451
452 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000453}
454
Mike Stump2a998142009-03-04 18:17:45 +0000455const llvm::Type *BlockModule::getGenericBlockLiteralType() {
Mike Stump9b8a7972009-02-13 15:25:34 +0000456 if (GenericBlockLiteralType)
457 return GenericBlockLiteralType;
458
Mike Stumpa5448542009-02-13 15:32:32 +0000459 const llvm::Type *BlockDescPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000460 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000461
Mike Stump7cbb3602009-02-13 16:01:35 +0000462 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
463 getTypes().ConvertType(getContext().IntTy));
464
Mike Stump9b8a7972009-02-13 15:25:34 +0000465 // struct __block_literal_generic {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000466 // void *__isa;
467 // int __flags;
468 // int __reserved;
469 // void (*__invoke)(void *);
470 // struct __block_descriptor *__descriptor;
Mike Stump9b8a7972009-02-13 15:25:34 +0000471 // };
Blaine Garst2a7eb282010-02-23 21:51:17 +0000472 GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
Owen Anderson47a434f2009-08-05 23:18:46 +0000473 PtrToInt8Ty,
Mike Stump7cbb3602009-02-13 16:01:35 +0000474 IntTy,
475 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000476 PtrToInt8Ty,
Mike Stump9b8a7972009-02-13 15:25:34 +0000477 BlockDescPtrTy,
478 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000479
Mike Stump9b8a7972009-02-13 15:25:34 +0000480 getModule().addTypeName("struct.__block_literal_generic",
481 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000482
Mike Stump9b8a7972009-02-13 15:25:34 +0000483 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000484}
485
Mike Stumpbd65cac2009-02-19 01:01:04 +0000486
Anders Carlssona1736c02009-12-24 21:13:40 +0000487RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E,
488 ReturnValueSlot ReturnValue) {
Mike Stumpa5448542009-02-13 15:32:32 +0000489 const BlockPointerType *BPT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000490 E->getCallee()->getType()->getAs<BlockPointerType>();
Mike Stumpa5448542009-02-13 15:32:32 +0000491
Anders Carlssonacfde802009-02-12 00:39:25 +0000492 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
493
494 // Get a pointer to the generic block literal.
495 const llvm::Type *BlockLiteralTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000496 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000497
498 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000499 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000500 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
501
502 // Get the function pointer from the literal.
503 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Anders Carlssonacfde802009-02-12 00:39:25 +0000504
Mike Stumpa5448542009-02-13 15:32:32 +0000505 BlockLiteral =
506 Builder.CreateBitCast(BlockLiteral,
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000507 llvm::Type::getInt8PtrTy(VMContext),
Anders Carlssonacfde802009-02-12 00:39:25 +0000508 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000509
Anders Carlssonacfde802009-02-12 00:39:25 +0000510 // Add the block literal.
511 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
512 CallArgList Args;
513 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000514
Anders Carlsson782f3972009-04-08 23:13:16 +0000515 QualType FnType = BPT->getPointeeType();
516
Anders Carlssonacfde802009-02-12 00:39:25 +0000517 // And the rest of the arguments.
John McCall183700f2009-09-21 23:43:11 +0000518 EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(),
Anders Carlsson782f3972009-04-08 23:13:16 +0000519 E->arg_begin(), E->arg_end());
Mike Stumpa5448542009-02-13 15:32:32 +0000520
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000521 // Load the function.
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000522 llvm::Value *Func = Builder.CreateLoad(FuncPtr, "tmp");
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000523
John McCall04a67a62010-02-05 21:31:56 +0000524 const FunctionType *FuncTy = FnType->getAs<FunctionType>();
525 QualType ResultType = FuncTy->getResultType();
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000526
Mike Stump1eb44332009-09-09 15:08:12 +0000527 const CGFunctionInfo &FnInfo =
Rafael Espindola264ba482010-03-30 20:24:48 +0000528 CGM.getTypes().getFunctionInfo(ResultType, Args,
529 FuncTy->getExtInfo());
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000531 // Cast the function pointer to the right type.
Mike Stump1eb44332009-09-09 15:08:12 +0000532 const llvm::Type *BlockFTy =
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000533 CGM.getTypes().GetFunctionType(FnInfo, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Owen Anderson96e0fc72009-07-29 22:16:19 +0000535 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000536 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000537
Anders Carlssonacfde802009-02-12 00:39:25 +0000538 // And call the block.
Anders Carlssona1736c02009-12-24 21:13:40 +0000539 return EmitCall(FnInfo, Func, ReturnValue, Args);
Anders Carlssonacfde802009-02-12 00:39:25 +0000540}
Anders Carlssond5cab542009-02-12 17:55:02 +0000541
John McCallea1471e2010-05-20 01:18:31 +0000542void CodeGenFunction::AllocateBlockCXXThisPointer(const CXXThisExpr *E) {
543 assert(BlockCXXThisOffset.isZero() && "already computed 'this' pointer");
544
545 // Figure out what the offset is.
546 QualType T = E->getType();
547 std::pair<CharUnits,CharUnits> TypeInfo = getContext().getTypeInfoInChars(T);
548 CharUnits Offset = getBlockOffset(TypeInfo.first, TypeInfo.second);
549
550 BlockCXXThisOffset = Offset;
551 BlockLayout.push_back(E);
552}
553
554void CodeGenFunction::AllocateBlockDecl(const BlockDeclRefExpr *E) {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000555 const ValueDecl *VD = E->getDecl();
John McCallea1471e2010-05-20 01:18:31 +0000556 CharUnits &Offset = BlockDecls[VD];
Mike Stumpdab514f2009-03-04 03:23:46 +0000557
Mike Stumpdab514f2009-03-04 03:23:46 +0000558 // See if we have already allocated an offset for this variable.
John McCallea1471e2010-05-20 01:18:31 +0000559 if (!Offset.isZero())
560 return;
Mike Stumpea26cb52009-10-21 03:49:08 +0000561
562 // Don't run the expensive check, unless we have to.
Mike Stump083c25e2009-10-22 00:49:09 +0000563 if (!BlockHasCopyDispose)
564 if (E->isByRef()
Fariborz Jahaniane27e9d62010-11-11 00:11:38 +0000565 || BlockRequiresCopying(E))
Mike Stump083c25e2009-10-22 00:49:09 +0000566 BlockHasCopyDispose = true;
Mike Stumpea26cb52009-10-21 03:49:08 +0000567
John McCallea1471e2010-05-20 01:18:31 +0000568 const ValueDecl *D = cast<ValueDecl>(E->getDecl());
Mike Stumpea26cb52009-10-21 03:49:08 +0000569
John McCallea1471e2010-05-20 01:18:31 +0000570 CharUnits Size;
571 CharUnits Align;
572
573 if (E->isByRef()) {
574 llvm::tie(Size,Align) =
575 getContext().getTypeInfoInChars(getContext().VoidPtrTy);
576 } else {
577 Size = getContext().getTypeSizeInChars(D->getType());
578 Align = getContext().getDeclAlign(D);
579 }
580
581 Offset = getBlockOffset(Size, Align);
582 BlockLayout.push_back(E);
Mike Stumpea26cb52009-10-21 03:49:08 +0000583}
584
John McCallee504292010-05-21 04:11:14 +0000585llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const ValueDecl *VD,
586 bool IsByRef) {
Fariborz Jahaniandf8b8ea2010-06-04 16:10:00 +0000587
John McCallea1471e2010-05-20 01:18:31 +0000588 CharUnits offset = BlockDecls[VD];
589 assert(!offset.isZero() && "getting address of unallocated decl");
Mike Stumpdab514f2009-03-04 03:23:46 +0000590
591 llvm::Value *BlockLiteral = LoadBlockStruct();
592 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
Chris Lattner77b89b82010-06-27 07:15:29 +0000593 llvm::ConstantInt::get(Int64Ty, offset.getQuantity()),
Mike Stump58a85142009-03-04 22:48:06 +0000594 "block.literal");
John McCallee504292010-05-21 04:11:14 +0000595 if (IsByRef) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000596 const llvm::Type *PtrStructTy
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000597 = llvm::PointerType::get(BuildByRefType(VD), 0);
Mike Stumpa8b60c92009-03-21 21:00:35 +0000598 // The block literal will need a copy/destroy helper.
599 BlockHasCopyDispose = true;
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000600
601 const llvm::Type *Ty = PtrStructTy;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000602 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000603 V = Builder.CreateBitCast(V, Ty);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000604 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000605 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000606 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000607 V = Builder.CreateBitCast(V, PtrStructTy);
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000608 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
609 VD->getNameAsString());
Fariborz Jahaniand33ded52010-05-04 17:59:32 +0000610 if (VD->getType()->isReferenceType())
611 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000612 } else {
John McCall46ec70e2010-10-28 21:37:57 +0000613 const llvm::Type *Ty = CGM.getTypes().ConvertTypeForMem(VD->getType());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000614 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000615 V = Builder.CreateBitCast(V, Ty);
Fariborz Jahanian79651722010-06-02 21:35:17 +0000616 if (VD->getType()->isReferenceType())
Fariborz Jahaniandf8b8ea2010-06-04 16:10:00 +0000617 V = Builder.CreateLoad(V, "ref.tmp");
Mike Stumpdab514f2009-03-04 03:23:46 +0000618 }
619 return V;
620}
621
Mike Stump67a64482009-02-14 22:16:35 +0000622llvm::Constant *
Mike Stump90a90432009-03-04 18:47:42 +0000623BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000624 // Generate the block descriptor.
625 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000626 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
627 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000628
Blaine Garst2a7eb282010-02-23 21:51:17 +0000629 llvm::Constant *DescriptorFields[4];
Mike Stumpa5448542009-02-13 15:32:32 +0000630
Anders Carlssond5cab542009-02-12 17:55:02 +0000631 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000632 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000633
Anders Carlssond5cab542009-02-12 17:55:02 +0000634 // Block literal size. For global blocks we just use the size of the generic
635 // block literal struct.
Ken Dyck687cc4a2010-01-26 13:48:07 +0000636 CharUnits BlockLiteralSize =
637 CGM.GetTargetTypeStoreSize(getGenericBlockLiteralType());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000638 DescriptorFields[1] =
Ken Dyck199c3d62010-01-11 17:06:35 +0000639 llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize.getQuantity());
Blaine Garst2a7eb282010-02-23 21:51:17 +0000640
641 // signature. non-optional ObjC-style method descriptor @encode sequence
642 std::string BlockTypeEncoding;
643 CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
Mike Stumpa5448542009-02-13 15:32:32 +0000644
Blaine Garst2a7eb282010-02-23 21:51:17 +0000645 DescriptorFields[2] = llvm::ConstantExpr::getBitCast(
646 CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty);
647
648 // layout
649 DescriptorFields[3] =
650 llvm::ConstantInt::get(UnsignedLongTy,0);
651
652 // build the structure from the 4 elements
Mike Stumpa5448542009-02-13 15:32:32 +0000653 llvm::Constant *DescriptorStruct =
Blaine Garst2a7eb282010-02-23 21:51:17 +0000654 llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 4, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000655
Anders Carlssond5cab542009-02-12 17:55:02 +0000656 llvm::GlobalVariable *Descriptor =
Owen Anderson1c431b32009-07-08 19:05:04 +0000657 new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000658 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000659 DescriptorStruct, "__block_descriptor_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000660
David Chisnall5e530af2009-11-17 19:33:30 +0000661 int FieldCount = 5;
Anders Carlssond5cab542009-02-12 17:55:02 +0000662 // Generate the constants for the block literal.
David Chisnall5e530af2009-11-17 19:33:30 +0000663
664 std::vector<llvm::Constant*> LiteralFields(FieldCount);
Mike Stumpa5448542009-02-13 15:32:32 +0000665
John McCallee504292010-05-21 04:11:14 +0000666 CGBlockInfo Info(n);
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000667 llvm::Constant *BlockVarLayout;
Mike Stump7f28a9c2009-03-13 23:34:28 +0000668 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000669 llvm::Function *Fn
Fariborz Jahanian7edddb82010-07-28 19:07:18 +0000670 = CodeGenFunction(CGM).GenerateBlockFunction(GlobalDecl(), BE,
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000671 Info, 0, BlockVarLayout,
672 LocalDeclMap);
John McCallee504292010-05-21 04:11:14 +0000673 assert(Info.BlockSize == BlockLiteralSize
Mike Stump4e7a1f72009-02-21 20:00:35 +0000674 && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000675
Anders Carlssond5cab542009-02-12 17:55:02 +0000676 // isa
Daniel Dunbar673431a2010-07-16 00:00:15 +0000677 LiteralFields[0] = CGM.getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000678
Fariborz Jahanian7edddb82010-07-28 19:07:18 +0000679 // __flags
680 unsigned flags = computeBlockFlag(CGM, BE,
681 (BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE));
Blaine Garst2a7eb282010-02-23 21:51:17 +0000682 LiteralFields[1] =
Fariborz Jahanian7edddb82010-07-28 19:07:18 +0000683 llvm::ConstantInt::get(IntTy, flags);
Mike Stumpa5448542009-02-13 15:32:32 +0000684
Anders Carlssond5cab542009-02-12 17:55:02 +0000685 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000686 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000687
Anders Carlssond5cab542009-02-12 17:55:02 +0000688 // Function
689 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000690
Anders Carlssond5cab542009-02-12 17:55:02 +0000691 // Descriptor
692 LiteralFields[4] = Descriptor;
David Chisnall5e530af2009-11-17 19:33:30 +0000693
Mike Stumpa5448542009-02-13 15:32:32 +0000694 llvm::Constant *BlockLiteralStruct =
David Chisnall5e530af2009-11-17 19:33:30 +0000695 llvm::ConstantStruct::get(VMContext, LiteralFields, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000696
697 llvm::GlobalVariable *BlockLiteral =
Owen Anderson1c431b32009-07-08 19:05:04 +0000698 new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000699 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000700 BlockLiteralStruct, "__block_literal_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000701
Anders Carlssond5cab542009-02-12 17:55:02 +0000702 return BlockLiteral;
703}
704
Mike Stump4e7a1f72009-02-21 20:00:35 +0000705llvm::Value *CodeGenFunction::LoadBlockStruct() {
Mike Stumpbf1914b2009-10-20 20:30:01 +0000706 llvm::Value *V = Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()],
707 "self");
708 // For now, we codegen based upon byte offsets.
709 return Builder.CreateBitCast(V, PtrToInt8Ty);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000710}
711
Mike Stump00470a12009-03-05 08:32:30 +0000712llvm::Function *
Fariborz Jahanian564360b2010-06-24 00:08:06 +0000713CodeGenFunction::GenerateBlockFunction(GlobalDecl GD, const BlockExpr *BExpr,
John McCallee504292010-05-21 04:11:14 +0000714 CGBlockInfo &Info,
Mike Stump6cc88f72009-03-20 21:53:12 +0000715 const Decl *OuterFuncDecl,
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000716 llvm::Constant *& BlockVarLayout,
John McCallee504292010-05-21 04:11:14 +0000717 llvm::DenseMap<const Decl*, llvm::Value*> ldm) {
Devang Patel963dfbd2009-04-15 21:51:44 +0000718
719 // Check if we should generate debug info for this block.
720 if (CGM.getDebugInfo())
721 DebugInfo = CGM.getDebugInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Mike Stump7f28a9c2009-03-13 23:34:28 +0000723 // Arrange for local static and local extern declarations to appear
724 // to be local to this function as well, as they are directly referenced
725 // in a block.
726 for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
727 i != ldm.end();
728 ++i) {
729 const VarDecl *VD = dyn_cast<VarDecl>(i->first);
Mike Stump1eb44332009-09-09 15:08:12 +0000730
John McCalld931b082010-08-26 03:08:43 +0000731 if (VD->getStorageClass() == SC_Static || VD->hasExternalStorage())
Mike Stump7f28a9c2009-03-13 23:34:28 +0000732 LocalDeclMap[VD] = i->second;
733 }
734
Ken Dyck687cc4a2010-01-26 13:48:07 +0000735 BlockOffset =
736 CGM.GetTargetTypeStoreSize(CGM.getGenericBlockLiteralType());
Ken Dyck30a8a272010-01-26 19:13:33 +0000737 BlockAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Eli Friedman48f91222009-03-28 03:24:54 +0000738
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000739 const FunctionType *BlockFunctionType = BExpr->getFunctionType();
740 QualType ResultType;
Rafael Espindola264ba482010-03-30 20:24:48 +0000741 FunctionType::ExtInfo EInfo = getFunctionExtInfo(*BlockFunctionType);
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000742 bool IsVariadic;
Mike Stump1eb44332009-09-09 15:08:12 +0000743 if (const FunctionProtoType *FTy =
Fariborz Jahanianda0895d2009-04-11 18:54:57 +0000744 dyn_cast<FunctionProtoType>(BlockFunctionType)) {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000745 ResultType = FTy->getResultType();
746 IsVariadic = FTy->isVariadic();
Mike Stumpb3589f42009-07-30 22:28:39 +0000747 } else {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000748 // K&R style block.
749 ResultType = BlockFunctionType->getResultType();
750 IsVariadic = false;
751 }
Mike Stumpa5448542009-02-13 15:32:32 +0000752
Anders Carlssond5cab542009-02-12 17:55:02 +0000753 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000754
Mike Stumpea26cb52009-10-21 03:49:08 +0000755 CurFuncDecl = OuterFuncDecl;
756
Chris Lattner161d36d2009-02-28 19:01:03 +0000757 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlssond5cab542009-02-12 17:55:02 +0000758
Mike Stumpea26cb52009-10-21 03:49:08 +0000759 IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor");
Mike Stumpadaaad32009-10-20 02:12:22 +0000760
John McCallea1471e2010-05-20 01:18:31 +0000761 // Build the block struct now.
John McCallee504292010-05-21 04:11:14 +0000762 AllocateAllBlockDeclRefs(*this, Info);
Mike Stumpea26cb52009-10-21 03:49:08 +0000763
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000764 // Capture block layout info. here.
765 if (CGM.getContext().getLangOptions().ObjC1)
Fariborz Jahanianc5904b42010-09-11 01:27:29 +0000766 BlockVarLayout = CGM.getObjCRuntime().GCBlockLayout(*this, BlockLayout);
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000767 else
768 BlockVarLayout = llvm::Constant::getNullValue(PtrToInt8Ty);
769
Mike Stump083c25e2009-10-22 00:49:09 +0000770 QualType ParmTy = getContext().getBlockParmType(BlockHasCopyDispose,
John McCallea1471e2010-05-20 01:18:31 +0000771 BlockLayout);
772
Anders Carlssond5cab542009-02-12 17:55:02 +0000773 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000774 ImplicitParamDecl *SelfDecl =
John McCall2888b652010-04-30 21:35:41 +0000775 ImplicitParamDecl::Create(getContext(), const_cast<BlockDecl*>(BD),
Mike Stumpadaaad32009-10-20 02:12:22 +0000776 SourceLocation(), II,
777 ParmTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000778
Anders Carlssond5cab542009-02-12 17:55:02 +0000779 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump4e7a1f72009-02-21 20:00:35 +0000780 BlockStructDecl = SelfDecl;
Mike Stumpa5448542009-02-13 15:32:32 +0000781
Steve Naroffe78b8092009-03-13 16:56:44 +0000782 for (BlockDecl::param_const_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000783 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000784 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000785
786 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +0000787 CGM.getTypes().getFunctionInfo(ResultType, Args, EInfo);
Anders Carlssond5cab542009-02-12 17:55:02 +0000788
Anders Carlssond5cab542009-02-12 17:55:02 +0000789 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000790 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic);
Mike Stumpa5448542009-02-13 15:32:32 +0000791
Douglas Gregor35415f52010-05-25 17:04:15 +0000792 MangleBuffer Name;
Fariborz Jahanian564360b2010-06-24 00:08:06 +0000793 CGM.getMangledName(GD, Name, BD);
Mike Stumpa5448542009-02-13 15:32:32 +0000794 llvm::Function *Fn =
Douglas Gregor6e5d9b02010-05-25 17:12:30 +0000795 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
796 Name.getString(), &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000797
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000798 CGM.SetInternalFunctionAttributes(BD, Fn, FI);
Fariborz Jahanian8404f672010-08-13 00:19:55 +0000799 StartFunction(BD, ResultType, Fn, Args,
800 BExpr->getBody()->getLocEnd());
801
802 CurFuncDecl = OuterFuncDecl;
803
Fariborz Jahanian16ac5ce2010-06-28 18:58:34 +0000804 QualType FnType(BlockFunctionType, 0);
Fariborz Jahanianef160b42010-06-28 19:42:10 +0000805 bool HasPrototype = isa<FunctionProtoType>(BlockFunctionType);
Fariborz Jahanian16ac5ce2010-06-28 18:58:34 +0000806
807 IdentifierInfo *ID = &getContext().Idents.get(Name.getString());
808 CurCodeDecl = FunctionDecl::Create(getContext(),
809 getContext().getTranslationUnitDecl(),
810 SourceLocation(), ID, FnType,
811 0,
John McCalld931b082010-08-26 03:08:43 +0000812 SC_Static,
813 SC_None,
Fariborz Jahanian16ac5ce2010-06-28 18:58:34 +0000814 false, HasPrototype);
Fariborz Jahanian8404f672010-08-13 00:19:55 +0000815 if (FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FnType)) {
816 const FunctionDecl *CFD = dyn_cast<FunctionDecl>(CurCodeDecl);
817 FunctionDecl *FD = const_cast<FunctionDecl *>(CFD);
818 llvm::SmallVector<ParmVarDecl*, 16> Params;
819 for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i)
820 Params.push_back(ParmVarDecl::Create(getContext(), FD,
821 SourceLocation(), 0,
822 FT->getArgType(i), /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +0000823 SC_None, SC_None, 0));
Fariborz Jahanian8404f672010-08-13 00:19:55 +0000824 FD->setParams(Params.data(), Params.size());
825 }
Fariborz Jahanian16ac5ce2010-06-28 18:58:34 +0000826
Fariborz Jahanian8404f672010-08-13 00:19:55 +0000827
John McCallea1471e2010-05-20 01:18:31 +0000828 // If we have a C++ 'this' reference, go ahead and force it into
829 // existence now.
830 if (Info.CXXThisRef) {
831 assert(!BlockCXXThisOffset.isZero() &&
832 "haven't yet allocated 'this' reference");
833
834 // TODO: I have a dream that one day this will be typed.
835 llvm::Value *BlockLiteral = LoadBlockStruct();
836 llvm::Value *ThisPtrRaw =
837 Builder.CreateConstInBoundsGEP1_64(BlockLiteral,
838 BlockCXXThisOffset.getQuantity(),
839 "this.ptr.raw");
840
841 const llvm::Type *Ty =
842 CGM.getTypes().ConvertType(Info.CXXThisRef->getType());
843 Ty = llvm::PointerType::get(Ty, 0);
844 llvm::Value *ThisPtr = Builder.CreateBitCast(ThisPtrRaw, Ty, "this.ptr");
845
846 CXXThisValue = Builder.CreateLoad(ThisPtr, "this");
847 }
848
John McCallee504292010-05-21 04:11:14 +0000849 // If we have an Objective C 'self' reference, go ahead and force it
850 // into existence now.
851 if (Info.NeedsObjCSelf) {
852 ValueDecl *Self = cast<ObjCMethodDecl>(CurFuncDecl)->getSelfDecl();
853 LocalDeclMap[Self] = GetAddrOfBlockDecl(Self, false);
854 }
855
Mike Stumpb289b3f2009-10-01 22:29:41 +0000856 // Save a spot to insert the debug information for all the BlockDeclRefDecls.
857 llvm::BasicBlock *entry = Builder.GetInsertBlock();
858 llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint();
859 --entry_ptr;
860
Chris Lattner161d36d2009-02-28 19:01:03 +0000861 EmitStmt(BExpr->getBody());
Mike Stumpb289b3f2009-10-01 22:29:41 +0000862
Mike Stumpde8c5c72009-10-01 00:27:30 +0000863 // Remember where we were...
864 llvm::BasicBlock *resume = Builder.GetInsertBlock();
Mike Stumpb289b3f2009-10-01 22:29:41 +0000865
Mike Stumpde8c5c72009-10-01 00:27:30 +0000866 // Go back to the entry.
Mike Stumpb289b3f2009-10-01 22:29:41 +0000867 ++entry_ptr;
868 Builder.SetInsertPoint(entry, entry_ptr);
869
Mike Stumpb1a6e682009-09-30 02:43:10 +0000870 if (CGDebugInfo *DI = getDebugInfo()) {
Mike Stumpb1a6e682009-09-30 02:43:10 +0000871 // Emit debug information for all the BlockDeclRefDecls.
John McCallea1471e2010-05-20 01:18:31 +0000872 // FIXME: also for 'this'
873 for (unsigned i = 0, e = BlockLayout.size(); i != e; ++i) {
874 if (const BlockDeclRefExpr *BDRE =
875 dyn_cast<BlockDeclRefExpr>(BlockLayout[i])) {
Mike Stumpb1a6e682009-09-30 02:43:10 +0000876 const ValueDecl *D = BDRE->getDecl();
877 DI->setLocation(D->getLocation());
878 DI->EmitDeclareOfBlockDeclRefVariable(BDRE,
879 LocalDeclMap[getBlockStructDecl()],
880 Builder, this);
881 }
882 }
Mike Stumpb1a6e682009-09-30 02:43:10 +0000883 }
Mike Stumpde8c5c72009-10-01 00:27:30 +0000884 // And resume where we left off.
885 if (resume == 0)
886 Builder.ClearInsertionPoint();
887 else
888 Builder.SetInsertPoint(resume);
Mike Stumpb1a6e682009-09-30 02:43:10 +0000889
Chris Lattner161d36d2009-02-28 19:01:03 +0000890 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlssond5cab542009-02-12 17:55:02 +0000891
Mike Stump8a2b4b12009-02-25 23:33:13 +0000892 // The runtime needs a minimum alignment of a void *.
Ken Dyck30a8a272010-01-26 19:13:33 +0000893 CharUnits MinAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Ken Dyck199c3d62010-01-11 17:06:35 +0000894 BlockOffset = CharUnits::fromQuantity(
Ken Dyck30a8a272010-01-26 19:13:33 +0000895 llvm::RoundUpToAlignment(BlockOffset.getQuantity(),
896 MinAlign.getQuantity()));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000897
John McCallee504292010-05-21 04:11:14 +0000898 Info.BlockSize = BlockOffset;
899 Info.BlockAlign = BlockAlign;
900 Info.BlockLayout = BlockLayout;
901 Info.BlockHasCopyDispose = BlockHasCopyDispose;
Anders Carlssond5cab542009-02-12 17:55:02 +0000902 return Fn;
903}
Mike Stumpa99038c2009-02-28 09:07:16 +0000904
John McCallea1471e2010-05-20 01:18:31 +0000905CharUnits BlockFunction::getBlockOffset(CharUnits Size, CharUnits Align) {
906 assert((Align.isPositive()) && "alignment must be 1 byte or more");
Mike Stumpa99038c2009-02-28 09:07:16 +0000907
Ken Dyck199c3d62010-01-11 17:06:35 +0000908 CharUnits OldOffset = BlockOffset;
Mike Stumpa99038c2009-02-28 09:07:16 +0000909
910 // Ensure proper alignment, even if it means we have to have a gap
Ken Dyck199c3d62010-01-11 17:06:35 +0000911 BlockOffset = CharUnits::fromQuantity(
Ken Dyck30a8a272010-01-26 19:13:33 +0000912 llvm::RoundUpToAlignment(BlockOffset.getQuantity(), Align.getQuantity()));
Mike Stumpa99038c2009-02-28 09:07:16 +0000913 BlockAlign = std::max(Align, BlockAlign);
Mike Stump00470a12009-03-05 08:32:30 +0000914
Ken Dyck199c3d62010-01-11 17:06:35 +0000915 CharUnits Pad = BlockOffset - OldOffset;
916 if (Pad.isPositive()) {
Mike Stumpa99038c2009-02-28 09:07:16 +0000917 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
Ken Dyck199c3d62010-01-11 17:06:35 +0000918 llvm::APInt(32,
919 Pad.getQuantity()),
Mike Stumpa99038c2009-02-28 09:07:16 +0000920 ArrayType::Normal, 0);
Douglas Gregorba55ec22010-05-11 18:17:16 +0000921 ValueDecl *PadDecl = VarDecl::Create(getContext(),
922 getContext().getTranslationUnitDecl(),
923 SourceLocation(),
Douglas Gregor16573fa2010-04-19 22:54:31 +0000924 0, QualType(PadTy), 0,
John McCalld931b082010-08-26 03:08:43 +0000925 SC_None, SC_None);
John McCallea1471e2010-05-20 01:18:31 +0000926 Expr *E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
927 SourceLocation());
928 BlockLayout.push_back(E);
Mike Stumpa99038c2009-02-28 09:07:16 +0000929 }
Mike Stumpa99038c2009-02-28 09:07:16 +0000930
931 BlockOffset += Size;
John McCallea1471e2010-05-20 01:18:31 +0000932 return BlockOffset - Size;
Mike Stumpa99038c2009-02-28 09:07:16 +0000933}
Mike Stumpdab514f2009-03-04 03:23:46 +0000934
Mike Stump08920992009-03-07 02:35:30 +0000935llvm::Constant *BlockFunction::
936GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000937 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000938 QualType R = getContext().VoidTy;
939
940 FunctionArgList Args;
941 // FIXME: This leaks
Mike Stump08920992009-03-07 02:35:30 +0000942 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +0000943 ImplicitParamDecl::Create(getContext(), 0,
944 SourceLocation(), 0,
Mike Stump08920992009-03-07 02:35:30 +0000945 getContext().getPointerType(getContext().VoidTy));
946 Args.push_back(std::make_pair(Dst, Dst->getType()));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000947 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +0000948 ImplicitParamDecl::Create(getContext(), 0,
949 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000950 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000951 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Mike Stumpa4f668f2009-03-06 01:33:24 +0000953 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +0000954 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000955
Mike Stump3899a7f2009-06-05 23:26:36 +0000956 // FIXME: We'd like to put these into a mergable by content, with
957 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000958 CodeGenTypes &Types = CGM.getTypes();
959 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
960
961 llvm::Function *Fn =
962 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000963 "__copy_helper_block_", &CGM.getModule());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000964
965 IdentifierInfo *II
966 = &CGM.getContext().Idents.get("__copy_helper_block_");
967
968 FunctionDecl *FD = FunctionDecl::Create(getContext(),
969 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000970 SourceLocation(), II, R, 0,
John McCalld931b082010-08-26 03:08:43 +0000971 SC_Static,
972 SC_None,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000973 false,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000974 true);
975 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump08920992009-03-07 02:35:30 +0000976
977 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
978 llvm::Type *PtrPtrT;
Mike Stump08920992009-03-07 02:35:30 +0000979
Mike Stumpb7477cf2009-04-10 18:52:28 +0000980 if (NoteForHelperp) {
981 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump08920992009-03-07 02:35:30 +0000982
Owen Anderson96e0fc72009-07-29 22:16:19 +0000983 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000984 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
985 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump08920992009-03-07 02:35:30 +0000986
Mike Stumpb7477cf2009-04-10 18:52:28 +0000987 llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000988 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000989 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000990 DstObj = Builder.CreateBitCast(DstObj, PtrPtrT);
991 DstObj = Builder.CreateLoad(DstObj);
Mike Stump08920992009-03-07 02:35:30 +0000992
Mike Stumpb7477cf2009-04-10 18:52:28 +0000993 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
994 int flag = NoteForHelper[i].flag;
995 int index = NoteForHelper[i].index;
Mike Stump08920992009-03-07 02:35:30 +0000996
Mike Stumpb7477cf2009-04-10 18:52:28 +0000997 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
998 || NoteForHelper[i].RequiresCopying) {
999 llvm::Value *Srcv = SrcObj;
1000 Srcv = Builder.CreateStructGEP(Srcv, index);
1001 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001002 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +00001003 Srcv = Builder.CreateLoad(Srcv);
1004
1005 llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
1006 Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
1007
Chris Lattner77b89b82010-06-27 07:15:29 +00001008 llvm::Value *N = llvm::ConstantInt::get(CGF.Int32Ty, flag);
Daniel Dunbar673431a2010-07-16 00:00:15 +00001009 llvm::Value *F = CGM.getBlockObjectAssign();
Mike Stumpb7477cf2009-04-10 18:52:28 +00001010 Builder.CreateCall3(F, Dstv, Srcv, N);
1011 }
Mike Stump08920992009-03-07 02:35:30 +00001012 }
1013 }
1014
Mike Stumpa4f668f2009-03-06 01:33:24 +00001015 CGF.FinishFunction();
1016
Owen Anderson3c4972d2009-07-29 18:54:39 +00001017 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +00001018}
1019
Mike Stumpcf62d392009-03-06 18:42:23 +00001020llvm::Constant *BlockFunction::
Mike Stump08920992009-03-07 02:35:30 +00001021GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
1022 const llvm::StructType* T,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001023 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +00001024 QualType R = getContext().VoidTy;
1025
1026 FunctionArgList Args;
1027 // FIXME: This leaks
1028 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001029 ImplicitParamDecl::Create(getContext(), 0,
1030 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +00001031 getContext().getPointerType(getContext().VoidTy));
1032
1033 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001034
Mike Stumpa4f668f2009-03-06 01:33:24 +00001035 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +00001036 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stumpa4f668f2009-03-06 01:33:24 +00001037
Mike Stump3899a7f2009-06-05 23:26:36 +00001038 // FIXME: We'd like to put these into a mergable by content, with
1039 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +00001040 CodeGenTypes &Types = CGM.getTypes();
1041 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1042
1043 llvm::Function *Fn =
1044 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001045 "__destroy_helper_block_", &CGM.getModule());
Mike Stumpa4f668f2009-03-06 01:33:24 +00001046
1047 IdentifierInfo *II
1048 = &CGM.getContext().Idents.get("__destroy_helper_block_");
1049
1050 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1051 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001052 SourceLocation(), II, R, 0,
John McCalld931b082010-08-26 03:08:43 +00001053 SC_Static,
1054 SC_None,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001055 false, true);
Mike Stumpa4f668f2009-03-06 01:33:24 +00001056 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1edf6b62009-03-07 02:53:18 +00001057
Mike Stumpb7477cf2009-04-10 18:52:28 +00001058 if (NoteForHelperp) {
1059 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump1edf6b62009-03-07 02:53:18 +00001060
Mike Stumpb7477cf2009-04-10 18:52:28 +00001061 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
1062 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +00001063 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +00001064 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
1065 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump1edf6b62009-03-07 02:53:18 +00001066
Mike Stumpb7477cf2009-04-10 18:52:28 +00001067 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
1068 int flag = NoteForHelper[i].flag;
1069 int index = NoteForHelper[i].index;
Mike Stump1edf6b62009-03-07 02:53:18 +00001070
Mike Stumpb7477cf2009-04-10 18:52:28 +00001071 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
1072 || NoteForHelper[i].RequiresCopying) {
1073 llvm::Value *Srcv = SrcObj;
1074 Srcv = Builder.CreateStructGEP(Srcv, index);
1075 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001076 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +00001077 Srcv = Builder.CreateLoad(Srcv);
1078
1079 BuildBlockRelease(Srcv, flag);
1080 }
Mike Stump1edf6b62009-03-07 02:53:18 +00001081 }
1082 }
1083
Mike Stumpa4f668f2009-03-06 01:33:24 +00001084 CGF.FinishFunction();
1085
Owen Anderson3c4972d2009-07-29 18:54:39 +00001086 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpa4f668f2009-03-06 01:33:24 +00001087}
1088
Mike Stump08920992009-03-07 02:35:30 +00001089llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001090 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump08920992009-03-07 02:35:30 +00001091 return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
1092 T, NoteForHelper);
Mike Stumpa4f668f2009-03-06 01:33:24 +00001093}
1094
Mike Stump08920992009-03-07 02:35:30 +00001095llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001096 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stump08920992009-03-07 02:35:30 +00001097 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001098 T, NoteForHelperp);
Mike Stumpdab514f2009-03-04 03:23:46 +00001099}
Mike Stump797b6322009-03-05 01:23:13 +00001100
Mike Stumpee094222009-03-06 06:12:24 +00001101llvm::Constant *BlockFunction::
1102GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
Mike Stump45031c02009-03-06 02:29:21 +00001103 QualType R = getContext().VoidTy;
1104
1105 FunctionArgList Args;
1106 // FIXME: This leaks
Mike Stumpee094222009-03-06 06:12:24 +00001107 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +00001108 ImplicitParamDecl::Create(getContext(), 0,
1109 SourceLocation(), 0,
Mike Stumpee094222009-03-06 06:12:24 +00001110 getContext().getPointerType(getContext().VoidTy));
1111 Args.push_back(std::make_pair(Dst, Dst->getType()));
1112
1113 // FIXME: This leaks
Mike Stump45031c02009-03-06 02:29:21 +00001114 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001115 ImplicitParamDecl::Create(getContext(), 0,
1116 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001117 getContext().getPointerType(getContext().VoidTy));
Mike Stump45031c02009-03-06 02:29:21 +00001118 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Mike Stump45031c02009-03-06 02:29:21 +00001120 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +00001121 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stump45031c02009-03-06 02:29:21 +00001122
Mike Stump45031c02009-03-06 02:29:21 +00001123 CodeGenTypes &Types = CGM.getTypes();
1124 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1125
Mike Stump3899a7f2009-06-05 23:26:36 +00001126 // FIXME: We'd like to put these into a mergable by content, with
1127 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001128 llvm::Function *Fn =
1129 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001130 "__Block_byref_id_object_copy_", &CGM.getModule());
Mike Stump45031c02009-03-06 02:29:21 +00001131
1132 IdentifierInfo *II
1133 = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
1134
1135 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1136 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001137 SourceLocation(), II, R, 0,
John McCalld931b082010-08-26 03:08:43 +00001138 SC_Static,
1139 SC_None,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001140 false, true);
Mike Stump45031c02009-03-06 02:29:21 +00001141 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpee094222009-03-06 06:12:24 +00001142
1143 // dst->x
1144 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001145 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001146 V = Builder.CreateLoad(V);
Mike Stumpee094222009-03-06 06:12:24 +00001147 V = Builder.CreateStructGEP(V, 6, "x");
1148 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
1149
1150 // src->x
1151 V = CGF.GetAddrOfLocalVar(Src);
1152 V = Builder.CreateLoad(V);
1153 V = Builder.CreateBitCast(V, T);
1154 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001155 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpee094222009-03-06 06:12:24 +00001156 llvm::Value *SrcObj = Builder.CreateLoad(V);
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Mike Stumpee094222009-03-06 06:12:24 +00001158 flag |= BLOCK_BYREF_CALLER;
1159
Chris Lattner77b89b82010-06-27 07:15:29 +00001160 llvm::Value *N = llvm::ConstantInt::get(CGF.Int32Ty, flag);
Daniel Dunbar673431a2010-07-16 00:00:15 +00001161 llvm::Value *F = CGM.getBlockObjectAssign();
Mike Stumpee094222009-03-06 06:12:24 +00001162 Builder.CreateCall3(F, DstObj, SrcObj, N);
1163
Mike Stump45031c02009-03-06 02:29:21 +00001164 CGF.FinishFunction();
1165
Owen Anderson3c4972d2009-07-29 18:54:39 +00001166 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001167}
1168
Mike Stump1851b682009-03-06 04:53:30 +00001169llvm::Constant *
1170BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
1171 int flag) {
Mike Stump45031c02009-03-06 02:29:21 +00001172 QualType R = getContext().VoidTy;
1173
1174 FunctionArgList Args;
1175 // FIXME: This leaks
1176 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001177 ImplicitParamDecl::Create(getContext(), 0,
1178 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001179 getContext().getPointerType(getContext().VoidTy));
1180
1181 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001182
Mike Stump45031c02009-03-06 02:29:21 +00001183 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +00001184 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stump45031c02009-03-06 02:29:21 +00001185
Mike Stump45031c02009-03-06 02:29:21 +00001186 CodeGenTypes &Types = CGM.getTypes();
1187 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1188
Mike Stump3899a7f2009-06-05 23:26:36 +00001189 // FIXME: We'd like to put these into a mergable by content, with
1190 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001191 llvm::Function *Fn =
1192 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001193 "__Block_byref_id_object_dispose_",
Mike Stump45031c02009-03-06 02:29:21 +00001194 &CGM.getModule());
1195
1196 IdentifierInfo *II
1197 = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
1198
1199 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1200 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001201 SourceLocation(), II, R, 0,
John McCalld931b082010-08-26 03:08:43 +00001202 SC_Static,
1203 SC_None,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001204 false, true);
Mike Stump45031c02009-03-06 02:29:21 +00001205 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1851b682009-03-06 04:53:30 +00001206
1207 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001208 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001209 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001210 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001211 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001212 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001213
Mike Stump1851b682009-03-06 04:53:30 +00001214 flag |= BLOCK_BYREF_CALLER;
1215 BuildBlockRelease(V, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001216 CGF.FinishFunction();
1217
Owen Anderson3c4972d2009-07-29 18:54:39 +00001218 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001219}
1220
Mike Stumpee094222009-03-06 06:12:24 +00001221llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001222 int Flag, unsigned Align) {
1223 // All alignments below that of pointer alignment collapse down to just
Mike Stump3899a7f2009-06-05 23:26:36 +00001224 // pointer alignment, as we always have at least that much alignment to begin
1225 // with.
1226 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001227
Mike Stump3899a7f2009-06-05 23:26:36 +00001228 // As an optimization, we only generate a single function of each kind we
1229 // might need. We need a different one for each alignment and for each
1230 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001231 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1232 llvm::Constant *&Entry = CGM.AssignCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001233 if (Entry)
1234 return Entry;
Chris Lattner10976d92009-12-05 08:21:30 +00001235 return Entry = CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, Flag);
Mike Stump45031c02009-03-06 02:29:21 +00001236}
1237
Mike Stump1851b682009-03-06 04:53:30 +00001238llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001239 int Flag,
Mike Stump3899a7f2009-06-05 23:26:36 +00001240 unsigned Align) {
1241 // All alignments below that of pointer alignment collpase down to just
1242 // pointer alignment, as we always have at least that much alignment to begin
1243 // with.
1244 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001245
Mike Stump3899a7f2009-06-05 23:26:36 +00001246 // As an optimization, we only generate a single function of each kind we
1247 // might need. We need a different one for each alignment and for each
1248 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001249 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1250 llvm::Constant *&Entry = CGM.DestroyCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001251 if (Entry)
1252 return Entry;
Chris Lattner10976d92009-12-05 08:21:30 +00001253 return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, Flag);
Mike Stump45031c02009-03-06 02:29:21 +00001254}
1255
Mike Stump1851b682009-03-06 04:53:30 +00001256void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Daniel Dunbar673431a2010-07-16 00:00:15 +00001257 llvm::Value *F = CGM.getBlockObjectDispose();
Mike Stump1851b682009-03-06 04:53:30 +00001258 llvm::Value *N;
Mike Stump797b6322009-03-05 01:23:13 +00001259 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Chris Lattner77b89b82010-06-27 07:15:29 +00001260 N = llvm::ConstantInt::get(CGF.Int32Ty, flag);
Mike Stump797b6322009-03-05 01:23:13 +00001261 Builder.CreateCall2(F, V, N);
1262}
Mike Stump00470a12009-03-05 08:32:30 +00001263
1264ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stump08920992009-03-07 02:35:30 +00001265
1266BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1267 CGBuilderTy &B)
Chris Lattner77b89b82010-06-27 07:15:29 +00001268 : CGM(cgm), VMContext(cgm.getLLVMContext()), CGF(cgf), Builder(B) {
Owen Anderson0032b272009-08-13 21:57:51 +00001269 PtrToInt8Ty = llvm::PointerType::getUnqual(
1270 llvm::Type::getInt8Ty(VMContext));
Mike Stump08920992009-03-07 02:35:30 +00001271
1272 BlockHasCopyDispose = false;
1273}