blob: 2f0961326a179fe7cc15fe6ae4ccd2b8cae1bab9 [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::
Ken Dyck199c3d62010-01-11 17:06:35 +000027BuildDescriptorBlockDecl(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
46 if (BlockHasCopyDispose) {
47 // copy_func_helper_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000048 Elts.push_back(BuildCopyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000049
50 // destroy_func_decl
Mike Stumpb7477cf2009-04-10 18:52:28 +000051 Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper));
Mike Stumpe5fee252009-02-13 16:19:19 +000052 }
53
Nick Lewycky0d36dd22009-09-19 20:00:52 +000054 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stumpe5fee252009-02-13 16:19:19 +000055
Owen Anderson1c431b32009-07-08 19:05:04 +000056 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Mike Stumpe5fee252009-02-13 16:19:19 +000057 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +000058 C, "__block_descriptor_tmp");
Mike Stumpe5fee252009-02-13 16:19:19 +000059 return C;
60}
61
Mike Stump2a998142009-03-04 18:17:45 +000062llvm::Constant *BlockModule::getNSConcreteGlobalBlock() {
Chris Lattnere2f79b62009-05-13 02:50:56 +000063 if (NSConcreteGlobalBlock == 0)
Mike Stump1eb44332009-09-09 15:08:12 +000064 NSConcreteGlobalBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
Chris Lattnere2f79b62009-05-13 02:50:56 +000065 "_NSConcreteGlobalBlock");
Mike Stumpf99f1d02009-02-13 17:23:42 +000066 return NSConcreteGlobalBlock;
67}
68
Mike Stump2a998142009-03-04 18:17:45 +000069llvm::Constant *BlockModule::getNSConcreteStackBlock() {
Chris Lattnere2f79b62009-05-13 02:50:56 +000070 if (NSConcreteStackBlock == 0)
Mike Stump1eb44332009-09-09 15:08:12 +000071 NSConcreteStackBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
Chris Lattnere2f79b62009-05-13 02:50:56 +000072 "_NSConcreteStackBlock");
Mike Stump59c5b112009-02-13 19:29:27 +000073 return NSConcreteStackBlock;
74}
75
Mike Stump38e16272009-10-21 22:01:24 +000076static void CollectBlockDeclRefInfo(
77 const Stmt *S, CodeGenFunction::BlockInfo &Info,
78 llvm::SmallSet<const DeclContext *, 16> &InnerContexts) {
Anders Carlsson4de9fce2009-03-01 01:09:12 +000079 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
80 I != E; ++I)
Daniel Dunbar82573ee2009-03-02 07:00:57 +000081 if (*I)
Mike Stump38e16272009-10-21 22:01:24 +000082 CollectBlockDeclRefInfo(*I, Info, InnerContexts);
Mike Stump00470a12009-03-05 08:32:30 +000083
Mike Stumpea26cb52009-10-21 03:49:08 +000084 // We want to ensure we walk down into block literals so we can find
85 // all nested BlockDeclRefExprs.
Mike Stump38e16272009-10-21 22:01:24 +000086 if (const BlockExpr *BE = dyn_cast<BlockExpr>(S)) {
87 InnerContexts.insert(cast<DeclContext>(BE->getBlockDecl()));
88 CollectBlockDeclRefInfo(BE->getBody(), Info, InnerContexts);
89 }
Mike Stumpea26cb52009-10-21 03:49:08 +000090
Mike Stump38e16272009-10-21 22:01:24 +000091 if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) {
Anders Carlsson4de9fce2009-03-01 01:09:12 +000092 // FIXME: Handle enums.
Mike Stump38e16272009-10-21 22:01:24 +000093 if (isa<FunctionDecl>(BDRE->getDecl()))
Anders Carlsson4de9fce2009-03-01 01:09:12 +000094 return;
Mike Stump00470a12009-03-05 08:32:30 +000095
Mike Stump38e16272009-10-21 22:01:24 +000096 // Only Decls that escape are added.
97 if (!InnerContexts.count(BDRE->getDecl()->getDeclContext()))
98 Info.DeclRefs.push_back(BDRE);
Anders Carlsson4de9fce2009-03-01 01:09:12 +000099 }
100}
101
Mike Stump58a85142009-03-04 22:48:06 +0000102/// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be
103/// declared as a global variable instead of on the stack.
Chris Lattnere2f79b62009-05-13 02:50:56 +0000104static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info) {
Mike Stumpea26cb52009-10-21 03:49:08 +0000105 return Info.DeclRefs.empty();
106}
107
108/// AllocateAllBlockDeclRefs - Preallocate all nested BlockDeclRefExprs to
109/// ensure we can generate the debug information for the parameter for the block
110/// invoke function.
111static void AllocateAllBlockDeclRefs(const CodeGenFunction::BlockInfo &Info,
112 CodeGenFunction *CGF) {
Mike Stumpea26cb52009-10-21 03:49:08 +0000113 // FIXME: Also always forward the this pointer in C++ as well.
114
115 for (size_t i = 0; i < Info.DeclRefs.size(); ++i)
116 CGF->AllocateBlockDecl(Info.DeclRefs[i]);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000117}
118
Mike Stump58a85142009-03-04 22:48:06 +0000119// FIXME: Push most into CGM, passing down a few bits, like current function
120// name.
Mike Stump8a2b4b12009-02-25 23:33:13 +0000121llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Mike Stumpe5fee252009-02-13 16:19:19 +0000122
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000123 std::string Name = CurFn->getName();
124 CodeGenFunction::BlockInfo Info(0, Name.c_str());
Mike Stump38e16272009-10-21 22:01:24 +0000125 llvm::SmallSet<const DeclContext *, 16> InnerContexts;
126 InnerContexts.insert(BE->getBlockDecl());
127 CollectBlockDeclRefInfo(BE->getBody(), Info, InnerContexts);
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000128
129 // Check if the block can be global.
Mike Stump58a85142009-03-04 22:48:06 +0000130 // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like
131 // to just have one code path. We should move this function into CGM and pass
132 // CGF, then we can just check to see if CGF is 0.
Mike Stumpdab514f2009-03-04 03:23:46 +0000133 if (0 && CanBlockBeGlobal(Info))
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000134 return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
Mike Stump00470a12009-03-05 08:32:30 +0000135
David Chisnall5e530af2009-11-17 19:33:30 +0000136 size_t BlockFields = 5;
137
138 bool hasIntrospection = CGM.getContext().getLangOptions().BlockIntrospection;
139
140 if (hasIntrospection) {
141 BlockFields++;
142 }
143 std::vector<llvm::Constant*> Elts(BlockFields);
144
145 if (hasIntrospection) {
146 std::string BlockTypeEncoding;
147 CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
148
149 Elts[5] = llvm::ConstantExpr::getBitCast(
150 CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty);
151 }
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();
158 unsigned int flags = BLOCK_HAS_DESCRIPTOR;
159
David Chisnall5e530af2009-11-17 19:33:30 +0000160 if (hasIntrospection)
161 flags |= BLOCK_HAS_OBJC_TYPE;
162
Mike Stump00470a12009-03-05 08:32:30 +0000163 // We run this first so that we set BlockHasCopyDispose from the entire
164 // block literal.
165 // __invoke
Ken Dyck199c3d62010-01-11 17:06:35 +0000166 CharUnits subBlockSize;
Ken Dyck30a8a272010-01-26 19:13:33 +0000167 CharUnits subBlockAlign;
Mike Stump00470a12009-03-05 08:32:30 +0000168 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Mike Stumpa803b0e2009-03-25 17:58:24 +0000169 bool subBlockHasCopyDispose = false;
Mike Stump00470a12009-03-05 08:32:30 +0000170 llvm::Function *Fn
Mike Stumpbb304192009-09-24 22:31:14 +0000171 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl,
172 LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000173 subBlockSize,
Mike Stump00470a12009-03-05 08:32:30 +0000174 subBlockAlign,
175 subBlockDeclRefDecls,
Mike Stumpa803b0e2009-03-25 17:58:24 +0000176 subBlockHasCopyDispose);
177 BlockHasCopyDispose |= subBlockHasCopyDispose;
Mike Stump00470a12009-03-05 08:32:30 +0000178 Elts[3] = Fn;
179
Mike Stumpf5408fe2009-05-16 07:57:57 +0000180 // FIXME: Don't use BlockHasCopyDispose, it is set more often then
181 // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); }
Mike Stumpa803b0e2009-03-25 17:58:24 +0000182 if (subBlockHasCopyDispose)
Mike Stumpe5fee252009-02-13 16:19:19 +0000183 flags |= BLOCK_HAS_COPY_DISPOSE;
184
Mike Stump7d6dc4f2009-02-13 20:17:16 +0000185 // __isa
Mike Stump59c5b112009-02-13 19:29:27 +0000186 C = CGM.getNSConcreteStackBlock();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000187 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
Mike Stump00470a12009-03-05 08:32:30 +0000188 Elts[0] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000189
190 // __flags
191 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
192 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000193 C = llvm::ConstantInt::get(IntTy, flags);
Mike Stump00470a12009-03-05 08:32:30 +0000194 Elts[1] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000195
196 // __reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000197 C = llvm::ConstantInt::get(IntTy, 0);
Mike Stump00470a12009-03-05 08:32:30 +0000198 Elts[2] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000199
Mike Stump8a2b4b12009-02-25 23:33:13 +0000200 if (subBlockDeclRefDecls.size() == 0) {
Mike Stumpcf62d392009-03-06 18:42:23 +0000201 // __descriptor
Mike Stumpea26cb52009-10-21 03:49:08 +0000202 Elts[4] = BuildDescriptorBlockDecl(subBlockHasCopyDispose, subBlockSize,
203 0, 0);
Mike Stumpcf62d392009-03-06 18:42:23 +0000204
Mike Stump5570cfe2009-03-01 20:07:53 +0000205 // Optimize to being a global block.
206 Elts[0] = CGM.getNSConcreteGlobalBlock();
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000207 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
Mike Stump5570cfe2009-03-01 20:07:53 +0000208
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000209 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000210
Owen Anderson1c431b32009-07-08 19:05:04 +0000211 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000212 llvm::GlobalValue::InternalLinkage, C,
213 "__block_holder_tmp_" +
214 llvm::Twine(CGM.getGlobalUniqueCount()));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000215 QualType BPT = BE->getType();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000216 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000217 return C;
218 }
Mike Stump00470a12009-03-05 08:32:30 +0000219
David Chisnall5e530af2009-11-17 19:33:30 +0000220 std::vector<const llvm::Type *> Types(BlockFields+subBlockDeclRefDecls.size());
Mike Stump08920992009-03-07 02:35:30 +0000221 for (int i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000222 Types[i] = Elts[i]->getType();
Mike Stump08920992009-03-07 02:35:30 +0000223 Types[4] = PtrToInt8Ty;
David Chisnall5e530af2009-11-17 19:33:30 +0000224 if (hasIntrospection)
225 Types[5] = PtrToInt8Ty;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000226
Mike Stumpa99038c2009-02-28 09:07:16 +0000227 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
228 const Expr *E = subBlockDeclRefDecls[i];
229 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
230 QualType Ty = E->getType();
Mike Stumpdab514f2009-03-04 03:23:46 +0000231 if (BDRE && BDRE->isByRef()) {
David Chisnall5e530af2009-11-17 19:33:30 +0000232 Types[i+BlockFields] = llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000233 } else
David Chisnall5e530af2009-11-17 19:33:30 +0000234 Types[i+BlockFields] = ConvertType(Ty);
Mike Stumpa99038c2009-02-28 09:07:16 +0000235 }
Mike Stump8a2b4b12009-02-25 23:33:13 +0000236
Owen Anderson47a434f2009-08-05 23:18:46 +0000237 llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000238
239 llvm::AllocaInst *A = CreateTempAlloca(Ty);
Ken Dyck30a8a272010-01-26 19:13:33 +0000240 A->setAlignment(subBlockAlign.getQuantity());
Mike Stump8a2b4b12009-02-25 23:33:13 +0000241 V = A;
242
Mike Stump08920992009-03-07 02:35:30 +0000243 std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size());
244 int helpersize = 0;
245
246 for (unsigned i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000247 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
David Chisnall5e530af2009-11-17 19:33:30 +0000248 if (hasIntrospection)
249 Builder.CreateStore(Elts[5], Builder.CreateStructGEP(V, 5, "block.tmp"));
Mike Stump00470a12009-03-05 08:32:30 +0000250
Mike Stump8a2b4b12009-02-25 23:33:13 +0000251 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
252 {
Mike Stumpa99038c2009-02-28 09:07:16 +0000253 // FIXME: Push const down.
254 Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
255 DeclRefExpr *DR;
256 ValueDecl *VD;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000257
Mike Stumpa99038c2009-02-28 09:07:16 +0000258 DR = dyn_cast<DeclRefExpr>(E);
259 // Skip padding.
260 if (DR) continue;
261
262 BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
263 VD = BDRE->getDecl();
264
David Chisnall5e530af2009-11-17 19:33:30 +0000265 llvm::Value* Addr = Builder.CreateStructGEP(V, i+BlockFields, "tmp");
Mike Stump08920992009-03-07 02:35:30 +0000266 NoteForHelper[helpersize].index = i+5;
Mike Stump39605b42009-09-22 02:12:52 +0000267 NoteForHelper[helpersize].RequiresCopying
268 = BlockRequiresCopying(VD->getType());
Mike Stump08920992009-03-07 02:35:30 +0000269 NoteForHelper[helpersize].flag
Mike Stump39605b42009-09-22 02:12:52 +0000270 = (VD->getType()->isBlockPointerType()
271 ? BLOCK_FIELD_IS_BLOCK
272 : BLOCK_FIELD_IS_OBJECT);
Mike Stump08920992009-03-07 02:35:30 +0000273
Mike Stumpa99038c2009-02-28 09:07:16 +0000274 if (LocalDeclMap[VD]) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000275 if (BDRE->isByRef()) {
Mike Stump08920992009-03-07 02:35:30 +0000276 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
Mike Stumpf4bc3122009-03-07 06:04:31 +0000277 // FIXME: Someone double check this.
278 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000279 llvm::Value *Loc = LocalDeclMap[VD];
280 Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000281 Loc = Builder.CreateLoad(Loc);
Mike Stumpdab514f2009-03-04 03:23:46 +0000282 Builder.CreateStore(Loc, Addr);
Mike Stump08920992009-03-07 02:35:30 +0000283 ++helpersize;
Mike Stumpdab514f2009-03-04 03:23:46 +0000284 continue;
285 } else
John McCalldbd872f2009-12-08 09:08:17 +0000286 E = new (getContext()) DeclRefExpr (VD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000287 VD->getType(),
288 SourceLocation());
Mike Stumpa99038c2009-02-28 09:07:16 +0000289 }
Mike Stumpdab514f2009-03-04 03:23:46 +0000290 if (BDRE->isByRef()) {
Mike Stumpa8b60c92009-03-21 21:00:35 +0000291 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
292 // FIXME: Someone double check this.
293 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpa99038c2009-02-28 09:07:16 +0000294 E = new (getContext())
295 UnaryOperator(E, UnaryOperator::AddrOf,
296 getContext().getPointerType(E->getType()),
297 SourceLocation());
Mike Stumpdab514f2009-03-04 03:23:46 +0000298 }
Mike Stump08920992009-03-07 02:35:30 +0000299 ++helpersize;
Mike Stumpa99038c2009-02-28 09:07:16 +0000300
Mike Stumpa99038c2009-02-28 09:07:16 +0000301 RValue r = EmitAnyExpr(E, Addr, false);
Mike Stumpdab514f2009-03-04 03:23:46 +0000302 if (r.isScalar()) {
303 llvm::Value *Loc = r.getScalarVal();
David Chisnall5e530af2009-11-17 19:33:30 +0000304 const llvm::Type *Ty = Types[i+BlockFields];
Mike Stumpdab514f2009-03-04 03:23:46 +0000305 if (BDRE->isByRef()) {
Mike Stump58a85142009-03-04 22:48:06 +0000306 // E is now the address of the value field, instead, we want the
307 // address of the actual ByRef struct. We optimize this slightly
308 // compared to gcc by not grabbing the forwarding slot as this must
309 // be done during Block_copy for us, and we can postpone the work
310 // until then.
Ken Dyck199c3d62010-01-11 17:06:35 +0000311 CharUnits offset = BlockDecls[BDRE->getDecl()];
Mike Stump00470a12009-03-05 08:32:30 +0000312
Mike Stump58a85142009-03-04 22:48:06 +0000313 llvm::Value *BlockLiteral = LoadBlockStruct();
Mike Stump00470a12009-03-05 08:32:30 +0000314
Mike Stump58a85142009-03-04 22:48:06 +0000315 Loc = Builder.CreateGEP(BlockLiteral,
Mike Stumpea26cb52009-10-21 03:49:08 +0000316 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Ken Dyck199c3d62010-01-11 17:06:35 +0000317 offset.getQuantity()),
Mike Stump58a85142009-03-04 22:48:06 +0000318 "block.literal");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000319 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000320 Loc = Builder.CreateBitCast(Loc, Ty);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000321 Loc = Builder.CreateLoad(Loc);
Mike Stump58a85142009-03-04 22:48:06 +0000322 // Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000323 }
324 Builder.CreateStore(Loc, Addr);
325 } else if (r.isComplex())
Mike Stump8a2b4b12009-02-25 23:33:13 +0000326 // FIXME: implement
327 ErrorUnsupported(BE, "complex in block literal");
328 else if (r.isAggregate())
329 ; // Already created into the destination
330 else
331 assert (0 && "bad block variable");
332 // FIXME: Ensure that the offset created by the backend for
333 // the struct matches the previously computed offset in BlockDecls.
334 }
Mike Stump08920992009-03-07 02:35:30 +0000335 NoteForHelper.resize(helpersize);
Mike Stumpcf62d392009-03-06 18:42:23 +0000336
337 // __descriptor
Mike Stumpa803b0e2009-03-25 17:58:24 +0000338 llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockHasCopyDispose,
339 subBlockSize, Ty,
Mike Stump08920992009-03-07 02:35:30 +0000340 &NoteForHelper);
341 Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
342 Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
Mike Stumpe5fee252009-02-13 16:19:19 +0000343 }
Mike Stump00470a12009-03-05 08:32:30 +0000344
Mike Stumpbd65cac2009-02-19 01:01:04 +0000345 QualType BPT = BE->getType();
Fariborz Jahanian263c4de2010-02-10 23:34:57 +0000346 V = Builder.CreateBitCast(V, ConvertType(BPT));
347 // See if this is a __weak block variable and the must call objc_read_weak
348 // on it.
349 const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>();
350 QualType RES = ftype->getResultType();
351 if (RES.isObjCGCWeak()) {
352 // Must cast argument to id*
353 const llvm::Type *ObjectPtrTy =
354 ConvertType(CGM.getContext().getObjCIdType());
355 const llvm::Type *PtrObjectPtrTy =
356 llvm::PointerType::getUnqual(ObjectPtrTy);
357 V = Builder.CreateBitCast(V, PtrObjectPtrTy);
358 V = CGM.getObjCRuntime().EmitObjCWeakRead(*this, V);
359 }
360 return V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000361}
362
363
Mike Stump2a998142009-03-04 18:17:45 +0000364const llvm::Type *BlockModule::getBlockDescriptorType() {
Mike Stumpab695142009-02-13 15:16:56 +0000365 if (BlockDescriptorType)
366 return BlockDescriptorType;
367
Mike Stumpa5448542009-02-13 15:32:32 +0000368 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000369 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000370
Mike Stumpab695142009-02-13 15:16:56 +0000371 // struct __block_descriptor {
372 // unsigned long reserved;
373 // unsigned long block_size;
374 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000375 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(),
376 UnsignedLongTy,
Mike Stumpa5448542009-02-13 15:32:32 +0000377 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000378 NULL);
379
380 getModule().addTypeName("struct.__block_descriptor",
381 BlockDescriptorType);
382
383 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000384}
385
Mike Stump2a998142009-03-04 18:17:45 +0000386const llvm::Type *BlockModule::getGenericBlockLiteralType() {
Mike Stump9b8a7972009-02-13 15:25:34 +0000387 if (GenericBlockLiteralType)
388 return GenericBlockLiteralType;
389
Mike Stumpa5448542009-02-13 15:32:32 +0000390 const llvm::Type *BlockDescPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000391 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000392
Mike Stump7cbb3602009-02-13 16:01:35 +0000393 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
394 getTypes().ConvertType(getContext().IntTy));
395
Mike Stump9b8a7972009-02-13 15:25:34 +0000396 // struct __block_literal_generic {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000397 // void *__isa;
398 // int __flags;
399 // int __reserved;
400 // void (*__invoke)(void *);
401 // struct __block_descriptor *__descriptor;
David Chisnall5e530af2009-11-17 19:33:30 +0000402 // // GNU runtime only:
403 // const char *types;
Mike Stump9b8a7972009-02-13 15:25:34 +0000404 // };
David Chisnall5e530af2009-11-17 19:33:30 +0000405 if (CGM.getContext().getLangOptions().BlockIntrospection)
406 GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
407 PtrToInt8Ty,
408 IntTy,
409 IntTy,
410 PtrToInt8Ty,
411 BlockDescPtrTy,
412 PtrToInt8Ty,
413 NULL);
414 else
415 GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
Owen Anderson47a434f2009-08-05 23:18:46 +0000416 PtrToInt8Ty,
Mike Stump7cbb3602009-02-13 16:01:35 +0000417 IntTy,
418 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000419 PtrToInt8Ty,
Mike Stump9b8a7972009-02-13 15:25:34 +0000420 BlockDescPtrTy,
421 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000422
Mike Stump9b8a7972009-02-13 15:25:34 +0000423 getModule().addTypeName("struct.__block_literal_generic",
424 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000425
Mike Stump9b8a7972009-02-13 15:25:34 +0000426 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000427}
428
Mike Stump2a998142009-03-04 18:17:45 +0000429const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000430 if (GenericExtendedBlockLiteralType)
431 return GenericExtendedBlockLiteralType;
432
Mike Stumpbd65cac2009-02-19 01:01:04 +0000433 const llvm::Type *BlockDescPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000434 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpbd65cac2009-02-19 01:01:04 +0000435
436 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
437 getTypes().ConvertType(getContext().IntTy));
438
439 // struct __block_literal_generic {
440 // void *__isa;
441 // int __flags;
442 // int __reserved;
443 // void (*__invoke)(void *);
444 // struct __block_descriptor *__descriptor;
445 // void *__copy_func_helper_decl;
446 // void *__destroy_func_decl;
447 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000448 GenericExtendedBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
449 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000450 IntTy,
451 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000452 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000453 BlockDescPtrTy,
Mike Stump797b6322009-03-05 01:23:13 +0000454 PtrToInt8Ty,
455 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000456 NULL);
457
458 getModule().addTypeName("struct.__block_literal_extended_generic",
459 GenericExtendedBlockLiteralType);
460
461 return GenericExtendedBlockLiteralType;
462}
463
Anders Carlssona1736c02009-12-24 21:13:40 +0000464RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E,
465 ReturnValueSlot ReturnValue) {
Mike Stumpa5448542009-02-13 15:32:32 +0000466 const BlockPointerType *BPT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000467 E->getCallee()->getType()->getAs<BlockPointerType>();
Mike Stumpa5448542009-02-13 15:32:32 +0000468
Anders Carlssonacfde802009-02-12 00:39:25 +0000469 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
470
471 // Get a pointer to the generic block literal.
472 const llvm::Type *BlockLiteralTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000473 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000474
475 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000476 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000477 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
478
479 // Get the function pointer from the literal.
480 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Anders Carlssonacfde802009-02-12 00:39:25 +0000481
Mike Stumpa5448542009-02-13 15:32:32 +0000482 BlockLiteral =
483 Builder.CreateBitCast(BlockLiteral,
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000484 llvm::Type::getInt8PtrTy(VMContext),
Anders Carlssonacfde802009-02-12 00:39:25 +0000485 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000486
Anders Carlssonacfde802009-02-12 00:39:25 +0000487 // Add the block literal.
488 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
489 CallArgList Args;
490 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000491
Anders Carlsson782f3972009-04-08 23:13:16 +0000492 QualType FnType = BPT->getPointeeType();
493
Anders Carlssonacfde802009-02-12 00:39:25 +0000494 // And the rest of the arguments.
John McCall183700f2009-09-21 23:43:11 +0000495 EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(),
Anders Carlsson782f3972009-04-08 23:13:16 +0000496 E->arg_begin(), E->arg_end());
Mike Stumpa5448542009-02-13 15:32:32 +0000497
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000498 // Load the function.
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000499 llvm::Value *Func = Builder.CreateLoad(FuncPtr, "tmp");
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000500
John McCall04a67a62010-02-05 21:31:56 +0000501 const FunctionType *FuncTy = FnType->getAs<FunctionType>();
502 QualType ResultType = FuncTy->getResultType();
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000503
Mike Stump1eb44332009-09-09 15:08:12 +0000504 const CGFunctionInfo &FnInfo =
John McCall04a67a62010-02-05 21:31:56 +0000505 CGM.getTypes().getFunctionInfo(ResultType, Args, FuncTy->getCallConv(),
506 FuncTy->getNoReturnAttr());
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000508 // Cast the function pointer to the right type.
Mike Stump1eb44332009-09-09 15:08:12 +0000509 const llvm::Type *BlockFTy =
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000510 CGM.getTypes().GetFunctionType(FnInfo, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Owen Anderson96e0fc72009-07-29 22:16:19 +0000512 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000513 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Anders Carlssonacfde802009-02-12 00:39:25 +0000515 // And call the block.
Anders Carlssona1736c02009-12-24 21:13:40 +0000516 return EmitCall(FnInfo, Func, ReturnValue, Args);
Anders Carlssonacfde802009-02-12 00:39:25 +0000517}
Anders Carlssond5cab542009-02-12 17:55:02 +0000518
Ken Dyck199c3d62010-01-11 17:06:35 +0000519CharUnits CodeGenFunction::AllocateBlockDecl(const BlockDeclRefExpr *E) {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000520 const ValueDecl *VD = E->getDecl();
Ken Dyck199c3d62010-01-11 17:06:35 +0000521 CharUnits &offset = BlockDecls[VD];
Mike Stumpdab514f2009-03-04 03:23:46 +0000522
Mike Stumpdab514f2009-03-04 03:23:46 +0000523 // See if we have already allocated an offset for this variable.
Ken Dyck199c3d62010-01-11 17:06:35 +0000524 if (offset.isPositive())
Mike Stumpea26cb52009-10-21 03:49:08 +0000525 return offset;
526
527 // Don't run the expensive check, unless we have to.
Mike Stump083c25e2009-10-22 00:49:09 +0000528 if (!BlockHasCopyDispose)
529 if (E->isByRef()
530 || BlockRequiresCopying(E->getType()))
531 BlockHasCopyDispose = true;
Mike Stumpea26cb52009-10-21 03:49:08 +0000532
533 // if not, allocate one now.
534 offset = getBlockOffset(E);
535
536 return offset;
537}
538
539llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
540 const ValueDecl *VD = E->getDecl();
Ken Dyck199c3d62010-01-11 17:06:35 +0000541 CharUnits offset = AllocateBlockDecl(E);
Mike Stumpea26cb52009-10-21 03:49:08 +0000542
Mike Stumpdab514f2009-03-04 03:23:46 +0000543
544 llvm::Value *BlockLiteral = LoadBlockStruct();
545 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
Mike Stumpea26cb52009-10-21 03:49:08 +0000546 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Ken Dyck199c3d62010-01-11 17:06:35 +0000547 offset.getQuantity()),
Mike Stump58a85142009-03-04 22:48:06 +0000548 "block.literal");
Mike Stumpdab514f2009-03-04 03:23:46 +0000549 if (E->isByRef()) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000550 const llvm::Type *PtrStructTy
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000551 = llvm::PointerType::get(BuildByRefType(VD), 0);
Mike Stumpa8b60c92009-03-21 21:00:35 +0000552 // The block literal will need a copy/destroy helper.
553 BlockHasCopyDispose = true;
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000554
555 const llvm::Type *Ty = PtrStructTy;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000556 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000557 V = Builder.CreateBitCast(V, Ty);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000558 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000559 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000560 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000561 V = Builder.CreateBitCast(V, PtrStructTy);
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000562 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
563 VD->getNameAsString());
Mike Stumpdab514f2009-03-04 03:23:46 +0000564 } else {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000565 const llvm::Type *Ty = CGM.getTypes().ConvertType(VD->getType());
566
Owen Anderson96e0fc72009-07-29 22:16:19 +0000567 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000568 V = Builder.CreateBitCast(V, Ty);
569 }
570 return V;
571}
572
Mike Stump6cc88f72009-03-20 21:53:12 +0000573void CodeGenFunction::BlockForwardSelf() {
574 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
575 ImplicitParamDecl *SelfDecl = OMD->getSelfDecl();
576 llvm::Value *&DMEntry = LocalDeclMap[SelfDecl];
577 if (DMEntry)
578 return;
579 // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care
580 BlockDeclRefExpr *BDRE = new (getContext())
581 BlockDeclRefExpr(SelfDecl,
582 SelfDecl->getType(), SourceLocation(), false);
583 DMEntry = GetAddrOfBlockDecl(BDRE);
584}
585
Mike Stump67a64482009-02-14 22:16:35 +0000586llvm::Constant *
Mike Stump90a90432009-03-04 18:47:42 +0000587BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000588 // Generate the block descriptor.
589 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000590 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
591 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000592
Anders Carlssond5cab542009-02-12 17:55:02 +0000593 llvm::Constant *DescriptorFields[2];
Mike Stumpa5448542009-02-13 15:32:32 +0000594
Anders Carlssond5cab542009-02-12 17:55:02 +0000595 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000596 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000597
Anders Carlssond5cab542009-02-12 17:55:02 +0000598 // Block literal size. For global blocks we just use the size of the generic
599 // block literal struct.
Ken Dyck687cc4a2010-01-26 13:48:07 +0000600 CharUnits BlockLiteralSize =
601 CGM.GetTargetTypeStoreSize(getGenericBlockLiteralType());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000602 DescriptorFields[1] =
Ken Dyck199c3d62010-01-11 17:06:35 +0000603 llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize.getQuantity());
Mike Stumpa5448542009-02-13 15:32:32 +0000604
605 llvm::Constant *DescriptorStruct =
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000606 llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 2, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000607
Anders Carlssond5cab542009-02-12 17:55:02 +0000608 llvm::GlobalVariable *Descriptor =
Owen Anderson1c431b32009-07-08 19:05:04 +0000609 new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000610 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000611 DescriptorStruct, "__block_descriptor_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000612
David Chisnall5e530af2009-11-17 19:33:30 +0000613 int FieldCount = 5;
Anders Carlssond5cab542009-02-12 17:55:02 +0000614 // Generate the constants for the block literal.
David Chisnall5e530af2009-11-17 19:33:30 +0000615 if (CGM.getContext().getLangOptions().BlockIntrospection)
616 FieldCount = 6;
617
618 std::vector<llvm::Constant*> LiteralFields(FieldCount);
Mike Stumpa5448542009-02-13 15:32:32 +0000619
Mike Stump67a64482009-02-14 22:16:35 +0000620 CodeGenFunction::BlockInfo Info(0, n);
Ken Dyck199c3d62010-01-11 17:06:35 +0000621 CharUnits subBlockSize;
Ken Dyck30a8a272010-01-26 19:13:33 +0000622 CharUnits subBlockAlign;
Mike Stumpa99038c2009-02-28 09:07:16 +0000623 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Daniel Dunbard67b09a2009-03-12 03:07:24 +0000624 bool subBlockHasCopyDispose = false;
Mike Stump7f28a9c2009-03-13 23:34:28 +0000625 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000626 llvm::Function *Fn
Mike Stump6cc88f72009-03-20 21:53:12 +0000627 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000628 subBlockSize,
Mike Stump90a90432009-03-04 18:47:42 +0000629 subBlockAlign,
Mike Stump00470a12009-03-05 08:32:30 +0000630 subBlockDeclRefDecls,
631 subBlockHasCopyDispose);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000632 assert(subBlockSize == BlockLiteralSize
633 && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000634
Anders Carlssond5cab542009-02-12 17:55:02 +0000635 // isa
Mike Stumpf99f1d02009-02-13 17:23:42 +0000636 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000637
Anders Carlssond5cab542009-02-12 17:55:02 +0000638 // Flags
David Chisnall5e530af2009-11-17 19:33:30 +0000639 LiteralFields[1] = CGM.getContext().getLangOptions().BlockIntrospection ?
640 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR |
641 BLOCK_HAS_OBJC_TYPE) :
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000642 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR);
Mike Stumpa5448542009-02-13 15:32:32 +0000643
Anders Carlssond5cab542009-02-12 17:55:02 +0000644 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000645 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000646
Anders Carlssond5cab542009-02-12 17:55:02 +0000647 // Function
648 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000649
Anders Carlssond5cab542009-02-12 17:55:02 +0000650 // Descriptor
651 LiteralFields[4] = Descriptor;
David Chisnall5e530af2009-11-17 19:33:30 +0000652
653 // Type encoding
654 if (CGM.getContext().getLangOptions().BlockIntrospection) {
655 std::string BlockTypeEncoding;
656 CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
657
658 LiteralFields[5] = CGM.GetAddrOfConstantCString(BlockTypeEncoding);
659 }
Mike Stumpa5448542009-02-13 15:32:32 +0000660
661 llvm::Constant *BlockLiteralStruct =
David Chisnall5e530af2009-11-17 19:33:30 +0000662 llvm::ConstantStruct::get(VMContext, LiteralFields, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000663
664 llvm::GlobalVariable *BlockLiteral =
Owen Anderson1c431b32009-07-08 19:05:04 +0000665 new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000666 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000667 BlockLiteralStruct, "__block_literal_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000668
Anders Carlssond5cab542009-02-12 17:55:02 +0000669 return BlockLiteral;
670}
671
Mike Stump4e7a1f72009-02-21 20:00:35 +0000672llvm::Value *CodeGenFunction::LoadBlockStruct() {
Mike Stumpbf1914b2009-10-20 20:30:01 +0000673 llvm::Value *V = Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()],
674 "self");
675 // For now, we codegen based upon byte offsets.
676 return Builder.CreateBitCast(V, PtrToInt8Ty);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000677}
678
Mike Stump00470a12009-03-05 08:32:30 +0000679llvm::Function *
680CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
681 const BlockInfo& Info,
Mike Stump6cc88f72009-03-20 21:53:12 +0000682 const Decl *OuterFuncDecl,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000683 llvm::DenseMap<const Decl*, llvm::Value*> ldm,
Ken Dyck199c3d62010-01-11 17:06:35 +0000684 CharUnits &Size,
Ken Dyck30a8a272010-01-26 19:13:33 +0000685 CharUnits &Align,
Mike Stump00470a12009-03-05 08:32:30 +0000686 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls,
687 bool &subBlockHasCopyDispose) {
Devang Patel963dfbd2009-04-15 21:51:44 +0000688
689 // Check if we should generate debug info for this block.
690 if (CGM.getDebugInfo())
691 DebugInfo = CGM.getDebugInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Mike Stump7f28a9c2009-03-13 23:34:28 +0000693 // Arrange for local static and local extern declarations to appear
694 // to be local to this function as well, as they are directly referenced
695 // in a block.
696 for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
697 i != ldm.end();
698 ++i) {
699 const VarDecl *VD = dyn_cast<VarDecl>(i->first);
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000701 if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage())
Mike Stump7f28a9c2009-03-13 23:34:28 +0000702 LocalDeclMap[VD] = i->second;
703 }
704
Ken Dyck687cc4a2010-01-26 13:48:07 +0000705 BlockOffset =
706 CGM.GetTargetTypeStoreSize(CGM.getGenericBlockLiteralType());
Ken Dyck30a8a272010-01-26 19:13:33 +0000707 BlockAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Eli Friedman48f91222009-03-28 03:24:54 +0000708
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000709 const FunctionType *BlockFunctionType = BExpr->getFunctionType();
710 QualType ResultType;
John McCall04a67a62010-02-05 21:31:56 +0000711 CallingConv CC = BlockFunctionType->getCallConv();
712 bool NoReturn = BlockFunctionType->getNoReturnAttr();
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000713 bool IsVariadic;
Mike Stump1eb44332009-09-09 15:08:12 +0000714 if (const FunctionProtoType *FTy =
Fariborz Jahanianda0895d2009-04-11 18:54:57 +0000715 dyn_cast<FunctionProtoType>(BlockFunctionType)) {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000716 ResultType = FTy->getResultType();
717 IsVariadic = FTy->isVariadic();
Mike Stumpb3589f42009-07-30 22:28:39 +0000718 } else {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000719 // K&R style block.
720 ResultType = BlockFunctionType->getResultType();
721 IsVariadic = false;
722 }
Mike Stumpa5448542009-02-13 15:32:32 +0000723
Anders Carlssond5cab542009-02-12 17:55:02 +0000724 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000725
Mike Stumpea26cb52009-10-21 03:49:08 +0000726 CurFuncDecl = OuterFuncDecl;
727
Chris Lattner161d36d2009-02-28 19:01:03 +0000728 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlssond5cab542009-02-12 17:55:02 +0000729
Mike Stumpea26cb52009-10-21 03:49:08 +0000730 IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor");
Mike Stumpadaaad32009-10-20 02:12:22 +0000731
Mike Stumpbfbd5df2009-10-21 18:24:18 +0000732 // Allocate all BlockDeclRefDecls, so we can calculate the right ParmTy below.
Mike Stump0298d382009-10-21 22:02:08 +0000733 AllocateAllBlockDeclRefs(Info, this);
Mike Stumpea26cb52009-10-21 03:49:08 +0000734
Mike Stump083c25e2009-10-22 00:49:09 +0000735 QualType ParmTy = getContext().getBlockParmType(BlockHasCopyDispose,
736 BlockDeclRefDecls);
Anders Carlssond5cab542009-02-12 17:55:02 +0000737 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000738 ImplicitParamDecl *SelfDecl =
Anders Carlssond5cab542009-02-12 17:55:02 +0000739 ImplicitParamDecl::Create(getContext(), 0,
Mike Stumpadaaad32009-10-20 02:12:22 +0000740 SourceLocation(), II,
741 ParmTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000742
Anders Carlssond5cab542009-02-12 17:55:02 +0000743 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump4e7a1f72009-02-21 20:00:35 +0000744 BlockStructDecl = SelfDecl;
Mike Stumpa5448542009-02-13 15:32:32 +0000745
Steve Naroffe78b8092009-03-13 16:56:44 +0000746 for (BlockDecl::param_const_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000747 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000748 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000749
750 const CGFunctionInfo &FI =
John McCall04a67a62010-02-05 21:31:56 +0000751 CGM.getTypes().getFunctionInfo(ResultType, Args, CC, NoReturn);
Anders Carlssond5cab542009-02-12 17:55:02 +0000752
Anders Carlssond5cab542009-02-12 17:55:02 +0000753 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000754 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic);
Mike Stumpa5448542009-02-13 15:32:32 +0000755
756 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000757 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000758 llvm::Twine("__") + Info.Name + "_block_invoke_",
Anders Carlssond5cab542009-02-12 17:55:02 +0000759 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000760
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000761 CGM.SetInternalFunctionAttributes(BD, Fn, FI);
762
Chris Lattner4863db42009-04-23 07:18:56 +0000763 StartFunction(BD, ResultType, Fn, Args,
Chris Lattner161d36d2009-02-28 19:01:03 +0000764 BExpr->getBody()->getLocEnd());
Mike Stumpb1a6e682009-09-30 02:43:10 +0000765
Chris Lattner4863db42009-04-23 07:18:56 +0000766 CurFuncDecl = OuterFuncDecl;
Chris Lattnerb5437d22009-04-23 05:30:27 +0000767 CurCodeDecl = BD;
Mike Stumpb289b3f2009-10-01 22:29:41 +0000768
769 // Save a spot to insert the debug information for all the BlockDeclRefDecls.
770 llvm::BasicBlock *entry = Builder.GetInsertBlock();
771 llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint();
772 --entry_ptr;
773
Chris Lattner161d36d2009-02-28 19:01:03 +0000774 EmitStmt(BExpr->getBody());
Mike Stumpb289b3f2009-10-01 22:29:41 +0000775
Mike Stumpde8c5c72009-10-01 00:27:30 +0000776 // Remember where we were...
777 llvm::BasicBlock *resume = Builder.GetInsertBlock();
Mike Stumpb289b3f2009-10-01 22:29:41 +0000778
Mike Stumpde8c5c72009-10-01 00:27:30 +0000779 // Go back to the entry.
Mike Stumpb289b3f2009-10-01 22:29:41 +0000780 ++entry_ptr;
781 Builder.SetInsertPoint(entry, entry_ptr);
782
Mike Stumpb1a6e682009-09-30 02:43:10 +0000783 if (CGDebugInfo *DI = getDebugInfo()) {
Mike Stumpb1a6e682009-09-30 02:43:10 +0000784 // Emit debug information for all the BlockDeclRefDecls.
Chris Lattner14b1a362010-01-25 03:29:35 +0000785 for (unsigned i = 0, e = BlockDeclRefDecls.size(); i != e; ++i) {
786 if (const BlockDeclRefExpr *BDRE =
787 dyn_cast<BlockDeclRefExpr>(BlockDeclRefDecls[i])) {
Mike Stumpb1a6e682009-09-30 02:43:10 +0000788 const ValueDecl *D = BDRE->getDecl();
789 DI->setLocation(D->getLocation());
790 DI->EmitDeclareOfBlockDeclRefVariable(BDRE,
791 LocalDeclMap[getBlockStructDecl()],
792 Builder, this);
793 }
794 }
Mike Stumpb1a6e682009-09-30 02:43:10 +0000795 }
Mike Stumpde8c5c72009-10-01 00:27:30 +0000796 // And resume where we left off.
797 if (resume == 0)
798 Builder.ClearInsertionPoint();
799 else
800 Builder.SetInsertPoint(resume);
Mike Stumpb1a6e682009-09-30 02:43:10 +0000801
Chris Lattner161d36d2009-02-28 19:01:03 +0000802 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlssond5cab542009-02-12 17:55:02 +0000803
Mike Stump8a2b4b12009-02-25 23:33:13 +0000804 // The runtime needs a minimum alignment of a void *.
Ken Dyck30a8a272010-01-26 19:13:33 +0000805 CharUnits MinAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Ken Dyck199c3d62010-01-11 17:06:35 +0000806 BlockOffset = CharUnits::fromQuantity(
Ken Dyck30a8a272010-01-26 19:13:33 +0000807 llvm::RoundUpToAlignment(BlockOffset.getQuantity(),
808 MinAlign.getQuantity()));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000809
Mike Stump4e7a1f72009-02-21 20:00:35 +0000810 Size = BlockOffset;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000811 Align = BlockAlign;
812 subBlockDeclRefDecls = BlockDeclRefDecls;
Mike Stump00470a12009-03-05 08:32:30 +0000813 subBlockHasCopyDispose |= BlockHasCopyDispose;
Anders Carlssond5cab542009-02-12 17:55:02 +0000814 return Fn;
815}
Mike Stumpa99038c2009-02-28 09:07:16 +0000816
Ken Dyck199c3d62010-01-11 17:06:35 +0000817CharUnits BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
Mike Stumpa99038c2009-02-28 09:07:16 +0000818 const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
819
Ken Dyck199c3d62010-01-11 17:06:35 +0000820 CharUnits Size = getContext().getTypeSizeInChars(D->getType());
Ken Dyck8b752f12010-01-27 17:10:57 +0000821 CharUnits Align = getContext().getDeclAlign(D);
Mike Stumpa99038c2009-02-28 09:07:16 +0000822
823 if (BDRE->isByRef()) {
Ken Dyck199c3d62010-01-11 17:06:35 +0000824 Size = getContext().getTypeSizeInChars(getContext().VoidPtrTy);
Ken Dyck30a8a272010-01-26 19:13:33 +0000825 Align = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
Mike Stumpa99038c2009-02-28 09:07:16 +0000826 }
827
Ken Dyck30a8a272010-01-26 19:13:33 +0000828 assert ((Align.isPositive()) && "alignment must be 1 byte or more");
Mike Stumpa99038c2009-02-28 09:07:16 +0000829
Ken Dyck199c3d62010-01-11 17:06:35 +0000830 CharUnits OldOffset = BlockOffset;
Mike Stumpa99038c2009-02-28 09:07:16 +0000831
832 // Ensure proper alignment, even if it means we have to have a gap
Ken Dyck199c3d62010-01-11 17:06:35 +0000833 BlockOffset = CharUnits::fromQuantity(
Ken Dyck30a8a272010-01-26 19:13:33 +0000834 llvm::RoundUpToAlignment(BlockOffset.getQuantity(), Align.getQuantity()));
Mike Stumpa99038c2009-02-28 09:07:16 +0000835 BlockAlign = std::max(Align, BlockAlign);
Mike Stump00470a12009-03-05 08:32:30 +0000836
Ken Dyck199c3d62010-01-11 17:06:35 +0000837 CharUnits Pad = BlockOffset - OldOffset;
838 if (Pad.isPositive()) {
839 llvm::ArrayType::get(llvm::Type::getInt8Ty(VMContext), Pad.getQuantity());
Mike Stumpa99038c2009-02-28 09:07:16 +0000840 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
Ken Dyck199c3d62010-01-11 17:06:35 +0000841 llvm::APInt(32,
842 Pad.getQuantity()),
Mike Stumpa99038c2009-02-28 09:07:16 +0000843 ArrayType::Normal, 0);
844 ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000845 0, QualType(PadTy), 0, VarDecl::None);
Mike Stumpa99038c2009-02-28 09:07:16 +0000846 Expr *E;
847 E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
Douglas Gregor0da76df2009-11-23 11:41:28 +0000848 SourceLocation());
Mike Stumpa99038c2009-02-28 09:07:16 +0000849 BlockDeclRefDecls.push_back(E);
850 }
851 BlockDeclRefDecls.push_back(BDRE);
852
853 BlockOffset += Size;
854 return BlockOffset-Size;
855}
Mike Stumpdab514f2009-03-04 03:23:46 +0000856
Mike Stump08920992009-03-07 02:35:30 +0000857llvm::Constant *BlockFunction::
858GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000859 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000860 QualType R = getContext().VoidTy;
861
862 FunctionArgList Args;
863 // FIXME: This leaks
Mike Stump08920992009-03-07 02:35:30 +0000864 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +0000865 ImplicitParamDecl::Create(getContext(), 0,
866 SourceLocation(), 0,
Mike Stump08920992009-03-07 02:35:30 +0000867 getContext().getPointerType(getContext().VoidTy));
868 Args.push_back(std::make_pair(Dst, Dst->getType()));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000869 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +0000870 ImplicitParamDecl::Create(getContext(), 0,
871 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000872 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000873 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Mike Stumpa4f668f2009-03-06 01:33:24 +0000875 const CGFunctionInfo &FI =
John McCall04a67a62010-02-05 21:31:56 +0000876 CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000877
Mike Stump3899a7f2009-06-05 23:26:36 +0000878 // FIXME: We'd like to put these into a mergable by content, with
879 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000880 CodeGenTypes &Types = CGM.getTypes();
881 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
882
883 llvm::Function *Fn =
884 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000885 "__copy_helper_block_", &CGM.getModule());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000886
887 IdentifierInfo *II
888 = &CGM.getContext().Idents.get("__copy_helper_block_");
889
890 FunctionDecl *FD = FunctionDecl::Create(getContext(),
891 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000892 SourceLocation(), II, R, 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000893 FunctionDecl::Static, false,
894 true);
895 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump08920992009-03-07 02:35:30 +0000896
897 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
898 llvm::Type *PtrPtrT;
Mike Stump08920992009-03-07 02:35:30 +0000899
Mike Stumpb7477cf2009-04-10 18:52:28 +0000900 if (NoteForHelperp) {
901 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump08920992009-03-07 02:35:30 +0000902
Owen Anderson96e0fc72009-07-29 22:16:19 +0000903 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000904 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
905 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump08920992009-03-07 02:35:30 +0000906
Mike Stumpb7477cf2009-04-10 18:52:28 +0000907 llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000908 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000909 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000910 DstObj = Builder.CreateBitCast(DstObj, PtrPtrT);
911 DstObj = Builder.CreateLoad(DstObj);
Mike Stump08920992009-03-07 02:35:30 +0000912
Mike Stumpb7477cf2009-04-10 18:52:28 +0000913 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
914 int flag = NoteForHelper[i].flag;
915 int index = NoteForHelper[i].index;
Mike Stump08920992009-03-07 02:35:30 +0000916
Mike Stumpb7477cf2009-04-10 18:52:28 +0000917 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
918 || NoteForHelper[i].RequiresCopying) {
919 llvm::Value *Srcv = SrcObj;
920 Srcv = Builder.CreateStructGEP(Srcv, index);
921 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000922 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000923 Srcv = Builder.CreateLoad(Srcv);
924
925 llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
926 Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
927
Owen Anderson0032b272009-08-13 21:57:51 +0000928 llvm::Value *N = llvm::ConstantInt::get(
929 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000930 llvm::Value *F = getBlockObjectAssign();
931 Builder.CreateCall3(F, Dstv, Srcv, N);
932 }
Mike Stump08920992009-03-07 02:35:30 +0000933 }
934 }
935
Mike Stumpa4f668f2009-03-06 01:33:24 +0000936 CGF.FinishFunction();
937
Owen Anderson3c4972d2009-07-29 18:54:39 +0000938 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000939}
940
Mike Stumpcf62d392009-03-06 18:42:23 +0000941llvm::Constant *BlockFunction::
Mike Stump08920992009-03-07 02:35:30 +0000942GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
943 const llvm::StructType* T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000944 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000945 QualType R = getContext().VoidTy;
946
947 FunctionArgList Args;
948 // FIXME: This leaks
949 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +0000950 ImplicitParamDecl::Create(getContext(), 0,
951 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000952 getContext().getPointerType(getContext().VoidTy));
953
954 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Mike Stumpa4f668f2009-03-06 01:33:24 +0000956 const CGFunctionInfo &FI =
John McCall04a67a62010-02-05 21:31:56 +0000957 CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000958
Mike Stump3899a7f2009-06-05 23:26:36 +0000959 // FIXME: We'd like to put these into a mergable by content, with
960 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000961 CodeGenTypes &Types = CGM.getTypes();
962 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
963
964 llvm::Function *Fn =
965 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +0000966 "__destroy_helper_block_", &CGM.getModule());
Mike Stumpa4f668f2009-03-06 01:33:24 +0000967
968 IdentifierInfo *II
969 = &CGM.getContext().Idents.get("__destroy_helper_block_");
970
971 FunctionDecl *FD = FunctionDecl::Create(getContext(),
972 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000973 SourceLocation(), II, R, 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000974 FunctionDecl::Static, false,
975 true);
976 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1edf6b62009-03-07 02:53:18 +0000977
Mike Stumpb7477cf2009-04-10 18:52:28 +0000978 if (NoteForHelperp) {
979 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump1edf6b62009-03-07 02:53:18 +0000980
Mike Stumpb7477cf2009-04-10 18:52:28 +0000981 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
982 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000983 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000984 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
985 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump1edf6b62009-03-07 02:53:18 +0000986
Mike Stumpb7477cf2009-04-10 18:52:28 +0000987 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
988 int flag = NoteForHelper[i].flag;
989 int index = NoteForHelper[i].index;
Mike Stump1edf6b62009-03-07 02:53:18 +0000990
Mike Stumpb7477cf2009-04-10 18:52:28 +0000991 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
992 || NoteForHelper[i].RequiresCopying) {
993 llvm::Value *Srcv = SrcObj;
994 Srcv = Builder.CreateStructGEP(Srcv, index);
995 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000996 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000997 Srcv = Builder.CreateLoad(Srcv);
998
999 BuildBlockRelease(Srcv, flag);
1000 }
Mike Stump1edf6b62009-03-07 02:53:18 +00001001 }
1002 }
1003
Mike Stumpa4f668f2009-03-06 01:33:24 +00001004 CGF.FinishFunction();
1005
Owen Anderson3c4972d2009-07-29 18:54:39 +00001006 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpa4f668f2009-03-06 01:33:24 +00001007}
1008
Mike Stump08920992009-03-07 02:35:30 +00001009llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001010 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump08920992009-03-07 02:35:30 +00001011 return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
1012 T, NoteForHelper);
Mike Stumpa4f668f2009-03-06 01:33:24 +00001013}
1014
Mike Stump08920992009-03-07 02:35:30 +00001015llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001016 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stump08920992009-03-07 02:35:30 +00001017 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001018 T, NoteForHelperp);
Mike Stumpdab514f2009-03-04 03:23:46 +00001019}
Mike Stump797b6322009-03-05 01:23:13 +00001020
Mike Stumpee094222009-03-06 06:12:24 +00001021llvm::Constant *BlockFunction::
1022GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
Mike Stump45031c02009-03-06 02:29:21 +00001023 QualType R = getContext().VoidTy;
1024
1025 FunctionArgList Args;
1026 // FIXME: This leaks
Mike Stumpee094222009-03-06 06:12:24 +00001027 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +00001028 ImplicitParamDecl::Create(getContext(), 0,
1029 SourceLocation(), 0,
Mike Stumpee094222009-03-06 06:12:24 +00001030 getContext().getPointerType(getContext().VoidTy));
1031 Args.push_back(std::make_pair(Dst, Dst->getType()));
1032
1033 // FIXME: This leaks
Mike Stump45031c02009-03-06 02:29:21 +00001034 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001035 ImplicitParamDecl::Create(getContext(), 0,
1036 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001037 getContext().getPointerType(getContext().VoidTy));
Mike Stump45031c02009-03-06 02:29:21 +00001038 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001039
Mike Stump45031c02009-03-06 02:29:21 +00001040 const CGFunctionInfo &FI =
John McCall04a67a62010-02-05 21:31:56 +00001041 CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false);
Mike Stump45031c02009-03-06 02:29:21 +00001042
Mike Stump45031c02009-03-06 02:29:21 +00001043 CodeGenTypes &Types = CGM.getTypes();
1044 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1045
Mike Stump3899a7f2009-06-05 23:26:36 +00001046 // FIXME: We'd like to put these into a mergable by content, with
1047 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001048 llvm::Function *Fn =
1049 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001050 "__Block_byref_id_object_copy_", &CGM.getModule());
Mike Stump45031c02009-03-06 02:29:21 +00001051
1052 IdentifierInfo *II
1053 = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
1054
1055 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1056 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001057 SourceLocation(), II, R, 0,
Mike Stump45031c02009-03-06 02:29:21 +00001058 FunctionDecl::Static, false,
1059 true);
1060 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpee094222009-03-06 06:12:24 +00001061
1062 // dst->x
1063 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001064 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001065 V = Builder.CreateLoad(V);
Mike Stumpee094222009-03-06 06:12:24 +00001066 V = Builder.CreateStructGEP(V, 6, "x");
1067 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
1068
1069 // src->x
1070 V = CGF.GetAddrOfLocalVar(Src);
1071 V = Builder.CreateLoad(V);
1072 V = Builder.CreateBitCast(V, T);
1073 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001074 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpee094222009-03-06 06:12:24 +00001075 llvm::Value *SrcObj = Builder.CreateLoad(V);
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Mike Stumpee094222009-03-06 06:12:24 +00001077 flag |= BLOCK_BYREF_CALLER;
1078
Owen Anderson0032b272009-08-13 21:57:51 +00001079 llvm::Value *N = llvm::ConstantInt::get(
1080 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpee094222009-03-06 06:12:24 +00001081 llvm::Value *F = getBlockObjectAssign();
1082 Builder.CreateCall3(F, DstObj, SrcObj, N);
1083
Mike Stump45031c02009-03-06 02:29:21 +00001084 CGF.FinishFunction();
1085
Owen Anderson3c4972d2009-07-29 18:54:39 +00001086 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001087}
1088
Mike Stump1851b682009-03-06 04:53:30 +00001089llvm::Constant *
1090BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
1091 int flag) {
Mike Stump45031c02009-03-06 02:29:21 +00001092 QualType R = getContext().VoidTy;
1093
1094 FunctionArgList Args;
1095 // FIXME: This leaks
1096 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001097 ImplicitParamDecl::Create(getContext(), 0,
1098 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001099 getContext().getPointerType(getContext().VoidTy));
1100
1101 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001102
Mike Stump45031c02009-03-06 02:29:21 +00001103 const CGFunctionInfo &FI =
John McCall04a67a62010-02-05 21:31:56 +00001104 CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false);
Mike Stump45031c02009-03-06 02:29:21 +00001105
Mike Stump45031c02009-03-06 02:29:21 +00001106 CodeGenTypes &Types = CGM.getTypes();
1107 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1108
Mike Stump3899a7f2009-06-05 23:26:36 +00001109 // FIXME: We'd like to put these into a mergable by content, with
1110 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001111 llvm::Function *Fn =
1112 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
Benjamin Kramer3cf7c5d2010-01-22 13:59:13 +00001113 "__Block_byref_id_object_dispose_",
Mike Stump45031c02009-03-06 02:29:21 +00001114 &CGM.getModule());
1115
1116 IdentifierInfo *II
1117 = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
1118
1119 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1120 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001121 SourceLocation(), II, R, 0,
Mike Stump45031c02009-03-06 02:29:21 +00001122 FunctionDecl::Static, false,
1123 true);
1124 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1851b682009-03-06 04:53:30 +00001125
1126 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001127 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001128 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001129 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001130 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001131 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001132
Mike Stump1851b682009-03-06 04:53:30 +00001133 flag |= BLOCK_BYREF_CALLER;
1134 BuildBlockRelease(V, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001135 CGF.FinishFunction();
1136
Owen Anderson3c4972d2009-07-29 18:54:39 +00001137 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001138}
1139
Mike Stumpee094222009-03-06 06:12:24 +00001140llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001141 int Flag, unsigned Align) {
1142 // All alignments below that of pointer alignment collapse down to just
Mike Stump3899a7f2009-06-05 23:26:36 +00001143 // pointer alignment, as we always have at least that much alignment to begin
1144 // with.
1145 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001146
Mike Stump3899a7f2009-06-05 23:26:36 +00001147 // As an optimization, we only generate a single function of each kind we
1148 // might need. We need a different one for each alignment and for each
1149 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001150 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1151 llvm::Constant *&Entry = CGM.AssignCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001152 if (Entry)
1153 return Entry;
Chris Lattner10976d92009-12-05 08:21:30 +00001154 return Entry = CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, Flag);
Mike Stump45031c02009-03-06 02:29:21 +00001155}
1156
Mike Stump1851b682009-03-06 04:53:30 +00001157llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001158 int Flag,
Mike Stump3899a7f2009-06-05 23:26:36 +00001159 unsigned Align) {
1160 // All alignments below that of pointer alignment collpase down to just
1161 // pointer alignment, as we always have at least that much alignment to begin
1162 // with.
1163 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001164
Mike Stump3899a7f2009-06-05 23:26:36 +00001165 // As an optimization, we only generate a single function of each kind we
1166 // might need. We need a different one for each alignment and for each
1167 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001168 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1169 llvm::Constant *&Entry = CGM.DestroyCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001170 if (Entry)
1171 return Entry;
Chris Lattner10976d92009-12-05 08:21:30 +00001172 return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, Flag);
Mike Stump45031c02009-03-06 02:29:21 +00001173}
1174
Mike Stump797b6322009-03-05 01:23:13 +00001175llvm::Value *BlockFunction::getBlockObjectDispose() {
1176 if (CGM.BlockObjectDispose == 0) {
1177 const llvm::FunctionType *FTy;
1178 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001179 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stump797b6322009-03-05 01:23:13 +00001180 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001181 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson96e0fc72009-07-29 22:16:19 +00001182 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stump00470a12009-03-05 08:32:30 +00001183 CGM.BlockObjectDispose
Mike Stump797b6322009-03-05 01:23:13 +00001184 = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
1185 }
1186 return CGM.BlockObjectDispose;
1187}
1188
Mike Stumpee094222009-03-06 06:12:24 +00001189llvm::Value *BlockFunction::getBlockObjectAssign() {
1190 if (CGM.BlockObjectAssign == 0) {
1191 const llvm::FunctionType *FTy;
1192 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001193 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stumpee094222009-03-06 06:12:24 +00001194 ArgTys.push_back(PtrToInt8Ty);
1195 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001196 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson96e0fc72009-07-29 22:16:19 +00001197 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stumpee094222009-03-06 06:12:24 +00001198 CGM.BlockObjectAssign
1199 = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
1200 }
1201 return CGM.BlockObjectAssign;
1202}
1203
Mike Stump1851b682009-03-06 04:53:30 +00001204void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Mike Stump797b6322009-03-05 01:23:13 +00001205 llvm::Value *F = getBlockObjectDispose();
Mike Stump1851b682009-03-06 04:53:30 +00001206 llvm::Value *N;
Mike Stump797b6322009-03-05 01:23:13 +00001207 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001208 N = llvm::ConstantInt::get(llvm::Type::getInt32Ty(V->getContext()), flag);
Mike Stump797b6322009-03-05 01:23:13 +00001209 Builder.CreateCall2(F, V, N);
1210}
Mike Stump00470a12009-03-05 08:32:30 +00001211
1212ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stump08920992009-03-07 02:35:30 +00001213
1214BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1215 CGBuilderTy &B)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001216 : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) {
Owen Anderson0032b272009-08-13 21:57:51 +00001217 PtrToInt8Ty = llvm::PointerType::getUnqual(
1218 llvm::Type::getInt8Ty(VMContext));
Mike Stump08920992009-03-07 02:35:30 +00001219
1220 BlockHasCopyDispose = false;
1221}