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 #include <algorithm> 20db714924STorok Edwin #include <cstdio> 21db714924STorok Edwin 222437cbfaSAnders Carlsson using namespace clang; 232437cbfaSAnders Carlsson using namespace CodeGen; 242437cbfaSAnders Carlsson 25d6ef62f0SMike Stump llvm::Constant *CodeGenFunction:: 26d2142cffSMike Stump BuildDescriptorBlockDecl(bool BlockHasCopyDispose, uint64_t Size, 27d2142cffSMike Stump const llvm::StructType* Ty, 28aeb0ffd5SMike Stump std::vector<HelperInfo> *NoteForHelper) { 29c2c38331SMike Stump const llvm::Type *UnsignedLongTy 30c2c38331SMike Stump = CGM.getTypes().ConvertType(getContext().UnsignedLongTy); 3185284bacSMike Stump llvm::Constant *C; 3285284bacSMike Stump std::vector<llvm::Constant*> Elts; 3385284bacSMike Stump 3485284bacSMike Stump // reserved 35b7a2fe6fSOwen Anderson C = llvm::ConstantInt::get(UnsignedLongTy, 0); 3685284bacSMike Stump Elts.push_back(C); 3785284bacSMike Stump 3885284bacSMike Stump // Size 392ac40a9fSMike Stump // FIXME: What is the right way to say this doesn't fit? We should give 402ac40a9fSMike Stump // a user diagnostic in that case. Better fix would be to change the 412ac40a9fSMike Stump // API to size_t. 42b7a2fe6fSOwen Anderson C = llvm::ConstantInt::get(UnsignedLongTy, Size); 4385284bacSMike Stump Elts.push_back(C); 4485284bacSMike Stump 4585284bacSMike Stump if (BlockHasCopyDispose) { 4685284bacSMike Stump // copy_func_helper_decl 4767645930SMike Stump Elts.push_back(BuildCopyHelper(Ty, NoteForHelper)); 4885284bacSMike Stump 4985284bacSMike Stump // destroy_func_decl 5067645930SMike Stump Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper)); 5185284bacSMike Stump } 5285284bacSMike Stump 5341eaf0acSNick Lewycky C = llvm::ConstantStruct::get(VMContext, Elts, false); 5485284bacSMike Stump 55c10c8d3eSOwen Anderson C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, 5685284bacSMike Stump llvm::GlobalValue::InternalLinkage, 57c10c8d3eSOwen Anderson C, "__block_descriptor_tmp"); 5885284bacSMike Stump return C; 5985284bacSMike Stump } 6085284bacSMike Stump 6195435674SMike Stump llvm::Constant *BlockModule::getNSConcreteGlobalBlock() { 6246813bbfSChris Lattner if (NSConcreteGlobalBlock == 0) 6346813bbfSChris Lattner NSConcreteGlobalBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty, 6446813bbfSChris Lattner "_NSConcreteGlobalBlock"); 65971f9b66SMike Stump return NSConcreteGlobalBlock; 66971f9b66SMike Stump } 67971f9b66SMike Stump 6895435674SMike Stump llvm::Constant *BlockModule::getNSConcreteStackBlock() { 6946813bbfSChris Lattner if (NSConcreteStackBlock == 0) 7046813bbfSChris Lattner NSConcreteStackBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty, 7146813bbfSChris Lattner "_NSConcreteStackBlock"); 727ab278dbSMike Stump return NSConcreteStackBlock; 737ab278dbSMike Stump } 747ab278dbSMike Stump 75ed5e69feSAnders Carlsson static void CollectBlockDeclRefInfo(const Stmt *S, 76ed5e69feSAnders Carlsson CodeGenFunction::BlockInfo &Info) { 77ed5e69feSAnders Carlsson for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); 78ed5e69feSAnders Carlsson I != E; ++I) 79dd942713SDaniel Dunbar if (*I) 80ed5e69feSAnders Carlsson CollectBlockDeclRefInfo(*I, Info); 81ed5e69feSAnders Carlsson 82ed5e69feSAnders Carlsson if (const BlockDeclRefExpr *DE = dyn_cast<BlockDeclRefExpr>(S)) { 83ed5e69feSAnders Carlsson // FIXME: Handle enums. 84ed5e69feSAnders Carlsson if (isa<FunctionDecl>(DE->getDecl())) 85ed5e69feSAnders Carlsson return; 86ed5e69feSAnders Carlsson 87ed5e69feSAnders Carlsson if (DE->isByRef()) 88ed5e69feSAnders Carlsson Info.ByRefDeclRefs.push_back(DE); 89ed5e69feSAnders Carlsson else 90ed5e69feSAnders Carlsson Info.ByCopyDeclRefs.push_back(DE); 91ed5e69feSAnders Carlsson } 92ed5e69feSAnders Carlsson } 93ed5e69feSAnders Carlsson 942d33d630SMike Stump /// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be 952d33d630SMike Stump /// declared as a global variable instead of on the stack. 9646813bbfSChris Lattner static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info) { 97ed5e69feSAnders Carlsson return Info.ByRefDeclRefs.empty() && Info.ByCopyDeclRefs.empty(); 98ed5e69feSAnders Carlsson } 99ed5e69feSAnders Carlsson 1002d33d630SMike Stump // FIXME: Push most into CGM, passing down a few bits, like current function 1012d33d630SMike Stump // name. 102b750d928SMike Stump llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) { 103ed5e69feSAnders Carlsson 104ed5e69feSAnders Carlsson std::string Name = CurFn->getName(); 105ed5e69feSAnders Carlsson CodeGenFunction::BlockInfo Info(0, Name.c_str()); 106ed5e69feSAnders Carlsson CollectBlockDeclRefInfo(BE->getBody(), Info); 107ed5e69feSAnders Carlsson 108ed5e69feSAnders Carlsson // Check if the block can be global. 1092d33d630SMike Stump // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like 1102d33d630SMike Stump // to just have one code path. We should move this function into CGM and pass 1112d33d630SMike Stump // CGF, then we can just check to see if CGF is 0. 11297d01d50SMike Stump if (0 && CanBlockBeGlobal(Info)) 113ed5e69feSAnders Carlsson return CGM.GetAddrOfGlobalBlock(BE, Name.c_str()); 11485284bacSMike Stump 1154446dcf0SMike Stump std::vector<llvm::Constant*> Elts(5); 11685284bacSMike Stump llvm::Constant *C; 117b750d928SMike Stump llvm::Value *V; 11885284bacSMike Stump 11985284bacSMike Stump { 12085284bacSMike Stump // C = BuildBlockStructInitlist(); 12185284bacSMike Stump unsigned int flags = BLOCK_HAS_DESCRIPTOR; 12285284bacSMike Stump 1234446dcf0SMike Stump // We run this first so that we set BlockHasCopyDispose from the entire 1244446dcf0SMike Stump // block literal. 1254446dcf0SMike Stump // __invoke 1264446dcf0SMike Stump uint64_t subBlockSize, subBlockAlign; 1274446dcf0SMike Stump llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; 128d2142cffSMike Stump bool subBlockHasCopyDispose = false; 1294446dcf0SMike Stump llvm::Function *Fn 130*d2a3e617SMike Stump = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl, 131*d2a3e617SMike Stump LocalDeclMap, 1325469f29aSMike Stump subBlockSize, 1334446dcf0SMike Stump subBlockAlign, 1344446dcf0SMike Stump subBlockDeclRefDecls, 135d2142cffSMike Stump subBlockHasCopyDispose); 136d2142cffSMike Stump BlockHasCopyDispose |= subBlockHasCopyDispose; 1374446dcf0SMike Stump Elts[3] = Fn; 1384446dcf0SMike Stump 13918bb9284SMike Stump // FIXME: Don't use BlockHasCopyDispose, it is set more often then 14018bb9284SMike Stump // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); } 141d2142cffSMike Stump if (subBlockHasCopyDispose) 14285284bacSMike Stump flags |= BLOCK_HAS_COPY_DISPOSE; 14385284bacSMike Stump 144499ae7ecSMike Stump // __isa 1457ab278dbSMike Stump C = CGM.getNSConcreteStackBlock(); 146ade90fd1SOwen Anderson C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty); 1474446dcf0SMike Stump Elts[0] = C; 14885284bacSMike Stump 14985284bacSMike Stump // __flags 15085284bacSMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 15185284bacSMike Stump CGM.getTypes().ConvertType(CGM.getContext().IntTy)); 152b7a2fe6fSOwen Anderson C = llvm::ConstantInt::get(IntTy, flags); 1534446dcf0SMike Stump Elts[1] = C; 15485284bacSMike Stump 15585284bacSMike Stump // __reserved 156b7a2fe6fSOwen Anderson C = llvm::ConstantInt::get(IntTy, 0); 1574446dcf0SMike Stump Elts[2] = C; 15885284bacSMike Stump 159b750d928SMike Stump if (subBlockDeclRefDecls.size() == 0) { 160d6ef62f0SMike Stump // __descriptor 161d2142cffSMike Stump Elts[4] = BuildDescriptorBlockDecl(subBlockHasCopyDispose, subBlockSize, 0, 0); 162d6ef62f0SMike Stump 1633a139f37SMike Stump // Optimize to being a global block. 1643a139f37SMike Stump Elts[0] = CGM.getNSConcreteGlobalBlock(); 165b7a2fe6fSOwen Anderson Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL); 1663a139f37SMike Stump 16741eaf0acSNick Lewycky C = llvm::ConstantStruct::get(VMContext, Elts, false); 16885284bacSMike Stump 16985284bacSMike Stump char Name[32]; 1709319db8aSMike Stump sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount()); 171c10c8d3eSOwen Anderson C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, 17285284bacSMike Stump llvm::GlobalValue::InternalLinkage, 173c10c8d3eSOwen Anderson C, Name); 1745d2534adSMike Stump QualType BPT = BE->getType(); 175ade90fd1SOwen Anderson C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT)); 17685284bacSMike Stump return C; 17785284bacSMike Stump } 17885284bacSMike Stump 179b750d928SMike Stump std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size()); 180aeb0ffd5SMike Stump for (int i=0; i<4; ++i) 181b750d928SMike Stump Types[i] = Elts[i]->getType(); 182aeb0ffd5SMike Stump Types[4] = PtrToInt8Ty; 18385284bacSMike Stump 1841db7d04bSMike Stump for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) { 1851db7d04bSMike Stump const Expr *E = subBlockDeclRefDecls[i]; 1861db7d04bSMike Stump const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); 1871db7d04bSMike Stump QualType Ty = E->getType(); 18897d01d50SMike Stump if (BDRE && BDRE->isByRef()) { 18971d1d92dSAnders Carlsson Types[i+5] = llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0); 19097d01d50SMike Stump } else 1911db7d04bSMike Stump Types[i+5] = ConvertType(Ty); 1921db7d04bSMike Stump } 193b750d928SMike Stump 194758428f4SOwen Anderson llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true); 195b750d928SMike Stump 196b750d928SMike Stump llvm::AllocaInst *A = CreateTempAlloca(Ty); 197b750d928SMike Stump A->setAlignment(subBlockAlign); 198b750d928SMike Stump V = A; 199b750d928SMike Stump 200aeb0ffd5SMike Stump std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size()); 201aeb0ffd5SMike Stump int helpersize = 0; 202aeb0ffd5SMike Stump 203aeb0ffd5SMike Stump for (unsigned i=0; i<4; ++i) 204b750d928SMike Stump Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp")); 205b750d928SMike Stump 206b750d928SMike Stump for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) 207b750d928SMike Stump { 2081db7d04bSMike Stump // FIXME: Push const down. 2091db7d04bSMike Stump Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]); 2101db7d04bSMike Stump DeclRefExpr *DR; 2111db7d04bSMike Stump ValueDecl *VD; 212b750d928SMike Stump 2131db7d04bSMike Stump DR = dyn_cast<DeclRefExpr>(E); 2141db7d04bSMike Stump // Skip padding. 2151db7d04bSMike Stump if (DR) continue; 2161db7d04bSMike Stump 2171db7d04bSMike Stump BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); 2181db7d04bSMike Stump VD = BDRE->getDecl(); 2191db7d04bSMike Stump 22097d01d50SMike Stump llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp"); 221aeb0ffd5SMike Stump NoteForHelper[helpersize].index = i+5; 2222114d7c2SMike Stump NoteForHelper[helpersize].RequiresCopying 2232114d7c2SMike Stump = BlockRequiresCopying(VD->getType()); 224aeb0ffd5SMike Stump NoteForHelper[helpersize].flag 2252114d7c2SMike Stump = (VD->getType()->isBlockPointerType() 2262114d7c2SMike Stump ? BLOCK_FIELD_IS_BLOCK 2272114d7c2SMike Stump : BLOCK_FIELD_IS_OBJECT); 228aeb0ffd5SMike Stump 2291db7d04bSMike Stump if (LocalDeclMap[VD]) { 23097d01d50SMike Stump if (BDRE->isByRef()) { 231aeb0ffd5SMike Stump NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF | 23290d8daf5SMike Stump // FIXME: Someone double check this. 23390d8daf5SMike Stump (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0); 23497d01d50SMike Stump llvm::Value *Loc = LocalDeclMap[VD]; 23597d01d50SMike Stump Loc = Builder.CreateStructGEP(Loc, 1, "forwarding"); 23697d01d50SMike Stump Loc = Builder.CreateLoad(Loc, false); 23797d01d50SMike Stump Builder.CreateStore(Loc, Addr); 238aeb0ffd5SMike Stump ++helpersize; 23997d01d50SMike Stump continue; 24097d01d50SMike Stump } else 2411db7d04bSMike Stump E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD), 2421db7d04bSMike Stump VD->getType(), SourceLocation(), 2431db7d04bSMike Stump false, false); 2441db7d04bSMike Stump } 24597d01d50SMike Stump if (BDRE->isByRef()) { 246efd7caa8SMike Stump NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF | 247efd7caa8SMike Stump // FIXME: Someone double check this. 248efd7caa8SMike Stump (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0); 2491db7d04bSMike Stump E = new (getContext()) 2501db7d04bSMike Stump UnaryOperator(E, UnaryOperator::AddrOf, 2511db7d04bSMike Stump getContext().getPointerType(E->getType()), 2521db7d04bSMike Stump SourceLocation()); 25397d01d50SMike Stump } 254aeb0ffd5SMike Stump ++helpersize; 2551db7d04bSMike Stump 2561db7d04bSMike Stump RValue r = EmitAnyExpr(E, Addr, false); 25797d01d50SMike Stump if (r.isScalar()) { 25897d01d50SMike Stump llvm::Value *Loc = r.getScalarVal(); 25997d01d50SMike Stump const llvm::Type *Ty = Types[i+5]; 26097d01d50SMike Stump if (BDRE->isByRef()) { 2612d33d630SMike Stump // E is now the address of the value field, instead, we want the 2622d33d630SMike Stump // address of the actual ByRef struct. We optimize this slightly 2632d33d630SMike Stump // compared to gcc by not grabbing the forwarding slot as this must 2642d33d630SMike Stump // be done during Block_copy for us, and we can postpone the work 2652d33d630SMike Stump // until then. 2662d33d630SMike Stump uint64_t offset = BlockDecls[BDRE->getDecl()]; 2672d33d630SMike Stump 2682d33d630SMike Stump llvm::Value *BlockLiteral = LoadBlockStruct(); 2692d33d630SMike Stump 2702d33d630SMike Stump Loc = Builder.CreateGEP(BlockLiteral, 27141a75027SOwen Anderson llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), 2722d33d630SMike Stump offset), 2732d33d630SMike Stump "block.literal"); 2749793f0e4SOwen Anderson Ty = llvm::PointerType::get(Ty, 0); 27597d01d50SMike Stump Loc = Builder.CreateBitCast(Loc, Ty); 2762d33d630SMike Stump Loc = Builder.CreateLoad(Loc, false); 2772d33d630SMike Stump // Loc = Builder.CreateBitCast(Loc, Ty); 27897d01d50SMike Stump } 27997d01d50SMike Stump Builder.CreateStore(Loc, Addr); 28097d01d50SMike Stump } else if (r.isComplex()) 281b750d928SMike Stump // FIXME: implement 282b750d928SMike Stump ErrorUnsupported(BE, "complex in block literal"); 283b750d928SMike Stump else if (r.isAggregate()) 284b750d928SMike Stump ; // Already created into the destination 285b750d928SMike Stump else 286b750d928SMike Stump assert (0 && "bad block variable"); 287b750d928SMike Stump // FIXME: Ensure that the offset created by the backend for 288b750d928SMike Stump // the struct matches the previously computed offset in BlockDecls. 289b750d928SMike Stump } 290aeb0ffd5SMike Stump NoteForHelper.resize(helpersize); 291d6ef62f0SMike Stump 292d6ef62f0SMike Stump // __descriptor 293d2142cffSMike Stump llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockHasCopyDispose, 294d2142cffSMike Stump subBlockSize, Ty, 295aeb0ffd5SMike Stump &NoteForHelper); 296aeb0ffd5SMike Stump Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty); 297aeb0ffd5SMike Stump Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp")); 298b750d928SMike Stump } 299b750d928SMike Stump 300b750d928SMike Stump QualType BPT = BE->getType(); 301b750d928SMike Stump return Builder.CreateBitCast(V, ConvertType(BPT)); 302b750d928SMike Stump } 30385284bacSMike Stump 30485284bacSMike Stump 30595435674SMike Stump const llvm::Type *BlockModule::getBlockDescriptorType() { 306650c932dSMike Stump if (BlockDescriptorType) 307650c932dSMike Stump return BlockDescriptorType; 3082437cbfaSAnders Carlsson 3092437cbfaSAnders Carlsson const llvm::Type *UnsignedLongTy = 310650c932dSMike Stump getTypes().ConvertType(getContext().UnsignedLongTy); 3112437cbfaSAnders Carlsson 3122437cbfaSAnders Carlsson // struct __block_descriptor { 3132437cbfaSAnders Carlsson // unsigned long reserved; 3142437cbfaSAnders Carlsson // unsigned long block_size; 3152437cbfaSAnders Carlsson // }; 316758428f4SOwen Anderson BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(), 317758428f4SOwen Anderson UnsignedLongTy, 3182437cbfaSAnders Carlsson UnsignedLongTy, 3192437cbfaSAnders Carlsson NULL); 3202437cbfaSAnders Carlsson 321650c932dSMike Stump getModule().addTypeName("struct.__block_descriptor", 322650c932dSMike Stump BlockDescriptorType); 3232437cbfaSAnders Carlsson 324650c932dSMike Stump return BlockDescriptorType; 3252437cbfaSAnders Carlsson } 3262437cbfaSAnders Carlsson 32795435674SMike Stump const llvm::Type *BlockModule::getGenericBlockLiteralType() { 328005c9a62SMike Stump if (GenericBlockLiteralType) 329005c9a62SMike Stump return GenericBlockLiteralType; 3302437cbfaSAnders Carlsson 3312437cbfaSAnders Carlsson const llvm::Type *BlockDescPtrTy = 3329793f0e4SOwen Anderson llvm::PointerType::getUnqual(getBlockDescriptorType()); 3332437cbfaSAnders Carlsson 33492bbd6d4SMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 33592bbd6d4SMike Stump getTypes().ConvertType(getContext().IntTy)); 33692bbd6d4SMike Stump 3372437cbfaSAnders Carlsson // struct __block_literal_generic { 3385d2534adSMike Stump // void *__isa; 3395d2534adSMike Stump // int __flags; 3405d2534adSMike Stump // int __reserved; 3415d2534adSMike Stump // void (*__invoke)(void *); 3425d2534adSMike Stump // struct __block_descriptor *__descriptor; 3432437cbfaSAnders Carlsson // }; 344758428f4SOwen Anderson GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(), 345758428f4SOwen Anderson PtrToInt8Ty, 34692bbd6d4SMike Stump IntTy, 34792bbd6d4SMike Stump IntTy, 348626aecc4SMike Stump PtrToInt8Ty, 3492437cbfaSAnders Carlsson BlockDescPtrTy, 3502437cbfaSAnders Carlsson NULL); 3512437cbfaSAnders Carlsson 352005c9a62SMike Stump getModule().addTypeName("struct.__block_literal_generic", 353005c9a62SMike Stump GenericBlockLiteralType); 3542437cbfaSAnders Carlsson 355005c9a62SMike Stump return GenericBlockLiteralType; 3562437cbfaSAnders Carlsson } 3572437cbfaSAnders Carlsson 35895435674SMike Stump const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() { 3595d2534adSMike Stump if (GenericExtendedBlockLiteralType) 3605d2534adSMike Stump return GenericExtendedBlockLiteralType; 3615d2534adSMike Stump 3625d2534adSMike Stump const llvm::Type *BlockDescPtrTy = 3639793f0e4SOwen Anderson llvm::PointerType::getUnqual(getBlockDescriptorType()); 3645d2534adSMike Stump 3655d2534adSMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 3665d2534adSMike Stump getTypes().ConvertType(getContext().IntTy)); 3675d2534adSMike Stump 3685d2534adSMike Stump // struct __block_literal_generic { 3695d2534adSMike Stump // void *__isa; 3705d2534adSMike Stump // int __flags; 3715d2534adSMike Stump // int __reserved; 3725d2534adSMike Stump // void (*__invoke)(void *); 3735d2534adSMike Stump // struct __block_descriptor *__descriptor; 3745d2534adSMike Stump // void *__copy_func_helper_decl; 3755d2534adSMike Stump // void *__destroy_func_decl; 3765d2534adSMike Stump // }; 377758428f4SOwen Anderson GenericExtendedBlockLiteralType = llvm::StructType::get(IntTy->getContext(), 378758428f4SOwen Anderson PtrToInt8Ty, 3795d2534adSMike Stump IntTy, 3805d2534adSMike Stump IntTy, 381626aecc4SMike Stump PtrToInt8Ty, 3825d2534adSMike Stump BlockDescPtrTy, 383626aecc4SMike Stump PtrToInt8Ty, 384626aecc4SMike Stump PtrToInt8Ty, 3855d2534adSMike Stump NULL); 3865d2534adSMike Stump 3875d2534adSMike Stump getModule().addTypeName("struct.__block_literal_extended_generic", 3885d2534adSMike Stump GenericExtendedBlockLiteralType); 3895d2534adSMike Stump 3905d2534adSMike Stump return GenericExtendedBlockLiteralType; 3915d2534adSMike Stump } 3925d2534adSMike Stump 3932114d7c2SMike Stump bool BlockFunction::BlockRequiresCopying(QualType Ty) { 3942114d7c2SMike Stump return CGM.BlockRequiresCopying(Ty); 3952114d7c2SMike Stump } 3962114d7c2SMike Stump 3976a60fa24SAnders Carlsson RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) { 3982437cbfaSAnders Carlsson const BlockPointerType *BPT = 399c23c7e6aSTed Kremenek E->getCallee()->getType()->getAs<BlockPointerType>(); 4002437cbfaSAnders Carlsson 4012437cbfaSAnders Carlsson llvm::Value *Callee = EmitScalarExpr(E->getCallee()); 4022437cbfaSAnders Carlsson 4032437cbfaSAnders Carlsson // Get a pointer to the generic block literal. 4042437cbfaSAnders Carlsson const llvm::Type *BlockLiteralTy = 4059793f0e4SOwen Anderson llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType()); 4062437cbfaSAnders Carlsson 4072437cbfaSAnders Carlsson // Bitcast the callee to a block literal. 4082437cbfaSAnders Carlsson llvm::Value *BlockLiteral = 4092437cbfaSAnders Carlsson Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal"); 4102437cbfaSAnders Carlsson 4112437cbfaSAnders Carlsson // Get the function pointer from the literal. 4122437cbfaSAnders Carlsson llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp"); 4132437cbfaSAnders Carlsson 4142437cbfaSAnders Carlsson BlockLiteral = 4152437cbfaSAnders Carlsson Builder.CreateBitCast(BlockLiteral, 41641a75027SOwen Anderson llvm::PointerType::getUnqual( 41741a75027SOwen Anderson llvm::Type::getInt8Ty(VMContext)), 4182437cbfaSAnders Carlsson "tmp"); 4192437cbfaSAnders Carlsson 4202437cbfaSAnders Carlsson // Add the block literal. 4212437cbfaSAnders Carlsson QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy); 4222437cbfaSAnders Carlsson CallArgList Args; 4232437cbfaSAnders Carlsson Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy)); 4242437cbfaSAnders Carlsson 425479e6fc7SAnders Carlsson QualType FnType = BPT->getPointeeType(); 426479e6fc7SAnders Carlsson 4272437cbfaSAnders Carlsson // And the rest of the arguments. 4289dd450bbSJohn McCall EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(), 429479e6fc7SAnders Carlsson E->arg_begin(), E->arg_end()); 4302437cbfaSAnders Carlsson 4315f50c651SAnders Carlsson // Load the function. 4325f50c651SAnders Carlsson llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp"); 4335f50c651SAnders Carlsson 4349dd450bbSJohn McCall QualType ResultType = FnType->getAs<FunctionType>()->getResultType(); 435a60cbcdfSAnders Carlsson 436a60cbcdfSAnders Carlsson const CGFunctionInfo &FnInfo = 437a60cbcdfSAnders Carlsson CGM.getTypes().getFunctionInfo(ResultType, Args); 4385f50c651SAnders Carlsson 4395f50c651SAnders Carlsson // Cast the function pointer to the right type. 4405f50c651SAnders Carlsson const llvm::Type *BlockFTy = 441a60cbcdfSAnders Carlsson CGM.getTypes().GetFunctionType(FnInfo, false); 4425f50c651SAnders Carlsson 4439793f0e4SOwen Anderson const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy); 4445f50c651SAnders Carlsson Func = Builder.CreateBitCast(Func, BlockFTyPtr); 4455f50c651SAnders Carlsson 4462437cbfaSAnders Carlsson // And call the block. 447350da607SAnders Carlsson return EmitCall(FnInfo, Func, Args); 4482437cbfaSAnders Carlsson } 4496a60fa24SAnders Carlsson 45097d01d50SMike Stump llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) { 4510168f4b2SAnders Carlsson const ValueDecl *VD = E->getDecl(); 45297d01d50SMike Stump 4530168f4b2SAnders Carlsson uint64_t &offset = BlockDecls[VD]; 4540168f4b2SAnders Carlsson 45597d01d50SMike Stump 45697d01d50SMike Stump // See if we have already allocated an offset for this variable. 45797d01d50SMike Stump if (offset == 0) { 4584446dcf0SMike Stump // Don't run the expensive check, unless we have to. 4594446dcf0SMike Stump if (!BlockHasCopyDispose && BlockRequiresCopying(E->getType())) 4604446dcf0SMike Stump BlockHasCopyDispose = true; 46197d01d50SMike Stump // if not, allocate one now. 46297d01d50SMike Stump offset = getBlockOffset(E); 46397d01d50SMike Stump } 46497d01d50SMike Stump 46597d01d50SMike Stump llvm::Value *BlockLiteral = LoadBlockStruct(); 46697d01d50SMike Stump llvm::Value *V = Builder.CreateGEP(BlockLiteral, 46741a75027SOwen Anderson llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), 46897d01d50SMike Stump offset), 4692d33d630SMike Stump "block.literal"); 47097d01d50SMike Stump if (E->isByRef()) { 47197d01d50SMike Stump const llvm::Type *PtrStructTy 4720168f4b2SAnders Carlsson = llvm::PointerType::get(BuildByRefType(VD), 0); 473efd7caa8SMike Stump // The block literal will need a copy/destroy helper. 474efd7caa8SMike Stump BlockHasCopyDispose = true; 4750168f4b2SAnders Carlsson 4760168f4b2SAnders Carlsson const llvm::Type *Ty = PtrStructTy; 4779793f0e4SOwen Anderson Ty = llvm::PointerType::get(Ty, 0); 47897d01d50SMike Stump V = Builder.CreateBitCast(V, Ty); 47997d01d50SMike Stump V = Builder.CreateLoad(V, false); 48097d01d50SMike Stump V = Builder.CreateStructGEP(V, 1, "forwarding"); 48197d01d50SMike Stump V = Builder.CreateLoad(V, false); 48297d01d50SMike Stump V = Builder.CreateBitCast(V, PtrStructTy); 4830168f4b2SAnders Carlsson V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD), 4840168f4b2SAnders Carlsson VD->getNameAsString()); 48597d01d50SMike Stump } else { 4860168f4b2SAnders Carlsson const llvm::Type *Ty = CGM.getTypes().ConvertType(VD->getType()); 4870168f4b2SAnders Carlsson 4889793f0e4SOwen Anderson Ty = llvm::PointerType::get(Ty, 0); 48997d01d50SMike Stump V = Builder.CreateBitCast(V, Ty); 49097d01d50SMike Stump } 49197d01d50SMike Stump return V; 49297d01d50SMike Stump } 49397d01d50SMike Stump 494692c6e37SMike Stump void CodeGenFunction::BlockForwardSelf() { 495692c6e37SMike Stump const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); 496692c6e37SMike Stump ImplicitParamDecl *SelfDecl = OMD->getSelfDecl(); 497692c6e37SMike Stump llvm::Value *&DMEntry = LocalDeclMap[SelfDecl]; 498692c6e37SMike Stump if (DMEntry) 499692c6e37SMike Stump return; 500692c6e37SMike Stump // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care 501692c6e37SMike Stump BlockDeclRefExpr *BDRE = new (getContext()) 502692c6e37SMike Stump BlockDeclRefExpr(SelfDecl, 503692c6e37SMike Stump SelfDecl->getType(), SourceLocation(), false); 504692c6e37SMike Stump DMEntry = GetAddrOfBlockDecl(BDRE); 505692c6e37SMike Stump } 506692c6e37SMike Stump 5072d5a2878SMike Stump llvm::Constant * 5086c39666aSMike Stump BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) { 5096a60fa24SAnders Carlsson // Generate the block descriptor. 5106a60fa24SAnders Carlsson const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy); 51192bbd6d4SMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 51292bbd6d4SMike Stump getTypes().ConvertType(getContext().IntTy)); 5136a60fa24SAnders Carlsson 5146a60fa24SAnders Carlsson llvm::Constant *DescriptorFields[2]; 5156a60fa24SAnders Carlsson 5166a60fa24SAnders Carlsson // Reserved 5170b75f23bSOwen Anderson DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy); 5186a60fa24SAnders Carlsson 5196a60fa24SAnders Carlsson // Block literal size. For global blocks we just use the size of the generic 5206a60fa24SAnders Carlsson // block literal struct. 5216a60fa24SAnders Carlsson uint64_t BlockLiteralSize = 522005c9a62SMike Stump TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8; 523170229f6SOwen Anderson DescriptorFields[1] = 524b7a2fe6fSOwen Anderson llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize); 5256a60fa24SAnders Carlsson 5266a60fa24SAnders Carlsson llvm::Constant *DescriptorStruct = 52741eaf0acSNick Lewycky llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 2, false); 5286a60fa24SAnders Carlsson 5296a60fa24SAnders Carlsson llvm::GlobalVariable *Descriptor = 530c10c8d3eSOwen Anderson new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true, 5316a60fa24SAnders Carlsson llvm::GlobalVariable::InternalLinkage, 532c10c8d3eSOwen Anderson DescriptorStruct, "__block_descriptor_global"); 5336a60fa24SAnders Carlsson 5346a60fa24SAnders Carlsson // Generate the constants for the block literal. 5356a60fa24SAnders Carlsson llvm::Constant *LiteralFields[5]; 5366a60fa24SAnders Carlsson 5372d5a2878SMike Stump CodeGenFunction::BlockInfo Info(0, n); 538b750d928SMike Stump uint64_t subBlockSize, subBlockAlign; 5391db7d04bSMike Stump llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; 540a61bb4d1SDaniel Dunbar bool subBlockHasCopyDispose = false; 5415469f29aSMike Stump llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap; 542cb2fbcb0SMike Stump llvm::Function *Fn 543692c6e37SMike Stump = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap, 5445469f29aSMike Stump subBlockSize, 545b750d928SMike Stump subBlockAlign, 5464446dcf0SMike Stump subBlockDeclRefDecls, 5474446dcf0SMike Stump subBlockHasCopyDispose); 548cb2fbcb0SMike Stump assert(subBlockSize == BlockLiteralSize 549cb2fbcb0SMike Stump && "no imports allowed for global block"); 5506a60fa24SAnders Carlsson 5516a60fa24SAnders Carlsson // isa 552971f9b66SMike Stump LiteralFields[0] = getNSConcreteGlobalBlock(); 5536a60fa24SAnders Carlsson 5546a60fa24SAnders Carlsson // Flags 5555878c797SAnders Carlsson LiteralFields[1] = 556b7a2fe6fSOwen Anderson llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR); 5576a60fa24SAnders Carlsson 5586a60fa24SAnders Carlsson // Reserved 5590b75f23bSOwen Anderson LiteralFields[2] = llvm::Constant::getNullValue(IntTy); 5606a60fa24SAnders Carlsson 5616a60fa24SAnders Carlsson // Function 5626a60fa24SAnders Carlsson LiteralFields[3] = Fn; 5636a60fa24SAnders Carlsson 5646a60fa24SAnders Carlsson // Descriptor 5656a60fa24SAnders Carlsson LiteralFields[4] = Descriptor; 5666a60fa24SAnders Carlsson 5676a60fa24SAnders Carlsson llvm::Constant *BlockLiteralStruct = 56841eaf0acSNick Lewycky llvm::ConstantStruct::get(VMContext, &LiteralFields[0], 5, false); 5696a60fa24SAnders Carlsson 5706a60fa24SAnders Carlsson llvm::GlobalVariable *BlockLiteral = 571c10c8d3eSOwen Anderson new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true, 5726a60fa24SAnders Carlsson llvm::GlobalVariable::InternalLinkage, 573c10c8d3eSOwen Anderson BlockLiteralStruct, "__block_literal_global"); 5746a60fa24SAnders Carlsson 5756a60fa24SAnders Carlsson return BlockLiteral; 5766a60fa24SAnders Carlsson } 5776a60fa24SAnders Carlsson 578cb2fbcb0SMike Stump llvm::Value *CodeGenFunction::LoadBlockStruct() { 579cb2fbcb0SMike Stump return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self"); 580cb2fbcb0SMike Stump } 581cb2fbcb0SMike Stump 5824446dcf0SMike Stump llvm::Function * 5834446dcf0SMike Stump CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr, 584cb2fbcb0SMike Stump const BlockInfo& Info, 585692c6e37SMike Stump const Decl *OuterFuncDecl, 5865469f29aSMike Stump llvm::DenseMap<const Decl*, llvm::Value*> ldm, 587b750d928SMike Stump uint64_t &Size, 588b750d928SMike Stump uint64_t &Align, 5894446dcf0SMike Stump llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls, 5904446dcf0SMike Stump bool &subBlockHasCopyDispose) { 5919074ed8cSDevang Patel 5929074ed8cSDevang Patel // Check if we should generate debug info for this block. 5939074ed8cSDevang Patel if (CGM.getDebugInfo()) 5949074ed8cSDevang Patel DebugInfo = CGM.getDebugInfo(); 5959074ed8cSDevang Patel 5965469f29aSMike Stump // Arrange for local static and local extern declarations to appear 5975469f29aSMike Stump // to be local to this function as well, as they are directly referenced 5985469f29aSMike Stump // in a block. 5995469f29aSMike Stump for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin(); 6005469f29aSMike Stump i != ldm.end(); 6015469f29aSMike Stump ++i) { 6025469f29aSMike Stump const VarDecl *VD = dyn_cast<VarDecl>(i->first); 6035469f29aSMike Stump 6040ca16601SDaniel Dunbar if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage()) 6055469f29aSMike Stump LocalDeclMap[VD] = i->second; 6065469f29aSMike Stump } 6075469f29aSMike Stump 60809a9b6e3SEli Friedman // FIXME: We need to rearrange the code for copy/dispose so we have this 60909a9b6e3SEli Friedman // sooner, so we can calculate offsets correctly. 61009a9b6e3SEli Friedman if (!BlockHasCopyDispose) 61109a9b6e3SEli Friedman BlockOffset = CGM.getTargetData() 61209a9b6e3SEli Friedman .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8; 61309a9b6e3SEli Friedman else 61409a9b6e3SEli Friedman BlockOffset = CGM.getTargetData() 61509a9b6e3SEli Friedman .getTypeStoreSizeInBits(CGM.getGenericExtendedBlockLiteralType()) / 8; 61609a9b6e3SEli Friedman BlockAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 61709a9b6e3SEli Friedman 618aae4349dSFariborz Jahanian const FunctionType *BlockFunctionType = BExpr->getFunctionType(); 619aae4349dSFariborz Jahanian QualType ResultType; 620aae4349dSFariborz Jahanian bool IsVariadic; 621a4f6b6b1SFariborz Jahanian if (const FunctionProtoType *FTy = 622a4f6b6b1SFariborz Jahanian dyn_cast<FunctionProtoType>(BlockFunctionType)) { 623aae4349dSFariborz Jahanian ResultType = FTy->getResultType(); 624aae4349dSFariborz Jahanian IsVariadic = FTy->isVariadic(); 625658fe02dSMike Stump } else { 626aae4349dSFariborz Jahanian // K&R style block. 627aae4349dSFariborz Jahanian ResultType = BlockFunctionType->getResultType(); 628aae4349dSFariborz Jahanian IsVariadic = false; 629aae4349dSFariborz Jahanian } 6306a60fa24SAnders Carlsson 6316a60fa24SAnders Carlsson FunctionArgList Args; 6326a60fa24SAnders Carlsson 6333385fe12SChris Lattner const BlockDecl *BD = BExpr->getBlockDecl(); 6346a60fa24SAnders Carlsson 6356a60fa24SAnders Carlsson // FIXME: This leaks 6366a60fa24SAnders Carlsson ImplicitParamDecl *SelfDecl = 6376a60fa24SAnders Carlsson ImplicitParamDecl::Create(getContext(), 0, 6386a60fa24SAnders Carlsson SourceLocation(), 0, 6396a60fa24SAnders Carlsson getContext().getPointerType(getContext().VoidTy)); 6406a60fa24SAnders Carlsson 6416a60fa24SAnders Carlsson Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType())); 642cb2fbcb0SMike Stump BlockStructDecl = SelfDecl; 6436a60fa24SAnders Carlsson 644c4b30e59SSteve Naroff for (BlockDecl::param_const_iterator i = BD->param_begin(), 6456a60fa24SAnders Carlsson e = BD->param_end(); i != e; ++i) 646eecd39f2SMike Stump Args.push_back(std::make_pair(*i, (*i)->getType())); 6476a60fa24SAnders Carlsson 6486a60fa24SAnders Carlsson const CGFunctionInfo &FI = 649aae4349dSFariborz Jahanian CGM.getTypes().getFunctionInfo(ResultType, Args); 6506a60fa24SAnders Carlsson 6512d5a2878SMike Stump std::string Name = std::string("__") + Info.Name + "_block_invoke_"; 6526a60fa24SAnders Carlsson CodeGenTypes &Types = CGM.getTypes(); 653aae4349dSFariborz Jahanian const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic); 6546a60fa24SAnders Carlsson 6556a60fa24SAnders Carlsson llvm::Function *Fn = 6566a60fa24SAnders Carlsson llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 6576a60fa24SAnders Carlsson Name, 6586a60fa24SAnders Carlsson &CGM.getModule()); 6596a60fa24SAnders Carlsson 660c3e7cff6SDaniel Dunbar CGM.SetInternalFunctionAttributes(BD, Fn, FI); 661c3e7cff6SDaniel Dunbar 662324f80b1SChris Lattner StartFunction(BD, ResultType, Fn, Args, 6633385fe12SChris Lattner BExpr->getBody()->getLocEnd()); 664324f80b1SChris Lattner CurFuncDecl = OuterFuncDecl; 66528ec0cf8SChris Lattner CurCodeDecl = BD; 6663385fe12SChris Lattner EmitStmt(BExpr->getBody()); 6673385fe12SChris Lattner FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc()); 6686a60fa24SAnders Carlsson 669b750d928SMike Stump // The runtime needs a minimum alignment of a void *. 670b750d928SMike Stump uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 671b750d928SMike Stump BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign); 672b750d928SMike Stump 673cb2fbcb0SMike Stump Size = BlockOffset; 674b750d928SMike Stump Align = BlockAlign; 675b750d928SMike Stump subBlockDeclRefDecls = BlockDeclRefDecls; 6764446dcf0SMike Stump subBlockHasCopyDispose |= BlockHasCopyDispose; 6776a60fa24SAnders Carlsson return Fn; 6786a60fa24SAnders Carlsson } 6791db7d04bSMike Stump 680aeb0ffd5SMike Stump uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) { 6811db7d04bSMike Stump const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl()); 6821db7d04bSMike Stump 6831db7d04bSMike Stump uint64_t Size = getContext().getTypeSize(D->getType()) / 8; 6841db7d04bSMike Stump uint64_t Align = getContext().getDeclAlignInBytes(D); 6851db7d04bSMike Stump 6861db7d04bSMike Stump if (BDRE->isByRef()) { 6871db7d04bSMike Stump Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8; 6881db7d04bSMike Stump Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 6891db7d04bSMike Stump } 6901db7d04bSMike Stump 6911db7d04bSMike Stump assert ((Align > 0) && "alignment must be 1 byte or more"); 6921db7d04bSMike Stump 6931db7d04bSMike Stump uint64_t OldOffset = BlockOffset; 6941db7d04bSMike Stump 6951db7d04bSMike Stump // Ensure proper alignment, even if it means we have to have a gap 6961db7d04bSMike Stump BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align); 6971db7d04bSMike Stump BlockAlign = std::max(Align, BlockAlign); 6981db7d04bSMike Stump 6991db7d04bSMike Stump uint64_t Pad = BlockOffset - OldOffset; 7001db7d04bSMike Stump if (Pad) { 70141a75027SOwen Anderson llvm::ArrayType::get(llvm::Type::getInt8Ty(VMContext), Pad); 7021db7d04bSMike Stump QualType PadTy = getContext().getConstantArrayType(getContext().CharTy, 7031db7d04bSMike Stump llvm::APInt(32, Pad), 7041db7d04bSMike Stump ArrayType::Normal, 0); 7051db7d04bSMike Stump ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(), 7066032ef1aSArgyrios Kyrtzidis 0, QualType(PadTy), 0, VarDecl::None); 7071db7d04bSMike Stump Expr *E; 7081db7d04bSMike Stump E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(), 7091db7d04bSMike Stump SourceLocation(), false, false); 7101db7d04bSMike Stump BlockDeclRefDecls.push_back(E); 7111db7d04bSMike Stump } 7121db7d04bSMike Stump BlockDeclRefDecls.push_back(BDRE); 7131db7d04bSMike Stump 7141db7d04bSMike Stump BlockOffset += Size; 7151db7d04bSMike Stump return BlockOffset-Size; 7161db7d04bSMike Stump } 71797d01d50SMike Stump 718aeb0ffd5SMike Stump llvm::Constant *BlockFunction:: 719aeb0ffd5SMike Stump GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T, 72067645930SMike Stump std::vector<HelperInfo> *NoteForHelperp) { 7210c743277SMike Stump QualType R = getContext().VoidTy; 7220c743277SMike Stump 7230c743277SMike Stump FunctionArgList Args; 7240c743277SMike Stump // FIXME: This leaks 725aeb0ffd5SMike Stump ImplicitParamDecl *Dst = 726aeb0ffd5SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 727aeb0ffd5SMike Stump getContext().getPointerType(getContext().VoidTy)); 728aeb0ffd5SMike Stump Args.push_back(std::make_pair(Dst, Dst->getType())); 7290c743277SMike Stump ImplicitParamDecl *Src = 7300c743277SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 7310c743277SMike Stump getContext().getPointerType(getContext().VoidTy)); 7320c743277SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 7330c743277SMike Stump 7340c743277SMike Stump const CGFunctionInfo &FI = 7350c743277SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 7360c743277SMike Stump 737cbc2bcafSMike Stump // FIXME: We'd like to put these into a mergable by content, with 738cbc2bcafSMike Stump // internal linkage. 7390c743277SMike Stump std::string Name = std::string("__copy_helper_block_"); 7400c743277SMike Stump CodeGenTypes &Types = CGM.getTypes(); 7410c743277SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 7420c743277SMike Stump 7430c743277SMike Stump llvm::Function *Fn = 7440c743277SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 7450c743277SMike Stump Name, 7460c743277SMike Stump &CGM.getModule()); 7470c743277SMike Stump 7480c743277SMike Stump IdentifierInfo *II 7490c743277SMike Stump = &CGM.getContext().Idents.get("__copy_helper_block_"); 7500c743277SMike Stump 7510c743277SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 7520c743277SMike Stump getContext().getTranslationUnitDecl(), 75360ed5604SArgyrios Kyrtzidis SourceLocation(), II, R, 0, 7540c743277SMike Stump FunctionDecl::Static, false, 7550c743277SMike Stump true); 7560c743277SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 757aeb0ffd5SMike Stump 758aeb0ffd5SMike Stump llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); 759aeb0ffd5SMike Stump llvm::Type *PtrPtrT; 76067645930SMike Stump 76167645930SMike Stump if (NoteForHelperp) { 76267645930SMike Stump std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp; 76367645930SMike Stump 7649793f0e4SOwen Anderson PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 765aeb0ffd5SMike Stump SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); 766aeb0ffd5SMike Stump SrcObj = Builder.CreateLoad(SrcObj); 767aeb0ffd5SMike Stump 768aeb0ffd5SMike Stump llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst); 7693b65ac26SMike Stump llvm::Type *PtrPtrT; 7709793f0e4SOwen Anderson PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 7713b65ac26SMike Stump DstObj = Builder.CreateBitCast(DstObj, PtrPtrT); 7723b65ac26SMike Stump DstObj = Builder.CreateLoad(DstObj); 773aeb0ffd5SMike Stump 774aeb0ffd5SMike Stump for (unsigned i=0; i < NoteForHelper.size(); ++i) { 775aeb0ffd5SMike Stump int flag = NoteForHelper[i].flag; 776aeb0ffd5SMike Stump int index = NoteForHelper[i].index; 777aeb0ffd5SMike Stump 778aeb0ffd5SMike Stump if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) 779aeb0ffd5SMike Stump || NoteForHelper[i].RequiresCopying) { 780aeb0ffd5SMike Stump llvm::Value *Srcv = SrcObj; 781aeb0ffd5SMike Stump Srcv = Builder.CreateStructGEP(Srcv, index); 782aeb0ffd5SMike Stump Srcv = Builder.CreateBitCast(Srcv, 7839793f0e4SOwen Anderson llvm::PointerType::get(PtrToInt8Ty, 0)); 784aeb0ffd5SMike Stump Srcv = Builder.CreateLoad(Srcv); 785aeb0ffd5SMike Stump 786aeb0ffd5SMike Stump llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index); 787aeb0ffd5SMike Stump Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty); 788aeb0ffd5SMike Stump 78941a75027SOwen Anderson llvm::Value *N = llvm::ConstantInt::get( 79041a75027SOwen Anderson llvm::Type::getInt32Ty(T->getContext()), flag); 791aeb0ffd5SMike Stump llvm::Value *F = getBlockObjectAssign(); 792aeb0ffd5SMike Stump Builder.CreateCall3(F, Dstv, Srcv, N); 793aeb0ffd5SMike Stump } 794aeb0ffd5SMike Stump } 79567645930SMike Stump } 796aeb0ffd5SMike Stump 7970c743277SMike Stump CGF.FinishFunction(); 7980c743277SMike Stump 799ade90fd1SOwen Anderson return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 80097d01d50SMike Stump } 80197d01d50SMike Stump 802d6ef62f0SMike Stump llvm::Constant *BlockFunction:: 803aeb0ffd5SMike Stump GenerateDestroyHelperFunction(bool BlockHasCopyDispose, 804aeb0ffd5SMike Stump const llvm::StructType* T, 80567645930SMike Stump std::vector<HelperInfo> *NoteForHelperp) { 8060c743277SMike Stump QualType R = getContext().VoidTy; 8070c743277SMike Stump 8080c743277SMike Stump FunctionArgList Args; 8090c743277SMike Stump // FIXME: This leaks 8100c743277SMike Stump ImplicitParamDecl *Src = 8110c743277SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 8120c743277SMike Stump getContext().getPointerType(getContext().VoidTy)); 8130c743277SMike Stump 8140c743277SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 8150c743277SMike Stump 8160c743277SMike Stump const CGFunctionInfo &FI = 8170c743277SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 8180c743277SMike Stump 819cbc2bcafSMike Stump // FIXME: We'd like to put these into a mergable by content, with 820cbc2bcafSMike Stump // internal linkage. 8210c743277SMike Stump std::string Name = std::string("__destroy_helper_block_"); 8220c743277SMike Stump CodeGenTypes &Types = CGM.getTypes(); 8230c743277SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 8240c743277SMike Stump 8250c743277SMike Stump llvm::Function *Fn = 8260c743277SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 8270c743277SMike Stump Name, 8280c743277SMike Stump &CGM.getModule()); 8290c743277SMike Stump 8300c743277SMike Stump IdentifierInfo *II 8310c743277SMike Stump = &CGM.getContext().Idents.get("__destroy_helper_block_"); 8320c743277SMike Stump 8330c743277SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 8340c743277SMike Stump getContext().getTranslationUnitDecl(), 83560ed5604SArgyrios Kyrtzidis SourceLocation(), II, R, 0, 8360c743277SMike Stump FunctionDecl::Static, false, 8370c743277SMike Stump true); 8380c743277SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 8396f7d9f87SMike Stump 84067645930SMike Stump if (NoteForHelperp) { 84167645930SMike Stump std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp; 84267645930SMike Stump 8436f7d9f87SMike Stump llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); 8446f7d9f87SMike Stump llvm::Type *PtrPtrT; 8459793f0e4SOwen Anderson PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 8466f7d9f87SMike Stump SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); 8476f7d9f87SMike Stump SrcObj = Builder.CreateLoad(SrcObj); 8486f7d9f87SMike Stump 8496f7d9f87SMike Stump for (unsigned i=0; i < NoteForHelper.size(); ++i) { 8506f7d9f87SMike Stump int flag = NoteForHelper[i].flag; 8516f7d9f87SMike Stump int index = NoteForHelper[i].index; 8526f7d9f87SMike Stump 8536f7d9f87SMike Stump if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) 8546f7d9f87SMike Stump || NoteForHelper[i].RequiresCopying) { 8556f7d9f87SMike Stump llvm::Value *Srcv = SrcObj; 8566f7d9f87SMike Stump Srcv = Builder.CreateStructGEP(Srcv, index); 8576f7d9f87SMike Stump Srcv = Builder.CreateBitCast(Srcv, 8589793f0e4SOwen Anderson llvm::PointerType::get(PtrToInt8Ty, 0)); 8596f7d9f87SMike Stump Srcv = Builder.CreateLoad(Srcv); 8606f7d9f87SMike Stump 8616f7d9f87SMike Stump BuildBlockRelease(Srcv, flag); 8626f7d9f87SMike Stump } 8636f7d9f87SMike Stump } 86467645930SMike Stump } 8656f7d9f87SMike Stump 8660c743277SMike Stump CGF.FinishFunction(); 8670c743277SMike Stump 868ade90fd1SOwen Anderson return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 8690c743277SMike Stump } 8700c743277SMike Stump 871aeb0ffd5SMike Stump llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T, 87267645930SMike Stump std::vector<HelperInfo> *NoteForHelper) { 873aeb0ffd5SMike Stump return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose, 874aeb0ffd5SMike Stump T, NoteForHelper); 8750c743277SMike Stump } 8760c743277SMike Stump 877aeb0ffd5SMike Stump llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T, 87867645930SMike Stump std::vector<HelperInfo> *NoteForHelperp) { 879aeb0ffd5SMike Stump return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose, 88067645930SMike Stump T, NoteForHelperp); 88197d01d50SMike Stump } 882626aecc4SMike Stump 883f89230d9SMike Stump llvm::Constant *BlockFunction:: 884f89230d9SMike Stump GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) { 885ee2a5ee5SMike Stump QualType R = getContext().VoidTy; 886ee2a5ee5SMike Stump 887ee2a5ee5SMike Stump FunctionArgList Args; 888ee2a5ee5SMike Stump // FIXME: This leaks 889f89230d9SMike Stump ImplicitParamDecl *Dst = 890f89230d9SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 891f89230d9SMike Stump getContext().getPointerType(getContext().VoidTy)); 892f89230d9SMike Stump Args.push_back(std::make_pair(Dst, Dst->getType())); 893f89230d9SMike Stump 894f89230d9SMike Stump // FIXME: This leaks 895ee2a5ee5SMike Stump ImplicitParamDecl *Src = 896ee2a5ee5SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 897ee2a5ee5SMike Stump getContext().getPointerType(getContext().VoidTy)); 898ee2a5ee5SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 899ee2a5ee5SMike Stump 900ee2a5ee5SMike Stump const CGFunctionInfo &FI = 901ee2a5ee5SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 902ee2a5ee5SMike Stump 903ee2a5ee5SMike Stump std::string Name = std::string("__Block_byref_id_object_copy_"); 904ee2a5ee5SMike Stump CodeGenTypes &Types = CGM.getTypes(); 905ee2a5ee5SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 906ee2a5ee5SMike Stump 907cbc2bcafSMike Stump // FIXME: We'd like to put these into a mergable by content, with 908cbc2bcafSMike Stump // internal linkage. 909ee2a5ee5SMike Stump llvm::Function *Fn = 910ee2a5ee5SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 911ee2a5ee5SMike Stump Name, 912ee2a5ee5SMike Stump &CGM.getModule()); 913ee2a5ee5SMike Stump 914ee2a5ee5SMike Stump IdentifierInfo *II 915ee2a5ee5SMike Stump = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_"); 916ee2a5ee5SMike Stump 917ee2a5ee5SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 918ee2a5ee5SMike Stump getContext().getTranslationUnitDecl(), 91960ed5604SArgyrios Kyrtzidis SourceLocation(), II, R, 0, 920ee2a5ee5SMike Stump FunctionDecl::Static, false, 921ee2a5ee5SMike Stump true); 922ee2a5ee5SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 923f89230d9SMike Stump 924f89230d9SMike Stump // dst->x 925f89230d9SMike Stump llvm::Value *V = CGF.GetAddrOfLocalVar(Dst); 9269793f0e4SOwen Anderson V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0)); 9273b65ac26SMike Stump V = Builder.CreateLoad(V); 928f89230d9SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 929f89230d9SMike Stump llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty); 930f89230d9SMike Stump 931f89230d9SMike Stump // src->x 932f89230d9SMike Stump V = CGF.GetAddrOfLocalVar(Src); 933f89230d9SMike Stump V = Builder.CreateLoad(V); 934f89230d9SMike Stump V = Builder.CreateBitCast(V, T); 935f89230d9SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 9369793f0e4SOwen Anderson V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); 937f89230d9SMike Stump llvm::Value *SrcObj = Builder.CreateLoad(V); 938f89230d9SMike Stump 939f89230d9SMike Stump flag |= BLOCK_BYREF_CALLER; 940f89230d9SMike Stump 94141a75027SOwen Anderson llvm::Value *N = llvm::ConstantInt::get( 94241a75027SOwen Anderson llvm::Type::getInt32Ty(T->getContext()), flag); 943f89230d9SMike Stump llvm::Value *F = getBlockObjectAssign(); 944f89230d9SMike Stump Builder.CreateCall3(F, DstObj, SrcObj, N); 945f89230d9SMike Stump 946ee2a5ee5SMike Stump CGF.FinishFunction(); 947ee2a5ee5SMike Stump 948ade90fd1SOwen Anderson return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 949ee2a5ee5SMike Stump } 950ee2a5ee5SMike Stump 951fbe25dd1SMike Stump llvm::Constant * 952fbe25dd1SMike Stump BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T, 953fbe25dd1SMike Stump int flag) { 954ee2a5ee5SMike Stump QualType R = getContext().VoidTy; 955ee2a5ee5SMike Stump 956ee2a5ee5SMike Stump FunctionArgList Args; 957ee2a5ee5SMike Stump // FIXME: This leaks 958ee2a5ee5SMike Stump ImplicitParamDecl *Src = 959ee2a5ee5SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 960ee2a5ee5SMike Stump getContext().getPointerType(getContext().VoidTy)); 961ee2a5ee5SMike Stump 962ee2a5ee5SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 963ee2a5ee5SMike Stump 964ee2a5ee5SMike Stump const CGFunctionInfo &FI = 965ee2a5ee5SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 966ee2a5ee5SMike Stump 967ee2a5ee5SMike Stump std::string Name = std::string("__Block_byref_id_object_dispose_"); 968ee2a5ee5SMike Stump CodeGenTypes &Types = CGM.getTypes(); 969ee2a5ee5SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 970ee2a5ee5SMike Stump 971cbc2bcafSMike Stump // FIXME: We'd like to put these into a mergable by content, with 972cbc2bcafSMike Stump // internal linkage. 973ee2a5ee5SMike Stump llvm::Function *Fn = 974ee2a5ee5SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 975ee2a5ee5SMike Stump Name, 976ee2a5ee5SMike Stump &CGM.getModule()); 977ee2a5ee5SMike Stump 978ee2a5ee5SMike Stump IdentifierInfo *II 979ee2a5ee5SMike Stump = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_"); 980ee2a5ee5SMike Stump 981ee2a5ee5SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 982ee2a5ee5SMike Stump getContext().getTranslationUnitDecl(), 98360ed5604SArgyrios Kyrtzidis SourceLocation(), II, R, 0, 984ee2a5ee5SMike Stump FunctionDecl::Static, false, 985ee2a5ee5SMike Stump true); 986ee2a5ee5SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 987fbe25dd1SMike Stump 988fbe25dd1SMike Stump llvm::Value *V = CGF.GetAddrOfLocalVar(Src); 9899793f0e4SOwen Anderson V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0)); 9903b65ac26SMike Stump V = Builder.CreateLoad(V); 991fbe25dd1SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 9929793f0e4SOwen Anderson V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); 9933b65ac26SMike Stump V = Builder.CreateLoad(V); 994fbe25dd1SMike Stump 995fbe25dd1SMike Stump flag |= BLOCK_BYREF_CALLER; 996fbe25dd1SMike Stump BuildBlockRelease(V, flag); 997ee2a5ee5SMike Stump CGF.FinishFunction(); 998ee2a5ee5SMike Stump 999ade90fd1SOwen Anderson return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 1000ee2a5ee5SMike Stump } 1001ee2a5ee5SMike Stump 1002f89230d9SMike Stump llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T, 1003cbc2bcafSMike Stump int flag, unsigned Align) { 1004cbc2bcafSMike Stump // All alignments below that of pointer alignment collpase down to just 1005cbc2bcafSMike Stump // pointer alignment, as we always have at least that much alignment to begin 1006cbc2bcafSMike Stump // with. 1007cbc2bcafSMike Stump Align /= unsigned(CGF.Target.getPointerAlign(0)/8); 1008cbc2bcafSMike Stump // As an optimization, we only generate a single function of each kind we 1009cbc2bcafSMike Stump // might need. We need a different one for each alignment and for each 1010cbc2bcafSMike Stump // setting of flags. We mix Align and flag to get the kind. 1011cbc2bcafSMike Stump uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag; 1012cbc2bcafSMike Stump llvm::Constant *& Entry = CGM.AssignCache[kind]; 1013cbc2bcafSMike Stump if (Entry) 1014cbc2bcafSMike Stump return Entry; 1015cbc2bcafSMike Stump return Entry=CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag); 1016ee2a5ee5SMike Stump } 1017ee2a5ee5SMike Stump 1018fbe25dd1SMike Stump llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T, 1019cbc2bcafSMike Stump int flag, 1020cbc2bcafSMike Stump unsigned Align) { 1021cbc2bcafSMike Stump // All alignments below that of pointer alignment collpase down to just 1022cbc2bcafSMike Stump // pointer alignment, as we always have at least that much alignment to begin 1023cbc2bcafSMike Stump // with. 1024cbc2bcafSMike Stump Align /= unsigned(CGF.Target.getPointerAlign(0)/8); 1025cbc2bcafSMike Stump // As an optimization, we only generate a single function of each kind we 1026cbc2bcafSMike Stump // might need. We need a different one for each alignment and for each 1027cbc2bcafSMike Stump // setting of flags. We mix Align and flag to get the kind. 1028cbc2bcafSMike Stump uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag; 1029cbc2bcafSMike Stump llvm::Constant *& Entry = CGM.DestroyCache[kind]; 1030cbc2bcafSMike Stump if (Entry) 1031cbc2bcafSMike Stump return Entry; 1032cbc2bcafSMike Stump return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag); 1033ee2a5ee5SMike Stump } 1034ee2a5ee5SMike Stump 1035626aecc4SMike Stump llvm::Value *BlockFunction::getBlockObjectDispose() { 1036626aecc4SMike Stump if (CGM.BlockObjectDispose == 0) { 1037626aecc4SMike Stump const llvm::FunctionType *FTy; 1038626aecc4SMike Stump std::vector<const llvm::Type*> ArgTys; 103941a75027SOwen Anderson const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext); 1040626aecc4SMike Stump ArgTys.push_back(PtrToInt8Ty); 104141a75027SOwen Anderson ArgTys.push_back(llvm::Type::getInt32Ty(VMContext)); 10429793f0e4SOwen Anderson FTy = llvm::FunctionType::get(ResultType, ArgTys, false); 1043626aecc4SMike Stump CGM.BlockObjectDispose 1044626aecc4SMike Stump = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose"); 1045626aecc4SMike Stump } 1046626aecc4SMike Stump return CGM.BlockObjectDispose; 1047626aecc4SMike Stump } 1048626aecc4SMike Stump 1049f89230d9SMike Stump llvm::Value *BlockFunction::getBlockObjectAssign() { 1050f89230d9SMike Stump if (CGM.BlockObjectAssign == 0) { 1051f89230d9SMike Stump const llvm::FunctionType *FTy; 1052f89230d9SMike Stump std::vector<const llvm::Type*> ArgTys; 105341a75027SOwen Anderson const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext); 1054f89230d9SMike Stump ArgTys.push_back(PtrToInt8Ty); 1055f89230d9SMike Stump ArgTys.push_back(PtrToInt8Ty); 105641a75027SOwen Anderson ArgTys.push_back(llvm::Type::getInt32Ty(VMContext)); 10579793f0e4SOwen Anderson FTy = llvm::FunctionType::get(ResultType, ArgTys, false); 1058f89230d9SMike Stump CGM.BlockObjectAssign 1059f89230d9SMike Stump = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign"); 1060f89230d9SMike Stump } 1061f89230d9SMike Stump return CGM.BlockObjectAssign; 1062f89230d9SMike Stump } 1063f89230d9SMike Stump 1064fbe25dd1SMike Stump void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) { 1065626aecc4SMike Stump llvm::Value *F = getBlockObjectDispose(); 1066fbe25dd1SMike Stump llvm::Value *N; 1067626aecc4SMike Stump V = Builder.CreateBitCast(V, PtrToInt8Ty); 106841a75027SOwen Anderson N = llvm::ConstantInt::get(llvm::Type::getInt32Ty(V->getContext()), flag); 1069626aecc4SMike Stump Builder.CreateCall2(F, V, N); 1070626aecc4SMike Stump } 10714446dcf0SMike Stump 10724446dcf0SMike Stump ASTContext &BlockFunction::getContext() const { return CGM.getContext(); } 1073aeb0ffd5SMike Stump 1074aeb0ffd5SMike Stump BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf, 1075aeb0ffd5SMike Stump CGBuilderTy &B) 1076170229f6SOwen Anderson : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) { 107741a75027SOwen Anderson PtrToInt8Ty = llvm::PointerType::getUnqual( 107841a75027SOwen Anderson llvm::Type::getInt8Ty(VMContext)); 1079aeb0ffd5SMike Stump 1080aeb0ffd5SMike Stump BlockHasCopyDispose = false; 1081aeb0ffd5SMike Stump } 1082