12437cbfaSAnders Carlsson //===--- CGBlocks.cpp - Emit LLVM Code for declarations -------------------===// 22437cbfaSAnders Carlsson // 32437cbfaSAnders Carlsson // The LLVM Compiler Infrastructure 42437cbfaSAnders Carlsson // 52437cbfaSAnders Carlsson // This file is distributed under the University of Illinois Open Source 62437cbfaSAnders Carlsson // License. See LICENSE.TXT for details. 72437cbfaSAnders Carlsson // 82437cbfaSAnders Carlsson //===----------------------------------------------------------------------===// 92437cbfaSAnders Carlsson // 102437cbfaSAnders Carlsson // This contains code to emit blocks. 112437cbfaSAnders Carlsson // 122437cbfaSAnders Carlsson //===----------------------------------------------------------------------===// 132437cbfaSAnders Carlsson 142437cbfaSAnders Carlsson #include "CodeGenFunction.h" 152437cbfaSAnders Carlsson #include "CodeGenModule.h" 16692c6e37SMike Stump #include "clang/AST/DeclObjC.h" 172437cbfaSAnders Carlsson #include "llvm/Module.h" 186a60fa24SAnders Carlsson #include "llvm/Target/TargetData.h" 192437cbfaSAnders Carlsson 202437cbfaSAnders Carlsson #include <algorithm> 212437cbfaSAnders Carlsson 222437cbfaSAnders Carlsson using namespace clang; 232437cbfaSAnders Carlsson using namespace CodeGen; 242437cbfaSAnders Carlsson 250874160bSMike Stump // Temporary code to enable testing of __block variables 260874160bSMike Stump // #include "clang/Frontend/CompileOptions.h" 270874160bSMike Stump #include "llvm/Support/CommandLine.h" 280874160bSMike Stump static llvm::cl::opt<bool> 290874160bSMike Stump Enable__block("f__block", 300874160bSMike Stump // See all the FIXMEs for the various work that needs to be done 310874160bSMike Stump llvm::cl::desc("temporary option to turn on __block precessing " 320874160bSMike Stump "even though the code isn't done yet"), 330874160bSMike Stump llvm::cl::ValueDisallowed, llvm::cl::AllowInverse, 344446dcf0SMike Stump llvm::cl::ZeroOrMore, 351ab20caeSMike Stump llvm::cl::init(true)); 360874160bSMike Stump 37d6ef62f0SMike Stump llvm::Constant *CodeGenFunction:: 38*d2142cffSMike Stump BuildDescriptorBlockDecl(bool BlockHasCopyDispose, uint64_t Size, 39*d2142cffSMike Stump const llvm::StructType* Ty, 40aeb0ffd5SMike Stump std::vector<HelperInfo> *NoteForHelper) { 41c2c38331SMike Stump const llvm::Type *UnsignedLongTy 42c2c38331SMike Stump = CGM.getTypes().ConvertType(getContext().UnsignedLongTy); 4385284bacSMike Stump llvm::Constant *C; 4485284bacSMike Stump std::vector<llvm::Constant*> Elts; 4585284bacSMike Stump 4685284bacSMike Stump // reserved 47c2c38331SMike Stump C = llvm::ConstantInt::get(UnsignedLongTy, 0); 4885284bacSMike Stump Elts.push_back(C); 4985284bacSMike Stump 5085284bacSMike Stump // Size 512ac40a9fSMike Stump // FIXME: What is the right way to say this doesn't fit? We should give 522ac40a9fSMike Stump // a user diagnostic in that case. Better fix would be to change the 532ac40a9fSMike Stump // API to size_t. 54cb2fbcb0SMike Stump C = llvm::ConstantInt::get(UnsignedLongTy, Size); 5585284bacSMike Stump Elts.push_back(C); 5685284bacSMike Stump 5785284bacSMike Stump if (BlockHasCopyDispose) { 5885284bacSMike Stump // copy_func_helper_decl 59aeb0ffd5SMike Stump Elts.push_back(BuildCopyHelper(Ty, *NoteForHelper)); 6085284bacSMike Stump 6185284bacSMike Stump // destroy_func_decl 62aeb0ffd5SMike Stump Elts.push_back(BuildDestroyHelper(Ty, *NoteForHelper)); 6385284bacSMike Stump } 6485284bacSMike Stump 6585284bacSMike Stump C = llvm::ConstantStruct::get(Elts); 6685284bacSMike Stump 6785284bacSMike Stump C = new llvm::GlobalVariable(C->getType(), true, 6885284bacSMike Stump llvm::GlobalValue::InternalLinkage, 69499ae7ecSMike Stump C, "__block_descriptor_tmp", &CGM.getModule()); 7085284bacSMike Stump return C; 7185284bacSMike Stump } 7285284bacSMike Stump 7395435674SMike Stump llvm::Constant *BlockModule::getNSConcreteGlobalBlock() { 74971f9b66SMike Stump if (NSConcreteGlobalBlock) 75971f9b66SMike Stump return NSConcreteGlobalBlock; 76971f9b66SMike Stump 7752d9c49dSMike Stump // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the 782d33d630SMike Stump // same thing as CreateRuntimeFunction if there's already a variable with the 792d33d630SMike Stump // same name. 80971f9b66SMike Stump NSConcreteGlobalBlock 81971f9b66SMike Stump = new llvm::GlobalVariable(PtrToInt8Ty, false, 82971f9b66SMike Stump llvm::GlobalValue::ExternalLinkage, 83971f9b66SMike Stump 0, "_NSConcreteGlobalBlock", 84971f9b66SMike Stump &getModule()); 85971f9b66SMike Stump 86971f9b66SMike Stump return NSConcreteGlobalBlock; 87971f9b66SMike Stump } 88971f9b66SMike Stump 8995435674SMike Stump llvm::Constant *BlockModule::getNSConcreteStackBlock() { 907ab278dbSMike Stump if (NSConcreteStackBlock) 917ab278dbSMike Stump return NSConcreteStackBlock; 927ab278dbSMike Stump 9352d9c49dSMike Stump // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the 942d33d630SMike Stump // same thing as CreateRuntimeFunction if there's already a variable with the 952d33d630SMike Stump // same name. 967ab278dbSMike Stump NSConcreteStackBlock 977ab278dbSMike Stump = new llvm::GlobalVariable(PtrToInt8Ty, false, 987ab278dbSMike Stump llvm::GlobalValue::ExternalLinkage, 997ab278dbSMike Stump 0, "_NSConcreteStackBlock", 1007ab278dbSMike Stump &getModule()); 1017ab278dbSMike Stump 1027ab278dbSMike Stump return NSConcreteStackBlock; 1037ab278dbSMike Stump } 1047ab278dbSMike Stump 105ed5e69feSAnders Carlsson static void CollectBlockDeclRefInfo(const Stmt *S, 106ed5e69feSAnders Carlsson CodeGenFunction::BlockInfo &Info) { 107ed5e69feSAnders Carlsson for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); 108ed5e69feSAnders Carlsson I != E; ++I) 109dd942713SDaniel Dunbar if (*I) 110ed5e69feSAnders Carlsson CollectBlockDeclRefInfo(*I, Info); 111ed5e69feSAnders Carlsson 112ed5e69feSAnders Carlsson if (const BlockDeclRefExpr *DE = dyn_cast<BlockDeclRefExpr>(S)) { 113ed5e69feSAnders Carlsson // FIXME: Handle enums. 114ed5e69feSAnders Carlsson if (isa<FunctionDecl>(DE->getDecl())) 115ed5e69feSAnders Carlsson return; 116ed5e69feSAnders Carlsson 117ed5e69feSAnders Carlsson if (DE->isByRef()) 118ed5e69feSAnders Carlsson Info.ByRefDeclRefs.push_back(DE); 119ed5e69feSAnders Carlsson else 120ed5e69feSAnders Carlsson Info.ByCopyDeclRefs.push_back(DE); 121ed5e69feSAnders Carlsson } 122ed5e69feSAnders Carlsson } 123ed5e69feSAnders Carlsson 1242d33d630SMike Stump /// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be 1252d33d630SMike Stump /// declared as a global variable instead of on the stack. 126ed5e69feSAnders Carlsson static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info) 127ed5e69feSAnders Carlsson { 128ed5e69feSAnders Carlsson return Info.ByRefDeclRefs.empty() && Info.ByCopyDeclRefs.empty(); 129ed5e69feSAnders Carlsson } 130ed5e69feSAnders Carlsson 1312d33d630SMike Stump // FIXME: Push most into CGM, passing down a few bits, like current function 1322d33d630SMike Stump // name. 133b750d928SMike Stump llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) { 134ed5e69feSAnders Carlsson 135ed5e69feSAnders Carlsson std::string Name = CurFn->getName(); 136ed5e69feSAnders Carlsson CodeGenFunction::BlockInfo Info(0, Name.c_str()); 137ed5e69feSAnders Carlsson CollectBlockDeclRefInfo(BE->getBody(), Info); 138ed5e69feSAnders Carlsson 139ed5e69feSAnders Carlsson // Check if the block can be global. 1402d33d630SMike Stump // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like 1412d33d630SMike Stump // to just have one code path. We should move this function into CGM and pass 1422d33d630SMike Stump // CGF, then we can just check to see if CGF is 0. 14397d01d50SMike Stump if (0 && CanBlockBeGlobal(Info)) 144ed5e69feSAnders Carlsson return CGM.GetAddrOfGlobalBlock(BE, Name.c_str()); 14585284bacSMike Stump 1464446dcf0SMike Stump std::vector<llvm::Constant*> Elts(5); 14785284bacSMike Stump llvm::Constant *C; 148b750d928SMike Stump llvm::Value *V; 14985284bacSMike Stump 15085284bacSMike Stump { 15185284bacSMike Stump // C = BuildBlockStructInitlist(); 15285284bacSMike Stump unsigned int flags = BLOCK_HAS_DESCRIPTOR; 15385284bacSMike Stump 1544446dcf0SMike Stump // We run this first so that we set BlockHasCopyDispose from the entire 1554446dcf0SMike Stump // block literal. 1564446dcf0SMike Stump // __invoke 1574446dcf0SMike Stump uint64_t subBlockSize, subBlockAlign; 1584446dcf0SMike Stump llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; 159*d2142cffSMike Stump bool subBlockHasCopyDispose = false; 1604446dcf0SMike Stump llvm::Function *Fn 161692c6e37SMike Stump = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl, LocalDeclMap, 1625469f29aSMike Stump subBlockSize, 1634446dcf0SMike Stump subBlockAlign, 1644446dcf0SMike Stump subBlockDeclRefDecls, 165*d2142cffSMike Stump subBlockHasCopyDispose); 166*d2142cffSMike Stump BlockHasCopyDispose |= subBlockHasCopyDispose; 1674446dcf0SMike Stump Elts[3] = Fn; 1684446dcf0SMike Stump 1694446dcf0SMike Stump if (!Enable__block && BlockHasCopyDispose) 1704446dcf0SMike Stump ErrorUnsupported(BE, "block literal that requires copy/dispose"); 1714446dcf0SMike Stump 172*d2142cffSMike Stump if (subBlockHasCopyDispose) 17385284bacSMike Stump flags |= BLOCK_HAS_COPY_DISPOSE; 17485284bacSMike Stump 175499ae7ecSMike Stump // __isa 1767ab278dbSMike Stump C = CGM.getNSConcreteStackBlock(); 17785284bacSMike Stump C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty); 1784446dcf0SMike Stump Elts[0] = C; 17985284bacSMike Stump 18085284bacSMike Stump // __flags 18185284bacSMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 18285284bacSMike Stump CGM.getTypes().ConvertType(CGM.getContext().IntTy)); 18385284bacSMike Stump C = llvm::ConstantInt::get(IntTy, flags); 1844446dcf0SMike Stump Elts[1] = C; 18585284bacSMike Stump 18685284bacSMike Stump // __reserved 18785284bacSMike Stump C = llvm::ConstantInt::get(IntTy, 0); 1884446dcf0SMike Stump Elts[2] = C; 18985284bacSMike Stump 190b750d928SMike Stump if (subBlockDeclRefDecls.size() == 0) { 191d6ef62f0SMike Stump // __descriptor 192*d2142cffSMike Stump assert(subBlockHasCopyDispose == false); 193*d2142cffSMike Stump Elts[4] = BuildDescriptorBlockDecl(subBlockHasCopyDispose, subBlockSize, 0, 0); 194d6ef62f0SMike Stump 1953a139f37SMike Stump // Optimize to being a global block. 1963a139f37SMike Stump Elts[0] = CGM.getNSConcreteGlobalBlock(); 1973a139f37SMike Stump Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL); 1983a139f37SMike Stump 19985284bacSMike Stump C = llvm::ConstantStruct::get(Elts); 20085284bacSMike Stump 20185284bacSMike Stump char Name[32]; 2029319db8aSMike Stump sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount()); 20385284bacSMike Stump C = new llvm::GlobalVariable(C->getType(), true, 20485284bacSMike Stump llvm::GlobalValue::InternalLinkage, 20585284bacSMike Stump C, Name, &CGM.getModule()); 2065d2534adSMike Stump QualType BPT = BE->getType(); 2075d2534adSMike Stump C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT)); 20885284bacSMike Stump return C; 20985284bacSMike Stump } 21085284bacSMike Stump 211b750d928SMike Stump std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size()); 212aeb0ffd5SMike Stump for (int i=0; i<4; ++i) 213b750d928SMike Stump Types[i] = Elts[i]->getType(); 214aeb0ffd5SMike Stump Types[4] = PtrToInt8Ty; 21585284bacSMike Stump 2161db7d04bSMike Stump for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) { 2171db7d04bSMike Stump const Expr *E = subBlockDeclRefDecls[i]; 2181db7d04bSMike Stump const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); 2191db7d04bSMike Stump QualType Ty = E->getType(); 22097d01d50SMike Stump if (BDRE && BDRE->isByRef()) { 22197d01d50SMike Stump uint64_t Align = getContext().getDeclAlignInBytes(BDRE->getDecl()); 22297d01d50SMike Stump Types[i+5] = llvm::PointerType::get(BuildByRefType(Ty, Align), 0); 22397d01d50SMike Stump } else 2241db7d04bSMike Stump Types[i+5] = ConvertType(Ty); 2251db7d04bSMike Stump } 226b750d928SMike Stump 227aeb0ffd5SMike Stump llvm::StructType *Ty = llvm::StructType::get(Types, true); 228b750d928SMike Stump 229b750d928SMike Stump llvm::AllocaInst *A = CreateTempAlloca(Ty); 230b750d928SMike Stump A->setAlignment(subBlockAlign); 231b750d928SMike Stump V = A; 232b750d928SMike Stump 233aeb0ffd5SMike Stump std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size()); 234aeb0ffd5SMike Stump int helpersize = 0; 235aeb0ffd5SMike Stump 236aeb0ffd5SMike Stump for (unsigned i=0; i<4; ++i) 237b750d928SMike Stump Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp")); 238b750d928SMike Stump 239b750d928SMike Stump for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) 240b750d928SMike Stump { 2411db7d04bSMike Stump // FIXME: Push const down. 2421db7d04bSMike Stump Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]); 2431db7d04bSMike Stump DeclRefExpr *DR; 2441db7d04bSMike Stump ValueDecl *VD; 245b750d928SMike Stump 2461db7d04bSMike Stump DR = dyn_cast<DeclRefExpr>(E); 2471db7d04bSMike Stump // Skip padding. 2481db7d04bSMike Stump if (DR) continue; 2491db7d04bSMike Stump 2501db7d04bSMike Stump BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); 2511db7d04bSMike Stump VD = BDRE->getDecl(); 2521db7d04bSMike Stump 25397d01d50SMike Stump llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp"); 254aeb0ffd5SMike Stump NoteForHelper[helpersize].index = i+5; 255aeb0ffd5SMike Stump NoteForHelper[helpersize].RequiresCopying = BlockRequiresCopying(VD->getType()); 256aeb0ffd5SMike Stump NoteForHelper[helpersize].flag 257aeb0ffd5SMike Stump = VD->getType()->isBlockPointerType() ? BLOCK_FIELD_IS_BLOCK : BLOCK_FIELD_IS_OBJECT; 258aeb0ffd5SMike Stump 2591db7d04bSMike Stump if (LocalDeclMap[VD]) { 26097d01d50SMike Stump if (BDRE->isByRef()) { 261aeb0ffd5SMike Stump NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF | 26290d8daf5SMike Stump // FIXME: Someone double check this. 26390d8daf5SMike Stump (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0); 26497d01d50SMike Stump const llvm::Type *Ty = Types[i+5]; 26597d01d50SMike Stump llvm::Value *Loc = LocalDeclMap[VD]; 26697d01d50SMike Stump Loc = Builder.CreateStructGEP(Loc, 1, "forwarding"); 26797d01d50SMike Stump Loc = Builder.CreateLoad(Loc, false); 26897d01d50SMike Stump Loc = Builder.CreateBitCast(Loc, Ty); 26997d01d50SMike Stump Builder.CreateStore(Loc, Addr); 270aeb0ffd5SMike Stump ++helpersize; 27197d01d50SMike Stump continue; 27297d01d50SMike Stump } else 2731db7d04bSMike Stump E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD), 2741db7d04bSMike Stump VD->getType(), SourceLocation(), 2751db7d04bSMike Stump false, false); 2761db7d04bSMike Stump } 27797d01d50SMike Stump if (BDRE->isByRef()) { 278efd7caa8SMike Stump NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF | 279efd7caa8SMike Stump // FIXME: Someone double check this. 280efd7caa8SMike Stump (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0); 2811db7d04bSMike Stump E = new (getContext()) 2821db7d04bSMike Stump UnaryOperator(E, UnaryOperator::AddrOf, 2831db7d04bSMike Stump getContext().getPointerType(E->getType()), 2841db7d04bSMike Stump SourceLocation()); 28597d01d50SMike Stump } 286aeb0ffd5SMike Stump ++helpersize; 2871db7d04bSMike Stump 2881db7d04bSMike Stump RValue r = EmitAnyExpr(E, Addr, false); 28997d01d50SMike Stump if (r.isScalar()) { 29097d01d50SMike Stump llvm::Value *Loc = r.getScalarVal(); 29197d01d50SMike Stump const llvm::Type *Ty = Types[i+5]; 29297d01d50SMike Stump if (BDRE->isByRef()) { 2932d33d630SMike Stump // E is now the address of the value field, instead, we want the 2942d33d630SMike Stump // address of the actual ByRef struct. We optimize this slightly 2952d33d630SMike Stump // compared to gcc by not grabbing the forwarding slot as this must 2962d33d630SMike Stump // be done during Block_copy for us, and we can postpone the work 2972d33d630SMike Stump // until then. 2982d33d630SMike Stump uint64_t offset = BlockDecls[BDRE->getDecl()]; 2992d33d630SMike Stump 3002d33d630SMike Stump llvm::Value *BlockLiteral = LoadBlockStruct(); 3012d33d630SMike Stump 3022d33d630SMike Stump Loc = Builder.CreateGEP(BlockLiteral, 3032d33d630SMike Stump llvm::ConstantInt::get(llvm::Type::Int64Ty, 3042d33d630SMike Stump offset), 3052d33d630SMike Stump "block.literal"); 3062d33d630SMike Stump Ty = llvm::PointerType::get(Ty, 0); 30797d01d50SMike Stump Loc = Builder.CreateBitCast(Loc, Ty); 3082d33d630SMike Stump Loc = Builder.CreateLoad(Loc, false); 3092d33d630SMike Stump // Loc = Builder.CreateBitCast(Loc, Ty); 31097d01d50SMike Stump } 31197d01d50SMike Stump Builder.CreateStore(Loc, Addr); 31297d01d50SMike Stump } else if (r.isComplex()) 313b750d928SMike Stump // FIXME: implement 314b750d928SMike Stump ErrorUnsupported(BE, "complex in block literal"); 315b750d928SMike Stump else if (r.isAggregate()) 316b750d928SMike Stump ; // Already created into the destination 317b750d928SMike Stump else 318b750d928SMike Stump assert (0 && "bad block variable"); 319b750d928SMike Stump // FIXME: Ensure that the offset created by the backend for 320b750d928SMike Stump // the struct matches the previously computed offset in BlockDecls. 321b750d928SMike Stump } 322aeb0ffd5SMike Stump NoteForHelper.resize(helpersize); 323d6ef62f0SMike Stump 324d6ef62f0SMike Stump // __descriptor 325*d2142cffSMike Stump llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockHasCopyDispose, 326*d2142cffSMike Stump subBlockSize, Ty, 327aeb0ffd5SMike Stump &NoteForHelper); 328aeb0ffd5SMike Stump Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty); 329aeb0ffd5SMike Stump Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp")); 330b750d928SMike Stump } 331b750d928SMike Stump 332b750d928SMike Stump QualType BPT = BE->getType(); 333b750d928SMike Stump return Builder.CreateBitCast(V, ConvertType(BPT)); 334b750d928SMike Stump } 33585284bacSMike Stump 33685284bacSMike Stump 33795435674SMike Stump const llvm::Type *BlockModule::getBlockDescriptorType() { 338650c932dSMike Stump if (BlockDescriptorType) 339650c932dSMike Stump return BlockDescriptorType; 3402437cbfaSAnders Carlsson 3412437cbfaSAnders Carlsson const llvm::Type *UnsignedLongTy = 342650c932dSMike Stump getTypes().ConvertType(getContext().UnsignedLongTy); 3432437cbfaSAnders Carlsson 3442437cbfaSAnders Carlsson // struct __block_descriptor { 3452437cbfaSAnders Carlsson // unsigned long reserved; 3462437cbfaSAnders Carlsson // unsigned long block_size; 3472437cbfaSAnders Carlsson // }; 348650c932dSMike Stump BlockDescriptorType = llvm::StructType::get(UnsignedLongTy, 3492437cbfaSAnders Carlsson UnsignedLongTy, 3502437cbfaSAnders Carlsson NULL); 3512437cbfaSAnders Carlsson 352650c932dSMike Stump getModule().addTypeName("struct.__block_descriptor", 353650c932dSMike Stump BlockDescriptorType); 3542437cbfaSAnders Carlsson 355650c932dSMike Stump return BlockDescriptorType; 3562437cbfaSAnders Carlsson } 3572437cbfaSAnders Carlsson 35895435674SMike Stump const llvm::Type *BlockModule::getGenericBlockLiteralType() { 359005c9a62SMike Stump if (GenericBlockLiteralType) 360005c9a62SMike Stump return GenericBlockLiteralType; 3612437cbfaSAnders Carlsson 3622437cbfaSAnders Carlsson const llvm::Type *BlockDescPtrTy = 363005c9a62SMike Stump llvm::PointerType::getUnqual(getBlockDescriptorType()); 3642437cbfaSAnders Carlsson 36592bbd6d4SMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 36692bbd6d4SMike Stump getTypes().ConvertType(getContext().IntTy)); 36792bbd6d4SMike Stump 3682437cbfaSAnders Carlsson // 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; 3742437cbfaSAnders Carlsson // }; 375626aecc4SMike Stump GenericBlockLiteralType = llvm::StructType::get(PtrToInt8Ty, 37692bbd6d4SMike Stump IntTy, 37792bbd6d4SMike Stump IntTy, 378626aecc4SMike Stump PtrToInt8Ty, 3792437cbfaSAnders Carlsson BlockDescPtrTy, 3802437cbfaSAnders Carlsson NULL); 3812437cbfaSAnders Carlsson 382005c9a62SMike Stump getModule().addTypeName("struct.__block_literal_generic", 383005c9a62SMike Stump GenericBlockLiteralType); 3842437cbfaSAnders Carlsson 385005c9a62SMike Stump return GenericBlockLiteralType; 3862437cbfaSAnders Carlsson } 3872437cbfaSAnders Carlsson 38895435674SMike Stump const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() { 3895d2534adSMike Stump if (GenericExtendedBlockLiteralType) 3905d2534adSMike Stump return GenericExtendedBlockLiteralType; 3915d2534adSMike Stump 3925d2534adSMike Stump const llvm::Type *BlockDescPtrTy = 3935d2534adSMike Stump llvm::PointerType::getUnqual(getBlockDescriptorType()); 3945d2534adSMike Stump 3955d2534adSMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 3965d2534adSMike Stump getTypes().ConvertType(getContext().IntTy)); 3975d2534adSMike Stump 3985d2534adSMike Stump // struct __block_literal_generic { 3995d2534adSMike Stump // void *__isa; 4005d2534adSMike Stump // int __flags; 4015d2534adSMike Stump // int __reserved; 4025d2534adSMike Stump // void (*__invoke)(void *); 4035d2534adSMike Stump // struct __block_descriptor *__descriptor; 4045d2534adSMike Stump // void *__copy_func_helper_decl; 4055d2534adSMike Stump // void *__destroy_func_decl; 4065d2534adSMike Stump // }; 407626aecc4SMike Stump GenericExtendedBlockLiteralType = llvm::StructType::get(PtrToInt8Ty, 4085d2534adSMike Stump IntTy, 4095d2534adSMike Stump IntTy, 410626aecc4SMike Stump PtrToInt8Ty, 4115d2534adSMike Stump BlockDescPtrTy, 412626aecc4SMike Stump PtrToInt8Ty, 413626aecc4SMike Stump PtrToInt8Ty, 4145d2534adSMike Stump NULL); 4155d2534adSMike Stump 4165d2534adSMike Stump getModule().addTypeName("struct.__block_literal_extended_generic", 4175d2534adSMike Stump GenericExtendedBlockLiteralType); 4185d2534adSMike Stump 4195d2534adSMike Stump return GenericExtendedBlockLiteralType; 4205d2534adSMike Stump } 4215d2534adSMike Stump 4222437cbfaSAnders Carlsson /// getBlockFunctionType - Given a BlockPointerType, will return the 4232437cbfaSAnders Carlsson /// function type for the block, including the first block literal argument. 4242437cbfaSAnders Carlsson static QualType getBlockFunctionType(ASTContext &Ctx, 4256a60fa24SAnders Carlsson const BlockPointerType *BPT) { 426deaad8ccSDouglas Gregor const FunctionProtoType *FTy = cast<FunctionProtoType>(BPT->getPointeeType()); 4272437cbfaSAnders Carlsson 4282437cbfaSAnders Carlsson llvm::SmallVector<QualType, 8> Types; 4292437cbfaSAnders Carlsson Types.push_back(Ctx.getPointerType(Ctx.VoidTy)); 4302437cbfaSAnders Carlsson 431deaad8ccSDouglas Gregor for (FunctionProtoType::arg_type_iterator i = FTy->arg_type_begin(), 4322437cbfaSAnders Carlsson e = FTy->arg_type_end(); i != e; ++i) 4332437cbfaSAnders Carlsson Types.push_back(*i); 4342437cbfaSAnders Carlsson 4352437cbfaSAnders Carlsson return Ctx.getFunctionType(FTy->getResultType(), 4362437cbfaSAnders Carlsson &Types[0], Types.size(), 4372437cbfaSAnders Carlsson FTy->isVariadic(), 0); 4382437cbfaSAnders Carlsson } 4392437cbfaSAnders Carlsson 4406a60fa24SAnders Carlsson RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) { 4412437cbfaSAnders Carlsson const BlockPointerType *BPT = 4422437cbfaSAnders Carlsson E->getCallee()->getType()->getAsBlockPointerType(); 4432437cbfaSAnders Carlsson 4442437cbfaSAnders Carlsson llvm::Value *Callee = EmitScalarExpr(E->getCallee()); 4452437cbfaSAnders Carlsson 4462437cbfaSAnders Carlsson // Get a pointer to the generic block literal. 4472437cbfaSAnders Carlsson const llvm::Type *BlockLiteralTy = 448005c9a62SMike Stump llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType()); 4492437cbfaSAnders Carlsson 4502437cbfaSAnders Carlsson // Bitcast the callee to a block literal. 4512437cbfaSAnders Carlsson llvm::Value *BlockLiteral = 4522437cbfaSAnders Carlsson Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal"); 4532437cbfaSAnders Carlsson 4542437cbfaSAnders Carlsson // Get the function pointer from the literal. 4552437cbfaSAnders Carlsson llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp"); 456624497c2SMike Stump llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp"); 4572437cbfaSAnders Carlsson 4582437cbfaSAnders Carlsson // Cast the function pointer to the right type. 4592437cbfaSAnders Carlsson const llvm::Type *BlockFTy = 4602437cbfaSAnders Carlsson ConvertType(getBlockFunctionType(getContext(), BPT)); 4612437cbfaSAnders Carlsson const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy); 4622437cbfaSAnders Carlsson Func = Builder.CreateBitCast(Func, BlockFTyPtr); 4632437cbfaSAnders Carlsson 4642437cbfaSAnders Carlsson BlockLiteral = 4652437cbfaSAnders Carlsson Builder.CreateBitCast(BlockLiteral, 4662437cbfaSAnders Carlsson llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4672437cbfaSAnders Carlsson "tmp"); 4682437cbfaSAnders Carlsson 4692437cbfaSAnders Carlsson // Add the block literal. 4702437cbfaSAnders Carlsson QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy); 4712437cbfaSAnders Carlsson CallArgList Args; 4722437cbfaSAnders Carlsson Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy)); 4732437cbfaSAnders Carlsson 4742437cbfaSAnders Carlsson // And the rest of the arguments. 4752437cbfaSAnders Carlsson for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end(); 4762437cbfaSAnders Carlsson i != e; ++i) 4772437cbfaSAnders Carlsson Args.push_back(std::make_pair(EmitAnyExprToTemp(*i), 4782437cbfaSAnders Carlsson i->getType())); 4792437cbfaSAnders Carlsson 4802437cbfaSAnders Carlsson // And call the block. 4812437cbfaSAnders Carlsson return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args), 4822437cbfaSAnders Carlsson Func, Args); 4832437cbfaSAnders Carlsson } 4846a60fa24SAnders Carlsson 48597d01d50SMike Stump llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) { 48697d01d50SMike Stump uint64_t &offset = BlockDecls[E->getDecl()]; 48797d01d50SMike Stump 48897d01d50SMike Stump const llvm::Type *Ty; 48997d01d50SMike Stump Ty = CGM.getTypes().ConvertType(E->getDecl()->getType()); 49097d01d50SMike Stump 4910874160bSMike Stump if (!Enable__block && E->isByRef()) 49297d01d50SMike Stump ErrorUnsupported(E, "__block variable in block literal"); 4930c743277SMike Stump else if (!Enable__block && E->getType()->isBlockPointerType()) 49497d01d50SMike Stump ErrorUnsupported(E, "block pointer in block literal"); 495d9728222SMike Stump else if (!Enable__block && (E->getDecl()->getAttr<ObjCNSObjectAttr>() || 496d9728222SMike Stump getContext().isObjCNSObjectType(E->getType()))) 49797d01d50SMike Stump ErrorUnsupported(E, "__attribute__((NSObject)) variable in block " 49897d01d50SMike Stump "literal"); 4990c743277SMike Stump else if (!Enable__block && getContext().isObjCObjectPointerType(E->getType())) 50097d01d50SMike Stump ErrorUnsupported(E, "Objective-C variable in block literal"); 50197d01d50SMike Stump 50297d01d50SMike Stump // See if we have already allocated an offset for this variable. 50397d01d50SMike Stump if (offset == 0) { 5044446dcf0SMike Stump // Don't run the expensive check, unless we have to. 5054446dcf0SMike Stump if (!BlockHasCopyDispose && BlockRequiresCopying(E->getType())) 5064446dcf0SMike Stump BlockHasCopyDispose = true; 50797d01d50SMike Stump // if not, allocate one now. 50897d01d50SMike Stump offset = getBlockOffset(E); 50997d01d50SMike Stump } 51097d01d50SMike Stump 51197d01d50SMike Stump llvm::Value *BlockLiteral = LoadBlockStruct(); 51297d01d50SMike Stump llvm::Value *V = Builder.CreateGEP(BlockLiteral, 51397d01d50SMike Stump llvm::ConstantInt::get(llvm::Type::Int64Ty, 51497d01d50SMike Stump offset), 5152d33d630SMike Stump "block.literal"); 51697d01d50SMike Stump if (E->isByRef()) { 51797d01d50SMike Stump bool needsCopyDispose = BlockRequiresCopying(E->getType()); 51897d01d50SMike Stump uint64_t Align = getContext().getDeclAlignInBytes(E->getDecl()); 51997d01d50SMike Stump const llvm::Type *PtrStructTy 52097d01d50SMike Stump = llvm::PointerType::get(BuildByRefType(E->getType(), Align), 0); 521efd7caa8SMike Stump // The block literal will need a copy/destroy helper. 522efd7caa8SMike Stump BlockHasCopyDispose = true; 52397d01d50SMike Stump Ty = PtrStructTy; 52497d01d50SMike Stump Ty = llvm::PointerType::get(Ty, 0); 52597d01d50SMike Stump V = Builder.CreateBitCast(V, Ty); 52697d01d50SMike Stump V = Builder.CreateLoad(V, false); 52797d01d50SMike Stump V = Builder.CreateStructGEP(V, 1, "forwarding"); 52897d01d50SMike Stump V = Builder.CreateLoad(V, false); 52997d01d50SMike Stump V = Builder.CreateBitCast(V, PtrStructTy); 53097d01d50SMike Stump V = Builder.CreateStructGEP(V, needsCopyDispose*2 + 4, "x"); 53197d01d50SMike Stump } else { 53297d01d50SMike Stump Ty = llvm::PointerType::get(Ty, 0); 53397d01d50SMike Stump V = Builder.CreateBitCast(V, Ty); 53497d01d50SMike Stump } 53597d01d50SMike Stump return V; 53697d01d50SMike Stump } 53797d01d50SMike Stump 538692c6e37SMike Stump void CodeGenFunction::BlockForwardSelf() { 539692c6e37SMike Stump const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); 540692c6e37SMike Stump ImplicitParamDecl *SelfDecl = OMD->getSelfDecl(); 541692c6e37SMike Stump llvm::Value *&DMEntry = LocalDeclMap[SelfDecl]; 542692c6e37SMike Stump if (DMEntry) 543692c6e37SMike Stump return; 544692c6e37SMike Stump // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care 545692c6e37SMike Stump BlockDeclRefExpr *BDRE = new (getContext()) 546692c6e37SMike Stump BlockDeclRefExpr(SelfDecl, 547692c6e37SMike Stump SelfDecl->getType(), SourceLocation(), false); 548692c6e37SMike Stump DMEntry = GetAddrOfBlockDecl(BDRE); 549692c6e37SMike Stump } 550692c6e37SMike Stump 5512d5a2878SMike Stump llvm::Constant * 5526c39666aSMike Stump BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) { 5536a60fa24SAnders Carlsson // Generate the block descriptor. 5546a60fa24SAnders Carlsson const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy); 55592bbd6d4SMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 55692bbd6d4SMike Stump getTypes().ConvertType(getContext().IntTy)); 5576a60fa24SAnders Carlsson 5586a60fa24SAnders Carlsson llvm::Constant *DescriptorFields[2]; 5596a60fa24SAnders Carlsson 5606a60fa24SAnders Carlsson // Reserved 5616a60fa24SAnders Carlsson DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy); 5626a60fa24SAnders Carlsson 5636a60fa24SAnders Carlsson // Block literal size. For global blocks we just use the size of the generic 5646a60fa24SAnders Carlsson // block literal struct. 5656a60fa24SAnders Carlsson uint64_t BlockLiteralSize = 566005c9a62SMike Stump TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8; 5676a60fa24SAnders Carlsson DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize); 5686a60fa24SAnders Carlsson 5696a60fa24SAnders Carlsson llvm::Constant *DescriptorStruct = 5706a60fa24SAnders Carlsson llvm::ConstantStruct::get(&DescriptorFields[0], 2); 5716a60fa24SAnders Carlsson 5726a60fa24SAnders Carlsson llvm::GlobalVariable *Descriptor = 5736a60fa24SAnders Carlsson new llvm::GlobalVariable(DescriptorStruct->getType(), true, 5746a60fa24SAnders Carlsson llvm::GlobalVariable::InternalLinkage, 5756a60fa24SAnders Carlsson DescriptorStruct, "__block_descriptor_global", 5766a60fa24SAnders Carlsson &getModule()); 5776a60fa24SAnders Carlsson 5786a60fa24SAnders Carlsson // Generate the constants for the block literal. 5796a60fa24SAnders Carlsson llvm::Constant *LiteralFields[5]; 5806a60fa24SAnders Carlsson 5812d5a2878SMike Stump CodeGenFunction::BlockInfo Info(0, n); 582b750d928SMike Stump uint64_t subBlockSize, subBlockAlign; 5831db7d04bSMike Stump llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; 584a61bb4d1SDaniel Dunbar bool subBlockHasCopyDispose = false; 5855469f29aSMike Stump llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap; 586cb2fbcb0SMike Stump llvm::Function *Fn 587692c6e37SMike Stump = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap, 5885469f29aSMike Stump subBlockSize, 589b750d928SMike Stump subBlockAlign, 5904446dcf0SMike Stump subBlockDeclRefDecls, 5914446dcf0SMike Stump subBlockHasCopyDispose); 592cb2fbcb0SMike Stump assert(subBlockSize == BlockLiteralSize 593cb2fbcb0SMike Stump && "no imports allowed for global block"); 594a61bb4d1SDaniel Dunbar assert(!subBlockHasCopyDispose && "no imports allowed for global block"); 5956a60fa24SAnders Carlsson 5966a60fa24SAnders Carlsson // isa 597971f9b66SMike Stump LiteralFields[0] = getNSConcreteGlobalBlock(); 5986a60fa24SAnders Carlsson 5996a60fa24SAnders Carlsson // Flags 6005878c797SAnders Carlsson LiteralFields[1] = 6015878c797SAnders Carlsson llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR); 6026a60fa24SAnders Carlsson 6036a60fa24SAnders Carlsson // Reserved 60492bbd6d4SMike Stump LiteralFields[2] = llvm::Constant::getNullValue(IntTy); 6056a60fa24SAnders Carlsson 6066a60fa24SAnders Carlsson // Function 6076a60fa24SAnders Carlsson LiteralFields[3] = Fn; 6086a60fa24SAnders Carlsson 6096a60fa24SAnders Carlsson // Descriptor 6106a60fa24SAnders Carlsson LiteralFields[4] = Descriptor; 6116a60fa24SAnders Carlsson 6126a60fa24SAnders Carlsson llvm::Constant *BlockLiteralStruct = 6136a60fa24SAnders Carlsson llvm::ConstantStruct::get(&LiteralFields[0], 5); 6146a60fa24SAnders Carlsson 6156a60fa24SAnders Carlsson llvm::GlobalVariable *BlockLiteral = 6166a60fa24SAnders Carlsson new llvm::GlobalVariable(BlockLiteralStruct->getType(), true, 6176a60fa24SAnders Carlsson llvm::GlobalVariable::InternalLinkage, 6186a60fa24SAnders Carlsson BlockLiteralStruct, "__block_literal_global", 6196a60fa24SAnders Carlsson &getModule()); 6206a60fa24SAnders Carlsson 6216a60fa24SAnders Carlsson return BlockLiteral; 6226a60fa24SAnders Carlsson } 6236a60fa24SAnders Carlsson 624cb2fbcb0SMike Stump llvm::Value *CodeGenFunction::LoadBlockStruct() { 625cb2fbcb0SMike Stump return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self"); 626cb2fbcb0SMike Stump } 627cb2fbcb0SMike Stump 6284446dcf0SMike Stump llvm::Function * 6294446dcf0SMike Stump CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr, 630cb2fbcb0SMike Stump const BlockInfo& Info, 631692c6e37SMike Stump const Decl *OuterFuncDecl, 6325469f29aSMike Stump llvm::DenseMap<const Decl*, llvm::Value*> ldm, 633b750d928SMike Stump uint64_t &Size, 634b750d928SMike Stump uint64_t &Align, 6354446dcf0SMike Stump llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls, 6364446dcf0SMike Stump bool &subBlockHasCopyDispose) { 6375469f29aSMike Stump // Arrange for local static and local extern declarations to appear 6385469f29aSMike Stump // to be local to this function as well, as they are directly referenced 6395469f29aSMike Stump // in a block. 6405469f29aSMike Stump for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin(); 6415469f29aSMike Stump i != ldm.end(); 6425469f29aSMike Stump ++i) { 6435469f29aSMike Stump const VarDecl *VD = dyn_cast<VarDecl>(i->first); 6445469f29aSMike Stump 6455469f29aSMike Stump if (VD->getStorageClass() == VarDecl::Static 6465469f29aSMike Stump || VD->getStorageClass() == VarDecl::Extern) 6475469f29aSMike Stump LocalDeclMap[VD] = i->second; 6485469f29aSMike Stump } 6495469f29aSMike Stump 650deaad8ccSDouglas Gregor const FunctionProtoType *FTy = 6513385fe12SChris Lattner cast<FunctionProtoType>(BExpr->getFunctionType()); 6526a60fa24SAnders Carlsson 6536a60fa24SAnders Carlsson FunctionArgList Args; 6546a60fa24SAnders Carlsson 6553385fe12SChris Lattner const BlockDecl *BD = BExpr->getBlockDecl(); 6566a60fa24SAnders Carlsson 6576a60fa24SAnders Carlsson // FIXME: This leaks 6586a60fa24SAnders Carlsson ImplicitParamDecl *SelfDecl = 6596a60fa24SAnders Carlsson ImplicitParamDecl::Create(getContext(), 0, 6606a60fa24SAnders Carlsson SourceLocation(), 0, 6616a60fa24SAnders Carlsson getContext().getPointerType(getContext().VoidTy)); 6626a60fa24SAnders Carlsson 6636a60fa24SAnders Carlsson Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType())); 664cb2fbcb0SMike Stump BlockStructDecl = SelfDecl; 6656a60fa24SAnders Carlsson 666c4b30e59SSteve Naroff for (BlockDecl::param_const_iterator i = BD->param_begin(), 6676a60fa24SAnders Carlsson e = BD->param_end(); i != e; ++i) 668eecd39f2SMike Stump Args.push_back(std::make_pair(*i, (*i)->getType())); 6696a60fa24SAnders Carlsson 6706a60fa24SAnders Carlsson const CGFunctionInfo &FI = 6716a60fa24SAnders Carlsson CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args); 6726a60fa24SAnders Carlsson 6732d5a2878SMike Stump std::string Name = std::string("__") + Info.Name + "_block_invoke_"; 6746a60fa24SAnders Carlsson CodeGenTypes &Types = CGM.getTypes(); 6756a60fa24SAnders Carlsson const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic()); 6766a60fa24SAnders Carlsson 6776a60fa24SAnders Carlsson llvm::Function *Fn = 6786a60fa24SAnders Carlsson llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 6796a60fa24SAnders Carlsson Name, 6806a60fa24SAnders Carlsson &CGM.getModule()); 6816a60fa24SAnders Carlsson 6826a60fa24SAnders Carlsson StartFunction(BD, FTy->getResultType(), Fn, Args, 6833385fe12SChris Lattner BExpr->getBody()->getLocEnd()); 684692c6e37SMike Stump CurFuncDecl = OuterFuncDecl; 6853385fe12SChris Lattner EmitStmt(BExpr->getBody()); 6863385fe12SChris Lattner FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc()); 6876a60fa24SAnders Carlsson 688b750d928SMike Stump // The runtime needs a minimum alignment of a void *. 689b750d928SMike Stump uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 690b750d928SMike Stump BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign); 691b750d928SMike Stump 692cb2fbcb0SMike Stump Size = BlockOffset; 693b750d928SMike Stump Align = BlockAlign; 694b750d928SMike Stump subBlockDeclRefDecls = BlockDeclRefDecls; 6954446dcf0SMike Stump subBlockHasCopyDispose |= BlockHasCopyDispose; 6966a60fa24SAnders Carlsson return Fn; 6976a60fa24SAnders Carlsson } 6981db7d04bSMike Stump 699aeb0ffd5SMike Stump uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) { 7001db7d04bSMike Stump const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl()); 7011db7d04bSMike Stump 7021db7d04bSMike Stump uint64_t Size = getContext().getTypeSize(D->getType()) / 8; 7031db7d04bSMike Stump uint64_t Align = getContext().getDeclAlignInBytes(D); 7041db7d04bSMike Stump 7051db7d04bSMike Stump if (BDRE->isByRef()) { 7061db7d04bSMike Stump Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8; 7071db7d04bSMike Stump Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 7081db7d04bSMike Stump } 7091db7d04bSMike Stump 7101db7d04bSMike Stump assert ((Align > 0) && "alignment must be 1 byte or more"); 7111db7d04bSMike Stump 7121db7d04bSMike Stump uint64_t OldOffset = BlockOffset; 7131db7d04bSMike Stump 7141db7d04bSMike Stump // Ensure proper alignment, even if it means we have to have a gap 7151db7d04bSMike Stump BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align); 7161db7d04bSMike Stump BlockAlign = std::max(Align, BlockAlign); 7171db7d04bSMike Stump 7181db7d04bSMike Stump uint64_t Pad = BlockOffset - OldOffset; 7191db7d04bSMike Stump if (Pad) { 7201db7d04bSMike Stump llvm::ArrayType::get(llvm::Type::Int8Ty, Pad); 7211db7d04bSMike Stump QualType PadTy = getContext().getConstantArrayType(getContext().CharTy, 7221db7d04bSMike Stump llvm::APInt(32, Pad), 7231db7d04bSMike Stump ArrayType::Normal, 0); 7241db7d04bSMike Stump ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(), 7251db7d04bSMike Stump 0, QualType(PadTy), VarDecl::None, 7261db7d04bSMike Stump SourceLocation()); 7271db7d04bSMike Stump Expr *E; 7281db7d04bSMike Stump E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(), 7291db7d04bSMike Stump SourceLocation(), false, false); 7301db7d04bSMike Stump BlockDeclRefDecls.push_back(E); 7311db7d04bSMike Stump } 7321db7d04bSMike Stump BlockDeclRefDecls.push_back(BDRE); 7331db7d04bSMike Stump 7341db7d04bSMike Stump BlockOffset += Size; 7351db7d04bSMike Stump return BlockOffset-Size; 7361db7d04bSMike Stump } 73797d01d50SMike Stump 738aeb0ffd5SMike Stump llvm::Constant *BlockFunction:: 739aeb0ffd5SMike Stump GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T, 740aeb0ffd5SMike Stump std::vector<HelperInfo> &NoteForHelper) { 7410c743277SMike Stump QualType R = getContext().VoidTy; 7420c743277SMike Stump 7430c743277SMike Stump FunctionArgList Args; 7440c743277SMike Stump // FIXME: This leaks 745aeb0ffd5SMike Stump ImplicitParamDecl *Dst = 746aeb0ffd5SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 747aeb0ffd5SMike Stump getContext().getPointerType(getContext().VoidTy)); 748aeb0ffd5SMike Stump Args.push_back(std::make_pair(Dst, Dst->getType())); 7490c743277SMike Stump ImplicitParamDecl *Src = 7500c743277SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 7510c743277SMike Stump getContext().getPointerType(getContext().VoidTy)); 7520c743277SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 7530c743277SMike Stump 7540c743277SMike Stump const CGFunctionInfo &FI = 7550c743277SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 7560c743277SMike Stump 7570c743277SMike Stump std::string Name = std::string("__copy_helper_block_"); 7580c743277SMike Stump CodeGenTypes &Types = CGM.getTypes(); 7590c743277SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 7600c743277SMike Stump 7610c743277SMike Stump llvm::Function *Fn = 7620c743277SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 7630c743277SMike Stump Name, 7640c743277SMike Stump &CGM.getModule()); 7650c743277SMike Stump 7660c743277SMike Stump IdentifierInfo *II 7670c743277SMike Stump = &CGM.getContext().Idents.get("__copy_helper_block_"); 7680c743277SMike Stump 7690c743277SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 7700c743277SMike Stump getContext().getTranslationUnitDecl(), 7710c743277SMike Stump SourceLocation(), II, R, 7720c743277SMike Stump FunctionDecl::Static, false, 7730c743277SMike Stump true); 7740c743277SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 775aeb0ffd5SMike Stump 776aeb0ffd5SMike Stump llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); 777aeb0ffd5SMike Stump llvm::Type *PtrPtrT; 778aeb0ffd5SMike Stump PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 779aeb0ffd5SMike Stump SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); 780aeb0ffd5SMike Stump SrcObj = Builder.CreateLoad(SrcObj); 781aeb0ffd5SMike Stump 782aeb0ffd5SMike Stump llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst); 783aeb0ffd5SMike Stump DstObj = Builder.CreateBitCast(DstObj, llvm::PointerType::get(T, 0)); 784aeb0ffd5SMike Stump 785aeb0ffd5SMike Stump for (unsigned i=0; i < NoteForHelper.size(); ++i) { 786aeb0ffd5SMike Stump int flag = NoteForHelper[i].flag; 787aeb0ffd5SMike Stump int index = NoteForHelper[i].index; 788aeb0ffd5SMike Stump 789aeb0ffd5SMike Stump if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) 790aeb0ffd5SMike Stump || NoteForHelper[i].RequiresCopying) { 791aeb0ffd5SMike Stump llvm::Value *Srcv = SrcObj; 792aeb0ffd5SMike Stump Srcv = Builder.CreateStructGEP(Srcv, index); 793aeb0ffd5SMike Stump Srcv = Builder.CreateBitCast(Srcv, 794aeb0ffd5SMike Stump llvm::PointerType::get(PtrToInt8Ty, 0)); 795aeb0ffd5SMike Stump Srcv = Builder.CreateLoad(Srcv); 796aeb0ffd5SMike Stump 797aeb0ffd5SMike Stump llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index); 798aeb0ffd5SMike Stump Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty); 799aeb0ffd5SMike Stump 800aeb0ffd5SMike Stump llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag); 801aeb0ffd5SMike Stump llvm::Value *F = getBlockObjectAssign(); 802aeb0ffd5SMike Stump Builder.CreateCall3(F, Dstv, Srcv, N); 803aeb0ffd5SMike Stump } 804aeb0ffd5SMike Stump } 805aeb0ffd5SMike Stump 8060c743277SMike Stump CGF.FinishFunction(); 8070c743277SMike Stump 8080c743277SMike Stump return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 80997d01d50SMike Stump } 81097d01d50SMike Stump 811d6ef62f0SMike Stump llvm::Constant *BlockFunction:: 812aeb0ffd5SMike Stump GenerateDestroyHelperFunction(bool BlockHasCopyDispose, 813aeb0ffd5SMike Stump const llvm::StructType* T, 814aeb0ffd5SMike Stump std::vector<HelperInfo> &NoteForHelper) { 8150c743277SMike Stump QualType R = getContext().VoidTy; 8160c743277SMike Stump 8170c743277SMike Stump FunctionArgList Args; 8180c743277SMike Stump // FIXME: This leaks 8190c743277SMike Stump ImplicitParamDecl *Src = 8200c743277SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 8210c743277SMike Stump getContext().getPointerType(getContext().VoidTy)); 8220c743277SMike Stump 8230c743277SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 8240c743277SMike Stump 8250c743277SMike Stump const CGFunctionInfo &FI = 8260c743277SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 8270c743277SMike Stump 8280c743277SMike Stump std::string Name = std::string("__destroy_helper_block_"); 8290c743277SMike Stump CodeGenTypes &Types = CGM.getTypes(); 8300c743277SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 8310c743277SMike Stump 8320c743277SMike Stump llvm::Function *Fn = 8330c743277SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 8340c743277SMike Stump Name, 8350c743277SMike Stump &CGM.getModule()); 8360c743277SMike Stump 8370c743277SMike Stump IdentifierInfo *II 8380c743277SMike Stump = &CGM.getContext().Idents.get("__destroy_helper_block_"); 8390c743277SMike Stump 8400c743277SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 8410c743277SMike Stump getContext().getTranslationUnitDecl(), 8420c743277SMike Stump SourceLocation(), II, R, 8430c743277SMike Stump FunctionDecl::Static, false, 8440c743277SMike Stump true); 8450c743277SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 8466f7d9f87SMike Stump 8476f7d9f87SMike Stump llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); 8486f7d9f87SMike Stump llvm::Type *PtrPtrT; 8496f7d9f87SMike Stump PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 8506f7d9f87SMike Stump SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); 8516f7d9f87SMike Stump SrcObj = Builder.CreateLoad(SrcObj); 8526f7d9f87SMike Stump 8536f7d9f87SMike Stump for (unsigned i=0; i < NoteForHelper.size(); ++i) { 8546f7d9f87SMike Stump int flag = NoteForHelper[i].flag; 8556f7d9f87SMike Stump int index = NoteForHelper[i].index; 8566f7d9f87SMike Stump 8576f7d9f87SMike Stump if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) 8586f7d9f87SMike Stump || NoteForHelper[i].RequiresCopying) { 8596f7d9f87SMike Stump llvm::Value *Srcv = SrcObj; 8606f7d9f87SMike Stump Srcv = Builder.CreateStructGEP(Srcv, index); 8616f7d9f87SMike Stump Srcv = Builder.CreateBitCast(Srcv, 8626f7d9f87SMike Stump llvm::PointerType::get(PtrToInt8Ty, 0)); 8636f7d9f87SMike Stump Srcv = Builder.CreateLoad(Srcv); 8646f7d9f87SMike Stump 8656f7d9f87SMike Stump BuildBlockRelease(Srcv, flag); 8666f7d9f87SMike Stump } 8676f7d9f87SMike Stump } 8686f7d9f87SMike Stump 8690c743277SMike Stump CGF.FinishFunction(); 8700c743277SMike Stump 8710c743277SMike Stump return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 8720c743277SMike Stump } 8730c743277SMike Stump 874aeb0ffd5SMike Stump llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T, 875aeb0ffd5SMike Stump std::vector<HelperInfo> &NoteForHelper) { 876aeb0ffd5SMike Stump return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose, 877aeb0ffd5SMike Stump T, NoteForHelper); 8780c743277SMike Stump } 8790c743277SMike Stump 880aeb0ffd5SMike Stump llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T, 881aeb0ffd5SMike Stump std::vector<HelperInfo> &NoteForHelper) { 882aeb0ffd5SMike Stump return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose, 883aeb0ffd5SMike Stump T, NoteForHelper); 88497d01d50SMike Stump } 885626aecc4SMike Stump 886f89230d9SMike Stump llvm::Constant *BlockFunction:: 887f89230d9SMike Stump GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) { 888ee2a5ee5SMike Stump QualType R = getContext().VoidTy; 889ee2a5ee5SMike Stump 890ee2a5ee5SMike Stump FunctionArgList Args; 891ee2a5ee5SMike Stump // FIXME: This leaks 892f89230d9SMike Stump ImplicitParamDecl *Dst = 893f89230d9SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 894f89230d9SMike Stump getContext().getPointerType(getContext().VoidTy)); 895f89230d9SMike Stump Args.push_back(std::make_pair(Dst, Dst->getType())); 896f89230d9SMike Stump 897f89230d9SMike Stump // FIXME: This leaks 898ee2a5ee5SMike Stump ImplicitParamDecl *Src = 899ee2a5ee5SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 900ee2a5ee5SMike Stump getContext().getPointerType(getContext().VoidTy)); 901ee2a5ee5SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 902ee2a5ee5SMike Stump 903ee2a5ee5SMike Stump const CGFunctionInfo &FI = 904ee2a5ee5SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 905ee2a5ee5SMike Stump 906ee2a5ee5SMike Stump std::string Name = std::string("__Block_byref_id_object_copy_"); 907ee2a5ee5SMike Stump CodeGenTypes &Types = CGM.getTypes(); 908ee2a5ee5SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 909ee2a5ee5SMike Stump 910ee2a5ee5SMike Stump llvm::Function *Fn = 911ee2a5ee5SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 912ee2a5ee5SMike Stump Name, 913ee2a5ee5SMike Stump &CGM.getModule()); 914ee2a5ee5SMike Stump 915ee2a5ee5SMike Stump IdentifierInfo *II 916ee2a5ee5SMike Stump = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_"); 917ee2a5ee5SMike Stump 918ee2a5ee5SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 919ee2a5ee5SMike Stump getContext().getTranslationUnitDecl(), 920ee2a5ee5SMike Stump SourceLocation(), II, R, 921ee2a5ee5SMike Stump FunctionDecl::Static, false, 922ee2a5ee5SMike Stump true); 923ee2a5ee5SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 924f89230d9SMike Stump 925f89230d9SMike Stump // dst->x 926f89230d9SMike Stump llvm::Value *V = CGF.GetAddrOfLocalVar(Dst); 927f89230d9SMike Stump V = Builder.CreateBitCast(V, T); 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"); 936f89230d9SMike Stump 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 941f89230d9SMike Stump llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag); 942f89230d9SMike Stump llvm::Value *F = getBlockObjectAssign(); 943f89230d9SMike Stump Builder.CreateCall3(F, DstObj, SrcObj, N); 944f89230d9SMike Stump 945ee2a5ee5SMike Stump CGF.FinishFunction(); 946ee2a5ee5SMike Stump 947ee2a5ee5SMike Stump 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 970ee2a5ee5SMike Stump llvm::Function *Fn = 971ee2a5ee5SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 972ee2a5ee5SMike Stump Name, 973ee2a5ee5SMike Stump &CGM.getModule()); 974ee2a5ee5SMike Stump 975ee2a5ee5SMike Stump IdentifierInfo *II 976ee2a5ee5SMike Stump = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_"); 977ee2a5ee5SMike Stump 978ee2a5ee5SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 979ee2a5ee5SMike Stump getContext().getTranslationUnitDecl(), 980ee2a5ee5SMike Stump SourceLocation(), II, R, 981ee2a5ee5SMike Stump FunctionDecl::Static, false, 982ee2a5ee5SMike Stump true); 983ee2a5ee5SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 984fbe25dd1SMike Stump 985fbe25dd1SMike Stump llvm::Value *V = CGF.GetAddrOfLocalVar(Src); 986fbe25dd1SMike Stump V = Builder.CreateBitCast(V, T); 987fbe25dd1SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 988fbe25dd1SMike Stump V = Builder.CreateBitCast(V, PtrToInt8Ty); 989fbe25dd1SMike Stump 990fbe25dd1SMike Stump flag |= BLOCK_BYREF_CALLER; 991fbe25dd1SMike Stump BuildBlockRelease(V, flag); 992ee2a5ee5SMike Stump CGF.FinishFunction(); 993ee2a5ee5SMike Stump 994ee2a5ee5SMike Stump return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 995ee2a5ee5SMike Stump } 996ee2a5ee5SMike Stump 997f89230d9SMike Stump llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T, 998f89230d9SMike Stump int flag) { 999f89230d9SMike Stump return CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag); 1000ee2a5ee5SMike Stump } 1001ee2a5ee5SMike Stump 1002fbe25dd1SMike Stump llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T, 1003fbe25dd1SMike Stump int flag) { 1004fbe25dd1SMike Stump return CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag); 1005ee2a5ee5SMike Stump } 1006ee2a5ee5SMike Stump 1007626aecc4SMike Stump llvm::Value *BlockFunction::getBlockObjectDispose() { 1008626aecc4SMike Stump if (CGM.BlockObjectDispose == 0) { 1009626aecc4SMike Stump const llvm::FunctionType *FTy; 1010626aecc4SMike Stump std::vector<const llvm::Type*> ArgTys; 1011626aecc4SMike Stump const llvm::Type *ResultType = llvm::Type::VoidTy; 1012626aecc4SMike Stump ArgTys.push_back(PtrToInt8Ty); 1013626aecc4SMike Stump ArgTys.push_back(llvm::Type::Int32Ty); 1014626aecc4SMike Stump FTy = llvm::FunctionType::get(ResultType, ArgTys, false); 1015626aecc4SMike Stump CGM.BlockObjectDispose 1016626aecc4SMike Stump = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose"); 1017626aecc4SMike Stump } 1018626aecc4SMike Stump return CGM.BlockObjectDispose; 1019626aecc4SMike Stump } 1020626aecc4SMike Stump 1021f89230d9SMike Stump llvm::Value *BlockFunction::getBlockObjectAssign() { 1022f89230d9SMike Stump if (CGM.BlockObjectAssign == 0) { 1023f89230d9SMike Stump const llvm::FunctionType *FTy; 1024f89230d9SMike Stump std::vector<const llvm::Type*> ArgTys; 1025f89230d9SMike Stump const llvm::Type *ResultType = llvm::Type::VoidTy; 1026f89230d9SMike Stump ArgTys.push_back(PtrToInt8Ty); 1027f89230d9SMike Stump ArgTys.push_back(PtrToInt8Ty); 1028f89230d9SMike Stump ArgTys.push_back(llvm::Type::Int32Ty); 1029f89230d9SMike Stump FTy = llvm::FunctionType::get(ResultType, ArgTys, false); 1030f89230d9SMike Stump CGM.BlockObjectAssign 1031f89230d9SMike Stump = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign"); 1032f89230d9SMike Stump } 1033f89230d9SMike Stump return CGM.BlockObjectAssign; 1034f89230d9SMike Stump } 1035f89230d9SMike Stump 1036fbe25dd1SMike Stump void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) { 1037626aecc4SMike Stump llvm::Value *F = getBlockObjectDispose(); 1038fbe25dd1SMike Stump llvm::Value *N; 1039626aecc4SMike Stump V = Builder.CreateBitCast(V, PtrToInt8Ty); 1040fbe25dd1SMike Stump N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag); 1041626aecc4SMike Stump Builder.CreateCall2(F, V, N); 1042626aecc4SMike Stump } 10434446dcf0SMike Stump 10444446dcf0SMike Stump ASTContext &BlockFunction::getContext() const { return CGM.getContext(); } 1045aeb0ffd5SMike Stump 1046aeb0ffd5SMike Stump BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf, 1047aeb0ffd5SMike Stump CGBuilderTy &B) 1048aeb0ffd5SMike Stump : CGM(cgm), CGF(cgf), Builder(B) { 1049aeb0ffd5SMike Stump PtrToInt8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 1050aeb0ffd5SMike Stump 1051aeb0ffd5SMike Stump BlockHasCopyDispose = false; 1052aeb0ffd5SMike Stump } 1053