12437cbfaSAnders Carlsson //===--- CGBlocks.cpp - Emit LLVM Code for declarations -------------------===// 22437cbfaSAnders Carlsson // 32437cbfaSAnders Carlsson // The LLVM Compiler Infrastructure 42437cbfaSAnders Carlsson // 52437cbfaSAnders Carlsson // This file is distributed under the University of Illinois Open Source 62437cbfaSAnders Carlsson // License. See LICENSE.TXT for details. 72437cbfaSAnders Carlsson // 82437cbfaSAnders Carlsson //===----------------------------------------------------------------------===// 92437cbfaSAnders Carlsson // 102437cbfaSAnders Carlsson // This contains code to emit blocks. 112437cbfaSAnders Carlsson // 122437cbfaSAnders Carlsson //===----------------------------------------------------------------------===// 132437cbfaSAnders Carlsson 142437cbfaSAnders Carlsson #include "CodeGenFunction.h" 152437cbfaSAnders Carlsson #include "CodeGenModule.h" 162437cbfaSAnders Carlsson #include "llvm/Module.h" 176a60fa24SAnders Carlsson #include "llvm/Target/TargetData.h" 182437cbfaSAnders Carlsson 192437cbfaSAnders Carlsson #include <algorithm> 202437cbfaSAnders Carlsson 212437cbfaSAnders Carlsson using namespace clang; 222437cbfaSAnders Carlsson using namespace CodeGen; 232437cbfaSAnders Carlsson 240874160bSMike Stump // Temporary code to enable testing of __block variables 250874160bSMike Stump // #include "clang/Frontend/CompileOptions.h" 260874160bSMike Stump #include "llvm/Support/CommandLine.h" 270874160bSMike Stump static llvm::cl::opt<bool> 280874160bSMike Stump Enable__block("f__block", 290874160bSMike Stump // See all the FIXMEs for the various work that needs to be done 300874160bSMike Stump llvm::cl::desc("temporary option to turn on __block precessing " 310874160bSMike Stump "even though the code isn't done yet"), 320874160bSMike Stump llvm::cl::ValueDisallowed, llvm::cl::AllowInverse, 334446dcf0SMike Stump llvm::cl::ZeroOrMore, 344446dcf0SMike Stump llvm::cl::init(false)); 350874160bSMike Stump 36d6ef62f0SMike Stump llvm::Constant *CodeGenFunction:: 37aeb0ffd5SMike Stump BuildDescriptorBlockDecl(uint64_t Size, const llvm::StructType* Ty, 38aeb0ffd5SMike Stump std::vector<HelperInfo> *NoteForHelper) { 39c2c38331SMike Stump const llvm::Type *UnsignedLongTy 40c2c38331SMike Stump = CGM.getTypes().ConvertType(getContext().UnsignedLongTy); 4185284bacSMike Stump llvm::Constant *C; 4285284bacSMike Stump std::vector<llvm::Constant*> Elts; 4385284bacSMike Stump 4485284bacSMike Stump // reserved 45c2c38331SMike Stump C = llvm::ConstantInt::get(UnsignedLongTy, 0); 4685284bacSMike Stump Elts.push_back(C); 4785284bacSMike Stump 4885284bacSMike Stump // Size 492ac40a9fSMike Stump // FIXME: What is the right way to say this doesn't fit? We should give 502ac40a9fSMike Stump // a user diagnostic in that case. Better fix would be to change the 512ac40a9fSMike Stump // API to size_t. 52cb2fbcb0SMike Stump C = llvm::ConstantInt::get(UnsignedLongTy, Size); 5385284bacSMike Stump Elts.push_back(C); 5485284bacSMike Stump 5585284bacSMike Stump if (BlockHasCopyDispose) { 5685284bacSMike Stump // copy_func_helper_decl 57aeb0ffd5SMike Stump Elts.push_back(BuildCopyHelper(Ty, *NoteForHelper)); 5885284bacSMike Stump 5985284bacSMike Stump // destroy_func_decl 60aeb0ffd5SMike Stump Elts.push_back(BuildDestroyHelper(Ty, *NoteForHelper)); 6185284bacSMike Stump } 6285284bacSMike Stump 6385284bacSMike Stump C = llvm::ConstantStruct::get(Elts); 6485284bacSMike Stump 6585284bacSMike Stump C = new llvm::GlobalVariable(C->getType(), true, 6685284bacSMike Stump llvm::GlobalValue::InternalLinkage, 67499ae7ecSMike Stump C, "__block_descriptor_tmp", &CGM.getModule()); 6885284bacSMike Stump return C; 6985284bacSMike Stump } 7085284bacSMike Stump 7195435674SMike Stump llvm::Constant *BlockModule::getNSConcreteGlobalBlock() { 72971f9b66SMike Stump if (NSConcreteGlobalBlock) 73971f9b66SMike Stump return NSConcreteGlobalBlock; 74971f9b66SMike Stump 7552d9c49dSMike Stump // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the 762d33d630SMike Stump // same thing as CreateRuntimeFunction if there's already a variable with the 772d33d630SMike Stump // same name. 78971f9b66SMike Stump NSConcreteGlobalBlock 79971f9b66SMike Stump = new llvm::GlobalVariable(PtrToInt8Ty, false, 80971f9b66SMike Stump llvm::GlobalValue::ExternalLinkage, 81971f9b66SMike Stump 0, "_NSConcreteGlobalBlock", 82971f9b66SMike Stump &getModule()); 83971f9b66SMike Stump 84971f9b66SMike Stump return NSConcreteGlobalBlock; 85971f9b66SMike Stump } 86971f9b66SMike Stump 8795435674SMike Stump llvm::Constant *BlockModule::getNSConcreteStackBlock() { 887ab278dbSMike Stump if (NSConcreteStackBlock) 897ab278dbSMike Stump return NSConcreteStackBlock; 907ab278dbSMike Stump 9152d9c49dSMike Stump // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the 922d33d630SMike Stump // same thing as CreateRuntimeFunction if there's already a variable with the 932d33d630SMike Stump // same name. 947ab278dbSMike Stump NSConcreteStackBlock 957ab278dbSMike Stump = new llvm::GlobalVariable(PtrToInt8Ty, false, 967ab278dbSMike Stump llvm::GlobalValue::ExternalLinkage, 977ab278dbSMike Stump 0, "_NSConcreteStackBlock", 987ab278dbSMike Stump &getModule()); 997ab278dbSMike Stump 1007ab278dbSMike Stump return NSConcreteStackBlock; 1017ab278dbSMike Stump } 1027ab278dbSMike Stump 103ed5e69feSAnders Carlsson static void CollectBlockDeclRefInfo(const Stmt *S, 104ed5e69feSAnders Carlsson CodeGenFunction::BlockInfo &Info) { 105ed5e69feSAnders Carlsson for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); 106ed5e69feSAnders Carlsson I != E; ++I) 107dd942713SDaniel Dunbar if (*I) 108ed5e69feSAnders Carlsson CollectBlockDeclRefInfo(*I, Info); 109ed5e69feSAnders Carlsson 110ed5e69feSAnders Carlsson if (const BlockDeclRefExpr *DE = dyn_cast<BlockDeclRefExpr>(S)) { 111ed5e69feSAnders Carlsson // FIXME: Handle enums. 112ed5e69feSAnders Carlsson if (isa<FunctionDecl>(DE->getDecl())) 113ed5e69feSAnders Carlsson return; 114ed5e69feSAnders Carlsson 115ed5e69feSAnders Carlsson if (DE->isByRef()) 116ed5e69feSAnders Carlsson Info.ByRefDeclRefs.push_back(DE); 117ed5e69feSAnders Carlsson else 118ed5e69feSAnders Carlsson Info.ByCopyDeclRefs.push_back(DE); 119ed5e69feSAnders Carlsson } 120ed5e69feSAnders Carlsson } 121ed5e69feSAnders Carlsson 1222d33d630SMike Stump /// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be 1232d33d630SMike Stump /// declared as a global variable instead of on the stack. 124ed5e69feSAnders Carlsson static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info) 125ed5e69feSAnders Carlsson { 126ed5e69feSAnders Carlsson return Info.ByRefDeclRefs.empty() && Info.ByCopyDeclRefs.empty(); 127ed5e69feSAnders Carlsson } 128ed5e69feSAnders Carlsson 1292d33d630SMike Stump // FIXME: Push most into CGM, passing down a few bits, like current function 1302d33d630SMike Stump // name. 131b750d928SMike Stump llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) { 132ed5e69feSAnders Carlsson 133ed5e69feSAnders Carlsson std::string Name = CurFn->getName(); 134ed5e69feSAnders Carlsson CodeGenFunction::BlockInfo Info(0, Name.c_str()); 135ed5e69feSAnders Carlsson CollectBlockDeclRefInfo(BE->getBody(), Info); 136ed5e69feSAnders Carlsson 137ed5e69feSAnders Carlsson // Check if the block can be global. 1382d33d630SMike Stump // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like 1392d33d630SMike Stump // to just have one code path. We should move this function into CGM and pass 1402d33d630SMike Stump // CGF, then we can just check to see if CGF is 0. 14197d01d50SMike Stump if (0 && CanBlockBeGlobal(Info)) 142ed5e69feSAnders Carlsson return CGM.GetAddrOfGlobalBlock(BE, Name.c_str()); 14385284bacSMike Stump 1444446dcf0SMike Stump std::vector<llvm::Constant*> Elts(5); 14585284bacSMike Stump llvm::Constant *C; 146b750d928SMike Stump llvm::Value *V; 14785284bacSMike Stump 14885284bacSMike Stump { 14985284bacSMike Stump // C = BuildBlockStructInitlist(); 15085284bacSMike Stump unsigned int flags = BLOCK_HAS_DESCRIPTOR; 15185284bacSMike Stump 1524446dcf0SMike Stump // We run this first so that we set BlockHasCopyDispose from the entire 1534446dcf0SMike Stump // block literal. 1544446dcf0SMike Stump // __invoke 1554446dcf0SMike Stump uint64_t subBlockSize, subBlockAlign; 1564446dcf0SMike Stump llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; 1574446dcf0SMike Stump llvm::Function *Fn 1584446dcf0SMike Stump = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, subBlockSize, 1594446dcf0SMike Stump subBlockAlign, 1604446dcf0SMike Stump subBlockDeclRefDecls, 1614446dcf0SMike Stump BlockHasCopyDispose); 1624446dcf0SMike Stump Elts[3] = Fn; 1634446dcf0SMike Stump 1644446dcf0SMike Stump if (!Enable__block && BlockHasCopyDispose) 1654446dcf0SMike Stump ErrorUnsupported(BE, "block literal that requires copy/dispose"); 1664446dcf0SMike Stump 16785284bacSMike Stump if (BlockHasCopyDispose) 16885284bacSMike Stump flags |= BLOCK_HAS_COPY_DISPOSE; 16985284bacSMike Stump 170499ae7ecSMike Stump // __isa 1717ab278dbSMike Stump C = CGM.getNSConcreteStackBlock(); 17285284bacSMike Stump C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty); 1734446dcf0SMike Stump Elts[0] = C; 17485284bacSMike Stump 17585284bacSMike Stump // __flags 17685284bacSMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 17785284bacSMike Stump CGM.getTypes().ConvertType(CGM.getContext().IntTy)); 17885284bacSMike Stump C = llvm::ConstantInt::get(IntTy, flags); 1794446dcf0SMike Stump Elts[1] = C; 18085284bacSMike Stump 18185284bacSMike Stump // __reserved 18285284bacSMike Stump C = llvm::ConstantInt::get(IntTy, 0); 1834446dcf0SMike Stump Elts[2] = C; 18485284bacSMike Stump 185b750d928SMike Stump if (subBlockDeclRefDecls.size() == 0) { 186d6ef62f0SMike Stump // __descriptor 187aeb0ffd5SMike Stump Elts[4] = BuildDescriptorBlockDecl(subBlockSize, 0, 0); 188d6ef62f0SMike Stump 1893a139f37SMike Stump // Optimize to being a global block. 1903a139f37SMike Stump Elts[0] = CGM.getNSConcreteGlobalBlock(); 1913a139f37SMike Stump Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL); 1923a139f37SMike Stump 19385284bacSMike Stump C = llvm::ConstantStruct::get(Elts); 19485284bacSMike Stump 19585284bacSMike Stump char Name[32]; 1969319db8aSMike Stump sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount()); 19785284bacSMike Stump C = new llvm::GlobalVariable(C->getType(), true, 19885284bacSMike Stump llvm::GlobalValue::InternalLinkage, 19985284bacSMike Stump C, Name, &CGM.getModule()); 2005d2534adSMike Stump QualType BPT = BE->getType(); 2015d2534adSMike Stump C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT)); 20285284bacSMike Stump return C; 20385284bacSMike Stump } 20485284bacSMike Stump 205b750d928SMike Stump std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size()); 206aeb0ffd5SMike Stump for (int i=0; i<4; ++i) 207b750d928SMike Stump Types[i] = Elts[i]->getType(); 208aeb0ffd5SMike Stump Types[4] = PtrToInt8Ty; 20985284bacSMike Stump 2101db7d04bSMike Stump for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) { 2111db7d04bSMike Stump const Expr *E = subBlockDeclRefDecls[i]; 2121db7d04bSMike Stump const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); 2131db7d04bSMike Stump QualType Ty = E->getType(); 21497d01d50SMike Stump if (BDRE && BDRE->isByRef()) { 21597d01d50SMike Stump uint64_t Align = getContext().getDeclAlignInBytes(BDRE->getDecl()); 21697d01d50SMike Stump Types[i+5] = llvm::PointerType::get(BuildByRefType(Ty, Align), 0); 21797d01d50SMike Stump } else 2181db7d04bSMike Stump Types[i+5] = ConvertType(Ty); 2191db7d04bSMike Stump } 220b750d928SMike Stump 221aeb0ffd5SMike Stump llvm::StructType *Ty = llvm::StructType::get(Types, true); 222b750d928SMike Stump 223b750d928SMike Stump llvm::AllocaInst *A = CreateTempAlloca(Ty); 224b750d928SMike Stump A->setAlignment(subBlockAlign); 225b750d928SMike Stump V = A; 226b750d928SMike Stump 227aeb0ffd5SMike Stump std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size()); 228aeb0ffd5SMike Stump int helpersize = 0; 229aeb0ffd5SMike Stump 230aeb0ffd5SMike Stump for (unsigned i=0; i<4; ++i) 231b750d928SMike Stump Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp")); 232b750d928SMike Stump 233b750d928SMike Stump for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) 234b750d928SMike Stump { 2351db7d04bSMike Stump // FIXME: Push const down. 2361db7d04bSMike Stump Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]); 2371db7d04bSMike Stump DeclRefExpr *DR; 2381db7d04bSMike Stump ValueDecl *VD; 239b750d928SMike Stump 2401db7d04bSMike Stump DR = dyn_cast<DeclRefExpr>(E); 2411db7d04bSMike Stump // Skip padding. 2421db7d04bSMike Stump if (DR) continue; 2431db7d04bSMike Stump 2441db7d04bSMike Stump BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); 2451db7d04bSMike Stump VD = BDRE->getDecl(); 2461db7d04bSMike Stump 24797d01d50SMike Stump llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp"); 248aeb0ffd5SMike Stump NoteForHelper[helpersize].index = i+5; 249aeb0ffd5SMike Stump NoteForHelper[helpersize].RequiresCopying = BlockRequiresCopying(VD->getType()); 250aeb0ffd5SMike Stump NoteForHelper[helpersize].flag 251aeb0ffd5SMike Stump = VD->getType()->isBlockPointerType() ? BLOCK_FIELD_IS_BLOCK : BLOCK_FIELD_IS_OBJECT; 252aeb0ffd5SMike Stump 2531db7d04bSMike Stump if (LocalDeclMap[VD]) { 25497d01d50SMike Stump if (BDRE->isByRef()) { 255aeb0ffd5SMike Stump // FIXME: For only local, or all byrefs? 256aeb0ffd5SMike Stump NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF | 257*90d8daf5SMike Stump // FIXME: Someone double check this. 258*90d8daf5SMike Stump (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0); 25997d01d50SMike Stump const llvm::Type *Ty = Types[i+5]; 26097d01d50SMike Stump llvm::Value *Loc = LocalDeclMap[VD]; 26197d01d50SMike Stump Loc = Builder.CreateStructGEP(Loc, 1, "forwarding"); 26297d01d50SMike Stump Loc = Builder.CreateLoad(Loc, false); 26397d01d50SMike Stump Loc = Builder.CreateBitCast(Loc, Ty); 26497d01d50SMike Stump Builder.CreateStore(Loc, Addr); 265aeb0ffd5SMike Stump ++helpersize; 26697d01d50SMike Stump continue; 26797d01d50SMike Stump } else 2681db7d04bSMike Stump E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD), 2691db7d04bSMike Stump VD->getType(), SourceLocation(), 2701db7d04bSMike Stump false, false); 2711db7d04bSMike Stump } 27297d01d50SMike Stump if (BDRE->isByRef()) { 2731db7d04bSMike Stump E = new (getContext()) 2741db7d04bSMike Stump UnaryOperator(E, UnaryOperator::AddrOf, 2751db7d04bSMike Stump getContext().getPointerType(E->getType()), 2761db7d04bSMike Stump SourceLocation()); 27797d01d50SMike Stump } 278aeb0ffd5SMike Stump ++helpersize; 2791db7d04bSMike Stump 2801db7d04bSMike Stump RValue r = EmitAnyExpr(E, Addr, false); 28197d01d50SMike Stump if (r.isScalar()) { 28297d01d50SMike Stump llvm::Value *Loc = r.getScalarVal(); 28397d01d50SMike Stump const llvm::Type *Ty = Types[i+5]; 28497d01d50SMike Stump if (BDRE->isByRef()) { 2852d33d630SMike Stump // E is now the address of the value field, instead, we want the 2862d33d630SMike Stump // address of the actual ByRef struct. We optimize this slightly 2872d33d630SMike Stump // compared to gcc by not grabbing the forwarding slot as this must 2882d33d630SMike Stump // be done during Block_copy for us, and we can postpone the work 2892d33d630SMike Stump // until then. 2902d33d630SMike Stump uint64_t offset = BlockDecls[BDRE->getDecl()]; 2912d33d630SMike Stump 2922d33d630SMike Stump llvm::Value *BlockLiteral = LoadBlockStruct(); 2932d33d630SMike Stump 2942d33d630SMike Stump Loc = Builder.CreateGEP(BlockLiteral, 2952d33d630SMike Stump llvm::ConstantInt::get(llvm::Type::Int64Ty, 2962d33d630SMike Stump offset), 2972d33d630SMike Stump "block.literal"); 2982d33d630SMike Stump Ty = llvm::PointerType::get(Ty, 0); 29997d01d50SMike Stump Loc = Builder.CreateBitCast(Loc, Ty); 3002d33d630SMike Stump Loc = Builder.CreateLoad(Loc, false); 3012d33d630SMike Stump // Loc = Builder.CreateBitCast(Loc, Ty); 30297d01d50SMike Stump } 30397d01d50SMike Stump Builder.CreateStore(Loc, Addr); 30497d01d50SMike Stump } else if (r.isComplex()) 305b750d928SMike Stump // FIXME: implement 306b750d928SMike Stump ErrorUnsupported(BE, "complex in block literal"); 307b750d928SMike Stump else if (r.isAggregate()) 308b750d928SMike Stump ; // Already created into the destination 309b750d928SMike Stump else 310b750d928SMike Stump assert (0 && "bad block variable"); 311b750d928SMike Stump // FIXME: Ensure that the offset created by the backend for 312b750d928SMike Stump // the struct matches the previously computed offset in BlockDecls. 313b750d928SMike Stump } 314aeb0ffd5SMike Stump NoteForHelper.resize(helpersize); 315d6ef62f0SMike Stump 316d6ef62f0SMike Stump // __descriptor 317aeb0ffd5SMike Stump llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockSize, Ty, 318aeb0ffd5SMike Stump &NoteForHelper); 319aeb0ffd5SMike Stump Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty); 320aeb0ffd5SMike Stump Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp")); 321b750d928SMike Stump } 322b750d928SMike Stump 323b750d928SMike Stump QualType BPT = BE->getType(); 324b750d928SMike Stump return Builder.CreateBitCast(V, ConvertType(BPT)); 325b750d928SMike Stump } 32685284bacSMike Stump 32785284bacSMike Stump 32895435674SMike Stump const llvm::Type *BlockModule::getBlockDescriptorType() { 329650c932dSMike Stump if (BlockDescriptorType) 330650c932dSMike Stump return BlockDescriptorType; 3312437cbfaSAnders Carlsson 3322437cbfaSAnders Carlsson const llvm::Type *UnsignedLongTy = 333650c932dSMike Stump getTypes().ConvertType(getContext().UnsignedLongTy); 3342437cbfaSAnders Carlsson 3352437cbfaSAnders Carlsson // struct __block_descriptor { 3362437cbfaSAnders Carlsson // unsigned long reserved; 3372437cbfaSAnders Carlsson // unsigned long block_size; 3382437cbfaSAnders Carlsson // }; 339650c932dSMike Stump BlockDescriptorType = llvm::StructType::get(UnsignedLongTy, 3402437cbfaSAnders Carlsson UnsignedLongTy, 3412437cbfaSAnders Carlsson NULL); 3422437cbfaSAnders Carlsson 343650c932dSMike Stump getModule().addTypeName("struct.__block_descriptor", 344650c932dSMike Stump BlockDescriptorType); 3452437cbfaSAnders Carlsson 346650c932dSMike Stump return BlockDescriptorType; 3472437cbfaSAnders Carlsson } 3482437cbfaSAnders Carlsson 34995435674SMike Stump const llvm::Type *BlockModule::getGenericBlockLiteralType() { 350005c9a62SMike Stump if (GenericBlockLiteralType) 351005c9a62SMike Stump return GenericBlockLiteralType; 3522437cbfaSAnders Carlsson 3532437cbfaSAnders Carlsson const llvm::Type *BlockDescPtrTy = 354005c9a62SMike Stump llvm::PointerType::getUnqual(getBlockDescriptorType()); 3552437cbfaSAnders Carlsson 35692bbd6d4SMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 35792bbd6d4SMike Stump getTypes().ConvertType(getContext().IntTy)); 35892bbd6d4SMike Stump 3592437cbfaSAnders Carlsson // struct __block_literal_generic { 3605d2534adSMike Stump // void *__isa; 3615d2534adSMike Stump // int __flags; 3625d2534adSMike Stump // int __reserved; 3635d2534adSMike Stump // void (*__invoke)(void *); 3645d2534adSMike Stump // struct __block_descriptor *__descriptor; 3652437cbfaSAnders Carlsson // }; 366626aecc4SMike Stump GenericBlockLiteralType = llvm::StructType::get(PtrToInt8Ty, 36792bbd6d4SMike Stump IntTy, 36892bbd6d4SMike Stump IntTy, 369626aecc4SMike Stump PtrToInt8Ty, 3702437cbfaSAnders Carlsson BlockDescPtrTy, 3712437cbfaSAnders Carlsson NULL); 3722437cbfaSAnders Carlsson 373005c9a62SMike Stump getModule().addTypeName("struct.__block_literal_generic", 374005c9a62SMike Stump GenericBlockLiteralType); 3752437cbfaSAnders Carlsson 376005c9a62SMike Stump return GenericBlockLiteralType; 3772437cbfaSAnders Carlsson } 3782437cbfaSAnders Carlsson 37995435674SMike Stump const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() { 3805d2534adSMike Stump if (GenericExtendedBlockLiteralType) 3815d2534adSMike Stump return GenericExtendedBlockLiteralType; 3825d2534adSMike Stump 3835d2534adSMike Stump const llvm::Type *BlockDescPtrTy = 3845d2534adSMike Stump llvm::PointerType::getUnqual(getBlockDescriptorType()); 3855d2534adSMike Stump 3865d2534adSMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 3875d2534adSMike Stump getTypes().ConvertType(getContext().IntTy)); 3885d2534adSMike Stump 3895d2534adSMike Stump // struct __block_literal_generic { 3905d2534adSMike Stump // void *__isa; 3915d2534adSMike Stump // int __flags; 3925d2534adSMike Stump // int __reserved; 3935d2534adSMike Stump // void (*__invoke)(void *); 3945d2534adSMike Stump // struct __block_descriptor *__descriptor; 3955d2534adSMike Stump // void *__copy_func_helper_decl; 3965d2534adSMike Stump // void *__destroy_func_decl; 3975d2534adSMike Stump // }; 398626aecc4SMike Stump GenericExtendedBlockLiteralType = llvm::StructType::get(PtrToInt8Ty, 3995d2534adSMike Stump IntTy, 4005d2534adSMike Stump IntTy, 401626aecc4SMike Stump PtrToInt8Ty, 4025d2534adSMike Stump BlockDescPtrTy, 403626aecc4SMike Stump PtrToInt8Ty, 404626aecc4SMike Stump PtrToInt8Ty, 4055d2534adSMike Stump NULL); 4065d2534adSMike Stump 4075d2534adSMike Stump getModule().addTypeName("struct.__block_literal_extended_generic", 4085d2534adSMike Stump GenericExtendedBlockLiteralType); 4095d2534adSMike Stump 4105d2534adSMike Stump return GenericExtendedBlockLiteralType; 4115d2534adSMike Stump } 4125d2534adSMike Stump 4132437cbfaSAnders Carlsson /// getBlockFunctionType - Given a BlockPointerType, will return the 4142437cbfaSAnders Carlsson /// function type for the block, including the first block literal argument. 4152437cbfaSAnders Carlsson static QualType getBlockFunctionType(ASTContext &Ctx, 4166a60fa24SAnders Carlsson const BlockPointerType *BPT) { 417deaad8ccSDouglas Gregor const FunctionProtoType *FTy = cast<FunctionProtoType>(BPT->getPointeeType()); 4182437cbfaSAnders Carlsson 4192437cbfaSAnders Carlsson llvm::SmallVector<QualType, 8> Types; 4202437cbfaSAnders Carlsson Types.push_back(Ctx.getPointerType(Ctx.VoidTy)); 4212437cbfaSAnders Carlsson 422deaad8ccSDouglas Gregor for (FunctionProtoType::arg_type_iterator i = FTy->arg_type_begin(), 4232437cbfaSAnders Carlsson e = FTy->arg_type_end(); i != e; ++i) 4242437cbfaSAnders Carlsson Types.push_back(*i); 4252437cbfaSAnders Carlsson 4262437cbfaSAnders Carlsson return Ctx.getFunctionType(FTy->getResultType(), 4272437cbfaSAnders Carlsson &Types[0], Types.size(), 4282437cbfaSAnders Carlsson FTy->isVariadic(), 0); 4292437cbfaSAnders Carlsson } 4302437cbfaSAnders Carlsson 4316a60fa24SAnders Carlsson RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) { 4322437cbfaSAnders Carlsson const BlockPointerType *BPT = 4332437cbfaSAnders Carlsson E->getCallee()->getType()->getAsBlockPointerType(); 4342437cbfaSAnders Carlsson 4352437cbfaSAnders Carlsson llvm::Value *Callee = EmitScalarExpr(E->getCallee()); 4362437cbfaSAnders Carlsson 4372437cbfaSAnders Carlsson // Get a pointer to the generic block literal. 4382437cbfaSAnders Carlsson const llvm::Type *BlockLiteralTy = 439005c9a62SMike Stump llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType()); 4402437cbfaSAnders Carlsson 4412437cbfaSAnders Carlsson // Bitcast the callee to a block literal. 4422437cbfaSAnders Carlsson llvm::Value *BlockLiteral = 4432437cbfaSAnders Carlsson Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal"); 4442437cbfaSAnders Carlsson 4452437cbfaSAnders Carlsson // Get the function pointer from the literal. 4462437cbfaSAnders Carlsson llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp"); 447624497c2SMike Stump llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp"); 4482437cbfaSAnders Carlsson 4492437cbfaSAnders Carlsson // Cast the function pointer to the right type. 4502437cbfaSAnders Carlsson const llvm::Type *BlockFTy = 4512437cbfaSAnders Carlsson ConvertType(getBlockFunctionType(getContext(), BPT)); 4522437cbfaSAnders Carlsson const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy); 4532437cbfaSAnders Carlsson Func = Builder.CreateBitCast(Func, BlockFTyPtr); 4542437cbfaSAnders Carlsson 4552437cbfaSAnders Carlsson BlockLiteral = 4562437cbfaSAnders Carlsson Builder.CreateBitCast(BlockLiteral, 4572437cbfaSAnders Carlsson llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4582437cbfaSAnders Carlsson "tmp"); 4592437cbfaSAnders Carlsson 4602437cbfaSAnders Carlsson // Add the block literal. 4612437cbfaSAnders Carlsson QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy); 4622437cbfaSAnders Carlsson CallArgList Args; 4632437cbfaSAnders Carlsson Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy)); 4642437cbfaSAnders Carlsson 4652437cbfaSAnders Carlsson // And the rest of the arguments. 4662437cbfaSAnders Carlsson for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end(); 4672437cbfaSAnders Carlsson i != e; ++i) 4682437cbfaSAnders Carlsson Args.push_back(std::make_pair(EmitAnyExprToTemp(*i), 4692437cbfaSAnders Carlsson i->getType())); 4702437cbfaSAnders Carlsson 4712437cbfaSAnders Carlsson // And call the block. 4722437cbfaSAnders Carlsson return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args), 4732437cbfaSAnders Carlsson Func, Args); 4742437cbfaSAnders Carlsson } 4756a60fa24SAnders Carlsson 47697d01d50SMike Stump llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) { 47797d01d50SMike Stump uint64_t &offset = BlockDecls[E->getDecl()]; 47897d01d50SMike Stump 47997d01d50SMike Stump const llvm::Type *Ty; 48097d01d50SMike Stump Ty = CGM.getTypes().ConvertType(E->getDecl()->getType()); 48197d01d50SMike Stump 48297d01d50SMike Stump // FIXME: add support for copy/dispose helpers. 4830874160bSMike Stump if (!Enable__block && E->isByRef()) 48497d01d50SMike Stump ErrorUnsupported(E, "__block variable in block literal"); 4850c743277SMike Stump else if (!Enable__block && E->getType()->isBlockPointerType()) 48697d01d50SMike Stump ErrorUnsupported(E, "block pointer in block literal"); 487*90d8daf5SMike Stump else if (!Enable__block && E->getDecl()->getAttr<ObjCNSObjectAttr>() || 48897d01d50SMike Stump getContext().isObjCNSObjectType(E->getType())) 48997d01d50SMike Stump ErrorUnsupported(E, "__attribute__((NSObject)) variable in block " 49097d01d50SMike Stump "literal"); 4910c743277SMike Stump else if (!Enable__block && getContext().isObjCObjectPointerType(E->getType())) 49297d01d50SMike Stump ErrorUnsupported(E, "Objective-C variable in block literal"); 49397d01d50SMike Stump 49497d01d50SMike Stump // See if we have already allocated an offset for this variable. 49597d01d50SMike Stump if (offset == 0) { 4964446dcf0SMike Stump // Don't run the expensive check, unless we have to. 4974446dcf0SMike Stump if (!BlockHasCopyDispose && BlockRequiresCopying(E->getType())) 4984446dcf0SMike Stump BlockHasCopyDispose = true; 49997d01d50SMike Stump // if not, allocate one now. 50097d01d50SMike Stump offset = getBlockOffset(E); 50197d01d50SMike Stump } 50297d01d50SMike Stump 50397d01d50SMike Stump llvm::Value *BlockLiteral = LoadBlockStruct(); 50497d01d50SMike Stump llvm::Value *V = Builder.CreateGEP(BlockLiteral, 50597d01d50SMike Stump llvm::ConstantInt::get(llvm::Type::Int64Ty, 50697d01d50SMike Stump offset), 5072d33d630SMike Stump "block.literal"); 50897d01d50SMike Stump if (E->isByRef()) { 50997d01d50SMike Stump bool needsCopyDispose = BlockRequiresCopying(E->getType()); 51097d01d50SMike Stump uint64_t Align = getContext().getDeclAlignInBytes(E->getDecl()); 51197d01d50SMike Stump const llvm::Type *PtrStructTy 51297d01d50SMike Stump = llvm::PointerType::get(BuildByRefType(E->getType(), Align), 0); 51397d01d50SMike Stump Ty = PtrStructTy; 51497d01d50SMike Stump Ty = llvm::PointerType::get(Ty, 0); 51597d01d50SMike Stump V = Builder.CreateBitCast(V, Ty); 51697d01d50SMike Stump V = Builder.CreateLoad(V, false); 51797d01d50SMike Stump V = Builder.CreateStructGEP(V, 1, "forwarding"); 51897d01d50SMike Stump V = Builder.CreateLoad(V, false); 51997d01d50SMike Stump V = Builder.CreateBitCast(V, PtrStructTy); 52097d01d50SMike Stump V = Builder.CreateStructGEP(V, needsCopyDispose*2 + 4, "x"); 52197d01d50SMike Stump } else { 52297d01d50SMike Stump Ty = llvm::PointerType::get(Ty, 0); 52397d01d50SMike Stump V = Builder.CreateBitCast(V, Ty); 52497d01d50SMike Stump } 52597d01d50SMike Stump return V; 52697d01d50SMike Stump } 52797d01d50SMike Stump 5282d5a2878SMike Stump llvm::Constant * 5296c39666aSMike Stump BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) { 5306a60fa24SAnders Carlsson // Generate the block descriptor. 5316a60fa24SAnders Carlsson const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy); 53292bbd6d4SMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 53392bbd6d4SMike Stump getTypes().ConvertType(getContext().IntTy)); 5346a60fa24SAnders Carlsson 5356a60fa24SAnders Carlsson llvm::Constant *DescriptorFields[2]; 5366a60fa24SAnders Carlsson 5376a60fa24SAnders Carlsson // Reserved 5386a60fa24SAnders Carlsson DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy); 5396a60fa24SAnders Carlsson 5406a60fa24SAnders Carlsson // Block literal size. For global blocks we just use the size of the generic 5416a60fa24SAnders Carlsson // block literal struct. 5426a60fa24SAnders Carlsson uint64_t BlockLiteralSize = 543005c9a62SMike Stump TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8; 5446a60fa24SAnders Carlsson DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize); 5456a60fa24SAnders Carlsson 5466a60fa24SAnders Carlsson llvm::Constant *DescriptorStruct = 5476a60fa24SAnders Carlsson llvm::ConstantStruct::get(&DescriptorFields[0], 2); 5486a60fa24SAnders Carlsson 5496a60fa24SAnders Carlsson llvm::GlobalVariable *Descriptor = 5506a60fa24SAnders Carlsson new llvm::GlobalVariable(DescriptorStruct->getType(), true, 5516a60fa24SAnders Carlsson llvm::GlobalVariable::InternalLinkage, 5526a60fa24SAnders Carlsson DescriptorStruct, "__block_descriptor_global", 5536a60fa24SAnders Carlsson &getModule()); 5546a60fa24SAnders Carlsson 5556a60fa24SAnders Carlsson // Generate the constants for the block literal. 5566a60fa24SAnders Carlsson llvm::Constant *LiteralFields[5]; 5576a60fa24SAnders Carlsson 5582d5a2878SMike Stump CodeGenFunction::BlockInfo Info(0, n); 559b750d928SMike Stump uint64_t subBlockSize, subBlockAlign; 5601db7d04bSMike Stump llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; 5614446dcf0SMike Stump bool subBlockHasCopyDispose; 562cb2fbcb0SMike Stump llvm::Function *Fn 5636c39666aSMike Stump = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, subBlockSize, 564b750d928SMike Stump subBlockAlign, 5654446dcf0SMike Stump subBlockDeclRefDecls, 5664446dcf0SMike Stump subBlockHasCopyDispose); 567cb2fbcb0SMike Stump assert(subBlockSize == BlockLiteralSize 568cb2fbcb0SMike Stump && "no imports allowed for global block"); 5694446dcf0SMike Stump assert(!subBlockHasCopyDispose && "no imports allowed for global block"); 5706a60fa24SAnders Carlsson 5716a60fa24SAnders Carlsson // isa 572971f9b66SMike Stump LiteralFields[0] = getNSConcreteGlobalBlock(); 5736a60fa24SAnders Carlsson 5746a60fa24SAnders Carlsson // Flags 5755878c797SAnders Carlsson LiteralFields[1] = 5765878c797SAnders Carlsson llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR); 5776a60fa24SAnders Carlsson 5786a60fa24SAnders Carlsson // Reserved 57992bbd6d4SMike Stump LiteralFields[2] = llvm::Constant::getNullValue(IntTy); 5806a60fa24SAnders Carlsson 5816a60fa24SAnders Carlsson // Function 5826a60fa24SAnders Carlsson LiteralFields[3] = Fn; 5836a60fa24SAnders Carlsson 5846a60fa24SAnders Carlsson // Descriptor 5856a60fa24SAnders Carlsson LiteralFields[4] = Descriptor; 5866a60fa24SAnders Carlsson 5876a60fa24SAnders Carlsson llvm::Constant *BlockLiteralStruct = 5886a60fa24SAnders Carlsson llvm::ConstantStruct::get(&LiteralFields[0], 5); 5896a60fa24SAnders Carlsson 5906a60fa24SAnders Carlsson llvm::GlobalVariable *BlockLiteral = 5916a60fa24SAnders Carlsson new llvm::GlobalVariable(BlockLiteralStruct->getType(), true, 5926a60fa24SAnders Carlsson llvm::GlobalVariable::InternalLinkage, 5936a60fa24SAnders Carlsson BlockLiteralStruct, "__block_literal_global", 5946a60fa24SAnders Carlsson &getModule()); 5956a60fa24SAnders Carlsson 5966a60fa24SAnders Carlsson return BlockLiteral; 5976a60fa24SAnders Carlsson } 5986a60fa24SAnders Carlsson 599cb2fbcb0SMike Stump llvm::Value *CodeGenFunction::LoadBlockStruct() { 600cb2fbcb0SMike Stump return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self"); 601cb2fbcb0SMike Stump } 602cb2fbcb0SMike Stump 6034446dcf0SMike Stump llvm::Function * 6044446dcf0SMike Stump CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr, 605cb2fbcb0SMike Stump const BlockInfo& Info, 606b750d928SMike Stump uint64_t &Size, 607b750d928SMike Stump uint64_t &Align, 6084446dcf0SMike Stump llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls, 6094446dcf0SMike Stump bool &subBlockHasCopyDispose) { 610deaad8ccSDouglas Gregor const FunctionProtoType *FTy = 6113385fe12SChris Lattner cast<FunctionProtoType>(BExpr->getFunctionType()); 6126a60fa24SAnders Carlsson 6136a60fa24SAnders Carlsson FunctionArgList Args; 6146a60fa24SAnders Carlsson 6153385fe12SChris Lattner const BlockDecl *BD = BExpr->getBlockDecl(); 6166a60fa24SAnders Carlsson 6176a60fa24SAnders Carlsson // FIXME: This leaks 6186a60fa24SAnders Carlsson ImplicitParamDecl *SelfDecl = 6196a60fa24SAnders Carlsson ImplicitParamDecl::Create(getContext(), 0, 6206a60fa24SAnders Carlsson SourceLocation(), 0, 6216a60fa24SAnders Carlsson getContext().getPointerType(getContext().VoidTy)); 6226a60fa24SAnders Carlsson 6236a60fa24SAnders Carlsson Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType())); 624cb2fbcb0SMike Stump BlockStructDecl = SelfDecl; 6256a60fa24SAnders Carlsson 6266a60fa24SAnders Carlsson for (BlockDecl::param_iterator i = BD->param_begin(), 6276a60fa24SAnders Carlsson e = BD->param_end(); i != e; ++i) 628eecd39f2SMike Stump Args.push_back(std::make_pair(*i, (*i)->getType())); 6296a60fa24SAnders Carlsson 6306a60fa24SAnders Carlsson const CGFunctionInfo &FI = 6316a60fa24SAnders Carlsson CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args); 6326a60fa24SAnders Carlsson 6332d5a2878SMike Stump std::string Name = std::string("__") + Info.Name + "_block_invoke_"; 6346a60fa24SAnders Carlsson CodeGenTypes &Types = CGM.getTypes(); 6356a60fa24SAnders Carlsson const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic()); 6366a60fa24SAnders Carlsson 6376a60fa24SAnders Carlsson llvm::Function *Fn = 6386a60fa24SAnders Carlsson llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 6396a60fa24SAnders Carlsson Name, 6406a60fa24SAnders Carlsson &CGM.getModule()); 6416a60fa24SAnders Carlsson 6426a60fa24SAnders Carlsson StartFunction(BD, FTy->getResultType(), Fn, Args, 6433385fe12SChris Lattner BExpr->getBody()->getLocEnd()); 6443385fe12SChris Lattner EmitStmt(BExpr->getBody()); 6453385fe12SChris Lattner FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc()); 6466a60fa24SAnders Carlsson 647b750d928SMike Stump // The runtime needs a minimum alignment of a void *. 648b750d928SMike Stump uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 649b750d928SMike Stump BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign); 650b750d928SMike Stump 651cb2fbcb0SMike Stump Size = BlockOffset; 652b750d928SMike Stump Align = BlockAlign; 653b750d928SMike Stump subBlockDeclRefDecls = BlockDeclRefDecls; 6544446dcf0SMike Stump subBlockHasCopyDispose |= BlockHasCopyDispose; 6556a60fa24SAnders Carlsson return Fn; 6566a60fa24SAnders Carlsson } 6571db7d04bSMike Stump 658aeb0ffd5SMike Stump uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) { 6591db7d04bSMike Stump const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl()); 6601db7d04bSMike Stump 6611db7d04bSMike Stump uint64_t Size = getContext().getTypeSize(D->getType()) / 8; 6621db7d04bSMike Stump uint64_t Align = getContext().getDeclAlignInBytes(D); 6631db7d04bSMike Stump 6641db7d04bSMike Stump if (BDRE->isByRef()) { 6651db7d04bSMike Stump Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8; 6661db7d04bSMike Stump Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 6671db7d04bSMike Stump } 6681db7d04bSMike Stump 6691db7d04bSMike Stump assert ((Align > 0) && "alignment must be 1 byte or more"); 6701db7d04bSMike Stump 6711db7d04bSMike Stump uint64_t OldOffset = BlockOffset; 6721db7d04bSMike Stump 6731db7d04bSMike Stump // Ensure proper alignment, even if it means we have to have a gap 6741db7d04bSMike Stump BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align); 6751db7d04bSMike Stump BlockAlign = std::max(Align, BlockAlign); 6761db7d04bSMike Stump 6771db7d04bSMike Stump uint64_t Pad = BlockOffset - OldOffset; 6781db7d04bSMike Stump if (Pad) { 6791db7d04bSMike Stump llvm::ArrayType::get(llvm::Type::Int8Ty, Pad); 6801db7d04bSMike Stump QualType PadTy = getContext().getConstantArrayType(getContext().CharTy, 6811db7d04bSMike Stump llvm::APInt(32, Pad), 6821db7d04bSMike Stump ArrayType::Normal, 0); 6831db7d04bSMike Stump ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(), 6841db7d04bSMike Stump 0, QualType(PadTy), VarDecl::None, 6851db7d04bSMike Stump SourceLocation()); 6861db7d04bSMike Stump Expr *E; 6871db7d04bSMike Stump E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(), 6881db7d04bSMike Stump SourceLocation(), false, false); 6891db7d04bSMike Stump BlockDeclRefDecls.push_back(E); 6901db7d04bSMike Stump } 6911db7d04bSMike Stump BlockDeclRefDecls.push_back(BDRE); 6921db7d04bSMike Stump 6931db7d04bSMike Stump BlockOffset += Size; 6941db7d04bSMike Stump return BlockOffset-Size; 6951db7d04bSMike Stump } 69697d01d50SMike Stump 697aeb0ffd5SMike Stump llvm::Constant *BlockFunction:: 698aeb0ffd5SMike Stump GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T, 699aeb0ffd5SMike Stump std::vector<HelperInfo> &NoteForHelper) { 7000c743277SMike Stump QualType R = getContext().VoidTy; 7010c743277SMike Stump 7020c743277SMike Stump FunctionArgList Args; 7030c743277SMike Stump // FIXME: This leaks 704aeb0ffd5SMike Stump ImplicitParamDecl *Dst = 705aeb0ffd5SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 706aeb0ffd5SMike Stump getContext().getPointerType(getContext().VoidTy)); 707aeb0ffd5SMike Stump Args.push_back(std::make_pair(Dst, Dst->getType())); 7080c743277SMike Stump ImplicitParamDecl *Src = 7090c743277SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 7100c743277SMike Stump getContext().getPointerType(getContext().VoidTy)); 7110c743277SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 7120c743277SMike Stump 7130c743277SMike Stump const CGFunctionInfo &FI = 7140c743277SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 7150c743277SMike Stump 7160c743277SMike Stump std::string Name = std::string("__copy_helper_block_"); 7170c743277SMike Stump CodeGenTypes &Types = CGM.getTypes(); 7180c743277SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 7190c743277SMike Stump 7200c743277SMike Stump llvm::Function *Fn = 7210c743277SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 7220c743277SMike Stump Name, 7230c743277SMike Stump &CGM.getModule()); 7240c743277SMike Stump 7250c743277SMike Stump IdentifierInfo *II 7260c743277SMike Stump = &CGM.getContext().Idents.get("__copy_helper_block_"); 7270c743277SMike Stump 7280c743277SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 7290c743277SMike Stump getContext().getTranslationUnitDecl(), 7300c743277SMike Stump SourceLocation(), II, R, 7310c743277SMike Stump FunctionDecl::Static, false, 7320c743277SMike Stump true); 7330c743277SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 734aeb0ffd5SMike Stump 735aeb0ffd5SMike Stump llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); 736aeb0ffd5SMike Stump llvm::Type *PtrPtrT; 737aeb0ffd5SMike Stump PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 738aeb0ffd5SMike Stump SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); 739aeb0ffd5SMike Stump SrcObj = Builder.CreateLoad(SrcObj); 740aeb0ffd5SMike Stump 741aeb0ffd5SMike Stump llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst); 742aeb0ffd5SMike Stump DstObj = Builder.CreateBitCast(DstObj, llvm::PointerType::get(T, 0)); 743aeb0ffd5SMike Stump 744aeb0ffd5SMike Stump for (unsigned i=0; i < NoteForHelper.size(); ++i) { 745aeb0ffd5SMike Stump int flag = NoteForHelper[i].flag; 746aeb0ffd5SMike Stump int index = NoteForHelper[i].index; 747aeb0ffd5SMike Stump 748aeb0ffd5SMike Stump if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) 749aeb0ffd5SMike Stump || NoteForHelper[i].RequiresCopying) { 750aeb0ffd5SMike Stump llvm::Value *Srcv = SrcObj; 751aeb0ffd5SMike Stump Srcv = Builder.CreateStructGEP(Srcv, index); 752aeb0ffd5SMike Stump Srcv = Builder.CreateBitCast(Srcv, 753aeb0ffd5SMike Stump llvm::PointerType::get(PtrToInt8Ty, 0)); 754aeb0ffd5SMike Stump Srcv = Builder.CreateLoad(Srcv); 755aeb0ffd5SMike Stump 756aeb0ffd5SMike Stump llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index); 757aeb0ffd5SMike Stump Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty); 758aeb0ffd5SMike Stump 759aeb0ffd5SMike Stump llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag); 760aeb0ffd5SMike Stump llvm::Value *F = getBlockObjectAssign(); 761aeb0ffd5SMike Stump Builder.CreateCall3(F, Dstv, Srcv, N); 762aeb0ffd5SMike Stump } 763aeb0ffd5SMike Stump } 764aeb0ffd5SMike Stump 7650c743277SMike Stump CGF.FinishFunction(); 7660c743277SMike Stump 7670c743277SMike Stump return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 76897d01d50SMike Stump } 76997d01d50SMike Stump 770d6ef62f0SMike Stump llvm::Constant *BlockFunction:: 771aeb0ffd5SMike Stump GenerateDestroyHelperFunction(bool BlockHasCopyDispose, 772aeb0ffd5SMike Stump const llvm::StructType* T, 773aeb0ffd5SMike Stump std::vector<HelperInfo> &NoteForHelper) { 7740c743277SMike Stump QualType R = getContext().VoidTy; 7750c743277SMike Stump 7760c743277SMike Stump FunctionArgList Args; 7770c743277SMike Stump // FIXME: This leaks 7780c743277SMike Stump ImplicitParamDecl *Src = 7790c743277SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 7800c743277SMike Stump getContext().getPointerType(getContext().VoidTy)); 7810c743277SMike Stump 7820c743277SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 7830c743277SMike Stump 7840c743277SMike Stump const CGFunctionInfo &FI = 7850c743277SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 7860c743277SMike Stump 7870c743277SMike Stump std::string Name = std::string("__destroy_helper_block_"); 7880c743277SMike Stump CodeGenTypes &Types = CGM.getTypes(); 7890c743277SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 7900c743277SMike Stump 7910c743277SMike Stump llvm::Function *Fn = 7920c743277SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 7930c743277SMike Stump Name, 7940c743277SMike Stump &CGM.getModule()); 7950c743277SMike Stump 7960c743277SMike Stump IdentifierInfo *II 7970c743277SMike Stump = &CGM.getContext().Idents.get("__destroy_helper_block_"); 7980c743277SMike Stump 7990c743277SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 8000c743277SMike Stump getContext().getTranslationUnitDecl(), 8010c743277SMike Stump SourceLocation(), II, R, 8020c743277SMike Stump FunctionDecl::Static, false, 8030c743277SMike Stump true); 8040c743277SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 8056f7d9f87SMike Stump 8066f7d9f87SMike Stump llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); 8076f7d9f87SMike Stump llvm::Type *PtrPtrT; 8086f7d9f87SMike Stump PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 8096f7d9f87SMike Stump SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); 8106f7d9f87SMike Stump SrcObj = Builder.CreateLoad(SrcObj); 8116f7d9f87SMike Stump 8126f7d9f87SMike Stump for (unsigned i=0; i < NoteForHelper.size(); ++i) { 8136f7d9f87SMike Stump int flag = NoteForHelper[i].flag; 8146f7d9f87SMike Stump int index = NoteForHelper[i].index; 8156f7d9f87SMike Stump 8166f7d9f87SMike Stump if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) 8176f7d9f87SMike Stump || NoteForHelper[i].RequiresCopying) { 8186f7d9f87SMike Stump llvm::Value *Srcv = SrcObj; 8196f7d9f87SMike Stump Srcv = Builder.CreateStructGEP(Srcv, index); 8206f7d9f87SMike Stump Srcv = Builder.CreateBitCast(Srcv, 8216f7d9f87SMike Stump llvm::PointerType::get(PtrToInt8Ty, 0)); 8226f7d9f87SMike Stump Srcv = Builder.CreateLoad(Srcv); 8236f7d9f87SMike Stump 8246f7d9f87SMike Stump BuildBlockRelease(Srcv, flag); 8256f7d9f87SMike Stump } 8266f7d9f87SMike Stump } 8276f7d9f87SMike Stump 8280c743277SMike Stump CGF.FinishFunction(); 8290c743277SMike Stump 8300c743277SMike Stump return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 8310c743277SMike Stump } 8320c743277SMike Stump 833aeb0ffd5SMike Stump llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T, 834aeb0ffd5SMike Stump std::vector<HelperInfo> &NoteForHelper) { 835aeb0ffd5SMike Stump return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose, 836aeb0ffd5SMike Stump T, NoteForHelper); 8370c743277SMike Stump } 8380c743277SMike Stump 839aeb0ffd5SMike Stump llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T, 840aeb0ffd5SMike Stump std::vector<HelperInfo> &NoteForHelper) { 841aeb0ffd5SMike Stump return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose, 842aeb0ffd5SMike Stump T, NoteForHelper); 84397d01d50SMike Stump } 844626aecc4SMike Stump 845f89230d9SMike Stump llvm::Constant *BlockFunction:: 846f89230d9SMike Stump GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) { 847ee2a5ee5SMike Stump QualType R = getContext().VoidTy; 848ee2a5ee5SMike Stump 849ee2a5ee5SMike Stump FunctionArgList Args; 850ee2a5ee5SMike Stump // FIXME: This leaks 851f89230d9SMike Stump ImplicitParamDecl *Dst = 852f89230d9SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 853f89230d9SMike Stump getContext().getPointerType(getContext().VoidTy)); 854f89230d9SMike Stump Args.push_back(std::make_pair(Dst, Dst->getType())); 855f89230d9SMike Stump 856f89230d9SMike Stump // FIXME: This leaks 857ee2a5ee5SMike Stump ImplicitParamDecl *Src = 858ee2a5ee5SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 859ee2a5ee5SMike Stump getContext().getPointerType(getContext().VoidTy)); 860ee2a5ee5SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 861ee2a5ee5SMike Stump 862ee2a5ee5SMike Stump const CGFunctionInfo &FI = 863ee2a5ee5SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 864ee2a5ee5SMike Stump 865ee2a5ee5SMike Stump std::string Name = std::string("__Block_byref_id_object_copy_"); 866ee2a5ee5SMike Stump CodeGenTypes &Types = CGM.getTypes(); 867ee2a5ee5SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 868ee2a5ee5SMike Stump 869ee2a5ee5SMike Stump llvm::Function *Fn = 870ee2a5ee5SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 871ee2a5ee5SMike Stump Name, 872ee2a5ee5SMike Stump &CGM.getModule()); 873ee2a5ee5SMike Stump 874ee2a5ee5SMike Stump IdentifierInfo *II 875ee2a5ee5SMike Stump = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_"); 876ee2a5ee5SMike Stump 877ee2a5ee5SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 878ee2a5ee5SMike Stump getContext().getTranslationUnitDecl(), 879ee2a5ee5SMike Stump SourceLocation(), II, R, 880ee2a5ee5SMike Stump FunctionDecl::Static, false, 881ee2a5ee5SMike Stump true); 882ee2a5ee5SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 883f89230d9SMike Stump 884f89230d9SMike Stump // dst->x 885f89230d9SMike Stump llvm::Value *V = CGF.GetAddrOfLocalVar(Dst); 886f89230d9SMike Stump V = Builder.CreateBitCast(V, T); 887f89230d9SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 888f89230d9SMike Stump llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty); 889f89230d9SMike Stump 890f89230d9SMike Stump // src->x 891f89230d9SMike Stump V = CGF.GetAddrOfLocalVar(Src); 892f89230d9SMike Stump V = Builder.CreateLoad(V); 893f89230d9SMike Stump V = Builder.CreateBitCast(V, T); 894f89230d9SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 895f89230d9SMike Stump V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); 896f89230d9SMike Stump llvm::Value *SrcObj = Builder.CreateLoad(V); 897f89230d9SMike Stump 898f89230d9SMike Stump flag |= BLOCK_BYREF_CALLER; 899f89230d9SMike Stump 900f89230d9SMike Stump llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag); 901f89230d9SMike Stump llvm::Value *F = getBlockObjectAssign(); 902f89230d9SMike Stump Builder.CreateCall3(F, DstObj, SrcObj, N); 903f89230d9SMike Stump 904ee2a5ee5SMike Stump CGF.FinishFunction(); 905ee2a5ee5SMike Stump 906ee2a5ee5SMike Stump return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 907ee2a5ee5SMike Stump } 908ee2a5ee5SMike Stump 909fbe25dd1SMike Stump llvm::Constant * 910fbe25dd1SMike Stump BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T, 911fbe25dd1SMike Stump int flag) { 912ee2a5ee5SMike Stump QualType R = getContext().VoidTy; 913ee2a5ee5SMike Stump 914ee2a5ee5SMike Stump FunctionArgList Args; 915ee2a5ee5SMike Stump // FIXME: This leaks 916ee2a5ee5SMike Stump ImplicitParamDecl *Src = 917ee2a5ee5SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 918ee2a5ee5SMike Stump getContext().getPointerType(getContext().VoidTy)); 919ee2a5ee5SMike Stump 920ee2a5ee5SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 921ee2a5ee5SMike Stump 922ee2a5ee5SMike Stump const CGFunctionInfo &FI = 923ee2a5ee5SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 924ee2a5ee5SMike Stump 925ee2a5ee5SMike Stump std::string Name = std::string("__Block_byref_id_object_dispose_"); 926ee2a5ee5SMike Stump CodeGenTypes &Types = CGM.getTypes(); 927ee2a5ee5SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 928ee2a5ee5SMike Stump 929ee2a5ee5SMike Stump llvm::Function *Fn = 930ee2a5ee5SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 931ee2a5ee5SMike Stump Name, 932ee2a5ee5SMike Stump &CGM.getModule()); 933ee2a5ee5SMike Stump 934ee2a5ee5SMike Stump IdentifierInfo *II 935ee2a5ee5SMike Stump = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_"); 936ee2a5ee5SMike Stump 937ee2a5ee5SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 938ee2a5ee5SMike Stump getContext().getTranslationUnitDecl(), 939ee2a5ee5SMike Stump SourceLocation(), II, R, 940ee2a5ee5SMike Stump FunctionDecl::Static, false, 941ee2a5ee5SMike Stump true); 942ee2a5ee5SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 943fbe25dd1SMike Stump 944fbe25dd1SMike Stump llvm::Value *V = CGF.GetAddrOfLocalVar(Src); 945fbe25dd1SMike Stump V = Builder.CreateBitCast(V, T); 946fbe25dd1SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 947fbe25dd1SMike Stump V = Builder.CreateBitCast(V, PtrToInt8Ty); 948fbe25dd1SMike Stump 949fbe25dd1SMike Stump flag |= BLOCK_BYREF_CALLER; 950fbe25dd1SMike Stump BuildBlockRelease(V, flag); 951ee2a5ee5SMike Stump CGF.FinishFunction(); 952ee2a5ee5SMike Stump 953ee2a5ee5SMike Stump return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 954ee2a5ee5SMike Stump } 955ee2a5ee5SMike Stump 956f89230d9SMike Stump llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T, 957f89230d9SMike Stump int flag) { 958f89230d9SMike Stump return CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag); 959ee2a5ee5SMike Stump } 960ee2a5ee5SMike Stump 961fbe25dd1SMike Stump llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T, 962fbe25dd1SMike Stump int flag) { 963fbe25dd1SMike Stump return CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag); 964ee2a5ee5SMike Stump } 965ee2a5ee5SMike Stump 966626aecc4SMike Stump llvm::Value *BlockFunction::getBlockObjectDispose() { 967626aecc4SMike Stump if (CGM.BlockObjectDispose == 0) { 968626aecc4SMike Stump const llvm::FunctionType *FTy; 969626aecc4SMike Stump std::vector<const llvm::Type*> ArgTys; 970626aecc4SMike Stump const llvm::Type *ResultType = llvm::Type::VoidTy; 971626aecc4SMike Stump ArgTys.push_back(PtrToInt8Ty); 972626aecc4SMike Stump ArgTys.push_back(llvm::Type::Int32Ty); 973626aecc4SMike Stump FTy = llvm::FunctionType::get(ResultType, ArgTys, false); 974626aecc4SMike Stump CGM.BlockObjectDispose 975626aecc4SMike Stump = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose"); 976626aecc4SMike Stump } 977626aecc4SMike Stump return CGM.BlockObjectDispose; 978626aecc4SMike Stump } 979626aecc4SMike Stump 980f89230d9SMike Stump llvm::Value *BlockFunction::getBlockObjectAssign() { 981f89230d9SMike Stump if (CGM.BlockObjectAssign == 0) { 982f89230d9SMike Stump const llvm::FunctionType *FTy; 983f89230d9SMike Stump std::vector<const llvm::Type*> ArgTys; 984f89230d9SMike Stump const llvm::Type *ResultType = llvm::Type::VoidTy; 985f89230d9SMike Stump ArgTys.push_back(PtrToInt8Ty); 986f89230d9SMike Stump ArgTys.push_back(PtrToInt8Ty); 987f89230d9SMike Stump ArgTys.push_back(llvm::Type::Int32Ty); 988f89230d9SMike Stump FTy = llvm::FunctionType::get(ResultType, ArgTys, false); 989f89230d9SMike Stump CGM.BlockObjectAssign 990f89230d9SMike Stump = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign"); 991f89230d9SMike Stump } 992f89230d9SMike Stump return CGM.BlockObjectAssign; 993f89230d9SMike Stump } 994f89230d9SMike Stump 995fbe25dd1SMike Stump void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) { 996626aecc4SMike Stump llvm::Value *F = getBlockObjectDispose(); 997fbe25dd1SMike Stump llvm::Value *N; 998626aecc4SMike Stump V = Builder.CreateBitCast(V, PtrToInt8Ty); 999fbe25dd1SMike Stump N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag); 1000626aecc4SMike Stump Builder.CreateCall2(F, V, N); 1001626aecc4SMike Stump } 10024446dcf0SMike Stump 10034446dcf0SMike Stump ASTContext &BlockFunction::getContext() const { return CGM.getContext(); } 1004aeb0ffd5SMike Stump 1005aeb0ffd5SMike Stump BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf, 1006aeb0ffd5SMike Stump CGBuilderTy &B) 1007aeb0ffd5SMike Stump : CGM(cgm), CGF(cgf), Builder(B) { 1008aeb0ffd5SMike Stump PtrToInt8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 1009aeb0ffd5SMike Stump 1010aeb0ffd5SMike Stump BlockHasCopyDispose = false; 1011aeb0ffd5SMike Stump } 1012