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