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