blob: 93e1b57fc989232ea56ff1e22c8d6454c2e46568 [file] [log] [blame]
Anders Carlssonacfde802009-02-12 00:39:25 +00001//===--- CGBlocks.cpp - Emit LLVM Code for declarations -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit blocks.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
16#include "llvm/Module.h"
Anders Carlssond5cab542009-02-12 17:55:02 +000017#include "llvm/Target/TargetData.h"
Anders Carlssonacfde802009-02-12 00:39:25 +000018
19#include <algorithm>
20
21using namespace clang;
22using namespace CodeGen;
23
Anders Carlssond5cab542009-02-12 17:55:02 +000024enum {
Mike Stumpe5fee252009-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 Carlssond5cab542009-02-12 17:55:02 +000031};
32
Mike Stump4e7a1f72009-02-21 20:00:35 +000033llvm::Constant *CodeGenFunction::BuildDescriptorBlockDecl(uint64_t Size) {
Mike Stumpe5fee252009-02-13 16:19:19 +000034 const llvm::PointerType *PtrToInt8Ty
35 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stump56129b12009-02-13 16:55:51 +000036 const llvm::Type *UnsignedLongTy
37 = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpe5fee252009-02-13 16:19:19 +000038 llvm::Constant *C;
39 std::vector<llvm::Constant*> Elts;
40
41 // reserved
Mike Stump56129b12009-02-13 16:55:51 +000042 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpe5fee252009-02-13 16:19:19 +000043 Elts.push_back(C);
44
45 // Size
Mike Stumpd6840002009-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 Stump4e7a1f72009-02-21 20:00:35 +000049 C = llvm::ConstantInt::get(UnsignedLongTy, Size);
Mike Stumpe5fee252009-02-13 16:19:19 +000050 Elts.push_back(C);
51
52 if (BlockHasCopyDispose) {
53 // copy_func_helper_decl
Mike Stump4e7a1f72009-02-21 20:00:35 +000054 // FIXME: implement
Mike Stump56129b12009-02-13 16:55:51 +000055 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpe5fee252009-02-13 16:19:19 +000056 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
57 Elts.push_back(C);
58
59 // destroy_func_decl
Mike Stump4e7a1f72009-02-21 20:00:35 +000060 // FIXME: implement
Mike Stump56129b12009-02-13 16:55:51 +000061 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpe5fee252009-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 Stumpe5fee252009-02-13 16:19:19 +000068 C = new llvm::GlobalVariable(C->getType(), true,
69 llvm::GlobalValue::InternalLinkage,
Mike Stump7d6dc4f2009-02-13 20:17:16 +000070 C, "__block_descriptor_tmp", &CGM.getModule());
Mike Stumpe5fee252009-02-13 16:19:19 +000071 return C;
72}
73
Mike Stumpf99f1d02009-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 Stumpf7448952009-02-13 19:38:12 +000080 // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
Mike Stumpf99f1d02009-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 Stump59c5b112009-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 Stumpf7448952009-02-13 19:38:12 +000098 // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
Mike Stump59c5b112009-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 Carlsson4de9fce2009-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
Mike Stumpbd65cac2009-02-19 01:01:04 +0000135// FIXME: Push most into CGM, passing down a few bits, like current
136// function name.
Mike Stump8a2b4b12009-02-25 23:33:13 +0000137llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Mike Stumpe5fee252009-02-13 16:19:19 +0000138
Anders Carlsson4de9fce2009-03-01 01:09:12 +0000139 std::string Name = CurFn->getName();
140 CodeGenFunction::BlockInfo Info(0, Name.c_str());
141 CollectBlockDeclRefInfo(BE->getBody(), Info);
142
143 // Check if the block can be global.
144 if (CanBlockBeGlobal(Info))
145 return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
146
Mike Stumpe5fee252009-02-13 16:19:19 +0000147 std::vector<llvm::Constant*> Elts;
148 llvm::Constant *C;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000149 llvm::Value *V;
Mike Stumpe5fee252009-02-13 16:19:19 +0000150
Mike Stumpe5fee252009-02-13 16:19:19 +0000151 {
152 // C = BuildBlockStructInitlist();
153 unsigned int flags = BLOCK_HAS_DESCRIPTOR;
154
155 if (BlockHasCopyDispose)
156 flags |= BLOCK_HAS_COPY_DISPOSE;
157
Mike Stump7d6dc4f2009-02-13 20:17:16 +0000158 // __isa
Mike Stump59c5b112009-02-13 19:29:27 +0000159 C = CGM.getNSConcreteStackBlock();
Mike Stump59c5b112009-02-13 19:29:27 +0000160 const llvm::PointerType *PtrToInt8Ty
161 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpe5fee252009-02-13 16:19:19 +0000162 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
163 Elts.push_back(C);
164
165 // __flags
166 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
167 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
168 C = llvm::ConstantInt::get(IntTy, flags);
169 Elts.push_back(C);
170
171 // __reserved
172 C = llvm::ConstantInt::get(IntTy, 0);
173 Elts.push_back(C);
174
Mike Stumpbd65cac2009-02-19 01:01:04 +0000175 // __invoke
Mike Stump8a2b4b12009-02-25 23:33:13 +0000176 uint64_t subBlockSize, subBlockAlign;
Mike Stumpa99038c2009-02-28 09:07:16 +0000177 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000178 llvm::Function *Fn
Mike Stump8a2b4b12009-02-25 23:33:13 +0000179 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, subBlockSize,
180 subBlockAlign, subBlockDeclRefDecls);
Mike Stump67a64482009-02-14 22:16:35 +0000181 Elts.push_back(Fn);
Mike Stumpe5fee252009-02-13 16:19:19 +0000182
183 // __descriptor
Mike Stump4e7a1f72009-02-21 20:00:35 +0000184 Elts.push_back(BuildDescriptorBlockDecl(subBlockSize));
Mike Stumpe5fee252009-02-13 16:19:19 +0000185
Mike Stump8a2b4b12009-02-25 23:33:13 +0000186 if (subBlockDeclRefDecls.size() == 0) {
187 C = llvm::ConstantStruct::get(Elts);
188
189 char Name[32];
190 sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
191 C = new llvm::GlobalVariable(C->getType(), true,
192 llvm::GlobalValue::InternalLinkage,
193 C, Name, &CGM.getModule());
194 QualType BPT = BE->getType();
195 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
196 return C;
197 }
198
199 std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size());
200 for (int i=0; i<5; ++i)
201 Types[i] = Elts[i]->getType();
202
Mike Stumpa99038c2009-02-28 09:07:16 +0000203 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
204 const Expr *E = subBlockDeclRefDecls[i];
205 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
206 QualType Ty = E->getType();
207 if (BDRE && BDRE->isByRef())
208 Ty = getContext().getPointerType(Ty);
209 Types[i+5] = ConvertType(Ty);
210 }
Mike Stump8a2b4b12009-02-25 23:33:13 +0000211
212 llvm::Type *Ty = llvm::StructType::get(Types, true);
213
214 llvm::AllocaInst *A = CreateTempAlloca(Ty);
215 A->setAlignment(subBlockAlign);
216 V = A;
217
218 for (unsigned i=0; i<5; ++i)
219 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
220
221 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
222 {
Mike Stumpa99038c2009-02-28 09:07:16 +0000223 // FIXME: Push const down.
224 Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
225 DeclRefExpr *DR;
226 ValueDecl *VD;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000227
Mike Stumpa99038c2009-02-28 09:07:16 +0000228 DR = dyn_cast<DeclRefExpr>(E);
229 // Skip padding.
230 if (DR) continue;
231
232 BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
233 VD = BDRE->getDecl();
234
235 // FIXME: I want a better way to do this.
236 if (LocalDeclMap[VD]) {
237 E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD),
238 VD->getType(), SourceLocation(),
239 false, false);
240 }
241 if (BDRE->isByRef())
242 E = new (getContext())
243 UnaryOperator(E, UnaryOperator::AddrOf,
244 getContext().getPointerType(E->getType()),
245 SourceLocation());
246
Mike Stump8a2b4b12009-02-25 23:33:13 +0000247 llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp");
Mike Stumpa99038c2009-02-28 09:07:16 +0000248 RValue r = EmitAnyExpr(E, Addr, false);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000249 if (r.isScalar())
250 Builder.CreateStore(r.getScalarVal(), Addr);
251 else if (r.isComplex())
252 // FIXME: implement
253 ErrorUnsupported(BE, "complex in block literal");
254 else if (r.isAggregate())
255 ; // Already created into the destination
256 else
257 assert (0 && "bad block variable");
258 // FIXME: Ensure that the offset created by the backend for
259 // the struct matches the previously computed offset in BlockDecls.
260 }
Mike Stumpe5fee252009-02-13 16:19:19 +0000261 }
262
Mike Stumpbd65cac2009-02-19 01:01:04 +0000263 QualType BPT = BE->getType();
Mike Stump8a2b4b12009-02-25 23:33:13 +0000264 return Builder.CreateBitCast(V, ConvertType(BPT));
Mike Stumpe5fee252009-02-13 16:19:19 +0000265}
266
267
Mike Stumpab695142009-02-13 15:16:56 +0000268const llvm::Type *CodeGenModule::getBlockDescriptorType() {
269 if (BlockDescriptorType)
270 return BlockDescriptorType;
271
Mike Stumpa5448542009-02-13 15:32:32 +0000272 const llvm::Type *UnsignedLongTy =
Mike Stumpab695142009-02-13 15:16:56 +0000273 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000274
Mike Stumpab695142009-02-13 15:16:56 +0000275 // struct __block_descriptor {
276 // unsigned long reserved;
277 // unsigned long block_size;
278 // };
Mike Stumpa5448542009-02-13 15:32:32 +0000279 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy,
280 UnsignedLongTy,
Mike Stumpab695142009-02-13 15:16:56 +0000281 NULL);
282
283 getModule().addTypeName("struct.__block_descriptor",
284 BlockDescriptorType);
285
286 return BlockDescriptorType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000287}
288
Mike Stump9b8a7972009-02-13 15:25:34 +0000289const llvm::Type *
290CodeGenModule::getGenericBlockLiteralType() {
291 if (GenericBlockLiteralType)
292 return GenericBlockLiteralType;
293
Mike Stumpa5448542009-02-13 15:32:32 +0000294 const llvm::Type *Int8PtrTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000295 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpa5448542009-02-13 15:32:32 +0000296
297 const llvm::Type *BlockDescPtrTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000298 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpa5448542009-02-13 15:32:32 +0000299
Mike Stump7cbb3602009-02-13 16:01:35 +0000300 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
301 getTypes().ConvertType(getContext().IntTy));
302
Mike Stump9b8a7972009-02-13 15:25:34 +0000303 // struct __block_literal_generic {
Mike Stumpbd65cac2009-02-19 01:01:04 +0000304 // void *__isa;
305 // int __flags;
306 // int __reserved;
307 // void (*__invoke)(void *);
308 // struct __block_descriptor *__descriptor;
Mike Stump9b8a7972009-02-13 15:25:34 +0000309 // };
310 GenericBlockLiteralType = llvm::StructType::get(Int8PtrTy,
Mike Stump7cbb3602009-02-13 16:01:35 +0000311 IntTy,
312 IntTy,
Mike Stump9b8a7972009-02-13 15:25:34 +0000313 Int8PtrTy,
314 BlockDescPtrTy,
315 NULL);
Mike Stumpa5448542009-02-13 15:32:32 +0000316
Mike Stump9b8a7972009-02-13 15:25:34 +0000317 getModule().addTypeName("struct.__block_literal_generic",
318 GenericBlockLiteralType);
Mike Stumpa5448542009-02-13 15:32:32 +0000319
Mike Stump9b8a7972009-02-13 15:25:34 +0000320 return GenericBlockLiteralType;
Anders Carlssonacfde802009-02-12 00:39:25 +0000321}
322
Mike Stumpbd65cac2009-02-19 01:01:04 +0000323const llvm::Type *
324CodeGenModule::getGenericExtendedBlockLiteralType() {
325 if (GenericExtendedBlockLiteralType)
326 return GenericExtendedBlockLiteralType;
327
328 const llvm::Type *Int8PtrTy =
329 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
330
331 const llvm::Type *BlockDescPtrTy =
332 llvm::PointerType::getUnqual(getBlockDescriptorType());
333
334 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
335 getTypes().ConvertType(getContext().IntTy));
336
337 // struct __block_literal_generic {
338 // void *__isa;
339 // int __flags;
340 // int __reserved;
341 // void (*__invoke)(void *);
342 // struct __block_descriptor *__descriptor;
343 // void *__copy_func_helper_decl;
344 // void *__destroy_func_decl;
345 // };
346 GenericExtendedBlockLiteralType = llvm::StructType::get(Int8PtrTy,
347 IntTy,
348 IntTy,
349 Int8PtrTy,
350 BlockDescPtrTy,
351 Int8PtrTy,
352 Int8PtrTy,
353 NULL);
354
355 getModule().addTypeName("struct.__block_literal_extended_generic",
356 GenericExtendedBlockLiteralType);
357
358 return GenericExtendedBlockLiteralType;
359}
360
Mike Stumpa5448542009-02-13 15:32:32 +0000361/// getBlockFunctionType - Given a BlockPointerType, will return the
Anders Carlssonacfde802009-02-12 00:39:25 +0000362/// function type for the block, including the first block literal argument.
363static QualType getBlockFunctionType(ASTContext &Ctx,
Anders Carlssond5cab542009-02-12 17:55:02 +0000364 const BlockPointerType *BPT) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000365 const FunctionProtoType *FTy = cast<FunctionProtoType>(BPT->getPointeeType());
Mike Stumpa5448542009-02-13 15:32:32 +0000366
Anders Carlssonacfde802009-02-12 00:39:25 +0000367 llvm::SmallVector<QualType, 8> Types;
368 Types.push_back(Ctx.getPointerType(Ctx.VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000369
Douglas Gregor72564e72009-02-26 23:50:07 +0000370 for (FunctionProtoType::arg_type_iterator i = FTy->arg_type_begin(),
Anders Carlssonacfde802009-02-12 00:39:25 +0000371 e = FTy->arg_type_end(); i != e; ++i)
372 Types.push_back(*i);
Mike Stumpa5448542009-02-13 15:32:32 +0000373
Anders Carlssonacfde802009-02-12 00:39:25 +0000374 return Ctx.getFunctionType(FTy->getResultType(),
Mike Stumpa5448542009-02-13 15:32:32 +0000375 &Types[0], Types.size(),
Anders Carlssonacfde802009-02-12 00:39:25 +0000376 FTy->isVariadic(), 0);
377}
378
Anders Carlssond5cab542009-02-12 17:55:02 +0000379RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpa5448542009-02-13 15:32:32 +0000380 const BlockPointerType *BPT =
Anders Carlssonacfde802009-02-12 00:39:25 +0000381 E->getCallee()->getType()->getAsBlockPointerType();
Mike Stumpa5448542009-02-13 15:32:32 +0000382
Anders Carlssonacfde802009-02-12 00:39:25 +0000383 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
384
385 // Get a pointer to the generic block literal.
386 const llvm::Type *BlockLiteralTy =
Mike Stump9b8a7972009-02-13 15:25:34 +0000387 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssonacfde802009-02-12 00:39:25 +0000388
389 // Bitcast the callee to a block literal.
Mike Stumpa5448542009-02-13 15:32:32 +0000390 llvm::Value *BlockLiteral =
Anders Carlssonacfde802009-02-12 00:39:25 +0000391 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
392
393 // Get the function pointer from the literal.
394 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Mike Stump20733cd2009-02-22 13:27:11 +0000395 llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp");
Anders Carlssonacfde802009-02-12 00:39:25 +0000396
397 // Cast the function pointer to the right type.
Mike Stumpa5448542009-02-13 15:32:32 +0000398 const llvm::Type *BlockFTy =
Anders Carlssonacfde802009-02-12 00:39:25 +0000399 ConvertType(getBlockFunctionType(getContext(), BPT));
400 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
401 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
402
Mike Stumpa5448542009-02-13 15:32:32 +0000403 BlockLiteral =
404 Builder.CreateBitCast(BlockLiteral,
Anders Carlssonacfde802009-02-12 00:39:25 +0000405 llvm::PointerType::getUnqual(llvm::Type::Int8Ty),
406 "tmp");
Mike Stumpa5448542009-02-13 15:32:32 +0000407
Anders Carlssonacfde802009-02-12 00:39:25 +0000408 // Add the block literal.
409 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
410 CallArgList Args;
411 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000412
Anders Carlssonacfde802009-02-12 00:39:25 +0000413 // And the rest of the arguments.
Mike Stumpa5448542009-02-13 15:32:32 +0000414 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
Anders Carlssonacfde802009-02-12 00:39:25 +0000415 i != e; ++i)
Mike Stumpa5448542009-02-13 15:32:32 +0000416 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i),
Anders Carlssonacfde802009-02-12 00:39:25 +0000417 i->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000418
Anders Carlssonacfde802009-02-12 00:39:25 +0000419 // And call the block.
Mike Stumpa5448542009-02-13 15:32:32 +0000420 return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args),
Anders Carlssonacfde802009-02-12 00:39:25 +0000421 Func, Args);
422}
Anders Carlssond5cab542009-02-12 17:55:02 +0000423
Mike Stump67a64482009-02-14 22:16:35 +0000424llvm::Constant *
Mike Stump30395dd2009-02-14 22:49:33 +0000425CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlssond5cab542009-02-12 17:55:02 +0000426 // Generate the block descriptor.
427 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7cbb3602009-02-13 16:01:35 +0000428 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
429 getTypes().ConvertType(getContext().IntTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000430
Anders Carlssond5cab542009-02-12 17:55:02 +0000431 llvm::Constant *DescriptorFields[2];
Mike Stumpa5448542009-02-13 15:32:32 +0000432
Anders Carlssond5cab542009-02-12 17:55:02 +0000433 // Reserved
434 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000435
Anders Carlssond5cab542009-02-12 17:55:02 +0000436 // Block literal size. For global blocks we just use the size of the generic
437 // block literal struct.
Mike Stumpa5448542009-02-13 15:32:32 +0000438 uint64_t BlockLiteralSize =
Mike Stump9b8a7972009-02-13 15:25:34 +0000439 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Anders Carlssond5cab542009-02-12 17:55:02 +0000440 DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpa5448542009-02-13 15:32:32 +0000441
442 llvm::Constant *DescriptorStruct =
Anders Carlssond5cab542009-02-12 17:55:02 +0000443 llvm::ConstantStruct::get(&DescriptorFields[0], 2);
Mike Stumpa5448542009-02-13 15:32:32 +0000444
Anders Carlssond5cab542009-02-12 17:55:02 +0000445 llvm::GlobalVariable *Descriptor =
446 new llvm::GlobalVariable(DescriptorStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000447 llvm::GlobalVariable::InternalLinkage,
448 DescriptorStruct, "__block_descriptor_global",
Anders Carlssond5cab542009-02-12 17:55:02 +0000449 &getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000450
Anders Carlssond5cab542009-02-12 17:55:02 +0000451 // Generate the constants for the block literal.
452 llvm::Constant *LiteralFields[5];
Mike Stumpa5448542009-02-13 15:32:32 +0000453
Mike Stump67a64482009-02-14 22:16:35 +0000454 CodeGenFunction::BlockInfo Info(0, n);
Mike Stump8a2b4b12009-02-25 23:33:13 +0000455 uint64_t subBlockSize, subBlockAlign;
Mike Stumpa99038c2009-02-28 09:07:16 +0000456 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000457 llvm::Function *Fn
Mike Stump8a2b4b12009-02-25 23:33:13 +0000458 = CodeGenFunction(*this).GenerateBlockFunction(BE, Info, subBlockSize,
459 subBlockAlign,
460 subBlockDeclRefDecls);
Mike Stump4e7a1f72009-02-21 20:00:35 +0000461 assert(subBlockSize == BlockLiteralSize
462 && "no imports allowed for global block");
Mike Stumpa5448542009-02-13 15:32:32 +0000463
Anders Carlssond5cab542009-02-12 17:55:02 +0000464 // isa
Mike Stumpf99f1d02009-02-13 17:23:42 +0000465 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpa5448542009-02-13 15:32:32 +0000466
Anders Carlssond5cab542009-02-12 17:55:02 +0000467 // Flags
Mike Stumpe5fee252009-02-13 16:19:19 +0000468 LiteralFields[1] = llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL);
Mike Stumpa5448542009-02-13 15:32:32 +0000469
Anders Carlssond5cab542009-02-12 17:55:02 +0000470 // Reserved
Mike Stump7cbb3602009-02-13 16:01:35 +0000471 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpa5448542009-02-13 15:32:32 +0000472
Anders Carlssond5cab542009-02-12 17:55:02 +0000473 // Function
474 LiteralFields[3] = Fn;
Mike Stumpa5448542009-02-13 15:32:32 +0000475
Anders Carlssond5cab542009-02-12 17:55:02 +0000476 // Descriptor
477 LiteralFields[4] = Descriptor;
Mike Stumpa5448542009-02-13 15:32:32 +0000478
479 llvm::Constant *BlockLiteralStruct =
Anders Carlssond5cab542009-02-12 17:55:02 +0000480 llvm::ConstantStruct::get(&LiteralFields[0], 5);
Mike Stumpa5448542009-02-13 15:32:32 +0000481
482 llvm::GlobalVariable *BlockLiteral =
Anders Carlssond5cab542009-02-12 17:55:02 +0000483 new llvm::GlobalVariable(BlockLiteralStruct->getType(), true,
Mike Stumpa5448542009-02-13 15:32:32 +0000484 llvm::GlobalVariable::InternalLinkage,
485 BlockLiteralStruct, "__block_literal_global",
Anders Carlssond5cab542009-02-12 17:55:02 +0000486 &getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000487
Anders Carlssond5cab542009-02-12 17:55:02 +0000488 return BlockLiteral;
489}
490
Mike Stump4e7a1f72009-02-21 20:00:35 +0000491llvm::Value *CodeGenFunction::LoadBlockStruct() {
492 return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self");
493}
494
Chris Lattner161d36d2009-02-28 19:01:03 +0000495llvm::Function *CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
Mike Stump4e7a1f72009-02-21 20:00:35 +0000496 const BlockInfo& Info,
Mike Stump8a2b4b12009-02-25 23:33:13 +0000497 uint64_t &Size,
498 uint64_t &Align,
Mike Stumpa99038c2009-02-28 09:07:16 +0000499 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000500 const FunctionProtoType *FTy =
Chris Lattner161d36d2009-02-28 19:01:03 +0000501 cast<FunctionProtoType>(BExpr->getFunctionType());
Mike Stumpa5448542009-02-13 15:32:32 +0000502
Anders Carlssond5cab542009-02-12 17:55:02 +0000503 FunctionArgList Args;
Mike Stumpa5448542009-02-13 15:32:32 +0000504
Chris Lattner161d36d2009-02-28 19:01:03 +0000505 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlssond5cab542009-02-12 17:55:02 +0000506
507 // FIXME: This leaks
Mike Stumpa5448542009-02-13 15:32:32 +0000508 ImplicitParamDecl *SelfDecl =
Anders Carlssond5cab542009-02-12 17:55:02 +0000509 ImplicitParamDecl::Create(getContext(), 0,
510 SourceLocation(), 0,
511 getContext().getPointerType(getContext().VoidTy));
Mike Stumpa5448542009-02-13 15:32:32 +0000512
Anders Carlssond5cab542009-02-12 17:55:02 +0000513 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump4e7a1f72009-02-21 20:00:35 +0000514 BlockStructDecl = SelfDecl;
Mike Stumpa5448542009-02-13 15:32:32 +0000515
516 for (BlockDecl::param_iterator i = BD->param_begin(),
Anders Carlssond5cab542009-02-12 17:55:02 +0000517 e = BD->param_end(); i != e; ++i)
Mike Stump19050612009-02-17 23:25:52 +0000518 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpa5448542009-02-13 15:32:32 +0000519
520 const CGFunctionInfo &FI =
Anders Carlssond5cab542009-02-12 17:55:02 +0000521 CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args);
522
Mike Stump67a64482009-02-14 22:16:35 +0000523 std::string Name = std::string("__") + Info.Name + "_block_invoke_";
Anders Carlssond5cab542009-02-12 17:55:02 +0000524 CodeGenTypes &Types = CGM.getTypes();
525 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic());
Mike Stumpa5448542009-02-13 15:32:32 +0000526
527 llvm::Function *Fn =
Anders Carlssond5cab542009-02-12 17:55:02 +0000528 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
529 Name,
530 &CGM.getModule());
Mike Stumpa5448542009-02-13 15:32:32 +0000531
532 StartFunction(BD, FTy->getResultType(), Fn, Args,
Chris Lattner161d36d2009-02-28 19:01:03 +0000533 BExpr->getBody()->getLocEnd());
534 EmitStmt(BExpr->getBody());
535 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlssond5cab542009-02-12 17:55:02 +0000536
Mike Stump8a2b4b12009-02-25 23:33:13 +0000537 // The runtime needs a minimum alignment of a void *.
538 uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
539 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign);
540
Mike Stump4e7a1f72009-02-21 20:00:35 +0000541 Size = BlockOffset;
Mike Stump8a2b4b12009-02-25 23:33:13 +0000542 Align = BlockAlign;
543 subBlockDeclRefDecls = BlockDeclRefDecls;
Mike Stump4e7a1f72009-02-21 20:00:35 +0000544
Anders Carlssond5cab542009-02-12 17:55:02 +0000545 return Fn;
546}
Mike Stumpa99038c2009-02-28 09:07:16 +0000547
548uint64_t CodeGenFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
549 const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
550
551 uint64_t Size = getContext().getTypeSize(D->getType()) / 8;
552 uint64_t Align = getContext().getDeclAlignInBytes(D);
553
554 if (BDRE->isByRef()) {
555 Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8;
556 Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
557 }
558
559 assert ((Align > 0) && "alignment must be 1 byte or more");
560
561 uint64_t OldOffset = BlockOffset;
562
563 // Ensure proper alignment, even if it means we have to have a gap
564 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align);
565 BlockAlign = std::max(Align, BlockAlign);
566
567 uint64_t Pad = BlockOffset - OldOffset;
568 if (Pad) {
569 llvm::ArrayType::get(llvm::Type::Int8Ty, Pad);
570 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
571 llvm::APInt(32, Pad),
572 ArrayType::Normal, 0);
573 ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
574 0, QualType(PadTy), VarDecl::None,
575 SourceLocation());
576 Expr *E;
577 E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
578 SourceLocation(), false, false);
579 BlockDeclRefDecls.push_back(E);
580 }
581 BlockDeclRefDecls.push_back(BDRE);
582
583 BlockOffset += Size;
584 return BlockOffset-Size;
585}