blob: dc4db4ac2c7c896010fdb38364987d05f1338d56 [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
149 std::vector<llvm::Constant*> Elts(5);
Mike Stumpe5fee252009-02-13 16:19:19 +0000150 llvm::Constant *C;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000151 llvm::Value *V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000152
Mike Stumpe5fee252009-02-13 16:19:19 +0000153 {
154 // C = BuildBlockStructInitlist();
155 unsigned int flags = BLOCK_HAS_DESCRIPTOR;
156
Mike Stump00470a12009-03-05 08:32:30 +0000157 // We run this first so that we set BlockHasCopyDispose from the entire
158 // block literal.
159 // __invoke
160 uint64_t subBlockSize, subBlockAlign;
161 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Mike Stumpa803b0e2009-03-25 17:58:24 +0000162 bool subBlockHasCopyDispose = false;
Mike Stump00470a12009-03-05 08:32:30 +0000163 llvm::Function *Fn
Mike Stumpbb304192009-09-24 22:31:14 +0000164 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl,
165 LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000166 subBlockSize,
Mike Stump00470a12009-03-05 08:32:30 +0000167 subBlockAlign,
168 subBlockDeclRefDecls,
Mike Stumpa803b0e2009-03-25 17:58:24 +0000169 subBlockHasCopyDispose);
170 BlockHasCopyDispose |= subBlockHasCopyDispose;
Mike Stump00470a12009-03-05 08:32:30 +0000171 Elts[3] = Fn;
172
Mike Stumpf5408fe2009-05-16 07:57:57 +0000173 // FIXME: Don't use BlockHasCopyDispose, it is set more often then
174 // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); }
Mike Stumpa803b0e2009-03-25 17:58:24 +0000175 if (subBlockHasCopyDispose)
Mike Stumpe5fee252009-02-13 16:19:19 +0000176 flags |= BLOCK_HAS_COPY_DISPOSE;
177
Mike Stump7d6dc4f2009-02-13 20:17:16 +0000178 // __isa
Mike Stump59c5b112009-02-13 19:29:27 +0000179 C = CGM.getNSConcreteStackBlock();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000180 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
Mike Stump00470a12009-03-05 08:32:30 +0000181 Elts[0] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000182
183 // __flags
184 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
185 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000186 C = llvm::ConstantInt::get(IntTy, flags);
Mike Stump00470a12009-03-05 08:32:30 +0000187 Elts[1] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000188
189 // __reserved
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000190 C = llvm::ConstantInt::get(IntTy, 0);
Mike Stump00470a12009-03-05 08:32:30 +0000191 Elts[2] = C;
Mike Stumpe5fee252009-02-13 16:19:19 +0000192
Mike Stump8a2b4b12009-02-25 23:33:13 +0000193 if (subBlockDeclRefDecls.size() == 0) {
Mike Stumpcf62d392009-03-06 18:42:23 +0000194 // __descriptor
Mike Stumpea26cb52009-10-21 03:49:08 +0000195 Elts[4] = BuildDescriptorBlockDecl(subBlockHasCopyDispose, subBlockSize,
196 0, 0);
Mike Stumpcf62d392009-03-06 18:42:23 +0000197
Mike Stump5570cfe2009-03-01 20:07:53 +0000198 // Optimize to being a global block.
199 Elts[0] = CGM.getNSConcreteGlobalBlock();
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000200 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
Mike Stump5570cfe2009-03-01 20:07:53 +0000201
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000202 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000203
204 char Name[32];
205 sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
Owen Anderson1c431b32009-07-08 19:05:04 +0000206 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Mike Stump8a2b4b12009-02-25 23:33:13 +0000207 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000208 C, Name);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000209 QualType BPT = BE->getType();
Owen Anderson3c4972d2009-07-29 18:54:39 +0000210 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
Mike Stump8a2b4b12009-02-25 23:33:13 +0000211 return C;
212 }
Mike Stump00470a12009-03-05 08:32:30 +0000213
Mike Stump8a2b4b12009-02-25 23:33:13 +0000214 std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size());
Mike Stump08920992009-03-07 02:35:30 +0000215 for (int i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000216 Types[i] = Elts[i]->getType();
Mike Stump08920992009-03-07 02:35:30 +0000217 Types[4] = PtrToInt8Ty;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000218
Mike Stumpa99038c2009-02-28 09:07:16 +0000219 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
220 const Expr *E = subBlockDeclRefDecls[i];
221 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
222 QualType Ty = E->getType();
Mike Stumpdab514f2009-03-04 03:23:46 +0000223 if (BDRE && BDRE->isByRef()) {
Anders Carlsson9ad55132009-09-09 02:51:03 +0000224 Types[i+5] = llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000225 } else
226 Types[i+5] = ConvertType(Ty);
Mike Stumpa99038c2009-02-28 09:07:16 +0000227 }
Mike Stump8a2b4b12009-02-25 23:33:13 +0000228
Owen Anderson47a434f2009-08-05 23:18:46 +0000229 llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000230
231 llvm::AllocaInst *A = CreateTempAlloca(Ty);
232 A->setAlignment(subBlockAlign);
233 V = A;
234
Mike Stump08920992009-03-07 02:35:30 +0000235 std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size());
236 int helpersize = 0;
237
238 for (unsigned i=0; i<4; ++i)
Mike Stump8a2b4b12009-02-25 23:33:13 +0000239 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
Mike Stump00470a12009-03-05 08:32:30 +0000240
Mike Stump8a2b4b12009-02-25 23:33:13 +0000241 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
242 {
Mike Stumpa99038c2009-02-28 09:07:16 +0000243 // FIXME: Push const down.
244 Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
245 DeclRefExpr *DR;
246 ValueDecl *VD;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000247
Mike Stumpa99038c2009-02-28 09:07:16 +0000248 DR = dyn_cast<DeclRefExpr>(E);
249 // Skip padding.
250 if (DR) continue;
251
252 BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
253 VD = BDRE->getDecl();
254
Mike Stumpdab514f2009-03-04 03:23:46 +0000255 llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp");
Mike Stump08920992009-03-07 02:35:30 +0000256 NoteForHelper[helpersize].index = i+5;
Mike Stump39605b42009-09-22 02:12:52 +0000257 NoteForHelper[helpersize].RequiresCopying
258 = BlockRequiresCopying(VD->getType());
Mike Stump08920992009-03-07 02:35:30 +0000259 NoteForHelper[helpersize].flag
Mike Stump39605b42009-09-22 02:12:52 +0000260 = (VD->getType()->isBlockPointerType()
261 ? BLOCK_FIELD_IS_BLOCK
262 : BLOCK_FIELD_IS_OBJECT);
Mike Stump08920992009-03-07 02:35:30 +0000263
Mike Stumpa99038c2009-02-28 09:07:16 +0000264 if (LocalDeclMap[VD]) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000265 if (BDRE->isByRef()) {
Mike Stump08920992009-03-07 02:35:30 +0000266 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
Mike Stumpf4bc3122009-03-07 06:04:31 +0000267 // FIXME: Someone double check this.
268 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000269 llvm::Value *Loc = LocalDeclMap[VD];
270 Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
271 Loc = Builder.CreateLoad(Loc, false);
Mike Stumpdab514f2009-03-04 03:23:46 +0000272 Builder.CreateStore(Loc, Addr);
Mike Stump08920992009-03-07 02:35:30 +0000273 ++helpersize;
Mike Stumpdab514f2009-03-04 03:23:46 +0000274 continue;
275 } else
276 E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD),
277 VD->getType(), SourceLocation(),
278 false, false);
Mike Stumpa99038c2009-02-28 09:07:16 +0000279 }
Mike Stumpdab514f2009-03-04 03:23:46 +0000280 if (BDRE->isByRef()) {
Mike Stumpa8b60c92009-03-21 21:00:35 +0000281 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
282 // FIXME: Someone double check this.
283 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stumpa99038c2009-02-28 09:07:16 +0000284 E = new (getContext())
285 UnaryOperator(E, UnaryOperator::AddrOf,
286 getContext().getPointerType(E->getType()),
287 SourceLocation());
Mike Stumpdab514f2009-03-04 03:23:46 +0000288 }
Mike Stump08920992009-03-07 02:35:30 +0000289 ++helpersize;
Mike Stumpa99038c2009-02-28 09:07:16 +0000290
Mike Stumpa99038c2009-02-28 09:07:16 +0000291 RValue r = EmitAnyExpr(E, Addr, false);
Mike Stumpdab514f2009-03-04 03:23:46 +0000292 if (r.isScalar()) {
293 llvm::Value *Loc = r.getScalarVal();
294 const llvm::Type *Ty = Types[i+5];
295 if (BDRE->isByRef()) {
Mike Stump58a85142009-03-04 22:48:06 +0000296 // E is now the address of the value field, instead, we want the
297 // address of the actual ByRef struct. We optimize this slightly
298 // compared to gcc by not grabbing the forwarding slot as this must
299 // be done during Block_copy for us, and we can postpone the work
300 // until then.
301 uint64_t offset = BlockDecls[BDRE->getDecl()];
Mike Stump00470a12009-03-05 08:32:30 +0000302
Mike Stump58a85142009-03-04 22:48:06 +0000303 llvm::Value *BlockLiteral = LoadBlockStruct();
Mike Stump00470a12009-03-05 08:32:30 +0000304
Mike Stump58a85142009-03-04 22:48:06 +0000305 Loc = Builder.CreateGEP(BlockLiteral,
Mike Stumpea26cb52009-10-21 03:49:08 +0000306 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Mike Stump58a85142009-03-04 22:48:06 +0000307 offset),
308 "block.literal");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000309 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000310 Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stump58a85142009-03-04 22:48:06 +0000311 Loc = Builder.CreateLoad(Loc, false);
312 // Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000313 }
314 Builder.CreateStore(Loc, Addr);
315 } else if (r.isComplex())
Mike Stump8a2b4b12009-02-25 23:33:13 +0000316 // FIXME: implement
317 ErrorUnsupported(BE, "complex in block literal");
318 else if (r.isAggregate())
319 ; // Already created into the destination
320 else
321 assert (0 && "bad block variable");
322 // FIXME: Ensure that the offset created by the backend for
323 // the struct matches the previously computed offset in BlockDecls.
324 }
Mike Stump08920992009-03-07 02:35:30 +0000325 NoteForHelper.resize(helpersize);
Mike Stumpcf62d392009-03-06 18:42:23 +0000326
327 // __descriptor
Mike Stumpa803b0e2009-03-25 17:58:24 +0000328 llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockHasCopyDispose,
329 subBlockSize, Ty,
Mike Stump08920992009-03-07 02:35:30 +0000330 &NoteForHelper);
331 Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
332 Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
Mike Stumpe5fee252009-02-13 16:19:19 +0000333 }
Mike Stump00470a12009-03-05 08:32:30 +0000334
Mike Stumpbd65cac2009-02-19 01:01:04 +0000335 QualType BPT = BE->getType();
Mike Stump8a2b4b12009-02-25 23:33:13 +0000336 return Builder.CreateBitCast(V, ConvertType(BPT));
Mike Stumpe5fee252009-02-13 16:19:19 +0000337}
338
339
Mike Stump2a998142009-03-04 18:17:45 +0000340const llvm::Type *BlockModule::getBlockDescriptorType() {
Mike Stumpab695142009-02-13 15:16:56 +0000341 if (BlockDescriptorType)
342 return BlockDescriptorType;
343
Mike Stumpa5448542009-02-13 15:32:32 +0000344 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000345 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000346
Mike Stumpab695142009-02-13 15:16:56 +0000347 // struct __block_descriptor {
348 // unsigned long reserved;
349 // unsigned long block_size;
350 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000351 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(),
352 UnsignedLongTy,
Mike Stumpa5448542009-02-13 15:32:32 +0000353 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000354 NULL);
355
356 getModule().addTypeName("struct.__block_descriptor",
357 BlockDescriptorType);
358
359 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000360}
361
Mike Stump2a998142009-03-04 18:17:45 +0000362const llvm::Type *BlockModule::getGenericBlockLiteralType() {
Mike Stump9b8a7972009-02-13 15:25:34 +0000363 if (GenericBlockLiteralType)
364 return GenericBlockLiteralType;
365
Mike Stumpa5448542009-02-13 15:32:32 +0000366 const llvm::Type *BlockDescPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000367 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000368
Mike Stump7cbb3602009-02-13 16:01:35 +0000369 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
370 getTypes().ConvertType(getContext().IntTy));
371
Mike Stump9b8a7972009-02-13 15:25:34 +0000372 // struct __block_literal_generic {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000373 // void *__isa;
374 // int __flags;
375 // int __reserved;
376 // void (*__invoke)(void *);
377 // struct __block_descriptor *__descriptor;
Mike Stump9b8a7972009-02-13 15:25:34 +0000378 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000379 GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
380 PtrToInt8Ty,
Mike Stump7cbb3602009-02-13 16:01:35 +0000381 IntTy,
382 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000383 PtrToInt8Ty,
Mike Stump9b8a7972009-02-13 15:25:34 +0000384 BlockDescPtrTy,
385 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000386
Mike Stump9b8a7972009-02-13 15:25:34 +0000387 getModule().addTypeName("struct.__block_literal_generic",
388 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000389
Mike Stump9b8a7972009-02-13 15:25:34 +0000390 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000391}
392
Mike Stump2a998142009-03-04 18:17:45 +0000393const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000394 if (GenericExtendedBlockLiteralType)
395 return GenericExtendedBlockLiteralType;
396
Mike Stumpbd65cac2009-02-19 01:01:04 +0000397 const llvm::Type *BlockDescPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000398 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpbd65cac2009-02-19 01:01:04 +0000399
400 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
401 getTypes().ConvertType(getContext().IntTy));
402
403 // struct __block_literal_generic {
404 // void *__isa;
405 // int __flags;
406 // int __reserved;
407 // void (*__invoke)(void *);
408 // struct __block_descriptor *__descriptor;
409 // void *__copy_func_helper_decl;
410 // void *__destroy_func_decl;
411 // };
Owen Anderson47a434f2009-08-05 23:18:46 +0000412 GenericExtendedBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
413 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000414 IntTy,
415 IntTy,
Mike Stump797b6322009-03-05 01:23:13 +0000416 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000417 BlockDescPtrTy,
Mike Stump797b6322009-03-05 01:23:13 +0000418 PtrToInt8Ty,
419 PtrToInt8Ty,
Mike Stumpbd65cac2009-02-19 01:01:04 +0000420 NULL);
421
422 getModule().addTypeName("struct.__block_literal_extended_generic",
423 GenericExtendedBlockLiteralType);
424
425 return GenericExtendedBlockLiteralType;
426}
427
Anders Carlssond5cab542009-02-12 17:55:02 +0000428RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpa5448542009-02-13 15:32:32 +0000429 const BlockPointerType *BPT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000430 E->getCallee()->getType()->getAs<BlockPointerType>();
Mike Stumpa5448542009-02-13 15:32:32 +0000431
Anders Carlssonacfde802009-02-12 00:39:25 +0000432 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
433
434 // Get a pointer to the generic block literal.
435 const llvm::Type *BlockLiteralTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000436 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000437
438 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000439 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000440 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
441
442 // Get the function pointer from the literal.
443 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Anders Carlssonacfde802009-02-12 00:39:25 +0000444
Mike Stumpa5448542009-02-13 15:32:32 +0000445 BlockLiteral =
446 Builder.CreateBitCast(BlockLiteral,
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000447 llvm::Type::getInt8PtrTy(VMContext),
Anders Carlssonacfde802009-02-12 00:39:25 +0000448 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000449
Anders Carlssonacfde802009-02-12 00:39:25 +0000450 // Add the block literal.
451 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
452 CallArgList Args;
453 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000454
Anders Carlsson782f3972009-04-08 23:13:16 +0000455 QualType FnType = BPT->getPointeeType();
456
Anders Carlssonacfde802009-02-12 00:39:25 +0000457 // And the rest of the arguments.
John McCall183700f2009-09-21 23:43:11 +0000458 EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(),
Anders Carlsson782f3972009-04-08 23:13:16 +0000459 E->arg_begin(), E->arg_end());
Mike Stumpa5448542009-02-13 15:32:32 +0000460
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000461 // Load the function.
462 llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp");
463
John McCall183700f2009-09-21 23:43:11 +0000464 QualType ResultType = FnType->getAs<FunctionType>()->getResultType();
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000465
Mike Stump1eb44332009-09-09 15:08:12 +0000466 const CGFunctionInfo &FnInfo =
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000467 CGM.getTypes().getFunctionInfo(ResultType, Args);
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000469 // Cast the function pointer to the right type.
Mike Stump1eb44332009-09-09 15:08:12 +0000470 const llvm::Type *BlockFTy =
Anders Carlssona17d7cc2009-04-08 02:55:55 +0000471 CGM.getTypes().GetFunctionType(FnInfo, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Owen Anderson96e0fc72009-07-29 22:16:19 +0000473 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
Anders Carlsson6e460ff2009-04-07 22:10:22 +0000474 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Anders Carlssonacfde802009-02-12 00:39:25 +0000476 // And call the block.
Anders Carlssonf8544a42009-04-07 00:20:24 +0000477 return EmitCall(FnInfo, Func, Args);
Anders Carlssonacfde802009-02-12 00:39:25 +0000478}
Anders Carlssond5cab542009-02-12 17:55:02 +0000479
Mike Stumpea26cb52009-10-21 03:49:08 +0000480uint64_t CodeGenFunction::AllocateBlockDecl(const BlockDeclRefExpr *E) {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000481 const ValueDecl *VD = E->getDecl();
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000482 uint64_t &offset = BlockDecls[VD];
Mike Stumpdab514f2009-03-04 03:23:46 +0000483
Mike Stumpdab514f2009-03-04 03:23:46 +0000484 // See if we have already allocated an offset for this variable.
Mike Stumpea26cb52009-10-21 03:49:08 +0000485 if (offset)
486 return offset;
487
488 // Don't run the expensive check, unless we have to.
Mike Stump083c25e2009-10-22 00:49:09 +0000489 if (!BlockHasCopyDispose)
490 if (E->isByRef()
491 || BlockRequiresCopying(E->getType()))
492 BlockHasCopyDispose = true;
Mike Stumpea26cb52009-10-21 03:49:08 +0000493
494 // if not, allocate one now.
495 offset = getBlockOffset(E);
496
497 return offset;
498}
499
500llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
501 const ValueDecl *VD = E->getDecl();
502 uint64_t offset = AllocateBlockDecl(E);
503
Mike Stumpdab514f2009-03-04 03:23:46 +0000504
505 llvm::Value *BlockLiteral = LoadBlockStruct();
506 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
Mike Stumpea26cb52009-10-21 03:49:08 +0000507 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000508 offset),
Mike Stump58a85142009-03-04 22:48:06 +0000509 "block.literal");
Mike Stumpdab514f2009-03-04 03:23:46 +0000510 if (E->isByRef()) {
Mike Stumpdab514f2009-03-04 03:23:46 +0000511 const llvm::Type *PtrStructTy
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000512 = llvm::PointerType::get(BuildByRefType(VD), 0);
Mike Stumpa8b60c92009-03-21 21:00:35 +0000513 // The block literal will need a copy/destroy helper.
514 BlockHasCopyDispose = true;
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000515
516 const llvm::Type *Ty = PtrStructTy;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000517 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000518 V = Builder.CreateBitCast(V, Ty);
519 V = Builder.CreateLoad(V, false);
520 V = Builder.CreateStructGEP(V, 1, "forwarding");
521 V = Builder.CreateLoad(V, false);
522 V = Builder.CreateBitCast(V, PtrStructTy);
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000523 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
524 VD->getNameAsString());
Mike Stumpdab514f2009-03-04 03:23:46 +0000525 } else {
Anders Carlsson7dfa4072009-09-12 02:14:24 +0000526 const llvm::Type *Ty = CGM.getTypes().ConvertType(VD->getType());
527
Owen Anderson96e0fc72009-07-29 22:16:19 +0000528 Ty = llvm::PointerType::get(Ty, 0);
Mike Stumpdab514f2009-03-04 03:23:46 +0000529 V = Builder.CreateBitCast(V, Ty);
530 }
531 return V;
532}
533
Mike Stump6cc88f72009-03-20 21:53:12 +0000534void CodeGenFunction::BlockForwardSelf() {
535 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
536 ImplicitParamDecl *SelfDecl = OMD->getSelfDecl();
537 llvm::Value *&DMEntry = LocalDeclMap[SelfDecl];
538 if (DMEntry)
539 return;
540 // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care
541 BlockDeclRefExpr *BDRE = new (getContext())
542 BlockDeclRefExpr(SelfDecl,
543 SelfDecl->getType(), SourceLocation(), false);
544 DMEntry = GetAddrOfBlockDecl(BDRE);
545}
546
Mike Stump67a64482009-02-14 22:16:35 +0000547llvm::Constant *
Mike Stump90a90432009-03-04 18:47:42 +0000548BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000549 // Generate the block descriptor.
550 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000551 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
552 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000553
Anders Carlssond5cab542009-02-12 17:55:02 +0000554 llvm::Constant *DescriptorFields[2];
Mike Stumpa5448542009-02-13 15:32:32 +0000555
Anders Carlssond5cab542009-02-12 17:55:02 +0000556 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000557 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000558
Anders Carlssond5cab542009-02-12 17:55:02 +0000559 // Block literal size. For global blocks we just use the size of the generic
560 // block literal struct.
Mike Stumpa5448542009-02-13 15:32:32 +0000561 uint64_t BlockLiteralSize =
Mike Stump9b8a7972009-02-13 15:25:34 +0000562 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Owen Andersona1cf15f2009-07-14 23:10:40 +0000563 DescriptorFields[1] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000564 llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpa5448542009-02-13 15:32:32 +0000565
566 llvm::Constant *DescriptorStruct =
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000567 llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 2, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000568
Anders Carlssond5cab542009-02-12 17:55:02 +0000569 llvm::GlobalVariable *Descriptor =
Owen Anderson1c431b32009-07-08 19:05:04 +0000570 new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000571 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000572 DescriptorStruct, "__block_descriptor_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000573
Anders Carlssond5cab542009-02-12 17:55:02 +0000574 // Generate the constants for the block literal.
575 llvm::Constant *LiteralFields[5];
Mike Stumpa5448542009-02-13 15:32:32 +0000576
Mike Stump67a64482009-02-14 22:16:35 +0000577 CodeGenFunction::BlockInfo Info(0, n);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000578 uint64_t subBlockSize, subBlockAlign;
Mike Stumpa99038c2009-02-28 09:07:16 +0000579 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Daniel Dunbard67b09a2009-03-12 03:07:24 +0000580 bool subBlockHasCopyDispose = false;
Mike Stump7f28a9c2009-03-13 23:34:28 +0000581 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000582 llvm::Function *Fn
Mike Stump6cc88f72009-03-20 21:53:12 +0000583 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000584 subBlockSize,
Mike Stump90a90432009-03-04 18:47:42 +0000585 subBlockAlign,
Mike Stump00470a12009-03-05 08:32:30 +0000586 subBlockDeclRefDecls,
587 subBlockHasCopyDispose);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000588 assert(subBlockSize == BlockLiteralSize
589 && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000590
Anders Carlssond5cab542009-02-12 17:55:02 +0000591 // isa
Mike Stumpf99f1d02009-02-13 17:23:42 +0000592 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000593
Anders Carlssond5cab542009-02-12 17:55:02 +0000594 // Flags
Mike Stump00470a12009-03-05 08:32:30 +0000595 LiteralFields[1] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000596 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR);
Mike Stumpa5448542009-02-13 15:32:32 +0000597
Anders Carlssond5cab542009-02-12 17:55:02 +0000598 // Reserved
Owen Andersonc9c88b42009-07-31 20:28:54 +0000599 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000600
Anders Carlssond5cab542009-02-12 17:55:02 +0000601 // Function
602 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000603
Anders Carlssond5cab542009-02-12 17:55:02 +0000604 // Descriptor
605 LiteralFields[4] = Descriptor;
Mike Stumpa5448542009-02-13 15:32:32 +0000606
607 llvm::Constant *BlockLiteralStruct =
Nick Lewycky0d36dd22009-09-19 20:00:52 +0000608 llvm::ConstantStruct::get(VMContext, &LiteralFields[0], 5, false);
Mike Stumpa5448542009-02-13 15:32:32 +0000609
610 llvm::GlobalVariable *BlockLiteral =
Owen Anderson1c431b32009-07-08 19:05:04 +0000611 new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000612 llvm::GlobalVariable::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +0000613 BlockLiteralStruct, "__block_literal_global");
Mike Stumpa5448542009-02-13 15:32:32 +0000614
Anders Carlssond5cab542009-02-12 17:55:02 +0000615 return BlockLiteral;
616}
617
Mike Stump4e7a1f72009-02-21 20:00:35 +0000618llvm::Value *CodeGenFunction::LoadBlockStruct() {
Mike Stumpbf1914b2009-10-20 20:30:01 +0000619 llvm::Value *V = Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()],
620 "self");
621 // For now, we codegen based upon byte offsets.
622 return Builder.CreateBitCast(V, PtrToInt8Ty);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000623}
624
Mike Stump00470a12009-03-05 08:32:30 +0000625llvm::Function *
626CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
627 const BlockInfo& Info,
Mike Stump6cc88f72009-03-20 21:53:12 +0000628 const Decl *OuterFuncDecl,
Mike Stump7f28a9c2009-03-13 23:34:28 +0000629 llvm::DenseMap<const Decl*, llvm::Value*> ldm,
Mike Stump00470a12009-03-05 08:32:30 +0000630 uint64_t &Size,
631 uint64_t &Align,
632 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls,
633 bool &subBlockHasCopyDispose) {
Devang Patel963dfbd2009-04-15 21:51:44 +0000634
635 // Check if we should generate debug info for this block.
636 if (CGM.getDebugInfo())
637 DebugInfo = CGM.getDebugInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Mike Stump7f28a9c2009-03-13 23:34:28 +0000639 // Arrange for local static and local extern declarations to appear
640 // to be local to this function as well, as they are directly referenced
641 // in a block.
642 for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
643 i != ldm.end();
644 ++i) {
645 const VarDecl *VD = dyn_cast<VarDecl>(i->first);
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000647 if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage())
Mike Stump7f28a9c2009-03-13 23:34:28 +0000648 LocalDeclMap[VD] = i->second;
649 }
650
Eli Friedman48f91222009-03-28 03:24:54 +0000651 // FIXME: We need to rearrange the code for copy/dispose so we have this
652 // sooner, so we can calculate offsets correctly.
653 if (!BlockHasCopyDispose)
654 BlockOffset = CGM.getTargetData()
655 .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8;
656 else
657 BlockOffset = CGM.getTargetData()
658 .getTypeStoreSizeInBits(CGM.getGenericExtendedBlockLiteralType()) / 8;
659 BlockAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
660
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000661 const FunctionType *BlockFunctionType = BExpr->getFunctionType();
662 QualType ResultType;
663 bool IsVariadic;
Mike Stump1eb44332009-09-09 15:08:12 +0000664 if (const FunctionProtoType *FTy =
Fariborz Jahanianda0895d2009-04-11 18:54:57 +0000665 dyn_cast<FunctionProtoType>(BlockFunctionType)) {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000666 ResultType = FTy->getResultType();
667 IsVariadic = FTy->isVariadic();
Mike Stumpb3589f42009-07-30 22:28:39 +0000668 } else {
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000669 // K&R style block.
670 ResultType = BlockFunctionType->getResultType();
671 IsVariadic = false;
672 }
Mike Stumpa5448542009-02-13 15:32:32 +0000673
Anders Carlssond5cab542009-02-12 17:55:02 +0000674 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000675
Mike Stumpea26cb52009-10-21 03:49:08 +0000676 CurFuncDecl = OuterFuncDecl;
677
Chris Lattner161d36d2009-02-28 19:01:03 +0000678 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlssond5cab542009-02-12 17:55:02 +0000679
Mike Stumpea26cb52009-10-21 03:49:08 +0000680 IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor");
Mike Stumpadaaad32009-10-20 02:12:22 +0000681
Mike Stumpbfbd5df2009-10-21 18:24:18 +0000682 // Allocate all BlockDeclRefDecls, so we can calculate the right ParmTy below.
Mike Stump0298d382009-10-21 22:02:08 +0000683 AllocateAllBlockDeclRefs(Info, this);
Mike Stumpea26cb52009-10-21 03:49:08 +0000684
Mike Stump083c25e2009-10-22 00:49:09 +0000685 QualType ParmTy = getContext().getBlockParmType(BlockHasCopyDispose,
686 BlockDeclRefDecls);
Anders Carlssond5cab542009-02-12 17:55:02 +0000687 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000688 ImplicitParamDecl *SelfDecl =
Anders Carlssond5cab542009-02-12 17:55:02 +0000689 ImplicitParamDecl::Create(getContext(), 0,
Mike Stumpadaaad32009-10-20 02:12:22 +0000690 SourceLocation(), II,
691 ParmTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000692
Anders Carlssond5cab542009-02-12 17:55:02 +0000693 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump4e7a1f72009-02-21 20:00:35 +0000694 BlockStructDecl = SelfDecl;
Mike Stumpa5448542009-02-13 15:32:32 +0000695
Steve Naroffe78b8092009-03-13 16:56:44 +0000696 for (BlockDecl::param_const_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000697 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000698 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000699
700 const CGFunctionInfo &FI =
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000701 CGM.getTypes().getFunctionInfo(ResultType, Args);
Anders Carlssond5cab542009-02-12 17:55:02 +0000702
Mike Stump67a64482009-02-14 22:16:35 +0000703 std::string Name = std::string("__") + Info.Name + "_block_invoke_";
Anders Carlssond5cab542009-02-12 17:55:02 +0000704 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian140fb262009-04-11 17:55:15 +0000705 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic);
Mike Stumpa5448542009-02-13 15:32:32 +0000706
707 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000708 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
709 Name,
710 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000711
Daniel Dunbar0e4f40e2009-04-17 00:48:04 +0000712 CGM.SetInternalFunctionAttributes(BD, Fn, FI);
713
Chris Lattner4863db42009-04-23 07:18:56 +0000714 StartFunction(BD, ResultType, Fn, Args,
Chris Lattner161d36d2009-02-28 19:01:03 +0000715 BExpr->getBody()->getLocEnd());
Mike Stumpb1a6e682009-09-30 02:43:10 +0000716
Chris Lattner4863db42009-04-23 07:18:56 +0000717 CurFuncDecl = OuterFuncDecl;
Chris Lattnerb5437d22009-04-23 05:30:27 +0000718 CurCodeDecl = BD;
Mike Stumpb289b3f2009-10-01 22:29:41 +0000719
720 // Save a spot to insert the debug information for all the BlockDeclRefDecls.
721 llvm::BasicBlock *entry = Builder.GetInsertBlock();
722 llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint();
723 --entry_ptr;
724
Chris Lattner161d36d2009-02-28 19:01:03 +0000725 EmitStmt(BExpr->getBody());
Mike Stumpb289b3f2009-10-01 22:29:41 +0000726
Mike Stumpde8c5c72009-10-01 00:27:30 +0000727 // Remember where we were...
728 llvm::BasicBlock *resume = Builder.GetInsertBlock();
Mike Stumpb289b3f2009-10-01 22:29:41 +0000729
Mike Stumpde8c5c72009-10-01 00:27:30 +0000730 // Go back to the entry.
Mike Stumpb289b3f2009-10-01 22:29:41 +0000731 ++entry_ptr;
732 Builder.SetInsertPoint(entry, entry_ptr);
733
Mike Stumpb1a6e682009-09-30 02:43:10 +0000734 if (CGDebugInfo *DI = getDebugInfo()) {
Mike Stumpb1a6e682009-09-30 02:43:10 +0000735 // Emit debug information for all the BlockDeclRefDecls.
Mike Stumpb1a6e682009-09-30 02:43:10 +0000736 for (unsigned i=0; i < BlockDeclRefDecls.size(); ++i) {
737 const Expr *E = BlockDeclRefDecls[i];
738 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
739 if (BDRE) {
740 const ValueDecl *D = BDRE->getDecl();
741 DI->setLocation(D->getLocation());
742 DI->EmitDeclareOfBlockDeclRefVariable(BDRE,
743 LocalDeclMap[getBlockStructDecl()],
744 Builder, this);
745 }
746 }
Mike Stumpb1a6e682009-09-30 02:43:10 +0000747 }
Mike Stumpde8c5c72009-10-01 00:27:30 +0000748 // And resume where we left off.
749 if (resume == 0)
750 Builder.ClearInsertionPoint();
751 else
752 Builder.SetInsertPoint(resume);
Mike Stumpb1a6e682009-09-30 02:43:10 +0000753
Chris Lattner161d36d2009-02-28 19:01:03 +0000754 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlssond5cab542009-02-12 17:55:02 +0000755
Mike Stump8a2b4b12009-02-25 23:33:13 +0000756 // The runtime needs a minimum alignment of a void *.
757 uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
758 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign);
759
Mike Stump4e7a1f72009-02-21 20:00:35 +0000760 Size = BlockOffset;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000761 Align = BlockAlign;
762 subBlockDeclRefDecls = BlockDeclRefDecls;
Mike Stump00470a12009-03-05 08:32:30 +0000763 subBlockHasCopyDispose |= BlockHasCopyDispose;
Anders Carlssond5cab542009-02-12 17:55:02 +0000764 return Fn;
765}
Mike Stumpa99038c2009-02-28 09:07:16 +0000766
Mike Stump08920992009-03-07 02:35:30 +0000767uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
Mike Stumpa99038c2009-02-28 09:07:16 +0000768 const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
769
770 uint64_t Size = getContext().getTypeSize(D->getType()) / 8;
771 uint64_t Align = getContext().getDeclAlignInBytes(D);
772
773 if (BDRE->isByRef()) {
774 Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8;
775 Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
776 }
777
778 assert ((Align > 0) && "alignment must be 1 byte or more");
779
780 uint64_t OldOffset = BlockOffset;
781
782 // Ensure proper alignment, even if it means we have to have a gap
783 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align);
784 BlockAlign = std::max(Align, BlockAlign);
Mike Stump00470a12009-03-05 08:32:30 +0000785
Mike Stumpa99038c2009-02-28 09:07:16 +0000786 uint64_t Pad = BlockOffset - OldOffset;
787 if (Pad) {
Owen Anderson0032b272009-08-13 21:57:51 +0000788 llvm::ArrayType::get(llvm::Type::getInt8Ty(VMContext), Pad);
Mike Stumpa99038c2009-02-28 09:07:16 +0000789 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
790 llvm::APInt(32, Pad),
791 ArrayType::Normal, 0);
792 ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000793 0, QualType(PadTy), 0, VarDecl::None);
Mike Stumpa99038c2009-02-28 09:07:16 +0000794 Expr *E;
795 E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
796 SourceLocation(), false, false);
797 BlockDeclRefDecls.push_back(E);
798 }
799 BlockDeclRefDecls.push_back(BDRE);
800
801 BlockOffset += Size;
802 return BlockOffset-Size;
803}
Mike Stumpdab514f2009-03-04 03:23:46 +0000804
Mike Stump08920992009-03-07 02:35:30 +0000805llvm::Constant *BlockFunction::
806GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000807 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000808 QualType R = getContext().VoidTy;
809
810 FunctionArgList Args;
811 // FIXME: This leaks
Mike Stump08920992009-03-07 02:35:30 +0000812 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +0000813 ImplicitParamDecl::Create(getContext(), 0,
814 SourceLocation(), 0,
Mike Stump08920992009-03-07 02:35:30 +0000815 getContext().getPointerType(getContext().VoidTy));
816 Args.push_back(std::make_pair(Dst, Dst->getType()));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000817 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +0000818 ImplicitParamDecl::Create(getContext(), 0,
819 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000820 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa4f668f2009-03-06 01:33:24 +0000821 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000822
Mike Stumpa4f668f2009-03-06 01:33:24 +0000823 const CGFunctionInfo &FI =
824 CGM.getTypes().getFunctionInfo(R, Args);
825
Mike Stump3899a7f2009-06-05 23:26:36 +0000826 // FIXME: We'd like to put these into a mergable by content, with
827 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000828 std::string Name = std::string("__copy_helper_block_");
829 CodeGenTypes &Types = CGM.getTypes();
830 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
831
832 llvm::Function *Fn =
833 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
834 Name,
835 &CGM.getModule());
836
837 IdentifierInfo *II
838 = &CGM.getContext().Idents.get("__copy_helper_block_");
839
840 FunctionDecl *FD = FunctionDecl::Create(getContext(),
841 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000842 SourceLocation(), II, R, 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000843 FunctionDecl::Static, false,
844 true);
845 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump08920992009-03-07 02:35:30 +0000846
847 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
848 llvm::Type *PtrPtrT;
Mike Stump08920992009-03-07 02:35:30 +0000849
Mike Stumpb7477cf2009-04-10 18:52:28 +0000850 if (NoteForHelperp) {
851 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump08920992009-03-07 02:35:30 +0000852
Owen Anderson96e0fc72009-07-29 22:16:19 +0000853 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000854 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
855 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump08920992009-03-07 02:35:30 +0000856
Mike Stumpb7477cf2009-04-10 18:52:28 +0000857 llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000858 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000859 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpc2f4c342009-04-15 22:11:36 +0000860 DstObj = Builder.CreateBitCast(DstObj, PtrPtrT);
861 DstObj = Builder.CreateLoad(DstObj);
Mike Stump08920992009-03-07 02:35:30 +0000862
Mike Stumpb7477cf2009-04-10 18:52:28 +0000863 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
864 int flag = NoteForHelper[i].flag;
865 int index = NoteForHelper[i].index;
Mike Stump08920992009-03-07 02:35:30 +0000866
Mike Stumpb7477cf2009-04-10 18:52:28 +0000867 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
868 || NoteForHelper[i].RequiresCopying) {
869 llvm::Value *Srcv = SrcObj;
870 Srcv = Builder.CreateStructGEP(Srcv, index);
871 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000872 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000873 Srcv = Builder.CreateLoad(Srcv);
874
875 llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
876 Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
877
Owen Anderson0032b272009-08-13 21:57:51 +0000878 llvm::Value *N = llvm::ConstantInt::get(
879 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000880 llvm::Value *F = getBlockObjectAssign();
881 Builder.CreateCall3(F, Dstv, Srcv, N);
882 }
Mike Stump08920992009-03-07 02:35:30 +0000883 }
884 }
885
Mike Stumpa4f668f2009-03-06 01:33:24 +0000886 CGF.FinishFunction();
887
Owen Anderson3c4972d2009-07-29 18:54:39 +0000888 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpdab514f2009-03-04 03:23:46 +0000889}
890
Mike Stumpcf62d392009-03-06 18:42:23 +0000891llvm::Constant *BlockFunction::
Mike Stump08920992009-03-07 02:35:30 +0000892GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
893 const llvm::StructType* T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000894 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpa4f668f2009-03-06 01:33:24 +0000895 QualType R = getContext().VoidTy;
896
897 FunctionArgList Args;
898 // FIXME: This leaks
899 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +0000900 ImplicitParamDecl::Create(getContext(), 0,
901 SourceLocation(), 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000902 getContext().getPointerType(getContext().VoidTy));
903
904 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Mike Stumpa4f668f2009-03-06 01:33:24 +0000906 const CGFunctionInfo &FI =
907 CGM.getTypes().getFunctionInfo(R, Args);
908
Mike Stump3899a7f2009-06-05 23:26:36 +0000909 // FIXME: We'd like to put these into a mergable by content, with
910 // internal linkage.
Mike Stumpa4f668f2009-03-06 01:33:24 +0000911 std::string Name = std::string("__destroy_helper_block_");
912 CodeGenTypes &Types = CGM.getTypes();
913 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
914
915 llvm::Function *Fn =
916 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
917 Name,
918 &CGM.getModule());
919
920 IdentifierInfo *II
921 = &CGM.getContext().Idents.get("__destroy_helper_block_");
922
923 FunctionDecl *FD = FunctionDecl::Create(getContext(),
924 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000925 SourceLocation(), II, R, 0,
Mike Stumpa4f668f2009-03-06 01:33:24 +0000926 FunctionDecl::Static, false,
927 true);
928 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1edf6b62009-03-07 02:53:18 +0000929
Mike Stumpb7477cf2009-04-10 18:52:28 +0000930 if (NoteForHelperp) {
931 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump1edf6b62009-03-07 02:53:18 +0000932
Mike Stumpb7477cf2009-04-10 18:52:28 +0000933 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
934 llvm::Type *PtrPtrT;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000935 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stumpb7477cf2009-04-10 18:52:28 +0000936 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
937 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump1edf6b62009-03-07 02:53:18 +0000938
Mike Stumpb7477cf2009-04-10 18:52:28 +0000939 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
940 int flag = NoteForHelper[i].flag;
941 int index = NoteForHelper[i].index;
Mike Stump1edf6b62009-03-07 02:53:18 +0000942
Mike Stumpb7477cf2009-04-10 18:52:28 +0000943 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
944 || NoteForHelper[i].RequiresCopying) {
945 llvm::Value *Srcv = SrcObj;
946 Srcv = Builder.CreateStructGEP(Srcv, index);
947 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000948 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpb7477cf2009-04-10 18:52:28 +0000949 Srcv = Builder.CreateLoad(Srcv);
950
951 BuildBlockRelease(Srcv, flag);
952 }
Mike Stump1edf6b62009-03-07 02:53:18 +0000953 }
954 }
955
Mike Stumpa4f668f2009-03-06 01:33:24 +0000956 CGF.FinishFunction();
957
Owen Anderson3c4972d2009-07-29 18:54:39 +0000958 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000959}
960
Mike Stump08920992009-03-07 02:35:30 +0000961llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000962 std::vector<HelperInfo> *NoteForHelper) {
Mike Stump08920992009-03-07 02:35:30 +0000963 return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
964 T, NoteForHelper);
Mike Stumpa4f668f2009-03-06 01:33:24 +0000965}
966
Mike Stump08920992009-03-07 02:35:30 +0000967llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000968 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stump08920992009-03-07 02:35:30 +0000969 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
Mike Stumpb7477cf2009-04-10 18:52:28 +0000970 T, NoteForHelperp);
Mike Stumpdab514f2009-03-04 03:23:46 +0000971}
Mike Stump797b6322009-03-05 01:23:13 +0000972
Mike Stumpee094222009-03-06 06:12:24 +0000973llvm::Constant *BlockFunction::
974GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
Mike Stump45031c02009-03-06 02:29:21 +0000975 QualType R = getContext().VoidTy;
976
977 FunctionArgList Args;
978 // FIXME: This leaks
Mike Stumpee094222009-03-06 06:12:24 +0000979 ImplicitParamDecl *Dst =
Mike Stumpea26cb52009-10-21 03:49:08 +0000980 ImplicitParamDecl::Create(getContext(), 0,
981 SourceLocation(), 0,
Mike Stumpee094222009-03-06 06:12:24 +0000982 getContext().getPointerType(getContext().VoidTy));
983 Args.push_back(std::make_pair(Dst, Dst->getType()));
984
985 // FIXME: This leaks
Mike Stump45031c02009-03-06 02:29:21 +0000986 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +0000987 ImplicitParamDecl::Create(getContext(), 0,
988 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +0000989 getContext().getPointerType(getContext().VoidTy));
Mike Stump45031c02009-03-06 02:29:21 +0000990 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Mike Stump45031c02009-03-06 02:29:21 +0000992 const CGFunctionInfo &FI =
993 CGM.getTypes().getFunctionInfo(R, Args);
994
995 std::string Name = std::string("__Block_byref_id_object_copy_");
996 CodeGenTypes &Types = CGM.getTypes();
997 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
998
Mike Stump3899a7f2009-06-05 23:26:36 +0000999 // FIXME: We'd like to put these into a mergable by content, with
1000 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001001 llvm::Function *Fn =
1002 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
1003 Name,
1004 &CGM.getModule());
1005
1006 IdentifierInfo *II
1007 = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
1008
1009 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1010 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001011 SourceLocation(), II, R, 0,
Mike Stump45031c02009-03-06 02:29:21 +00001012 FunctionDecl::Static, false,
1013 true);
1014 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpee094222009-03-06 06:12:24 +00001015
1016 // dst->x
1017 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001018 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001019 V = Builder.CreateLoad(V);
Mike Stumpee094222009-03-06 06:12:24 +00001020 V = Builder.CreateStructGEP(V, 6, "x");
1021 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
1022
1023 // src->x
1024 V = CGF.GetAddrOfLocalVar(Src);
1025 V = Builder.CreateLoad(V);
1026 V = Builder.CreateBitCast(V, T);
1027 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001028 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpee094222009-03-06 06:12:24 +00001029 llvm::Value *SrcObj = Builder.CreateLoad(V);
Mike Stump1eb44332009-09-09 15:08:12 +00001030
Mike Stumpee094222009-03-06 06:12:24 +00001031 flag |= BLOCK_BYREF_CALLER;
1032
Owen Anderson0032b272009-08-13 21:57:51 +00001033 llvm::Value *N = llvm::ConstantInt::get(
1034 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpee094222009-03-06 06:12:24 +00001035 llvm::Value *F = getBlockObjectAssign();
1036 Builder.CreateCall3(F, DstObj, SrcObj, N);
1037
Mike Stump45031c02009-03-06 02:29:21 +00001038 CGF.FinishFunction();
1039
Owen Anderson3c4972d2009-07-29 18:54:39 +00001040 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001041}
1042
Mike Stump1851b682009-03-06 04:53:30 +00001043llvm::Constant *
1044BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
1045 int flag) {
Mike Stump45031c02009-03-06 02:29:21 +00001046 QualType R = getContext().VoidTy;
1047
1048 FunctionArgList Args;
1049 // FIXME: This leaks
1050 ImplicitParamDecl *Src =
Mike Stumpea26cb52009-10-21 03:49:08 +00001051 ImplicitParamDecl::Create(getContext(), 0,
1052 SourceLocation(), 0,
Mike Stump45031c02009-03-06 02:29:21 +00001053 getContext().getPointerType(getContext().VoidTy));
1054
1055 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Mike Stump45031c02009-03-06 02:29:21 +00001057 const CGFunctionInfo &FI =
1058 CGM.getTypes().getFunctionInfo(R, Args);
1059
1060 std::string Name = std::string("__Block_byref_id_object_dispose_");
1061 CodeGenTypes &Types = CGM.getTypes();
1062 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1063
Mike Stump3899a7f2009-06-05 23:26:36 +00001064 // FIXME: We'd like to put these into a mergable by content, with
1065 // internal linkage.
Mike Stump45031c02009-03-06 02:29:21 +00001066 llvm::Function *Fn =
1067 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
1068 Name,
1069 &CGM.getModule());
1070
1071 IdentifierInfo *II
1072 = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
1073
1074 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1075 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001076 SourceLocation(), II, R, 0,
Mike Stump45031c02009-03-06 02:29:21 +00001077 FunctionDecl::Static, false,
1078 true);
1079 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump1851b682009-03-06 04:53:30 +00001080
1081 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001082 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001083 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001084 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001085 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpc2f4c342009-04-15 22:11:36 +00001086 V = Builder.CreateLoad(V);
Mike Stump1851b682009-03-06 04:53:30 +00001087
Mike Stump1851b682009-03-06 04:53:30 +00001088 flag |= BLOCK_BYREF_CALLER;
1089 BuildBlockRelease(V, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001090 CGF.FinishFunction();
1091
Owen Anderson3c4972d2009-07-29 18:54:39 +00001092 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump45031c02009-03-06 02:29:21 +00001093}
1094
Mike Stumpee094222009-03-06 06:12:24 +00001095llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
Mike Stump3899a7f2009-06-05 23:26:36 +00001096 int flag, unsigned Align) {
1097 // All alignments below that of pointer alignment collpase down to just
1098 // pointer alignment, as we always have at least that much alignment to begin
1099 // with.
1100 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
1101 // As an optimization, we only generate a single function of each kind we
1102 // might need. We need a different one for each alignment and for each
1103 // setting of flags. We mix Align and flag to get the kind.
1104 uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag;
1105 llvm::Constant *& Entry = CGM.AssignCache[kind];
1106 if (Entry)
1107 return Entry;
1108 return Entry=CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001109}
1110
Mike Stump1851b682009-03-06 04:53:30 +00001111llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
Mike Stump3899a7f2009-06-05 23:26:36 +00001112 int flag,
1113 unsigned Align) {
1114 // All alignments below that of pointer alignment collpase down to just
1115 // pointer alignment, as we always have at least that much alignment to begin
1116 // with.
1117 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
1118 // As an optimization, we only generate a single function of each kind we
1119 // might need. We need a different one for each alignment and for each
1120 // setting of flags. We mix Align and flag to get the kind.
1121 uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag;
1122 llvm::Constant *& Entry = CGM.DestroyCache[kind];
1123 if (Entry)
1124 return Entry;
1125 return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag);
Mike Stump45031c02009-03-06 02:29:21 +00001126}
1127
Mike Stump797b6322009-03-05 01:23:13 +00001128llvm::Value *BlockFunction::getBlockObjectDispose() {
1129 if (CGM.BlockObjectDispose == 0) {
1130 const llvm::FunctionType *FTy;
1131 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001132 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stump797b6322009-03-05 01:23:13 +00001133 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001134 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson96e0fc72009-07-29 22:16:19 +00001135 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stump00470a12009-03-05 08:32:30 +00001136 CGM.BlockObjectDispose
Mike Stump797b6322009-03-05 01:23:13 +00001137 = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
1138 }
1139 return CGM.BlockObjectDispose;
1140}
1141
Mike Stumpee094222009-03-06 06:12:24 +00001142llvm::Value *BlockFunction::getBlockObjectAssign() {
1143 if (CGM.BlockObjectAssign == 0) {
1144 const llvm::FunctionType *FTy;
1145 std::vector<const llvm::Type*> ArgTys;
Owen Anderson0032b272009-08-13 21:57:51 +00001146 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stumpee094222009-03-06 06:12:24 +00001147 ArgTys.push_back(PtrToInt8Ty);
1148 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001149 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson96e0fc72009-07-29 22:16:19 +00001150 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stumpee094222009-03-06 06:12:24 +00001151 CGM.BlockObjectAssign
1152 = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
1153 }
1154 return CGM.BlockObjectAssign;
1155}
1156
Mike Stump1851b682009-03-06 04:53:30 +00001157void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Mike Stump797b6322009-03-05 01:23:13 +00001158 llvm::Value *F = getBlockObjectDispose();
Mike Stump1851b682009-03-06 04:53:30 +00001159 llvm::Value *N;
Mike Stump797b6322009-03-05 01:23:13 +00001160 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Owen Anderson0032b272009-08-13 21:57:51 +00001161 N = llvm::ConstantInt::get(llvm::Type::getInt32Ty(V->getContext()), flag);
Mike Stump797b6322009-03-05 01:23:13 +00001162 Builder.CreateCall2(F, V, N);
1163}
Mike Stump00470a12009-03-05 08:32:30 +00001164
1165ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stump08920992009-03-07 02:35:30 +00001166
1167BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1168 CGBuilderTy &B)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001169 : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) {
Owen Anderson0032b272009-08-13 21:57:51 +00001170 PtrToInt8Ty = llvm::PointerType::getUnqual(
1171 llvm::Type::getInt8Ty(VMContext));
Mike Stump08920992009-03-07 02:35:30 +00001172
1173 BlockHasCopyDispose = false;
1174}