blob: 2f1fba02c72dda0de42aadf5c06c96f9a9840799 [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
Mike Stumpb3a6fac2009-03-04 13:17:22 +000024// Temporary code to enable testing of __block variables
25// #include "clang/Frontend/CompileOptions.h"
26#include "llvm/Support/CommandLine.h"
27static llvm::cl::opt<bool>
28Enable__block("f__block",
29 // See all the FIXMEs for the various work that needs to be done
30 llvm::cl::desc("temporary option to turn on __block precessing "
31 "even though the code isn't done yet"),
32 llvm::cl::ValueDisallowed, llvm::cl::AllowInverse,
33 llvm::cl::ZeroOrMore);
34
35
Mike Stumpfca5da02009-02-21 20:00:35 +000036llvm::Constant *CodeGenFunction::BuildDescriptorBlockDecl(uint64_t Size) {
Mike Stumpb95bc002009-02-13 16:19:19 +000037 const llvm::PointerType *PtrToInt8Ty
38 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpff8f0872009-02-13 16:55:51 +000039 const llvm::Type *UnsignedLongTy
40 = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpb95bc002009-02-13 16:19:19 +000041 llvm::Constant *C;
42 std::vector<llvm::Constant*> Elts;
43
44 // reserved
Mike Stumpff8f0872009-02-13 16:55:51 +000045 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpb95bc002009-02-13 16:19:19 +000046 Elts.push_back(C);
47
48 // Size
Mike Stump139c3962009-02-21 20:07:44 +000049 // FIXME: What is the right way to say this doesn't fit? We should give
50 // a user diagnostic in that case. Better fix would be to change the
51 // API to size_t.
Mike Stumpfca5da02009-02-21 20:00:35 +000052 C = llvm::ConstantInt::get(UnsignedLongTy, Size);
Mike Stumpb95bc002009-02-13 16:19:19 +000053 Elts.push_back(C);
54
55 if (BlockHasCopyDispose) {
56 // copy_func_helper_decl
Mike Stumpfca5da02009-02-21 20:00:35 +000057 // FIXME: implement
Mike Stumpff8f0872009-02-13 16:55:51 +000058 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpb95bc002009-02-13 16:19:19 +000059 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
60 Elts.push_back(C);
61
62 // destroy_func_decl
Mike Stumpfca5da02009-02-21 20:00:35 +000063 // FIXME: implement
Mike Stumpff8f0872009-02-13 16:55:51 +000064 C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Mike Stumpb95bc002009-02-13 16:19:19 +000065 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
66 Elts.push_back(C);
67 }
68
69 C = llvm::ConstantStruct::get(Elts);
70
Mike Stumpb95bc002009-02-13 16:19:19 +000071 C = new llvm::GlobalVariable(C->getType(), true,
72 llvm::GlobalValue::InternalLinkage,
Mike Stump92ea8882009-02-13 20:17:16 +000073 C, "__block_descriptor_tmp", &CGM.getModule());
Mike Stumpb95bc002009-02-13 16:19:19 +000074 return C;
75}
76
Mike Stump5f87e9d2009-02-13 17:23:42 +000077llvm::Constant *CodeGenModule::getNSConcreteGlobalBlock() {
78 if (NSConcreteGlobalBlock)
79 return NSConcreteGlobalBlock;
80
81 const llvm::PointerType *PtrToInt8Ty
82 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stump56447902009-02-13 19:38:12 +000083 // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
Mike Stump5f87e9d2009-02-13 17:23:42 +000084 // same thing as CreateRuntimeFunction if there's already a variable with
85 // the same name.
86 NSConcreteGlobalBlock
87 = new llvm::GlobalVariable(PtrToInt8Ty, false,
88 llvm::GlobalValue::ExternalLinkage,
89 0, "_NSConcreteGlobalBlock",
90 &getModule());
91
92 return NSConcreteGlobalBlock;
93}
94
Mike Stumpeb3a5ca2009-02-13 19:29:27 +000095llvm::Constant *CodeGenModule::getNSConcreteStackBlock() {
96 if (NSConcreteStackBlock)
97 return NSConcreteStackBlock;
98
99 const llvm::PointerType *PtrToInt8Ty
100 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stump56447902009-02-13 19:38:12 +0000101 // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
Mike Stumpeb3a5ca2009-02-13 19:29:27 +0000102 // same thing as CreateRuntimeFunction if there's already a variable with
103 // the same name.
104 NSConcreteStackBlock
105 = new llvm::GlobalVariable(PtrToInt8Ty, false,
106 llvm::GlobalValue::ExternalLinkage,
107 0, "_NSConcreteStackBlock",
108 &getModule());
109
110 return NSConcreteStackBlock;
111}
112
Anders Carlsson6cf64be2009-03-01 01:09:12 +0000113static void CollectBlockDeclRefInfo(const Stmt *S,
114 CodeGenFunction::BlockInfo &Info) {
115 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
116 I != E; ++I)
Daniel Dunbar56941d32009-03-02 07:00:57 +0000117 if (*I)
118 CollectBlockDeclRefInfo(*I, Info);
Anders Carlsson6cf64be2009-03-01 01:09:12 +0000119
120 if (const BlockDeclRefExpr *DE = dyn_cast<BlockDeclRefExpr>(S)) {
121 // FIXME: Handle enums.
122 if (isa<FunctionDecl>(DE->getDecl()))
123 return;
124
125 if (DE->isByRef())
126 Info.ByRefDeclRefs.push_back(DE);
127 else
128 Info.ByCopyDeclRefs.push_back(DE);
129 }
130}
131
132/// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block
133/// can be declared as a global variable instead of on the stack.
134static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info)
135{
136 return Info.ByRefDeclRefs.empty() && Info.ByCopyDeclRefs.empty();
137}
138
Mike Stumpd55240e2009-02-19 01:01:04 +0000139// FIXME: Push most into CGM, passing down a few bits, like current
140// function name.
Mike Stumpf1711822009-02-25 23:33:13 +0000141llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Mike Stumpb95bc002009-02-13 16:19:19 +0000142
Anders Carlsson6cf64be2009-03-01 01:09:12 +0000143 std::string Name = CurFn->getName();
144 CodeGenFunction::BlockInfo Info(0, Name.c_str());
145 CollectBlockDeclRefInfo(BE->getBody(), Info);
146
147 // Check if the block can be global.
Mike Stumpad9605d2009-03-04 03:23:46 +0000148 // FIXME: This test doesn't work for nested blocks yet. Longer
149 // term, I'd like to just have one code path. We should move
150 // this function into CGM and pass CGF, then we can just check to
151 // see if CGF is 0.
152 if (0 && CanBlockBeGlobal(Info))
Anders Carlsson6cf64be2009-03-01 01:09:12 +0000153 return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
154
Mike Stumpb95bc002009-02-13 16:19:19 +0000155 std::vector<llvm::Constant*> Elts;
156 llvm::Constant *C;
Mike Stumpf1711822009-02-25 23:33:13 +0000157 llvm::Value *V;
Mike Stumpb95bc002009-02-13 16:19:19 +0000158
Mike Stumpb95bc002009-02-13 16:19:19 +0000159 {
160 // C = BuildBlockStructInitlist();
161 unsigned int flags = BLOCK_HAS_DESCRIPTOR;
162
163 if (BlockHasCopyDispose)
164 flags |= BLOCK_HAS_COPY_DISPOSE;
165
Mike Stump92ea8882009-02-13 20:17:16 +0000166 // __isa
Mike Stumpeb3a5ca2009-02-13 19:29:27 +0000167 C = CGM.getNSConcreteStackBlock();
Mike Stumpeb3a5ca2009-02-13 19:29:27 +0000168 const llvm::PointerType *PtrToInt8Ty
169 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpb95bc002009-02-13 16:19:19 +0000170 C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
171 Elts.push_back(C);
172
173 // __flags
174 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
175 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
176 C = llvm::ConstantInt::get(IntTy, flags);
177 Elts.push_back(C);
178
179 // __reserved
180 C = llvm::ConstantInt::get(IntTy, 0);
181 Elts.push_back(C);
182
Mike Stumpd55240e2009-02-19 01:01:04 +0000183 // __invoke
Mike Stumpf1711822009-02-25 23:33:13 +0000184 uint64_t subBlockSize, subBlockAlign;
Mike Stump2b6933f2009-02-28 09:07:16 +0000185 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Mike Stumpfca5da02009-02-21 20:00:35 +0000186 llvm::Function *Fn
Mike Stumpf1711822009-02-25 23:33:13 +0000187 = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, subBlockSize,
188 subBlockAlign, subBlockDeclRefDecls);
Mike Stump084ba462009-02-14 22:16:35 +0000189 Elts.push_back(Fn);
Mike Stumpb95bc002009-02-13 16:19:19 +0000190
191 // __descriptor
Mike Stumpfca5da02009-02-21 20:00:35 +0000192 Elts.push_back(BuildDescriptorBlockDecl(subBlockSize));
Mike Stumpb95bc002009-02-13 16:19:19 +0000193
Mike Stumpf1711822009-02-25 23:33:13 +0000194 if (subBlockDeclRefDecls.size() == 0) {
Mike Stumpa7db9be2009-03-01 20:07:53 +0000195 // Optimize to being a global block.
196 Elts[0] = CGM.getNSConcreteGlobalBlock();
197 Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
198
Mike Stumpf1711822009-02-25 23:33:13 +0000199 C = llvm::ConstantStruct::get(Elts);
200
201 char Name[32];
202 sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
203 C = new llvm::GlobalVariable(C->getType(), true,
204 llvm::GlobalValue::InternalLinkage,
205 C, Name, &CGM.getModule());
206 QualType BPT = BE->getType();
207 C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
208 return C;
209 }
210
211 std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size());
212 for (int i=0; i<5; ++i)
213 Types[i] = Elts[i]->getType();
214
Mike Stump2b6933f2009-02-28 09:07:16 +0000215 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
216 const Expr *E = subBlockDeclRefDecls[i];
217 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
218 QualType Ty = E->getType();
Mike Stumpad9605d2009-03-04 03:23:46 +0000219 if (BDRE && BDRE->isByRef()) {
220 uint64_t Align = getContext().getDeclAlignInBytes(BDRE->getDecl());
221 Types[i+5] = llvm::PointerType::get(BuildByRefType(Ty, Align), 0);
222 } else
223 Types[i+5] = ConvertType(Ty);
Mike Stump2b6933f2009-02-28 09:07:16 +0000224 }
Mike Stumpf1711822009-02-25 23:33:13 +0000225
226 llvm::Type *Ty = llvm::StructType::get(Types, true);
227
228 llvm::AllocaInst *A = CreateTempAlloca(Ty);
229 A->setAlignment(subBlockAlign);
230 V = A;
231
232 for (unsigned i=0; i<5; ++i)
233 Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
234
235 for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
236 {
Mike Stump2b6933f2009-02-28 09:07:16 +0000237 // FIXME: Push const down.
238 Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
239 DeclRefExpr *DR;
240 ValueDecl *VD;
Mike Stumpf1711822009-02-25 23:33:13 +0000241
Mike Stump2b6933f2009-02-28 09:07:16 +0000242 DR = dyn_cast<DeclRefExpr>(E);
243 // Skip padding.
244 if (DR) continue;
245
246 BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
247 VD = BDRE->getDecl();
248
Mike Stumpad9605d2009-03-04 03:23:46 +0000249 llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp");
Mike Stump2b6933f2009-02-28 09:07:16 +0000250 // FIXME: I want a better way to do this.
251 if (LocalDeclMap[VD]) {
Mike Stumpad9605d2009-03-04 03:23:46 +0000252 if (BDRE->isByRef()) {
253 const llvm::Type *Ty = Types[i+5];
254 llvm::Value *Loc = LocalDeclMap[VD];
255 Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
256 Loc = Builder.CreateLoad(Loc, false);
257 Loc = Builder.CreateBitCast(Loc, Ty);
258 Builder.CreateStore(Loc, Addr);
259 continue;
260 } else
261 E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD),
262 VD->getType(), SourceLocation(),
263 false, false);
Mike Stump2b6933f2009-02-28 09:07:16 +0000264 }
Mike Stumpad9605d2009-03-04 03:23:46 +0000265 if (BDRE->isByRef()) {
266 // FIXME: __block in nested literals
Mike Stump2b6933f2009-02-28 09:07:16 +0000267 E = new (getContext())
268 UnaryOperator(E, UnaryOperator::AddrOf,
269 getContext().getPointerType(E->getType()),
270 SourceLocation());
Mike Stumpad9605d2009-03-04 03:23:46 +0000271 }
Mike Stump2b6933f2009-02-28 09:07:16 +0000272
Mike Stump2b6933f2009-02-28 09:07:16 +0000273 RValue r = EmitAnyExpr(E, Addr, false);
Mike Stumpad9605d2009-03-04 03:23:46 +0000274 if (r.isScalar()) {
275 llvm::Value *Loc = r.getScalarVal();
276 const llvm::Type *Ty = Types[i+5];
277 if (BDRE->isByRef()) {
278 Loc = Builder.CreateBitCast(Loc, Ty);
279 Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
280 Loc = Builder.CreateBitCast(Loc, Ty);
281 }
282 Builder.CreateStore(Loc, Addr);
283 } else if (r.isComplex())
Mike Stumpf1711822009-02-25 23:33:13 +0000284 // FIXME: implement
285 ErrorUnsupported(BE, "complex in block literal");
286 else if (r.isAggregate())
287 ; // Already created into the destination
288 else
289 assert (0 && "bad block variable");
290 // FIXME: Ensure that the offset created by the backend for
291 // the struct matches the previously computed offset in BlockDecls.
292 }
Mike Stumpb95bc002009-02-13 16:19:19 +0000293 }
294
Mike Stumpd55240e2009-02-19 01:01:04 +0000295 QualType BPT = BE->getType();
Mike Stumpf1711822009-02-25 23:33:13 +0000296 return Builder.CreateBitCast(V, ConvertType(BPT));
Mike Stumpb95bc002009-02-13 16:19:19 +0000297}
298
299
Mike Stump95e54802009-02-13 15:16:56 +0000300const llvm::Type *CodeGenModule::getBlockDescriptorType() {
301 if (BlockDescriptorType)
302 return BlockDescriptorType;
303
Mike Stumpc4ae9632009-02-13 15:32:32 +0000304 const llvm::Type *UnsignedLongTy =
Mike Stump95e54802009-02-13 15:16:56 +0000305 getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000306
Mike Stump95e54802009-02-13 15:16:56 +0000307 // struct __block_descriptor {
308 // unsigned long reserved;
309 // unsigned long block_size;
310 // };
Mike Stumpc4ae9632009-02-13 15:32:32 +0000311 BlockDescriptorType = llvm::StructType::get(UnsignedLongTy,
312 UnsignedLongTy,
Mike Stump95e54802009-02-13 15:16:56 +0000313 NULL);
314
315 getModule().addTypeName("struct.__block_descriptor",
316 BlockDescriptorType);
317
318 return BlockDescriptorType;
Anders Carlssond2a889b2009-02-12 00:39:25 +0000319}
320
Mike Stump0dffa462009-02-13 15:25:34 +0000321const llvm::Type *
322CodeGenModule::getGenericBlockLiteralType() {
323 if (GenericBlockLiteralType)
324 return GenericBlockLiteralType;
325
Mike Stumpc4ae9632009-02-13 15:32:32 +0000326 const llvm::Type *Int8PtrTy =
Mike Stump0dffa462009-02-13 15:25:34 +0000327 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000328
329 const llvm::Type *BlockDescPtrTy =
Mike Stump0dffa462009-02-13 15:25:34 +0000330 llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000331
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000332 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
333 getTypes().ConvertType(getContext().IntTy));
334
Mike Stump0dffa462009-02-13 15:25:34 +0000335 // struct __block_literal_generic {
Mike Stumpd55240e2009-02-19 01:01:04 +0000336 // void *__isa;
337 // int __flags;
338 // int __reserved;
339 // void (*__invoke)(void *);
340 // struct __block_descriptor *__descriptor;
Mike Stump0dffa462009-02-13 15:25:34 +0000341 // };
342 GenericBlockLiteralType = llvm::StructType::get(Int8PtrTy,
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000343 IntTy,
344 IntTy,
Mike Stump0dffa462009-02-13 15:25:34 +0000345 Int8PtrTy,
346 BlockDescPtrTy,
347 NULL);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000348
Mike Stump0dffa462009-02-13 15:25:34 +0000349 getModule().addTypeName("struct.__block_literal_generic",
350 GenericBlockLiteralType);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000351
Mike Stump0dffa462009-02-13 15:25:34 +0000352 return GenericBlockLiteralType;
Anders Carlssond2a889b2009-02-12 00:39:25 +0000353}
354
Mike Stumpd55240e2009-02-19 01:01:04 +0000355const llvm::Type *
356CodeGenModule::getGenericExtendedBlockLiteralType() {
357 if (GenericExtendedBlockLiteralType)
358 return GenericExtendedBlockLiteralType;
359
360 const llvm::Type *Int8PtrTy =
361 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
362
363 const llvm::Type *BlockDescPtrTy =
364 llvm::PointerType::getUnqual(getBlockDescriptorType());
365
366 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
367 getTypes().ConvertType(getContext().IntTy));
368
369 // struct __block_literal_generic {
370 // void *__isa;
371 // int __flags;
372 // int __reserved;
373 // void (*__invoke)(void *);
374 // struct __block_descriptor *__descriptor;
375 // void *__copy_func_helper_decl;
376 // void *__destroy_func_decl;
377 // };
378 GenericExtendedBlockLiteralType = llvm::StructType::get(Int8PtrTy,
379 IntTy,
380 IntTy,
381 Int8PtrTy,
382 BlockDescPtrTy,
383 Int8PtrTy,
384 Int8PtrTy,
385 NULL);
386
387 getModule().addTypeName("struct.__block_literal_extended_generic",
388 GenericExtendedBlockLiteralType);
389
390 return GenericExtendedBlockLiteralType;
391}
392
Mike Stumpc4ae9632009-02-13 15:32:32 +0000393/// getBlockFunctionType - Given a BlockPointerType, will return the
Anders Carlssond2a889b2009-02-12 00:39:25 +0000394/// function type for the block, including the first block literal argument.
395static QualType getBlockFunctionType(ASTContext &Ctx,
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000396 const BlockPointerType *BPT) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000397 const FunctionProtoType *FTy = cast<FunctionProtoType>(BPT->getPointeeType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000398
Anders Carlssond2a889b2009-02-12 00:39:25 +0000399 llvm::SmallVector<QualType, 8> Types;
400 Types.push_back(Ctx.getPointerType(Ctx.VoidTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000401
Douglas Gregor4fa58902009-02-26 23:50:07 +0000402 for (FunctionProtoType::arg_type_iterator i = FTy->arg_type_begin(),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000403 e = FTy->arg_type_end(); i != e; ++i)
404 Types.push_back(*i);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000405
Anders Carlssond2a889b2009-02-12 00:39:25 +0000406 return Ctx.getFunctionType(FTy->getResultType(),
Mike Stumpc4ae9632009-02-13 15:32:32 +0000407 &Types[0], Types.size(),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000408 FTy->isVariadic(), 0);
409}
410
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000411RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stumpc4ae9632009-02-13 15:32:32 +0000412 const BlockPointerType *BPT =
Anders Carlssond2a889b2009-02-12 00:39:25 +0000413 E->getCallee()->getType()->getAsBlockPointerType();
Mike Stumpc4ae9632009-02-13 15:32:32 +0000414
Anders Carlssond2a889b2009-02-12 00:39:25 +0000415 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
416
417 // Get a pointer to the generic block literal.
418 const llvm::Type *BlockLiteralTy =
Mike Stump0dffa462009-02-13 15:25:34 +0000419 llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
Anders Carlssond2a889b2009-02-12 00:39:25 +0000420
421 // Bitcast the callee to a block literal.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000422 llvm::Value *BlockLiteral =
Anders Carlssond2a889b2009-02-12 00:39:25 +0000423 Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
424
425 // Get the function pointer from the literal.
426 llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
Mike Stump39bcc612009-02-22 13:27:11 +0000427 llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp");
Anders Carlssond2a889b2009-02-12 00:39:25 +0000428
429 // Cast the function pointer to the right type.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000430 const llvm::Type *BlockFTy =
Anders Carlssond2a889b2009-02-12 00:39:25 +0000431 ConvertType(getBlockFunctionType(getContext(), BPT));
432 const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
433 Func = Builder.CreateBitCast(Func, BlockFTyPtr);
434
Mike Stumpc4ae9632009-02-13 15:32:32 +0000435 BlockLiteral =
436 Builder.CreateBitCast(BlockLiteral,
Anders Carlssond2a889b2009-02-12 00:39:25 +0000437 llvm::PointerType::getUnqual(llvm::Type::Int8Ty),
438 "tmp");
Mike Stumpc4ae9632009-02-13 15:32:32 +0000439
Anders Carlssond2a889b2009-02-12 00:39:25 +0000440 // Add the block literal.
441 QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
442 CallArgList Args;
443 Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000444
Anders Carlssond2a889b2009-02-12 00:39:25 +0000445 // And the rest of the arguments.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000446 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
Anders Carlssond2a889b2009-02-12 00:39:25 +0000447 i != e; ++i)
Mike Stumpc4ae9632009-02-13 15:32:32 +0000448 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000449 i->getType()));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000450
Anders Carlssond2a889b2009-02-12 00:39:25 +0000451 // And call the block.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000452 return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args),
Anders Carlssond2a889b2009-02-12 00:39:25 +0000453 Func, Args);
454}
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000455
Mike Stumpad9605d2009-03-04 03:23:46 +0000456llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
457 uint64_t &offset = BlockDecls[E->getDecl()];
458
459 const llvm::Type *Ty;
460 Ty = CGM.getTypes().ConvertType(E->getDecl()->getType());
461
462 // FIXME: add support for copy/dispose helpers.
Mike Stumpb3a6fac2009-03-04 13:17:22 +0000463 if (!Enable__block && E->isByRef())
Mike Stumpad9605d2009-03-04 03:23:46 +0000464 ErrorUnsupported(E, "__block variable in block literal");
465 else if (E->getType()->isBlockPointerType())
466 ErrorUnsupported(E, "block pointer in block literal");
467 else if (E->getDecl()->getAttr<ObjCNSObjectAttr>() ||
468 getContext().isObjCNSObjectType(E->getType()))
469 ErrorUnsupported(E, "__attribute__((NSObject)) variable in block "
470 "literal");
471 else if (getContext().isObjCObjectPointerType(E->getType()))
472 ErrorUnsupported(E, "Objective-C variable in block literal");
473
474 // See if we have already allocated an offset for this variable.
475 if (offset == 0) {
476 // if not, allocate one now.
477 offset = getBlockOffset(E);
478 }
479
480 llvm::Value *BlockLiteral = LoadBlockStruct();
481 llvm::Value *V = Builder.CreateGEP(BlockLiteral,
482 llvm::ConstantInt::get(llvm::Type::Int64Ty,
483 offset),
484 "tmp");
485 if (E->isByRef()) {
486 bool needsCopyDispose = BlockRequiresCopying(E->getType());
487 uint64_t Align = getContext().getDeclAlignInBytes(E->getDecl());
488 const llvm::Type *PtrStructTy
489 = llvm::PointerType::get(BuildByRefType(E->getType(), Align), 0);
490 Ty = PtrStructTy;
491 Ty = llvm::PointerType::get(Ty, 0);
492 V = Builder.CreateBitCast(V, Ty);
493 V = Builder.CreateLoad(V, false);
494 V = Builder.CreateStructGEP(V, 1, "forwarding");
495 V = Builder.CreateLoad(V, false);
496 V = Builder.CreateBitCast(V, PtrStructTy);
497 V = Builder.CreateStructGEP(V, needsCopyDispose*2 + 4, "x");
498 } else {
499 Ty = llvm::PointerType::get(Ty, 0);
500 V = Builder.CreateBitCast(V, Ty);
501 }
502 return V;
503}
504
Mike Stump084ba462009-02-14 22:16:35 +0000505llvm::Constant *
Mike Stump4b55c7f2009-02-14 22:49:33 +0000506CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000507 // Generate the block descriptor.
508 const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000509 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
510 getTypes().ConvertType(getContext().IntTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000511
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000512 llvm::Constant *DescriptorFields[2];
Mike Stumpc4ae9632009-02-13 15:32:32 +0000513
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000514 // Reserved
515 DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000516
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000517 // Block literal size. For global blocks we just use the size of the generic
518 // block literal struct.
Mike Stumpc4ae9632009-02-13 15:32:32 +0000519 uint64_t BlockLiteralSize =
Mike Stump0dffa462009-02-13 15:25:34 +0000520 TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000521 DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000522
523 llvm::Constant *DescriptorStruct =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000524 llvm::ConstantStruct::get(&DescriptorFields[0], 2);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000525
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000526 llvm::GlobalVariable *Descriptor =
527 new llvm::GlobalVariable(DescriptorStruct->getType(), true,
Mike Stumpc4ae9632009-02-13 15:32:32 +0000528 llvm::GlobalVariable::InternalLinkage,
529 DescriptorStruct, "__block_descriptor_global",
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000530 &getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000531
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000532 // Generate the constants for the block literal.
533 llvm::Constant *LiteralFields[5];
Mike Stumpc4ae9632009-02-13 15:32:32 +0000534
Mike Stump084ba462009-02-14 22:16:35 +0000535 CodeGenFunction::BlockInfo Info(0, n);
Mike Stumpf1711822009-02-25 23:33:13 +0000536 uint64_t subBlockSize, subBlockAlign;
Mike Stump2b6933f2009-02-28 09:07:16 +0000537 llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
Mike Stumpfca5da02009-02-21 20:00:35 +0000538 llvm::Function *Fn
Mike Stumpf1711822009-02-25 23:33:13 +0000539 = CodeGenFunction(*this).GenerateBlockFunction(BE, Info, subBlockSize,
540 subBlockAlign,
541 subBlockDeclRefDecls);
Mike Stumpfca5da02009-02-21 20:00:35 +0000542 assert(subBlockSize == BlockLiteralSize
543 && "no imports allowed for global block");
Mike Stumpc4ae9632009-02-13 15:32:32 +0000544
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000545 // isa
Mike Stump5f87e9d2009-02-13 17:23:42 +0000546 LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stumpc4ae9632009-02-13 15:32:32 +0000547
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000548 // Flags
Anders Carlsson63810c62009-03-01 21:09:29 +0000549 LiteralFields[1] =
550 llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000551
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000552 // Reserved
Mike Stump7b7cd8b2009-02-13 16:01:35 +0000553 LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000554
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000555 // Function
556 LiteralFields[3] = Fn;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000557
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000558 // Descriptor
559 LiteralFields[4] = Descriptor;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000560
561 llvm::Constant *BlockLiteralStruct =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000562 llvm::ConstantStruct::get(&LiteralFields[0], 5);
Mike Stumpc4ae9632009-02-13 15:32:32 +0000563
564 llvm::GlobalVariable *BlockLiteral =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000565 new llvm::GlobalVariable(BlockLiteralStruct->getType(), true,
Mike Stumpc4ae9632009-02-13 15:32:32 +0000566 llvm::GlobalVariable::InternalLinkage,
567 BlockLiteralStruct, "__block_literal_global",
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000568 &getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000569
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000570 return BlockLiteral;
571}
572
Mike Stumpfca5da02009-02-21 20:00:35 +0000573llvm::Value *CodeGenFunction::LoadBlockStruct() {
574 return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self");
575}
576
Chris Lattner8130e7f2009-02-28 19:01:03 +0000577llvm::Function *CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
Mike Stumpfca5da02009-02-21 20:00:35 +0000578 const BlockInfo& Info,
Mike Stumpf1711822009-02-25 23:33:13 +0000579 uint64_t &Size,
580 uint64_t &Align,
Mike Stump2b6933f2009-02-28 09:07:16 +0000581 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000582 const FunctionProtoType *FTy =
Chris Lattner8130e7f2009-02-28 19:01:03 +0000583 cast<FunctionProtoType>(BExpr->getFunctionType());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000584
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000585 FunctionArgList Args;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000586
Chris Lattner8130e7f2009-02-28 19:01:03 +0000587 const BlockDecl *BD = BExpr->getBlockDecl();
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000588
589 // FIXME: This leaks
Mike Stumpc4ae9632009-02-13 15:32:32 +0000590 ImplicitParamDecl *SelfDecl =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000591 ImplicitParamDecl::Create(getContext(), 0,
592 SourceLocation(), 0,
593 getContext().getPointerType(getContext().VoidTy));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000594
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000595 Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stumpfca5da02009-02-21 20:00:35 +0000596 BlockStructDecl = SelfDecl;
Mike Stumpc4ae9632009-02-13 15:32:32 +0000597
598 for (BlockDecl::param_iterator i = BD->param_begin(),
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000599 e = BD->param_end(); i != e; ++i)
Mike Stump459207f2009-02-17 23:25:52 +0000600 Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stumpc4ae9632009-02-13 15:32:32 +0000601
602 const CGFunctionInfo &FI =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000603 CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args);
604
Mike Stump084ba462009-02-14 22:16:35 +0000605 std::string Name = std::string("__") + Info.Name + "_block_invoke_";
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000606 CodeGenTypes &Types = CGM.getTypes();
607 const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000608
609 llvm::Function *Fn =
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000610 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
611 Name,
612 &CGM.getModule());
Mike Stumpc4ae9632009-02-13 15:32:32 +0000613
614 StartFunction(BD, FTy->getResultType(), Fn, Args,
Chris Lattner8130e7f2009-02-28 19:01:03 +0000615 BExpr->getBody()->getLocEnd());
616 EmitStmt(BExpr->getBody());
617 FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000618
Mike Stumpf1711822009-02-25 23:33:13 +0000619 // The runtime needs a minimum alignment of a void *.
620 uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
621 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign);
622
Mike Stumpfca5da02009-02-21 20:00:35 +0000623 Size = BlockOffset;
Mike Stumpf1711822009-02-25 23:33:13 +0000624 Align = BlockAlign;
625 subBlockDeclRefDecls = BlockDeclRefDecls;
Mike Stumpfca5da02009-02-21 20:00:35 +0000626
Anders Carlsson1f1cd392009-02-12 17:55:02 +0000627 return Fn;
628}
Mike Stump2b6933f2009-02-28 09:07:16 +0000629
630uint64_t CodeGenFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
631 const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
632
633 uint64_t Size = getContext().getTypeSize(D->getType()) / 8;
634 uint64_t Align = getContext().getDeclAlignInBytes(D);
635
636 if (BDRE->isByRef()) {
637 Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8;
638 Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
639 }
640
641 assert ((Align > 0) && "alignment must be 1 byte or more");
642
643 uint64_t OldOffset = BlockOffset;
644
645 // Ensure proper alignment, even if it means we have to have a gap
646 BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align);
647 BlockAlign = std::max(Align, BlockAlign);
648
649 uint64_t Pad = BlockOffset - OldOffset;
650 if (Pad) {
651 llvm::ArrayType::get(llvm::Type::Int8Ty, Pad);
652 QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
653 llvm::APInt(32, Pad),
654 ArrayType::Normal, 0);
655 ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
656 0, QualType(PadTy), VarDecl::None,
657 SourceLocation());
658 Expr *E;
659 E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
660 SourceLocation(), false, false);
661 BlockDeclRefDecls.push_back(E);
662 }
663 BlockDeclRefDecls.push_back(BDRE);
664
665 BlockOffset += Size;
666 return BlockOffset-Size;
667}
Mike Stumpad9605d2009-03-04 03:23:46 +0000668
669llvm::Value *CodeGenFunction::BuildCopyHelper(int flag) {
670 const llvm::PointerType *PtrToInt8Ty
671 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
672 // FIXME: implement
673 llvm::Value *V = llvm::ConstantInt::get(llvm::Type::Int32Ty, 43);
674 V = Builder.CreateIntToPtr(V, PtrToInt8Ty, "tmp");
675 V = Builder.CreateBitCast(V, PtrToInt8Ty, "tmp");
676 return V;
677}
678
679llvm::Value *CodeGenFunction::BuildDestroyHelper(int flag) {
680 const llvm::PointerType *PtrToInt8Ty
681 = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
682 // FIXME: implement
683 llvm::Value *V = llvm::ConstantInt::get(llvm::Type::Int32Ty, 44);
684 V = Builder.CreateIntToPtr(V, PtrToInt8Ty, "tmp");
685 V = Builder.CreateBitCast(V, PtrToInt8Ty, "tmp");
686 return V;
687}