blob: 8ed1f901c6b327a5d8ba3db5737a0b9e9ebb55b5 [file] [log] [blame]
Anders Carlsson2437cbf2009-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
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
Mike Stump692c6e32009-03-20 21:53:12 +000016#include "clang/AST/DeclObjC.h"
Anders Carlsson2437cbf2009-02-12 00:39:25 +000017#include "llvm/Module.h"
Anders Carlsson6a60fa22009-02-12 17:55:02 +000018#include "llvm/Target/TargetData.h"
Anders Carlsson2437cbf2009-02-12 00:39:25 +000019#include <algorithm>
Torok Edwindb714922009-08-24 13:25:12 +000020#include <cstdio>
21
Anders Carlsson2437cbf2009-02-12 00:39:25 +000022using namespace clang;
23using namespace CodeGen;
24
Mike Stumpd6ef62f2009-03-06 18:42:23 +000025llvm::Constant *CodeGenFunction::
Mike Stumpd2142cf2009-03-25 17:58:24 +000026BuildDescriptorBlockDecl(bool BlockHasCopyDispose, uint64_t Size,
27 const llvm::StructType* Ty,
Mike Stumpaeb0ffd2009-03-07 02:35:30 +000028 std::vector<HelperInfo> *NoteForHelper) {
Mike Stumpc2c38332009-02-13 16:55:51 +000029 const llvm::Type *UnsignedLongTy
30 = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stump85284ba2009-02-13 16:19:19 +000031 llvm::Constant *C;
32 std::vector<llvm::Constant*> Elts;
33
34 // reserved
Owen Andersonb7a2fe62009-07-24 23:12:58 +000035 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stump85284ba2009-02-13 16:19:19 +000036 Elts.push_back(C);
37
38 // Size
Mike Stump2ac40a92009-02-21 20:07:44 +000039 // FIXME: What is the right way to say this doesn't fit? We should give
40 // a user diagnostic in that case. Better fix would be to change the
41 // API to size_t.
Owen Andersonb7a2fe62009-07-24 23:12:58 +000042 C = llvm::ConstantInt::get(UnsignedLongTy, Size);
Mike Stump85284ba2009-02-13 16:19:19 +000043 Elts.push_back(C);
44
45 if (BlockHasCopyDispose) {
46 // copy_func_helper_decl
Mike Stump67645932009-04-10 18:52:28 +000047 Elts.push_back(BuildCopyHelper(Ty, NoteForHelper));
Mike Stump85284ba2009-02-13 16:19:19 +000048
49 // destroy_func_decl
Mike Stump67645932009-04-10 18:52:28 +000050 Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper));
Mike Stump85284ba2009-02-13 16:19:19 +000051 }
52
Nick Lewycky41eaf0a2009-09-19 20:00:52 +000053 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stump85284ba2009-02-13 16:19:19 +000054
Owen Andersonc10c8d32009-07-08 19:05:04 +000055 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Mike Stump85284ba2009-02-13 16:19:19 +000056 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +000057 C, "__block_descriptor_tmp");
Mike Stump85284ba2009-02-13 16:19:19 +000058 return C;
59}
60
Mike Stump95435672009-03-04 18:17:45 +000061llvm::Constant *BlockModule::getNSConcreteGlobalBlock() {
Chris Lattner46813bb2009-05-13 02:50:56 +000062 if (NSConcreteGlobalBlock == 0)
Mike Stump11289f42009-09-09 15:08:12 +000063 NSConcreteGlobalBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
Chris Lattner46813bb2009-05-13 02:50:56 +000064 "_NSConcreteGlobalBlock");
Mike Stump971f9b62009-02-13 17:23:42 +000065 return NSConcreteGlobalBlock;
66}
67
Mike Stump95435672009-03-04 18:17:45 +000068llvm::Constant *BlockModule::getNSConcreteStackBlock() {
Chris Lattner46813bb2009-05-13 02:50:56 +000069 if (NSConcreteStackBlock == 0)
Mike Stump11289f42009-09-09 15:08:12 +000070 NSConcreteStackBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
Chris Lattner46813bb2009-05-13 02:50:56 +000071 "_NSConcreteStackBlock");
Mike Stump7ab278d2009-02-13 19:29:27 +000072 return NSConcreteStackBlock;
73}
74
Mike Stump4446dcf2009-03-05 08:32:30 +000075static void CollectBlockDeclRefInfo(const Stmt *S,
Anders Carlssoned5e69f2009-03-01 01:09:12 +000076 CodeGenFunction::BlockInfo &Info) {
77 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
78 I != E; ++I)
Daniel Dunbardd942712009-03-02 07:00:57 +000079 if (*I)
80 CollectBlockDeclRefInfo(*I, Info);
Mike Stump4446dcf2009-03-05 08:32:30 +000081
Anders Carlssoned5e69f2009-03-01 01:09:12 +000082 if (const BlockDeclRefExpr *DE = dyn_cast<BlockDeclRefExpr>(S)) {
83 // FIXME: Handle enums.
84 if (isa<FunctionDecl>(DE->getDecl()))
85 return;
Mike Stump4446dcf2009-03-05 08:32:30 +000086
Anders Carlssoned5e69f2009-03-01 01:09:12 +000087 if (DE->isByRef())
88 Info.ByRefDeclRefs.push_back(DE);
89 else
90 Info.ByCopyDeclRefs.push_back(DE);
91 }
92}
93
Mike Stump2d33d632009-03-04 22:48:06 +000094/// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be
95/// declared as a global variable instead of on the stack.
Chris Lattner46813bb2009-05-13 02:50:56 +000096static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info) {
Anders Carlssoned5e69f2009-03-01 01:09:12 +000097 return Info.ByRefDeclRefs.empty() && Info.ByCopyDeclRefs.empty();
98}
99
Mike Stump2d33d632009-03-04 22:48:06 +0000100// FIXME: Push most into CGM, passing down a few bits, like current function
101// name.
Mike Stumpb750d922009-02-25 23:33:13 +0000102llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Mike Stump85284ba2009-02-13 16:19:19 +0000103
Anders Carlssoned5e69f2009-03-01 01:09:12 +0000104 std::string Name = CurFn->getName();
105 CodeGenFunction::BlockInfo Info(0, Name.c_str());
106 CollectBlockDeclRefInfo(BE->getBody(), Info);
107
108 // Check if the block can be global.
Mike Stump2d33d632009-03-04 22:48:06 +0000109 // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like
110 // to just have one code path. We should move this function into CGM and pass
111 // CGF, then we can just check to see if CGF is 0.
Mike Stump97d01d52009-03-04 03:23:46 +0000112 if (0 && CanBlockBeGlobal(Info))
Anders Carlssoned5e69f2009-03-01 01:09:12 +0000113 return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
Mike Stump4446dcf2009-03-05 08:32:30 +0000114
115 std::vector<llvm::Constant*> Elts(5);
Mike Stump85284ba2009-02-13 16:19:19 +0000116 llvm::Constant *C;
Mike Stumpb750d922009-02-25 23:33:13 +0000117 llvm::Value *V;
Mike Stump85284ba2009-02-13 16:19:19 +0000118
Mike Stump85284ba2009-02-13 16:19:19 +0000119 {
120 // C = BuildBlockStructInitlist();
121 unsigned int flags = BLOCK_HAS_DESCRIPTOR;
122
Mike Stump4446dcf2009-03-05 08:32:30 +0000123 // We run this first so that we set BlockHasCopyDispose from the entire
124 // block literal.
125 // __invoke
126 uint64_t subBlockSize, subBlockAlign;
127 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Mike Stumpd2142cf2009-03-25 17:58:24 +0000128 bool subBlockHasCopyDispose = false;
Mike Stump4446dcf2009-03-05 08:32:30 +0000129 llvm::Function *Fn
Mike Stump692c6e32009-03-20 21:53:12 +0000130 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl, LocalDeclMap,
Mike Stump5469f292009-03-13 23:34:28 +0000131 subBlockSize,
Mike Stump4446dcf2009-03-05 08:32:30 +0000132 subBlockAlign,
133 subBlockDeclRefDecls,
Mike Stumpd2142cf2009-03-25 17:58:24 +0000134 subBlockHasCopyDispose);
135 BlockHasCopyDispose |= subBlockHasCopyDispose;
Mike Stump4446dcf2009-03-05 08:32:30 +0000136 Elts[3] = Fn;
137
Mike Stump18bb9282009-05-16 07:57:57 +0000138 // FIXME: Don't use BlockHasCopyDispose, it is set more often then
139 // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); }
Mike Stumpd2142cf2009-03-25 17:58:24 +0000140 if (subBlockHasCopyDispose)
Mike Stump85284ba2009-02-13 16:19:19 +0000141 flags |= BLOCK_HAS_COPY_DISPOSE;
142
Mike Stump499ae7e2009-02-13 20:17:16 +0000143 // __isa
Mike Stump7ab278d2009-02-13 19:29:27 +0000144 C = CGM.getNSConcreteStackBlock();
Owen Andersonade90fd2009-07-29 18:54:39 +0000145 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
Mike Stump4446dcf2009-03-05 08:32:30 +0000146 Elts[0] = C;
Mike Stump85284ba2009-02-13 16:19:19 +0000147
148 // __flags
149 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
150 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000151 C = llvm::ConstantInt::get(IntTy, flags);
Mike Stump4446dcf2009-03-05 08:32:30 +0000152 Elts[1] = C;
Mike Stump85284ba2009-02-13 16:19:19 +0000153
154 // __reserved
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000155 C = llvm::ConstantInt::get(IntTy, 0);
Mike Stump4446dcf2009-03-05 08:32:30 +0000156 Elts[2] = C;
Mike Stump85284ba2009-02-13 16:19:19 +0000157
Mike Stumpb750d922009-02-25 23:33:13 +0000158 if (subBlockDeclRefDecls.size() == 0) {
Mike Stumpd6ef62f2009-03-06 18:42:23 +0000159 // __descriptor
Mike Stumpd2142cf2009-03-25 17:58:24 +0000160 Elts[4] = BuildDescriptorBlockDecl(subBlockHasCopyDispose, subBlockSize, 0, 0);
Mike Stumpd6ef62f2009-03-06 18:42:23 +0000161
Mike Stump3a139f32009-03-01 20:07:53 +0000162 // Optimize to being a global block.
163 Elts[0] = CGM.getNSConcreteGlobalBlock();
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000164 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
Mike Stump3a139f32009-03-01 20:07:53 +0000165
Nick Lewycky41eaf0a2009-09-19 20:00:52 +0000166 C = llvm::ConstantStruct::get(VMContext, Elts, false);
Mike Stumpb750d922009-02-25 23:33:13 +0000167
168 char Name[32];
169 sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
Owen Andersonc10c8d32009-07-08 19:05:04 +0000170 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
Mike Stumpb750d922009-02-25 23:33:13 +0000171 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +0000172 C, Name);
Mike Stumpb750d922009-02-25 23:33:13 +0000173 QualType BPT = BE->getType();
Owen Andersonade90fd2009-07-29 18:54:39 +0000174 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
Mike Stumpb750d922009-02-25 23:33:13 +0000175 return C;
176 }
Mike Stump4446dcf2009-03-05 08:32:30 +0000177
Mike Stumpb750d922009-02-25 23:33:13 +0000178 std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size());
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000179 for (int i=0; i<4; ++i)
Mike Stumpb750d922009-02-25 23:33:13 +0000180 Types[i] = Elts[i]->getType();
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000181 Types[4] = PtrToInt8Ty;
Mike Stumpb750d922009-02-25 23:33:13 +0000182
Mike Stump1db7d042009-02-28 09:07:16 +0000183 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
184 const Expr *E = subBlockDeclRefDecls[i];
185 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
186 QualType Ty = E->getType();
Mike Stump97d01d52009-03-04 03:23:46 +0000187 if (BDRE && BDRE->isByRef()) {
Anders Carlsson71d1d922009-09-09 02:51:03 +0000188 Types[i+5] = llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0);
Mike Stump97d01d52009-03-04 03:23:46 +0000189 } else
190 Types[i+5] = ConvertType(Ty);
Mike Stump1db7d042009-02-28 09:07:16 +0000191 }
Mike Stumpb750d922009-02-25 23:33:13 +0000192
Owen Anderson758428f2009-08-05 23:18:46 +0000193 llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true);
Mike Stumpb750d922009-02-25 23:33:13 +0000194
195 llvm::AllocaInst *A = CreateTempAlloca(Ty);
196 A->setAlignment(subBlockAlign);
197 V = A;
198
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000199 std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size());
200 int helpersize = 0;
201
202 for (unsigned i=0; i<4; ++i)
Mike Stumpb750d922009-02-25 23:33:13 +0000203 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
Mike Stump4446dcf2009-03-05 08:32:30 +0000204
Mike Stumpb750d922009-02-25 23:33:13 +0000205 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
206 {
Mike Stump1db7d042009-02-28 09:07:16 +0000207 // FIXME: Push const down.
208 Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
209 DeclRefExpr *DR;
210 ValueDecl *VD;
Mike Stumpb750d922009-02-25 23:33:13 +0000211
Mike Stump1db7d042009-02-28 09:07:16 +0000212 DR = dyn_cast<DeclRefExpr>(E);
213 // Skip padding.
214 if (DR) continue;
215
216 BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
217 VD = BDRE->getDecl();
218
Mike Stump97d01d52009-03-04 03:23:46 +0000219 llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp");
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000220 NoteForHelper[helpersize].index = i+5;
Mike Stump2114d7c2009-09-22 02:12:52 +0000221 NoteForHelper[helpersize].RequiresCopying
222 = BlockRequiresCopying(VD->getType());
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000223 NoteForHelper[helpersize].flag
Mike Stump2114d7c2009-09-22 02:12:52 +0000224 = (VD->getType()->isBlockPointerType()
225 ? BLOCK_FIELD_IS_BLOCK
226 : BLOCK_FIELD_IS_OBJECT);
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000227
Mike Stump1db7d042009-02-28 09:07:16 +0000228 if (LocalDeclMap[VD]) {
Mike Stump97d01d52009-03-04 03:23:46 +0000229 if (BDRE->isByRef()) {
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000230 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
Mike Stump90d8daf2009-03-07 06:04:31 +0000231 // FIXME: Someone double check this.
232 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stump97d01d52009-03-04 03:23:46 +0000233 llvm::Value *Loc = LocalDeclMap[VD];
234 Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
235 Loc = Builder.CreateLoad(Loc, false);
Mike Stump97d01d52009-03-04 03:23:46 +0000236 Builder.CreateStore(Loc, Addr);
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000237 ++helpersize;
Mike Stump97d01d52009-03-04 03:23:46 +0000238 continue;
239 } else
240 E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD),
241 VD->getType(), SourceLocation(),
242 false, false);
Mike Stump1db7d042009-02-28 09:07:16 +0000243 }
Mike Stump97d01d52009-03-04 03:23:46 +0000244 if (BDRE->isByRef()) {
Mike Stumpefd7caa82009-03-21 21:00:35 +0000245 NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
246 // FIXME: Someone double check this.
247 (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
Mike Stump1db7d042009-02-28 09:07:16 +0000248 E = new (getContext())
249 UnaryOperator(E, UnaryOperator::AddrOf,
250 getContext().getPointerType(E->getType()),
251 SourceLocation());
Mike Stump97d01d52009-03-04 03:23:46 +0000252 }
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000253 ++helpersize;
Mike Stump1db7d042009-02-28 09:07:16 +0000254
Mike Stump1db7d042009-02-28 09:07:16 +0000255 RValue r = EmitAnyExpr(E, Addr, false);
Mike Stump97d01d52009-03-04 03:23:46 +0000256 if (r.isScalar()) {
257 llvm::Value *Loc = r.getScalarVal();
258 const llvm::Type *Ty = Types[i+5];
259 if (BDRE->isByRef()) {
Mike Stump2d33d632009-03-04 22:48:06 +0000260 // E is now the address of the value field, instead, we want the
261 // address of the actual ByRef struct. We optimize this slightly
262 // compared to gcc by not grabbing the forwarding slot as this must
263 // be done during Block_copy for us, and we can postpone the work
264 // until then.
265 uint64_t offset = BlockDecls[BDRE->getDecl()];
Mike Stump4446dcf2009-03-05 08:32:30 +0000266
Mike Stump2d33d632009-03-04 22:48:06 +0000267 llvm::Value *BlockLiteral = LoadBlockStruct();
Mike Stump4446dcf2009-03-05 08:32:30 +0000268
Mike Stump2d33d632009-03-04 22:48:06 +0000269 Loc = Builder.CreateGEP(BlockLiteral,
Owen Anderson41a75022009-08-13 21:57:51 +0000270 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Mike Stump2d33d632009-03-04 22:48:06 +0000271 offset),
272 "block.literal");
Owen Anderson9793f0e2009-07-29 22:16:19 +0000273 Ty = llvm::PointerType::get(Ty, 0);
Mike Stump97d01d52009-03-04 03:23:46 +0000274 Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stump2d33d632009-03-04 22:48:06 +0000275 Loc = Builder.CreateLoad(Loc, false);
276 // Loc = Builder.CreateBitCast(Loc, Ty);
Mike Stump97d01d52009-03-04 03:23:46 +0000277 }
278 Builder.CreateStore(Loc, Addr);
279 } else if (r.isComplex())
Mike Stumpb750d922009-02-25 23:33:13 +0000280 // FIXME: implement
281 ErrorUnsupported(BE, "complex in block literal");
282 else if (r.isAggregate())
283 ; // Already created into the destination
284 else
285 assert (0 && "bad block variable");
286 // FIXME: Ensure that the offset created by the backend for
287 // the struct matches the previously computed offset in BlockDecls.
288 }
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000289 NoteForHelper.resize(helpersize);
Mike Stumpd6ef62f2009-03-06 18:42:23 +0000290
291 // __descriptor
Mike Stumpd2142cf2009-03-25 17:58:24 +0000292 llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockHasCopyDispose,
293 subBlockSize, Ty,
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000294 &NoteForHelper);
295 Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
296 Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
Mike Stump85284ba2009-02-13 16:19:19 +0000297 }
Mike Stump4446dcf2009-03-05 08:32:30 +0000298
Mike Stump5d2534ad2009-02-19 01:01:04 +0000299 QualType BPT = BE->getType();
Mike Stumpb750d922009-02-25 23:33:13 +0000300 return Builder.CreateBitCast(V, ConvertType(BPT));
Mike Stump85284ba2009-02-13 16:19:19 +0000301}
302
303
Mike Stump95435672009-03-04 18:17:45 +0000304const llvm::Type *BlockModule::getBlockDescriptorType() {
Mike Stump650c9322009-02-13 15:16:56 +0000305 if (BlockDescriptorType)
306 return BlockDescriptorType;
307
Mike Stumpb7074c02009-02-13 15:32:32 +0000308 const llvm::Type *UnsignedLongTy =
Mike Stump650c9322009-02-13 15:16:56 +0000309 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpb7074c02009-02-13 15:32:32 +0000310
Mike Stump650c9322009-02-13 15:16:56 +0000311 // struct __block_descriptor {
312 // unsigned long reserved;
313 // unsigned long block_size;
314 // };
Owen Anderson758428f2009-08-05 23:18:46 +0000315 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(),
316 UnsignedLongTy,
Mike Stumpb7074c02009-02-13 15:32:32 +0000317 UnsignedLongTy,
Mike Stump650c9322009-02-13 15:16:56 +0000318 NULL);
319
320 getModule().addTypeName("struct.__block_descriptor",
321 BlockDescriptorType);
322
323 return BlockDescriptorType;
Anders Carlsson2437cbf2009-02-12 00:39:25 +0000324}
325
Mike Stump95435672009-03-04 18:17:45 +0000326const llvm::Type *BlockModule::getGenericBlockLiteralType() {
Mike Stump005c9a62009-02-13 15:25:34 +0000327 if (GenericBlockLiteralType)
328 return GenericBlockLiteralType;
329
Mike Stumpb7074c02009-02-13 15:32:32 +0000330 const llvm::Type *BlockDescPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000331 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpb7074c02009-02-13 15:32:32 +0000332
Mike Stump92bbd6d2009-02-13 16:01:35 +0000333 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
334 getTypes().ConvertType(getContext().IntTy));
335
Mike Stump005c9a62009-02-13 15:25:34 +0000336 // struct __block_literal_generic {
Mike Stump5d2534ad2009-02-19 01:01:04 +0000337 // void *__isa;
338 // int __flags;
339 // int __reserved;
340 // void (*__invoke)(void *);
341 // struct __block_descriptor *__descriptor;
Mike Stump005c9a62009-02-13 15:25:34 +0000342 // };
Owen Anderson758428f2009-08-05 23:18:46 +0000343 GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
344 PtrToInt8Ty,
Mike Stump92bbd6d2009-02-13 16:01:35 +0000345 IntTy,
346 IntTy,
Mike Stump626aecc2009-03-05 01:23:13 +0000347 PtrToInt8Ty,
Mike Stump005c9a62009-02-13 15:25:34 +0000348 BlockDescPtrTy,
349 NULL);
Mike Stumpb7074c02009-02-13 15:32:32 +0000350
Mike Stump005c9a62009-02-13 15:25:34 +0000351 getModule().addTypeName("struct.__block_literal_generic",
352 GenericBlockLiteralType);
Mike Stumpb7074c02009-02-13 15:32:32 +0000353
Mike Stump005c9a62009-02-13 15:25:34 +0000354 return GenericBlockLiteralType;
Anders Carlsson2437cbf2009-02-12 00:39:25 +0000355}
356
Mike Stump95435672009-03-04 18:17:45 +0000357const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() {
Mike Stump5d2534ad2009-02-19 01:01:04 +0000358 if (GenericExtendedBlockLiteralType)
359 return GenericExtendedBlockLiteralType;
360
Mike Stump5d2534ad2009-02-19 01:01:04 +0000361 const llvm::Type *BlockDescPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000362 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stump5d2534ad2009-02-19 01:01:04 +0000363
364 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
365 getTypes().ConvertType(getContext().IntTy));
366
367 // struct __block_literal_generic {
368 // void *__isa;
369 // int __flags;
370 // int __reserved;
371 // void (*__invoke)(void *);
372 // struct __block_descriptor *__descriptor;
373 // void *__copy_func_helper_decl;
374 // void *__destroy_func_decl;
375 // };
Owen Anderson758428f2009-08-05 23:18:46 +0000376 GenericExtendedBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
377 PtrToInt8Ty,
Mike Stump5d2534ad2009-02-19 01:01:04 +0000378 IntTy,
379 IntTy,
Mike Stump626aecc2009-03-05 01:23:13 +0000380 PtrToInt8Ty,
Mike Stump5d2534ad2009-02-19 01:01:04 +0000381 BlockDescPtrTy,
Mike Stump626aecc2009-03-05 01:23:13 +0000382 PtrToInt8Ty,
383 PtrToInt8Ty,
Mike Stump5d2534ad2009-02-19 01:01:04 +0000384 NULL);
385
386 getModule().addTypeName("struct.__block_literal_extended_generic",
387 GenericExtendedBlockLiteralType);
388
389 return GenericExtendedBlockLiteralType;
390}
391
Mike Stump2114d7c2009-09-22 02:12:52 +0000392bool BlockFunction::BlockRequiresCopying(QualType Ty) {
393 return CGM.BlockRequiresCopying(Ty);
394}
395
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000396RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpb7074c02009-02-13 15:32:32 +0000397 const BlockPointerType *BPT =
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000398 E->getCallee()->getType()->getAs<BlockPointerType>();
Mike Stumpb7074c02009-02-13 15:32:32 +0000399
Anders Carlsson2437cbf2009-02-12 00:39:25 +0000400 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
401
402 // Get a pointer to the generic block literal.
403 const llvm::Type *BlockLiteralTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000404 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlsson2437cbf2009-02-12 00:39:25 +0000405
406 // Bitcast the callee to a block literal.
Mike Stumpb7074c02009-02-13 15:32:32 +0000407 llvm::Value *BlockLiteral =
Anders Carlsson2437cbf2009-02-12 00:39:25 +0000408 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
409
410 // Get the function pointer from the literal.
411 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Anders Carlsson2437cbf2009-02-12 00:39:25 +0000412
Mike Stumpb7074c02009-02-13 15:32:32 +0000413 BlockLiteral =
414 Builder.CreateBitCast(BlockLiteral,
Owen Anderson41a75022009-08-13 21:57:51 +0000415 llvm::PointerType::getUnqual(
416 llvm::Type::getInt8Ty(VMContext)),
Anders Carlsson2437cbf2009-02-12 00:39:25 +0000417 "tmp");
Mike Stumpb7074c02009-02-13 15:32:32 +0000418
Anders Carlsson2437cbf2009-02-12 00:39:25 +0000419 // Add the block literal.
420 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
421 CallArgList Args;
422 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpb7074c02009-02-13 15:32:32 +0000423
Anders Carlsson479e6fc2009-04-08 23:13:16 +0000424 QualType FnType = BPT->getPointeeType();
425
Anders Carlsson2437cbf2009-02-12 00:39:25 +0000426 // And the rest of the arguments.
John McCall9dd450b2009-09-21 23:43:11 +0000427 EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(),
Anders Carlsson479e6fc2009-04-08 23:13:16 +0000428 E->arg_begin(), E->arg_end());
Mike Stumpb7074c02009-02-13 15:32:32 +0000429
Anders Carlsson5f50c652009-04-07 22:10:22 +0000430 // Load the function.
431 llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp");
432
John McCall9dd450b2009-09-21 23:43:11 +0000433 QualType ResultType = FnType->getAs<FunctionType>()->getResultType();
Anders Carlssona60cbcd2009-04-08 02:55:55 +0000434
Mike Stump11289f42009-09-09 15:08:12 +0000435 const CGFunctionInfo &FnInfo =
Anders Carlssona60cbcd2009-04-08 02:55:55 +0000436 CGM.getTypes().getFunctionInfo(ResultType, Args);
Mike Stump11289f42009-09-09 15:08:12 +0000437
Anders Carlsson5f50c652009-04-07 22:10:22 +0000438 // Cast the function pointer to the right type.
Mike Stump11289f42009-09-09 15:08:12 +0000439 const llvm::Type *BlockFTy =
Anders Carlssona60cbcd2009-04-08 02:55:55 +0000440 CGM.getTypes().GetFunctionType(FnInfo, false);
Mike Stump11289f42009-09-09 15:08:12 +0000441
Owen Anderson9793f0e2009-07-29 22:16:19 +0000442 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
Anders Carlsson5f50c652009-04-07 22:10:22 +0000443 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000444
Anders Carlsson2437cbf2009-02-12 00:39:25 +0000445 // And call the block.
Anders Carlsson350da602009-04-07 00:20:24 +0000446 return EmitCall(FnInfo, Func, Args);
Anders Carlsson2437cbf2009-02-12 00:39:25 +0000447}
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000448
Mike Stump97d01d52009-03-04 03:23:46 +0000449llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
Anders Carlsson0168f4b2009-09-12 02:14:24 +0000450 const ValueDecl *VD = E->getDecl();
451
452 uint64_t &offset = BlockDecls[VD];
Mike Stump97d01d52009-03-04 03:23:46 +0000453
Mike Stump97d01d52009-03-04 03:23:46 +0000454
Mike Stump97d01d52009-03-04 03:23:46 +0000455 // See if we have already allocated an offset for this variable.
456 if (offset == 0) {
Mike Stump4446dcf2009-03-05 08:32:30 +0000457 // Don't run the expensive check, unless we have to.
458 if (!BlockHasCopyDispose && BlockRequiresCopying(E->getType()))
459 BlockHasCopyDispose = true;
Mike Stump97d01d52009-03-04 03:23:46 +0000460 // if not, allocate one now.
461 offset = getBlockOffset(E);
462 }
463
464 llvm::Value *BlockLiteral = LoadBlockStruct();
465 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
Owen Anderson41a75022009-08-13 21:57:51 +0000466 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Mike Stump4a3999f2009-09-09 13:00:44 +0000467 offset),
Mike Stump2d33d632009-03-04 22:48:06 +0000468 "block.literal");
Mike Stump97d01d52009-03-04 03:23:46 +0000469 if (E->isByRef()) {
Mike Stump97d01d52009-03-04 03:23:46 +0000470 const llvm::Type *PtrStructTy
Anders Carlsson0168f4b2009-09-12 02:14:24 +0000471 = llvm::PointerType::get(BuildByRefType(VD), 0);
Mike Stumpefd7caa82009-03-21 21:00:35 +0000472 // The block literal will need a copy/destroy helper.
473 BlockHasCopyDispose = true;
Anders Carlsson0168f4b2009-09-12 02:14:24 +0000474
475 const llvm::Type *Ty = PtrStructTy;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000476 Ty = llvm::PointerType::get(Ty, 0);
Mike Stump97d01d52009-03-04 03:23:46 +0000477 V = Builder.CreateBitCast(V, Ty);
478 V = Builder.CreateLoad(V, false);
479 V = Builder.CreateStructGEP(V, 1, "forwarding");
480 V = Builder.CreateLoad(V, false);
481 V = Builder.CreateBitCast(V, PtrStructTy);
Anders Carlsson0168f4b2009-09-12 02:14:24 +0000482 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
483 VD->getNameAsString());
Mike Stump97d01d52009-03-04 03:23:46 +0000484 } else {
Anders Carlsson0168f4b2009-09-12 02:14:24 +0000485 const llvm::Type *Ty = CGM.getTypes().ConvertType(VD->getType());
486
Owen Anderson9793f0e2009-07-29 22:16:19 +0000487 Ty = llvm::PointerType::get(Ty, 0);
Mike Stump97d01d52009-03-04 03:23:46 +0000488 V = Builder.CreateBitCast(V, Ty);
489 }
490 return V;
491}
492
Mike Stump692c6e32009-03-20 21:53:12 +0000493void CodeGenFunction::BlockForwardSelf() {
494 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
495 ImplicitParamDecl *SelfDecl = OMD->getSelfDecl();
496 llvm::Value *&DMEntry = LocalDeclMap[SelfDecl];
497 if (DMEntry)
498 return;
499 // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care
500 BlockDeclRefExpr *BDRE = new (getContext())
501 BlockDeclRefExpr(SelfDecl,
502 SelfDecl->getType(), SourceLocation(), false);
503 DMEntry = GetAddrOfBlockDecl(BDRE);
504}
505
Mike Stump2d5a2872009-02-14 22:16:35 +0000506llvm::Constant *
Mike Stump6c396662009-03-04 18:47:42 +0000507BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000508 // Generate the block descriptor.
509 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump92bbd6d2009-02-13 16:01:35 +0000510 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
511 getTypes().ConvertType(getContext().IntTy));
Mike Stumpb7074c02009-02-13 15:32:32 +0000512
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000513 llvm::Constant *DescriptorFields[2];
Mike Stumpb7074c02009-02-13 15:32:32 +0000514
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000515 // Reserved
Owen Anderson0b75f232009-07-31 20:28:54 +0000516 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpb7074c02009-02-13 15:32:32 +0000517
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000518 // Block literal size. For global blocks we just use the size of the generic
519 // block literal struct.
Mike Stumpb7074c02009-02-13 15:32:32 +0000520 uint64_t BlockLiteralSize =
Mike Stump005c9a62009-02-13 15:25:34 +0000521 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Owen Anderson170229f2009-07-14 23:10:40 +0000522 DescriptorFields[1] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000523 llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpb7074c02009-02-13 15:32:32 +0000524
525 llvm::Constant *DescriptorStruct =
Nick Lewycky41eaf0a2009-09-19 20:00:52 +0000526 llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 2, false);
Mike Stumpb7074c02009-02-13 15:32:32 +0000527
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000528 llvm::GlobalVariable *Descriptor =
Owen Andersonc10c8d32009-07-08 19:05:04 +0000529 new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true,
Mike Stumpb7074c02009-02-13 15:32:32 +0000530 llvm::GlobalVariable::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +0000531 DescriptorStruct, "__block_descriptor_global");
Mike Stumpb7074c02009-02-13 15:32:32 +0000532
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000533 // Generate the constants for the block literal.
534 llvm::Constant *LiteralFields[5];
Mike Stumpb7074c02009-02-13 15:32:32 +0000535
Mike Stump2d5a2872009-02-14 22:16:35 +0000536 CodeGenFunction::BlockInfo Info(0, n);
Mike Stumpb750d922009-02-25 23:33:13 +0000537 uint64_t subBlockSize, subBlockAlign;
Mike Stump1db7d042009-02-28 09:07:16 +0000538 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Daniel Dunbara61bb4d2009-03-12 03:07:24 +0000539 bool subBlockHasCopyDispose = false;
Mike Stump5469f292009-03-13 23:34:28 +0000540 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
Mike Stumpcb2fbcb2009-02-21 20:00:35 +0000541 llvm::Function *Fn
Mike Stump692c6e32009-03-20 21:53:12 +0000542 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap,
Mike Stump5469f292009-03-13 23:34:28 +0000543 subBlockSize,
Mike Stump6c396662009-03-04 18:47:42 +0000544 subBlockAlign,
Mike Stump4446dcf2009-03-05 08:32:30 +0000545 subBlockDeclRefDecls,
546 subBlockHasCopyDispose);
Mike Stumpcb2fbcb2009-02-21 20:00:35 +0000547 assert(subBlockSize == BlockLiteralSize
548 && "no imports allowed for global block");
Mike Stumpb7074c02009-02-13 15:32:32 +0000549
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000550 // isa
Mike Stump971f9b62009-02-13 17:23:42 +0000551 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpb7074c02009-02-13 15:32:32 +0000552
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000553 // Flags
Mike Stump4446dcf2009-03-05 08:32:30 +0000554 LiteralFields[1] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000555 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR);
Mike Stumpb7074c02009-02-13 15:32:32 +0000556
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000557 // Reserved
Owen Anderson0b75f232009-07-31 20:28:54 +0000558 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpb7074c02009-02-13 15:32:32 +0000559
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000560 // Function
561 LiteralFields[3] = Fn;
Mike Stumpb7074c02009-02-13 15:32:32 +0000562
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000563 // Descriptor
564 LiteralFields[4] = Descriptor;
Mike Stumpb7074c02009-02-13 15:32:32 +0000565
566 llvm::Constant *BlockLiteralStruct =
Nick Lewycky41eaf0a2009-09-19 20:00:52 +0000567 llvm::ConstantStruct::get(VMContext, &LiteralFields[0], 5, false);
Mike Stumpb7074c02009-02-13 15:32:32 +0000568
569 llvm::GlobalVariable *BlockLiteral =
Owen Andersonc10c8d32009-07-08 19:05:04 +0000570 new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true,
Mike Stumpb7074c02009-02-13 15:32:32 +0000571 llvm::GlobalVariable::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +0000572 BlockLiteralStruct, "__block_literal_global");
Mike Stumpb7074c02009-02-13 15:32:32 +0000573
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000574 return BlockLiteral;
575}
576
Mike Stumpcb2fbcb2009-02-21 20:00:35 +0000577llvm::Value *CodeGenFunction::LoadBlockStruct() {
578 return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self");
579}
580
Mike Stump4446dcf2009-03-05 08:32:30 +0000581llvm::Function *
582CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
583 const BlockInfo& Info,
Mike Stump692c6e32009-03-20 21:53:12 +0000584 const Decl *OuterFuncDecl,
Mike Stump5469f292009-03-13 23:34:28 +0000585 llvm::DenseMap<const Decl*, llvm::Value*> ldm,
Mike Stump4446dcf2009-03-05 08:32:30 +0000586 uint64_t &Size,
587 uint64_t &Align,
588 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls,
589 bool &subBlockHasCopyDispose) {
Devang Patel9074ed82009-04-15 21:51:44 +0000590
591 // Check if we should generate debug info for this block.
592 if (CGM.getDebugInfo())
593 DebugInfo = CGM.getDebugInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000594
Mike Stump5469f292009-03-13 23:34:28 +0000595 // Arrange for local static and local extern declarations to appear
596 // to be local to this function as well, as they are directly referenced
597 // in a block.
598 for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
599 i != ldm.end();
600 ++i) {
601 const VarDecl *VD = dyn_cast<VarDecl>(i->first);
Mike Stump11289f42009-09-09 15:08:12 +0000602
Daniel Dunbar0ca16602009-04-14 02:25:56 +0000603 if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage())
Mike Stump5469f292009-03-13 23:34:28 +0000604 LocalDeclMap[VD] = i->second;
605 }
606
Eli Friedman09a9b6e2009-03-28 03:24:54 +0000607 // FIXME: We need to rearrange the code for copy/dispose so we have this
608 // sooner, so we can calculate offsets correctly.
609 if (!BlockHasCopyDispose)
610 BlockOffset = CGM.getTargetData()
611 .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8;
612 else
613 BlockOffset = CGM.getTargetData()
614 .getTypeStoreSizeInBits(CGM.getGenericExtendedBlockLiteralType()) / 8;
615 BlockAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
616
Fariborz Jahanianaae43492009-04-11 17:55:15 +0000617 const FunctionType *BlockFunctionType = BExpr->getFunctionType();
618 QualType ResultType;
619 bool IsVariadic;
Mike Stump11289f42009-09-09 15:08:12 +0000620 if (const FunctionProtoType *FTy =
Fariborz Jahaniana4f6b6b2009-04-11 18:54:57 +0000621 dyn_cast<FunctionProtoType>(BlockFunctionType)) {
Fariborz Jahanianaae43492009-04-11 17:55:15 +0000622 ResultType = FTy->getResultType();
623 IsVariadic = FTy->isVariadic();
Mike Stump658fe022009-07-30 22:28:39 +0000624 } else {
Fariborz Jahanianaae43492009-04-11 17:55:15 +0000625 // K&R style block.
626 ResultType = BlockFunctionType->getResultType();
627 IsVariadic = false;
628 }
Mike Stumpb7074c02009-02-13 15:32:32 +0000629
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000630 FunctionArgList Args;
Mike Stumpb7074c02009-02-13 15:32:32 +0000631
Chris Lattner3385fe122009-02-28 19:01:03 +0000632 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000633
634 // FIXME: This leaks
Mike Stumpb7074c02009-02-13 15:32:32 +0000635 ImplicitParamDecl *SelfDecl =
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000636 ImplicitParamDecl::Create(getContext(), 0,
637 SourceLocation(), 0,
638 getContext().getPointerType(getContext().VoidTy));
Mike Stumpb7074c02009-02-13 15:32:32 +0000639
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000640 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stumpcb2fbcb2009-02-21 20:00:35 +0000641 BlockStructDecl = SelfDecl;
Mike Stumpb7074c02009-02-13 15:32:32 +0000642
Steve Naroffc4b30e52009-03-13 16:56:44 +0000643 for (BlockDecl::param_const_iterator i = BD->param_begin(),
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000644 e = BD->param_end(); i != e; ++i)
Mike Stumpeecd39f2009-02-17 23:25:52 +0000645 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpb7074c02009-02-13 15:32:32 +0000646
647 const CGFunctionInfo &FI =
Fariborz Jahanianaae43492009-04-11 17:55:15 +0000648 CGM.getTypes().getFunctionInfo(ResultType, Args);
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000649
Mike Stump2d5a2872009-02-14 22:16:35 +0000650 std::string Name = std::string("__") + Info.Name + "_block_invoke_";
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000651 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanianaae43492009-04-11 17:55:15 +0000652 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic);
Mike Stumpb7074c02009-02-13 15:32:32 +0000653
654 llvm::Function *Fn =
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000655 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
656 Name,
657 &CGM.getModule());
Mike Stumpb7074c02009-02-13 15:32:32 +0000658
Daniel Dunbarc3e7cff2009-04-17 00:48:04 +0000659 CGM.SetInternalFunctionAttributes(BD, Fn, FI);
660
Chris Lattner324f80b2009-04-23 07:18:56 +0000661 StartFunction(BD, ResultType, Fn, Args,
Chris Lattner3385fe122009-02-28 19:01:03 +0000662 BExpr->getBody()->getLocEnd());
Chris Lattner324f80b2009-04-23 07:18:56 +0000663 CurFuncDecl = OuterFuncDecl;
Chris Lattner28ec0cf2009-04-23 05:30:27 +0000664 CurCodeDecl = BD;
Chris Lattner3385fe122009-02-28 19:01:03 +0000665 EmitStmt(BExpr->getBody());
666 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000667
Mike Stumpb750d922009-02-25 23:33:13 +0000668 // The runtime needs a minimum alignment of a void *.
669 uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
670 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign);
671
Mike Stumpcb2fbcb2009-02-21 20:00:35 +0000672 Size = BlockOffset;
Mike Stumpb750d922009-02-25 23:33:13 +0000673 Align = BlockAlign;
674 subBlockDeclRefDecls = BlockDeclRefDecls;
Mike Stump4446dcf2009-03-05 08:32:30 +0000675 subBlockHasCopyDispose |= BlockHasCopyDispose;
Anders Carlsson6a60fa22009-02-12 17:55:02 +0000676 return Fn;
677}
Mike Stump1db7d042009-02-28 09:07:16 +0000678
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000679uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
Mike Stump1db7d042009-02-28 09:07:16 +0000680 const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
681
682 uint64_t Size = getContext().getTypeSize(D->getType()) / 8;
683 uint64_t Align = getContext().getDeclAlignInBytes(D);
684
685 if (BDRE->isByRef()) {
686 Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8;
687 Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
688 }
689
690 assert ((Align > 0) && "alignment must be 1 byte or more");
691
692 uint64_t OldOffset = BlockOffset;
693
694 // Ensure proper alignment, even if it means we have to have a gap
695 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align);
696 BlockAlign = std::max(Align, BlockAlign);
Mike Stump4446dcf2009-03-05 08:32:30 +0000697
Mike Stump1db7d042009-02-28 09:07:16 +0000698 uint64_t Pad = BlockOffset - OldOffset;
699 if (Pad) {
Owen Anderson41a75022009-08-13 21:57:51 +0000700 llvm::ArrayType::get(llvm::Type::getInt8Ty(VMContext), Pad);
Mike Stump1db7d042009-02-28 09:07:16 +0000701 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
702 llvm::APInt(32, Pad),
703 ArrayType::Normal, 0);
704 ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000705 0, QualType(PadTy), 0, VarDecl::None);
Mike Stump1db7d042009-02-28 09:07:16 +0000706 Expr *E;
707 E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
708 SourceLocation(), false, false);
709 BlockDeclRefDecls.push_back(E);
710 }
711 BlockDeclRefDecls.push_back(BDRE);
712
713 BlockOffset += Size;
714 return BlockOffset-Size;
715}
Mike Stump97d01d52009-03-04 03:23:46 +0000716
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000717llvm::Constant *BlockFunction::
718GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
Mike Stump67645932009-04-10 18:52:28 +0000719 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stump0c743272009-03-06 01:33:24 +0000720 QualType R = getContext().VoidTy;
721
722 FunctionArgList Args;
723 // FIXME: This leaks
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000724 ImplicitParamDecl *Dst =
725 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
726 getContext().getPointerType(getContext().VoidTy));
727 Args.push_back(std::make_pair(Dst, Dst->getType()));
Mike Stump0c743272009-03-06 01:33:24 +0000728 ImplicitParamDecl *Src =
729 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
730 getContext().getPointerType(getContext().VoidTy));
Mike Stump0c743272009-03-06 01:33:24 +0000731 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump11289f42009-09-09 15:08:12 +0000732
Mike Stump0c743272009-03-06 01:33:24 +0000733 const CGFunctionInfo &FI =
734 CGM.getTypes().getFunctionInfo(R, Args);
735
Mike Stumpcbc2bca2009-06-05 23:26:36 +0000736 // FIXME: We'd like to put these into a mergable by content, with
737 // internal linkage.
Mike Stump0c743272009-03-06 01:33:24 +0000738 std::string Name = std::string("__copy_helper_block_");
739 CodeGenTypes &Types = CGM.getTypes();
740 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
741
742 llvm::Function *Fn =
743 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
744 Name,
745 &CGM.getModule());
746
747 IdentifierInfo *II
748 = &CGM.getContext().Idents.get("__copy_helper_block_");
749
750 FunctionDecl *FD = FunctionDecl::Create(getContext(),
751 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000752 SourceLocation(), II, R, 0,
Mike Stump0c743272009-03-06 01:33:24 +0000753 FunctionDecl::Static, false,
754 true);
755 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000756
757 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
758 llvm::Type *PtrPtrT;
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000759
Mike Stump67645932009-04-10 18:52:28 +0000760 if (NoteForHelperp) {
761 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000762
Owen Anderson9793f0e2009-07-29 22:16:19 +0000763 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stump67645932009-04-10 18:52:28 +0000764 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
765 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000766
Mike Stump67645932009-04-10 18:52:28 +0000767 llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
Mike Stump3b65ac22009-04-15 22:11:36 +0000768 llvm::Type *PtrPtrT;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000769 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stump3b65ac22009-04-15 22:11:36 +0000770 DstObj = Builder.CreateBitCast(DstObj, PtrPtrT);
771 DstObj = Builder.CreateLoad(DstObj);
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000772
Mike Stump67645932009-04-10 18:52:28 +0000773 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
774 int flag = NoteForHelper[i].flag;
775 int index = NoteForHelper[i].index;
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000776
Mike Stump67645932009-04-10 18:52:28 +0000777 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
778 || NoteForHelper[i].RequiresCopying) {
779 llvm::Value *Srcv = SrcObj;
780 Srcv = Builder.CreateStructGEP(Srcv, index);
781 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson9793f0e2009-07-29 22:16:19 +0000782 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stump67645932009-04-10 18:52:28 +0000783 Srcv = Builder.CreateLoad(Srcv);
784
785 llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
786 Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
787
Owen Anderson41a75022009-08-13 21:57:51 +0000788 llvm::Value *N = llvm::ConstantInt::get(
789 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stump67645932009-04-10 18:52:28 +0000790 llvm::Value *F = getBlockObjectAssign();
791 Builder.CreateCall3(F, Dstv, Srcv, N);
792 }
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000793 }
794 }
795
Mike Stump0c743272009-03-06 01:33:24 +0000796 CGF.FinishFunction();
797
Owen Andersonade90fd2009-07-29 18:54:39 +0000798 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump97d01d52009-03-04 03:23:46 +0000799}
800
Mike Stumpd6ef62f2009-03-06 18:42:23 +0000801llvm::Constant *BlockFunction::
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000802GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
803 const llvm::StructType* T,
Mike Stump67645932009-04-10 18:52:28 +0000804 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stump0c743272009-03-06 01:33:24 +0000805 QualType R = getContext().VoidTy;
806
807 FunctionArgList Args;
808 // FIXME: This leaks
809 ImplicitParamDecl *Src =
810 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
811 getContext().getPointerType(getContext().VoidTy));
812
813 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump11289f42009-09-09 15:08:12 +0000814
Mike Stump0c743272009-03-06 01:33:24 +0000815 const CGFunctionInfo &FI =
816 CGM.getTypes().getFunctionInfo(R, Args);
817
Mike Stumpcbc2bca2009-06-05 23:26:36 +0000818 // FIXME: We'd like to put these into a mergable by content, with
819 // internal linkage.
Mike Stump0c743272009-03-06 01:33:24 +0000820 std::string Name = std::string("__destroy_helper_block_");
821 CodeGenTypes &Types = CGM.getTypes();
822 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
823
824 llvm::Function *Fn =
825 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
826 Name,
827 &CGM.getModule());
828
829 IdentifierInfo *II
830 = &CGM.getContext().Idents.get("__destroy_helper_block_");
831
832 FunctionDecl *FD = FunctionDecl::Create(getContext(),
833 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000834 SourceLocation(), II, R, 0,
Mike Stump0c743272009-03-06 01:33:24 +0000835 FunctionDecl::Static, false,
836 true);
837 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stump6f7d9f82009-03-07 02:53:18 +0000838
Mike Stump67645932009-04-10 18:52:28 +0000839 if (NoteForHelperp) {
840 std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
Mike Stump6f7d9f82009-03-07 02:53:18 +0000841
Mike Stump67645932009-04-10 18:52:28 +0000842 llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
843 llvm::Type *PtrPtrT;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000844 PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
Mike Stump67645932009-04-10 18:52:28 +0000845 SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
846 SrcObj = Builder.CreateLoad(SrcObj);
Mike Stump6f7d9f82009-03-07 02:53:18 +0000847
Mike Stump67645932009-04-10 18:52:28 +0000848 for (unsigned i=0; i < NoteForHelper.size(); ++i) {
849 int flag = NoteForHelper[i].flag;
850 int index = NoteForHelper[i].index;
Mike Stump6f7d9f82009-03-07 02:53:18 +0000851
Mike Stump67645932009-04-10 18:52:28 +0000852 if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
853 || NoteForHelper[i].RequiresCopying) {
854 llvm::Value *Srcv = SrcObj;
855 Srcv = Builder.CreateStructGEP(Srcv, index);
856 Srcv = Builder.CreateBitCast(Srcv,
Owen Anderson9793f0e2009-07-29 22:16:19 +0000857 llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stump67645932009-04-10 18:52:28 +0000858 Srcv = Builder.CreateLoad(Srcv);
859
860 BuildBlockRelease(Srcv, flag);
861 }
Mike Stump6f7d9f82009-03-07 02:53:18 +0000862 }
863 }
864
Mike Stump0c743272009-03-06 01:33:24 +0000865 CGF.FinishFunction();
866
Owen Andersonade90fd2009-07-29 18:54:39 +0000867 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stump0c743272009-03-06 01:33:24 +0000868}
869
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000870llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
Mike Stump67645932009-04-10 18:52:28 +0000871 std::vector<HelperInfo> *NoteForHelper) {
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000872 return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
873 T, NoteForHelper);
Mike Stump0c743272009-03-06 01:33:24 +0000874}
875
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000876llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
Mike Stump67645932009-04-10 18:52:28 +0000877 std::vector<HelperInfo> *NoteForHelperp) {
Mike Stumpaeb0ffd2009-03-07 02:35:30 +0000878 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
Mike Stump67645932009-04-10 18:52:28 +0000879 T, NoteForHelperp);
Mike Stump97d01d52009-03-04 03:23:46 +0000880}
Mike Stump626aecc2009-03-05 01:23:13 +0000881
Mike Stumpf89230d2009-03-06 06:12:24 +0000882llvm::Constant *BlockFunction::
883GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
Mike Stumpee2a5ee2009-03-06 02:29:21 +0000884 QualType R = getContext().VoidTy;
885
886 FunctionArgList Args;
887 // FIXME: This leaks
Mike Stumpf89230d2009-03-06 06:12:24 +0000888 ImplicitParamDecl *Dst =
889 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
890 getContext().getPointerType(getContext().VoidTy));
891 Args.push_back(std::make_pair(Dst, Dst->getType()));
892
893 // FIXME: This leaks
Mike Stumpee2a5ee2009-03-06 02:29:21 +0000894 ImplicitParamDecl *Src =
895 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
896 getContext().getPointerType(getContext().VoidTy));
Mike Stumpee2a5ee2009-03-06 02:29:21 +0000897 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump11289f42009-09-09 15:08:12 +0000898
Mike Stumpee2a5ee2009-03-06 02:29:21 +0000899 const CGFunctionInfo &FI =
900 CGM.getTypes().getFunctionInfo(R, Args);
901
902 std::string Name = std::string("__Block_byref_id_object_copy_");
903 CodeGenTypes &Types = CGM.getTypes();
904 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
905
Mike Stumpcbc2bca2009-06-05 23:26:36 +0000906 // FIXME: We'd like to put these into a mergable by content, with
907 // internal linkage.
Mike Stumpee2a5ee2009-03-06 02:29:21 +0000908 llvm::Function *Fn =
909 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
910 Name,
911 &CGM.getModule());
912
913 IdentifierInfo *II
914 = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
915
916 FunctionDecl *FD = FunctionDecl::Create(getContext(),
917 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000918 SourceLocation(), II, R, 0,
Mike Stumpee2a5ee2009-03-06 02:29:21 +0000919 FunctionDecl::Static, false,
920 true);
921 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpf89230d2009-03-06 06:12:24 +0000922
923 // dst->x
924 llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000925 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stump3b65ac22009-04-15 22:11:36 +0000926 V = Builder.CreateLoad(V);
Mike Stumpf89230d2009-03-06 06:12:24 +0000927 V = Builder.CreateStructGEP(V, 6, "x");
928 llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
929
930 // src->x
931 V = CGF.GetAddrOfLocalVar(Src);
932 V = Builder.CreateLoad(V);
933 V = Builder.CreateBitCast(V, T);
934 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson9793f0e2009-07-29 22:16:19 +0000935 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stumpf89230d2009-03-06 06:12:24 +0000936 llvm::Value *SrcObj = Builder.CreateLoad(V);
Mike Stump11289f42009-09-09 15:08:12 +0000937
Mike Stumpf89230d2009-03-06 06:12:24 +0000938 flag |= BLOCK_BYREF_CALLER;
939
Owen Anderson41a75022009-08-13 21:57:51 +0000940 llvm::Value *N = llvm::ConstantInt::get(
941 llvm::Type::getInt32Ty(T->getContext()), flag);
Mike Stumpf89230d2009-03-06 06:12:24 +0000942 llvm::Value *F = getBlockObjectAssign();
943 Builder.CreateCall3(F, DstObj, SrcObj, N);
944
Mike Stumpee2a5ee2009-03-06 02:29:21 +0000945 CGF.FinishFunction();
946
Owen Andersonade90fd2009-07-29 18:54:39 +0000947 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpee2a5ee2009-03-06 02:29:21 +0000948}
949
Mike Stumpfbe25dd2009-03-06 04:53:30 +0000950llvm::Constant *
951BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
952 int flag) {
Mike Stumpee2a5ee2009-03-06 02:29:21 +0000953 QualType R = getContext().VoidTy;
954
955 FunctionArgList Args;
956 // FIXME: This leaks
957 ImplicitParamDecl *Src =
958 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
959 getContext().getPointerType(getContext().VoidTy));
960
961 Args.push_back(std::make_pair(Src, Src->getType()));
Mike Stump11289f42009-09-09 15:08:12 +0000962
Mike Stumpee2a5ee2009-03-06 02:29:21 +0000963 const CGFunctionInfo &FI =
964 CGM.getTypes().getFunctionInfo(R, Args);
965
966 std::string Name = std::string("__Block_byref_id_object_dispose_");
967 CodeGenTypes &Types = CGM.getTypes();
968 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
969
Mike Stumpcbc2bca2009-06-05 23:26:36 +0000970 // FIXME: We'd like to put these into a mergable by content, with
971 // internal linkage.
Mike Stumpee2a5ee2009-03-06 02:29:21 +0000972 llvm::Function *Fn =
973 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
974 Name,
975 &CGM.getModule());
976
977 IdentifierInfo *II
978 = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
979
980 FunctionDecl *FD = FunctionDecl::Create(getContext(),
981 getContext().getTranslationUnitDecl(),
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000982 SourceLocation(), II, R, 0,
Mike Stumpee2a5ee2009-03-06 02:29:21 +0000983 FunctionDecl::Static, false,
984 true);
985 CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
Mike Stumpfbe25dd2009-03-06 04:53:30 +0000986
987 llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000988 V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
Mike Stump3b65ac22009-04-15 22:11:36 +0000989 V = Builder.CreateLoad(V);
Mike Stumpfbe25dd2009-03-06 04:53:30 +0000990 V = Builder.CreateStructGEP(V, 6, "x");
Owen Anderson9793f0e2009-07-29 22:16:19 +0000991 V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
Mike Stump3b65ac22009-04-15 22:11:36 +0000992 V = Builder.CreateLoad(V);
Mike Stumpfbe25dd2009-03-06 04:53:30 +0000993
Mike Stumpfbe25dd2009-03-06 04:53:30 +0000994 flag |= BLOCK_BYREF_CALLER;
995 BuildBlockRelease(V, flag);
Mike Stumpee2a5ee2009-03-06 02:29:21 +0000996 CGF.FinishFunction();
997
Owen Andersonade90fd2009-07-29 18:54:39 +0000998 return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
Mike Stumpee2a5ee2009-03-06 02:29:21 +0000999}
1000
Mike Stumpf89230d2009-03-06 06:12:24 +00001001llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
Mike Stumpcbc2bca2009-06-05 23:26:36 +00001002 int flag, unsigned Align) {
1003 // All alignments below that of pointer alignment collpase down to just
1004 // pointer alignment, as we always have at least that much alignment to begin
1005 // with.
1006 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
1007 // As an optimization, we only generate a single function of each kind we
1008 // might need. We need a different one for each alignment and for each
1009 // setting of flags. We mix Align and flag to get the kind.
1010 uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag;
1011 llvm::Constant *& Entry = CGM.AssignCache[kind];
1012 if (Entry)
1013 return Entry;
1014 return Entry=CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag);
Mike Stumpee2a5ee2009-03-06 02:29:21 +00001015}
1016
Mike Stumpfbe25dd2009-03-06 04:53:30 +00001017llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
Mike Stumpcbc2bca2009-06-05 23:26:36 +00001018 int flag,
1019 unsigned Align) {
1020 // All alignments below that of pointer alignment collpase down to just
1021 // pointer alignment, as we always have at least that much alignment to begin
1022 // with.
1023 Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
1024 // As an optimization, we only generate a single function of each kind we
1025 // might need. We need a different one for each alignment and for each
1026 // setting of flags. We mix Align and flag to get the kind.
1027 uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag;
1028 llvm::Constant *& Entry = CGM.DestroyCache[kind];
1029 if (Entry)
1030 return Entry;
1031 return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag);
Mike Stumpee2a5ee2009-03-06 02:29:21 +00001032}
1033
Mike Stump626aecc2009-03-05 01:23:13 +00001034llvm::Value *BlockFunction::getBlockObjectDispose() {
1035 if (CGM.BlockObjectDispose == 0) {
1036 const llvm::FunctionType *FTy;
1037 std::vector<const llvm::Type*> ArgTys;
Owen Anderson41a75022009-08-13 21:57:51 +00001038 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stump626aecc2009-03-05 01:23:13 +00001039 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson41a75022009-08-13 21:57:51 +00001040 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson9793f0e2009-07-29 22:16:19 +00001041 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stump4446dcf2009-03-05 08:32:30 +00001042 CGM.BlockObjectDispose
Mike Stump626aecc2009-03-05 01:23:13 +00001043 = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
1044 }
1045 return CGM.BlockObjectDispose;
1046}
1047
Mike Stumpf89230d2009-03-06 06:12:24 +00001048llvm::Value *BlockFunction::getBlockObjectAssign() {
1049 if (CGM.BlockObjectAssign == 0) {
1050 const llvm::FunctionType *FTy;
1051 std::vector<const llvm::Type*> ArgTys;
Owen Anderson41a75022009-08-13 21:57:51 +00001052 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
Mike Stumpf89230d2009-03-06 06:12:24 +00001053 ArgTys.push_back(PtrToInt8Ty);
1054 ArgTys.push_back(PtrToInt8Ty);
Owen Anderson41a75022009-08-13 21:57:51 +00001055 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
Owen Anderson9793f0e2009-07-29 22:16:19 +00001056 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
Mike Stumpf89230d2009-03-06 06:12:24 +00001057 CGM.BlockObjectAssign
1058 = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
1059 }
1060 return CGM.BlockObjectAssign;
1061}
1062
Mike Stumpfbe25dd2009-03-06 04:53:30 +00001063void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
Mike Stump626aecc2009-03-05 01:23:13 +00001064 llvm::Value *F = getBlockObjectDispose();
Mike Stumpfbe25dd2009-03-06 04:53:30 +00001065 llvm::Value *N;
Mike Stump626aecc2009-03-05 01:23:13 +00001066 V = Builder.CreateBitCast(V, PtrToInt8Ty);
Owen Anderson41a75022009-08-13 21:57:51 +00001067 N = llvm::ConstantInt::get(llvm::Type::getInt32Ty(V->getContext()), flag);
Mike Stump626aecc2009-03-05 01:23:13 +00001068 Builder.CreateCall2(F, V, N);
1069}
Mike Stump4446dcf2009-03-05 08:32:30 +00001070
1071ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
Mike Stumpaeb0ffd2009-03-07 02:35:30 +00001072
1073BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1074 CGBuilderTy &B)
Owen Anderson170229f2009-07-14 23:10:40 +00001075 : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) {
Owen Anderson41a75022009-08-13 21:57:51 +00001076 PtrToInt8Ty = llvm::PointerType::getUnqual(
1077 llvm::Type::getInt8Ty(VMContext));
Mike Stumpaeb0ffd2009-03-07 02:35:30 +00001078
1079 BlockHasCopyDispose = false;
1080}