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