12437cbfaSAnders Carlsson //===--- CGBlocks.cpp - Emit LLVM Code for declarations -------------------===// 22437cbfaSAnders Carlsson // 32437cbfaSAnders Carlsson // The LLVM Compiler Infrastructure 42437cbfaSAnders Carlsson // 52437cbfaSAnders Carlsson // This file is distributed under the University of Illinois Open Source 62437cbfaSAnders Carlsson // License. See LICENSE.TXT for details. 72437cbfaSAnders Carlsson // 82437cbfaSAnders Carlsson //===----------------------------------------------------------------------===// 92437cbfaSAnders Carlsson // 102437cbfaSAnders Carlsson // This contains code to emit blocks. 112437cbfaSAnders Carlsson // 122437cbfaSAnders Carlsson //===----------------------------------------------------------------------===// 132437cbfaSAnders Carlsson 142e722b91SMike Stump #include "CGDebugInfo.h" 152437cbfaSAnders Carlsson #include "CodeGenFunction.h" 162437cbfaSAnders Carlsson #include "CodeGenModule.h" 17692c6e37SMike Stump #include "clang/AST/DeclObjC.h" 182437cbfaSAnders Carlsson #include "llvm/Module.h" 196a60fa24SAnders Carlsson #include "llvm/Target/TargetData.h" 202437cbfaSAnders Carlsson #include <algorithm> 21db714924STorok Edwin #include <cstdio> 22db714924STorok Edwin 232437cbfaSAnders Carlsson using namespace clang; 242437cbfaSAnders Carlsson using namespace CodeGen; 252437cbfaSAnders Carlsson 26d6ef62f0SMike Stump llvm::Constant *CodeGenFunction:: 27d2142cffSMike Stump BuildDescriptorBlockDecl(bool BlockHasCopyDispose, uint64_t Size, 28d2142cffSMike Stump const llvm::StructType* Ty, 29aeb0ffd5SMike Stump std::vector<HelperInfo> *NoteForHelper) { 30c2c38331SMike Stump const llvm::Type *UnsignedLongTy 31c2c38331SMike Stump = CGM.getTypes().ConvertType(getContext().UnsignedLongTy); 3285284bacSMike Stump llvm::Constant *C; 3385284bacSMike Stump std::vector<llvm::Constant*> Elts; 3485284bacSMike Stump 3585284bacSMike Stump // reserved 36b7a2fe6fSOwen Anderson C = llvm::ConstantInt::get(UnsignedLongTy, 0); 3785284bacSMike Stump Elts.push_back(C); 3885284bacSMike Stump 3985284bacSMike Stump // Size 402ac40a9fSMike Stump // FIXME: What is the right way to say this doesn't fit? We should give 412ac40a9fSMike Stump // a user diagnostic in that case. Better fix would be to change the 422ac40a9fSMike Stump // API to size_t. 43b7a2fe6fSOwen Anderson C = llvm::ConstantInt::get(UnsignedLongTy, Size); 4485284bacSMike Stump Elts.push_back(C); 4585284bacSMike Stump 4685284bacSMike Stump if (BlockHasCopyDispose) { 4785284bacSMike Stump // copy_func_helper_decl 4867645930SMike Stump Elts.push_back(BuildCopyHelper(Ty, NoteForHelper)); 4985284bacSMike Stump 5085284bacSMike Stump // destroy_func_decl 5167645930SMike Stump Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper)); 5285284bacSMike Stump } 5385284bacSMike Stump 5441eaf0acSNick Lewycky C = llvm::ConstantStruct::get(VMContext, Elts, false); 5585284bacSMike Stump 56c10c8d3eSOwen Anderson C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, 5785284bacSMike Stump llvm::GlobalValue::InternalLinkage, 58c10c8d3eSOwen Anderson C, "__block_descriptor_tmp"); 5985284bacSMike Stump return C; 6085284bacSMike Stump } 6185284bacSMike Stump 6295435674SMike Stump llvm::Constant *BlockModule::getNSConcreteGlobalBlock() { 6346813bbfSChris Lattner if (NSConcreteGlobalBlock == 0) 6446813bbfSChris Lattner NSConcreteGlobalBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty, 6546813bbfSChris Lattner "_NSConcreteGlobalBlock"); 66971f9b66SMike Stump return NSConcreteGlobalBlock; 67971f9b66SMike Stump } 68971f9b66SMike Stump 6995435674SMike Stump llvm::Constant *BlockModule::getNSConcreteStackBlock() { 7046813bbfSChris Lattner if (NSConcreteStackBlock == 0) 7146813bbfSChris Lattner NSConcreteStackBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty, 7246813bbfSChris Lattner "_NSConcreteStackBlock"); 737ab278dbSMike Stump return NSConcreteStackBlock; 747ab278dbSMike Stump } 757ab278dbSMike Stump 76ed5e69feSAnders Carlsson static void CollectBlockDeclRefInfo(const Stmt *S, 77ed5e69feSAnders Carlsson CodeGenFunction::BlockInfo &Info) { 78ed5e69feSAnders Carlsson for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); 79ed5e69feSAnders Carlsson I != E; ++I) 80dd942713SDaniel Dunbar if (*I) 81ed5e69feSAnders Carlsson CollectBlockDeclRefInfo(*I, Info); 82ed5e69feSAnders Carlsson 837fe9cc1dSMike Stump // We want to ensure we walk down into block literals so we can find 847fe9cc1dSMike Stump // all nested BlockDeclRefExprs. 857fe9cc1dSMike Stump if (const BlockExpr *BE = dyn_cast<BlockExpr>(S)) 867fe9cc1dSMike Stump CollectBlockDeclRefInfo(BE->getBody(), Info); 877fe9cc1dSMike Stump 88ed5e69feSAnders Carlsson if (const BlockDeclRefExpr *DE = dyn_cast<BlockDeclRefExpr>(S)) { 89ed5e69feSAnders Carlsson // FIXME: Handle enums. 90ed5e69feSAnders Carlsson if (isa<FunctionDecl>(DE->getDecl())) 91ed5e69feSAnders Carlsson return; 92ed5e69feSAnders Carlsson 937fe9cc1dSMike Stump Info.DeclRefs.push_back(DE); 94ed5e69feSAnders Carlsson } 95ed5e69feSAnders Carlsson } 96ed5e69feSAnders Carlsson 972d33d630SMike Stump /// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be 982d33d630SMike Stump /// declared as a global variable instead of on the stack. 9946813bbfSChris Lattner static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info) { 1007fe9cc1dSMike Stump return Info.DeclRefs.empty(); 1017fe9cc1dSMike Stump } 1027fe9cc1dSMike Stump 1037fe9cc1dSMike Stump /// AllocateAllBlockDeclRefs - Preallocate all nested BlockDeclRefExprs to 1047fe9cc1dSMike Stump /// ensure we can generate the debug information for the parameter for the block 1057fe9cc1dSMike Stump /// invoke function. 1067fe9cc1dSMike Stump static void AllocateAllBlockDeclRefs(const CodeGenFunction::BlockInfo &Info, 1077fe9cc1dSMike Stump CodeGenFunction *CGF) { 1087fe9cc1dSMike Stump // Always allocate self, as it is often handy in the debugger, even if there 1097fe9cc1dSMike Stump // is no codegen in the block that uses it. This is also useful to always do 1107fe9cc1dSMike Stump // this as if we didn't, we'd have to figure out all code that uses a self 1117fe9cc1dSMike Stump // pointer, including implicit uses. 1127fe9cc1dSMike Stump if (const ObjCMethodDecl *OMD 1137fe9cc1dSMike Stump = dyn_cast_or_null<ObjCMethodDecl>(CGF->CurFuncDecl)) { 1147fe9cc1dSMike Stump ImplicitParamDecl *SelfDecl = OMD->getSelfDecl(); 1157fe9cc1dSMike Stump BlockDeclRefExpr *BDRE = new (CGF->getContext()) 1167fe9cc1dSMike Stump BlockDeclRefExpr(SelfDecl, 1177fe9cc1dSMike Stump SelfDecl->getType(), SourceLocation(), false); 1187fe9cc1dSMike Stump CGF->AllocateBlockDecl(BDRE); 1197fe9cc1dSMike Stump } 1207fe9cc1dSMike Stump 1217fe9cc1dSMike Stump // FIXME: Also always forward the this pointer in C++ as well. 1227fe9cc1dSMike Stump 1237fe9cc1dSMike Stump for (size_t i = 0; i < Info.DeclRefs.size(); ++i) 1247fe9cc1dSMike Stump CGF->AllocateBlockDecl(Info.DeclRefs[i]); 125ed5e69feSAnders Carlsson } 126ed5e69feSAnders Carlsson 1272d33d630SMike Stump // FIXME: Push most into CGM, passing down a few bits, like current function 1282d33d630SMike Stump // name. 129b750d928SMike Stump llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) { 130ed5e69feSAnders Carlsson 131ed5e69feSAnders Carlsson std::string Name = CurFn->getName(); 132ed5e69feSAnders Carlsson CodeGenFunction::BlockInfo Info(0, Name.c_str()); 133ed5e69feSAnders Carlsson CollectBlockDeclRefInfo(BE->getBody(), Info); 134ed5e69feSAnders Carlsson 135ed5e69feSAnders Carlsson // Check if the block can be global. 1362d33d630SMike Stump // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like 1372d33d630SMike Stump // to just have one code path. We should move this function into CGM and pass 1382d33d630SMike Stump // CGF, then we can just check to see if CGF is 0. 13997d01d50SMike Stump if (0 && CanBlockBeGlobal(Info)) 140ed5e69feSAnders Carlsson return CGM.GetAddrOfGlobalBlock(BE, Name.c_str()); 14185284bacSMike Stump 1424446dcf0SMike Stump std::vector<llvm::Constant*> Elts(5); 14385284bacSMike Stump llvm::Constant *C; 144b750d928SMike Stump llvm::Value *V; 14585284bacSMike Stump 14685284bacSMike Stump { 14785284bacSMike Stump // C = BuildBlockStructInitlist(); 14885284bacSMike Stump unsigned int flags = BLOCK_HAS_DESCRIPTOR; 14985284bacSMike Stump 1504446dcf0SMike Stump // We run this first so that we set BlockHasCopyDispose from the entire 1514446dcf0SMike Stump // block literal. 1524446dcf0SMike Stump // __invoke 1534446dcf0SMike Stump uint64_t subBlockSize, subBlockAlign; 1544446dcf0SMike Stump llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; 155d2142cffSMike Stump bool subBlockHasCopyDispose = false; 1564446dcf0SMike Stump llvm::Function *Fn 157d2a3e617SMike Stump = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl, 158d2a3e617SMike Stump LocalDeclMap, 1595469f29aSMike Stump subBlockSize, 1604446dcf0SMike Stump subBlockAlign, 1614446dcf0SMike Stump subBlockDeclRefDecls, 162d2142cffSMike Stump subBlockHasCopyDispose); 163d2142cffSMike Stump BlockHasCopyDispose |= subBlockHasCopyDispose; 1644446dcf0SMike Stump Elts[3] = Fn; 1654446dcf0SMike Stump 16618bb9284SMike Stump // FIXME: Don't use BlockHasCopyDispose, it is set more often then 16718bb9284SMike Stump // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); } 168d2142cffSMike Stump if (subBlockHasCopyDispose) 16985284bacSMike Stump flags |= BLOCK_HAS_COPY_DISPOSE; 17085284bacSMike Stump 171499ae7ecSMike Stump // __isa 1727ab278dbSMike Stump C = CGM.getNSConcreteStackBlock(); 173ade90fd1SOwen Anderson C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty); 1744446dcf0SMike Stump Elts[0] = C; 17585284bacSMike Stump 17685284bacSMike Stump // __flags 17785284bacSMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 17885284bacSMike Stump CGM.getTypes().ConvertType(CGM.getContext().IntTy)); 179b7a2fe6fSOwen Anderson C = llvm::ConstantInt::get(IntTy, flags); 1804446dcf0SMike Stump Elts[1] = C; 18185284bacSMike Stump 18285284bacSMike Stump // __reserved 183b7a2fe6fSOwen Anderson C = llvm::ConstantInt::get(IntTy, 0); 1844446dcf0SMike Stump Elts[2] = C; 18585284bacSMike Stump 186b750d928SMike Stump if (subBlockDeclRefDecls.size() == 0) { 187d6ef62f0SMike Stump // __descriptor 1887fe9cc1dSMike Stump Elts[4] = BuildDescriptorBlockDecl(subBlockHasCopyDispose, subBlockSize, 1897fe9cc1dSMike Stump 0, 0); 190d6ef62f0SMike Stump 1913a139f37SMike Stump // Optimize to being a global block. 1923a139f37SMike Stump Elts[0] = CGM.getNSConcreteGlobalBlock(); 193b7a2fe6fSOwen Anderson Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL); 1943a139f37SMike Stump 19541eaf0acSNick Lewycky C = llvm::ConstantStruct::get(VMContext, Elts, false); 19685284bacSMike Stump 19785284bacSMike Stump char Name[32]; 1989319db8aSMike Stump sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount()); 199c10c8d3eSOwen Anderson C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, 20085284bacSMike Stump llvm::GlobalValue::InternalLinkage, 201c10c8d3eSOwen Anderson C, Name); 2025d2534adSMike Stump QualType BPT = BE->getType(); 203ade90fd1SOwen Anderson C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT)); 20485284bacSMike Stump return C; 20585284bacSMike Stump } 20685284bacSMike Stump 207b750d928SMike Stump std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size()); 208aeb0ffd5SMike Stump for (int i=0; i<4; ++i) 209b750d928SMike Stump Types[i] = Elts[i]->getType(); 210aeb0ffd5SMike Stump Types[4] = PtrToInt8Ty; 21185284bacSMike Stump 2121db7d04bSMike Stump for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) { 2131db7d04bSMike Stump const Expr *E = subBlockDeclRefDecls[i]; 2141db7d04bSMike Stump const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); 2151db7d04bSMike Stump QualType Ty = E->getType(); 21697d01d50SMike Stump if (BDRE && BDRE->isByRef()) { 21771d1d92dSAnders Carlsson Types[i+5] = llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0); 21897d01d50SMike Stump } else 2191db7d04bSMike Stump Types[i+5] = ConvertType(Ty); 2201db7d04bSMike Stump } 221b750d928SMike Stump 222758428f4SOwen Anderson llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true); 223b750d928SMike Stump 224b750d928SMike Stump llvm::AllocaInst *A = CreateTempAlloca(Ty); 225b750d928SMike Stump A->setAlignment(subBlockAlign); 226b750d928SMike Stump V = A; 227b750d928SMike Stump 228aeb0ffd5SMike Stump std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size()); 229aeb0ffd5SMike Stump int helpersize = 0; 230aeb0ffd5SMike Stump 231aeb0ffd5SMike Stump for (unsigned i=0; i<4; ++i) 232b750d928SMike Stump Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp")); 233b750d928SMike Stump 234b750d928SMike Stump for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) 235b750d928SMike Stump { 2361db7d04bSMike Stump // FIXME: Push const down. 2371db7d04bSMike Stump Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]); 2381db7d04bSMike Stump DeclRefExpr *DR; 2391db7d04bSMike Stump ValueDecl *VD; 240b750d928SMike Stump 2411db7d04bSMike Stump DR = dyn_cast<DeclRefExpr>(E); 2421db7d04bSMike Stump // Skip padding. 2431db7d04bSMike Stump if (DR) continue; 2441db7d04bSMike Stump 2451db7d04bSMike Stump BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); 2461db7d04bSMike Stump VD = BDRE->getDecl(); 2471db7d04bSMike Stump 24897d01d50SMike Stump llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp"); 249aeb0ffd5SMike Stump NoteForHelper[helpersize].index = i+5; 2502114d7c2SMike Stump NoteForHelper[helpersize].RequiresCopying 2512114d7c2SMike Stump = BlockRequiresCopying(VD->getType()); 252aeb0ffd5SMike Stump NoteForHelper[helpersize].flag 2532114d7c2SMike Stump = (VD->getType()->isBlockPointerType() 2542114d7c2SMike Stump ? BLOCK_FIELD_IS_BLOCK 2552114d7c2SMike Stump : BLOCK_FIELD_IS_OBJECT); 256aeb0ffd5SMike Stump 2571db7d04bSMike Stump if (LocalDeclMap[VD]) { 25897d01d50SMike Stump if (BDRE->isByRef()) { 259aeb0ffd5SMike Stump NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF | 26090d8daf5SMike Stump // FIXME: Someone double check this. 26190d8daf5SMike Stump (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0); 26297d01d50SMike Stump llvm::Value *Loc = LocalDeclMap[VD]; 26397d01d50SMike Stump Loc = Builder.CreateStructGEP(Loc, 1, "forwarding"); 26497d01d50SMike Stump Loc = Builder.CreateLoad(Loc, false); 26597d01d50SMike Stump Builder.CreateStore(Loc, Addr); 266aeb0ffd5SMike Stump ++helpersize; 26797d01d50SMike Stump continue; 26897d01d50SMike Stump } else 2691db7d04bSMike Stump E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD), 2701db7d04bSMike Stump VD->getType(), SourceLocation(), 2711db7d04bSMike Stump false, false); 2721db7d04bSMike Stump } 27397d01d50SMike Stump if (BDRE->isByRef()) { 274efd7caa8SMike Stump NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF | 275efd7caa8SMike Stump // FIXME: Someone double check this. 276efd7caa8SMike Stump (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0); 2771db7d04bSMike Stump E = new (getContext()) 2781db7d04bSMike Stump UnaryOperator(E, UnaryOperator::AddrOf, 2791db7d04bSMike Stump getContext().getPointerType(E->getType()), 2801db7d04bSMike Stump SourceLocation()); 28197d01d50SMike Stump } 282aeb0ffd5SMike Stump ++helpersize; 2831db7d04bSMike Stump 2841db7d04bSMike Stump RValue r = EmitAnyExpr(E, Addr, false); 28597d01d50SMike Stump if (r.isScalar()) { 28697d01d50SMike Stump llvm::Value *Loc = r.getScalarVal(); 28797d01d50SMike Stump const llvm::Type *Ty = Types[i+5]; 28897d01d50SMike Stump if (BDRE->isByRef()) { 2892d33d630SMike Stump // E is now the address of the value field, instead, we want the 2902d33d630SMike Stump // address of the actual ByRef struct. We optimize this slightly 2912d33d630SMike Stump // compared to gcc by not grabbing the forwarding slot as this must 2922d33d630SMike Stump // be done during Block_copy for us, and we can postpone the work 2932d33d630SMike Stump // until then. 2942d33d630SMike Stump uint64_t offset = BlockDecls[BDRE->getDecl()]; 2952d33d630SMike Stump 2962d33d630SMike Stump llvm::Value *BlockLiteral = LoadBlockStruct(); 2972d33d630SMike Stump 2982d33d630SMike Stump Loc = Builder.CreateGEP(BlockLiteral, 29941a75027SOwen Anderson llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), 3002d33d630SMike Stump offset), 3012d33d630SMike Stump "block.literal"); 3029793f0e4SOwen Anderson Ty = llvm::PointerType::get(Ty, 0); 30397d01d50SMike Stump Loc = Builder.CreateBitCast(Loc, Ty); 3042d33d630SMike Stump Loc = Builder.CreateLoad(Loc, false); 3052d33d630SMike Stump // Loc = Builder.CreateBitCast(Loc, Ty); 30697d01d50SMike Stump } 30797d01d50SMike Stump Builder.CreateStore(Loc, Addr); 30897d01d50SMike Stump } else if (r.isComplex()) 309b750d928SMike Stump // FIXME: implement 310b750d928SMike Stump ErrorUnsupported(BE, "complex in block literal"); 311b750d928SMike Stump else if (r.isAggregate()) 312b750d928SMike Stump ; // Already created into the destination 313b750d928SMike Stump else 314b750d928SMike Stump assert (0 && "bad block variable"); 315b750d928SMike Stump // FIXME: Ensure that the offset created by the backend for 316b750d928SMike Stump // the struct matches the previously computed offset in BlockDecls. 317b750d928SMike Stump } 318aeb0ffd5SMike Stump NoteForHelper.resize(helpersize); 319d6ef62f0SMike Stump 320d6ef62f0SMike Stump // __descriptor 321d2142cffSMike Stump llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockHasCopyDispose, 322d2142cffSMike Stump subBlockSize, Ty, 323aeb0ffd5SMike Stump &NoteForHelper); 324aeb0ffd5SMike Stump Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty); 325aeb0ffd5SMike Stump Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp")); 326b750d928SMike Stump } 327b750d928SMike Stump 328b750d928SMike Stump QualType BPT = BE->getType(); 329b750d928SMike Stump return Builder.CreateBitCast(V, ConvertType(BPT)); 330b750d928SMike Stump } 33185284bacSMike Stump 33285284bacSMike Stump 33395435674SMike Stump const llvm::Type *BlockModule::getBlockDescriptorType() { 334650c932dSMike Stump if (BlockDescriptorType) 335650c932dSMike Stump return BlockDescriptorType; 3362437cbfaSAnders Carlsson 3372437cbfaSAnders Carlsson const llvm::Type *UnsignedLongTy = 338650c932dSMike Stump getTypes().ConvertType(getContext().UnsignedLongTy); 3392437cbfaSAnders Carlsson 3402437cbfaSAnders Carlsson // struct __block_descriptor { 3412437cbfaSAnders Carlsson // unsigned long reserved; 3422437cbfaSAnders Carlsson // unsigned long block_size; 3432437cbfaSAnders Carlsson // }; 344758428f4SOwen Anderson BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(), 345758428f4SOwen Anderson UnsignedLongTy, 3462437cbfaSAnders Carlsson UnsignedLongTy, 3472437cbfaSAnders Carlsson NULL); 3482437cbfaSAnders Carlsson 349650c932dSMike Stump getModule().addTypeName("struct.__block_descriptor", 350650c932dSMike Stump BlockDescriptorType); 3512437cbfaSAnders Carlsson 352650c932dSMike Stump return BlockDescriptorType; 3532437cbfaSAnders Carlsson } 3542437cbfaSAnders Carlsson 35595435674SMike Stump const llvm::Type *BlockModule::getGenericBlockLiteralType() { 356005c9a62SMike Stump if (GenericBlockLiteralType) 357005c9a62SMike Stump return GenericBlockLiteralType; 3582437cbfaSAnders Carlsson 3592437cbfaSAnders Carlsson const llvm::Type *BlockDescPtrTy = 3609793f0e4SOwen Anderson llvm::PointerType::getUnqual(getBlockDescriptorType()); 3612437cbfaSAnders Carlsson 36292bbd6d4SMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 36392bbd6d4SMike Stump getTypes().ConvertType(getContext().IntTy)); 36492bbd6d4SMike Stump 3652437cbfaSAnders Carlsson // struct __block_literal_generic { 3665d2534adSMike Stump // void *__isa; 3675d2534adSMike Stump // int __flags; 3685d2534adSMike Stump // int __reserved; 3695d2534adSMike Stump // void (*__invoke)(void *); 3705d2534adSMike Stump // struct __block_descriptor *__descriptor; 3712437cbfaSAnders Carlsson // }; 372758428f4SOwen Anderson GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(), 373758428f4SOwen Anderson PtrToInt8Ty, 37492bbd6d4SMike Stump IntTy, 37592bbd6d4SMike Stump IntTy, 376626aecc4SMike Stump PtrToInt8Ty, 3772437cbfaSAnders Carlsson BlockDescPtrTy, 3782437cbfaSAnders Carlsson NULL); 3792437cbfaSAnders Carlsson 380005c9a62SMike Stump getModule().addTypeName("struct.__block_literal_generic", 381005c9a62SMike Stump GenericBlockLiteralType); 3822437cbfaSAnders Carlsson 383005c9a62SMike Stump return GenericBlockLiteralType; 3842437cbfaSAnders Carlsson } 3852437cbfaSAnders Carlsson 38695435674SMike Stump const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() { 3875d2534adSMike Stump if (GenericExtendedBlockLiteralType) 3885d2534adSMike Stump return GenericExtendedBlockLiteralType; 3895d2534adSMike Stump 3905d2534adSMike Stump const llvm::Type *BlockDescPtrTy = 3919793f0e4SOwen Anderson llvm::PointerType::getUnqual(getBlockDescriptorType()); 3925d2534adSMike Stump 3935d2534adSMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 3945d2534adSMike Stump getTypes().ConvertType(getContext().IntTy)); 3955d2534adSMike Stump 3965d2534adSMike Stump // struct __block_literal_generic { 3975d2534adSMike Stump // void *__isa; 3985d2534adSMike Stump // int __flags; 3995d2534adSMike Stump // int __reserved; 4005d2534adSMike Stump // void (*__invoke)(void *); 4015d2534adSMike Stump // struct __block_descriptor *__descriptor; 4025d2534adSMike Stump // void *__copy_func_helper_decl; 4035d2534adSMike Stump // void *__destroy_func_decl; 4045d2534adSMike Stump // }; 405758428f4SOwen Anderson GenericExtendedBlockLiteralType = llvm::StructType::get(IntTy->getContext(), 406758428f4SOwen Anderson PtrToInt8Ty, 4075d2534adSMike Stump IntTy, 4085d2534adSMike Stump IntTy, 409626aecc4SMike Stump PtrToInt8Ty, 4105d2534adSMike Stump BlockDescPtrTy, 411626aecc4SMike Stump PtrToInt8Ty, 412626aecc4SMike Stump PtrToInt8Ty, 4135d2534adSMike Stump NULL); 4145d2534adSMike Stump 4155d2534adSMike Stump getModule().addTypeName("struct.__block_literal_extended_generic", 4165d2534adSMike Stump GenericExtendedBlockLiteralType); 4175d2534adSMike Stump 4185d2534adSMike Stump return GenericExtendedBlockLiteralType; 4195d2534adSMike Stump } 4205d2534adSMike Stump 4216a60fa24SAnders Carlsson RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) { 4222437cbfaSAnders Carlsson const BlockPointerType *BPT = 423c23c7e6aSTed Kremenek E->getCallee()->getType()->getAs<BlockPointerType>(); 4242437cbfaSAnders Carlsson 4252437cbfaSAnders Carlsson llvm::Value *Callee = EmitScalarExpr(E->getCallee()); 4262437cbfaSAnders Carlsson 4272437cbfaSAnders Carlsson // Get a pointer to the generic block literal. 4282437cbfaSAnders Carlsson const llvm::Type *BlockLiteralTy = 4299793f0e4SOwen Anderson llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType()); 4302437cbfaSAnders Carlsson 4312437cbfaSAnders Carlsson // Bitcast the callee to a block literal. 4322437cbfaSAnders Carlsson llvm::Value *BlockLiteral = 4332437cbfaSAnders Carlsson Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal"); 4342437cbfaSAnders Carlsson 4352437cbfaSAnders Carlsson // Get the function pointer from the literal. 4362437cbfaSAnders Carlsson llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp"); 4372437cbfaSAnders Carlsson 4382437cbfaSAnders Carlsson BlockLiteral = 4392437cbfaSAnders Carlsson Builder.CreateBitCast(BlockLiteral, 440abd5b90eSBenjamin Kramer llvm::Type::getInt8PtrTy(VMContext), 4412437cbfaSAnders Carlsson "tmp"); 4422437cbfaSAnders Carlsson 4432437cbfaSAnders Carlsson // Add the block literal. 4442437cbfaSAnders Carlsson QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy); 4452437cbfaSAnders Carlsson CallArgList Args; 4462437cbfaSAnders Carlsson Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy)); 4472437cbfaSAnders Carlsson 448479e6fc7SAnders Carlsson QualType FnType = BPT->getPointeeType(); 449479e6fc7SAnders Carlsson 4502437cbfaSAnders Carlsson // And the rest of the arguments. 4519dd450bbSJohn McCall EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(), 452479e6fc7SAnders Carlsson E->arg_begin(), E->arg_end()); 4532437cbfaSAnders Carlsson 4545f50c651SAnders Carlsson // Load the function. 4555f50c651SAnders Carlsson llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp"); 4565f50c651SAnders Carlsson 4579dd450bbSJohn McCall QualType ResultType = FnType->getAs<FunctionType>()->getResultType(); 458a60cbcdfSAnders Carlsson 459a60cbcdfSAnders Carlsson const CGFunctionInfo &FnInfo = 460a60cbcdfSAnders Carlsson CGM.getTypes().getFunctionInfo(ResultType, Args); 4615f50c651SAnders Carlsson 4625f50c651SAnders Carlsson // Cast the function pointer to the right type. 4635f50c651SAnders Carlsson const llvm::Type *BlockFTy = 464a60cbcdfSAnders Carlsson CGM.getTypes().GetFunctionType(FnInfo, false); 4655f50c651SAnders Carlsson 4669793f0e4SOwen Anderson const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy); 4675f50c651SAnders Carlsson Func = Builder.CreateBitCast(Func, BlockFTyPtr); 4685f50c651SAnders Carlsson 4692437cbfaSAnders Carlsson // And call the block. 470350da607SAnders Carlsson return EmitCall(FnInfo, Func, Args); 4712437cbfaSAnders Carlsson } 4726a60fa24SAnders Carlsson 4737fe9cc1dSMike Stump uint64_t CodeGenFunction::AllocateBlockDecl(const BlockDeclRefExpr *E) { 4740168f4b2SAnders Carlsson const ValueDecl *VD = E->getDecl(); 4750168f4b2SAnders Carlsson uint64_t &offset = BlockDecls[VD]; 4760168f4b2SAnders Carlsson 47797d01d50SMike Stump // See if we have already allocated an offset for this variable. 4787fe9cc1dSMike Stump if (offset) 4797fe9cc1dSMike Stump return offset; 4807fe9cc1dSMike Stump 4814446dcf0SMike Stump // Don't run the expensive check, unless we have to. 4824446dcf0SMike Stump if (!BlockHasCopyDispose && BlockRequiresCopying(E->getType())) 4834446dcf0SMike Stump BlockHasCopyDispose = true; 4847fe9cc1dSMike Stump 48597d01d50SMike Stump // if not, allocate one now. 48697d01d50SMike Stump offset = getBlockOffset(E); 4877fe9cc1dSMike Stump 4887fe9cc1dSMike Stump return offset; 48997d01d50SMike Stump } 49097d01d50SMike Stump 4917fe9cc1dSMike Stump llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) { 4927fe9cc1dSMike Stump const ValueDecl *VD = E->getDecl(); 4937fe9cc1dSMike Stump uint64_t offset = AllocateBlockDecl(E); 4947fe9cc1dSMike Stump 4957fe9cc1dSMike Stump 49697d01d50SMike Stump llvm::Value *BlockLiteral = LoadBlockStruct(); 49797d01d50SMike Stump llvm::Value *V = Builder.CreateGEP(BlockLiteral, 49841a75027SOwen Anderson llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), 49997d01d50SMike Stump offset), 5002d33d630SMike Stump "block.literal"); 50197d01d50SMike Stump if (E->isByRef()) { 50297d01d50SMike Stump const llvm::Type *PtrStructTy 5030168f4b2SAnders Carlsson = llvm::PointerType::get(BuildByRefType(VD), 0); 504efd7caa8SMike Stump // The block literal will need a copy/destroy helper. 505efd7caa8SMike Stump BlockHasCopyDispose = true; 5060168f4b2SAnders Carlsson 5070168f4b2SAnders Carlsson const llvm::Type *Ty = PtrStructTy; 5089793f0e4SOwen Anderson Ty = llvm::PointerType::get(Ty, 0); 50997d01d50SMike Stump V = Builder.CreateBitCast(V, Ty); 51097d01d50SMike Stump V = Builder.CreateLoad(V, false); 51197d01d50SMike Stump V = Builder.CreateStructGEP(V, 1, "forwarding"); 51297d01d50SMike Stump V = Builder.CreateLoad(V, false); 51397d01d50SMike Stump V = Builder.CreateBitCast(V, PtrStructTy); 5140168f4b2SAnders Carlsson V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD), 5150168f4b2SAnders Carlsson VD->getNameAsString()); 51697d01d50SMike Stump } else { 5170168f4b2SAnders Carlsson const llvm::Type *Ty = CGM.getTypes().ConvertType(VD->getType()); 5180168f4b2SAnders Carlsson 5199793f0e4SOwen Anderson Ty = llvm::PointerType::get(Ty, 0); 52097d01d50SMike Stump V = Builder.CreateBitCast(V, Ty); 52197d01d50SMike Stump } 52297d01d50SMike Stump return V; 52397d01d50SMike Stump } 52497d01d50SMike Stump 525692c6e37SMike Stump void CodeGenFunction::BlockForwardSelf() { 526692c6e37SMike Stump const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); 527692c6e37SMike Stump ImplicitParamDecl *SelfDecl = OMD->getSelfDecl(); 528692c6e37SMike Stump llvm::Value *&DMEntry = LocalDeclMap[SelfDecl]; 529692c6e37SMike Stump if (DMEntry) 530692c6e37SMike Stump return; 531692c6e37SMike Stump // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care 532692c6e37SMike Stump BlockDeclRefExpr *BDRE = new (getContext()) 533692c6e37SMike Stump BlockDeclRefExpr(SelfDecl, 534692c6e37SMike Stump SelfDecl->getType(), SourceLocation(), false); 535692c6e37SMike Stump DMEntry = GetAddrOfBlockDecl(BDRE); 536692c6e37SMike Stump } 537692c6e37SMike Stump 5382d5a2878SMike Stump llvm::Constant * 5396c39666aSMike Stump BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) { 5406a60fa24SAnders Carlsson // Generate the block descriptor. 5416a60fa24SAnders Carlsson const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy); 54292bbd6d4SMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 54392bbd6d4SMike Stump getTypes().ConvertType(getContext().IntTy)); 5446a60fa24SAnders Carlsson 5456a60fa24SAnders Carlsson llvm::Constant *DescriptorFields[2]; 5466a60fa24SAnders Carlsson 5476a60fa24SAnders Carlsson // Reserved 5480b75f23bSOwen Anderson DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy); 5496a60fa24SAnders Carlsson 5506a60fa24SAnders Carlsson // Block literal size. For global blocks we just use the size of the generic 5516a60fa24SAnders Carlsson // block literal struct. 5526a60fa24SAnders Carlsson uint64_t BlockLiteralSize = 553005c9a62SMike Stump TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8; 554170229f6SOwen Anderson DescriptorFields[1] = 555b7a2fe6fSOwen Anderson llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize); 5566a60fa24SAnders Carlsson 5576a60fa24SAnders Carlsson llvm::Constant *DescriptorStruct = 55841eaf0acSNick Lewycky llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 2, false); 5596a60fa24SAnders Carlsson 5606a60fa24SAnders Carlsson llvm::GlobalVariable *Descriptor = 561c10c8d3eSOwen Anderson new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true, 5626a60fa24SAnders Carlsson llvm::GlobalVariable::InternalLinkage, 563c10c8d3eSOwen Anderson DescriptorStruct, "__block_descriptor_global"); 5646a60fa24SAnders Carlsson 5656a60fa24SAnders Carlsson // Generate the constants for the block literal. 5666a60fa24SAnders Carlsson llvm::Constant *LiteralFields[5]; 5676a60fa24SAnders Carlsson 5682d5a2878SMike Stump CodeGenFunction::BlockInfo Info(0, n); 569b750d928SMike Stump uint64_t subBlockSize, subBlockAlign; 5701db7d04bSMike Stump llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; 571a61bb4d1SDaniel Dunbar bool subBlockHasCopyDispose = false; 5725469f29aSMike Stump llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap; 573cb2fbcb0SMike Stump llvm::Function *Fn 574692c6e37SMike Stump = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap, 5755469f29aSMike Stump subBlockSize, 576b750d928SMike Stump subBlockAlign, 5774446dcf0SMike Stump subBlockDeclRefDecls, 5784446dcf0SMike Stump subBlockHasCopyDispose); 579cb2fbcb0SMike Stump assert(subBlockSize == BlockLiteralSize 580cb2fbcb0SMike Stump && "no imports allowed for global block"); 5816a60fa24SAnders Carlsson 5826a60fa24SAnders Carlsson // isa 583971f9b66SMike Stump LiteralFields[0] = getNSConcreteGlobalBlock(); 5846a60fa24SAnders Carlsson 5856a60fa24SAnders Carlsson // Flags 5865878c797SAnders Carlsson LiteralFields[1] = 587b7a2fe6fSOwen Anderson llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR); 5886a60fa24SAnders Carlsson 5896a60fa24SAnders Carlsson // Reserved 5900b75f23bSOwen Anderson LiteralFields[2] = llvm::Constant::getNullValue(IntTy); 5916a60fa24SAnders Carlsson 5926a60fa24SAnders Carlsson // Function 5936a60fa24SAnders Carlsson LiteralFields[3] = Fn; 5946a60fa24SAnders Carlsson 5956a60fa24SAnders Carlsson // Descriptor 5966a60fa24SAnders Carlsson LiteralFields[4] = Descriptor; 5976a60fa24SAnders Carlsson 5986a60fa24SAnders Carlsson llvm::Constant *BlockLiteralStruct = 59941eaf0acSNick Lewycky llvm::ConstantStruct::get(VMContext, &LiteralFields[0], 5, false); 6006a60fa24SAnders Carlsson 6016a60fa24SAnders Carlsson llvm::GlobalVariable *BlockLiteral = 602c10c8d3eSOwen Anderson new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true, 6036a60fa24SAnders Carlsson llvm::GlobalVariable::InternalLinkage, 604c10c8d3eSOwen Anderson BlockLiteralStruct, "__block_literal_global"); 6056a60fa24SAnders Carlsson 6066a60fa24SAnders Carlsson return BlockLiteral; 6076a60fa24SAnders Carlsson } 6086a60fa24SAnders Carlsson 609cb2fbcb0SMike Stump llvm::Value *CodeGenFunction::LoadBlockStruct() { 61038382028SMike Stump llvm::Value *V = Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], 61138382028SMike Stump "self"); 61238382028SMike Stump // For now, we codegen based upon byte offsets. 61338382028SMike Stump return Builder.CreateBitCast(V, PtrToInt8Ty); 614cb2fbcb0SMike Stump } 615cb2fbcb0SMike Stump 6164446dcf0SMike Stump llvm::Function * 6174446dcf0SMike Stump CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr, 618cb2fbcb0SMike Stump const BlockInfo& Info, 619692c6e37SMike Stump const Decl *OuterFuncDecl, 6205469f29aSMike Stump llvm::DenseMap<const Decl*, llvm::Value*> ldm, 621b750d928SMike Stump uint64_t &Size, 622b750d928SMike Stump uint64_t &Align, 6234446dcf0SMike Stump llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls, 6244446dcf0SMike Stump bool &subBlockHasCopyDispose) { 6259074ed8cSDevang Patel 6269074ed8cSDevang Patel // Check if we should generate debug info for this block. 6279074ed8cSDevang Patel if (CGM.getDebugInfo()) 6289074ed8cSDevang Patel DebugInfo = CGM.getDebugInfo(); 6299074ed8cSDevang Patel 6305469f29aSMike Stump // Arrange for local static and local extern declarations to appear 6315469f29aSMike Stump // to be local to this function as well, as they are directly referenced 6325469f29aSMike Stump // in a block. 6335469f29aSMike Stump for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin(); 6345469f29aSMike Stump i != ldm.end(); 6355469f29aSMike Stump ++i) { 6365469f29aSMike Stump const VarDecl *VD = dyn_cast<VarDecl>(i->first); 6375469f29aSMike Stump 6380ca16601SDaniel Dunbar if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage()) 6395469f29aSMike Stump LocalDeclMap[VD] = i->second; 6405469f29aSMike Stump } 6415469f29aSMike Stump 64209a9b6e3SEli Friedman // FIXME: We need to rearrange the code for copy/dispose so we have this 64309a9b6e3SEli Friedman // sooner, so we can calculate offsets correctly. 64409a9b6e3SEli Friedman if (!BlockHasCopyDispose) 64509a9b6e3SEli Friedman BlockOffset = CGM.getTargetData() 64609a9b6e3SEli Friedman .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8; 64709a9b6e3SEli Friedman else 64809a9b6e3SEli Friedman BlockOffset = CGM.getTargetData() 64909a9b6e3SEli Friedman .getTypeStoreSizeInBits(CGM.getGenericExtendedBlockLiteralType()) / 8; 65009a9b6e3SEli Friedman BlockAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 65109a9b6e3SEli Friedman 652aae4349dSFariborz Jahanian const FunctionType *BlockFunctionType = BExpr->getFunctionType(); 653aae4349dSFariborz Jahanian QualType ResultType; 654aae4349dSFariborz Jahanian bool IsVariadic; 655a4f6b6b1SFariborz Jahanian if (const FunctionProtoType *FTy = 656a4f6b6b1SFariborz Jahanian dyn_cast<FunctionProtoType>(BlockFunctionType)) { 657aae4349dSFariborz Jahanian ResultType = FTy->getResultType(); 658aae4349dSFariborz Jahanian IsVariadic = FTy->isVariadic(); 659658fe02dSMike Stump } else { 660aae4349dSFariborz Jahanian // K&R style block. 661aae4349dSFariborz Jahanian ResultType = BlockFunctionType->getResultType(); 662aae4349dSFariborz Jahanian IsVariadic = false; 663aae4349dSFariborz Jahanian } 6646a60fa24SAnders Carlsson 6656a60fa24SAnders Carlsson FunctionArgList Args; 6666a60fa24SAnders Carlsson 6677fe9cc1dSMike Stump CurFuncDecl = OuterFuncDecl; 6687fe9cc1dSMike Stump 6693385fe12SChris Lattner const BlockDecl *BD = BExpr->getBlockDecl(); 6706a60fa24SAnders Carlsson 6717fe9cc1dSMike Stump IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor"); 672d015328fSMike Stump 673*945fec05SMike Stump // Allocate all BlockDeclRefDecls, so we can calculate the right ParmTy below. 6747fe9cc1dSMike Stump // FIXME: Resolve testsuite problems, then enable. 6757fe9cc1dSMike Stump if (0) 6767fe9cc1dSMike Stump AllocateAllBlockDeclRefs(Info, this); 6777fe9cc1dSMike Stump 6787fe9cc1dSMike Stump QualType ParmTy = getContext().getBlockParmType(BlockDeclRefDecls); 6796a60fa24SAnders Carlsson // FIXME: This leaks 6806a60fa24SAnders Carlsson ImplicitParamDecl *SelfDecl = 6816a60fa24SAnders Carlsson ImplicitParamDecl::Create(getContext(), 0, 682d015328fSMike Stump SourceLocation(), II, 683d015328fSMike Stump ParmTy); 6846a60fa24SAnders Carlsson 6856a60fa24SAnders Carlsson Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType())); 686cb2fbcb0SMike Stump BlockStructDecl = SelfDecl; 6876a60fa24SAnders Carlsson 688c4b30e59SSteve Naroff for (BlockDecl::param_const_iterator i = BD->param_begin(), 6896a60fa24SAnders Carlsson e = BD->param_end(); i != e; ++i) 690eecd39f2SMike Stump Args.push_back(std::make_pair(*i, (*i)->getType())); 6916a60fa24SAnders Carlsson 6926a60fa24SAnders Carlsson const CGFunctionInfo &FI = 693aae4349dSFariborz Jahanian CGM.getTypes().getFunctionInfo(ResultType, Args); 6946a60fa24SAnders Carlsson 6952d5a2878SMike Stump std::string Name = std::string("__") + Info.Name + "_block_invoke_"; 6966a60fa24SAnders Carlsson CodeGenTypes &Types = CGM.getTypes(); 697aae4349dSFariborz Jahanian const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic); 6986a60fa24SAnders Carlsson 6996a60fa24SAnders Carlsson llvm::Function *Fn = 7006a60fa24SAnders Carlsson llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 7016a60fa24SAnders Carlsson Name, 7026a60fa24SAnders Carlsson &CGM.getModule()); 7036a60fa24SAnders Carlsson 704c3e7cff6SDaniel Dunbar CGM.SetInternalFunctionAttributes(BD, Fn, FI); 705c3e7cff6SDaniel Dunbar 706324f80b1SChris Lattner StartFunction(BD, ResultType, Fn, Args, 7073385fe12SChris Lattner BExpr->getBody()->getLocEnd()); 7082e722b91SMike Stump 709324f80b1SChris Lattner CurFuncDecl = OuterFuncDecl; 71028ec0cf8SChris Lattner CurCodeDecl = BD; 711017460abSMike Stump 712017460abSMike Stump // Save a spot to insert the debug information for all the BlockDeclRefDecls. 713017460abSMike Stump llvm::BasicBlock *entry = Builder.GetInsertBlock(); 714017460abSMike Stump llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint(); 715017460abSMike Stump --entry_ptr; 716017460abSMike Stump 7173385fe12SChris Lattner EmitStmt(BExpr->getBody()); 718017460abSMike Stump 7197d69911aSMike Stump // Remember where we were... 7207d69911aSMike Stump llvm::BasicBlock *resume = Builder.GetInsertBlock(); 721017460abSMike Stump 7227d69911aSMike Stump // Go back to the entry. 723017460abSMike Stump ++entry_ptr; 724017460abSMike Stump Builder.SetInsertPoint(entry, entry_ptr); 725017460abSMike Stump 7262e722b91SMike Stump if (CGDebugInfo *DI = getDebugInfo()) { 7272e722b91SMike Stump // Emit debug information for all the BlockDeclRefDecls. 7282e722b91SMike Stump for (unsigned i=0; i < BlockDeclRefDecls.size(); ++i) { 7292e722b91SMike Stump const Expr *E = BlockDeclRefDecls[i]; 7302e722b91SMike Stump const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); 7312e722b91SMike Stump if (BDRE) { 7322e722b91SMike Stump const ValueDecl *D = BDRE->getDecl(); 7332e722b91SMike Stump DI->setLocation(D->getLocation()); 7342e722b91SMike Stump DI->EmitDeclareOfBlockDeclRefVariable(BDRE, 7352e722b91SMike Stump LocalDeclMap[getBlockStructDecl()], 7362e722b91SMike Stump Builder, this); 7372e722b91SMike Stump } 7382e722b91SMike Stump } 7392e722b91SMike Stump } 7407d69911aSMike Stump // And resume where we left off. 7417d69911aSMike Stump if (resume == 0) 7427d69911aSMike Stump Builder.ClearInsertionPoint(); 7437d69911aSMike Stump else 7447d69911aSMike Stump Builder.SetInsertPoint(resume); 7452e722b91SMike Stump 7463385fe12SChris Lattner FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc()); 7476a60fa24SAnders Carlsson 748b750d928SMike Stump // The runtime needs a minimum alignment of a void *. 749b750d928SMike Stump uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 750b750d928SMike Stump BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign); 751b750d928SMike Stump 752cb2fbcb0SMike Stump Size = BlockOffset; 753b750d928SMike Stump Align = BlockAlign; 754b750d928SMike Stump subBlockDeclRefDecls = BlockDeclRefDecls; 7554446dcf0SMike Stump subBlockHasCopyDispose |= BlockHasCopyDispose; 7566a60fa24SAnders Carlsson return Fn; 7576a60fa24SAnders Carlsson } 7581db7d04bSMike Stump 759aeb0ffd5SMike Stump uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) { 7601db7d04bSMike Stump const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl()); 7611db7d04bSMike Stump 7621db7d04bSMike Stump uint64_t Size = getContext().getTypeSize(D->getType()) / 8; 7631db7d04bSMike Stump uint64_t Align = getContext().getDeclAlignInBytes(D); 7641db7d04bSMike Stump 7651db7d04bSMike Stump if (BDRE->isByRef()) { 7661db7d04bSMike Stump Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8; 7671db7d04bSMike Stump Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 7681db7d04bSMike Stump } 7691db7d04bSMike Stump 7701db7d04bSMike Stump assert ((Align > 0) && "alignment must be 1 byte or more"); 7711db7d04bSMike Stump 7721db7d04bSMike Stump uint64_t OldOffset = BlockOffset; 7731db7d04bSMike Stump 7741db7d04bSMike Stump // Ensure proper alignment, even if it means we have to have a gap 7751db7d04bSMike Stump BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align); 7761db7d04bSMike Stump BlockAlign = std::max(Align, BlockAlign); 7771db7d04bSMike Stump 7781db7d04bSMike Stump uint64_t Pad = BlockOffset - OldOffset; 7791db7d04bSMike Stump if (Pad) { 78041a75027SOwen Anderson llvm::ArrayType::get(llvm::Type::getInt8Ty(VMContext), Pad); 7811db7d04bSMike Stump QualType PadTy = getContext().getConstantArrayType(getContext().CharTy, 7821db7d04bSMike Stump llvm::APInt(32, Pad), 7831db7d04bSMike Stump ArrayType::Normal, 0); 7841db7d04bSMike Stump ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(), 7856032ef1aSArgyrios Kyrtzidis 0, QualType(PadTy), 0, VarDecl::None); 7861db7d04bSMike Stump Expr *E; 7871db7d04bSMike Stump E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(), 7881db7d04bSMike Stump SourceLocation(), false, false); 7891db7d04bSMike Stump BlockDeclRefDecls.push_back(E); 7901db7d04bSMike Stump } 7911db7d04bSMike Stump BlockDeclRefDecls.push_back(BDRE); 7921db7d04bSMike Stump 7931db7d04bSMike Stump BlockOffset += Size; 7941db7d04bSMike Stump return BlockOffset-Size; 7951db7d04bSMike Stump } 79697d01d50SMike Stump 797aeb0ffd5SMike Stump llvm::Constant *BlockFunction:: 798aeb0ffd5SMike Stump GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T, 79967645930SMike Stump std::vector<HelperInfo> *NoteForHelperp) { 8000c743277SMike Stump QualType R = getContext().VoidTy; 8010c743277SMike Stump 8020c743277SMike Stump FunctionArgList Args; 8030c743277SMike Stump // FIXME: This leaks 804aeb0ffd5SMike Stump ImplicitParamDecl *Dst = 8057fe9cc1dSMike Stump ImplicitParamDecl::Create(getContext(), 0, 8067fe9cc1dSMike Stump SourceLocation(), 0, 807aeb0ffd5SMike Stump getContext().getPointerType(getContext().VoidTy)); 808aeb0ffd5SMike Stump Args.push_back(std::make_pair(Dst, Dst->getType())); 8090c743277SMike Stump ImplicitParamDecl *Src = 8107fe9cc1dSMike Stump ImplicitParamDecl::Create(getContext(), 0, 8117fe9cc1dSMike Stump SourceLocation(), 0, 8120c743277SMike Stump getContext().getPointerType(getContext().VoidTy)); 8130c743277SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 8140c743277SMike Stump 8150c743277SMike Stump const CGFunctionInfo &FI = 8160c743277SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 8170c743277SMike Stump 818cbc2bcafSMike Stump // FIXME: We'd like to put these into a mergable by content, with 819cbc2bcafSMike Stump // internal linkage. 8200c743277SMike Stump std::string Name = std::string("__copy_helper_block_"); 8210c743277SMike Stump CodeGenTypes &Types = CGM.getTypes(); 8220c743277SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 8230c743277SMike Stump 8240c743277SMike Stump llvm::Function *Fn = 8250c743277SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 8260c743277SMike Stump Name, 8270c743277SMike Stump &CGM.getModule()); 8280c743277SMike Stump 8290c743277SMike Stump IdentifierInfo *II 8300c743277SMike Stump = &CGM.getContext().Idents.get("__copy_helper_block_"); 8310c743277SMike Stump 8320c743277SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 8330c743277SMike Stump getContext().getTranslationUnitDecl(), 83460ed5604SArgyrios Kyrtzidis SourceLocation(), II, R, 0, 8350c743277SMike Stump FunctionDecl::Static, false, 8360c743277SMike Stump true); 8370c743277SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 838aeb0ffd5SMike Stump 839aeb0ffd5SMike Stump llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); 840aeb0ffd5SMike Stump llvm::Type *PtrPtrT; 84167645930SMike Stump 84267645930SMike Stump if (NoteForHelperp) { 84367645930SMike Stump std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp; 84467645930SMike Stump 8459793f0e4SOwen Anderson PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 846aeb0ffd5SMike Stump SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); 847aeb0ffd5SMike Stump SrcObj = Builder.CreateLoad(SrcObj); 848aeb0ffd5SMike Stump 849aeb0ffd5SMike Stump llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst); 8503b65ac26SMike Stump llvm::Type *PtrPtrT; 8519793f0e4SOwen Anderson PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 8523b65ac26SMike Stump DstObj = Builder.CreateBitCast(DstObj, PtrPtrT); 8533b65ac26SMike Stump DstObj = Builder.CreateLoad(DstObj); 854aeb0ffd5SMike Stump 855aeb0ffd5SMike Stump for (unsigned i=0; i < NoteForHelper.size(); ++i) { 856aeb0ffd5SMike Stump int flag = NoteForHelper[i].flag; 857aeb0ffd5SMike Stump int index = NoteForHelper[i].index; 858aeb0ffd5SMike Stump 859aeb0ffd5SMike Stump if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) 860aeb0ffd5SMike Stump || NoteForHelper[i].RequiresCopying) { 861aeb0ffd5SMike Stump llvm::Value *Srcv = SrcObj; 862aeb0ffd5SMike Stump Srcv = Builder.CreateStructGEP(Srcv, index); 863aeb0ffd5SMike Stump Srcv = Builder.CreateBitCast(Srcv, 8649793f0e4SOwen Anderson llvm::PointerType::get(PtrToInt8Ty, 0)); 865aeb0ffd5SMike Stump Srcv = Builder.CreateLoad(Srcv); 866aeb0ffd5SMike Stump 867aeb0ffd5SMike Stump llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index); 868aeb0ffd5SMike Stump Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty); 869aeb0ffd5SMike Stump 87041a75027SOwen Anderson llvm::Value *N = llvm::ConstantInt::get( 87141a75027SOwen Anderson llvm::Type::getInt32Ty(T->getContext()), flag); 872aeb0ffd5SMike Stump llvm::Value *F = getBlockObjectAssign(); 873aeb0ffd5SMike Stump Builder.CreateCall3(F, Dstv, Srcv, N); 874aeb0ffd5SMike Stump } 875aeb0ffd5SMike Stump } 87667645930SMike Stump } 877aeb0ffd5SMike Stump 8780c743277SMike Stump CGF.FinishFunction(); 8790c743277SMike Stump 880ade90fd1SOwen Anderson return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 88197d01d50SMike Stump } 88297d01d50SMike Stump 883d6ef62f0SMike Stump llvm::Constant *BlockFunction:: 884aeb0ffd5SMike Stump GenerateDestroyHelperFunction(bool BlockHasCopyDispose, 885aeb0ffd5SMike Stump const llvm::StructType* T, 88667645930SMike Stump std::vector<HelperInfo> *NoteForHelperp) { 8870c743277SMike Stump QualType R = getContext().VoidTy; 8880c743277SMike Stump 8890c743277SMike Stump FunctionArgList Args; 8900c743277SMike Stump // FIXME: This leaks 8910c743277SMike Stump ImplicitParamDecl *Src = 8927fe9cc1dSMike Stump ImplicitParamDecl::Create(getContext(), 0, 8937fe9cc1dSMike Stump SourceLocation(), 0, 8940c743277SMike Stump getContext().getPointerType(getContext().VoidTy)); 8950c743277SMike Stump 8960c743277SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 8970c743277SMike Stump 8980c743277SMike Stump const CGFunctionInfo &FI = 8990c743277SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 9000c743277SMike Stump 901cbc2bcafSMike Stump // FIXME: We'd like to put these into a mergable by content, with 902cbc2bcafSMike Stump // internal linkage. 9030c743277SMike Stump std::string Name = std::string("__destroy_helper_block_"); 9040c743277SMike Stump CodeGenTypes &Types = CGM.getTypes(); 9050c743277SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 9060c743277SMike Stump 9070c743277SMike Stump llvm::Function *Fn = 9080c743277SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 9090c743277SMike Stump Name, 9100c743277SMike Stump &CGM.getModule()); 9110c743277SMike Stump 9120c743277SMike Stump IdentifierInfo *II 9130c743277SMike Stump = &CGM.getContext().Idents.get("__destroy_helper_block_"); 9140c743277SMike Stump 9150c743277SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 9160c743277SMike Stump getContext().getTranslationUnitDecl(), 91760ed5604SArgyrios Kyrtzidis SourceLocation(), II, R, 0, 9180c743277SMike Stump FunctionDecl::Static, false, 9190c743277SMike Stump true); 9200c743277SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 9216f7d9f87SMike Stump 92267645930SMike Stump if (NoteForHelperp) { 92367645930SMike Stump std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp; 92467645930SMike Stump 9256f7d9f87SMike Stump llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); 9266f7d9f87SMike Stump llvm::Type *PtrPtrT; 9279793f0e4SOwen Anderson PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 9286f7d9f87SMike Stump SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); 9296f7d9f87SMike Stump SrcObj = Builder.CreateLoad(SrcObj); 9306f7d9f87SMike Stump 9316f7d9f87SMike Stump for (unsigned i=0; i < NoteForHelper.size(); ++i) { 9326f7d9f87SMike Stump int flag = NoteForHelper[i].flag; 9336f7d9f87SMike Stump int index = NoteForHelper[i].index; 9346f7d9f87SMike Stump 9356f7d9f87SMike Stump if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) 9366f7d9f87SMike Stump || NoteForHelper[i].RequiresCopying) { 9376f7d9f87SMike Stump llvm::Value *Srcv = SrcObj; 9386f7d9f87SMike Stump Srcv = Builder.CreateStructGEP(Srcv, index); 9396f7d9f87SMike Stump Srcv = Builder.CreateBitCast(Srcv, 9409793f0e4SOwen Anderson llvm::PointerType::get(PtrToInt8Ty, 0)); 9416f7d9f87SMike Stump Srcv = Builder.CreateLoad(Srcv); 9426f7d9f87SMike Stump 9436f7d9f87SMike Stump BuildBlockRelease(Srcv, flag); 9446f7d9f87SMike Stump } 9456f7d9f87SMike Stump } 94667645930SMike Stump } 9476f7d9f87SMike Stump 9480c743277SMike Stump CGF.FinishFunction(); 9490c743277SMike Stump 950ade90fd1SOwen Anderson return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 9510c743277SMike Stump } 9520c743277SMike Stump 953aeb0ffd5SMike Stump llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T, 95467645930SMike Stump std::vector<HelperInfo> *NoteForHelper) { 955aeb0ffd5SMike Stump return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose, 956aeb0ffd5SMike Stump T, NoteForHelper); 9570c743277SMike Stump } 9580c743277SMike Stump 959aeb0ffd5SMike Stump llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T, 96067645930SMike Stump std::vector<HelperInfo> *NoteForHelperp) { 961aeb0ffd5SMike Stump return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose, 96267645930SMike Stump T, NoteForHelperp); 96397d01d50SMike Stump } 964626aecc4SMike Stump 965f89230d9SMike Stump llvm::Constant *BlockFunction:: 966f89230d9SMike Stump GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) { 967ee2a5ee5SMike Stump QualType R = getContext().VoidTy; 968ee2a5ee5SMike Stump 969ee2a5ee5SMike Stump FunctionArgList Args; 970ee2a5ee5SMike Stump // FIXME: This leaks 971f89230d9SMike Stump ImplicitParamDecl *Dst = 9727fe9cc1dSMike Stump ImplicitParamDecl::Create(getContext(), 0, 9737fe9cc1dSMike Stump SourceLocation(), 0, 974f89230d9SMike Stump getContext().getPointerType(getContext().VoidTy)); 975f89230d9SMike Stump Args.push_back(std::make_pair(Dst, Dst->getType())); 976f89230d9SMike Stump 977f89230d9SMike Stump // FIXME: This leaks 978ee2a5ee5SMike Stump ImplicitParamDecl *Src = 9797fe9cc1dSMike Stump ImplicitParamDecl::Create(getContext(), 0, 9807fe9cc1dSMike Stump SourceLocation(), 0, 981ee2a5ee5SMike Stump getContext().getPointerType(getContext().VoidTy)); 982ee2a5ee5SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 983ee2a5ee5SMike Stump 984ee2a5ee5SMike Stump const CGFunctionInfo &FI = 985ee2a5ee5SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 986ee2a5ee5SMike Stump 987ee2a5ee5SMike Stump std::string Name = std::string("__Block_byref_id_object_copy_"); 988ee2a5ee5SMike Stump CodeGenTypes &Types = CGM.getTypes(); 989ee2a5ee5SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 990ee2a5ee5SMike Stump 991cbc2bcafSMike Stump // FIXME: We'd like to put these into a mergable by content, with 992cbc2bcafSMike Stump // internal linkage. 993ee2a5ee5SMike Stump llvm::Function *Fn = 994ee2a5ee5SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 995ee2a5ee5SMike Stump Name, 996ee2a5ee5SMike Stump &CGM.getModule()); 997ee2a5ee5SMike Stump 998ee2a5ee5SMike Stump IdentifierInfo *II 999ee2a5ee5SMike Stump = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_"); 1000ee2a5ee5SMike Stump 1001ee2a5ee5SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 1002ee2a5ee5SMike Stump getContext().getTranslationUnitDecl(), 100360ed5604SArgyrios Kyrtzidis SourceLocation(), II, R, 0, 1004ee2a5ee5SMike Stump FunctionDecl::Static, false, 1005ee2a5ee5SMike Stump true); 1006ee2a5ee5SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 1007f89230d9SMike Stump 1008f89230d9SMike Stump // dst->x 1009f89230d9SMike Stump llvm::Value *V = CGF.GetAddrOfLocalVar(Dst); 10109793f0e4SOwen Anderson V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0)); 10113b65ac26SMike Stump V = Builder.CreateLoad(V); 1012f89230d9SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 1013f89230d9SMike Stump llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty); 1014f89230d9SMike Stump 1015f89230d9SMike Stump // src->x 1016f89230d9SMike Stump V = CGF.GetAddrOfLocalVar(Src); 1017f89230d9SMike Stump V = Builder.CreateLoad(V); 1018f89230d9SMike Stump V = Builder.CreateBitCast(V, T); 1019f89230d9SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 10209793f0e4SOwen Anderson V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); 1021f89230d9SMike Stump llvm::Value *SrcObj = Builder.CreateLoad(V); 1022f89230d9SMike Stump 1023f89230d9SMike Stump flag |= BLOCK_BYREF_CALLER; 1024f89230d9SMike Stump 102541a75027SOwen Anderson llvm::Value *N = llvm::ConstantInt::get( 102641a75027SOwen Anderson llvm::Type::getInt32Ty(T->getContext()), flag); 1027f89230d9SMike Stump llvm::Value *F = getBlockObjectAssign(); 1028f89230d9SMike Stump Builder.CreateCall3(F, DstObj, SrcObj, N); 1029f89230d9SMike Stump 1030ee2a5ee5SMike Stump CGF.FinishFunction(); 1031ee2a5ee5SMike Stump 1032ade90fd1SOwen Anderson return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 1033ee2a5ee5SMike Stump } 1034ee2a5ee5SMike Stump 1035fbe25dd1SMike Stump llvm::Constant * 1036fbe25dd1SMike Stump BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T, 1037fbe25dd1SMike Stump int flag) { 1038ee2a5ee5SMike Stump QualType R = getContext().VoidTy; 1039ee2a5ee5SMike Stump 1040ee2a5ee5SMike Stump FunctionArgList Args; 1041ee2a5ee5SMike Stump // FIXME: This leaks 1042ee2a5ee5SMike Stump ImplicitParamDecl *Src = 10437fe9cc1dSMike Stump ImplicitParamDecl::Create(getContext(), 0, 10447fe9cc1dSMike Stump SourceLocation(), 0, 1045ee2a5ee5SMike Stump getContext().getPointerType(getContext().VoidTy)); 1046ee2a5ee5SMike Stump 1047ee2a5ee5SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 1048ee2a5ee5SMike Stump 1049ee2a5ee5SMike Stump const CGFunctionInfo &FI = 1050ee2a5ee5SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 1051ee2a5ee5SMike Stump 1052ee2a5ee5SMike Stump std::string Name = std::string("__Block_byref_id_object_dispose_"); 1053ee2a5ee5SMike Stump CodeGenTypes &Types = CGM.getTypes(); 1054ee2a5ee5SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 1055ee2a5ee5SMike Stump 1056cbc2bcafSMike Stump // FIXME: We'd like to put these into a mergable by content, with 1057cbc2bcafSMike Stump // internal linkage. 1058ee2a5ee5SMike Stump llvm::Function *Fn = 1059ee2a5ee5SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 1060ee2a5ee5SMike Stump Name, 1061ee2a5ee5SMike Stump &CGM.getModule()); 1062ee2a5ee5SMike Stump 1063ee2a5ee5SMike Stump IdentifierInfo *II 1064ee2a5ee5SMike Stump = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_"); 1065ee2a5ee5SMike Stump 1066ee2a5ee5SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 1067ee2a5ee5SMike Stump getContext().getTranslationUnitDecl(), 106860ed5604SArgyrios Kyrtzidis SourceLocation(), II, R, 0, 1069ee2a5ee5SMike Stump FunctionDecl::Static, false, 1070ee2a5ee5SMike Stump true); 1071ee2a5ee5SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 1072fbe25dd1SMike Stump 1073fbe25dd1SMike Stump llvm::Value *V = CGF.GetAddrOfLocalVar(Src); 10749793f0e4SOwen Anderson V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0)); 10753b65ac26SMike Stump V = Builder.CreateLoad(V); 1076fbe25dd1SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 10779793f0e4SOwen Anderson V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); 10783b65ac26SMike Stump V = Builder.CreateLoad(V); 1079fbe25dd1SMike Stump 1080fbe25dd1SMike Stump flag |= BLOCK_BYREF_CALLER; 1081fbe25dd1SMike Stump BuildBlockRelease(V, flag); 1082ee2a5ee5SMike Stump CGF.FinishFunction(); 1083ee2a5ee5SMike Stump 1084ade90fd1SOwen Anderson return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 1085ee2a5ee5SMike Stump } 1086ee2a5ee5SMike Stump 1087f89230d9SMike Stump llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T, 1088cbc2bcafSMike Stump int flag, unsigned Align) { 1089cbc2bcafSMike Stump // All alignments below that of pointer alignment collpase down to just 1090cbc2bcafSMike Stump // pointer alignment, as we always have at least that much alignment to begin 1091cbc2bcafSMike Stump // with. 1092cbc2bcafSMike Stump Align /= unsigned(CGF.Target.getPointerAlign(0)/8); 1093cbc2bcafSMike Stump // As an optimization, we only generate a single function of each kind we 1094cbc2bcafSMike Stump // might need. We need a different one for each alignment and for each 1095cbc2bcafSMike Stump // setting of flags. We mix Align and flag to get the kind. 1096cbc2bcafSMike Stump uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag; 1097cbc2bcafSMike Stump llvm::Constant *& Entry = CGM.AssignCache[kind]; 1098cbc2bcafSMike Stump if (Entry) 1099cbc2bcafSMike Stump return Entry; 1100cbc2bcafSMike Stump return Entry=CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag); 1101ee2a5ee5SMike Stump } 1102ee2a5ee5SMike Stump 1103fbe25dd1SMike Stump llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T, 1104cbc2bcafSMike Stump int flag, 1105cbc2bcafSMike Stump unsigned Align) { 1106cbc2bcafSMike Stump // All alignments below that of pointer alignment collpase down to just 1107cbc2bcafSMike Stump // pointer alignment, as we always have at least that much alignment to begin 1108cbc2bcafSMike Stump // with. 1109cbc2bcafSMike Stump Align /= unsigned(CGF.Target.getPointerAlign(0)/8); 1110cbc2bcafSMike Stump // As an optimization, we only generate a single function of each kind we 1111cbc2bcafSMike Stump // might need. We need a different one for each alignment and for each 1112cbc2bcafSMike Stump // setting of flags. We mix Align and flag to get the kind. 1113cbc2bcafSMike Stump uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag; 1114cbc2bcafSMike Stump llvm::Constant *& Entry = CGM.DestroyCache[kind]; 1115cbc2bcafSMike Stump if (Entry) 1116cbc2bcafSMike Stump return Entry; 1117cbc2bcafSMike Stump return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag); 1118ee2a5ee5SMike Stump } 1119ee2a5ee5SMike Stump 1120626aecc4SMike Stump llvm::Value *BlockFunction::getBlockObjectDispose() { 1121626aecc4SMike Stump if (CGM.BlockObjectDispose == 0) { 1122626aecc4SMike Stump const llvm::FunctionType *FTy; 1123626aecc4SMike Stump std::vector<const llvm::Type*> ArgTys; 112441a75027SOwen Anderson const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext); 1125626aecc4SMike Stump ArgTys.push_back(PtrToInt8Ty); 112641a75027SOwen Anderson ArgTys.push_back(llvm::Type::getInt32Ty(VMContext)); 11279793f0e4SOwen Anderson FTy = llvm::FunctionType::get(ResultType, ArgTys, false); 1128626aecc4SMike Stump CGM.BlockObjectDispose 1129626aecc4SMike Stump = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose"); 1130626aecc4SMike Stump } 1131626aecc4SMike Stump return CGM.BlockObjectDispose; 1132626aecc4SMike Stump } 1133626aecc4SMike Stump 1134f89230d9SMike Stump llvm::Value *BlockFunction::getBlockObjectAssign() { 1135f89230d9SMike Stump if (CGM.BlockObjectAssign == 0) { 1136f89230d9SMike Stump const llvm::FunctionType *FTy; 1137f89230d9SMike Stump std::vector<const llvm::Type*> ArgTys; 113841a75027SOwen Anderson const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext); 1139f89230d9SMike Stump ArgTys.push_back(PtrToInt8Ty); 1140f89230d9SMike Stump ArgTys.push_back(PtrToInt8Ty); 114141a75027SOwen Anderson ArgTys.push_back(llvm::Type::getInt32Ty(VMContext)); 11429793f0e4SOwen Anderson FTy = llvm::FunctionType::get(ResultType, ArgTys, false); 1143f89230d9SMike Stump CGM.BlockObjectAssign 1144f89230d9SMike Stump = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign"); 1145f89230d9SMike Stump } 1146f89230d9SMike Stump return CGM.BlockObjectAssign; 1147f89230d9SMike Stump } 1148f89230d9SMike Stump 1149fbe25dd1SMike Stump void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) { 1150626aecc4SMike Stump llvm::Value *F = getBlockObjectDispose(); 1151fbe25dd1SMike Stump llvm::Value *N; 1152626aecc4SMike Stump V = Builder.CreateBitCast(V, PtrToInt8Ty); 115341a75027SOwen Anderson N = llvm::ConstantInt::get(llvm::Type::getInt32Ty(V->getContext()), flag); 1154626aecc4SMike Stump Builder.CreateCall2(F, V, N); 1155626aecc4SMike Stump } 11564446dcf0SMike Stump 11574446dcf0SMike Stump ASTContext &BlockFunction::getContext() const { return CGM.getContext(); } 1158aeb0ffd5SMike Stump 1159aeb0ffd5SMike Stump BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf, 1160aeb0ffd5SMike Stump CGBuilderTy &B) 1161170229f6SOwen Anderson : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) { 116241a75027SOwen Anderson PtrToInt8Ty = llvm::PointerType::getUnqual( 116341a75027SOwen Anderson llvm::Type::getInt8Ty(VMContext)); 1164aeb0ffd5SMike Stump 1165aeb0ffd5SMike Stump BlockHasCopyDispose = false; 1166aeb0ffd5SMike Stump } 1167