blob: 1bece7fec6fa7c9e4619bd37e9fcaf335c4220f5 [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"
16#include "CodeGenModule.h"
Mike Stump6cc88f72009-03-20 21:53:12 +000017#include "clang/AST/DeclObjC.h"
Anders Carlssonacfde802009-02-12 00:39:25 +000018#include "llvm/Module.h"
Anders Carlssond5cab542009-02-12 17:55:02 +000019#include "llvm/Target/TargetData.h"
Anders Carlssonacfde802009-02-12 00:39:25 +000020#include <algorithm>
Torok Edwinf42e4a62009-08-24 13:25:12 +000021#include <cstdio>
22
Anders Carlssonacfde802009-02-12 00:39:25 +000023using namespace clang;
24using namespace CodeGen;
25
Mike Stumpcf62d392009-03-06 18:42:23 +000026llvm::Constant *CodeGenFunction::
Mike Stumpa803b0e2009-03-25 17:58:24 +000027BuildDescriptorBlockDecl(bool BlockHasCopyDispose, uint64_t Size,
28 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.
Owen Anderson4a28d5d2009-07-24 23:12:58 +000043 C = llvm::ConstantInt::get(UnsignedLongTy, Size);
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) {
113 // Always allocate self, as it is often handy in the debugger, even if there
114 // is no codegen in the block that uses it. This is also useful to always do
115 // this as if we didn't, we'd have to figure out all code that uses a self
116 // pointer, including implicit uses.
117 if (const ObjCMethodDecl *OMD
118 = dyn_cast_or_null<ObjCMethodDecl>(CGF->CurFuncDecl)) {
119 ImplicitParamDecl *SelfDecl = OMD->getSelfDecl();
120 BlockDeclRefExpr *BDRE = new (CGF->getContext())
121 BlockDeclRefExpr(SelfDecl,
122 SelfDecl->getType(), SourceLocation(), false);
123 CGF->AllocateBlockDecl(BDRE);
124 }
125
126 // 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
151 bool hasIntrospection = CGM.getContext().getLangOptions().BlockIntrospection;
152
153 if (hasIntrospection) {
154 BlockFields++;
155 }
156 std::vector<llvm::Constant*> Elts(BlockFields);
157
158 if (hasIntrospection) {
159 std::string BlockTypeEncoding;
160 CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
161
162 Elts[5] = llvm::ConstantExpr::getBitCast(
163 CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty);
164 }
165
Mike Stumpe5fee252009-02-13 16:19:19 +0000166 llvm::Constant *C;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000167 llvm::Value *V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000168
Mike Stumpe5fee252009-02-13 16:19:19 +0000169 {
170 // C = BuildBlockStructInitlist();
171 unsigned int flags = BLOCK_HAS_DESCRIPTOR;
172
David Chisnall5e530af2009-11-17 19:33:30 +0000173 if (hasIntrospection)
174 flags |= BLOCK_HAS_OBJC_TYPE;
175
Mike Stump00470a12009-03-05 08:32:30 +0000176 // We run this first so that we set BlockHasCopyDispose from the entire
177 // block literal.
178 // __invoke
179 uint64_t subBlockSize, subBlockAlign;
180 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Mike Stumpa803b0e2009-03-25 17:58:24 +0000181 bool subBlockHasCopyDispose = false;
Mike Stump00470a12009-03-05 08:32:30 +0000182 llvm::Function *Fn
Mike Stumpbb304192009-09-24 22:31:14 +0000183 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl,
184 LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000185 subBlockSize,
Mike Stump00470a12009-03-05 08:32:30 +0000186 subBlockAlign,
187 subBlockDeclRefDecls,
Mike Stumpa803b0e2009-03-25 17:58:24 +0000188 subBlockHasCopyDispose);
189 BlockHasCopyDispose |= subBlockHasCopyDispose;
Mike Stump00470a12009-03-05 08:32:30 +0000190 Elts[3] = Fn;
191
Mike Stumpf5408fe2009-05-16 07:57:57 +0000192 // FIXME: Don't use BlockHasCopyDispose, it is set more often then
193 // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); }
Mike Stumpa803b0e2009-03-25 17:58:24 +0000194 if (subBlockHasCopyDispose)
Mike Stumpe5fee252009-02-13 16:19:19 +0000195 flags |= BLOCK_HAS_COPY_DISPOSE;
196
Mike Stump7d6dc4f2009-02-13 20:17:16 +0000197 // __isa
Mike Stump59c5b112009-02-13 19:29:27 +0000198 C = CGM.getNSConcreteStackBlock();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000199 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
Mike Stump00470a12009-03-05 08:32:30 +0000200 Elts[0] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000201
202 // __flags
203 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
204 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000205 C = llvm::ConstantInt::get(IntTy, flags);
Mike Stump00470a12009-03-05 08:32:30 +0000206 Elts[1] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000207
208 // __reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000209 C = llvm::ConstantInt::get(IntTy, 0);
Mike Stump00470a12009-03-05 08:32:30 +0000210 Elts[2] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000211
Mike Stump8a2b4b12009-02-25 23:33:13 +0000212 if (subBlockDeclRefDecls.size() == 0) {
Mike Stumpcf62d392009-03-06 18:42:23 +0000213 // __descriptor
Mike Stumpea26cb52009-10-21 03:49:08 +0000214 Elts[4] = BuildDescriptorBlockDecl(subBlockHasCopyDispose, subBlockSize,
215 0, 0);
Mike Stumpcf62d392009-03-06 18:42:23 +0000216
Mike Stump5570cfe2009-03-01 20:07:53 +0000217 // Optimize to being a global block.
218 Elts[0] = CGM.getNSConcreteGlobalBlock();
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000219 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
Mike Stump5570cfe2009-03-01 20:07:53 +0000220
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000221 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000222
223 char Name[32];
224 sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
Owen Anderson1c431b32009-07-08 19:05:04 +0000225 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Mike Stump8a2b4b12009-02-25 23:33:13 +0000226 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000227 C, Name);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000228 QualType BPT = BE->getType();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000229 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000230 return C;
231 }
Mike Stump00470a12009-03-05 08:32:30 +0000232
David Chisnall5e530af2009-11-17 19:33:30 +0000233 std::vector<const llvm::Type *> Types(BlockFields+subBlockDeclRefDecls.size());
Mike Stump08920992009-03-07 02:35:30 +0000234 for (int i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000235 Types[i] = Elts[i]->getType();
Mike Stump08920992009-03-07 02:35:30 +0000236 Types[4] = PtrToInt8Ty;
David Chisnall5e530af2009-11-17 19:33:30 +0000237 if (hasIntrospection)
238 Types[5] = PtrToInt8Ty;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000239
Mike Stumpa99038c2009-02-28 09:07:16 +0000240 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
241 const Expr *E = subBlockDeclRefDecls[i];
242 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
243 QualType Ty = E->getType();
Mike Stumpdab514f2009-03-04 03:23:46 +0000244 if (BDRE && BDRE->isByRef()) {
David Chisnall5e530af2009-11-17 19:33:30 +0000245 Types[i+BlockFields] = llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000246 } else
David Chisnall5e530af2009-11-17 19:33:30 +0000247 Types[i+BlockFields] = ConvertType(Ty);
Mike Stumpa99038c2009-02-28 09:07:16 +0000248 }
Mike Stump8a2b4b12009-02-25 23:33:13 +0000249
Owen Anderson47a434f2009-08-05 23:18:46 +0000250 llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000251
252 llvm::AllocaInst *A = CreateTempAlloca(Ty);
253 A->setAlignment(subBlockAlign);
254 V = A;
255
Mike Stump08920992009-03-07 02:35:30 +0000256 std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size());
257 int helpersize = 0;
258
259 for (unsigned i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000260 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
David Chisnall5e530af2009-11-17 19:33:30 +0000261 if (hasIntrospection)
262 Builder.CreateStore(Elts[5], Builder.CreateStructGEP(V, 5, "block.tmp"));
Mike Stump00470a12009-03-05 08:32:30 +0000263
Mike Stump8a2b4b12009-02-25 23:33:13 +0000264 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
265 {
Mike Stumpa99038c2009-02-28 09:07:16 +0000266 // FIXME: Push const down.
267 Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
268 DeclRefExpr *DR;
269 ValueDecl *VD;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000270
Mike Stumpa99038c2009-02-28 09:07:16 +0000271 DR = dyn_cast<DeclRefExpr>(E);
272 // Skip padding.
273 if (DR) continue;
274
275 BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
276 VD = BDRE->getDecl();
277
David Chisnall5e530af2009-11-17 19:33:30 +0000278 llvm::Value* Addr = Builder.CreateStructGEP(V, i+BlockFields, "tmp");
Mike Stump08920992009-03-07 02:35:30 +0000279 NoteForHelper[helpersize].index = i+5;
Mike Stump39605b42009-09-22 02:12:52 +0000280 NoteForHelper[helpersize].RequiresCopying
281 = BlockRequiresCopying(VD->getType());
Mike Stump08920992009-03-07 02:35:30 +0000282 NoteForHelper[helpersize].flag
Mike Stump39605b42009-09-22 02:12:52 +0000283 = (VD->getType()->isBlockPointerType()
284 ? BLOCK_FIELD_IS_BLOCK
285 : BLOCK_FIELD_IS_OBJECT);
Mike Stump08920992009-03-07 02:35:30 +0000286
Mike Stumpa99038c2009-02-28 09:07:16 +0000287 if (LocalDeclMap[VD]) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000288 if (BDRE->isByRef()) {
Mike Stump08920992009-03-07 02:35:30 +0000289 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
Mike Stumpf4bc3122009-03-07 06:04:31 +0000290 // FIXME: Someone double check this.
291 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000292 llvm::Value *Loc = LocalDeclMap[VD];
293 Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000294 Loc = Builder.CreateLoad(Loc);
Mike Stumpdab514f2009-03-04 03:23:46 +0000295 Builder.CreateStore(Loc, Addr);
Mike Stump08920992009-03-07 02:35:30 +0000296 ++helpersize;
Mike Stumpdab514f2009-03-04 03:23:46 +0000297 continue;
298 } else
John McCalldbd872f2009-12-08 09:08:17 +0000299 E = new (getContext()) DeclRefExpr (VD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000300 VD->getType(),
301 SourceLocation());
Mike Stumpa99038c2009-02-28 09:07:16 +0000302 }
Mike Stumpdab514f2009-03-04 03:23:46 +0000303 if (BDRE->isByRef()) {
Mike Stumpa8b60c92009-03-21 21:00:35 +0000304 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
305 // FIXME: Someone double check this.
306 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpa99038c2009-02-28 09:07:16 +0000307 E = new (getContext())
308 UnaryOperator(E, UnaryOperator::AddrOf,
309 getContext().getPointerType(E->getType()),
310 SourceLocation());
Mike Stumpdab514f2009-03-04 03:23:46 +0000311 }
Mike Stump08920992009-03-07 02:35:30 +0000312 ++helpersize;
Mike Stumpa99038c2009-02-28 09:07:16 +0000313
Mike Stumpa99038c2009-02-28 09:07:16 +0000314 RValue r = EmitAnyExpr(E, Addr, false);
Mike Stumpdab514f2009-03-04 03:23:46 +0000315 if (r.isScalar()) {
316 llvm::Value *Loc = r.getScalarVal();
David Chisnall5e530af2009-11-17 19:33:30 +0000317 const llvm::Type *Ty = Types[i+BlockFields];
Mike Stumpdab514f2009-03-04 03:23:46 +0000318 if (BDRE->isByRef()) {
Mike Stump58a85142009-03-04 22:48:06 +0000319 // E is now the address of the value field, instead, we want the
320 // address of the actual ByRef struct. We optimize this slightly
321 // compared to gcc by not grabbing the forwarding slot as this must
322 // be done during Block_copy for us, and we can postpone the work
323 // until then.
324 uint64_t offset = BlockDecls[BDRE->getDecl()];
Mike Stump00470a12009-03-05 08:32:30 +0000325
Mike Stump58a85142009-03-04 22:48:06 +0000326 llvm::Value *BlockLiteral = LoadBlockStruct();
Mike Stump00470a12009-03-05 08:32:30 +0000327
Mike Stump58a85142009-03-04 22:48:06 +0000328 Loc = Builder.CreateGEP(BlockLiteral,
Mike Stumpea26cb52009-10-21 03:49:08 +0000329 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Mike Stump58a85142009-03-04 22:48:06 +0000330 offset),
331 "block.literal");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000332 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000333 Loc = Builder.CreateBitCast(Loc, Ty);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000334 Loc = Builder.CreateLoad(Loc);
Mike Stump58a85142009-03-04 22:48:06 +0000335 // Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000336 }
337 Builder.CreateStore(Loc, Addr);
338 } else if (r.isComplex())
Mike Stump8a2b4b12009-02-25 23:33:13 +0000339 // FIXME: implement
340 ErrorUnsupported(BE, "complex in block literal");
341 else if (r.isAggregate())
342 ; // Already created into the destination
343 else
344 assert (0 && "bad block variable");
345 // FIXME: Ensure that the offset created by the backend for
346 // the struct matches the previously computed offset in BlockDecls.
347 }
Mike Stump08920992009-03-07 02:35:30 +0000348 NoteForHelper.resize(helpersize);
Mike Stumpcf62d392009-03-06 18:42:23 +0000349
350 // __descriptor
Mike Stumpa803b0e2009-03-25 17:58:24 +0000351 llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockHasCopyDispose,
352 subBlockSize, Ty,
Mike Stump08920992009-03-07 02:35:30 +0000353 &NoteForHelper);
354 Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
355 Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
Mike Stumpe5fee252009-02-13 16:19:19 +0000356 }
Mike Stump00470a12009-03-05 08:32:30 +0000357
Mike Stumpbd65cac2009-02-19 01:01:04 +0000358 QualType BPT = BE->getType();
Mike Stump8a2b4b12009-02-25 23:33:13 +0000359 return Builder.CreateBitCast(V, ConvertType(BPT));
Mike Stumpe5fee252009-02-13 16:19:19 +0000360}
361
362
Mike Stump2a998142009-03-04 18:17:45 +0000363const llvm::Type *BlockModule::getBlockDescriptorType() {
Mike Stumpab695142009-02-13 15:16:56 +0000364 if (BlockDescriptorType)
365 return BlockDescriptorType;
366
Mike Stumpa5448542009-02-13 15:32:32 +0000367 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000368 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000369
Mike Stumpab695142009-02-13 15:16:56 +0000370 // struct __block_descriptor {
371 // unsigned long reserved;
372 // unsigned long block_size;
373 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000374 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(),
375 UnsignedLongTy,
Mike Stumpa5448542009-02-13 15:32:32 +0000376 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000377 NULL);
378
379 getModule().addTypeName("struct.__block_descriptor",
380 BlockDescriptorType);
381
382 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000383}
384
Mike Stump2a998142009-03-04 18:17:45 +0000385const llvm::Type *BlockModule::getGenericBlockLiteralType() {
Mike Stump9b8a7972009-02-13 15:25:34 +0000386 if (GenericBlockLiteralType)
387 return GenericBlockLiteralType;
388
Mike Stumpa5448542009-02-13 15:32:32 +0000389 const llvm::Type *BlockDescPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000390 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000391
Mike Stump7cbb3602009-02-13 16:01:35 +0000392 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
393 getTypes().ConvertType(getContext().IntTy));
394
Mike Stump9b8a7972009-02-13 15:25:34 +0000395 // struct __block_literal_generic {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000396 // void *__isa;
397 // int __flags;
398 // int __reserved;
399 // void (*__invoke)(void *);
400 // struct __block_descriptor *__descriptor;
David Chisnall5e530af2009-11-17 19:33:30 +0000401 // // GNU runtime only:
402 // const char *types;
Mike Stump9b8a7972009-02-13 15:25:34 +0000403 // };
David Chisnall5e530af2009-11-17 19:33:30 +0000404 if (CGM.getContext().getLangOptions().BlockIntrospection)
405 GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
406 PtrToInt8Ty,
407 IntTy,
408 IntTy,
409 PtrToInt8Ty,
410 BlockDescPtrTy,
411 PtrToInt8Ty,
412 NULL);
413 else
414 GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
Owen Anderson47a434f2009-08-05 23:18:46 +0000415 PtrToInt8Ty,
Mike Stump7cbb3602009-02-13 16:01:35 +0000416 IntTy,
417 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000418 PtrToInt8Ty,
Mike Stump9b8a7972009-02-13 15:25:34 +0000419 BlockDescPtrTy,
420 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000421
Mike Stump9b8a7972009-02-13 15:25:34 +0000422 getModule().addTypeName("struct.__block_literal_generic",
423 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000424
Mike Stump9b8a7972009-02-13 15:25:34 +0000425 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000426}
427
Mike Stump2a998142009-03-04 18:17:45 +0000428const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000429 if (GenericExtendedBlockLiteralType)
430 return GenericExtendedBlockLiteralType;
431
Mike Stumpbd65cac2009-02-19 01:01:04 +0000432 const llvm::Type *BlockDescPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000433 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpbd65cac2009-02-19 01:01:04 +0000434
435 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
436 getTypes().ConvertType(getContext().IntTy));
437
438 // struct __block_literal_generic {
439 // void *__isa;
440 // int __flags;
441 // int __reserved;
442 // void (*__invoke)(void *);
443 // struct __block_descriptor *__descriptor;
444 // void *__copy_func_helper_decl;
445 // void *__destroy_func_decl;
446 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000447 GenericExtendedBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
448 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000449 IntTy,
450 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000451 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000452 BlockDescPtrTy,
Mike Stump797b6322009-03-05 01:23:13 +0000453 PtrToInt8Ty,
454 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000455 NULL);
456
457 getModule().addTypeName("struct.__block_literal_extended_generic",
458 GenericExtendedBlockLiteralType);
459
460 return GenericExtendedBlockLiteralType;
461}
462
Anders Carlssona1736c02009-12-24 21:13:40 +0000463RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E,
464 ReturnValueSlot ReturnValue) {
Mike Stumpa5448542009-02-13 15:32:32 +0000465 const BlockPointerType *BPT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000466 E->getCallee()->getType()->getAs<BlockPointerType>();
Mike Stumpa5448542009-02-13 15:32:32 +0000467
Anders Carlssonacfde802009-02-12 00:39:25 +0000468 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
469
470 // Get a pointer to the generic block literal.
471 const llvm::Type *BlockLiteralTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000472 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000473
474 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000475 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000476 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
477
478 // Get the function pointer from the literal.
479 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Anders Carlssonacfde802009-02-12 00:39:25 +0000480
Mike Stumpa5448542009-02-13 15:32:32 +0000481 BlockLiteral =
482 Builder.CreateBitCast(BlockLiteral,
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000483 llvm::Type::getInt8PtrTy(VMContext),
Anders Carlssonacfde802009-02-12 00:39:25 +0000484 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000485
Anders Carlssonacfde802009-02-12 00:39:25 +0000486 // Add the block literal.
487 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
488 CallArgList Args;
489 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000490
Anders Carlsson782f3972009-04-08 23:13:16 +0000491 QualType FnType = BPT->getPointeeType();
492
Anders Carlssonacfde802009-02-12 00:39:25 +0000493 // And the rest of the arguments.
John McCall183700f2009-09-21 23:43:11 +0000494 EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(),
Anders Carlsson782f3972009-04-08 23:13:16 +0000495 E->arg_begin(), E->arg_end());
Mike Stumpa5448542009-02-13 15:32:32 +0000496
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000497 // Load the function.
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000498 llvm::Value *Func = Builder.CreateLoad(FuncPtr, "tmp");
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000499
John McCall183700f2009-09-21 23:43:11 +0000500 QualType ResultType = FnType->getAs<FunctionType>()->getResultType();
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000501
Mike Stump1eb44332009-09-09 15:08:12 +0000502 const CGFunctionInfo &FnInfo =
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000503 CGM.getTypes().getFunctionInfo(ResultType, Args);
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000505 // Cast the function pointer to the right type.
Mike Stump1eb44332009-09-09 15:08:12 +0000506 const llvm::Type *BlockFTy =
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000507 CGM.getTypes().GetFunctionType(FnInfo, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Owen Anderson96e0fc72009-07-29 22:16:19 +0000509 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000510 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Anders Carlssonacfde802009-02-12 00:39:25 +0000512 // And call the block.
Anders Carlssona1736c02009-12-24 21:13:40 +0000513 return EmitCall(FnInfo, Func, ReturnValue, Args);
Anders Carlssonacfde802009-02-12 00:39:25 +0000514}
Anders Carlssond5cab542009-02-12 17:55:02 +0000515
Mike Stumpea26cb52009-10-21 03:49:08 +0000516uint64_t CodeGenFunction::AllocateBlockDecl(const BlockDeclRefExpr *E) {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000517 const ValueDecl *VD = E->getDecl();
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000518 uint64_t &offset = BlockDecls[VD];
Mike Stumpdab514f2009-03-04 03:23:46 +0000519
Mike Stumpdab514f2009-03-04 03:23:46 +0000520 // See if we have already allocated an offset for this variable.
Mike Stumpea26cb52009-10-21 03:49:08 +0000521 if (offset)
522 return offset;
523
524 // Don't run the expensive check, unless we have to.
Mike Stump083c25e2009-10-22 00:49:09 +0000525 if (!BlockHasCopyDispose)
526 if (E->isByRef()
527 || BlockRequiresCopying(E->getType()))
528 BlockHasCopyDispose = true;
Mike Stumpea26cb52009-10-21 03:49:08 +0000529
530 // if not, allocate one now.
531 offset = getBlockOffset(E);
532
533 return offset;
534}
535
536llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
537 const ValueDecl *VD = E->getDecl();
538 uint64_t offset = AllocateBlockDecl(E);
539
Mike Stumpdab514f2009-03-04 03:23:46 +0000540
541 llvm::Value *BlockLiteral = LoadBlockStruct();
542 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
Mike Stumpea26cb52009-10-21 03:49:08 +0000543 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000544 offset),
Mike Stump58a85142009-03-04 22:48:06 +0000545 "block.literal");
Mike Stumpdab514f2009-03-04 03:23:46 +0000546 if (E->isByRef()) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000547 const llvm::Type *PtrStructTy
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000548 = llvm::PointerType::get(BuildByRefType(VD), 0);
Mike Stumpa8b60c92009-03-21 21:00:35 +0000549 // The block literal will need a copy/destroy helper.
550 BlockHasCopyDispose = true;
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000551
552 const llvm::Type *Ty = PtrStructTy;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000553 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000554 V = Builder.CreateBitCast(V, Ty);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000555 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000556 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000557 V = Builder.CreateLoad(V);
Mike Stumpdab514f2009-03-04 03:23:46 +0000558 V = Builder.CreateBitCast(V, PtrStructTy);
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000559 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
560 VD->getNameAsString());
Mike Stumpdab514f2009-03-04 03:23:46 +0000561 } else {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000562 const llvm::Type *Ty = CGM.getTypes().ConvertType(VD->getType());
563
Owen Anderson96e0fc72009-07-29 22:16:19 +0000564 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000565 V = Builder.CreateBitCast(V, Ty);
566 }
567 return V;
568}
569
Mike Stump6cc88f72009-03-20 21:53:12 +0000570void CodeGenFunction::BlockForwardSelf() {
571 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
572 ImplicitParamDecl *SelfDecl = OMD->getSelfDecl();
573 llvm::Value *&DMEntry = LocalDeclMap[SelfDecl];
574 if (DMEntry)
575 return;
576 // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care
577 BlockDeclRefExpr *BDRE = new (getContext())
578 BlockDeclRefExpr(SelfDecl,
579 SelfDecl->getType(), SourceLocation(), false);
580 DMEntry = GetAddrOfBlockDecl(BDRE);
581}
582
Mike Stump67a64482009-02-14 22:16:35 +0000583llvm::Constant *
Mike Stump90a90432009-03-04 18:47:42 +0000584BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000585 // Generate the block descriptor.
586 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000587 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
588 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000589
Anders Carlssond5cab542009-02-12 17:55:02 +0000590 llvm::Constant *DescriptorFields[2];
Mike Stumpa5448542009-02-13 15:32:32 +0000591
Anders Carlssond5cab542009-02-12 17:55:02 +0000592 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000593 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000594
Anders Carlssond5cab542009-02-12 17:55:02 +0000595 // Block literal size. For global blocks we just use the size of the generic
596 // block literal struct.
Mike Stumpa5448542009-02-13 15:32:32 +0000597 uint64_t BlockLiteralSize =
Mike Stump9b8a7972009-02-13 15:25:34 +0000598 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Owen Andersona1cf15f2009-07-14 23:10:40 +0000599 DescriptorFields[1] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000600 llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpa5448542009-02-13 15:32:32 +0000601
602 llvm::Constant *DescriptorStruct =
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000603 llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 2, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000604
Anders Carlssond5cab542009-02-12 17:55:02 +0000605 llvm::GlobalVariable *Descriptor =
Owen Anderson1c431b32009-07-08 19:05:04 +0000606 new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000607 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000608 DescriptorStruct, "__block_descriptor_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000609
David Chisnall5e530af2009-11-17 19:33:30 +0000610 int FieldCount = 5;
Anders Carlssond5cab542009-02-12 17:55:02 +0000611 // Generate the constants for the block literal.
David Chisnall5e530af2009-11-17 19:33:30 +0000612 if (CGM.getContext().getLangOptions().BlockIntrospection)
613 FieldCount = 6;
614
615 std::vector<llvm::Constant*> LiteralFields(FieldCount);
Mike Stumpa5448542009-02-13 15:32:32 +0000616
Mike Stump67a64482009-02-14 22:16:35 +0000617 CodeGenFunction::BlockInfo Info(0, n);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000618 uint64_t subBlockSize, subBlockAlign;
Mike Stumpa99038c2009-02-28 09:07:16 +0000619 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Daniel Dunbard67b09a2009-03-12 03:07:24 +0000620 bool subBlockHasCopyDispose = false;
Mike Stump7f28a9c2009-03-13 23:34:28 +0000621 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000622 llvm::Function *Fn
Mike Stump6cc88f72009-03-20 21:53:12 +0000623 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000624 subBlockSize,
Mike Stump90a90432009-03-04 18:47:42 +0000625 subBlockAlign,
Mike Stump00470a12009-03-05 08:32:30 +0000626 subBlockDeclRefDecls,
627 subBlockHasCopyDispose);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000628 assert(subBlockSize == BlockLiteralSize
629 && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000630
Anders Carlssond5cab542009-02-12 17:55:02 +0000631 // isa
Mike Stumpf99f1d02009-02-13 17:23:42 +0000632 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000633
Anders Carlssond5cab542009-02-12 17:55:02 +0000634 // Flags
David Chisnall5e530af2009-11-17 19:33:30 +0000635 LiteralFields[1] = CGM.getContext().getLangOptions().BlockIntrospection ?
636 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR |
637 BLOCK_HAS_OBJC_TYPE) :
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000638 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR);
Mike Stumpa5448542009-02-13 15:32:32 +0000639
Anders Carlssond5cab542009-02-12 17:55:02 +0000640 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000641 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000642
Anders Carlssond5cab542009-02-12 17:55:02 +0000643 // Function
644 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000645
Anders Carlssond5cab542009-02-12 17:55:02 +0000646 // Descriptor
647 LiteralFields[4] = Descriptor;
David Chisnall5e530af2009-11-17 19:33:30 +0000648
649 // Type encoding
650 if (CGM.getContext().getLangOptions().BlockIntrospection) {
651 std::string BlockTypeEncoding;
652 CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
653
654 LiteralFields[5] = CGM.GetAddrOfConstantCString(BlockTypeEncoding);
655 }
Mike Stumpa5448542009-02-13 15:32:32 +0000656
657 llvm::Constant *BlockLiteralStruct =
David Chisnall5e530af2009-11-17 19:33:30 +0000658 llvm::ConstantStruct::get(VMContext, LiteralFields, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000659
660 llvm::GlobalVariable *BlockLiteral =
Owen Anderson1c431b32009-07-08 19:05:04 +0000661 new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000662 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000663 BlockLiteralStruct, "__block_literal_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000664
Anders Carlssond5cab542009-02-12 17:55:02 +0000665 return BlockLiteral;
666}
667
Mike Stump4e7a1f72009-02-21 20:00:35 +0000668llvm::Value *CodeGenFunction::LoadBlockStruct() {
Mike Stumpbf1914b2009-10-20 20:30:01 +0000669 llvm::Value *V = Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()],
670 "self");
671 // For now, we codegen based upon byte offsets.
672 return Builder.CreateBitCast(V, PtrToInt8Ty);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000673}
674
Mike Stump00470a12009-03-05 08:32:30 +0000675llvm::Function *
676CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
677 const BlockInfo& Info,
Mike Stump6cc88f72009-03-20 21:53:12 +0000678 const Decl *OuterFuncDecl,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000679 llvm::DenseMap<const Decl*, llvm::Value*> ldm,
Mike Stump00470a12009-03-05 08:32:30 +0000680 uint64_t &Size,
681 uint64_t &Align,
682 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls,
683 bool &subBlockHasCopyDispose) {
Devang Patel963dfbd2009-04-15 21:51:44 +0000684
685 // Check if we should generate debug info for this block.
686 if (CGM.getDebugInfo())
687 DebugInfo = CGM.getDebugInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000688
Mike Stump7f28a9c2009-03-13 23:34:28 +0000689 // Arrange for local static and local extern declarations to appear
690 // to be local to this function as well, as they are directly referenced
691 // in a block.
692 for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
693 i != ldm.end();
694 ++i) {
695 const VarDecl *VD = dyn_cast<VarDecl>(i->first);
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000697 if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage())
Mike Stump7f28a9c2009-03-13 23:34:28 +0000698 LocalDeclMap[VD] = i->second;
699 }
700
Mike Stumpd3dd0ae2009-10-22 01:31:24 +0000701 BlockOffset = CGM.getTargetData()
702 .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8;
Eli Friedman48f91222009-03-28 03:24:54 +0000703 BlockAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
704
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000705 const FunctionType *BlockFunctionType = BExpr->getFunctionType();
706 QualType ResultType;
707 bool IsVariadic;
Mike Stump1eb44332009-09-09 15:08:12 +0000708 if (const FunctionProtoType *FTy =
Fariborz Jahanianda0895d2009-04-11 18:54:57 +0000709 dyn_cast<FunctionProtoType>(BlockFunctionType)) {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000710 ResultType = FTy->getResultType();
711 IsVariadic = FTy->isVariadic();
Mike Stumpb3589f42009-07-30 22:28:39 +0000712 } else {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000713 // K&R style block.
714 ResultType = BlockFunctionType->getResultType();
715 IsVariadic = false;
716 }
Mike Stumpa5448542009-02-13 15:32:32 +0000717
Anders Carlssond5cab542009-02-12 17:55:02 +0000718 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000719
Mike Stumpea26cb52009-10-21 03:49:08 +0000720 CurFuncDecl = OuterFuncDecl;
721
Chris Lattner161d36d2009-02-28 19:01:03 +0000722 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlssond5cab542009-02-12 17:55:02 +0000723
Mike Stumpea26cb52009-10-21 03:49:08 +0000724 IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor");
Mike Stumpadaaad32009-10-20 02:12:22 +0000725
Mike Stumpbfbd5df2009-10-21 18:24:18 +0000726 // Allocate all BlockDeclRefDecls, so we can calculate the right ParmTy below.
Mike Stump0298d382009-10-21 22:02:08 +0000727 AllocateAllBlockDeclRefs(Info, this);
Mike Stumpea26cb52009-10-21 03:49:08 +0000728
Mike Stump083c25e2009-10-22 00:49:09 +0000729 QualType ParmTy = getContext().getBlockParmType(BlockHasCopyDispose,
730 BlockDeclRefDecls);
Anders Carlssond5cab542009-02-12 17:55:02 +0000731 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000732 ImplicitParamDecl *SelfDecl =
Anders Carlssond5cab542009-02-12 17:55:02 +0000733 ImplicitParamDecl::Create(getContext(), 0,
Mike Stumpadaaad32009-10-20 02:12:22 +0000734 SourceLocation(), II,
735 ParmTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000736
Anders Carlssond5cab542009-02-12 17:55:02 +0000737 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump4e7a1f72009-02-21 20:00:35 +0000738 BlockStructDecl = SelfDecl;
Mike Stumpa5448542009-02-13 15:32:32 +0000739
Steve Naroffe78b8092009-03-13 16:56:44 +0000740 for (BlockDecl::param_const_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000741 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000742 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000743
744 const CGFunctionInfo &FI =
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000745 CGM.getTypes().getFunctionInfo(ResultType, Args);
Anders Carlssond5cab542009-02-12 17:55:02 +0000746
Mike Stump67a64482009-02-14 22:16:35 +0000747 std::string Name = std::string("__") + Info.Name + "_block_invoke_";
Anders Carlssond5cab542009-02-12 17:55:02 +0000748 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000749 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic);
Mike Stumpa5448542009-02-13 15:32:32 +0000750
751 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000752 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
753 Name,
754 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000755
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000756 CGM.SetInternalFunctionAttributes(BD, Fn, FI);
757
Chris Lattner4863db42009-04-23 07:18:56 +0000758 StartFunction(BD, ResultType, Fn, Args,
Chris Lattner161d36d2009-02-28 19:01:03 +0000759 BExpr->getBody()->getLocEnd());
Mike Stumpb1a6e682009-09-30 02:43:10 +0000760
Chris Lattner4863db42009-04-23 07:18:56 +0000761 CurFuncDecl = OuterFuncDecl;
Chris Lattnerb5437d22009-04-23 05:30:27 +0000762 CurCodeDecl = BD;
Mike Stumpb289b3f2009-10-01 22:29:41 +0000763
764 // Save a spot to insert the debug information for all the BlockDeclRefDecls.
765 llvm::BasicBlock *entry = Builder.GetInsertBlock();
766 llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint();
767 --entry_ptr;
768
Chris Lattner161d36d2009-02-28 19:01:03 +0000769 EmitStmt(BExpr->getBody());
Mike Stumpb289b3f2009-10-01 22:29:41 +0000770
Mike Stumpde8c5c72009-10-01 00:27:30 +0000771 // Remember where we were...
772 llvm::BasicBlock *resume = Builder.GetInsertBlock();
Mike Stumpb289b3f2009-10-01 22:29:41 +0000773
Mike Stumpde8c5c72009-10-01 00:27:30 +0000774 // Go back to the entry.
Mike Stumpb289b3f2009-10-01 22:29:41 +0000775 ++entry_ptr;
776 Builder.SetInsertPoint(entry, entry_ptr);
777
Mike Stumpb1a6e682009-09-30 02:43:10 +0000778 if (CGDebugInfo *DI = getDebugInfo()) {
Mike Stumpb1a6e682009-09-30 02:43:10 +0000779 // Emit debug information for all the BlockDeclRefDecls.
Mike Stumpb1a6e682009-09-30 02:43:10 +0000780 for (unsigned i=0; i < BlockDeclRefDecls.size(); ++i) {
781 const Expr *E = BlockDeclRefDecls[i];
782 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
783 if (BDRE) {
784 const ValueDecl *D = BDRE->getDecl();
785 DI->setLocation(D->getLocation());
786 DI->EmitDeclareOfBlockDeclRefVariable(BDRE,
787 LocalDeclMap[getBlockStructDecl()],
788 Builder, this);
789 }
790 }
Mike Stumpb1a6e682009-09-30 02:43:10 +0000791 }
Mike Stumpde8c5c72009-10-01 00:27:30 +0000792 // And resume where we left off.
793 if (resume == 0)
794 Builder.ClearInsertionPoint();
795 else
796 Builder.SetInsertPoint(resume);
Mike Stumpb1a6e682009-09-30 02:43:10 +0000797
Chris Lattner161d36d2009-02-28 19:01:03 +0000798 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlssond5cab542009-02-12 17:55:02 +0000799
Mike Stump8a2b4b12009-02-25 23:33:13 +0000800 // The runtime needs a minimum alignment of a void *.
801 uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
802 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign);
803
Mike Stump4e7a1f72009-02-21 20:00:35 +0000804 Size = BlockOffset;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000805 Align = BlockAlign;
806 subBlockDeclRefDecls = BlockDeclRefDecls;
Mike Stump00470a12009-03-05 08:32:30 +0000807 subBlockHasCopyDispose |= BlockHasCopyDispose;
Anders Carlssond5cab542009-02-12 17:55:02 +0000808 return Fn;
809}
Mike Stumpa99038c2009-02-28 09:07:16 +0000810
Mike Stump08920992009-03-07 02:35:30 +0000811uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
Mike Stumpa99038c2009-02-28 09:07:16 +0000812 const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
813
814 uint64_t Size = getContext().getTypeSize(D->getType()) / 8;
815 uint64_t Align = getContext().getDeclAlignInBytes(D);
816
817 if (BDRE->isByRef()) {
818 Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8;
819 Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
820 }
821
822 assert ((Align > 0) && "alignment must be 1 byte or more");
823
824 uint64_t OldOffset = BlockOffset;
825
826 // Ensure proper alignment, even if it means we have to have a gap
827 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align);
828 BlockAlign = std::max(Align, BlockAlign);
Mike Stump00470a12009-03-05 08:32:30 +0000829
Mike Stumpa99038c2009-02-28 09:07:16 +0000830 uint64_t Pad = BlockOffset - OldOffset;
831 if (Pad) {
Owen Anderson0032b272009-08-13 21:57:51 +0000832 llvm::ArrayType::get(llvm::Type::getInt8Ty(VMContext), Pad);
Mike Stumpa99038c2009-02-28 09:07:16 +0000833 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
834 llvm::APInt(32, Pad),
835 ArrayType::Normal, 0);
836 ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000837 0, QualType(PadTy), 0, VarDecl::None);
Mike Stumpa99038c2009-02-28 09:07:16 +0000838 Expr *E;
839 E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
Douglas Gregor0da76df2009-11-23 11:41:28 +0000840 SourceLocation());
Mike Stumpa99038c2009-02-28 09:07:16 +0000841 BlockDeclRefDecls.push_back(E);
842 }
843 BlockDeclRefDecls.push_back(BDRE);
844
845 BlockOffset += Size;
846 return BlockOffset-Size;
847}
Mike Stumpdab514f2009-03-04 03:23:46 +0000848
Mike Stump08920992009-03-07 02:35:30 +0000849llvm::Constant *BlockFunction::
850GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000851 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000852 QualType R = getContext().VoidTy;
853
854 FunctionArgList Args;
855 // FIXME: This leaks
Mike Stump08920992009-03-07 02:35:30 +0000856 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +0000857 ImplicitParamDecl::Create(getContext(), 0,
858 SourceLocation(), 0,
Mike Stump08920992009-03-07 02:35:30 +0000859 getContext().getPointerType(getContext().VoidTy));
860 Args.push_back(std::make_pair(Dst, Dst->getType()));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000861 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +0000862 ImplicitParamDecl::Create(getContext(), 0,
863 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000864 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000865 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Mike Stumpa4f668f2009-03-06 01:33:24 +0000867 const CGFunctionInfo &FI =
868 CGM.getTypes().getFunctionInfo(R, Args);
869
Mike Stump3899a7f2009-06-05 23:26:36 +0000870 // FIXME: We'd like to put these into a mergable by content, with
871 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000872 std::string Name = std::string("__copy_helper_block_");
873 CodeGenTypes &Types = CGM.getTypes();
874 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
875
876 llvm::Function *Fn =
877 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
878 Name,
879 &CGM.getModule());
880
881 IdentifierInfo *II
882 = &CGM.getContext().Idents.get("__copy_helper_block_");
883
884 FunctionDecl *FD = FunctionDecl::Create(getContext(),
885 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000886 SourceLocation(), II, R, 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000887 FunctionDecl::Static, false,
888 true);
889 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump08920992009-03-07 02:35:30 +0000890
891 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
892 llvm::Type *PtrPtrT;
Mike Stump08920992009-03-07 02:35:30 +0000893
Mike Stumpb7477cf2009-04-10 18:52:28 +0000894 if (NoteForHelperp) {
895 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump08920992009-03-07 02:35:30 +0000896
Owen Anderson96e0fc72009-07-29 22:16:19 +0000897 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000898 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
899 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump08920992009-03-07 02:35:30 +0000900
Mike Stumpb7477cf2009-04-10 18:52:28 +0000901 llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000902 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000903 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000904 DstObj = Builder.CreateBitCast(DstObj, PtrPtrT);
905 DstObj = Builder.CreateLoad(DstObj);
Mike Stump08920992009-03-07 02:35:30 +0000906
Mike Stumpb7477cf2009-04-10 18:52:28 +0000907 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
908 int flag = NoteForHelper[i].flag;
909 int index = NoteForHelper[i].index;
Mike Stump08920992009-03-07 02:35:30 +0000910
Mike Stumpb7477cf2009-04-10 18:52:28 +0000911 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
912 || NoteForHelper[i].RequiresCopying) {
913 llvm::Value *Srcv = SrcObj;
914 Srcv = Builder.CreateStructGEP(Srcv, index);
915 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000916 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000917 Srcv = Builder.CreateLoad(Srcv);
918
919 llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
920 Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
921
Owen Anderson0032b272009-08-13 21:57:51 +0000922 llvm::Value *N = llvm::ConstantInt::get(
923 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000924 llvm::Value *F = getBlockObjectAssign();
925 Builder.CreateCall3(F, Dstv, Srcv, N);
926 }
Mike Stump08920992009-03-07 02:35:30 +0000927 }
928 }
929
Mike Stumpa4f668f2009-03-06 01:33:24 +0000930 CGF.FinishFunction();
931
Owen Anderson3c4972d2009-07-29 18:54:39 +0000932 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000933}
934
Mike Stumpcf62d392009-03-06 18:42:23 +0000935llvm::Constant *BlockFunction::
Mike Stump08920992009-03-07 02:35:30 +0000936GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
937 const llvm::StructType* T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000938 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000939 QualType R = getContext().VoidTy;
940
941 FunctionArgList Args;
942 // FIXME: This leaks
943 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +0000944 ImplicitParamDecl::Create(getContext(), 0,
945 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000946 getContext().getPointerType(getContext().VoidTy));
947
948 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000949
Mike Stumpa4f668f2009-03-06 01:33:24 +0000950 const CGFunctionInfo &FI =
951 CGM.getTypes().getFunctionInfo(R, Args);
952
Mike Stump3899a7f2009-06-05 23:26:36 +0000953 // FIXME: We'd like to put these into a mergable by content, with
954 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000955 std::string Name = std::string("__destroy_helper_block_");
956 CodeGenTypes &Types = CGM.getTypes();
957 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
958
959 llvm::Function *Fn =
960 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
961 Name,
962 &CGM.getModule());
963
964 IdentifierInfo *II
965 = &CGM.getContext().Idents.get("__destroy_helper_block_");
966
967 FunctionDecl *FD = FunctionDecl::Create(getContext(),
968 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000969 SourceLocation(), II, R, 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000970 FunctionDecl::Static, false,
971 true);
972 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1edf6b62009-03-07 02:53:18 +0000973
Mike Stumpb7477cf2009-04-10 18:52:28 +0000974 if (NoteForHelperp) {
975 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump1edf6b62009-03-07 02:53:18 +0000976
Mike Stumpb7477cf2009-04-10 18:52:28 +0000977 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
978 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000979 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000980 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
981 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump1edf6b62009-03-07 02:53:18 +0000982
Mike Stumpb7477cf2009-04-10 18:52:28 +0000983 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
984 int flag = NoteForHelper[i].flag;
985 int index = NoteForHelper[i].index;
Mike Stump1edf6b62009-03-07 02:53:18 +0000986
Mike Stumpb7477cf2009-04-10 18:52:28 +0000987 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
988 || NoteForHelper[i].RequiresCopying) {
989 llvm::Value *Srcv = SrcObj;
990 Srcv = Builder.CreateStructGEP(Srcv, index);
991 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000992 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000993 Srcv = Builder.CreateLoad(Srcv);
994
995 BuildBlockRelease(Srcv, flag);
996 }
Mike Stump1edf6b62009-03-07 02:53:18 +0000997 }
998 }
999
Mike Stumpa4f668f2009-03-06 01:33:24 +00001000 CGF.FinishFunction();
1001
Owen Anderson3c4972d2009-07-29 18:54:39 +00001002 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpa4f668f2009-03-06 01:33:24 +00001003}
1004
Mike Stump08920992009-03-07 02:35:30 +00001005llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001006 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump08920992009-03-07 02:35:30 +00001007 return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
1008 T, NoteForHelper);
Mike Stumpa4f668f2009-03-06 01:33:24 +00001009}
1010
Mike Stump08920992009-03-07 02:35:30 +00001011llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001012 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stump08920992009-03-07 02:35:30 +00001013 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
Mike Stumpb7477cf2009-04-10 18:52:28 +00001014 T, NoteForHelperp);
Mike Stumpdab514f2009-03-04 03:23:46 +00001015}
Mike Stump797b6322009-03-05 01:23:13 +00001016
Mike Stumpee094222009-03-06 06:12:24 +00001017llvm::Constant *BlockFunction::
1018GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
Mike Stump45031c02009-03-06 02:29:21 +00001019 QualType R = getContext().VoidTy;
1020
1021 FunctionArgList Args;
1022 // FIXME: This leaks
Mike Stumpee094222009-03-06 06:12:24 +00001023 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +00001024 ImplicitParamDecl::Create(getContext(), 0,
1025 SourceLocation(), 0,
Mike Stumpee094222009-03-06 06:12:24 +00001026 getContext().getPointerType(getContext().VoidTy));
1027 Args.push_back(std::make_pair(Dst, Dst->getType()));
1028
1029 // FIXME: This leaks
Mike Stump45031c02009-03-06 02:29:21 +00001030 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001031 ImplicitParamDecl::Create(getContext(), 0,
1032 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001033 getContext().getPointerType(getContext().VoidTy));
Mike Stump45031c02009-03-06 02:29:21 +00001034 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001035
Mike Stump45031c02009-03-06 02:29:21 +00001036 const CGFunctionInfo &FI =
1037 CGM.getTypes().getFunctionInfo(R, Args);
1038
1039 std::string Name = std::string("__Block_byref_id_object_copy_");
1040 CodeGenTypes &Types = CGM.getTypes();
1041 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1042
Mike Stump3899a7f2009-06-05 23:26:36 +00001043 // FIXME: We'd like to put these into a mergable by content, with
1044 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001045 llvm::Function *Fn =
1046 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
1047 Name,
1048 &CGM.getModule());
1049
1050 IdentifierInfo *II
1051 = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
1052
1053 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1054 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001055 SourceLocation(), II, R, 0,
Mike Stump45031c02009-03-06 02:29:21 +00001056 FunctionDecl::Static, false,
1057 true);
1058 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpee094222009-03-06 06:12:24 +00001059
1060 // dst->x
1061 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001062 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001063 V = Builder.CreateLoad(V);
Mike Stumpee094222009-03-06 06:12:24 +00001064 V = Builder.CreateStructGEP(V, 6, "x");
1065 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
1066
1067 // src->x
1068 V = CGF.GetAddrOfLocalVar(Src);
1069 V = Builder.CreateLoad(V);
1070 V = Builder.CreateBitCast(V, T);
1071 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001072 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpee094222009-03-06 06:12:24 +00001073 llvm::Value *SrcObj = Builder.CreateLoad(V);
Mike Stump1eb44332009-09-09 15:08:12 +00001074
Mike Stumpee094222009-03-06 06:12:24 +00001075 flag |= BLOCK_BYREF_CALLER;
1076
Owen Anderson0032b272009-08-13 21:57:51 +00001077 llvm::Value *N = llvm::ConstantInt::get(
1078 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpee094222009-03-06 06:12:24 +00001079 llvm::Value *F = getBlockObjectAssign();
1080 Builder.CreateCall3(F, DstObj, SrcObj, N);
1081
Mike Stump45031c02009-03-06 02:29:21 +00001082 CGF.FinishFunction();
1083
Owen Anderson3c4972d2009-07-29 18:54:39 +00001084 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001085}
1086
Mike Stump1851b682009-03-06 04:53:30 +00001087llvm::Constant *
1088BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
1089 int flag) {
Mike Stump45031c02009-03-06 02:29:21 +00001090 QualType R = getContext().VoidTy;
1091
1092 FunctionArgList Args;
1093 // FIXME: This leaks
1094 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001095 ImplicitParamDecl::Create(getContext(), 0,
1096 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001097 getContext().getPointerType(getContext().VoidTy));
1098
1099 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Mike Stump45031c02009-03-06 02:29:21 +00001101 const CGFunctionInfo &FI =
1102 CGM.getTypes().getFunctionInfo(R, Args);
1103
1104 std::string Name = std::string("__Block_byref_id_object_dispose_");
1105 CodeGenTypes &Types = CGM.getTypes();
1106 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1107
Mike Stump3899a7f2009-06-05 23:26:36 +00001108 // FIXME: We'd like to put these into a mergable by content, with
1109 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001110 llvm::Function *Fn =
1111 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
1112 Name,
1113 &CGM.getModule());
1114
1115 IdentifierInfo *II
1116 = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
1117
1118 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1119 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001120 SourceLocation(), II, R, 0,
Mike Stump45031c02009-03-06 02:29:21 +00001121 FunctionDecl::Static, false,
1122 true);
1123 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1851b682009-03-06 04:53:30 +00001124
1125 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001126 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001127 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001128 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001129 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001130 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001131
Mike Stump1851b682009-03-06 04:53:30 +00001132 flag |= BLOCK_BYREF_CALLER;
1133 BuildBlockRelease(V, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001134 CGF.FinishFunction();
1135
Owen Anderson3c4972d2009-07-29 18:54:39 +00001136 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001137}
1138
Mike Stumpee094222009-03-06 06:12:24 +00001139llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001140 int Flag, unsigned Align) {
1141 // All alignments below that of pointer alignment collapse down to just
Mike Stump3899a7f2009-06-05 23:26:36 +00001142 // pointer alignment, as we always have at least that much alignment to begin
1143 // with.
1144 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001145
Mike Stump3899a7f2009-06-05 23:26:36 +00001146 // As an optimization, we only generate a single function of each kind we
1147 // might need. We need a different one for each alignment and for each
1148 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001149 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1150 llvm::Constant *&Entry = CGM.AssignCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001151 if (Entry)
1152 return Entry;
Chris Lattner10976d92009-12-05 08:21:30 +00001153 return Entry = CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, Flag);
Mike Stump45031c02009-03-06 02:29:21 +00001154}
1155
Mike Stump1851b682009-03-06 04:53:30 +00001156llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
Chris Lattner10976d92009-12-05 08:21:30 +00001157 int Flag,
Mike Stump3899a7f2009-06-05 23:26:36 +00001158 unsigned Align) {
1159 // All alignments below that of pointer alignment collpase down to just
1160 // pointer alignment, as we always have at least that much alignment to begin
1161 // with.
1162 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
Chris Lattner10976d92009-12-05 08:21:30 +00001163
Mike Stump3899a7f2009-06-05 23:26:36 +00001164 // As an optimization, we only generate a single function of each kind we
1165 // might need. We need a different one for each alignment and for each
1166 // setting of flags. We mix Align and flag to get the kind.
Chris Lattner10976d92009-12-05 08:21:30 +00001167 uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1168 llvm::Constant *&Entry = CGM.DestroyCache[Kind];
Mike Stump3899a7f2009-06-05 23:26:36 +00001169 if (Entry)
1170 return Entry;
Chris Lattner10976d92009-12-05 08:21:30 +00001171 return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, Flag);
Mike Stump45031c02009-03-06 02:29:21 +00001172}
1173
Mike Stump797b6322009-03-05 01:23:13 +00001174llvm::Value *BlockFunction::getBlockObjectDispose() {
1175 if (CGM.BlockObjectDispose == 0) {
1176 const llvm::FunctionType *FTy;
1177 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001178 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stump797b6322009-03-05 01:23:13 +00001179 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001180 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson96e0fc72009-07-29 22:16:19 +00001181 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stump00470a12009-03-05 08:32:30 +00001182 CGM.BlockObjectDispose
Mike Stump797b6322009-03-05 01:23:13 +00001183 = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
1184 }
1185 return CGM.BlockObjectDispose;
1186}
1187
Mike Stumpee094222009-03-06 06:12:24 +00001188llvm::Value *BlockFunction::getBlockObjectAssign() {
1189 if (CGM.BlockObjectAssign == 0) {
1190 const llvm::FunctionType *FTy;
1191 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001192 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stumpee094222009-03-06 06:12:24 +00001193 ArgTys.push_back(PtrToInt8Ty);
1194 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001195 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson96e0fc72009-07-29 22:16:19 +00001196 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stumpee094222009-03-06 06:12:24 +00001197 CGM.BlockObjectAssign
1198 = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
1199 }
1200 return CGM.BlockObjectAssign;
1201}
1202
Mike Stump1851b682009-03-06 04:53:30 +00001203void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Mike Stump797b6322009-03-05 01:23:13 +00001204 llvm::Value *F = getBlockObjectDispose();
Mike Stump1851b682009-03-06 04:53:30 +00001205 llvm::Value *N;
Mike Stump797b6322009-03-05 01:23:13 +00001206 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001207 N = llvm::ConstantInt::get(llvm::Type::getInt32Ty(V->getContext()), flag);
Mike Stump797b6322009-03-05 01:23:13 +00001208 Builder.CreateCall2(F, V, N);
1209}
Mike Stump00470a12009-03-05 08:32:30 +00001210
1211ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stump08920992009-03-07 02:35:30 +00001212
1213BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1214 CGBuilderTy &B)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001215 : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) {
Owen Anderson0032b272009-08-13 21:57:51 +00001216 PtrToInt8Ty = llvm::PointerType::getUnqual(
1217 llvm::Type::getInt8Ty(VMContext));
Mike Stump08920992009-03-07 02:35:30 +00001218
1219 BlockHasCopyDispose = false;
1220}