12437cbfaSAnders Carlsson //===--- CGBlocks.cpp - Emit LLVM Code for declarations -------------------===// 22437cbfaSAnders Carlsson // 32437cbfaSAnders Carlsson // The LLVM Compiler Infrastructure 42437cbfaSAnders Carlsson // 52437cbfaSAnders Carlsson // This file is distributed under the University of Illinois Open Source 62437cbfaSAnders Carlsson // License. See LICENSE.TXT for details. 72437cbfaSAnders Carlsson // 82437cbfaSAnders Carlsson //===----------------------------------------------------------------------===// 92437cbfaSAnders Carlsson // 102437cbfaSAnders Carlsson // This contains code to emit blocks. 112437cbfaSAnders Carlsson // 122437cbfaSAnders Carlsson //===----------------------------------------------------------------------===// 132437cbfaSAnders Carlsson 142e722b91SMike Stump #include "CGDebugInfo.h" 152437cbfaSAnders Carlsson #include "CodeGenFunction.h" 16*989908fbSFariborz Jahanian #include "CGObjCRuntime.h" 172437cbfaSAnders Carlsson #include "CodeGenModule.h" 18692c6e37SMike Stump #include "clang/AST/DeclObjC.h" 192437cbfaSAnders Carlsson #include "llvm/Module.h" 206a60fa24SAnders Carlsson #include "llvm/Target/TargetData.h" 212437cbfaSAnders Carlsson #include <algorithm> 22db714924STorok Edwin 232437cbfaSAnders Carlsson using namespace clang; 242437cbfaSAnders Carlsson using namespace CodeGen; 252437cbfaSAnders Carlsson 26d6ef62f0SMike Stump llvm::Constant *CodeGenFunction:: 2740775003SKen Dyck BuildDescriptorBlockDecl(bool BlockHasCopyDispose, CharUnits Size, 28d2142cffSMike Stump const llvm::StructType* Ty, 29aeb0ffd5SMike Stump std::vector<HelperInfo> *NoteForHelper) { 30c2c38331SMike Stump const llvm::Type *UnsignedLongTy 31c2c38331SMike Stump = CGM.getTypes().ConvertType(getContext().UnsignedLongTy); 3285284bacSMike Stump llvm::Constant *C; 3385284bacSMike Stump std::vector<llvm::Constant*> Elts; 3485284bacSMike Stump 3585284bacSMike Stump // reserved 36b7a2fe6fSOwen Anderson C = llvm::ConstantInt::get(UnsignedLongTy, 0); 3785284bacSMike Stump Elts.push_back(C); 3885284bacSMike Stump 3985284bacSMike Stump // Size 402ac40a9fSMike Stump // FIXME: What is the right way to say this doesn't fit? We should give 412ac40a9fSMike Stump // a user diagnostic in that case. Better fix would be to change the 422ac40a9fSMike Stump // API to size_t. 4340775003SKen Dyck C = llvm::ConstantInt::get(UnsignedLongTy, Size.getQuantity()); 4485284bacSMike Stump Elts.push_back(C); 4585284bacSMike Stump 4685284bacSMike Stump if (BlockHasCopyDispose) { 4785284bacSMike Stump // copy_func_helper_decl 4867645930SMike Stump Elts.push_back(BuildCopyHelper(Ty, NoteForHelper)); 4985284bacSMike Stump 5085284bacSMike Stump // destroy_func_decl 5167645930SMike Stump Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper)); 5285284bacSMike Stump } 5385284bacSMike Stump 5441eaf0acSNick Lewycky C = llvm::ConstantStruct::get(VMContext, Elts, false); 5585284bacSMike Stump 56c10c8d3eSOwen Anderson C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, 5785284bacSMike Stump llvm::GlobalValue::InternalLinkage, 58c10c8d3eSOwen Anderson C, "__block_descriptor_tmp"); 5985284bacSMike Stump return C; 6085284bacSMike Stump } 6185284bacSMike Stump 6295435674SMike Stump llvm::Constant *BlockModule::getNSConcreteGlobalBlock() { 6346813bbfSChris Lattner if (NSConcreteGlobalBlock == 0) 6446813bbfSChris Lattner NSConcreteGlobalBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty, 6546813bbfSChris Lattner "_NSConcreteGlobalBlock"); 66971f9b66SMike Stump return NSConcreteGlobalBlock; 67971f9b66SMike Stump } 68971f9b66SMike Stump 6995435674SMike Stump llvm::Constant *BlockModule::getNSConcreteStackBlock() { 7046813bbfSChris Lattner if (NSConcreteStackBlock == 0) 7146813bbfSChris Lattner NSConcreteStackBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty, 7246813bbfSChris Lattner "_NSConcreteStackBlock"); 737ab278dbSMike Stump return NSConcreteStackBlock; 747ab278dbSMike Stump } 757ab278dbSMike Stump 76066b6166SMike Stump static void CollectBlockDeclRefInfo( 77066b6166SMike Stump const Stmt *S, CodeGenFunction::BlockInfo &Info, 78066b6166SMike Stump llvm::SmallSet<const DeclContext *, 16> &InnerContexts) { 79ed5e69feSAnders Carlsson for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); 80ed5e69feSAnders Carlsson I != E; ++I) 81dd942713SDaniel Dunbar if (*I) 82066b6166SMike Stump CollectBlockDeclRefInfo(*I, Info, InnerContexts); 83ed5e69feSAnders Carlsson 847fe9cc1dSMike Stump // We want to ensure we walk down into block literals so we can find 857fe9cc1dSMike Stump // all nested BlockDeclRefExprs. 86066b6166SMike Stump if (const BlockExpr *BE = dyn_cast<BlockExpr>(S)) { 87066b6166SMike Stump InnerContexts.insert(cast<DeclContext>(BE->getBlockDecl())); 88066b6166SMike Stump CollectBlockDeclRefInfo(BE->getBody(), Info, InnerContexts); 89066b6166SMike Stump } 907fe9cc1dSMike Stump 91066b6166SMike Stump if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) { 92ed5e69feSAnders Carlsson // FIXME: Handle enums. 93066b6166SMike Stump if (isa<FunctionDecl>(BDRE->getDecl())) 94ed5e69feSAnders Carlsson return; 95ed5e69feSAnders Carlsson 96066b6166SMike Stump // Only Decls that escape are added. 97066b6166SMike Stump if (!InnerContexts.count(BDRE->getDecl()->getDeclContext())) 98066b6166SMike Stump Info.DeclRefs.push_back(BDRE); 99ed5e69feSAnders Carlsson } 100ed5e69feSAnders Carlsson } 101ed5e69feSAnders Carlsson 1022d33d630SMike Stump /// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be 1032d33d630SMike Stump /// declared as a global variable instead of on the stack. 10446813bbfSChris Lattner static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info) { 1057fe9cc1dSMike Stump return Info.DeclRefs.empty(); 1067fe9cc1dSMike Stump } 1077fe9cc1dSMike Stump 1087fe9cc1dSMike Stump /// AllocateAllBlockDeclRefs - Preallocate all nested BlockDeclRefExprs to 1097fe9cc1dSMike Stump /// ensure we can generate the debug information for the parameter for the block 1107fe9cc1dSMike Stump /// invoke function. 1117fe9cc1dSMike Stump static void AllocateAllBlockDeclRefs(const CodeGenFunction::BlockInfo &Info, 1127fe9cc1dSMike Stump CodeGenFunction *CGF) { 1137fe9cc1dSMike Stump // Always allocate self, as it is often handy in the debugger, even if there 1147fe9cc1dSMike Stump // is no codegen in the block that uses it. This is also useful to always do 1157fe9cc1dSMike Stump // this as if we didn't, we'd have to figure out all code that uses a self 1167fe9cc1dSMike Stump // pointer, including implicit uses. 1177fe9cc1dSMike Stump if (const ObjCMethodDecl *OMD 1187fe9cc1dSMike Stump = dyn_cast_or_null<ObjCMethodDecl>(CGF->CurFuncDecl)) { 1197fe9cc1dSMike Stump ImplicitParamDecl *SelfDecl = OMD->getSelfDecl(); 1207fe9cc1dSMike Stump BlockDeclRefExpr *BDRE = new (CGF->getContext()) 1217fe9cc1dSMike Stump BlockDeclRefExpr(SelfDecl, 1227fe9cc1dSMike Stump SelfDecl->getType(), SourceLocation(), false); 1237fe9cc1dSMike Stump CGF->AllocateBlockDecl(BDRE); 1247fe9cc1dSMike Stump } 1257fe9cc1dSMike Stump 1267fe9cc1dSMike Stump // FIXME: Also always forward the this pointer in C++ as well. 1277fe9cc1dSMike Stump 1287fe9cc1dSMike Stump for (size_t i = 0; i < Info.DeclRefs.size(); ++i) 1297fe9cc1dSMike Stump CGF->AllocateBlockDecl(Info.DeclRefs[i]); 130ed5e69feSAnders Carlsson } 131ed5e69feSAnders Carlsson 1322d33d630SMike Stump // FIXME: Push most into CGM, passing down a few bits, like current function 1332d33d630SMike Stump // name. 134b750d928SMike Stump llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) { 135ed5e69feSAnders Carlsson 136ed5e69feSAnders Carlsson std::string Name = CurFn->getName(); 137ed5e69feSAnders Carlsson CodeGenFunction::BlockInfo Info(0, Name.c_str()); 138066b6166SMike Stump llvm::SmallSet<const DeclContext *, 16> InnerContexts; 139066b6166SMike Stump InnerContexts.insert(BE->getBlockDecl()); 140066b6166SMike Stump CollectBlockDeclRefInfo(BE->getBody(), Info, InnerContexts); 141ed5e69feSAnders Carlsson 142ed5e69feSAnders Carlsson // Check if the block can be global. 1432d33d630SMike Stump // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like 1442d33d630SMike Stump // to just have one code path. We should move this function into CGM and pass 1452d33d630SMike Stump // CGF, then we can just check to see if CGF is 0. 14697d01d50SMike Stump if (0 && CanBlockBeGlobal(Info)) 147ed5e69feSAnders Carlsson return CGM.GetAddrOfGlobalBlock(BE, Name.c_str()); 14885284bacSMike Stump 149950a9518SDavid Chisnall size_t BlockFields = 5; 150950a9518SDavid Chisnall 151950a9518SDavid Chisnall bool hasIntrospection = CGM.getContext().getLangOptions().BlockIntrospection; 152950a9518SDavid Chisnall 153950a9518SDavid Chisnall if (hasIntrospection) { 154950a9518SDavid Chisnall BlockFields++; 155950a9518SDavid Chisnall } 156950a9518SDavid Chisnall std::vector<llvm::Constant*> Elts(BlockFields); 157950a9518SDavid Chisnall 158950a9518SDavid Chisnall if (hasIntrospection) { 159950a9518SDavid Chisnall std::string BlockTypeEncoding; 160950a9518SDavid Chisnall CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding); 161950a9518SDavid Chisnall 162950a9518SDavid Chisnall Elts[5] = llvm::ConstantExpr::getBitCast( 163950a9518SDavid Chisnall CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty); 164950a9518SDavid Chisnall } 165950a9518SDavid Chisnall 16685284bacSMike Stump llvm::Constant *C; 167b750d928SMike Stump llvm::Value *V; 16885284bacSMike Stump 16985284bacSMike Stump { 17085284bacSMike Stump // C = BuildBlockStructInitlist(); 17185284bacSMike Stump unsigned int flags = BLOCK_HAS_DESCRIPTOR; 17285284bacSMike Stump 173950a9518SDavid Chisnall if (hasIntrospection) 174950a9518SDavid Chisnall flags |= BLOCK_HAS_OBJC_TYPE; 175950a9518SDavid Chisnall 1764446dcf0SMike Stump // We run this first so that we set BlockHasCopyDispose from the entire 1774446dcf0SMike Stump // block literal. 1784446dcf0SMike Stump // __invoke 17940775003SKen Dyck CharUnits subBlockSize; 1805262b119SKen Dyck CharUnits subBlockAlign; 1814446dcf0SMike Stump llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; 182d2142cffSMike Stump bool subBlockHasCopyDispose = false; 1834446dcf0SMike Stump llvm::Function *Fn 184d2a3e617SMike Stump = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl, 185d2a3e617SMike Stump LocalDeclMap, 1865469f29aSMike Stump subBlockSize, 1874446dcf0SMike Stump subBlockAlign, 1884446dcf0SMike Stump subBlockDeclRefDecls, 189d2142cffSMike Stump subBlockHasCopyDispose); 190d2142cffSMike Stump BlockHasCopyDispose |= subBlockHasCopyDispose; 1914446dcf0SMike Stump Elts[3] = Fn; 1924446dcf0SMike Stump 19318bb9284SMike Stump // FIXME: Don't use BlockHasCopyDispose, it is set more often then 19418bb9284SMike Stump // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); } 195d2142cffSMike Stump if (subBlockHasCopyDispose) 19685284bacSMike Stump flags |= BLOCK_HAS_COPY_DISPOSE; 19785284bacSMike Stump 198499ae7ecSMike Stump // __isa 1997ab278dbSMike Stump C = CGM.getNSConcreteStackBlock(); 200ade90fd1SOwen Anderson C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty); 2014446dcf0SMike Stump Elts[0] = C; 20285284bacSMike Stump 20385284bacSMike Stump // __flags 20485284bacSMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 20585284bacSMike Stump CGM.getTypes().ConvertType(CGM.getContext().IntTy)); 206b7a2fe6fSOwen Anderson C = llvm::ConstantInt::get(IntTy, flags); 2074446dcf0SMike Stump Elts[1] = C; 20885284bacSMike Stump 20985284bacSMike Stump // __reserved 210b7a2fe6fSOwen Anderson C = llvm::ConstantInt::get(IntTy, 0); 2114446dcf0SMike Stump Elts[2] = C; 21285284bacSMike Stump 213b750d928SMike Stump if (subBlockDeclRefDecls.size() == 0) { 214d6ef62f0SMike Stump // __descriptor 2157fe9cc1dSMike Stump Elts[4] = BuildDescriptorBlockDecl(subBlockHasCopyDispose, subBlockSize, 2167fe9cc1dSMike Stump 0, 0); 217d6ef62f0SMike Stump 2183a139f37SMike Stump // Optimize to being a global block. 2193a139f37SMike Stump Elts[0] = CGM.getNSConcreteGlobalBlock(); 220b7a2fe6fSOwen Anderson Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL); 2213a139f37SMike Stump 22241eaf0acSNick Lewycky C = llvm::ConstantStruct::get(VMContext, Elts, false); 22385284bacSMike Stump 224c10c8d3eSOwen Anderson C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, 225d6b28fc6SBenjamin Kramer llvm::GlobalValue::InternalLinkage, C, 226d6b28fc6SBenjamin Kramer "__block_holder_tmp_" + 227d6b28fc6SBenjamin Kramer llvm::Twine(CGM.getGlobalUniqueCount())); 2285d2534adSMike Stump QualType BPT = BE->getType(); 229ade90fd1SOwen Anderson C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT)); 23085284bacSMike Stump return C; 23185284bacSMike Stump } 23285284bacSMike Stump 233950a9518SDavid Chisnall std::vector<const llvm::Type *> Types(BlockFields+subBlockDeclRefDecls.size()); 234aeb0ffd5SMike Stump for (int i=0; i<4; ++i) 235b750d928SMike Stump Types[i] = Elts[i]->getType(); 236aeb0ffd5SMike Stump Types[4] = PtrToInt8Ty; 237950a9518SDavid Chisnall if (hasIntrospection) 238950a9518SDavid Chisnall Types[5] = PtrToInt8Ty; 23985284bacSMike Stump 2401db7d04bSMike Stump for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) { 2411db7d04bSMike Stump const Expr *E = subBlockDeclRefDecls[i]; 2421db7d04bSMike Stump const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); 2431db7d04bSMike Stump QualType Ty = E->getType(); 24497d01d50SMike Stump if (BDRE && BDRE->isByRef()) { 245950a9518SDavid Chisnall Types[i+BlockFields] = llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0); 24697d01d50SMike Stump } else 247950a9518SDavid Chisnall Types[i+BlockFields] = ConvertType(Ty); 2481db7d04bSMike Stump } 249b750d928SMike Stump 250758428f4SOwen Anderson llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true); 251b750d928SMike Stump 252b750d928SMike Stump llvm::AllocaInst *A = CreateTempAlloca(Ty); 2535262b119SKen Dyck A->setAlignment(subBlockAlign.getQuantity()); 254b750d928SMike Stump V = A; 255b750d928SMike Stump 256aeb0ffd5SMike Stump std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size()); 257aeb0ffd5SMike Stump int helpersize = 0; 258aeb0ffd5SMike Stump 259aeb0ffd5SMike Stump for (unsigned i=0; i<4; ++i) 260b750d928SMike Stump Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp")); 261950a9518SDavid Chisnall if (hasIntrospection) 262950a9518SDavid Chisnall Builder.CreateStore(Elts[5], Builder.CreateStructGEP(V, 5, "block.tmp")); 263b750d928SMike Stump 264b750d928SMike Stump for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) 265b750d928SMike Stump { 2661db7d04bSMike Stump // FIXME: Push const down. 2671db7d04bSMike Stump Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]); 2681db7d04bSMike Stump DeclRefExpr *DR; 2691db7d04bSMike Stump ValueDecl *VD; 270b750d928SMike Stump 2711db7d04bSMike Stump DR = dyn_cast<DeclRefExpr>(E); 2721db7d04bSMike Stump // Skip padding. 2731db7d04bSMike Stump if (DR) continue; 2741db7d04bSMike Stump 2751db7d04bSMike Stump BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); 2761db7d04bSMike Stump VD = BDRE->getDecl(); 2771db7d04bSMike Stump 278950a9518SDavid Chisnall llvm::Value* Addr = Builder.CreateStructGEP(V, i+BlockFields, "tmp"); 279aeb0ffd5SMike Stump NoteForHelper[helpersize].index = i+5; 2802114d7c2SMike Stump NoteForHelper[helpersize].RequiresCopying 2812114d7c2SMike Stump = BlockRequiresCopying(VD->getType()); 282aeb0ffd5SMike Stump NoteForHelper[helpersize].flag 2832114d7c2SMike Stump = (VD->getType()->isBlockPointerType() 2842114d7c2SMike Stump ? BLOCK_FIELD_IS_BLOCK 2852114d7c2SMike Stump : BLOCK_FIELD_IS_OBJECT); 286aeb0ffd5SMike Stump 2871db7d04bSMike Stump if (LocalDeclMap[VD]) { 28897d01d50SMike Stump if (BDRE->isByRef()) { 289aeb0ffd5SMike Stump NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF | 29090d8daf5SMike Stump // FIXME: Someone double check this. 29190d8daf5SMike Stump (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0); 29297d01d50SMike Stump llvm::Value *Loc = LocalDeclMap[VD]; 29397d01d50SMike Stump Loc = Builder.CreateStructGEP(Loc, 1, "forwarding"); 294c76493a6SDaniel Dunbar Loc = Builder.CreateLoad(Loc); 29597d01d50SMike Stump Builder.CreateStore(Loc, Addr); 296aeb0ffd5SMike Stump ++helpersize; 29797d01d50SMike Stump continue; 29897d01d50SMike Stump } else 299ce54657eSJohn McCall E = new (getContext()) DeclRefExpr (VD, 300ed6c7440SDouglas Gregor VD->getType(), 301ed6c7440SDouglas Gregor SourceLocation()); 3021db7d04bSMike Stump } 30397d01d50SMike Stump if (BDRE->isByRef()) { 304efd7caa8SMike Stump NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF | 305efd7caa8SMike Stump // FIXME: Someone double check this. 306efd7caa8SMike Stump (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0); 3071db7d04bSMike Stump E = new (getContext()) 3081db7d04bSMike Stump UnaryOperator(E, UnaryOperator::AddrOf, 3091db7d04bSMike Stump getContext().getPointerType(E->getType()), 3101db7d04bSMike Stump SourceLocation()); 31197d01d50SMike Stump } 312aeb0ffd5SMike Stump ++helpersize; 3131db7d04bSMike Stump 3141db7d04bSMike Stump RValue r = EmitAnyExpr(E, Addr, false); 31597d01d50SMike Stump if (r.isScalar()) { 31697d01d50SMike Stump llvm::Value *Loc = r.getScalarVal(); 317950a9518SDavid Chisnall const llvm::Type *Ty = Types[i+BlockFields]; 31897d01d50SMike Stump if (BDRE->isByRef()) { 3192d33d630SMike Stump // E is now the address of the value field, instead, we want the 3202d33d630SMike Stump // address of the actual ByRef struct. We optimize this slightly 3212d33d630SMike Stump // compared to gcc by not grabbing the forwarding slot as this must 3222d33d630SMike Stump // be done during Block_copy for us, and we can postpone the work 3232d33d630SMike Stump // until then. 32440775003SKen Dyck CharUnits offset = BlockDecls[BDRE->getDecl()]; 3252d33d630SMike Stump 3262d33d630SMike Stump llvm::Value *BlockLiteral = LoadBlockStruct(); 3272d33d630SMike Stump 3282d33d630SMike Stump Loc = Builder.CreateGEP(BlockLiteral, 32941a75027SOwen Anderson llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), 33040775003SKen Dyck offset.getQuantity()), 3312d33d630SMike Stump "block.literal"); 3329793f0e4SOwen Anderson Ty = llvm::PointerType::get(Ty, 0); 33397d01d50SMike Stump Loc = Builder.CreateBitCast(Loc, Ty); 334c76493a6SDaniel Dunbar Loc = Builder.CreateLoad(Loc); 3352d33d630SMike Stump // Loc = Builder.CreateBitCast(Loc, Ty); 33697d01d50SMike Stump } 33797d01d50SMike Stump Builder.CreateStore(Loc, Addr); 33897d01d50SMike Stump } else if (r.isComplex()) 339b750d928SMike Stump // FIXME: implement 340b750d928SMike Stump ErrorUnsupported(BE, "complex in block literal"); 341b750d928SMike Stump else if (r.isAggregate()) 342b750d928SMike Stump ; // Already created into the destination 343b750d928SMike Stump else 344b750d928SMike Stump assert (0 && "bad block variable"); 345b750d928SMike Stump // FIXME: Ensure that the offset created by the backend for 346b750d928SMike Stump // the struct matches the previously computed offset in BlockDecls. 347b750d928SMike Stump } 348aeb0ffd5SMike Stump NoteForHelper.resize(helpersize); 349d6ef62f0SMike Stump 350d6ef62f0SMike Stump // __descriptor 351d2142cffSMike Stump llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockHasCopyDispose, 352d2142cffSMike Stump subBlockSize, Ty, 353aeb0ffd5SMike Stump &NoteForHelper); 354aeb0ffd5SMike Stump Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty); 355aeb0ffd5SMike Stump Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp")); 356b750d928SMike Stump } 357b750d928SMike Stump 358b750d928SMike Stump QualType BPT = BE->getType(); 359*989908fbSFariborz Jahanian V = Builder.CreateBitCast(V, ConvertType(BPT)); 360*989908fbSFariborz Jahanian // See if this is a __weak block variable and the must call objc_read_weak 361*989908fbSFariborz Jahanian // on it. 362*989908fbSFariborz Jahanian const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>(); 363*989908fbSFariborz Jahanian QualType RES = ftype->getResultType(); 364*989908fbSFariborz Jahanian if (RES.isObjCGCWeak()) { 365*989908fbSFariborz Jahanian // Must cast argument to id* 366*989908fbSFariborz Jahanian const llvm::Type *ObjectPtrTy = 367*989908fbSFariborz Jahanian ConvertType(CGM.getContext().getObjCIdType()); 368*989908fbSFariborz Jahanian const llvm::Type *PtrObjectPtrTy = 369*989908fbSFariborz Jahanian llvm::PointerType::getUnqual(ObjectPtrTy); 370*989908fbSFariborz Jahanian V = Builder.CreateBitCast(V, PtrObjectPtrTy); 371*989908fbSFariborz Jahanian V = CGM.getObjCRuntime().EmitObjCWeakRead(*this, V); 372*989908fbSFariborz Jahanian } 373*989908fbSFariborz Jahanian return V; 374b750d928SMike Stump } 37585284bacSMike Stump 37685284bacSMike Stump 37795435674SMike Stump const llvm::Type *BlockModule::getBlockDescriptorType() { 378650c932dSMike Stump if (BlockDescriptorType) 379650c932dSMike Stump return BlockDescriptorType; 3802437cbfaSAnders Carlsson 3812437cbfaSAnders Carlsson const llvm::Type *UnsignedLongTy = 382650c932dSMike Stump getTypes().ConvertType(getContext().UnsignedLongTy); 3832437cbfaSAnders Carlsson 3842437cbfaSAnders Carlsson // struct __block_descriptor { 3852437cbfaSAnders Carlsson // unsigned long reserved; 3862437cbfaSAnders Carlsson // unsigned long block_size; 3872437cbfaSAnders Carlsson // }; 388758428f4SOwen Anderson BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(), 389758428f4SOwen Anderson UnsignedLongTy, 3902437cbfaSAnders Carlsson UnsignedLongTy, 3912437cbfaSAnders Carlsson NULL); 3922437cbfaSAnders Carlsson 393650c932dSMike Stump getModule().addTypeName("struct.__block_descriptor", 394650c932dSMike Stump BlockDescriptorType); 3952437cbfaSAnders Carlsson 396650c932dSMike Stump return BlockDescriptorType; 3972437cbfaSAnders Carlsson } 3982437cbfaSAnders Carlsson 39995435674SMike Stump const llvm::Type *BlockModule::getGenericBlockLiteralType() { 400005c9a62SMike Stump if (GenericBlockLiteralType) 401005c9a62SMike Stump return GenericBlockLiteralType; 4022437cbfaSAnders Carlsson 4032437cbfaSAnders Carlsson const llvm::Type *BlockDescPtrTy = 4049793f0e4SOwen Anderson llvm::PointerType::getUnqual(getBlockDescriptorType()); 4052437cbfaSAnders Carlsson 40692bbd6d4SMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 40792bbd6d4SMike Stump getTypes().ConvertType(getContext().IntTy)); 40892bbd6d4SMike Stump 4092437cbfaSAnders Carlsson // struct __block_literal_generic { 4105d2534adSMike Stump // void *__isa; 4115d2534adSMike Stump // int __flags; 4125d2534adSMike Stump // int __reserved; 4135d2534adSMike Stump // void (*__invoke)(void *); 4145d2534adSMike Stump // struct __block_descriptor *__descriptor; 415950a9518SDavid Chisnall // // GNU runtime only: 416950a9518SDavid Chisnall // const char *types; 4172437cbfaSAnders Carlsson // }; 418950a9518SDavid Chisnall if (CGM.getContext().getLangOptions().BlockIntrospection) 419950a9518SDavid Chisnall GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(), 420950a9518SDavid Chisnall PtrToInt8Ty, 421950a9518SDavid Chisnall IntTy, 422950a9518SDavid Chisnall IntTy, 423950a9518SDavid Chisnall PtrToInt8Ty, 424950a9518SDavid Chisnall BlockDescPtrTy, 425950a9518SDavid Chisnall PtrToInt8Ty, 426950a9518SDavid Chisnall NULL); 427950a9518SDavid Chisnall else 428758428f4SOwen Anderson GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(), 429758428f4SOwen Anderson PtrToInt8Ty, 43092bbd6d4SMike Stump IntTy, 43192bbd6d4SMike Stump IntTy, 432626aecc4SMike Stump PtrToInt8Ty, 4332437cbfaSAnders Carlsson BlockDescPtrTy, 4342437cbfaSAnders Carlsson NULL); 4352437cbfaSAnders Carlsson 436005c9a62SMike Stump getModule().addTypeName("struct.__block_literal_generic", 437005c9a62SMike Stump GenericBlockLiteralType); 4382437cbfaSAnders Carlsson 439005c9a62SMike Stump return GenericBlockLiteralType; 4402437cbfaSAnders Carlsson } 4412437cbfaSAnders Carlsson 44295435674SMike Stump const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() { 4435d2534adSMike Stump if (GenericExtendedBlockLiteralType) 4445d2534adSMike Stump return GenericExtendedBlockLiteralType; 4455d2534adSMike Stump 4465d2534adSMike Stump const llvm::Type *BlockDescPtrTy = 4479793f0e4SOwen Anderson llvm::PointerType::getUnqual(getBlockDescriptorType()); 4485d2534adSMike Stump 4495d2534adSMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 4505d2534adSMike Stump getTypes().ConvertType(getContext().IntTy)); 4515d2534adSMike Stump 4525d2534adSMike Stump // struct __block_literal_generic { 4535d2534adSMike Stump // void *__isa; 4545d2534adSMike Stump // int __flags; 4555d2534adSMike Stump // int __reserved; 4565d2534adSMike Stump // void (*__invoke)(void *); 4575d2534adSMike Stump // struct __block_descriptor *__descriptor; 4585d2534adSMike Stump // void *__copy_func_helper_decl; 4595d2534adSMike Stump // void *__destroy_func_decl; 4605d2534adSMike Stump // }; 461758428f4SOwen Anderson GenericExtendedBlockLiteralType = llvm::StructType::get(IntTy->getContext(), 462758428f4SOwen Anderson PtrToInt8Ty, 4635d2534adSMike Stump IntTy, 4645d2534adSMike Stump IntTy, 465626aecc4SMike Stump PtrToInt8Ty, 4665d2534adSMike Stump BlockDescPtrTy, 467626aecc4SMike Stump PtrToInt8Ty, 468626aecc4SMike Stump PtrToInt8Ty, 4695d2534adSMike Stump NULL); 4705d2534adSMike Stump 4715d2534adSMike Stump getModule().addTypeName("struct.__block_literal_extended_generic", 4725d2534adSMike Stump GenericExtendedBlockLiteralType); 4735d2534adSMike Stump 4745d2534adSMike Stump return GenericExtendedBlockLiteralType; 4755d2534adSMike Stump } 4765d2534adSMike Stump 477bfb3671bSAnders Carlsson RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E, 478bfb3671bSAnders Carlsson ReturnValueSlot ReturnValue) { 4792437cbfaSAnders Carlsson const BlockPointerType *BPT = 480c23c7e6aSTed Kremenek E->getCallee()->getType()->getAs<BlockPointerType>(); 4812437cbfaSAnders Carlsson 4822437cbfaSAnders Carlsson llvm::Value *Callee = EmitScalarExpr(E->getCallee()); 4832437cbfaSAnders Carlsson 4842437cbfaSAnders Carlsson // Get a pointer to the generic block literal. 4852437cbfaSAnders Carlsson const llvm::Type *BlockLiteralTy = 4869793f0e4SOwen Anderson llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType()); 4872437cbfaSAnders Carlsson 4882437cbfaSAnders Carlsson // Bitcast the callee to a block literal. 4892437cbfaSAnders Carlsson llvm::Value *BlockLiteral = 4902437cbfaSAnders Carlsson Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal"); 4912437cbfaSAnders Carlsson 4922437cbfaSAnders Carlsson // Get the function pointer from the literal. 4932437cbfaSAnders Carlsson llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp"); 4942437cbfaSAnders Carlsson 4952437cbfaSAnders Carlsson BlockLiteral = 4962437cbfaSAnders Carlsson Builder.CreateBitCast(BlockLiteral, 497abd5b90eSBenjamin Kramer llvm::Type::getInt8PtrTy(VMContext), 4982437cbfaSAnders Carlsson "tmp"); 4992437cbfaSAnders Carlsson 5002437cbfaSAnders Carlsson // Add the block literal. 5012437cbfaSAnders Carlsson QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy); 5022437cbfaSAnders Carlsson CallArgList Args; 5032437cbfaSAnders Carlsson Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy)); 5042437cbfaSAnders Carlsson 505479e6fc7SAnders Carlsson QualType FnType = BPT->getPointeeType(); 506479e6fc7SAnders Carlsson 5072437cbfaSAnders Carlsson // And the rest of the arguments. 5089dd450bbSJohn McCall EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(), 509479e6fc7SAnders Carlsson E->arg_begin(), E->arg_end()); 5102437cbfaSAnders Carlsson 5115f50c651SAnders Carlsson // Load the function. 512c76493a6SDaniel Dunbar llvm::Value *Func = Builder.CreateLoad(FuncPtr, "tmp"); 5135f50c651SAnders Carlsson 514ab26cfa5SJohn McCall const FunctionType *FuncTy = FnType->getAs<FunctionType>(); 515ab26cfa5SJohn McCall QualType ResultType = FuncTy->getResultType(); 516a60cbcdfSAnders Carlsson 517a60cbcdfSAnders Carlsson const CGFunctionInfo &FnInfo = 518ab26cfa5SJohn McCall CGM.getTypes().getFunctionInfo(ResultType, Args, FuncTy->getCallConv(), 519ab26cfa5SJohn McCall FuncTy->getNoReturnAttr()); 5205f50c651SAnders Carlsson 5215f50c651SAnders Carlsson // Cast the function pointer to the right type. 5225f50c651SAnders Carlsson const llvm::Type *BlockFTy = 523a60cbcdfSAnders Carlsson CGM.getTypes().GetFunctionType(FnInfo, false); 5245f50c651SAnders Carlsson 5259793f0e4SOwen Anderson const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy); 5265f50c651SAnders Carlsson Func = Builder.CreateBitCast(Func, BlockFTyPtr); 5275f50c651SAnders Carlsson 5282437cbfaSAnders Carlsson // And call the block. 529bfb3671bSAnders Carlsson return EmitCall(FnInfo, Func, ReturnValue, Args); 5302437cbfaSAnders Carlsson } 5316a60fa24SAnders Carlsson 53240775003SKen Dyck CharUnits CodeGenFunction::AllocateBlockDecl(const BlockDeclRefExpr *E) { 5330168f4b2SAnders Carlsson const ValueDecl *VD = E->getDecl(); 53440775003SKen Dyck CharUnits &offset = BlockDecls[VD]; 5350168f4b2SAnders Carlsson 53697d01d50SMike Stump // See if we have already allocated an offset for this variable. 53740775003SKen Dyck if (offset.isPositive()) 5387fe9cc1dSMike Stump return offset; 5397fe9cc1dSMike Stump 5404446dcf0SMike Stump // Don't run the expensive check, unless we have to. 541e1b19ba0SMike Stump if (!BlockHasCopyDispose) 542e1b19ba0SMike Stump if (E->isByRef() 543e1b19ba0SMike Stump || BlockRequiresCopying(E->getType())) 5444446dcf0SMike Stump BlockHasCopyDispose = true; 5457fe9cc1dSMike Stump 54697d01d50SMike Stump // if not, allocate one now. 54797d01d50SMike Stump offset = getBlockOffset(E); 5487fe9cc1dSMike Stump 5497fe9cc1dSMike Stump return offset; 55097d01d50SMike Stump } 55197d01d50SMike Stump 5527fe9cc1dSMike Stump llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) { 5537fe9cc1dSMike Stump const ValueDecl *VD = E->getDecl(); 55440775003SKen Dyck CharUnits offset = AllocateBlockDecl(E); 5557fe9cc1dSMike Stump 5567fe9cc1dSMike Stump 55797d01d50SMike Stump llvm::Value *BlockLiteral = LoadBlockStruct(); 55897d01d50SMike Stump llvm::Value *V = Builder.CreateGEP(BlockLiteral, 55941a75027SOwen Anderson llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), 56040775003SKen Dyck offset.getQuantity()), 5612d33d630SMike Stump "block.literal"); 56297d01d50SMike Stump if (E->isByRef()) { 56397d01d50SMike Stump const llvm::Type *PtrStructTy 5640168f4b2SAnders Carlsson = llvm::PointerType::get(BuildByRefType(VD), 0); 565efd7caa8SMike Stump // The block literal will need a copy/destroy helper. 566efd7caa8SMike Stump BlockHasCopyDispose = true; 5670168f4b2SAnders Carlsson 5680168f4b2SAnders Carlsson const llvm::Type *Ty = PtrStructTy; 5699793f0e4SOwen Anderson Ty = llvm::PointerType::get(Ty, 0); 57097d01d50SMike Stump V = Builder.CreateBitCast(V, Ty); 571c76493a6SDaniel Dunbar V = Builder.CreateLoad(V); 57297d01d50SMike Stump V = Builder.CreateStructGEP(V, 1, "forwarding"); 573c76493a6SDaniel Dunbar V = Builder.CreateLoad(V); 57497d01d50SMike Stump V = Builder.CreateBitCast(V, PtrStructTy); 5750168f4b2SAnders Carlsson V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD), 5760168f4b2SAnders Carlsson VD->getNameAsString()); 57797d01d50SMike Stump } else { 5780168f4b2SAnders Carlsson const llvm::Type *Ty = CGM.getTypes().ConvertType(VD->getType()); 5790168f4b2SAnders Carlsson 5809793f0e4SOwen Anderson Ty = llvm::PointerType::get(Ty, 0); 58197d01d50SMike Stump V = Builder.CreateBitCast(V, Ty); 58297d01d50SMike Stump } 58397d01d50SMike Stump return V; 58497d01d50SMike Stump } 58597d01d50SMike Stump 586692c6e37SMike Stump void CodeGenFunction::BlockForwardSelf() { 587692c6e37SMike Stump const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); 588692c6e37SMike Stump ImplicitParamDecl *SelfDecl = OMD->getSelfDecl(); 589692c6e37SMike Stump llvm::Value *&DMEntry = LocalDeclMap[SelfDecl]; 590692c6e37SMike Stump if (DMEntry) 591692c6e37SMike Stump return; 592692c6e37SMike Stump // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care 593692c6e37SMike Stump BlockDeclRefExpr *BDRE = new (getContext()) 594692c6e37SMike Stump BlockDeclRefExpr(SelfDecl, 595692c6e37SMike Stump SelfDecl->getType(), SourceLocation(), false); 596692c6e37SMike Stump DMEntry = GetAddrOfBlockDecl(BDRE); 597692c6e37SMike Stump } 598692c6e37SMike Stump 5992d5a2878SMike Stump llvm::Constant * 6006c39666aSMike Stump BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) { 6016a60fa24SAnders Carlsson // Generate the block descriptor. 6026a60fa24SAnders Carlsson const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy); 60392bbd6d4SMike Stump const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( 60492bbd6d4SMike Stump getTypes().ConvertType(getContext().IntTy)); 6056a60fa24SAnders Carlsson 6066a60fa24SAnders Carlsson llvm::Constant *DescriptorFields[2]; 6076a60fa24SAnders Carlsson 6086a60fa24SAnders Carlsson // Reserved 6090b75f23bSOwen Anderson DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy); 6106a60fa24SAnders Carlsson 6116a60fa24SAnders Carlsson // Block literal size. For global blocks we just use the size of the generic 6126a60fa24SAnders Carlsson // block literal struct. 61398ca7943SKen Dyck CharUnits BlockLiteralSize = 61498ca7943SKen Dyck CGM.GetTargetTypeStoreSize(getGenericBlockLiteralType()); 615170229f6SOwen Anderson DescriptorFields[1] = 61640775003SKen Dyck llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize.getQuantity()); 6176a60fa24SAnders Carlsson 6186a60fa24SAnders Carlsson llvm::Constant *DescriptorStruct = 61941eaf0acSNick Lewycky llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 2, false); 6206a60fa24SAnders Carlsson 6216a60fa24SAnders Carlsson llvm::GlobalVariable *Descriptor = 622c10c8d3eSOwen Anderson new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true, 6236a60fa24SAnders Carlsson llvm::GlobalVariable::InternalLinkage, 624c10c8d3eSOwen Anderson DescriptorStruct, "__block_descriptor_global"); 6256a60fa24SAnders Carlsson 626950a9518SDavid Chisnall int FieldCount = 5; 6276a60fa24SAnders Carlsson // Generate the constants for the block literal. 628950a9518SDavid Chisnall if (CGM.getContext().getLangOptions().BlockIntrospection) 629950a9518SDavid Chisnall FieldCount = 6; 630950a9518SDavid Chisnall 631950a9518SDavid Chisnall std::vector<llvm::Constant*> LiteralFields(FieldCount); 6326a60fa24SAnders Carlsson 6332d5a2878SMike Stump CodeGenFunction::BlockInfo Info(0, n); 63440775003SKen Dyck CharUnits subBlockSize; 6355262b119SKen Dyck CharUnits subBlockAlign; 6361db7d04bSMike Stump llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; 637a61bb4d1SDaniel Dunbar bool subBlockHasCopyDispose = false; 6385469f29aSMike Stump llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap; 639cb2fbcb0SMike Stump llvm::Function *Fn 640692c6e37SMike Stump = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap, 6415469f29aSMike Stump subBlockSize, 642b750d928SMike Stump subBlockAlign, 6434446dcf0SMike Stump subBlockDeclRefDecls, 6444446dcf0SMike Stump subBlockHasCopyDispose); 645cb2fbcb0SMike Stump assert(subBlockSize == BlockLiteralSize 646cb2fbcb0SMike Stump && "no imports allowed for global block"); 6476a60fa24SAnders Carlsson 6486a60fa24SAnders Carlsson // isa 649971f9b66SMike Stump LiteralFields[0] = getNSConcreteGlobalBlock(); 6506a60fa24SAnders Carlsson 6516a60fa24SAnders Carlsson // Flags 652950a9518SDavid Chisnall LiteralFields[1] = CGM.getContext().getLangOptions().BlockIntrospection ? 653950a9518SDavid Chisnall llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR | 654950a9518SDavid Chisnall BLOCK_HAS_OBJC_TYPE) : 655b7a2fe6fSOwen Anderson llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR); 6566a60fa24SAnders Carlsson 6576a60fa24SAnders Carlsson // Reserved 6580b75f23bSOwen Anderson LiteralFields[2] = llvm::Constant::getNullValue(IntTy); 6596a60fa24SAnders Carlsson 6606a60fa24SAnders Carlsson // Function 6616a60fa24SAnders Carlsson LiteralFields[3] = Fn; 6626a60fa24SAnders Carlsson 6636a60fa24SAnders Carlsson // Descriptor 6646a60fa24SAnders Carlsson LiteralFields[4] = Descriptor; 6656a60fa24SAnders Carlsson 666950a9518SDavid Chisnall // Type encoding 667950a9518SDavid Chisnall if (CGM.getContext().getLangOptions().BlockIntrospection) { 668950a9518SDavid Chisnall std::string BlockTypeEncoding; 669950a9518SDavid Chisnall CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding); 670950a9518SDavid Chisnall 671950a9518SDavid Chisnall LiteralFields[5] = CGM.GetAddrOfConstantCString(BlockTypeEncoding); 672950a9518SDavid Chisnall } 673950a9518SDavid Chisnall 6746a60fa24SAnders Carlsson llvm::Constant *BlockLiteralStruct = 675950a9518SDavid Chisnall llvm::ConstantStruct::get(VMContext, LiteralFields, false); 6766a60fa24SAnders Carlsson 6776a60fa24SAnders Carlsson llvm::GlobalVariable *BlockLiteral = 678c10c8d3eSOwen Anderson new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true, 6796a60fa24SAnders Carlsson llvm::GlobalVariable::InternalLinkage, 680c10c8d3eSOwen Anderson BlockLiteralStruct, "__block_literal_global"); 6816a60fa24SAnders Carlsson 6826a60fa24SAnders Carlsson return BlockLiteral; 6836a60fa24SAnders Carlsson } 6846a60fa24SAnders Carlsson 685cb2fbcb0SMike Stump llvm::Value *CodeGenFunction::LoadBlockStruct() { 68638382028SMike Stump llvm::Value *V = Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], 68738382028SMike Stump "self"); 68838382028SMike Stump // For now, we codegen based upon byte offsets. 68938382028SMike Stump return Builder.CreateBitCast(V, PtrToInt8Ty); 690cb2fbcb0SMike Stump } 691cb2fbcb0SMike Stump 6924446dcf0SMike Stump llvm::Function * 6934446dcf0SMike Stump CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr, 694cb2fbcb0SMike Stump const BlockInfo& Info, 695692c6e37SMike Stump const Decl *OuterFuncDecl, 6965469f29aSMike Stump llvm::DenseMap<const Decl*, llvm::Value*> ldm, 69740775003SKen Dyck CharUnits &Size, 6985262b119SKen Dyck CharUnits &Align, 6994446dcf0SMike Stump llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls, 7004446dcf0SMike Stump bool &subBlockHasCopyDispose) { 7019074ed8cSDevang Patel 7029074ed8cSDevang Patel // Check if we should generate debug info for this block. 7039074ed8cSDevang Patel if (CGM.getDebugInfo()) 7049074ed8cSDevang Patel DebugInfo = CGM.getDebugInfo(); 7059074ed8cSDevang Patel 7065469f29aSMike Stump // Arrange for local static and local extern declarations to appear 7075469f29aSMike Stump // to be local to this function as well, as they are directly referenced 7085469f29aSMike Stump // in a block. 7095469f29aSMike Stump for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin(); 7105469f29aSMike Stump i != ldm.end(); 7115469f29aSMike Stump ++i) { 7125469f29aSMike Stump const VarDecl *VD = dyn_cast<VarDecl>(i->first); 7135469f29aSMike Stump 7140ca16601SDaniel Dunbar if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage()) 7155469f29aSMike Stump LocalDeclMap[VD] = i->second; 7165469f29aSMike Stump } 7175469f29aSMike Stump 71898ca7943SKen Dyck BlockOffset = 71998ca7943SKen Dyck CGM.GetTargetTypeStoreSize(CGM.getGenericBlockLiteralType()); 7205262b119SKen Dyck BlockAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy); 72109a9b6e3SEli Friedman 722aae4349dSFariborz Jahanian const FunctionType *BlockFunctionType = BExpr->getFunctionType(); 723aae4349dSFariborz Jahanian QualType ResultType; 724ab26cfa5SJohn McCall CallingConv CC = BlockFunctionType->getCallConv(); 725ab26cfa5SJohn McCall bool NoReturn = BlockFunctionType->getNoReturnAttr(); 726aae4349dSFariborz Jahanian bool IsVariadic; 727a4f6b6b1SFariborz Jahanian if (const FunctionProtoType *FTy = 728a4f6b6b1SFariborz Jahanian dyn_cast<FunctionProtoType>(BlockFunctionType)) { 729aae4349dSFariborz Jahanian ResultType = FTy->getResultType(); 730aae4349dSFariborz Jahanian IsVariadic = FTy->isVariadic(); 731658fe02dSMike Stump } else { 732aae4349dSFariborz Jahanian // K&R style block. 733aae4349dSFariborz Jahanian ResultType = BlockFunctionType->getResultType(); 734aae4349dSFariborz Jahanian IsVariadic = false; 735aae4349dSFariborz Jahanian } 7366a60fa24SAnders Carlsson 7376a60fa24SAnders Carlsson FunctionArgList Args; 7386a60fa24SAnders Carlsson 7397fe9cc1dSMike Stump CurFuncDecl = OuterFuncDecl; 7407fe9cc1dSMike Stump 7413385fe12SChris Lattner const BlockDecl *BD = BExpr->getBlockDecl(); 7426a60fa24SAnders Carlsson 7437fe9cc1dSMike Stump IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor"); 744d015328fSMike Stump 745945fec05SMike Stump // Allocate all BlockDeclRefDecls, so we can calculate the right ParmTy below. 7467fe9cc1dSMike Stump AllocateAllBlockDeclRefs(Info, this); 7477fe9cc1dSMike Stump 748e1b19ba0SMike Stump QualType ParmTy = getContext().getBlockParmType(BlockHasCopyDispose, 749e1b19ba0SMike Stump BlockDeclRefDecls); 7506a60fa24SAnders Carlsson // FIXME: This leaks 7516a60fa24SAnders Carlsson ImplicitParamDecl *SelfDecl = 7526a60fa24SAnders Carlsson ImplicitParamDecl::Create(getContext(), 0, 753d015328fSMike Stump SourceLocation(), II, 754d015328fSMike Stump ParmTy); 7556a60fa24SAnders Carlsson 7566a60fa24SAnders Carlsson Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType())); 757cb2fbcb0SMike Stump BlockStructDecl = SelfDecl; 7586a60fa24SAnders Carlsson 759c4b30e59SSteve Naroff for (BlockDecl::param_const_iterator i = BD->param_begin(), 7606a60fa24SAnders Carlsson e = BD->param_end(); i != e; ++i) 761eecd39f2SMike Stump Args.push_back(std::make_pair(*i, (*i)->getType())); 7626a60fa24SAnders Carlsson 7636a60fa24SAnders Carlsson const CGFunctionInfo &FI = 764ab26cfa5SJohn McCall CGM.getTypes().getFunctionInfo(ResultType, Args, CC, NoReturn); 7656a60fa24SAnders Carlsson 7666a60fa24SAnders Carlsson CodeGenTypes &Types = CGM.getTypes(); 767aae4349dSFariborz Jahanian const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic); 7686a60fa24SAnders Carlsson 7696a60fa24SAnders Carlsson llvm::Function *Fn = 7706a60fa24SAnders Carlsson llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 771d6b28fc6SBenjamin Kramer llvm::Twine("__") + Info.Name + "_block_invoke_", 7726a60fa24SAnders Carlsson &CGM.getModule()); 7736a60fa24SAnders Carlsson 774c3e7cff6SDaniel Dunbar CGM.SetInternalFunctionAttributes(BD, Fn, FI); 775c3e7cff6SDaniel Dunbar 776324f80b1SChris Lattner StartFunction(BD, ResultType, Fn, Args, 7773385fe12SChris Lattner BExpr->getBody()->getLocEnd()); 7782e722b91SMike Stump 779324f80b1SChris Lattner CurFuncDecl = OuterFuncDecl; 78028ec0cf8SChris Lattner CurCodeDecl = BD; 781017460abSMike Stump 782017460abSMike Stump // Save a spot to insert the debug information for all the BlockDeclRefDecls. 783017460abSMike Stump llvm::BasicBlock *entry = Builder.GetInsertBlock(); 784017460abSMike Stump llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint(); 785017460abSMike Stump --entry_ptr; 786017460abSMike Stump 7873385fe12SChris Lattner EmitStmt(BExpr->getBody()); 788017460abSMike Stump 7897d69911aSMike Stump // Remember where we were... 7907d69911aSMike Stump llvm::BasicBlock *resume = Builder.GetInsertBlock(); 791017460abSMike Stump 7927d69911aSMike Stump // Go back to the entry. 793017460abSMike Stump ++entry_ptr; 794017460abSMike Stump Builder.SetInsertPoint(entry, entry_ptr); 795017460abSMike Stump 7962e722b91SMike Stump if (CGDebugInfo *DI = getDebugInfo()) { 7972e722b91SMike Stump // Emit debug information for all the BlockDeclRefDecls. 798bf78478dSChris Lattner for (unsigned i = 0, e = BlockDeclRefDecls.size(); i != e; ++i) { 799bf78478dSChris Lattner if (const BlockDeclRefExpr *BDRE = 800bf78478dSChris Lattner dyn_cast<BlockDeclRefExpr>(BlockDeclRefDecls[i])) { 8012e722b91SMike Stump const ValueDecl *D = BDRE->getDecl(); 8022e722b91SMike Stump DI->setLocation(D->getLocation()); 8032e722b91SMike Stump DI->EmitDeclareOfBlockDeclRefVariable(BDRE, 8042e722b91SMike Stump LocalDeclMap[getBlockStructDecl()], 8052e722b91SMike Stump Builder, this); 8062e722b91SMike Stump } 8072e722b91SMike Stump } 8082e722b91SMike Stump } 8097d69911aSMike Stump // And resume where we left off. 8107d69911aSMike Stump if (resume == 0) 8117d69911aSMike Stump Builder.ClearInsertionPoint(); 8127d69911aSMike Stump else 8137d69911aSMike Stump Builder.SetInsertPoint(resume); 8142e722b91SMike Stump 8153385fe12SChris Lattner FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc()); 8166a60fa24SAnders Carlsson 817b750d928SMike Stump // The runtime needs a minimum alignment of a void *. 8185262b119SKen Dyck CharUnits MinAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy); 81940775003SKen Dyck BlockOffset = CharUnits::fromQuantity( 8205262b119SKen Dyck llvm::RoundUpToAlignment(BlockOffset.getQuantity(), 8215262b119SKen Dyck MinAlign.getQuantity())); 822b750d928SMike Stump 823cb2fbcb0SMike Stump Size = BlockOffset; 824b750d928SMike Stump Align = BlockAlign; 825b750d928SMike Stump subBlockDeclRefDecls = BlockDeclRefDecls; 8264446dcf0SMike Stump subBlockHasCopyDispose |= BlockHasCopyDispose; 8276a60fa24SAnders Carlsson return Fn; 8286a60fa24SAnders Carlsson } 8291db7d04bSMike Stump 83040775003SKen Dyck CharUnits BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) { 8311db7d04bSMike Stump const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl()); 8321db7d04bSMike Stump 83340775003SKen Dyck CharUnits Size = getContext().getTypeSizeInChars(D->getType()); 834160146ebSKen Dyck CharUnits Align = getContext().getDeclAlign(D); 8351db7d04bSMike Stump 8361db7d04bSMike Stump if (BDRE->isByRef()) { 83740775003SKen Dyck Size = getContext().getTypeSizeInChars(getContext().VoidPtrTy); 8385262b119SKen Dyck Align = getContext().getTypeAlignInChars(getContext().VoidPtrTy); 8391db7d04bSMike Stump } 8401db7d04bSMike Stump 8415262b119SKen Dyck assert ((Align.isPositive()) && "alignment must be 1 byte or more"); 8421db7d04bSMike Stump 84340775003SKen Dyck CharUnits OldOffset = BlockOffset; 8441db7d04bSMike Stump 8451db7d04bSMike Stump // Ensure proper alignment, even if it means we have to have a gap 84640775003SKen Dyck BlockOffset = CharUnits::fromQuantity( 8475262b119SKen Dyck llvm::RoundUpToAlignment(BlockOffset.getQuantity(), Align.getQuantity())); 8481db7d04bSMike Stump BlockAlign = std::max(Align, BlockAlign); 8491db7d04bSMike Stump 85040775003SKen Dyck CharUnits Pad = BlockOffset - OldOffset; 85140775003SKen Dyck if (Pad.isPositive()) { 85240775003SKen Dyck llvm::ArrayType::get(llvm::Type::getInt8Ty(VMContext), Pad.getQuantity()); 8531db7d04bSMike Stump QualType PadTy = getContext().getConstantArrayType(getContext().CharTy, 85440775003SKen Dyck llvm::APInt(32, 85540775003SKen Dyck Pad.getQuantity()), 8561db7d04bSMike Stump ArrayType::Normal, 0); 8571db7d04bSMike Stump ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(), 8586032ef1aSArgyrios Kyrtzidis 0, QualType(PadTy), 0, VarDecl::None); 8591db7d04bSMike Stump Expr *E; 8601db7d04bSMike Stump E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(), 861ed6c7440SDouglas Gregor SourceLocation()); 8621db7d04bSMike Stump BlockDeclRefDecls.push_back(E); 8631db7d04bSMike Stump } 8641db7d04bSMike Stump BlockDeclRefDecls.push_back(BDRE); 8651db7d04bSMike Stump 8661db7d04bSMike Stump BlockOffset += Size; 8671db7d04bSMike Stump return BlockOffset-Size; 8681db7d04bSMike Stump } 86997d01d50SMike Stump 870aeb0ffd5SMike Stump llvm::Constant *BlockFunction:: 871aeb0ffd5SMike Stump GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T, 87267645930SMike Stump std::vector<HelperInfo> *NoteForHelperp) { 8730c743277SMike Stump QualType R = getContext().VoidTy; 8740c743277SMike Stump 8750c743277SMike Stump FunctionArgList Args; 8760c743277SMike Stump // FIXME: This leaks 877aeb0ffd5SMike Stump ImplicitParamDecl *Dst = 8787fe9cc1dSMike Stump ImplicitParamDecl::Create(getContext(), 0, 8797fe9cc1dSMike Stump SourceLocation(), 0, 880aeb0ffd5SMike Stump getContext().getPointerType(getContext().VoidTy)); 881aeb0ffd5SMike Stump Args.push_back(std::make_pair(Dst, Dst->getType())); 8820c743277SMike Stump ImplicitParamDecl *Src = 8837fe9cc1dSMike Stump ImplicitParamDecl::Create(getContext(), 0, 8847fe9cc1dSMike Stump SourceLocation(), 0, 8850c743277SMike Stump getContext().getPointerType(getContext().VoidTy)); 8860c743277SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 8870c743277SMike Stump 8880c743277SMike Stump const CGFunctionInfo &FI = 889ab26cfa5SJohn McCall CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false); 8900c743277SMike Stump 891cbc2bcafSMike Stump // FIXME: We'd like to put these into a mergable by content, with 892cbc2bcafSMike Stump // internal linkage. 8930c743277SMike Stump CodeGenTypes &Types = CGM.getTypes(); 8940c743277SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 8950c743277SMike Stump 8960c743277SMike Stump llvm::Function *Fn = 8970c743277SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 898d6b28fc6SBenjamin Kramer "__copy_helper_block_", &CGM.getModule()); 8990c743277SMike Stump 9000c743277SMike Stump IdentifierInfo *II 9010c743277SMike Stump = &CGM.getContext().Idents.get("__copy_helper_block_"); 9020c743277SMike Stump 9030c743277SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 9040c743277SMike Stump getContext().getTranslationUnitDecl(), 90560ed5604SArgyrios Kyrtzidis SourceLocation(), II, R, 0, 9060c743277SMike Stump FunctionDecl::Static, false, 9070c743277SMike Stump true); 9080c743277SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 909aeb0ffd5SMike Stump 910aeb0ffd5SMike Stump llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); 911aeb0ffd5SMike Stump llvm::Type *PtrPtrT; 91267645930SMike Stump 91367645930SMike Stump if (NoteForHelperp) { 91467645930SMike Stump std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp; 91567645930SMike Stump 9169793f0e4SOwen Anderson PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 917aeb0ffd5SMike Stump SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); 918aeb0ffd5SMike Stump SrcObj = Builder.CreateLoad(SrcObj); 919aeb0ffd5SMike Stump 920aeb0ffd5SMike Stump llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst); 9213b65ac26SMike Stump llvm::Type *PtrPtrT; 9229793f0e4SOwen Anderson PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 9233b65ac26SMike Stump DstObj = Builder.CreateBitCast(DstObj, PtrPtrT); 9243b65ac26SMike Stump DstObj = Builder.CreateLoad(DstObj); 925aeb0ffd5SMike Stump 926aeb0ffd5SMike Stump for (unsigned i=0; i < NoteForHelper.size(); ++i) { 927aeb0ffd5SMike Stump int flag = NoteForHelper[i].flag; 928aeb0ffd5SMike Stump int index = NoteForHelper[i].index; 929aeb0ffd5SMike Stump 930aeb0ffd5SMike Stump if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) 931aeb0ffd5SMike Stump || NoteForHelper[i].RequiresCopying) { 932aeb0ffd5SMike Stump llvm::Value *Srcv = SrcObj; 933aeb0ffd5SMike Stump Srcv = Builder.CreateStructGEP(Srcv, index); 934aeb0ffd5SMike Stump Srcv = Builder.CreateBitCast(Srcv, 9359793f0e4SOwen Anderson llvm::PointerType::get(PtrToInt8Ty, 0)); 936aeb0ffd5SMike Stump Srcv = Builder.CreateLoad(Srcv); 937aeb0ffd5SMike Stump 938aeb0ffd5SMike Stump llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index); 939aeb0ffd5SMike Stump Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty); 940aeb0ffd5SMike Stump 94141a75027SOwen Anderson llvm::Value *N = llvm::ConstantInt::get( 94241a75027SOwen Anderson llvm::Type::getInt32Ty(T->getContext()), flag); 943aeb0ffd5SMike Stump llvm::Value *F = getBlockObjectAssign(); 944aeb0ffd5SMike Stump Builder.CreateCall3(F, Dstv, Srcv, N); 945aeb0ffd5SMike Stump } 946aeb0ffd5SMike Stump } 94767645930SMike Stump } 948aeb0ffd5SMike Stump 9490c743277SMike Stump CGF.FinishFunction(); 9500c743277SMike Stump 951ade90fd1SOwen Anderson return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 95297d01d50SMike Stump } 95397d01d50SMike Stump 954d6ef62f0SMike Stump llvm::Constant *BlockFunction:: 955aeb0ffd5SMike Stump GenerateDestroyHelperFunction(bool BlockHasCopyDispose, 956aeb0ffd5SMike Stump const llvm::StructType* T, 95767645930SMike Stump std::vector<HelperInfo> *NoteForHelperp) { 9580c743277SMike Stump QualType R = getContext().VoidTy; 9590c743277SMike Stump 9600c743277SMike Stump FunctionArgList Args; 9610c743277SMike Stump // FIXME: This leaks 9620c743277SMike Stump ImplicitParamDecl *Src = 9637fe9cc1dSMike Stump ImplicitParamDecl::Create(getContext(), 0, 9647fe9cc1dSMike Stump SourceLocation(), 0, 9650c743277SMike Stump getContext().getPointerType(getContext().VoidTy)); 9660c743277SMike Stump 9670c743277SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 9680c743277SMike Stump 9690c743277SMike Stump const CGFunctionInfo &FI = 970ab26cfa5SJohn McCall CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false); 9710c743277SMike Stump 972cbc2bcafSMike Stump // FIXME: We'd like to put these into a mergable by content, with 973cbc2bcafSMike Stump // internal linkage. 9740c743277SMike Stump CodeGenTypes &Types = CGM.getTypes(); 9750c743277SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 9760c743277SMike Stump 9770c743277SMike Stump llvm::Function *Fn = 9780c743277SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 979d6b28fc6SBenjamin Kramer "__destroy_helper_block_", &CGM.getModule()); 9800c743277SMike Stump 9810c743277SMike Stump IdentifierInfo *II 9820c743277SMike Stump = &CGM.getContext().Idents.get("__destroy_helper_block_"); 9830c743277SMike Stump 9840c743277SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 9850c743277SMike Stump getContext().getTranslationUnitDecl(), 98660ed5604SArgyrios Kyrtzidis SourceLocation(), II, R, 0, 9870c743277SMike Stump FunctionDecl::Static, false, 9880c743277SMike Stump true); 9890c743277SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 9906f7d9f87SMike Stump 99167645930SMike Stump if (NoteForHelperp) { 99267645930SMike Stump std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp; 99367645930SMike Stump 9946f7d9f87SMike Stump llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); 9956f7d9f87SMike Stump llvm::Type *PtrPtrT; 9969793f0e4SOwen Anderson PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); 9976f7d9f87SMike Stump SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); 9986f7d9f87SMike Stump SrcObj = Builder.CreateLoad(SrcObj); 9996f7d9f87SMike Stump 10006f7d9f87SMike Stump for (unsigned i=0; i < NoteForHelper.size(); ++i) { 10016f7d9f87SMike Stump int flag = NoteForHelper[i].flag; 10026f7d9f87SMike Stump int index = NoteForHelper[i].index; 10036f7d9f87SMike Stump 10046f7d9f87SMike Stump if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) 10056f7d9f87SMike Stump || NoteForHelper[i].RequiresCopying) { 10066f7d9f87SMike Stump llvm::Value *Srcv = SrcObj; 10076f7d9f87SMike Stump Srcv = Builder.CreateStructGEP(Srcv, index); 10086f7d9f87SMike Stump Srcv = Builder.CreateBitCast(Srcv, 10099793f0e4SOwen Anderson llvm::PointerType::get(PtrToInt8Ty, 0)); 10106f7d9f87SMike Stump Srcv = Builder.CreateLoad(Srcv); 10116f7d9f87SMike Stump 10126f7d9f87SMike Stump BuildBlockRelease(Srcv, flag); 10136f7d9f87SMike Stump } 10146f7d9f87SMike Stump } 101567645930SMike Stump } 10166f7d9f87SMike Stump 10170c743277SMike Stump CGF.FinishFunction(); 10180c743277SMike Stump 1019ade90fd1SOwen Anderson return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 10200c743277SMike Stump } 10210c743277SMike Stump 1022aeb0ffd5SMike Stump llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T, 102367645930SMike Stump std::vector<HelperInfo> *NoteForHelper) { 1024aeb0ffd5SMike Stump return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose, 1025aeb0ffd5SMike Stump T, NoteForHelper); 10260c743277SMike Stump } 10270c743277SMike Stump 1028aeb0ffd5SMike Stump llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T, 102967645930SMike Stump std::vector<HelperInfo> *NoteForHelperp) { 1030aeb0ffd5SMike Stump return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose, 103167645930SMike Stump T, NoteForHelperp); 103297d01d50SMike Stump } 1033626aecc4SMike Stump 1034f89230d9SMike Stump llvm::Constant *BlockFunction:: 1035f89230d9SMike Stump GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) { 1036ee2a5ee5SMike Stump QualType R = getContext().VoidTy; 1037ee2a5ee5SMike Stump 1038ee2a5ee5SMike Stump FunctionArgList Args; 1039ee2a5ee5SMike Stump // FIXME: This leaks 1040f89230d9SMike Stump ImplicitParamDecl *Dst = 10417fe9cc1dSMike Stump ImplicitParamDecl::Create(getContext(), 0, 10427fe9cc1dSMike Stump SourceLocation(), 0, 1043f89230d9SMike Stump getContext().getPointerType(getContext().VoidTy)); 1044f89230d9SMike Stump Args.push_back(std::make_pair(Dst, Dst->getType())); 1045f89230d9SMike Stump 1046f89230d9SMike Stump // FIXME: This leaks 1047ee2a5ee5SMike Stump ImplicitParamDecl *Src = 10487fe9cc1dSMike Stump ImplicitParamDecl::Create(getContext(), 0, 10497fe9cc1dSMike Stump SourceLocation(), 0, 1050ee2a5ee5SMike Stump getContext().getPointerType(getContext().VoidTy)); 1051ee2a5ee5SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 1052ee2a5ee5SMike Stump 1053ee2a5ee5SMike Stump const CGFunctionInfo &FI = 1054ab26cfa5SJohn McCall CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false); 1055ee2a5ee5SMike Stump 1056ee2a5ee5SMike Stump CodeGenTypes &Types = CGM.getTypes(); 1057ee2a5ee5SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 1058ee2a5ee5SMike Stump 1059cbc2bcafSMike Stump // FIXME: We'd like to put these into a mergable by content, with 1060cbc2bcafSMike Stump // internal linkage. 1061ee2a5ee5SMike Stump llvm::Function *Fn = 1062ee2a5ee5SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 1063d6b28fc6SBenjamin Kramer "__Block_byref_id_object_copy_", &CGM.getModule()); 1064ee2a5ee5SMike Stump 1065ee2a5ee5SMike Stump IdentifierInfo *II 1066ee2a5ee5SMike Stump = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_"); 1067ee2a5ee5SMike Stump 1068ee2a5ee5SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 1069ee2a5ee5SMike Stump getContext().getTranslationUnitDecl(), 107060ed5604SArgyrios Kyrtzidis SourceLocation(), II, R, 0, 1071ee2a5ee5SMike Stump FunctionDecl::Static, false, 1072ee2a5ee5SMike Stump true); 1073ee2a5ee5SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 1074f89230d9SMike Stump 1075f89230d9SMike Stump // dst->x 1076f89230d9SMike Stump llvm::Value *V = CGF.GetAddrOfLocalVar(Dst); 10779793f0e4SOwen Anderson V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0)); 10783b65ac26SMike Stump V = Builder.CreateLoad(V); 1079f89230d9SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 1080f89230d9SMike Stump llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty); 1081f89230d9SMike Stump 1082f89230d9SMike Stump // src->x 1083f89230d9SMike Stump V = CGF.GetAddrOfLocalVar(Src); 1084f89230d9SMike Stump V = Builder.CreateLoad(V); 1085f89230d9SMike Stump V = Builder.CreateBitCast(V, T); 1086f89230d9SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 10879793f0e4SOwen Anderson V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); 1088f89230d9SMike Stump llvm::Value *SrcObj = Builder.CreateLoad(V); 1089f89230d9SMike Stump 1090f89230d9SMike Stump flag |= BLOCK_BYREF_CALLER; 1091f89230d9SMike Stump 109241a75027SOwen Anderson llvm::Value *N = llvm::ConstantInt::get( 109341a75027SOwen Anderson llvm::Type::getInt32Ty(T->getContext()), flag); 1094f89230d9SMike Stump llvm::Value *F = getBlockObjectAssign(); 1095f89230d9SMike Stump Builder.CreateCall3(F, DstObj, SrcObj, N); 1096f89230d9SMike Stump 1097ee2a5ee5SMike Stump CGF.FinishFunction(); 1098ee2a5ee5SMike Stump 1099ade90fd1SOwen Anderson return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 1100ee2a5ee5SMike Stump } 1101ee2a5ee5SMike Stump 1102fbe25dd1SMike Stump llvm::Constant * 1103fbe25dd1SMike Stump BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T, 1104fbe25dd1SMike Stump int flag) { 1105ee2a5ee5SMike Stump QualType R = getContext().VoidTy; 1106ee2a5ee5SMike Stump 1107ee2a5ee5SMike Stump FunctionArgList Args; 1108ee2a5ee5SMike Stump // FIXME: This leaks 1109ee2a5ee5SMike Stump ImplicitParamDecl *Src = 11107fe9cc1dSMike Stump ImplicitParamDecl::Create(getContext(), 0, 11117fe9cc1dSMike Stump SourceLocation(), 0, 1112ee2a5ee5SMike Stump getContext().getPointerType(getContext().VoidTy)); 1113ee2a5ee5SMike Stump 1114ee2a5ee5SMike Stump Args.push_back(std::make_pair(Src, Src->getType())); 1115ee2a5ee5SMike Stump 1116ee2a5ee5SMike Stump const CGFunctionInfo &FI = 1117ab26cfa5SJohn McCall CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false); 1118ee2a5ee5SMike Stump 1119ee2a5ee5SMike Stump CodeGenTypes &Types = CGM.getTypes(); 1120ee2a5ee5SMike Stump const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); 1121ee2a5ee5SMike Stump 1122cbc2bcafSMike Stump // FIXME: We'd like to put these into a mergable by content, with 1123cbc2bcafSMike Stump // internal linkage. 1124ee2a5ee5SMike Stump llvm::Function *Fn = 1125ee2a5ee5SMike Stump llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 1126d6b28fc6SBenjamin Kramer "__Block_byref_id_object_dispose_", 1127ee2a5ee5SMike Stump &CGM.getModule()); 1128ee2a5ee5SMike Stump 1129ee2a5ee5SMike Stump IdentifierInfo *II 1130ee2a5ee5SMike Stump = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_"); 1131ee2a5ee5SMike Stump 1132ee2a5ee5SMike Stump FunctionDecl *FD = FunctionDecl::Create(getContext(), 1133ee2a5ee5SMike Stump getContext().getTranslationUnitDecl(), 113460ed5604SArgyrios Kyrtzidis SourceLocation(), II, R, 0, 1135ee2a5ee5SMike Stump FunctionDecl::Static, false, 1136ee2a5ee5SMike Stump true); 1137ee2a5ee5SMike Stump CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); 1138fbe25dd1SMike Stump 1139fbe25dd1SMike Stump llvm::Value *V = CGF.GetAddrOfLocalVar(Src); 11409793f0e4SOwen Anderson V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0)); 11413b65ac26SMike Stump V = Builder.CreateLoad(V); 1142fbe25dd1SMike Stump V = Builder.CreateStructGEP(V, 6, "x"); 11439793f0e4SOwen Anderson V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); 11443b65ac26SMike Stump V = Builder.CreateLoad(V); 1145fbe25dd1SMike Stump 1146fbe25dd1SMike Stump flag |= BLOCK_BYREF_CALLER; 1147fbe25dd1SMike Stump BuildBlockRelease(V, flag); 1148ee2a5ee5SMike Stump CGF.FinishFunction(); 1149ee2a5ee5SMike Stump 1150ade90fd1SOwen Anderson return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); 1151ee2a5ee5SMike Stump } 1152ee2a5ee5SMike Stump 1153f89230d9SMike Stump llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T, 1154e08ea787SChris Lattner int Flag, unsigned Align) { 1155e08ea787SChris Lattner // All alignments below that of pointer alignment collapse down to just 1156cbc2bcafSMike Stump // pointer alignment, as we always have at least that much alignment to begin 1157cbc2bcafSMike Stump // with. 1158cbc2bcafSMike Stump Align /= unsigned(CGF.Target.getPointerAlign(0)/8); 1159e08ea787SChris Lattner 1160cbc2bcafSMike Stump // As an optimization, we only generate a single function of each kind we 1161cbc2bcafSMike Stump // might need. We need a different one for each alignment and for each 1162cbc2bcafSMike Stump // setting of flags. We mix Align and flag to get the kind. 1163e08ea787SChris Lattner uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag; 1164e08ea787SChris Lattner llvm::Constant *&Entry = CGM.AssignCache[Kind]; 1165cbc2bcafSMike Stump if (Entry) 1166cbc2bcafSMike Stump return Entry; 1167e08ea787SChris Lattner return Entry = CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, Flag); 1168ee2a5ee5SMike Stump } 1169ee2a5ee5SMike Stump 1170fbe25dd1SMike Stump llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T, 1171e08ea787SChris Lattner int Flag, 1172cbc2bcafSMike Stump unsigned Align) { 1173cbc2bcafSMike Stump // All alignments below that of pointer alignment collpase down to just 1174cbc2bcafSMike Stump // pointer alignment, as we always have at least that much alignment to begin 1175cbc2bcafSMike Stump // with. 1176cbc2bcafSMike Stump Align /= unsigned(CGF.Target.getPointerAlign(0)/8); 1177e08ea787SChris Lattner 1178cbc2bcafSMike Stump // As an optimization, we only generate a single function of each kind we 1179cbc2bcafSMike Stump // might need. We need a different one for each alignment and for each 1180cbc2bcafSMike Stump // setting of flags. We mix Align and flag to get the kind. 1181e08ea787SChris Lattner uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag; 1182e08ea787SChris Lattner llvm::Constant *&Entry = CGM.DestroyCache[Kind]; 1183cbc2bcafSMike Stump if (Entry) 1184cbc2bcafSMike Stump return Entry; 1185e08ea787SChris Lattner return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, Flag); 1186ee2a5ee5SMike Stump } 1187ee2a5ee5SMike Stump 1188626aecc4SMike Stump llvm::Value *BlockFunction::getBlockObjectDispose() { 1189626aecc4SMike Stump if (CGM.BlockObjectDispose == 0) { 1190626aecc4SMike Stump const llvm::FunctionType *FTy; 1191626aecc4SMike Stump std::vector<const llvm::Type*> ArgTys; 119241a75027SOwen Anderson const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext); 1193626aecc4SMike Stump ArgTys.push_back(PtrToInt8Ty); 119441a75027SOwen Anderson ArgTys.push_back(llvm::Type::getInt32Ty(VMContext)); 11959793f0e4SOwen Anderson FTy = llvm::FunctionType::get(ResultType, ArgTys, false); 1196626aecc4SMike Stump CGM.BlockObjectDispose 1197626aecc4SMike Stump = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose"); 1198626aecc4SMike Stump } 1199626aecc4SMike Stump return CGM.BlockObjectDispose; 1200626aecc4SMike Stump } 1201626aecc4SMike Stump 1202f89230d9SMike Stump llvm::Value *BlockFunction::getBlockObjectAssign() { 1203f89230d9SMike Stump if (CGM.BlockObjectAssign == 0) { 1204f89230d9SMike Stump const llvm::FunctionType *FTy; 1205f89230d9SMike Stump std::vector<const llvm::Type*> ArgTys; 120641a75027SOwen Anderson const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext); 1207f89230d9SMike Stump ArgTys.push_back(PtrToInt8Ty); 1208f89230d9SMike Stump ArgTys.push_back(PtrToInt8Ty); 120941a75027SOwen Anderson ArgTys.push_back(llvm::Type::getInt32Ty(VMContext)); 12109793f0e4SOwen Anderson FTy = llvm::FunctionType::get(ResultType, ArgTys, false); 1211f89230d9SMike Stump CGM.BlockObjectAssign 1212f89230d9SMike Stump = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign"); 1213f89230d9SMike Stump } 1214f89230d9SMike Stump return CGM.BlockObjectAssign; 1215f89230d9SMike Stump } 1216f89230d9SMike Stump 1217fbe25dd1SMike Stump void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) { 1218626aecc4SMike Stump llvm::Value *F = getBlockObjectDispose(); 1219fbe25dd1SMike Stump llvm::Value *N; 1220626aecc4SMike Stump V = Builder.CreateBitCast(V, PtrToInt8Ty); 122141a75027SOwen Anderson N = llvm::ConstantInt::get(llvm::Type::getInt32Ty(V->getContext()), flag); 1222626aecc4SMike Stump Builder.CreateCall2(F, V, N); 1223626aecc4SMike Stump } 12244446dcf0SMike Stump 12254446dcf0SMike Stump ASTContext &BlockFunction::getContext() const { return CGM.getContext(); } 1226aeb0ffd5SMike Stump 1227aeb0ffd5SMike Stump BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf, 1228aeb0ffd5SMike Stump CGBuilderTy &B) 1229170229f6SOwen Anderson : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) { 123041a75027SOwen Anderson PtrToInt8Ty = llvm::PointerType::getUnqual( 123141a75027SOwen Anderson llvm::Type::getInt8Ty(VMContext)); 1232aeb0ffd5SMike Stump 1233aeb0ffd5SMike Stump BlockHasCopyDispose = false; 1234aeb0ffd5SMike Stump } 1235