blob: ee17a862b16145496279ee23d72048f999d1e846 [file] [log] [blame]
Anders Carlssond2a889b2009-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"
16#include "llvm/Module.h"
Anders Carlsson1f1cd392009-02-12 17:55:02 +000017#include "llvm/Target/TargetData.h"
Anders Carlssond2a889b2009-02-12 00:39:25 +000018
19#include <algorithm>
20
21using namespace clang;
22using namespace CodeGen;
23
Anders Carlsson1f1cd392009-02-12 17:55:02 +000024enum {
Mike Stumpb95bc002009-02-13 16:19:19 +000025 BLOCK_NEEDS_FREE = (1 << 24),
26 BLOCK_HAS_COPY_DISPOSE = (1 << 25),
27 BLOCK_HAS_CXX_OBJ = (1 << 26),
28 BLOCK_IS_GC = (1 << 27),
29 BLOCK_IS_GLOBAL = (1 << 28),
30 BLOCK_HAS_DESCRIPTOR = (1 << 29)
Anders Carlsson1f1cd392009-02-12 17:55:02 +000031};
32
Mike Stumpfca5da02009-02-21 20:00:35 +000033llvm::Constant *CodeGenFunction::BuildDescriptorBlockDecl(uint64_t Size) {
Mike Stumpb95bc002009-02-13 16:19:19 +000034 const llvm::PointerType *PtrToInt8Ty
35 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpff8f0872009-02-13 16:55:51 +000036 const llvm::Type *UnsignedLongTy
37 = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpb95bc002009-02-13 16:19:19 +000038 llvm::Constant *C;
39 std::vector<llvm::Constant*> Elts;
40
41 // reserved
Mike Stumpff8f0872009-02-13 16:55:51 +000042 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpb95bc002009-02-13 16:19:19 +000043 Elts.push_back(C);
44
45 // Size
Mike Stump139c3962009-02-21 20:07:44 +000046 // FIXME: What is the right way to say this doesn't fit? We should give
47 // a user diagnostic in that case. Better fix would be to change the
48 // API to size_t.
Mike Stumpfca5da02009-02-21 20:00:35 +000049 C = llvm::ConstantInt::get(UnsignedLongTy, Size);
Mike Stumpb95bc002009-02-13 16:19:19 +000050 Elts.push_back(C);
51
52 if (BlockHasCopyDispose) {
53 // copy_func_helper_decl
Mike Stumpfca5da02009-02-21 20:00:35 +000054 // FIXME: implement
Mike Stumpff8f0872009-02-13 16:55:51 +000055 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpb95bc002009-02-13 16:19:19 +000056 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
57 Elts.push_back(C);
58
59 // destroy_func_decl
Mike Stumpfca5da02009-02-21 20:00:35 +000060 // FIXME: implement
Mike Stumpff8f0872009-02-13 16:55:51 +000061 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpb95bc002009-02-13 16:19:19 +000062 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
63 Elts.push_back(C);
64 }
65
66 C = llvm::ConstantStruct::get(Elts);
67
Mike Stumpb95bc002009-02-13 16:19:19 +000068 C = new llvm::GlobalVariable(C->getType(), true,
69 llvm::GlobalValue::InternalLinkage,
Mike Stump92ea8882009-02-13 20:17:16 +000070 C, "__block_descriptor_tmp", &CGM.getModule());
Mike Stumpb95bc002009-02-13 16:19:19 +000071 return C;
72}
73
Mike Stump5f87e9d2009-02-13 17:23:42 +000074llvm::Constant *CodeGenModule::getNSConcreteGlobalBlock() {
75 if (NSConcreteGlobalBlock)
76 return NSConcreteGlobalBlock;
77
78 const llvm::PointerType *PtrToInt8Ty
79 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stump56447902009-02-13 19:38:12 +000080 // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
Mike Stump5f87e9d2009-02-13 17:23:42 +000081 // same thing as CreateRuntimeFunction if there's already a variable with
82 // the same name.
83 NSConcreteGlobalBlock
84 = new llvm::GlobalVariable(PtrToInt8Ty, false,
85 llvm::GlobalValue::ExternalLinkage,
86 0, "_NSConcreteGlobalBlock",
87 &getModule());
88
89 return NSConcreteGlobalBlock;
90}
91
Mike Stumpeb3a5ca2009-02-13 19:29:27 +000092llvm::Constant *CodeGenModule::getNSConcreteStackBlock() {
93 if (NSConcreteStackBlock)
94 return NSConcreteStackBlock;
95
96 const llvm::PointerType *PtrToInt8Ty
97 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stump56447902009-02-13 19:38:12 +000098 // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
Mike Stumpeb3a5ca2009-02-13 19:29:27 +000099 // same thing as CreateRuntimeFunction if there's already a variable with
100 // the same name.
101 NSConcreteStackBlock
102 = new llvm::GlobalVariable(PtrToInt8Ty, false,
103 llvm::GlobalValue::ExternalLinkage,
104 0, "_NSConcreteStackBlock",
105 &getModule());
106
107 return NSConcreteStackBlock;
108}
109
Anders Carlsson6cf64be2009-03-01 01:09:12 +0000110static void CollectBlockDeclRefInfo(const Stmt *S,
111 CodeGenFunction::BlockInfo &Info) {
112 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
113 I != E; ++I)
Daniel Dunbar56941d32009-03-02 07:00:57 +0000114 if (*I)
115 CollectBlockDeclRefInfo(*I, Info);
Anders Carlsson6cf64be2009-03-01 01:09:12 +0000116
117 if (const BlockDeclRefExpr *DE = dyn_cast<BlockDeclRefExpr>(S)) {
118 // FIXME: Handle enums.
119 if (isa<FunctionDecl>(DE->getDecl()))
120 return;
121
122 if (DE->isByRef())
123 Info.ByRefDeclRefs.push_back(DE);
124 else
125 Info.ByCopyDeclRefs.push_back(DE);
126 }
127}
128
129/// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block
130/// can be declared as a global variable instead of on the stack.
131static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info)
132{
133 return Info.ByRefDeclRefs.empty() && Info.ByCopyDeclRefs.empty();
134}
135
Mike Stumpd55240e2009-02-19 01:01:04 +0000136// FIXME: Push most into CGM, passing down a few bits, like current
137// function name.
Mike Stumpf1711822009-02-25 23:33:13 +0000138llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Mike Stumpb95bc002009-02-13 16:19:19 +0000139
Anders Carlsson6cf64be2009-03-01 01:09:12 +0000140 std::string Name = CurFn->getName();
141 CodeGenFunction::BlockInfo Info(0, Name.c_str());
142 CollectBlockDeclRefInfo(BE->getBody(), Info);
143
144 // Check if the block can be global.
145 if (CanBlockBeGlobal(Info))
146 return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
147
Mike Stumpb95bc002009-02-13 16:19:19 +0000148 std::vector<llvm::Constant*> Elts;
149 llvm::Constant *C;
Mike Stumpf1711822009-02-25 23:33:13 +0000150 llvm::Value *V;
Mike Stumpb95bc002009-02-13 16:19:19 +0000151
Mike Stumpb95bc002009-02-13 16:19:19 +0000152 {
153 // C = BuildBlockStructInitlist();
154 unsigned int flags = BLOCK_HAS_DESCRIPTOR;
155
156 if (BlockHasCopyDispose)
157 flags |= BLOCK_HAS_COPY_DISPOSE;
158
Mike Stump92ea8882009-02-13 20:17:16 +0000159 // __isa
Mike Stumpeb3a5ca2009-02-13 19:29:27 +0000160 C = CGM.getNSConcreteStackBlock();
Mike Stumpeb3a5ca2009-02-13 19:29:27 +0000161 const llvm::PointerType *PtrToInt8Ty
162 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpb95bc002009-02-13 16:19:19 +0000163 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
164 Elts.push_back(C);
165
166 // __flags
167 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
168 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
169 C = llvm::ConstantInt::get(IntTy, flags);
170 Elts.push_back(C);
171
172 // __reserved
173 C = llvm::ConstantInt::get(IntTy, 0);
174 Elts.push_back(C);
175
Mike Stumpd55240e2009-02-19 01:01:04 +0000176 // __invoke
Mike Stumpf1711822009-02-25 23:33:13 +0000177 uint64_t subBlockSize, subBlockAlign;
Mike Stump2b6933f2009-02-28 09:07:16 +0000178 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Mike Stumpfca5da02009-02-21 20:00:35 +0000179 llvm::Function *Fn
Mike Stumpf1711822009-02-25 23:33:13 +0000180 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, subBlockSize,
181 subBlockAlign, subBlockDeclRefDecls);
Mike Stump084ba462009-02-14 22:16:35 +0000182 Elts.push_back(Fn);
Mike Stumpb95bc002009-02-13 16:19:19 +0000183
184 // __descriptor
Mike Stumpfca5da02009-02-21 20:00:35 +0000185 Elts.push_back(BuildDescriptorBlockDecl(subBlockSize));
Mike Stumpb95bc002009-02-13 16:19:19 +0000186
Mike Stumpf1711822009-02-25 23:33:13 +0000187 if (subBlockDeclRefDecls.size() == 0) {
Mike Stumpa7db9be2009-03-01 20:07:53 +0000188 // Optimize to being a global block.
189 Elts[0] = CGM.getNSConcreteGlobalBlock();
190 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
191
Mike Stumpf1711822009-02-25 23:33:13 +0000192 C = llvm::ConstantStruct::get(Elts);
193
194 char Name[32];
195 sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
196 C = new llvm::GlobalVariable(C->getType(), true,
197 llvm::GlobalValue::InternalLinkage,
198 C, Name, &CGM.getModule());
199 QualType BPT = BE->getType();
200 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
201 return C;
202 }
203
204 std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size());
205 for (int i=0; i<5; ++i)
206 Types[i] = Elts[i]->getType();
207
Mike Stump2b6933f2009-02-28 09:07:16 +0000208 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
209 const Expr *E = subBlockDeclRefDecls[i];
210 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
211 QualType Ty = E->getType();
212 if (BDRE && BDRE->isByRef())
213 Ty = getContext().getPointerType(Ty);
214 Types[i+5] = ConvertType(Ty);
215 }
Mike Stumpf1711822009-02-25 23:33:13 +0000216
217 llvm::Type *Ty = llvm::StructType::get(Types, true);
218
219 llvm::AllocaInst *A = CreateTempAlloca(Ty);
220 A->setAlignment(subBlockAlign);
221 V = A;
222
223 for (unsigned i=0; i<5; ++i)
224 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
225
226 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
227 {
Mike Stump2b6933f2009-02-28 09:07:16 +0000228 // FIXME: Push const down.
229 Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
230 DeclRefExpr *DR;
231 ValueDecl *VD;
Mike Stumpf1711822009-02-25 23:33:13 +0000232
Mike Stump2b6933f2009-02-28 09:07:16 +0000233 DR = dyn_cast<DeclRefExpr>(E);
234 // Skip padding.
235 if (DR) continue;
236
237 BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
238 VD = BDRE->getDecl();
239
240 // FIXME: I want a better way to do this.
241 if (LocalDeclMap[VD]) {
242 E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD),
243 VD->getType(), SourceLocation(),
244 false, false);
245 }
246 if (BDRE->isByRef())
247 E = new (getContext())
248 UnaryOperator(E, UnaryOperator::AddrOf,
249 getContext().getPointerType(E->getType()),
250 SourceLocation());
251
Mike Stumpf1711822009-02-25 23:33:13 +0000252 llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp");
Mike Stump2b6933f2009-02-28 09:07:16 +0000253 RValue r = EmitAnyExpr(E, Addr, false);
Mike Stumpf1711822009-02-25 23:33:13 +0000254 if (r.isScalar())
255 Builder.CreateStore(r.getScalarVal(), Addr);
256 else if (r.isComplex())
257 // FIXME: implement
258 ErrorUnsupported(BE, "complex in block literal");
259 else if (r.isAggregate())
260 ; // Already created into the destination
261 else
262 assert (0 && "bad block variable");
263 // FIXME: Ensure that the offset created by the backend for
264 // the struct matches the previously computed offset in BlockDecls.
265 }
Mike Stumpb95bc002009-02-13 16:19:19 +0000266 }
267
Mike Stumpd55240e2009-02-19 01:01:04 +0000268 QualType BPT = BE->getType();
Mike Stumpf1711822009-02-25 23:33:13 +0000269 return Builder.CreateBitCast(V, ConvertType(BPT));
Mike Stumpb95bc002009-02-13 16:19:19 +0000270}
271
272
Mike Stump95e54802009-02-13 15:16:56 +0000273const llvm::Type *CodeGenModule::getBlockDescriptorType() {
274 if (BlockDescriptorType)
275 return BlockDescriptorType;
276
Mike Stumpc4ae9632009-02-13 15:32:32 +0000277 const llvm::Type *UnsignedLongTy =
Mike Stump95e54802009-02-13 15:16:56 +0000278 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000279
Mike Stump95e54802009-02-13 15:16:56 +0000280 // struct __block_descriptor {
281 // unsigned long reserved;
282 // unsigned long block_size;
283 // };
Mike Stumpc4ae9632009-02-13 15:32:32 +0000284 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy,
285 UnsignedLongTy,
Mike Stump95e54802009-02-13 15:16:56 +0000286 NULL);
287
288 getModule().addTypeName("struct.__block_descriptor",
289 BlockDescriptorType);
290
291 return BlockDescriptorType;
Anders Carlssond2a889b2009-02-12 00:39:25 +0000292}
293
Mike Stump0dffa462009-02-13 15:25:34 +0000294const llvm::Type *
295CodeGenModule::getGenericBlockLiteralType() {
296 if (GenericBlockLiteralType)
297 return GenericBlockLiteralType;
298
Mike Stumpc4ae9632009-02-13 15:32:32 +0000299 const llvm::Type *Int8PtrTy =
Mike Stump0dffa462009-02-13 15:25:34 +0000300 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000301
302 const llvm::Type *BlockDescPtrTy =
Mike Stump0dffa462009-02-13 15:25:34 +0000303 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000304
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000305 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
306 getTypes().ConvertType(getContext().IntTy));
307
Mike Stump0dffa462009-02-13 15:25:34 +0000308 // struct __block_literal_generic {
Mike Stumpd55240e2009-02-19 01:01:04 +0000309 // void *__isa;
310 // int __flags;
311 // int __reserved;
312 // void (*__invoke)(void *);
313 // struct __block_descriptor *__descriptor;
Mike Stump0dffa462009-02-13 15:25:34 +0000314 // };
315 GenericBlockLiteralType = llvm::StructType::get(Int8PtrTy,
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000316 IntTy,
317 IntTy,
Mike Stump0dffa462009-02-13 15:25:34 +0000318 Int8PtrTy,
319 BlockDescPtrTy,
320 NULL);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000321
Mike Stump0dffa462009-02-13 15:25:34 +0000322 getModule().addTypeName("struct.__block_literal_generic",
323 GenericBlockLiteralType);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000324
Mike Stump0dffa462009-02-13 15:25:34 +0000325 return GenericBlockLiteralType;
Anders Carlssond2a889b2009-02-12 00:39:25 +0000326}
327
Mike Stumpd55240e2009-02-19 01:01:04 +0000328const llvm::Type *
329CodeGenModule::getGenericExtendedBlockLiteralType() {
330 if (GenericExtendedBlockLiteralType)
331 return GenericExtendedBlockLiteralType;
332
333 const llvm::Type *Int8PtrTy =
334 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
335
336 const llvm::Type *BlockDescPtrTy =
337 llvm::PointerType::getUnqual(getBlockDescriptorType());
338
339 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
340 getTypes().ConvertType(getContext().IntTy));
341
342 // struct __block_literal_generic {
343 // void *__isa;
344 // int __flags;
345 // int __reserved;
346 // void (*__invoke)(void *);
347 // struct __block_descriptor *__descriptor;
348 // void *__copy_func_helper_decl;
349 // void *__destroy_func_decl;
350 // };
351 GenericExtendedBlockLiteralType = llvm::StructType::get(Int8PtrTy,
352 IntTy,
353 IntTy,
354 Int8PtrTy,
355 BlockDescPtrTy,
356 Int8PtrTy,
357 Int8PtrTy,
358 NULL);
359
360 getModule().addTypeName("struct.__block_literal_extended_generic",
361 GenericExtendedBlockLiteralType);
362
363 return GenericExtendedBlockLiteralType;
364}
365
Mike Stumpc4ae9632009-02-13 15:32:32 +0000366/// getBlockFunctionType - Given a BlockPointerType, will return the
Anders Carlssond2a889b2009-02-12 00:39:25 +0000367/// function type for the block, including the first block literal argument.
368static QualType getBlockFunctionType(ASTContext &Ctx,
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000369 const BlockPointerType *BPT) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000370 const FunctionProtoType *FTy = cast<FunctionProtoType>(BPT->getPointeeType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000371
Anders Carlssond2a889b2009-02-12 00:39:25 +0000372 llvm::SmallVector<QualType, 8> Types;
373 Types.push_back(Ctx.getPointerType(Ctx.VoidTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000374
Douglas Gregor4fa58902009-02-26 23:50:07 +0000375 for (FunctionProtoType::arg_type_iterator i = FTy->arg_type_begin(),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000376 e = FTy->arg_type_end(); i != e; ++i)
377 Types.push_back(*i);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000378
Anders Carlssond2a889b2009-02-12 00:39:25 +0000379 return Ctx.getFunctionType(FTy->getResultType(),
Mike Stumpc4ae9632009-02-13 15:32:32 +0000380 &Types[0], Types.size(),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000381 FTy->isVariadic(), 0);
382}
383
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000384RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpc4ae9632009-02-13 15:32:32 +0000385 const BlockPointerType *BPT =
Anders Carlssond2a889b2009-02-12 00:39:25 +0000386 E->getCallee()->getType()->getAsBlockPointerType();
Mike Stumpc4ae9632009-02-13 15:32:32 +0000387
Anders Carlssond2a889b2009-02-12 00:39:25 +0000388 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
389
390 // Get a pointer to the generic block literal.
391 const llvm::Type *BlockLiteralTy =
Mike Stump0dffa462009-02-13 15:25:34 +0000392 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssond2a889b2009-02-12 00:39:25 +0000393
394 // Bitcast the callee to a block literal.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000395 llvm::Value *BlockLiteral =
Anders Carlssond2a889b2009-02-12 00:39:25 +0000396 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
397
398 // Get the function pointer from the literal.
399 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Mike Stump39bcc612009-02-22 13:27:11 +0000400 llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp");
Anders Carlssond2a889b2009-02-12 00:39:25 +0000401
402 // Cast the function pointer to the right type.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000403 const llvm::Type *BlockFTy =
Anders Carlssond2a889b2009-02-12 00:39:25 +0000404 ConvertType(getBlockFunctionType(getContext(), BPT));
405 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
406 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
407
Mike Stumpc4ae9632009-02-13 15:32:32 +0000408 BlockLiteral =
409 Builder.CreateBitCast(BlockLiteral,
Anders Carlssond2a889b2009-02-12 00:39:25 +0000410 llvm::PointerType::getUnqual(llvm::Type::Int8Ty),
411 "tmp");
Mike Stumpc4ae9632009-02-13 15:32:32 +0000412
Anders Carlssond2a889b2009-02-12 00:39:25 +0000413 // Add the block literal.
414 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
415 CallArgList Args;
416 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000417
Anders Carlssond2a889b2009-02-12 00:39:25 +0000418 // And the rest of the arguments.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000419 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
Anders Carlssond2a889b2009-02-12 00:39:25 +0000420 i != e; ++i)
Mike Stumpc4ae9632009-02-13 15:32:32 +0000421 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000422 i->getType()));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000423
Anders Carlssond2a889b2009-02-12 00:39:25 +0000424 // And call the block.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000425 return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000426 Func, Args);
427}
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000428
Mike Stump084ba462009-02-14 22:16:35 +0000429llvm::Constant *
Mike Stump4b55c7f2009-02-14 22:49:33 +0000430CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000431 // Generate the block descriptor.
432 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000433 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
434 getTypes().ConvertType(getContext().IntTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000435
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000436 llvm::Constant *DescriptorFields[2];
Mike Stumpc4ae9632009-02-13 15:32:32 +0000437
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000438 // Reserved
439 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000440
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000441 // Block literal size. For global blocks we just use the size of the generic
442 // block literal struct.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000443 uint64_t BlockLiteralSize =
Mike Stump0dffa462009-02-13 15:25:34 +0000444 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000445 DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000446
447 llvm::Constant *DescriptorStruct =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000448 llvm::ConstantStruct::get(&DescriptorFields[0], 2);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000449
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000450 llvm::GlobalVariable *Descriptor =
451 new llvm::GlobalVariable(DescriptorStruct->getType(), true,
Mike Stumpc4ae9632009-02-13 15:32:32 +0000452 llvm::GlobalVariable::InternalLinkage,
453 DescriptorStruct, "__block_descriptor_global",
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000454 &getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000455
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000456 // Generate the constants for the block literal.
457 llvm::Constant *LiteralFields[5];
Mike Stumpc4ae9632009-02-13 15:32:32 +0000458
Mike Stump084ba462009-02-14 22:16:35 +0000459 CodeGenFunction::BlockInfo Info(0, n);
Mike Stumpf1711822009-02-25 23:33:13 +0000460 uint64_t subBlockSize, subBlockAlign;
Mike Stump2b6933f2009-02-28 09:07:16 +0000461 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Mike Stumpfca5da02009-02-21 20:00:35 +0000462 llvm::Function *Fn
Mike Stumpf1711822009-02-25 23:33:13 +0000463 = CodeGenFunction(*this).GenerateBlockFunction(BE, Info, subBlockSize,
464 subBlockAlign,
465 subBlockDeclRefDecls);
Mike Stumpfca5da02009-02-21 20:00:35 +0000466 assert(subBlockSize == BlockLiteralSize
467 && "no imports allowed for global block");
Mike Stumpc4ae9632009-02-13 15:32:32 +0000468
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000469 // isa
Mike Stump5f87e9d2009-02-13 17:23:42 +0000470 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpc4ae9632009-02-13 15:32:32 +0000471
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000472 // Flags
Anders Carlsson63810c62009-03-01 21:09:29 +0000473 LiteralFields[1] =
474 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000475
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000476 // Reserved
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000477 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000478
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000479 // Function
480 LiteralFields[3] = Fn;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000481
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000482 // Descriptor
483 LiteralFields[4] = Descriptor;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000484
485 llvm::Constant *BlockLiteralStruct =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000486 llvm::ConstantStruct::get(&LiteralFields[0], 5);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000487
488 llvm::GlobalVariable *BlockLiteral =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000489 new llvm::GlobalVariable(BlockLiteralStruct->getType(), true,
Mike Stumpc4ae9632009-02-13 15:32:32 +0000490 llvm::GlobalVariable::InternalLinkage,
491 BlockLiteralStruct, "__block_literal_global",
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000492 &getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000493
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000494 return BlockLiteral;
495}
496
Mike Stumpfca5da02009-02-21 20:00:35 +0000497llvm::Value *CodeGenFunction::LoadBlockStruct() {
498 return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self");
499}
500
Chris Lattner8130e7f2009-02-28 19:01:03 +0000501llvm::Function *CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
Mike Stumpfca5da02009-02-21 20:00:35 +0000502 const BlockInfo& Info,
Mike Stumpf1711822009-02-25 23:33:13 +0000503 uint64_t &Size,
504 uint64_t &Align,
Mike Stump2b6933f2009-02-28 09:07:16 +0000505 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000506 const FunctionProtoType *FTy =
Chris Lattner8130e7f2009-02-28 19:01:03 +0000507 cast<FunctionProtoType>(BExpr->getFunctionType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000508
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000509 FunctionArgList Args;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000510
Chris Lattner8130e7f2009-02-28 19:01:03 +0000511 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000512
513 // FIXME: This leaks
Mike Stumpc4ae9632009-02-13 15:32:32 +0000514 ImplicitParamDecl *SelfDecl =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000515 ImplicitParamDecl::Create(getContext(), 0,
516 SourceLocation(), 0,
517 getContext().getPointerType(getContext().VoidTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000518
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000519 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stumpfca5da02009-02-21 20:00:35 +0000520 BlockStructDecl = SelfDecl;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000521
522 for (BlockDecl::param_iterator i = BD->param_begin(),
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000523 e = BD->param_end(); i != e; ++i)
Mike Stump459207f2009-02-17 23:25:52 +0000524 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000525
526 const CGFunctionInfo &FI =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000527 CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args);
528
Mike Stump084ba462009-02-14 22:16:35 +0000529 std::string Name = std::string("__") + Info.Name + "_block_invoke_";
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000530 CodeGenTypes &Types = CGM.getTypes();
531 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000532
533 llvm::Function *Fn =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000534 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
535 Name,
536 &CGM.getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000537
538 StartFunction(BD, FTy->getResultType(), Fn, Args,
Chris Lattner8130e7f2009-02-28 19:01:03 +0000539 BExpr->getBody()->getLocEnd());
540 EmitStmt(BExpr->getBody());
541 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000542
Mike Stumpf1711822009-02-25 23:33:13 +0000543 // The runtime needs a minimum alignment of a void *.
544 uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
545 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign);
546
Mike Stumpfca5da02009-02-21 20:00:35 +0000547 Size = BlockOffset;
Mike Stumpf1711822009-02-25 23:33:13 +0000548 Align = BlockAlign;
549 subBlockDeclRefDecls = BlockDeclRefDecls;
Mike Stumpfca5da02009-02-21 20:00:35 +0000550
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000551 return Fn;
552}
Mike Stump2b6933f2009-02-28 09:07:16 +0000553
554uint64_t CodeGenFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
555 const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
556
557 uint64_t Size = getContext().getTypeSize(D->getType()) / 8;
558 uint64_t Align = getContext().getDeclAlignInBytes(D);
559
560 if (BDRE->isByRef()) {
561 Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8;
562 Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
563 }
564
565 assert ((Align > 0) && "alignment must be 1 byte or more");
566
567 uint64_t OldOffset = BlockOffset;
568
569 // Ensure proper alignment, even if it means we have to have a gap
570 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align);
571 BlockAlign = std::max(Align, BlockAlign);
572
573 uint64_t Pad = BlockOffset - OldOffset;
574 if (Pad) {
575 llvm::ArrayType::get(llvm::Type::Int8Ty, Pad);
576 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
577 llvm::APInt(32, Pad),
578 ArrayType::Normal, 0);
579 ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
580 0, QualType(PadTy), VarDecl::None,
581 SourceLocation());
582 Expr *E;
583 E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
584 SourceLocation(), false, false);
585 BlockDeclRefDecls.push_back(E);
586 }
587 BlockDeclRefDecls.push_back(BDRE);
588
589 BlockOffset += Size;
590 return BlockOffset-Size;
591}