Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 1 | //===--- CGBlocks.cpp - Emit LLVM Code for declarations -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code to emit blocks. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
| 15 | #include "CodeGenModule.h" |
Mike Stump | 6cc88f7 | 2009-03-20 21:53:12 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetData.h" |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 19 | #include <algorithm> |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | using namespace CodeGen; |
| 22 | |
Mike Stump | cf62d39 | 2009-03-06 18:42:23 +0000 | [diff] [blame] | 23 | llvm::Constant *CodeGenFunction:: |
Mike Stump | a803b0e | 2009-03-25 17:58:24 +0000 | [diff] [blame] | 24 | BuildDescriptorBlockDecl(bool BlockHasCopyDispose, uint64_t Size, |
| 25 | const llvm::StructType* Ty, |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 26 | std::vector<HelperInfo> *NoteForHelper) { |
Mike Stump | 56129b1 | 2009-02-13 16:55:51 +0000 | [diff] [blame] | 27 | const llvm::Type *UnsignedLongTy |
| 28 | = CGM.getTypes().ConvertType(getContext().UnsignedLongTy); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 29 | llvm::Constant *C; |
| 30 | std::vector<llvm::Constant*> Elts; |
| 31 | |
| 32 | // reserved |
Mike Stump | 56129b1 | 2009-02-13 16:55:51 +0000 | [diff] [blame] | 33 | C = llvm::ConstantInt::get(UnsignedLongTy, 0); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 34 | Elts.push_back(C); |
| 35 | |
| 36 | // Size |
Mike Stump | d684000 | 2009-02-21 20:07:44 +0000 | [diff] [blame] | 37 | // FIXME: What is the right way to say this doesn't fit? We should give |
| 38 | // a user diagnostic in that case. Better fix would be to change the |
| 39 | // API to size_t. |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 40 | C = llvm::ConstantInt::get(UnsignedLongTy, Size); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 41 | Elts.push_back(C); |
| 42 | |
| 43 | if (BlockHasCopyDispose) { |
| 44 | // copy_func_helper_decl |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 45 | Elts.push_back(BuildCopyHelper(Ty, NoteForHelper)); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 46 | |
| 47 | // destroy_func_decl |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 48 | Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper)); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | C = llvm::ConstantStruct::get(Elts); |
| 52 | |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame^] | 53 | C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 54 | llvm::GlobalValue::InternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame^] | 55 | C, "__block_descriptor_tmp"); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 56 | return C; |
| 57 | } |
| 58 | |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 59 | llvm::Constant *BlockModule::getNSConcreteGlobalBlock() { |
Chris Lattner | e2f79b6 | 2009-05-13 02:50:56 +0000 | [diff] [blame] | 60 | if (NSConcreteGlobalBlock == 0) |
| 61 | NSConcreteGlobalBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty, |
| 62 | "_NSConcreteGlobalBlock"); |
Mike Stump | f99f1d0 | 2009-02-13 17:23:42 +0000 | [diff] [blame] | 63 | return NSConcreteGlobalBlock; |
| 64 | } |
| 65 | |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 66 | llvm::Constant *BlockModule::getNSConcreteStackBlock() { |
Chris Lattner | e2f79b6 | 2009-05-13 02:50:56 +0000 | [diff] [blame] | 67 | if (NSConcreteStackBlock == 0) |
| 68 | NSConcreteStackBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty, |
| 69 | "_NSConcreteStackBlock"); |
Mike Stump | 59c5b11 | 2009-02-13 19:29:27 +0000 | [diff] [blame] | 70 | return NSConcreteStackBlock; |
| 71 | } |
| 72 | |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 73 | static void CollectBlockDeclRefInfo(const Stmt *S, |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 74 | CodeGenFunction::BlockInfo &Info) { |
| 75 | for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); |
| 76 | I != E; ++I) |
Daniel Dunbar | 82573ee | 2009-03-02 07:00:57 +0000 | [diff] [blame] | 77 | if (*I) |
| 78 | CollectBlockDeclRefInfo(*I, Info); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 79 | |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 80 | if (const BlockDeclRefExpr *DE = dyn_cast<BlockDeclRefExpr>(S)) { |
| 81 | // FIXME: Handle enums. |
| 82 | if (isa<FunctionDecl>(DE->getDecl())) |
| 83 | return; |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 84 | |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 85 | if (DE->isByRef()) |
| 86 | Info.ByRefDeclRefs.push_back(DE); |
| 87 | else |
| 88 | Info.ByCopyDeclRefs.push_back(DE); |
| 89 | } |
| 90 | } |
| 91 | |
Mike Stump | 58a8514 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 92 | /// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be |
| 93 | /// declared as a global variable instead of on the stack. |
Chris Lattner | e2f79b6 | 2009-05-13 02:50:56 +0000 | [diff] [blame] | 94 | static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info) { |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 95 | return Info.ByRefDeclRefs.empty() && Info.ByCopyDeclRefs.empty(); |
| 96 | } |
| 97 | |
Mike Stump | 58a8514 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 98 | // FIXME: Push most into CGM, passing down a few bits, like current function |
| 99 | // name. |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 100 | llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) { |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 101 | |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 102 | std::string Name = CurFn->getName(); |
| 103 | CodeGenFunction::BlockInfo Info(0, Name.c_str()); |
| 104 | CollectBlockDeclRefInfo(BE->getBody(), Info); |
| 105 | |
| 106 | // Check if the block can be global. |
Mike Stump | 58a8514 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 107 | // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like |
| 108 | // to just have one code path. We should move this function into CGM and pass |
| 109 | // CGF, then we can just check to see if CGF is 0. |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 110 | if (0 && CanBlockBeGlobal(Info)) |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 111 | return CGM.GetAddrOfGlobalBlock(BE, Name.c_str()); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 112 | |
| 113 | std::vector<llvm::Constant*> Elts(5); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 114 | llvm::Constant *C; |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 115 | llvm::Value *V; |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 116 | |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 117 | { |
| 118 | // C = BuildBlockStructInitlist(); |
| 119 | unsigned int flags = BLOCK_HAS_DESCRIPTOR; |
| 120 | |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 121 | // We run this first so that we set BlockHasCopyDispose from the entire |
| 122 | // block literal. |
| 123 | // __invoke |
| 124 | uint64_t subBlockSize, subBlockAlign; |
| 125 | llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; |
Mike Stump | a803b0e | 2009-03-25 17:58:24 +0000 | [diff] [blame] | 126 | bool subBlockHasCopyDispose = false; |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 127 | llvm::Function *Fn |
Mike Stump | 6cc88f7 | 2009-03-20 21:53:12 +0000 | [diff] [blame] | 128 | = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl, LocalDeclMap, |
Mike Stump | 7f28a9c | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 129 | subBlockSize, |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 130 | subBlockAlign, |
| 131 | subBlockDeclRefDecls, |
Mike Stump | a803b0e | 2009-03-25 17:58:24 +0000 | [diff] [blame] | 132 | subBlockHasCopyDispose); |
| 133 | BlockHasCopyDispose |= subBlockHasCopyDispose; |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 134 | Elts[3] = Fn; |
| 135 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 136 | // FIXME: Don't use BlockHasCopyDispose, it is set more often then |
| 137 | // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); } |
Mike Stump | a803b0e | 2009-03-25 17:58:24 +0000 | [diff] [blame] | 138 | if (subBlockHasCopyDispose) |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 139 | flags |= BLOCK_HAS_COPY_DISPOSE; |
| 140 | |
Mike Stump | 7d6dc4f | 2009-02-13 20:17:16 +0000 | [diff] [blame] | 141 | // __isa |
Mike Stump | 59c5b11 | 2009-02-13 19:29:27 +0000 | [diff] [blame] | 142 | C = CGM.getNSConcreteStackBlock(); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 143 | C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 144 | Elts[0] = C; |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 145 | |
| 146 | // __flags |
| 147 | const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( |
| 148 | CGM.getTypes().ConvertType(CGM.getContext().IntTy)); |
| 149 | C = llvm::ConstantInt::get(IntTy, flags); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 150 | Elts[1] = C; |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 151 | |
| 152 | // __reserved |
| 153 | C = llvm::ConstantInt::get(IntTy, 0); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 154 | Elts[2] = C; |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 155 | |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 156 | if (subBlockDeclRefDecls.size() == 0) { |
Mike Stump | cf62d39 | 2009-03-06 18:42:23 +0000 | [diff] [blame] | 157 | // __descriptor |
Mike Stump | a803b0e | 2009-03-25 17:58:24 +0000 | [diff] [blame] | 158 | Elts[4] = BuildDescriptorBlockDecl(subBlockHasCopyDispose, subBlockSize, 0, 0); |
Mike Stump | cf62d39 | 2009-03-06 18:42:23 +0000 | [diff] [blame] | 159 | |
Mike Stump | 5570cfe | 2009-03-01 20:07:53 +0000 | [diff] [blame] | 160 | // Optimize to being a global block. |
| 161 | Elts[0] = CGM.getNSConcreteGlobalBlock(); |
| 162 | Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL); |
| 163 | |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 164 | C = llvm::ConstantStruct::get(Elts); |
| 165 | |
| 166 | char Name[32]; |
| 167 | sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount()); |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame^] | 168 | C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 169 | llvm::GlobalValue::InternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame^] | 170 | C, Name); |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 171 | QualType BPT = BE->getType(); |
| 172 | C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT)); |
| 173 | return C; |
| 174 | } |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 175 | |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 176 | std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size()); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 177 | for (int i=0; i<4; ++i) |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 178 | Types[i] = Elts[i]->getType(); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 179 | Types[4] = PtrToInt8Ty; |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 180 | |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 181 | for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) { |
| 182 | const Expr *E = subBlockDeclRefDecls[i]; |
| 183 | const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); |
| 184 | QualType Ty = E->getType(); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 185 | if (BDRE && BDRE->isByRef()) { |
| 186 | uint64_t Align = getContext().getDeclAlignInBytes(BDRE->getDecl()); |
| 187 | Types[i+5] = llvm::PointerType::get(BuildByRefType(Ty, Align), 0); |
| 188 | } else |
| 189 | Types[i+5] = ConvertType(Ty); |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 190 | } |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 191 | |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 192 | llvm::StructType *Ty = llvm::StructType::get(Types, true); |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 193 | |
| 194 | llvm::AllocaInst *A = CreateTempAlloca(Ty); |
| 195 | A->setAlignment(subBlockAlign); |
| 196 | V = A; |
| 197 | |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 198 | std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size()); |
| 199 | int helpersize = 0; |
| 200 | |
| 201 | for (unsigned i=0; i<4; ++i) |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 202 | Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp")); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 203 | |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 204 | for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) |
| 205 | { |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 206 | // FIXME: Push const down. |
| 207 | Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]); |
| 208 | DeclRefExpr *DR; |
| 209 | ValueDecl *VD; |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 210 | |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 211 | DR = dyn_cast<DeclRefExpr>(E); |
| 212 | // Skip padding. |
| 213 | if (DR) continue; |
| 214 | |
| 215 | BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); |
| 216 | VD = BDRE->getDecl(); |
| 217 | |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 218 | llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp"); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 219 | NoteForHelper[helpersize].index = i+5; |
| 220 | NoteForHelper[helpersize].RequiresCopying = BlockRequiresCopying(VD->getType()); |
| 221 | NoteForHelper[helpersize].flag |
| 222 | = VD->getType()->isBlockPointerType() ? BLOCK_FIELD_IS_BLOCK : BLOCK_FIELD_IS_OBJECT; |
| 223 | |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 224 | if (LocalDeclMap[VD]) { |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 225 | if (BDRE->isByRef()) { |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 226 | NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF | |
Mike Stump | f4bc312 | 2009-03-07 06:04:31 +0000 | [diff] [blame] | 227 | // FIXME: Someone double check this. |
| 228 | (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 229 | const llvm::Type *Ty = Types[i+5]; |
| 230 | llvm::Value *Loc = LocalDeclMap[VD]; |
| 231 | Loc = Builder.CreateStructGEP(Loc, 1, "forwarding"); |
| 232 | Loc = Builder.CreateLoad(Loc, false); |
| 233 | Loc = Builder.CreateBitCast(Loc, Ty); |
| 234 | Builder.CreateStore(Loc, Addr); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 235 | ++helpersize; |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 236 | continue; |
| 237 | } else |
| 238 | E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD), |
| 239 | VD->getType(), SourceLocation(), |
| 240 | false, false); |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 241 | } |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 242 | if (BDRE->isByRef()) { |
Mike Stump | a8b60c9 | 2009-03-21 21:00:35 +0000 | [diff] [blame] | 243 | NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF | |
| 244 | // FIXME: Someone double check this. |
| 245 | (VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0); |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 246 | E = new (getContext()) |
| 247 | UnaryOperator(E, UnaryOperator::AddrOf, |
| 248 | getContext().getPointerType(E->getType()), |
| 249 | SourceLocation()); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 250 | } |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 251 | ++helpersize; |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 252 | |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 253 | RValue r = EmitAnyExpr(E, Addr, false); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 254 | if (r.isScalar()) { |
| 255 | llvm::Value *Loc = r.getScalarVal(); |
| 256 | const llvm::Type *Ty = Types[i+5]; |
| 257 | if (BDRE->isByRef()) { |
Mike Stump | 58a8514 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 258 | // E is now the address of the value field, instead, we want the |
| 259 | // address of the actual ByRef struct. We optimize this slightly |
| 260 | // compared to gcc by not grabbing the forwarding slot as this must |
| 261 | // be done during Block_copy for us, and we can postpone the work |
| 262 | // until then. |
| 263 | uint64_t offset = BlockDecls[BDRE->getDecl()]; |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 264 | |
Mike Stump | 58a8514 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 265 | llvm::Value *BlockLiteral = LoadBlockStruct(); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 266 | |
Mike Stump | 58a8514 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 267 | Loc = Builder.CreateGEP(BlockLiteral, |
| 268 | llvm::ConstantInt::get(llvm::Type::Int64Ty, |
| 269 | offset), |
| 270 | "block.literal"); |
| 271 | Ty = llvm::PointerType::get(Ty, 0); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 272 | Loc = Builder.CreateBitCast(Loc, Ty); |
Mike Stump | 58a8514 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 273 | Loc = Builder.CreateLoad(Loc, false); |
| 274 | // Loc = Builder.CreateBitCast(Loc, Ty); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 275 | } |
| 276 | Builder.CreateStore(Loc, Addr); |
| 277 | } else if (r.isComplex()) |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 278 | // FIXME: implement |
| 279 | ErrorUnsupported(BE, "complex in block literal"); |
| 280 | else if (r.isAggregate()) |
| 281 | ; // Already created into the destination |
| 282 | else |
| 283 | assert (0 && "bad block variable"); |
| 284 | // FIXME: Ensure that the offset created by the backend for |
| 285 | // the struct matches the previously computed offset in BlockDecls. |
| 286 | } |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 287 | NoteForHelper.resize(helpersize); |
Mike Stump | cf62d39 | 2009-03-06 18:42:23 +0000 | [diff] [blame] | 288 | |
| 289 | // __descriptor |
Mike Stump | a803b0e | 2009-03-25 17:58:24 +0000 | [diff] [blame] | 290 | llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockHasCopyDispose, |
| 291 | subBlockSize, Ty, |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 292 | &NoteForHelper); |
| 293 | Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty); |
| 294 | Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp")); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 295 | } |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 296 | |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 297 | QualType BPT = BE->getType(); |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 298 | return Builder.CreateBitCast(V, ConvertType(BPT)); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 302 | const llvm::Type *BlockModule::getBlockDescriptorType() { |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 303 | if (BlockDescriptorType) |
| 304 | return BlockDescriptorType; |
| 305 | |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 306 | const llvm::Type *UnsignedLongTy = |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 307 | getTypes().ConvertType(getContext().UnsignedLongTy); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 308 | |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 309 | // struct __block_descriptor { |
| 310 | // unsigned long reserved; |
| 311 | // unsigned long block_size; |
| 312 | // }; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 313 | BlockDescriptorType = llvm::StructType::get(UnsignedLongTy, |
| 314 | UnsignedLongTy, |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 315 | NULL); |
| 316 | |
| 317 | getModule().addTypeName("struct.__block_descriptor", |
| 318 | BlockDescriptorType); |
| 319 | |
| 320 | return BlockDescriptorType; |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 323 | const llvm::Type *BlockModule::getGenericBlockLiteralType() { |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 324 | if (GenericBlockLiteralType) |
| 325 | return GenericBlockLiteralType; |
| 326 | |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 327 | const llvm::Type *BlockDescPtrTy = |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 328 | llvm::PointerType::getUnqual(getBlockDescriptorType()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 329 | |
Mike Stump | 7cbb360 | 2009-02-13 16:01:35 +0000 | [diff] [blame] | 330 | const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( |
| 331 | getTypes().ConvertType(getContext().IntTy)); |
| 332 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 333 | // struct __block_literal_generic { |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 334 | // void *__isa; |
| 335 | // int __flags; |
| 336 | // int __reserved; |
| 337 | // void (*__invoke)(void *); |
| 338 | // struct __block_descriptor *__descriptor; |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 339 | // }; |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 340 | GenericBlockLiteralType = llvm::StructType::get(PtrToInt8Ty, |
Mike Stump | 7cbb360 | 2009-02-13 16:01:35 +0000 | [diff] [blame] | 341 | IntTy, |
| 342 | IntTy, |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 343 | PtrToInt8Ty, |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 344 | BlockDescPtrTy, |
| 345 | NULL); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 346 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 347 | getModule().addTypeName("struct.__block_literal_generic", |
| 348 | GenericBlockLiteralType); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 349 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 350 | return GenericBlockLiteralType; |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 353 | const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() { |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 354 | if (GenericExtendedBlockLiteralType) |
| 355 | return GenericExtendedBlockLiteralType; |
| 356 | |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 357 | const llvm::Type *BlockDescPtrTy = |
| 358 | llvm::PointerType::getUnqual(getBlockDescriptorType()); |
| 359 | |
| 360 | const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( |
| 361 | getTypes().ConvertType(getContext().IntTy)); |
| 362 | |
| 363 | // struct __block_literal_generic { |
| 364 | // void *__isa; |
| 365 | // int __flags; |
| 366 | // int __reserved; |
| 367 | // void (*__invoke)(void *); |
| 368 | // struct __block_descriptor *__descriptor; |
| 369 | // void *__copy_func_helper_decl; |
| 370 | // void *__destroy_func_decl; |
| 371 | // }; |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 372 | GenericExtendedBlockLiteralType = llvm::StructType::get(PtrToInt8Ty, |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 373 | IntTy, |
| 374 | IntTy, |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 375 | PtrToInt8Ty, |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 376 | BlockDescPtrTy, |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 377 | PtrToInt8Ty, |
| 378 | PtrToInt8Ty, |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 379 | NULL); |
| 380 | |
| 381 | getModule().addTypeName("struct.__block_literal_extended_generic", |
| 382 | GenericExtendedBlockLiteralType); |
| 383 | |
| 384 | return GenericExtendedBlockLiteralType; |
| 385 | } |
| 386 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 387 | RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) { |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 388 | const BlockPointerType *BPT = |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 389 | E->getCallee()->getType()->getAsBlockPointerType(); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 390 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 391 | llvm::Value *Callee = EmitScalarExpr(E->getCallee()); |
| 392 | |
| 393 | // Get a pointer to the generic block literal. |
| 394 | const llvm::Type *BlockLiteralTy = |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 395 | llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType()); |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 396 | |
| 397 | // Bitcast the callee to a block literal. |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 398 | llvm::Value *BlockLiteral = |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 399 | Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal"); |
| 400 | |
| 401 | // Get the function pointer from the literal. |
| 402 | llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp"); |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 403 | |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 404 | BlockLiteral = |
| 405 | Builder.CreateBitCast(BlockLiteral, |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 406 | llvm::PointerType::getUnqual(llvm::Type::Int8Ty), |
| 407 | "tmp"); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 408 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 409 | // Add the block literal. |
| 410 | QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy); |
| 411 | CallArgList Args; |
| 412 | Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy)); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 413 | |
Anders Carlsson | 782f397 | 2009-04-08 23:13:16 +0000 | [diff] [blame] | 414 | QualType FnType = BPT->getPointeeType(); |
| 415 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 416 | // And the rest of the arguments. |
Anders Carlsson | 782f397 | 2009-04-08 23:13:16 +0000 | [diff] [blame] | 417 | EmitCallArgs(Args, FnType->getAsFunctionProtoType(), |
| 418 | E->arg_begin(), E->arg_end()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 419 | |
Anders Carlsson | 6e460ff | 2009-04-07 22:10:22 +0000 | [diff] [blame] | 420 | // Load the function. |
| 421 | llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp"); |
| 422 | |
Anders Carlsson | a17d7cc | 2009-04-08 02:55:55 +0000 | [diff] [blame] | 423 | QualType ResultType = FnType->getAsFunctionType()->getResultType(); |
| 424 | |
| 425 | const CGFunctionInfo &FnInfo = |
| 426 | CGM.getTypes().getFunctionInfo(ResultType, Args); |
Anders Carlsson | 6e460ff | 2009-04-07 22:10:22 +0000 | [diff] [blame] | 427 | |
| 428 | // Cast the function pointer to the right type. |
| 429 | const llvm::Type *BlockFTy = |
Anders Carlsson | a17d7cc | 2009-04-08 02:55:55 +0000 | [diff] [blame] | 430 | CGM.getTypes().GetFunctionType(FnInfo, false); |
Anders Carlsson | 6e460ff | 2009-04-07 22:10:22 +0000 | [diff] [blame] | 431 | |
| 432 | const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy); |
| 433 | Func = Builder.CreateBitCast(Func, BlockFTyPtr); |
| 434 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 435 | // And call the block. |
Anders Carlsson | f8544a4 | 2009-04-07 00:20:24 +0000 | [diff] [blame] | 436 | return EmitCall(FnInfo, Func, Args); |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 437 | } |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 438 | |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 439 | llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) { |
| 440 | uint64_t &offset = BlockDecls[E->getDecl()]; |
| 441 | |
| 442 | const llvm::Type *Ty; |
| 443 | Ty = CGM.getTypes().ConvertType(E->getDecl()->getType()); |
| 444 | |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 445 | // See if we have already allocated an offset for this variable. |
| 446 | if (offset == 0) { |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 447 | // Don't run the expensive check, unless we have to. |
| 448 | if (!BlockHasCopyDispose && BlockRequiresCopying(E->getType())) |
| 449 | BlockHasCopyDispose = true; |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 450 | // if not, allocate one now. |
| 451 | offset = getBlockOffset(E); |
| 452 | } |
| 453 | |
| 454 | llvm::Value *BlockLiteral = LoadBlockStruct(); |
| 455 | llvm::Value *V = Builder.CreateGEP(BlockLiteral, |
| 456 | llvm::ConstantInt::get(llvm::Type::Int64Ty, |
| 457 | offset), |
Mike Stump | 58a8514 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 458 | "block.literal"); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 459 | if (E->isByRef()) { |
| 460 | bool needsCopyDispose = BlockRequiresCopying(E->getType()); |
| 461 | uint64_t Align = getContext().getDeclAlignInBytes(E->getDecl()); |
| 462 | const llvm::Type *PtrStructTy |
| 463 | = llvm::PointerType::get(BuildByRefType(E->getType(), Align), 0); |
Mike Stump | a8b60c9 | 2009-03-21 21:00:35 +0000 | [diff] [blame] | 464 | // The block literal will need a copy/destroy helper. |
| 465 | BlockHasCopyDispose = true; |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 466 | Ty = PtrStructTy; |
| 467 | Ty = llvm::PointerType::get(Ty, 0); |
| 468 | V = Builder.CreateBitCast(V, Ty); |
| 469 | V = Builder.CreateLoad(V, false); |
| 470 | V = Builder.CreateStructGEP(V, 1, "forwarding"); |
| 471 | V = Builder.CreateLoad(V, false); |
| 472 | V = Builder.CreateBitCast(V, PtrStructTy); |
| 473 | V = Builder.CreateStructGEP(V, needsCopyDispose*2 + 4, "x"); |
| 474 | } else { |
| 475 | Ty = llvm::PointerType::get(Ty, 0); |
| 476 | V = Builder.CreateBitCast(V, Ty); |
| 477 | } |
| 478 | return V; |
| 479 | } |
| 480 | |
Mike Stump | 6cc88f7 | 2009-03-20 21:53:12 +0000 | [diff] [blame] | 481 | void CodeGenFunction::BlockForwardSelf() { |
| 482 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
| 483 | ImplicitParamDecl *SelfDecl = OMD->getSelfDecl(); |
| 484 | llvm::Value *&DMEntry = LocalDeclMap[SelfDecl]; |
| 485 | if (DMEntry) |
| 486 | return; |
| 487 | // FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care |
| 488 | BlockDeclRefExpr *BDRE = new (getContext()) |
| 489 | BlockDeclRefExpr(SelfDecl, |
| 490 | SelfDecl->getType(), SourceLocation(), false); |
| 491 | DMEntry = GetAddrOfBlockDecl(BDRE); |
| 492 | } |
| 493 | |
Mike Stump | 67a6448 | 2009-02-14 22:16:35 +0000 | [diff] [blame] | 494 | llvm::Constant * |
Mike Stump | 90a9043 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 495 | BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) { |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 496 | // Generate the block descriptor. |
| 497 | const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy); |
Mike Stump | 7cbb360 | 2009-02-13 16:01:35 +0000 | [diff] [blame] | 498 | const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( |
| 499 | getTypes().ConvertType(getContext().IntTy)); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 500 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 501 | llvm::Constant *DescriptorFields[2]; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 502 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 503 | // Reserved |
| 504 | DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 505 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 506 | // Block literal size. For global blocks we just use the size of the generic |
| 507 | // block literal struct. |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 508 | uint64_t BlockLiteralSize = |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 509 | TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8; |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 510 | DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 511 | |
| 512 | llvm::Constant *DescriptorStruct = |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 513 | llvm::ConstantStruct::get(&DescriptorFields[0], 2); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 514 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 515 | llvm::GlobalVariable *Descriptor = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame^] | 516 | new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true, |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 517 | llvm::GlobalVariable::InternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame^] | 518 | DescriptorStruct, "__block_descriptor_global"); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 519 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 520 | // Generate the constants for the block literal. |
| 521 | llvm::Constant *LiteralFields[5]; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 522 | |
Mike Stump | 67a6448 | 2009-02-14 22:16:35 +0000 | [diff] [blame] | 523 | CodeGenFunction::BlockInfo Info(0, n); |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 524 | uint64_t subBlockSize, subBlockAlign; |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 525 | llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; |
Daniel Dunbar | d67b09a | 2009-03-12 03:07:24 +0000 | [diff] [blame] | 526 | bool subBlockHasCopyDispose = false; |
Mike Stump | 7f28a9c | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 527 | llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap; |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 528 | llvm::Function *Fn |
Mike Stump | 6cc88f7 | 2009-03-20 21:53:12 +0000 | [diff] [blame] | 529 | = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap, |
Mike Stump | 7f28a9c | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 530 | subBlockSize, |
Mike Stump | 90a9043 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 531 | subBlockAlign, |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 532 | subBlockDeclRefDecls, |
| 533 | subBlockHasCopyDispose); |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 534 | assert(subBlockSize == BlockLiteralSize |
| 535 | && "no imports allowed for global block"); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 536 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 537 | // isa |
Mike Stump | f99f1d0 | 2009-02-13 17:23:42 +0000 | [diff] [blame] | 538 | LiteralFields[0] = getNSConcreteGlobalBlock(); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 539 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 540 | // Flags |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 541 | LiteralFields[1] = |
Anders Carlsson | 8045ee0 | 2009-03-01 21:09:29 +0000 | [diff] [blame] | 542 | llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 543 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 544 | // Reserved |
Mike Stump | 7cbb360 | 2009-02-13 16:01:35 +0000 | [diff] [blame] | 545 | LiteralFields[2] = llvm::Constant::getNullValue(IntTy); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 546 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 547 | // Function |
| 548 | LiteralFields[3] = Fn; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 549 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 550 | // Descriptor |
| 551 | LiteralFields[4] = Descriptor; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 552 | |
| 553 | llvm::Constant *BlockLiteralStruct = |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 554 | llvm::ConstantStruct::get(&LiteralFields[0], 5); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 555 | |
| 556 | llvm::GlobalVariable *BlockLiteral = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame^] | 557 | new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true, |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 558 | llvm::GlobalVariable::InternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame^] | 559 | BlockLiteralStruct, "__block_literal_global"); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 560 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 561 | return BlockLiteral; |
| 562 | } |
| 563 | |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 564 | llvm::Value *CodeGenFunction::LoadBlockStruct() { |
| 565 | return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self"); |
| 566 | } |
| 567 | |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 568 | llvm::Function * |
| 569 | CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr, |
| 570 | const BlockInfo& Info, |
Mike Stump | 6cc88f7 | 2009-03-20 21:53:12 +0000 | [diff] [blame] | 571 | const Decl *OuterFuncDecl, |
Mike Stump | 7f28a9c | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 572 | llvm::DenseMap<const Decl*, llvm::Value*> ldm, |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 573 | uint64_t &Size, |
| 574 | uint64_t &Align, |
| 575 | llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls, |
| 576 | bool &subBlockHasCopyDispose) { |
Devang Patel | 963dfbd | 2009-04-15 21:51:44 +0000 | [diff] [blame] | 577 | |
| 578 | // Check if we should generate debug info for this block. |
| 579 | if (CGM.getDebugInfo()) |
| 580 | DebugInfo = CGM.getDebugInfo(); |
| 581 | |
Mike Stump | 7f28a9c | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 582 | // Arrange for local static and local extern declarations to appear |
| 583 | // to be local to this function as well, as they are directly referenced |
| 584 | // in a block. |
| 585 | for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin(); |
| 586 | i != ldm.end(); |
| 587 | ++i) { |
| 588 | const VarDecl *VD = dyn_cast<VarDecl>(i->first); |
| 589 | |
Daniel Dunbar | 5466c7b | 2009-04-14 02:25:56 +0000 | [diff] [blame] | 590 | if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage()) |
Mike Stump | 7f28a9c | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 591 | LocalDeclMap[VD] = i->second; |
| 592 | } |
| 593 | |
Eli Friedman | 48f9122 | 2009-03-28 03:24:54 +0000 | [diff] [blame] | 594 | // FIXME: We need to rearrange the code for copy/dispose so we have this |
| 595 | // sooner, so we can calculate offsets correctly. |
| 596 | if (!BlockHasCopyDispose) |
| 597 | BlockOffset = CGM.getTargetData() |
| 598 | .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8; |
| 599 | else |
| 600 | BlockOffset = CGM.getTargetData() |
| 601 | .getTypeStoreSizeInBits(CGM.getGenericExtendedBlockLiteralType()) / 8; |
| 602 | BlockAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; |
| 603 | |
Fariborz Jahanian | 140fb26 | 2009-04-11 17:55:15 +0000 | [diff] [blame] | 604 | const FunctionType *BlockFunctionType = BExpr->getFunctionType(); |
| 605 | QualType ResultType; |
| 606 | bool IsVariadic; |
Fariborz Jahanian | da0895d | 2009-04-11 18:54:57 +0000 | [diff] [blame] | 607 | if (const FunctionProtoType *FTy = |
| 608 | dyn_cast<FunctionProtoType>(BlockFunctionType)) { |
Fariborz Jahanian | 140fb26 | 2009-04-11 17:55:15 +0000 | [diff] [blame] | 609 | ResultType = FTy->getResultType(); |
| 610 | IsVariadic = FTy->isVariadic(); |
| 611 | } |
| 612 | else { |
| 613 | // K&R style block. |
| 614 | ResultType = BlockFunctionType->getResultType(); |
| 615 | IsVariadic = false; |
| 616 | } |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 617 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 618 | FunctionArgList Args; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 619 | |
Chris Lattner | 161d36d | 2009-02-28 19:01:03 +0000 | [diff] [blame] | 620 | const BlockDecl *BD = BExpr->getBlockDecl(); |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 621 | |
| 622 | // FIXME: This leaks |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 623 | ImplicitParamDecl *SelfDecl = |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 624 | ImplicitParamDecl::Create(getContext(), 0, |
| 625 | SourceLocation(), 0, |
| 626 | getContext().getPointerType(getContext().VoidTy)); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 627 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 628 | Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType())); |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 629 | BlockStructDecl = SelfDecl; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 630 | |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 631 | for (BlockDecl::param_const_iterator i = BD->param_begin(), |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 632 | e = BD->param_end(); i != e; ++i) |
Mike Stump | 1905061 | 2009-02-17 23:25:52 +0000 | [diff] [blame] | 633 | Args.push_back(std::make_pair(*i, (*i)->getType())); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 634 | |
| 635 | const CGFunctionInfo &FI = |
Fariborz Jahanian | 140fb26 | 2009-04-11 17:55:15 +0000 | [diff] [blame] | 636 | CGM.getTypes().getFunctionInfo(ResultType, Args); |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 637 | |
Mike Stump | 67a6448 | 2009-02-14 22:16:35 +0000 | [diff] [blame] | 638 | std::string Name = std::string("__") + Info.Name + "_block_invoke_"; |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 639 | CodeGenTypes &Types = CGM.getTypes(); |
Fariborz Jahanian | 140fb26 | 2009-04-11 17:55:15 +0000 | [diff] [blame] | 640 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 641 | |
| 642 | llvm::Function *Fn = |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 643 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
| 644 | Name, |
| 645 | &CGM.getModule()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 646 | |
Daniel Dunbar | 0e4f40e | 2009-04-17 00:48:04 +0000 | [diff] [blame] | 647 | CGM.SetInternalFunctionAttributes(BD, Fn, FI); |
| 648 | |
Chris Lattner | 4863db4 | 2009-04-23 07:18:56 +0000 | [diff] [blame] | 649 | StartFunction(BD, ResultType, Fn, Args, |
Chris Lattner | 161d36d | 2009-02-28 19:01:03 +0000 | [diff] [blame] | 650 | BExpr->getBody()->getLocEnd()); |
Chris Lattner | 4863db4 | 2009-04-23 07:18:56 +0000 | [diff] [blame] | 651 | CurFuncDecl = OuterFuncDecl; |
Chris Lattner | b5437d2 | 2009-04-23 05:30:27 +0000 | [diff] [blame] | 652 | CurCodeDecl = BD; |
Chris Lattner | 161d36d | 2009-02-28 19:01:03 +0000 | [diff] [blame] | 653 | EmitStmt(BExpr->getBody()); |
| 654 | FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc()); |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 655 | |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 656 | // The runtime needs a minimum alignment of a void *. |
| 657 | uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; |
| 658 | BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign); |
| 659 | |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 660 | Size = BlockOffset; |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 661 | Align = BlockAlign; |
| 662 | subBlockDeclRefDecls = BlockDeclRefDecls; |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 663 | subBlockHasCopyDispose |= BlockHasCopyDispose; |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 664 | return Fn; |
| 665 | } |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 666 | |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 667 | uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) { |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 668 | const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl()); |
| 669 | |
| 670 | uint64_t Size = getContext().getTypeSize(D->getType()) / 8; |
| 671 | uint64_t Align = getContext().getDeclAlignInBytes(D); |
| 672 | |
| 673 | if (BDRE->isByRef()) { |
| 674 | Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8; |
| 675 | Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; |
| 676 | } |
| 677 | |
| 678 | assert ((Align > 0) && "alignment must be 1 byte or more"); |
| 679 | |
| 680 | uint64_t OldOffset = BlockOffset; |
| 681 | |
| 682 | // Ensure proper alignment, even if it means we have to have a gap |
| 683 | BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align); |
| 684 | BlockAlign = std::max(Align, BlockAlign); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 685 | |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 686 | uint64_t Pad = BlockOffset - OldOffset; |
| 687 | if (Pad) { |
| 688 | llvm::ArrayType::get(llvm::Type::Int8Ty, Pad); |
| 689 | QualType PadTy = getContext().getConstantArrayType(getContext().CharTy, |
| 690 | llvm::APInt(32, Pad), |
| 691 | ArrayType::Normal, 0); |
| 692 | ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(), |
| 693 | 0, QualType(PadTy), VarDecl::None, |
| 694 | SourceLocation()); |
| 695 | Expr *E; |
| 696 | E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(), |
| 697 | SourceLocation(), false, false); |
| 698 | BlockDeclRefDecls.push_back(E); |
| 699 | } |
| 700 | BlockDeclRefDecls.push_back(BDRE); |
| 701 | |
| 702 | BlockOffset += Size; |
| 703 | return BlockOffset-Size; |
| 704 | } |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 705 | |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 706 | llvm::Constant *BlockFunction:: |
| 707 | GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 708 | std::vector<HelperInfo> *NoteForHelperp) { |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 709 | QualType R = getContext().VoidTy; |
| 710 | |
| 711 | FunctionArgList Args; |
| 712 | // FIXME: This leaks |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 713 | ImplicitParamDecl *Dst = |
| 714 | ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, |
| 715 | getContext().getPointerType(getContext().VoidTy)); |
| 716 | Args.push_back(std::make_pair(Dst, Dst->getType())); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 717 | ImplicitParamDecl *Src = |
| 718 | ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, |
| 719 | getContext().getPointerType(getContext().VoidTy)); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 720 | Args.push_back(std::make_pair(Src, Src->getType())); |
| 721 | |
| 722 | const CGFunctionInfo &FI = |
| 723 | CGM.getTypes().getFunctionInfo(R, Args); |
| 724 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 725 | // FIXME: We'd like to put these into a mergable by content, with |
| 726 | // internal linkage. |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 727 | std::string Name = std::string("__copy_helper_block_"); |
| 728 | CodeGenTypes &Types = CGM.getTypes(); |
| 729 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); |
| 730 | |
| 731 | llvm::Function *Fn = |
| 732 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
| 733 | Name, |
| 734 | &CGM.getModule()); |
| 735 | |
| 736 | IdentifierInfo *II |
| 737 | = &CGM.getContext().Idents.get("__copy_helper_block_"); |
| 738 | |
| 739 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 740 | getContext().getTranslationUnitDecl(), |
| 741 | SourceLocation(), II, R, |
| 742 | FunctionDecl::Static, false, |
| 743 | true); |
| 744 | CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 745 | |
| 746 | llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); |
| 747 | llvm::Type *PtrPtrT; |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 748 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 749 | if (NoteForHelperp) { |
| 750 | std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp; |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 751 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 752 | PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); |
| 753 | SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); |
| 754 | SrcObj = Builder.CreateLoad(SrcObj); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 755 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 756 | llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst); |
Mike Stump | c2f4c34 | 2009-04-15 22:11:36 +0000 | [diff] [blame] | 757 | llvm::Type *PtrPtrT; |
| 758 | PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); |
| 759 | DstObj = Builder.CreateBitCast(DstObj, PtrPtrT); |
| 760 | DstObj = Builder.CreateLoad(DstObj); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 761 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 762 | for (unsigned i=0; i < NoteForHelper.size(); ++i) { |
| 763 | int flag = NoteForHelper[i].flag; |
| 764 | int index = NoteForHelper[i].index; |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 765 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 766 | if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) |
| 767 | || NoteForHelper[i].RequiresCopying) { |
| 768 | llvm::Value *Srcv = SrcObj; |
| 769 | Srcv = Builder.CreateStructGEP(Srcv, index); |
| 770 | Srcv = Builder.CreateBitCast(Srcv, |
| 771 | llvm::PointerType::get(PtrToInt8Ty, 0)); |
| 772 | Srcv = Builder.CreateLoad(Srcv); |
| 773 | |
| 774 | llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index); |
| 775 | Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty); |
| 776 | |
| 777 | llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag); |
| 778 | llvm::Value *F = getBlockObjectAssign(); |
| 779 | Builder.CreateCall3(F, Dstv, Srcv, N); |
| 780 | } |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 784 | CGF.FinishFunction(); |
| 785 | |
| 786 | return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Mike Stump | cf62d39 | 2009-03-06 18:42:23 +0000 | [diff] [blame] | 789 | llvm::Constant *BlockFunction:: |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 790 | GenerateDestroyHelperFunction(bool BlockHasCopyDispose, |
| 791 | const llvm::StructType* T, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 792 | std::vector<HelperInfo> *NoteForHelperp) { |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 793 | QualType R = getContext().VoidTy; |
| 794 | |
| 795 | FunctionArgList Args; |
| 796 | // FIXME: This leaks |
| 797 | ImplicitParamDecl *Src = |
| 798 | ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, |
| 799 | getContext().getPointerType(getContext().VoidTy)); |
| 800 | |
| 801 | Args.push_back(std::make_pair(Src, Src->getType())); |
| 802 | |
| 803 | const CGFunctionInfo &FI = |
| 804 | CGM.getTypes().getFunctionInfo(R, Args); |
| 805 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 806 | // FIXME: We'd like to put these into a mergable by content, with |
| 807 | // internal linkage. |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 808 | std::string Name = std::string("__destroy_helper_block_"); |
| 809 | CodeGenTypes &Types = CGM.getTypes(); |
| 810 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); |
| 811 | |
| 812 | llvm::Function *Fn = |
| 813 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
| 814 | Name, |
| 815 | &CGM.getModule()); |
| 816 | |
| 817 | IdentifierInfo *II |
| 818 | = &CGM.getContext().Idents.get("__destroy_helper_block_"); |
| 819 | |
| 820 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 821 | getContext().getTranslationUnitDecl(), |
| 822 | SourceLocation(), II, R, |
| 823 | FunctionDecl::Static, false, |
| 824 | true); |
| 825 | CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 826 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 827 | if (NoteForHelperp) { |
| 828 | std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp; |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 829 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 830 | llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); |
| 831 | llvm::Type *PtrPtrT; |
| 832 | PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); |
| 833 | SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); |
| 834 | SrcObj = Builder.CreateLoad(SrcObj); |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 835 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 836 | for (unsigned i=0; i < NoteForHelper.size(); ++i) { |
| 837 | int flag = NoteForHelper[i].flag; |
| 838 | int index = NoteForHelper[i].index; |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 839 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 840 | if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) |
| 841 | || NoteForHelper[i].RequiresCopying) { |
| 842 | llvm::Value *Srcv = SrcObj; |
| 843 | Srcv = Builder.CreateStructGEP(Srcv, index); |
| 844 | Srcv = Builder.CreateBitCast(Srcv, |
| 845 | llvm::PointerType::get(PtrToInt8Ty, 0)); |
| 846 | Srcv = Builder.CreateLoad(Srcv); |
| 847 | |
| 848 | BuildBlockRelease(Srcv, flag); |
| 849 | } |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 850 | } |
| 851 | } |
| 852 | |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 853 | CGF.FinishFunction(); |
| 854 | |
| 855 | return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); |
| 856 | } |
| 857 | |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 858 | llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 859 | std::vector<HelperInfo> *NoteForHelper) { |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 860 | return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose, |
| 861 | T, NoteForHelper); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 864 | llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 865 | std::vector<HelperInfo> *NoteForHelperp) { |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 866 | return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 867 | T, NoteForHelperp); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 868 | } |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 869 | |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 870 | llvm::Constant *BlockFunction:: |
| 871 | GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) { |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 872 | QualType R = getContext().VoidTy; |
| 873 | |
| 874 | FunctionArgList Args; |
| 875 | // FIXME: This leaks |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 876 | ImplicitParamDecl *Dst = |
| 877 | ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, |
| 878 | getContext().getPointerType(getContext().VoidTy)); |
| 879 | Args.push_back(std::make_pair(Dst, Dst->getType())); |
| 880 | |
| 881 | // FIXME: This leaks |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 882 | ImplicitParamDecl *Src = |
| 883 | ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, |
| 884 | getContext().getPointerType(getContext().VoidTy)); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 885 | Args.push_back(std::make_pair(Src, Src->getType())); |
| 886 | |
| 887 | const CGFunctionInfo &FI = |
| 888 | CGM.getTypes().getFunctionInfo(R, Args); |
| 889 | |
| 890 | std::string Name = std::string("__Block_byref_id_object_copy_"); |
| 891 | CodeGenTypes &Types = CGM.getTypes(); |
| 892 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); |
| 893 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 894 | // FIXME: We'd like to put these into a mergable by content, with |
| 895 | // internal linkage. |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 896 | llvm::Function *Fn = |
| 897 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
| 898 | Name, |
| 899 | &CGM.getModule()); |
| 900 | |
| 901 | IdentifierInfo *II |
| 902 | = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_"); |
| 903 | |
| 904 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 905 | getContext().getTranslationUnitDecl(), |
| 906 | SourceLocation(), II, R, |
| 907 | FunctionDecl::Static, false, |
| 908 | true); |
| 909 | CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 910 | |
| 911 | // dst->x |
| 912 | llvm::Value *V = CGF.GetAddrOfLocalVar(Dst); |
Mike Stump | c2f4c34 | 2009-04-15 22:11:36 +0000 | [diff] [blame] | 913 | V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0)); |
| 914 | V = Builder.CreateLoad(V); |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 915 | V = Builder.CreateStructGEP(V, 6, "x"); |
| 916 | llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty); |
| 917 | |
| 918 | // src->x |
| 919 | V = CGF.GetAddrOfLocalVar(Src); |
| 920 | V = Builder.CreateLoad(V); |
| 921 | V = Builder.CreateBitCast(V, T); |
| 922 | V = Builder.CreateStructGEP(V, 6, "x"); |
| 923 | V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); |
| 924 | llvm::Value *SrcObj = Builder.CreateLoad(V); |
| 925 | |
| 926 | flag |= BLOCK_BYREF_CALLER; |
| 927 | |
| 928 | llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag); |
| 929 | llvm::Value *F = getBlockObjectAssign(); |
| 930 | Builder.CreateCall3(F, DstObj, SrcObj, N); |
| 931 | |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 932 | CGF.FinishFunction(); |
| 933 | |
| 934 | return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); |
| 935 | } |
| 936 | |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 937 | llvm::Constant * |
| 938 | BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T, |
| 939 | int flag) { |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 940 | QualType R = getContext().VoidTy; |
| 941 | |
| 942 | FunctionArgList Args; |
| 943 | // FIXME: This leaks |
| 944 | ImplicitParamDecl *Src = |
| 945 | ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, |
| 946 | getContext().getPointerType(getContext().VoidTy)); |
| 947 | |
| 948 | Args.push_back(std::make_pair(Src, Src->getType())); |
| 949 | |
| 950 | const CGFunctionInfo &FI = |
| 951 | CGM.getTypes().getFunctionInfo(R, Args); |
| 952 | |
| 953 | std::string Name = std::string("__Block_byref_id_object_dispose_"); |
| 954 | CodeGenTypes &Types = CGM.getTypes(); |
| 955 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); |
| 956 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 957 | // FIXME: We'd like to put these into a mergable by content, with |
| 958 | // internal linkage. |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 959 | llvm::Function *Fn = |
| 960 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
| 961 | Name, |
| 962 | &CGM.getModule()); |
| 963 | |
| 964 | IdentifierInfo *II |
| 965 | = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_"); |
| 966 | |
| 967 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 968 | getContext().getTranslationUnitDecl(), |
| 969 | SourceLocation(), II, R, |
| 970 | FunctionDecl::Static, false, |
| 971 | true); |
| 972 | CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 973 | |
| 974 | llvm::Value *V = CGF.GetAddrOfLocalVar(Src); |
Mike Stump | c2f4c34 | 2009-04-15 22:11:36 +0000 | [diff] [blame] | 975 | V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0)); |
| 976 | V = Builder.CreateLoad(V); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 977 | V = Builder.CreateStructGEP(V, 6, "x"); |
Mike Stump | c2f4c34 | 2009-04-15 22:11:36 +0000 | [diff] [blame] | 978 | V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); |
| 979 | V = Builder.CreateLoad(V); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 980 | |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 981 | flag |= BLOCK_BYREF_CALLER; |
| 982 | BuildBlockRelease(V, flag); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 983 | CGF.FinishFunction(); |
| 984 | |
| 985 | return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); |
| 986 | } |
| 987 | |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 988 | llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T, |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 989 | int flag, unsigned Align) { |
| 990 | // All alignments below that of pointer alignment collpase down to just |
| 991 | // pointer alignment, as we always have at least that much alignment to begin |
| 992 | // with. |
| 993 | Align /= unsigned(CGF.Target.getPointerAlign(0)/8); |
| 994 | // As an optimization, we only generate a single function of each kind we |
| 995 | // might need. We need a different one for each alignment and for each |
| 996 | // setting of flags. We mix Align and flag to get the kind. |
| 997 | uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag; |
| 998 | llvm::Constant *& Entry = CGM.AssignCache[kind]; |
| 999 | if (Entry) |
| 1000 | return Entry; |
| 1001 | return Entry=CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1004 | llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T, |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1005 | int flag, |
| 1006 | unsigned Align) { |
| 1007 | // All alignments below that of pointer alignment collpase down to just |
| 1008 | // pointer alignment, as we always have at least that much alignment to begin |
| 1009 | // with. |
| 1010 | Align /= unsigned(CGF.Target.getPointerAlign(0)/8); |
| 1011 | // As an optimization, we only generate a single function of each kind we |
| 1012 | // might need. We need a different one for each alignment and for each |
| 1013 | // setting of flags. We mix Align and flag to get the kind. |
| 1014 | uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag; |
| 1015 | llvm::Constant *& Entry = CGM.DestroyCache[kind]; |
| 1016 | if (Entry) |
| 1017 | return Entry; |
| 1018 | return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1021 | llvm::Value *BlockFunction::getBlockObjectDispose() { |
| 1022 | if (CGM.BlockObjectDispose == 0) { |
| 1023 | const llvm::FunctionType *FTy; |
| 1024 | std::vector<const llvm::Type*> ArgTys; |
| 1025 | const llvm::Type *ResultType = llvm::Type::VoidTy; |
| 1026 | ArgTys.push_back(PtrToInt8Ty); |
| 1027 | ArgTys.push_back(llvm::Type::Int32Ty); |
| 1028 | FTy = llvm::FunctionType::get(ResultType, ArgTys, false); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 1029 | CGM.BlockObjectDispose |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1030 | = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose"); |
| 1031 | } |
| 1032 | return CGM.BlockObjectDispose; |
| 1033 | } |
| 1034 | |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1035 | llvm::Value *BlockFunction::getBlockObjectAssign() { |
| 1036 | if (CGM.BlockObjectAssign == 0) { |
| 1037 | const llvm::FunctionType *FTy; |
| 1038 | std::vector<const llvm::Type*> ArgTys; |
| 1039 | const llvm::Type *ResultType = llvm::Type::VoidTy; |
| 1040 | ArgTys.push_back(PtrToInt8Ty); |
| 1041 | ArgTys.push_back(PtrToInt8Ty); |
| 1042 | ArgTys.push_back(llvm::Type::Int32Ty); |
| 1043 | FTy = llvm::FunctionType::get(ResultType, ArgTys, false); |
| 1044 | CGM.BlockObjectAssign |
| 1045 | = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign"); |
| 1046 | } |
| 1047 | return CGM.BlockObjectAssign; |
| 1048 | } |
| 1049 | |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1050 | void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) { |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1051 | llvm::Value *F = getBlockObjectDispose(); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1052 | llvm::Value *N; |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1053 | V = Builder.CreateBitCast(V, PtrToInt8Ty); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1054 | N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag); |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1055 | Builder.CreateCall2(F, V, N); |
| 1056 | } |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 1057 | |
| 1058 | ASTContext &BlockFunction::getContext() const { return CGM.getContext(); } |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1059 | |
| 1060 | BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf, |
| 1061 | CGBuilderTy &B) |
| 1062 | : CGM(cgm), CGF(cgf), Builder(B) { |
| 1063 | PtrToInt8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 1064 | |
| 1065 | BlockHasCopyDispose = false; |
| 1066 | } |