 |
|
 |
|
| Files: |
1 |
|
Branches Taken: |
77.8% |
140 / 180 |
| Generated: |
2010-02-10 01:31 |
|
Branches Executed: |
95.6% |
172 / 180 |
| |
|
Line Coverage: |
93.4% |
495 / 530 |
| |
 |
|
 |
1 : //===--- 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 "CGDebugInfo.h"
15 : #include "CodeGenFunction.h"
16 : #include "CodeGenModule.h"
17 : #include "clang/AST/DeclObjC.h"
18 : #include "llvm/Module.h"
19 : #include "llvm/Target/TargetData.h"
20 : #include <algorithm>
21 :
22 : using namespace clang;
23 : using namespace CodeGen;
24 :
25 : llvm::Constant *CodeGenFunction::
26 : BuildDescriptorBlockDecl(bool BlockHasCopyDispose, CharUnits Size,
27 : const llvm::StructType* Ty,
28 32: std::vector<HelperInfo> *NoteForHelper) {
29 : const llvm::Type *UnsignedLongTy
30 32: = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
31 : llvm::Constant *C;
32 32: std::vector<llvm::Constant*> Elts;
33 :
34 : // reserved
35 32: C = llvm::ConstantInt::get(UnsignedLongTy, 0);
36 32: Elts.push_back(C);
37 :
38 : // Size
39 : // FIXME: What is the right way to say this doesn't fit? We should give
40 : // a user diagnostic in that case. Better fix would be to change the
41 : // API to size_t.
42 32: C = llvm::ConstantInt::get(UnsignedLongTy, Size.getQuantity());
43 32: Elts.push_back(C);
44 :
21: branch 0 taken
11: branch 1 taken
45 32: if (BlockHasCopyDispose) {
46 : // copy_func_helper_decl
47 21: Elts.push_back(BuildCopyHelper(Ty, NoteForHelper));
48 :
49 : // destroy_func_decl
50 21: Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper));
51 : }
52 :
53 32: C = llvm::ConstantStruct::get(VMContext, Elts, false);
54 :
55 : C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
56 : llvm::GlobalValue::InternalLinkage,
57 32: C, "__block_descriptor_tmp");
58 32: return C;
59 : }
60 :
61 15: llvm::Constant *BlockModule::getNSConcreteGlobalBlock() {
10: branch 0 taken
5: branch 1 taken
62 15: if (NSConcreteGlobalBlock == 0)
63 : NSConcreteGlobalBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
64 10: "_NSConcreteGlobalBlock");
65 15: return NSConcreteGlobalBlock;
66 : }
67 :
68 32: llvm::Constant *BlockModule::getNSConcreteStackBlock() {
17: branch 0 taken
15: branch 1 taken
69 32: if (NSConcreteStackBlock == 0)
70 : NSConcreteStackBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
71 17: "_NSConcreteStackBlock");
72 32: return NSConcreteStackBlock;
73 : }
74 :
75 : static void CollectBlockDeclRefInfo(
76 : const Stmt *S, CodeGenFunction::BlockInfo &Info,
77 175: llvm::SmallSet<const DeclContext *, 16> &InnerContexts) {
145: branch 4 taken
175: branch 5 taken
78 320: for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
79 : I != E; ++I)
140: branch 1 taken
5: branch 2 taken
80 145: if (*I)
81 140: CollectBlockDeclRefInfo(*I, Info, InnerContexts);
82 :
83 : // We want to ensure we walk down into block literals so we can find
84 : // all nested BlockDeclRefExprs.
3: branch 1 taken
172: branch 2 taken
85 175: if (const BlockExpr *BE = dyn_cast<BlockExpr>(S)) {
86 3: InnerContexts.insert(cast<DeclContext>(BE->getBlockDecl()));
87 3: CollectBlockDeclRefInfo(BE->getBody(), Info, InnerContexts);
88 : }
89 :
32: branch 1 taken
143: branch 2 taken
90 175: if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) {
91 : // FIXME: Handle enums.
0: branch 2 not taken
32: branch 3 taken
92 32: if (isa<FunctionDecl>(BDRE->getDecl()))
93 0: return;
94 :
95 : // Only Decls that escape are added.
31: branch 3 taken
1: branch 4 taken
96 32: if (!InnerContexts.count(BDRE->getDecl()->getDeclContext()))
97 31: Info.DeclRefs.push_back(BDRE);
98 : }
99 : }
100 :
101 : /// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be
102 : /// declared as a global variable instead of on the stack.
103 0: static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info) {
104 0: return Info.DeclRefs.empty();
105 : }
106 :
107 : /// AllocateAllBlockDeclRefs - Preallocate all nested BlockDeclRefExprs to
108 : /// ensure we can generate the debug information for the parameter for the block
109 : /// invoke function.
110 : static void AllocateAllBlockDeclRefs(const CodeGenFunction::BlockInfo &Info,
111 35: CodeGenFunction *CGF) {
112 : // Always allocate self, as it is often handy in the debugger, even if there
113 : // is no codegen in the block that uses it. This is also useful to always do
114 : // this as if we didn't, we'd have to figure out all code that uses a self
115 : // pointer, including implicit uses.
3: branch 0 taken
32: branch 1 taken
116 35: if (const ObjCMethodDecl *OMD
117 35: = dyn_cast_or_null<ObjCMethodDecl>(CGF->CurFuncDecl)) {
118 3: ImplicitParamDecl *SelfDecl = OMD->getSelfDecl();
119 : BlockDeclRefExpr *BDRE = new (CGF->getContext())
120 : BlockDeclRefExpr(SelfDecl,
3: branch 4 taken
0: branch 5 not taken
121 3: SelfDecl->getType(), SourceLocation(), false);
122 3: CGF->AllocateBlockDecl(BDRE);
123 : }
124 :
125 : // FIXME: Also always forward the this pointer in C++ as well.
126 :
31: branch 1 taken
35: branch 2 taken
127 66: for (size_t i = 0; i < Info.DeclRefs.size(); ++i)
128 31: CGF->AllocateBlockDecl(Info.DeclRefs[i]);
129 35: }
130 :
131 : // FIXME: Push most into CGM, passing down a few bits, like current function
132 : // name.
133 32: llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
134 :
135 32: std::string Name = CurFn->getName();
136 32: CodeGenFunction::BlockInfo Info(0, Name.c_str());
137 32: llvm::SmallSet<const DeclContext *, 16> InnerContexts;
32: branch 1 taken
0: branch 2 not taken
138 32: InnerContexts.insert(BE->getBlockDecl());
139 32: CollectBlockDeclRefInfo(BE->getBody(), Info, InnerContexts);
140 :
141 : // Check if the block can be global.
142 : // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like
143 : // to just have one code path. We should move this function into CGM and pass
144 : // CGF, then we can just check to see if CGF is 0.
145 : if (0 && CanBlockBeGlobal(Info))
146 : return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
147 :
148 32: size_t BlockFields = 5;
149 :
150 32: bool hasIntrospection = CGM.getContext().getLangOptions().BlockIntrospection;
151 :
0: branch 0 not taken
32: branch 1 taken
152 32: if (hasIntrospection) {
153 0: BlockFields++;
154 : }
155 32: std::vector<llvm::Constant*> Elts(BlockFields);
156 :
0: branch 0 not taken
32: branch 1 taken
157 32: if (hasIntrospection) {
158 0: std::string BlockTypeEncoding;
159 0: CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
160 :
161 : Elts[5] = llvm::ConstantExpr::getBitCast(
162 0: CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty);
163 : }
164 :
165 : llvm::Constant *C;
166 : llvm::Value *V;
167 :
168 : {
169 : // C = BuildBlockStructInitlist();
170 32: unsigned int flags = BLOCK_HAS_DESCRIPTOR;
171 :
0: branch 0 not taken
32: branch 1 taken
172 32: if (hasIntrospection)
173 0: flags |= BLOCK_HAS_OBJC_TYPE;
174 :
175 : // We run this first so that we set BlockHasCopyDispose from the entire
176 : // block literal.
177 : // __invoke
178 32: CharUnits subBlockSize;
179 32: CharUnits subBlockAlign;
180 32: llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
181 32: bool subBlockHasCopyDispose = false;
182 : llvm::Function *Fn
183 : = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl,
184 : LocalDeclMap,
185 : subBlockSize,
186 : subBlockAlign,
187 : subBlockDeclRefDecls,
188 32: subBlockHasCopyDispose);
189 32: BlockHasCopyDispose |= subBlockHasCopyDispose;
190 32: Elts[3] = Fn;
191 :
192 : // FIXME: Don't use BlockHasCopyDispose, it is set more often then
193 : // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); }
21: branch 0 taken
11: branch 1 taken
194 32: if (subBlockHasCopyDispose)
195 21: flags |= BLOCK_HAS_COPY_DISPOSE;
196 :
197 : // __isa
198 32: C = CGM.getNSConcreteStackBlock();
199 32: C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
200 32: Elts[0] = C;
201 :
202 : // __flags
203 : const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
204 32: CGM.getTypes().ConvertType(CGM.getContext().IntTy));
205 32: C = llvm::ConstantInt::get(IntTy, flags);
206 32: Elts[1] = C;
207 :
208 : // __reserved
209 32: C = llvm::ConstantInt::get(IntTy, 0);
210 32: Elts[2] = C;
211 :
12: branch 1 taken
20: branch 2 taken
212 32: if (subBlockDeclRefDecls.size() == 0) {
213 : // __descriptor
214 : Elts[4] = BuildDescriptorBlockDecl(subBlockHasCopyDispose, subBlockSize,
215 12: 0, 0);
216 :
217 : // Optimize to being a global block.
218 12: Elts[0] = CGM.getNSConcreteGlobalBlock();
219 12: Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
220 :
221 12: C = llvm::ConstantStruct::get(VMContext, Elts, false);
222 :
223 : C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
224 : llvm::GlobalValue::InternalLinkage, C,
225 : "__block_holder_tmp_" +
226 12: llvm::Twine(CGM.getGlobalUniqueCount()));
227 12: QualType BPT = BE->getType();
228 12: C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
229 12: return C;
230 : }
231 :
232 20: std::vector<const llvm::Type *> Types(BlockFields+subBlockDeclRefDecls.size());
80: branch 0 taken
20: branch 1 taken
233 100: for (int i=0; i<4; ++i)
234 80: Types[i] = Elts[i]->getType();
235 20: Types[4] = PtrToInt8Ty;
0: branch 0 not taken
20: branch 1 taken
236 20: if (hasIntrospection)
237 0: Types[5] = PtrToInt8Ty;
238 :
31: branch 1 taken
20: branch 2 taken
239 51: for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
240 31: const Expr *E = subBlockDeclRefDecls[i];
241 31: const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
242 31: QualType Ty = E->getType();
31: branch 0 taken
0: branch 1 not taken
23: branch 3 taken
8: branch 4 taken
23: branch 5 taken
8: branch 6 taken
243 31: if (BDRE && BDRE->isByRef()) {
244 23: Types[i+BlockFields] = llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0);
245 : } else
246 8: Types[i+BlockFields] = ConvertType(Ty);
247 : }
248 :
249 20: llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true);
250 :
251 20: llvm::AllocaInst *A = CreateTempAlloca(Ty);
252 20: A->setAlignment(subBlockAlign.getQuantity());
253 20: V = A;
254 :
255 20: std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size());
256 20: int helpersize = 0;
257 :
80: branch 0 taken
20: branch 1 taken
258 100: for (unsigned i=0; i<4; ++i)
259 80: Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
0: branch 0 not taken
20: branch 1 taken
260 20: if (hasIntrospection)
261 0: Builder.CreateStore(Elts[5], Builder.CreateStructGEP(V, 5, "block.tmp"));
262 :
31: branch 1 taken
20: branch 2 taken
263 51: for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
264 : {
265 : // FIXME: Push const down.
266 31: Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
267 : DeclRefExpr *DR;
268 : ValueDecl *VD;
269 :
270 31: DR = dyn_cast<DeclRefExpr>(E);
271 : // Skip padding.
31: branch 0 taken
0: branch 1 not taken
272 31: if (DR) continue;
273 :
274 31: BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
275 31: VD = BDRE->getDecl();
276 :
277 31: llvm::Value* Addr = Builder.CreateStructGEP(V, i+BlockFields, "tmp");
278 31: NoteForHelper[helpersize].index = i+5;
279 : NoteForHelper[helpersize].RequiresCopying
280 31: = BlockRequiresCopying(VD->getType());
281 : NoteForHelper[helpersize].flag
282 : = (VD->getType()->isBlockPointerType()
283 : ? BLOCK_FIELD_IS_BLOCK
3: branch 4 taken
28: branch 5 taken
284 31: : BLOCK_FIELD_IS_OBJECT);
285 :
30: branch 1 taken
1: branch 2 taken
286 31: if (LocalDeclMap[VD]) {
22: branch 1 taken
8: branch 2 taken
287 30: if (BDRE->isByRef()) {
288 : NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
289 : // FIXME: Someone double check this.
2: branch 3 taken
20: branch 4 taken
290 22: (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
291 22: llvm::Value *Loc = LocalDeclMap[VD];
292 22: Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
293 22: Loc = Builder.CreateLoad(Loc);
294 22: Builder.CreateStore(Loc, Addr);
295 22: ++helpersize;
296 22: continue;
297 : } else
298 : E = new (getContext()) DeclRefExpr (VD,
299 : VD->getType(),
8: branch 4 taken
0: branch 5 not taken
300 8: SourceLocation());
301 : }
1: branch 1 taken
8: branch 2 taken
302 9: if (BDRE->isByRef()) {
303 : NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
304 : // FIXME: Someone double check this.
0: branch 3 not taken
1: branch 4 taken
305 1: (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
306 : E = new (getContext())
307 : UnaryOperator(E, UnaryOperator::AddrOf,
308 : getContext().getPointerType(E->getType()),
1: branch 6 taken
0: branch 7 not taken
309 1: SourceLocation());
310 : }
311 9: ++helpersize;
312 :
313 9: RValue r = EmitAnyExpr(E, Addr, false);
9: branch 1 taken
0: branch 2 not taken
314 9: if (r.isScalar()) {
315 9: llvm::Value *Loc = r.getScalarVal();
316 9: const llvm::Type *Ty = Types[i+BlockFields];
1: branch 1 taken
8: branch 2 taken
317 9: if (BDRE->isByRef()) {
318 : // E is now the address of the value field, instead, we want the
319 : // address of the actual ByRef struct. We optimize this slightly
320 : // compared to gcc by not grabbing the forwarding slot as this must
321 : // be done during Block_copy for us, and we can postpone the work
322 : // until then.
323 1: CharUnits offset = BlockDecls[BDRE->getDecl()];
324 :
325 1: llvm::Value *BlockLiteral = LoadBlockStruct();
326 :
327 : Loc = Builder.CreateGEP(BlockLiteral,
328 : llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
329 : offset.getQuantity()),
330 1: "block.literal");
331 1: Ty = llvm::PointerType::get(Ty, 0);
332 1: Loc = Builder.CreateBitCast(Loc, Ty);
333 1: Loc = Builder.CreateLoad(Loc);
334 : // Loc = Builder.CreateBitCast(Loc, Ty);
335 : }
336 9: Builder.CreateStore(Loc, Addr);
0: branch 1 not taken
0: branch 2 not taken
337 0: } else if (r.isComplex())
338 : // FIXME: implement
339 0: ErrorUnsupported(BE, "complex in block literal");
0: branch 1 not taken
0: branch 2 not taken
340 0: else if (r.isAggregate())
341 : ; // Already created into the destination
342 : else
343 0: assert (0 && "bad block variable");
344 : // FIXME: Ensure that the offset created by the backend for
345 : // the struct matches the previously computed offset in BlockDecls.
346 : }
347 20: NoteForHelper.resize(helpersize);
348 :
349 : // __descriptor
350 : llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockHasCopyDispose,
351 : subBlockSize, Ty,
352 20: &NoteForHelper);
353 20: Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
20: branch 6 taken
12: branch 7 taken
354 20: Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
355 : }
356 :
357 20: QualType BPT = BE->getType();
358 20: return Builder.CreateBitCast(V, ConvertType(BPT));
359 : }
360 :
361 :
362 17: const llvm::Type *BlockModule::getBlockDescriptorType() {
0: branch 0 not taken
17: branch 1 taken
363 17: if (BlockDescriptorType)
364 0: return BlockDescriptorType;
365 :
366 : const llvm::Type *UnsignedLongTy =
367 17: getTypes().ConvertType(getContext().UnsignedLongTy);
368 :
369 : // struct __block_descriptor {
370 : // unsigned long reserved;
371 : // unsigned long block_size;
372 : // };
373 : BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(),
374 : UnsignedLongTy,
375 : UnsignedLongTy,
376 17: NULL);
377 :
378 : getModule().addTypeName("struct.__block_descriptor",
379 17: BlockDescriptorType);
380 :
381 17: return BlockDescriptorType;
382 : }
383 :
384 62: const llvm::Type *BlockModule::getGenericBlockLiteralType() {
45: branch 0 taken
17: branch 1 taken
385 62: if (GenericBlockLiteralType)
386 45: return GenericBlockLiteralType;
387 :
388 : const llvm::Type *BlockDescPtrTy =
389 17: llvm::PointerType::getUnqual(getBlockDescriptorType());
390 :
391 : const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
392 17: getTypes().ConvertType(getContext().IntTy));
393 :
394 : // struct __block_literal_generic {
395 : // void *__isa;
396 : // int __flags;
397 : // int __reserved;
398 : // void (*__invoke)(void *);
399 : // struct __block_descriptor *__descriptor;
400 : // // GNU runtime only:
401 : // const char *types;
402 : // };
0: branch 2 not taken
17: branch 3 taken
403 17: if (CGM.getContext().getLangOptions().BlockIntrospection)
404 : GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
405 : PtrToInt8Ty,
406 : IntTy,
407 : IntTy,
408 : PtrToInt8Ty,
409 : BlockDescPtrTy,
410 : PtrToInt8Ty,
411 0: NULL);
412 : else
413 : GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
414 : PtrToInt8Ty,
415 : IntTy,
416 : IntTy,
417 : PtrToInt8Ty,
418 : BlockDescPtrTy,
419 17: NULL);
420 :
421 : getModule().addTypeName("struct.__block_literal_generic",
422 17: GenericBlockLiteralType);
423 :
424 17: return GenericBlockLiteralType;
425 : }
426 :
427 0: const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() {
0: branch 0 not taken
0: branch 1 not taken
428 0: if (GenericExtendedBlockLiteralType)
429 0: return GenericExtendedBlockLiteralType;
430 :
431 : const llvm::Type *BlockDescPtrTy =
432 0: llvm::PointerType::getUnqual(getBlockDescriptorType());
433 :
434 : const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
435 0: getTypes().ConvertType(getContext().IntTy));
436 :
437 : // struct __block_literal_generic {
438 : // void *__isa;
439 : // int __flags;
440 : // int __reserved;
441 : // void (*__invoke)(void *);
442 : // struct __block_descriptor *__descriptor;
443 : // void *__copy_func_helper_decl;
444 : // void *__destroy_func_decl;
445 : // };
446 : GenericExtendedBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
447 : PtrToInt8Ty,
448 : IntTy,
449 : IntTy,
450 : PtrToInt8Ty,
451 : BlockDescPtrTy,
452 : PtrToInt8Ty,
453 : PtrToInt8Ty,
454 0: NULL);
455 :
456 : getModule().addTypeName("struct.__block_literal_extended_generic",
457 0: GenericExtendedBlockLiteralType);
458 :
459 0: return GenericExtendedBlockLiteralType;
460 : }
461 :
462 : RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E,
463 24: ReturnValueSlot ReturnValue) {
464 : const BlockPointerType *BPT =
465 24: E->getCallee()->getType()->getAs<BlockPointerType>();
466 :
467 24: llvm::Value *Callee = EmitScalarExpr(E->getCallee());
468 :
469 : // Get a pointer to the generic block literal.
470 : const llvm::Type *BlockLiteralTy =
471 24: llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
472 :
473 : // Bitcast the callee to a block literal.
474 : llvm::Value *BlockLiteral =
475 24: Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
476 :
477 : // Get the function pointer from the literal.
478 24: llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
479 :
480 : BlockLiteral =
481 : Builder.CreateBitCast(BlockLiteral,
482 : llvm::Type::getInt8PtrTy(VMContext),
483 24: "tmp");
484 :
485 : // Add the block literal.
486 24: QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
487 24: CallArgList Args;
488 24: Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
489 :
490 24: QualType FnType = BPT->getPointeeType();
491 :
492 : // And the rest of the arguments.
493 : EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(),
494 24: E->arg_begin(), E->arg_end());
495 :
496 : // Load the function.
497 24: llvm::Value *Func = Builder.CreateLoad(FuncPtr, "tmp");
498 :
499 24: const FunctionType *FuncTy = FnType->getAs<FunctionType>();
500 24: QualType ResultType = FuncTy->getResultType();
501 :
502 : const CGFunctionInfo &FnInfo =
503 : CGM.getTypes().getFunctionInfo(ResultType, Args, FuncTy->getCallConv(),
504 24: FuncTy->getNoReturnAttr());
505 :
506 : // Cast the function pointer to the right type.
507 : const llvm::Type *BlockFTy =
508 24: CGM.getTypes().GetFunctionType(FnInfo, false);
509 :
510 24: const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
511 24: Func = Builder.CreateBitCast(Func, BlockFTyPtr);
512 :
513 : // And call the block.
514 24: return EmitCall(FnInfo, Func, ReturnValue, Args);
515 : }
516 :
517 66: CharUnits CodeGenFunction::AllocateBlockDecl(const BlockDeclRefExpr *E) {
518 66: const ValueDecl *VD = E->getDecl();
519 66: CharUnits &offset = BlockDecls[VD];
520 :
521 : // See if we have already allocated an offset for this variable.
35: branch 1 taken
31: branch 2 taken
522 66: if (offset.isPositive())
523 35: return offset;
524 :
525 : // Don't run the expensive check, unless we have to.
20: branch 0 taken
11: branch 1 taken
526 31: if (!BlockHasCopyDispose)
5: branch 1 taken
15: branch 2 taken
5: branch 5 taken
0: branch 6 not taken
20: branch 7 taken
0: branch 8 not taken
527 20: if (E->isByRef()
528 : || BlockRequiresCopying(E->getType()))
529 20: BlockHasCopyDispose = true;
530 :
531 : // if not, allocate one now.
532 31: offset = getBlockOffset(E);
533 :
534 31: return offset;
535 : }
536 :
537 32: llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
538 32: const ValueDecl *VD = E->getDecl();
539 32: CharUnits offset = AllocateBlockDecl(E);
540 :
541 :
542 32: llvm::Value *BlockLiteral = LoadBlockStruct();
543 : llvm::Value *V = Builder.CreateGEP(BlockLiteral,
544 : llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
545 : offset.getQuantity()),
546 32: "block.literal");
26: branch 1 taken
6: branch 2 taken
547 32: if (E->isByRef()) {
548 : const llvm::Type *PtrStructTy
549 26: = llvm::PointerType::get(BuildByRefType(VD), 0);
550 : // The block literal will need a copy/destroy helper.
551 26: BlockHasCopyDispose = true;
552 :
553 26: const llvm::Type *Ty = PtrStructTy;
554 26: Ty = llvm::PointerType::get(Ty, 0);
555 26: V = Builder.CreateBitCast(V, Ty);
556 26: V = Builder.CreateLoad(V);
557 26: V = Builder.CreateStructGEP(V, 1, "forwarding");
558 26: V = Builder.CreateLoad(V);
559 26: V = Builder.CreateBitCast(V, PtrStructTy);
560 : V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
561 26: VD->getNameAsString());
562 : } else {
563 6: const llvm::Type *Ty = CGM.getTypes().ConvertType(VD->getType());
564 :
565 6: Ty = llvm::PointerType::get(Ty, 0);
566 6: V = Builder.CreateBitCast(V, Ty);
567 : }
568 32: return V;
569 : }
570 :
571 107: void CodeGenFunction::BlockForwardSelf() {
572 107: const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
573 107: ImplicitParamDecl *SelfDecl = OMD->getSelfDecl();
574 107: llvm::Value *&DMEntry = LocalDeclMap[SelfDecl];
106: branch 0 taken
1: branch 1 taken
575 107: if (DMEntry)
576 106: return;
577 : // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care
578 : BlockDeclRefExpr *BDRE = new (getContext())
579 : BlockDeclRefExpr(SelfDecl,
1: branch 4 taken
0: branch 5 not taken
580 1: SelfDecl->getType(), SourceLocation(), false);
581 1: DMEntry = GetAddrOfBlockDecl(BDRE);
582 : }
583 :
584 : llvm::Constant *
585 3: BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
586 : // Generate the block descriptor.
587 3: const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
588 : const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
589 3: getTypes().ConvertType(getContext().IntTy));
590 :
591 : llvm::Constant *DescriptorFields[2];
592 :
593 : // Reserved
594 3: DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
595 :
596 : // Block literal size. For global blocks we just use the size of the generic
597 : // block literal struct.
598 : CharUnits BlockLiteralSize =
599 3: CGM.GetTargetTypeStoreSize(getGenericBlockLiteralType());
600 : DescriptorFields[1] =
601 3: llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize.getQuantity());
602 :
603 : llvm::Constant *DescriptorStruct =
604 3: llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 2, false);
605 :
606 : llvm::GlobalVariable *Descriptor =
607 : new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true,
608 : llvm::GlobalVariable::InternalLinkage,
609 3: DescriptorStruct, "__block_descriptor_global");
610 :
611 3: int FieldCount = 5;
612 : // Generate the constants for the block literal.
0: branch 2 not taken
3: branch 3 taken
613 3: if (CGM.getContext().getLangOptions().BlockIntrospection)
614 0: FieldCount = 6;
615 :
616 3: std::vector<llvm::Constant*> LiteralFields(FieldCount);
617 :
618 3: CodeGenFunction::BlockInfo Info(0, n);
619 3: CharUnits subBlockSize;
620 3: CharUnits subBlockAlign;
621 3: llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
622 3: bool subBlockHasCopyDispose = false;
623 3: llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
624 : llvm::Function *Fn
625 : = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap,
626 : subBlockSize,
627 : subBlockAlign,
628 : subBlockDeclRefDecls,
629 3: subBlockHasCopyDispose);
630 : assert(subBlockSize == BlockLiteralSize
3: branch 1 taken
0: branch 2 not taken
631 3: && "no imports allowed for global block");
632 :
633 : // isa
634 3: LiteralFields[0] = getNSConcreteGlobalBlock();
635 :
636 : // Flags
637 : LiteralFields[1] = CGM.getContext().getLangOptions().BlockIntrospection ?
638 : llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR |
639 : BLOCK_HAS_OBJC_TYPE) :
0: branch 3 not taken
3: branch 4 taken
640 3: llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR);
641 :
642 : // Reserved
643 3: LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
644 :
645 : // Function
646 3: LiteralFields[3] = Fn;
647 :
648 : // Descriptor
649 3: LiteralFields[4] = Descriptor;
650 :
651 : // Type encoding
0: branch 2 not taken
3: branch 3 taken
652 3: if (CGM.getContext().getLangOptions().BlockIntrospection) {
653 0: std::string BlockTypeEncoding;
654 0: CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
655 :
656 0: LiteralFields[5] = CGM.GetAddrOfConstantCString(BlockTypeEncoding);
657 : }
658 :
659 : llvm::Constant *BlockLiteralStruct =
660 3: llvm::ConstantStruct::get(VMContext, LiteralFields, false);
661 :
662 : llvm::GlobalVariable *BlockLiteral =
663 : new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true,
664 : llvm::GlobalVariable::InternalLinkage,
665 3: BlockLiteralStruct, "__block_literal_global");
666 :
667 3: return BlockLiteral;
668 : }
669 :
670 33: llvm::Value *CodeGenFunction::LoadBlockStruct() {
671 : llvm::Value *V = Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()],
672 33: "self");
673 : // For now, we codegen based upon byte offsets.
674 33: return Builder.CreateBitCast(V, PtrToInt8Ty);
675 : }
676 :
677 : llvm::Function *
678 : CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
679 : const BlockInfo& Info,
680 : const Decl *OuterFuncDecl,
681 : llvm::DenseMap<const Decl*, llvm::Value*> ldm,
682 : CharUnits &Size,
683 : CharUnits &Align,
684 : llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls,
685 35: bool &subBlockHasCopyDispose) {
686 :
687 : // Check if we should generate debug info for this block.
4: branch 1 taken
31: branch 2 taken
688 35: if (CGM.getDebugInfo())
689 4: DebugInfo = CGM.getDebugInfo();
690 :
691 : // Arrange for local static and local extern declarations to appear
692 : // to be local to this function as well, as they are directly referenced
693 : // in a block.
58: branch 5 taken
35: branch 6 taken
694 93: for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
695 : i != ldm.end();
696 : ++i) {
697 58: const VarDecl *VD = dyn_cast<VarDecl>(i->first);
698 :
56: branch 1 taken
2: branch 2 taken
0: branch 4 not taken
56: branch 5 taken
2: branch 6 taken
56: branch 7 taken
699 58: if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage())
700 2: LocalDeclMap[VD] = i->second;
701 : }
702 :
703 : BlockOffset =
704 35: CGM.GetTargetTypeStoreSize(CGM.getGenericBlockLiteralType());
705 35: BlockAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
706 :
707 35: const FunctionType *BlockFunctionType = BExpr->getFunctionType();
708 35: QualType ResultType;
709 35: CallingConv CC = BlockFunctionType->getCallConv();
710 35: bool NoReturn = BlockFunctionType->getNoReturnAttr();
711 : bool IsVariadic;
35: branch 0 taken
0: branch 1 not taken
712 35: if (const FunctionProtoType *FTy =
713 35: dyn_cast<FunctionProtoType>(BlockFunctionType)) {
714 35: ResultType = FTy->getResultType();
715 35: IsVariadic = FTy->isVariadic();
716 : } else {
717 : // K&R style block.
718 0: ResultType = BlockFunctionType->getResultType();
719 0: IsVariadic = false;
720 : }
721 :
722 35: FunctionArgList Args;
723 :
724 35: CurFuncDecl = OuterFuncDecl;
725 :
726 35: const BlockDecl *BD = BExpr->getBlockDecl();
727 :
728 35: IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor");
729 :
730 : // Allocate all BlockDeclRefDecls, so we can calculate the right ParmTy below.
731 35: AllocateAllBlockDeclRefs(Info, this);
732 :
733 : QualType ParmTy = getContext().getBlockParmType(BlockHasCopyDispose,
734 35: BlockDeclRefDecls);
735 : // FIXME: This leaks
736 : ImplicitParamDecl *SelfDecl =
737 : ImplicitParamDecl::Create(getContext(), 0,
738 : SourceLocation(), II,
739 35: ParmTy);
740 :
741 35: Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
742 35: BlockStructDecl = SelfDecl;
743 :
11: branch 1 taken
35: branch 2 taken
744 81: for (BlockDecl::param_const_iterator i = BD->param_begin(),
745 35: e = BD->param_end(); i != e; ++i)
746 11: Args.push_back(std::make_pair(*i, (*i)->getType()));
747 :
748 : const CGFunctionInfo &FI =
749 35: CGM.getTypes().getFunctionInfo(ResultType, Args, CC, NoReturn);
750 :
751 35: CodeGenTypes &Types = CGM.getTypes();
752 35: const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic);
753 :
754 : llvm::Function *Fn =
755 : llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
756 : llvm::Twine("__") + Info.Name + "_block_invoke_",
757 35: &CGM.getModule());
758 :
759 35: CGM.SetInternalFunctionAttributes(BD, Fn, FI);
760 :
761 : StartFunction(BD, ResultType, Fn, Args,
762 35: BExpr->getBody()->getLocEnd());
763 :
764 35: CurFuncDecl = OuterFuncDecl;
765 35: CurCodeDecl = BD;
766 :
767 : // Save a spot to insert the debug information for all the BlockDeclRefDecls.
768 35: llvm::BasicBlock *entry = Builder.GetInsertBlock();
769 35: llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint();
770 35: --entry_ptr;
771 :
772 35: EmitStmt(BExpr->getBody());
773 :
774 : // Remember where we were...
775 35: llvm::BasicBlock *resume = Builder.GetInsertBlock();
776 :
777 : // Go back to the entry.
778 35: ++entry_ptr;
779 35: Builder.SetInsertPoint(entry, entry_ptr);
780 :
4: branch 1 taken
31: branch 2 taken
781 35: if (CGDebugInfo *DI = getDebugInfo()) {
782 : // Emit debug information for all the BlockDeclRefDecls.
3: branch 1 taken
4: branch 2 taken
783 7: for (unsigned i = 0, e = BlockDeclRefDecls.size(); i != e; ++i) {
3: branch 0 taken
0: branch 1 not taken
784 3: if (const BlockDeclRefExpr *BDRE =
785 3: dyn_cast<BlockDeclRefExpr>(BlockDeclRefDecls[i])) {
786 3: const ValueDecl *D = BDRE->getDecl();
787 3: DI->setLocation(D->getLocation());
788 : DI->EmitDeclareOfBlockDeclRefVariable(BDRE,
789 : LocalDeclMap[getBlockStructDecl()],
790 3: Builder, this);
791 : }
792 : }
793 : }
794 : // And resume where we left off.
7: branch 0 taken
28: branch 1 taken
795 35: if (resume == 0)
796 7: Builder.ClearInsertionPoint();
797 : else
798 28: Builder.SetInsertPoint(resume);
799 :
800 35: FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
801 :
802 : // The runtime needs a minimum alignment of a void *.
803 35: CharUnits MinAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
804 : BlockOffset = CharUnits::fromQuantity(
805 : llvm::RoundUpToAlignment(BlockOffset.getQuantity(),
806 35: MinAlign.getQuantity()));
807 :
808 35: Size = BlockOffset;
809 35: Align = BlockAlign;
810 35: subBlockDeclRefDecls = BlockDeclRefDecls;
811 35: subBlockHasCopyDispose |= BlockHasCopyDispose;
812 35: return Fn;
813 : }
814 :
815 31: CharUnits BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
816 31: const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
817 :
818 31: CharUnits Size = getContext().getTypeSizeInChars(D->getType());
819 31: CharUnits Align = getContext().getDeclAlign(D);
820 :
23: branch 1 taken
8: branch 2 taken
821 31: if (BDRE->isByRef()) {
822 23: Size = getContext().getTypeSizeInChars(getContext().VoidPtrTy);
823 23: Align = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
824 : }
825 :
31: branch 1 taken
0: branch 2 not taken
826 31: assert ((Align.isPositive()) && "alignment must be 1 byte or more");
827 :
828 31: CharUnits OldOffset = BlockOffset;
829 :
830 : // Ensure proper alignment, even if it means we have to have a gap
831 : BlockOffset = CharUnits::fromQuantity(
832 31: llvm::RoundUpToAlignment(BlockOffset.getQuantity(), Align.getQuantity()));
833 31: BlockAlign = std::max(Align, BlockAlign);
834 :
835 31: CharUnits Pad = BlockOffset - OldOffset;
0: branch 1 not taken
31: branch 2 taken
836 31: if (Pad.isPositive()) {
837 0: llvm::ArrayType::get(llvm::Type::getInt8Ty(VMContext), Pad.getQuantity());
838 : QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
839 : llvm::APInt(32,
840 : Pad.getQuantity()),
841 0: ArrayType::Normal, 0);
842 : ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
843 0: 0, QualType(PadTy), 0, VarDecl::None);
844 : Expr *E;
845 : E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
0: branch 4 not taken
0: branch 5 not taken
846 0: SourceLocation());
847 0: BlockDeclRefDecls.push_back(E);
848 : }
849 31: BlockDeclRefDecls.push_back(BDRE);
850 :
851 31: BlockOffset += Size;
852 31: return BlockOffset-Size;
853 : }
854 :
855 : llvm::Constant *BlockFunction::
856 : GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
857 21: std::vector<HelperInfo> *NoteForHelperp) {
858 21: QualType R = getContext().VoidTy;
859 :
860 21: FunctionArgList Args;
861 : // FIXME: This leaks
862 : ImplicitParamDecl *Dst =
863 : ImplicitParamDecl::Create(getContext(), 0,
864 : SourceLocation(), 0,
865 21: getContext().getPointerType(getContext().VoidTy));
866 21: Args.push_back(std::make_pair(Dst, Dst->getType()));
867 : ImplicitParamDecl *Src =
868 : ImplicitParamDecl::Create(getContext(), 0,
869 : SourceLocation(), 0,
870 21: getContext().getPointerType(getContext().VoidTy));
871 21: Args.push_back(std::make_pair(Src, Src->getType()));
872 :
873 : const CGFunctionInfo &FI =
874 21: CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false);
875 :
876 : // FIXME: We'd like to put these into a mergable by content, with
877 : // internal linkage.
878 21: CodeGenTypes &Types = CGM.getTypes();
879 21: const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
880 :
881 : llvm::Function *Fn =
882 : llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
883 21: "__copy_helper_block_", &CGM.getModule());
884 :
885 : IdentifierInfo *II
886 21: = &CGM.getContext().Idents.get("__copy_helper_block_");
887 :
888 : FunctionDecl *FD = FunctionDecl::Create(getContext(),
889 : getContext().getTranslationUnitDecl(),
890 : SourceLocation(), II, R, 0,
891 : FunctionDecl::Static, false,
21: branch 4 taken
0: branch 5 not taken
892 21: true);
893 21: CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
894 :
895 21: llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
896 : llvm::Type *PtrPtrT;
897 :
20: branch 0 taken
1: branch 1 taken
898 21: if (NoteForHelperp) {
899 20: std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
900 :
901 20: PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
902 20: SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
903 20: SrcObj = Builder.CreateLoad(SrcObj);
904 :
905 20: llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
906 : llvm::Type *PtrPtrT;
907 20: PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
908 20: DstObj = Builder.CreateBitCast(DstObj, PtrPtrT);
909 20: DstObj = Builder.CreateLoad(DstObj);
910 :
31: branch 1 taken
20: branch 2 taken
911 51: for (unsigned i=0; i < NoteForHelper.size(); ++i) {
912 31: int flag = NoteForHelper[i].flag;
913 31: int index = NoteForHelper[i].index;
914 :
8: branch 1 taken
23: branch 2 taken
6: branch 4 taken
2: branch 5 taken
29: branch 6 taken
2: branch 7 taken
915 31: if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
916 : || NoteForHelper[i].RequiresCopying) {
917 29: llvm::Value *Srcv = SrcObj;
918 29: Srcv = Builder.CreateStructGEP(Srcv, index);
919 : Srcv = Builder.CreateBitCast(Srcv,
920 29: llvm::PointerType::get(PtrToInt8Ty, 0));
921 29: Srcv = Builder.CreateLoad(Srcv);
922 :
923 29: llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
924 29: Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
925 :
926 : llvm::Value *N = llvm::ConstantInt::get(
927 29: llvm::Type::getInt32Ty(T->getContext()), flag);
928 29: llvm::Value *F = getBlockObjectAssign();
929 29: Builder.CreateCall3(F, Dstv, Srcv, N);
930 : }
931 : }
932 : }
933 :
934 21: CGF.FinishFunction();
935 :
936 21: return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
937 : }
938 :
939 : llvm::Constant *BlockFunction::
940 : GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
941 : const llvm::StructType* T,
942 21: std::vector<HelperInfo> *NoteForHelperp) {
943 21: QualType R = getContext().VoidTy;
944 :
945 21: FunctionArgList Args;
946 : // FIXME: This leaks
947 : ImplicitParamDecl *Src =
948 : ImplicitParamDecl::Create(getContext(), 0,
949 : SourceLocation(), 0,
950 21: getContext().getPointerType(getContext().VoidTy));
951 :
952 21: Args.push_back(std::make_pair(Src, Src->getType()));
953 :
954 : const CGFunctionInfo &FI =
955 21: CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false);
956 :
957 : // FIXME: We'd like to put these into a mergable by content, with
958 : // internal linkage.
959 21: CodeGenTypes &Types = CGM.getTypes();
960 21: const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
961 :
962 : llvm::Function *Fn =
963 : llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
964 21: "__destroy_helper_block_", &CGM.getModule());
965 :
966 : IdentifierInfo *II
967 21: = &CGM.getContext().Idents.get("__destroy_helper_block_");
968 :
969 : FunctionDecl *FD = FunctionDecl::Create(getContext(),
970 : getContext().getTranslationUnitDecl(),
971 : SourceLocation(), II, R, 0,
972 : FunctionDecl::Static, false,
21: branch 4 taken
0: branch 5 not taken
973 21: true);
974 21: CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
975 :
20: branch 0 taken
1: branch 1 taken
976 21: if (NoteForHelperp) {
977 20: std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
978 :
979 20: llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
980 : llvm::Type *PtrPtrT;
981 20: PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
982 20: SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
983 20: SrcObj = Builder.CreateLoad(SrcObj);
984 :
31: branch 1 taken
20: branch 2 taken
985 51: for (unsigned i=0; i < NoteForHelper.size(); ++i) {
986 31: int flag = NoteForHelper[i].flag;
987 31: int index = NoteForHelper[i].index;
988 :
8: branch 1 taken
23: branch 2 taken
6: branch 4 taken
2: branch 5 taken
29: branch 6 taken
2: branch 7 taken
989 31: if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
990 : || NoteForHelper[i].RequiresCopying) {
991 29: llvm::Value *Srcv = SrcObj;
992 29: Srcv = Builder.CreateStructGEP(Srcv, index);
993 : Srcv = Builder.CreateBitCast(Srcv,
994 29: llvm::PointerType::get(PtrToInt8Ty, 0));
995 29: Srcv = Builder.CreateLoad(Srcv);
996 :
997 29: BuildBlockRelease(Srcv, flag);
998 : }
999 : }
1000 : }
1001 :
1002 21: CGF.FinishFunction();
1003 :
1004 21: return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
1005 : }
1006 :
1007 : llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
1008 21: std::vector<HelperInfo> *NoteForHelper) {
1009 : return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
1010 21: T, NoteForHelper);
1011 : }
1012 :
1013 : llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
1014 21: std::vector<HelperInfo> *NoteForHelperp) {
1015 : return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
1016 21: T, NoteForHelperp);
1017 : }
1018 :
1019 : llvm::Constant *BlockFunction::
1020 7: GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
1021 7: QualType R = getContext().VoidTy;
1022 :
1023 7: FunctionArgList Args;
1024 : // FIXME: This leaks
1025 : ImplicitParamDecl *Dst =
1026 : ImplicitParamDecl::Create(getContext(), 0,
1027 : SourceLocation(), 0,
1028 7: getContext().getPointerType(getContext().VoidTy));
1029 7: Args.push_back(std::make_pair(Dst, Dst->getType()));
1030 :
1031 : // FIXME: This leaks
1032 : ImplicitParamDecl *Src =
1033 : ImplicitParamDecl::Create(getContext(), 0,
1034 : SourceLocation(), 0,
1035 7: getContext().getPointerType(getContext().VoidTy));
1036 7: Args.push_back(std::make_pair(Src, Src->getType()));
1037 :
1038 : const CGFunctionInfo &FI =
1039 7: CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false);
1040 :
1041 7: CodeGenTypes &Types = CGM.getTypes();
1042 7: const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1043 :
1044 : // FIXME: We'd like to put these into a mergable by content, with
1045 : // internal linkage.
1046 : llvm::Function *Fn =
1047 : llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
1048 7: "__Block_byref_id_object_copy_", &CGM.getModule());
1049 :
1050 : IdentifierInfo *II
1051 7: = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
1052 :
1053 : FunctionDecl *FD = FunctionDecl::Create(getContext(),
1054 : getContext().getTranslationUnitDecl(),
1055 : SourceLocation(), II, R, 0,
1056 : FunctionDecl::Static, false,
7: branch 4 taken
0: branch 5 not taken
1057 7: true);
1058 7: CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
1059 :
1060 : // dst->x
1061 7: llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
1062 7: V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
1063 7: V = Builder.CreateLoad(V);
1064 7: V = Builder.CreateStructGEP(V, 6, "x");
1065 7: llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
1066 :
1067 : // src->x
1068 7: V = CGF.GetAddrOfLocalVar(Src);
1069 7: V = Builder.CreateLoad(V);
1070 7: V = Builder.CreateBitCast(V, T);
1071 7: V = Builder.CreateStructGEP(V, 6, "x");
1072 7: V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
1073 7: llvm::Value *SrcObj = Builder.CreateLoad(V);
1074 :
1075 7: flag |= BLOCK_BYREF_CALLER;
1076 :
1077 : llvm::Value *N = llvm::ConstantInt::get(
1078 7: llvm::Type::getInt32Ty(T->getContext()), flag);
1079 7: llvm::Value *F = getBlockObjectAssign();
1080 7: Builder.CreateCall3(F, DstObj, SrcObj, N);
1081 :
1082 7: CGF.FinishFunction();
1083 :
1084 7: return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
1085 : }
1086 :
1087 : llvm::Constant *
1088 : BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
1089 7: int flag) {
1090 7: QualType R = getContext().VoidTy;
1091 :
1092 7: FunctionArgList Args;
1093 : // FIXME: This leaks
1094 : ImplicitParamDecl *Src =
1095 : ImplicitParamDecl::Create(getContext(), 0,
1096 : SourceLocation(), 0,
1097 7: getContext().getPointerType(getContext().VoidTy));
1098 :
1099 7: Args.push_back(std::make_pair(Src, Src->getType()));
1100 :
1101 : const CGFunctionInfo &FI =
1102 7: CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false);
1103 :
1104 7: CodeGenTypes &Types = CGM.getTypes();
1105 7: const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1106 :
1107 : // FIXME: We'd like to put these into a mergable by content, with
1108 : // internal linkage.
1109 : llvm::Function *Fn =
1110 : llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
1111 : "__Block_byref_id_object_dispose_",
1112 7: &CGM.getModule());
1113 :
1114 : IdentifierInfo *II
1115 7: = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
1116 :
1117 : FunctionDecl *FD = FunctionDecl::Create(getContext(),
1118 : getContext().getTranslationUnitDecl(),
1119 : SourceLocation(), II, R, 0,
1120 : FunctionDecl::Static, false,
7: branch 4 taken
0: branch 5 not taken
1121 7: true);
1122 7: CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
1123 :
1124 7: llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
1125 7: V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
1126 7: V = Builder.CreateLoad(V);
1127 7: V = Builder.CreateStructGEP(V, 6, "x");
1128 7: V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
1129 7: V = Builder.CreateLoad(V);
1130 :
1131 7: flag |= BLOCK_BYREF_CALLER;
1132 7: BuildBlockRelease(V, flag);
1133 7: CGF.FinishFunction();
1134 :
1135 7: return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
1136 : }
1137 :
1138 : llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
1139 13: int Flag, unsigned Align) {
1140 : // All alignments below that of pointer alignment collapse down to just
1141 : // pointer alignment, as we always have at least that much alignment to begin
1142 : // with.
1143 13: Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
1144 :
1145 : // As an optimization, we only generate a single function of each kind we
1146 : // might need. We need a different one for each alignment and for each
1147 : // setting of flags. We mix Align and flag to get the kind.
1148 13: uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1149 13: llvm::Constant *&Entry = CGM.AssignCache[Kind];
6: branch 0 taken
7: branch 1 taken
1150 13: if (Entry)
1151 6: return Entry;
1152 7: return Entry = CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, Flag);
1153 : }
1154 :
1155 : llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
1156 : int Flag,
1157 13: unsigned Align) {
1158 : // All alignments below that of pointer alignment collpase down to just
1159 : // pointer alignment, as we always have at least that much alignment to begin
1160 : // with.
1161 13: Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
1162 :
1163 : // As an optimization, we only generate a single function of each kind we
1164 : // might need. We need a different one for each alignment and for each
1165 : // setting of flags. We mix Align and flag to get the kind.
1166 13: uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
1167 13: llvm::Constant *&Entry = CGM.DestroyCache[Kind];
6: branch 0 taken
7: branch 1 taken
1168 13: if (Entry)
1169 6: return Entry;
1170 7: return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, Flag);
1171 : }
1172 :
1173 60: llvm::Value *BlockFunction::getBlockObjectDispose() {
13: branch 0 taken
47: branch 1 taken
1174 60: if (CGM.BlockObjectDispose == 0) {
1175 : const llvm::FunctionType *FTy;
1176 13: std::vector<const llvm::Type*> ArgTys;
1177 13: const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
1178 13: ArgTys.push_back(PtrToInt8Ty);
1179 13: ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
1180 13: FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
1181 : CGM.BlockObjectDispose
1182 13: = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
1183 : }
1184 60: return CGM.BlockObjectDispose;
1185 : }
1186 :
1187 36: llvm::Value *BlockFunction::getBlockObjectAssign() {
12: branch 0 taken
24: branch 1 taken
1188 36: if (CGM.BlockObjectAssign == 0) {
1189 : const llvm::FunctionType *FTy;
1190 12: std::vector<const llvm::Type*> ArgTys;
1191 12: const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
1192 12: ArgTys.push_back(PtrToInt8Ty);
1193 12: ArgTys.push_back(PtrToInt8Ty);
1194 12: ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
1195 12: FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
1196 : CGM.BlockObjectAssign
1197 12: = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
1198 : }
1199 36: return CGM.BlockObjectAssign;
1200 : }
1201 :
1202 60: void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
1203 60: llvm::Value *F = getBlockObjectDispose();
1204 : llvm::Value *N;
1205 60: V = Builder.CreateBitCast(V, PtrToInt8Ty);
1206 60: N = llvm::ConstantInt::get(llvm::Type::getInt32Ty(V->getContext()), flag);
1207 60: Builder.CreateCall2(F, V, N);
1208 60: }
1209 :
1210 679: ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
1211 :
1212 : BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
1213 3076: CGBuilderTy &B)
1214 3076: : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) {
1215 : PtrToInt8Ty = llvm::PointerType::getUnqual(
1216 3076: llvm::Type::getInt8Ty(VMContext));
1217 :
1218 3076: BlockHasCopyDispose = false;
1219 3076: }
Generated: 2010-02-10 01:31 by zcov