blob: 7076067e4381b2b050bf19cc841b3cb6236f9e91 [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 Garst2a7eb282010-02-23 21:51:17 +0000157 unsigned int flags = BLOCK_HAS_OBJC_TYPE;
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
187 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
188 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000189 C = llvm::ConstantInt::get(IntTy, flags);
Mike Stump00470a12009-03-05 08:32:30 +0000190 Elts[1] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000191
192 // __reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000193 C = llvm::ConstantInt::get(IntTy, 0);
Mike Stump00470a12009-03-05 08:32:30 +0000194 Elts[2] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000195
Mike Stump8a2b4b12009-02-25 23:33:13 +0000196 if (subBlockDeclRefDecls.size() == 0) {
Mike Stumpcf62d392009-03-06 18:42:23 +0000197 // __descriptor
Blaine Garst2a7eb282010-02-23 21:51:17 +0000198 Elts[4] = BuildDescriptorBlockDecl(BE, subBlockHasCopyDispose, subBlockSize,
Mike Stumpea26cb52009-10-21 03:49:08 +0000199 0, 0);
Mike Stumpcf62d392009-03-06 18:42:23 +0000200
Mike Stump5570cfe2009-03-01 20:07:53 +0000201 // Optimize to being a global block.
202 Elts[0] = CGM.getNSConcreteGlobalBlock();
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000203 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
Mike Stump5570cfe2009-03-01 20:07:53 +0000204
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000205 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000206
Owen Anderson1c431b32009-07-08 19:05:04 +0000207 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000208 llvm::GlobalValue::InternalLinkage, C,
209 "__block_holder_tmp_" +
210 llvm::Twine(CGM.getGlobalUniqueCount()));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000211 QualType BPT = BE->getType();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000212 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000213 return C;
214 }
Mike Stump00470a12009-03-05 08:32:30 +0000215
David Chisnall5e530af2009-11-17 19:33:30 +0000216 std::vector<const llvm::Type *> Types(BlockFields+subBlockDeclRefDecls.size());
Mike Stump08920992009-03-07 02:35:30 +0000217 for (int i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000218 Types[i] = Elts[i]->getType();
Mike Stump08920992009-03-07 02:35:30 +0000219 Types[4] = PtrToInt8Ty;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000220
Mike Stumpa99038c2009-02-28 09:07:16 +0000221 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
222 const Expr *E = subBlockDeclRefDecls[i];
223 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
224 QualType Ty = E->getType();
Mike Stumpdab514f2009-03-04 03:23:46 +0000225 if (BDRE && BDRE->isByRef()) {
David Chisnall5e530af2009-11-17 19:33:30 +0000226 Types[i+BlockFields] = llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000227 } else
David Chisnall5e530af2009-11-17 19:33:30 +0000228 Types[i+BlockFields] = ConvertType(Ty);
Mike Stumpa99038c2009-02-28 09:07:16 +0000229 }
Mike Stump8a2b4b12009-02-25 23:33:13 +0000230
Owen Anderson47a434f2009-08-05 23:18:46 +0000231 llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000232
233 llvm::AllocaInst *A = CreateTempAlloca(Ty);
Ken Dyck30a8a272010-01-26 19:13:33 +0000234 A->setAlignment(subBlockAlign.getQuantity());
Mike Stump8a2b4b12009-02-25 23:33:13 +0000235 V = A;
236
Mike Stump08920992009-03-07 02:35:30 +0000237 std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size());
238 int helpersize = 0;
239
240 for (unsigned i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000241 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
Mike Stump00470a12009-03-05 08:32:30 +0000242
Mike Stump8a2b4b12009-02-25 23:33:13 +0000243 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
244 {
Mike Stumpa99038c2009-02-28 09:07:16 +0000245 // FIXME: Push const down.
246 Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
247 DeclRefExpr *DR;
248 ValueDecl *VD;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000249
Mike Stumpa99038c2009-02-28 09:07:16 +0000250 DR = dyn_cast<DeclRefExpr>(E);
251 // Skip padding.
252 if (DR) continue;
253
254 BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
255 VD = BDRE->getDecl();
256
David Chisnall5e530af2009-11-17 19:33:30 +0000257 llvm::Value* Addr = Builder.CreateStructGEP(V, i+BlockFields, "tmp");
Mike Stump08920992009-03-07 02:35:30 +0000258 NoteForHelper[helpersize].index = i+5;
Mike Stump39605b42009-09-22 02:12:52 +0000259 NoteForHelper[helpersize].RequiresCopying
260 = BlockRequiresCopying(VD->getType());
Mike Stump08920992009-03-07 02:35:30 +0000261 NoteForHelper[helpersize].flag
Mike Stump39605b42009-09-22 02:12:52 +0000262 = (VD->getType()->isBlockPointerType()
263 ? BLOCK_FIELD_IS_BLOCK
264 : BLOCK_FIELD_IS_OBJECT);
Mike Stump08920992009-03-07 02:35:30 +0000265
Mike Stumpa99038c2009-02-28 09:07:16 +0000266 if (LocalDeclMap[VD]) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000267 if (BDRE->isByRef()) {
Mike Stump08920992009-03-07 02:35:30 +0000268 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
Mike Stumpf4bc3122009-03-07 06:04:31 +0000269 // FIXME: Someone double check this.
270 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000271 llvm::Value *Loc = LocalDeclMap[VD];
272 Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000273 Loc = Builder.CreateLoad(Loc);
Mike Stumpdab514f2009-03-04 03:23:46 +0000274 Builder.CreateStore(Loc, Addr);
Mike Stump08920992009-03-07 02:35:30 +0000275 ++helpersize;
Mike Stumpdab514f2009-03-04 03:23:46 +0000276 continue;
277 } else
John McCalldbd872f2009-12-08 09:08:17 +0000278 E = new (getContext()) DeclRefExpr (VD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000279 VD->getType(),
280 SourceLocation());
Mike Stumpa99038c2009-02-28 09:07:16 +0000281 }
Mike Stumpdab514f2009-03-04 03:23:46 +0000282 if (BDRE->isByRef()) {
Mike Stumpa8b60c92009-03-21 21:00:35 +0000283 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
284 // FIXME: Someone double check this.
285 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpa99038c2009-02-28 09:07:16 +0000286 E = new (getContext())
287 UnaryOperator(E, UnaryOperator::AddrOf,
288 getContext().getPointerType(E->getType()),
289 SourceLocation());
Mike Stumpdab514f2009-03-04 03:23:46 +0000290 }
Mike Stump08920992009-03-07 02:35:30 +0000291 ++helpersize;
Mike Stumpa99038c2009-02-28 09:07:16 +0000292
Mike Stumpa99038c2009-02-28 09:07:16 +0000293 RValue r = EmitAnyExpr(E, Addr, false);
Mike Stumpdab514f2009-03-04 03:23:46 +0000294 if (r.isScalar()) {
295 llvm::Value *Loc = r.getScalarVal();
David Chisnall5e530af2009-11-17 19:33:30 +0000296 const llvm::Type *Ty = Types[i+BlockFields];
Mike Stumpdab514f2009-03-04 03:23:46 +0000297 if (BDRE->isByRef()) {
Mike Stump58a85142009-03-04 22:48:06 +0000298 // E is now the address of the value field, instead, we want the
299 // address of the actual ByRef struct. We optimize this slightly
300 // compared to gcc by not grabbing the forwarding slot as this must
301 // be done during Block_copy for us, and we can postpone the work
302 // until then.
Ken Dyck199c3d62010-01-11 17:06:35 +0000303 CharUnits offset = BlockDecls[BDRE->getDecl()];
Mike Stump00470a12009-03-05 08:32:30 +0000304
Mike Stump58a85142009-03-04 22:48:06 +0000305 llvm::Value *BlockLiteral = LoadBlockStruct();
Mike Stump00470a12009-03-05 08:32:30 +0000306
Mike Stump58a85142009-03-04 22:48:06 +0000307 Loc = Builder.CreateGEP(BlockLiteral,
Mike Stumpea26cb52009-10-21 03:49:08 +0000308 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Ken Dyck199c3d62010-01-11 17:06:35 +0000309 offset.getQuantity()),
Mike Stump58a85142009-03-04 22:48:06 +0000310 "block.literal");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000311 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000312 Loc = Builder.CreateBitCast(Loc, Ty);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000313 Loc = Builder.CreateLoad(Loc);
Mike Stump58a85142009-03-04 22:48:06 +0000314 // Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000315 }
316 Builder.CreateStore(Loc, Addr);
317 } else if (r.isComplex())
Mike Stump8a2b4b12009-02-25 23:33:13 +0000318 // FIXME: implement
319 ErrorUnsupported(BE, "complex in block literal");
320 else if (r.isAggregate())
321 ; // Already created into the destination
322 else
323 assert (0 && "bad block variable");
324 // FIXME: Ensure that the offset created by the backend for
325 // the struct matches the previously computed offset in BlockDecls.
326 }
Mike Stump08920992009-03-07 02:35:30 +0000327 NoteForHelper.resize(helpersize);
Mike Stumpcf62d392009-03-06 18:42:23 +0000328
329 // __descriptor
Blaine Garst2a7eb282010-02-23 21:51:17 +0000330 llvm::Value *Descriptor = BuildDescriptorBlockDecl(BE,
331 subBlockHasCopyDispose,
Mike Stumpa803b0e2009-03-25 17:58:24 +0000332 subBlockSize, Ty,
Mike Stump08920992009-03-07 02:35:30 +0000333 &NoteForHelper);
334 Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
335 Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
Mike Stumpe5fee252009-02-13 16:19:19 +0000336 }
Mike Stump00470a12009-03-05 08:32:30 +0000337
Mike Stumpbd65cac2009-02-19 01:01:04 +0000338 QualType BPT = BE->getType();
Fariborz Jahanian263c4de2010-02-10 23:34:57 +0000339 V = Builder.CreateBitCast(V, ConvertType(BPT));
340 // See if this is a __weak block variable and the must call objc_read_weak
341 // on it.
342 const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>();
343 QualType RES = ftype->getResultType();
344 if (RES.isObjCGCWeak()) {
345 // Must cast argument to id*
346 const llvm::Type *ObjectPtrTy =
347 ConvertType(CGM.getContext().getObjCIdType());
348 const llvm::Type *PtrObjectPtrTy =
349 llvm::PointerType::getUnqual(ObjectPtrTy);
350 V = Builder.CreateBitCast(V, PtrObjectPtrTy);
351 V = CGM.getObjCRuntime().EmitObjCWeakRead(*this, V);
352 }
353 return V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000354}
355
356
Mike Stump2a998142009-03-04 18:17:45 +0000357const llvm::Type *BlockModule::getBlockDescriptorType() {
Mike Stumpab695142009-02-13 15:16:56 +0000358 if (BlockDescriptorType)
359 return BlockDescriptorType;
360
Mike Stumpa5448542009-02-13 15:32:32 +0000361 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000362 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000363
Mike Stumpab695142009-02-13 15:16:56 +0000364 // struct __block_descriptor {
365 // unsigned long reserved;
366 // unsigned long block_size;
Blaine Garst2a7eb282010-02-23 21:51:17 +0000367 //
368 // // later, the following will be added
369 //
370 // struct {
371 // void (*copyHelper)();
372 // void (*copyHelper)();
373 // } helpers; // !!! optional
374 //
375 // const char *signature; // the block signature
376 // const char *layout; // reserved
Mike Stumpab695142009-02-13 15:16:56 +0000377 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000378 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(),
379 UnsignedLongTy,
Mike Stumpa5448542009-02-13 15:32:32 +0000380 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000381 NULL);
382
383 getModule().addTypeName("struct.__block_descriptor",
384 BlockDescriptorType);
385
386 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000387}
388
Mike Stump2a998142009-03-04 18:17:45 +0000389const llvm::Type *BlockModule::getGenericBlockLiteralType() {
Mike Stump9b8a7972009-02-13 15:25:34 +0000390 if (GenericBlockLiteralType)
391 return GenericBlockLiteralType;
392
Mike Stumpa5448542009-02-13 15:32:32 +0000393 const llvm::Type *BlockDescPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000394 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000395
Mike Stump7cbb3602009-02-13 16:01:35 +0000396 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
397 getTypes().ConvertType(getContext().IntTy));
398
Mike Stump9b8a7972009-02-13 15:25:34 +0000399 // struct __block_literal_generic {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000400 // void *__isa;
401 // int __flags;
402 // int __reserved;
403 // void (*__invoke)(void *);
404 // struct __block_descriptor *__descriptor;
Mike Stump9b8a7972009-02-13 15:25:34 +0000405 // };
Blaine Garst2a7eb282010-02-23 21:51:17 +0000406 GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
Owen Anderson47a434f2009-08-05 23:18:46 +0000407 PtrToInt8Ty,
Mike Stump7cbb3602009-02-13 16:01:35 +0000408 IntTy,
409 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000410 PtrToInt8Ty,
Mike Stump9b8a7972009-02-13 15:25:34 +0000411 BlockDescPtrTy,
412 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000413
Mike Stump9b8a7972009-02-13 15:25:34 +0000414 getModule().addTypeName("struct.__block_literal_generic",
415 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000416
Mike Stump9b8a7972009-02-13 15:25:34 +0000417 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000418}
419
Mike Stumpbd65cac2009-02-19 01:01:04 +0000420
Anders Carlssona1736c02009-12-24 21:13:40 +0000421RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E,
422 ReturnValueSlot ReturnValue) {
Mike Stumpa5448542009-02-13 15:32:32 +0000423 const BlockPointerType *BPT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000424 E->getCallee()->getType()->getAs<BlockPointerType>();
Mike Stumpa5448542009-02-13 15:32:32 +0000425
Anders Carlssonacfde802009-02-12 00:39:25 +0000426 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
427
428 // Get a pointer to the generic block literal.
429 const llvm::Type *BlockLiteralTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000430 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000431
432 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000433 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000434 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
435
436 // Get the function pointer from the literal.
437 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Anders Carlssonacfde802009-02-12 00:39:25 +0000438
Mike Stumpa5448542009-02-13 15:32:32 +0000439 BlockLiteral =
440 Builder.CreateBitCast(BlockLiteral,
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000441 llvm::Type::getInt8PtrTy(VMContext),
Anders Carlssonacfde802009-02-12 00:39:25 +0000442 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000443
Anders Carlssonacfde802009-02-12 00:39:25 +0000444 // Add the block literal.
445 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
446 CallArgList Args;
447 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000448
Anders Carlsson782f3972009-04-08 23:13:16 +0000449 QualType FnType = BPT->getPointeeType();
450
Anders Carlssonacfde802009-02-12 00:39:25 +0000451 // And the rest of the arguments.
John McCall183700f2009-09-21 23:43:11 +0000452 EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(),
Anders Carlsson782f3972009-04-08 23:13:16 +0000453 E->arg_begin(), E->arg_end());
Mike Stumpa5448542009-02-13 15:32:32 +0000454
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000455 // Load the function.
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000456 llvm::Value *Func = Builder.CreateLoad(FuncPtr, "tmp");
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000457
John McCall04a67a62010-02-05 21:31:56 +0000458 const FunctionType *FuncTy = FnType->getAs<FunctionType>();
459 QualType ResultType = FuncTy->getResultType();
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000460
Mike Stump1eb44332009-09-09 15:08:12 +0000461 const CGFunctionInfo &FnInfo =
John McCall04a67a62010-02-05 21:31:56 +0000462 CGM.getTypes().getFunctionInfo(ResultType, Args, FuncTy->getCallConv(),
463 FuncTy->getNoReturnAttr());
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000465 // Cast the function pointer to the right type.
Mike Stump1eb44332009-09-09 15:08:12 +0000466 const llvm::Type *BlockFTy =
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000467 CGM.getTypes().GetFunctionType(FnInfo, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Owen Anderson96e0fc72009-07-29 22:16:19 +0000469 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000470 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Anders Carlssonacfde802009-02-12 00:39:25 +0000472 // And call the block.
Anders Carlssona1736c02009-12-24 21:13:40 +0000473 return EmitCall(FnInfo, Func, ReturnValue, Args);
Anders Carlssonacfde802009-02-12 00:39:25 +0000474}
Anders Carlssond5cab542009-02-12 17:55:02 +0000475
Ken Dyck199c3d62010-01-11 17:06:35 +0000476CharUnits CodeGenFunction::AllocateBlockDecl(const BlockDeclRefExpr *E) {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000477 const ValueDecl *VD = E->getDecl();
Ken Dyck199c3d62010-01-11 17:06:35 +0000478 CharUnits &offset = BlockDecls[VD];
Mike Stumpdab514f2009-03-04 03:23:46 +0000479
Mike Stumpdab514f2009-03-04 03:23:46 +0000480 // See if we have already allocated an offset for this variable.
Ken Dyck199c3d62010-01-11 17:06:35 +0000481 if (offset.isPositive())
Mike Stumpea26cb52009-10-21 03:49:08 +0000482 return offset;
483
484 // Don't run the expensive check, unless we have to.
Mike Stump083c25e2009-10-22 00:49:09 +0000485 if (!BlockHasCopyDispose)
486 if (E->isByRef()
487 || BlockRequiresCopying(E->getType()))
488 BlockHasCopyDispose = true;
Mike Stumpea26cb52009-10-21 03:49:08 +0000489
490 // if not, allocate one now.
491 offset = getBlockOffset(E);
492
493 return offset;
494}
495
496llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
497 const ValueDecl *VD = E->getDecl();
Ken Dyck199c3d62010-01-11 17:06:35 +0000498 CharUnits offset = AllocateBlockDecl(E);
Mike Stumpea26cb52009-10-21 03:49:08 +0000499
Mike Stumpdab514f2009-03-04 03:23:46 +0000500
501 llvm::Value *BlockLiteral = LoadBlockStruct();
502 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
Mike Stumpea26cb52009-10-21 03:49:08 +0000503 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Ken Dyck199c3d62010-01-11 17:06:35 +0000504 offset.getQuantity()),
Mike Stump58a85142009-03-04 22:48:06 +0000505 "block.literal");
Mike Stumpdab514f2009-03-04 03:23:46 +0000506 if (E->isByRef()) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000507 const llvm::Type *PtrStructTy
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000508 = llvm::PointerType::get(BuildByRefType(VD), 0);
Mike Stumpa8b60c92009-03-21 21:00:35 +0000509 // The block literal will need a copy/destroy helper.
510 BlockHasCopyDispose = true;
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000511
512 const llvm::Type *Ty = PtrStructTy;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000513 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000514 V = Builder.CreateBitCast(V, Ty);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000515 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000516 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000517 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000518 V = Builder.CreateBitCast(V, PtrStructTy);
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000519 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
520 VD->getNameAsString());
Mike Stumpdab514f2009-03-04 03:23:46 +0000521 } else {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000522 const llvm::Type *Ty = CGM.getTypes().ConvertType(VD->getType());
523
Owen Anderson96e0fc72009-07-29 22:16:19 +0000524 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000525 V = Builder.CreateBitCast(V, Ty);
526 }
527 return V;
528}
529
Mike Stump6cc88f72009-03-20 21:53:12 +0000530void CodeGenFunction::BlockForwardSelf() {
531 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
532 ImplicitParamDecl *SelfDecl = OMD->getSelfDecl();
533 llvm::Value *&DMEntry = LocalDeclMap[SelfDecl];
534 if (DMEntry)
535 return;
536 // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care
537 BlockDeclRefExpr *BDRE = new (getContext())
538 BlockDeclRefExpr(SelfDecl,
539 SelfDecl->getType(), SourceLocation(), false);
540 DMEntry = GetAddrOfBlockDecl(BDRE);
541}
542
Mike Stump67a64482009-02-14 22:16:35 +0000543llvm::Constant *
Mike Stump90a90432009-03-04 18:47:42 +0000544BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000545 // Generate the block descriptor.
546 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000547 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
548 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000549
Blaine Garst2a7eb282010-02-23 21:51:17 +0000550 llvm::Constant *DescriptorFields[4];
Mike Stumpa5448542009-02-13 15:32:32 +0000551
Anders Carlssond5cab542009-02-12 17:55:02 +0000552 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000553 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000554
Anders Carlssond5cab542009-02-12 17:55:02 +0000555 // Block literal size. For global blocks we just use the size of the generic
556 // block literal struct.
Ken Dyck687cc4a2010-01-26 13:48:07 +0000557 CharUnits BlockLiteralSize =
558 CGM.GetTargetTypeStoreSize(getGenericBlockLiteralType());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000559 DescriptorFields[1] =
Ken Dyck199c3d62010-01-11 17:06:35 +0000560 llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize.getQuantity());
Blaine Garst2a7eb282010-02-23 21:51:17 +0000561
562 // signature. non-optional ObjC-style method descriptor @encode sequence
563 std::string BlockTypeEncoding;
564 CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
Mike Stumpa5448542009-02-13 15:32:32 +0000565
Blaine Garst2a7eb282010-02-23 21:51:17 +0000566 DescriptorFields[2] = llvm::ConstantExpr::getBitCast(
567 CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty);
568
569 // layout
570 DescriptorFields[3] =
571 llvm::ConstantInt::get(UnsignedLongTy,0);
572
573 // build the structure from the 4 elements
Mike Stumpa5448542009-02-13 15:32:32 +0000574 llvm::Constant *DescriptorStruct =
Blaine Garst2a7eb282010-02-23 21:51:17 +0000575 llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 4, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000576
Anders Carlssond5cab542009-02-12 17:55:02 +0000577 llvm::GlobalVariable *Descriptor =
Owen Anderson1c431b32009-07-08 19:05:04 +0000578 new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000579 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000580 DescriptorStruct, "__block_descriptor_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000581
David Chisnall5e530af2009-11-17 19:33:30 +0000582 int FieldCount = 5;
Anders Carlssond5cab542009-02-12 17:55:02 +0000583 // Generate the constants for the block literal.
David Chisnall5e530af2009-11-17 19:33:30 +0000584
585 std::vector<llvm::Constant*> LiteralFields(FieldCount);
Mike Stumpa5448542009-02-13 15:32:32 +0000586
Mike Stump67a64482009-02-14 22:16:35 +0000587 CodeGenFunction::BlockInfo Info(0, n);
Ken Dyck199c3d62010-01-11 17:06:35 +0000588 CharUnits subBlockSize;
Ken Dyck30a8a272010-01-26 19:13:33 +0000589 CharUnits subBlockAlign;
Mike Stumpa99038c2009-02-28 09:07:16 +0000590 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Daniel Dunbard67b09a2009-03-12 03:07:24 +0000591 bool subBlockHasCopyDispose = false;
Mike Stump7f28a9c2009-03-13 23:34:28 +0000592 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000593 llvm::Function *Fn
Mike Stump6cc88f72009-03-20 21:53:12 +0000594 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000595 subBlockSize,
Mike Stump90a90432009-03-04 18:47:42 +0000596 subBlockAlign,
Mike Stump00470a12009-03-05 08:32:30 +0000597 subBlockDeclRefDecls,
598 subBlockHasCopyDispose);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000599 assert(subBlockSize == BlockLiteralSize
600 && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000601
Anders Carlssond5cab542009-02-12 17:55:02 +0000602 // isa
Mike Stumpf99f1d02009-02-13 17:23:42 +0000603 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000604
Anders Carlssond5cab542009-02-12 17:55:02 +0000605 // Flags
Blaine Garst2a7eb282010-02-23 21:51:17 +0000606 LiteralFields[1] =
607 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_OBJC_TYPE);
Mike Stumpa5448542009-02-13 15:32:32 +0000608
Anders Carlssond5cab542009-02-12 17:55:02 +0000609 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000610 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000611
Anders Carlssond5cab542009-02-12 17:55:02 +0000612 // Function
613 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000614
Anders Carlssond5cab542009-02-12 17:55:02 +0000615 // Descriptor
616 LiteralFields[4] = Descriptor;
David Chisnall5e530af2009-11-17 19:33:30 +0000617
Mike Stumpa5448542009-02-13 15:32:32 +0000618 llvm::Constant *BlockLiteralStruct =
David Chisnall5e530af2009-11-17 19:33:30 +0000619 llvm::ConstantStruct::get(VMContext, LiteralFields, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000620
621 llvm::GlobalVariable *BlockLiteral =
Owen Anderson1c431b32009-07-08 19:05:04 +0000622 new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000623 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000624 BlockLiteralStruct, "__block_literal_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000625
Anders Carlssond5cab542009-02-12 17:55:02 +0000626 return BlockLiteral;
627}
628
Mike Stump4e7a1f72009-02-21 20:00:35 +0000629llvm::Value *CodeGenFunction::LoadBlockStruct() {
Mike Stumpbf1914b2009-10-20 20:30:01 +0000630 llvm::Value *V = Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()],
631 "self");
632 // For now, we codegen based upon byte offsets.
633 return Builder.CreateBitCast(V, PtrToInt8Ty);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000634}
635
Mike Stump00470a12009-03-05 08:32:30 +0000636llvm::Function *
637CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
638 const BlockInfo& Info,
Mike Stump6cc88f72009-03-20 21:53:12 +0000639 const Decl *OuterFuncDecl,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000640 llvm::DenseMap<const Decl*, llvm::Value*> ldm,
Ken Dyck199c3d62010-01-11 17:06:35 +0000641 CharUnits &Size,
Ken Dyck30a8a272010-01-26 19:13:33 +0000642 CharUnits &Align,
Mike Stump00470a12009-03-05 08:32:30 +0000643 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls,
644 bool &subBlockHasCopyDispose) {
Devang Patel963dfbd2009-04-15 21:51:44 +0000645
646 // Check if we should generate debug info for this block.
647 if (CGM.getDebugInfo())
648 DebugInfo = CGM.getDebugInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Mike Stump7f28a9c2009-03-13 23:34:28 +0000650 // Arrange for local static and local extern declarations to appear
651 // to be local to this function as well, as they are directly referenced
652 // in a block.
653 for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
654 i != ldm.end();
655 ++i) {
656 const VarDecl *VD = dyn_cast<VarDecl>(i->first);
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000658 if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage())
Mike Stump7f28a9c2009-03-13 23:34:28 +0000659 LocalDeclMap[VD] = i->second;
660 }
661
Ken Dyck687cc4a2010-01-26 13:48:07 +0000662 BlockOffset =
663 CGM.GetTargetTypeStoreSize(CGM.getGenericBlockLiteralType());
Ken Dyck30a8a272010-01-26 19:13:33 +0000664 BlockAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Eli Friedman48f91222009-03-28 03:24:54 +0000665
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000666 const FunctionType *BlockFunctionType = BExpr->getFunctionType();
667 QualType ResultType;
John McCall04a67a62010-02-05 21:31:56 +0000668 CallingConv CC = BlockFunctionType->getCallConv();
669 bool NoReturn = BlockFunctionType->getNoReturnAttr();
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000670 bool IsVariadic;
Mike Stump1eb44332009-09-09 15:08:12 +0000671 if (const FunctionProtoType *FTy =
Fariborz Jahanianda0895d2009-04-11 18:54:57 +0000672 dyn_cast<FunctionProtoType>(BlockFunctionType)) {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000673 ResultType = FTy->getResultType();
674 IsVariadic = FTy->isVariadic();
Mike Stumpb3589f42009-07-30 22:28:39 +0000675 } else {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000676 // K&R style block.
677 ResultType = BlockFunctionType->getResultType();
678 IsVariadic = false;
679 }
Mike Stumpa5448542009-02-13 15:32:32 +0000680
Anders Carlssond5cab542009-02-12 17:55:02 +0000681 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000682
Mike Stumpea26cb52009-10-21 03:49:08 +0000683 CurFuncDecl = OuterFuncDecl;
684
Chris Lattner161d36d2009-02-28 19:01:03 +0000685 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlssond5cab542009-02-12 17:55:02 +0000686
Mike Stumpea26cb52009-10-21 03:49:08 +0000687 IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor");
Mike Stumpadaaad32009-10-20 02:12:22 +0000688
Mike Stumpbfbd5df2009-10-21 18:24:18 +0000689 // Allocate all BlockDeclRefDecls, so we can calculate the right ParmTy below.
Mike Stump0298d382009-10-21 22:02:08 +0000690 AllocateAllBlockDeclRefs(Info, this);
Mike Stumpea26cb52009-10-21 03:49:08 +0000691
Mike Stump083c25e2009-10-22 00:49:09 +0000692 QualType ParmTy = getContext().getBlockParmType(BlockHasCopyDispose,
693 BlockDeclRefDecls);
Anders Carlssond5cab542009-02-12 17:55:02 +0000694 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000695 ImplicitParamDecl *SelfDecl =
Anders Carlssond5cab542009-02-12 17:55:02 +0000696 ImplicitParamDecl::Create(getContext(), 0,
Mike Stumpadaaad32009-10-20 02:12:22 +0000697 SourceLocation(), II,
698 ParmTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000699
Anders Carlssond5cab542009-02-12 17:55:02 +0000700 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump4e7a1f72009-02-21 20:00:35 +0000701 BlockStructDecl = SelfDecl;
Mike Stumpa5448542009-02-13 15:32:32 +0000702
Steve Naroffe78b8092009-03-13 16:56:44 +0000703 for (BlockDecl::param_const_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000704 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000705 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000706
707 const CGFunctionInfo &FI =
John McCall04a67a62010-02-05 21:31:56 +0000708 CGM.getTypes().getFunctionInfo(ResultType, Args, CC, NoReturn);
Anders Carlssond5cab542009-02-12 17:55:02 +0000709
Anders Carlssond5cab542009-02-12 17:55:02 +0000710 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000711 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic);
Mike Stumpa5448542009-02-13 15:32:32 +0000712
713 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000714 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000715 llvm::Twine("__") + Info.Name + "_block_invoke_",
Anders Carlssond5cab542009-02-12 17:55:02 +0000716 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000717
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000718 CGM.SetInternalFunctionAttributes(BD, Fn, FI);
719
Chris Lattner4863db42009-04-23 07:18:56 +0000720 StartFunction(BD, ResultType, Fn, Args,
Chris Lattner161d36d2009-02-28 19:01:03 +0000721 BExpr->getBody()->getLocEnd());
Mike Stumpb1a6e682009-09-30 02:43:10 +0000722
Chris Lattner4863db42009-04-23 07:18:56 +0000723 CurFuncDecl = OuterFuncDecl;
Chris Lattnerb5437d22009-04-23 05:30:27 +0000724 CurCodeDecl = BD;
Mike Stumpb289b3f2009-10-01 22:29:41 +0000725
726 // Save a spot to insert the debug information for all the BlockDeclRefDecls.
727 llvm::BasicBlock *entry = Builder.GetInsertBlock();
728 llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint();
729 --entry_ptr;
730
Chris Lattner161d36d2009-02-28 19:01:03 +0000731 EmitStmt(BExpr->getBody());
Mike Stumpb289b3f2009-10-01 22:29:41 +0000732
Mike Stumpde8c5c72009-10-01 00:27:30 +0000733 // Remember where we were...
734 llvm::BasicBlock *resume = Builder.GetInsertBlock();
Mike Stumpb289b3f2009-10-01 22:29:41 +0000735
Mike Stumpde8c5c72009-10-01 00:27:30 +0000736 // Go back to the entry.
Mike Stumpb289b3f2009-10-01 22:29:41 +0000737 ++entry_ptr;
738 Builder.SetInsertPoint(entry, entry_ptr);
739
Mike Stumpb1a6e682009-09-30 02:43:10 +0000740 if (CGDebugInfo *DI = getDebugInfo()) {
Mike Stumpb1a6e682009-09-30 02:43:10 +0000741 // Emit debug information for all the BlockDeclRefDecls.
Chris Lattner14b1a362010-01-25 03:29:35 +0000742 for (unsigned i = 0, e = BlockDeclRefDecls.size(); i != e; ++i) {
743 if (const BlockDeclRefExpr *BDRE =
744 dyn_cast<BlockDeclRefExpr>(BlockDeclRefDecls[i])) {
Mike Stumpb1a6e682009-09-30 02:43:10 +0000745 const ValueDecl *D = BDRE->getDecl();
746 DI->setLocation(D->getLocation());
747 DI->EmitDeclareOfBlockDeclRefVariable(BDRE,
748 LocalDeclMap[getBlockStructDecl()],
749 Builder, this);
750 }
751 }
Mike Stumpb1a6e682009-09-30 02:43:10 +0000752 }
Mike Stumpde8c5c72009-10-01 00:27:30 +0000753 // And resume where we left off.
754 if (resume == 0)
755 Builder.ClearInsertionPoint();
756 else
757 Builder.SetInsertPoint(resume);
Mike Stumpb1a6e682009-09-30 02:43:10 +0000758
Chris Lattner161d36d2009-02-28 19:01:03 +0000759 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlssond5cab542009-02-12 17:55:02 +0000760
Mike Stump8a2b4b12009-02-25 23:33:13 +0000761 // The runtime needs a minimum alignment of a void *.
Ken Dyck30a8a272010-01-26 19:13:33 +0000762 CharUnits MinAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Ken Dyck199c3d62010-01-11 17:06:35 +0000763 BlockOffset = CharUnits::fromQuantity(
Ken Dyck30a8a272010-01-26 19:13:33 +0000764 llvm::RoundUpToAlignment(BlockOffset.getQuantity(),
765 MinAlign.getQuantity()));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000766
Mike Stump4e7a1f72009-02-21 20:00:35 +0000767 Size = BlockOffset;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000768 Align = BlockAlign;
769 subBlockDeclRefDecls = BlockDeclRefDecls;
Mike Stump00470a12009-03-05 08:32:30 +0000770 subBlockHasCopyDispose |= BlockHasCopyDispose;
Anders Carlssond5cab542009-02-12 17:55:02 +0000771 return Fn;
772}
Mike Stumpa99038c2009-02-28 09:07:16 +0000773
Ken Dyck199c3d62010-01-11 17:06:35 +0000774CharUnits BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
Mike Stumpa99038c2009-02-28 09:07:16 +0000775 const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
776
Ken Dyck199c3d62010-01-11 17:06:35 +0000777 CharUnits Size = getContext().getTypeSizeInChars(D->getType());
Ken Dyck8b752f12010-01-27 17:10:57 +0000778 CharUnits Align = getContext().getDeclAlign(D);
Mike Stumpa99038c2009-02-28 09:07:16 +0000779
780 if (BDRE->isByRef()) {
Ken Dyck199c3d62010-01-11 17:06:35 +0000781 Size = getContext().getTypeSizeInChars(getContext().VoidPtrTy);
Ken Dyck30a8a272010-01-26 19:13:33 +0000782 Align = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Mike Stumpa99038c2009-02-28 09:07:16 +0000783 }
784
Ken Dyck30a8a272010-01-26 19:13:33 +0000785 assert ((Align.isPositive()) && "alignment must be 1 byte or more");
Mike Stumpa99038c2009-02-28 09:07:16 +0000786
Ken Dyck199c3d62010-01-11 17:06:35 +0000787 CharUnits OldOffset = BlockOffset;
Mike Stumpa99038c2009-02-28 09:07:16 +0000788
789 // Ensure proper alignment, even if it means we have to have a gap
Ken Dyck199c3d62010-01-11 17:06:35 +0000790 BlockOffset = CharUnits::fromQuantity(
Ken Dyck30a8a272010-01-26 19:13:33 +0000791 llvm::RoundUpToAlignment(BlockOffset.getQuantity(), Align.getQuantity()));
Mike Stumpa99038c2009-02-28 09:07:16 +0000792 BlockAlign = std::max(Align, BlockAlign);
Mike Stump00470a12009-03-05 08:32:30 +0000793
Ken Dyck199c3d62010-01-11 17:06:35 +0000794 CharUnits Pad = BlockOffset - OldOffset;
795 if (Pad.isPositive()) {
796 llvm::ArrayType::get(llvm::Type::getInt8Ty(VMContext), Pad.getQuantity());
Mike Stumpa99038c2009-02-28 09:07:16 +0000797 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
Ken Dyck199c3d62010-01-11 17:06:35 +0000798 llvm::APInt(32,
799 Pad.getQuantity()),
Mike Stumpa99038c2009-02-28 09:07:16 +0000800 ArrayType::Normal, 0);
801 ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000802 0, QualType(PadTy), 0, VarDecl::None);
Mike Stumpa99038c2009-02-28 09:07:16 +0000803 Expr *E;
804 E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
Douglas Gregor0da76df2009-11-23 11:41:28 +0000805 SourceLocation());
Mike Stumpa99038c2009-02-28 09:07:16 +0000806 BlockDeclRefDecls.push_back(E);
807 }
808 BlockDeclRefDecls.push_back(BDRE);
809
810 BlockOffset += Size;
811 return BlockOffset-Size;
812}
Mike Stumpdab514f2009-03-04 03:23:46 +0000813
Mike Stump08920992009-03-07 02:35:30 +0000814llvm::Constant *BlockFunction::
815GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000816 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000817 QualType R = getContext().VoidTy;
818
819 FunctionArgList Args;
820 // FIXME: This leaks
Mike Stump08920992009-03-07 02:35:30 +0000821 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +0000822 ImplicitParamDecl::Create(getContext(), 0,
823 SourceLocation(), 0,
Mike Stump08920992009-03-07 02:35:30 +0000824 getContext().getPointerType(getContext().VoidTy));
825 Args.push_back(std::make_pair(Dst, Dst->getType()));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000826 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +0000827 ImplicitParamDecl::Create(getContext(), 0,
828 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000829 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000830 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Mike Stumpa4f668f2009-03-06 01:33:24 +0000832 const CGFunctionInfo &FI =
John McCall04a67a62010-02-05 21:31:56 +0000833 CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000834
Mike Stump3899a7f2009-06-05 23:26:36 +0000835 // FIXME: We'd like to put these into a mergable by content, with
836 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000837 CodeGenTypes &Types = CGM.getTypes();
838 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
839
840 llvm::Function *Fn =
841 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000842 "__copy_helper_block_", &CGM.getModule());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000843
844 IdentifierInfo *II
845 = &CGM.getContext().Idents.get("__copy_helper_block_");
846
847 FunctionDecl *FD = FunctionDecl::Create(getContext(),
848 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000849 SourceLocation(), II, R, 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000850 FunctionDecl::Static, false,
851 true);
852 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump08920992009-03-07 02:35:30 +0000853
854 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
855 llvm::Type *PtrPtrT;
Mike Stump08920992009-03-07 02:35:30 +0000856
Mike Stumpb7477cf2009-04-10 18:52:28 +0000857 if (NoteForHelperp) {
858 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump08920992009-03-07 02:35:30 +0000859
Owen Anderson96e0fc72009-07-29 22:16:19 +0000860 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000861 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
862 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump08920992009-03-07 02:35:30 +0000863
Mike Stumpb7477cf2009-04-10 18:52:28 +0000864 llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000865 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000866 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000867 DstObj = Builder.CreateBitCast(DstObj, PtrPtrT);
868 DstObj = Builder.CreateLoad(DstObj);
Mike Stump08920992009-03-07 02:35:30 +0000869
Mike Stumpb7477cf2009-04-10 18:52:28 +0000870 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
871 int flag = NoteForHelper[i].flag;
872 int index = NoteForHelper[i].index;
Mike Stump08920992009-03-07 02:35:30 +0000873
Mike Stumpb7477cf2009-04-10 18:52:28 +0000874 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
875 || NoteForHelper[i].RequiresCopying) {
876 llvm::Value *Srcv = SrcObj;
877 Srcv = Builder.CreateStructGEP(Srcv, index);
878 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000879 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000880 Srcv = Builder.CreateLoad(Srcv);
881
882 llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
883 Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
884
Owen Anderson0032b272009-08-13 21:57:51 +0000885 llvm::Value *N = llvm::ConstantInt::get(
886 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000887 llvm::Value *F = getBlockObjectAssign();
888 Builder.CreateCall3(F, Dstv, Srcv, N);
889 }
Mike Stump08920992009-03-07 02:35:30 +0000890 }
891 }
892
Mike Stumpa4f668f2009-03-06 01:33:24 +0000893 CGF.FinishFunction();
894
Owen Anderson3c4972d2009-07-29 18:54:39 +0000895 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000896}
897
Mike Stumpcf62d392009-03-06 18:42:23 +0000898llvm::Constant *BlockFunction::
Mike Stump08920992009-03-07 02:35:30 +0000899GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
900 const llvm::StructType* T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000901 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000902 QualType R = getContext().VoidTy;
903
904 FunctionArgList Args;
905 // FIXME: This leaks
906 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +0000907 ImplicitParamDecl::Create(getContext(), 0,
908 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000909 getContext().getPointerType(getContext().VoidTy));
910
911 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Mike Stumpa4f668f2009-03-06 01:33:24 +0000913 const CGFunctionInfo &FI =
John McCall04a67a62010-02-05 21:31:56 +0000914 CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000915
Mike Stump3899a7f2009-06-05 23:26:36 +0000916 // FIXME: We'd like to put these into a mergable by content, with
917 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000918 CodeGenTypes &Types = CGM.getTypes();
919 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
920
921 llvm::Function *Fn =
922 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000923 "__destroy_helper_block_", &CGM.getModule());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000924
925 IdentifierInfo *II
926 = &CGM.getContext().Idents.get("__destroy_helper_block_");
927
928 FunctionDecl *FD = FunctionDecl::Create(getContext(),
929 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000930 SourceLocation(), II, R, 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000931 FunctionDecl::Static, false,
932 true);
933 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1edf6b62009-03-07 02:53:18 +0000934
Mike Stumpb7477cf2009-04-10 18:52:28 +0000935 if (NoteForHelperp) {
936 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump1edf6b62009-03-07 02:53:18 +0000937
Mike Stumpb7477cf2009-04-10 18:52:28 +0000938 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
939 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000940 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000941 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
942 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump1edf6b62009-03-07 02:53:18 +0000943
Mike Stumpb7477cf2009-04-10 18:52:28 +0000944 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
945 int flag = NoteForHelper[i].flag;
946 int index = NoteForHelper[i].index;
Mike Stump1edf6b62009-03-07 02:53:18 +0000947
Mike Stumpb7477cf2009-04-10 18:52:28 +0000948 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
949 || NoteForHelper[i].RequiresCopying) {
950 llvm::Value *Srcv = SrcObj;
951 Srcv = Builder.CreateStructGEP(Srcv, index);
952 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000953 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000954 Srcv = Builder.CreateLoad(Srcv);
955
956 BuildBlockRelease(Srcv, flag);
957 }
Mike Stump1edf6b62009-03-07 02:53:18 +0000958 }
959 }
960
Mike Stumpa4f668f2009-03-06 01:33:24 +0000961 CGF.FinishFunction();
962
Owen Anderson3c4972d2009-07-29 18:54:39 +0000963 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000964}
965
Mike Stump08920992009-03-07 02:35:30 +0000966llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000967 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump08920992009-03-07 02:35:30 +0000968 return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
969 T, NoteForHelper);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000970}
971
Mike Stump08920992009-03-07 02:35:30 +0000972llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000973 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stump08920992009-03-07 02:35:30 +0000974 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000975 T, NoteForHelperp);
Mike Stumpdab514f2009-03-04 03:23:46 +0000976}
Mike Stump797b6322009-03-05 01:23:13 +0000977
Mike Stumpee094222009-03-06 06:12:24 +0000978llvm::Constant *BlockFunction::
979GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
Mike Stump45031c02009-03-06 02:29:21 +0000980 QualType R = getContext().VoidTy;
981
982 FunctionArgList Args;
983 // FIXME: This leaks
Mike Stumpee094222009-03-06 06:12:24 +0000984 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +0000985 ImplicitParamDecl::Create(getContext(), 0,
986 SourceLocation(), 0,
Mike Stumpee094222009-03-06 06:12:24 +0000987 getContext().getPointerType(getContext().VoidTy));
988 Args.push_back(std::make_pair(Dst, Dst->getType()));
989
990 // FIXME: This leaks
Mike Stump45031c02009-03-06 02:29:21 +0000991 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +0000992 ImplicitParamDecl::Create(getContext(), 0,
993 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +0000994 getContext().getPointerType(getContext().VoidTy));
Mike Stump45031c02009-03-06 02:29:21 +0000995 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Mike Stump45031c02009-03-06 02:29:21 +0000997 const CGFunctionInfo &FI =
John McCall04a67a62010-02-05 21:31:56 +0000998 CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false);
Mike Stump45031c02009-03-06 02:29:21 +0000999
Mike Stump45031c02009-03-06 02:29:21 +00001000 CodeGenTypes &Types = CGM.getTypes();
1001 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1002
Mike Stump3899a7f2009-06-05 23:26:36 +00001003 // FIXME: We'd like to put these into a mergable by content, with
1004 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001005 llvm::Function *Fn =
1006 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001007 "__Block_byref_id_object_copy_", &CGM.getModule());
Mike Stump45031c02009-03-06 02:29:21 +00001008
1009 IdentifierInfo *II
1010 = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
1011
1012 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1013 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001014 SourceLocation(), II, R, 0,
Mike Stump45031c02009-03-06 02:29:21 +00001015 FunctionDecl::Static, false,
1016 true);
1017 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpee094222009-03-06 06:12:24 +00001018
1019 // dst->x
1020 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001021 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001022 V = Builder.CreateLoad(V);
Mike Stumpee094222009-03-06 06:12:24 +00001023 V = Builder.CreateStructGEP(V, 6, "x");
1024 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
1025
1026 // src->x
1027 V = CGF.GetAddrOfLocalVar(Src);
1028 V = Builder.CreateLoad(V);
1029 V = Builder.CreateBitCast(V, T);
1030 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001031 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpee094222009-03-06 06:12:24 +00001032 llvm::Value *SrcObj = Builder.CreateLoad(V);
Mike Stump1eb44332009-09-09 15:08:12 +00001033
Mike Stumpee094222009-03-06 06:12:24 +00001034 flag |= BLOCK_BYREF_CALLER;
1035
Owen Anderson0032b272009-08-13 21:57:51 +00001036 llvm::Value *N = llvm::ConstantInt::get(
1037 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpee094222009-03-06 06:12:24 +00001038 llvm::Value *F = getBlockObjectAssign();
1039 Builder.CreateCall3(F, DstObj, SrcObj, N);
1040
Mike Stump45031c02009-03-06 02:29:21 +00001041 CGF.FinishFunction();
1042
Owen Anderson3c4972d2009-07-29 18:54:39 +00001043 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001044}
1045
Mike Stump1851b682009-03-06 04:53:30 +00001046llvm::Constant *
1047BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
1048 int flag) {
Mike Stump45031c02009-03-06 02:29:21 +00001049 QualType R = getContext().VoidTy;
1050
1051 FunctionArgList Args;
1052 // FIXME: This leaks
1053 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001054 ImplicitParamDecl::Create(getContext(), 0,
1055 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001056 getContext().getPointerType(getContext().VoidTy));
1057
1058 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Mike Stump45031c02009-03-06 02:29:21 +00001060 const CGFunctionInfo &FI =
John McCall04a67a62010-02-05 21:31:56 +00001061 CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false);
Mike Stump45031c02009-03-06 02:29:21 +00001062
Mike Stump45031c02009-03-06 02:29:21 +00001063 CodeGenTypes &Types = CGM.getTypes();
1064 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1065
Mike Stump3899a7f2009-06-05 23:26:36 +00001066 // FIXME: We'd like to put these into a mergable by content, with
1067 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001068 llvm::Function *Fn =
1069 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001070 "__Block_byref_id_object_dispose_",
Mike Stump45031c02009-03-06 02:29:21 +00001071 &CGM.getModule());
1072
1073 IdentifierInfo *II
1074 = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
1075
1076 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1077 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001078 SourceLocation(), II, R, 0,
Mike Stump45031c02009-03-06 02:29:21 +00001079 FunctionDecl::Static, false,
1080 true);
1081 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1851b682009-03-06 04:53:30 +00001082
1083 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001084 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001085 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001086 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001087 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001088 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001089
Mike Stump1851b682009-03-06 04:53:30 +00001090 flag |= BLOCK_BYREF_CALLER;
1091 BuildBlockRelease(V, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001092 CGF.FinishFunction();
1093
Owen Anderson3c4972d2009-07-29 18:54:39 +00001094 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001095}
1096
Mike Stumpee094222009-03-06 06:12:24 +00001097llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001098 int Flag, unsigned Align) {
1099 // All alignments below that of pointer alignment collapse down to just
Mike Stump3899a7f2009-06-05 23:26:36 +00001100 // pointer alignment, as we always have at least that much alignment to begin
1101 // with.
1102 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001103
Mike Stump3899a7f2009-06-05 23:26:36 +00001104 // As an optimization, we only generate a single function of each kind we
1105 // might need. We need a different one for each alignment and for each
1106 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001107 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1108 llvm::Constant *&Entry = CGM.AssignCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001109 if (Entry)
1110 return Entry;
Chris Lattner10976d92009-12-05 08:21:30 +00001111 return Entry = CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, Flag);
Mike Stump45031c02009-03-06 02:29:21 +00001112}
1113
Mike Stump1851b682009-03-06 04:53:30 +00001114llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001115 int Flag,
Mike Stump3899a7f2009-06-05 23:26:36 +00001116 unsigned Align) {
1117 // All alignments below that of pointer alignment collpase down to just
1118 // pointer alignment, as we always have at least that much alignment to begin
1119 // with.
1120 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001121
Mike Stump3899a7f2009-06-05 23:26:36 +00001122 // As an optimization, we only generate a single function of each kind we
1123 // might need. We need a different one for each alignment and for each
1124 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001125 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1126 llvm::Constant *&Entry = CGM.DestroyCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001127 if (Entry)
1128 return Entry;
Chris Lattner10976d92009-12-05 08:21:30 +00001129 return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, Flag);
Mike Stump45031c02009-03-06 02:29:21 +00001130}
1131
Mike Stump797b6322009-03-05 01:23:13 +00001132llvm::Value *BlockFunction::getBlockObjectDispose() {
1133 if (CGM.BlockObjectDispose == 0) {
1134 const llvm::FunctionType *FTy;
1135 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001136 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stump797b6322009-03-05 01:23:13 +00001137 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001138 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson96e0fc72009-07-29 22:16:19 +00001139 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stump00470a12009-03-05 08:32:30 +00001140 CGM.BlockObjectDispose
Mike Stump797b6322009-03-05 01:23:13 +00001141 = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
1142 }
1143 return CGM.BlockObjectDispose;
1144}
1145
Mike Stumpee094222009-03-06 06:12:24 +00001146llvm::Value *BlockFunction::getBlockObjectAssign() {
1147 if (CGM.BlockObjectAssign == 0) {
1148 const llvm::FunctionType *FTy;
1149 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001150 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stumpee094222009-03-06 06:12:24 +00001151 ArgTys.push_back(PtrToInt8Ty);
1152 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001153 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson96e0fc72009-07-29 22:16:19 +00001154 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stumpee094222009-03-06 06:12:24 +00001155 CGM.BlockObjectAssign
1156 = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
1157 }
1158 return CGM.BlockObjectAssign;
1159}
1160
Mike Stump1851b682009-03-06 04:53:30 +00001161void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Mike Stump797b6322009-03-05 01:23:13 +00001162 llvm::Value *F = getBlockObjectDispose();
Mike Stump1851b682009-03-06 04:53:30 +00001163 llvm::Value *N;
Mike Stump797b6322009-03-05 01:23:13 +00001164 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001165 N = llvm::ConstantInt::get(llvm::Type::getInt32Ty(V->getContext()), flag);
Mike Stump797b6322009-03-05 01:23:13 +00001166 Builder.CreateCall2(F, V, N);
1167}
Mike Stump00470a12009-03-05 08:32:30 +00001168
1169ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stump08920992009-03-07 02:35:30 +00001170
1171BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1172 CGBuilderTy &B)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001173 : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) {
Owen Anderson0032b272009-08-13 21:57:51 +00001174 PtrToInt8Ty = llvm::PointerType::getUnqual(
1175 llvm::Type::getInt8Ty(VMContext));
Mike Stump08920992009-03-07 02:35:30 +00001176
1177 BlockHasCopyDispose = false;
1178}