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