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 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 626 | const FunctionProtoType *FTy = |
Chris Lattner | 161d36d | 2009-02-28 19:01:03 +0000 | [diff] [blame] | 627 | cast<FunctionProtoType>(BExpr->getFunctionType()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 628 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 629 | FunctionArgList Args; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 630 | |
Chris Lattner | 161d36d | 2009-02-28 19:01:03 +0000 | [diff] [blame] | 631 | const BlockDecl *BD = BExpr->getBlockDecl(); |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 632 | |
| 633 | // FIXME: This leaks |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 634 | ImplicitParamDecl *SelfDecl = |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 635 | ImplicitParamDecl::Create(getContext(), 0, |
| 636 | SourceLocation(), 0, |
| 637 | getContext().getPointerType(getContext().VoidTy)); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 638 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 639 | Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType())); |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 640 | BlockStructDecl = SelfDecl; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 641 | |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 642 | for (BlockDecl::param_const_iterator i = BD->param_begin(), |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 643 | e = BD->param_end(); i != e; ++i) |
Mike Stump | 1905061 | 2009-02-17 23:25:52 +0000 | [diff] [blame] | 644 | Args.push_back(std::make_pair(*i, (*i)->getType())); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 645 | |
| 646 | const CGFunctionInfo &FI = |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 647 | CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args); |
| 648 | |
Mike Stump | 67a6448 | 2009-02-14 22:16:35 +0000 | [diff] [blame] | 649 | std::string Name = std::string("__") + Info.Name + "_block_invoke_"; |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 650 | CodeGenTypes &Types = CGM.getTypes(); |
| 651 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 652 | |
| 653 | llvm::Function *Fn = |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 654 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
| 655 | Name, |
| 656 | &CGM.getModule()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 657 | |
| 658 | StartFunction(BD, FTy->getResultType(), Fn, Args, |
Chris Lattner | 161d36d | 2009-02-28 19:01:03 +0000 | [diff] [blame] | 659 | BExpr->getBody()->getLocEnd()); |
Mike Stump | 6cc88f7 | 2009-03-20 21:53:12 +0000 | [diff] [blame] | 660 | CurFuncDecl = OuterFuncDecl; |
Chris Lattner | 161d36d | 2009-02-28 19:01:03 +0000 | [diff] [blame] | 661 | EmitStmt(BExpr->getBody()); |
| 662 | FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc()); |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 663 | |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 664 | // The runtime needs a minimum alignment of a void *. |
| 665 | uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; |
| 666 | BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign); |
| 667 | |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 668 | Size = BlockOffset; |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 669 | Align = BlockAlign; |
| 670 | subBlockDeclRefDecls = BlockDeclRefDecls; |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 671 | subBlockHasCopyDispose |= BlockHasCopyDispose; |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 672 | return Fn; |
| 673 | } |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 674 | |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 675 | uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) { |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 676 | const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl()); |
| 677 | |
| 678 | uint64_t Size = getContext().getTypeSize(D->getType()) / 8; |
| 679 | uint64_t Align = getContext().getDeclAlignInBytes(D); |
| 680 | |
| 681 | if (BDRE->isByRef()) { |
| 682 | Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8; |
| 683 | Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; |
| 684 | } |
| 685 | |
| 686 | assert ((Align > 0) && "alignment must be 1 byte or more"); |
| 687 | |
| 688 | uint64_t OldOffset = BlockOffset; |
| 689 | |
| 690 | // Ensure proper alignment, even if it means we have to have a gap |
| 691 | BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align); |
| 692 | BlockAlign = std::max(Align, BlockAlign); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 693 | |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 694 | uint64_t Pad = BlockOffset - OldOffset; |
| 695 | if (Pad) { |
| 696 | llvm::ArrayType::get(llvm::Type::Int8Ty, Pad); |
| 697 | QualType PadTy = getContext().getConstantArrayType(getContext().CharTy, |
| 698 | llvm::APInt(32, Pad), |
| 699 | ArrayType::Normal, 0); |
| 700 | ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(), |
| 701 | 0, QualType(PadTy), VarDecl::None, |
| 702 | SourceLocation()); |
| 703 | Expr *E; |
| 704 | E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(), |
| 705 | SourceLocation(), false, false); |
| 706 | BlockDeclRefDecls.push_back(E); |
| 707 | } |
| 708 | BlockDeclRefDecls.push_back(BDRE); |
| 709 | |
| 710 | BlockOffset += Size; |
| 711 | return BlockOffset-Size; |
| 712 | } |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 713 | |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 714 | llvm::Constant *BlockFunction:: |
| 715 | GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame^] | 716 | std::vector<HelperInfo> *NoteForHelperp) { |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 717 | QualType R = getContext().VoidTy; |
| 718 | |
| 719 | FunctionArgList Args; |
| 720 | // FIXME: This leaks |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 721 | ImplicitParamDecl *Dst = |
| 722 | ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, |
| 723 | getContext().getPointerType(getContext().VoidTy)); |
| 724 | Args.push_back(std::make_pair(Dst, Dst->getType())); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 725 | ImplicitParamDecl *Src = |
| 726 | ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, |
| 727 | getContext().getPointerType(getContext().VoidTy)); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 728 | Args.push_back(std::make_pair(Src, Src->getType())); |
| 729 | |
| 730 | const CGFunctionInfo &FI = |
| 731 | CGM.getTypes().getFunctionInfo(R, Args); |
| 732 | |
| 733 | std::string Name = std::string("__copy_helper_block_"); |
| 734 | CodeGenTypes &Types = CGM.getTypes(); |
| 735 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); |
| 736 | |
| 737 | llvm::Function *Fn = |
| 738 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
| 739 | Name, |
| 740 | &CGM.getModule()); |
| 741 | |
| 742 | IdentifierInfo *II |
| 743 | = &CGM.getContext().Idents.get("__copy_helper_block_"); |
| 744 | |
| 745 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 746 | getContext().getTranslationUnitDecl(), |
| 747 | SourceLocation(), II, R, |
| 748 | FunctionDecl::Static, false, |
| 749 | true); |
| 750 | CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 751 | |
| 752 | llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); |
| 753 | llvm::Type *PtrPtrT; |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 754 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame^] | 755 | if (NoteForHelperp) { |
| 756 | std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp; |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 757 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame^] | 758 | PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); |
| 759 | SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); |
| 760 | SrcObj = Builder.CreateLoad(SrcObj); |
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 | llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst); |
| 763 | DstObj = Builder.CreateBitCast(DstObj, llvm::PointerType::get(T, 0)); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 764 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame^] | 765 | for (unsigned i=0; i < NoteForHelper.size(); ++i) { |
| 766 | int flag = NoteForHelper[i].flag; |
| 767 | int index = NoteForHelper[i].index; |
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 | if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) |
| 770 | || NoteForHelper[i].RequiresCopying) { |
| 771 | llvm::Value *Srcv = SrcObj; |
| 772 | Srcv = Builder.CreateStructGEP(Srcv, index); |
| 773 | Srcv = Builder.CreateBitCast(Srcv, |
| 774 | llvm::PointerType::get(PtrToInt8Ty, 0)); |
| 775 | Srcv = Builder.CreateLoad(Srcv); |
| 776 | |
| 777 | llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index); |
| 778 | Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty); |
| 779 | |
| 780 | llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag); |
| 781 | llvm::Value *F = getBlockObjectAssign(); |
| 782 | Builder.CreateCall3(F, Dstv, Srcv, N); |
| 783 | } |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 787 | CGF.FinishFunction(); |
| 788 | |
| 789 | return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 790 | } |
| 791 | |
Mike Stump | cf62d39 | 2009-03-06 18:42:23 +0000 | [diff] [blame] | 792 | llvm::Constant *BlockFunction:: |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 793 | GenerateDestroyHelperFunction(bool BlockHasCopyDispose, |
| 794 | const llvm::StructType* T, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame^] | 795 | std::vector<HelperInfo> *NoteForHelperp) { |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 796 | QualType R = getContext().VoidTy; |
| 797 | |
| 798 | FunctionArgList Args; |
| 799 | // FIXME: This leaks |
| 800 | ImplicitParamDecl *Src = |
| 801 | ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, |
| 802 | getContext().getPointerType(getContext().VoidTy)); |
| 803 | |
| 804 | Args.push_back(std::make_pair(Src, Src->getType())); |
| 805 | |
| 806 | const CGFunctionInfo &FI = |
| 807 | CGM.getTypes().getFunctionInfo(R, Args); |
| 808 | |
| 809 | std::string Name = std::string("__destroy_helper_block_"); |
| 810 | CodeGenTypes &Types = CGM.getTypes(); |
| 811 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); |
| 812 | |
| 813 | llvm::Function *Fn = |
| 814 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
| 815 | Name, |
| 816 | &CGM.getModule()); |
| 817 | |
| 818 | IdentifierInfo *II |
| 819 | = &CGM.getContext().Idents.get("__destroy_helper_block_"); |
| 820 | |
| 821 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 822 | getContext().getTranslationUnitDecl(), |
| 823 | SourceLocation(), II, R, |
| 824 | FunctionDecl::Static, false, |
| 825 | true); |
| 826 | CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 827 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame^] | 828 | if (NoteForHelperp) { |
| 829 | std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp; |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 830 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame^] | 831 | llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); |
| 832 | llvm::Type *PtrPtrT; |
| 833 | PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); |
| 834 | SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); |
| 835 | SrcObj = Builder.CreateLoad(SrcObj); |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 836 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame^] | 837 | for (unsigned i=0; i < NoteForHelper.size(); ++i) { |
| 838 | int flag = NoteForHelper[i].flag; |
| 839 | int index = NoteForHelper[i].index; |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 840 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame^] | 841 | if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) |
| 842 | || NoteForHelper[i].RequiresCopying) { |
| 843 | llvm::Value *Srcv = SrcObj; |
| 844 | Srcv = Builder.CreateStructGEP(Srcv, index); |
| 845 | Srcv = Builder.CreateBitCast(Srcv, |
| 846 | llvm::PointerType::get(PtrToInt8Ty, 0)); |
| 847 | Srcv = Builder.CreateLoad(Srcv); |
| 848 | |
| 849 | BuildBlockRelease(Srcv, flag); |
| 850 | } |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 854 | CGF.FinishFunction(); |
| 855 | |
| 856 | return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); |
| 857 | } |
| 858 | |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 859 | llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame^] | 860 | std::vector<HelperInfo> *NoteForHelper) { |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 861 | return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose, |
| 862 | T, NoteForHelper); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 865 | llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame^] | 866 | std::vector<HelperInfo> *NoteForHelperp) { |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 867 | return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame^] | 868 | T, NoteForHelperp); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 869 | } |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 870 | |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 871 | llvm::Constant *BlockFunction:: |
| 872 | GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) { |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 873 | QualType R = getContext().VoidTy; |
| 874 | |
| 875 | FunctionArgList Args; |
| 876 | // FIXME: This leaks |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 877 | ImplicitParamDecl *Dst = |
| 878 | ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, |
| 879 | getContext().getPointerType(getContext().VoidTy)); |
| 880 | Args.push_back(std::make_pair(Dst, Dst->getType())); |
| 881 | |
| 882 | // FIXME: This leaks |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 883 | ImplicitParamDecl *Src = |
| 884 | ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, |
| 885 | getContext().getPointerType(getContext().VoidTy)); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 886 | Args.push_back(std::make_pair(Src, Src->getType())); |
| 887 | |
| 888 | const CGFunctionInfo &FI = |
| 889 | CGM.getTypes().getFunctionInfo(R, Args); |
| 890 | |
| 891 | std::string Name = std::string("__Block_byref_id_object_copy_"); |
| 892 | CodeGenTypes &Types = CGM.getTypes(); |
| 893 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); |
| 894 | |
| 895 | llvm::Function *Fn = |
| 896 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
| 897 | Name, |
| 898 | &CGM.getModule()); |
| 899 | |
| 900 | IdentifierInfo *II |
| 901 | = &CGM.getContext().Idents.get("__Block_byref_id_object_copy_"); |
| 902 | |
| 903 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 904 | getContext().getTranslationUnitDecl(), |
| 905 | SourceLocation(), II, R, |
| 906 | FunctionDecl::Static, false, |
| 907 | true); |
| 908 | CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 909 | |
| 910 | // dst->x |
| 911 | llvm::Value *V = CGF.GetAddrOfLocalVar(Dst); |
| 912 | V = Builder.CreateBitCast(V, T); |
| 913 | V = Builder.CreateStructGEP(V, 6, "x"); |
| 914 | llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty); |
| 915 | |
| 916 | // src->x |
| 917 | V = CGF.GetAddrOfLocalVar(Src); |
| 918 | V = Builder.CreateLoad(V); |
| 919 | V = Builder.CreateBitCast(V, T); |
| 920 | V = Builder.CreateStructGEP(V, 6, "x"); |
| 921 | V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); |
| 922 | llvm::Value *SrcObj = Builder.CreateLoad(V); |
| 923 | |
| 924 | flag |= BLOCK_BYREF_CALLER; |
| 925 | |
| 926 | llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag); |
| 927 | llvm::Value *F = getBlockObjectAssign(); |
| 928 | Builder.CreateCall3(F, DstObj, SrcObj, N); |
| 929 | |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 930 | CGF.FinishFunction(); |
| 931 | |
| 932 | return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); |
| 933 | } |
| 934 | |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 935 | llvm::Constant * |
| 936 | BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T, |
| 937 | int flag) { |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 938 | QualType R = getContext().VoidTy; |
| 939 | |
| 940 | FunctionArgList Args; |
| 941 | // FIXME: This leaks |
| 942 | ImplicitParamDecl *Src = |
| 943 | ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, |
| 944 | getContext().getPointerType(getContext().VoidTy)); |
| 945 | |
| 946 | Args.push_back(std::make_pair(Src, Src->getType())); |
| 947 | |
| 948 | const CGFunctionInfo &FI = |
| 949 | CGM.getTypes().getFunctionInfo(R, Args); |
| 950 | |
| 951 | std::string Name = std::string("__Block_byref_id_object_dispose_"); |
| 952 | CodeGenTypes &Types = CGM.getTypes(); |
| 953 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); |
| 954 | |
| 955 | llvm::Function *Fn = |
| 956 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
| 957 | Name, |
| 958 | &CGM.getModule()); |
| 959 | |
| 960 | IdentifierInfo *II |
| 961 | = &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_"); |
| 962 | |
| 963 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 964 | getContext().getTranslationUnitDecl(), |
| 965 | SourceLocation(), II, R, |
| 966 | FunctionDecl::Static, false, |
| 967 | true); |
| 968 | CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 969 | |
| 970 | llvm::Value *V = CGF.GetAddrOfLocalVar(Src); |
| 971 | V = Builder.CreateBitCast(V, T); |
| 972 | V = Builder.CreateStructGEP(V, 6, "x"); |
| 973 | V = Builder.CreateBitCast(V, PtrToInt8Ty); |
| 974 | |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 975 | flag |= BLOCK_BYREF_CALLER; |
| 976 | BuildBlockRelease(V, flag); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 977 | CGF.FinishFunction(); |
| 978 | |
| 979 | return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); |
| 980 | } |
| 981 | |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 982 | llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T, |
| 983 | int flag) { |
| 984 | return CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 985 | } |
| 986 | |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 987 | llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T, |
| 988 | int flag) { |
| 989 | return CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 990 | } |
| 991 | |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 992 | llvm::Value *BlockFunction::getBlockObjectDispose() { |
| 993 | if (CGM.BlockObjectDispose == 0) { |
| 994 | const llvm::FunctionType *FTy; |
| 995 | std::vector<const llvm::Type*> ArgTys; |
| 996 | const llvm::Type *ResultType = llvm::Type::VoidTy; |
| 997 | ArgTys.push_back(PtrToInt8Ty); |
| 998 | ArgTys.push_back(llvm::Type::Int32Ty); |
| 999 | FTy = llvm::FunctionType::get(ResultType, ArgTys, false); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 1000 | CGM.BlockObjectDispose |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1001 | = CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose"); |
| 1002 | } |
| 1003 | return CGM.BlockObjectDispose; |
| 1004 | } |
| 1005 | |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1006 | llvm::Value *BlockFunction::getBlockObjectAssign() { |
| 1007 | if (CGM.BlockObjectAssign == 0) { |
| 1008 | const llvm::FunctionType *FTy; |
| 1009 | std::vector<const llvm::Type*> ArgTys; |
| 1010 | const llvm::Type *ResultType = llvm::Type::VoidTy; |
| 1011 | ArgTys.push_back(PtrToInt8Ty); |
| 1012 | ArgTys.push_back(PtrToInt8Ty); |
| 1013 | ArgTys.push_back(llvm::Type::Int32Ty); |
| 1014 | FTy = llvm::FunctionType::get(ResultType, ArgTys, false); |
| 1015 | CGM.BlockObjectAssign |
| 1016 | = CGM.CreateRuntimeFunction(FTy, "_Block_object_assign"); |
| 1017 | } |
| 1018 | return CGM.BlockObjectAssign; |
| 1019 | } |
| 1020 | |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1021 | void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) { |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1022 | llvm::Value *F = getBlockObjectDispose(); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1023 | llvm::Value *N; |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1024 | V = Builder.CreateBitCast(V, PtrToInt8Ty); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1025 | N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag); |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1026 | Builder.CreateCall2(F, V, N); |
| 1027 | } |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 1028 | |
| 1029 | ASTContext &BlockFunction::getContext() const { return CGM.getContext(); } |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1030 | |
| 1031 | BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf, |
| 1032 | CGBuilderTy &B) |
| 1033 | : CGM(cgm), CGF(cgf), Builder(B) { |
| 1034 | PtrToInt8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 1035 | |
| 1036 | BlockHasCopyDispose = false; |
| 1037 | } |