blob: a6929649efa1a1ae6541fca871d840be0bf02bb9 [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 bool BlockHasCopyDispose = Info.BlockHasCopyDispose;
43 CharUnits Size = Info.BlockSize;
Mike Stump56129b12009-02-13 16:55:51 +000044 const llvm::Type *UnsignedLongTy
45 = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpe5fee252009-02-13 16:19:19 +000046 llvm::Constant *C;
47 std::vector<llvm::Constant*> Elts;
48
49 // reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +000050 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpe5fee252009-02-13 16:19:19 +000051 Elts.push_back(C);
52
53 // Size
Mike Stumpd6840002009-02-21 20:07:44 +000054 // FIXME: What is the right way to say this doesn't fit? We should give
55 // a user diagnostic in that case. Better fix would be to change the
56 // API to size_t.
Ken Dyck199c3d62010-01-11 17:06:35 +000057 C = llvm::ConstantInt::get(UnsignedLongTy, Size.getQuantity());
Mike Stumpe5fee252009-02-13 16:19:19 +000058 Elts.push_back(C);
59
Blaine Garst2a7eb282010-02-23 21:51:17 +000060 // optional copy/dispose helpers
Mike Stumpe5fee252009-02-13 16:19:19 +000061 if (BlockHasCopyDispose) {
62 // copy_func_helper_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000063 Elts.push_back(BuildCopyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000064
65 // destroy_func_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000066 Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000067 }
68
Blaine Garst2a7eb282010-02-23 21:51:17 +000069 // Signature. non-optional ObjC-style method descriptor @encode sequence
70 std::string BlockTypeEncoding;
71 CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
72
73 Elts.push_back(llvm::ConstantExpr::getBitCast(
74 CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty));
75
76 // Layout.
Fariborz Jahanian44034db2010-08-04 18:44:59 +000077 C = BlockVarLayout;
78
Blaine Garst2a7eb282010-02-23 21:51:17 +000079 Elts.push_back(C);
80
Nick Lewycky0d36dd22009-09-19 20:00:52 +000081 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stumpe5fee252009-02-13 16:19:19 +000082
Owen Anderson1c431b32009-07-08 19:05:04 +000083 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Mike Stumpe5fee252009-02-13 16:19:19 +000084 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +000085 C, "__block_descriptor_tmp");
Mike Stumpe5fee252009-02-13 16:19:19 +000086 return C;
87}
88
John McCallee504292010-05-21 04:11:14 +000089static void CollectBlockDeclRefInfo(const Stmt *S, CGBlockInfo &Info) {
Anders Carlsson4de9fce2009-03-01 01:09:12 +000090 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
91 I != E; ++I)
Daniel Dunbar82573ee2009-03-02 07:00:57 +000092 if (*I)
John McCallee504292010-05-21 04:11:14 +000093 CollectBlockDeclRefInfo(*I, Info);
Mike Stump00470a12009-03-05 08:32:30 +000094
Mike Stumpea26cb52009-10-21 03:49:08 +000095 // We want to ensure we walk down into block literals so we can find
96 // all nested BlockDeclRefExprs.
Mike Stump38e16272009-10-21 22:01:24 +000097 if (const BlockExpr *BE = dyn_cast<BlockExpr>(S)) {
John McCallee504292010-05-21 04:11:14 +000098 Info.InnerBlocks.insert(BE->getBlockDecl());
99 CollectBlockDeclRefInfo(BE->getBody(), Info);
Mike Stump38e16272009-10-21 22:01:24 +0000100 }
Mike Stumpea26cb52009-10-21 03:49:08 +0000101
John McCallee504292010-05-21 04:11:14 +0000102 else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) {
103 const ValueDecl *D = BDRE->getDecl();
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000104 // FIXME: Handle enums.
John McCallee504292010-05-21 04:11:14 +0000105 if (isa<FunctionDecl>(D))
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000106 return;
Mike Stump00470a12009-03-05 08:32:30 +0000107
John McCallee504292010-05-21 04:11:14 +0000108 if (isa<ImplicitParamDecl>(D) &&
109 isa<ObjCMethodDecl>(D->getDeclContext()) &&
110 cast<ObjCMethodDecl>(D->getDeclContext())->getSelfDecl() == D) {
111 Info.NeedsObjCSelf = true;
112 return;
113 }
114
Mike Stump38e16272009-10-21 22:01:24 +0000115 // Only Decls that escape are added.
Fariborz Jahanian34999872010-11-13 21:53:34 +0000116 if (!Info.InnerBlocks.count(D->getDeclContext())) {
117 if (BDRE->getCopyConstructorExpr())
118 Info.HasCXXObject = true;
Mike Stump38e16272009-10-21 22:01:24 +0000119 Info.DeclRefs.push_back(BDRE);
Fariborz Jahanian34999872010-11-13 21:53:34 +0000120 }
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000121 }
John McCallea1471e2010-05-20 01:18:31 +0000122
John McCallee504292010-05-21 04:11:14 +0000123 // Make sure to capture implicit 'self' references due to super calls.
Chandler Carruthf54b80f2010-05-21 10:29:28 +0000124 else if (const ObjCMessageExpr *E = dyn_cast<ObjCMessageExpr>(S)) {
John McCallee504292010-05-21 04:11:14 +0000125 if (E->getReceiverKind() == ObjCMessageExpr::SuperClass ||
126 E->getReceiverKind() == ObjCMessageExpr::SuperInstance)
127 Info.NeedsObjCSelf = true;
Chandler Carruthf54b80f2010-05-21 10:29:28 +0000128 }
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000129 else if (const ObjCPropertyRefExpr *PE = dyn_cast<ObjCPropertyRefExpr>(S)) {
130 // Getter/setter uses may also cause implicit super references,
131 // which we can check for with:
132 if (PE->isSuperReceiver())
133 Info.NeedsObjCSelf = true;
134 }
135 else if (const ObjCImplicitSetterGetterRefExpr *IE =
136 dyn_cast<ObjCImplicitSetterGetterRefExpr>(S)) {
137 // Getter/setter uses may also cause implicit super references,
138 // which we can check for with:
139 if (IE->isSuperReceiver())
140 Info.NeedsObjCSelf = true;
141 }
John McCallee504292010-05-21 04:11:14 +0000142 else if (isa<CXXThisExpr>(S))
John McCallea1471e2010-05-20 01:18:31 +0000143 Info.CXXThisRef = cast<CXXThisExpr>(S);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000144}
145
John McCallee504292010-05-21 04:11:14 +0000146/// CanBlockBeGlobal - Given a CGBlockInfo struct, determines if a block can be
Mike Stump58a85142009-03-04 22:48:06 +0000147/// declared as a global variable instead of on the stack.
John McCallee504292010-05-21 04:11:14 +0000148static bool CanBlockBeGlobal(const CGBlockInfo &Info) {
Mike Stumpea26cb52009-10-21 03:49:08 +0000149 return Info.DeclRefs.empty();
150}
151
152/// AllocateAllBlockDeclRefs - Preallocate all nested BlockDeclRefExprs to
153/// ensure we can generate the debug information for the parameter for the block
154/// invoke function.
John McCallee504292010-05-21 04:11:14 +0000155static void AllocateAllBlockDeclRefs(CodeGenFunction &CGF, CGBlockInfo &Info) {
John McCallea1471e2010-05-20 01:18:31 +0000156 if (Info.CXXThisRef)
John McCallee504292010-05-21 04:11:14 +0000157 CGF.AllocateBlockCXXThisPointer(Info.CXXThisRef);
Mike Stumpea26cb52009-10-21 03:49:08 +0000158
159 for (size_t i = 0; i < Info.DeclRefs.size(); ++i)
John McCallee504292010-05-21 04:11:14 +0000160 CGF.AllocateBlockDecl(Info.DeclRefs[i]);
161
162 if (Info.NeedsObjCSelf) {
163 ValueDecl *Self = cast<ObjCMethodDecl>(CGF.CurFuncDecl)->getSelfDecl();
164 BlockDeclRefExpr *BDRE =
165 new (CGF.getContext()) BlockDeclRefExpr(Self, Self->getType(),
166 SourceLocation(), false);
167 Info.DeclRefs.push_back(BDRE);
168 CGF.AllocateBlockDecl(BDRE);
169 }
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000170}
171
Fariborz Jahanian7edddb82010-07-28 19:07:18 +0000172static unsigned computeBlockFlag(CodeGenModule &CGM,
173 const BlockExpr *BE, unsigned flags) {
174 QualType BPT = BE->getType();
175 const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>();
176 QualType ResultType = ftype->getResultType();
177
178 CallArgList Args;
179 CodeGenTypes &Types = CGM.getTypes();
180 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, Args,
181 FunctionType::ExtInfo());
182 if (CGM.ReturnTypeUsesSRet(FnInfo))
183 flags |= CodeGenFunction::BLOCK_USE_STRET;
184 return flags;
185}
186
Mike Stump58a85142009-03-04 22:48:06 +0000187// FIXME: Push most into CGM, passing down a few bits, like current function
188// name.
Mike Stump8a2b4b12009-02-25 23:33:13 +0000189llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000190 std::string Name = CurFn->getName();
John McCallee504292010-05-21 04:11:14 +0000191 CGBlockInfo Info(Name.c_str());
192 Info.InnerBlocks.insert(BE->getBlockDecl());
193 CollectBlockDeclRefInfo(BE->getBody(), Info);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000194
195 // Check if the block can be global.
Mike Stump58a85142009-03-04 22:48:06 +0000196 // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like
197 // to just have one code path. We should move this function into CGM and pass
198 // CGF, then we can just check to see if CGF is 0.
Mike Stumpdab514f2009-03-04 03:23:46 +0000199 if (0 && CanBlockBeGlobal(Info))
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000200 return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
Mike Stump00470a12009-03-05 08:32:30 +0000201
David Chisnall5e530af2009-11-17 19:33:30 +0000202 size_t BlockFields = 5;
203
David Chisnall5e530af2009-11-17 19:33:30 +0000204 std::vector<llvm::Constant*> Elts(BlockFields);
205
Mike Stumpe5fee252009-02-13 16:19:19 +0000206 llvm::Constant *C;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000207 llvm::Value *V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000208
Mike Stumpe5fee252009-02-13 16:19:19 +0000209 {
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000210 llvm::Constant *BlockVarLayout;
Mike Stumpe5fee252009-02-13 16:19:19 +0000211 // C = BuildBlockStructInitlist();
Blaine Garsta36e2232010-03-05 01:29:59 +0000212 unsigned int flags = BLOCK_HAS_SIGNATURE;
David Chisnall5e530af2009-11-17 19:33:30 +0000213
Mike Stump00470a12009-03-05 08:32:30 +0000214 // We run this first so that we set BlockHasCopyDispose from the entire
215 // block literal.
216 // __invoke
Mike Stump00470a12009-03-05 08:32:30 +0000217 llvm::Function *Fn
Fariborz Jahanian564360b2010-06-24 00:08:06 +0000218 = CodeGenFunction(CGM).GenerateBlockFunction(CurGD, BE, Info, CurFuncDecl,
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000219 BlockVarLayout,
John McCallee504292010-05-21 04:11:14 +0000220 LocalDeclMap);
221 BlockHasCopyDispose |= Info.BlockHasCopyDispose;
Mike Stump00470a12009-03-05 08:32:30 +0000222 Elts[3] = Fn;
223
Mike Stumpf5408fe2009-05-16 07:57:57 +0000224 // FIXME: Don't use BlockHasCopyDispose, it is set more often then
225 // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); }
John McCallee504292010-05-21 04:11:14 +0000226 if (Info.BlockHasCopyDispose)
Mike Stumpe5fee252009-02-13 16:19:19 +0000227 flags |= BLOCK_HAS_COPY_DISPOSE;
Fariborz Jahanian34999872010-11-13 21:53:34 +0000228 // This block may import a c++ object.
229 if (Info.HasCXXObject)
230 flags |= BLOCK_HAS_CXX_OBJ;
Mike Stumpe5fee252009-02-13 16:19:19 +0000231
Mike Stump7d6dc4f2009-02-13 20:17:16 +0000232 // __isa
Mike Stump59c5b112009-02-13 19:29:27 +0000233 C = CGM.getNSConcreteStackBlock();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000234 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
Mike Stump00470a12009-03-05 08:32:30 +0000235 Elts[0] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000236
237 // __flags
Fariborz Jahanian7edddb82010-07-28 19:07:18 +0000238 flags = computeBlockFlag(CGM, BE, flags);
Mike Stumpe5fee252009-02-13 16:19:19 +0000239 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
240 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000241 C = llvm::ConstantInt::get(IntTy, flags);
Mike Stump00470a12009-03-05 08:32:30 +0000242 Elts[1] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000243
244 // __reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000245 C = llvm::ConstantInt::get(IntTy, 0);
Mike Stump00470a12009-03-05 08:32:30 +0000246 Elts[2] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000247
John McCallee504292010-05-21 04:11:14 +0000248 if (Info.BlockLayout.empty()) {
Mike Stumpcf62d392009-03-06 18:42:23 +0000249 // __descriptor
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000250 C = llvm::Constant::getNullValue(PtrToInt8Ty);
251 Elts[4] = BuildDescriptorBlockDecl(BE, Info, 0, C, 0);
Mike Stumpcf62d392009-03-06 18:42:23 +0000252
Mike Stump5570cfe2009-03-01 20:07:53 +0000253 // Optimize to being a global block.
254 Elts[0] = CGM.getNSConcreteGlobalBlock();
Blaine Garsta36e2232010-03-05 01:29:59 +0000255
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000256 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
Mike Stump5570cfe2009-03-01 20:07:53 +0000257
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000258 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000259
Owen Anderson1c431b32009-07-08 19:05:04 +0000260 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000261 llvm::GlobalValue::InternalLinkage, C,
262 "__block_holder_tmp_" +
263 llvm::Twine(CGM.getGlobalUniqueCount()));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000264 QualType BPT = BE->getType();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000265 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000266 return C;
267 }
Mike Stump00470a12009-03-05 08:32:30 +0000268
John McCallee504292010-05-21 04:11:14 +0000269 std::vector<const llvm::Type *> Types(BlockFields+Info.BlockLayout.size());
Mike Stump08920992009-03-07 02:35:30 +0000270 for (int i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000271 Types[i] = Elts[i]->getType();
Mike Stump08920992009-03-07 02:35:30 +0000272 Types[4] = PtrToInt8Ty;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000273
John McCallee504292010-05-21 04:11:14 +0000274 for (unsigned i = 0, n = Info.BlockLayout.size(); i != n; ++i) {
275 const Expr *E = Info.BlockLayout[i];
Mike Stumpa99038c2009-02-28 09:07:16 +0000276 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
277 QualType Ty = E->getType();
Mike Stumpdab514f2009-03-04 03:23:46 +0000278 if (BDRE && BDRE->isByRef()) {
Fariborz Jahanian79651722010-06-02 21:35:17 +0000279 Types[i+BlockFields] =
280 llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0);
281 } else if (BDRE && BDRE->getDecl()->getType()->isReferenceType()) {
282 Types[i+BlockFields] = llvm::PointerType::get(ConvertType(Ty), 0);
283 } else
David Chisnall5e530af2009-11-17 19:33:30 +0000284 Types[i+BlockFields] = ConvertType(Ty);
Mike Stumpa99038c2009-02-28 09:07:16 +0000285 }
Mike Stump8a2b4b12009-02-25 23:33:13 +0000286
Owen Anderson47a434f2009-08-05 23:18:46 +0000287 llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000288
289 llvm::AllocaInst *A = CreateTempAlloca(Ty);
John McCallee504292010-05-21 04:11:14 +0000290 A->setAlignment(Info.BlockAlign.getQuantity());
Mike Stump8a2b4b12009-02-25 23:33:13 +0000291 V = A;
292
John McCallea1471e2010-05-20 01:18:31 +0000293 // Build layout / cleanup information for all the data entries in the
294 // layout, and write the enclosing fields into the type.
John McCallee504292010-05-21 04:11:14 +0000295 std::vector<HelperInfo> NoteForHelper(Info.BlockLayout.size());
John McCallea1471e2010-05-20 01:18:31 +0000296 unsigned NumHelpers = 0;
Mike Stump08920992009-03-07 02:35:30 +0000297
298 for (unsigned i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000299 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
Mike Stump00470a12009-03-05 08:32:30 +0000300
John McCallee504292010-05-21 04:11:14 +0000301 for (unsigned i=0; i < Info.BlockLayout.size(); ++i) {
302 const Expr *E = Info.BlockLayout[i];
Mike Stump8a2b4b12009-02-25 23:33:13 +0000303
John McCallea1471e2010-05-20 01:18:31 +0000304 // Skip padding.
305 if (isa<DeclRefExpr>(E)) continue;
Mike Stumpa99038c2009-02-28 09:07:16 +0000306
John McCallea1471e2010-05-20 01:18:31 +0000307 llvm::Value* Addr = Builder.CreateStructGEP(V, i+BlockFields, "tmp");
308 HelperInfo &Note = NoteForHelper[NumHelpers++];
Mike Stumpa99038c2009-02-28 09:07:16 +0000309
John McCallea1471e2010-05-20 01:18:31 +0000310 Note.index = i+5;
Mike Stump08920992009-03-07 02:35:30 +0000311
John McCallea1471e2010-05-20 01:18:31 +0000312 if (isa<CXXThisExpr>(E)) {
313 Note.RequiresCopying = false;
314 Note.flag = BLOCK_FIELD_IS_OBJECT;
Mike Stumpa99038c2009-02-28 09:07:16 +0000315
John McCallea1471e2010-05-20 01:18:31 +0000316 Builder.CreateStore(LoadCXXThis(), Addr);
317 continue;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000318 }
John McCallea1471e2010-05-20 01:18:31 +0000319
320 const BlockDeclRefExpr *BDRE = cast<BlockDeclRefExpr>(E);
Fariborz Jahaniane27e9d62010-11-11 00:11:38 +0000321 Note.RequiresCopying = BlockRequiresCopying(BDRE);
Fariborz Jahanian34999872010-11-13 21:53:34 +0000322 Note.cxxvar_import =
323 BDRE->getCopyConstructorExpr() ? BDRE : 0;
John McCallea1471e2010-05-20 01:18:31 +0000324 const ValueDecl *VD = BDRE->getDecl();
325 QualType T = VD->getType();
Fariborz Jahanian34999872010-11-13 21:53:34 +0000326
John McCallea1471e2010-05-20 01:18:31 +0000327 if (BDRE->isByRef()) {
328 Note.flag = BLOCK_FIELD_IS_BYREF;
329 if (T.isObjCGCWeak())
330 Note.flag |= BLOCK_FIELD_IS_WEAK;
331 } else if (T->isBlockPointerType()) {
332 Note.flag = BLOCK_FIELD_IS_BLOCK;
333 } else {
334 Note.flag = BLOCK_FIELD_IS_OBJECT;
335 }
336
337 if (LocalDeclMap[VD]) {
338 if (BDRE->isByRef()) {
339 llvm::Value *Loc = LocalDeclMap[VD];
340 Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
341 Loc = Builder.CreateLoad(Loc);
342 Builder.CreateStore(Loc, Addr);
343 continue;
344 } else {
Fariborz Jahanianac7362d2010-06-08 20:57:22 +0000345 if (BDRE->getCopyConstructorExpr()) {
346 E = BDRE->getCopyConstructorExpr();
John McCallf1549f62010-07-06 01:34:17 +0000347 PushDestructorCleanup(E->getType(), Addr);
Fariborz Jahanianac7362d2010-06-08 20:57:22 +0000348 }
Fariborz Jahanian59da45a2010-06-04 21:35:44 +0000349 else {
350 E = new (getContext()) DeclRefExpr(const_cast<ValueDecl*>(VD),
Fariborz Jahanian79651722010-06-02 21:35:17 +0000351 VD->getType().getNonReferenceType(),
352 SourceLocation());
Fariborz Jahanian59da45a2010-06-04 21:35:44 +0000353 if (VD->getType()->isReferenceType()) {
354 E = new (getContext())
John McCall2de56d12010-08-25 11:45:40 +0000355 UnaryOperator(const_cast<Expr*>(E), UO_AddrOf,
Fariborz Jahanian59da45a2010-06-04 21:35:44 +0000356 getContext().getPointerType(E->getType()),
357 SourceLocation());
358 }
Fariborz Jahaniandf8b8ea2010-06-04 16:10:00 +0000359 }
360 }
John McCallea1471e2010-05-20 01:18:31 +0000361 }
John McCallea1471e2010-05-20 01:18:31 +0000362
363 if (BDRE->isByRef()) {
364 E = new (getContext())
John McCall2de56d12010-08-25 11:45:40 +0000365 UnaryOperator(const_cast<Expr*>(E), UO_AddrOf,
John McCallea1471e2010-05-20 01:18:31 +0000366 getContext().getPointerType(E->getType()),
367 SourceLocation());
368 }
369
John McCall558d2ab2010-09-15 10:14:12 +0000370 RValue r = EmitAnyExpr(E, AggValueSlot::forAddr(Addr, false, true));
John McCallea1471e2010-05-20 01:18:31 +0000371 if (r.isScalar()) {
372 llvm::Value *Loc = r.getScalarVal();
373 const llvm::Type *Ty = Types[i+BlockFields];
374 if (BDRE->isByRef()) {
375 // E is now the address of the value field, instead, we want the
376 // address of the actual ByRef struct. We optimize this slightly
377 // compared to gcc by not grabbing the forwarding slot as this must
378 // be done during Block_copy for us, and we can postpone the work
379 // until then.
380 CharUnits offset = BlockDecls[BDRE->getDecl()];
381
382 llvm::Value *BlockLiteral = LoadBlockStruct();
383
384 Loc = Builder.CreateGEP(BlockLiteral,
Chris Lattner77b89b82010-06-27 07:15:29 +0000385 llvm::ConstantInt::get(Int64Ty, offset.getQuantity()),
John McCallea1471e2010-05-20 01:18:31 +0000386 "block.literal");
387 Ty = llvm::PointerType::get(Ty, 0);
388 Loc = Builder.CreateBitCast(Loc, Ty);
389 Loc = Builder.CreateLoad(Loc);
390 // Loc = Builder.CreateBitCast(Loc, Ty);
391 }
392 Builder.CreateStore(Loc, Addr);
393 } else if (r.isComplex())
394 // FIXME: implement
395 ErrorUnsupported(BE, "complex in block literal");
396 else if (r.isAggregate())
397 ; // Already created into the destination
398 else
399 assert (0 && "bad block variable");
400 // FIXME: Ensure that the offset created by the backend for
401 // the struct matches the previously computed offset in BlockDecls.
402 }
403 NoteForHelper.resize(NumHelpers);
Mike Stumpcf62d392009-03-06 18:42:23 +0000404
405 // __descriptor
Fariborz Jahanian89ecd412010-08-04 16:57:49 +0000406 llvm::Value *Descriptor = BuildDescriptorBlockDecl(BE, Info, Ty,
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000407 BlockVarLayout,
Mike Stump08920992009-03-07 02:35:30 +0000408 &NoteForHelper);
409 Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
410 Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
Mike Stumpe5fee252009-02-13 16:19:19 +0000411 }
Mike Stump00470a12009-03-05 08:32:30 +0000412
Mike Stumpbd65cac2009-02-19 01:01:04 +0000413 QualType BPT = BE->getType();
Fariborz Jahanian263c4de2010-02-10 23:34:57 +0000414 V = Builder.CreateBitCast(V, ConvertType(BPT));
415 // See if this is a __weak block variable and the must call objc_read_weak
416 // on it.
417 const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>();
418 QualType RES = ftype->getResultType();
419 if (RES.isObjCGCWeak()) {
420 // Must cast argument to id*
421 const llvm::Type *ObjectPtrTy =
422 ConvertType(CGM.getContext().getObjCIdType());
423 const llvm::Type *PtrObjectPtrTy =
424 llvm::PointerType::getUnqual(ObjectPtrTy);
425 V = Builder.CreateBitCast(V, PtrObjectPtrTy);
426 V = CGM.getObjCRuntime().EmitObjCWeakRead(*this, V);
427 }
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.
Mike Stump083c25e2009-10-22 00:49:09 +0000572 if (!BlockHasCopyDispose)
573 if (E->isByRef()
Fariborz Jahaniane27e9d62010-11-11 00:11:38 +0000574 || BlockRequiresCopying(E))
Mike Stump083c25e2009-10-22 00:49:09 +0000575 BlockHasCopyDispose = true;
Mike Stumpea26cb52009-10-21 03:49:08 +0000576
John McCallea1471e2010-05-20 01:18:31 +0000577 const ValueDecl *D = cast<ValueDecl>(E->getDecl());
Mike Stumpea26cb52009-10-21 03:49:08 +0000578
John McCallea1471e2010-05-20 01:18:31 +0000579 CharUnits Size;
580 CharUnits Align;
581
582 if (E->isByRef()) {
583 llvm::tie(Size,Align) =
584 getContext().getTypeInfoInChars(getContext().VoidPtrTy);
585 } else {
586 Size = getContext().getTypeSizeInChars(D->getType());
587 Align = getContext().getDeclAlign(D);
588 }
589
590 Offset = getBlockOffset(Size, Align);
591 BlockLayout.push_back(E);
Mike Stumpea26cb52009-10-21 03:49:08 +0000592}
593
John McCallee504292010-05-21 04:11:14 +0000594llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const ValueDecl *VD,
595 bool IsByRef) {
Fariborz Jahaniandf8b8ea2010-06-04 16:10:00 +0000596
John McCallea1471e2010-05-20 01:18:31 +0000597 CharUnits offset = BlockDecls[VD];
598 assert(!offset.isZero() && "getting address of unallocated decl");
Mike Stumpdab514f2009-03-04 03:23:46 +0000599
600 llvm::Value *BlockLiteral = LoadBlockStruct();
601 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
Chris Lattner77b89b82010-06-27 07:15:29 +0000602 llvm::ConstantInt::get(Int64Ty, offset.getQuantity()),
Mike Stump58a85142009-03-04 22:48:06 +0000603 "block.literal");
John McCallee504292010-05-21 04:11:14 +0000604 if (IsByRef) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000605 const llvm::Type *PtrStructTy
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000606 = llvm::PointerType::get(BuildByRefType(VD), 0);
Mike Stumpa8b60c92009-03-21 21:00:35 +0000607 // The block literal will need a copy/destroy helper.
608 BlockHasCopyDispose = true;
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000609
610 const llvm::Type *Ty = PtrStructTy;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000611 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000612 V = Builder.CreateBitCast(V, Ty);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000613 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000614 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000615 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000616 V = Builder.CreateBitCast(V, PtrStructTy);
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000617 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
618 VD->getNameAsString());
Fariborz Jahaniand33ded52010-05-04 17:59:32 +0000619 if (VD->getType()->isReferenceType())
620 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000621 } else {
John McCall46ec70e2010-10-28 21:37:57 +0000622 const llvm::Type *Ty = CGM.getTypes().ConvertTypeForMem(VD->getType());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000623 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000624 V = Builder.CreateBitCast(V, Ty);
Fariborz Jahanian79651722010-06-02 21:35:17 +0000625 if (VD->getType()->isReferenceType())
Fariborz Jahaniandf8b8ea2010-06-04 16:10:00 +0000626 V = Builder.CreateLoad(V, "ref.tmp");
Mike Stumpdab514f2009-03-04 03:23:46 +0000627 }
628 return V;
629}
630
Mike Stump67a64482009-02-14 22:16:35 +0000631llvm::Constant *
Mike Stump90a90432009-03-04 18:47:42 +0000632BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000633 // Generate the block descriptor.
634 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000635 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
636 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000637
Blaine Garst2a7eb282010-02-23 21:51:17 +0000638 llvm::Constant *DescriptorFields[4];
Mike Stumpa5448542009-02-13 15:32:32 +0000639
Anders Carlssond5cab542009-02-12 17:55:02 +0000640 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000641 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000642
Anders Carlssond5cab542009-02-12 17:55:02 +0000643 // Block literal size. For global blocks we just use the size of the generic
644 // block literal struct.
Ken Dyck687cc4a2010-01-26 13:48:07 +0000645 CharUnits BlockLiteralSize =
646 CGM.GetTargetTypeStoreSize(getGenericBlockLiteralType());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000647 DescriptorFields[1] =
Ken Dyck199c3d62010-01-11 17:06:35 +0000648 llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize.getQuantity());
Blaine Garst2a7eb282010-02-23 21:51:17 +0000649
650 // signature. non-optional ObjC-style method descriptor @encode sequence
651 std::string BlockTypeEncoding;
652 CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
Mike Stumpa5448542009-02-13 15:32:32 +0000653
Blaine Garst2a7eb282010-02-23 21:51:17 +0000654 DescriptorFields[2] = llvm::ConstantExpr::getBitCast(
655 CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty);
656
657 // layout
658 DescriptorFields[3] =
659 llvm::ConstantInt::get(UnsignedLongTy,0);
660
661 // build the structure from the 4 elements
Mike Stumpa5448542009-02-13 15:32:32 +0000662 llvm::Constant *DescriptorStruct =
Blaine Garst2a7eb282010-02-23 21:51:17 +0000663 llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 4, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000664
Anders Carlssond5cab542009-02-12 17:55:02 +0000665 llvm::GlobalVariable *Descriptor =
Owen Anderson1c431b32009-07-08 19:05:04 +0000666 new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000667 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000668 DescriptorStruct, "__block_descriptor_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000669
David Chisnall5e530af2009-11-17 19:33:30 +0000670 int FieldCount = 5;
Anders Carlssond5cab542009-02-12 17:55:02 +0000671 // Generate the constants for the block literal.
David Chisnall5e530af2009-11-17 19:33:30 +0000672
673 std::vector<llvm::Constant*> LiteralFields(FieldCount);
Mike Stumpa5448542009-02-13 15:32:32 +0000674
John McCallee504292010-05-21 04:11:14 +0000675 CGBlockInfo Info(n);
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000676 llvm::Constant *BlockVarLayout;
Mike Stump7f28a9c2009-03-13 23:34:28 +0000677 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000678 llvm::Function *Fn
Fariborz Jahanian7edddb82010-07-28 19:07:18 +0000679 = CodeGenFunction(CGM).GenerateBlockFunction(GlobalDecl(), BE,
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000680 Info, 0, BlockVarLayout,
681 LocalDeclMap);
John McCallee504292010-05-21 04:11:14 +0000682 assert(Info.BlockSize == BlockLiteralSize
Mike Stump4e7a1f72009-02-21 20:00:35 +0000683 && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000684
Anders Carlssond5cab542009-02-12 17:55:02 +0000685 // isa
Daniel Dunbar673431a2010-07-16 00:00:15 +0000686 LiteralFields[0] = CGM.getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000687
Fariborz Jahanian7edddb82010-07-28 19:07:18 +0000688 // __flags
689 unsigned flags = computeBlockFlag(CGM, BE,
690 (BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE));
Blaine Garst2a7eb282010-02-23 21:51:17 +0000691 LiteralFields[1] =
Fariborz Jahanian7edddb82010-07-28 19:07:18 +0000692 llvm::ConstantInt::get(IntTy, flags);
Mike Stumpa5448542009-02-13 15:32:32 +0000693
Anders Carlssond5cab542009-02-12 17:55:02 +0000694 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000695 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000696
Anders Carlssond5cab542009-02-12 17:55:02 +0000697 // Function
698 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000699
Anders Carlssond5cab542009-02-12 17:55:02 +0000700 // Descriptor
701 LiteralFields[4] = Descriptor;
David Chisnall5e530af2009-11-17 19:33:30 +0000702
Mike Stumpa5448542009-02-13 15:32:32 +0000703 llvm::Constant *BlockLiteralStruct =
David Chisnall5e530af2009-11-17 19:33:30 +0000704 llvm::ConstantStruct::get(VMContext, LiteralFields, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000705
706 llvm::GlobalVariable *BlockLiteral =
Owen Anderson1c431b32009-07-08 19:05:04 +0000707 new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000708 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000709 BlockLiteralStruct, "__block_literal_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000710
Anders Carlssond5cab542009-02-12 17:55:02 +0000711 return BlockLiteral;
712}
713
Mike Stump4e7a1f72009-02-21 20:00:35 +0000714llvm::Value *CodeGenFunction::LoadBlockStruct() {
Mike Stumpbf1914b2009-10-20 20:30:01 +0000715 llvm::Value *V = Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()],
716 "self");
717 // For now, we codegen based upon byte offsets.
718 return Builder.CreateBitCast(V, PtrToInt8Ty);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000719}
720
Mike Stump00470a12009-03-05 08:32:30 +0000721llvm::Function *
Fariborz Jahanian564360b2010-06-24 00:08:06 +0000722CodeGenFunction::GenerateBlockFunction(GlobalDecl GD, const BlockExpr *BExpr,
John McCallee504292010-05-21 04:11:14 +0000723 CGBlockInfo &Info,
Mike Stump6cc88f72009-03-20 21:53:12 +0000724 const Decl *OuterFuncDecl,
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000725 llvm::Constant *& BlockVarLayout,
John McCallee504292010-05-21 04:11:14 +0000726 llvm::DenseMap<const Decl*, llvm::Value*> ldm) {
Devang Patel963dfbd2009-04-15 21:51:44 +0000727
728 // Check if we should generate debug info for this block.
729 if (CGM.getDebugInfo())
730 DebugInfo = CGM.getDebugInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Mike Stump7f28a9c2009-03-13 23:34:28 +0000732 // Arrange for local static and local extern declarations to appear
733 // to be local to this function as well, as they are directly referenced
734 // in a block.
735 for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
736 i != ldm.end();
737 ++i) {
738 const VarDecl *VD = dyn_cast<VarDecl>(i->first);
Mike Stump1eb44332009-09-09 15:08:12 +0000739
John McCalld931b082010-08-26 03:08:43 +0000740 if (VD->getStorageClass() == SC_Static || VD->hasExternalStorage())
Mike Stump7f28a9c2009-03-13 23:34:28 +0000741 LocalDeclMap[VD] = i->second;
742 }
743
Ken Dyck687cc4a2010-01-26 13:48:07 +0000744 BlockOffset =
745 CGM.GetTargetTypeStoreSize(CGM.getGenericBlockLiteralType());
Ken Dyck30a8a272010-01-26 19:13:33 +0000746 BlockAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Eli Friedman48f91222009-03-28 03:24:54 +0000747
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000748 const FunctionType *BlockFunctionType = BExpr->getFunctionType();
749 QualType ResultType;
Rafael Espindola264ba482010-03-30 20:24:48 +0000750 FunctionType::ExtInfo EInfo = getFunctionExtInfo(*BlockFunctionType);
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000751 bool IsVariadic;
Mike Stump1eb44332009-09-09 15:08:12 +0000752 if (const FunctionProtoType *FTy =
Fariborz Jahanianda0895d2009-04-11 18:54:57 +0000753 dyn_cast<FunctionProtoType>(BlockFunctionType)) {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000754 ResultType = FTy->getResultType();
755 IsVariadic = FTy->isVariadic();
Mike Stumpb3589f42009-07-30 22:28:39 +0000756 } else {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000757 // K&R style block.
758 ResultType = BlockFunctionType->getResultType();
759 IsVariadic = false;
760 }
Mike Stumpa5448542009-02-13 15:32:32 +0000761
Anders Carlssond5cab542009-02-12 17:55:02 +0000762 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000763
Mike Stumpea26cb52009-10-21 03:49:08 +0000764 CurFuncDecl = OuterFuncDecl;
765
Chris Lattner161d36d2009-02-28 19:01:03 +0000766 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlssond5cab542009-02-12 17:55:02 +0000767
Mike Stumpea26cb52009-10-21 03:49:08 +0000768 IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor");
Mike Stumpadaaad32009-10-20 02:12:22 +0000769
John McCallea1471e2010-05-20 01:18:31 +0000770 // Build the block struct now.
John McCallee504292010-05-21 04:11:14 +0000771 AllocateAllBlockDeclRefs(*this, Info);
Mike Stumpea26cb52009-10-21 03:49:08 +0000772
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000773 // Capture block layout info. here.
774 if (CGM.getContext().getLangOptions().ObjC1)
Fariborz Jahanianc5904b42010-09-11 01:27:29 +0000775 BlockVarLayout = CGM.getObjCRuntime().GCBlockLayout(*this, BlockLayout);
Fariborz Jahanian44034db2010-08-04 18:44:59 +0000776 else
777 BlockVarLayout = llvm::Constant::getNullValue(PtrToInt8Ty);
778
Mike Stump083c25e2009-10-22 00:49:09 +0000779 QualType ParmTy = getContext().getBlockParmType(BlockHasCopyDispose,
John McCallea1471e2010-05-20 01:18:31 +0000780 BlockLayout);
781
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;
910 Info.BlockHasCopyDispose = BlockHasCopyDispose;
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(),
936 SourceLocation());
937 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::
945GenerateCopyHelperFunction(bool BlockHasCopyDispose, 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);
1010 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001011 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +00001012 llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
Fariborz Jahanian34999872010-11-13 21:53:34 +00001013 if (NoteForHelper[i].cxxvar_import) {
1014 CGF.EmitSynthesizedCXXCopyCtor(Dstv, Srcv,
1015 NoteForHelper[i].cxxvar_import);
1016 }
1017 else {
1018 Srcv = Builder.CreateLoad(Srcv);
1019 Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
1020 llvm::Value *N = llvm::ConstantInt::get(CGF.Int32Ty, flag);
1021 llvm::Value *F = CGM.getBlockObjectAssign();
1022 Builder.CreateCall3(F, Dstv, Srcv, N);
1023 }
Mike Stumpb7477cf2009-04-10 18:52:28 +00001024 }
Mike Stump08920992009-03-07 02:35:30 +00001025 }
1026 }
1027
Mike Stumpa4f668f2009-03-06 01:33:24 +00001028 CGF.FinishFunction();
1029
Owen Anderson3c4972d2009-07-29 18:54:39 +00001030 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +00001031}
1032
Mike Stumpcf62d392009-03-06 18:42:23 +00001033llvm::Constant *BlockFunction::
Mike Stump08920992009-03-07 02:35:30 +00001034GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
1035 const llvm::StructType* T,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001036 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +00001037 QualType R = getContext().VoidTy;
1038
1039 FunctionArgList Args;
1040 // FIXME: This leaks
1041 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001042 ImplicitParamDecl::Create(getContext(), 0,
1043 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +00001044 getContext().getPointerType(getContext().VoidTy));
1045
1046 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Mike Stumpa4f668f2009-03-06 01:33:24 +00001048 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +00001049 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stumpa4f668f2009-03-06 01:33:24 +00001050
Mike Stump3899a7f2009-06-05 23:26:36 +00001051 // FIXME: We'd like to put these into a mergable by content, with
1052 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +00001053 CodeGenTypes &Types = CGM.getTypes();
1054 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1055
1056 llvm::Function *Fn =
1057 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001058 "__destroy_helper_block_", &CGM.getModule());
Mike Stumpa4f668f2009-03-06 01:33:24 +00001059
1060 IdentifierInfo *II
1061 = &CGM.getContext().Idents.get("__destroy_helper_block_");
1062
1063 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1064 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001065 SourceLocation(), II, R, 0,
John McCalld931b082010-08-26 03:08:43 +00001066 SC_Static,
1067 SC_None,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001068 false, true);
Mike Stumpa4f668f2009-03-06 01:33:24 +00001069 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1edf6b62009-03-07 02:53:18 +00001070
Mike Stumpb7477cf2009-04-10 18:52:28 +00001071 if (NoteForHelperp) {
1072 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump1edf6b62009-03-07 02:53:18 +00001073
Mike Stumpb7477cf2009-04-10 18:52:28 +00001074 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
1075 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +00001076 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +00001077 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
1078 SrcObj = Builder.CreateLoad(SrcObj);
Fariborz Jahanian34999872010-11-13 21:53:34 +00001079 EHScopeStack::stable_iterator CleanupDepth = CGF.EHStack.stable_begin();
Mike Stumpb7477cf2009-04-10 18:52:28 +00001080 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
1081 int flag = NoteForHelper[i].flag;
1082 int index = NoteForHelper[i].index;
Mike Stump1edf6b62009-03-07 02:53:18 +00001083
Mike Stumpb7477cf2009-04-10 18:52:28 +00001084 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
1085 || NoteForHelper[i].RequiresCopying) {
1086 llvm::Value *Srcv = SrcObj;
1087 Srcv = Builder.CreateStructGEP(Srcv, index);
1088 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001089 llvm::PointerType::get(PtrToInt8Ty, 0));
Fariborz Jahanian34999872010-11-13 21:53:34 +00001090 if (NoteForHelper[i].cxxvar_import) {
1091 const BlockDeclRefExpr *BDRE = NoteForHelper[i].cxxvar_import;
1092 const Expr *E = BDRE->getCopyConstructorExpr();
1093 QualType ClassTy = E->getType();
1094 QualType PtrClassTy = getContext().getPointerType(ClassTy);
1095 const llvm::Type *t = CGM.getTypes().ConvertType(PtrClassTy);
1096 Srcv = Builder.CreateBitCast(Srcv, t);
1097 CGF.PushDestructorCleanup(ClassTy, Srcv);
1098 }
1099 else {
1100 Srcv = Builder.CreateLoad(Srcv);
1101 BuildBlockRelease(Srcv, flag);
1102 }
Mike Stumpb7477cf2009-04-10 18:52:28 +00001103 }
Mike Stump1edf6b62009-03-07 02:53:18 +00001104 }
Fariborz Jahanian34999872010-11-13 21:53:34 +00001105 CGF.PopCleanupBlocks(CleanupDepth);
Mike Stump1edf6b62009-03-07 02:53:18 +00001106 }
1107
Mike Stumpa4f668f2009-03-06 01:33:24 +00001108 CGF.FinishFunction();
1109
Owen Anderson3c4972d2009-07-29 18:54:39 +00001110 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpa4f668f2009-03-06 01:33:24 +00001111}
1112
Mike Stump08920992009-03-07 02:35:30 +00001113llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001114 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump08920992009-03-07 02:35:30 +00001115 return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
1116 T, NoteForHelper);
Mike Stumpa4f668f2009-03-06 01:33:24 +00001117}
1118
Mike Stump08920992009-03-07 02:35:30 +00001119llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001120 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stump08920992009-03-07 02:35:30 +00001121 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001122 T, NoteForHelperp);
Mike Stumpdab514f2009-03-04 03:23:46 +00001123}
Mike Stump797b6322009-03-05 01:23:13 +00001124
Mike Stumpee094222009-03-06 06:12:24 +00001125llvm::Constant *BlockFunction::
1126GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
Mike Stump45031c02009-03-06 02:29:21 +00001127 QualType R = getContext().VoidTy;
1128
1129 FunctionArgList Args;
1130 // FIXME: This leaks
Mike Stumpee094222009-03-06 06:12:24 +00001131 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +00001132 ImplicitParamDecl::Create(getContext(), 0,
1133 SourceLocation(), 0,
Mike Stumpee094222009-03-06 06:12:24 +00001134 getContext().getPointerType(getContext().VoidTy));
1135 Args.push_back(std::make_pair(Dst, Dst->getType()));
1136
1137 // FIXME: This leaks
Mike Stump45031c02009-03-06 02:29:21 +00001138 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001139 ImplicitParamDecl::Create(getContext(), 0,
1140 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001141 getContext().getPointerType(getContext().VoidTy));
Mike Stump45031c02009-03-06 02:29:21 +00001142 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Mike Stump45031c02009-03-06 02:29:21 +00001144 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +00001145 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stump45031c02009-03-06 02:29:21 +00001146
Mike Stump45031c02009-03-06 02:29:21 +00001147 CodeGenTypes &Types = CGM.getTypes();
1148 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1149
Mike Stump3899a7f2009-06-05 23:26:36 +00001150 // FIXME: We'd like to put these into a mergable by content, with
1151 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001152 llvm::Function *Fn =
1153 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001154 "__Block_byref_id_object_copy_", &CGM.getModule());
Mike Stump45031c02009-03-06 02:29:21 +00001155
1156 IdentifierInfo *II
1157 = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
1158
1159 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1160 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001161 SourceLocation(), II, R, 0,
John McCalld931b082010-08-26 03:08:43 +00001162 SC_Static,
1163 SC_None,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001164 false, true);
Mike Stump45031c02009-03-06 02:29:21 +00001165 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpee094222009-03-06 06:12:24 +00001166
1167 // dst->x
1168 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001169 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001170 V = Builder.CreateLoad(V);
Mike Stumpee094222009-03-06 06:12:24 +00001171 V = Builder.CreateStructGEP(V, 6, "x");
1172 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
1173
1174 // src->x
1175 V = CGF.GetAddrOfLocalVar(Src);
1176 V = Builder.CreateLoad(V);
1177 V = Builder.CreateBitCast(V, T);
1178 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001179 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpee094222009-03-06 06:12:24 +00001180 llvm::Value *SrcObj = Builder.CreateLoad(V);
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Mike Stumpee094222009-03-06 06:12:24 +00001182 flag |= BLOCK_BYREF_CALLER;
1183
Chris Lattner77b89b82010-06-27 07:15:29 +00001184 llvm::Value *N = llvm::ConstantInt::get(CGF.Int32Ty, flag);
Daniel Dunbar673431a2010-07-16 00:00:15 +00001185 llvm::Value *F = CGM.getBlockObjectAssign();
Mike Stumpee094222009-03-06 06:12:24 +00001186 Builder.CreateCall3(F, DstObj, SrcObj, N);
1187
Mike Stump45031c02009-03-06 02:29:21 +00001188 CGF.FinishFunction();
1189
Owen Anderson3c4972d2009-07-29 18:54:39 +00001190 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001191}
1192
Mike Stump1851b682009-03-06 04:53:30 +00001193llvm::Constant *
1194BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
1195 int flag) {
Mike Stump45031c02009-03-06 02:29:21 +00001196 QualType R = getContext().VoidTy;
1197
1198 FunctionArgList Args;
1199 // FIXME: This leaks
1200 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001201 ImplicitParamDecl::Create(getContext(), 0,
1202 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001203 getContext().getPointerType(getContext().VoidTy));
1204
1205 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Mike Stump45031c02009-03-06 02:29:21 +00001207 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +00001208 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stump45031c02009-03-06 02:29:21 +00001209
Mike Stump45031c02009-03-06 02:29:21 +00001210 CodeGenTypes &Types = CGM.getTypes();
1211 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1212
Mike Stump3899a7f2009-06-05 23:26:36 +00001213 // FIXME: We'd like to put these into a mergable by content, with
1214 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001215 llvm::Function *Fn =
1216 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001217 "__Block_byref_id_object_dispose_",
Mike Stump45031c02009-03-06 02:29:21 +00001218 &CGM.getModule());
1219
1220 IdentifierInfo *II
1221 = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
1222
1223 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1224 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001225 SourceLocation(), II, R, 0,
John McCalld931b082010-08-26 03:08:43 +00001226 SC_Static,
1227 SC_None,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001228 false, true);
Mike Stump45031c02009-03-06 02:29:21 +00001229 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1851b682009-03-06 04:53:30 +00001230
1231 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001232 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001233 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001234 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001235 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001236 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001237
Mike Stump1851b682009-03-06 04:53:30 +00001238 flag |= BLOCK_BYREF_CALLER;
1239 BuildBlockRelease(V, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001240 CGF.FinishFunction();
1241
Owen Anderson3c4972d2009-07-29 18:54:39 +00001242 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001243}
1244
Mike Stumpee094222009-03-06 06:12:24 +00001245llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001246 int Flag, unsigned Align) {
1247 // All alignments below that of pointer alignment collapse down to just
Mike Stump3899a7f2009-06-05 23:26:36 +00001248 // pointer alignment, as we always have at least that much alignment to begin
1249 // with.
1250 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001251
Mike Stump3899a7f2009-06-05 23:26:36 +00001252 // As an optimization, we only generate a single function of each kind we
1253 // might need. We need a different one for each alignment and for each
1254 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001255 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1256 llvm::Constant *&Entry = CGM.AssignCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001257 if (Entry)
1258 return Entry;
Chris Lattner10976d92009-12-05 08:21:30 +00001259 return Entry = CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, Flag);
Mike Stump45031c02009-03-06 02:29:21 +00001260}
1261
Mike Stump1851b682009-03-06 04:53:30 +00001262llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001263 int Flag,
Mike Stump3899a7f2009-06-05 23:26:36 +00001264 unsigned Align) {
1265 // All alignments below that of pointer alignment collpase down to just
1266 // pointer alignment, as we always have at least that much alignment to begin
1267 // with.
1268 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001269
Mike Stump3899a7f2009-06-05 23:26:36 +00001270 // As an optimization, we only generate a single function of each kind we
1271 // might need. We need a different one for each alignment and for each
1272 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001273 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1274 llvm::Constant *&Entry = CGM.DestroyCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001275 if (Entry)
1276 return Entry;
Chris Lattner10976d92009-12-05 08:21:30 +00001277 return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, Flag);
Mike Stump45031c02009-03-06 02:29:21 +00001278}
1279
Mike Stump1851b682009-03-06 04:53:30 +00001280void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Daniel Dunbar673431a2010-07-16 00:00:15 +00001281 llvm::Value *F = CGM.getBlockObjectDispose();
Mike Stump1851b682009-03-06 04:53:30 +00001282 llvm::Value *N;
Mike Stump797b6322009-03-05 01:23:13 +00001283 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Chris Lattner77b89b82010-06-27 07:15:29 +00001284 N = llvm::ConstantInt::get(CGF.Int32Ty, flag);
Mike Stump797b6322009-03-05 01:23:13 +00001285 Builder.CreateCall2(F, V, N);
1286}
Mike Stump00470a12009-03-05 08:32:30 +00001287
1288ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stump08920992009-03-07 02:35:30 +00001289
1290BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1291 CGBuilderTy &B)
Chris Lattner77b89b82010-06-27 07:15:29 +00001292 : CGM(cgm), VMContext(cgm.getLLVMContext()), CGF(cgf), Builder(B) {
Owen Anderson0032b272009-08-13 21:57:51 +00001293 PtrToInt8Ty = llvm::PointerType::getUnqual(
1294 llvm::Type::getInt8Ty(VMContext));
Mike Stump08920992009-03-07 02:35:30 +00001295
1296 BlockHasCopyDispose = false;
1297}