blob: e5871b27d46804937b1503c8de839fcd88497f03 [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 +000027/// CGBlockInfo - Information to generate a block literal.
28class clang::CodeGen::CGBlockInfo {
29public:
30 /// Name - The name of the block, kindof.
31 const char *Name;
32
33 /// DeclRefs - Variables from parent scopes that have been
34 /// imported into this block.
35 llvm::SmallVector<const BlockDeclRefExpr *, 8> DeclRefs;
36
37 /// InnerBlocks - This block and the blocks it encloses.
38 llvm::SmallPtrSet<const DeclContext *, 4> InnerBlocks;
39
40 /// CXXThisRef - Non-null if 'this' was required somewhere, in
41 /// which case this is that expression.
42 const CXXThisExpr *CXXThisRef;
43
44 /// NeedsObjCSelf - True if something in this block has an implicit
45 /// reference to 'self'.
46 bool NeedsObjCSelf;
47
48 /// These are initialized by GenerateBlockFunction.
49 bool BlockHasCopyDispose;
50 CharUnits BlockSize;
51 CharUnits BlockAlign;
52 llvm::SmallVector<const Expr*, 8> BlockLayout;
53
54 CGBlockInfo(const char *Name);
55};
56
57CGBlockInfo::CGBlockInfo(const char *N)
58 : Name(N), CXXThisRef(0), NeedsObjCSelf(false) {
59
60 // Skip asm prefix, if any.
61 if (Name && Name[0] == '\01')
62 ++Name;
63}
64
65
Mike Stumpcf62d392009-03-06 18:42:23 +000066llvm::Constant *CodeGenFunction::
Blaine Garst2a7eb282010-02-23 21:51:17 +000067BuildDescriptorBlockDecl(const BlockExpr *BE, bool BlockHasCopyDispose, CharUnits Size,
Mike Stumpa803b0e2009-03-25 17:58:24 +000068 const llvm::StructType* Ty,
Mike Stump08920992009-03-07 02:35:30 +000069 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump56129b12009-02-13 16:55:51 +000070 const llvm::Type *UnsignedLongTy
71 = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpe5fee252009-02-13 16:19:19 +000072 llvm::Constant *C;
73 std::vector<llvm::Constant*> Elts;
74
75 // reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +000076 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpe5fee252009-02-13 16:19:19 +000077 Elts.push_back(C);
78
79 // Size
Mike Stumpd6840002009-02-21 20:07:44 +000080 // FIXME: What is the right way to say this doesn't fit? We should give
81 // a user diagnostic in that case. Better fix would be to change the
82 // API to size_t.
Ken Dyck199c3d62010-01-11 17:06:35 +000083 C = llvm::ConstantInt::get(UnsignedLongTy, Size.getQuantity());
Mike Stumpe5fee252009-02-13 16:19:19 +000084 Elts.push_back(C);
85
Blaine Garst2a7eb282010-02-23 21:51:17 +000086 // optional copy/dispose helpers
Mike Stumpe5fee252009-02-13 16:19:19 +000087 if (BlockHasCopyDispose) {
88 // copy_func_helper_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000089 Elts.push_back(BuildCopyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000090
91 // destroy_func_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000092 Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000093 }
94
Blaine Garst2a7eb282010-02-23 21:51:17 +000095 // Signature. non-optional ObjC-style method descriptor @encode sequence
96 std::string BlockTypeEncoding;
97 CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
98
99 Elts.push_back(llvm::ConstantExpr::getBitCast(
100 CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty));
101
102 // Layout.
103 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
104 Elts.push_back(C);
105
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000106 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stumpe5fee252009-02-13 16:19:19 +0000107
Owen Anderson1c431b32009-07-08 19:05:04 +0000108 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Mike Stumpe5fee252009-02-13 16:19:19 +0000109 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000110 C, "__block_descriptor_tmp");
Mike Stumpe5fee252009-02-13 16:19:19 +0000111 return C;
112}
113
Mike Stump2a998142009-03-04 18:17:45 +0000114llvm::Constant *BlockModule::getNSConcreteGlobalBlock() {
Chris Lattnere2f79b62009-05-13 02:50:56 +0000115 if (NSConcreteGlobalBlock == 0)
Mike Stump1eb44332009-09-09 15:08:12 +0000116 NSConcreteGlobalBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
Chris Lattnere2f79b62009-05-13 02:50:56 +0000117 "_NSConcreteGlobalBlock");
Mike Stumpf99f1d02009-02-13 17:23:42 +0000118 return NSConcreteGlobalBlock;
119}
120
Mike Stump2a998142009-03-04 18:17:45 +0000121llvm::Constant *BlockModule::getNSConcreteStackBlock() {
Chris Lattnere2f79b62009-05-13 02:50:56 +0000122 if (NSConcreteStackBlock == 0)
Mike Stump1eb44332009-09-09 15:08:12 +0000123 NSConcreteStackBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
Chris Lattnere2f79b62009-05-13 02:50:56 +0000124 "_NSConcreteStackBlock");
Mike Stump59c5b112009-02-13 19:29:27 +0000125 return NSConcreteStackBlock;
126}
127
John McCallee504292010-05-21 04:11:14 +0000128static void CollectBlockDeclRefInfo(const Stmt *S, CGBlockInfo &Info) {
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000129 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
130 I != E; ++I)
Daniel Dunbar82573ee2009-03-02 07:00:57 +0000131 if (*I)
John McCallee504292010-05-21 04:11:14 +0000132 CollectBlockDeclRefInfo(*I, Info);
Mike Stump00470a12009-03-05 08:32:30 +0000133
Mike Stumpea26cb52009-10-21 03:49:08 +0000134 // We want to ensure we walk down into block literals so we can find
135 // all nested BlockDeclRefExprs.
Mike Stump38e16272009-10-21 22:01:24 +0000136 if (const BlockExpr *BE = dyn_cast<BlockExpr>(S)) {
John McCallee504292010-05-21 04:11:14 +0000137 Info.InnerBlocks.insert(BE->getBlockDecl());
138 CollectBlockDeclRefInfo(BE->getBody(), Info);
Mike Stump38e16272009-10-21 22:01:24 +0000139 }
Mike Stumpea26cb52009-10-21 03:49:08 +0000140
John McCallee504292010-05-21 04:11:14 +0000141 else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) {
142 const ValueDecl *D = BDRE->getDecl();
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000143 // FIXME: Handle enums.
John McCallee504292010-05-21 04:11:14 +0000144 if (isa<FunctionDecl>(D))
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000145 return;
Mike Stump00470a12009-03-05 08:32:30 +0000146
John McCallee504292010-05-21 04:11:14 +0000147 if (isa<ImplicitParamDecl>(D) &&
148 isa<ObjCMethodDecl>(D->getDeclContext()) &&
149 cast<ObjCMethodDecl>(D->getDeclContext())->getSelfDecl() == D) {
150 Info.NeedsObjCSelf = true;
151 return;
152 }
153
Mike Stump38e16272009-10-21 22:01:24 +0000154 // Only Decls that escape are added.
John McCallee504292010-05-21 04:11:14 +0000155 if (!Info.InnerBlocks.count(D->getDeclContext()))
Mike Stump38e16272009-10-21 22:01:24 +0000156 Info.DeclRefs.push_back(BDRE);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000157 }
John McCallea1471e2010-05-20 01:18:31 +0000158
John McCallee504292010-05-21 04:11:14 +0000159 // Make sure to capture implicit 'self' references due to super calls.
Chandler Carruthf54b80f2010-05-21 10:29:28 +0000160 else if (const ObjCMessageExpr *E = dyn_cast<ObjCMessageExpr>(S)) {
John McCallee504292010-05-21 04:11:14 +0000161 if (E->getReceiverKind() == ObjCMessageExpr::SuperClass ||
162 E->getReceiverKind() == ObjCMessageExpr::SuperInstance)
163 Info.NeedsObjCSelf = true;
Chandler Carruthf54b80f2010-05-21 10:29:28 +0000164 }
John McCallee504292010-05-21 04:11:14 +0000165
166 // Getter/setter uses may also cause implicit super references,
167 // which we can check for with:
168 else if (isa<ObjCSuperExpr>(S))
169 Info.NeedsObjCSelf = true;
170
171 else if (isa<CXXThisExpr>(S))
John McCallea1471e2010-05-20 01:18:31 +0000172 Info.CXXThisRef = cast<CXXThisExpr>(S);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000173}
174
John McCallee504292010-05-21 04:11:14 +0000175/// CanBlockBeGlobal - Given a CGBlockInfo struct, determines if a block can be
Mike Stump58a85142009-03-04 22:48:06 +0000176/// declared as a global variable instead of on the stack.
John McCallee504292010-05-21 04:11:14 +0000177static bool CanBlockBeGlobal(const CGBlockInfo &Info) {
Mike Stumpea26cb52009-10-21 03:49:08 +0000178 return Info.DeclRefs.empty();
179}
180
181/// AllocateAllBlockDeclRefs - Preallocate all nested BlockDeclRefExprs to
182/// ensure we can generate the debug information for the parameter for the block
183/// invoke function.
John McCallee504292010-05-21 04:11:14 +0000184static void AllocateAllBlockDeclRefs(CodeGenFunction &CGF, CGBlockInfo &Info) {
John McCallea1471e2010-05-20 01:18:31 +0000185 if (Info.CXXThisRef)
John McCallee504292010-05-21 04:11:14 +0000186 CGF.AllocateBlockCXXThisPointer(Info.CXXThisRef);
Mike Stumpea26cb52009-10-21 03:49:08 +0000187
188 for (size_t i = 0; i < Info.DeclRefs.size(); ++i)
John McCallee504292010-05-21 04:11:14 +0000189 CGF.AllocateBlockDecl(Info.DeclRefs[i]);
190
191 if (Info.NeedsObjCSelf) {
192 ValueDecl *Self = cast<ObjCMethodDecl>(CGF.CurFuncDecl)->getSelfDecl();
193 BlockDeclRefExpr *BDRE =
194 new (CGF.getContext()) BlockDeclRefExpr(Self, Self->getType(),
195 SourceLocation(), false);
196 Info.DeclRefs.push_back(BDRE);
197 CGF.AllocateBlockDecl(BDRE);
198 }
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000199}
200
Mike Stump58a85142009-03-04 22:48:06 +0000201// FIXME: Push most into CGM, passing down a few bits, like current function
202// name.
Mike Stump8a2b4b12009-02-25 23:33:13 +0000203llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000204 std::string Name = CurFn->getName();
John McCallee504292010-05-21 04:11:14 +0000205 CGBlockInfo Info(Name.c_str());
206 Info.InnerBlocks.insert(BE->getBlockDecl());
207 CollectBlockDeclRefInfo(BE->getBody(), Info);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000208
209 // Check if the block can be global.
Mike Stump58a85142009-03-04 22:48:06 +0000210 // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like
211 // to just have one code path. We should move this function into CGM and pass
212 // CGF, then we can just check to see if CGF is 0.
Mike Stumpdab514f2009-03-04 03:23:46 +0000213 if (0 && CanBlockBeGlobal(Info))
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000214 return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
Mike Stump00470a12009-03-05 08:32:30 +0000215
David Chisnall5e530af2009-11-17 19:33:30 +0000216 size_t BlockFields = 5;
217
David Chisnall5e530af2009-11-17 19:33:30 +0000218 std::vector<llvm::Constant*> Elts(BlockFields);
219
Mike Stumpe5fee252009-02-13 16:19:19 +0000220 llvm::Constant *C;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000221 llvm::Value *V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000222
Mike Stumpe5fee252009-02-13 16:19:19 +0000223 {
224 // C = BuildBlockStructInitlist();
Blaine Garsta36e2232010-03-05 01:29:59 +0000225 unsigned int flags = BLOCK_HAS_SIGNATURE;
David Chisnall5e530af2009-11-17 19:33:30 +0000226
Mike Stump00470a12009-03-05 08:32:30 +0000227 // We run this first so that we set BlockHasCopyDispose from the entire
228 // block literal.
229 // __invoke
Mike Stump00470a12009-03-05 08:32:30 +0000230 llvm::Function *Fn
Fariborz Jahanian564360b2010-06-24 00:08:06 +0000231 = CodeGenFunction(CGM).GenerateBlockFunction(CurGD, BE, Info, CurFuncDecl,
John McCallee504292010-05-21 04:11:14 +0000232 LocalDeclMap);
233 BlockHasCopyDispose |= Info.BlockHasCopyDispose;
Mike Stump00470a12009-03-05 08:32:30 +0000234 Elts[3] = Fn;
235
Mike Stumpf5408fe2009-05-16 07:57:57 +0000236 // FIXME: Don't use BlockHasCopyDispose, it is set more often then
237 // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); }
John McCallee504292010-05-21 04:11:14 +0000238 if (Info.BlockHasCopyDispose)
Mike Stumpe5fee252009-02-13 16:19:19 +0000239 flags |= BLOCK_HAS_COPY_DISPOSE;
240
Mike Stump7d6dc4f2009-02-13 20:17:16 +0000241 // __isa
Mike Stump59c5b112009-02-13 19:29:27 +0000242 C = CGM.getNSConcreteStackBlock();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000243 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
Mike Stump00470a12009-03-05 08:32:30 +0000244 Elts[0] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000245
246 // __flags
Blaine Garsta36e2232010-03-05 01:29:59 +0000247 {
248 QualType BPT = BE->getType();
249 const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>();
250 QualType ResultType = ftype->getResultType();
251
252 CallArgList Args;
253 CodeGenTypes &Types = CGM.getTypes();
254 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000255 FunctionType::ExtInfo());
Blaine Garsta36e2232010-03-05 01:29:59 +0000256 if (CGM.ReturnTypeUsesSret(FnInfo))
257 flags |= BLOCK_USE_STRET;
258 }
Mike Stumpe5fee252009-02-13 16:19:19 +0000259 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
260 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000261 C = llvm::ConstantInt::get(IntTy, flags);
Mike Stump00470a12009-03-05 08:32:30 +0000262 Elts[1] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000263
264 // __reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000265 C = llvm::ConstantInt::get(IntTy, 0);
Mike Stump00470a12009-03-05 08:32:30 +0000266 Elts[2] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000267
John McCallee504292010-05-21 04:11:14 +0000268 if (Info.BlockLayout.empty()) {
Mike Stumpcf62d392009-03-06 18:42:23 +0000269 // __descriptor
John McCallee504292010-05-21 04:11:14 +0000270 Elts[4] = BuildDescriptorBlockDecl(BE, Info.BlockHasCopyDispose,
271 Info.BlockSize, 0, 0);
Mike Stumpcf62d392009-03-06 18:42:23 +0000272
Mike Stump5570cfe2009-03-01 20:07:53 +0000273 // Optimize to being a global block.
274 Elts[0] = CGM.getNSConcreteGlobalBlock();
Blaine Garsta36e2232010-03-05 01:29:59 +0000275
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000276 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
Mike Stump5570cfe2009-03-01 20:07:53 +0000277
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000278 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000279
Owen Anderson1c431b32009-07-08 19:05:04 +0000280 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000281 llvm::GlobalValue::InternalLinkage, C,
282 "__block_holder_tmp_" +
283 llvm::Twine(CGM.getGlobalUniqueCount()));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000284 QualType BPT = BE->getType();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000285 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000286 return C;
287 }
Mike Stump00470a12009-03-05 08:32:30 +0000288
John McCallee504292010-05-21 04:11:14 +0000289 std::vector<const llvm::Type *> Types(BlockFields+Info.BlockLayout.size());
Mike Stump08920992009-03-07 02:35:30 +0000290 for (int i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000291 Types[i] = Elts[i]->getType();
Mike Stump08920992009-03-07 02:35:30 +0000292 Types[4] = PtrToInt8Ty;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000293
John McCallee504292010-05-21 04:11:14 +0000294 for (unsigned i = 0, n = Info.BlockLayout.size(); i != n; ++i) {
295 const Expr *E = Info.BlockLayout[i];
Mike Stumpa99038c2009-02-28 09:07:16 +0000296 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
297 QualType Ty = E->getType();
Mike Stumpdab514f2009-03-04 03:23:46 +0000298 if (BDRE && BDRE->isByRef()) {
Fariborz Jahanian79651722010-06-02 21:35:17 +0000299 Types[i+BlockFields] =
300 llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0);
301 } else if (BDRE && BDRE->getDecl()->getType()->isReferenceType()) {
302 Types[i+BlockFields] = llvm::PointerType::get(ConvertType(Ty), 0);
303 } else
David Chisnall5e530af2009-11-17 19:33:30 +0000304 Types[i+BlockFields] = ConvertType(Ty);
Mike Stumpa99038c2009-02-28 09:07:16 +0000305 }
Mike Stump8a2b4b12009-02-25 23:33:13 +0000306
Owen Anderson47a434f2009-08-05 23:18:46 +0000307 llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000308
309 llvm::AllocaInst *A = CreateTempAlloca(Ty);
John McCallee504292010-05-21 04:11:14 +0000310 A->setAlignment(Info.BlockAlign.getQuantity());
Mike Stump8a2b4b12009-02-25 23:33:13 +0000311 V = A;
312
John McCallea1471e2010-05-20 01:18:31 +0000313 // Build layout / cleanup information for all the data entries in the
314 // layout, and write the enclosing fields into the type.
John McCallee504292010-05-21 04:11:14 +0000315 std::vector<HelperInfo> NoteForHelper(Info.BlockLayout.size());
John McCallea1471e2010-05-20 01:18:31 +0000316 unsigned NumHelpers = 0;
Mike Stump08920992009-03-07 02:35:30 +0000317
318 for (unsigned i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000319 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
Mike Stump00470a12009-03-05 08:32:30 +0000320
John McCallee504292010-05-21 04:11:14 +0000321 for (unsigned i=0; i < Info.BlockLayout.size(); ++i) {
322 const Expr *E = Info.BlockLayout[i];
Mike Stump8a2b4b12009-02-25 23:33:13 +0000323
John McCallea1471e2010-05-20 01:18:31 +0000324 // Skip padding.
325 if (isa<DeclRefExpr>(E)) continue;
Mike Stumpa99038c2009-02-28 09:07:16 +0000326
John McCallea1471e2010-05-20 01:18:31 +0000327 llvm::Value* Addr = Builder.CreateStructGEP(V, i+BlockFields, "tmp");
328 HelperInfo &Note = NoteForHelper[NumHelpers++];
Mike Stumpa99038c2009-02-28 09:07:16 +0000329
John McCallea1471e2010-05-20 01:18:31 +0000330 Note.index = i+5;
Mike Stump08920992009-03-07 02:35:30 +0000331
John McCallea1471e2010-05-20 01:18:31 +0000332 if (isa<CXXThisExpr>(E)) {
333 Note.RequiresCopying = false;
334 Note.flag = BLOCK_FIELD_IS_OBJECT;
Mike Stumpa99038c2009-02-28 09:07:16 +0000335
John McCallea1471e2010-05-20 01:18:31 +0000336 Builder.CreateStore(LoadCXXThis(), Addr);
337 continue;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000338 }
John McCallea1471e2010-05-20 01:18:31 +0000339
340 const BlockDeclRefExpr *BDRE = cast<BlockDeclRefExpr>(E);
341 const ValueDecl *VD = BDRE->getDecl();
342 QualType T = VD->getType();
343
344 Note.RequiresCopying = BlockRequiresCopying(T);
345
346 if (BDRE->isByRef()) {
347 Note.flag = BLOCK_FIELD_IS_BYREF;
348 if (T.isObjCGCWeak())
349 Note.flag |= BLOCK_FIELD_IS_WEAK;
350 } else if (T->isBlockPointerType()) {
351 Note.flag = BLOCK_FIELD_IS_BLOCK;
352 } else {
353 Note.flag = BLOCK_FIELD_IS_OBJECT;
354 }
355
356 if (LocalDeclMap[VD]) {
357 if (BDRE->isByRef()) {
358 llvm::Value *Loc = LocalDeclMap[VD];
359 Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
360 Loc = Builder.CreateLoad(Loc);
361 Builder.CreateStore(Loc, Addr);
362 continue;
363 } else {
Fariborz Jahanianac7362d2010-06-08 20:57:22 +0000364 if (BDRE->getCopyConstructorExpr()) {
365 E = BDRE->getCopyConstructorExpr();
366 // Code to destruct copy-constructed descriptor element for
367 // copied-in class object.
368 // TODO: Refactor this into common code with mostly similar
369 // CodeGenFunction::EmitLocalBlockVarDecl
370 QualType DtorTy = E->getType();
371 if (const RecordType *RT = DtorTy->getAs<RecordType>())
372 if (CXXRecordDecl *ClassDecl =
373 dyn_cast<CXXRecordDecl>(RT->getDecl())) {
374 if (!ClassDecl->hasTrivialDestructor()) {
Douglas Gregor1d110e02010-07-01 14:13:13 +0000375 const CXXDestructorDecl *D = ClassDecl->getDestructor();
Fariborz Jahanianac7362d2010-06-08 20:57:22 +0000376 assert(D && "BuildBlockLiteralTmp - destructor is nul");
377 {
378 // Normal destruction.
379 DelayedCleanupBlock Scope(*this);
380 EmitCXXDestructorCall(D, Dtor_Complete,
381 /*ForVirtualBase=*/false, Addr);
382 // Make sure to jump to the exit block.
383 EmitBranch(Scope.getCleanupExitBlock());
384 }
385 if (Exceptions) {
386 EHCleanupBlock Cleanup(*this);
387 EmitCXXDestructorCall(D, Dtor_Complete,
388 /*ForVirtualBase=*/false, Addr);
389 }
390 }
391 }
392 }
Fariborz Jahanian59da45a2010-06-04 21:35:44 +0000393 else {
394 E = new (getContext()) DeclRefExpr(const_cast<ValueDecl*>(VD),
Fariborz Jahanian79651722010-06-02 21:35:17 +0000395 VD->getType().getNonReferenceType(),
396 SourceLocation());
Fariborz Jahanian59da45a2010-06-04 21:35:44 +0000397 if (VD->getType()->isReferenceType()) {
398 E = new (getContext())
399 UnaryOperator(const_cast<Expr*>(E), UnaryOperator::AddrOf,
400 getContext().getPointerType(E->getType()),
401 SourceLocation());
402 }
Fariborz Jahaniandf8b8ea2010-06-04 16:10:00 +0000403 }
404 }
John McCallea1471e2010-05-20 01:18:31 +0000405 }
John McCallea1471e2010-05-20 01:18:31 +0000406
407 if (BDRE->isByRef()) {
408 E = new (getContext())
409 UnaryOperator(const_cast<Expr*>(E), UnaryOperator::AddrOf,
410 getContext().getPointerType(E->getType()),
411 SourceLocation());
412 }
413
414 RValue r = EmitAnyExpr(E, Addr, false);
415 if (r.isScalar()) {
416 llvm::Value *Loc = r.getScalarVal();
417 const llvm::Type *Ty = Types[i+BlockFields];
418 if (BDRE->isByRef()) {
419 // E is now the address of the value field, instead, we want the
420 // address of the actual ByRef struct. We optimize this slightly
421 // compared to gcc by not grabbing the forwarding slot as this must
422 // be done during Block_copy for us, and we can postpone the work
423 // until then.
424 CharUnits offset = BlockDecls[BDRE->getDecl()];
425
426 llvm::Value *BlockLiteral = LoadBlockStruct();
427
428 Loc = Builder.CreateGEP(BlockLiteral,
Chris Lattner77b89b82010-06-27 07:15:29 +0000429 llvm::ConstantInt::get(Int64Ty, offset.getQuantity()),
John McCallea1471e2010-05-20 01:18:31 +0000430 "block.literal");
431 Ty = llvm::PointerType::get(Ty, 0);
432 Loc = Builder.CreateBitCast(Loc, Ty);
433 Loc = Builder.CreateLoad(Loc);
434 // Loc = Builder.CreateBitCast(Loc, Ty);
435 }
436 Builder.CreateStore(Loc, Addr);
437 } else if (r.isComplex())
438 // FIXME: implement
439 ErrorUnsupported(BE, "complex in block literal");
440 else if (r.isAggregate())
441 ; // Already created into the destination
442 else
443 assert (0 && "bad block variable");
444 // FIXME: Ensure that the offset created by the backend for
445 // the struct matches the previously computed offset in BlockDecls.
446 }
447 NoteForHelper.resize(NumHelpers);
Mike Stumpcf62d392009-03-06 18:42:23 +0000448
449 // __descriptor
Blaine Garst2a7eb282010-02-23 21:51:17 +0000450 llvm::Value *Descriptor = BuildDescriptorBlockDecl(BE,
John McCallee504292010-05-21 04:11:14 +0000451 Info.BlockHasCopyDispose,
452 Info.BlockSize, Ty,
Mike Stump08920992009-03-07 02:35:30 +0000453 &NoteForHelper);
454 Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
455 Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
Mike Stumpe5fee252009-02-13 16:19:19 +0000456 }
Mike Stump00470a12009-03-05 08:32:30 +0000457
Mike Stumpbd65cac2009-02-19 01:01:04 +0000458 QualType BPT = BE->getType();
Fariborz Jahanian263c4de2010-02-10 23:34:57 +0000459 V = Builder.CreateBitCast(V, ConvertType(BPT));
460 // See if this is a __weak block variable and the must call objc_read_weak
461 // on it.
462 const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>();
463 QualType RES = ftype->getResultType();
464 if (RES.isObjCGCWeak()) {
465 // Must cast argument to id*
466 const llvm::Type *ObjectPtrTy =
467 ConvertType(CGM.getContext().getObjCIdType());
468 const llvm::Type *PtrObjectPtrTy =
469 llvm::PointerType::getUnqual(ObjectPtrTy);
470 V = Builder.CreateBitCast(V, PtrObjectPtrTy);
471 V = CGM.getObjCRuntime().EmitObjCWeakRead(*this, V);
472 }
473 return V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000474}
475
476
Mike Stump2a998142009-03-04 18:17:45 +0000477const llvm::Type *BlockModule::getBlockDescriptorType() {
Mike Stumpab695142009-02-13 15:16:56 +0000478 if (BlockDescriptorType)
479 return BlockDescriptorType;
480
Mike Stumpa5448542009-02-13 15:32:32 +0000481 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000482 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000483
Mike Stumpab695142009-02-13 15:16:56 +0000484 // struct __block_descriptor {
485 // unsigned long reserved;
486 // unsigned long block_size;
Blaine Garst2a7eb282010-02-23 21:51:17 +0000487 //
488 // // later, the following will be added
489 //
490 // struct {
491 // void (*copyHelper)();
492 // void (*copyHelper)();
493 // } helpers; // !!! optional
494 //
495 // const char *signature; // the block signature
496 // const char *layout; // reserved
Mike Stumpab695142009-02-13 15:16:56 +0000497 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000498 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(),
499 UnsignedLongTy,
Mike Stumpa5448542009-02-13 15:32:32 +0000500 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000501 NULL);
502
503 getModule().addTypeName("struct.__block_descriptor",
504 BlockDescriptorType);
505
506 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000507}
508
Mike Stump2a998142009-03-04 18:17:45 +0000509const llvm::Type *BlockModule::getGenericBlockLiteralType() {
Mike Stump9b8a7972009-02-13 15:25:34 +0000510 if (GenericBlockLiteralType)
511 return GenericBlockLiteralType;
512
Mike Stumpa5448542009-02-13 15:32:32 +0000513 const llvm::Type *BlockDescPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000514 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000515
Mike Stump7cbb3602009-02-13 16:01:35 +0000516 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
517 getTypes().ConvertType(getContext().IntTy));
518
Mike Stump9b8a7972009-02-13 15:25:34 +0000519 // struct __block_literal_generic {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000520 // void *__isa;
521 // int __flags;
522 // int __reserved;
523 // void (*__invoke)(void *);
524 // struct __block_descriptor *__descriptor;
Mike Stump9b8a7972009-02-13 15:25:34 +0000525 // };
Blaine Garst2a7eb282010-02-23 21:51:17 +0000526 GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
Owen Anderson47a434f2009-08-05 23:18:46 +0000527 PtrToInt8Ty,
Mike Stump7cbb3602009-02-13 16:01:35 +0000528 IntTy,
529 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000530 PtrToInt8Ty,
Mike Stump9b8a7972009-02-13 15:25:34 +0000531 BlockDescPtrTy,
532 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000533
Mike Stump9b8a7972009-02-13 15:25:34 +0000534 getModule().addTypeName("struct.__block_literal_generic",
535 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000536
Mike Stump9b8a7972009-02-13 15:25:34 +0000537 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000538}
539
Mike Stumpbd65cac2009-02-19 01:01:04 +0000540
Anders Carlssona1736c02009-12-24 21:13:40 +0000541RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E,
542 ReturnValueSlot ReturnValue) {
Mike Stumpa5448542009-02-13 15:32:32 +0000543 const BlockPointerType *BPT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000544 E->getCallee()->getType()->getAs<BlockPointerType>();
Mike Stumpa5448542009-02-13 15:32:32 +0000545
Anders Carlssonacfde802009-02-12 00:39:25 +0000546 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
547
548 // Get a pointer to the generic block literal.
549 const llvm::Type *BlockLiteralTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000550 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000551
552 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000553 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000554 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
555
556 // Get the function pointer from the literal.
557 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Anders Carlssonacfde802009-02-12 00:39:25 +0000558
Mike Stumpa5448542009-02-13 15:32:32 +0000559 BlockLiteral =
560 Builder.CreateBitCast(BlockLiteral,
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000561 llvm::Type::getInt8PtrTy(VMContext),
Anders Carlssonacfde802009-02-12 00:39:25 +0000562 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000563
Anders Carlssonacfde802009-02-12 00:39:25 +0000564 // Add the block literal.
565 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
566 CallArgList Args;
567 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000568
Anders Carlsson782f3972009-04-08 23:13:16 +0000569 QualType FnType = BPT->getPointeeType();
570
Anders Carlssonacfde802009-02-12 00:39:25 +0000571 // And the rest of the arguments.
John McCall183700f2009-09-21 23:43:11 +0000572 EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(),
Anders Carlsson782f3972009-04-08 23:13:16 +0000573 E->arg_begin(), E->arg_end());
Mike Stumpa5448542009-02-13 15:32:32 +0000574
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000575 // Load the function.
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000576 llvm::Value *Func = Builder.CreateLoad(FuncPtr, "tmp");
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000577
John McCall04a67a62010-02-05 21:31:56 +0000578 const FunctionType *FuncTy = FnType->getAs<FunctionType>();
579 QualType ResultType = FuncTy->getResultType();
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000580
Mike Stump1eb44332009-09-09 15:08:12 +0000581 const CGFunctionInfo &FnInfo =
Rafael Espindola264ba482010-03-30 20:24:48 +0000582 CGM.getTypes().getFunctionInfo(ResultType, Args,
583 FuncTy->getExtInfo());
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000585 // Cast the function pointer to the right type.
Mike Stump1eb44332009-09-09 15:08:12 +0000586 const llvm::Type *BlockFTy =
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000587 CGM.getTypes().GetFunctionType(FnInfo, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000588
Owen Anderson96e0fc72009-07-29 22:16:19 +0000589 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000590 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Anders Carlssonacfde802009-02-12 00:39:25 +0000592 // And call the block.
Anders Carlssona1736c02009-12-24 21:13:40 +0000593 return EmitCall(FnInfo, Func, ReturnValue, Args);
Anders Carlssonacfde802009-02-12 00:39:25 +0000594}
Anders Carlssond5cab542009-02-12 17:55:02 +0000595
John McCallea1471e2010-05-20 01:18:31 +0000596void CodeGenFunction::AllocateBlockCXXThisPointer(const CXXThisExpr *E) {
597 assert(BlockCXXThisOffset.isZero() && "already computed 'this' pointer");
598
599 // Figure out what the offset is.
600 QualType T = E->getType();
601 std::pair<CharUnits,CharUnits> TypeInfo = getContext().getTypeInfoInChars(T);
602 CharUnits Offset = getBlockOffset(TypeInfo.first, TypeInfo.second);
603
604 BlockCXXThisOffset = Offset;
605 BlockLayout.push_back(E);
606}
607
608void CodeGenFunction::AllocateBlockDecl(const BlockDeclRefExpr *E) {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000609 const ValueDecl *VD = E->getDecl();
John McCallea1471e2010-05-20 01:18:31 +0000610 CharUnits &Offset = BlockDecls[VD];
Mike Stumpdab514f2009-03-04 03:23:46 +0000611
Mike Stumpdab514f2009-03-04 03:23:46 +0000612 // See if we have already allocated an offset for this variable.
John McCallea1471e2010-05-20 01:18:31 +0000613 if (!Offset.isZero())
614 return;
Mike Stumpea26cb52009-10-21 03:49:08 +0000615
616 // Don't run the expensive check, unless we have to.
Mike Stump083c25e2009-10-22 00:49:09 +0000617 if (!BlockHasCopyDispose)
618 if (E->isByRef()
619 || BlockRequiresCopying(E->getType()))
620 BlockHasCopyDispose = true;
Mike Stumpea26cb52009-10-21 03:49:08 +0000621
John McCallea1471e2010-05-20 01:18:31 +0000622 const ValueDecl *D = cast<ValueDecl>(E->getDecl());
Mike Stumpea26cb52009-10-21 03:49:08 +0000623
John McCallea1471e2010-05-20 01:18:31 +0000624 CharUnits Size;
625 CharUnits Align;
626
627 if (E->isByRef()) {
628 llvm::tie(Size,Align) =
629 getContext().getTypeInfoInChars(getContext().VoidPtrTy);
630 } else {
631 Size = getContext().getTypeSizeInChars(D->getType());
632 Align = getContext().getDeclAlign(D);
633 }
634
635 Offset = getBlockOffset(Size, Align);
636 BlockLayout.push_back(E);
Mike Stumpea26cb52009-10-21 03:49:08 +0000637}
638
John McCallee504292010-05-21 04:11:14 +0000639llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const ValueDecl *VD,
640 bool IsByRef) {
Fariborz Jahaniandf8b8ea2010-06-04 16:10:00 +0000641
John McCallea1471e2010-05-20 01:18:31 +0000642 CharUnits offset = BlockDecls[VD];
643 assert(!offset.isZero() && "getting address of unallocated decl");
Mike Stumpdab514f2009-03-04 03:23:46 +0000644
645 llvm::Value *BlockLiteral = LoadBlockStruct();
646 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
Chris Lattner77b89b82010-06-27 07:15:29 +0000647 llvm::ConstantInt::get(Int64Ty, offset.getQuantity()),
Mike Stump58a85142009-03-04 22:48:06 +0000648 "block.literal");
John McCallee504292010-05-21 04:11:14 +0000649 if (IsByRef) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000650 const llvm::Type *PtrStructTy
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000651 = llvm::PointerType::get(BuildByRefType(VD), 0);
Mike Stumpa8b60c92009-03-21 21:00:35 +0000652 // The block literal will need a copy/destroy helper.
653 BlockHasCopyDispose = true;
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000654
655 const llvm::Type *Ty = PtrStructTy;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000656 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000657 V = Builder.CreateBitCast(V, Ty);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000658 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000659 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000660 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000661 V = Builder.CreateBitCast(V, PtrStructTy);
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000662 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
663 VD->getNameAsString());
Fariborz Jahaniand33ded52010-05-04 17:59:32 +0000664 if (VD->getType()->isReferenceType())
665 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000666 } else {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000667 const llvm::Type *Ty = CGM.getTypes().ConvertType(VD->getType());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000668 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000669 V = Builder.CreateBitCast(V, Ty);
Fariborz Jahanian79651722010-06-02 21:35:17 +0000670 if (VD->getType()->isReferenceType())
Fariborz Jahaniandf8b8ea2010-06-04 16:10:00 +0000671 V = Builder.CreateLoad(V, "ref.tmp");
Mike Stumpdab514f2009-03-04 03:23:46 +0000672 }
673 return V;
674}
675
Mike Stump67a64482009-02-14 22:16:35 +0000676llvm::Constant *
Mike Stump90a90432009-03-04 18:47:42 +0000677BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000678 // Generate the block descriptor.
679 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000680 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
681 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000682
Blaine Garst2a7eb282010-02-23 21:51:17 +0000683 llvm::Constant *DescriptorFields[4];
Mike Stumpa5448542009-02-13 15:32:32 +0000684
Anders Carlssond5cab542009-02-12 17:55:02 +0000685 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000686 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000687
Anders Carlssond5cab542009-02-12 17:55:02 +0000688 // Block literal size. For global blocks we just use the size of the generic
689 // block literal struct.
Ken Dyck687cc4a2010-01-26 13:48:07 +0000690 CharUnits BlockLiteralSize =
691 CGM.GetTargetTypeStoreSize(getGenericBlockLiteralType());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000692 DescriptorFields[1] =
Ken Dyck199c3d62010-01-11 17:06:35 +0000693 llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize.getQuantity());
Blaine Garst2a7eb282010-02-23 21:51:17 +0000694
695 // signature. non-optional ObjC-style method descriptor @encode sequence
696 std::string BlockTypeEncoding;
697 CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
Mike Stumpa5448542009-02-13 15:32:32 +0000698
Blaine Garst2a7eb282010-02-23 21:51:17 +0000699 DescriptorFields[2] = llvm::ConstantExpr::getBitCast(
700 CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty);
701
702 // layout
703 DescriptorFields[3] =
704 llvm::ConstantInt::get(UnsignedLongTy,0);
705
706 // build the structure from the 4 elements
Mike Stumpa5448542009-02-13 15:32:32 +0000707 llvm::Constant *DescriptorStruct =
Blaine Garst2a7eb282010-02-23 21:51:17 +0000708 llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 4, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000709
Anders Carlssond5cab542009-02-12 17:55:02 +0000710 llvm::GlobalVariable *Descriptor =
Owen Anderson1c431b32009-07-08 19:05:04 +0000711 new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000712 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000713 DescriptorStruct, "__block_descriptor_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000714
David Chisnall5e530af2009-11-17 19:33:30 +0000715 int FieldCount = 5;
Anders Carlssond5cab542009-02-12 17:55:02 +0000716 // Generate the constants for the block literal.
David Chisnall5e530af2009-11-17 19:33:30 +0000717
718 std::vector<llvm::Constant*> LiteralFields(FieldCount);
Mike Stumpa5448542009-02-13 15:32:32 +0000719
John McCallee504292010-05-21 04:11:14 +0000720 CGBlockInfo Info(n);
Mike Stump7f28a9c2009-03-13 23:34:28 +0000721 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000722 llvm::Function *Fn
Fariborz Jahanian564360b2010-06-24 00:08:06 +0000723 = CodeGenFunction(CGM).GenerateBlockFunction(GlobalDecl(), BE, Info, 0, LocalDeclMap);
John McCallee504292010-05-21 04:11:14 +0000724 assert(Info.BlockSize == BlockLiteralSize
Mike Stump4e7a1f72009-02-21 20:00:35 +0000725 && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000726
Anders Carlssond5cab542009-02-12 17:55:02 +0000727 // isa
Mike Stumpf99f1d02009-02-13 17:23:42 +0000728 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000729
Anders Carlssond5cab542009-02-12 17:55:02 +0000730 // Flags
Blaine Garst2a7eb282010-02-23 21:51:17 +0000731 LiteralFields[1] =
Blaine Garsta36e2232010-03-05 01:29:59 +0000732 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE);
Mike Stumpa5448542009-02-13 15:32:32 +0000733
Anders Carlssond5cab542009-02-12 17:55:02 +0000734 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000735 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000736
Anders Carlssond5cab542009-02-12 17:55:02 +0000737 // Function
738 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000739
Anders Carlssond5cab542009-02-12 17:55:02 +0000740 // Descriptor
741 LiteralFields[4] = Descriptor;
David Chisnall5e530af2009-11-17 19:33:30 +0000742
Mike Stumpa5448542009-02-13 15:32:32 +0000743 llvm::Constant *BlockLiteralStruct =
David Chisnall5e530af2009-11-17 19:33:30 +0000744 llvm::ConstantStruct::get(VMContext, LiteralFields, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000745
746 llvm::GlobalVariable *BlockLiteral =
Owen Anderson1c431b32009-07-08 19:05:04 +0000747 new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000748 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000749 BlockLiteralStruct, "__block_literal_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000750
Anders Carlssond5cab542009-02-12 17:55:02 +0000751 return BlockLiteral;
752}
753
Mike Stump4e7a1f72009-02-21 20:00:35 +0000754llvm::Value *CodeGenFunction::LoadBlockStruct() {
Mike Stumpbf1914b2009-10-20 20:30:01 +0000755 llvm::Value *V = Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()],
756 "self");
757 // For now, we codegen based upon byte offsets.
758 return Builder.CreateBitCast(V, PtrToInt8Ty);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000759}
760
Mike Stump00470a12009-03-05 08:32:30 +0000761llvm::Function *
Fariborz Jahanian564360b2010-06-24 00:08:06 +0000762CodeGenFunction::GenerateBlockFunction(GlobalDecl GD, const BlockExpr *BExpr,
John McCallee504292010-05-21 04:11:14 +0000763 CGBlockInfo &Info,
Mike Stump6cc88f72009-03-20 21:53:12 +0000764 const Decl *OuterFuncDecl,
John McCallee504292010-05-21 04:11:14 +0000765 llvm::DenseMap<const Decl*, llvm::Value*> ldm) {
Devang Patel963dfbd2009-04-15 21:51:44 +0000766
767 // Check if we should generate debug info for this block.
768 if (CGM.getDebugInfo())
769 DebugInfo = CGM.getDebugInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Mike Stump7f28a9c2009-03-13 23:34:28 +0000771 // Arrange for local static and local extern declarations to appear
772 // to be local to this function as well, as they are directly referenced
773 // in a block.
774 for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
775 i != ldm.end();
776 ++i) {
777 const VarDecl *VD = dyn_cast<VarDecl>(i->first);
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000779 if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage())
Mike Stump7f28a9c2009-03-13 23:34:28 +0000780 LocalDeclMap[VD] = i->second;
781 }
782
Ken Dyck687cc4a2010-01-26 13:48:07 +0000783 BlockOffset =
784 CGM.GetTargetTypeStoreSize(CGM.getGenericBlockLiteralType());
Ken Dyck30a8a272010-01-26 19:13:33 +0000785 BlockAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Eli Friedman48f91222009-03-28 03:24:54 +0000786
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000787 const FunctionType *BlockFunctionType = BExpr->getFunctionType();
788 QualType ResultType;
Rafael Espindola264ba482010-03-30 20:24:48 +0000789 FunctionType::ExtInfo EInfo = getFunctionExtInfo(*BlockFunctionType);
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000790 bool IsVariadic;
Mike Stump1eb44332009-09-09 15:08:12 +0000791 if (const FunctionProtoType *FTy =
Fariborz Jahanianda0895d2009-04-11 18:54:57 +0000792 dyn_cast<FunctionProtoType>(BlockFunctionType)) {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000793 ResultType = FTy->getResultType();
794 IsVariadic = FTy->isVariadic();
Mike Stumpb3589f42009-07-30 22:28:39 +0000795 } else {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000796 // K&R style block.
797 ResultType = BlockFunctionType->getResultType();
798 IsVariadic = false;
799 }
Mike Stumpa5448542009-02-13 15:32:32 +0000800
Anders Carlssond5cab542009-02-12 17:55:02 +0000801 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000802
Mike Stumpea26cb52009-10-21 03:49:08 +0000803 CurFuncDecl = OuterFuncDecl;
804
Chris Lattner161d36d2009-02-28 19:01:03 +0000805 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlssond5cab542009-02-12 17:55:02 +0000806
Mike Stumpea26cb52009-10-21 03:49:08 +0000807 IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor");
Mike Stumpadaaad32009-10-20 02:12:22 +0000808
John McCallea1471e2010-05-20 01:18:31 +0000809 // Build the block struct now.
John McCallee504292010-05-21 04:11:14 +0000810 AllocateAllBlockDeclRefs(*this, Info);
Mike Stumpea26cb52009-10-21 03:49:08 +0000811
Mike Stump083c25e2009-10-22 00:49:09 +0000812 QualType ParmTy = getContext().getBlockParmType(BlockHasCopyDispose,
John McCallea1471e2010-05-20 01:18:31 +0000813 BlockLayout);
814
Anders Carlssond5cab542009-02-12 17:55:02 +0000815 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000816 ImplicitParamDecl *SelfDecl =
John McCall2888b652010-04-30 21:35:41 +0000817 ImplicitParamDecl::Create(getContext(), const_cast<BlockDecl*>(BD),
Mike Stumpadaaad32009-10-20 02:12:22 +0000818 SourceLocation(), II,
819 ParmTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000820
Anders Carlssond5cab542009-02-12 17:55:02 +0000821 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump4e7a1f72009-02-21 20:00:35 +0000822 BlockStructDecl = SelfDecl;
Mike Stumpa5448542009-02-13 15:32:32 +0000823
Steve Naroffe78b8092009-03-13 16:56:44 +0000824 for (BlockDecl::param_const_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000825 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000826 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000827
828 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +0000829 CGM.getTypes().getFunctionInfo(ResultType, Args, EInfo);
Anders Carlssond5cab542009-02-12 17:55:02 +0000830
Anders Carlssond5cab542009-02-12 17:55:02 +0000831 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000832 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic);
Mike Stumpa5448542009-02-13 15:32:32 +0000833
Douglas Gregor35415f52010-05-25 17:04:15 +0000834 MangleBuffer Name;
Fariborz Jahanian564360b2010-06-24 00:08:06 +0000835 CGM.getMangledName(GD, Name, BD);
Mike Stumpa5448542009-02-13 15:32:32 +0000836 llvm::Function *Fn =
Douglas Gregor6e5d9b02010-05-25 17:12:30 +0000837 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
838 Name.getString(), &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000839
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000840 CGM.SetInternalFunctionAttributes(BD, Fn, FI);
841
Fariborz Jahanian16ac5ce2010-06-28 18:58:34 +0000842 QualType FnType(BlockFunctionType, 0);
Fariborz Jahanianef160b42010-06-28 19:42:10 +0000843 bool HasPrototype = isa<FunctionProtoType>(BlockFunctionType);
Fariborz Jahanian16ac5ce2010-06-28 18:58:34 +0000844
845 IdentifierInfo *ID = &getContext().Idents.get(Name.getString());
846 CurCodeDecl = FunctionDecl::Create(getContext(),
847 getContext().getTranslationUnitDecl(),
848 SourceLocation(), ID, FnType,
849 0,
850 FunctionDecl::Static,
851 FunctionDecl::None,
852 false, HasPrototype);
853
Chris Lattner4863db42009-04-23 07:18:56 +0000854 StartFunction(BD, ResultType, Fn, Args,
Chris Lattner161d36d2009-02-28 19:01:03 +0000855 BExpr->getBody()->getLocEnd());
Mike Stumpb1a6e682009-09-30 02:43:10 +0000856
Chris Lattner4863db42009-04-23 07:18:56 +0000857 CurFuncDecl = OuterFuncDecl;
Mike Stumpb289b3f2009-10-01 22:29:41 +0000858
John McCallea1471e2010-05-20 01:18:31 +0000859 // If we have a C++ 'this' reference, go ahead and force it into
860 // existence now.
861 if (Info.CXXThisRef) {
862 assert(!BlockCXXThisOffset.isZero() &&
863 "haven't yet allocated 'this' reference");
864
865 // TODO: I have a dream that one day this will be typed.
866 llvm::Value *BlockLiteral = LoadBlockStruct();
867 llvm::Value *ThisPtrRaw =
868 Builder.CreateConstInBoundsGEP1_64(BlockLiteral,
869 BlockCXXThisOffset.getQuantity(),
870 "this.ptr.raw");
871
872 const llvm::Type *Ty =
873 CGM.getTypes().ConvertType(Info.CXXThisRef->getType());
874 Ty = llvm::PointerType::get(Ty, 0);
875 llvm::Value *ThisPtr = Builder.CreateBitCast(ThisPtrRaw, Ty, "this.ptr");
876
877 CXXThisValue = Builder.CreateLoad(ThisPtr, "this");
878 }
879
John McCallee504292010-05-21 04:11:14 +0000880 // If we have an Objective C 'self' reference, go ahead and force it
881 // into existence now.
882 if (Info.NeedsObjCSelf) {
883 ValueDecl *Self = cast<ObjCMethodDecl>(CurFuncDecl)->getSelfDecl();
884 LocalDeclMap[Self] = GetAddrOfBlockDecl(Self, false);
885 }
886
Mike Stumpb289b3f2009-10-01 22:29:41 +0000887 // Save a spot to insert the debug information for all the BlockDeclRefDecls.
888 llvm::BasicBlock *entry = Builder.GetInsertBlock();
889 llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint();
890 --entry_ptr;
891
Chris Lattner161d36d2009-02-28 19:01:03 +0000892 EmitStmt(BExpr->getBody());
Mike Stumpb289b3f2009-10-01 22:29:41 +0000893
Mike Stumpde8c5c72009-10-01 00:27:30 +0000894 // Remember where we were...
895 llvm::BasicBlock *resume = Builder.GetInsertBlock();
Mike Stumpb289b3f2009-10-01 22:29:41 +0000896
Mike Stumpde8c5c72009-10-01 00:27:30 +0000897 // Go back to the entry.
Mike Stumpb289b3f2009-10-01 22:29:41 +0000898 ++entry_ptr;
899 Builder.SetInsertPoint(entry, entry_ptr);
900
Mike Stumpb1a6e682009-09-30 02:43:10 +0000901 if (CGDebugInfo *DI = getDebugInfo()) {
Mike Stumpb1a6e682009-09-30 02:43:10 +0000902 // Emit debug information for all the BlockDeclRefDecls.
John McCallea1471e2010-05-20 01:18:31 +0000903 // FIXME: also for 'this'
904 for (unsigned i = 0, e = BlockLayout.size(); i != e; ++i) {
905 if (const BlockDeclRefExpr *BDRE =
906 dyn_cast<BlockDeclRefExpr>(BlockLayout[i])) {
Mike Stumpb1a6e682009-09-30 02:43:10 +0000907 const ValueDecl *D = BDRE->getDecl();
908 DI->setLocation(D->getLocation());
909 DI->EmitDeclareOfBlockDeclRefVariable(BDRE,
910 LocalDeclMap[getBlockStructDecl()],
911 Builder, this);
912 }
913 }
Mike Stumpb1a6e682009-09-30 02:43:10 +0000914 }
Mike Stumpde8c5c72009-10-01 00:27:30 +0000915 // And resume where we left off.
916 if (resume == 0)
917 Builder.ClearInsertionPoint();
918 else
919 Builder.SetInsertPoint(resume);
Mike Stumpb1a6e682009-09-30 02:43:10 +0000920
Chris Lattner161d36d2009-02-28 19:01:03 +0000921 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlssond5cab542009-02-12 17:55:02 +0000922
Mike Stump8a2b4b12009-02-25 23:33:13 +0000923 // The runtime needs a minimum alignment of a void *.
Ken Dyck30a8a272010-01-26 19:13:33 +0000924 CharUnits MinAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Ken Dyck199c3d62010-01-11 17:06:35 +0000925 BlockOffset = CharUnits::fromQuantity(
Ken Dyck30a8a272010-01-26 19:13:33 +0000926 llvm::RoundUpToAlignment(BlockOffset.getQuantity(),
927 MinAlign.getQuantity()));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000928
John McCallee504292010-05-21 04:11:14 +0000929 Info.BlockSize = BlockOffset;
930 Info.BlockAlign = BlockAlign;
931 Info.BlockLayout = BlockLayout;
932 Info.BlockHasCopyDispose = BlockHasCopyDispose;
Anders Carlssond5cab542009-02-12 17:55:02 +0000933 return Fn;
934}
Mike Stumpa99038c2009-02-28 09:07:16 +0000935
John McCallea1471e2010-05-20 01:18:31 +0000936CharUnits BlockFunction::getBlockOffset(CharUnits Size, CharUnits Align) {
937 assert((Align.isPositive()) && "alignment must be 1 byte or more");
Mike Stumpa99038c2009-02-28 09:07:16 +0000938
Ken Dyck199c3d62010-01-11 17:06:35 +0000939 CharUnits OldOffset = BlockOffset;
Mike Stumpa99038c2009-02-28 09:07:16 +0000940
941 // Ensure proper alignment, even if it means we have to have a gap
Ken Dyck199c3d62010-01-11 17:06:35 +0000942 BlockOffset = CharUnits::fromQuantity(
Ken Dyck30a8a272010-01-26 19:13:33 +0000943 llvm::RoundUpToAlignment(BlockOffset.getQuantity(), Align.getQuantity()));
Mike Stumpa99038c2009-02-28 09:07:16 +0000944 BlockAlign = std::max(Align, BlockAlign);
Mike Stump00470a12009-03-05 08:32:30 +0000945
Ken Dyck199c3d62010-01-11 17:06:35 +0000946 CharUnits Pad = BlockOffset - OldOffset;
947 if (Pad.isPositive()) {
Mike Stumpa99038c2009-02-28 09:07:16 +0000948 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
Ken Dyck199c3d62010-01-11 17:06:35 +0000949 llvm::APInt(32,
950 Pad.getQuantity()),
Mike Stumpa99038c2009-02-28 09:07:16 +0000951 ArrayType::Normal, 0);
Douglas Gregorba55ec22010-05-11 18:17:16 +0000952 ValueDecl *PadDecl = VarDecl::Create(getContext(),
953 getContext().getTranslationUnitDecl(),
954 SourceLocation(),
Douglas Gregor16573fa2010-04-19 22:54:31 +0000955 0, QualType(PadTy), 0,
956 VarDecl::None, VarDecl::None);
John McCallea1471e2010-05-20 01:18:31 +0000957 Expr *E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
958 SourceLocation());
959 BlockLayout.push_back(E);
Mike Stumpa99038c2009-02-28 09:07:16 +0000960 }
Mike Stumpa99038c2009-02-28 09:07:16 +0000961
962 BlockOffset += Size;
John McCallea1471e2010-05-20 01:18:31 +0000963 return BlockOffset - Size;
Mike Stumpa99038c2009-02-28 09:07:16 +0000964}
Mike Stumpdab514f2009-03-04 03:23:46 +0000965
Mike Stump08920992009-03-07 02:35:30 +0000966llvm::Constant *BlockFunction::
967GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000968 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000969 QualType R = getContext().VoidTy;
970
971 FunctionArgList Args;
972 // FIXME: This leaks
Mike Stump08920992009-03-07 02:35:30 +0000973 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +0000974 ImplicitParamDecl::Create(getContext(), 0,
975 SourceLocation(), 0,
Mike Stump08920992009-03-07 02:35:30 +0000976 getContext().getPointerType(getContext().VoidTy));
977 Args.push_back(std::make_pair(Dst, Dst->getType()));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000978 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +0000979 ImplicitParamDecl::Create(getContext(), 0,
980 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000981 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000982 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Mike Stumpa4f668f2009-03-06 01:33:24 +0000984 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +0000985 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000986
Mike Stump3899a7f2009-06-05 23:26:36 +0000987 // FIXME: We'd like to put these into a mergable by content, with
988 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000989 CodeGenTypes &Types = CGM.getTypes();
990 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
991
992 llvm::Function *Fn =
993 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000994 "__copy_helper_block_", &CGM.getModule());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000995
996 IdentifierInfo *II
997 = &CGM.getContext().Idents.get("__copy_helper_block_");
998
999 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1000 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001001 SourceLocation(), II, R, 0,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001002 FunctionDecl::Static,
1003 FunctionDecl::None,
1004 false,
Mike Stumpa4f668f2009-03-06 01:33:24 +00001005 true);
1006 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump08920992009-03-07 02:35:30 +00001007
1008 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
1009 llvm::Type *PtrPtrT;
Mike Stump08920992009-03-07 02:35:30 +00001010
Mike Stumpb7477cf2009-04-10 18:52:28 +00001011 if (NoteForHelperp) {
1012 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump08920992009-03-07 02:35:30 +00001013
Owen Anderson96e0fc72009-07-29 22:16:19 +00001014 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +00001015 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
1016 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump08920992009-03-07 02:35:30 +00001017
Mike Stumpb7477cf2009-04-10 18:52:28 +00001018 llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
Mike Stumpc2f4c342009-04-15 22:11:36 +00001019 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +00001020 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpc2f4c342009-04-15 22:11:36 +00001021 DstObj = Builder.CreateBitCast(DstObj, PtrPtrT);
1022 DstObj = Builder.CreateLoad(DstObj);
Mike Stump08920992009-03-07 02:35:30 +00001023
Mike Stumpb7477cf2009-04-10 18:52:28 +00001024 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
1025 int flag = NoteForHelper[i].flag;
1026 int index = NoteForHelper[i].index;
Mike Stump08920992009-03-07 02:35:30 +00001027
Mike Stumpb7477cf2009-04-10 18:52:28 +00001028 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
1029 || NoteForHelper[i].RequiresCopying) {
1030 llvm::Value *Srcv = SrcObj;
1031 Srcv = Builder.CreateStructGEP(Srcv, index);
1032 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001033 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +00001034 Srcv = Builder.CreateLoad(Srcv);
1035
1036 llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
1037 Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
1038
Chris Lattner77b89b82010-06-27 07:15:29 +00001039 llvm::Value *N = llvm::ConstantInt::get(CGF.Int32Ty, flag);
Mike Stumpb7477cf2009-04-10 18:52:28 +00001040 llvm::Value *F = getBlockObjectAssign();
1041 Builder.CreateCall3(F, Dstv, Srcv, N);
1042 }
Mike Stump08920992009-03-07 02:35:30 +00001043 }
1044 }
1045
Mike Stumpa4f668f2009-03-06 01:33:24 +00001046 CGF.FinishFunction();
1047
Owen Anderson3c4972d2009-07-29 18:54:39 +00001048 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +00001049}
1050
Mike Stumpcf62d392009-03-06 18:42:23 +00001051llvm::Constant *BlockFunction::
Mike Stump08920992009-03-07 02:35:30 +00001052GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
1053 const llvm::StructType* T,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001054 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +00001055 QualType R = getContext().VoidTy;
1056
1057 FunctionArgList Args;
1058 // FIXME: This leaks
1059 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001060 ImplicitParamDecl::Create(getContext(), 0,
1061 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +00001062 getContext().getPointerType(getContext().VoidTy));
1063
1064 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001065
Mike Stumpa4f668f2009-03-06 01:33:24 +00001066 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +00001067 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stumpa4f668f2009-03-06 01:33:24 +00001068
Mike Stump3899a7f2009-06-05 23:26:36 +00001069 // FIXME: We'd like to put these into a mergable by content, with
1070 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +00001071 CodeGenTypes &Types = CGM.getTypes();
1072 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1073
1074 llvm::Function *Fn =
1075 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001076 "__destroy_helper_block_", &CGM.getModule());
Mike Stumpa4f668f2009-03-06 01:33:24 +00001077
1078 IdentifierInfo *II
1079 = &CGM.getContext().Idents.get("__destroy_helper_block_");
1080
1081 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1082 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001083 SourceLocation(), II, R, 0,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001084 FunctionDecl::Static,
1085 FunctionDecl::None,
1086 false, true);
Mike Stumpa4f668f2009-03-06 01:33:24 +00001087 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1edf6b62009-03-07 02:53:18 +00001088
Mike Stumpb7477cf2009-04-10 18:52:28 +00001089 if (NoteForHelperp) {
1090 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump1edf6b62009-03-07 02:53:18 +00001091
Mike Stumpb7477cf2009-04-10 18:52:28 +00001092 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
1093 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +00001094 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +00001095 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
1096 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump1edf6b62009-03-07 02:53:18 +00001097
Mike Stumpb7477cf2009-04-10 18:52:28 +00001098 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
1099 int flag = NoteForHelper[i].flag;
1100 int index = NoteForHelper[i].index;
Mike Stump1edf6b62009-03-07 02:53:18 +00001101
Mike Stumpb7477cf2009-04-10 18:52:28 +00001102 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
1103 || NoteForHelper[i].RequiresCopying) {
1104 llvm::Value *Srcv = SrcObj;
1105 Srcv = Builder.CreateStructGEP(Srcv, index);
1106 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001107 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +00001108 Srcv = Builder.CreateLoad(Srcv);
1109
1110 BuildBlockRelease(Srcv, flag);
1111 }
Mike Stump1edf6b62009-03-07 02:53:18 +00001112 }
1113 }
1114
Mike Stumpa4f668f2009-03-06 01:33:24 +00001115 CGF.FinishFunction();
1116
Owen Anderson3c4972d2009-07-29 18:54:39 +00001117 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpa4f668f2009-03-06 01:33:24 +00001118}
1119
Mike Stump08920992009-03-07 02:35:30 +00001120llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001121 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump08920992009-03-07 02:35:30 +00001122 return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
1123 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) {
Mike Stump08920992009-03-07 02:35:30 +00001128 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001129 T, NoteForHelperp);
Mike Stumpdab514f2009-03-04 03:23:46 +00001130}
Mike Stump797b6322009-03-05 01:23:13 +00001131
Mike Stumpee094222009-03-06 06:12:24 +00001132llvm::Constant *BlockFunction::
1133GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
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,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001161 "__Block_byref_id_object_copy_", &CGM.getModule());
Mike Stump45031c02009-03-06 02:29:21 +00001162
1163 IdentifierInfo *II
1164 = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
1165
1166 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1167 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001168 SourceLocation(), II, R, 0,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001169 FunctionDecl::Static,
1170 FunctionDecl::None,
1171 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");
1179 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
1180
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");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001186 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpee094222009-03-06 06:12:24 +00001187 llvm::Value *SrcObj = Builder.CreateLoad(V);
Mike Stump1eb44332009-09-09 15:08:12 +00001188
Mike Stumpee094222009-03-06 06:12:24 +00001189 flag |= BLOCK_BYREF_CALLER;
1190
Chris Lattner77b89b82010-06-27 07:15:29 +00001191 llvm::Value *N = llvm::ConstantInt::get(CGF.Int32Ty, flag);
Mike Stumpee094222009-03-06 06:12:24 +00001192 llvm::Value *F = getBlockObjectAssign();
1193 Builder.CreateCall3(F, DstObj, SrcObj, N);
1194
Mike Stump45031c02009-03-06 02:29:21 +00001195 CGF.FinishFunction();
1196
Owen Anderson3c4972d2009-07-29 18:54:39 +00001197 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001198}
1199
Mike Stump1851b682009-03-06 04:53:30 +00001200llvm::Constant *
1201BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
1202 int flag) {
Mike Stump45031c02009-03-06 02:29:21 +00001203 QualType R = getContext().VoidTy;
1204
1205 FunctionArgList Args;
1206 // FIXME: This leaks
1207 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001208 ImplicitParamDecl::Create(getContext(), 0,
1209 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001210 getContext().getPointerType(getContext().VoidTy));
1211
1212 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001213
Mike Stump45031c02009-03-06 02:29:21 +00001214 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +00001215 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stump45031c02009-03-06 02:29:21 +00001216
Mike Stump45031c02009-03-06 02:29:21 +00001217 CodeGenTypes &Types = CGM.getTypes();
1218 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1219
Mike Stump3899a7f2009-06-05 23:26:36 +00001220 // FIXME: We'd like to put these into a mergable by content, with
1221 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001222 llvm::Function *Fn =
1223 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001224 "__Block_byref_id_object_dispose_",
Mike Stump45031c02009-03-06 02:29:21 +00001225 &CGM.getModule());
1226
1227 IdentifierInfo *II
1228 = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
1229
1230 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1231 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001232 SourceLocation(), II, R, 0,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001233 FunctionDecl::Static,
1234 FunctionDecl::None,
1235 false, true);
Mike Stump45031c02009-03-06 02:29:21 +00001236 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1851b682009-03-06 04:53:30 +00001237
1238 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001239 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001240 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001241 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001242 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001243 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001244
Mike Stump1851b682009-03-06 04:53:30 +00001245 flag |= BLOCK_BYREF_CALLER;
1246 BuildBlockRelease(V, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001247 CGF.FinishFunction();
1248
Owen Anderson3c4972d2009-07-29 18:54:39 +00001249 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001250}
1251
Mike Stumpee094222009-03-06 06:12:24 +00001252llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001253 int Flag, unsigned Align) {
1254 // All alignments below that of pointer alignment collapse down to just
Mike Stump3899a7f2009-06-05 23:26:36 +00001255 // pointer alignment, as we always have at least that much alignment to begin
1256 // with.
1257 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001258
Mike Stump3899a7f2009-06-05 23:26:36 +00001259 // As an optimization, we only generate a single function of each kind we
1260 // might need. We need a different one for each alignment and for each
1261 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001262 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1263 llvm::Constant *&Entry = CGM.AssignCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001264 if (Entry)
1265 return Entry;
Chris Lattner10976d92009-12-05 08:21:30 +00001266 return Entry = CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, Flag);
Mike Stump45031c02009-03-06 02:29:21 +00001267}
1268
Mike Stump1851b682009-03-06 04:53:30 +00001269llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001270 int Flag,
Mike Stump3899a7f2009-06-05 23:26:36 +00001271 unsigned Align) {
1272 // All alignments below that of pointer alignment collpase down to just
1273 // pointer alignment, as we always have at least that much alignment to begin
1274 // with.
1275 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001276
Mike Stump3899a7f2009-06-05 23:26:36 +00001277 // As an optimization, we only generate a single function of each kind we
1278 // might need. We need a different one for each alignment and for each
1279 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001280 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1281 llvm::Constant *&Entry = CGM.DestroyCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001282 if (Entry)
1283 return Entry;
Chris Lattner10976d92009-12-05 08:21:30 +00001284 return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, Flag);
Mike Stump45031c02009-03-06 02:29:21 +00001285}
1286
Mike Stump797b6322009-03-05 01:23:13 +00001287llvm::Value *BlockFunction::getBlockObjectDispose() {
1288 if (CGM.BlockObjectDispose == 0) {
1289 const llvm::FunctionType *FTy;
1290 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001291 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stump797b6322009-03-05 01:23:13 +00001292 ArgTys.push_back(PtrToInt8Ty);
Chris Lattner77b89b82010-06-27 07:15:29 +00001293 ArgTys.push_back(CGF.Int32Ty);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001294 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stump00470a12009-03-05 08:32:30 +00001295 CGM.BlockObjectDispose
Mike Stump797b6322009-03-05 01:23:13 +00001296 = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
1297 }
1298 return CGM.BlockObjectDispose;
1299}
1300
Mike Stumpee094222009-03-06 06:12:24 +00001301llvm::Value *BlockFunction::getBlockObjectAssign() {
1302 if (CGM.BlockObjectAssign == 0) {
1303 const llvm::FunctionType *FTy;
1304 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001305 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stumpee094222009-03-06 06:12:24 +00001306 ArgTys.push_back(PtrToInt8Ty);
1307 ArgTys.push_back(PtrToInt8Ty);
Chris Lattner77b89b82010-06-27 07:15:29 +00001308 ArgTys.push_back(CGF.Int32Ty);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001309 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stumpee094222009-03-06 06:12:24 +00001310 CGM.BlockObjectAssign
1311 = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
1312 }
1313 return CGM.BlockObjectAssign;
1314}
1315
Mike Stump1851b682009-03-06 04:53:30 +00001316void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Mike Stump797b6322009-03-05 01:23:13 +00001317 llvm::Value *F = getBlockObjectDispose();
Mike Stump1851b682009-03-06 04:53:30 +00001318 llvm::Value *N;
Mike Stump797b6322009-03-05 01:23:13 +00001319 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Chris Lattner77b89b82010-06-27 07:15:29 +00001320 N = llvm::ConstantInt::get(CGF.Int32Ty, flag);
Mike Stump797b6322009-03-05 01:23:13 +00001321 Builder.CreateCall2(F, V, N);
1322}
Mike Stump00470a12009-03-05 08:32:30 +00001323
1324ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stump08920992009-03-07 02:35:30 +00001325
1326BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1327 CGBuilderTy &B)
Chris Lattner77b89b82010-06-27 07:15:29 +00001328 : CGM(cgm), VMContext(cgm.getLLVMContext()), CGF(cgf), Builder(B) {
Owen Anderson0032b272009-08-13 21:57:51 +00001329 PtrToInt8Ty = llvm::PointerType::getUnqual(
1330 llvm::Type::getInt8Ty(VMContext));
Mike Stump08920992009-03-07 02:35:30 +00001331
1332 BlockHasCopyDispose = false;
1333}