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