blob: 703615a500bc7f3fa3c43faddda1fbc7b5927a68 [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)
114 CollectBlockDeclRefInfo(*I, Info);
115
116 if (const BlockDeclRefExpr *DE = dyn_cast<BlockDeclRefExpr>(S)) {
117 // FIXME: Handle enums.
118 if (isa<FunctionDecl>(DE->getDecl()))
119 return;
120
121 if (DE->isByRef())
122 Info.ByRefDeclRefs.push_back(DE);
123 else
124 Info.ByCopyDeclRefs.push_back(DE);
125 }
126}
127
128/// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block
129/// can be declared as a global variable instead of on the stack.
130static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info)
131{
132 return Info.ByRefDeclRefs.empty() && Info.ByCopyDeclRefs.empty();
133}
134
Anders Carlssona1363ff2009-03-01 01:45:25 +0000135/// CanGenerateCodeForBlockExpr - Returns whether CodeGen for the block expr
136/// is supported. Will emit a diagnostic and return false if not.
137/// FIXME: Once we support everything this should of course be removed.
138static bool CanGenerateCodeForBlockExpr(CodeGenFunction &CGF,
139 const BlockExpr* BE,
140 const CodeGenFunction::BlockInfo &Info)
141{
142 if (!Info.ByRefDeclRefs.empty()) {
143 CGF.ErrorUnsupported(BE, "block expression with __block variables");
144 return false;
145 }
146
147 for (size_t I = 0, E = Info.ByCopyDeclRefs.size(); I != E; ++I) {
148 const BlockDeclRefExpr *E = Info.ByCopyDeclRefs[I];
149
Anders Carlssona1363ff2009-03-01 01:45:25 +0000150 if (E->getType()->isBlockPointerType()) {
151 CGF.ErrorUnsupported(BE, "block expression with imported block");
152 return false;
153 }
154
155 if (E->getDecl()->getAttr<ObjCNSObjectAttr>() ||
156 CGF.getContext().isObjCNSObjectType(E->getType())) {
157 CGF.ErrorUnsupported(BE, "block expression with __attribute__((NSObject))"
158 " variable");
159 return false;
160 }
161
162 if (CGF.getContext().isObjCObjectPointerType(E->getType())) {
163 CGF.ErrorUnsupported(BE, "block expression with Objective-C variable");
164 return false;
165 }
166 }
167
168 return true;
169}
170
Mike Stumpd55240e2009-02-19 01:01:04 +0000171// FIXME: Push most into CGM, passing down a few bits, like current
172// function name.
Mike Stumpf1711822009-02-25 23:33:13 +0000173llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Mike Stumpb95bc002009-02-13 16:19:19 +0000174
Anders Carlsson6cf64be2009-03-01 01:09:12 +0000175 std::string Name = CurFn->getName();
176 CodeGenFunction::BlockInfo Info(0, Name.c_str());
177 CollectBlockDeclRefInfo(BE->getBody(), Info);
178
179 // Check if the block can be global.
180 if (CanBlockBeGlobal(Info))
181 return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
182
Anders Carlssona1363ff2009-03-01 01:45:25 +0000183 if (!CanGenerateCodeForBlockExpr(*this, BE, Info))
184 return llvm::UndefValue::get(ConvertType(BE->getType()));
185
Mike Stumpb95bc002009-02-13 16:19:19 +0000186 std::vector<llvm::Constant*> Elts;
187 llvm::Constant *C;
Mike Stumpf1711822009-02-25 23:33:13 +0000188 llvm::Value *V;
Mike Stumpb95bc002009-02-13 16:19:19 +0000189
Mike Stumpb95bc002009-02-13 16:19:19 +0000190 {
191 // C = BuildBlockStructInitlist();
192 unsigned int flags = BLOCK_HAS_DESCRIPTOR;
193
194 if (BlockHasCopyDispose)
195 flags |= BLOCK_HAS_COPY_DISPOSE;
196
Mike Stump92ea8882009-02-13 20:17:16 +0000197 // __isa
Mike Stumpeb3a5ca2009-02-13 19:29:27 +0000198 C = CGM.getNSConcreteStackBlock();
Mike Stumpeb3a5ca2009-02-13 19:29:27 +0000199 const llvm::PointerType *PtrToInt8Ty
200 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpb95bc002009-02-13 16:19:19 +0000201 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
202 Elts.push_back(C);
203
204 // __flags
205 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
206 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
207 C = llvm::ConstantInt::get(IntTy, flags);
208 Elts.push_back(C);
209
210 // __reserved
211 C = llvm::ConstantInt::get(IntTy, 0);
212 Elts.push_back(C);
213
Mike Stumpd55240e2009-02-19 01:01:04 +0000214 // __invoke
Mike Stumpf1711822009-02-25 23:33:13 +0000215 uint64_t subBlockSize, subBlockAlign;
Mike Stump2b6933f2009-02-28 09:07:16 +0000216 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Mike Stumpfca5da02009-02-21 20:00:35 +0000217 llvm::Function *Fn
Mike Stumpf1711822009-02-25 23:33:13 +0000218 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, subBlockSize,
219 subBlockAlign, subBlockDeclRefDecls);
Mike Stump084ba462009-02-14 22:16:35 +0000220 Elts.push_back(Fn);
Mike Stumpb95bc002009-02-13 16:19:19 +0000221
222 // __descriptor
Mike Stumpfca5da02009-02-21 20:00:35 +0000223 Elts.push_back(BuildDescriptorBlockDecl(subBlockSize));
Mike Stumpb95bc002009-02-13 16:19:19 +0000224
Mike Stumpf1711822009-02-25 23:33:13 +0000225 if (subBlockDeclRefDecls.size() == 0) {
226 C = llvm::ConstantStruct::get(Elts);
227
228 char Name[32];
229 sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
230 C = new llvm::GlobalVariable(C->getType(), true,
231 llvm::GlobalValue::InternalLinkage,
232 C, Name, &CGM.getModule());
233 QualType BPT = BE->getType();
234 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
235 return C;
236 }
237
238 std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size());
239 for (int i=0; i<5; ++i)
240 Types[i] = Elts[i]->getType();
241
Mike Stump2b6933f2009-02-28 09:07:16 +0000242 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
243 const Expr *E = subBlockDeclRefDecls[i];
244 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
245 QualType Ty = E->getType();
246 if (BDRE && BDRE->isByRef())
247 Ty = getContext().getPointerType(Ty);
248 Types[i+5] = ConvertType(Ty);
249 }
Mike Stumpf1711822009-02-25 23:33:13 +0000250
251 llvm::Type *Ty = llvm::StructType::get(Types, true);
252
253 llvm::AllocaInst *A = CreateTempAlloca(Ty);
254 A->setAlignment(subBlockAlign);
255 V = A;
256
257 for (unsigned i=0; i<5; ++i)
258 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
259
260 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
261 {
Mike Stump2b6933f2009-02-28 09:07:16 +0000262 // FIXME: Push const down.
263 Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
264 DeclRefExpr *DR;
265 ValueDecl *VD;
Mike Stumpf1711822009-02-25 23:33:13 +0000266
Mike Stump2b6933f2009-02-28 09:07:16 +0000267 DR = dyn_cast<DeclRefExpr>(E);
268 // Skip padding.
269 if (DR) continue;
270
271 BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
272 VD = BDRE->getDecl();
273
274 // FIXME: I want a better way to do this.
275 if (LocalDeclMap[VD]) {
276 E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD),
277 VD->getType(), SourceLocation(),
278 false, false);
279 }
280 if (BDRE->isByRef())
281 E = new (getContext())
282 UnaryOperator(E, UnaryOperator::AddrOf,
283 getContext().getPointerType(E->getType()),
284 SourceLocation());
285
Mike Stumpf1711822009-02-25 23:33:13 +0000286 llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp");
Mike Stump2b6933f2009-02-28 09:07:16 +0000287 RValue r = EmitAnyExpr(E, Addr, false);
Mike Stumpf1711822009-02-25 23:33:13 +0000288 if (r.isScalar())
289 Builder.CreateStore(r.getScalarVal(), Addr);
290 else if (r.isComplex())
291 // FIXME: implement
292 ErrorUnsupported(BE, "complex in block literal");
293 else if (r.isAggregate())
294 ; // Already created into the destination
295 else
296 assert (0 && "bad block variable");
297 // FIXME: Ensure that the offset created by the backend for
298 // the struct matches the previously computed offset in BlockDecls.
299 }
Mike Stumpb95bc002009-02-13 16:19:19 +0000300 }
301
Mike Stumpd55240e2009-02-19 01:01:04 +0000302 QualType BPT = BE->getType();
Mike Stumpf1711822009-02-25 23:33:13 +0000303 return Builder.CreateBitCast(V, ConvertType(BPT));
Mike Stumpb95bc002009-02-13 16:19:19 +0000304}
305
306
Mike Stump95e54802009-02-13 15:16:56 +0000307const llvm::Type *CodeGenModule::getBlockDescriptorType() {
308 if (BlockDescriptorType)
309 return BlockDescriptorType;
310
Mike Stumpc4ae9632009-02-13 15:32:32 +0000311 const llvm::Type *UnsignedLongTy =
Mike Stump95e54802009-02-13 15:16:56 +0000312 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000313
Mike Stump95e54802009-02-13 15:16:56 +0000314 // struct __block_descriptor {
315 // unsigned long reserved;
316 // unsigned long block_size;
317 // };
Mike Stumpc4ae9632009-02-13 15:32:32 +0000318 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy,
319 UnsignedLongTy,
Mike Stump95e54802009-02-13 15:16:56 +0000320 NULL);
321
322 getModule().addTypeName("struct.__block_descriptor",
323 BlockDescriptorType);
324
325 return BlockDescriptorType;
Anders Carlssond2a889b2009-02-12 00:39:25 +0000326}
327
Mike Stump0dffa462009-02-13 15:25:34 +0000328const llvm::Type *
329CodeGenModule::getGenericBlockLiteralType() {
330 if (GenericBlockLiteralType)
331 return GenericBlockLiteralType;
332
Mike Stumpc4ae9632009-02-13 15:32:32 +0000333 const llvm::Type *Int8PtrTy =
Mike Stump0dffa462009-02-13 15:25:34 +0000334 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000335
336 const llvm::Type *BlockDescPtrTy =
Mike Stump0dffa462009-02-13 15:25:34 +0000337 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000338
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000339 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
340 getTypes().ConvertType(getContext().IntTy));
341
Mike Stump0dffa462009-02-13 15:25:34 +0000342 // struct __block_literal_generic {
Mike Stumpd55240e2009-02-19 01:01:04 +0000343 // void *__isa;
344 // int __flags;
345 // int __reserved;
346 // void (*__invoke)(void *);
347 // struct __block_descriptor *__descriptor;
Mike Stump0dffa462009-02-13 15:25:34 +0000348 // };
349 GenericBlockLiteralType = llvm::StructType::get(Int8PtrTy,
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000350 IntTy,
351 IntTy,
Mike Stump0dffa462009-02-13 15:25:34 +0000352 Int8PtrTy,
353 BlockDescPtrTy,
354 NULL);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000355
Mike Stump0dffa462009-02-13 15:25:34 +0000356 getModule().addTypeName("struct.__block_literal_generic",
357 GenericBlockLiteralType);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000358
Mike Stump0dffa462009-02-13 15:25:34 +0000359 return GenericBlockLiteralType;
Anders Carlssond2a889b2009-02-12 00:39:25 +0000360}
361
Mike Stumpd55240e2009-02-19 01:01:04 +0000362const llvm::Type *
363CodeGenModule::getGenericExtendedBlockLiteralType() {
364 if (GenericExtendedBlockLiteralType)
365 return GenericExtendedBlockLiteralType;
366
367 const llvm::Type *Int8PtrTy =
368 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
369
370 const llvm::Type *BlockDescPtrTy =
371 llvm::PointerType::getUnqual(getBlockDescriptorType());
372
373 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
374 getTypes().ConvertType(getContext().IntTy));
375
376 // struct __block_literal_generic {
377 // void *__isa;
378 // int __flags;
379 // int __reserved;
380 // void (*__invoke)(void *);
381 // struct __block_descriptor *__descriptor;
382 // void *__copy_func_helper_decl;
383 // void *__destroy_func_decl;
384 // };
385 GenericExtendedBlockLiteralType = llvm::StructType::get(Int8PtrTy,
386 IntTy,
387 IntTy,
388 Int8PtrTy,
389 BlockDescPtrTy,
390 Int8PtrTy,
391 Int8PtrTy,
392 NULL);
393
394 getModule().addTypeName("struct.__block_literal_extended_generic",
395 GenericExtendedBlockLiteralType);
396
397 return GenericExtendedBlockLiteralType;
398}
399
Mike Stumpc4ae9632009-02-13 15:32:32 +0000400/// getBlockFunctionType - Given a BlockPointerType, will return the
Anders Carlssond2a889b2009-02-12 00:39:25 +0000401/// function type for the block, including the first block literal argument.
402static QualType getBlockFunctionType(ASTContext &Ctx,
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000403 const BlockPointerType *BPT) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000404 const FunctionProtoType *FTy = cast<FunctionProtoType>(BPT->getPointeeType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000405
Anders Carlssond2a889b2009-02-12 00:39:25 +0000406 llvm::SmallVector<QualType, 8> Types;
407 Types.push_back(Ctx.getPointerType(Ctx.VoidTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000408
Douglas Gregor4fa58902009-02-26 23:50:07 +0000409 for (FunctionProtoType::arg_type_iterator i = FTy->arg_type_begin(),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000410 e = FTy->arg_type_end(); i != e; ++i)
411 Types.push_back(*i);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000412
Anders Carlssond2a889b2009-02-12 00:39:25 +0000413 return Ctx.getFunctionType(FTy->getResultType(),
Mike Stumpc4ae9632009-02-13 15:32:32 +0000414 &Types[0], Types.size(),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000415 FTy->isVariadic(), 0);
416}
417
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000418RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpc4ae9632009-02-13 15:32:32 +0000419 const BlockPointerType *BPT =
Anders Carlssond2a889b2009-02-12 00:39:25 +0000420 E->getCallee()->getType()->getAsBlockPointerType();
Mike Stumpc4ae9632009-02-13 15:32:32 +0000421
Anders Carlssond2a889b2009-02-12 00:39:25 +0000422 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
423
424 // Get a pointer to the generic block literal.
425 const llvm::Type *BlockLiteralTy =
Mike Stump0dffa462009-02-13 15:25:34 +0000426 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssond2a889b2009-02-12 00:39:25 +0000427
428 // Bitcast the callee to a block literal.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000429 llvm::Value *BlockLiteral =
Anders Carlssond2a889b2009-02-12 00:39:25 +0000430 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
431
432 // Get the function pointer from the literal.
433 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Mike Stump39bcc612009-02-22 13:27:11 +0000434 llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp");
Anders Carlssond2a889b2009-02-12 00:39:25 +0000435
436 // Cast the function pointer to the right type.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000437 const llvm::Type *BlockFTy =
Anders Carlssond2a889b2009-02-12 00:39:25 +0000438 ConvertType(getBlockFunctionType(getContext(), BPT));
439 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
440 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
441
Mike Stumpc4ae9632009-02-13 15:32:32 +0000442 BlockLiteral =
443 Builder.CreateBitCast(BlockLiteral,
Anders Carlssond2a889b2009-02-12 00:39:25 +0000444 llvm::PointerType::getUnqual(llvm::Type::Int8Ty),
445 "tmp");
Mike Stumpc4ae9632009-02-13 15:32:32 +0000446
Anders Carlssond2a889b2009-02-12 00:39:25 +0000447 // Add the block literal.
448 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
449 CallArgList Args;
450 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000451
Anders Carlssond2a889b2009-02-12 00:39:25 +0000452 // And the rest of the arguments.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000453 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
Anders Carlssond2a889b2009-02-12 00:39:25 +0000454 i != e; ++i)
Mike Stumpc4ae9632009-02-13 15:32:32 +0000455 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000456 i->getType()));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000457
Anders Carlssond2a889b2009-02-12 00:39:25 +0000458 // And call the block.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000459 return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000460 Func, Args);
461}
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000462
Mike Stump084ba462009-02-14 22:16:35 +0000463llvm::Constant *
Mike Stump4b55c7f2009-02-14 22:49:33 +0000464CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000465 // Generate the block descriptor.
466 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000467 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
468 getTypes().ConvertType(getContext().IntTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000469
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000470 llvm::Constant *DescriptorFields[2];
Mike Stumpc4ae9632009-02-13 15:32:32 +0000471
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000472 // Reserved
473 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000474
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000475 // Block literal size. For global blocks we just use the size of the generic
476 // block literal struct.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000477 uint64_t BlockLiteralSize =
Mike Stump0dffa462009-02-13 15:25:34 +0000478 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000479 DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000480
481 llvm::Constant *DescriptorStruct =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000482 llvm::ConstantStruct::get(&DescriptorFields[0], 2);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000483
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000484 llvm::GlobalVariable *Descriptor =
485 new llvm::GlobalVariable(DescriptorStruct->getType(), true,
Mike Stumpc4ae9632009-02-13 15:32:32 +0000486 llvm::GlobalVariable::InternalLinkage,
487 DescriptorStruct, "__block_descriptor_global",
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000488 &getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000489
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000490 // Generate the constants for the block literal.
491 llvm::Constant *LiteralFields[5];
Mike Stumpc4ae9632009-02-13 15:32:32 +0000492
Mike Stump084ba462009-02-14 22:16:35 +0000493 CodeGenFunction::BlockInfo Info(0, n);
Mike Stumpf1711822009-02-25 23:33:13 +0000494 uint64_t subBlockSize, subBlockAlign;
Mike Stump2b6933f2009-02-28 09:07:16 +0000495 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Mike Stumpfca5da02009-02-21 20:00:35 +0000496 llvm::Function *Fn
Mike Stumpf1711822009-02-25 23:33:13 +0000497 = CodeGenFunction(*this).GenerateBlockFunction(BE, Info, subBlockSize,
498 subBlockAlign,
499 subBlockDeclRefDecls);
Mike Stumpfca5da02009-02-21 20:00:35 +0000500 assert(subBlockSize == BlockLiteralSize
501 && "no imports allowed for global block");
Mike Stumpc4ae9632009-02-13 15:32:32 +0000502
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000503 // isa
Mike Stump5f87e9d2009-02-13 17:23:42 +0000504 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpc4ae9632009-02-13 15:32:32 +0000505
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000506 // Flags
Mike Stumpb95bc002009-02-13 16:19:19 +0000507 LiteralFields[1] = llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000508
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000509 // Reserved
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000510 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000511
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000512 // Function
513 LiteralFields[3] = Fn;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000514
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000515 // Descriptor
516 LiteralFields[4] = Descriptor;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000517
518 llvm::Constant *BlockLiteralStruct =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000519 llvm::ConstantStruct::get(&LiteralFields[0], 5);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000520
521 llvm::GlobalVariable *BlockLiteral =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000522 new llvm::GlobalVariable(BlockLiteralStruct->getType(), true,
Mike Stumpc4ae9632009-02-13 15:32:32 +0000523 llvm::GlobalVariable::InternalLinkage,
524 BlockLiteralStruct, "__block_literal_global",
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000525 &getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000526
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000527 return BlockLiteral;
528}
529
Mike Stumpfca5da02009-02-21 20:00:35 +0000530llvm::Value *CodeGenFunction::LoadBlockStruct() {
531 return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self");
532}
533
Chris Lattner8130e7f2009-02-28 19:01:03 +0000534llvm::Function *CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
Mike Stumpfca5da02009-02-21 20:00:35 +0000535 const BlockInfo& Info,
Mike Stumpf1711822009-02-25 23:33:13 +0000536 uint64_t &Size,
537 uint64_t &Align,
Mike Stump2b6933f2009-02-28 09:07:16 +0000538 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000539 const FunctionProtoType *FTy =
Chris Lattner8130e7f2009-02-28 19:01:03 +0000540 cast<FunctionProtoType>(BExpr->getFunctionType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000541
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000542 FunctionArgList Args;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000543
Chris Lattner8130e7f2009-02-28 19:01:03 +0000544 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000545
546 // FIXME: This leaks
Mike Stumpc4ae9632009-02-13 15:32:32 +0000547 ImplicitParamDecl *SelfDecl =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000548 ImplicitParamDecl::Create(getContext(), 0,
549 SourceLocation(), 0,
550 getContext().getPointerType(getContext().VoidTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000551
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000552 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stumpfca5da02009-02-21 20:00:35 +0000553 BlockStructDecl = SelfDecl;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000554
555 for (BlockDecl::param_iterator i = BD->param_begin(),
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000556 e = BD->param_end(); i != e; ++i)
Mike Stump459207f2009-02-17 23:25:52 +0000557 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000558
559 const CGFunctionInfo &FI =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000560 CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args);
561
Mike Stump084ba462009-02-14 22:16:35 +0000562 std::string Name = std::string("__") + Info.Name + "_block_invoke_";
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000563 CodeGenTypes &Types = CGM.getTypes();
564 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000565
566 llvm::Function *Fn =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000567 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
568 Name,
569 &CGM.getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000570
571 StartFunction(BD, FTy->getResultType(), Fn, Args,
Chris Lattner8130e7f2009-02-28 19:01:03 +0000572 BExpr->getBody()->getLocEnd());
573 EmitStmt(BExpr->getBody());
574 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000575
Mike Stumpf1711822009-02-25 23:33:13 +0000576 // The runtime needs a minimum alignment of a void *.
577 uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
578 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign);
579
Mike Stumpfca5da02009-02-21 20:00:35 +0000580 Size = BlockOffset;
Mike Stumpf1711822009-02-25 23:33:13 +0000581 Align = BlockAlign;
582 subBlockDeclRefDecls = BlockDeclRefDecls;
Mike Stumpfca5da02009-02-21 20:00:35 +0000583
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000584 return Fn;
585}
Mike Stump2b6933f2009-02-28 09:07:16 +0000586
587uint64_t CodeGenFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
588 const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
589
590 uint64_t Size = getContext().getTypeSize(D->getType()) / 8;
591 uint64_t Align = getContext().getDeclAlignInBytes(D);
592
593 if (BDRE->isByRef()) {
594 Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8;
595 Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
596 }
597
598 assert ((Align > 0) && "alignment must be 1 byte or more");
599
600 uint64_t OldOffset = BlockOffset;
601
602 // Ensure proper alignment, even if it means we have to have a gap
603 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align);
604 BlockAlign = std::max(Align, BlockAlign);
605
606 uint64_t Pad = BlockOffset - OldOffset;
607 if (Pad) {
608 llvm::ArrayType::get(llvm::Type::Int8Ty, Pad);
609 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
610 llvm::APInt(32, Pad),
611 ArrayType::Normal, 0);
612 ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
613 0, QualType(PadTy), VarDecl::None,
614 SourceLocation());
615 Expr *E;
616 E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
617 SourceLocation(), false, false);
618 BlockDeclRefDecls.push_back(E);
619 }
620 BlockDeclRefDecls.push_back(BDRE);
621
622 BlockOffset += Size;
623 return BlockOffset-Size;
624}