blob: c40cfaf6a97887b6d15ef6ddd9a1aaae63628aeb [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)
Fariborz Jahanianbc747642010-11-13 23:48:30 +000028 : Name(N), CXXThisRef(0), NeedsObjCSelf(false),
Fariborz Jahanianeec82172010-11-14 05:25:15 +000029 HasCXXObject(false) {
John McCallee504292010-05-21 04:11:14 +000030
31 // Skip asm prefix, if any.
32 if (Name && Name[0] == '\01')
33 ++Name;
34}
35
36
Mike Stumpcf62d392009-03-06 18:42:23 +000037llvm::Constant *CodeGenFunction::
Fariborz Jahanian89ecd412010-08-04 16:57:49 +000038BuildDescriptorBlockDecl(const BlockExpr *BE, const CGBlockInfo &Info,
Mike Stumpa803b0e2009-03-25 17:58:24 +000039 const llvm::StructType* Ty,
Fariborz Jahanian44034db2010-08-04 18:44:59 +000040 llvm::Constant *BlockVarLayout,
Mike Stump08920992009-03-07 02:35:30 +000041 std::vector<HelperInfo> *NoteForHelper) {
Fariborz Jahanian89ecd412010-08-04 16:57:49 +000042 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
Fariborz Jahanian2715b202010-11-15 19:19:38 +000060 if (Info.BlockHasCopyDispose) {
Mike Stumpe5fee252009-02-13 16:19:19 +000061 // 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.
Fariborz Jahanian34999872010-11-13 21:53:34 +0000115 if (!Info.InnerBlocks.count(D->getDeclContext())) {
116 if (BDRE->getCopyConstructorExpr())
117 Info.HasCXXObject = true;
Mike Stump38e16272009-10-21 22:01:24 +0000118 Info.DeclRefs.push_back(BDRE);
Fariborz Jahanian34999872010-11-13 21:53:34 +0000119 }
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000120 }
John McCallea1471e2010-05-20 01:18:31 +0000121
John McCallee504292010-05-21 04:11:14 +0000122 // Make sure to capture implicit 'self' references due to super calls.
Chandler Carruthf54b80f2010-05-21 10:29:28 +0000123 else if (const ObjCMessageExpr *E = dyn_cast<ObjCMessageExpr>(S)) {
John McCallee504292010-05-21 04:11:14 +0000124 if (E->getReceiverKind() == ObjCMessageExpr::SuperClass ||
125 E->getReceiverKind() == ObjCMessageExpr::SuperInstance)
126 Info.NeedsObjCSelf = true;
Chandler Carruthf54b80f2010-05-21 10:29:28 +0000127 }
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000128 else if (const ObjCPropertyRefExpr *PE = dyn_cast<ObjCPropertyRefExpr>(S)) {
129 // Getter/setter uses may also cause implicit super references,
130 // which we can check for with:
131 if (PE->isSuperReceiver())
132 Info.NeedsObjCSelf = true;
133 }
134 else if (const ObjCImplicitSetterGetterRefExpr *IE =
135 dyn_cast<ObjCImplicitSetterGetterRefExpr>(S)) {
136 // Getter/setter uses may also cause implicit super references,
137 // which we can check for with:
138 if (IE->isSuperReceiver())
139 Info.NeedsObjCSelf = true;
140 }
John McCallee504292010-05-21 04:11:14 +0000141 else if (isa<CXXThisExpr>(S))
John McCallea1471e2010-05-20 01:18:31 +0000142 Info.CXXThisRef = cast<CXXThisExpr>(S);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000143}
144
John McCallee504292010-05-21 04:11:14 +0000145/// CanBlockBeGlobal - Given a CGBlockInfo struct, determines if a block can be
Mike Stump58a85142009-03-04 22:48:06 +0000146/// declared as a global variable instead of on the stack.
John McCallee504292010-05-21 04:11:14 +0000147static bool CanBlockBeGlobal(const CGBlockInfo &Info) {
Mike Stumpea26cb52009-10-21 03:49:08 +0000148 return Info.DeclRefs.empty();
149}
150
151/// AllocateAllBlockDeclRefs - Preallocate all nested BlockDeclRefExprs to
152/// ensure we can generate the debug information for the parameter for the block
153/// invoke function.
John McCallee504292010-05-21 04:11:14 +0000154static void AllocateAllBlockDeclRefs(CodeGenFunction &CGF, CGBlockInfo &Info) {
John McCallea1471e2010-05-20 01:18:31 +0000155 if (Info.CXXThisRef)
John McCallee504292010-05-21 04:11:14 +0000156 CGF.AllocateBlockCXXThisPointer(Info.CXXThisRef);
Mike Stumpea26cb52009-10-21 03:49:08 +0000157
158 for (size_t i = 0; i < Info.DeclRefs.size(); ++i)
John McCallee504292010-05-21 04:11:14 +0000159 CGF.AllocateBlockDecl(Info.DeclRefs[i]);
160
161 if (Info.NeedsObjCSelf) {
162 ValueDecl *Self = cast<ObjCMethodDecl>(CGF.CurFuncDecl)->getSelfDecl();
163 BlockDeclRefExpr *BDRE =
164 new (CGF.getContext()) BlockDeclRefExpr(Self, Self->getType(),
165 SourceLocation(), false);
166 Info.DeclRefs.push_back(BDRE);
167 CGF.AllocateBlockDecl(BDRE);
168 }
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000169}
170
Fariborz Jahanian7edddb82010-07-28 19:07:18 +0000171static unsigned computeBlockFlag(CodeGenModule &CGM,
172 const BlockExpr *BE, unsigned flags) {
173 QualType BPT = BE->getType();
174 const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>();
175 QualType ResultType = ftype->getResultType();
176
177 CallArgList Args;
178 CodeGenTypes &Types = CGM.getTypes();
179 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, Args,
180 FunctionType::ExtInfo());
181 if (CGM.ReturnTypeUsesSRet(FnInfo))
182 flags |= CodeGenFunction::BLOCK_USE_STRET;
183 return flags;
184}
185
Mike Stump58a85142009-03-04 22:48:06 +0000186// FIXME: Push most into CGM, passing down a few bits, like current function
187// name.
Mike Stump8a2b4b12009-02-25 23:33:13 +0000188llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000189 std::string Name = CurFn->getName();
John McCallee504292010-05-21 04:11:14 +0000190 CGBlockInfo Info(Name.c_str());
191 Info.InnerBlocks.insert(BE->getBlockDecl());
192 CollectBlockDeclRefInfo(BE->getBody(), Info);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000193
194 // Check if the block can be global.
Mike Stump58a85142009-03-04 22:48:06 +0000195 // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like
196 // to just have one code path. We should move this function into CGM and pass
197 // CGF, then we can just check to see if CGF is 0.
Mike Stumpdab514f2009-03-04 03:23:46 +0000198 if (0 && CanBlockBeGlobal(Info))
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000199 return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
Mike Stump00470a12009-03-05 08:32:30 +0000200
David Chisnall5e530af2009-11-17 19:33:30 +0000201 size_t BlockFields = 5;
202
David Chisnall5e530af2009-11-17 19:33:30 +0000203 std::vector<llvm::Constant*> Elts(BlockFields);
204
Mike Stumpe5fee252009-02-13 16:19:19 +0000205 llvm::Constant *C;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000206 llvm::Value *V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000207
Mike Stumpe5fee252009-02-13 16:19:19 +0000208 {
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000209 llvm::Constant *BlockVarLayout;
Mike Stumpe5fee252009-02-13 16:19:19 +0000210 // C = BuildBlockStructInitlist();
Blaine Garsta36e2232010-03-05 01:29:59 +0000211 unsigned int flags = BLOCK_HAS_SIGNATURE;
David Chisnall5e530af2009-11-17 19:33:30 +0000212
Mike Stump00470a12009-03-05 08:32:30 +0000213 // We run this first so that we set BlockHasCopyDispose from the entire
214 // block literal.
215 // __invoke
Mike Stump00470a12009-03-05 08:32:30 +0000216 llvm::Function *Fn
Fariborz Jahanian564360b2010-06-24 00:08:06 +0000217 = CodeGenFunction(CGM).GenerateBlockFunction(CurGD, BE, Info, CurFuncDecl,
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000218 BlockVarLayout,
John McCallee504292010-05-21 04:11:14 +0000219 LocalDeclMap);
Fariborz Jahanian2715b202010-11-15 19:19:38 +0000220 SynthesizeCopyDisposeHelpers |= Info.BlockHasCopyDispose;
Mike Stump00470a12009-03-05 08:32:30 +0000221 Elts[3] = Fn;
222
Mike Stumpf5408fe2009-05-16 07:57:57 +0000223 // FIXME: Don't use BlockHasCopyDispose, it is set more often then
224 // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); }
John McCallee504292010-05-21 04:11:14 +0000225 if (Info.BlockHasCopyDispose)
Mike Stumpe5fee252009-02-13 16:19:19 +0000226 flags |= BLOCK_HAS_COPY_DISPOSE;
Fariborz Jahanian34999872010-11-13 21:53:34 +0000227 // This block may import a c++ object.
228 if (Info.HasCXXObject)
229 flags |= BLOCK_HAS_CXX_OBJ;
Mike Stumpe5fee252009-02-13 16:19:19 +0000230
Mike Stump7d6dc4f2009-02-13 20:17:16 +0000231 // __isa
Mike Stump59c5b112009-02-13 19:29:27 +0000232 C = CGM.getNSConcreteStackBlock();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000233 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
Mike Stump00470a12009-03-05 08:32:30 +0000234 Elts[0] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000235
236 // __flags
Fariborz Jahanian7edddb82010-07-28 19:07:18 +0000237 flags = computeBlockFlag(CGM, BE, flags);
Mike Stumpe5fee252009-02-13 16:19:19 +0000238 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
239 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000240 C = llvm::ConstantInt::get(IntTy, flags);
Mike Stump00470a12009-03-05 08:32:30 +0000241 Elts[1] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000242
243 // __reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000244 C = llvm::ConstantInt::get(IntTy, 0);
Mike Stump00470a12009-03-05 08:32:30 +0000245 Elts[2] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000246
John McCallee504292010-05-21 04:11:14 +0000247 if (Info.BlockLayout.empty()) {
Mike Stumpcf62d392009-03-06 18:42:23 +0000248 // __descriptor
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000249 C = llvm::Constant::getNullValue(PtrToInt8Ty);
250 Elts[4] = BuildDescriptorBlockDecl(BE, Info, 0, C, 0);
Mike Stumpcf62d392009-03-06 18:42:23 +0000251
Mike Stump5570cfe2009-03-01 20:07:53 +0000252 // Optimize to being a global block.
253 Elts[0] = CGM.getNSConcreteGlobalBlock();
Blaine Garsta36e2232010-03-05 01:29:59 +0000254
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000255 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
Mike Stump5570cfe2009-03-01 20:07:53 +0000256
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000257 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000258
Owen Anderson1c431b32009-07-08 19:05:04 +0000259 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000260 llvm::GlobalValue::InternalLinkage, C,
261 "__block_holder_tmp_" +
262 llvm::Twine(CGM.getGlobalUniqueCount()));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000263 QualType BPT = BE->getType();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000264 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000265 return C;
266 }
Mike Stump00470a12009-03-05 08:32:30 +0000267
John McCallee504292010-05-21 04:11:14 +0000268 std::vector<const llvm::Type *> Types(BlockFields+Info.BlockLayout.size());
Mike Stump08920992009-03-07 02:35:30 +0000269 for (int i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000270 Types[i] = Elts[i]->getType();
Mike Stump08920992009-03-07 02:35:30 +0000271 Types[4] = PtrToInt8Ty;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000272
John McCallee504292010-05-21 04:11:14 +0000273 for (unsigned i = 0, n = Info.BlockLayout.size(); i != n; ++i) {
274 const Expr *E = Info.BlockLayout[i];
Mike Stumpa99038c2009-02-28 09:07:16 +0000275 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
276 QualType Ty = E->getType();
Mike Stumpdab514f2009-03-04 03:23:46 +0000277 if (BDRE && BDRE->isByRef()) {
Fariborz Jahanian79651722010-06-02 21:35:17 +0000278 Types[i+BlockFields] =
279 llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0);
280 } else if (BDRE && BDRE->getDecl()->getType()->isReferenceType()) {
281 Types[i+BlockFields] = llvm::PointerType::get(ConvertType(Ty), 0);
282 } else
David Chisnall5e530af2009-11-17 19:33:30 +0000283 Types[i+BlockFields] = ConvertType(Ty);
Mike Stumpa99038c2009-02-28 09:07:16 +0000284 }
Mike Stump8a2b4b12009-02-25 23:33:13 +0000285
Owen Anderson47a434f2009-08-05 23:18:46 +0000286 llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000287
288 llvm::AllocaInst *A = CreateTempAlloca(Ty);
John McCallee504292010-05-21 04:11:14 +0000289 A->setAlignment(Info.BlockAlign.getQuantity());
Mike Stump8a2b4b12009-02-25 23:33:13 +0000290 V = A;
291
John McCallea1471e2010-05-20 01:18:31 +0000292 // Build layout / cleanup information for all the data entries in the
293 // layout, and write the enclosing fields into the type.
John McCallee504292010-05-21 04:11:14 +0000294 std::vector<HelperInfo> NoteForHelper(Info.BlockLayout.size());
John McCallea1471e2010-05-20 01:18:31 +0000295 unsigned NumHelpers = 0;
Mike Stump08920992009-03-07 02:35:30 +0000296
297 for (unsigned i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000298 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
Mike Stump00470a12009-03-05 08:32:30 +0000299
John McCallee504292010-05-21 04:11:14 +0000300 for (unsigned i=0; i < Info.BlockLayout.size(); ++i) {
301 const Expr *E = Info.BlockLayout[i];
Mike Stump8a2b4b12009-02-25 23:33:13 +0000302
John McCallea1471e2010-05-20 01:18:31 +0000303 // Skip padding.
304 if (isa<DeclRefExpr>(E)) continue;
Mike Stumpa99038c2009-02-28 09:07:16 +0000305
John McCallea1471e2010-05-20 01:18:31 +0000306 llvm::Value* Addr = Builder.CreateStructGEP(V, i+BlockFields, "tmp");
307 HelperInfo &Note = NoteForHelper[NumHelpers++];
Mike Stumpa99038c2009-02-28 09:07:16 +0000308
John McCallea1471e2010-05-20 01:18:31 +0000309 Note.index = i+5;
Mike Stump08920992009-03-07 02:35:30 +0000310
John McCallea1471e2010-05-20 01:18:31 +0000311 if (isa<CXXThisExpr>(E)) {
312 Note.RequiresCopying = false;
313 Note.flag = BLOCK_FIELD_IS_OBJECT;
Mike Stumpa99038c2009-02-28 09:07:16 +0000314
John McCallea1471e2010-05-20 01:18:31 +0000315 Builder.CreateStore(LoadCXXThis(), Addr);
316 continue;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000317 }
John McCallea1471e2010-05-20 01:18:31 +0000318
319 const BlockDeclRefExpr *BDRE = cast<BlockDeclRefExpr>(E);
Fariborz Jahaniane27e9d62010-11-11 00:11:38 +0000320 Note.RequiresCopying = BlockRequiresCopying(BDRE);
Fariborz Jahanian34999872010-11-13 21:53:34 +0000321 Note.cxxvar_import =
322 BDRE->getCopyConstructorExpr() ? BDRE : 0;
John McCallea1471e2010-05-20 01:18:31 +0000323 const ValueDecl *VD = BDRE->getDecl();
324 QualType T = VD->getType();
Fariborz Jahanian34999872010-11-13 21:53:34 +0000325
John McCallea1471e2010-05-20 01:18:31 +0000326 if (BDRE->isByRef()) {
327 Note.flag = BLOCK_FIELD_IS_BYREF;
328 if (T.isObjCGCWeak())
329 Note.flag |= BLOCK_FIELD_IS_WEAK;
330 } else if (T->isBlockPointerType()) {
331 Note.flag = BLOCK_FIELD_IS_BLOCK;
332 } else {
333 Note.flag = BLOCK_FIELD_IS_OBJECT;
334 }
335
336 if (LocalDeclMap[VD]) {
337 if (BDRE->isByRef()) {
338 llvm::Value *Loc = LocalDeclMap[VD];
339 Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
340 Loc = Builder.CreateLoad(Loc);
341 Builder.CreateStore(Loc, Addr);
342 continue;
343 } else {
Fariborz Jahanianac7362d2010-06-08 20:57:22 +0000344 if (BDRE->getCopyConstructorExpr()) {
345 E = BDRE->getCopyConstructorExpr();
John McCallf1549f62010-07-06 01:34:17 +0000346 PushDestructorCleanup(E->getType(), Addr);
Fariborz Jahanianac7362d2010-06-08 20:57:22 +0000347 }
Fariborz Jahanian59da45a2010-06-04 21:35:44 +0000348 else {
349 E = new (getContext()) DeclRefExpr(const_cast<ValueDecl*>(VD),
Fariborz Jahanian79651722010-06-02 21:35:17 +0000350 VD->getType().getNonReferenceType(),
351 SourceLocation());
Fariborz Jahanian59da45a2010-06-04 21:35:44 +0000352 if (VD->getType()->isReferenceType()) {
353 E = new (getContext())
John McCall2de56d12010-08-25 11:45:40 +0000354 UnaryOperator(const_cast<Expr*>(E), UO_AddrOf,
Fariborz Jahanian59da45a2010-06-04 21:35:44 +0000355 getContext().getPointerType(E->getType()),
356 SourceLocation());
357 }
Fariborz Jahaniandf8b8ea2010-06-04 16:10:00 +0000358 }
359 }
John McCallea1471e2010-05-20 01:18:31 +0000360 }
John McCallea1471e2010-05-20 01:18:31 +0000361
362 if (BDRE->isByRef()) {
363 E = new (getContext())
John McCall2de56d12010-08-25 11:45:40 +0000364 UnaryOperator(const_cast<Expr*>(E), UO_AddrOf,
John McCallea1471e2010-05-20 01:18:31 +0000365 getContext().getPointerType(E->getType()),
366 SourceLocation());
367 }
368
John McCall558d2ab2010-09-15 10:14:12 +0000369 RValue r = EmitAnyExpr(E, AggValueSlot::forAddr(Addr, false, true));
John McCallea1471e2010-05-20 01:18:31 +0000370 if (r.isScalar()) {
371 llvm::Value *Loc = r.getScalarVal();
372 const llvm::Type *Ty = Types[i+BlockFields];
373 if (BDRE->isByRef()) {
374 // E is now the address of the value field, instead, we want the
375 // address of the actual ByRef struct. We optimize this slightly
376 // compared to gcc by not grabbing the forwarding slot as this must
377 // be done during Block_copy for us, and we can postpone the work
378 // until then.
379 CharUnits offset = BlockDecls[BDRE->getDecl()];
380
381 llvm::Value *BlockLiteral = LoadBlockStruct();
382
383 Loc = Builder.CreateGEP(BlockLiteral,
Chris Lattner77b89b82010-06-27 07:15:29 +0000384 llvm::ConstantInt::get(Int64Ty, offset.getQuantity()),
John McCallea1471e2010-05-20 01:18:31 +0000385 "block.literal");
386 Ty = llvm::PointerType::get(Ty, 0);
387 Loc = Builder.CreateBitCast(Loc, Ty);
388 Loc = Builder.CreateLoad(Loc);
389 // Loc = Builder.CreateBitCast(Loc, Ty);
390 }
391 Builder.CreateStore(Loc, Addr);
392 } else if (r.isComplex())
393 // FIXME: implement
394 ErrorUnsupported(BE, "complex in block literal");
395 else if (r.isAggregate())
396 ; // Already created into the destination
397 else
398 assert (0 && "bad block variable");
399 // FIXME: Ensure that the offset created by the backend for
400 // the struct matches the previously computed offset in BlockDecls.
401 }
402 NoteForHelper.resize(NumHelpers);
Mike Stumpcf62d392009-03-06 18:42:23 +0000403
404 // __descriptor
Fariborz Jahanian89ecd412010-08-04 16:57:49 +0000405 llvm::Value *Descriptor = BuildDescriptorBlockDecl(BE, Info, Ty,
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000406 BlockVarLayout,
Mike Stump08920992009-03-07 02:35:30 +0000407 &NoteForHelper);
408 Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
409 Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
Mike Stumpe5fee252009-02-13 16:19:19 +0000410 }
Mike Stump00470a12009-03-05 08:32:30 +0000411
Mike Stumpbd65cac2009-02-19 01:01:04 +0000412 QualType BPT = BE->getType();
Fariborz Jahanian263c4de2010-02-10 23:34:57 +0000413 V = Builder.CreateBitCast(V, ConvertType(BPT));
414 // See if this is a __weak block variable and the must call objc_read_weak
415 // on it.
416 const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>();
417 QualType RES = ftype->getResultType();
418 if (RES.isObjCGCWeak()) {
419 // Must cast argument to id*
420 const llvm::Type *ObjectPtrTy =
421 ConvertType(CGM.getContext().getObjCIdType());
422 const llvm::Type *PtrObjectPtrTy =
423 llvm::PointerType::getUnqual(ObjectPtrTy);
424 V = Builder.CreateBitCast(V, PtrObjectPtrTy);
425 V = CGM.getObjCRuntime().EmitObjCWeakRead(*this, V);
426 }
427 return V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000428}
429
430
Mike Stump2a998142009-03-04 18:17:45 +0000431const llvm::Type *BlockModule::getBlockDescriptorType() {
Mike Stumpab695142009-02-13 15:16:56 +0000432 if (BlockDescriptorType)
433 return BlockDescriptorType;
434
Mike Stumpa5448542009-02-13 15:32:32 +0000435 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000436 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000437
Mike Stumpab695142009-02-13 15:16:56 +0000438 // struct __block_descriptor {
439 // unsigned long reserved;
440 // unsigned long block_size;
Blaine Garst2a7eb282010-02-23 21:51:17 +0000441 //
442 // // later, the following will be added
443 //
444 // struct {
445 // void (*copyHelper)();
446 // void (*copyHelper)();
447 // } helpers; // !!! optional
448 //
449 // const char *signature; // the block signature
450 // const char *layout; // reserved
Mike Stumpab695142009-02-13 15:16:56 +0000451 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000452 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(),
453 UnsignedLongTy,
Mike Stumpa5448542009-02-13 15:32:32 +0000454 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000455 NULL);
456
457 getModule().addTypeName("struct.__block_descriptor",
458 BlockDescriptorType);
459
460 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000461}
462
Mike Stump2a998142009-03-04 18:17:45 +0000463const llvm::Type *BlockModule::getGenericBlockLiteralType() {
Mike Stump9b8a7972009-02-13 15:25:34 +0000464 if (GenericBlockLiteralType)
465 return GenericBlockLiteralType;
466
Mike Stumpa5448542009-02-13 15:32:32 +0000467 const llvm::Type *BlockDescPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000468 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000469
Mike Stump7cbb3602009-02-13 16:01:35 +0000470 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
471 getTypes().ConvertType(getContext().IntTy));
472
Mike Stump9b8a7972009-02-13 15:25:34 +0000473 // struct __block_literal_generic {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000474 // void *__isa;
475 // int __flags;
476 // int __reserved;
477 // void (*__invoke)(void *);
478 // struct __block_descriptor *__descriptor;
Mike Stump9b8a7972009-02-13 15:25:34 +0000479 // };
Blaine Garst2a7eb282010-02-23 21:51:17 +0000480 GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
Owen Anderson47a434f2009-08-05 23:18:46 +0000481 PtrToInt8Ty,
Mike Stump7cbb3602009-02-13 16:01:35 +0000482 IntTy,
483 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000484 PtrToInt8Ty,
Mike Stump9b8a7972009-02-13 15:25:34 +0000485 BlockDescPtrTy,
486 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000487
Mike Stump9b8a7972009-02-13 15:25:34 +0000488 getModule().addTypeName("struct.__block_literal_generic",
489 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000490
Mike Stump9b8a7972009-02-13 15:25:34 +0000491 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000492}
493
Mike Stumpbd65cac2009-02-19 01:01:04 +0000494
Anders Carlssona1736c02009-12-24 21:13:40 +0000495RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E,
496 ReturnValueSlot ReturnValue) {
Mike Stumpa5448542009-02-13 15:32:32 +0000497 const BlockPointerType *BPT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000498 E->getCallee()->getType()->getAs<BlockPointerType>();
Mike Stumpa5448542009-02-13 15:32:32 +0000499
Anders Carlssonacfde802009-02-12 00:39:25 +0000500 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
501
502 // Get a pointer to the generic block literal.
503 const llvm::Type *BlockLiteralTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000504 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000505
506 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000507 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000508 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
509
510 // Get the function pointer from the literal.
511 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Anders Carlssonacfde802009-02-12 00:39:25 +0000512
Mike Stumpa5448542009-02-13 15:32:32 +0000513 BlockLiteral =
514 Builder.CreateBitCast(BlockLiteral,
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000515 llvm::Type::getInt8PtrTy(VMContext),
Anders Carlssonacfde802009-02-12 00:39:25 +0000516 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000517
Anders Carlssonacfde802009-02-12 00:39:25 +0000518 // Add the block literal.
519 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
520 CallArgList Args;
521 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000522
Anders Carlsson782f3972009-04-08 23:13:16 +0000523 QualType FnType = BPT->getPointeeType();
524
Anders Carlssonacfde802009-02-12 00:39:25 +0000525 // And the rest of the arguments.
John McCall183700f2009-09-21 23:43:11 +0000526 EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(),
Anders Carlsson782f3972009-04-08 23:13:16 +0000527 E->arg_begin(), E->arg_end());
Mike Stumpa5448542009-02-13 15:32:32 +0000528
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000529 // Load the function.
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000530 llvm::Value *Func = Builder.CreateLoad(FuncPtr, "tmp");
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000531
John McCall04a67a62010-02-05 21:31:56 +0000532 const FunctionType *FuncTy = FnType->getAs<FunctionType>();
533 QualType ResultType = FuncTy->getResultType();
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000534
Mike Stump1eb44332009-09-09 15:08:12 +0000535 const CGFunctionInfo &FnInfo =
Rafael Espindola264ba482010-03-30 20:24:48 +0000536 CGM.getTypes().getFunctionInfo(ResultType, Args,
537 FuncTy->getExtInfo());
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000539 // Cast the function pointer to the right type.
Mike Stump1eb44332009-09-09 15:08:12 +0000540 const llvm::Type *BlockFTy =
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000541 CGM.getTypes().GetFunctionType(FnInfo, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Owen Anderson96e0fc72009-07-29 22:16:19 +0000543 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000544 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Anders Carlssonacfde802009-02-12 00:39:25 +0000546 // And call the block.
Anders Carlssona1736c02009-12-24 21:13:40 +0000547 return EmitCall(FnInfo, Func, ReturnValue, Args);
Anders Carlssonacfde802009-02-12 00:39:25 +0000548}
Anders Carlssond5cab542009-02-12 17:55:02 +0000549
John McCallea1471e2010-05-20 01:18:31 +0000550void CodeGenFunction::AllocateBlockCXXThisPointer(const CXXThisExpr *E) {
551 assert(BlockCXXThisOffset.isZero() && "already computed 'this' pointer");
552
553 // Figure out what the offset is.
554 QualType T = E->getType();
555 std::pair<CharUnits,CharUnits> TypeInfo = getContext().getTypeInfoInChars(T);
556 CharUnits Offset = getBlockOffset(TypeInfo.first, TypeInfo.second);
557
558 BlockCXXThisOffset = Offset;
559 BlockLayout.push_back(E);
560}
561
562void CodeGenFunction::AllocateBlockDecl(const BlockDeclRefExpr *E) {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000563 const ValueDecl *VD = E->getDecl();
John McCallea1471e2010-05-20 01:18:31 +0000564 CharUnits &Offset = BlockDecls[VD];
Mike Stumpdab514f2009-03-04 03:23:46 +0000565
Mike Stumpdab514f2009-03-04 03:23:46 +0000566 // See if we have already allocated an offset for this variable.
John McCallea1471e2010-05-20 01:18:31 +0000567 if (!Offset.isZero())
568 return;
Mike Stumpea26cb52009-10-21 03:49:08 +0000569
570 // Don't run the expensive check, unless we have to.
Fariborz Jahanian2715b202010-11-15 19:19:38 +0000571 if (!SynthesizeCopyDisposeHelpers)
572 if (E->isByRef() || BlockRequiresCopying(E))
573 SynthesizeCopyDisposeHelpers = true;
Mike Stumpea26cb52009-10-21 03:49:08 +0000574
John McCallea1471e2010-05-20 01:18:31 +0000575 const ValueDecl *D = cast<ValueDecl>(E->getDecl());
Mike Stumpea26cb52009-10-21 03:49:08 +0000576
John McCallea1471e2010-05-20 01:18:31 +0000577 CharUnits Size;
578 CharUnits Align;
579
580 if (E->isByRef()) {
581 llvm::tie(Size,Align) =
582 getContext().getTypeInfoInChars(getContext().VoidPtrTy);
583 } else {
584 Size = getContext().getTypeSizeInChars(D->getType());
585 Align = getContext().getDeclAlign(D);
586 }
587
588 Offset = getBlockOffset(Size, Align);
589 BlockLayout.push_back(E);
Mike Stumpea26cb52009-10-21 03:49:08 +0000590}
591
John McCallee504292010-05-21 04:11:14 +0000592llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const ValueDecl *VD,
593 bool IsByRef) {
Fariborz Jahaniandf8b8ea2010-06-04 16:10:00 +0000594
John McCallea1471e2010-05-20 01:18:31 +0000595 CharUnits offset = BlockDecls[VD];
596 assert(!offset.isZero() && "getting address of unallocated decl");
Mike Stumpdab514f2009-03-04 03:23:46 +0000597
598 llvm::Value *BlockLiteral = LoadBlockStruct();
599 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
Chris Lattner77b89b82010-06-27 07:15:29 +0000600 llvm::ConstantInt::get(Int64Ty, offset.getQuantity()),
Mike Stump58a85142009-03-04 22:48:06 +0000601 "block.literal");
John McCallee504292010-05-21 04:11:14 +0000602 if (IsByRef) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000603 const llvm::Type *PtrStructTy
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000604 = llvm::PointerType::get(BuildByRefType(VD), 0);
Mike Stumpa8b60c92009-03-21 21:00:35 +0000605 // The block literal will need a copy/destroy helper.
Fariborz Jahanian2715b202010-11-15 19:19:38 +0000606 SynthesizeCopyDisposeHelpers = true;
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000607
608 const llvm::Type *Ty = PtrStructTy;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000609 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000610 V = Builder.CreateBitCast(V, Ty);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000611 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000612 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000613 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000614 V = Builder.CreateBitCast(V, PtrStructTy);
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000615 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
616 VD->getNameAsString());
Fariborz Jahaniand33ded52010-05-04 17:59:32 +0000617 if (VD->getType()->isReferenceType())
618 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000619 } else {
John McCall46ec70e2010-10-28 21:37:57 +0000620 const llvm::Type *Ty = CGM.getTypes().ConvertTypeForMem(VD->getType());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000621 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000622 V = Builder.CreateBitCast(V, Ty);
Fariborz Jahanian79651722010-06-02 21:35:17 +0000623 if (VD->getType()->isReferenceType())
Fariborz Jahaniandf8b8ea2010-06-04 16:10:00 +0000624 V = Builder.CreateLoad(V, "ref.tmp");
Mike Stumpdab514f2009-03-04 03:23:46 +0000625 }
626 return V;
627}
628
Mike Stump67a64482009-02-14 22:16:35 +0000629llvm::Constant *
Mike Stump90a90432009-03-04 18:47:42 +0000630BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000631 // Generate the block descriptor.
632 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000633 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
634 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000635
Blaine Garst2a7eb282010-02-23 21:51:17 +0000636 llvm::Constant *DescriptorFields[4];
Mike Stumpa5448542009-02-13 15:32:32 +0000637
Anders Carlssond5cab542009-02-12 17:55:02 +0000638 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000639 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000640
Anders Carlssond5cab542009-02-12 17:55:02 +0000641 // Block literal size. For global blocks we just use the size of the generic
642 // block literal struct.
Ken Dyck687cc4a2010-01-26 13:48:07 +0000643 CharUnits BlockLiteralSize =
644 CGM.GetTargetTypeStoreSize(getGenericBlockLiteralType());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000645 DescriptorFields[1] =
Ken Dyck199c3d62010-01-11 17:06:35 +0000646 llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize.getQuantity());
Blaine Garst2a7eb282010-02-23 21:51:17 +0000647
648 // signature. non-optional ObjC-style method descriptor @encode sequence
649 std::string BlockTypeEncoding;
650 CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
Mike Stumpa5448542009-02-13 15:32:32 +0000651
Blaine Garst2a7eb282010-02-23 21:51:17 +0000652 DescriptorFields[2] = llvm::ConstantExpr::getBitCast(
653 CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty);
654
655 // layout
656 DescriptorFields[3] =
657 llvm::ConstantInt::get(UnsignedLongTy,0);
658
659 // build the structure from the 4 elements
Mike Stumpa5448542009-02-13 15:32:32 +0000660 llvm::Constant *DescriptorStruct =
Blaine Garst2a7eb282010-02-23 21:51:17 +0000661 llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 4, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000662
Anders Carlssond5cab542009-02-12 17:55:02 +0000663 llvm::GlobalVariable *Descriptor =
Owen Anderson1c431b32009-07-08 19:05:04 +0000664 new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000665 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000666 DescriptorStruct, "__block_descriptor_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000667
David Chisnall5e530af2009-11-17 19:33:30 +0000668 int FieldCount = 5;
Anders Carlssond5cab542009-02-12 17:55:02 +0000669 // Generate the constants for the block literal.
David Chisnall5e530af2009-11-17 19:33:30 +0000670
671 std::vector<llvm::Constant*> LiteralFields(FieldCount);
Mike Stumpa5448542009-02-13 15:32:32 +0000672
John McCallee504292010-05-21 04:11:14 +0000673 CGBlockInfo Info(n);
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000674 llvm::Constant *BlockVarLayout;
Mike Stump7f28a9c2009-03-13 23:34:28 +0000675 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000676 llvm::Function *Fn
Fariborz Jahanian7edddb82010-07-28 19:07:18 +0000677 = CodeGenFunction(CGM).GenerateBlockFunction(GlobalDecl(), BE,
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000678 Info, 0, BlockVarLayout,
679 LocalDeclMap);
John McCallee504292010-05-21 04:11:14 +0000680 assert(Info.BlockSize == BlockLiteralSize
Mike Stump4e7a1f72009-02-21 20:00:35 +0000681 && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000682
Anders Carlssond5cab542009-02-12 17:55:02 +0000683 // isa
Daniel Dunbar673431a2010-07-16 00:00:15 +0000684 LiteralFields[0] = CGM.getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000685
Fariborz Jahanian7edddb82010-07-28 19:07:18 +0000686 // __flags
687 unsigned flags = computeBlockFlag(CGM, BE,
688 (BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE));
Blaine Garst2a7eb282010-02-23 21:51:17 +0000689 LiteralFields[1] =
Fariborz Jahanian7edddb82010-07-28 19:07:18 +0000690 llvm::ConstantInt::get(IntTy, flags);
Mike Stumpa5448542009-02-13 15:32:32 +0000691
Anders Carlssond5cab542009-02-12 17:55:02 +0000692 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000693 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000694
Anders Carlssond5cab542009-02-12 17:55:02 +0000695 // Function
696 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000697
Anders Carlssond5cab542009-02-12 17:55:02 +0000698 // Descriptor
699 LiteralFields[4] = Descriptor;
David Chisnall5e530af2009-11-17 19:33:30 +0000700
Mike Stumpa5448542009-02-13 15:32:32 +0000701 llvm::Constant *BlockLiteralStruct =
David Chisnall5e530af2009-11-17 19:33:30 +0000702 llvm::ConstantStruct::get(VMContext, LiteralFields, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000703
704 llvm::GlobalVariable *BlockLiteral =
Owen Anderson1c431b32009-07-08 19:05:04 +0000705 new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000706 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000707 BlockLiteralStruct, "__block_literal_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000708
Anders Carlssond5cab542009-02-12 17:55:02 +0000709 return BlockLiteral;
710}
711
Mike Stump4e7a1f72009-02-21 20:00:35 +0000712llvm::Value *CodeGenFunction::LoadBlockStruct() {
Mike Stumpbf1914b2009-10-20 20:30:01 +0000713 llvm::Value *V = Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()],
714 "self");
715 // For now, we codegen based upon byte offsets.
716 return Builder.CreateBitCast(V, PtrToInt8Ty);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000717}
718
Mike Stump00470a12009-03-05 08:32:30 +0000719llvm::Function *
Fariborz Jahanian564360b2010-06-24 00:08:06 +0000720CodeGenFunction::GenerateBlockFunction(GlobalDecl GD, const BlockExpr *BExpr,
John McCallee504292010-05-21 04:11:14 +0000721 CGBlockInfo &Info,
Mike Stump6cc88f72009-03-20 21:53:12 +0000722 const Decl *OuterFuncDecl,
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000723 llvm::Constant *& BlockVarLayout,
John McCallee504292010-05-21 04:11:14 +0000724 llvm::DenseMap<const Decl*, llvm::Value*> ldm) {
Devang Patel963dfbd2009-04-15 21:51:44 +0000725
726 // Check if we should generate debug info for this block.
727 if (CGM.getDebugInfo())
728 DebugInfo = CGM.getDebugInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Mike Stump7f28a9c2009-03-13 23:34:28 +0000730 // Arrange for local static and local extern declarations to appear
731 // to be local to this function as well, as they are directly referenced
732 // in a block.
733 for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
734 i != ldm.end();
735 ++i) {
736 const VarDecl *VD = dyn_cast<VarDecl>(i->first);
Mike Stump1eb44332009-09-09 15:08:12 +0000737
John McCalld931b082010-08-26 03:08:43 +0000738 if (VD->getStorageClass() == SC_Static || VD->hasExternalStorage())
Mike Stump7f28a9c2009-03-13 23:34:28 +0000739 LocalDeclMap[VD] = i->second;
740 }
741
Ken Dyck687cc4a2010-01-26 13:48:07 +0000742 BlockOffset =
743 CGM.GetTargetTypeStoreSize(CGM.getGenericBlockLiteralType());
Ken Dyck30a8a272010-01-26 19:13:33 +0000744 BlockAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Eli Friedman48f91222009-03-28 03:24:54 +0000745
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000746 const FunctionType *BlockFunctionType = BExpr->getFunctionType();
747 QualType ResultType;
Rafael Espindola264ba482010-03-30 20:24:48 +0000748 FunctionType::ExtInfo EInfo = getFunctionExtInfo(*BlockFunctionType);
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000749 bool IsVariadic;
Mike Stump1eb44332009-09-09 15:08:12 +0000750 if (const FunctionProtoType *FTy =
Fariborz Jahanianda0895d2009-04-11 18:54:57 +0000751 dyn_cast<FunctionProtoType>(BlockFunctionType)) {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000752 ResultType = FTy->getResultType();
753 IsVariadic = FTy->isVariadic();
Mike Stumpb3589f42009-07-30 22:28:39 +0000754 } else {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000755 // K&R style block.
756 ResultType = BlockFunctionType->getResultType();
757 IsVariadic = false;
758 }
Mike Stumpa5448542009-02-13 15:32:32 +0000759
Anders Carlssond5cab542009-02-12 17:55:02 +0000760 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000761
Mike Stumpea26cb52009-10-21 03:49:08 +0000762 CurFuncDecl = OuterFuncDecl;
763
Chris Lattner161d36d2009-02-28 19:01:03 +0000764 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlssond5cab542009-02-12 17:55:02 +0000765
Mike Stumpea26cb52009-10-21 03:49:08 +0000766 IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor");
Mike Stumpadaaad32009-10-20 02:12:22 +0000767
John McCallea1471e2010-05-20 01:18:31 +0000768 // Build the block struct now.
John McCallee504292010-05-21 04:11:14 +0000769 AllocateAllBlockDeclRefs(*this, Info);
Mike Stumpea26cb52009-10-21 03:49:08 +0000770
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000771 // Capture block layout info. here.
772 if (CGM.getContext().getLangOptions().ObjC1)
Fariborz Jahanianc5904b42010-09-11 01:27:29 +0000773 BlockVarLayout = CGM.getObjCRuntime().GCBlockLayout(*this, BlockLayout);
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000774 else
775 BlockVarLayout = llvm::Constant::getNullValue(PtrToInt8Ty);
776
Fariborz Jahanian2715b202010-11-15 19:19:38 +0000777 QualType ParmTy = getContext().getBlockParmType(
778 SynthesizeCopyDisposeHelpers,
779 BlockLayout);
John McCallea1471e2010-05-20 01:18:31 +0000780
Anders Carlssond5cab542009-02-12 17:55:02 +0000781 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000782 ImplicitParamDecl *SelfDecl =
John McCall2888b652010-04-30 21:35:41 +0000783 ImplicitParamDecl::Create(getContext(), const_cast<BlockDecl*>(BD),
Mike Stumpadaaad32009-10-20 02:12:22 +0000784 SourceLocation(), II,
785 ParmTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000786
Anders Carlssond5cab542009-02-12 17:55:02 +0000787 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump4e7a1f72009-02-21 20:00:35 +0000788 BlockStructDecl = SelfDecl;
Mike Stumpa5448542009-02-13 15:32:32 +0000789
Steve Naroffe78b8092009-03-13 16:56:44 +0000790 for (BlockDecl::param_const_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000791 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000792 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000793
794 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +0000795 CGM.getTypes().getFunctionInfo(ResultType, Args, EInfo);
Anders Carlssond5cab542009-02-12 17:55:02 +0000796
Anders Carlssond5cab542009-02-12 17:55:02 +0000797 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000798 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic);
Mike Stumpa5448542009-02-13 15:32:32 +0000799
Douglas Gregor35415f52010-05-25 17:04:15 +0000800 MangleBuffer Name;
Fariborz Jahanian564360b2010-06-24 00:08:06 +0000801 CGM.getMangledName(GD, Name, BD);
Mike Stumpa5448542009-02-13 15:32:32 +0000802 llvm::Function *Fn =
Douglas Gregor6e5d9b02010-05-25 17:12:30 +0000803 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
804 Name.getString(), &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000805
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000806 CGM.SetInternalFunctionAttributes(BD, Fn, FI);
Fariborz Jahanian8404f672010-08-13 00:19:55 +0000807 StartFunction(BD, ResultType, Fn, Args,
808 BExpr->getBody()->getLocEnd());
809
810 CurFuncDecl = OuterFuncDecl;
811
Fariborz Jahanian16ac5ce2010-06-28 18:58:34 +0000812 QualType FnType(BlockFunctionType, 0);
Fariborz Jahanianef160b42010-06-28 19:42:10 +0000813 bool HasPrototype = isa<FunctionProtoType>(BlockFunctionType);
Fariborz Jahanian16ac5ce2010-06-28 18:58:34 +0000814
815 IdentifierInfo *ID = &getContext().Idents.get(Name.getString());
816 CurCodeDecl = FunctionDecl::Create(getContext(),
817 getContext().getTranslationUnitDecl(),
818 SourceLocation(), ID, FnType,
819 0,
John McCalld931b082010-08-26 03:08:43 +0000820 SC_Static,
821 SC_None,
Fariborz Jahanian16ac5ce2010-06-28 18:58:34 +0000822 false, HasPrototype);
Fariborz Jahanian8404f672010-08-13 00:19:55 +0000823 if (FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FnType)) {
824 const FunctionDecl *CFD = dyn_cast<FunctionDecl>(CurCodeDecl);
825 FunctionDecl *FD = const_cast<FunctionDecl *>(CFD);
826 llvm::SmallVector<ParmVarDecl*, 16> Params;
827 for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i)
828 Params.push_back(ParmVarDecl::Create(getContext(), FD,
829 SourceLocation(), 0,
830 FT->getArgType(i), /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +0000831 SC_None, SC_None, 0));
Fariborz Jahanian8404f672010-08-13 00:19:55 +0000832 FD->setParams(Params.data(), Params.size());
833 }
Fariborz Jahanian16ac5ce2010-06-28 18:58:34 +0000834
Fariborz Jahanian8404f672010-08-13 00:19:55 +0000835
John McCallea1471e2010-05-20 01:18:31 +0000836 // If we have a C++ 'this' reference, go ahead and force it into
837 // existence now.
838 if (Info.CXXThisRef) {
839 assert(!BlockCXXThisOffset.isZero() &&
840 "haven't yet allocated 'this' reference");
841
842 // TODO: I have a dream that one day this will be typed.
843 llvm::Value *BlockLiteral = LoadBlockStruct();
844 llvm::Value *ThisPtrRaw =
845 Builder.CreateConstInBoundsGEP1_64(BlockLiteral,
846 BlockCXXThisOffset.getQuantity(),
847 "this.ptr.raw");
848
849 const llvm::Type *Ty =
850 CGM.getTypes().ConvertType(Info.CXXThisRef->getType());
851 Ty = llvm::PointerType::get(Ty, 0);
852 llvm::Value *ThisPtr = Builder.CreateBitCast(ThisPtrRaw, Ty, "this.ptr");
853
854 CXXThisValue = Builder.CreateLoad(ThisPtr, "this");
855 }
856
John McCallee504292010-05-21 04:11:14 +0000857 // If we have an Objective C 'self' reference, go ahead and force it
858 // into existence now.
859 if (Info.NeedsObjCSelf) {
860 ValueDecl *Self = cast<ObjCMethodDecl>(CurFuncDecl)->getSelfDecl();
861 LocalDeclMap[Self] = GetAddrOfBlockDecl(Self, false);
862 }
863
Mike Stumpb289b3f2009-10-01 22:29:41 +0000864 // Save a spot to insert the debug information for all the BlockDeclRefDecls.
865 llvm::BasicBlock *entry = Builder.GetInsertBlock();
866 llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint();
867 --entry_ptr;
868
Chris Lattner161d36d2009-02-28 19:01:03 +0000869 EmitStmt(BExpr->getBody());
Mike Stumpb289b3f2009-10-01 22:29:41 +0000870
Mike Stumpde8c5c72009-10-01 00:27:30 +0000871 // Remember where we were...
872 llvm::BasicBlock *resume = Builder.GetInsertBlock();
Mike Stumpb289b3f2009-10-01 22:29:41 +0000873
Mike Stumpde8c5c72009-10-01 00:27:30 +0000874 // Go back to the entry.
Mike Stumpb289b3f2009-10-01 22:29:41 +0000875 ++entry_ptr;
876 Builder.SetInsertPoint(entry, entry_ptr);
877
Mike Stumpb1a6e682009-09-30 02:43:10 +0000878 if (CGDebugInfo *DI = getDebugInfo()) {
Mike Stumpb1a6e682009-09-30 02:43:10 +0000879 // Emit debug information for all the BlockDeclRefDecls.
John McCallea1471e2010-05-20 01:18:31 +0000880 // FIXME: also for 'this'
881 for (unsigned i = 0, e = BlockLayout.size(); i != e; ++i) {
882 if (const BlockDeclRefExpr *BDRE =
883 dyn_cast<BlockDeclRefExpr>(BlockLayout[i])) {
Mike Stumpb1a6e682009-09-30 02:43:10 +0000884 const ValueDecl *D = BDRE->getDecl();
885 DI->setLocation(D->getLocation());
886 DI->EmitDeclareOfBlockDeclRefVariable(BDRE,
887 LocalDeclMap[getBlockStructDecl()],
888 Builder, this);
889 }
890 }
Mike Stumpb1a6e682009-09-30 02:43:10 +0000891 }
Mike Stumpde8c5c72009-10-01 00:27:30 +0000892 // And resume where we left off.
893 if (resume == 0)
894 Builder.ClearInsertionPoint();
895 else
896 Builder.SetInsertPoint(resume);
Mike Stumpb1a6e682009-09-30 02:43:10 +0000897
Chris Lattner161d36d2009-02-28 19:01:03 +0000898 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlssond5cab542009-02-12 17:55:02 +0000899
Mike Stump8a2b4b12009-02-25 23:33:13 +0000900 // The runtime needs a minimum alignment of a void *.
Ken Dyck30a8a272010-01-26 19:13:33 +0000901 CharUnits MinAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Ken Dyck199c3d62010-01-11 17:06:35 +0000902 BlockOffset = CharUnits::fromQuantity(
Ken Dyck30a8a272010-01-26 19:13:33 +0000903 llvm::RoundUpToAlignment(BlockOffset.getQuantity(),
904 MinAlign.getQuantity()));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000905
John McCallee504292010-05-21 04:11:14 +0000906 Info.BlockSize = BlockOffset;
907 Info.BlockAlign = BlockAlign;
908 Info.BlockLayout = BlockLayout;
Fariborz Jahanian2715b202010-11-15 19:19:38 +0000909 Info.BlockHasCopyDispose = SynthesizeCopyDisposeHelpers;
Anders Carlssond5cab542009-02-12 17:55:02 +0000910 return Fn;
911}
Mike Stumpa99038c2009-02-28 09:07:16 +0000912
John McCallea1471e2010-05-20 01:18:31 +0000913CharUnits BlockFunction::getBlockOffset(CharUnits Size, CharUnits Align) {
914 assert((Align.isPositive()) && "alignment must be 1 byte or more");
Mike Stumpa99038c2009-02-28 09:07:16 +0000915
Ken Dyck199c3d62010-01-11 17:06:35 +0000916 CharUnits OldOffset = BlockOffset;
Mike Stumpa99038c2009-02-28 09:07:16 +0000917
918 // Ensure proper alignment, even if it means we have to have a gap
Ken Dyck199c3d62010-01-11 17:06:35 +0000919 BlockOffset = CharUnits::fromQuantity(
Ken Dyck30a8a272010-01-26 19:13:33 +0000920 llvm::RoundUpToAlignment(BlockOffset.getQuantity(), Align.getQuantity()));
Mike Stumpa99038c2009-02-28 09:07:16 +0000921 BlockAlign = std::max(Align, BlockAlign);
Mike Stump00470a12009-03-05 08:32:30 +0000922
Ken Dyck199c3d62010-01-11 17:06:35 +0000923 CharUnits Pad = BlockOffset - OldOffset;
924 if (Pad.isPositive()) {
Mike Stumpa99038c2009-02-28 09:07:16 +0000925 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
Ken Dyck199c3d62010-01-11 17:06:35 +0000926 llvm::APInt(32,
927 Pad.getQuantity()),
Mike Stumpa99038c2009-02-28 09:07:16 +0000928 ArrayType::Normal, 0);
Douglas Gregorba55ec22010-05-11 18:17:16 +0000929 ValueDecl *PadDecl = VarDecl::Create(getContext(),
930 getContext().getTranslationUnitDecl(),
931 SourceLocation(),
Douglas Gregor16573fa2010-04-19 22:54:31 +0000932 0, QualType(PadTy), 0,
John McCalld931b082010-08-26 03:08:43 +0000933 SC_None, SC_None);
John McCallea1471e2010-05-20 01:18:31 +0000934 Expr *E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
935 SourceLocation());
936 BlockLayout.push_back(E);
Mike Stumpa99038c2009-02-28 09:07:16 +0000937 }
Mike Stumpa99038c2009-02-28 09:07:16 +0000938
939 BlockOffset += Size;
John McCallea1471e2010-05-20 01:18:31 +0000940 return BlockOffset - Size;
Mike Stumpa99038c2009-02-28 09:07:16 +0000941}
Mike Stumpdab514f2009-03-04 03:23:46 +0000942
Mike Stump08920992009-03-07 02:35:30 +0000943llvm::Constant *BlockFunction::
Fariborz Jahanian2715b202010-11-15 19:19:38 +0000944GenerateCopyHelperFunction(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000945 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000946 QualType R = getContext().VoidTy;
947
948 FunctionArgList Args;
949 // FIXME: This leaks
Mike Stump08920992009-03-07 02:35:30 +0000950 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +0000951 ImplicitParamDecl::Create(getContext(), 0,
952 SourceLocation(), 0,
Mike Stump08920992009-03-07 02:35:30 +0000953 getContext().getPointerType(getContext().VoidTy));
954 Args.push_back(std::make_pair(Dst, Dst->getType()));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000955 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +0000956 ImplicitParamDecl::Create(getContext(), 0,
957 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000958 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000959 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Mike Stumpa4f668f2009-03-06 01:33:24 +0000961 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +0000962 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000963
Mike Stump3899a7f2009-06-05 23:26:36 +0000964 // FIXME: We'd like to put these into a mergable by content, with
965 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000966 CodeGenTypes &Types = CGM.getTypes();
967 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
968
969 llvm::Function *Fn =
970 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000971 "__copy_helper_block_", &CGM.getModule());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000972
973 IdentifierInfo *II
974 = &CGM.getContext().Idents.get("__copy_helper_block_");
975
976 FunctionDecl *FD = FunctionDecl::Create(getContext(),
977 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000978 SourceLocation(), II, R, 0,
John McCalld931b082010-08-26 03:08:43 +0000979 SC_Static,
980 SC_None,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000981 false,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000982 true);
983 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump08920992009-03-07 02:35:30 +0000984
985 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
986 llvm::Type *PtrPtrT;
Mike Stump08920992009-03-07 02:35:30 +0000987
Mike Stumpb7477cf2009-04-10 18:52:28 +0000988 if (NoteForHelperp) {
989 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump08920992009-03-07 02:35:30 +0000990
Owen Anderson96e0fc72009-07-29 22:16:19 +0000991 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000992 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
993 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump08920992009-03-07 02:35:30 +0000994
Mike Stumpb7477cf2009-04-10 18:52:28 +0000995 llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000996 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000997 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000998 DstObj = Builder.CreateBitCast(DstObj, PtrPtrT);
999 DstObj = Builder.CreateLoad(DstObj);
Mike Stump08920992009-03-07 02:35:30 +00001000
Mike Stumpb7477cf2009-04-10 18:52:28 +00001001 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
1002 int flag = NoteForHelper[i].flag;
1003 int index = NoteForHelper[i].index;
Mike Stump08920992009-03-07 02:35:30 +00001004
Mike Stumpb7477cf2009-04-10 18:52:28 +00001005 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
1006 || NoteForHelper[i].RequiresCopying) {
1007 llvm::Value *Srcv = SrcObj;
1008 Srcv = Builder.CreateStructGEP(Srcv, index);
Fariborz Jahaniane2204552010-11-16 19:29:39 +00001009 llvm::Value *Dstv;
Fariborz Jahanian34999872010-11-13 21:53:34 +00001010 if (NoteForHelper[i].cxxvar_import) {
Fariborz Jahaniane2204552010-11-16 19:29:39 +00001011 if (NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) {
1012 const ValueDecl *VD = NoteForHelper[i].cxxvar_import->getDecl();
1013 const llvm::Type *PtrStructTy
1014 = llvm::PointerType::get(CGF.BuildByRefType(VD), 0);
1015 Srcv = Builder.CreateLoad(Srcv);
1016 Srcv = Builder.CreateBitCast(Srcv, PtrStructTy);
1017 Srcv = Builder.CreateStructGEP(Srcv, CGF.getByRefValueLLVMField(VD),
1018 VD->getNameAsString());
Fariborz Jahaniane38be612010-11-17 00:21:28 +00001019
1020 Dstv = Builder.CreateBitCast(DstObj, PtrStructTy);
Fariborz Jahaniane2204552010-11-16 19:29:39 +00001021 Dstv = Builder.CreateStructGEP(Dstv, CGF.getByRefValueLLVMField(VD),
1022 VD->getNameAsString());
1023 CGF.EmitSynthesizedCXXCopyCtor(Dstv, Srcv,
1024 NoteForHelper[i].cxxvar_import);
1025 }
1026 else {
1027 Srcv = Builder.CreateBitCast(Srcv,
1028 llvm::PointerType::get(PtrToInt8Ty, 0));
1029 Dstv = Builder.CreateStructGEP(DstObj, index);
1030 CGF.EmitSynthesizedCXXCopyCtor(Dstv, Srcv,
1031 NoteForHelper[i].cxxvar_import);
1032 }
Fariborz Jahanian34999872010-11-13 21:53:34 +00001033 }
1034 else {
Fariborz Jahaniane2204552010-11-16 19:29:39 +00001035 Srcv = Builder.CreateBitCast(Srcv,
1036 llvm::PointerType::get(PtrToInt8Ty, 0));
1037 Dstv = Builder.CreateStructGEP(DstObj, index);
Fariborz Jahanian34999872010-11-13 21:53:34 +00001038 Srcv = Builder.CreateLoad(Srcv);
1039 Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
1040 llvm::Value *N = llvm::ConstantInt::get(CGF.Int32Ty, flag);
1041 llvm::Value *F = CGM.getBlockObjectAssign();
1042 Builder.CreateCall3(F, Dstv, Srcv, N);
1043 }
Mike Stumpb7477cf2009-04-10 18:52:28 +00001044 }
Mike Stump08920992009-03-07 02:35:30 +00001045 }
1046 }
1047
Mike Stumpa4f668f2009-03-06 01:33:24 +00001048 CGF.FinishFunction();
1049
Owen Anderson3c4972d2009-07-29 18:54:39 +00001050 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +00001051}
1052
Mike Stumpcf62d392009-03-06 18:42:23 +00001053llvm::Constant *BlockFunction::
Fariborz Jahanian2715b202010-11-15 19:19:38 +00001054GenerateDestroyHelperFunction(const llvm::StructType* T,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001055 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +00001056 QualType R = getContext().VoidTy;
1057
1058 FunctionArgList Args;
1059 // FIXME: This leaks
1060 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001061 ImplicitParamDecl::Create(getContext(), 0,
1062 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +00001063 getContext().getPointerType(getContext().VoidTy));
1064
1065 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001066
Mike Stumpa4f668f2009-03-06 01:33:24 +00001067 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +00001068 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stumpa4f668f2009-03-06 01:33:24 +00001069
Mike Stump3899a7f2009-06-05 23:26:36 +00001070 // FIXME: We'd like to put these into a mergable by content, with
1071 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +00001072 CodeGenTypes &Types = CGM.getTypes();
1073 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1074
1075 llvm::Function *Fn =
1076 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001077 "__destroy_helper_block_", &CGM.getModule());
Mike Stumpa4f668f2009-03-06 01:33:24 +00001078
1079 IdentifierInfo *II
1080 = &CGM.getContext().Idents.get("__destroy_helper_block_");
1081
1082 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1083 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001084 SourceLocation(), II, R, 0,
John McCalld931b082010-08-26 03:08:43 +00001085 SC_Static,
1086 SC_None,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001087 false, true);
Mike Stumpa4f668f2009-03-06 01:33:24 +00001088 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1edf6b62009-03-07 02:53:18 +00001089
Mike Stumpb7477cf2009-04-10 18:52:28 +00001090 if (NoteForHelperp) {
1091 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump1edf6b62009-03-07 02:53:18 +00001092
Mike Stumpb7477cf2009-04-10 18:52:28 +00001093 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
1094 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +00001095 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +00001096 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
1097 SrcObj = Builder.CreateLoad(SrcObj);
Fariborz Jahanian34999872010-11-13 21:53:34 +00001098 EHScopeStack::stable_iterator CleanupDepth = CGF.EHStack.stable_begin();
Mike Stumpb7477cf2009-04-10 18:52:28 +00001099 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
1100 int flag = NoteForHelper[i].flag;
1101 int index = NoteForHelper[i].index;
Mike Stump1edf6b62009-03-07 02:53:18 +00001102
Mike Stumpb7477cf2009-04-10 18:52:28 +00001103 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
1104 || NoteForHelper[i].RequiresCopying) {
1105 llvm::Value *Srcv = SrcObj;
1106 Srcv = Builder.CreateStructGEP(Srcv, index);
1107 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001108 llvm::PointerType::get(PtrToInt8Ty, 0));
Fariborz Jahanian34999872010-11-13 21:53:34 +00001109 if (NoteForHelper[i].cxxvar_import) {
1110 const BlockDeclRefExpr *BDRE = NoteForHelper[i].cxxvar_import;
1111 const Expr *E = BDRE->getCopyConstructorExpr();
1112 QualType ClassTy = E->getType();
1113 QualType PtrClassTy = getContext().getPointerType(ClassTy);
1114 const llvm::Type *t = CGM.getTypes().ConvertType(PtrClassTy);
Fariborz Jahaniane2204552010-11-16 19:29:39 +00001115
1116 if (NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) {
1117 const ValueDecl *VD = NoteForHelper[i].cxxvar_import->getDecl();
1118 const llvm::Type *PtrStructTy
1119 = llvm::PointerType::get(CGF.BuildByRefType(VD), 0);
1120 Srcv = Builder.CreateLoad(Srcv);
1121 Srcv = Builder.CreateBitCast(Srcv, PtrStructTy);
1122 Srcv = Builder.CreateStructGEP(Srcv, CGF.getByRefValueLLVMField(VD),
1123 VD->getNameAsString());
1124
1125 }
Fariborz Jahanian34999872010-11-13 21:53:34 +00001126 Srcv = Builder.CreateBitCast(Srcv, t);
1127 CGF.PushDestructorCleanup(ClassTy, Srcv);
1128 }
1129 else {
1130 Srcv = Builder.CreateLoad(Srcv);
1131 BuildBlockRelease(Srcv, flag);
1132 }
Mike Stumpb7477cf2009-04-10 18:52:28 +00001133 }
Mike Stump1edf6b62009-03-07 02:53:18 +00001134 }
Fariborz Jahanian34999872010-11-13 21:53:34 +00001135 CGF.PopCleanupBlocks(CleanupDepth);
Mike Stump1edf6b62009-03-07 02:53:18 +00001136 }
1137
Mike Stumpa4f668f2009-03-06 01:33:24 +00001138 CGF.FinishFunction();
1139
Owen Anderson3c4972d2009-07-29 18:54:39 +00001140 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpa4f668f2009-03-06 01:33:24 +00001141}
1142
Mike Stump08920992009-03-07 02:35:30 +00001143llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001144 std::vector<HelperInfo> *NoteForHelper) {
Fariborz Jahanian2715b202010-11-15 19:19:38 +00001145 return CodeGenFunction(CGM).GenerateCopyHelperFunction(T, NoteForHelper);
Mike Stumpa4f668f2009-03-06 01:33:24 +00001146}
1147
Mike Stump08920992009-03-07 02:35:30 +00001148llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001149 std::vector<HelperInfo> *NoteForHelperp) {
Fariborz Jahanian2715b202010-11-15 19:19:38 +00001150 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(T, NoteForHelperp);
Mike Stumpdab514f2009-03-04 03:23:46 +00001151}
Mike Stump797b6322009-03-05 01:23:13 +00001152
Mike Stumpee094222009-03-06 06:12:24 +00001153llvm::Constant *BlockFunction::
1154GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
Mike Stump45031c02009-03-06 02:29:21 +00001155 QualType R = getContext().VoidTy;
1156
1157 FunctionArgList Args;
1158 // FIXME: This leaks
Mike Stumpee094222009-03-06 06:12:24 +00001159 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +00001160 ImplicitParamDecl::Create(getContext(), 0,
1161 SourceLocation(), 0,
Mike Stumpee094222009-03-06 06:12:24 +00001162 getContext().getPointerType(getContext().VoidTy));
1163 Args.push_back(std::make_pair(Dst, Dst->getType()));
1164
1165 // FIXME: This leaks
Mike Stump45031c02009-03-06 02:29:21 +00001166 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001167 ImplicitParamDecl::Create(getContext(), 0,
1168 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001169 getContext().getPointerType(getContext().VoidTy));
Mike Stump45031c02009-03-06 02:29:21 +00001170 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001171
Mike Stump45031c02009-03-06 02:29:21 +00001172 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +00001173 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stump45031c02009-03-06 02:29:21 +00001174
Mike Stump45031c02009-03-06 02:29:21 +00001175 CodeGenTypes &Types = CGM.getTypes();
1176 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1177
Mike Stump3899a7f2009-06-05 23:26:36 +00001178 // FIXME: We'd like to put these into a mergable by content, with
1179 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001180 llvm::Function *Fn =
1181 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001182 "__Block_byref_id_object_copy_", &CGM.getModule());
Mike Stump45031c02009-03-06 02:29:21 +00001183
1184 IdentifierInfo *II
1185 = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
1186
1187 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1188 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001189 SourceLocation(), II, R, 0,
John McCalld931b082010-08-26 03:08:43 +00001190 SC_Static,
1191 SC_None,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001192 false, true);
Mike Stump45031c02009-03-06 02:29:21 +00001193 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpee094222009-03-06 06:12:24 +00001194
1195 // dst->x
1196 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001197 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001198 V = Builder.CreateLoad(V);
Mike Stumpee094222009-03-06 06:12:24 +00001199 V = Builder.CreateStructGEP(V, 6, "x");
1200 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
1201
1202 // src->x
1203 V = CGF.GetAddrOfLocalVar(Src);
1204 V = Builder.CreateLoad(V);
1205 V = Builder.CreateBitCast(V, T);
1206 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001207 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpee094222009-03-06 06:12:24 +00001208 llvm::Value *SrcObj = Builder.CreateLoad(V);
Mike Stump1eb44332009-09-09 15:08:12 +00001209
Mike Stumpee094222009-03-06 06:12:24 +00001210 flag |= BLOCK_BYREF_CALLER;
1211
Chris Lattner77b89b82010-06-27 07:15:29 +00001212 llvm::Value *N = llvm::ConstantInt::get(CGF.Int32Ty, flag);
Daniel Dunbar673431a2010-07-16 00:00:15 +00001213 llvm::Value *F = CGM.getBlockObjectAssign();
Mike Stumpee094222009-03-06 06:12:24 +00001214 Builder.CreateCall3(F, DstObj, SrcObj, N);
1215
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 Stump1851b682009-03-06 04:53:30 +00001221llvm::Constant *
1222BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
1223 int flag) {
Mike Stump45031c02009-03-06 02:29:21 +00001224 QualType R = getContext().VoidTy;
1225
1226 FunctionArgList Args;
1227 // FIXME: This leaks
1228 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001229 ImplicitParamDecl::Create(getContext(), 0,
1230 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001231 getContext().getPointerType(getContext().VoidTy));
1232
1233 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001234
Mike Stump45031c02009-03-06 02:29:21 +00001235 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +00001236 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stump45031c02009-03-06 02:29:21 +00001237
Mike Stump45031c02009-03-06 02:29:21 +00001238 CodeGenTypes &Types = CGM.getTypes();
1239 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1240
Mike Stump3899a7f2009-06-05 23:26:36 +00001241 // FIXME: We'd like to put these into a mergable by content, with
1242 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001243 llvm::Function *Fn =
1244 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001245 "__Block_byref_id_object_dispose_",
Mike Stump45031c02009-03-06 02:29:21 +00001246 &CGM.getModule());
1247
1248 IdentifierInfo *II
1249 = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
1250
1251 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1252 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001253 SourceLocation(), II, R, 0,
John McCalld931b082010-08-26 03:08:43 +00001254 SC_Static,
1255 SC_None,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001256 false, true);
Mike Stump45031c02009-03-06 02:29:21 +00001257 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1851b682009-03-06 04:53:30 +00001258
1259 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001260 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001261 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001262 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001263 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001264 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001265
Mike Stump1851b682009-03-06 04:53:30 +00001266 flag |= BLOCK_BYREF_CALLER;
1267 BuildBlockRelease(V, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001268 CGF.FinishFunction();
1269
Owen Anderson3c4972d2009-07-29 18:54:39 +00001270 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001271}
1272
Mike Stumpee094222009-03-06 06:12:24 +00001273llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001274 int Flag, unsigned Align) {
1275 // All alignments below that of pointer alignment collapse down to just
Mike Stump3899a7f2009-06-05 23:26:36 +00001276 // pointer alignment, as we always have at least that much alignment to begin
1277 // with.
1278 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001279
Mike Stump3899a7f2009-06-05 23:26:36 +00001280 // As an optimization, we only generate a single function of each kind we
1281 // might need. We need a different one for each alignment and for each
1282 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001283 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1284 llvm::Constant *&Entry = CGM.AssignCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001285 if (Entry)
1286 return Entry;
Chris Lattner10976d92009-12-05 08:21:30 +00001287 return Entry = CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, Flag);
Mike Stump45031c02009-03-06 02:29:21 +00001288}
1289
Mike Stump1851b682009-03-06 04:53:30 +00001290llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001291 int Flag,
Mike Stump3899a7f2009-06-05 23:26:36 +00001292 unsigned Align) {
1293 // All alignments below that of pointer alignment collpase down to just
1294 // pointer alignment, as we always have at least that much alignment to begin
1295 // with.
1296 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001297
Mike Stump3899a7f2009-06-05 23:26:36 +00001298 // As an optimization, we only generate a single function of each kind we
1299 // might need. We need a different one for each alignment and for each
1300 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001301 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1302 llvm::Constant *&Entry = CGM.DestroyCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001303 if (Entry)
1304 return Entry;
Chris Lattner10976d92009-12-05 08:21:30 +00001305 return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, Flag);
Mike Stump45031c02009-03-06 02:29:21 +00001306}
1307
Mike Stump1851b682009-03-06 04:53:30 +00001308void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Daniel Dunbar673431a2010-07-16 00:00:15 +00001309 llvm::Value *F = CGM.getBlockObjectDispose();
Mike Stump1851b682009-03-06 04:53:30 +00001310 llvm::Value *N;
Mike Stump797b6322009-03-05 01:23:13 +00001311 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Chris Lattner77b89b82010-06-27 07:15:29 +00001312 N = llvm::ConstantInt::get(CGF.Int32Ty, flag);
Mike Stump797b6322009-03-05 01:23:13 +00001313 Builder.CreateCall2(F, V, N);
1314}
Mike Stump00470a12009-03-05 08:32:30 +00001315
1316ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stump08920992009-03-07 02:35:30 +00001317
1318BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1319 CGBuilderTy &B)
Chris Lattner77b89b82010-06-27 07:15:29 +00001320 : CGM(cgm), VMContext(cgm.getLLVMContext()), CGF(cgf), Builder(B) {
Owen Anderson0032b272009-08-13 21:57:51 +00001321 PtrToInt8Ty = llvm::PointerType::getUnqual(
1322 llvm::Type::getInt8Ty(VMContext));
Mike Stump08920992009-03-07 02:35:30 +00001323
Fariborz Jahanian2715b202010-11-15 19:19:38 +00001324 SynthesizeCopyDisposeHelpers = false;
Mike Stump08920992009-03-07 02:35:30 +00001325}