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