12437cbfaSAnders Carlsson //===--- CGBlocks.cpp - Emit LLVM Code for declarations -------------------===// 22437cbfaSAnders Carlsson // 32437cbfaSAnders Carlsson // The LLVM Compiler Infrastructure 42437cbfaSAnders Carlsson // 52437cbfaSAnders Carlsson // This file is distributed under the University of Illinois Open Source 62437cbfaSAnders Carlsson // License. See LICENSE.TXT for details. 72437cbfaSAnders Carlsson // 82437cbfaSAnders Carlsson //===----------------------------------------------------------------------===// 92437cbfaSAnders Carlsson // 102437cbfaSAnders Carlsson // This contains code to emit blocks. 112437cbfaSAnders Carlsson // 122437cbfaSAnders Carlsson //===----------------------------------------------------------------------===// 132437cbfaSAnders Carlsson 142437cbfaSAnders Carlsson #include "CodeGenFunction.h" 152437cbfaSAnders Carlsson #include "CodeGenModule.h" 16692c6e37SMike Stump #include "clang/AST/DeclObjC.h" 172437cbfaSAnders Carlsson #include "llvm/Module.h" 186a60fa24SAnders Carlsson #include "llvm/Target/TargetData.h" 192437cbfaSAnders Carlsson 202437cbfaSAnders Carlsson #include <algorithm> 212437cbfaSAnders Carlsson 222437cbfaSAnders Carlsson using namespace clang; 232437cbfaSAnders Carlsson using namespace CodeGen; 242437cbfaSAnders Carlsson 250874160bSMike Stump // Temporary code to enable testing of __block variables 260874160bSMike Stump // #include "clang/Frontend/CompileOptions.h" 270874160bSMike Stump #include "llvm/Support/CommandLine.h" 280874160bSMike Stump static llvm::cl::opt<bool> 290874160bSMike Stump Enable__block("f__block", 300874160bSMike Stump // See all the FIXMEs for the various work that needs to be done 310874160bSMike Stump llvm::cl::desc("temporary option to turn on __block precessing " 320874160bSMike Stump "even though the code isn't done yet"), 330874160bSMike Stump llvm::cl::ValueDisallowed, llvm::cl::AllowInverse, 344446dcf0SMike Stump llvm::cl::ZeroOrMore, 351ab20caeSMike Stump llvm::cl::init(true)); 360874160bSMike Stump 37d6ef62f0SMike Stump llvm::Constant *CodeGenFunction:: 38aeb0ffd5SMike Stump BuildDescriptorBlockDecl(uint64_t Size, const llvm::StructType* Ty, 39aeb0ffd5SMike Stump std::vector<HelperInfo> *NoteForHelper) { 40c2c38331SMike Stump const llvm::Type *UnsignedLongTy 41c2c38331SMike Stump = CGM.getTypes().ConvertType(getContext().UnsignedLongTy); 4285284bacSMike Stump llvm::Constant *C; 4385284bacSMike Stump std::vector<llvm::Constant*> Elts; 4485284bacSMike Stump 4585284bacSMike Stump // reserved 46c2c38331SMike Stump C = llvm::ConstantInt::get(UnsignedLongTy, 0); 4785284bacSMike Stump Elts.push_back(C); 4885284bacSMike Stump 4985284bacSMike Stump // Size 502ac40a9fSMike Stump // FIXME: What is the right way to say this doesn't fit? We should give 512ac40a9fSMike Stump // a user diagnostic in that case. Better fix would be to change the 522ac40a9fSMike Stump // API to size_t. 53cb2fbcb0SMike Stump C = llvm::ConstantInt::get(UnsignedLongTy, Size); 5485284bacSMike Stump Elts.push_back(C); 5585284bacSMike Stump 5685284bacSMike Stump if (BlockHasCopyDispose) { 5785284bacSMike Stump // copy_func_helper_decl 58aeb0ffd5SMike Stump Elts.push_back(BuildCopyHelper(Ty, *NoteForHelper)); 5985284bacSMike Stump 6085284bacSMike Stump // destroy_func_decl 61aeb0ffd5SMike Stump Elts.push_back(BuildDestroyHelper(Ty, *NoteForHelper)); 6285284bacSMike Stump } 6385284bacSMike Stump 6485284bacSMike Stump C = llvm::ConstantStruct::get(Elts); 6585284bacSMike Stump 6685284bacSMike Stump C = new llvm::GlobalVariable(C->getType(), true, 6785284bacSMike Stump llvm::GlobalValue::InternalLinkage, 68499ae7ecSMike Stump C, "__block_descriptor_tmp", &CGM.getModule()); 6985284bacSMike Stump return C; 7085284bacSMike Stump } 7185284bacSMike Stump 7295435674SMike Stump llvm::Constant *BlockModule::getNSConcreteGlobalBlock() { 73971f9b66SMike Stump if (NSConcreteGlobalBlock) 74971f9b66SMike Stump return NSConcreteGlobalBlock; 75971f9b66SMike Stump 7652d9c49dSMike Stump // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the 772d33d630SMike Stump // same thing as CreateRuntimeFunction if there's already a variable with the 782d33d630SMike Stump // same name. 79971f9b66SMike Stump NSConcreteGlobalBlock 80971f9b66SMike Stump = new llvm::GlobalVariable(PtrToInt8Ty, false, 81971f9b66SMike Stump llvm::GlobalValue::ExternalLinkage, 82971f9b66SMike Stump 0, "_NSConcreteGlobalBlock", 83971f9b66SMike Stump &getModule()); 84971f9b66SMike Stump 85971f9b66SMike Stump return NSConcreteGlobalBlock; 86971f9b66SMike Stump } 87971f9b66SMike Stump 8895435674SMike Stump llvm::Constant *BlockModule::getNSConcreteStackBlock() { 897ab278dbSMike Stump if (NSConcreteStackBlock) 907ab278dbSMike Stump return NSConcreteStackBlock; 917ab278dbSMike Stump 9252d9c49dSMike Stump // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the 932d33d630SMike Stump // same thing as CreateRuntimeFunction if there's already a variable with the 942d33d630SMike Stump // same name. 957ab278dbSMike Stump NSConcreteStackBlock 967ab278dbSMike Stump = new llvm::GlobalVariable(PtrToInt8Ty, false, 977ab278dbSMike Stump llvm::GlobalValue::ExternalLinkage, 987ab278dbSMike Stump 0, "_NSConcreteStackBlock", 997ab278dbSMike Stump &getModule()); 1007ab278dbSMike Stump 1017ab278dbSMike Stump return NSConcreteStackBlock; 1027ab278dbSMike Stump } 1037ab278dbSMike Stump 104ed5e69feSAnders Carlsson static void CollectBlockDeclRefInfo(const Stmt *S, 105ed5e69feSAnders Carlsson CodeGenFunction::BlockInfo &Info) { 106ed5e69feSAnders Carlsson for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); 107ed5e69feSAnders Carlsson I != E; ++I) 108dd942713SDaniel Dunbar if (*I) 109ed5e69feSAnders Carlsson CollectBlockDeclRefInfo(*I, Info); 110ed5e69feSAnders Carlsson 111ed5e69feSAnders Carlsson if (const BlockDeclRefExpr *DE = dyn_cast<BlockDeclRefExpr>(S)) { 112ed5e69feSAnders Carlsson // FIXME: Handle enums. 113ed5e69feSAnders Carlsson if (isa<FunctionDecl>(DE->getDecl())) 114ed5e69feSAnders Carlsson return; 115ed5e69feSAnders Carlsson 116ed5e69feSAnders Carlsson if (DE->isByRef()) 117ed5e69feSAnders Carlsson Info.ByRefDeclRefs.push_back(DE); 118ed5e69feSAnders Carlsson else 119ed5e69feSAnders Carlsson Info.ByCopyDeclRefs.push_back(DE); 120ed5e69feSAnders Carlsson } 121ed5e69feSAnders Carlsson } 122ed5e69feSAnders Carlsson 1232d33d630SMike Stump /// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be 1242d33d630SMike Stump /// declared as a global variable instead of on the stack. 125ed5e69feSAnders Carlsson static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info) 126ed5e69feSAnders Carlsson { 127ed5e69feSAnders Carlsson return Info.ByRefDeclRefs.empty() && Info.ByCopyDeclRefs.empty(); 128ed5e69feSAnders Carlsson } 129ed5e69feSAnders Carlsson 1302d33d630SMike Stump // FIXME: Push most into CGM, passing down a few bits, like current function 1312d33d630SMike Stump // name. 132b750d928SMike Stump llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) { 133ed5e69feSAnders Carlsson 134ed5e69feSAnders Carlsson std::string Name = CurFn->getName(); 135ed5e69feSAnders Carlsson CodeGenFunction::BlockInfo Info(0, Name.c_str()); 136ed5e69feSAnders Carlsson CollectBlockDeclRefInfo(BE->getBody(), Info); 137ed5e69feSAnders Carlsson 138ed5e69feSAnders Carlsson // Check if the block can be global. 1392d33d630SMike Stump // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like 1402d33d630SMike Stump // to just have one code path. We should move this function into CGM and pass 1412d33d630SMike Stump // CGF, then we can just check to see if CGF is 0. 14297d01d50SMike Stump if (0 && CanBlockBeGlobal(Info)) 143ed5e69feSAnders Carlsson return CGM.GetAddrOfGlobalBlock(BE, Name.c_str()); 14485284bacSMike Stump 1454446dcf0SMike Stump std::vector<llvm::Constant*> Elts(5); 14685284bacSMike Stump llvm::Constant *C; 147b750d928SMike Stump llvm::Value *V; 14885284bacSMike Stump 14985284bacSMike Stump { 15085284bacSMike Stump // C = BuildBlockStructInitlist(); 15185284bacSMike Stump unsigned int flags = BLOCK_HAS_DESCRIPTOR; 15285284bacSMike Stump 1534446dcf0SMike Stump // We run this first so that we set BlockHasCopyDispose from the entire 1544446dcf0SMike Stump // block literal. 1554446dcf0SMike Stump // __invoke 1564446dcf0SMike Stump uint64_t subBlockSize, subBlockAlign; 1574446dcf0SMike Stump llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; 1584446dcf0SMike Stump llvm::Function *Fn 159692c6e37SMike Stump = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl, LocalDeclMap, 1605469f29aSMike Stump subBlockSize, 1614446dcf0SMike Stump subBlockAlign, 1624446dcf0SMike Stump subBlockDeclRefDecls, 1634446dcf0SMike Stump BlockHasCopyDispose); 1644446dcf0SMike Stump Elts[3] = Fn; 1654446dcf0SMike Stump 1664446dcf0SMike Stump if (!Enable__block && BlockHasCopyDispose) 1674446dcf0SMike Stump ErrorUnsupported(BE, "block literal that requires copy/dispose"); 1684446dcf0SMike Stump 16985284bacSMike Stump if (BlockHasCopyDispose) 17085284bacSMike Stump flags |= BLOCK_HAS_COPY_DISPOSE; 17185284bacSMike Stump 172499ae7ecSMike Stump // __isa 1737ab278dbSMike Stump C = CGM.getNSConcreteStackBlock(); 17485284bacSMike Stump C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty); 1754446dcf0SMike Stump Elts[0] = C; 17685284bacSMike Stump 17785284bacSMike Stump // __flags 17885284bacSMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 17985284bacSMike Stump CGM.getTypes().ConvertType(CGM.getContext().IntTy)); 18085284bacSMike Stump C = llvm::ConstantInt::get(IntTy, flags); 1814446dcf0SMike Stump Elts[1] = C; 18285284bacSMike Stump 18385284bacSMike Stump // __reserved 18485284bacSMike Stump C = llvm::ConstantInt::get(IntTy, 0); 1854446dcf0SMike Stump Elts[2] = C; 18685284bacSMike Stump 187b750d928SMike Stump if (subBlockDeclRefDecls.size() == 0) { 188d6ef62f0SMike Stump // __descriptor 189aeb0ffd5SMike Stump Elts[4] = BuildDescriptorBlockDecl(subBlockSize, 0, 0); 190d6ef62f0SMike Stump 1913a139f37SMike Stump // Optimize to being a global block. 1923a139f37SMike Stump Elts[0] = CGM.getNSConcreteGlobalBlock(); 1933a139f37SMike Stump Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL); 1943a139f37SMike Stump 19585284bacSMike Stump C = llvm::ConstantStruct::get(Elts); 19685284bacSMike Stump 19785284bacSMike Stump char Name[32]; 1989319db8aSMike Stump sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount()); 19985284bacSMike Stump C = new llvm::GlobalVariable(C->getType(), true, 20085284bacSMike Stump llvm::GlobalValue::InternalLinkage, 20185284bacSMike Stump C, Name, &CGM.getModule()); 2025d2534adSMike Stump QualType BPT = BE->getType(); 2035d2534adSMike Stump C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT)); 20485284bacSMike Stump return C; 20585284bacSMike Stump } 20685284bacSMike Stump 207b750d928SMike Stump std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size()); 208aeb0ffd5SMike Stump for (int i=0; i<4; ++i) 209b750d928SMike Stump Types[i] = Elts[i]->getType(); 210aeb0ffd5SMike Stump Types[4] = PtrToInt8Ty; 21185284bacSMike Stump 2121db7d04bSMike Stump for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) { 2131db7d04bSMike Stump const Expr *E = subBlockDeclRefDecls[i]; 2141db7d04bSMike Stump const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); 2151db7d04bSMike Stump QualType Ty = E->getType(); 21697d01d50SMike Stump if (BDRE && BDRE->isByRef()) { 21797d01d50SMike Stump uint64_t Align = getContext().getDeclAlignInBytes(BDRE->getDecl()); 21897d01d50SMike Stump Types[i+5] = llvm::PointerType::get(BuildByRefType(Ty, Align), 0); 21997d01d50SMike Stump } else 2201db7d04bSMike Stump Types[i+5] = ConvertType(Ty); 2211db7d04bSMike Stump } 222b750d928SMike Stump 223aeb0ffd5SMike Stump llvm::StructType *Ty = llvm::StructType::get(Types, true); 224b750d928SMike Stump 225b750d928SMike Stump llvm::AllocaInst *A = CreateTempAlloca(Ty); 226b750d928SMike Stump A->setAlignment(subBlockAlign); 227b750d928SMike Stump V = A; 228b750d928SMike Stump 229aeb0ffd5SMike Stump std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size()); 230aeb0ffd5SMike Stump int helpersize = 0; 231aeb0ffd5SMike Stump 232aeb0ffd5SMike Stump for (unsigned i=0; i<4; ++i) 233b750d928SMike Stump Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp")); 234b750d928SMike Stump 235b750d928SMike Stump for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) 236b750d928SMike Stump { 2371db7d04bSMike Stump // FIXME: Push const down. 2381db7d04bSMike Stump Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]); 2391db7d04bSMike Stump DeclRefExpr *DR; 2401db7d04bSMike Stump ValueDecl *VD; 241b750d928SMike Stump 2421db7d04bSMike Stump DR = dyn_cast<DeclRefExpr>(E); 2431db7d04bSMike Stump // Skip padding. 2441db7d04bSMike Stump if (DR) continue; 2451db7d04bSMike Stump 2461db7d04bSMike Stump BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); 2471db7d04bSMike Stump VD = BDRE->getDecl(); 2481db7d04bSMike Stump 24997d01d50SMike Stump llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp"); 250aeb0ffd5SMike Stump NoteForHelper[helpersize].index = i+5; 251aeb0ffd5SMike Stump NoteForHelper[helpersize].RequiresCopying = BlockRequiresCopying(VD->getType()); 252aeb0ffd5SMike Stump NoteForHelper[helpersize].flag 253aeb0ffd5SMike Stump = VD->getType()->isBlockPointerType() ? BLOCK_FIELD_IS_BLOCK : BLOCK_FIELD_IS_OBJECT; 254aeb0ffd5SMike Stump 2551db7d04bSMike Stump if (LocalDeclMap[VD]) { 25697d01d50SMike Stump if (BDRE->isByRef()) { 257aeb0ffd5SMike Stump NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF | 25890d8daf5SMike Stump // FIXME: Someone double check this. 25990d8daf5SMike Stump (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0); 26097d01d50SMike Stump const llvm::Type *Ty = Types[i+5]; 26197d01d50SMike Stump llvm::Value *Loc = LocalDeclMap[VD]; 26297d01d50SMike Stump Loc = Builder.CreateStructGEP(Loc, 1, "forwarding"); 26397d01d50SMike Stump Loc = Builder.CreateLoad(Loc, false); 26497d01d50SMike Stump Loc = Builder.CreateBitCast(Loc, Ty); 26597d01d50SMike Stump Builder.CreateStore(Loc, Addr); 266aeb0ffd5SMike Stump ++helpersize; 26797d01d50SMike Stump continue; 26897d01d50SMike Stump } else 2691db7d04bSMike Stump E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD), 2701db7d04bSMike Stump VD->getType(), SourceLocation(), 2711db7d04bSMike Stump false, false); 2721db7d04bSMike Stump } 27397d01d50SMike Stump if (BDRE->isByRef()) { 274*efd7caa8SMike Stump NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF | 275*efd7caa8SMike Stump // FIXME: Someone double check this. 276*efd7caa8SMike Stump (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0); 2771db7d04bSMike Stump E = new (getContext()) 2781db7d04bSMike Stump UnaryOperator(E, UnaryOperator::AddrOf, 2791db7d04bSMike Stump getContext().getPointerType(E->getType()), 2801db7d04bSMike Stump SourceLocation()); 28197d01d50SMike Stump } 282aeb0ffd5SMike Stump ++helpersize; 2831db7d04bSMike Stump 2841db7d04bSMike Stump RValue r = EmitAnyExpr(E, Addr, false); 28597d01d50SMike Stump if (r.isScalar()) { 28697d01d50SMike Stump llvm::Value *Loc = r.getScalarVal(); 28797d01d50SMike Stump const llvm::Type *Ty = Types[i+5]; 28897d01d50SMike Stump if (BDRE->isByRef()) { 2892d33d630SMike Stump // E is now the address of the value field, instead, we want the 2902d33d630SMike Stump // address of the actual ByRef struct. We optimize this slightly 2912d33d630SMike Stump // compared to gcc by not grabbing the forwarding slot as this must 2922d33d630SMike Stump // be done during Block_copy for us, and we can postpone the work 2932d33d630SMike Stump // until then. 2942d33d630SMike Stump uint64_t offset = BlockDecls[BDRE->getDecl()]; 2952d33d630SMike Stump 2962d33d630SMike Stump llvm::Value *BlockLiteral = LoadBlockStruct(); 2972d33d630SMike Stump 2982d33d630SMike Stump Loc = Builder.CreateGEP(BlockLiteral, 2992d33d630SMike Stump llvm::ConstantInt::get(llvm::Type::Int64Ty, 3002d33d630SMike Stump offset), 3012d33d630SMike Stump "block.literal"); 3022d33d630SMike Stump Ty = llvm::PointerType::get(Ty, 0); 30397d01d50SMike Stump Loc = Builder.CreateBitCast(Loc, Ty); 3042d33d630SMike Stump Loc = Builder.CreateLoad(Loc, false); 3052d33d630SMike Stump // Loc = Builder.CreateBitCast(Loc, Ty); 30697d01d50SMike Stump } 30797d01d50SMike Stump Builder.CreateStore(Loc, Addr); 30897d01d50SMike Stump } else if (r.isComplex()) 309b750d928SMike Stump // FIXME: implement 310b750d928SMike Stump ErrorUnsupported(BE, "complex in block literal"); 311b750d928SMike Stump else if (r.isAggregate()) 312b750d928SMike Stump ; // Already created into the destination 313b750d928SMike Stump else 314b750d928SMike Stump assert (0 && "bad block variable"); 315b750d928SMike Stump // FIXME: Ensure that the offset created by the backend for 316b750d928SMike Stump // the struct matches the previously computed offset in BlockDecls. 317b750d928SMike Stump } 318aeb0ffd5SMike Stump NoteForHelper.resize(helpersize); 319d6ef62f0SMike Stump 320d6ef62f0SMike Stump // __descriptor 321aeb0ffd5SMike Stump llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockSize, Ty, 322aeb0ffd5SMike Stump &NoteForHelper); 323aeb0ffd5SMike Stump Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty); 324aeb0ffd5SMike Stump Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp")); 325b750d928SMike Stump } 326b750d928SMike Stump 327b750d928SMike Stump QualType BPT = BE->getType(); 328b750d928SMike Stump return Builder.CreateBitCast(V, ConvertType(BPT)); 329b750d928SMike Stump } 33085284bacSMike Stump 33185284bacSMike Stump 33295435674SMike Stump const llvm::Type *BlockModule::getBlockDescriptorType() { 333650c932dSMike Stump if (BlockDescriptorType) 334650c932dSMike Stump return BlockDescriptorType; 3352437cbfaSAnders Carlsson 3362437cbfaSAnders Carlsson const llvm::Type *UnsignedLongTy = 337650c932dSMike Stump getTypes().ConvertType(getContext().UnsignedLongTy); 3382437cbfaSAnders Carlsson 3392437cbfaSAnders Carlsson // struct __block_descriptor { 3402437cbfaSAnders Carlsson // unsigned long reserved; 3412437cbfaSAnders Carlsson // unsigned long block_size; 3422437cbfaSAnders Carlsson // }; 343650c932dSMike Stump BlockDescriptorType = llvm::StructType::get(UnsignedLongTy, 3442437cbfaSAnders Carlsson UnsignedLongTy, 3452437cbfaSAnders Carlsson NULL); 3462437cbfaSAnders Carlsson 347650c932dSMike Stump getModule().addTypeName("struct.__block_descriptor", 348650c932dSMike Stump BlockDescriptorType); 3492437cbfaSAnders Carlsson 350650c932dSMike Stump return BlockDescriptorType; 3512437cbfaSAnders Carlsson } 3522437cbfaSAnders Carlsson 35395435674SMike Stump const llvm::Type *BlockModule::getGenericBlockLiteralType() { 354005c9a62SMike Stump if (GenericBlockLiteralType) 355005c9a62SMike Stump return GenericBlockLiteralType; 3562437cbfaSAnders Carlsson 3572437cbfaSAnders Carlsson const llvm::Type *BlockDescPtrTy = 358005c9a62SMike Stump llvm::PointerType::getUnqual(getBlockDescriptorType()); 3592437cbfaSAnders Carlsson 36092bbd6d4SMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 36192bbd6d4SMike Stump getTypes().ConvertType(getContext().IntTy)); 36292bbd6d4SMike Stump 3632437cbfaSAnders Carlsson // struct __block_literal_generic { 3645d2534adSMike Stump // void *__isa; 3655d2534adSMike Stump // int __flags; 3665d2534adSMike Stump // int __reserved; 3675d2534adSMike Stump // void (*__invoke)(void *); 3685d2534adSMike Stump // struct __block_descriptor *__descriptor; 3692437cbfaSAnders Carlsson // }; 370626aecc4SMike Stump GenericBlockLiteralType = llvm::StructType::get(PtrToInt8Ty, 37192bbd6d4SMike Stump IntTy, 37292bbd6d4SMike Stump IntTy, 373626aecc4SMike Stump PtrToInt8Ty, 3742437cbfaSAnders Carlsson BlockDescPtrTy, 3752437cbfaSAnders Carlsson NULL); 3762437cbfaSAnders Carlsson 377005c9a62SMike Stump getModule().addTypeName("struct.__block_literal_generic", 378005c9a62SMike Stump GenericBlockLiteralType); 3792437cbfaSAnders Carlsson 380005c9a62SMike Stump return GenericBlockLiteralType; 3812437cbfaSAnders Carlsson } 3822437cbfaSAnders Carlsson 38395435674SMike Stump const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() { 3845d2534adSMike Stump if (GenericExtendedBlockLiteralType) 3855d2534adSMike Stump return GenericExtendedBlockLiteralType; 3865d2534adSMike Stump 3875d2534adSMike Stump const llvm::Type *BlockDescPtrTy = 3885d2534adSMike Stump llvm::PointerType::getUnqual(getBlockDescriptorType()); 3895d2534adSMike Stump 3905d2534adSMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 3915d2534adSMike Stump getTypes().ConvertType(getContext().IntTy)); 3925d2534adSMike Stump 3935d2534adSMike Stump // struct __block_literal_generic { 3945d2534adSMike Stump // void *__isa; 3955d2534adSMike Stump // int __flags; 3965d2534adSMike Stump // int __reserved; 3975d2534adSMike Stump // void (*__invoke)(void *); 3985d2534adSMike Stump // struct __block_descriptor *__descriptor; 3995d2534adSMike Stump // void *__copy_func_helper_decl; 4005d2534adSMike Stump // void *__destroy_func_decl; 4015d2534adSMike Stump // }; 402626aecc4SMike Stump GenericExtendedBlockLiteralType = llvm::StructType::get(PtrToInt8Ty, 4035d2534adSMike Stump IntTy, 4045d2534adSMike Stump IntTy, 405626aecc4SMike Stump PtrToInt8Ty, 4065d2534adSMike Stump BlockDescPtrTy, 407626aecc4SMike Stump PtrToInt8Ty, 408626aecc4SMike Stump PtrToInt8Ty, 4095d2534adSMike Stump NULL); 4105d2534adSMike Stump 4115d2534adSMike Stump getModule().addTypeName("struct.__block_literal_extended_generic", 4125d2534adSMike Stump GenericExtendedBlockLiteralType); 4135d2534adSMike Stump 4145d2534adSMike Stump return GenericExtendedBlockLiteralType; 4155d2534adSMike Stump } 4165d2534adSMike Stump 4172437cbfaSAnders Carlsson /// getBlockFunctionType - Given a BlockPointerType, will return the 4182437cbfaSAnders Carlsson /// function type for the block, including the first block literal argument. 4192437cbfaSAnders Carlsson static QualType getBlockFunctionType(ASTContext &Ctx, 4206a60fa24SAnders Carlsson const BlockPointerType *BPT) { 421deaad8ccSDouglas Gregor const FunctionProtoType *FTy = cast<FunctionProtoType>(BPT->getPointeeType()); 4222437cbfaSAnders Carlsson 4232437cbfaSAnders Carlsson llvm::SmallVector<QualType, 8> Types; 4242437cbfaSAnders Carlsson Types.push_back(Ctx.getPointerType(Ctx.VoidTy)); 4252437cbfaSAnders Carlsson 426deaad8ccSDouglas Gregor for (FunctionProtoType::arg_type_iterator i = FTy->arg_type_begin(), 4272437cbfaSAnders Carlsson e = FTy->arg_type_end(); i != e; ++i) 4282437cbfaSAnders Carlsson Types.push_back(*i); 4292437cbfaSAnders Carlsson 4302437cbfaSAnders Carlsson return Ctx.getFunctionType(FTy->getResultType(), 4312437cbfaSAnders Carlsson &Types[0], Types.size(), 4322437cbfaSAnders Carlsson FTy->isVariadic(), 0); 4332437cbfaSAnders Carlsson } 4342437cbfaSAnders Carlsson 4356a60fa24SAnders Carlsson RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) { 4362437cbfaSAnders Carlsson const BlockPointerType *BPT = 4372437cbfaSAnders Carlsson E->getCallee()->getType()->getAsBlockPointerType(); 4382437cbfaSAnders Carlsson 4392437cbfaSAnders Carlsson llvm::Value *Callee = EmitScalarExpr(E->getCallee()); 4402437cbfaSAnders Carlsson 4412437cbfaSAnders Carlsson // Get a pointer to the generic block literal. 4422437cbfaSAnders Carlsson const llvm::Type *BlockLiteralTy = 443005c9a62SMike Stump llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType()); 4442437cbfaSAnders Carlsson 4452437cbfaSAnders Carlsson // Bitcast the callee to a block literal. 4462437cbfaSAnders Carlsson llvm::Value *BlockLiteral = 4472437cbfaSAnders Carlsson Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal"); 4482437cbfaSAnders Carlsson 4492437cbfaSAnders Carlsson // Get the function pointer from the literal. 4502437cbfaSAnders Carlsson llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp"); 451624497c2SMike Stump llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp"); 4522437cbfaSAnders Carlsson 4532437cbfaSAnders Carlsson // Cast the function pointer to the right type. 4542437cbfaSAnders Carlsson const llvm::Type *BlockFTy = 4552437cbfaSAnders Carlsson ConvertType(getBlockFunctionType(getContext(), BPT)); 4562437cbfaSAnders Carlsson const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy); 4572437cbfaSAnders Carlsson Func = Builder.CreateBitCast(Func, BlockFTyPtr); 4582437cbfaSAnders Carlsson 4592437cbfaSAnders Carlsson BlockLiteral = 4602437cbfaSAnders Carlsson Builder.CreateBitCast(BlockLiteral, 4612437cbfaSAnders Carlsson llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4622437cbfaSAnders Carlsson "tmp"); 4632437cbfaSAnders Carlsson 4642437cbfaSAnders Carlsson // Add the block literal. 4652437cbfaSAnders Carlsson QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy); 4662437cbfaSAnders Carlsson CallArgList Args; 4672437cbfaSAnders Carlsson Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy)); 4682437cbfaSAnders Carlsson 4692437cbfaSAnders Carlsson // And the rest of the arguments. 4702437cbfaSAnders Carlsson for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end(); 4712437cbfaSAnders Carlsson i != e; ++i) 4722437cbfaSAnders Carlsson Args.push_back(std::make_pair(EmitAnyExprToTemp(*i), 4732437cbfaSAnders Carlsson i->getType())); 4742437cbfaSAnders Carlsson 4752437cbfaSAnders Carlsson // And call the block. 4762437cbfaSAnders Carlsson return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args), 4772437cbfaSAnders Carlsson Func, Args); 4782437cbfaSAnders Carlsson } 4796a60fa24SAnders Carlsson 48097d01d50SMike Stump llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) { 48197d01d50SMike Stump uint64_t &offset = BlockDecls[E->getDecl()]; 48297d01d50SMike Stump 48397d01d50SMike Stump const llvm::Type *Ty; 48497d01d50SMike Stump Ty = CGM.getTypes().ConvertType(E->getDecl()->getType()); 48597d01d50SMike Stump 4860874160bSMike Stump if (!Enable__block && E->isByRef()) 48797d01d50SMike Stump ErrorUnsupported(E, "__block variable in block literal"); 4880c743277SMike Stump else if (!Enable__block && E->getType()->isBlockPointerType()) 48997d01d50SMike Stump ErrorUnsupported(E, "block pointer in block literal"); 490d9728222SMike Stump else if (!Enable__block && (E->getDecl()->getAttr<ObjCNSObjectAttr>() || 491d9728222SMike Stump getContext().isObjCNSObjectType(E->getType()))) 49297d01d50SMike Stump ErrorUnsupported(E, "__attribute__((NSObject)) variable in block " 49397d01d50SMike Stump "literal"); 4940c743277SMike Stump else if (!Enable__block && getContext().isObjCObjectPointerType(E->getType())) 49597d01d50SMike Stump ErrorUnsupported(E, "Objective-C variable in block literal"); 49697d01d50SMike Stump 49797d01d50SMike Stump // See if we have already allocated an offset for this variable. 49897d01d50SMike Stump if (offset == 0) { 4994446dcf0SMike Stump // Don't run the expensive check, unless we have to. 5004446dcf0SMike Stump if (!BlockHasCopyDispose && BlockRequiresCopying(E->getType())) 5014446dcf0SMike Stump BlockHasCopyDispose = true; 50297d01d50SMike Stump // if not, allocate one now. 50397d01d50SMike Stump offset = getBlockOffset(E); 50497d01d50SMike Stump } 50597d01d50SMike Stump 50697d01d50SMike Stump llvm::Value *BlockLiteral = LoadBlockStruct(); 50797d01d50SMike Stump llvm::Value *V = Builder.CreateGEP(BlockLiteral, 50897d01d50SMike Stump llvm::ConstantInt::get(llvm::Type::Int64Ty, 50997d01d50SMike Stump offset), 5102d33d630SMike Stump "block.literal"); 51197d01d50SMike Stump if (E->isByRef()) { 51297d01d50SMike Stump bool needsCopyDispose = BlockRequiresCopying(E->getType()); 51397d01d50SMike Stump uint64_t Align = getContext().getDeclAlignInBytes(E->getDecl()); 51497d01d50SMike Stump const llvm::Type *PtrStructTy 51597d01d50SMike Stump = llvm::PointerType::get(BuildByRefType(E->getType(), Align), 0); 516*efd7caa8SMike Stump // The block literal will need a copy/destroy helper. 517*efd7caa8SMike Stump BlockHasCopyDispose = true; 51897d01d50SMike Stump Ty = PtrStructTy; 51997d01d50SMike Stump Ty = llvm::PointerType::get(Ty, 0); 52097d01d50SMike Stump V = Builder.CreateBitCast(V, Ty); 52197d01d50SMike Stump V = Builder.CreateLoad(V, false); 52297d01d50SMike Stump V = Builder.CreateStructGEP(V, 1, "forwarding"); 52397d01d50SMike Stump V = Builder.CreateLoad(V, false); 52497d01d50SMike Stump V = Builder.CreateBitCast(V, PtrStructTy); 52597d01d50SMike Stump V = Builder.CreateStructGEP(V, needsCopyDispose*2 + 4, "x"); 52697d01d50SMike Stump } else { 52797d01d50SMike Stump Ty = llvm::PointerType::get(Ty, 0); 52897d01d50SMike Stump V = Builder.CreateBitCast(V, Ty); 52997d01d50SMike Stump } 53097d01d50SMike Stump return V; 53197d01d50SMike Stump } 53297d01d50SMike Stump 533692c6e37SMike Stump void CodeGenFunction::BlockForwardSelf() { 534692c6e37SMike Stump const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); 535692c6e37SMike Stump ImplicitParamDecl *SelfDecl = OMD->getSelfDecl(); 536692c6e37SMike Stump llvm::Value *&DMEntry = LocalDeclMap[SelfDecl]; 537692c6e37SMike Stump if (DMEntry) 538692c6e37SMike Stump return; 539692c6e37SMike Stump // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care 540692c6e37SMike Stump BlockDeclRefExpr *BDRE = new (getContext()) 541692c6e37SMike Stump BlockDeclRefExpr(SelfDecl, 542692c6e37SMike Stump SelfDecl->getType(), SourceLocation(), false); 543692c6e37SMike Stump DMEntry = GetAddrOfBlockDecl(BDRE); 544692c6e37SMike Stump } 545692c6e37SMike Stump 5462d5a2878SMike Stump llvm::Constant * 5476c39666aSMike Stump BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) { 5486a60fa24SAnders Carlsson // Generate the block descriptor. 5496a60fa24SAnders Carlsson const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy); 55092bbd6d4SMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 55192bbd6d4SMike Stump getTypes().ConvertType(getContext().IntTy)); 5526a60fa24SAnders Carlsson 5536a60fa24SAnders Carlsson llvm::Constant *DescriptorFields[2]; 5546a60fa24SAnders Carlsson 5556a60fa24SAnders Carlsson // Reserved 5566a60fa24SAnders Carlsson DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy); 5576a60fa24SAnders Carlsson 5586a60fa24SAnders Carlsson // Block literal size. For global blocks we just use the size of the generic 5596a60fa24SAnders Carlsson // block literal struct. 5606a60fa24SAnders Carlsson uint64_t BlockLiteralSize = 561005c9a62SMike Stump TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8; 5626a60fa24SAnders Carlsson DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize); 5636a60fa24SAnders Carlsson 5646a60fa24SAnders Carlsson llvm::Constant *DescriptorStruct = 5656a60fa24SAnders Carlsson llvm::ConstantStruct::get(&DescriptorFields[0], 2); 5666a60fa24SAnders Carlsson 5676a60fa24SAnders Carlsson llvm::GlobalVariable *Descriptor = 5686a60fa24SAnders Carlsson new llvm::GlobalVariable(DescriptorStruct->getType(), true, 5696a60fa24SAnders Carlsson llvm::GlobalVariable::InternalLinkage, 5706a60fa24SAnders Carlsson DescriptorStruct, "__block_descriptor_global", 5716a60fa24SAnders Carlsson &getModule()); 5726a60fa24SAnders Carlsson 5736a60fa24SAnders Carlsson // Generate the constants for the block literal. 5746a60fa24SAnders Carlsson llvm::Constant *LiteralFields[5]; 5756a60fa24SAnders Carlsson 5762d5a2878SMike Stump CodeGenFunction::BlockInfo Info(0, n); 577b750d928SMike Stump uint64_t subBlockSize, subBlockAlign; 5781db7d04bSMike Stump llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; 579a61bb4d1SDaniel Dunbar bool subBlockHasCopyDispose = false; 5805469f29aSMike Stump llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap; 581cb2fbcb0SMike Stump llvm::Function *Fn 582692c6e37SMike Stump = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap, 5835469f29aSMike Stump subBlockSize, 584b750d928SMike Stump subBlockAlign, 5854446dcf0SMike Stump subBlockDeclRefDecls, 5864446dcf0SMike Stump subBlockHasCopyDispose); 587cb2fbcb0SMike Stump assert(subBlockSize == BlockLiteralSize 588cb2fbcb0SMike Stump && "no imports allowed for global block"); 589a61bb4d1SDaniel Dunbar assert(!subBlockHasCopyDispose && "no imports allowed for global block"); 5906a60fa24SAnders Carlsson 5916a60fa24SAnders Carlsson // isa 592971f9b66SMike Stump LiteralFields[0] = getNSConcreteGlobalBlock(); 5936a60fa24SAnders Carlsson 5946a60fa24SAnders Carlsson // Flags 5955878c797SAnders Carlsson LiteralFields[1] = 5965878c797SAnders Carlsson llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR); 5976a60fa24SAnders Carlsson 5986a60fa24SAnders Carlsson // Reserved 59992bbd6d4SMike Stump LiteralFields[2] = llvm::Constant::getNullValue(IntTy); 6006a60fa24SAnders Carlsson 6016a60fa24SAnders Carlsson // Function 6026a60fa24SAnders Carlsson LiteralFields[3] = Fn; 6036a60fa24SAnders Carlsson 6046a60fa24SAnders Carlsson // Descriptor 6056a60fa24SAnders Carlsson LiteralFields[4] = Descriptor; 6066a60fa24SAnders Carlsson 6076a60fa24SAnders Carlsson llvm::Constant *BlockLiteralStruct = 6086a60fa24SAnders Carlsson llvm::ConstantStruct::get(&LiteralFields[0], 5); 6096a60fa24SAnders Carlsson 6106a60fa24SAnders Carlsson llvm::GlobalVariable *BlockLiteral = 6116a60fa24SAnders Carlsson new llvm::GlobalVariable(BlockLiteralStruct->getType(), true, 6126a60fa24SAnders Carlsson llvm::GlobalVariable::InternalLinkage, 6136a60fa24SAnders Carlsson BlockLiteralStruct, "__block_literal_global", 6146a60fa24SAnders Carlsson &getModule()); 6156a60fa24SAnders Carlsson 6166a60fa24SAnders Carlsson return BlockLiteral; 6176a60fa24SAnders Carlsson } 6186a60fa24SAnders Carlsson 619cb2fbcb0SMike Stump llvm::Value *CodeGenFunction::LoadBlockStruct() { 620cb2fbcb0SMike Stump return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self"); 621cb2fbcb0SMike Stump } 622cb2fbcb0SMike Stump 6234446dcf0SMike Stump llvm::Function * 6244446dcf0SMike Stump CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr, 625cb2fbcb0SMike Stump const BlockInfo& Info, 626692c6e37SMike Stump const Decl *OuterFuncDecl, 6275469f29aSMike Stump llvm::DenseMap<const Decl*, llvm::Value*> ldm, 628b750d928SMike Stump uint64_t &Size, 629b750d928SMike Stump uint64_t &Align, 6304446dcf0SMike Stump llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls, 6314446dcf0SMike Stump bool &subBlockHasCopyDispose) { 6325469f29aSMike Stump // Arrange for local static and local extern declarations to appear 6335469f29aSMike Stump // to be local to this function as well, as they are directly referenced 6345469f29aSMike Stump // in a block. 6355469f29aSMike Stump for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin(); 6365469f29aSMike Stump i != ldm.end(); 6375469f29aSMike Stump ++i) { 6385469f29aSMike Stump const VarDecl *VD = dyn_cast<VarDecl>(i->first); 6395469f29aSMike Stump 6405469f29aSMike Stump if (VD->getStorageClass() == VarDecl::Static 6415469f29aSMike Stump || VD->getStorageClass() == VarDecl::Extern) 6425469f29aSMike Stump LocalDeclMap[VD] = i->second; 6435469f29aSMike Stump } 6445469f29aSMike Stump 645deaad8ccSDouglas Gregor const FunctionProtoType *FTy = 6463385fe12SChris Lattner cast<FunctionProtoType>(BExpr->getFunctionType()); 6476a60fa24SAnders Carlsson 6486a60fa24SAnders Carlsson FunctionArgList Args; 6496a60fa24SAnders Carlsson 6503385fe12SChris Lattner const BlockDecl *BD = BExpr->getBlockDecl(); 6516a60fa24SAnders Carlsson 6526a60fa24SAnders Carlsson // FIXME: This leaks 6536a60fa24SAnders Carlsson ImplicitParamDecl *SelfDecl = 6546a60fa24SAnders Carlsson ImplicitParamDecl::Create(getContext(), 0, 6556a60fa24SAnders Carlsson SourceLocation(), 0, 6566a60fa24SAnders Carlsson getContext().getPointerType(getContext().VoidTy)); 6576a60fa24SAnders Carlsson 6586a60fa24SAnders Carlsson Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType())); 659cb2fbcb0SMike Stump BlockStructDecl = SelfDecl; 6606a60fa24SAnders Carlsson 661c4b30e59SSteve Naroff for (BlockDecl::param_const_iterator i = BD->param_begin(), 6626a60fa24SAnders Carlsson e = BD->param_end(); i != e; ++i) 663eecd39f2SMike Stump Args.push_back(std::make_pair(*i, (*i)->getType())); 6646a60fa24SAnders Carlsson 6656a60fa24SAnders Carlsson const CGFunctionInfo &FI = 6666a60fa24SAnders Carlsson CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args); 6676a60fa24SAnders Carlsson 6682d5a2878SMike Stump std::string Name = std::string("__") + Info.Name + "_block_invoke_"; 6696a60fa24SAnders Carlsson CodeGenTypes &Types = CGM.getTypes(); 6706a60fa24SAnders Carlsson const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic()); 6716a60fa24SAnders Carlsson 6726a60fa24SAnders Carlsson llvm::Function *Fn = 6736a60fa24SAnders Carlsson llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 6746a60fa24SAnders Carlsson Name, 6756a60fa24SAnders Carlsson &CGM.getModule()); 6766a60fa24SAnders Carlsson 6776a60fa24SAnders Carlsson StartFunction(BD, FTy->getResultType(), Fn, Args, 6783385fe12SChris Lattner BExpr->getBody()->getLocEnd()); 679692c6e37SMike Stump CurFuncDecl = OuterFuncDecl; 6803385fe12SChris Lattner EmitStmt(BExpr->getBody()); 6813385fe12SChris Lattner FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc()); 6826a60fa24SAnders Carlsson 683b750d928SMike Stump // The runtime needs a minimum alignment of a void *. 684b750d928SMike Stump uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 685b750d928SMike Stump BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign); 686b750d928SMike Stump 687cb2fbcb0SMike Stump Size = BlockOffset; 688b750d928SMike Stump Align = BlockAlign; 689b750d928SMike Stump subBlockDeclRefDecls = BlockDeclRefDecls; 6904446dcf0SMike Stump subBlockHasCopyDispose |= BlockHasCopyDispose; 6916a60fa24SAnders Carlsson return Fn; 6926a60fa24SAnders Carlsson } 6931db7d04bSMike Stump 694aeb0ffd5SMike Stump uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) { 6951db7d04bSMike Stump const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl()); 6961db7d04bSMike Stump 6971db7d04bSMike Stump uint64_t Size = getContext().getTypeSize(D->getType()) / 8; 6981db7d04bSMike Stump uint64_t Align = getContext().getDeclAlignInBytes(D); 6991db7d04bSMike Stump 7001db7d04bSMike Stump if (BDRE->isByRef()) { 7011db7d04bSMike Stump Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8; 7021db7d04bSMike Stump Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 7031db7d04bSMike Stump } 7041db7d04bSMike Stump 7051db7d04bSMike Stump assert ((Align > 0) && "alignment must be 1 byte or more"); 7061db7d04bSMike Stump 7071db7d04bSMike Stump uint64_t OldOffset = BlockOffset; 7081db7d04bSMike Stump 7091db7d04bSMike Stump // Ensure proper alignment, even if it means we have to have a gap 7101db7d04bSMike Stump BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align); 7111db7d04bSMike Stump BlockAlign = std::max(Align, BlockAlign); 7121db7d04bSMike Stump 7131db7d04bSMike Stump uint64_t Pad = BlockOffset - OldOffset; 7141db7d04bSMike Stump if (Pad) { 7151db7d04bSMike Stump llvm::ArrayType::get(llvm::Type::Int8Ty, Pad); 7161db7d04bSMike Stump QualType PadTy = getContext().getConstantArrayType(getContext().CharTy, 7171db7d04bSMike Stump llvm::APInt(32, Pad), 7181db7d04bSMike Stump ArrayType::Normal, 0); 7191db7d04bSMike Stump ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(), 7201db7d04bSMike Stump 0, QualType(PadTy), VarDecl::None, 7211db7d04bSMike Stump SourceLocation()); 7221db7d04bSMike Stump Expr *E; 7231db7d04bSMike Stump E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(), 7241db7d04bSMike Stump SourceLocation(), false, false); 7251db7d04bSMike Stump BlockDeclRefDecls.push_back(E); 7261db7d04bSMike Stump } 7271db7d04bSMike Stump BlockDeclRefDecls.push_back(BDRE); 7281db7d04bSMike Stump 7291db7d04bSMike Stump BlockOffset += Size; 7301db7d04bSMike Stump return BlockOffset-Size; 7311db7d04bSMike Stump } 73297d01d50SMike Stump 733aeb0ffd5SMike Stump llvm::Constant *BlockFunction:: 734aeb0ffd5SMike Stump GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T, 735aeb0ffd5SMike Stump std::vector<HelperInfo> &NoteForHelper) { 7360c743277SMike Stump QualType R = getContext().VoidTy; 7370c743277SMike Stump 7380c743277SMike Stump FunctionArgList Args; 7390c743277SMike Stump // FIXME: This leaks 740aeb0ffd5SMike Stump ImplicitParamDecl *Dst = 741aeb0ffd5SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 742aeb0ffd5SMike Stump getContext().getPointerType(getContext().VoidTy)); 743aeb0ffd5SMike Stump Args.push_back(std::make_pair(Dst, Dst->getType())); 7440c743277SMike Stump ImplicitParamDecl *Src = 7450c743277SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 7460c743277SMike Stump getContext().getPointerType(getContext().VoidTy)); 7470c743277SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 7480c743277SMike Stump 7490c743277SMike Stump const CGFunctionInfo &FI = 7500c743277SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 7510c743277SMike Stump 7520c743277SMike Stump std::string Name = std::string("__copy_helper_block_"); 7530c743277SMike Stump CodeGenTypes &Types = CGM.getTypes(); 7540c743277SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 7550c743277SMike Stump 7560c743277SMike Stump llvm::Function *Fn = 7570c743277SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 7580c743277SMike Stump Name, 7590c743277SMike Stump &CGM.getModule()); 7600c743277SMike Stump 7610c743277SMike Stump IdentifierInfo *II 7620c743277SMike Stump = &CGM.getContext().Idents.get("__copy_helper_block_"); 7630c743277SMike Stump 7640c743277SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 7650c743277SMike Stump getContext().getTranslationUnitDecl(), 7660c743277SMike Stump SourceLocation(), II, R, 7670c743277SMike Stump FunctionDecl::Static, false, 7680c743277SMike Stump true); 7690c743277SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 770aeb0ffd5SMike Stump 771aeb0ffd5SMike Stump llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); 772aeb0ffd5SMike Stump llvm::Type *PtrPtrT; 773aeb0ffd5SMike Stump PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 774aeb0ffd5SMike Stump SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); 775aeb0ffd5SMike Stump SrcObj = Builder.CreateLoad(SrcObj); 776aeb0ffd5SMike Stump 777aeb0ffd5SMike Stump llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst); 778aeb0ffd5SMike Stump DstObj = Builder.CreateBitCast(DstObj, llvm::PointerType::get(T, 0)); 779aeb0ffd5SMike Stump 780aeb0ffd5SMike Stump for (unsigned i=0; i < NoteForHelper.size(); ++i) { 781aeb0ffd5SMike Stump int flag = NoteForHelper[i].flag; 782aeb0ffd5SMike Stump int index = NoteForHelper[i].index; 783aeb0ffd5SMike Stump 784aeb0ffd5SMike Stump if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) 785aeb0ffd5SMike Stump || NoteForHelper[i].RequiresCopying) { 786aeb0ffd5SMike Stump llvm::Value *Srcv = SrcObj; 787aeb0ffd5SMike Stump Srcv = Builder.CreateStructGEP(Srcv, index); 788aeb0ffd5SMike Stump Srcv = Builder.CreateBitCast(Srcv, 789aeb0ffd5SMike Stump llvm::PointerType::get(PtrToInt8Ty, 0)); 790aeb0ffd5SMike Stump Srcv = Builder.CreateLoad(Srcv); 791aeb0ffd5SMike Stump 792aeb0ffd5SMike Stump llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index); 793aeb0ffd5SMike Stump Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty); 794aeb0ffd5SMike Stump 795aeb0ffd5SMike Stump llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag); 796aeb0ffd5SMike Stump llvm::Value *F = getBlockObjectAssign(); 797aeb0ffd5SMike Stump Builder.CreateCall3(F, Dstv, Srcv, N); 798aeb0ffd5SMike Stump } 799aeb0ffd5SMike Stump } 800aeb0ffd5SMike Stump 8010c743277SMike Stump CGF.FinishFunction(); 8020c743277SMike Stump 8030c743277SMike Stump return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 80497d01d50SMike Stump } 80597d01d50SMike Stump 806d6ef62f0SMike Stump llvm::Constant *BlockFunction:: 807aeb0ffd5SMike Stump GenerateDestroyHelperFunction(bool BlockHasCopyDispose, 808aeb0ffd5SMike Stump const llvm::StructType* T, 809aeb0ffd5SMike Stump std::vector<HelperInfo> &NoteForHelper) { 8100c743277SMike Stump QualType R = getContext().VoidTy; 8110c743277SMike Stump 8120c743277SMike Stump FunctionArgList Args; 8130c743277SMike Stump // FIXME: This leaks 8140c743277SMike Stump ImplicitParamDecl *Src = 8150c743277SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 8160c743277SMike Stump getContext().getPointerType(getContext().VoidTy)); 8170c743277SMike Stump 8180c743277SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 8190c743277SMike Stump 8200c743277SMike Stump const CGFunctionInfo &FI = 8210c743277SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 8220c743277SMike Stump 8230c743277SMike Stump std::string Name = std::string("__destroy_helper_block_"); 8240c743277SMike Stump CodeGenTypes &Types = CGM.getTypes(); 8250c743277SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 8260c743277SMike Stump 8270c743277SMike Stump llvm::Function *Fn = 8280c743277SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 8290c743277SMike Stump Name, 8300c743277SMike Stump &CGM.getModule()); 8310c743277SMike Stump 8320c743277SMike Stump IdentifierInfo *II 8330c743277SMike Stump = &CGM.getContext().Idents.get("__destroy_helper_block_"); 8340c743277SMike Stump 8350c743277SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 8360c743277SMike Stump getContext().getTranslationUnitDecl(), 8370c743277SMike Stump SourceLocation(), II, R, 8380c743277SMike Stump FunctionDecl::Static, false, 8390c743277SMike Stump true); 8400c743277SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 8416f7d9f87SMike Stump 8426f7d9f87SMike Stump llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); 8436f7d9f87SMike Stump llvm::Type *PtrPtrT; 8446f7d9f87SMike Stump PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 8456f7d9f87SMike Stump SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); 8466f7d9f87SMike Stump SrcObj = Builder.CreateLoad(SrcObj); 8476f7d9f87SMike Stump 8486f7d9f87SMike Stump for (unsigned i=0; i < NoteForHelper.size(); ++i) { 8496f7d9f87SMike Stump int flag = NoteForHelper[i].flag; 8506f7d9f87SMike Stump int index = NoteForHelper[i].index; 8516f7d9f87SMike Stump 8526f7d9f87SMike Stump if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) 8536f7d9f87SMike Stump || NoteForHelper[i].RequiresCopying) { 8546f7d9f87SMike Stump llvm::Value *Srcv = SrcObj; 8556f7d9f87SMike Stump Srcv = Builder.CreateStructGEP(Srcv, index); 8566f7d9f87SMike Stump Srcv = Builder.CreateBitCast(Srcv, 8576f7d9f87SMike Stump llvm::PointerType::get(PtrToInt8Ty, 0)); 8586f7d9f87SMike Stump Srcv = Builder.CreateLoad(Srcv); 8596f7d9f87SMike Stump 8606f7d9f87SMike Stump BuildBlockRelease(Srcv, flag); 8616f7d9f87SMike Stump } 8626f7d9f87SMike Stump } 8636f7d9f87SMike Stump 8640c743277SMike Stump CGF.FinishFunction(); 8650c743277SMike Stump 8660c743277SMike Stump return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 8670c743277SMike Stump } 8680c743277SMike Stump 869aeb0ffd5SMike Stump llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T, 870aeb0ffd5SMike Stump std::vector<HelperInfo> &NoteForHelper) { 871aeb0ffd5SMike Stump return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose, 872aeb0ffd5SMike Stump T, NoteForHelper); 8730c743277SMike Stump } 8740c743277SMike Stump 875aeb0ffd5SMike Stump llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T, 876aeb0ffd5SMike Stump std::vector<HelperInfo> &NoteForHelper) { 877aeb0ffd5SMike Stump return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose, 878aeb0ffd5SMike Stump T, NoteForHelper); 87997d01d50SMike Stump } 880626aecc4SMike Stump 881f89230d9SMike Stump llvm::Constant *BlockFunction:: 882f89230d9SMike Stump GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) { 883ee2a5ee5SMike Stump QualType R = getContext().VoidTy; 884ee2a5ee5SMike Stump 885ee2a5ee5SMike Stump FunctionArgList Args; 886ee2a5ee5SMike Stump // FIXME: This leaks 887f89230d9SMike Stump ImplicitParamDecl *Dst = 888f89230d9SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 889f89230d9SMike Stump getContext().getPointerType(getContext().VoidTy)); 890f89230d9SMike Stump Args.push_back(std::make_pair(Dst, Dst->getType())); 891f89230d9SMike Stump 892f89230d9SMike Stump // FIXME: This leaks 893ee2a5ee5SMike Stump ImplicitParamDecl *Src = 894ee2a5ee5SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 895ee2a5ee5SMike Stump getContext().getPointerType(getContext().VoidTy)); 896ee2a5ee5SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 897ee2a5ee5SMike Stump 898ee2a5ee5SMike Stump const CGFunctionInfo &FI = 899ee2a5ee5SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 900ee2a5ee5SMike Stump 901ee2a5ee5SMike Stump std::string Name = std::string("__Block_byref_id_object_copy_"); 902ee2a5ee5SMike Stump CodeGenTypes &Types = CGM.getTypes(); 903ee2a5ee5SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 904ee2a5ee5SMike Stump 905ee2a5ee5SMike Stump llvm::Function *Fn = 906ee2a5ee5SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 907ee2a5ee5SMike Stump Name, 908ee2a5ee5SMike Stump &CGM.getModule()); 909ee2a5ee5SMike Stump 910ee2a5ee5SMike Stump IdentifierInfo *II 911ee2a5ee5SMike Stump = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_"); 912ee2a5ee5SMike Stump 913ee2a5ee5SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 914ee2a5ee5SMike Stump getContext().getTranslationUnitDecl(), 915ee2a5ee5SMike Stump SourceLocation(), II, R, 916ee2a5ee5SMike Stump FunctionDecl::Static, false, 917ee2a5ee5SMike Stump true); 918ee2a5ee5SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 919f89230d9SMike Stump 920f89230d9SMike Stump // dst->x 921f89230d9SMike Stump llvm::Value *V = CGF.GetAddrOfLocalVar(Dst); 922f89230d9SMike Stump V = Builder.CreateBitCast(V, T); 923f89230d9SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 924f89230d9SMike Stump llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty); 925f89230d9SMike Stump 926f89230d9SMike Stump // src->x 927f89230d9SMike Stump V = CGF.GetAddrOfLocalVar(Src); 928f89230d9SMike Stump V = Builder.CreateLoad(V); 929f89230d9SMike Stump V = Builder.CreateBitCast(V, T); 930f89230d9SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 931f89230d9SMike Stump V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); 932f89230d9SMike Stump llvm::Value *SrcObj = Builder.CreateLoad(V); 933f89230d9SMike Stump 934f89230d9SMike Stump flag |= BLOCK_BYREF_CALLER; 935f89230d9SMike Stump 936f89230d9SMike Stump llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag); 937f89230d9SMike Stump llvm::Value *F = getBlockObjectAssign(); 938f89230d9SMike Stump Builder.CreateCall3(F, DstObj, SrcObj, N); 939f89230d9SMike Stump 940ee2a5ee5SMike Stump CGF.FinishFunction(); 941ee2a5ee5SMike Stump 942ee2a5ee5SMike Stump return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 943ee2a5ee5SMike Stump } 944ee2a5ee5SMike Stump 945fbe25dd1SMike Stump llvm::Constant * 946fbe25dd1SMike Stump BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T, 947fbe25dd1SMike Stump int flag) { 948ee2a5ee5SMike Stump QualType R = getContext().VoidTy; 949ee2a5ee5SMike Stump 950ee2a5ee5SMike Stump FunctionArgList Args; 951ee2a5ee5SMike Stump // FIXME: This leaks 952ee2a5ee5SMike Stump ImplicitParamDecl *Src = 953ee2a5ee5SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 954ee2a5ee5SMike Stump getContext().getPointerType(getContext().VoidTy)); 955ee2a5ee5SMike Stump 956ee2a5ee5SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 957ee2a5ee5SMike Stump 958ee2a5ee5SMike Stump const CGFunctionInfo &FI = 959ee2a5ee5SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 960ee2a5ee5SMike Stump 961ee2a5ee5SMike Stump std::string Name = std::string("__Block_byref_id_object_dispose_"); 962ee2a5ee5SMike Stump CodeGenTypes &Types = CGM.getTypes(); 963ee2a5ee5SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 964ee2a5ee5SMike Stump 965ee2a5ee5SMike Stump llvm::Function *Fn = 966ee2a5ee5SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 967ee2a5ee5SMike Stump Name, 968ee2a5ee5SMike Stump &CGM.getModule()); 969ee2a5ee5SMike Stump 970ee2a5ee5SMike Stump IdentifierInfo *II 971ee2a5ee5SMike Stump = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_"); 972ee2a5ee5SMike Stump 973ee2a5ee5SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 974ee2a5ee5SMike Stump getContext().getTranslationUnitDecl(), 975ee2a5ee5SMike Stump SourceLocation(), II, R, 976ee2a5ee5SMike Stump FunctionDecl::Static, false, 977ee2a5ee5SMike Stump true); 978ee2a5ee5SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 979fbe25dd1SMike Stump 980fbe25dd1SMike Stump llvm::Value *V = CGF.GetAddrOfLocalVar(Src); 981fbe25dd1SMike Stump V = Builder.CreateBitCast(V, T); 982fbe25dd1SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 983fbe25dd1SMike Stump V = Builder.CreateBitCast(V, PtrToInt8Ty); 984fbe25dd1SMike Stump 985fbe25dd1SMike Stump flag |= BLOCK_BYREF_CALLER; 986fbe25dd1SMike Stump BuildBlockRelease(V, flag); 987ee2a5ee5SMike Stump CGF.FinishFunction(); 988ee2a5ee5SMike Stump 989ee2a5ee5SMike Stump return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 990ee2a5ee5SMike Stump } 991ee2a5ee5SMike Stump 992f89230d9SMike Stump llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T, 993f89230d9SMike Stump int flag) { 994f89230d9SMike Stump return CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag); 995ee2a5ee5SMike Stump } 996ee2a5ee5SMike Stump 997fbe25dd1SMike Stump llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T, 998fbe25dd1SMike Stump int flag) { 999fbe25dd1SMike Stump return CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag); 1000ee2a5ee5SMike Stump } 1001ee2a5ee5SMike Stump 1002626aecc4SMike Stump llvm::Value *BlockFunction::getBlockObjectDispose() { 1003626aecc4SMike Stump if (CGM.BlockObjectDispose == 0) { 1004626aecc4SMike Stump const llvm::FunctionType *FTy; 1005626aecc4SMike Stump std::vector<const llvm::Type*> ArgTys; 1006626aecc4SMike Stump const llvm::Type *ResultType = llvm::Type::VoidTy; 1007626aecc4SMike Stump ArgTys.push_back(PtrToInt8Ty); 1008626aecc4SMike Stump ArgTys.push_back(llvm::Type::Int32Ty); 1009626aecc4SMike Stump FTy = llvm::FunctionType::get(ResultType, ArgTys, false); 1010626aecc4SMike Stump CGM.BlockObjectDispose 1011626aecc4SMike Stump = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose"); 1012626aecc4SMike Stump } 1013626aecc4SMike Stump return CGM.BlockObjectDispose; 1014626aecc4SMike Stump } 1015626aecc4SMike Stump 1016f89230d9SMike Stump llvm::Value *BlockFunction::getBlockObjectAssign() { 1017f89230d9SMike Stump if (CGM.BlockObjectAssign == 0) { 1018f89230d9SMike Stump const llvm::FunctionType *FTy; 1019f89230d9SMike Stump std::vector<const llvm::Type*> ArgTys; 1020f89230d9SMike Stump const llvm::Type *ResultType = llvm::Type::VoidTy; 1021f89230d9SMike Stump ArgTys.push_back(PtrToInt8Ty); 1022f89230d9SMike Stump ArgTys.push_back(PtrToInt8Ty); 1023f89230d9SMike Stump ArgTys.push_back(llvm::Type::Int32Ty); 1024f89230d9SMike Stump FTy = llvm::FunctionType::get(ResultType, ArgTys, false); 1025f89230d9SMike Stump CGM.BlockObjectAssign 1026f89230d9SMike Stump = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign"); 1027f89230d9SMike Stump } 1028f89230d9SMike Stump return CGM.BlockObjectAssign; 1029f89230d9SMike Stump } 1030f89230d9SMike Stump 1031fbe25dd1SMike Stump void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) { 1032626aecc4SMike Stump llvm::Value *F = getBlockObjectDispose(); 1033fbe25dd1SMike Stump llvm::Value *N; 1034626aecc4SMike Stump V = Builder.CreateBitCast(V, PtrToInt8Ty); 1035fbe25dd1SMike Stump N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag); 1036626aecc4SMike Stump Builder.CreateCall2(F, V, N); 1037626aecc4SMike Stump } 10384446dcf0SMike Stump 10394446dcf0SMike Stump ASTContext &BlockFunction::getContext() const { return CGM.getContext(); } 1040aeb0ffd5SMike Stump 1041aeb0ffd5SMike Stump BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf, 1042aeb0ffd5SMike Stump CGBuilderTy &B) 1043aeb0ffd5SMike Stump : CGM(cgm), CGF(cgf), Builder(B) { 1044aeb0ffd5SMike Stump PtrToInt8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 1045aeb0ffd5SMike Stump 1046aeb0ffd5SMike Stump BlockHasCopyDispose = false; 1047aeb0ffd5SMike Stump } 1048