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 29d6ef62f0SMike Stump llvm::Constant *CodeGenFunction:: 30d2142cffSMike Stump BuildDescriptorBlockDecl(bool BlockHasCopyDispose, uint64_t Size, 31d2142cffSMike Stump const llvm::StructType* Ty, 32aeb0ffd5SMike Stump std::vector<HelperInfo> *NoteForHelper) { 33c2c38331SMike Stump const llvm::Type *UnsignedLongTy 34c2c38331SMike Stump = CGM.getTypes().ConvertType(getContext().UnsignedLongTy); 3585284bacSMike Stump llvm::Constant *C; 3685284bacSMike Stump std::vector<llvm::Constant*> Elts; 3785284bacSMike Stump 3885284bacSMike Stump // reserved 39c2c38331SMike Stump C = llvm::ConstantInt::get(UnsignedLongTy, 0); 4085284bacSMike Stump Elts.push_back(C); 4185284bacSMike Stump 4285284bacSMike Stump // Size 432ac40a9fSMike Stump // FIXME: What is the right way to say this doesn't fit? We should give 442ac40a9fSMike Stump // a user diagnostic in that case. Better fix would be to change the 452ac40a9fSMike Stump // API to size_t. 46cb2fbcb0SMike Stump C = llvm::ConstantInt::get(UnsignedLongTy, Size); 4785284bacSMike Stump Elts.push_back(C); 4885284bacSMike Stump 4985284bacSMike Stump if (BlockHasCopyDispose) { 5085284bacSMike Stump // copy_func_helper_decl 5167645930SMike Stump Elts.push_back(BuildCopyHelper(Ty, NoteForHelper)); 5285284bacSMike Stump 5385284bacSMike Stump // destroy_func_decl 5467645930SMike Stump Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper)); 5585284bacSMike Stump } 5685284bacSMike Stump 5785284bacSMike Stump C = llvm::ConstantStruct::get(Elts); 5885284bacSMike Stump 5985284bacSMike Stump C = new llvm::GlobalVariable(C->getType(), true, 6085284bacSMike Stump llvm::GlobalValue::InternalLinkage, 61499ae7ecSMike Stump C, "__block_descriptor_tmp", &CGM.getModule()); 6285284bacSMike Stump return C; 6385284bacSMike Stump } 6485284bacSMike Stump 6595435674SMike Stump llvm::Constant *BlockModule::getNSConcreteGlobalBlock() { 66971f9b66SMike Stump if (NSConcreteGlobalBlock) 67971f9b66SMike Stump return NSConcreteGlobalBlock; 68971f9b66SMike Stump 6952d9c49dSMike Stump // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the 702d33d630SMike Stump // same thing as CreateRuntimeFunction if there's already a variable with the 712d33d630SMike Stump // same name. 72971f9b66SMike Stump NSConcreteGlobalBlock 73971f9b66SMike Stump = new llvm::GlobalVariable(PtrToInt8Ty, false, 74971f9b66SMike Stump llvm::GlobalValue::ExternalLinkage, 75971f9b66SMike Stump 0, "_NSConcreteGlobalBlock", 76971f9b66SMike Stump &getModule()); 77971f9b66SMike Stump 78971f9b66SMike Stump return NSConcreteGlobalBlock; 79971f9b66SMike Stump } 80971f9b66SMike Stump 8195435674SMike Stump llvm::Constant *BlockModule::getNSConcreteStackBlock() { 827ab278dbSMike Stump if (NSConcreteStackBlock) 837ab278dbSMike Stump return NSConcreteStackBlock; 847ab278dbSMike Stump 8552d9c49dSMike Stump // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the 862d33d630SMike Stump // same thing as CreateRuntimeFunction if there's already a variable with the 872d33d630SMike Stump // same name. 887ab278dbSMike Stump NSConcreteStackBlock 897ab278dbSMike Stump = new llvm::GlobalVariable(PtrToInt8Ty, false, 907ab278dbSMike Stump llvm::GlobalValue::ExternalLinkage, 917ab278dbSMike Stump 0, "_NSConcreteStackBlock", 927ab278dbSMike Stump &getModule()); 937ab278dbSMike Stump 947ab278dbSMike Stump return NSConcreteStackBlock; 957ab278dbSMike Stump } 967ab278dbSMike Stump 97ed5e69feSAnders Carlsson static void CollectBlockDeclRefInfo(const Stmt *S, 98ed5e69feSAnders Carlsson CodeGenFunction::BlockInfo &Info) { 99ed5e69feSAnders Carlsson for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); 100ed5e69feSAnders Carlsson I != E; ++I) 101dd942713SDaniel Dunbar if (*I) 102ed5e69feSAnders Carlsson CollectBlockDeclRefInfo(*I, Info); 103ed5e69feSAnders Carlsson 104ed5e69feSAnders Carlsson if (const BlockDeclRefExpr *DE = dyn_cast<BlockDeclRefExpr>(S)) { 105ed5e69feSAnders Carlsson // FIXME: Handle enums. 106ed5e69feSAnders Carlsson if (isa<FunctionDecl>(DE->getDecl())) 107ed5e69feSAnders Carlsson return; 108ed5e69feSAnders Carlsson 109ed5e69feSAnders Carlsson if (DE->isByRef()) 110ed5e69feSAnders Carlsson Info.ByRefDeclRefs.push_back(DE); 111ed5e69feSAnders Carlsson else 112ed5e69feSAnders Carlsson Info.ByCopyDeclRefs.push_back(DE); 113ed5e69feSAnders Carlsson } 114ed5e69feSAnders Carlsson } 115ed5e69feSAnders Carlsson 1162d33d630SMike Stump /// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be 1172d33d630SMike Stump /// declared as a global variable instead of on the stack. 118ed5e69feSAnders Carlsson static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info) 119ed5e69feSAnders Carlsson { 120ed5e69feSAnders Carlsson return Info.ByRefDeclRefs.empty() && Info.ByCopyDeclRefs.empty(); 121ed5e69feSAnders Carlsson } 122ed5e69feSAnders Carlsson 1232d33d630SMike Stump // FIXME: Push most into CGM, passing down a few bits, like current function 1242d33d630SMike Stump // name. 125b750d928SMike Stump llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) { 126ed5e69feSAnders Carlsson 127ed5e69feSAnders Carlsson std::string Name = CurFn->getName(); 128ed5e69feSAnders Carlsson CodeGenFunction::BlockInfo Info(0, Name.c_str()); 129ed5e69feSAnders Carlsson CollectBlockDeclRefInfo(BE->getBody(), Info); 130ed5e69feSAnders Carlsson 131ed5e69feSAnders Carlsson // Check if the block can be global. 1322d33d630SMike Stump // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like 1332d33d630SMike Stump // to just have one code path. We should move this function into CGM and pass 1342d33d630SMike Stump // CGF, then we can just check to see if CGF is 0. 13597d01d50SMike Stump if (0 && CanBlockBeGlobal(Info)) 136ed5e69feSAnders Carlsson return CGM.GetAddrOfGlobalBlock(BE, Name.c_str()); 13785284bacSMike Stump 1384446dcf0SMike Stump std::vector<llvm::Constant*> Elts(5); 13985284bacSMike Stump llvm::Constant *C; 140b750d928SMike Stump llvm::Value *V; 14185284bacSMike Stump 14285284bacSMike Stump { 14385284bacSMike Stump // C = BuildBlockStructInitlist(); 14485284bacSMike Stump unsigned int flags = BLOCK_HAS_DESCRIPTOR; 14585284bacSMike Stump 1464446dcf0SMike Stump // We run this first so that we set BlockHasCopyDispose from the entire 1474446dcf0SMike Stump // block literal. 1484446dcf0SMike Stump // __invoke 1494446dcf0SMike Stump uint64_t subBlockSize, subBlockAlign; 1504446dcf0SMike Stump llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; 151d2142cffSMike Stump bool subBlockHasCopyDispose = false; 1524446dcf0SMike Stump llvm::Function *Fn 153692c6e37SMike Stump = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl, LocalDeclMap, 1545469f29aSMike Stump subBlockSize, 1554446dcf0SMike Stump subBlockAlign, 1564446dcf0SMike Stump subBlockDeclRefDecls, 157d2142cffSMike Stump subBlockHasCopyDispose); 158d2142cffSMike Stump BlockHasCopyDispose |= subBlockHasCopyDispose; 1594446dcf0SMike Stump Elts[3] = Fn; 1604446dcf0SMike Stump 161*d898026eSMike Stump // FIXME: Don't use BlockHasCopyDispose, it is set more often then necessary, for 162*d898026eSMike Stump // example: { ^{ __block int i; ^{ i = 1; }(); }(); } 163d2142cffSMike Stump if (subBlockHasCopyDispose) 16485284bacSMike Stump flags |= BLOCK_HAS_COPY_DISPOSE; 16585284bacSMike Stump 166499ae7ecSMike Stump // __isa 1677ab278dbSMike Stump C = CGM.getNSConcreteStackBlock(); 16885284bacSMike Stump C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty); 1694446dcf0SMike Stump Elts[0] = C; 17085284bacSMike Stump 17185284bacSMike Stump // __flags 17285284bacSMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 17385284bacSMike Stump CGM.getTypes().ConvertType(CGM.getContext().IntTy)); 17485284bacSMike Stump C = llvm::ConstantInt::get(IntTy, flags); 1754446dcf0SMike Stump Elts[1] = C; 17685284bacSMike Stump 17785284bacSMike Stump // __reserved 17885284bacSMike Stump C = llvm::ConstantInt::get(IntTy, 0); 1794446dcf0SMike Stump Elts[2] = C; 18085284bacSMike Stump 181b750d928SMike Stump if (subBlockDeclRefDecls.size() == 0) { 182d6ef62f0SMike Stump // __descriptor 183d2142cffSMike Stump Elts[4] = BuildDescriptorBlockDecl(subBlockHasCopyDispose, subBlockSize, 0, 0); 184d6ef62f0SMike Stump 1853a139f37SMike Stump // Optimize to being a global block. 1863a139f37SMike Stump Elts[0] = CGM.getNSConcreteGlobalBlock(); 1873a139f37SMike Stump Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL); 1883a139f37SMike Stump 18985284bacSMike Stump C = llvm::ConstantStruct::get(Elts); 19085284bacSMike Stump 19185284bacSMike Stump char Name[32]; 1929319db8aSMike Stump sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount()); 19385284bacSMike Stump C = new llvm::GlobalVariable(C->getType(), true, 19485284bacSMike Stump llvm::GlobalValue::InternalLinkage, 19585284bacSMike Stump C, Name, &CGM.getModule()); 1965d2534adSMike Stump QualType BPT = BE->getType(); 1975d2534adSMike Stump C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT)); 19885284bacSMike Stump return C; 19985284bacSMike Stump } 20085284bacSMike Stump 201b750d928SMike Stump std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size()); 202aeb0ffd5SMike Stump for (int i=0; i<4; ++i) 203b750d928SMike Stump Types[i] = Elts[i]->getType(); 204aeb0ffd5SMike Stump Types[4] = PtrToInt8Ty; 20585284bacSMike Stump 2061db7d04bSMike Stump for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) { 2071db7d04bSMike Stump const Expr *E = subBlockDeclRefDecls[i]; 2081db7d04bSMike Stump const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); 2091db7d04bSMike Stump QualType Ty = E->getType(); 21097d01d50SMike Stump if (BDRE && BDRE->isByRef()) { 21197d01d50SMike Stump uint64_t Align = getContext().getDeclAlignInBytes(BDRE->getDecl()); 21297d01d50SMike Stump Types[i+5] = llvm::PointerType::get(BuildByRefType(Ty, Align), 0); 21397d01d50SMike Stump } else 2141db7d04bSMike Stump Types[i+5] = ConvertType(Ty); 2151db7d04bSMike Stump } 216b750d928SMike Stump 217aeb0ffd5SMike Stump llvm::StructType *Ty = llvm::StructType::get(Types, true); 218b750d928SMike Stump 219b750d928SMike Stump llvm::AllocaInst *A = CreateTempAlloca(Ty); 220b750d928SMike Stump A->setAlignment(subBlockAlign); 221b750d928SMike Stump V = A; 222b750d928SMike Stump 223aeb0ffd5SMike Stump std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size()); 224aeb0ffd5SMike Stump int helpersize = 0; 225aeb0ffd5SMike Stump 226aeb0ffd5SMike Stump for (unsigned i=0; i<4; ++i) 227b750d928SMike Stump Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp")); 228b750d928SMike Stump 229b750d928SMike Stump for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) 230b750d928SMike Stump { 2311db7d04bSMike Stump // FIXME: Push const down. 2321db7d04bSMike Stump Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]); 2331db7d04bSMike Stump DeclRefExpr *DR; 2341db7d04bSMike Stump ValueDecl *VD; 235b750d928SMike Stump 2361db7d04bSMike Stump DR = dyn_cast<DeclRefExpr>(E); 2371db7d04bSMike Stump // Skip padding. 2381db7d04bSMike Stump if (DR) continue; 2391db7d04bSMike Stump 2401db7d04bSMike Stump BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); 2411db7d04bSMike Stump VD = BDRE->getDecl(); 2421db7d04bSMike Stump 24397d01d50SMike Stump llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp"); 244aeb0ffd5SMike Stump NoteForHelper[helpersize].index = i+5; 245aeb0ffd5SMike Stump NoteForHelper[helpersize].RequiresCopying = BlockRequiresCopying(VD->getType()); 246aeb0ffd5SMike Stump NoteForHelper[helpersize].flag 247aeb0ffd5SMike Stump = VD->getType()->isBlockPointerType() ? BLOCK_FIELD_IS_BLOCK : BLOCK_FIELD_IS_OBJECT; 248aeb0ffd5SMike Stump 2491db7d04bSMike Stump if (LocalDeclMap[VD]) { 25097d01d50SMike Stump if (BDRE->isByRef()) { 251aeb0ffd5SMike Stump NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF | 25290d8daf5SMike Stump // FIXME: Someone double check this. 25390d8daf5SMike Stump (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0); 25497d01d50SMike Stump const llvm::Type *Ty = Types[i+5]; 25597d01d50SMike Stump llvm::Value *Loc = LocalDeclMap[VD]; 25697d01d50SMike Stump Loc = Builder.CreateStructGEP(Loc, 1, "forwarding"); 25797d01d50SMike Stump Loc = Builder.CreateLoad(Loc, false); 25897d01d50SMike Stump Loc = Builder.CreateBitCast(Loc, Ty); 25997d01d50SMike Stump Builder.CreateStore(Loc, Addr); 260aeb0ffd5SMike Stump ++helpersize; 26197d01d50SMike Stump continue; 26297d01d50SMike Stump } else 2631db7d04bSMike Stump E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD), 2641db7d04bSMike Stump VD->getType(), SourceLocation(), 2651db7d04bSMike Stump false, false); 2661db7d04bSMike Stump } 26797d01d50SMike Stump if (BDRE->isByRef()) { 268efd7caa8SMike Stump NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF | 269efd7caa8SMike Stump // FIXME: Someone double check this. 270efd7caa8SMike Stump (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0); 2711db7d04bSMike Stump E = new (getContext()) 2721db7d04bSMike Stump UnaryOperator(E, UnaryOperator::AddrOf, 2731db7d04bSMike Stump getContext().getPointerType(E->getType()), 2741db7d04bSMike Stump SourceLocation()); 27597d01d50SMike Stump } 276aeb0ffd5SMike Stump ++helpersize; 2771db7d04bSMike Stump 2781db7d04bSMike Stump RValue r = EmitAnyExpr(E, Addr, false); 27997d01d50SMike Stump if (r.isScalar()) { 28097d01d50SMike Stump llvm::Value *Loc = r.getScalarVal(); 28197d01d50SMike Stump const llvm::Type *Ty = Types[i+5]; 28297d01d50SMike Stump if (BDRE->isByRef()) { 2832d33d630SMike Stump // E is now the address of the value field, instead, we want the 2842d33d630SMike Stump // address of the actual ByRef struct. We optimize this slightly 2852d33d630SMike Stump // compared to gcc by not grabbing the forwarding slot as this must 2862d33d630SMike Stump // be done during Block_copy for us, and we can postpone the work 2872d33d630SMike Stump // until then. 2882d33d630SMike Stump uint64_t offset = BlockDecls[BDRE->getDecl()]; 2892d33d630SMike Stump 2902d33d630SMike Stump llvm::Value *BlockLiteral = LoadBlockStruct(); 2912d33d630SMike Stump 2922d33d630SMike Stump Loc = Builder.CreateGEP(BlockLiteral, 2932d33d630SMike Stump llvm::ConstantInt::get(llvm::Type::Int64Ty, 2942d33d630SMike Stump offset), 2952d33d630SMike Stump "block.literal"); 2962d33d630SMike Stump Ty = llvm::PointerType::get(Ty, 0); 29797d01d50SMike Stump Loc = Builder.CreateBitCast(Loc, Ty); 2982d33d630SMike Stump Loc = Builder.CreateLoad(Loc, false); 2992d33d630SMike Stump // Loc = Builder.CreateBitCast(Loc, Ty); 30097d01d50SMike Stump } 30197d01d50SMike Stump Builder.CreateStore(Loc, Addr); 30297d01d50SMike Stump } else if (r.isComplex()) 303b750d928SMike Stump // FIXME: implement 304b750d928SMike Stump ErrorUnsupported(BE, "complex in block literal"); 305b750d928SMike Stump else if (r.isAggregate()) 306b750d928SMike Stump ; // Already created into the destination 307b750d928SMike Stump else 308b750d928SMike Stump assert (0 && "bad block variable"); 309b750d928SMike Stump // FIXME: Ensure that the offset created by the backend for 310b750d928SMike Stump // the struct matches the previously computed offset in BlockDecls. 311b750d928SMike Stump } 312aeb0ffd5SMike Stump NoteForHelper.resize(helpersize); 313d6ef62f0SMike Stump 314d6ef62f0SMike Stump // __descriptor 315d2142cffSMike Stump llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockHasCopyDispose, 316d2142cffSMike Stump subBlockSize, Ty, 317aeb0ffd5SMike Stump &NoteForHelper); 318aeb0ffd5SMike Stump Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty); 319aeb0ffd5SMike Stump Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp")); 320b750d928SMike Stump } 321b750d928SMike Stump 322b750d928SMike Stump QualType BPT = BE->getType(); 323b750d928SMike Stump return Builder.CreateBitCast(V, ConvertType(BPT)); 324b750d928SMike Stump } 32585284bacSMike Stump 32685284bacSMike Stump 32795435674SMike Stump const llvm::Type *BlockModule::getBlockDescriptorType() { 328650c932dSMike Stump if (BlockDescriptorType) 329650c932dSMike Stump return BlockDescriptorType; 3302437cbfaSAnders Carlsson 3312437cbfaSAnders Carlsson const llvm::Type *UnsignedLongTy = 332650c932dSMike Stump getTypes().ConvertType(getContext().UnsignedLongTy); 3332437cbfaSAnders Carlsson 3342437cbfaSAnders Carlsson // struct __block_descriptor { 3352437cbfaSAnders Carlsson // unsigned long reserved; 3362437cbfaSAnders Carlsson // unsigned long block_size; 3372437cbfaSAnders Carlsson // }; 338650c932dSMike Stump BlockDescriptorType = llvm::StructType::get(UnsignedLongTy, 3392437cbfaSAnders Carlsson UnsignedLongTy, 3402437cbfaSAnders Carlsson NULL); 3412437cbfaSAnders Carlsson 342650c932dSMike Stump getModule().addTypeName("struct.__block_descriptor", 343650c932dSMike Stump BlockDescriptorType); 3442437cbfaSAnders Carlsson 345650c932dSMike Stump return BlockDescriptorType; 3462437cbfaSAnders Carlsson } 3472437cbfaSAnders Carlsson 34895435674SMike Stump const llvm::Type *BlockModule::getGenericBlockLiteralType() { 349005c9a62SMike Stump if (GenericBlockLiteralType) 350005c9a62SMike Stump return GenericBlockLiteralType; 3512437cbfaSAnders Carlsson 3522437cbfaSAnders Carlsson const llvm::Type *BlockDescPtrTy = 353005c9a62SMike Stump llvm::PointerType::getUnqual(getBlockDescriptorType()); 3542437cbfaSAnders Carlsson 35592bbd6d4SMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 35692bbd6d4SMike Stump getTypes().ConvertType(getContext().IntTy)); 35792bbd6d4SMike Stump 3582437cbfaSAnders Carlsson // struct __block_literal_generic { 3595d2534adSMike Stump // void *__isa; 3605d2534adSMike Stump // int __flags; 3615d2534adSMike Stump // int __reserved; 3625d2534adSMike Stump // void (*__invoke)(void *); 3635d2534adSMike Stump // struct __block_descriptor *__descriptor; 3642437cbfaSAnders Carlsson // }; 365626aecc4SMike Stump GenericBlockLiteralType = llvm::StructType::get(PtrToInt8Ty, 36692bbd6d4SMike Stump IntTy, 36792bbd6d4SMike Stump IntTy, 368626aecc4SMike Stump PtrToInt8Ty, 3692437cbfaSAnders Carlsson BlockDescPtrTy, 3702437cbfaSAnders Carlsson NULL); 3712437cbfaSAnders Carlsson 372005c9a62SMike Stump getModule().addTypeName("struct.__block_literal_generic", 373005c9a62SMike Stump GenericBlockLiteralType); 3742437cbfaSAnders Carlsson 375005c9a62SMike Stump return GenericBlockLiteralType; 3762437cbfaSAnders Carlsson } 3772437cbfaSAnders Carlsson 37895435674SMike Stump const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() { 3795d2534adSMike Stump if (GenericExtendedBlockLiteralType) 3805d2534adSMike Stump return GenericExtendedBlockLiteralType; 3815d2534adSMike Stump 3825d2534adSMike Stump const llvm::Type *BlockDescPtrTy = 3835d2534adSMike Stump llvm::PointerType::getUnqual(getBlockDescriptorType()); 3845d2534adSMike Stump 3855d2534adSMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 3865d2534adSMike Stump getTypes().ConvertType(getContext().IntTy)); 3875d2534adSMike Stump 3885d2534adSMike Stump // struct __block_literal_generic { 3895d2534adSMike Stump // void *__isa; 3905d2534adSMike Stump // int __flags; 3915d2534adSMike Stump // int __reserved; 3925d2534adSMike Stump // void (*__invoke)(void *); 3935d2534adSMike Stump // struct __block_descriptor *__descriptor; 3945d2534adSMike Stump // void *__copy_func_helper_decl; 3955d2534adSMike Stump // void *__destroy_func_decl; 3965d2534adSMike Stump // }; 397626aecc4SMike Stump GenericExtendedBlockLiteralType = llvm::StructType::get(PtrToInt8Ty, 3985d2534adSMike Stump IntTy, 3995d2534adSMike Stump IntTy, 400626aecc4SMike Stump PtrToInt8Ty, 4015d2534adSMike Stump BlockDescPtrTy, 402626aecc4SMike Stump PtrToInt8Ty, 403626aecc4SMike Stump PtrToInt8Ty, 4045d2534adSMike Stump NULL); 4055d2534adSMike Stump 4065d2534adSMike Stump getModule().addTypeName("struct.__block_literal_extended_generic", 4075d2534adSMike Stump GenericExtendedBlockLiteralType); 4085d2534adSMike Stump 4095d2534adSMike Stump return GenericExtendedBlockLiteralType; 4105d2534adSMike Stump } 4115d2534adSMike Stump 4126a60fa24SAnders Carlsson RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) { 4132437cbfaSAnders Carlsson const BlockPointerType *BPT = 4142437cbfaSAnders Carlsson E->getCallee()->getType()->getAsBlockPointerType(); 4152437cbfaSAnders Carlsson 4162437cbfaSAnders Carlsson llvm::Value *Callee = EmitScalarExpr(E->getCallee()); 4172437cbfaSAnders Carlsson 4182437cbfaSAnders Carlsson // Get a pointer to the generic block literal. 4192437cbfaSAnders Carlsson const llvm::Type *BlockLiteralTy = 420005c9a62SMike Stump llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType()); 4212437cbfaSAnders Carlsson 4222437cbfaSAnders Carlsson // Bitcast the callee to a block literal. 4232437cbfaSAnders Carlsson llvm::Value *BlockLiteral = 4242437cbfaSAnders Carlsson Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal"); 4252437cbfaSAnders Carlsson 4262437cbfaSAnders Carlsson // Get the function pointer from the literal. 4272437cbfaSAnders Carlsson llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp"); 4282437cbfaSAnders Carlsson 4292437cbfaSAnders Carlsson BlockLiteral = 4302437cbfaSAnders Carlsson Builder.CreateBitCast(BlockLiteral, 4312437cbfaSAnders Carlsson llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4322437cbfaSAnders Carlsson "tmp"); 4332437cbfaSAnders Carlsson 4342437cbfaSAnders Carlsson // Add the block literal. 4352437cbfaSAnders Carlsson QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy); 4362437cbfaSAnders Carlsson CallArgList Args; 4372437cbfaSAnders Carlsson Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy)); 4382437cbfaSAnders Carlsson 439479e6fc7SAnders Carlsson QualType FnType = BPT->getPointeeType(); 440479e6fc7SAnders Carlsson 4412437cbfaSAnders Carlsson // And the rest of the arguments. 442479e6fc7SAnders Carlsson EmitCallArgs(Args, FnType->getAsFunctionProtoType(), 443479e6fc7SAnders Carlsson E->arg_begin(), E->arg_end()); 4442437cbfaSAnders Carlsson 4455f50c651SAnders Carlsson // Load the function. 4465f50c651SAnders Carlsson llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp"); 4475f50c651SAnders Carlsson 448a60cbcdfSAnders Carlsson QualType ResultType = FnType->getAsFunctionType()->getResultType(); 449a60cbcdfSAnders Carlsson 450a60cbcdfSAnders Carlsson const CGFunctionInfo &FnInfo = 451a60cbcdfSAnders Carlsson CGM.getTypes().getFunctionInfo(ResultType, Args); 4525f50c651SAnders Carlsson 4535f50c651SAnders Carlsson // Cast the function pointer to the right type. 4545f50c651SAnders Carlsson const llvm::Type *BlockFTy = 455a60cbcdfSAnders Carlsson CGM.getTypes().GetFunctionType(FnInfo, false); 4565f50c651SAnders Carlsson 4575f50c651SAnders Carlsson const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy); 4585f50c651SAnders Carlsson Func = Builder.CreateBitCast(Func, BlockFTyPtr); 4595f50c651SAnders Carlsson 4602437cbfaSAnders Carlsson // And call the block. 461350da607SAnders Carlsson return EmitCall(FnInfo, Func, Args); 4622437cbfaSAnders Carlsson } 4636a60fa24SAnders Carlsson 46497d01d50SMike Stump llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) { 46597d01d50SMike Stump uint64_t &offset = BlockDecls[E->getDecl()]; 46697d01d50SMike Stump 46797d01d50SMike Stump const llvm::Type *Ty; 46897d01d50SMike Stump Ty = CGM.getTypes().ConvertType(E->getDecl()->getType()); 46997d01d50SMike Stump 47097d01d50SMike Stump // See if we have already allocated an offset for this variable. 47197d01d50SMike Stump if (offset == 0) { 4724446dcf0SMike Stump // Don't run the expensive check, unless we have to. 4734446dcf0SMike Stump if (!BlockHasCopyDispose && BlockRequiresCopying(E->getType())) 4744446dcf0SMike Stump BlockHasCopyDispose = true; 47597d01d50SMike Stump // if not, allocate one now. 47697d01d50SMike Stump offset = getBlockOffset(E); 47797d01d50SMike Stump } 47897d01d50SMike Stump 47997d01d50SMike Stump llvm::Value *BlockLiteral = LoadBlockStruct(); 48097d01d50SMike Stump llvm::Value *V = Builder.CreateGEP(BlockLiteral, 48197d01d50SMike Stump llvm::ConstantInt::get(llvm::Type::Int64Ty, 48297d01d50SMike Stump offset), 4832d33d630SMike Stump "block.literal"); 48497d01d50SMike Stump if (E->isByRef()) { 48597d01d50SMike Stump bool needsCopyDispose = BlockRequiresCopying(E->getType()); 48697d01d50SMike Stump uint64_t Align = getContext().getDeclAlignInBytes(E->getDecl()); 48797d01d50SMike Stump const llvm::Type *PtrStructTy 48897d01d50SMike Stump = llvm::PointerType::get(BuildByRefType(E->getType(), Align), 0); 489efd7caa8SMike Stump // The block literal will need a copy/destroy helper. 490efd7caa8SMike Stump BlockHasCopyDispose = true; 49197d01d50SMike Stump Ty = PtrStructTy; 49297d01d50SMike Stump Ty = llvm::PointerType::get(Ty, 0); 49397d01d50SMike Stump V = Builder.CreateBitCast(V, Ty); 49497d01d50SMike Stump V = Builder.CreateLoad(V, false); 49597d01d50SMike Stump V = Builder.CreateStructGEP(V, 1, "forwarding"); 49697d01d50SMike Stump V = Builder.CreateLoad(V, false); 49797d01d50SMike Stump V = Builder.CreateBitCast(V, PtrStructTy); 49897d01d50SMike Stump V = Builder.CreateStructGEP(V, needsCopyDispose*2 + 4, "x"); 49997d01d50SMike Stump } else { 50097d01d50SMike Stump Ty = llvm::PointerType::get(Ty, 0); 50197d01d50SMike Stump V = Builder.CreateBitCast(V, Ty); 50297d01d50SMike Stump } 50397d01d50SMike Stump return V; 50497d01d50SMike Stump } 50597d01d50SMike Stump 506692c6e37SMike Stump void CodeGenFunction::BlockForwardSelf() { 507692c6e37SMike Stump const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); 508692c6e37SMike Stump ImplicitParamDecl *SelfDecl = OMD->getSelfDecl(); 509692c6e37SMike Stump llvm::Value *&DMEntry = LocalDeclMap[SelfDecl]; 510692c6e37SMike Stump if (DMEntry) 511692c6e37SMike Stump return; 512692c6e37SMike Stump // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care 513692c6e37SMike Stump BlockDeclRefExpr *BDRE = new (getContext()) 514692c6e37SMike Stump BlockDeclRefExpr(SelfDecl, 515692c6e37SMike Stump SelfDecl->getType(), SourceLocation(), false); 516692c6e37SMike Stump DMEntry = GetAddrOfBlockDecl(BDRE); 517692c6e37SMike Stump } 518692c6e37SMike Stump 5192d5a2878SMike Stump llvm::Constant * 5206c39666aSMike Stump BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) { 5216a60fa24SAnders Carlsson // Generate the block descriptor. 5226a60fa24SAnders Carlsson const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy); 52392bbd6d4SMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 52492bbd6d4SMike Stump getTypes().ConvertType(getContext().IntTy)); 5256a60fa24SAnders Carlsson 5266a60fa24SAnders Carlsson llvm::Constant *DescriptorFields[2]; 5276a60fa24SAnders Carlsson 5286a60fa24SAnders Carlsson // Reserved 5296a60fa24SAnders Carlsson DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy); 5306a60fa24SAnders Carlsson 5316a60fa24SAnders Carlsson // Block literal size. For global blocks we just use the size of the generic 5326a60fa24SAnders Carlsson // block literal struct. 5336a60fa24SAnders Carlsson uint64_t BlockLiteralSize = 534005c9a62SMike Stump TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8; 5356a60fa24SAnders Carlsson DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize); 5366a60fa24SAnders Carlsson 5376a60fa24SAnders Carlsson llvm::Constant *DescriptorStruct = 5386a60fa24SAnders Carlsson llvm::ConstantStruct::get(&DescriptorFields[0], 2); 5396a60fa24SAnders Carlsson 5406a60fa24SAnders Carlsson llvm::GlobalVariable *Descriptor = 5416a60fa24SAnders Carlsson new llvm::GlobalVariable(DescriptorStruct->getType(), true, 5426a60fa24SAnders Carlsson llvm::GlobalVariable::InternalLinkage, 5436a60fa24SAnders Carlsson DescriptorStruct, "__block_descriptor_global", 5446a60fa24SAnders Carlsson &getModule()); 5456a60fa24SAnders Carlsson 5466a60fa24SAnders Carlsson // Generate the constants for the block literal. 5476a60fa24SAnders Carlsson llvm::Constant *LiteralFields[5]; 5486a60fa24SAnders Carlsson 5492d5a2878SMike Stump CodeGenFunction::BlockInfo Info(0, n); 550b750d928SMike Stump uint64_t subBlockSize, subBlockAlign; 5511db7d04bSMike Stump llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; 552a61bb4d1SDaniel Dunbar bool subBlockHasCopyDispose = false; 5535469f29aSMike Stump llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap; 554cb2fbcb0SMike Stump llvm::Function *Fn 555692c6e37SMike Stump = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap, 5565469f29aSMike Stump subBlockSize, 557b750d928SMike Stump subBlockAlign, 5584446dcf0SMike Stump subBlockDeclRefDecls, 5594446dcf0SMike Stump subBlockHasCopyDispose); 560cb2fbcb0SMike Stump assert(subBlockSize == BlockLiteralSize 561cb2fbcb0SMike Stump && "no imports allowed for global block"); 5626a60fa24SAnders Carlsson 5636a60fa24SAnders Carlsson // isa 564971f9b66SMike Stump LiteralFields[0] = getNSConcreteGlobalBlock(); 5656a60fa24SAnders Carlsson 5666a60fa24SAnders Carlsson // Flags 5675878c797SAnders Carlsson LiteralFields[1] = 5685878c797SAnders Carlsson llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR); 5696a60fa24SAnders Carlsson 5706a60fa24SAnders Carlsson // Reserved 57192bbd6d4SMike Stump LiteralFields[2] = llvm::Constant::getNullValue(IntTy); 5726a60fa24SAnders Carlsson 5736a60fa24SAnders Carlsson // Function 5746a60fa24SAnders Carlsson LiteralFields[3] = Fn; 5756a60fa24SAnders Carlsson 5766a60fa24SAnders Carlsson // Descriptor 5776a60fa24SAnders Carlsson LiteralFields[4] = Descriptor; 5786a60fa24SAnders Carlsson 5796a60fa24SAnders Carlsson llvm::Constant *BlockLiteralStruct = 5806a60fa24SAnders Carlsson llvm::ConstantStruct::get(&LiteralFields[0], 5); 5816a60fa24SAnders Carlsson 5826a60fa24SAnders Carlsson llvm::GlobalVariable *BlockLiteral = 5836a60fa24SAnders Carlsson new llvm::GlobalVariable(BlockLiteralStruct->getType(), true, 5846a60fa24SAnders Carlsson llvm::GlobalVariable::InternalLinkage, 5856a60fa24SAnders Carlsson BlockLiteralStruct, "__block_literal_global", 5866a60fa24SAnders Carlsson &getModule()); 5876a60fa24SAnders Carlsson 5886a60fa24SAnders Carlsson return BlockLiteral; 5896a60fa24SAnders Carlsson } 5906a60fa24SAnders Carlsson 591cb2fbcb0SMike Stump llvm::Value *CodeGenFunction::LoadBlockStruct() { 592cb2fbcb0SMike Stump return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self"); 593cb2fbcb0SMike Stump } 594cb2fbcb0SMike Stump 5954446dcf0SMike Stump llvm::Function * 5964446dcf0SMike Stump CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr, 597cb2fbcb0SMike Stump const BlockInfo& Info, 598692c6e37SMike Stump const Decl *OuterFuncDecl, 5995469f29aSMike Stump llvm::DenseMap<const Decl*, llvm::Value*> ldm, 600b750d928SMike Stump uint64_t &Size, 601b750d928SMike Stump uint64_t &Align, 6024446dcf0SMike Stump llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls, 6034446dcf0SMike Stump bool &subBlockHasCopyDispose) { 6049074ed8cSDevang Patel 6059074ed8cSDevang Patel // Check if we should generate debug info for this block. 6069074ed8cSDevang Patel if (CGM.getDebugInfo()) 6079074ed8cSDevang Patel DebugInfo = CGM.getDebugInfo(); 6089074ed8cSDevang Patel 6095469f29aSMike Stump // Arrange for local static and local extern declarations to appear 6105469f29aSMike Stump // to be local to this function as well, as they are directly referenced 6115469f29aSMike Stump // in a block. 6125469f29aSMike Stump for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin(); 6135469f29aSMike Stump i != ldm.end(); 6145469f29aSMike Stump ++i) { 6155469f29aSMike Stump const VarDecl *VD = dyn_cast<VarDecl>(i->first); 6165469f29aSMike Stump 6170ca16601SDaniel Dunbar if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage()) 6185469f29aSMike Stump LocalDeclMap[VD] = i->second; 6195469f29aSMike Stump } 6205469f29aSMike Stump 62109a9b6e3SEli Friedman // FIXME: We need to rearrange the code for copy/dispose so we have this 62209a9b6e3SEli Friedman // sooner, so we can calculate offsets correctly. 62309a9b6e3SEli Friedman if (!BlockHasCopyDispose) 62409a9b6e3SEli Friedman BlockOffset = CGM.getTargetData() 62509a9b6e3SEli Friedman .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8; 62609a9b6e3SEli Friedman else 62709a9b6e3SEli Friedman BlockOffset = CGM.getTargetData() 62809a9b6e3SEli Friedman .getTypeStoreSizeInBits(CGM.getGenericExtendedBlockLiteralType()) / 8; 62909a9b6e3SEli Friedman BlockAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 63009a9b6e3SEli Friedman 631aae4349dSFariborz Jahanian const FunctionType *BlockFunctionType = BExpr->getFunctionType(); 632aae4349dSFariborz Jahanian QualType ResultType; 633aae4349dSFariborz Jahanian bool IsVariadic; 634a4f6b6b1SFariborz Jahanian if (const FunctionProtoType *FTy = 635a4f6b6b1SFariborz Jahanian dyn_cast<FunctionProtoType>(BlockFunctionType)) { 636aae4349dSFariborz Jahanian ResultType = FTy->getResultType(); 637aae4349dSFariborz Jahanian IsVariadic = FTy->isVariadic(); 638aae4349dSFariborz Jahanian } 639aae4349dSFariborz Jahanian else { 640aae4349dSFariborz Jahanian // K&R style block. 641aae4349dSFariborz Jahanian ResultType = BlockFunctionType->getResultType(); 642aae4349dSFariborz Jahanian IsVariadic = false; 643aae4349dSFariborz Jahanian } 6446a60fa24SAnders Carlsson 6456a60fa24SAnders Carlsson FunctionArgList Args; 6466a60fa24SAnders Carlsson 6473385fe12SChris Lattner const BlockDecl *BD = BExpr->getBlockDecl(); 6486a60fa24SAnders Carlsson 6496a60fa24SAnders Carlsson // FIXME: This leaks 6506a60fa24SAnders Carlsson ImplicitParamDecl *SelfDecl = 6516a60fa24SAnders Carlsson ImplicitParamDecl::Create(getContext(), 0, 6526a60fa24SAnders Carlsson SourceLocation(), 0, 6536a60fa24SAnders Carlsson getContext().getPointerType(getContext().VoidTy)); 6546a60fa24SAnders Carlsson 6556a60fa24SAnders Carlsson Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType())); 656cb2fbcb0SMike Stump BlockStructDecl = SelfDecl; 6576a60fa24SAnders Carlsson 658c4b30e59SSteve Naroff for (BlockDecl::param_const_iterator i = BD->param_begin(), 6596a60fa24SAnders Carlsson e = BD->param_end(); i != e; ++i) 660eecd39f2SMike Stump Args.push_back(std::make_pair(*i, (*i)->getType())); 6616a60fa24SAnders Carlsson 6626a60fa24SAnders Carlsson const CGFunctionInfo &FI = 663aae4349dSFariborz Jahanian CGM.getTypes().getFunctionInfo(ResultType, Args); 6646a60fa24SAnders Carlsson 6652d5a2878SMike Stump std::string Name = std::string("__") + Info.Name + "_block_invoke_"; 6666a60fa24SAnders Carlsson CodeGenTypes &Types = CGM.getTypes(); 667aae4349dSFariborz Jahanian const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic); 6686a60fa24SAnders Carlsson 6696a60fa24SAnders Carlsson llvm::Function *Fn = 6706a60fa24SAnders Carlsson llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 6716a60fa24SAnders Carlsson Name, 6726a60fa24SAnders Carlsson &CGM.getModule()); 6736a60fa24SAnders Carlsson 674c3e7cff6SDaniel Dunbar CGM.SetInternalFunctionAttributes(BD, Fn, FI); 675c3e7cff6SDaniel Dunbar 676324f80b1SChris Lattner StartFunction(BD, ResultType, Fn, Args, 6773385fe12SChris Lattner BExpr->getBody()->getLocEnd()); 678324f80b1SChris Lattner CurFuncDecl = OuterFuncDecl; 67928ec0cf8SChris Lattner CurCodeDecl = BD; 6803385fe12SChris Lattner EmitStmt(BExpr->getBody()); 6813385fe12SChris Lattner FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc()); 6826a60fa24SAnders Carlsson 683b750d928SMike Stump // The runtime needs a minimum alignment of a void *. 684b750d928SMike Stump uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 685b750d928SMike Stump BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign); 686b750d928SMike Stump 687cb2fbcb0SMike Stump Size = BlockOffset; 688b750d928SMike Stump Align = BlockAlign; 689b750d928SMike Stump subBlockDeclRefDecls = BlockDeclRefDecls; 6904446dcf0SMike Stump subBlockHasCopyDispose |= BlockHasCopyDispose; 6916a60fa24SAnders Carlsson return Fn; 6926a60fa24SAnders Carlsson } 6931db7d04bSMike Stump 694aeb0ffd5SMike Stump uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) { 6951db7d04bSMike Stump const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl()); 6961db7d04bSMike Stump 6971db7d04bSMike Stump uint64_t Size = getContext().getTypeSize(D->getType()) / 8; 6981db7d04bSMike Stump uint64_t Align = getContext().getDeclAlignInBytes(D); 6991db7d04bSMike Stump 7001db7d04bSMike Stump if (BDRE->isByRef()) { 7011db7d04bSMike Stump Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8; 7021db7d04bSMike Stump Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 7031db7d04bSMike Stump } 7041db7d04bSMike Stump 7051db7d04bSMike Stump assert ((Align > 0) && "alignment must be 1 byte or more"); 7061db7d04bSMike Stump 7071db7d04bSMike Stump uint64_t OldOffset = BlockOffset; 7081db7d04bSMike Stump 7091db7d04bSMike Stump // Ensure proper alignment, even if it means we have to have a gap 7101db7d04bSMike Stump BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align); 7111db7d04bSMike Stump BlockAlign = std::max(Align, BlockAlign); 7121db7d04bSMike Stump 7131db7d04bSMike Stump uint64_t Pad = BlockOffset - OldOffset; 7141db7d04bSMike Stump if (Pad) { 7151db7d04bSMike Stump llvm::ArrayType::get(llvm::Type::Int8Ty, Pad); 7161db7d04bSMike Stump QualType PadTy = getContext().getConstantArrayType(getContext().CharTy, 7171db7d04bSMike Stump llvm::APInt(32, Pad), 7181db7d04bSMike Stump ArrayType::Normal, 0); 7191db7d04bSMike Stump ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(), 7201db7d04bSMike Stump 0, QualType(PadTy), VarDecl::None, 7211db7d04bSMike Stump SourceLocation()); 7221db7d04bSMike Stump Expr *E; 7231db7d04bSMike Stump E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(), 7241db7d04bSMike Stump SourceLocation(), false, false); 7251db7d04bSMike Stump BlockDeclRefDecls.push_back(E); 7261db7d04bSMike Stump } 7271db7d04bSMike Stump BlockDeclRefDecls.push_back(BDRE); 7281db7d04bSMike Stump 7291db7d04bSMike Stump BlockOffset += Size; 7301db7d04bSMike Stump return BlockOffset-Size; 7311db7d04bSMike Stump } 73297d01d50SMike Stump 733aeb0ffd5SMike Stump llvm::Constant *BlockFunction:: 734aeb0ffd5SMike Stump GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T, 73567645930SMike Stump std::vector<HelperInfo> *NoteForHelperp) { 7360c743277SMike Stump QualType R = getContext().VoidTy; 7370c743277SMike Stump 7380c743277SMike Stump FunctionArgList Args; 7390c743277SMike Stump // FIXME: This leaks 740aeb0ffd5SMike Stump ImplicitParamDecl *Dst = 741aeb0ffd5SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 742aeb0ffd5SMike Stump getContext().getPointerType(getContext().VoidTy)); 743aeb0ffd5SMike Stump Args.push_back(std::make_pair(Dst, Dst->getType())); 7440c743277SMike Stump ImplicitParamDecl *Src = 7450c743277SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 7460c743277SMike Stump getContext().getPointerType(getContext().VoidTy)); 7470c743277SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 7480c743277SMike Stump 7490c743277SMike Stump const CGFunctionInfo &FI = 7500c743277SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 7510c743277SMike Stump 7520c743277SMike Stump std::string Name = std::string("__copy_helper_block_"); 7530c743277SMike Stump CodeGenTypes &Types = CGM.getTypes(); 7540c743277SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 7550c743277SMike Stump 7560c743277SMike Stump llvm::Function *Fn = 7570c743277SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 7580c743277SMike Stump Name, 7590c743277SMike Stump &CGM.getModule()); 7600c743277SMike Stump 7610c743277SMike Stump IdentifierInfo *II 7620c743277SMike Stump = &CGM.getContext().Idents.get("__copy_helper_block_"); 7630c743277SMike Stump 7640c743277SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 7650c743277SMike Stump getContext().getTranslationUnitDecl(), 7660c743277SMike Stump SourceLocation(), II, R, 7670c743277SMike Stump FunctionDecl::Static, false, 7680c743277SMike Stump true); 7690c743277SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 770aeb0ffd5SMike Stump 771aeb0ffd5SMike Stump llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); 772aeb0ffd5SMike Stump llvm::Type *PtrPtrT; 77367645930SMike Stump 77467645930SMike Stump if (NoteForHelperp) { 77567645930SMike Stump std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp; 77667645930SMike Stump 777aeb0ffd5SMike Stump PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 778aeb0ffd5SMike Stump SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); 779aeb0ffd5SMike Stump SrcObj = Builder.CreateLoad(SrcObj); 780aeb0ffd5SMike Stump 781aeb0ffd5SMike Stump llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst); 7823b65ac26SMike Stump llvm::Type *PtrPtrT; 7833b65ac26SMike Stump PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 7843b65ac26SMike Stump DstObj = Builder.CreateBitCast(DstObj, PtrPtrT); 7853b65ac26SMike Stump DstObj = Builder.CreateLoad(DstObj); 786aeb0ffd5SMike Stump 787aeb0ffd5SMike Stump for (unsigned i=0; i < NoteForHelper.size(); ++i) { 788aeb0ffd5SMike Stump int flag = NoteForHelper[i].flag; 789aeb0ffd5SMike Stump int index = NoteForHelper[i].index; 790aeb0ffd5SMike Stump 791aeb0ffd5SMike Stump if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) 792aeb0ffd5SMike Stump || NoteForHelper[i].RequiresCopying) { 793aeb0ffd5SMike Stump llvm::Value *Srcv = SrcObj; 794aeb0ffd5SMike Stump Srcv = Builder.CreateStructGEP(Srcv, index); 795aeb0ffd5SMike Stump Srcv = Builder.CreateBitCast(Srcv, 796aeb0ffd5SMike Stump llvm::PointerType::get(PtrToInt8Ty, 0)); 797aeb0ffd5SMike Stump Srcv = Builder.CreateLoad(Srcv); 798aeb0ffd5SMike Stump 799aeb0ffd5SMike Stump llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index); 800aeb0ffd5SMike Stump Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty); 801aeb0ffd5SMike Stump 802aeb0ffd5SMike Stump llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag); 803aeb0ffd5SMike Stump llvm::Value *F = getBlockObjectAssign(); 804aeb0ffd5SMike Stump Builder.CreateCall3(F, Dstv, Srcv, N); 805aeb0ffd5SMike Stump } 806aeb0ffd5SMike Stump } 80767645930SMike Stump } 808aeb0ffd5SMike Stump 8090c743277SMike Stump CGF.FinishFunction(); 8100c743277SMike Stump 8110c743277SMike Stump return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 81297d01d50SMike Stump } 81397d01d50SMike Stump 814d6ef62f0SMike Stump llvm::Constant *BlockFunction:: 815aeb0ffd5SMike Stump GenerateDestroyHelperFunction(bool BlockHasCopyDispose, 816aeb0ffd5SMike Stump const llvm::StructType* T, 81767645930SMike Stump std::vector<HelperInfo> *NoteForHelperp) { 8180c743277SMike Stump QualType R = getContext().VoidTy; 8190c743277SMike Stump 8200c743277SMike Stump FunctionArgList Args; 8210c743277SMike Stump // FIXME: This leaks 8220c743277SMike Stump ImplicitParamDecl *Src = 8230c743277SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 8240c743277SMike Stump getContext().getPointerType(getContext().VoidTy)); 8250c743277SMike Stump 8260c743277SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 8270c743277SMike Stump 8280c743277SMike Stump const CGFunctionInfo &FI = 8290c743277SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 8300c743277SMike Stump 8310c743277SMike Stump std::string Name = std::string("__destroy_helper_block_"); 8320c743277SMike Stump CodeGenTypes &Types = CGM.getTypes(); 8330c743277SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 8340c743277SMike Stump 8350c743277SMike Stump llvm::Function *Fn = 8360c743277SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 8370c743277SMike Stump Name, 8380c743277SMike Stump &CGM.getModule()); 8390c743277SMike Stump 8400c743277SMike Stump IdentifierInfo *II 8410c743277SMike Stump = &CGM.getContext().Idents.get("__destroy_helper_block_"); 8420c743277SMike Stump 8430c743277SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 8440c743277SMike Stump getContext().getTranslationUnitDecl(), 8450c743277SMike Stump SourceLocation(), II, R, 8460c743277SMike Stump FunctionDecl::Static, false, 8470c743277SMike Stump true); 8480c743277SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 8496f7d9f87SMike Stump 85067645930SMike Stump if (NoteForHelperp) { 85167645930SMike Stump std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp; 85267645930SMike Stump 8536f7d9f87SMike Stump llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); 8546f7d9f87SMike Stump llvm::Type *PtrPtrT; 8556f7d9f87SMike Stump PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 8566f7d9f87SMike Stump SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); 8576f7d9f87SMike Stump SrcObj = Builder.CreateLoad(SrcObj); 8586f7d9f87SMike Stump 8596f7d9f87SMike Stump for (unsigned i=0; i < NoteForHelper.size(); ++i) { 8606f7d9f87SMike Stump int flag = NoteForHelper[i].flag; 8616f7d9f87SMike Stump int index = NoteForHelper[i].index; 8626f7d9f87SMike Stump 8636f7d9f87SMike Stump if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) 8646f7d9f87SMike Stump || NoteForHelper[i].RequiresCopying) { 8656f7d9f87SMike Stump llvm::Value *Srcv = SrcObj; 8666f7d9f87SMike Stump Srcv = Builder.CreateStructGEP(Srcv, index); 8676f7d9f87SMike Stump Srcv = Builder.CreateBitCast(Srcv, 8686f7d9f87SMike Stump llvm::PointerType::get(PtrToInt8Ty, 0)); 8696f7d9f87SMike Stump Srcv = Builder.CreateLoad(Srcv); 8706f7d9f87SMike Stump 8716f7d9f87SMike Stump BuildBlockRelease(Srcv, flag); 8726f7d9f87SMike Stump } 8736f7d9f87SMike Stump } 87467645930SMike Stump } 8756f7d9f87SMike Stump 8760c743277SMike Stump CGF.FinishFunction(); 8770c743277SMike Stump 8780c743277SMike Stump return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 8790c743277SMike Stump } 8800c743277SMike Stump 881aeb0ffd5SMike Stump llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T, 88267645930SMike Stump std::vector<HelperInfo> *NoteForHelper) { 883aeb0ffd5SMike Stump return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose, 884aeb0ffd5SMike Stump T, NoteForHelper); 8850c743277SMike Stump } 8860c743277SMike Stump 887aeb0ffd5SMike Stump llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T, 88867645930SMike Stump std::vector<HelperInfo> *NoteForHelperp) { 889aeb0ffd5SMike Stump return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose, 89067645930SMike Stump T, NoteForHelperp); 89197d01d50SMike Stump } 892626aecc4SMike Stump 893f89230d9SMike Stump llvm::Constant *BlockFunction:: 894f89230d9SMike Stump GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) { 895ee2a5ee5SMike Stump QualType R = getContext().VoidTy; 896ee2a5ee5SMike Stump 897ee2a5ee5SMike Stump FunctionArgList Args; 898ee2a5ee5SMike Stump // FIXME: This leaks 899f89230d9SMike Stump ImplicitParamDecl *Dst = 900f89230d9SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 901f89230d9SMike Stump getContext().getPointerType(getContext().VoidTy)); 902f89230d9SMike Stump Args.push_back(std::make_pair(Dst, Dst->getType())); 903f89230d9SMike Stump 904f89230d9SMike Stump // FIXME: This leaks 905ee2a5ee5SMike Stump ImplicitParamDecl *Src = 906ee2a5ee5SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 907ee2a5ee5SMike Stump getContext().getPointerType(getContext().VoidTy)); 908ee2a5ee5SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 909ee2a5ee5SMike Stump 910ee2a5ee5SMike Stump const CGFunctionInfo &FI = 911ee2a5ee5SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 912ee2a5ee5SMike Stump 913ee2a5ee5SMike Stump std::string Name = std::string("__Block_byref_id_object_copy_"); 914ee2a5ee5SMike Stump CodeGenTypes &Types = CGM.getTypes(); 915ee2a5ee5SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 916ee2a5ee5SMike Stump 917ee2a5ee5SMike Stump llvm::Function *Fn = 918ee2a5ee5SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 919ee2a5ee5SMike Stump Name, 920ee2a5ee5SMike Stump &CGM.getModule()); 921ee2a5ee5SMike Stump 922ee2a5ee5SMike Stump IdentifierInfo *II 923ee2a5ee5SMike Stump = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_"); 924ee2a5ee5SMike Stump 925ee2a5ee5SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 926ee2a5ee5SMike Stump getContext().getTranslationUnitDecl(), 927ee2a5ee5SMike Stump SourceLocation(), II, R, 928ee2a5ee5SMike Stump FunctionDecl::Static, false, 929ee2a5ee5SMike Stump true); 930ee2a5ee5SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 931f89230d9SMike Stump 932f89230d9SMike Stump // dst->x 933f89230d9SMike Stump llvm::Value *V = CGF.GetAddrOfLocalVar(Dst); 9343b65ac26SMike Stump V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0)); 9353b65ac26SMike Stump V = Builder.CreateLoad(V); 936f89230d9SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 937f89230d9SMike Stump llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty); 938f89230d9SMike Stump 939f89230d9SMike Stump // src->x 940f89230d9SMike Stump V = CGF.GetAddrOfLocalVar(Src); 941f89230d9SMike Stump V = Builder.CreateLoad(V); 942f89230d9SMike Stump V = Builder.CreateBitCast(V, T); 943f89230d9SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 944f89230d9SMike Stump V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); 945f89230d9SMike Stump llvm::Value *SrcObj = Builder.CreateLoad(V); 946f89230d9SMike Stump 947f89230d9SMike Stump flag |= BLOCK_BYREF_CALLER; 948f89230d9SMike Stump 949f89230d9SMike Stump llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag); 950f89230d9SMike Stump llvm::Value *F = getBlockObjectAssign(); 951f89230d9SMike Stump Builder.CreateCall3(F, DstObj, SrcObj, N); 952f89230d9SMike Stump 953ee2a5ee5SMike Stump CGF.FinishFunction(); 954ee2a5ee5SMike Stump 955ee2a5ee5SMike Stump return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 956ee2a5ee5SMike Stump } 957ee2a5ee5SMike Stump 958fbe25dd1SMike Stump llvm::Constant * 959fbe25dd1SMike Stump BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T, 960fbe25dd1SMike Stump int flag) { 961ee2a5ee5SMike Stump QualType R = getContext().VoidTy; 962ee2a5ee5SMike Stump 963ee2a5ee5SMike Stump FunctionArgList Args; 964ee2a5ee5SMike Stump // FIXME: This leaks 965ee2a5ee5SMike Stump ImplicitParamDecl *Src = 966ee2a5ee5SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 967ee2a5ee5SMike Stump getContext().getPointerType(getContext().VoidTy)); 968ee2a5ee5SMike Stump 969ee2a5ee5SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 970ee2a5ee5SMike Stump 971ee2a5ee5SMike Stump const CGFunctionInfo &FI = 972ee2a5ee5SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 973ee2a5ee5SMike Stump 974ee2a5ee5SMike Stump std::string Name = std::string("__Block_byref_id_object_dispose_"); 975ee2a5ee5SMike Stump CodeGenTypes &Types = CGM.getTypes(); 976ee2a5ee5SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 977ee2a5ee5SMike Stump 978ee2a5ee5SMike Stump llvm::Function *Fn = 979ee2a5ee5SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 980ee2a5ee5SMike Stump Name, 981ee2a5ee5SMike Stump &CGM.getModule()); 982ee2a5ee5SMike Stump 983ee2a5ee5SMike Stump IdentifierInfo *II 984ee2a5ee5SMike Stump = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_"); 985ee2a5ee5SMike Stump 986ee2a5ee5SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 987ee2a5ee5SMike Stump getContext().getTranslationUnitDecl(), 988ee2a5ee5SMike Stump SourceLocation(), II, R, 989ee2a5ee5SMike Stump FunctionDecl::Static, false, 990ee2a5ee5SMike Stump true); 991ee2a5ee5SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 992fbe25dd1SMike Stump 993fbe25dd1SMike Stump llvm::Value *V = CGF.GetAddrOfLocalVar(Src); 9943b65ac26SMike Stump V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0)); 9953b65ac26SMike Stump V = Builder.CreateLoad(V); 996fbe25dd1SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 9973b65ac26SMike Stump V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); 9983b65ac26SMike Stump V = Builder.CreateLoad(V); 999fbe25dd1SMike Stump 1000fbe25dd1SMike Stump flag |= BLOCK_BYREF_CALLER; 1001fbe25dd1SMike Stump BuildBlockRelease(V, flag); 1002ee2a5ee5SMike Stump CGF.FinishFunction(); 1003ee2a5ee5SMike Stump 1004ee2a5ee5SMike Stump return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 1005ee2a5ee5SMike Stump } 1006ee2a5ee5SMike Stump 1007f89230d9SMike Stump llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T, 1008f89230d9SMike Stump int flag) { 1009f89230d9SMike Stump return CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag); 1010ee2a5ee5SMike Stump } 1011ee2a5ee5SMike Stump 1012fbe25dd1SMike Stump llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T, 1013fbe25dd1SMike Stump int flag) { 1014fbe25dd1SMike Stump return CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag); 1015ee2a5ee5SMike Stump } 1016ee2a5ee5SMike Stump 1017626aecc4SMike Stump llvm::Value *BlockFunction::getBlockObjectDispose() { 1018626aecc4SMike Stump if (CGM.BlockObjectDispose == 0) { 1019626aecc4SMike Stump const llvm::FunctionType *FTy; 1020626aecc4SMike Stump std::vector<const llvm::Type*> ArgTys; 1021626aecc4SMike Stump const llvm::Type *ResultType = llvm::Type::VoidTy; 1022626aecc4SMike Stump ArgTys.push_back(PtrToInt8Ty); 1023626aecc4SMike Stump ArgTys.push_back(llvm::Type::Int32Ty); 1024626aecc4SMike Stump FTy = llvm::FunctionType::get(ResultType, ArgTys, false); 1025626aecc4SMike Stump CGM.BlockObjectDispose 1026626aecc4SMike Stump = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose"); 1027626aecc4SMike Stump } 1028626aecc4SMike Stump return CGM.BlockObjectDispose; 1029626aecc4SMike Stump } 1030626aecc4SMike Stump 1031f89230d9SMike Stump llvm::Value *BlockFunction::getBlockObjectAssign() { 1032f89230d9SMike Stump if (CGM.BlockObjectAssign == 0) { 1033f89230d9SMike Stump const llvm::FunctionType *FTy; 1034f89230d9SMike Stump std::vector<const llvm::Type*> ArgTys; 1035f89230d9SMike Stump const llvm::Type *ResultType = llvm::Type::VoidTy; 1036f89230d9SMike Stump ArgTys.push_back(PtrToInt8Ty); 1037f89230d9SMike Stump ArgTys.push_back(PtrToInt8Ty); 1038f89230d9SMike Stump ArgTys.push_back(llvm::Type::Int32Ty); 1039f89230d9SMike Stump FTy = llvm::FunctionType::get(ResultType, ArgTys, false); 1040f89230d9SMike Stump CGM.BlockObjectAssign 1041f89230d9SMike Stump = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign"); 1042f89230d9SMike Stump } 1043f89230d9SMike Stump return CGM.BlockObjectAssign; 1044f89230d9SMike Stump } 1045f89230d9SMike Stump 1046fbe25dd1SMike Stump void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) { 1047626aecc4SMike Stump llvm::Value *F = getBlockObjectDispose(); 1048fbe25dd1SMike Stump llvm::Value *N; 1049626aecc4SMike Stump V = Builder.CreateBitCast(V, PtrToInt8Ty); 1050fbe25dd1SMike Stump N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag); 1051626aecc4SMike Stump Builder.CreateCall2(F, V, N); 1052626aecc4SMike Stump } 10534446dcf0SMike Stump 10544446dcf0SMike Stump ASTContext &BlockFunction::getContext() const { return CGM.getContext(); } 1055aeb0ffd5SMike Stump 1056aeb0ffd5SMike Stump BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf, 1057aeb0ffd5SMike Stump CGBuilderTy &B) 1058aeb0ffd5SMike Stump : CGM(cgm), CGF(cgf), Builder(B) { 1059aeb0ffd5SMike Stump PtrToInt8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 1060aeb0ffd5SMike Stump 1061aeb0ffd5SMike Stump BlockHasCopyDispose = false; 1062aeb0ffd5SMike Stump } 1063