blob: c10a401d8abfa0b4e52ffab27168860a3aa498bc [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"
Anders Carlssond5cab542009-02-12 17:55:02 +000020#include "llvm/Target/TargetData.h"
Anders Carlssonacfde802009-02-12 00:39:25 +000021#include <algorithm>
Torok Edwinf42e4a62009-08-24 13:25:12 +000022
Anders Carlssonacfde802009-02-12 00:39:25 +000023using namespace clang;
24using namespace CodeGen;
25
Mike Stumpcf62d392009-03-06 18:42:23 +000026llvm::Constant *CodeGenFunction::
Blaine Garst2a7eb282010-02-23 21:51:17 +000027BuildDescriptorBlockDecl(const BlockExpr *BE, bool BlockHasCopyDispose, CharUnits Size,
Mike Stumpa803b0e2009-03-25 17:58:24 +000028 const llvm::StructType* Ty,
Mike Stump08920992009-03-07 02:35:30 +000029 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump56129b12009-02-13 16:55:51 +000030 const llvm::Type *UnsignedLongTy
31 = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpe5fee252009-02-13 16:19:19 +000032 llvm::Constant *C;
33 std::vector<llvm::Constant*> Elts;
34
35 // reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +000036 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpe5fee252009-02-13 16:19:19 +000037 Elts.push_back(C);
38
39 // Size
Mike Stumpd6840002009-02-21 20:07:44 +000040 // FIXME: What is the right way to say this doesn't fit? We should give
41 // a user diagnostic in that case. Better fix would be to change the
42 // API to size_t.
Ken Dyck199c3d62010-01-11 17:06:35 +000043 C = llvm::ConstantInt::get(UnsignedLongTy, Size.getQuantity());
Mike Stumpe5fee252009-02-13 16:19:19 +000044 Elts.push_back(C);
45
Blaine Garst2a7eb282010-02-23 21:51:17 +000046 // optional copy/dispose helpers
Mike Stumpe5fee252009-02-13 16:19:19 +000047 if (BlockHasCopyDispose) {
48 // copy_func_helper_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000049 Elts.push_back(BuildCopyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000050
51 // destroy_func_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000052 Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000053 }
54
Blaine Garst2a7eb282010-02-23 21:51:17 +000055 // Signature. non-optional ObjC-style method descriptor @encode sequence
56 std::string BlockTypeEncoding;
57 CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
58
59 Elts.push_back(llvm::ConstantExpr::getBitCast(
60 CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty));
61
62 // Layout.
63 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
64 Elts.push_back(C);
65
Nick Lewycky0d36dd22009-09-19 20:00:52 +000066 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stumpe5fee252009-02-13 16:19:19 +000067
Owen Anderson1c431b32009-07-08 19:05:04 +000068 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Mike Stumpe5fee252009-02-13 16:19:19 +000069 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +000070 C, "__block_descriptor_tmp");
Mike Stumpe5fee252009-02-13 16:19:19 +000071 return C;
72}
73
Mike Stump2a998142009-03-04 18:17:45 +000074llvm::Constant *BlockModule::getNSConcreteGlobalBlock() {
Chris Lattnere2f79b62009-05-13 02:50:56 +000075 if (NSConcreteGlobalBlock == 0)
Mike Stump1eb44332009-09-09 15:08:12 +000076 NSConcreteGlobalBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
Chris Lattnere2f79b62009-05-13 02:50:56 +000077 "_NSConcreteGlobalBlock");
Mike Stumpf99f1d02009-02-13 17:23:42 +000078 return NSConcreteGlobalBlock;
79}
80
Mike Stump2a998142009-03-04 18:17:45 +000081llvm::Constant *BlockModule::getNSConcreteStackBlock() {
Chris Lattnere2f79b62009-05-13 02:50:56 +000082 if (NSConcreteStackBlock == 0)
Mike Stump1eb44332009-09-09 15:08:12 +000083 NSConcreteStackBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
Chris Lattnere2f79b62009-05-13 02:50:56 +000084 "_NSConcreteStackBlock");
Mike Stump59c5b112009-02-13 19:29:27 +000085 return NSConcreteStackBlock;
86}
87
Mike Stump38e16272009-10-21 22:01:24 +000088static void CollectBlockDeclRefInfo(
89 const Stmt *S, CodeGenFunction::BlockInfo &Info,
90 llvm::SmallSet<const DeclContext *, 16> &InnerContexts) {
Anders Carlsson4de9fce2009-03-01 01:09:12 +000091 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
92 I != E; ++I)
Daniel Dunbar82573ee2009-03-02 07:00:57 +000093 if (*I)
Mike Stump38e16272009-10-21 22:01:24 +000094 CollectBlockDeclRefInfo(*I, Info, InnerContexts);
Mike Stump00470a12009-03-05 08:32:30 +000095
Mike Stumpea26cb52009-10-21 03:49:08 +000096 // We want to ensure we walk down into block literals so we can find
97 // all nested BlockDeclRefExprs.
Mike Stump38e16272009-10-21 22:01:24 +000098 if (const BlockExpr *BE = dyn_cast<BlockExpr>(S)) {
99 InnerContexts.insert(cast<DeclContext>(BE->getBlockDecl()));
100 CollectBlockDeclRefInfo(BE->getBody(), Info, InnerContexts);
101 }
Mike Stumpea26cb52009-10-21 03:49:08 +0000102
Mike Stump38e16272009-10-21 22:01:24 +0000103 if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) {
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000104 // FIXME: Handle enums.
Mike Stump38e16272009-10-21 22:01:24 +0000105 if (isa<FunctionDecl>(BDRE->getDecl()))
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000106 return;
Mike Stump00470a12009-03-05 08:32:30 +0000107
Mike Stump38e16272009-10-21 22:01:24 +0000108 // Only Decls that escape are added.
109 if (!InnerContexts.count(BDRE->getDecl()->getDeclContext()))
110 Info.DeclRefs.push_back(BDRE);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000111 }
112}
113
Mike Stump58a85142009-03-04 22:48:06 +0000114/// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be
115/// declared as a global variable instead of on the stack.
Chris Lattnere2f79b62009-05-13 02:50:56 +0000116static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info) {
Mike Stumpea26cb52009-10-21 03:49:08 +0000117 return Info.DeclRefs.empty();
118}
119
120/// AllocateAllBlockDeclRefs - Preallocate all nested BlockDeclRefExprs to
121/// ensure we can generate the debug information for the parameter for the block
122/// invoke function.
123static void AllocateAllBlockDeclRefs(const CodeGenFunction::BlockInfo &Info,
124 CodeGenFunction *CGF) {
Mike Stumpea26cb52009-10-21 03:49:08 +0000125 // FIXME: Also always forward the this pointer in C++ as well.
126
127 for (size_t i = 0; i < Info.DeclRefs.size(); ++i)
128 CGF->AllocateBlockDecl(Info.DeclRefs[i]);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000129}
130
Mike Stump58a85142009-03-04 22:48:06 +0000131// FIXME: Push most into CGM, passing down a few bits, like current function
132// name.
Mike Stump8a2b4b12009-02-25 23:33:13 +0000133llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Mike Stumpe5fee252009-02-13 16:19:19 +0000134
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000135 std::string Name = CurFn->getName();
136 CodeGenFunction::BlockInfo Info(0, Name.c_str());
Mike Stump38e16272009-10-21 22:01:24 +0000137 llvm::SmallSet<const DeclContext *, 16> InnerContexts;
138 InnerContexts.insert(BE->getBlockDecl());
139 CollectBlockDeclRefInfo(BE->getBody(), Info, InnerContexts);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000140
141 // Check if the block can be global.
Mike Stump58a85142009-03-04 22:48:06 +0000142 // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like
143 // to just have one code path. We should move this function into CGM and pass
144 // CGF, then we can just check to see if CGF is 0.
Mike Stumpdab514f2009-03-04 03:23:46 +0000145 if (0 && CanBlockBeGlobal(Info))
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000146 return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
Mike Stump00470a12009-03-05 08:32:30 +0000147
David Chisnall5e530af2009-11-17 19:33:30 +0000148 size_t BlockFields = 5;
149
David Chisnall5e530af2009-11-17 19:33:30 +0000150 std::vector<llvm::Constant*> Elts(BlockFields);
151
Mike Stumpe5fee252009-02-13 16:19:19 +0000152 llvm::Constant *C;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000153 llvm::Value *V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000154
Mike Stumpe5fee252009-02-13 16:19:19 +0000155 {
156 // C = BuildBlockStructInitlist();
Blaine Garsta36e2232010-03-05 01:29:59 +0000157 unsigned int flags = BLOCK_HAS_SIGNATURE;
David Chisnall5e530af2009-11-17 19:33:30 +0000158
Mike Stump00470a12009-03-05 08:32:30 +0000159 // We run this first so that we set BlockHasCopyDispose from the entire
160 // block literal.
161 // __invoke
Ken Dyck199c3d62010-01-11 17:06:35 +0000162 CharUnits subBlockSize;
Ken Dyck30a8a272010-01-26 19:13:33 +0000163 CharUnits subBlockAlign;
Mike Stump00470a12009-03-05 08:32:30 +0000164 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Mike Stumpa803b0e2009-03-25 17:58:24 +0000165 bool subBlockHasCopyDispose = false;
Mike Stump00470a12009-03-05 08:32:30 +0000166 llvm::Function *Fn
Mike Stumpbb304192009-09-24 22:31:14 +0000167 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl,
168 LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000169 subBlockSize,
Mike Stump00470a12009-03-05 08:32:30 +0000170 subBlockAlign,
171 subBlockDeclRefDecls,
Mike Stumpa803b0e2009-03-25 17:58:24 +0000172 subBlockHasCopyDispose);
173 BlockHasCopyDispose |= subBlockHasCopyDispose;
Mike Stump00470a12009-03-05 08:32:30 +0000174 Elts[3] = Fn;
175
Mike Stumpf5408fe2009-05-16 07:57:57 +0000176 // FIXME: Don't use BlockHasCopyDispose, it is set more often then
177 // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); }
Mike Stumpa803b0e2009-03-25 17:58:24 +0000178 if (subBlockHasCopyDispose)
Mike Stumpe5fee252009-02-13 16:19:19 +0000179 flags |= BLOCK_HAS_COPY_DISPOSE;
180
Mike Stump7d6dc4f2009-02-13 20:17:16 +0000181 // __isa
Mike Stump59c5b112009-02-13 19:29:27 +0000182 C = CGM.getNSConcreteStackBlock();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000183 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
Mike Stump00470a12009-03-05 08:32:30 +0000184 Elts[0] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000185
186 // __flags
Blaine Garsta36e2232010-03-05 01:29:59 +0000187 {
188 QualType BPT = BE->getType();
189 const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>();
190 QualType ResultType = ftype->getResultType();
191
192 CallArgList Args;
193 CodeGenTypes &Types = CGM.getTypes();
194 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, Args,
195 CC_Default, false);
196 if (CGM.ReturnTypeUsesSret(FnInfo))
197 flags |= BLOCK_USE_STRET;
198 }
Mike Stumpe5fee252009-02-13 16:19:19 +0000199 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
200 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000201 C = llvm::ConstantInt::get(IntTy, flags);
Mike Stump00470a12009-03-05 08:32:30 +0000202 Elts[1] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000203
204 // __reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000205 C = llvm::ConstantInt::get(IntTy, 0);
Mike Stump00470a12009-03-05 08:32:30 +0000206 Elts[2] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000207
Mike Stump8a2b4b12009-02-25 23:33:13 +0000208 if (subBlockDeclRefDecls.size() == 0) {
Mike Stumpcf62d392009-03-06 18:42:23 +0000209 // __descriptor
Blaine Garst2a7eb282010-02-23 21:51:17 +0000210 Elts[4] = BuildDescriptorBlockDecl(BE, subBlockHasCopyDispose, subBlockSize,
Mike Stumpea26cb52009-10-21 03:49:08 +0000211 0, 0);
Mike Stumpcf62d392009-03-06 18:42:23 +0000212
Mike Stump5570cfe2009-03-01 20:07:53 +0000213 // Optimize to being a global block.
214 Elts[0] = CGM.getNSConcreteGlobalBlock();
Blaine Garsta36e2232010-03-05 01:29:59 +0000215
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000216 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
Mike Stump5570cfe2009-03-01 20:07:53 +0000217
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000218 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000219
Owen Anderson1c431b32009-07-08 19:05:04 +0000220 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000221 llvm::GlobalValue::InternalLinkage, C,
222 "__block_holder_tmp_" +
223 llvm::Twine(CGM.getGlobalUniqueCount()));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000224 QualType BPT = BE->getType();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000225 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000226 return C;
227 }
Mike Stump00470a12009-03-05 08:32:30 +0000228
David Chisnall5e530af2009-11-17 19:33:30 +0000229 std::vector<const llvm::Type *> Types(BlockFields+subBlockDeclRefDecls.size());
Mike Stump08920992009-03-07 02:35:30 +0000230 for (int i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000231 Types[i] = Elts[i]->getType();
Mike Stump08920992009-03-07 02:35:30 +0000232 Types[4] = PtrToInt8Ty;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000233
Mike Stumpa99038c2009-02-28 09:07:16 +0000234 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
235 const Expr *E = subBlockDeclRefDecls[i];
236 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
237 QualType Ty = E->getType();
Mike Stumpdab514f2009-03-04 03:23:46 +0000238 if (BDRE && BDRE->isByRef()) {
David Chisnall5e530af2009-11-17 19:33:30 +0000239 Types[i+BlockFields] = llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000240 } else
David Chisnall5e530af2009-11-17 19:33:30 +0000241 Types[i+BlockFields] = ConvertType(Ty);
Mike Stumpa99038c2009-02-28 09:07:16 +0000242 }
Mike Stump8a2b4b12009-02-25 23:33:13 +0000243
Owen Anderson47a434f2009-08-05 23:18:46 +0000244 llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000245
246 llvm::AllocaInst *A = CreateTempAlloca(Ty);
Ken Dyck30a8a272010-01-26 19:13:33 +0000247 A->setAlignment(subBlockAlign.getQuantity());
Mike Stump8a2b4b12009-02-25 23:33:13 +0000248 V = A;
249
Mike Stump08920992009-03-07 02:35:30 +0000250 std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size());
251 int helpersize = 0;
252
253 for (unsigned i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000254 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
Mike Stump00470a12009-03-05 08:32:30 +0000255
Mike Stump8a2b4b12009-02-25 23:33:13 +0000256 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
257 {
Mike Stumpa99038c2009-02-28 09:07:16 +0000258 // FIXME: Push const down.
259 Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
260 DeclRefExpr *DR;
261 ValueDecl *VD;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000262
Mike Stumpa99038c2009-02-28 09:07:16 +0000263 DR = dyn_cast<DeclRefExpr>(E);
264 // Skip padding.
265 if (DR) continue;
266
267 BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
268 VD = BDRE->getDecl();
269
David Chisnall5e530af2009-11-17 19:33:30 +0000270 llvm::Value* Addr = Builder.CreateStructGEP(V, i+BlockFields, "tmp");
Mike Stump08920992009-03-07 02:35:30 +0000271 NoteForHelper[helpersize].index = i+5;
Mike Stump39605b42009-09-22 02:12:52 +0000272 NoteForHelper[helpersize].RequiresCopying
273 = BlockRequiresCopying(VD->getType());
Mike Stump08920992009-03-07 02:35:30 +0000274 NoteForHelper[helpersize].flag
Mike Stump39605b42009-09-22 02:12:52 +0000275 = (VD->getType()->isBlockPointerType()
276 ? BLOCK_FIELD_IS_BLOCK
277 : BLOCK_FIELD_IS_OBJECT);
Mike Stump08920992009-03-07 02:35:30 +0000278
Mike Stumpa99038c2009-02-28 09:07:16 +0000279 if (LocalDeclMap[VD]) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000280 if (BDRE->isByRef()) {
Mike Stump08920992009-03-07 02:35:30 +0000281 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
Mike Stumpf4bc3122009-03-07 06:04:31 +0000282 // FIXME: Someone double check this.
283 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000284 llvm::Value *Loc = LocalDeclMap[VD];
285 Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000286 Loc = Builder.CreateLoad(Loc);
Mike Stumpdab514f2009-03-04 03:23:46 +0000287 Builder.CreateStore(Loc, Addr);
Mike Stump08920992009-03-07 02:35:30 +0000288 ++helpersize;
Mike Stumpdab514f2009-03-04 03:23:46 +0000289 continue;
290 } else
John McCalldbd872f2009-12-08 09:08:17 +0000291 E = new (getContext()) DeclRefExpr (VD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000292 VD->getType(),
293 SourceLocation());
Mike Stumpa99038c2009-02-28 09:07:16 +0000294 }
Mike Stumpdab514f2009-03-04 03:23:46 +0000295 if (BDRE->isByRef()) {
Mike Stumpa8b60c92009-03-21 21:00:35 +0000296 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
297 // FIXME: Someone double check this.
298 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpa99038c2009-02-28 09:07:16 +0000299 E = new (getContext())
300 UnaryOperator(E, UnaryOperator::AddrOf,
301 getContext().getPointerType(E->getType()),
302 SourceLocation());
Mike Stumpdab514f2009-03-04 03:23:46 +0000303 }
Mike Stump08920992009-03-07 02:35:30 +0000304 ++helpersize;
Mike Stumpa99038c2009-02-28 09:07:16 +0000305
Mike Stumpa99038c2009-02-28 09:07:16 +0000306 RValue r = EmitAnyExpr(E, Addr, false);
Mike Stumpdab514f2009-03-04 03:23:46 +0000307 if (r.isScalar()) {
308 llvm::Value *Loc = r.getScalarVal();
David Chisnall5e530af2009-11-17 19:33:30 +0000309 const llvm::Type *Ty = Types[i+BlockFields];
Mike Stumpdab514f2009-03-04 03:23:46 +0000310 if (BDRE->isByRef()) {
Mike Stump58a85142009-03-04 22:48:06 +0000311 // E is now the address of the value field, instead, we want the
312 // address of the actual ByRef struct. We optimize this slightly
313 // compared to gcc by not grabbing the forwarding slot as this must
314 // be done during Block_copy for us, and we can postpone the work
315 // until then.
Ken Dyck199c3d62010-01-11 17:06:35 +0000316 CharUnits offset = BlockDecls[BDRE->getDecl()];
Mike Stump00470a12009-03-05 08:32:30 +0000317
Mike Stump58a85142009-03-04 22:48:06 +0000318 llvm::Value *BlockLiteral = LoadBlockStruct();
Mike Stump00470a12009-03-05 08:32:30 +0000319
Mike Stump58a85142009-03-04 22:48:06 +0000320 Loc = Builder.CreateGEP(BlockLiteral,
Mike Stumpea26cb52009-10-21 03:49:08 +0000321 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Ken Dyck199c3d62010-01-11 17:06:35 +0000322 offset.getQuantity()),
Mike Stump58a85142009-03-04 22:48:06 +0000323 "block.literal");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000324 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000325 Loc = Builder.CreateBitCast(Loc, Ty);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000326 Loc = Builder.CreateLoad(Loc);
Mike Stump58a85142009-03-04 22:48:06 +0000327 // Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000328 }
329 Builder.CreateStore(Loc, Addr);
330 } else if (r.isComplex())
Mike Stump8a2b4b12009-02-25 23:33:13 +0000331 // FIXME: implement
332 ErrorUnsupported(BE, "complex in block literal");
333 else if (r.isAggregate())
334 ; // Already created into the destination
335 else
336 assert (0 && "bad block variable");
337 // FIXME: Ensure that the offset created by the backend for
338 // the struct matches the previously computed offset in BlockDecls.
339 }
Mike Stump08920992009-03-07 02:35:30 +0000340 NoteForHelper.resize(helpersize);
Mike Stumpcf62d392009-03-06 18:42:23 +0000341
342 // __descriptor
Blaine Garst2a7eb282010-02-23 21:51:17 +0000343 llvm::Value *Descriptor = BuildDescriptorBlockDecl(BE,
344 subBlockHasCopyDispose,
Mike Stumpa803b0e2009-03-25 17:58:24 +0000345 subBlockSize, Ty,
Mike Stump08920992009-03-07 02:35:30 +0000346 &NoteForHelper);
347 Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
348 Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
Mike Stumpe5fee252009-02-13 16:19:19 +0000349 }
Mike Stump00470a12009-03-05 08:32:30 +0000350
Mike Stumpbd65cac2009-02-19 01:01:04 +0000351 QualType BPT = BE->getType();
Fariborz Jahanian263c4de2010-02-10 23:34:57 +0000352 V = Builder.CreateBitCast(V, ConvertType(BPT));
353 // See if this is a __weak block variable and the must call objc_read_weak
354 // on it.
355 const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>();
356 QualType RES = ftype->getResultType();
357 if (RES.isObjCGCWeak()) {
358 // Must cast argument to id*
359 const llvm::Type *ObjectPtrTy =
360 ConvertType(CGM.getContext().getObjCIdType());
361 const llvm::Type *PtrObjectPtrTy =
362 llvm::PointerType::getUnqual(ObjectPtrTy);
363 V = Builder.CreateBitCast(V, PtrObjectPtrTy);
364 V = CGM.getObjCRuntime().EmitObjCWeakRead(*this, V);
365 }
366 return V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000367}
368
369
Mike Stump2a998142009-03-04 18:17:45 +0000370const llvm::Type *BlockModule::getBlockDescriptorType() {
Mike Stumpab695142009-02-13 15:16:56 +0000371 if (BlockDescriptorType)
372 return BlockDescriptorType;
373
Mike Stumpa5448542009-02-13 15:32:32 +0000374 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000375 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000376
Mike Stumpab695142009-02-13 15:16:56 +0000377 // struct __block_descriptor {
378 // unsigned long reserved;
379 // unsigned long block_size;
Blaine Garst2a7eb282010-02-23 21:51:17 +0000380 //
381 // // later, the following will be added
382 //
383 // struct {
384 // void (*copyHelper)();
385 // void (*copyHelper)();
386 // } helpers; // !!! optional
387 //
388 // const char *signature; // the block signature
389 // const char *layout; // reserved
Mike Stumpab695142009-02-13 15:16:56 +0000390 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000391 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(),
392 UnsignedLongTy,
Mike Stumpa5448542009-02-13 15:32:32 +0000393 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000394 NULL);
395
396 getModule().addTypeName("struct.__block_descriptor",
397 BlockDescriptorType);
398
399 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000400}
401
Mike Stump2a998142009-03-04 18:17:45 +0000402const llvm::Type *BlockModule::getGenericBlockLiteralType() {
Mike Stump9b8a7972009-02-13 15:25:34 +0000403 if (GenericBlockLiteralType)
404 return GenericBlockLiteralType;
405
Mike Stumpa5448542009-02-13 15:32:32 +0000406 const llvm::Type *BlockDescPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000407 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000408
Mike Stump7cbb3602009-02-13 16:01:35 +0000409 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
410 getTypes().ConvertType(getContext().IntTy));
411
Mike Stump9b8a7972009-02-13 15:25:34 +0000412 // struct __block_literal_generic {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000413 // void *__isa;
414 // int __flags;
415 // int __reserved;
416 // void (*__invoke)(void *);
417 // struct __block_descriptor *__descriptor;
Mike Stump9b8a7972009-02-13 15:25:34 +0000418 // };
Blaine Garst2a7eb282010-02-23 21:51:17 +0000419 GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
Owen Anderson47a434f2009-08-05 23:18:46 +0000420 PtrToInt8Ty,
Mike Stump7cbb3602009-02-13 16:01:35 +0000421 IntTy,
422 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000423 PtrToInt8Ty,
Mike Stump9b8a7972009-02-13 15:25:34 +0000424 BlockDescPtrTy,
425 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000426
Mike Stump9b8a7972009-02-13 15:25:34 +0000427 getModule().addTypeName("struct.__block_literal_generic",
428 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000429
Mike Stump9b8a7972009-02-13 15:25:34 +0000430 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000431}
432
Mike Stumpbd65cac2009-02-19 01:01:04 +0000433
Anders Carlssona1736c02009-12-24 21:13:40 +0000434RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E,
435 ReturnValueSlot ReturnValue) {
Mike Stumpa5448542009-02-13 15:32:32 +0000436 const BlockPointerType *BPT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000437 E->getCallee()->getType()->getAs<BlockPointerType>();
Mike Stumpa5448542009-02-13 15:32:32 +0000438
Anders Carlssonacfde802009-02-12 00:39:25 +0000439 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
440
441 // Get a pointer to the generic block literal.
442 const llvm::Type *BlockLiteralTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000443 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000444
445 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000446 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000447 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
448
449 // Get the function pointer from the literal.
450 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Anders Carlssonacfde802009-02-12 00:39:25 +0000451
Mike Stumpa5448542009-02-13 15:32:32 +0000452 BlockLiteral =
453 Builder.CreateBitCast(BlockLiteral,
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000454 llvm::Type::getInt8PtrTy(VMContext),
Anders Carlssonacfde802009-02-12 00:39:25 +0000455 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000456
Anders Carlssonacfde802009-02-12 00:39:25 +0000457 // Add the block literal.
458 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
459 CallArgList Args;
460 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000461
Anders Carlsson782f3972009-04-08 23:13:16 +0000462 QualType FnType = BPT->getPointeeType();
463
Anders Carlssonacfde802009-02-12 00:39:25 +0000464 // And the rest of the arguments.
John McCall183700f2009-09-21 23:43:11 +0000465 EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(),
Anders Carlsson782f3972009-04-08 23:13:16 +0000466 E->arg_begin(), E->arg_end());
Mike Stumpa5448542009-02-13 15:32:32 +0000467
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000468 // Load the function.
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000469 llvm::Value *Func = Builder.CreateLoad(FuncPtr, "tmp");
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000470
John McCall04a67a62010-02-05 21:31:56 +0000471 const FunctionType *FuncTy = FnType->getAs<FunctionType>();
472 QualType ResultType = FuncTy->getResultType();
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000473
Mike Stump1eb44332009-09-09 15:08:12 +0000474 const CGFunctionInfo &FnInfo =
John McCall04a67a62010-02-05 21:31:56 +0000475 CGM.getTypes().getFunctionInfo(ResultType, Args, FuncTy->getCallConv(),
476 FuncTy->getNoReturnAttr());
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000478 // Cast the function pointer to the right type.
Mike Stump1eb44332009-09-09 15:08:12 +0000479 const llvm::Type *BlockFTy =
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000480 CGM.getTypes().GetFunctionType(FnInfo, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Owen Anderson96e0fc72009-07-29 22:16:19 +0000482 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000483 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Anders Carlssonacfde802009-02-12 00:39:25 +0000485 // And call the block.
Anders Carlssona1736c02009-12-24 21:13:40 +0000486 return EmitCall(FnInfo, Func, ReturnValue, Args);
Anders Carlssonacfde802009-02-12 00:39:25 +0000487}
Anders Carlssond5cab542009-02-12 17:55:02 +0000488
Ken Dyck199c3d62010-01-11 17:06:35 +0000489CharUnits CodeGenFunction::AllocateBlockDecl(const BlockDeclRefExpr *E) {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000490 const ValueDecl *VD = E->getDecl();
Ken Dyck199c3d62010-01-11 17:06:35 +0000491 CharUnits &offset = BlockDecls[VD];
Mike Stumpdab514f2009-03-04 03:23:46 +0000492
Mike Stumpdab514f2009-03-04 03:23:46 +0000493 // See if we have already allocated an offset for this variable.
Ken Dyck199c3d62010-01-11 17:06:35 +0000494 if (offset.isPositive())
Mike Stumpea26cb52009-10-21 03:49:08 +0000495 return offset;
496
497 // Don't run the expensive check, unless we have to.
Mike Stump083c25e2009-10-22 00:49:09 +0000498 if (!BlockHasCopyDispose)
499 if (E->isByRef()
500 || BlockRequiresCopying(E->getType()))
501 BlockHasCopyDispose = true;
Mike Stumpea26cb52009-10-21 03:49:08 +0000502
503 // if not, allocate one now.
504 offset = getBlockOffset(E);
505
506 return offset;
507}
508
509llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
510 const ValueDecl *VD = E->getDecl();
Ken Dyck199c3d62010-01-11 17:06:35 +0000511 CharUnits offset = AllocateBlockDecl(E);
Mike Stumpea26cb52009-10-21 03:49:08 +0000512
Mike Stumpdab514f2009-03-04 03:23:46 +0000513
514 llvm::Value *BlockLiteral = LoadBlockStruct();
515 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
Mike Stumpea26cb52009-10-21 03:49:08 +0000516 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Ken Dyck199c3d62010-01-11 17:06:35 +0000517 offset.getQuantity()),
Mike Stump58a85142009-03-04 22:48:06 +0000518 "block.literal");
Mike Stumpdab514f2009-03-04 03:23:46 +0000519 if (E->isByRef()) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000520 const llvm::Type *PtrStructTy
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000521 = llvm::PointerType::get(BuildByRefType(VD), 0);
Mike Stumpa8b60c92009-03-21 21:00:35 +0000522 // The block literal will need a copy/destroy helper.
523 BlockHasCopyDispose = true;
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000524
525 const llvm::Type *Ty = PtrStructTy;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000526 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000527 V = Builder.CreateBitCast(V, Ty);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000528 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000529 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000530 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000531 V = Builder.CreateBitCast(V, PtrStructTy);
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000532 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
533 VD->getNameAsString());
Mike Stumpdab514f2009-03-04 03:23:46 +0000534 } else {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000535 const llvm::Type *Ty = CGM.getTypes().ConvertType(VD->getType());
536
Owen Anderson96e0fc72009-07-29 22:16:19 +0000537 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000538 V = Builder.CreateBitCast(V, Ty);
539 }
540 return V;
541}
542
Mike Stump6cc88f72009-03-20 21:53:12 +0000543void CodeGenFunction::BlockForwardSelf() {
544 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
545 ImplicitParamDecl *SelfDecl = OMD->getSelfDecl();
546 llvm::Value *&DMEntry = LocalDeclMap[SelfDecl];
547 if (DMEntry)
548 return;
549 // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care
550 BlockDeclRefExpr *BDRE = new (getContext())
551 BlockDeclRefExpr(SelfDecl,
552 SelfDecl->getType(), SourceLocation(), false);
553 DMEntry = GetAddrOfBlockDecl(BDRE);
554}
555
Mike Stump67a64482009-02-14 22:16:35 +0000556llvm::Constant *
Mike Stump90a90432009-03-04 18:47:42 +0000557BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000558 // Generate the block descriptor.
559 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000560 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
561 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000562
Blaine Garst2a7eb282010-02-23 21:51:17 +0000563 llvm::Constant *DescriptorFields[4];
Mike Stumpa5448542009-02-13 15:32:32 +0000564
Anders Carlssond5cab542009-02-12 17:55:02 +0000565 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000566 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000567
Anders Carlssond5cab542009-02-12 17:55:02 +0000568 // Block literal size. For global blocks we just use the size of the generic
569 // block literal struct.
Ken Dyck687cc4a2010-01-26 13:48:07 +0000570 CharUnits BlockLiteralSize =
571 CGM.GetTargetTypeStoreSize(getGenericBlockLiteralType());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000572 DescriptorFields[1] =
Ken Dyck199c3d62010-01-11 17:06:35 +0000573 llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize.getQuantity());
Blaine Garst2a7eb282010-02-23 21:51:17 +0000574
575 // signature. non-optional ObjC-style method descriptor @encode sequence
576 std::string BlockTypeEncoding;
577 CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
Mike Stumpa5448542009-02-13 15:32:32 +0000578
Blaine Garst2a7eb282010-02-23 21:51:17 +0000579 DescriptorFields[2] = llvm::ConstantExpr::getBitCast(
580 CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty);
581
582 // layout
583 DescriptorFields[3] =
584 llvm::ConstantInt::get(UnsignedLongTy,0);
585
586 // build the structure from the 4 elements
Mike Stumpa5448542009-02-13 15:32:32 +0000587 llvm::Constant *DescriptorStruct =
Blaine Garst2a7eb282010-02-23 21:51:17 +0000588 llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 4, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000589
Anders Carlssond5cab542009-02-12 17:55:02 +0000590 llvm::GlobalVariable *Descriptor =
Owen Anderson1c431b32009-07-08 19:05:04 +0000591 new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000592 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000593 DescriptorStruct, "__block_descriptor_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000594
David Chisnall5e530af2009-11-17 19:33:30 +0000595 int FieldCount = 5;
Anders Carlssond5cab542009-02-12 17:55:02 +0000596 // Generate the constants for the block literal.
David Chisnall5e530af2009-11-17 19:33:30 +0000597
598 std::vector<llvm::Constant*> LiteralFields(FieldCount);
Mike Stumpa5448542009-02-13 15:32:32 +0000599
Mike Stump67a64482009-02-14 22:16:35 +0000600 CodeGenFunction::BlockInfo Info(0, n);
Ken Dyck199c3d62010-01-11 17:06:35 +0000601 CharUnits subBlockSize;
Ken Dyck30a8a272010-01-26 19:13:33 +0000602 CharUnits subBlockAlign;
Mike Stumpa99038c2009-02-28 09:07:16 +0000603 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Daniel Dunbard67b09a2009-03-12 03:07:24 +0000604 bool subBlockHasCopyDispose = false;
Mike Stump7f28a9c2009-03-13 23:34:28 +0000605 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000606 llvm::Function *Fn
Mike Stump6cc88f72009-03-20 21:53:12 +0000607 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000608 subBlockSize,
Mike Stump90a90432009-03-04 18:47:42 +0000609 subBlockAlign,
Mike Stump00470a12009-03-05 08:32:30 +0000610 subBlockDeclRefDecls,
611 subBlockHasCopyDispose);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000612 assert(subBlockSize == BlockLiteralSize
613 && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000614
Anders Carlssond5cab542009-02-12 17:55:02 +0000615 // isa
Mike Stumpf99f1d02009-02-13 17:23:42 +0000616 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000617
Anders Carlssond5cab542009-02-12 17:55:02 +0000618 // Flags
Blaine Garst2a7eb282010-02-23 21:51:17 +0000619 LiteralFields[1] =
Blaine Garsta36e2232010-03-05 01:29:59 +0000620 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE);
Mike Stumpa5448542009-02-13 15:32:32 +0000621
Anders Carlssond5cab542009-02-12 17:55:02 +0000622 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000623 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000624
Anders Carlssond5cab542009-02-12 17:55:02 +0000625 // Function
626 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000627
Anders Carlssond5cab542009-02-12 17:55:02 +0000628 // Descriptor
629 LiteralFields[4] = Descriptor;
David Chisnall5e530af2009-11-17 19:33:30 +0000630
Mike Stumpa5448542009-02-13 15:32:32 +0000631 llvm::Constant *BlockLiteralStruct =
David Chisnall5e530af2009-11-17 19:33:30 +0000632 llvm::ConstantStruct::get(VMContext, LiteralFields, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000633
634 llvm::GlobalVariable *BlockLiteral =
Owen Anderson1c431b32009-07-08 19:05:04 +0000635 new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000636 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000637 BlockLiteralStruct, "__block_literal_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000638
Anders Carlssond5cab542009-02-12 17:55:02 +0000639 return BlockLiteral;
640}
641
Mike Stump4e7a1f72009-02-21 20:00:35 +0000642llvm::Value *CodeGenFunction::LoadBlockStruct() {
Mike Stumpbf1914b2009-10-20 20:30:01 +0000643 llvm::Value *V = Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()],
644 "self");
645 // For now, we codegen based upon byte offsets.
646 return Builder.CreateBitCast(V, PtrToInt8Ty);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000647}
648
Mike Stump00470a12009-03-05 08:32:30 +0000649llvm::Function *
650CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
651 const BlockInfo& Info,
Mike Stump6cc88f72009-03-20 21:53:12 +0000652 const Decl *OuterFuncDecl,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000653 llvm::DenseMap<const Decl*, llvm::Value*> ldm,
Ken Dyck199c3d62010-01-11 17:06:35 +0000654 CharUnits &Size,
Ken Dyck30a8a272010-01-26 19:13:33 +0000655 CharUnits &Align,
Mike Stump00470a12009-03-05 08:32:30 +0000656 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls,
657 bool &subBlockHasCopyDispose) {
Devang Patel963dfbd2009-04-15 21:51:44 +0000658
659 // Check if we should generate debug info for this block.
660 if (CGM.getDebugInfo())
661 DebugInfo = CGM.getDebugInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Mike Stump7f28a9c2009-03-13 23:34:28 +0000663 // Arrange for local static and local extern declarations to appear
664 // to be local to this function as well, as they are directly referenced
665 // in a block.
666 for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
667 i != ldm.end();
668 ++i) {
669 const VarDecl *VD = dyn_cast<VarDecl>(i->first);
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000671 if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage())
Mike Stump7f28a9c2009-03-13 23:34:28 +0000672 LocalDeclMap[VD] = i->second;
673 }
674
Ken Dyck687cc4a2010-01-26 13:48:07 +0000675 BlockOffset =
676 CGM.GetTargetTypeStoreSize(CGM.getGenericBlockLiteralType());
Ken Dyck30a8a272010-01-26 19:13:33 +0000677 BlockAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Eli Friedman48f91222009-03-28 03:24:54 +0000678
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000679 const FunctionType *BlockFunctionType = BExpr->getFunctionType();
680 QualType ResultType;
John McCall04a67a62010-02-05 21:31:56 +0000681 CallingConv CC = BlockFunctionType->getCallConv();
682 bool NoReturn = BlockFunctionType->getNoReturnAttr();
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 =
John McCall04a67a62010-02-05 21:31:56 +0000721 CGM.getTypes().getFunctionInfo(ResultType, Args, CC, NoReturn);
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(),
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000815 0, QualType(PadTy), 0, VarDecl::None);
Mike Stumpa99038c2009-02-28 09:07:16 +0000816 Expr *E;
817 E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
Douglas Gregor0da76df2009-11-23 11:41:28 +0000818 SourceLocation());
Mike Stumpa99038c2009-02-28 09:07:16 +0000819 BlockDeclRefDecls.push_back(E);
820 }
821 BlockDeclRefDecls.push_back(BDRE);
822
823 BlockOffset += Size;
824 return BlockOffset-Size;
825}
Mike Stumpdab514f2009-03-04 03:23:46 +0000826
Mike Stump08920992009-03-07 02:35:30 +0000827llvm::Constant *BlockFunction::
828GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000829 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000830 QualType R = getContext().VoidTy;
831
832 FunctionArgList Args;
833 // FIXME: This leaks
Mike Stump08920992009-03-07 02:35:30 +0000834 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +0000835 ImplicitParamDecl::Create(getContext(), 0,
836 SourceLocation(), 0,
Mike Stump08920992009-03-07 02:35:30 +0000837 getContext().getPointerType(getContext().VoidTy));
838 Args.push_back(std::make_pair(Dst, Dst->getType()));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000839 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +0000840 ImplicitParamDecl::Create(getContext(), 0,
841 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000842 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000843 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Mike Stumpa4f668f2009-03-06 01:33:24 +0000845 const CGFunctionInfo &FI =
John McCall04a67a62010-02-05 21:31:56 +0000846 CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000847
Mike Stump3899a7f2009-06-05 23:26:36 +0000848 // FIXME: We'd like to put these into a mergable by content, with
849 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000850 CodeGenTypes &Types = CGM.getTypes();
851 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
852
853 llvm::Function *Fn =
854 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000855 "__copy_helper_block_", &CGM.getModule());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000856
857 IdentifierInfo *II
858 = &CGM.getContext().Idents.get("__copy_helper_block_");
859
860 FunctionDecl *FD = FunctionDecl::Create(getContext(),
861 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000862 SourceLocation(), II, R, 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000863 FunctionDecl::Static, false,
864 true);
865 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump08920992009-03-07 02:35:30 +0000866
867 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
868 llvm::Type *PtrPtrT;
Mike Stump08920992009-03-07 02:35:30 +0000869
Mike Stumpb7477cf2009-04-10 18:52:28 +0000870 if (NoteForHelperp) {
871 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump08920992009-03-07 02:35:30 +0000872
Owen Anderson96e0fc72009-07-29 22:16:19 +0000873 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000874 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
875 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump08920992009-03-07 02:35:30 +0000876
Mike Stumpb7477cf2009-04-10 18:52:28 +0000877 llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000878 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000879 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000880 DstObj = Builder.CreateBitCast(DstObj, PtrPtrT);
881 DstObj = Builder.CreateLoad(DstObj);
Mike Stump08920992009-03-07 02:35:30 +0000882
Mike Stumpb7477cf2009-04-10 18:52:28 +0000883 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
884 int flag = NoteForHelper[i].flag;
885 int index = NoteForHelper[i].index;
Mike Stump08920992009-03-07 02:35:30 +0000886
Mike Stumpb7477cf2009-04-10 18:52:28 +0000887 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
888 || NoteForHelper[i].RequiresCopying) {
889 llvm::Value *Srcv = SrcObj;
890 Srcv = Builder.CreateStructGEP(Srcv, index);
891 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000892 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000893 Srcv = Builder.CreateLoad(Srcv);
894
895 llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
896 Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
897
Owen Anderson0032b272009-08-13 21:57:51 +0000898 llvm::Value *N = llvm::ConstantInt::get(
899 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000900 llvm::Value *F = getBlockObjectAssign();
901 Builder.CreateCall3(F, Dstv, Srcv, N);
902 }
Mike Stump08920992009-03-07 02:35:30 +0000903 }
904 }
905
Mike Stumpa4f668f2009-03-06 01:33:24 +0000906 CGF.FinishFunction();
907
Owen Anderson3c4972d2009-07-29 18:54:39 +0000908 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000909}
910
Mike Stumpcf62d392009-03-06 18:42:23 +0000911llvm::Constant *BlockFunction::
Mike Stump08920992009-03-07 02:35:30 +0000912GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
913 const llvm::StructType* T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000914 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000915 QualType R = getContext().VoidTy;
916
917 FunctionArgList Args;
918 // FIXME: This leaks
919 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +0000920 ImplicitParamDecl::Create(getContext(), 0,
921 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000922 getContext().getPointerType(getContext().VoidTy));
923
924 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Mike Stumpa4f668f2009-03-06 01:33:24 +0000926 const CGFunctionInfo &FI =
John McCall04a67a62010-02-05 21:31:56 +0000927 CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000928
Mike Stump3899a7f2009-06-05 23:26:36 +0000929 // FIXME: We'd like to put these into a mergable by content, with
930 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000931 CodeGenTypes &Types = CGM.getTypes();
932 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
933
934 llvm::Function *Fn =
935 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000936 "__destroy_helper_block_", &CGM.getModule());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000937
938 IdentifierInfo *II
939 = &CGM.getContext().Idents.get("__destroy_helper_block_");
940
941 FunctionDecl *FD = FunctionDecl::Create(getContext(),
942 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000943 SourceLocation(), II, R, 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000944 FunctionDecl::Static, false,
945 true);
946 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1edf6b62009-03-07 02:53:18 +0000947
Mike Stumpb7477cf2009-04-10 18:52:28 +0000948 if (NoteForHelperp) {
949 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump1edf6b62009-03-07 02:53:18 +0000950
Mike Stumpb7477cf2009-04-10 18:52:28 +0000951 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
952 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000953 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000954 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
955 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump1edf6b62009-03-07 02:53:18 +0000956
Mike Stumpb7477cf2009-04-10 18:52:28 +0000957 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
958 int flag = NoteForHelper[i].flag;
959 int index = NoteForHelper[i].index;
Mike Stump1edf6b62009-03-07 02:53:18 +0000960
Mike Stumpb7477cf2009-04-10 18:52:28 +0000961 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
962 || NoteForHelper[i].RequiresCopying) {
963 llvm::Value *Srcv = SrcObj;
964 Srcv = Builder.CreateStructGEP(Srcv, index);
965 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000966 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000967 Srcv = Builder.CreateLoad(Srcv);
968
969 BuildBlockRelease(Srcv, flag);
970 }
Mike Stump1edf6b62009-03-07 02:53:18 +0000971 }
972 }
973
Mike Stumpa4f668f2009-03-06 01:33:24 +0000974 CGF.FinishFunction();
975
Owen Anderson3c4972d2009-07-29 18:54:39 +0000976 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000977}
978
Mike Stump08920992009-03-07 02:35:30 +0000979llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000980 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump08920992009-03-07 02:35:30 +0000981 return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
982 T, NoteForHelper);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000983}
984
Mike Stump08920992009-03-07 02:35:30 +0000985llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000986 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stump08920992009-03-07 02:35:30 +0000987 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000988 T, NoteForHelperp);
Mike Stumpdab514f2009-03-04 03:23:46 +0000989}
Mike Stump797b6322009-03-05 01:23:13 +0000990
Mike Stumpee094222009-03-06 06:12:24 +0000991llvm::Constant *BlockFunction::
992GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
Mike Stump45031c02009-03-06 02:29:21 +0000993 QualType R = getContext().VoidTy;
994
995 FunctionArgList Args;
996 // FIXME: This leaks
Mike Stumpee094222009-03-06 06:12:24 +0000997 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +0000998 ImplicitParamDecl::Create(getContext(), 0,
999 SourceLocation(), 0,
Mike Stumpee094222009-03-06 06:12:24 +00001000 getContext().getPointerType(getContext().VoidTy));
1001 Args.push_back(std::make_pair(Dst, Dst->getType()));
1002
1003 // FIXME: This leaks
Mike Stump45031c02009-03-06 02:29:21 +00001004 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001005 ImplicitParamDecl::Create(getContext(), 0,
1006 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001007 getContext().getPointerType(getContext().VoidTy));
Mike Stump45031c02009-03-06 02:29:21 +00001008 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Mike Stump45031c02009-03-06 02:29:21 +00001010 const CGFunctionInfo &FI =
John McCall04a67a62010-02-05 21:31:56 +00001011 CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false);
Mike Stump45031c02009-03-06 02:29:21 +00001012
Mike Stump45031c02009-03-06 02:29:21 +00001013 CodeGenTypes &Types = CGM.getTypes();
1014 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1015
Mike Stump3899a7f2009-06-05 23:26:36 +00001016 // FIXME: We'd like to put these into a mergable by content, with
1017 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001018 llvm::Function *Fn =
1019 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001020 "__Block_byref_id_object_copy_", &CGM.getModule());
Mike Stump45031c02009-03-06 02:29:21 +00001021
1022 IdentifierInfo *II
1023 = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
1024
1025 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1026 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001027 SourceLocation(), II, R, 0,
Mike Stump45031c02009-03-06 02:29:21 +00001028 FunctionDecl::Static, false,
1029 true);
1030 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpee094222009-03-06 06:12:24 +00001031
1032 // dst->x
1033 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001034 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001035 V = Builder.CreateLoad(V);
Mike Stumpee094222009-03-06 06:12:24 +00001036 V = Builder.CreateStructGEP(V, 6, "x");
1037 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
1038
1039 // src->x
1040 V = CGF.GetAddrOfLocalVar(Src);
1041 V = Builder.CreateLoad(V);
1042 V = Builder.CreateBitCast(V, T);
1043 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001044 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpee094222009-03-06 06:12:24 +00001045 llvm::Value *SrcObj = Builder.CreateLoad(V);
Mike Stump1eb44332009-09-09 15:08:12 +00001046
Mike Stumpee094222009-03-06 06:12:24 +00001047 flag |= BLOCK_BYREF_CALLER;
1048
Owen Anderson0032b272009-08-13 21:57:51 +00001049 llvm::Value *N = llvm::ConstantInt::get(
1050 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpee094222009-03-06 06:12:24 +00001051 llvm::Value *F = getBlockObjectAssign();
1052 Builder.CreateCall3(F, DstObj, SrcObj, N);
1053
Mike Stump45031c02009-03-06 02:29:21 +00001054 CGF.FinishFunction();
1055
Owen Anderson3c4972d2009-07-29 18:54:39 +00001056 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001057}
1058
Mike Stump1851b682009-03-06 04:53:30 +00001059llvm::Constant *
1060BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
1061 int flag) {
Mike Stump45031c02009-03-06 02:29:21 +00001062 QualType R = getContext().VoidTy;
1063
1064 FunctionArgList Args;
1065 // FIXME: This leaks
1066 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001067 ImplicitParamDecl::Create(getContext(), 0,
1068 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001069 getContext().getPointerType(getContext().VoidTy));
1070
1071 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001072
Mike Stump45031c02009-03-06 02:29:21 +00001073 const CGFunctionInfo &FI =
John McCall04a67a62010-02-05 21:31:56 +00001074 CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false);
Mike Stump45031c02009-03-06 02:29:21 +00001075
Mike Stump45031c02009-03-06 02:29:21 +00001076 CodeGenTypes &Types = CGM.getTypes();
1077 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1078
Mike Stump3899a7f2009-06-05 23:26:36 +00001079 // FIXME: We'd like to put these into a mergable by content, with
1080 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001081 llvm::Function *Fn =
1082 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001083 "__Block_byref_id_object_dispose_",
Mike Stump45031c02009-03-06 02:29:21 +00001084 &CGM.getModule());
1085
1086 IdentifierInfo *II
1087 = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
1088
1089 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1090 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001091 SourceLocation(), II, R, 0,
Mike Stump45031c02009-03-06 02:29:21 +00001092 FunctionDecl::Static, false,
1093 true);
1094 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1851b682009-03-06 04:53:30 +00001095
1096 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001097 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001098 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001099 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001100 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001101 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001102
Mike Stump1851b682009-03-06 04:53:30 +00001103 flag |= BLOCK_BYREF_CALLER;
1104 BuildBlockRelease(V, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001105 CGF.FinishFunction();
1106
Owen Anderson3c4972d2009-07-29 18:54:39 +00001107 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001108}
1109
Mike Stumpee094222009-03-06 06:12:24 +00001110llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001111 int Flag, unsigned Align) {
1112 // All alignments below that of pointer alignment collapse down to just
Mike Stump3899a7f2009-06-05 23:26:36 +00001113 // pointer alignment, as we always have at least that much alignment to begin
1114 // with.
1115 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001116
Mike Stump3899a7f2009-06-05 23:26:36 +00001117 // As an optimization, we only generate a single function of each kind we
1118 // might need. We need a different one for each alignment and for each
1119 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001120 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1121 llvm::Constant *&Entry = CGM.AssignCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001122 if (Entry)
1123 return Entry;
Chris Lattner10976d92009-12-05 08:21:30 +00001124 return Entry = CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, Flag);
Mike Stump45031c02009-03-06 02:29:21 +00001125}
1126
Mike Stump1851b682009-03-06 04:53:30 +00001127llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001128 int Flag,
Mike Stump3899a7f2009-06-05 23:26:36 +00001129 unsigned Align) {
1130 // All alignments below that of pointer alignment collpase down to just
1131 // pointer alignment, as we always have at least that much alignment to begin
1132 // with.
1133 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001134
Mike Stump3899a7f2009-06-05 23:26:36 +00001135 // As an optimization, we only generate a single function of each kind we
1136 // might need. We need a different one for each alignment and for each
1137 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001138 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1139 llvm::Constant *&Entry = CGM.DestroyCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001140 if (Entry)
1141 return Entry;
Chris Lattner10976d92009-12-05 08:21:30 +00001142 return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, Flag);
Mike Stump45031c02009-03-06 02:29:21 +00001143}
1144
Mike Stump797b6322009-03-05 01:23:13 +00001145llvm::Value *BlockFunction::getBlockObjectDispose() {
1146 if (CGM.BlockObjectDispose == 0) {
1147 const llvm::FunctionType *FTy;
1148 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001149 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stump797b6322009-03-05 01:23:13 +00001150 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001151 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson96e0fc72009-07-29 22:16:19 +00001152 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stump00470a12009-03-05 08:32:30 +00001153 CGM.BlockObjectDispose
Mike Stump797b6322009-03-05 01:23:13 +00001154 = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
1155 }
1156 return CGM.BlockObjectDispose;
1157}
1158
Mike Stumpee094222009-03-06 06:12:24 +00001159llvm::Value *BlockFunction::getBlockObjectAssign() {
1160 if (CGM.BlockObjectAssign == 0) {
1161 const llvm::FunctionType *FTy;
1162 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001163 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stumpee094222009-03-06 06:12:24 +00001164 ArgTys.push_back(PtrToInt8Ty);
1165 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001166 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson96e0fc72009-07-29 22:16:19 +00001167 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stumpee094222009-03-06 06:12:24 +00001168 CGM.BlockObjectAssign
1169 = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
1170 }
1171 return CGM.BlockObjectAssign;
1172}
1173
Mike Stump1851b682009-03-06 04:53:30 +00001174void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Mike Stump797b6322009-03-05 01:23:13 +00001175 llvm::Value *F = getBlockObjectDispose();
Mike Stump1851b682009-03-06 04:53:30 +00001176 llvm::Value *N;
Mike Stump797b6322009-03-05 01:23:13 +00001177 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001178 N = llvm::ConstantInt::get(llvm::Type::getInt32Ty(V->getContext()), flag);
Mike Stump797b6322009-03-05 01:23:13 +00001179 Builder.CreateCall2(F, V, N);
1180}
Mike Stump00470a12009-03-05 08:32:30 +00001181
1182ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stump08920992009-03-07 02:35:30 +00001183
1184BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1185 CGBuilderTy &B)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001186 : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) {
Owen Anderson0032b272009-08-13 21:57:51 +00001187 PtrToInt8Ty = llvm::PointerType::getUnqual(
1188 llvm::Type::getInt8Ty(VMContext));
Mike Stump08920992009-03-07 02:35:30 +00001189
1190 BlockHasCopyDispose = false;
1191}