blob: 7b4eb8f582dc93367a1686c1ab8037f5a5c645f0 [file] [log] [blame]
Anders Carlssonacfde802009-02-12 00:39:25 +00001//===--- CGBlocks.cpp - Emit LLVM Code for declarations -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit blocks.
11//
12//===----------------------------------------------------------------------===//
13
Mike Stumpb1a6e682009-09-30 02:43:10 +000014#include "CGDebugInfo.h"
Anders Carlssonacfde802009-02-12 00:39:25 +000015#include "CodeGenFunction.h"
Fariborz Jahanian263c4de2010-02-10 23:34:57 +000016#include "CGObjCRuntime.h"
Anders Carlssonacfde802009-02-12 00:39:25 +000017#include "CodeGenModule.h"
Mike Stump6cc88f72009-03-20 21:53:12 +000018#include "clang/AST/DeclObjC.h"
Anders Carlssonacfde802009-02-12 00:39:25 +000019#include "llvm/Module.h"
Benjamin Kramer6876fe62010-03-31 15:04:05 +000020#include "llvm/ADT/SmallSet.h"
Anders Carlssond5cab542009-02-12 17:55:02 +000021#include "llvm/Target/TargetData.h"
Anders Carlssonacfde802009-02-12 00:39:25 +000022#include <algorithm>
Torok Edwinf42e4a62009-08-24 13:25:12 +000023
Anders Carlssonacfde802009-02-12 00:39:25 +000024using namespace clang;
25using namespace CodeGen;
26
Mike Stumpcf62d392009-03-06 18:42:23 +000027llvm::Constant *CodeGenFunction::
Blaine Garst2a7eb282010-02-23 21:51:17 +000028BuildDescriptorBlockDecl(const BlockExpr *BE, bool BlockHasCopyDispose, CharUnits Size,
Mike Stumpa803b0e2009-03-25 17:58:24 +000029 const llvm::StructType* Ty,
Mike Stump08920992009-03-07 02:35:30 +000030 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump56129b12009-02-13 16:55:51 +000031 const llvm::Type *UnsignedLongTy
32 = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpe5fee252009-02-13 16:19:19 +000033 llvm::Constant *C;
34 std::vector<llvm::Constant*> Elts;
35
36 // reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +000037 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpe5fee252009-02-13 16:19:19 +000038 Elts.push_back(C);
39
40 // Size
Mike Stumpd6840002009-02-21 20:07:44 +000041 // FIXME: What is the right way to say this doesn't fit? We should give
42 // a user diagnostic in that case. Better fix would be to change the
43 // API to size_t.
Ken Dyck199c3d62010-01-11 17:06:35 +000044 C = llvm::ConstantInt::get(UnsignedLongTy, Size.getQuantity());
Mike Stumpe5fee252009-02-13 16:19:19 +000045 Elts.push_back(C);
46
Blaine Garst2a7eb282010-02-23 21:51:17 +000047 // optional copy/dispose helpers
Mike Stumpe5fee252009-02-13 16:19:19 +000048 if (BlockHasCopyDispose) {
49 // copy_func_helper_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000050 Elts.push_back(BuildCopyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000051
52 // destroy_func_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000053 Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000054 }
55
Blaine Garst2a7eb282010-02-23 21:51:17 +000056 // Signature. non-optional ObjC-style method descriptor @encode sequence
57 std::string BlockTypeEncoding;
58 CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
59
60 Elts.push_back(llvm::ConstantExpr::getBitCast(
61 CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty));
62
63 // Layout.
64 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
65 Elts.push_back(C);
66
Nick Lewycky0d36dd22009-09-19 20:00:52 +000067 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stumpe5fee252009-02-13 16:19:19 +000068
Owen Anderson1c431b32009-07-08 19:05:04 +000069 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Mike Stumpe5fee252009-02-13 16:19:19 +000070 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +000071 C, "__block_descriptor_tmp");
Mike Stumpe5fee252009-02-13 16:19:19 +000072 return C;
73}
74
Mike Stump2a998142009-03-04 18:17:45 +000075llvm::Constant *BlockModule::getNSConcreteGlobalBlock() {
Chris Lattnere2f79b62009-05-13 02:50:56 +000076 if (NSConcreteGlobalBlock == 0)
Mike Stump1eb44332009-09-09 15:08:12 +000077 NSConcreteGlobalBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
Chris Lattnere2f79b62009-05-13 02:50:56 +000078 "_NSConcreteGlobalBlock");
Mike Stumpf99f1d02009-02-13 17:23:42 +000079 return NSConcreteGlobalBlock;
80}
81
Mike Stump2a998142009-03-04 18:17:45 +000082llvm::Constant *BlockModule::getNSConcreteStackBlock() {
Chris Lattnere2f79b62009-05-13 02:50:56 +000083 if (NSConcreteStackBlock == 0)
Mike Stump1eb44332009-09-09 15:08:12 +000084 NSConcreteStackBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
Chris Lattnere2f79b62009-05-13 02:50:56 +000085 "_NSConcreteStackBlock");
Mike Stump59c5b112009-02-13 19:29:27 +000086 return NSConcreteStackBlock;
87}
88
Mike Stump38e16272009-10-21 22:01:24 +000089static void CollectBlockDeclRefInfo(
90 const Stmt *S, CodeGenFunction::BlockInfo &Info,
91 llvm::SmallSet<const DeclContext *, 16> &InnerContexts) {
Anders Carlsson4de9fce2009-03-01 01:09:12 +000092 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
93 I != E; ++I)
Daniel Dunbar82573ee2009-03-02 07:00:57 +000094 if (*I)
Mike Stump38e16272009-10-21 22:01:24 +000095 CollectBlockDeclRefInfo(*I, Info, InnerContexts);
Mike Stump00470a12009-03-05 08:32:30 +000096
Mike Stumpea26cb52009-10-21 03:49:08 +000097 // We want to ensure we walk down into block literals so we can find
98 // all nested BlockDeclRefExprs.
Mike Stump38e16272009-10-21 22:01:24 +000099 if (const BlockExpr *BE = dyn_cast<BlockExpr>(S)) {
100 InnerContexts.insert(cast<DeclContext>(BE->getBlockDecl()));
101 CollectBlockDeclRefInfo(BE->getBody(), Info, InnerContexts);
102 }
Mike Stumpea26cb52009-10-21 03:49:08 +0000103
Mike Stump38e16272009-10-21 22:01:24 +0000104 if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) {
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000105 // FIXME: Handle enums.
Mike Stump38e16272009-10-21 22:01:24 +0000106 if (isa<FunctionDecl>(BDRE->getDecl()))
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000107 return;
Mike Stump00470a12009-03-05 08:32:30 +0000108
Mike Stump38e16272009-10-21 22:01:24 +0000109 // Only Decls that escape are added.
110 if (!InnerContexts.count(BDRE->getDecl()->getDeclContext()))
111 Info.DeclRefs.push_back(BDRE);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000112 }
John McCallea1471e2010-05-20 01:18:31 +0000113
114 if (isa<CXXThisExpr>(S))
115 Info.CXXThisRef = cast<CXXThisExpr>(S);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000116}
117
Mike Stump58a85142009-03-04 22:48:06 +0000118/// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be
119/// declared as a global variable instead of on the stack.
Chris Lattnere2f79b62009-05-13 02:50:56 +0000120static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info) {
Mike Stumpea26cb52009-10-21 03:49:08 +0000121 return Info.DeclRefs.empty();
122}
123
124/// AllocateAllBlockDeclRefs - Preallocate all nested BlockDeclRefExprs to
125/// ensure we can generate the debug information for the parameter for the block
126/// invoke function.
127static void AllocateAllBlockDeclRefs(const CodeGenFunction::BlockInfo &Info,
128 CodeGenFunction *CGF) {
John McCallea1471e2010-05-20 01:18:31 +0000129 if (Info.CXXThisRef)
130 CGF->AllocateBlockCXXThisPointer(Info.CXXThisRef);
Mike Stumpea26cb52009-10-21 03:49:08 +0000131
132 for (size_t i = 0; i < Info.DeclRefs.size(); ++i)
133 CGF->AllocateBlockDecl(Info.DeclRefs[i]);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000134}
135
Mike Stump58a85142009-03-04 22:48:06 +0000136// FIXME: Push most into CGM, passing down a few bits, like current function
137// name.
Mike Stump8a2b4b12009-02-25 23:33:13 +0000138llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Mike Stumpe5fee252009-02-13 16:19:19 +0000139
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000140 std::string Name = CurFn->getName();
141 CodeGenFunction::BlockInfo Info(0, Name.c_str());
Mike Stump38e16272009-10-21 22:01:24 +0000142 llvm::SmallSet<const DeclContext *, 16> InnerContexts;
143 InnerContexts.insert(BE->getBlockDecl());
144 CollectBlockDeclRefInfo(BE->getBody(), Info, InnerContexts);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000145
146 // Check if the block can be global.
Mike Stump58a85142009-03-04 22:48:06 +0000147 // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like
148 // to just have one code path. We should move this function into CGM and pass
149 // CGF, then we can just check to see if CGF is 0.
Mike Stumpdab514f2009-03-04 03:23:46 +0000150 if (0 && CanBlockBeGlobal(Info))
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000151 return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
Mike Stump00470a12009-03-05 08:32:30 +0000152
David Chisnall5e530af2009-11-17 19:33:30 +0000153 size_t BlockFields = 5;
154
David Chisnall5e530af2009-11-17 19:33:30 +0000155 std::vector<llvm::Constant*> Elts(BlockFields);
156
Mike Stumpe5fee252009-02-13 16:19:19 +0000157 llvm::Constant *C;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000158 llvm::Value *V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000159
Mike Stumpe5fee252009-02-13 16:19:19 +0000160 {
161 // C = BuildBlockStructInitlist();
Blaine Garsta36e2232010-03-05 01:29:59 +0000162 unsigned int flags = BLOCK_HAS_SIGNATURE;
David Chisnall5e530af2009-11-17 19:33:30 +0000163
Mike Stump00470a12009-03-05 08:32:30 +0000164 // We run this first so that we set BlockHasCopyDispose from the entire
165 // block literal.
166 // __invoke
Ken Dyck199c3d62010-01-11 17:06:35 +0000167 CharUnits subBlockSize;
Ken Dyck30a8a272010-01-26 19:13:33 +0000168 CharUnits subBlockAlign;
John McCallea1471e2010-05-20 01:18:31 +0000169 llvm::SmallVector<const Expr *, 8> subBlockLayout;
Mike Stumpa803b0e2009-03-25 17:58:24 +0000170 bool subBlockHasCopyDispose = false;
Mike Stump00470a12009-03-05 08:32:30 +0000171 llvm::Function *Fn
Mike Stumpbb304192009-09-24 22:31:14 +0000172 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl,
173 LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000174 subBlockSize,
Mike Stump00470a12009-03-05 08:32:30 +0000175 subBlockAlign,
John McCallea1471e2010-05-20 01:18:31 +0000176 subBlockLayout,
Mike Stumpa803b0e2009-03-25 17:58:24 +0000177 subBlockHasCopyDispose);
178 BlockHasCopyDispose |= subBlockHasCopyDispose;
Mike Stump00470a12009-03-05 08:32:30 +0000179 Elts[3] = Fn;
180
Mike Stumpf5408fe2009-05-16 07:57:57 +0000181 // FIXME: Don't use BlockHasCopyDispose, it is set more often then
182 // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); }
Mike Stumpa803b0e2009-03-25 17:58:24 +0000183 if (subBlockHasCopyDispose)
Mike Stumpe5fee252009-02-13 16:19:19 +0000184 flags |= BLOCK_HAS_COPY_DISPOSE;
185
Mike Stump7d6dc4f2009-02-13 20:17:16 +0000186 // __isa
Mike Stump59c5b112009-02-13 19:29:27 +0000187 C = CGM.getNSConcreteStackBlock();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000188 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
Mike Stump00470a12009-03-05 08:32:30 +0000189 Elts[0] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000190
191 // __flags
Blaine Garsta36e2232010-03-05 01:29:59 +0000192 {
193 QualType BPT = BE->getType();
194 const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>();
195 QualType ResultType = ftype->getResultType();
196
197 CallArgList Args;
198 CodeGenTypes &Types = CGM.getTypes();
199 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000200 FunctionType::ExtInfo());
Blaine Garsta36e2232010-03-05 01:29:59 +0000201 if (CGM.ReturnTypeUsesSret(FnInfo))
202 flags |= BLOCK_USE_STRET;
203 }
Mike Stumpe5fee252009-02-13 16:19:19 +0000204 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
205 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000206 C = llvm::ConstantInt::get(IntTy, flags);
Mike Stump00470a12009-03-05 08:32:30 +0000207 Elts[1] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000208
209 // __reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000210 C = llvm::ConstantInt::get(IntTy, 0);
Mike Stump00470a12009-03-05 08:32:30 +0000211 Elts[2] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000212
John McCallea1471e2010-05-20 01:18:31 +0000213 if (subBlockLayout.empty()) {
Mike Stumpcf62d392009-03-06 18:42:23 +0000214 // __descriptor
Blaine Garst2a7eb282010-02-23 21:51:17 +0000215 Elts[4] = BuildDescriptorBlockDecl(BE, subBlockHasCopyDispose, subBlockSize,
Mike Stumpea26cb52009-10-21 03:49:08 +0000216 0, 0);
Mike Stumpcf62d392009-03-06 18:42:23 +0000217
Mike Stump5570cfe2009-03-01 20:07:53 +0000218 // Optimize to being a global block.
219 Elts[0] = CGM.getNSConcreteGlobalBlock();
Blaine Garsta36e2232010-03-05 01:29:59 +0000220
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000221 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
Mike Stump5570cfe2009-03-01 20:07:53 +0000222
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000223 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000224
Owen Anderson1c431b32009-07-08 19:05:04 +0000225 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000226 llvm::GlobalValue::InternalLinkage, C,
227 "__block_holder_tmp_" +
228 llvm::Twine(CGM.getGlobalUniqueCount()));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000229 QualType BPT = BE->getType();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000230 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000231 return C;
232 }
Mike Stump00470a12009-03-05 08:32:30 +0000233
John McCallea1471e2010-05-20 01:18:31 +0000234 std::vector<const llvm::Type *> Types(BlockFields+subBlockLayout.size());
Mike Stump08920992009-03-07 02:35:30 +0000235 for (int i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000236 Types[i] = Elts[i]->getType();
Mike Stump08920992009-03-07 02:35:30 +0000237 Types[4] = PtrToInt8Ty;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000238
John McCallea1471e2010-05-20 01:18:31 +0000239 for (unsigned i = 0, n = subBlockLayout.size(); i != n; ++i) {
240 const Expr *E = subBlockLayout[i];
Mike Stumpa99038c2009-02-28 09:07:16 +0000241 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
242 QualType Ty = E->getType();
Mike Stumpdab514f2009-03-04 03:23:46 +0000243 if (BDRE && BDRE->isByRef()) {
David Chisnall5e530af2009-11-17 19:33:30 +0000244 Types[i+BlockFields] = llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000245 } else
David Chisnall5e530af2009-11-17 19:33:30 +0000246 Types[i+BlockFields] = ConvertType(Ty);
Mike Stumpa99038c2009-02-28 09:07:16 +0000247 }
Mike Stump8a2b4b12009-02-25 23:33:13 +0000248
Owen Anderson47a434f2009-08-05 23:18:46 +0000249 llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000250
251 llvm::AllocaInst *A = CreateTempAlloca(Ty);
Ken Dyck30a8a272010-01-26 19:13:33 +0000252 A->setAlignment(subBlockAlign.getQuantity());
Mike Stump8a2b4b12009-02-25 23:33:13 +0000253 V = A;
254
John McCallea1471e2010-05-20 01:18:31 +0000255 // Build layout / cleanup information for all the data entries in the
256 // layout, and write the enclosing fields into the type.
257 std::vector<HelperInfo> NoteForHelper(subBlockLayout.size());
258 unsigned NumHelpers = 0;
Mike Stump08920992009-03-07 02:35:30 +0000259
260 for (unsigned i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000261 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
Mike Stump00470a12009-03-05 08:32:30 +0000262
John McCallea1471e2010-05-20 01:18:31 +0000263 for (unsigned i=0; i < subBlockLayout.size(); ++i) {
264 const Expr *E = subBlockLayout[i];
Mike Stump8a2b4b12009-02-25 23:33:13 +0000265
John McCallea1471e2010-05-20 01:18:31 +0000266 // Skip padding.
267 if (isa<DeclRefExpr>(E)) continue;
Mike Stumpa99038c2009-02-28 09:07:16 +0000268
John McCallea1471e2010-05-20 01:18:31 +0000269 llvm::Value* Addr = Builder.CreateStructGEP(V, i+BlockFields, "tmp");
270 HelperInfo &Note = NoteForHelper[NumHelpers++];
Mike Stumpa99038c2009-02-28 09:07:16 +0000271
John McCallea1471e2010-05-20 01:18:31 +0000272 Note.index = i+5;
Mike Stump08920992009-03-07 02:35:30 +0000273
John McCallea1471e2010-05-20 01:18:31 +0000274 if (isa<CXXThisExpr>(E)) {
275 Note.RequiresCopying = false;
276 Note.flag = BLOCK_FIELD_IS_OBJECT;
Mike Stumpa99038c2009-02-28 09:07:16 +0000277
John McCallea1471e2010-05-20 01:18:31 +0000278 Builder.CreateStore(LoadCXXThis(), Addr);
279 continue;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000280 }
John McCallea1471e2010-05-20 01:18:31 +0000281
282 const BlockDeclRefExpr *BDRE = cast<BlockDeclRefExpr>(E);
283 const ValueDecl *VD = BDRE->getDecl();
284 QualType T = VD->getType();
285
286 Note.RequiresCopying = BlockRequiresCopying(T);
287
288 if (BDRE->isByRef()) {
289 Note.flag = BLOCK_FIELD_IS_BYREF;
290 if (T.isObjCGCWeak())
291 Note.flag |= BLOCK_FIELD_IS_WEAK;
292 } else if (T->isBlockPointerType()) {
293 Note.flag = BLOCK_FIELD_IS_BLOCK;
294 } else {
295 Note.flag = BLOCK_FIELD_IS_OBJECT;
296 }
297
298 if (LocalDeclMap[VD]) {
299 if (BDRE->isByRef()) {
300 llvm::Value *Loc = LocalDeclMap[VD];
301 Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
302 Loc = Builder.CreateLoad(Loc);
303 Builder.CreateStore(Loc, Addr);
304 continue;
305 } else {
306 E = new (getContext()) DeclRefExpr(const_cast<ValueDecl*>(VD),
307 VD->getType(),
308 SourceLocation());
309 }
310 }
311
312 if (BDRE->isByRef()) {
313 E = new (getContext())
314 UnaryOperator(const_cast<Expr*>(E), UnaryOperator::AddrOf,
315 getContext().getPointerType(E->getType()),
316 SourceLocation());
317 }
318
319 RValue r = EmitAnyExpr(E, Addr, false);
320 if (r.isScalar()) {
321 llvm::Value *Loc = r.getScalarVal();
322 const llvm::Type *Ty = Types[i+BlockFields];
323 if (BDRE->isByRef()) {
324 // E is now the address of the value field, instead, we want the
325 // address of the actual ByRef struct. We optimize this slightly
326 // compared to gcc by not grabbing the forwarding slot as this must
327 // be done during Block_copy for us, and we can postpone the work
328 // until then.
329 CharUnits offset = BlockDecls[BDRE->getDecl()];
330
331 llvm::Value *BlockLiteral = LoadBlockStruct();
332
333 Loc = Builder.CreateGEP(BlockLiteral,
334 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
335 offset.getQuantity()),
336 "block.literal");
337 Ty = llvm::PointerType::get(Ty, 0);
338 Loc = Builder.CreateBitCast(Loc, Ty);
339 Loc = Builder.CreateLoad(Loc);
340 // Loc = Builder.CreateBitCast(Loc, Ty);
341 }
342 Builder.CreateStore(Loc, Addr);
343 } else if (r.isComplex())
344 // FIXME: implement
345 ErrorUnsupported(BE, "complex in block literal");
346 else if (r.isAggregate())
347 ; // Already created into the destination
348 else
349 assert (0 && "bad block variable");
350 // FIXME: Ensure that the offset created by the backend for
351 // the struct matches the previously computed offset in BlockDecls.
352 }
353 NoteForHelper.resize(NumHelpers);
Mike Stumpcf62d392009-03-06 18:42:23 +0000354
355 // __descriptor
Blaine Garst2a7eb282010-02-23 21:51:17 +0000356 llvm::Value *Descriptor = BuildDescriptorBlockDecl(BE,
357 subBlockHasCopyDispose,
Mike Stumpa803b0e2009-03-25 17:58:24 +0000358 subBlockSize, Ty,
Mike Stump08920992009-03-07 02:35:30 +0000359 &NoteForHelper);
360 Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
361 Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
Mike Stumpe5fee252009-02-13 16:19:19 +0000362 }
Mike Stump00470a12009-03-05 08:32:30 +0000363
Mike Stumpbd65cac2009-02-19 01:01:04 +0000364 QualType BPT = BE->getType();
Fariborz Jahanian263c4de2010-02-10 23:34:57 +0000365 V = Builder.CreateBitCast(V, ConvertType(BPT));
366 // See if this is a __weak block variable and the must call objc_read_weak
367 // on it.
368 const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>();
369 QualType RES = ftype->getResultType();
370 if (RES.isObjCGCWeak()) {
371 // Must cast argument to id*
372 const llvm::Type *ObjectPtrTy =
373 ConvertType(CGM.getContext().getObjCIdType());
374 const llvm::Type *PtrObjectPtrTy =
375 llvm::PointerType::getUnqual(ObjectPtrTy);
376 V = Builder.CreateBitCast(V, PtrObjectPtrTy);
377 V = CGM.getObjCRuntime().EmitObjCWeakRead(*this, V);
378 }
379 return V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000380}
381
382
Mike Stump2a998142009-03-04 18:17:45 +0000383const llvm::Type *BlockModule::getBlockDescriptorType() {
Mike Stumpab695142009-02-13 15:16:56 +0000384 if (BlockDescriptorType)
385 return BlockDescriptorType;
386
Mike Stumpa5448542009-02-13 15:32:32 +0000387 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000388 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000389
Mike Stumpab695142009-02-13 15:16:56 +0000390 // struct __block_descriptor {
391 // unsigned long reserved;
392 // unsigned long block_size;
Blaine Garst2a7eb282010-02-23 21:51:17 +0000393 //
394 // // later, the following will be added
395 //
396 // struct {
397 // void (*copyHelper)();
398 // void (*copyHelper)();
399 // } helpers; // !!! optional
400 //
401 // const char *signature; // the block signature
402 // const char *layout; // reserved
Mike Stumpab695142009-02-13 15:16:56 +0000403 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000404 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(),
405 UnsignedLongTy,
Mike Stumpa5448542009-02-13 15:32:32 +0000406 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000407 NULL);
408
409 getModule().addTypeName("struct.__block_descriptor",
410 BlockDescriptorType);
411
412 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000413}
414
Mike Stump2a998142009-03-04 18:17:45 +0000415const llvm::Type *BlockModule::getGenericBlockLiteralType() {
Mike Stump9b8a7972009-02-13 15:25:34 +0000416 if (GenericBlockLiteralType)
417 return GenericBlockLiteralType;
418
Mike Stumpa5448542009-02-13 15:32:32 +0000419 const llvm::Type *BlockDescPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000420 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000421
Mike Stump7cbb3602009-02-13 16:01:35 +0000422 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
423 getTypes().ConvertType(getContext().IntTy));
424
Mike Stump9b8a7972009-02-13 15:25:34 +0000425 // struct __block_literal_generic {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000426 // void *__isa;
427 // int __flags;
428 // int __reserved;
429 // void (*__invoke)(void *);
430 // struct __block_descriptor *__descriptor;
Mike Stump9b8a7972009-02-13 15:25:34 +0000431 // };
Blaine Garst2a7eb282010-02-23 21:51:17 +0000432 GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
Owen Anderson47a434f2009-08-05 23:18:46 +0000433 PtrToInt8Ty,
Mike Stump7cbb3602009-02-13 16:01:35 +0000434 IntTy,
435 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000436 PtrToInt8Ty,
Mike Stump9b8a7972009-02-13 15:25:34 +0000437 BlockDescPtrTy,
438 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000439
Mike Stump9b8a7972009-02-13 15:25:34 +0000440 getModule().addTypeName("struct.__block_literal_generic",
441 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000442
Mike Stump9b8a7972009-02-13 15:25:34 +0000443 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000444}
445
Mike Stumpbd65cac2009-02-19 01:01:04 +0000446
Anders Carlssona1736c02009-12-24 21:13:40 +0000447RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E,
448 ReturnValueSlot ReturnValue) {
Mike Stumpa5448542009-02-13 15:32:32 +0000449 const BlockPointerType *BPT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000450 E->getCallee()->getType()->getAs<BlockPointerType>();
Mike Stumpa5448542009-02-13 15:32:32 +0000451
Anders Carlssonacfde802009-02-12 00:39:25 +0000452 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
453
454 // Get a pointer to the generic block literal.
455 const llvm::Type *BlockLiteralTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000456 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000457
458 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000459 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000460 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
461
462 // Get the function pointer from the literal.
463 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Anders Carlssonacfde802009-02-12 00:39:25 +0000464
Mike Stumpa5448542009-02-13 15:32:32 +0000465 BlockLiteral =
466 Builder.CreateBitCast(BlockLiteral,
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000467 llvm::Type::getInt8PtrTy(VMContext),
Anders Carlssonacfde802009-02-12 00:39:25 +0000468 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000469
Anders Carlssonacfde802009-02-12 00:39:25 +0000470 // Add the block literal.
471 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
472 CallArgList Args;
473 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000474
Anders Carlsson782f3972009-04-08 23:13:16 +0000475 QualType FnType = BPT->getPointeeType();
476
Anders Carlssonacfde802009-02-12 00:39:25 +0000477 // And the rest of the arguments.
John McCall183700f2009-09-21 23:43:11 +0000478 EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(),
Anders Carlsson782f3972009-04-08 23:13:16 +0000479 E->arg_begin(), E->arg_end());
Mike Stumpa5448542009-02-13 15:32:32 +0000480
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000481 // Load the function.
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000482 llvm::Value *Func = Builder.CreateLoad(FuncPtr, "tmp");
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000483
John McCall04a67a62010-02-05 21:31:56 +0000484 const FunctionType *FuncTy = FnType->getAs<FunctionType>();
485 QualType ResultType = FuncTy->getResultType();
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000486
Mike Stump1eb44332009-09-09 15:08:12 +0000487 const CGFunctionInfo &FnInfo =
Rafael Espindola264ba482010-03-30 20:24:48 +0000488 CGM.getTypes().getFunctionInfo(ResultType, Args,
489 FuncTy->getExtInfo());
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000491 // Cast the function pointer to the right type.
Mike Stump1eb44332009-09-09 15:08:12 +0000492 const llvm::Type *BlockFTy =
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000493 CGM.getTypes().GetFunctionType(FnInfo, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Owen Anderson96e0fc72009-07-29 22:16:19 +0000495 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000496 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Anders Carlssonacfde802009-02-12 00:39:25 +0000498 // And call the block.
Anders Carlssona1736c02009-12-24 21:13:40 +0000499 return EmitCall(FnInfo, Func, ReturnValue, Args);
Anders Carlssonacfde802009-02-12 00:39:25 +0000500}
Anders Carlssond5cab542009-02-12 17:55:02 +0000501
John McCallea1471e2010-05-20 01:18:31 +0000502void CodeGenFunction::AllocateBlockCXXThisPointer(const CXXThisExpr *E) {
503 assert(BlockCXXThisOffset.isZero() && "already computed 'this' pointer");
504
505 // Figure out what the offset is.
506 QualType T = E->getType();
507 std::pair<CharUnits,CharUnits> TypeInfo = getContext().getTypeInfoInChars(T);
508 CharUnits Offset = getBlockOffset(TypeInfo.first, TypeInfo.second);
509
510 BlockCXXThisOffset = Offset;
511 BlockLayout.push_back(E);
512}
513
514void CodeGenFunction::AllocateBlockDecl(const BlockDeclRefExpr *E) {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000515 const ValueDecl *VD = E->getDecl();
John McCallea1471e2010-05-20 01:18:31 +0000516 CharUnits &Offset = BlockDecls[VD];
Mike Stumpdab514f2009-03-04 03:23:46 +0000517
Mike Stumpdab514f2009-03-04 03:23:46 +0000518 // See if we have already allocated an offset for this variable.
John McCallea1471e2010-05-20 01:18:31 +0000519 if (!Offset.isZero())
520 return;
Mike Stumpea26cb52009-10-21 03:49:08 +0000521
522 // Don't run the expensive check, unless we have to.
Mike Stump083c25e2009-10-22 00:49:09 +0000523 if (!BlockHasCopyDispose)
524 if (E->isByRef()
525 || BlockRequiresCopying(E->getType()))
526 BlockHasCopyDispose = true;
Mike Stumpea26cb52009-10-21 03:49:08 +0000527
John McCallea1471e2010-05-20 01:18:31 +0000528 const ValueDecl *D = cast<ValueDecl>(E->getDecl());
Mike Stumpea26cb52009-10-21 03:49:08 +0000529
John McCallea1471e2010-05-20 01:18:31 +0000530 CharUnits Size;
531 CharUnits Align;
532
533 if (E->isByRef()) {
534 llvm::tie(Size,Align) =
535 getContext().getTypeInfoInChars(getContext().VoidPtrTy);
536 } else {
537 Size = getContext().getTypeSizeInChars(D->getType());
538 Align = getContext().getDeclAlign(D);
539 }
540
541 Offset = getBlockOffset(Size, Align);
542 BlockLayout.push_back(E);
Mike Stumpea26cb52009-10-21 03:49:08 +0000543}
544
545llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
546 const ValueDecl *VD = E->getDecl();
John McCallea1471e2010-05-20 01:18:31 +0000547 CharUnits offset = BlockDecls[VD];
548 assert(!offset.isZero() && "getting address of unallocated decl");
Mike Stumpdab514f2009-03-04 03:23:46 +0000549
550 llvm::Value *BlockLiteral = LoadBlockStruct();
551 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
Mike Stumpea26cb52009-10-21 03:49:08 +0000552 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Ken Dyck199c3d62010-01-11 17:06:35 +0000553 offset.getQuantity()),
Mike Stump58a85142009-03-04 22:48:06 +0000554 "block.literal");
Mike Stumpdab514f2009-03-04 03:23:46 +0000555 if (E->isByRef()) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000556 const llvm::Type *PtrStructTy
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000557 = llvm::PointerType::get(BuildByRefType(VD), 0);
Mike Stumpa8b60c92009-03-21 21:00:35 +0000558 // The block literal will need a copy/destroy helper.
559 BlockHasCopyDispose = true;
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000560
561 const llvm::Type *Ty = PtrStructTy;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000562 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000563 V = Builder.CreateBitCast(V, Ty);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000564 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000565 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000566 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000567 V = Builder.CreateBitCast(V, PtrStructTy);
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000568 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
569 VD->getNameAsString());
Fariborz Jahaniand33ded52010-05-04 17:59:32 +0000570 if (VD->getType()->isReferenceType())
571 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000572 } else {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000573 const llvm::Type *Ty = CGM.getTypes().ConvertType(VD->getType());
574
Owen Anderson96e0fc72009-07-29 22:16:19 +0000575 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000576 V = Builder.CreateBitCast(V, Ty);
577 }
578 return V;
579}
580
Mike Stump6cc88f72009-03-20 21:53:12 +0000581void CodeGenFunction::BlockForwardSelf() {
582 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
583 ImplicitParamDecl *SelfDecl = OMD->getSelfDecl();
584 llvm::Value *&DMEntry = LocalDeclMap[SelfDecl];
585 if (DMEntry)
586 return;
587 // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care
588 BlockDeclRefExpr *BDRE = new (getContext())
589 BlockDeclRefExpr(SelfDecl,
590 SelfDecl->getType(), SourceLocation(), false);
591 DMEntry = GetAddrOfBlockDecl(BDRE);
592}
593
Mike Stump67a64482009-02-14 22:16:35 +0000594llvm::Constant *
Mike Stump90a90432009-03-04 18:47:42 +0000595BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000596 // Generate the block descriptor.
597 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000598 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
599 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000600
Blaine Garst2a7eb282010-02-23 21:51:17 +0000601 llvm::Constant *DescriptorFields[4];
Mike Stumpa5448542009-02-13 15:32:32 +0000602
Anders Carlssond5cab542009-02-12 17:55:02 +0000603 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000604 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000605
Anders Carlssond5cab542009-02-12 17:55:02 +0000606 // Block literal size. For global blocks we just use the size of the generic
607 // block literal struct.
Ken Dyck687cc4a2010-01-26 13:48:07 +0000608 CharUnits BlockLiteralSize =
609 CGM.GetTargetTypeStoreSize(getGenericBlockLiteralType());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000610 DescriptorFields[1] =
Ken Dyck199c3d62010-01-11 17:06:35 +0000611 llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize.getQuantity());
Blaine Garst2a7eb282010-02-23 21:51:17 +0000612
613 // signature. non-optional ObjC-style method descriptor @encode sequence
614 std::string BlockTypeEncoding;
615 CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
Mike Stumpa5448542009-02-13 15:32:32 +0000616
Blaine Garst2a7eb282010-02-23 21:51:17 +0000617 DescriptorFields[2] = llvm::ConstantExpr::getBitCast(
618 CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty);
619
620 // layout
621 DescriptorFields[3] =
622 llvm::ConstantInt::get(UnsignedLongTy,0);
623
624 // build the structure from the 4 elements
Mike Stumpa5448542009-02-13 15:32:32 +0000625 llvm::Constant *DescriptorStruct =
Blaine Garst2a7eb282010-02-23 21:51:17 +0000626 llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 4, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000627
Anders Carlssond5cab542009-02-12 17:55:02 +0000628 llvm::GlobalVariable *Descriptor =
Owen Anderson1c431b32009-07-08 19:05:04 +0000629 new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000630 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000631 DescriptorStruct, "__block_descriptor_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000632
David Chisnall5e530af2009-11-17 19:33:30 +0000633 int FieldCount = 5;
Anders Carlssond5cab542009-02-12 17:55:02 +0000634 // Generate the constants for the block literal.
David Chisnall5e530af2009-11-17 19:33:30 +0000635
636 std::vector<llvm::Constant*> LiteralFields(FieldCount);
Mike Stumpa5448542009-02-13 15:32:32 +0000637
Mike Stump67a64482009-02-14 22:16:35 +0000638 CodeGenFunction::BlockInfo Info(0, n);
Ken Dyck199c3d62010-01-11 17:06:35 +0000639 CharUnits subBlockSize;
Ken Dyck30a8a272010-01-26 19:13:33 +0000640 CharUnits subBlockAlign;
John McCallea1471e2010-05-20 01:18:31 +0000641 llvm::SmallVector<const Expr *, 8> subBlockLayout;
Daniel Dunbard67b09a2009-03-12 03:07:24 +0000642 bool subBlockHasCopyDispose = false;
Mike Stump7f28a9c2009-03-13 23:34:28 +0000643 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000644 llvm::Function *Fn
Mike Stump6cc88f72009-03-20 21:53:12 +0000645 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000646 subBlockSize,
Mike Stump90a90432009-03-04 18:47:42 +0000647 subBlockAlign,
John McCallea1471e2010-05-20 01:18:31 +0000648 subBlockLayout,
Mike Stump00470a12009-03-05 08:32:30 +0000649 subBlockHasCopyDispose);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000650 assert(subBlockSize == BlockLiteralSize
651 && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000652
Anders Carlssond5cab542009-02-12 17:55:02 +0000653 // isa
Mike Stumpf99f1d02009-02-13 17:23:42 +0000654 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000655
Anders Carlssond5cab542009-02-12 17:55:02 +0000656 // Flags
Blaine Garst2a7eb282010-02-23 21:51:17 +0000657 LiteralFields[1] =
Blaine Garsta36e2232010-03-05 01:29:59 +0000658 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE);
Mike Stumpa5448542009-02-13 15:32:32 +0000659
Anders Carlssond5cab542009-02-12 17:55:02 +0000660 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000661 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000662
Anders Carlssond5cab542009-02-12 17:55:02 +0000663 // Function
664 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000665
Anders Carlssond5cab542009-02-12 17:55:02 +0000666 // Descriptor
667 LiteralFields[4] = Descriptor;
David Chisnall5e530af2009-11-17 19:33:30 +0000668
Mike Stumpa5448542009-02-13 15:32:32 +0000669 llvm::Constant *BlockLiteralStruct =
David Chisnall5e530af2009-11-17 19:33:30 +0000670 llvm::ConstantStruct::get(VMContext, LiteralFields, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000671
672 llvm::GlobalVariable *BlockLiteral =
Owen Anderson1c431b32009-07-08 19:05:04 +0000673 new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000674 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000675 BlockLiteralStruct, "__block_literal_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000676
Anders Carlssond5cab542009-02-12 17:55:02 +0000677 return BlockLiteral;
678}
679
Mike Stump4e7a1f72009-02-21 20:00:35 +0000680llvm::Value *CodeGenFunction::LoadBlockStruct() {
Mike Stumpbf1914b2009-10-20 20:30:01 +0000681 llvm::Value *V = Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()],
682 "self");
683 // For now, we codegen based upon byte offsets.
684 return Builder.CreateBitCast(V, PtrToInt8Ty);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000685}
686
Mike Stump00470a12009-03-05 08:32:30 +0000687llvm::Function *
688CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
689 const BlockInfo& Info,
Mike Stump6cc88f72009-03-20 21:53:12 +0000690 const Decl *OuterFuncDecl,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000691 llvm::DenseMap<const Decl*, llvm::Value*> ldm,
Ken Dyck199c3d62010-01-11 17:06:35 +0000692 CharUnits &Size,
Ken Dyck30a8a272010-01-26 19:13:33 +0000693 CharUnits &Align,
John McCallea1471e2010-05-20 01:18:31 +0000694 llvm::SmallVectorImpl<const Expr *> &subBlockLayout,
Mike Stump00470a12009-03-05 08:32:30 +0000695 bool &subBlockHasCopyDispose) {
Devang Patel963dfbd2009-04-15 21:51:44 +0000696
697 // Check if we should generate debug info for this block.
698 if (CGM.getDebugInfo())
699 DebugInfo = CGM.getDebugInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Mike Stump7f28a9c2009-03-13 23:34:28 +0000701 // Arrange for local static and local extern declarations to appear
702 // to be local to this function as well, as they are directly referenced
703 // in a block.
704 for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
705 i != ldm.end();
706 ++i) {
707 const VarDecl *VD = dyn_cast<VarDecl>(i->first);
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000709 if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage())
Mike Stump7f28a9c2009-03-13 23:34:28 +0000710 LocalDeclMap[VD] = i->second;
711 }
712
Ken Dyck687cc4a2010-01-26 13:48:07 +0000713 BlockOffset =
714 CGM.GetTargetTypeStoreSize(CGM.getGenericBlockLiteralType());
Ken Dyck30a8a272010-01-26 19:13:33 +0000715 BlockAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Eli Friedman48f91222009-03-28 03:24:54 +0000716
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000717 const FunctionType *BlockFunctionType = BExpr->getFunctionType();
718 QualType ResultType;
Rafael Espindola264ba482010-03-30 20:24:48 +0000719 FunctionType::ExtInfo EInfo = getFunctionExtInfo(*BlockFunctionType);
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000720 bool IsVariadic;
Mike Stump1eb44332009-09-09 15:08:12 +0000721 if (const FunctionProtoType *FTy =
Fariborz Jahanianda0895d2009-04-11 18:54:57 +0000722 dyn_cast<FunctionProtoType>(BlockFunctionType)) {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000723 ResultType = FTy->getResultType();
724 IsVariadic = FTy->isVariadic();
Mike Stumpb3589f42009-07-30 22:28:39 +0000725 } else {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000726 // K&R style block.
727 ResultType = BlockFunctionType->getResultType();
728 IsVariadic = false;
729 }
Mike Stumpa5448542009-02-13 15:32:32 +0000730
Anders Carlssond5cab542009-02-12 17:55:02 +0000731 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000732
Mike Stumpea26cb52009-10-21 03:49:08 +0000733 CurFuncDecl = OuterFuncDecl;
734
Chris Lattner161d36d2009-02-28 19:01:03 +0000735 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlssond5cab542009-02-12 17:55:02 +0000736
Mike Stumpea26cb52009-10-21 03:49:08 +0000737 IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor");
Mike Stumpadaaad32009-10-20 02:12:22 +0000738
John McCallea1471e2010-05-20 01:18:31 +0000739 // Build the block struct now.
Mike Stump0298d382009-10-21 22:02:08 +0000740 AllocateAllBlockDeclRefs(Info, this);
Mike Stumpea26cb52009-10-21 03:49:08 +0000741
Mike Stump083c25e2009-10-22 00:49:09 +0000742 QualType ParmTy = getContext().getBlockParmType(BlockHasCopyDispose,
John McCallea1471e2010-05-20 01:18:31 +0000743 BlockLayout);
744
Anders Carlssond5cab542009-02-12 17:55:02 +0000745 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000746 ImplicitParamDecl *SelfDecl =
John McCall2888b652010-04-30 21:35:41 +0000747 ImplicitParamDecl::Create(getContext(), const_cast<BlockDecl*>(BD),
Mike Stumpadaaad32009-10-20 02:12:22 +0000748 SourceLocation(), II,
749 ParmTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000750
Anders Carlssond5cab542009-02-12 17:55:02 +0000751 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump4e7a1f72009-02-21 20:00:35 +0000752 BlockStructDecl = SelfDecl;
Mike Stumpa5448542009-02-13 15:32:32 +0000753
Steve Naroffe78b8092009-03-13 16:56:44 +0000754 for (BlockDecl::param_const_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000755 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000756 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000757
758 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +0000759 CGM.getTypes().getFunctionInfo(ResultType, Args, EInfo);
Anders Carlssond5cab542009-02-12 17:55:02 +0000760
Anders Carlssond5cab542009-02-12 17:55:02 +0000761 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000762 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic);
Mike Stumpa5448542009-02-13 15:32:32 +0000763
764 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000765 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000766 llvm::Twine("__") + Info.Name + "_block_invoke_",
Anders Carlssond5cab542009-02-12 17:55:02 +0000767 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000768
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000769 CGM.SetInternalFunctionAttributes(BD, Fn, FI);
770
Chris Lattner4863db42009-04-23 07:18:56 +0000771 StartFunction(BD, ResultType, Fn, Args,
Chris Lattner161d36d2009-02-28 19:01:03 +0000772 BExpr->getBody()->getLocEnd());
Mike Stumpb1a6e682009-09-30 02:43:10 +0000773
Chris Lattner4863db42009-04-23 07:18:56 +0000774 CurFuncDecl = OuterFuncDecl;
Chris Lattnerb5437d22009-04-23 05:30:27 +0000775 CurCodeDecl = BD;
Mike Stumpb289b3f2009-10-01 22:29:41 +0000776
John McCallea1471e2010-05-20 01:18:31 +0000777 // If we have a C++ 'this' reference, go ahead and force it into
778 // existence now.
779 if (Info.CXXThisRef) {
780 assert(!BlockCXXThisOffset.isZero() &&
781 "haven't yet allocated 'this' reference");
782
783 // TODO: I have a dream that one day this will be typed.
784 llvm::Value *BlockLiteral = LoadBlockStruct();
785 llvm::Value *ThisPtrRaw =
786 Builder.CreateConstInBoundsGEP1_64(BlockLiteral,
787 BlockCXXThisOffset.getQuantity(),
788 "this.ptr.raw");
789
790 const llvm::Type *Ty =
791 CGM.getTypes().ConvertType(Info.CXXThisRef->getType());
792 Ty = llvm::PointerType::get(Ty, 0);
793 llvm::Value *ThisPtr = Builder.CreateBitCast(ThisPtrRaw, Ty, "this.ptr");
794
795 CXXThisValue = Builder.CreateLoad(ThisPtr, "this");
796 }
797
Mike Stumpb289b3f2009-10-01 22:29:41 +0000798 // Save a spot to insert the debug information for all the BlockDeclRefDecls.
799 llvm::BasicBlock *entry = Builder.GetInsertBlock();
800 llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint();
801 --entry_ptr;
802
Chris Lattner161d36d2009-02-28 19:01:03 +0000803 EmitStmt(BExpr->getBody());
Mike Stumpb289b3f2009-10-01 22:29:41 +0000804
Mike Stumpde8c5c72009-10-01 00:27:30 +0000805 // Remember where we were...
806 llvm::BasicBlock *resume = Builder.GetInsertBlock();
Mike Stumpb289b3f2009-10-01 22:29:41 +0000807
Mike Stumpde8c5c72009-10-01 00:27:30 +0000808 // Go back to the entry.
Mike Stumpb289b3f2009-10-01 22:29:41 +0000809 ++entry_ptr;
810 Builder.SetInsertPoint(entry, entry_ptr);
811
Mike Stumpb1a6e682009-09-30 02:43:10 +0000812 if (CGDebugInfo *DI = getDebugInfo()) {
Mike Stumpb1a6e682009-09-30 02:43:10 +0000813 // Emit debug information for all the BlockDeclRefDecls.
John McCallea1471e2010-05-20 01:18:31 +0000814 // FIXME: also for 'this'
815 for (unsigned i = 0, e = BlockLayout.size(); i != e; ++i) {
816 if (const BlockDeclRefExpr *BDRE =
817 dyn_cast<BlockDeclRefExpr>(BlockLayout[i])) {
Mike Stumpb1a6e682009-09-30 02:43:10 +0000818 const ValueDecl *D = BDRE->getDecl();
819 DI->setLocation(D->getLocation());
820 DI->EmitDeclareOfBlockDeclRefVariable(BDRE,
821 LocalDeclMap[getBlockStructDecl()],
822 Builder, this);
823 }
824 }
Mike Stumpb1a6e682009-09-30 02:43:10 +0000825 }
Mike Stumpde8c5c72009-10-01 00:27:30 +0000826 // And resume where we left off.
827 if (resume == 0)
828 Builder.ClearInsertionPoint();
829 else
830 Builder.SetInsertPoint(resume);
Mike Stumpb1a6e682009-09-30 02:43:10 +0000831
Chris Lattner161d36d2009-02-28 19:01:03 +0000832 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlssond5cab542009-02-12 17:55:02 +0000833
Mike Stump8a2b4b12009-02-25 23:33:13 +0000834 // The runtime needs a minimum alignment of a void *.
Ken Dyck30a8a272010-01-26 19:13:33 +0000835 CharUnits MinAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Ken Dyck199c3d62010-01-11 17:06:35 +0000836 BlockOffset = CharUnits::fromQuantity(
Ken Dyck30a8a272010-01-26 19:13:33 +0000837 llvm::RoundUpToAlignment(BlockOffset.getQuantity(),
838 MinAlign.getQuantity()));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000839
Mike Stump4e7a1f72009-02-21 20:00:35 +0000840 Size = BlockOffset;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000841 Align = BlockAlign;
John McCallea1471e2010-05-20 01:18:31 +0000842 subBlockLayout = BlockLayout;
Mike Stump00470a12009-03-05 08:32:30 +0000843 subBlockHasCopyDispose |= BlockHasCopyDispose;
Anders Carlssond5cab542009-02-12 17:55:02 +0000844 return Fn;
845}
Mike Stumpa99038c2009-02-28 09:07:16 +0000846
John McCallea1471e2010-05-20 01:18:31 +0000847CharUnits BlockFunction::getBlockOffset(CharUnits Size, CharUnits Align) {
848 assert((Align.isPositive()) && "alignment must be 1 byte or more");
Mike Stumpa99038c2009-02-28 09:07:16 +0000849
Ken Dyck199c3d62010-01-11 17:06:35 +0000850 CharUnits OldOffset = BlockOffset;
Mike Stumpa99038c2009-02-28 09:07:16 +0000851
852 // Ensure proper alignment, even if it means we have to have a gap
Ken Dyck199c3d62010-01-11 17:06:35 +0000853 BlockOffset = CharUnits::fromQuantity(
Ken Dyck30a8a272010-01-26 19:13:33 +0000854 llvm::RoundUpToAlignment(BlockOffset.getQuantity(), Align.getQuantity()));
Mike Stumpa99038c2009-02-28 09:07:16 +0000855 BlockAlign = std::max(Align, BlockAlign);
Mike Stump00470a12009-03-05 08:32:30 +0000856
Ken Dyck199c3d62010-01-11 17:06:35 +0000857 CharUnits Pad = BlockOffset - OldOffset;
858 if (Pad.isPositive()) {
Mike Stumpa99038c2009-02-28 09:07:16 +0000859 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
Ken Dyck199c3d62010-01-11 17:06:35 +0000860 llvm::APInt(32,
861 Pad.getQuantity()),
Mike Stumpa99038c2009-02-28 09:07:16 +0000862 ArrayType::Normal, 0);
Douglas Gregorba55ec22010-05-11 18:17:16 +0000863 ValueDecl *PadDecl = VarDecl::Create(getContext(),
864 getContext().getTranslationUnitDecl(),
865 SourceLocation(),
Douglas Gregor16573fa2010-04-19 22:54:31 +0000866 0, QualType(PadTy), 0,
867 VarDecl::None, VarDecl::None);
John McCallea1471e2010-05-20 01:18:31 +0000868 Expr *E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
869 SourceLocation());
870 BlockLayout.push_back(E);
Mike Stumpa99038c2009-02-28 09:07:16 +0000871 }
Mike Stumpa99038c2009-02-28 09:07:16 +0000872
873 BlockOffset += Size;
John McCallea1471e2010-05-20 01:18:31 +0000874 return BlockOffset - Size;
Mike Stumpa99038c2009-02-28 09:07:16 +0000875}
Mike Stumpdab514f2009-03-04 03:23:46 +0000876
Mike Stump08920992009-03-07 02:35:30 +0000877llvm::Constant *BlockFunction::
878GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000879 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000880 QualType R = getContext().VoidTy;
881
882 FunctionArgList Args;
883 // FIXME: This leaks
Mike Stump08920992009-03-07 02:35:30 +0000884 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +0000885 ImplicitParamDecl::Create(getContext(), 0,
886 SourceLocation(), 0,
Mike Stump08920992009-03-07 02:35:30 +0000887 getContext().getPointerType(getContext().VoidTy));
888 Args.push_back(std::make_pair(Dst, Dst->getType()));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000889 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +0000890 ImplicitParamDecl::Create(getContext(), 0,
891 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000892 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000893 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000894
Mike Stumpa4f668f2009-03-06 01:33:24 +0000895 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +0000896 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000897
Mike Stump3899a7f2009-06-05 23:26:36 +0000898 // FIXME: We'd like to put these into a mergable by content, with
899 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000900 CodeGenTypes &Types = CGM.getTypes();
901 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
902
903 llvm::Function *Fn =
904 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000905 "__copy_helper_block_", &CGM.getModule());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000906
907 IdentifierInfo *II
908 = &CGM.getContext().Idents.get("__copy_helper_block_");
909
910 FunctionDecl *FD = FunctionDecl::Create(getContext(),
911 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000912 SourceLocation(), II, R, 0,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000913 FunctionDecl::Static,
914 FunctionDecl::None,
915 false,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000916 true);
917 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump08920992009-03-07 02:35:30 +0000918
919 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
920 llvm::Type *PtrPtrT;
Mike Stump08920992009-03-07 02:35:30 +0000921
Mike Stumpb7477cf2009-04-10 18:52:28 +0000922 if (NoteForHelperp) {
923 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump08920992009-03-07 02:35:30 +0000924
Owen Anderson96e0fc72009-07-29 22:16:19 +0000925 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000926 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
927 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump08920992009-03-07 02:35:30 +0000928
Mike Stumpb7477cf2009-04-10 18:52:28 +0000929 llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000930 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000931 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000932 DstObj = Builder.CreateBitCast(DstObj, PtrPtrT);
933 DstObj = Builder.CreateLoad(DstObj);
Mike Stump08920992009-03-07 02:35:30 +0000934
Mike Stumpb7477cf2009-04-10 18:52:28 +0000935 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
936 int flag = NoteForHelper[i].flag;
937 int index = NoteForHelper[i].index;
Mike Stump08920992009-03-07 02:35:30 +0000938
Mike Stumpb7477cf2009-04-10 18:52:28 +0000939 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
940 || NoteForHelper[i].RequiresCopying) {
941 llvm::Value *Srcv = SrcObj;
942 Srcv = Builder.CreateStructGEP(Srcv, index);
943 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000944 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000945 Srcv = Builder.CreateLoad(Srcv);
946
947 llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
948 Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
949
Owen Anderson0032b272009-08-13 21:57:51 +0000950 llvm::Value *N = llvm::ConstantInt::get(
951 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000952 llvm::Value *F = getBlockObjectAssign();
953 Builder.CreateCall3(F, Dstv, Srcv, N);
954 }
Mike Stump08920992009-03-07 02:35:30 +0000955 }
956 }
957
Mike Stumpa4f668f2009-03-06 01:33:24 +0000958 CGF.FinishFunction();
959
Owen Anderson3c4972d2009-07-29 18:54:39 +0000960 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000961}
962
Mike Stumpcf62d392009-03-06 18:42:23 +0000963llvm::Constant *BlockFunction::
Mike Stump08920992009-03-07 02:35:30 +0000964GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
965 const llvm::StructType* T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000966 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000967 QualType R = getContext().VoidTy;
968
969 FunctionArgList Args;
970 // FIXME: This leaks
971 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +0000972 ImplicitParamDecl::Create(getContext(), 0,
973 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000974 getContext().getPointerType(getContext().VoidTy));
975
976 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Mike Stumpa4f668f2009-03-06 01:33:24 +0000978 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +0000979 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000980
Mike Stump3899a7f2009-06-05 23:26:36 +0000981 // FIXME: We'd like to put these into a mergable by content, with
982 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000983 CodeGenTypes &Types = CGM.getTypes();
984 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
985
986 llvm::Function *Fn =
987 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000988 "__destroy_helper_block_", &CGM.getModule());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000989
990 IdentifierInfo *II
991 = &CGM.getContext().Idents.get("__destroy_helper_block_");
992
993 FunctionDecl *FD = FunctionDecl::Create(getContext(),
994 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000995 SourceLocation(), II, R, 0,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000996 FunctionDecl::Static,
997 FunctionDecl::None,
998 false, true);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000999 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1edf6b62009-03-07 02:53:18 +00001000
Mike Stumpb7477cf2009-04-10 18:52:28 +00001001 if (NoteForHelperp) {
1002 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump1edf6b62009-03-07 02:53:18 +00001003
Mike Stumpb7477cf2009-04-10 18:52:28 +00001004 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
1005 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +00001006 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +00001007 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
1008 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump1edf6b62009-03-07 02:53:18 +00001009
Mike Stumpb7477cf2009-04-10 18:52:28 +00001010 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
1011 int flag = NoteForHelper[i].flag;
1012 int index = NoteForHelper[i].index;
Mike Stump1edf6b62009-03-07 02:53:18 +00001013
Mike Stumpb7477cf2009-04-10 18:52:28 +00001014 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
1015 || NoteForHelper[i].RequiresCopying) {
1016 llvm::Value *Srcv = SrcObj;
1017 Srcv = Builder.CreateStructGEP(Srcv, index);
1018 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001019 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +00001020 Srcv = Builder.CreateLoad(Srcv);
1021
1022 BuildBlockRelease(Srcv, flag);
1023 }
Mike Stump1edf6b62009-03-07 02:53:18 +00001024 }
1025 }
1026
Mike Stumpa4f668f2009-03-06 01:33:24 +00001027 CGF.FinishFunction();
1028
Owen Anderson3c4972d2009-07-29 18:54:39 +00001029 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpa4f668f2009-03-06 01:33:24 +00001030}
1031
Mike Stump08920992009-03-07 02:35:30 +00001032llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001033 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump08920992009-03-07 02:35:30 +00001034 return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
1035 T, NoteForHelper);
Mike Stumpa4f668f2009-03-06 01:33:24 +00001036}
1037
Mike Stump08920992009-03-07 02:35:30 +00001038llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001039 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stump08920992009-03-07 02:35:30 +00001040 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001041 T, NoteForHelperp);
Mike Stumpdab514f2009-03-04 03:23:46 +00001042}
Mike Stump797b6322009-03-05 01:23:13 +00001043
Mike Stumpee094222009-03-06 06:12:24 +00001044llvm::Constant *BlockFunction::
1045GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
Mike Stump45031c02009-03-06 02:29:21 +00001046 QualType R = getContext().VoidTy;
1047
1048 FunctionArgList Args;
1049 // FIXME: This leaks
Mike Stumpee094222009-03-06 06:12:24 +00001050 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +00001051 ImplicitParamDecl::Create(getContext(), 0,
1052 SourceLocation(), 0,
Mike Stumpee094222009-03-06 06:12:24 +00001053 getContext().getPointerType(getContext().VoidTy));
1054 Args.push_back(std::make_pair(Dst, Dst->getType()));
1055
1056 // FIXME: This leaks
Mike Stump45031c02009-03-06 02:29:21 +00001057 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001058 ImplicitParamDecl::Create(getContext(), 0,
1059 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001060 getContext().getPointerType(getContext().VoidTy));
Mike Stump45031c02009-03-06 02:29:21 +00001061 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001062
Mike Stump45031c02009-03-06 02:29:21 +00001063 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +00001064 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stump45031c02009-03-06 02:29:21 +00001065
Mike Stump45031c02009-03-06 02:29:21 +00001066 CodeGenTypes &Types = CGM.getTypes();
1067 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1068
Mike Stump3899a7f2009-06-05 23:26:36 +00001069 // FIXME: We'd like to put these into a mergable by content, with
1070 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001071 llvm::Function *Fn =
1072 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001073 "__Block_byref_id_object_copy_", &CGM.getModule());
Mike Stump45031c02009-03-06 02:29:21 +00001074
1075 IdentifierInfo *II
1076 = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
1077
1078 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1079 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001080 SourceLocation(), II, R, 0,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001081 FunctionDecl::Static,
1082 FunctionDecl::None,
1083 false, true);
Mike Stump45031c02009-03-06 02:29:21 +00001084 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpee094222009-03-06 06:12:24 +00001085
1086 // dst->x
1087 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001088 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001089 V = Builder.CreateLoad(V);
Mike Stumpee094222009-03-06 06:12:24 +00001090 V = Builder.CreateStructGEP(V, 6, "x");
1091 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
1092
1093 // src->x
1094 V = CGF.GetAddrOfLocalVar(Src);
1095 V = Builder.CreateLoad(V);
1096 V = Builder.CreateBitCast(V, T);
1097 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001098 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpee094222009-03-06 06:12:24 +00001099 llvm::Value *SrcObj = Builder.CreateLoad(V);
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Mike Stumpee094222009-03-06 06:12:24 +00001101 flag |= BLOCK_BYREF_CALLER;
1102
Owen Anderson0032b272009-08-13 21:57:51 +00001103 llvm::Value *N = llvm::ConstantInt::get(
1104 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpee094222009-03-06 06:12:24 +00001105 llvm::Value *F = getBlockObjectAssign();
1106 Builder.CreateCall3(F, DstObj, SrcObj, N);
1107
Mike Stump45031c02009-03-06 02:29:21 +00001108 CGF.FinishFunction();
1109
Owen Anderson3c4972d2009-07-29 18:54:39 +00001110 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001111}
1112
Mike Stump1851b682009-03-06 04:53:30 +00001113llvm::Constant *
1114BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
1115 int flag) {
Mike Stump45031c02009-03-06 02:29:21 +00001116 QualType R = getContext().VoidTy;
1117
1118 FunctionArgList Args;
1119 // FIXME: This leaks
1120 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001121 ImplicitParamDecl::Create(getContext(), 0,
1122 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001123 getContext().getPointerType(getContext().VoidTy));
1124
1125 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001126
Mike Stump45031c02009-03-06 02:29:21 +00001127 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +00001128 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stump45031c02009-03-06 02:29:21 +00001129
Mike Stump45031c02009-03-06 02:29:21 +00001130 CodeGenTypes &Types = CGM.getTypes();
1131 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1132
Mike Stump3899a7f2009-06-05 23:26:36 +00001133 // FIXME: We'd like to put these into a mergable by content, with
1134 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001135 llvm::Function *Fn =
1136 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001137 "__Block_byref_id_object_dispose_",
Mike Stump45031c02009-03-06 02:29:21 +00001138 &CGM.getModule());
1139
1140 IdentifierInfo *II
1141 = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
1142
1143 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1144 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001145 SourceLocation(), II, R, 0,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001146 FunctionDecl::Static,
1147 FunctionDecl::None,
1148 false, true);
Mike Stump45031c02009-03-06 02:29:21 +00001149 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1851b682009-03-06 04:53:30 +00001150
1151 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001152 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001153 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001154 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001155 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001156 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001157
Mike Stump1851b682009-03-06 04:53:30 +00001158 flag |= BLOCK_BYREF_CALLER;
1159 BuildBlockRelease(V, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001160 CGF.FinishFunction();
1161
Owen Anderson3c4972d2009-07-29 18:54:39 +00001162 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001163}
1164
Mike Stumpee094222009-03-06 06:12:24 +00001165llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001166 int Flag, unsigned Align) {
1167 // All alignments below that of pointer alignment collapse down to just
Mike Stump3899a7f2009-06-05 23:26:36 +00001168 // pointer alignment, as we always have at least that much alignment to begin
1169 // with.
1170 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001171
Mike Stump3899a7f2009-06-05 23:26:36 +00001172 // As an optimization, we only generate a single function of each kind we
1173 // might need. We need a different one for each alignment and for each
1174 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001175 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1176 llvm::Constant *&Entry = CGM.AssignCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001177 if (Entry)
1178 return Entry;
Chris Lattner10976d92009-12-05 08:21:30 +00001179 return Entry = CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, Flag);
Mike Stump45031c02009-03-06 02:29:21 +00001180}
1181
Mike Stump1851b682009-03-06 04:53:30 +00001182llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001183 int Flag,
Mike Stump3899a7f2009-06-05 23:26:36 +00001184 unsigned Align) {
1185 // All alignments below that of pointer alignment collpase down to just
1186 // pointer alignment, as we always have at least that much alignment to begin
1187 // with.
1188 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001189
Mike Stump3899a7f2009-06-05 23:26:36 +00001190 // As an optimization, we only generate a single function of each kind we
1191 // might need. We need a different one for each alignment and for each
1192 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001193 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1194 llvm::Constant *&Entry = CGM.DestroyCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001195 if (Entry)
1196 return Entry;
Chris Lattner10976d92009-12-05 08:21:30 +00001197 return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, Flag);
Mike Stump45031c02009-03-06 02:29:21 +00001198}
1199
Mike Stump797b6322009-03-05 01:23:13 +00001200llvm::Value *BlockFunction::getBlockObjectDispose() {
1201 if (CGM.BlockObjectDispose == 0) {
1202 const llvm::FunctionType *FTy;
1203 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001204 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stump797b6322009-03-05 01:23:13 +00001205 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001206 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson96e0fc72009-07-29 22:16:19 +00001207 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stump00470a12009-03-05 08:32:30 +00001208 CGM.BlockObjectDispose
Mike Stump797b6322009-03-05 01:23:13 +00001209 = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
1210 }
1211 return CGM.BlockObjectDispose;
1212}
1213
Mike Stumpee094222009-03-06 06:12:24 +00001214llvm::Value *BlockFunction::getBlockObjectAssign() {
1215 if (CGM.BlockObjectAssign == 0) {
1216 const llvm::FunctionType *FTy;
1217 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001218 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stumpee094222009-03-06 06:12:24 +00001219 ArgTys.push_back(PtrToInt8Ty);
1220 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001221 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson96e0fc72009-07-29 22:16:19 +00001222 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stumpee094222009-03-06 06:12:24 +00001223 CGM.BlockObjectAssign
1224 = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
1225 }
1226 return CGM.BlockObjectAssign;
1227}
1228
Mike Stump1851b682009-03-06 04:53:30 +00001229void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Mike Stump797b6322009-03-05 01:23:13 +00001230 llvm::Value *F = getBlockObjectDispose();
Mike Stump1851b682009-03-06 04:53:30 +00001231 llvm::Value *N;
Mike Stump797b6322009-03-05 01:23:13 +00001232 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001233 N = llvm::ConstantInt::get(llvm::Type::getInt32Ty(V->getContext()), flag);
Mike Stump797b6322009-03-05 01:23:13 +00001234 Builder.CreateCall2(F, V, N);
1235}
Mike Stump00470a12009-03-05 08:32:30 +00001236
1237ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stump08920992009-03-07 02:35:30 +00001238
1239BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1240 CGBuilderTy &B)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001241 : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) {
Owen Anderson0032b272009-08-13 21:57:51 +00001242 PtrToInt8Ty = llvm::PointerType::getUnqual(
1243 llvm::Type::getInt8Ty(VMContext));
Mike Stump08920992009-03-07 02:35:30 +00001244
1245 BlockHasCopyDispose = false;
1246}