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