blob: a876938c02e95e5afbd7d75609d0760687ba8406 [file] [log] [blame]
Anders Carlssond2a889b2009-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
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
16#include "llvm/Module.h"
Anders Carlsson1f1cd392009-02-12 17:55:02 +000017#include "llvm/Target/TargetData.h"
Anders Carlssond2a889b2009-02-12 00:39:25 +000018
19#include <algorithm>
20
21using namespace clang;
22using namespace CodeGen;
23
Mike Stumpb3a6fac2009-03-04 13:17:22 +000024// Temporary code to enable testing of __block variables
25// #include "clang/Frontend/CompileOptions.h"
26#include "llvm/Support/CommandLine.h"
27static llvm::cl::opt<bool>
28Enable__block("f__block",
29 // See all the FIXMEs for the various work that needs to be done
30 llvm::cl::desc("temporary option to turn on __block precessing "
31 "even though the code isn't done yet"),
32 llvm::cl::ValueDisallowed, llvm::cl::AllowInverse,
Mike Stump1bdbf632009-03-05 08:32:30 +000033 llvm::cl::ZeroOrMore,
Mike Stumpb0fa8b52009-03-07 06:16:52 +000034 llvm::cl::init(true));
Mike Stumpb3a6fac2009-03-04 13:17:22 +000035
Mike Stump9d8c1262009-03-06 18:42:23 +000036llvm::Constant *CodeGenFunction::
Mike Stump1fa52fe2009-03-07 02:35:30 +000037BuildDescriptorBlockDecl(uint64_t Size, const llvm::StructType* Ty,
38 std::vector<HelperInfo> *NoteForHelper) {
Mike Stumpff8f0872009-02-13 16:55:51 +000039 const llvm::Type *UnsignedLongTy
40 = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpb95bc002009-02-13 16:19:19 +000041 llvm::Constant *C;
42 std::vector<llvm::Constant*> Elts;
43
44 // reserved
Mike Stumpff8f0872009-02-13 16:55:51 +000045 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpb95bc002009-02-13 16:19:19 +000046 Elts.push_back(C);
47
48 // Size
Mike Stump139c3962009-02-21 20:07:44 +000049 // FIXME: What is the right way to say this doesn't fit? We should give
50 // a user diagnostic in that case. Better fix would be to change the
51 // API to size_t.
Mike Stumpfca5da02009-02-21 20:00:35 +000052 C = llvm::ConstantInt::get(UnsignedLongTy, Size);
Mike Stumpb95bc002009-02-13 16:19:19 +000053 Elts.push_back(C);
54
55 if (BlockHasCopyDispose) {
56 // copy_func_helper_decl
Mike Stump1fa52fe2009-03-07 02:35:30 +000057 Elts.push_back(BuildCopyHelper(Ty, *NoteForHelper));
Mike Stumpb95bc002009-02-13 16:19:19 +000058
59 // destroy_func_decl
Mike Stump1fa52fe2009-03-07 02:35:30 +000060 Elts.push_back(BuildDestroyHelper(Ty, *NoteForHelper));
Mike Stumpb95bc002009-02-13 16:19:19 +000061 }
62
63 C = llvm::ConstantStruct::get(Elts);
64
Mike Stumpb95bc002009-02-13 16:19:19 +000065 C = new llvm::GlobalVariable(C->getType(), true,
66 llvm::GlobalValue::InternalLinkage,
Mike Stump92ea8882009-02-13 20:17:16 +000067 C, "__block_descriptor_tmp", &CGM.getModule());
Mike Stumpb95bc002009-02-13 16:19:19 +000068 return C;
69}
70
Mike Stump1f010b52009-03-04 18:17:45 +000071llvm::Constant *BlockModule::getNSConcreteGlobalBlock() {
Mike Stump5f87e9d2009-02-13 17:23:42 +000072 if (NSConcreteGlobalBlock)
73 return NSConcreteGlobalBlock;
74
Mike Stump56447902009-02-13 19:38:12 +000075 // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
Mike Stumpf13eac12009-03-04 22:48:06 +000076 // same thing as CreateRuntimeFunction if there's already a variable with the
77 // same name.
Mike Stump5f87e9d2009-02-13 17:23:42 +000078 NSConcreteGlobalBlock
Mike Stump1bdbf632009-03-05 08:32:30 +000079 = new llvm::GlobalVariable(PtrToInt8Ty, false,
Mike Stump5f87e9d2009-02-13 17:23:42 +000080 llvm::GlobalValue::ExternalLinkage,
81 0, "_NSConcreteGlobalBlock",
82 &getModule());
83
84 return NSConcreteGlobalBlock;
85}
86
Mike Stump1f010b52009-03-04 18:17:45 +000087llvm::Constant *BlockModule::getNSConcreteStackBlock() {
Mike Stumpeb3a5ca2009-02-13 19:29:27 +000088 if (NSConcreteStackBlock)
89 return NSConcreteStackBlock;
90
Mike Stump56447902009-02-13 19:38:12 +000091 // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
Mike Stumpf13eac12009-03-04 22:48:06 +000092 // same thing as CreateRuntimeFunction if there's already a variable with the
93 // same name.
Mike Stumpeb3a5ca2009-02-13 19:29:27 +000094 NSConcreteStackBlock
Mike Stump1bdbf632009-03-05 08:32:30 +000095 = new llvm::GlobalVariable(PtrToInt8Ty, false,
Mike Stumpeb3a5ca2009-02-13 19:29:27 +000096 llvm::GlobalValue::ExternalLinkage,
97 0, "_NSConcreteStackBlock",
98 &getModule());
99
100 return NSConcreteStackBlock;
101}
102
Mike Stump1bdbf632009-03-05 08:32:30 +0000103static void CollectBlockDeclRefInfo(const Stmt *S,
Anders Carlsson6cf64be2009-03-01 01:09:12 +0000104 CodeGenFunction::BlockInfo &Info) {
105 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
106 I != E; ++I)
Daniel Dunbar56941d32009-03-02 07:00:57 +0000107 if (*I)
108 CollectBlockDeclRefInfo(*I, Info);
Mike Stump1bdbf632009-03-05 08:32:30 +0000109
Anders Carlsson6cf64be2009-03-01 01:09:12 +0000110 if (const BlockDeclRefExpr *DE = dyn_cast<BlockDeclRefExpr>(S)) {
111 // FIXME: Handle enums.
112 if (isa<FunctionDecl>(DE->getDecl()))
113 return;
Mike Stump1bdbf632009-03-05 08:32:30 +0000114
Anders Carlsson6cf64be2009-03-01 01:09:12 +0000115 if (DE->isByRef())
116 Info.ByRefDeclRefs.push_back(DE);
117 else
118 Info.ByCopyDeclRefs.push_back(DE);
119 }
120}
121
Mike Stumpf13eac12009-03-04 22:48:06 +0000122/// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be
123/// declared as a global variable instead of on the stack.
Anders Carlsson6cf64be2009-03-01 01:09:12 +0000124static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info)
125{
126 return Info.ByRefDeclRefs.empty() && Info.ByCopyDeclRefs.empty();
127}
128
Mike Stumpf13eac12009-03-04 22:48:06 +0000129// FIXME: Push most into CGM, passing down a few bits, like current function
130// name.
Mike Stumpf1711822009-02-25 23:33:13 +0000131llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Mike Stumpb95bc002009-02-13 16:19:19 +0000132
Anders Carlsson6cf64be2009-03-01 01:09:12 +0000133 std::string Name = CurFn->getName();
134 CodeGenFunction::BlockInfo Info(0, Name.c_str());
135 CollectBlockDeclRefInfo(BE->getBody(), Info);
136
137 // Check if the block can be global.
Mike Stumpf13eac12009-03-04 22:48:06 +0000138 // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like
139 // to just have one code path. We should move this function into CGM and pass
140 // CGF, then we can just check to see if CGF is 0.
Mike Stumpad9605d2009-03-04 03:23:46 +0000141 if (0 && CanBlockBeGlobal(Info))
Anders Carlsson6cf64be2009-03-01 01:09:12 +0000142 return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
Mike Stump1bdbf632009-03-05 08:32:30 +0000143
144 std::vector<llvm::Constant*> Elts(5);
Mike Stumpb95bc002009-02-13 16:19:19 +0000145 llvm::Constant *C;
Mike Stumpf1711822009-02-25 23:33:13 +0000146 llvm::Value *V;
Mike Stumpb95bc002009-02-13 16:19:19 +0000147
Mike Stumpb95bc002009-02-13 16:19:19 +0000148 {
149 // C = BuildBlockStructInitlist();
150 unsigned int flags = BLOCK_HAS_DESCRIPTOR;
151
Mike Stump1bdbf632009-03-05 08:32:30 +0000152 // We run this first so that we set BlockHasCopyDispose from the entire
153 // block literal.
154 // __invoke
155 uint64_t subBlockSize, subBlockAlign;
156 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
157 llvm::Function *Fn
158 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, subBlockSize,
159 subBlockAlign,
160 subBlockDeclRefDecls,
161 BlockHasCopyDispose);
162 Elts[3] = Fn;
163
164 if (!Enable__block && BlockHasCopyDispose)
165 ErrorUnsupported(BE, "block literal that requires copy/dispose");
166
Mike Stumpb95bc002009-02-13 16:19:19 +0000167 if (BlockHasCopyDispose)
168 flags |= BLOCK_HAS_COPY_DISPOSE;
169
Mike Stump92ea8882009-02-13 20:17:16 +0000170 // __isa
Mike Stumpeb3a5ca2009-02-13 19:29:27 +0000171 C = CGM.getNSConcreteStackBlock();
Mike Stumpb95bc002009-02-13 16:19:19 +0000172 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
Mike Stump1bdbf632009-03-05 08:32:30 +0000173 Elts[0] = C;
Mike Stumpb95bc002009-02-13 16:19:19 +0000174
175 // __flags
176 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
177 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
178 C = llvm::ConstantInt::get(IntTy, flags);
Mike Stump1bdbf632009-03-05 08:32:30 +0000179 Elts[1] = C;
Mike Stumpb95bc002009-02-13 16:19:19 +0000180
181 // __reserved
182 C = llvm::ConstantInt::get(IntTy, 0);
Mike Stump1bdbf632009-03-05 08:32:30 +0000183 Elts[2] = C;
Mike Stumpb95bc002009-02-13 16:19:19 +0000184
Mike Stumpf1711822009-02-25 23:33:13 +0000185 if (subBlockDeclRefDecls.size() == 0) {
Mike Stump9d8c1262009-03-06 18:42:23 +0000186 // __descriptor
Mike Stump1fa52fe2009-03-07 02:35:30 +0000187 Elts[4] = BuildDescriptorBlockDecl(subBlockSize, 0, 0);
Mike Stump9d8c1262009-03-06 18:42:23 +0000188
Mike Stumpa7db9be2009-03-01 20:07:53 +0000189 // Optimize to being a global block.
190 Elts[0] = CGM.getNSConcreteGlobalBlock();
191 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
192
Mike Stumpf1711822009-02-25 23:33:13 +0000193 C = llvm::ConstantStruct::get(Elts);
194
195 char Name[32];
196 sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
197 C = new llvm::GlobalVariable(C->getType(), true,
198 llvm::GlobalValue::InternalLinkage,
199 C, Name, &CGM.getModule());
200 QualType BPT = BE->getType();
201 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
202 return C;
203 }
Mike Stump1bdbf632009-03-05 08:32:30 +0000204
Mike Stumpf1711822009-02-25 23:33:13 +0000205 std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size());
Mike Stump1fa52fe2009-03-07 02:35:30 +0000206 for (int i=0; i<4; ++i)
Mike Stumpf1711822009-02-25 23:33:13 +0000207 Types[i] = Elts[i]->getType();
Mike Stump1fa52fe2009-03-07 02:35:30 +0000208 Types[4] = PtrToInt8Ty;
Mike Stumpf1711822009-02-25 23:33:13 +0000209
Mike Stump2b6933f2009-02-28 09:07:16 +0000210 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
211 const Expr *E = subBlockDeclRefDecls[i];
212 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
213 QualType Ty = E->getType();
Mike Stumpad9605d2009-03-04 03:23:46 +0000214 if (BDRE && BDRE->isByRef()) {
215 uint64_t Align = getContext().getDeclAlignInBytes(BDRE->getDecl());
216 Types[i+5] = llvm::PointerType::get(BuildByRefType(Ty, Align), 0);
217 } else
218 Types[i+5] = ConvertType(Ty);
Mike Stump2b6933f2009-02-28 09:07:16 +0000219 }
Mike Stumpf1711822009-02-25 23:33:13 +0000220
Mike Stump1fa52fe2009-03-07 02:35:30 +0000221 llvm::StructType *Ty = llvm::StructType::get(Types, true);
Mike Stumpf1711822009-02-25 23:33:13 +0000222
223 llvm::AllocaInst *A = CreateTempAlloca(Ty);
224 A->setAlignment(subBlockAlign);
225 V = A;
226
Mike Stump1fa52fe2009-03-07 02:35:30 +0000227 std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size());
228 int helpersize = 0;
229
230 for (unsigned i=0; i<4; ++i)
Mike Stumpf1711822009-02-25 23:33:13 +0000231 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
Mike Stump1bdbf632009-03-05 08:32:30 +0000232
Mike Stumpf1711822009-02-25 23:33:13 +0000233 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
234 {
Mike Stump2b6933f2009-02-28 09:07:16 +0000235 // FIXME: Push const down.
236 Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
237 DeclRefExpr *DR;
238 ValueDecl *VD;
Mike Stumpf1711822009-02-25 23:33:13 +0000239
Mike Stump2b6933f2009-02-28 09:07:16 +0000240 DR = dyn_cast<DeclRefExpr>(E);
241 // Skip padding.
242 if (DR) continue;
243
244 BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
245 VD = BDRE->getDecl();
246
Mike Stumpad9605d2009-03-04 03:23:46 +0000247 llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp");
Mike Stump1fa52fe2009-03-07 02:35:30 +0000248 NoteForHelper[helpersize].index = i+5;
249 NoteForHelper[helpersize].RequiresCopying = BlockRequiresCopying(VD->getType());
250 NoteForHelper[helpersize].flag
251 = VD->getType()->isBlockPointerType() ? BLOCK_FIELD_IS_BLOCK : BLOCK_FIELD_IS_OBJECT;
252
Mike Stump2b6933f2009-02-28 09:07:16 +0000253 if (LocalDeclMap[VD]) {
Mike Stumpad9605d2009-03-04 03:23:46 +0000254 if (BDRE->isByRef()) {
Mike Stump1fa52fe2009-03-07 02:35:30 +0000255 // FIXME: For only local, or all byrefs?
256 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
Mike Stump0a33ed32009-03-07 06:04:31 +0000257 // FIXME: Someone double check this.
258 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpad9605d2009-03-04 03:23:46 +0000259 const llvm::Type *Ty = Types[i+5];
260 llvm::Value *Loc = LocalDeclMap[VD];
261 Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
262 Loc = Builder.CreateLoad(Loc, false);
263 Loc = Builder.CreateBitCast(Loc, Ty);
264 Builder.CreateStore(Loc, Addr);
Mike Stump1fa52fe2009-03-07 02:35:30 +0000265 ++helpersize;
Mike Stumpad9605d2009-03-04 03:23:46 +0000266 continue;
267 } else
268 E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD),
269 VD->getType(), SourceLocation(),
270 false, false);
Mike Stump2b6933f2009-02-28 09:07:16 +0000271 }
Mike Stumpad9605d2009-03-04 03:23:46 +0000272 if (BDRE->isByRef()) {
Mike Stump2b6933f2009-02-28 09:07:16 +0000273 E = new (getContext())
274 UnaryOperator(E, UnaryOperator::AddrOf,
275 getContext().getPointerType(E->getType()),
276 SourceLocation());
Mike Stumpad9605d2009-03-04 03:23:46 +0000277 }
Mike Stump1fa52fe2009-03-07 02:35:30 +0000278 ++helpersize;
Mike Stump2b6933f2009-02-28 09:07:16 +0000279
Mike Stump2b6933f2009-02-28 09:07:16 +0000280 RValue r = EmitAnyExpr(E, Addr, false);
Mike Stumpad9605d2009-03-04 03:23:46 +0000281 if (r.isScalar()) {
282 llvm::Value *Loc = r.getScalarVal();
283 const llvm::Type *Ty = Types[i+5];
284 if (BDRE->isByRef()) {
Mike Stumpf13eac12009-03-04 22:48:06 +0000285 // E is now the address of the value field, instead, we want the
286 // address of the actual ByRef struct. We optimize this slightly
287 // compared to gcc by not grabbing the forwarding slot as this must
288 // be done during Block_copy for us, and we can postpone the work
289 // until then.
290 uint64_t offset = BlockDecls[BDRE->getDecl()];
Mike Stump1bdbf632009-03-05 08:32:30 +0000291
Mike Stumpf13eac12009-03-04 22:48:06 +0000292 llvm::Value *BlockLiteral = LoadBlockStruct();
Mike Stump1bdbf632009-03-05 08:32:30 +0000293
Mike Stumpf13eac12009-03-04 22:48:06 +0000294 Loc = Builder.CreateGEP(BlockLiteral,
295 llvm::ConstantInt::get(llvm::Type::Int64Ty,
296 offset),
297 "block.literal");
298 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpad9605d2009-03-04 03:23:46 +0000299 Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stumpf13eac12009-03-04 22:48:06 +0000300 Loc = Builder.CreateLoad(Loc, false);
301 // Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stumpad9605d2009-03-04 03:23:46 +0000302 }
303 Builder.CreateStore(Loc, Addr);
304 } else if (r.isComplex())
Mike Stumpf1711822009-02-25 23:33:13 +0000305 // FIXME: implement
306 ErrorUnsupported(BE, "complex in block literal");
307 else if (r.isAggregate())
308 ; // Already created into the destination
309 else
310 assert (0 && "bad block variable");
311 // FIXME: Ensure that the offset created by the backend for
312 // the struct matches the previously computed offset in BlockDecls.
313 }
Mike Stump1fa52fe2009-03-07 02:35:30 +0000314 NoteForHelper.resize(helpersize);
Mike Stump9d8c1262009-03-06 18:42:23 +0000315
316 // __descriptor
Mike Stump1fa52fe2009-03-07 02:35:30 +0000317 llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockSize, Ty,
318 &NoteForHelper);
319 Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
320 Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
Mike Stumpb95bc002009-02-13 16:19:19 +0000321 }
Mike Stump1bdbf632009-03-05 08:32:30 +0000322
Mike Stumpd55240e2009-02-19 01:01:04 +0000323 QualType BPT = BE->getType();
Mike Stumpf1711822009-02-25 23:33:13 +0000324 return Builder.CreateBitCast(V, ConvertType(BPT));
Mike Stumpb95bc002009-02-13 16:19:19 +0000325}
326
327
Mike Stump1f010b52009-03-04 18:17:45 +0000328const llvm::Type *BlockModule::getBlockDescriptorType() {
Mike Stump95e54802009-02-13 15:16:56 +0000329 if (BlockDescriptorType)
330 return BlockDescriptorType;
331
Mike Stumpc4ae9632009-02-13 15:32:32 +0000332 const llvm::Type *UnsignedLongTy =
Mike Stump95e54802009-02-13 15:16:56 +0000333 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000334
Mike Stump95e54802009-02-13 15:16:56 +0000335 // struct __block_descriptor {
336 // unsigned long reserved;
337 // unsigned long block_size;
338 // };
Mike Stumpc4ae9632009-02-13 15:32:32 +0000339 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy,
340 UnsignedLongTy,
Mike Stump95e54802009-02-13 15:16:56 +0000341 NULL);
342
343 getModule().addTypeName("struct.__block_descriptor",
344 BlockDescriptorType);
345
346 return BlockDescriptorType;
Anders Carlssond2a889b2009-02-12 00:39:25 +0000347}
348
Mike Stump1f010b52009-03-04 18:17:45 +0000349const llvm::Type *BlockModule::getGenericBlockLiteralType() {
Mike Stump0dffa462009-02-13 15:25:34 +0000350 if (GenericBlockLiteralType)
351 return GenericBlockLiteralType;
352
Mike Stumpc4ae9632009-02-13 15:32:32 +0000353 const llvm::Type *BlockDescPtrTy =
Mike Stump0dffa462009-02-13 15:25:34 +0000354 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000355
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000356 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
357 getTypes().ConvertType(getContext().IntTy));
358
Mike Stump0dffa462009-02-13 15:25:34 +0000359 // struct __block_literal_generic {
Mike Stumpd55240e2009-02-19 01:01:04 +0000360 // void *__isa;
361 // int __flags;
362 // int __reserved;
363 // void (*__invoke)(void *);
364 // struct __block_descriptor *__descriptor;
Mike Stump0dffa462009-02-13 15:25:34 +0000365 // };
Mike Stump60294662009-03-05 01:23:13 +0000366 GenericBlockLiteralType = llvm::StructType::get(PtrToInt8Ty,
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000367 IntTy,
368 IntTy,
Mike Stump60294662009-03-05 01:23:13 +0000369 PtrToInt8Ty,
Mike Stump0dffa462009-02-13 15:25:34 +0000370 BlockDescPtrTy,
371 NULL);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000372
Mike Stump0dffa462009-02-13 15:25:34 +0000373 getModule().addTypeName("struct.__block_literal_generic",
374 GenericBlockLiteralType);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000375
Mike Stump0dffa462009-02-13 15:25:34 +0000376 return GenericBlockLiteralType;
Anders Carlssond2a889b2009-02-12 00:39:25 +0000377}
378
Mike Stump1f010b52009-03-04 18:17:45 +0000379const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() {
Mike Stumpd55240e2009-02-19 01:01:04 +0000380 if (GenericExtendedBlockLiteralType)
381 return GenericExtendedBlockLiteralType;
382
Mike Stumpd55240e2009-02-19 01:01:04 +0000383 const llvm::Type *BlockDescPtrTy =
384 llvm::PointerType::getUnqual(getBlockDescriptorType());
385
386 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
387 getTypes().ConvertType(getContext().IntTy));
388
389 // struct __block_literal_generic {
390 // void *__isa;
391 // int __flags;
392 // int __reserved;
393 // void (*__invoke)(void *);
394 // struct __block_descriptor *__descriptor;
395 // void *__copy_func_helper_decl;
396 // void *__destroy_func_decl;
397 // };
Mike Stump60294662009-03-05 01:23:13 +0000398 GenericExtendedBlockLiteralType = llvm::StructType::get(PtrToInt8Ty,
Mike Stumpd55240e2009-02-19 01:01:04 +0000399 IntTy,
400 IntTy,
Mike Stump60294662009-03-05 01:23:13 +0000401 PtrToInt8Ty,
Mike Stumpd55240e2009-02-19 01:01:04 +0000402 BlockDescPtrTy,
Mike Stump60294662009-03-05 01:23:13 +0000403 PtrToInt8Ty,
404 PtrToInt8Ty,
Mike Stumpd55240e2009-02-19 01:01:04 +0000405 NULL);
406
407 getModule().addTypeName("struct.__block_literal_extended_generic",
408 GenericExtendedBlockLiteralType);
409
410 return GenericExtendedBlockLiteralType;
411}
412
Mike Stumpc4ae9632009-02-13 15:32:32 +0000413/// getBlockFunctionType - Given a BlockPointerType, will return the
Anders Carlssond2a889b2009-02-12 00:39:25 +0000414/// function type for the block, including the first block literal argument.
415static QualType getBlockFunctionType(ASTContext &Ctx,
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000416 const BlockPointerType *BPT) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000417 const FunctionProtoType *FTy = cast<FunctionProtoType>(BPT->getPointeeType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000418
Anders Carlssond2a889b2009-02-12 00:39:25 +0000419 llvm::SmallVector<QualType, 8> Types;
420 Types.push_back(Ctx.getPointerType(Ctx.VoidTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000421
Douglas Gregor4fa58902009-02-26 23:50:07 +0000422 for (FunctionProtoType::arg_type_iterator i = FTy->arg_type_begin(),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000423 e = FTy->arg_type_end(); i != e; ++i)
424 Types.push_back(*i);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000425
Anders Carlssond2a889b2009-02-12 00:39:25 +0000426 return Ctx.getFunctionType(FTy->getResultType(),
Mike Stumpc4ae9632009-02-13 15:32:32 +0000427 &Types[0], Types.size(),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000428 FTy->isVariadic(), 0);
429}
430
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000431RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpc4ae9632009-02-13 15:32:32 +0000432 const BlockPointerType *BPT =
Anders Carlssond2a889b2009-02-12 00:39:25 +0000433 E->getCallee()->getType()->getAsBlockPointerType();
Mike Stumpc4ae9632009-02-13 15:32:32 +0000434
Anders Carlssond2a889b2009-02-12 00:39:25 +0000435 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
436
437 // Get a pointer to the generic block literal.
438 const llvm::Type *BlockLiteralTy =
Mike Stump0dffa462009-02-13 15:25:34 +0000439 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssond2a889b2009-02-12 00:39:25 +0000440
441 // Bitcast the callee to a block literal.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000442 llvm::Value *BlockLiteral =
Anders Carlssond2a889b2009-02-12 00:39:25 +0000443 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
444
445 // Get the function pointer from the literal.
446 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Mike Stump39bcc612009-02-22 13:27:11 +0000447 llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp");
Anders Carlssond2a889b2009-02-12 00:39:25 +0000448
449 // Cast the function pointer to the right type.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000450 const llvm::Type *BlockFTy =
Anders Carlssond2a889b2009-02-12 00:39:25 +0000451 ConvertType(getBlockFunctionType(getContext(), BPT));
452 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
453 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
454
Mike Stumpc4ae9632009-02-13 15:32:32 +0000455 BlockLiteral =
456 Builder.CreateBitCast(BlockLiteral,
Anders Carlssond2a889b2009-02-12 00:39:25 +0000457 llvm::PointerType::getUnqual(llvm::Type::Int8Ty),
458 "tmp");
Mike Stumpc4ae9632009-02-13 15:32:32 +0000459
Anders Carlssond2a889b2009-02-12 00:39:25 +0000460 // Add the block literal.
461 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
462 CallArgList Args;
463 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000464
Anders Carlssond2a889b2009-02-12 00:39:25 +0000465 // And the rest of the arguments.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000466 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
Anders Carlssond2a889b2009-02-12 00:39:25 +0000467 i != e; ++i)
Mike Stumpc4ae9632009-02-13 15:32:32 +0000468 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000469 i->getType()));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000470
Anders Carlssond2a889b2009-02-12 00:39:25 +0000471 // And call the block.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000472 return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000473 Func, Args);
474}
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000475
Mike Stumpad9605d2009-03-04 03:23:46 +0000476llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
477 uint64_t &offset = BlockDecls[E->getDecl()];
478
479 const llvm::Type *Ty;
480 Ty = CGM.getTypes().ConvertType(E->getDecl()->getType());
481
Mike Stumpb3a6fac2009-03-04 13:17:22 +0000482 if (!Enable__block && E->isByRef())
Mike Stumpad9605d2009-03-04 03:23:46 +0000483 ErrorUnsupported(E, "__block variable in block literal");
Mike Stumpecd79422009-03-06 01:33:24 +0000484 else if (!Enable__block && E->getType()->isBlockPointerType())
Mike Stumpad9605d2009-03-04 03:23:46 +0000485 ErrorUnsupported(E, "block pointer in block literal");
Mike Stumpe43c54a2009-03-07 14:53:10 +0000486 else if (!Enable__block && (E->getDecl()->getAttr<ObjCNSObjectAttr>() ||
487 getContext().isObjCNSObjectType(E->getType())))
Mike Stumpad9605d2009-03-04 03:23:46 +0000488 ErrorUnsupported(E, "__attribute__((NSObject)) variable in block "
489 "literal");
Mike Stumpecd79422009-03-06 01:33:24 +0000490 else if (!Enable__block && getContext().isObjCObjectPointerType(E->getType()))
Mike Stumpad9605d2009-03-04 03:23:46 +0000491 ErrorUnsupported(E, "Objective-C variable in block literal");
492
493 // See if we have already allocated an offset for this variable.
494 if (offset == 0) {
Mike Stump1bdbf632009-03-05 08:32:30 +0000495 // Don't run the expensive check, unless we have to.
496 if (!BlockHasCopyDispose && BlockRequiresCopying(E->getType()))
497 BlockHasCopyDispose = true;
Mike Stumpad9605d2009-03-04 03:23:46 +0000498 // if not, allocate one now.
499 offset = getBlockOffset(E);
500 }
501
502 llvm::Value *BlockLiteral = LoadBlockStruct();
503 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
504 llvm::ConstantInt::get(llvm::Type::Int64Ty,
505 offset),
Mike Stumpf13eac12009-03-04 22:48:06 +0000506 "block.literal");
Mike Stumpad9605d2009-03-04 03:23:46 +0000507 if (E->isByRef()) {
508 bool needsCopyDispose = BlockRequiresCopying(E->getType());
509 uint64_t Align = getContext().getDeclAlignInBytes(E->getDecl());
510 const llvm::Type *PtrStructTy
511 = llvm::PointerType::get(BuildByRefType(E->getType(), Align), 0);
512 Ty = PtrStructTy;
513 Ty = llvm::PointerType::get(Ty, 0);
514 V = Builder.CreateBitCast(V, Ty);
515 V = Builder.CreateLoad(V, false);
516 V = Builder.CreateStructGEP(V, 1, "forwarding");
517 V = Builder.CreateLoad(V, false);
518 V = Builder.CreateBitCast(V, PtrStructTy);
519 V = Builder.CreateStructGEP(V, needsCopyDispose*2 + 4, "x");
520 } else {
521 Ty = llvm::PointerType::get(Ty, 0);
522 V = Builder.CreateBitCast(V, Ty);
523 }
524 return V;
525}
526
Mike Stump084ba462009-02-14 22:16:35 +0000527llvm::Constant *
Mike Stumpd6d0ebe2009-03-04 18:47:42 +0000528BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000529 // Generate the block descriptor.
530 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000531 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
532 getTypes().ConvertType(getContext().IntTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000533
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000534 llvm::Constant *DescriptorFields[2];
Mike Stumpc4ae9632009-02-13 15:32:32 +0000535
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000536 // Reserved
537 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000538
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000539 // Block literal size. For global blocks we just use the size of the generic
540 // block literal struct.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000541 uint64_t BlockLiteralSize =
Mike Stump0dffa462009-02-13 15:25:34 +0000542 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000543 DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000544
545 llvm::Constant *DescriptorStruct =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000546 llvm::ConstantStruct::get(&DescriptorFields[0], 2);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000547
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000548 llvm::GlobalVariable *Descriptor =
549 new llvm::GlobalVariable(DescriptorStruct->getType(), true,
Mike Stumpc4ae9632009-02-13 15:32:32 +0000550 llvm::GlobalVariable::InternalLinkage,
551 DescriptorStruct, "__block_descriptor_global",
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000552 &getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000553
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000554 // Generate the constants for the block literal.
555 llvm::Constant *LiteralFields[5];
Mike Stumpc4ae9632009-02-13 15:32:32 +0000556
Mike Stump084ba462009-02-14 22:16:35 +0000557 CodeGenFunction::BlockInfo Info(0, n);
Mike Stumpf1711822009-02-25 23:33:13 +0000558 uint64_t subBlockSize, subBlockAlign;
Mike Stump2b6933f2009-02-28 09:07:16 +0000559 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Daniel Dunbaradaf35f2009-03-12 03:07:24 +0000560 bool subBlockHasCopyDispose = false;
Mike Stumpfca5da02009-02-21 20:00:35 +0000561 llvm::Function *Fn
Mike Stumpd6d0ebe2009-03-04 18:47:42 +0000562 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, subBlockSize,
563 subBlockAlign,
Mike Stump1bdbf632009-03-05 08:32:30 +0000564 subBlockDeclRefDecls,
565 subBlockHasCopyDispose);
Mike Stumpfca5da02009-02-21 20:00:35 +0000566 assert(subBlockSize == BlockLiteralSize
567 && "no imports allowed for global block");
Daniel Dunbaradaf35f2009-03-12 03:07:24 +0000568 assert(!subBlockHasCopyDispose && "no imports allowed for global block");
Mike Stumpc4ae9632009-02-13 15:32:32 +0000569
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000570 // isa
Mike Stump5f87e9d2009-02-13 17:23:42 +0000571 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpc4ae9632009-02-13 15:32:32 +0000572
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000573 // Flags
Mike Stump1bdbf632009-03-05 08:32:30 +0000574 LiteralFields[1] =
Anders Carlsson63810c62009-03-01 21:09:29 +0000575 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000576
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000577 // Reserved
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000578 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000579
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000580 // Function
581 LiteralFields[3] = Fn;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000582
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000583 // Descriptor
584 LiteralFields[4] = Descriptor;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000585
586 llvm::Constant *BlockLiteralStruct =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000587 llvm::ConstantStruct::get(&LiteralFields[0], 5);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000588
589 llvm::GlobalVariable *BlockLiteral =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000590 new llvm::GlobalVariable(BlockLiteralStruct->getType(), true,
Mike Stumpc4ae9632009-02-13 15:32:32 +0000591 llvm::GlobalVariable::InternalLinkage,
592 BlockLiteralStruct, "__block_literal_global",
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000593 &getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000594
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000595 return BlockLiteral;
596}
597
Mike Stumpfca5da02009-02-21 20:00:35 +0000598llvm::Value *CodeGenFunction::LoadBlockStruct() {
599 return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self");
600}
601
Mike Stump1bdbf632009-03-05 08:32:30 +0000602llvm::Function *
603CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
604 const BlockInfo& Info,
605 uint64_t &Size,
606 uint64_t &Align,
607 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls,
608 bool &subBlockHasCopyDispose) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000609 const FunctionProtoType *FTy =
Chris Lattner8130e7f2009-02-28 19:01:03 +0000610 cast<FunctionProtoType>(BExpr->getFunctionType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000611
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000612 FunctionArgList Args;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000613
Chris Lattner8130e7f2009-02-28 19:01:03 +0000614 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000615
616 // FIXME: This leaks
Mike Stumpc4ae9632009-02-13 15:32:32 +0000617 ImplicitParamDecl *SelfDecl =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000618 ImplicitParamDecl::Create(getContext(), 0,
619 SourceLocation(), 0,
620 getContext().getPointerType(getContext().VoidTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000621
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000622 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stumpfca5da02009-02-21 20:00:35 +0000623 BlockStructDecl = SelfDecl;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000624
Steve Naroff494cb0f2009-03-13 16:56:44 +0000625 for (BlockDecl::param_const_iterator i = BD->param_begin(),
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000626 e = BD->param_end(); i != e; ++i)
Mike Stump459207f2009-02-17 23:25:52 +0000627 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000628
629 const CGFunctionInfo &FI =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000630 CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args);
631
Mike Stump084ba462009-02-14 22:16:35 +0000632 std::string Name = std::string("__") + Info.Name + "_block_invoke_";
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000633 CodeGenTypes &Types = CGM.getTypes();
634 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000635
636 llvm::Function *Fn =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000637 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
638 Name,
639 &CGM.getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000640
641 StartFunction(BD, FTy->getResultType(), Fn, Args,
Chris Lattner8130e7f2009-02-28 19:01:03 +0000642 BExpr->getBody()->getLocEnd());
643 EmitStmt(BExpr->getBody());
644 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000645
Mike Stumpf1711822009-02-25 23:33:13 +0000646 // The runtime needs a minimum alignment of a void *.
647 uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
648 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign);
649
Mike Stumpfca5da02009-02-21 20:00:35 +0000650 Size = BlockOffset;
Mike Stumpf1711822009-02-25 23:33:13 +0000651 Align = BlockAlign;
652 subBlockDeclRefDecls = BlockDeclRefDecls;
Mike Stump1bdbf632009-03-05 08:32:30 +0000653 subBlockHasCopyDispose |= BlockHasCopyDispose;
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000654 return Fn;
655}
Mike Stump2b6933f2009-02-28 09:07:16 +0000656
Mike Stump1fa52fe2009-03-07 02:35:30 +0000657uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
Mike Stump2b6933f2009-02-28 09:07:16 +0000658 const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
659
660 uint64_t Size = getContext().getTypeSize(D->getType()) / 8;
661 uint64_t Align = getContext().getDeclAlignInBytes(D);
662
663 if (BDRE->isByRef()) {
664 Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8;
665 Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
666 }
667
668 assert ((Align > 0) && "alignment must be 1 byte or more");
669
670 uint64_t OldOffset = BlockOffset;
671
672 // Ensure proper alignment, even if it means we have to have a gap
673 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align);
674 BlockAlign = std::max(Align, BlockAlign);
Mike Stump1bdbf632009-03-05 08:32:30 +0000675
Mike Stump2b6933f2009-02-28 09:07:16 +0000676 uint64_t Pad = BlockOffset - OldOffset;
677 if (Pad) {
678 llvm::ArrayType::get(llvm::Type::Int8Ty, Pad);
679 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
680 llvm::APInt(32, Pad),
681 ArrayType::Normal, 0);
682 ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
683 0, QualType(PadTy), VarDecl::None,
684 SourceLocation());
685 Expr *E;
686 E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
687 SourceLocation(), false, false);
688 BlockDeclRefDecls.push_back(E);
689 }
690 BlockDeclRefDecls.push_back(BDRE);
691
692 BlockOffset += Size;
693 return BlockOffset-Size;
694}
Mike Stumpad9605d2009-03-04 03:23:46 +0000695
Mike Stump1fa52fe2009-03-07 02:35:30 +0000696llvm::Constant *BlockFunction::
697GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
698 std::vector<HelperInfo> &NoteForHelper) {
Mike Stumpecd79422009-03-06 01:33:24 +0000699 QualType R = getContext().VoidTy;
700
701 FunctionArgList Args;
702 // FIXME: This leaks
Mike Stump1fa52fe2009-03-07 02:35:30 +0000703 ImplicitParamDecl *Dst =
704 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
705 getContext().getPointerType(getContext().VoidTy));
706 Args.push_back(std::make_pair(Dst, Dst->getType()));
Mike Stumpecd79422009-03-06 01:33:24 +0000707 ImplicitParamDecl *Src =
708 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
709 getContext().getPointerType(getContext().VoidTy));
Mike Stumpecd79422009-03-06 01:33:24 +0000710 Args.push_back(std::make_pair(Src, Src->getType()));
711
712 const CGFunctionInfo &FI =
713 CGM.getTypes().getFunctionInfo(R, Args);
714
715 std::string Name = std::string("__copy_helper_block_");
716 CodeGenTypes &Types = CGM.getTypes();
717 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
718
719 llvm::Function *Fn =
720 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
721 Name,
722 &CGM.getModule());
723
724 IdentifierInfo *II
725 = &CGM.getContext().Idents.get("__copy_helper_block_");
726
727 FunctionDecl *FD = FunctionDecl::Create(getContext(),
728 getContext().getTranslationUnitDecl(),
729 SourceLocation(), II, R,
730 FunctionDecl::Static, false,
731 true);
732 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1fa52fe2009-03-07 02:35:30 +0000733
734 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
735 llvm::Type *PtrPtrT;
736 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
737 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
738 SrcObj = Builder.CreateLoad(SrcObj);
739
740 llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
741 DstObj = Builder.CreateBitCast(DstObj, llvm::PointerType::get(T, 0));
742
743 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
744 int flag = NoteForHelper[i].flag;
745 int index = NoteForHelper[i].index;
746
747 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
748 || NoteForHelper[i].RequiresCopying) {
749 llvm::Value *Srcv = SrcObj;
750 Srcv = Builder.CreateStructGEP(Srcv, index);
751 Srcv = Builder.CreateBitCast(Srcv,
752 llvm::PointerType::get(PtrToInt8Ty, 0));
753 Srcv = Builder.CreateLoad(Srcv);
754
755 llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
756 Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
757
758 llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag);
759 llvm::Value *F = getBlockObjectAssign();
760 Builder.CreateCall3(F, Dstv, Srcv, N);
761 }
762 }
763
Mike Stumpecd79422009-03-06 01:33:24 +0000764 CGF.FinishFunction();
765
766 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpad9605d2009-03-04 03:23:46 +0000767}
768
Mike Stump9d8c1262009-03-06 18:42:23 +0000769llvm::Constant *BlockFunction::
Mike Stump1fa52fe2009-03-07 02:35:30 +0000770GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
771 const llvm::StructType* T,
772 std::vector<HelperInfo> &NoteForHelper) {
Mike Stumpecd79422009-03-06 01:33:24 +0000773 QualType R = getContext().VoidTy;
774
775 FunctionArgList Args;
776 // FIXME: This leaks
777 ImplicitParamDecl *Src =
778 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
779 getContext().getPointerType(getContext().VoidTy));
780
781 Args.push_back(std::make_pair(Src, Src->getType()));
782
783 const CGFunctionInfo &FI =
784 CGM.getTypes().getFunctionInfo(R, Args);
785
786 std::string Name = std::string("__destroy_helper_block_");
787 CodeGenTypes &Types = CGM.getTypes();
788 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
789
790 llvm::Function *Fn =
791 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
792 Name,
793 &CGM.getModule());
794
795 IdentifierInfo *II
796 = &CGM.getContext().Idents.get("__destroy_helper_block_");
797
798 FunctionDecl *FD = FunctionDecl::Create(getContext(),
799 getContext().getTranslationUnitDecl(),
800 SourceLocation(), II, R,
801 FunctionDecl::Static, false,
802 true);
803 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump05a6d4e2009-03-07 02:53:18 +0000804
805 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
806 llvm::Type *PtrPtrT;
807 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
808 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
809 SrcObj = Builder.CreateLoad(SrcObj);
810
811 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
812 int flag = NoteForHelper[i].flag;
813 int index = NoteForHelper[i].index;
814
815 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
816 || NoteForHelper[i].RequiresCopying) {
817 llvm::Value *Srcv = SrcObj;
818 Srcv = Builder.CreateStructGEP(Srcv, index);
819 Srcv = Builder.CreateBitCast(Srcv,
820 llvm::PointerType::get(PtrToInt8Ty, 0));
821 Srcv = Builder.CreateLoad(Srcv);
822
823 BuildBlockRelease(Srcv, flag);
824 }
825 }
826
Mike Stumpecd79422009-03-06 01:33:24 +0000827 CGF.FinishFunction();
828
829 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
830}
831
Mike Stump1fa52fe2009-03-07 02:35:30 +0000832llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
833 std::vector<HelperInfo> &NoteForHelper) {
834 return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
835 T, NoteForHelper);
Mike Stumpecd79422009-03-06 01:33:24 +0000836}
837
Mike Stump1fa52fe2009-03-07 02:35:30 +0000838llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
839 std::vector<HelperInfo> &NoteForHelper) {
840 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
841 T, NoteForHelper);
Mike Stumpad9605d2009-03-04 03:23:46 +0000842}
Mike Stump60294662009-03-05 01:23:13 +0000843
Mike Stump11973b62009-03-06 06:12:24 +0000844llvm::Constant *BlockFunction::
845GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
Mike Stumpf4b62342009-03-06 02:29:21 +0000846 QualType R = getContext().VoidTy;
847
848 FunctionArgList Args;
849 // FIXME: This leaks
Mike Stump11973b62009-03-06 06:12:24 +0000850 ImplicitParamDecl *Dst =
851 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
852 getContext().getPointerType(getContext().VoidTy));
853 Args.push_back(std::make_pair(Dst, Dst->getType()));
854
855 // FIXME: This leaks
Mike Stumpf4b62342009-03-06 02:29:21 +0000856 ImplicitParamDecl *Src =
857 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
858 getContext().getPointerType(getContext().VoidTy));
Mike Stumpf4b62342009-03-06 02:29:21 +0000859 Args.push_back(std::make_pair(Src, Src->getType()));
860
861 const CGFunctionInfo &FI =
862 CGM.getTypes().getFunctionInfo(R, Args);
863
864 std::string Name = std::string("__Block_byref_id_object_copy_");
865 CodeGenTypes &Types = CGM.getTypes();
866 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
867
868 llvm::Function *Fn =
869 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
870 Name,
871 &CGM.getModule());
872
873 IdentifierInfo *II
874 = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
875
876 FunctionDecl *FD = FunctionDecl::Create(getContext(),
877 getContext().getTranslationUnitDecl(),
878 SourceLocation(), II, R,
879 FunctionDecl::Static, false,
880 true);
881 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump11973b62009-03-06 06:12:24 +0000882
883 // dst->x
884 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
885 V = Builder.CreateBitCast(V, T);
886 V = Builder.CreateStructGEP(V, 6, "x");
887 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
888
889 // src->x
890 V = CGF.GetAddrOfLocalVar(Src);
891 V = Builder.CreateLoad(V);
892 V = Builder.CreateBitCast(V, T);
893 V = Builder.CreateStructGEP(V, 6, "x");
894 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
895 llvm::Value *SrcObj = Builder.CreateLoad(V);
896
897 flag |= BLOCK_BYREF_CALLER;
898
899 llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag);
900 llvm::Value *F = getBlockObjectAssign();
901 Builder.CreateCall3(F, DstObj, SrcObj, N);
902
Mike Stumpf4b62342009-03-06 02:29:21 +0000903 CGF.FinishFunction();
904
905 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
906}
907
Mike Stump4a0e5132009-03-06 04:53:30 +0000908llvm::Constant *
909BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
910 int flag) {
Mike Stumpf4b62342009-03-06 02:29:21 +0000911 QualType R = getContext().VoidTy;
912
913 FunctionArgList Args;
914 // FIXME: This leaks
915 ImplicitParamDecl *Src =
916 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
917 getContext().getPointerType(getContext().VoidTy));
918
919 Args.push_back(std::make_pair(Src, Src->getType()));
920
921 const CGFunctionInfo &FI =
922 CGM.getTypes().getFunctionInfo(R, Args);
923
924 std::string Name = std::string("__Block_byref_id_object_dispose_");
925 CodeGenTypes &Types = CGM.getTypes();
926 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
927
928 llvm::Function *Fn =
929 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
930 Name,
931 &CGM.getModule());
932
933 IdentifierInfo *II
934 = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
935
936 FunctionDecl *FD = FunctionDecl::Create(getContext(),
937 getContext().getTranslationUnitDecl(),
938 SourceLocation(), II, R,
939 FunctionDecl::Static, false,
940 true);
941 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump4a0e5132009-03-06 04:53:30 +0000942
943 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
944 V = Builder.CreateBitCast(V, T);
945 V = Builder.CreateStructGEP(V, 6, "x");
946 V = Builder.CreateBitCast(V, PtrToInt8Ty);
947
Mike Stump4a0e5132009-03-06 04:53:30 +0000948 flag |= BLOCK_BYREF_CALLER;
949 BuildBlockRelease(V, flag);
Mike Stumpf4b62342009-03-06 02:29:21 +0000950 CGF.FinishFunction();
951
952 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
953}
954
Mike Stump11973b62009-03-06 06:12:24 +0000955llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
956 int flag) {
957 return CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag);
Mike Stumpf4b62342009-03-06 02:29:21 +0000958}
959
Mike Stump4a0e5132009-03-06 04:53:30 +0000960llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
961 int flag) {
962 return CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag);
Mike Stumpf4b62342009-03-06 02:29:21 +0000963}
964
Mike Stump60294662009-03-05 01:23:13 +0000965llvm::Value *BlockFunction::getBlockObjectDispose() {
966 if (CGM.BlockObjectDispose == 0) {
967 const llvm::FunctionType *FTy;
968 std::vector<const llvm::Type*> ArgTys;
969 const llvm::Type *ResultType = llvm::Type::VoidTy;
970 ArgTys.push_back(PtrToInt8Ty);
971 ArgTys.push_back(llvm::Type::Int32Ty);
972 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stump1bdbf632009-03-05 08:32:30 +0000973 CGM.BlockObjectDispose
Mike Stump60294662009-03-05 01:23:13 +0000974 = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
975 }
976 return CGM.BlockObjectDispose;
977}
978
Mike Stump11973b62009-03-06 06:12:24 +0000979llvm::Value *BlockFunction::getBlockObjectAssign() {
980 if (CGM.BlockObjectAssign == 0) {
981 const llvm::FunctionType *FTy;
982 std::vector<const llvm::Type*> ArgTys;
983 const llvm::Type *ResultType = llvm::Type::VoidTy;
984 ArgTys.push_back(PtrToInt8Ty);
985 ArgTys.push_back(PtrToInt8Ty);
986 ArgTys.push_back(llvm::Type::Int32Ty);
987 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
988 CGM.BlockObjectAssign
989 = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
990 }
991 return CGM.BlockObjectAssign;
992}
993
Mike Stump4a0e5132009-03-06 04:53:30 +0000994void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Mike Stump60294662009-03-05 01:23:13 +0000995 llvm::Value *F = getBlockObjectDispose();
Mike Stump4a0e5132009-03-06 04:53:30 +0000996 llvm::Value *N;
Mike Stump60294662009-03-05 01:23:13 +0000997 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Mike Stump4a0e5132009-03-06 04:53:30 +0000998 N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag);
Mike Stump60294662009-03-05 01:23:13 +0000999 Builder.CreateCall2(F, V, N);
1000}
Mike Stump1bdbf632009-03-05 08:32:30 +00001001
1002ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stump1fa52fe2009-03-07 02:35:30 +00001003
1004BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1005 CGBuilderTy &B)
1006 : CGM(cgm), CGF(cgf), Builder(B) {
1007 PtrToInt8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1008
1009 BlockHasCopyDispose = false;
1010}