blob: f10f0159e2ec5c2613805134cf507aaff558291a [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;
Mike Stump1bdbf632009-03-05 08:32:30 +0000560 bool subBlockHasCopyDispose;
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");
Mike Stump5225ad82009-03-07 16:32:25 +0000568 // FIXME: This causes a failure on clang-i686-linux, not sure why,
569 // disable for now.
570 // assert(!subBlockHasCopyDispose && "no imports allowed for global block");
Mike Stumpc4ae9632009-02-13 15:32:32 +0000571
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000572 // isa
Mike Stump5f87e9d2009-02-13 17:23:42 +0000573 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpc4ae9632009-02-13 15:32:32 +0000574
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000575 // Flags
Mike Stump1bdbf632009-03-05 08:32:30 +0000576 LiteralFields[1] =
Anders Carlsson63810c62009-03-01 21:09:29 +0000577 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000578
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000579 // Reserved
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000580 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000581
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000582 // Function
583 LiteralFields[3] = Fn;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000584
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000585 // Descriptor
586 LiteralFields[4] = Descriptor;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000587
588 llvm::Constant *BlockLiteralStruct =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000589 llvm::ConstantStruct::get(&LiteralFields[0], 5);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000590
591 llvm::GlobalVariable *BlockLiteral =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000592 new llvm::GlobalVariable(BlockLiteralStruct->getType(), true,
Mike Stumpc4ae9632009-02-13 15:32:32 +0000593 llvm::GlobalVariable::InternalLinkage,
594 BlockLiteralStruct, "__block_literal_global",
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000595 &getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000596
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000597 return BlockLiteral;
598}
599
Mike Stumpfca5da02009-02-21 20:00:35 +0000600llvm::Value *CodeGenFunction::LoadBlockStruct() {
601 return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self");
602}
603
Mike Stump1bdbf632009-03-05 08:32:30 +0000604llvm::Function *
605CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
606 const BlockInfo& Info,
607 uint64_t &Size,
608 uint64_t &Align,
609 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls,
610 bool &subBlockHasCopyDispose) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000611 const FunctionProtoType *FTy =
Chris Lattner8130e7f2009-02-28 19:01:03 +0000612 cast<FunctionProtoType>(BExpr->getFunctionType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000613
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000614 FunctionArgList Args;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000615
Chris Lattner8130e7f2009-02-28 19:01:03 +0000616 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000617
618 // FIXME: This leaks
Mike Stumpc4ae9632009-02-13 15:32:32 +0000619 ImplicitParamDecl *SelfDecl =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000620 ImplicitParamDecl::Create(getContext(), 0,
621 SourceLocation(), 0,
622 getContext().getPointerType(getContext().VoidTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000623
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000624 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stumpfca5da02009-02-21 20:00:35 +0000625 BlockStructDecl = SelfDecl;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000626
627 for (BlockDecl::param_iterator i = BD->param_begin(),
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000628 e = BD->param_end(); i != e; ++i)
Mike Stump459207f2009-02-17 23:25:52 +0000629 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000630
631 const CGFunctionInfo &FI =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000632 CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args);
633
Mike Stump084ba462009-02-14 22:16:35 +0000634 std::string Name = std::string("__") + Info.Name + "_block_invoke_";
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000635 CodeGenTypes &Types = CGM.getTypes();
636 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000637
638 llvm::Function *Fn =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000639 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
640 Name,
641 &CGM.getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000642
643 StartFunction(BD, FTy->getResultType(), Fn, Args,
Chris Lattner8130e7f2009-02-28 19:01:03 +0000644 BExpr->getBody()->getLocEnd());
645 EmitStmt(BExpr->getBody());
646 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000647
Mike Stumpf1711822009-02-25 23:33:13 +0000648 // The runtime needs a minimum alignment of a void *.
649 uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
650 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign);
651
Mike Stumpfca5da02009-02-21 20:00:35 +0000652 Size = BlockOffset;
Mike Stumpf1711822009-02-25 23:33:13 +0000653 Align = BlockAlign;
654 subBlockDeclRefDecls = BlockDeclRefDecls;
Mike Stump1bdbf632009-03-05 08:32:30 +0000655 subBlockHasCopyDispose |= BlockHasCopyDispose;
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000656 return Fn;
657}
Mike Stump2b6933f2009-02-28 09:07:16 +0000658
Mike Stump1fa52fe2009-03-07 02:35:30 +0000659uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
Mike Stump2b6933f2009-02-28 09:07:16 +0000660 const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
661
662 uint64_t Size = getContext().getTypeSize(D->getType()) / 8;
663 uint64_t Align = getContext().getDeclAlignInBytes(D);
664
665 if (BDRE->isByRef()) {
666 Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8;
667 Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
668 }
669
670 assert ((Align > 0) && "alignment must be 1 byte or more");
671
672 uint64_t OldOffset = BlockOffset;
673
674 // Ensure proper alignment, even if it means we have to have a gap
675 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align);
676 BlockAlign = std::max(Align, BlockAlign);
Mike Stump1bdbf632009-03-05 08:32:30 +0000677
Mike Stump2b6933f2009-02-28 09:07:16 +0000678 uint64_t Pad = BlockOffset - OldOffset;
679 if (Pad) {
680 llvm::ArrayType::get(llvm::Type::Int8Ty, Pad);
681 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
682 llvm::APInt(32, Pad),
683 ArrayType::Normal, 0);
684 ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
685 0, QualType(PadTy), VarDecl::None,
686 SourceLocation());
687 Expr *E;
688 E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
689 SourceLocation(), false, false);
690 BlockDeclRefDecls.push_back(E);
691 }
692 BlockDeclRefDecls.push_back(BDRE);
693
694 BlockOffset += Size;
695 return BlockOffset-Size;
696}
Mike Stumpad9605d2009-03-04 03:23:46 +0000697
Mike Stump1fa52fe2009-03-07 02:35:30 +0000698llvm::Constant *BlockFunction::
699GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
700 std::vector<HelperInfo> &NoteForHelper) {
Mike Stumpecd79422009-03-06 01:33:24 +0000701 QualType R = getContext().VoidTy;
702
703 FunctionArgList Args;
704 // FIXME: This leaks
Mike Stump1fa52fe2009-03-07 02:35:30 +0000705 ImplicitParamDecl *Dst =
706 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
707 getContext().getPointerType(getContext().VoidTy));
708 Args.push_back(std::make_pair(Dst, Dst->getType()));
Mike Stumpecd79422009-03-06 01:33:24 +0000709 ImplicitParamDecl *Src =
710 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
711 getContext().getPointerType(getContext().VoidTy));
Mike Stumpecd79422009-03-06 01:33:24 +0000712 Args.push_back(std::make_pair(Src, Src->getType()));
713
714 const CGFunctionInfo &FI =
715 CGM.getTypes().getFunctionInfo(R, Args);
716
717 std::string Name = std::string("__copy_helper_block_");
718 CodeGenTypes &Types = CGM.getTypes();
719 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
720
721 llvm::Function *Fn =
722 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
723 Name,
724 &CGM.getModule());
725
726 IdentifierInfo *II
727 = &CGM.getContext().Idents.get("__copy_helper_block_");
728
729 FunctionDecl *FD = FunctionDecl::Create(getContext(),
730 getContext().getTranslationUnitDecl(),
731 SourceLocation(), II, R,
732 FunctionDecl::Static, false,
733 true);
734 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1fa52fe2009-03-07 02:35:30 +0000735
736 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
737 llvm::Type *PtrPtrT;
738 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
739 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
740 SrcObj = Builder.CreateLoad(SrcObj);
741
742 llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
743 DstObj = Builder.CreateBitCast(DstObj, llvm::PointerType::get(T, 0));
744
745 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
746 int flag = NoteForHelper[i].flag;
747 int index = NoteForHelper[i].index;
748
749 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
750 || NoteForHelper[i].RequiresCopying) {
751 llvm::Value *Srcv = SrcObj;
752 Srcv = Builder.CreateStructGEP(Srcv, index);
753 Srcv = Builder.CreateBitCast(Srcv,
754 llvm::PointerType::get(PtrToInt8Ty, 0));
755 Srcv = Builder.CreateLoad(Srcv);
756
757 llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
758 Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
759
760 llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag);
761 llvm::Value *F = getBlockObjectAssign();
762 Builder.CreateCall3(F, Dstv, Srcv, N);
763 }
764 }
765
Mike Stumpecd79422009-03-06 01:33:24 +0000766 CGF.FinishFunction();
767
768 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpad9605d2009-03-04 03:23:46 +0000769}
770
Mike Stump9d8c1262009-03-06 18:42:23 +0000771llvm::Constant *BlockFunction::
Mike Stump1fa52fe2009-03-07 02:35:30 +0000772GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
773 const llvm::StructType* T,
774 std::vector<HelperInfo> &NoteForHelper) {
Mike Stumpecd79422009-03-06 01:33:24 +0000775 QualType R = getContext().VoidTy;
776
777 FunctionArgList Args;
778 // FIXME: This leaks
779 ImplicitParamDecl *Src =
780 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
781 getContext().getPointerType(getContext().VoidTy));
782
783 Args.push_back(std::make_pair(Src, Src->getType()));
784
785 const CGFunctionInfo &FI =
786 CGM.getTypes().getFunctionInfo(R, Args);
787
788 std::string Name = std::string("__destroy_helper_block_");
789 CodeGenTypes &Types = CGM.getTypes();
790 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
791
792 llvm::Function *Fn =
793 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
794 Name,
795 &CGM.getModule());
796
797 IdentifierInfo *II
798 = &CGM.getContext().Idents.get("__destroy_helper_block_");
799
800 FunctionDecl *FD = FunctionDecl::Create(getContext(),
801 getContext().getTranslationUnitDecl(),
802 SourceLocation(), II, R,
803 FunctionDecl::Static, false,
804 true);
805 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump05a6d4e2009-03-07 02:53:18 +0000806
807 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
808 llvm::Type *PtrPtrT;
809 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
810 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
811 SrcObj = Builder.CreateLoad(SrcObj);
812
813 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
814 int flag = NoteForHelper[i].flag;
815 int index = NoteForHelper[i].index;
816
817 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
818 || NoteForHelper[i].RequiresCopying) {
819 llvm::Value *Srcv = SrcObj;
820 Srcv = Builder.CreateStructGEP(Srcv, index);
821 Srcv = Builder.CreateBitCast(Srcv,
822 llvm::PointerType::get(PtrToInt8Ty, 0));
823 Srcv = Builder.CreateLoad(Srcv);
824
825 BuildBlockRelease(Srcv, flag);
826 }
827 }
828
Mike Stumpecd79422009-03-06 01:33:24 +0000829 CGF.FinishFunction();
830
831 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
832}
833
Mike Stump1fa52fe2009-03-07 02:35:30 +0000834llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
835 std::vector<HelperInfo> &NoteForHelper) {
836 return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
837 T, NoteForHelper);
Mike Stumpecd79422009-03-06 01:33:24 +0000838}
839
Mike Stump1fa52fe2009-03-07 02:35:30 +0000840llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
841 std::vector<HelperInfo> &NoteForHelper) {
842 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
843 T, NoteForHelper);
Mike Stumpad9605d2009-03-04 03:23:46 +0000844}
Mike Stump60294662009-03-05 01:23:13 +0000845
Mike Stump11973b62009-03-06 06:12:24 +0000846llvm::Constant *BlockFunction::
847GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
Mike Stumpf4b62342009-03-06 02:29:21 +0000848 QualType R = getContext().VoidTy;
849
850 FunctionArgList Args;
851 // FIXME: This leaks
Mike Stump11973b62009-03-06 06:12:24 +0000852 ImplicitParamDecl *Dst =
853 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
854 getContext().getPointerType(getContext().VoidTy));
855 Args.push_back(std::make_pair(Dst, Dst->getType()));
856
857 // FIXME: This leaks
Mike Stumpf4b62342009-03-06 02:29:21 +0000858 ImplicitParamDecl *Src =
859 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
860 getContext().getPointerType(getContext().VoidTy));
Mike Stumpf4b62342009-03-06 02:29:21 +0000861 Args.push_back(std::make_pair(Src, Src->getType()));
862
863 const CGFunctionInfo &FI =
864 CGM.getTypes().getFunctionInfo(R, Args);
865
866 std::string Name = std::string("__Block_byref_id_object_copy_");
867 CodeGenTypes &Types = CGM.getTypes();
868 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
869
870 llvm::Function *Fn =
871 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
872 Name,
873 &CGM.getModule());
874
875 IdentifierInfo *II
876 = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
877
878 FunctionDecl *FD = FunctionDecl::Create(getContext(),
879 getContext().getTranslationUnitDecl(),
880 SourceLocation(), II, R,
881 FunctionDecl::Static, false,
882 true);
883 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump11973b62009-03-06 06:12:24 +0000884
885 // dst->x
886 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
887 V = Builder.CreateBitCast(V, T);
888 V = Builder.CreateStructGEP(V, 6, "x");
889 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
890
891 // src->x
892 V = CGF.GetAddrOfLocalVar(Src);
893 V = Builder.CreateLoad(V);
894 V = Builder.CreateBitCast(V, T);
895 V = Builder.CreateStructGEP(V, 6, "x");
896 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
897 llvm::Value *SrcObj = Builder.CreateLoad(V);
898
899 flag |= BLOCK_BYREF_CALLER;
900
901 llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag);
902 llvm::Value *F = getBlockObjectAssign();
903 Builder.CreateCall3(F, DstObj, SrcObj, N);
904
Mike Stumpf4b62342009-03-06 02:29:21 +0000905 CGF.FinishFunction();
906
907 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
908}
909
Mike Stump4a0e5132009-03-06 04:53:30 +0000910llvm::Constant *
911BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
912 int flag) {
Mike Stumpf4b62342009-03-06 02:29:21 +0000913 QualType R = getContext().VoidTy;
914
915 FunctionArgList Args;
916 // FIXME: This leaks
917 ImplicitParamDecl *Src =
918 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
919 getContext().getPointerType(getContext().VoidTy));
920
921 Args.push_back(std::make_pair(Src, Src->getType()));
922
923 const CGFunctionInfo &FI =
924 CGM.getTypes().getFunctionInfo(R, Args);
925
926 std::string Name = std::string("__Block_byref_id_object_dispose_");
927 CodeGenTypes &Types = CGM.getTypes();
928 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
929
930 llvm::Function *Fn =
931 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
932 Name,
933 &CGM.getModule());
934
935 IdentifierInfo *II
936 = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
937
938 FunctionDecl *FD = FunctionDecl::Create(getContext(),
939 getContext().getTranslationUnitDecl(),
940 SourceLocation(), II, R,
941 FunctionDecl::Static, false,
942 true);
943 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump4a0e5132009-03-06 04:53:30 +0000944
945 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
946 V = Builder.CreateBitCast(V, T);
947 V = Builder.CreateStructGEP(V, 6, "x");
948 V = Builder.CreateBitCast(V, PtrToInt8Ty);
949
Mike Stump4a0e5132009-03-06 04:53:30 +0000950 flag |= BLOCK_BYREF_CALLER;
951 BuildBlockRelease(V, flag);
Mike Stumpf4b62342009-03-06 02:29:21 +0000952 CGF.FinishFunction();
953
954 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
955}
956
Mike Stump11973b62009-03-06 06:12:24 +0000957llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
958 int flag) {
959 return CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag);
Mike Stumpf4b62342009-03-06 02:29:21 +0000960}
961
Mike Stump4a0e5132009-03-06 04:53:30 +0000962llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
963 int flag) {
964 return CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag);
Mike Stumpf4b62342009-03-06 02:29:21 +0000965}
966
Mike Stump60294662009-03-05 01:23:13 +0000967llvm::Value *BlockFunction::getBlockObjectDispose() {
968 if (CGM.BlockObjectDispose == 0) {
969 const llvm::FunctionType *FTy;
970 std::vector<const llvm::Type*> ArgTys;
971 const llvm::Type *ResultType = llvm::Type::VoidTy;
972 ArgTys.push_back(PtrToInt8Ty);
973 ArgTys.push_back(llvm::Type::Int32Ty);
974 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stump1bdbf632009-03-05 08:32:30 +0000975 CGM.BlockObjectDispose
Mike Stump60294662009-03-05 01:23:13 +0000976 = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
977 }
978 return CGM.BlockObjectDispose;
979}
980
Mike Stump11973b62009-03-06 06:12:24 +0000981llvm::Value *BlockFunction::getBlockObjectAssign() {
982 if (CGM.BlockObjectAssign == 0) {
983 const llvm::FunctionType *FTy;
984 std::vector<const llvm::Type*> ArgTys;
985 const llvm::Type *ResultType = llvm::Type::VoidTy;
986 ArgTys.push_back(PtrToInt8Ty);
987 ArgTys.push_back(PtrToInt8Ty);
988 ArgTys.push_back(llvm::Type::Int32Ty);
989 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
990 CGM.BlockObjectAssign
991 = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
992 }
993 return CGM.BlockObjectAssign;
994}
995
Mike Stump4a0e5132009-03-06 04:53:30 +0000996void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Mike Stump60294662009-03-05 01:23:13 +0000997 llvm::Value *F = getBlockObjectDispose();
Mike Stump4a0e5132009-03-06 04:53:30 +0000998 llvm::Value *N;
Mike Stump60294662009-03-05 01:23:13 +0000999 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Mike Stump4a0e5132009-03-06 04:53:30 +00001000 N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag);
Mike Stump60294662009-03-05 01:23:13 +00001001 Builder.CreateCall2(F, V, N);
1002}
Mike Stump1bdbf632009-03-05 08:32:30 +00001003
1004ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stump1fa52fe2009-03-07 02:35:30 +00001005
1006BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1007 CGBuilderTy &B)
1008 : CGM(cgm), CGF(cgf), Builder(B) {
1009 PtrToInt8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1010
1011 BlockHasCopyDispose = false;
1012}