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