blob: 2997b21915f5ad686c59f25bdedd25e7f754e4a1 [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 }
113}
114
Mike Stump58a85142009-03-04 22:48:06 +0000115/// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be
116/// declared as a global variable instead of on the stack.
Chris Lattnere2f79b62009-05-13 02:50:56 +0000117static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info) {
Mike Stumpea26cb52009-10-21 03:49:08 +0000118 return Info.DeclRefs.empty();
119}
120
121/// AllocateAllBlockDeclRefs - Preallocate all nested BlockDeclRefExprs to
122/// ensure we can generate the debug information for the parameter for the block
123/// invoke function.
124static void AllocateAllBlockDeclRefs(const CodeGenFunction::BlockInfo &Info,
125 CodeGenFunction *CGF) {
Mike Stumpea26cb52009-10-21 03:49:08 +0000126 // FIXME: Also always forward the this pointer in C++ as well.
127
128 for (size_t i = 0; i < Info.DeclRefs.size(); ++i)
129 CGF->AllocateBlockDecl(Info.DeclRefs[i]);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000130}
131
Mike Stump58a85142009-03-04 22:48:06 +0000132// FIXME: Push most into CGM, passing down a few bits, like current function
133// name.
Mike Stump8a2b4b12009-02-25 23:33:13 +0000134llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Mike Stumpe5fee252009-02-13 16:19:19 +0000135
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000136 std::string Name = CurFn->getName();
137 CodeGenFunction::BlockInfo Info(0, Name.c_str());
Mike Stump38e16272009-10-21 22:01:24 +0000138 llvm::SmallSet<const DeclContext *, 16> InnerContexts;
139 InnerContexts.insert(BE->getBlockDecl());
140 CollectBlockDeclRefInfo(BE->getBody(), Info, InnerContexts);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000141
142 // Check if the block can be global.
Mike Stump58a85142009-03-04 22:48:06 +0000143 // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like
144 // to just have one code path. We should move this function into CGM and pass
145 // CGF, then we can just check to see if CGF is 0.
Mike Stumpdab514f2009-03-04 03:23:46 +0000146 if (0 && CanBlockBeGlobal(Info))
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000147 return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
Mike Stump00470a12009-03-05 08:32:30 +0000148
David Chisnall5e530af2009-11-17 19:33:30 +0000149 size_t BlockFields = 5;
150
David Chisnall5e530af2009-11-17 19:33:30 +0000151 std::vector<llvm::Constant*> Elts(BlockFields);
152
Mike Stumpe5fee252009-02-13 16:19:19 +0000153 llvm::Constant *C;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000154 llvm::Value *V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000155
Mike Stumpe5fee252009-02-13 16:19:19 +0000156 {
157 // C = BuildBlockStructInitlist();
Blaine Garsta36e2232010-03-05 01:29:59 +0000158 unsigned int flags = BLOCK_HAS_SIGNATURE;
David Chisnall5e530af2009-11-17 19:33:30 +0000159
Mike Stump00470a12009-03-05 08:32:30 +0000160 // We run this first so that we set BlockHasCopyDispose from the entire
161 // block literal.
162 // __invoke
Ken Dyck199c3d62010-01-11 17:06:35 +0000163 CharUnits subBlockSize;
Ken Dyck30a8a272010-01-26 19:13:33 +0000164 CharUnits subBlockAlign;
Mike Stump00470a12009-03-05 08:32:30 +0000165 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Mike Stumpa803b0e2009-03-25 17:58:24 +0000166 bool subBlockHasCopyDispose = false;
Mike Stump00470a12009-03-05 08:32:30 +0000167 llvm::Function *Fn
Mike Stumpbb304192009-09-24 22:31:14 +0000168 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl,
169 LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000170 subBlockSize,
Mike Stump00470a12009-03-05 08:32:30 +0000171 subBlockAlign,
172 subBlockDeclRefDecls,
Mike Stumpa803b0e2009-03-25 17:58:24 +0000173 subBlockHasCopyDispose);
174 BlockHasCopyDispose |= subBlockHasCopyDispose;
Mike Stump00470a12009-03-05 08:32:30 +0000175 Elts[3] = Fn;
176
Mike Stumpf5408fe2009-05-16 07:57:57 +0000177 // FIXME: Don't use BlockHasCopyDispose, it is set more often then
178 // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); }
Mike Stumpa803b0e2009-03-25 17:58:24 +0000179 if (subBlockHasCopyDispose)
Mike Stumpe5fee252009-02-13 16:19:19 +0000180 flags |= BLOCK_HAS_COPY_DISPOSE;
181
Mike Stump7d6dc4f2009-02-13 20:17:16 +0000182 // __isa
Mike Stump59c5b112009-02-13 19:29:27 +0000183 C = CGM.getNSConcreteStackBlock();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000184 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
Mike Stump00470a12009-03-05 08:32:30 +0000185 Elts[0] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000186
187 // __flags
Blaine Garsta36e2232010-03-05 01:29:59 +0000188 {
189 QualType BPT = BE->getType();
190 const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>();
191 QualType ResultType = ftype->getResultType();
192
193 CallArgList Args;
194 CodeGenTypes &Types = CGM.getTypes();
195 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000196 FunctionType::ExtInfo());
Blaine Garsta36e2232010-03-05 01:29:59 +0000197 if (CGM.ReturnTypeUsesSret(FnInfo))
198 flags |= BLOCK_USE_STRET;
199 }
Mike Stumpe5fee252009-02-13 16:19:19 +0000200 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
201 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000202 C = llvm::ConstantInt::get(IntTy, flags);
Mike Stump00470a12009-03-05 08:32:30 +0000203 Elts[1] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000204
205 // __reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000206 C = llvm::ConstantInt::get(IntTy, 0);
Mike Stump00470a12009-03-05 08:32:30 +0000207 Elts[2] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000208
Mike Stump8a2b4b12009-02-25 23:33:13 +0000209 if (subBlockDeclRefDecls.size() == 0) {
Mike Stumpcf62d392009-03-06 18:42:23 +0000210 // __descriptor
Blaine Garst2a7eb282010-02-23 21:51:17 +0000211 Elts[4] = BuildDescriptorBlockDecl(BE, subBlockHasCopyDispose, subBlockSize,
Mike Stumpea26cb52009-10-21 03:49:08 +0000212 0, 0);
Mike Stumpcf62d392009-03-06 18:42:23 +0000213
Mike Stump5570cfe2009-03-01 20:07:53 +0000214 // Optimize to being a global block.
215 Elts[0] = CGM.getNSConcreteGlobalBlock();
Blaine Garsta36e2232010-03-05 01:29:59 +0000216
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000217 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
Mike Stump5570cfe2009-03-01 20:07:53 +0000218
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000219 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000220
Owen Anderson1c431b32009-07-08 19:05:04 +0000221 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000222 llvm::GlobalValue::InternalLinkage, C,
223 "__block_holder_tmp_" +
224 llvm::Twine(CGM.getGlobalUniqueCount()));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000225 QualType BPT = BE->getType();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000226 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000227 return C;
228 }
Mike Stump00470a12009-03-05 08:32:30 +0000229
David Chisnall5e530af2009-11-17 19:33:30 +0000230 std::vector<const llvm::Type *> Types(BlockFields+subBlockDeclRefDecls.size());
Mike Stump08920992009-03-07 02:35:30 +0000231 for (int i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000232 Types[i] = Elts[i]->getType();
Mike Stump08920992009-03-07 02:35:30 +0000233 Types[4] = PtrToInt8Ty;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000234
Mike Stumpa99038c2009-02-28 09:07:16 +0000235 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
236 const Expr *E = subBlockDeclRefDecls[i];
237 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
238 QualType Ty = E->getType();
Mike Stumpdab514f2009-03-04 03:23:46 +0000239 if (BDRE && BDRE->isByRef()) {
David Chisnall5e530af2009-11-17 19:33:30 +0000240 Types[i+BlockFields] = llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000241 } else
David Chisnall5e530af2009-11-17 19:33:30 +0000242 Types[i+BlockFields] = ConvertType(Ty);
Mike Stumpa99038c2009-02-28 09:07:16 +0000243 }
Mike Stump8a2b4b12009-02-25 23:33:13 +0000244
Owen Anderson47a434f2009-08-05 23:18:46 +0000245 llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000246
247 llvm::AllocaInst *A = CreateTempAlloca(Ty);
Ken Dyck30a8a272010-01-26 19:13:33 +0000248 A->setAlignment(subBlockAlign.getQuantity());
Mike Stump8a2b4b12009-02-25 23:33:13 +0000249 V = A;
250
Mike Stump08920992009-03-07 02:35:30 +0000251 std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size());
252 int helpersize = 0;
253
254 for (unsigned i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000255 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
Mike Stump00470a12009-03-05 08:32:30 +0000256
Mike Stump8a2b4b12009-02-25 23:33:13 +0000257 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
258 {
Mike Stumpa99038c2009-02-28 09:07:16 +0000259 // FIXME: Push const down.
260 Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
261 DeclRefExpr *DR;
262 ValueDecl *VD;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000263
Mike Stumpa99038c2009-02-28 09:07:16 +0000264 DR = dyn_cast<DeclRefExpr>(E);
265 // Skip padding.
266 if (DR) continue;
267
268 BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
269 VD = BDRE->getDecl();
270
David Chisnall5e530af2009-11-17 19:33:30 +0000271 llvm::Value* Addr = Builder.CreateStructGEP(V, i+BlockFields, "tmp");
Mike Stump08920992009-03-07 02:35:30 +0000272 NoteForHelper[helpersize].index = i+5;
Mike Stump39605b42009-09-22 02:12:52 +0000273 NoteForHelper[helpersize].RequiresCopying
274 = BlockRequiresCopying(VD->getType());
Mike Stump08920992009-03-07 02:35:30 +0000275 NoteForHelper[helpersize].flag
Mike Stump39605b42009-09-22 02:12:52 +0000276 = (VD->getType()->isBlockPointerType()
277 ? BLOCK_FIELD_IS_BLOCK
278 : BLOCK_FIELD_IS_OBJECT);
Mike Stump08920992009-03-07 02:35:30 +0000279
Mike Stumpa99038c2009-02-28 09:07:16 +0000280 if (LocalDeclMap[VD]) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000281 if (BDRE->isByRef()) {
Mike Stump08920992009-03-07 02:35:30 +0000282 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
Mike Stumpf4bc3122009-03-07 06:04:31 +0000283 // FIXME: Someone double check this.
284 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000285 llvm::Value *Loc = LocalDeclMap[VD];
286 Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000287 Loc = Builder.CreateLoad(Loc);
Mike Stumpdab514f2009-03-04 03:23:46 +0000288 Builder.CreateStore(Loc, Addr);
Mike Stump08920992009-03-07 02:35:30 +0000289 ++helpersize;
Mike Stumpdab514f2009-03-04 03:23:46 +0000290 continue;
291 } else
John McCalldbd872f2009-12-08 09:08:17 +0000292 E = new (getContext()) DeclRefExpr (VD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000293 VD->getType(),
294 SourceLocation());
Mike Stumpa99038c2009-02-28 09:07:16 +0000295 }
Mike Stumpdab514f2009-03-04 03:23:46 +0000296 if (BDRE->isByRef()) {
Mike Stumpa8b60c92009-03-21 21:00:35 +0000297 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
298 // FIXME: Someone double check this.
299 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpa99038c2009-02-28 09:07:16 +0000300 E = new (getContext())
301 UnaryOperator(E, UnaryOperator::AddrOf,
302 getContext().getPointerType(E->getType()),
303 SourceLocation());
Mike Stumpdab514f2009-03-04 03:23:46 +0000304 }
Mike Stump08920992009-03-07 02:35:30 +0000305 ++helpersize;
Mike Stumpa99038c2009-02-28 09:07:16 +0000306
Mike Stumpa99038c2009-02-28 09:07:16 +0000307 RValue r = EmitAnyExpr(E, Addr, false);
Mike Stumpdab514f2009-03-04 03:23:46 +0000308 if (r.isScalar()) {
309 llvm::Value *Loc = r.getScalarVal();
David Chisnall5e530af2009-11-17 19:33:30 +0000310 const llvm::Type *Ty = Types[i+BlockFields];
Mike Stumpdab514f2009-03-04 03:23:46 +0000311 if (BDRE->isByRef()) {
Mike Stump58a85142009-03-04 22:48:06 +0000312 // E is now the address of the value field, instead, we want the
313 // address of the actual ByRef struct. We optimize this slightly
314 // compared to gcc by not grabbing the forwarding slot as this must
315 // be done during Block_copy for us, and we can postpone the work
316 // until then.
Ken Dyck199c3d62010-01-11 17:06:35 +0000317 CharUnits offset = BlockDecls[BDRE->getDecl()];
Mike Stump00470a12009-03-05 08:32:30 +0000318
Mike Stump58a85142009-03-04 22:48:06 +0000319 llvm::Value *BlockLiteral = LoadBlockStruct();
Mike Stump00470a12009-03-05 08:32:30 +0000320
Mike Stump58a85142009-03-04 22:48:06 +0000321 Loc = Builder.CreateGEP(BlockLiteral,
Mike Stumpea26cb52009-10-21 03:49:08 +0000322 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Ken Dyck199c3d62010-01-11 17:06:35 +0000323 offset.getQuantity()),
Mike Stump58a85142009-03-04 22:48:06 +0000324 "block.literal");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000325 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000326 Loc = Builder.CreateBitCast(Loc, Ty);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000327 Loc = Builder.CreateLoad(Loc);
Mike Stump58a85142009-03-04 22:48:06 +0000328 // Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000329 }
330 Builder.CreateStore(Loc, Addr);
331 } else if (r.isComplex())
Mike Stump8a2b4b12009-02-25 23:33:13 +0000332 // FIXME: implement
333 ErrorUnsupported(BE, "complex in block literal");
334 else if (r.isAggregate())
335 ; // Already created into the destination
336 else
337 assert (0 && "bad block variable");
338 // FIXME: Ensure that the offset created by the backend for
339 // the struct matches the previously computed offset in BlockDecls.
340 }
Mike Stump08920992009-03-07 02:35:30 +0000341 NoteForHelper.resize(helpersize);
Mike Stumpcf62d392009-03-06 18:42:23 +0000342
343 // __descriptor
Blaine Garst2a7eb282010-02-23 21:51:17 +0000344 llvm::Value *Descriptor = BuildDescriptorBlockDecl(BE,
345 subBlockHasCopyDispose,
Mike Stumpa803b0e2009-03-25 17:58:24 +0000346 subBlockSize, Ty,
Mike Stump08920992009-03-07 02:35:30 +0000347 &NoteForHelper);
348 Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
349 Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
Mike Stumpe5fee252009-02-13 16:19:19 +0000350 }
Mike Stump00470a12009-03-05 08:32:30 +0000351
Mike Stumpbd65cac2009-02-19 01:01:04 +0000352 QualType BPT = BE->getType();
Fariborz Jahanian263c4de2010-02-10 23:34:57 +0000353 V = Builder.CreateBitCast(V, ConvertType(BPT));
354 // See if this is a __weak block variable and the must call objc_read_weak
355 // on it.
356 const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>();
357 QualType RES = ftype->getResultType();
358 if (RES.isObjCGCWeak()) {
359 // Must cast argument to id*
360 const llvm::Type *ObjectPtrTy =
361 ConvertType(CGM.getContext().getObjCIdType());
362 const llvm::Type *PtrObjectPtrTy =
363 llvm::PointerType::getUnqual(ObjectPtrTy);
364 V = Builder.CreateBitCast(V, PtrObjectPtrTy);
365 V = CGM.getObjCRuntime().EmitObjCWeakRead(*this, V);
366 }
367 return V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000368}
369
370
Mike Stump2a998142009-03-04 18:17:45 +0000371const llvm::Type *BlockModule::getBlockDescriptorType() {
Mike Stumpab695142009-02-13 15:16:56 +0000372 if (BlockDescriptorType)
373 return BlockDescriptorType;
374
Mike Stumpa5448542009-02-13 15:32:32 +0000375 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000376 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000377
Mike Stumpab695142009-02-13 15:16:56 +0000378 // struct __block_descriptor {
379 // unsigned long reserved;
380 // unsigned long block_size;
Blaine Garst2a7eb282010-02-23 21:51:17 +0000381 //
382 // // later, the following will be added
383 //
384 // struct {
385 // void (*copyHelper)();
386 // void (*copyHelper)();
387 // } helpers; // !!! optional
388 //
389 // const char *signature; // the block signature
390 // const char *layout; // reserved
Mike Stumpab695142009-02-13 15:16:56 +0000391 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000392 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(),
393 UnsignedLongTy,
Mike Stumpa5448542009-02-13 15:32:32 +0000394 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000395 NULL);
396
397 getModule().addTypeName("struct.__block_descriptor",
398 BlockDescriptorType);
399
400 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000401}
402
Mike Stump2a998142009-03-04 18:17:45 +0000403const llvm::Type *BlockModule::getGenericBlockLiteralType() {
Mike Stump9b8a7972009-02-13 15:25:34 +0000404 if (GenericBlockLiteralType)
405 return GenericBlockLiteralType;
406
Mike Stumpa5448542009-02-13 15:32:32 +0000407 const llvm::Type *BlockDescPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000408 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000409
Mike Stump7cbb3602009-02-13 16:01:35 +0000410 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
411 getTypes().ConvertType(getContext().IntTy));
412
Mike Stump9b8a7972009-02-13 15:25:34 +0000413 // struct __block_literal_generic {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000414 // void *__isa;
415 // int __flags;
416 // int __reserved;
417 // void (*__invoke)(void *);
418 // struct __block_descriptor *__descriptor;
Mike Stump9b8a7972009-02-13 15:25:34 +0000419 // };
Blaine Garst2a7eb282010-02-23 21:51:17 +0000420 GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
Owen Anderson47a434f2009-08-05 23:18:46 +0000421 PtrToInt8Ty,
Mike Stump7cbb3602009-02-13 16:01:35 +0000422 IntTy,
423 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000424 PtrToInt8Ty,
Mike Stump9b8a7972009-02-13 15:25:34 +0000425 BlockDescPtrTy,
426 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000427
Mike Stump9b8a7972009-02-13 15:25:34 +0000428 getModule().addTypeName("struct.__block_literal_generic",
429 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000430
Mike Stump9b8a7972009-02-13 15:25:34 +0000431 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000432}
433
Mike Stumpbd65cac2009-02-19 01:01:04 +0000434
Anders Carlssona1736c02009-12-24 21:13:40 +0000435RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E,
436 ReturnValueSlot ReturnValue) {
Mike Stumpa5448542009-02-13 15:32:32 +0000437 const BlockPointerType *BPT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000438 E->getCallee()->getType()->getAs<BlockPointerType>();
Mike Stumpa5448542009-02-13 15:32:32 +0000439
Anders Carlssonacfde802009-02-12 00:39:25 +0000440 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
441
442 // Get a pointer to the generic block literal.
443 const llvm::Type *BlockLiteralTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000444 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000445
446 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000447 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000448 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
449
450 // Get the function pointer from the literal.
451 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Anders Carlssonacfde802009-02-12 00:39:25 +0000452
Mike Stumpa5448542009-02-13 15:32:32 +0000453 BlockLiteral =
454 Builder.CreateBitCast(BlockLiteral,
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000455 llvm::Type::getInt8PtrTy(VMContext),
Anders Carlssonacfde802009-02-12 00:39:25 +0000456 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000457
Anders Carlssonacfde802009-02-12 00:39:25 +0000458 // Add the block literal.
459 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
460 CallArgList Args;
461 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000462
Anders Carlsson782f3972009-04-08 23:13:16 +0000463 QualType FnType = BPT->getPointeeType();
464
Anders Carlssonacfde802009-02-12 00:39:25 +0000465 // And the rest of the arguments.
John McCall183700f2009-09-21 23:43:11 +0000466 EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(),
Anders Carlsson782f3972009-04-08 23:13:16 +0000467 E->arg_begin(), E->arg_end());
Mike Stumpa5448542009-02-13 15:32:32 +0000468
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000469 // Load the function.
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000470 llvm::Value *Func = Builder.CreateLoad(FuncPtr, "tmp");
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000471
John McCall04a67a62010-02-05 21:31:56 +0000472 const FunctionType *FuncTy = FnType->getAs<FunctionType>();
473 QualType ResultType = FuncTy->getResultType();
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000474
Mike Stump1eb44332009-09-09 15:08:12 +0000475 const CGFunctionInfo &FnInfo =
Rafael Espindola264ba482010-03-30 20:24:48 +0000476 CGM.getTypes().getFunctionInfo(ResultType, Args,
477 FuncTy->getExtInfo());
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000479 // Cast the function pointer to the right type.
Mike Stump1eb44332009-09-09 15:08:12 +0000480 const llvm::Type *BlockFTy =
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000481 CGM.getTypes().GetFunctionType(FnInfo, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Owen Anderson96e0fc72009-07-29 22:16:19 +0000483 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000484 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Anders Carlssonacfde802009-02-12 00:39:25 +0000486 // And call the block.
Anders Carlssona1736c02009-12-24 21:13:40 +0000487 return EmitCall(FnInfo, Func, ReturnValue, Args);
Anders Carlssonacfde802009-02-12 00:39:25 +0000488}
Anders Carlssond5cab542009-02-12 17:55:02 +0000489
Ken Dyck199c3d62010-01-11 17:06:35 +0000490CharUnits CodeGenFunction::AllocateBlockDecl(const BlockDeclRefExpr *E) {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000491 const ValueDecl *VD = E->getDecl();
Ken Dyck199c3d62010-01-11 17:06:35 +0000492 CharUnits &offset = BlockDecls[VD];
Mike Stumpdab514f2009-03-04 03:23:46 +0000493
Mike Stumpdab514f2009-03-04 03:23:46 +0000494 // See if we have already allocated an offset for this variable.
Ken Dyck199c3d62010-01-11 17:06:35 +0000495 if (offset.isPositive())
Mike Stumpea26cb52009-10-21 03:49:08 +0000496 return offset;
497
498 // Don't run the expensive check, unless we have to.
Mike Stump083c25e2009-10-22 00:49:09 +0000499 if (!BlockHasCopyDispose)
500 if (E->isByRef()
501 || BlockRequiresCopying(E->getType()))
502 BlockHasCopyDispose = true;
Mike Stumpea26cb52009-10-21 03:49:08 +0000503
504 // if not, allocate one now.
505 offset = getBlockOffset(E);
506
507 return offset;
508}
509
510llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
511 const ValueDecl *VD = E->getDecl();
Ken Dyck199c3d62010-01-11 17:06:35 +0000512 CharUnits offset = AllocateBlockDecl(E);
Mike Stumpea26cb52009-10-21 03:49:08 +0000513
Mike Stumpdab514f2009-03-04 03:23:46 +0000514
515 llvm::Value *BlockLiteral = LoadBlockStruct();
516 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
Mike Stumpea26cb52009-10-21 03:49:08 +0000517 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Ken Dyck199c3d62010-01-11 17:06:35 +0000518 offset.getQuantity()),
Mike Stump58a85142009-03-04 22:48:06 +0000519 "block.literal");
Mike Stumpdab514f2009-03-04 03:23:46 +0000520 if (E->isByRef()) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000521 const llvm::Type *PtrStructTy
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000522 = llvm::PointerType::get(BuildByRefType(VD), 0);
Mike Stumpa8b60c92009-03-21 21:00:35 +0000523 // The block literal will need a copy/destroy helper.
524 BlockHasCopyDispose = true;
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000525
526 const llvm::Type *Ty = PtrStructTy;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000527 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000528 V = Builder.CreateBitCast(V, Ty);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000529 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000530 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000531 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000532 V = Builder.CreateBitCast(V, PtrStructTy);
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000533 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
534 VD->getNameAsString());
Mike Stumpdab514f2009-03-04 03:23:46 +0000535 } else {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000536 const llvm::Type *Ty = CGM.getTypes().ConvertType(VD->getType());
537
Owen Anderson96e0fc72009-07-29 22:16:19 +0000538 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000539 V = Builder.CreateBitCast(V, Ty);
540 }
541 return V;
542}
543
Mike Stump6cc88f72009-03-20 21:53:12 +0000544void CodeGenFunction::BlockForwardSelf() {
545 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
546 ImplicitParamDecl *SelfDecl = OMD->getSelfDecl();
547 llvm::Value *&DMEntry = LocalDeclMap[SelfDecl];
548 if (DMEntry)
549 return;
550 // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care
551 BlockDeclRefExpr *BDRE = new (getContext())
552 BlockDeclRefExpr(SelfDecl,
553 SelfDecl->getType(), SourceLocation(), false);
554 DMEntry = GetAddrOfBlockDecl(BDRE);
555}
556
Mike Stump67a64482009-02-14 22:16:35 +0000557llvm::Constant *
Mike Stump90a90432009-03-04 18:47:42 +0000558BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000559 // Generate the block descriptor.
560 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000561 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
562 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000563
Blaine Garst2a7eb282010-02-23 21:51:17 +0000564 llvm::Constant *DescriptorFields[4];
Mike Stumpa5448542009-02-13 15:32:32 +0000565
Anders Carlssond5cab542009-02-12 17:55:02 +0000566 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000567 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000568
Anders Carlssond5cab542009-02-12 17:55:02 +0000569 // Block literal size. For global blocks we just use the size of the generic
570 // block literal struct.
Ken Dyck687cc4a2010-01-26 13:48:07 +0000571 CharUnits BlockLiteralSize =
572 CGM.GetTargetTypeStoreSize(getGenericBlockLiteralType());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000573 DescriptorFields[1] =
Ken Dyck199c3d62010-01-11 17:06:35 +0000574 llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize.getQuantity());
Blaine Garst2a7eb282010-02-23 21:51:17 +0000575
576 // signature. non-optional ObjC-style method descriptor @encode sequence
577 std::string BlockTypeEncoding;
578 CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
Mike Stumpa5448542009-02-13 15:32:32 +0000579
Blaine Garst2a7eb282010-02-23 21:51:17 +0000580 DescriptorFields[2] = llvm::ConstantExpr::getBitCast(
581 CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty);
582
583 // layout
584 DescriptorFields[3] =
585 llvm::ConstantInt::get(UnsignedLongTy,0);
586
587 // build the structure from the 4 elements
Mike Stumpa5448542009-02-13 15:32:32 +0000588 llvm::Constant *DescriptorStruct =
Blaine Garst2a7eb282010-02-23 21:51:17 +0000589 llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 4, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000590
Anders Carlssond5cab542009-02-12 17:55:02 +0000591 llvm::GlobalVariable *Descriptor =
Owen Anderson1c431b32009-07-08 19:05:04 +0000592 new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000593 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000594 DescriptorStruct, "__block_descriptor_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000595
David Chisnall5e530af2009-11-17 19:33:30 +0000596 int FieldCount = 5;
Anders Carlssond5cab542009-02-12 17:55:02 +0000597 // Generate the constants for the block literal.
David Chisnall5e530af2009-11-17 19:33:30 +0000598
599 std::vector<llvm::Constant*> LiteralFields(FieldCount);
Mike Stumpa5448542009-02-13 15:32:32 +0000600
Mike Stump67a64482009-02-14 22:16:35 +0000601 CodeGenFunction::BlockInfo Info(0, n);
Ken Dyck199c3d62010-01-11 17:06:35 +0000602 CharUnits subBlockSize;
Ken Dyck30a8a272010-01-26 19:13:33 +0000603 CharUnits subBlockAlign;
Mike Stumpa99038c2009-02-28 09:07:16 +0000604 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Daniel Dunbard67b09a2009-03-12 03:07:24 +0000605 bool subBlockHasCopyDispose = false;
Mike Stump7f28a9c2009-03-13 23:34:28 +0000606 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000607 llvm::Function *Fn
Mike Stump6cc88f72009-03-20 21:53:12 +0000608 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000609 subBlockSize,
Mike Stump90a90432009-03-04 18:47:42 +0000610 subBlockAlign,
Mike Stump00470a12009-03-05 08:32:30 +0000611 subBlockDeclRefDecls,
612 subBlockHasCopyDispose);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000613 assert(subBlockSize == BlockLiteralSize
614 && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000615
Anders Carlssond5cab542009-02-12 17:55:02 +0000616 // isa
Mike Stumpf99f1d02009-02-13 17:23:42 +0000617 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000618
Anders Carlssond5cab542009-02-12 17:55:02 +0000619 // Flags
Blaine Garst2a7eb282010-02-23 21:51:17 +0000620 LiteralFields[1] =
Blaine Garsta36e2232010-03-05 01:29:59 +0000621 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE);
Mike Stumpa5448542009-02-13 15:32:32 +0000622
Anders Carlssond5cab542009-02-12 17:55:02 +0000623 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000624 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000625
Anders Carlssond5cab542009-02-12 17:55:02 +0000626 // Function
627 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000628
Anders Carlssond5cab542009-02-12 17:55:02 +0000629 // Descriptor
630 LiteralFields[4] = Descriptor;
David Chisnall5e530af2009-11-17 19:33:30 +0000631
Mike Stumpa5448542009-02-13 15:32:32 +0000632 llvm::Constant *BlockLiteralStruct =
David Chisnall5e530af2009-11-17 19:33:30 +0000633 llvm::ConstantStruct::get(VMContext, LiteralFields, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000634
635 llvm::GlobalVariable *BlockLiteral =
Owen Anderson1c431b32009-07-08 19:05:04 +0000636 new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000637 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000638 BlockLiteralStruct, "__block_literal_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000639
Anders Carlssond5cab542009-02-12 17:55:02 +0000640 return BlockLiteral;
641}
642
Mike Stump4e7a1f72009-02-21 20:00:35 +0000643llvm::Value *CodeGenFunction::LoadBlockStruct() {
Mike Stumpbf1914b2009-10-20 20:30:01 +0000644 llvm::Value *V = Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()],
645 "self");
646 // For now, we codegen based upon byte offsets.
647 return Builder.CreateBitCast(V, PtrToInt8Ty);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000648}
649
Mike Stump00470a12009-03-05 08:32:30 +0000650llvm::Function *
651CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
652 const BlockInfo& Info,
Mike Stump6cc88f72009-03-20 21:53:12 +0000653 const Decl *OuterFuncDecl,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000654 llvm::DenseMap<const Decl*, llvm::Value*> ldm,
Ken Dyck199c3d62010-01-11 17:06:35 +0000655 CharUnits &Size,
Ken Dyck30a8a272010-01-26 19:13:33 +0000656 CharUnits &Align,
Mike Stump00470a12009-03-05 08:32:30 +0000657 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls,
658 bool &subBlockHasCopyDispose) {
Devang Patel963dfbd2009-04-15 21:51:44 +0000659
660 // Check if we should generate debug info for this block.
661 if (CGM.getDebugInfo())
662 DebugInfo = CGM.getDebugInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000663
Mike Stump7f28a9c2009-03-13 23:34:28 +0000664 // Arrange for local static and local extern declarations to appear
665 // to be local to this function as well, as they are directly referenced
666 // in a block.
667 for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
668 i != ldm.end();
669 ++i) {
670 const VarDecl *VD = dyn_cast<VarDecl>(i->first);
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000672 if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage())
Mike Stump7f28a9c2009-03-13 23:34:28 +0000673 LocalDeclMap[VD] = i->second;
674 }
675
Ken Dyck687cc4a2010-01-26 13:48:07 +0000676 BlockOffset =
677 CGM.GetTargetTypeStoreSize(CGM.getGenericBlockLiteralType());
Ken Dyck30a8a272010-01-26 19:13:33 +0000678 BlockAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Eli Friedman48f91222009-03-28 03:24:54 +0000679
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000680 const FunctionType *BlockFunctionType = BExpr->getFunctionType();
681 QualType ResultType;
Rafael Espindola264ba482010-03-30 20:24:48 +0000682 FunctionType::ExtInfo EInfo = getFunctionExtInfo(*BlockFunctionType);
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000683 bool IsVariadic;
Mike Stump1eb44332009-09-09 15:08:12 +0000684 if (const FunctionProtoType *FTy =
Fariborz Jahanianda0895d2009-04-11 18:54:57 +0000685 dyn_cast<FunctionProtoType>(BlockFunctionType)) {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000686 ResultType = FTy->getResultType();
687 IsVariadic = FTy->isVariadic();
Mike Stumpb3589f42009-07-30 22:28:39 +0000688 } else {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000689 // K&R style block.
690 ResultType = BlockFunctionType->getResultType();
691 IsVariadic = false;
692 }
Mike Stumpa5448542009-02-13 15:32:32 +0000693
Anders Carlssond5cab542009-02-12 17:55:02 +0000694 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000695
Mike Stumpea26cb52009-10-21 03:49:08 +0000696 CurFuncDecl = OuterFuncDecl;
697
Chris Lattner161d36d2009-02-28 19:01:03 +0000698 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlssond5cab542009-02-12 17:55:02 +0000699
Mike Stumpea26cb52009-10-21 03:49:08 +0000700 IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor");
Mike Stumpadaaad32009-10-20 02:12:22 +0000701
Mike Stumpbfbd5df2009-10-21 18:24:18 +0000702 // Allocate all BlockDeclRefDecls, so we can calculate the right ParmTy below.
Mike Stump0298d382009-10-21 22:02:08 +0000703 AllocateAllBlockDeclRefs(Info, this);
Mike Stumpea26cb52009-10-21 03:49:08 +0000704
Mike Stump083c25e2009-10-22 00:49:09 +0000705 QualType ParmTy = getContext().getBlockParmType(BlockHasCopyDispose,
706 BlockDeclRefDecls);
Anders Carlssond5cab542009-02-12 17:55:02 +0000707 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000708 ImplicitParamDecl *SelfDecl =
Anders Carlssond5cab542009-02-12 17:55:02 +0000709 ImplicitParamDecl::Create(getContext(), 0,
Mike Stumpadaaad32009-10-20 02:12:22 +0000710 SourceLocation(), II,
711 ParmTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000712
Anders Carlssond5cab542009-02-12 17:55:02 +0000713 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump4e7a1f72009-02-21 20:00:35 +0000714 BlockStructDecl = SelfDecl;
Mike Stumpa5448542009-02-13 15:32:32 +0000715
Steve Naroffe78b8092009-03-13 16:56:44 +0000716 for (BlockDecl::param_const_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000717 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000718 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000719
720 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +0000721 CGM.getTypes().getFunctionInfo(ResultType, Args, EInfo);
Anders Carlssond5cab542009-02-12 17:55:02 +0000722
Anders Carlssond5cab542009-02-12 17:55:02 +0000723 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000724 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic);
Mike Stumpa5448542009-02-13 15:32:32 +0000725
726 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000727 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000728 llvm::Twine("__") + Info.Name + "_block_invoke_",
Anders Carlssond5cab542009-02-12 17:55:02 +0000729 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000730
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000731 CGM.SetInternalFunctionAttributes(BD, Fn, FI);
732
Chris Lattner4863db42009-04-23 07:18:56 +0000733 StartFunction(BD, ResultType, Fn, Args,
Chris Lattner161d36d2009-02-28 19:01:03 +0000734 BExpr->getBody()->getLocEnd());
Mike Stumpb1a6e682009-09-30 02:43:10 +0000735
Chris Lattner4863db42009-04-23 07:18:56 +0000736 CurFuncDecl = OuterFuncDecl;
Chris Lattnerb5437d22009-04-23 05:30:27 +0000737 CurCodeDecl = BD;
Mike Stumpb289b3f2009-10-01 22:29:41 +0000738
739 // Save a spot to insert the debug information for all the BlockDeclRefDecls.
740 llvm::BasicBlock *entry = Builder.GetInsertBlock();
741 llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint();
742 --entry_ptr;
743
Chris Lattner161d36d2009-02-28 19:01:03 +0000744 EmitStmt(BExpr->getBody());
Mike Stumpb289b3f2009-10-01 22:29:41 +0000745
Mike Stumpde8c5c72009-10-01 00:27:30 +0000746 // Remember where we were...
747 llvm::BasicBlock *resume = Builder.GetInsertBlock();
Mike Stumpb289b3f2009-10-01 22:29:41 +0000748
Mike Stumpde8c5c72009-10-01 00:27:30 +0000749 // Go back to the entry.
Mike Stumpb289b3f2009-10-01 22:29:41 +0000750 ++entry_ptr;
751 Builder.SetInsertPoint(entry, entry_ptr);
752
Mike Stumpb1a6e682009-09-30 02:43:10 +0000753 if (CGDebugInfo *DI = getDebugInfo()) {
Mike Stumpb1a6e682009-09-30 02:43:10 +0000754 // Emit debug information for all the BlockDeclRefDecls.
Chris Lattner14b1a362010-01-25 03:29:35 +0000755 for (unsigned i = 0, e = BlockDeclRefDecls.size(); i != e; ++i) {
756 if (const BlockDeclRefExpr *BDRE =
757 dyn_cast<BlockDeclRefExpr>(BlockDeclRefDecls[i])) {
Mike Stumpb1a6e682009-09-30 02:43:10 +0000758 const ValueDecl *D = BDRE->getDecl();
759 DI->setLocation(D->getLocation());
760 DI->EmitDeclareOfBlockDeclRefVariable(BDRE,
761 LocalDeclMap[getBlockStructDecl()],
762 Builder, this);
763 }
764 }
Mike Stumpb1a6e682009-09-30 02:43:10 +0000765 }
Mike Stumpde8c5c72009-10-01 00:27:30 +0000766 // And resume where we left off.
767 if (resume == 0)
768 Builder.ClearInsertionPoint();
769 else
770 Builder.SetInsertPoint(resume);
Mike Stumpb1a6e682009-09-30 02:43:10 +0000771
Chris Lattner161d36d2009-02-28 19:01:03 +0000772 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlssond5cab542009-02-12 17:55:02 +0000773
Mike Stump8a2b4b12009-02-25 23:33:13 +0000774 // The runtime needs a minimum alignment of a void *.
Ken Dyck30a8a272010-01-26 19:13:33 +0000775 CharUnits MinAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Ken Dyck199c3d62010-01-11 17:06:35 +0000776 BlockOffset = CharUnits::fromQuantity(
Ken Dyck30a8a272010-01-26 19:13:33 +0000777 llvm::RoundUpToAlignment(BlockOffset.getQuantity(),
778 MinAlign.getQuantity()));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000779
Mike Stump4e7a1f72009-02-21 20:00:35 +0000780 Size = BlockOffset;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000781 Align = BlockAlign;
782 subBlockDeclRefDecls = BlockDeclRefDecls;
Mike Stump00470a12009-03-05 08:32:30 +0000783 subBlockHasCopyDispose |= BlockHasCopyDispose;
Anders Carlssond5cab542009-02-12 17:55:02 +0000784 return Fn;
785}
Mike Stumpa99038c2009-02-28 09:07:16 +0000786
Ken Dyck199c3d62010-01-11 17:06:35 +0000787CharUnits BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
Mike Stumpa99038c2009-02-28 09:07:16 +0000788 const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
789
Ken Dyck199c3d62010-01-11 17:06:35 +0000790 CharUnits Size = getContext().getTypeSizeInChars(D->getType());
Ken Dyck8b752f12010-01-27 17:10:57 +0000791 CharUnits Align = getContext().getDeclAlign(D);
Mike Stumpa99038c2009-02-28 09:07:16 +0000792
793 if (BDRE->isByRef()) {
Ken Dyck199c3d62010-01-11 17:06:35 +0000794 Size = getContext().getTypeSizeInChars(getContext().VoidPtrTy);
Ken Dyck30a8a272010-01-26 19:13:33 +0000795 Align = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Mike Stumpa99038c2009-02-28 09:07:16 +0000796 }
797
Ken Dyck30a8a272010-01-26 19:13:33 +0000798 assert ((Align.isPositive()) && "alignment must be 1 byte or more");
Mike Stumpa99038c2009-02-28 09:07:16 +0000799
Ken Dyck199c3d62010-01-11 17:06:35 +0000800 CharUnits OldOffset = BlockOffset;
Mike Stumpa99038c2009-02-28 09:07:16 +0000801
802 // Ensure proper alignment, even if it means we have to have a gap
Ken Dyck199c3d62010-01-11 17:06:35 +0000803 BlockOffset = CharUnits::fromQuantity(
Ken Dyck30a8a272010-01-26 19:13:33 +0000804 llvm::RoundUpToAlignment(BlockOffset.getQuantity(), Align.getQuantity()));
Mike Stumpa99038c2009-02-28 09:07:16 +0000805 BlockAlign = std::max(Align, BlockAlign);
Mike Stump00470a12009-03-05 08:32:30 +0000806
Ken Dyck199c3d62010-01-11 17:06:35 +0000807 CharUnits Pad = BlockOffset - OldOffset;
808 if (Pad.isPositive()) {
809 llvm::ArrayType::get(llvm::Type::getInt8Ty(VMContext), Pad.getQuantity());
Mike Stumpa99038c2009-02-28 09:07:16 +0000810 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
Ken Dyck199c3d62010-01-11 17:06:35 +0000811 llvm::APInt(32,
812 Pad.getQuantity()),
Mike Stumpa99038c2009-02-28 09:07:16 +0000813 ArrayType::Normal, 0);
814 ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
Douglas Gregor16573fa2010-04-19 22:54:31 +0000815 0, QualType(PadTy), 0,
816 VarDecl::None, VarDecl::None);
Mike Stumpa99038c2009-02-28 09:07:16 +0000817 Expr *E;
818 E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
Douglas Gregor0da76df2009-11-23 11:41:28 +0000819 SourceLocation());
Mike Stumpa99038c2009-02-28 09:07:16 +0000820 BlockDeclRefDecls.push_back(E);
821 }
822 BlockDeclRefDecls.push_back(BDRE);
823
824 BlockOffset += Size;
825 return BlockOffset-Size;
826}
Mike Stumpdab514f2009-03-04 03:23:46 +0000827
Mike Stump08920992009-03-07 02:35:30 +0000828llvm::Constant *BlockFunction::
829GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000830 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000831 QualType R = getContext().VoidTy;
832
833 FunctionArgList Args;
834 // FIXME: This leaks
Mike Stump08920992009-03-07 02:35:30 +0000835 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +0000836 ImplicitParamDecl::Create(getContext(), 0,
837 SourceLocation(), 0,
Mike Stump08920992009-03-07 02:35:30 +0000838 getContext().getPointerType(getContext().VoidTy));
839 Args.push_back(std::make_pair(Dst, Dst->getType()));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000840 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +0000841 ImplicitParamDecl::Create(getContext(), 0,
842 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000843 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000844 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000845
Mike Stumpa4f668f2009-03-06 01:33:24 +0000846 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +0000847 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000848
Mike Stump3899a7f2009-06-05 23:26:36 +0000849 // FIXME: We'd like to put these into a mergable by content, with
850 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000851 CodeGenTypes &Types = CGM.getTypes();
852 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
853
854 llvm::Function *Fn =
855 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000856 "__copy_helper_block_", &CGM.getModule());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000857
858 IdentifierInfo *II
859 = &CGM.getContext().Idents.get("__copy_helper_block_");
860
861 FunctionDecl *FD = FunctionDecl::Create(getContext(),
862 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000863 SourceLocation(), II, R, 0,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000864 FunctionDecl::Static,
865 FunctionDecl::None,
866 false,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000867 true);
868 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump08920992009-03-07 02:35:30 +0000869
870 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
871 llvm::Type *PtrPtrT;
Mike Stump08920992009-03-07 02:35:30 +0000872
Mike Stumpb7477cf2009-04-10 18:52:28 +0000873 if (NoteForHelperp) {
874 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump08920992009-03-07 02:35:30 +0000875
Owen Anderson96e0fc72009-07-29 22:16:19 +0000876 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000877 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
878 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump08920992009-03-07 02:35:30 +0000879
Mike Stumpb7477cf2009-04-10 18:52:28 +0000880 llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000881 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000882 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000883 DstObj = Builder.CreateBitCast(DstObj, PtrPtrT);
884 DstObj = Builder.CreateLoad(DstObj);
Mike Stump08920992009-03-07 02:35:30 +0000885
Mike Stumpb7477cf2009-04-10 18:52:28 +0000886 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
887 int flag = NoteForHelper[i].flag;
888 int index = NoteForHelper[i].index;
Mike Stump08920992009-03-07 02:35:30 +0000889
Mike Stumpb7477cf2009-04-10 18:52:28 +0000890 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
891 || NoteForHelper[i].RequiresCopying) {
892 llvm::Value *Srcv = SrcObj;
893 Srcv = Builder.CreateStructGEP(Srcv, index);
894 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000895 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000896 Srcv = Builder.CreateLoad(Srcv);
897
898 llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
899 Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
900
Owen Anderson0032b272009-08-13 21:57:51 +0000901 llvm::Value *N = llvm::ConstantInt::get(
902 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000903 llvm::Value *F = getBlockObjectAssign();
904 Builder.CreateCall3(F, Dstv, Srcv, N);
905 }
Mike Stump08920992009-03-07 02:35:30 +0000906 }
907 }
908
Mike Stumpa4f668f2009-03-06 01:33:24 +0000909 CGF.FinishFunction();
910
Owen Anderson3c4972d2009-07-29 18:54:39 +0000911 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000912}
913
Mike Stumpcf62d392009-03-06 18:42:23 +0000914llvm::Constant *BlockFunction::
Mike Stump08920992009-03-07 02:35:30 +0000915GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
916 const llvm::StructType* T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000917 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000918 QualType R = getContext().VoidTy;
919
920 FunctionArgList Args;
921 // FIXME: This leaks
922 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +0000923 ImplicitParamDecl::Create(getContext(), 0,
924 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000925 getContext().getPointerType(getContext().VoidTy));
926
927 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Mike Stumpa4f668f2009-03-06 01:33:24 +0000929 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +0000930 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000931
Mike Stump3899a7f2009-06-05 23:26:36 +0000932 // FIXME: We'd like to put these into a mergable by content, with
933 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000934 CodeGenTypes &Types = CGM.getTypes();
935 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
936
937 llvm::Function *Fn =
938 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000939 "__destroy_helper_block_", &CGM.getModule());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000940
941 IdentifierInfo *II
942 = &CGM.getContext().Idents.get("__destroy_helper_block_");
943
944 FunctionDecl *FD = FunctionDecl::Create(getContext(),
945 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000946 SourceLocation(), II, R, 0,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000947 FunctionDecl::Static,
948 FunctionDecl::None,
949 false, true);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000950 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1edf6b62009-03-07 02:53:18 +0000951
Mike Stumpb7477cf2009-04-10 18:52:28 +0000952 if (NoteForHelperp) {
953 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump1edf6b62009-03-07 02:53:18 +0000954
Mike Stumpb7477cf2009-04-10 18:52:28 +0000955 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
956 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000957 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000958 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
959 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump1edf6b62009-03-07 02:53:18 +0000960
Mike Stumpb7477cf2009-04-10 18:52:28 +0000961 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
962 int flag = NoteForHelper[i].flag;
963 int index = NoteForHelper[i].index;
Mike Stump1edf6b62009-03-07 02:53:18 +0000964
Mike Stumpb7477cf2009-04-10 18:52:28 +0000965 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
966 || NoteForHelper[i].RequiresCopying) {
967 llvm::Value *Srcv = SrcObj;
968 Srcv = Builder.CreateStructGEP(Srcv, index);
969 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000970 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000971 Srcv = Builder.CreateLoad(Srcv);
972
973 BuildBlockRelease(Srcv, flag);
974 }
Mike Stump1edf6b62009-03-07 02:53:18 +0000975 }
976 }
977
Mike Stumpa4f668f2009-03-06 01:33:24 +0000978 CGF.FinishFunction();
979
Owen Anderson3c4972d2009-07-29 18:54:39 +0000980 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000981}
982
Mike Stump08920992009-03-07 02:35:30 +0000983llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000984 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump08920992009-03-07 02:35:30 +0000985 return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
986 T, NoteForHelper);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000987}
988
Mike Stump08920992009-03-07 02:35:30 +0000989llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000990 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stump08920992009-03-07 02:35:30 +0000991 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000992 T, NoteForHelperp);
Mike Stumpdab514f2009-03-04 03:23:46 +0000993}
Mike Stump797b6322009-03-05 01:23:13 +0000994
Mike Stumpee094222009-03-06 06:12:24 +0000995llvm::Constant *BlockFunction::
996GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
Mike Stump45031c02009-03-06 02:29:21 +0000997 QualType R = getContext().VoidTy;
998
999 FunctionArgList Args;
1000 // FIXME: This leaks
Mike Stumpee094222009-03-06 06:12:24 +00001001 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +00001002 ImplicitParamDecl::Create(getContext(), 0,
1003 SourceLocation(), 0,
Mike Stumpee094222009-03-06 06:12:24 +00001004 getContext().getPointerType(getContext().VoidTy));
1005 Args.push_back(std::make_pair(Dst, Dst->getType()));
1006
1007 // FIXME: This leaks
Mike Stump45031c02009-03-06 02:29:21 +00001008 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001009 ImplicitParamDecl::Create(getContext(), 0,
1010 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001011 getContext().getPointerType(getContext().VoidTy));
Mike Stump45031c02009-03-06 02:29:21 +00001012 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Mike Stump45031c02009-03-06 02:29:21 +00001014 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +00001015 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stump45031c02009-03-06 02:29:21 +00001016
Mike Stump45031c02009-03-06 02:29:21 +00001017 CodeGenTypes &Types = CGM.getTypes();
1018 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1019
Mike Stump3899a7f2009-06-05 23:26:36 +00001020 // FIXME: We'd like to put these into a mergable by content, with
1021 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001022 llvm::Function *Fn =
1023 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001024 "__Block_byref_id_object_copy_", &CGM.getModule());
Mike Stump45031c02009-03-06 02:29:21 +00001025
1026 IdentifierInfo *II
1027 = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
1028
1029 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1030 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001031 SourceLocation(), II, R, 0,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001032 FunctionDecl::Static,
1033 FunctionDecl::None,
1034 false, true);
Mike Stump45031c02009-03-06 02:29:21 +00001035 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpee094222009-03-06 06:12:24 +00001036
1037 // dst->x
1038 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001039 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001040 V = Builder.CreateLoad(V);
Mike Stumpee094222009-03-06 06:12:24 +00001041 V = Builder.CreateStructGEP(V, 6, "x");
1042 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
1043
1044 // src->x
1045 V = CGF.GetAddrOfLocalVar(Src);
1046 V = Builder.CreateLoad(V);
1047 V = Builder.CreateBitCast(V, T);
1048 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001049 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpee094222009-03-06 06:12:24 +00001050 llvm::Value *SrcObj = Builder.CreateLoad(V);
Mike Stump1eb44332009-09-09 15:08:12 +00001051
Mike Stumpee094222009-03-06 06:12:24 +00001052 flag |= BLOCK_BYREF_CALLER;
1053
Owen Anderson0032b272009-08-13 21:57:51 +00001054 llvm::Value *N = llvm::ConstantInt::get(
1055 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpee094222009-03-06 06:12:24 +00001056 llvm::Value *F = getBlockObjectAssign();
1057 Builder.CreateCall3(F, DstObj, SrcObj, N);
1058
Mike Stump45031c02009-03-06 02:29:21 +00001059 CGF.FinishFunction();
1060
Owen Anderson3c4972d2009-07-29 18:54:39 +00001061 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001062}
1063
Mike Stump1851b682009-03-06 04:53:30 +00001064llvm::Constant *
1065BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
1066 int flag) {
Mike Stump45031c02009-03-06 02:29:21 +00001067 QualType R = getContext().VoidTy;
1068
1069 FunctionArgList Args;
1070 // FIXME: This leaks
1071 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001072 ImplicitParamDecl::Create(getContext(), 0,
1073 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001074 getContext().getPointerType(getContext().VoidTy));
1075
1076 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001077
Mike Stump45031c02009-03-06 02:29:21 +00001078 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +00001079 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stump45031c02009-03-06 02:29:21 +00001080
Mike Stump45031c02009-03-06 02:29:21 +00001081 CodeGenTypes &Types = CGM.getTypes();
1082 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1083
Mike Stump3899a7f2009-06-05 23:26:36 +00001084 // FIXME: We'd like to put these into a mergable by content, with
1085 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001086 llvm::Function *Fn =
1087 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001088 "__Block_byref_id_object_dispose_",
Mike Stump45031c02009-03-06 02:29:21 +00001089 &CGM.getModule());
1090
1091 IdentifierInfo *II
1092 = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
1093
1094 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1095 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001096 SourceLocation(), II, R, 0,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001097 FunctionDecl::Static,
1098 FunctionDecl::None,
1099 false, true);
Mike Stump45031c02009-03-06 02:29:21 +00001100 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1851b682009-03-06 04:53:30 +00001101
1102 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001103 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001104 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001105 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001106 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001107 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001108
Mike Stump1851b682009-03-06 04:53:30 +00001109 flag |= BLOCK_BYREF_CALLER;
1110 BuildBlockRelease(V, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001111 CGF.FinishFunction();
1112
Owen Anderson3c4972d2009-07-29 18:54:39 +00001113 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001114}
1115
Mike Stumpee094222009-03-06 06:12:24 +00001116llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001117 int Flag, unsigned Align) {
1118 // All alignments below that of pointer alignment collapse down to just
Mike Stump3899a7f2009-06-05 23:26:36 +00001119 // pointer alignment, as we always have at least that much alignment to begin
1120 // with.
1121 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001122
Mike Stump3899a7f2009-06-05 23:26:36 +00001123 // As an optimization, we only generate a single function of each kind we
1124 // might need. We need a different one for each alignment and for each
1125 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001126 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1127 llvm::Constant *&Entry = CGM.AssignCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001128 if (Entry)
1129 return Entry;
Chris Lattner10976d92009-12-05 08:21:30 +00001130 return Entry = CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, Flag);
Mike Stump45031c02009-03-06 02:29:21 +00001131}
1132
Mike Stump1851b682009-03-06 04:53:30 +00001133llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001134 int Flag,
Mike Stump3899a7f2009-06-05 23:26:36 +00001135 unsigned Align) {
1136 // All alignments below that of pointer alignment collpase down to just
1137 // pointer alignment, as we always have at least that much alignment to begin
1138 // with.
1139 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001140
Mike Stump3899a7f2009-06-05 23:26:36 +00001141 // As an optimization, we only generate a single function of each kind we
1142 // might need. We need a different one for each alignment and for each
1143 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001144 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1145 llvm::Constant *&Entry = CGM.DestroyCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001146 if (Entry)
1147 return Entry;
Chris Lattner10976d92009-12-05 08:21:30 +00001148 return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, Flag);
Mike Stump45031c02009-03-06 02:29:21 +00001149}
1150
Mike Stump797b6322009-03-05 01:23:13 +00001151llvm::Value *BlockFunction::getBlockObjectDispose() {
1152 if (CGM.BlockObjectDispose == 0) {
1153 const llvm::FunctionType *FTy;
1154 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001155 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stump797b6322009-03-05 01:23:13 +00001156 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001157 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson96e0fc72009-07-29 22:16:19 +00001158 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stump00470a12009-03-05 08:32:30 +00001159 CGM.BlockObjectDispose
Mike Stump797b6322009-03-05 01:23:13 +00001160 = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
1161 }
1162 return CGM.BlockObjectDispose;
1163}
1164
Mike Stumpee094222009-03-06 06:12:24 +00001165llvm::Value *BlockFunction::getBlockObjectAssign() {
1166 if (CGM.BlockObjectAssign == 0) {
1167 const llvm::FunctionType *FTy;
1168 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001169 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stumpee094222009-03-06 06:12:24 +00001170 ArgTys.push_back(PtrToInt8Ty);
1171 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001172 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson96e0fc72009-07-29 22:16:19 +00001173 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stumpee094222009-03-06 06:12:24 +00001174 CGM.BlockObjectAssign
1175 = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
1176 }
1177 return CGM.BlockObjectAssign;
1178}
1179
Mike Stump1851b682009-03-06 04:53:30 +00001180void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Mike Stump797b6322009-03-05 01:23:13 +00001181 llvm::Value *F = getBlockObjectDispose();
Mike Stump1851b682009-03-06 04:53:30 +00001182 llvm::Value *N;
Mike Stump797b6322009-03-05 01:23:13 +00001183 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001184 N = llvm::ConstantInt::get(llvm::Type::getInt32Ty(V->getContext()), flag);
Mike Stump797b6322009-03-05 01:23:13 +00001185 Builder.CreateCall2(F, V, N);
1186}
Mike Stump00470a12009-03-05 08:32:30 +00001187
1188ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stump08920992009-03-07 02:35:30 +00001189
1190BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1191 CGBuilderTy &B)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001192 : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) {
Owen Anderson0032b272009-08-13 21:57:51 +00001193 PtrToInt8Ty = llvm::PointerType::getUnqual(
1194 llvm::Type::getInt8Ty(VMContext));
Mike Stump08920992009-03-07 02:35:30 +00001195
1196 BlockHasCopyDispose = false;
1197}