blob: da7afe1d436fc479ec42f5a0c76e66edd44e9d60 [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) {
Mike Stumpa7db9be2009-03-01 20:07:53 +0000226 // Optimize to being a global block.
227 Elts[0] = CGM.getNSConcreteGlobalBlock();
228 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
229
Mike Stumpf1711822009-02-25 23:33:13 +0000230 C = llvm::ConstantStruct::get(Elts);
231
232 char Name[32];
233 sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
234 C = new llvm::GlobalVariable(C->getType(), true,
235 llvm::GlobalValue::InternalLinkage,
236 C, Name, &CGM.getModule());
237 QualType BPT = BE->getType();
238 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
239 return C;
240 }
241
242 std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size());
243 for (int i=0; i<5; ++i)
244 Types[i] = Elts[i]->getType();
245
Mike Stump2b6933f2009-02-28 09:07:16 +0000246 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
247 const Expr *E = subBlockDeclRefDecls[i];
248 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
249 QualType Ty = E->getType();
250 if (BDRE && BDRE->isByRef())
251 Ty = getContext().getPointerType(Ty);
252 Types[i+5] = ConvertType(Ty);
253 }
Mike Stumpf1711822009-02-25 23:33:13 +0000254
255 llvm::Type *Ty = llvm::StructType::get(Types, true);
256
257 llvm::AllocaInst *A = CreateTempAlloca(Ty);
258 A->setAlignment(subBlockAlign);
259 V = A;
260
261 for (unsigned i=0; i<5; ++i)
262 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
263
264 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
265 {
Mike Stump2b6933f2009-02-28 09:07:16 +0000266 // FIXME: Push const down.
267 Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
268 DeclRefExpr *DR;
269 ValueDecl *VD;
Mike Stumpf1711822009-02-25 23:33:13 +0000270
Mike Stump2b6933f2009-02-28 09:07:16 +0000271 DR = dyn_cast<DeclRefExpr>(E);
272 // Skip padding.
273 if (DR) continue;
274
275 BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
276 VD = BDRE->getDecl();
277
278 // FIXME: I want a better way to do this.
279 if (LocalDeclMap[VD]) {
280 E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD),
281 VD->getType(), SourceLocation(),
282 false, false);
283 }
284 if (BDRE->isByRef())
285 E = new (getContext())
286 UnaryOperator(E, UnaryOperator::AddrOf,
287 getContext().getPointerType(E->getType()),
288 SourceLocation());
289
Mike Stumpf1711822009-02-25 23:33:13 +0000290 llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp");
Mike Stump2b6933f2009-02-28 09:07:16 +0000291 RValue r = EmitAnyExpr(E, Addr, false);
Mike Stumpf1711822009-02-25 23:33:13 +0000292 if (r.isScalar())
293 Builder.CreateStore(r.getScalarVal(), Addr);
294 else if (r.isComplex())
295 // FIXME: implement
296 ErrorUnsupported(BE, "complex in block literal");
297 else if (r.isAggregate())
298 ; // Already created into the destination
299 else
300 assert (0 && "bad block variable");
301 // FIXME: Ensure that the offset created by the backend for
302 // the struct matches the previously computed offset in BlockDecls.
303 }
Mike Stumpb95bc002009-02-13 16:19:19 +0000304 }
305
Mike Stumpd55240e2009-02-19 01:01:04 +0000306 QualType BPT = BE->getType();
Mike Stumpf1711822009-02-25 23:33:13 +0000307 return Builder.CreateBitCast(V, ConvertType(BPT));
Mike Stumpb95bc002009-02-13 16:19:19 +0000308}
309
310
Mike Stump95e54802009-02-13 15:16:56 +0000311const llvm::Type *CodeGenModule::getBlockDescriptorType() {
312 if (BlockDescriptorType)
313 return BlockDescriptorType;
314
Mike Stumpc4ae9632009-02-13 15:32:32 +0000315 const llvm::Type *UnsignedLongTy =
Mike Stump95e54802009-02-13 15:16:56 +0000316 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000317
Mike Stump95e54802009-02-13 15:16:56 +0000318 // struct __block_descriptor {
319 // unsigned long reserved;
320 // unsigned long block_size;
321 // };
Mike Stumpc4ae9632009-02-13 15:32:32 +0000322 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy,
323 UnsignedLongTy,
Mike Stump95e54802009-02-13 15:16:56 +0000324 NULL);
325
326 getModule().addTypeName("struct.__block_descriptor",
327 BlockDescriptorType);
328
329 return BlockDescriptorType;
Anders Carlssond2a889b2009-02-12 00:39:25 +0000330}
331
Mike Stump0dffa462009-02-13 15:25:34 +0000332const llvm::Type *
333CodeGenModule::getGenericBlockLiteralType() {
334 if (GenericBlockLiteralType)
335 return GenericBlockLiteralType;
336
Mike Stumpc4ae9632009-02-13 15:32:32 +0000337 const llvm::Type *Int8PtrTy =
Mike Stump0dffa462009-02-13 15:25:34 +0000338 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000339
340 const llvm::Type *BlockDescPtrTy =
Mike Stump0dffa462009-02-13 15:25:34 +0000341 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000342
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000343 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
344 getTypes().ConvertType(getContext().IntTy));
345
Mike Stump0dffa462009-02-13 15:25:34 +0000346 // struct __block_literal_generic {
Mike Stumpd55240e2009-02-19 01:01:04 +0000347 // void *__isa;
348 // int __flags;
349 // int __reserved;
350 // void (*__invoke)(void *);
351 // struct __block_descriptor *__descriptor;
Mike Stump0dffa462009-02-13 15:25:34 +0000352 // };
353 GenericBlockLiteralType = llvm::StructType::get(Int8PtrTy,
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000354 IntTy,
355 IntTy,
Mike Stump0dffa462009-02-13 15:25:34 +0000356 Int8PtrTy,
357 BlockDescPtrTy,
358 NULL);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000359
Mike Stump0dffa462009-02-13 15:25:34 +0000360 getModule().addTypeName("struct.__block_literal_generic",
361 GenericBlockLiteralType);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000362
Mike Stump0dffa462009-02-13 15:25:34 +0000363 return GenericBlockLiteralType;
Anders Carlssond2a889b2009-02-12 00:39:25 +0000364}
365
Mike Stumpd55240e2009-02-19 01:01:04 +0000366const llvm::Type *
367CodeGenModule::getGenericExtendedBlockLiteralType() {
368 if (GenericExtendedBlockLiteralType)
369 return GenericExtendedBlockLiteralType;
370
371 const llvm::Type *Int8PtrTy =
372 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
373
374 const llvm::Type *BlockDescPtrTy =
375 llvm::PointerType::getUnqual(getBlockDescriptorType());
376
377 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
378 getTypes().ConvertType(getContext().IntTy));
379
380 // struct __block_literal_generic {
381 // void *__isa;
382 // int __flags;
383 // int __reserved;
384 // void (*__invoke)(void *);
385 // struct __block_descriptor *__descriptor;
386 // void *__copy_func_helper_decl;
387 // void *__destroy_func_decl;
388 // };
389 GenericExtendedBlockLiteralType = llvm::StructType::get(Int8PtrTy,
390 IntTy,
391 IntTy,
392 Int8PtrTy,
393 BlockDescPtrTy,
394 Int8PtrTy,
395 Int8PtrTy,
396 NULL);
397
398 getModule().addTypeName("struct.__block_literal_extended_generic",
399 GenericExtendedBlockLiteralType);
400
401 return GenericExtendedBlockLiteralType;
402}
403
Mike Stumpc4ae9632009-02-13 15:32:32 +0000404/// getBlockFunctionType - Given a BlockPointerType, will return the
Anders Carlssond2a889b2009-02-12 00:39:25 +0000405/// function type for the block, including the first block literal argument.
406static QualType getBlockFunctionType(ASTContext &Ctx,
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000407 const BlockPointerType *BPT) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000408 const FunctionProtoType *FTy = cast<FunctionProtoType>(BPT->getPointeeType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000409
Anders Carlssond2a889b2009-02-12 00:39:25 +0000410 llvm::SmallVector<QualType, 8> Types;
411 Types.push_back(Ctx.getPointerType(Ctx.VoidTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000412
Douglas Gregor4fa58902009-02-26 23:50:07 +0000413 for (FunctionProtoType::arg_type_iterator i = FTy->arg_type_begin(),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000414 e = FTy->arg_type_end(); i != e; ++i)
415 Types.push_back(*i);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000416
Anders Carlssond2a889b2009-02-12 00:39:25 +0000417 return Ctx.getFunctionType(FTy->getResultType(),
Mike Stumpc4ae9632009-02-13 15:32:32 +0000418 &Types[0], Types.size(),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000419 FTy->isVariadic(), 0);
420}
421
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000422RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpc4ae9632009-02-13 15:32:32 +0000423 const BlockPointerType *BPT =
Anders Carlssond2a889b2009-02-12 00:39:25 +0000424 E->getCallee()->getType()->getAsBlockPointerType();
Mike Stumpc4ae9632009-02-13 15:32:32 +0000425
Anders Carlssond2a889b2009-02-12 00:39:25 +0000426 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
427
428 // Get a pointer to the generic block literal.
429 const llvm::Type *BlockLiteralTy =
Mike Stump0dffa462009-02-13 15:25:34 +0000430 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssond2a889b2009-02-12 00:39:25 +0000431
432 // Bitcast the callee to a block literal.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000433 llvm::Value *BlockLiteral =
Anders Carlssond2a889b2009-02-12 00:39:25 +0000434 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
435
436 // Get the function pointer from the literal.
437 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Mike Stump39bcc612009-02-22 13:27:11 +0000438 llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp");
Anders Carlssond2a889b2009-02-12 00:39:25 +0000439
440 // Cast the function pointer to the right type.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000441 const llvm::Type *BlockFTy =
Anders Carlssond2a889b2009-02-12 00:39:25 +0000442 ConvertType(getBlockFunctionType(getContext(), BPT));
443 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
444 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
445
Mike Stumpc4ae9632009-02-13 15:32:32 +0000446 BlockLiteral =
447 Builder.CreateBitCast(BlockLiteral,
Anders Carlssond2a889b2009-02-12 00:39:25 +0000448 llvm::PointerType::getUnqual(llvm::Type::Int8Ty),
449 "tmp");
Mike Stumpc4ae9632009-02-13 15:32:32 +0000450
Anders Carlssond2a889b2009-02-12 00:39:25 +0000451 // Add the block literal.
452 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
453 CallArgList Args;
454 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000455
Anders Carlssond2a889b2009-02-12 00:39:25 +0000456 // And the rest of the arguments.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000457 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
Anders Carlssond2a889b2009-02-12 00:39:25 +0000458 i != e; ++i)
Mike Stumpc4ae9632009-02-13 15:32:32 +0000459 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000460 i->getType()));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000461
Anders Carlssond2a889b2009-02-12 00:39:25 +0000462 // And call the block.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000463 return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000464 Func, Args);
465}
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000466
Mike Stump084ba462009-02-14 22:16:35 +0000467llvm::Constant *
Mike Stump4b55c7f2009-02-14 22:49:33 +0000468CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000469 // Generate the block descriptor.
470 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000471 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
472 getTypes().ConvertType(getContext().IntTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000473
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000474 llvm::Constant *DescriptorFields[2];
Mike Stumpc4ae9632009-02-13 15:32:32 +0000475
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000476 // Reserved
477 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000478
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000479 // Block literal size. For global blocks we just use the size of the generic
480 // block literal struct.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000481 uint64_t BlockLiteralSize =
Mike Stump0dffa462009-02-13 15:25:34 +0000482 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000483 DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000484
485 llvm::Constant *DescriptorStruct =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000486 llvm::ConstantStruct::get(&DescriptorFields[0], 2);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000487
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000488 llvm::GlobalVariable *Descriptor =
489 new llvm::GlobalVariable(DescriptorStruct->getType(), true,
Mike Stumpc4ae9632009-02-13 15:32:32 +0000490 llvm::GlobalVariable::InternalLinkage,
491 DescriptorStruct, "__block_descriptor_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 // Generate the constants for the block literal.
495 llvm::Constant *LiteralFields[5];
Mike Stumpc4ae9632009-02-13 15:32:32 +0000496
Mike Stump084ba462009-02-14 22:16:35 +0000497 CodeGenFunction::BlockInfo Info(0, n);
Mike Stumpf1711822009-02-25 23:33:13 +0000498 uint64_t subBlockSize, subBlockAlign;
Mike Stump2b6933f2009-02-28 09:07:16 +0000499 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Mike Stumpfca5da02009-02-21 20:00:35 +0000500 llvm::Function *Fn
Mike Stumpf1711822009-02-25 23:33:13 +0000501 = CodeGenFunction(*this).GenerateBlockFunction(BE, Info, subBlockSize,
502 subBlockAlign,
503 subBlockDeclRefDecls);
Mike Stumpfca5da02009-02-21 20:00:35 +0000504 assert(subBlockSize == BlockLiteralSize
505 && "no imports allowed for global block");
Mike Stumpc4ae9632009-02-13 15:32:32 +0000506
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000507 // isa
Mike Stump5f87e9d2009-02-13 17:23:42 +0000508 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpc4ae9632009-02-13 15:32:32 +0000509
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000510 // Flags
Mike Stumpb95bc002009-02-13 16:19:19 +0000511 LiteralFields[1] = llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000512
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000513 // Reserved
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000514 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000515
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000516 // Function
517 LiteralFields[3] = Fn;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000518
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000519 // Descriptor
520 LiteralFields[4] = Descriptor;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000521
522 llvm::Constant *BlockLiteralStruct =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000523 llvm::ConstantStruct::get(&LiteralFields[0], 5);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000524
525 llvm::GlobalVariable *BlockLiteral =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000526 new llvm::GlobalVariable(BlockLiteralStruct->getType(), true,
Mike Stumpc4ae9632009-02-13 15:32:32 +0000527 llvm::GlobalVariable::InternalLinkage,
528 BlockLiteralStruct, "__block_literal_global",
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000529 &getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000530
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000531 return BlockLiteral;
532}
533
Mike Stumpfca5da02009-02-21 20:00:35 +0000534llvm::Value *CodeGenFunction::LoadBlockStruct() {
535 return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self");
536}
537
Chris Lattner8130e7f2009-02-28 19:01:03 +0000538llvm::Function *CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
Mike Stumpfca5da02009-02-21 20:00:35 +0000539 const BlockInfo& Info,
Mike Stumpf1711822009-02-25 23:33:13 +0000540 uint64_t &Size,
541 uint64_t &Align,
Mike Stump2b6933f2009-02-28 09:07:16 +0000542 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000543 const FunctionProtoType *FTy =
Chris Lattner8130e7f2009-02-28 19:01:03 +0000544 cast<FunctionProtoType>(BExpr->getFunctionType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000545
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000546 FunctionArgList Args;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000547
Chris Lattner8130e7f2009-02-28 19:01:03 +0000548 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000549
550 // FIXME: This leaks
Mike Stumpc4ae9632009-02-13 15:32:32 +0000551 ImplicitParamDecl *SelfDecl =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000552 ImplicitParamDecl::Create(getContext(), 0,
553 SourceLocation(), 0,
554 getContext().getPointerType(getContext().VoidTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000555
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000556 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stumpfca5da02009-02-21 20:00:35 +0000557 BlockStructDecl = SelfDecl;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000558
559 for (BlockDecl::param_iterator i = BD->param_begin(),
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000560 e = BD->param_end(); i != e; ++i)
Mike Stump459207f2009-02-17 23:25:52 +0000561 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000562
563 const CGFunctionInfo &FI =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000564 CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args);
565
Mike Stump084ba462009-02-14 22:16:35 +0000566 std::string Name = std::string("__") + Info.Name + "_block_invoke_";
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000567 CodeGenTypes &Types = CGM.getTypes();
568 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000569
570 llvm::Function *Fn =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000571 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
572 Name,
573 &CGM.getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000574
575 StartFunction(BD, FTy->getResultType(), Fn, Args,
Chris Lattner8130e7f2009-02-28 19:01:03 +0000576 BExpr->getBody()->getLocEnd());
577 EmitStmt(BExpr->getBody());
578 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000579
Mike Stumpf1711822009-02-25 23:33:13 +0000580 // The runtime needs a minimum alignment of a void *.
581 uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
582 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign);
583
Mike Stumpfca5da02009-02-21 20:00:35 +0000584 Size = BlockOffset;
Mike Stumpf1711822009-02-25 23:33:13 +0000585 Align = BlockAlign;
586 subBlockDeclRefDecls = BlockDeclRefDecls;
Mike Stumpfca5da02009-02-21 20:00:35 +0000587
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000588 return Fn;
589}
Mike Stump2b6933f2009-02-28 09:07:16 +0000590
591uint64_t CodeGenFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
592 const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
593
594 uint64_t Size = getContext().getTypeSize(D->getType()) / 8;
595 uint64_t Align = getContext().getDeclAlignInBytes(D);
596
597 if (BDRE->isByRef()) {
598 Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8;
599 Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
600 }
601
602 assert ((Align > 0) && "alignment must be 1 byte or more");
603
604 uint64_t OldOffset = BlockOffset;
605
606 // Ensure proper alignment, even if it means we have to have a gap
607 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align);
608 BlockAlign = std::max(Align, BlockAlign);
609
610 uint64_t Pad = BlockOffset - OldOffset;
611 if (Pad) {
612 llvm::ArrayType::get(llvm::Type::Int8Ty, Pad);
613 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
614 llvm::APInt(32, Pad),
615 ArrayType::Normal, 0);
616 ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
617 0, QualType(PadTy), VarDecl::None,
618 SourceLocation());
619 Expr *E;
620 E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
621 SourceLocation(), false, false);
622 BlockDeclRefDecls.push_back(E);
623 }
624 BlockDeclRefDecls.push_back(BDRE);
625
626 BlockOffset += Size;
627 return BlockOffset-Size;
628}