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 4152437cbfaSAnders Carlsson llvm::Value *Callee = EmitScalarExpr(E->getCallee()); 4162437cbfaSAnders Carlsson 4172437cbfaSAnders Carlsson // Get a pointer to the generic block literal. 4182437cbfaSAnders Carlsson const llvm::Type *BlockLiteralTy = 419005c9a62SMike Stump llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType()); 4202437cbfaSAnders Carlsson 4212437cbfaSAnders Carlsson // Bitcast the callee to a block literal. 4222437cbfaSAnders Carlsson llvm::Value *BlockLiteral = 4232437cbfaSAnders Carlsson Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal"); 4242437cbfaSAnders Carlsson 4252437cbfaSAnders Carlsson // Get the function pointer from the literal. 4262437cbfaSAnders Carlsson llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp"); 4272437cbfaSAnders Carlsson 4282437cbfaSAnders Carlsson BlockLiteral = 4292437cbfaSAnders Carlsson Builder.CreateBitCast(BlockLiteral, 4302437cbfaSAnders Carlsson llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4312437cbfaSAnders Carlsson "tmp"); 4322437cbfaSAnders Carlsson 4332437cbfaSAnders Carlsson // Add the block literal. 4342437cbfaSAnders Carlsson QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy); 4352437cbfaSAnders Carlsson CallArgList Args; 4362437cbfaSAnders Carlsson Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy)); 4372437cbfaSAnders Carlsson 4382437cbfaSAnders Carlsson // And the rest of the arguments. 4392437cbfaSAnders Carlsson for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end(); 4402437cbfaSAnders Carlsson i != e; ++i) 4412437cbfaSAnders Carlsson Args.push_back(std::make_pair(EmitAnyExprToTemp(*i), 4422437cbfaSAnders Carlsson i->getType())); 4432437cbfaSAnders Carlsson 4445f50c651SAnders Carlsson // Load the function. 4455f50c651SAnders Carlsson llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp"); 4465f50c651SAnders Carlsson 447*a60cbcdfSAnders Carlsson QualType FnType = BPT->getPointeeType(); 448*a60cbcdfSAnders Carlsson QualType ResultType = FnType->getAsFunctionType()->getResultType(); 449*a60cbcdfSAnders Carlsson 450*a60cbcdfSAnders Carlsson const CGFunctionInfo &FnInfo = 451*a60cbcdfSAnders Carlsson CGM.getTypes().getFunctionInfo(ResultType, Args); 4525f50c651SAnders Carlsson 4535f50c651SAnders Carlsson // Cast the function pointer to the right type. 4545f50c651SAnders Carlsson const llvm::Type *BlockFTy = 455*a60cbcdfSAnders Carlsson CGM.getTypes().GetFunctionType(FnInfo, false); 4565f50c651SAnders Carlsson 4575f50c651SAnders Carlsson const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy); 4585f50c651SAnders Carlsson Func = Builder.CreateBitCast(Func, BlockFTyPtr); 4595f50c651SAnders Carlsson 4602437cbfaSAnders Carlsson // And call the block. 461350da607SAnders Carlsson return EmitCall(FnInfo, Func, Args); 4622437cbfaSAnders Carlsson } 4636a60fa24SAnders Carlsson 46497d01d50SMike Stump llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) { 46597d01d50SMike Stump uint64_t &offset = BlockDecls[E->getDecl()]; 46697d01d50SMike Stump 46797d01d50SMike Stump const llvm::Type *Ty; 46897d01d50SMike Stump Ty = CGM.getTypes().ConvertType(E->getDecl()->getType()); 46997d01d50SMike Stump 47097d01d50SMike Stump // See if we have already allocated an offset for this variable. 47197d01d50SMike Stump if (offset == 0) { 4724446dcf0SMike Stump // Don't run the expensive check, unless we have to. 4734446dcf0SMike Stump if (!BlockHasCopyDispose && BlockRequiresCopying(E->getType())) 4744446dcf0SMike Stump BlockHasCopyDispose = true; 47597d01d50SMike Stump // if not, allocate one now. 47697d01d50SMike Stump offset = getBlockOffset(E); 47797d01d50SMike Stump } 47897d01d50SMike Stump 47997d01d50SMike Stump llvm::Value *BlockLiteral = LoadBlockStruct(); 48097d01d50SMike Stump llvm::Value *V = Builder.CreateGEP(BlockLiteral, 48197d01d50SMike Stump llvm::ConstantInt::get(llvm::Type::Int64Ty, 48297d01d50SMike Stump offset), 4832d33d630SMike Stump "block.literal"); 48497d01d50SMike Stump if (E->isByRef()) { 48597d01d50SMike Stump bool needsCopyDispose = BlockRequiresCopying(E->getType()); 48697d01d50SMike Stump uint64_t Align = getContext().getDeclAlignInBytes(E->getDecl()); 48797d01d50SMike Stump const llvm::Type *PtrStructTy 48897d01d50SMike Stump = llvm::PointerType::get(BuildByRefType(E->getType(), Align), 0); 489efd7caa8SMike Stump // The block literal will need a copy/destroy helper. 490efd7caa8SMike Stump BlockHasCopyDispose = true; 49197d01d50SMike Stump Ty = PtrStructTy; 49297d01d50SMike Stump Ty = llvm::PointerType::get(Ty, 0); 49397d01d50SMike Stump V = Builder.CreateBitCast(V, Ty); 49497d01d50SMike Stump V = Builder.CreateLoad(V, false); 49597d01d50SMike Stump V = Builder.CreateStructGEP(V, 1, "forwarding"); 49697d01d50SMike Stump V = Builder.CreateLoad(V, false); 49797d01d50SMike Stump V = Builder.CreateBitCast(V, PtrStructTy); 49897d01d50SMike Stump V = Builder.CreateStructGEP(V, needsCopyDispose*2 + 4, "x"); 49997d01d50SMike Stump } else { 50097d01d50SMike Stump Ty = llvm::PointerType::get(Ty, 0); 50197d01d50SMike Stump V = Builder.CreateBitCast(V, Ty); 50297d01d50SMike Stump } 50397d01d50SMike Stump return V; 50497d01d50SMike Stump } 50597d01d50SMike Stump 506692c6e37SMike Stump void CodeGenFunction::BlockForwardSelf() { 507692c6e37SMike Stump const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); 508692c6e37SMike Stump ImplicitParamDecl *SelfDecl = OMD->getSelfDecl(); 509692c6e37SMike Stump llvm::Value *&DMEntry = LocalDeclMap[SelfDecl]; 510692c6e37SMike Stump if (DMEntry) 511692c6e37SMike Stump return; 512692c6e37SMike Stump // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care 513692c6e37SMike Stump BlockDeclRefExpr *BDRE = new (getContext()) 514692c6e37SMike Stump BlockDeclRefExpr(SelfDecl, 515692c6e37SMike Stump SelfDecl->getType(), SourceLocation(), false); 516692c6e37SMike Stump DMEntry = GetAddrOfBlockDecl(BDRE); 517692c6e37SMike Stump } 518692c6e37SMike Stump 5192d5a2878SMike Stump llvm::Constant * 5206c39666aSMike Stump BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) { 5216a60fa24SAnders Carlsson // Generate the block descriptor. 5226a60fa24SAnders Carlsson const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy); 52392bbd6d4SMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 52492bbd6d4SMike Stump getTypes().ConvertType(getContext().IntTy)); 5256a60fa24SAnders Carlsson 5266a60fa24SAnders Carlsson llvm::Constant *DescriptorFields[2]; 5276a60fa24SAnders Carlsson 5286a60fa24SAnders Carlsson // Reserved 5296a60fa24SAnders Carlsson DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy); 5306a60fa24SAnders Carlsson 5316a60fa24SAnders Carlsson // Block literal size. For global blocks we just use the size of the generic 5326a60fa24SAnders Carlsson // block literal struct. 5336a60fa24SAnders Carlsson uint64_t BlockLiteralSize = 534005c9a62SMike Stump TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8; 5356a60fa24SAnders Carlsson DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize); 5366a60fa24SAnders Carlsson 5376a60fa24SAnders Carlsson llvm::Constant *DescriptorStruct = 5386a60fa24SAnders Carlsson llvm::ConstantStruct::get(&DescriptorFields[0], 2); 5396a60fa24SAnders Carlsson 5406a60fa24SAnders Carlsson llvm::GlobalVariable *Descriptor = 5416a60fa24SAnders Carlsson new llvm::GlobalVariable(DescriptorStruct->getType(), true, 5426a60fa24SAnders Carlsson llvm::GlobalVariable::InternalLinkage, 5436a60fa24SAnders Carlsson DescriptorStruct, "__block_descriptor_global", 5446a60fa24SAnders Carlsson &getModule()); 5456a60fa24SAnders Carlsson 5466a60fa24SAnders Carlsson // Generate the constants for the block literal. 5476a60fa24SAnders Carlsson llvm::Constant *LiteralFields[5]; 5486a60fa24SAnders Carlsson 5492d5a2878SMike Stump CodeGenFunction::BlockInfo Info(0, n); 550b750d928SMike Stump uint64_t subBlockSize, subBlockAlign; 5511db7d04bSMike Stump llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; 552a61bb4d1SDaniel Dunbar bool subBlockHasCopyDispose = false; 5535469f29aSMike Stump llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap; 554cb2fbcb0SMike Stump llvm::Function *Fn 555692c6e37SMike Stump = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap, 5565469f29aSMike Stump subBlockSize, 557b750d928SMike Stump subBlockAlign, 5584446dcf0SMike Stump subBlockDeclRefDecls, 5594446dcf0SMike Stump subBlockHasCopyDispose); 560cb2fbcb0SMike Stump assert(subBlockSize == BlockLiteralSize 561cb2fbcb0SMike Stump && "no imports allowed for global block"); 562a61bb4d1SDaniel Dunbar assert(!subBlockHasCopyDispose && "no imports allowed for global block"); 5636a60fa24SAnders Carlsson 5646a60fa24SAnders Carlsson // isa 565971f9b66SMike Stump LiteralFields[0] = getNSConcreteGlobalBlock(); 5666a60fa24SAnders Carlsson 5676a60fa24SAnders Carlsson // Flags 5685878c797SAnders Carlsson LiteralFields[1] = 5695878c797SAnders Carlsson llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR); 5706a60fa24SAnders Carlsson 5716a60fa24SAnders Carlsson // Reserved 57292bbd6d4SMike Stump LiteralFields[2] = llvm::Constant::getNullValue(IntTy); 5736a60fa24SAnders Carlsson 5746a60fa24SAnders Carlsson // Function 5756a60fa24SAnders Carlsson LiteralFields[3] = Fn; 5766a60fa24SAnders Carlsson 5776a60fa24SAnders Carlsson // Descriptor 5786a60fa24SAnders Carlsson LiteralFields[4] = Descriptor; 5796a60fa24SAnders Carlsson 5806a60fa24SAnders Carlsson llvm::Constant *BlockLiteralStruct = 5816a60fa24SAnders Carlsson llvm::ConstantStruct::get(&LiteralFields[0], 5); 5826a60fa24SAnders Carlsson 5836a60fa24SAnders Carlsson llvm::GlobalVariable *BlockLiteral = 5846a60fa24SAnders Carlsson new llvm::GlobalVariable(BlockLiteralStruct->getType(), true, 5856a60fa24SAnders Carlsson llvm::GlobalVariable::InternalLinkage, 5866a60fa24SAnders Carlsson BlockLiteralStruct, "__block_literal_global", 5876a60fa24SAnders Carlsson &getModule()); 5886a60fa24SAnders Carlsson 5896a60fa24SAnders Carlsson return BlockLiteral; 5906a60fa24SAnders Carlsson } 5916a60fa24SAnders Carlsson 592cb2fbcb0SMike Stump llvm::Value *CodeGenFunction::LoadBlockStruct() { 593cb2fbcb0SMike Stump return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self"); 594cb2fbcb0SMike Stump } 595cb2fbcb0SMike Stump 5964446dcf0SMike Stump llvm::Function * 5974446dcf0SMike Stump CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr, 598cb2fbcb0SMike Stump const BlockInfo& Info, 599692c6e37SMike Stump const Decl *OuterFuncDecl, 6005469f29aSMike Stump llvm::DenseMap<const Decl*, llvm::Value*> ldm, 601b750d928SMike Stump uint64_t &Size, 602b750d928SMike Stump uint64_t &Align, 6034446dcf0SMike Stump llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls, 6044446dcf0SMike Stump bool &subBlockHasCopyDispose) { 6055469f29aSMike Stump // Arrange for local static and local extern declarations to appear 6065469f29aSMike Stump // to be local to this function as well, as they are directly referenced 6075469f29aSMike Stump // in a block. 6085469f29aSMike Stump for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin(); 6095469f29aSMike Stump i != ldm.end(); 6105469f29aSMike Stump ++i) { 6115469f29aSMike Stump const VarDecl *VD = dyn_cast<VarDecl>(i->first); 6125469f29aSMike Stump 6135469f29aSMike Stump if (VD->getStorageClass() == VarDecl::Static 6145469f29aSMike Stump || VD->getStorageClass() == VarDecl::Extern) 6155469f29aSMike Stump LocalDeclMap[VD] = i->second; 6165469f29aSMike Stump } 6175469f29aSMike Stump 61809a9b6e3SEli Friedman // FIXME: We need to rearrange the code for copy/dispose so we have this 61909a9b6e3SEli Friedman // sooner, so we can calculate offsets correctly. 62009a9b6e3SEli Friedman if (!BlockHasCopyDispose) 62109a9b6e3SEli Friedman BlockOffset = CGM.getTargetData() 62209a9b6e3SEli Friedman .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8; 62309a9b6e3SEli Friedman else 62409a9b6e3SEli Friedman BlockOffset = CGM.getTargetData() 62509a9b6e3SEli Friedman .getTypeStoreSizeInBits(CGM.getGenericExtendedBlockLiteralType()) / 8; 62609a9b6e3SEli Friedman BlockAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 62709a9b6e3SEli Friedman 628deaad8ccSDouglas Gregor const FunctionProtoType *FTy = 6293385fe12SChris Lattner cast<FunctionProtoType>(BExpr->getFunctionType()); 6306a60fa24SAnders Carlsson 6316a60fa24SAnders Carlsson FunctionArgList Args; 6326a60fa24SAnders Carlsson 6333385fe12SChris Lattner const BlockDecl *BD = BExpr->getBlockDecl(); 6346a60fa24SAnders Carlsson 6356a60fa24SAnders Carlsson // FIXME: This leaks 6366a60fa24SAnders Carlsson ImplicitParamDecl *SelfDecl = 6376a60fa24SAnders Carlsson ImplicitParamDecl::Create(getContext(), 0, 6386a60fa24SAnders Carlsson SourceLocation(), 0, 6396a60fa24SAnders Carlsson getContext().getPointerType(getContext().VoidTy)); 6406a60fa24SAnders Carlsson 6416a60fa24SAnders Carlsson Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType())); 642cb2fbcb0SMike Stump BlockStructDecl = SelfDecl; 6436a60fa24SAnders Carlsson 644c4b30e59SSteve Naroff for (BlockDecl::param_const_iterator i = BD->param_begin(), 6456a60fa24SAnders Carlsson e = BD->param_end(); i != e; ++i) 646eecd39f2SMike Stump Args.push_back(std::make_pair(*i, (*i)->getType())); 6476a60fa24SAnders Carlsson 6486a60fa24SAnders Carlsson const CGFunctionInfo &FI = 6496a60fa24SAnders Carlsson CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args); 6506a60fa24SAnders Carlsson 6512d5a2878SMike Stump std::string Name = std::string("__") + Info.Name + "_block_invoke_"; 6526a60fa24SAnders Carlsson CodeGenTypes &Types = CGM.getTypes(); 6536a60fa24SAnders Carlsson const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic()); 6546a60fa24SAnders Carlsson 6556a60fa24SAnders Carlsson llvm::Function *Fn = 6566a60fa24SAnders Carlsson llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 6576a60fa24SAnders Carlsson Name, 6586a60fa24SAnders Carlsson &CGM.getModule()); 6596a60fa24SAnders Carlsson 6606a60fa24SAnders Carlsson StartFunction(BD, FTy->getResultType(), Fn, Args, 6613385fe12SChris Lattner BExpr->getBody()->getLocEnd()); 662692c6e37SMike Stump CurFuncDecl = OuterFuncDecl; 6633385fe12SChris Lattner EmitStmt(BExpr->getBody()); 6643385fe12SChris Lattner FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc()); 6656a60fa24SAnders Carlsson 666b750d928SMike Stump // The runtime needs a minimum alignment of a void *. 667b750d928SMike Stump uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 668b750d928SMike Stump BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign); 669b750d928SMike Stump 670cb2fbcb0SMike Stump Size = BlockOffset; 671b750d928SMike Stump Align = BlockAlign; 672b750d928SMike Stump subBlockDeclRefDecls = BlockDeclRefDecls; 6734446dcf0SMike Stump subBlockHasCopyDispose |= BlockHasCopyDispose; 6746a60fa24SAnders Carlsson return Fn; 6756a60fa24SAnders Carlsson } 6761db7d04bSMike Stump 677aeb0ffd5SMike Stump uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) { 6781db7d04bSMike Stump const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl()); 6791db7d04bSMike Stump 6801db7d04bSMike Stump uint64_t Size = getContext().getTypeSize(D->getType()) / 8; 6811db7d04bSMike Stump uint64_t Align = getContext().getDeclAlignInBytes(D); 6821db7d04bSMike Stump 6831db7d04bSMike Stump if (BDRE->isByRef()) { 6841db7d04bSMike Stump Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8; 6851db7d04bSMike Stump Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; 6861db7d04bSMike Stump } 6871db7d04bSMike Stump 6881db7d04bSMike Stump assert ((Align > 0) && "alignment must be 1 byte or more"); 6891db7d04bSMike Stump 6901db7d04bSMike Stump uint64_t OldOffset = BlockOffset; 6911db7d04bSMike Stump 6921db7d04bSMike Stump // Ensure proper alignment, even if it means we have to have a gap 6931db7d04bSMike Stump BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align); 6941db7d04bSMike Stump BlockAlign = std::max(Align, BlockAlign); 6951db7d04bSMike Stump 6961db7d04bSMike Stump uint64_t Pad = BlockOffset - OldOffset; 6971db7d04bSMike Stump if (Pad) { 6981db7d04bSMike Stump llvm::ArrayType::get(llvm::Type::Int8Ty, Pad); 6991db7d04bSMike Stump QualType PadTy = getContext().getConstantArrayType(getContext().CharTy, 7001db7d04bSMike Stump llvm::APInt(32, Pad), 7011db7d04bSMike Stump ArrayType::Normal, 0); 7021db7d04bSMike Stump ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(), 7031db7d04bSMike Stump 0, QualType(PadTy), VarDecl::None, 7041db7d04bSMike Stump SourceLocation()); 7051db7d04bSMike Stump Expr *E; 7061db7d04bSMike Stump E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(), 7071db7d04bSMike Stump SourceLocation(), false, false); 7081db7d04bSMike Stump BlockDeclRefDecls.push_back(E); 7091db7d04bSMike Stump } 7101db7d04bSMike Stump BlockDeclRefDecls.push_back(BDRE); 7111db7d04bSMike Stump 7121db7d04bSMike Stump BlockOffset += Size; 7131db7d04bSMike Stump return BlockOffset-Size; 7141db7d04bSMike Stump } 71597d01d50SMike Stump 716aeb0ffd5SMike Stump llvm::Constant *BlockFunction:: 717aeb0ffd5SMike Stump GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T, 718aeb0ffd5SMike Stump std::vector<HelperInfo> &NoteForHelper) { 7190c743277SMike Stump QualType R = getContext().VoidTy; 7200c743277SMike Stump 7210c743277SMike Stump FunctionArgList Args; 7220c743277SMike Stump // FIXME: This leaks 723aeb0ffd5SMike Stump ImplicitParamDecl *Dst = 724aeb0ffd5SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 725aeb0ffd5SMike Stump getContext().getPointerType(getContext().VoidTy)); 726aeb0ffd5SMike Stump Args.push_back(std::make_pair(Dst, Dst->getType())); 7270c743277SMike Stump ImplicitParamDecl *Src = 7280c743277SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 7290c743277SMike Stump getContext().getPointerType(getContext().VoidTy)); 7300c743277SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 7310c743277SMike Stump 7320c743277SMike Stump const CGFunctionInfo &FI = 7330c743277SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 7340c743277SMike Stump 7350c743277SMike Stump std::string Name = std::string("__copy_helper_block_"); 7360c743277SMike Stump CodeGenTypes &Types = CGM.getTypes(); 7370c743277SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 7380c743277SMike Stump 7390c743277SMike Stump llvm::Function *Fn = 7400c743277SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 7410c743277SMike Stump Name, 7420c743277SMike Stump &CGM.getModule()); 7430c743277SMike Stump 7440c743277SMike Stump IdentifierInfo *II 7450c743277SMike Stump = &CGM.getContext().Idents.get("__copy_helper_block_"); 7460c743277SMike Stump 7470c743277SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 7480c743277SMike Stump getContext().getTranslationUnitDecl(), 7490c743277SMike Stump SourceLocation(), II, R, 7500c743277SMike Stump FunctionDecl::Static, false, 7510c743277SMike Stump true); 7520c743277SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 753aeb0ffd5SMike Stump 754aeb0ffd5SMike Stump llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); 755aeb0ffd5SMike Stump llvm::Type *PtrPtrT; 756aeb0ffd5SMike Stump PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 757aeb0ffd5SMike Stump SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); 758aeb0ffd5SMike Stump SrcObj = Builder.CreateLoad(SrcObj); 759aeb0ffd5SMike Stump 760aeb0ffd5SMike Stump llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst); 761aeb0ffd5SMike Stump DstObj = Builder.CreateBitCast(DstObj, llvm::PointerType::get(T, 0)); 762aeb0ffd5SMike Stump 763aeb0ffd5SMike Stump for (unsigned i=0; i < NoteForHelper.size(); ++i) { 764aeb0ffd5SMike Stump int flag = NoteForHelper[i].flag; 765aeb0ffd5SMike Stump int index = NoteForHelper[i].index; 766aeb0ffd5SMike Stump 767aeb0ffd5SMike Stump if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) 768aeb0ffd5SMike Stump || NoteForHelper[i].RequiresCopying) { 769aeb0ffd5SMike Stump llvm::Value *Srcv = SrcObj; 770aeb0ffd5SMike Stump Srcv = Builder.CreateStructGEP(Srcv, index); 771aeb0ffd5SMike Stump Srcv = Builder.CreateBitCast(Srcv, 772aeb0ffd5SMike Stump llvm::PointerType::get(PtrToInt8Ty, 0)); 773aeb0ffd5SMike Stump Srcv = Builder.CreateLoad(Srcv); 774aeb0ffd5SMike Stump 775aeb0ffd5SMike Stump llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index); 776aeb0ffd5SMike Stump Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty); 777aeb0ffd5SMike Stump 778aeb0ffd5SMike Stump llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag); 779aeb0ffd5SMike Stump llvm::Value *F = getBlockObjectAssign(); 780aeb0ffd5SMike Stump Builder.CreateCall3(F, Dstv, Srcv, N); 781aeb0ffd5SMike Stump } 782aeb0ffd5SMike Stump } 783aeb0ffd5SMike Stump 7840c743277SMike Stump CGF.FinishFunction(); 7850c743277SMike Stump 7860c743277SMike Stump return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 78797d01d50SMike Stump } 78897d01d50SMike Stump 789d6ef62f0SMike Stump llvm::Constant *BlockFunction:: 790aeb0ffd5SMike Stump GenerateDestroyHelperFunction(bool BlockHasCopyDispose, 791aeb0ffd5SMike Stump const llvm::StructType* T, 792aeb0ffd5SMike Stump std::vector<HelperInfo> &NoteForHelper) { 7930c743277SMike Stump QualType R = getContext().VoidTy; 7940c743277SMike Stump 7950c743277SMike Stump FunctionArgList Args; 7960c743277SMike Stump // FIXME: This leaks 7970c743277SMike Stump ImplicitParamDecl *Src = 7980c743277SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 7990c743277SMike Stump getContext().getPointerType(getContext().VoidTy)); 8000c743277SMike Stump 8010c743277SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 8020c743277SMike Stump 8030c743277SMike Stump const CGFunctionInfo &FI = 8040c743277SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 8050c743277SMike Stump 8060c743277SMike Stump std::string Name = std::string("__destroy_helper_block_"); 8070c743277SMike Stump CodeGenTypes &Types = CGM.getTypes(); 8080c743277SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 8090c743277SMike Stump 8100c743277SMike Stump llvm::Function *Fn = 8110c743277SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 8120c743277SMike Stump Name, 8130c743277SMike Stump &CGM.getModule()); 8140c743277SMike Stump 8150c743277SMike Stump IdentifierInfo *II 8160c743277SMike Stump = &CGM.getContext().Idents.get("__destroy_helper_block_"); 8170c743277SMike Stump 8180c743277SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 8190c743277SMike Stump getContext().getTranslationUnitDecl(), 8200c743277SMike Stump SourceLocation(), II, R, 8210c743277SMike Stump FunctionDecl::Static, false, 8220c743277SMike Stump true); 8230c743277SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 8246f7d9f87SMike Stump 8256f7d9f87SMike Stump llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); 8266f7d9f87SMike Stump llvm::Type *PtrPtrT; 8276f7d9f87SMike Stump PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 8286f7d9f87SMike Stump SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); 8296f7d9f87SMike Stump SrcObj = Builder.CreateLoad(SrcObj); 8306f7d9f87SMike Stump 8316f7d9f87SMike Stump for (unsigned i=0; i < NoteForHelper.size(); ++i) { 8326f7d9f87SMike Stump int flag = NoteForHelper[i].flag; 8336f7d9f87SMike Stump int index = NoteForHelper[i].index; 8346f7d9f87SMike Stump 8356f7d9f87SMike Stump if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) 8366f7d9f87SMike Stump || NoteForHelper[i].RequiresCopying) { 8376f7d9f87SMike Stump llvm::Value *Srcv = SrcObj; 8386f7d9f87SMike Stump Srcv = Builder.CreateStructGEP(Srcv, index); 8396f7d9f87SMike Stump Srcv = Builder.CreateBitCast(Srcv, 8406f7d9f87SMike Stump llvm::PointerType::get(PtrToInt8Ty, 0)); 8416f7d9f87SMike Stump Srcv = Builder.CreateLoad(Srcv); 8426f7d9f87SMike Stump 8436f7d9f87SMike Stump BuildBlockRelease(Srcv, flag); 8446f7d9f87SMike Stump } 8456f7d9f87SMike Stump } 8466f7d9f87SMike Stump 8470c743277SMike Stump CGF.FinishFunction(); 8480c743277SMike Stump 8490c743277SMike Stump return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 8500c743277SMike Stump } 8510c743277SMike Stump 852aeb0ffd5SMike Stump llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T, 853aeb0ffd5SMike Stump std::vector<HelperInfo> &NoteForHelper) { 854aeb0ffd5SMike Stump return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose, 855aeb0ffd5SMike Stump T, NoteForHelper); 8560c743277SMike Stump } 8570c743277SMike Stump 858aeb0ffd5SMike Stump llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T, 859aeb0ffd5SMike Stump std::vector<HelperInfo> &NoteForHelper) { 860aeb0ffd5SMike Stump return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose, 861aeb0ffd5SMike Stump T, NoteForHelper); 86297d01d50SMike Stump } 863626aecc4SMike Stump 864f89230d9SMike Stump llvm::Constant *BlockFunction:: 865f89230d9SMike Stump GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) { 866ee2a5ee5SMike Stump QualType R = getContext().VoidTy; 867ee2a5ee5SMike Stump 868ee2a5ee5SMike Stump FunctionArgList Args; 869ee2a5ee5SMike Stump // FIXME: This leaks 870f89230d9SMike Stump ImplicitParamDecl *Dst = 871f89230d9SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 872f89230d9SMike Stump getContext().getPointerType(getContext().VoidTy)); 873f89230d9SMike Stump Args.push_back(std::make_pair(Dst, Dst->getType())); 874f89230d9SMike Stump 875f89230d9SMike Stump // FIXME: This leaks 876ee2a5ee5SMike Stump ImplicitParamDecl *Src = 877ee2a5ee5SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 878ee2a5ee5SMike Stump getContext().getPointerType(getContext().VoidTy)); 879ee2a5ee5SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 880ee2a5ee5SMike Stump 881ee2a5ee5SMike Stump const CGFunctionInfo &FI = 882ee2a5ee5SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 883ee2a5ee5SMike Stump 884ee2a5ee5SMike Stump std::string Name = std::string("__Block_byref_id_object_copy_"); 885ee2a5ee5SMike Stump CodeGenTypes &Types = CGM.getTypes(); 886ee2a5ee5SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 887ee2a5ee5SMike Stump 888ee2a5ee5SMike Stump llvm::Function *Fn = 889ee2a5ee5SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 890ee2a5ee5SMike Stump Name, 891ee2a5ee5SMike Stump &CGM.getModule()); 892ee2a5ee5SMike Stump 893ee2a5ee5SMike Stump IdentifierInfo *II 894ee2a5ee5SMike Stump = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_"); 895ee2a5ee5SMike Stump 896ee2a5ee5SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 897ee2a5ee5SMike Stump getContext().getTranslationUnitDecl(), 898ee2a5ee5SMike Stump SourceLocation(), II, R, 899ee2a5ee5SMike Stump FunctionDecl::Static, false, 900ee2a5ee5SMike Stump true); 901ee2a5ee5SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 902f89230d9SMike Stump 903f89230d9SMike Stump // dst->x 904f89230d9SMike Stump llvm::Value *V = CGF.GetAddrOfLocalVar(Dst); 905f89230d9SMike Stump V = Builder.CreateBitCast(V, T); 906f89230d9SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 907f89230d9SMike Stump llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty); 908f89230d9SMike Stump 909f89230d9SMike Stump // src->x 910f89230d9SMike Stump V = CGF.GetAddrOfLocalVar(Src); 911f89230d9SMike Stump V = Builder.CreateLoad(V); 912f89230d9SMike Stump V = Builder.CreateBitCast(V, T); 913f89230d9SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 914f89230d9SMike Stump V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); 915f89230d9SMike Stump llvm::Value *SrcObj = Builder.CreateLoad(V); 916f89230d9SMike Stump 917f89230d9SMike Stump flag |= BLOCK_BYREF_CALLER; 918f89230d9SMike Stump 919f89230d9SMike Stump llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag); 920f89230d9SMike Stump llvm::Value *F = getBlockObjectAssign(); 921f89230d9SMike Stump Builder.CreateCall3(F, DstObj, SrcObj, N); 922f89230d9SMike Stump 923ee2a5ee5SMike Stump CGF.FinishFunction(); 924ee2a5ee5SMike Stump 925ee2a5ee5SMike Stump return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 926ee2a5ee5SMike Stump } 927ee2a5ee5SMike Stump 928fbe25dd1SMike Stump llvm::Constant * 929fbe25dd1SMike Stump BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T, 930fbe25dd1SMike Stump int flag) { 931ee2a5ee5SMike Stump QualType R = getContext().VoidTy; 932ee2a5ee5SMike Stump 933ee2a5ee5SMike Stump FunctionArgList Args; 934ee2a5ee5SMike Stump // FIXME: This leaks 935ee2a5ee5SMike Stump ImplicitParamDecl *Src = 936ee2a5ee5SMike Stump ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 937ee2a5ee5SMike Stump getContext().getPointerType(getContext().VoidTy)); 938ee2a5ee5SMike Stump 939ee2a5ee5SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 940ee2a5ee5SMike Stump 941ee2a5ee5SMike Stump const CGFunctionInfo &FI = 942ee2a5ee5SMike Stump CGM.getTypes().getFunctionInfo(R, Args); 943ee2a5ee5SMike Stump 944ee2a5ee5SMike Stump std::string Name = std::string("__Block_byref_id_object_dispose_"); 945ee2a5ee5SMike Stump CodeGenTypes &Types = CGM.getTypes(); 946ee2a5ee5SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 947ee2a5ee5SMike Stump 948ee2a5ee5SMike Stump llvm::Function *Fn = 949ee2a5ee5SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 950ee2a5ee5SMike Stump Name, 951ee2a5ee5SMike Stump &CGM.getModule()); 952ee2a5ee5SMike Stump 953ee2a5ee5SMike Stump IdentifierInfo *II 954ee2a5ee5SMike Stump = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_"); 955ee2a5ee5SMike Stump 956ee2a5ee5SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 957ee2a5ee5SMike Stump getContext().getTranslationUnitDecl(), 958ee2a5ee5SMike Stump SourceLocation(), II, R, 959ee2a5ee5SMike Stump FunctionDecl::Static, false, 960ee2a5ee5SMike Stump true); 961ee2a5ee5SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 962fbe25dd1SMike Stump 963fbe25dd1SMike Stump llvm::Value *V = CGF.GetAddrOfLocalVar(Src); 964fbe25dd1SMike Stump V = Builder.CreateBitCast(V, T); 965fbe25dd1SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 966fbe25dd1SMike Stump V = Builder.CreateBitCast(V, PtrToInt8Ty); 967fbe25dd1SMike Stump 968fbe25dd1SMike Stump flag |= BLOCK_BYREF_CALLER; 969fbe25dd1SMike Stump BuildBlockRelease(V, flag); 970ee2a5ee5SMike Stump CGF.FinishFunction(); 971ee2a5ee5SMike Stump 972ee2a5ee5SMike Stump return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 973ee2a5ee5SMike Stump } 974ee2a5ee5SMike Stump 975f89230d9SMike Stump llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T, 976f89230d9SMike Stump int flag) { 977f89230d9SMike Stump return CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag); 978ee2a5ee5SMike Stump } 979ee2a5ee5SMike Stump 980fbe25dd1SMike Stump llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T, 981fbe25dd1SMike Stump int flag) { 982fbe25dd1SMike Stump return CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag); 983ee2a5ee5SMike Stump } 984ee2a5ee5SMike Stump 985626aecc4SMike Stump llvm::Value *BlockFunction::getBlockObjectDispose() { 986626aecc4SMike Stump if (CGM.BlockObjectDispose == 0) { 987626aecc4SMike Stump const llvm::FunctionType *FTy; 988626aecc4SMike Stump std::vector<const llvm::Type*> ArgTys; 989626aecc4SMike Stump const llvm::Type *ResultType = llvm::Type::VoidTy; 990626aecc4SMike Stump ArgTys.push_back(PtrToInt8Ty); 991626aecc4SMike Stump ArgTys.push_back(llvm::Type::Int32Ty); 992626aecc4SMike Stump FTy = llvm::FunctionType::get(ResultType, ArgTys, false); 993626aecc4SMike Stump CGM.BlockObjectDispose 994626aecc4SMike Stump = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose"); 995626aecc4SMike Stump } 996626aecc4SMike Stump return CGM.BlockObjectDispose; 997626aecc4SMike Stump } 998626aecc4SMike Stump 999f89230d9SMike Stump llvm::Value *BlockFunction::getBlockObjectAssign() { 1000f89230d9SMike Stump if (CGM.BlockObjectAssign == 0) { 1001f89230d9SMike Stump const llvm::FunctionType *FTy; 1002f89230d9SMike Stump std::vector<const llvm::Type*> ArgTys; 1003f89230d9SMike Stump const llvm::Type *ResultType = llvm::Type::VoidTy; 1004f89230d9SMike Stump ArgTys.push_back(PtrToInt8Ty); 1005f89230d9SMike Stump ArgTys.push_back(PtrToInt8Ty); 1006f89230d9SMike Stump ArgTys.push_back(llvm::Type::Int32Ty); 1007f89230d9SMike Stump FTy = llvm::FunctionType::get(ResultType, ArgTys, false); 1008f89230d9SMike Stump CGM.BlockObjectAssign 1009f89230d9SMike Stump = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign"); 1010f89230d9SMike Stump } 1011f89230d9SMike Stump return CGM.BlockObjectAssign; 1012f89230d9SMike Stump } 1013f89230d9SMike Stump 1014fbe25dd1SMike Stump void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) { 1015626aecc4SMike Stump llvm::Value *F = getBlockObjectDispose(); 1016fbe25dd1SMike Stump llvm::Value *N; 1017626aecc4SMike Stump V = Builder.CreateBitCast(V, PtrToInt8Ty); 1018fbe25dd1SMike Stump N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag); 1019626aecc4SMike Stump Builder.CreateCall2(F, V, N); 1020626aecc4SMike Stump } 10214446dcf0SMike Stump 10224446dcf0SMike Stump ASTContext &BlockFunction::getContext() const { return CGM.getContext(); } 1023aeb0ffd5SMike Stump 1024aeb0ffd5SMike Stump BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf, 1025aeb0ffd5SMike Stump CGBuilderTy &B) 1026aeb0ffd5SMike Stump : CGM(cgm), CGF(cgf), Builder(B) { 1027aeb0ffd5SMike Stump PtrToInt8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 1028aeb0ffd5SMike Stump 1029aeb0ffd5SMike Stump BlockHasCopyDispose = false; 1030aeb0ffd5SMike Stump } 1031