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