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