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; 179*5262b119SKen Dyck CharUnits 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); 252*5262b119SKen Dyck A->setAlignment(subBlockAlign.getQuantity()); 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. 59698ca7943SKen Dyck CharUnits BlockLiteralSize = 59798ca7943SKen 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; 618*5262b119SKen Dyck CharUnits 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, 681*5262b119SKen Dyck CharUnits &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 70198ca7943SKen Dyck BlockOffset = 70298ca7943SKen Dyck CGM.GetTargetTypeStoreSize(CGM.getGenericBlockLiteralType()); 703*5262b119SKen Dyck BlockAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy); 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 *. 799*5262b119SKen Dyck CharUnits MinAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy); 80040775003SKen Dyck BlockOffset = CharUnits::fromQuantity( 801*5262b119SKen Dyck llvm::RoundUpToAlignment(BlockOffset.getQuantity(), 802*5262b119SKen Dyck MinAlign.getQuantity())); 803b750d928SMike Stump 804cb2fbcb0SMike Stump Size = BlockOffset; 805b750d928SMike Stump Align = BlockAlign; 806b750d928SMike Stump subBlockDeclRefDecls = BlockDeclRefDecls; 8074446dcf0SMike Stump subBlockHasCopyDispose |= BlockHasCopyDispose; 8086a60fa24SAnders Carlsson return Fn; 8096a60fa24SAnders Carlsson } 8101db7d04bSMike Stump 81140775003SKen Dyck CharUnits BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) { 8121db7d04bSMike Stump const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl()); 8131db7d04bSMike Stump 81440775003SKen Dyck CharUnits Size = getContext().getTypeSizeInChars(D->getType()); 815*5262b119SKen Dyck CharUnits Align = 816*5262b119SKen Dyck CharUnits::fromQuantity(getContext().getDeclAlignInBytes(D)); 8171db7d04bSMike Stump 8181db7d04bSMike Stump if (BDRE->isByRef()) { 81940775003SKen Dyck Size = getContext().getTypeSizeInChars(getContext().VoidPtrTy); 820*5262b119SKen Dyck Align = getContext().getTypeAlignInChars(getContext().VoidPtrTy); 8211db7d04bSMike Stump } 8221db7d04bSMike Stump 823*5262b119SKen Dyck assert ((Align.isPositive()) && "alignment must be 1 byte or more"); 8241db7d04bSMike Stump 82540775003SKen Dyck CharUnits OldOffset = BlockOffset; 8261db7d04bSMike Stump 8271db7d04bSMike Stump // Ensure proper alignment, even if it means we have to have a gap 82840775003SKen Dyck BlockOffset = CharUnits::fromQuantity( 829*5262b119SKen Dyck llvm::RoundUpToAlignment(BlockOffset.getQuantity(), Align.getQuantity())); 8301db7d04bSMike Stump BlockAlign = std::max(Align, BlockAlign); 8311db7d04bSMike Stump 83240775003SKen Dyck CharUnits Pad = BlockOffset - OldOffset; 83340775003SKen Dyck if (Pad.isPositive()) { 83440775003SKen Dyck llvm::ArrayType::get(llvm::Type::getInt8Ty(VMContext), Pad.getQuantity()); 8351db7d04bSMike Stump QualType PadTy = getContext().getConstantArrayType(getContext().CharTy, 83640775003SKen Dyck llvm::APInt(32, 83740775003SKen Dyck Pad.getQuantity()), 8381db7d04bSMike Stump ArrayType::Normal, 0); 8391db7d04bSMike Stump ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(), 8406032ef1aSArgyrios Kyrtzidis 0, QualType(PadTy), 0, VarDecl::None); 8411db7d04bSMike Stump Expr *E; 8421db7d04bSMike Stump E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(), 843ed6c7440SDouglas Gregor SourceLocation()); 8441db7d04bSMike Stump BlockDeclRefDecls.push_back(E); 8451db7d04bSMike Stump } 8461db7d04bSMike Stump BlockDeclRefDecls.push_back(BDRE); 8471db7d04bSMike Stump 8481db7d04bSMike Stump BlockOffset += Size; 8491db7d04bSMike Stump return BlockOffset-Size; 8501db7d04bSMike Stump } 85197d01d50SMike Stump 852aeb0ffd5SMike Stump llvm::Constant *BlockFunction:: 853aeb0ffd5SMike Stump GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T, 85467645930SMike Stump std::vector<HelperInfo> *NoteForHelperp) { 8550c743277SMike Stump QualType R = getContext().VoidTy; 8560c743277SMike Stump 8570c743277SMike Stump FunctionArgList Args; 8580c743277SMike Stump // FIXME: This leaks 859aeb0ffd5SMike Stump ImplicitParamDecl *Dst = 8607fe9cc1dSMike Stump ImplicitParamDecl::Create(getContext(), 0, 8617fe9cc1dSMike Stump SourceLocation(), 0, 862aeb0ffd5SMike Stump getContext().getPointerType(getContext().VoidTy)); 863aeb0ffd5SMike Stump Args.push_back(std::make_pair(Dst, Dst->getType())); 8640c743277SMike Stump ImplicitParamDecl *Src = 8657fe9cc1dSMike Stump ImplicitParamDecl::Create(getContext(), 0, 8667fe9cc1dSMike Stump SourceLocation(), 0, 8670c743277SMike Stump getContext().getPointerType(getContext().VoidTy)); 8680c743277SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 8690c743277SMike Stump 8700c743277SMike Stump const CGFunctionInfo &FI = 8710c743277SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 8720c743277SMike Stump 873cbc2bcafSMike Stump // FIXME: We'd like to put these into a mergable by content, with 874cbc2bcafSMike Stump // internal linkage. 8750c743277SMike Stump CodeGenTypes &Types = CGM.getTypes(); 8760c743277SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 8770c743277SMike Stump 8780c743277SMike Stump llvm::Function *Fn = 8790c743277SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 880d6b28fc6SBenjamin Kramer "__copy_helper_block_", &CGM.getModule()); 8810c743277SMike Stump 8820c743277SMike Stump IdentifierInfo *II 8830c743277SMike Stump = &CGM.getContext().Idents.get("__copy_helper_block_"); 8840c743277SMike Stump 8850c743277SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 8860c743277SMike Stump getContext().getTranslationUnitDecl(), 88760ed5604SArgyrios Kyrtzidis SourceLocation(), II, R, 0, 8880c743277SMike Stump FunctionDecl::Static, false, 8890c743277SMike Stump true); 8900c743277SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 891aeb0ffd5SMike Stump 892aeb0ffd5SMike Stump llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); 893aeb0ffd5SMike Stump llvm::Type *PtrPtrT; 89467645930SMike Stump 89567645930SMike Stump if (NoteForHelperp) { 89667645930SMike Stump std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp; 89767645930SMike Stump 8989793f0e4SOwen Anderson PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 899aeb0ffd5SMike Stump SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); 900aeb0ffd5SMike Stump SrcObj = Builder.CreateLoad(SrcObj); 901aeb0ffd5SMike Stump 902aeb0ffd5SMike Stump llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst); 9033b65ac26SMike Stump llvm::Type *PtrPtrT; 9049793f0e4SOwen Anderson PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 9053b65ac26SMike Stump DstObj = Builder.CreateBitCast(DstObj, PtrPtrT); 9063b65ac26SMike Stump DstObj = Builder.CreateLoad(DstObj); 907aeb0ffd5SMike Stump 908aeb0ffd5SMike Stump for (unsigned i=0; i < NoteForHelper.size(); ++i) { 909aeb0ffd5SMike Stump int flag = NoteForHelper[i].flag; 910aeb0ffd5SMike Stump int index = NoteForHelper[i].index; 911aeb0ffd5SMike Stump 912aeb0ffd5SMike Stump if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) 913aeb0ffd5SMike Stump || NoteForHelper[i].RequiresCopying) { 914aeb0ffd5SMike Stump llvm::Value *Srcv = SrcObj; 915aeb0ffd5SMike Stump Srcv = Builder.CreateStructGEP(Srcv, index); 916aeb0ffd5SMike Stump Srcv = Builder.CreateBitCast(Srcv, 9179793f0e4SOwen Anderson llvm::PointerType::get(PtrToInt8Ty, 0)); 918aeb0ffd5SMike Stump Srcv = Builder.CreateLoad(Srcv); 919aeb0ffd5SMike Stump 920aeb0ffd5SMike Stump llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index); 921aeb0ffd5SMike Stump Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty); 922aeb0ffd5SMike Stump 92341a75027SOwen Anderson llvm::Value *N = llvm::ConstantInt::get( 92441a75027SOwen Anderson llvm::Type::getInt32Ty(T->getContext()), flag); 925aeb0ffd5SMike Stump llvm::Value *F = getBlockObjectAssign(); 926aeb0ffd5SMike Stump Builder.CreateCall3(F, Dstv, Srcv, N); 927aeb0ffd5SMike Stump } 928aeb0ffd5SMike Stump } 92967645930SMike Stump } 930aeb0ffd5SMike Stump 9310c743277SMike Stump CGF.FinishFunction(); 9320c743277SMike Stump 933ade90fd1SOwen Anderson return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 93497d01d50SMike Stump } 93597d01d50SMike Stump 936d6ef62f0SMike Stump llvm::Constant *BlockFunction:: 937aeb0ffd5SMike Stump GenerateDestroyHelperFunction(bool BlockHasCopyDispose, 938aeb0ffd5SMike Stump const llvm::StructType* T, 93967645930SMike Stump std::vector<HelperInfo> *NoteForHelperp) { 9400c743277SMike Stump QualType R = getContext().VoidTy; 9410c743277SMike Stump 9420c743277SMike Stump FunctionArgList Args; 9430c743277SMike Stump // FIXME: This leaks 9440c743277SMike Stump ImplicitParamDecl *Src = 9457fe9cc1dSMike Stump ImplicitParamDecl::Create(getContext(), 0, 9467fe9cc1dSMike Stump SourceLocation(), 0, 9470c743277SMike Stump getContext().getPointerType(getContext().VoidTy)); 9480c743277SMike Stump 9490c743277SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 9500c743277SMike Stump 9510c743277SMike Stump const CGFunctionInfo &FI = 9520c743277SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 9530c743277SMike Stump 954cbc2bcafSMike Stump // FIXME: We'd like to put these into a mergable by content, with 955cbc2bcafSMike Stump // internal linkage. 9560c743277SMike Stump CodeGenTypes &Types = CGM.getTypes(); 9570c743277SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 9580c743277SMike Stump 9590c743277SMike Stump llvm::Function *Fn = 9600c743277SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 961d6b28fc6SBenjamin Kramer "__destroy_helper_block_", &CGM.getModule()); 9620c743277SMike Stump 9630c743277SMike Stump IdentifierInfo *II 9640c743277SMike Stump = &CGM.getContext().Idents.get("__destroy_helper_block_"); 9650c743277SMike Stump 9660c743277SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 9670c743277SMike Stump getContext().getTranslationUnitDecl(), 96860ed5604SArgyrios Kyrtzidis SourceLocation(), II, R, 0, 9690c743277SMike Stump FunctionDecl::Static, false, 9700c743277SMike Stump true); 9710c743277SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 9726f7d9f87SMike Stump 97367645930SMike Stump if (NoteForHelperp) { 97467645930SMike Stump std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp; 97567645930SMike Stump 9766f7d9f87SMike Stump llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); 9776f7d9f87SMike Stump llvm::Type *PtrPtrT; 9789793f0e4SOwen Anderson PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 9796f7d9f87SMike Stump SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); 9806f7d9f87SMike Stump SrcObj = Builder.CreateLoad(SrcObj); 9816f7d9f87SMike Stump 9826f7d9f87SMike Stump for (unsigned i=0; i < NoteForHelper.size(); ++i) { 9836f7d9f87SMike Stump int flag = NoteForHelper[i].flag; 9846f7d9f87SMike Stump int index = NoteForHelper[i].index; 9856f7d9f87SMike Stump 9866f7d9f87SMike Stump if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) 9876f7d9f87SMike Stump || NoteForHelper[i].RequiresCopying) { 9886f7d9f87SMike Stump llvm::Value *Srcv = SrcObj; 9896f7d9f87SMike Stump Srcv = Builder.CreateStructGEP(Srcv, index); 9906f7d9f87SMike Stump Srcv = Builder.CreateBitCast(Srcv, 9919793f0e4SOwen Anderson llvm::PointerType::get(PtrToInt8Ty, 0)); 9926f7d9f87SMike Stump Srcv = Builder.CreateLoad(Srcv); 9936f7d9f87SMike Stump 9946f7d9f87SMike Stump BuildBlockRelease(Srcv, flag); 9956f7d9f87SMike Stump } 9966f7d9f87SMike Stump } 99767645930SMike Stump } 9986f7d9f87SMike Stump 9990c743277SMike Stump CGF.FinishFunction(); 10000c743277SMike Stump 1001ade90fd1SOwen Anderson return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 10020c743277SMike Stump } 10030c743277SMike Stump 1004aeb0ffd5SMike Stump llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T, 100567645930SMike Stump std::vector<HelperInfo> *NoteForHelper) { 1006aeb0ffd5SMike Stump return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose, 1007aeb0ffd5SMike Stump T, NoteForHelper); 10080c743277SMike Stump } 10090c743277SMike Stump 1010aeb0ffd5SMike Stump llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T, 101167645930SMike Stump std::vector<HelperInfo> *NoteForHelperp) { 1012aeb0ffd5SMike Stump return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose, 101367645930SMike Stump T, NoteForHelperp); 101497d01d50SMike Stump } 1015626aecc4SMike Stump 1016f89230d9SMike Stump llvm::Constant *BlockFunction:: 1017f89230d9SMike Stump GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) { 1018ee2a5ee5SMike Stump QualType R = getContext().VoidTy; 1019ee2a5ee5SMike Stump 1020ee2a5ee5SMike Stump FunctionArgList Args; 1021ee2a5ee5SMike Stump // FIXME: This leaks 1022f89230d9SMike Stump ImplicitParamDecl *Dst = 10237fe9cc1dSMike Stump ImplicitParamDecl::Create(getContext(), 0, 10247fe9cc1dSMike Stump SourceLocation(), 0, 1025f89230d9SMike Stump getContext().getPointerType(getContext().VoidTy)); 1026f89230d9SMike Stump Args.push_back(std::make_pair(Dst, Dst->getType())); 1027f89230d9SMike Stump 1028f89230d9SMike Stump // FIXME: This leaks 1029ee2a5ee5SMike Stump ImplicitParamDecl *Src = 10307fe9cc1dSMike Stump ImplicitParamDecl::Create(getContext(), 0, 10317fe9cc1dSMike Stump SourceLocation(), 0, 1032ee2a5ee5SMike Stump getContext().getPointerType(getContext().VoidTy)); 1033ee2a5ee5SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 1034ee2a5ee5SMike Stump 1035ee2a5ee5SMike Stump const CGFunctionInfo &FI = 1036ee2a5ee5SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 1037ee2a5ee5SMike Stump 1038ee2a5ee5SMike Stump CodeGenTypes &Types = CGM.getTypes(); 1039ee2a5ee5SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 1040ee2a5ee5SMike Stump 1041cbc2bcafSMike Stump // FIXME: We'd like to put these into a mergable by content, with 1042cbc2bcafSMike Stump // internal linkage. 1043ee2a5ee5SMike Stump llvm::Function *Fn = 1044ee2a5ee5SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 1045d6b28fc6SBenjamin Kramer "__Block_byref_id_object_copy_", &CGM.getModule()); 1046ee2a5ee5SMike Stump 1047ee2a5ee5SMike Stump IdentifierInfo *II 1048ee2a5ee5SMike Stump = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_"); 1049ee2a5ee5SMike Stump 1050ee2a5ee5SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 1051ee2a5ee5SMike Stump getContext().getTranslationUnitDecl(), 105260ed5604SArgyrios Kyrtzidis SourceLocation(), II, R, 0, 1053ee2a5ee5SMike Stump FunctionDecl::Static, false, 1054ee2a5ee5SMike Stump true); 1055ee2a5ee5SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 1056f89230d9SMike Stump 1057f89230d9SMike Stump // dst->x 1058f89230d9SMike Stump llvm::Value *V = CGF.GetAddrOfLocalVar(Dst); 10599793f0e4SOwen Anderson V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0)); 10603b65ac26SMike Stump V = Builder.CreateLoad(V); 1061f89230d9SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 1062f89230d9SMike Stump llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty); 1063f89230d9SMike Stump 1064f89230d9SMike Stump // src->x 1065f89230d9SMike Stump V = CGF.GetAddrOfLocalVar(Src); 1066f89230d9SMike Stump V = Builder.CreateLoad(V); 1067f89230d9SMike Stump V = Builder.CreateBitCast(V, T); 1068f89230d9SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 10699793f0e4SOwen Anderson V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); 1070f89230d9SMike Stump llvm::Value *SrcObj = Builder.CreateLoad(V); 1071f89230d9SMike Stump 1072f89230d9SMike Stump flag |= BLOCK_BYREF_CALLER; 1073f89230d9SMike Stump 107441a75027SOwen Anderson llvm::Value *N = llvm::ConstantInt::get( 107541a75027SOwen Anderson llvm::Type::getInt32Ty(T->getContext()), flag); 1076f89230d9SMike Stump llvm::Value *F = getBlockObjectAssign(); 1077f89230d9SMike Stump Builder.CreateCall3(F, DstObj, SrcObj, N); 1078f89230d9SMike Stump 1079ee2a5ee5SMike Stump CGF.FinishFunction(); 1080ee2a5ee5SMike Stump 1081ade90fd1SOwen Anderson return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 1082ee2a5ee5SMike Stump } 1083ee2a5ee5SMike Stump 1084fbe25dd1SMike Stump llvm::Constant * 1085fbe25dd1SMike Stump BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T, 1086fbe25dd1SMike Stump int flag) { 1087ee2a5ee5SMike Stump QualType R = getContext().VoidTy; 1088ee2a5ee5SMike Stump 1089ee2a5ee5SMike Stump FunctionArgList Args; 1090ee2a5ee5SMike Stump // FIXME: This leaks 1091ee2a5ee5SMike Stump ImplicitParamDecl *Src = 10927fe9cc1dSMike Stump ImplicitParamDecl::Create(getContext(), 0, 10937fe9cc1dSMike Stump SourceLocation(), 0, 1094ee2a5ee5SMike Stump getContext().getPointerType(getContext().VoidTy)); 1095ee2a5ee5SMike Stump 1096ee2a5ee5SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 1097ee2a5ee5SMike Stump 1098ee2a5ee5SMike Stump const CGFunctionInfo &FI = 1099ee2a5ee5SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 1100ee2a5ee5SMike Stump 1101ee2a5ee5SMike Stump CodeGenTypes &Types = CGM.getTypes(); 1102ee2a5ee5SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 1103ee2a5ee5SMike Stump 1104cbc2bcafSMike Stump // FIXME: We'd like to put these into a mergable by content, with 1105cbc2bcafSMike Stump // internal linkage. 1106ee2a5ee5SMike Stump llvm::Function *Fn = 1107ee2a5ee5SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 1108d6b28fc6SBenjamin Kramer "__Block_byref_id_object_dispose_", 1109ee2a5ee5SMike Stump &CGM.getModule()); 1110ee2a5ee5SMike Stump 1111ee2a5ee5SMike Stump IdentifierInfo *II 1112ee2a5ee5SMike Stump = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_"); 1113ee2a5ee5SMike Stump 1114ee2a5ee5SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 1115ee2a5ee5SMike Stump getContext().getTranslationUnitDecl(), 111660ed5604SArgyrios Kyrtzidis SourceLocation(), II, R, 0, 1117ee2a5ee5SMike Stump FunctionDecl::Static, false, 1118ee2a5ee5SMike Stump true); 1119ee2a5ee5SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 1120fbe25dd1SMike Stump 1121fbe25dd1SMike Stump llvm::Value *V = CGF.GetAddrOfLocalVar(Src); 11229793f0e4SOwen Anderson V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0)); 11233b65ac26SMike Stump V = Builder.CreateLoad(V); 1124fbe25dd1SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 11259793f0e4SOwen Anderson V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); 11263b65ac26SMike Stump V = Builder.CreateLoad(V); 1127fbe25dd1SMike Stump 1128fbe25dd1SMike Stump flag |= BLOCK_BYREF_CALLER; 1129fbe25dd1SMike Stump BuildBlockRelease(V, flag); 1130ee2a5ee5SMike Stump CGF.FinishFunction(); 1131ee2a5ee5SMike Stump 1132ade90fd1SOwen Anderson return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 1133ee2a5ee5SMike Stump } 1134ee2a5ee5SMike Stump 1135f89230d9SMike Stump llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T, 1136e08ea787SChris Lattner int Flag, unsigned Align) { 1137e08ea787SChris Lattner // All alignments below that of pointer alignment collapse down to just 1138cbc2bcafSMike Stump // pointer alignment, as we always have at least that much alignment to begin 1139cbc2bcafSMike Stump // with. 1140cbc2bcafSMike Stump Align /= unsigned(CGF.Target.getPointerAlign(0)/8); 1141e08ea787SChris Lattner 1142cbc2bcafSMike Stump // As an optimization, we only generate a single function of each kind we 1143cbc2bcafSMike Stump // might need. We need a different one for each alignment and for each 1144cbc2bcafSMike Stump // setting of flags. We mix Align and flag to get the kind. 1145e08ea787SChris Lattner uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag; 1146e08ea787SChris Lattner llvm::Constant *&Entry = CGM.AssignCache[Kind]; 1147cbc2bcafSMike Stump if (Entry) 1148cbc2bcafSMike Stump return Entry; 1149e08ea787SChris Lattner return Entry = CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, Flag); 1150ee2a5ee5SMike Stump } 1151ee2a5ee5SMike Stump 1152fbe25dd1SMike Stump llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T, 1153e08ea787SChris Lattner int Flag, 1154cbc2bcafSMike Stump unsigned Align) { 1155cbc2bcafSMike Stump // All alignments below that of pointer alignment collpase down to just 1156cbc2bcafSMike Stump // pointer alignment, as we always have at least that much alignment to begin 1157cbc2bcafSMike Stump // with. 1158cbc2bcafSMike Stump Align /= unsigned(CGF.Target.getPointerAlign(0)/8); 1159e08ea787SChris Lattner 1160cbc2bcafSMike Stump // As an optimization, we only generate a single function of each kind we 1161cbc2bcafSMike Stump // might need. We need a different one for each alignment and for each 1162cbc2bcafSMike Stump // setting of flags. We mix Align and flag to get the kind. 1163e08ea787SChris Lattner uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag; 1164e08ea787SChris Lattner llvm::Constant *&Entry = CGM.DestroyCache[Kind]; 1165cbc2bcafSMike Stump if (Entry) 1166cbc2bcafSMike Stump return Entry; 1167e08ea787SChris Lattner return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, Flag); 1168ee2a5ee5SMike Stump } 1169ee2a5ee5SMike Stump 1170626aecc4SMike Stump llvm::Value *BlockFunction::getBlockObjectDispose() { 1171626aecc4SMike Stump if (CGM.BlockObjectDispose == 0) { 1172626aecc4SMike Stump const llvm::FunctionType *FTy; 1173626aecc4SMike Stump std::vector<const llvm::Type*> ArgTys; 117441a75027SOwen Anderson const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext); 1175626aecc4SMike Stump ArgTys.push_back(PtrToInt8Ty); 117641a75027SOwen Anderson ArgTys.push_back(llvm::Type::getInt32Ty(VMContext)); 11779793f0e4SOwen Anderson FTy = llvm::FunctionType::get(ResultType, ArgTys, false); 1178626aecc4SMike Stump CGM.BlockObjectDispose 1179626aecc4SMike Stump = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose"); 1180626aecc4SMike Stump } 1181626aecc4SMike Stump return CGM.BlockObjectDispose; 1182626aecc4SMike Stump } 1183626aecc4SMike Stump 1184f89230d9SMike Stump llvm::Value *BlockFunction::getBlockObjectAssign() { 1185f89230d9SMike Stump if (CGM.BlockObjectAssign == 0) { 1186f89230d9SMike Stump const llvm::FunctionType *FTy; 1187f89230d9SMike Stump std::vector<const llvm::Type*> ArgTys; 118841a75027SOwen Anderson const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext); 1189f89230d9SMike Stump ArgTys.push_back(PtrToInt8Ty); 1190f89230d9SMike Stump ArgTys.push_back(PtrToInt8Ty); 119141a75027SOwen Anderson ArgTys.push_back(llvm::Type::getInt32Ty(VMContext)); 11929793f0e4SOwen Anderson FTy = llvm::FunctionType::get(ResultType, ArgTys, false); 1193f89230d9SMike Stump CGM.BlockObjectAssign 1194f89230d9SMike Stump = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign"); 1195f89230d9SMike Stump } 1196f89230d9SMike Stump return CGM.BlockObjectAssign; 1197f89230d9SMike Stump } 1198f89230d9SMike Stump 1199fbe25dd1SMike Stump void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) { 1200626aecc4SMike Stump llvm::Value *F = getBlockObjectDispose(); 1201fbe25dd1SMike Stump llvm::Value *N; 1202626aecc4SMike Stump V = Builder.CreateBitCast(V, PtrToInt8Ty); 120341a75027SOwen Anderson N = llvm::ConstantInt::get(llvm::Type::getInt32Ty(V->getContext()), flag); 1204626aecc4SMike Stump Builder.CreateCall2(F, V, N); 1205626aecc4SMike Stump } 12064446dcf0SMike Stump 12074446dcf0SMike Stump ASTContext &BlockFunction::getContext() const { return CGM.getContext(); } 1208aeb0ffd5SMike Stump 1209aeb0ffd5SMike Stump BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf, 1210aeb0ffd5SMike Stump CGBuilderTy &B) 1211170229f6SOwen Anderson : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) { 121241a75027SOwen Anderson PtrToInt8Ty = llvm::PointerType::getUnqual( 121341a75027SOwen Anderson llvm::Type::getInt8Ty(VMContext)); 1214aeb0ffd5SMike Stump 1215aeb0ffd5SMike Stump BlockHasCopyDispose = false; 1216aeb0ffd5SMike Stump } 1217