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