blob: f157f2f406e07c4b472c03c9a6f7a8a0bf48943e [file] [log] [blame]
Anders Carlssonacfde802009-02-12 00:39:25 +00001//===--- CGBlocks.cpp - Emit LLVM Code for declarations -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit blocks.
11//
12//===----------------------------------------------------------------------===//
13
Mike Stumpb1a6e682009-09-30 02:43:10 +000014#include "CGDebugInfo.h"
Anders Carlssonacfde802009-02-12 00:39:25 +000015#include "CodeGenFunction.h"
Fariborz Jahanian263c4de2010-02-10 23:34:57 +000016#include "CGObjCRuntime.h"
Anders Carlssonacfde802009-02-12 00:39:25 +000017#include "CodeGenModule.h"
Mike Stump6cc88f72009-03-20 21:53:12 +000018#include "clang/AST/DeclObjC.h"
Anders Carlssonacfde802009-02-12 00:39:25 +000019#include "llvm/Module.h"
Benjamin Kramer6876fe62010-03-31 15:04:05 +000020#include "llvm/ADT/SmallSet.h"
Anders Carlssond5cab542009-02-12 17:55:02 +000021#include "llvm/Target/TargetData.h"
Anders Carlssonacfde802009-02-12 00:39:25 +000022#include <algorithm>
Torok Edwinf42e4a62009-08-24 13:25:12 +000023
Anders Carlssonacfde802009-02-12 00:39:25 +000024using namespace clang;
25using namespace CodeGen;
26
Mike Stumpcf62d392009-03-06 18:42:23 +000027llvm::Constant *CodeGenFunction::
Blaine Garst2a7eb282010-02-23 21:51:17 +000028BuildDescriptorBlockDecl(const BlockExpr *BE, bool BlockHasCopyDispose, CharUnits Size,
Mike Stumpa803b0e2009-03-25 17:58:24 +000029 const llvm::StructType* Ty,
Mike Stump08920992009-03-07 02:35:30 +000030 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump56129b12009-02-13 16:55:51 +000031 const llvm::Type *UnsignedLongTy
32 = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpe5fee252009-02-13 16:19:19 +000033 llvm::Constant *C;
34 std::vector<llvm::Constant*> Elts;
35
36 // reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +000037 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpe5fee252009-02-13 16:19:19 +000038 Elts.push_back(C);
39
40 // Size
Mike Stumpd6840002009-02-21 20:07:44 +000041 // FIXME: What is the right way to say this doesn't fit? We should give
42 // a user diagnostic in that case. Better fix would be to change the
43 // API to size_t.
Ken Dyck199c3d62010-01-11 17:06:35 +000044 C = llvm::ConstantInt::get(UnsignedLongTy, Size.getQuantity());
Mike Stumpe5fee252009-02-13 16:19:19 +000045 Elts.push_back(C);
46
Blaine Garst2a7eb282010-02-23 21:51:17 +000047 // optional copy/dispose helpers
Mike Stumpe5fee252009-02-13 16:19:19 +000048 if (BlockHasCopyDispose) {
49 // copy_func_helper_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000050 Elts.push_back(BuildCopyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000051
52 // destroy_func_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000053 Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000054 }
55
Blaine Garst2a7eb282010-02-23 21:51:17 +000056 // Signature. non-optional ObjC-style method descriptor @encode sequence
57 std::string BlockTypeEncoding;
58 CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
59
60 Elts.push_back(llvm::ConstantExpr::getBitCast(
61 CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty));
62
63 // Layout.
64 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
65 Elts.push_back(C);
66
Nick Lewycky0d36dd22009-09-19 20:00:52 +000067 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stumpe5fee252009-02-13 16:19:19 +000068
Owen Anderson1c431b32009-07-08 19:05:04 +000069 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Mike Stumpe5fee252009-02-13 16:19:19 +000070 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +000071 C, "__block_descriptor_tmp");
Mike Stumpe5fee252009-02-13 16:19:19 +000072 return C;
73}
74
Mike Stump2a998142009-03-04 18:17:45 +000075llvm::Constant *BlockModule::getNSConcreteGlobalBlock() {
Chris Lattnere2f79b62009-05-13 02:50:56 +000076 if (NSConcreteGlobalBlock == 0)
Mike Stump1eb44332009-09-09 15:08:12 +000077 NSConcreteGlobalBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
Chris Lattnere2f79b62009-05-13 02:50:56 +000078 "_NSConcreteGlobalBlock");
Mike Stumpf99f1d02009-02-13 17:23:42 +000079 return NSConcreteGlobalBlock;
80}
81
Mike Stump2a998142009-03-04 18:17:45 +000082llvm::Constant *BlockModule::getNSConcreteStackBlock() {
Chris Lattnere2f79b62009-05-13 02:50:56 +000083 if (NSConcreteStackBlock == 0)
Mike Stump1eb44332009-09-09 15:08:12 +000084 NSConcreteStackBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
Chris Lattnere2f79b62009-05-13 02:50:56 +000085 "_NSConcreteStackBlock");
Mike Stump59c5b112009-02-13 19:29:27 +000086 return NSConcreteStackBlock;
87}
88
Mike Stump38e16272009-10-21 22:01:24 +000089static void CollectBlockDeclRefInfo(
90 const Stmt *S, CodeGenFunction::BlockInfo &Info,
91 llvm::SmallSet<const DeclContext *, 16> &InnerContexts) {
Anders Carlsson4de9fce2009-03-01 01:09:12 +000092 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
93 I != E; ++I)
Daniel Dunbar82573ee2009-03-02 07:00:57 +000094 if (*I)
Mike Stump38e16272009-10-21 22:01:24 +000095 CollectBlockDeclRefInfo(*I, Info, InnerContexts);
Mike Stump00470a12009-03-05 08:32:30 +000096
Mike Stumpea26cb52009-10-21 03:49:08 +000097 // We want to ensure we walk down into block literals so we can find
98 // all nested BlockDeclRefExprs.
Mike Stump38e16272009-10-21 22:01:24 +000099 if (const BlockExpr *BE = dyn_cast<BlockExpr>(S)) {
100 InnerContexts.insert(cast<DeclContext>(BE->getBlockDecl()));
101 CollectBlockDeclRefInfo(BE->getBody(), Info, InnerContexts);
102 }
Mike Stumpea26cb52009-10-21 03:49:08 +0000103
Mike Stump38e16272009-10-21 22:01:24 +0000104 if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) {
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000105 // FIXME: Handle enums.
Mike Stump38e16272009-10-21 22:01:24 +0000106 if (isa<FunctionDecl>(BDRE->getDecl()))
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000107 return;
Mike Stump00470a12009-03-05 08:32:30 +0000108
Mike Stump38e16272009-10-21 22:01:24 +0000109 // Only Decls that escape are added.
110 if (!InnerContexts.count(BDRE->getDecl()->getDeclContext()))
111 Info.DeclRefs.push_back(BDRE);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000112 }
113}
114
Mike Stump58a85142009-03-04 22:48:06 +0000115/// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be
116/// declared as a global variable instead of on the stack.
Chris Lattnere2f79b62009-05-13 02:50:56 +0000117static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info) {
Mike Stumpea26cb52009-10-21 03:49:08 +0000118 return Info.DeclRefs.empty();
119}
120
121/// AllocateAllBlockDeclRefs - Preallocate all nested BlockDeclRefExprs to
122/// ensure we can generate the debug information for the parameter for the block
123/// invoke function.
124static void AllocateAllBlockDeclRefs(const CodeGenFunction::BlockInfo &Info,
125 CodeGenFunction *CGF) {
Mike Stumpea26cb52009-10-21 03:49:08 +0000126 // FIXME: Also always forward the this pointer in C++ as well.
127
128 for (size_t i = 0; i < Info.DeclRefs.size(); ++i)
129 CGF->AllocateBlockDecl(Info.DeclRefs[i]);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000130}
131
Mike Stump58a85142009-03-04 22:48:06 +0000132// FIXME: Push most into CGM, passing down a few bits, like current function
133// name.
Mike Stump8a2b4b12009-02-25 23:33:13 +0000134llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Mike Stumpe5fee252009-02-13 16:19:19 +0000135
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000136 std::string Name = CurFn->getName();
137 CodeGenFunction::BlockInfo Info(0, Name.c_str());
Mike Stump38e16272009-10-21 22:01:24 +0000138 llvm::SmallSet<const DeclContext *, 16> InnerContexts;
139 InnerContexts.insert(BE->getBlockDecl());
140 CollectBlockDeclRefInfo(BE->getBody(), Info, InnerContexts);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000141
142 // Check if the block can be global.
Mike Stump58a85142009-03-04 22:48:06 +0000143 // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like
144 // to just have one code path. We should move this function into CGM and pass
145 // CGF, then we can just check to see if CGF is 0.
Mike Stumpdab514f2009-03-04 03:23:46 +0000146 if (0 && CanBlockBeGlobal(Info))
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000147 return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
Mike Stump00470a12009-03-05 08:32:30 +0000148
David Chisnall5e530af2009-11-17 19:33:30 +0000149 size_t BlockFields = 5;
150
David Chisnall5e530af2009-11-17 19:33:30 +0000151 std::vector<llvm::Constant*> Elts(BlockFields);
152
Mike Stumpe5fee252009-02-13 16:19:19 +0000153 llvm::Constant *C;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000154 llvm::Value *V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000155
Mike Stumpe5fee252009-02-13 16:19:19 +0000156 {
157 // C = BuildBlockStructInitlist();
Blaine Garsta36e2232010-03-05 01:29:59 +0000158 unsigned int flags = BLOCK_HAS_SIGNATURE;
David Chisnall5e530af2009-11-17 19:33:30 +0000159
Mike Stump00470a12009-03-05 08:32:30 +0000160 // We run this first so that we set BlockHasCopyDispose from the entire
161 // block literal.
162 // __invoke
Ken Dyck199c3d62010-01-11 17:06:35 +0000163 CharUnits subBlockSize;
Ken Dyck30a8a272010-01-26 19:13:33 +0000164 CharUnits subBlockAlign;
Mike Stump00470a12009-03-05 08:32:30 +0000165 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Mike Stumpa803b0e2009-03-25 17:58:24 +0000166 bool subBlockHasCopyDispose = false;
Mike Stump00470a12009-03-05 08:32:30 +0000167 llvm::Function *Fn
Mike Stumpbb304192009-09-24 22:31:14 +0000168 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl,
169 LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000170 subBlockSize,
Mike Stump00470a12009-03-05 08:32:30 +0000171 subBlockAlign,
172 subBlockDeclRefDecls,
Mike Stumpa803b0e2009-03-25 17:58:24 +0000173 subBlockHasCopyDispose);
174 BlockHasCopyDispose |= subBlockHasCopyDispose;
Mike Stump00470a12009-03-05 08:32:30 +0000175 Elts[3] = Fn;
176
Mike Stumpf5408fe2009-05-16 07:57:57 +0000177 // FIXME: Don't use BlockHasCopyDispose, it is set more often then
178 // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); }
Mike Stumpa803b0e2009-03-25 17:58:24 +0000179 if (subBlockHasCopyDispose)
Mike Stumpe5fee252009-02-13 16:19:19 +0000180 flags |= BLOCK_HAS_COPY_DISPOSE;
181
Mike Stump7d6dc4f2009-02-13 20:17:16 +0000182 // __isa
Mike Stump59c5b112009-02-13 19:29:27 +0000183 C = CGM.getNSConcreteStackBlock();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000184 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
Mike Stump00470a12009-03-05 08:32:30 +0000185 Elts[0] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000186
187 // __flags
Blaine Garsta36e2232010-03-05 01:29:59 +0000188 {
189 QualType BPT = BE->getType();
190 const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>();
191 QualType ResultType = ftype->getResultType();
192
193 CallArgList Args;
194 CodeGenTypes &Types = CGM.getTypes();
195 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000196 FunctionType::ExtInfo());
Blaine Garsta36e2232010-03-05 01:29:59 +0000197 if (CGM.ReturnTypeUsesSret(FnInfo))
198 flags |= BLOCK_USE_STRET;
199 }
Mike Stumpe5fee252009-02-13 16:19:19 +0000200 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
201 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000202 C = llvm::ConstantInt::get(IntTy, flags);
Mike Stump00470a12009-03-05 08:32:30 +0000203 Elts[1] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000204
205 // __reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000206 C = llvm::ConstantInt::get(IntTy, 0);
Mike Stump00470a12009-03-05 08:32:30 +0000207 Elts[2] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000208
Mike Stump8a2b4b12009-02-25 23:33:13 +0000209 if (subBlockDeclRefDecls.size() == 0) {
Mike Stumpcf62d392009-03-06 18:42:23 +0000210 // __descriptor
Blaine Garst2a7eb282010-02-23 21:51:17 +0000211 Elts[4] = BuildDescriptorBlockDecl(BE, subBlockHasCopyDispose, subBlockSize,
Mike Stumpea26cb52009-10-21 03:49:08 +0000212 0, 0);
Mike Stumpcf62d392009-03-06 18:42:23 +0000213
Mike Stump5570cfe2009-03-01 20:07:53 +0000214 // Optimize to being a global block.
215 Elts[0] = CGM.getNSConcreteGlobalBlock();
Blaine Garsta36e2232010-03-05 01:29:59 +0000216
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000217 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
Mike Stump5570cfe2009-03-01 20:07:53 +0000218
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000219 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000220
Owen Anderson1c431b32009-07-08 19:05:04 +0000221 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000222 llvm::GlobalValue::InternalLinkage, C,
223 "__block_holder_tmp_" +
224 llvm::Twine(CGM.getGlobalUniqueCount()));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000225 QualType BPT = BE->getType();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000226 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000227 return C;
228 }
Mike Stump00470a12009-03-05 08:32:30 +0000229
David Chisnall5e530af2009-11-17 19:33:30 +0000230 std::vector<const llvm::Type *> Types(BlockFields+subBlockDeclRefDecls.size());
Mike Stump08920992009-03-07 02:35:30 +0000231 for (int i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000232 Types[i] = Elts[i]->getType();
Mike Stump08920992009-03-07 02:35:30 +0000233 Types[4] = PtrToInt8Ty;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000234
Mike Stumpa99038c2009-02-28 09:07:16 +0000235 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
236 const Expr *E = subBlockDeclRefDecls[i];
237 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
238 QualType Ty = E->getType();
Mike Stumpdab514f2009-03-04 03:23:46 +0000239 if (BDRE && BDRE->isByRef()) {
David Chisnall5e530af2009-11-17 19:33:30 +0000240 Types[i+BlockFields] = llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000241 } else
David Chisnall5e530af2009-11-17 19:33:30 +0000242 Types[i+BlockFields] = ConvertType(Ty);
Mike Stumpa99038c2009-02-28 09:07:16 +0000243 }
Mike Stump8a2b4b12009-02-25 23:33:13 +0000244
Owen Anderson47a434f2009-08-05 23:18:46 +0000245 llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000246
247 llvm::AllocaInst *A = CreateTempAlloca(Ty);
Ken Dyck30a8a272010-01-26 19:13:33 +0000248 A->setAlignment(subBlockAlign.getQuantity());
Mike Stump8a2b4b12009-02-25 23:33:13 +0000249 V = A;
250
Mike Stump08920992009-03-07 02:35:30 +0000251 std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size());
252 int helpersize = 0;
253
254 for (unsigned i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000255 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
Mike Stump00470a12009-03-05 08:32:30 +0000256
Mike Stump8a2b4b12009-02-25 23:33:13 +0000257 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
258 {
Mike Stumpa99038c2009-02-28 09:07:16 +0000259 // FIXME: Push const down.
260 Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
261 DeclRefExpr *DR;
262 ValueDecl *VD;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000263
Mike Stumpa99038c2009-02-28 09:07:16 +0000264 DR = dyn_cast<DeclRefExpr>(E);
265 // Skip padding.
266 if (DR) continue;
267
268 BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
269 VD = BDRE->getDecl();
270
David Chisnall5e530af2009-11-17 19:33:30 +0000271 llvm::Value* Addr = Builder.CreateStructGEP(V, i+BlockFields, "tmp");
Mike Stump08920992009-03-07 02:35:30 +0000272 NoteForHelper[helpersize].index = i+5;
Mike Stump39605b42009-09-22 02:12:52 +0000273 NoteForHelper[helpersize].RequiresCopying
274 = BlockRequiresCopying(VD->getType());
Mike Stump08920992009-03-07 02:35:30 +0000275 NoteForHelper[helpersize].flag
Mike Stump39605b42009-09-22 02:12:52 +0000276 = (VD->getType()->isBlockPointerType()
277 ? BLOCK_FIELD_IS_BLOCK
278 : BLOCK_FIELD_IS_OBJECT);
Mike Stump08920992009-03-07 02:35:30 +0000279
Mike Stumpa99038c2009-02-28 09:07:16 +0000280 if (LocalDeclMap[VD]) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000281 if (BDRE->isByRef()) {
Mike Stump08920992009-03-07 02:35:30 +0000282 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
Mike Stumpf4bc3122009-03-07 06:04:31 +0000283 // FIXME: Someone double check this.
284 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000285 llvm::Value *Loc = LocalDeclMap[VD];
286 Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000287 Loc = Builder.CreateLoad(Loc);
Mike Stumpdab514f2009-03-04 03:23:46 +0000288 Builder.CreateStore(Loc, Addr);
Mike Stump08920992009-03-07 02:35:30 +0000289 ++helpersize;
Mike Stumpdab514f2009-03-04 03:23:46 +0000290 continue;
291 } else
John McCalldbd872f2009-12-08 09:08:17 +0000292 E = new (getContext()) DeclRefExpr (VD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000293 VD->getType(),
294 SourceLocation());
Mike Stumpa99038c2009-02-28 09:07:16 +0000295 }
Mike Stumpdab514f2009-03-04 03:23:46 +0000296 if (BDRE->isByRef()) {
Mike Stumpa8b60c92009-03-21 21:00:35 +0000297 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
298 // FIXME: Someone double check this.
299 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpa99038c2009-02-28 09:07:16 +0000300 E = new (getContext())
301 UnaryOperator(E, UnaryOperator::AddrOf,
302 getContext().getPointerType(E->getType()),
303 SourceLocation());
Mike Stumpdab514f2009-03-04 03:23:46 +0000304 }
Mike Stump08920992009-03-07 02:35:30 +0000305 ++helpersize;
Mike Stumpa99038c2009-02-28 09:07:16 +0000306
Mike Stumpa99038c2009-02-28 09:07:16 +0000307 RValue r = EmitAnyExpr(E, Addr, false);
Mike Stumpdab514f2009-03-04 03:23:46 +0000308 if (r.isScalar()) {
309 llvm::Value *Loc = r.getScalarVal();
David Chisnall5e530af2009-11-17 19:33:30 +0000310 const llvm::Type *Ty = Types[i+BlockFields];
Mike Stumpdab514f2009-03-04 03:23:46 +0000311 if (BDRE->isByRef()) {
Mike Stump58a85142009-03-04 22:48:06 +0000312 // E is now the address of the value field, instead, we want the
313 // address of the actual ByRef struct. We optimize this slightly
314 // compared to gcc by not grabbing the forwarding slot as this must
315 // be done during Block_copy for us, and we can postpone the work
316 // until then.
Ken Dyck199c3d62010-01-11 17:06:35 +0000317 CharUnits offset = BlockDecls[BDRE->getDecl()];
Mike Stump00470a12009-03-05 08:32:30 +0000318
Mike Stump58a85142009-03-04 22:48:06 +0000319 llvm::Value *BlockLiteral = LoadBlockStruct();
Mike Stump00470a12009-03-05 08:32:30 +0000320
Mike Stump58a85142009-03-04 22:48:06 +0000321 Loc = Builder.CreateGEP(BlockLiteral,
Mike Stumpea26cb52009-10-21 03:49:08 +0000322 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Ken Dyck199c3d62010-01-11 17:06:35 +0000323 offset.getQuantity()),
Mike Stump58a85142009-03-04 22:48:06 +0000324 "block.literal");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000325 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000326 Loc = Builder.CreateBitCast(Loc, Ty);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000327 Loc = Builder.CreateLoad(Loc);
Mike Stump58a85142009-03-04 22:48:06 +0000328 // Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000329 }
330 Builder.CreateStore(Loc, Addr);
331 } else if (r.isComplex())
Mike Stump8a2b4b12009-02-25 23:33:13 +0000332 // FIXME: implement
333 ErrorUnsupported(BE, "complex in block literal");
334 else if (r.isAggregate())
335 ; // Already created into the destination
336 else
337 assert (0 && "bad block variable");
338 // FIXME: Ensure that the offset created by the backend for
339 // the struct matches the previously computed offset in BlockDecls.
340 }
Mike Stump08920992009-03-07 02:35:30 +0000341 NoteForHelper.resize(helpersize);
Mike Stumpcf62d392009-03-06 18:42:23 +0000342
343 // __descriptor
Blaine Garst2a7eb282010-02-23 21:51:17 +0000344 llvm::Value *Descriptor = BuildDescriptorBlockDecl(BE,
345 subBlockHasCopyDispose,
Mike Stumpa803b0e2009-03-25 17:58:24 +0000346 subBlockSize, Ty,
Mike Stump08920992009-03-07 02:35:30 +0000347 &NoteForHelper);
348 Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
349 Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
Mike Stumpe5fee252009-02-13 16:19:19 +0000350 }
Mike Stump00470a12009-03-05 08:32:30 +0000351
Mike Stumpbd65cac2009-02-19 01:01:04 +0000352 QualType BPT = BE->getType();
Fariborz Jahanian263c4de2010-02-10 23:34:57 +0000353 V = Builder.CreateBitCast(V, ConvertType(BPT));
354 // See if this is a __weak block variable and the must call objc_read_weak
355 // on it.
356 const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>();
357 QualType RES = ftype->getResultType();
358 if (RES.isObjCGCWeak()) {
359 // Must cast argument to id*
360 const llvm::Type *ObjectPtrTy =
361 ConvertType(CGM.getContext().getObjCIdType());
362 const llvm::Type *PtrObjectPtrTy =
363 llvm::PointerType::getUnqual(ObjectPtrTy);
364 V = Builder.CreateBitCast(V, PtrObjectPtrTy);
365 V = CGM.getObjCRuntime().EmitObjCWeakRead(*this, V);
366 }
367 return V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000368}
369
370
Mike Stump2a998142009-03-04 18:17:45 +0000371const llvm::Type *BlockModule::getBlockDescriptorType() {
Mike Stumpab695142009-02-13 15:16:56 +0000372 if (BlockDescriptorType)
373 return BlockDescriptorType;
374
Mike Stumpa5448542009-02-13 15:32:32 +0000375 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000376 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000377
Mike Stumpab695142009-02-13 15:16:56 +0000378 // struct __block_descriptor {
379 // unsigned long reserved;
380 // unsigned long block_size;
Blaine Garst2a7eb282010-02-23 21:51:17 +0000381 //
382 // // later, the following will be added
383 //
384 // struct {
385 // void (*copyHelper)();
386 // void (*copyHelper)();
387 // } helpers; // !!! optional
388 //
389 // const char *signature; // the block signature
390 // const char *layout; // reserved
Mike Stumpab695142009-02-13 15:16:56 +0000391 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000392 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(),
393 UnsignedLongTy,
Mike Stumpa5448542009-02-13 15:32:32 +0000394 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000395 NULL);
396
397 getModule().addTypeName("struct.__block_descriptor",
398 BlockDescriptorType);
399
400 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000401}
402
Mike Stump2a998142009-03-04 18:17:45 +0000403const llvm::Type *BlockModule::getGenericBlockLiteralType() {
Mike Stump9b8a7972009-02-13 15:25:34 +0000404 if (GenericBlockLiteralType)
405 return GenericBlockLiteralType;
406
Mike Stumpa5448542009-02-13 15:32:32 +0000407 const llvm::Type *BlockDescPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000408 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000409
Mike Stump7cbb3602009-02-13 16:01:35 +0000410 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
411 getTypes().ConvertType(getContext().IntTy));
412
Mike Stump9b8a7972009-02-13 15:25:34 +0000413 // struct __block_literal_generic {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000414 // void *__isa;
415 // int __flags;
416 // int __reserved;
417 // void (*__invoke)(void *);
418 // struct __block_descriptor *__descriptor;
Mike Stump9b8a7972009-02-13 15:25:34 +0000419 // };
Blaine Garst2a7eb282010-02-23 21:51:17 +0000420 GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
Owen Anderson47a434f2009-08-05 23:18:46 +0000421 PtrToInt8Ty,
Mike Stump7cbb3602009-02-13 16:01:35 +0000422 IntTy,
423 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000424 PtrToInt8Ty,
Mike Stump9b8a7972009-02-13 15:25:34 +0000425 BlockDescPtrTy,
426 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000427
Mike Stump9b8a7972009-02-13 15:25:34 +0000428 getModule().addTypeName("struct.__block_literal_generic",
429 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000430
Mike Stump9b8a7972009-02-13 15:25:34 +0000431 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000432}
433
Mike Stumpbd65cac2009-02-19 01:01:04 +0000434
Anders Carlssona1736c02009-12-24 21:13:40 +0000435RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E,
436 ReturnValueSlot ReturnValue) {
Mike Stumpa5448542009-02-13 15:32:32 +0000437 const BlockPointerType *BPT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000438 E->getCallee()->getType()->getAs<BlockPointerType>();
Mike Stumpa5448542009-02-13 15:32:32 +0000439
Anders Carlssonacfde802009-02-12 00:39:25 +0000440 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
441
442 // Get a pointer to the generic block literal.
443 const llvm::Type *BlockLiteralTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000444 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000445
446 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000447 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000448 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
449
450 // Get the function pointer from the literal.
451 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Anders Carlssonacfde802009-02-12 00:39:25 +0000452
Mike Stumpa5448542009-02-13 15:32:32 +0000453 BlockLiteral =
454 Builder.CreateBitCast(BlockLiteral,
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000455 llvm::Type::getInt8PtrTy(VMContext),
Anders Carlssonacfde802009-02-12 00:39:25 +0000456 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000457
Anders Carlssonacfde802009-02-12 00:39:25 +0000458 // Add the block literal.
459 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
460 CallArgList Args;
461 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000462
Anders Carlsson782f3972009-04-08 23:13:16 +0000463 QualType FnType = BPT->getPointeeType();
464
Anders Carlssonacfde802009-02-12 00:39:25 +0000465 // And the rest of the arguments.
John McCall183700f2009-09-21 23:43:11 +0000466 EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(),
Anders Carlsson782f3972009-04-08 23:13:16 +0000467 E->arg_begin(), E->arg_end());
Mike Stumpa5448542009-02-13 15:32:32 +0000468
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000469 // Load the function.
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000470 llvm::Value *Func = Builder.CreateLoad(FuncPtr, "tmp");
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000471
John McCall04a67a62010-02-05 21:31:56 +0000472 const FunctionType *FuncTy = FnType->getAs<FunctionType>();
473 QualType ResultType = FuncTy->getResultType();
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000474
Mike Stump1eb44332009-09-09 15:08:12 +0000475 const CGFunctionInfo &FnInfo =
Rafael Espindola264ba482010-03-30 20:24:48 +0000476 CGM.getTypes().getFunctionInfo(ResultType, Args,
477 FuncTy->getExtInfo());
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000479 // Cast the function pointer to the right type.
Mike Stump1eb44332009-09-09 15:08:12 +0000480 const llvm::Type *BlockFTy =
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000481 CGM.getTypes().GetFunctionType(FnInfo, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Owen Anderson96e0fc72009-07-29 22:16:19 +0000483 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000484 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Anders Carlssonacfde802009-02-12 00:39:25 +0000486 // And call the block.
Anders Carlssona1736c02009-12-24 21:13:40 +0000487 return EmitCall(FnInfo, Func, ReturnValue, Args);
Anders Carlssonacfde802009-02-12 00:39:25 +0000488}
Anders Carlssond5cab542009-02-12 17:55:02 +0000489
Ken Dyck199c3d62010-01-11 17:06:35 +0000490CharUnits CodeGenFunction::AllocateBlockDecl(const BlockDeclRefExpr *E) {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000491 const ValueDecl *VD = E->getDecl();
Ken Dyck199c3d62010-01-11 17:06:35 +0000492 CharUnits &offset = BlockDecls[VD];
Mike Stumpdab514f2009-03-04 03:23:46 +0000493
Mike Stumpdab514f2009-03-04 03:23:46 +0000494 // See if we have already allocated an offset for this variable.
Ken Dyck199c3d62010-01-11 17:06:35 +0000495 if (offset.isPositive())
Mike Stumpea26cb52009-10-21 03:49:08 +0000496 return offset;
497
498 // Don't run the expensive check, unless we have to.
Mike Stump083c25e2009-10-22 00:49:09 +0000499 if (!BlockHasCopyDispose)
500 if (E->isByRef()
501 || BlockRequiresCopying(E->getType()))
502 BlockHasCopyDispose = true;
Mike Stumpea26cb52009-10-21 03:49:08 +0000503
504 // if not, allocate one now.
505 offset = getBlockOffset(E);
506
507 return offset;
508}
509
510llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
511 const ValueDecl *VD = E->getDecl();
Ken Dyck199c3d62010-01-11 17:06:35 +0000512 CharUnits offset = AllocateBlockDecl(E);
Mike Stumpea26cb52009-10-21 03:49:08 +0000513
Mike Stumpdab514f2009-03-04 03:23:46 +0000514
515 llvm::Value *BlockLiteral = LoadBlockStruct();
516 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
Mike Stumpea26cb52009-10-21 03:49:08 +0000517 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Ken Dyck199c3d62010-01-11 17:06:35 +0000518 offset.getQuantity()),
Mike Stump58a85142009-03-04 22:48:06 +0000519 "block.literal");
Mike Stumpdab514f2009-03-04 03:23:46 +0000520 if (E->isByRef()) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000521 const llvm::Type *PtrStructTy
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000522 = llvm::PointerType::get(BuildByRefType(VD), 0);
Mike Stumpa8b60c92009-03-21 21:00:35 +0000523 // The block literal will need a copy/destroy helper.
524 BlockHasCopyDispose = true;
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000525
526 const llvm::Type *Ty = PtrStructTy;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000527 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000528 V = Builder.CreateBitCast(V, Ty);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000529 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000530 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000531 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000532 V = Builder.CreateBitCast(V, PtrStructTy);
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000533 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
534 VD->getNameAsString());
Fariborz Jahaniand33ded52010-05-04 17:59:32 +0000535 if (VD->getType()->isReferenceType())
536 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000537 } else {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000538 const llvm::Type *Ty = CGM.getTypes().ConvertType(VD->getType());
539
Owen Anderson96e0fc72009-07-29 22:16:19 +0000540 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000541 V = Builder.CreateBitCast(V, Ty);
542 }
543 return V;
544}
545
Mike Stump6cc88f72009-03-20 21:53:12 +0000546void CodeGenFunction::BlockForwardSelf() {
547 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
548 ImplicitParamDecl *SelfDecl = OMD->getSelfDecl();
549 llvm::Value *&DMEntry = LocalDeclMap[SelfDecl];
550 if (DMEntry)
551 return;
552 // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care
553 BlockDeclRefExpr *BDRE = new (getContext())
554 BlockDeclRefExpr(SelfDecl,
555 SelfDecl->getType(), SourceLocation(), false);
556 DMEntry = GetAddrOfBlockDecl(BDRE);
557}
558
Mike Stump67a64482009-02-14 22:16:35 +0000559llvm::Constant *
Mike Stump90a90432009-03-04 18:47:42 +0000560BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000561 // Generate the block descriptor.
562 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000563 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
564 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000565
Blaine Garst2a7eb282010-02-23 21:51:17 +0000566 llvm::Constant *DescriptorFields[4];
Mike Stumpa5448542009-02-13 15:32:32 +0000567
Anders Carlssond5cab542009-02-12 17:55:02 +0000568 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000569 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000570
Anders Carlssond5cab542009-02-12 17:55:02 +0000571 // Block literal size. For global blocks we just use the size of the generic
572 // block literal struct.
Ken Dyck687cc4a2010-01-26 13:48:07 +0000573 CharUnits BlockLiteralSize =
574 CGM.GetTargetTypeStoreSize(getGenericBlockLiteralType());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000575 DescriptorFields[1] =
Ken Dyck199c3d62010-01-11 17:06:35 +0000576 llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize.getQuantity());
Blaine Garst2a7eb282010-02-23 21:51:17 +0000577
578 // signature. non-optional ObjC-style method descriptor @encode sequence
579 std::string BlockTypeEncoding;
580 CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
Mike Stumpa5448542009-02-13 15:32:32 +0000581
Blaine Garst2a7eb282010-02-23 21:51:17 +0000582 DescriptorFields[2] = llvm::ConstantExpr::getBitCast(
583 CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty);
584
585 // layout
586 DescriptorFields[3] =
587 llvm::ConstantInt::get(UnsignedLongTy,0);
588
589 // build the structure from the 4 elements
Mike Stumpa5448542009-02-13 15:32:32 +0000590 llvm::Constant *DescriptorStruct =
Blaine Garst2a7eb282010-02-23 21:51:17 +0000591 llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 4, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000592
Anders Carlssond5cab542009-02-12 17:55:02 +0000593 llvm::GlobalVariable *Descriptor =
Owen Anderson1c431b32009-07-08 19:05:04 +0000594 new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000595 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000596 DescriptorStruct, "__block_descriptor_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000597
David Chisnall5e530af2009-11-17 19:33:30 +0000598 int FieldCount = 5;
Anders Carlssond5cab542009-02-12 17:55:02 +0000599 // Generate the constants for the block literal.
David Chisnall5e530af2009-11-17 19:33:30 +0000600
601 std::vector<llvm::Constant*> LiteralFields(FieldCount);
Mike Stumpa5448542009-02-13 15:32:32 +0000602
Mike Stump67a64482009-02-14 22:16:35 +0000603 CodeGenFunction::BlockInfo Info(0, n);
Ken Dyck199c3d62010-01-11 17:06:35 +0000604 CharUnits subBlockSize;
Ken Dyck30a8a272010-01-26 19:13:33 +0000605 CharUnits subBlockAlign;
Mike Stumpa99038c2009-02-28 09:07:16 +0000606 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Daniel Dunbard67b09a2009-03-12 03:07:24 +0000607 bool subBlockHasCopyDispose = false;
Mike Stump7f28a9c2009-03-13 23:34:28 +0000608 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000609 llvm::Function *Fn
Mike Stump6cc88f72009-03-20 21:53:12 +0000610 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000611 subBlockSize,
Mike Stump90a90432009-03-04 18:47:42 +0000612 subBlockAlign,
Mike Stump00470a12009-03-05 08:32:30 +0000613 subBlockDeclRefDecls,
614 subBlockHasCopyDispose);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000615 assert(subBlockSize == BlockLiteralSize
616 && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000617
Anders Carlssond5cab542009-02-12 17:55:02 +0000618 // isa
Mike Stumpf99f1d02009-02-13 17:23:42 +0000619 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000620
Anders Carlssond5cab542009-02-12 17:55:02 +0000621 // Flags
Blaine Garst2a7eb282010-02-23 21:51:17 +0000622 LiteralFields[1] =
Blaine Garsta36e2232010-03-05 01:29:59 +0000623 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE);
Mike Stumpa5448542009-02-13 15:32:32 +0000624
Anders Carlssond5cab542009-02-12 17:55:02 +0000625 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000626 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000627
Anders Carlssond5cab542009-02-12 17:55:02 +0000628 // Function
629 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000630
Anders Carlssond5cab542009-02-12 17:55:02 +0000631 // Descriptor
632 LiteralFields[4] = Descriptor;
David Chisnall5e530af2009-11-17 19:33:30 +0000633
Mike Stumpa5448542009-02-13 15:32:32 +0000634 llvm::Constant *BlockLiteralStruct =
David Chisnall5e530af2009-11-17 19:33:30 +0000635 llvm::ConstantStruct::get(VMContext, LiteralFields, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000636
637 llvm::GlobalVariable *BlockLiteral =
Owen Anderson1c431b32009-07-08 19:05:04 +0000638 new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000639 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000640 BlockLiteralStruct, "__block_literal_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000641
Anders Carlssond5cab542009-02-12 17:55:02 +0000642 return BlockLiteral;
643}
644
Mike Stump4e7a1f72009-02-21 20:00:35 +0000645llvm::Value *CodeGenFunction::LoadBlockStruct() {
Mike Stumpbf1914b2009-10-20 20:30:01 +0000646 llvm::Value *V = Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()],
647 "self");
648 // For now, we codegen based upon byte offsets.
649 return Builder.CreateBitCast(V, PtrToInt8Ty);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000650}
651
Mike Stump00470a12009-03-05 08:32:30 +0000652llvm::Function *
653CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
654 const BlockInfo& Info,
Mike Stump6cc88f72009-03-20 21:53:12 +0000655 const Decl *OuterFuncDecl,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000656 llvm::DenseMap<const Decl*, llvm::Value*> ldm,
Ken Dyck199c3d62010-01-11 17:06:35 +0000657 CharUnits &Size,
Ken Dyck30a8a272010-01-26 19:13:33 +0000658 CharUnits &Align,
Mike Stump00470a12009-03-05 08:32:30 +0000659 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls,
660 bool &subBlockHasCopyDispose) {
Devang Patel963dfbd2009-04-15 21:51:44 +0000661
662 // Check if we should generate debug info for this block.
663 if (CGM.getDebugInfo())
664 DebugInfo = CGM.getDebugInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000665
Mike Stump7f28a9c2009-03-13 23:34:28 +0000666 // Arrange for local static and local extern declarations to appear
667 // to be local to this function as well, as they are directly referenced
668 // in a block.
669 for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
670 i != ldm.end();
671 ++i) {
672 const VarDecl *VD = dyn_cast<VarDecl>(i->first);
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000674 if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage())
Mike Stump7f28a9c2009-03-13 23:34:28 +0000675 LocalDeclMap[VD] = i->second;
676 }
677
Ken Dyck687cc4a2010-01-26 13:48:07 +0000678 BlockOffset =
679 CGM.GetTargetTypeStoreSize(CGM.getGenericBlockLiteralType());
Ken Dyck30a8a272010-01-26 19:13:33 +0000680 BlockAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Eli Friedman48f91222009-03-28 03:24:54 +0000681
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000682 const FunctionType *BlockFunctionType = BExpr->getFunctionType();
683 QualType ResultType;
Rafael Espindola264ba482010-03-30 20:24:48 +0000684 FunctionType::ExtInfo EInfo = getFunctionExtInfo(*BlockFunctionType);
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000685 bool IsVariadic;
Mike Stump1eb44332009-09-09 15:08:12 +0000686 if (const FunctionProtoType *FTy =
Fariborz Jahanianda0895d2009-04-11 18:54:57 +0000687 dyn_cast<FunctionProtoType>(BlockFunctionType)) {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000688 ResultType = FTy->getResultType();
689 IsVariadic = FTy->isVariadic();
Mike Stumpb3589f42009-07-30 22:28:39 +0000690 } else {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000691 // K&R style block.
692 ResultType = BlockFunctionType->getResultType();
693 IsVariadic = false;
694 }
Mike Stumpa5448542009-02-13 15:32:32 +0000695
Anders Carlssond5cab542009-02-12 17:55:02 +0000696 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000697
Mike Stumpea26cb52009-10-21 03:49:08 +0000698 CurFuncDecl = OuterFuncDecl;
699
Chris Lattner161d36d2009-02-28 19:01:03 +0000700 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlssond5cab542009-02-12 17:55:02 +0000701
Mike Stumpea26cb52009-10-21 03:49:08 +0000702 IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor");
Mike Stumpadaaad32009-10-20 02:12:22 +0000703
Mike Stumpbfbd5df2009-10-21 18:24:18 +0000704 // Allocate all BlockDeclRefDecls, so we can calculate the right ParmTy below.
Mike Stump0298d382009-10-21 22:02:08 +0000705 AllocateAllBlockDeclRefs(Info, this);
Mike Stumpea26cb52009-10-21 03:49:08 +0000706
Mike Stump083c25e2009-10-22 00:49:09 +0000707 QualType ParmTy = getContext().getBlockParmType(BlockHasCopyDispose,
708 BlockDeclRefDecls);
Anders Carlssond5cab542009-02-12 17:55:02 +0000709 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000710 ImplicitParamDecl *SelfDecl =
John McCall2888b652010-04-30 21:35:41 +0000711 ImplicitParamDecl::Create(getContext(), const_cast<BlockDecl*>(BD),
Mike Stumpadaaad32009-10-20 02:12:22 +0000712 SourceLocation(), II,
713 ParmTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000714
Anders Carlssond5cab542009-02-12 17:55:02 +0000715 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump4e7a1f72009-02-21 20:00:35 +0000716 BlockStructDecl = SelfDecl;
Mike Stumpa5448542009-02-13 15:32:32 +0000717
Steve Naroffe78b8092009-03-13 16:56:44 +0000718 for (BlockDecl::param_const_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000719 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000720 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000721
722 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +0000723 CGM.getTypes().getFunctionInfo(ResultType, Args, EInfo);
Anders Carlssond5cab542009-02-12 17:55:02 +0000724
Anders Carlssond5cab542009-02-12 17:55:02 +0000725 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000726 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic);
Mike Stumpa5448542009-02-13 15:32:32 +0000727
728 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000729 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000730 llvm::Twine("__") + Info.Name + "_block_invoke_",
Anders Carlssond5cab542009-02-12 17:55:02 +0000731 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000732
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000733 CGM.SetInternalFunctionAttributes(BD, Fn, FI);
734
Chris Lattner4863db42009-04-23 07:18:56 +0000735 StartFunction(BD, ResultType, Fn, Args,
Chris Lattner161d36d2009-02-28 19:01:03 +0000736 BExpr->getBody()->getLocEnd());
Mike Stumpb1a6e682009-09-30 02:43:10 +0000737
Chris Lattner4863db42009-04-23 07:18:56 +0000738 CurFuncDecl = OuterFuncDecl;
Chris Lattnerb5437d22009-04-23 05:30:27 +0000739 CurCodeDecl = BD;
Mike Stumpb289b3f2009-10-01 22:29:41 +0000740
741 // Save a spot to insert the debug information for all the BlockDeclRefDecls.
742 llvm::BasicBlock *entry = Builder.GetInsertBlock();
743 llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint();
744 --entry_ptr;
745
Chris Lattner161d36d2009-02-28 19:01:03 +0000746 EmitStmt(BExpr->getBody());
Mike Stumpb289b3f2009-10-01 22:29:41 +0000747
Mike Stumpde8c5c72009-10-01 00:27:30 +0000748 // Remember where we were...
749 llvm::BasicBlock *resume = Builder.GetInsertBlock();
Mike Stumpb289b3f2009-10-01 22:29:41 +0000750
Mike Stumpde8c5c72009-10-01 00:27:30 +0000751 // Go back to the entry.
Mike Stumpb289b3f2009-10-01 22:29:41 +0000752 ++entry_ptr;
753 Builder.SetInsertPoint(entry, entry_ptr);
754
Mike Stumpb1a6e682009-09-30 02:43:10 +0000755 if (CGDebugInfo *DI = getDebugInfo()) {
Mike Stumpb1a6e682009-09-30 02:43:10 +0000756 // Emit debug information for all the BlockDeclRefDecls.
Chris Lattner14b1a362010-01-25 03:29:35 +0000757 for (unsigned i = 0, e = BlockDeclRefDecls.size(); i != e; ++i) {
758 if (const BlockDeclRefExpr *BDRE =
759 dyn_cast<BlockDeclRefExpr>(BlockDeclRefDecls[i])) {
Mike Stumpb1a6e682009-09-30 02:43:10 +0000760 const ValueDecl *D = BDRE->getDecl();
761 DI->setLocation(D->getLocation());
762 DI->EmitDeclareOfBlockDeclRefVariable(BDRE,
763 LocalDeclMap[getBlockStructDecl()],
764 Builder, this);
765 }
766 }
Mike Stumpb1a6e682009-09-30 02:43:10 +0000767 }
Mike Stumpde8c5c72009-10-01 00:27:30 +0000768 // And resume where we left off.
769 if (resume == 0)
770 Builder.ClearInsertionPoint();
771 else
772 Builder.SetInsertPoint(resume);
Mike Stumpb1a6e682009-09-30 02:43:10 +0000773
Chris Lattner161d36d2009-02-28 19:01:03 +0000774 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlssond5cab542009-02-12 17:55:02 +0000775
Mike Stump8a2b4b12009-02-25 23:33:13 +0000776 // The runtime needs a minimum alignment of a void *.
Ken Dyck30a8a272010-01-26 19:13:33 +0000777 CharUnits MinAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Ken Dyck199c3d62010-01-11 17:06:35 +0000778 BlockOffset = CharUnits::fromQuantity(
Ken Dyck30a8a272010-01-26 19:13:33 +0000779 llvm::RoundUpToAlignment(BlockOffset.getQuantity(),
780 MinAlign.getQuantity()));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000781
Mike Stump4e7a1f72009-02-21 20:00:35 +0000782 Size = BlockOffset;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000783 Align = BlockAlign;
784 subBlockDeclRefDecls = BlockDeclRefDecls;
Mike Stump00470a12009-03-05 08:32:30 +0000785 subBlockHasCopyDispose |= BlockHasCopyDispose;
Anders Carlssond5cab542009-02-12 17:55:02 +0000786 return Fn;
787}
Mike Stumpa99038c2009-02-28 09:07:16 +0000788
Ken Dyck199c3d62010-01-11 17:06:35 +0000789CharUnits BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
Mike Stumpa99038c2009-02-28 09:07:16 +0000790 const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
791
Ken Dyck199c3d62010-01-11 17:06:35 +0000792 CharUnits Size = getContext().getTypeSizeInChars(D->getType());
Ken Dyck8b752f12010-01-27 17:10:57 +0000793 CharUnits Align = getContext().getDeclAlign(D);
Mike Stumpa99038c2009-02-28 09:07:16 +0000794
795 if (BDRE->isByRef()) {
Ken Dyck199c3d62010-01-11 17:06:35 +0000796 Size = getContext().getTypeSizeInChars(getContext().VoidPtrTy);
Ken Dyck30a8a272010-01-26 19:13:33 +0000797 Align = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Mike Stumpa99038c2009-02-28 09:07:16 +0000798 }
799
Ken Dyck30a8a272010-01-26 19:13:33 +0000800 assert ((Align.isPositive()) && "alignment must be 1 byte or more");
Mike Stumpa99038c2009-02-28 09:07:16 +0000801
Ken Dyck199c3d62010-01-11 17:06:35 +0000802 CharUnits OldOffset = BlockOffset;
Mike Stumpa99038c2009-02-28 09:07:16 +0000803
804 // Ensure proper alignment, even if it means we have to have a gap
Ken Dyck199c3d62010-01-11 17:06:35 +0000805 BlockOffset = CharUnits::fromQuantity(
Ken Dyck30a8a272010-01-26 19:13:33 +0000806 llvm::RoundUpToAlignment(BlockOffset.getQuantity(), Align.getQuantity()));
Mike Stumpa99038c2009-02-28 09:07:16 +0000807 BlockAlign = std::max(Align, BlockAlign);
Mike Stump00470a12009-03-05 08:32:30 +0000808
Ken Dyck199c3d62010-01-11 17:06:35 +0000809 CharUnits Pad = BlockOffset - OldOffset;
810 if (Pad.isPositive()) {
811 llvm::ArrayType::get(llvm::Type::getInt8Ty(VMContext), Pad.getQuantity());
Mike Stumpa99038c2009-02-28 09:07:16 +0000812 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
Ken Dyck199c3d62010-01-11 17:06:35 +0000813 llvm::APInt(32,
814 Pad.getQuantity()),
Mike Stumpa99038c2009-02-28 09:07:16 +0000815 ArrayType::Normal, 0);
Douglas Gregorba55ec22010-05-11 18:17:16 +0000816 ValueDecl *PadDecl = VarDecl::Create(getContext(),
817 getContext().getTranslationUnitDecl(),
818 SourceLocation(),
Douglas Gregor16573fa2010-04-19 22:54:31 +0000819 0, QualType(PadTy), 0,
820 VarDecl::None, VarDecl::None);
Mike Stumpa99038c2009-02-28 09:07:16 +0000821 Expr *E;
822 E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
Douglas Gregor0da76df2009-11-23 11:41:28 +0000823 SourceLocation());
Mike Stumpa99038c2009-02-28 09:07:16 +0000824 BlockDeclRefDecls.push_back(E);
825 }
826 BlockDeclRefDecls.push_back(BDRE);
827
828 BlockOffset += Size;
829 return BlockOffset-Size;
830}
Mike Stumpdab514f2009-03-04 03:23:46 +0000831
Mike Stump08920992009-03-07 02:35:30 +0000832llvm::Constant *BlockFunction::
833GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000834 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000835 QualType R = getContext().VoidTy;
836
837 FunctionArgList Args;
838 // FIXME: This leaks
Mike Stump08920992009-03-07 02:35:30 +0000839 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +0000840 ImplicitParamDecl::Create(getContext(), 0,
841 SourceLocation(), 0,
Mike Stump08920992009-03-07 02:35:30 +0000842 getContext().getPointerType(getContext().VoidTy));
843 Args.push_back(std::make_pair(Dst, Dst->getType()));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000844 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +0000845 ImplicitParamDecl::Create(getContext(), 0,
846 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000847 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000848 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Mike Stumpa4f668f2009-03-06 01:33:24 +0000850 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +0000851 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000852
Mike Stump3899a7f2009-06-05 23:26:36 +0000853 // FIXME: We'd like to put these into a mergable by content, with
854 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000855 CodeGenTypes &Types = CGM.getTypes();
856 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
857
858 llvm::Function *Fn =
859 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000860 "__copy_helper_block_", &CGM.getModule());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000861
862 IdentifierInfo *II
863 = &CGM.getContext().Idents.get("__copy_helper_block_");
864
865 FunctionDecl *FD = FunctionDecl::Create(getContext(),
866 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000867 SourceLocation(), II, R, 0,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000868 FunctionDecl::Static,
869 FunctionDecl::None,
870 false,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000871 true);
872 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump08920992009-03-07 02:35:30 +0000873
874 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
875 llvm::Type *PtrPtrT;
Mike Stump08920992009-03-07 02:35:30 +0000876
Mike Stumpb7477cf2009-04-10 18:52:28 +0000877 if (NoteForHelperp) {
878 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump08920992009-03-07 02:35:30 +0000879
Owen Anderson96e0fc72009-07-29 22:16:19 +0000880 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000881 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
882 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump08920992009-03-07 02:35:30 +0000883
Mike Stumpb7477cf2009-04-10 18:52:28 +0000884 llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000885 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000886 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000887 DstObj = Builder.CreateBitCast(DstObj, PtrPtrT);
888 DstObj = Builder.CreateLoad(DstObj);
Mike Stump08920992009-03-07 02:35:30 +0000889
Mike Stumpb7477cf2009-04-10 18:52:28 +0000890 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
891 int flag = NoteForHelper[i].flag;
892 int index = NoteForHelper[i].index;
Mike Stump08920992009-03-07 02:35:30 +0000893
Mike Stumpb7477cf2009-04-10 18:52:28 +0000894 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
895 || NoteForHelper[i].RequiresCopying) {
896 llvm::Value *Srcv = SrcObj;
897 Srcv = Builder.CreateStructGEP(Srcv, index);
898 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000899 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000900 Srcv = Builder.CreateLoad(Srcv);
901
902 llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
903 Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
904
Owen Anderson0032b272009-08-13 21:57:51 +0000905 llvm::Value *N = llvm::ConstantInt::get(
906 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000907 llvm::Value *F = getBlockObjectAssign();
908 Builder.CreateCall3(F, Dstv, Srcv, N);
909 }
Mike Stump08920992009-03-07 02:35:30 +0000910 }
911 }
912
Mike Stumpa4f668f2009-03-06 01:33:24 +0000913 CGF.FinishFunction();
914
Owen Anderson3c4972d2009-07-29 18:54:39 +0000915 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000916}
917
Mike Stumpcf62d392009-03-06 18:42:23 +0000918llvm::Constant *BlockFunction::
Mike Stump08920992009-03-07 02:35:30 +0000919GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
920 const llvm::StructType* T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000921 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000922 QualType R = getContext().VoidTy;
923
924 FunctionArgList Args;
925 // FIXME: This leaks
926 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +0000927 ImplicitParamDecl::Create(getContext(), 0,
928 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000929 getContext().getPointerType(getContext().VoidTy));
930
931 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Mike Stumpa4f668f2009-03-06 01:33:24 +0000933 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +0000934 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000935
Mike Stump3899a7f2009-06-05 23:26:36 +0000936 // FIXME: We'd like to put these into a mergable by content, with
937 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000938 CodeGenTypes &Types = CGM.getTypes();
939 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
940
941 llvm::Function *Fn =
942 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000943 "__destroy_helper_block_", &CGM.getModule());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000944
945 IdentifierInfo *II
946 = &CGM.getContext().Idents.get("__destroy_helper_block_");
947
948 FunctionDecl *FD = FunctionDecl::Create(getContext(),
949 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000950 SourceLocation(), II, R, 0,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000951 FunctionDecl::Static,
952 FunctionDecl::None,
953 false, true);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000954 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1edf6b62009-03-07 02:53:18 +0000955
Mike Stumpb7477cf2009-04-10 18:52:28 +0000956 if (NoteForHelperp) {
957 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump1edf6b62009-03-07 02:53:18 +0000958
Mike Stumpb7477cf2009-04-10 18:52:28 +0000959 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
960 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000961 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000962 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
963 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump1edf6b62009-03-07 02:53:18 +0000964
Mike Stumpb7477cf2009-04-10 18:52:28 +0000965 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
966 int flag = NoteForHelper[i].flag;
967 int index = NoteForHelper[i].index;
Mike Stump1edf6b62009-03-07 02:53:18 +0000968
Mike Stumpb7477cf2009-04-10 18:52:28 +0000969 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
970 || NoteForHelper[i].RequiresCopying) {
971 llvm::Value *Srcv = SrcObj;
972 Srcv = Builder.CreateStructGEP(Srcv, index);
973 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000974 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000975 Srcv = Builder.CreateLoad(Srcv);
976
977 BuildBlockRelease(Srcv, flag);
978 }
Mike Stump1edf6b62009-03-07 02:53:18 +0000979 }
980 }
981
Mike Stumpa4f668f2009-03-06 01:33:24 +0000982 CGF.FinishFunction();
983
Owen Anderson3c4972d2009-07-29 18:54:39 +0000984 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000985}
986
Mike Stump08920992009-03-07 02:35:30 +0000987llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000988 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump08920992009-03-07 02:35:30 +0000989 return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
990 T, NoteForHelper);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000991}
992
Mike Stump08920992009-03-07 02:35:30 +0000993llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000994 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stump08920992009-03-07 02:35:30 +0000995 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000996 T, NoteForHelperp);
Mike Stumpdab514f2009-03-04 03:23:46 +0000997}
Mike Stump797b6322009-03-05 01:23:13 +0000998
Mike Stumpee094222009-03-06 06:12:24 +0000999llvm::Constant *BlockFunction::
1000GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
Mike Stump45031c02009-03-06 02:29:21 +00001001 QualType R = getContext().VoidTy;
1002
1003 FunctionArgList Args;
1004 // FIXME: This leaks
Mike Stumpee094222009-03-06 06:12:24 +00001005 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +00001006 ImplicitParamDecl::Create(getContext(), 0,
1007 SourceLocation(), 0,
Mike Stumpee094222009-03-06 06:12:24 +00001008 getContext().getPointerType(getContext().VoidTy));
1009 Args.push_back(std::make_pair(Dst, Dst->getType()));
1010
1011 // FIXME: This leaks
Mike Stump45031c02009-03-06 02:29:21 +00001012 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001013 ImplicitParamDecl::Create(getContext(), 0,
1014 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001015 getContext().getPointerType(getContext().VoidTy));
Mike Stump45031c02009-03-06 02:29:21 +00001016 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Mike Stump45031c02009-03-06 02:29:21 +00001018 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +00001019 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stump45031c02009-03-06 02:29:21 +00001020
Mike Stump45031c02009-03-06 02:29:21 +00001021 CodeGenTypes &Types = CGM.getTypes();
1022 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1023
Mike Stump3899a7f2009-06-05 23:26:36 +00001024 // FIXME: We'd like to put these into a mergable by content, with
1025 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001026 llvm::Function *Fn =
1027 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001028 "__Block_byref_id_object_copy_", &CGM.getModule());
Mike Stump45031c02009-03-06 02:29:21 +00001029
1030 IdentifierInfo *II
1031 = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
1032
1033 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1034 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001035 SourceLocation(), II, R, 0,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001036 FunctionDecl::Static,
1037 FunctionDecl::None,
1038 false, true);
Mike Stump45031c02009-03-06 02:29:21 +00001039 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpee094222009-03-06 06:12:24 +00001040
1041 // dst->x
1042 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001043 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001044 V = Builder.CreateLoad(V);
Mike Stumpee094222009-03-06 06:12:24 +00001045 V = Builder.CreateStructGEP(V, 6, "x");
1046 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
1047
1048 // src->x
1049 V = CGF.GetAddrOfLocalVar(Src);
1050 V = Builder.CreateLoad(V);
1051 V = Builder.CreateBitCast(V, T);
1052 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001053 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpee094222009-03-06 06:12:24 +00001054 llvm::Value *SrcObj = Builder.CreateLoad(V);
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Mike Stumpee094222009-03-06 06:12:24 +00001056 flag |= BLOCK_BYREF_CALLER;
1057
Owen Anderson0032b272009-08-13 21:57:51 +00001058 llvm::Value *N = llvm::ConstantInt::get(
1059 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpee094222009-03-06 06:12:24 +00001060 llvm::Value *F = getBlockObjectAssign();
1061 Builder.CreateCall3(F, DstObj, SrcObj, N);
1062
Mike Stump45031c02009-03-06 02:29:21 +00001063 CGF.FinishFunction();
1064
Owen Anderson3c4972d2009-07-29 18:54:39 +00001065 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001066}
1067
Mike Stump1851b682009-03-06 04:53:30 +00001068llvm::Constant *
1069BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
1070 int flag) {
Mike Stump45031c02009-03-06 02:29:21 +00001071 QualType R = getContext().VoidTy;
1072
1073 FunctionArgList Args;
1074 // FIXME: This leaks
1075 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001076 ImplicitParamDecl::Create(getContext(), 0,
1077 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001078 getContext().getPointerType(getContext().VoidTy));
1079
1080 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001081
Mike Stump45031c02009-03-06 02:29:21 +00001082 const CGFunctionInfo &FI =
Rafael Espindola264ba482010-03-30 20:24:48 +00001083 CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo());
Mike Stump45031c02009-03-06 02:29:21 +00001084
Mike Stump45031c02009-03-06 02:29:21 +00001085 CodeGenTypes &Types = CGM.getTypes();
1086 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1087
Mike Stump3899a7f2009-06-05 23:26:36 +00001088 // FIXME: We'd like to put these into a mergable by content, with
1089 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001090 llvm::Function *Fn =
1091 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001092 "__Block_byref_id_object_dispose_",
Mike Stump45031c02009-03-06 02:29:21 +00001093 &CGM.getModule());
1094
1095 IdentifierInfo *II
1096 = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
1097
1098 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1099 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001100 SourceLocation(), II, R, 0,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001101 FunctionDecl::Static,
1102 FunctionDecl::None,
1103 false, true);
Mike Stump45031c02009-03-06 02:29:21 +00001104 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1851b682009-03-06 04:53:30 +00001105
1106 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001107 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001108 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001109 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001110 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001111 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001112
Mike Stump1851b682009-03-06 04:53:30 +00001113 flag |= BLOCK_BYREF_CALLER;
1114 BuildBlockRelease(V, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001115 CGF.FinishFunction();
1116
Owen Anderson3c4972d2009-07-29 18:54:39 +00001117 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001118}
1119
Mike Stumpee094222009-03-06 06:12:24 +00001120llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001121 int Flag, unsigned Align) {
1122 // All alignments below that of pointer alignment collapse down to just
Mike Stump3899a7f2009-06-05 23:26:36 +00001123 // pointer alignment, as we always have at least that much alignment to begin
1124 // with.
1125 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001126
Mike Stump3899a7f2009-06-05 23:26:36 +00001127 // As an optimization, we only generate a single function of each kind we
1128 // might need. We need a different one for each alignment and for each
1129 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001130 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1131 llvm::Constant *&Entry = CGM.AssignCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001132 if (Entry)
1133 return Entry;
Chris Lattner10976d92009-12-05 08:21:30 +00001134 return Entry = CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, Flag);
Mike Stump45031c02009-03-06 02:29:21 +00001135}
1136
Mike Stump1851b682009-03-06 04:53:30 +00001137llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001138 int Flag,
Mike Stump3899a7f2009-06-05 23:26:36 +00001139 unsigned Align) {
1140 // All alignments below that of pointer alignment collpase down to just
1141 // pointer alignment, as we always have at least that much alignment to begin
1142 // with.
1143 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001144
Mike Stump3899a7f2009-06-05 23:26:36 +00001145 // As an optimization, we only generate a single function of each kind we
1146 // might need. We need a different one for each alignment and for each
1147 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001148 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1149 llvm::Constant *&Entry = CGM.DestroyCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001150 if (Entry)
1151 return Entry;
Chris Lattner10976d92009-12-05 08:21:30 +00001152 return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, Flag);
Mike Stump45031c02009-03-06 02:29:21 +00001153}
1154
Mike Stump797b6322009-03-05 01:23:13 +00001155llvm::Value *BlockFunction::getBlockObjectDispose() {
1156 if (CGM.BlockObjectDispose == 0) {
1157 const llvm::FunctionType *FTy;
1158 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001159 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stump797b6322009-03-05 01:23:13 +00001160 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001161 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson96e0fc72009-07-29 22:16:19 +00001162 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stump00470a12009-03-05 08:32:30 +00001163 CGM.BlockObjectDispose
Mike Stump797b6322009-03-05 01:23:13 +00001164 = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
1165 }
1166 return CGM.BlockObjectDispose;
1167}
1168
Mike Stumpee094222009-03-06 06:12:24 +00001169llvm::Value *BlockFunction::getBlockObjectAssign() {
1170 if (CGM.BlockObjectAssign == 0) {
1171 const llvm::FunctionType *FTy;
1172 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001173 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stumpee094222009-03-06 06:12:24 +00001174 ArgTys.push_back(PtrToInt8Ty);
1175 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001176 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson96e0fc72009-07-29 22:16:19 +00001177 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stumpee094222009-03-06 06:12:24 +00001178 CGM.BlockObjectAssign
1179 = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
1180 }
1181 return CGM.BlockObjectAssign;
1182}
1183
Mike Stump1851b682009-03-06 04:53:30 +00001184void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Mike Stump797b6322009-03-05 01:23:13 +00001185 llvm::Value *F = getBlockObjectDispose();
Mike Stump1851b682009-03-06 04:53:30 +00001186 llvm::Value *N;
Mike Stump797b6322009-03-05 01:23:13 +00001187 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001188 N = llvm::ConstantInt::get(llvm::Type::getInt32Ty(V->getContext()), flag);
Mike Stump797b6322009-03-05 01:23:13 +00001189 Builder.CreateCall2(F, V, N);
1190}
Mike Stump00470a12009-03-05 08:32:30 +00001191
1192ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stump08920992009-03-07 02:35:30 +00001193
1194BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1195 CGBuilderTy &B)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001196 : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) {
Owen Anderson0032b272009-08-13 21:57:51 +00001197 PtrToInt8Ty = llvm::PointerType::getUnqual(
1198 llvm::Type::getInt8Ty(VMContext));
Mike Stump08920992009-03-07 02:35:30 +00001199
1200 BlockHasCopyDispose = false;
1201}