blob: e50b320a0cd21bde799e089d0649afd2e55bcff8 [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
Mike Stumpd55240e2009-02-19 01:01:04 +0000135// FIXME: Push most into CGM, passing down a few bits, like current
136// function name.
Mike Stumpf1711822009-02-25 23:33:13 +0000137llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Mike Stumpb95bc002009-02-13 16:19:19 +0000138
Anders Carlsson6cf64be2009-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 Stumpb95bc002009-02-13 16:19:19 +0000147 std::vector<llvm::Constant*> Elts;
148 llvm::Constant *C;
Mike Stumpf1711822009-02-25 23:33:13 +0000149 llvm::Value *V;
Mike Stumpb95bc002009-02-13 16:19:19 +0000150
Mike Stumpb95bc002009-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 Stump92ea8882009-02-13 20:17:16 +0000158 // __isa
Mike Stumpeb3a5ca2009-02-13 19:29:27 +0000159 C = CGM.getNSConcreteStackBlock();
Mike Stumpeb3a5ca2009-02-13 19:29:27 +0000160 const llvm::PointerType *PtrToInt8Ty
161 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpb95bc002009-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 Stumpd55240e2009-02-19 01:01:04 +0000175 // __invoke
Mike Stumpf1711822009-02-25 23:33:13 +0000176 uint64_t subBlockSize, subBlockAlign;
Mike Stump2b6933f2009-02-28 09:07:16 +0000177 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Mike Stumpfca5da02009-02-21 20:00:35 +0000178 llvm::Function *Fn
Mike Stumpf1711822009-02-25 23:33:13 +0000179 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, subBlockSize,
180 subBlockAlign, subBlockDeclRefDecls);
Mike Stump084ba462009-02-14 22:16:35 +0000181 Elts.push_back(Fn);
Mike Stumpb95bc002009-02-13 16:19:19 +0000182
183 // __descriptor
Mike Stumpfca5da02009-02-21 20:00:35 +0000184 Elts.push_back(BuildDescriptorBlockDecl(subBlockSize));
Mike Stumpb95bc002009-02-13 16:19:19 +0000185
Mike Stumpf1711822009-02-25 23:33:13 +0000186 if (subBlockDeclRefDecls.size() == 0) {
Mike Stumpa7db9be2009-03-01 20:07:53 +0000187 // Optimize to being a global block.
188 Elts[0] = CGM.getNSConcreteGlobalBlock();
189 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
190
Mike Stumpf1711822009-02-25 23:33:13 +0000191 C = llvm::ConstantStruct::get(Elts);
192
193 char Name[32];
194 sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
195 C = new llvm::GlobalVariable(C->getType(), true,
196 llvm::GlobalValue::InternalLinkage,
197 C, Name, &CGM.getModule());
198 QualType BPT = BE->getType();
199 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
200 return C;
201 }
202
203 std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size());
204 for (int i=0; i<5; ++i)
205 Types[i] = Elts[i]->getType();
206
Mike Stump2b6933f2009-02-28 09:07:16 +0000207 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
208 const Expr *E = subBlockDeclRefDecls[i];
209 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
210 QualType Ty = E->getType();
211 if (BDRE && BDRE->isByRef())
212 Ty = getContext().getPointerType(Ty);
213 Types[i+5] = ConvertType(Ty);
214 }
Mike Stumpf1711822009-02-25 23:33:13 +0000215
216 llvm::Type *Ty = llvm::StructType::get(Types, true);
217
218 llvm::AllocaInst *A = CreateTempAlloca(Ty);
219 A->setAlignment(subBlockAlign);
220 V = A;
221
222 for (unsigned i=0; i<5; ++i)
223 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
224
225 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
226 {
Mike Stump2b6933f2009-02-28 09:07:16 +0000227 // FIXME: Push const down.
228 Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
229 DeclRefExpr *DR;
230 ValueDecl *VD;
Mike Stumpf1711822009-02-25 23:33:13 +0000231
Mike Stump2b6933f2009-02-28 09:07:16 +0000232 DR = dyn_cast<DeclRefExpr>(E);
233 // Skip padding.
234 if (DR) continue;
235
236 BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
237 VD = BDRE->getDecl();
238
239 // FIXME: I want a better way to do this.
240 if (LocalDeclMap[VD]) {
241 E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD),
242 VD->getType(), SourceLocation(),
243 false, false);
244 }
245 if (BDRE->isByRef())
246 E = new (getContext())
247 UnaryOperator(E, UnaryOperator::AddrOf,
248 getContext().getPointerType(E->getType()),
249 SourceLocation());
250
Mike Stumpf1711822009-02-25 23:33:13 +0000251 llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp");
Mike Stump2b6933f2009-02-28 09:07:16 +0000252 RValue r = EmitAnyExpr(E, Addr, false);
Mike Stumpf1711822009-02-25 23:33:13 +0000253 if (r.isScalar())
254 Builder.CreateStore(r.getScalarVal(), Addr);
255 else if (r.isComplex())
256 // FIXME: implement
257 ErrorUnsupported(BE, "complex in block literal");
258 else if (r.isAggregate())
259 ; // Already created into the destination
260 else
261 assert (0 && "bad block variable");
262 // FIXME: Ensure that the offset created by the backend for
263 // the struct matches the previously computed offset in BlockDecls.
264 }
Mike Stumpb95bc002009-02-13 16:19:19 +0000265 }
266
Mike Stumpd55240e2009-02-19 01:01:04 +0000267 QualType BPT = BE->getType();
Mike Stumpf1711822009-02-25 23:33:13 +0000268 return Builder.CreateBitCast(V, ConvertType(BPT));
Mike Stumpb95bc002009-02-13 16:19:19 +0000269}
270
271
Mike Stump95e54802009-02-13 15:16:56 +0000272const llvm::Type *CodeGenModule::getBlockDescriptorType() {
273 if (BlockDescriptorType)
274 return BlockDescriptorType;
275
Mike Stumpc4ae9632009-02-13 15:32:32 +0000276 const llvm::Type *UnsignedLongTy =
Mike Stump95e54802009-02-13 15:16:56 +0000277 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000278
Mike Stump95e54802009-02-13 15:16:56 +0000279 // struct __block_descriptor {
280 // unsigned long reserved;
281 // unsigned long block_size;
282 // };
Mike Stumpc4ae9632009-02-13 15:32:32 +0000283 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy,
284 UnsignedLongTy,
Mike Stump95e54802009-02-13 15:16:56 +0000285 NULL);
286
287 getModule().addTypeName("struct.__block_descriptor",
288 BlockDescriptorType);
289
290 return BlockDescriptorType;
Anders Carlssond2a889b2009-02-12 00:39:25 +0000291}
292
Mike Stump0dffa462009-02-13 15:25:34 +0000293const llvm::Type *
294CodeGenModule::getGenericBlockLiteralType() {
295 if (GenericBlockLiteralType)
296 return GenericBlockLiteralType;
297
Mike Stumpc4ae9632009-02-13 15:32:32 +0000298 const llvm::Type *Int8PtrTy =
Mike Stump0dffa462009-02-13 15:25:34 +0000299 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000300
301 const llvm::Type *BlockDescPtrTy =
Mike Stump0dffa462009-02-13 15:25:34 +0000302 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000303
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000304 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
305 getTypes().ConvertType(getContext().IntTy));
306
Mike Stump0dffa462009-02-13 15:25:34 +0000307 // struct __block_literal_generic {
Mike Stumpd55240e2009-02-19 01:01:04 +0000308 // void *__isa;
309 // int __flags;
310 // int __reserved;
311 // void (*__invoke)(void *);
312 // struct __block_descriptor *__descriptor;
Mike Stump0dffa462009-02-13 15:25:34 +0000313 // };
314 GenericBlockLiteralType = llvm::StructType::get(Int8PtrTy,
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000315 IntTy,
316 IntTy,
Mike Stump0dffa462009-02-13 15:25:34 +0000317 Int8PtrTy,
318 BlockDescPtrTy,
319 NULL);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000320
Mike Stump0dffa462009-02-13 15:25:34 +0000321 getModule().addTypeName("struct.__block_literal_generic",
322 GenericBlockLiteralType);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000323
Mike Stump0dffa462009-02-13 15:25:34 +0000324 return GenericBlockLiteralType;
Anders Carlssond2a889b2009-02-12 00:39:25 +0000325}
326
Mike Stumpd55240e2009-02-19 01:01:04 +0000327const llvm::Type *
328CodeGenModule::getGenericExtendedBlockLiteralType() {
329 if (GenericExtendedBlockLiteralType)
330 return GenericExtendedBlockLiteralType;
331
332 const llvm::Type *Int8PtrTy =
333 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
334
335 const llvm::Type *BlockDescPtrTy =
336 llvm::PointerType::getUnqual(getBlockDescriptorType());
337
338 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
339 getTypes().ConvertType(getContext().IntTy));
340
341 // struct __block_literal_generic {
342 // void *__isa;
343 // int __flags;
344 // int __reserved;
345 // void (*__invoke)(void *);
346 // struct __block_descriptor *__descriptor;
347 // void *__copy_func_helper_decl;
348 // void *__destroy_func_decl;
349 // };
350 GenericExtendedBlockLiteralType = llvm::StructType::get(Int8PtrTy,
351 IntTy,
352 IntTy,
353 Int8PtrTy,
354 BlockDescPtrTy,
355 Int8PtrTy,
356 Int8PtrTy,
357 NULL);
358
359 getModule().addTypeName("struct.__block_literal_extended_generic",
360 GenericExtendedBlockLiteralType);
361
362 return GenericExtendedBlockLiteralType;
363}
364
Mike Stumpc4ae9632009-02-13 15:32:32 +0000365/// getBlockFunctionType - Given a BlockPointerType, will return the
Anders Carlssond2a889b2009-02-12 00:39:25 +0000366/// function type for the block, including the first block literal argument.
367static QualType getBlockFunctionType(ASTContext &Ctx,
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000368 const BlockPointerType *BPT) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000369 const FunctionProtoType *FTy = cast<FunctionProtoType>(BPT->getPointeeType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000370
Anders Carlssond2a889b2009-02-12 00:39:25 +0000371 llvm::SmallVector<QualType, 8> Types;
372 Types.push_back(Ctx.getPointerType(Ctx.VoidTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000373
Douglas Gregor4fa58902009-02-26 23:50:07 +0000374 for (FunctionProtoType::arg_type_iterator i = FTy->arg_type_begin(),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000375 e = FTy->arg_type_end(); i != e; ++i)
376 Types.push_back(*i);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000377
Anders Carlssond2a889b2009-02-12 00:39:25 +0000378 return Ctx.getFunctionType(FTy->getResultType(),
Mike Stumpc4ae9632009-02-13 15:32:32 +0000379 &Types[0], Types.size(),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000380 FTy->isVariadic(), 0);
381}
382
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000383RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpc4ae9632009-02-13 15:32:32 +0000384 const BlockPointerType *BPT =
Anders Carlssond2a889b2009-02-12 00:39:25 +0000385 E->getCallee()->getType()->getAsBlockPointerType();
Mike Stumpc4ae9632009-02-13 15:32:32 +0000386
Anders Carlssond2a889b2009-02-12 00:39:25 +0000387 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
388
389 // Get a pointer to the generic block literal.
390 const llvm::Type *BlockLiteralTy =
Mike Stump0dffa462009-02-13 15:25:34 +0000391 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssond2a889b2009-02-12 00:39:25 +0000392
393 // Bitcast the callee to a block literal.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000394 llvm::Value *BlockLiteral =
Anders Carlssond2a889b2009-02-12 00:39:25 +0000395 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
396
397 // Get the function pointer from the literal.
398 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Mike Stump39bcc612009-02-22 13:27:11 +0000399 llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp");
Anders Carlssond2a889b2009-02-12 00:39:25 +0000400
401 // Cast the function pointer to the right type.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000402 const llvm::Type *BlockFTy =
Anders Carlssond2a889b2009-02-12 00:39:25 +0000403 ConvertType(getBlockFunctionType(getContext(), BPT));
404 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
405 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
406
Mike Stumpc4ae9632009-02-13 15:32:32 +0000407 BlockLiteral =
408 Builder.CreateBitCast(BlockLiteral,
Anders Carlssond2a889b2009-02-12 00:39:25 +0000409 llvm::PointerType::getUnqual(llvm::Type::Int8Ty),
410 "tmp");
Mike Stumpc4ae9632009-02-13 15:32:32 +0000411
Anders Carlssond2a889b2009-02-12 00:39:25 +0000412 // Add the block literal.
413 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
414 CallArgList Args;
415 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000416
Anders Carlssond2a889b2009-02-12 00:39:25 +0000417 // And the rest of the arguments.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000418 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
Anders Carlssond2a889b2009-02-12 00:39:25 +0000419 i != e; ++i)
Mike Stumpc4ae9632009-02-13 15:32:32 +0000420 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000421 i->getType()));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000422
Anders Carlssond2a889b2009-02-12 00:39:25 +0000423 // And call the block.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000424 return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000425 Func, Args);
426}
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000427
Mike Stump084ba462009-02-14 22:16:35 +0000428llvm::Constant *
Mike Stump4b55c7f2009-02-14 22:49:33 +0000429CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000430 // Generate the block descriptor.
431 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000432 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
433 getTypes().ConvertType(getContext().IntTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000434
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000435 llvm::Constant *DescriptorFields[2];
Mike Stumpc4ae9632009-02-13 15:32:32 +0000436
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000437 // Reserved
438 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000439
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000440 // Block literal size. For global blocks we just use the size of the generic
441 // block literal struct.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000442 uint64_t BlockLiteralSize =
Mike Stump0dffa462009-02-13 15:25:34 +0000443 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000444 DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000445
446 llvm::Constant *DescriptorStruct =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000447 llvm::ConstantStruct::get(&DescriptorFields[0], 2);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000448
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000449 llvm::GlobalVariable *Descriptor =
450 new llvm::GlobalVariable(DescriptorStruct->getType(), true,
Mike Stumpc4ae9632009-02-13 15:32:32 +0000451 llvm::GlobalVariable::InternalLinkage,
452 DescriptorStruct, "__block_descriptor_global",
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000453 &getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000454
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000455 // Generate the constants for the block literal.
456 llvm::Constant *LiteralFields[5];
Mike Stumpc4ae9632009-02-13 15:32:32 +0000457
Mike Stump084ba462009-02-14 22:16:35 +0000458 CodeGenFunction::BlockInfo Info(0, n);
Mike Stumpf1711822009-02-25 23:33:13 +0000459 uint64_t subBlockSize, subBlockAlign;
Mike Stump2b6933f2009-02-28 09:07:16 +0000460 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Mike Stumpfca5da02009-02-21 20:00:35 +0000461 llvm::Function *Fn
Mike Stumpf1711822009-02-25 23:33:13 +0000462 = CodeGenFunction(*this).GenerateBlockFunction(BE, Info, subBlockSize,
463 subBlockAlign,
464 subBlockDeclRefDecls);
Mike Stumpfca5da02009-02-21 20:00:35 +0000465 assert(subBlockSize == BlockLiteralSize
466 && "no imports allowed for global block");
Mike Stumpc4ae9632009-02-13 15:32:32 +0000467
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000468 // isa
Mike Stump5f87e9d2009-02-13 17:23:42 +0000469 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpc4ae9632009-02-13 15:32:32 +0000470
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000471 // Flags
Anders Carlsson63810c62009-03-01 21:09:29 +0000472 LiteralFields[1] =
473 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000474
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000475 // Reserved
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000476 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000477
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000478 // Function
479 LiteralFields[3] = Fn;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000480
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000481 // Descriptor
482 LiteralFields[4] = Descriptor;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000483
484 llvm::Constant *BlockLiteralStruct =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000485 llvm::ConstantStruct::get(&LiteralFields[0], 5);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000486
487 llvm::GlobalVariable *BlockLiteral =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000488 new llvm::GlobalVariable(BlockLiteralStruct->getType(), true,
Mike Stumpc4ae9632009-02-13 15:32:32 +0000489 llvm::GlobalVariable::InternalLinkage,
490 BlockLiteralStruct, "__block_literal_global",
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000491 &getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000492
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000493 return BlockLiteral;
494}
495
Mike Stumpfca5da02009-02-21 20:00:35 +0000496llvm::Value *CodeGenFunction::LoadBlockStruct() {
497 return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self");
498}
499
Chris Lattner8130e7f2009-02-28 19:01:03 +0000500llvm::Function *CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
Mike Stumpfca5da02009-02-21 20:00:35 +0000501 const BlockInfo& Info,
Mike Stumpf1711822009-02-25 23:33:13 +0000502 uint64_t &Size,
503 uint64_t &Align,
Mike Stump2b6933f2009-02-28 09:07:16 +0000504 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000505 const FunctionProtoType *FTy =
Chris Lattner8130e7f2009-02-28 19:01:03 +0000506 cast<FunctionProtoType>(BExpr->getFunctionType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000507
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000508 FunctionArgList Args;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000509
Chris Lattner8130e7f2009-02-28 19:01:03 +0000510 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000511
512 // FIXME: This leaks
Mike Stumpc4ae9632009-02-13 15:32:32 +0000513 ImplicitParamDecl *SelfDecl =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000514 ImplicitParamDecl::Create(getContext(), 0,
515 SourceLocation(), 0,
516 getContext().getPointerType(getContext().VoidTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000517
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000518 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stumpfca5da02009-02-21 20:00:35 +0000519 BlockStructDecl = SelfDecl;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000520
521 for (BlockDecl::param_iterator i = BD->param_begin(),
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000522 e = BD->param_end(); i != e; ++i)
Mike Stump459207f2009-02-17 23:25:52 +0000523 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000524
525 const CGFunctionInfo &FI =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000526 CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args);
527
Mike Stump084ba462009-02-14 22:16:35 +0000528 std::string Name = std::string("__") + Info.Name + "_block_invoke_";
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000529 CodeGenTypes &Types = CGM.getTypes();
530 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000531
532 llvm::Function *Fn =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000533 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
534 Name,
535 &CGM.getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000536
537 StartFunction(BD, FTy->getResultType(), Fn, Args,
Chris Lattner8130e7f2009-02-28 19:01:03 +0000538 BExpr->getBody()->getLocEnd());
539 EmitStmt(BExpr->getBody());
540 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000541
Mike Stumpf1711822009-02-25 23:33:13 +0000542 // The runtime needs a minimum alignment of a void *.
543 uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
544 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign);
545
Mike Stumpfca5da02009-02-21 20:00:35 +0000546 Size = BlockOffset;
Mike Stumpf1711822009-02-25 23:33:13 +0000547 Align = BlockAlign;
548 subBlockDeclRefDecls = BlockDeclRefDecls;
Mike Stumpfca5da02009-02-21 20:00:35 +0000549
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000550 return Fn;
551}
Mike Stump2b6933f2009-02-28 09:07:16 +0000552
553uint64_t CodeGenFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
554 const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
555
556 uint64_t Size = getContext().getTypeSize(D->getType()) / 8;
557 uint64_t Align = getContext().getDeclAlignInBytes(D);
558
559 if (BDRE->isByRef()) {
560 Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8;
561 Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
562 }
563
564 assert ((Align > 0) && "alignment must be 1 byte or more");
565
566 uint64_t OldOffset = BlockOffset;
567
568 // Ensure proper alignment, even if it means we have to have a gap
569 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align);
570 BlockAlign = std::max(Align, BlockAlign);
571
572 uint64_t Pad = BlockOffset - OldOffset;
573 if (Pad) {
574 llvm::ArrayType::get(llvm::Type::Int8Ty, Pad);
575 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
576 llvm::APInt(32, Pad),
577 ArrayType::Normal, 0);
578 ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
579 0, QualType(PadTy), VarDecl::None,
580 SourceLocation());
581 Expr *E;
582 E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
583 SourceLocation(), false, false);
584 BlockDeclRefDecls.push_back(E);
585 }
586 BlockDeclRefDecls.push_back(BDRE);
587
588 BlockOffset += Size;
589 return BlockOffset-Size;
590}