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 222437cbfaSAnders Carlsson using namespace clang; 232437cbfaSAnders Carlsson using namespace CodeGen; 242437cbfaSAnders Carlsson 25d6ef62f0SMike Stump llvm::Constant *CodeGenFunction:: 2640775003SKen Dyck BuildDescriptorBlockDecl(bool BlockHasCopyDispose, CharUnits Size, 27d2142cffSMike Stump const llvm::StructType* Ty, 28aeb0ffd5SMike Stump std::vector<HelperInfo> *NoteForHelper) { 29c2c38331SMike Stump const llvm::Type *UnsignedLongTy 30c2c38331SMike Stump = CGM.getTypes().ConvertType(getContext().UnsignedLongTy); 3185284bacSMike Stump llvm::Constant *C; 3285284bacSMike Stump std::vector<llvm::Constant*> Elts; 3385284bacSMike Stump 3485284bacSMike Stump // reserved 35b7a2fe6fSOwen Anderson C = llvm::ConstantInt::get(UnsignedLongTy, 0); 3685284bacSMike Stump Elts.push_back(C); 3785284bacSMike Stump 3885284bacSMike Stump // Size 392ac40a9fSMike Stump // FIXME: What is the right way to say this doesn't fit? We should give 402ac40a9fSMike Stump // a user diagnostic in that case. Better fix would be to change the 412ac40a9fSMike Stump // API to size_t. 4240775003SKen Dyck C = llvm::ConstantInt::get(UnsignedLongTy, Size.getQuantity()); 4385284bacSMike Stump Elts.push_back(C); 4485284bacSMike Stump 4585284bacSMike Stump if (BlockHasCopyDispose) { 4685284bacSMike Stump // copy_func_helper_decl 4767645930SMike Stump Elts.push_back(BuildCopyHelper(Ty, NoteForHelper)); 4885284bacSMike Stump 4985284bacSMike Stump // destroy_func_decl 5067645930SMike Stump Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper)); 5185284bacSMike Stump } 5285284bacSMike Stump 5341eaf0acSNick Lewycky C = llvm::ConstantStruct::get(VMContext, Elts, false); 5485284bacSMike Stump 55c10c8d3eSOwen Anderson C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, 5685284bacSMike Stump llvm::GlobalValue::InternalLinkage, 57c10c8d3eSOwen Anderson C, "__block_descriptor_tmp"); 5885284bacSMike Stump return C; 5985284bacSMike Stump } 6085284bacSMike Stump 6195435674SMike Stump llvm::Constant *BlockModule::getNSConcreteGlobalBlock() { 6246813bbfSChris Lattner if (NSConcreteGlobalBlock == 0) 6346813bbfSChris Lattner NSConcreteGlobalBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty, 6446813bbfSChris Lattner "_NSConcreteGlobalBlock"); 65971f9b66SMike Stump return NSConcreteGlobalBlock; 66971f9b66SMike Stump } 67971f9b66SMike Stump 6895435674SMike Stump llvm::Constant *BlockModule::getNSConcreteStackBlock() { 6946813bbfSChris Lattner if (NSConcreteStackBlock == 0) 7046813bbfSChris Lattner NSConcreteStackBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty, 7146813bbfSChris Lattner "_NSConcreteStackBlock"); 727ab278dbSMike Stump return NSConcreteStackBlock; 737ab278dbSMike Stump } 747ab278dbSMike Stump 75066b6166SMike Stump static void CollectBlockDeclRefInfo( 76066b6166SMike Stump const Stmt *S, CodeGenFunction::BlockInfo &Info, 77066b6166SMike Stump llvm::SmallSet<const DeclContext *, 16> &InnerContexts) { 78ed5e69feSAnders Carlsson for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); 79ed5e69feSAnders Carlsson I != E; ++I) 80dd942713SDaniel Dunbar if (*I) 81066b6166SMike Stump CollectBlockDeclRefInfo(*I, Info, InnerContexts); 82ed5e69feSAnders Carlsson 837fe9cc1dSMike Stump // We want to ensure we walk down into block literals so we can find 847fe9cc1dSMike Stump // all nested BlockDeclRefExprs. 85066b6166SMike Stump if (const BlockExpr *BE = dyn_cast<BlockExpr>(S)) { 86066b6166SMike Stump InnerContexts.insert(cast<DeclContext>(BE->getBlockDecl())); 87066b6166SMike Stump CollectBlockDeclRefInfo(BE->getBody(), Info, InnerContexts); 88066b6166SMike Stump } 897fe9cc1dSMike Stump 90066b6166SMike Stump if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) { 91ed5e69feSAnders Carlsson // FIXME: Handle enums. 92066b6166SMike Stump if (isa<FunctionDecl>(BDRE->getDecl())) 93ed5e69feSAnders Carlsson return; 94ed5e69feSAnders Carlsson 95066b6166SMike Stump // Only Decls that escape are added. 96066b6166SMike Stump if (!InnerContexts.count(BDRE->getDecl()->getDeclContext())) 97066b6166SMike Stump Info.DeclRefs.push_back(BDRE); 98ed5e69feSAnders Carlsson } 99ed5e69feSAnders Carlsson } 100ed5e69feSAnders Carlsson 1012d33d630SMike Stump /// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be 1022d33d630SMike Stump /// declared as a global variable instead of on the stack. 10346813bbfSChris Lattner static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info) { 1047fe9cc1dSMike Stump return Info.DeclRefs.empty(); 1057fe9cc1dSMike Stump } 1067fe9cc1dSMike Stump 1077fe9cc1dSMike Stump /// AllocateAllBlockDeclRefs - Preallocate all nested BlockDeclRefExprs to 1087fe9cc1dSMike Stump /// ensure we can generate the debug information for the parameter for the block 1097fe9cc1dSMike Stump /// invoke function. 1107fe9cc1dSMike Stump static void AllocateAllBlockDeclRefs(const CodeGenFunction::BlockInfo &Info, 1117fe9cc1dSMike Stump CodeGenFunction *CGF) { 1127fe9cc1dSMike Stump // Always allocate self, as it is often handy in the debugger, even if there 1137fe9cc1dSMike Stump // is no codegen in the block that uses it. This is also useful to always do 1147fe9cc1dSMike Stump // this as if we didn't, we'd have to figure out all code that uses a self 1157fe9cc1dSMike Stump // pointer, including implicit uses. 1167fe9cc1dSMike Stump if (const ObjCMethodDecl *OMD 1177fe9cc1dSMike Stump = dyn_cast_or_null<ObjCMethodDecl>(CGF->CurFuncDecl)) { 1187fe9cc1dSMike Stump ImplicitParamDecl *SelfDecl = OMD->getSelfDecl(); 1197fe9cc1dSMike Stump BlockDeclRefExpr *BDRE = new (CGF->getContext()) 1207fe9cc1dSMike Stump BlockDeclRefExpr(SelfDecl, 1217fe9cc1dSMike Stump SelfDecl->getType(), SourceLocation(), false); 1227fe9cc1dSMike Stump CGF->AllocateBlockDecl(BDRE); 1237fe9cc1dSMike Stump } 1247fe9cc1dSMike Stump 1257fe9cc1dSMike Stump // FIXME: Also always forward the this pointer in C++ as well. 1267fe9cc1dSMike Stump 1277fe9cc1dSMike Stump for (size_t i = 0; i < Info.DeclRefs.size(); ++i) 1287fe9cc1dSMike Stump CGF->AllocateBlockDecl(Info.DeclRefs[i]); 129ed5e69feSAnders Carlsson } 130ed5e69feSAnders Carlsson 1312d33d630SMike Stump // FIXME: Push most into CGM, passing down a few bits, like current function 1322d33d630SMike Stump // name. 133b750d928SMike Stump llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) { 134ed5e69feSAnders Carlsson 135ed5e69feSAnders Carlsson std::string Name = CurFn->getName(); 136ed5e69feSAnders Carlsson CodeGenFunction::BlockInfo Info(0, Name.c_str()); 137066b6166SMike Stump llvm::SmallSet<const DeclContext *, 16> InnerContexts; 138066b6166SMike Stump InnerContexts.insert(BE->getBlockDecl()); 139066b6166SMike Stump CollectBlockDeclRefInfo(BE->getBody(), Info, InnerContexts); 140ed5e69feSAnders Carlsson 141ed5e69feSAnders Carlsson // Check if the block can be global. 1422d33d630SMike Stump // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like 1432d33d630SMike Stump // to just have one code path. We should move this function into CGM and pass 1442d33d630SMike Stump // CGF, then we can just check to see if CGF is 0. 14597d01d50SMike Stump if (0 && CanBlockBeGlobal(Info)) 146ed5e69feSAnders Carlsson return CGM.GetAddrOfGlobalBlock(BE, Name.c_str()); 14785284bacSMike Stump 148950a9518SDavid Chisnall size_t BlockFields = 5; 149950a9518SDavid Chisnall 150950a9518SDavid Chisnall bool hasIntrospection = CGM.getContext().getLangOptions().BlockIntrospection; 151950a9518SDavid Chisnall 152950a9518SDavid Chisnall if (hasIntrospection) { 153950a9518SDavid Chisnall BlockFields++; 154950a9518SDavid Chisnall } 155950a9518SDavid Chisnall std::vector<llvm::Constant*> Elts(BlockFields); 156950a9518SDavid Chisnall 157950a9518SDavid Chisnall if (hasIntrospection) { 158950a9518SDavid Chisnall std::string BlockTypeEncoding; 159950a9518SDavid Chisnall CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding); 160950a9518SDavid Chisnall 161950a9518SDavid Chisnall Elts[5] = llvm::ConstantExpr::getBitCast( 162950a9518SDavid Chisnall CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty); 163950a9518SDavid Chisnall } 164950a9518SDavid Chisnall 16585284bacSMike Stump llvm::Constant *C; 166b750d928SMike Stump llvm::Value *V; 16785284bacSMike Stump 16885284bacSMike Stump { 16985284bacSMike Stump // C = BuildBlockStructInitlist(); 17085284bacSMike Stump unsigned int flags = BLOCK_HAS_DESCRIPTOR; 17185284bacSMike Stump 172950a9518SDavid Chisnall if (hasIntrospection) 173950a9518SDavid Chisnall flags |= BLOCK_HAS_OBJC_TYPE; 174950a9518SDavid Chisnall 1754446dcf0SMike Stump // We run this first so that we set BlockHasCopyDispose from the entire 1764446dcf0SMike Stump // block literal. 1774446dcf0SMike Stump // __invoke 17840775003SKen Dyck CharUnits subBlockSize; 17940775003SKen Dyck uint64_t subBlockAlign; 1804446dcf0SMike Stump llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; 181d2142cffSMike Stump bool subBlockHasCopyDispose = false; 1824446dcf0SMike Stump llvm::Function *Fn 183d2a3e617SMike Stump = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl, 184d2a3e617SMike Stump LocalDeclMap, 1855469f29aSMike Stump subBlockSize, 1864446dcf0SMike Stump subBlockAlign, 1874446dcf0SMike Stump subBlockDeclRefDecls, 188d2142cffSMike Stump subBlockHasCopyDispose); 189d2142cffSMike Stump BlockHasCopyDispose |= subBlockHasCopyDispose; 1904446dcf0SMike Stump Elts[3] = Fn; 1914446dcf0SMike Stump 19218bb9284SMike Stump // FIXME: Don't use BlockHasCopyDispose, it is set more often then 19318bb9284SMike Stump // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); } 194d2142cffSMike Stump if (subBlockHasCopyDispose) 19585284bacSMike Stump flags |= BLOCK_HAS_COPY_DISPOSE; 19685284bacSMike Stump 197499ae7ecSMike Stump // __isa 1987ab278dbSMike Stump C = CGM.getNSConcreteStackBlock(); 199ade90fd1SOwen Anderson C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty); 2004446dcf0SMike Stump Elts[0] = C; 20185284bacSMike Stump 20285284bacSMike Stump // __flags 20385284bacSMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 20485284bacSMike Stump CGM.getTypes().ConvertType(CGM.getContext().IntTy)); 205b7a2fe6fSOwen Anderson C = llvm::ConstantInt::get(IntTy, flags); 2064446dcf0SMike Stump Elts[1] = C; 20785284bacSMike Stump 20885284bacSMike Stump // __reserved 209b7a2fe6fSOwen Anderson C = llvm::ConstantInt::get(IntTy, 0); 2104446dcf0SMike Stump Elts[2] = C; 21185284bacSMike Stump 212b750d928SMike Stump if (subBlockDeclRefDecls.size() == 0) { 213d6ef62f0SMike Stump // __descriptor 2147fe9cc1dSMike Stump Elts[4] = BuildDescriptorBlockDecl(subBlockHasCopyDispose, subBlockSize, 2157fe9cc1dSMike Stump 0, 0); 216d6ef62f0SMike Stump 2173a139f37SMike Stump // Optimize to being a global block. 2183a139f37SMike Stump Elts[0] = CGM.getNSConcreteGlobalBlock(); 219b7a2fe6fSOwen Anderson Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL); 2203a139f37SMike Stump 22141eaf0acSNick Lewycky C = llvm::ConstantStruct::get(VMContext, Elts, false); 22285284bacSMike Stump 223c10c8d3eSOwen Anderson C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, 224d6b28fc6SBenjamin Kramer llvm::GlobalValue::InternalLinkage, C, 225d6b28fc6SBenjamin Kramer "__block_holder_tmp_" + 226d6b28fc6SBenjamin Kramer llvm::Twine(CGM.getGlobalUniqueCount())); 2275d2534adSMike Stump QualType BPT = BE->getType(); 228ade90fd1SOwen Anderson C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT)); 22985284bacSMike Stump return C; 23085284bacSMike Stump } 23185284bacSMike Stump 232950a9518SDavid Chisnall std::vector<const llvm::Type *> Types(BlockFields+subBlockDeclRefDecls.size()); 233aeb0ffd5SMike Stump for (int i=0; i<4; ++i) 234b750d928SMike Stump Types[i] = Elts[i]->getType(); 235aeb0ffd5SMike Stump Types[4] = PtrToInt8Ty; 236950a9518SDavid Chisnall if (hasIntrospection) 237950a9518SDavid Chisnall Types[5] = PtrToInt8Ty; 23885284bacSMike Stump 2391db7d04bSMike Stump for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) { 2401db7d04bSMike Stump const Expr *E = subBlockDeclRefDecls[i]; 2411db7d04bSMike Stump const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); 2421db7d04bSMike Stump QualType Ty = E->getType(); 24397d01d50SMike Stump if (BDRE && BDRE->isByRef()) { 244950a9518SDavid Chisnall Types[i+BlockFields] = llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0); 24597d01d50SMike Stump } else 246950a9518SDavid Chisnall Types[i+BlockFields] = ConvertType(Ty); 2471db7d04bSMike Stump } 248b750d928SMike Stump 249758428f4SOwen Anderson llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true); 250b750d928SMike Stump 251b750d928SMike Stump llvm::AllocaInst *A = CreateTempAlloca(Ty); 252b750d928SMike Stump A->setAlignment(subBlockAlign); 253b750d928SMike Stump V = A; 254b750d928SMike Stump 255aeb0ffd5SMike Stump std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size()); 256aeb0ffd5SMike Stump int helpersize = 0; 257aeb0ffd5SMike Stump 258aeb0ffd5SMike Stump for (unsigned i=0; i<4; ++i) 259b750d928SMike Stump Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp")); 260950a9518SDavid Chisnall if (hasIntrospection) 261950a9518SDavid Chisnall Builder.CreateStore(Elts[5], Builder.CreateStructGEP(V, 5, "block.tmp")); 262b750d928SMike Stump 263b750d928SMike Stump for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) 264b750d928SMike Stump { 2651db7d04bSMike Stump // FIXME: Push const down. 2661db7d04bSMike Stump Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]); 2671db7d04bSMike Stump DeclRefExpr *DR; 2681db7d04bSMike Stump ValueDecl *VD; 269b750d928SMike Stump 2701db7d04bSMike Stump DR = dyn_cast<DeclRefExpr>(E); 2711db7d04bSMike Stump // Skip padding. 2721db7d04bSMike Stump if (DR) continue; 2731db7d04bSMike Stump 2741db7d04bSMike Stump BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); 2751db7d04bSMike Stump VD = BDRE->getDecl(); 2761db7d04bSMike Stump 277950a9518SDavid Chisnall llvm::Value* Addr = Builder.CreateStructGEP(V, i+BlockFields, "tmp"); 278aeb0ffd5SMike Stump NoteForHelper[helpersize].index = i+5; 2792114d7c2SMike Stump NoteForHelper[helpersize].RequiresCopying 2802114d7c2SMike Stump = BlockRequiresCopying(VD->getType()); 281aeb0ffd5SMike Stump NoteForHelper[helpersize].flag 2822114d7c2SMike Stump = (VD->getType()->isBlockPointerType() 2832114d7c2SMike Stump ? BLOCK_FIELD_IS_BLOCK 2842114d7c2SMike Stump : BLOCK_FIELD_IS_OBJECT); 285aeb0ffd5SMike Stump 2861db7d04bSMike Stump if (LocalDeclMap[VD]) { 28797d01d50SMike Stump if (BDRE->isByRef()) { 288aeb0ffd5SMike Stump NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF | 28990d8daf5SMike Stump // FIXME: Someone double check this. 29090d8daf5SMike Stump (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0); 29197d01d50SMike Stump llvm::Value *Loc = LocalDeclMap[VD]; 29297d01d50SMike Stump Loc = Builder.CreateStructGEP(Loc, 1, "forwarding"); 293c76493a6SDaniel Dunbar Loc = Builder.CreateLoad(Loc); 29497d01d50SMike Stump Builder.CreateStore(Loc, Addr); 295aeb0ffd5SMike Stump ++helpersize; 29697d01d50SMike Stump continue; 29797d01d50SMike Stump } else 298ce54657eSJohn McCall E = new (getContext()) DeclRefExpr (VD, 299ed6c7440SDouglas Gregor VD->getType(), 300ed6c7440SDouglas Gregor SourceLocation()); 3011db7d04bSMike Stump } 30297d01d50SMike Stump if (BDRE->isByRef()) { 303efd7caa8SMike Stump NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF | 304efd7caa8SMike Stump // FIXME: Someone double check this. 305efd7caa8SMike Stump (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0); 3061db7d04bSMike Stump E = new (getContext()) 3071db7d04bSMike Stump UnaryOperator(E, UnaryOperator::AddrOf, 3081db7d04bSMike Stump getContext().getPointerType(E->getType()), 3091db7d04bSMike Stump SourceLocation()); 31097d01d50SMike Stump } 311aeb0ffd5SMike Stump ++helpersize; 3121db7d04bSMike Stump 3131db7d04bSMike Stump RValue r = EmitAnyExpr(E, Addr, false); 31497d01d50SMike Stump if (r.isScalar()) { 31597d01d50SMike Stump llvm::Value *Loc = r.getScalarVal(); 316950a9518SDavid Chisnall const llvm::Type *Ty = Types[i+BlockFields]; 31797d01d50SMike Stump if (BDRE->isByRef()) { 3182d33d630SMike Stump // E is now the address of the value field, instead, we want the 3192d33d630SMike Stump // address of the actual ByRef struct. We optimize this slightly 3202d33d630SMike Stump // compared to gcc by not grabbing the forwarding slot as this must 3212d33d630SMike Stump // be done during Block_copy for us, and we can postpone the work 3222d33d630SMike Stump // until then. 32340775003SKen Dyck CharUnits offset = BlockDecls[BDRE->getDecl()]; 3242d33d630SMike Stump 3252d33d630SMike Stump llvm::Value *BlockLiteral = LoadBlockStruct(); 3262d33d630SMike Stump 3272d33d630SMike Stump Loc = Builder.CreateGEP(BlockLiteral, 32841a75027SOwen Anderson llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), 32940775003SKen Dyck offset.getQuantity()), 3302d33d630SMike Stump "block.literal"); 3319793f0e4SOwen Anderson Ty = llvm::PointerType::get(Ty, 0); 33297d01d50SMike Stump Loc = Builder.CreateBitCast(Loc, Ty); 333c76493a6SDaniel Dunbar Loc = Builder.CreateLoad(Loc); 3342d33d630SMike Stump // Loc = Builder.CreateBitCast(Loc, Ty); 33597d01d50SMike Stump } 33697d01d50SMike Stump Builder.CreateStore(Loc, Addr); 33797d01d50SMike Stump } else if (r.isComplex()) 338b750d928SMike Stump // FIXME: implement 339b750d928SMike Stump ErrorUnsupported(BE, "complex in block literal"); 340b750d928SMike Stump else if (r.isAggregate()) 341b750d928SMike Stump ; // Already created into the destination 342b750d928SMike Stump else 343b750d928SMike Stump assert (0 && "bad block variable"); 344b750d928SMike Stump // FIXME: Ensure that the offset created by the backend for 345b750d928SMike Stump // the struct matches the previously computed offset in BlockDecls. 346b750d928SMike Stump } 347aeb0ffd5SMike Stump NoteForHelper.resize(helpersize); 348d6ef62f0SMike Stump 349d6ef62f0SMike Stump // __descriptor 350d2142cffSMike Stump llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockHasCopyDispose, 351d2142cffSMike Stump subBlockSize, Ty, 352aeb0ffd5SMike Stump &NoteForHelper); 353aeb0ffd5SMike Stump Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty); 354aeb0ffd5SMike Stump Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp")); 355b750d928SMike Stump } 356b750d928SMike Stump 357b750d928SMike Stump QualType BPT = BE->getType(); 358b750d928SMike Stump return Builder.CreateBitCast(V, ConvertType(BPT)); 359b750d928SMike Stump } 36085284bacSMike Stump 36185284bacSMike Stump 36295435674SMike Stump const llvm::Type *BlockModule::getBlockDescriptorType() { 363650c932dSMike Stump if (BlockDescriptorType) 364650c932dSMike Stump return BlockDescriptorType; 3652437cbfaSAnders Carlsson 3662437cbfaSAnders Carlsson const llvm::Type *UnsignedLongTy = 367650c932dSMike Stump getTypes().ConvertType(getContext().UnsignedLongTy); 3682437cbfaSAnders Carlsson 3692437cbfaSAnders Carlsson // struct __block_descriptor { 3702437cbfaSAnders Carlsson // unsigned long reserved; 3712437cbfaSAnders Carlsson // unsigned long block_size; 3722437cbfaSAnders Carlsson // }; 373758428f4SOwen Anderson BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(), 374758428f4SOwen Anderson UnsignedLongTy, 3752437cbfaSAnders Carlsson UnsignedLongTy, 3762437cbfaSAnders Carlsson NULL); 3772437cbfaSAnders Carlsson 378650c932dSMike Stump getModule().addTypeName("struct.__block_descriptor", 379650c932dSMike Stump BlockDescriptorType); 3802437cbfaSAnders Carlsson 381650c932dSMike Stump return BlockDescriptorType; 3822437cbfaSAnders Carlsson } 3832437cbfaSAnders Carlsson 38495435674SMike Stump const llvm::Type *BlockModule::getGenericBlockLiteralType() { 385005c9a62SMike Stump if (GenericBlockLiteralType) 386005c9a62SMike Stump return GenericBlockLiteralType; 3872437cbfaSAnders Carlsson 3882437cbfaSAnders Carlsson const llvm::Type *BlockDescPtrTy = 3899793f0e4SOwen Anderson llvm::PointerType::getUnqual(getBlockDescriptorType()); 3902437cbfaSAnders Carlsson 39192bbd6d4SMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 39292bbd6d4SMike Stump getTypes().ConvertType(getContext().IntTy)); 39392bbd6d4SMike Stump 3942437cbfaSAnders Carlsson // struct __block_literal_generic { 3955d2534adSMike Stump // void *__isa; 3965d2534adSMike Stump // int __flags; 3975d2534adSMike Stump // int __reserved; 3985d2534adSMike Stump // void (*__invoke)(void *); 3995d2534adSMike Stump // struct __block_descriptor *__descriptor; 400950a9518SDavid Chisnall // // GNU runtime only: 401950a9518SDavid Chisnall // const char *types; 4022437cbfaSAnders Carlsson // }; 403950a9518SDavid Chisnall if (CGM.getContext().getLangOptions().BlockIntrospection) 404950a9518SDavid Chisnall GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(), 405950a9518SDavid Chisnall PtrToInt8Ty, 406950a9518SDavid Chisnall IntTy, 407950a9518SDavid Chisnall IntTy, 408950a9518SDavid Chisnall PtrToInt8Ty, 409950a9518SDavid Chisnall BlockDescPtrTy, 410950a9518SDavid Chisnall PtrToInt8Ty, 411950a9518SDavid Chisnall NULL); 412950a9518SDavid Chisnall else 413758428f4SOwen Anderson GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(), 414758428f4SOwen Anderson PtrToInt8Ty, 41592bbd6d4SMike Stump IntTy, 41692bbd6d4SMike Stump IntTy, 417626aecc4SMike Stump PtrToInt8Ty, 4182437cbfaSAnders Carlsson BlockDescPtrTy, 4192437cbfaSAnders Carlsson NULL); 4202437cbfaSAnders Carlsson 421005c9a62SMike Stump getModule().addTypeName("struct.__block_literal_generic", 422005c9a62SMike Stump GenericBlockLiteralType); 4232437cbfaSAnders Carlsson 424005c9a62SMike Stump return GenericBlockLiteralType; 4252437cbfaSAnders Carlsson } 4262437cbfaSAnders Carlsson 42795435674SMike Stump const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() { 4285d2534adSMike Stump if (GenericExtendedBlockLiteralType) 4295d2534adSMike Stump return GenericExtendedBlockLiteralType; 4305d2534adSMike Stump 4315d2534adSMike Stump const llvm::Type *BlockDescPtrTy = 4329793f0e4SOwen Anderson llvm::PointerType::getUnqual(getBlockDescriptorType()); 4335d2534adSMike Stump 4345d2534adSMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 4355d2534adSMike Stump getTypes().ConvertType(getContext().IntTy)); 4365d2534adSMike Stump 4375d2534adSMike Stump // struct __block_literal_generic { 4385d2534adSMike Stump // void *__isa; 4395d2534adSMike Stump // int __flags; 4405d2534adSMike Stump // int __reserved; 4415d2534adSMike Stump // void (*__invoke)(void *); 4425d2534adSMike Stump // struct __block_descriptor *__descriptor; 4435d2534adSMike Stump // void *__copy_func_helper_decl; 4445d2534adSMike Stump // void *__destroy_func_decl; 4455d2534adSMike Stump // }; 446758428f4SOwen Anderson GenericExtendedBlockLiteralType = llvm::StructType::get(IntTy->getContext(), 447758428f4SOwen Anderson PtrToInt8Ty, 4485d2534adSMike Stump IntTy, 4495d2534adSMike Stump IntTy, 450626aecc4SMike Stump PtrToInt8Ty, 4515d2534adSMike Stump BlockDescPtrTy, 452626aecc4SMike Stump PtrToInt8Ty, 453626aecc4SMike Stump PtrToInt8Ty, 4545d2534adSMike Stump NULL); 4555d2534adSMike Stump 4565d2534adSMike Stump getModule().addTypeName("struct.__block_literal_extended_generic", 4575d2534adSMike Stump GenericExtendedBlockLiteralType); 4585d2534adSMike Stump 4595d2534adSMike Stump return GenericExtendedBlockLiteralType; 4605d2534adSMike Stump } 4615d2534adSMike Stump 462bfb3671bSAnders Carlsson RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E, 463bfb3671bSAnders Carlsson ReturnValueSlot ReturnValue) { 4642437cbfaSAnders Carlsson const BlockPointerType *BPT = 465c23c7e6aSTed Kremenek E->getCallee()->getType()->getAs<BlockPointerType>(); 4662437cbfaSAnders Carlsson 4672437cbfaSAnders Carlsson llvm::Value *Callee = EmitScalarExpr(E->getCallee()); 4682437cbfaSAnders Carlsson 4692437cbfaSAnders Carlsson // Get a pointer to the generic block literal. 4702437cbfaSAnders Carlsson const llvm::Type *BlockLiteralTy = 4719793f0e4SOwen Anderson llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType()); 4722437cbfaSAnders Carlsson 4732437cbfaSAnders Carlsson // Bitcast the callee to a block literal. 4742437cbfaSAnders Carlsson llvm::Value *BlockLiteral = 4752437cbfaSAnders Carlsson Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal"); 4762437cbfaSAnders Carlsson 4772437cbfaSAnders Carlsson // Get the function pointer from the literal. 4782437cbfaSAnders Carlsson llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp"); 4792437cbfaSAnders Carlsson 4802437cbfaSAnders Carlsson BlockLiteral = 4812437cbfaSAnders Carlsson Builder.CreateBitCast(BlockLiteral, 482abd5b90eSBenjamin Kramer llvm::Type::getInt8PtrTy(VMContext), 4832437cbfaSAnders Carlsson "tmp"); 4842437cbfaSAnders Carlsson 4852437cbfaSAnders Carlsson // Add the block literal. 4862437cbfaSAnders Carlsson QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy); 4872437cbfaSAnders Carlsson CallArgList Args; 4882437cbfaSAnders Carlsson Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy)); 4892437cbfaSAnders Carlsson 490479e6fc7SAnders Carlsson QualType FnType = BPT->getPointeeType(); 491479e6fc7SAnders Carlsson 4922437cbfaSAnders Carlsson // And the rest of the arguments. 4939dd450bbSJohn McCall EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(), 494479e6fc7SAnders Carlsson E->arg_begin(), E->arg_end()); 4952437cbfaSAnders Carlsson 4965f50c651SAnders Carlsson // Load the function. 497c76493a6SDaniel Dunbar llvm::Value *Func = Builder.CreateLoad(FuncPtr, "tmp"); 4985f50c651SAnders Carlsson 4999dd450bbSJohn McCall QualType ResultType = FnType->getAs<FunctionType>()->getResultType(); 500a60cbcdfSAnders Carlsson 501a60cbcdfSAnders Carlsson const CGFunctionInfo &FnInfo = 502a60cbcdfSAnders Carlsson CGM.getTypes().getFunctionInfo(ResultType, Args); 5035f50c651SAnders Carlsson 5045f50c651SAnders Carlsson // Cast the function pointer to the right type. 5055f50c651SAnders Carlsson const llvm::Type *BlockFTy = 506a60cbcdfSAnders Carlsson CGM.getTypes().GetFunctionType(FnInfo, false); 5075f50c651SAnders Carlsson 5089793f0e4SOwen Anderson const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy); 5095f50c651SAnders Carlsson Func = Builder.CreateBitCast(Func, BlockFTyPtr); 5105f50c651SAnders Carlsson 5112437cbfaSAnders Carlsson // And call the block. 512bfb3671bSAnders Carlsson return EmitCall(FnInfo, Func, ReturnValue, Args); 5132437cbfaSAnders Carlsson } 5146a60fa24SAnders Carlsson 51540775003SKen Dyck CharUnits CodeGenFunction::AllocateBlockDecl(const BlockDeclRefExpr *E) { 5160168f4b2SAnders Carlsson const ValueDecl *VD = E->getDecl(); 51740775003SKen Dyck CharUnits &offset = BlockDecls[VD]; 5180168f4b2SAnders Carlsson 51997d01d50SMike Stump // See if we have already allocated an offset for this variable. 52040775003SKen Dyck if (offset.isPositive()) 5217fe9cc1dSMike Stump return offset; 5227fe9cc1dSMike Stump 5234446dcf0SMike Stump // Don't run the expensive check, unless we have to. 524e1b19ba0SMike Stump if (!BlockHasCopyDispose) 525e1b19ba0SMike Stump if (E->isByRef() 526e1b19ba0SMike Stump || BlockRequiresCopying(E->getType())) 5274446dcf0SMike Stump BlockHasCopyDispose = true; 5287fe9cc1dSMike Stump 52997d01d50SMike Stump // if not, allocate one now. 53097d01d50SMike Stump offset = getBlockOffset(E); 5317fe9cc1dSMike Stump 5327fe9cc1dSMike Stump return offset; 53397d01d50SMike Stump } 53497d01d50SMike Stump 5357fe9cc1dSMike Stump llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) { 5367fe9cc1dSMike Stump const ValueDecl *VD = E->getDecl(); 53740775003SKen Dyck CharUnits offset = AllocateBlockDecl(E); 5387fe9cc1dSMike Stump 5397fe9cc1dSMike Stump 54097d01d50SMike Stump llvm::Value *BlockLiteral = LoadBlockStruct(); 54197d01d50SMike Stump llvm::Value *V = Builder.CreateGEP(BlockLiteral, 54241a75027SOwen Anderson llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), 54340775003SKen Dyck offset.getQuantity()), 5442d33d630SMike Stump "block.literal"); 54597d01d50SMike Stump if (E->isByRef()) { 54697d01d50SMike Stump const llvm::Type *PtrStructTy 5470168f4b2SAnders Carlsson = llvm::PointerType::get(BuildByRefType(VD), 0); 548efd7caa8SMike Stump // The block literal will need a copy/destroy helper. 549efd7caa8SMike Stump BlockHasCopyDispose = true; 5500168f4b2SAnders Carlsson 5510168f4b2SAnders Carlsson const llvm::Type *Ty = PtrStructTy; 5529793f0e4SOwen Anderson Ty = llvm::PointerType::get(Ty, 0); 55397d01d50SMike Stump V = Builder.CreateBitCast(V, Ty); 554c76493a6SDaniel Dunbar V = Builder.CreateLoad(V); 55597d01d50SMike Stump V = Builder.CreateStructGEP(V, 1, "forwarding"); 556c76493a6SDaniel Dunbar V = Builder.CreateLoad(V); 55797d01d50SMike Stump V = Builder.CreateBitCast(V, PtrStructTy); 5580168f4b2SAnders Carlsson V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD), 5590168f4b2SAnders Carlsson VD->getNameAsString()); 56097d01d50SMike Stump } else { 5610168f4b2SAnders Carlsson const llvm::Type *Ty = CGM.getTypes().ConvertType(VD->getType()); 5620168f4b2SAnders Carlsson 5639793f0e4SOwen Anderson Ty = llvm::PointerType::get(Ty, 0); 56497d01d50SMike Stump V = Builder.CreateBitCast(V, Ty); 56597d01d50SMike Stump } 56697d01d50SMike Stump return V; 56797d01d50SMike Stump } 56897d01d50SMike Stump 569692c6e37SMike Stump void CodeGenFunction::BlockForwardSelf() { 570692c6e37SMike Stump const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); 571692c6e37SMike Stump ImplicitParamDecl *SelfDecl = OMD->getSelfDecl(); 572692c6e37SMike Stump llvm::Value *&DMEntry = LocalDeclMap[SelfDecl]; 573692c6e37SMike Stump if (DMEntry) 574692c6e37SMike Stump return; 575692c6e37SMike Stump // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care 576692c6e37SMike Stump BlockDeclRefExpr *BDRE = new (getContext()) 577692c6e37SMike Stump BlockDeclRefExpr(SelfDecl, 578692c6e37SMike Stump SelfDecl->getType(), SourceLocation(), false); 579692c6e37SMike Stump DMEntry = GetAddrOfBlockDecl(BDRE); 580692c6e37SMike Stump } 581692c6e37SMike Stump 5822d5a2878SMike Stump llvm::Constant * 5836c39666aSMike Stump BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) { 5846a60fa24SAnders Carlsson // Generate the block descriptor. 5856a60fa24SAnders Carlsson const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy); 58692bbd6d4SMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 58792bbd6d4SMike Stump getTypes().ConvertType(getContext().IntTy)); 5886a60fa24SAnders Carlsson 5896a60fa24SAnders Carlsson llvm::Constant *DescriptorFields[2]; 5906a60fa24SAnders Carlsson 5916a60fa24SAnders Carlsson // Reserved 5920b75f23bSOwen Anderson DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy); 5936a60fa24SAnders Carlsson 5946a60fa24SAnders Carlsson // Block literal size. For global blocks we just use the size of the generic 5956a60fa24SAnders Carlsson // block literal struct. 596*98ca7943SKen Dyck CharUnits BlockLiteralSize = 597*98ca7943SKen Dyck CGM.GetTargetTypeStoreSize(getGenericBlockLiteralType()); 598170229f6SOwen Anderson DescriptorFields[1] = 59940775003SKen Dyck llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize.getQuantity()); 6006a60fa24SAnders Carlsson 6016a60fa24SAnders Carlsson llvm::Constant *DescriptorStruct = 60241eaf0acSNick Lewycky llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 2, false); 6036a60fa24SAnders Carlsson 6046a60fa24SAnders Carlsson llvm::GlobalVariable *Descriptor = 605c10c8d3eSOwen Anderson new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true, 6066a60fa24SAnders Carlsson llvm::GlobalVariable::InternalLinkage, 607c10c8d3eSOwen Anderson DescriptorStruct, "__block_descriptor_global"); 6086a60fa24SAnders Carlsson 609950a9518SDavid Chisnall int FieldCount = 5; 6106a60fa24SAnders Carlsson // Generate the constants for the block literal. 611950a9518SDavid Chisnall if (CGM.getContext().getLangOptions().BlockIntrospection) 612950a9518SDavid Chisnall FieldCount = 6; 613950a9518SDavid Chisnall 614950a9518SDavid Chisnall std::vector<llvm::Constant*> LiteralFields(FieldCount); 6156a60fa24SAnders Carlsson 6162d5a2878SMike Stump CodeGenFunction::BlockInfo Info(0, n); 61740775003SKen Dyck CharUnits subBlockSize; 61840775003SKen Dyck uint64_t subBlockAlign; 6191db7d04bSMike Stump llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; 620a61bb4d1SDaniel Dunbar bool subBlockHasCopyDispose = false; 6215469f29aSMike Stump llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap; 622cb2fbcb0SMike Stump llvm::Function *Fn 623692c6e37SMike Stump = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap, 6245469f29aSMike Stump subBlockSize, 625b750d928SMike Stump subBlockAlign, 6264446dcf0SMike Stump subBlockDeclRefDecls, 6274446dcf0SMike Stump subBlockHasCopyDispose); 628cb2fbcb0SMike Stump assert(subBlockSize == BlockLiteralSize 629cb2fbcb0SMike Stump && "no imports allowed for global block"); 6306a60fa24SAnders Carlsson 6316a60fa24SAnders Carlsson // isa 632971f9b66SMike Stump LiteralFields[0] = getNSConcreteGlobalBlock(); 6336a60fa24SAnders Carlsson 6346a60fa24SAnders Carlsson // Flags 635950a9518SDavid Chisnall LiteralFields[1] = CGM.getContext().getLangOptions().BlockIntrospection ? 636950a9518SDavid Chisnall llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR | 637950a9518SDavid Chisnall BLOCK_HAS_OBJC_TYPE) : 638b7a2fe6fSOwen Anderson llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR); 6396a60fa24SAnders Carlsson 6406a60fa24SAnders Carlsson // Reserved 6410b75f23bSOwen Anderson LiteralFields[2] = llvm::Constant::getNullValue(IntTy); 6426a60fa24SAnders Carlsson 6436a60fa24SAnders Carlsson // Function 6446a60fa24SAnders Carlsson LiteralFields[3] = Fn; 6456a60fa24SAnders Carlsson 6466a60fa24SAnders Carlsson // Descriptor 6476a60fa24SAnders Carlsson LiteralFields[4] = Descriptor; 6486a60fa24SAnders Carlsson 649950a9518SDavid Chisnall // Type encoding 650950a9518SDavid Chisnall if (CGM.getContext().getLangOptions().BlockIntrospection) { 651950a9518SDavid Chisnall std::string BlockTypeEncoding; 652950a9518SDavid Chisnall CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding); 653950a9518SDavid Chisnall 654950a9518SDavid Chisnall LiteralFields[5] = CGM.GetAddrOfConstantCString(BlockTypeEncoding); 655950a9518SDavid Chisnall } 656950a9518SDavid Chisnall 6576a60fa24SAnders Carlsson llvm::Constant *BlockLiteralStruct = 658950a9518SDavid Chisnall llvm::ConstantStruct::get(VMContext, LiteralFields, false); 6596a60fa24SAnders Carlsson 6606a60fa24SAnders Carlsson llvm::GlobalVariable *BlockLiteral = 661c10c8d3eSOwen Anderson new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true, 6626a60fa24SAnders Carlsson llvm::GlobalVariable::InternalLinkage, 663c10c8d3eSOwen Anderson BlockLiteralStruct, "__block_literal_global"); 6646a60fa24SAnders Carlsson 6656a60fa24SAnders Carlsson return BlockLiteral; 6666a60fa24SAnders Carlsson } 6676a60fa24SAnders Carlsson 668cb2fbcb0SMike Stump llvm::Value *CodeGenFunction::LoadBlockStruct() { 66938382028SMike Stump llvm::Value *V = Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], 67038382028SMike Stump "self"); 67138382028SMike Stump // For now, we codegen based upon byte offsets. 67238382028SMike Stump return Builder.CreateBitCast(V, PtrToInt8Ty); 673cb2fbcb0SMike Stump } 674cb2fbcb0SMike Stump 6754446dcf0SMike Stump llvm::Function * 6764446dcf0SMike Stump CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr, 677cb2fbcb0SMike Stump const BlockInfo& Info, 678692c6e37SMike Stump const Decl *OuterFuncDecl, 6795469f29aSMike Stump llvm::DenseMap<const Decl*, llvm::Value*> ldm, 68040775003SKen Dyck CharUnits &Size, 681b750d928SMike Stump uint64_t &Align, 6824446dcf0SMike Stump llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls, 6834446dcf0SMike Stump bool &subBlockHasCopyDispose) { 6849074ed8cSDevang Patel 6859074ed8cSDevang Patel // Check if we should generate debug info for this block. 6869074ed8cSDevang Patel if (CGM.getDebugInfo()) 6879074ed8cSDevang Patel DebugInfo = CGM.getDebugInfo(); 6889074ed8cSDevang Patel 6895469f29aSMike Stump // Arrange for local static and local extern declarations to appear 6905469f29aSMike Stump // to be local to this function as well, as they are directly referenced 6915469f29aSMike Stump // in a block. 6925469f29aSMike Stump for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin(); 6935469f29aSMike Stump i != ldm.end(); 6945469f29aSMike Stump ++i) { 6955469f29aSMike Stump const VarDecl *VD = dyn_cast<VarDecl>(i->first); 6965469f29aSMike Stump 6970ca16601SDaniel Dunbar if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage()) 6985469f29aSMike Stump LocalDeclMap[VD] = i->second; 6995469f29aSMike Stump } 7005469f29aSMike Stump 701*98ca7943SKen Dyck BlockOffset = 702*98ca7943SKen Dyck CGM.GetTargetTypeStoreSize(CGM.getGenericBlockLiteralType()); 70309a9b6e3SEli Friedman BlockAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 70409a9b6e3SEli Friedman 705aae4349dSFariborz Jahanian const FunctionType *BlockFunctionType = BExpr->getFunctionType(); 706aae4349dSFariborz Jahanian QualType ResultType; 707aae4349dSFariborz Jahanian bool IsVariadic; 708a4f6b6b1SFariborz Jahanian if (const FunctionProtoType *FTy = 709a4f6b6b1SFariborz Jahanian dyn_cast<FunctionProtoType>(BlockFunctionType)) { 710aae4349dSFariborz Jahanian ResultType = FTy->getResultType(); 711aae4349dSFariborz Jahanian IsVariadic = FTy->isVariadic(); 712658fe02dSMike Stump } else { 713aae4349dSFariborz Jahanian // K&R style block. 714aae4349dSFariborz Jahanian ResultType = BlockFunctionType->getResultType(); 715aae4349dSFariborz Jahanian IsVariadic = false; 716aae4349dSFariborz Jahanian } 7176a60fa24SAnders Carlsson 7186a60fa24SAnders Carlsson FunctionArgList Args; 7196a60fa24SAnders Carlsson 7207fe9cc1dSMike Stump CurFuncDecl = OuterFuncDecl; 7217fe9cc1dSMike Stump 7223385fe12SChris Lattner const BlockDecl *BD = BExpr->getBlockDecl(); 7236a60fa24SAnders Carlsson 7247fe9cc1dSMike Stump IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor"); 725d015328fSMike Stump 726945fec05SMike Stump // Allocate all BlockDeclRefDecls, so we can calculate the right ParmTy below. 7277fe9cc1dSMike Stump AllocateAllBlockDeclRefs(Info, this); 7287fe9cc1dSMike Stump 729e1b19ba0SMike Stump QualType ParmTy = getContext().getBlockParmType(BlockHasCopyDispose, 730e1b19ba0SMike Stump BlockDeclRefDecls); 7316a60fa24SAnders Carlsson // FIXME: This leaks 7326a60fa24SAnders Carlsson ImplicitParamDecl *SelfDecl = 7336a60fa24SAnders Carlsson ImplicitParamDecl::Create(getContext(), 0, 734d015328fSMike Stump SourceLocation(), II, 735d015328fSMike Stump ParmTy); 7366a60fa24SAnders Carlsson 7376a60fa24SAnders Carlsson Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType())); 738cb2fbcb0SMike Stump BlockStructDecl = SelfDecl; 7396a60fa24SAnders Carlsson 740c4b30e59SSteve Naroff for (BlockDecl::param_const_iterator i = BD->param_begin(), 7416a60fa24SAnders Carlsson e = BD->param_end(); i != e; ++i) 742eecd39f2SMike Stump Args.push_back(std::make_pair(*i, (*i)->getType())); 7436a60fa24SAnders Carlsson 7446a60fa24SAnders Carlsson const CGFunctionInfo &FI = 745aae4349dSFariborz Jahanian CGM.getTypes().getFunctionInfo(ResultType, Args); 7466a60fa24SAnders Carlsson 7476a60fa24SAnders Carlsson CodeGenTypes &Types = CGM.getTypes(); 748aae4349dSFariborz Jahanian const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic); 7496a60fa24SAnders Carlsson 7506a60fa24SAnders Carlsson llvm::Function *Fn = 7516a60fa24SAnders Carlsson llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 752d6b28fc6SBenjamin Kramer llvm::Twine("__") + Info.Name + "_block_invoke_", 7536a60fa24SAnders Carlsson &CGM.getModule()); 7546a60fa24SAnders Carlsson 755c3e7cff6SDaniel Dunbar CGM.SetInternalFunctionAttributes(BD, Fn, FI); 756c3e7cff6SDaniel Dunbar 757324f80b1SChris Lattner StartFunction(BD, ResultType, Fn, Args, 7583385fe12SChris Lattner BExpr->getBody()->getLocEnd()); 7592e722b91SMike Stump 760324f80b1SChris Lattner CurFuncDecl = OuterFuncDecl; 76128ec0cf8SChris Lattner CurCodeDecl = BD; 762017460abSMike Stump 763017460abSMike Stump // Save a spot to insert the debug information for all the BlockDeclRefDecls. 764017460abSMike Stump llvm::BasicBlock *entry = Builder.GetInsertBlock(); 765017460abSMike Stump llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint(); 766017460abSMike Stump --entry_ptr; 767017460abSMike Stump 7683385fe12SChris Lattner EmitStmt(BExpr->getBody()); 769017460abSMike Stump 7707d69911aSMike Stump // Remember where we were... 7717d69911aSMike Stump llvm::BasicBlock *resume = Builder.GetInsertBlock(); 772017460abSMike Stump 7737d69911aSMike Stump // Go back to the entry. 774017460abSMike Stump ++entry_ptr; 775017460abSMike Stump Builder.SetInsertPoint(entry, entry_ptr); 776017460abSMike Stump 7772e722b91SMike Stump if (CGDebugInfo *DI = getDebugInfo()) { 7782e722b91SMike Stump // Emit debug information for all the BlockDeclRefDecls. 779bf78478dSChris Lattner for (unsigned i = 0, e = BlockDeclRefDecls.size(); i != e; ++i) { 780bf78478dSChris Lattner if (const BlockDeclRefExpr *BDRE = 781bf78478dSChris Lattner dyn_cast<BlockDeclRefExpr>(BlockDeclRefDecls[i])) { 7822e722b91SMike Stump const ValueDecl *D = BDRE->getDecl(); 7832e722b91SMike Stump DI->setLocation(D->getLocation()); 7842e722b91SMike Stump DI->EmitDeclareOfBlockDeclRefVariable(BDRE, 7852e722b91SMike Stump LocalDeclMap[getBlockStructDecl()], 7862e722b91SMike Stump Builder, this); 7872e722b91SMike Stump } 7882e722b91SMike Stump } 7892e722b91SMike Stump } 7907d69911aSMike Stump // And resume where we left off. 7917d69911aSMike Stump if (resume == 0) 7927d69911aSMike Stump Builder.ClearInsertionPoint(); 7937d69911aSMike Stump else 7947d69911aSMike Stump Builder.SetInsertPoint(resume); 7952e722b91SMike Stump 7963385fe12SChris Lattner FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc()); 7976a60fa24SAnders Carlsson 798b750d928SMike Stump // The runtime needs a minimum alignment of a void *. 799b750d928SMike Stump uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 80040775003SKen Dyck BlockOffset = CharUnits::fromQuantity( 80140775003SKen Dyck llvm::RoundUpToAlignment(BlockOffset.getQuantity(), MinAlign)); 802b750d928SMike Stump 803cb2fbcb0SMike Stump Size = BlockOffset; 804b750d928SMike Stump Align = BlockAlign; 805b750d928SMike Stump subBlockDeclRefDecls = BlockDeclRefDecls; 8064446dcf0SMike Stump subBlockHasCopyDispose |= BlockHasCopyDispose; 8076a60fa24SAnders Carlsson return Fn; 8086a60fa24SAnders Carlsson } 8091db7d04bSMike Stump 81040775003SKen Dyck CharUnits BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) { 8111db7d04bSMike Stump const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl()); 8121db7d04bSMike Stump 81340775003SKen Dyck CharUnits Size = getContext().getTypeSizeInChars(D->getType()); 8141db7d04bSMike Stump uint64_t Align = getContext().getDeclAlignInBytes(D); 8151db7d04bSMike Stump 8161db7d04bSMike Stump if (BDRE->isByRef()) { 81740775003SKen Dyck Size = getContext().getTypeSizeInChars(getContext().VoidPtrTy); 8181db7d04bSMike Stump Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 8191db7d04bSMike Stump } 8201db7d04bSMike Stump 8211db7d04bSMike Stump assert ((Align > 0) && "alignment must be 1 byte or more"); 8221db7d04bSMike Stump 82340775003SKen Dyck CharUnits OldOffset = BlockOffset; 8241db7d04bSMike Stump 8251db7d04bSMike Stump // Ensure proper alignment, even if it means we have to have a gap 82640775003SKen Dyck BlockOffset = CharUnits::fromQuantity( 82740775003SKen Dyck llvm::RoundUpToAlignment(BlockOffset.getQuantity(), Align)); 8281db7d04bSMike Stump BlockAlign = std::max(Align, BlockAlign); 8291db7d04bSMike Stump 83040775003SKen Dyck CharUnits Pad = BlockOffset - OldOffset; 83140775003SKen Dyck if (Pad.isPositive()) { 83240775003SKen Dyck llvm::ArrayType::get(llvm::Type::getInt8Ty(VMContext), Pad.getQuantity()); 8331db7d04bSMike Stump QualType PadTy = getContext().getConstantArrayType(getContext().CharTy, 83440775003SKen Dyck llvm::APInt(32, 83540775003SKen Dyck Pad.getQuantity()), 8361db7d04bSMike Stump ArrayType::Normal, 0); 8371db7d04bSMike Stump ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(), 8386032ef1aSArgyrios Kyrtzidis 0, QualType(PadTy), 0, VarDecl::None); 8391db7d04bSMike Stump Expr *E; 8401db7d04bSMike Stump E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(), 841ed6c7440SDouglas Gregor SourceLocation()); 8421db7d04bSMike Stump BlockDeclRefDecls.push_back(E); 8431db7d04bSMike Stump } 8441db7d04bSMike Stump BlockDeclRefDecls.push_back(BDRE); 8451db7d04bSMike Stump 8461db7d04bSMike Stump BlockOffset += Size; 8471db7d04bSMike Stump return BlockOffset-Size; 8481db7d04bSMike Stump } 84997d01d50SMike Stump 850aeb0ffd5SMike Stump llvm::Constant *BlockFunction:: 851aeb0ffd5SMike Stump GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T, 85267645930SMike Stump std::vector<HelperInfo> *NoteForHelperp) { 8530c743277SMike Stump QualType R = getContext().VoidTy; 8540c743277SMike Stump 8550c743277SMike Stump FunctionArgList Args; 8560c743277SMike Stump // FIXME: This leaks 857aeb0ffd5SMike Stump ImplicitParamDecl *Dst = 8587fe9cc1dSMike Stump ImplicitParamDecl::Create(getContext(), 0, 8597fe9cc1dSMike Stump SourceLocation(), 0, 860aeb0ffd5SMike Stump getContext().getPointerType(getContext().VoidTy)); 861aeb0ffd5SMike Stump Args.push_back(std::make_pair(Dst, Dst->getType())); 8620c743277SMike Stump ImplicitParamDecl *Src = 8637fe9cc1dSMike Stump ImplicitParamDecl::Create(getContext(), 0, 8647fe9cc1dSMike Stump SourceLocation(), 0, 8650c743277SMike Stump getContext().getPointerType(getContext().VoidTy)); 8660c743277SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 8670c743277SMike Stump 8680c743277SMike Stump const CGFunctionInfo &FI = 8690c743277SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 8700c743277SMike Stump 871cbc2bcafSMike Stump // FIXME: We'd like to put these into a mergable by content, with 872cbc2bcafSMike Stump // internal linkage. 8730c743277SMike Stump CodeGenTypes &Types = CGM.getTypes(); 8740c743277SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 8750c743277SMike Stump 8760c743277SMike Stump llvm::Function *Fn = 8770c743277SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 878d6b28fc6SBenjamin Kramer "__copy_helper_block_", &CGM.getModule()); 8790c743277SMike Stump 8800c743277SMike Stump IdentifierInfo *II 8810c743277SMike Stump = &CGM.getContext().Idents.get("__copy_helper_block_"); 8820c743277SMike Stump 8830c743277SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 8840c743277SMike Stump getContext().getTranslationUnitDecl(), 88560ed5604SArgyrios Kyrtzidis SourceLocation(), II, R, 0, 8860c743277SMike Stump FunctionDecl::Static, false, 8870c743277SMike Stump true); 8880c743277SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 889aeb0ffd5SMike Stump 890aeb0ffd5SMike Stump llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); 891aeb0ffd5SMike Stump llvm::Type *PtrPtrT; 89267645930SMike Stump 89367645930SMike Stump if (NoteForHelperp) { 89467645930SMike Stump std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp; 89567645930SMike Stump 8969793f0e4SOwen Anderson PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 897aeb0ffd5SMike Stump SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); 898aeb0ffd5SMike Stump SrcObj = Builder.CreateLoad(SrcObj); 899aeb0ffd5SMike Stump 900aeb0ffd5SMike Stump llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst); 9013b65ac26SMike Stump llvm::Type *PtrPtrT; 9029793f0e4SOwen Anderson PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 9033b65ac26SMike Stump DstObj = Builder.CreateBitCast(DstObj, PtrPtrT); 9043b65ac26SMike Stump DstObj = Builder.CreateLoad(DstObj); 905aeb0ffd5SMike Stump 906aeb0ffd5SMike Stump for (unsigned i=0; i < NoteForHelper.size(); ++i) { 907aeb0ffd5SMike Stump int flag = NoteForHelper[i].flag; 908aeb0ffd5SMike Stump int index = NoteForHelper[i].index; 909aeb0ffd5SMike Stump 910aeb0ffd5SMike Stump if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) 911aeb0ffd5SMike Stump || NoteForHelper[i].RequiresCopying) { 912aeb0ffd5SMike Stump llvm::Value *Srcv = SrcObj; 913aeb0ffd5SMike Stump Srcv = Builder.CreateStructGEP(Srcv, index); 914aeb0ffd5SMike Stump Srcv = Builder.CreateBitCast(Srcv, 9159793f0e4SOwen Anderson llvm::PointerType::get(PtrToInt8Ty, 0)); 916aeb0ffd5SMike Stump Srcv = Builder.CreateLoad(Srcv); 917aeb0ffd5SMike Stump 918aeb0ffd5SMike Stump llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index); 919aeb0ffd5SMike Stump Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty); 920aeb0ffd5SMike Stump 92141a75027SOwen Anderson llvm::Value *N = llvm::ConstantInt::get( 92241a75027SOwen Anderson llvm::Type::getInt32Ty(T->getContext()), flag); 923aeb0ffd5SMike Stump llvm::Value *F = getBlockObjectAssign(); 924aeb0ffd5SMike Stump Builder.CreateCall3(F, Dstv, Srcv, N); 925aeb0ffd5SMike Stump } 926aeb0ffd5SMike Stump } 92767645930SMike Stump } 928aeb0ffd5SMike Stump 9290c743277SMike Stump CGF.FinishFunction(); 9300c743277SMike Stump 931ade90fd1SOwen Anderson return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 93297d01d50SMike Stump } 93397d01d50SMike Stump 934d6ef62f0SMike Stump llvm::Constant *BlockFunction:: 935aeb0ffd5SMike Stump GenerateDestroyHelperFunction(bool BlockHasCopyDispose, 936aeb0ffd5SMike Stump const llvm::StructType* T, 93767645930SMike Stump std::vector<HelperInfo> *NoteForHelperp) { 9380c743277SMike Stump QualType R = getContext().VoidTy; 9390c743277SMike Stump 9400c743277SMike Stump FunctionArgList Args; 9410c743277SMike Stump // FIXME: This leaks 9420c743277SMike Stump ImplicitParamDecl *Src = 9437fe9cc1dSMike Stump ImplicitParamDecl::Create(getContext(), 0, 9447fe9cc1dSMike Stump SourceLocation(), 0, 9450c743277SMike Stump getContext().getPointerType(getContext().VoidTy)); 9460c743277SMike Stump 9470c743277SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 9480c743277SMike Stump 9490c743277SMike Stump const CGFunctionInfo &FI = 9500c743277SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 9510c743277SMike Stump 952cbc2bcafSMike Stump // FIXME: We'd like to put these into a mergable by content, with 953cbc2bcafSMike Stump // internal linkage. 9540c743277SMike Stump CodeGenTypes &Types = CGM.getTypes(); 9550c743277SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 9560c743277SMike Stump 9570c743277SMike Stump llvm::Function *Fn = 9580c743277SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 959d6b28fc6SBenjamin Kramer "__destroy_helper_block_", &CGM.getModule()); 9600c743277SMike Stump 9610c743277SMike Stump IdentifierInfo *II 9620c743277SMike Stump = &CGM.getContext().Idents.get("__destroy_helper_block_"); 9630c743277SMike Stump 9640c743277SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 9650c743277SMike Stump getContext().getTranslationUnitDecl(), 96660ed5604SArgyrios Kyrtzidis SourceLocation(), II, R, 0, 9670c743277SMike Stump FunctionDecl::Static, false, 9680c743277SMike Stump true); 9690c743277SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 9706f7d9f87SMike Stump 97167645930SMike Stump if (NoteForHelperp) { 97267645930SMike Stump std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp; 97367645930SMike Stump 9746f7d9f87SMike Stump llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); 9756f7d9f87SMike Stump llvm::Type *PtrPtrT; 9769793f0e4SOwen Anderson PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 9776f7d9f87SMike Stump SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); 9786f7d9f87SMike Stump SrcObj = Builder.CreateLoad(SrcObj); 9796f7d9f87SMike Stump 9806f7d9f87SMike Stump for (unsigned i=0; i < NoteForHelper.size(); ++i) { 9816f7d9f87SMike Stump int flag = NoteForHelper[i].flag; 9826f7d9f87SMike Stump int index = NoteForHelper[i].index; 9836f7d9f87SMike Stump 9846f7d9f87SMike Stump if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) 9856f7d9f87SMike Stump || NoteForHelper[i].RequiresCopying) { 9866f7d9f87SMike Stump llvm::Value *Srcv = SrcObj; 9876f7d9f87SMike Stump Srcv = Builder.CreateStructGEP(Srcv, index); 9886f7d9f87SMike Stump Srcv = Builder.CreateBitCast(Srcv, 9899793f0e4SOwen Anderson llvm::PointerType::get(PtrToInt8Ty, 0)); 9906f7d9f87SMike Stump Srcv = Builder.CreateLoad(Srcv); 9916f7d9f87SMike Stump 9926f7d9f87SMike Stump BuildBlockRelease(Srcv, flag); 9936f7d9f87SMike Stump } 9946f7d9f87SMike Stump } 99567645930SMike Stump } 9966f7d9f87SMike Stump 9970c743277SMike Stump CGF.FinishFunction(); 9980c743277SMike Stump 999ade90fd1SOwen Anderson return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 10000c743277SMike Stump } 10010c743277SMike Stump 1002aeb0ffd5SMike Stump llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T, 100367645930SMike Stump std::vector<HelperInfo> *NoteForHelper) { 1004aeb0ffd5SMike Stump return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose, 1005aeb0ffd5SMike Stump T, NoteForHelper); 10060c743277SMike Stump } 10070c743277SMike Stump 1008aeb0ffd5SMike Stump llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T, 100967645930SMike Stump std::vector<HelperInfo> *NoteForHelperp) { 1010aeb0ffd5SMike Stump return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose, 101167645930SMike Stump T, NoteForHelperp); 101297d01d50SMike Stump } 1013626aecc4SMike Stump 1014f89230d9SMike Stump llvm::Constant *BlockFunction:: 1015f89230d9SMike Stump GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) { 1016ee2a5ee5SMike Stump QualType R = getContext().VoidTy; 1017ee2a5ee5SMike Stump 1018ee2a5ee5SMike Stump FunctionArgList Args; 1019ee2a5ee5SMike Stump // FIXME: This leaks 1020f89230d9SMike Stump ImplicitParamDecl *Dst = 10217fe9cc1dSMike Stump ImplicitParamDecl::Create(getContext(), 0, 10227fe9cc1dSMike Stump SourceLocation(), 0, 1023f89230d9SMike Stump getContext().getPointerType(getContext().VoidTy)); 1024f89230d9SMike Stump Args.push_back(std::make_pair(Dst, Dst->getType())); 1025f89230d9SMike Stump 1026f89230d9SMike Stump // FIXME: This leaks 1027ee2a5ee5SMike Stump ImplicitParamDecl *Src = 10287fe9cc1dSMike Stump ImplicitParamDecl::Create(getContext(), 0, 10297fe9cc1dSMike Stump SourceLocation(), 0, 1030ee2a5ee5SMike Stump getContext().getPointerType(getContext().VoidTy)); 1031ee2a5ee5SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 1032ee2a5ee5SMike Stump 1033ee2a5ee5SMike Stump const CGFunctionInfo &FI = 1034ee2a5ee5SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 1035ee2a5ee5SMike Stump 1036ee2a5ee5SMike Stump CodeGenTypes &Types = CGM.getTypes(); 1037ee2a5ee5SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 1038ee2a5ee5SMike Stump 1039cbc2bcafSMike Stump // FIXME: We'd like to put these into a mergable by content, with 1040cbc2bcafSMike Stump // internal linkage. 1041ee2a5ee5SMike Stump llvm::Function *Fn = 1042ee2a5ee5SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 1043d6b28fc6SBenjamin Kramer "__Block_byref_id_object_copy_", &CGM.getModule()); 1044ee2a5ee5SMike Stump 1045ee2a5ee5SMike Stump IdentifierInfo *II 1046ee2a5ee5SMike Stump = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_"); 1047ee2a5ee5SMike Stump 1048ee2a5ee5SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 1049ee2a5ee5SMike Stump getContext().getTranslationUnitDecl(), 105060ed5604SArgyrios Kyrtzidis SourceLocation(), II, R, 0, 1051ee2a5ee5SMike Stump FunctionDecl::Static, false, 1052ee2a5ee5SMike Stump true); 1053ee2a5ee5SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 1054f89230d9SMike Stump 1055f89230d9SMike Stump // dst->x 1056f89230d9SMike Stump llvm::Value *V = CGF.GetAddrOfLocalVar(Dst); 10579793f0e4SOwen Anderson V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0)); 10583b65ac26SMike Stump V = Builder.CreateLoad(V); 1059f89230d9SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 1060f89230d9SMike Stump llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty); 1061f89230d9SMike Stump 1062f89230d9SMike Stump // src->x 1063f89230d9SMike Stump V = CGF.GetAddrOfLocalVar(Src); 1064f89230d9SMike Stump V = Builder.CreateLoad(V); 1065f89230d9SMike Stump V = Builder.CreateBitCast(V, T); 1066f89230d9SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 10679793f0e4SOwen Anderson V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); 1068f89230d9SMike Stump llvm::Value *SrcObj = Builder.CreateLoad(V); 1069f89230d9SMike Stump 1070f89230d9SMike Stump flag |= BLOCK_BYREF_CALLER; 1071f89230d9SMike Stump 107241a75027SOwen Anderson llvm::Value *N = llvm::ConstantInt::get( 107341a75027SOwen Anderson llvm::Type::getInt32Ty(T->getContext()), flag); 1074f89230d9SMike Stump llvm::Value *F = getBlockObjectAssign(); 1075f89230d9SMike Stump Builder.CreateCall3(F, DstObj, SrcObj, N); 1076f89230d9SMike Stump 1077ee2a5ee5SMike Stump CGF.FinishFunction(); 1078ee2a5ee5SMike Stump 1079ade90fd1SOwen Anderson return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 1080ee2a5ee5SMike Stump } 1081ee2a5ee5SMike Stump 1082fbe25dd1SMike Stump llvm::Constant * 1083fbe25dd1SMike Stump BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T, 1084fbe25dd1SMike Stump int flag) { 1085ee2a5ee5SMike Stump QualType R = getContext().VoidTy; 1086ee2a5ee5SMike Stump 1087ee2a5ee5SMike Stump FunctionArgList Args; 1088ee2a5ee5SMike Stump // FIXME: This leaks 1089ee2a5ee5SMike Stump ImplicitParamDecl *Src = 10907fe9cc1dSMike Stump ImplicitParamDecl::Create(getContext(), 0, 10917fe9cc1dSMike Stump SourceLocation(), 0, 1092ee2a5ee5SMike Stump getContext().getPointerType(getContext().VoidTy)); 1093ee2a5ee5SMike Stump 1094ee2a5ee5SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 1095ee2a5ee5SMike Stump 1096ee2a5ee5SMike Stump const CGFunctionInfo &FI = 1097ee2a5ee5SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 1098ee2a5ee5SMike Stump 1099ee2a5ee5SMike Stump CodeGenTypes &Types = CGM.getTypes(); 1100ee2a5ee5SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 1101ee2a5ee5SMike Stump 1102cbc2bcafSMike Stump // FIXME: We'd like to put these into a mergable by content, with 1103cbc2bcafSMike Stump // internal linkage. 1104ee2a5ee5SMike Stump llvm::Function *Fn = 1105ee2a5ee5SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 1106d6b28fc6SBenjamin Kramer "__Block_byref_id_object_dispose_", 1107ee2a5ee5SMike Stump &CGM.getModule()); 1108ee2a5ee5SMike Stump 1109ee2a5ee5SMike Stump IdentifierInfo *II 1110ee2a5ee5SMike Stump = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_"); 1111ee2a5ee5SMike Stump 1112ee2a5ee5SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 1113ee2a5ee5SMike Stump getContext().getTranslationUnitDecl(), 111460ed5604SArgyrios Kyrtzidis SourceLocation(), II, R, 0, 1115ee2a5ee5SMike Stump FunctionDecl::Static, false, 1116ee2a5ee5SMike Stump true); 1117ee2a5ee5SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 1118fbe25dd1SMike Stump 1119fbe25dd1SMike Stump llvm::Value *V = CGF.GetAddrOfLocalVar(Src); 11209793f0e4SOwen Anderson V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0)); 11213b65ac26SMike Stump V = Builder.CreateLoad(V); 1122fbe25dd1SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 11239793f0e4SOwen Anderson V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); 11243b65ac26SMike Stump V = Builder.CreateLoad(V); 1125fbe25dd1SMike Stump 1126fbe25dd1SMike Stump flag |= BLOCK_BYREF_CALLER; 1127fbe25dd1SMike Stump BuildBlockRelease(V, flag); 1128ee2a5ee5SMike Stump CGF.FinishFunction(); 1129ee2a5ee5SMike Stump 1130ade90fd1SOwen Anderson return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 1131ee2a5ee5SMike Stump } 1132ee2a5ee5SMike Stump 1133f89230d9SMike Stump llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T, 1134e08ea787SChris Lattner int Flag, unsigned Align) { 1135e08ea787SChris Lattner // All alignments below that of pointer alignment collapse down to just 1136cbc2bcafSMike Stump // pointer alignment, as we always have at least that much alignment to begin 1137cbc2bcafSMike Stump // with. 1138cbc2bcafSMike Stump Align /= unsigned(CGF.Target.getPointerAlign(0)/8); 1139e08ea787SChris Lattner 1140cbc2bcafSMike Stump // As an optimization, we only generate a single function of each kind we 1141cbc2bcafSMike Stump // might need. We need a different one for each alignment and for each 1142cbc2bcafSMike Stump // setting of flags. We mix Align and flag to get the kind. 1143e08ea787SChris Lattner uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag; 1144e08ea787SChris Lattner llvm::Constant *&Entry = CGM.AssignCache[Kind]; 1145cbc2bcafSMike Stump if (Entry) 1146cbc2bcafSMike Stump return Entry; 1147e08ea787SChris Lattner return Entry = CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, Flag); 1148ee2a5ee5SMike Stump } 1149ee2a5ee5SMike Stump 1150fbe25dd1SMike Stump llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T, 1151e08ea787SChris Lattner int Flag, 1152cbc2bcafSMike Stump unsigned Align) { 1153cbc2bcafSMike Stump // All alignments below that of pointer alignment collpase down to just 1154cbc2bcafSMike Stump // pointer alignment, as we always have at least that much alignment to begin 1155cbc2bcafSMike Stump // with. 1156cbc2bcafSMike Stump Align /= unsigned(CGF.Target.getPointerAlign(0)/8); 1157e08ea787SChris Lattner 1158cbc2bcafSMike Stump // As an optimization, we only generate a single function of each kind we 1159cbc2bcafSMike Stump // might need. We need a different one for each alignment and for each 1160cbc2bcafSMike Stump // setting of flags. We mix Align and flag to get the kind. 1161e08ea787SChris Lattner uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag; 1162e08ea787SChris Lattner llvm::Constant *&Entry = CGM.DestroyCache[Kind]; 1163cbc2bcafSMike Stump if (Entry) 1164cbc2bcafSMike Stump return Entry; 1165e08ea787SChris Lattner return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, Flag); 1166ee2a5ee5SMike Stump } 1167ee2a5ee5SMike Stump 1168626aecc4SMike Stump llvm::Value *BlockFunction::getBlockObjectDispose() { 1169626aecc4SMike Stump if (CGM.BlockObjectDispose == 0) { 1170626aecc4SMike Stump const llvm::FunctionType *FTy; 1171626aecc4SMike Stump std::vector<const llvm::Type*> ArgTys; 117241a75027SOwen Anderson const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext); 1173626aecc4SMike Stump ArgTys.push_back(PtrToInt8Ty); 117441a75027SOwen Anderson ArgTys.push_back(llvm::Type::getInt32Ty(VMContext)); 11759793f0e4SOwen Anderson FTy = llvm::FunctionType::get(ResultType, ArgTys, false); 1176626aecc4SMike Stump CGM.BlockObjectDispose 1177626aecc4SMike Stump = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose"); 1178626aecc4SMike Stump } 1179626aecc4SMike Stump return CGM.BlockObjectDispose; 1180626aecc4SMike Stump } 1181626aecc4SMike Stump 1182f89230d9SMike Stump llvm::Value *BlockFunction::getBlockObjectAssign() { 1183f89230d9SMike Stump if (CGM.BlockObjectAssign == 0) { 1184f89230d9SMike Stump const llvm::FunctionType *FTy; 1185f89230d9SMike Stump std::vector<const llvm::Type*> ArgTys; 118641a75027SOwen Anderson const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext); 1187f89230d9SMike Stump ArgTys.push_back(PtrToInt8Ty); 1188f89230d9SMike Stump ArgTys.push_back(PtrToInt8Ty); 118941a75027SOwen Anderson ArgTys.push_back(llvm::Type::getInt32Ty(VMContext)); 11909793f0e4SOwen Anderson FTy = llvm::FunctionType::get(ResultType, ArgTys, false); 1191f89230d9SMike Stump CGM.BlockObjectAssign 1192f89230d9SMike Stump = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign"); 1193f89230d9SMike Stump } 1194f89230d9SMike Stump return CGM.BlockObjectAssign; 1195f89230d9SMike Stump } 1196f89230d9SMike Stump 1197fbe25dd1SMike Stump void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) { 1198626aecc4SMike Stump llvm::Value *F = getBlockObjectDispose(); 1199fbe25dd1SMike Stump llvm::Value *N; 1200626aecc4SMike Stump V = Builder.CreateBitCast(V, PtrToInt8Ty); 120141a75027SOwen Anderson N = llvm::ConstantInt::get(llvm::Type::getInt32Ty(V->getContext()), flag); 1202626aecc4SMike Stump Builder.CreateCall2(F, V, N); 1203626aecc4SMike Stump } 12044446dcf0SMike Stump 12054446dcf0SMike Stump ASTContext &BlockFunction::getContext() const { return CGM.getContext(); } 1206aeb0ffd5SMike Stump 1207aeb0ffd5SMike Stump BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf, 1208aeb0ffd5SMike Stump CGBuilderTy &B) 1209170229f6SOwen Anderson : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) { 121041a75027SOwen Anderson PtrToInt8Ty = llvm::PointerType::getUnqual( 121141a75027SOwen Anderson llvm::Type::getInt8Ty(VMContext)); 1212aeb0ffd5SMike Stump 1213aeb0ffd5SMike Stump BlockHasCopyDispose = false; 1214aeb0ffd5SMike Stump } 1215