blob: 36767711b48c51c5edf12d012f0b4efa5cae50af [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 }
John McCallee504292010-05-21 04:11:14 +0000134 else if (isa<CXXThisExpr>(S))
John McCallea1471e2010-05-20 01:18:31 +0000135 Info.CXXThisRef = cast<CXXThisExpr>(S);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000136}
137
John McCallee504292010-05-21 04:11:14 +0000138/// CanBlockBeGlobal - Given a CGBlockInfo struct, determines if a block can be
Mike Stump58a85142009-03-04 22:48:06 +0000139/// declared as a global variable instead of on the stack.
John McCallee504292010-05-21 04:11:14 +0000140static bool CanBlockBeGlobal(const CGBlockInfo &Info) {
Mike Stumpea26cb52009-10-21 03:49:08 +0000141 return Info.DeclRefs.empty();
142}
143
144/// AllocateAllBlockDeclRefs - Preallocate all nested BlockDeclRefExprs to
145/// ensure we can generate the debug information for the parameter for the block
146/// invoke function.
John McCallee504292010-05-21 04:11:14 +0000147static void AllocateAllBlockDeclRefs(CodeGenFunction &CGF, CGBlockInfo &Info) {
John McCallea1471e2010-05-20 01:18:31 +0000148 if (Info.CXXThisRef)
John McCallee504292010-05-21 04:11:14 +0000149 CGF.AllocateBlockCXXThisPointer(Info.CXXThisRef);
Mike Stumpea26cb52009-10-21 03:49:08 +0000150
151 for (size_t i = 0; i < Info.DeclRefs.size(); ++i)
John McCallee504292010-05-21 04:11:14 +0000152 CGF.AllocateBlockDecl(Info.DeclRefs[i]);
153
154 if (Info.NeedsObjCSelf) {
155 ValueDecl *Self = cast<ObjCMethodDecl>(CGF.CurFuncDecl)->getSelfDecl();
156 BlockDeclRefExpr *BDRE =
John McCallf89e55a2010-11-18 06:31:45 +0000157 new (CGF.getContext()) BlockDeclRefExpr(Self, Self->getType(), VK_RValue,
John McCallee504292010-05-21 04:11:14 +0000158 SourceLocation(), false);
159 Info.DeclRefs.push_back(BDRE);
160 CGF.AllocateBlockDecl(BDRE);
161 }
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000162}
163
Fariborz Jahanian7edddb82010-07-28 19:07:18 +0000164static unsigned computeBlockFlag(CodeGenModule &CGM,
165 const BlockExpr *BE, unsigned flags) {
166 QualType BPT = BE->getType();
167 const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>();
168 QualType ResultType = ftype->getResultType();
169
170 CallArgList Args;
171 CodeGenTypes &Types = CGM.getTypes();
172 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, Args,
173 FunctionType::ExtInfo());
174 if (CGM.ReturnTypeUsesSRet(FnInfo))
175 flags |= CodeGenFunction::BLOCK_USE_STRET;
176 return flags;
177}
178
Mike Stump58a85142009-03-04 22:48:06 +0000179// FIXME: Push most into CGM, passing down a few bits, like current function
180// name.
Mike Stump8a2b4b12009-02-25 23:33:13 +0000181llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000182 std::string Name = CurFn->getName();
John McCallee504292010-05-21 04:11:14 +0000183 CGBlockInfo Info(Name.c_str());
184 Info.InnerBlocks.insert(BE->getBlockDecl());
185 CollectBlockDeclRefInfo(BE->getBody(), Info);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000186
187 // Check if the block can be global.
Mike Stump58a85142009-03-04 22:48:06 +0000188 // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like
189 // to just have one code path. We should move this function into CGM and pass
190 // CGF, then we can just check to see if CGF is 0.
Mike Stumpdab514f2009-03-04 03:23:46 +0000191 if (0 && CanBlockBeGlobal(Info))
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000192 return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
Mike Stump00470a12009-03-05 08:32:30 +0000193
David Chisnall5e530af2009-11-17 19:33:30 +0000194 size_t BlockFields = 5;
195
David Chisnall5e530af2009-11-17 19:33:30 +0000196 std::vector<llvm::Constant*> Elts(BlockFields);
197
Mike Stumpe5fee252009-02-13 16:19:19 +0000198 llvm::Constant *C;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000199 llvm::Value *V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000200
John McCall711c52b2011-01-05 12:14:39 +0000201 bool hasWeakBlockVariable = false;
202
Mike Stumpe5fee252009-02-13 16:19:19 +0000203 {
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000204 llvm::Constant *BlockVarLayout;
Mike Stumpe5fee252009-02-13 16:19:19 +0000205 // C = BuildBlockStructInitlist();
Blaine Garsta36e2232010-03-05 01:29:59 +0000206 unsigned int flags = BLOCK_HAS_SIGNATURE;
David Chisnall5e530af2009-11-17 19:33:30 +0000207
Mike Stump00470a12009-03-05 08:32:30 +0000208 // We run this first so that we set BlockHasCopyDispose from the entire
209 // block literal.
210 // __invoke
Mike Stump00470a12009-03-05 08:32:30 +0000211 llvm::Function *Fn
Fariborz Jahanian564360b2010-06-24 00:08:06 +0000212 = CodeGenFunction(CGM).GenerateBlockFunction(CurGD, BE, Info, CurFuncDecl,
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000213 BlockVarLayout,
John McCallee504292010-05-21 04:11:14 +0000214 LocalDeclMap);
Fariborz Jahanian2715b202010-11-15 19:19:38 +0000215 SynthesizeCopyDisposeHelpers |= Info.BlockHasCopyDispose;
Mike Stump00470a12009-03-05 08:32:30 +0000216 Elts[3] = Fn;
217
Mike Stumpf5408fe2009-05-16 07:57:57 +0000218 // FIXME: Don't use BlockHasCopyDispose, it is set more often then
219 // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); }
John McCallee504292010-05-21 04:11:14 +0000220 if (Info.BlockHasCopyDispose)
Mike Stumpe5fee252009-02-13 16:19:19 +0000221 flags |= BLOCK_HAS_COPY_DISPOSE;
Fariborz Jahanian34999872010-11-13 21:53:34 +0000222 // This block may import a c++ object.
223 if (Info.HasCXXObject)
224 flags |= BLOCK_HAS_CXX_OBJ;
Mike Stumpe5fee252009-02-13 16:19:19 +0000225
Mike Stump7d6dc4f2009-02-13 20:17:16 +0000226 // __isa
Mike Stump59c5b112009-02-13 19:29:27 +0000227 C = CGM.getNSConcreteStackBlock();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000228 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
Mike Stump00470a12009-03-05 08:32:30 +0000229 Elts[0] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000230
231 // __flags
Fariborz Jahanian7edddb82010-07-28 19:07:18 +0000232 flags = computeBlockFlag(CGM, BE, flags);
Mike Stumpe5fee252009-02-13 16:19:19 +0000233 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
234 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000235 C = llvm::ConstantInt::get(IntTy, flags);
Mike Stump00470a12009-03-05 08:32:30 +0000236 Elts[1] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000237
238 // __reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000239 C = llvm::ConstantInt::get(IntTy, 0);
Mike Stump00470a12009-03-05 08:32:30 +0000240 Elts[2] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000241
John McCallee504292010-05-21 04:11:14 +0000242 if (Info.BlockLayout.empty()) {
Mike Stumpcf62d392009-03-06 18:42:23 +0000243 // __descriptor
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000244 C = llvm::Constant::getNullValue(PtrToInt8Ty);
245 Elts[4] = BuildDescriptorBlockDecl(BE, Info, 0, C, 0);
Mike Stumpcf62d392009-03-06 18:42:23 +0000246
Mike Stump5570cfe2009-03-01 20:07:53 +0000247 // Optimize to being a global block.
248 Elts[0] = CGM.getNSConcreteGlobalBlock();
Blaine Garsta36e2232010-03-05 01:29:59 +0000249
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000250 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
Mike Stump5570cfe2009-03-01 20:07:53 +0000251
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000252 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000253
Owen Anderson1c431b32009-07-08 19:05:04 +0000254 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000255 llvm::GlobalValue::InternalLinkage, C,
256 "__block_holder_tmp_" +
257 llvm::Twine(CGM.getGlobalUniqueCount()));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000258 QualType BPT = BE->getType();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000259 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000260 return C;
261 }
Mike Stump00470a12009-03-05 08:32:30 +0000262
John McCallee504292010-05-21 04:11:14 +0000263 std::vector<const llvm::Type *> Types(BlockFields+Info.BlockLayout.size());
Mike Stump08920992009-03-07 02:35:30 +0000264 for (int i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000265 Types[i] = Elts[i]->getType();
Mike Stump08920992009-03-07 02:35:30 +0000266 Types[4] = PtrToInt8Ty;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000267
John McCallee504292010-05-21 04:11:14 +0000268 for (unsigned i = 0, n = Info.BlockLayout.size(); i != n; ++i) {
269 const Expr *E = Info.BlockLayout[i];
Mike Stumpa99038c2009-02-28 09:07:16 +0000270 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
271 QualType Ty = E->getType();
Mike Stumpdab514f2009-03-04 03:23:46 +0000272 if (BDRE && BDRE->isByRef()) {
John McCall711c52b2011-01-05 12:14:39 +0000273 if (BDRE->getDecl()->getType().isObjCGCWeak())
274 hasWeakBlockVariable = true;
Fariborz Jahanian79651722010-06-02 21:35:17 +0000275 Types[i+BlockFields] =
276 llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0);
277 } else if (BDRE && BDRE->getDecl()->getType()->isReferenceType()) {
278 Types[i+BlockFields] = llvm::PointerType::get(ConvertType(Ty), 0);
279 } else
David Chisnall5e530af2009-11-17 19:33:30 +0000280 Types[i+BlockFields] = ConvertType(Ty);
Mike Stumpa99038c2009-02-28 09:07:16 +0000281 }
Mike Stump8a2b4b12009-02-25 23:33:13 +0000282
Owen Anderson47a434f2009-08-05 23:18:46 +0000283 llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000284
285 llvm::AllocaInst *A = CreateTempAlloca(Ty);
John McCallee504292010-05-21 04:11:14 +0000286 A->setAlignment(Info.BlockAlign.getQuantity());
Mike Stump8a2b4b12009-02-25 23:33:13 +0000287 V = A;
288
John McCallea1471e2010-05-20 01:18:31 +0000289 // Build layout / cleanup information for all the data entries in the
290 // layout, and write the enclosing fields into the type.
John McCallee504292010-05-21 04:11:14 +0000291 std::vector<HelperInfo> NoteForHelper(Info.BlockLayout.size());
John McCallea1471e2010-05-20 01:18:31 +0000292 unsigned NumHelpers = 0;
Mike Stump08920992009-03-07 02:35:30 +0000293
294 for (unsigned i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000295 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
Mike Stump00470a12009-03-05 08:32:30 +0000296
John McCallee504292010-05-21 04:11:14 +0000297 for (unsigned i=0; i < Info.BlockLayout.size(); ++i) {
298 const Expr *E = Info.BlockLayout[i];
Mike Stump8a2b4b12009-02-25 23:33:13 +0000299
John McCallea1471e2010-05-20 01:18:31 +0000300 // Skip padding.
301 if (isa<DeclRefExpr>(E)) continue;
Mike Stumpa99038c2009-02-28 09:07:16 +0000302
John McCallea1471e2010-05-20 01:18:31 +0000303 llvm::Value* Addr = Builder.CreateStructGEP(V, i+BlockFields, "tmp");
304 HelperInfo &Note = NoteForHelper[NumHelpers++];
Mike Stumpa99038c2009-02-28 09:07:16 +0000305
John McCallea1471e2010-05-20 01:18:31 +0000306 Note.index = i+5;
Mike Stump08920992009-03-07 02:35:30 +0000307
John McCallea1471e2010-05-20 01:18:31 +0000308 if (isa<CXXThisExpr>(E)) {
309 Note.RequiresCopying = false;
310 Note.flag = BLOCK_FIELD_IS_OBJECT;
Mike Stumpa99038c2009-02-28 09:07:16 +0000311
John McCallea1471e2010-05-20 01:18:31 +0000312 Builder.CreateStore(LoadCXXThis(), Addr);
313 continue;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000314 }
John McCallea1471e2010-05-20 01:18:31 +0000315
316 const BlockDeclRefExpr *BDRE = cast<BlockDeclRefExpr>(E);
Fariborz Jahaniane27e9d62010-11-11 00:11:38 +0000317 Note.RequiresCopying = BlockRequiresCopying(BDRE);
Fariborz Jahanian34999872010-11-13 21:53:34 +0000318 Note.cxxvar_import =
319 BDRE->getCopyConstructorExpr() ? BDRE : 0;
John McCallea1471e2010-05-20 01:18:31 +0000320 const ValueDecl *VD = BDRE->getDecl();
321 QualType T = VD->getType();
Fariborz Jahanian34999872010-11-13 21:53:34 +0000322
John McCallea1471e2010-05-20 01:18:31 +0000323 if (BDRE->isByRef()) {
324 Note.flag = BLOCK_FIELD_IS_BYREF;
325 if (T.isObjCGCWeak())
326 Note.flag |= BLOCK_FIELD_IS_WEAK;
327 } else if (T->isBlockPointerType()) {
328 Note.flag = BLOCK_FIELD_IS_BLOCK;
329 } else {
330 Note.flag = BLOCK_FIELD_IS_OBJECT;
331 }
332
333 if (LocalDeclMap[VD]) {
334 if (BDRE->isByRef()) {
335 llvm::Value *Loc = LocalDeclMap[VD];
336 Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
337 Loc = Builder.CreateLoad(Loc);
338 Builder.CreateStore(Loc, Addr);
339 continue;
340 } else {
Fariborz Jahanianac7362d2010-06-08 20:57:22 +0000341 if (BDRE->getCopyConstructorExpr()) {
342 E = BDRE->getCopyConstructorExpr();
John McCallf1549f62010-07-06 01:34:17 +0000343 PushDestructorCleanup(E->getType(), Addr);
John McCallf89e55a2010-11-18 06:31:45 +0000344 } else {
345 E = new (getContext()) DeclRefExpr(const_cast<ValueDecl*>(VD),
346 VD->getType().getNonReferenceType(),
347 Expr::getValueKindForType(VD->getType()),
348 SourceLocation());
349 if (VD->getType()->isReferenceType()) {
350 E = new (getContext())
351 UnaryOperator(const_cast<Expr*>(E), UO_AddrOf,
352 getContext().getPointerType(E->getType()),
353 VK_RValue, OK_Ordinary, SourceLocation());
Fariborz Jahaniandf8b8ea2010-06-04 16:10:00 +0000354 }
355 }
John McCallea1471e2010-05-20 01:18:31 +0000356 }
John McCallf89e55a2010-11-18 06:31:45 +0000357 }
John McCallea1471e2010-05-20 01:18:31 +0000358
359 if (BDRE->isByRef()) {
360 E = new (getContext())
John McCall2de56d12010-08-25 11:45:40 +0000361 UnaryOperator(const_cast<Expr*>(E), UO_AddrOf,
John McCallea1471e2010-05-20 01:18:31 +0000362 getContext().getPointerType(E->getType()),
John McCallf89e55a2010-11-18 06:31:45 +0000363 VK_RValue, OK_Ordinary, SourceLocation());
John McCallea1471e2010-05-20 01:18:31 +0000364 }
365
John McCall558d2ab2010-09-15 10:14:12 +0000366 RValue r = EmitAnyExpr(E, AggValueSlot::forAddr(Addr, false, true));
John McCallea1471e2010-05-20 01:18:31 +0000367 if (r.isScalar()) {
368 llvm::Value *Loc = r.getScalarVal();
369 const llvm::Type *Ty = Types[i+BlockFields];
370 if (BDRE->isByRef()) {
371 // E is now the address of the value field, instead, we want the
372 // address of the actual ByRef struct. We optimize this slightly
373 // compared to gcc by not grabbing the forwarding slot as this must
374 // be done during Block_copy for us, and we can postpone the work
375 // until then.
376 CharUnits offset = BlockDecls[BDRE->getDecl()];
377
378 llvm::Value *BlockLiteral = LoadBlockStruct();
379
380 Loc = Builder.CreateGEP(BlockLiteral,
Chris Lattner77b89b82010-06-27 07:15:29 +0000381 llvm::ConstantInt::get(Int64Ty, offset.getQuantity()),
John McCallea1471e2010-05-20 01:18:31 +0000382 "block.literal");
383 Ty = llvm::PointerType::get(Ty, 0);
384 Loc = Builder.CreateBitCast(Loc, Ty);
385 Loc = Builder.CreateLoad(Loc);
386 // Loc = Builder.CreateBitCast(Loc, Ty);
387 }
388 Builder.CreateStore(Loc, Addr);
389 } else if (r.isComplex())
390 // FIXME: implement
391 ErrorUnsupported(BE, "complex in block literal");
392 else if (r.isAggregate())
393 ; // Already created into the destination
394 else
395 assert (0 && "bad block variable");
396 // FIXME: Ensure that the offset created by the backend for
397 // the struct matches the previously computed offset in BlockDecls.
398 }
399 NoteForHelper.resize(NumHelpers);
Mike Stumpcf62d392009-03-06 18:42:23 +0000400
401 // __descriptor
Fariborz Jahanian89ecd412010-08-04 16:57:49 +0000402 llvm::Value *Descriptor = BuildDescriptorBlockDecl(BE, Info, Ty,
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000403 BlockVarLayout,
Mike Stump08920992009-03-07 02:35:30 +0000404 &NoteForHelper);
405 Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
406 Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
Mike Stumpe5fee252009-02-13 16:19:19 +0000407 }
Mike Stump00470a12009-03-05 08:32:30 +0000408
Mike Stumpbd65cac2009-02-19 01:01:04 +0000409 QualType BPT = BE->getType();
Fariborz Jahanian263c4de2010-02-10 23:34:57 +0000410 V = Builder.CreateBitCast(V, ConvertType(BPT));
John McCall711c52b2011-01-05 12:14:39 +0000411
412 // We must call objc_read_weak on the block literal itself if it closes
413 // on any __weak __block variables. For some reason.
414 if (hasWeakBlockVariable) {
415 const llvm::Type *OrigTy = V->getType();
416
Fariborz Jahanian263c4de2010-02-10 23:34:57 +0000417 // Must cast argument to id*
418 const llvm::Type *ObjectPtrTy =
419 ConvertType(CGM.getContext().getObjCIdType());
420 const llvm::Type *PtrObjectPtrTy =
421 llvm::PointerType::getUnqual(ObjectPtrTy);
422 V = Builder.CreateBitCast(V, PtrObjectPtrTy);
John McCall711c52b2011-01-05 12:14:39 +0000423 V = CGM.getObjCRuntime().EmitObjCWeakRead(*this, V);
424
425 // Cast back to the original type.
426 V = Builder.CreateBitCast(V, OrigTy);
Fariborz Jahanian263c4de2010-02-10 23:34:57 +0000427 }
428 return V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000429}
430
431
Mike Stump2a998142009-03-04 18:17:45 +0000432const llvm::Type *BlockModule::getBlockDescriptorType() {
Mike Stumpab695142009-02-13 15:16:56 +0000433 if (BlockDescriptorType)
434 return BlockDescriptorType;
435
Mike Stumpa5448542009-02-13 15:32:32 +0000436 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000437 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000438
Mike Stumpab695142009-02-13 15:16:56 +0000439 // struct __block_descriptor {
440 // unsigned long reserved;
441 // unsigned long block_size;
Blaine Garst2a7eb282010-02-23 21:51:17 +0000442 //
443 // // later, the following will be added
444 //
445 // struct {
446 // void (*copyHelper)();
447 // void (*copyHelper)();
448 // } helpers; // !!! optional
449 //
450 // const char *signature; // the block signature
451 // const char *layout; // reserved
Mike Stumpab695142009-02-13 15:16:56 +0000452 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000453 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(),
454 UnsignedLongTy,
Mike Stumpa5448542009-02-13 15:32:32 +0000455 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000456 NULL);
457
458 getModule().addTypeName("struct.__block_descriptor",
459 BlockDescriptorType);
460
461 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000462}
463
Mike Stump2a998142009-03-04 18:17:45 +0000464const llvm::Type *BlockModule::getGenericBlockLiteralType() {
Mike Stump9b8a7972009-02-13 15:25:34 +0000465 if (GenericBlockLiteralType)
466 return GenericBlockLiteralType;
467
Mike Stumpa5448542009-02-13 15:32:32 +0000468 const llvm::Type *BlockDescPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000469 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000470
Mike Stump7cbb3602009-02-13 16:01:35 +0000471 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
472 getTypes().ConvertType(getContext().IntTy));
473
Mike Stump9b8a7972009-02-13 15:25:34 +0000474 // struct __block_literal_generic {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000475 // void *__isa;
476 // int __flags;
477 // int __reserved;
478 // void (*__invoke)(void *);
479 // struct __block_descriptor *__descriptor;
Mike Stump9b8a7972009-02-13 15:25:34 +0000480 // };
Blaine Garst2a7eb282010-02-23 21:51:17 +0000481 GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
Owen Anderson47a434f2009-08-05 23:18:46 +0000482 PtrToInt8Ty,
Mike Stump7cbb3602009-02-13 16:01:35 +0000483 IntTy,
484 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000485 PtrToInt8Ty,
Mike Stump9b8a7972009-02-13 15:25:34 +0000486 BlockDescPtrTy,
487 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000488
Mike Stump9b8a7972009-02-13 15:25:34 +0000489 getModule().addTypeName("struct.__block_literal_generic",
490 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000491
Mike Stump9b8a7972009-02-13 15:25:34 +0000492 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000493}
494
Mike Stumpbd65cac2009-02-19 01:01:04 +0000495
Anders Carlssona1736c02009-12-24 21:13:40 +0000496RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E,
497 ReturnValueSlot ReturnValue) {
Mike Stumpa5448542009-02-13 15:32:32 +0000498 const BlockPointerType *BPT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000499 E->getCallee()->getType()->getAs<BlockPointerType>();
Mike Stumpa5448542009-02-13 15:32:32 +0000500
Anders Carlssonacfde802009-02-12 00:39:25 +0000501 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
502
503 // Get a pointer to the generic block literal.
504 const llvm::Type *BlockLiteralTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000505 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000506
507 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000508 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000509 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
510
511 // Get the function pointer from the literal.
512 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Anders Carlssonacfde802009-02-12 00:39:25 +0000513
Mike Stumpa5448542009-02-13 15:32:32 +0000514 BlockLiteral =
515 Builder.CreateBitCast(BlockLiteral,
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000516 llvm::Type::getInt8PtrTy(VMContext),
Anders Carlssonacfde802009-02-12 00:39:25 +0000517 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000518
Anders Carlssonacfde802009-02-12 00:39:25 +0000519 // Add the block literal.
520 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
521 CallArgList Args;
522 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000523
Anders Carlsson782f3972009-04-08 23:13:16 +0000524 QualType FnType = BPT->getPointeeType();
525
Anders Carlssonacfde802009-02-12 00:39:25 +0000526 // And the rest of the arguments.
John McCall183700f2009-09-21 23:43:11 +0000527 EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(),
Anders Carlsson782f3972009-04-08 23:13:16 +0000528 E->arg_begin(), E->arg_end());
Mike Stumpa5448542009-02-13 15:32:32 +0000529
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000530 // Load the function.
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000531 llvm::Value *Func = Builder.CreateLoad(FuncPtr, "tmp");
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000532
John McCall04a67a62010-02-05 21:31:56 +0000533 const FunctionType *FuncTy = FnType->getAs<FunctionType>();
534 QualType ResultType = FuncTy->getResultType();
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000535
Mike Stump1eb44332009-09-09 15:08:12 +0000536 const CGFunctionInfo &FnInfo =
Rafael Espindola264ba482010-03-30 20:24:48 +0000537 CGM.getTypes().getFunctionInfo(ResultType, Args,
538 FuncTy->getExtInfo());
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000540 // Cast the function pointer to the right type.
Mike Stump1eb44332009-09-09 15:08:12 +0000541 const llvm::Type *BlockFTy =
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000542 CGM.getTypes().GetFunctionType(FnInfo, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Owen Anderson96e0fc72009-07-29 22:16:19 +0000544 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000545 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Anders Carlssonacfde802009-02-12 00:39:25 +0000547 // And call the block.
Anders Carlssona1736c02009-12-24 21:13:40 +0000548 return EmitCall(FnInfo, Func, ReturnValue, Args);
Anders Carlssonacfde802009-02-12 00:39:25 +0000549}
Anders Carlssond5cab542009-02-12 17:55:02 +0000550
John McCallea1471e2010-05-20 01:18:31 +0000551void CodeGenFunction::AllocateBlockCXXThisPointer(const CXXThisExpr *E) {
552 assert(BlockCXXThisOffset.isZero() && "already computed 'this' pointer");
553
554 // Figure out what the offset is.
555 QualType T = E->getType();
556 std::pair<CharUnits,CharUnits> TypeInfo = getContext().getTypeInfoInChars(T);
557 CharUnits Offset = getBlockOffset(TypeInfo.first, TypeInfo.second);
558
559 BlockCXXThisOffset = Offset;
560 BlockLayout.push_back(E);
561}
562
563void CodeGenFunction::AllocateBlockDecl(const BlockDeclRefExpr *E) {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000564 const ValueDecl *VD = E->getDecl();
John McCallea1471e2010-05-20 01:18:31 +0000565 CharUnits &Offset = BlockDecls[VD];
Mike Stumpdab514f2009-03-04 03:23:46 +0000566
Mike Stumpdab514f2009-03-04 03:23:46 +0000567 // See if we have already allocated an offset for this variable.
John McCallea1471e2010-05-20 01:18:31 +0000568 if (!Offset.isZero())
569 return;
Mike Stumpea26cb52009-10-21 03:49:08 +0000570
571 // Don't run the expensive check, unless we have to.
Fariborz Jahanian2715b202010-11-15 19:19:38 +0000572 if (!SynthesizeCopyDisposeHelpers)
573 if (E->isByRef() || BlockRequiresCopying(E))
574 SynthesizeCopyDisposeHelpers = true;
Mike Stumpea26cb52009-10-21 03:49:08 +0000575
John McCallea1471e2010-05-20 01:18:31 +0000576 const ValueDecl *D = cast<ValueDecl>(E->getDecl());
Mike Stumpea26cb52009-10-21 03:49:08 +0000577
John McCallea1471e2010-05-20 01:18:31 +0000578 CharUnits Size;
579 CharUnits Align;
580
581 if (E->isByRef()) {
582 llvm::tie(Size,Align) =
583 getContext().getTypeInfoInChars(getContext().VoidPtrTy);
584 } else {
585 Size = getContext().getTypeSizeInChars(D->getType());
586 Align = getContext().getDeclAlign(D);
587 }
588
589 Offset = getBlockOffset(Size, Align);
590 BlockLayout.push_back(E);
Mike Stumpea26cb52009-10-21 03:49:08 +0000591}
592
John McCallee504292010-05-21 04:11:14 +0000593llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const ValueDecl *VD,
594 bool IsByRef) {
Fariborz Jahaniandf8b8ea2010-06-04 16:10:00 +0000595
John McCallea1471e2010-05-20 01:18:31 +0000596 CharUnits offset = BlockDecls[VD];
597 assert(!offset.isZero() && "getting address of unallocated decl");
Mike Stumpdab514f2009-03-04 03:23:46 +0000598
599 llvm::Value *BlockLiteral = LoadBlockStruct();
600 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
Chris Lattner77b89b82010-06-27 07:15:29 +0000601 llvm::ConstantInt::get(Int64Ty, offset.getQuantity()),
Mike Stump58a85142009-03-04 22:48:06 +0000602 "block.literal");
John McCallee504292010-05-21 04:11:14 +0000603 if (IsByRef) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000604 const llvm::Type *PtrStructTy
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000605 = llvm::PointerType::get(BuildByRefType(VD), 0);
Mike Stumpa8b60c92009-03-21 21:00:35 +0000606 // The block literal will need a copy/destroy helper.
Fariborz Jahanian2715b202010-11-15 19:19:38 +0000607 SynthesizeCopyDisposeHelpers = true;
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000608
609 const llvm::Type *Ty = PtrStructTy;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000610 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000611 V = Builder.CreateBitCast(V, Ty);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000612 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000613 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000614 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000615 V = Builder.CreateBitCast(V, PtrStructTy);
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000616 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
617 VD->getNameAsString());
Fariborz Jahaniand33ded52010-05-04 17:59:32 +0000618 if (VD->getType()->isReferenceType())
619 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000620 } else {
John McCall46ec70e2010-10-28 21:37:57 +0000621 const llvm::Type *Ty = CGM.getTypes().ConvertTypeForMem(VD->getType());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000622 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000623 V = Builder.CreateBitCast(V, Ty);
Fariborz Jahanian79651722010-06-02 21:35:17 +0000624 if (VD->getType()->isReferenceType())
Fariborz Jahaniandf8b8ea2010-06-04 16:10:00 +0000625 V = Builder.CreateLoad(V, "ref.tmp");
Mike Stumpdab514f2009-03-04 03:23:46 +0000626 }
627 return V;
628}
629
Mike Stump67a64482009-02-14 22:16:35 +0000630llvm::Constant *
Mike Stump90a90432009-03-04 18:47:42 +0000631BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000632 // Generate the block descriptor.
633 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000634 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
635 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000636
Blaine Garst2a7eb282010-02-23 21:51:17 +0000637 llvm::Constant *DescriptorFields[4];
Mike Stumpa5448542009-02-13 15:32:32 +0000638
Anders Carlssond5cab542009-02-12 17:55:02 +0000639 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000640 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000641
Anders Carlssond5cab542009-02-12 17:55:02 +0000642 // Block literal size. For global blocks we just use the size of the generic
643 // block literal struct.
Ken Dyck687cc4a2010-01-26 13:48:07 +0000644 CharUnits BlockLiteralSize =
645 CGM.GetTargetTypeStoreSize(getGenericBlockLiteralType());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000646 DescriptorFields[1] =
Ken Dyck199c3d62010-01-11 17:06:35 +0000647 llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize.getQuantity());
Blaine Garst2a7eb282010-02-23 21:51:17 +0000648
649 // signature. non-optional ObjC-style method descriptor @encode sequence
650 std::string BlockTypeEncoding;
651 CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
Mike Stumpa5448542009-02-13 15:32:32 +0000652
Blaine Garst2a7eb282010-02-23 21:51:17 +0000653 DescriptorFields[2] = llvm::ConstantExpr::getBitCast(
654 CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty);
655
656 // layout
657 DescriptorFields[3] =
658 llvm::ConstantInt::get(UnsignedLongTy,0);
659
660 // build the structure from the 4 elements
Mike Stumpa5448542009-02-13 15:32:32 +0000661 llvm::Constant *DescriptorStruct =
Blaine Garst2a7eb282010-02-23 21:51:17 +0000662 llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 4, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000663
Anders Carlssond5cab542009-02-12 17:55:02 +0000664 llvm::GlobalVariable *Descriptor =
Owen Anderson1c431b32009-07-08 19:05:04 +0000665 new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000666 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000667 DescriptorStruct, "__block_descriptor_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000668
David Chisnall5e530af2009-11-17 19:33:30 +0000669 int FieldCount = 5;
Anders Carlssond5cab542009-02-12 17:55:02 +0000670 // Generate the constants for the block literal.
David Chisnall5e530af2009-11-17 19:33:30 +0000671
672 std::vector<llvm::Constant*> LiteralFields(FieldCount);
Mike Stumpa5448542009-02-13 15:32:32 +0000673
John McCallee504292010-05-21 04:11:14 +0000674 CGBlockInfo Info(n);
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000675 llvm::Constant *BlockVarLayout;
Mike Stump7f28a9c2009-03-13 23:34:28 +0000676 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000677 llvm::Function *Fn
Fariborz Jahanian7edddb82010-07-28 19:07:18 +0000678 = CodeGenFunction(CGM).GenerateBlockFunction(GlobalDecl(), BE,
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000679 Info, 0, BlockVarLayout,
680 LocalDeclMap);
John McCallee504292010-05-21 04:11:14 +0000681 assert(Info.BlockSize == BlockLiteralSize
Mike Stump4e7a1f72009-02-21 20:00:35 +0000682 && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000683
Anders Carlssond5cab542009-02-12 17:55:02 +0000684 // isa
Daniel Dunbar673431a2010-07-16 00:00:15 +0000685 LiteralFields[0] = CGM.getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000686
Fariborz Jahanian7edddb82010-07-28 19:07:18 +0000687 // __flags
688 unsigned flags = computeBlockFlag(CGM, BE,
689 (BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE));
Blaine Garst2a7eb282010-02-23 21:51:17 +0000690 LiteralFields[1] =
Fariborz Jahanian7edddb82010-07-28 19:07:18 +0000691 llvm::ConstantInt::get(IntTy, flags);
Mike Stumpa5448542009-02-13 15:32:32 +0000692
Anders Carlssond5cab542009-02-12 17:55:02 +0000693 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000694 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000695
Anders Carlssond5cab542009-02-12 17:55:02 +0000696 // Function
697 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000698
Anders Carlssond5cab542009-02-12 17:55:02 +0000699 // Descriptor
700 LiteralFields[4] = Descriptor;
David Chisnall5e530af2009-11-17 19:33:30 +0000701
Mike Stumpa5448542009-02-13 15:32:32 +0000702 llvm::Constant *BlockLiteralStruct =
David Chisnall5e530af2009-11-17 19:33:30 +0000703 llvm::ConstantStruct::get(VMContext, LiteralFields, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000704
705 llvm::GlobalVariable *BlockLiteral =
Owen Anderson1c431b32009-07-08 19:05:04 +0000706 new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000707 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000708 BlockLiteralStruct, "__block_literal_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000709
Anders Carlssond5cab542009-02-12 17:55:02 +0000710 return BlockLiteral;
711}
712
Mike Stump4e7a1f72009-02-21 20:00:35 +0000713llvm::Value *CodeGenFunction::LoadBlockStruct() {
Mike Stumpbf1914b2009-10-20 20:30:01 +0000714 llvm::Value *V = Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()],
715 "self");
716 // For now, we codegen based upon byte offsets.
717 return Builder.CreateBitCast(V, PtrToInt8Ty);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000718}
719
Mike Stump00470a12009-03-05 08:32:30 +0000720llvm::Function *
Fariborz Jahanian564360b2010-06-24 00:08:06 +0000721CodeGenFunction::GenerateBlockFunction(GlobalDecl GD, const BlockExpr *BExpr,
John McCallee504292010-05-21 04:11:14 +0000722 CGBlockInfo &Info,
Mike Stump6cc88f72009-03-20 21:53:12 +0000723 const Decl *OuterFuncDecl,
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000724 llvm::Constant *& BlockVarLayout,
John McCallee504292010-05-21 04:11:14 +0000725 llvm::DenseMap<const Decl*, llvm::Value*> ldm) {
Devang Patel963dfbd2009-04-15 21:51:44 +0000726
727 // Check if we should generate debug info for this block.
728 if (CGM.getDebugInfo())
729 DebugInfo = CGM.getDebugInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Mike Stump7f28a9c2009-03-13 23:34:28 +0000731 // Arrange for local static and local extern declarations to appear
732 // to be local to this function as well, as they are directly referenced
733 // in a block.
734 for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
735 i != ldm.end();
736 ++i) {
737 const VarDecl *VD = dyn_cast<VarDecl>(i->first);
Mike Stump1eb44332009-09-09 15:08:12 +0000738
John McCalld931b082010-08-26 03:08:43 +0000739 if (VD->getStorageClass() == SC_Static || VD->hasExternalStorage())
Mike Stump7f28a9c2009-03-13 23:34:28 +0000740 LocalDeclMap[VD] = i->second;
741 }
742
Ken Dyck687cc4a2010-01-26 13:48:07 +0000743 BlockOffset =
744 CGM.GetTargetTypeStoreSize(CGM.getGenericBlockLiteralType());
Ken Dyck30a8a272010-01-26 19:13:33 +0000745 BlockAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Eli Friedman48f91222009-03-28 03:24:54 +0000746
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000747 const FunctionType *BlockFunctionType = BExpr->getFunctionType();
748 QualType ResultType;
Rafael Espindola264ba482010-03-30 20:24:48 +0000749 FunctionType::ExtInfo EInfo = getFunctionExtInfo(*BlockFunctionType);
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000750 bool IsVariadic;
Mike Stump1eb44332009-09-09 15:08:12 +0000751 if (const FunctionProtoType *FTy =
Fariborz Jahanianda0895d2009-04-11 18:54:57 +0000752 dyn_cast<FunctionProtoType>(BlockFunctionType)) {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000753 ResultType = FTy->getResultType();
754 IsVariadic = FTy->isVariadic();
Mike Stumpb3589f42009-07-30 22:28:39 +0000755 } else {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000756 // K&R style block.
757 ResultType = BlockFunctionType->getResultType();
758 IsVariadic = false;
759 }
Mike Stumpa5448542009-02-13 15:32:32 +0000760
Anders Carlssond5cab542009-02-12 17:55:02 +0000761 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000762
Mike Stumpea26cb52009-10-21 03:49:08 +0000763 CurFuncDecl = OuterFuncDecl;
764
Chris Lattner161d36d2009-02-28 19:01:03 +0000765 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlssond5cab542009-02-12 17:55:02 +0000766
Mike Stumpea26cb52009-10-21 03:49:08 +0000767 IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor");
Mike Stumpadaaad32009-10-20 02:12:22 +0000768
John McCallea1471e2010-05-20 01:18:31 +0000769 // Build the block struct now.
John McCallee504292010-05-21 04:11:14 +0000770 AllocateAllBlockDeclRefs(*this, Info);
Mike Stumpea26cb52009-10-21 03:49:08 +0000771
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000772 // Capture block layout info. here.
773 if (CGM.getContext().getLangOptions().ObjC1)
Fariborz Jahanianc5904b42010-09-11 01:27:29 +0000774 BlockVarLayout = CGM.getObjCRuntime().GCBlockLayout(*this, BlockLayout);
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000775 else
776 BlockVarLayout = llvm::Constant::getNullValue(PtrToInt8Ty);
777
Fariborz Jahanian2715b202010-11-15 19:19:38 +0000778 QualType ParmTy = getContext().getBlockParmType(
779 SynthesizeCopyDisposeHelpers,
780 BlockLayout);
John McCallea1471e2010-05-20 01:18:31 +0000781
Anders Carlssond5cab542009-02-12 17:55:02 +0000782 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000783 ImplicitParamDecl *SelfDecl =
John McCall2888b652010-04-30 21:35:41 +0000784 ImplicitParamDecl::Create(getContext(), const_cast<BlockDecl*>(BD),
Mike Stumpadaaad32009-10-20 02:12:22 +0000785 SourceLocation(), II,
786 ParmTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000787
Anders Carlssond5cab542009-02-12 17:55:02 +0000788 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump4e7a1f72009-02-21 20:00:35 +0000789 BlockStructDecl = SelfDecl;
Mike Stumpa5448542009-02-13 15:32:32 +0000790
Steve Naroffe78b8092009-03-13 16:56:44 +0000791 for (BlockDecl::param_const_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000792 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000793 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000794
795 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +0000796 CGM.getTypes().getFunctionInfo(ResultType, Args, EInfo);
Anders Carlssond5cab542009-02-12 17:55:02 +0000797
Anders Carlssond5cab542009-02-12 17:55:02 +0000798 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000799 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic);
Mike Stumpa5448542009-02-13 15:32:32 +0000800
Douglas Gregor35415f52010-05-25 17:04:15 +0000801 MangleBuffer Name;
Fariborz Jahanian564360b2010-06-24 00:08:06 +0000802 CGM.getMangledName(GD, Name, BD);
Mike Stumpa5448542009-02-13 15:32:32 +0000803 llvm::Function *Fn =
Douglas Gregor6e5d9b02010-05-25 17:12:30 +0000804 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
805 Name.getString(), &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000806
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000807 CGM.SetInternalFunctionAttributes(BD, Fn, FI);
Fariborz Jahanian8404f672010-08-13 00:19:55 +0000808 StartFunction(BD, ResultType, Fn, Args,
809 BExpr->getBody()->getLocEnd());
810
811 CurFuncDecl = OuterFuncDecl;
812
Fariborz Jahanian16ac5ce2010-06-28 18:58:34 +0000813 QualType FnType(BlockFunctionType, 0);
Fariborz Jahanianef160b42010-06-28 19:42:10 +0000814 bool HasPrototype = isa<FunctionProtoType>(BlockFunctionType);
Fariborz Jahanian16ac5ce2010-06-28 18:58:34 +0000815
816 IdentifierInfo *ID = &getContext().Idents.get(Name.getString());
817 CurCodeDecl = FunctionDecl::Create(getContext(),
818 getContext().getTranslationUnitDecl(),
819 SourceLocation(), ID, FnType,
820 0,
John McCalld931b082010-08-26 03:08:43 +0000821 SC_Static,
822 SC_None,
Fariborz Jahanian16ac5ce2010-06-28 18:58:34 +0000823 false, HasPrototype);
Fariborz Jahanian8404f672010-08-13 00:19:55 +0000824 if (FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FnType)) {
825 const FunctionDecl *CFD = dyn_cast<FunctionDecl>(CurCodeDecl);
826 FunctionDecl *FD = const_cast<FunctionDecl *>(CFD);
827 llvm::SmallVector<ParmVarDecl*, 16> Params;
828 for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i)
829 Params.push_back(ParmVarDecl::Create(getContext(), FD,
830 SourceLocation(), 0,
831 FT->getArgType(i), /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +0000832 SC_None, SC_None, 0));
Fariborz Jahanian8404f672010-08-13 00:19:55 +0000833 FD->setParams(Params.data(), Params.size());
834 }
Fariborz Jahanian16ac5ce2010-06-28 18:58:34 +0000835
Fariborz Jahanian8404f672010-08-13 00:19:55 +0000836
John McCallea1471e2010-05-20 01:18:31 +0000837 // If we have a C++ 'this' reference, go ahead and force it into
838 // existence now.
839 if (Info.CXXThisRef) {
840 assert(!BlockCXXThisOffset.isZero() &&
841 "haven't yet allocated 'this' reference");
842
843 // TODO: I have a dream that one day this will be typed.
844 llvm::Value *BlockLiteral = LoadBlockStruct();
845 llvm::Value *ThisPtrRaw =
846 Builder.CreateConstInBoundsGEP1_64(BlockLiteral,
847 BlockCXXThisOffset.getQuantity(),
848 "this.ptr.raw");
849
850 const llvm::Type *Ty =
851 CGM.getTypes().ConvertType(Info.CXXThisRef->getType());
852 Ty = llvm::PointerType::get(Ty, 0);
853 llvm::Value *ThisPtr = Builder.CreateBitCast(ThisPtrRaw, Ty, "this.ptr");
854
855 CXXThisValue = Builder.CreateLoad(ThisPtr, "this");
856 }
857
John McCallee504292010-05-21 04:11:14 +0000858 // If we have an Objective C 'self' reference, go ahead and force it
859 // into existence now.
860 if (Info.NeedsObjCSelf) {
861 ValueDecl *Self = cast<ObjCMethodDecl>(CurFuncDecl)->getSelfDecl();
862 LocalDeclMap[Self] = GetAddrOfBlockDecl(Self, false);
863 }
864
Mike Stumpb289b3f2009-10-01 22:29:41 +0000865 // Save a spot to insert the debug information for all the BlockDeclRefDecls.
866 llvm::BasicBlock *entry = Builder.GetInsertBlock();
867 llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint();
868 --entry_ptr;
869
Chris Lattner161d36d2009-02-28 19:01:03 +0000870 EmitStmt(BExpr->getBody());
Mike Stumpb289b3f2009-10-01 22:29:41 +0000871
Mike Stumpde8c5c72009-10-01 00:27:30 +0000872 // Remember where we were...
873 llvm::BasicBlock *resume = Builder.GetInsertBlock();
Mike Stumpb289b3f2009-10-01 22:29:41 +0000874
Mike Stumpde8c5c72009-10-01 00:27:30 +0000875 // Go back to the entry.
Mike Stumpb289b3f2009-10-01 22:29:41 +0000876 ++entry_ptr;
877 Builder.SetInsertPoint(entry, entry_ptr);
878
Mike Stumpb1a6e682009-09-30 02:43:10 +0000879 if (CGDebugInfo *DI = getDebugInfo()) {
Mike Stumpb1a6e682009-09-30 02:43:10 +0000880 // Emit debug information for all the BlockDeclRefDecls.
John McCallea1471e2010-05-20 01:18:31 +0000881 // FIXME: also for 'this'
882 for (unsigned i = 0, e = BlockLayout.size(); i != e; ++i) {
883 if (const BlockDeclRefExpr *BDRE =
884 dyn_cast<BlockDeclRefExpr>(BlockLayout[i])) {
Mike Stumpb1a6e682009-09-30 02:43:10 +0000885 const ValueDecl *D = BDRE->getDecl();
886 DI->setLocation(D->getLocation());
887 DI->EmitDeclareOfBlockDeclRefVariable(BDRE,
888 LocalDeclMap[getBlockStructDecl()],
889 Builder, this);
890 }
891 }
Mike Stumpb1a6e682009-09-30 02:43:10 +0000892 }
Mike Stumpde8c5c72009-10-01 00:27:30 +0000893 // And resume where we left off.
894 if (resume == 0)
895 Builder.ClearInsertionPoint();
896 else
897 Builder.SetInsertPoint(resume);
Mike Stumpb1a6e682009-09-30 02:43:10 +0000898
Chris Lattner161d36d2009-02-28 19:01:03 +0000899 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlssond5cab542009-02-12 17:55:02 +0000900
Mike Stump8a2b4b12009-02-25 23:33:13 +0000901 // The runtime needs a minimum alignment of a void *.
Ken Dyck30a8a272010-01-26 19:13:33 +0000902 CharUnits MinAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Ken Dyck199c3d62010-01-11 17:06:35 +0000903 BlockOffset = CharUnits::fromQuantity(
Ken Dyck30a8a272010-01-26 19:13:33 +0000904 llvm::RoundUpToAlignment(BlockOffset.getQuantity(),
905 MinAlign.getQuantity()));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000906
John McCallee504292010-05-21 04:11:14 +0000907 Info.BlockSize = BlockOffset;
908 Info.BlockAlign = BlockAlign;
909 Info.BlockLayout = BlockLayout;
Fariborz Jahanian2715b202010-11-15 19:19:38 +0000910 Info.BlockHasCopyDispose = SynthesizeCopyDisposeHelpers;
Anders Carlssond5cab542009-02-12 17:55:02 +0000911 return Fn;
912}
Mike Stumpa99038c2009-02-28 09:07:16 +0000913
John McCallea1471e2010-05-20 01:18:31 +0000914CharUnits BlockFunction::getBlockOffset(CharUnits Size, CharUnits Align) {
915 assert((Align.isPositive()) && "alignment must be 1 byte or more");
Mike Stumpa99038c2009-02-28 09:07:16 +0000916
Ken Dyck199c3d62010-01-11 17:06:35 +0000917 CharUnits OldOffset = BlockOffset;
Mike Stumpa99038c2009-02-28 09:07:16 +0000918
919 // Ensure proper alignment, even if it means we have to have a gap
Ken Dyck199c3d62010-01-11 17:06:35 +0000920 BlockOffset = CharUnits::fromQuantity(
Ken Dyck30a8a272010-01-26 19:13:33 +0000921 llvm::RoundUpToAlignment(BlockOffset.getQuantity(), Align.getQuantity()));
Mike Stumpa99038c2009-02-28 09:07:16 +0000922 BlockAlign = std::max(Align, BlockAlign);
Mike Stump00470a12009-03-05 08:32:30 +0000923
Ken Dyck199c3d62010-01-11 17:06:35 +0000924 CharUnits Pad = BlockOffset - OldOffset;
925 if (Pad.isPositive()) {
Mike Stumpa99038c2009-02-28 09:07:16 +0000926 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
Ken Dyck199c3d62010-01-11 17:06:35 +0000927 llvm::APInt(32,
928 Pad.getQuantity()),
Mike Stumpa99038c2009-02-28 09:07:16 +0000929 ArrayType::Normal, 0);
Douglas Gregorba55ec22010-05-11 18:17:16 +0000930 ValueDecl *PadDecl = VarDecl::Create(getContext(),
931 getContext().getTranslationUnitDecl(),
932 SourceLocation(),
Douglas Gregor16573fa2010-04-19 22:54:31 +0000933 0, QualType(PadTy), 0,
John McCalld931b082010-08-26 03:08:43 +0000934 SC_None, SC_None);
John McCallea1471e2010-05-20 01:18:31 +0000935 Expr *E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
John McCallf89e55a2010-11-18 06:31:45 +0000936 VK_LValue, SourceLocation());
John McCallea1471e2010-05-20 01:18:31 +0000937 BlockLayout.push_back(E);
Mike Stumpa99038c2009-02-28 09:07:16 +0000938 }
Mike Stumpa99038c2009-02-28 09:07:16 +0000939
940 BlockOffset += Size;
John McCallea1471e2010-05-20 01:18:31 +0000941 return BlockOffset - Size;
Mike Stumpa99038c2009-02-28 09:07:16 +0000942}
Mike Stumpdab514f2009-03-04 03:23:46 +0000943
Mike Stump08920992009-03-07 02:35:30 +0000944llvm::Constant *BlockFunction::
Fariborz Jahanian2715b202010-11-15 19:19:38 +0000945GenerateCopyHelperFunction(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000946 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000947 QualType R = getContext().VoidTy;
948
949 FunctionArgList Args;
950 // FIXME: This leaks
Mike Stump08920992009-03-07 02:35:30 +0000951 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +0000952 ImplicitParamDecl::Create(getContext(), 0,
953 SourceLocation(), 0,
Mike Stump08920992009-03-07 02:35:30 +0000954 getContext().getPointerType(getContext().VoidTy));
955 Args.push_back(std::make_pair(Dst, Dst->getType()));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000956 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +0000957 ImplicitParamDecl::Create(getContext(), 0,
958 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000959 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000960 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Mike Stumpa4f668f2009-03-06 01:33:24 +0000962 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +0000963 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000964
Mike Stump3899a7f2009-06-05 23:26:36 +0000965 // FIXME: We'd like to put these into a mergable by content, with
966 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000967 CodeGenTypes &Types = CGM.getTypes();
968 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
969
970 llvm::Function *Fn =
971 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000972 "__copy_helper_block_", &CGM.getModule());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000973
974 IdentifierInfo *II
975 = &CGM.getContext().Idents.get("__copy_helper_block_");
976
977 FunctionDecl *FD = FunctionDecl::Create(getContext(),
978 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000979 SourceLocation(), II, R, 0,
John McCalld931b082010-08-26 03:08:43 +0000980 SC_Static,
981 SC_None,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000982 false,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000983 true);
984 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump08920992009-03-07 02:35:30 +0000985
986 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
987 llvm::Type *PtrPtrT;
Mike Stump08920992009-03-07 02:35:30 +0000988
Mike Stumpb7477cf2009-04-10 18:52:28 +0000989 if (NoteForHelperp) {
990 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump08920992009-03-07 02:35:30 +0000991
Owen Anderson96e0fc72009-07-29 22:16:19 +0000992 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000993 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
994 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump08920992009-03-07 02:35:30 +0000995
Mike Stumpb7477cf2009-04-10 18:52:28 +0000996 llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000997 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000998 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000999 DstObj = Builder.CreateBitCast(DstObj, PtrPtrT);
1000 DstObj = Builder.CreateLoad(DstObj);
Mike Stump08920992009-03-07 02:35:30 +00001001
Mike Stumpb7477cf2009-04-10 18:52:28 +00001002 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
1003 int flag = NoteForHelper[i].flag;
1004 int index = NoteForHelper[i].index;
Mike Stump08920992009-03-07 02:35:30 +00001005
Mike Stumpb7477cf2009-04-10 18:52:28 +00001006 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
1007 || NoteForHelper[i].RequiresCopying) {
1008 llvm::Value *Srcv = SrcObj;
1009 Srcv = Builder.CreateStructGEP(Srcv, index);
Fariborz Jahaniane2204552010-11-16 19:29:39 +00001010 llvm::Value *Dstv;
Fariborz Jahanian830937b2010-12-02 17:02:11 +00001011 if (NoteForHelper[i].cxxvar_import &&
1012 (NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) == 0) {
1013 // Note that cxx objects declared as __block require a
1014 // different API. They are copy constructed in their own copy
1015 // helper functions (see GeneratebyrefCopyHelperFunction).
1016 Srcv = Builder.CreateBitCast(Srcv,
1017 llvm::PointerType::get(PtrToInt8Ty, 0));
1018 Dstv = Builder.CreateStructGEP(DstObj, index);
1019 CGF.EmitSynthesizedCXXCopyCtor(Dstv, Srcv,
1020 NoteForHelper[i].cxxvar_import->getCopyConstructorExpr());
Fariborz Jahanian34999872010-11-13 21:53:34 +00001021 }
1022 else {
Fariborz Jahaniane2204552010-11-16 19:29:39 +00001023 Srcv = Builder.CreateBitCast(Srcv,
1024 llvm::PointerType::get(PtrToInt8Ty, 0));
1025 Dstv = Builder.CreateStructGEP(DstObj, index);
Fariborz Jahanian34999872010-11-13 21:53:34 +00001026 Srcv = Builder.CreateLoad(Srcv);
1027 Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
1028 llvm::Value *N = llvm::ConstantInt::get(CGF.Int32Ty, flag);
1029 llvm::Value *F = CGM.getBlockObjectAssign();
1030 Builder.CreateCall3(F, Dstv, Srcv, N);
1031 }
Mike Stumpb7477cf2009-04-10 18:52:28 +00001032 }
Mike Stump08920992009-03-07 02:35:30 +00001033 }
1034 }
1035
Mike Stumpa4f668f2009-03-06 01:33:24 +00001036 CGF.FinishFunction();
1037
Owen Anderson3c4972d2009-07-29 18:54:39 +00001038 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +00001039}
1040
Mike Stumpcf62d392009-03-06 18:42:23 +00001041llvm::Constant *BlockFunction::
Fariborz Jahanian2715b202010-11-15 19:19:38 +00001042GenerateDestroyHelperFunction(const llvm::StructType* T,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001043 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +00001044 QualType R = getContext().VoidTy;
1045
1046 FunctionArgList Args;
1047 // FIXME: This leaks
1048 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001049 ImplicitParamDecl::Create(getContext(), 0,
1050 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +00001051 getContext().getPointerType(getContext().VoidTy));
1052
1053 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Mike Stumpa4f668f2009-03-06 01:33:24 +00001055 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +00001056 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stumpa4f668f2009-03-06 01:33:24 +00001057
Mike Stump3899a7f2009-06-05 23:26:36 +00001058 // FIXME: We'd like to put these into a mergable by content, with
1059 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +00001060 CodeGenTypes &Types = CGM.getTypes();
1061 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1062
1063 llvm::Function *Fn =
1064 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001065 "__destroy_helper_block_", &CGM.getModule());
Mike Stumpa4f668f2009-03-06 01:33:24 +00001066
1067 IdentifierInfo *II
1068 = &CGM.getContext().Idents.get("__destroy_helper_block_");
1069
1070 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1071 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001072 SourceLocation(), II, R, 0,
John McCalld931b082010-08-26 03:08:43 +00001073 SC_Static,
1074 SC_None,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001075 false, true);
Mike Stumpa4f668f2009-03-06 01:33:24 +00001076 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1edf6b62009-03-07 02:53:18 +00001077
Mike Stumpb7477cf2009-04-10 18:52:28 +00001078 if (NoteForHelperp) {
1079 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump1edf6b62009-03-07 02:53:18 +00001080
Mike Stumpb7477cf2009-04-10 18:52:28 +00001081 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
1082 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +00001083 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +00001084 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
1085 SrcObj = Builder.CreateLoad(SrcObj);
Fariborz Jahanian34999872010-11-13 21:53:34 +00001086 EHScopeStack::stable_iterator CleanupDepth = CGF.EHStack.stable_begin();
Mike Stumpb7477cf2009-04-10 18:52:28 +00001087 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
1088 int flag = NoteForHelper[i].flag;
1089 int index = NoteForHelper[i].index;
Mike Stump1edf6b62009-03-07 02:53:18 +00001090
Mike Stumpb7477cf2009-04-10 18:52:28 +00001091 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
1092 || NoteForHelper[i].RequiresCopying) {
1093 llvm::Value *Srcv = SrcObj;
1094 Srcv = Builder.CreateStructGEP(Srcv, index);
1095 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001096 llvm::PointerType::get(PtrToInt8Ty, 0));
Fariborz Jahanian830937b2010-12-02 17:02:11 +00001097 if (NoteForHelper[i].cxxvar_import &&
1098 (NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) == 0) {
Fariborz Jahanian34999872010-11-13 21:53:34 +00001099 const BlockDeclRefExpr *BDRE = NoteForHelper[i].cxxvar_import;
1100 const Expr *E = BDRE->getCopyConstructorExpr();
1101 QualType ClassTy = E->getType();
1102 QualType PtrClassTy = getContext().getPointerType(ClassTy);
1103 const llvm::Type *t = CGM.getTypes().ConvertType(PtrClassTy);
1104 Srcv = Builder.CreateBitCast(Srcv, t);
1105 CGF.PushDestructorCleanup(ClassTy, Srcv);
1106 }
1107 else {
1108 Srcv = Builder.CreateLoad(Srcv);
1109 BuildBlockRelease(Srcv, flag);
1110 }
Mike Stumpb7477cf2009-04-10 18:52:28 +00001111 }
Mike Stump1edf6b62009-03-07 02:53:18 +00001112 }
Fariborz Jahanian34999872010-11-13 21:53:34 +00001113 CGF.PopCleanupBlocks(CleanupDepth);
Mike Stump1edf6b62009-03-07 02:53:18 +00001114 }
1115
Mike Stumpa4f668f2009-03-06 01:33:24 +00001116 CGF.FinishFunction();
1117
Owen Anderson3c4972d2009-07-29 18:54:39 +00001118 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpa4f668f2009-03-06 01:33:24 +00001119}
1120
Mike Stump08920992009-03-07 02:35:30 +00001121llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001122 std::vector<HelperInfo> *NoteForHelper) {
Fariborz Jahanian2715b202010-11-15 19:19:38 +00001123 return CodeGenFunction(CGM).GenerateCopyHelperFunction(T, NoteForHelper);
Mike Stumpa4f668f2009-03-06 01:33:24 +00001124}
1125
Mike Stump08920992009-03-07 02:35:30 +00001126llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001127 std::vector<HelperInfo> *NoteForHelperp) {
Fariborz Jahanian2715b202010-11-15 19:19:38 +00001128 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(T, NoteForHelperp);
Mike Stumpdab514f2009-03-04 03:23:46 +00001129}
Mike Stump797b6322009-03-05 01:23:13 +00001130
Mike Stumpee094222009-03-06 06:12:24 +00001131llvm::Constant *BlockFunction::
Fariborz Jahanian830937b2010-12-02 17:02:11 +00001132GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag,
1133 const VarDecl *BD) {
Mike Stump45031c02009-03-06 02:29:21 +00001134 QualType R = getContext().VoidTy;
1135
1136 FunctionArgList Args;
1137 // FIXME: This leaks
Mike Stumpee094222009-03-06 06:12:24 +00001138 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +00001139 ImplicitParamDecl::Create(getContext(), 0,
1140 SourceLocation(), 0,
Mike Stumpee094222009-03-06 06:12:24 +00001141 getContext().getPointerType(getContext().VoidTy));
1142 Args.push_back(std::make_pair(Dst, Dst->getType()));
1143
1144 // FIXME: This leaks
Mike Stump45031c02009-03-06 02:29:21 +00001145 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001146 ImplicitParamDecl::Create(getContext(), 0,
1147 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001148 getContext().getPointerType(getContext().VoidTy));
Mike Stump45031c02009-03-06 02:29:21 +00001149 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Mike Stump45031c02009-03-06 02:29:21 +00001151 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +00001152 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stump45031c02009-03-06 02:29:21 +00001153
Mike Stump45031c02009-03-06 02:29:21 +00001154 CodeGenTypes &Types = CGM.getTypes();
1155 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1156
Mike Stump3899a7f2009-06-05 23:26:36 +00001157 // FIXME: We'd like to put these into a mergable by content, with
1158 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001159 llvm::Function *Fn =
1160 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian830937b2010-12-02 17:02:11 +00001161 "__Block_byref_object_copy_", &CGM.getModule());
Mike Stump45031c02009-03-06 02:29:21 +00001162
1163 IdentifierInfo *II
Fariborz Jahanian830937b2010-12-02 17:02:11 +00001164 = &CGM.getContext().Idents.get("__Block_byref_object_copy_");
Mike Stump45031c02009-03-06 02:29:21 +00001165
1166 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1167 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001168 SourceLocation(), II, R, 0,
John McCalld931b082010-08-26 03:08:43 +00001169 SC_Static,
1170 SC_None,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001171 false, true);
Mike Stump45031c02009-03-06 02:29:21 +00001172 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpee094222009-03-06 06:12:24 +00001173
1174 // dst->x
1175 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001176 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001177 V = Builder.CreateLoad(V);
Mike Stumpee094222009-03-06 06:12:24 +00001178 V = Builder.CreateStructGEP(V, 6, "x");
Fariborz Jahanian830937b2010-12-02 17:02:11 +00001179 llvm::Value *DstObj = V;
Mike Stumpee094222009-03-06 06:12:24 +00001180
1181 // src->x
1182 V = CGF.GetAddrOfLocalVar(Src);
1183 V = Builder.CreateLoad(V);
1184 V = Builder.CreateBitCast(V, T);
1185 V = Builder.CreateStructGEP(V, 6, "x");
Fariborz Jahanian830937b2010-12-02 17:02:11 +00001186
1187 if (flag & BLOCK_HAS_CXX_OBJ) {
1188 assert (BD && "VarDecl is null - GeneratebyrefCopyHelperFunction");
1189 llvm::Value *SrcObj = V;
1190 CGF.EmitSynthesizedCXXCopyCtor(DstObj, SrcObj,
1191 getContext().getBlockVarCopyInits(BD));
1192 }
1193 else {
1194 DstObj = Builder.CreateBitCast(DstObj, PtrToInt8Ty);
1195 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
1196 llvm::Value *SrcObj = Builder.CreateLoad(V);
1197 flag |= BLOCK_BYREF_CALLER;
1198 llvm::Value *N = llvm::ConstantInt::get(CGF.Int32Ty, flag);
1199 llvm::Value *F = CGM.getBlockObjectAssign();
1200 Builder.CreateCall3(F, DstObj, SrcObj, N);
1201 }
1202
Mike Stump45031c02009-03-06 02:29:21 +00001203 CGF.FinishFunction();
1204
Owen Anderson3c4972d2009-07-29 18:54:39 +00001205 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001206}
1207
Mike Stump1851b682009-03-06 04:53:30 +00001208llvm::Constant *
1209BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
Fariborz Jahanian830937b2010-12-02 17:02:11 +00001210 int flag,
1211 const VarDecl *BD) {
Mike Stump45031c02009-03-06 02:29:21 +00001212 QualType R = getContext().VoidTy;
1213
1214 FunctionArgList Args;
1215 // FIXME: This leaks
1216 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001217 ImplicitParamDecl::Create(getContext(), 0,
1218 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001219 getContext().getPointerType(getContext().VoidTy));
1220
1221 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001222
Mike Stump45031c02009-03-06 02:29:21 +00001223 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +00001224 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stump45031c02009-03-06 02:29:21 +00001225
Mike Stump45031c02009-03-06 02:29:21 +00001226 CodeGenTypes &Types = CGM.getTypes();
1227 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1228
Mike Stump3899a7f2009-06-05 23:26:36 +00001229 // FIXME: We'd like to put these into a mergable by content, with
1230 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001231 llvm::Function *Fn =
1232 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian830937b2010-12-02 17:02:11 +00001233 "__Block_byref_object_dispose_",
Mike Stump45031c02009-03-06 02:29:21 +00001234 &CGM.getModule());
1235
1236 IdentifierInfo *II
Fariborz Jahanian830937b2010-12-02 17:02:11 +00001237 = &CGM.getContext().Idents.get("__Block_byref_object_dispose_");
Mike Stump45031c02009-03-06 02:29:21 +00001238
1239 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1240 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001241 SourceLocation(), II, R, 0,
John McCalld931b082010-08-26 03:08:43 +00001242 SC_Static,
1243 SC_None,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001244 false, true);
Mike Stump45031c02009-03-06 02:29:21 +00001245 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1851b682009-03-06 04:53:30 +00001246
1247 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001248 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001249 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001250 V = Builder.CreateStructGEP(V, 6, "x");
Fariborz Jahanian830937b2010-12-02 17:02:11 +00001251 if (flag & BLOCK_HAS_CXX_OBJ) {
1252 EHScopeStack::stable_iterator CleanupDepth = CGF.EHStack.stable_begin();
1253 assert (BD && "VarDecl is null - GeneratebyrefDestroyHelperFunction");
1254 QualType ClassTy = BD->getType();
1255 CGF.PushDestructorCleanup(ClassTy, V);
1256 CGF.PopCleanupBlocks(CleanupDepth);
1257 }
1258 else {
1259 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
1260 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001261
Fariborz Jahanian830937b2010-12-02 17:02:11 +00001262 flag |= BLOCK_BYREF_CALLER;
1263 BuildBlockRelease(V, flag);
1264 }
Mike Stump45031c02009-03-06 02:29:21 +00001265 CGF.FinishFunction();
1266
Owen Anderson3c4972d2009-07-29 18:54:39 +00001267 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001268}
1269
Mike Stumpee094222009-03-06 06:12:24 +00001270llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
Fariborz Jahanian830937b2010-12-02 17:02:11 +00001271 int Flag, unsigned Align,
1272 const VarDecl *BD) {
Chris Lattner10976d92009-12-05 08:21:30 +00001273 // All alignments below that of pointer alignment collapse down to just
Mike Stump3899a7f2009-06-05 23:26:36 +00001274 // pointer alignment, as we always have at least that much alignment to begin
1275 // with.
1276 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001277
Mike Stump3899a7f2009-06-05 23:26:36 +00001278 // As an optimization, we only generate a single function of each kind we
1279 // might need. We need a different one for each alignment and for each
1280 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001281 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1282 llvm::Constant *&Entry = CGM.AssignCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001283 if (Entry)
1284 return Entry;
Fariborz Jahanian830937b2010-12-02 17:02:11 +00001285 return Entry =
1286 CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, Flag, BD);
Mike Stump45031c02009-03-06 02:29:21 +00001287}
1288
Mike Stump1851b682009-03-06 04:53:30 +00001289llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001290 int Flag,
Fariborz Jahanian830937b2010-12-02 17:02:11 +00001291 unsigned Align,
1292 const VarDecl *BD) {
Mike Stump3899a7f2009-06-05 23:26:36 +00001293 // 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;
Fariborz Jahanian830937b2010-12-02 17:02:11 +00001305 return Entry =
1306 CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, Flag, BD);
Mike Stump45031c02009-03-06 02:29:21 +00001307}
1308
Mike Stump1851b682009-03-06 04:53:30 +00001309void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Daniel Dunbar673431a2010-07-16 00:00:15 +00001310 llvm::Value *F = CGM.getBlockObjectDispose();
Mike Stump1851b682009-03-06 04:53:30 +00001311 llvm::Value *N;
Mike Stump797b6322009-03-05 01:23:13 +00001312 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Chris Lattner77b89b82010-06-27 07:15:29 +00001313 N = llvm::ConstantInt::get(CGF.Int32Ty, flag);
Mike Stump797b6322009-03-05 01:23:13 +00001314 Builder.CreateCall2(F, V, N);
1315}
Mike Stump00470a12009-03-05 08:32:30 +00001316
1317ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stump08920992009-03-07 02:35:30 +00001318
1319BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1320 CGBuilderTy &B)
Chris Lattner77b89b82010-06-27 07:15:29 +00001321 : CGM(cgm), VMContext(cgm.getLLVMContext()), CGF(cgf), Builder(B) {
Owen Anderson0032b272009-08-13 21:57:51 +00001322 PtrToInt8Ty = llvm::PointerType::getUnqual(
1323 llvm::Type::getInt8Ty(VMContext));
Mike Stump08920992009-03-07 02:35:30 +00001324
Fariborz Jahanian2715b202010-11-15 19:19:38 +00001325 SynthesizeCopyDisposeHelpers = false;
Mike Stump08920992009-03-07 02:35:30 +00001326}