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" 16692c6e37SMike Stump #include "clang/AST/DeclObjC.h" 172437cbfaSAnders Carlsson #include "llvm/Module.h" 186a60fa24SAnders Carlsson #include "llvm/Target/TargetData.h" 192437cbfaSAnders Carlsson 202437cbfaSAnders Carlsson #include <algorithm> 212437cbfaSAnders Carlsson 222437cbfaSAnders Carlsson using namespace clang; 232437cbfaSAnders Carlsson using namespace CodeGen; 242437cbfaSAnders Carlsson 250874160bSMike Stump // Temporary code to enable testing of __block variables 260874160bSMike Stump // #include "clang/Frontend/CompileOptions.h" 270874160bSMike Stump #include "llvm/Support/CommandLine.h" 280874160bSMike Stump 29d6ef62f0SMike Stump llvm::Constant *CodeGenFunction:: 30d2142cffSMike Stump BuildDescriptorBlockDecl(bool BlockHasCopyDispose, uint64_t Size, 31d2142cffSMike Stump const llvm::StructType* Ty, 32aeb0ffd5SMike Stump std::vector<HelperInfo> *NoteForHelper) { 33c2c38331SMike Stump const llvm::Type *UnsignedLongTy 34c2c38331SMike Stump = CGM.getTypes().ConvertType(getContext().UnsignedLongTy); 3585284bacSMike Stump llvm::Constant *C; 3685284bacSMike Stump std::vector<llvm::Constant*> Elts; 3785284bacSMike Stump 3885284bacSMike Stump // reserved 39c2c38331SMike Stump C = llvm::ConstantInt::get(UnsignedLongTy, 0); 4085284bacSMike Stump Elts.push_back(C); 4185284bacSMike Stump 4285284bacSMike Stump // Size 432ac40a9fSMike Stump // FIXME: What is the right way to say this doesn't fit? We should give 442ac40a9fSMike Stump // a user diagnostic in that case. Better fix would be to change the 452ac40a9fSMike Stump // API to size_t. 46cb2fbcb0SMike Stump C = llvm::ConstantInt::get(UnsignedLongTy, Size); 4785284bacSMike Stump Elts.push_back(C); 4885284bacSMike Stump 4985284bacSMike Stump if (BlockHasCopyDispose) { 5085284bacSMike Stump // copy_func_helper_decl 51aeb0ffd5SMike Stump Elts.push_back(BuildCopyHelper(Ty, *NoteForHelper)); 5285284bacSMike Stump 5385284bacSMike Stump // destroy_func_decl 54aeb0ffd5SMike Stump Elts.push_back(BuildDestroyHelper(Ty, *NoteForHelper)); 5585284bacSMike Stump } 5685284bacSMike Stump 5785284bacSMike Stump C = llvm::ConstantStruct::get(Elts); 5885284bacSMike Stump 5985284bacSMike Stump C = new llvm::GlobalVariable(C->getType(), true, 6085284bacSMike Stump llvm::GlobalValue::InternalLinkage, 61499ae7ecSMike Stump C, "__block_descriptor_tmp", &CGM.getModule()); 6285284bacSMike Stump return C; 6385284bacSMike Stump } 6485284bacSMike Stump 6595435674SMike Stump llvm::Constant *BlockModule::getNSConcreteGlobalBlock() { 66971f9b66SMike Stump if (NSConcreteGlobalBlock) 67971f9b66SMike Stump return NSConcreteGlobalBlock; 68971f9b66SMike Stump 6952d9c49dSMike Stump // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the 702d33d630SMike Stump // same thing as CreateRuntimeFunction if there's already a variable with the 712d33d630SMike Stump // same name. 72971f9b66SMike Stump NSConcreteGlobalBlock 73971f9b66SMike Stump = new llvm::GlobalVariable(PtrToInt8Ty, false, 74971f9b66SMike Stump llvm::GlobalValue::ExternalLinkage, 75971f9b66SMike Stump 0, "_NSConcreteGlobalBlock", 76971f9b66SMike Stump &getModule()); 77971f9b66SMike Stump 78971f9b66SMike Stump return NSConcreteGlobalBlock; 79971f9b66SMike Stump } 80971f9b66SMike Stump 8195435674SMike Stump llvm::Constant *BlockModule::getNSConcreteStackBlock() { 827ab278dbSMike Stump if (NSConcreteStackBlock) 837ab278dbSMike Stump return NSConcreteStackBlock; 847ab278dbSMike Stump 8552d9c49dSMike Stump // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the 862d33d630SMike Stump // same thing as CreateRuntimeFunction if there's already a variable with the 872d33d630SMike Stump // same name. 887ab278dbSMike Stump NSConcreteStackBlock 897ab278dbSMike Stump = new llvm::GlobalVariable(PtrToInt8Ty, false, 907ab278dbSMike Stump llvm::GlobalValue::ExternalLinkage, 917ab278dbSMike Stump 0, "_NSConcreteStackBlock", 927ab278dbSMike Stump &getModule()); 937ab278dbSMike Stump 947ab278dbSMike Stump return NSConcreteStackBlock; 957ab278dbSMike Stump } 967ab278dbSMike Stump 97ed5e69feSAnders Carlsson static void CollectBlockDeclRefInfo(const Stmt *S, 98ed5e69feSAnders Carlsson CodeGenFunction::BlockInfo &Info) { 99ed5e69feSAnders Carlsson for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); 100ed5e69feSAnders Carlsson I != E; ++I) 101dd942713SDaniel Dunbar if (*I) 102ed5e69feSAnders Carlsson CollectBlockDeclRefInfo(*I, Info); 103ed5e69feSAnders Carlsson 104ed5e69feSAnders Carlsson if (const BlockDeclRefExpr *DE = dyn_cast<BlockDeclRefExpr>(S)) { 105ed5e69feSAnders Carlsson // FIXME: Handle enums. 106ed5e69feSAnders Carlsson if (isa<FunctionDecl>(DE->getDecl())) 107ed5e69feSAnders Carlsson return; 108ed5e69feSAnders Carlsson 109ed5e69feSAnders Carlsson if (DE->isByRef()) 110ed5e69feSAnders Carlsson Info.ByRefDeclRefs.push_back(DE); 111ed5e69feSAnders Carlsson else 112ed5e69feSAnders Carlsson Info.ByCopyDeclRefs.push_back(DE); 113ed5e69feSAnders Carlsson } 114ed5e69feSAnders Carlsson } 115ed5e69feSAnders Carlsson 1162d33d630SMike Stump /// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be 1172d33d630SMike Stump /// declared as a global variable instead of on the stack. 118ed5e69feSAnders Carlsson static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info) 119ed5e69feSAnders Carlsson { 120ed5e69feSAnders Carlsson return Info.ByRefDeclRefs.empty() && Info.ByCopyDeclRefs.empty(); 121ed5e69feSAnders Carlsson } 122ed5e69feSAnders Carlsson 1232d33d630SMike Stump // FIXME: Push most into CGM, passing down a few bits, like current function 1242d33d630SMike Stump // name. 125b750d928SMike Stump llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) { 126ed5e69feSAnders Carlsson 127ed5e69feSAnders Carlsson std::string Name = CurFn->getName(); 128ed5e69feSAnders Carlsson CodeGenFunction::BlockInfo Info(0, Name.c_str()); 129ed5e69feSAnders Carlsson CollectBlockDeclRefInfo(BE->getBody(), Info); 130ed5e69feSAnders Carlsson 131ed5e69feSAnders Carlsson // Check if the block can be global. 1322d33d630SMike Stump // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like 1332d33d630SMike Stump // to just have one code path. We should move this function into CGM and pass 1342d33d630SMike Stump // CGF, then we can just check to see if CGF is 0. 13597d01d50SMike Stump if (0 && CanBlockBeGlobal(Info)) 136ed5e69feSAnders Carlsson return CGM.GetAddrOfGlobalBlock(BE, Name.c_str()); 13785284bacSMike Stump 1384446dcf0SMike Stump std::vector<llvm::Constant*> Elts(5); 13985284bacSMike Stump llvm::Constant *C; 140b750d928SMike Stump llvm::Value *V; 14185284bacSMike Stump 14285284bacSMike Stump { 14385284bacSMike Stump // C = BuildBlockStructInitlist(); 14485284bacSMike Stump unsigned int flags = BLOCK_HAS_DESCRIPTOR; 14585284bacSMike Stump 1464446dcf0SMike Stump // We run this first so that we set BlockHasCopyDispose from the entire 1474446dcf0SMike Stump // block literal. 1484446dcf0SMike Stump // __invoke 1494446dcf0SMike Stump uint64_t subBlockSize, subBlockAlign; 1504446dcf0SMike Stump llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; 151d2142cffSMike Stump bool subBlockHasCopyDispose = false; 1524446dcf0SMike Stump llvm::Function *Fn 153692c6e37SMike Stump = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl, LocalDeclMap, 1545469f29aSMike Stump subBlockSize, 1554446dcf0SMike Stump subBlockAlign, 1564446dcf0SMike Stump subBlockDeclRefDecls, 157d2142cffSMike Stump subBlockHasCopyDispose); 158d2142cffSMike Stump BlockHasCopyDispose |= subBlockHasCopyDispose; 1594446dcf0SMike Stump Elts[3] = Fn; 1604446dcf0SMike Stump 161d2142cffSMike Stump if (subBlockHasCopyDispose) 16285284bacSMike Stump flags |= BLOCK_HAS_COPY_DISPOSE; 16385284bacSMike Stump 164499ae7ecSMike Stump // __isa 1657ab278dbSMike Stump C = CGM.getNSConcreteStackBlock(); 16685284bacSMike Stump C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty); 1674446dcf0SMike Stump Elts[0] = C; 16885284bacSMike Stump 16985284bacSMike Stump // __flags 17085284bacSMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 17185284bacSMike Stump CGM.getTypes().ConvertType(CGM.getContext().IntTy)); 17285284bacSMike Stump C = llvm::ConstantInt::get(IntTy, flags); 1734446dcf0SMike Stump Elts[1] = C; 17485284bacSMike Stump 17585284bacSMike Stump // __reserved 17685284bacSMike Stump C = llvm::ConstantInt::get(IntTy, 0); 1774446dcf0SMike Stump Elts[2] = C; 17885284bacSMike Stump 179b750d928SMike Stump if (subBlockDeclRefDecls.size() == 0) { 180d6ef62f0SMike Stump // __descriptor 181d2142cffSMike Stump assert(subBlockHasCopyDispose == false); 182d2142cffSMike Stump Elts[4] = BuildDescriptorBlockDecl(subBlockHasCopyDispose, subBlockSize, 0, 0); 183d6ef62f0SMike Stump 1843a139f37SMike Stump // Optimize to being a global block. 1853a139f37SMike Stump Elts[0] = CGM.getNSConcreteGlobalBlock(); 1863a139f37SMike Stump Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL); 1873a139f37SMike Stump 18885284bacSMike Stump C = llvm::ConstantStruct::get(Elts); 18985284bacSMike Stump 19085284bacSMike Stump char Name[32]; 1919319db8aSMike Stump sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount()); 19285284bacSMike Stump C = new llvm::GlobalVariable(C->getType(), true, 19385284bacSMike Stump llvm::GlobalValue::InternalLinkage, 19485284bacSMike Stump C, Name, &CGM.getModule()); 1955d2534adSMike Stump QualType BPT = BE->getType(); 1965d2534adSMike Stump C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT)); 19785284bacSMike Stump return C; 19885284bacSMike Stump } 19985284bacSMike Stump 200b750d928SMike Stump std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size()); 201aeb0ffd5SMike Stump for (int i=0; i<4; ++i) 202b750d928SMike Stump Types[i] = Elts[i]->getType(); 203aeb0ffd5SMike Stump Types[4] = PtrToInt8Ty; 20485284bacSMike Stump 2051db7d04bSMike Stump for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) { 2061db7d04bSMike Stump const Expr *E = subBlockDeclRefDecls[i]; 2071db7d04bSMike Stump const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); 2081db7d04bSMike Stump QualType Ty = E->getType(); 20997d01d50SMike Stump if (BDRE && BDRE->isByRef()) { 21097d01d50SMike Stump uint64_t Align = getContext().getDeclAlignInBytes(BDRE->getDecl()); 21197d01d50SMike Stump Types[i+5] = llvm::PointerType::get(BuildByRefType(Ty, Align), 0); 21297d01d50SMike Stump } else 2131db7d04bSMike Stump Types[i+5] = ConvertType(Ty); 2141db7d04bSMike Stump } 215b750d928SMike Stump 216aeb0ffd5SMike Stump llvm::StructType *Ty = llvm::StructType::get(Types, true); 217b750d928SMike Stump 218b750d928SMike Stump llvm::AllocaInst *A = CreateTempAlloca(Ty); 219b750d928SMike Stump A->setAlignment(subBlockAlign); 220b750d928SMike Stump V = A; 221b750d928SMike Stump 222aeb0ffd5SMike Stump std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size()); 223aeb0ffd5SMike Stump int helpersize = 0; 224aeb0ffd5SMike Stump 225aeb0ffd5SMike Stump for (unsigned i=0; i<4; ++i) 226b750d928SMike Stump Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp")); 227b750d928SMike Stump 228b750d928SMike Stump for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) 229b750d928SMike Stump { 2301db7d04bSMike Stump // FIXME: Push const down. 2311db7d04bSMike Stump Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]); 2321db7d04bSMike Stump DeclRefExpr *DR; 2331db7d04bSMike Stump ValueDecl *VD; 234b750d928SMike Stump 2351db7d04bSMike Stump DR = dyn_cast<DeclRefExpr>(E); 2361db7d04bSMike Stump // Skip padding. 2371db7d04bSMike Stump if (DR) continue; 2381db7d04bSMike Stump 2391db7d04bSMike Stump BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); 2401db7d04bSMike Stump VD = BDRE->getDecl(); 2411db7d04bSMike Stump 24297d01d50SMike Stump llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp"); 243aeb0ffd5SMike Stump NoteForHelper[helpersize].index = i+5; 244aeb0ffd5SMike Stump NoteForHelper[helpersize].RequiresCopying = BlockRequiresCopying(VD->getType()); 245aeb0ffd5SMike Stump NoteForHelper[helpersize].flag 246aeb0ffd5SMike Stump = VD->getType()->isBlockPointerType() ? BLOCK_FIELD_IS_BLOCK : BLOCK_FIELD_IS_OBJECT; 247aeb0ffd5SMike Stump 2481db7d04bSMike Stump if (LocalDeclMap[VD]) { 24997d01d50SMike Stump if (BDRE->isByRef()) { 250aeb0ffd5SMike Stump NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF | 25190d8daf5SMike Stump // FIXME: Someone double check this. 25290d8daf5SMike Stump (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0); 25397d01d50SMike Stump const llvm::Type *Ty = Types[i+5]; 25497d01d50SMike Stump llvm::Value *Loc = LocalDeclMap[VD]; 25597d01d50SMike Stump Loc = Builder.CreateStructGEP(Loc, 1, "forwarding"); 25697d01d50SMike Stump Loc = Builder.CreateLoad(Loc, false); 25797d01d50SMike Stump Loc = Builder.CreateBitCast(Loc, Ty); 25897d01d50SMike Stump Builder.CreateStore(Loc, Addr); 259aeb0ffd5SMike Stump ++helpersize; 26097d01d50SMike Stump continue; 26197d01d50SMike Stump } else 2621db7d04bSMike Stump E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD), 2631db7d04bSMike Stump VD->getType(), SourceLocation(), 2641db7d04bSMike Stump false, false); 2651db7d04bSMike Stump } 26697d01d50SMike Stump if (BDRE->isByRef()) { 267efd7caa8SMike Stump NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF | 268efd7caa8SMike Stump // FIXME: Someone double check this. 269efd7caa8SMike Stump (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0); 2701db7d04bSMike Stump E = new (getContext()) 2711db7d04bSMike Stump UnaryOperator(E, UnaryOperator::AddrOf, 2721db7d04bSMike Stump getContext().getPointerType(E->getType()), 2731db7d04bSMike Stump SourceLocation()); 27497d01d50SMike Stump } 275aeb0ffd5SMike Stump ++helpersize; 2761db7d04bSMike Stump 2771db7d04bSMike Stump RValue r = EmitAnyExpr(E, Addr, false); 27897d01d50SMike Stump if (r.isScalar()) { 27997d01d50SMike Stump llvm::Value *Loc = r.getScalarVal(); 28097d01d50SMike Stump const llvm::Type *Ty = Types[i+5]; 28197d01d50SMike Stump if (BDRE->isByRef()) { 2822d33d630SMike Stump // E is now the address of the value field, instead, we want the 2832d33d630SMike Stump // address of the actual ByRef struct. We optimize this slightly 2842d33d630SMike Stump // compared to gcc by not grabbing the forwarding slot as this must 2852d33d630SMike Stump // be done during Block_copy for us, and we can postpone the work 2862d33d630SMike Stump // until then. 2872d33d630SMike Stump uint64_t offset = BlockDecls[BDRE->getDecl()]; 2882d33d630SMike Stump 2892d33d630SMike Stump llvm::Value *BlockLiteral = LoadBlockStruct(); 2902d33d630SMike Stump 2912d33d630SMike Stump Loc = Builder.CreateGEP(BlockLiteral, 2922d33d630SMike Stump llvm::ConstantInt::get(llvm::Type::Int64Ty, 2932d33d630SMike Stump offset), 2942d33d630SMike Stump "block.literal"); 2952d33d630SMike Stump Ty = llvm::PointerType::get(Ty, 0); 29697d01d50SMike Stump Loc = Builder.CreateBitCast(Loc, Ty); 2972d33d630SMike Stump Loc = Builder.CreateLoad(Loc, false); 2982d33d630SMike Stump // Loc = Builder.CreateBitCast(Loc, Ty); 29997d01d50SMike Stump } 30097d01d50SMike Stump Builder.CreateStore(Loc, Addr); 30197d01d50SMike Stump } else if (r.isComplex()) 302b750d928SMike Stump // FIXME: implement 303b750d928SMike Stump ErrorUnsupported(BE, "complex in block literal"); 304b750d928SMike Stump else if (r.isAggregate()) 305b750d928SMike Stump ; // Already created into the destination 306b750d928SMike Stump else 307b750d928SMike Stump assert (0 && "bad block variable"); 308b750d928SMike Stump // FIXME: Ensure that the offset created by the backend for 309b750d928SMike Stump // the struct matches the previously computed offset in BlockDecls. 310b750d928SMike Stump } 311aeb0ffd5SMike Stump NoteForHelper.resize(helpersize); 312d6ef62f0SMike Stump 313d6ef62f0SMike Stump // __descriptor 314d2142cffSMike Stump llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockHasCopyDispose, 315d2142cffSMike Stump subBlockSize, Ty, 316aeb0ffd5SMike Stump &NoteForHelper); 317aeb0ffd5SMike Stump Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty); 318aeb0ffd5SMike Stump Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp")); 319b750d928SMike Stump } 320b750d928SMike Stump 321b750d928SMike Stump QualType BPT = BE->getType(); 322b750d928SMike Stump return Builder.CreateBitCast(V, ConvertType(BPT)); 323b750d928SMike Stump } 32485284bacSMike Stump 32585284bacSMike Stump 32695435674SMike Stump const llvm::Type *BlockModule::getBlockDescriptorType() { 327650c932dSMike Stump if (BlockDescriptorType) 328650c932dSMike Stump return BlockDescriptorType; 3292437cbfaSAnders Carlsson 3302437cbfaSAnders Carlsson const llvm::Type *UnsignedLongTy = 331650c932dSMike Stump getTypes().ConvertType(getContext().UnsignedLongTy); 3322437cbfaSAnders Carlsson 3332437cbfaSAnders Carlsson // struct __block_descriptor { 3342437cbfaSAnders Carlsson // unsigned long reserved; 3352437cbfaSAnders Carlsson // unsigned long block_size; 3362437cbfaSAnders Carlsson // }; 337650c932dSMike Stump BlockDescriptorType = llvm::StructType::get(UnsignedLongTy, 3382437cbfaSAnders Carlsson UnsignedLongTy, 3392437cbfaSAnders Carlsson NULL); 3402437cbfaSAnders Carlsson 341650c932dSMike Stump getModule().addTypeName("struct.__block_descriptor", 342650c932dSMike Stump BlockDescriptorType); 3432437cbfaSAnders Carlsson 344650c932dSMike Stump return BlockDescriptorType; 3452437cbfaSAnders Carlsson } 3462437cbfaSAnders Carlsson 34795435674SMike Stump const llvm::Type *BlockModule::getGenericBlockLiteralType() { 348005c9a62SMike Stump if (GenericBlockLiteralType) 349005c9a62SMike Stump return GenericBlockLiteralType; 3502437cbfaSAnders Carlsson 3512437cbfaSAnders Carlsson const llvm::Type *BlockDescPtrTy = 352005c9a62SMike Stump llvm::PointerType::getUnqual(getBlockDescriptorType()); 3532437cbfaSAnders Carlsson 35492bbd6d4SMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 35592bbd6d4SMike Stump getTypes().ConvertType(getContext().IntTy)); 35692bbd6d4SMike Stump 3572437cbfaSAnders Carlsson // struct __block_literal_generic { 3585d2534adSMike Stump // void *__isa; 3595d2534adSMike Stump // int __flags; 3605d2534adSMike Stump // int __reserved; 3615d2534adSMike Stump // void (*__invoke)(void *); 3625d2534adSMike Stump // struct __block_descriptor *__descriptor; 3632437cbfaSAnders Carlsson // }; 364626aecc4SMike Stump GenericBlockLiteralType = llvm::StructType::get(PtrToInt8Ty, 36592bbd6d4SMike Stump IntTy, 36692bbd6d4SMike Stump IntTy, 367626aecc4SMike Stump PtrToInt8Ty, 3682437cbfaSAnders Carlsson BlockDescPtrTy, 3692437cbfaSAnders Carlsson NULL); 3702437cbfaSAnders Carlsson 371005c9a62SMike Stump getModule().addTypeName("struct.__block_literal_generic", 372005c9a62SMike Stump GenericBlockLiteralType); 3732437cbfaSAnders Carlsson 374005c9a62SMike Stump return GenericBlockLiteralType; 3752437cbfaSAnders Carlsson } 3762437cbfaSAnders Carlsson 37795435674SMike Stump const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() { 3785d2534adSMike Stump if (GenericExtendedBlockLiteralType) 3795d2534adSMike Stump return GenericExtendedBlockLiteralType; 3805d2534adSMike Stump 3815d2534adSMike Stump const llvm::Type *BlockDescPtrTy = 3825d2534adSMike Stump llvm::PointerType::getUnqual(getBlockDescriptorType()); 3835d2534adSMike Stump 3845d2534adSMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 3855d2534adSMike Stump getTypes().ConvertType(getContext().IntTy)); 3865d2534adSMike Stump 3875d2534adSMike Stump // struct __block_literal_generic { 3885d2534adSMike Stump // void *__isa; 3895d2534adSMike Stump // int __flags; 3905d2534adSMike Stump // int __reserved; 3915d2534adSMike Stump // void (*__invoke)(void *); 3925d2534adSMike Stump // struct __block_descriptor *__descriptor; 3935d2534adSMike Stump // void *__copy_func_helper_decl; 3945d2534adSMike Stump // void *__destroy_func_decl; 3955d2534adSMike Stump // }; 396626aecc4SMike Stump GenericExtendedBlockLiteralType = llvm::StructType::get(PtrToInt8Ty, 3975d2534adSMike Stump IntTy, 3985d2534adSMike Stump IntTy, 399626aecc4SMike Stump PtrToInt8Ty, 4005d2534adSMike Stump BlockDescPtrTy, 401626aecc4SMike Stump PtrToInt8Ty, 402626aecc4SMike Stump PtrToInt8Ty, 4035d2534adSMike Stump NULL); 4045d2534adSMike Stump 4055d2534adSMike Stump getModule().addTypeName("struct.__block_literal_extended_generic", 4065d2534adSMike Stump GenericExtendedBlockLiteralType); 4075d2534adSMike Stump 4085d2534adSMike Stump return GenericExtendedBlockLiteralType; 4095d2534adSMike Stump } 4105d2534adSMike Stump 4116a60fa24SAnders Carlsson RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) { 4122437cbfaSAnders Carlsson const BlockPointerType *BPT = 4132437cbfaSAnders Carlsson E->getCallee()->getType()->getAsBlockPointerType(); 4142437cbfaSAnders Carlsson 415*350da607SAnders Carlsson const CGFunctionInfo &FnInfo = CGM.getTypes().getFunctionInfo(BPT); 416*350da607SAnders Carlsson bool IsVariadic = 417*350da607SAnders Carlsson BPT->getPointeeType()->getAsFunctionProtoType()->isVariadic(); 418*350da607SAnders Carlsson 4192437cbfaSAnders Carlsson llvm::Value *Callee = EmitScalarExpr(E->getCallee()); 4202437cbfaSAnders Carlsson 4212437cbfaSAnders Carlsson // Get a pointer to the generic block literal. 4222437cbfaSAnders Carlsson const llvm::Type *BlockLiteralTy = 423005c9a62SMike Stump llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType()); 4242437cbfaSAnders Carlsson 4252437cbfaSAnders Carlsson // Bitcast the callee to a block literal. 4262437cbfaSAnders Carlsson llvm::Value *BlockLiteral = 4272437cbfaSAnders Carlsson Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal"); 4282437cbfaSAnders Carlsson 4292437cbfaSAnders Carlsson // Get the function pointer from the literal. 4302437cbfaSAnders Carlsson llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp"); 431624497c2SMike Stump llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp"); 4322437cbfaSAnders Carlsson 4332437cbfaSAnders Carlsson // Cast the function pointer to the right type. 4342437cbfaSAnders Carlsson const llvm::Type *BlockFTy = 435*350da607SAnders Carlsson CGM.getTypes().GetFunctionType(FnInfo, IsVariadic); 436*350da607SAnders Carlsson 4372437cbfaSAnders Carlsson const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy); 4382437cbfaSAnders Carlsson Func = Builder.CreateBitCast(Func, BlockFTyPtr); 4392437cbfaSAnders Carlsson 4402437cbfaSAnders Carlsson BlockLiteral = 4412437cbfaSAnders Carlsson Builder.CreateBitCast(BlockLiteral, 4422437cbfaSAnders Carlsson llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4432437cbfaSAnders Carlsson "tmp"); 4442437cbfaSAnders Carlsson 4452437cbfaSAnders Carlsson // Add the block literal. 4462437cbfaSAnders Carlsson QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy); 4472437cbfaSAnders Carlsson CallArgList Args; 4482437cbfaSAnders Carlsson Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy)); 4492437cbfaSAnders Carlsson 4502437cbfaSAnders Carlsson // And the rest of the arguments. 4512437cbfaSAnders Carlsson for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end(); 4522437cbfaSAnders Carlsson i != e; ++i) 4532437cbfaSAnders Carlsson Args.push_back(std::make_pair(EmitAnyExprToTemp(*i), 4542437cbfaSAnders Carlsson i->getType())); 4552437cbfaSAnders Carlsson 4562437cbfaSAnders Carlsson // And call the block. 457*350da607SAnders Carlsson return EmitCall(FnInfo, Func, Args); 4582437cbfaSAnders Carlsson } 4596a60fa24SAnders Carlsson 46097d01d50SMike Stump llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) { 46197d01d50SMike Stump uint64_t &offset = BlockDecls[E->getDecl()]; 46297d01d50SMike Stump 46397d01d50SMike Stump const llvm::Type *Ty; 46497d01d50SMike Stump Ty = CGM.getTypes().ConvertType(E->getDecl()->getType()); 46597d01d50SMike Stump 46697d01d50SMike Stump // See if we have already allocated an offset for this variable. 46797d01d50SMike Stump if (offset == 0) { 4684446dcf0SMike Stump // Don't run the expensive check, unless we have to. 4694446dcf0SMike Stump if (!BlockHasCopyDispose && BlockRequiresCopying(E->getType())) 4704446dcf0SMike Stump BlockHasCopyDispose = true; 47197d01d50SMike Stump // if not, allocate one now. 47297d01d50SMike Stump offset = getBlockOffset(E); 47397d01d50SMike Stump } 47497d01d50SMike Stump 47597d01d50SMike Stump llvm::Value *BlockLiteral = LoadBlockStruct(); 47697d01d50SMike Stump llvm::Value *V = Builder.CreateGEP(BlockLiteral, 47797d01d50SMike Stump llvm::ConstantInt::get(llvm::Type::Int64Ty, 47897d01d50SMike Stump offset), 4792d33d630SMike Stump "block.literal"); 48097d01d50SMike Stump if (E->isByRef()) { 48197d01d50SMike Stump bool needsCopyDispose = BlockRequiresCopying(E->getType()); 48297d01d50SMike Stump uint64_t Align = getContext().getDeclAlignInBytes(E->getDecl()); 48397d01d50SMike Stump const llvm::Type *PtrStructTy 48497d01d50SMike Stump = llvm::PointerType::get(BuildByRefType(E->getType(), Align), 0); 485efd7caa8SMike Stump // The block literal will need a copy/destroy helper. 486efd7caa8SMike Stump BlockHasCopyDispose = true; 48797d01d50SMike Stump Ty = PtrStructTy; 48897d01d50SMike Stump Ty = llvm::PointerType::get(Ty, 0); 48997d01d50SMike Stump V = Builder.CreateBitCast(V, Ty); 49097d01d50SMike Stump V = Builder.CreateLoad(V, false); 49197d01d50SMike Stump V = Builder.CreateStructGEP(V, 1, "forwarding"); 49297d01d50SMike Stump V = Builder.CreateLoad(V, false); 49397d01d50SMike Stump V = Builder.CreateBitCast(V, PtrStructTy); 49497d01d50SMike Stump V = Builder.CreateStructGEP(V, needsCopyDispose*2 + 4, "x"); 49597d01d50SMike Stump } else { 49697d01d50SMike Stump Ty = llvm::PointerType::get(Ty, 0); 49797d01d50SMike Stump V = Builder.CreateBitCast(V, Ty); 49897d01d50SMike Stump } 49997d01d50SMike Stump return V; 50097d01d50SMike Stump } 50197d01d50SMike Stump 502692c6e37SMike Stump void CodeGenFunction::BlockForwardSelf() { 503692c6e37SMike Stump const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); 504692c6e37SMike Stump ImplicitParamDecl *SelfDecl = OMD->getSelfDecl(); 505692c6e37SMike Stump llvm::Value *&DMEntry = LocalDeclMap[SelfDecl]; 506692c6e37SMike Stump if (DMEntry) 507692c6e37SMike Stump return; 508692c6e37SMike Stump // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care 509692c6e37SMike Stump BlockDeclRefExpr *BDRE = new (getContext()) 510692c6e37SMike Stump BlockDeclRefExpr(SelfDecl, 511692c6e37SMike Stump SelfDecl->getType(), SourceLocation(), false); 512692c6e37SMike Stump DMEntry = GetAddrOfBlockDecl(BDRE); 513692c6e37SMike Stump } 514692c6e37SMike Stump 5152d5a2878SMike Stump llvm::Constant * 5166c39666aSMike Stump BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) { 5176a60fa24SAnders Carlsson // Generate the block descriptor. 5186a60fa24SAnders Carlsson const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy); 51992bbd6d4SMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 52092bbd6d4SMike Stump getTypes().ConvertType(getContext().IntTy)); 5216a60fa24SAnders Carlsson 5226a60fa24SAnders Carlsson llvm::Constant *DescriptorFields[2]; 5236a60fa24SAnders Carlsson 5246a60fa24SAnders Carlsson // Reserved 5256a60fa24SAnders Carlsson DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy); 5266a60fa24SAnders Carlsson 5276a60fa24SAnders Carlsson // Block literal size. For global blocks we just use the size of the generic 5286a60fa24SAnders Carlsson // block literal struct. 5296a60fa24SAnders Carlsson uint64_t BlockLiteralSize = 530005c9a62SMike Stump TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8; 5316a60fa24SAnders Carlsson DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize); 5326a60fa24SAnders Carlsson 5336a60fa24SAnders Carlsson llvm::Constant *DescriptorStruct = 5346a60fa24SAnders Carlsson llvm::ConstantStruct::get(&DescriptorFields[0], 2); 5356a60fa24SAnders Carlsson 5366a60fa24SAnders Carlsson llvm::GlobalVariable *Descriptor = 5376a60fa24SAnders Carlsson new llvm::GlobalVariable(DescriptorStruct->getType(), true, 5386a60fa24SAnders Carlsson llvm::GlobalVariable::InternalLinkage, 5396a60fa24SAnders Carlsson DescriptorStruct, "__block_descriptor_global", 5406a60fa24SAnders Carlsson &getModule()); 5416a60fa24SAnders Carlsson 5426a60fa24SAnders Carlsson // Generate the constants for the block literal. 5436a60fa24SAnders Carlsson llvm::Constant *LiteralFields[5]; 5446a60fa24SAnders Carlsson 5452d5a2878SMike Stump CodeGenFunction::BlockInfo Info(0, n); 546b750d928SMike Stump uint64_t subBlockSize, subBlockAlign; 5471db7d04bSMike Stump llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; 548a61bb4d1SDaniel Dunbar bool subBlockHasCopyDispose = false; 5495469f29aSMike Stump llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap; 550cb2fbcb0SMike Stump llvm::Function *Fn 551692c6e37SMike Stump = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap, 5525469f29aSMike Stump subBlockSize, 553b750d928SMike Stump subBlockAlign, 5544446dcf0SMike Stump subBlockDeclRefDecls, 5554446dcf0SMike Stump subBlockHasCopyDispose); 556cb2fbcb0SMike Stump assert(subBlockSize == BlockLiteralSize 557cb2fbcb0SMike Stump && "no imports allowed for global block"); 558a61bb4d1SDaniel Dunbar assert(!subBlockHasCopyDispose && "no imports allowed for global block"); 5596a60fa24SAnders Carlsson 5606a60fa24SAnders Carlsson // isa 561971f9b66SMike Stump LiteralFields[0] = getNSConcreteGlobalBlock(); 5626a60fa24SAnders Carlsson 5636a60fa24SAnders Carlsson // Flags 5645878c797SAnders Carlsson LiteralFields[1] = 5655878c797SAnders Carlsson llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR); 5666a60fa24SAnders Carlsson 5676a60fa24SAnders Carlsson // Reserved 56892bbd6d4SMike Stump LiteralFields[2] = llvm::Constant::getNullValue(IntTy); 5696a60fa24SAnders Carlsson 5706a60fa24SAnders Carlsson // Function 5716a60fa24SAnders Carlsson LiteralFields[3] = Fn; 5726a60fa24SAnders Carlsson 5736a60fa24SAnders Carlsson // Descriptor 5746a60fa24SAnders Carlsson LiteralFields[4] = Descriptor; 5756a60fa24SAnders Carlsson 5766a60fa24SAnders Carlsson llvm::Constant *BlockLiteralStruct = 5776a60fa24SAnders Carlsson llvm::ConstantStruct::get(&LiteralFields[0], 5); 5786a60fa24SAnders Carlsson 5796a60fa24SAnders Carlsson llvm::GlobalVariable *BlockLiteral = 5806a60fa24SAnders Carlsson new llvm::GlobalVariable(BlockLiteralStruct->getType(), true, 5816a60fa24SAnders Carlsson llvm::GlobalVariable::InternalLinkage, 5826a60fa24SAnders Carlsson BlockLiteralStruct, "__block_literal_global", 5836a60fa24SAnders Carlsson &getModule()); 5846a60fa24SAnders Carlsson 5856a60fa24SAnders Carlsson return BlockLiteral; 5866a60fa24SAnders Carlsson } 5876a60fa24SAnders Carlsson 588cb2fbcb0SMike Stump llvm::Value *CodeGenFunction::LoadBlockStruct() { 589cb2fbcb0SMike Stump return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self"); 590cb2fbcb0SMike Stump } 591cb2fbcb0SMike Stump 5924446dcf0SMike Stump llvm::Function * 5934446dcf0SMike Stump CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr, 594cb2fbcb0SMike Stump const BlockInfo& Info, 595692c6e37SMike Stump const Decl *OuterFuncDecl, 5965469f29aSMike Stump llvm::DenseMap<const Decl*, llvm::Value*> ldm, 597b750d928SMike Stump uint64_t &Size, 598b750d928SMike Stump uint64_t &Align, 5994446dcf0SMike Stump llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls, 6004446dcf0SMike Stump bool &subBlockHasCopyDispose) { 6015469f29aSMike Stump // Arrange for local static and local extern declarations to appear 6025469f29aSMike Stump // to be local to this function as well, as they are directly referenced 6035469f29aSMike Stump // in a block. 6045469f29aSMike Stump for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin(); 6055469f29aSMike Stump i != ldm.end(); 6065469f29aSMike Stump ++i) { 6075469f29aSMike Stump const VarDecl *VD = dyn_cast<VarDecl>(i->first); 6085469f29aSMike Stump 6095469f29aSMike Stump if (VD->getStorageClass() == VarDecl::Static 6105469f29aSMike Stump || VD->getStorageClass() == VarDecl::Extern) 6115469f29aSMike Stump LocalDeclMap[VD] = i->second; 6125469f29aSMike Stump } 6135469f29aSMike Stump 61409a9b6e3SEli Friedman // FIXME: We need to rearrange the code for copy/dispose so we have this 61509a9b6e3SEli Friedman // sooner, so we can calculate offsets correctly. 61609a9b6e3SEli Friedman if (!BlockHasCopyDispose) 61709a9b6e3SEli Friedman BlockOffset = CGM.getTargetData() 61809a9b6e3SEli Friedman .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8; 61909a9b6e3SEli Friedman else 62009a9b6e3SEli Friedman BlockOffset = CGM.getTargetData() 62109a9b6e3SEli Friedman .getTypeStoreSizeInBits(CGM.getGenericExtendedBlockLiteralType()) / 8; 62209a9b6e3SEli Friedman BlockAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 62309a9b6e3SEli Friedman 624deaad8ccSDouglas Gregor const FunctionProtoType *FTy = 6253385fe12SChris Lattner cast<FunctionProtoType>(BExpr->getFunctionType()); 6266a60fa24SAnders Carlsson 6276a60fa24SAnders Carlsson FunctionArgList Args; 6286a60fa24SAnders Carlsson 6293385fe12SChris Lattner const BlockDecl *BD = BExpr->getBlockDecl(); 6306a60fa24SAnders Carlsson 6316a60fa24SAnders Carlsson // FIXME: This leaks 6326a60fa24SAnders Carlsson ImplicitParamDecl *SelfDecl = 6336a60fa24SAnders Carlsson ImplicitParamDecl::Create(getContext(), 0, 6346a60fa24SAnders Carlsson SourceLocation(), 0, 6356a60fa24SAnders Carlsson getContext().getPointerType(getContext().VoidTy)); 6366a60fa24SAnders Carlsson 6376a60fa24SAnders Carlsson Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType())); 638cb2fbcb0SMike Stump BlockStructDecl = SelfDecl; 6396a60fa24SAnders Carlsson 640c4b30e59SSteve Naroff for (BlockDecl::param_const_iterator i = BD->param_begin(), 6416a60fa24SAnders Carlsson e = BD->param_end(); i != e; ++i) 642eecd39f2SMike Stump Args.push_back(std::make_pair(*i, (*i)->getType())); 6436a60fa24SAnders Carlsson 6446a60fa24SAnders Carlsson const CGFunctionInfo &FI = 6456a60fa24SAnders Carlsson CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args); 6466a60fa24SAnders Carlsson 6472d5a2878SMike Stump std::string Name = std::string("__") + Info.Name + "_block_invoke_"; 6486a60fa24SAnders Carlsson CodeGenTypes &Types = CGM.getTypes(); 6496a60fa24SAnders Carlsson const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic()); 6506a60fa24SAnders Carlsson 6516a60fa24SAnders Carlsson llvm::Function *Fn = 6526a60fa24SAnders Carlsson llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 6536a60fa24SAnders Carlsson Name, 6546a60fa24SAnders Carlsson &CGM.getModule()); 6556a60fa24SAnders Carlsson 6566a60fa24SAnders Carlsson StartFunction(BD, FTy->getResultType(), Fn, Args, 6573385fe12SChris Lattner BExpr->getBody()->getLocEnd()); 658692c6e37SMike Stump CurFuncDecl = OuterFuncDecl; 6593385fe12SChris Lattner EmitStmt(BExpr->getBody()); 6603385fe12SChris Lattner FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc()); 6616a60fa24SAnders Carlsson 662b750d928SMike Stump // The runtime needs a minimum alignment of a void *. 663b750d928SMike Stump uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 664b750d928SMike Stump BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign); 665b750d928SMike Stump 666cb2fbcb0SMike Stump Size = BlockOffset; 667b750d928SMike Stump Align = BlockAlign; 668b750d928SMike Stump subBlockDeclRefDecls = BlockDeclRefDecls; 6694446dcf0SMike Stump subBlockHasCopyDispose |= BlockHasCopyDispose; 6706a60fa24SAnders Carlsson return Fn; 6716a60fa24SAnders Carlsson } 6721db7d04bSMike Stump 673aeb0ffd5SMike Stump uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) { 6741db7d04bSMike Stump const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl()); 6751db7d04bSMike Stump 6761db7d04bSMike Stump uint64_t Size = getContext().getTypeSize(D->getType()) / 8; 6771db7d04bSMike Stump uint64_t Align = getContext().getDeclAlignInBytes(D); 6781db7d04bSMike Stump 6791db7d04bSMike Stump if (BDRE->isByRef()) { 6801db7d04bSMike Stump Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8; 6811db7d04bSMike Stump Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 6821db7d04bSMike Stump } 6831db7d04bSMike Stump 6841db7d04bSMike Stump assert ((Align > 0) && "alignment must be 1 byte or more"); 6851db7d04bSMike Stump 6861db7d04bSMike Stump uint64_t OldOffset = BlockOffset; 6871db7d04bSMike Stump 6881db7d04bSMike Stump // Ensure proper alignment, even if it means we have to have a gap 6891db7d04bSMike Stump BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align); 6901db7d04bSMike Stump BlockAlign = std::max(Align, BlockAlign); 6911db7d04bSMike Stump 6921db7d04bSMike Stump uint64_t Pad = BlockOffset - OldOffset; 6931db7d04bSMike Stump if (Pad) { 6941db7d04bSMike Stump llvm::ArrayType::get(llvm::Type::Int8Ty, Pad); 6951db7d04bSMike Stump QualType PadTy = getContext().getConstantArrayType(getContext().CharTy, 6961db7d04bSMike Stump llvm::APInt(32, Pad), 6971db7d04bSMike Stump ArrayType::Normal, 0); 6981db7d04bSMike Stump ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(), 6991db7d04bSMike Stump 0, QualType(PadTy), VarDecl::None, 7001db7d04bSMike Stump SourceLocation()); 7011db7d04bSMike Stump Expr *E; 7021db7d04bSMike Stump E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(), 7031db7d04bSMike Stump SourceLocation(), false, false); 7041db7d04bSMike Stump BlockDeclRefDecls.push_back(E); 7051db7d04bSMike Stump } 7061db7d04bSMike Stump BlockDeclRefDecls.push_back(BDRE); 7071db7d04bSMike Stump 7081db7d04bSMike Stump BlockOffset += Size; 7091db7d04bSMike Stump return BlockOffset-Size; 7101db7d04bSMike Stump } 71197d01d50SMike Stump 712aeb0ffd5SMike Stump llvm::Constant *BlockFunction:: 713aeb0ffd5SMike Stump GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T, 714aeb0ffd5SMike Stump std::vector<HelperInfo> &NoteForHelper) { 7150c743277SMike Stump QualType R = getContext().VoidTy; 7160c743277SMike Stump 7170c743277SMike Stump FunctionArgList Args; 7180c743277SMike Stump // FIXME: This leaks 719aeb0ffd5SMike Stump ImplicitParamDecl *Dst = 720aeb0ffd5SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 721aeb0ffd5SMike Stump getContext().getPointerType(getContext().VoidTy)); 722aeb0ffd5SMike Stump Args.push_back(std::make_pair(Dst, Dst->getType())); 7230c743277SMike Stump ImplicitParamDecl *Src = 7240c743277SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 7250c743277SMike Stump getContext().getPointerType(getContext().VoidTy)); 7260c743277SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 7270c743277SMike Stump 7280c743277SMike Stump const CGFunctionInfo &FI = 7290c743277SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 7300c743277SMike Stump 7310c743277SMike Stump std::string Name = std::string("__copy_helper_block_"); 7320c743277SMike Stump CodeGenTypes &Types = CGM.getTypes(); 7330c743277SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 7340c743277SMike Stump 7350c743277SMike Stump llvm::Function *Fn = 7360c743277SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 7370c743277SMike Stump Name, 7380c743277SMike Stump &CGM.getModule()); 7390c743277SMike Stump 7400c743277SMike Stump IdentifierInfo *II 7410c743277SMike Stump = &CGM.getContext().Idents.get("__copy_helper_block_"); 7420c743277SMike Stump 7430c743277SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 7440c743277SMike Stump getContext().getTranslationUnitDecl(), 7450c743277SMike Stump SourceLocation(), II, R, 7460c743277SMike Stump FunctionDecl::Static, false, 7470c743277SMike Stump true); 7480c743277SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 749aeb0ffd5SMike Stump 750aeb0ffd5SMike Stump llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); 751aeb0ffd5SMike Stump llvm::Type *PtrPtrT; 752aeb0ffd5SMike Stump PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 753aeb0ffd5SMike Stump SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); 754aeb0ffd5SMike Stump SrcObj = Builder.CreateLoad(SrcObj); 755aeb0ffd5SMike Stump 756aeb0ffd5SMike Stump llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst); 757aeb0ffd5SMike Stump DstObj = Builder.CreateBitCast(DstObj, llvm::PointerType::get(T, 0)); 758aeb0ffd5SMike Stump 759aeb0ffd5SMike Stump for (unsigned i=0; i < NoteForHelper.size(); ++i) { 760aeb0ffd5SMike Stump int flag = NoteForHelper[i].flag; 761aeb0ffd5SMike Stump int index = NoteForHelper[i].index; 762aeb0ffd5SMike Stump 763aeb0ffd5SMike Stump if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) 764aeb0ffd5SMike Stump || NoteForHelper[i].RequiresCopying) { 765aeb0ffd5SMike Stump llvm::Value *Srcv = SrcObj; 766aeb0ffd5SMike Stump Srcv = Builder.CreateStructGEP(Srcv, index); 767aeb0ffd5SMike Stump Srcv = Builder.CreateBitCast(Srcv, 768aeb0ffd5SMike Stump llvm::PointerType::get(PtrToInt8Ty, 0)); 769aeb0ffd5SMike Stump Srcv = Builder.CreateLoad(Srcv); 770aeb0ffd5SMike Stump 771aeb0ffd5SMike Stump llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index); 772aeb0ffd5SMike Stump Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty); 773aeb0ffd5SMike Stump 774aeb0ffd5SMike Stump llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag); 775aeb0ffd5SMike Stump llvm::Value *F = getBlockObjectAssign(); 776aeb0ffd5SMike Stump Builder.CreateCall3(F, Dstv, Srcv, N); 777aeb0ffd5SMike Stump } 778aeb0ffd5SMike Stump } 779aeb0ffd5SMike Stump 7800c743277SMike Stump CGF.FinishFunction(); 7810c743277SMike Stump 7820c743277SMike Stump return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 78397d01d50SMike Stump } 78497d01d50SMike Stump 785d6ef62f0SMike Stump llvm::Constant *BlockFunction:: 786aeb0ffd5SMike Stump GenerateDestroyHelperFunction(bool BlockHasCopyDispose, 787aeb0ffd5SMike Stump const llvm::StructType* T, 788aeb0ffd5SMike Stump std::vector<HelperInfo> &NoteForHelper) { 7890c743277SMike Stump QualType R = getContext().VoidTy; 7900c743277SMike Stump 7910c743277SMike Stump FunctionArgList Args; 7920c743277SMike Stump // FIXME: This leaks 7930c743277SMike Stump ImplicitParamDecl *Src = 7940c743277SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 7950c743277SMike Stump getContext().getPointerType(getContext().VoidTy)); 7960c743277SMike Stump 7970c743277SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 7980c743277SMike Stump 7990c743277SMike Stump const CGFunctionInfo &FI = 8000c743277SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 8010c743277SMike Stump 8020c743277SMike Stump std::string Name = std::string("__destroy_helper_block_"); 8030c743277SMike Stump CodeGenTypes &Types = CGM.getTypes(); 8040c743277SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 8050c743277SMike Stump 8060c743277SMike Stump llvm::Function *Fn = 8070c743277SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 8080c743277SMike Stump Name, 8090c743277SMike Stump &CGM.getModule()); 8100c743277SMike Stump 8110c743277SMike Stump IdentifierInfo *II 8120c743277SMike Stump = &CGM.getContext().Idents.get("__destroy_helper_block_"); 8130c743277SMike Stump 8140c743277SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 8150c743277SMike Stump getContext().getTranslationUnitDecl(), 8160c743277SMike Stump SourceLocation(), II, R, 8170c743277SMike Stump FunctionDecl::Static, false, 8180c743277SMike Stump true); 8190c743277SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 8206f7d9f87SMike Stump 8216f7d9f87SMike Stump llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); 8226f7d9f87SMike Stump llvm::Type *PtrPtrT; 8236f7d9f87SMike Stump PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 8246f7d9f87SMike Stump SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); 8256f7d9f87SMike Stump SrcObj = Builder.CreateLoad(SrcObj); 8266f7d9f87SMike Stump 8276f7d9f87SMike Stump for (unsigned i=0; i < NoteForHelper.size(); ++i) { 8286f7d9f87SMike Stump int flag = NoteForHelper[i].flag; 8296f7d9f87SMike Stump int index = NoteForHelper[i].index; 8306f7d9f87SMike Stump 8316f7d9f87SMike Stump if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) 8326f7d9f87SMike Stump || NoteForHelper[i].RequiresCopying) { 8336f7d9f87SMike Stump llvm::Value *Srcv = SrcObj; 8346f7d9f87SMike Stump Srcv = Builder.CreateStructGEP(Srcv, index); 8356f7d9f87SMike Stump Srcv = Builder.CreateBitCast(Srcv, 8366f7d9f87SMike Stump llvm::PointerType::get(PtrToInt8Ty, 0)); 8376f7d9f87SMike Stump Srcv = Builder.CreateLoad(Srcv); 8386f7d9f87SMike Stump 8396f7d9f87SMike Stump BuildBlockRelease(Srcv, flag); 8406f7d9f87SMike Stump } 8416f7d9f87SMike Stump } 8426f7d9f87SMike Stump 8430c743277SMike Stump CGF.FinishFunction(); 8440c743277SMike Stump 8450c743277SMike Stump return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 8460c743277SMike Stump } 8470c743277SMike Stump 848aeb0ffd5SMike Stump llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T, 849aeb0ffd5SMike Stump std::vector<HelperInfo> &NoteForHelper) { 850aeb0ffd5SMike Stump return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose, 851aeb0ffd5SMike Stump T, NoteForHelper); 8520c743277SMike Stump } 8530c743277SMike Stump 854aeb0ffd5SMike Stump llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T, 855aeb0ffd5SMike Stump std::vector<HelperInfo> &NoteForHelper) { 856aeb0ffd5SMike Stump return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose, 857aeb0ffd5SMike Stump T, NoteForHelper); 85897d01d50SMike Stump } 859626aecc4SMike Stump 860f89230d9SMike Stump llvm::Constant *BlockFunction:: 861f89230d9SMike Stump GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) { 862ee2a5ee5SMike Stump QualType R = getContext().VoidTy; 863ee2a5ee5SMike Stump 864ee2a5ee5SMike Stump FunctionArgList Args; 865ee2a5ee5SMike Stump // FIXME: This leaks 866f89230d9SMike Stump ImplicitParamDecl *Dst = 867f89230d9SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 868f89230d9SMike Stump getContext().getPointerType(getContext().VoidTy)); 869f89230d9SMike Stump Args.push_back(std::make_pair(Dst, Dst->getType())); 870f89230d9SMike Stump 871f89230d9SMike Stump // FIXME: This leaks 872ee2a5ee5SMike Stump ImplicitParamDecl *Src = 873ee2a5ee5SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 874ee2a5ee5SMike Stump getContext().getPointerType(getContext().VoidTy)); 875ee2a5ee5SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 876ee2a5ee5SMike Stump 877ee2a5ee5SMike Stump const CGFunctionInfo &FI = 878ee2a5ee5SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 879ee2a5ee5SMike Stump 880ee2a5ee5SMike Stump std::string Name = std::string("__Block_byref_id_object_copy_"); 881ee2a5ee5SMike Stump CodeGenTypes &Types = CGM.getTypes(); 882ee2a5ee5SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 883ee2a5ee5SMike Stump 884ee2a5ee5SMike Stump llvm::Function *Fn = 885ee2a5ee5SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 886ee2a5ee5SMike Stump Name, 887ee2a5ee5SMike Stump &CGM.getModule()); 888ee2a5ee5SMike Stump 889ee2a5ee5SMike Stump IdentifierInfo *II 890ee2a5ee5SMike Stump = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_"); 891ee2a5ee5SMike Stump 892ee2a5ee5SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 893ee2a5ee5SMike Stump getContext().getTranslationUnitDecl(), 894ee2a5ee5SMike Stump SourceLocation(), II, R, 895ee2a5ee5SMike Stump FunctionDecl::Static, false, 896ee2a5ee5SMike Stump true); 897ee2a5ee5SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 898f89230d9SMike Stump 899f89230d9SMike Stump // dst->x 900f89230d9SMike Stump llvm::Value *V = CGF.GetAddrOfLocalVar(Dst); 901f89230d9SMike Stump V = Builder.CreateBitCast(V, T); 902f89230d9SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 903f89230d9SMike Stump llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty); 904f89230d9SMike Stump 905f89230d9SMike Stump // src->x 906f89230d9SMike Stump V = CGF.GetAddrOfLocalVar(Src); 907f89230d9SMike Stump V = Builder.CreateLoad(V); 908f89230d9SMike Stump V = Builder.CreateBitCast(V, T); 909f89230d9SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 910f89230d9SMike Stump V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); 911f89230d9SMike Stump llvm::Value *SrcObj = Builder.CreateLoad(V); 912f89230d9SMike Stump 913f89230d9SMike Stump flag |= BLOCK_BYREF_CALLER; 914f89230d9SMike Stump 915f89230d9SMike Stump llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag); 916f89230d9SMike Stump llvm::Value *F = getBlockObjectAssign(); 917f89230d9SMike Stump Builder.CreateCall3(F, DstObj, SrcObj, N); 918f89230d9SMike Stump 919ee2a5ee5SMike Stump CGF.FinishFunction(); 920ee2a5ee5SMike Stump 921ee2a5ee5SMike Stump return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 922ee2a5ee5SMike Stump } 923ee2a5ee5SMike Stump 924fbe25dd1SMike Stump llvm::Constant * 925fbe25dd1SMike Stump BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T, 926fbe25dd1SMike Stump int flag) { 927ee2a5ee5SMike Stump QualType R = getContext().VoidTy; 928ee2a5ee5SMike Stump 929ee2a5ee5SMike Stump FunctionArgList Args; 930ee2a5ee5SMike Stump // FIXME: This leaks 931ee2a5ee5SMike Stump ImplicitParamDecl *Src = 932ee2a5ee5SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 933ee2a5ee5SMike Stump getContext().getPointerType(getContext().VoidTy)); 934ee2a5ee5SMike Stump 935ee2a5ee5SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 936ee2a5ee5SMike Stump 937ee2a5ee5SMike Stump const CGFunctionInfo &FI = 938ee2a5ee5SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 939ee2a5ee5SMike Stump 940ee2a5ee5SMike Stump std::string Name = std::string("__Block_byref_id_object_dispose_"); 941ee2a5ee5SMike Stump CodeGenTypes &Types = CGM.getTypes(); 942ee2a5ee5SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 943ee2a5ee5SMike Stump 944ee2a5ee5SMike Stump llvm::Function *Fn = 945ee2a5ee5SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 946ee2a5ee5SMike Stump Name, 947ee2a5ee5SMike Stump &CGM.getModule()); 948ee2a5ee5SMike Stump 949ee2a5ee5SMike Stump IdentifierInfo *II 950ee2a5ee5SMike Stump = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_"); 951ee2a5ee5SMike Stump 952ee2a5ee5SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 953ee2a5ee5SMike Stump getContext().getTranslationUnitDecl(), 954ee2a5ee5SMike Stump SourceLocation(), II, R, 955ee2a5ee5SMike Stump FunctionDecl::Static, false, 956ee2a5ee5SMike Stump true); 957ee2a5ee5SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 958fbe25dd1SMike Stump 959fbe25dd1SMike Stump llvm::Value *V = CGF.GetAddrOfLocalVar(Src); 960fbe25dd1SMike Stump V = Builder.CreateBitCast(V, T); 961fbe25dd1SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 962fbe25dd1SMike Stump V = Builder.CreateBitCast(V, PtrToInt8Ty); 963fbe25dd1SMike Stump 964fbe25dd1SMike Stump flag |= BLOCK_BYREF_CALLER; 965fbe25dd1SMike Stump BuildBlockRelease(V, flag); 966ee2a5ee5SMike Stump CGF.FinishFunction(); 967ee2a5ee5SMike Stump 968ee2a5ee5SMike Stump return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 969ee2a5ee5SMike Stump } 970ee2a5ee5SMike Stump 971f89230d9SMike Stump llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T, 972f89230d9SMike Stump int flag) { 973f89230d9SMike Stump return CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag); 974ee2a5ee5SMike Stump } 975ee2a5ee5SMike Stump 976fbe25dd1SMike Stump llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T, 977fbe25dd1SMike Stump int flag) { 978fbe25dd1SMike Stump return CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag); 979ee2a5ee5SMike Stump } 980ee2a5ee5SMike Stump 981626aecc4SMike Stump llvm::Value *BlockFunction::getBlockObjectDispose() { 982626aecc4SMike Stump if (CGM.BlockObjectDispose == 0) { 983626aecc4SMike Stump const llvm::FunctionType *FTy; 984626aecc4SMike Stump std::vector<const llvm::Type*> ArgTys; 985626aecc4SMike Stump const llvm::Type *ResultType = llvm::Type::VoidTy; 986626aecc4SMike Stump ArgTys.push_back(PtrToInt8Ty); 987626aecc4SMike Stump ArgTys.push_back(llvm::Type::Int32Ty); 988626aecc4SMike Stump FTy = llvm::FunctionType::get(ResultType, ArgTys, false); 989626aecc4SMike Stump CGM.BlockObjectDispose 990626aecc4SMike Stump = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose"); 991626aecc4SMike Stump } 992626aecc4SMike Stump return CGM.BlockObjectDispose; 993626aecc4SMike Stump } 994626aecc4SMike Stump 995f89230d9SMike Stump llvm::Value *BlockFunction::getBlockObjectAssign() { 996f89230d9SMike Stump if (CGM.BlockObjectAssign == 0) { 997f89230d9SMike Stump const llvm::FunctionType *FTy; 998f89230d9SMike Stump std::vector<const llvm::Type*> ArgTys; 999f89230d9SMike Stump const llvm::Type *ResultType = llvm::Type::VoidTy; 1000f89230d9SMike Stump ArgTys.push_back(PtrToInt8Ty); 1001f89230d9SMike Stump ArgTys.push_back(PtrToInt8Ty); 1002f89230d9SMike Stump ArgTys.push_back(llvm::Type::Int32Ty); 1003f89230d9SMike Stump FTy = llvm::FunctionType::get(ResultType, ArgTys, false); 1004f89230d9SMike Stump CGM.BlockObjectAssign 1005f89230d9SMike Stump = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign"); 1006f89230d9SMike Stump } 1007f89230d9SMike Stump return CGM.BlockObjectAssign; 1008f89230d9SMike Stump } 1009f89230d9SMike Stump 1010fbe25dd1SMike Stump void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) { 1011626aecc4SMike Stump llvm::Value *F = getBlockObjectDispose(); 1012fbe25dd1SMike Stump llvm::Value *N; 1013626aecc4SMike Stump V = Builder.CreateBitCast(V, PtrToInt8Ty); 1014fbe25dd1SMike Stump N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag); 1015626aecc4SMike Stump Builder.CreateCall2(F, V, N); 1016626aecc4SMike Stump } 10174446dcf0SMike Stump 10184446dcf0SMike Stump ASTContext &BlockFunction::getContext() const { return CGM.getContext(); } 1019aeb0ffd5SMike Stump 1020aeb0ffd5SMike Stump BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf, 1021aeb0ffd5SMike Stump CGBuilderTy &B) 1022aeb0ffd5SMike Stump : CGM(cgm), CGF(cgf), Builder(B) { 1023aeb0ffd5SMike Stump PtrToInt8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 1024aeb0ffd5SMike Stump 1025aeb0ffd5SMike Stump BlockHasCopyDispose = false; 1026aeb0ffd5SMike Stump } 1027