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 | |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 14 | #include "CGDebugInfo.h" |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 15 | #include "CodeGenFunction.h" |
Fariborz Jahanian | 263c4de | 2010-02-10 23:34:57 +0000 | [diff] [blame] | 16 | #include "CGObjCRuntime.h" |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 17 | #include "CodeGenModule.h" |
Mike Stump | 6cc88f7 | 2009-03-20 21:53:12 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 19 | #include "llvm/Module.h" |
Benjamin Kramer | 6876fe6 | 2010-03-31 15:04:05 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallSet.h" |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetData.h" |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 22 | #include <algorithm> |
Torok Edwin | f42e4a6 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 23 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | using namespace CodeGen; |
| 26 | |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 27 | CGBlockInfo::CGBlockInfo(const char *N) |
Fariborz Jahanian | bc74764 | 2010-11-13 23:48:30 +0000 | [diff] [blame] | 28 | : Name(N), CXXThisRef(0), NeedsObjCSelf(false), |
Fariborz Jahanian | eec8217 | 2010-11-14 05:25:15 +0000 | [diff] [blame] | 29 | HasCXXObject(false) { |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 30 | |
| 31 | // Skip asm prefix, if any. |
| 32 | if (Name && Name[0] == '\01') |
| 33 | ++Name; |
| 34 | } |
| 35 | |
| 36 | |
Mike Stump | cf62d39 | 2009-03-06 18:42:23 +0000 | [diff] [blame] | 37 | llvm::Constant *CodeGenFunction:: |
Fariborz Jahanian | 89ecd41 | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 38 | BuildDescriptorBlockDecl(const BlockExpr *BE, const CGBlockInfo &Info, |
Mike Stump | a803b0e | 2009-03-25 17:58:24 +0000 | [diff] [blame] | 39 | const llvm::StructType* Ty, |
Fariborz Jahanian | 44034db | 2010-08-04 18:44:59 +0000 | [diff] [blame] | 40 | llvm::Constant *BlockVarLayout, |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 41 | std::vector<HelperInfo> *NoteForHelper) { |
Fariborz Jahanian | 89ecd41 | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 42 | CharUnits Size = Info.BlockSize; |
Mike Stump | 56129b1 | 2009-02-13 16:55:51 +0000 | [diff] [blame] | 43 | const llvm::Type *UnsignedLongTy |
| 44 | = CGM.getTypes().ConvertType(getContext().UnsignedLongTy); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 45 | llvm::Constant *C; |
| 46 | std::vector<llvm::Constant*> Elts; |
| 47 | |
| 48 | // reserved |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 49 | C = llvm::ConstantInt::get(UnsignedLongTy, 0); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 50 | Elts.push_back(C); |
| 51 | |
| 52 | // Size |
Mike Stump | d684000 | 2009-02-21 20:07:44 +0000 | [diff] [blame] | 53 | // FIXME: What is the right way to say this doesn't fit? We should give |
| 54 | // a user diagnostic in that case. Better fix would be to change the |
| 55 | // API to size_t. |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 56 | C = llvm::ConstantInt::get(UnsignedLongTy, Size.getQuantity()); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 57 | Elts.push_back(C); |
| 58 | |
Blaine Garst | 2a7eb28 | 2010-02-23 21:51:17 +0000 | [diff] [blame] | 59 | // optional copy/dispose helpers |
Fariborz Jahanian | 2715b20 | 2010-11-15 19:19:38 +0000 | [diff] [blame] | 60 | if (Info.BlockHasCopyDispose) { |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 61 | // copy_func_helper_decl |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 62 | Elts.push_back(BuildCopyHelper(Ty, NoteForHelper)); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 63 | |
| 64 | // destroy_func_decl |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 65 | Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper)); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Blaine Garst | 2a7eb28 | 2010-02-23 21:51:17 +0000 | [diff] [blame] | 68 | // Signature. non-optional ObjC-style method descriptor @encode sequence |
| 69 | std::string BlockTypeEncoding; |
| 70 | CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding); |
| 71 | |
| 72 | Elts.push_back(llvm::ConstantExpr::getBitCast( |
| 73 | CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty)); |
| 74 | |
| 75 | // Layout. |
Fariborz Jahanian | 44034db | 2010-08-04 18:44:59 +0000 | [diff] [blame] | 76 | C = BlockVarLayout; |
| 77 | |
Blaine Garst | 2a7eb28 | 2010-02-23 21:51:17 +0000 | [diff] [blame] | 78 | Elts.push_back(C); |
| 79 | |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 80 | C = llvm::ConstantStruct::get(VMContext, Elts, false); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 81 | |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 82 | C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 83 | llvm::GlobalValue::InternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 84 | C, "__block_descriptor_tmp"); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 85 | return C; |
| 86 | } |
| 87 | |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 88 | static void CollectBlockDeclRefInfo(const Stmt *S, CGBlockInfo &Info) { |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 89 | for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); |
| 90 | I != E; ++I) |
Daniel Dunbar | 82573ee | 2009-03-02 07:00:57 +0000 | [diff] [blame] | 91 | if (*I) |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 92 | CollectBlockDeclRefInfo(*I, Info); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 93 | |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 94 | // We want to ensure we walk down into block literals so we can find |
| 95 | // all nested BlockDeclRefExprs. |
Mike Stump | 38e1627 | 2009-10-21 22:01:24 +0000 | [diff] [blame] | 96 | if (const BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 97 | Info.InnerBlocks.insert(BE->getBlockDecl()); |
| 98 | CollectBlockDeclRefInfo(BE->getBody(), Info); |
Mike Stump | 38e1627 | 2009-10-21 22:01:24 +0000 | [diff] [blame] | 99 | } |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 100 | |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 101 | else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) { |
| 102 | const ValueDecl *D = BDRE->getDecl(); |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 103 | // FIXME: Handle enums. |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 104 | if (isa<FunctionDecl>(D)) |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 105 | return; |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 106 | |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 107 | if (isa<ImplicitParamDecl>(D) && |
| 108 | isa<ObjCMethodDecl>(D->getDeclContext()) && |
| 109 | cast<ObjCMethodDecl>(D->getDeclContext())->getSelfDecl() == D) { |
| 110 | Info.NeedsObjCSelf = true; |
| 111 | return; |
| 112 | } |
| 113 | |
Mike Stump | 38e1627 | 2009-10-21 22:01:24 +0000 | [diff] [blame] | 114 | // Only Decls that escape are added. |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 115 | if (!Info.InnerBlocks.count(D->getDeclContext())) { |
| 116 | if (BDRE->getCopyConstructorExpr()) |
| 117 | Info.HasCXXObject = true; |
Mike Stump | 38e1627 | 2009-10-21 22:01:24 +0000 | [diff] [blame] | 118 | Info.DeclRefs.push_back(BDRE); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 119 | } |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 120 | } |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 121 | |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 122 | // Make sure to capture implicit 'self' references due to super calls. |
Chandler Carruth | f54b80f | 2010-05-21 10:29:28 +0000 | [diff] [blame] | 123 | else if (const ObjCMessageExpr *E = dyn_cast<ObjCMessageExpr>(S)) { |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 124 | if (E->getReceiverKind() == ObjCMessageExpr::SuperClass || |
| 125 | E->getReceiverKind() == ObjCMessageExpr::SuperInstance) |
| 126 | Info.NeedsObjCSelf = true; |
Chandler Carruth | f54b80f | 2010-05-21 10:29:28 +0000 | [diff] [blame] | 127 | } |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 128 | else if (const ObjCPropertyRefExpr *PE = dyn_cast<ObjCPropertyRefExpr>(S)) { |
| 129 | // Getter/setter uses may also cause implicit super references, |
| 130 | // which we can check for with: |
| 131 | if (PE->isSuperReceiver()) |
| 132 | Info.NeedsObjCSelf = true; |
| 133 | } |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 134 | else if (isa<CXXThisExpr>(S)) |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 135 | Info.CXXThisRef = cast<CXXThisExpr>(S); |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 136 | } |
| 137 | |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 138 | /// CanBlockBeGlobal - Given a CGBlockInfo struct, determines if a block can be |
Mike Stump | 58a8514 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 139 | /// declared as a global variable instead of on the stack. |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 140 | static bool CanBlockBeGlobal(const CGBlockInfo &Info) { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 141 | return Info.DeclRefs.empty(); |
| 142 | } |
| 143 | |
| 144 | /// AllocateAllBlockDeclRefs - Preallocate all nested BlockDeclRefExprs to |
| 145 | /// ensure we can generate the debug information for the parameter for the block |
| 146 | /// invoke function. |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 147 | static void AllocateAllBlockDeclRefs(CodeGenFunction &CGF, CGBlockInfo &Info) { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 148 | if (Info.CXXThisRef) |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 149 | CGF.AllocateBlockCXXThisPointer(Info.CXXThisRef); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 150 | |
| 151 | for (size_t i = 0; i < Info.DeclRefs.size(); ++i) |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 152 | CGF.AllocateBlockDecl(Info.DeclRefs[i]); |
| 153 | |
| 154 | if (Info.NeedsObjCSelf) { |
| 155 | ValueDecl *Self = cast<ObjCMethodDecl>(CGF.CurFuncDecl)->getSelfDecl(); |
| 156 | BlockDeclRefExpr *BDRE = |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 157 | new (CGF.getContext()) BlockDeclRefExpr(Self, Self->getType(), VK_RValue, |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 158 | SourceLocation(), false); |
| 159 | Info.DeclRefs.push_back(BDRE); |
| 160 | CGF.AllocateBlockDecl(BDRE); |
| 161 | } |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Fariborz Jahanian | 7edddb8 | 2010-07-28 19:07:18 +0000 | [diff] [blame] | 164 | static unsigned computeBlockFlag(CodeGenModule &CGM, |
| 165 | const BlockExpr *BE, unsigned flags) { |
| 166 | QualType BPT = BE->getType(); |
| 167 | const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>(); |
| 168 | QualType ResultType = ftype->getResultType(); |
| 169 | |
| 170 | CallArgList Args; |
| 171 | CodeGenTypes &Types = CGM.getTypes(); |
| 172 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, Args, |
| 173 | FunctionType::ExtInfo()); |
| 174 | if (CGM.ReturnTypeUsesSRet(FnInfo)) |
| 175 | flags |= CodeGenFunction::BLOCK_USE_STRET; |
| 176 | return flags; |
| 177 | } |
| 178 | |
Mike Stump | 58a8514 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 179 | // FIXME: Push most into CGM, passing down a few bits, like current function |
| 180 | // name. |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 181 | llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) { |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 182 | std::string Name = CurFn->getName(); |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 183 | CGBlockInfo Info(Name.c_str()); |
| 184 | Info.InnerBlocks.insert(BE->getBlockDecl()); |
| 185 | CollectBlockDeclRefInfo(BE->getBody(), Info); |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 186 | |
| 187 | // Check if the block can be global. |
Mike Stump | 58a8514 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 188 | // FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like |
| 189 | // to just have one code path. We should move this function into CGM and pass |
| 190 | // CGF, then we can just check to see if CGF is 0. |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 191 | if (0 && CanBlockBeGlobal(Info)) |
Anders Carlsson | 4de9fce | 2009-03-01 01:09:12 +0000 | [diff] [blame] | 192 | return CGM.GetAddrOfGlobalBlock(BE, Name.c_str()); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 193 | |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 194 | size_t BlockFields = 5; |
| 195 | |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 196 | std::vector<llvm::Constant*> Elts(BlockFields); |
| 197 | |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 198 | llvm::Constant *C; |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 199 | llvm::Value *V; |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 200 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 201 | bool hasWeakBlockVariable = false; |
| 202 | |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 203 | { |
Fariborz Jahanian | 44034db | 2010-08-04 18:44:59 +0000 | [diff] [blame] | 204 | llvm::Constant *BlockVarLayout; |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 205 | // C = BuildBlockStructInitlist(); |
Blaine Garst | a36e223 | 2010-03-05 01:29:59 +0000 | [diff] [blame] | 206 | unsigned int flags = BLOCK_HAS_SIGNATURE; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 207 | |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 208 | // We run this first so that we set BlockHasCopyDispose from the entire |
| 209 | // block literal. |
| 210 | // __invoke |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 211 | llvm::Function *Fn |
Fariborz Jahanian | 564360b | 2010-06-24 00:08:06 +0000 | [diff] [blame] | 212 | = CodeGenFunction(CGM).GenerateBlockFunction(CurGD, BE, Info, CurFuncDecl, |
Fariborz Jahanian | 44034db | 2010-08-04 18:44:59 +0000 | [diff] [blame] | 213 | BlockVarLayout, |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 214 | LocalDeclMap); |
Fariborz Jahanian | 2715b20 | 2010-11-15 19:19:38 +0000 | [diff] [blame] | 215 | SynthesizeCopyDisposeHelpers |= Info.BlockHasCopyDispose; |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 216 | Elts[3] = Fn; |
| 217 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 218 | // FIXME: Don't use BlockHasCopyDispose, it is set more often then |
| 219 | // necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); } |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 220 | if (Info.BlockHasCopyDispose) |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 221 | flags |= BLOCK_HAS_COPY_DISPOSE; |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 222 | // This block may import a c++ object. |
| 223 | if (Info.HasCXXObject) |
| 224 | flags |= BLOCK_HAS_CXX_OBJ; |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 225 | |
Mike Stump | 7d6dc4f | 2009-02-13 20:17:16 +0000 | [diff] [blame] | 226 | // __isa |
Mike Stump | 59c5b11 | 2009-02-13 19:29:27 +0000 | [diff] [blame] | 227 | C = CGM.getNSConcreteStackBlock(); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 228 | C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 229 | Elts[0] = C; |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 230 | |
| 231 | // __flags |
Fariborz Jahanian | 7edddb8 | 2010-07-28 19:07:18 +0000 | [diff] [blame] | 232 | flags = computeBlockFlag(CGM, BE, flags); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 233 | const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( |
| 234 | CGM.getTypes().ConvertType(CGM.getContext().IntTy)); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 235 | C = llvm::ConstantInt::get(IntTy, flags); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 236 | Elts[1] = C; |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 237 | |
| 238 | // __reserved |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 239 | C = llvm::ConstantInt::get(IntTy, 0); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 240 | Elts[2] = C; |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 241 | |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 242 | if (Info.BlockLayout.empty()) { |
Mike Stump | cf62d39 | 2009-03-06 18:42:23 +0000 | [diff] [blame] | 243 | // __descriptor |
Fariborz Jahanian | 44034db | 2010-08-04 18:44:59 +0000 | [diff] [blame] | 244 | C = llvm::Constant::getNullValue(PtrToInt8Ty); |
| 245 | Elts[4] = BuildDescriptorBlockDecl(BE, Info, 0, C, 0); |
Mike Stump | cf62d39 | 2009-03-06 18:42:23 +0000 | [diff] [blame] | 246 | |
Mike Stump | 5570cfe | 2009-03-01 20:07:53 +0000 | [diff] [blame] | 247 | // Optimize to being a global block. |
| 248 | Elts[0] = CGM.getNSConcreteGlobalBlock(); |
Blaine Garst | a36e223 | 2010-03-05 01:29:59 +0000 | [diff] [blame] | 249 | |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 250 | Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL); |
Mike Stump | 5570cfe | 2009-03-01 20:07:53 +0000 | [diff] [blame] | 251 | |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 252 | C = llvm::ConstantStruct::get(VMContext, Elts, false); |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 253 | |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 254 | C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, |
Benjamin Kramer | 3cf7c5d | 2010-01-22 13:59:13 +0000 | [diff] [blame] | 255 | llvm::GlobalValue::InternalLinkage, C, |
| 256 | "__block_holder_tmp_" + |
| 257 | llvm::Twine(CGM.getGlobalUniqueCount())); |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 258 | QualType BPT = BE->getType(); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 259 | C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT)); |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 260 | return C; |
| 261 | } |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 262 | |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 263 | std::vector<const llvm::Type *> Types(BlockFields+Info.BlockLayout.size()); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 264 | for (int i=0; i<4; ++i) |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 265 | Types[i] = Elts[i]->getType(); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 266 | Types[4] = PtrToInt8Ty; |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 267 | |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 268 | for (unsigned i = 0, n = Info.BlockLayout.size(); i != n; ++i) { |
| 269 | const Expr *E = Info.BlockLayout[i]; |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 270 | const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); |
| 271 | QualType Ty = E->getType(); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 272 | if (BDRE && BDRE->isByRef()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 273 | if (BDRE->getDecl()->getType().isObjCGCWeak()) |
| 274 | hasWeakBlockVariable = true; |
Fariborz Jahanian | 7965172 | 2010-06-02 21:35:17 +0000 | [diff] [blame] | 275 | Types[i+BlockFields] = |
| 276 | llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0); |
| 277 | } else if (BDRE && BDRE->getDecl()->getType()->isReferenceType()) { |
| 278 | Types[i+BlockFields] = llvm::PointerType::get(ConvertType(Ty), 0); |
| 279 | } else |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 280 | Types[i+BlockFields] = ConvertType(Ty); |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 281 | } |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 282 | |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 283 | llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true); |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 284 | |
| 285 | llvm::AllocaInst *A = CreateTempAlloca(Ty); |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 286 | A->setAlignment(Info.BlockAlign.getQuantity()); |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 287 | V = A; |
| 288 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 289 | // Build layout / cleanup information for all the data entries in the |
| 290 | // layout, and write the enclosing fields into the type. |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 291 | std::vector<HelperInfo> NoteForHelper(Info.BlockLayout.size()); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 292 | unsigned NumHelpers = 0; |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 293 | |
| 294 | for (unsigned i=0; i<4; ++i) |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 295 | Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp")); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 296 | |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 297 | for (unsigned i=0; i < Info.BlockLayout.size(); ++i) { |
| 298 | const Expr *E = Info.BlockLayout[i]; |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 299 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 300 | // Skip padding. |
| 301 | if (isa<DeclRefExpr>(E)) continue; |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 302 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 303 | llvm::Value* Addr = Builder.CreateStructGEP(V, i+BlockFields, "tmp"); |
| 304 | HelperInfo &Note = NoteForHelper[NumHelpers++]; |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 305 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 306 | Note.index = i+5; |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 307 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 308 | if (isa<CXXThisExpr>(E)) { |
| 309 | Note.RequiresCopying = false; |
| 310 | Note.flag = BLOCK_FIELD_IS_OBJECT; |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 311 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 312 | Builder.CreateStore(LoadCXXThis(), Addr); |
| 313 | continue; |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 314 | } |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 315 | |
| 316 | const BlockDeclRefExpr *BDRE = cast<BlockDeclRefExpr>(E); |
Fariborz Jahanian | e27e9d6 | 2010-11-11 00:11:38 +0000 | [diff] [blame] | 317 | Note.RequiresCopying = BlockRequiresCopying(BDRE); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 318 | Note.cxxvar_import = |
| 319 | BDRE->getCopyConstructorExpr() ? BDRE : 0; |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 320 | const ValueDecl *VD = BDRE->getDecl(); |
| 321 | QualType T = VD->getType(); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 322 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 323 | if (BDRE->isByRef()) { |
| 324 | Note.flag = BLOCK_FIELD_IS_BYREF; |
| 325 | if (T.isObjCGCWeak()) |
| 326 | Note.flag |= BLOCK_FIELD_IS_WEAK; |
| 327 | } else if (T->isBlockPointerType()) { |
| 328 | Note.flag = BLOCK_FIELD_IS_BLOCK; |
| 329 | } else { |
| 330 | Note.flag = BLOCK_FIELD_IS_OBJECT; |
| 331 | } |
| 332 | |
| 333 | if (LocalDeclMap[VD]) { |
| 334 | if (BDRE->isByRef()) { |
| 335 | llvm::Value *Loc = LocalDeclMap[VD]; |
| 336 | Loc = Builder.CreateStructGEP(Loc, 1, "forwarding"); |
| 337 | Loc = Builder.CreateLoad(Loc); |
| 338 | Builder.CreateStore(Loc, Addr); |
| 339 | continue; |
| 340 | } else { |
Fariborz Jahanian | ac7362d | 2010-06-08 20:57:22 +0000 | [diff] [blame] | 341 | if (BDRE->getCopyConstructorExpr()) { |
| 342 | E = BDRE->getCopyConstructorExpr(); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 343 | PushDestructorCleanup(E->getType(), Addr); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 344 | } else { |
| 345 | E = new (getContext()) DeclRefExpr(const_cast<ValueDecl*>(VD), |
| 346 | VD->getType().getNonReferenceType(), |
| 347 | Expr::getValueKindForType(VD->getType()), |
| 348 | SourceLocation()); |
| 349 | if (VD->getType()->isReferenceType()) { |
| 350 | E = new (getContext()) |
| 351 | UnaryOperator(const_cast<Expr*>(E), UO_AddrOf, |
| 352 | getContext().getPointerType(E->getType()), |
| 353 | VK_RValue, OK_Ordinary, SourceLocation()); |
Fariborz Jahanian | df8b8ea | 2010-06-04 16:10:00 +0000 | [diff] [blame] | 354 | } |
| 355 | } |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 356 | } |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 357 | } |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 358 | |
| 359 | if (BDRE->isByRef()) { |
| 360 | E = new (getContext()) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 361 | UnaryOperator(const_cast<Expr*>(E), UO_AddrOf, |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 362 | getContext().getPointerType(E->getType()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 363 | VK_RValue, OK_Ordinary, SourceLocation()); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 364 | } |
| 365 | |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 366 | RValue r = EmitAnyExpr(E, AggValueSlot::forAddr(Addr, false, true)); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 367 | if (r.isScalar()) { |
| 368 | llvm::Value *Loc = r.getScalarVal(); |
| 369 | const llvm::Type *Ty = Types[i+BlockFields]; |
| 370 | if (BDRE->isByRef()) { |
| 371 | // E is now the address of the value field, instead, we want the |
| 372 | // address of the actual ByRef struct. We optimize this slightly |
| 373 | // compared to gcc by not grabbing the forwarding slot as this must |
| 374 | // be done during Block_copy for us, and we can postpone the work |
| 375 | // until then. |
| 376 | CharUnits offset = BlockDecls[BDRE->getDecl()]; |
| 377 | |
| 378 | llvm::Value *BlockLiteral = LoadBlockStruct(); |
| 379 | |
| 380 | Loc = Builder.CreateGEP(BlockLiteral, |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 381 | llvm::ConstantInt::get(Int64Ty, offset.getQuantity()), |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 382 | "block.literal"); |
| 383 | Ty = llvm::PointerType::get(Ty, 0); |
| 384 | Loc = Builder.CreateBitCast(Loc, Ty); |
| 385 | Loc = Builder.CreateLoad(Loc); |
| 386 | // Loc = Builder.CreateBitCast(Loc, Ty); |
| 387 | } |
| 388 | Builder.CreateStore(Loc, Addr); |
| 389 | } else if (r.isComplex()) |
| 390 | // FIXME: implement |
| 391 | ErrorUnsupported(BE, "complex in block literal"); |
| 392 | else if (r.isAggregate()) |
| 393 | ; // Already created into the destination |
| 394 | else |
| 395 | assert (0 && "bad block variable"); |
| 396 | // FIXME: Ensure that the offset created by the backend for |
| 397 | // the struct matches the previously computed offset in BlockDecls. |
| 398 | } |
| 399 | NoteForHelper.resize(NumHelpers); |
Mike Stump | cf62d39 | 2009-03-06 18:42:23 +0000 | [diff] [blame] | 400 | |
| 401 | // __descriptor |
Fariborz Jahanian | 89ecd41 | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 402 | llvm::Value *Descriptor = BuildDescriptorBlockDecl(BE, Info, Ty, |
Fariborz Jahanian | 44034db | 2010-08-04 18:44:59 +0000 | [diff] [blame] | 403 | BlockVarLayout, |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 404 | &NoteForHelper); |
| 405 | Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty); |
| 406 | Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp")); |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 407 | } |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 408 | |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 409 | QualType BPT = BE->getType(); |
Fariborz Jahanian | 263c4de | 2010-02-10 23:34:57 +0000 | [diff] [blame] | 410 | V = Builder.CreateBitCast(V, ConvertType(BPT)); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 411 | |
| 412 | // We must call objc_read_weak on the block literal itself if it closes |
| 413 | // on any __weak __block variables. For some reason. |
| 414 | if (hasWeakBlockVariable) { |
| 415 | const llvm::Type *OrigTy = V->getType(); |
| 416 | |
Fariborz Jahanian | 263c4de | 2010-02-10 23:34:57 +0000 | [diff] [blame] | 417 | // Must cast argument to id* |
| 418 | const llvm::Type *ObjectPtrTy = |
| 419 | ConvertType(CGM.getContext().getObjCIdType()); |
| 420 | const llvm::Type *PtrObjectPtrTy = |
| 421 | llvm::PointerType::getUnqual(ObjectPtrTy); |
| 422 | V = Builder.CreateBitCast(V, PtrObjectPtrTy); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 423 | V = CGM.getObjCRuntime().EmitObjCWeakRead(*this, V); |
| 424 | |
| 425 | // Cast back to the original type. |
| 426 | V = Builder.CreateBitCast(V, OrigTy); |
Fariborz Jahanian | 263c4de | 2010-02-10 23:34:57 +0000 | [diff] [blame] | 427 | } |
| 428 | return V; |
Mike Stump | e5fee25 | 2009-02-13 16:19:19 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 432 | const llvm::Type *BlockModule::getBlockDescriptorType() { |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 433 | if (BlockDescriptorType) |
| 434 | return BlockDescriptorType; |
| 435 | |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 436 | const llvm::Type *UnsignedLongTy = |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 437 | getTypes().ConvertType(getContext().UnsignedLongTy); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 438 | |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 439 | // struct __block_descriptor { |
| 440 | // unsigned long reserved; |
| 441 | // unsigned long block_size; |
Blaine Garst | 2a7eb28 | 2010-02-23 21:51:17 +0000 | [diff] [blame] | 442 | // |
| 443 | // // later, the following will be added |
| 444 | // |
| 445 | // struct { |
| 446 | // void (*copyHelper)(); |
| 447 | // void (*copyHelper)(); |
| 448 | // } helpers; // !!! optional |
| 449 | // |
| 450 | // const char *signature; // the block signature |
| 451 | // const char *layout; // reserved |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 452 | // }; |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 453 | BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(), |
| 454 | UnsignedLongTy, |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 455 | UnsignedLongTy, |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 456 | NULL); |
| 457 | |
| 458 | getModule().addTypeName("struct.__block_descriptor", |
| 459 | BlockDescriptorType); |
| 460 | |
| 461 | return BlockDescriptorType; |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 464 | const llvm::Type *BlockModule::getGenericBlockLiteralType() { |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 465 | if (GenericBlockLiteralType) |
| 466 | return GenericBlockLiteralType; |
| 467 | |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 468 | const llvm::Type *BlockDescPtrTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 469 | llvm::PointerType::getUnqual(getBlockDescriptorType()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 470 | |
Mike Stump | 7cbb360 | 2009-02-13 16:01:35 +0000 | [diff] [blame] | 471 | const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( |
| 472 | getTypes().ConvertType(getContext().IntTy)); |
| 473 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 474 | // struct __block_literal_generic { |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 475 | // void *__isa; |
| 476 | // int __flags; |
| 477 | // int __reserved; |
| 478 | // void (*__invoke)(void *); |
| 479 | // struct __block_descriptor *__descriptor; |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 480 | // }; |
Blaine Garst | 2a7eb28 | 2010-02-23 21:51:17 +0000 | [diff] [blame] | 481 | GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(), |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 482 | PtrToInt8Ty, |
Mike Stump | 7cbb360 | 2009-02-13 16:01:35 +0000 | [diff] [blame] | 483 | IntTy, |
| 484 | IntTy, |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 485 | PtrToInt8Ty, |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 486 | BlockDescPtrTy, |
| 487 | NULL); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 488 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 489 | getModule().addTypeName("struct.__block_literal_generic", |
| 490 | GenericBlockLiteralType); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 491 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame] | 492 | return GenericBlockLiteralType; |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Mike Stump | bd65cac | 2009-02-19 01:01:04 +0000 | [diff] [blame] | 495 | |
Anders Carlsson | a1736c0 | 2009-12-24 21:13:40 +0000 | [diff] [blame] | 496 | RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E, |
| 497 | ReturnValueSlot ReturnValue) { |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 498 | const BlockPointerType *BPT = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 499 | E->getCallee()->getType()->getAs<BlockPointerType>(); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 500 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 501 | llvm::Value *Callee = EmitScalarExpr(E->getCallee()); |
| 502 | |
| 503 | // Get a pointer to the generic block literal. |
| 504 | const llvm::Type *BlockLiteralTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 505 | llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType()); |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 506 | |
| 507 | // Bitcast the callee to a block literal. |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 508 | llvm::Value *BlockLiteral = |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 509 | Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal"); |
| 510 | |
| 511 | // Get the function pointer from the literal. |
| 512 | llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp"); |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 513 | |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 514 | BlockLiteral = |
| 515 | Builder.CreateBitCast(BlockLiteral, |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 516 | llvm::Type::getInt8PtrTy(VMContext), |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 517 | "tmp"); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 518 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 519 | // Add the block literal. |
| 520 | QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy); |
| 521 | CallArgList Args; |
| 522 | Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy)); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 523 | |
Anders Carlsson | 782f397 | 2009-04-08 23:13:16 +0000 | [diff] [blame] | 524 | QualType FnType = BPT->getPointeeType(); |
| 525 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 526 | // And the rest of the arguments. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 527 | EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(), |
Anders Carlsson | 782f397 | 2009-04-08 23:13:16 +0000 | [diff] [blame] | 528 | E->arg_begin(), E->arg_end()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 529 | |
Anders Carlsson | 6e460ff | 2009-04-07 22:10:22 +0000 | [diff] [blame] | 530 | // Load the function. |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 531 | llvm::Value *Func = Builder.CreateLoad(FuncPtr, "tmp"); |
Anders Carlsson | 6e460ff | 2009-04-07 22:10:22 +0000 | [diff] [blame] | 532 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 533 | const FunctionType *FuncTy = FnType->getAs<FunctionType>(); |
| 534 | QualType ResultType = FuncTy->getResultType(); |
Anders Carlsson | a17d7cc | 2009-04-08 02:55:55 +0000 | [diff] [blame] | 535 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 536 | const CGFunctionInfo &FnInfo = |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 537 | CGM.getTypes().getFunctionInfo(ResultType, Args, |
| 538 | FuncTy->getExtInfo()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 539 | |
Anders Carlsson | 6e460ff | 2009-04-07 22:10:22 +0000 | [diff] [blame] | 540 | // Cast the function pointer to the right type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 541 | const llvm::Type *BlockFTy = |
Anders Carlsson | a17d7cc | 2009-04-08 02:55:55 +0000 | [diff] [blame] | 542 | CGM.getTypes().GetFunctionType(FnInfo, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 543 | |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 544 | const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy); |
Anders Carlsson | 6e460ff | 2009-04-07 22:10:22 +0000 | [diff] [blame] | 545 | Func = Builder.CreateBitCast(Func, BlockFTyPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 546 | |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 547 | // And call the block. |
Anders Carlsson | a1736c0 | 2009-12-24 21:13:40 +0000 | [diff] [blame] | 548 | return EmitCall(FnInfo, Func, ReturnValue, Args); |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 549 | } |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 550 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 551 | void CodeGenFunction::AllocateBlockCXXThisPointer(const CXXThisExpr *E) { |
| 552 | assert(BlockCXXThisOffset.isZero() && "already computed 'this' pointer"); |
| 553 | |
| 554 | // Figure out what the offset is. |
| 555 | QualType T = E->getType(); |
| 556 | std::pair<CharUnits,CharUnits> TypeInfo = getContext().getTypeInfoInChars(T); |
| 557 | CharUnits Offset = getBlockOffset(TypeInfo.first, TypeInfo.second); |
| 558 | |
| 559 | BlockCXXThisOffset = Offset; |
| 560 | BlockLayout.push_back(E); |
| 561 | } |
| 562 | |
| 563 | void CodeGenFunction::AllocateBlockDecl(const BlockDeclRefExpr *E) { |
Anders Carlsson | 7dfa407 | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 564 | const ValueDecl *VD = E->getDecl(); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 565 | CharUnits &Offset = BlockDecls[VD]; |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 566 | |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 567 | // See if we have already allocated an offset for this variable. |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 568 | if (!Offset.isZero()) |
| 569 | return; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 570 | |
| 571 | // Don't run the expensive check, unless we have to. |
Fariborz Jahanian | 2715b20 | 2010-11-15 19:19:38 +0000 | [diff] [blame] | 572 | if (!SynthesizeCopyDisposeHelpers) |
| 573 | if (E->isByRef() || BlockRequiresCopying(E)) |
| 574 | SynthesizeCopyDisposeHelpers = true; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 575 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 576 | const ValueDecl *D = cast<ValueDecl>(E->getDecl()); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 577 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 578 | CharUnits Size; |
| 579 | CharUnits Align; |
| 580 | |
| 581 | if (E->isByRef()) { |
| 582 | llvm::tie(Size,Align) = |
| 583 | getContext().getTypeInfoInChars(getContext().VoidPtrTy); |
| 584 | } else { |
| 585 | Size = getContext().getTypeSizeInChars(D->getType()); |
| 586 | Align = getContext().getDeclAlign(D); |
| 587 | } |
| 588 | |
| 589 | Offset = getBlockOffset(Size, Align); |
| 590 | BlockLayout.push_back(E); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 591 | } |
| 592 | |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 593 | llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const ValueDecl *VD, |
| 594 | bool IsByRef) { |
Fariborz Jahanian | df8b8ea | 2010-06-04 16:10:00 +0000 | [diff] [blame] | 595 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 596 | CharUnits offset = BlockDecls[VD]; |
| 597 | assert(!offset.isZero() && "getting address of unallocated decl"); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 598 | |
| 599 | llvm::Value *BlockLiteral = LoadBlockStruct(); |
| 600 | llvm::Value *V = Builder.CreateGEP(BlockLiteral, |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 601 | llvm::ConstantInt::get(Int64Ty, offset.getQuantity()), |
Mike Stump | 58a8514 | 2009-03-04 22:48:06 +0000 | [diff] [blame] | 602 | "block.literal"); |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 603 | if (IsByRef) { |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 604 | const llvm::Type *PtrStructTy |
Anders Carlsson | 7dfa407 | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 605 | = llvm::PointerType::get(BuildByRefType(VD), 0); |
Mike Stump | a8b60c9 | 2009-03-21 21:00:35 +0000 | [diff] [blame] | 606 | // The block literal will need a copy/destroy helper. |
Fariborz Jahanian | 2715b20 | 2010-11-15 19:19:38 +0000 | [diff] [blame] | 607 | SynthesizeCopyDisposeHelpers = true; |
Anders Carlsson | 7dfa407 | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 608 | |
| 609 | const llvm::Type *Ty = PtrStructTy; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 610 | Ty = llvm::PointerType::get(Ty, 0); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 611 | V = Builder.CreateBitCast(V, Ty); |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 612 | V = Builder.CreateLoad(V); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 613 | V = Builder.CreateStructGEP(V, 1, "forwarding"); |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 614 | V = Builder.CreateLoad(V); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 615 | V = Builder.CreateBitCast(V, PtrStructTy); |
Anders Carlsson | 7dfa407 | 2009-09-12 02:14:24 +0000 | [diff] [blame] | 616 | V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD), |
| 617 | VD->getNameAsString()); |
Fariborz Jahanian | d33ded5 | 2010-05-04 17:59:32 +0000 | [diff] [blame] | 618 | if (VD->getType()->isReferenceType()) |
| 619 | V = Builder.CreateLoad(V); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 620 | } else { |
John McCall | 46ec70e | 2010-10-28 21:37:57 +0000 | [diff] [blame] | 621 | const llvm::Type *Ty = CGM.getTypes().ConvertTypeForMem(VD->getType()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 622 | Ty = llvm::PointerType::get(Ty, 0); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 623 | V = Builder.CreateBitCast(V, Ty); |
Fariborz Jahanian | 7965172 | 2010-06-02 21:35:17 +0000 | [diff] [blame] | 624 | if (VD->getType()->isReferenceType()) |
Fariborz Jahanian | df8b8ea | 2010-06-04 16:10:00 +0000 | [diff] [blame] | 625 | V = Builder.CreateLoad(V, "ref.tmp"); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 626 | } |
| 627 | return V; |
| 628 | } |
| 629 | |
Mike Stump | 67a6448 | 2009-02-14 22:16:35 +0000 | [diff] [blame] | 630 | llvm::Constant * |
Mike Stump | 90a9043 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 631 | BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) { |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 632 | // Generate the block descriptor. |
| 633 | const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy); |
Mike Stump | 7cbb360 | 2009-02-13 16:01:35 +0000 | [diff] [blame] | 634 | const llvm::IntegerType *IntTy = cast<llvm::IntegerType>( |
| 635 | getTypes().ConvertType(getContext().IntTy)); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 636 | |
Blaine Garst | 2a7eb28 | 2010-02-23 21:51:17 +0000 | [diff] [blame] | 637 | llvm::Constant *DescriptorFields[4]; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 638 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 639 | // Reserved |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 640 | DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 641 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 642 | // Block literal size. For global blocks we just use the size of the generic |
| 643 | // block literal struct. |
Ken Dyck | 687cc4a | 2010-01-26 13:48:07 +0000 | [diff] [blame] | 644 | CharUnits BlockLiteralSize = |
| 645 | CGM.GetTargetTypeStoreSize(getGenericBlockLiteralType()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 646 | DescriptorFields[1] = |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 647 | llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize.getQuantity()); |
Blaine Garst | 2a7eb28 | 2010-02-23 21:51:17 +0000 | [diff] [blame] | 648 | |
| 649 | // signature. non-optional ObjC-style method descriptor @encode sequence |
| 650 | std::string BlockTypeEncoding; |
| 651 | CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 652 | |
Blaine Garst | 2a7eb28 | 2010-02-23 21:51:17 +0000 | [diff] [blame] | 653 | DescriptorFields[2] = llvm::ConstantExpr::getBitCast( |
| 654 | CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty); |
| 655 | |
| 656 | // layout |
| 657 | DescriptorFields[3] = |
| 658 | llvm::ConstantInt::get(UnsignedLongTy,0); |
| 659 | |
| 660 | // build the structure from the 4 elements |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 661 | llvm::Constant *DescriptorStruct = |
Blaine Garst | 2a7eb28 | 2010-02-23 21:51:17 +0000 | [diff] [blame] | 662 | llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 4, false); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 663 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 664 | llvm::GlobalVariable *Descriptor = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 665 | new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true, |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 666 | llvm::GlobalVariable::InternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 667 | DescriptorStruct, "__block_descriptor_global"); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 668 | |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 669 | int FieldCount = 5; |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 670 | // Generate the constants for the block literal. |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 671 | |
| 672 | std::vector<llvm::Constant*> LiteralFields(FieldCount); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 673 | |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 674 | CGBlockInfo Info(n); |
Fariborz Jahanian | 44034db | 2010-08-04 18:44:59 +0000 | [diff] [blame] | 675 | llvm::Constant *BlockVarLayout; |
Mike Stump | 7f28a9c | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 676 | llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap; |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 677 | llvm::Function *Fn |
Fariborz Jahanian | 7edddb8 | 2010-07-28 19:07:18 +0000 | [diff] [blame] | 678 | = CodeGenFunction(CGM).GenerateBlockFunction(GlobalDecl(), BE, |
Fariborz Jahanian | 44034db | 2010-08-04 18:44:59 +0000 | [diff] [blame] | 679 | Info, 0, BlockVarLayout, |
| 680 | LocalDeclMap); |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 681 | assert(Info.BlockSize == BlockLiteralSize |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 682 | && "no imports allowed for global block"); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 683 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 684 | // isa |
Daniel Dunbar | 673431a | 2010-07-16 00:00:15 +0000 | [diff] [blame] | 685 | LiteralFields[0] = CGM.getNSConcreteGlobalBlock(); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 686 | |
Fariborz Jahanian | 7edddb8 | 2010-07-28 19:07:18 +0000 | [diff] [blame] | 687 | // __flags |
| 688 | unsigned flags = computeBlockFlag(CGM, BE, |
| 689 | (BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE)); |
Blaine Garst | 2a7eb28 | 2010-02-23 21:51:17 +0000 | [diff] [blame] | 690 | LiteralFields[1] = |
Fariborz Jahanian | 7edddb8 | 2010-07-28 19:07:18 +0000 | [diff] [blame] | 691 | llvm::ConstantInt::get(IntTy, flags); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 692 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 693 | // Reserved |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 694 | LiteralFields[2] = llvm::Constant::getNullValue(IntTy); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 695 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 696 | // Function |
| 697 | LiteralFields[3] = Fn; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 698 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 699 | // Descriptor |
| 700 | LiteralFields[4] = Descriptor; |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 701 | |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 702 | llvm::Constant *BlockLiteralStruct = |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 703 | llvm::ConstantStruct::get(VMContext, LiteralFields, false); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 704 | |
| 705 | llvm::GlobalVariable *BlockLiteral = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 706 | new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true, |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 707 | llvm::GlobalVariable::InternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 708 | BlockLiteralStruct, "__block_literal_global"); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 709 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 710 | return BlockLiteral; |
| 711 | } |
| 712 | |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 713 | llvm::Value *CodeGenFunction::LoadBlockStruct() { |
Mike Stump | bf1914b | 2009-10-20 20:30:01 +0000 | [diff] [blame] | 714 | llvm::Value *V = Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], |
| 715 | "self"); |
| 716 | // For now, we codegen based upon byte offsets. |
| 717 | return Builder.CreateBitCast(V, PtrToInt8Ty); |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 720 | llvm::Function * |
Fariborz Jahanian | 564360b | 2010-06-24 00:08:06 +0000 | [diff] [blame] | 721 | CodeGenFunction::GenerateBlockFunction(GlobalDecl GD, const BlockExpr *BExpr, |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 722 | CGBlockInfo &Info, |
Mike Stump | 6cc88f7 | 2009-03-20 21:53:12 +0000 | [diff] [blame] | 723 | const Decl *OuterFuncDecl, |
Fariborz Jahanian | 44034db | 2010-08-04 18:44:59 +0000 | [diff] [blame] | 724 | llvm::Constant *& BlockVarLayout, |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 725 | llvm::DenseMap<const Decl*, llvm::Value*> ldm) { |
Devang Patel | 963dfbd | 2009-04-15 21:51:44 +0000 | [diff] [blame] | 726 | |
| 727 | // Check if we should generate debug info for this block. |
| 728 | if (CGM.getDebugInfo()) |
| 729 | DebugInfo = CGM.getDebugInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 730 | |
Mike Stump | 7f28a9c | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 731 | // Arrange for local static and local extern declarations to appear |
| 732 | // to be local to this function as well, as they are directly referenced |
| 733 | // in a block. |
| 734 | for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin(); |
| 735 | i != ldm.end(); |
| 736 | ++i) { |
| 737 | const VarDecl *VD = dyn_cast<VarDecl>(i->first); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 738 | |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 739 | if (VD->getStorageClass() == SC_Static || VD->hasExternalStorage()) |
Mike Stump | 7f28a9c | 2009-03-13 23:34:28 +0000 | [diff] [blame] | 740 | LocalDeclMap[VD] = i->second; |
| 741 | } |
| 742 | |
Ken Dyck | 687cc4a | 2010-01-26 13:48:07 +0000 | [diff] [blame] | 743 | BlockOffset = |
| 744 | CGM.GetTargetTypeStoreSize(CGM.getGenericBlockLiteralType()); |
Ken Dyck | 30a8a27 | 2010-01-26 19:13:33 +0000 | [diff] [blame] | 745 | BlockAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy); |
Eli Friedman | 48f9122 | 2009-03-28 03:24:54 +0000 | [diff] [blame] | 746 | |
Fariborz Jahanian | 140fb26 | 2009-04-11 17:55:15 +0000 | [diff] [blame] | 747 | const FunctionType *BlockFunctionType = BExpr->getFunctionType(); |
| 748 | QualType ResultType; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 749 | FunctionType::ExtInfo EInfo = getFunctionExtInfo(*BlockFunctionType); |
Fariborz Jahanian | 140fb26 | 2009-04-11 17:55:15 +0000 | [diff] [blame] | 750 | bool IsVariadic; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 751 | if (const FunctionProtoType *FTy = |
Fariborz Jahanian | da0895d | 2009-04-11 18:54:57 +0000 | [diff] [blame] | 752 | dyn_cast<FunctionProtoType>(BlockFunctionType)) { |
Fariborz Jahanian | 140fb26 | 2009-04-11 17:55:15 +0000 | [diff] [blame] | 753 | ResultType = FTy->getResultType(); |
| 754 | IsVariadic = FTy->isVariadic(); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 755 | } else { |
Fariborz Jahanian | 140fb26 | 2009-04-11 17:55:15 +0000 | [diff] [blame] | 756 | // K&R style block. |
| 757 | ResultType = BlockFunctionType->getResultType(); |
| 758 | IsVariadic = false; |
| 759 | } |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 760 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 761 | FunctionArgList Args; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 762 | |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 763 | CurFuncDecl = OuterFuncDecl; |
| 764 | |
Chris Lattner | 161d36d | 2009-02-28 19:01:03 +0000 | [diff] [blame] | 765 | const BlockDecl *BD = BExpr->getBlockDecl(); |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 766 | |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 767 | IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor"); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 768 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 769 | // Build the block struct now. |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 770 | AllocateAllBlockDeclRefs(*this, Info); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 771 | |
Fariborz Jahanian | 44034db | 2010-08-04 18:44:59 +0000 | [diff] [blame] | 772 | // Capture block layout info. here. |
| 773 | if (CGM.getContext().getLangOptions().ObjC1) |
Fariborz Jahanian | c5904b4 | 2010-09-11 01:27:29 +0000 | [diff] [blame] | 774 | BlockVarLayout = CGM.getObjCRuntime().GCBlockLayout(*this, BlockLayout); |
Fariborz Jahanian | 44034db | 2010-08-04 18:44:59 +0000 | [diff] [blame] | 775 | else |
| 776 | BlockVarLayout = llvm::Constant::getNullValue(PtrToInt8Ty); |
| 777 | |
Fariborz Jahanian | 2715b20 | 2010-11-15 19:19:38 +0000 | [diff] [blame] | 778 | QualType ParmTy = getContext().getBlockParmType( |
| 779 | SynthesizeCopyDisposeHelpers, |
| 780 | BlockLayout); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 781 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 782 | // FIXME: This leaks |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 783 | ImplicitParamDecl *SelfDecl = |
John McCall | 2888b65 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 784 | ImplicitParamDecl::Create(getContext(), const_cast<BlockDecl*>(BD), |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 785 | SourceLocation(), II, |
| 786 | ParmTy); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 787 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 788 | Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType())); |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 789 | BlockStructDecl = SelfDecl; |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 790 | |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 791 | for (BlockDecl::param_const_iterator i = BD->param_begin(), |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 792 | e = BD->param_end(); i != e; ++i) |
Mike Stump | 1905061 | 2009-02-17 23:25:52 +0000 | [diff] [blame] | 793 | Args.push_back(std::make_pair(*i, (*i)->getType())); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 794 | |
| 795 | const CGFunctionInfo &FI = |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 796 | CGM.getTypes().getFunctionInfo(ResultType, Args, EInfo); |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 797 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 798 | CodeGenTypes &Types = CGM.getTypes(); |
Fariborz Jahanian | 140fb26 | 2009-04-11 17:55:15 +0000 | [diff] [blame] | 799 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 800 | |
Douglas Gregor | 35415f5 | 2010-05-25 17:04:15 +0000 | [diff] [blame] | 801 | MangleBuffer Name; |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 802 | CGM.getBlockMangledName(GD, Name, BD); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 803 | llvm::Function *Fn = |
Douglas Gregor | 6e5d9b0 | 2010-05-25 17:12:30 +0000 | [diff] [blame] | 804 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
| 805 | Name.getString(), &CGM.getModule()); |
Mike Stump | a544854 | 2009-02-13 15:32:32 +0000 | [diff] [blame] | 806 | |
Daniel Dunbar | 0e4f40e | 2009-04-17 00:48:04 +0000 | [diff] [blame] | 807 | CGM.SetInternalFunctionAttributes(BD, Fn, FI); |
Fariborz Jahanian | 8404f67 | 2010-08-13 00:19:55 +0000 | [diff] [blame] | 808 | StartFunction(BD, ResultType, Fn, Args, |
| 809 | BExpr->getBody()->getLocEnd()); |
| 810 | |
| 811 | CurFuncDecl = OuterFuncDecl; |
| 812 | |
Fariborz Jahanian | 16ac5ce | 2010-06-28 18:58:34 +0000 | [diff] [blame] | 813 | QualType FnType(BlockFunctionType, 0); |
Fariborz Jahanian | ef160b4 | 2010-06-28 19:42:10 +0000 | [diff] [blame] | 814 | bool HasPrototype = isa<FunctionProtoType>(BlockFunctionType); |
Fariborz Jahanian | 16ac5ce | 2010-06-28 18:58:34 +0000 | [diff] [blame] | 815 | |
| 816 | IdentifierInfo *ID = &getContext().Idents.get(Name.getString()); |
| 817 | CurCodeDecl = FunctionDecl::Create(getContext(), |
| 818 | getContext().getTranslationUnitDecl(), |
| 819 | SourceLocation(), ID, FnType, |
| 820 | 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 821 | SC_Static, |
| 822 | SC_None, |
Fariborz Jahanian | 16ac5ce | 2010-06-28 18:58:34 +0000 | [diff] [blame] | 823 | false, HasPrototype); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 824 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FnType)) { |
Fariborz Jahanian | 8404f67 | 2010-08-13 00:19:55 +0000 | [diff] [blame] | 825 | const FunctionDecl *CFD = dyn_cast<FunctionDecl>(CurCodeDecl); |
| 826 | FunctionDecl *FD = const_cast<FunctionDecl *>(CFD); |
| 827 | llvm::SmallVector<ParmVarDecl*, 16> Params; |
| 828 | for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) |
| 829 | Params.push_back(ParmVarDecl::Create(getContext(), FD, |
| 830 | SourceLocation(), 0, |
| 831 | FT->getArgType(i), /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 832 | SC_None, SC_None, 0)); |
Fariborz Jahanian | 8404f67 | 2010-08-13 00:19:55 +0000 | [diff] [blame] | 833 | FD->setParams(Params.data(), Params.size()); |
| 834 | } |
Fariborz Jahanian | 16ac5ce | 2010-06-28 18:58:34 +0000 | [diff] [blame] | 835 | |
Fariborz Jahanian | 8404f67 | 2010-08-13 00:19:55 +0000 | [diff] [blame] | 836 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 837 | // If we have a C++ 'this' reference, go ahead and force it into |
| 838 | // existence now. |
| 839 | if (Info.CXXThisRef) { |
| 840 | assert(!BlockCXXThisOffset.isZero() && |
| 841 | "haven't yet allocated 'this' reference"); |
| 842 | |
| 843 | // TODO: I have a dream that one day this will be typed. |
| 844 | llvm::Value *BlockLiteral = LoadBlockStruct(); |
| 845 | llvm::Value *ThisPtrRaw = |
| 846 | Builder.CreateConstInBoundsGEP1_64(BlockLiteral, |
| 847 | BlockCXXThisOffset.getQuantity(), |
| 848 | "this.ptr.raw"); |
| 849 | |
| 850 | const llvm::Type *Ty = |
| 851 | CGM.getTypes().ConvertType(Info.CXXThisRef->getType()); |
| 852 | Ty = llvm::PointerType::get(Ty, 0); |
| 853 | llvm::Value *ThisPtr = Builder.CreateBitCast(ThisPtrRaw, Ty, "this.ptr"); |
| 854 | |
| 855 | CXXThisValue = Builder.CreateLoad(ThisPtr, "this"); |
| 856 | } |
| 857 | |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 858 | // If we have an Objective C 'self' reference, go ahead and force it |
| 859 | // into existence now. |
| 860 | if (Info.NeedsObjCSelf) { |
| 861 | ValueDecl *Self = cast<ObjCMethodDecl>(CurFuncDecl)->getSelfDecl(); |
| 862 | LocalDeclMap[Self] = GetAddrOfBlockDecl(Self, false); |
| 863 | } |
| 864 | |
Mike Stump | b289b3f | 2009-10-01 22:29:41 +0000 | [diff] [blame] | 865 | // Save a spot to insert the debug information for all the BlockDeclRefDecls. |
| 866 | llvm::BasicBlock *entry = Builder.GetInsertBlock(); |
| 867 | llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint(); |
| 868 | --entry_ptr; |
| 869 | |
Chris Lattner | 161d36d | 2009-02-28 19:01:03 +0000 | [diff] [blame] | 870 | EmitStmt(BExpr->getBody()); |
Mike Stump | b289b3f | 2009-10-01 22:29:41 +0000 | [diff] [blame] | 871 | |
Mike Stump | de8c5c7 | 2009-10-01 00:27:30 +0000 | [diff] [blame] | 872 | // Remember where we were... |
| 873 | llvm::BasicBlock *resume = Builder.GetInsertBlock(); |
Mike Stump | b289b3f | 2009-10-01 22:29:41 +0000 | [diff] [blame] | 874 | |
Mike Stump | de8c5c7 | 2009-10-01 00:27:30 +0000 | [diff] [blame] | 875 | // Go back to the entry. |
Mike Stump | b289b3f | 2009-10-01 22:29:41 +0000 | [diff] [blame] | 876 | ++entry_ptr; |
| 877 | Builder.SetInsertPoint(entry, entry_ptr); |
| 878 | |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 879 | if (CGDebugInfo *DI = getDebugInfo()) { |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 880 | // Emit debug information for all the BlockDeclRefDecls. |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 881 | // FIXME: also for 'this' |
| 882 | for (unsigned i = 0, e = BlockLayout.size(); i != e; ++i) { |
| 883 | if (const BlockDeclRefExpr *BDRE = |
| 884 | dyn_cast<BlockDeclRefExpr>(BlockLayout[i])) { |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 885 | const ValueDecl *D = BDRE->getDecl(); |
| 886 | DI->setLocation(D->getLocation()); |
| 887 | DI->EmitDeclareOfBlockDeclRefVariable(BDRE, |
| 888 | LocalDeclMap[getBlockStructDecl()], |
| 889 | Builder, this); |
| 890 | } |
| 891 | } |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 892 | } |
Mike Stump | de8c5c7 | 2009-10-01 00:27:30 +0000 | [diff] [blame] | 893 | // And resume where we left off. |
| 894 | if (resume == 0) |
| 895 | Builder.ClearInsertionPoint(); |
| 896 | else |
| 897 | Builder.SetInsertPoint(resume); |
Mike Stump | b1a6e68 | 2009-09-30 02:43:10 +0000 | [diff] [blame] | 898 | |
Chris Lattner | 161d36d | 2009-02-28 19:01:03 +0000 | [diff] [blame] | 899 | FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc()); |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 900 | |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 901 | // The runtime needs a minimum alignment of a void *. |
Ken Dyck | 30a8a27 | 2010-01-26 19:13:33 +0000 | [diff] [blame] | 902 | CharUnits MinAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy); |
Ken Dyck | 3dbdb58 | 2011-01-20 01:59:55 +0000 | [diff] [blame^] | 903 | BlockOffset = BlockOffset.RoundUpToAlignment(MinAlign); |
Mike Stump | 8a2b4b1 | 2009-02-25 23:33:13 +0000 | [diff] [blame] | 904 | |
John McCall | ee50429 | 2010-05-21 04:11:14 +0000 | [diff] [blame] | 905 | Info.BlockSize = BlockOffset; |
| 906 | Info.BlockAlign = BlockAlign; |
| 907 | Info.BlockLayout = BlockLayout; |
Fariborz Jahanian | 2715b20 | 2010-11-15 19:19:38 +0000 | [diff] [blame] | 908 | Info.BlockHasCopyDispose = SynthesizeCopyDisposeHelpers; |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 909 | return Fn; |
| 910 | } |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 911 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 912 | CharUnits BlockFunction::getBlockOffset(CharUnits Size, CharUnits Align) { |
| 913 | assert((Align.isPositive()) && "alignment must be 1 byte or more"); |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 914 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 915 | CharUnits OldOffset = BlockOffset; |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 916 | |
| 917 | // Ensure proper alignment, even if it means we have to have a gap |
Ken Dyck | 3dbdb58 | 2011-01-20 01:59:55 +0000 | [diff] [blame^] | 918 | BlockOffset = BlockOffset.RoundUpToAlignment(Align); |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 919 | BlockAlign = std::max(Align, BlockAlign); |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 920 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 921 | CharUnits Pad = BlockOffset - OldOffset; |
| 922 | if (Pad.isPositive()) { |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 923 | QualType PadTy = getContext().getConstantArrayType(getContext().CharTy, |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 924 | llvm::APInt(32, |
| 925 | Pad.getQuantity()), |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 926 | ArrayType::Normal, 0); |
Douglas Gregor | ba55ec2 | 2010-05-11 18:17:16 +0000 | [diff] [blame] | 927 | ValueDecl *PadDecl = VarDecl::Create(getContext(), |
| 928 | getContext().getTranslationUnitDecl(), |
| 929 | SourceLocation(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 930 | 0, QualType(PadTy), 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 931 | SC_None, SC_None); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 932 | Expr *E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 933 | VK_LValue, SourceLocation()); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 934 | BlockLayout.push_back(E); |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 935 | } |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 936 | |
| 937 | BlockOffset += Size; |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 938 | return BlockOffset - Size; |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 939 | } |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 940 | |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 941 | llvm::Constant *BlockFunction:: |
Fariborz Jahanian | 2715b20 | 2010-11-15 19:19:38 +0000 | [diff] [blame] | 942 | GenerateCopyHelperFunction(const llvm::StructType *T, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 943 | std::vector<HelperInfo> *NoteForHelperp) { |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 944 | QualType R = getContext().VoidTy; |
| 945 | |
| 946 | FunctionArgList Args; |
| 947 | // FIXME: This leaks |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 948 | ImplicitParamDecl *Dst = |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 949 | ImplicitParamDecl::Create(getContext(), 0, |
| 950 | SourceLocation(), 0, |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 951 | getContext().getPointerType(getContext().VoidTy)); |
| 952 | Args.push_back(std::make_pair(Dst, Dst->getType())); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 953 | ImplicitParamDecl *Src = |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 954 | ImplicitParamDecl::Create(getContext(), 0, |
| 955 | SourceLocation(), 0, |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 956 | getContext().getPointerType(getContext().VoidTy)); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 957 | Args.push_back(std::make_pair(Src, Src->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 958 | |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 959 | const CGFunctionInfo &FI = |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 960 | CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo()); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 961 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 962 | // FIXME: We'd like to put these into a mergable by content, with |
| 963 | // internal linkage. |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 964 | CodeGenTypes &Types = CGM.getTypes(); |
| 965 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); |
| 966 | |
| 967 | llvm::Function *Fn = |
| 968 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
Benjamin Kramer | 3cf7c5d | 2010-01-22 13:59:13 +0000 | [diff] [blame] | 969 | "__copy_helper_block_", &CGM.getModule()); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 970 | |
| 971 | IdentifierInfo *II |
| 972 | = &CGM.getContext().Idents.get("__copy_helper_block_"); |
| 973 | |
| 974 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 975 | getContext().getTranslationUnitDecl(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 976 | SourceLocation(), II, R, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 977 | SC_Static, |
| 978 | SC_None, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 979 | false, |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 980 | true); |
| 981 | CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 982 | |
| 983 | llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); |
| 984 | llvm::Type *PtrPtrT; |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 985 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 986 | if (NoteForHelperp) { |
| 987 | std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp; |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 988 | |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 989 | PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 990 | SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); |
| 991 | SrcObj = Builder.CreateLoad(SrcObj); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 992 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 993 | llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst); |
Mike Stump | c2f4c34 | 2009-04-15 22:11:36 +0000 | [diff] [blame] | 994 | llvm::Type *PtrPtrT; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 995 | PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); |
Mike Stump | c2f4c34 | 2009-04-15 22:11:36 +0000 | [diff] [blame] | 996 | DstObj = Builder.CreateBitCast(DstObj, PtrPtrT); |
| 997 | DstObj = Builder.CreateLoad(DstObj); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 998 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 999 | for (unsigned i=0; i < NoteForHelper.size(); ++i) { |
| 1000 | int flag = NoteForHelper[i].flag; |
| 1001 | int index = NoteForHelper[i].index; |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1002 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 1003 | if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) |
| 1004 | || NoteForHelper[i].RequiresCopying) { |
| 1005 | llvm::Value *Srcv = SrcObj; |
| 1006 | Srcv = Builder.CreateStructGEP(Srcv, index); |
Fariborz Jahanian | e220455 | 2010-11-16 19:29:39 +0000 | [diff] [blame] | 1007 | llvm::Value *Dstv; |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1008 | if (NoteForHelper[i].cxxvar_import && |
| 1009 | (NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) == 0) { |
| 1010 | // Note that cxx objects declared as __block require a |
| 1011 | // different API. They are copy constructed in their own copy |
| 1012 | // helper functions (see GeneratebyrefCopyHelperFunction). |
| 1013 | Srcv = Builder.CreateBitCast(Srcv, |
| 1014 | llvm::PointerType::get(PtrToInt8Ty, 0)); |
| 1015 | Dstv = Builder.CreateStructGEP(DstObj, index); |
| 1016 | CGF.EmitSynthesizedCXXCopyCtor(Dstv, Srcv, |
| 1017 | NoteForHelper[i].cxxvar_import->getCopyConstructorExpr()); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1018 | } |
| 1019 | else { |
Fariborz Jahanian | e220455 | 2010-11-16 19:29:39 +0000 | [diff] [blame] | 1020 | Srcv = Builder.CreateBitCast(Srcv, |
| 1021 | llvm::PointerType::get(PtrToInt8Ty, 0)); |
| 1022 | Dstv = Builder.CreateStructGEP(DstObj, index); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1023 | Srcv = Builder.CreateLoad(Srcv); |
| 1024 | Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty); |
| 1025 | llvm::Value *N = llvm::ConstantInt::get(CGF.Int32Ty, flag); |
| 1026 | llvm::Value *F = CGM.getBlockObjectAssign(); |
| 1027 | Builder.CreateCall3(F, Dstv, Srcv, N); |
| 1028 | } |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 1029 | } |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1030 | } |
| 1031 | } |
| 1032 | |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1033 | CGF.FinishFunction(); |
| 1034 | |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1035 | return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Mike Stump | cf62d39 | 2009-03-06 18:42:23 +0000 | [diff] [blame] | 1038 | llvm::Constant *BlockFunction:: |
Fariborz Jahanian | 2715b20 | 2010-11-15 19:19:38 +0000 | [diff] [blame] | 1039 | GenerateDestroyHelperFunction(const llvm::StructType* T, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 1040 | std::vector<HelperInfo> *NoteForHelperp) { |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1041 | QualType R = getContext().VoidTy; |
| 1042 | |
| 1043 | FunctionArgList Args; |
| 1044 | // FIXME: This leaks |
| 1045 | ImplicitParamDecl *Src = |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 1046 | ImplicitParamDecl::Create(getContext(), 0, |
| 1047 | SourceLocation(), 0, |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1048 | getContext().getPointerType(getContext().VoidTy)); |
| 1049 | |
| 1050 | Args.push_back(std::make_pair(Src, Src->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1051 | |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1052 | const CGFunctionInfo &FI = |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1053 | CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo()); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1054 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1055 | // FIXME: We'd like to put these into a mergable by content, with |
| 1056 | // internal linkage. |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1057 | CodeGenTypes &Types = CGM.getTypes(); |
| 1058 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); |
| 1059 | |
| 1060 | llvm::Function *Fn = |
| 1061 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
Benjamin Kramer | 3cf7c5d | 2010-01-22 13:59:13 +0000 | [diff] [blame] | 1062 | "__destroy_helper_block_", &CGM.getModule()); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1063 | |
| 1064 | IdentifierInfo *II |
| 1065 | = &CGM.getContext().Idents.get("__destroy_helper_block_"); |
| 1066 | |
| 1067 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 1068 | getContext().getTranslationUnitDecl(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1069 | SourceLocation(), II, R, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1070 | SC_Static, |
| 1071 | SC_None, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1072 | false, true); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1073 | CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 1074 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 1075 | if (NoteForHelperp) { |
| 1076 | std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp; |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 1077 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 1078 | llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src); |
| 1079 | llvm::Type *PtrPtrT; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1080 | PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0); |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 1081 | SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT); |
| 1082 | SrcObj = Builder.CreateLoad(SrcObj); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1083 | EHScopeStack::stable_iterator CleanupDepth = CGF.EHStack.stable_begin(); |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 1084 | for (unsigned i=0; i < NoteForHelper.size(); ++i) { |
| 1085 | int flag = NoteForHelper[i].flag; |
| 1086 | int index = NoteForHelper[i].index; |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 1087 | |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 1088 | if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) |
| 1089 | || NoteForHelper[i].RequiresCopying) { |
| 1090 | llvm::Value *Srcv = SrcObj; |
| 1091 | Srcv = Builder.CreateStructGEP(Srcv, index); |
| 1092 | Srcv = Builder.CreateBitCast(Srcv, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1093 | llvm::PointerType::get(PtrToInt8Ty, 0)); |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1094 | if (NoteForHelper[i].cxxvar_import && |
| 1095 | (NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF) == 0) { |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1096 | const BlockDeclRefExpr *BDRE = NoteForHelper[i].cxxvar_import; |
| 1097 | const Expr *E = BDRE->getCopyConstructorExpr(); |
| 1098 | QualType ClassTy = E->getType(); |
| 1099 | QualType PtrClassTy = getContext().getPointerType(ClassTy); |
| 1100 | const llvm::Type *t = CGM.getTypes().ConvertType(PtrClassTy); |
| 1101 | Srcv = Builder.CreateBitCast(Srcv, t); |
| 1102 | CGF.PushDestructorCleanup(ClassTy, Srcv); |
| 1103 | } |
| 1104 | else { |
| 1105 | Srcv = Builder.CreateLoad(Srcv); |
| 1106 | BuildBlockRelease(Srcv, flag); |
| 1107 | } |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 1108 | } |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 1109 | } |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1110 | CGF.PopCleanupBlocks(CleanupDepth); |
Mike Stump | 1edf6b6 | 2009-03-07 02:53:18 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1113 | CGF.FinishFunction(); |
| 1114 | |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1115 | return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1118 | llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 1119 | std::vector<HelperInfo> *NoteForHelper) { |
Fariborz Jahanian | 2715b20 | 2010-11-15 19:19:38 +0000 | [diff] [blame] | 1120 | return CodeGenFunction(CGM).GenerateCopyHelperFunction(T, NoteForHelper); |
Mike Stump | a4f668f | 2009-03-06 01:33:24 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1123 | llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T, |
Mike Stump | b7477cf | 2009-04-10 18:52:28 +0000 | [diff] [blame] | 1124 | std::vector<HelperInfo> *NoteForHelperp) { |
Fariborz Jahanian | 2715b20 | 2010-11-15 19:19:38 +0000 | [diff] [blame] | 1125 | return CodeGenFunction(CGM).GenerateDestroyHelperFunction(T, NoteForHelperp); |
Mike Stump | dab514f | 2009-03-04 03:23:46 +0000 | [diff] [blame] | 1126 | } |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1127 | |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1128 | llvm::Constant *BlockFunction:: |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1129 | GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag, |
| 1130 | const VarDecl *BD) { |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1131 | QualType R = getContext().VoidTy; |
| 1132 | |
| 1133 | FunctionArgList Args; |
| 1134 | // FIXME: This leaks |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1135 | ImplicitParamDecl *Dst = |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 1136 | ImplicitParamDecl::Create(getContext(), 0, |
| 1137 | SourceLocation(), 0, |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1138 | getContext().getPointerType(getContext().VoidTy)); |
| 1139 | Args.push_back(std::make_pair(Dst, Dst->getType())); |
| 1140 | |
| 1141 | // FIXME: This leaks |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1142 | ImplicitParamDecl *Src = |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 1143 | ImplicitParamDecl::Create(getContext(), 0, |
| 1144 | SourceLocation(), 0, |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1145 | getContext().getPointerType(getContext().VoidTy)); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1146 | Args.push_back(std::make_pair(Src, Src->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1147 | |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1148 | const CGFunctionInfo &FI = |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1149 | CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo()); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1150 | |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1151 | CodeGenTypes &Types = CGM.getTypes(); |
| 1152 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); |
| 1153 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1154 | // FIXME: We'd like to put these into a mergable by content, with |
| 1155 | // internal linkage. |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1156 | llvm::Function *Fn = |
| 1157 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1158 | "__Block_byref_object_copy_", &CGM.getModule()); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1159 | |
| 1160 | IdentifierInfo *II |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1161 | = &CGM.getContext().Idents.get("__Block_byref_object_copy_"); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1162 | |
| 1163 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 1164 | getContext().getTranslationUnitDecl(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1165 | SourceLocation(), II, R, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1166 | SC_Static, |
| 1167 | SC_None, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1168 | false, true); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1169 | CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1170 | |
| 1171 | // dst->x |
| 1172 | llvm::Value *V = CGF.GetAddrOfLocalVar(Dst); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1173 | V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0)); |
Mike Stump | c2f4c34 | 2009-04-15 22:11:36 +0000 | [diff] [blame] | 1174 | V = Builder.CreateLoad(V); |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1175 | V = Builder.CreateStructGEP(V, 6, "x"); |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1176 | llvm::Value *DstObj = V; |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1177 | |
| 1178 | // src->x |
| 1179 | V = CGF.GetAddrOfLocalVar(Src); |
| 1180 | V = Builder.CreateLoad(V); |
| 1181 | V = Builder.CreateBitCast(V, T); |
| 1182 | V = Builder.CreateStructGEP(V, 6, "x"); |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1183 | |
| 1184 | if (flag & BLOCK_HAS_CXX_OBJ) { |
| 1185 | assert (BD && "VarDecl is null - GeneratebyrefCopyHelperFunction"); |
| 1186 | llvm::Value *SrcObj = V; |
| 1187 | CGF.EmitSynthesizedCXXCopyCtor(DstObj, SrcObj, |
| 1188 | getContext().getBlockVarCopyInits(BD)); |
| 1189 | } |
| 1190 | else { |
| 1191 | DstObj = Builder.CreateBitCast(DstObj, PtrToInt8Ty); |
| 1192 | V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); |
| 1193 | llvm::Value *SrcObj = Builder.CreateLoad(V); |
| 1194 | flag |= BLOCK_BYREF_CALLER; |
| 1195 | llvm::Value *N = llvm::ConstantInt::get(CGF.Int32Ty, flag); |
| 1196 | llvm::Value *F = CGM.getBlockObjectAssign(); |
| 1197 | Builder.CreateCall3(F, DstObj, SrcObj, N); |
| 1198 | } |
| 1199 | |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1200 | CGF.FinishFunction(); |
| 1201 | |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1202 | return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1205 | llvm::Constant * |
| 1206 | BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T, |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1207 | int flag, |
| 1208 | const VarDecl *BD) { |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1209 | QualType R = getContext().VoidTy; |
| 1210 | |
| 1211 | FunctionArgList Args; |
| 1212 | // FIXME: This leaks |
| 1213 | ImplicitParamDecl *Src = |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 1214 | ImplicitParamDecl::Create(getContext(), 0, |
| 1215 | SourceLocation(), 0, |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1216 | getContext().getPointerType(getContext().VoidTy)); |
| 1217 | |
| 1218 | Args.push_back(std::make_pair(Src, Src->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1219 | |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1220 | const CGFunctionInfo &FI = |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1221 | CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo()); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1222 | |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1223 | CodeGenTypes &Types = CGM.getTypes(); |
| 1224 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false); |
| 1225 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1226 | // FIXME: We'd like to put these into a mergable by content, with |
| 1227 | // internal linkage. |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1228 | llvm::Function *Fn = |
| 1229 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1230 | "__Block_byref_object_dispose_", |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1231 | &CGM.getModule()); |
| 1232 | |
| 1233 | IdentifierInfo *II |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1234 | = &CGM.getContext().Idents.get("__Block_byref_object_dispose_"); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1235 | |
| 1236 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 1237 | getContext().getTranslationUnitDecl(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1238 | SourceLocation(), II, R, 0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1239 | SC_Static, |
| 1240 | SC_None, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1241 | false, true); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1242 | CGF.StartFunction(FD, R, Fn, Args, SourceLocation()); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1243 | |
| 1244 | llvm::Value *V = CGF.GetAddrOfLocalVar(Src); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1245 | V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0)); |
Mike Stump | c2f4c34 | 2009-04-15 22:11:36 +0000 | [diff] [blame] | 1246 | V = Builder.CreateLoad(V); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1247 | V = Builder.CreateStructGEP(V, 6, "x"); |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1248 | if (flag & BLOCK_HAS_CXX_OBJ) { |
| 1249 | EHScopeStack::stable_iterator CleanupDepth = CGF.EHStack.stable_begin(); |
| 1250 | assert (BD && "VarDecl is null - GeneratebyrefDestroyHelperFunction"); |
| 1251 | QualType ClassTy = BD->getType(); |
| 1252 | CGF.PushDestructorCleanup(ClassTy, V); |
| 1253 | CGF.PopCleanupBlocks(CleanupDepth); |
| 1254 | } |
| 1255 | else { |
| 1256 | V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0)); |
| 1257 | V = Builder.CreateLoad(V); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1258 | |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1259 | flag |= BLOCK_BYREF_CALLER; |
| 1260 | BuildBlockRelease(V, flag); |
| 1261 | } |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1262 | CGF.FinishFunction(); |
| 1263 | |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1264 | return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
Mike Stump | ee09422 | 2009-03-06 06:12:24 +0000 | [diff] [blame] | 1267 | llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T, |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1268 | int Flag, unsigned Align, |
| 1269 | const VarDecl *BD) { |
Chris Lattner | 10976d9 | 2009-12-05 08:21:30 +0000 | [diff] [blame] | 1270 | // All alignments below that of pointer alignment collapse down to just |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1271 | // pointer alignment, as we always have at least that much alignment to begin |
| 1272 | // with. |
| 1273 | Align /= unsigned(CGF.Target.getPointerAlign(0)/8); |
Chris Lattner | 10976d9 | 2009-12-05 08:21:30 +0000 | [diff] [blame] | 1274 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1275 | // As an optimization, we only generate a single function of each kind we |
| 1276 | // might need. We need a different one for each alignment and for each |
| 1277 | // setting of flags. We mix Align and flag to get the kind. |
Chris Lattner | 10976d9 | 2009-12-05 08:21:30 +0000 | [diff] [blame] | 1278 | uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag; |
| 1279 | llvm::Constant *&Entry = CGM.AssignCache[Kind]; |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1280 | if (Entry) |
| 1281 | return Entry; |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1282 | return Entry = |
| 1283 | CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, Flag, BD); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1286 | llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T, |
Chris Lattner | 10976d9 | 2009-12-05 08:21:30 +0000 | [diff] [blame] | 1287 | int Flag, |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1288 | unsigned Align, |
| 1289 | const VarDecl *BD) { |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1290 | // All alignments below that of pointer alignment collpase down to just |
| 1291 | // pointer alignment, as we always have at least that much alignment to begin |
| 1292 | // with. |
| 1293 | Align /= unsigned(CGF.Target.getPointerAlign(0)/8); |
Chris Lattner | 10976d9 | 2009-12-05 08:21:30 +0000 | [diff] [blame] | 1294 | |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1295 | // As an optimization, we only generate a single function of each kind we |
| 1296 | // might need. We need a different one for each alignment and for each |
| 1297 | // setting of flags. We mix Align and flag to get the kind. |
Chris Lattner | 10976d9 | 2009-12-05 08:21:30 +0000 | [diff] [blame] | 1298 | uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag; |
| 1299 | llvm::Constant *&Entry = CGM.DestroyCache[Kind]; |
Mike Stump | 3899a7f | 2009-06-05 23:26:36 +0000 | [diff] [blame] | 1300 | if (Entry) |
| 1301 | return Entry; |
Fariborz Jahanian | 830937b | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1302 | return Entry = |
| 1303 | CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, Flag, BD); |
Mike Stump | 45031c0 | 2009-03-06 02:29:21 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1306 | void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) { |
Daniel Dunbar | 673431a | 2010-07-16 00:00:15 +0000 | [diff] [blame] | 1307 | llvm::Value *F = CGM.getBlockObjectDispose(); |
Mike Stump | 1851b68 | 2009-03-06 04:53:30 +0000 | [diff] [blame] | 1308 | llvm::Value *N; |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1309 | V = Builder.CreateBitCast(V, PtrToInt8Ty); |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1310 | N = llvm::ConstantInt::get(CGF.Int32Ty, flag); |
Mike Stump | 797b632 | 2009-03-05 01:23:13 +0000 | [diff] [blame] | 1311 | Builder.CreateCall2(F, V, N); |
| 1312 | } |
Mike Stump | 00470a1 | 2009-03-05 08:32:30 +0000 | [diff] [blame] | 1313 | |
| 1314 | ASTContext &BlockFunction::getContext() const { return CGM.getContext(); } |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1315 | |
| 1316 | BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf, |
| 1317 | CGBuilderTy &B) |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1318 | : CGM(cgm), VMContext(cgm.getLLVMContext()), CGF(cgf), Builder(B) { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1319 | PtrToInt8Ty = llvm::PointerType::getUnqual( |
| 1320 | llvm::Type::getInt8Ty(VMContext)); |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1321 | |
Fariborz Jahanian | 2715b20 | 2010-11-15 19:19:38 +0000 | [diff] [blame] | 1322 | SynthesizeCopyDisposeHelpers = false; |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1323 | } |