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 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 24 | enum { |
Mike Stump | e5fee25 | 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 | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 33 | llvm::Constant *CodeGenFunction::BuildDescriptorBlockDecl(uint64_t Size) { |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 34 | const llvm::PointerType *PtrToInt8Ty |
| 35 | = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
Mike Stump | 56129b1 | 2009-02-13 16:55:51 +0000 | [diff] [blame] | 36 | const llvm::Type *UnsignedLongTy |
| 37 | = CGM.getTypes().ConvertType(getContext().UnsignedLongTy); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 38 | llvm::Constant *C; |
| 39 | std::vector<llvm::Constant*> Elts; |
| 40 | |
| 41 | // reserved |
Mike Stump | 56129b1 | 2009-02-13 16:55:51 +0000 | [diff] [blame] | 42 | C = llvm::ConstantInt::get(UnsignedLongTy, 0); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 43 | Elts.push_back(C); |
| 44 | |
| 45 | // Size |
Mike Stump | d684000 | 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 | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 49 | C = llvm::ConstantInt::get(UnsignedLongTy, Size); |
Mike Stump | e5fee25 | 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 | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 54 | // FIXME: implement |
Mike Stump | 56129b1 | 2009-02-13 16:55:51 +0000 | [diff] [blame] | 55 | C = llvm::ConstantInt::get(UnsignedLongTy, 0); |
Mike Stump | e5fee25 | 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 | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 60 | // FIXME: implement |
Mike Stump | 56129b1 | 2009-02-13 16:55:51 +0000 | [diff] [blame] | 61 | C = llvm::ConstantInt::get(UnsignedLongTy, 0); |
Mike Stump | e5fee25 | 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 | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 68 | C = new llvm::GlobalVariable(C->getType(), true, |
| 69 | llvm::GlobalValue::InternalLinkage, |
Mike Stump | 7d6dc4f | 2009-02-13 20:17:16 +0000 | [diff] [blame] | 70 | C, "__block_descriptor_tmp", &CGM.getModule()); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 71 | return C; |
| 72 | } |
| 73 | |
Mike Stump | f99f1d0 | 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 | f744895 | 2009-02-13 19:38:12 +0000 | [diff] [blame] | 80 | // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the |
Mike Stump | f99f1d0 | 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 | 59c5b11 | 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 | f744895 | 2009-02-13 19:38:12 +0000 | [diff] [blame] | 98 | // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the |
Mike Stump | 59c5b11 | 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 | 4de9fce | 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 | |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 135 | // FIXME: Push most into CGM, passing down a few bits, like current |
| 136 | // function name. |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 137 | llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) { |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 138 | |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame^] | 139 | std::string Name = CurFn->getName(); |
| 140 | CodeGenFunction::BlockInfo Info(0, Name.c_str()); |
| 141 | CollectBlockDeclRefInfo(BE->getBody(), Info); |
| 142 | |
| 143 | // Check if the block can be global. |
| 144 | if (CanBlockBeGlobal(Info)) |
| 145 | return CGM.GetAddrOfGlobalBlock(BE, Name.c_str()); |
| 146 | |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 147 | std::vector<llvm::Constant*> Elts; |
| 148 | llvm::Constant *C; |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 149 | llvm::Value *V; |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 150 | |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 151 | { |
| 152 | // C = BuildBlockStructInitlist(); |
| 153 | unsigned int flags = BLOCK_HAS_DESCRIPTOR; |
| 154 | |
| 155 | if (BlockHasCopyDispose) |
| 156 | flags |= BLOCK_HAS_COPY_DISPOSE; |
| 157 | |
Mike Stump | 7d6dc4f | 2009-02-13 20:17:16 +0000 | [diff] [blame] | 158 | // __isa |
Mike Stump | 59c5b11 | 2009-02-13 19:29:27 +0000 | [diff] [blame] | 159 | C = CGM.getNSConcreteStackBlock(); |
Mike Stump | 59c5b11 | 2009-02-13 19:29:27 +0000 | [diff] [blame] | 160 | const llvm::PointerType *PtrToInt8Ty |
| 161 | = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 162 | C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty); |
| 163 | Elts.push_back(C); |
| 164 | |
| 165 | // __flags |
| 166 | const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( |
| 167 | CGM.getTypes().ConvertType(CGM.getContext().IntTy)); |
| 168 | C = llvm::ConstantInt::get(IntTy, flags); |
| 169 | Elts.push_back(C); |
| 170 | |
| 171 | // __reserved |
| 172 | C = llvm::ConstantInt::get(IntTy, 0); |
| 173 | Elts.push_back(C); |
| 174 | |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 175 | // __invoke |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 176 | uint64_t subBlockSize, subBlockAlign; |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 177 | llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 178 | llvm::Function *Fn |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 179 | = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, subBlockSize, |
| 180 | subBlockAlign, subBlockDeclRefDecls); |
Mike Stump | 67a6448 | 2009-02-14 22:16:35 +0000 | [diff] [blame] | 181 | Elts.push_back(Fn); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 182 | |
| 183 | // __descriptor |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 184 | Elts.push_back(BuildDescriptorBlockDecl(subBlockSize)); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 185 | |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 186 | if (subBlockDeclRefDecls.size() == 0) { |
| 187 | C = llvm::ConstantStruct::get(Elts); |
| 188 | |
| 189 | char Name[32]; |
| 190 | sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount()); |
| 191 | C = new llvm::GlobalVariable(C->getType(), true, |
| 192 | llvm::GlobalValue::InternalLinkage, |
| 193 | C, Name, &CGM.getModule()); |
| 194 | QualType BPT = BE->getType(); |
| 195 | C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT)); |
| 196 | return C; |
| 197 | } |
| 198 | |
| 199 | std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size()); |
| 200 | for (int i=0; i<5; ++i) |
| 201 | Types[i] = Elts[i]->getType(); |
| 202 | |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 203 | for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) { |
| 204 | const Expr *E = subBlockDeclRefDecls[i]; |
| 205 | const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); |
| 206 | QualType Ty = E->getType(); |
| 207 | if (BDRE && BDRE->isByRef()) |
| 208 | Ty = getContext().getPointerType(Ty); |
| 209 | Types[i+5] = ConvertType(Ty); |
| 210 | } |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 211 | |
| 212 | llvm::Type *Ty = llvm::StructType::get(Types, true); |
| 213 | |
| 214 | llvm::AllocaInst *A = CreateTempAlloca(Ty); |
| 215 | A->setAlignment(subBlockAlign); |
| 216 | V = A; |
| 217 | |
| 218 | for (unsigned i=0; i<5; ++i) |
| 219 | Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp")); |
| 220 | |
| 221 | for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) |
| 222 | { |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 223 | // FIXME: Push const down. |
| 224 | Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]); |
| 225 | DeclRefExpr *DR; |
| 226 | ValueDecl *VD; |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 227 | |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 228 | DR = dyn_cast<DeclRefExpr>(E); |
| 229 | // Skip padding. |
| 230 | if (DR) continue; |
| 231 | |
| 232 | BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); |
| 233 | VD = BDRE->getDecl(); |
| 234 | |
| 235 | // FIXME: I want a better way to do this. |
| 236 | if (LocalDeclMap[VD]) { |
| 237 | E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD), |
| 238 | VD->getType(), SourceLocation(), |
| 239 | false, false); |
| 240 | } |
| 241 | if (BDRE->isByRef()) |
| 242 | E = new (getContext()) |
| 243 | UnaryOperator(E, UnaryOperator::AddrOf, |
| 244 | getContext().getPointerType(E->getType()), |
| 245 | SourceLocation()); |
| 246 | |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +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 | RValue r = EmitAnyExpr(E, Addr, false); |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 249 | if (r.isScalar()) |
| 250 | Builder.CreateStore(r.getScalarVal(), Addr); |
| 251 | else if (r.isComplex()) |
| 252 | // FIXME: implement |
| 253 | ErrorUnsupported(BE, "complex in block literal"); |
| 254 | else if (r.isAggregate()) |
| 255 | ; // Already created into the destination |
| 256 | else |
| 257 | assert (0 && "bad block variable"); |
| 258 | // FIXME: Ensure that the offset created by the backend for |
| 259 | // the struct matches the previously computed offset in BlockDecls. |
| 260 | } |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 263 | QualType BPT = BE->getType(); |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 264 | return Builder.CreateBitCast(V, ConvertType(BPT)); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 268 | const llvm::Type *CodeGenModule::getBlockDescriptorType() { |
| 269 | if (BlockDescriptorType) |
| 270 | return BlockDescriptorType; |
| 271 | |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 272 | const llvm::Type *UnsignedLongTy = |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 273 | getTypes().ConvertType(getContext().UnsignedLongTy); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 274 | |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 275 | // struct __block_descriptor { |
| 276 | // unsigned long reserved; |
| 277 | // unsigned long block_size; |
| 278 | // }; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 279 | BlockDescriptorType = llvm::StructType::get(UnsignedLongTy, |
| 280 | UnsignedLongTy, |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 281 | NULL); |
| 282 | |
| 283 | getModule().addTypeName("struct.__block_descriptor", |
| 284 | BlockDescriptorType); |
| 285 | |
| 286 | return BlockDescriptorType; |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 289 | const llvm::Type * |
| 290 | CodeGenModule::getGenericBlockLiteralType() { |
| 291 | if (GenericBlockLiteralType) |
| 292 | return GenericBlockLiteralType; |
| 293 | |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 294 | const llvm::Type *Int8PtrTy = |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 295 | llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 296 | |
| 297 | const llvm::Type *BlockDescPtrTy = |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 298 | llvm::PointerType::getUnqual(getBlockDescriptorType()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 299 | |
Mike Stump | 7cbb360 | 2009-02-13 16:01:35 +0000 | [diff] [blame] | 300 | const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( |
| 301 | getTypes().ConvertType(getContext().IntTy)); |
| 302 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 303 | // struct __block_literal_generic { |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 304 | // void *__isa; |
| 305 | // int __flags; |
| 306 | // int __reserved; |
| 307 | // void (*__invoke)(void *); |
| 308 | // struct __block_descriptor *__descriptor; |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 309 | // }; |
| 310 | GenericBlockLiteralType = llvm::StructType::get(Int8PtrTy, |
Mike Stump | 7cbb360 | 2009-02-13 16:01:35 +0000 | [diff] [blame] | 311 | IntTy, |
| 312 | IntTy, |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 313 | Int8PtrTy, |
| 314 | BlockDescPtrTy, |
| 315 | NULL); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 316 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 317 | getModule().addTypeName("struct.__block_literal_generic", |
| 318 | GenericBlockLiteralType); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 319 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 320 | return GenericBlockLiteralType; |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 323 | const llvm::Type * |
| 324 | CodeGenModule::getGenericExtendedBlockLiteralType() { |
| 325 | if (GenericExtendedBlockLiteralType) |
| 326 | return GenericExtendedBlockLiteralType; |
| 327 | |
| 328 | const llvm::Type *Int8PtrTy = |
| 329 | llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 330 | |
| 331 | const llvm::Type *BlockDescPtrTy = |
| 332 | llvm::PointerType::getUnqual(getBlockDescriptorType()); |
| 333 | |
| 334 | const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( |
| 335 | getTypes().ConvertType(getContext().IntTy)); |
| 336 | |
| 337 | // struct __block_literal_generic { |
| 338 | // void *__isa; |
| 339 | // int __flags; |
| 340 | // int __reserved; |
| 341 | // void (*__invoke)(void *); |
| 342 | // struct __block_descriptor *__descriptor; |
| 343 | // void *__copy_func_helper_decl; |
| 344 | // void *__destroy_func_decl; |
| 345 | // }; |
| 346 | GenericExtendedBlockLiteralType = llvm::StructType::get(Int8PtrTy, |
| 347 | IntTy, |
| 348 | IntTy, |
| 349 | Int8PtrTy, |
| 350 | BlockDescPtrTy, |
| 351 | Int8PtrTy, |
| 352 | Int8PtrTy, |
| 353 | NULL); |
| 354 | |
| 355 | getModule().addTypeName("struct.__block_literal_extended_generic", |
| 356 | GenericExtendedBlockLiteralType); |
| 357 | |
| 358 | return GenericExtendedBlockLiteralType; |
| 359 | } |
| 360 | |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 361 | /// getBlockFunctionType - Given a BlockPointerType, will return the |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 362 | /// function type for the block, including the first block literal argument. |
| 363 | static QualType getBlockFunctionType(ASTContext &Ctx, |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 364 | const BlockPointerType *BPT) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 365 | const FunctionProtoType *FTy = cast<FunctionProtoType>(BPT->getPointeeType()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 366 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 367 | llvm::SmallVector<QualType, 8> Types; |
| 368 | Types.push_back(Ctx.getPointerType(Ctx.VoidTy)); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 369 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 370 | for (FunctionProtoType::arg_type_iterator i = FTy->arg_type_begin(), |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 371 | e = FTy->arg_type_end(); i != e; ++i) |
| 372 | Types.push_back(*i); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 373 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 374 | return Ctx.getFunctionType(FTy->getResultType(), |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 375 | &Types[0], Types.size(), |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 376 | FTy->isVariadic(), 0); |
| 377 | } |
| 378 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 379 | RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) { |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 380 | const BlockPointerType *BPT = |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 381 | E->getCallee()->getType()->getAsBlockPointerType(); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 382 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 383 | llvm::Value *Callee = EmitScalarExpr(E->getCallee()); |
| 384 | |
| 385 | // Get a pointer to the generic block literal. |
| 386 | const llvm::Type *BlockLiteralTy = |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 387 | llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType()); |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 388 | |
| 389 | // Bitcast the callee to a block literal. |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 390 | llvm::Value *BlockLiteral = |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 391 | Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal"); |
| 392 | |
| 393 | // Get the function pointer from the literal. |
| 394 | llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp"); |
Mike Stump | 20733cd | 2009-02-22 13:27:11 +0000 | [diff] [blame] | 395 | llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp"); |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 396 | |
| 397 | // Cast the function pointer to the right type. |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 398 | const llvm::Type *BlockFTy = |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 399 | ConvertType(getBlockFunctionType(getContext(), BPT)); |
| 400 | const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy); |
| 401 | Func = Builder.CreateBitCast(Func, BlockFTyPtr); |
| 402 | |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 403 | BlockLiteral = |
| 404 | Builder.CreateBitCast(BlockLiteral, |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 405 | llvm::PointerType::getUnqual(llvm::Type::Int8Ty), |
| 406 | "tmp"); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 407 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 408 | // Add the block literal. |
| 409 | QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy); |
| 410 | CallArgList Args; |
| 411 | Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy)); |
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 | // And the rest of the arguments. |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 414 | 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] | 415 | i != e; ++i) |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 416 | Args.push_back(std::make_pair(EmitAnyExprToTemp(*i), |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 417 | i->getType())); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 418 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 419 | // And call the block. |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 420 | return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args), |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 421 | Func, Args); |
| 422 | } |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 423 | |
Mike Stump | 67a6448 | 2009-02-14 22:16:35 +0000 | [diff] [blame] | 424 | llvm::Constant * |
Mike Stump | 30395dd | 2009-02-14 22:49:33 +0000 | [diff] [blame] | 425 | CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) { |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 426 | // Generate the block descriptor. |
| 427 | const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy); |
Mike Stump | 7cbb360 | 2009-02-13 16:01:35 +0000 | [diff] [blame] | 428 | const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( |
| 429 | getTypes().ConvertType(getContext().IntTy)); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 430 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 431 | llvm::Constant *DescriptorFields[2]; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 432 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 433 | // Reserved |
| 434 | DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 435 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 436 | // Block literal size. For global blocks we just use the size of the generic |
| 437 | // block literal struct. |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 438 | uint64_t BlockLiteralSize = |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 439 | TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8; |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 440 | DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 441 | |
| 442 | llvm::Constant *DescriptorStruct = |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 443 | llvm::ConstantStruct::get(&DescriptorFields[0], 2); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 444 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 445 | llvm::GlobalVariable *Descriptor = |
| 446 | new llvm::GlobalVariable(DescriptorStruct->getType(), true, |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 447 | llvm::GlobalVariable::InternalLinkage, |
| 448 | DescriptorStruct, "__block_descriptor_global", |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 449 | &getModule()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 450 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 451 | // Generate the constants for the block literal. |
| 452 | llvm::Constant *LiteralFields[5]; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 453 | |
Mike Stump | 67a6448 | 2009-02-14 22:16:35 +0000 | [diff] [blame] | 454 | CodeGenFunction::BlockInfo Info(0, n); |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 455 | uint64_t subBlockSize, subBlockAlign; |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 456 | llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls; |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 457 | llvm::Function *Fn |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 458 | = CodeGenFunction(*this).GenerateBlockFunction(BE, Info, subBlockSize, |
| 459 | subBlockAlign, |
| 460 | subBlockDeclRefDecls); |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 461 | assert(subBlockSize == BlockLiteralSize |
| 462 | && "no imports allowed for global block"); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 463 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 464 | // isa |
Mike Stump | f99f1d0 | 2009-02-13 17:23:42 +0000 | [diff] [blame] | 465 | LiteralFields[0] = getNSConcreteGlobalBlock(); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 466 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 467 | // Flags |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 468 | LiteralFields[1] = llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 469 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 470 | // Reserved |
Mike Stump | 7cbb360 | 2009-02-13 16:01:35 +0000 | [diff] [blame] | 471 | LiteralFields[2] = llvm::Constant::getNullValue(IntTy); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 472 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 473 | // Function |
| 474 | LiteralFields[3] = Fn; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 475 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 476 | // Descriptor |
| 477 | LiteralFields[4] = Descriptor; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 478 | |
| 479 | llvm::Constant *BlockLiteralStruct = |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 480 | llvm::ConstantStruct::get(&LiteralFields[0], 5); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 481 | |
| 482 | llvm::GlobalVariable *BlockLiteral = |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 483 | new llvm::GlobalVariable(BlockLiteralStruct->getType(), true, |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 484 | llvm::GlobalVariable::InternalLinkage, |
| 485 | BlockLiteralStruct, "__block_literal_global", |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 486 | &getModule()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 487 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 488 | return BlockLiteral; |
| 489 | } |
| 490 | |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 491 | llvm::Value *CodeGenFunction::LoadBlockStruct() { |
| 492 | return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self"); |
| 493 | } |
| 494 | |
Chris Lattner | 161d36d | 2009-02-28 19:01:03 +0000 | [diff] [blame] | 495 | llvm::Function *CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr, |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 496 | const BlockInfo& Info, |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 497 | uint64_t &Size, |
| 498 | uint64_t &Align, |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 499 | llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 500 | const FunctionProtoType *FTy = |
Chris Lattner | 161d36d | 2009-02-28 19:01:03 +0000 | [diff] [blame] | 501 | cast<FunctionProtoType>(BExpr->getFunctionType()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 502 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 503 | FunctionArgList Args; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 504 | |
Chris Lattner | 161d36d | 2009-02-28 19:01:03 +0000 | [diff] [blame] | 505 | const BlockDecl *BD = BExpr->getBlockDecl(); |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 506 | |
| 507 | // FIXME: This leaks |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 508 | ImplicitParamDecl *SelfDecl = |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 509 | ImplicitParamDecl::Create(getContext(), 0, |
| 510 | SourceLocation(), 0, |
| 511 | getContext().getPointerType(getContext().VoidTy)); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 512 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 513 | Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType())); |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 514 | BlockStructDecl = SelfDecl; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 515 | |
| 516 | for (BlockDecl::param_iterator i = BD->param_begin(), |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 517 | e = BD->param_end(); i != e; ++i) |
Mike Stump | 1905061 | 2009-02-17 23:25:52 +0000 | [diff] [blame] | 518 | Args.push_back(std::make_pair(*i, (*i)->getType())); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 519 | |
| 520 | const CGFunctionInfo &FI = |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 521 | CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args); |
| 522 | |
Mike Stump | 67a6448 | 2009-02-14 22:16:35 +0000 | [diff] [blame] | 523 | std::string Name = std::string("__") + Info.Name + "_block_invoke_"; |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 524 | CodeGenTypes &Types = CGM.getTypes(); |
| 525 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 526 | |
| 527 | llvm::Function *Fn = |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 528 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
| 529 | Name, |
| 530 | &CGM.getModule()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 531 | |
| 532 | StartFunction(BD, FTy->getResultType(), Fn, Args, |
Chris Lattner | 161d36d | 2009-02-28 19:01:03 +0000 | [diff] [blame] | 533 | BExpr->getBody()->getLocEnd()); |
| 534 | EmitStmt(BExpr->getBody()); |
| 535 | FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc()); |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 536 | |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 537 | // The runtime needs a minimum alignment of a void *. |
| 538 | uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; |
| 539 | BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign); |
| 540 | |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 541 | Size = BlockOffset; |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 542 | Align = BlockAlign; |
| 543 | subBlockDeclRefDecls = BlockDeclRefDecls; |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 544 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 545 | return Fn; |
| 546 | } |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 547 | |
| 548 | uint64_t CodeGenFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) { |
| 549 | const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl()); |
| 550 | |
| 551 | uint64_t Size = getContext().getTypeSize(D->getType()) / 8; |
| 552 | uint64_t Align = getContext().getDeclAlignInBytes(D); |
| 553 | |
| 554 | if (BDRE->isByRef()) { |
| 555 | Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8; |
| 556 | Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8; |
| 557 | } |
| 558 | |
| 559 | assert ((Align > 0) && "alignment must be 1 byte or more"); |
| 560 | |
| 561 | uint64_t OldOffset = BlockOffset; |
| 562 | |
| 563 | // Ensure proper alignment, even if it means we have to have a gap |
| 564 | BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align); |
| 565 | BlockAlign = std::max(Align, BlockAlign); |
| 566 | |
| 567 | uint64_t Pad = BlockOffset - OldOffset; |
| 568 | if (Pad) { |
| 569 | llvm::ArrayType::get(llvm::Type::Int8Ty, Pad); |
| 570 | QualType PadTy = getContext().getConstantArrayType(getContext().CharTy, |
| 571 | llvm::APInt(32, Pad), |
| 572 | ArrayType::Normal, 0); |
| 573 | ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(), |
| 574 | 0, QualType(PadTy), VarDecl::None, |
| 575 | SourceLocation()); |
| 576 | Expr *E; |
| 577 | E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(), |
| 578 | SourceLocation(), false, false); |
| 579 | BlockDeclRefDecls.push_back(E); |
| 580 | } |
| 581 | BlockDeclRefDecls.push_back(BDRE); |
| 582 | |
| 583 | BlockOffset += Size; |
| 584 | return BlockOffset-Size; |
| 585 | } |