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