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 130692c6e37SMike Stump = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl, LocalDeclMap, 1315469f29aSMike Stump subBlockSize, 1324446dcf0SMike Stump subBlockAlign, 1334446dcf0SMike Stump subBlockDeclRefDecls, 134d2142cffSMike Stump subBlockHasCopyDispose); 135d2142cffSMike Stump BlockHasCopyDispose |= subBlockHasCopyDispose; 1364446dcf0SMike Stump Elts[3] = Fn; 1374446dcf0SMike Stump 13818bb9284SMike Stump // FIXME: Don't use BlockHasCopyDispose, it is set more often then 13918bb9284SMike Stump // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); } 140d2142cffSMike Stump if (subBlockHasCopyDispose) 14185284bacSMike Stump flags |= BLOCK_HAS_COPY_DISPOSE; 14285284bacSMike Stump 143499ae7ecSMike Stump // __isa 1447ab278dbSMike Stump C = CGM.getNSConcreteStackBlock(); 145ade90fd1SOwen Anderson C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty); 1464446dcf0SMike Stump Elts[0] = C; 14785284bacSMike Stump 14885284bacSMike Stump // __flags 14985284bacSMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 15085284bacSMike Stump CGM.getTypes().ConvertType(CGM.getContext().IntTy)); 151b7a2fe6fSOwen Anderson C = llvm::ConstantInt::get(IntTy, flags); 1524446dcf0SMike Stump Elts[1] = C; 15385284bacSMike Stump 15485284bacSMike Stump // __reserved 155b7a2fe6fSOwen Anderson C = llvm::ConstantInt::get(IntTy, 0); 1564446dcf0SMike Stump Elts[2] = C; 15785284bacSMike Stump 158b750d928SMike Stump if (subBlockDeclRefDecls.size() == 0) { 159d6ef62f0SMike Stump // __descriptor 160d2142cffSMike Stump Elts[4] = BuildDescriptorBlockDecl(subBlockHasCopyDispose, subBlockSize, 0, 0); 161d6ef62f0SMike Stump 1623a139f37SMike Stump // Optimize to being a global block. 1633a139f37SMike Stump Elts[0] = CGM.getNSConcreteGlobalBlock(); 164b7a2fe6fSOwen Anderson Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL); 1653a139f37SMike Stump 16641eaf0acSNick Lewycky C = llvm::ConstantStruct::get(VMContext, Elts, false); 16785284bacSMike Stump 16885284bacSMike Stump char Name[32]; 1699319db8aSMike Stump sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount()); 170c10c8d3eSOwen Anderson C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, 17185284bacSMike Stump llvm::GlobalValue::InternalLinkage, 172c10c8d3eSOwen Anderson C, Name); 1735d2534adSMike Stump QualType BPT = BE->getType(); 174ade90fd1SOwen Anderson C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT)); 17585284bacSMike Stump return C; 17685284bacSMike Stump } 17785284bacSMike Stump 178b750d928SMike Stump std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size()); 179aeb0ffd5SMike Stump for (int i=0; i<4; ++i) 180b750d928SMike Stump Types[i] = Elts[i]->getType(); 181aeb0ffd5SMike Stump Types[4] = PtrToInt8Ty; 18285284bacSMike Stump 1831db7d04bSMike Stump for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) { 1841db7d04bSMike Stump const Expr *E = subBlockDeclRefDecls[i]; 1851db7d04bSMike Stump const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); 1861db7d04bSMike Stump QualType Ty = E->getType(); 18797d01d50SMike Stump if (BDRE && BDRE->isByRef()) { 18871d1d92dSAnders Carlsson Types[i+5] = llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0); 18997d01d50SMike Stump } else 1901db7d04bSMike Stump Types[i+5] = ConvertType(Ty); 1911db7d04bSMike Stump } 192b750d928SMike Stump 193758428f4SOwen Anderson llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true); 194b750d928SMike Stump 195b750d928SMike Stump llvm::AllocaInst *A = CreateTempAlloca(Ty); 196b750d928SMike Stump A->setAlignment(subBlockAlign); 197b750d928SMike Stump V = A; 198b750d928SMike Stump 199aeb0ffd5SMike Stump std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size()); 200aeb0ffd5SMike Stump int helpersize = 0; 201aeb0ffd5SMike Stump 202aeb0ffd5SMike Stump for (unsigned i=0; i<4; ++i) 203b750d928SMike Stump Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp")); 204b750d928SMike Stump 205b750d928SMike Stump for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) 206b750d928SMike Stump { 2071db7d04bSMike Stump // FIXME: Push const down. 2081db7d04bSMike Stump Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]); 2091db7d04bSMike Stump DeclRefExpr *DR; 2101db7d04bSMike Stump ValueDecl *VD; 211b750d928SMike Stump 2121db7d04bSMike Stump DR = dyn_cast<DeclRefExpr>(E); 2131db7d04bSMike Stump // Skip padding. 2141db7d04bSMike Stump if (DR) continue; 2151db7d04bSMike Stump 2161db7d04bSMike Stump BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); 2171db7d04bSMike Stump VD = BDRE->getDecl(); 2181db7d04bSMike Stump 21997d01d50SMike Stump llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp"); 220aeb0ffd5SMike Stump NoteForHelper[helpersize].index = i+5; 221*2114d7c2SMike Stump NoteForHelper[helpersize].RequiresCopying 222*2114d7c2SMike Stump = BlockRequiresCopying(VD->getType()); 223aeb0ffd5SMike Stump NoteForHelper[helpersize].flag 224*2114d7c2SMike Stump = (VD->getType()->isBlockPointerType() 225*2114d7c2SMike Stump ? BLOCK_FIELD_IS_BLOCK 226*2114d7c2SMike Stump : BLOCK_FIELD_IS_OBJECT); 227aeb0ffd5SMike Stump 2281db7d04bSMike Stump if (LocalDeclMap[VD]) { 22997d01d50SMike Stump if (BDRE->isByRef()) { 230aeb0ffd5SMike Stump NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF | 23190d8daf5SMike Stump // FIXME: Someone double check this. 23290d8daf5SMike Stump (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0); 23397d01d50SMike Stump llvm::Value *Loc = LocalDeclMap[VD]; 23497d01d50SMike Stump Loc = Builder.CreateStructGEP(Loc, 1, "forwarding"); 23597d01d50SMike Stump Loc = Builder.CreateLoad(Loc, false); 23697d01d50SMike Stump Builder.CreateStore(Loc, Addr); 237aeb0ffd5SMike Stump ++helpersize; 23897d01d50SMike Stump continue; 23997d01d50SMike Stump } else 2401db7d04bSMike Stump E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD), 2411db7d04bSMike Stump VD->getType(), SourceLocation(), 2421db7d04bSMike Stump false, false); 2431db7d04bSMike Stump } 24497d01d50SMike Stump if (BDRE->isByRef()) { 245efd7caa8SMike Stump NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF | 246efd7caa8SMike Stump // FIXME: Someone double check this. 247efd7caa8SMike Stump (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0); 2481db7d04bSMike Stump E = new (getContext()) 2491db7d04bSMike Stump UnaryOperator(E, UnaryOperator::AddrOf, 2501db7d04bSMike Stump getContext().getPointerType(E->getType()), 2511db7d04bSMike Stump SourceLocation()); 25297d01d50SMike Stump } 253aeb0ffd5SMike Stump ++helpersize; 2541db7d04bSMike Stump 2551db7d04bSMike Stump RValue r = EmitAnyExpr(E, Addr, false); 25697d01d50SMike Stump if (r.isScalar()) { 25797d01d50SMike Stump llvm::Value *Loc = r.getScalarVal(); 25897d01d50SMike Stump const llvm::Type *Ty = Types[i+5]; 25997d01d50SMike Stump if (BDRE->isByRef()) { 2602d33d630SMike Stump // E is now the address of the value field, instead, we want the 2612d33d630SMike Stump // address of the actual ByRef struct. We optimize this slightly 2622d33d630SMike Stump // compared to gcc by not grabbing the forwarding slot as this must 2632d33d630SMike Stump // be done during Block_copy for us, and we can postpone the work 2642d33d630SMike Stump // until then. 2652d33d630SMike Stump uint64_t offset = BlockDecls[BDRE->getDecl()]; 2662d33d630SMike Stump 2672d33d630SMike Stump llvm::Value *BlockLiteral = LoadBlockStruct(); 2682d33d630SMike Stump 2692d33d630SMike Stump Loc = Builder.CreateGEP(BlockLiteral, 27041a75027SOwen Anderson llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), 2712d33d630SMike Stump offset), 2722d33d630SMike Stump "block.literal"); 2739793f0e4SOwen Anderson Ty = llvm::PointerType::get(Ty, 0); 27497d01d50SMike Stump Loc = Builder.CreateBitCast(Loc, Ty); 2752d33d630SMike Stump Loc = Builder.CreateLoad(Loc, false); 2762d33d630SMike Stump // Loc = Builder.CreateBitCast(Loc, Ty); 27797d01d50SMike Stump } 27897d01d50SMike Stump Builder.CreateStore(Loc, Addr); 27997d01d50SMike Stump } else if (r.isComplex()) 280b750d928SMike Stump // FIXME: implement 281b750d928SMike Stump ErrorUnsupported(BE, "complex in block literal"); 282b750d928SMike Stump else if (r.isAggregate()) 283b750d928SMike Stump ; // Already created into the destination 284b750d928SMike Stump else 285b750d928SMike Stump assert (0 && "bad block variable"); 286b750d928SMike Stump // FIXME: Ensure that the offset created by the backend for 287b750d928SMike Stump // the struct matches the previously computed offset in BlockDecls. 288b750d928SMike Stump } 289aeb0ffd5SMike Stump NoteForHelper.resize(helpersize); 290d6ef62f0SMike Stump 291d6ef62f0SMike Stump // __descriptor 292d2142cffSMike Stump llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockHasCopyDispose, 293d2142cffSMike Stump subBlockSize, Ty, 294aeb0ffd5SMike Stump &NoteForHelper); 295aeb0ffd5SMike Stump Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty); 296aeb0ffd5SMike Stump Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp")); 297b750d928SMike Stump } 298b750d928SMike Stump 299b750d928SMike Stump QualType BPT = BE->getType(); 300b750d928SMike Stump return Builder.CreateBitCast(V, ConvertType(BPT)); 301b750d928SMike Stump } 30285284bacSMike Stump 30385284bacSMike Stump 30495435674SMike Stump const llvm::Type *BlockModule::getBlockDescriptorType() { 305650c932dSMike Stump if (BlockDescriptorType) 306650c932dSMike Stump return BlockDescriptorType; 3072437cbfaSAnders Carlsson 3082437cbfaSAnders Carlsson const llvm::Type *UnsignedLongTy = 309650c932dSMike Stump getTypes().ConvertType(getContext().UnsignedLongTy); 3102437cbfaSAnders Carlsson 3112437cbfaSAnders Carlsson // struct __block_descriptor { 3122437cbfaSAnders Carlsson // unsigned long reserved; 3132437cbfaSAnders Carlsson // unsigned long block_size; 3142437cbfaSAnders Carlsson // }; 315758428f4SOwen Anderson BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(), 316758428f4SOwen Anderson UnsignedLongTy, 3172437cbfaSAnders Carlsson UnsignedLongTy, 3182437cbfaSAnders Carlsson NULL); 3192437cbfaSAnders Carlsson 320650c932dSMike Stump getModule().addTypeName("struct.__block_descriptor", 321650c932dSMike Stump BlockDescriptorType); 3222437cbfaSAnders Carlsson 323650c932dSMike Stump return BlockDescriptorType; 3242437cbfaSAnders Carlsson } 3252437cbfaSAnders Carlsson 32695435674SMike Stump const llvm::Type *BlockModule::getGenericBlockLiteralType() { 327005c9a62SMike Stump if (GenericBlockLiteralType) 328005c9a62SMike Stump return GenericBlockLiteralType; 3292437cbfaSAnders Carlsson 3302437cbfaSAnders Carlsson const llvm::Type *BlockDescPtrTy = 3319793f0e4SOwen Anderson llvm::PointerType::getUnqual(getBlockDescriptorType()); 3322437cbfaSAnders Carlsson 33392bbd6d4SMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 33492bbd6d4SMike Stump getTypes().ConvertType(getContext().IntTy)); 33592bbd6d4SMike Stump 3362437cbfaSAnders Carlsson // struct __block_literal_generic { 3375d2534adSMike Stump // void *__isa; 3385d2534adSMike Stump // int __flags; 3395d2534adSMike Stump // int __reserved; 3405d2534adSMike Stump // void (*__invoke)(void *); 3415d2534adSMike Stump // struct __block_descriptor *__descriptor; 3422437cbfaSAnders Carlsson // }; 343758428f4SOwen Anderson GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(), 344758428f4SOwen Anderson PtrToInt8Ty, 34592bbd6d4SMike Stump IntTy, 34692bbd6d4SMike Stump IntTy, 347626aecc4SMike Stump PtrToInt8Ty, 3482437cbfaSAnders Carlsson BlockDescPtrTy, 3492437cbfaSAnders Carlsson NULL); 3502437cbfaSAnders Carlsson 351005c9a62SMike Stump getModule().addTypeName("struct.__block_literal_generic", 352005c9a62SMike Stump GenericBlockLiteralType); 3532437cbfaSAnders Carlsson 354005c9a62SMike Stump return GenericBlockLiteralType; 3552437cbfaSAnders Carlsson } 3562437cbfaSAnders Carlsson 35795435674SMike Stump const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() { 3585d2534adSMike Stump if (GenericExtendedBlockLiteralType) 3595d2534adSMike Stump return GenericExtendedBlockLiteralType; 3605d2534adSMike Stump 3615d2534adSMike Stump const llvm::Type *BlockDescPtrTy = 3629793f0e4SOwen Anderson llvm::PointerType::getUnqual(getBlockDescriptorType()); 3635d2534adSMike Stump 3645d2534adSMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 3655d2534adSMike Stump getTypes().ConvertType(getContext().IntTy)); 3665d2534adSMike Stump 3675d2534adSMike Stump // struct __block_literal_generic { 3685d2534adSMike Stump // void *__isa; 3695d2534adSMike Stump // int __flags; 3705d2534adSMike Stump // int __reserved; 3715d2534adSMike Stump // void (*__invoke)(void *); 3725d2534adSMike Stump // struct __block_descriptor *__descriptor; 3735d2534adSMike Stump // void *__copy_func_helper_decl; 3745d2534adSMike Stump // void *__destroy_func_decl; 3755d2534adSMike Stump // }; 376758428f4SOwen Anderson GenericExtendedBlockLiteralType = llvm::StructType::get(IntTy->getContext(), 377758428f4SOwen Anderson PtrToInt8Ty, 3785d2534adSMike Stump IntTy, 3795d2534adSMike Stump IntTy, 380626aecc4SMike Stump PtrToInt8Ty, 3815d2534adSMike Stump BlockDescPtrTy, 382626aecc4SMike Stump PtrToInt8Ty, 383626aecc4SMike Stump PtrToInt8Ty, 3845d2534adSMike Stump NULL); 3855d2534adSMike Stump 3865d2534adSMike Stump getModule().addTypeName("struct.__block_literal_extended_generic", 3875d2534adSMike Stump GenericExtendedBlockLiteralType); 3885d2534adSMike Stump 3895d2534adSMike Stump return GenericExtendedBlockLiteralType; 3905d2534adSMike Stump } 3915d2534adSMike Stump 392*2114d7c2SMike Stump bool BlockFunction::BlockRequiresCopying(QualType Ty) { 393*2114d7c2SMike Stump return CGM.BlockRequiresCopying(Ty); 394*2114d7c2SMike Stump } 395*2114d7c2SMike Stump 3966a60fa24SAnders Carlsson RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) { 3972437cbfaSAnders Carlsson const BlockPointerType *BPT = 398c23c7e6aSTed Kremenek E->getCallee()->getType()->getAs<BlockPointerType>(); 3992437cbfaSAnders Carlsson 4002437cbfaSAnders Carlsson llvm::Value *Callee = EmitScalarExpr(E->getCallee()); 4012437cbfaSAnders Carlsson 4022437cbfaSAnders Carlsson // Get a pointer to the generic block literal. 4032437cbfaSAnders Carlsson const llvm::Type *BlockLiteralTy = 4049793f0e4SOwen Anderson llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType()); 4052437cbfaSAnders Carlsson 4062437cbfaSAnders Carlsson // Bitcast the callee to a block literal. 4072437cbfaSAnders Carlsson llvm::Value *BlockLiteral = 4082437cbfaSAnders Carlsson Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal"); 4092437cbfaSAnders Carlsson 4102437cbfaSAnders Carlsson // Get the function pointer from the literal. 4112437cbfaSAnders Carlsson llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp"); 4122437cbfaSAnders Carlsson 4132437cbfaSAnders Carlsson BlockLiteral = 4142437cbfaSAnders Carlsson Builder.CreateBitCast(BlockLiteral, 41541a75027SOwen Anderson llvm::PointerType::getUnqual( 41641a75027SOwen Anderson llvm::Type::getInt8Ty(VMContext)), 4172437cbfaSAnders Carlsson "tmp"); 4182437cbfaSAnders Carlsson 4192437cbfaSAnders Carlsson // Add the block literal. 4202437cbfaSAnders Carlsson QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy); 4212437cbfaSAnders Carlsson CallArgList Args; 4222437cbfaSAnders Carlsson Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy)); 4232437cbfaSAnders Carlsson 424479e6fc7SAnders Carlsson QualType FnType = BPT->getPointeeType(); 425479e6fc7SAnders Carlsson 4262437cbfaSAnders Carlsson // And the rest of the arguments. 4279dd450bbSJohn McCall EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(), 428479e6fc7SAnders Carlsson E->arg_begin(), E->arg_end()); 4292437cbfaSAnders Carlsson 4305f50c651SAnders Carlsson // Load the function. 4315f50c651SAnders Carlsson llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp"); 4325f50c651SAnders Carlsson 4339dd450bbSJohn McCall QualType ResultType = FnType->getAs<FunctionType>()->getResultType(); 434a60cbcdfSAnders Carlsson 435a60cbcdfSAnders Carlsson const CGFunctionInfo &FnInfo = 436a60cbcdfSAnders Carlsson CGM.getTypes().getFunctionInfo(ResultType, Args); 4375f50c651SAnders Carlsson 4385f50c651SAnders Carlsson // Cast the function pointer to the right type. 4395f50c651SAnders Carlsson const llvm::Type *BlockFTy = 440a60cbcdfSAnders Carlsson CGM.getTypes().GetFunctionType(FnInfo, false); 4415f50c651SAnders Carlsson 4429793f0e4SOwen Anderson const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy); 4435f50c651SAnders Carlsson Func = Builder.CreateBitCast(Func, BlockFTyPtr); 4445f50c651SAnders Carlsson 4452437cbfaSAnders Carlsson // And call the block. 446350da607SAnders Carlsson return EmitCall(FnInfo, Func, Args); 4472437cbfaSAnders Carlsson } 4486a60fa24SAnders Carlsson 44997d01d50SMike Stump llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) { 4500168f4b2SAnders Carlsson const ValueDecl *VD = E->getDecl(); 45197d01d50SMike Stump 4520168f4b2SAnders Carlsson uint64_t &offset = BlockDecls[VD]; 4530168f4b2SAnders Carlsson 45497d01d50SMike Stump 45597d01d50SMike Stump // See if we have already allocated an offset for this variable. 45697d01d50SMike Stump if (offset == 0) { 4574446dcf0SMike Stump // Don't run the expensive check, unless we have to. 4584446dcf0SMike Stump if (!BlockHasCopyDispose && BlockRequiresCopying(E->getType())) 4594446dcf0SMike Stump BlockHasCopyDispose = true; 46097d01d50SMike Stump // if not, allocate one now. 46197d01d50SMike Stump offset = getBlockOffset(E); 46297d01d50SMike Stump } 46397d01d50SMike Stump 46497d01d50SMike Stump llvm::Value *BlockLiteral = LoadBlockStruct(); 46597d01d50SMike Stump llvm::Value *V = Builder.CreateGEP(BlockLiteral, 46641a75027SOwen Anderson llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), 46797d01d50SMike Stump offset), 4682d33d630SMike Stump "block.literal"); 46997d01d50SMike Stump if (E->isByRef()) { 47097d01d50SMike Stump const llvm::Type *PtrStructTy 4710168f4b2SAnders Carlsson = llvm::PointerType::get(BuildByRefType(VD), 0); 472efd7caa8SMike Stump // The block literal will need a copy/destroy helper. 473efd7caa8SMike Stump BlockHasCopyDispose = true; 4740168f4b2SAnders Carlsson 4750168f4b2SAnders Carlsson const llvm::Type *Ty = PtrStructTy; 4769793f0e4SOwen Anderson Ty = llvm::PointerType::get(Ty, 0); 47797d01d50SMike Stump V = Builder.CreateBitCast(V, Ty); 47897d01d50SMike Stump V = Builder.CreateLoad(V, false); 47997d01d50SMike Stump V = Builder.CreateStructGEP(V, 1, "forwarding"); 48097d01d50SMike Stump V = Builder.CreateLoad(V, false); 48197d01d50SMike Stump V = Builder.CreateBitCast(V, PtrStructTy); 4820168f4b2SAnders Carlsson V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD), 4830168f4b2SAnders Carlsson VD->getNameAsString()); 48497d01d50SMike Stump } else { 4850168f4b2SAnders Carlsson const llvm::Type *Ty = CGM.getTypes().ConvertType(VD->getType()); 4860168f4b2SAnders Carlsson 4879793f0e4SOwen Anderson Ty = llvm::PointerType::get(Ty, 0); 48897d01d50SMike Stump V = Builder.CreateBitCast(V, Ty); 48997d01d50SMike Stump } 49097d01d50SMike Stump return V; 49197d01d50SMike Stump } 49297d01d50SMike Stump 493692c6e37SMike Stump void CodeGenFunction::BlockForwardSelf() { 494692c6e37SMike Stump const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); 495692c6e37SMike Stump ImplicitParamDecl *SelfDecl = OMD->getSelfDecl(); 496692c6e37SMike Stump llvm::Value *&DMEntry = LocalDeclMap[SelfDecl]; 497692c6e37SMike Stump if (DMEntry) 498692c6e37SMike Stump return; 499692c6e37SMike Stump // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care 500692c6e37SMike Stump BlockDeclRefExpr *BDRE = new (getContext()) 501692c6e37SMike Stump BlockDeclRefExpr(SelfDecl, 502692c6e37SMike Stump SelfDecl->getType(), SourceLocation(), false); 503692c6e37SMike Stump DMEntry = GetAddrOfBlockDecl(BDRE); 504692c6e37SMike Stump } 505692c6e37SMike Stump 5062d5a2878SMike Stump llvm::Constant * 5076c39666aSMike Stump BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) { 5086a60fa24SAnders Carlsson // Generate the block descriptor. 5096a60fa24SAnders Carlsson const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy); 51092bbd6d4SMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 51192bbd6d4SMike Stump getTypes().ConvertType(getContext().IntTy)); 5126a60fa24SAnders Carlsson 5136a60fa24SAnders Carlsson llvm::Constant *DescriptorFields[2]; 5146a60fa24SAnders Carlsson 5156a60fa24SAnders Carlsson // Reserved 5160b75f23bSOwen Anderson DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy); 5176a60fa24SAnders Carlsson 5186a60fa24SAnders Carlsson // Block literal size. For global blocks we just use the size of the generic 5196a60fa24SAnders Carlsson // block literal struct. 5206a60fa24SAnders Carlsson uint64_t BlockLiteralSize = 521005c9a62SMike Stump TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8; 522170229f6SOwen Anderson DescriptorFields[1] = 523b7a2fe6fSOwen Anderson llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize); 5246a60fa24SAnders Carlsson 5256a60fa24SAnders Carlsson llvm::Constant *DescriptorStruct = 52641eaf0acSNick Lewycky llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 2, false); 5276a60fa24SAnders Carlsson 5286a60fa24SAnders Carlsson llvm::GlobalVariable *Descriptor = 529c10c8d3eSOwen Anderson new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true, 5306a60fa24SAnders Carlsson llvm::GlobalVariable::InternalLinkage, 531c10c8d3eSOwen Anderson DescriptorStruct, "__block_descriptor_global"); 5326a60fa24SAnders Carlsson 5336a60fa24SAnders Carlsson // Generate the constants for the block literal. 5346a60fa24SAnders Carlsson llvm::Constant *LiteralFields[5]; 5356a60fa24SAnders Carlsson 5362d5a2878SMike Stump CodeGenFunction::BlockInfo Info(0, n); 537b750d928SMike Stump uint64_t subBlockSize, subBlockAlign; 5381db7d04bSMike Stump llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; 539a61bb4d1SDaniel Dunbar bool subBlockHasCopyDispose = false; 5405469f29aSMike Stump llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap; 541cb2fbcb0SMike Stump llvm::Function *Fn 542692c6e37SMike Stump = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap, 5435469f29aSMike Stump subBlockSize, 544b750d928SMike Stump subBlockAlign, 5454446dcf0SMike Stump subBlockDeclRefDecls, 5464446dcf0SMike Stump subBlockHasCopyDispose); 547cb2fbcb0SMike Stump assert(subBlockSize == BlockLiteralSize 548cb2fbcb0SMike Stump && "no imports allowed for global block"); 5496a60fa24SAnders Carlsson 5506a60fa24SAnders Carlsson // isa 551971f9b66SMike Stump LiteralFields[0] = getNSConcreteGlobalBlock(); 5526a60fa24SAnders Carlsson 5536a60fa24SAnders Carlsson // Flags 5545878c797SAnders Carlsson LiteralFields[1] = 555b7a2fe6fSOwen Anderson llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR); 5566a60fa24SAnders Carlsson 5576a60fa24SAnders Carlsson // Reserved 5580b75f23bSOwen Anderson LiteralFields[2] = llvm::Constant::getNullValue(IntTy); 5596a60fa24SAnders Carlsson 5606a60fa24SAnders Carlsson // Function 5616a60fa24SAnders Carlsson LiteralFields[3] = Fn; 5626a60fa24SAnders Carlsson 5636a60fa24SAnders Carlsson // Descriptor 5646a60fa24SAnders Carlsson LiteralFields[4] = Descriptor; 5656a60fa24SAnders Carlsson 5666a60fa24SAnders Carlsson llvm::Constant *BlockLiteralStruct = 56741eaf0acSNick Lewycky llvm::ConstantStruct::get(VMContext, &LiteralFields[0], 5, false); 5686a60fa24SAnders Carlsson 5696a60fa24SAnders Carlsson llvm::GlobalVariable *BlockLiteral = 570c10c8d3eSOwen Anderson new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true, 5716a60fa24SAnders Carlsson llvm::GlobalVariable::InternalLinkage, 572c10c8d3eSOwen Anderson BlockLiteralStruct, "__block_literal_global"); 5736a60fa24SAnders Carlsson 5746a60fa24SAnders Carlsson return BlockLiteral; 5756a60fa24SAnders Carlsson } 5766a60fa24SAnders Carlsson 577cb2fbcb0SMike Stump llvm::Value *CodeGenFunction::LoadBlockStruct() { 578cb2fbcb0SMike Stump return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self"); 579cb2fbcb0SMike Stump } 580cb2fbcb0SMike Stump 5814446dcf0SMike Stump llvm::Function * 5824446dcf0SMike Stump CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr, 583cb2fbcb0SMike Stump const BlockInfo& Info, 584692c6e37SMike Stump const Decl *OuterFuncDecl, 5855469f29aSMike Stump llvm::DenseMap<const Decl*, llvm::Value*> ldm, 586b750d928SMike Stump uint64_t &Size, 587b750d928SMike Stump uint64_t &Align, 5884446dcf0SMike Stump llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls, 5894446dcf0SMike Stump bool &subBlockHasCopyDispose) { 5909074ed8cSDevang Patel 5919074ed8cSDevang Patel // Check if we should generate debug info for this block. 5929074ed8cSDevang Patel if (CGM.getDebugInfo()) 5939074ed8cSDevang Patel DebugInfo = CGM.getDebugInfo(); 5949074ed8cSDevang Patel 5955469f29aSMike Stump // Arrange for local static and local extern declarations to appear 5965469f29aSMike Stump // to be local to this function as well, as they are directly referenced 5975469f29aSMike Stump // in a block. 5985469f29aSMike Stump for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin(); 5995469f29aSMike Stump i != ldm.end(); 6005469f29aSMike Stump ++i) { 6015469f29aSMike Stump const VarDecl *VD = dyn_cast<VarDecl>(i->first); 6025469f29aSMike Stump 6030ca16601SDaniel Dunbar if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage()) 6045469f29aSMike Stump LocalDeclMap[VD] = i->second; 6055469f29aSMike Stump } 6065469f29aSMike Stump 60709a9b6e3SEli Friedman // FIXME: We need to rearrange the code for copy/dispose so we have this 60809a9b6e3SEli Friedman // sooner, so we can calculate offsets correctly. 60909a9b6e3SEli Friedman if (!BlockHasCopyDispose) 61009a9b6e3SEli Friedman BlockOffset = CGM.getTargetData() 61109a9b6e3SEli Friedman .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8; 61209a9b6e3SEli Friedman else 61309a9b6e3SEli Friedman BlockOffset = CGM.getTargetData() 61409a9b6e3SEli Friedman .getTypeStoreSizeInBits(CGM.getGenericExtendedBlockLiteralType()) / 8; 61509a9b6e3SEli Friedman BlockAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 61609a9b6e3SEli Friedman 617aae4349dSFariborz Jahanian const FunctionType *BlockFunctionType = BExpr->getFunctionType(); 618aae4349dSFariborz Jahanian QualType ResultType; 619aae4349dSFariborz Jahanian bool IsVariadic; 620a4f6b6b1SFariborz Jahanian if (const FunctionProtoType *FTy = 621a4f6b6b1SFariborz Jahanian dyn_cast<FunctionProtoType>(BlockFunctionType)) { 622aae4349dSFariborz Jahanian ResultType = FTy->getResultType(); 623aae4349dSFariborz Jahanian IsVariadic = FTy->isVariadic(); 624658fe02dSMike Stump } else { 625aae4349dSFariborz Jahanian // K&R style block. 626aae4349dSFariborz Jahanian ResultType = BlockFunctionType->getResultType(); 627aae4349dSFariborz Jahanian IsVariadic = false; 628aae4349dSFariborz Jahanian } 6296a60fa24SAnders Carlsson 6306a60fa24SAnders Carlsson FunctionArgList Args; 6316a60fa24SAnders Carlsson 6323385fe12SChris Lattner const BlockDecl *BD = BExpr->getBlockDecl(); 6336a60fa24SAnders Carlsson 6346a60fa24SAnders Carlsson // FIXME: This leaks 6356a60fa24SAnders Carlsson ImplicitParamDecl *SelfDecl = 6366a60fa24SAnders Carlsson ImplicitParamDecl::Create(getContext(), 0, 6376a60fa24SAnders Carlsson SourceLocation(), 0, 6386a60fa24SAnders Carlsson getContext().getPointerType(getContext().VoidTy)); 6396a60fa24SAnders Carlsson 6406a60fa24SAnders Carlsson Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType())); 641cb2fbcb0SMike Stump BlockStructDecl = SelfDecl; 6426a60fa24SAnders Carlsson 643c4b30e59SSteve Naroff for (BlockDecl::param_const_iterator i = BD->param_begin(), 6446a60fa24SAnders Carlsson e = BD->param_end(); i != e; ++i) 645eecd39f2SMike Stump Args.push_back(std::make_pair(*i, (*i)->getType())); 6466a60fa24SAnders Carlsson 6476a60fa24SAnders Carlsson const CGFunctionInfo &FI = 648aae4349dSFariborz Jahanian CGM.getTypes().getFunctionInfo(ResultType, Args); 6496a60fa24SAnders Carlsson 6502d5a2878SMike Stump std::string Name = std::string("__") + Info.Name + "_block_invoke_"; 6516a60fa24SAnders Carlsson CodeGenTypes &Types = CGM.getTypes(); 652aae4349dSFariborz Jahanian const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic); 6536a60fa24SAnders Carlsson 6546a60fa24SAnders Carlsson llvm::Function *Fn = 6556a60fa24SAnders Carlsson llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 6566a60fa24SAnders Carlsson Name, 6576a60fa24SAnders Carlsson &CGM.getModule()); 6586a60fa24SAnders Carlsson 659c3e7cff6SDaniel Dunbar CGM.SetInternalFunctionAttributes(BD, Fn, FI); 660c3e7cff6SDaniel Dunbar 661324f80b1SChris Lattner StartFunction(BD, ResultType, Fn, Args, 6623385fe12SChris Lattner BExpr->getBody()->getLocEnd()); 663324f80b1SChris Lattner CurFuncDecl = OuterFuncDecl; 66428ec0cf8SChris Lattner CurCodeDecl = BD; 6653385fe12SChris Lattner EmitStmt(BExpr->getBody()); 6663385fe12SChris Lattner FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc()); 6676a60fa24SAnders Carlsson 668b750d928SMike Stump // The runtime needs a minimum alignment of a void *. 669b750d928SMike Stump uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 670b750d928SMike Stump BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign); 671b750d928SMike Stump 672cb2fbcb0SMike Stump Size = BlockOffset; 673b750d928SMike Stump Align = BlockAlign; 674b750d928SMike Stump subBlockDeclRefDecls = BlockDeclRefDecls; 6754446dcf0SMike Stump subBlockHasCopyDispose |= BlockHasCopyDispose; 6766a60fa24SAnders Carlsson return Fn; 6776a60fa24SAnders Carlsson } 6781db7d04bSMike Stump 679aeb0ffd5SMike Stump uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) { 6801db7d04bSMike Stump const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl()); 6811db7d04bSMike Stump 6821db7d04bSMike Stump uint64_t Size = getContext().getTypeSize(D->getType()) / 8; 6831db7d04bSMike Stump uint64_t Align = getContext().getDeclAlignInBytes(D); 6841db7d04bSMike Stump 6851db7d04bSMike Stump if (BDRE->isByRef()) { 6861db7d04bSMike Stump Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8; 6871db7d04bSMike Stump Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 6881db7d04bSMike Stump } 6891db7d04bSMike Stump 6901db7d04bSMike Stump assert ((Align > 0) && "alignment must be 1 byte or more"); 6911db7d04bSMike Stump 6921db7d04bSMike Stump uint64_t OldOffset = BlockOffset; 6931db7d04bSMike Stump 6941db7d04bSMike Stump // Ensure proper alignment, even if it means we have to have a gap 6951db7d04bSMike Stump BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align); 6961db7d04bSMike Stump BlockAlign = std::max(Align, BlockAlign); 6971db7d04bSMike Stump 6981db7d04bSMike Stump uint64_t Pad = BlockOffset - OldOffset; 6991db7d04bSMike Stump if (Pad) { 70041a75027SOwen Anderson llvm::ArrayType::get(llvm::Type::getInt8Ty(VMContext), Pad); 7011db7d04bSMike Stump QualType PadTy = getContext().getConstantArrayType(getContext().CharTy, 7021db7d04bSMike Stump llvm::APInt(32, Pad), 7031db7d04bSMike Stump ArrayType::Normal, 0); 7041db7d04bSMike Stump ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(), 7056032ef1aSArgyrios Kyrtzidis 0, QualType(PadTy), 0, VarDecl::None); 7061db7d04bSMike Stump Expr *E; 7071db7d04bSMike Stump E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(), 7081db7d04bSMike Stump SourceLocation(), false, false); 7091db7d04bSMike Stump BlockDeclRefDecls.push_back(E); 7101db7d04bSMike Stump } 7111db7d04bSMike Stump BlockDeclRefDecls.push_back(BDRE); 7121db7d04bSMike Stump 7131db7d04bSMike Stump BlockOffset += Size; 7141db7d04bSMike Stump return BlockOffset-Size; 7151db7d04bSMike Stump } 71697d01d50SMike Stump 717aeb0ffd5SMike Stump llvm::Constant *BlockFunction:: 718aeb0ffd5SMike Stump GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T, 71967645930SMike Stump std::vector<HelperInfo> *NoteForHelperp) { 7200c743277SMike Stump QualType R = getContext().VoidTy; 7210c743277SMike Stump 7220c743277SMike Stump FunctionArgList Args; 7230c743277SMike Stump // FIXME: This leaks 724aeb0ffd5SMike Stump ImplicitParamDecl *Dst = 725aeb0ffd5SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 726aeb0ffd5SMike Stump getContext().getPointerType(getContext().VoidTy)); 727aeb0ffd5SMike Stump Args.push_back(std::make_pair(Dst, Dst->getType())); 7280c743277SMike Stump ImplicitParamDecl *Src = 7290c743277SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 7300c743277SMike Stump getContext().getPointerType(getContext().VoidTy)); 7310c743277SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 7320c743277SMike Stump 7330c743277SMike Stump const CGFunctionInfo &FI = 7340c743277SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 7350c743277SMike Stump 736cbc2bcafSMike Stump // FIXME: We'd like to put these into a mergable by content, with 737cbc2bcafSMike Stump // internal linkage. 7380c743277SMike Stump std::string Name = std::string("__copy_helper_block_"); 7390c743277SMike Stump CodeGenTypes &Types = CGM.getTypes(); 7400c743277SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 7410c743277SMike Stump 7420c743277SMike Stump llvm::Function *Fn = 7430c743277SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 7440c743277SMike Stump Name, 7450c743277SMike Stump &CGM.getModule()); 7460c743277SMike Stump 7470c743277SMike Stump IdentifierInfo *II 7480c743277SMike Stump = &CGM.getContext().Idents.get("__copy_helper_block_"); 7490c743277SMike Stump 7500c743277SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 7510c743277SMike Stump getContext().getTranslationUnitDecl(), 75260ed5604SArgyrios Kyrtzidis SourceLocation(), II, R, 0, 7530c743277SMike Stump FunctionDecl::Static, false, 7540c743277SMike Stump true); 7550c743277SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 756aeb0ffd5SMike Stump 757aeb0ffd5SMike Stump llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); 758aeb0ffd5SMike Stump llvm::Type *PtrPtrT; 75967645930SMike Stump 76067645930SMike Stump if (NoteForHelperp) { 76167645930SMike Stump std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp; 76267645930SMike Stump 7639793f0e4SOwen Anderson PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 764aeb0ffd5SMike Stump SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); 765aeb0ffd5SMike Stump SrcObj = Builder.CreateLoad(SrcObj); 766aeb0ffd5SMike Stump 767aeb0ffd5SMike Stump llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst); 7683b65ac26SMike Stump llvm::Type *PtrPtrT; 7699793f0e4SOwen Anderson PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 7703b65ac26SMike Stump DstObj = Builder.CreateBitCast(DstObj, PtrPtrT); 7713b65ac26SMike Stump DstObj = Builder.CreateLoad(DstObj); 772aeb0ffd5SMike Stump 773aeb0ffd5SMike Stump for (unsigned i=0; i < NoteForHelper.size(); ++i) { 774aeb0ffd5SMike Stump int flag = NoteForHelper[i].flag; 775aeb0ffd5SMike Stump int index = NoteForHelper[i].index; 776aeb0ffd5SMike Stump 777aeb0ffd5SMike Stump if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) 778aeb0ffd5SMike Stump || NoteForHelper[i].RequiresCopying) { 779aeb0ffd5SMike Stump llvm::Value *Srcv = SrcObj; 780aeb0ffd5SMike Stump Srcv = Builder.CreateStructGEP(Srcv, index); 781aeb0ffd5SMike Stump Srcv = Builder.CreateBitCast(Srcv, 7829793f0e4SOwen Anderson llvm::PointerType::get(PtrToInt8Ty, 0)); 783aeb0ffd5SMike Stump Srcv = Builder.CreateLoad(Srcv); 784aeb0ffd5SMike Stump 785aeb0ffd5SMike Stump llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index); 786aeb0ffd5SMike Stump Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty); 787aeb0ffd5SMike Stump 78841a75027SOwen Anderson llvm::Value *N = llvm::ConstantInt::get( 78941a75027SOwen Anderson llvm::Type::getInt32Ty(T->getContext()), flag); 790aeb0ffd5SMike Stump llvm::Value *F = getBlockObjectAssign(); 791aeb0ffd5SMike Stump Builder.CreateCall3(F, Dstv, Srcv, N); 792aeb0ffd5SMike Stump } 793aeb0ffd5SMike Stump } 79467645930SMike Stump } 795aeb0ffd5SMike Stump 7960c743277SMike Stump CGF.FinishFunction(); 7970c743277SMike Stump 798ade90fd1SOwen Anderson return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 79997d01d50SMike Stump } 80097d01d50SMike Stump 801d6ef62f0SMike Stump llvm::Constant *BlockFunction:: 802aeb0ffd5SMike Stump GenerateDestroyHelperFunction(bool BlockHasCopyDispose, 803aeb0ffd5SMike Stump const llvm::StructType* T, 80467645930SMike Stump std::vector<HelperInfo> *NoteForHelperp) { 8050c743277SMike Stump QualType R = getContext().VoidTy; 8060c743277SMike Stump 8070c743277SMike Stump FunctionArgList Args; 8080c743277SMike Stump // FIXME: This leaks 8090c743277SMike Stump ImplicitParamDecl *Src = 8100c743277SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 8110c743277SMike Stump getContext().getPointerType(getContext().VoidTy)); 8120c743277SMike Stump 8130c743277SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 8140c743277SMike Stump 8150c743277SMike Stump const CGFunctionInfo &FI = 8160c743277SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 8170c743277SMike Stump 818cbc2bcafSMike Stump // FIXME: We'd like to put these into a mergable by content, with 819cbc2bcafSMike Stump // internal linkage. 8200c743277SMike Stump std::string Name = std::string("__destroy_helper_block_"); 8210c743277SMike Stump CodeGenTypes &Types = CGM.getTypes(); 8220c743277SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 8230c743277SMike Stump 8240c743277SMike Stump llvm::Function *Fn = 8250c743277SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 8260c743277SMike Stump Name, 8270c743277SMike Stump &CGM.getModule()); 8280c743277SMike Stump 8290c743277SMike Stump IdentifierInfo *II 8300c743277SMike Stump = &CGM.getContext().Idents.get("__destroy_helper_block_"); 8310c743277SMike Stump 8320c743277SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 8330c743277SMike Stump getContext().getTranslationUnitDecl(), 83460ed5604SArgyrios Kyrtzidis SourceLocation(), II, R, 0, 8350c743277SMike Stump FunctionDecl::Static, false, 8360c743277SMike Stump true); 8370c743277SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 8386f7d9f87SMike Stump 83967645930SMike Stump if (NoteForHelperp) { 84067645930SMike Stump std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp; 84167645930SMike Stump 8426f7d9f87SMike Stump llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); 8436f7d9f87SMike Stump llvm::Type *PtrPtrT; 8449793f0e4SOwen Anderson 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, 8579793f0e4SOwen Anderson llvm::PointerType::get(PtrToInt8Ty, 0)); 8586f7d9f87SMike Stump Srcv = Builder.CreateLoad(Srcv); 8596f7d9f87SMike Stump 8606f7d9f87SMike Stump BuildBlockRelease(Srcv, flag); 8616f7d9f87SMike Stump } 8626f7d9f87SMike Stump } 86367645930SMike Stump } 8646f7d9f87SMike Stump 8650c743277SMike Stump CGF.FinishFunction(); 8660c743277SMike Stump 867ade90fd1SOwen Anderson return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 8680c743277SMike Stump } 8690c743277SMike Stump 870aeb0ffd5SMike Stump llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T, 87167645930SMike Stump std::vector<HelperInfo> *NoteForHelper) { 872aeb0ffd5SMike Stump return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose, 873aeb0ffd5SMike Stump T, NoteForHelper); 8740c743277SMike Stump } 8750c743277SMike Stump 876aeb0ffd5SMike Stump llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T, 87767645930SMike Stump std::vector<HelperInfo> *NoteForHelperp) { 878aeb0ffd5SMike Stump return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose, 87967645930SMike Stump T, NoteForHelperp); 88097d01d50SMike Stump } 881626aecc4SMike Stump 882f89230d9SMike Stump llvm::Constant *BlockFunction:: 883f89230d9SMike Stump GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) { 884ee2a5ee5SMike Stump QualType R = getContext().VoidTy; 885ee2a5ee5SMike Stump 886ee2a5ee5SMike Stump FunctionArgList Args; 887ee2a5ee5SMike Stump // FIXME: This leaks 888f89230d9SMike Stump ImplicitParamDecl *Dst = 889f89230d9SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 890f89230d9SMike Stump getContext().getPointerType(getContext().VoidTy)); 891f89230d9SMike Stump Args.push_back(std::make_pair(Dst, Dst->getType())); 892f89230d9SMike Stump 893f89230d9SMike Stump // FIXME: This leaks 894ee2a5ee5SMike Stump ImplicitParamDecl *Src = 895ee2a5ee5SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 896ee2a5ee5SMike Stump getContext().getPointerType(getContext().VoidTy)); 897ee2a5ee5SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 898ee2a5ee5SMike Stump 899ee2a5ee5SMike Stump const CGFunctionInfo &FI = 900ee2a5ee5SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 901ee2a5ee5SMike Stump 902ee2a5ee5SMike Stump std::string Name = std::string("__Block_byref_id_object_copy_"); 903ee2a5ee5SMike Stump CodeGenTypes &Types = CGM.getTypes(); 904ee2a5ee5SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 905ee2a5ee5SMike Stump 906cbc2bcafSMike Stump // FIXME: We'd like to put these into a mergable by content, with 907cbc2bcafSMike Stump // internal linkage. 908ee2a5ee5SMike Stump llvm::Function *Fn = 909ee2a5ee5SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 910ee2a5ee5SMike Stump Name, 911ee2a5ee5SMike Stump &CGM.getModule()); 912ee2a5ee5SMike Stump 913ee2a5ee5SMike Stump IdentifierInfo *II 914ee2a5ee5SMike Stump = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_"); 915ee2a5ee5SMike Stump 916ee2a5ee5SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 917ee2a5ee5SMike Stump getContext().getTranslationUnitDecl(), 91860ed5604SArgyrios Kyrtzidis SourceLocation(), II, R, 0, 919ee2a5ee5SMike Stump FunctionDecl::Static, false, 920ee2a5ee5SMike Stump true); 921ee2a5ee5SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 922f89230d9SMike Stump 923f89230d9SMike Stump // dst->x 924f89230d9SMike Stump llvm::Value *V = CGF.GetAddrOfLocalVar(Dst); 9259793f0e4SOwen Anderson V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0)); 9263b65ac26SMike Stump V = Builder.CreateLoad(V); 927f89230d9SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 928f89230d9SMike Stump llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty); 929f89230d9SMike Stump 930f89230d9SMike Stump // src->x 931f89230d9SMike Stump V = CGF.GetAddrOfLocalVar(Src); 932f89230d9SMike Stump V = Builder.CreateLoad(V); 933f89230d9SMike Stump V = Builder.CreateBitCast(V, T); 934f89230d9SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 9359793f0e4SOwen Anderson V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); 936f89230d9SMike Stump llvm::Value *SrcObj = Builder.CreateLoad(V); 937f89230d9SMike Stump 938f89230d9SMike Stump flag |= BLOCK_BYREF_CALLER; 939f89230d9SMike Stump 94041a75027SOwen Anderson llvm::Value *N = llvm::ConstantInt::get( 94141a75027SOwen Anderson llvm::Type::getInt32Ty(T->getContext()), flag); 942f89230d9SMike Stump llvm::Value *F = getBlockObjectAssign(); 943f89230d9SMike Stump Builder.CreateCall3(F, DstObj, SrcObj, N); 944f89230d9SMike Stump 945ee2a5ee5SMike Stump CGF.FinishFunction(); 946ee2a5ee5SMike Stump 947ade90fd1SOwen Anderson return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 948ee2a5ee5SMike Stump } 949ee2a5ee5SMike Stump 950fbe25dd1SMike Stump llvm::Constant * 951fbe25dd1SMike Stump BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T, 952fbe25dd1SMike Stump int flag) { 953ee2a5ee5SMike Stump QualType R = getContext().VoidTy; 954ee2a5ee5SMike Stump 955ee2a5ee5SMike Stump FunctionArgList Args; 956ee2a5ee5SMike Stump // FIXME: This leaks 957ee2a5ee5SMike Stump ImplicitParamDecl *Src = 958ee2a5ee5SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 959ee2a5ee5SMike Stump getContext().getPointerType(getContext().VoidTy)); 960ee2a5ee5SMike Stump 961ee2a5ee5SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 962ee2a5ee5SMike Stump 963ee2a5ee5SMike Stump const CGFunctionInfo &FI = 964ee2a5ee5SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 965ee2a5ee5SMike Stump 966ee2a5ee5SMike Stump std::string Name = std::string("__Block_byref_id_object_dispose_"); 967ee2a5ee5SMike Stump CodeGenTypes &Types = CGM.getTypes(); 968ee2a5ee5SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 969ee2a5ee5SMike Stump 970cbc2bcafSMike Stump // FIXME: We'd like to put these into a mergable by content, with 971cbc2bcafSMike Stump // internal linkage. 972ee2a5ee5SMike Stump llvm::Function *Fn = 973ee2a5ee5SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 974ee2a5ee5SMike Stump Name, 975ee2a5ee5SMike Stump &CGM.getModule()); 976ee2a5ee5SMike Stump 977ee2a5ee5SMike Stump IdentifierInfo *II 978ee2a5ee5SMike Stump = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_"); 979ee2a5ee5SMike Stump 980ee2a5ee5SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 981ee2a5ee5SMike Stump getContext().getTranslationUnitDecl(), 98260ed5604SArgyrios Kyrtzidis SourceLocation(), II, R, 0, 983ee2a5ee5SMike Stump FunctionDecl::Static, false, 984ee2a5ee5SMike Stump true); 985ee2a5ee5SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 986fbe25dd1SMike Stump 987fbe25dd1SMike Stump llvm::Value *V = CGF.GetAddrOfLocalVar(Src); 9889793f0e4SOwen Anderson V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0)); 9893b65ac26SMike Stump V = Builder.CreateLoad(V); 990fbe25dd1SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 9919793f0e4SOwen Anderson V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); 9923b65ac26SMike Stump V = Builder.CreateLoad(V); 993fbe25dd1SMike Stump 994fbe25dd1SMike Stump flag |= BLOCK_BYREF_CALLER; 995fbe25dd1SMike Stump BuildBlockRelease(V, flag); 996ee2a5ee5SMike Stump CGF.FinishFunction(); 997ee2a5ee5SMike Stump 998ade90fd1SOwen Anderson return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 999ee2a5ee5SMike Stump } 1000ee2a5ee5SMike Stump 1001f89230d9SMike Stump llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T, 1002cbc2bcafSMike Stump int flag, unsigned Align) { 1003cbc2bcafSMike Stump // All alignments below that of pointer alignment collpase down to just 1004cbc2bcafSMike Stump // pointer alignment, as we always have at least that much alignment to begin 1005cbc2bcafSMike Stump // with. 1006cbc2bcafSMike Stump Align /= unsigned(CGF.Target.getPointerAlign(0)/8); 1007cbc2bcafSMike Stump // As an optimization, we only generate a single function of each kind we 1008cbc2bcafSMike Stump // might need. We need a different one for each alignment and for each 1009cbc2bcafSMike Stump // setting of flags. We mix Align and flag to get the kind. 1010cbc2bcafSMike Stump uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag; 1011cbc2bcafSMike Stump llvm::Constant *& Entry = CGM.AssignCache[kind]; 1012cbc2bcafSMike Stump if (Entry) 1013cbc2bcafSMike Stump return Entry; 1014cbc2bcafSMike Stump return Entry=CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag); 1015ee2a5ee5SMike Stump } 1016ee2a5ee5SMike Stump 1017fbe25dd1SMike Stump llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T, 1018cbc2bcafSMike Stump int flag, 1019cbc2bcafSMike Stump unsigned Align) { 1020cbc2bcafSMike Stump // All alignments below that of pointer alignment collpase down to just 1021cbc2bcafSMike Stump // pointer alignment, as we always have at least that much alignment to begin 1022cbc2bcafSMike Stump // with. 1023cbc2bcafSMike Stump Align /= unsigned(CGF.Target.getPointerAlign(0)/8); 1024cbc2bcafSMike Stump // As an optimization, we only generate a single function of each kind we 1025cbc2bcafSMike Stump // might need. We need a different one for each alignment and for each 1026cbc2bcafSMike Stump // setting of flags. We mix Align and flag to get the kind. 1027cbc2bcafSMike Stump uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag; 1028cbc2bcafSMike Stump llvm::Constant *& Entry = CGM.DestroyCache[kind]; 1029cbc2bcafSMike Stump if (Entry) 1030cbc2bcafSMike Stump return Entry; 1031cbc2bcafSMike Stump return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag); 1032ee2a5ee5SMike Stump } 1033ee2a5ee5SMike Stump 1034626aecc4SMike Stump llvm::Value *BlockFunction::getBlockObjectDispose() { 1035626aecc4SMike Stump if (CGM.BlockObjectDispose == 0) { 1036626aecc4SMike Stump const llvm::FunctionType *FTy; 1037626aecc4SMike Stump std::vector<const llvm::Type*> ArgTys; 103841a75027SOwen Anderson const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext); 1039626aecc4SMike Stump ArgTys.push_back(PtrToInt8Ty); 104041a75027SOwen Anderson ArgTys.push_back(llvm::Type::getInt32Ty(VMContext)); 10419793f0e4SOwen Anderson FTy = llvm::FunctionType::get(ResultType, ArgTys, false); 1042626aecc4SMike Stump CGM.BlockObjectDispose 1043626aecc4SMike Stump = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose"); 1044626aecc4SMike Stump } 1045626aecc4SMike Stump return CGM.BlockObjectDispose; 1046626aecc4SMike Stump } 1047626aecc4SMike Stump 1048f89230d9SMike Stump llvm::Value *BlockFunction::getBlockObjectAssign() { 1049f89230d9SMike Stump if (CGM.BlockObjectAssign == 0) { 1050f89230d9SMike Stump const llvm::FunctionType *FTy; 1051f89230d9SMike Stump std::vector<const llvm::Type*> ArgTys; 105241a75027SOwen Anderson const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext); 1053f89230d9SMike Stump ArgTys.push_back(PtrToInt8Ty); 1054f89230d9SMike Stump ArgTys.push_back(PtrToInt8Ty); 105541a75027SOwen Anderson ArgTys.push_back(llvm::Type::getInt32Ty(VMContext)); 10569793f0e4SOwen Anderson FTy = llvm::FunctionType::get(ResultType, ArgTys, false); 1057f89230d9SMike Stump CGM.BlockObjectAssign 1058f89230d9SMike Stump = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign"); 1059f89230d9SMike Stump } 1060f89230d9SMike Stump return CGM.BlockObjectAssign; 1061f89230d9SMike Stump } 1062f89230d9SMike Stump 1063fbe25dd1SMike Stump void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) { 1064626aecc4SMike Stump llvm::Value *F = getBlockObjectDispose(); 1065fbe25dd1SMike Stump llvm::Value *N; 1066626aecc4SMike Stump V = Builder.CreateBitCast(V, PtrToInt8Ty); 106741a75027SOwen Anderson N = llvm::ConstantInt::get(llvm::Type::getInt32Ty(V->getContext()), flag); 1068626aecc4SMike Stump Builder.CreateCall2(F, V, N); 1069626aecc4SMike Stump } 10704446dcf0SMike Stump 10714446dcf0SMike Stump ASTContext &BlockFunction::getContext() const { return CGM.getContext(); } 1072aeb0ffd5SMike Stump 1073aeb0ffd5SMike Stump BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf, 1074aeb0ffd5SMike Stump CGBuilderTy &B) 1075170229f6SOwen Anderson : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) { 107641a75027SOwen Anderson PtrToInt8Ty = llvm::PointerType::getUnqual( 107741a75027SOwen Anderson llvm::Type::getInt8Ty(VMContext)); 1078aeb0ffd5SMike Stump 1079aeb0ffd5SMike Stump BlockHasCopyDispose = false; 1080aeb0ffd5SMike Stump } 1081