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