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