blob: dee9d4ce9b4110b778bf63d4f8f4dc9294b01f9c [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"
Mike Stumpfc5e6de2009-03-20 21:53:12 +000016#include "clang/AST/DeclObjC.h"
Anders Carlssond2a889b2009-02-12 00:39:25 +000017#include "llvm/Module.h"
Anders Carlsson1f1cd392009-02-12 17:55:02 +000018#include "llvm/Target/TargetData.h"
Anders Carlssond2a889b2009-02-12 00:39:25 +000019
20#include <algorithm>
21
22using namespace clang;
23using namespace CodeGen;
24
Mike Stumpb3a6fac2009-03-04 13:17:22 +000025// Temporary code to enable testing of __block variables
26// #include "clang/Frontend/CompileOptions.h"
27#include "llvm/Support/CommandLine.h"
Mike Stumpb3a6fac2009-03-04 13:17:22 +000028
Mike Stump9d8c1262009-03-06 18:42:23 +000029llvm::Constant *CodeGenFunction::
Mike Stump68bb7a22009-03-25 17:58:24 +000030BuildDescriptorBlockDecl(bool BlockHasCopyDispose, uint64_t Size,
31 const llvm::StructType* Ty,
Mike Stump1fa52fe2009-03-07 02:35:30 +000032 std::vector<HelperInfo> *NoteForHelper) {
Mike Stumpff8f0872009-02-13 16:55:51 +000033 const llvm::Type *UnsignedLongTy
34 = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpb95bc002009-02-13 16:19:19 +000035 llvm::Constant *C;
36 std::vector<llvm::Constant*> Elts;
37
38 // reserved
Mike Stumpff8f0872009-02-13 16:55:51 +000039 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpb95bc002009-02-13 16:19:19 +000040 Elts.push_back(C);
41
42 // Size
Mike Stump139c3962009-02-21 20:07:44 +000043 // FIXME: What is the right way to say this doesn't fit? We should give
44 // a user diagnostic in that case. Better fix would be to change the
45 // API to size_t.
Mike Stumpfca5da02009-02-21 20:00:35 +000046 C = llvm::ConstantInt::get(UnsignedLongTy, Size);
Mike Stumpb95bc002009-02-13 16:19:19 +000047 Elts.push_back(C);
48
49 if (BlockHasCopyDispose) {
50 // copy_func_helper_decl
Mike Stump2f9a4262009-04-10 18:52:28 +000051 Elts.push_back(BuildCopyHelper(Ty, NoteForHelper));
Mike Stumpb95bc002009-02-13 16:19:19 +000052
53 // destroy_func_decl
Mike Stump2f9a4262009-04-10 18:52:28 +000054 Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper));
Mike Stumpb95bc002009-02-13 16:19:19 +000055 }
56
57 C = llvm::ConstantStruct::get(Elts);
58
Mike Stumpb95bc002009-02-13 16:19:19 +000059 C = new llvm::GlobalVariable(C->getType(), true,
60 llvm::GlobalValue::InternalLinkage,
Mike Stump92ea8882009-02-13 20:17:16 +000061 C, "__block_descriptor_tmp", &CGM.getModule());
Mike Stumpb95bc002009-02-13 16:19:19 +000062 return C;
63}
64
Mike Stump1f010b52009-03-04 18:17:45 +000065llvm::Constant *BlockModule::getNSConcreteGlobalBlock() {
Mike Stump5f87e9d2009-02-13 17:23:42 +000066 if (NSConcreteGlobalBlock)
67 return NSConcreteGlobalBlock;
68
Mike Stump56447902009-02-13 19:38:12 +000069 // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
Mike Stumpf13eac12009-03-04 22:48:06 +000070 // same thing as CreateRuntimeFunction if there's already a variable with the
71 // same name.
Mike Stump5f87e9d2009-02-13 17:23:42 +000072 NSConcreteGlobalBlock
Mike Stump1bdbf632009-03-05 08:32:30 +000073 = new llvm::GlobalVariable(PtrToInt8Ty, false,
Mike Stump5f87e9d2009-02-13 17:23:42 +000074 llvm::GlobalValue::ExternalLinkage,
75 0, "_NSConcreteGlobalBlock",
76 &getModule());
77
78 return NSConcreteGlobalBlock;
79}
80
Mike Stump1f010b52009-03-04 18:17:45 +000081llvm::Constant *BlockModule::getNSConcreteStackBlock() {
Mike Stumpeb3a5ca2009-02-13 19:29:27 +000082 if (NSConcreteStackBlock)
83 return NSConcreteStackBlock;
84
Mike Stump56447902009-02-13 19:38:12 +000085 // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
Mike Stumpf13eac12009-03-04 22:48:06 +000086 // same thing as CreateRuntimeFunction if there's already a variable with the
87 // same name.
Mike Stumpeb3a5ca2009-02-13 19:29:27 +000088 NSConcreteStackBlock
Mike Stump1bdbf632009-03-05 08:32:30 +000089 = new llvm::GlobalVariable(PtrToInt8Ty, false,
Mike Stumpeb3a5ca2009-02-13 19:29:27 +000090 llvm::GlobalValue::ExternalLinkage,
91 0, "_NSConcreteStackBlock",
92 &getModule());
93
94 return NSConcreteStackBlock;
95}
96
Mike Stump1bdbf632009-03-05 08:32:30 +000097static void CollectBlockDeclRefInfo(const Stmt *S,
Anders Carlsson6cf64be2009-03-01 01:09:12 +000098 CodeGenFunction::BlockInfo &Info) {
99 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
100 I != E; ++I)
Daniel Dunbar56941d32009-03-02 07:00:57 +0000101 if (*I)
102 CollectBlockDeclRefInfo(*I, Info);
Mike Stump1bdbf632009-03-05 08:32:30 +0000103
Anders Carlsson6cf64be2009-03-01 01:09:12 +0000104 if (const BlockDeclRefExpr *DE = dyn_cast<BlockDeclRefExpr>(S)) {
105 // FIXME: Handle enums.
106 if (isa<FunctionDecl>(DE->getDecl()))
107 return;
Mike Stump1bdbf632009-03-05 08:32:30 +0000108
Anders Carlsson6cf64be2009-03-01 01:09:12 +0000109 if (DE->isByRef())
110 Info.ByRefDeclRefs.push_back(DE);
111 else
112 Info.ByCopyDeclRefs.push_back(DE);
113 }
114}
115
Mike Stumpf13eac12009-03-04 22:48:06 +0000116/// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be
117/// declared as a global variable instead of on the stack.
Anders Carlsson6cf64be2009-03-01 01:09:12 +0000118static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info)
119{
120 return Info.ByRefDeclRefs.empty() && Info.ByCopyDeclRefs.empty();
121}
122
Mike Stumpf13eac12009-03-04 22:48:06 +0000123// FIXME: Push most into CGM, passing down a few bits, like current function
124// name.
Mike Stumpf1711822009-02-25 23:33:13 +0000125llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Mike Stumpb95bc002009-02-13 16:19:19 +0000126
Anders Carlsson6cf64be2009-03-01 01:09:12 +0000127 std::string Name = CurFn->getName();
128 CodeGenFunction::BlockInfo Info(0, Name.c_str());
129 CollectBlockDeclRefInfo(BE->getBody(), Info);
130
131 // Check if the block can be global.
Mike Stumpf13eac12009-03-04 22:48:06 +0000132 // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like
133 // to just have one code path. We should move this function into CGM and pass
134 // CGF, then we can just check to see if CGF is 0.
Mike Stumpad9605d2009-03-04 03:23:46 +0000135 if (0 && CanBlockBeGlobal(Info))
Anders Carlsson6cf64be2009-03-01 01:09:12 +0000136 return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
Mike Stump1bdbf632009-03-05 08:32:30 +0000137
138 std::vector<llvm::Constant*> Elts(5);
Mike Stumpb95bc002009-02-13 16:19:19 +0000139 llvm::Constant *C;
Mike Stumpf1711822009-02-25 23:33:13 +0000140 llvm::Value *V;
Mike Stumpb95bc002009-02-13 16:19:19 +0000141
Mike Stumpb95bc002009-02-13 16:19:19 +0000142 {
143 // C = BuildBlockStructInitlist();
144 unsigned int flags = BLOCK_HAS_DESCRIPTOR;
145
Mike Stump1bdbf632009-03-05 08:32:30 +0000146 // We run this first so that we set BlockHasCopyDispose from the entire
147 // block literal.
148 // __invoke
149 uint64_t subBlockSize, subBlockAlign;
150 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Mike Stump68bb7a22009-03-25 17:58:24 +0000151 bool subBlockHasCopyDispose = false;
Mike Stump1bdbf632009-03-05 08:32:30 +0000152 llvm::Function *Fn
Mike Stumpfc5e6de2009-03-20 21:53:12 +0000153 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl, LocalDeclMap,
Mike Stumpa20c59e2009-03-13 23:34:28 +0000154 subBlockSize,
Mike Stump1bdbf632009-03-05 08:32:30 +0000155 subBlockAlign,
156 subBlockDeclRefDecls,
Mike Stump68bb7a22009-03-25 17:58:24 +0000157 subBlockHasCopyDispose);
158 BlockHasCopyDispose |= subBlockHasCopyDispose;
Mike Stump1bdbf632009-03-05 08:32:30 +0000159 Elts[3] = Fn;
160
Mike Stump159875f2009-05-01 01:31:57 +0000161 // FIXME: Don't use BlockHasCopyDispose, it is set more often then necessary, for
162 // example: { ^{ __block int i; ^{ i = 1; }(); }(); }
Mike Stump68bb7a22009-03-25 17:58:24 +0000163 if (subBlockHasCopyDispose)
Mike Stumpb95bc002009-02-13 16:19:19 +0000164 flags |= BLOCK_HAS_COPY_DISPOSE;
165
Mike Stump92ea8882009-02-13 20:17:16 +0000166 // __isa
Mike Stumpeb3a5ca2009-02-13 19:29:27 +0000167 C = CGM.getNSConcreteStackBlock();
Mike Stumpb95bc002009-02-13 16:19:19 +0000168 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
Mike Stump1bdbf632009-03-05 08:32:30 +0000169 Elts[0] = C;
Mike Stumpb95bc002009-02-13 16:19:19 +0000170
171 // __flags
172 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
173 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
174 C = llvm::ConstantInt::get(IntTy, flags);
Mike Stump1bdbf632009-03-05 08:32:30 +0000175 Elts[1] = C;
Mike Stumpb95bc002009-02-13 16:19:19 +0000176
177 // __reserved
178 C = llvm::ConstantInt::get(IntTy, 0);
Mike Stump1bdbf632009-03-05 08:32:30 +0000179 Elts[2] = C;
Mike Stumpb95bc002009-02-13 16:19:19 +0000180
Mike Stumpf1711822009-02-25 23:33:13 +0000181 if (subBlockDeclRefDecls.size() == 0) {
Mike Stump9d8c1262009-03-06 18:42:23 +0000182 // __descriptor
Mike Stump68bb7a22009-03-25 17:58:24 +0000183 Elts[4] = BuildDescriptorBlockDecl(subBlockHasCopyDispose, subBlockSize, 0, 0);
Mike Stump9d8c1262009-03-06 18:42:23 +0000184
Mike Stumpa7db9be2009-03-01 20:07:53 +0000185 // Optimize to being a global block.
186 Elts[0] = CGM.getNSConcreteGlobalBlock();
187 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
188
Mike Stumpf1711822009-02-25 23:33:13 +0000189 C = llvm::ConstantStruct::get(Elts);
190
191 char Name[32];
192 sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
193 C = new llvm::GlobalVariable(C->getType(), true,
194 llvm::GlobalValue::InternalLinkage,
195 C, Name, &CGM.getModule());
196 QualType BPT = BE->getType();
197 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
198 return C;
199 }
Mike Stump1bdbf632009-03-05 08:32:30 +0000200
Mike Stumpf1711822009-02-25 23:33:13 +0000201 std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size());
Mike Stump1fa52fe2009-03-07 02:35:30 +0000202 for (int i=0; i<4; ++i)
Mike Stumpf1711822009-02-25 23:33:13 +0000203 Types[i] = Elts[i]->getType();
Mike Stump1fa52fe2009-03-07 02:35:30 +0000204 Types[4] = PtrToInt8Ty;
Mike Stumpf1711822009-02-25 23:33:13 +0000205
Mike Stump2b6933f2009-02-28 09:07:16 +0000206 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
207 const Expr *E = subBlockDeclRefDecls[i];
208 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
209 QualType Ty = E->getType();
Mike Stumpad9605d2009-03-04 03:23:46 +0000210 if (BDRE && BDRE->isByRef()) {
211 uint64_t Align = getContext().getDeclAlignInBytes(BDRE->getDecl());
212 Types[i+5] = llvm::PointerType::get(BuildByRefType(Ty, Align), 0);
213 } else
214 Types[i+5] = ConvertType(Ty);
Mike Stump2b6933f2009-02-28 09:07:16 +0000215 }
Mike Stumpf1711822009-02-25 23:33:13 +0000216
Mike Stump1fa52fe2009-03-07 02:35:30 +0000217 llvm::StructType *Ty = llvm::StructType::get(Types, true);
Mike Stumpf1711822009-02-25 23:33:13 +0000218
219 llvm::AllocaInst *A = CreateTempAlloca(Ty);
220 A->setAlignment(subBlockAlign);
221 V = A;
222
Mike Stump1fa52fe2009-03-07 02:35:30 +0000223 std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size());
224 int helpersize = 0;
225
226 for (unsigned i=0; i<4; ++i)
Mike Stumpf1711822009-02-25 23:33:13 +0000227 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
Mike Stump1bdbf632009-03-05 08:32:30 +0000228
Mike Stumpf1711822009-02-25 23:33:13 +0000229 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
230 {
Mike Stump2b6933f2009-02-28 09:07:16 +0000231 // FIXME: Push const down.
232 Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
233 DeclRefExpr *DR;
234 ValueDecl *VD;
Mike Stumpf1711822009-02-25 23:33:13 +0000235
Mike Stump2b6933f2009-02-28 09:07:16 +0000236 DR = dyn_cast<DeclRefExpr>(E);
237 // Skip padding.
238 if (DR) continue;
239
240 BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
241 VD = BDRE->getDecl();
242
Mike Stumpad9605d2009-03-04 03:23:46 +0000243 llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp");
Mike Stump1fa52fe2009-03-07 02:35:30 +0000244 NoteForHelper[helpersize].index = i+5;
245 NoteForHelper[helpersize].RequiresCopying = BlockRequiresCopying(VD->getType());
246 NoteForHelper[helpersize].flag
247 = VD->getType()->isBlockPointerType() ? BLOCK_FIELD_IS_BLOCK : BLOCK_FIELD_IS_OBJECT;
248
Mike Stump2b6933f2009-02-28 09:07:16 +0000249 if (LocalDeclMap[VD]) {
Mike Stumpad9605d2009-03-04 03:23:46 +0000250 if (BDRE->isByRef()) {
Mike Stump1fa52fe2009-03-07 02:35:30 +0000251 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
Mike Stump0a33ed32009-03-07 06:04:31 +0000252 // FIXME: Someone double check this.
253 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpad9605d2009-03-04 03:23:46 +0000254 const llvm::Type *Ty = Types[i+5];
255 llvm::Value *Loc = LocalDeclMap[VD];
256 Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
257 Loc = Builder.CreateLoad(Loc, false);
258 Loc = Builder.CreateBitCast(Loc, Ty);
259 Builder.CreateStore(Loc, Addr);
Mike Stump1fa52fe2009-03-07 02:35:30 +0000260 ++helpersize;
Mike Stumpad9605d2009-03-04 03:23:46 +0000261 continue;
262 } else
263 E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD),
264 VD->getType(), SourceLocation(),
265 false, false);
Mike Stump2b6933f2009-02-28 09:07:16 +0000266 }
Mike Stumpad9605d2009-03-04 03:23:46 +0000267 if (BDRE->isByRef()) {
Mike Stump29782de2009-03-21 21:00:35 +0000268 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
269 // FIXME: Someone double check this.
270 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stump2b6933f2009-02-28 09:07:16 +0000271 E = new (getContext())
272 UnaryOperator(E, UnaryOperator::AddrOf,
273 getContext().getPointerType(E->getType()),
274 SourceLocation());
Mike Stumpad9605d2009-03-04 03:23:46 +0000275 }
Mike Stump1fa52fe2009-03-07 02:35:30 +0000276 ++helpersize;
Mike Stump2b6933f2009-02-28 09:07:16 +0000277
Mike Stump2b6933f2009-02-28 09:07:16 +0000278 RValue r = EmitAnyExpr(E, Addr, false);
Mike Stumpad9605d2009-03-04 03:23:46 +0000279 if (r.isScalar()) {
280 llvm::Value *Loc = r.getScalarVal();
281 const llvm::Type *Ty = Types[i+5];
282 if (BDRE->isByRef()) {
Mike Stumpf13eac12009-03-04 22:48:06 +0000283 // E is now the address of the value field, instead, we want the
284 // address of the actual ByRef struct. We optimize this slightly
285 // compared to gcc by not grabbing the forwarding slot as this must
286 // be done during Block_copy for us, and we can postpone the work
287 // until then.
288 uint64_t offset = BlockDecls[BDRE->getDecl()];
Mike Stump1bdbf632009-03-05 08:32:30 +0000289
Mike Stumpf13eac12009-03-04 22:48:06 +0000290 llvm::Value *BlockLiteral = LoadBlockStruct();
Mike Stump1bdbf632009-03-05 08:32:30 +0000291
Mike Stumpf13eac12009-03-04 22:48:06 +0000292 Loc = Builder.CreateGEP(BlockLiteral,
293 llvm::ConstantInt::get(llvm::Type::Int64Ty,
294 offset),
295 "block.literal");
296 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpad9605d2009-03-04 03:23:46 +0000297 Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stumpf13eac12009-03-04 22:48:06 +0000298 Loc = Builder.CreateLoad(Loc, false);
299 // Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stumpad9605d2009-03-04 03:23:46 +0000300 }
301 Builder.CreateStore(Loc, Addr);
302 } else if (r.isComplex())
Mike Stumpf1711822009-02-25 23:33:13 +0000303 // FIXME: implement
304 ErrorUnsupported(BE, "complex in block literal");
305 else if (r.isAggregate())
306 ; // Already created into the destination
307 else
308 assert (0 && "bad block variable");
309 // FIXME: Ensure that the offset created by the backend for
310 // the struct matches the previously computed offset in BlockDecls.
311 }
Mike Stump1fa52fe2009-03-07 02:35:30 +0000312 NoteForHelper.resize(helpersize);
Mike Stump9d8c1262009-03-06 18:42:23 +0000313
314 // __descriptor
Mike Stump68bb7a22009-03-25 17:58:24 +0000315 llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockHasCopyDispose,
316 subBlockSize, Ty,
Mike Stump1fa52fe2009-03-07 02:35:30 +0000317 &NoteForHelper);
318 Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
319 Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
Mike Stumpb95bc002009-02-13 16:19:19 +0000320 }
Mike Stump1bdbf632009-03-05 08:32:30 +0000321
Mike Stumpd55240e2009-02-19 01:01:04 +0000322 QualType BPT = BE->getType();
Mike Stumpf1711822009-02-25 23:33:13 +0000323 return Builder.CreateBitCast(V, ConvertType(BPT));
Mike Stumpb95bc002009-02-13 16:19:19 +0000324}
325
326
Mike Stump1f010b52009-03-04 18:17:45 +0000327const llvm::Type *BlockModule::getBlockDescriptorType() {
Mike Stump95e54802009-02-13 15:16:56 +0000328 if (BlockDescriptorType)
329 return BlockDescriptorType;
330
Mike Stumpc4ae9632009-02-13 15:32:32 +0000331 const llvm::Type *UnsignedLongTy =
Mike Stump95e54802009-02-13 15:16:56 +0000332 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000333
Mike Stump95e54802009-02-13 15:16:56 +0000334 // struct __block_descriptor {
335 // unsigned long reserved;
336 // unsigned long block_size;
337 // };
Mike Stumpc4ae9632009-02-13 15:32:32 +0000338 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy,
339 UnsignedLongTy,
Mike Stump95e54802009-02-13 15:16:56 +0000340 NULL);
341
342 getModule().addTypeName("struct.__block_descriptor",
343 BlockDescriptorType);
344
345 return BlockDescriptorType;
Anders Carlssond2a889b2009-02-12 00:39:25 +0000346}
347
Mike Stump1f010b52009-03-04 18:17:45 +0000348const llvm::Type *BlockModule::getGenericBlockLiteralType() {
Mike Stump0dffa462009-02-13 15:25:34 +0000349 if (GenericBlockLiteralType)
350 return GenericBlockLiteralType;
351
Mike Stumpc4ae9632009-02-13 15:32:32 +0000352 const llvm::Type *BlockDescPtrTy =
Mike Stump0dffa462009-02-13 15:25:34 +0000353 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000354
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000355 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
356 getTypes().ConvertType(getContext().IntTy));
357
Mike Stump0dffa462009-02-13 15:25:34 +0000358 // struct __block_literal_generic {
Mike Stumpd55240e2009-02-19 01:01:04 +0000359 // void *__isa;
360 // int __flags;
361 // int __reserved;
362 // void (*__invoke)(void *);
363 // struct __block_descriptor *__descriptor;
Mike Stump0dffa462009-02-13 15:25:34 +0000364 // };
Mike Stump60294662009-03-05 01:23:13 +0000365 GenericBlockLiteralType = llvm::StructType::get(PtrToInt8Ty,
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000366 IntTy,
367 IntTy,
Mike Stump60294662009-03-05 01:23:13 +0000368 PtrToInt8Ty,
Mike Stump0dffa462009-02-13 15:25:34 +0000369 BlockDescPtrTy,
370 NULL);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000371
Mike Stump0dffa462009-02-13 15:25:34 +0000372 getModule().addTypeName("struct.__block_literal_generic",
373 GenericBlockLiteralType);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000374
Mike Stump0dffa462009-02-13 15:25:34 +0000375 return GenericBlockLiteralType;
Anders Carlssond2a889b2009-02-12 00:39:25 +0000376}
377
Mike Stump1f010b52009-03-04 18:17:45 +0000378const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() {
Mike Stumpd55240e2009-02-19 01:01:04 +0000379 if (GenericExtendedBlockLiteralType)
380 return GenericExtendedBlockLiteralType;
381
Mike Stumpd55240e2009-02-19 01:01:04 +0000382 const llvm::Type *BlockDescPtrTy =
383 llvm::PointerType::getUnqual(getBlockDescriptorType());
384
385 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
386 getTypes().ConvertType(getContext().IntTy));
387
388 // struct __block_literal_generic {
389 // void *__isa;
390 // int __flags;
391 // int __reserved;
392 // void (*__invoke)(void *);
393 // struct __block_descriptor *__descriptor;
394 // void *__copy_func_helper_decl;
395 // void *__destroy_func_decl;
396 // };
Mike Stump60294662009-03-05 01:23:13 +0000397 GenericExtendedBlockLiteralType = llvm::StructType::get(PtrToInt8Ty,
Mike Stumpd55240e2009-02-19 01:01:04 +0000398 IntTy,
399 IntTy,
Mike Stump60294662009-03-05 01:23:13 +0000400 PtrToInt8Ty,
Mike Stumpd55240e2009-02-19 01:01:04 +0000401 BlockDescPtrTy,
Mike Stump60294662009-03-05 01:23:13 +0000402 PtrToInt8Ty,
403 PtrToInt8Ty,
Mike Stumpd55240e2009-02-19 01:01:04 +0000404 NULL);
405
406 getModule().addTypeName("struct.__block_literal_extended_generic",
407 GenericExtendedBlockLiteralType);
408
409 return GenericExtendedBlockLiteralType;
410}
411
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000412RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpc4ae9632009-02-13 15:32:32 +0000413 const BlockPointerType *BPT =
Anders Carlssond2a889b2009-02-12 00:39:25 +0000414 E->getCallee()->getType()->getAsBlockPointerType();
Mike Stumpc4ae9632009-02-13 15:32:32 +0000415
Anders Carlssond2a889b2009-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 Stump0dffa462009-02-13 15:25:34 +0000420 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssond2a889b2009-02-12 00:39:25 +0000421
422 // Bitcast the callee to a block literal.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000423 llvm::Value *BlockLiteral =
Anders Carlssond2a889b2009-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");
Anders Carlssond2a889b2009-02-12 00:39:25 +0000428
Mike Stumpc4ae9632009-02-13 15:32:32 +0000429 BlockLiteral =
430 Builder.CreateBitCast(BlockLiteral,
Anders Carlssond2a889b2009-02-12 00:39:25 +0000431 llvm::PointerType::getUnqual(llvm::Type::Int8Ty),
432 "tmp");
Mike Stumpc4ae9632009-02-13 15:32:32 +0000433
Anders Carlssond2a889b2009-02-12 00:39:25 +0000434 // Add the block literal.
435 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
436 CallArgList Args;
437 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000438
Anders Carlsson6759c4d2009-04-08 23:13:16 +0000439 QualType FnType = BPT->getPointeeType();
440
Anders Carlssond2a889b2009-02-12 00:39:25 +0000441 // And the rest of the arguments.
Anders Carlsson6759c4d2009-04-08 23:13:16 +0000442 EmitCallArgs(Args, FnType->getAsFunctionProtoType(),
443 E->arg_begin(), E->arg_end());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000444
Anders Carlsson417d9832009-04-07 22:10:22 +0000445 // Load the function.
446 llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp");
447
Anders Carlssonea92a922009-04-08 02:55:55 +0000448 QualType ResultType = FnType->getAsFunctionType()->getResultType();
449
450 const CGFunctionInfo &FnInfo =
451 CGM.getTypes().getFunctionInfo(ResultType, Args);
Anders Carlsson417d9832009-04-07 22:10:22 +0000452
453 // Cast the function pointer to the right type.
454 const llvm::Type *BlockFTy =
Anders Carlssonea92a922009-04-08 02:55:55 +0000455 CGM.getTypes().GetFunctionType(FnInfo, false);
Anders Carlsson417d9832009-04-07 22:10:22 +0000456
457 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
458 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
459
Anders Carlssond2a889b2009-02-12 00:39:25 +0000460 // And call the block.
Anders Carlsson3f03b812009-04-07 00:20:24 +0000461 return EmitCall(FnInfo, Func, Args);
Anders Carlssond2a889b2009-02-12 00:39:25 +0000462}
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000463
Mike Stumpad9605d2009-03-04 03:23:46 +0000464llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
465 uint64_t &offset = BlockDecls[E->getDecl()];
466
467 const llvm::Type *Ty;
468 Ty = CGM.getTypes().ConvertType(E->getDecl()->getType());
469
Mike Stumpad9605d2009-03-04 03:23:46 +0000470 // See if we have already allocated an offset for this variable.
471 if (offset == 0) {
Mike Stump1bdbf632009-03-05 08:32:30 +0000472 // Don't run the expensive check, unless we have to.
473 if (!BlockHasCopyDispose && BlockRequiresCopying(E->getType()))
474 BlockHasCopyDispose = true;
Mike Stumpad9605d2009-03-04 03:23:46 +0000475 // if not, allocate one now.
476 offset = getBlockOffset(E);
477 }
478
479 llvm::Value *BlockLiteral = LoadBlockStruct();
480 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
481 llvm::ConstantInt::get(llvm::Type::Int64Ty,
482 offset),
Mike Stumpf13eac12009-03-04 22:48:06 +0000483 "block.literal");
Mike Stumpad9605d2009-03-04 03:23:46 +0000484 if (E->isByRef()) {
485 bool needsCopyDispose = BlockRequiresCopying(E->getType());
486 uint64_t Align = getContext().getDeclAlignInBytes(E->getDecl());
487 const llvm::Type *PtrStructTy
488 = llvm::PointerType::get(BuildByRefType(E->getType(), Align), 0);
Mike Stump29782de2009-03-21 21:00:35 +0000489 // The block literal will need a copy/destroy helper.
490 BlockHasCopyDispose = true;
Mike Stumpad9605d2009-03-04 03:23:46 +0000491 Ty = PtrStructTy;
492 Ty = llvm::PointerType::get(Ty, 0);
493 V = Builder.CreateBitCast(V, Ty);
494 V = Builder.CreateLoad(V, false);
495 V = Builder.CreateStructGEP(V, 1, "forwarding");
496 V = Builder.CreateLoad(V, false);
497 V = Builder.CreateBitCast(V, PtrStructTy);
498 V = Builder.CreateStructGEP(V, needsCopyDispose*2 + 4, "x");
499 } else {
500 Ty = llvm::PointerType::get(Ty, 0);
501 V = Builder.CreateBitCast(V, Ty);
502 }
503 return V;
504}
505
Mike Stumpfc5e6de2009-03-20 21:53:12 +0000506void CodeGenFunction::BlockForwardSelf() {
507 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
508 ImplicitParamDecl *SelfDecl = OMD->getSelfDecl();
509 llvm::Value *&DMEntry = LocalDeclMap[SelfDecl];
510 if (DMEntry)
511 return;
512 // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care
513 BlockDeclRefExpr *BDRE = new (getContext())
514 BlockDeclRefExpr(SelfDecl,
515 SelfDecl->getType(), SourceLocation(), false);
516 DMEntry = GetAddrOfBlockDecl(BDRE);
517}
518
Mike Stump084ba462009-02-14 22:16:35 +0000519llvm::Constant *
Mike Stumpd6d0ebe2009-03-04 18:47:42 +0000520BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000521 // Generate the block descriptor.
522 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000523 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
524 getTypes().ConvertType(getContext().IntTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000525
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000526 llvm::Constant *DescriptorFields[2];
Mike Stumpc4ae9632009-02-13 15:32:32 +0000527
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000528 // Reserved
529 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000530
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000531 // Block literal size. For global blocks we just use the size of the generic
532 // block literal struct.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000533 uint64_t BlockLiteralSize =
Mike Stump0dffa462009-02-13 15:25:34 +0000534 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000535 DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000536
537 llvm::Constant *DescriptorStruct =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000538 llvm::ConstantStruct::get(&DescriptorFields[0], 2);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000539
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000540 llvm::GlobalVariable *Descriptor =
541 new llvm::GlobalVariable(DescriptorStruct->getType(), true,
Mike Stumpc4ae9632009-02-13 15:32:32 +0000542 llvm::GlobalVariable::InternalLinkage,
543 DescriptorStruct, "__block_descriptor_global",
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000544 &getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000545
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000546 // Generate the constants for the block literal.
547 llvm::Constant *LiteralFields[5];
Mike Stumpc4ae9632009-02-13 15:32:32 +0000548
Mike Stump084ba462009-02-14 22:16:35 +0000549 CodeGenFunction::BlockInfo Info(0, n);
Mike Stumpf1711822009-02-25 23:33:13 +0000550 uint64_t subBlockSize, subBlockAlign;
Mike Stump2b6933f2009-02-28 09:07:16 +0000551 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Daniel Dunbaradaf35f2009-03-12 03:07:24 +0000552 bool subBlockHasCopyDispose = false;
Mike Stumpa20c59e2009-03-13 23:34:28 +0000553 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Mike Stumpfca5da02009-02-21 20:00:35 +0000554 llvm::Function *Fn
Mike Stumpfc5e6de2009-03-20 21:53:12 +0000555 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap,
Mike Stumpa20c59e2009-03-13 23:34:28 +0000556 subBlockSize,
Mike Stumpd6d0ebe2009-03-04 18:47:42 +0000557 subBlockAlign,
Mike Stump1bdbf632009-03-05 08:32:30 +0000558 subBlockDeclRefDecls,
559 subBlockHasCopyDispose);
Mike Stumpfca5da02009-02-21 20:00:35 +0000560 assert(subBlockSize == BlockLiteralSize
561 && "no imports allowed for global block");
Mike Stumpc4ae9632009-02-13 15:32:32 +0000562
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000563 // isa
Mike Stump5f87e9d2009-02-13 17:23:42 +0000564 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpc4ae9632009-02-13 15:32:32 +0000565
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000566 // Flags
Mike Stump1bdbf632009-03-05 08:32:30 +0000567 LiteralFields[1] =
Anders Carlsson63810c62009-03-01 21:09:29 +0000568 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000569
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000570 // Reserved
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000571 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000572
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000573 // Function
574 LiteralFields[3] = Fn;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000575
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000576 // Descriptor
577 LiteralFields[4] = Descriptor;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000578
579 llvm::Constant *BlockLiteralStruct =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000580 llvm::ConstantStruct::get(&LiteralFields[0], 5);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000581
582 llvm::GlobalVariable *BlockLiteral =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000583 new llvm::GlobalVariable(BlockLiteralStruct->getType(), true,
Mike Stumpc4ae9632009-02-13 15:32:32 +0000584 llvm::GlobalVariable::InternalLinkage,
585 BlockLiteralStruct, "__block_literal_global",
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000586 &getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000587
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000588 return BlockLiteral;
589}
590
Mike Stumpfca5da02009-02-21 20:00:35 +0000591llvm::Value *CodeGenFunction::LoadBlockStruct() {
592 return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self");
593}
594
Mike Stump1bdbf632009-03-05 08:32:30 +0000595llvm::Function *
596CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
597 const BlockInfo& Info,
Mike Stumpfc5e6de2009-03-20 21:53:12 +0000598 const Decl *OuterFuncDecl,
Mike Stumpa20c59e2009-03-13 23:34:28 +0000599 llvm::DenseMap<const Decl*, llvm::Value*> ldm,
Mike Stump1bdbf632009-03-05 08:32:30 +0000600 uint64_t &Size,
601 uint64_t &Align,
602 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls,
603 bool &subBlockHasCopyDispose) {
Devang Patel6ae64bb2009-04-15 21:51:44 +0000604
605 // Check if we should generate debug info for this block.
606 if (CGM.getDebugInfo())
607 DebugInfo = CGM.getDebugInfo();
608
Mike Stumpa20c59e2009-03-13 23:34:28 +0000609 // Arrange for local static and local extern declarations to appear
610 // to be local to this function as well, as they are directly referenced
611 // in a block.
612 for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
613 i != ldm.end();
614 ++i) {
615 const VarDecl *VD = dyn_cast<VarDecl>(i->first);
616
Daniel Dunbar644c15e2009-04-14 02:25:56 +0000617 if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage())
Mike Stumpa20c59e2009-03-13 23:34:28 +0000618 LocalDeclMap[VD] = i->second;
619 }
620
Eli Friedman584abaf2009-03-28 03:24:54 +0000621 // FIXME: We need to rearrange the code for copy/dispose so we have this
622 // sooner, so we can calculate offsets correctly.
623 if (!BlockHasCopyDispose)
624 BlockOffset = CGM.getTargetData()
625 .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8;
626 else
627 BlockOffset = CGM.getTargetData()
628 .getTypeStoreSizeInBits(CGM.getGenericExtendedBlockLiteralType()) / 8;
629 BlockAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
630
Fariborz Jahanian3413b1a2009-04-11 17:55:15 +0000631 const FunctionType *BlockFunctionType = BExpr->getFunctionType();
632 QualType ResultType;
633 bool IsVariadic;
Fariborz Jahaniande6a6492009-04-11 18:54:57 +0000634 if (const FunctionProtoType *FTy =
635 dyn_cast<FunctionProtoType>(BlockFunctionType)) {
Fariborz Jahanian3413b1a2009-04-11 17:55:15 +0000636 ResultType = FTy->getResultType();
637 IsVariadic = FTy->isVariadic();
638 }
639 else {
640 // K&R style block.
641 ResultType = BlockFunctionType->getResultType();
642 IsVariadic = false;
643 }
Mike Stumpc4ae9632009-02-13 15:32:32 +0000644
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000645 FunctionArgList Args;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000646
Chris Lattner8130e7f2009-02-28 19:01:03 +0000647 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000648
649 // FIXME: This leaks
Mike Stumpc4ae9632009-02-13 15:32:32 +0000650 ImplicitParamDecl *SelfDecl =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000651 ImplicitParamDecl::Create(getContext(), 0,
652 SourceLocation(), 0,
653 getContext().getPointerType(getContext().VoidTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000654
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000655 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stumpfca5da02009-02-21 20:00:35 +0000656 BlockStructDecl = SelfDecl;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000657
Steve Naroff494cb0f2009-03-13 16:56:44 +0000658 for (BlockDecl::param_const_iterator i = BD->param_begin(),
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000659 e = BD->param_end(); i != e; ++i)
Mike Stump459207f2009-02-17 23:25:52 +0000660 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000661
662 const CGFunctionInfo &FI =
Fariborz Jahanian3413b1a2009-04-11 17:55:15 +0000663 CGM.getTypes().getFunctionInfo(ResultType, Args);
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000664
Mike Stump084ba462009-02-14 22:16:35 +0000665 std::string Name = std::string("__") + Info.Name + "_block_invoke_";
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000666 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian3413b1a2009-04-11 17:55:15 +0000667 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000668
669 llvm::Function *Fn =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000670 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
671 Name,
672 &CGM.getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000673
Daniel Dunbar5b90ae12009-04-17 00:48:04 +0000674 CGM.SetInternalFunctionAttributes(BD, Fn, FI);
675
Chris Lattnerab3d3732009-04-23 07:18:56 +0000676 StartFunction(BD, ResultType, Fn, Args,
Chris Lattner8130e7f2009-02-28 19:01:03 +0000677 BExpr->getBody()->getLocEnd());
Chris Lattnerab3d3732009-04-23 07:18:56 +0000678 CurFuncDecl = OuterFuncDecl;
Chris Lattnerf6279ae2009-04-23 05:30:27 +0000679 CurCodeDecl = BD;
Chris Lattner8130e7f2009-02-28 19:01:03 +0000680 EmitStmt(BExpr->getBody());
681 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000682
Mike Stumpf1711822009-02-25 23:33:13 +0000683 // The runtime needs a minimum alignment of a void *.
684 uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
685 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign);
686
Mike Stumpfca5da02009-02-21 20:00:35 +0000687 Size = BlockOffset;
Mike Stumpf1711822009-02-25 23:33:13 +0000688 Align = BlockAlign;
689 subBlockDeclRefDecls = BlockDeclRefDecls;
Mike Stump1bdbf632009-03-05 08:32:30 +0000690 subBlockHasCopyDispose |= BlockHasCopyDispose;
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000691 return Fn;
692}
Mike Stump2b6933f2009-02-28 09:07:16 +0000693
Mike Stump1fa52fe2009-03-07 02:35:30 +0000694uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
Mike Stump2b6933f2009-02-28 09:07:16 +0000695 const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
696
697 uint64_t Size = getContext().getTypeSize(D->getType()) / 8;
698 uint64_t Align = getContext().getDeclAlignInBytes(D);
699
700 if (BDRE->isByRef()) {
701 Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8;
702 Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
703 }
704
705 assert ((Align > 0) && "alignment must be 1 byte or more");
706
707 uint64_t OldOffset = BlockOffset;
708
709 // Ensure proper alignment, even if it means we have to have a gap
710 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align);
711 BlockAlign = std::max(Align, BlockAlign);
Mike Stump1bdbf632009-03-05 08:32:30 +0000712
Mike Stump2b6933f2009-02-28 09:07:16 +0000713 uint64_t Pad = BlockOffset - OldOffset;
714 if (Pad) {
715 llvm::ArrayType::get(llvm::Type::Int8Ty, Pad);
716 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
717 llvm::APInt(32, Pad),
718 ArrayType::Normal, 0);
719 ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
720 0, QualType(PadTy), VarDecl::None,
721 SourceLocation());
722 Expr *E;
723 E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
724 SourceLocation(), false, false);
725 BlockDeclRefDecls.push_back(E);
726 }
727 BlockDeclRefDecls.push_back(BDRE);
728
729 BlockOffset += Size;
730 return BlockOffset-Size;
731}
Mike Stumpad9605d2009-03-04 03:23:46 +0000732
Mike Stump1fa52fe2009-03-07 02:35:30 +0000733llvm::Constant *BlockFunction::
734GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
Mike Stump2f9a4262009-04-10 18:52:28 +0000735 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpecd79422009-03-06 01:33:24 +0000736 QualType R = getContext().VoidTy;
737
738 FunctionArgList Args;
739 // FIXME: This leaks
Mike Stump1fa52fe2009-03-07 02:35:30 +0000740 ImplicitParamDecl *Dst =
741 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
742 getContext().getPointerType(getContext().VoidTy));
743 Args.push_back(std::make_pair(Dst, Dst->getType()));
Mike Stumpecd79422009-03-06 01:33:24 +0000744 ImplicitParamDecl *Src =
745 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
746 getContext().getPointerType(getContext().VoidTy));
Mike Stumpecd79422009-03-06 01:33:24 +0000747 Args.push_back(std::make_pair(Src, Src->getType()));
748
749 const CGFunctionInfo &FI =
750 CGM.getTypes().getFunctionInfo(R, Args);
751
752 std::string Name = std::string("__copy_helper_block_");
753 CodeGenTypes &Types = CGM.getTypes();
754 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
755
756 llvm::Function *Fn =
757 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
758 Name,
759 &CGM.getModule());
760
761 IdentifierInfo *II
762 = &CGM.getContext().Idents.get("__copy_helper_block_");
763
764 FunctionDecl *FD = FunctionDecl::Create(getContext(),
765 getContext().getTranslationUnitDecl(),
766 SourceLocation(), II, R,
767 FunctionDecl::Static, false,
768 true);
769 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1fa52fe2009-03-07 02:35:30 +0000770
771 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
772 llvm::Type *PtrPtrT;
Mike Stump1fa52fe2009-03-07 02:35:30 +0000773
Mike Stump2f9a4262009-04-10 18:52:28 +0000774 if (NoteForHelperp) {
775 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump1fa52fe2009-03-07 02:35:30 +0000776
Mike Stump2f9a4262009-04-10 18:52:28 +0000777 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
778 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
779 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump1fa52fe2009-03-07 02:35:30 +0000780
Mike Stump2f9a4262009-04-10 18:52:28 +0000781 llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
Mike Stumpb71fa972009-04-15 22:11:36 +0000782 llvm::Type *PtrPtrT;
783 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
784 DstObj = Builder.CreateBitCast(DstObj, PtrPtrT);
785 DstObj = Builder.CreateLoad(DstObj);
Mike Stump1fa52fe2009-03-07 02:35:30 +0000786
Mike Stump2f9a4262009-04-10 18:52:28 +0000787 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
788 int flag = NoteForHelper[i].flag;
789 int index = NoteForHelper[i].index;
Mike Stump1fa52fe2009-03-07 02:35:30 +0000790
Mike Stump2f9a4262009-04-10 18:52:28 +0000791 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
792 || NoteForHelper[i].RequiresCopying) {
793 llvm::Value *Srcv = SrcObj;
794 Srcv = Builder.CreateStructGEP(Srcv, index);
795 Srcv = Builder.CreateBitCast(Srcv,
796 llvm::PointerType::get(PtrToInt8Ty, 0));
797 Srcv = Builder.CreateLoad(Srcv);
798
799 llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
800 Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
801
802 llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag);
803 llvm::Value *F = getBlockObjectAssign();
804 Builder.CreateCall3(F, Dstv, Srcv, N);
805 }
Mike Stump1fa52fe2009-03-07 02:35:30 +0000806 }
807 }
808
Mike Stumpecd79422009-03-06 01:33:24 +0000809 CGF.FinishFunction();
810
811 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpad9605d2009-03-04 03:23:46 +0000812}
813
Mike Stump9d8c1262009-03-06 18:42:23 +0000814llvm::Constant *BlockFunction::
Mike Stump1fa52fe2009-03-07 02:35:30 +0000815GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
816 const llvm::StructType* T,
Mike Stump2f9a4262009-04-10 18:52:28 +0000817 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpecd79422009-03-06 01:33:24 +0000818 QualType R = getContext().VoidTy;
819
820 FunctionArgList Args;
821 // FIXME: This leaks
822 ImplicitParamDecl *Src =
823 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
824 getContext().getPointerType(getContext().VoidTy));
825
826 Args.push_back(std::make_pair(Src, Src->getType()));
827
828 const CGFunctionInfo &FI =
829 CGM.getTypes().getFunctionInfo(R, Args);
830
831 std::string Name = std::string("__destroy_helper_block_");
832 CodeGenTypes &Types = CGM.getTypes();
833 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
834
835 llvm::Function *Fn =
836 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
837 Name,
838 &CGM.getModule());
839
840 IdentifierInfo *II
841 = &CGM.getContext().Idents.get("__destroy_helper_block_");
842
843 FunctionDecl *FD = FunctionDecl::Create(getContext(),
844 getContext().getTranslationUnitDecl(),
845 SourceLocation(), II, R,
846 FunctionDecl::Static, false,
847 true);
848 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump05a6d4e2009-03-07 02:53:18 +0000849
Mike Stump2f9a4262009-04-10 18:52:28 +0000850 if (NoteForHelperp) {
851 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump05a6d4e2009-03-07 02:53:18 +0000852
Mike Stump2f9a4262009-04-10 18:52:28 +0000853 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
854 llvm::Type *PtrPtrT;
855 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
856 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
857 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump05a6d4e2009-03-07 02:53:18 +0000858
Mike Stump2f9a4262009-04-10 18:52:28 +0000859 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
860 int flag = NoteForHelper[i].flag;
861 int index = NoteForHelper[i].index;
Mike Stump05a6d4e2009-03-07 02:53:18 +0000862
Mike Stump2f9a4262009-04-10 18:52:28 +0000863 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
864 || NoteForHelper[i].RequiresCopying) {
865 llvm::Value *Srcv = SrcObj;
866 Srcv = Builder.CreateStructGEP(Srcv, index);
867 Srcv = Builder.CreateBitCast(Srcv,
868 llvm::PointerType::get(PtrToInt8Ty, 0));
869 Srcv = Builder.CreateLoad(Srcv);
870
871 BuildBlockRelease(Srcv, flag);
872 }
Mike Stump05a6d4e2009-03-07 02:53:18 +0000873 }
874 }
875
Mike Stumpecd79422009-03-06 01:33:24 +0000876 CGF.FinishFunction();
877
878 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
879}
880
Mike Stump1fa52fe2009-03-07 02:35:30 +0000881llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
Mike Stump2f9a4262009-04-10 18:52:28 +0000882 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump1fa52fe2009-03-07 02:35:30 +0000883 return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
884 T, NoteForHelper);
Mike Stumpecd79422009-03-06 01:33:24 +0000885}
886
Mike Stump1fa52fe2009-03-07 02:35:30 +0000887llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
Mike Stump2f9a4262009-04-10 18:52:28 +0000888 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stump1fa52fe2009-03-07 02:35:30 +0000889 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
Mike Stump2f9a4262009-04-10 18:52:28 +0000890 T, NoteForHelperp);
Mike Stumpad9605d2009-03-04 03:23:46 +0000891}
Mike Stump60294662009-03-05 01:23:13 +0000892
Mike Stump11973b62009-03-06 06:12:24 +0000893llvm::Constant *BlockFunction::
894GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
Mike Stumpf4b62342009-03-06 02:29:21 +0000895 QualType R = getContext().VoidTy;
896
897 FunctionArgList Args;
898 // FIXME: This leaks
Mike Stump11973b62009-03-06 06:12:24 +0000899 ImplicitParamDecl *Dst =
900 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
901 getContext().getPointerType(getContext().VoidTy));
902 Args.push_back(std::make_pair(Dst, Dst->getType()));
903
904 // FIXME: This leaks
Mike Stumpf4b62342009-03-06 02:29:21 +0000905 ImplicitParamDecl *Src =
906 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
907 getContext().getPointerType(getContext().VoidTy));
Mike Stumpf4b62342009-03-06 02:29:21 +0000908 Args.push_back(std::make_pair(Src, Src->getType()));
909
910 const CGFunctionInfo &FI =
911 CGM.getTypes().getFunctionInfo(R, Args);
912
913 std::string Name = std::string("__Block_byref_id_object_copy_");
914 CodeGenTypes &Types = CGM.getTypes();
915 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
916
917 llvm::Function *Fn =
918 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
919 Name,
920 &CGM.getModule());
921
922 IdentifierInfo *II
923 = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
924
925 FunctionDecl *FD = FunctionDecl::Create(getContext(),
926 getContext().getTranslationUnitDecl(),
927 SourceLocation(), II, R,
928 FunctionDecl::Static, false,
929 true);
930 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump11973b62009-03-06 06:12:24 +0000931
932 // dst->x
933 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
Mike Stumpb71fa972009-04-15 22:11:36 +0000934 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
935 V = Builder.CreateLoad(V);
Mike Stump11973b62009-03-06 06:12:24 +0000936 V = Builder.CreateStructGEP(V, 6, "x");
937 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
938
939 // src->x
940 V = CGF.GetAddrOfLocalVar(Src);
941 V = Builder.CreateLoad(V);
942 V = Builder.CreateBitCast(V, T);
943 V = Builder.CreateStructGEP(V, 6, "x");
944 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
945 llvm::Value *SrcObj = Builder.CreateLoad(V);
946
947 flag |= BLOCK_BYREF_CALLER;
948
949 llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag);
950 llvm::Value *F = getBlockObjectAssign();
951 Builder.CreateCall3(F, DstObj, SrcObj, N);
952
Mike Stumpf4b62342009-03-06 02:29:21 +0000953 CGF.FinishFunction();
954
955 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
956}
957
Mike Stump4a0e5132009-03-06 04:53:30 +0000958llvm::Constant *
959BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
960 int flag) {
Mike Stumpf4b62342009-03-06 02:29:21 +0000961 QualType R = getContext().VoidTy;
962
963 FunctionArgList Args;
964 // FIXME: This leaks
965 ImplicitParamDecl *Src =
966 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
967 getContext().getPointerType(getContext().VoidTy));
968
969 Args.push_back(std::make_pair(Src, Src->getType()));
970
971 const CGFunctionInfo &FI =
972 CGM.getTypes().getFunctionInfo(R, Args);
973
974 std::string Name = std::string("__Block_byref_id_object_dispose_");
975 CodeGenTypes &Types = CGM.getTypes();
976 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
977
978 llvm::Function *Fn =
979 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
980 Name,
981 &CGM.getModule());
982
983 IdentifierInfo *II
984 = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
985
986 FunctionDecl *FD = FunctionDecl::Create(getContext(),
987 getContext().getTranslationUnitDecl(),
988 SourceLocation(), II, R,
989 FunctionDecl::Static, false,
990 true);
991 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump4a0e5132009-03-06 04:53:30 +0000992
993 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
Mike Stumpb71fa972009-04-15 22:11:36 +0000994 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
995 V = Builder.CreateLoad(V);
Mike Stump4a0e5132009-03-06 04:53:30 +0000996 V = Builder.CreateStructGEP(V, 6, "x");
Mike Stumpb71fa972009-04-15 22:11:36 +0000997 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
998 V = Builder.CreateLoad(V);
Mike Stump4a0e5132009-03-06 04:53:30 +0000999
Mike Stump4a0e5132009-03-06 04:53:30 +00001000 flag |= BLOCK_BYREF_CALLER;
1001 BuildBlockRelease(V, flag);
Mike Stumpf4b62342009-03-06 02:29:21 +00001002 CGF.FinishFunction();
1003
1004 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
1005}
1006
Mike Stump11973b62009-03-06 06:12:24 +00001007llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
1008 int flag) {
1009 return CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag);
Mike Stumpf4b62342009-03-06 02:29:21 +00001010}
1011
Mike Stump4a0e5132009-03-06 04:53:30 +00001012llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
1013 int flag) {
1014 return CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag);
Mike Stumpf4b62342009-03-06 02:29:21 +00001015}
1016
Mike Stump60294662009-03-05 01:23:13 +00001017llvm::Value *BlockFunction::getBlockObjectDispose() {
1018 if (CGM.BlockObjectDispose == 0) {
1019 const llvm::FunctionType *FTy;
1020 std::vector<const llvm::Type*> ArgTys;
1021 const llvm::Type *ResultType = llvm::Type::VoidTy;
1022 ArgTys.push_back(PtrToInt8Ty);
1023 ArgTys.push_back(llvm::Type::Int32Ty);
1024 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stump1bdbf632009-03-05 08:32:30 +00001025 CGM.BlockObjectDispose
Mike Stump60294662009-03-05 01:23:13 +00001026 = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
1027 }
1028 return CGM.BlockObjectDispose;
1029}
1030
Mike Stump11973b62009-03-06 06:12:24 +00001031llvm::Value *BlockFunction::getBlockObjectAssign() {
1032 if (CGM.BlockObjectAssign == 0) {
1033 const llvm::FunctionType *FTy;
1034 std::vector<const llvm::Type*> ArgTys;
1035 const llvm::Type *ResultType = llvm::Type::VoidTy;
1036 ArgTys.push_back(PtrToInt8Ty);
1037 ArgTys.push_back(PtrToInt8Ty);
1038 ArgTys.push_back(llvm::Type::Int32Ty);
1039 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
1040 CGM.BlockObjectAssign
1041 = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
1042 }
1043 return CGM.BlockObjectAssign;
1044}
1045
Mike Stump4a0e5132009-03-06 04:53:30 +00001046void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Mike Stump60294662009-03-05 01:23:13 +00001047 llvm::Value *F = getBlockObjectDispose();
Mike Stump4a0e5132009-03-06 04:53:30 +00001048 llvm::Value *N;
Mike Stump60294662009-03-05 01:23:13 +00001049 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Mike Stump4a0e5132009-03-06 04:53:30 +00001050 N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag);
Mike Stump60294662009-03-05 01:23:13 +00001051 Builder.CreateCall2(F, V, N);
1052}
Mike Stump1bdbf632009-03-05 08:32:30 +00001053
1054ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stump1fa52fe2009-03-07 02:35:30 +00001055
1056BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1057 CGBuilderTy &B)
1058 : CGM(cgm), CGF(cgf), Builder(B) {
1059 PtrToInt8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1060
1061 BlockHasCopyDispose = false;
1062}