blob: 3b602a0a1b790afc37b9ae17e6c160505db1b1df [file] [log] [blame]
Anders Carlssonacfde802009-02-12 00:39:25 +00001//===--- CGBlocks.cpp - Emit LLVM Code for declarations -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit blocks.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
16#include "llvm/Module.h"
Anders Carlssond5cab542009-02-12 17:55:02 +000017#include "llvm/Target/TargetData.h"
Anders Carlssonacfde802009-02-12 00:39:25 +000018
19#include <algorithm>
20
21using namespace clang;
22using namespace CodeGen;
23
Mike Stump58919e12009-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 Stump00470a12009-03-05 08:32:30 +000033 llvm::cl::ZeroOrMore,
34 llvm::cl::init(false));
Mike Stump58919e12009-03-04 13:17:22 +000035
Mike Stumpcf62d392009-03-06 18:42:23 +000036llvm::Constant *CodeGenFunction::
37BuildDescriptorBlockDecl(uint64_t Size, const llvm::Type* Ty) {
Mike Stump56129b12009-02-13 16:55:51 +000038 const llvm::Type *UnsignedLongTy
39 = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpe5fee252009-02-13 16:19:19 +000040 llvm::Constant *C;
41 std::vector<llvm::Constant*> Elts;
42
43 // reserved
Mike Stump56129b12009-02-13 16:55:51 +000044 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpe5fee252009-02-13 16:19:19 +000045 Elts.push_back(C);
46
47 // Size
Mike Stumpd6840002009-02-21 20:07:44 +000048 // FIXME: What is the right way to say this doesn't fit? We should give
49 // a user diagnostic in that case. Better fix would be to change the
50 // API to size_t.
Mike Stump4e7a1f72009-02-21 20:00:35 +000051 C = llvm::ConstantInt::get(UnsignedLongTy, Size);
Mike Stumpe5fee252009-02-13 16:19:19 +000052 Elts.push_back(C);
53
54 if (BlockHasCopyDispose) {
55 // copy_func_helper_decl
Mike Stumpcf62d392009-03-06 18:42:23 +000056 Elts.push_back(BuildCopyHelper(Ty));
Mike Stumpe5fee252009-02-13 16:19:19 +000057
58 // destroy_func_decl
Mike Stumpcf62d392009-03-06 18:42:23 +000059 Elts.push_back(BuildDestroyHelper(Ty));
Mike Stumpe5fee252009-02-13 16:19:19 +000060 }
61
62 C = llvm::ConstantStruct::get(Elts);
63
Mike Stumpe5fee252009-02-13 16:19:19 +000064 C = new llvm::GlobalVariable(C->getType(), true,
65 llvm::GlobalValue::InternalLinkage,
Mike Stump7d6dc4f2009-02-13 20:17:16 +000066 C, "__block_descriptor_tmp", &CGM.getModule());
Mike Stumpe5fee252009-02-13 16:19:19 +000067 return C;
68}
69
Mike Stump2a998142009-03-04 18:17:45 +000070llvm::Constant *BlockModule::getNSConcreteGlobalBlock() {
Mike Stumpf99f1d02009-02-13 17:23:42 +000071 if (NSConcreteGlobalBlock)
72 return NSConcreteGlobalBlock;
73
Mike Stumpf7448952009-02-13 19:38:12 +000074 // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
Mike Stump58a85142009-03-04 22:48:06 +000075 // same thing as CreateRuntimeFunction if there's already a variable with the
76 // same name.
Mike Stumpf99f1d02009-02-13 17:23:42 +000077 NSConcreteGlobalBlock
Mike Stump00470a12009-03-05 08:32:30 +000078 = new llvm::GlobalVariable(PtrToInt8Ty, false,
Mike Stumpf99f1d02009-02-13 17:23:42 +000079 llvm::GlobalValue::ExternalLinkage,
80 0, "_NSConcreteGlobalBlock",
81 &getModule());
82
83 return NSConcreteGlobalBlock;
84}
85
Mike Stump2a998142009-03-04 18:17:45 +000086llvm::Constant *BlockModule::getNSConcreteStackBlock() {
Mike Stump59c5b112009-02-13 19:29:27 +000087 if (NSConcreteStackBlock)
88 return NSConcreteStackBlock;
89
Mike Stumpf7448952009-02-13 19:38:12 +000090 // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
Mike Stump58a85142009-03-04 22:48:06 +000091 // same thing as CreateRuntimeFunction if there's already a variable with the
92 // same name.
Mike Stump59c5b112009-02-13 19:29:27 +000093 NSConcreteStackBlock
Mike Stump00470a12009-03-05 08:32:30 +000094 = new llvm::GlobalVariable(PtrToInt8Ty, false,
Mike Stump59c5b112009-02-13 19:29:27 +000095 llvm::GlobalValue::ExternalLinkage,
96 0, "_NSConcreteStackBlock",
97 &getModule());
98
99 return NSConcreteStackBlock;
100}
101
Mike Stump00470a12009-03-05 08:32:30 +0000102static void CollectBlockDeclRefInfo(const Stmt *S,
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000103 CodeGenFunction::BlockInfo &Info) {
104 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
105 I != E; ++I)
Daniel Dunbar82573ee2009-03-02 07:00:57 +0000106 if (*I)
107 CollectBlockDeclRefInfo(*I, Info);
Mike Stump00470a12009-03-05 08:32:30 +0000108
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000109 if (const BlockDeclRefExpr *DE = dyn_cast<BlockDeclRefExpr>(S)) {
110 // FIXME: Handle enums.
111 if (isa<FunctionDecl>(DE->getDecl()))
112 return;
Mike Stump00470a12009-03-05 08:32:30 +0000113
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000114 if (DE->isByRef())
115 Info.ByRefDeclRefs.push_back(DE);
116 else
117 Info.ByCopyDeclRefs.push_back(DE);
118 }
119}
120
Mike Stump58a85142009-03-04 22:48:06 +0000121/// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be
122/// declared as a global variable instead of on the stack.
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000123static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info)
124{
125 return Info.ByRefDeclRefs.empty() && Info.ByCopyDeclRefs.empty();
126}
127
Mike Stump58a85142009-03-04 22:48:06 +0000128// FIXME: Push most into CGM, passing down a few bits, like current function
129// name.
Mike Stump8a2b4b12009-02-25 23:33:13 +0000130llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Mike Stumpe5fee252009-02-13 16:19:19 +0000131
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000132 std::string Name = CurFn->getName();
133 CodeGenFunction::BlockInfo Info(0, Name.c_str());
134 CollectBlockDeclRefInfo(BE->getBody(), Info);
135
136 // Check if the block can be global.
Mike Stump58a85142009-03-04 22:48:06 +0000137 // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like
138 // to just have one code path. We should move this function into CGM and pass
139 // CGF, then we can just check to see if CGF is 0.
Mike Stumpdab514f2009-03-04 03:23:46 +0000140 if (0 && CanBlockBeGlobal(Info))
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000141 return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
Mike Stump00470a12009-03-05 08:32:30 +0000142
143 std::vector<llvm::Constant*> Elts(5);
Mike Stumpe5fee252009-02-13 16:19:19 +0000144 llvm::Constant *C;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000145 llvm::Value *V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000146
Mike Stumpe5fee252009-02-13 16:19:19 +0000147 {
148 // C = BuildBlockStructInitlist();
149 unsigned int flags = BLOCK_HAS_DESCRIPTOR;
150
Mike Stump00470a12009-03-05 08:32:30 +0000151 // We run this first so that we set BlockHasCopyDispose from the entire
152 // block literal.
153 // __invoke
154 uint64_t subBlockSize, subBlockAlign;
155 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
156 llvm::Function *Fn
157 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, subBlockSize,
158 subBlockAlign,
159 subBlockDeclRefDecls,
160 BlockHasCopyDispose);
161 Elts[3] = Fn;
162
163 if (!Enable__block && BlockHasCopyDispose)
164 ErrorUnsupported(BE, "block literal that requires copy/dispose");
165
Mike Stumpe5fee252009-02-13 16:19:19 +0000166 if (BlockHasCopyDispose)
167 flags |= BLOCK_HAS_COPY_DISPOSE;
168
Mike Stump7d6dc4f2009-02-13 20:17:16 +0000169 // __isa
Mike Stump59c5b112009-02-13 19:29:27 +0000170 C = CGM.getNSConcreteStackBlock();
Mike Stumpe5fee252009-02-13 16:19:19 +0000171 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
Mike Stump00470a12009-03-05 08:32:30 +0000172 Elts[0] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000173
174 // __flags
175 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
176 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
177 C = llvm::ConstantInt::get(IntTy, flags);
Mike Stump00470a12009-03-05 08:32:30 +0000178 Elts[1] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000179
180 // __reserved
181 C = llvm::ConstantInt::get(IntTy, 0);
Mike Stump00470a12009-03-05 08:32:30 +0000182 Elts[2] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000183
Mike Stump8a2b4b12009-02-25 23:33:13 +0000184 if (subBlockDeclRefDecls.size() == 0) {
Mike Stumpcf62d392009-03-06 18:42:23 +0000185 // __descriptor
186 Elts[4] = BuildDescriptorBlockDecl(subBlockSize, 0);
187
Mike Stump5570cfe2009-03-01 20:07:53 +0000188 // Optimize to being a global block.
189 Elts[0] = CGM.getNSConcreteGlobalBlock();
190 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
191
Mike Stump8a2b4b12009-02-25 23:33:13 +0000192 C = llvm::ConstantStruct::get(Elts);
193
194 char Name[32];
195 sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
196 C = new llvm::GlobalVariable(C->getType(), true,
197 llvm::GlobalValue::InternalLinkage,
198 C, Name, &CGM.getModule());
199 QualType BPT = BE->getType();
200 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
201 return C;
202 }
Mike Stump00470a12009-03-05 08:32:30 +0000203
Mike Stump8a2b4b12009-02-25 23:33:13 +0000204 std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size());
205 for (int i=0; i<5; ++i)
206 Types[i] = Elts[i]->getType();
207
Mike Stumpa99038c2009-02-28 09:07:16 +0000208 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
209 const Expr *E = subBlockDeclRefDecls[i];
210 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
211 QualType Ty = E->getType();
Mike Stumpdab514f2009-03-04 03:23:46 +0000212 if (BDRE && BDRE->isByRef()) {
213 uint64_t Align = getContext().getDeclAlignInBytes(BDRE->getDecl());
214 Types[i+5] = llvm::PointerType::get(BuildByRefType(Ty, Align), 0);
215 } else
216 Types[i+5] = ConvertType(Ty);
Mike Stumpa99038c2009-02-28 09:07:16 +0000217 }
Mike Stump8a2b4b12009-02-25 23:33:13 +0000218
219 llvm::Type *Ty = llvm::StructType::get(Types, true);
220
221 llvm::AllocaInst *A = CreateTempAlloca(Ty);
222 A->setAlignment(subBlockAlign);
223 V = A;
224
225 for (unsigned i=0; i<5; ++i)
226 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
Mike Stump00470a12009-03-05 08:32:30 +0000227
Mike Stump8a2b4b12009-02-25 23:33:13 +0000228 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
229 {
Mike Stumpa99038c2009-02-28 09:07:16 +0000230 // FIXME: Push const down.
231 Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
232 DeclRefExpr *DR;
233 ValueDecl *VD;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000234
Mike Stumpa99038c2009-02-28 09:07:16 +0000235 DR = dyn_cast<DeclRefExpr>(E);
236 // Skip padding.
237 if (DR) continue;
238
239 BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
240 VD = BDRE->getDecl();
241
Mike Stumpdab514f2009-03-04 03:23:46 +0000242 llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp");
Mike Stumpa99038c2009-02-28 09:07:16 +0000243 // FIXME: I want a better way to do this.
244 if (LocalDeclMap[VD]) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000245 if (BDRE->isByRef()) {
246 const llvm::Type *Ty = Types[i+5];
247 llvm::Value *Loc = LocalDeclMap[VD];
248 Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
249 Loc = Builder.CreateLoad(Loc, false);
250 Loc = Builder.CreateBitCast(Loc, Ty);
251 Builder.CreateStore(Loc, Addr);
252 continue;
253 } else
254 E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD),
255 VD->getType(), SourceLocation(),
256 false, false);
Mike Stumpa99038c2009-02-28 09:07:16 +0000257 }
Mike Stumpdab514f2009-03-04 03:23:46 +0000258 if (BDRE->isByRef()) {
Mike Stumpa99038c2009-02-28 09:07:16 +0000259 E = new (getContext())
260 UnaryOperator(E, UnaryOperator::AddrOf,
261 getContext().getPointerType(E->getType()),
262 SourceLocation());
Mike Stumpdab514f2009-03-04 03:23:46 +0000263 }
Mike Stumpa99038c2009-02-28 09:07:16 +0000264
Mike Stumpa99038c2009-02-28 09:07:16 +0000265 RValue r = EmitAnyExpr(E, Addr, false);
Mike Stumpdab514f2009-03-04 03:23:46 +0000266 if (r.isScalar()) {
267 llvm::Value *Loc = r.getScalarVal();
268 const llvm::Type *Ty = Types[i+5];
269 if (BDRE->isByRef()) {
Mike Stump58a85142009-03-04 22:48:06 +0000270 // E is now the address of the value field, instead, we want the
271 // address of the actual ByRef struct. We optimize this slightly
272 // compared to gcc by not grabbing the forwarding slot as this must
273 // be done during Block_copy for us, and we can postpone the work
274 // until then.
275 uint64_t offset = BlockDecls[BDRE->getDecl()];
Mike Stump00470a12009-03-05 08:32:30 +0000276
Mike Stump58a85142009-03-04 22:48:06 +0000277 llvm::Value *BlockLiteral = LoadBlockStruct();
Mike Stump00470a12009-03-05 08:32:30 +0000278
Mike Stump58a85142009-03-04 22:48:06 +0000279 Loc = Builder.CreateGEP(BlockLiteral,
280 llvm::ConstantInt::get(llvm::Type::Int64Ty,
281 offset),
282 "block.literal");
283 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000284 Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stump58a85142009-03-04 22:48:06 +0000285 Loc = Builder.CreateLoad(Loc, false);
286 // Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000287 }
288 Builder.CreateStore(Loc, Addr);
289 } else if (r.isComplex())
Mike Stump8a2b4b12009-02-25 23:33:13 +0000290 // FIXME: implement
291 ErrorUnsupported(BE, "complex in block literal");
292 else if (r.isAggregate())
293 ; // Already created into the destination
294 else
295 assert (0 && "bad block variable");
296 // FIXME: Ensure that the offset created by the backend for
297 // the struct matches the previously computed offset in BlockDecls.
298 }
Mike Stumpcf62d392009-03-06 18:42:23 +0000299
300 // __descriptor
301 Elts[4] = BuildDescriptorBlockDecl(subBlockSize, Ty);
Mike Stumpe5fee252009-02-13 16:19:19 +0000302 }
Mike Stump00470a12009-03-05 08:32:30 +0000303
Mike Stumpbd65cac2009-02-19 01:01:04 +0000304 QualType BPT = BE->getType();
Mike Stump8a2b4b12009-02-25 23:33:13 +0000305 return Builder.CreateBitCast(V, ConvertType(BPT));
Mike Stumpe5fee252009-02-13 16:19:19 +0000306}
307
308
Mike Stump2a998142009-03-04 18:17:45 +0000309const llvm::Type *BlockModule::getBlockDescriptorType() {
Mike Stumpab695142009-02-13 15:16:56 +0000310 if (BlockDescriptorType)
311 return BlockDescriptorType;
312
Mike Stumpa5448542009-02-13 15:32:32 +0000313 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000314 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000315
Mike Stumpab695142009-02-13 15:16:56 +0000316 // struct __block_descriptor {
317 // unsigned long reserved;
318 // unsigned long block_size;
319 // };
Mike Stumpa5448542009-02-13 15:32:32 +0000320 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy,
321 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000322 NULL);
323
324 getModule().addTypeName("struct.__block_descriptor",
325 BlockDescriptorType);
326
327 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000328}
329
Mike Stump2a998142009-03-04 18:17:45 +0000330const llvm::Type *BlockModule::getGenericBlockLiteralType() {
Mike Stump9b8a7972009-02-13 15:25:34 +0000331 if (GenericBlockLiteralType)
332 return GenericBlockLiteralType;
333
Mike Stumpa5448542009-02-13 15:32:32 +0000334 const llvm::Type *BlockDescPtrTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000335 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000336
Mike Stump7cbb3602009-02-13 16:01:35 +0000337 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
338 getTypes().ConvertType(getContext().IntTy));
339
Mike Stump9b8a7972009-02-13 15:25:34 +0000340 // struct __block_literal_generic {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000341 // void *__isa;
342 // int __flags;
343 // int __reserved;
344 // void (*__invoke)(void *);
345 // struct __block_descriptor *__descriptor;
Mike Stump9b8a7972009-02-13 15:25:34 +0000346 // };
Mike Stump797b6322009-03-05 01:23:13 +0000347 GenericBlockLiteralType = llvm::StructType::get(PtrToInt8Ty,
Mike Stump7cbb3602009-02-13 16:01:35 +0000348 IntTy,
349 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000350 PtrToInt8Ty,
Mike Stump9b8a7972009-02-13 15:25:34 +0000351 BlockDescPtrTy,
352 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000353
Mike Stump9b8a7972009-02-13 15:25:34 +0000354 getModule().addTypeName("struct.__block_literal_generic",
355 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000356
Mike Stump9b8a7972009-02-13 15:25:34 +0000357 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000358}
359
Mike Stump2a998142009-03-04 18:17:45 +0000360const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000361 if (GenericExtendedBlockLiteralType)
362 return GenericExtendedBlockLiteralType;
363
Mike Stumpbd65cac2009-02-19 01:01:04 +0000364 const llvm::Type *BlockDescPtrTy =
365 llvm::PointerType::getUnqual(getBlockDescriptorType());
366
367 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
368 getTypes().ConvertType(getContext().IntTy));
369
370 // struct __block_literal_generic {
371 // void *__isa;
372 // int __flags;
373 // int __reserved;
374 // void (*__invoke)(void *);
375 // struct __block_descriptor *__descriptor;
376 // void *__copy_func_helper_decl;
377 // void *__destroy_func_decl;
378 // };
Mike Stump797b6322009-03-05 01:23:13 +0000379 GenericExtendedBlockLiteralType = llvm::StructType::get(PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000380 IntTy,
381 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000382 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000383 BlockDescPtrTy,
Mike Stump797b6322009-03-05 01:23:13 +0000384 PtrToInt8Ty,
385 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000386 NULL);
387
388 getModule().addTypeName("struct.__block_literal_extended_generic",
389 GenericExtendedBlockLiteralType);
390
391 return GenericExtendedBlockLiteralType;
392}
393
Mike Stumpa5448542009-02-13 15:32:32 +0000394/// getBlockFunctionType - Given a BlockPointerType, will return the
Anders Carlssonacfde802009-02-12 00:39:25 +0000395/// function type for the block, including the first block literal argument.
396static QualType getBlockFunctionType(ASTContext &Ctx,
Anders Carlssond5cab542009-02-12 17:55:02 +0000397 const BlockPointerType *BPT) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000398 const FunctionProtoType *FTy = cast<FunctionProtoType>(BPT->getPointeeType());
Mike Stumpa5448542009-02-13 15:32:32 +0000399
Anders Carlssonacfde802009-02-12 00:39:25 +0000400 llvm::SmallVector<QualType, 8> Types;
401 Types.push_back(Ctx.getPointerType(Ctx.VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000402
Douglas Gregor72564e72009-02-26 23:50:07 +0000403 for (FunctionProtoType::arg_type_iterator i = FTy->arg_type_begin(),
Anders Carlssonacfde802009-02-12 00:39:25 +0000404 e = FTy->arg_type_end(); i != e; ++i)
405 Types.push_back(*i);
Mike Stumpa5448542009-02-13 15:32:32 +0000406
Anders Carlssonacfde802009-02-12 00:39:25 +0000407 return Ctx.getFunctionType(FTy->getResultType(),
Mike Stumpa5448542009-02-13 15:32:32 +0000408 &Types[0], Types.size(),
Anders Carlssonacfde802009-02-12 00:39:25 +0000409 FTy->isVariadic(), 0);
410}
411
Anders Carlssond5cab542009-02-12 17:55:02 +0000412RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpa5448542009-02-13 15:32:32 +0000413 const BlockPointerType *BPT =
Anders Carlssonacfde802009-02-12 00:39:25 +0000414 E->getCallee()->getType()->getAsBlockPointerType();
Mike Stumpa5448542009-02-13 15:32:32 +0000415
Anders Carlssonacfde802009-02-12 00:39:25 +0000416 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
417
418 // Get a pointer to the generic block literal.
419 const llvm::Type *BlockLiteralTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000420 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000421
422 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000423 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000424 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
425
426 // Get the function pointer from the literal.
427 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Mike Stump20733cd2009-02-22 13:27:11 +0000428 llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp");
Anders Carlssonacfde802009-02-12 00:39:25 +0000429
430 // Cast the function pointer to the right type.
Mike Stumpa5448542009-02-13 15:32:32 +0000431 const llvm::Type *BlockFTy =
Anders Carlssonacfde802009-02-12 00:39:25 +0000432 ConvertType(getBlockFunctionType(getContext(), BPT));
433 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
434 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
435
Mike Stumpa5448542009-02-13 15:32:32 +0000436 BlockLiteral =
437 Builder.CreateBitCast(BlockLiteral,
Anders Carlssonacfde802009-02-12 00:39:25 +0000438 llvm::PointerType::getUnqual(llvm::Type::Int8Ty),
439 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000440
Anders Carlssonacfde802009-02-12 00:39:25 +0000441 // Add the block literal.
442 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
443 CallArgList Args;
444 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000445
Anders Carlssonacfde802009-02-12 00:39:25 +0000446 // And the rest of the arguments.
Mike Stumpa5448542009-02-13 15:32:32 +0000447 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
Anders Carlssonacfde802009-02-12 00:39:25 +0000448 i != e; ++i)
Mike Stumpa5448542009-02-13 15:32:32 +0000449 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i),
Anders Carlssonacfde802009-02-12 00:39:25 +0000450 i->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000451
Anders Carlssonacfde802009-02-12 00:39:25 +0000452 // And call the block.
Mike Stumpa5448542009-02-13 15:32:32 +0000453 return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args),
Anders Carlssonacfde802009-02-12 00:39:25 +0000454 Func, Args);
455}
Anders Carlssond5cab542009-02-12 17:55:02 +0000456
Mike Stumpdab514f2009-03-04 03:23:46 +0000457llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
458 uint64_t &offset = BlockDecls[E->getDecl()];
459
460 const llvm::Type *Ty;
461 Ty = CGM.getTypes().ConvertType(E->getDecl()->getType());
462
463 // FIXME: add support for copy/dispose helpers.
Mike Stump58919e12009-03-04 13:17:22 +0000464 if (!Enable__block && E->isByRef())
Mike Stumpdab514f2009-03-04 03:23:46 +0000465 ErrorUnsupported(E, "__block variable in block literal");
Mike Stumpa4f668f2009-03-06 01:33:24 +0000466 else if (!Enable__block && E->getType()->isBlockPointerType())
Mike Stumpdab514f2009-03-04 03:23:46 +0000467 ErrorUnsupported(E, "block pointer in block literal");
Mike Stump00470a12009-03-05 08:32:30 +0000468 else if (E->getDecl()->getAttr<ObjCNSObjectAttr>() ||
Mike Stumpdab514f2009-03-04 03:23:46 +0000469 getContext().isObjCNSObjectType(E->getType()))
470 ErrorUnsupported(E, "__attribute__((NSObject)) variable in block "
471 "literal");
Mike Stumpa4f668f2009-03-06 01:33:24 +0000472 else if (!Enable__block && getContext().isObjCObjectPointerType(E->getType()))
Mike Stumpdab514f2009-03-04 03:23:46 +0000473 ErrorUnsupported(E, "Objective-C variable in block literal");
474
475 // See if we have already allocated an offset for this variable.
476 if (offset == 0) {
Mike Stump00470a12009-03-05 08:32:30 +0000477 // Don't run the expensive check, unless we have to.
478 if (!BlockHasCopyDispose && BlockRequiresCopying(E->getType()))
479 BlockHasCopyDispose = true;
Mike Stumpdab514f2009-03-04 03:23:46 +0000480 // if not, allocate one now.
481 offset = getBlockOffset(E);
482 }
483
484 llvm::Value *BlockLiteral = LoadBlockStruct();
485 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
486 llvm::ConstantInt::get(llvm::Type::Int64Ty,
487 offset),
Mike Stump58a85142009-03-04 22:48:06 +0000488 "block.literal");
Mike Stumpdab514f2009-03-04 03:23:46 +0000489 if (E->isByRef()) {
490 bool needsCopyDispose = BlockRequiresCopying(E->getType());
491 uint64_t Align = getContext().getDeclAlignInBytes(E->getDecl());
492 const llvm::Type *PtrStructTy
493 = llvm::PointerType::get(BuildByRefType(E->getType(), Align), 0);
494 Ty = PtrStructTy;
495 Ty = llvm::PointerType::get(Ty, 0);
496 V = Builder.CreateBitCast(V, Ty);
497 V = Builder.CreateLoad(V, false);
498 V = Builder.CreateStructGEP(V, 1, "forwarding");
499 V = Builder.CreateLoad(V, false);
500 V = Builder.CreateBitCast(V, PtrStructTy);
501 V = Builder.CreateStructGEP(V, needsCopyDispose*2 + 4, "x");
502 } else {
503 Ty = llvm::PointerType::get(Ty, 0);
504 V = Builder.CreateBitCast(V, Ty);
505 }
506 return V;
507}
508
Mike Stump67a64482009-02-14 22:16:35 +0000509llvm::Constant *
Mike Stump90a90432009-03-04 18:47:42 +0000510BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000511 // Generate the block descriptor.
512 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000513 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
514 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000515
Anders Carlssond5cab542009-02-12 17:55:02 +0000516 llvm::Constant *DescriptorFields[2];
Mike Stumpa5448542009-02-13 15:32:32 +0000517
Anders Carlssond5cab542009-02-12 17:55:02 +0000518 // Reserved
519 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000520
Anders Carlssond5cab542009-02-12 17:55:02 +0000521 // Block literal size. For global blocks we just use the size of the generic
522 // block literal struct.
Mike Stumpa5448542009-02-13 15:32:32 +0000523 uint64_t BlockLiteralSize =
Mike Stump9b8a7972009-02-13 15:25:34 +0000524 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Anders Carlssond5cab542009-02-12 17:55:02 +0000525 DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpa5448542009-02-13 15:32:32 +0000526
527 llvm::Constant *DescriptorStruct =
Anders Carlssond5cab542009-02-12 17:55:02 +0000528 llvm::ConstantStruct::get(&DescriptorFields[0], 2);
Mike Stumpa5448542009-02-13 15:32:32 +0000529
Anders Carlssond5cab542009-02-12 17:55:02 +0000530 llvm::GlobalVariable *Descriptor =
531 new llvm::GlobalVariable(DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000532 llvm::GlobalVariable::InternalLinkage,
533 DescriptorStruct, "__block_descriptor_global",
Anders Carlssond5cab542009-02-12 17:55:02 +0000534 &getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000535
Anders Carlssond5cab542009-02-12 17:55:02 +0000536 // Generate the constants for the block literal.
537 llvm::Constant *LiteralFields[5];
Mike Stumpa5448542009-02-13 15:32:32 +0000538
Mike Stump67a64482009-02-14 22:16:35 +0000539 CodeGenFunction::BlockInfo Info(0, n);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000540 uint64_t subBlockSize, subBlockAlign;
Mike Stumpa99038c2009-02-28 09:07:16 +0000541 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Mike Stump00470a12009-03-05 08:32:30 +0000542 bool subBlockHasCopyDispose;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000543 llvm::Function *Fn
Mike Stump90a90432009-03-04 18:47:42 +0000544 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, subBlockSize,
545 subBlockAlign,
Mike Stump00470a12009-03-05 08:32:30 +0000546 subBlockDeclRefDecls,
547 subBlockHasCopyDispose);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000548 assert(subBlockSize == BlockLiteralSize
549 && "no imports allowed for global block");
Mike Stump00470a12009-03-05 08:32:30 +0000550 assert(!subBlockHasCopyDispose && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000551
Anders Carlssond5cab542009-02-12 17:55:02 +0000552 // isa
Mike Stumpf99f1d02009-02-13 17:23:42 +0000553 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000554
Anders Carlssond5cab542009-02-12 17:55:02 +0000555 // Flags
Mike Stump00470a12009-03-05 08:32:30 +0000556 LiteralFields[1] =
Anders Carlsson8045ee02009-03-01 21:09:29 +0000557 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR);
Mike Stumpa5448542009-02-13 15:32:32 +0000558
Anders Carlssond5cab542009-02-12 17:55:02 +0000559 // Reserved
Mike Stump7cbb3602009-02-13 16:01:35 +0000560 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000561
Anders Carlssond5cab542009-02-12 17:55:02 +0000562 // Function
563 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000564
Anders Carlssond5cab542009-02-12 17:55:02 +0000565 // Descriptor
566 LiteralFields[4] = Descriptor;
Mike Stumpa5448542009-02-13 15:32:32 +0000567
568 llvm::Constant *BlockLiteralStruct =
Anders Carlssond5cab542009-02-12 17:55:02 +0000569 llvm::ConstantStruct::get(&LiteralFields[0], 5);
Mike Stumpa5448542009-02-13 15:32:32 +0000570
571 llvm::GlobalVariable *BlockLiteral =
Anders Carlssond5cab542009-02-12 17:55:02 +0000572 new llvm::GlobalVariable(BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000573 llvm::GlobalVariable::InternalLinkage,
574 BlockLiteralStruct, "__block_literal_global",
Anders Carlssond5cab542009-02-12 17:55:02 +0000575 &getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000576
Anders Carlssond5cab542009-02-12 17:55:02 +0000577 return BlockLiteral;
578}
579
Mike Stump4e7a1f72009-02-21 20:00:35 +0000580llvm::Value *CodeGenFunction::LoadBlockStruct() {
581 return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self");
582}
583
Mike Stump00470a12009-03-05 08:32:30 +0000584llvm::Function *
585CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
586 const BlockInfo& Info,
587 uint64_t &Size,
588 uint64_t &Align,
589 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls,
590 bool &subBlockHasCopyDispose) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000591 const FunctionProtoType *FTy =
Chris Lattner161d36d2009-02-28 19:01:03 +0000592 cast<FunctionProtoType>(BExpr->getFunctionType());
Mike Stumpa5448542009-02-13 15:32:32 +0000593
Anders Carlssond5cab542009-02-12 17:55:02 +0000594 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000595
Chris Lattner161d36d2009-02-28 19:01:03 +0000596 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlssond5cab542009-02-12 17:55:02 +0000597
598 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000599 ImplicitParamDecl *SelfDecl =
Anders Carlssond5cab542009-02-12 17:55:02 +0000600 ImplicitParamDecl::Create(getContext(), 0,
601 SourceLocation(), 0,
602 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000603
Anders Carlssond5cab542009-02-12 17:55:02 +0000604 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump4e7a1f72009-02-21 20:00:35 +0000605 BlockStructDecl = SelfDecl;
Mike Stumpa5448542009-02-13 15:32:32 +0000606
607 for (BlockDecl::param_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000608 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000609 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000610
611 const CGFunctionInfo &FI =
Anders Carlssond5cab542009-02-12 17:55:02 +0000612 CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args);
613
Mike Stump67a64482009-02-14 22:16:35 +0000614 std::string Name = std::string("__") + Info.Name + "_block_invoke_";
Anders Carlssond5cab542009-02-12 17:55:02 +0000615 CodeGenTypes &Types = CGM.getTypes();
616 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic());
Mike Stumpa5448542009-02-13 15:32:32 +0000617
618 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000619 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
620 Name,
621 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000622
623 StartFunction(BD, FTy->getResultType(), Fn, Args,
Chris Lattner161d36d2009-02-28 19:01:03 +0000624 BExpr->getBody()->getLocEnd());
625 EmitStmt(BExpr->getBody());
626 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlssond5cab542009-02-12 17:55:02 +0000627
Mike Stump8a2b4b12009-02-25 23:33:13 +0000628 // The runtime needs a minimum alignment of a void *.
629 uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
630 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign);
631
Mike Stump4e7a1f72009-02-21 20:00:35 +0000632 Size = BlockOffset;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000633 Align = BlockAlign;
634 subBlockDeclRefDecls = BlockDeclRefDecls;
Mike Stump00470a12009-03-05 08:32:30 +0000635 subBlockHasCopyDispose |= BlockHasCopyDispose;
Anders Carlssond5cab542009-02-12 17:55:02 +0000636 return Fn;
637}
Mike Stumpa99038c2009-02-28 09:07:16 +0000638
639uint64_t CodeGenFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
640 const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
641
642 uint64_t Size = getContext().getTypeSize(D->getType()) / 8;
643 uint64_t Align = getContext().getDeclAlignInBytes(D);
644
645 if (BDRE->isByRef()) {
646 Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8;
647 Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
648 }
649
650 assert ((Align > 0) && "alignment must be 1 byte or more");
651
652 uint64_t OldOffset = BlockOffset;
653
654 // Ensure proper alignment, even if it means we have to have a gap
655 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align);
656 BlockAlign = std::max(Align, BlockAlign);
Mike Stump00470a12009-03-05 08:32:30 +0000657
Mike Stumpa99038c2009-02-28 09:07:16 +0000658 uint64_t Pad = BlockOffset - OldOffset;
659 if (Pad) {
660 llvm::ArrayType::get(llvm::Type::Int8Ty, Pad);
661 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
662 llvm::APInt(32, Pad),
663 ArrayType::Normal, 0);
664 ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
665 0, QualType(PadTy), VarDecl::None,
666 SourceLocation());
667 Expr *E;
668 E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
669 SourceLocation(), false, false);
670 BlockDeclRefDecls.push_back(E);
671 }
672 BlockDeclRefDecls.push_back(BDRE);
673
674 BlockOffset += Size;
675 return BlockOffset-Size;
676}
Mike Stumpdab514f2009-03-04 03:23:46 +0000677
Mike Stumpcf62d392009-03-06 18:42:23 +0000678llvm::Constant *BlockFunction::GenerateCopyHelperFunction(const llvm::Type *Ty) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000679 QualType R = getContext().VoidTy;
680
681 FunctionArgList Args;
682 // FIXME: This leaks
683 ImplicitParamDecl *Src =
684 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
685 getContext().getPointerType(getContext().VoidTy));
686
687 Args.push_back(std::make_pair(Src, Src->getType()));
688
689 const CGFunctionInfo &FI =
690 CGM.getTypes().getFunctionInfo(R, Args);
691
692 std::string Name = std::string("__copy_helper_block_");
693 CodeGenTypes &Types = CGM.getTypes();
694 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
695
696 llvm::Function *Fn =
697 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
698 Name,
699 &CGM.getModule());
700
701 IdentifierInfo *II
702 = &CGM.getContext().Idents.get("__copy_helper_block_");
703
704 FunctionDecl *FD = FunctionDecl::Create(getContext(),
705 getContext().getTranslationUnitDecl(),
706 SourceLocation(), II, R,
707 FunctionDecl::Static, false,
708 true);
709 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
710 // EmitStmt(BExpr->getBody());
711 CGF.FinishFunction();
712
713 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000714}
715
Mike Stumpcf62d392009-03-06 18:42:23 +0000716llvm::Constant *BlockFunction::
717GenerateDestroyHelperFunction(const llvm::Type* Ty) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000718 QualType R = getContext().VoidTy;
719
720 FunctionArgList Args;
721 // FIXME: This leaks
722 ImplicitParamDecl *Src =
723 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
724 getContext().getPointerType(getContext().VoidTy));
725
726 Args.push_back(std::make_pair(Src, Src->getType()));
727
728 const CGFunctionInfo &FI =
729 CGM.getTypes().getFunctionInfo(R, Args);
730
731 std::string Name = std::string("__destroy_helper_block_");
732 CodeGenTypes &Types = CGM.getTypes();
733 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
734
735 llvm::Function *Fn =
736 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
737 Name,
738 &CGM.getModule());
739
740 IdentifierInfo *II
741 = &CGM.getContext().Idents.get("__destroy_helper_block_");
742
743 FunctionDecl *FD = FunctionDecl::Create(getContext(),
744 getContext().getTranslationUnitDecl(),
745 SourceLocation(), II, R,
746 FunctionDecl::Static, false,
747 true);
748 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
749 // EmitStmt(BExpr->getBody());
750 CGF.FinishFunction();
751
752 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
753}
754
Mike Stumpcf62d392009-03-06 18:42:23 +0000755llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::Type *Ty) {
756 return CodeGenFunction(CGM).GenerateCopyHelperFunction(Ty);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000757}
758
Mike Stumpcf62d392009-03-06 18:42:23 +0000759llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::Type *Ty) {
760 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000761}
Mike Stump797b6322009-03-05 01:23:13 +0000762
Mike Stumpee094222009-03-06 06:12:24 +0000763llvm::Constant *BlockFunction::
764GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
Mike Stump45031c02009-03-06 02:29:21 +0000765 QualType R = getContext().VoidTy;
766
767 FunctionArgList Args;
768 // FIXME: This leaks
Mike Stumpee094222009-03-06 06:12:24 +0000769 ImplicitParamDecl *Dst =
770 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
771 getContext().getPointerType(getContext().VoidTy));
772 Args.push_back(std::make_pair(Dst, Dst->getType()));
773
774 // FIXME: This leaks
Mike Stump45031c02009-03-06 02:29:21 +0000775 ImplicitParamDecl *Src =
776 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
777 getContext().getPointerType(getContext().VoidTy));
Mike Stump45031c02009-03-06 02:29:21 +0000778 Args.push_back(std::make_pair(Src, Src->getType()));
779
780 const CGFunctionInfo &FI =
781 CGM.getTypes().getFunctionInfo(R, Args);
782
783 std::string Name = std::string("__Block_byref_id_object_copy_");
784 CodeGenTypes &Types = CGM.getTypes();
785 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
786
787 llvm::Function *Fn =
788 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
789 Name,
790 &CGM.getModule());
791
792 IdentifierInfo *II
793 = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
794
795 FunctionDecl *FD = FunctionDecl::Create(getContext(),
796 getContext().getTranslationUnitDecl(),
797 SourceLocation(), II, R,
798 FunctionDecl::Static, false,
799 true);
800 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpee094222009-03-06 06:12:24 +0000801
802 // dst->x
803 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
804 V = Builder.CreateBitCast(V, T);
805 V = Builder.CreateStructGEP(V, 6, "x");
806 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
807
808 // src->x
809 V = CGF.GetAddrOfLocalVar(Src);
810 V = Builder.CreateLoad(V);
811 V = Builder.CreateBitCast(V, T);
812 V = Builder.CreateStructGEP(V, 6, "x");
813 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
814 llvm::Value *SrcObj = Builder.CreateLoad(V);
815
816 flag |= BLOCK_BYREF_CALLER;
817
818 llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag);
819 llvm::Value *F = getBlockObjectAssign();
820 Builder.CreateCall3(F, DstObj, SrcObj, N);
821
Mike Stump45031c02009-03-06 02:29:21 +0000822 CGF.FinishFunction();
823
824 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
825}
826
Mike Stump1851b682009-03-06 04:53:30 +0000827llvm::Constant *
828BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
829 int flag) {
Mike Stump45031c02009-03-06 02:29:21 +0000830 QualType R = getContext().VoidTy;
831
832 FunctionArgList Args;
833 // FIXME: This leaks
834 ImplicitParamDecl *Src =
835 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
836 getContext().getPointerType(getContext().VoidTy));
837
838 Args.push_back(std::make_pair(Src, Src->getType()));
839
840 const CGFunctionInfo &FI =
841 CGM.getTypes().getFunctionInfo(R, Args);
842
843 std::string Name = std::string("__Block_byref_id_object_dispose_");
844 CodeGenTypes &Types = CGM.getTypes();
845 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
846
847 llvm::Function *Fn =
848 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
849 Name,
850 &CGM.getModule());
851
852 IdentifierInfo *II
853 = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
854
855 FunctionDecl *FD = FunctionDecl::Create(getContext(),
856 getContext().getTranslationUnitDecl(),
857 SourceLocation(), II, R,
858 FunctionDecl::Static, false,
859 true);
860 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1851b682009-03-06 04:53:30 +0000861
862 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
863 V = Builder.CreateBitCast(V, T);
864 V = Builder.CreateStructGEP(V, 6, "x");
865 V = Builder.CreateBitCast(V, PtrToInt8Ty);
866
867 // FIXME: Move to other one.
868 // int flag = BLOCK_FIELD_IS_BYREF;
869 // FIXME: Add weak support
870 if (0)
871 flag |= BLOCK_FIELD_IS_WEAK;
872 flag |= BLOCK_BYREF_CALLER;
873 BuildBlockRelease(V, flag);
Mike Stump45031c02009-03-06 02:29:21 +0000874 CGF.FinishFunction();
875
876 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
877}
878
Mike Stumpee094222009-03-06 06:12:24 +0000879llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
880 int flag) {
881 return CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag);
Mike Stump45031c02009-03-06 02:29:21 +0000882}
883
Mike Stump1851b682009-03-06 04:53:30 +0000884llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
885 int flag) {
886 return CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag);
Mike Stump45031c02009-03-06 02:29:21 +0000887}
888
Mike Stump797b6322009-03-05 01:23:13 +0000889llvm::Value *BlockFunction::getBlockObjectDispose() {
890 if (CGM.BlockObjectDispose == 0) {
891 const llvm::FunctionType *FTy;
892 std::vector<const llvm::Type*> ArgTys;
893 const llvm::Type *ResultType = llvm::Type::VoidTy;
894 ArgTys.push_back(PtrToInt8Ty);
895 ArgTys.push_back(llvm::Type::Int32Ty);
896 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stump00470a12009-03-05 08:32:30 +0000897 CGM.BlockObjectDispose
Mike Stump797b6322009-03-05 01:23:13 +0000898 = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
899 }
900 return CGM.BlockObjectDispose;
901}
902
Mike Stumpee094222009-03-06 06:12:24 +0000903llvm::Value *BlockFunction::getBlockObjectAssign() {
904 if (CGM.BlockObjectAssign == 0) {
905 const llvm::FunctionType *FTy;
906 std::vector<const llvm::Type*> ArgTys;
907 const llvm::Type *ResultType = llvm::Type::VoidTy;
908 ArgTys.push_back(PtrToInt8Ty);
909 ArgTys.push_back(PtrToInt8Ty);
910 ArgTys.push_back(llvm::Type::Int32Ty);
911 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
912 CGM.BlockObjectAssign
913 = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
914 }
915 return CGM.BlockObjectAssign;
916}
917
Mike Stump1851b682009-03-06 04:53:30 +0000918void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Mike Stump797b6322009-03-05 01:23:13 +0000919 llvm::Value *F = getBlockObjectDispose();
Mike Stump1851b682009-03-06 04:53:30 +0000920 llvm::Value *N;
Mike Stump797b6322009-03-05 01:23:13 +0000921 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Mike Stump1851b682009-03-06 04:53:30 +0000922 N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag);
Mike Stump797b6322009-03-05 01:23:13 +0000923 Builder.CreateCall2(F, V, N);
924}
Mike Stump00470a12009-03-05 08:32:30 +0000925
926ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }