blob: d5fc993c797d711629c2dc4845df9e6440ebf9fa [file] [log] [blame]
Anders Carlsson2437cbf2009-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 Stump2e722b92009-09-30 02:43:10 +000014#include "CGDebugInfo.h"
Anders Carlsson2437cbf2009-02-12 00:39:25 +000015#include "CodeGenFunction.h"
Fariborz Jahanian989908f2010-02-10 23:34:57 +000016#include "CGObjCRuntime.h"
Anders Carlsson2437cbf2009-02-12 00:39:25 +000017#include "CodeGenModule.h"
Mike Stump692c6e32009-03-20 21:53:12 +000018#include "clang/AST/DeclObjC.h"
Anders Carlsson2437cbf2009-02-12 00:39:25 +000019#include "llvm/Module.h"
Benjamin Kramer9e2e1c92010-03-31 15:04:05 +000020#include "llvm/ADT/SmallSet.h"
Anders Carlsson6a60fa22009-02-12 17:55:02 +000021#include "llvm/Target/TargetData.h"
Anders Carlsson2437cbf2009-02-12 00:39:25 +000022#include <algorithm>
Torok Edwindb714922009-08-24 13:25:12 +000023
Anders Carlsson2437cbf2009-02-12 00:39:25 +000024using namespace clang;
25using namespace CodeGen;
26
John McCall9d42f0f2010-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 Stumpd6ef62f2009-03-06 18:42:23 +000066llvm::Constant *CodeGenFunction::
Blaine Garstfc83aa02010-02-23 21:51:17 +000067BuildDescriptorBlockDecl(const BlockExpr *BE, bool BlockHasCopyDispose, CharUnits Size,
Mike Stumpd2142cf2009-03-25 17:58:24 +000068 const llvm::StructType* Ty,
Mike Stumpaeb0ffd2009-03-07 02:35:30 +000069 std::vector<HelperInfo> *NoteForHelper) {
Mike Stumpc2c38332009-02-13 16:55:51 +000070 const llvm::Type *UnsignedLongTy
71 = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stump85284ba2009-02-13 16:19:19 +000072 llvm::Constant *C;
73 std::vector<llvm::Constant*> Elts;
74
75 // reserved
Owen Andersonb7a2fe62009-07-24 23:12:58 +000076 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stump85284ba2009-02-13 16:19:19 +000077 Elts.push_back(C);
78
79 // Size
Mike Stump2ac40a92009-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 Dyck40775002010-01-11 17:06:35 +000083 C = llvm::ConstantInt::get(UnsignedLongTy, Size.getQuantity());
Mike Stump85284ba2009-02-13 16:19:19 +000084 Elts.push_back(C);
85
Blaine Garstfc83aa02010-02-23 21:51:17 +000086 // optional copy/dispose helpers
Mike Stump85284ba2009-02-13 16:19:19 +000087 if (BlockHasCopyDispose) {
88 // copy_func_helper_decl
Mike Stump67645932009-04-10 18:52:28 +000089 Elts.push_back(BuildCopyHelper(Ty, NoteForHelper));
Mike Stump85284ba2009-02-13 16:19:19 +000090
91 // destroy_func_decl
Mike Stump67645932009-04-10 18:52:28 +000092 Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper));
Mike Stump85284ba2009-02-13 16:19:19 +000093 }
94
Blaine Garstfc83aa02010-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 Lewycky41eaf0a2009-09-19 20:00:52 +0000106 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stump85284ba2009-02-13 16:19:19 +0000107
Owen Andersonc10c8d32009-07-08 19:05:04 +0000108 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Mike Stump85284ba2009-02-13 16:19:19 +0000109 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +0000110 C, "__block_descriptor_tmp");
Mike Stump85284ba2009-02-13 16:19:19 +0000111 return C;
112}
113
Mike Stump95435672009-03-04 18:17:45 +0000114llvm::Constant *BlockModule::getNSConcreteGlobalBlock() {
Chris Lattner46813bb2009-05-13 02:50:56 +0000115 if (NSConcreteGlobalBlock == 0)
Mike Stump11289f42009-09-09 15:08:12 +0000116 NSConcreteGlobalBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
Chris Lattner46813bb2009-05-13 02:50:56 +0000117 "_NSConcreteGlobalBlock");
Mike Stump971f9b62009-02-13 17:23:42 +0000118 return NSConcreteGlobalBlock;
119}
120
Mike Stump95435672009-03-04 18:17:45 +0000121llvm::Constant *BlockModule::getNSConcreteStackBlock() {
Chris Lattner46813bb2009-05-13 02:50:56 +0000122 if (NSConcreteStackBlock == 0)
Mike Stump11289f42009-09-09 15:08:12 +0000123 NSConcreteStackBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
Chris Lattner46813bb2009-05-13 02:50:56 +0000124 "_NSConcreteStackBlock");
Mike Stump7ab278d2009-02-13 19:29:27 +0000125 return NSConcreteStackBlock;
126}
127
John McCall9d42f0f2010-05-21 04:11:14 +0000128static void CollectBlockDeclRefInfo(const Stmt *S, CGBlockInfo &Info) {
Anders Carlssoned5e69f2009-03-01 01:09:12 +0000129 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
130 I != E; ++I)
Daniel Dunbardd942712009-03-02 07:00:57 +0000131 if (*I)
John McCall9d42f0f2010-05-21 04:11:14 +0000132 CollectBlockDeclRefInfo(*I, Info);
Mike Stump4446dcf2009-03-05 08:32:30 +0000133
Mike Stump7fe9cc12009-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 Stump066b6162009-10-21 22:01:24 +0000136 if (const BlockExpr *BE = dyn_cast<BlockExpr>(S)) {
John McCall9d42f0f2010-05-21 04:11:14 +0000137 Info.InnerBlocks.insert(BE->getBlockDecl());
138 CollectBlockDeclRefInfo(BE->getBody(), Info);
Mike Stump066b6162009-10-21 22:01:24 +0000139 }
Mike Stump7fe9cc12009-10-21 03:49:08 +0000140
John McCall9d42f0f2010-05-21 04:11:14 +0000141 else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) {
142 const ValueDecl *D = BDRE->getDecl();
Anders Carlssoned5e69f2009-03-01 01:09:12 +0000143 // FIXME: Handle enums.
John McCall9d42f0f2010-05-21 04:11:14 +0000144 if (isa<FunctionDecl>(D))
Anders Carlssoned5e69f2009-03-01 01:09:12 +0000145 return;
Mike Stump4446dcf2009-03-05 08:32:30 +0000146
John McCall9d42f0f2010-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 Stump066b6162009-10-21 22:01:24 +0000154 // Only Decls that escape are added.
John McCall9d42f0f2010-05-21 04:11:14 +0000155 if (!Info.InnerBlocks.count(D->getDeclContext()))
Mike Stump066b6162009-10-21 22:01:24 +0000156 Info.DeclRefs.push_back(BDRE);
Anders Carlssoned5e69f2009-03-01 01:09:12 +0000157 }
John McCall87fe5d52010-05-20 01:18:31 +0000158
John McCall9d42f0f2010-05-21 04:11:14 +0000159 // Make sure to capture implicit 'self' references due to super calls.
Chandler Carruth7ea01c32010-05-21 10:29:28 +0000160 else if (const ObjCMessageExpr *E = dyn_cast<ObjCMessageExpr>(S)) {
John McCall9d42f0f2010-05-21 04:11:14 +0000161 if (E->getReceiverKind() == ObjCMessageExpr::SuperClass ||
162 E->getReceiverKind() == ObjCMessageExpr::SuperInstance)
163 Info.NeedsObjCSelf = true;
Chandler Carruth7ea01c32010-05-21 10:29:28 +0000164 }
John McCall9d42f0f2010-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 McCall87fe5d52010-05-20 01:18:31 +0000172 Info.CXXThisRef = cast<CXXThisExpr>(S);
Anders Carlssoned5e69f2009-03-01 01:09:12 +0000173}
174
John McCall9d42f0f2010-05-21 04:11:14 +0000175/// CanBlockBeGlobal - Given a CGBlockInfo struct, determines if a block can be
Mike Stump2d33d632009-03-04 22:48:06 +0000176/// declared as a global variable instead of on the stack.
John McCall9d42f0f2010-05-21 04:11:14 +0000177static bool CanBlockBeGlobal(const CGBlockInfo &Info) {
Mike Stump7fe9cc12009-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 McCall9d42f0f2010-05-21 04:11:14 +0000184static void AllocateAllBlockDeclRefs(CodeGenFunction &CGF, CGBlockInfo &Info) {
John McCall87fe5d52010-05-20 01:18:31 +0000185 if (Info.CXXThisRef)
John McCall9d42f0f2010-05-21 04:11:14 +0000186 CGF.AllocateBlockCXXThisPointer(Info.CXXThisRef);
Mike Stump7fe9cc12009-10-21 03:49:08 +0000187
188 for (size_t i = 0; i < Info.DeclRefs.size(); ++i)
John McCall9d42f0f2010-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 Carlssoned5e69f2009-03-01 01:09:12 +0000199}
200
Mike Stump2d33d632009-03-04 22:48:06 +0000201// FIXME: Push most into CGM, passing down a few bits, like current function
202// name.
Mike Stumpb750d922009-02-25 23:33:13 +0000203llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Anders Carlssoned5e69f2009-03-01 01:09:12 +0000204 std::string Name = CurFn->getName();
John McCall9d42f0f2010-05-21 04:11:14 +0000205 CGBlockInfo Info(Name.c_str());
206 Info.InnerBlocks.insert(BE->getBlockDecl());
207 CollectBlockDeclRefInfo(BE->getBody(), Info);
Anders Carlssoned5e69f2009-03-01 01:09:12 +0000208
209 // Check if the block can be global.
Mike Stump2d33d632009-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 Stump97d01d52009-03-04 03:23:46 +0000213 if (0 && CanBlockBeGlobal(Info))
Anders Carlssoned5e69f2009-03-01 01:09:12 +0000214 return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
Mike Stump4446dcf2009-03-05 08:32:30 +0000215
David Chisnall950a9512009-11-17 19:33:30 +0000216 size_t BlockFields = 5;
217
David Chisnall950a9512009-11-17 19:33:30 +0000218 std::vector<llvm::Constant*> Elts(BlockFields);
219
Mike Stump85284ba2009-02-13 16:19:19 +0000220 llvm::Constant *C;
Mike Stumpb750d922009-02-25 23:33:13 +0000221 llvm::Value *V;
Mike Stump85284ba2009-02-13 16:19:19 +0000222
Mike Stump85284ba2009-02-13 16:19:19 +0000223 {
224 // C = BuildBlockStructInitlist();
Blaine Garstf27ab712010-03-05 01:29:59 +0000225 unsigned int flags = BLOCK_HAS_SIGNATURE;
David Chisnall950a9512009-11-17 19:33:30 +0000226
Mike Stump4446dcf2009-03-05 08:32:30 +0000227 // We run this first so that we set BlockHasCopyDispose from the entire
228 // block literal.
229 // __invoke
Mike Stump4446dcf2009-03-05 08:32:30 +0000230 llvm::Function *Fn
Fariborz Jahanian9b5528d2010-06-24 00:08:06 +0000231 = CodeGenFunction(CGM).GenerateBlockFunction(CurGD, BE, Info, CurFuncDecl,
John McCall9d42f0f2010-05-21 04:11:14 +0000232 LocalDeclMap);
233 BlockHasCopyDispose |= Info.BlockHasCopyDispose;
Mike Stump4446dcf2009-03-05 08:32:30 +0000234 Elts[3] = Fn;
235
Mike Stump18bb9282009-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 McCall9d42f0f2010-05-21 04:11:14 +0000238 if (Info.BlockHasCopyDispose)
Mike Stump85284ba2009-02-13 16:19:19 +0000239 flags |= BLOCK_HAS_COPY_DISPOSE;
240
Mike Stump499ae7e2009-02-13 20:17:16 +0000241 // __isa
Mike Stump7ab278d2009-02-13 19:29:27 +0000242 C = CGM.getNSConcreteStackBlock();
Owen Andersonade90fd2009-07-29 18:54:39 +0000243 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
Mike Stump4446dcf2009-03-05 08:32:30 +0000244 Elts[0] = C;
Mike Stump85284ba2009-02-13 16:19:19 +0000245
246 // __flags
Blaine Garstf27ab712010-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 Espindolac50c27c2010-03-30 20:24:48 +0000255 FunctionType::ExtInfo());
Blaine Garstf27ab712010-03-05 01:29:59 +0000256 if (CGM.ReturnTypeUsesSret(FnInfo))
257 flags |= BLOCK_USE_STRET;
258 }
Mike Stump85284ba2009-02-13 16:19:19 +0000259 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
260 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000261 C = llvm::ConstantInt::get(IntTy, flags);
Mike Stump4446dcf2009-03-05 08:32:30 +0000262 Elts[1] = C;
Mike Stump85284ba2009-02-13 16:19:19 +0000263
264 // __reserved
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000265 C = llvm::ConstantInt::get(IntTy, 0);
Mike Stump4446dcf2009-03-05 08:32:30 +0000266 Elts[2] = C;
Mike Stump85284ba2009-02-13 16:19:19 +0000267
John McCall9d42f0f2010-05-21 04:11:14 +0000268 if (Info.BlockLayout.empty()) {
Mike Stumpd6ef62f2009-03-06 18:42:23 +0000269 // __descriptor
John McCall9d42f0f2010-05-21 04:11:14 +0000270 Elts[4] = BuildDescriptorBlockDecl(BE, Info.BlockHasCopyDispose,
271 Info.BlockSize, 0, 0);
Mike Stumpd6ef62f2009-03-06 18:42:23 +0000272
Mike Stump3a139f32009-03-01 20:07:53 +0000273 // Optimize to being a global block.
274 Elts[0] = CGM.getNSConcreteGlobalBlock();
Blaine Garstf27ab712010-03-05 01:29:59 +0000275
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000276 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
Mike Stump3a139f32009-03-01 20:07:53 +0000277
Nick Lewycky41eaf0a2009-09-19 20:00:52 +0000278 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stumpb750d922009-02-25 23:33:13 +0000279
Owen Andersonc10c8d32009-07-08 19:05:04 +0000280 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Benjamin Kramerd6b28fc2010-01-22 13:59:13 +0000281 llvm::GlobalValue::InternalLinkage, C,
282 "__block_holder_tmp_" +
283 llvm::Twine(CGM.getGlobalUniqueCount()));
Mike Stumpb750d922009-02-25 23:33:13 +0000284 QualType BPT = BE->getType();
Owen Andersonade90fd2009-07-29 18:54:39 +0000285 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
Mike Stumpb750d922009-02-25 23:33:13 +0000286 return C;
287 }
Mike Stump4446dcf2009-03-05 08:32:30 +0000288
John McCall9d42f0f2010-05-21 04:11:14 +0000289 std::vector<const llvm::Type *> Types(BlockFields+Info.BlockLayout.size());
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000290 for (int i=0; i<4; ++i)
Mike Stumpb750d922009-02-25 23:33:13 +0000291 Types[i] = Elts[i]->getType();
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000292 Types[4] = PtrToInt8Ty;
Mike Stumpb750d922009-02-25 23:33:13 +0000293
John McCall9d42f0f2010-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 Stump1db7d042009-02-28 09:07:16 +0000296 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
297 QualType Ty = E->getType();
Mike Stump97d01d52009-03-04 03:23:46 +0000298 if (BDRE && BDRE->isByRef()) {
Fariborz Jahaniand1b378e2010-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 Chisnall950a9512009-11-17 19:33:30 +0000304 Types[i+BlockFields] = ConvertType(Ty);
Mike Stump1db7d042009-02-28 09:07:16 +0000305 }
Mike Stumpb750d922009-02-25 23:33:13 +0000306
Owen Anderson758428f2009-08-05 23:18:46 +0000307 llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true);
Mike Stumpb750d922009-02-25 23:33:13 +0000308
309 llvm::AllocaInst *A = CreateTempAlloca(Ty);
John McCall9d42f0f2010-05-21 04:11:14 +0000310 A->setAlignment(Info.BlockAlign.getQuantity());
Mike Stumpb750d922009-02-25 23:33:13 +0000311 V = A;
312
John McCall87fe5d52010-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 McCall9d42f0f2010-05-21 04:11:14 +0000315 std::vector<HelperInfo> NoteForHelper(Info.BlockLayout.size());
John McCall87fe5d52010-05-20 01:18:31 +0000316 unsigned NumHelpers = 0;
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000317
318 for (unsigned i=0; i<4; ++i)
Mike Stumpb750d922009-02-25 23:33:13 +0000319 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
Mike Stump4446dcf2009-03-05 08:32:30 +0000320
John McCall9d42f0f2010-05-21 04:11:14 +0000321 for (unsigned i=0; i < Info.BlockLayout.size(); ++i) {
322 const Expr *E = Info.BlockLayout[i];
Mike Stumpb750d922009-02-25 23:33:13 +0000323
John McCall87fe5d52010-05-20 01:18:31 +0000324 // Skip padding.
325 if (isa<DeclRefExpr>(E)) continue;
Mike Stump1db7d042009-02-28 09:07:16 +0000326
John McCall87fe5d52010-05-20 01:18:31 +0000327 llvm::Value* Addr = Builder.CreateStructGEP(V, i+BlockFields, "tmp");
328 HelperInfo &Note = NoteForHelper[NumHelpers++];
Mike Stump1db7d042009-02-28 09:07:16 +0000329
John McCall87fe5d52010-05-20 01:18:31 +0000330 Note.index = i+5;
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000331
John McCall87fe5d52010-05-20 01:18:31 +0000332 if (isa<CXXThisExpr>(E)) {
333 Note.RequiresCopying = false;
334 Note.flag = BLOCK_FIELD_IS_OBJECT;
Mike Stump1db7d042009-02-28 09:07:16 +0000335
John McCall87fe5d52010-05-20 01:18:31 +0000336 Builder.CreateStore(LoadCXXThis(), Addr);
337 continue;
Mike Stumpb750d922009-02-25 23:33:13 +0000338 }
John McCall87fe5d52010-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 Jahanian3b836182010-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()) {
375 const CXXDestructorDecl *D =
376 ClassDecl->getDestructor(getContext());
377 assert(D && "BuildBlockLiteralTmp - destructor is nul");
378 {
379 // Normal destruction.
380 DelayedCleanupBlock Scope(*this);
381 EmitCXXDestructorCall(D, Dtor_Complete,
382 /*ForVirtualBase=*/false, Addr);
383 // Make sure to jump to the exit block.
384 EmitBranch(Scope.getCleanupExitBlock());
385 }
386 if (Exceptions) {
387 EHCleanupBlock Cleanup(*this);
388 EmitCXXDestructorCall(D, Dtor_Complete,
389 /*ForVirtualBase=*/false, Addr);
390 }
391 }
392 }
393 }
Fariborz Jahanianea882cd2010-06-04 21:35:44 +0000394 else {
395 E = new (getContext()) DeclRefExpr(const_cast<ValueDecl*>(VD),
Fariborz Jahaniand1b378e2010-06-02 21:35:17 +0000396 VD->getType().getNonReferenceType(),
397 SourceLocation());
Fariborz Jahanianea882cd2010-06-04 21:35:44 +0000398 if (VD->getType()->isReferenceType()) {
399 E = new (getContext())
400 UnaryOperator(const_cast<Expr*>(E), UnaryOperator::AddrOf,
401 getContext().getPointerType(E->getType()),
402 SourceLocation());
403 }
Fariborz Jahanian64176c22010-06-04 16:10:00 +0000404 }
405 }
John McCall87fe5d52010-05-20 01:18:31 +0000406 }
John McCall87fe5d52010-05-20 01:18:31 +0000407
408 if (BDRE->isByRef()) {
409 E = new (getContext())
410 UnaryOperator(const_cast<Expr*>(E), UnaryOperator::AddrOf,
411 getContext().getPointerType(E->getType()),
412 SourceLocation());
413 }
414
415 RValue r = EmitAnyExpr(E, Addr, false);
416 if (r.isScalar()) {
417 llvm::Value *Loc = r.getScalarVal();
418 const llvm::Type *Ty = Types[i+BlockFields];
419 if (BDRE->isByRef()) {
420 // E is now the address of the value field, instead, we want the
421 // address of the actual ByRef struct. We optimize this slightly
422 // compared to gcc by not grabbing the forwarding slot as this must
423 // be done during Block_copy for us, and we can postpone the work
424 // until then.
425 CharUnits offset = BlockDecls[BDRE->getDecl()];
426
427 llvm::Value *BlockLiteral = LoadBlockStruct();
428
429 Loc = Builder.CreateGEP(BlockLiteral,
Chris Lattner5e016ae2010-06-27 07:15:29 +0000430 llvm::ConstantInt::get(Int64Ty, offset.getQuantity()),
John McCall87fe5d52010-05-20 01:18:31 +0000431 "block.literal");
432 Ty = llvm::PointerType::get(Ty, 0);
433 Loc = Builder.CreateBitCast(Loc, Ty);
434 Loc = Builder.CreateLoad(Loc);
435 // Loc = Builder.CreateBitCast(Loc, Ty);
436 }
437 Builder.CreateStore(Loc, Addr);
438 } else if (r.isComplex())
439 // FIXME: implement
440 ErrorUnsupported(BE, "complex in block literal");
441 else if (r.isAggregate())
442 ; // Already created into the destination
443 else
444 assert (0 && "bad block variable");
445 // FIXME: Ensure that the offset created by the backend for
446 // the struct matches the previously computed offset in BlockDecls.
447 }
448 NoteForHelper.resize(NumHelpers);
Mike Stumpd6ef62f2009-03-06 18:42:23 +0000449
450 // __descriptor
Blaine Garstfc83aa02010-02-23 21:51:17 +0000451 llvm::Value *Descriptor = BuildDescriptorBlockDecl(BE,
John McCall9d42f0f2010-05-21 04:11:14 +0000452 Info.BlockHasCopyDispose,
453 Info.BlockSize, Ty,
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000454 &NoteForHelper);
455 Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
456 Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
Mike Stump85284ba2009-02-13 16:19:19 +0000457 }
Mike Stump4446dcf2009-03-05 08:32:30 +0000458
Mike Stump5d2534ad2009-02-19 01:01:04 +0000459 QualType BPT = BE->getType();
Fariborz Jahanian989908f2010-02-10 23:34:57 +0000460 V = Builder.CreateBitCast(V, ConvertType(BPT));
461 // See if this is a __weak block variable and the must call objc_read_weak
462 // on it.
463 const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>();
464 QualType RES = ftype->getResultType();
465 if (RES.isObjCGCWeak()) {
466 // Must cast argument to id*
467 const llvm::Type *ObjectPtrTy =
468 ConvertType(CGM.getContext().getObjCIdType());
469 const llvm::Type *PtrObjectPtrTy =
470 llvm::PointerType::getUnqual(ObjectPtrTy);
471 V = Builder.CreateBitCast(V, PtrObjectPtrTy);
472 V = CGM.getObjCRuntime().EmitObjCWeakRead(*this, V);
473 }
474 return V;
Mike Stump85284ba2009-02-13 16:19:19 +0000475}
476
477
Mike Stump95435672009-03-04 18:17:45 +0000478const llvm::Type *BlockModule::getBlockDescriptorType() {
Mike Stump650c9322009-02-13 15:16:56 +0000479 if (BlockDescriptorType)
480 return BlockDescriptorType;
481
Mike Stumpb7074c02009-02-13 15:32:32 +0000482 const llvm::Type *UnsignedLongTy =
Mike Stump650c9322009-02-13 15:16:56 +0000483 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpb7074c02009-02-13 15:32:32 +0000484
Mike Stump650c9322009-02-13 15:16:56 +0000485 // struct __block_descriptor {
486 // unsigned long reserved;
487 // unsigned long block_size;
Blaine Garstfc83aa02010-02-23 21:51:17 +0000488 //
489 // // later, the following will be added
490 //
491 // struct {
492 // void (*copyHelper)();
493 // void (*copyHelper)();
494 // } helpers; // !!! optional
495 //
496 // const char *signature; // the block signature
497 // const char *layout; // reserved
Mike Stump650c9322009-02-13 15:16:56 +0000498 // };
Owen Anderson758428f2009-08-05 23:18:46 +0000499 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(),
500 UnsignedLongTy,
Mike Stumpb7074c02009-02-13 15:32:32 +0000501 UnsignedLongTy,
Mike Stump650c9322009-02-13 15:16:56 +0000502 NULL);
503
504 getModule().addTypeName("struct.__block_descriptor",
505 BlockDescriptorType);
506
507 return BlockDescriptorType;
Anders Carlsson2437cbf2009-02-12 00:39:25 +0000508}
509
Mike Stump95435672009-03-04 18:17:45 +0000510const llvm::Type *BlockModule::getGenericBlockLiteralType() {
Mike Stump005c9a62009-02-13 15:25:34 +0000511 if (GenericBlockLiteralType)
512 return GenericBlockLiteralType;
513
Mike Stumpb7074c02009-02-13 15:32:32 +0000514 const llvm::Type *BlockDescPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000515 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpb7074c02009-02-13 15:32:32 +0000516
Mike Stump92bbd6d2009-02-13 16:01:35 +0000517 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
518 getTypes().ConvertType(getContext().IntTy));
519
Mike Stump005c9a62009-02-13 15:25:34 +0000520 // struct __block_literal_generic {
Mike Stump5d2534ad2009-02-19 01:01:04 +0000521 // void *__isa;
522 // int __flags;
523 // int __reserved;
524 // void (*__invoke)(void *);
525 // struct __block_descriptor *__descriptor;
Mike Stump005c9a62009-02-13 15:25:34 +0000526 // };
Blaine Garstfc83aa02010-02-23 21:51:17 +0000527 GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
Owen Anderson758428f2009-08-05 23:18:46 +0000528 PtrToInt8Ty,
Mike Stump92bbd6d2009-02-13 16:01:35 +0000529 IntTy,
530 IntTy,
Mike Stump626aecc2009-03-05 01:23:13 +0000531 PtrToInt8Ty,
Mike Stump005c9a62009-02-13 15:25:34 +0000532 BlockDescPtrTy,
533 NULL);
Mike Stumpb7074c02009-02-13 15:32:32 +0000534
Mike Stump005c9a62009-02-13 15:25:34 +0000535 getModule().addTypeName("struct.__block_literal_generic",
536 GenericBlockLiteralType);
Mike Stumpb7074c02009-02-13 15:32:32 +0000537
Mike Stump005c9a62009-02-13 15:25:34 +0000538 return GenericBlockLiteralType;
Anders Carlsson2437cbf2009-02-12 00:39:25 +0000539}
540
Mike Stump5d2534ad2009-02-19 01:01:04 +0000541
Anders Carlssonbfb36712009-12-24 21:13:40 +0000542RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E,
543 ReturnValueSlot ReturnValue) {
Mike Stumpb7074c02009-02-13 15:32:32 +0000544 const BlockPointerType *BPT =
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000545 E->getCallee()->getType()->getAs<BlockPointerType>();
Mike Stumpb7074c02009-02-13 15:32:32 +0000546
Anders Carlsson2437cbf2009-02-12 00:39:25 +0000547 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
548
549 // Get a pointer to the generic block literal.
550 const llvm::Type *BlockLiteralTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000551 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlsson2437cbf2009-02-12 00:39:25 +0000552
553 // Bitcast the callee to a block literal.
Mike Stumpb7074c02009-02-13 15:32:32 +0000554 llvm::Value *BlockLiteral =
Anders Carlsson2437cbf2009-02-12 00:39:25 +0000555 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
556
557 // Get the function pointer from the literal.
558 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Anders Carlsson2437cbf2009-02-12 00:39:25 +0000559
Mike Stumpb7074c02009-02-13 15:32:32 +0000560 BlockLiteral =
561 Builder.CreateBitCast(BlockLiteral,
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000562 llvm::Type::getInt8PtrTy(VMContext),
Anders Carlsson2437cbf2009-02-12 00:39:25 +0000563 "tmp");
Mike Stumpb7074c02009-02-13 15:32:32 +0000564
Anders Carlsson2437cbf2009-02-12 00:39:25 +0000565 // Add the block literal.
566 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
567 CallArgList Args;
568 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpb7074c02009-02-13 15:32:32 +0000569
Anders Carlsson479e6fc2009-04-08 23:13:16 +0000570 QualType FnType = BPT->getPointeeType();
571
Anders Carlsson2437cbf2009-02-12 00:39:25 +0000572 // And the rest of the arguments.
John McCall9dd450b2009-09-21 23:43:11 +0000573 EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(),
Anders Carlsson479e6fc2009-04-08 23:13:16 +0000574 E->arg_begin(), E->arg_end());
Mike Stumpb7074c02009-02-13 15:32:32 +0000575
Anders Carlsson5f50c652009-04-07 22:10:22 +0000576 // Load the function.
Daniel Dunbarc76493a2009-11-29 21:23:36 +0000577 llvm::Value *Func = Builder.CreateLoad(FuncPtr, "tmp");
Anders Carlsson5f50c652009-04-07 22:10:22 +0000578
John McCallab26cfa2010-02-05 21:31:56 +0000579 const FunctionType *FuncTy = FnType->getAs<FunctionType>();
580 QualType ResultType = FuncTy->getResultType();
Anders Carlssona60cbcd2009-04-08 02:55:55 +0000581
Mike Stump11289f42009-09-09 15:08:12 +0000582 const CGFunctionInfo &FnInfo =
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000583 CGM.getTypes().getFunctionInfo(ResultType, Args,
584 FuncTy->getExtInfo());
Mike Stump11289f42009-09-09 15:08:12 +0000585
Anders Carlsson5f50c652009-04-07 22:10:22 +0000586 // Cast the function pointer to the right type.
Mike Stump11289f42009-09-09 15:08:12 +0000587 const llvm::Type *BlockFTy =
Anders Carlssona60cbcd2009-04-08 02:55:55 +0000588 CGM.getTypes().GetFunctionType(FnInfo, false);
Mike Stump11289f42009-09-09 15:08:12 +0000589
Owen Anderson9793f0e2009-07-29 22:16:19 +0000590 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
Anders Carlsson5f50c652009-04-07 22:10:22 +0000591 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000592
Anders Carlsson2437cbf2009-02-12 00:39:25 +0000593 // And call the block.
Anders Carlssonbfb36712009-12-24 21:13:40 +0000594 return EmitCall(FnInfo, Func, ReturnValue, Args);
Anders Carlsson2437cbf2009-02-12 00:39:25 +0000595}
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000596
John McCall87fe5d52010-05-20 01:18:31 +0000597void CodeGenFunction::AllocateBlockCXXThisPointer(const CXXThisExpr *E) {
598 assert(BlockCXXThisOffset.isZero() && "already computed 'this' pointer");
599
600 // Figure out what the offset is.
601 QualType T = E->getType();
602 std::pair<CharUnits,CharUnits> TypeInfo = getContext().getTypeInfoInChars(T);
603 CharUnits Offset = getBlockOffset(TypeInfo.first, TypeInfo.second);
604
605 BlockCXXThisOffset = Offset;
606 BlockLayout.push_back(E);
607}
608
609void CodeGenFunction::AllocateBlockDecl(const BlockDeclRefExpr *E) {
Anders Carlsson0168f4b2009-09-12 02:14:24 +0000610 const ValueDecl *VD = E->getDecl();
John McCall87fe5d52010-05-20 01:18:31 +0000611 CharUnits &Offset = BlockDecls[VD];
Mike Stump97d01d52009-03-04 03:23:46 +0000612
Mike Stump97d01d52009-03-04 03:23:46 +0000613 // See if we have already allocated an offset for this variable.
John McCall87fe5d52010-05-20 01:18:31 +0000614 if (!Offset.isZero())
615 return;
Mike Stump7fe9cc12009-10-21 03:49:08 +0000616
617 // Don't run the expensive check, unless we have to.
Mike Stumpe1b19ba2009-10-22 00:49:09 +0000618 if (!BlockHasCopyDispose)
619 if (E->isByRef()
620 || BlockRequiresCopying(E->getType()))
621 BlockHasCopyDispose = true;
Mike Stump7fe9cc12009-10-21 03:49:08 +0000622
John McCall87fe5d52010-05-20 01:18:31 +0000623 const ValueDecl *D = cast<ValueDecl>(E->getDecl());
Mike Stump7fe9cc12009-10-21 03:49:08 +0000624
John McCall87fe5d52010-05-20 01:18:31 +0000625 CharUnits Size;
626 CharUnits Align;
627
628 if (E->isByRef()) {
629 llvm::tie(Size,Align) =
630 getContext().getTypeInfoInChars(getContext().VoidPtrTy);
631 } else {
632 Size = getContext().getTypeSizeInChars(D->getType());
633 Align = getContext().getDeclAlign(D);
634 }
635
636 Offset = getBlockOffset(Size, Align);
637 BlockLayout.push_back(E);
Mike Stump7fe9cc12009-10-21 03:49:08 +0000638}
639
John McCall9d42f0f2010-05-21 04:11:14 +0000640llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const ValueDecl *VD,
641 bool IsByRef) {
Fariborz Jahanian64176c22010-06-04 16:10:00 +0000642
John McCall87fe5d52010-05-20 01:18:31 +0000643 CharUnits offset = BlockDecls[VD];
644 assert(!offset.isZero() && "getting address of unallocated decl");
Mike Stump97d01d52009-03-04 03:23:46 +0000645
646 llvm::Value *BlockLiteral = LoadBlockStruct();
647 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
Chris Lattner5e016ae2010-06-27 07:15:29 +0000648 llvm::ConstantInt::get(Int64Ty, offset.getQuantity()),
Mike Stump2d33d632009-03-04 22:48:06 +0000649 "block.literal");
John McCall9d42f0f2010-05-21 04:11:14 +0000650 if (IsByRef) {
Mike Stump97d01d52009-03-04 03:23:46 +0000651 const llvm::Type *PtrStructTy
Anders Carlsson0168f4b2009-09-12 02:14:24 +0000652 = llvm::PointerType::get(BuildByRefType(VD), 0);
Mike Stumpefd7caa82009-03-21 21:00:35 +0000653 // The block literal will need a copy/destroy helper.
654 BlockHasCopyDispose = true;
Anders Carlsson0168f4b2009-09-12 02:14:24 +0000655
656 const llvm::Type *Ty = PtrStructTy;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000657 Ty = llvm::PointerType::get(Ty, 0);
Mike Stump97d01d52009-03-04 03:23:46 +0000658 V = Builder.CreateBitCast(V, Ty);
Daniel Dunbarc76493a2009-11-29 21:23:36 +0000659 V = Builder.CreateLoad(V);
Mike Stump97d01d52009-03-04 03:23:46 +0000660 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbarc76493a2009-11-29 21:23:36 +0000661 V = Builder.CreateLoad(V);
Mike Stump97d01d52009-03-04 03:23:46 +0000662 V = Builder.CreateBitCast(V, PtrStructTy);
Anders Carlsson0168f4b2009-09-12 02:14:24 +0000663 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
664 VD->getNameAsString());
Fariborz Jahanianeeb23372010-05-04 17:59:32 +0000665 if (VD->getType()->isReferenceType())
666 V = Builder.CreateLoad(V);
Mike Stump97d01d52009-03-04 03:23:46 +0000667 } else {
Anders Carlsson0168f4b2009-09-12 02:14:24 +0000668 const llvm::Type *Ty = CGM.getTypes().ConvertType(VD->getType());
Owen Anderson9793f0e2009-07-29 22:16:19 +0000669 Ty = llvm::PointerType::get(Ty, 0);
Mike Stump97d01d52009-03-04 03:23:46 +0000670 V = Builder.CreateBitCast(V, Ty);
Fariborz Jahaniand1b378e2010-06-02 21:35:17 +0000671 if (VD->getType()->isReferenceType())
Fariborz Jahanian64176c22010-06-04 16:10:00 +0000672 V = Builder.CreateLoad(V, "ref.tmp");
Mike Stump97d01d52009-03-04 03:23:46 +0000673 }
674 return V;
675}
676
Mike Stump2d5a2872009-02-14 22:16:35 +0000677llvm::Constant *
Mike Stump6c396662009-03-04 18:47:42 +0000678BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000679 // Generate the block descriptor.
680 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump92bbd6d2009-02-13 16:01:35 +0000681 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
682 getTypes().ConvertType(getContext().IntTy));
Mike Stumpb7074c02009-02-13 15:32:32 +0000683
Blaine Garstfc83aa02010-02-23 21:51:17 +0000684 llvm::Constant *DescriptorFields[4];
Mike Stumpb7074c02009-02-13 15:32:32 +0000685
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000686 // Reserved
Owen Anderson0b75f232009-07-31 20:28:54 +0000687 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpb7074c02009-02-13 15:32:32 +0000688
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000689 // Block literal size. For global blocks we just use the size of the generic
690 // block literal struct.
Ken Dyck98ca7942010-01-26 13:48:07 +0000691 CharUnits BlockLiteralSize =
692 CGM.GetTargetTypeStoreSize(getGenericBlockLiteralType());
Owen Anderson170229f2009-07-14 23:10:40 +0000693 DescriptorFields[1] =
Ken Dyck40775002010-01-11 17:06:35 +0000694 llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize.getQuantity());
Blaine Garstfc83aa02010-02-23 21:51:17 +0000695
696 // signature. non-optional ObjC-style method descriptor @encode sequence
697 std::string BlockTypeEncoding;
698 CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
Mike Stumpb7074c02009-02-13 15:32:32 +0000699
Blaine Garstfc83aa02010-02-23 21:51:17 +0000700 DescriptorFields[2] = llvm::ConstantExpr::getBitCast(
701 CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty);
702
703 // layout
704 DescriptorFields[3] =
705 llvm::ConstantInt::get(UnsignedLongTy,0);
706
707 // build the structure from the 4 elements
Mike Stumpb7074c02009-02-13 15:32:32 +0000708 llvm::Constant *DescriptorStruct =
Blaine Garstfc83aa02010-02-23 21:51:17 +0000709 llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 4, false);
Mike Stumpb7074c02009-02-13 15:32:32 +0000710
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000711 llvm::GlobalVariable *Descriptor =
Owen Andersonc10c8d32009-07-08 19:05:04 +0000712 new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true,
Mike Stumpb7074c02009-02-13 15:32:32 +0000713 llvm::GlobalVariable::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +0000714 DescriptorStruct, "__block_descriptor_global");
Mike Stumpb7074c02009-02-13 15:32:32 +0000715
David Chisnall950a9512009-11-17 19:33:30 +0000716 int FieldCount = 5;
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000717 // Generate the constants for the block literal.
David Chisnall950a9512009-11-17 19:33:30 +0000718
719 std::vector<llvm::Constant*> LiteralFields(FieldCount);
Mike Stumpb7074c02009-02-13 15:32:32 +0000720
John McCall9d42f0f2010-05-21 04:11:14 +0000721 CGBlockInfo Info(n);
Mike Stump5469f292009-03-13 23:34:28 +0000722 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Mike Stumpcb2fbcb2009-02-21 20:00:35 +0000723 llvm::Function *Fn
Fariborz Jahanian9b5528d2010-06-24 00:08:06 +0000724 = CodeGenFunction(CGM).GenerateBlockFunction(GlobalDecl(), BE, Info, 0, LocalDeclMap);
John McCall9d42f0f2010-05-21 04:11:14 +0000725 assert(Info.BlockSize == BlockLiteralSize
Mike Stumpcb2fbcb2009-02-21 20:00:35 +0000726 && "no imports allowed for global block");
Mike Stumpb7074c02009-02-13 15:32:32 +0000727
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000728 // isa
Mike Stump971f9b62009-02-13 17:23:42 +0000729 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpb7074c02009-02-13 15:32:32 +0000730
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000731 // Flags
Blaine Garstfc83aa02010-02-23 21:51:17 +0000732 LiteralFields[1] =
Blaine Garstf27ab712010-03-05 01:29:59 +0000733 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE);
Mike Stumpb7074c02009-02-13 15:32:32 +0000734
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000735 // Reserved
Owen Anderson0b75f232009-07-31 20:28:54 +0000736 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpb7074c02009-02-13 15:32:32 +0000737
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000738 // Function
739 LiteralFields[3] = Fn;
Mike Stumpb7074c02009-02-13 15:32:32 +0000740
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000741 // Descriptor
742 LiteralFields[4] = Descriptor;
David Chisnall950a9512009-11-17 19:33:30 +0000743
Mike Stumpb7074c02009-02-13 15:32:32 +0000744 llvm::Constant *BlockLiteralStruct =
David Chisnall950a9512009-11-17 19:33:30 +0000745 llvm::ConstantStruct::get(VMContext, LiteralFields, false);
Mike Stumpb7074c02009-02-13 15:32:32 +0000746
747 llvm::GlobalVariable *BlockLiteral =
Owen Andersonc10c8d32009-07-08 19:05:04 +0000748 new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true,
Mike Stumpb7074c02009-02-13 15:32:32 +0000749 llvm::GlobalVariable::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +0000750 BlockLiteralStruct, "__block_literal_global");
Mike Stumpb7074c02009-02-13 15:32:32 +0000751
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000752 return BlockLiteral;
753}
754
Mike Stumpcb2fbcb2009-02-21 20:00:35 +0000755llvm::Value *CodeGenFunction::LoadBlockStruct() {
Mike Stump38382022009-10-20 20:30:01 +0000756 llvm::Value *V = Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()],
757 "self");
758 // For now, we codegen based upon byte offsets.
759 return Builder.CreateBitCast(V, PtrToInt8Ty);
Mike Stumpcb2fbcb2009-02-21 20:00:35 +0000760}
761
Mike Stump4446dcf2009-03-05 08:32:30 +0000762llvm::Function *
Fariborz Jahanian9b5528d2010-06-24 00:08:06 +0000763CodeGenFunction::GenerateBlockFunction(GlobalDecl GD, const BlockExpr *BExpr,
John McCall9d42f0f2010-05-21 04:11:14 +0000764 CGBlockInfo &Info,
Mike Stump692c6e32009-03-20 21:53:12 +0000765 const Decl *OuterFuncDecl,
John McCall9d42f0f2010-05-21 04:11:14 +0000766 llvm::DenseMap<const Decl*, llvm::Value*> ldm) {
Devang Patel9074ed82009-04-15 21:51:44 +0000767
768 // Check if we should generate debug info for this block.
769 if (CGM.getDebugInfo())
770 DebugInfo = CGM.getDebugInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000771
Mike Stump5469f292009-03-13 23:34:28 +0000772 // Arrange for local static and local extern declarations to appear
773 // to be local to this function as well, as they are directly referenced
774 // in a block.
775 for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
776 i != ldm.end();
777 ++i) {
778 const VarDecl *VD = dyn_cast<VarDecl>(i->first);
Mike Stump11289f42009-09-09 15:08:12 +0000779
Daniel Dunbar0ca16602009-04-14 02:25:56 +0000780 if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage())
Mike Stump5469f292009-03-13 23:34:28 +0000781 LocalDeclMap[VD] = i->second;
782 }
783
Ken Dyck98ca7942010-01-26 13:48:07 +0000784 BlockOffset =
785 CGM.GetTargetTypeStoreSize(CGM.getGenericBlockLiteralType());
Ken Dyck5262b112010-01-26 19:13:33 +0000786 BlockAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Eli Friedman09a9b6e2009-03-28 03:24:54 +0000787
Fariborz Jahanianaae43492009-04-11 17:55:15 +0000788 const FunctionType *BlockFunctionType = BExpr->getFunctionType();
789 QualType ResultType;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000790 FunctionType::ExtInfo EInfo = getFunctionExtInfo(*BlockFunctionType);
Fariborz Jahanianaae43492009-04-11 17:55:15 +0000791 bool IsVariadic;
Mike Stump11289f42009-09-09 15:08:12 +0000792 if (const FunctionProtoType *FTy =
Fariborz Jahaniana4f6b6b2009-04-11 18:54:57 +0000793 dyn_cast<FunctionProtoType>(BlockFunctionType)) {
Fariborz Jahanianaae43492009-04-11 17:55:15 +0000794 ResultType = FTy->getResultType();
795 IsVariadic = FTy->isVariadic();
Mike Stump658fe022009-07-30 22:28:39 +0000796 } else {
Fariborz Jahanianaae43492009-04-11 17:55:15 +0000797 // K&R style block.
798 ResultType = BlockFunctionType->getResultType();
799 IsVariadic = false;
800 }
Mike Stumpb7074c02009-02-13 15:32:32 +0000801
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000802 FunctionArgList Args;
Mike Stumpb7074c02009-02-13 15:32:32 +0000803
Mike Stump7fe9cc12009-10-21 03:49:08 +0000804 CurFuncDecl = OuterFuncDecl;
805
Chris Lattner3385fe122009-02-28 19:01:03 +0000806 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000807
Mike Stump7fe9cc12009-10-21 03:49:08 +0000808 IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor");
Mike Stumpd0153282009-10-20 02:12:22 +0000809
John McCall87fe5d52010-05-20 01:18:31 +0000810 // Build the block struct now.
John McCall9d42f0f2010-05-21 04:11:14 +0000811 AllocateAllBlockDeclRefs(*this, Info);
Mike Stump7fe9cc12009-10-21 03:49:08 +0000812
Mike Stumpe1b19ba2009-10-22 00:49:09 +0000813 QualType ParmTy = getContext().getBlockParmType(BlockHasCopyDispose,
John McCall87fe5d52010-05-20 01:18:31 +0000814 BlockLayout);
815
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000816 // FIXME: This leaks
Mike Stumpb7074c02009-02-13 15:32:32 +0000817 ImplicitParamDecl *SelfDecl =
John McCall4d4dcc82010-04-30 21:35:41 +0000818 ImplicitParamDecl::Create(getContext(), const_cast<BlockDecl*>(BD),
Mike Stumpd0153282009-10-20 02:12:22 +0000819 SourceLocation(), II,
820 ParmTy);
Mike Stumpb7074c02009-02-13 15:32:32 +0000821
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000822 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stumpcb2fbcb2009-02-21 20:00:35 +0000823 BlockStructDecl = SelfDecl;
Mike Stumpb7074c02009-02-13 15:32:32 +0000824
Steve Naroffc4b30e52009-03-13 16:56:44 +0000825 for (BlockDecl::param_const_iterator i = BD->param_begin(),
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000826 e = BD->param_end(); i != e; ++i)
Mike Stumpeecd39f2009-02-17 23:25:52 +0000827 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpb7074c02009-02-13 15:32:32 +0000828
829 const CGFunctionInfo &FI =
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000830 CGM.getTypes().getFunctionInfo(ResultType, Args, EInfo);
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000831
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000832 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanianaae43492009-04-11 17:55:15 +0000833 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic);
Mike Stumpb7074c02009-02-13 15:32:32 +0000834
Douglas Gregor7e10f362010-05-25 17:04:15 +0000835 MangleBuffer Name;
Fariborz Jahanian9b5528d2010-06-24 00:08:06 +0000836 CGM.getMangledName(GD, Name, BD);
Mike Stumpb7074c02009-02-13 15:32:32 +0000837 llvm::Function *Fn =
Douglas Gregor02df9da2010-05-25 17:12:30 +0000838 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
839 Name.getString(), &CGM.getModule());
Mike Stumpb7074c02009-02-13 15:32:32 +0000840
Daniel Dunbarc3e7cff2009-04-17 00:48:04 +0000841 CGM.SetInternalFunctionAttributes(BD, Fn, FI);
842
Chris Lattner324f80b2009-04-23 07:18:56 +0000843 StartFunction(BD, ResultType, Fn, Args,
Chris Lattner3385fe122009-02-28 19:01:03 +0000844 BExpr->getBody()->getLocEnd());
Mike Stump2e722b92009-09-30 02:43:10 +0000845
Chris Lattner324f80b2009-04-23 07:18:56 +0000846 CurFuncDecl = OuterFuncDecl;
Chris Lattner28ec0cf2009-04-23 05:30:27 +0000847 CurCodeDecl = BD;
Mike Stump017460a2009-10-01 22:29:41 +0000848
John McCall87fe5d52010-05-20 01:18:31 +0000849 // If we have a C++ 'this' reference, go ahead and force it into
850 // existence now.
851 if (Info.CXXThisRef) {
852 assert(!BlockCXXThisOffset.isZero() &&
853 "haven't yet allocated 'this' reference");
854
855 // TODO: I have a dream that one day this will be typed.
856 llvm::Value *BlockLiteral = LoadBlockStruct();
857 llvm::Value *ThisPtrRaw =
858 Builder.CreateConstInBoundsGEP1_64(BlockLiteral,
859 BlockCXXThisOffset.getQuantity(),
860 "this.ptr.raw");
861
862 const llvm::Type *Ty =
863 CGM.getTypes().ConvertType(Info.CXXThisRef->getType());
864 Ty = llvm::PointerType::get(Ty, 0);
865 llvm::Value *ThisPtr = Builder.CreateBitCast(ThisPtrRaw, Ty, "this.ptr");
866
867 CXXThisValue = Builder.CreateLoad(ThisPtr, "this");
868 }
869
John McCall9d42f0f2010-05-21 04:11:14 +0000870 // If we have an Objective C 'self' reference, go ahead and force it
871 // into existence now.
872 if (Info.NeedsObjCSelf) {
873 ValueDecl *Self = cast<ObjCMethodDecl>(CurFuncDecl)->getSelfDecl();
874 LocalDeclMap[Self] = GetAddrOfBlockDecl(Self, false);
875 }
876
Mike Stump017460a2009-10-01 22:29:41 +0000877 // Save a spot to insert the debug information for all the BlockDeclRefDecls.
878 llvm::BasicBlock *entry = Builder.GetInsertBlock();
879 llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint();
880 --entry_ptr;
881
Chris Lattner3385fe122009-02-28 19:01:03 +0000882 EmitStmt(BExpr->getBody());
Mike Stump017460a2009-10-01 22:29:41 +0000883
Mike Stump7d699112009-10-01 00:27:30 +0000884 // Remember where we were...
885 llvm::BasicBlock *resume = Builder.GetInsertBlock();
Mike Stump017460a2009-10-01 22:29:41 +0000886
Mike Stump7d699112009-10-01 00:27:30 +0000887 // Go back to the entry.
Mike Stump017460a2009-10-01 22:29:41 +0000888 ++entry_ptr;
889 Builder.SetInsertPoint(entry, entry_ptr);
890
Mike Stump2e722b92009-09-30 02:43:10 +0000891 if (CGDebugInfo *DI = getDebugInfo()) {
Mike Stump2e722b92009-09-30 02:43:10 +0000892 // Emit debug information for all the BlockDeclRefDecls.
John McCall87fe5d52010-05-20 01:18:31 +0000893 // FIXME: also for 'this'
894 for (unsigned i = 0, e = BlockLayout.size(); i != e; ++i) {
895 if (const BlockDeclRefExpr *BDRE =
896 dyn_cast<BlockDeclRefExpr>(BlockLayout[i])) {
Mike Stump2e722b92009-09-30 02:43:10 +0000897 const ValueDecl *D = BDRE->getDecl();
898 DI->setLocation(D->getLocation());
899 DI->EmitDeclareOfBlockDeclRefVariable(BDRE,
900 LocalDeclMap[getBlockStructDecl()],
901 Builder, this);
902 }
903 }
Mike Stump2e722b92009-09-30 02:43:10 +0000904 }
Mike Stump7d699112009-10-01 00:27:30 +0000905 // And resume where we left off.
906 if (resume == 0)
907 Builder.ClearInsertionPoint();
908 else
909 Builder.SetInsertPoint(resume);
Mike Stump2e722b92009-09-30 02:43:10 +0000910
Chris Lattner3385fe122009-02-28 19:01:03 +0000911 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000912
Mike Stumpb750d922009-02-25 23:33:13 +0000913 // The runtime needs a minimum alignment of a void *.
Ken Dyck5262b112010-01-26 19:13:33 +0000914 CharUnits MinAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Ken Dyck40775002010-01-11 17:06:35 +0000915 BlockOffset = CharUnits::fromQuantity(
Ken Dyck5262b112010-01-26 19:13:33 +0000916 llvm::RoundUpToAlignment(BlockOffset.getQuantity(),
917 MinAlign.getQuantity()));
Mike Stumpb750d922009-02-25 23:33:13 +0000918
John McCall9d42f0f2010-05-21 04:11:14 +0000919 Info.BlockSize = BlockOffset;
920 Info.BlockAlign = BlockAlign;
921 Info.BlockLayout = BlockLayout;
922 Info.BlockHasCopyDispose = BlockHasCopyDispose;
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000923 return Fn;
924}
Mike Stump1db7d042009-02-28 09:07:16 +0000925
John McCall87fe5d52010-05-20 01:18:31 +0000926CharUnits BlockFunction::getBlockOffset(CharUnits Size, CharUnits Align) {
927 assert((Align.isPositive()) && "alignment must be 1 byte or more");
Mike Stump1db7d042009-02-28 09:07:16 +0000928
Ken Dyck40775002010-01-11 17:06:35 +0000929 CharUnits OldOffset = BlockOffset;
Mike Stump1db7d042009-02-28 09:07:16 +0000930
931 // Ensure proper alignment, even if it means we have to have a gap
Ken Dyck40775002010-01-11 17:06:35 +0000932 BlockOffset = CharUnits::fromQuantity(
Ken Dyck5262b112010-01-26 19:13:33 +0000933 llvm::RoundUpToAlignment(BlockOffset.getQuantity(), Align.getQuantity()));
Mike Stump1db7d042009-02-28 09:07:16 +0000934 BlockAlign = std::max(Align, BlockAlign);
Mike Stump4446dcf2009-03-05 08:32:30 +0000935
Ken Dyck40775002010-01-11 17:06:35 +0000936 CharUnits Pad = BlockOffset - OldOffset;
937 if (Pad.isPositive()) {
Mike Stump1db7d042009-02-28 09:07:16 +0000938 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
Ken Dyck40775002010-01-11 17:06:35 +0000939 llvm::APInt(32,
940 Pad.getQuantity()),
Mike Stump1db7d042009-02-28 09:07:16 +0000941 ArrayType::Normal, 0);
Douglas Gregor6a047c92010-05-11 18:17:16 +0000942 ValueDecl *PadDecl = VarDecl::Create(getContext(),
943 getContext().getTranslationUnitDecl(),
944 SourceLocation(),
Douglas Gregorc4df4072010-04-19 22:54:31 +0000945 0, QualType(PadTy), 0,
946 VarDecl::None, VarDecl::None);
John McCall87fe5d52010-05-20 01:18:31 +0000947 Expr *E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
948 SourceLocation());
949 BlockLayout.push_back(E);
Mike Stump1db7d042009-02-28 09:07:16 +0000950 }
Mike Stump1db7d042009-02-28 09:07:16 +0000951
952 BlockOffset += Size;
John McCall87fe5d52010-05-20 01:18:31 +0000953 return BlockOffset - Size;
Mike Stump1db7d042009-02-28 09:07:16 +0000954}
Mike Stump97d01d52009-03-04 03:23:46 +0000955
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000956llvm::Constant *BlockFunction::
957GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
Mike Stump67645932009-04-10 18:52:28 +0000958 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stump0c743272009-03-06 01:33:24 +0000959 QualType R = getContext().VoidTy;
960
961 FunctionArgList Args;
962 // FIXME: This leaks
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000963 ImplicitParamDecl *Dst =
Mike Stump7fe9cc12009-10-21 03:49:08 +0000964 ImplicitParamDecl::Create(getContext(), 0,
965 SourceLocation(), 0,
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000966 getContext().getPointerType(getContext().VoidTy));
967 Args.push_back(std::make_pair(Dst, Dst->getType()));
Mike Stump0c743272009-03-06 01:33:24 +0000968 ImplicitParamDecl *Src =
Mike Stump7fe9cc12009-10-21 03:49:08 +0000969 ImplicitParamDecl::Create(getContext(), 0,
970 SourceLocation(), 0,
Mike Stump0c743272009-03-06 01:33:24 +0000971 getContext().getPointerType(getContext().VoidTy));
Mike Stump0c743272009-03-06 01:33:24 +0000972 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump11289f42009-09-09 15:08:12 +0000973
Mike Stump0c743272009-03-06 01:33:24 +0000974 const CGFunctionInfo &FI =
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000975 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stump0c743272009-03-06 01:33:24 +0000976
Mike Stumpcbc2bca2009-06-05 23:26:36 +0000977 // FIXME: We'd like to put these into a mergable by content, with
978 // internal linkage.
Mike Stump0c743272009-03-06 01:33:24 +0000979 CodeGenTypes &Types = CGM.getTypes();
980 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
981
982 llvm::Function *Fn =
983 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramerd6b28fc2010-01-22 13:59:13 +0000984 "__copy_helper_block_", &CGM.getModule());
Mike Stump0c743272009-03-06 01:33:24 +0000985
986 IdentifierInfo *II
987 = &CGM.getContext().Idents.get("__copy_helper_block_");
988
989 FunctionDecl *FD = FunctionDecl::Create(getContext(),
990 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000991 SourceLocation(), II, R, 0,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000992 FunctionDecl::Static,
993 FunctionDecl::None,
994 false,
Mike Stump0c743272009-03-06 01:33:24 +0000995 true);
996 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000997
998 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
999 llvm::Type *PtrPtrT;
Mike Stumpaeb0ffd2009-03-07 02:35:30 +00001000
Mike Stump67645932009-04-10 18:52:28 +00001001 if (NoteForHelperp) {
1002 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stumpaeb0ffd2009-03-07 02:35:30 +00001003
Owen Anderson9793f0e2009-07-29 22:16:19 +00001004 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stump67645932009-04-10 18:52:28 +00001005 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
1006 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stumpaeb0ffd2009-03-07 02:35:30 +00001007
Mike Stump67645932009-04-10 18:52:28 +00001008 llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
Mike Stump3b65ac22009-04-15 22:11:36 +00001009 llvm::Type *PtrPtrT;
Owen Anderson9793f0e2009-07-29 22:16:19 +00001010 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stump3b65ac22009-04-15 22:11:36 +00001011 DstObj = Builder.CreateBitCast(DstObj, PtrPtrT);
1012 DstObj = Builder.CreateLoad(DstObj);
Mike Stumpaeb0ffd2009-03-07 02:35:30 +00001013
Mike Stump67645932009-04-10 18:52:28 +00001014 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
1015 int flag = NoteForHelper[i].flag;
1016 int index = NoteForHelper[i].index;
Mike Stumpaeb0ffd2009-03-07 02:35:30 +00001017
Mike Stump67645932009-04-10 18:52:28 +00001018 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
1019 || NoteForHelper[i].RequiresCopying) {
1020 llvm::Value *Srcv = SrcObj;
1021 Srcv = Builder.CreateStructGEP(Srcv, index);
1022 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001023 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stump67645932009-04-10 18:52:28 +00001024 Srcv = Builder.CreateLoad(Srcv);
1025
1026 llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
1027 Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
1028
Chris Lattner5e016ae2010-06-27 07:15:29 +00001029 llvm::Value *N = llvm::ConstantInt::get(CGF.Int32Ty, flag);
Mike Stump67645932009-04-10 18:52:28 +00001030 llvm::Value *F = getBlockObjectAssign();
1031 Builder.CreateCall3(F, Dstv, Srcv, N);
1032 }
Mike Stumpaeb0ffd2009-03-07 02:35:30 +00001033 }
1034 }
1035
Mike Stump0c743272009-03-06 01:33:24 +00001036 CGF.FinishFunction();
1037
Owen Andersonade90fd2009-07-29 18:54:39 +00001038 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump97d01d52009-03-04 03:23:46 +00001039}
1040
Mike Stumpd6ef62f2009-03-06 18:42:23 +00001041llvm::Constant *BlockFunction::
Mike Stumpaeb0ffd2009-03-07 02:35:30 +00001042GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
1043 const llvm::StructType* T,
Mike Stump67645932009-04-10 18:52:28 +00001044 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stump0c743272009-03-06 01:33:24 +00001045 QualType R = getContext().VoidTy;
1046
1047 FunctionArgList Args;
1048 // FIXME: This leaks
1049 ImplicitParamDecl *Src =
Mike Stump7fe9cc12009-10-21 03:49:08 +00001050 ImplicitParamDecl::Create(getContext(), 0,
1051 SourceLocation(), 0,
Mike Stump0c743272009-03-06 01:33:24 +00001052 getContext().getPointerType(getContext().VoidTy));
1053
1054 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00001055
Mike Stump0c743272009-03-06 01:33:24 +00001056 const CGFunctionInfo &FI =
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001057 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stump0c743272009-03-06 01:33:24 +00001058
Mike Stumpcbc2bca2009-06-05 23:26:36 +00001059 // FIXME: We'd like to put these into a mergable by content, with
1060 // internal linkage.
Mike Stump0c743272009-03-06 01:33:24 +00001061 CodeGenTypes &Types = CGM.getTypes();
1062 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1063
1064 llvm::Function *Fn =
1065 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramerd6b28fc2010-01-22 13:59:13 +00001066 "__destroy_helper_block_", &CGM.getModule());
Mike Stump0c743272009-03-06 01:33:24 +00001067
1068 IdentifierInfo *II
1069 = &CGM.getContext().Idents.get("__destroy_helper_block_");
1070
1071 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1072 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00001073 SourceLocation(), II, R, 0,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001074 FunctionDecl::Static,
1075 FunctionDecl::None,
1076 false, true);
Mike Stump0c743272009-03-06 01:33:24 +00001077 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump6f7d9f82009-03-07 02:53:18 +00001078
Mike Stump67645932009-04-10 18:52:28 +00001079 if (NoteForHelperp) {
1080 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump6f7d9f82009-03-07 02:53:18 +00001081
Mike Stump67645932009-04-10 18:52:28 +00001082 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
1083 llvm::Type *PtrPtrT;
Owen Anderson9793f0e2009-07-29 22:16:19 +00001084 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stump67645932009-04-10 18:52:28 +00001085 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
1086 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump6f7d9f82009-03-07 02:53:18 +00001087
Mike Stump67645932009-04-10 18:52:28 +00001088 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
1089 int flag = NoteForHelper[i].flag;
1090 int index = NoteForHelper[i].index;
Mike Stump6f7d9f82009-03-07 02:53:18 +00001091
Mike Stump67645932009-04-10 18:52:28 +00001092 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
1093 || NoteForHelper[i].RequiresCopying) {
1094 llvm::Value *Srcv = SrcObj;
1095 Srcv = Builder.CreateStructGEP(Srcv, index);
1096 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001097 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stump67645932009-04-10 18:52:28 +00001098 Srcv = Builder.CreateLoad(Srcv);
1099
1100 BuildBlockRelease(Srcv, flag);
1101 }
Mike Stump6f7d9f82009-03-07 02:53:18 +00001102 }
1103 }
1104
Mike Stump0c743272009-03-06 01:33:24 +00001105 CGF.FinishFunction();
1106
Owen Andersonade90fd2009-07-29 18:54:39 +00001107 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump0c743272009-03-06 01:33:24 +00001108}
1109
Mike Stumpaeb0ffd2009-03-07 02:35:30 +00001110llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
Mike Stump67645932009-04-10 18:52:28 +00001111 std::vector<HelperInfo> *NoteForHelper) {
Mike Stumpaeb0ffd2009-03-07 02:35:30 +00001112 return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
1113 T, NoteForHelper);
Mike Stump0c743272009-03-06 01:33:24 +00001114}
1115
Mike Stumpaeb0ffd2009-03-07 02:35:30 +00001116llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
Mike Stump67645932009-04-10 18:52:28 +00001117 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpaeb0ffd2009-03-07 02:35:30 +00001118 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
Mike Stump67645932009-04-10 18:52:28 +00001119 T, NoteForHelperp);
Mike Stump97d01d52009-03-04 03:23:46 +00001120}
Mike Stump626aecc2009-03-05 01:23:13 +00001121
Mike Stumpf89230d2009-03-06 06:12:24 +00001122llvm::Constant *BlockFunction::
1123GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
Mike Stumpee2a5ee2009-03-06 02:29:21 +00001124 QualType R = getContext().VoidTy;
1125
1126 FunctionArgList Args;
1127 // FIXME: This leaks
Mike Stumpf89230d2009-03-06 06:12:24 +00001128 ImplicitParamDecl *Dst =
Mike Stump7fe9cc12009-10-21 03:49:08 +00001129 ImplicitParamDecl::Create(getContext(), 0,
1130 SourceLocation(), 0,
Mike Stumpf89230d2009-03-06 06:12:24 +00001131 getContext().getPointerType(getContext().VoidTy));
1132 Args.push_back(std::make_pair(Dst, Dst->getType()));
1133
1134 // FIXME: This leaks
Mike Stumpee2a5ee2009-03-06 02:29:21 +00001135 ImplicitParamDecl *Src =
Mike Stump7fe9cc12009-10-21 03:49:08 +00001136 ImplicitParamDecl::Create(getContext(), 0,
1137 SourceLocation(), 0,
Mike Stumpee2a5ee2009-03-06 02:29:21 +00001138 getContext().getPointerType(getContext().VoidTy));
Mike Stumpee2a5ee2009-03-06 02:29:21 +00001139 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00001140
Mike Stumpee2a5ee2009-03-06 02:29:21 +00001141 const CGFunctionInfo &FI =
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001142 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stumpee2a5ee2009-03-06 02:29:21 +00001143
Mike Stumpee2a5ee2009-03-06 02:29:21 +00001144 CodeGenTypes &Types = CGM.getTypes();
1145 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1146
Mike Stumpcbc2bca2009-06-05 23:26:36 +00001147 // FIXME: We'd like to put these into a mergable by content, with
1148 // internal linkage.
Mike Stumpee2a5ee2009-03-06 02:29:21 +00001149 llvm::Function *Fn =
1150 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramerd6b28fc2010-01-22 13:59:13 +00001151 "__Block_byref_id_object_copy_", &CGM.getModule());
Mike Stumpee2a5ee2009-03-06 02:29:21 +00001152
1153 IdentifierInfo *II
1154 = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
1155
1156 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1157 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00001158 SourceLocation(), II, R, 0,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001159 FunctionDecl::Static,
1160 FunctionDecl::None,
1161 false, true);
Mike Stumpee2a5ee2009-03-06 02:29:21 +00001162 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpf89230d2009-03-06 06:12:24 +00001163
1164 // dst->x
1165 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
Owen Anderson9793f0e2009-07-29 22:16:19 +00001166 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stump3b65ac22009-04-15 22:11:36 +00001167 V = Builder.CreateLoad(V);
Mike Stumpf89230d2009-03-06 06:12:24 +00001168 V = Builder.CreateStructGEP(V, 6, "x");
1169 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
1170
1171 // src->x
1172 V = CGF.GetAddrOfLocalVar(Src);
1173 V = Builder.CreateLoad(V);
1174 V = Builder.CreateBitCast(V, T);
1175 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson9793f0e2009-07-29 22:16:19 +00001176 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpf89230d2009-03-06 06:12:24 +00001177 llvm::Value *SrcObj = Builder.CreateLoad(V);
Mike Stump11289f42009-09-09 15:08:12 +00001178
Mike Stumpf89230d2009-03-06 06:12:24 +00001179 flag |= BLOCK_BYREF_CALLER;
1180
Chris Lattner5e016ae2010-06-27 07:15:29 +00001181 llvm::Value *N = llvm::ConstantInt::get(CGF.Int32Ty, flag);
Mike Stumpf89230d2009-03-06 06:12:24 +00001182 llvm::Value *F = getBlockObjectAssign();
1183 Builder.CreateCall3(F, DstObj, SrcObj, N);
1184
Mike Stumpee2a5ee2009-03-06 02:29:21 +00001185 CGF.FinishFunction();
1186
Owen Andersonade90fd2009-07-29 18:54:39 +00001187 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpee2a5ee2009-03-06 02:29:21 +00001188}
1189
Mike Stumpfbe25dd2009-03-06 04:53:30 +00001190llvm::Constant *
1191BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
1192 int flag) {
Mike Stumpee2a5ee2009-03-06 02:29:21 +00001193 QualType R = getContext().VoidTy;
1194
1195 FunctionArgList Args;
1196 // FIXME: This leaks
1197 ImplicitParamDecl *Src =
Mike Stump7fe9cc12009-10-21 03:49:08 +00001198 ImplicitParamDecl::Create(getContext(), 0,
1199 SourceLocation(), 0,
Mike Stumpee2a5ee2009-03-06 02:29:21 +00001200 getContext().getPointerType(getContext().VoidTy));
1201
1202 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00001203
Mike Stumpee2a5ee2009-03-06 02:29:21 +00001204 const CGFunctionInfo &FI =
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001205 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stumpee2a5ee2009-03-06 02:29:21 +00001206
Mike Stumpee2a5ee2009-03-06 02:29:21 +00001207 CodeGenTypes &Types = CGM.getTypes();
1208 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1209
Mike Stumpcbc2bca2009-06-05 23:26:36 +00001210 // FIXME: We'd like to put these into a mergable by content, with
1211 // internal linkage.
Mike Stumpee2a5ee2009-03-06 02:29:21 +00001212 llvm::Function *Fn =
1213 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramerd6b28fc2010-01-22 13:59:13 +00001214 "__Block_byref_id_object_dispose_",
Mike Stumpee2a5ee2009-03-06 02:29:21 +00001215 &CGM.getModule());
1216
1217 IdentifierInfo *II
1218 = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
1219
1220 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1221 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00001222 SourceLocation(), II, R, 0,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001223 FunctionDecl::Static,
1224 FunctionDecl::None,
1225 false, true);
Mike Stumpee2a5ee2009-03-06 02:29:21 +00001226 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpfbe25dd2009-03-06 04:53:30 +00001227
1228 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
Owen Anderson9793f0e2009-07-29 22:16:19 +00001229 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stump3b65ac22009-04-15 22:11:36 +00001230 V = Builder.CreateLoad(V);
Mike Stumpfbe25dd2009-03-06 04:53:30 +00001231 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson9793f0e2009-07-29 22:16:19 +00001232 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stump3b65ac22009-04-15 22:11:36 +00001233 V = Builder.CreateLoad(V);
Mike Stumpfbe25dd2009-03-06 04:53:30 +00001234
Mike Stumpfbe25dd2009-03-06 04:53:30 +00001235 flag |= BLOCK_BYREF_CALLER;
1236 BuildBlockRelease(V, flag);
Mike Stumpee2a5ee2009-03-06 02:29:21 +00001237 CGF.FinishFunction();
1238
Owen Andersonade90fd2009-07-29 18:54:39 +00001239 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpee2a5ee2009-03-06 02:29:21 +00001240}
1241
Mike Stumpf89230d2009-03-06 06:12:24 +00001242llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
Chris Lattnere08ea782009-12-05 08:21:30 +00001243 int Flag, unsigned Align) {
1244 // All alignments below that of pointer alignment collapse down to just
Mike Stumpcbc2bca2009-06-05 23:26:36 +00001245 // pointer alignment, as we always have at least that much alignment to begin
1246 // with.
1247 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattnere08ea782009-12-05 08:21:30 +00001248
Mike Stumpcbc2bca2009-06-05 23:26:36 +00001249 // As an optimization, we only generate a single function of each kind we
1250 // might need. We need a different one for each alignment and for each
1251 // setting of flags. We mix Align and flag to get the kind.
Chris Lattnere08ea782009-12-05 08:21:30 +00001252 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1253 llvm::Constant *&Entry = CGM.AssignCache[Kind];
Mike Stumpcbc2bca2009-06-05 23:26:36 +00001254 if (Entry)
1255 return Entry;
Chris Lattnere08ea782009-12-05 08:21:30 +00001256 return Entry = CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, Flag);
Mike Stumpee2a5ee2009-03-06 02:29:21 +00001257}
1258
Mike Stumpfbe25dd2009-03-06 04:53:30 +00001259llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
Chris Lattnere08ea782009-12-05 08:21:30 +00001260 int Flag,
Mike Stumpcbc2bca2009-06-05 23:26:36 +00001261 unsigned Align) {
1262 // All alignments below that of pointer alignment collpase down to just
1263 // pointer alignment, as we always have at least that much alignment to begin
1264 // with.
1265 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattnere08ea782009-12-05 08:21:30 +00001266
Mike Stumpcbc2bca2009-06-05 23:26:36 +00001267 // As an optimization, we only generate a single function of each kind we
1268 // might need. We need a different one for each alignment and for each
1269 // setting of flags. We mix Align and flag to get the kind.
Chris Lattnere08ea782009-12-05 08:21:30 +00001270 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1271 llvm::Constant *&Entry = CGM.DestroyCache[Kind];
Mike Stumpcbc2bca2009-06-05 23:26:36 +00001272 if (Entry)
1273 return Entry;
Chris Lattnere08ea782009-12-05 08:21:30 +00001274 return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, Flag);
Mike Stumpee2a5ee2009-03-06 02:29:21 +00001275}
1276
Mike Stump626aecc2009-03-05 01:23:13 +00001277llvm::Value *BlockFunction::getBlockObjectDispose() {
1278 if (CGM.BlockObjectDispose == 0) {
1279 const llvm::FunctionType *FTy;
1280 std::vector<const llvm::Type*> ArgTys;
Owen Anderson41a75022009-08-13 21:57:51 +00001281 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stump626aecc2009-03-05 01:23:13 +00001282 ArgTys.push_back(PtrToInt8Ty);
Chris Lattner5e016ae2010-06-27 07:15:29 +00001283 ArgTys.push_back(CGF.Int32Ty);
Owen Anderson9793f0e2009-07-29 22:16:19 +00001284 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stump4446dcf2009-03-05 08:32:30 +00001285 CGM.BlockObjectDispose
Mike Stump626aecc2009-03-05 01:23:13 +00001286 = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
1287 }
1288 return CGM.BlockObjectDispose;
1289}
1290
Mike Stumpf89230d2009-03-06 06:12:24 +00001291llvm::Value *BlockFunction::getBlockObjectAssign() {
1292 if (CGM.BlockObjectAssign == 0) {
1293 const llvm::FunctionType *FTy;
1294 std::vector<const llvm::Type*> ArgTys;
Owen Anderson41a75022009-08-13 21:57:51 +00001295 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stumpf89230d2009-03-06 06:12:24 +00001296 ArgTys.push_back(PtrToInt8Ty);
1297 ArgTys.push_back(PtrToInt8Ty);
Chris Lattner5e016ae2010-06-27 07:15:29 +00001298 ArgTys.push_back(CGF.Int32Ty);
Owen Anderson9793f0e2009-07-29 22:16:19 +00001299 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stumpf89230d2009-03-06 06:12:24 +00001300 CGM.BlockObjectAssign
1301 = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
1302 }
1303 return CGM.BlockObjectAssign;
1304}
1305
Mike Stumpfbe25dd2009-03-06 04:53:30 +00001306void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Mike Stump626aecc2009-03-05 01:23:13 +00001307 llvm::Value *F = getBlockObjectDispose();
Mike Stumpfbe25dd2009-03-06 04:53:30 +00001308 llvm::Value *N;
Mike Stump626aecc2009-03-05 01:23:13 +00001309 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Chris Lattner5e016ae2010-06-27 07:15:29 +00001310 N = llvm::ConstantInt::get(CGF.Int32Ty, flag);
Mike Stump626aecc2009-03-05 01:23:13 +00001311 Builder.CreateCall2(F, V, N);
1312}
Mike Stump4446dcf2009-03-05 08:32:30 +00001313
1314ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stumpaeb0ffd2009-03-07 02:35:30 +00001315
1316BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1317 CGBuilderTy &B)
Chris Lattner5e016ae2010-06-27 07:15:29 +00001318 : CGM(cgm), VMContext(cgm.getLLVMContext()), CGF(cgf), Builder(B) {
Owen Anderson41a75022009-08-13 21:57:51 +00001319 PtrToInt8Ty = llvm::PointerType::getUnqual(
1320 llvm::Type::getInt8Ty(VMContext));
Mike Stumpaeb0ffd2009-03-07 02:35:30 +00001321
1322 BlockHasCopyDispose = false;
1323}