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 142e722b91SMike Stump #include "CGDebugInfo.h" 152437cbfaSAnders Carlsson #include "CodeGenFunction.h" 162437cbfaSAnders Carlsson #include "CodeGenModule.h" 17692c6e37SMike Stump #include "clang/AST/DeclObjC.h" 182437cbfaSAnders Carlsson #include "llvm/Module.h" 196a60fa24SAnders Carlsson #include "llvm/Target/TargetData.h" 202437cbfaSAnders Carlsson #include <algorithm> 21db714924STorok Edwin #include <cstdio> 22db714924STorok Edwin 232437cbfaSAnders Carlsson using namespace clang; 242437cbfaSAnders Carlsson using namespace CodeGen; 252437cbfaSAnders Carlsson 26d6ef62f0SMike Stump llvm::Constant *CodeGenFunction:: 27d2142cffSMike Stump BuildDescriptorBlockDecl(bool BlockHasCopyDispose, uint64_t Size, 28d2142cffSMike Stump const llvm::StructType* Ty, 29aeb0ffd5SMike Stump std::vector<HelperInfo> *NoteForHelper) { 30c2c38331SMike Stump const llvm::Type *UnsignedLongTy 31c2c38331SMike Stump = CGM.getTypes().ConvertType(getContext().UnsignedLongTy); 3285284bacSMike Stump llvm::Constant *C; 3385284bacSMike Stump std::vector<llvm::Constant*> Elts; 3485284bacSMike Stump 3585284bacSMike Stump // reserved 36b7a2fe6fSOwen Anderson C = llvm::ConstantInt::get(UnsignedLongTy, 0); 3785284bacSMike Stump Elts.push_back(C); 3885284bacSMike Stump 3985284bacSMike Stump // Size 402ac40a9fSMike Stump // FIXME: What is the right way to say this doesn't fit? We should give 412ac40a9fSMike Stump // a user diagnostic in that case. Better fix would be to change the 422ac40a9fSMike Stump // API to size_t. 43b7a2fe6fSOwen Anderson C = llvm::ConstantInt::get(UnsignedLongTy, Size); 4485284bacSMike Stump Elts.push_back(C); 4585284bacSMike Stump 4685284bacSMike Stump if (BlockHasCopyDispose) { 4785284bacSMike Stump // copy_func_helper_decl 4867645930SMike Stump Elts.push_back(BuildCopyHelper(Ty, NoteForHelper)); 4985284bacSMike Stump 5085284bacSMike Stump // destroy_func_decl 5167645930SMike Stump Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper)); 5285284bacSMike Stump } 5385284bacSMike Stump 5441eaf0acSNick Lewycky C = llvm::ConstantStruct::get(VMContext, Elts, false); 5585284bacSMike Stump 56c10c8d3eSOwen Anderson C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, 5785284bacSMike Stump llvm::GlobalValue::InternalLinkage, 58c10c8d3eSOwen Anderson C, "__block_descriptor_tmp"); 5985284bacSMike Stump return C; 6085284bacSMike Stump } 6185284bacSMike Stump 6295435674SMike Stump llvm::Constant *BlockModule::getNSConcreteGlobalBlock() { 6346813bbfSChris Lattner if (NSConcreteGlobalBlock == 0) 6446813bbfSChris Lattner NSConcreteGlobalBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty, 6546813bbfSChris Lattner "_NSConcreteGlobalBlock"); 66971f9b66SMike Stump return NSConcreteGlobalBlock; 67971f9b66SMike Stump } 68971f9b66SMike Stump 6995435674SMike Stump llvm::Constant *BlockModule::getNSConcreteStackBlock() { 7046813bbfSChris Lattner if (NSConcreteStackBlock == 0) 7146813bbfSChris Lattner NSConcreteStackBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty, 7246813bbfSChris Lattner "_NSConcreteStackBlock"); 737ab278dbSMike Stump return NSConcreteStackBlock; 747ab278dbSMike Stump } 757ab278dbSMike Stump 76ed5e69feSAnders Carlsson static void CollectBlockDeclRefInfo(const Stmt *S, 77ed5e69feSAnders Carlsson CodeGenFunction::BlockInfo &Info) { 78ed5e69feSAnders Carlsson for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); 79ed5e69feSAnders Carlsson I != E; ++I) 80dd942713SDaniel Dunbar if (*I) 81ed5e69feSAnders Carlsson CollectBlockDeclRefInfo(*I, Info); 82ed5e69feSAnders Carlsson 83ed5e69feSAnders Carlsson if (const BlockDeclRefExpr *DE = dyn_cast<BlockDeclRefExpr>(S)) { 84ed5e69feSAnders Carlsson // FIXME: Handle enums. 85ed5e69feSAnders Carlsson if (isa<FunctionDecl>(DE->getDecl())) 86ed5e69feSAnders Carlsson return; 87ed5e69feSAnders Carlsson 88ed5e69feSAnders Carlsson if (DE->isByRef()) 89ed5e69feSAnders Carlsson Info.ByRefDeclRefs.push_back(DE); 90ed5e69feSAnders Carlsson else 91ed5e69feSAnders Carlsson Info.ByCopyDeclRefs.push_back(DE); 92ed5e69feSAnders Carlsson } 93ed5e69feSAnders Carlsson } 94ed5e69feSAnders Carlsson 952d33d630SMike Stump /// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be 962d33d630SMike Stump /// declared as a global variable instead of on the stack. 9746813bbfSChris Lattner static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info) { 98ed5e69feSAnders Carlsson return Info.ByRefDeclRefs.empty() && Info.ByCopyDeclRefs.empty(); 99ed5e69feSAnders Carlsson } 100ed5e69feSAnders Carlsson 1012d33d630SMike Stump // FIXME: Push most into CGM, passing down a few bits, like current function 1022d33d630SMike Stump // name. 103b750d928SMike Stump llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) { 104ed5e69feSAnders Carlsson 105ed5e69feSAnders Carlsson std::string Name = CurFn->getName(); 106ed5e69feSAnders Carlsson CodeGenFunction::BlockInfo Info(0, Name.c_str()); 107ed5e69feSAnders Carlsson CollectBlockDeclRefInfo(BE->getBody(), Info); 108ed5e69feSAnders Carlsson 109ed5e69feSAnders Carlsson // Check if the block can be global. 1102d33d630SMike Stump // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like 1112d33d630SMike Stump // to just have one code path. We should move this function into CGM and pass 1122d33d630SMike Stump // CGF, then we can just check to see if CGF is 0. 11397d01d50SMike Stump if (0 && CanBlockBeGlobal(Info)) 114ed5e69feSAnders Carlsson return CGM.GetAddrOfGlobalBlock(BE, Name.c_str()); 11585284bacSMike Stump 1164446dcf0SMike Stump std::vector<llvm::Constant*> Elts(5); 11785284bacSMike Stump llvm::Constant *C; 118b750d928SMike Stump llvm::Value *V; 11985284bacSMike Stump 12085284bacSMike Stump { 12185284bacSMike Stump // C = BuildBlockStructInitlist(); 12285284bacSMike Stump unsigned int flags = BLOCK_HAS_DESCRIPTOR; 12385284bacSMike Stump 1244446dcf0SMike Stump // We run this first so that we set BlockHasCopyDispose from the entire 1254446dcf0SMike Stump // block literal. 1264446dcf0SMike Stump // __invoke 1274446dcf0SMike Stump uint64_t subBlockSize, subBlockAlign; 1284446dcf0SMike Stump llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; 129d2142cffSMike Stump bool subBlockHasCopyDispose = false; 1304446dcf0SMike Stump llvm::Function *Fn 131d2a3e617SMike Stump = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl, 132d2a3e617SMike Stump LocalDeclMap, 1335469f29aSMike Stump subBlockSize, 1344446dcf0SMike Stump subBlockAlign, 1354446dcf0SMike Stump subBlockDeclRefDecls, 136d2142cffSMike Stump subBlockHasCopyDispose); 137d2142cffSMike Stump BlockHasCopyDispose |= subBlockHasCopyDispose; 1384446dcf0SMike Stump Elts[3] = Fn; 1394446dcf0SMike Stump 14018bb9284SMike Stump // FIXME: Don't use BlockHasCopyDispose, it is set more often then 14118bb9284SMike Stump // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); } 142d2142cffSMike Stump if (subBlockHasCopyDispose) 14385284bacSMike Stump flags |= BLOCK_HAS_COPY_DISPOSE; 14485284bacSMike Stump 145499ae7ecSMike Stump // __isa 1467ab278dbSMike Stump C = CGM.getNSConcreteStackBlock(); 147ade90fd1SOwen Anderson C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty); 1484446dcf0SMike Stump Elts[0] = C; 14985284bacSMike Stump 15085284bacSMike Stump // __flags 15185284bacSMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 15285284bacSMike Stump CGM.getTypes().ConvertType(CGM.getContext().IntTy)); 153b7a2fe6fSOwen Anderson C = llvm::ConstantInt::get(IntTy, flags); 1544446dcf0SMike Stump Elts[1] = C; 15585284bacSMike Stump 15685284bacSMike Stump // __reserved 157b7a2fe6fSOwen Anderson C = llvm::ConstantInt::get(IntTy, 0); 1584446dcf0SMike Stump Elts[2] = C; 15985284bacSMike Stump 160b750d928SMike Stump if (subBlockDeclRefDecls.size() == 0) { 161d6ef62f0SMike Stump // __descriptor 162d2142cffSMike Stump Elts[4] = BuildDescriptorBlockDecl(subBlockHasCopyDispose, subBlockSize, 0, 0); 163d6ef62f0SMike Stump 1643a139f37SMike Stump // Optimize to being a global block. 1653a139f37SMike Stump Elts[0] = CGM.getNSConcreteGlobalBlock(); 166b7a2fe6fSOwen Anderson Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL); 1673a139f37SMike Stump 16841eaf0acSNick Lewycky C = llvm::ConstantStruct::get(VMContext, Elts, false); 16985284bacSMike Stump 17085284bacSMike Stump char Name[32]; 1719319db8aSMike Stump sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount()); 172c10c8d3eSOwen Anderson C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, 17385284bacSMike Stump llvm::GlobalValue::InternalLinkage, 174c10c8d3eSOwen Anderson C, Name); 1755d2534adSMike Stump QualType BPT = BE->getType(); 176ade90fd1SOwen Anderson C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT)); 17785284bacSMike Stump return C; 17885284bacSMike Stump } 17985284bacSMike Stump 180b750d928SMike Stump std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size()); 181aeb0ffd5SMike Stump for (int i=0; i<4; ++i) 182b750d928SMike Stump Types[i] = Elts[i]->getType(); 183aeb0ffd5SMike Stump Types[4] = PtrToInt8Ty; 18485284bacSMike Stump 1851db7d04bSMike Stump for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) { 1861db7d04bSMike Stump const Expr *E = subBlockDeclRefDecls[i]; 1871db7d04bSMike Stump const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); 1881db7d04bSMike Stump QualType Ty = E->getType(); 18997d01d50SMike Stump if (BDRE && BDRE->isByRef()) { 19071d1d92dSAnders Carlsson Types[i+5] = llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0); 19197d01d50SMike Stump } else 1921db7d04bSMike Stump Types[i+5] = ConvertType(Ty); 1931db7d04bSMike Stump } 194b750d928SMike Stump 195758428f4SOwen Anderson llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true); 196b750d928SMike Stump 197b750d928SMike Stump llvm::AllocaInst *A = CreateTempAlloca(Ty); 198b750d928SMike Stump A->setAlignment(subBlockAlign); 199b750d928SMike Stump V = A; 200b750d928SMike Stump 201aeb0ffd5SMike Stump std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size()); 202aeb0ffd5SMike Stump int helpersize = 0; 203aeb0ffd5SMike Stump 204aeb0ffd5SMike Stump for (unsigned i=0; i<4; ++i) 205b750d928SMike Stump Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp")); 206b750d928SMike Stump 207b750d928SMike Stump for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) 208b750d928SMike Stump { 2091db7d04bSMike Stump // FIXME: Push const down. 2101db7d04bSMike Stump Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]); 2111db7d04bSMike Stump DeclRefExpr *DR; 2121db7d04bSMike Stump ValueDecl *VD; 213b750d928SMike Stump 2141db7d04bSMike Stump DR = dyn_cast<DeclRefExpr>(E); 2151db7d04bSMike Stump // Skip padding. 2161db7d04bSMike Stump if (DR) continue; 2171db7d04bSMike Stump 2181db7d04bSMike Stump BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); 2191db7d04bSMike Stump VD = BDRE->getDecl(); 2201db7d04bSMike Stump 22197d01d50SMike Stump llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp"); 222aeb0ffd5SMike Stump NoteForHelper[helpersize].index = i+5; 2232114d7c2SMike Stump NoteForHelper[helpersize].RequiresCopying 2242114d7c2SMike Stump = BlockRequiresCopying(VD->getType()); 225aeb0ffd5SMike Stump NoteForHelper[helpersize].flag 2262114d7c2SMike Stump = (VD->getType()->isBlockPointerType() 2272114d7c2SMike Stump ? BLOCK_FIELD_IS_BLOCK 2282114d7c2SMike Stump : BLOCK_FIELD_IS_OBJECT); 229aeb0ffd5SMike Stump 2301db7d04bSMike Stump if (LocalDeclMap[VD]) { 23197d01d50SMike Stump if (BDRE->isByRef()) { 232aeb0ffd5SMike Stump NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF | 23390d8daf5SMike Stump // FIXME: Someone double check this. 23490d8daf5SMike Stump (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0); 23597d01d50SMike Stump llvm::Value *Loc = LocalDeclMap[VD]; 23697d01d50SMike Stump Loc = Builder.CreateStructGEP(Loc, 1, "forwarding"); 23797d01d50SMike Stump Loc = Builder.CreateLoad(Loc, false); 23897d01d50SMike Stump Builder.CreateStore(Loc, Addr); 239aeb0ffd5SMike Stump ++helpersize; 24097d01d50SMike Stump continue; 24197d01d50SMike Stump } else 2421db7d04bSMike Stump E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD), 2431db7d04bSMike Stump VD->getType(), SourceLocation(), 2441db7d04bSMike Stump false, false); 2451db7d04bSMike Stump } 24697d01d50SMike Stump if (BDRE->isByRef()) { 247efd7caa8SMike Stump NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF | 248efd7caa8SMike Stump // FIXME: Someone double check this. 249efd7caa8SMike Stump (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0); 2501db7d04bSMike Stump E = new (getContext()) 2511db7d04bSMike Stump UnaryOperator(E, UnaryOperator::AddrOf, 2521db7d04bSMike Stump getContext().getPointerType(E->getType()), 2531db7d04bSMike Stump SourceLocation()); 25497d01d50SMike Stump } 255aeb0ffd5SMike Stump ++helpersize; 2561db7d04bSMike Stump 2571db7d04bSMike Stump RValue r = EmitAnyExpr(E, Addr, false); 25897d01d50SMike Stump if (r.isScalar()) { 25997d01d50SMike Stump llvm::Value *Loc = r.getScalarVal(); 26097d01d50SMike Stump const llvm::Type *Ty = Types[i+5]; 26197d01d50SMike Stump if (BDRE->isByRef()) { 2622d33d630SMike Stump // E is now the address of the value field, instead, we want the 2632d33d630SMike Stump // address of the actual ByRef struct. We optimize this slightly 2642d33d630SMike Stump // compared to gcc by not grabbing the forwarding slot as this must 2652d33d630SMike Stump // be done during Block_copy for us, and we can postpone the work 2662d33d630SMike Stump // until then. 2672d33d630SMike Stump uint64_t offset = BlockDecls[BDRE->getDecl()]; 2682d33d630SMike Stump 2692d33d630SMike Stump llvm::Value *BlockLiteral = LoadBlockStruct(); 2702d33d630SMike Stump 2712d33d630SMike Stump Loc = Builder.CreateGEP(BlockLiteral, 27241a75027SOwen Anderson llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), 2732d33d630SMike Stump offset), 2742d33d630SMike Stump "block.literal"); 2759793f0e4SOwen Anderson Ty = llvm::PointerType::get(Ty, 0); 27697d01d50SMike Stump Loc = Builder.CreateBitCast(Loc, Ty); 2772d33d630SMike Stump Loc = Builder.CreateLoad(Loc, false); 2782d33d630SMike Stump // Loc = Builder.CreateBitCast(Loc, Ty); 27997d01d50SMike Stump } 28097d01d50SMike Stump Builder.CreateStore(Loc, Addr); 28197d01d50SMike Stump } else if (r.isComplex()) 282b750d928SMike Stump // FIXME: implement 283b750d928SMike Stump ErrorUnsupported(BE, "complex in block literal"); 284b750d928SMike Stump else if (r.isAggregate()) 285b750d928SMike Stump ; // Already created into the destination 286b750d928SMike Stump else 287b750d928SMike Stump assert (0 && "bad block variable"); 288b750d928SMike Stump // FIXME: Ensure that the offset created by the backend for 289b750d928SMike Stump // the struct matches the previously computed offset in BlockDecls. 290b750d928SMike Stump } 291aeb0ffd5SMike Stump NoteForHelper.resize(helpersize); 292d6ef62f0SMike Stump 293d6ef62f0SMike Stump // __descriptor 294d2142cffSMike Stump llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockHasCopyDispose, 295d2142cffSMike Stump subBlockSize, Ty, 296aeb0ffd5SMike Stump &NoteForHelper); 297aeb0ffd5SMike Stump Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty); 298aeb0ffd5SMike Stump Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp")); 299b750d928SMike Stump } 300b750d928SMike Stump 301b750d928SMike Stump QualType BPT = BE->getType(); 302b750d928SMike Stump return Builder.CreateBitCast(V, ConvertType(BPT)); 303b750d928SMike Stump } 30485284bacSMike Stump 30585284bacSMike Stump 30695435674SMike Stump const llvm::Type *BlockModule::getBlockDescriptorType() { 307650c932dSMike Stump if (BlockDescriptorType) 308650c932dSMike Stump return BlockDescriptorType; 3092437cbfaSAnders Carlsson 3102437cbfaSAnders Carlsson const llvm::Type *UnsignedLongTy = 311650c932dSMike Stump getTypes().ConvertType(getContext().UnsignedLongTy); 3122437cbfaSAnders Carlsson 3132437cbfaSAnders Carlsson // struct __block_descriptor { 3142437cbfaSAnders Carlsson // unsigned long reserved; 3152437cbfaSAnders Carlsson // unsigned long block_size; 3162437cbfaSAnders Carlsson // }; 317758428f4SOwen Anderson BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(), 318758428f4SOwen Anderson UnsignedLongTy, 3192437cbfaSAnders Carlsson UnsignedLongTy, 3202437cbfaSAnders Carlsson NULL); 3212437cbfaSAnders Carlsson 322650c932dSMike Stump getModule().addTypeName("struct.__block_descriptor", 323650c932dSMike Stump BlockDescriptorType); 3242437cbfaSAnders Carlsson 325650c932dSMike Stump return BlockDescriptorType; 3262437cbfaSAnders Carlsson } 3272437cbfaSAnders Carlsson 32895435674SMike Stump const llvm::Type *BlockModule::getGenericBlockLiteralType() { 329005c9a62SMike Stump if (GenericBlockLiteralType) 330005c9a62SMike Stump return GenericBlockLiteralType; 3312437cbfaSAnders Carlsson 3322437cbfaSAnders Carlsson const llvm::Type *BlockDescPtrTy = 3339793f0e4SOwen Anderson llvm::PointerType::getUnqual(getBlockDescriptorType()); 3342437cbfaSAnders Carlsson 33592bbd6d4SMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 33692bbd6d4SMike Stump getTypes().ConvertType(getContext().IntTy)); 33792bbd6d4SMike Stump 3382437cbfaSAnders Carlsson // struct __block_literal_generic { 3395d2534adSMike Stump // void *__isa; 3405d2534adSMike Stump // int __flags; 3415d2534adSMike Stump // int __reserved; 3425d2534adSMike Stump // void (*__invoke)(void *); 3435d2534adSMike Stump // struct __block_descriptor *__descriptor; 3442437cbfaSAnders Carlsson // }; 345758428f4SOwen Anderson GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(), 346758428f4SOwen Anderson PtrToInt8Ty, 34792bbd6d4SMike Stump IntTy, 34892bbd6d4SMike Stump IntTy, 349626aecc4SMike Stump PtrToInt8Ty, 3502437cbfaSAnders Carlsson BlockDescPtrTy, 3512437cbfaSAnders Carlsson NULL); 3522437cbfaSAnders Carlsson 353005c9a62SMike Stump getModule().addTypeName("struct.__block_literal_generic", 354005c9a62SMike Stump GenericBlockLiteralType); 3552437cbfaSAnders Carlsson 356005c9a62SMike Stump return GenericBlockLiteralType; 3572437cbfaSAnders Carlsson } 3582437cbfaSAnders Carlsson 35995435674SMike Stump const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() { 3605d2534adSMike Stump if (GenericExtendedBlockLiteralType) 3615d2534adSMike Stump return GenericExtendedBlockLiteralType; 3625d2534adSMike Stump 3635d2534adSMike Stump const llvm::Type *BlockDescPtrTy = 3649793f0e4SOwen Anderson llvm::PointerType::getUnqual(getBlockDescriptorType()); 3655d2534adSMike Stump 3665d2534adSMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 3675d2534adSMike Stump getTypes().ConvertType(getContext().IntTy)); 3685d2534adSMike Stump 3695d2534adSMike Stump // struct __block_literal_generic { 3705d2534adSMike Stump // void *__isa; 3715d2534adSMike Stump // int __flags; 3725d2534adSMike Stump // int __reserved; 3735d2534adSMike Stump // void (*__invoke)(void *); 3745d2534adSMike Stump // struct __block_descriptor *__descriptor; 3755d2534adSMike Stump // void *__copy_func_helper_decl; 3765d2534adSMike Stump // void *__destroy_func_decl; 3775d2534adSMike Stump // }; 378758428f4SOwen Anderson GenericExtendedBlockLiteralType = llvm::StructType::get(IntTy->getContext(), 379758428f4SOwen Anderson PtrToInt8Ty, 3805d2534adSMike Stump IntTy, 3815d2534adSMike Stump IntTy, 382626aecc4SMike Stump PtrToInt8Ty, 3835d2534adSMike Stump BlockDescPtrTy, 384626aecc4SMike Stump PtrToInt8Ty, 385626aecc4SMike Stump PtrToInt8Ty, 3865d2534adSMike Stump NULL); 3875d2534adSMike Stump 3885d2534adSMike Stump getModule().addTypeName("struct.__block_literal_extended_generic", 3895d2534adSMike Stump GenericExtendedBlockLiteralType); 3905d2534adSMike Stump 3915d2534adSMike Stump return GenericExtendedBlockLiteralType; 3925d2534adSMike Stump } 3935d2534adSMike Stump 3942114d7c2SMike Stump bool BlockFunction::BlockRequiresCopying(QualType Ty) { 3952114d7c2SMike Stump return CGM.BlockRequiresCopying(Ty); 3962114d7c2SMike Stump } 3972114d7c2SMike Stump 3986a60fa24SAnders Carlsson RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) { 3992437cbfaSAnders Carlsson const BlockPointerType *BPT = 400c23c7e6aSTed Kremenek E->getCallee()->getType()->getAs<BlockPointerType>(); 4012437cbfaSAnders Carlsson 4022437cbfaSAnders Carlsson llvm::Value *Callee = EmitScalarExpr(E->getCallee()); 4032437cbfaSAnders Carlsson 4042437cbfaSAnders Carlsson // Get a pointer to the generic block literal. 4052437cbfaSAnders Carlsson const llvm::Type *BlockLiteralTy = 4069793f0e4SOwen Anderson llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType()); 4072437cbfaSAnders Carlsson 4082437cbfaSAnders Carlsson // Bitcast the callee to a block literal. 4092437cbfaSAnders Carlsson llvm::Value *BlockLiteral = 4102437cbfaSAnders Carlsson Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal"); 4112437cbfaSAnders Carlsson 4122437cbfaSAnders Carlsson // Get the function pointer from the literal. 4132437cbfaSAnders Carlsson llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp"); 4142437cbfaSAnders Carlsson 4152437cbfaSAnders Carlsson BlockLiteral = 4162437cbfaSAnders Carlsson Builder.CreateBitCast(BlockLiteral, 417*abd5b90eSBenjamin Kramer llvm::Type::getInt8PtrTy(VMContext), 4182437cbfaSAnders Carlsson "tmp"); 4192437cbfaSAnders Carlsson 4202437cbfaSAnders Carlsson // Add the block literal. 4212437cbfaSAnders Carlsson QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy); 4222437cbfaSAnders Carlsson CallArgList Args; 4232437cbfaSAnders Carlsson Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy)); 4242437cbfaSAnders Carlsson 425479e6fc7SAnders Carlsson QualType FnType = BPT->getPointeeType(); 426479e6fc7SAnders Carlsson 4272437cbfaSAnders Carlsson // And the rest of the arguments. 4289dd450bbSJohn McCall EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(), 429479e6fc7SAnders Carlsson E->arg_begin(), E->arg_end()); 4302437cbfaSAnders Carlsson 4315f50c651SAnders Carlsson // Load the function. 4325f50c651SAnders Carlsson llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp"); 4335f50c651SAnders Carlsson 4349dd450bbSJohn McCall QualType ResultType = FnType->getAs<FunctionType>()->getResultType(); 435a60cbcdfSAnders Carlsson 436a60cbcdfSAnders Carlsson const CGFunctionInfo &FnInfo = 437a60cbcdfSAnders Carlsson CGM.getTypes().getFunctionInfo(ResultType, Args); 4385f50c651SAnders Carlsson 4395f50c651SAnders Carlsson // Cast the function pointer to the right type. 4405f50c651SAnders Carlsson const llvm::Type *BlockFTy = 441a60cbcdfSAnders Carlsson CGM.getTypes().GetFunctionType(FnInfo, false); 4425f50c651SAnders Carlsson 4439793f0e4SOwen Anderson const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy); 4445f50c651SAnders Carlsson Func = Builder.CreateBitCast(Func, BlockFTyPtr); 4455f50c651SAnders Carlsson 4462437cbfaSAnders Carlsson // And call the block. 447350da607SAnders Carlsson return EmitCall(FnInfo, Func, Args); 4482437cbfaSAnders Carlsson } 4496a60fa24SAnders Carlsson 45097d01d50SMike Stump llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) { 4510168f4b2SAnders Carlsson const ValueDecl *VD = E->getDecl(); 45297d01d50SMike Stump 4530168f4b2SAnders Carlsson uint64_t &offset = BlockDecls[VD]; 4540168f4b2SAnders Carlsson 45597d01d50SMike Stump 45697d01d50SMike Stump // See if we have already allocated an offset for this variable. 45797d01d50SMike Stump if (offset == 0) { 4584446dcf0SMike Stump // Don't run the expensive check, unless we have to. 4594446dcf0SMike Stump if (!BlockHasCopyDispose && BlockRequiresCopying(E->getType())) 4604446dcf0SMike Stump BlockHasCopyDispose = true; 46197d01d50SMike Stump // if not, allocate one now. 46297d01d50SMike Stump offset = getBlockOffset(E); 46397d01d50SMike Stump } 46497d01d50SMike Stump 46597d01d50SMike Stump llvm::Value *BlockLiteral = LoadBlockStruct(); 46697d01d50SMike Stump llvm::Value *V = Builder.CreateGEP(BlockLiteral, 46741a75027SOwen Anderson llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), 46897d01d50SMike Stump offset), 4692d33d630SMike Stump "block.literal"); 47097d01d50SMike Stump if (E->isByRef()) { 47197d01d50SMike Stump const llvm::Type *PtrStructTy 4720168f4b2SAnders Carlsson = llvm::PointerType::get(BuildByRefType(VD), 0); 473efd7caa8SMike Stump // The block literal will need a copy/destroy helper. 474efd7caa8SMike Stump BlockHasCopyDispose = true; 4750168f4b2SAnders Carlsson 4760168f4b2SAnders Carlsson const llvm::Type *Ty = PtrStructTy; 4779793f0e4SOwen Anderson Ty = llvm::PointerType::get(Ty, 0); 47897d01d50SMike Stump V = Builder.CreateBitCast(V, Ty); 47997d01d50SMike Stump V = Builder.CreateLoad(V, false); 48097d01d50SMike Stump V = Builder.CreateStructGEP(V, 1, "forwarding"); 48197d01d50SMike Stump V = Builder.CreateLoad(V, false); 48297d01d50SMike Stump V = Builder.CreateBitCast(V, PtrStructTy); 4830168f4b2SAnders Carlsson V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD), 4840168f4b2SAnders Carlsson VD->getNameAsString()); 48597d01d50SMike Stump } else { 4860168f4b2SAnders Carlsson const llvm::Type *Ty = CGM.getTypes().ConvertType(VD->getType()); 4870168f4b2SAnders Carlsson 4889793f0e4SOwen Anderson Ty = llvm::PointerType::get(Ty, 0); 48997d01d50SMike Stump V = Builder.CreateBitCast(V, Ty); 49097d01d50SMike Stump } 49197d01d50SMike Stump return V; 49297d01d50SMike Stump } 49397d01d50SMike Stump 494692c6e37SMike Stump void CodeGenFunction::BlockForwardSelf() { 495692c6e37SMike Stump const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); 496692c6e37SMike Stump ImplicitParamDecl *SelfDecl = OMD->getSelfDecl(); 497692c6e37SMike Stump llvm::Value *&DMEntry = LocalDeclMap[SelfDecl]; 498692c6e37SMike Stump if (DMEntry) 499692c6e37SMike Stump return; 500692c6e37SMike Stump // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care 501692c6e37SMike Stump BlockDeclRefExpr *BDRE = new (getContext()) 502692c6e37SMike Stump BlockDeclRefExpr(SelfDecl, 503692c6e37SMike Stump SelfDecl->getType(), SourceLocation(), false); 504692c6e37SMike Stump DMEntry = GetAddrOfBlockDecl(BDRE); 505692c6e37SMike Stump } 506692c6e37SMike Stump 5072d5a2878SMike Stump llvm::Constant * 5086c39666aSMike Stump BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) { 5096a60fa24SAnders Carlsson // Generate the block descriptor. 5106a60fa24SAnders Carlsson const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy); 51192bbd6d4SMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 51292bbd6d4SMike Stump getTypes().ConvertType(getContext().IntTy)); 5136a60fa24SAnders Carlsson 5146a60fa24SAnders Carlsson llvm::Constant *DescriptorFields[2]; 5156a60fa24SAnders Carlsson 5166a60fa24SAnders Carlsson // Reserved 5170b75f23bSOwen Anderson DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy); 5186a60fa24SAnders Carlsson 5196a60fa24SAnders Carlsson // Block literal size. For global blocks we just use the size of the generic 5206a60fa24SAnders Carlsson // block literal struct. 5216a60fa24SAnders Carlsson uint64_t BlockLiteralSize = 522005c9a62SMike Stump TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8; 523170229f6SOwen Anderson DescriptorFields[1] = 524b7a2fe6fSOwen Anderson llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize); 5256a60fa24SAnders Carlsson 5266a60fa24SAnders Carlsson llvm::Constant *DescriptorStruct = 52741eaf0acSNick Lewycky llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 2, false); 5286a60fa24SAnders Carlsson 5296a60fa24SAnders Carlsson llvm::GlobalVariable *Descriptor = 530c10c8d3eSOwen Anderson new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true, 5316a60fa24SAnders Carlsson llvm::GlobalVariable::InternalLinkage, 532c10c8d3eSOwen Anderson DescriptorStruct, "__block_descriptor_global"); 5336a60fa24SAnders Carlsson 5346a60fa24SAnders Carlsson // Generate the constants for the block literal. 5356a60fa24SAnders Carlsson llvm::Constant *LiteralFields[5]; 5366a60fa24SAnders Carlsson 5372d5a2878SMike Stump CodeGenFunction::BlockInfo Info(0, n); 538b750d928SMike Stump uint64_t subBlockSize, subBlockAlign; 5391db7d04bSMike Stump llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; 540a61bb4d1SDaniel Dunbar bool subBlockHasCopyDispose = false; 5415469f29aSMike Stump llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap; 542cb2fbcb0SMike Stump llvm::Function *Fn 543692c6e37SMike Stump = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap, 5445469f29aSMike Stump subBlockSize, 545b750d928SMike Stump subBlockAlign, 5464446dcf0SMike Stump subBlockDeclRefDecls, 5474446dcf0SMike Stump subBlockHasCopyDispose); 548cb2fbcb0SMike Stump assert(subBlockSize == BlockLiteralSize 549cb2fbcb0SMike Stump && "no imports allowed for global block"); 5506a60fa24SAnders Carlsson 5516a60fa24SAnders Carlsson // isa 552971f9b66SMike Stump LiteralFields[0] = getNSConcreteGlobalBlock(); 5536a60fa24SAnders Carlsson 5546a60fa24SAnders Carlsson // Flags 5555878c797SAnders Carlsson LiteralFields[1] = 556b7a2fe6fSOwen Anderson llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR); 5576a60fa24SAnders Carlsson 5586a60fa24SAnders Carlsson // Reserved 5590b75f23bSOwen Anderson LiteralFields[2] = llvm::Constant::getNullValue(IntTy); 5606a60fa24SAnders Carlsson 5616a60fa24SAnders Carlsson // Function 5626a60fa24SAnders Carlsson LiteralFields[3] = Fn; 5636a60fa24SAnders Carlsson 5646a60fa24SAnders Carlsson // Descriptor 5656a60fa24SAnders Carlsson LiteralFields[4] = Descriptor; 5666a60fa24SAnders Carlsson 5676a60fa24SAnders Carlsson llvm::Constant *BlockLiteralStruct = 56841eaf0acSNick Lewycky llvm::ConstantStruct::get(VMContext, &LiteralFields[0], 5, false); 5696a60fa24SAnders Carlsson 5706a60fa24SAnders Carlsson llvm::GlobalVariable *BlockLiteral = 571c10c8d3eSOwen Anderson new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true, 5726a60fa24SAnders Carlsson llvm::GlobalVariable::InternalLinkage, 573c10c8d3eSOwen Anderson BlockLiteralStruct, "__block_literal_global"); 5746a60fa24SAnders Carlsson 5756a60fa24SAnders Carlsson return BlockLiteral; 5766a60fa24SAnders Carlsson } 5776a60fa24SAnders Carlsson 578cb2fbcb0SMike Stump llvm::Value *CodeGenFunction::LoadBlockStruct() { 579cb2fbcb0SMike Stump return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self"); 580cb2fbcb0SMike Stump } 581cb2fbcb0SMike Stump 5824446dcf0SMike Stump llvm::Function * 5834446dcf0SMike Stump CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr, 584cb2fbcb0SMike Stump const BlockInfo& Info, 585692c6e37SMike Stump const Decl *OuterFuncDecl, 5865469f29aSMike Stump llvm::DenseMap<const Decl*, llvm::Value*> ldm, 587b750d928SMike Stump uint64_t &Size, 588b750d928SMike Stump uint64_t &Align, 5894446dcf0SMike Stump llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls, 5904446dcf0SMike Stump bool &subBlockHasCopyDispose) { 5919074ed8cSDevang Patel 5929074ed8cSDevang Patel // Check if we should generate debug info for this block. 5939074ed8cSDevang Patel if (CGM.getDebugInfo()) 5949074ed8cSDevang Patel DebugInfo = CGM.getDebugInfo(); 5959074ed8cSDevang Patel 5965469f29aSMike Stump // Arrange for local static and local extern declarations to appear 5975469f29aSMike Stump // to be local to this function as well, as they are directly referenced 5985469f29aSMike Stump // in a block. 5995469f29aSMike Stump for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin(); 6005469f29aSMike Stump i != ldm.end(); 6015469f29aSMike Stump ++i) { 6025469f29aSMike Stump const VarDecl *VD = dyn_cast<VarDecl>(i->first); 6035469f29aSMike Stump 6040ca16601SDaniel Dunbar if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage()) 6055469f29aSMike Stump LocalDeclMap[VD] = i->second; 6065469f29aSMike Stump } 6075469f29aSMike Stump 60809a9b6e3SEli Friedman // FIXME: We need to rearrange the code for copy/dispose so we have this 60909a9b6e3SEli Friedman // sooner, so we can calculate offsets correctly. 61009a9b6e3SEli Friedman if (!BlockHasCopyDispose) 61109a9b6e3SEli Friedman BlockOffset = CGM.getTargetData() 61209a9b6e3SEli Friedman .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8; 61309a9b6e3SEli Friedman else 61409a9b6e3SEli Friedman BlockOffset = CGM.getTargetData() 61509a9b6e3SEli Friedman .getTypeStoreSizeInBits(CGM.getGenericExtendedBlockLiteralType()) / 8; 61609a9b6e3SEli Friedman BlockAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 61709a9b6e3SEli Friedman 618aae4349dSFariborz Jahanian const FunctionType *BlockFunctionType = BExpr->getFunctionType(); 619aae4349dSFariborz Jahanian QualType ResultType; 620aae4349dSFariborz Jahanian bool IsVariadic; 621a4f6b6b1SFariborz Jahanian if (const FunctionProtoType *FTy = 622a4f6b6b1SFariborz Jahanian dyn_cast<FunctionProtoType>(BlockFunctionType)) { 623aae4349dSFariborz Jahanian ResultType = FTy->getResultType(); 624aae4349dSFariborz Jahanian IsVariadic = FTy->isVariadic(); 625658fe02dSMike Stump } else { 626aae4349dSFariborz Jahanian // K&R style block. 627aae4349dSFariborz Jahanian ResultType = BlockFunctionType->getResultType(); 628aae4349dSFariborz Jahanian IsVariadic = false; 629aae4349dSFariborz Jahanian } 6306a60fa24SAnders Carlsson 6316a60fa24SAnders Carlsson FunctionArgList Args; 6326a60fa24SAnders Carlsson 6333385fe12SChris Lattner const BlockDecl *BD = BExpr->getBlockDecl(); 6346a60fa24SAnders Carlsson 6356a60fa24SAnders Carlsson // FIXME: This leaks 6366a60fa24SAnders Carlsson ImplicitParamDecl *SelfDecl = 6376a60fa24SAnders Carlsson ImplicitParamDecl::Create(getContext(), 0, 6386a60fa24SAnders Carlsson SourceLocation(), 0, 6396a60fa24SAnders Carlsson getContext().getPointerType(getContext().VoidTy)); 6406a60fa24SAnders Carlsson 6416a60fa24SAnders Carlsson Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType())); 642cb2fbcb0SMike Stump BlockStructDecl = SelfDecl; 6436a60fa24SAnders Carlsson 644c4b30e59SSteve Naroff for (BlockDecl::param_const_iterator i = BD->param_begin(), 6456a60fa24SAnders Carlsson e = BD->param_end(); i != e; ++i) 646eecd39f2SMike Stump Args.push_back(std::make_pair(*i, (*i)->getType())); 6476a60fa24SAnders Carlsson 6486a60fa24SAnders Carlsson const CGFunctionInfo &FI = 649aae4349dSFariborz Jahanian CGM.getTypes().getFunctionInfo(ResultType, Args); 6506a60fa24SAnders Carlsson 6512d5a2878SMike Stump std::string Name = std::string("__") + Info.Name + "_block_invoke_"; 6526a60fa24SAnders Carlsson CodeGenTypes &Types = CGM.getTypes(); 653aae4349dSFariborz Jahanian const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic); 6546a60fa24SAnders Carlsson 6556a60fa24SAnders Carlsson llvm::Function *Fn = 6566a60fa24SAnders Carlsson llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 6576a60fa24SAnders Carlsson Name, 6586a60fa24SAnders Carlsson &CGM.getModule()); 6596a60fa24SAnders Carlsson 660c3e7cff6SDaniel Dunbar CGM.SetInternalFunctionAttributes(BD, Fn, FI); 661c3e7cff6SDaniel Dunbar 662324f80b1SChris Lattner StartFunction(BD, ResultType, Fn, Args, 6633385fe12SChris Lattner BExpr->getBody()->getLocEnd()); 6642e722b91SMike Stump 665324f80b1SChris Lattner CurFuncDecl = OuterFuncDecl; 66628ec0cf8SChris Lattner CurCodeDecl = BD; 667017460abSMike Stump 668017460abSMike Stump // Save a spot to insert the debug information for all the BlockDeclRefDecls. 669017460abSMike Stump llvm::BasicBlock *entry = Builder.GetInsertBlock(); 670017460abSMike Stump llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint(); 671017460abSMike Stump --entry_ptr; 672017460abSMike Stump 6733385fe12SChris Lattner EmitStmt(BExpr->getBody()); 674017460abSMike Stump 6757d69911aSMike Stump // Remember where we were... 6767d69911aSMike Stump llvm::BasicBlock *resume = Builder.GetInsertBlock(); 677017460abSMike Stump 6787d69911aSMike Stump // Go back to the entry. 679017460abSMike Stump ++entry_ptr; 680017460abSMike Stump Builder.SetInsertPoint(entry, entry_ptr); 681017460abSMike Stump 6822e722b91SMike Stump if (CGDebugInfo *DI = getDebugInfo()) { 6832e722b91SMike Stump // Emit debug information for all the BlockDeclRefDecls. 6842e722b91SMike Stump for (unsigned i=0; i < BlockDeclRefDecls.size(); ++i) { 6852e722b91SMike Stump const Expr *E = BlockDeclRefDecls[i]; 6862e722b91SMike Stump const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); 6872e722b91SMike Stump if (BDRE) { 6882e722b91SMike Stump const ValueDecl *D = BDRE->getDecl(); 6892e722b91SMike Stump DI->setLocation(D->getLocation()); 6902e722b91SMike Stump DI->EmitDeclareOfBlockDeclRefVariable(BDRE, 6912e722b91SMike Stump LocalDeclMap[getBlockStructDecl()], 6922e722b91SMike Stump Builder, this); 6932e722b91SMike Stump } 6942e722b91SMike Stump } 6952e722b91SMike Stump } 6967d69911aSMike Stump // And resume where we left off. 6977d69911aSMike Stump if (resume == 0) 6987d69911aSMike Stump Builder.ClearInsertionPoint(); 6997d69911aSMike Stump else 7007d69911aSMike Stump Builder.SetInsertPoint(resume); 7012e722b91SMike Stump 7023385fe12SChris Lattner FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc()); 7036a60fa24SAnders Carlsson 704b750d928SMike Stump // The runtime needs a minimum alignment of a void *. 705b750d928SMike Stump uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 706b750d928SMike Stump BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign); 707b750d928SMike Stump 708cb2fbcb0SMike Stump Size = BlockOffset; 709b750d928SMike Stump Align = BlockAlign; 710b750d928SMike Stump subBlockDeclRefDecls = BlockDeclRefDecls; 7114446dcf0SMike Stump subBlockHasCopyDispose |= BlockHasCopyDispose; 7126a60fa24SAnders Carlsson return Fn; 7136a60fa24SAnders Carlsson } 7141db7d04bSMike Stump 715aeb0ffd5SMike Stump uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) { 7161db7d04bSMike Stump const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl()); 7171db7d04bSMike Stump 7181db7d04bSMike Stump uint64_t Size = getContext().getTypeSize(D->getType()) / 8; 7191db7d04bSMike Stump uint64_t Align = getContext().getDeclAlignInBytes(D); 7201db7d04bSMike Stump 7211db7d04bSMike Stump if (BDRE->isByRef()) { 7221db7d04bSMike Stump Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8; 7231db7d04bSMike Stump Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 7241db7d04bSMike Stump } 7251db7d04bSMike Stump 7261db7d04bSMike Stump assert ((Align > 0) && "alignment must be 1 byte or more"); 7271db7d04bSMike Stump 7281db7d04bSMike Stump uint64_t OldOffset = BlockOffset; 7291db7d04bSMike Stump 7301db7d04bSMike Stump // Ensure proper alignment, even if it means we have to have a gap 7311db7d04bSMike Stump BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align); 7321db7d04bSMike Stump BlockAlign = std::max(Align, BlockAlign); 7331db7d04bSMike Stump 7341db7d04bSMike Stump uint64_t Pad = BlockOffset - OldOffset; 7351db7d04bSMike Stump if (Pad) { 73641a75027SOwen Anderson llvm::ArrayType::get(llvm::Type::getInt8Ty(VMContext), Pad); 7371db7d04bSMike Stump QualType PadTy = getContext().getConstantArrayType(getContext().CharTy, 7381db7d04bSMike Stump llvm::APInt(32, Pad), 7391db7d04bSMike Stump ArrayType::Normal, 0); 7401db7d04bSMike Stump ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(), 7416032ef1aSArgyrios Kyrtzidis 0, QualType(PadTy), 0, VarDecl::None); 7421db7d04bSMike Stump Expr *E; 7431db7d04bSMike Stump E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(), 7441db7d04bSMike Stump SourceLocation(), false, false); 7451db7d04bSMike Stump BlockDeclRefDecls.push_back(E); 7461db7d04bSMike Stump } 7471db7d04bSMike Stump BlockDeclRefDecls.push_back(BDRE); 7481db7d04bSMike Stump 7491db7d04bSMike Stump BlockOffset += Size; 7501db7d04bSMike Stump return BlockOffset-Size; 7511db7d04bSMike Stump } 75297d01d50SMike Stump 753aeb0ffd5SMike Stump llvm::Constant *BlockFunction:: 754aeb0ffd5SMike Stump GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T, 75567645930SMike Stump std::vector<HelperInfo> *NoteForHelperp) { 7560c743277SMike Stump QualType R = getContext().VoidTy; 7570c743277SMike Stump 7580c743277SMike Stump FunctionArgList Args; 7590c743277SMike Stump // FIXME: This leaks 760aeb0ffd5SMike Stump ImplicitParamDecl *Dst = 761aeb0ffd5SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 762aeb0ffd5SMike Stump getContext().getPointerType(getContext().VoidTy)); 763aeb0ffd5SMike Stump Args.push_back(std::make_pair(Dst, Dst->getType())); 7640c743277SMike Stump ImplicitParamDecl *Src = 7650c743277SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 7660c743277SMike Stump getContext().getPointerType(getContext().VoidTy)); 7670c743277SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 7680c743277SMike Stump 7690c743277SMike Stump const CGFunctionInfo &FI = 7700c743277SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 7710c743277SMike Stump 772cbc2bcafSMike Stump // FIXME: We'd like to put these into a mergable by content, with 773cbc2bcafSMike Stump // internal linkage. 7740c743277SMike Stump std::string Name = std::string("__copy_helper_block_"); 7750c743277SMike Stump CodeGenTypes &Types = CGM.getTypes(); 7760c743277SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 7770c743277SMike Stump 7780c743277SMike Stump llvm::Function *Fn = 7790c743277SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 7800c743277SMike Stump Name, 7810c743277SMike Stump &CGM.getModule()); 7820c743277SMike Stump 7830c743277SMike Stump IdentifierInfo *II 7840c743277SMike Stump = &CGM.getContext().Idents.get("__copy_helper_block_"); 7850c743277SMike Stump 7860c743277SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 7870c743277SMike Stump getContext().getTranslationUnitDecl(), 78860ed5604SArgyrios Kyrtzidis SourceLocation(), II, R, 0, 7890c743277SMike Stump FunctionDecl::Static, false, 7900c743277SMike Stump true); 7910c743277SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 792aeb0ffd5SMike Stump 793aeb0ffd5SMike Stump llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); 794aeb0ffd5SMike Stump llvm::Type *PtrPtrT; 79567645930SMike Stump 79667645930SMike Stump if (NoteForHelperp) { 79767645930SMike Stump std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp; 79867645930SMike Stump 7999793f0e4SOwen Anderson PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 800aeb0ffd5SMike Stump SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); 801aeb0ffd5SMike Stump SrcObj = Builder.CreateLoad(SrcObj); 802aeb0ffd5SMike Stump 803aeb0ffd5SMike Stump llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst); 8043b65ac26SMike Stump llvm::Type *PtrPtrT; 8059793f0e4SOwen Anderson PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 8063b65ac26SMike Stump DstObj = Builder.CreateBitCast(DstObj, PtrPtrT); 8073b65ac26SMike Stump DstObj = Builder.CreateLoad(DstObj); 808aeb0ffd5SMike Stump 809aeb0ffd5SMike Stump for (unsigned i=0; i < NoteForHelper.size(); ++i) { 810aeb0ffd5SMike Stump int flag = NoteForHelper[i].flag; 811aeb0ffd5SMike Stump int index = NoteForHelper[i].index; 812aeb0ffd5SMike Stump 813aeb0ffd5SMike Stump if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) 814aeb0ffd5SMike Stump || NoteForHelper[i].RequiresCopying) { 815aeb0ffd5SMike Stump llvm::Value *Srcv = SrcObj; 816aeb0ffd5SMike Stump Srcv = Builder.CreateStructGEP(Srcv, index); 817aeb0ffd5SMike Stump Srcv = Builder.CreateBitCast(Srcv, 8189793f0e4SOwen Anderson llvm::PointerType::get(PtrToInt8Ty, 0)); 819aeb0ffd5SMike Stump Srcv = Builder.CreateLoad(Srcv); 820aeb0ffd5SMike Stump 821aeb0ffd5SMike Stump llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index); 822aeb0ffd5SMike Stump Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty); 823aeb0ffd5SMike Stump 82441a75027SOwen Anderson llvm::Value *N = llvm::ConstantInt::get( 82541a75027SOwen Anderson llvm::Type::getInt32Ty(T->getContext()), flag); 826aeb0ffd5SMike Stump llvm::Value *F = getBlockObjectAssign(); 827aeb0ffd5SMike Stump Builder.CreateCall3(F, Dstv, Srcv, N); 828aeb0ffd5SMike Stump } 829aeb0ffd5SMike Stump } 83067645930SMike Stump } 831aeb0ffd5SMike Stump 8320c743277SMike Stump CGF.FinishFunction(); 8330c743277SMike Stump 834ade90fd1SOwen Anderson return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 83597d01d50SMike Stump } 83697d01d50SMike Stump 837d6ef62f0SMike Stump llvm::Constant *BlockFunction:: 838aeb0ffd5SMike Stump GenerateDestroyHelperFunction(bool BlockHasCopyDispose, 839aeb0ffd5SMike Stump const llvm::StructType* T, 84067645930SMike Stump std::vector<HelperInfo> *NoteForHelperp) { 8410c743277SMike Stump QualType R = getContext().VoidTy; 8420c743277SMike Stump 8430c743277SMike Stump FunctionArgList Args; 8440c743277SMike Stump // FIXME: This leaks 8450c743277SMike Stump ImplicitParamDecl *Src = 8460c743277SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 8470c743277SMike Stump getContext().getPointerType(getContext().VoidTy)); 8480c743277SMike Stump 8490c743277SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 8500c743277SMike Stump 8510c743277SMike Stump const CGFunctionInfo &FI = 8520c743277SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 8530c743277SMike Stump 854cbc2bcafSMike Stump // FIXME: We'd like to put these into a mergable by content, with 855cbc2bcafSMike Stump // internal linkage. 8560c743277SMike Stump std::string Name = std::string("__destroy_helper_block_"); 8570c743277SMike Stump CodeGenTypes &Types = CGM.getTypes(); 8580c743277SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 8590c743277SMike Stump 8600c743277SMike Stump llvm::Function *Fn = 8610c743277SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 8620c743277SMike Stump Name, 8630c743277SMike Stump &CGM.getModule()); 8640c743277SMike Stump 8650c743277SMike Stump IdentifierInfo *II 8660c743277SMike Stump = &CGM.getContext().Idents.get("__destroy_helper_block_"); 8670c743277SMike Stump 8680c743277SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 8690c743277SMike Stump getContext().getTranslationUnitDecl(), 87060ed5604SArgyrios Kyrtzidis SourceLocation(), II, R, 0, 8710c743277SMike Stump FunctionDecl::Static, false, 8720c743277SMike Stump true); 8730c743277SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 8746f7d9f87SMike Stump 87567645930SMike Stump if (NoteForHelperp) { 87667645930SMike Stump std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp; 87767645930SMike Stump 8786f7d9f87SMike Stump llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); 8796f7d9f87SMike Stump llvm::Type *PtrPtrT; 8809793f0e4SOwen Anderson PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 8816f7d9f87SMike Stump SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); 8826f7d9f87SMike Stump SrcObj = Builder.CreateLoad(SrcObj); 8836f7d9f87SMike Stump 8846f7d9f87SMike Stump for (unsigned i=0; i < NoteForHelper.size(); ++i) { 8856f7d9f87SMike Stump int flag = NoteForHelper[i].flag; 8866f7d9f87SMike Stump int index = NoteForHelper[i].index; 8876f7d9f87SMike Stump 8886f7d9f87SMike Stump if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) 8896f7d9f87SMike Stump || NoteForHelper[i].RequiresCopying) { 8906f7d9f87SMike Stump llvm::Value *Srcv = SrcObj; 8916f7d9f87SMike Stump Srcv = Builder.CreateStructGEP(Srcv, index); 8926f7d9f87SMike Stump Srcv = Builder.CreateBitCast(Srcv, 8939793f0e4SOwen Anderson llvm::PointerType::get(PtrToInt8Ty, 0)); 8946f7d9f87SMike Stump Srcv = Builder.CreateLoad(Srcv); 8956f7d9f87SMike Stump 8966f7d9f87SMike Stump BuildBlockRelease(Srcv, flag); 8976f7d9f87SMike Stump } 8986f7d9f87SMike Stump } 89967645930SMike Stump } 9006f7d9f87SMike Stump 9010c743277SMike Stump CGF.FinishFunction(); 9020c743277SMike Stump 903ade90fd1SOwen Anderson return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 9040c743277SMike Stump } 9050c743277SMike Stump 906aeb0ffd5SMike Stump llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T, 90767645930SMike Stump std::vector<HelperInfo> *NoteForHelper) { 908aeb0ffd5SMike Stump return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose, 909aeb0ffd5SMike Stump T, NoteForHelper); 9100c743277SMike Stump } 9110c743277SMike Stump 912aeb0ffd5SMike Stump llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T, 91367645930SMike Stump std::vector<HelperInfo> *NoteForHelperp) { 914aeb0ffd5SMike Stump return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose, 91567645930SMike Stump T, NoteForHelperp); 91697d01d50SMike Stump } 917626aecc4SMike Stump 918f89230d9SMike Stump llvm::Constant *BlockFunction:: 919f89230d9SMike Stump GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) { 920ee2a5ee5SMike Stump QualType R = getContext().VoidTy; 921ee2a5ee5SMike Stump 922ee2a5ee5SMike Stump FunctionArgList Args; 923ee2a5ee5SMike Stump // FIXME: This leaks 924f89230d9SMike Stump ImplicitParamDecl *Dst = 925f89230d9SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 926f89230d9SMike Stump getContext().getPointerType(getContext().VoidTy)); 927f89230d9SMike Stump Args.push_back(std::make_pair(Dst, Dst->getType())); 928f89230d9SMike Stump 929f89230d9SMike Stump // FIXME: This leaks 930ee2a5ee5SMike Stump ImplicitParamDecl *Src = 931ee2a5ee5SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 932ee2a5ee5SMike Stump getContext().getPointerType(getContext().VoidTy)); 933ee2a5ee5SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 934ee2a5ee5SMike Stump 935ee2a5ee5SMike Stump const CGFunctionInfo &FI = 936ee2a5ee5SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 937ee2a5ee5SMike Stump 938ee2a5ee5SMike Stump std::string Name = std::string("__Block_byref_id_object_copy_"); 939ee2a5ee5SMike Stump CodeGenTypes &Types = CGM.getTypes(); 940ee2a5ee5SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 941ee2a5ee5SMike Stump 942cbc2bcafSMike Stump // FIXME: We'd like to put these into a mergable by content, with 943cbc2bcafSMike Stump // internal linkage. 944ee2a5ee5SMike Stump llvm::Function *Fn = 945ee2a5ee5SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 946ee2a5ee5SMike Stump Name, 947ee2a5ee5SMike Stump &CGM.getModule()); 948ee2a5ee5SMike Stump 949ee2a5ee5SMike Stump IdentifierInfo *II 950ee2a5ee5SMike Stump = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_"); 951ee2a5ee5SMike Stump 952ee2a5ee5SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 953ee2a5ee5SMike Stump getContext().getTranslationUnitDecl(), 95460ed5604SArgyrios Kyrtzidis SourceLocation(), II, R, 0, 955ee2a5ee5SMike Stump FunctionDecl::Static, false, 956ee2a5ee5SMike Stump true); 957ee2a5ee5SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 958f89230d9SMike Stump 959f89230d9SMike Stump // dst->x 960f89230d9SMike Stump llvm::Value *V = CGF.GetAddrOfLocalVar(Dst); 9619793f0e4SOwen Anderson V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0)); 9623b65ac26SMike Stump V = Builder.CreateLoad(V); 963f89230d9SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 964f89230d9SMike Stump llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty); 965f89230d9SMike Stump 966f89230d9SMike Stump // src->x 967f89230d9SMike Stump V = CGF.GetAddrOfLocalVar(Src); 968f89230d9SMike Stump V = Builder.CreateLoad(V); 969f89230d9SMike Stump V = Builder.CreateBitCast(V, T); 970f89230d9SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 9719793f0e4SOwen Anderson V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); 972f89230d9SMike Stump llvm::Value *SrcObj = Builder.CreateLoad(V); 973f89230d9SMike Stump 974f89230d9SMike Stump flag |= BLOCK_BYREF_CALLER; 975f89230d9SMike Stump 97641a75027SOwen Anderson llvm::Value *N = llvm::ConstantInt::get( 97741a75027SOwen Anderson llvm::Type::getInt32Ty(T->getContext()), flag); 978f89230d9SMike Stump llvm::Value *F = getBlockObjectAssign(); 979f89230d9SMike Stump Builder.CreateCall3(F, DstObj, SrcObj, N); 980f89230d9SMike Stump 981ee2a5ee5SMike Stump CGF.FinishFunction(); 982ee2a5ee5SMike Stump 983ade90fd1SOwen Anderson return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 984ee2a5ee5SMike Stump } 985ee2a5ee5SMike Stump 986fbe25dd1SMike Stump llvm::Constant * 987fbe25dd1SMike Stump BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T, 988fbe25dd1SMike Stump int flag) { 989ee2a5ee5SMike Stump QualType R = getContext().VoidTy; 990ee2a5ee5SMike Stump 991ee2a5ee5SMike Stump FunctionArgList Args; 992ee2a5ee5SMike Stump // FIXME: This leaks 993ee2a5ee5SMike Stump ImplicitParamDecl *Src = 994ee2a5ee5SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 995ee2a5ee5SMike Stump getContext().getPointerType(getContext().VoidTy)); 996ee2a5ee5SMike Stump 997ee2a5ee5SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 998ee2a5ee5SMike Stump 999ee2a5ee5SMike Stump const CGFunctionInfo &FI = 1000ee2a5ee5SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 1001ee2a5ee5SMike Stump 1002ee2a5ee5SMike Stump std::string Name = std::string("__Block_byref_id_object_dispose_"); 1003ee2a5ee5SMike Stump CodeGenTypes &Types = CGM.getTypes(); 1004ee2a5ee5SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 1005ee2a5ee5SMike Stump 1006cbc2bcafSMike Stump // FIXME: We'd like to put these into a mergable by content, with 1007cbc2bcafSMike Stump // internal linkage. 1008ee2a5ee5SMike Stump llvm::Function *Fn = 1009ee2a5ee5SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 1010ee2a5ee5SMike Stump Name, 1011ee2a5ee5SMike Stump &CGM.getModule()); 1012ee2a5ee5SMike Stump 1013ee2a5ee5SMike Stump IdentifierInfo *II 1014ee2a5ee5SMike Stump = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_"); 1015ee2a5ee5SMike Stump 1016ee2a5ee5SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 1017ee2a5ee5SMike Stump getContext().getTranslationUnitDecl(), 101860ed5604SArgyrios Kyrtzidis SourceLocation(), II, R, 0, 1019ee2a5ee5SMike Stump FunctionDecl::Static, false, 1020ee2a5ee5SMike Stump true); 1021ee2a5ee5SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 1022fbe25dd1SMike Stump 1023fbe25dd1SMike Stump llvm::Value *V = CGF.GetAddrOfLocalVar(Src); 10249793f0e4SOwen Anderson V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0)); 10253b65ac26SMike Stump V = Builder.CreateLoad(V); 1026fbe25dd1SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 10279793f0e4SOwen Anderson V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); 10283b65ac26SMike Stump V = Builder.CreateLoad(V); 1029fbe25dd1SMike Stump 1030fbe25dd1SMike Stump flag |= BLOCK_BYREF_CALLER; 1031fbe25dd1SMike Stump BuildBlockRelease(V, flag); 1032ee2a5ee5SMike Stump CGF.FinishFunction(); 1033ee2a5ee5SMike Stump 1034ade90fd1SOwen Anderson return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 1035ee2a5ee5SMike Stump } 1036ee2a5ee5SMike Stump 1037f89230d9SMike Stump llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T, 1038cbc2bcafSMike Stump int flag, unsigned Align) { 1039cbc2bcafSMike Stump // All alignments below that of pointer alignment collpase down to just 1040cbc2bcafSMike Stump // pointer alignment, as we always have at least that much alignment to begin 1041cbc2bcafSMike Stump // with. 1042cbc2bcafSMike Stump Align /= unsigned(CGF.Target.getPointerAlign(0)/8); 1043cbc2bcafSMike Stump // As an optimization, we only generate a single function of each kind we 1044cbc2bcafSMike Stump // might need. We need a different one for each alignment and for each 1045cbc2bcafSMike Stump // setting of flags. We mix Align and flag to get the kind. 1046cbc2bcafSMike Stump uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag; 1047cbc2bcafSMike Stump llvm::Constant *& Entry = CGM.AssignCache[kind]; 1048cbc2bcafSMike Stump if (Entry) 1049cbc2bcafSMike Stump return Entry; 1050cbc2bcafSMike Stump return Entry=CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag); 1051ee2a5ee5SMike Stump } 1052ee2a5ee5SMike Stump 1053fbe25dd1SMike Stump llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T, 1054cbc2bcafSMike Stump int flag, 1055cbc2bcafSMike Stump unsigned Align) { 1056cbc2bcafSMike Stump // All alignments below that of pointer alignment collpase down to just 1057cbc2bcafSMike Stump // pointer alignment, as we always have at least that much alignment to begin 1058cbc2bcafSMike Stump // with. 1059cbc2bcafSMike Stump Align /= unsigned(CGF.Target.getPointerAlign(0)/8); 1060cbc2bcafSMike Stump // As an optimization, we only generate a single function of each kind we 1061cbc2bcafSMike Stump // might need. We need a different one for each alignment and for each 1062cbc2bcafSMike Stump // setting of flags. We mix Align and flag to get the kind. 1063cbc2bcafSMike Stump uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag; 1064cbc2bcafSMike Stump llvm::Constant *& Entry = CGM.DestroyCache[kind]; 1065cbc2bcafSMike Stump if (Entry) 1066cbc2bcafSMike Stump return Entry; 1067cbc2bcafSMike Stump return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag); 1068ee2a5ee5SMike Stump } 1069ee2a5ee5SMike Stump 1070626aecc4SMike Stump llvm::Value *BlockFunction::getBlockObjectDispose() { 1071626aecc4SMike Stump if (CGM.BlockObjectDispose == 0) { 1072626aecc4SMike Stump const llvm::FunctionType *FTy; 1073626aecc4SMike Stump std::vector<const llvm::Type*> ArgTys; 107441a75027SOwen Anderson const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext); 1075626aecc4SMike Stump ArgTys.push_back(PtrToInt8Ty); 107641a75027SOwen Anderson ArgTys.push_back(llvm::Type::getInt32Ty(VMContext)); 10779793f0e4SOwen Anderson FTy = llvm::FunctionType::get(ResultType, ArgTys, false); 1078626aecc4SMike Stump CGM.BlockObjectDispose 1079626aecc4SMike Stump = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose"); 1080626aecc4SMike Stump } 1081626aecc4SMike Stump return CGM.BlockObjectDispose; 1082626aecc4SMike Stump } 1083626aecc4SMike Stump 1084f89230d9SMike Stump llvm::Value *BlockFunction::getBlockObjectAssign() { 1085f89230d9SMike Stump if (CGM.BlockObjectAssign == 0) { 1086f89230d9SMike Stump const llvm::FunctionType *FTy; 1087f89230d9SMike Stump std::vector<const llvm::Type*> ArgTys; 108841a75027SOwen Anderson const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext); 1089f89230d9SMike Stump ArgTys.push_back(PtrToInt8Ty); 1090f89230d9SMike Stump ArgTys.push_back(PtrToInt8Ty); 109141a75027SOwen Anderson ArgTys.push_back(llvm::Type::getInt32Ty(VMContext)); 10929793f0e4SOwen Anderson FTy = llvm::FunctionType::get(ResultType, ArgTys, false); 1093f89230d9SMike Stump CGM.BlockObjectAssign 1094f89230d9SMike Stump = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign"); 1095f89230d9SMike Stump } 1096f89230d9SMike Stump return CGM.BlockObjectAssign; 1097f89230d9SMike Stump } 1098f89230d9SMike Stump 1099fbe25dd1SMike Stump void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) { 1100626aecc4SMike Stump llvm::Value *F = getBlockObjectDispose(); 1101fbe25dd1SMike Stump llvm::Value *N; 1102626aecc4SMike Stump V = Builder.CreateBitCast(V, PtrToInt8Ty); 110341a75027SOwen Anderson N = llvm::ConstantInt::get(llvm::Type::getInt32Ty(V->getContext()), flag); 1104626aecc4SMike Stump Builder.CreateCall2(F, V, N); 1105626aecc4SMike Stump } 11064446dcf0SMike Stump 11074446dcf0SMike Stump ASTContext &BlockFunction::getContext() const { return CGM.getContext(); } 1108aeb0ffd5SMike Stump 1109aeb0ffd5SMike Stump BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf, 1110aeb0ffd5SMike Stump CGBuilderTy &B) 1111170229f6SOwen Anderson : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) { 111241a75027SOwen Anderson PtrToInt8Ty = llvm::PointerType::getUnqual( 111341a75027SOwen Anderson llvm::Type::getInt8Ty(VMContext)); 1114aeb0ffd5SMike Stump 1115aeb0ffd5SMike Stump BlockHasCopyDispose = false; 1116aeb0ffd5SMike Stump } 1117